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

Cache object: 8345c08af5f29cc94616df2186862067


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