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/fs/nfsclient/nfs_clvnops.c

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

Cache object: 3e251e9eb2d82a6bf6e8b0e5fe812d0d


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