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


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

FreeBSD/Linux Kernel Cross Reference
sys/fs/nfsclient/nfs_clport.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * Rick Macklem at The University of Guelph.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD: releng/8.0/sys/fs/nfsclient/nfs_clport.c 197192 2009-09-14 15:16:17Z rmacklem $");
   36 
   37 /*
   38  * generally, I don't like #includes inside .h files, but it seems to
   39  * be the easiest way to handle the port.
   40  */
   41 #include <fs/nfs/nfsport.h>
   42 #include <netinet/if_ether.h>
   43 #include <net/if_types.h>
   44 
   45 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
   46 extern struct vop_vector newnfs_vnodeops;
   47 extern struct vop_vector newnfs_fifoops;
   48 extern uma_zone_t newnfsnode_zone;
   49 extern struct buf_ops buf_ops_newnfs;
   50 extern int ncl_pbuf_freecnt;
   51 extern short nfsv4_cbport;
   52 extern int nfscl_enablecallb;
   53 extern int nfs_numnfscbd;
   54 extern int nfscl_inited;
   55 struct mtx nfs_clstate_mutex;
   56 struct mtx ncl_iod_mutex;
   57 NFSDLOCKMUTEX;
   58 
   59 extern void (*ncl_call_invalcaches)(struct vnode *);
   60 
   61 /*
   62  * Comparison function for vfs_hash functions.
   63  */
   64 int
   65 newnfs_vncmpf(struct vnode *vp, void *arg)
   66 {
   67         struct nfsfh *nfhp = (struct nfsfh *)arg;
   68         struct nfsnode *np = VTONFS(vp);
   69 
   70         if (np->n_fhp->nfh_len != nfhp->nfh_len ||
   71             NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len))
   72                 return (1);
   73         return (0);
   74 }
   75 
   76 /*
   77  * Look up a vnode/nfsnode by file handle.
   78  * Callers must check for mount points!!
   79  * In all cases, a pointer to a
   80  * nfsnode structure is returned.
   81  * This variant takes a "struct nfsfh *" as second argument and uses
   82  * that structure up, either by hanging off the nfsnode or FREEing it.
   83  */
   84 int
   85 nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp,
   86     struct componentname *cnp, struct thread *td, struct nfsnode **npp,
   87     void *stuff)
   88 {
   89         struct nfsnode *np, *dnp;
   90         struct vnode *vp, *nvp;
   91         struct nfsv4node *newd, *oldd;
   92         int error;
   93         u_int hash;
   94         struct nfsmount *nmp;
   95 
   96         nmp = VFSTONFS(mntp);
   97         dnp = VTONFS(dvp);
   98         *npp = NULL;
   99 
  100         hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT);
  101 
  102         error = vfs_hash_get(mntp, hash, LK_EXCLUSIVE,
  103             td, &nvp, newnfs_vncmpf, nfhp);
  104         if (error == 0 && nvp != NULL) {
  105                 /*
  106                  * I believe there is a slight chance that vgonel() could
  107                  * get called on this vnode between when vn_lock() drops
  108                  * the VI_LOCK() and vget() acquires it again, so that it
  109                  * hasn't yet had v_usecount incremented. If this were to
  110                  * happen, the VI_DOOMED flag would be set, so check for
  111                  * that here. Since we now have the v_usecount incremented,
  112                  * we should be ok until we vrele() it, if the VI_DOOMED
  113                  * flag isn't set now.
  114                  */
  115                 VI_LOCK(nvp);
  116                 if ((nvp->v_iflag & VI_DOOMED)) {
  117                         VI_UNLOCK(nvp);
  118                         vrele(nvp);
  119                         error = ENOENT;
  120                 } else {
  121                         VI_UNLOCK(nvp);
  122                 }
  123         }
  124         if (error) {
  125                 FREE((caddr_t)nfhp, M_NFSFH);
  126                 return (error);
  127         }
  128         if (nvp != NULL) {
  129                 np = VTONFS(nvp);
  130                 /*
  131                  * For NFSv4, check to see if it is the same name and
  132                  * replace the name, if it is different.
  133                  */
  134                 oldd = newd = NULL;
  135                 if ((nmp->nm_flag & NFSMNT_NFSV4) && np->n_v4 != NULL &&
  136                     nvp->v_type == VREG &&
  137                     (np->n_v4->n4_namelen != cnp->cn_namelen ||
  138                      NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
  139                      cnp->cn_namelen) ||
  140                      dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
  141                      NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
  142                      dnp->n_fhp->nfh_len))) {
  143                     MALLOC(newd, struct nfsv4node *,
  144                         sizeof (struct nfsv4node) + dnp->n_fhp->nfh_len +
  145                         + cnp->cn_namelen - 1, M_NFSV4NODE, M_WAITOK);
  146                     NFSLOCKNODE(np);
  147                     if (newd != NULL && np->n_v4 != NULL && nvp->v_type == VREG
  148                         && (np->n_v4->n4_namelen != cnp->cn_namelen ||
  149                          NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
  150                          cnp->cn_namelen) ||
  151                          dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
  152                          NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
  153                          dnp->n_fhp->nfh_len))) {
  154                         oldd = np->n_v4;
  155                         np->n_v4 = newd;
  156                         newd = NULL;
  157                         np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
  158                         np->n_v4->n4_namelen = cnp->cn_namelen;
  159                         NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
  160                             dnp->n_fhp->nfh_len);
  161                         NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
  162                             cnp->cn_namelen);
  163                     }
  164                     NFSUNLOCKNODE(np);
  165                 }
  166                 if (newd != NULL)
  167                         FREE((caddr_t)newd, M_NFSV4NODE);
  168                 if (oldd != NULL)
  169                         FREE((caddr_t)oldd, M_NFSV4NODE);
  170                 *npp = np;
  171                 FREE((caddr_t)nfhp, M_NFSFH);
  172                 return (0);
  173         }
  174 
  175         /*
  176          * Allocate before getnewvnode since doing so afterward
  177          * might cause a bogus v_data pointer to get dereferenced
  178          * elsewhere if zalloc should block.
  179          */
  180         np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
  181 
  182         error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
  183         if (error) {
  184                 uma_zfree(newnfsnode_zone, np);
  185                 FREE((caddr_t)nfhp, M_NFSFH);
  186                 return (error);
  187         }
  188         vp = nvp;
  189         vp->v_bufobj.bo_ops = &buf_ops_newnfs;
  190         vp->v_data = np;
  191         np->n_vnode = vp;
  192         /* 
  193          * Initialize the mutex even if the vnode is going to be a loser.
  194          * This simplifies the logic in reclaim, which can then unconditionally
  195          * destroy the mutex (in the case of the loser, or if hash_insert
  196          * happened to return an error no special casing is needed).
  197          */
  198         mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
  199 
  200         /* 
  201          * Are we getting the root? If so, make sure the vnode flags
  202          * are correct 
  203          */
  204         if ((nfhp->nfh_len == nmp->nm_fhsize) &&
  205             !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len)) {
  206                 if (vp->v_type == VNON)
  207                         vp->v_type = VDIR;
  208                 vp->v_vflag |= VV_ROOT;
  209         }
  210         
  211         np->n_fhp = nfhp;
  212         /*
  213          * For NFSv4, we have to attach the directory file handle and
  214          * file name, so that Open Ops can be done later.
  215          */
  216         if (nmp->nm_flag & NFSMNT_NFSV4) {
  217                 MALLOC(np->n_v4, struct nfsv4node *, sizeof (struct nfsv4node)
  218                     + dnp->n_fhp->nfh_len + cnp->cn_namelen - 1, M_NFSV4NODE,
  219                     M_WAITOK);
  220                 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
  221                 np->n_v4->n4_namelen = cnp->cn_namelen;
  222                 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
  223                     dnp->n_fhp->nfh_len);
  224                 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
  225                     cnp->cn_namelen);
  226         } else {
  227                 np->n_v4 = NULL;
  228         }
  229 
  230         /*
  231          * NFS supports recursive and shared locking.
  232          */
  233         VN_LOCK_AREC(vp);
  234         VN_LOCK_ASHARE(vp);
  235         lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
  236         error = insmntque(vp, mntp);
  237         if (error != 0) {
  238                 *npp = NULL;
  239                 mtx_destroy(&np->n_mtx);
  240                 FREE((caddr_t)nfhp, M_NFSFH);
  241                 if (np->n_v4 != NULL)
  242                         FREE((caddr_t)np->n_v4, M_NFSV4NODE);
  243                 uma_zfree(newnfsnode_zone, np);
  244                 return (error);
  245         }
  246         error = vfs_hash_insert(vp, hash, LK_EXCLUSIVE, 
  247             td, &nvp, newnfs_vncmpf, nfhp);
  248         if (error)
  249                 return (error);
  250         if (nvp != NULL) {
  251                 *npp = VTONFS(nvp);
  252                 /* vfs_hash_insert() vput()'s the losing vnode */
  253                 return (0);
  254         }
  255         *npp = np;
  256 
  257         return (0);
  258 }
  259 
  260 /*
  261  * Anothe variant of nfs_nget(). This one is only used by reopen. It
  262  * takes almost the same args as nfs_nget(), but only succeeds if an entry
  263  * exists in the cache. (Since files should already be "open" with a
  264  * vnode ref cnt on the node when reopen calls this, it should always
  265  * succeed.)
  266  * Also, don't get a vnode lock, since it may already be locked by some
  267  * other process that is handling it. This is ok, since all other threads
  268  * on the client are blocked by the nfsc_lock being exclusively held by the
  269  * caller of this function.
  270  */
  271 int
  272 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
  273     struct thread *td, struct nfsnode **npp)
  274 {
  275         struct vnode *nvp;
  276         u_int hash;
  277         struct nfsfh *nfhp;
  278         int error;
  279 
  280         *npp = NULL;
  281         /* For forced dismounts, just return error. */
  282         if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
  283                 return (EINTR);
  284         MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
  285             M_NFSFH, M_WAITOK);
  286         bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
  287         nfhp->nfh_len = fhsize;
  288 
  289         hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
  290 
  291         /*
  292          * First, try to get the vnode locked, but don't block for the lock.
  293          */
  294         error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp,
  295             newnfs_vncmpf, nfhp);
  296         if (error == 0 && nvp != NULL) {
  297                 VOP_UNLOCK(nvp, 0);
  298         } else if (error == EBUSY) {
  299                 /*
  300                  * The LK_EXCLOTHER lock type tells nfs_lock1() to not try
  301                  * and lock the vnode, but just get a v_usecount on it.
  302                  * LK_NOWAIT is set so that when vget() returns ENOENT,
  303                  * vfs_hash_get() fails instead of looping.
  304                  * If this succeeds, it is safe so long as a vflush() with
  305                  * FORCECLOSE has not been done. Since the Renew thread is
  306                  * stopped and the MNTK_UNMOUNTF flag is set before doing
  307                  * a vflush() with FORCECLOSE, we should be ok here.
  308                  */
  309                 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
  310                         error = EINTR;
  311                 else
  312                         error = vfs_hash_get(mntp, hash,
  313                             (LK_EXCLOTHER | LK_NOWAIT), td, &nvp,
  314                             newnfs_vncmpf, nfhp);
  315         }
  316         FREE(nfhp, M_NFSFH);
  317         if (error)
  318                 return (error);
  319         if (nvp != NULL) {
  320                 *npp = VTONFS(nvp);
  321                 return (0);
  322         }
  323         return (EINVAL);
  324 }
  325 
  326 /*
  327  * Load the attribute cache (that lives in the nfsnode entry) with
  328  * the attributes of the second argument and
  329  * Iff vaper not NULL
  330  *    copy the attributes to *vaper
  331  * Similar to nfs_loadattrcache(), except the attributes are passed in
  332  * instead of being parsed out of the mbuf list.
  333  */
  334 int
  335 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper,
  336     void *stuff, int writeattr, int dontshrink)
  337 {
  338         struct vnode *vp = *vpp;
  339         struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper;
  340         struct nfsnode *np;
  341         struct nfsmount *nmp;
  342         struct timespec mtime_save;
  343         struct thread *td = curthread;
  344 
  345         /*
  346          * If v_type == VNON it is a new node, so fill in the v_type,
  347          * n_mtime fields. Check to see if it represents a special 
  348          * device, and if so, check for a possible alias. Once the
  349          * correct vnode has been obtained, fill in the rest of the
  350          * information.
  351          */
  352         np = VTONFS(vp);
  353         NFSLOCKNODE(np);
  354         if (vp->v_type != nvap->va_type) {
  355                 vp->v_type = nvap->va_type;
  356                 if (vp->v_type == VFIFO)
  357                         vp->v_op = &newnfs_fifoops;
  358                 np->n_mtime = nvap->va_mtime;
  359         }
  360         nmp = VFSTONFS(vp->v_mount);
  361         vap = &np->n_vattr.na_vattr;
  362         mtime_save = vap->va_mtime;
  363         if (writeattr) {
  364                 np->n_vattr.na_filerev = nap->na_filerev;
  365                 np->n_vattr.na_size = nap->na_size;
  366                 np->n_vattr.na_mtime = nap->na_mtime;
  367                 np->n_vattr.na_ctime = nap->na_ctime;
  368                 np->n_vattr.na_fsid = nap->na_fsid;
  369         } else {
  370                 NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
  371                     sizeof (struct nfsvattr));
  372         }
  373 
  374         /*
  375          * For NFSv4, if the node's fsid is not equal to the mount point's
  376          * fsid, return the low order 32bits of the node's fsid. This
  377          * allows getcwd(3) to work. There is a chance that the fsid might
  378          * be the same as a local fs, but since this is in an NFS mount
  379          * point, I don't think that will cause any problems?
  380          */
  381         if ((nmp->nm_flag & (NFSMNT_NFSV4 | NFSMNT_HASSETFSID)) ==
  382             (NFSMNT_NFSV4 | NFSMNT_HASSETFSID) &&
  383             (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
  384              nmp->nm_fsid[1] != np->n_vattr.na_filesid[1]))
  385                 vap->va_fsid = np->n_vattr.na_filesid[0];
  386         else
  387                 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
  388         np->n_attrstamp = time_second;
  389         /* Timestamp the NFS otw getattr fetch */
  390         if (td->td_proc) {
  391                 np->n_ac_ts_tid = td->td_tid;
  392                 np->n_ac_ts_pid = td->td_proc->p_pid;
  393                 np->n_ac_ts_syscalls = td->td_syscalls;
  394         } else
  395                 bzero(&np->n_ac_ts, sizeof(struct nfs_attrcache_timestamp));
  396         
  397         if (vap->va_size != np->n_size) {
  398                 if (vap->va_type == VREG) {
  399                         if (dontshrink && vap->va_size < np->n_size) {
  400                                 /*
  401                                  * We've been told not to shrink the file;
  402                                  * zero np->n_attrstamp to indicate that
  403                                  * the attributes are stale.
  404                                  */
  405                                 vap->va_size = np->n_size;
  406                                 np->n_attrstamp = 0;
  407                         } else if (np->n_flag & NMODIFIED) {
  408                                 /*
  409                                  * We've modified the file: Use the larger
  410                                  * of our size, and the server's size.
  411                                  */
  412                                 if (vap->va_size < np->n_size) {
  413                                         vap->va_size = np->n_size;
  414                                 } else {
  415                                         np->n_size = vap->va_size;
  416                                         np->n_flag |= NSIZECHANGED;
  417                                 }
  418                         } else {
  419                                 np->n_size = vap->va_size;
  420                                 np->n_flag |= NSIZECHANGED;
  421                         }
  422                         vnode_pager_setsize(vp, np->n_size);
  423                 } else {
  424                         np->n_size = vap->va_size;
  425                 }
  426         }
  427         /*
  428          * The following checks are added to prevent a race between (say)
  429          * a READDIR+ and a WRITE. 
  430          * READDIR+, WRITE requests sent out.
  431          * READDIR+ resp, WRITE resp received on client.
  432          * However, the WRITE resp was handled before the READDIR+ resp
  433          * causing the post op attrs from the write to be loaded first
  434          * and the attrs from the READDIR+ to be loaded later. If this 
  435          * happens, we have stale attrs loaded into the attrcache.
  436          * We detect this by for the mtime moving back. We invalidate the 
  437          * attrcache when this happens.
  438          */
  439         if (timespeccmp(&mtime_save, &vap->va_mtime, >))
  440                 /* Size changed or mtime went backwards */
  441                 np->n_attrstamp = 0;
  442         if (vaper != NULL) {
  443                 NFSBCOPY((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
  444                 if (np->n_flag & NCHG) {
  445                         if (np->n_flag & NACC)
  446                                 vaper->va_atime = np->n_atim;
  447                         if (np->n_flag & NUPD)
  448                                 vaper->va_mtime = np->n_mtim;
  449                 }
  450         }
  451         NFSUNLOCKNODE(np);
  452         return (0);
  453 }
  454 
  455 /*
  456  * Fill in the client id name. For these bytes:
  457  * 1 - they must be unique
  458  * 2 - they should be persistent across client reboots
  459  * 1 is more critical than 2
  460  * Use the mount point's unique id plus either the uuid or, if that
  461  * isn't set, random junk.
  462  */
  463 void
  464 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
  465 {
  466         int uuidlen;
  467 
  468         /*
  469          * First, put in the 64bit mount point identifier.
  470          */
  471         if (idlen >= sizeof (u_int64_t)) {
  472                 NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
  473                 cp += sizeof (u_int64_t);
  474                 idlen -= sizeof (u_int64_t);
  475         }
  476 
  477         /*
  478          * If uuid is non-zero length, use it.
  479          */
  480         uuidlen = strlen(uuid);
  481         if (uuidlen > 0 && idlen >= uuidlen) {
  482                 NFSBCOPY(uuid, cp, uuidlen);
  483                 cp += uuidlen;
  484                 idlen -= uuidlen;
  485         }
  486 
  487         /*
  488          * This only normally happens if the uuid isn't set.
  489          */
  490         while (idlen > 0) {
  491                 *cp++ = (u_int8_t)(arc4random() % 256);
  492                 idlen--;
  493         }
  494 }
  495 
  496 /*
  497  * Fill in a lock owner name. For now, pid + the process's creation time.
  498  */
  499 void
  500 nfscl_filllockowner(struct thread *td, u_int8_t *cp)
  501 {
  502         union {
  503                 u_int32_t       lval;
  504                 u_int8_t        cval[4];
  505         } tl;
  506         struct proc *p;
  507 
  508 if (td == NULL) {
  509         printf("NULL td\n");
  510         bzero(cp, 12);
  511         return;
  512 }
  513         p = td->td_proc;
  514 if (p == NULL) {
  515         printf("NULL pid\n");
  516         bzero(cp, 12);
  517         return;
  518 }
  519         tl.lval = p->p_pid;
  520         *cp++ = tl.cval[0];
  521         *cp++ = tl.cval[1];
  522         *cp++ = tl.cval[2];
  523         *cp++ = tl.cval[3];
  524 if (p->p_stats == NULL) {
  525         printf("pstats null\n");
  526         bzero(cp, 8);
  527         return;
  528 }
  529         tl.lval = p->p_stats->p_start.tv_sec;
  530         *cp++ = tl.cval[0];
  531         *cp++ = tl.cval[1];
  532         *cp++ = tl.cval[2];
  533         *cp++ = tl.cval[3];
  534         tl.lval = p->p_stats->p_start.tv_usec;
  535         *cp++ = tl.cval[0];
  536         *cp++ = tl.cval[1];
  537         *cp++ = tl.cval[2];
  538         *cp = tl.cval[3];
  539 }
  540 
  541 /*
  542  * Find the parent process for the thread passed in as an argument.
  543  * If none exists, return NULL, otherwise return a thread for the parent.
  544  * (Can be any of the threads, since it is only used for td->td_proc.)
  545  */
  546 NFSPROC_T *
  547 nfscl_getparent(struct thread *td)
  548 {
  549         struct proc *p;
  550         struct thread *ptd;
  551 
  552         if (td == NULL)
  553                 return (NULL);
  554         p = td->td_proc;
  555         if (p->p_pid == 0)
  556                 return (NULL);
  557         p = p->p_pptr;
  558         if (p == NULL)
  559                 return (NULL);
  560         ptd = TAILQ_FIRST(&p->p_threads);
  561         return (ptd);
  562 }
  563 
  564 /*
  565  * Start up the renew kernel thread.
  566  */
  567 static void
  568 start_nfscl(void *arg)
  569 {
  570         struct nfsclclient *clp;
  571         struct thread *td;
  572 
  573         clp = (struct nfsclclient *)arg;
  574         td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
  575         nfscl_renewthread(clp, td);
  576         kproc_exit(0);
  577 }
  578 
  579 void
  580 nfscl_start_renewthread(struct nfsclclient *clp)
  581 {
  582 
  583         kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
  584             "nfscl");
  585 }
  586 
  587 /*
  588  * Handle wcc_data.
  589  * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
  590  * as the first Op after PutFH.
  591  * (For NFSv4, the postop attributes are after the Op, so they can't be
  592  *  parsed here. A separate call to nfscl_postop_attr() is required.)
  593  */
  594 int
  595 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
  596     struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff)
  597 {
  598         u_int32_t *tl;
  599         struct nfsnode *np = VTONFS(vp);
  600         struct nfsvattr nfsva;
  601         int error = 0;
  602 
  603         if (wccflagp != NULL)
  604                 *wccflagp = 0;
  605         if (nd->nd_flag & ND_NFSV3) {
  606                 *flagp = 0;
  607                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
  608                 if (*tl == newnfs_true) {
  609                         NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
  610                         if (wccflagp != NULL) {
  611                                 mtx_lock(&np->n_mtx);
  612                                 *wccflagp = (np->n_mtime.tv_sec ==
  613                                     fxdr_unsigned(u_int32_t, *(tl + 2)) &&
  614                                     np->n_mtime.tv_nsec ==
  615                                     fxdr_unsigned(u_int32_t, *(tl + 3)));
  616                                 mtx_unlock(&np->n_mtx);
  617                         }
  618                 }
  619                 error = nfscl_postop_attr(nd, nap, flagp, stuff);
  620         } else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
  621             == (ND_NFSV4 | ND_V4WCCATTR)) {
  622                 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
  623                     NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
  624                     NULL, NULL, NULL, NULL, NULL);
  625                 if (error)
  626                         return (error);
  627                 /*
  628                  * Get rid of Op# and status for next op.
  629                  */
  630                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  631                 if (*++tl)
  632                         nd->nd_flag |= ND_NOMOREDATA;
  633                 if (wccflagp != NULL &&
  634                     nfsva.na_vattr.va_mtime.tv_sec != 0) {
  635                         mtx_lock(&np->n_mtx);
  636                         *wccflagp = (np->n_mtime.tv_sec ==
  637                             nfsva.na_vattr.va_mtime.tv_sec &&
  638                             np->n_mtime.tv_nsec ==
  639                             nfsva.na_vattr.va_mtime.tv_sec);
  640                         mtx_unlock(&np->n_mtx);
  641                 }
  642         }
  643 nfsmout:
  644         return (error);
  645 }
  646 
  647 /*
  648  * Get postop attributes.
  649  */
  650 int
  651 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp,
  652     void *stuff)
  653 {
  654         u_int32_t *tl;
  655         int error = 0;
  656 
  657         *retp = 0;
  658         if (nd->nd_flag & ND_NOMOREDATA)
  659                 return (error);
  660         if (nd->nd_flag & ND_NFSV3) {
  661                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
  662                 *retp = fxdr_unsigned(int, *tl);
  663         } else if (nd->nd_flag & ND_NFSV4) {
  664                 /*
  665                  * For NFSv4, the postop attr are at the end, so no point
  666                  * in looking if nd_repstat != 0.
  667                  */
  668                 if (!nd->nd_repstat) {
  669                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  670                         if (*(tl + 1))
  671                                 /* should never happen since nd_repstat != 0 */
  672                                 nd->nd_flag |= ND_NOMOREDATA;
  673                         else
  674                                 *retp = 1;
  675                 }
  676         } else if (!nd->nd_repstat) {
  677                 /* For NFSv2, the attributes are here iff nd_repstat == 0 */
  678                 *retp = 1;
  679         }
  680         if (*retp) {
  681                 error = nfsm_loadattr(nd, nap);
  682                 if (error)
  683                         *retp = 0;
  684         }
  685 nfsmout:
  686         return (error);
  687 }
  688 
  689 /*
  690  * Fill in the setable attributes. The full argument indicates whether
  691  * to fill in them all or just mode and time.
  692  */
  693 void
  694 nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
  695     struct vnode *vp, int flags, u_int32_t rdev)
  696 {
  697         u_int32_t *tl;
  698         struct nfsv2_sattr *sp;
  699         nfsattrbit_t attrbits;
  700         struct timeval curtime;
  701 
  702         switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
  703         case ND_NFSV2:
  704                 NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
  705                 if (vap->va_mode == (mode_t)VNOVAL)
  706                         sp->sa_mode = newnfs_xdrneg1;
  707                 else
  708                         sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
  709                 if (vap->va_uid == (uid_t)VNOVAL)
  710                         sp->sa_uid = newnfs_xdrneg1;
  711                 else
  712                         sp->sa_uid = txdr_unsigned(vap->va_uid);
  713                 if (vap->va_gid == (gid_t)VNOVAL)
  714                         sp->sa_gid = newnfs_xdrneg1;
  715                 else
  716                         sp->sa_gid = txdr_unsigned(vap->va_gid);
  717                 if (flags & NFSSATTR_SIZE0)
  718                         sp->sa_size = 0;
  719                 else if (flags & NFSSATTR_SIZENEG1)
  720                         sp->sa_size = newnfs_xdrneg1;
  721                 else if (flags & NFSSATTR_SIZERDEV)
  722                         sp->sa_size = txdr_unsigned(rdev);
  723                 else
  724                         sp->sa_size = txdr_unsigned(vap->va_size);
  725                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
  726                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
  727                 break;
  728         case ND_NFSV3:
  729                 getmicrotime(&curtime);
  730                 if (vap->va_mode != (mode_t)VNOVAL) {
  731                         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  732                         *tl++ = newnfs_true;
  733                         *tl = txdr_unsigned(vap->va_mode);
  734                 } else {
  735                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
  736                         *tl = newnfs_false;
  737                 }
  738                 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) {
  739                         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  740                         *tl++ = newnfs_true;
  741                         *tl = txdr_unsigned(vap->va_uid);
  742                 } else {
  743                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
  744                         *tl = newnfs_false;
  745                 }
  746                 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) {
  747                         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  748                         *tl++ = newnfs_true;
  749                         *tl = txdr_unsigned(vap->va_gid);
  750                 } else {
  751                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
  752                         *tl = newnfs_false;
  753                 }
  754                 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) {
  755                         NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
  756                         *tl++ = newnfs_true;
  757                         txdr_hyper(vap->va_size, tl);
  758                 } else {
  759                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
  760                         *tl = newnfs_false;
  761                 }
  762                 if (vap->va_atime.tv_sec != VNOVAL) {
  763                         if (vap->va_atime.tv_sec != curtime.tv_sec) {
  764                                 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
  765                                 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
  766                                 txdr_nfsv3time(&vap->va_atime, tl);
  767                         } else {
  768                                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
  769                                 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
  770                         }
  771                 } else {
  772                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
  773                         *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
  774                 }
  775                 if (vap->va_mtime.tv_sec != VNOVAL) {
  776                         if (vap->va_mtime.tv_sec != curtime.tv_sec) {
  777                                 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
  778                                 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
  779                                 txdr_nfsv3time(&vap->va_mtime, tl);
  780                         } else {
  781                                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
  782                                 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
  783                         }
  784                 } else {
  785                         NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
  786                         *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
  787                 }
  788                 break;
  789         case ND_NFSV4:
  790                 NFSZERO_ATTRBIT(&attrbits);
  791                 if (vap->va_mode != (mode_t)VNOVAL)
  792                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
  793                 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL)
  794                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
  795                 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL)
  796                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
  797                 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL)
  798                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
  799                 if (vap->va_atime.tv_sec != VNOVAL)
  800                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
  801                 if (vap->va_mtime.tv_sec != VNOVAL)
  802                         NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
  803                 (void) nfsv4_fillattr(nd, vp, NULL, vap, NULL, 0, &attrbits,
  804                     NULL, NULL, 0, 0);
  805                 break;
  806         };
  807 }
  808 
  809 /*
  810  * nfscl_request() - mostly a wrapper for newnfs_request().
  811  */
  812 int
  813 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
  814     struct ucred *cred, void *stuff)
  815 {
  816         int ret, vers;
  817         struct nfsmount *nmp;
  818 
  819         nmp = VFSTONFS(vp->v_mount);
  820         if (nd->nd_flag & ND_NFSV4)
  821                 vers = NFS_VER4;
  822         else if (nd->nd_flag & ND_NFSV3)
  823                 vers = NFS_VER3;
  824         else
  825                 vers = NFS_VER2;
  826         ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
  827                 NFS_PROG, vers, NULL, 1, NULL);
  828         return (ret);
  829 }
  830 
  831 /*
  832  * fill in this bsden's variant of statfs using nfsstatfs.
  833  */
  834 void
  835 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
  836 {
  837         struct statfs *sbp = (struct statfs *)statfs;
  838         nfsquad_t tquad;
  839 
  840         if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
  841                 sbp->f_bsize = NFS_FABLKSIZE;
  842                 tquad.qval = sfp->sf_tbytes;
  843                 sbp->f_blocks = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
  844                 tquad.qval = sfp->sf_fbytes;
  845                 sbp->f_bfree = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
  846                 tquad.qval = sfp->sf_abytes;
  847                 sbp->f_bavail = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
  848                 tquad.qval = sfp->sf_tfiles;
  849                 sbp->f_files = (tquad.lval[0] & 0x7fffffff);
  850                 tquad.qval = sfp->sf_ffiles;
  851                 sbp->f_ffree = (tquad.lval[0] & 0x7fffffff);
  852         } else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
  853                 sbp->f_bsize = (int32_t)sfp->sf_bsize;
  854                 sbp->f_blocks = (int32_t)sfp->sf_blocks;
  855                 sbp->f_bfree = (int32_t)sfp->sf_bfree;
  856                 sbp->f_bavail = (int32_t)sfp->sf_bavail;
  857                 sbp->f_files = 0;
  858                 sbp->f_ffree = 0;
  859         }
  860 }
  861 
  862 /*
  863  * Use the fsinfo stuff to update the mount point.
  864  */
  865 void
  866 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
  867 {
  868 
  869         if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
  870             fsp->fs_wtpref >= NFS_FABLKSIZE)
  871                 nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
  872                     ~(NFS_FABLKSIZE - 1);
  873         if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
  874                 nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
  875                 if (nmp->nm_wsize == 0)
  876                         nmp->nm_wsize = fsp->fs_wtmax;
  877         }
  878         if (nmp->nm_wsize < NFS_FABLKSIZE)
  879                 nmp->nm_wsize = NFS_FABLKSIZE;
  880         if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
  881             fsp->fs_rtpref >= NFS_FABLKSIZE)
  882                 nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
  883                     ~(NFS_FABLKSIZE - 1);
  884         if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
  885                 nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
  886                 if (nmp->nm_rsize == 0)
  887                         nmp->nm_rsize = fsp->fs_rtmax;
  888         }
  889         if (nmp->nm_rsize < NFS_FABLKSIZE)
  890                 nmp->nm_rsize = NFS_FABLKSIZE;
  891         if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
  892             && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
  893                 nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
  894                     ~(NFS_DIRBLKSIZ - 1);
  895         if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
  896                 nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
  897                 if (nmp->nm_readdirsize == 0)
  898                         nmp->nm_readdirsize = fsp->fs_rtmax;
  899         }
  900         if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
  901                 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
  902         if (fsp->fs_maxfilesize > 0 &&
  903             fsp->fs_maxfilesize < nmp->nm_maxfilesize)
  904                 nmp->nm_maxfilesize = fsp->fs_maxfilesize;
  905         nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
  906         nmp->nm_state |= NFSSTA_GOTFSINFO;
  907 }
  908 
  909 /*
  910  * Get a pointer to my IP addrress and return it.
  911  * Return NULL if you can't find one.
  912  */
  913 u_int8_t *
  914 nfscl_getmyip(struct nfsmount *nmp, int *isinet6p)
  915 {
  916         struct sockaddr_in sad, *sin;
  917         struct rtentry *rt;
  918         u_int8_t *retp = NULL;
  919         static struct in_addr laddr;
  920 
  921         *isinet6p = 0;
  922         /*
  923          * Loop up a route for the destination address.
  924          */
  925         if (nmp->nm_nam->sa_family == AF_INET) {
  926                 bzero(&sad, sizeof (sad));
  927                 sin = (struct sockaddr_in *)nmp->nm_nam;
  928                 sad.sin_family = AF_INET;
  929                 sad.sin_len = sizeof (struct sockaddr_in);
  930                 sad.sin_addr.s_addr = sin->sin_addr.s_addr;
  931                 rt = rtalloc1((struct sockaddr *)&sad, 0, 0UL);
  932                 if (rt != NULL) {
  933                         if (rt->rt_ifp != NULL &&
  934                             rt->rt_ifa != NULL &&
  935                             ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
  936                             rt->rt_ifa->ifa_addr->sa_family == AF_INET) {
  937                                 sin = (struct sockaddr_in *)
  938                                     rt->rt_ifa->ifa_addr;
  939                                 laddr.s_addr = sin->sin_addr.s_addr;
  940                                 retp = (u_int8_t *)&laddr;
  941                         }
  942                         RTFREE_LOCKED(rt);
  943                 }
  944 #ifdef INET6
  945         } else if (nmp->nm_nam->sa_family == AF_INET6) {
  946                 struct sockaddr_in6 sad6, *sin6;
  947                 static struct in6_addr laddr6;
  948 
  949                 bzero(&sad6, sizeof (sad6));
  950                 sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
  951                 sad6.sin6_family = AF_INET6;
  952                 sad6.sin6_len = sizeof (struct sockaddr_in6);
  953                 sad6.sin6_addr = sin6->sin6_addr;
  954                 rt = rtalloc1((struct sockaddr *)&sad6, 0, 0UL);
  955                 if (rt != NULL) {
  956                         if (rt->rt_ifp != NULL &&
  957                             rt->rt_ifa != NULL &&
  958                             ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
  959                             rt->rt_ifa->ifa_addr->sa_family == AF_INET6) {
  960                                 sin6 = (struct sockaddr_in6 *)
  961                                     rt->rt_ifa->ifa_addr;
  962                                 laddr6 = sin6->sin6_addr;
  963                                 retp = (u_int8_t *)&laddr6;
  964                                 *isinet6p = 1;
  965                         }
  966                         RTFREE_LOCKED(rt);
  967                 }
  968 #endif
  969         }
  970         return (retp);
  971 }
  972 
  973 /*
  974  * Copy NFS uid, gids from the cred structure.
  975  */
  976 void
  977 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
  978 {
  979         int i;
  980 
  981         nfscr->nfsc_uid = cr->cr_uid;
  982         nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1);
  983         for (i = 0; i < nfscr->nfsc_ngroups; i++)
  984                 nfscr->nfsc_groups[i] = cr->cr_groups[i];
  985 }
  986 
  987 
  988 /*
  989  * Do any client specific initialization.
  990  */
  991 void
  992 nfscl_init(void)
  993 {
  994         static int inited = 0;
  995 
  996         if (inited)
  997                 return;
  998         inited = 1;
  999         nfscl_inited = 1;
 1000         ncl_pbuf_freecnt = nswbuf / 2 + 1;
 1001 }
 1002 
 1003 /*
 1004  * Check each of the attributes to be set, to ensure they aren't already
 1005  * the correct value. Disable setting ones already correct.
 1006  */
 1007 int
 1008 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
 1009 {
 1010 
 1011         if (vap->va_mode != (mode_t)VNOVAL) {
 1012                 if (vap->va_mode == nvap->na_mode)
 1013                         vap->va_mode = (mode_t)VNOVAL;
 1014         }
 1015         if (vap->va_uid != (uid_t)VNOVAL) {
 1016                 if (vap->va_uid == nvap->na_uid)
 1017                         vap->va_uid = (uid_t)VNOVAL;
 1018         }
 1019         if (vap->va_gid != (gid_t)VNOVAL) {
 1020                 if (vap->va_gid == nvap->na_gid)
 1021                         vap->va_gid = (gid_t)VNOVAL;
 1022         }
 1023         if (vap->va_size != VNOVAL) {
 1024                 if (vap->va_size == nvap->na_size)
 1025                         vap->va_size = VNOVAL;
 1026         }
 1027 
 1028         /*
 1029          * We are normally called with only a partially initialized
 1030          * VAP.  Since the NFSv3 spec says that server may use the
 1031          * file attributes to store the verifier, the spec requires
 1032          * us to do a SETATTR RPC. FreeBSD servers store the verifier
 1033          * in atime, but we can't really assume that all servers will
 1034          * so we ensure that our SETATTR sets both atime and mtime.
 1035          */
 1036         if (vap->va_mtime.tv_sec == VNOVAL)
 1037                 vfs_timestamp(&vap->va_mtime);
 1038         if (vap->va_atime.tv_sec == VNOVAL)
 1039                 vap->va_atime = vap->va_mtime;
 1040         return (1);
 1041 }
 1042 
 1043 /*
 1044  * Map nfsv4 errors to errno.h errors.
 1045  * The uid and gid arguments are only used for NFSERR_BADOWNER and that
 1046  * error should only be returned for the Open, Create and Setattr Ops.
 1047  * As such, most calls can just pass in 0 for those arguments.
 1048  */
 1049 APPLESTATIC int
 1050 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
 1051 {
 1052         struct proc *p;
 1053 
 1054         if (error < 10000)
 1055                 return (error);
 1056         if (td != NULL)
 1057                 p = td->td_proc;
 1058         else
 1059                 p = NULL;
 1060         switch (error) {
 1061         case NFSERR_BADOWNER:
 1062                 tprintf(p, LOG_INFO,
 1063                     "No name and/or group mapping for uid,gid:(%d,%d)\n",
 1064                     uid, gid);
 1065                 return (EPERM);
 1066         case NFSERR_STALECLIENTID:
 1067         case NFSERR_STALESTATEID:
 1068         case NFSERR_EXPIRED:
 1069         case NFSERR_BADSTATEID:
 1070                 printf("nfsv4 recover err returned %d\n", error);
 1071                 return (EIO);
 1072         case NFSERR_BADHANDLE:
 1073         case NFSERR_SERVERFAULT:
 1074         case NFSERR_BADTYPE:
 1075         case NFSERR_FHEXPIRED:
 1076         case NFSERR_RESOURCE:
 1077         case NFSERR_MOVED:
 1078         case NFSERR_NOFILEHANDLE:
 1079         case NFSERR_MINORVERMISMATCH:
 1080         case NFSERR_OLDSTATEID:
 1081         case NFSERR_BADSEQID:
 1082         case NFSERR_LEASEMOVED:
 1083         case NFSERR_RECLAIMBAD:
 1084         case NFSERR_BADXDR:
 1085         case NFSERR_BADCHAR:
 1086         case NFSERR_BADNAME:
 1087         case NFSERR_OPILLEGAL:
 1088                 printf("nfsv4 client/server protocol prob err=%d\n",
 1089                     error);
 1090                 return (EIO);
 1091         default:
 1092                 tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
 1093                 return (EIO);
 1094         };
 1095 }
 1096 
 1097 /*
 1098  * Locate a process by number; return only "live" processes -- i.e., neither
 1099  * zombies nor newly born but incompletely initialized processes.  By not
 1100  * returning processes in the PRS_NEW state, we allow callers to avoid
 1101  * testing for that condition to avoid dereferencing p_ucred, et al.
 1102  * Identical to pfind() in kern_proc.c, except it assume the list is
 1103  * already locked.
 1104  */
 1105 static struct proc *
 1106 pfind_locked(pid_t pid)
 1107 {
 1108         struct proc *p;
 1109 
 1110         LIST_FOREACH(p, PIDHASH(pid), p_hash)
 1111                 if (p->p_pid == pid) {
 1112                         if (p->p_state == PRS_NEW) {
 1113                                 p = NULL;
 1114                                 break;
 1115                         }
 1116                         PROC_LOCK(p);
 1117                         break;
 1118                 }
 1119         return (p);
 1120 }
 1121 
 1122 /*
 1123  * Check to see if the process for this owner exists. Return 1 if it doesn't
 1124  * and 0 otherwise.
 1125  */
 1126 int
 1127 nfscl_procdoesntexist(u_int8_t *own)
 1128 {
 1129         union {
 1130                 u_int32_t       lval;
 1131                 u_int8_t        cval[4];
 1132         } tl;
 1133         struct proc *p;
 1134         pid_t pid;
 1135         int ret = 0;
 1136 
 1137         tl.cval[0] = *own++;
 1138         tl.cval[1] = *own++;
 1139         tl.cval[2] = *own++;
 1140         tl.cval[3] = *own++;
 1141         pid = tl.lval;
 1142         p = pfind_locked(pid);
 1143         if (p == NULL)
 1144                 return (1);
 1145         if (p->p_stats == NULL) {
 1146                 PROC_UNLOCK(p);
 1147                 return (0);
 1148         }
 1149         tl.cval[0] = *own++;
 1150         tl.cval[1] = *own++;
 1151         tl.cval[2] = *own++;
 1152         tl.cval[3] = *own++;
 1153         if (tl.lval != p->p_stats->p_start.tv_sec) {
 1154                 ret = 1;
 1155         } else {
 1156                 tl.cval[0] = *own++;
 1157                 tl.cval[1] = *own++;
 1158                 tl.cval[2] = *own++;
 1159                 tl.cval[3] = *own;
 1160                 if (tl.lval != p->p_stats->p_start.tv_usec)
 1161                         ret = 1;
 1162         }
 1163         PROC_UNLOCK(p);
 1164         return (ret);
 1165 }
 1166 
 1167 /*
 1168  * - nfs pseudo system call for the client
 1169  */
 1170 /*
 1171  * MPSAFE
 1172  */
 1173 static int
 1174 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
 1175 {
 1176         struct file *fp;
 1177         struct nfscbd_args nfscbdarg;
 1178         struct nfsd_nfscbd_args nfscbdarg2;
 1179         int error;
 1180 
 1181         if (uap->flag & NFSSVC_CBADDSOCK) {
 1182                 error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
 1183                 if (error)
 1184                         return (error);
 1185                 if ((error = fget(td, nfscbdarg.sock, &fp)) != 0) {
 1186                         return (error);
 1187                 }
 1188                 if (fp->f_type != DTYPE_SOCKET) {
 1189                         fdrop(fp, td);
 1190                         return (EPERM);
 1191                 }
 1192                 error = nfscbd_addsock(fp);
 1193                 fdrop(fp, td);
 1194                 if (!error && nfscl_enablecallb == 0) {
 1195                         nfsv4_cbport = nfscbdarg.port;
 1196                         nfscl_enablecallb = 1;
 1197                 }
 1198         } else if (uap->flag & NFSSVC_NFSCBD) {
 1199                 if (uap->argp == NULL) 
 1200                         return (EINVAL);
 1201                 error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
 1202                     sizeof(nfscbdarg2));
 1203                 if (error)
 1204                         return (error);
 1205                 error = nfscbd_nfsd(td, &nfscbdarg2);
 1206         } else {
 1207                 error = EINVAL;
 1208         }
 1209         return (error);
 1210 }
 1211 
 1212 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
 1213 
 1214 /*
 1215  * Called once to initialize data structures...
 1216  */
 1217 static int
 1218 nfscl_modevent(module_t mod, int type, void *data)
 1219 {
 1220         int error = 0;
 1221         static int loaded = 0;
 1222 
 1223         switch (type) {
 1224         case MOD_LOAD:
 1225                 if (loaded)
 1226                         return (0);
 1227                 newnfs_portinit();
 1228                 mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL,
 1229                     MTX_DEF);
 1230                 mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
 1231                 nfscl_init();
 1232                 NFSD_LOCK();
 1233                 nfsrvd_cbinit(0);
 1234                 NFSD_UNLOCK();
 1235                 ncl_call_invalcaches = ncl_invalcaches;
 1236                 nfsd_call_nfscl = nfssvc_nfscl;
 1237                 loaded = 1;
 1238                 break;
 1239 
 1240         case MOD_UNLOAD:
 1241                 if (nfs_numnfscbd != 0) {
 1242                         error = EBUSY;
 1243                         break;
 1244                 }
 1245 
 1246                 ncl_call_invalcaches = NULL;
 1247                 nfsd_call_nfscl = NULL;
 1248                 /* and get rid of the mutexes */
 1249                 mtx_destroy(&nfs_clstate_mutex);
 1250                 mtx_destroy(&ncl_iod_mutex);
 1251                 loaded = 0;
 1252                 break;
 1253         default:
 1254                 error = EOPNOTSUPP;
 1255                 break;
 1256         }
 1257         return error;
 1258 }
 1259 static moduledata_t nfscl_mod = {
 1260         "nfscl",
 1261         nfscl_modevent,
 1262         NULL,
 1263 };
 1264 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_ANY);
 1265 
 1266 /* So that loader and kldload(2) can find us, wherever we are.. */
 1267 MODULE_VERSION(nfscl, 1);
 1268 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);
 1269 

Cache object: d3b0f1acee66f7313cca44a8dfdc7d2f


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