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_clstate.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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2009 Rick Macklem, University of Guelph
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  *
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: stable/12/sys/fs/nfsclient/nfs_clstate.c 371049 2021-11-19 22:45:14Z git2svn $");
   32 
   33 /*
   34  * These functions implement the client side state handling for NFSv4.
   35  * NFSv4 state handling:
   36  * - A lockowner is used to determine lock contention, so it
   37  *   corresponds directly to a Posix pid. (1 to 1 mapping)
   38  * - The correct granularity of an OpenOwner is not nearly so
   39  *   obvious. An OpenOwner does the following:
   40  *   - provides a serial sequencing of Open/Close/Lock-with-new-lockowner
   41  *   - is used to check for Open/Share contention (not applicable to
   42  *     this client, since all Opens are Deny_None)
   43  *   As such, I considered both extreme.
   44  *   1 OpenOwner per ClientID - Simple to manage, but fully serializes
   45  *   all Open, Close and Lock (with a new lockowner) Ops.
   46  *   1 OpenOwner for each Open - This one results in an OpenConfirm for
   47  *   every Open, for most servers.
   48  *   So, I chose to use the same mapping as I did for LockOwnwers.
   49  *   The main concern here is that you can end up with multiple Opens
   50  *   for the same File Handle, but on different OpenOwners (opens
   51  *   inherited from parents, grandparents...) and you do not know
   52  *   which of these the vnodeop close applies to. This is handled by
   53  *   delaying the Close Op(s) until all of the Opens have been closed.
   54  *   (It is not yet obvious if this is the correct granularity.)
   55  * - How the code handles serialization:
   56  *   - For the ClientId, it uses an exclusive lock while getting its
   57  *     SetClientId and during recovery. Otherwise, it uses a shared
   58  *     lock via a reference count.
   59  *   - For the rest of the data structures, it uses an SMP mutex
   60  *     (once the nfs client is SMP safe) and doesn't sleep while
   61  *     manipulating the linked lists.
   62  *   - The serialization of Open/Close/Lock/LockU falls out in the
   63  *     "wash", since OpenOwners and LockOwners are both mapped from
   64  *     Posix pid. In other words, there is only one Posix pid using
   65  *     any given owner, so that owner is serialized. (If you change
   66  *     the granularity of the OpenOwner, then code must be added to
   67  *     serialize Ops on the OpenOwner.)
   68  * - When to get rid of OpenOwners and LockOwners.
   69  *   - The function nfscl_cleanup_common() is executed after a process exits.
   70  *     It goes through the client list looking for all Open and Lock Owners.
   71  *     When one is found, it is marked "defunct" or in the case of
   72  *     an OpenOwner without any Opens, freed.
   73  *     The renew thread scans for defunct Owners and gets rid of them,
   74  *     if it can. The LockOwners will also be deleted when the
   75  *     associated Open is closed.
   76  *   - If the LockU or Close Op(s) fail during close in a way
   77  *     that could be recovered upon retry, they are relinked to the
   78  *     ClientId's defunct open list and retried by the renew thread
   79  *     until they succeed or an unmount/recovery occurs.
   80  *     (Since we are done with them, they do not need to be recovered.)
   81  */
   82 
   83 #include <fs/nfs/nfsport.h>
   84 
   85 /*
   86  * Global variables
   87  */
   88 extern struct nfsstatsv1 nfsstatsv1;
   89 extern struct nfsreqhead nfsd_reqq;
   90 extern u_int32_t newnfs_false, newnfs_true;
   91 extern int nfscl_debuglevel;
   92 extern int nfscl_enablecallb;
   93 extern int nfs_numnfscbd;
   94 NFSREQSPINLOCK;
   95 NFSCLSTATEMUTEX;
   96 int nfscl_inited = 0;
   97 struct nfsclhead nfsclhead;     /* Head of clientid list */
   98 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
   99 int nfscl_layouthighwater = NFSCLLAYOUTHIGHWATER;
  100 
  101 static int nfscl_delegcnt = 0;
  102 static int nfscl_layoutcnt = 0;
  103 static int nfscl_getopen(struct nfsclownerhead *, u_int8_t *, int, u_int8_t *,
  104     u_int8_t *, u_int32_t, struct nfscllockowner **, struct nfsclopen **);
  105 static void nfscl_clrelease(struct nfsclclient *);
  106 static void nfscl_unlinkopen(struct nfsclopen *);
  107 static void nfscl_cleanclient(struct nfsclclient *);
  108 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
  109     struct ucred *, NFSPROC_T *);
  110 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
  111     struct nfsmount *, struct ucred *, NFSPROC_T *);
  112 static void nfscl_recover(struct nfsclclient *, struct ucred *, NFSPROC_T *);
  113 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
  114     struct nfscllock *, int);
  115 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
  116     struct nfscllock **, int);
  117 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *,
  118     struct nfscldeleghead *);
  119 static u_int32_t nfscl_nextcbident(void);
  120 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
  121 static struct nfsclclient *nfscl_getclnt(u_int32_t);
  122 static struct nfsclclient *nfscl_getclntsess(uint8_t *);
  123 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
  124     int);
  125 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
  126     int, struct nfsclrecalllayout **, struct nfscllayout **);
  127 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
  128 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
  129     int);
  130 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
  131 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
  132     u_int8_t *, struct nfscllock **);
  133 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
  134 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
  135     struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
  136 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
  137     struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
  138     struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *);
  139 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
  140     struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
  141     struct nfscldeleg *, struct ucred *, NFSPROC_T *);
  142 static void nfscl_totalrecall(struct nfsclclient *);
  143 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
  144     struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
  145 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
  146     u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
  147     struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
  148 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
  149     int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
  150     struct ucred *, NFSPROC_T *);
  151 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
  152     struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
  153 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *,
  154     bool);
  155 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
  156 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
  157 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
  158     struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int,
  159     vnode_t *);
  160 static void nfscl_freeopenowner(struct nfsclowner *, int);
  161 static void nfscl_cleandeleg(struct nfscldeleg *);
  162 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
  163     struct nfsmount *, NFSPROC_T *);
  164 static void nfscl_emptylockowner(struct nfscllockowner *,
  165     struct nfscllockownerfhhead *);
  166 static void nfscl_mergeflayouts(struct nfsclflayouthead *,
  167     struct nfsclflayouthead *);
  168 static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t,
  169     uint64_t, uint32_t, uint32_t, uint32_t, char *, struct nfsclrecalllayout *);
  170 static int nfscl_seq(uint32_t, uint32_t);
  171 static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *,
  172     struct ucred *, NFSPROC_T *);
  173 static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *,
  174     struct ucred *, NFSPROC_T *);
  175 
  176 static short nfscberr_null[] = {
  177         0,
  178         0,
  179 };
  180 
  181 static short nfscberr_getattr[] = {
  182         NFSERR_RESOURCE,
  183         NFSERR_BADHANDLE,
  184         NFSERR_BADXDR,
  185         NFSERR_RESOURCE,
  186         NFSERR_SERVERFAULT,
  187         0,
  188 };
  189 
  190 static short nfscberr_recall[] = {
  191         NFSERR_RESOURCE,
  192         NFSERR_BADHANDLE,
  193         NFSERR_BADSTATEID,
  194         NFSERR_BADXDR,
  195         NFSERR_RESOURCE,
  196         NFSERR_SERVERFAULT,
  197         0,
  198 };
  199 
  200 static short *nfscl_cberrmap[] = {
  201         nfscberr_null,
  202         nfscberr_null,
  203         nfscberr_null,
  204         nfscberr_getattr,
  205         nfscberr_recall
  206 };
  207 
  208 #define NETFAMILY(clp) \
  209                 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
  210 
  211 /*
  212  * Called for an open operation.
  213  * If the nfhp argument is NULL, just get an openowner.
  214  */
  215 int
  216 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
  217     struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
  218     struct nfsclopen **opp, int *newonep, int *retp, int lockit, bool firstref)
  219 {
  220         struct nfsclclient *clp;
  221         struct nfsclowner *owp, *nowp;
  222         struct nfsclopen *op = NULL, *nop = NULL;
  223         struct nfscldeleg *dp;
  224         struct nfsclownerhead *ohp;
  225         u_int8_t own[NFSV4CL_LOCKNAMELEN];
  226         int ret;
  227 
  228         if (newonep != NULL)
  229                 *newonep = 0;
  230         if (opp != NULL)
  231                 *opp = NULL;
  232         if (owpp != NULL)
  233                 *owpp = NULL;
  234 
  235         /*
  236          * Might need one or both of these, so MALLOC them now, to
  237          * avoid a tsleep() in MALLOC later.
  238          */
  239         nowp = malloc(sizeof (struct nfsclowner),
  240             M_NFSCLOWNER, M_WAITOK);
  241         if (nfhp != NULL)
  242             nop = malloc(sizeof (struct nfsclopen) +
  243                 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
  244         ret = nfscl_getcl(vnode_mount(vp), cred, p, 1, firstref, &clp);
  245         if (ret != 0) {
  246                 free(nowp, M_NFSCLOWNER);
  247                 if (nop != NULL)
  248                         free(nop, M_NFSCLOPEN);
  249                 return (ret);
  250         }
  251 
  252         /*
  253          * Get the Open iff it already exists.
  254          * If none found, add the new one or return error, depending upon
  255          * "create".
  256          */
  257         NFSLOCKCLSTATE();
  258         dp = NULL;
  259         /* First check the delegation list */
  260         if (nfhp != NULL && usedeleg) {
  261                 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
  262                         if (dp->nfsdl_fhlen == fhlen &&
  263                             !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
  264                                 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
  265                                     (dp->nfsdl_flags & NFSCLDL_WRITE))
  266                                         break;
  267                                 dp = NULL;
  268                                 break;
  269                         }
  270                 }
  271         }
  272 
  273         if (dp != NULL) {
  274                 nfscl_filllockowner(p->td_proc, own, F_POSIX);
  275                 ohp = &dp->nfsdl_owner;
  276         } else {
  277                 /* For NFSv4.1 and this option, use a single open_owner. */
  278                 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
  279                         nfscl_filllockowner(NULL, own, F_POSIX);
  280                 else
  281                         nfscl_filllockowner(p->td_proc, own, F_POSIX);
  282                 ohp = &clp->nfsc_owner;
  283         }
  284         /* Now, search for an openowner */
  285         LIST_FOREACH(owp, ohp, nfsow_list) {
  286                 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
  287                         break;
  288         }
  289 
  290         /*
  291          * Create a new open, as required.
  292          */
  293         nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
  294             cred, newonep);
  295 
  296         /*
  297          * Now, check the mode on the open and return the appropriate
  298          * value.
  299          */
  300         if (retp != NULL) {
  301                 if (nfhp != NULL && dp != NULL && nop == NULL)
  302                         /* new local open on delegation */
  303                         *retp = NFSCLOPEN_SETCRED;
  304                 else
  305                         *retp = NFSCLOPEN_OK;
  306         }
  307         if (op != NULL && (amode & ~(op->nfso_mode))) {
  308                 op->nfso_mode |= amode;
  309                 if (retp != NULL && dp == NULL)
  310                         *retp = NFSCLOPEN_DOOPEN;
  311         }
  312 
  313         /*
  314          * Serialize modifications to the open owner for multiple threads
  315          * within the same process using a read/write sleep lock.
  316          * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
  317          * by acquiring a shared lock.  The close operations still use an
  318          * exclusive lock for this case.
  319          */
  320         if (lockit != 0) {
  321                 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp)))) {
  322                         /*
  323                          * Get a shared lock on the OpenOwner, but first
  324                          * wait for any pending exclusive lock, so that the
  325                          * exclusive locker gets priority.
  326                          */
  327                         nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
  328                             NFSCLSTATEMUTEXPTR, NULL);
  329                         nfsv4_getref(&owp->nfsow_rwlock, NULL,
  330                             NFSCLSTATEMUTEXPTR, NULL);
  331                 } else
  332                         nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
  333         }
  334         NFSUNLOCKCLSTATE();
  335         if (nowp != NULL)
  336                 free(nowp, M_NFSCLOWNER);
  337         if (nop != NULL)
  338                 free(nop, M_NFSCLOPEN);
  339         if (owpp != NULL)
  340                 *owpp = owp;
  341         if (opp != NULL)
  342                 *opp = op;
  343         return (0);
  344 }
  345 
  346 /*
  347  * Create a new open, as required.
  348  */
  349 static void
  350 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
  351     struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
  352     struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
  353     struct ucred *cred, int *newonep)
  354 {
  355         struct nfsclowner *owp = *owpp, *nowp;
  356         struct nfsclopen *op, *nop;
  357 
  358         if (nowpp != NULL)
  359                 nowp = *nowpp;
  360         else
  361                 nowp = NULL;
  362         if (nopp != NULL)
  363                 nop = *nopp;
  364         else
  365                 nop = NULL;
  366         if (owp == NULL && nowp != NULL) {
  367                 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
  368                 LIST_INIT(&nowp->nfsow_open);
  369                 nowp->nfsow_clp = clp;
  370                 nowp->nfsow_seqid = 0;
  371                 nowp->nfsow_defunct = 0;
  372                 nfscl_lockinit(&nowp->nfsow_rwlock);
  373                 if (dp != NULL) {
  374                         nfsstatsv1.cllocalopenowners++;
  375                         LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
  376                 } else {
  377                         nfsstatsv1.clopenowners++;
  378                         LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
  379                 }
  380                 owp = *owpp = nowp;
  381                 *nowpp = NULL;
  382                 if (newonep != NULL)
  383                         *newonep = 1;
  384         }
  385 
  386          /* If an fhp has been specified, create an Open as well. */
  387         if (fhp != NULL) {
  388                 /* and look for the correct open, based upon FH */
  389                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
  390                         if (op->nfso_fhlen == fhlen &&
  391                             !NFSBCMP(op->nfso_fh, fhp, fhlen))
  392                                 break;
  393                 }
  394                 if (op == NULL && nop != NULL) {
  395                         nop->nfso_own = owp;
  396                         nop->nfso_mode = 0;
  397                         nop->nfso_opencnt = 0;
  398                         nop->nfso_posixlock = 1;
  399                         nop->nfso_fhlen = fhlen;
  400                         NFSBCOPY(fhp, nop->nfso_fh, fhlen);
  401                         LIST_INIT(&nop->nfso_lock);
  402                         nop->nfso_stateid.seqid = 0;
  403                         nop->nfso_stateid.other[0] = 0;
  404                         nop->nfso_stateid.other[1] = 0;
  405                         nop->nfso_stateid.other[2] = 0;
  406                         KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
  407                         newnfs_copyincred(cred, &nop->nfso_cred);
  408                         if (dp != NULL) {
  409                                 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
  410                                 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
  411                                     nfsdl_list);
  412                                 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
  413                                 nfsstatsv1.cllocalopens++;
  414                         } else {
  415                                 nfsstatsv1.clopens++;
  416                         }
  417                         LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
  418                         *opp = nop;
  419                         *nopp = NULL;
  420                         if (newonep != NULL)
  421                                 *newonep = 1;
  422                 } else {
  423                         *opp = op;
  424                 }
  425         }
  426 }
  427 
  428 /*
  429  * Called to find/add a delegation to a client.
  430  */
  431 int
  432 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
  433     int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
  434 {
  435         struct nfscldeleg *dp = *dpp, *tdp;
  436 
  437         /*
  438          * First, if we have received a Read delegation for a file on a
  439          * read/write file system, just return it, because they aren't
  440          * useful, imho.
  441          */
  442         if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
  443             (dp->nfsdl_flags & NFSCLDL_READ)) {
  444                 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
  445                 free(dp, M_NFSCLDELEG);
  446                 *dpp = NULL;
  447                 return (0);
  448         }
  449 
  450         /* Look for the correct deleg, based upon FH */
  451         NFSLOCKCLSTATE();
  452         tdp = nfscl_finddeleg(clp, nfhp, fhlen);
  453         if (tdp == NULL) {
  454                 if (dp == NULL) {
  455                         NFSUNLOCKCLSTATE();
  456                         return (NFSERR_BADSTATEID);
  457                 }
  458                 *dpp = NULL;
  459                 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
  460                 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
  461                     nfsdl_hash);
  462                 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
  463                 nfsstatsv1.cldelegates++;
  464                 nfscl_delegcnt++;
  465         } else {
  466                 /*
  467                  * Delegation already exists, what do we do if a new one??
  468                  */
  469                 if (dp != NULL) {
  470                         printf("Deleg already exists!\n");
  471                         free(dp, M_NFSCLDELEG);
  472                         *dpp = NULL;
  473                 } else {
  474                         *dpp = tdp;
  475                 }
  476         }
  477         NFSUNLOCKCLSTATE();
  478         return (0);
  479 }
  480 
  481 /*
  482  * Find a delegation for this file handle. Return NULL upon failure.
  483  */
  484 static struct nfscldeleg *
  485 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
  486 {
  487         struct nfscldeleg *dp;
  488 
  489         LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
  490             if (dp->nfsdl_fhlen == fhlen &&
  491                 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
  492                 break;
  493         }
  494         return (dp);
  495 }
  496 
  497 /*
  498  * Get a stateid for an I/O operation. First, look for an open and iff
  499  * found, return either a lockowner stateid or the open stateid.
  500  * If no Open is found, just return error and the special stateid of all zeros.
  501  */
  502 int
  503 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
  504     int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
  505     void **lckpp)
  506 {
  507         struct nfsclclient *clp;
  508         struct nfsclowner *owp;
  509         struct nfsclopen *op = NULL, *top;
  510         struct nfscllockowner *lp;
  511         struct nfscldeleg *dp;
  512         struct nfsnode *np;
  513         struct nfsmount *nmp;
  514         u_int8_t own[NFSV4CL_LOCKNAMELEN];
  515         int error, done;
  516 
  517         *lckpp = NULL;
  518         /*
  519          * Initially, just set the special stateid of all zeros.
  520          * (Don't do this for a DS, since the special stateid can't be used.)
  521          */
  522         if (fords == 0) {
  523                 stateidp->seqid = 0;
  524                 stateidp->other[0] = 0;
  525                 stateidp->other[1] = 0;
  526                 stateidp->other[2] = 0;
  527         }
  528         if (vnode_vtype(vp) != VREG)
  529                 return (EISDIR);
  530         np = VTONFS(vp);
  531         nmp = VFSTONFS(vnode_mount(vp));
  532         NFSLOCKCLSTATE();
  533         clp = nfscl_findcl(nmp);
  534         if (clp == NULL) {
  535                 NFSUNLOCKCLSTATE();
  536                 return (EACCES);
  537         }
  538 
  539         /*
  540          * Wait for recovery to complete.
  541          */
  542         while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
  543                 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
  544                     PZERO, "nfsrecvr", NULL);
  545 
  546         /*
  547          * First, look for a delegation.
  548          */
  549         LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
  550                 if (dp->nfsdl_fhlen == fhlen &&
  551                     !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
  552                         if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
  553                             (dp->nfsdl_flags & NFSCLDL_WRITE)) {
  554                                 stateidp->seqid = dp->nfsdl_stateid.seqid;
  555                                 stateidp->other[0] = dp->nfsdl_stateid.other[0];
  556                                 stateidp->other[1] = dp->nfsdl_stateid.other[1];
  557                                 stateidp->other[2] = dp->nfsdl_stateid.other[2];
  558                                 if (!(np->n_flag & NDELEGRECALL)) {
  559                                         TAILQ_REMOVE(&clp->nfsc_deleg, dp,
  560                                             nfsdl_list);
  561                                         TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
  562                                             nfsdl_list);
  563                                         dp->nfsdl_timestamp = NFSD_MONOSEC +
  564                                             120;
  565                                         dp->nfsdl_rwlock.nfslock_usecnt++;
  566                                         *lckpp = (void *)&dp->nfsdl_rwlock;
  567                                 }
  568                                 NFSUNLOCKCLSTATE();
  569                                 return (0);
  570                         }
  571                         break;
  572                 }
  573         }
  574 
  575         if (p != NULL) {
  576                 /*
  577                  * If p != NULL, we want to search the parentage tree
  578                  * for a matching OpenOwner and use that.
  579                  */
  580                 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
  581                         nfscl_filllockowner(NULL, own, F_POSIX);
  582                 else
  583                         nfscl_filllockowner(p->td_proc, own, F_POSIX);
  584                 lp = NULL;
  585                 error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, own, own,
  586                     mode, &lp, &op);
  587                 if (error == 0 && lp != NULL && fords == 0) {
  588                         /* Don't return a lock stateid for a DS. */
  589                         stateidp->seqid =
  590                             lp->nfsl_stateid.seqid;
  591                         stateidp->other[0] =
  592                             lp->nfsl_stateid.other[0];
  593                         stateidp->other[1] =
  594                             lp->nfsl_stateid.other[1];
  595                         stateidp->other[2] =
  596                             lp->nfsl_stateid.other[2];
  597                         NFSUNLOCKCLSTATE();
  598                         return (0);
  599                 }
  600         }
  601         if (op == NULL) {
  602                 /* If not found, just look for any OpenOwner that will work. */
  603                 top = NULL;
  604                 done = 0;
  605                 owp = LIST_FIRST(&clp->nfsc_owner);
  606                 while (!done && owp != NULL) {
  607                         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
  608                                 if (op->nfso_fhlen == fhlen &&
  609                                     !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
  610                                         if (top == NULL && (op->nfso_mode &
  611                                             NFSV4OPEN_ACCESSWRITE) != 0 &&
  612                                             (mode & NFSV4OPEN_ACCESSREAD) != 0)
  613                                                 top = op;
  614                                         if ((mode & op->nfso_mode) == mode) {
  615                                                 done = 1;
  616                                                 break;
  617                                         }
  618                                 }
  619                         }
  620                         if (!done)
  621                                 owp = LIST_NEXT(owp, nfsow_list);
  622                 }
  623                 if (!done) {
  624                         NFSCL_DEBUG(2, "openmode top=%p\n", top);
  625                         if (top == NULL || NFSHASOPENMODE(nmp)) {
  626                                 NFSUNLOCKCLSTATE();
  627                                 return (ENOENT);
  628                         } else
  629                                 op = top;
  630                 }
  631                 /*
  632                  * For read aheads or write behinds, use the open cred.
  633                  * A read ahead or write behind is indicated by p == NULL.
  634                  */
  635                 if (p == NULL)
  636                         newnfs_copycred(&op->nfso_cred, cred);
  637         }
  638 
  639         /*
  640          * No lock stateid, so return the open stateid.
  641          */
  642         stateidp->seqid = op->nfso_stateid.seqid;
  643         stateidp->other[0] = op->nfso_stateid.other[0];
  644         stateidp->other[1] = op->nfso_stateid.other[1];
  645         stateidp->other[2] = op->nfso_stateid.other[2];
  646         NFSUNLOCKCLSTATE();
  647         return (0);
  648 }
  649 
  650 /*
  651  * Search for a matching file, mode and, optionally, lockowner.
  652  */
  653 static int
  654 nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
  655     u_int8_t *openown, u_int8_t *lockown, u_int32_t mode,
  656     struct nfscllockowner **lpp, struct nfsclopen **opp)
  657 {
  658         struct nfsclowner *owp;
  659         struct nfsclopen *op, *rop, *rop2;
  660         struct nfscllockowner *lp;
  661         int keep_looping;
  662 
  663         if (lpp != NULL)
  664                 *lpp = NULL;
  665         /*
  666          * rop will be set to the open to be returned. There are three
  667          * variants of this, all for an open of the correct file:
  668          * 1 - A match of lockown.
  669          * 2 - A match of the openown, when no lockown match exists.
  670          * 3 - A match for any open, if no openown or lockown match exists.
  671          * Looking for #2 over #3 probably isn't necessary, but since
  672          * RFC3530 is vague w.r.t. the relationship between openowners and
  673          * lockowners, I think this is the safer way to go.
  674          */
  675         rop = NULL;
  676         rop2 = NULL;
  677         keep_looping = 1;
  678         /* Search the client list */
  679         owp = LIST_FIRST(ohp);
  680         while (owp != NULL && keep_looping != 0) {
  681                 /* and look for the correct open */
  682                 op = LIST_FIRST(&owp->nfsow_open);
  683                 while (op != NULL && keep_looping != 0) {
  684                         if (op->nfso_fhlen == fhlen &&
  685                             !NFSBCMP(op->nfso_fh, nfhp, fhlen)
  686                             && (op->nfso_mode & mode) == mode) {
  687                                 if (lpp != NULL) {
  688                                         /* Now look for a matching lockowner. */
  689                                         LIST_FOREACH(lp, &op->nfso_lock,
  690                                             nfsl_list) {
  691                                                 if (!NFSBCMP(lp->nfsl_owner,
  692                                                     lockown,
  693                                                     NFSV4CL_LOCKNAMELEN)) {
  694                                                         *lpp = lp;
  695                                                         rop = op;
  696                                                         keep_looping = 0;
  697                                                         break;
  698                                                 }
  699                                         }
  700                                 }
  701                                 if (rop == NULL && !NFSBCMP(owp->nfsow_owner,
  702                                     openown, NFSV4CL_LOCKNAMELEN)) {
  703                                         rop = op;
  704                                         if (lpp == NULL)
  705                                                 keep_looping = 0;
  706                                 }
  707                                 if (rop2 == NULL)
  708                                         rop2 = op;
  709                         }
  710                         op = LIST_NEXT(op, nfso_list);
  711                 }
  712                 owp = LIST_NEXT(owp, nfsow_list);
  713         }
  714         if (rop == NULL)
  715                 rop = rop2;
  716         if (rop == NULL)
  717                 return (EBADF);
  718         *opp = rop;
  719         return (0);
  720 }
  721 
  722 /*
  723  * Release use of an open owner. Called when open operations are done
  724  * with the open owner.
  725  */
  726 void
  727 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
  728     __unused int error, __unused int candelete, int unlocked)
  729 {
  730 
  731         if (owp == NULL)
  732                 return;
  733         NFSLOCKCLSTATE();
  734         if (unlocked == 0) {
  735                 if (NFSHASONEOPENOWN(nmp))
  736                         nfsv4_relref(&owp->nfsow_rwlock);
  737                 else
  738                         nfscl_lockunlock(&owp->nfsow_rwlock);
  739         }
  740         nfscl_clrelease(owp->nfsow_clp);
  741         NFSUNLOCKCLSTATE();
  742 }
  743 
  744 /*
  745  * Release use of an open structure under an open owner.
  746  */
  747 void
  748 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
  749     int candelete)
  750 {
  751         struct nfsclclient *clp;
  752         struct nfsclowner *owp;
  753 
  754         if (op == NULL)
  755                 return;
  756         NFSLOCKCLSTATE();
  757         owp = op->nfso_own;
  758         if (NFSHASONEOPENOWN(nmp))
  759                 nfsv4_relref(&owp->nfsow_rwlock);
  760         else
  761                 nfscl_lockunlock(&owp->nfsow_rwlock);
  762         clp = owp->nfsow_clp;
  763         if (error && candelete && op->nfso_opencnt == 0)
  764                 nfscl_freeopen(op, 0, true);
  765         nfscl_clrelease(clp);
  766         NFSUNLOCKCLSTATE();
  767 }
  768 
  769 /*
  770  * Called to get a clientid structure. It will optionally lock the
  771  * client data structures to do the SetClientId/SetClientId_confirm,
  772  * but will release that lock and return the clientid with a reference
  773  * count on it.
  774  * If the "cred" argument is NULL, a new clientid should not be created.
  775  * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
  776  * be done.
  777  * The start_renewthread argument tells nfscl_getcl() to start a renew
  778  * thread if this creates a new clp.
  779  * It always clpp with a reference count on it, unless returning an error.
  780  */
  781 int
  782 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
  783     int start_renewthread, bool firstref, struct nfsclclient **clpp)
  784 {
  785         struct nfsclclient *clp;
  786         struct nfsclclient *newclp = NULL;
  787         struct nfsmount *nmp;
  788         char uuid[HOSTUUIDLEN];
  789         int igotlock = 0, error, trystalecnt, clidinusedelay, i;
  790         u_int16_t idlen = 0;
  791 
  792         nmp = VFSTONFS(mp);
  793         if (cred != NULL) {
  794                 getcredhostuuid(cred, uuid, sizeof uuid);
  795                 idlen = strlen(uuid);
  796                 if (idlen > 0)
  797                         idlen += sizeof (u_int64_t);
  798                 else
  799                         idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
  800                 newclp = malloc(
  801                     sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
  802                     M_WAITOK | M_ZERO);
  803         }
  804         NFSLOCKCLSTATE();
  805         /*
  806          * If a forced dismount is already in progress, don't
  807          * allocate a new clientid and get out now. For the case where
  808          * clp != NULL, this is a harmless optimization.
  809          */
  810         if (NFSCL_FORCEDISM(mp)) {
  811                 NFSUNLOCKCLSTATE();
  812                 if (newclp != NULL)
  813                         free(newclp, M_NFSCLCLIENT);
  814                 return (EBADF);
  815         }
  816         clp = nmp->nm_clp;
  817         if (clp == NULL) {
  818                 if (newclp == NULL) {
  819                         NFSUNLOCKCLSTATE();
  820                         return (EACCES);
  821                 }
  822                 clp = newclp;
  823                 clp->nfsc_idlen = idlen;
  824                 LIST_INIT(&clp->nfsc_owner);
  825                 TAILQ_INIT(&clp->nfsc_deleg);
  826                 TAILQ_INIT(&clp->nfsc_layout);
  827                 LIST_INIT(&clp->nfsc_devinfo);
  828                 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
  829                         LIST_INIT(&clp->nfsc_deleghash[i]);
  830                 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
  831                         LIST_INIT(&clp->nfsc_layouthash[i]);
  832                 clp->nfsc_flags = NFSCLFLAGS_INITED;
  833                 clp->nfsc_clientidrev = 1;
  834                 clp->nfsc_cbident = nfscl_nextcbident();
  835                 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
  836                     clp->nfsc_idlen);
  837                 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
  838                 nmp->nm_clp = clp;
  839                 clp->nfsc_nmp = nmp;
  840                 NFSUNLOCKCLSTATE();
  841                 if (start_renewthread != 0)
  842                         nfscl_start_renewthread(clp);
  843         } else {
  844                 NFSUNLOCKCLSTATE();
  845                 if (newclp != NULL)
  846                         free(newclp, M_NFSCLCLIENT);
  847         }
  848         NFSLOCKCLSTATE();
  849         while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
  850             !NFSCL_FORCEDISM(mp))
  851                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
  852                     NFSCLSTATEMUTEXPTR, mp);
  853         if (igotlock == 0) {
  854                 /*
  855                  * Call nfsv4_lock() with "iwantlock == 0" on the firstref so
  856                  * that it will wait for a pending exclusive lock request.
  857                  * This gives the exclusive lock request priority over this
  858                  * shared lock request.
  859                  * An exclusive lock on nfsc_lock is used mainly for server
  860                  * crash recoveries and delegation recalls.
  861                  */
  862                 if (firstref)
  863                         nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR,
  864                             mp);
  865                 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
  866         }
  867         if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
  868                 /*
  869                  * Both nfsv4_lock() and nfsv4_getref() know to check
  870                  * for NFSCL_FORCEDISM() and return without sleeping to
  871                  * wait for the exclusive lock to be released, since it
  872                  * might be held by nfscl_umount() and we need to get out
  873                  * now for that case and not wait until nfscl_umount()
  874                  * releases it.
  875                  */
  876                 NFSUNLOCKCLSTATE();
  877                 return (EBADF);
  878         }
  879         NFSUNLOCKCLSTATE();
  880 
  881         /*
  882          * If it needs a clientid, do the setclientid now.
  883          */
  884         if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
  885                 if (!igotlock)
  886                         panic("nfscl_clget");
  887                 if (p == NULL || cred == NULL) {
  888                         NFSLOCKCLSTATE();
  889                         nfsv4_unlock(&clp->nfsc_lock, 0);
  890                         NFSUNLOCKCLSTATE();
  891                         return (EACCES);
  892                 }
  893                 /*
  894                  * If RFC3530 Sec. 14.2.33 is taken literally,
  895                  * NFSERR_CLIDINUSE will be returned persistently for the
  896                  * case where a new mount of the same file system is using
  897                  * a different principal. In practice, NFSERR_CLIDINUSE is
  898                  * only returned when there is outstanding unexpired state
  899                  * on the clientid. As such, try for twice the lease
  900                  * interval, if we know what that is. Otherwise, make a
  901                  * wild ass guess.
  902                  * The case of returning NFSERR_STALECLIENTID is far less
  903                  * likely, but might occur if there is a significant delay
  904                  * between doing the SetClientID and SetClientIDConfirm Ops,
  905                  * such that the server throws away the clientid before
  906                  * receiving the SetClientIDConfirm.
  907                  */
  908                 if (clp->nfsc_renew > 0)
  909                         clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
  910                 else
  911                         clidinusedelay = 120;
  912                 trystalecnt = 3;
  913                 do {
  914                         error = nfsrpc_setclient(nmp, clp, 0, cred, p);
  915                         if (error == NFSERR_STALECLIENTID ||
  916                             error == NFSERR_STALEDONTRECOVER ||
  917                             error == NFSERR_BADSESSION ||
  918                             error == NFSERR_CLIDINUSE) {
  919                                 (void) nfs_catnap(PZERO, error, "nfs_setcl");
  920                         }
  921                 } while (((error == NFSERR_STALECLIENTID ||
  922                      error == NFSERR_BADSESSION ||
  923                      error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
  924                     (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
  925                 if (error) {
  926                         NFSLOCKCLSTATE();
  927                         nfsv4_unlock(&clp->nfsc_lock, 0);
  928                         NFSUNLOCKCLSTATE();
  929                         return (error);
  930                 }
  931                 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
  932         }
  933         if (igotlock) {
  934                 NFSLOCKCLSTATE();
  935                 nfsv4_unlock(&clp->nfsc_lock, 1);
  936                 NFSUNLOCKCLSTATE();
  937         }
  938 
  939         *clpp = clp;
  940         return (0);
  941 }
  942 
  943 /*
  944  * Get a reference to a clientid and return it, if valid.
  945  */
  946 struct nfsclclient *
  947 nfscl_findcl(struct nfsmount *nmp)
  948 {
  949         struct nfsclclient *clp;
  950 
  951         clp = nmp->nm_clp;
  952         if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
  953                 return (NULL);
  954         return (clp);
  955 }
  956 
  957 /*
  958  * Release the clientid structure. It may be locked or reference counted.
  959  */
  960 static void
  961 nfscl_clrelease(struct nfsclclient *clp)
  962 {
  963 
  964         if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
  965                 nfsv4_unlock(&clp->nfsc_lock, 0);
  966         else
  967                 nfsv4_relref(&clp->nfsc_lock);
  968 }
  969 
  970 /*
  971  * External call for nfscl_clrelease.
  972  */
  973 void
  974 nfscl_clientrelease(struct nfsclclient *clp)
  975 {
  976 
  977         NFSLOCKCLSTATE();
  978         if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
  979                 nfsv4_unlock(&clp->nfsc_lock, 0);
  980         else
  981                 nfsv4_relref(&clp->nfsc_lock);
  982         NFSUNLOCKCLSTATE();
  983 }
  984 
  985 /*
  986  * Called when wanting to lock a byte region.
  987  */
  988 int
  989 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
  990     short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
  991     int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
  992     struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
  993 {
  994         struct nfscllockowner *lp;
  995         struct nfsclopen *op;
  996         struct nfsclclient *clp;
  997         struct nfscllockowner *nlp;
  998         struct nfscllock *nlop, *otherlop;
  999         struct nfscldeleg *dp = NULL, *ldp = NULL;
 1000         struct nfscllockownerhead *lhp = NULL;
 1001         struct nfsnode *np;
 1002         u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
 1003         u_int8_t *openownp;
 1004         int error = 0, ret, donelocally = 0;
 1005         u_int32_t mode;
 1006 
 1007         /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
 1008         mode = 0;
 1009         np = VTONFS(vp);
 1010         *lpp = NULL;
 1011         lp = NULL;
 1012         *newonep = 0;
 1013         *donelocallyp = 0;
 1014 
 1015         /*
 1016          * Might need these, so MALLOC them now, to
 1017          * avoid a tsleep() in MALLOC later.
 1018          */
 1019         nlp = malloc(
 1020             sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
 1021         otherlop = malloc(
 1022             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
 1023         nlop = malloc(
 1024             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
 1025         nlop->nfslo_type = type;
 1026         nlop->nfslo_first = off;
 1027         if (len == NFS64BITSSET) {
 1028                 nlop->nfslo_end = NFS64BITSSET;
 1029         } else {
 1030                 nlop->nfslo_end = off + len;
 1031                 if (nlop->nfslo_end <= nlop->nfslo_first)
 1032                         error = NFSERR_INVAL;
 1033         }
 1034 
 1035         if (!error) {
 1036                 if (recovery)
 1037                         clp = rclp;
 1038                 else
 1039                         error = nfscl_getcl(vnode_mount(vp), cred, p, 1, true,
 1040                             &clp);
 1041         }
 1042         if (error) {
 1043                 free(nlp, M_NFSCLLOCKOWNER);
 1044                 free(otherlop, M_NFSCLLOCK);
 1045                 free(nlop, M_NFSCLLOCK);
 1046                 return (error);
 1047         }
 1048 
 1049         op = NULL;
 1050         if (recovery) {
 1051                 ownp = rownp;
 1052                 openownp = ropenownp;
 1053         } else {
 1054                 nfscl_filllockowner(id, own, flags);
 1055                 ownp = own;
 1056                 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
 1057                         nfscl_filllockowner(NULL, openown, F_POSIX);
 1058                 else
 1059                         nfscl_filllockowner(p->td_proc, openown, F_POSIX);
 1060                 openownp = openown;
 1061         }
 1062         if (!recovery) {
 1063                 NFSLOCKCLSTATE();
 1064                 /*
 1065                  * First, search for a delegation. If one exists for this file,
 1066                  * the lock can be done locally against it, so long as there
 1067                  * isn't a local lock conflict.
 1068                  */
 1069                 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
 1070                     np->n_fhp->nfh_len);
 1071                 /* Just sanity check for correct type of delegation */
 1072                 if (dp != NULL && ((dp->nfsdl_flags &
 1073                     (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
 1074                      (type == F_WRLCK &&
 1075                       (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
 1076                         dp = NULL;
 1077         }
 1078         if (dp != NULL) {
 1079                 /* Now, find an open and maybe a lockowner. */
 1080                 ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
 1081                     np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
 1082                 if (ret)
 1083                         ret = nfscl_getopen(&clp->nfsc_owner,
 1084                             np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
 1085                             ownp, mode, NULL, &op);
 1086                 if (!ret) {
 1087                         lhp = &dp->nfsdl_lock;
 1088                         TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
 1089                         TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
 1090                         dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
 1091                         donelocally = 1;
 1092                 } else {
 1093                         dp = NULL;
 1094                 }
 1095         }
 1096         if (!donelocally) {
 1097                 /*
 1098                  * Get the related Open and maybe lockowner.
 1099                  */
 1100                 error = nfscl_getopen(&clp->nfsc_owner,
 1101                     np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
 1102                     ownp, mode, &lp, &op);
 1103                 if (!error)
 1104                         lhp = &op->nfso_lock;
 1105         }
 1106         if (!error && !recovery)
 1107                 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
 1108                     np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
 1109         if (error) {
 1110                 if (!recovery) {
 1111                         nfscl_clrelease(clp);
 1112                         NFSUNLOCKCLSTATE();
 1113                 }
 1114                 free(nlp, M_NFSCLLOCKOWNER);
 1115                 free(otherlop, M_NFSCLLOCK);
 1116                 free(nlop, M_NFSCLLOCK);
 1117                 return (error);
 1118         }
 1119 
 1120         /*
 1121          * Ok, see if a lockowner exists and create one, as required.
 1122          */
 1123         if (lp == NULL)
 1124                 LIST_FOREACH(lp, lhp, nfsl_list) {
 1125                         if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
 1126                                 break;
 1127                 }
 1128         if (lp == NULL) {
 1129                 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
 1130                 if (recovery)
 1131                         NFSBCOPY(ropenownp, nlp->nfsl_openowner,
 1132                             NFSV4CL_LOCKNAMELEN);
 1133                 else
 1134                         NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
 1135                             NFSV4CL_LOCKNAMELEN);
 1136                 nlp->nfsl_seqid = 0;
 1137                 nlp->nfsl_lockflags = flags;
 1138                 nlp->nfsl_inprog = NULL;
 1139                 nfscl_lockinit(&nlp->nfsl_rwlock);
 1140                 LIST_INIT(&nlp->nfsl_lock);
 1141                 if (donelocally) {
 1142                         nlp->nfsl_open = NULL;
 1143                         nfsstatsv1.cllocallockowners++;
 1144                 } else {
 1145                         nlp->nfsl_open = op;
 1146                         nfsstatsv1.cllockowners++;
 1147                 }
 1148                 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
 1149                 lp = nlp;
 1150                 nlp = NULL;
 1151                 *newonep = 1;
 1152         }
 1153 
 1154         /*
 1155          * Now, update the byte ranges for locks.
 1156          */
 1157         ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
 1158         if (!ret)
 1159                 donelocally = 1;
 1160         if (donelocally) {
 1161                 *donelocallyp = 1;
 1162                 if (!recovery)
 1163                         nfscl_clrelease(clp);
 1164         } else {
 1165                 /*
 1166                  * Serial modifications on the lock owner for multiple threads
 1167                  * for the same process using a read/write lock.
 1168                  */
 1169                 if (!recovery)
 1170                         nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
 1171         }
 1172         if (!recovery)
 1173                 NFSUNLOCKCLSTATE();
 1174 
 1175         if (nlp)
 1176                 free(nlp, M_NFSCLLOCKOWNER);
 1177         if (nlop)
 1178                 free(nlop, M_NFSCLLOCK);
 1179         if (otherlop)
 1180                 free(otherlop, M_NFSCLLOCK);
 1181 
 1182         *lpp = lp;
 1183         return (0);
 1184 }
 1185 
 1186 /*
 1187  * Called to unlock a byte range, for LockU.
 1188  */
 1189 int
 1190 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
 1191     __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
 1192     struct nfsclclient *clp, void *id, int flags,
 1193     struct nfscllockowner **lpp, int *dorpcp)
 1194 {
 1195         struct nfscllockowner *lp;
 1196         struct nfsclowner *owp;
 1197         struct nfsclopen *op;
 1198         struct nfscllock *nlop, *other_lop = NULL;
 1199         struct nfscldeleg *dp;
 1200         struct nfsnode *np;
 1201         u_int8_t own[NFSV4CL_LOCKNAMELEN];
 1202         int ret = 0, fnd;
 1203 
 1204         np = VTONFS(vp);
 1205         *lpp = NULL;
 1206         *dorpcp = 0;
 1207 
 1208         /*
 1209          * Might need these, so MALLOC them now, to
 1210          * avoid a tsleep() in MALLOC later.
 1211          */
 1212         nlop = malloc(
 1213             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
 1214         nlop->nfslo_type = F_UNLCK;
 1215         nlop->nfslo_first = off;
 1216         if (len == NFS64BITSSET) {
 1217                 nlop->nfslo_end = NFS64BITSSET;
 1218         } else {
 1219                 nlop->nfslo_end = off + len;
 1220                 if (nlop->nfslo_end <= nlop->nfslo_first) {
 1221                         free(nlop, M_NFSCLLOCK);
 1222                         return (NFSERR_INVAL);
 1223                 }
 1224         }
 1225         if (callcnt == 0) {
 1226                 other_lop = malloc(
 1227                     sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
 1228                 *other_lop = *nlop;
 1229         }
 1230         nfscl_filllockowner(id, own, flags);
 1231         dp = NULL;
 1232         NFSLOCKCLSTATE();
 1233         if (callcnt == 0)
 1234                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
 1235                     np->n_fhp->nfh_len);
 1236 
 1237         /*
 1238          * First, unlock any local regions on a delegation.
 1239          */
 1240         if (dp != NULL) {
 1241                 /* Look for this lockowner. */
 1242                 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
 1243                         if (!NFSBCMP(lp->nfsl_owner, own,
 1244                             NFSV4CL_LOCKNAMELEN))
 1245                                 break;
 1246                 }
 1247                 if (lp != NULL)
 1248                         /* Use other_lop, so nlop is still available */
 1249                         (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
 1250         }
 1251 
 1252         /*
 1253          * Now, find a matching open/lockowner that hasn't already been done,
 1254          * as marked by nfsl_inprog.
 1255          */
 1256         lp = NULL;
 1257         fnd = 0;
 1258         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 1259             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 1260                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
 1261                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
 1262                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
 1263                         if (lp->nfsl_inprog == NULL &&
 1264                             !NFSBCMP(lp->nfsl_owner, own,
 1265                              NFSV4CL_LOCKNAMELEN)) {
 1266                                 fnd = 1;
 1267                                 break;
 1268                         }
 1269                     }
 1270                     if (fnd)
 1271                         break;
 1272                 }
 1273             }
 1274             if (fnd)
 1275                 break;
 1276         }
 1277 
 1278         if (lp != NULL) {
 1279                 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
 1280                 if (ret)
 1281                         *dorpcp = 1;
 1282                 /*
 1283                  * Serial modifications on the lock owner for multiple
 1284                  * threads for the same process using a read/write lock.
 1285                  */
 1286                 lp->nfsl_inprog = p;
 1287                 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
 1288                 *lpp = lp;
 1289         }
 1290         NFSUNLOCKCLSTATE();
 1291         if (nlop)
 1292                 free(nlop, M_NFSCLLOCK);
 1293         if (other_lop)
 1294                 free(other_lop, M_NFSCLLOCK);
 1295         return (0);
 1296 }
 1297 
 1298 /*
 1299  * Release all lockowners marked in progess for this process and file.
 1300  */
 1301 void
 1302 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
 1303     void *id, int flags)
 1304 {
 1305         struct nfsclowner *owp;
 1306         struct nfsclopen *op;
 1307         struct nfscllockowner *lp;
 1308         struct nfsnode *np;
 1309         u_int8_t own[NFSV4CL_LOCKNAMELEN];
 1310 
 1311         np = VTONFS(vp);
 1312         nfscl_filllockowner(id, own, flags);
 1313         NFSLOCKCLSTATE();
 1314         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 1315             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 1316                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
 1317                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
 1318                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
 1319                         if (lp->nfsl_inprog == p &&
 1320                             !NFSBCMP(lp->nfsl_owner, own,
 1321                             NFSV4CL_LOCKNAMELEN)) {
 1322                             lp->nfsl_inprog = NULL;
 1323                             nfscl_lockunlock(&lp->nfsl_rwlock);
 1324                         }
 1325                     }
 1326                 }
 1327             }
 1328         }
 1329         nfscl_clrelease(clp);
 1330         NFSUNLOCKCLSTATE();
 1331 }
 1332 
 1333 /*
 1334  * Called to find out if any bytes within the byte range specified are
 1335  * write locked by the calling process. Used to determine if flushing
 1336  * is required before a LockU.
 1337  * If in doubt, return 1, so the flush will occur.
 1338  */
 1339 int
 1340 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
 1341     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
 1342 {
 1343         struct nfsclowner *owp;
 1344         struct nfscllockowner *lp;
 1345         struct nfsclopen *op;
 1346         struct nfsclclient *clp;
 1347         struct nfscllock *lop;
 1348         struct nfscldeleg *dp;
 1349         struct nfsnode *np;
 1350         u_int64_t off, end;
 1351         u_int8_t own[NFSV4CL_LOCKNAMELEN];
 1352         int error = 0;
 1353 
 1354         np = VTONFS(vp);
 1355         switch (fl->l_whence) {
 1356         case SEEK_SET:
 1357         case SEEK_CUR:
 1358                 /*
 1359                  * Caller is responsible for adding any necessary offset
 1360                  * when SEEK_CUR is used.
 1361                  */
 1362                 off = fl->l_start;
 1363                 break;
 1364         case SEEK_END:
 1365                 off = np->n_size + fl->l_start;
 1366                 break;
 1367         default:
 1368                 return (1);
 1369         }
 1370         if (fl->l_len != 0) {
 1371                 end = off + fl->l_len;
 1372                 if (end < off)
 1373                         return (1);
 1374         } else {
 1375                 end = NFS64BITSSET;
 1376         }
 1377 
 1378         error = nfscl_getcl(vnode_mount(vp), cred, p, 1, true, &clp);
 1379         if (error)
 1380                 return (1);
 1381         nfscl_filllockowner(id, own, flags);
 1382         NFSLOCKCLSTATE();
 1383 
 1384         /*
 1385          * First check the delegation locks.
 1386          */
 1387         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
 1388         if (dp != NULL) {
 1389                 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
 1390                         if (!NFSBCMP(lp->nfsl_owner, own,
 1391                             NFSV4CL_LOCKNAMELEN))
 1392                                 break;
 1393                 }
 1394                 if (lp != NULL) {
 1395                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
 1396                                 if (lop->nfslo_first >= end)
 1397                                         break;
 1398                                 if (lop->nfslo_end <= off)
 1399                                         continue;
 1400                                 if (lop->nfslo_type == F_WRLCK) {
 1401                                         nfscl_clrelease(clp);
 1402                                         NFSUNLOCKCLSTATE();
 1403                                         return (1);
 1404                                 }
 1405                         }
 1406                 }
 1407         }
 1408 
 1409         /*
 1410          * Now, check state against the server.
 1411          */
 1412         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 1413             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 1414                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
 1415                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
 1416                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
 1417                         if (!NFSBCMP(lp->nfsl_owner, own,
 1418                             NFSV4CL_LOCKNAMELEN))
 1419                             break;
 1420                     }
 1421                     if (lp != NULL) {
 1422                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
 1423                             if (lop->nfslo_first >= end)
 1424                                 break;
 1425                             if (lop->nfslo_end <= off)
 1426                                 continue;
 1427                             if (lop->nfslo_type == F_WRLCK) {
 1428                                 nfscl_clrelease(clp);
 1429                                 NFSUNLOCKCLSTATE();
 1430                                 return (1);
 1431                             }
 1432                         }
 1433                     }
 1434                 }
 1435             }
 1436         }
 1437         nfscl_clrelease(clp);
 1438         NFSUNLOCKCLSTATE();
 1439         return (0);
 1440 }
 1441 
 1442 /*
 1443  * Release a byte range lock owner structure.
 1444  */
 1445 void
 1446 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
 1447 {
 1448         struct nfsclclient *clp;
 1449 
 1450         if (lp == NULL)
 1451                 return;
 1452         NFSLOCKCLSTATE();
 1453         clp = lp->nfsl_open->nfso_own->nfsow_clp;
 1454         if (error != 0 && candelete &&
 1455             (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
 1456                 nfscl_freelockowner(lp, 0);
 1457         else
 1458                 nfscl_lockunlock(&lp->nfsl_rwlock);
 1459         nfscl_clrelease(clp);
 1460         NFSUNLOCKCLSTATE();
 1461 }
 1462 
 1463 /*
 1464  * Unlink the open structure.
 1465  */
 1466 static void
 1467 nfscl_unlinkopen(struct nfsclopen *op)
 1468 {
 1469 
 1470         LIST_REMOVE(op, nfso_list);
 1471 }
 1472 
 1473 /*
 1474  * Free up an open structure and any associated byte range lock structures.
 1475  */
 1476 void
 1477 nfscl_freeopen(struct nfsclopen *op, int local, bool unlink)
 1478 {
 1479 
 1480         if (unlink)
 1481                 nfscl_unlinkopen(op);
 1482         nfscl_freealllocks(&op->nfso_lock, local);
 1483         free(op, M_NFSCLOPEN);
 1484         if (local)
 1485                 nfsstatsv1.cllocalopens--;
 1486         else
 1487                 nfsstatsv1.clopens--;
 1488 }
 1489 
 1490 /*
 1491  * Free up all lock owners and associated locks.
 1492  */
 1493 static void
 1494 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
 1495 {
 1496         struct nfscllockowner *lp, *nlp;
 1497 
 1498         LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
 1499                 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
 1500                         panic("nfscllckw");
 1501                 nfscl_freelockowner(lp, local);
 1502         }
 1503 }
 1504 
 1505 /*
 1506  * Called for an Open when NFSERR_EXPIRED is received from the server.
 1507  * If there are no byte range locks nor a Share Deny lost, try to do a
 1508  * fresh Open. Otherwise, free the open.
 1509  */
 1510 static int
 1511 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
 1512     struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
 1513 {
 1514         struct nfscllockowner *lp;
 1515         struct nfscldeleg *dp;
 1516         int mustdelete = 0, error;
 1517 
 1518         /*
 1519          * Look for any byte range lock(s).
 1520          */
 1521         LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
 1522                 if (!LIST_EMPTY(&lp->nfsl_lock)) {
 1523                         mustdelete = 1;
 1524                         break;
 1525                 }
 1526         }
 1527 
 1528         /*
 1529          * If no byte range lock(s) nor a Share deny, try to re-open.
 1530          */
 1531         if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
 1532                 newnfs_copycred(&op->nfso_cred, cred);
 1533                 dp = NULL;
 1534                 error = nfsrpc_reopen(nmp, op->nfso_fh,
 1535                     op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
 1536                 if (error) {
 1537                         mustdelete = 1;
 1538                         if (dp != NULL) {
 1539                                 free(dp, M_NFSCLDELEG);
 1540                                 dp = NULL;
 1541                         }
 1542                 }
 1543                 if (dp != NULL)
 1544                         nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
 1545                             op->nfso_fhlen, cred, p, &dp);
 1546         }
 1547 
 1548         /*
 1549          * If a byte range lock or Share deny or couldn't re-open, free it.
 1550          */
 1551         if (mustdelete)
 1552                 nfscl_freeopen(op, 0, true);
 1553         return (mustdelete);
 1554 }
 1555 
 1556 /*
 1557  * Free up an open owner structure.
 1558  */
 1559 static void
 1560 nfscl_freeopenowner(struct nfsclowner *owp, int local)
 1561 {
 1562 
 1563         LIST_REMOVE(owp, nfsow_list);
 1564         free(owp, M_NFSCLOWNER);
 1565         if (local)
 1566                 nfsstatsv1.cllocalopenowners--;
 1567         else
 1568                 nfsstatsv1.clopenowners--;
 1569 }
 1570 
 1571 /*
 1572  * Free up a byte range lock owner structure.
 1573  */
 1574 void
 1575 nfscl_freelockowner(struct nfscllockowner *lp, int local)
 1576 {
 1577         struct nfscllock *lop, *nlop;
 1578 
 1579         LIST_REMOVE(lp, nfsl_list);
 1580         LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
 1581                 nfscl_freelock(lop, local);
 1582         }
 1583         free(lp, M_NFSCLLOCKOWNER);
 1584         if (local)
 1585                 nfsstatsv1.cllocallockowners--;
 1586         else
 1587                 nfsstatsv1.cllockowners--;
 1588 }
 1589 
 1590 /*
 1591  * Free up a byte range lock structure.
 1592  */
 1593 void
 1594 nfscl_freelock(struct nfscllock *lop, int local)
 1595 {
 1596 
 1597         LIST_REMOVE(lop, nfslo_list);
 1598         free(lop, M_NFSCLLOCK);
 1599         if (local)
 1600                 nfsstatsv1.cllocallocks--;
 1601         else
 1602                 nfsstatsv1.cllocks--;
 1603 }
 1604 
 1605 /*
 1606  * Clean out the state related to a delegation.
 1607  */
 1608 static void
 1609 nfscl_cleandeleg(struct nfscldeleg *dp)
 1610 {
 1611         struct nfsclowner *owp, *nowp;
 1612         struct nfsclopen *op;
 1613 
 1614         LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
 1615                 op = LIST_FIRST(&owp->nfsow_open);
 1616                 if (op != NULL) {
 1617                         if (LIST_NEXT(op, nfso_list) != NULL)
 1618                                 panic("nfscleandel");
 1619                         nfscl_freeopen(op, 1, true);
 1620                 }
 1621                 nfscl_freeopenowner(owp, 1);
 1622         }
 1623         nfscl_freealllocks(&dp->nfsdl_lock, 1);
 1624 }
 1625 
 1626 /*
 1627  * Free a delegation.
 1628  */
 1629 static void
 1630 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp, bool freeit)
 1631 {
 1632 
 1633         TAILQ_REMOVE(hdp, dp, nfsdl_list);
 1634         LIST_REMOVE(dp, nfsdl_hash);
 1635         if (freeit)
 1636                 free(dp, M_NFSCLDELEG);
 1637         nfsstatsv1.cldelegates--;
 1638         nfscl_delegcnt--;
 1639 }
 1640 
 1641 /*
 1642  * Free up all state related to this client structure.
 1643  */
 1644 static void
 1645 nfscl_cleanclient(struct nfsclclient *clp)
 1646 {
 1647         struct nfsclowner *owp, *nowp;
 1648         struct nfsclopen *op, *nop;
 1649         struct nfscllayout *lyp, *nlyp;
 1650         struct nfscldevinfo *dip, *ndip;
 1651 
 1652         TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
 1653                 nfscl_freelayout(lyp);
 1654 
 1655         LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
 1656                 nfscl_freedevinfo(dip);
 1657 
 1658         /* Now, all the OpenOwners, etc. */
 1659         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
 1660                 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
 1661                         nfscl_freeopen(op, 0, true);
 1662                 }
 1663                 nfscl_freeopenowner(owp, 0);
 1664         }
 1665 }
 1666 
 1667 /*
 1668  * Called when an NFSERR_EXPIRED is received from the server.
 1669  */
 1670 static void
 1671 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
 1672     struct ucred *cred, NFSPROC_T *p)
 1673 {
 1674         struct nfsclowner *owp, *nowp, *towp;
 1675         struct nfsclopen *op, *nop, *top;
 1676         struct nfscldeleg *dp, *ndp;
 1677         int ret, printed = 0;
 1678 
 1679         /*
 1680          * First, merge locally issued Opens into the list for the server.
 1681          */
 1682         dp = TAILQ_FIRST(&clp->nfsc_deleg);
 1683         while (dp != NULL) {
 1684             ndp = TAILQ_NEXT(dp, nfsdl_list);
 1685             owp = LIST_FIRST(&dp->nfsdl_owner);
 1686             while (owp != NULL) {
 1687                 nowp = LIST_NEXT(owp, nfsow_list);
 1688                 op = LIST_FIRST(&owp->nfsow_open);
 1689                 if (op != NULL) {
 1690                     if (LIST_NEXT(op, nfso_list) != NULL)
 1691                         panic("nfsclexp");
 1692                     LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
 1693                         if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
 1694                             NFSV4CL_LOCKNAMELEN))
 1695                             break;
 1696                     }
 1697                     if (towp != NULL) {
 1698                         /* Merge opens in */
 1699                         LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
 1700                             if (top->nfso_fhlen == op->nfso_fhlen &&
 1701                                 !NFSBCMP(top->nfso_fh, op->nfso_fh,
 1702                                  op->nfso_fhlen)) {
 1703                                 top->nfso_mode |= op->nfso_mode;
 1704                                 top->nfso_opencnt += op->nfso_opencnt;
 1705                                 break;
 1706                             }
 1707                         }
 1708                         if (top == NULL) {
 1709                             /* Just add the open to the owner list */
 1710                             LIST_REMOVE(op, nfso_list);
 1711                             op->nfso_own = towp;
 1712                             LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
 1713                             nfsstatsv1.cllocalopens--;
 1714                             nfsstatsv1.clopens++;
 1715                         }
 1716                     } else {
 1717                         /* Just add the openowner to the client list */
 1718                         LIST_REMOVE(owp, nfsow_list);
 1719                         owp->nfsow_clp = clp;
 1720                         LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
 1721                         nfsstatsv1.cllocalopenowners--;
 1722                         nfsstatsv1.clopenowners++;
 1723                         nfsstatsv1.cllocalopens--;
 1724                         nfsstatsv1.clopens++;
 1725                     }
 1726                 }
 1727                 owp = nowp;
 1728             }
 1729             if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
 1730                 printed = 1;
 1731                 printf("nfsv4 expired locks lost\n");
 1732             }
 1733             nfscl_cleandeleg(dp);
 1734             nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
 1735             dp = ndp;
 1736         }
 1737         if (!TAILQ_EMPTY(&clp->nfsc_deleg))
 1738             panic("nfsclexp");
 1739 
 1740         /*
 1741          * Now, try and reopen against the server.
 1742          */
 1743         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
 1744                 owp->nfsow_seqid = 0;
 1745                 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
 1746                         ret = nfscl_expireopen(clp, op, nmp, cred, p);
 1747                         if (ret && !printed) {
 1748                                 printed = 1;
 1749                                 printf("nfsv4 expired locks lost\n");
 1750                         }
 1751                 }
 1752                 if (LIST_EMPTY(&owp->nfsow_open))
 1753                         nfscl_freeopenowner(owp, 0);
 1754         }
 1755 }
 1756 
 1757 /*
 1758  * This function must be called after the process represented by "own" has
 1759  * exited. Must be called with CLSTATE lock held.
 1760  */
 1761 static void
 1762 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
 1763 {
 1764         struct nfsclowner *owp, *nowp;
 1765         struct nfscllockowner *lp, *nlp;
 1766         struct nfscldeleg *dp;
 1767 
 1768         /* First, get rid of local locks on delegations. */
 1769         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
 1770                 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
 1771                     if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
 1772                         if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
 1773                             panic("nfscllckw");
 1774                         nfscl_freelockowner(lp, 1);
 1775                     }
 1776                 }
 1777         }
 1778         owp = LIST_FIRST(&clp->nfsc_owner);
 1779         while (owp != NULL) {
 1780                 nowp = LIST_NEXT(owp, nfsow_list);
 1781                 if (!NFSBCMP(owp->nfsow_owner, own,
 1782                     NFSV4CL_LOCKNAMELEN)) {
 1783                         /*
 1784                          * If there are children that haven't closed the
 1785                          * file descriptors yet, the opens will still be
 1786                          * here. For that case, let the renew thread clear
 1787                          * out the OpenOwner later.
 1788                          */
 1789                         if (LIST_EMPTY(&owp->nfsow_open))
 1790                                 nfscl_freeopenowner(owp, 0);
 1791                         else
 1792                                 owp->nfsow_defunct = 1;
 1793                 }
 1794                 owp = nowp;
 1795         }
 1796 }
 1797 
 1798 /*
 1799  * Find open/lock owners for processes that have exited.
 1800  */
 1801 static void
 1802 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
 1803 {
 1804         struct nfsclowner *owp, *nowp;
 1805         struct nfsclopen *op;
 1806         struct nfscllockowner *lp, *nlp;
 1807         struct nfscldeleg *dp;
 1808 
 1809         NFSPROCLISTLOCK();
 1810         NFSLOCKCLSTATE();
 1811         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
 1812                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 1813                         LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
 1814                                 if (LIST_EMPTY(&lp->nfsl_lock))
 1815                                         nfscl_emptylockowner(lp, lhp);
 1816                         }
 1817                 }
 1818                 if (nfscl_procdoesntexist(owp->nfsow_owner))
 1819                         nfscl_cleanup_common(clp, owp->nfsow_owner);
 1820         }
 1821 
 1822         /*
 1823          * For the single open_owner case, these lock owners need to be
 1824          * checked to see if they still exist separately.
 1825          * This is because nfscl_procdoesntexist() never returns true for
 1826          * the single open_owner so that the above doesn't ever call
 1827          * nfscl_cleanup_common().
 1828          */
 1829         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
 1830                 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
 1831                         if (nfscl_procdoesntexist(lp->nfsl_owner))
 1832                                 nfscl_cleanup_common(clp, lp->nfsl_owner);
 1833                 }
 1834         }
 1835         NFSUNLOCKCLSTATE();
 1836         NFSPROCLISTUNLOCK();
 1837 }
 1838 
 1839 /*
 1840  * Take the empty lock owner and move it to the local lhp list if the
 1841  * associated process no longer exists.
 1842  */
 1843 static void
 1844 nfscl_emptylockowner(struct nfscllockowner *lp,
 1845     struct nfscllockownerfhhead *lhp)
 1846 {
 1847         struct nfscllockownerfh *lfhp, *mylfhp;
 1848         struct nfscllockowner *nlp;
 1849         int fnd_it;
 1850 
 1851         /* If not a Posix lock owner, just return. */
 1852         if ((lp->nfsl_lockflags & F_POSIX) == 0)
 1853                 return;
 1854 
 1855         fnd_it = 0;
 1856         mylfhp = NULL;
 1857         /*
 1858          * First, search to see if this lock owner is already in the list.
 1859          * If it is, then the associated process no longer exists.
 1860          */
 1861         SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
 1862                 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
 1863                     !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
 1864                     lfhp->nfslfh_len))
 1865                         mylfhp = lfhp;
 1866                 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
 1867                         if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
 1868                             NFSV4CL_LOCKNAMELEN))
 1869                                 fnd_it = 1;
 1870         }
 1871         /* If not found, check if process still exists. */
 1872         if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
 1873                 return;
 1874 
 1875         /* Move the lock owner over to the local list. */
 1876         if (mylfhp == NULL) {
 1877                 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
 1878                     M_NOWAIT);
 1879                 if (mylfhp == NULL)
 1880                         return;
 1881                 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
 1882                 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
 1883                     mylfhp->nfslfh_len);
 1884                 LIST_INIT(&mylfhp->nfslfh_lock);
 1885                 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
 1886         }
 1887         LIST_REMOVE(lp, nfsl_list);
 1888         LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
 1889 }
 1890 
 1891 static int      fake_global;    /* Used to force visibility of MNTK_UNMOUNTF */
 1892 /*
 1893  * Called from nfs umount to free up the clientid.
 1894  */
 1895 void
 1896 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p, struct nfscldeleghead *dhp)
 1897 {
 1898         struct nfsclclient *clp;
 1899         struct ucred *cred;
 1900         int igotlock;
 1901 
 1902         /*
 1903          * For the case that matters, this is the thread that set
 1904          * MNTK_UNMOUNTF, so it will see it set. The code that follows is
 1905          * done to ensure that any thread executing nfscl_getcl() after
 1906          * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
 1907          * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
 1908          * explanation, courtesy of Alan Cox.
 1909          * What follows is a snippet from Alan Cox's email at:
 1910          * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
 1911          * 
 1912          * 1. Set MNTK_UNMOUNTF
 1913          * 2. Acquire a standard FreeBSD mutex "m".
 1914          * 3. Update some data structures.
 1915          * 4. Release mutex "m".
 1916          * 
 1917          * Then, other threads that acquire "m" after step 4 has occurred will
 1918          * see MNTK_UNMOUNTF as set.  But, other threads that beat thread X to
 1919          * step 2 may or may not see MNTK_UNMOUNTF as set.
 1920          */
 1921         NFSLOCKCLSTATE();
 1922         if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
 1923                 fake_global++;
 1924                 NFSUNLOCKCLSTATE();
 1925                 NFSLOCKCLSTATE();
 1926         }
 1927 
 1928         clp = nmp->nm_clp;
 1929         if (clp != NULL) {
 1930                 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
 1931                         panic("nfscl umount");
 1932         
 1933                 /*
 1934                  * First, handshake with the nfscl renew thread, to terminate
 1935                  * it.
 1936                  */
 1937                 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
 1938                 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
 1939                         (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
 1940                             "nfsclumnt", hz);
 1941         
 1942                 /*
 1943                  * Now, get the exclusive lock on the client state, so
 1944                  * that no uses of the state are still in progress.
 1945                  */
 1946                 do {
 1947                         igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
 1948                             NFSCLSTATEMUTEXPTR, NULL);
 1949                 } while (!igotlock);
 1950                 NFSUNLOCKCLSTATE();
 1951         
 1952                 /*
 1953                  * Free up all the state. It will expire on the server, but
 1954                  * maybe we should do a SetClientId/SetClientIdConfirm so
 1955                  * the server throws it away?
 1956                  */
 1957                 LIST_REMOVE(clp, nfsc_list);
 1958                 nfscl_delegreturnall(clp, p, dhp);
 1959                 cred = newnfs_getcred();
 1960                 if (NFSHASNFSV4N(nmp)) {
 1961                         (void)nfsrpc_destroysession(nmp, clp, cred, p);
 1962                         (void)nfsrpc_destroyclient(nmp, clp, cred, p);
 1963                 } else
 1964                         (void)nfsrpc_setclient(nmp, clp, 0, cred, p);
 1965                 nfscl_cleanclient(clp);
 1966                 nmp->nm_clp = NULL;
 1967                 NFSFREECRED(cred);
 1968                 free(clp, M_NFSCLCLIENT);
 1969         } else
 1970                 NFSUNLOCKCLSTATE();
 1971 }
 1972 
 1973 /*
 1974  * This function is called when a server replies with NFSERR_STALECLIENTID
 1975  * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
 1976  * doing Opens and Locks with reclaim. If these fail, it deletes the
 1977  * corresponding state.
 1978  */
 1979 static void
 1980 nfscl_recover(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
 1981 {
 1982         struct nfsclowner *owp, *nowp;
 1983         struct nfsclopen *op, *nop;
 1984         struct nfscllockowner *lp, *nlp;
 1985         struct nfscllock *lop, *nlop;
 1986         struct nfscldeleg *dp, *ndp, *tdp;
 1987         struct nfsmount *nmp;
 1988         struct ucred *tcred;
 1989         struct nfsclopenhead extra_open;
 1990         struct nfscldeleghead extra_deleg;
 1991         struct nfsreq *rep;
 1992         u_int64_t len;
 1993         u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
 1994         int i, igotlock = 0, error, trycnt, firstlock;
 1995         struct nfscllayout *lyp, *nlyp;
 1996         bool recovered_one;
 1997 
 1998         /*
 1999          * First, lock the client structure, so everyone else will
 2000          * block when trying to use state.
 2001          */
 2002         NFSLOCKCLSTATE();
 2003         clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
 2004         do {
 2005                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
 2006                     NFSCLSTATEMUTEXPTR, NULL);
 2007         } while (!igotlock);
 2008         NFSUNLOCKCLSTATE();
 2009 
 2010         nmp = clp->nfsc_nmp;
 2011         if (nmp == NULL)
 2012                 panic("nfscl recover");
 2013 
 2014         /*
 2015          * For now, just get rid of all layouts. There may be a need
 2016          * to do LayoutCommit Ops with reclaim == true later.
 2017          */
 2018         TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
 2019                 nfscl_freelayout(lyp);
 2020         TAILQ_INIT(&clp->nfsc_layout);
 2021         for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
 2022                 LIST_INIT(&clp->nfsc_layouthash[i]);
 2023 
 2024         trycnt = 5;
 2025         do {
 2026                 error = nfsrpc_setclient(nmp, clp, 1, cred, p);
 2027         } while ((error == NFSERR_STALECLIENTID ||
 2028              error == NFSERR_BADSESSION ||
 2029              error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
 2030         if (error) {
 2031                 NFSLOCKCLSTATE();
 2032                 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
 2033                     NFSCLFLAGS_RECVRINPROG);
 2034                 wakeup(&clp->nfsc_flags);
 2035                 nfsv4_unlock(&clp->nfsc_lock, 0);
 2036                 NFSUNLOCKCLSTATE();
 2037                 return;
 2038         }
 2039         clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
 2040         clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
 2041 
 2042         /*
 2043          * Mark requests already queued on the server, so that they don't
 2044          * initiate another recovery cycle. Any requests already in the
 2045          * queue that handle state information will have the old stale
 2046          * clientid/stateid and will get a NFSERR_STALESTATEID,
 2047          * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
 2048          * This will be translated to NFSERR_STALEDONTRECOVER when
 2049          * R_DONTRECOVER is set.
 2050          */
 2051         NFSLOCKREQ();
 2052         TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
 2053                 if (rep->r_nmp == nmp)
 2054                         rep->r_flags |= R_DONTRECOVER;
 2055         }
 2056         NFSUNLOCKREQ();
 2057 
 2058         /*
 2059          * Now, mark all delegations "need reclaim".
 2060          */
 2061         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
 2062                 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
 2063 
 2064         TAILQ_INIT(&extra_deleg);
 2065         LIST_INIT(&extra_open);
 2066         /*
 2067          * Now traverse the state lists, doing Open and Lock Reclaims.
 2068          */
 2069         tcred = newnfs_getcred();
 2070         recovered_one = false;
 2071         owp = LIST_FIRST(&clp->nfsc_owner);
 2072         while (owp != NULL) {
 2073             nowp = LIST_NEXT(owp, nfsow_list);
 2074             owp->nfsow_seqid = 0;
 2075             op = LIST_FIRST(&owp->nfsow_open);
 2076             while (op != NULL) {
 2077                 nop = LIST_NEXT(op, nfso_list);
 2078                 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
 2079                     /* Search for a delegation to reclaim with the open */
 2080                     TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
 2081                         if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
 2082                             continue;
 2083                         if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
 2084                             mode = NFSV4OPEN_ACCESSWRITE;
 2085                             delegtype = NFSV4OPEN_DELEGATEWRITE;
 2086                         } else {
 2087                             mode = NFSV4OPEN_ACCESSREAD;
 2088                             delegtype = NFSV4OPEN_DELEGATEREAD;
 2089                         }
 2090                         if ((op->nfso_mode & mode) == mode &&
 2091                             op->nfso_fhlen == dp->nfsdl_fhlen &&
 2092                             !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
 2093                             break;
 2094                     }
 2095                     ndp = dp;
 2096                     if (dp == NULL)
 2097                         delegtype = NFSV4OPEN_DELEGATENONE;
 2098                     newnfs_copycred(&op->nfso_cred, tcred);
 2099                     error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
 2100                         op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
 2101                         op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
 2102                         tcred, p);
 2103                     if (!error) {
 2104                         recovered_one = true;
 2105                         /* Handle any replied delegation */
 2106                         if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
 2107                             || NFSMNT_RDONLY(nmp->nm_mountp))) {
 2108                             if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
 2109                                 mode = NFSV4OPEN_ACCESSWRITE;
 2110                             else
 2111                                 mode = NFSV4OPEN_ACCESSREAD;
 2112                             TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
 2113                                 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
 2114                                     continue;
 2115                                 if ((op->nfso_mode & mode) == mode &&
 2116                                     op->nfso_fhlen == dp->nfsdl_fhlen &&
 2117                                     !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
 2118                                     op->nfso_fhlen)) {
 2119                                     dp->nfsdl_stateid = ndp->nfsdl_stateid;
 2120                                     dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
 2121                                     dp->nfsdl_ace = ndp->nfsdl_ace;
 2122                                     dp->nfsdl_change = ndp->nfsdl_change;
 2123                                     dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
 2124                                     if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
 2125                                         dp->nfsdl_flags |= NFSCLDL_RECALL;
 2126                                     free(ndp, M_NFSCLDELEG);
 2127                                     ndp = NULL;
 2128                                     break;
 2129                                 }
 2130                             }
 2131                         }
 2132                         if (ndp != NULL)
 2133                             TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
 2134 
 2135                         /* and reclaim all byte range locks */
 2136                         lp = LIST_FIRST(&op->nfso_lock);
 2137                         while (lp != NULL) {
 2138                             nlp = LIST_NEXT(lp, nfsl_list);
 2139                             lp->nfsl_seqid = 0;
 2140                             firstlock = 1;
 2141                             lop = LIST_FIRST(&lp->nfsl_lock);
 2142                             while (lop != NULL) {
 2143                                 nlop = LIST_NEXT(lop, nfslo_list);
 2144                                 if (lop->nfslo_end == NFS64BITSSET)
 2145                                     len = NFS64BITSSET;
 2146                                 else
 2147                                     len = lop->nfslo_end - lop->nfslo_first;
 2148                                 error = nfscl_trylock(nmp, NULL,
 2149                                     op->nfso_fh, op->nfso_fhlen, lp,
 2150                                     firstlock, 1, lop->nfslo_first, len,
 2151                                     lop->nfslo_type, tcred, p);
 2152                                 if (error != 0)
 2153                                     nfscl_freelock(lop, 0);
 2154                                 else
 2155                                     firstlock = 0;
 2156                                 lop = nlop;
 2157                             }
 2158                             /* If no locks, but a lockowner, just delete it. */
 2159                             if (LIST_EMPTY(&lp->nfsl_lock))
 2160                                 nfscl_freelockowner(lp, 0);
 2161                             lp = nlp;
 2162                         }
 2163                     } else if (error == NFSERR_NOGRACE && !recovered_one &&
 2164                         NFSHASNFSV4N(nmp)) {
 2165                         /*
 2166                          * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
 2167                          * actually end up here, since the client will do
 2168                          * a recovery for NFSERR_BADSESSION, but will get
 2169                          * an NFSERR_NOGRACE reply for the first "reclaim"
 2170                          * attempt.
 2171                          * So, call nfscl_expireclient() to recover the
 2172                          * opens as best we can and then do a reclaim
 2173                          * complete and return.
 2174                          */
 2175                         nfsrpc_reclaimcomplete(nmp, cred, p);
 2176                         nfscl_expireclient(clp, nmp, tcred, p);
 2177                         goto out;
 2178                     }
 2179                 }
 2180                 if (error != 0 && error != NFSERR_BADSESSION)
 2181                     nfscl_freeopen(op, 0, true);
 2182                 op = nop;
 2183             }
 2184             owp = nowp;
 2185         }
 2186 
 2187         /*
 2188          * Now, try and get any delegations not yet reclaimed by cobbling
 2189          * to-gether an appropriate open.
 2190          */
 2191         nowp = NULL;
 2192         dp = TAILQ_FIRST(&clp->nfsc_deleg);
 2193         while (dp != NULL) {
 2194             ndp = TAILQ_NEXT(dp, nfsdl_list);
 2195             if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
 2196                 if (nowp == NULL) {
 2197                     nowp = malloc(
 2198                         sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
 2199                     /*
 2200                      * Name must be as long an largest possible
 2201                      * NFSV4CL_LOCKNAMELEN. 12 for now.
 2202                      */
 2203                     NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
 2204                         NFSV4CL_LOCKNAMELEN);
 2205                     LIST_INIT(&nowp->nfsow_open);
 2206                     nowp->nfsow_clp = clp;
 2207                     nowp->nfsow_seqid = 0;
 2208                     nowp->nfsow_defunct = 0;
 2209                     nfscl_lockinit(&nowp->nfsow_rwlock);
 2210                 }
 2211                 nop = NULL;
 2212                 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
 2213                     nop = malloc(sizeof (struct nfsclopen) +
 2214                         dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
 2215                     nop->nfso_own = nowp;
 2216                     if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
 2217                         nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
 2218                         delegtype = NFSV4OPEN_DELEGATEWRITE;
 2219                     } else {
 2220                         nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
 2221                         delegtype = NFSV4OPEN_DELEGATEREAD;
 2222                     }
 2223                     nop->nfso_opencnt = 0;
 2224                     nop->nfso_posixlock = 1;
 2225                     nop->nfso_fhlen = dp->nfsdl_fhlen;
 2226                     NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
 2227                     LIST_INIT(&nop->nfso_lock);
 2228                     nop->nfso_stateid.seqid = 0;
 2229                     nop->nfso_stateid.other[0] = 0;
 2230                     nop->nfso_stateid.other[1] = 0;
 2231                     nop->nfso_stateid.other[2] = 0;
 2232                     newnfs_copycred(&dp->nfsdl_cred, tcred);
 2233                     newnfs_copyincred(tcred, &nop->nfso_cred);
 2234                     tdp = NULL;
 2235                     error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
 2236                         nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
 2237                         nop->nfso_mode, nop, NULL, 0, &tdp, 1,
 2238                         delegtype, tcred, p);
 2239                     if (tdp != NULL) {
 2240                         if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
 2241                             mode = NFSV4OPEN_ACCESSWRITE;
 2242                         else
 2243                             mode = NFSV4OPEN_ACCESSREAD;
 2244                         if ((nop->nfso_mode & mode) == mode &&
 2245                             nop->nfso_fhlen == tdp->nfsdl_fhlen &&
 2246                             !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
 2247                             nop->nfso_fhlen)) {
 2248                             dp->nfsdl_stateid = tdp->nfsdl_stateid;
 2249                             dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
 2250                             dp->nfsdl_ace = tdp->nfsdl_ace;
 2251                             dp->nfsdl_change = tdp->nfsdl_change;
 2252                             dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
 2253                             if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
 2254                                 dp->nfsdl_flags |= NFSCLDL_RECALL;
 2255                             free(tdp, M_NFSCLDELEG);
 2256                         } else {
 2257                             TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
 2258                         }
 2259                     }
 2260                 }
 2261                 if (error) {
 2262                     if (nop != NULL)
 2263                         free(nop, M_NFSCLOPEN);
 2264                     if (error == NFSERR_NOGRACE && !recovered_one &&
 2265                         NFSHASNFSV4N(nmp)) {
 2266                         /*
 2267                          * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
 2268                          * actually end up here, since the client will do
 2269                          * a recovery for NFSERR_BADSESSION, but will get
 2270                          * an NFSERR_NOGRACE reply for the first "reclaim"
 2271                          * attempt.
 2272                          * So, call nfscl_expireclient() to recover the
 2273                          * opens as best we can and then do a reclaim
 2274                          * complete and return.
 2275                          */
 2276                         nfsrpc_reclaimcomplete(nmp, cred, p);
 2277                         nfscl_expireclient(clp, nmp, tcred, p);
 2278                         free(nowp, M_NFSCLOWNER);
 2279                         goto out;
 2280                     }
 2281                     /*
 2282                      * Couldn't reclaim it, so throw the state
 2283                      * away. Ouch!!
 2284                      */
 2285                     nfscl_cleandeleg(dp);
 2286                     nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
 2287                 } else {
 2288                     recovered_one = true;
 2289                     LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
 2290                 }
 2291             }
 2292             dp = ndp;
 2293         }
 2294 
 2295         /*
 2296          * Now, get rid of extra Opens and Delegations.
 2297          */
 2298         LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
 2299                 do {
 2300                         newnfs_copycred(&op->nfso_cred, tcred);
 2301                         error = nfscl_tryclose(op, tcred, nmp, p, true);
 2302                         if (error == NFSERR_GRACE)
 2303                                 (void) nfs_catnap(PZERO, error, "nfsexcls");
 2304                 } while (error == NFSERR_GRACE);
 2305                 LIST_REMOVE(op, nfso_list);
 2306                 free(op, M_NFSCLOPEN);
 2307         }
 2308         if (nowp != NULL)
 2309                 free(nowp, M_NFSCLOWNER);
 2310 
 2311         TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
 2312                 do {
 2313                         newnfs_copycred(&dp->nfsdl_cred, tcred);
 2314                         error = nfscl_trydelegreturn(dp, tcred, nmp, p);
 2315                         if (error == NFSERR_GRACE)
 2316                                 (void) nfs_catnap(PZERO, error, "nfsexdlg");
 2317                 } while (error == NFSERR_GRACE);
 2318                 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
 2319                 free(dp, M_NFSCLDELEG);
 2320         }
 2321 
 2322         /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
 2323         if (NFSHASNFSV4N(nmp))
 2324                 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
 2325 
 2326 out:
 2327         NFSLOCKCLSTATE();
 2328         clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
 2329         wakeup(&clp->nfsc_flags);
 2330         nfsv4_unlock(&clp->nfsc_lock, 0);
 2331         NFSUNLOCKCLSTATE();
 2332         NFSFREECRED(tcred);
 2333 }
 2334 
 2335 /*
 2336  * This function is called when a server replies with NFSERR_EXPIRED.
 2337  * It deletes all state for the client and does a fresh SetClientId/confirm.
 2338  * XXX Someday it should post a signal to the process(es) that hold the
 2339  * state, so they know that lock state has been lost.
 2340  */
 2341 int
 2342 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
 2343 {
 2344         struct nfsmount *nmp;
 2345         struct ucred *cred;
 2346         int igotlock = 0, error, trycnt;
 2347 
 2348         /*
 2349          * If the clientid has gone away or a new SetClientid has already
 2350          * been done, just return ok.
 2351          */
 2352         if (clp == NULL || clidrev != clp->nfsc_clientidrev)
 2353                 return (0);
 2354 
 2355         /*
 2356          * First, lock the client structure, so everyone else will
 2357          * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
 2358          * that only one thread does the work.
 2359          */
 2360         NFSLOCKCLSTATE();
 2361         clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
 2362         do {
 2363                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
 2364                     NFSCLSTATEMUTEXPTR, NULL);
 2365         } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
 2366         if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
 2367                 if (igotlock)
 2368                         nfsv4_unlock(&clp->nfsc_lock, 0);
 2369                 NFSUNLOCKCLSTATE();
 2370                 return (0);
 2371         }
 2372         clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
 2373         NFSUNLOCKCLSTATE();
 2374 
 2375         nmp = clp->nfsc_nmp;
 2376         if (nmp == NULL)
 2377                 panic("nfscl expired");
 2378         cred = newnfs_getcred();
 2379         trycnt = 5;
 2380         do {
 2381                 error = nfsrpc_setclient(nmp, clp, 0, cred, p);
 2382         } while ((error == NFSERR_STALECLIENTID ||
 2383              error == NFSERR_BADSESSION ||
 2384              error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
 2385         if (error) {
 2386                 NFSLOCKCLSTATE();
 2387                 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
 2388         } else {
 2389                 /*
 2390                  * Expire the state for the client.
 2391                  */
 2392                 nfscl_expireclient(clp, nmp, cred, p);
 2393                 NFSLOCKCLSTATE();
 2394                 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
 2395                 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
 2396         }
 2397         clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
 2398         wakeup(&clp->nfsc_flags);
 2399         nfsv4_unlock(&clp->nfsc_lock, 0);
 2400         NFSUNLOCKCLSTATE();
 2401         NFSFREECRED(cred);
 2402         return (error);
 2403 }
 2404 
 2405 /*
 2406  * This function inserts a lock in the list after insert_lop.
 2407  */
 2408 static void
 2409 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
 2410     struct nfscllock *insert_lop, int local)
 2411 {
 2412 
 2413         if ((struct nfscllockowner *)insert_lop == lp)
 2414                 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
 2415         else
 2416                 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
 2417         if (local)
 2418                 nfsstatsv1.cllocallocks++;
 2419         else
 2420                 nfsstatsv1.cllocks++;
 2421 }
 2422 
 2423 /*
 2424  * This function updates the locking for a lock owner and given file. It
 2425  * maintains a list of lock ranges ordered on increasing file offset that
 2426  * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
 2427  * It always adds new_lop to the list and sometimes uses the one pointed
 2428  * at by other_lopp.
 2429  * Returns 1 if the locks were modified, 0 otherwise.
 2430  */
 2431 static int
 2432 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
 2433     struct nfscllock **other_lopp, int local)
 2434 {
 2435         struct nfscllock *new_lop = *new_lopp;
 2436         struct nfscllock *lop, *tlop, *ilop;
 2437         struct nfscllock *other_lop;
 2438         int unlock = 0, modified = 0;
 2439         u_int64_t tmp;
 2440 
 2441         /*
 2442          * Work down the list until the lock is merged.
 2443          */
 2444         if (new_lop->nfslo_type == F_UNLCK)
 2445                 unlock = 1;
 2446         ilop = (struct nfscllock *)lp;
 2447         lop = LIST_FIRST(&lp->nfsl_lock);
 2448         while (lop != NULL) {
 2449             /*
 2450              * Only check locks for this file that aren't before the start of
 2451              * new lock's range.
 2452              */
 2453             if (lop->nfslo_end >= new_lop->nfslo_first) {
 2454                 if (new_lop->nfslo_end < lop->nfslo_first) {
 2455                     /*
 2456                      * If the new lock ends before the start of the
 2457                      * current lock's range, no merge, just insert
 2458                      * the new lock.
 2459                      */
 2460                     break;
 2461                 }
 2462                 if (new_lop->nfslo_type == lop->nfslo_type ||
 2463                     (new_lop->nfslo_first <= lop->nfslo_first &&
 2464                      new_lop->nfslo_end >= lop->nfslo_end)) {
 2465                     /*
 2466                      * This lock can be absorbed by the new lock/unlock.
 2467                      * This happens when it covers the entire range
 2468                      * of the old lock or is contiguous
 2469                      * with the old lock and is of the same type or an
 2470                      * unlock.
 2471                      */
 2472                     if (new_lop->nfslo_type != lop->nfslo_type ||
 2473                         new_lop->nfslo_first != lop->nfslo_first ||
 2474                         new_lop->nfslo_end != lop->nfslo_end)
 2475                         modified = 1;
 2476                     if (lop->nfslo_first < new_lop->nfslo_first)
 2477                         new_lop->nfslo_first = lop->nfslo_first;
 2478                     if (lop->nfslo_end > new_lop->nfslo_end)
 2479                         new_lop->nfslo_end = lop->nfslo_end;
 2480                     tlop = lop;
 2481                     lop = LIST_NEXT(lop, nfslo_list);
 2482                     nfscl_freelock(tlop, local);
 2483                     continue;
 2484                 }
 2485 
 2486                 /*
 2487                  * All these cases are for contiguous locks that are not the
 2488                  * same type, so they can't be merged.
 2489                  */
 2490                 if (new_lop->nfslo_first <= lop->nfslo_first) {
 2491                     /*
 2492                      * This case is where the new lock overlaps with the
 2493                      * first part of the old lock. Move the start of the
 2494                      * old lock to just past the end of the new lock. The
 2495                      * new lock will be inserted in front of the old, since
 2496                      * ilop hasn't been updated. (We are done now.)
 2497                      */
 2498                     if (lop->nfslo_first != new_lop->nfslo_end) {
 2499                         lop->nfslo_first = new_lop->nfslo_end;
 2500                         modified = 1;
 2501                     }
 2502                     break;
 2503                 }
 2504                 if (new_lop->nfslo_end >= lop->nfslo_end) {
 2505                     /*
 2506                      * This case is where the new lock overlaps with the
 2507                      * end of the old lock's range. Move the old lock's
 2508                      * end to just before the new lock's first and insert
 2509                      * the new lock after the old lock.
 2510                      * Might not be done yet, since the new lock could
 2511                      * overlap further locks with higher ranges.
 2512                      */
 2513                     if (lop->nfslo_end != new_lop->nfslo_first) {
 2514                         lop->nfslo_end = new_lop->nfslo_first;
 2515                         modified = 1;
 2516                     }
 2517                     ilop = lop;
 2518                     lop = LIST_NEXT(lop, nfslo_list);
 2519                     continue;
 2520                 }
 2521                 /*
 2522                  * The final case is where the new lock's range is in the
 2523                  * middle of the current lock's and splits the current lock
 2524                  * up. Use *other_lopp to handle the second part of the
 2525                  * split old lock range. (We are done now.)
 2526                  * For unlock, we use new_lop as other_lop and tmp, since
 2527                  * other_lop and new_lop are the same for this case.
 2528                  * We noted the unlock case above, so we don't need
 2529                  * new_lop->nfslo_type any longer.
 2530                  */
 2531                 tmp = new_lop->nfslo_first;
 2532                 if (unlock) {
 2533                     other_lop = new_lop;
 2534                     *new_lopp = NULL;
 2535                 } else {
 2536                     other_lop = *other_lopp;
 2537                     *other_lopp = NULL;
 2538                 }
 2539                 other_lop->nfslo_first = new_lop->nfslo_end;
 2540                 other_lop->nfslo_end = lop->nfslo_end;
 2541                 other_lop->nfslo_type = lop->nfslo_type;
 2542                 lop->nfslo_end = tmp;
 2543                 nfscl_insertlock(lp, other_lop, lop, local);
 2544                 ilop = lop;
 2545                 modified = 1;
 2546                 break;
 2547             }
 2548             ilop = lop;
 2549             lop = LIST_NEXT(lop, nfslo_list);
 2550             if (lop == NULL)
 2551                 break;
 2552         }
 2553 
 2554         /*
 2555          * Insert the new lock in the list at the appropriate place.
 2556          */
 2557         if (!unlock) {
 2558                 nfscl_insertlock(lp, new_lop, ilop, local);
 2559                 *new_lopp = NULL;
 2560                 modified = 1;
 2561         }
 2562         return (modified);
 2563 }
 2564 
 2565 /*
 2566  * This function must be run as a kernel thread.
 2567  * It does Renew Ops and recovery, when required.
 2568  */
 2569 void
 2570 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
 2571 {
 2572         struct nfsclowner *owp, *nowp;
 2573         struct nfsclopen *op;
 2574         struct nfscllockowner *lp, *nlp;
 2575         struct nfscldeleghead dh;
 2576         struct nfscldeleg *dp, *ndp;
 2577         struct ucred *cred;
 2578         u_int32_t clidrev;
 2579         int error, cbpathdown, islept, igotlock, ret, clearok;
 2580         uint32_t recover_done_time = 0;
 2581         time_t mytime;
 2582         static time_t prevsec = 0;
 2583         struct nfscllockownerfh *lfhp, *nlfhp;
 2584         struct nfscllockownerfhhead lfh;
 2585         struct nfscllayout *lyp, *nlyp;
 2586         struct nfscldevinfo *dip, *ndip;
 2587         struct nfscllayouthead rlh;
 2588         struct nfsclrecalllayout *recallp;
 2589         struct nfsclds *dsp;
 2590         struct mount *mp;
 2591         vnode_t vp;
 2592 
 2593         cred = newnfs_getcred();
 2594         NFSLOCKCLSTATE();
 2595         clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
 2596         mp = clp->nfsc_nmp->nm_mountp;
 2597         NFSUNLOCKCLSTATE();
 2598         for(;;) {
 2599                 newnfs_setroot(cred);
 2600                 cbpathdown = 0;
 2601                 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
 2602                         /*
 2603                          * Only allow one recover within 1/2 of the lease
 2604                          * duration (nfsc_renew).
 2605                          */
 2606                         if (recover_done_time < NFSD_MONOSEC) {
 2607                                 recover_done_time = NFSD_MONOSEC +
 2608                                     clp->nfsc_renew;
 2609                                 NFSCL_DEBUG(1, "Doing recovery..\n");
 2610                                 nfscl_recover(clp, cred, p);
 2611                         } else {
 2612                                 NFSCL_DEBUG(1, "Clear Recovery dt=%u ms=%jd\n",
 2613                                     recover_done_time, (intmax_t)NFSD_MONOSEC);
 2614                                 NFSLOCKCLSTATE();
 2615                                 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
 2616                                 NFSUNLOCKCLSTATE();
 2617                         }
 2618                 }
 2619                 if (clp->nfsc_expire <= NFSD_MONOSEC &&
 2620                     (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
 2621                         clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
 2622                         clidrev = clp->nfsc_clientidrev;
 2623                         error = nfsrpc_renew(clp, NULL, cred, p);
 2624                         if (error == NFSERR_CBPATHDOWN)
 2625                             cbpathdown = 1;
 2626                         else if (error == NFSERR_STALECLIENTID ||
 2627                             error == NFSERR_BADSESSION) {
 2628                             NFSLOCKCLSTATE();
 2629                             clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
 2630                             NFSUNLOCKCLSTATE();
 2631                         } else if (error == NFSERR_EXPIRED)
 2632                             (void) nfscl_hasexpired(clp, clidrev, p);
 2633                 }
 2634 
 2635 checkdsrenew:
 2636                 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
 2637                         /* Do renews for any DS sessions. */
 2638                         NFSLOCKMNT(clp->nfsc_nmp);
 2639                         /* Skip first entry, since the MDS is handled above. */
 2640                         dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
 2641                         if (dsp != NULL)
 2642                                 dsp = TAILQ_NEXT(dsp, nfsclds_list);
 2643                         while (dsp != NULL) {
 2644                                 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
 2645                                     dsp->nfsclds_sess.nfsess_defunct == 0) {
 2646                                         dsp->nfsclds_expire = NFSD_MONOSEC +
 2647                                             clp->nfsc_renew;
 2648                                         NFSUNLOCKMNT(clp->nfsc_nmp);
 2649                                         (void)nfsrpc_renew(clp, dsp, cred, p);
 2650                                         goto checkdsrenew;
 2651                                 }
 2652                                 dsp = TAILQ_NEXT(dsp, nfsclds_list);
 2653                         }
 2654                         NFSUNLOCKMNT(clp->nfsc_nmp);
 2655                 }
 2656 
 2657                 TAILQ_INIT(&dh);
 2658                 NFSLOCKCLSTATE();
 2659                 if (cbpathdown)
 2660                         /* It's a Total Recall! */
 2661                         nfscl_totalrecall(clp);
 2662 
 2663                 /*
 2664                  * Now, handle defunct owners.
 2665                  */
 2666                 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
 2667                         if (LIST_EMPTY(&owp->nfsow_open)) {
 2668                                 if (owp->nfsow_defunct != 0)
 2669                                         nfscl_freeopenowner(owp, 0);
 2670                         }
 2671                 }
 2672 
 2673                 /*
 2674                  * Do the recall on any delegations. To avoid trouble, always
 2675                  * come back up here after having slept.
 2676                  */
 2677                 igotlock = 0;
 2678 tryagain:
 2679                 dp = TAILQ_FIRST(&clp->nfsc_deleg);
 2680                 while (dp != NULL) {
 2681                         ndp = TAILQ_NEXT(dp, nfsdl_list);
 2682                         if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
 2683                                 /*
 2684                                  * Wait for outstanding I/O ops to be done.
 2685                                  */
 2686                                 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
 2687                                     if (igotlock) {
 2688                                         nfsv4_unlock(&clp->nfsc_lock, 0);
 2689                                         igotlock = 0;
 2690                                     }
 2691                                     dp->nfsdl_rwlock.nfslock_lock |=
 2692                                         NFSV4LOCK_WANTED;
 2693                                     msleep(&dp->nfsdl_rwlock,
 2694                                         NFSCLSTATEMUTEXPTR, PVFS, "nfscld",
 2695                                         5 * hz);
 2696                                     if (NFSCL_FORCEDISM(mp))
 2697                                         goto terminate;
 2698                                     goto tryagain;
 2699                                 }
 2700                                 while (!igotlock) {
 2701                                     igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
 2702                                         &islept, NFSCLSTATEMUTEXPTR, mp);
 2703                                     if (igotlock == 0 && NFSCL_FORCEDISM(mp))
 2704                                         goto terminate;
 2705                                     if (islept)
 2706                                         goto tryagain;
 2707                                 }
 2708                                 NFSUNLOCKCLSTATE();
 2709                                 newnfs_copycred(&dp->nfsdl_cred, cred);
 2710                                 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
 2711                                     NULL, cred, p, 1, &vp);
 2712                                 if (!ret) {
 2713                                     nfscl_cleandeleg(dp);
 2714                                     TAILQ_REMOVE(&clp->nfsc_deleg, dp,
 2715                                         nfsdl_list);
 2716                                     LIST_REMOVE(dp, nfsdl_hash);
 2717                                     TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
 2718                                     nfscl_delegcnt--;
 2719                                     nfsstatsv1.cldelegates--;
 2720                                 }
 2721                                 NFSLOCKCLSTATE();
 2722                                 /*
 2723                                  * The nfsc_lock must be released before doing
 2724                                  * vrele(), since it might call nfs_inactive().
 2725                                  * For the unlikely case where the vnode failed
 2726                                  * to be acquired by nfscl_recalldeleg(), a
 2727                                  * VOP_RECLAIM() should be in progress and it
 2728                                  * will return the delegation.
 2729                                  */
 2730                                 nfsv4_unlock(&clp->nfsc_lock, 0);
 2731                                 igotlock = 0;
 2732                                 if (vp != NULL) {
 2733                                         NFSUNLOCKCLSTATE();
 2734                                         vrele(vp);
 2735                                         NFSLOCKCLSTATE();
 2736                                 }
 2737                                 goto tryagain;
 2738                         }
 2739                         dp = ndp;
 2740                 }
 2741 
 2742                 /*
 2743                  * Clear out old delegations, if we are above the high water
 2744                  * mark. Only clear out ones with no state related to them.
 2745                  * The tailq list is in LRU order.
 2746                  */
 2747                 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
 2748                 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
 2749                     ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
 2750                     if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
 2751                         dp->nfsdl_rwlock.nfslock_lock == 0 &&
 2752                         dp->nfsdl_timestamp < NFSD_MONOSEC &&
 2753                         (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
 2754                           NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
 2755                         clearok = 1;
 2756                         LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
 2757                             op = LIST_FIRST(&owp->nfsow_open);
 2758                             if (op != NULL) {
 2759                                 clearok = 0;
 2760                                 break;
 2761                             }
 2762                         }
 2763                         if (clearok) {
 2764                             LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
 2765                                 if (!LIST_EMPTY(&lp->nfsl_lock)) {
 2766                                     clearok = 0;
 2767                                     break;
 2768                                 }
 2769                             }
 2770                         }
 2771                         if (clearok) {
 2772                             TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
 2773                             LIST_REMOVE(dp, nfsdl_hash);
 2774                             TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
 2775                             nfscl_delegcnt--;
 2776                             nfsstatsv1.cldelegates--;
 2777                         }
 2778                     }
 2779                     dp = ndp;
 2780                 }
 2781                 if (igotlock)
 2782                         nfsv4_unlock(&clp->nfsc_lock, 0);
 2783 
 2784                 /*
 2785                  * Do the recall on any layouts. To avoid trouble, always
 2786                  * come back up here after having slept.
 2787                  */
 2788                 TAILQ_INIT(&rlh);
 2789 tryagain2:
 2790                 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
 2791                         if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
 2792                                 /*
 2793                                  * Wait for outstanding I/O ops to be done.
 2794                                  */
 2795                                 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
 2796                                     (lyp->nfsly_lock.nfslock_lock &
 2797                                      NFSV4LOCK_LOCK) != 0) {
 2798                                         lyp->nfsly_lock.nfslock_lock |=
 2799                                             NFSV4LOCK_WANTED;
 2800                                         msleep(&lyp->nfsly_lock.nfslock_lock,
 2801                                             NFSCLSTATEMUTEXPTR, PVFS, "nfslyp",
 2802                                             5 * hz);
 2803                                         if (NFSCL_FORCEDISM(mp))
 2804                                             goto terminate;
 2805                                         goto tryagain2;
 2806                                 }
 2807                                 /* Move the layout to the recall list. */
 2808                                 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
 2809                                     nfsly_list);
 2810                                 LIST_REMOVE(lyp, nfsly_hash);
 2811                                 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
 2812 
 2813                                 /* Handle any layout commits. */
 2814                                 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
 2815                                     (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
 2816                                         lyp->nfsly_flags &= ~NFSLY_WRITTEN;
 2817                                         NFSUNLOCKCLSTATE();
 2818                                         NFSCL_DEBUG(3, "do layoutcommit\n");
 2819                                         nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
 2820                                             cred, p);
 2821                                         NFSLOCKCLSTATE();
 2822                                         goto tryagain2;
 2823                                 }
 2824                         }
 2825                 }
 2826 
 2827                 /* Now, look for stale layouts. */
 2828                 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
 2829                 while (lyp != NULL) {
 2830                         nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
 2831                         if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
 2832                             (lyp->nfsly_flags & (NFSLY_RECALL |
 2833                              NFSLY_RETONCLOSE)) == 0 &&
 2834                             lyp->nfsly_lock.nfslock_usecnt == 0 &&
 2835                             lyp->nfsly_lock.nfslock_lock == 0) {
 2836                                 NFSCL_DEBUG(4, "ret stale lay=%d\n",
 2837                                     nfscl_layoutcnt);
 2838                                 recallp = malloc(sizeof(*recallp),
 2839                                     M_NFSLAYRECALL, M_NOWAIT);
 2840                                 if (recallp == NULL)
 2841                                         break;
 2842                                 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
 2843                                     lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
 2844                                     lyp->nfsly_stateid.seqid, 0, 0, NULL,
 2845                                     recallp);
 2846                         }
 2847                         lyp = nlyp;
 2848                 }
 2849 
 2850                 /*
 2851                  * Free up any unreferenced device info structures.
 2852                  */
 2853                 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
 2854                         if (dip->nfsdi_layoutrefs == 0 &&
 2855                             dip->nfsdi_refcnt == 0) {
 2856                                 NFSCL_DEBUG(4, "freeing devinfo\n");
 2857                                 LIST_REMOVE(dip, nfsdi_list);
 2858                                 nfscl_freedevinfo(dip);
 2859                         }
 2860                 }
 2861                 NFSUNLOCKCLSTATE();
 2862 
 2863                 /* Do layout return(s), as required. */
 2864                 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
 2865                         TAILQ_REMOVE(&rlh, lyp, nfsly_list);
 2866                         NFSCL_DEBUG(4, "ret layout\n");
 2867                         nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
 2868                         if ((lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) {
 2869                                 NFSLOCKCLSTATE();
 2870                                 lyp->nfsly_flags |= NFSLY_RETURNED;
 2871                                 wakeup(lyp);
 2872                                 NFSUNLOCKCLSTATE();
 2873                         } else
 2874                                 nfscl_freelayout(lyp);
 2875                 }
 2876 
 2877                 /*
 2878                  * Delegreturn any delegations cleaned out or recalled.
 2879                  */
 2880                 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
 2881                         newnfs_copycred(&dp->nfsdl_cred, cred);
 2882                         (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
 2883                         TAILQ_REMOVE(&dh, dp, nfsdl_list);
 2884                         free(dp, M_NFSCLDELEG);
 2885                 }
 2886 
 2887                 SLIST_INIT(&lfh);
 2888                 /*
 2889                  * Call nfscl_cleanupkext() once per second to check for
 2890                  * open/lock owners where the process has exited.
 2891                  */
 2892                 mytime = NFSD_MONOSEC;
 2893                 if (prevsec != mytime) {
 2894                         prevsec = mytime;
 2895                         nfscl_cleanupkext(clp, &lfh);
 2896                 }
 2897 
 2898                 /*
 2899                  * Do a ReleaseLockOwner for all lock owners where the
 2900                  * associated process no longer exists, as found by
 2901                  * nfscl_cleanupkext().
 2902                  */
 2903                 newnfs_setroot(cred);
 2904                 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
 2905                         LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
 2906                             nlp) {
 2907                                 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
 2908                                     lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
 2909                                     p);
 2910                                 nfscl_freelockowner(lp, 0);
 2911                         }
 2912                         free(lfhp, M_TEMP);
 2913                 }
 2914                 SLIST_INIT(&lfh);
 2915 
 2916                 NFSLOCKCLSTATE();
 2917                 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
 2918                         (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
 2919                             hz);
 2920 terminate:
 2921                 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
 2922                         clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
 2923                         NFSUNLOCKCLSTATE();
 2924                         NFSFREECRED(cred);
 2925                         wakeup((caddr_t)clp);
 2926                         return;
 2927                 }
 2928                 NFSUNLOCKCLSTATE();
 2929         }
 2930 }
 2931 
 2932 /*
 2933  * Initiate state recovery. Called when NFSERR_STALECLIENTID,
 2934  * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
 2935  */
 2936 void
 2937 nfscl_initiate_recovery(struct nfsclclient *clp)
 2938 {
 2939 
 2940         if (clp == NULL)
 2941                 return;
 2942         NFSLOCKCLSTATE();
 2943         clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
 2944         NFSUNLOCKCLSTATE();
 2945         wakeup((caddr_t)clp);
 2946 }
 2947 
 2948 /*
 2949  * Dump out the state stuff for debugging.
 2950  */
 2951 void
 2952 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
 2953     int lockowner, int locks)
 2954 {
 2955         struct nfsclclient *clp;
 2956         struct nfsclowner *owp;
 2957         struct nfsclopen *op;
 2958         struct nfscllockowner *lp;
 2959         struct nfscllock *lop;
 2960         struct nfscldeleg *dp;
 2961 
 2962         clp = nmp->nm_clp;
 2963         if (clp == NULL) {
 2964                 printf("nfscl dumpstate NULL clp\n");
 2965                 return;
 2966         }
 2967         NFSLOCKCLSTATE();
 2968         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
 2969           LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
 2970             if (openowner && !LIST_EMPTY(&owp->nfsow_open))
 2971                 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
 2972                     owp->nfsow_owner[0], owp->nfsow_owner[1],
 2973                     owp->nfsow_owner[2], owp->nfsow_owner[3],
 2974                     owp->nfsow_seqid);
 2975             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 2976                 if (opens)
 2977                     printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
 2978                         op->nfso_stateid.other[0], op->nfso_stateid.other[1],
 2979                         op->nfso_stateid.other[2], op->nfso_opencnt,
 2980                         op->nfso_fh[12]);
 2981                 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
 2982                     if (lockowner)
 2983                         printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
 2984                             lp->nfsl_owner[0], lp->nfsl_owner[1],
 2985                             lp->nfsl_owner[2], lp->nfsl_owner[3],
 2986                             lp->nfsl_seqid,
 2987                             lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
 2988                             lp->nfsl_stateid.other[2]);
 2989                     LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
 2990                         if (locks)
 2991 #ifdef __FreeBSD__
 2992                             printf("lck typ=%d fst=%ju end=%ju\n",
 2993                                 lop->nfslo_type, (intmax_t)lop->nfslo_first,
 2994                                 (intmax_t)lop->nfslo_end);
 2995 #else
 2996                             printf("lck typ=%d fst=%qd end=%qd\n",
 2997                                 lop->nfslo_type, lop->nfslo_first,
 2998                                 lop->nfslo_end);
 2999 #endif
 3000                     }
 3001                 }
 3002             }
 3003           }
 3004         }
 3005         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 3006             if (openowner && !LIST_EMPTY(&owp->nfsow_open))
 3007                 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
 3008                     owp->nfsow_owner[0], owp->nfsow_owner[1],
 3009                     owp->nfsow_owner[2], owp->nfsow_owner[3],
 3010                     owp->nfsow_seqid);
 3011             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 3012                 if (opens)
 3013                     printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
 3014                         op->nfso_stateid.other[0], op->nfso_stateid.other[1],
 3015                         op->nfso_stateid.other[2], op->nfso_opencnt,
 3016                         op->nfso_fh[12]);
 3017                 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
 3018                     if (lockowner)
 3019                         printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
 3020                             lp->nfsl_owner[0], lp->nfsl_owner[1],
 3021                             lp->nfsl_owner[2], lp->nfsl_owner[3],
 3022                             lp->nfsl_seqid,
 3023                             lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
 3024                             lp->nfsl_stateid.other[2]);
 3025                     LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
 3026                         if (locks)
 3027 #ifdef __FreeBSD__
 3028                             printf("lck typ=%d fst=%ju end=%ju\n",
 3029                                 lop->nfslo_type, (intmax_t)lop->nfslo_first,
 3030                                 (intmax_t)lop->nfslo_end);
 3031 #else
 3032                             printf("lck typ=%d fst=%qd end=%qd\n",
 3033                                 lop->nfslo_type, lop->nfslo_first,
 3034                                 lop->nfslo_end);
 3035 #endif
 3036                     }
 3037                 }
 3038             }
 3039         }
 3040         NFSUNLOCKCLSTATE();
 3041 }
 3042 
 3043 /*
 3044  * Check for duplicate open owners and opens.
 3045  * (Only used as a diagnostic aid.)
 3046  */
 3047 void
 3048 nfscl_dupopen(vnode_t vp, int dupopens)
 3049 {
 3050         struct nfsclclient *clp;
 3051         struct nfsclowner *owp, *owp2;
 3052         struct nfsclopen *op, *op2;
 3053         struct nfsfh *nfhp;
 3054 
 3055         clp = VFSTONFS(vnode_mount(vp))->nm_clp;
 3056         if (clp == NULL) {
 3057                 printf("nfscl dupopen NULL clp\n");
 3058                 return;
 3059         }
 3060         nfhp = VTONFS(vp)->n_fhp;
 3061         NFSLOCKCLSTATE();
 3062 
 3063         /*
 3064          * First, search for duplicate owners.
 3065          * These should never happen!
 3066          */
 3067         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
 3068             LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 3069                 if (owp != owp2 &&
 3070                     !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
 3071                     NFSV4CL_LOCKNAMELEN)) {
 3072                         NFSUNLOCKCLSTATE();
 3073                         printf("DUP OWNER\n");
 3074                         nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0, 0);
 3075                         return;
 3076                 }
 3077             }
 3078         }
 3079 
 3080         /*
 3081          * Now, search for duplicate stateids.
 3082          * These shouldn't happen, either.
 3083          */
 3084         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
 3085             LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
 3086                 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 3087                     LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 3088                         if (op != op2 &&
 3089                             (op->nfso_stateid.other[0] != 0 ||
 3090                              op->nfso_stateid.other[1] != 0 ||
 3091                              op->nfso_stateid.other[2] != 0) &&
 3092                             op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
 3093                             op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
 3094                             op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
 3095                             NFSUNLOCKCLSTATE();
 3096                             printf("DUP STATEID\n");
 3097                             nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0,
 3098                                 0);
 3099                             return;
 3100                         }
 3101                     }
 3102                 }
 3103             }
 3104         }
 3105 
 3106         /*
 3107          * Now search for duplicate opens.
 3108          * Duplicate opens for the same owner
 3109          * should never occur. Other duplicates are
 3110          * possible and are checked for if "dupopens"
 3111          * is true.
 3112          */
 3113         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
 3114             LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
 3115                 if (nfhp->nfh_len == op2->nfso_fhlen &&
 3116                     !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
 3117                     LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 3118                         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 3119                             if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
 3120                                 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
 3121                                 (!NFSBCMP(op->nfso_own->nfsow_owner,
 3122                                  op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
 3123                                  dupopens)) {
 3124                                 if (!NFSBCMP(op->nfso_own->nfsow_owner,
 3125                                     op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
 3126                                     NFSUNLOCKCLSTATE();
 3127                                     printf("BADDUP OPEN\n");
 3128                                 } else {
 3129                                     NFSUNLOCKCLSTATE();
 3130                                     printf("DUP OPEN\n");
 3131                                 }
 3132                                 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1,
 3133                                     0, 0);
 3134                                 return;
 3135                             }
 3136                         }
 3137                     }
 3138                 }
 3139             }
 3140         }
 3141         NFSUNLOCKCLSTATE();
 3142 }
 3143 
 3144 /*
 3145  * During close, find an open that needs to be dereferenced and
 3146  * dereference it. If there are no more opens for this file,
 3147  * log a message to that effect.
 3148  * Opens aren't actually Close'd until VOP_INACTIVE() is performed
 3149  * on the file's vnode.
 3150  * This is the safe way, since it is difficult to identify
 3151  * which open the close is for and I/O can be performed after the
 3152  * close(2) system call when a file is mmap'd.
 3153  * If it returns 0 for success, there will be a referenced
 3154  * clp returned via clpp.
 3155  */
 3156 int
 3157 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
 3158 {
 3159         struct nfsclclient *clp;
 3160         struct nfsclowner *owp;
 3161         struct nfsclopen *op;
 3162         struct nfscldeleg *dp;
 3163         struct nfsfh *nfhp;
 3164         int error, notdecr;
 3165 
 3166         error = nfscl_getcl(vnode_mount(vp), NULL, NULL, 1, true, &clp);
 3167         if (error)
 3168                 return (error);
 3169         *clpp = clp;
 3170 
 3171         nfhp = VTONFS(vp)->n_fhp;
 3172         notdecr = 1;
 3173         NFSLOCKCLSTATE();
 3174         /*
 3175          * First, look for one under a delegation that was locally issued
 3176          * and just decrement the opencnt for it. Since all my Opens against
 3177          * the server are DENY_NONE, I don't see a problem with hanging
 3178          * onto them. (It is much easier to use one of the extant Opens
 3179          * that I already have on the server when a Delegation is recalled
 3180          * than to do fresh Opens.) Someday, I might need to rethink this, but.
 3181          */
 3182         dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
 3183         if (dp != NULL) {
 3184                 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
 3185                         op = LIST_FIRST(&owp->nfsow_open);
 3186                         if (op != NULL) {
 3187                                 /*
 3188                                  * Since a delegation is for a file, there
 3189                                  * should never be more than one open for
 3190                                  * each openowner.
 3191                                  */
 3192                                 if (LIST_NEXT(op, nfso_list) != NULL)
 3193                                         panic("nfscdeleg opens");
 3194                                 if (notdecr && op->nfso_opencnt > 0) {
 3195                                         notdecr = 0;
 3196                                         op->nfso_opencnt--;
 3197                                         break;
 3198                                 }
 3199                         }
 3200                 }
 3201         }
 3202 
 3203         /* Now process the opens against the server. */
 3204         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 3205                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 3206                         if (op->nfso_fhlen == nfhp->nfh_len &&
 3207                             !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
 3208                             nfhp->nfh_len)) {
 3209                                 /* Found an open, decrement cnt if possible */
 3210                                 if (notdecr && op->nfso_opencnt > 0) {
 3211                                         notdecr = 0;
 3212                                         op->nfso_opencnt--;
 3213                                 }
 3214                                 /*
 3215                                  * There are more opens, so just return.
 3216                                  */
 3217                                 if (op->nfso_opencnt > 0) {
 3218                                         NFSUNLOCKCLSTATE();
 3219                                         return (0);
 3220                                 }
 3221                         }
 3222                 }
 3223         }
 3224         NFSUNLOCKCLSTATE();
 3225         if (notdecr)
 3226                 printf("nfscl: never fnd open\n");
 3227         return (0);
 3228 }
 3229 
 3230 int
 3231 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
 3232 {
 3233         struct nfsclclient *clp;
 3234         struct nfsmount *nmp;
 3235         struct nfsclowner *owp, *nowp;
 3236         struct nfsclopen *op, *nop;
 3237         struct nfsclopenhead delayed;
 3238         struct nfscldeleg *dp;
 3239         struct nfsfh *nfhp;
 3240         struct nfsclrecalllayout *recallp;
 3241         struct nfscllayout *lyp;
 3242         int error;
 3243 
 3244         error = nfscl_getcl(vnode_mount(vp), NULL, NULL, 1, true, &clp);
 3245         if (error)
 3246                 return (error);
 3247         *clpp = clp;
 3248 
 3249         nmp = VFSTONFS(vnode_mount(vp));
 3250         nfhp = VTONFS(vp)->n_fhp;
 3251         recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
 3252         NFSLOCKCLSTATE();
 3253         /*
 3254          * First get rid of the local Open structures, which should be no
 3255          * longer in use.
 3256          */
 3257         dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
 3258         if (dp != NULL) {
 3259                 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
 3260                         op = LIST_FIRST(&owp->nfsow_open);
 3261                         if (op != NULL) {
 3262                                 KASSERT((op->nfso_opencnt == 0),
 3263                                     ("nfscl: bad open cnt on deleg"));
 3264                                 nfscl_freeopen(op, 1, true);
 3265                         }
 3266                         nfscl_freeopenowner(owp, 1);
 3267                 }
 3268         }
 3269 
 3270         /* Return any layouts marked return on close. */
 3271         nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp,
 3272             &lyp);
 3273 
 3274         /* Now process the opens against the server. */
 3275         LIST_INIT(&delayed);
 3276 lookformore:
 3277         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 3278                 op = LIST_FIRST(&owp->nfsow_open);
 3279                 while (op != NULL) {
 3280                         if (op->nfso_fhlen == nfhp->nfh_len &&
 3281                             !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
 3282                             nfhp->nfh_len)) {
 3283                                 /* Found an open, close it. */
 3284                                 KASSERT((op->nfso_opencnt == 0),
 3285                                     ("nfscl: bad open cnt on server"));
 3286                                 NFSUNLOCKCLSTATE();
 3287                                 if (NFSHASNFSV4N(nmp))
 3288                                         nfsrpc_doclose(nmp, op, p, false,
 3289                                             true);
 3290                                 else
 3291                                         nfsrpc_doclose(nmp, op, p, true,
 3292                                             true);
 3293                                 NFSLOCKCLSTATE();
 3294                                 if (error == NFSERR_DELAY) {
 3295                                         nfscl_unlinkopen(op);
 3296                                         op->nfso_own = NULL;
 3297                                         LIST_INSERT_HEAD(&delayed, op,
 3298                                             nfso_list);
 3299                                 }
 3300                                 goto lookformore;
 3301                         }
 3302                         op = LIST_NEXT(op, nfso_list);
 3303                 }
 3304         }
 3305         nfscl_clrelease(clp);
 3306 
 3307         /* Now, wait for any layout that is returned upon close. */
 3308         if (lyp != NULL) {
 3309                 while ((lyp->nfsly_flags & NFSLY_RETURNED) == 0) {
 3310                         if (NFSCL_FORCEDISM(vnode_mount(vp))) {
 3311                                 lyp = NULL;
 3312                                 break;
 3313                         }
 3314                         msleep(lyp, NFSCLSTATEMUTEXPTR, PZERO, "nfslroc", hz);
 3315                 }
 3316                 if (lyp != NULL)
 3317                         nfscl_freelayout(lyp);
 3318         }
 3319 
 3320         NFSUNLOCKCLSTATE();
 3321         /*
 3322          * recallp has been set NULL by nfscl_retoncloselayout() if it was
 3323          * used by the function, but calling free() with a NULL pointer is ok.
 3324          */
 3325         free(recallp, M_NFSLAYRECALL);
 3326 
 3327         /* Now, loop retrying the delayed closes. */
 3328         LIST_FOREACH_SAFE(op, &delayed, nfso_list, nop) {
 3329                 nfsrpc_doclose(nmp, op, p, true, false);
 3330                 LIST_REMOVE(op, nfso_list);
 3331                 nfscl_freeopen(op, 0, false);
 3332         }
 3333         return (0);
 3334 }
 3335 
 3336 /*
 3337  * Return all delegations on this client.
 3338  * (Must be called with client sleep lock.)
 3339  */
 3340 static void
 3341 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p,
 3342     struct nfscldeleghead *dhp)
 3343 {
 3344         struct nfscldeleg *dp, *ndp;
 3345         struct ucred *cred;
 3346 
 3347         cred = newnfs_getcred();
 3348         TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
 3349                 nfscl_cleandeleg(dp);
 3350                 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
 3351                 if (dhp != NULL) {
 3352                         nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
 3353                         TAILQ_INSERT_HEAD(dhp, dp, nfsdl_list);
 3354                 } else
 3355                         nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
 3356         }
 3357         NFSFREECRED(cred);
 3358 }
 3359 
 3360 /*
 3361  * Return any delegation for this vp.
 3362  */
 3363 void
 3364 nfscl_delegreturnvp(vnode_t vp, NFSPROC_T *p)
 3365 {
 3366         struct nfsclclient *clp;
 3367         struct nfscldeleg *dp;
 3368         struct ucred *cred;
 3369         struct nfsnode *np;
 3370 
 3371         np = VTONFS(vp);
 3372         cred = newnfs_getcred();
 3373         dp = NULL;
 3374         NFSLOCKCLSTATE();
 3375         clp = VFSTONFS(vp->v_mount)->nm_clp;
 3376         if (clp != NULL)
 3377                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
 3378                     np->n_fhp->nfh_len);
 3379         if (dp != NULL) {
 3380                 nfscl_cleandeleg(dp);
 3381                 nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
 3382                 NFSUNLOCKCLSTATE();
 3383                 newnfs_copycred(&dp->nfsdl_cred, cred);
 3384                 nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
 3385                 free(dp, M_NFSCLDELEG);
 3386         } else
 3387                 NFSUNLOCKCLSTATE();
 3388         NFSFREECRED(cred);
 3389 }
 3390 
 3391 /*
 3392  * Do a callback RPC.
 3393  */
 3394 void
 3395 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
 3396 {
 3397         int clist, gotseq_ok, i, j, k, op, rcalls;
 3398         u_int32_t *tl;
 3399         struct nfsclclient *clp;
 3400         struct nfscldeleg *dp = NULL;
 3401         int numops, taglen = -1, error = 0, trunc __unused;
 3402         u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
 3403         u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
 3404         vnode_t vp = NULL;
 3405         struct nfsnode *np;
 3406         struct vattr va;
 3407         struct nfsfh *nfhp;
 3408         mount_t mp;
 3409         nfsattrbit_t attrbits, rattrbits;
 3410         nfsv4stateid_t stateid;
 3411         uint32_t seqid, slotid = 0, highslot, cachethis __unused;
 3412         uint8_t sessionid[NFSX_V4SESSIONID];
 3413         struct mbuf *rep;
 3414         struct nfscllayout *lyp;
 3415         uint64_t filesid[2], len, off;
 3416         int changed, gotone, laytype, recalltype;
 3417         uint32_t iomode;
 3418         struct nfsclrecalllayout *recallp = NULL;
 3419         struct nfsclsession *tsep;
 3420 
 3421         gotseq_ok = 0;
 3422         nfsrvd_rephead(nd);
 3423         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
 3424         taglen = fxdr_unsigned(int, *tl);
 3425         if (taglen < 0) {
 3426                 error = EBADRPC;
 3427                 goto nfsmout;
 3428         }
 3429         if (taglen <= NFSV4_SMALLSTR)
 3430                 tagstr = tag;
 3431         else
 3432                 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
 3433         error = nfsrv_mtostr(nd, tagstr, taglen);
 3434         if (error) {
 3435                 if (taglen > NFSV4_SMALLSTR)
 3436                         free(tagstr, M_TEMP);
 3437                 taglen = -1;
 3438                 goto nfsmout;
 3439         }
 3440         (void) nfsm_strtom(nd, tag, taglen);
 3441         if (taglen > NFSV4_SMALLSTR) {
 3442                 free(tagstr, M_TEMP);
 3443         }
 3444         NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
 3445         NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
 3446         minorvers = fxdr_unsigned(u_int32_t, *tl++);
 3447         if (minorvers != NFSV4_MINORVERSION && minorvers != NFSV41_MINORVERSION)
 3448                 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
 3449         cbident = fxdr_unsigned(u_int32_t, *tl++);
 3450         if (nd->nd_repstat)
 3451                 numops = 0;
 3452         else
 3453                 numops = fxdr_unsigned(int, *tl);
 3454         /*
 3455          * Loop around doing the sub ops.
 3456          */
 3457         for (i = 0; i < numops; i++) {
 3458                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
 3459                 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
 3460                 *repp++ = *tl;
 3461                 op = fxdr_unsigned(int, *tl);
 3462                 if (op < NFSV4OP_CBGETATTR ||
 3463                    (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
 3464                    (op > NFSV4OP_CBNOTIFYDEVID &&
 3465                     minorvers == NFSV41_MINORVERSION)) {
 3466                     nd->nd_repstat = NFSERR_OPILLEGAL;
 3467                     *repp = nfscl_errmap(nd, minorvers);
 3468                     retops++;
 3469                     break;
 3470                 }
 3471                 nd->nd_procnum = op;
 3472                 if (op < NFSV41_CBNOPS)
 3473                         nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
 3474                 switch (op) {
 3475                 case NFSV4OP_CBGETATTR:
 3476                         NFSCL_DEBUG(4, "cbgetattr\n");
 3477                         mp = NULL;
 3478                         vp = NULL;
 3479                         error = nfsm_getfh(nd, &nfhp);
 3480                         if (!error)
 3481                                 error = nfsrv_getattrbits(nd, &attrbits,
 3482                                     NULL, NULL);
 3483                         if (error == 0 && i == 0 &&
 3484                             minorvers != NFSV4_MINORVERSION)
 3485                                 error = NFSERR_OPNOTINSESS;
 3486                         if (!error) {
 3487                                 mp = nfscl_getmnt(minorvers, sessionid, cbident,
 3488                                     &clp);
 3489                                 if (mp == NULL)
 3490                                         error = NFSERR_SERVERFAULT;
 3491                         }
 3492                         if (!error) {
 3493                                 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
 3494                                     nfhp->nfh_len, p, &np);
 3495                                 if (!error)
 3496                                         vp = NFSTOV(np);
 3497                         }
 3498                         if (!error) {
 3499                                 NFSZERO_ATTRBIT(&rattrbits);
 3500                                 NFSLOCKCLSTATE();
 3501                                 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
 3502                                     nfhp->nfh_len);
 3503                                 if (dp != NULL) {
 3504                                         if (NFSISSET_ATTRBIT(&attrbits,
 3505                                             NFSATTRBIT_SIZE)) {
 3506                                                 if (vp != NULL)
 3507                                                         va.va_size = np->n_size;
 3508                                                 else
 3509                                                         va.va_size =
 3510                                                             dp->nfsdl_size;
 3511                                                 NFSSETBIT_ATTRBIT(&rattrbits,
 3512                                                     NFSATTRBIT_SIZE);
 3513                                         }
 3514                                         if (NFSISSET_ATTRBIT(&attrbits,
 3515                                             NFSATTRBIT_CHANGE)) {
 3516                                                 va.va_filerev =
 3517                                                     dp->nfsdl_change;
 3518                                                 if (vp == NULL ||
 3519                                                     (np->n_flag & NDELEGMOD))
 3520                                                         va.va_filerev++;
 3521                                                 NFSSETBIT_ATTRBIT(&rattrbits,
 3522                                                     NFSATTRBIT_CHANGE);
 3523                                         }
 3524                                 } else
 3525                                         error = NFSERR_SERVERFAULT;
 3526                                 NFSUNLOCKCLSTATE();
 3527                         }
 3528                         if (vp != NULL)
 3529                                 vrele(vp);
 3530                         if (mp != NULL)
 3531                                 vfs_unbusy(mp);
 3532                         if (nfhp != NULL)
 3533                                 free(nfhp, M_NFSFH);
 3534                         if (!error)
 3535                                 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
 3536                                     NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
 3537                                     (uint64_t)0, NULL);
 3538                         break;
 3539                 case NFSV4OP_CBRECALL:
 3540                         NFSCL_DEBUG(4, "cbrecall\n");
 3541                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
 3542                             NFSX_UNSIGNED);
 3543                         stateid.seqid = *tl++;
 3544                         NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
 3545                             NFSX_STATEIDOTHER);
 3546                         tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
 3547                         trunc = fxdr_unsigned(int, *tl);
 3548                         error = nfsm_getfh(nd, &nfhp);
 3549                         if (error == 0 && i == 0 &&
 3550                             minorvers != NFSV4_MINORVERSION)
 3551                                 error = NFSERR_OPNOTINSESS;
 3552                         if (!error) {
 3553                                 NFSLOCKCLSTATE();
 3554                                 if (minorvers == NFSV4_MINORVERSION)
 3555                                         clp = nfscl_getclnt(cbident);
 3556                                 else
 3557                                         clp = nfscl_getclntsess(sessionid);
 3558                                 if (clp != NULL) {
 3559                                         dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
 3560                                             nfhp->nfh_len);
 3561                                         if (dp != NULL && (dp->nfsdl_flags &
 3562                                             NFSCLDL_DELEGRET) == 0) {
 3563                                                 dp->nfsdl_flags |=
 3564                                                     NFSCLDL_RECALL;
 3565                                                 wakeup((caddr_t)clp);
 3566                                         }
 3567                                 } else {
 3568                                         error = NFSERR_SERVERFAULT;
 3569                                 }
 3570                                 NFSUNLOCKCLSTATE();
 3571                         }
 3572                         if (nfhp != NULL)
 3573                                 free(nfhp, M_NFSFH);
 3574                         break;
 3575                 case NFSV4OP_CBLAYOUTRECALL:
 3576                         NFSCL_DEBUG(4, "cblayrec\n");
 3577                         nfhp = NULL;
 3578                         NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
 3579                         laytype = fxdr_unsigned(int, *tl++);
 3580                         iomode = fxdr_unsigned(uint32_t, *tl++);
 3581                         if (newnfs_true == *tl++)
 3582                                 changed = 1;
 3583                         else
 3584                                 changed = 0;
 3585                         recalltype = fxdr_unsigned(int, *tl);
 3586                         NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
 3587                             laytype, iomode, changed, recalltype);
 3588                         recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
 3589                             M_WAITOK);
 3590                         if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
 3591                             laytype != NFSLAYOUT_FLEXFILE)
 3592                                 error = NFSERR_NOMATCHLAYOUT;
 3593                         else if (recalltype == NFSLAYOUTRETURN_FILE) {
 3594                                 error = nfsm_getfh(nd, &nfhp);
 3595                                 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
 3596                                 if (error != 0)
 3597                                         goto nfsmout;
 3598                                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
 3599                                     NFSX_STATEID);
 3600                                 off = fxdr_hyper(tl); tl += 2;
 3601                                 len = fxdr_hyper(tl); tl += 2;
 3602                                 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
 3603                                 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
 3604                                 if (minorvers == NFSV4_MINORVERSION)
 3605                                         error = NFSERR_NOTSUPP;
 3606                                 else if (i == 0)
 3607                                         error = NFSERR_OPNOTINSESS;
 3608                                 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
 3609                                     (uintmax_t)off, (uintmax_t)len,
 3610                                     stateid.seqid, error);
 3611                                 if (error == 0) {
 3612                                         NFSLOCKCLSTATE();
 3613                                         clp = nfscl_getclntsess(sessionid);
 3614                                         NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
 3615                                         if (clp != NULL) {
 3616                                                 lyp = nfscl_findlayout(clp,
 3617                                                     nfhp->nfh_fh,
 3618                                                     nfhp->nfh_len);
 3619                                                 NFSCL_DEBUG(4, "cblyp=%p\n",
 3620                                                     lyp);
 3621                                                 if (lyp != NULL &&
 3622                                                     (lyp->nfsly_flags &
 3623                                                      (NFSLY_FILES |
 3624                                                       NFSLY_FLEXFILE)) != 0 &&
 3625                                                     !NFSBCMP(stateid.other,
 3626                                                     lyp->nfsly_stateid.other,
 3627                                                     NFSX_STATEIDOTHER)) {
 3628                                                         error =
 3629                                                             nfscl_layoutrecall(
 3630                                                             recalltype,
 3631                                                             lyp, iomode, off,
 3632                                                             len, stateid.seqid,
 3633                                                             0, 0, NULL,
 3634                                                             recallp);
 3635                                                         if (error == 0 &&
 3636                                                             stateid.seqid >
 3637                                                             lyp->nfsly_stateid.seqid)
 3638                                                                 lyp->nfsly_stateid.seqid =
 3639                                                                     stateid.seqid;
 3640                                                         recallp = NULL;
 3641                                                         wakeup(clp);
 3642                                                         NFSCL_DEBUG(4,
 3643                                                             "aft layrcal=%d "
 3644                                                             "layseqid=%d\n",
 3645                                                             error,
 3646                                                             lyp->nfsly_stateid.seqid);
 3647                                                 } else
 3648                                                         error =
 3649                                                           NFSERR_NOMATCHLAYOUT;
 3650                                         } else
 3651                                                 error = NFSERR_NOMATCHLAYOUT;
 3652                                         NFSUNLOCKCLSTATE();
 3653                                 }
 3654                                 free(nfhp, M_NFSFH);
 3655                         } else if (recalltype == NFSLAYOUTRETURN_FSID) {
 3656                                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
 3657                                 filesid[0] = fxdr_hyper(tl); tl += 2;
 3658                                 filesid[1] = fxdr_hyper(tl); tl += 2;
 3659                                 gotone = 0;
 3660                                 NFSLOCKCLSTATE();
 3661                                 clp = nfscl_getclntsess(sessionid);
 3662                                 if (clp != NULL) {
 3663                                         TAILQ_FOREACH(lyp, &clp->nfsc_layout,
 3664                                             nfsly_list) {
 3665                                                 if (lyp->nfsly_filesid[0] ==
 3666                                                     filesid[0] &&
 3667                                                     lyp->nfsly_filesid[1] ==
 3668                                                     filesid[1]) {
 3669                                                         error =
 3670                                                             nfscl_layoutrecall(
 3671                                                             recalltype,
 3672                                                             lyp, iomode, 0,
 3673                                                             UINT64_MAX,
 3674                                                             lyp->nfsly_stateid.seqid,
 3675                                                             0, 0, NULL,
 3676                                                             recallp);
 3677                                                         recallp = NULL;
 3678                                                         gotone = 1;
 3679                                                 }
 3680                                         }
 3681                                         if (gotone != 0)
 3682                                                 wakeup(clp);
 3683                                         else
 3684                                                 error = NFSERR_NOMATCHLAYOUT;
 3685                                 } else
 3686                                         error = NFSERR_NOMATCHLAYOUT;
 3687                                 NFSUNLOCKCLSTATE();
 3688                         } else if (recalltype == NFSLAYOUTRETURN_ALL) {
 3689                                 gotone = 0;
 3690                                 NFSLOCKCLSTATE();
 3691                                 clp = nfscl_getclntsess(sessionid);
 3692                                 if (clp != NULL) {
 3693                                         TAILQ_FOREACH(lyp, &clp->nfsc_layout,
 3694                                             nfsly_list) {
 3695                                                 error = nfscl_layoutrecall(
 3696                                                     recalltype, lyp, iomode, 0,
 3697                                                     UINT64_MAX,
 3698                                                     lyp->nfsly_stateid.seqid,
 3699                                                     0, 0, NULL, recallp);
 3700                                                 recallp = NULL;
 3701                                                 gotone = 1;
 3702                                         }
 3703                                         if (gotone != 0)
 3704                                                 wakeup(clp);
 3705                                         else
 3706                                                 error = NFSERR_NOMATCHLAYOUT;
 3707                                 } else
 3708                                         error = NFSERR_NOMATCHLAYOUT;
 3709                                 NFSUNLOCKCLSTATE();
 3710                         } else
 3711                                 error = NFSERR_NOMATCHLAYOUT;
 3712                         if (recallp != NULL) {
 3713                                 free(recallp, M_NFSLAYRECALL);
 3714                                 recallp = NULL;
 3715                         }
 3716                         break;
 3717                 case NFSV4OP_CBSEQUENCE:
 3718                         NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
 3719                             5 * NFSX_UNSIGNED);
 3720                         bcopy(tl, sessionid, NFSX_V4SESSIONID);
 3721                         tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
 3722                         seqid = fxdr_unsigned(uint32_t, *tl++);
 3723                         slotid = fxdr_unsigned(uint32_t, *tl++);
 3724                         highslot = fxdr_unsigned(uint32_t, *tl++);
 3725                         cachethis = *tl++;
 3726                         /* Throw away the referring call stuff. */
 3727                         clist = fxdr_unsigned(int, *tl);
 3728                         for (j = 0; j < clist; j++) {
 3729                                 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
 3730                                     NFSX_UNSIGNED);
 3731                                 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
 3732                                 rcalls = fxdr_unsigned(int, *tl);
 3733                                 for (k = 0; k < rcalls; k++) {
 3734                                         NFSM_DISSECT(tl, uint32_t *,
 3735                                             2 * NFSX_UNSIGNED);
 3736                                 }
 3737                         }
 3738                         NFSLOCKCLSTATE();
 3739                         if (i == 0) {
 3740                                 clp = nfscl_getclntsess(sessionid);
 3741                                 if (clp == NULL)
 3742                                         error = NFSERR_SERVERFAULT;
 3743                         } else
 3744                                 error = NFSERR_SEQUENCEPOS;
 3745                         if (error == 0) {
 3746                                 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
 3747                                 error = nfsv4_seqsession(seqid, slotid,
 3748                                     highslot, tsep->nfsess_cbslots, &rep,
 3749                                     tsep->nfsess_backslots);
 3750                         }
 3751                         NFSUNLOCKCLSTATE();
 3752                         if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
 3753                                 gotseq_ok = 1;
 3754                                 if (rep != NULL) {
 3755                                         /*
 3756                                          * Handle a reply for a retried
 3757                                          * callback.  The reply will be
 3758                                          * re-inserted in the session cache
 3759                                          * by the nfsv4_seqsess_cacherep() call
 3760                                          * after out:
 3761                                          */
 3762                                         KASSERT(error == NFSERR_REPLYFROMCACHE,
 3763                                             ("cbsequence: non-NULL rep"));
 3764                                         NFSCL_DEBUG(4, "Got cbretry\n");
 3765                                         m_freem(nd->nd_mreq);
 3766                                         nd->nd_mreq = rep;
 3767                                         rep = NULL;
 3768                                         goto out;
 3769                                 }
 3770                                 NFSM_BUILD(tl, uint32_t *,
 3771                                     NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
 3772                                 bcopy(sessionid, tl, NFSX_V4SESSIONID);
 3773                                 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
 3774                                 *tl++ = txdr_unsigned(seqid);
 3775                                 *tl++ = txdr_unsigned(slotid);
 3776                                 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
 3777                                 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
 3778                         }
 3779                         break;
 3780                 default:
 3781                         if (i == 0 && minorvers == NFSV41_MINORVERSION)
 3782                                 error = NFSERR_OPNOTINSESS;
 3783                         else {
 3784                                 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
 3785                                 error = NFSERR_NOTSUPP;
 3786                         }
 3787                         break;
 3788                 }
 3789                 if (error) {
 3790                         if (error == EBADRPC || error == NFSERR_BADXDR) {
 3791                                 nd->nd_repstat = NFSERR_BADXDR;
 3792                         } else {
 3793                                 nd->nd_repstat = error;
 3794                         }
 3795                         error = 0;
 3796                 }
 3797                 retops++;
 3798                 if (nd->nd_repstat) {
 3799                         *repp = nfscl_errmap(nd, minorvers);
 3800                         break;
 3801                 } else
 3802                         *repp = 0;      /* NFS4_OK */
 3803         }
 3804 nfsmout:
 3805         if (recallp != NULL)
 3806                 free(recallp, M_NFSLAYRECALL);
 3807         if (error) {
 3808                 if (error == EBADRPC || error == NFSERR_BADXDR)
 3809                         nd->nd_repstat = NFSERR_BADXDR;
 3810                 else
 3811                         printf("nfsv4 comperr1=%d\n", error);
 3812         }
 3813         if (taglen == -1) {
 3814                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
 3815                 *tl++ = 0;
 3816                 *tl = 0;
 3817         } else {
 3818                 *retopsp = txdr_unsigned(retops);
 3819         }
 3820         *nd->nd_errp = nfscl_errmap(nd, minorvers);
 3821 out:
 3822         if (gotseq_ok != 0) {
 3823                 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
 3824                 NFSLOCKCLSTATE();
 3825                 clp = nfscl_getclntsess(sessionid);
 3826                 if (clp != NULL) {
 3827                         tsep = nfsmnt_mdssession(clp->nfsc_nmp);
 3828                         nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
 3829                             NFSERR_OK, &rep);
 3830                         NFSUNLOCKCLSTATE();
 3831                 } else {
 3832                         NFSUNLOCKCLSTATE();
 3833                         m_freem(rep);
 3834                 }
 3835         }
 3836 }
 3837 
 3838 /*
 3839  * Generate the next cbident value. Basically just increment a static value
 3840  * and then check that it isn't already in the list, if it has wrapped around.
 3841  */
 3842 static u_int32_t
 3843 nfscl_nextcbident(void)
 3844 {
 3845         struct nfsclclient *clp;
 3846         int matched;
 3847         static u_int32_t nextcbident = 0;
 3848         static int haswrapped = 0;
 3849 
 3850         nextcbident++;
 3851         if (nextcbident == 0)
 3852                 haswrapped = 1;
 3853         if (haswrapped) {
 3854                 /*
 3855                  * Search the clientid list for one already using this cbident.
 3856                  */
 3857                 do {
 3858                         matched = 0;
 3859                         NFSLOCKCLSTATE();
 3860                         LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
 3861                                 if (clp->nfsc_cbident == nextcbident) {
 3862                                         matched = 1;
 3863                                         break;
 3864                                 }
 3865                         }
 3866                         NFSUNLOCKCLSTATE();
 3867                         if (matched == 1)
 3868                                 nextcbident++;
 3869                 } while (matched);
 3870         }
 3871         return (nextcbident);
 3872 }
 3873 
 3874 /*
 3875  * Get the mount point related to a given cbident or session and busy it.
 3876  */
 3877 static mount_t
 3878 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
 3879     struct nfsclclient **clpp)
 3880 {
 3881         struct nfsclclient *clp;
 3882         mount_t mp;
 3883         int error;
 3884         struct nfsclsession *tsep;
 3885 
 3886         *clpp = NULL;
 3887         NFSLOCKCLSTATE();
 3888         LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
 3889                 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
 3890                 if (minorvers == NFSV4_MINORVERSION) {
 3891                         if (clp->nfsc_cbident == cbident)
 3892                                 break;
 3893                 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
 3894                     NFSX_V4SESSIONID))
 3895                         break;
 3896         }
 3897         if (clp == NULL) {
 3898                 NFSUNLOCKCLSTATE();
 3899                 return (NULL);
 3900         }
 3901         mp = clp->nfsc_nmp->nm_mountp;
 3902         vfs_ref(mp);
 3903         NFSUNLOCKCLSTATE();
 3904         error = vfs_busy(mp, 0);
 3905         vfs_rel(mp);
 3906         if (error != 0)
 3907                 return (NULL);
 3908         *clpp = clp;
 3909         return (mp);
 3910 }
 3911 
 3912 /*
 3913  * Get the clientid pointer related to a given cbident.
 3914  */
 3915 static struct nfsclclient *
 3916 nfscl_getclnt(u_int32_t cbident)
 3917 {
 3918         struct nfsclclient *clp;
 3919 
 3920         LIST_FOREACH(clp, &nfsclhead, nfsc_list)
 3921                 if (clp->nfsc_cbident == cbident)
 3922                         break;
 3923         return (clp);
 3924 }
 3925 
 3926 /*
 3927  * Get the clientid pointer related to a given sessionid.
 3928  */
 3929 static struct nfsclclient *
 3930 nfscl_getclntsess(uint8_t *sessionid)
 3931 {
 3932         struct nfsclclient *clp;
 3933         struct nfsclsession *tsep;
 3934 
 3935         LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
 3936                 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
 3937                 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
 3938                     NFSX_V4SESSIONID))
 3939                         break;
 3940         }
 3941         return (clp);
 3942 }
 3943 
 3944 /*
 3945  * Search for a lock conflict locally on the client. A conflict occurs if
 3946  * - not same owner and overlapping byte range and at least one of them is
 3947  *   a write lock or this is an unlock.
 3948  */
 3949 static int
 3950 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
 3951     struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
 3952     struct nfscllock **lopp)
 3953 {
 3954         struct nfsclowner *owp;
 3955         struct nfsclopen *op;
 3956         int ret;
 3957 
 3958         if (dp != NULL) {
 3959                 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
 3960                 if (ret)
 3961                         return (ret);
 3962         }
 3963         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 3964                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 3965                         if (op->nfso_fhlen == fhlen &&
 3966                             !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
 3967                                 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
 3968                                     own, lopp);
 3969                                 if (ret)
 3970                                         return (ret);
 3971                         }
 3972                 }
 3973         }
 3974         return (0);
 3975 }
 3976 
 3977 static int
 3978 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
 3979     u_int8_t *own, struct nfscllock **lopp)
 3980 {
 3981         struct nfscllockowner *lp;
 3982         struct nfscllock *lop;
 3983 
 3984         LIST_FOREACH(lp, lhp, nfsl_list) {
 3985                 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
 3986                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
 3987                                 if (lop->nfslo_first >= nlop->nfslo_end)
 3988                                         break;
 3989                                 if (lop->nfslo_end <= nlop->nfslo_first)
 3990                                         continue;
 3991                                 if (lop->nfslo_type == F_WRLCK ||
 3992                                     nlop->nfslo_type == F_WRLCK ||
 3993                                     nlop->nfslo_type == F_UNLCK) {
 3994                                         if (lopp != NULL)
 3995                                                 *lopp = lop;
 3996                                         return (NFSERR_DENIED);
 3997                                 }
 3998                         }
 3999                 }
 4000         }
 4001         return (0);
 4002 }
 4003 
 4004 /*
 4005  * Check for a local conflicting lock.
 4006  */
 4007 int
 4008 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
 4009     u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
 4010 {
 4011         struct nfscllock *lop, nlck;
 4012         struct nfscldeleg *dp;
 4013         struct nfsnode *np;
 4014         u_int8_t own[NFSV4CL_LOCKNAMELEN];
 4015         int error;
 4016 
 4017         nlck.nfslo_type = fl->l_type;
 4018         nlck.nfslo_first = off;
 4019         if (len == NFS64BITSSET) {
 4020                 nlck.nfslo_end = NFS64BITSSET;
 4021         } else {
 4022                 nlck.nfslo_end = off + len;
 4023                 if (nlck.nfslo_end <= nlck.nfslo_first)
 4024                         return (NFSERR_INVAL);
 4025         }
 4026         np = VTONFS(vp);
 4027         nfscl_filllockowner(id, own, flags);
 4028         NFSLOCKCLSTATE();
 4029         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
 4030         error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
 4031             &nlck, own, dp, &lop);
 4032         if (error != 0) {
 4033                 fl->l_whence = SEEK_SET;
 4034                 fl->l_start = lop->nfslo_first;
 4035                 if (lop->nfslo_end == NFS64BITSSET)
 4036                         fl->l_len = 0;
 4037                 else
 4038                         fl->l_len = lop->nfslo_end - lop->nfslo_first;
 4039                 fl->l_pid = (pid_t)0;
 4040                 fl->l_type = lop->nfslo_type;
 4041                 error = -1;                     /* no RPC required */
 4042         } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
 4043             fl->l_type == F_RDLCK)) {
 4044                 /*
 4045                  * The delegation ensures that there isn't a conflicting
 4046                  * lock on the server, so return -1 to indicate an RPC
 4047                  * isn't required.
 4048                  */
 4049                 fl->l_type = F_UNLCK;
 4050                 error = -1;
 4051         }
 4052         NFSUNLOCKCLSTATE();
 4053         return (error);
 4054 }
 4055 
 4056 /*
 4057  * Handle Recall of a delegation.
 4058  * The clp must be exclusive locked when this is called.
 4059  */
 4060 static int
 4061 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
 4062     struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
 4063     int called_from_renewthread, vnode_t *vpp)
 4064 {
 4065         struct nfsclowner *owp, *lowp, *nowp;
 4066         struct nfsclopen *op, *lop;
 4067         struct nfscllockowner *lp;
 4068         struct nfscllock *lckp;
 4069         struct nfsnode *np;
 4070         int error = 0, ret;
 4071 
 4072         if (vp == NULL) {
 4073                 KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL"));
 4074                 *vpp = NULL;
 4075                 /*
 4076                  * First, get a vnode for the file. This is needed to do RPCs.
 4077                  */
 4078                 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
 4079                     dp->nfsdl_fhlen, p, &np);
 4080                 if (ret) {
 4081                         /*
 4082                          * File isn't open, so nothing to move over to the
 4083                          * server.
 4084                          */
 4085                         return (0);
 4086                 }
 4087                 vp = NFSTOV(np);
 4088                 *vpp = vp;
 4089         } else {
 4090                 np = VTONFS(vp);
 4091         }
 4092         dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
 4093 
 4094         /*
 4095          * Ok, if it's a write delegation, flush data to the server, so
 4096          * that close/open consistency is retained.
 4097          */
 4098         ret = 0;
 4099         NFSLOCKNODE(np);
 4100         if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
 4101                 np->n_flag |= NDELEGRECALL;
 4102                 NFSUNLOCKNODE(np);
 4103                 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
 4104                 NFSLOCKNODE(np);
 4105                 np->n_flag &= ~NDELEGRECALL;
 4106         }
 4107         NFSINVALATTRCACHE(np);
 4108         NFSUNLOCKNODE(np);
 4109         if (ret == EIO && called_from_renewthread != 0) {
 4110                 /*
 4111                  * If the flush failed with EIO for the renew thread,
 4112                  * return now, so that the dirty buffer will be flushed
 4113                  * later.
 4114                  */
 4115                 return (ret);
 4116         }
 4117 
 4118         /*
 4119          * Now, for each openowner with opens issued locally, move them
 4120          * over to state against the server.
 4121          */
 4122         LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
 4123                 lop = LIST_FIRST(&lowp->nfsow_open);
 4124                 if (lop != NULL) {
 4125                         if (LIST_NEXT(lop, nfso_list) != NULL)
 4126                                 panic("nfsdlg mult opens");
 4127                         /*
 4128                          * Look for the same openowner against the server.
 4129                          */
 4130                         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
 4131                                 if (!NFSBCMP(lowp->nfsow_owner,
 4132                                     owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
 4133                                         newnfs_copycred(&dp->nfsdl_cred, cred);
 4134                                         ret = nfscl_moveopen(vp, clp, nmp, lop,
 4135                                             owp, dp, cred, p);
 4136                                         if (ret == NFSERR_STALECLIENTID ||
 4137                                             ret == NFSERR_STALEDONTRECOVER ||
 4138                                             ret == NFSERR_BADSESSION)
 4139                                                 return (ret);
 4140                                         if (ret) {
 4141                                                 nfscl_freeopen(lop, 1, true);
 4142                                                 if (!error)
 4143                                                         error = ret;
 4144                                         }
 4145                                         break;
 4146                                 }
 4147                         }
 4148 
 4149                         /*
 4150                          * If no openowner found, create one and get an open
 4151                          * for it.
 4152                          */
 4153                         if (owp == NULL) {
 4154                                 nowp = malloc(
 4155                                     sizeof (struct nfsclowner), M_NFSCLOWNER,
 4156                                     M_WAITOK);
 4157                                 nfscl_newopen(clp, NULL, &owp, &nowp, &op, 
 4158                                     NULL, lowp->nfsow_owner, dp->nfsdl_fh,
 4159                                     dp->nfsdl_fhlen, NULL, NULL);
 4160                                 newnfs_copycred(&dp->nfsdl_cred, cred);
 4161                                 ret = nfscl_moveopen(vp, clp, nmp, lop,
 4162                                     owp, dp, cred, p);
 4163                                 if (ret) {
 4164                                         nfscl_freeopenowner(owp, 0);
 4165                                         if (ret == NFSERR_STALECLIENTID ||
 4166                                             ret == NFSERR_STALEDONTRECOVER ||
 4167                                             ret == NFSERR_BADSESSION)
 4168                                                 return (ret);
 4169                                         if (ret) {
 4170                                                 nfscl_freeopen(lop, 1, true);
 4171                                                 if (!error)
 4172                                                         error = ret;
 4173                                         }
 4174                                 }
 4175                         }
 4176                 }
 4177         }
 4178 
 4179         /*
 4180          * Now, get byte range locks for any locks done locally.
 4181          */
 4182         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
 4183                 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
 4184                         newnfs_copycred(&dp->nfsdl_cred, cred);
 4185                         ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
 4186                         if (ret == NFSERR_STALESTATEID ||
 4187                             ret == NFSERR_STALEDONTRECOVER ||
 4188                             ret == NFSERR_STALECLIENTID ||
 4189                             ret == NFSERR_BADSESSION)
 4190                                 return (ret);
 4191                         if (ret && !error)
 4192                                 error = ret;
 4193                 }
 4194         }
 4195         return (error);
 4196 }
 4197 
 4198 /*
 4199  * Move a locally issued open over to an owner on the state list.
 4200  * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
 4201  * returns with it unlocked.
 4202  */
 4203 static int
 4204 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
 4205     struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
 4206     struct ucred *cred, NFSPROC_T *p)
 4207 {
 4208         struct nfsclopen *op, *nop;
 4209         struct nfscldeleg *ndp;
 4210         struct nfsnode *np;
 4211         int error = 0, newone;
 4212 
 4213         /*
 4214          * First, look for an appropriate open, If found, just increment the
 4215          * opencnt in it.
 4216          */
 4217         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
 4218                 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
 4219                     op->nfso_fhlen == lop->nfso_fhlen &&
 4220                     !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
 4221                         op->nfso_opencnt += lop->nfso_opencnt;
 4222                         nfscl_freeopen(lop, 1, true);
 4223                         return (0);
 4224                 }
 4225         }
 4226 
 4227         /* No appropriate open, so we have to do one against the server. */
 4228         np = VTONFS(vp);
 4229         nop = malloc(sizeof (struct nfsclopen) +
 4230             lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
 4231         newone = 0;
 4232         nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
 4233             lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
 4234         ndp = dp;
 4235         error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
 4236             lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
 4237             NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
 4238         if (error) {
 4239                 if (newone)
 4240                         nfscl_freeopen(op, 0, true);
 4241         } else {
 4242                 op->nfso_mode |= lop->nfso_mode;
 4243                 op->nfso_opencnt += lop->nfso_opencnt;
 4244                 nfscl_freeopen(lop, 1, true);
 4245         }
 4246         if (nop != NULL)
 4247                 free(nop, M_NFSCLOPEN);
 4248         if (ndp != NULL) {
 4249                 /*
 4250                  * What should I do with the returned delegation, since the
 4251                  * delegation is being recalled? For now, just printf and
 4252                  * through it away.
 4253                  */
 4254                 printf("Moveopen returned deleg\n");
 4255                 free(ndp, M_NFSCLDELEG);
 4256         }
 4257         return (error);
 4258 }
 4259 
 4260 /*
 4261  * Recall all delegations on this client.
 4262  */
 4263 static void
 4264 nfscl_totalrecall(struct nfsclclient *clp)
 4265 {
 4266         struct nfscldeleg *dp;
 4267 
 4268         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
 4269                 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
 4270                         dp->nfsdl_flags |= NFSCLDL_RECALL;
 4271         }
 4272 }
 4273 
 4274 /*
 4275  * Relock byte ranges. Called for delegation recall and state expiry.
 4276  */
 4277 static int
 4278 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
 4279     struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
 4280     NFSPROC_T *p)
 4281 {
 4282         struct nfscllockowner *nlp;
 4283         struct nfsfh *nfhp;
 4284         u_int64_t off, len;
 4285         int error, newone, donelocally;
 4286 
 4287         off = lop->nfslo_first;
 4288         len = lop->nfslo_end - lop->nfslo_first;
 4289         error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
 4290             clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
 4291             lp->nfsl_openowner, &nlp, &newone, &donelocally);
 4292         if (error || donelocally)
 4293                 return (error);
 4294         nfhp = VTONFS(vp)->n_fhp;
 4295         error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
 4296             nfhp->nfh_len, nlp, newone, 0, off,
 4297             len, lop->nfslo_type, cred, p);
 4298         if (error)
 4299                 nfscl_freelockowner(nlp, 0);
 4300         return (error);
 4301 }
 4302 
 4303 /*
 4304  * Called to re-open a file. Basically get a vnode for the file handle
 4305  * and then call nfsrpc_openrpc() to do the rest.
 4306  */
 4307 static int
 4308 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
 4309     u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
 4310     struct ucred *cred, NFSPROC_T *p)
 4311 {
 4312         struct nfsnode *np;
 4313         vnode_t vp;
 4314         int error;
 4315 
 4316         error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
 4317         if (error)
 4318                 return (error);
 4319         vp = NFSTOV(np);
 4320         if (np->n_v4 != NULL) {
 4321                 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
 4322                     np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
 4323                     NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
 4324                     cred, p);
 4325         } else {
 4326                 error = EINVAL;
 4327         }
 4328         vrele(vp);
 4329         return (error);
 4330 }
 4331 
 4332 /*
 4333  * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
 4334  * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
 4335  * fail.
 4336  */
 4337 static int
 4338 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
 4339     u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
 4340     u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
 4341     int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
 4342 {
 4343         int error;
 4344 
 4345         do {
 4346                 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
 4347                     mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
 4348                     0, 0);
 4349                 if (error == NFSERR_DELAY)
 4350                         (void) nfs_catnap(PZERO, error, "nfstryop");
 4351         } while (error == NFSERR_DELAY);
 4352         if (error == EAUTH || error == EACCES) {
 4353                 /* Try again using system credentials */
 4354                 newnfs_setroot(cred);
 4355                 do {
 4356                     error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
 4357                         newfhlen, mode, op, name, namelen, ndpp, reclaim,
 4358                         delegtype, cred, p, 1, 0);
 4359                     if (error == NFSERR_DELAY)
 4360                         (void) nfs_catnap(PZERO, error, "nfstryop");
 4361                 } while (error == NFSERR_DELAY);
 4362         }
 4363         return (error);
 4364 }
 4365 
 4366 /*
 4367  * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
 4368  * NFSERR_DELAY. Also, retry with system credentials, if the provided
 4369  * cred don't work.
 4370  */
 4371 static int
 4372 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
 4373     int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
 4374     u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
 4375 {
 4376         struct nfsrv_descript nfsd, *nd = &nfsd;
 4377         int error;
 4378 
 4379         do {
 4380                 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
 4381                     reclaim, off, len, type, cred, p, 0);
 4382                 if (!error && nd->nd_repstat == NFSERR_DELAY)
 4383                         (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
 4384                             "nfstrylck");
 4385         } while (!error && nd->nd_repstat == NFSERR_DELAY);
 4386         if (!error)
 4387                 error = nd->nd_repstat;
 4388         if (error == EAUTH || error == EACCES) {
 4389                 /* Try again using root credentials */
 4390                 newnfs_setroot(cred);
 4391                 do {
 4392                         error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
 4393                             newone, reclaim, off, len, type, cred, p, 1);
 4394                         if (!error && nd->nd_repstat == NFSERR_DELAY)
 4395                                 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
 4396                                     "nfstrylck");
 4397                 } while (!error && nd->nd_repstat == NFSERR_DELAY);
 4398                 if (!error)
 4399                         error = nd->nd_repstat;
 4400         }
 4401         return (error);
 4402 }
 4403 
 4404 /*
 4405  * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
 4406  * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
 4407  * credentials fail.
 4408  */
 4409 static int
 4410 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
 4411     struct nfsmount *nmp, NFSPROC_T *p)
 4412 {
 4413         int error;
 4414 
 4415         do {
 4416                 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
 4417                 if (error == NFSERR_DELAY)
 4418                         (void) nfs_catnap(PZERO, error, "nfstrydp");
 4419         } while (error == NFSERR_DELAY);
 4420         if (error == EAUTH || error == EACCES) {
 4421                 /* Try again using system credentials */
 4422                 newnfs_setroot(cred);
 4423                 do {
 4424                         error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
 4425                         if (error == NFSERR_DELAY)
 4426                                 (void) nfs_catnap(PZERO, error, "nfstrydp");
 4427                 } while (error == NFSERR_DELAY);
 4428         }
 4429         return (error);
 4430 }
 4431 
 4432 /*
 4433  * Try a close against the server. Just call nfsrpc_closerpc(),
 4434  * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
 4435  * credentials fail.
 4436  */
 4437 int
 4438 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
 4439     struct nfsmount *nmp, NFSPROC_T *p, bool loop_on_delayed)
 4440 {
 4441         struct nfsrv_descript nfsd, *nd = &nfsd;
 4442         int error;
 4443 
 4444         do {
 4445                 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
 4446                 if (loop_on_delayed && error == NFSERR_DELAY)
 4447                         (void) nfs_catnap(PZERO, error, "nfstrycl");
 4448         } while (loop_on_delayed && error == NFSERR_DELAY);
 4449         if (error == EAUTH || error == EACCES) {
 4450                 /* Try again using system credentials */
 4451                 newnfs_setroot(cred);
 4452                 do {
 4453                         error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
 4454                         if (loop_on_delayed && error == NFSERR_DELAY)
 4455                                 (void) nfs_catnap(PZERO, error, "nfstrycl");
 4456                 } while (loop_on_delayed && error == NFSERR_DELAY);
 4457         }
 4458         return (error);
 4459 }
 4460 
 4461 /*
 4462  * Decide if a delegation on a file permits close without flushing writes
 4463  * to the server. This might be a big performance win in some environments.
 4464  * (Not useful until the client does caching on local stable storage.)
 4465  */
 4466 int
 4467 nfscl_mustflush(vnode_t vp)
 4468 {
 4469         struct nfsclclient *clp;
 4470         struct nfscldeleg *dp;
 4471         struct nfsnode *np;
 4472         struct nfsmount *nmp;
 4473 
 4474         np = VTONFS(vp);
 4475         nmp = VFSTONFS(vnode_mount(vp));
 4476         if (!NFSHASNFSV4(nmp))
 4477                 return (1);
 4478         NFSLOCKCLSTATE();
 4479         clp = nfscl_findcl(nmp);
 4480         if (clp == NULL) {
 4481                 NFSUNLOCKCLSTATE();
 4482                 return (1);
 4483         }
 4484         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
 4485         if (dp != NULL && (dp->nfsdl_flags &
 4486             (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
 4487              NFSCLDL_WRITE &&
 4488             (dp->nfsdl_sizelimit >= np->n_size ||
 4489              !NFSHASSTRICT3530(nmp))) {
 4490                 NFSUNLOCKCLSTATE();
 4491                 return (0);
 4492         }
 4493         NFSUNLOCKCLSTATE();
 4494         return (1);
 4495 }
 4496 
 4497 /*
 4498  * See if a (write) delegation exists for this file.
 4499  */
 4500 int
 4501 nfscl_nodeleg(vnode_t vp, int writedeleg)
 4502 {
 4503         struct nfsclclient *clp;
 4504         struct nfscldeleg *dp;
 4505         struct nfsnode *np;
 4506         struct nfsmount *nmp;
 4507 
 4508         np = VTONFS(vp);
 4509         nmp = VFSTONFS(vnode_mount(vp));
 4510         if (!NFSHASNFSV4(nmp))
 4511                 return (1);
 4512         NFSLOCKCLSTATE();
 4513         clp = nfscl_findcl(nmp);
 4514         if (clp == NULL) {
 4515                 NFSUNLOCKCLSTATE();
 4516                 return (1);
 4517         }
 4518         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
 4519         if (dp != NULL &&
 4520             (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
 4521             (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
 4522              NFSCLDL_WRITE)) {
 4523                 NFSUNLOCKCLSTATE();
 4524                 return (0);
 4525         }
 4526         NFSUNLOCKCLSTATE();
 4527         return (1);
 4528 }
 4529 
 4530 /*
 4531  * Look for an associated delegation that should be DelegReturned.
 4532  */
 4533 int
 4534 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
 4535 {
 4536         struct nfsclclient *clp;
 4537         struct nfscldeleg *dp;
 4538         struct nfsclowner *owp;
 4539         struct nfscllockowner *lp;
 4540         struct nfsmount *nmp;
 4541         struct mount *mp;
 4542         struct ucred *cred;
 4543         struct nfsnode *np;
 4544         int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
 4545 
 4546         nmp = VFSTONFS(vnode_mount(vp));
 4547         if (NFSHASPNFS(nmp))
 4548                 return (retcnt);
 4549         np = VTONFS(vp);
 4550         mp = nmp->nm_mountp;
 4551         NFSLOCKCLSTATE();
 4552         /*
 4553          * Loop around waiting for:
 4554          * - outstanding I/O operations on delegations to complete
 4555          * - for a delegation on vp that has state, lock the client and
 4556          *   do a recall
 4557          * - return delegation with no state
 4558          */
 4559         while (1) {
 4560                 clp = nfscl_findcl(nmp);
 4561                 if (clp == NULL) {
 4562                         NFSUNLOCKCLSTATE();
 4563                         return (retcnt);
 4564                 }
 4565                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
 4566                     np->n_fhp->nfh_len);
 4567                 if (dp != NULL) {
 4568                     /*
 4569                      * Wait for outstanding I/O ops to be done.
 4570                      */
 4571                     if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
 4572                         if (igotlock) {
 4573                             nfsv4_unlock(&clp->nfsc_lock, 0);
 4574                             igotlock = 0;
 4575                         }
 4576                         dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
 4577                         msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
 4578                             "nfscld", hz);
 4579                         if (NFSCL_FORCEDISM(mp)) {
 4580                             dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
 4581                             NFSUNLOCKCLSTATE();
 4582                             return (0);
 4583                         }
 4584                         continue;
 4585                     }
 4586                     needsrecall = 0;
 4587                     LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
 4588                         if (!LIST_EMPTY(&owp->nfsow_open)) {
 4589                             needsrecall = 1;
 4590                             break;
 4591                         }
 4592                     }
 4593                     if (!needsrecall) {
 4594                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
 4595                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
 4596                                 needsrecall = 1;
 4597                                 break;
 4598                             }
 4599                         }
 4600                     }
 4601                     if (needsrecall && !triedrecall) {
 4602                         dp->nfsdl_flags |= NFSCLDL_DELEGRET;
 4603                         islept = 0;
 4604                         while (!igotlock) {
 4605                             igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
 4606                                 &islept, NFSCLSTATEMUTEXPTR, mp);
 4607                             if (NFSCL_FORCEDISM(mp)) {
 4608                                 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
 4609                                 if (igotlock)
 4610                                     nfsv4_unlock(&clp->nfsc_lock, 0);
 4611                                 NFSUNLOCKCLSTATE();
 4612                                 return (0);
 4613                             }
 4614                             if (islept)
 4615                                 break;
 4616                         }
 4617                         if (islept)
 4618                             continue;
 4619                         NFSUNLOCKCLSTATE();
 4620                         cred = newnfs_getcred();
 4621                         newnfs_copycred(&dp->nfsdl_cred, cred);
 4622                         nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL);
 4623                         NFSFREECRED(cred);
 4624                         triedrecall = 1;
 4625                         NFSLOCKCLSTATE();
 4626                         nfsv4_unlock(&clp->nfsc_lock, 0);
 4627                         igotlock = 0;
 4628                         continue;
 4629                     }
 4630                     *stp = dp->nfsdl_stateid;
 4631                     retcnt = 1;
 4632                     nfscl_cleandeleg(dp);
 4633                     nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
 4634                 }
 4635                 if (igotlock)
 4636                     nfsv4_unlock(&clp->nfsc_lock, 0);
 4637                 NFSUNLOCKCLSTATE();
 4638                 return (retcnt);
 4639         }
 4640 }
 4641 
 4642 /*
 4643  * Look for associated delegation(s) that should be DelegReturned.
 4644  */
 4645 int
 4646 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
 4647     nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
 4648 {
 4649         struct nfsclclient *clp;
 4650         struct nfscldeleg *dp;
 4651         struct nfsclowner *owp;
 4652         struct nfscllockowner *lp;
 4653         struct nfsmount *nmp;
 4654         struct mount *mp;
 4655         struct ucred *cred;
 4656         struct nfsnode *np;
 4657         int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
 4658 
 4659         nmp = VFSTONFS(vnode_mount(fvp));
 4660         *gotfdp = 0;
 4661         *gottdp = 0;
 4662         if (NFSHASPNFS(nmp))
 4663                 return (retcnt);
 4664         mp = nmp->nm_mountp;
 4665         NFSLOCKCLSTATE();
 4666         /*
 4667          * Loop around waiting for:
 4668          * - outstanding I/O operations on delegations to complete
 4669          * - for a delegation on fvp that has state, lock the client and
 4670          *   do a recall
 4671          * - return delegation(s) with no state.
 4672          */
 4673         while (1) {
 4674                 clp = nfscl_findcl(nmp);
 4675                 if (clp == NULL) {
 4676                         NFSUNLOCKCLSTATE();
 4677                         return (retcnt);
 4678                 }
 4679                 np = VTONFS(fvp);
 4680                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
 4681                     np->n_fhp->nfh_len);
 4682                 if (dp != NULL && *gotfdp == 0) {
 4683                     /*
 4684                      * Wait for outstanding I/O ops to be done.
 4685                      */
 4686                     if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
 4687                         if (igotlock) {
 4688                             nfsv4_unlock(&clp->nfsc_lock, 0);
 4689                             igotlock = 0;
 4690                         }
 4691                         dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
 4692                         msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
 4693                             "nfscld", hz);
 4694                         if (NFSCL_FORCEDISM(mp)) {
 4695                             dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
 4696                             NFSUNLOCKCLSTATE();
 4697                             *gotfdp = 0;
 4698                             *gottdp = 0;
 4699                             return (0);
 4700                         }
 4701                         continue;
 4702                     }
 4703                     needsrecall = 0;
 4704                     LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
 4705                         if (!LIST_EMPTY(&owp->nfsow_open)) {
 4706                             needsrecall = 1;
 4707                             break;
 4708                         }
 4709                     }
 4710                     if (!needsrecall) {
 4711                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
 4712                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
 4713                                 needsrecall = 1;
 4714                                 break;
 4715                             }
 4716                         }
 4717                     }
 4718                     if (needsrecall && !triedrecall) {
 4719                         dp->nfsdl_flags |= NFSCLDL_DELEGRET;
 4720                         islept = 0;
 4721                         while (!igotlock) {
 4722                             igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
 4723                                 &islept, NFSCLSTATEMUTEXPTR, mp);
 4724                             if (NFSCL_FORCEDISM(mp)) {
 4725                                 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
 4726                                 if (igotlock)
 4727                                     nfsv4_unlock(&clp->nfsc_lock, 0);
 4728                                 NFSUNLOCKCLSTATE();
 4729                                 *gotfdp = 0;
 4730                                 *gottdp = 0;
 4731                                 return (0);
 4732                             }
 4733                             if (islept)
 4734                                 break;
 4735                         }
 4736                         if (islept)
 4737                             continue;
 4738                         NFSUNLOCKCLSTATE();
 4739                         cred = newnfs_getcred();
 4740                         newnfs_copycred(&dp->nfsdl_cred, cred);
 4741                         nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL);
 4742                         NFSFREECRED(cred);
 4743                         triedrecall = 1;
 4744                         NFSLOCKCLSTATE();
 4745                         nfsv4_unlock(&clp->nfsc_lock, 0);
 4746                         igotlock = 0;
 4747                         continue;
 4748                     }
 4749                     *fstp = dp->nfsdl_stateid;
 4750                     retcnt++;
 4751                     *gotfdp = 1;
 4752                     nfscl_cleandeleg(dp);
 4753                     nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
 4754                 }
 4755                 if (igotlock) {
 4756                     nfsv4_unlock(&clp->nfsc_lock, 0);
 4757                     igotlock = 0;
 4758                 }
 4759                 if (tvp != NULL) {
 4760                     np = VTONFS(tvp);
 4761                     dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
 4762                         np->n_fhp->nfh_len);
 4763                     if (dp != NULL && *gottdp == 0) {
 4764                         /*
 4765                          * Wait for outstanding I/O ops to be done.
 4766                          */
 4767                         if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
 4768                             dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
 4769                             msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
 4770                                 "nfscld", hz);
 4771                             if (NFSCL_FORCEDISM(mp)) {
 4772                                 NFSUNLOCKCLSTATE();
 4773                                 *gotfdp = 0;
 4774                                 *gottdp = 0;
 4775                                 return (0);
 4776                             }
 4777                             continue;
 4778                         }
 4779                         LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
 4780                             if (!LIST_EMPTY(&owp->nfsow_open)) {
 4781                                 NFSUNLOCKCLSTATE();
 4782                                 return (retcnt);
 4783                             }
 4784                         }
 4785                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
 4786                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
 4787                                 NFSUNLOCKCLSTATE();
 4788                                 return (retcnt);
 4789                             }
 4790                         }
 4791                         *tstp = dp->nfsdl_stateid;
 4792                         retcnt++;
 4793                         *gottdp = 1;
 4794                         nfscl_cleandeleg(dp);
 4795                         nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
 4796                     }
 4797                 }
 4798                 NFSUNLOCKCLSTATE();
 4799                 return (retcnt);
 4800         }
 4801 }
 4802 
 4803 /*
 4804  * Get a reference on the clientid associated with the mount point.
 4805  * Return 1 if success, 0 otherwise.
 4806  */
 4807 int
 4808 nfscl_getref(struct nfsmount *nmp)
 4809 {
 4810         struct nfsclclient *clp;
 4811         int ret;
 4812 
 4813         NFSLOCKCLSTATE();
 4814         clp = nfscl_findcl(nmp);
 4815         if (clp == NULL) {
 4816                 NFSUNLOCKCLSTATE();
 4817                 return (0);
 4818         }
 4819         nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, nmp->nm_mountp);
 4820         ret = 1;
 4821         if (NFSCL_FORCEDISM(nmp->nm_mountp))
 4822                 ret = 0;
 4823         NFSUNLOCKCLSTATE();
 4824         return (ret);
 4825 }
 4826 
 4827 /*
 4828  * Release a reference on a clientid acquired with the above call.
 4829  */
 4830 void
 4831 nfscl_relref(struct nfsmount *nmp)
 4832 {
 4833         struct nfsclclient *clp;
 4834 
 4835         NFSLOCKCLSTATE();
 4836         clp = nfscl_findcl(nmp);
 4837         if (clp == NULL) {
 4838                 NFSUNLOCKCLSTATE();
 4839                 return;
 4840         }
 4841         nfsv4_relref(&clp->nfsc_lock);
 4842         NFSUNLOCKCLSTATE();
 4843 }
 4844 
 4845 /*
 4846  * Save the size attribute in the delegation, since the nfsnode
 4847  * is going away.
 4848  */
 4849 void
 4850 nfscl_reclaimnode(vnode_t vp)
 4851 {
 4852         struct nfsclclient *clp;
 4853         struct nfscldeleg *dp;
 4854         struct nfsnode *np = VTONFS(vp);
 4855         struct nfsmount *nmp;
 4856 
 4857         nmp = VFSTONFS(vnode_mount(vp));
 4858         if (!NFSHASNFSV4(nmp))
 4859                 return;
 4860         NFSLOCKCLSTATE();
 4861         clp = nfscl_findcl(nmp);
 4862         if (clp == NULL) {
 4863                 NFSUNLOCKCLSTATE();
 4864                 return;
 4865         }
 4866         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
 4867         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
 4868                 dp->nfsdl_size = np->n_size;
 4869         NFSUNLOCKCLSTATE();
 4870 }
 4871 
 4872 /*
 4873  * Get the saved size attribute in the delegation, since it is a
 4874  * newly allocated nfsnode.
 4875  */
 4876 void
 4877 nfscl_newnode(vnode_t vp)
 4878 {
 4879         struct nfsclclient *clp;
 4880         struct nfscldeleg *dp;
 4881         struct nfsnode *np = VTONFS(vp);
 4882         struct nfsmount *nmp;
 4883 
 4884         nmp = VFSTONFS(vnode_mount(vp));
 4885         if (!NFSHASNFSV4(nmp))
 4886                 return;
 4887         NFSLOCKCLSTATE();
 4888         clp = nfscl_findcl(nmp);
 4889         if (clp == NULL) {
 4890                 NFSUNLOCKCLSTATE();
 4891                 return;
 4892         }
 4893         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
 4894         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
 4895                 np->n_size = dp->nfsdl_size;
 4896         NFSUNLOCKCLSTATE();
 4897 }
 4898 
 4899 /*
 4900  * If there is a valid write delegation for this file, set the modtime
 4901  * to the local clock time.
 4902  */
 4903 void
 4904 nfscl_delegmodtime(vnode_t vp)
 4905 {
 4906         struct nfsclclient *clp;
 4907         struct nfscldeleg *dp;
 4908         struct nfsnode *np = VTONFS(vp);
 4909         struct nfsmount *nmp;
 4910 
 4911         nmp = VFSTONFS(vnode_mount(vp));
 4912         if (!NFSHASNFSV4(nmp))
 4913                 return;
 4914         NFSLOCKCLSTATE();
 4915         clp = nfscl_findcl(nmp);
 4916         if (clp == NULL) {
 4917                 NFSUNLOCKCLSTATE();
 4918                 return;
 4919         }
 4920         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
 4921         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
 4922                 nanotime(&dp->nfsdl_modtime);
 4923                 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
 4924         }
 4925         NFSUNLOCKCLSTATE();
 4926 }
 4927 
 4928 /*
 4929  * If there is a valid write delegation for this file with a modtime set,
 4930  * put that modtime in mtime.
 4931  */
 4932 void
 4933 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
 4934 {
 4935         struct nfsclclient *clp;
 4936         struct nfscldeleg *dp;
 4937         struct nfsnode *np = VTONFS(vp);
 4938         struct nfsmount *nmp;
 4939 
 4940         nmp = VFSTONFS(vnode_mount(vp));
 4941         if (!NFSHASNFSV4(nmp))
 4942                 return;
 4943         NFSLOCKCLSTATE();
 4944         clp = nfscl_findcl(nmp);
 4945         if (clp == NULL) {
 4946                 NFSUNLOCKCLSTATE();
 4947                 return;
 4948         }
 4949         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
 4950         if (dp != NULL &&
 4951             (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
 4952             (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
 4953                 *mtime = dp->nfsdl_modtime;
 4954         NFSUNLOCKCLSTATE();
 4955 }
 4956 
 4957 static int
 4958 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
 4959 {
 4960         short *defaulterrp, *errp;
 4961 
 4962         if (!nd->nd_repstat)
 4963                 return (0);
 4964         if (nd->nd_procnum == NFSPROC_NOOP)
 4965                 return (txdr_unsigned(nd->nd_repstat & 0xffff));
 4966         if (nd->nd_repstat == EBADRPC)
 4967                 return (txdr_unsigned(NFSERR_BADXDR));
 4968         if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
 4969             nd->nd_repstat == NFSERR_OPILLEGAL)
 4970                 return (txdr_unsigned(nd->nd_repstat));
 4971         if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
 4972             minorvers > NFSV4_MINORVERSION) {
 4973                 /* NFSv4.n error. */
 4974                 return (txdr_unsigned(nd->nd_repstat));
 4975         }
 4976         if (nd->nd_procnum < NFSV4OP_CBNOPS)
 4977                 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
 4978         else
 4979                 return (txdr_unsigned(nd->nd_repstat));
 4980         while (*++errp)
 4981                 if (*errp == (short)nd->nd_repstat)
 4982                         return (txdr_unsigned(nd->nd_repstat));
 4983         return (txdr_unsigned(*defaulterrp));
 4984 }
 4985 
 4986 /*
 4987  * Called to find/add a layout to a client.
 4988  * This function returns the layout with a refcnt (shared lock) upon
 4989  * success (returns 0) or with no lock/refcnt on the layout when an
 4990  * error is returned.
 4991  * If a layout is passed in via lypp, it is locked (exclusively locked).
 4992  */
 4993 int
 4994 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
 4995     nfsv4stateid_t *stateidp, int layouttype, int retonclose,
 4996     struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
 4997     struct ucred *cred, NFSPROC_T *p)
 4998 {
 4999         struct nfsclclient *clp;
 5000         struct nfscllayout *lyp, *tlyp;
 5001         struct nfsclflayout *flp;
 5002         struct nfsnode *np = VTONFS(vp);
 5003         mount_t mp;
 5004         int layout_passed_in;
 5005 
 5006         mp = nmp->nm_mountp;
 5007         layout_passed_in = 1;
 5008         tlyp = NULL;
 5009         lyp = *lypp;
 5010         if (lyp == NULL) {
 5011                 layout_passed_in = 0;
 5012                 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
 5013                     M_WAITOK | M_ZERO);
 5014         }
 5015 
 5016         NFSLOCKCLSTATE();
 5017         clp = nmp->nm_clp;
 5018         if (clp == NULL) {
 5019                 if (layout_passed_in != 0)
 5020                         nfsv4_unlock(&lyp->nfsly_lock, 0);
 5021                 NFSUNLOCKCLSTATE();
 5022                 if (tlyp != NULL)
 5023                         free(tlyp, M_NFSLAYOUT);
 5024                 return (EPERM);
 5025         }
 5026         if (lyp == NULL) {
 5027                 /*
 5028                  * Although no lyp was passed in, another thread might have
 5029                  * allocated one. If one is found, just increment it's ref
 5030                  * count and return it.
 5031                  */
 5032                 lyp = nfscl_findlayout(clp, fhp, fhlen);
 5033                 if (lyp == NULL) {
 5034                         lyp = tlyp;
 5035                         tlyp = NULL;
 5036                         lyp->nfsly_stateid.seqid = stateidp->seqid;
 5037                         lyp->nfsly_stateid.other[0] = stateidp->other[0];
 5038                         lyp->nfsly_stateid.other[1] = stateidp->other[1];
 5039                         lyp->nfsly_stateid.other[2] = stateidp->other[2];
 5040                         lyp->nfsly_lastbyte = 0;
 5041                         LIST_INIT(&lyp->nfsly_flayread);
 5042                         LIST_INIT(&lyp->nfsly_flayrw);
 5043                         LIST_INIT(&lyp->nfsly_recall);
 5044                         lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
 5045                         lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
 5046                         lyp->nfsly_clp = clp;
 5047                         if (layouttype == NFSLAYOUT_FLEXFILE)
 5048                                 lyp->nfsly_flags = NFSLY_FLEXFILE;
 5049                         else
 5050                                 lyp->nfsly_flags = NFSLY_FILES;
 5051                         if (retonclose != 0)
 5052                                 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
 5053                         lyp->nfsly_fhlen = fhlen;
 5054                         NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
 5055                         TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
 5056                         LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
 5057                             nfsly_hash);
 5058                         lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
 5059                         nfscl_layoutcnt++;
 5060                 } else {
 5061                         if (retonclose != 0)
 5062                                 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
 5063                         if (stateidp->seqid > lyp->nfsly_stateid.seqid)
 5064                                 lyp->nfsly_stateid.seqid = stateidp->seqid;
 5065                         TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
 5066                         TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
 5067                         lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
 5068                 }
 5069                 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
 5070                 if (NFSCL_FORCEDISM(mp)) {
 5071                         NFSUNLOCKCLSTATE();
 5072                         if (tlyp != NULL)
 5073                                 free(tlyp, M_NFSLAYOUT);
 5074                         return (EPERM);
 5075                 }
 5076                 *lypp = lyp;
 5077         } else if (stateidp->seqid > lyp->nfsly_stateid.seqid)
 5078                 lyp->nfsly_stateid.seqid = stateidp->seqid;
 5079 
 5080         /* Merge the new list of File Layouts into the list. */
 5081         flp = LIST_FIRST(fhlp);
 5082         if (flp != NULL) {
 5083                 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
 5084                         nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
 5085                 else
 5086                         nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
 5087         }
 5088         if (layout_passed_in != 0)
 5089                 nfsv4_unlock(&lyp->nfsly_lock, 1);
 5090         NFSUNLOCKCLSTATE();
 5091         if (tlyp != NULL)
 5092                 free(tlyp, M_NFSLAYOUT);
 5093         return (0);
 5094 }
 5095 
 5096 /*
 5097  * Search for a layout by MDS file handle.
 5098  * If one is found, it is returned with a refcnt (shared lock) iff
 5099  * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
 5100  * returned NULL.
 5101  */
 5102 struct nfscllayout *
 5103 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
 5104     uint64_t off, uint32_t rwaccess, struct nfsclflayout **retflpp,
 5105     int *recalledp)
 5106 {
 5107         struct nfscllayout *lyp;
 5108         mount_t mp;
 5109         int error, igotlock;
 5110 
 5111         mp = clp->nfsc_nmp->nm_mountp;
 5112         *recalledp = 0;
 5113         *retflpp = NULL;
 5114         NFSLOCKCLSTATE();
 5115         lyp = nfscl_findlayout(clp, fhp, fhlen);
 5116         if (lyp != NULL) {
 5117                 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
 5118                         TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
 5119                         TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
 5120                         lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
 5121                         error = nfscl_findlayoutforio(lyp, off, rwaccess,
 5122                             retflpp);
 5123                         if (error == 0)
 5124                                 nfsv4_getref(&lyp->nfsly_lock, NULL,
 5125                                     NFSCLSTATEMUTEXPTR, mp);
 5126                         else {
 5127                                 do {
 5128                                         igotlock = nfsv4_lock(&lyp->nfsly_lock,
 5129                                             1, NULL, NFSCLSTATEMUTEXPTR, mp);
 5130                                 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
 5131                                 *retflpp = NULL;
 5132                         }
 5133                         if (NFSCL_FORCEDISM(mp)) {
 5134                                 lyp = NULL;
 5135                                 *recalledp = 1;
 5136                         }
 5137                 } else {
 5138                         lyp = NULL;
 5139                         *recalledp = 1;
 5140                 }
 5141         }
 5142         NFSUNLOCKCLSTATE();
 5143         return (lyp);
 5144 }
 5145 
 5146 /*
 5147  * Search for a layout by MDS file handle. If one is found, mark in to be
 5148  * recalled, if it already marked "return on close".
 5149  */
 5150 static void
 5151 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
 5152     int fhlen, struct nfsclrecalllayout **recallpp, struct nfscllayout **lypp)
 5153 {
 5154         struct nfscllayout *lyp;
 5155         uint32_t iomode;
 5156 
 5157         *lypp = NULL;
 5158         if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vnode_mount(vp))) ||
 5159             nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
 5160             (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
 5161                 return;
 5162         lyp = nfscl_findlayout(clp, fhp, fhlen);
 5163         if (lyp != NULL && (lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) {
 5164                 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
 5165                         iomode = 0;
 5166                         if (!LIST_EMPTY(&lyp->nfsly_flayread))
 5167                                 iomode |= NFSLAYOUTIOMODE_READ;
 5168                         if (!LIST_EMPTY(&lyp->nfsly_flayrw))
 5169                                 iomode |= NFSLAYOUTIOMODE_RW;
 5170                         nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
 5171                             0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
 5172                             *recallpp);
 5173                         NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
 5174                         *recallpp = NULL;
 5175                 }
 5176 
 5177                 /* Now, wake up renew thread to do LayoutReturn. */
 5178                 wakeup(clp);
 5179                 *lypp = lyp;
 5180         }
 5181 }
 5182 
 5183 /*
 5184  * Mark the layout to be recalled and with an error.
 5185  * Also, disable the dsp from further use.
 5186  */
 5187 void
 5188 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
 5189     struct nfscllayout *lyp, struct nfsclds *dsp)
 5190 {
 5191         struct nfsclrecalllayout *recallp;
 5192         uint32_t iomode;
 5193 
 5194         printf("DS being disabled, error=%d\n", stat);
 5195         /* Set up the return of the layout. */
 5196         recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
 5197         iomode = 0;
 5198         NFSLOCKCLSTATE();
 5199         if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
 5200                 if (!LIST_EMPTY(&lyp->nfsly_flayread))
 5201                         iomode |= NFSLAYOUTIOMODE_READ;
 5202                 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
 5203                         iomode |= NFSLAYOUTIOMODE_RW;
 5204                 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
 5205                     0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
 5206                     dp->nfsdi_deviceid, recallp);
 5207                 NFSUNLOCKCLSTATE();
 5208                 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
 5209         } else {
 5210                 NFSUNLOCKCLSTATE();
 5211                 free(recallp, M_NFSLAYRECALL);
 5212         }
 5213 
 5214         /* And shut the TCP connection down. */
 5215         nfscl_cancelreqs(dsp);
 5216 }
 5217 
 5218 /*
 5219  * Cancel all RPCs for this "dsp" by closing the connection.
 5220  * Also, mark the session as defunct.
 5221  * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
 5222  * cannot be shut down.
 5223  */
 5224 void
 5225 nfscl_cancelreqs(struct nfsclds *dsp)
 5226 {
 5227         struct __rpc_client *cl;
 5228         static int non_event;
 5229 
 5230         NFSLOCKDS(dsp);
 5231         if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
 5232             dsp->nfsclds_sockp != NULL &&
 5233             dsp->nfsclds_sockp->nr_client != NULL) {
 5234                 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
 5235                 cl = dsp->nfsclds_sockp->nr_client;
 5236                 dsp->nfsclds_sess.nfsess_defunct = 1;
 5237                 NFSUNLOCKDS(dsp);
 5238                 CLNT_CLOSE(cl);
 5239                 /*
 5240                  * This 1sec sleep is done to reduce the number of reconnect
 5241                  * attempts made on the DS while it has failed.
 5242                  */
 5243                 tsleep(&non_event, PVFS, "ndscls", hz);
 5244                 return;
 5245         }
 5246         NFSUNLOCKDS(dsp);
 5247 }
 5248 
 5249 /*
 5250  * Dereference a layout.
 5251  */
 5252 void
 5253 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
 5254 {
 5255 
 5256         NFSLOCKCLSTATE();
 5257         if (exclocked != 0)
 5258                 nfsv4_unlock(&lyp->nfsly_lock, 0);
 5259         else
 5260                 nfsv4_relref(&lyp->nfsly_lock);
 5261         NFSUNLOCKCLSTATE();
 5262 }
 5263 
 5264 /*
 5265  * Search for a devinfo by deviceid. If one is found, return it after
 5266  * acquiring a reference count on it.
 5267  */
 5268 struct nfscldevinfo *
 5269 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
 5270     struct nfscldevinfo *dip)
 5271 {
 5272 
 5273         NFSLOCKCLSTATE();
 5274         if (dip == NULL)
 5275                 dip = nfscl_finddevinfo(clp, deviceid);
 5276         if (dip != NULL)
 5277                 dip->nfsdi_refcnt++;
 5278         NFSUNLOCKCLSTATE();
 5279         return (dip);
 5280 }
 5281 
 5282 /*
 5283  * Dereference a devinfo structure.
 5284  */
 5285 static void
 5286 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
 5287 {
 5288 
 5289         dip->nfsdi_refcnt--;
 5290         if (dip->nfsdi_refcnt == 0)
 5291                 wakeup(&dip->nfsdi_refcnt);
 5292 }
 5293 
 5294 /*
 5295  * Dereference a devinfo structure.
 5296  */
 5297 void
 5298 nfscl_reldevinfo(struct nfscldevinfo *dip)
 5299 {
 5300 
 5301         NFSLOCKCLSTATE();
 5302         nfscl_reldevinfo_locked(dip);
 5303         NFSUNLOCKCLSTATE();
 5304 }
 5305 
 5306 /*
 5307  * Find a layout for this file handle. Return NULL upon failure.
 5308  */
 5309 static struct nfscllayout *
 5310 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
 5311 {
 5312         struct nfscllayout *lyp;
 5313 
 5314         LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
 5315                 if (lyp->nfsly_fhlen == fhlen &&
 5316                     !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
 5317                         break;
 5318         return (lyp);
 5319 }
 5320 
 5321 /*
 5322  * Find a devinfo for this deviceid. Return NULL upon failure.
 5323  */
 5324 static struct nfscldevinfo *
 5325 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
 5326 {
 5327         struct nfscldevinfo *dip;
 5328 
 5329         LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
 5330                 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
 5331                     == 0)
 5332                         break;
 5333         return (dip);
 5334 }
 5335 
 5336 /*
 5337  * Merge the new file layout list into the main one, maintaining it in
 5338  * increasing offset order.
 5339  */
 5340 static void
 5341 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
 5342     struct nfsclflayouthead *newfhlp)
 5343 {
 5344         struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
 5345 
 5346         flp = LIST_FIRST(fhlp);
 5347         prevflp = NULL;
 5348         LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
 5349                 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
 5350                         prevflp = flp;
 5351                         flp = LIST_NEXT(flp, nfsfl_list);
 5352                 }
 5353                 if (prevflp == NULL)
 5354                         LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
 5355                 else
 5356                         LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
 5357                 prevflp = nflp;
 5358         }
 5359 }
 5360 
 5361 /*
 5362  * Add this nfscldevinfo to the client, if it doesn't already exist.
 5363  * This function consumes the structure pointed at by dip, if not NULL.
 5364  */
 5365 int
 5366 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
 5367     struct nfsclflayout *flp)
 5368 {
 5369         struct