The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/nfsclient/nfs_vnops.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * Rick Macklem at The University of Guelph.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/6.3/sys/nfsclient/nfs_vnops.c 170553 2007-06-11 10:53:48Z kib $");
   37 
   38 /*
   39  * vnode op calls for Sun NFS version 2 and 3
   40  */
   41 
   42 #include "opt_inet.h"
   43 
   44 #include <sys/param.h>
   45 #include <sys/kernel.h>
   46 #include <sys/systm.h>
   47 #include <sys/resourcevar.h>
   48 #include <sys/proc.h>
   49 #include <sys/mount.h>
   50 #include <sys/bio.h>
   51 #include <sys/buf.h>
   52 #include <sys/malloc.h>
   53 #include <sys/mbuf.h>
   54 #include <sys/namei.h>
   55 #include <sys/socket.h>
   56 #include <sys/vnode.h>
   57 #include <sys/dirent.h>
   58 #include <sys/fcntl.h>
   59 #include <sys/lockf.h>
   60 #include <sys/stat.h>
   61 #include <sys/sysctl.h>
   62 #include <sys/signalvar.h>
   63 
   64 #include <vm/vm.h>
   65 #include <vm/vm_object.h>
   66 #include <vm/vm_extern.h>
   67 #include <vm/vm_object.h>
   68 
   69 #include <fs/fifofs/fifo.h>
   70 
   71 #include <rpc/rpcclnt.h>
   72 
   73 #include <nfs/rpcv2.h>
   74 #include <nfs/nfsproto.h>
   75 #include <nfsclient/nfs.h>
   76 #include <nfsclient/nfsnode.h>
   77 #include <nfsclient/nfsmount.h>
   78 #include <nfsclient/nfs_lock.h>
   79 #include <nfs/xdr_subs.h>
   80 #include <nfsclient/nfsm_subs.h>
   81 
   82 #include <net/if.h>
   83 #include <netinet/in.h>
   84 #include <netinet/in_var.h>
   85 
   86 /* Defs */
   87 #define TRUE    1
   88 #define FALSE   0
   89 
   90 /*
   91  * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
   92  * calls are not in getblk() and brelse() so that they would not be necessary
   93  * here.
   94  */
   95 #ifndef B_VMIO
   96 #define vfs_busy_pages(bp, f)
   97 #endif
   98 
   99 static vop_read_t       nfsfifo_read;
  100 static vop_write_t      nfsfifo_write;
  101 static vop_close_t      nfsfifo_close;
  102 static int      nfs_flush(struct vnode *, int, struct thread *,
  103                     int);
  104 static int      nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
  105                     struct thread *);
  106 static vop_lookup_t     nfs_lookup;
  107 static vop_create_t     nfs_create;
  108 static vop_mknod_t      nfs_mknod;
  109 static vop_open_t       nfs_open;
  110 static vop_close_t      nfs_close;
  111 static vop_access_t     nfs_access;
  112 static vop_getattr_t    nfs_getattr;
  113 static vop_setattr_t    nfs_setattr;
  114 static vop_read_t       nfs_read;
  115 static vop_fsync_t      nfs_fsync;
  116 static vop_remove_t     nfs_remove;
  117 static vop_link_t       nfs_link;
  118 static vop_rename_t     nfs_rename;
  119 static vop_mkdir_t      nfs_mkdir;
  120 static vop_rmdir_t      nfs_rmdir;
  121 static vop_symlink_t    nfs_symlink;
  122 static vop_readdir_t    nfs_readdir;
  123 static vop_strategy_t   nfs_strategy;
  124 static  int     nfs_lookitup(struct vnode *, const char *, int,
  125                     struct ucred *, struct thread *, struct nfsnode **);
  126 static  int     nfs_sillyrename(struct vnode *, struct vnode *,
  127                     struct componentname *);
  128 static vop_access_t     nfsspec_access;
  129 static vop_readlink_t   nfs_readlink;
  130 static vop_print_t      nfs_print;
  131 static vop_advlock_t    nfs_advlock;
  132 
  133 /*
  134  * Global vfs data structures for nfs
  135  */
  136 struct vop_vector nfs_vnodeops = {
  137         .vop_default =          &default_vnodeops,
  138         .vop_access =           nfs_access,
  139         .vop_advlock =          nfs_advlock,
  140         .vop_close =            nfs_close,
  141         .vop_create =           nfs_create,
  142         .vop_fsync =            nfs_fsync,
  143         .vop_getattr =          nfs_getattr,
  144         .vop_getpages =         nfs_getpages,
  145         .vop_putpages =         nfs_putpages,
  146         .vop_inactive =         nfs_inactive,
  147         .vop_lease =            VOP_NULL,
  148         .vop_link =             nfs_link,
  149         .vop_lookup =           nfs_lookup,
  150         .vop_mkdir =            nfs_mkdir,
  151         .vop_mknod =            nfs_mknod,
  152         .vop_open =             nfs_open,
  153         .vop_print =            nfs_print,
  154         .vop_read =             nfs_read,
  155         .vop_readdir =          nfs_readdir,
  156         .vop_readlink =         nfs_readlink,
  157         .vop_reclaim =          nfs_reclaim,
  158         .vop_remove =           nfs_remove,
  159         .vop_rename =           nfs_rename,
  160         .vop_rmdir =            nfs_rmdir,
  161         .vop_setattr =          nfs_setattr,
  162         .vop_strategy =         nfs_strategy,
  163         .vop_symlink =          nfs_symlink,
  164         .vop_write =            nfs_write,
  165 };
  166 
  167 struct vop_vector nfs_fifoops = {
  168         .vop_default =          &fifo_specops,
  169         .vop_access =           nfsspec_access,
  170         .vop_close =            nfsfifo_close,
  171         .vop_fsync =            nfs_fsync,
  172         .vop_getattr =          nfs_getattr,
  173         .vop_inactive =         nfs_inactive,
  174         .vop_print =            nfs_print,
  175         .vop_read =             nfsfifo_read,
  176         .vop_reclaim =          nfs_reclaim,
  177         .vop_setattr =          nfs_setattr,
  178         .vop_write =            nfsfifo_write,
  179 };
  180 
  181 static int      nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
  182                              struct componentname *cnp, struct vattr *vap);
  183 static int      nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
  184                               struct ucred *cred, struct thread *td);
  185 static int      nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
  186                               int fnamelen, struct vnode *tdvp,
  187                               const char *tnameptr, int tnamelen,
  188                               struct ucred *cred, struct thread *td);
  189 static int      nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
  190                              struct sillyrename *sp);
  191 
  192 /*
  193  * Global variables
  194  */
  195 struct proc     *nfs_iodwant[NFS_MAXASYNCDAEMON];
  196 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
  197 int              nfs_numasync = 0;
  198 #define DIRHDSIZ        (sizeof (struct dirent) - (MAXNAMLEN + 1))
  199 
  200 SYSCTL_DECL(_vfs_nfs);
  201 
  202 static int      nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
  203 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
  204            &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
  205 
  206 static int      nfsv3_commit_on_close = 0;
  207 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
  208            &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
  209 
  210 static int      nfs_clean_pages_on_close = 1;
  211 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
  212            &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
  213 
  214 int nfs_directio_enable = 0;
  215 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
  216            &nfs_directio_enable, 0, "Enable NFS directio");
  217 
  218 /*
  219  * This sysctl allows other processes to mmap a file that has been opened
  220  * O_DIRECT by a process.  In general, having processes mmap the file while
  221  * Direct IO is in progress can lead to Data Inconsistencies.  But, we allow
  222  * this by default to prevent DoS attacks - to prevent a malicious user from
  223  * opening up files O_DIRECT preventing other users from mmap'ing these
  224  * files.  "Protected" environments where stricter consistency guarantees are
  225  * required can disable this knob.  The process that opened the file O_DIRECT
  226  * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
  227  * meaningful.
  228  */
  229 int nfs_directio_allow_mmap = 1;
  230 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
  231            &nfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
  232 
  233 #if 0
  234 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
  235            &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
  236 
  237 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
  238            &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
  239 #endif
  240 
  241 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY          \
  242                          | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE     \
  243                          | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
  244 static int
  245 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
  246     struct ucred *cred)
  247 {
  248         const int v3 = 1;
  249         u_int32_t *tl;
  250         int error = 0, attrflag;
  251 
  252         struct mbuf *mreq, *mrep, *md, *mb;
  253         caddr_t bpos, dpos;
  254         u_int32_t rmode;
  255         struct nfsnode *np = VTONFS(vp);
  256 
  257         nfsstats.rpccnt[NFSPROC_ACCESS]++;
  258         mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
  259         mb = mreq;
  260         bpos = mtod(mb, caddr_t);
  261         nfsm_fhtom(vp, v3);
  262         tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
  263         *tl = txdr_unsigned(wmode);
  264         nfsm_request(vp, NFSPROC_ACCESS, td, cred);
  265         nfsm_postop_attr(vp, attrflag);
  266         if (!error) {
  267                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
  268                 rmode = fxdr_unsigned(u_int32_t, *tl);
  269                 np->n_mode = rmode;
  270                 np->n_modeuid = cred->cr_uid;
  271                 np->n_modestamp = time_second;
  272         }
  273         m_freem(mrep);
  274 nfsmout:
  275         return (error);
  276 }
  277 
  278 /*
  279  * nfs access vnode op.
  280  * For nfs version 2, just return ok. File accesses may fail later.
  281  * For nfs version 3, use the access rpc to check accessibility. If file modes
  282  * are changed on the server, accesses might still fail later.
  283  */
  284 static int
  285 nfs_access(struct vop_access_args *ap)
  286 {
  287         struct vnode *vp = ap->a_vp;
  288         int error = 0;
  289         u_int32_t mode, wmode;
  290         int v3 = NFS_ISV3(vp);
  291         struct nfsnode *np = VTONFS(vp);
  292 
  293         /*
  294          * Disallow write attempts on filesystems mounted read-only;
  295          * unless the file is a socket, fifo, or a block or character
  296          * device resident on the filesystem.
  297          */
  298         if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
  299                 switch (vp->v_type) {
  300                 case VREG:
  301                 case VDIR:
  302                 case VLNK:
  303                         return (EROFS);
  304                 default:
  305                         break;
  306                 }
  307         }
  308         /*
  309          * For nfs v3, check to see if we have done this recently, and if
  310          * so return our cached result instead of making an ACCESS call.
  311          * If not, do an access rpc, otherwise you are stuck emulating
  312          * ufs_access() locally using the vattr. This may not be correct,
  313          * since the server may apply other access criteria such as
  314          * client uid-->server uid mapping that we do not know about.
  315          */
  316         if (v3) {
  317                 if (ap->a_mode & VREAD)
  318                         mode = NFSV3ACCESS_READ;
  319                 else
  320                         mode = 0;
  321                 if (vp->v_type != VDIR) {
  322                         if (ap->a_mode & VWRITE)
  323                                 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
  324                         if (ap->a_mode & VEXEC)
  325                                 mode |= NFSV3ACCESS_EXECUTE;
  326                 } else {
  327                         if (ap->a_mode & VWRITE)
  328                                 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
  329                                          NFSV3ACCESS_DELETE);
  330                         if (ap->a_mode & VEXEC)
  331                                 mode |= NFSV3ACCESS_LOOKUP;
  332                 }
  333                 /* XXX safety belt, only make blanket request if caching */
  334                 if (nfsaccess_cache_timeout > 0) {
  335                         wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
  336                                 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
  337                                 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
  338                 } else {
  339                         wmode = mode;
  340                 }
  341 
  342                 /*
  343                  * Does our cached result allow us to give a definite yes to
  344                  * this request?
  345                  */
  346                 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
  347                     (ap->a_cred->cr_uid == np->n_modeuid) &&
  348                     ((np->n_mode & mode) == mode)) {
  349                         nfsstats.accesscache_hits++;
  350                 } else {
  351                         /*
  352                          * Either a no, or a don't know.  Go to the wire.
  353                          */
  354                         nfsstats.accesscache_misses++;
  355                         error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
  356                         if (!error) {
  357                                 if ((np->n_mode & mode) != mode) {
  358                                         error = EACCES;
  359                                 }
  360                         }
  361                 }
  362                 return (error);
  363         } else {
  364                 if ((error = nfsspec_access(ap)) != 0)
  365                         return (error);
  366 
  367                 /*
  368                  * Attempt to prevent a mapped root from accessing a file
  369                  * which it shouldn't.  We try to read a byte from the file
  370                  * if the user is root and the file is not zero length.
  371                  * After calling nfsspec_access, we should have the correct
  372                  * file size cached.
  373                  */
  374                 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
  375                     && VTONFS(vp)->n_size > 0) {
  376                         struct iovec aiov;
  377                         struct uio auio;
  378                         char buf[1];
  379 
  380                         aiov.iov_base = buf;
  381                         aiov.iov_len = 1;
  382                         auio.uio_iov = &aiov;
  383                         auio.uio_iovcnt = 1;
  384                         auio.uio_offset = 0;
  385                         auio.uio_resid = 1;
  386                         auio.uio_segflg = UIO_SYSSPACE;
  387                         auio.uio_rw = UIO_READ;
  388                         auio.uio_td = ap->a_td;
  389 
  390                         if (vp->v_type == VREG)
  391                                 error = nfs_readrpc(vp, &auio, ap->a_cred);
  392                         else if (vp->v_type == VDIR) {
  393                                 char* bp;
  394                                 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
  395                                 aiov.iov_base = bp;
  396                                 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
  397                                 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
  398                                 free(bp, M_TEMP);
  399                         } else if (vp->v_type == VLNK)
  400                                 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
  401                         else
  402                                 error = EACCES;
  403                 }
  404                 return (error);
  405         }
  406 }
  407 
  408 /*
  409  * nfs open vnode op
  410  * Check to see if the type is ok
  411  * and that deletion is not in progress.
  412  * For paged in text files, you will need to flush the page cache
  413  * if consistency is lost.
  414  */
  415 /* ARGSUSED */
  416 static int
  417 nfs_open(struct vop_open_args *ap)
  418 {
  419         struct vnode *vp = ap->a_vp;
  420         struct nfsnode *np = VTONFS(vp);
  421         struct vattr vattr;
  422         int error;
  423         int fmode = ap->a_mode;
  424 
  425         if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
  426                 return (EOPNOTSUPP);
  427 
  428         /*
  429          * Get a valid lease. If cached data is stale, flush it.
  430          */
  431         if (np->n_flag & NMODIFIED) {
  432                 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
  433                 if (error == EINTR || error == EIO)
  434                         return (error);
  435                 np->n_attrstamp = 0;
  436                 if (vp->v_type == VDIR)
  437                         np->n_direofoffset = 0;
  438                 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
  439                 if (error)
  440                         return (error);
  441                 np->n_mtime = vattr.va_mtime;
  442         } else {
  443                 np->n_attrstamp = 0;
  444                 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
  445                 if (error)
  446                         return (error);
  447                 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
  448                         if (vp->v_type == VDIR)
  449                                 np->n_direofoffset = 0;
  450                         error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
  451                         if (error == EINTR || error == EIO)
  452                                 return (error);
  453                         np->n_mtime = vattr.va_mtime;
  454                 }
  455         }
  456         /*
  457          * If the object has >= 1 O_DIRECT active opens, we disable caching.
  458          */
  459         if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
  460                 if (np->n_directio_opens == 0) {
  461                         error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
  462                         if (error)
  463                                 return (error);
  464                         np->n_flag |= NNONCACHE;
  465                 }
  466                 np->n_directio_opens++;
  467         }
  468         np->ra_expect_lbn = 0;
  469         vnode_create_vobject_off(vp, vattr.va_size, ap->a_td);
  470         return (0);
  471 }
  472 
  473 /*
  474  * nfs close vnode op
  475  * What an NFS client should do upon close after writing is a debatable issue.
  476  * Most NFS clients push delayed writes to the server upon close, basically for
  477  * two reasons:
  478  * 1 - So that any write errors may be reported back to the client process
  479  *     doing the close system call. By far the two most likely errors are
  480  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
  481  * 2 - To put a worst case upper bound on cache inconsistency between
  482  *     multiple clients for the file.
  483  * There is also a consistency problem for Version 2 of the protocol w.r.t.
  484  * not being able to tell if other clients are writing a file concurrently,
  485  * since there is no way of knowing if the changed modify time in the reply
  486  * is only due to the write for this client.
  487  * (NFS Version 3 provides weak cache consistency data in the reply that
  488  *  should be sufficient to detect and handle this case.)
  489  *
  490  * The current code does the following:
  491  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
  492  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
  493  *                     or commit them (this satisfies 1 and 2 except for the
  494  *                     case where the server crashes after this close but
  495  *                     before the commit RPC, which is felt to be "good
  496  *                     enough". Changing the last argument to nfs_flush() to
  497  *                     a 1 would force a commit operation, if it is felt a
  498  *                     commit is necessary now.
  499  */
  500 /* ARGSUSED */
  501 static int
  502 nfs_close(struct vop_close_args *ap)
  503 {
  504         struct vnode *vp = ap->a_vp;
  505         struct nfsnode *np = VTONFS(vp);
  506         int error = 0;
  507         int fmode = ap->a_fflag;
  508 
  509         if (vp->v_type == VREG) {
  510             /*
  511              * Examine and clean dirty pages, regardless of NMODIFIED.
  512              * This closes a major hole in close-to-open consistency.
  513              * We want to push out all dirty pages (and buffers) on
  514              * close, regardless of whether they were dirtied by
  515              * mmap'ed writes or via write().
  516              */
  517             if (nfs_clean_pages_on_close && vp->v_object) {
  518                 VM_OBJECT_LOCK(vp->v_object);
  519                 vm_object_page_clean(vp->v_object, 0, 0, 0);
  520                 VM_OBJECT_UNLOCK(vp->v_object);
  521             }
  522             if (np->n_flag & NMODIFIED) {
  523                 if (NFS_ISV3(vp)) {
  524                     /*
  525                      * Under NFSv3 we have dirty buffers to dispose of.  We
  526                      * must flush them to the NFS server.  We have the option
  527                      * of waiting all the way through the commit rpc or just
  528                      * waiting for the initial write.  The default is to only
  529                      * wait through the initial write so the data is in the
  530                      * server's cache, which is roughly similar to the state
  531                      * a standard disk subsystem leaves the file in on close().
  532                      *
  533                      * We cannot clear the NMODIFIED bit in np->n_flag due to
  534                      * potential races with other processes, and certainly
  535                      * cannot clear it if we don't commit.
  536                      */
  537                     int cm = nfsv3_commit_on_close ? 1 : 0;
  538                     error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
  539                     /* np->n_flag &= ~NMODIFIED; */
  540                 } else
  541                     error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
  542             }
  543             /* 
  544              * Invalidate the attribute cache in all cases.
  545              * An open is going to fetch fresh attrs any way, other procs
  546              * on this node that have file open will be forced to do an 
  547              * otw attr fetch, but this is safe.
  548              */
  549             np->n_attrstamp = 0;
  550             if (np->n_flag & NWRITEERR) {
  551                 np->n_flag &= ~NWRITEERR;
  552                 error = np->n_error;
  553             }
  554         }
  555         if (nfs_directio_enable)
  556                 KASSERT((np->n_directio_asyncwr == 0),
  557                         ("nfs_close: dirty unflushed (%d) directio buffers\n",
  558                          np->n_directio_asyncwr));
  559         if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
  560                 KASSERT((np->n_directio_opens > 0), 
  561                         ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));           
  562                 np->n_directio_opens--;
  563                 if (np->n_directio_opens == 0)
  564                         np->n_flag &= ~NNONCACHE;
  565         }
  566         return (error);
  567 }
  568 
  569 /*
  570  * nfs getattr call from vfs.
  571  */
  572 static int
  573 nfs_getattr(struct vop_getattr_args *ap)
  574 {
  575         struct vnode *vp = ap->a_vp;
  576         struct nfsnode *np = VTONFS(vp);
  577         caddr_t bpos, dpos;
  578         int error = 0;
  579         struct mbuf *mreq, *mrep, *md, *mb;
  580         int v3 = NFS_ISV3(vp);
  581 
  582         /*
  583          * Update local times for special files.
  584          */
  585         if (np->n_flag & (NACC | NUPD))
  586                 np->n_flag |= NCHG;
  587         /*
  588          * First look in the cache.
  589          */
  590         if (nfs_getattrcache(vp, ap->a_vap) == 0)
  591                 return (0);
  592 
  593         if (v3 && nfsaccess_cache_timeout > 0) {
  594                 nfsstats.accesscache_misses++;
  595                 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, ap->a_cred);
  596                 if (nfs_getattrcache(vp, ap->a_vap) == 0)
  597                         return (0);
  598         }
  599 
  600         nfsstats.rpccnt[NFSPROC_GETATTR]++;
  601         mreq = nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
  602         mb = mreq;
  603         bpos = mtod(mb, caddr_t);
  604         nfsm_fhtom(vp, v3);
  605         nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, ap->a_cred);
  606         if (!error) {
  607                 nfsm_loadattr(vp, ap->a_vap);
  608         }
  609         m_freem(mrep);
  610 nfsmout:
  611         return (error);
  612 }
  613 
  614 /*
  615  * nfs setattr call.
  616  */
  617 static int
  618 nfs_setattr(struct vop_setattr_args *ap)
  619 {
  620         struct vnode *vp = ap->a_vp;
  621         struct nfsnode *np = VTONFS(vp);
  622         struct vattr *vap = ap->a_vap;
  623         int error = 0;
  624         u_quad_t tsize;
  625 
  626 #ifndef nolint
  627         tsize = (u_quad_t)0;
  628 #endif
  629 
  630         /*
  631          * Setting of flags and marking of atimes are not supported.
  632          */
  633         if (vap->va_flags != VNOVAL || (vap->va_vaflags & VA_MARK_ATIME))
  634                 return (EOPNOTSUPP);
  635 
  636         /*
  637          * Disallow write attempts if the filesystem is mounted read-only.
  638          */
  639         if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
  640             vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
  641             vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
  642             (vp->v_mount->mnt_flag & MNT_RDONLY))
  643                 return (EROFS);
  644         if (vap->va_size != VNOVAL) {
  645                 switch (vp->v_type) {
  646                 case VDIR:
  647                         return (EISDIR);
  648                 case VCHR:
  649                 case VBLK:
  650                 case VSOCK:
  651                 case VFIFO:
  652                         if (vap->va_mtime.tv_sec == VNOVAL &&
  653                             vap->va_atime.tv_sec == VNOVAL &&
  654                             vap->va_mode == (mode_t)VNOVAL &&
  655                             vap->va_uid == (uid_t)VNOVAL &&
  656                             vap->va_gid == (gid_t)VNOVAL)
  657                                 return (0);
  658                         vap->va_size = VNOVAL;
  659                         break;
  660                 default:
  661                         /*
  662                          * Disallow write attempts if the filesystem is
  663                          * mounted read-only.
  664                          */
  665                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
  666                                 return (EROFS);
  667 
  668                         /*
  669                          *  We run vnode_pager_setsize() early (why?),
  670                          * we must set np->n_size now to avoid vinvalbuf
  671                          * V_SAVE races that might setsize a lower
  672                          * value.
  673                          */
  674 
  675                         tsize = np->n_size;
  676                         error = nfs_meta_setsize(vp, ap->a_cred, 
  677                                                 ap->a_td, vap->va_size);
  678 
  679                         if (np->n_flag & NMODIFIED) {
  680                             if (vap->va_size == 0)
  681                                 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
  682                             else
  683                                 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
  684                             if (error) {
  685                                 vnode_pager_setsize(vp, np->n_size);
  686                                 return (error);
  687                             }
  688                         }
  689                         /*
  690                          * np->n_size has already been set to vap->va_size
  691                          * in nfs_meta_setsize(). We must set it again since
  692                          * nfs_loadattrcache() could be called through
  693                          * nfs_meta_setsize() and could modify np->n_size.
  694                          */
  695                         np->n_vattr.va_size = np->n_size = vap->va_size;
  696                 };
  697         } else if ((vap->va_mtime.tv_sec != VNOVAL ||
  698                 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
  699                 vp->v_type == VREG &&
  700                 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) != 0 &&
  701                     (error == EINTR || error == EIO))
  702                 return (error);
  703         error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
  704         if (error && vap->va_size != VNOVAL) {
  705                 np->n_size = np->n_vattr.va_size = tsize;
  706                 vnode_pager_setsize(vp, np->n_size);
  707         }
  708         return (error);
  709 }
  710 
  711 /*
  712  * Do an nfs setattr rpc.
  713  */
  714 static int
  715 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
  716     struct thread *td)
  717 {
  718         struct nfsv2_sattr *sp;
  719         struct nfsnode *np = VTONFS(vp);
  720         caddr_t bpos, dpos;
  721         u_int32_t *tl;
  722         int error = 0, wccflag = NFSV3_WCCRATTR;
  723         struct mbuf *mreq, *mrep, *md, *mb;
  724         int v3 = NFS_ISV3(vp);
  725 
  726         nfsstats.rpccnt[NFSPROC_SETATTR]++;
  727         mreq = nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
  728         mb = mreq;
  729         bpos = mtod(mb, caddr_t);
  730         nfsm_fhtom(vp, v3);
  731         if (v3) {
  732                 nfsm_v3attrbuild(vap, TRUE);
  733                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
  734                 *tl = nfs_false;
  735         } else {
  736                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
  737                 if (vap->va_mode == (mode_t)VNOVAL)
  738                         sp->sa_mode = nfs_xdrneg1;
  739                 else
  740                         sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
  741                 if (vap->va_uid == (uid_t)VNOVAL)
  742                         sp->sa_uid = nfs_xdrneg1;
  743                 else
  744                         sp->sa_uid = txdr_unsigned(vap->va_uid);
  745                 if (vap->va_gid == (gid_t)VNOVAL)
  746                         sp->sa_gid = nfs_xdrneg1;
  747                 else
  748                         sp->sa_gid = txdr_unsigned(vap->va_gid);
  749                 sp->sa_size = txdr_unsigned(vap->va_size);
  750                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
  751                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
  752         }
  753         nfsm_request(vp, NFSPROC_SETATTR, td, cred);
  754         if (v3) {
  755                 np->n_modestamp = 0;
  756                 nfsm_wcc_data(vp, wccflag);
  757         } else
  758                 nfsm_loadattr(vp, NULL);
  759         m_freem(mrep);
  760 nfsmout:
  761         return (error);
  762 }
  763 
  764 /*
  765  * nfs lookup call, one step at a time...
  766  * First look in cache
  767  * If not found, unlock the directory nfsnode and do the rpc
  768  */
  769 static int
  770 nfs_lookup(struct vop_lookup_args *ap)
  771 {
  772         struct componentname *cnp = ap->a_cnp;
  773         struct vnode *dvp = ap->a_dvp;
  774         struct vnode **vpp = ap->a_vpp;
  775         int flags = cnp->cn_flags;
  776         struct vnode *newvp;
  777         struct nfsmount *nmp;
  778         caddr_t bpos, dpos;
  779         struct mbuf *mreq, *mrep, *md, *mb;
  780         long len;
  781         nfsfh_t *fhp;
  782         struct nfsnode *np;
  783         int error = 0, attrflag, fhsize;
  784         int v3 = NFS_ISV3(dvp);
  785         struct thread *td = cnp->cn_thread;
  786 
  787         *vpp = NULLVP;
  788         if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
  789             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
  790                 return (EROFS);
  791         if (dvp->v_type != VDIR)
  792                 return (ENOTDIR);
  793         nmp = VFSTONFS(dvp->v_mount);
  794         np = VTONFS(dvp);
  795         if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
  796                 *vpp = NULLVP;
  797                 return (error);
  798         }
  799         if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
  800                 struct vattr vattr;
  801 
  802                 newvp = *vpp;
  803                 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
  804                  && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
  805                      nfsstats.lookupcache_hits++;
  806                      if (cnp->cn_nameiop != LOOKUP &&
  807                          (flags & ISLASTCN))
  808                              cnp->cn_flags |= SAVENAME;
  809                      return (0);
  810                 }
  811                 cache_purge(newvp);
  812                 if (dvp != newvp)
  813                         vput(newvp);
  814                 else 
  815                         vrele(newvp);
  816                 *vpp = NULLVP;
  817         }
  818         error = 0;
  819         newvp = NULLVP;
  820         nfsstats.lookupcache_misses++;
  821         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
  822         len = cnp->cn_namelen;
  823         mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
  824                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
  825         mb = mreq;
  826         bpos = mtod(mb, caddr_t);
  827         nfsm_fhtom(dvp, v3);
  828         nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
  829         nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
  830         if (error) {
  831                 if (v3) {
  832                         nfsm_postop_attr(dvp, attrflag);
  833                         m_freem(mrep);
  834                 }
  835                 goto nfsmout;
  836         }
  837         nfsm_getfh(fhp, fhsize, v3);
  838 
  839         /*
  840          * Handle RENAME case...
  841          */
  842         if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
  843                 if (NFS_CMPFH(np, fhp, fhsize)) {
  844                         m_freem(mrep);
  845                         return (EISDIR);
  846                 }
  847                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
  848                 if (error) {
  849                         m_freem(mrep);
  850                         return (error);
  851                 }
  852                 newvp = NFSTOV(np);
  853                 if (v3) {
  854                         nfsm_postop_attr(newvp, attrflag);
  855                         nfsm_postop_attr(dvp, attrflag);
  856                 } else
  857                         nfsm_loadattr(newvp, NULL);
  858                 *vpp = newvp;
  859                 m_freem(mrep);
  860                 cnp->cn_flags |= SAVENAME;
  861                 return (0);
  862         }
  863 
  864         if (flags & ISDOTDOT) {
  865                 VOP_UNLOCK(dvp, 0, td);
  866                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, cnp->cn_lkflags);
  867                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
  868                 if (error)
  869                         return (error);
  870                 newvp = NFSTOV(np);
  871         } else if (NFS_CMPFH(np, fhp, fhsize)) {
  872                 VREF(dvp);
  873                 newvp = dvp;
  874         } else {
  875                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, cnp->cn_lkflags);
  876                 if (error) {
  877                         m_freem(mrep);
  878                         return (error);
  879                 }
  880                 newvp = NFSTOV(np);
  881         }
  882         if (v3) {
  883                 nfsm_postop_attr(newvp, attrflag);
  884                 nfsm_postop_attr(dvp, attrflag);
  885         } else
  886                 nfsm_loadattr(newvp, NULL);
  887         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
  888                 cnp->cn_flags |= SAVENAME;
  889         if ((cnp->cn_flags & MAKEENTRY) &&
  890             (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
  891                 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
  892                 cache_enter(dvp, newvp, cnp);
  893         }
  894         *vpp = newvp;
  895         m_freem(mrep);
  896 nfsmout:
  897         if (error) {
  898                 if (newvp != NULLVP) {
  899                         vput(newvp);
  900                         *vpp = NULLVP;
  901                 }
  902                 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
  903                     (flags & ISLASTCN) && error == ENOENT) {
  904                         if (dvp->v_mount->mnt_flag & MNT_RDONLY)
  905                                 error = EROFS;
  906                         else
  907                                 error = EJUSTRETURN;
  908                 }
  909                 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
  910                         cnp->cn_flags |= SAVENAME;
  911         }
  912         return (error);
  913 }
  914 
  915 /*
  916  * nfs read call.
  917  * Just call nfs_bioread() to do the work.
  918  */
  919 static int
  920 nfs_read(struct vop_read_args *ap)
  921 {
  922         struct vnode *vp = ap->a_vp;
  923 
  924         switch (vp->v_type) {
  925         case VREG:
  926                 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
  927         case VDIR:
  928                 return (EISDIR);
  929         default:
  930                 return (EOPNOTSUPP);
  931         }
  932 }
  933 
  934 /*
  935  * nfs readlink call
  936  */
  937 static int
  938 nfs_readlink(struct vop_readlink_args *ap)
  939 {
  940         struct vnode *vp = ap->a_vp;
  941 
  942         if (vp->v_type != VLNK)
  943                 return (EINVAL);
  944         return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
  945 }
  946 
  947 /*
  948  * Do a readlink rpc.
  949  * Called by nfs_doio() from below the buffer cache.
  950  */
  951 int
  952 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
  953 {
  954         caddr_t bpos, dpos;
  955         int error = 0, len, attrflag;
  956         struct mbuf *mreq, *mrep, *md, *mb;
  957         int v3 = NFS_ISV3(vp);
  958 
  959         nfsstats.rpccnt[NFSPROC_READLINK]++;
  960         mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
  961         mb = mreq;
  962         bpos = mtod(mb, caddr_t);
  963         nfsm_fhtom(vp, v3);
  964         nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
  965         if (v3)
  966                 nfsm_postop_attr(vp, attrflag);
  967         if (!error) {
  968                 nfsm_strsiz(len, NFS_MAXPATHLEN);
  969                 if (len == NFS_MAXPATHLEN) {
  970                         struct nfsnode *np = VTONFS(vp);
  971                         if (np->n_size && np->n_size < NFS_MAXPATHLEN)
  972                                 len = np->n_size;
  973                 }
  974                 nfsm_mtouio(uiop, len);
  975         }
  976         m_freem(mrep);
  977 nfsmout:
  978         return (error);
  979 }
  980 
  981 /*
  982  * nfs read rpc call
  983  * Ditto above
  984  */
  985 int
  986 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
  987 {
  988         u_int32_t *tl;
  989         caddr_t bpos, dpos;
  990         struct mbuf *mreq, *mrep, *md, *mb;
  991         struct nfsmount *nmp;
  992         int error = 0, len, retlen, tsiz, eof, attrflag;
  993         int v3 = NFS_ISV3(vp);
  994 
  995 #ifndef nolint
  996         eof = 0;
  997 #endif
  998         nmp = VFSTONFS(vp->v_mount);
  999         tsiz = uiop->uio_resid;
 1000         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
 1001                 return (EFBIG);
 1002         while (tsiz > 0) {
 1003                 nfsstats.rpccnt[NFSPROC_READ]++;
 1004                 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
 1005                 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
 1006                 mb = mreq;
 1007                 bpos = mtod(mb, caddr_t);
 1008                 nfsm_fhtom(vp, v3);
 1009                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
 1010                 if (v3) {
 1011                         txdr_hyper(uiop->uio_offset, tl);
 1012                         *(tl + 2) = txdr_unsigned(len);
 1013                 } else {
 1014                         *tl++ = txdr_unsigned(uiop->uio_offset);
 1015                         *tl++ = txdr_unsigned(len);
 1016                         *tl = 0;
 1017                 }
 1018                 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
 1019                 if (v3) {
 1020                         nfsm_postop_attr(vp, attrflag);
 1021                         if (error) {
 1022                                 m_freem(mrep);
 1023                                 goto nfsmout;
 1024                         }
 1025                         tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
 1026                         eof = fxdr_unsigned(int, *(tl + 1));
 1027                 } else
 1028                         nfsm_loadattr(vp, NULL);
 1029                 nfsm_strsiz(retlen, nmp->nm_rsize);
 1030                 nfsm_mtouio(uiop, retlen);
 1031                 m_freem(mrep);
 1032                 tsiz -= retlen;
 1033                 if (v3) {
 1034                         if (eof || retlen == 0) {
 1035                                 tsiz = 0;
 1036                         }
 1037                 } else if (retlen < len) {
 1038                         tsiz = 0;
 1039                 }
 1040         }
 1041 nfsmout:
 1042         return (error);
 1043 }
 1044 
 1045 /*
 1046  * nfs write call
 1047  */
 1048 int
 1049 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
 1050     int *iomode, int *must_commit)
 1051 {
 1052         u_int32_t *tl;
 1053         int32_t backup;
 1054         caddr_t bpos, dpos;
 1055         struct mbuf *mreq, *mrep, *md, *mb;
 1056         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
 1057         int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
 1058         int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
 1059 
 1060 #ifndef DIAGNOSTIC
 1061         if (uiop->uio_iovcnt != 1)
 1062                 panic("nfs: writerpc iovcnt > 1");
 1063 #endif
 1064         *must_commit = 0;
 1065         tsiz = uiop->uio_resid;
 1066         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
 1067                 return (EFBIG);
 1068         while (tsiz > 0) {
 1069                 nfsstats.rpccnt[NFSPROC_WRITE]++;
 1070                 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
 1071                 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
 1072                         NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
 1073                 mb = mreq;
 1074                 bpos = mtod(mb, caddr_t);
 1075                 nfsm_fhtom(vp, v3);
 1076                 if (v3) {
 1077                         tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
 1078                         txdr_hyper(uiop->uio_offset, tl);
 1079                         tl += 2;
 1080                         *tl++ = txdr_unsigned(len);
 1081                         *tl++ = txdr_unsigned(*iomode);
 1082                         *tl = txdr_unsigned(len);
 1083                 } else {
 1084                         u_int32_t x;
 1085 
 1086                         tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
 1087                         /* Set both "begin" and "current" to non-garbage. */
 1088                         x = txdr_unsigned((u_int32_t)uiop->uio_offset);
 1089                         *tl++ = x;      /* "begin offset" */
 1090                         *tl++ = x;      /* "current offset" */
 1091                         x = txdr_unsigned(len);
 1092                         *tl++ = x;      /* total to this offset */
 1093                         *tl = x;        /* size of this write */
 1094                 }
 1095                 nfsm_uiotom(uiop, len);
 1096                 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
 1097                 if (v3) {
 1098                         wccflag = NFSV3_WCCCHK;
 1099                         nfsm_wcc_data(vp, wccflag);
 1100                         if (!error) {
 1101                                 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
 1102                                         + NFSX_V3WRITEVERF);
 1103                                 rlen = fxdr_unsigned(int, *tl++);
 1104                                 if (rlen == 0) {
 1105                                         error = NFSERR_IO;
 1106                                         m_freem(mrep);
 1107                                         break;
 1108                                 } else if (rlen < len) {
 1109                                         backup = len - rlen;
 1110                                         uiop->uio_iov->iov_base =
 1111                                             (char *)uiop->uio_iov->iov_base -
 1112                                             backup;
 1113                                         uiop->uio_iov->iov_len += backup;
 1114                                         uiop->uio_offset -= backup;
 1115                                         uiop->uio_resid += backup;
 1116                                         len = rlen;
 1117                                 }
 1118                                 commit = fxdr_unsigned(int, *tl++);
 1119 
 1120                                 /*
 1121                                  * Return the lowest committment level
 1122                                  * obtained by any of the RPCs.
 1123                                  */
 1124                                 if (committed == NFSV3WRITE_FILESYNC)
 1125                                         committed = commit;
 1126                                 else if (committed == NFSV3WRITE_DATASYNC &&
 1127                                         commit == NFSV3WRITE_UNSTABLE)
 1128                                         committed = commit;
 1129                                 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
 1130                                     bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
 1131                                         NFSX_V3WRITEVERF);
 1132                                     nmp->nm_state |= NFSSTA_HASWRITEVERF;
 1133                                 } else if (bcmp((caddr_t)tl,
 1134                                     (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
 1135                                     *must_commit = 1;
 1136                                     bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
 1137                                         NFSX_V3WRITEVERF);
 1138                                 }
 1139                         }
 1140                 } else
 1141                     nfsm_loadattr(vp, NULL);
 1142                 if (wccflag)
 1143                     VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
 1144                 m_freem(mrep);
 1145                 if (error)
 1146                         break;
 1147                 tsiz -= len;
 1148         }
 1149 nfsmout:
 1150         if (vp->v_mount->mnt_flag & MNT_ASYNC)
 1151                 committed = NFSV3WRITE_FILESYNC;
 1152         *iomode = committed;
 1153         if (error)
 1154                 uiop->uio_resid = tsiz;
 1155         return (error);
 1156 }
 1157 
 1158 /*
 1159  * nfs mknod rpc
 1160  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
 1161  * mode set to specify the file type and the size field for rdev.
 1162  */
 1163 static int
 1164 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
 1165     struct vattr *vap)
 1166 {
 1167         struct nfsv2_sattr *sp;
 1168         u_int32_t *tl;
 1169         struct vnode *newvp = NULL;
 1170         struct nfsnode *np = NULL;
 1171         struct vattr vattr;
 1172         caddr_t bpos, dpos;
 1173         int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
 1174         struct mbuf *mreq, *mrep, *md, *mb;
 1175         u_int32_t rdev;
 1176         int v3 = NFS_ISV3(dvp);
 1177 
 1178         if (vap->va_type == VCHR || vap->va_type == VBLK)
 1179                 rdev = txdr_unsigned(vap->va_rdev);
 1180         else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
 1181                 rdev = nfs_xdrneg1;
 1182         else {
 1183                 return (EOPNOTSUPP);
 1184         }
 1185         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
 1186                 return (error);
 1187         }
 1188         nfsstats.rpccnt[NFSPROC_MKNOD]++;
 1189         mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
 1190                 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
 1191         mb = mreq;
 1192         bpos = mtod(mb, caddr_t);
 1193         nfsm_fhtom(dvp, v3);
 1194         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
 1195         if (v3) {
 1196                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
 1197                 *tl++ = vtonfsv3_type(vap->va_type);
 1198                 nfsm_v3attrbuild(vap, FALSE);
 1199                 if (vap->va_type == VCHR || vap->va_type == VBLK) {
 1200                         tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
 1201                         *tl++ = txdr_unsigned(umajor(vap->va_rdev));
 1202                         *tl = txdr_unsigned(uminor(vap->va_rdev));
 1203                 }
 1204         } else {
 1205                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
 1206                 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
 1207                 sp->sa_uid = nfs_xdrneg1;
 1208                 sp->sa_gid = nfs_xdrneg1;
 1209                 sp->sa_size = rdev;
 1210                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
 1211                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
 1212         }
 1213         nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
 1214         if (!error) {
 1215                 nfsm_mtofh(dvp, newvp, v3, gotvp);
 1216                 if (!gotvp) {
 1217                         if (newvp) {
 1218                                 vput(newvp);
 1219                                 newvp = NULL;
 1220                         }
 1221                         error = nfs_lookitup(dvp, cnp->cn_nameptr,
 1222                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
 1223                         if (!error)
 1224                                 newvp = NFSTOV(np);
 1225                 }
 1226         }
 1227         if (v3)
 1228                 nfsm_wcc_data(dvp, wccflag);
 1229         m_freem(mrep);
 1230 nfsmout:
 1231         if (error) {
 1232                 if (newvp)
 1233                         vput(newvp);
 1234         } else {
 1235                 if (cnp->cn_flags & MAKEENTRY)
 1236                         cache_enter(dvp, newvp, cnp);
 1237                 *vpp = newvp;
 1238         }
 1239         VTONFS(dvp)->n_flag |= NMODIFIED;
 1240         if (!wccflag)
 1241                 VTONFS(dvp)->n_attrstamp = 0;
 1242         return (error);
 1243 }
 1244 
 1245 /*
 1246  * nfs mknod vop
 1247  * just call nfs_mknodrpc() to do the work.
 1248  */
 1249 /* ARGSUSED */
 1250 static int
 1251 nfs_mknod(struct vop_mknod_args *ap)
 1252 {
 1253 
 1254         return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
 1255 }
 1256 
 1257 static u_long create_verf;
 1258 /*
 1259  * nfs file create call
 1260  */
 1261 static int
 1262 nfs_create(struct vop_create_args *ap)
 1263 {
 1264         struct vnode *dvp = ap->a_dvp;
 1265         struct vattr *vap = ap->a_vap;
 1266         struct componentname *cnp = ap->a_cnp;
 1267         struct nfsv2_sattr *sp;
 1268         u_int32_t *tl;
 1269         struct nfsnode *np = NULL;
 1270         struct vnode *newvp = NULL;
 1271         caddr_t bpos, dpos;
 1272         int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
 1273         struct mbuf *mreq, *mrep, *md, *mb;
 1274         struct vattr vattr;
 1275         int v3 = NFS_ISV3(dvp);
 1276 
 1277         /*
 1278          * Oops, not for me..
 1279          */
 1280         if (vap->va_type == VSOCK)
 1281                 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
 1282 
 1283         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
 1284                 return (error);
 1285         }
 1286         if (vap->va_vaflags & VA_EXCLUSIVE)
 1287                 fmode |= O_EXCL;
 1288 again:
 1289         nfsstats.rpccnt[NFSPROC_CREATE]++;
 1290         mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
 1291                 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
 1292         mb = mreq;
 1293         bpos = mtod(mb, caddr_t);
 1294         nfsm_fhtom(dvp, v3);
 1295         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
 1296         if (v3) {
 1297                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
 1298                 if (fmode & O_EXCL) {
 1299                         *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
 1300                         tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
 1301 #ifdef INET
 1302                         if (!TAILQ_EMPTY(&in_ifaddrhead))
 1303                                 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
 1304                         else
 1305 #endif
 1306                                 *tl++ = create_verf;
 1307                         *tl = ++create_verf;
 1308                 } else {
 1309                         *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
 1310                         nfsm_v3attrbuild(vap, FALSE);
 1311                 }
 1312         } else {
 1313                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
 1314                 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
 1315                 sp->sa_uid = nfs_xdrneg1;
 1316                 sp->sa_gid = nfs_xdrneg1;
 1317                 sp->sa_size = 0;
 1318                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
 1319                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
 1320         }
 1321         nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
 1322         if (!error) {
 1323                 nfsm_mtofh(dvp, newvp, v3, gotvp);
 1324                 if (!gotvp) {
 1325                         if (newvp) {
 1326                                 vput(newvp);
 1327                                 newvp = NULL;
 1328                         }
 1329                         error = nfs_lookitup(dvp, cnp->cn_nameptr,
 1330                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
 1331                         if (!error)
 1332                                 newvp = NFSTOV(np);
 1333                 }
 1334         }
 1335         if (v3)
 1336                 nfsm_wcc_data(dvp, wccflag);
 1337         m_freem(mrep);
 1338 nfsmout:
 1339         if (error) {
 1340                 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
 1341                         fmode &= ~O_EXCL;
 1342                         goto again;
 1343                 }
 1344                 if (newvp)
 1345                         vput(newvp);
 1346         } else if (v3 && (fmode & O_EXCL)) {
 1347                 /*
 1348                  * We are normally called with only a partially initialized
 1349                  * VAP.  Since the NFSv3 spec says that server may use the
 1350                  * file attributes to store the verifier, the spec requires
 1351                  * us to do a SETATTR RPC. FreeBSD servers store the verifier
 1352                  * in atime, but we can't really assume that all servers will
 1353                  * so we ensure that our SETATTR sets both atime and mtime.
 1354                  */
 1355                 if (vap->va_mtime.tv_sec == VNOVAL)
 1356                         vfs_timestamp(&vap->va_mtime);
 1357                 if (vap->va_atime.tv_sec == VNOVAL)
 1358                         vap->va_atime = vap->va_mtime;
 1359                 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
 1360                 if (error)
 1361                         vput(newvp);
 1362         }
 1363         if (!error) {
 1364                 if (cnp->cn_flags & MAKEENTRY)
 1365                         cache_enter(dvp, newvp, cnp);
 1366                 *ap->a_vpp = newvp;
 1367         }
 1368         VTONFS(dvp)->n_flag |= NMODIFIED;
 1369         if (!wccflag)
 1370                 VTONFS(dvp)->n_attrstamp = 0;
 1371         return (error);
 1372 }
 1373 
 1374 /*
 1375  * nfs file remove call
 1376  * To try and make nfs semantics closer to ufs semantics, a file that has
 1377  * other processes using the vnode is renamed instead of removed and then
 1378  * removed later on the last close.
 1379  * - If v_usecount > 1
 1380  *        If a rename is not already in the works
 1381  *           call nfs_sillyrename() to set it up
 1382  *     else
 1383  *        do the remove rpc
 1384  */
 1385 static int
 1386 nfs_remove(struct vop_remove_args *ap)
 1387 {
 1388         struct vnode *vp = ap->a_vp;
 1389         struct vnode *dvp = ap->a_dvp;
 1390         struct componentname *cnp = ap->a_cnp;
 1391         struct nfsnode *np = VTONFS(vp);
 1392         int error = 0;
 1393         struct vattr vattr;
 1394 
 1395 #ifndef DIAGNOSTIC
 1396         if ((cnp->cn_flags & HASBUF) == 0)
 1397                 panic("nfs_remove: no name");
 1398         if (vrefcnt(vp) < 1)
 1399                 panic("nfs_remove: bad v_usecount");
 1400 #endif
 1401         if (vp->v_type == VDIR)
 1402                 error = EPERM;
 1403         else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
 1404             VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
 1405             vattr.va_nlink > 1)) {
 1406                 /*
 1407                  * Purge the name cache so that the chance of a lookup for
 1408                  * the name succeeding while the remove is in progress is
 1409                  * minimized. Without node locking it can still happen, such
 1410                  * that an I/O op returns ESTALE, but since you get this if
 1411                  * another host removes the file..
 1412                  */
 1413                 cache_purge(vp);
 1414                 /*
 1415                  * throw away biocache buffers, mainly to avoid
 1416                  * unnecessary delayed writes later.
 1417                  */
 1418                 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
 1419                 /* Do the rpc */
 1420                 if (error != EINTR && error != EIO)
 1421                         error = nfs_removerpc(dvp, cnp->cn_nameptr,
 1422                                 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
 1423                 /*
 1424                  * Kludge City: If the first reply to the remove rpc is lost..
 1425                  *   the reply to the retransmitted request will be ENOENT
 1426                  *   since the file was in fact removed
 1427                  *   Therefore, we cheat and return success.
 1428                  */
 1429                 if (error == ENOENT)
 1430                         error = 0;
 1431         } else if (!np->n_sillyrename)
 1432                 error = nfs_sillyrename(dvp, vp, cnp);
 1433         np->n_attrstamp = 0;
 1434         return (error);
 1435 }
 1436 
 1437 /*
 1438  * nfs file remove rpc called from nfs_inactive
 1439  */
 1440 int
 1441 nfs_removeit(struct sillyrename *sp)
 1442 {
 1443 
 1444         /*
 1445          * Make sure that the directory vnode is still valid.
 1446          * XXX we should lock sp->s_dvp here.
 1447          */
 1448         if (sp->s_dvp->v_type == VBAD)
 1449                 return (0);
 1450         return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
 1451                 NULL));
 1452 }
 1453 
 1454 /*
 1455  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
 1456  */
 1457 static int
 1458 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
 1459     struct ucred *cred, struct thread *td)
 1460 {
 1461         caddr_t bpos, dpos;
 1462         int error = 0, wccflag = NFSV3_WCCRATTR;
 1463         struct mbuf *mreq, *mrep, *md, *mb;
 1464         int v3 = NFS_ISV3(dvp);
 1465 
 1466         nfsstats.rpccnt[NFSPROC_REMOVE]++;
 1467         mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
 1468                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
 1469         mb = mreq;
 1470         bpos = mtod(mb, caddr_t);
 1471         nfsm_fhtom(dvp, v3);
 1472         nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
 1473         nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
 1474         if (v3)
 1475                 nfsm_wcc_data(dvp, wccflag);
 1476         m_freem(mrep);
 1477 nfsmout:
 1478         VTONFS(dvp)->n_flag |= NMODIFIED;
 1479         if (!wccflag)
 1480                 VTONFS(dvp)->n_attrstamp = 0;
 1481         return (error);
 1482 }
 1483 
 1484 /*
 1485  * nfs file rename call
 1486  */
 1487 static int
 1488 nfs_rename(struct vop_rename_args *ap)
 1489 {
 1490         struct vnode *fvp = ap->a_fvp;
 1491         struct vnode *tvp = ap->a_tvp;
 1492         struct vnode *fdvp = ap->a_fdvp;
 1493         struct vnode *tdvp = ap->a_tdvp;
 1494         struct componentname *tcnp = ap->a_tcnp;
 1495         struct componentname *fcnp = ap->a_fcnp;
 1496         int error;
 1497 
 1498 #ifndef DIAGNOSTIC
 1499         if ((tcnp->cn_flags & HASBUF) == 0 ||
 1500             (fcnp->cn_flags & HASBUF) == 0)
 1501                 panic("nfs_rename: no name");
 1502 #endif
 1503         /* Check for cross-device rename */
 1504         if ((fvp->v_mount != tdvp->v_mount) ||
 1505             (tvp && (fvp->v_mount != tvp->v_mount))) {
 1506                 error = EXDEV;
 1507                 goto out;
 1508         }
 1509 
 1510         if (fvp == tvp) {
 1511                 printf("nfs_rename: fvp == tvp (can't happen)\n");
 1512                 error = 0;
 1513                 goto out;
 1514         }
 1515         if ((error = vn_lock(fvp, LK_EXCLUSIVE, fcnp->cn_thread)) != 0)
 1516                 goto out;
 1517 
 1518         /*
 1519          * We have to flush B_DELWRI data prior to renaming
 1520          * the file.  If we don't, the delayed-write buffers
 1521          * can be flushed out later after the file has gone stale
 1522          * under NFSV3.  NFSV2 does not have this problem because
 1523          * ( as far as I can tell ) it flushes dirty buffers more
 1524          * often.
 1525          * 
 1526          * Skip the rename operation if the fsync fails, this can happen
 1527          * due to the server's volume being full, when we pushed out data
 1528          * that was written back to our cache earlier. Not checking for
 1529          * this condition can result in potential (silent) data loss.
 1530          */
 1531         error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
 1532         VOP_UNLOCK(fvp, 0, fcnp->cn_thread);
 1533         if (!error && tvp)
 1534                 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
 1535         if (error)
 1536                 goto out;
 1537 
 1538         /*
 1539          * If the tvp exists and is in use, sillyrename it before doing the
 1540          * rename of the new file over it.
 1541          * XXX Can't sillyrename a directory.
 1542          */
 1543         if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
 1544                 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
 1545                 vput(tvp);
 1546                 tvp = NULL;
 1547         }
 1548 
 1549         error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
 1550                 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
 1551                 tcnp->cn_thread);
 1552 
 1553         if (fvp->v_type == VDIR) {
 1554                 if (tvp != NULL && tvp->v_type == VDIR)
 1555                         cache_purge(tdvp);
 1556                 cache_purge(fdvp);
 1557         }
 1558 
 1559 out:
 1560         if (tdvp == tvp)
 1561                 vrele(tdvp);
 1562         else
 1563                 vput(tdvp);
 1564         if (tvp)
 1565                 vput(tvp);
 1566         vrele(fdvp);
 1567         vrele(fvp);
 1568         /*
 1569          * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
 1570          */
 1571         if (error == ENOENT)
 1572                 error = 0;
 1573         return (error);
 1574 }
 1575 
 1576 /*
 1577  * nfs file rename rpc called from nfs_remove() above
 1578  */
 1579 static int
 1580 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
 1581     struct sillyrename *sp)
 1582 {
 1583 
 1584         return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
 1585             sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
 1586 }
 1587 
 1588 /*
 1589  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
 1590  */
 1591 static int
 1592 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
 1593     struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
 1594     struct thread *td)
 1595 {
 1596         caddr_t bpos, dpos;
 1597         int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
 1598         struct mbuf *mreq, *mrep, *md, *mb;
 1599         int v3 = NFS_ISV3(fdvp);
 1600 
 1601         nfsstats.rpccnt[NFSPROC_RENAME]++;
 1602         mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
 1603                 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
 1604                 nfsm_rndup(tnamelen));
 1605         mb = mreq;
 1606         bpos = mtod(mb, caddr_t);
 1607         nfsm_fhtom(fdvp, v3);
 1608         nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
 1609         nfsm_fhtom(tdvp, v3);
 1610         nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
 1611         nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
 1612         if (v3) {
 1613                 nfsm_wcc_data(fdvp, fwccflag);
 1614                 nfsm_wcc_data(tdvp, twccflag);
 1615         }
 1616         m_freem(mrep);
 1617 nfsmout:
 1618         VTONFS(fdvp)->n_flag |= NMODIFIED;
 1619         VTONFS(tdvp)->n_flag |= NMODIFIED;
 1620         if (!fwccflag)
 1621                 VTONFS(fdvp)->n_attrstamp = 0;
 1622         if (!twccflag)
 1623                 VTONFS(tdvp)->n_attrstamp = 0;
 1624         return (error);
 1625 }
 1626 
 1627 /*
 1628  * nfs hard link create call
 1629  */
 1630 static int
 1631 nfs_link(struct vop_link_args *ap)
 1632 {
 1633         struct vnode *vp = ap->a_vp;
 1634         struct vnode *tdvp = ap->a_tdvp;
 1635         struct componentname *cnp = ap->a_cnp;
 1636         caddr_t bpos, dpos;
 1637         int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
 1638         struct mbuf *mreq, *mrep, *md, *mb;
 1639         int v3;
 1640 
 1641         if (vp->v_mount != tdvp->v_mount) {
 1642                 return (EXDEV);
 1643         }
 1644 
 1645         /*
 1646          * Push all writes to the server, so that the attribute cache
 1647          * doesn't get "out of sync" with the server.
 1648          * XXX There should be a better way!
 1649          */
 1650         VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
 1651 
 1652         v3 = NFS_ISV3(vp);
 1653         nfsstats.rpccnt[NFSPROC_LINK]++;
 1654         mreq = nfsm_reqhead(vp, NFSPROC_LINK,
 1655                 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
 1656         mb = mreq;
 1657         bpos = mtod(mb, caddr_t);
 1658         nfsm_fhtom(vp, v3);
 1659         nfsm_fhtom(tdvp, v3);
 1660         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
 1661         nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
 1662         if (v3) {
 1663                 nfsm_postop_attr(vp, attrflag);
 1664                 nfsm_wcc_data(tdvp, wccflag);
 1665         }
 1666         m_freem(mrep);
 1667 nfsmout:
 1668         VTONFS(tdvp)->n_flag |= NMODIFIED;
 1669         if (!attrflag)
 1670                 VTONFS(vp)->n_attrstamp = 0;
 1671         if (!wccflag)
 1672                 VTONFS(tdvp)->n_attrstamp = 0;
 1673         /*
 1674          * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
 1675          */
 1676         if (error == EEXIST)
 1677                 error = 0;
 1678         return (error);
 1679 }
 1680 
 1681 /*
 1682  * nfs symbolic link create call
 1683  */
 1684 static int
 1685 nfs_symlink(struct vop_symlink_args *ap)
 1686 {
 1687         struct vnode *dvp = ap->a_dvp;
 1688         struct vattr *vap = ap->a_vap;
 1689         struct componentname *cnp = ap->a_cnp;
 1690         struct nfsv2_sattr *sp;
 1691         caddr_t bpos, dpos;
 1692         int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
 1693         struct mbuf *mreq, *mrep, *md, *mb;
 1694         struct vnode *newvp = NULL;
 1695         int v3 = NFS_ISV3(dvp);
 1696 
 1697         nfsstats.rpccnt[NFSPROC_SYMLINK]++;
 1698         slen = strlen(ap->a_target);
 1699         mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
 1700             nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
 1701         mb = mreq;
 1702         bpos = mtod(mb, caddr_t);
 1703         nfsm_fhtom(dvp, v3);
 1704         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
 1705         if (v3) {
 1706                 nfsm_v3attrbuild(vap, FALSE);
 1707         }
 1708         nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
 1709         if (!v3) {
 1710                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
 1711                 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
 1712                 sp->sa_uid = nfs_xdrneg1;
 1713                 sp->sa_gid = nfs_xdrneg1;
 1714                 sp->sa_size = nfs_xdrneg1;
 1715                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
 1716                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
 1717         }
 1718 
 1719         /*
 1720          * Issue the NFS request and get the rpc response.
 1721          *
 1722          * Only NFSv3 responses returning an error of 0 actually return
 1723          * a file handle that can be converted into newvp without having
 1724          * to do an extra lookup rpc.
 1725          */
 1726         nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
 1727         if (v3) {
 1728                 if (error == 0)
 1729                         nfsm_mtofh(dvp, newvp, v3, gotvp);
 1730                 nfsm_wcc_data(dvp, wccflag);
 1731         }
 1732 
 1733         /*
 1734          * out code jumps -> here, mrep is also freed.
 1735          */
 1736 
 1737         m_freem(mrep);
 1738 nfsmout:
 1739 
 1740         /*
 1741          * If we get an EEXIST error, silently convert it to no-error
 1742          * in case of an NFS retry.
 1743          */
 1744         if (error == EEXIST)
 1745                 error = 0;
 1746 
 1747         /*
 1748          * If we do not have (or no longer have) an error, and we could
 1749          * not extract the newvp from the response due to the request being
 1750          * NFSv2 or the error being EEXIST.  We have to do a lookup in order
 1751          * to obtain a newvp to return.
 1752          */
 1753         if (error == 0 && newvp == NULL) {
 1754                 struct nfsnode *np = NULL;
 1755 
 1756                 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
 1757                     cnp->cn_cred, cnp->cn_thread, &np);
 1758                 if (!error)
 1759                         newvp = NFSTOV(np);
 1760         }
 1761         if (error) {
 1762                 if (newvp)
 1763                         vput(newvp);
 1764         } else {
 1765                 *ap->a_vpp = newvp;
 1766         }
 1767         VTONFS(dvp)->n_flag |= NMODIFIED;
 1768         if (!wccflag)
 1769                 VTONFS(dvp)->n_attrstamp = 0;
 1770         return (error);
 1771 }
 1772 
 1773 /*
 1774  * nfs make dir call
 1775  */
 1776 static int
 1777 nfs_mkdir(struct vop_mkdir_args *ap)
 1778 {
 1779         struct vnode *dvp = ap->a_dvp;
 1780         struct vattr *vap = ap->a_vap;
 1781         struct componentname *cnp = ap->a_cnp;
 1782         struct nfsv2_sattr *sp;
 1783         int len;
 1784         struct nfsnode *np = NULL;
 1785         struct vnode *newvp = NULL;
 1786         caddr_t bpos, dpos;
 1787         int error = 0, wccflag = NFSV3_WCCRATTR;
 1788         int gotvp = 0;
 1789         struct mbuf *mreq, *mrep, *md, *mb;
 1790         struct vattr vattr;
 1791         int v3 = NFS_ISV3(dvp);
 1792 
 1793         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
 1794                 return (error);
 1795         }
 1796         len = cnp->cn_namelen;
 1797         nfsstats.rpccnt[NFSPROC_MKDIR]++;
 1798         mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
 1799           NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
 1800         mb = mreq;
 1801         bpos = mtod(mb, caddr_t);
 1802         nfsm_fhtom(dvp, v3);
 1803         nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
 1804         if (v3) {
 1805                 nfsm_v3attrbuild(vap, FALSE);
 1806         } else {
 1807                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
 1808                 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
 1809                 sp->sa_uid = nfs_xdrneg1;
 1810                 sp->sa_gid = nfs_xdrneg1;
 1811                 sp->sa_size = nfs_xdrneg1;
 1812                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
 1813                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
 1814         }
 1815         nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
 1816         if (!error)
 1817                 nfsm_mtofh(dvp, newvp, v3, gotvp);
 1818         if (v3)
 1819                 nfsm_wcc_data(dvp, wccflag);
 1820         m_freem(mrep);
 1821 nfsmout:
 1822         VTONFS(dvp)->n_flag |= NMODIFIED;
 1823         if (!wccflag)
 1824                 VTONFS(dvp)->n_attrstamp = 0;
 1825         /*
 1826          * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
 1827          * if we can succeed in looking up the directory.
 1828          */
 1829         if (error == EEXIST || (!error && !gotvp)) {
 1830                 if (newvp) {
 1831                         vput(newvp);
 1832                         newvp = NULL;
 1833                 }
 1834                 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
 1835                         cnp->cn_thread, &np);
 1836                 if (!error) {
 1837                         newvp = NFSTOV(np);
 1838                         if (newvp->v_type != VDIR)
 1839                                 error = EEXIST;
 1840                 }
 1841         }
 1842         if (error) {
 1843                 if (newvp)
 1844                         vput(newvp);
 1845         } else
 1846                 *ap->a_vpp = newvp;
 1847         return (error);
 1848 }
 1849 
 1850 /*
 1851  * nfs remove directory call
 1852  */
 1853 static int
 1854 nfs_rmdir(struct vop_rmdir_args *ap)
 1855 {
 1856         struct vnode *vp = ap->a_vp;
 1857         struct vnode *dvp = ap->a_dvp;
 1858         struct componentname *cnp = ap->a_cnp;
 1859         caddr_t bpos, dpos;
 1860         int error = 0, wccflag = NFSV3_WCCRATTR;
 1861         struct mbuf *mreq, *mrep, *md, *mb;
 1862         int v3 = NFS_ISV3(dvp);
 1863 
 1864         if (dvp == vp)
 1865                 return (EINVAL);
 1866         nfsstats.rpccnt[NFSPROC_RMDIR]++;
 1867         mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
 1868                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
 1869         mb = mreq;
 1870         bpos = mtod(mb, caddr_t);
 1871         nfsm_fhtom(dvp, v3);
 1872         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
 1873         nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
 1874         if (v3)
 1875                 nfsm_wcc_data(dvp, wccflag);
 1876         m_freem(mrep);
 1877 nfsmout:
 1878         VTONFS(dvp)->n_flag |= NMODIFIED;
 1879         if (!wccflag)
 1880                 VTONFS(dvp)->n_attrstamp = 0;
 1881         cache_purge(dvp);
 1882         cache_purge(vp);
 1883         /*
 1884          * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
 1885          */
 1886         if (error == ENOENT)
 1887                 error = 0;
 1888         return (error);
 1889 }
 1890 
 1891 /*
 1892  * nfs readdir call
 1893  */
 1894 static int
 1895 nfs_readdir(struct vop_readdir_args *ap)
 1896 {
 1897         struct vnode *vp = ap->a_vp;
 1898         struct nfsnode *np = VTONFS(vp);
 1899         struct uio *uio = ap->a_uio;
 1900         int tresid, error;
 1901         struct vattr vattr;
 1902 
 1903         if (vp->v_type != VDIR)
 1904                 return (EPERM);
 1905         /*
 1906          * First, check for hit on the EOF offset cache
 1907          */
 1908         if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
 1909             (np->n_flag & NMODIFIED) == 0) {
 1910                 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0 &&
 1911                     !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
 1912                         nfsstats.direofcache_hits++;
 1913                         return (0);
 1914                 }
 1915         }
 1916 
 1917         /*
 1918          * Call nfs_bioread() to do the real work.
 1919          */
 1920         tresid = uio->uio_resid;
 1921         error = nfs_bioread(vp, uio, 0, ap->a_cred);
 1922 
 1923         if (!error && uio->uio_resid == tresid)
 1924                 nfsstats.direofcache_misses++;
 1925         return (error);
 1926 }
 1927 
 1928 /*
 1929  * Readdir rpc call.
 1930  * Called from below the buffer cache by nfs_doio().
 1931  */
 1932 int
 1933 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
 1934 {
 1935         int len, left;
 1936         struct dirent *dp = NULL;
 1937         u_int32_t *tl;
 1938         caddr_t cp;
 1939         nfsuint64 *cookiep;
 1940         caddr_t bpos, dpos;
 1941         struct mbuf *mreq, *mrep, *md, *mb;
 1942         nfsuint64 cookie;
 1943         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
 1944         struct nfsnode *dnp = VTONFS(vp);
 1945         u_quad_t fileno;
 1946         int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
 1947         int attrflag;
 1948         int v3 = NFS_ISV3(vp);
 1949 
 1950 #ifndef DIAGNOSTIC
 1951         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
 1952                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
 1953                 panic("nfs readdirrpc bad uio");
 1954 #endif
 1955 
 1956         /*
 1957          * If there is no cookie, assume directory was stale.
 1958          */
 1959         cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
 1960         if (cookiep)
 1961                 cookie = *cookiep;
 1962         else
 1963                 return (NFSERR_BAD_COOKIE);
 1964         /*
 1965          * Loop around doing readdir rpc's of size nm_readdirsize
 1966          * truncated to a multiple of DIRBLKSIZ.
 1967          * The stopping criteria is EOF or buffer full.
 1968          */
 1969         while (more_dirs && bigenough) {
 1970                 nfsstats.rpccnt[NFSPROC_READDIR]++;
 1971                 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
 1972                         NFSX_READDIR(v3));
 1973                 mb = mreq;
 1974                 bpos = mtod(mb, caddr_t);
 1975                 nfsm_fhtom(vp, v3);
 1976                 if (v3) {
 1977                         tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
 1978                         *tl++ = cookie.nfsuquad[0];
 1979                         *tl++ = cookie.nfsuquad[1];
 1980                         *tl++ = dnp->n_cookieverf.nfsuquad[0];
 1981                         *tl++ = dnp->n_cookieverf.nfsuquad[1];
 1982                 } else {
 1983                         tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
 1984                         *tl++ = cookie.nfsuquad[0];
 1985                 }
 1986                 *tl = txdr_unsigned(nmp->nm_readdirsize);
 1987                 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
 1988                 if (v3) {
 1989                         nfsm_postop_attr(vp, attrflag);
 1990                         if (!error) {
 1991                                 tl = nfsm_dissect(u_int32_t *,
 1992                                     2 * NFSX_UNSIGNED);
 1993                                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
 1994                                 dnp->n_cookieverf.nfsuquad[1] = *tl;
 1995                         } else {
 1996                                 m_freem(mrep);
 1997                                 goto nfsmout;
 1998                         }
 1999                 }
 2000                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
 2001                 more_dirs = fxdr_unsigned(int, *tl);
 2002 
 2003                 /* loop thru the dir entries, doctoring them to 4bsd form */
 2004                 while (more_dirs && bigenough) {
 2005                         if (v3) {
 2006                                 tl = nfsm_dissect(u_int32_t *,
 2007                                     3 * NFSX_UNSIGNED);
 2008                                 fileno = fxdr_hyper(tl);
 2009                                 len = fxdr_unsigned(int, *(tl + 2));
 2010                         } else {
 2011                                 tl = nfsm_dissect(u_int32_t *,
 2012                                     2 * NFSX_UNSIGNED);
 2013                                 fileno = fxdr_unsigned(u_quad_t, *tl++);
 2014                                 len = fxdr_unsigned(int, *tl);
 2015                         }
 2016                         if (len <= 0 || len > NFS_MAXNAMLEN) {
 2017                                 error = EBADRPC;
 2018                                 m_freem(mrep);
 2019                                 goto nfsmout;
 2020                         }
 2021                         tlen = nfsm_rndup(len);
 2022                         if (tlen == len)
 2023                                 tlen += 4;      /* To ensure null termination */
 2024                         left = DIRBLKSIZ - blksiz;
 2025                         if ((tlen + DIRHDSIZ) > left) {
 2026                                 dp->d_reclen += left;
 2027                                 uiop->uio_iov->iov_base =
 2028                                     (char *)uiop->uio_iov->iov_base + left;
 2029                                 uiop->uio_iov->iov_len -= left;
 2030                                 uiop->uio_offset += left;
 2031                                 uiop->uio_resid -= left;
 2032                                 blksiz = 0;
 2033                         }
 2034                         if ((tlen + DIRHDSIZ) > uiop->uio_resid)
 2035                                 bigenough = 0;
 2036                         if (bigenough) {
 2037                                 dp = (struct dirent *)uiop->uio_iov->iov_base;
 2038                                 dp->d_fileno = (int)fileno;
 2039                                 dp->d_namlen = len;
 2040                                 dp->d_reclen = tlen + DIRHDSIZ;
 2041                                 dp->d_type = DT_UNKNOWN;
 2042                                 blksiz += dp->d_reclen;
 2043                                 if (blksiz == DIRBLKSIZ)
 2044                                         blksiz = 0;
 2045                                 uiop->uio_offset += DIRHDSIZ;
 2046                                 uiop->uio_resid -= DIRHDSIZ;
 2047                                 uiop->uio_iov->iov_base =
 2048                                     (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
 2049                                 uiop->uio_iov->iov_len -= DIRHDSIZ;
 2050                                 nfsm_mtouio(uiop, len);
 2051                                 cp = uiop->uio_iov->iov_base;
 2052                                 tlen -= len;
 2053                                 *cp = '\0';     /* null terminate */
 2054                                 uiop->uio_iov->iov_base =
 2055                                     (char *)uiop->uio_iov->iov_base + tlen;
 2056                                 uiop->uio_iov->iov_len -= tlen;
 2057                                 uiop->uio_offset += tlen;
 2058                                 uiop->uio_resid -= tlen;
 2059                         } else
 2060                                 nfsm_adv(nfsm_rndup(len));
 2061                         if (v3) {
 2062                                 tl = nfsm_dissect(u_int32_t *,
 2063                                     3 * NFSX_UNSIGNED);
 2064                         } else {
 2065                                 tl = nfsm_dissect(u_int32_t *,
 2066                                     2 * NFSX_UNSIGNED);
 2067                         }
 2068                         if (bigenough) {
 2069                                 cookie.nfsuquad[0] = *tl++;
 2070                                 if (v3)
 2071                                         cookie.nfsuquad[1] = *tl++;
 2072                         } else if (v3)
 2073                                 tl += 2;
 2074                         else
 2075                                 tl++;
 2076                         more_dirs = fxdr_unsigned(int, *tl);
 2077                 }
 2078                 /*
 2079                  * If at end of rpc data, get the eof boolean
 2080                  */
 2081                 if (!more_dirs) {
 2082                         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
 2083                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
 2084                 }
 2085                 m_freem(mrep);
 2086         }
 2087         /*
 2088          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
 2089          * by increasing d_reclen for the last record.
 2090          */
 2091         if (blksiz > 0) {
 2092                 left = DIRBLKSIZ - blksiz;
 2093                 dp->d_reclen += left;
 2094                 uiop->uio_iov->iov_base =
 2095                     (char *)uiop->uio_iov->iov_base + left;
 2096                 uiop->uio_iov->iov_len -= left;
 2097                 uiop->uio_offset += left;
 2098                 uiop->uio_resid -= left;
 2099         }
 2100 
 2101         /*
 2102          * We are now either at the end of the directory or have filled the
 2103          * block.
 2104          */
 2105         if (bigenough)
 2106                 dnp->n_direofoffset = uiop->uio_offset;
 2107         else {
 2108                 if (uiop->uio_resid > 0)
 2109                         printf("EEK! readdirrpc resid > 0\n");
 2110                 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
 2111                 *cookiep = cookie;
 2112         }
 2113 nfsmout:
 2114         return (error);
 2115 }
 2116 
 2117 /*
 2118  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
 2119  */
 2120 int
 2121 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
 2122 {
 2123         int len, left;
 2124         struct dirent *dp;
 2125         u_int32_t *tl;
 2126         caddr_t cp;
 2127         struct vnode *newvp;
 2128         nfsuint64 *cookiep;
 2129         caddr_t bpos, dpos, dpossav1, dpossav2;
 2130         struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
 2131         struct nameidata nami, *ndp = &nami;
 2132         struct componentname *cnp = &ndp->ni_cnd;
 2133         nfsuint64 cookie;
 2134         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
 2135         struct nfsnode *dnp = VTONFS(vp), *np;
 2136         nfsfh_t *fhp;
 2137         u_quad_t fileno;
 2138         int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
 2139         int attrflag, fhsize;
 2140 
 2141 #ifndef nolint
 2142         dp = NULL;
 2143 #endif
 2144 #ifndef DIAGNOSTIC
 2145         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
 2146                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
 2147                 panic("nfs readdirplusrpc bad uio");
 2148 #endif
 2149         ndp->ni_dvp = vp;
 2150         newvp = NULLVP;
 2151 
 2152         /*
 2153          * If there is no cookie, assume directory was stale.
 2154          */
 2155         cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
 2156         if (cookiep)
 2157                 cookie = *cookiep;
 2158         else
 2159                 return (NFSERR_BAD_COOKIE);
 2160         /*
 2161          * Loop around doing readdir rpc's of size nm_readdirsize
 2162          * truncated to a multiple of DIRBLKSIZ.
 2163          * The stopping criteria is EOF or buffer full.
 2164          */
 2165         while (more_dirs && bigenough) {
 2166                 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
 2167                 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
 2168                         NFSX_FH(1) + 6 * NFSX_UNSIGNED);
 2169                 mb = mreq;
 2170                 bpos = mtod(mb, caddr_t);
 2171                 nfsm_fhtom(vp, 1);
 2172                 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
 2173                 *tl++ = cookie.nfsuquad[0];
 2174                 *tl++ = cookie.nfsuquad[1];
 2175                 *tl++ = dnp->n_cookieverf.nfsuquad[0];
 2176                 *tl++ = dnp->n_cookieverf.nfsuquad[1];
 2177                 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
 2178                 *tl = txdr_unsigned(nmp->nm_rsize);
 2179                 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
 2180                 nfsm_postop_attr(vp, attrflag);
 2181                 if (error) {
 2182                         m_freem(mrep);
 2183                         goto nfsmout;
 2184                 }
 2185                 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
 2186                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
 2187                 dnp->n_cookieverf.nfsuquad[1] = *tl++;
 2188                 more_dirs = fxdr_unsigned(int, *tl);
 2189 
 2190                 /* loop thru the dir entries, doctoring them to 4bsd form */
 2191                 while (more_dirs && bigenough) {
 2192                         tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
 2193                         fileno = fxdr_hyper(tl);
 2194                         len = fxdr_unsigned(int, *(tl + 2));
 2195                         if (len <= 0 || len > NFS_MAXNAMLEN) {
 2196                                 error = EBADRPC;
 2197                                 m_freem(mrep);
 2198                                 goto nfsmout;
 2199                         }
 2200                         tlen = nfsm_rndup(len);
 2201                         if (tlen == len)
 2202                                 tlen += 4;      /* To ensure null termination*/
 2203                         left = DIRBLKSIZ - blksiz;
 2204                         if ((tlen + DIRHDSIZ) > left) {
 2205                                 dp->d_reclen += left;
 2206                                 uiop->uio_iov->iov_base =
 2207                                     (char *)uiop->uio_iov->iov_base + left;
 2208                                 uiop->uio_iov->iov_len -= left;
 2209                                 uiop->uio_offset += left;
 2210                                 uiop->uio_resid -= left;
 2211                                 blksiz = 0;
 2212                         }
 2213                         if ((tlen + DIRHDSIZ) > uiop->uio_resid)
 2214                                 bigenough = 0;
 2215                         if (bigenough) {
 2216                                 dp = (struct dirent *)uiop->uio_iov->iov_base;
 2217                                 dp->d_fileno = (int)fileno;
 2218                                 dp->d_namlen = len;
 2219                                 dp->d_reclen = tlen + DIRHDSIZ;
 2220                                 dp->d_type = DT_UNKNOWN;
 2221                                 blksiz += dp->d_reclen;
 2222                                 if (blksiz == DIRBLKSIZ)
 2223                                         blksiz = 0;
 2224                                 uiop->uio_offset += DIRHDSIZ;
 2225                                 uiop->uio_resid -= DIRHDSIZ;
 2226                                 uiop->uio_iov->iov_base =
 2227                                     (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
 2228                                 uiop->uio_iov->iov_len -= DIRHDSIZ;
 2229                                 cnp->cn_nameptr = uiop->uio_iov->iov_base;
 2230                                 cnp->cn_namelen = len;
 2231                                 nfsm_mtouio(uiop, len);
 2232                                 cp = uiop->uio_iov->iov_base;
 2233                                 tlen -= len;
 2234                                 *cp = '\0';
 2235                                 uiop->uio_iov->iov_base =
 2236                                     (char *)uiop->uio_iov->iov_base + tlen;
 2237                                 uiop->uio_iov->iov_len -= tlen;
 2238                                 uiop->uio_offset += tlen;
 2239                                 uiop->uio_resid -= tlen;
 2240                         } else
 2241                                 nfsm_adv(nfsm_rndup(len));
 2242                         tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
 2243                         if (bigenough) {
 2244                                 cookie.nfsuquad[0] = *tl++;
 2245                                 cookie.nfsuquad[1] = *tl++;
 2246                         } else
 2247                                 tl += 2;
 2248 
 2249                         /*
 2250                          * Since the attributes are before the file handle
 2251                          * (sigh), we must skip over the attributes and then
 2252                          * come back and get them.
 2253                          */
 2254                         attrflag = fxdr_unsigned(int, *tl);
 2255                         if (attrflag) {
 2256                             dpossav1 = dpos;
 2257                             mdsav1 = md;
 2258                             nfsm_adv(NFSX_V3FATTR);
 2259                             tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
 2260                             doit = fxdr_unsigned(int, *tl);
 2261                             /*
 2262                              * Skip loading the attrs for "..". There's a 
 2263                              * race between loading the attrs here and 
 2264                              * lookups that look for the directory currently
 2265                              * being read (in the parent). We try to acquire
 2266                              * the exclusive lock on ".." here, owning the 
 2267                              * lock on the directory being read. Lookup will
 2268                              * hold the lock on ".." and try to acquire the 
 2269                              * lock on the directory being read.
 2270                              * 
 2271                              * There are other ways of fixing this, one would
 2272                              * be to do a trylock on the ".." vnode and skip
 2273                              * loading the attrs on ".." if it happens to be 
 2274                              * locked by another process. But skipping the
 2275                              * attrload on ".." seems the easiest option.
 2276                              */
 2277                             if (strcmp(dp->d_name, "..") == 0) {
 2278                                     doit = 0;
 2279                                     /*
 2280                                      * We've already skipped over the attrs, 
 2281                                      * skip over the filehandle. And store d_type
 2282                                      * as VDIR.
 2283                                      */
 2284                                     tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
 2285                                     i = fxdr_unsigned(int, *tl);
 2286                                     nfsm_adv(nfsm_rndup(i));
 2287                                     dp->d_type = IFTODT(VTTOIF(VDIR));
 2288                             }       
 2289                             if (doit) {
 2290                                 nfsm_getfh(fhp, fhsize, 1);
 2291                                 if (NFS_CMPFH(dnp, fhp, fhsize)) {
 2292                                     VREF(vp);
 2293                                     newvp = vp;
 2294                                     np = dnp;
 2295                                 } else {
 2296                                     error = nfs_nget(vp->v_mount, fhp,
 2297                                         fhsize, &np, LK_EXCLUSIVE);
 2298                                     if (error)
 2299                                         doit = 0;
 2300                                     else
 2301                                         newvp = NFSTOV(np);
 2302                                 }
 2303                             }
 2304                             if (doit && bigenough) {
 2305                                 dpossav2 = dpos;
 2306                                 dpos = dpossav1;
 2307                                 mdsav2 = md;
 2308                                 md = mdsav1;
 2309                                 nfsm_loadattr(newvp, NULL);
 2310                                 dpos = dpossav2;
 2311                                 md = mdsav2;
 2312                                 dp->d_type =
 2313                                     IFTODT(VTTOIF(np->n_vattr.va_type));
 2314                                 ndp->ni_vp = newvp;
 2315                                 /* Update n_ctime, so subsequent lookup doesn't purge entry */
 2316                                 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
 2317                                 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
 2318                             }
 2319                         } else {
 2320                             /* Just skip over the file handle */
 2321                             tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
 2322                             i = fxdr_unsigned(int, *tl);
 2323                             if (i) {
 2324                                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
 2325                                 fhsize = fxdr_unsigned(int, *tl);
 2326                                 nfsm_adv(nfsm_rndup(fhsize));
 2327                             }
 2328                         }
 2329                         if (newvp != NULLVP) {
 2330                             if (newvp == vp)
 2331                                 vrele(newvp);
 2332                             else
 2333                                 vput(newvp);
 2334                             newvp = NULLVP;
 2335                         }
 2336                         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
 2337                         more_dirs = fxdr_unsigned(int, *tl);
 2338                 }
 2339                 /*
 2340                  * If at end of rpc data, get the eof boolean
 2341                  */
 2342                 if (!more_dirs) {
 2343                         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
 2344                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
 2345                 }
 2346                 m_freem(mrep);
 2347         }
 2348         /*
 2349          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
 2350          * by increasing d_reclen for the last record.
 2351          */
 2352         if (blksiz > 0) {
 2353                 left = DIRBLKSIZ - blksiz;
 2354                 dp->d_reclen += left;
 2355                 uiop->uio_iov->iov_base =
 2356                     (char *)uiop->uio_iov->iov_base + left;
 2357                 uiop->uio_iov->iov_len -= left;
 2358                 uiop->uio_offset += left;
 2359                 uiop->uio_resid -= left;
 2360         }
 2361 
 2362         /*
 2363          * We are now either at the end of the directory or have filled the
 2364          * block.
 2365          */
 2366         if (bigenough)
 2367                 dnp->n_direofoffset = uiop->uio_offset;
 2368         else {
 2369                 if (uiop->uio_resid > 0)
 2370                         printf("EEK! readdirplusrpc resid > 0\n");
 2371                 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
 2372                 *cookiep = cookie;
 2373         }
 2374 nfsmout:
 2375         if (newvp != NULLVP) {
 2376                 if (newvp == vp)
 2377                         vrele(newvp);
 2378                 else
 2379                         vput(newvp);
 2380                 newvp = NULLVP;
 2381         }
 2382         return (error);
 2383 }
 2384 
 2385 /*
 2386  * Silly rename. To make the NFS filesystem that is stateless look a little
 2387  * more like the "ufs" a remove of an active vnode is translated to a rename
 2388  * to a funny looking filename that is removed by nfs_inactive on the
 2389  * nfsnode. There is the potential for another process on a different client
 2390  * to create the same funny name between the nfs_lookitup() fails and the
 2391  * nfs_rename() completes, but...
 2392  */
 2393 static int
 2394 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
 2395 {
 2396         struct sillyrename *sp;
 2397         struct nfsnode *np;
 2398         int error;
 2399         short pid;
 2400         unsigned int lticks;
 2401 
 2402         cache_purge(dvp);
 2403         np = VTONFS(vp);
 2404 #ifndef DIAGNOSTIC
 2405         if (vp->v_type == VDIR)
 2406                 panic("nfs: sillyrename dir");
 2407 #endif
 2408         MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
 2409                 M_NFSREQ, M_WAITOK);
 2410         sp->s_cred = crhold(cnp->cn_cred);
 2411         sp->s_dvp = dvp;
 2412         sp->s_removeit = nfs_removeit;
 2413         VREF(dvp);
 2414 
 2415         /* 
 2416          * Fudge together a funny name.
 2417          * Changing the format of the funny name to accomodate more 
 2418          * sillynames per directory.
 2419          * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is 
 2420          * CPU ticks since boot.
 2421          */
 2422         pid = cnp->cn_thread->td_proc->p_pid;
 2423         lticks = (unsigned int)ticks;
 2424         for ( ; ; ) {
 2425                 sp->s_namlen = sprintf(sp->s_name, 
 2426                                        ".nfs.%08x.%04x4.4", lticks, 
 2427                                        pid);
 2428                 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
 2429                                  cnp->cn_thread, NULL))
 2430                         break;
 2431                 lticks++;
 2432         }
 2433         error = nfs_renameit(dvp, cnp, sp);
 2434         if (error)
 2435                 goto bad;
 2436         error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
 2437                 cnp->cn_thread, &np);
 2438         np->n_sillyrename = sp;
 2439         return (0);
 2440 bad:
 2441         vrele(sp->s_dvp);
 2442         crfree(sp->s_cred);
 2443         free((caddr_t)sp, M_NFSREQ);
 2444         return (error);
 2445 }
 2446 
 2447 /*
 2448  * Look up a file name and optionally either update the file handle or
 2449  * allocate an nfsnode, depending on the value of npp.
 2450  * npp == NULL  --> just do the lookup
 2451  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
 2452  *                      handled too
 2453  * *npp != NULL --> update the file handle in the vnode
 2454  */
 2455 static int
 2456 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
 2457     struct thread *td, struct nfsnode **npp)
 2458 {
 2459         struct vnode *newvp = NULL;
 2460         struct nfsnode *np, *dnp = VTONFS(dvp);
 2461         caddr_t bpos, dpos;
 2462         int error = 0, fhlen, attrflag;
 2463         struct mbuf *mreq, *mrep, *md, *mb;
 2464         nfsfh_t *nfhp;
 2465         int v3 = NFS_ISV3(dvp);
 2466 
 2467         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
 2468         mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
 2469                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
 2470         mb = mreq;
 2471         bpos = mtod(mb, caddr_t);
 2472         nfsm_fhtom(dvp, v3);
 2473         nfsm_strtom(name, len, NFS_MAXNAMLEN);
 2474         nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
 2475         if (npp && !error) {
 2476                 nfsm_getfh(nfhp, fhlen, v3);
 2477                 if (*npp) {
 2478                     np = *npp;
 2479                     if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
 2480                         free((caddr_t)np->n_fhp, M_NFSBIGFH);
 2481                         np->n_fhp = &np->n_fh;
 2482                     } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
 2483                         np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
 2484                     bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
 2485                     np->n_fhsize = fhlen;
 2486                     newvp = NFSTOV(np);
 2487                 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
 2488                     VREF(dvp);
 2489                     newvp = dvp;
 2490                 } else {
 2491                     error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, LK_EXCLUSIVE);
 2492                     if (error) {
 2493                         m_freem(mrep);
 2494                         return (error);
 2495                     }
 2496                     newvp = NFSTOV(np);
 2497                 }
 2498                 if (v3) {
 2499                         nfsm_postop_attr(newvp, attrflag);
 2500                         if (!attrflag && *npp == NULL) {
 2501                                 m_freem(mrep);
 2502                                 if (newvp == dvp)
 2503                                         vrele(newvp);
 2504                                 else
 2505                                         vput(newvp);
 2506                                 return (ENOENT);
 2507                         }
 2508                 } else
 2509                         nfsm_loadattr(newvp, NULL);
 2510         }
 2511         m_freem(mrep);
 2512 nfsmout:
 2513         if (npp && *npp == NULL) {
 2514                 if (error) {
 2515                         if (newvp) {
 2516                                 if (newvp == dvp)
 2517                                         vrele(newvp);
 2518                                 else
 2519                                         vput(newvp);
 2520                         }
 2521                 } else
 2522                         *npp = np;
 2523         }
 2524         return (error);
 2525 }
 2526 
 2527 /*
 2528  * Nfs Version 3 commit rpc
 2529  */
 2530 int
 2531 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
 2532     struct thread *td)
 2533 {
 2534         u_int32_t *tl;
 2535         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
 2536         caddr_t bpos, dpos;
 2537         int error = 0, wccflag = NFSV3_WCCRATTR;
 2538         struct mbuf *mreq, *mrep, *md, *mb;
 2539 
 2540         if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
 2541                 return (0);
 2542         nfsstats.rpccnt[NFSPROC_COMMIT]++;
 2543         mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
 2544         mb = mreq;
 2545         bpos = mtod(mb, caddr_t);
 2546         nfsm_fhtom(vp, 1);
 2547         tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
 2548         txdr_hyper(offset, tl);
 2549         tl += 2;
 2550         *tl = txdr_unsigned(cnt);
 2551         nfsm_request(vp, NFSPROC_COMMIT, td, cred);
 2552         nfsm_wcc_data(vp, wccflag);
 2553         if (!error) {
 2554                 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
 2555                 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
 2556                         NFSX_V3WRITEVERF)) {
 2557                         bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
 2558                                 NFSX_V3WRITEVERF);
 2559                         error = NFSERR_STALEWRITEVERF;
 2560                 }
 2561         }
 2562         m_freem(mrep);
 2563 nfsmout:
 2564         return (error);
 2565 }
 2566 
 2567 /*
 2568  * Strategy routine.
 2569  * For async requests when nfsiod(s) are running, queue the request by
 2570  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
 2571  * request.
 2572  */
 2573 static int
 2574 nfs_strategy(struct vop_strategy_args *ap)
 2575 {
 2576         struct buf *bp = ap->a_bp;
 2577         struct ucred *cr;
 2578 
 2579         KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
 2580         KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
 2581 
 2582         if (bp->b_iocmd == BIO_READ)
 2583                 cr = bp->b_rcred;
 2584         else
 2585                 cr = bp->b_wcred;
 2586 
 2587         /*
 2588          * If the op is asynchronous and an i/o daemon is waiting
 2589          * queue the request, wake it up and wait for completion
 2590          * otherwise just do it ourselves.
 2591          */
 2592         if ((bp->b_flags & B_ASYNC) == 0 ||
 2593             nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
 2594                 (void)nfs_doio(ap->a_vp, bp, cr, curthread);
 2595         return (0);
 2596 }
 2597 
 2598 /*
 2599  * fsync vnode op. Just call nfs_flush() with commit == 1.
 2600  */
 2601 /* ARGSUSED */
 2602 static int
 2603 nfs_fsync(struct vop_fsync_args *ap)
 2604 {
 2605 
 2606         return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
 2607 }
 2608 
 2609 /*
 2610  * Flush all the blocks associated with a vnode.
 2611  *      Walk through the buffer pool and push any dirty pages
 2612  *      associated with the vnode.
 2613  */
 2614 static int
 2615 nfs_flush(struct vnode *vp, int waitfor, struct thread *td,
 2616     int commit)
 2617 {
 2618         struct nfsnode *np = VTONFS(vp);
 2619         struct buf *bp;
 2620         int i;
 2621         struct buf *nbp;
 2622         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
 2623         int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
 2624         int passone = 1;
 2625         u_quad_t off, endoff, toff;
 2626         struct ucred* wcred = NULL;
 2627         struct buf **bvec = NULL;
 2628 #ifndef NFS_COMMITBVECSIZ
 2629 #define NFS_COMMITBVECSIZ       20
 2630 #endif
 2631         struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
 2632         int bvecsize = 0, bveccount;
 2633 
 2634         if (nmp->nm_flag & NFSMNT_INT)
 2635                 slpflag = PCATCH;
 2636         if (!commit)
 2637                 passone = 0;
 2638         /*
 2639          * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
 2640          * server, but has not been committed to stable storage on the server
 2641          * yet. On the first pass, the byte range is worked out and the commit
 2642          * rpc is done. On the second pass, nfs_writebp() is called to do the
 2643          * job.
 2644          */
 2645 again:
 2646         off = (u_quad_t)-1;
 2647         endoff = 0;
 2648         bvecpos = 0;
 2649         if (NFS_ISV3(vp) && commit) {
 2650                 s = splbio();
 2651                 if (bvec != NULL && bvec != bvec_on_stack)
 2652                         free(bvec, M_TEMP);
 2653                 /*
 2654                  * Count up how many buffers waiting for a commit.
 2655                  */
 2656                 bveccount = 0;
 2657                 VI_LOCK(vp);
 2658                 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
 2659                         if (BUF_REFCNT(bp) == 0 &&
 2660                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
 2661                                 == (B_DELWRI | B_NEEDCOMMIT))
 2662                                 bveccount++;
 2663                 }
 2664                 /*
 2665                  * Allocate space to remember the list of bufs to commit.  It is
 2666                  * important to use M_NOWAIT here to avoid a race with nfs_write.
 2667                  * If we can't get memory (for whatever reason), we will end up
 2668                  * committing the buffers one-by-one in the loop below.
 2669                  */
 2670                 if (bveccount > NFS_COMMITBVECSIZ) {
 2671                         /*
 2672                          * Release the vnode interlock to avoid a lock
 2673                          * order reversal.
 2674                          */
 2675                         VI_UNLOCK(vp);
 2676                         bvec = (struct buf **)
 2677                                 malloc(bveccount * sizeof(struct buf *),
 2678                                        M_TEMP, M_NOWAIT);
 2679                         VI_LOCK(vp);
 2680                         if (bvec == NULL) {
 2681                                 bvec = bvec_on_stack;
 2682                                 bvecsize = NFS_COMMITBVECSIZ;
 2683                         } else
 2684                                 bvecsize = bveccount;
 2685                 } else {
 2686                         bvec = bvec_on_stack;
 2687                         bvecsize = NFS_COMMITBVECSIZ;
 2688                 }
 2689                 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
 2690                         if (bvecpos >= bvecsize)
 2691                                 break;
 2692                         if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
 2693                                 nbp = TAILQ_NEXT(bp, b_bobufs);
 2694                                 continue;
 2695                         }
 2696                         if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
 2697                             (B_DELWRI | B_NEEDCOMMIT)) {
 2698                                 BUF_UNLOCK(bp);
 2699                                 nbp = TAILQ_NEXT(bp, b_bobufs);
 2700                                 continue;
 2701                         }
 2702                         VI_UNLOCK(vp);
 2703                         bremfree(bp);
 2704                         /*
 2705                          * Work out if all buffers are using the same cred
 2706                          * so we can deal with them all with one commit.
 2707                          *
 2708                          * NOTE: we are not clearing B_DONE here, so we have
 2709                          * to do it later on in this routine if we intend to
 2710                          * initiate I/O on the bp.
 2711                          *
 2712                          * Note: to avoid loopback deadlocks, we do not
 2713                          * assign b_runningbufspace.
 2714                          */
 2715                         if (wcred == NULL)
 2716                                 wcred = bp->b_wcred;
 2717                         else if (wcred != bp->b_wcred)
 2718                                 wcred = NOCRED;
 2719                         vfs_busy_pages(bp, 1);
 2720 
 2721                         VI_LOCK(vp);
 2722                         /*
 2723                          * bp is protected by being locked, but nbp is not
 2724                          * and vfs_busy_pages() may sleep.  We have to
 2725                          * recalculate nbp.
 2726                          */
 2727                         nbp = TAILQ_NEXT(bp, b_bobufs);
 2728 
 2729                         /*
 2730                          * A list of these buffers is kept so that the
 2731                          * second loop knows which buffers have actually
 2732                          * been committed. This is necessary, since there
 2733                          * may be a race between the commit rpc and new
 2734                          * uncommitted writes on the file.
 2735                          */
 2736                         bvec[bvecpos++] = bp;
 2737                         toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
 2738                                 bp->b_dirtyoff;
 2739                         if (toff < off)
 2740                                 off = toff;
 2741                         toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
 2742                         if (toff > endoff)
 2743                                 endoff = toff;
 2744                 }
 2745                 splx(s);
 2746                 VI_UNLOCK(vp);
 2747         }
 2748         if (bvecpos > 0) {
 2749                 /*
 2750                  * Commit data on the server, as required.
 2751                  * If all bufs are using the same wcred, then use that with
 2752                  * one call for all of them, otherwise commit each one
 2753                  * separately.
 2754                  */
 2755                 if (wcred != NOCRED)
 2756                         retv = nfs_commit(vp, off, (int)(endoff - off),
 2757                                           wcred, td);
 2758                 else {
 2759                         retv = 0;
 2760                         for (i = 0; i < bvecpos; i++) {
 2761                                 off_t off, size;
 2762                                 bp = bvec[i];
 2763                                 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
 2764                                         bp->b_dirtyoff;
 2765                                 size = (u_quad_t)(bp->b_dirtyend
 2766                                                   - bp->b_dirtyoff);
 2767                                 retv = nfs_commit(vp, off, (int)size,
 2768                                                   bp->b_wcred, td);
 2769                                 if (retv) break;
 2770                         }
 2771                 }
 2772 
 2773                 if (retv == NFSERR_STALEWRITEVERF)
 2774                         nfs_clearcommit(vp->v_mount);
 2775 
 2776                 /*
 2777                  * Now, either mark the blocks I/O done or mark the
 2778                  * blocks dirty, depending on whether the commit
 2779                  * succeeded.
 2780                  */
 2781                 for (i = 0; i < bvecpos; i++) {
 2782                         bp = bvec[i];
 2783                         bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
 2784                         if (retv) {
 2785                                 /*
 2786                                  * Error, leave B_DELWRI intact
 2787                                  */
 2788                                 vfs_unbusy_pages(bp);
 2789                                 brelse(bp);
 2790                         } else {
 2791                                 /*
 2792                                  * Success, remove B_DELWRI ( bundirty() ).
 2793                                  *
 2794                                  * b_dirtyoff/b_dirtyend seem to be NFS
 2795                                  * specific.  We should probably move that
 2796                                  * into bundirty(). XXX
 2797                                  */
 2798                                 s = splbio();
 2799                                 bufobj_wref(&vp->v_bufobj);
 2800                                 bp->b_flags |= B_ASYNC;
 2801                                 bundirty(bp);
 2802                                 bp->b_flags &= ~B_DONE;
 2803                                 bp->b_ioflags &= ~BIO_ERROR;
 2804                                 bp->b_dirtyoff = bp->b_dirtyend = 0;
 2805                                 splx(s);
 2806                                 bufdone(bp);
 2807                         }
 2808                 }
 2809         }
 2810 
 2811         /*
 2812          * Start/do any write(s) that are required.
 2813          */
 2814 loop:
 2815         s = splbio();
 2816         VI_LOCK(vp);
 2817         TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
 2818                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
 2819                         if (waitfor != MNT_WAIT || passone)
 2820                                 continue;
 2821 
 2822                         error = BUF_TIMELOCK(bp,
 2823                             LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
 2824                             VI_MTX(vp), "nfsfsync", slpflag, slptimeo);
 2825                         splx(s);
 2826                         if (error == 0)
 2827                                 panic("nfs_fsync: inconsistent lock");
 2828                         if (error == ENOLCK)
 2829                                 goto loop;
 2830                         if (nfs_sigintr(nmp, NULL, td)) {
 2831                                 error = EINTR;
 2832                                 goto done;
 2833                         }
 2834                         if (slpflag == PCATCH) {
 2835                                 slpflag = 0;
 2836                                 slptimeo = 2 * hz;
 2837                         }
 2838                         goto loop;
 2839                 }
 2840                 if ((bp->b_flags & B_DELWRI) == 0)
 2841                         panic("nfs_fsync: not dirty");
 2842                 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
 2843                         BUF_UNLOCK(bp);
 2844                         continue;
 2845                 }
 2846                 VI_UNLOCK(vp);
 2847                 bremfree(bp);
 2848                 if (passone || !commit)
 2849                     bp->b_flags |= B_ASYNC;
 2850                 else
 2851                     bp->b_flags |= B_ASYNC;
 2852                 splx(s);
 2853                 bwrite(bp);
 2854                 if (nfs_sigintr(nmp, NULL, td)) {
 2855                         error = EINTR;
 2856                         goto done;
 2857                 }
 2858                 goto loop;
 2859         }
 2860         splx(s);
 2861         if (passone) {
 2862                 passone = 0;
 2863                 VI_UNLOCK(vp);
 2864                 goto again;
 2865         }
 2866         if (waitfor == MNT_WAIT) {
 2867                 while (vp->v_bufobj.bo_numoutput) {
 2868                         error = bufobj_wwait(&vp->v_bufobj, slpflag, slptimeo);
 2869                         if (error) {
 2870                             VI_UNLOCK(vp);
 2871                             error = nfs_sigintr(nmp, NULL, td);
 2872                             if (error)
 2873                                 goto done;
 2874                             if (slpflag == PCATCH) {
 2875                                 slpflag = 0;
 2876                                 slptimeo = 2 * hz;
 2877                             }
 2878                             VI_LOCK(vp);
 2879                         }
 2880                 }
 2881                 if (vp->v_bufobj.bo_dirty.bv_cnt != 0 && commit) {
 2882                         VI_UNLOCK(vp);
 2883                         goto loop;
 2884                 }
 2885                 VI_UNLOCK(vp);
 2886                 /* 
 2887                  * Wait for all the async IO requests to drain 
 2888                  */
 2889                 while (np->n_directio_asyncwr > 0) {
 2890                         np->n_flag |= NFSYNCWAIT;
 2891                         error = nfs_tsleep(td, (caddr_t)&np->n_directio_asyncwr,
 2892                                            slpflag | (PRIBIO + 1), "nfsfsync", 0);
 2893                         if (error) {
 2894                                 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
 2895                                         error = EINTR;
 2896                                         goto done;
 2897                                 }
 2898                         }
 2899                 }
 2900 
 2901         } else
 2902                 VI_UNLOCK(vp);
 2903         if (np->n_flag & NWRITEERR) {
 2904                 error = np->n_error;
 2905                 np->n_flag &= ~NWRITEERR;
 2906         }
 2907         if (commit && vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
 2908             vp->v_bufobj.bo_numoutput == 0 && np->n_directio_asyncwr == 0)
 2909                 np->n_flag &= ~NMODIFIED;
 2910 done:
 2911         if (bvec != NULL && bvec != bvec_on_stack)
 2912                 free(bvec, M_TEMP);
 2913         return (error);
 2914 }
 2915 
 2916 /*
 2917  * NFS advisory byte-level locks.
 2918  */
 2919 static int
 2920 nfs_advlock(struct vop_advlock_args *ap)
 2921 {
 2922 
 2923         if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
 2924                 struct nfsnode *np = VTONFS(ap->a_vp);
 2925 
 2926                 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
 2927         }
 2928         return (nfs_dolock(ap));
 2929 }
 2930 
 2931 /*
 2932  * Print out the contents of an nfsnode.
 2933  */
 2934 static int
 2935 nfs_print(struct vop_print_args *ap)
 2936 {
 2937         struct vnode *vp = ap->a_vp;
 2938         struct nfsnode *np = VTONFS(vp);
 2939 
 2940         printf("\tfileid %ld fsid 0x%x",
 2941            np->n_vattr.va_fileid, np->n_vattr.va_fsid);
 2942         if (vp->v_type == VFIFO)
 2943                 fifo_printinfo(vp);
 2944         printf("\n");
 2945         return (0);
 2946 }
 2947 
 2948 /*
 2949  * This is the "real" nfs::bwrite(struct buf*).
 2950  * We set B_CACHE if this is a VMIO buffer.
 2951  */
 2952 int
 2953 nfs_writebp(struct buf *bp, int force __unused, struct thread *td)
 2954 {
 2955         int s;
 2956         int oldflags = bp->b_flags;
 2957 #if 0
 2958         int retv = 1;
 2959         off_t off;
 2960 #endif
 2961 
 2962         if (BUF_REFCNT(bp) == 0)
 2963                 panic("bwrite: buffer is not locked???");
 2964 
 2965         if (bp->b_flags & B_INVAL) {
 2966                 brelse(bp);
 2967                 return(0);
 2968         }
 2969 
 2970         bp->b_flags |= B_CACHE;
 2971 
 2972         /*
 2973          * Undirty the bp.  We will redirty it later if the I/O fails.
 2974          */
 2975 
 2976         s = splbio();
 2977         bundirty(bp);
 2978         bp->b_flags &= ~B_DONE;
 2979         bp->b_ioflags &= ~BIO_ERROR;
 2980         bp->b_iocmd = BIO_WRITE;
 2981 
 2982         bufobj_wref(bp->b_bufobj);
 2983         curthread->td_proc->p_stats->p_ru.ru_oublock++;
 2984         splx(s);
 2985 
 2986         /*
 2987          * Note: to avoid loopback deadlocks, we do not
 2988          * assign b_runningbufspace.
 2989          */
 2990         vfs_busy_pages(bp, 1);
 2991 
 2992         BUF_KERNPROC(bp);
 2993         bp->b_iooffset = dbtob(bp->b_blkno);
 2994         bstrategy(bp);
 2995 
 2996         if( (oldflags & B_ASYNC) == 0) {
 2997                 int rtval = bufwait(bp);
 2998 
 2999                 if (oldflags & B_DELWRI) {
 3000                         s = splbio();
 3001                         reassignbuf(bp);
 3002                         splx(s);
 3003                 }
 3004 
 3005                 brelse(bp);
 3006                 return (rtval);
 3007         }
 3008 
 3009         return (0);
 3010 }
 3011 
 3012 /*
 3013  * nfs special file access vnode op.
 3014  * Essentially just get vattr and then imitate iaccess() since the device is
 3015  * local to the client.
 3016  */
 3017 static int
 3018 nfsspec_access(struct vop_access_args *ap)
 3019 {
 3020         struct vattr *vap;
 3021         struct ucred *cred = ap->a_cred;
 3022         struct vnode *vp = ap->a_vp;
 3023         mode_t mode = ap->a_mode;
 3024         struct vattr vattr;
 3025         int error;
 3026 
 3027         /*
 3028          * Disallow write attempts on filesystems mounted read-only;
 3029          * unless the file is a socket, fifo, or a block or character
 3030          * device resident on the filesystem.
 3031          */
 3032         if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
 3033                 switch (vp->v_type) {
 3034                 case VREG:
 3035                 case VDIR:
 3036                 case VLNK:
 3037                         return (EROFS);
 3038                 default:
 3039                         break;
 3040                 }
 3041         }
 3042         vap = &vattr;
 3043         error = VOP_GETATTR(vp, vap, cred, ap->a_td);
 3044         if (error)
 3045                 return (error);
 3046         return (vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
 3047             mode, cred, NULL));
 3048 }
 3049 
 3050 /*
 3051  * Read wrapper for fifos.
 3052  */
 3053 static int
 3054 nfsfifo_read(struct vop_read_args *ap)
 3055 {
 3056         struct nfsnode *np = VTONFS(ap->a_vp);
 3057 
 3058         /*
 3059          * Set access flag.
 3060          */
 3061         np->n_flag |= NACC;
 3062         getnanotime(&np->n_atim);
 3063         return (fifo_specops.vop_read(ap));
 3064 }
 3065 
 3066 /*
 3067  * Write wrapper for fifos.
 3068  */
 3069 static int
 3070 nfsfifo_write(struct vop_write_args *ap)
 3071 {
 3072         struct nfsnode *np = VTONFS(ap->a_vp);
 3073 
 3074         /*
 3075          * Set update flag.
 3076          */
 3077         np->n_flag |= NUPD;
 3078         getnanotime(&np->n_mtim);
 3079         return (fifo_specops.vop_write(ap));
 3080 }
 3081 
 3082 /*
 3083  * Close wrapper for fifos.
 3084  *
 3085  * Update the times on the nfsnode then do fifo close.
 3086  */
 3087 static int
 3088 nfsfifo_close(struct vop_close_args *ap)
 3089 {
 3090         struct vnode *vp = ap->a_vp;
 3091         struct nfsnode *np = VTONFS(vp);
 3092         struct vattr vattr;
 3093         struct timespec ts;
 3094 
 3095         if (np->n_flag & (NACC | NUPD)) {
 3096                 getnanotime(&ts);
 3097                 if (np->n_flag & NACC)
 3098                         np->n_atim = ts;
 3099                 if (np->n_flag & NUPD)
 3100                         np->n_mtim = ts;
 3101                 np->n_flag |= NCHG;
 3102                 if (vrefcnt(vp) == 1 &&
 3103                     (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
 3104                         VATTR_NULL(&vattr);
 3105                         if (np->n_flag & NACC)
 3106                                 vattr.va_atime = np->n_atim;
 3107                         if (np->n_flag & NUPD)
 3108                                 vattr.va_mtime = np->n_mtim;
 3109                         (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
 3110                 }
 3111         }
 3112         return (fifo_specops.vop_close(ap));
 3113 }
 3114 
 3115 /*
 3116  * Just call nfs_writebp() with the force argument set to 1.
 3117  *
 3118  * NOTE: B_DONE may or may not be set in a_bp on call.
 3119  */
 3120 static int
 3121 nfs_bwrite(struct buf *bp)
 3122 {
 3123 
 3124         return (nfs_writebp(bp, 1, curthread));
 3125 }
 3126 
 3127 struct buf_ops buf_ops_nfs = {
 3128         .bop_name       =       "buf_ops_nfs",
 3129         .bop_write      =       nfs_bwrite,
 3130         .bop_strategy   =       bufstrategy,
 3131         .bop_sync       =       bufsync,
 3132         .bop_bdflush    =       bufbdflush,
 3133 };

Cache object: 96e8411ed689381cc2b23a9348678c03


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.