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


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

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

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

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

Cache object: 620de813945e95065d9a5d664959a3d0


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