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

Cache object: 9e3c733f1d0f2152ab19e58dd49db77d


[ 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.