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

Cache object: 7b043f2b9d1c89b70e1f5b6b515c97d9


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