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/nfsserver/nfs_nfsdstate.c

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

    1 /*-
    2  * Copyright (c) 2009 Rick Macklem, University of Guelph
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD$");
   30 
   31 #ifndef APPLEKEXT
   32 #include <fs/nfs/nfsport.h>
   33 
   34 struct nfsrv_stablefirst nfsrv_stablefirst;
   35 int nfsrv_issuedelegs = 0;
   36 int nfsrv_dolocallocks = 0;
   37 struct nfsv4lock nfsv4rootfs_lock;
   38 
   39 extern int newnfs_numnfsd;
   40 extern struct nfsstats newnfsstats;
   41 extern int nfsrv_lease;
   42 extern struct timeval nfsboottime;
   43 extern u_int32_t newnfs_true, newnfs_false;
   44 NFSV4ROOTLOCKMUTEX;
   45 NFSSTATESPINLOCK;
   46 
   47 /*
   48  * Hash lists for nfs V4.
   49  * (Some would put them in the .h file, but I don't like declaring storage
   50  *  in a .h)
   51  */
   52 struct nfsclienthashhead nfsclienthash[NFSCLIENTHASHSIZE];
   53 struct nfslockhashhead nfslockhash[NFSLOCKHASHSIZE];
   54 #endif  /* !APPLEKEXT */
   55 
   56 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
   57 static time_t nfsrvboottime;
   58 static int nfsrv_writedelegifpos = 1;
   59 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
   60 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
   61 static int nfsrv_nogsscallback = 0;
   62 
   63 /* local functions */
   64 static void nfsrv_dumpaclient(struct nfsclient *clp,
   65     struct nfsd_dumpclients *dumpp);
   66 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
   67     NFSPROC_T *p);
   68 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
   69     NFSPROC_T *p);
   70 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
   71     NFSPROC_T *p);
   72 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
   73     int cansleep, NFSPROC_T *p);
   74 static void nfsrv_freenfslock(struct nfslock *lop);
   75 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
   76 static void nfsrv_freedeleg(struct nfsstate *);
   77 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, 
   78     u_int32_t flags, struct nfsstate **stpp);
   79 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
   80     struct nfsstate **stpp);
   81 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
   82     struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
   83 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
   84     struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
   85 static void nfsrv_insertlock(struct nfslock *new_lop,
   86     struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
   87 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
   88     struct nfslock **other_lopp, struct nfslockfile *lfp);
   89 static int nfsrv_getipnumber(u_char *cp);
   90 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
   91     nfsv4stateid_t *stateidp, int specialid);
   92 static int nfsrv_checkgrace(u_int32_t flags);
   93 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
   94     nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
   95     struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p);
   96 static u_int32_t nfsrv_nextclientindex(void);
   97 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
   98 static void nfsrv_markstable(struct nfsclient *clp);
   99 static int nfsrv_checkstable(struct nfsclient *clp);
  100 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct 
  101     vnode *vp, NFSPROC_T *p);
  102 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
  103     NFSPROC_T *p, vnode_t vp);
  104 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
  105     struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
  106 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
  107     struct nfsclient *clp);
  108 static time_t nfsrv_leaseexpiry(void);
  109 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
  110 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
  111     struct nfsstate *stp, struct nfsrvcache *op);
  112 static int nfsrv_nootherstate(struct nfsstate *stp);
  113 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
  114     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
  115 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
  116     uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
  117 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
  118     int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
  119     NFSPROC_T *p);
  120 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
  121     NFSPROC_T *p);
  122 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
  123     uint64_t first, uint64_t end);
  124 static void nfsrv_locklf(struct nfslockfile *lfp);
  125 static void nfsrv_unlocklf(struct nfslockfile *lfp);
  126 
  127 /*
  128  * Scan the client list for a match and either return the current one,
  129  * create a new entry or return an error.
  130  * If returning a non-error, the clp structure must either be linked into
  131  * the client list or free'd.
  132  */
  133 APPLESTATIC int
  134 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
  135     nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
  136 {
  137         struct nfsclient *clp = NULL, *new_clp = *new_clpp;
  138         int i, error = 0;
  139         struct nfsstate *stp, *tstp;
  140         struct sockaddr_in *sad, *rad;
  141         int zapit = 0, gotit, hasstate = 0, igotlock;
  142         static u_int64_t confirm_index = 0;
  143 
  144         /*
  145          * Check for state resource limit exceeded.
  146          */
  147         if (nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
  148                 error = NFSERR_RESOURCE;
  149                 goto out;
  150         }
  151 
  152         if (nfsrv_issuedelegs == 0 ||
  153             ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
  154                 /*
  155                  * Don't do callbacks when delegations are disabled or
  156                  * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
  157                  * If establishing a callback connection is attempted
  158                  * when a firewall is blocking the callback path, the
  159                  * server may wait too long for the connect attempt to
  160                  * succeed during the Open. Some clients, such as Linux,
  161                  * may timeout and give up on the Open before the server
  162                  * replies. Also, since AUTH_GSS callbacks are not
  163                  * yet interoperability tested, they might cause the
  164                  * server to crap out, if they get past the Init call to
  165                  * the client.
  166                  */
  167                 new_clp->lc_program = 0;
  168 
  169         /* Lock out other nfsd threads */
  170         NFSLOCKV4ROOTMUTEX();
  171         nfsv4_relref(&nfsv4rootfs_lock);
  172         do {
  173                 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
  174                     NFSV4ROOTLOCKMUTEXPTR, NULL);
  175         } while (!igotlock);
  176         NFSUNLOCKV4ROOTMUTEX();
  177 
  178         /*
  179          * Search for a match in the client list.
  180          */
  181         gotit = i = 0;
  182         while (i < NFSCLIENTHASHSIZE && !gotit) {
  183             LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
  184                 if (new_clp->lc_idlen == clp->lc_idlen &&
  185                     !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
  186                         gotit = 1;
  187                         break;
  188                 }
  189             }
  190             if (gotit == 0)
  191                 i++;
  192         }
  193         if (!gotit ||
  194             (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
  195                 /*
  196                  * Get rid of the old one.
  197                  */
  198                 if (i != NFSCLIENTHASHSIZE) {
  199                         LIST_REMOVE(clp, lc_hash);
  200                         nfsrv_cleanclient(clp, p);
  201                         nfsrv_freedeleglist(&clp->lc_deleg);
  202                         nfsrv_freedeleglist(&clp->lc_olddeleg);
  203                         zapit = 1;
  204                 }
  205                 /*
  206                  * Add it after assigning a client id to it.
  207                  */
  208                 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
  209                 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
  210                 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
  211                     (u_int32_t)nfsrvboottime;
  212                 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
  213                     nfsrv_nextclientindex();
  214                 new_clp->lc_stateindex = 0;
  215                 new_clp->lc_statemaxindex = 0;
  216                 new_clp->lc_cbref = 0;
  217                 new_clp->lc_expiry = nfsrv_leaseexpiry();
  218                 LIST_INIT(&new_clp->lc_open);
  219                 LIST_INIT(&new_clp->lc_deleg);
  220                 LIST_INIT(&new_clp->lc_olddeleg);
  221                 for (i = 0; i < NFSSTATEHASHSIZE; i++)
  222                         LIST_INIT(&new_clp->lc_stateid[i]);
  223                 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
  224                     lc_hash);
  225                 newnfsstats.srvclients++;
  226                 nfsrv_openpluslock++;
  227                 nfsrv_clients++;
  228                 NFSLOCKV4ROOTMUTEX();
  229                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
  230                 NFSUNLOCKV4ROOTMUTEX();
  231                 if (zapit)
  232                         nfsrv_zapclient(clp, p);
  233                 *new_clpp = NULL;
  234                 goto out;
  235         }
  236 
  237         /*
  238          * Now, handle the cases where the id is already issued.
  239          */
  240         if (nfsrv_notsamecredname(nd, clp)) {
  241             /*
  242              * Check to see if there is expired state that should go away.
  243              */
  244             if (clp->lc_expiry < NFSD_MONOSEC &&
  245                 (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
  246                 nfsrv_cleanclient(clp, p);
  247                 nfsrv_freedeleglist(&clp->lc_deleg);
  248             }
  249 
  250             /*
  251              * If there is outstanding state, then reply NFSERR_CLIDINUSE per
  252              * RFC3530 Sec. 8.1.2 last para.
  253              */
  254             if (!LIST_EMPTY(&clp->lc_deleg)) {
  255                 hasstate = 1;
  256             } else if (LIST_EMPTY(&clp->lc_open)) {
  257                 hasstate = 0;
  258             } else {
  259                 hasstate = 0;
  260                 /* Look for an Open on the OpenOwner */
  261                 LIST_FOREACH(stp, &clp->lc_open, ls_list) {
  262                     if (!LIST_EMPTY(&stp->ls_open)) {
  263                         hasstate = 1;
  264                         break;
  265                     }
  266                 }
  267             }
  268             if (hasstate) {
  269                 /*
  270                  * If the uid doesn't match, return NFSERR_CLIDINUSE after
  271                  * filling out the correct ipaddr and portnum.
  272                  */
  273                 sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
  274                 rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
  275                 sad->sin_addr.s_addr = rad->sin_addr.s_addr;
  276                 sad->sin_port = rad->sin_port;
  277                 NFSLOCKV4ROOTMUTEX();
  278                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
  279                 NFSUNLOCKV4ROOTMUTEX();
  280                 error = NFSERR_CLIDINUSE;
  281                 goto out;
  282             }
  283         }
  284 
  285         if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
  286                 /*
  287                  * If the verifier has changed, the client has rebooted
  288                  * and a new client id is issued. The old state info
  289                  * can be thrown away once the SETCLIENTID_CONFIRM occurs.
  290                  */
  291                 LIST_REMOVE(clp, lc_hash);
  292                 new_clp->lc_flags |= LCL_NEEDSCONFIRM;
  293                 confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
  294                 clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
  295                     nfsrvboottime;
  296                 clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
  297                     nfsrv_nextclientindex();
  298                 new_clp->lc_stateindex = 0;
  299                 new_clp->lc_statemaxindex = 0;
  300                 new_clp->lc_cbref = 0;
  301                 new_clp->lc_expiry = nfsrv_leaseexpiry();
  302 
  303                 /*
  304                  * Save the state until confirmed.
  305                  */
  306                 LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
  307                 LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
  308                         tstp->ls_clp = new_clp;
  309                 LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
  310                 LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
  311                         tstp->ls_clp = new_clp;
  312                 LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
  313                     ls_list);
  314                 LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
  315                         tstp->ls_clp = new_clp;
  316                 for (i = 0; i < NFSSTATEHASHSIZE; i++) {
  317                         LIST_NEWHEAD(&new_clp->lc_stateid[i],
  318                             &clp->lc_stateid[i], ls_hash);
  319                         LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
  320                                 tstp->ls_clp = new_clp;
  321                 }
  322                 LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
  323                     lc_hash);
  324                 newnfsstats.srvclients++;
  325                 nfsrv_openpluslock++;
  326                 nfsrv_clients++;
  327                 NFSLOCKV4ROOTMUTEX();
  328                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
  329                 NFSUNLOCKV4ROOTMUTEX();
  330 
  331                 /*
  332                  * Must wait until any outstanding callback on the old clp
  333                  * completes.
  334                  */
  335                 NFSLOCKSTATE();
  336                 while (clp->lc_cbref) {
  337                         clp->lc_flags |= LCL_WAKEUPWANTED;
  338                         (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
  339                             "nfsd clp", 10 * hz);
  340                 }
  341                 NFSUNLOCKSTATE();
  342                 nfsrv_zapclient(clp, p);
  343                 *new_clpp = NULL;
  344                 goto out;
  345         }
  346         /*
  347          * id and verifier match, so update the net address info
  348          * and get rid of any existing callback authentication
  349          * handle, so a new one will be acquired.
  350          */
  351         LIST_REMOVE(clp, lc_hash);
  352         new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
  353         new_clp->lc_expiry = nfsrv_leaseexpiry();
  354         confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
  355         clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
  356             clp->lc_clientid.lval[0];
  357         clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
  358             clp->lc_clientid.lval[1];
  359         new_clp->lc_delegtime = clp->lc_delegtime;
  360         new_clp->lc_stateindex = clp->lc_stateindex;
  361         new_clp->lc_statemaxindex = clp->lc_statemaxindex;
  362         new_clp->lc_cbref = 0;
  363         LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
  364         LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
  365                 tstp->ls_clp = new_clp;
  366         LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
  367         LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
  368                 tstp->ls_clp = new_clp;
  369         LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
  370         LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
  371                 tstp->ls_clp = new_clp;
  372         for (i = 0; i < NFSSTATEHASHSIZE; i++) {
  373                 LIST_NEWHEAD(&new_clp->lc_stateid[i], &clp->lc_stateid[i],
  374                     ls_hash);
  375                 LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
  376                         tstp->ls_clp = new_clp;
  377         }
  378         LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
  379             lc_hash);
  380         newnfsstats.srvclients++;
  381         nfsrv_openpluslock++;
  382         nfsrv_clients++;
  383         NFSLOCKV4ROOTMUTEX();
  384         nfsv4_unlock(&nfsv4rootfs_lock, 1);
  385         NFSUNLOCKV4ROOTMUTEX();
  386 
  387         /*
  388          * Must wait until any outstanding callback on the old clp
  389          * completes.
  390          */
  391         NFSLOCKSTATE();
  392         while (clp->lc_cbref) {
  393                 clp->lc_flags |= LCL_WAKEUPWANTED;
  394                 (void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1, "nfsd clp",
  395                     10 * hz);
  396         }
  397         NFSUNLOCKSTATE();
  398         nfsrv_zapclient(clp, p);
  399         *new_clpp = NULL;
  400 
  401 out:
  402         NFSEXITCODE2(error, nd);
  403         return (error);
  404 }
  405 
  406 /*
  407  * Check to see if the client id exists and optionally confirm it.
  408  */
  409 APPLESTATIC int
  410 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
  411     nfsquad_t confirm, struct nfsrv_descript *nd, NFSPROC_T *p)
  412 {
  413         struct nfsclient *clp;
  414         struct nfsstate *stp;
  415         int i;
  416         struct nfsclienthashhead *hp;
  417         int error = 0, igotlock, doneok;
  418 
  419         if (clpp)
  420                 *clpp = NULL;
  421         if (nfsrvboottime != clientid.lval[0]) {
  422                 error = NFSERR_STALECLIENTID;
  423                 goto out;
  424         }
  425 
  426         /*
  427          * If called with opflags == CLOPS_RENEW, the State Lock is
  428          * already held. Otherwise, we need to get either that or,
  429          * for the case of Confirm, lock out the nfsd threads.
  430          */
  431         if (opflags & CLOPS_CONFIRM) {
  432                 NFSLOCKV4ROOTMUTEX();
  433                 nfsv4_relref(&nfsv4rootfs_lock);
  434                 do {
  435                         igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
  436                             NFSV4ROOTLOCKMUTEXPTR, NULL);
  437                 } while (!igotlock);
  438                 NFSUNLOCKV4ROOTMUTEX();
  439         } else if (opflags != CLOPS_RENEW) {
  440                 NFSLOCKSTATE();
  441         }
  442 
  443         hp = NFSCLIENTHASH(clientid);
  444         LIST_FOREACH(clp, hp, lc_hash) {
  445                 if (clp->lc_clientid.lval[1] == clientid.lval[1])
  446                         break;
  447         }
  448         if (clp == LIST_END(hp)) {
  449                 if (opflags & CLOPS_CONFIRM)
  450                         error = NFSERR_STALECLIENTID;
  451                 else
  452                         error = NFSERR_EXPIRED;
  453         } else if (clp->lc_flags & LCL_ADMINREVOKED) {
  454                 /*
  455                  * If marked admin revoked, just return the error.
  456                  */
  457                 error = NFSERR_ADMINREVOKED;
  458         }
  459         if (error) {
  460                 if (opflags & CLOPS_CONFIRM) {
  461                         NFSLOCKV4ROOTMUTEX();
  462                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
  463                         NFSUNLOCKV4ROOTMUTEX();
  464                 } else if (opflags != CLOPS_RENEW) {
  465                         NFSUNLOCKSTATE();
  466                 }
  467                 goto out;
  468         }
  469 
  470         /*
  471          * Perform any operations specified by the opflags.
  472          */
  473         if (opflags & CLOPS_CONFIRM) {
  474                 if (clp->lc_confirm.qval != confirm.qval)
  475                         error = NFSERR_STALECLIENTID;
  476                 else if (nfsrv_notsamecredname(nd, clp))
  477                         error = NFSERR_CLIDINUSE;
  478 
  479                 if (!error) {
  480                     if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
  481                         LCL_NEEDSCONFIRM) {
  482                         /*
  483                          * Hang onto the delegations (as old delegations)
  484                          * for an Open with CLAIM_DELEGATE_PREV unless in
  485                          * grace, but get rid of the rest of the state.
  486                          */
  487                         nfsrv_cleanclient(clp, p);
  488                         nfsrv_freedeleglist(&clp->lc_olddeleg);
  489                         if (nfsrv_checkgrace(0)) {
  490                             /* In grace, so just delete delegations */
  491                             nfsrv_freedeleglist(&clp->lc_deleg);
  492                         } else {
  493                             LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
  494                                 stp->ls_flags |= NFSLCK_OLDDELEG;
  495                             clp->lc_delegtime = NFSD_MONOSEC +
  496                                 nfsrv_lease + NFSRV_LEASEDELTA;
  497                             LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
  498                                 ls_list);
  499                         }
  500                     }
  501                     clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
  502                     if (clp->lc_program)
  503                         clp->lc_flags |= LCL_NEEDSCBNULL;
  504                 }
  505         } else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
  506                 error = NFSERR_EXPIRED;
  507         }
  508 
  509         /*
  510          * If called by the Renew Op, we must check the principal.
  511          */
  512         if (!error && (opflags & CLOPS_RENEWOP)) {
  513             if (nfsrv_notsamecredname(nd, clp)) {
  514                 doneok = 0;
  515                 for (i = 0; i < NFSSTATEHASHSIZE && doneok == 0; i++) {
  516                     LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
  517                         if ((stp->ls_flags & NFSLCK_OPEN) &&
  518                             stp->ls_uid == nd->nd_cred->cr_uid) {
  519                                 doneok = 1;
  520                                 break;
  521                         }
  522                     }
  523                 }
  524                 if (!doneok)
  525                         error = NFSERR_ACCES;
  526             }
  527             if (!error && (clp->lc_flags & LCL_CBDOWN))
  528                 error = NFSERR_CBPATHDOWN;
  529         }
  530         if ((!error || error == NFSERR_CBPATHDOWN) &&
  531              (opflags & CLOPS_RENEW)) {
  532                 clp->lc_expiry = nfsrv_leaseexpiry();
  533         }
  534         if (opflags & CLOPS_CONFIRM) {
  535                 NFSLOCKV4ROOTMUTEX();
  536                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
  537                 NFSUNLOCKV4ROOTMUTEX();
  538         } else if (opflags != CLOPS_RENEW) {
  539                 NFSUNLOCKSTATE();
  540         }
  541         if (clpp)
  542                 *clpp = clp;
  543 
  544 out:
  545         NFSEXITCODE2(error, nd);
  546         return (error);
  547 }
  548 
  549 /*
  550  * Called from the new nfssvc syscall to admin revoke a clientid.
  551  * Returns 0 for success, error otherwise.
  552  */
  553 APPLESTATIC int
  554 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
  555 {
  556         struct nfsclient *clp = NULL;
  557         int i, error = 0;
  558         int gotit, igotlock;
  559 
  560         /*
  561          * First, lock out the nfsd so that state won't change while the
  562          * revocation record is being written to the stable storage restart
  563          * file.
  564          */
  565         NFSLOCKV4ROOTMUTEX();
  566         do {
  567                 igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
  568                     NFSV4ROOTLOCKMUTEXPTR, NULL);
  569         } while (!igotlock);
  570         NFSUNLOCKV4ROOTMUTEX();
  571 
  572         /*
  573          * Search for a match in the client list.
  574          */
  575         gotit = i = 0;
  576         while (i < NFSCLIENTHASHSIZE && !gotit) {
  577             LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
  578                 if (revokep->nclid_idlen == clp->lc_idlen &&
  579                     !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
  580                         gotit = 1;
  581                         break;
  582                 }
  583             }
  584             i++;
  585         }
  586         if (!gotit) {
  587                 NFSLOCKV4ROOTMUTEX();
  588                 nfsv4_unlock(&nfsv4rootfs_lock, 0);
  589                 NFSUNLOCKV4ROOTMUTEX();
  590                 error = EPERM;
  591                 goto out;
  592         }
  593 
  594         /*
  595          * Now, write out the revocation record
  596          */
  597         nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
  598         nfsrv_backupstable();
  599 
  600         /*
  601          * and clear out the state, marking the clientid revoked.
  602          */
  603         clp->lc_flags &= ~LCL_CALLBACKSON;
  604         clp->lc_flags |= LCL_ADMINREVOKED;
  605         nfsrv_cleanclient(clp, p);
  606         nfsrv_freedeleglist(&clp->lc_deleg);
  607         nfsrv_freedeleglist(&clp->lc_olddeleg);
  608         NFSLOCKV4ROOTMUTEX();
  609         nfsv4_unlock(&nfsv4rootfs_lock, 0);
  610         NFSUNLOCKV4ROOTMUTEX();
  611 
  612 out:
  613         NFSEXITCODE(error);
  614         return (error);
  615 }
  616 
  617 /*
  618  * Dump out stats for all clients. Called from nfssvc(2), that is used
  619  * newnfsstats.
  620  */
  621 APPLESTATIC void
  622 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
  623 {
  624         struct nfsclient *clp;
  625         int i = 0, cnt = 0;
  626 
  627         /*
  628          * First, get a reference on the nfsv4rootfs_lock so that an
  629          * exclusive lock cannot be acquired while dumping the clients.
  630          */
  631         NFSLOCKV4ROOTMUTEX();
  632         nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
  633         NFSUNLOCKV4ROOTMUTEX();
  634         NFSLOCKSTATE();
  635         /*
  636          * Rattle through the client lists until done.
  637          */
  638         while (i < NFSCLIENTHASHSIZE && cnt < maxcnt) {
  639             clp = LIST_FIRST(&nfsclienthash[i]);
  640             while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
  641                 nfsrv_dumpaclient(clp, &dumpp[cnt]);
  642                 cnt++;
  643                 clp = LIST_NEXT(clp, lc_hash);
  644             }
  645             i++;
  646         }
  647         if (cnt < maxcnt)
  648             dumpp[cnt].ndcl_clid.nclid_idlen = 0;
  649         NFSUNLOCKSTATE();
  650         NFSLOCKV4ROOTMUTEX();
  651         nfsv4_relref(&nfsv4rootfs_lock);
  652         NFSUNLOCKV4ROOTMUTEX();
  653 }
  654 
  655 /*
  656  * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
  657  */
  658 static void
  659 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
  660 {
  661         struct nfsstate *stp, *openstp, *lckownstp;
  662         struct nfslock *lop;
  663         struct sockaddr *sad;
  664         struct sockaddr_in *rad;
  665         struct sockaddr_in6 *rad6;
  666 
  667         dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
  668         dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
  669         dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
  670         dumpp->ndcl_flags = clp->lc_flags;
  671         dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
  672         NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
  673         sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
  674         dumpp->ndcl_addrfam = sad->sa_family;
  675         if (sad->sa_family == AF_INET) {
  676                 rad = (struct sockaddr_in *)sad;
  677                 dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
  678         } else {
  679                 rad6 = (struct sockaddr_in6 *)sad;
  680                 dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
  681         }
  682 
  683         /*
  684          * Now, scan the state lists and total up the opens and locks.
  685          */
  686         LIST_FOREACH(stp, &clp->lc_open, ls_list) {
  687             dumpp->ndcl_nopenowners++;
  688             LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
  689                 dumpp->ndcl_nopens++;
  690                 LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
  691                     dumpp->ndcl_nlockowners++;
  692                     LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
  693                         dumpp->ndcl_nlocks++;
  694                     }
  695                 }
  696             }
  697         }
  698 
  699         /*
  700          * and the delegation lists.
  701          */
  702         LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
  703             dumpp->ndcl_ndelegs++;
  704         }
  705         LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
  706             dumpp->ndcl_nolddelegs++;
  707         }
  708 }
  709 
  710 /*
  711  * Dump out lock stats for a file.
  712  */
  713 APPLESTATIC void
  714 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
  715     NFSPROC_T *p)
  716 {
  717         struct nfsstate *stp;
  718         struct nfslock *lop;
  719         int cnt = 0;
  720         struct nfslockfile *lfp;
  721         struct sockaddr *sad;
  722         struct sockaddr_in *rad;
  723         struct sockaddr_in6 *rad6;
  724         int ret;
  725         fhandle_t nfh;
  726 
  727         ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
  728         /*
  729          * First, get a reference on the nfsv4rootfs_lock so that an
  730          * exclusive lock on it cannot be acquired while dumping the locks.
  731          */
  732         NFSLOCKV4ROOTMUTEX();
  733         nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
  734         NFSUNLOCKV4ROOTMUTEX();
  735         NFSLOCKSTATE();
  736         if (!ret)
  737                 ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
  738         if (ret) {
  739                 ldumpp[0].ndlck_clid.nclid_idlen = 0;
  740                 NFSUNLOCKSTATE();
  741                 NFSLOCKV4ROOTMUTEX();
  742                 nfsv4_relref(&nfsv4rootfs_lock);
  743                 NFSUNLOCKV4ROOTMUTEX();
  744                 return;
  745         }
  746 
  747         /*
  748          * For each open share on file, dump it out.
  749          */
  750         stp = LIST_FIRST(&lfp->lf_open);
  751         while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
  752                 ldumpp[cnt].ndlck_flags = stp->ls_flags;
  753                 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
  754                 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
  755                 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
  756                 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
  757                 ldumpp[cnt].ndlck_owner.nclid_idlen =
  758                     stp->ls_openowner->ls_ownerlen;
  759                 NFSBCOPY(stp->ls_openowner->ls_owner,
  760                     ldumpp[cnt].ndlck_owner.nclid_id,
  761                     stp->ls_openowner->ls_ownerlen);
  762                 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
  763                 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
  764                     stp->ls_clp->lc_idlen);
  765                 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
  766                 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
  767                 if (sad->sa_family == AF_INET) {
  768                         rad = (struct sockaddr_in *)sad;
  769                         ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
  770                 } else {
  771                         rad6 = (struct sockaddr_in6 *)sad;
  772                         ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
  773                 }
  774                 stp = LIST_NEXT(stp, ls_file);
  775                 cnt++;
  776         }
  777 
  778         /*
  779          * and all locks.
  780          */
  781         lop = LIST_FIRST(&lfp->lf_lock);
  782         while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
  783                 stp = lop->lo_stp;
  784                 ldumpp[cnt].ndlck_flags = lop->lo_flags;
  785                 ldumpp[cnt].ndlck_first = lop->lo_first;
  786                 ldumpp[cnt].ndlck_end = lop->lo_end;
  787                 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
  788                 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
  789                 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
  790                 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
  791                 ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
  792                 NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
  793                     stp->ls_ownerlen);
  794                 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
  795                 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
  796                     stp->ls_clp->lc_idlen);
  797                 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
  798                 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
  799                 if (sad->sa_family == AF_INET) {
  800                         rad = (struct sockaddr_in *)sad;
  801                         ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
  802                 } else {
  803                         rad6 = (struct sockaddr_in6 *)sad;
  804                         ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
  805                 }
  806                 lop = LIST_NEXT(lop, lo_lckfile);
  807                 cnt++;
  808         }
  809 
  810         /*
  811          * and the delegations.
  812          */
  813         stp = LIST_FIRST(&lfp->lf_deleg);
  814         while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
  815                 ldumpp[cnt].ndlck_flags = stp->ls_flags;
  816                 ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
  817                 ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
  818                 ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
  819                 ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
  820                 ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
  821                 ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
  822                 NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
  823                     stp->ls_clp->lc_idlen);
  824                 sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
  825                 ldumpp[cnt].ndlck_addrfam = sad->sa_family;
  826                 if (sad->sa_family == AF_INET) {
  827                         rad = (struct sockaddr_in *)sad;
  828                         ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
  829                 } else {
  830                         rad6 = (struct sockaddr_in6 *)sad;
  831                         ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
  832                 }
  833                 stp = LIST_NEXT(stp, ls_file);
  834                 cnt++;
  835         }
  836 
  837         /*
  838          * If list isn't full, mark end of list by setting the client name
  839          * to zero length.
  840          */
  841         if (cnt < maxcnt)
  842                 ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
  843         NFSUNLOCKSTATE();
  844         NFSLOCKV4ROOTMUTEX();
  845         nfsv4_relref(&nfsv4rootfs_lock);
  846         NFSUNLOCKV4ROOTMUTEX();
  847 }
  848 
  849 /*
  850  * Server timer routine. It can scan any linked list, so long
  851  * as it holds the spin/mutex lock and there is no exclusive lock on
  852  * nfsv4rootfs_lock.
  853  * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
  854  *  to do this from a callout, since the spin locks work. For
  855  *  Darwin, I'm not sure what will work correctly yet.)
  856  * Should be called once per second.
  857  */
  858 APPLESTATIC void
  859 nfsrv_servertimer(void)
  860 {
  861         struct nfsclient *clp, *nclp;
  862         struct nfsstate *stp, *nstp;
  863         int got_ref, i;
  864 
  865         /*
  866          * Make sure nfsboottime is set. This is used by V3 as well
  867          * as V4. Note that nfsboottime is not nfsrvboottime, which is
  868          * only used by the V4 server for leases.
  869          */
  870         if (nfsboottime.tv_sec == 0)
  871                 NFSSETBOOTTIME(nfsboottime);
  872 
  873         /*
  874          * If server hasn't started yet, just return.
  875          */
  876         NFSLOCKSTATE();
  877         if (nfsrv_stablefirst.nsf_eograce == 0) {
  878                 NFSUNLOCKSTATE();
  879                 return;
  880         }
  881         if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
  882                 if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
  883                     NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
  884                         nfsrv_stablefirst.nsf_flags |=
  885                             (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
  886                 NFSUNLOCKSTATE();
  887                 return;
  888         }
  889 
  890         /*
  891          * Try and get a reference count on the nfsv4rootfs_lock so that
  892          * no nfsd thread can acquire an exclusive lock on it before this
  893          * call is done. If it is already exclusively locked, just return.
  894          */
  895         NFSLOCKV4ROOTMUTEX();
  896         got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
  897         NFSUNLOCKV4ROOTMUTEX();
  898         if (got_ref == 0) {
  899                 NFSUNLOCKSTATE();
  900                 return;
  901         }
  902 
  903         /*
  904          * For each client...
  905          */
  906         for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
  907             clp = LIST_FIRST(&nfsclienthash[i]);
  908             while (clp != LIST_END(&nfsclienthash[i])) {
  909                 nclp = LIST_NEXT(clp, lc_hash);
  910                 if (!(clp->lc_flags & LCL_EXPIREIT)) {
  911                     if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
  912                          && ((LIST_EMPTY(&clp->lc_deleg)
  913                               && LIST_EMPTY(&clp->lc_open)) ||
  914                              nfsrv_clients > nfsrv_clienthighwater)) ||
  915                         (clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
  916                         (clp->lc_expiry < NFSD_MONOSEC &&
  917                          (nfsrv_openpluslock * 10 / 9) > NFSRV_V4STATELIMIT)) {
  918                         /*
  919                          * Lease has expired several nfsrv_lease times ago:
  920                          * PLUS
  921                          *    - no state is associated with it
  922                          *    OR
  923                          *    - above high water mark for number of clients
  924                          *      (nfsrv_clienthighwater should be large enough
  925                          *       that this only occurs when clients fail to
  926                          *       use the same nfs_client_id4.id. Maybe somewhat
  927                          *       higher that the maximum number of clients that
  928                          *       will mount this server?)
  929                          * OR
  930                          * Lease has expired a very long time ago
  931                          * OR
  932                          * Lease has expired PLUS the number of opens + locks
  933                          * has exceeded 90% of capacity
  934                          *
  935                          * --> Mark for expiry. The actual expiry will be done
  936                          *     by an nfsd sometime soon.
  937                          */
  938                         clp->lc_flags |= LCL_EXPIREIT;
  939                         nfsrv_stablefirst.nsf_flags |=
  940                             (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
  941                     } else {
  942                         /*
  943                          * If there are no opens, increment no open tick cnt
  944                          * If time exceeds NFSNOOPEN, mark it to be thrown away
  945                          * otherwise, if there is an open, reset no open time
  946                          * Hopefully, this will avoid excessive re-creation
  947                          * of open owners and subsequent open confirms.
  948                          */
  949                         stp = LIST_FIRST(&clp->lc_open);
  950                         while (stp != LIST_END(&clp->lc_open)) {
  951                                 nstp = LIST_NEXT(stp, ls_list);
  952                                 if (LIST_EMPTY(&stp->ls_open)) {
  953                                         stp->ls_noopens++;
  954                                         if (stp->ls_noopens > NFSNOOPEN ||
  955                                             (nfsrv_openpluslock * 2) >
  956                                             NFSRV_V4STATELIMIT)
  957                                                 nfsrv_stablefirst.nsf_flags |=
  958                                                         NFSNSF_NOOPENS;
  959                                 } else {
  960                                         stp->ls_noopens = 0;
  961                                 }
  962                                 stp = nstp;
  963                         }
  964                     }
  965                 }
  966                 clp = nclp;
  967             }
  968         }
  969         NFSUNLOCKSTATE();
  970         NFSLOCKV4ROOTMUTEX();
  971         nfsv4_relref(&nfsv4rootfs_lock);
  972         NFSUNLOCKV4ROOTMUTEX();
  973 }
  974 
  975 /*
  976  * The following set of functions free up the various data structures.
  977  */
  978 /*
  979  * Clear out all open/lock state related to this nfsclient.
  980  * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
  981  * there are no other active nfsd threads.
  982  */
  983 APPLESTATIC void
  984 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
  985 {
  986         struct nfsstate *stp, *nstp;
  987 
  988         LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
  989                 nfsrv_freeopenowner(stp, 1, p);
  990 }
  991 
  992 /*
  993  * Free a client that has been cleaned. It should also already have been
  994  * removed from the lists.
  995  * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
  996  *  softclock interrupts are enabled.)
  997  */
  998 APPLESTATIC void
  999 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
 1000 {
 1001 
 1002 #ifdef notyet
 1003         if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
 1004              (LCL_GSS | LCL_CALLBACKSON) &&
 1005             (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
 1006             clp->lc_handlelen > 0) {
 1007                 clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
 1008                 clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
 1009                 (void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
 1010                         NULL, 0, NULL, NULL, NULL, p);
 1011         }
 1012 #endif
 1013         newnfs_disconnect(&clp->lc_req);
 1014         NFSSOCKADDRFREE(clp->lc_req.nr_nam);
 1015         NFSFREEMUTEX(&clp->lc_req.nr_mtx);
 1016         free((caddr_t)clp, M_NFSDCLIENT);
 1017         NFSLOCKSTATE();
 1018         newnfsstats.srvclients--;
 1019         nfsrv_openpluslock--;
 1020         nfsrv_clients--;
 1021         NFSUNLOCKSTATE();
 1022 }
 1023 
 1024 /*
 1025  * Free a list of delegation state structures.
 1026  * (This function will also free all nfslockfile structures that no
 1027  *  longer have associated state.)
 1028  */
 1029 APPLESTATIC void
 1030 nfsrv_freedeleglist(struct nfsstatehead *sthp)
 1031 {
 1032         struct nfsstate *stp, *nstp;
 1033 
 1034         LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
 1035                 nfsrv_freedeleg(stp);
 1036         }
 1037         LIST_INIT(sthp);
 1038 }
 1039 
 1040 /*
 1041  * Free up a delegation.
 1042  */
 1043 static void
 1044 nfsrv_freedeleg(struct nfsstate *stp)
 1045 {
 1046         struct nfslockfile *lfp;
 1047 
 1048         LIST_REMOVE(stp, ls_hash);
 1049         LIST_REMOVE(stp, ls_list);
 1050         LIST_REMOVE(stp, ls_file);
 1051         lfp = stp->ls_lfp;
 1052         if (LIST_EMPTY(&lfp->lf_open) &&
 1053             LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
 1054             LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
 1055             lfp->lf_usecount == 0 &&
 1056             nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
 1057                 nfsrv_freenfslockfile(lfp);
 1058         FREE((caddr_t)stp, M_NFSDSTATE);
 1059         newnfsstats.srvdelegates--;
 1060         nfsrv_openpluslock--;
 1061         nfsrv_delegatecnt--;
 1062 }
 1063 
 1064 /*
 1065  * This function frees an open owner and all associated opens.
 1066  */
 1067 static void
 1068 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
 1069 {
 1070         struct nfsstate *nstp, *tstp;
 1071 
 1072         LIST_REMOVE(stp, ls_list);
 1073         /*
 1074          * Now, free all associated opens.
 1075          */
 1076         nstp = LIST_FIRST(&stp->ls_open);
 1077         while (nstp != LIST_END(&stp->ls_open)) {
 1078                 tstp = nstp;
 1079                 nstp = LIST_NEXT(nstp, ls_list);
 1080                 (void) nfsrv_freeopen(tstp, NULL, cansleep, p);
 1081         }
 1082         if (stp->ls_op)
 1083                 nfsrvd_derefcache(stp->ls_op);
 1084         FREE((caddr_t)stp, M_NFSDSTATE);
 1085         newnfsstats.srvopenowners--;
 1086         nfsrv_openpluslock--;
 1087 }
 1088 
 1089 /*
 1090  * This function frees an open (nfsstate open structure) with all associated
 1091  * lock_owners and locks. It also frees the nfslockfile structure iff there
 1092  * are no other opens on the file.
 1093  * Returns 1 if it free'd the nfslockfile, 0 otherwise.
 1094  */
 1095 static int
 1096 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
 1097 {
 1098         struct nfsstate *nstp, *tstp;
 1099         struct nfslockfile *lfp;
 1100         int ret;
 1101 
 1102         LIST_REMOVE(stp, ls_hash);
 1103         LIST_REMOVE(stp, ls_list);
 1104         LIST_REMOVE(stp, ls_file);
 1105 
 1106         lfp = stp->ls_lfp;
 1107         /*
 1108          * Now, free all lockowners associated with this open.
 1109          */
 1110         LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
 1111                 nfsrv_freelockowner(tstp, vp, cansleep, p);
 1112 
 1113         /*
 1114          * The nfslockfile is freed here if there are no locks
 1115          * associated with the open.
 1116          * If there are locks associated with the open, the
 1117          * nfslockfile structure can be freed via nfsrv_freelockowner().
 1118          * Acquire the state mutex to avoid races with calls to
 1119          * nfsrv_getlockfile().
 1120          */
 1121         if (cansleep != 0)
 1122                 NFSLOCKSTATE();
 1123         if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
 1124             LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
 1125             LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
 1126             lfp->lf_usecount == 0 &&
 1127             (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
 1128                 nfsrv_freenfslockfile(lfp);
 1129                 ret = 1;
 1130         } else
 1131                 ret = 0;
 1132         if (cansleep != 0)
 1133                 NFSUNLOCKSTATE();
 1134         FREE((caddr_t)stp, M_NFSDSTATE);
 1135         newnfsstats.srvopens--;
 1136         nfsrv_openpluslock--;
 1137         return (ret);
 1138 }
 1139 
 1140 /*
 1141  * Frees a lockowner and all associated locks.
 1142  */
 1143 static void
 1144 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
 1145     NFSPROC_T *p)
 1146 {
 1147 
 1148         LIST_REMOVE(stp, ls_hash);
 1149         LIST_REMOVE(stp, ls_list);
 1150         nfsrv_freeallnfslocks(stp, vp, cansleep, p);
 1151         if (stp->ls_op)
 1152                 nfsrvd_derefcache(stp->ls_op);
 1153         FREE((caddr_t)stp, M_NFSDSTATE);
 1154         newnfsstats.srvlockowners--;
 1155         nfsrv_openpluslock--;
 1156 }
 1157 
 1158 /*
 1159  * Free all the nfs locks on a lockowner.
 1160  */
 1161 static void
 1162 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
 1163     NFSPROC_T *p)
 1164 {
 1165         struct nfslock *lop, *nlop;
 1166         struct nfsrollback *rlp, *nrlp;
 1167         struct nfslockfile *lfp = NULL;
 1168         int gottvp = 0;
 1169         vnode_t tvp = NULL;
 1170         uint64_t first, end;
 1171 
 1172         if (vp != NULL)
 1173                 ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
 1174         lop = LIST_FIRST(&stp->ls_lock);
 1175         while (lop != LIST_END(&stp->ls_lock)) {
 1176                 nlop = LIST_NEXT(lop, lo_lckowner);
 1177                 /*
 1178                  * Since all locks should be for the same file, lfp should
 1179                  * not change.
 1180                  */
 1181                 if (lfp == NULL)
 1182                         lfp = lop->lo_lfp;
 1183                 else if (lfp != lop->lo_lfp)
 1184                         panic("allnfslocks");
 1185                 /*
 1186                  * If vp is NULL and cansleep != 0, a vnode must be acquired
 1187                  * from the file handle. This only occurs when called from
 1188                  * nfsrv_cleanclient().
 1189                  */
 1190                 if (gottvp == 0) {
 1191                         if (nfsrv_dolocallocks == 0)
 1192                                 tvp = NULL;
 1193                         else if (vp == NULL && cansleep != 0) {
 1194                                 tvp = nfsvno_getvp(&lfp->lf_fh);
 1195                                 NFSVOPUNLOCK(tvp, 0);
 1196                         } else
 1197                                 tvp = vp;
 1198                         gottvp = 1;
 1199                 }
 1200 
 1201                 if (tvp != NULL) {
 1202                         if (cansleep == 0)
 1203                                 panic("allnfs2");
 1204                         first = lop->lo_first;
 1205                         end = lop->lo_end;
 1206                         nfsrv_freenfslock(lop);
 1207                         nfsrv_localunlock(tvp, lfp, first, end, p);
 1208                         LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
 1209                             nrlp)
 1210                                 free(rlp, M_NFSDROLLBACK);
 1211                         LIST_INIT(&lfp->lf_rollback);
 1212                 } else
 1213                         nfsrv_freenfslock(lop);
 1214                 lop = nlop;
 1215         }
 1216         if (vp == NULL && tvp != NULL)
 1217                 vrele(tvp);
 1218 }
 1219 
 1220 /*
 1221  * Free an nfslock structure.
 1222  */
 1223 static void
 1224 nfsrv_freenfslock(struct nfslock *lop)
 1225 {
 1226 
 1227         if (lop->lo_lckfile.le_prev != NULL) {
 1228                 LIST_REMOVE(lop, lo_lckfile);
 1229                 newnfsstats.srvlocks--;
 1230                 nfsrv_openpluslock--;
 1231         }
 1232         LIST_REMOVE(lop, lo_lckowner);
 1233         FREE((caddr_t)lop, M_NFSDLOCK);
 1234 }
 1235 
 1236 /*
 1237  * This function frees an nfslockfile structure.
 1238  */
 1239 static void
 1240 nfsrv_freenfslockfile(struct nfslockfile *lfp)
 1241 {
 1242 
 1243         LIST_REMOVE(lfp, lf_hash);
 1244         FREE((caddr_t)lfp, M_NFSDLOCKFILE);
 1245 }
 1246 
 1247 /*
 1248  * This function looks up an nfsstate structure via stateid.
 1249  */
 1250 static int
 1251 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
 1252     struct nfsstate **stpp)
 1253 {
 1254         struct nfsstate *stp;
 1255         struct nfsstatehead *hp;
 1256         int error = 0;
 1257 
 1258         *stpp = NULL;
 1259         hp = NFSSTATEHASH(clp, *stateidp);
 1260         LIST_FOREACH(stp, hp, ls_hash) {
 1261                 if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
 1262                         NFSX_STATEIDOTHER))
 1263                         break;
 1264         }
 1265 
 1266         /*
 1267          * If no state id in list, return NFSERR_BADSTATEID.
 1268          */
 1269         if (stp == LIST_END(hp)) {
 1270                 error = NFSERR_BADSTATEID;
 1271                 goto out;
 1272         }
 1273         *stpp = stp;
 1274 
 1275 out:
 1276         NFSEXITCODE(error);
 1277         return (error);
 1278 }
 1279 
 1280 /*
 1281  * This function gets an nfsstate structure via owner string.
 1282  */
 1283 static void
 1284 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
 1285     struct nfsstate **stpp)
 1286 {
 1287         struct nfsstate *stp;
 1288 
 1289         *stpp = NULL;
 1290         LIST_FOREACH(stp, hp, ls_list) {
 1291                 if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
 1292                   !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
 1293                         *stpp = stp;
 1294                         return;
 1295                 }
 1296         }
 1297 }
 1298 
 1299 /*
 1300  * Lock control function called to update lock status.
 1301  * Returns 0 upon success, -1 if there is no lock and the flags indicate
 1302  * that one isn't to be created and an NFSERR_xxx for other errors.
 1303  * The structures new_stp and new_lop are passed in as pointers that should
 1304  * be set to NULL if the structure is used and shouldn't be free'd.
 1305  * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
 1306  * never used and can safely be allocated on the stack. For all other
 1307  * cases, *new_stpp and *new_lopp should be malloc'd before the call,
 1308  * in case they are used.
 1309  */
 1310 APPLESTATIC int
 1311 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
 1312     struct nfslock **new_lopp, struct nfslockconflict *cfp,
 1313     nfsquad_t clientid, nfsv4stateid_t *stateidp,
 1314     __unused struct nfsexstuff *exp,
 1315     struct nfsrv_descript *nd, NFSPROC_T *p)
 1316 {
 1317         struct nfslock *lop;
 1318         struct nfsstate *new_stp = *new_stpp;
 1319         struct nfslock *new_lop = *new_lopp;
 1320         struct nfsstate *tstp, *mystp, *nstp;
 1321         int specialid = 0;
 1322         struct nfslockfile *lfp;
 1323         struct nfslock *other_lop = NULL;
 1324         struct nfsstate *stp, *lckstp = NULL;
 1325         struct nfsclient *clp = NULL;
 1326         u_int32_t bits;
 1327         int error = 0, haslock = 0, ret, reterr;
 1328         int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
 1329         fhandle_t nfh;
 1330         uint64_t first, end;
 1331         uint32_t lock_flags;
 1332 
 1333         if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
 1334                 /*
 1335                  * Note the special cases of "all 1s" or "all 0s" stateids and
 1336                  * let reads with all 1s go ahead.
 1337                  */
 1338                 if (new_stp->ls_stateid.seqid == 0x0 &&
 1339                     new_stp->ls_stateid.other[0] == 0x0 &&
 1340                     new_stp->ls_stateid.other[1] == 0x0 &&
 1341                     new_stp->ls_stateid.other[2] == 0x0)
 1342                         specialid = 1;
 1343                 else if (new_stp->ls_stateid.seqid == 0xffffffff &&
 1344                     new_stp->ls_stateid.other[0] == 0xffffffff &&
 1345                     new_stp->ls_stateid.other[1] == 0xffffffff &&
 1346                     new_stp->ls_stateid.other[2] == 0xffffffff)
 1347                         specialid = 2;
 1348         }
 1349 
 1350         /*
 1351          * Check for restart conditions (client and server).
 1352          */
 1353         error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
 1354             &new_stp->ls_stateid, specialid);
 1355         if (error)
 1356                 goto out;
 1357 
 1358         /*
 1359          * Check for state resource limit exceeded.
 1360          */
 1361         if ((new_stp->ls_flags & NFSLCK_LOCK) &&
 1362             nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
 1363                 error = NFSERR_RESOURCE;
 1364                 goto out;
 1365         }
 1366 
 1367         /*
 1368          * For the lock case, get another nfslock structure,
 1369          * just in case we need it.
 1370          * Malloc now, before we start sifting through the linked lists,
 1371          * in case we have to wait for memory.
 1372          */
 1373 tryagain:
 1374         if (new_stp->ls_flags & NFSLCK_LOCK)
 1375                 MALLOC(other_lop, struct nfslock *, sizeof (struct nfslock),
 1376                     M_NFSDLOCK, M_WAITOK);
 1377         filestruct_locked = 0;
 1378         reterr = 0;
 1379         lfp = NULL;
 1380 
 1381         /*
 1382          * Get the lockfile structure for CFH now, so we can do a sanity
 1383          * check against the stateid, before incrementing the seqid#, since
 1384          * we want to return NFSERR_BADSTATEID on failure and the seqid#
 1385          * shouldn't be incremented for this case.
 1386          * If nfsrv_getlockfile() returns -1, it means "not found", which
 1387          * will be handled later.
 1388          * If we are doing Lock/LockU and local locking is enabled, sleep
 1389          * lock the nfslockfile structure.
 1390          */
 1391         getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
 1392         NFSLOCKSTATE();
 1393         if (getlckret == 0) {
 1394                 if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
 1395                     nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
 1396                         getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
 1397                             &lfp, &nfh, 1);
 1398                         if (getlckret == 0)
 1399                                 filestruct_locked = 1;
 1400                 } else
 1401                         getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
 1402                             &lfp, &nfh, 0);
 1403         }
 1404         if (getlckret != 0 && getlckret != -1)
 1405                 reterr = getlckret;
 1406 
 1407         if (filestruct_locked != 0) {
 1408                 LIST_INIT(&lfp->lf_rollback);
 1409                 if ((new_stp->ls_flags & NFSLCK_LOCK)) {
 1410                         /*
 1411                          * For local locking, do the advisory locking now, so
 1412                          * that any conflict can be detected. A failure later
 1413                          * can be rolled back locally. If an error is returned,
 1414                          * struct nfslockfile has been unlocked and any local
 1415                          * locking rolled back.
 1416                          */
 1417                         NFSUNLOCKSTATE();
 1418                         if (vnode_unlocked == 0) {
 1419                                 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
 1420                                 vnode_unlocked = 1;
 1421                                 NFSVOPUNLOCK(vp, 0);
 1422                         }
 1423                         reterr = nfsrv_locallock(vp, lfp,
 1424                             (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
 1425                             new_lop->lo_first, new_lop->lo_end, cfp, p);
 1426                         NFSLOCKSTATE();
 1427                 }
 1428         }
 1429 
 1430         if (specialid == 0) {
 1431             if (new_stp->ls_flags & NFSLCK_TEST) {
 1432                 /*
 1433                  * RFC 3530 does not list LockT as an op that renews a
 1434                  * lease, but the concensus seems to be that it is ok
 1435                  * for a server to do so.
 1436                  */
 1437                 error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
 1438                     (nfsquad_t)((u_quad_t)0), NULL, p);
 1439 
 1440                 /*
 1441                  * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
 1442                  * error returns for LockT, just go ahead and test for a lock,
 1443                  * since there are no locks for this client, but other locks
 1444                  * can conflict. (ie. same client will always be false)
 1445                  */
 1446                 if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
 1447                     error = 0;
 1448                 lckstp = new_stp;
 1449             } else {
 1450               error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
 1451                 (nfsquad_t)((u_quad_t)0), NULL, p);
 1452               if (error == 0)
 1453                 /*
 1454                  * Look up the stateid
 1455                  */
 1456                 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
 1457                   new_stp->ls_flags, &stp);
 1458               /*
 1459                * do some sanity checks for an unconfirmed open or a
 1460                * stateid that refers to the wrong file, for an open stateid
 1461                */
 1462               if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
 1463                   ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
 1464                    (getlckret == 0 && stp->ls_lfp != lfp)))
 1465                         error = NFSERR_BADSTATEID;
 1466               if (error == 0 &&
 1467                   (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
 1468                   getlckret == 0 && stp->ls_lfp != lfp)
 1469                         error = NFSERR_BADSTATEID;
 1470 
 1471               /*
 1472                * If the lockowner stateid doesn't refer to the same file,
 1473                * I believe that is considered ok, since some clients will
 1474                * only create a single lockowner and use that for all locks
 1475                * on all files.
 1476                * For now, log it as a diagnostic, instead of considering it
 1477                * a BadStateid.
 1478                */
 1479               if (error == 0 && (stp->ls_flags &
 1480                   (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
 1481                   getlckret == 0 && stp->ls_lfp != lfp) {
 1482 #ifdef DIAGNOSTIC
 1483                   printf("Got a lock statid for different file open\n");
 1484 #endif
 1485                   /*
 1486                   error = NFSERR_BADSTATEID;
 1487                   */
 1488               }
 1489 
 1490               if (error == 0) {
 1491                     if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
 1492                         /*
 1493                          * If haslock set, we've already checked the seqid.
 1494                          */
 1495                         if (!haslock) {
 1496                             if (stp->ls_flags & NFSLCK_OPEN)
 1497                                 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
 1498                                     stp->ls_openowner, new_stp->ls_op);
 1499                             else
 1500                                 error = NFSERR_BADSTATEID;
 1501                         }
 1502                         if (!error)
 1503                             nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
 1504                         if (lckstp)
 1505                             /*
 1506                              * I believe this should be an error, but it
 1507                              * isn't obvious what NFSERR_xxx would be
 1508                              * appropriate, so I'll use NFSERR_INVAL for now.
 1509                              */
 1510                             error = NFSERR_INVAL;
 1511                         else
 1512                             lckstp = new_stp;
 1513                     } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
 1514                         /*
 1515                          * If haslock set, ditto above.
 1516                          */
 1517                         if (!haslock) {
 1518                             if (stp->ls_flags & NFSLCK_OPEN)
 1519                                 error = NFSERR_BADSTATEID;
 1520                             else
 1521                                 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
 1522                                     stp, new_stp->ls_op);
 1523                         }
 1524                         lckstp = stp;
 1525                     } else {
 1526                         lckstp = stp;
 1527                     }
 1528               }
 1529               /*
 1530                * If the seqid part of the stateid isn't the same, return
 1531                * NFSERR_OLDSTATEID for cases other than I/O Ops.
 1532                * For I/O Ops, only return NFSERR_OLDSTATEID if
 1533                * nfsrv_returnoldstateid is set. (The concensus on the email
 1534                * list was that most clients would prefer to not receive
 1535                * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
 1536                * is what will happen, so I use the nfsrv_returnoldstateid to
 1537                * allow for either server configuration.)
 1538                */
 1539               if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
 1540                   (!(new_stp->ls_flags & NFSLCK_CHECK) ||
 1541                    nfsrv_returnoldstateid))
 1542                     error = NFSERR_OLDSTATEID;
 1543             }
 1544         }
 1545 
 1546         /*
 1547          * Now we can check for grace.
 1548          */
 1549         if (!error)
 1550                 error = nfsrv_checkgrace(new_stp->ls_flags);
 1551         if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
 1552                 nfsrv_checkstable(clp))
 1553                 error = NFSERR_NOGRACE;
 1554         /*
 1555          * If we successfully Reclaimed state, note that.
 1556          */
 1557         if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
 1558                 nfsrv_markstable(clp);
 1559 
 1560         /*
 1561          * At this point, either error == NFSERR_BADSTATEID or the
 1562          * seqid# has been updated, so we can return any error.
 1563          * If error == 0, there may be an error in:
 1564          *    nd_repstat - Set by the calling function.
 1565          *    reterr - Set above, if getting the nfslockfile structure
 1566          *       or acquiring the local lock failed.
 1567          *    (If both of these are set, nd_repstat should probably be
 1568          *     returned, since that error was detected before this
 1569          *     function call.)
 1570          */
 1571         if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
 1572                 if (error == 0) {
 1573                         if (nd->nd_repstat != 0)
 1574                                 error = nd->nd_repstat;
 1575                         else
 1576                                 error = reterr;
 1577                 }
 1578                 if (filestruct_locked != 0) {
 1579                         /* Roll back local locks. */
 1580                         NFSUNLOCKSTATE();
 1581                         if (vnode_unlocked == 0) {
 1582                                 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
 1583                                 vnode_unlocked = 1;
 1584                                 NFSVOPUNLOCK(vp, 0);
 1585                         }
 1586                         nfsrv_locallock_rollback(vp, lfp, p);
 1587                         NFSLOCKSTATE();
 1588                         nfsrv_unlocklf(lfp);
 1589                 }
 1590                 NFSUNLOCKSTATE();
 1591                 goto out;
 1592         }
 1593 
 1594         /*
 1595          * Check the nfsrv_getlockfile return.
 1596          * Returned -1 if no structure found.
 1597          */
 1598         if (getlckret == -1) {
 1599                 error = NFSERR_EXPIRED;
 1600                 /*
 1601                  * Called from lockt, so no lock is OK.
 1602                  */
 1603                 if (new_stp->ls_flags & NFSLCK_TEST) {
 1604                         error = 0;
 1605                 } else if (new_stp->ls_flags &
 1606                     (NFSLCK_CHECK | NFSLCK_SETATTR)) {
 1607                         /*
 1608                          * Called to check for a lock, OK if the stateid is all
 1609                          * 1s or all 0s, but there should be an nfsstate
 1610                          * otherwise.
 1611                          * (ie. If there is no open, I'll assume no share
 1612                          *  deny bits.)
 1613                          */
 1614                         if (specialid)
 1615                                 error = 0;
 1616                         else
 1617                                 error = NFSERR_BADSTATEID;
 1618                 }
 1619                 NFSUNLOCKSTATE();
 1620                 goto out;
 1621         }
 1622 
 1623         /*
 1624          * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
 1625          * For NFSLCK_CHECK, allow a read if write access is granted,
 1626          * but check for a deny. For NFSLCK_LOCK, require correct access,
 1627          * which implies a conflicting deny can't exist.
 1628          */
 1629         if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
 1630             /*
 1631              * Four kinds of state id:
 1632              * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
 1633              * - stateid for an open
 1634              * - stateid for a delegation
 1635              * - stateid for a lock owner
 1636              */
 1637             if (!specialid) {
 1638                 if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
 1639                     delegation = 1;
 1640                     mystp = stp;
 1641                     nfsrv_delaydelegtimeout(stp);
 1642                 } else if (stp->ls_flags & NFSLCK_OPEN) {
 1643                     mystp = stp;
 1644                 } else {
 1645                     mystp = stp->ls_openstp;
 1646                 }
 1647                 /*
 1648                  * If locking or checking, require correct access
 1649                  * bit set.
 1650                  */
 1651                 if (((new_stp->ls_flags & NFSLCK_LOCK) &&
 1652                      !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
 1653                        mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
 1654                     ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
 1655                       (NFSLCK_CHECK | NFSLCK_READACCESS) &&
 1656                      !(mystp->ls_flags & NFSLCK_READACCESS)) ||
 1657                     ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
 1658                       (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
 1659                      !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
 1660                         if (filestruct_locked != 0) {
 1661                                 /* Roll back local locks. */
 1662                                 NFSUNLOCKSTATE();
 1663                                 if (vnode_unlocked == 0) {
 1664                                         ASSERT_VOP_ELOCKED(vp,
 1665                                             "nfsrv_lockctrl3");
 1666                                         vnode_unlocked = 1;
 1667                                         NFSVOPUNLOCK(vp, 0);
 1668                                 }
 1669                                 nfsrv_locallock_rollback(vp, lfp, p);
 1670                                 NFSLOCKSTATE();
 1671                                 nfsrv_unlocklf(lfp);
 1672                         }
 1673                         NFSUNLOCKSTATE();
 1674                         error = NFSERR_OPENMODE;
 1675                         goto out;
 1676                 }
 1677             } else
 1678                 mystp = NULL;
 1679             if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
 1680                 /*
 1681                  * Check for a conflicting deny bit.
 1682                  */
 1683                 LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
 1684                     if (tstp != mystp) {
 1685                         bits = tstp->ls_flags;
 1686                         bits >>= NFSLCK_SHIFT;
 1687                         if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
 1688                             KASSERT(vnode_unlocked == 0,
 1689                                 ("nfsrv_lockctrl: vnode unlocked1"));
 1690                             ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
 1691                                 vp, p);
 1692                             if (ret == 1) {
 1693                                 /*
 1694                                 * nfsrv_clientconflict unlocks state
 1695                                  * when it returns non-zero.
 1696                                  */
 1697                                 lckstp = NULL;
 1698                                 goto tryagain;
 1699                             }
 1700                             if (ret == 0)
 1701                                 NFSUNLOCKSTATE();
 1702                             if (ret == 2)
 1703                                 error = NFSERR_PERM;
 1704                             else
 1705                                 error = NFSERR_OPENMODE;
 1706                             goto out;
 1707                         }
 1708                     }
 1709                 }
 1710 
 1711                 /* We're outta here */
 1712                 NFSUNLOCKSTATE();
 1713                 goto out;
 1714             }
 1715         }
 1716 
 1717         /*
 1718          * For setattr, just get rid of all the Delegations for other clients.
 1719          */
 1720         if (new_stp->ls_flags & NFSLCK_SETATTR) {
 1721                 KASSERT(vnode_unlocked == 0,
 1722                     ("nfsrv_lockctrl: vnode unlocked2"));
 1723                 ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
 1724                 if (ret) {
 1725                         /*
 1726                          * nfsrv_cleandeleg() unlocks state when it
 1727                          * returns non-zero.
 1728                          */
 1729                         if (ret == -1) {
 1730                                 lckstp = NULL;
 1731                                 goto tryagain;
 1732                         }
 1733                         error = ret;
 1734                         goto out;
 1735                 }
 1736                 if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
 1737                     (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
 1738                      LIST_EMPTY(&lfp->lf_deleg))) {
 1739                         NFSUNLOCKSTATE();
 1740                         goto out;
 1741                 }
 1742         }
 1743 
 1744         /*
 1745          * Check for a conflicting delegation. If one is found, call
 1746          * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
 1747          * been set yet, it will get the lock. Otherwise, it will recall
 1748          * the delegation. Then, we try try again...
 1749          * I currently believe the conflict algorithm to be:
 1750          * For Lock Ops (Lock/LockT/LockU)
 1751          * - there is a conflict iff a different client has a write delegation
 1752          * For Reading (Read Op)
 1753          * - there is a conflict iff a different client has a write delegation
 1754          *   (the specialids are always a different client)
 1755          * For Writing (Write/Setattr of size)
 1756          * - there is a conflict if a different client has any delegation
 1757          * - there is a conflict if the same client has a read delegation
 1758          *   (I don't understand why this isn't allowed, but that seems to be
 1759          *    the current concensus?)
 1760          */
 1761         tstp = LIST_FIRST(&lfp->lf_deleg);
 1762         while (tstp != LIST_END(&lfp->lf_deleg)) {
 1763             nstp = LIST_NEXT(tstp, ls_file);
 1764             if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
 1765                  ((new_stp->ls_flags & NFSLCK_CHECK) &&
 1766                   (new_lop->lo_flags & NFSLCK_READ))) &&
 1767                   clp != tstp->ls_clp &&
 1768                  (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
 1769                  ((new_stp->ls_flags & NFSLCK_CHECK) &&
 1770                    (new_lop->lo_flags & NFSLCK_WRITE) &&
 1771                   (clp != tstp->ls_clp ||
 1772                    (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
 1773                 ret = 0;
 1774                 if (filestruct_locked != 0) {
 1775                         /* Roll back local locks. */
 1776                         NFSUNLOCKSTATE();
 1777                         if (vnode_unlocked == 0) {
 1778                                 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
 1779                                 NFSVOPUNLOCK(vp, 0);
 1780                         }
 1781                         nfsrv_locallock_rollback(vp, lfp, p);
 1782                         NFSLOCKSTATE();
 1783                         nfsrv_unlocklf(lfp);
 1784                         NFSUNLOCKSTATE();
 1785                         NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
 1786                         vnode_unlocked = 0;
 1787                         if ((vp->v_iflag & VI_DOOMED) != 0)
 1788                                 ret = NFSERR_SERVERFAULT;
 1789                         NFSLOCKSTATE();
 1790                 }
 1791                 if (ret == 0)
 1792                         ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
 1793                 if (ret) {
 1794                     /*
 1795                      * nfsrv_delegconflict unlocks state when it
 1796                      * returns non-zero, which it always does.
 1797                      */
 1798                     if (other_lop) {
 1799                         FREE((caddr_t)other_lop, M_NFSDLOCK);
 1800                         other_lop = NULL;
 1801                     }
 1802                     if (ret == -1) {
 1803                         lckstp = NULL;
 1804                         goto tryagain;
 1805                     }
 1806                     error = ret;
 1807                     goto out;
 1808                 }
 1809                 /* Never gets here. */
 1810             }
 1811             tstp = nstp;
 1812         }
 1813 
 1814         /*
 1815          * Handle the unlock case by calling nfsrv_updatelock().
 1816          * (Should I have done some access checking above for unlock? For now,
 1817          *  just let it happen.)
 1818          */
 1819         if (new_stp->ls_flags & NFSLCK_UNLOCK) {
 1820                 first = new_lop->lo_first;
 1821                 end = new_lop->lo_end;
 1822                 nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
 1823                 stateidp->seqid = ++(stp->ls_stateid.seqid);
 1824                 stateidp->other[0] = stp->ls_stateid.other[0];
 1825                 stateidp->other[1] = stp->ls_stateid.other[1];
 1826                 stateidp->other[2] = stp->ls_stateid.other[2];
 1827                 if (filestruct_locked != 0) {
 1828                         NFSUNLOCKSTATE();
 1829                         if (vnode_unlocked == 0) {
 1830                                 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
 1831                                 vnode_unlocked = 1;
 1832                                 NFSVOPUNLOCK(vp, 0);
 1833                         }
 1834                         /* Update the local locks. */
 1835                         nfsrv_localunlock(vp, lfp, first, end, p);
 1836                         NFSLOCKSTATE();
 1837                         nfsrv_unlocklf(lfp);
 1838                 }
 1839                 NFSUNLOCKSTATE();
 1840                 goto out;
 1841         }
 1842 
 1843         /*
 1844          * Search for a conflicting lock. A lock conflicts if:
 1845          * - the lock range overlaps and
 1846          * - at least one lock is a write lock and
 1847          * - it is not owned by the same lock owner
 1848          */
 1849         if (!delegation) {
 1850           LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
 1851             if (new_lop->lo_end > lop->lo_first &&
 1852                 new_lop->lo_first < lop->lo_end &&
 1853                 (new_lop->lo_flags == NFSLCK_WRITE ||
 1854                  lop->lo_flags == NFSLCK_WRITE) &&
 1855                 lckstp != lop->lo_stp &&
 1856                 (clp != lop->lo_stp->ls_clp ||
 1857                  lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
 1858                  NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
 1859                     lckstp->ls_ownerlen))) {
 1860                 if (other_lop) {
 1861                     FREE((caddr_t)other_lop, M_NFSDLOCK);
 1862                     other_lop = NULL;
 1863                 }
 1864                 if (vnode_unlocked != 0)
 1865                     ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
 1866                         NULL, p);
 1867                 else
 1868                     ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
 1869                         vp, p);
 1870                 if (ret == 1) {
 1871                     if (filestruct_locked != 0) {
 1872                         if (vnode_unlocked == 0) {
 1873                                 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
 1874                                 NFSVOPUNLOCK(vp, 0);
 1875                         }
 1876                         /* Roll back local locks. */
 1877                         nfsrv_locallock_rollback(vp, lfp, p);
 1878                         NFSLOCKSTATE();
 1879                         nfsrv_unlocklf(lfp);
 1880                         NFSUNLOCKSTATE();
 1881                         NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
 1882                         vnode_unlocked = 0;
 1883                         if ((vp->v_iflag & VI_DOOMED) != 0) {
 1884                                 error = NFSERR_SERVERFAULT;
 1885                                 goto out;
 1886                         }
 1887                     }
 1888                     /*
 1889                      * nfsrv_clientconflict() unlocks state when it
 1890                      * returns non-zero.
 1891                      */
 1892                     lckstp = NULL;
 1893                     goto tryagain;
 1894                 }
 1895                 /*
 1896                  * Found a conflicting lock, so record the conflict and
 1897                  * return the error.
 1898                  */
 1899                 if (cfp != NULL && ret == 0) {
 1900                     cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
 1901                     cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
 1902                     cfp->cl_first = lop->lo_first;
 1903                     cfp->cl_end = lop->lo_end;
 1904                     cfp->cl_flags = lop->lo_flags;
 1905                     cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
 1906                     NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
 1907                         cfp->cl_ownerlen);
 1908                 }
 1909                 if (ret == 2)
 1910                     error = NFSERR_PERM;
 1911                 else if (new_stp->ls_flags & NFSLCK_RECLAIM)
 1912                     error = NFSERR_RECLAIMCONFLICT;
 1913                 else if (new_stp->ls_flags & NFSLCK_CHECK)
 1914                     error = NFSERR_LOCKED;
 1915                 else
 1916                     error = NFSERR_DENIED;
 1917                 if (filestruct_locked != 0 && ret == 0) {
 1918                         /* Roll back local locks. */
 1919                         NFSUNLOCKSTATE();
 1920                         if (vnode_unlocked == 0) {
 1921                                 ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
 1922                                 vnode_unlocked = 1;
 1923                                 NFSVOPUNLOCK(vp, 0);
 1924                         }
 1925                         nfsrv_locallock_rollback(vp, lfp, p);
 1926                         NFSLOCKSTATE();
 1927                         nfsrv_unlocklf(lfp);
 1928                 }
 1929                 if (ret == 0)
 1930                         NFSUNLOCKSTATE();
 1931                 goto out;
 1932             }
 1933           }
 1934         }
 1935 
 1936         /*
 1937          * We only get here if there was no lock that conflicted.
 1938          */
 1939         if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
 1940                 NFSUNLOCKSTATE();
 1941                 goto out;
 1942         }
 1943 
 1944         /*
 1945          * We only get here when we are creating or modifying a lock.
 1946          * There are two variants:
 1947          * - exist_lock_owner where lock_owner exists
 1948          * - open_to_lock_owner with new lock_owner
 1949          */
 1950         first = new_lop->lo_first;
 1951         end = new_lop->lo_end;
 1952         lock_flags = new_lop->lo_flags;
 1953         if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
 1954                 nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
 1955                 stateidp->seqid = ++(lckstp->ls_stateid.seqid);
 1956                 stateidp->other[0] = lckstp->ls_stateid.other[0];
 1957                 stateidp->other[1] = lckstp->ls_stateid.other[1];
 1958                 stateidp->other[2] = lckstp->ls_stateid.other[2];
 1959         } else {
 1960                 /*
 1961                  * The new open_to_lock_owner case.
 1962                  * Link the new nfsstate into the lists.
 1963                  */
 1964                 new_stp->ls_seq = new_stp->ls_opentolockseq;
 1965                 nfsrvd_refcache(new_stp->ls_op);
 1966                 stateidp->seqid = new_stp->ls_stateid.seqid = 1;
 1967                 stateidp->other[0] = new_stp->ls_stateid.other[0] =
 1968                     clp->lc_clientid.lval[0];
 1969                 stateidp->other[1] = new_stp->ls_stateid.other[1] =
 1970                     clp->lc_clientid.lval[1];
 1971                 stateidp->other[2] = new_stp->ls_stateid.other[2] =
 1972                     nfsrv_nextstateindex(clp);
 1973                 new_stp->ls_clp = clp;
 1974                 LIST_INIT(&new_stp->ls_lock);
 1975                 new_stp->ls_openstp = stp;
 1976                 new_stp->ls_lfp = lfp;
 1977                 nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
 1978                     lfp);
 1979                 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
 1980                     new_stp, ls_hash);
 1981                 LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
 1982                 *new_lopp = NULL;
 1983                 *new_stpp = NULL;
 1984                 newnfsstats.srvlockowners++;
 1985                 nfsrv_openpluslock++;
 1986         }
 1987         if (filestruct_locked != 0) {
 1988                 NFSUNLOCKSTATE();
 1989                 nfsrv_locallock_commit(lfp, lock_flags, first, end);
 1990                 NFSLOCKSTATE();
 1991                 nfsrv_unlocklf(lfp);
 1992         }
 1993         NFSUNLOCKSTATE();
 1994 
 1995 out:
 1996         if (haslock) {
 1997                 NFSLOCKV4ROOTMUTEX();
 1998                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
 1999                 NFSUNLOCKV4ROOTMUTEX();
 2000         }
 2001         if (vnode_unlocked != 0) {
 2002                 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
 2003                 if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
 2004                         error = NFSERR_SERVERFAULT;
 2005         }
 2006         if (other_lop)
 2007                 FREE((caddr_t)other_lop, M_NFSDLOCK);
 2008         NFSEXITCODE2(error, nd);
 2009         return (error);
 2010 }
 2011 
 2012 /*
 2013  * Check for state errors for Open.
 2014  * repstat is passed back out as an error if more critical errors
 2015  * are not detected.
 2016  */
 2017 APPLESTATIC int
 2018 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
 2019     struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
 2020     NFSPROC_T *p, int repstat)
 2021 {
 2022         struct nfsstate *stp, *nstp;
 2023         struct nfsclient *clp;
 2024         struct nfsstate *ownerstp;
 2025         struct nfslockfile *lfp, *new_lfp;
 2026         int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
 2027 
 2028         if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
 2029                 readonly = 1;
 2030         /*
 2031          * Check for restart conditions (client and server).
 2032          */
 2033         error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
 2034                 &new_stp->ls_stateid, 0);
 2035         if (error)
 2036                 goto out;
 2037 
 2038         /*
 2039          * Check for state resource limit exceeded.
 2040          * Technically this should be SMP protected, but the worst
 2041          * case error is "out by one or two" on the count when it
 2042          * returns NFSERR_RESOURCE and the limit is just a rather
 2043          * arbitrary high water mark, so no harm is done.
 2044          */
 2045         if (nfsrv_openpluslock > NFSRV_V4STATELIMIT) {
 2046                 error = NFSERR_RESOURCE;
 2047                 goto out;
 2048         }
 2049 
 2050 tryagain:
 2051         MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
 2052             M_NFSDLOCKFILE, M_WAITOK);
 2053         if (vp)
 2054                 getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
 2055                     NULL, p);
 2056         NFSLOCKSTATE();
 2057         /*
 2058          * Get the nfsclient structure.
 2059          */
 2060         error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
 2061             (nfsquad_t)((u_quad_t)0), NULL, p);
 2062 
 2063         /*
 2064          * Look up the open owner. See if it needs confirmation and
 2065          * check the seq#, as required.
 2066          */
 2067         if (!error)
 2068                 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
 2069 
 2070         if (!error && ownerstp) {
 2071                 error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
 2072                     new_stp->ls_op);
 2073                 /*
 2074                  * If the OpenOwner hasn't been confirmed, assume the
 2075                  * old one was a replay and this one is ok.
 2076                  * See: RFC3530 Sec. 14.2.18.
 2077                  */
 2078                 if (error == NFSERR_BADSEQID &&
 2079                     (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
 2080                         error = 0;
 2081         }
 2082 
 2083         /*
 2084          * Check for grace.
 2085          */
 2086         if (!error)
 2087                 error = nfsrv_checkgrace(new_stp->ls_flags);
 2088         if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
 2089                 nfsrv_checkstable(clp))
 2090                 error = NFSERR_NOGRACE;
 2091 
 2092         /*
 2093          * If none of the above errors occurred, let repstat be
 2094          * returned.
 2095          */
 2096         if (repstat && !error)
 2097                 error = repstat;
 2098         if (error) {
 2099                 NFSUNLOCKSTATE();
 2100                 if (haslock) {
 2101                         NFSLOCKV4ROOTMUTEX();
 2102                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2103                         NFSUNLOCKV4ROOTMUTEX();
 2104                 }
 2105                 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
 2106                 goto out;
 2107         }
 2108 
 2109         /*
 2110          * If vp == NULL, the file doesn't exist yet, so return ok.
 2111          * (This always happens on the first pass, so haslock must be 0.)
 2112          */
 2113         if (vp == NULL) {
 2114                 NFSUNLOCKSTATE();
 2115                 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
 2116                 goto out;
 2117         }
 2118 
 2119         /*
 2120          * Get the structure for the underlying file.
 2121          */
 2122         if (getfhret)
 2123                 error = getfhret;
 2124         else
 2125                 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
 2126                     NULL, 0);
 2127         if (new_lfp)
 2128                 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
 2129         if (error) {
 2130                 NFSUNLOCKSTATE();
 2131                 if (haslock) {
 2132                         NFSLOCKV4ROOTMUTEX();
 2133                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2134                         NFSUNLOCKV4ROOTMUTEX();
 2135                 }
 2136                 goto out;
 2137         }
 2138 
 2139         /*
 2140          * Search for a conflicting open/share.
 2141          */
 2142         if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
 2143             /*
 2144              * For Delegate_Cur, search for the matching Delegation,
 2145              * which indicates no conflict.
 2146              * An old delegation should have been recovered by the
 2147              * client doing a Claim_DELEGATE_Prev, so I won't let
 2148              * it match and return NFSERR_EXPIRED. Should I let it
 2149              * match?
 2150              */
 2151             LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
 2152                 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
 2153                     stateidp->seqid == stp->ls_stateid.seqid &&
 2154                     !NFSBCMP(stateidp->other, stp->ls_stateid.other,
 2155                           NFSX_STATEIDOTHER))
 2156                         break;
 2157             }
 2158             if (stp == LIST_END(&lfp->lf_deleg) ||
 2159                 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
 2160                  (stp->ls_flags & NFSLCK_DELEGREAD))) {
 2161                 NFSUNLOCKSTATE();
 2162                 if (haslock) {
 2163                         NFSLOCKV4ROOTMUTEX();
 2164                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2165                         NFSUNLOCKV4ROOTMUTEX();
 2166                 }
 2167                 error = NFSERR_EXPIRED;
 2168                 goto out;
 2169             }
 2170         }
 2171 
 2172         /*
 2173          * Check for access/deny bit conflicts. I check for the same
 2174          * owner as well, in case the client didn't bother.
 2175          */
 2176         LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
 2177                 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
 2178                     (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
 2179                       ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
 2180                      ((stp->ls_flags & NFSLCK_ACCESSBITS) &
 2181                       ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
 2182                         ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
 2183                         if (ret == 1) {
 2184                                 /*
 2185                                  * nfsrv_clientconflict() unlocks
 2186                                  * state when it returns non-zero.
 2187                                  */
 2188                                 goto tryagain;
 2189                         }
 2190                         if (ret == 2)
 2191                                 error = NFSERR_PERM;
 2192                         else if (new_stp->ls_flags & NFSLCK_RECLAIM)
 2193                                 error = NFSERR_RECLAIMCONFLICT;
 2194                         else
 2195                                 error = NFSERR_SHAREDENIED;
 2196                         if (ret == 0)
 2197                                 NFSUNLOCKSTATE();
 2198                         if (haslock) {
 2199                                 NFSLOCKV4ROOTMUTEX();
 2200                                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2201                                 NFSUNLOCKV4ROOTMUTEX();
 2202                         }
 2203                         goto out;
 2204                 }
 2205         }
 2206 
 2207         /*
 2208          * Check for a conflicting delegation. If one is found, call
 2209          * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
 2210          * been set yet, it will get the lock. Otherwise, it will recall
 2211          * the delegation. Then, we try try again...
 2212          * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
 2213          *  isn't a conflict.)
 2214          * I currently believe the conflict algorithm to be:
 2215          * For Open with Read Access and Deny None
 2216          * - there is a conflict iff a different client has a write delegation
 2217          * For Open with other Write Access or any Deny except None
 2218          * - there is a conflict if a different client has any delegation
 2219          * - there is a conflict if the same client has a read delegation
 2220          *   (The current concensus is that this last case should be
 2221          *    considered a conflict since the client with a read delegation
 2222          *    could have done an Open with ReadAccess and WriteDeny
 2223          *    locally and then not have checked for the WriteDeny.)
 2224          * Don't check for a Reclaim, since that will be dealt with
 2225          * by nfsrv_openctrl().
 2226          */
 2227         if (!(new_stp->ls_flags &
 2228                 (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
 2229             stp = LIST_FIRST(&lfp->lf_deleg);
 2230             while (stp != LIST_END(&lfp->lf_deleg)) {
 2231                 nstp = LIST_NEXT(stp, ls_file);
 2232                 if ((readonly && stp->ls_clp != clp &&
 2233                        (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
 2234                     (!readonly && (stp->ls_clp != clp ||
 2235                          (stp->ls_flags & NFSLCK_DELEGREAD)))) {
 2236                         ret = nfsrv_delegconflict(stp, &haslock, p, vp);
 2237                         if (ret) {
 2238                             /*
 2239                              * nfsrv_delegconflict() unlocks state
 2240                              * when it returns non-zero.
 2241                              */
 2242                             if (ret == -1)
 2243                                 goto tryagain;
 2244                             error = ret;
 2245                             goto out;
 2246                         }
 2247                 }
 2248                 stp = nstp;
 2249             }
 2250         }
 2251         NFSUNLOCKSTATE();
 2252         if (haslock) {
 2253                 NFSLOCKV4ROOTMUTEX();
 2254                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2255                 NFSUNLOCKV4ROOTMUTEX();
 2256         }
 2257 
 2258 out:
 2259         NFSEXITCODE2(error, nd);
 2260         return (error);
 2261 }
 2262 
 2263 /*
 2264  * Open control function to create/update open state for an open.
 2265  */
 2266 APPLESTATIC int
 2267 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
 2268     struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
 2269     nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
 2270     NFSPROC_T *p, u_quad_t filerev)
 2271 {
 2272         struct nfsstate *new_stp = *new_stpp;
 2273         struct nfsstate *stp, *nstp;
 2274         struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
 2275         struct nfslockfile *lfp, *new_lfp;
 2276         struct nfsclient *clp;
 2277         int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
 2278         int readonly = 0, cbret = 1, getfhret = 0;
 2279 
 2280         if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
 2281                 readonly = 1;
 2282         /*
 2283          * Check for restart conditions (client and server).
 2284          * (Paranoia, should have been detected by nfsrv_opencheck().)
 2285          * If an error does show up, return NFSERR_EXPIRED, since the
 2286          * the seqid# has already been incremented.
 2287          */
 2288         error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
 2289             &new_stp->ls_stateid, 0);
 2290         if (error) {
 2291                 printf("Nfsd: openctrl unexpected restart err=%d\n",
 2292                     error);
 2293                 error = NFSERR_EXPIRED;
 2294                 goto out;
 2295         }
 2296 
 2297 tryagain:
 2298         MALLOC(new_lfp, struct nfslockfile *, sizeof (struct nfslockfile),
 2299             M_NFSDLOCKFILE, M_WAITOK);
 2300         MALLOC(new_open, struct nfsstate *, sizeof (struct nfsstate),
 2301             M_NFSDSTATE, M_WAITOK);
 2302         MALLOC(new_deleg, struct nfsstate *, sizeof (struct nfsstate),
 2303             M_NFSDSTATE, M_WAITOK);
 2304         getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
 2305             NULL, p);
 2306         NFSLOCKSTATE();
 2307         /*
 2308          * Get the client structure. Since the linked lists could be changed
 2309          * by other nfsd processes if this process does a tsleep(), one of
 2310          * two things must be done.
 2311          * 1 - don't tsleep()
 2312          * or
 2313          * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
 2314          *     before using the lists, since this lock stops the other
 2315          *     nfsd. This should only be used for rare cases, since it
 2316          *     essentially single threads the nfsd.
 2317          *     At this time, it is only done for cases where the stable
 2318          *     storage file must be written prior to completion of state
 2319          *     expiration.
 2320          */
 2321         error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
 2322             (nfsquad_t)((u_quad_t)0), NULL, p);
 2323         if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
 2324             clp->lc_program) {
 2325                 /*
 2326                  * This happens on the first open for a client
 2327                  * that supports callbacks.
 2328                  */
 2329                 NFSUNLOCKSTATE();
 2330                 /*
 2331                  * Although nfsrv_docallback() will sleep, clp won't
 2332                  * go away, since they are only removed when the
 2333                  * nfsv4_lock() has blocked the nfsd threads. The
 2334                  * fields in clp can change, but having multiple
 2335                  * threads do this Null callback RPC should be
 2336                  * harmless.
 2337                  */
 2338                 cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
 2339                     NULL, 0, NULL, NULL, NULL, p);
 2340                 NFSLOCKSTATE();
 2341                 clp->lc_flags &= ~LCL_NEEDSCBNULL;
 2342                 if (!cbret)
 2343                         clp->lc_flags |= LCL_CALLBACKSON;
 2344         }
 2345 
 2346         /*
 2347          * Look up the open owner. See if it needs confirmation and
 2348          * check the seq#, as required.
 2349          */
 2350         if (!error)
 2351                 nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
 2352 
 2353         if (error) {
 2354                 NFSUNLOCKSTATE();
 2355                 printf("Nfsd: openctrl unexpected state err=%d\n",
 2356                         error);
 2357                 free((caddr_t)new_lfp, M_NFSDLOCKFILE);
 2358                 free((caddr_t)new_open, M_NFSDSTATE);
 2359                 free((caddr_t)new_deleg, M_NFSDSTATE);
 2360                 if (haslock) {
 2361                         NFSLOCKV4ROOTMUTEX();
 2362                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2363                         NFSUNLOCKV4ROOTMUTEX();
 2364                 }
 2365                 error = NFSERR_EXPIRED;
 2366                 goto out;
 2367         }
 2368 
 2369         if (new_stp->ls_flags & NFSLCK_RECLAIM)
 2370                 nfsrv_markstable(clp);
 2371 
 2372         /*
 2373          * Get the structure for the underlying file.
 2374          */
 2375         if (getfhret)
 2376                 error = getfhret;
 2377         else
 2378                 error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
 2379                     NULL, 0);
 2380         if (new_lfp)
 2381                 FREE((caddr_t)new_lfp, M_NFSDLOCKFILE);
 2382         if (error) {
 2383                 NFSUNLOCKSTATE();
 2384                 printf("Nfsd openctrl unexpected getlockfile err=%d\n",
 2385                     error);
 2386                 free((caddr_t)new_open, M_NFSDSTATE);
 2387                 free((caddr_t)new_deleg, M_NFSDSTATE);
 2388                 if (haslock) {
 2389                         NFSLOCKV4ROOTMUTEX();
 2390                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2391                         NFSUNLOCKV4ROOTMUTEX();
 2392                 }
 2393                 goto out;
 2394         }
 2395 
 2396         /*
 2397          * Search for a conflicting open/share.
 2398          */
 2399         if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
 2400             /*
 2401              * For Delegate_Cur, search for the matching Delegation,
 2402              * which indicates no conflict.
 2403              * An old delegation should have been recovered by the
 2404              * client doing a Claim_DELEGATE_Prev, so I won't let
 2405              * it match and return NFSERR_EXPIRED. Should I let it
 2406              * match?
 2407              */
 2408             LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
 2409                 if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
 2410                     stateidp->seqid == stp->ls_stateid.seqid &&
 2411                     !NFSBCMP(stateidp->other, stp->ls_stateid.other,
 2412                         NFSX_STATEIDOTHER))
 2413                         break;
 2414             }
 2415             if (stp == LIST_END(&lfp->lf_deleg) ||
 2416                 ((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
 2417                  (stp->ls_flags & NFSLCK_DELEGREAD))) {
 2418                 NFSUNLOCKSTATE();
 2419                 printf("Nfsd openctrl unexpected expiry\n");
 2420                 free((caddr_t)new_open, M_NFSDSTATE);
 2421                 free((caddr_t)new_deleg, M_NFSDSTATE);
 2422                 if (haslock) {
 2423                         NFSLOCKV4ROOTMUTEX();
 2424                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2425                         NFSUNLOCKV4ROOTMUTEX();
 2426                 }
 2427                 error = NFSERR_EXPIRED;
 2428                 goto out;
 2429             }
 2430 
 2431             /*
 2432              * Don't issue a Delegation, since one already exists and
 2433              * delay delegation timeout, as required.
 2434              */
 2435             delegate = 0;
 2436             nfsrv_delaydelegtimeout(stp);
 2437         }
 2438 
 2439         /*
 2440          * Check for access/deny bit conflicts. I also check for the
 2441          * same owner, since the client might not have bothered to check.
 2442          * Also, note an open for the same file and owner, if found,
 2443          * which is all we do here for Delegate_Cur, since conflict
 2444          * checking is already done.
 2445          */
 2446         LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
 2447                 if (ownerstp && stp->ls_openowner == ownerstp)
 2448                         openstp = stp;
 2449                 if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
 2450                     /*
 2451                      * If another client has the file open, the only
 2452                      * delegation that can be issued is a Read delegation
 2453                      * and only if it is a Read open with Deny none.
 2454                      */
 2455                     if (clp != stp->ls_clp) {
 2456                         if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
 2457                             NFSLCK_READACCESS)
 2458                             writedeleg = 0;
 2459                         else
 2460                             delegate = 0;
 2461                     }
 2462                     if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
 2463                         ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
 2464                        ((stp->ls_flags & NFSLCK_ACCESSBITS) &
 2465                         ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
 2466                         ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
 2467                         if (ret == 1) {
 2468                                 /*
 2469                                  * nfsrv_clientconflict() unlocks state
 2470                                  * when it returns non-zero.
 2471                                  */
 2472                                 free((caddr_t)new_open, M_NFSDSTATE);
 2473                                 free((caddr_t)new_deleg, M_NFSDSTATE);
 2474                                 openstp = NULL;
 2475                                 goto tryagain;
 2476                         }
 2477                         if (ret == 2)
 2478                                 error = NFSERR_PERM;
 2479                         else if (new_stp->ls_flags & NFSLCK_RECLAIM)
 2480                                 error = NFSERR_RECLAIMCONFLICT;
 2481                         else
 2482                                 error = NFSERR_SHAREDENIED;
 2483                         if (ret == 0)
 2484                                 NFSUNLOCKSTATE();
 2485                         if (haslock) {
 2486                                 NFSLOCKV4ROOTMUTEX();
 2487                                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2488                                 NFSUNLOCKV4ROOTMUTEX();
 2489                         }
 2490                         free((caddr_t)new_open, M_NFSDSTATE);
 2491                         free((caddr_t)new_deleg, M_NFSDSTATE);
 2492                         printf("nfsd openctrl unexpected client cnfl\n");
 2493                         goto out;
 2494                     }
 2495                 }
 2496         }
 2497 
 2498         /*
 2499          * Check for a conflicting delegation. If one is found, call
 2500          * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
 2501          * been set yet, it will get the lock. Otherwise, it will recall
 2502          * the delegation. Then, we try try again...
 2503          * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
 2504          *  isn't a conflict.)
 2505          * I currently believe the conflict algorithm to be:
 2506          * For Open with Read Access and Deny None
 2507          * - there is a conflict iff a different client has a write delegation
 2508          * For Open with other Write Access or any Deny except None
 2509          * - there is a conflict if a different client has any delegation
 2510          * - there is a conflict if the same client has a read delegation
 2511          *   (The current concensus is that this last case should be
 2512          *    considered a conflict since the client with a read delegation
 2513          *    could have done an Open with ReadAccess and WriteDeny
 2514          *    locally and then not have checked for the WriteDeny.)
 2515          */
 2516         if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
 2517             stp = LIST_FIRST(&lfp->lf_deleg);
 2518             while (stp != LIST_END(&lfp->lf_deleg)) {
 2519                 nstp = LIST_NEXT(stp, ls_file);
 2520                 if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
 2521                         writedeleg = 0;
 2522                 else
 2523                         delegate = 0;
 2524                 if ((readonly && stp->ls_clp != clp &&
 2525                        (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
 2526                     (!readonly && (stp->ls_clp != clp ||
 2527                          (stp->ls_flags & NFSLCK_DELEGREAD)))) {
 2528                     if (new_stp->ls_flags & NFSLCK_RECLAIM) {
 2529                         delegate = 2;
 2530                     } else {
 2531                         ret = nfsrv_delegconflict(stp, &haslock, p, vp);
 2532                         if (ret) {
 2533                             /*
 2534                              * nfsrv_delegconflict() unlocks state
 2535                              * when it returns non-zero.
 2536                              */
 2537                             printf("Nfsd openctrl unexpected deleg cnfl\n");
 2538                             free((caddr_t)new_open, M_NFSDSTATE);
 2539                             free((caddr_t)new_deleg, M_NFSDSTATE);
 2540                             if (ret == -1) {
 2541                                 openstp = NULL;
 2542                                 goto tryagain;
 2543                             }
 2544                             error = ret;
 2545                             goto out;
 2546                         }
 2547                     }
 2548                 }
 2549                 stp = nstp;
 2550             }
 2551         }
 2552 
 2553         /*
 2554          * We only get here if there was no open that conflicted.
 2555          * If an open for the owner exists, or in the access/deny bits.
 2556          * Otherwise it is a new open. If the open_owner hasn't been
 2557          * confirmed, replace the open with the new one needing confirmation,
 2558          * otherwise add the open.
 2559          */
 2560         if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
 2561             /*
 2562              * Handle NFSLCK_DELEGPREV by searching the old delegations for
 2563              * a match. If found, just move the old delegation to the current
 2564              * delegation list and issue open. If not found, return
 2565              * NFSERR_EXPIRED.
 2566              */
 2567             LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
 2568                 if (stp->ls_lfp == lfp) {
 2569                     /* Found it */
 2570                     if (stp->ls_clp != clp)
 2571                         panic("olddeleg clp");
 2572                     LIST_REMOVE(stp, ls_list);
 2573                     LIST_REMOVE(stp, ls_hash);
 2574                     stp->ls_flags &= ~NFSLCK_OLDDELEG;
 2575                     stp->ls_stateid.seqid = delegstateidp->seqid = 0;
 2576                     stp->ls_stateid.other[0] = delegstateidp->other[0] =
 2577                         clp->lc_clientid.lval[0];
 2578                     stp->ls_stateid.other[1] = delegstateidp->other[1] =
 2579                         clp->lc_clientid.lval[1];
 2580                     stp->ls_stateid.other[2] = delegstateidp->other[2] =
 2581                         nfsrv_nextstateindex(clp);
 2582                     stp->ls_compref = nd->nd_compref;
 2583                     LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
 2584                     LIST_INSERT_HEAD(NFSSTATEHASH(clp,
 2585                         stp->ls_stateid), stp, ls_hash);
 2586                     if (stp->ls_flags & NFSLCK_DELEGWRITE)
 2587                         *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
 2588                     else
 2589                         *rflagsp |= NFSV4OPEN_READDELEGATE;
 2590                     clp->lc_delegtime = NFSD_MONOSEC +
 2591                         nfsrv_lease + NFSRV_LEASEDELTA;
 2592 
 2593                     /*
 2594                      * Now, do the associated open.
 2595                      */
 2596                     new_open->ls_stateid.seqid = 0;
 2597                     new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
 2598                     new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
 2599                     new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
 2600                     new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
 2601                         NFSLCK_OPEN;
 2602                     if (stp->ls_flags & NFSLCK_DELEGWRITE)
 2603                         new_open->ls_flags |= (NFSLCK_READACCESS |
 2604                             NFSLCK_WRITEACCESS);
 2605                     else
 2606                         new_open->ls_flags |= NFSLCK_READACCESS;
 2607                     new_open->ls_uid = new_stp->ls_uid;
 2608                     new_open->ls_lfp = lfp;
 2609                     new_open->ls_clp = clp;
 2610                     LIST_INIT(&new_open->ls_open);
 2611                     LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
 2612                     LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
 2613                         new_open, ls_hash);
 2614                     /*
 2615                      * and handle the open owner
 2616                      */
 2617                     if (ownerstp) {
 2618                         new_open->ls_openowner = ownerstp;
 2619                         LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
 2620                     } else {
 2621                         new_open->ls_openowner = new_stp;
 2622                         new_stp->ls_flags = 0;
 2623                         nfsrvd_refcache(new_stp->ls_op);
 2624                         new_stp->ls_noopens = 0;
 2625                         LIST_INIT(&new_stp->ls_open);
 2626                         LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
 2627                         LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
 2628                         *new_stpp = NULL;
 2629                         newnfsstats.srvopenowners++;
 2630                         nfsrv_openpluslock++;
 2631                     }
 2632                     openstp = new_open;
 2633                     new_open = NULL;
 2634                     newnfsstats.srvopens++;
 2635                     nfsrv_openpluslock++;
 2636                     break;
 2637                 }
 2638             }
 2639             if (stp == LIST_END(&clp->lc_olddeleg))
 2640                 error = NFSERR_EXPIRED;
 2641         } else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
 2642             /*
 2643              * Scan to see that no delegation for this client and file
 2644              * doesn't already exist.
 2645              * There also shouldn't yet be an Open for this file and
 2646              * openowner.
 2647              */
 2648             LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
 2649                 if (stp->ls_clp == clp)
 2650                     break;
 2651             }
 2652             if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
 2653                 /*
 2654                  * This is the Claim_Previous case with a delegation
 2655                  * type != Delegate_None.
 2656                  */
 2657                 /*
 2658                  * First, add the delegation. (Although we must issue the
 2659                  * delegation, we can also ask for an immediate return.)
 2660                  */
 2661                 new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
 2662                 new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
 2663                     clp->lc_clientid.lval[0];
 2664                 new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
 2665                     clp->lc_clientid.lval[1];
 2666                 new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
 2667                     nfsrv_nextstateindex(clp);
 2668                 if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
 2669                     new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
 2670                         NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
 2671                     *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
 2672                 } else {
 2673                     new_deleg->ls_flags = (NFSLCK_DELEGREAD |
 2674                         NFSLCK_READACCESS);
 2675                     *rflagsp |= NFSV4OPEN_READDELEGATE;
 2676                 }
 2677                 new_deleg->ls_uid = new_stp->ls_uid;
 2678                 new_deleg->ls_lfp = lfp;
 2679                 new_deleg->ls_clp = clp;
 2680                 new_deleg->ls_filerev = filerev;
 2681                 new_deleg->ls_compref = nd->nd_compref;
 2682                 LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
 2683                 LIST_INSERT_HEAD(NFSSTATEHASH(clp,
 2684                     new_deleg->ls_stateid), new_deleg, ls_hash);
 2685                 LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
 2686                 new_deleg = NULL;
 2687                 if (delegate == 2 || nfsrv_issuedelegs == 0 ||
 2688                     (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
 2689                      LCL_CALLBACKSON ||
 2690                     NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
 2691                     !NFSVNO_DELEGOK(vp))
 2692                     *rflagsp |= NFSV4OPEN_RECALL;
 2693                 newnfsstats.srvdelegates++;
 2694                 nfsrv_openpluslock++;
 2695                 nfsrv_delegatecnt++;
 2696 
 2697                 /*
 2698                  * Now, do the associated open.
 2699                  */
 2700                 new_open->ls_stateid.seqid = 0;
 2701                 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
 2702                 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
 2703                 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
 2704                 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
 2705                     NFSLCK_OPEN;
 2706                 if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
 2707                         new_open->ls_flags |= (NFSLCK_READACCESS |
 2708                             NFSLCK_WRITEACCESS);
 2709                 else
 2710                         new_open->ls_flags |= NFSLCK_READACCESS;
 2711                 new_open->ls_uid = new_stp->ls_uid;
 2712                 new_open->ls_lfp = lfp;
 2713                 new_open->ls_clp = clp;
 2714                 LIST_INIT(&new_open->ls_open);
 2715                 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
 2716                 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
 2717                    new_open, ls_hash);
 2718                 /*
 2719                  * and handle the open owner
 2720                  */
 2721                 if (ownerstp) {
 2722                     new_open->ls_openowner = ownerstp;
 2723                     LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
 2724                 } else {
 2725                     new_open->ls_openowner = new_stp;
 2726                     new_stp->ls_flags = 0;
 2727                     nfsrvd_refcache(new_stp->ls_op);
 2728                     new_stp->ls_noopens = 0;
 2729                     LIST_INIT(&new_stp->ls_open);
 2730                     LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
 2731                     LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
 2732                     *new_stpp = NULL;
 2733                     newnfsstats.srvopenowners++;
 2734                     nfsrv_openpluslock++;
 2735                 }
 2736                 openstp = new_open;
 2737                 new_open = NULL;
 2738                 newnfsstats.srvopens++;
 2739                 nfsrv_openpluslock++;
 2740             } else {
 2741                 error = NFSERR_RECLAIMCONFLICT;
 2742             }
 2743         } else if (ownerstp) {
 2744                 if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
 2745                     /* Replace the open */
 2746                     if (ownerstp->ls_op)
 2747                         nfsrvd_derefcache(ownerstp->ls_op);
 2748                     ownerstp->ls_op = new_stp->ls_op;
 2749                     nfsrvd_refcache(ownerstp->ls_op);
 2750                     ownerstp->ls_seq = new_stp->ls_seq;
 2751                     *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
 2752                     stp = LIST_FIRST(&ownerstp->ls_open);
 2753                     stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
 2754                         NFSLCK_OPEN;
 2755                     stp->ls_stateid.seqid = 0;
 2756                     stp->ls_uid = new_stp->ls_uid;
 2757                     if (lfp != stp->ls_lfp) {
 2758                         LIST_REMOVE(stp, ls_file);
 2759                         LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
 2760                         stp->ls_lfp = lfp;
 2761                     }
 2762                     openstp = stp;
 2763                 } else if (openstp) {
 2764                     openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
 2765                     openstp->ls_stateid.seqid++;
 2766 
 2767                     /*
 2768                      * This is where we can choose to issue a delegation.
 2769                      */
 2770                     if (delegate && nfsrv_issuedelegs &&
 2771                         writedeleg && !NFSVNO_EXRDONLY(exp) &&
 2772                         (nfsrv_writedelegifpos || !readonly) &&
 2773                         (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
 2774                          LCL_CALLBACKSON &&
 2775                         !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
 2776                         NFSVNO_DELEGOK(vp)) {
 2777                         new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
 2778                         new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
 2779                             = clp->lc_clientid.lval[0];
 2780                         new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
 2781                             = clp->lc_clientid.lval[1];
 2782                         new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
 2783                             = nfsrv_nextstateindex(clp);
 2784                         new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
 2785                             NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
 2786                         *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
 2787                         new_deleg->ls_uid = new_stp->ls_uid;
 2788                         new_deleg->ls_lfp = lfp;
 2789                         new_deleg->ls_clp = clp;
 2790                         new_deleg->ls_filerev = filerev;
 2791                         new_deleg->ls_compref = nd->nd_compref;
 2792                         LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
 2793                         LIST_INSERT_HEAD(NFSSTATEHASH(clp,
 2794                             new_deleg->ls_stateid), new_deleg, ls_hash);
 2795                         LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
 2796                         new_deleg = NULL;
 2797                         newnfsstats.srvdelegates++;
 2798                         nfsrv_openpluslock++;
 2799                         nfsrv_delegatecnt++;
 2800                     }
 2801                 } else {
 2802                     new_open->ls_stateid.seqid = 0;
 2803                     new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
 2804                     new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
 2805                     new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
 2806                     new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
 2807                         NFSLCK_OPEN;
 2808                     new_open->ls_uid = new_stp->ls_uid;
 2809                     new_open->ls_openowner = ownerstp;
 2810                     new_open->ls_lfp = lfp;
 2811                     new_open->ls_clp = clp;
 2812                     LIST_INIT(&new_open->ls_open);
 2813                     LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
 2814                     LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
 2815                     LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
 2816                         new_open, ls_hash);
 2817                     openstp = new_open;
 2818                     new_open = NULL;
 2819                     newnfsstats.srvopens++;
 2820                     nfsrv_openpluslock++;
 2821 
 2822                     /*
 2823                      * This is where we can choose to issue a delegation.
 2824                      */
 2825                     if (delegate && nfsrv_issuedelegs &&
 2826                         (writedeleg || readonly) &&
 2827                         (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
 2828                          LCL_CALLBACKSON &&
 2829                         !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
 2830                         NFSVNO_DELEGOK(vp)) {
 2831                         new_deleg->ls_stateid.seqid = delegstateidp->seqid = 0;
 2832                         new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
 2833                             = clp->lc_clientid.lval[0];
 2834                         new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
 2835                             = clp->lc_clientid.lval[1];
 2836                         new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
 2837                             = nfsrv_nextstateindex(clp);
 2838                         if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
 2839                             (nfsrv_writedelegifpos || !readonly)) {
 2840                             new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
 2841                                 NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
 2842                             *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
 2843                         } else {
 2844                             new_deleg->ls_flags = (NFSLCK_DELEGREAD |
 2845                                 NFSLCK_READACCESS);
 2846                             *rflagsp |= NFSV4OPEN_READDELEGATE;
 2847                         }
 2848                         new_deleg->ls_uid = new_stp->ls_uid;
 2849                         new_deleg->ls_lfp = lfp;
 2850                         new_deleg->ls_clp = clp;
 2851                         new_deleg->ls_filerev = filerev;
 2852                         new_deleg->ls_compref = nd->nd_compref;
 2853                         LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
 2854                         LIST_INSERT_HEAD(NFSSTATEHASH(clp,
 2855                             new_deleg->ls_stateid), new_deleg, ls_hash);
 2856                         LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
 2857                         new_deleg = NULL;
 2858                         newnfsstats.srvdelegates++;
 2859                         nfsrv_openpluslock++;
 2860                         nfsrv_delegatecnt++;
 2861                     }
 2862                 }
 2863         } else {
 2864                 /*
 2865                  * New owner case. Start the open_owner sequence with a
 2866                  * Needs confirmation (unless a reclaim) and hang the
 2867                  * new open off it.
 2868                  */
 2869                 new_open->ls_stateid.seqid = 0;
 2870                 new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
 2871                 new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
 2872                 new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
 2873                 new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
 2874                     NFSLCK_OPEN;
 2875                 new_open->ls_uid = new_stp->ls_uid;
 2876                 LIST_INIT(&new_open->ls_open);
 2877                 new_open->ls_openowner = new_stp;
 2878                 new_open->ls_lfp = lfp;
 2879                 new_open->ls_clp = clp;
 2880                 LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
 2881                 if (new_stp->ls_flags & NFSLCK_RECLAIM) {
 2882                         new_stp->ls_flags = 0;
 2883                 } else {
 2884                         *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
 2885                         new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
 2886                 }
 2887                 nfsrvd_refcache(new_stp->ls_op);
 2888                 new_stp->ls_noopens = 0;
 2889                 LIST_INIT(&new_stp->ls_open);
 2890                 LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
 2891                 LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
 2892                 LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
 2893                     new_open, ls_hash);
 2894                 openstp = new_open;
 2895                 new_open = NULL;
 2896                 *new_stpp = NULL;
 2897                 newnfsstats.srvopens++;
 2898                 nfsrv_openpluslock++;
 2899                 newnfsstats.srvopenowners++;
 2900                 nfsrv_openpluslock++;
 2901         }
 2902         if (!error) {
 2903                 stateidp->seqid = openstp->ls_stateid.seqid;
 2904                 stateidp->other[0] = openstp->ls_stateid.other[0];
 2905                 stateidp->other[1] = openstp->ls_stateid.other[1];
 2906                 stateidp->other[2] = openstp->ls_stateid.other[2];
 2907         }
 2908         NFSUNLOCKSTATE();
 2909         if (haslock) {
 2910                 NFSLOCKV4ROOTMUTEX();
 2911                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
 2912                 NFSUNLOCKV4ROOTMUTEX();
 2913         }
 2914         if (new_open)
 2915                 FREE((caddr_t)new_open, M_NFSDSTATE);
 2916         if (new_deleg)
 2917                 FREE((caddr_t)new_deleg, M_NFSDSTATE);
 2918 
 2919 out:
 2920         NFSEXITCODE2(error, nd);
 2921         return (error);
 2922 }
 2923 
 2924 /*
 2925  * Open update. Does the confirm, downgrade and close.
 2926  */
 2927 APPLESTATIC int
 2928 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
 2929     nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p)
 2930 {
 2931         struct nfsstate *stp, *ownerstp;
 2932         struct nfsclient *clp;
 2933         struct nfslockfile *lfp;
 2934         u_int32_t bits;
 2935         int error = 0, gotstate = 0, len = 0;
 2936         u_char client[NFSV4_OPAQUELIMIT];
 2937 
 2938         /*
 2939          * Check for restart conditions (client and server).
 2940          */
 2941         error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
 2942             &new_stp->ls_stateid, 0);
 2943         if (error)
 2944                 goto out;
 2945 
 2946         NFSLOCKSTATE();
 2947         /*
 2948          * Get the open structure via clientid and stateid.
 2949          */
 2950         error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
 2951             (nfsquad_t)((u_quad_t)0), NULL, p);
 2952         if (!error)
 2953                 error = nfsrv_getstate(clp, &new_stp->ls_stateid,
 2954                     new_stp->ls_flags, &stp);
 2955 
 2956         /*
 2957          * Sanity check the open.
 2958          */
 2959         if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
 2960                 (!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
 2961                  (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
 2962                 ((new_stp->ls_flags & NFSLCK_CONFIRM) &&
 2963                  (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
 2964                 error = NFSERR_BADSTATEID;
 2965 
 2966         if (!error)
 2967                 error = nfsrv_checkseqid(nd, new_stp->ls_seq,
 2968                     stp->ls_openowner, new_stp->ls_op);
 2969         if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
 2970             !(new_stp->ls_flags & NFSLCK_CONFIRM))
 2971                 error = NFSERR_OLDSTATEID;
 2972         if (!error && vnode_vtype(vp) != VREG) {
 2973                 if (vnode_vtype(vp) == VDIR)
 2974                         error = NFSERR_ISDIR;
 2975                 else
 2976                         error = NFSERR_INVAL;
 2977         }
 2978 
 2979         if (error) {
 2980                 /*
 2981                  * If a client tries to confirm an Open with a bad
 2982                  * seqid# and there are no byte range locks or other Opens
 2983                  * on the openowner, just throw it away, so the next use of the
 2984                  * openowner will start a fresh seq#.
 2985                  */
 2986                 if (error == NFSERR_BADSEQID &&
 2987                     (new_stp->ls_flags & NFSLCK_CONFIRM) &&
 2988                     nfsrv_nootherstate(stp))
 2989                         nfsrv_freeopenowner(stp->ls_openowner, 0, p);
 2990                 NFSUNLOCKSTATE();
 2991                 goto out;
 2992         }
 2993 
 2994         /*
 2995          * Set the return stateid.
 2996          */
 2997         stateidp->seqid = stp->ls_stateid.seqid + 1;
 2998         stateidp->other[0] = stp->ls_stateid.other[0];
 2999         stateidp->other[1] = stp->ls_stateid.other[1];
 3000         stateidp->other[2] = stp->ls_stateid.other[2];
 3001         /*
 3002          * Now, handle the three cases.
 3003          */
 3004         if (new_stp->ls_flags & NFSLCK_CONFIRM) {
 3005                 /*
 3006                  * If the open doesn't need confirmation, it seems to me that
 3007                  * there is a client error, but I'll just log it and keep going?
 3008                  */
 3009                 if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
 3010                         printf("Nfsv4d: stray open confirm\n");
 3011                 stp->ls_openowner->ls_flags = 0;
 3012                 stp->ls_stateid.seqid++;
 3013                 if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
 3014                         clp->lc_flags |= LCL_STAMPEDSTABLE;
 3015                         len = clp->lc_idlen;
 3016                         NFSBCOPY(clp->lc_id, client, len);
 3017                         gotstate = 1;
 3018                 }
 3019                 NFSUNLOCKSTATE();
 3020         } else if (new_stp->ls_flags & NFSLCK_CLOSE) {
 3021                 ownerstp = stp->ls_openowner;
 3022                 lfp = stp->ls_lfp;
 3023                 if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
 3024                         /* Get the lf lock */
 3025                         nfsrv_locklf(lfp);
 3026                         NFSUNLOCKSTATE();
 3027                         ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
 3028                         NFSVOPUNLOCK(vp, 0);
 3029                         if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
 3030                                 NFSLOCKSTATE();
 3031                                 nfsrv_unlocklf(lfp);
 3032                                 NFSUNLOCKSTATE();
 3033                         }
 3034                         NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
 3035                 } else {
 3036                         (void) nfsrv_freeopen(stp, NULL, 0, p);
 3037                         NFSUNLOCKSTATE();
 3038                 }
 3039         } else {
 3040                 /*
 3041                  * Update the share bits, making sure that the new set are a
 3042                  * subset of the old ones.
 3043                  */
 3044                 bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
 3045                 if (~(stp->ls_flags) & bits) {
 3046                         NFSUNLOCKSTATE();
 3047                         error = NFSERR_INVAL;
 3048                         goto out;
 3049                 }
 3050                 stp->ls_flags = (bits | NFSLCK_OPEN);
 3051                 stp->ls_stateid.seqid++;
 3052                 NFSUNLOCKSTATE();
 3053         }
 3054 
 3055         /*
 3056          * If the client just confirmed its first open, write a timestamp
 3057          * to the stable storage file.
 3058          */
 3059         if (gotstate != 0) {
 3060                 nfsrv_writestable(client, len, NFSNST_NEWSTATE, p);
 3061                 nfsrv_backupstable();
 3062         }
 3063 
 3064 out:
 3065         NFSEXITCODE2(error, nd);
 3066         return (error);
 3067 }
 3068 
 3069 /*
 3070  * Delegation update. Does the purge and return.
 3071  */
 3072 APPLESTATIC int
 3073 nfsrv_delegupdate(nfsquad_t clientid, nfsv4stateid_t *stateidp,
 3074     vnode_t vp, int op, struct ucred *cred, NFSPROC_T *p)
 3075 {
 3076         struct nfsstate *stp;
 3077         struct nfsclient *clp;
 3078         int error = 0;
 3079         fhandle_t fh;
 3080 
 3081         /*
 3082          * Do a sanity check against the file handle for DelegReturn.
 3083          */
 3084         if (vp) {
 3085                 error = nfsvno_getfh(vp, &fh, p);
 3086                 if (error)
 3087                         goto out;
 3088         }
 3089         /*
 3090          * Check for restart conditions (client and server).
 3091          */
 3092         if (op == NFSV4OP_DELEGRETURN)
 3093                 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
 3094                         stateidp, 0);
 3095         else
 3096                 error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
 3097                         stateidp, 0);
 3098 
 3099         NFSLOCKSTATE();
 3100         /*
 3101          * Get the open structure via clientid and stateid.
 3102          */
 3103         if (!error)
 3104             error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
 3105                 (nfsquad_t)((u_quad_t)0), NULL, p);
 3106         if (error) {
 3107                 if (error == NFSERR_CBPATHDOWN)
 3108                         error = 0;
 3109                 if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
 3110                         error = NFSERR_STALESTATEID;
 3111         }
 3112         if (!error && op == NFSV4OP_DELEGRETURN) {
 3113             error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
 3114             if (!error && stp->ls_stateid.seqid != stateidp->seqid)
 3115                 error = NFSERR_OLDSTATEID;
 3116         }
 3117         /*
 3118          * NFSERR_EXPIRED means that the state has gone away,
 3119          * so Delegations have been purged. Just return ok.
 3120          */
 3121         if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
 3122                 NFSUNLOCKSTATE();
 3123                 error = 0;
 3124                 goto out;
 3125         }
 3126         if (error) {
 3127                 NFSUNLOCKSTATE();
 3128                 goto out;
 3129         }
 3130 
 3131         if (op == NFSV4OP_DELEGRETURN) {
 3132                 if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
 3133                     sizeof (fhandle_t))) {
 3134                         NFSUNLOCKSTATE();
 3135                         error = NFSERR_BADSTATEID;
 3136                         goto out;
 3137                 }
 3138                 nfsrv_freedeleg(stp);
 3139         } else {
 3140                 nfsrv_freedeleglist(&clp->lc_olddeleg);
 3141         }
 3142         NFSUNLOCKSTATE();
 3143         error = 0;
 3144 
 3145 out:
 3146         NFSEXITCODE(error);
 3147         return (error);
 3148 }
 3149 
 3150 /*
 3151  * Release lock owner.
 3152  */
 3153 APPLESTATIC int
 3154 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
 3155     NFSPROC_T *p)
 3156 {
 3157         struct nfsstate *stp, *nstp, *openstp, *ownstp;
 3158         struct nfsclient *clp;
 3159         int error = 0;
 3160 
 3161         /*
 3162          * Check for restart conditions (client and server).
 3163          */
 3164         error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
 3165             &new_stp->ls_stateid, 0);
 3166         if (error)
 3167                 goto out;
 3168 
 3169         NFSLOCKSTATE();
 3170         /*
 3171          * Get the lock owner by name.
 3172          */
 3173         error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp,
 3174             (nfsquad_t)((u_quad_t)0), NULL, p);
 3175         if (error) {
 3176                 NFSUNLOCKSTATE();
 3177                 goto out;
 3178         }
 3179         LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
 3180             LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
 3181                 stp = LIST_FIRST(&openstp->ls_open);
 3182                 while (stp != LIST_END(&openstp->ls_open)) {
 3183                     nstp = LIST_NEXT(stp, ls_list);
 3184                     /*
 3185                      * If the owner matches, check for locks and
 3186                      * then free or return an error.
 3187                      */
 3188                     if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
 3189                         !NFSBCMP(stp->ls_owner, new_stp->ls_owner,
 3190                          stp->ls_ownerlen)){
 3191                         if (LIST_EMPTY(&stp->ls_lock)) {
 3192                             nfsrv_freelockowner(stp, NULL, 0, p);
 3193                         } else {
 3194                             NFSUNLOCKSTATE();
 3195                             error = NFSERR_LOCKSHELD;
 3196                             goto out;
 3197                         }
 3198                     }
 3199                     stp = nstp;
 3200                 }
 3201             }
 3202         }
 3203         NFSUNLOCKSTATE();
 3204 
 3205 out:
 3206         NFSEXITCODE(error);
 3207         return (error);
 3208 }
 3209 
 3210 /*
 3211  * Get the file handle for a lock structure.
 3212  */
 3213 static int
 3214 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
 3215     fhandle_t *nfhp, NFSPROC_T *p)
 3216 {
 3217         fhandle_t *fhp = NULL;
 3218         int error;
 3219 
 3220         /*
 3221          * For lock, use the new nfslock structure, otherwise just
 3222          * a fhandle_t on the stack.
 3223          */
 3224         if (flags & NFSLCK_OPEN) {
 3225                 KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
 3226                 fhp = &new_lfp->lf_fh;
 3227         } else if (nfhp) {
 3228                 fhp = nfhp;
 3229         } else {
 3230                 panic("nfsrv_getlockfh");
 3231         }
 3232         error = nfsvno_getfh(vp, fhp, p);
 3233         NFSEXITCODE(error);
 3234         return (error);
 3235 }
 3236 
 3237 /*
 3238  * Get an nfs lock structure. Allocate one, as required, and return a
 3239  * pointer to it.
 3240  * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
 3241  */
 3242 static int
 3243 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
 3244     struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
 3245 {
 3246         struct nfslockfile *lfp;
 3247         fhandle_t *fhp = NULL, *tfhp;
 3248         struct nfslockhashhead *hp;
 3249         struct nfslockfile *new_lfp = NULL;
 3250 
 3251         /*
 3252          * For lock, use the new nfslock structure, otherwise just
 3253          * a fhandle_t on the stack.
 3254          */
 3255         if (flags & NFSLCK_OPEN) {
 3256                 new_lfp = *new_lfpp;
 3257                 fhp = &new_lfp->lf_fh;
 3258         } else if (nfhp) {
 3259                 fhp = nfhp;
 3260         } else {
 3261                 panic("nfsrv_getlockfile");
 3262         }
 3263 
 3264         hp = NFSLOCKHASH(fhp);
 3265         LIST_FOREACH(lfp, hp, lf_hash) {
 3266                 tfhp = &lfp->lf_fh;
 3267                 if (NFSVNO_CMPFH(fhp, tfhp)) {
 3268                         if (lockit)
 3269                                 nfsrv_locklf(lfp);
 3270                         *lfpp = lfp;
 3271                         return (0);
 3272                 }
 3273         }
 3274         if (!(flags & NFSLCK_OPEN))
 3275                 return (-1);
 3276 
 3277         /*
 3278          * No match, so chain the new one into the list.
 3279          */
 3280         LIST_INIT(&new_lfp->lf_open);
 3281         LIST_INIT(&new_lfp->lf_lock);
 3282         LIST_INIT(&new_lfp->lf_deleg);
 3283         LIST_INIT(&new_lfp->lf_locallock);
 3284         LIST_INIT(&new_lfp->lf_rollback);
 3285         new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
 3286         new_lfp->lf_locallock_lck.nfslock_lock = 0;
 3287         new_lfp->lf_usecount = 0;
 3288         LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
 3289         *lfpp = new_lfp;
 3290         *new_lfpp = NULL;
 3291         return (0);
 3292 }
 3293 
 3294 /*
 3295  * This function adds a nfslock lock structure to the list for the associated
 3296  * nfsstate and nfslockfile structures. It will be inserted after the
 3297  * entry pointed at by insert_lop.
 3298  */
 3299 static void
 3300 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
 3301     struct nfsstate *stp, struct nfslockfile *lfp)
 3302 {
 3303         struct nfslock *lop, *nlop;
 3304 
 3305         new_lop->lo_stp = stp;
 3306         new_lop->lo_lfp = lfp;
 3307 
 3308         if (stp != NULL) {
 3309                 /* Insert in increasing lo_first order */
 3310                 lop = LIST_FIRST(&lfp->lf_lock);
 3311                 if (lop == LIST_END(&lfp->lf_lock) ||
 3312                     new_lop->lo_first <= lop->lo_first) {
 3313                         LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
 3314                 } else {
 3315                         nlop = LIST_NEXT(lop, lo_lckfile);
 3316                         while (nlop != LIST_END(&lfp->lf_lock) &&
 3317                                nlop->lo_first < new_lop->lo_first) {
 3318                                 lop = nlop;
 3319                                 nlop = LIST_NEXT(lop, lo_lckfile);
 3320                         }
 3321                         LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
 3322                 }
 3323         } else {
 3324                 new_lop->lo_lckfile.le_prev = NULL;     /* list not used */
 3325         }
 3326 
 3327         /*
 3328          * Insert after insert_lop, which is overloaded as stp or lfp for
 3329          * an empty list.
 3330          */
 3331         if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
 3332                 LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
 3333         else if ((struct nfsstate *)insert_lop == stp)
 3334                 LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
 3335         else
 3336                 LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
 3337         if (stp != NULL) {
 3338                 newnfsstats.srvlocks++;
 3339                 nfsrv_openpluslock++;
 3340         }
 3341 }
 3342 
 3343 /*
 3344  * This function updates the locking for a lock owner and given file. It
 3345  * maintains a list of lock ranges ordered on increasing file offset that
 3346  * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
 3347  * It always adds new_lop to the list and sometimes uses the one pointed
 3348  * at by other_lopp.
 3349  */
 3350 static void
 3351 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
 3352     struct nfslock **other_lopp, struct nfslockfile *lfp)
 3353 {
 3354         struct nfslock *new_lop = *new_lopp;
 3355         struct nfslock *lop, *tlop, *ilop;
 3356         struct nfslock *other_lop = *other_lopp;
 3357         int unlock = 0, myfile = 0;
 3358         u_int64_t tmp;
 3359 
 3360         /*
 3361          * Work down the list until the lock is merged.
 3362          */
 3363         if (new_lop->lo_flags & NFSLCK_UNLOCK)
 3364                 unlock = 1;
 3365         if (stp != NULL) {
 3366                 ilop = (struct nfslock *)stp;
 3367                 lop = LIST_FIRST(&stp->ls_lock);
 3368         } else {
 3369                 ilop = (struct nfslock *)lfp;
 3370                 lop = LIST_FIRST(&lfp->lf_locallock);
 3371         }
 3372         while (lop != NULL) {
 3373             /*
 3374              * Only check locks for this file that aren't before the start of
 3375              * new lock's range.
 3376              */
 3377             if (lop->lo_lfp == lfp) {
 3378               myfile = 1;
 3379               if (lop->lo_end >= new_lop->lo_first) {
 3380                 if (new_lop->lo_end < lop->lo_first) {
 3381                         /*
 3382                          * If the new lock ends before the start of the
 3383                          * current lock's range, no merge, just insert
 3384                          * the new lock.
 3385                          */
 3386                         break;
 3387                 }
 3388                 if (new_lop->lo_flags == lop->lo_flags ||
 3389                     (new_lop->lo_first <= lop->lo_first &&
 3390                      new_lop->lo_end >= lop->lo_end)) {
 3391                         /*
 3392                          * This lock can be absorbed by the new lock/unlock.
 3393                          * This happens when it covers the entire range
 3394                          * of the old lock or is contiguous
 3395                          * with the old lock and is of the same type or an
 3396                          * unlock.
 3397                          */
 3398                         if (lop->lo_first < new_lop->lo_first)
 3399                                 new_lop->lo_first = lop->lo_first;
 3400                         if (lop->lo_end > new_lop->lo_end)
 3401                                 new_lop->lo_end = lop->lo_end;
 3402                         tlop = lop;
 3403                         lop = LIST_NEXT(lop, lo_lckowner);
 3404                         nfsrv_freenfslock(tlop);
 3405                         continue;
 3406                 }
 3407 
 3408                 /*
 3409                  * All these cases are for contiguous locks that are not the
 3410                  * same type, so they can't be merged.
 3411                  */
 3412                 if (new_lop->lo_first <= lop->lo_first) {
 3413                         /*
 3414                          * This case is where the new lock overlaps with the
 3415                          * first part of the old lock. Move the start of the
 3416                          * old lock to just past the end of the new lock. The
 3417                          * new lock will be inserted in front of the old, since
 3418                          * ilop hasn't been updated. (We are done now.)
 3419                          */
 3420                         lop->lo_first = new_lop->lo_end;
 3421                         break;
 3422                 }
 3423                 if (new_lop->lo_end >= lop->lo_end) {
 3424                         /*
 3425                          * This case is where the new lock overlaps with the
 3426                          * end of the old lock's range. Move the old lock's
 3427                          * end to just before the new lock's first and insert
 3428                          * the new lock after the old lock.
 3429                          * Might not be done yet, since the new lock could
 3430                          * overlap further locks with higher ranges.
 3431                          */
 3432                         lop->lo_end = new_lop->lo_first;
 3433                         ilop = lop;
 3434                         lop = LIST_NEXT(lop, lo_lckowner);
 3435                         continue;
 3436                 }
 3437                 /*
 3438                  * The final case is where the new lock's range is in the
 3439                  * middle of the current lock's and splits the current lock
 3440                  * up. Use *other_lopp to handle the second part of the
 3441                  * split old lock range. (We are done now.)
 3442                  * For unlock, we use new_lop as other_lop and tmp, since
 3443                  * other_lop and new_lop are the same for this case.
 3444                  * We noted the unlock case above, so we don't need
 3445                  * new_lop->lo_flags any longer.
 3446                  */
 3447                 tmp = new_lop->lo_first;
 3448                 if (other_lop == NULL) {
 3449                         if (!unlock)
 3450                                 panic("nfsd srv update unlock");
 3451                         other_lop = new_lop;
 3452                         *new_lopp = NULL;
 3453                 }
 3454                 other_lop->lo_first = new_lop->lo_end;
 3455                 other_lop->lo_end = lop->lo_end;
 3456                 other_lop->lo_flags = lop->lo_flags;
 3457                 other_lop->lo_stp = stp;
 3458                 other_lop->lo_lfp = lfp;
 3459                 lop->lo_end = tmp;
 3460                 nfsrv_insertlock(other_lop, lop, stp, lfp);
 3461                 *other_lopp = NULL;
 3462                 ilop = lop;
 3463                 break;
 3464               }
 3465             }
 3466             ilop = lop;
 3467             lop = LIST_NEXT(lop, lo_lckowner);
 3468             if (myfile && (lop == NULL || lop->lo_lfp != lfp))
 3469                 break;
 3470         }
 3471 
 3472         /*
 3473          * Insert the new lock in the list at the appropriate place.
 3474          */
 3475         if (!unlock) {
 3476                 nfsrv_insertlock(new_lop, ilop, stp, lfp);
 3477                 *new_lopp = NULL;
 3478         }
 3479 }
 3480 
 3481 /*
 3482  * This function handles sequencing of locks, etc.
 3483  * It returns an error that indicates what the caller should do.
 3484  */
 3485 static int
 3486 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
 3487     struct nfsstate *stp, struct nfsrvcache *op)
 3488 {
 3489         int error = 0;
 3490 
 3491         if (op != nd->nd_rp)
 3492                 panic("nfsrvstate checkseqid");
 3493         if (!(op->rc_flag & RC_INPROG))
 3494                 panic("nfsrvstate not inprog");
 3495         if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
 3496                 printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
 3497                 panic("nfsrvstate op refcnt");
 3498         }
 3499         if ((stp->ls_seq + 1) == seqid) {
 3500                 if (stp->ls_op)
 3501                         nfsrvd_derefcache(stp->ls_op);
 3502                 stp->ls_op = op;
 3503                 nfsrvd_refcache(op);
 3504                 stp->ls_seq = seqid;
 3505                 goto out;
 3506         } else if (stp->ls_seq == seqid && stp->ls_op &&
 3507                 op->rc_xid == stp->ls_op->rc_xid &&
 3508                 op->rc_refcnt == 0 &&
 3509                 op->rc_reqlen == stp->ls_op->rc_reqlen &&
 3510                 op->rc_cksum == stp->ls_op->rc_cksum) {
 3511                 if (stp->ls_op->rc_flag & RC_INPROG) {
 3512                         error = NFSERR_DONTREPLY;
 3513                         goto out;
 3514                 }
 3515                 nd->nd_rp = stp->ls_op;
 3516                 nd->nd_rp->rc_flag |= RC_INPROG;
 3517                 nfsrvd_delcache(op);
 3518                 error = NFSERR_REPLYFROMCACHE;
 3519                 goto out;
 3520         }
 3521         error = NFSERR_BADSEQID;
 3522 
 3523 out:
 3524         NFSEXITCODE2(error, nd);
 3525         return (error);
 3526 }
 3527 
 3528 /*
 3529  * Get the client ip address for callbacks. If the strings can't be parsed,
 3530  * just set lc_program to 0 to indicate no callbacks are possible.
 3531  * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
 3532  *  the address to the client's transport address. This won't be used
 3533  *  for callbacks, but can be printed out by newnfsstats for info.)
 3534  * Return error if the xdr can't be parsed, 0 otherwise.
 3535  */
 3536 APPLESTATIC int
 3537 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
 3538 {
 3539         u_int32_t *tl;
 3540         u_char *cp, *cp2;
 3541         int i, j;
 3542         struct sockaddr_in *rad, *sad;
 3543         u_char protocol[5], addr[24];
 3544         int error = 0, cantparse = 0;
 3545         union {
 3546                 u_long ival;
 3547                 u_char cval[4];
 3548         } ip;
 3549         union {
 3550                 u_short sval;
 3551                 u_char cval[2];
 3552         } port;
 3553 
 3554         rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
 3555         rad->sin_family = AF_INET;
 3556         rad->sin_len = sizeof (struct sockaddr_in);
 3557         rad->sin_addr.s_addr = 0;
 3558         rad->sin_port = 0;
 3559         clp->lc_req.nr_client = NULL;
 3560         clp->lc_req.nr_lock = 0;
 3561         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
 3562         i = fxdr_unsigned(int, *tl);
 3563         if (i >= 3 && i <= 4) {
 3564                 error = nfsrv_mtostr(nd, protocol, i);
 3565                 if (error)
 3566                         goto nfsmout;
 3567                 if (!strcmp(protocol, "tcp")) {
 3568                         clp->lc_flags |= LCL_TCPCALLBACK;
 3569                         clp->lc_req.nr_sotype = SOCK_STREAM;
 3570                         clp->lc_req.nr_soproto = IPPROTO_TCP;
 3571                 } else if (!strcmp(protocol, "udp")) {
 3572                         clp->lc_req.nr_sotype = SOCK_DGRAM;
 3573                         clp->lc_req.nr_soproto = IPPROTO_UDP;
 3574                 } else {
 3575                         cantparse = 1;
 3576                 }
 3577         } else {
 3578                 cantparse = 1;
 3579                 if (i > 0) {
 3580                         error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
 3581                         if (error)
 3582                                 goto nfsmout;
 3583                 }
 3584         }
 3585         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
 3586         i = fxdr_unsigned(int, *tl);
 3587         if (i < 0) {
 3588                 error = NFSERR_BADXDR;
 3589                 goto nfsmout;
 3590         } else if (i == 0) {
 3591                 cantparse = 1;
 3592         } else if (!cantparse && i <= 23 && i >= 11) {
 3593                 error = nfsrv_mtostr(nd, addr, i);
 3594                 if (error)
 3595                         goto nfsmout;
 3596 
 3597                 /*
 3598                  * Parse out the address fields. We expect 6 decimal numbers
 3599                  * separated by '.'s.
 3600                  */
 3601                 cp = addr;
 3602                 i = 0;
 3603                 while (*cp && i < 6) {
 3604                         cp2 = cp;
 3605                         while (*cp2 && *cp2 != '.')
 3606                                 cp2++;
 3607                         if (*cp2)
 3608                                 *cp2++ = '\0';
 3609                         else if (i != 5) {
 3610                                 cantparse = 1;
 3611                                 break;
 3612                         }
 3613                         j = nfsrv_getipnumber(cp);
 3614                         if (j >= 0) {
 3615                                 if (i < 4)
 3616                                         ip.cval[3 - i] = j;
 3617                                 else
 3618                                         port.cval[5 - i] = j;
 3619                         } else {
 3620                                 cantparse = 1;
 3621                                 break;
 3622                         }
 3623                         cp = cp2;
 3624                         i++;
 3625                 }
 3626                 if (!cantparse) {
 3627                         if (ip.ival != 0x0) {
 3628                                 rad->sin_addr.s_addr = htonl(ip.ival);
 3629                                 rad->sin_port = htons(port.sval);
 3630                         } else {
 3631                                 cantparse = 1;
 3632                         }
 3633                 }
 3634         } else {
 3635                 cantparse = 1;
 3636                 if (i > 0) {
 3637                         error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
 3638                         if (error)
 3639                                 goto nfsmout;
 3640                 }
 3641         }
 3642         if (cantparse) {
 3643                 sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
 3644                 rad->sin_addr.s_addr = sad->sin_addr.s_addr;
 3645                 rad->sin_port = 0x0;
 3646                 clp->lc_program = 0;
 3647         }
 3648 nfsmout:
 3649         NFSEXITCODE2(error, nd);
 3650         return (error);
 3651 }
 3652 
 3653 /*
 3654  * Turn a string of up to three decimal digits into a number. Return -1 upon
 3655  * error.
 3656  */
 3657 static int
 3658 nfsrv_getipnumber(u_char *cp)
 3659 {
 3660         int i = 0, j = 0;
 3661 
 3662         while (*cp) {
 3663                 if (j > 2 || *cp < '' || *cp > '9')
 3664                         return (-1);
 3665                 i *= 10;
 3666                 i += (*cp - '');
 3667                 cp++;
 3668                 j++;
 3669         }
 3670         if (i < 256)
 3671                 return (i);
 3672         return (-1);
 3673 }
 3674 
 3675 /*
 3676  * This function checks for restart conditions.
 3677  */
 3678 static int
 3679 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
 3680     nfsv4stateid_t *stateidp, int specialid)
 3681 {
 3682         int ret = 0;
 3683 
 3684         /*
 3685          * First check for a server restart. Open, LockT, ReleaseLockOwner
 3686          * and DelegPurge have a clientid, the rest a stateid.
 3687          */
 3688         if (flags &
 3689             (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
 3690                 if (clientid.lval[0] != nfsrvboottime) {
 3691                         ret = NFSERR_STALECLIENTID;
 3692                         goto out;
 3693                 }
 3694         } else if (stateidp->other[0] != nfsrvboottime &&
 3695                 specialid == 0) {
 3696                 ret = NFSERR_STALESTATEID;
 3697                 goto out;
 3698         }
 3699 
 3700         /*
 3701          * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
 3702          * not use a lock/open owner seqid#, so the check can be done now.
 3703          * (The others will be checked, as required, later.)
 3704          */
 3705         if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
 3706                 goto out;
 3707 
 3708         NFSLOCKSTATE();
 3709         ret = nfsrv_checkgrace(flags);
 3710         NFSUNLOCKSTATE();
 3711 
 3712 out:
 3713         NFSEXITCODE(ret);
 3714         return (ret);
 3715 }
 3716 
 3717 /*
 3718  * Check for grace.
 3719  */
 3720 static int
 3721 nfsrv_checkgrace(u_int32_t flags)
 3722 {
 3723         int error = 0;
 3724 
 3725         if (nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) {
 3726                 if (flags & NFSLCK_RECLAIM) {
 3727                         error = NFSERR_NOGRACE;
 3728                         goto out;
 3729                 }
 3730         } else {
 3731                 if (!(flags & NFSLCK_RECLAIM)) {
 3732                         error = NFSERR_GRACE;
 3733                         goto out;
 3734                 }
 3735 
 3736                 /*
 3737                  * If grace is almost over and we are still getting Reclaims,
 3738                  * extend grace a bit.
 3739                  */
 3740                 if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
 3741                     nfsrv_stablefirst.nsf_eograce)
 3742                         nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
 3743                                 NFSRV_LEASEDELTA;
 3744         }
 3745 
 3746 out:
 3747         NFSEXITCODE(error);
 3748         return (error);
 3749 }
 3750 
 3751 /*
 3752  * Do a server callback.
 3753  */
 3754 static int
 3755 nfsrv_docallback(struct nfsclient *clp, int procnum,
 3756     nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
 3757     struct nfsvattr *nap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
 3758 {
 3759         mbuf_t m;
 3760         u_int32_t *tl;
 3761         struct nfsrv_descript nfsd, *nd = &nfsd;
 3762         struct ucred *cred;
 3763         int error = 0;
 3764         u_int32_t callback;
 3765 
 3766         cred = newnfs_getcred();
 3767         NFSLOCKSTATE(); /* mostly for lc_cbref++ */
 3768         if (clp->lc_flags & LCL_NEEDSCONFIRM) {
 3769                 NFSUNLOCKSTATE();
 3770                 panic("docallb");
 3771         }
 3772         clp->lc_cbref++;
 3773 
 3774         /*
 3775          * Fill the callback program# and version into the request
 3776          * structure for newnfs_connect() to use.
 3777          */
 3778         clp->lc_req.nr_prog = clp->lc_program;
 3779         clp->lc_req.nr_vers = NFSV4_CBVERS;
 3780 
 3781         /*
 3782          * First, fill in some of the fields of nd and cr.
 3783          */
 3784         nd->nd_flag = ND_NFSV4;
 3785         if (clp->lc_flags & LCL_GSS)
 3786                 nd->nd_flag |= ND_KERBV;
 3787         nd->nd_repstat = 0;
 3788         cred->cr_uid = clp->lc_uid;
 3789         cred->cr_gid = clp->lc_gid;
 3790         callback = clp->lc_callback;
 3791         NFSUNLOCKSTATE();
 3792         cred->cr_ngroups = 1;
 3793 
 3794         /*
 3795          * Get the first mbuf for the request.
 3796          */
 3797         MGET(m, M_WAIT, MT_DATA);
 3798         mbuf_setlen(m, 0);
 3799         nd->nd_mreq = nd->nd_mb = m;
 3800         nd->nd_bpos = NFSMTOD(m, caddr_t);
 3801         
 3802         /*
 3803          * and build the callback request.
 3804          */
 3805         if (procnum == NFSV4OP_CBGETATTR) {
 3806                 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
 3807                 (void) nfsm_strtom(nd, "CB Getattr", 10);
 3808                 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
 3809                 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
 3810                 *tl++ = txdr_unsigned(callback);
 3811                 *tl++ = txdr_unsigned(1);
 3812                 *tl = txdr_unsigned(NFSV4OP_CBGETATTR);
 3813                 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
 3814                 (void) nfsrv_putattrbit(nd, attrbitp);
 3815         } else if (procnum == NFSV4OP_CBRECALL) {
 3816                 nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
 3817                 (void) nfsm_strtom(nd, "CB Recall", 9);
 3818                 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED + NFSX_STATEID);
 3819                 *tl++ = txdr_unsigned(NFSV4_MINORVERSION);
 3820                 *tl++ = txdr_unsigned(callback);
 3821                 *tl++ = txdr_unsigned(1);
 3822                 *tl++ = txdr_unsigned(NFSV4OP_CBRECALL);
 3823                 *tl++ = txdr_unsigned(stateidp->seqid);
 3824                 NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
 3825                     NFSX_STATEIDOTHER);
 3826                 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
 3827                 if (trunc)
 3828                         *tl = newnfs_true;
 3829                 else
 3830                         *tl = newnfs_false;
 3831                 (void) nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
 3832         } else {
 3833                 nd->nd_procnum = NFSV4PROC_CBNULL;
 3834         }
 3835 
 3836         /*
 3837          * Call newnfs_connect(), as required, and then newnfs_request().
 3838          */
 3839         (void) newnfs_sndlock(&clp->lc_req.nr_lock);
 3840         if (clp->lc_req.nr_client == NULL) {
 3841                 if (nd->nd_procnum == NFSV4PROC_CBNULL)
 3842                         error = newnfs_connect(NULL, &clp->lc_req, cred,
 3843                             NULL, 1);
 3844                 else
 3845                         error = newnfs_connect(NULL, &clp->lc_req, cred,
 3846                             NULL, 3);
 3847         }
 3848         newnfs_sndunlock(&clp->lc_req.nr_lock);
 3849         if (!error) {
 3850                 error = newnfs_request(nd, NULL, clp, &clp->lc_req, NULL,
 3851                     NULL, cred, clp->lc_program, NFSV4_CBVERS, NULL, 1, NULL);
 3852         }
 3853         NFSFREECRED(cred);
 3854 
 3855         /*
 3856          * If error is set here, the Callback path isn't working
 3857          * properly, so twiddle the appropriate LCL_ flags.
 3858          * (nd_repstat != 0 indicates the Callback path is working,
 3859          *  but the callback failed on the client.)
 3860          */
 3861         if (error) {
 3862                 /*
 3863                  * Mark the callback pathway down, which disabled issuing
 3864                  * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
 3865                  */
 3866                 NFSLOCKSTATE();
 3867                 clp->lc_flags |= LCL_CBDOWN;
 3868                 NFSUNLOCKSTATE();
 3869         } else {
 3870                 /*
 3871                  * Callback worked. If the callback path was down, disable
 3872                  * callbacks, so no more delegations will be issued. (This
 3873                  * is done on the assumption that the callback pathway is
 3874                  * flakey.)
 3875                  */
 3876                 NFSLOCKSTATE();
 3877                 if (clp->lc_flags & LCL_CBDOWN)
 3878                         clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
 3879                 NFSUNLOCKSTATE();
 3880                 if (nd->nd_repstat)
 3881                         error = nd->nd_repstat;
 3882                 else if (procnum == NFSV4OP_CBGETATTR)
 3883                         error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
 3884                             NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
 3885                             p, NULL);
 3886                 mbuf_freem(nd->nd_mrep);
 3887         }
 3888         NFSLOCKSTATE();
 3889         clp->lc_cbref--;
 3890         if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
 3891                 clp->lc_flags &= ~LCL_WAKEUPWANTED;
 3892                 wakeup(clp);
 3893         }
 3894         NFSUNLOCKSTATE();
 3895 
 3896         NFSEXITCODE(error);
 3897         return (error);
 3898 }
 3899 
 3900 /*
 3901  * Return the next index# for a clientid. Mostly just increment and return
 3902  * the next one, but... if the 32bit unsigned does actually wrap around,
 3903  * it should be rebooted.
 3904  * At an average rate of one new client per second, it will wrap around in
 3905  * approximately 136 years. (I think the server will have been shut
 3906  * down or rebooted before then.)
 3907  */
 3908 static u_int32_t
 3909 nfsrv_nextclientindex(void)
 3910 {
 3911         static u_int32_t client_index = 0;
 3912 
 3913         client_index++;
 3914         if (client_index != 0)
 3915                 return (client_index);
 3916 
 3917         printf("%s: out of clientids\n", __func__);
 3918         return (client_index);
 3919 }
 3920 
 3921 /*
 3922  * Return the next index# for a stateid. Mostly just increment and return
 3923  * the next one, but... if the 32bit unsigned does actually wrap around
 3924  * (will a BSD server stay up that long?), find
 3925  * new start and end values.
 3926  */
 3927 static u_int32_t
 3928 nfsrv_nextstateindex(struct nfsclient *clp)
 3929 {
 3930         struct nfsstate *stp;
 3931         int i;
 3932         u_int32_t canuse, min_index, max_index;
 3933 
 3934         if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
 3935                 clp->lc_stateindex++;
 3936                 if (clp->lc_stateindex != clp->lc_statemaxindex)
 3937                         return (clp->lc_stateindex);
 3938         }
 3939 
 3940         /*
 3941          * Yuck, we've hit the end.
 3942          * Look for a new min and max.
 3943          */
 3944         min_index = 0;
 3945         max_index = 0xffffffff;
 3946         for (i = 0; i < NFSSTATEHASHSIZE; i++) {
 3947             LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
 3948                 if (stp->ls_stateid.other[2] > 0x80000000) {
 3949                     if (stp->ls_stateid.other[2] < max_index)
 3950                         max_index = stp->ls_stateid.other[2];
 3951                 } else {
 3952                     if (stp->ls_stateid.other[2] > min_index)
 3953                         min_index = stp->ls_stateid.other[2];
 3954                 }
 3955             }
 3956         }
 3957 
 3958         /*
 3959          * Yikes, highly unlikely, but I'll handle it anyhow.
 3960          */
 3961         if (min_index == 0x80000000 && max_index == 0x80000001) {
 3962             canuse = 0;
 3963             /*
 3964              * Loop around until we find an unused entry. Return that
 3965              * and set LCL_INDEXNOTOK, so the search will continue next time.
 3966              * (This is one of those rare cases where a goto is the
 3967              *  cleanest way to code the loop.)
 3968              */
 3969 tryagain:
 3970             for (i = 0; i < NFSSTATEHASHSIZE; i++) {
 3971                 LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
 3972                     if (stp->ls_stateid.other[2] == canuse) {
 3973                         canuse++;
 3974                         goto tryagain;
 3975                     }
 3976                 }
 3977             }
 3978             clp->lc_flags |= LCL_INDEXNOTOK;
 3979             return (canuse);
 3980         }
 3981 
 3982         /*
 3983          * Ok to start again from min + 1.
 3984          */
 3985         clp->lc_stateindex = min_index + 1;
 3986         clp->lc_statemaxindex = max_index;
 3987         clp->lc_flags &= ~LCL_INDEXNOTOK;
 3988         return (clp->lc_stateindex);
 3989 }
 3990 
 3991 /*
 3992  * The following functions handle the stable storage file that deals with
 3993  * the edge conditions described in RFC3530 Sec. 8.6.3.
 3994  * The file is as follows:
 3995  * - a single record at the beginning that has the lease time of the
 3996  *   previous server instance (before the last reboot) and the nfsrvboottime
 3997  *   values for the previous server boots.
 3998  *   These previous boot times are used to ensure that the current
 3999  *   nfsrvboottime does not, somehow, get set to a previous one.
 4000  *   (This is important so that Stale ClientIDs and StateIDs can
 4001  *    be recognized.)
 4002  *   The number of previous nfsvrboottime values preceeds the list.
 4003  * - followed by some number of appended records with:
 4004  *   - client id string
 4005  *   - flag that indicates it is a record revoking state via lease
 4006  *     expiration or similar
 4007  *     OR has successfully acquired state.
 4008  * These structures vary in length, with the client string at the end, up
 4009  * to NFSV4_OPAQUELIMIT in size.
 4010  *
 4011  * At the end of the grace period, the file is truncated, the first
 4012  * record is rewritten with updated information and any acquired state
 4013  * records for successful reclaims of state are written.
 4014  *
 4015  * Subsequent records are appended when the first state is issued to
 4016  * a client and when state is revoked for a client.
 4017  *
 4018  * When reading the file in, state issued records that come later in
 4019  * the file override older ones, since the append log is in cronological order.
 4020  * If, for some reason, the file can't be read, the grace period is
 4021  * immediately terminated and all reclaims get NFSERR_NOGRACE.
 4022  */
 4023 
 4024 /*
 4025  * Read in the stable storage file. Called by nfssvc() before the nfsd
 4026  * processes start servicing requests.
 4027  */
 4028 APPLESTATIC void
 4029 nfsrv_setupstable(NFSPROC_T *p)
 4030 {
 4031         struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
 4032         struct nfsrv_stable *sp, *nsp;
 4033         struct nfst_rec *tsp;
 4034         int error, i, tryagain;
 4035         off_t off = 0;
 4036         ssize_t aresid, len;
 4037 
 4038         /*
 4039          * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
 4040          * a reboot, so state has not been lost.
 4041          */
 4042         if (sf->nsf_flags & NFSNSF_UPDATEDONE)
 4043                 return;
 4044         /*
 4045          * Set Grace over just until the file reads successfully.
 4046          */
 4047         nfsrvboottime = time_second;
 4048         LIST_INIT(&sf->nsf_head);
 4049         sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
 4050         sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
 4051         if (sf->nsf_fp == NULL)
 4052                 return;
 4053         error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
 4054             (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
 4055             0, NFSFPCRED(sf->nsf_fp), &aresid, p);
 4056         if (error || aresid || sf->nsf_numboots == 0 ||
 4057                 sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
 4058                 return;
 4059 
 4060         /*
 4061          * Now, read in the boottimes.
 4062          */
 4063         sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
 4064                 sizeof (time_t), M_TEMP, M_WAITOK);
 4065         off = sizeof (struct nfsf_rec);
 4066         error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
 4067             (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
 4068             UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
 4069         if (error || aresid) {
 4070                 free((caddr_t)sf->nsf_bootvals, M_TEMP);
 4071                 sf->nsf_bootvals = NULL;
 4072                 return;
 4073         }
 4074 
 4075         /*
 4076          * Make sure this nfsrvboottime is different from all recorded
 4077          * previous ones.
 4078          */
 4079         do {
 4080                 tryagain = 0;
 4081                 for (i = 0; i < sf->nsf_numboots; i++) {
 4082                         if (nfsrvboottime == sf->nsf_bootvals[i]) {
 4083                                 nfsrvboottime++;
 4084                                 tryagain = 1;
 4085                                 break;
 4086                         }
 4087                 }
 4088         } while (tryagain);
 4089 
 4090         sf->nsf_flags |= NFSNSF_OK;
 4091         off += (sf->nsf_numboots * sizeof (time_t));
 4092 
 4093         /*
 4094          * Read through the file, building a list of records for grace
 4095          * checking.
 4096          * Each record is between sizeof (struct nfst_rec) and
 4097          * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
 4098          * and is actually sizeof (struct nfst_rec) + nst_len - 1.
 4099          */
 4100         tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
 4101                 NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
 4102         do {
 4103             error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
 4104                 (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
 4105                 off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
 4106             len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
 4107             if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
 4108                 len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
 4109                 /*
 4110                  * Yuck, the file has been corrupted, so just return
 4111                  * after clearing out any restart state, so the grace period
 4112                  * is over.
 4113                  */
 4114                 LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
 4115                         LIST_REMOVE(sp, nst_list);
 4116                         free((caddr_t)sp, M_TEMP);
 4117                 }
 4118                 free((caddr_t)tsp, M_TEMP);
 4119                 sf->nsf_flags &= ~NFSNSF_OK;
 4120                 free((caddr_t)sf->nsf_bootvals, M_TEMP);
 4121                 sf->nsf_bootvals = NULL;
 4122                 return;
 4123             }
 4124             if (len > 0) {
 4125                 off += sizeof (struct nfst_rec) + tsp->len - 1;
 4126                 /*
 4127                  * Search the list for a matching client.
 4128                  */
 4129                 LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
 4130                         if (tsp->len == sp->nst_len &&
 4131                             !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
 4132                                 break;
 4133                 }
 4134                 if (sp == LIST_END(&sf->nsf_head)) {
 4135                         sp = (struct nfsrv_stable *)malloc(tsp->len +
 4136                                 sizeof (struct nfsrv_stable) - 1, M_TEMP,
 4137                                 M_WAITOK);
 4138                         NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
 4139                                 sizeof (struct nfst_rec) + tsp->len - 1);
 4140                         LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
 4141                 } else {
 4142                         if (tsp->flag == NFSNST_REVOKE)
 4143                                 sp->nst_flag |= NFSNST_REVOKE;
 4144                         else
 4145                                 /*
 4146                                  * A subsequent timestamp indicates the client
 4147                                  * did a setclientid/confirm and any previous
 4148                                  * revoke is no longer relevant.
 4149                                  */
 4150                                 sp->nst_flag &= ~NFSNST_REVOKE;
 4151                 }
 4152             }
 4153         } while (len > 0);
 4154         free((caddr_t)tsp, M_TEMP);
 4155         sf->nsf_flags = NFSNSF_OK;
 4156         sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
 4157                 NFSRV_LEASEDELTA;
 4158 }
 4159 
 4160 /*
 4161  * Update the stable storage file, now that the grace period is over.
 4162  */
 4163 APPLESTATIC void
 4164 nfsrv_updatestable(NFSPROC_T *p)
 4165 {
 4166         struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
 4167         struct nfsrv_stable *sp, *nsp;
 4168         int i;
 4169         struct nfsvattr nva;
 4170         vnode_t vp;
 4171 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
 4172         mount_t mp = NULL;
 4173 #endif
 4174         int error;
 4175 
 4176         if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
 4177                 return;
 4178         sf->nsf_flags |= NFSNSF_UPDATEDONE;
 4179         /*
 4180          * Ok, we need to rewrite the stable storage file.
 4181          * - truncate to 0 length
 4182          * - write the new first structure
 4183          * - loop through the data structures, writing out any that
 4184          *   have timestamps older than the old boot
 4185          */
 4186         if (sf->nsf_bootvals) {
 4187                 sf->nsf_numboots++;
 4188                 for (i = sf->nsf_numboots - 2; i >= 0; i--)
 4189                         sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
 4190         } else {
 4191                 sf->nsf_numboots = 1;
 4192                 sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
 4193                         M_TEMP, M_WAITOK);
 4194         }
 4195         sf->nsf_bootvals[0] = nfsrvboottime;
 4196         sf->nsf_lease = nfsrv_lease;
 4197         NFSVNO_ATTRINIT(&nva);
 4198         NFSVNO_SETATTRVAL(&nva, size, 0);
 4199         vp = NFSFPVNODE(sf->nsf_fp);
 4200         vn_start_write(vp, &mp, V_WAIT);
 4201         if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
 4202                 error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
 4203                     NULL);
 4204                 NFSVOPUNLOCK(vp, 0);
 4205         } else
 4206                 error = EPERM;
 4207         vn_finished_write(mp);
 4208         if (!error)
 4209             error = NFSD_RDWR(UIO_WRITE, vp,
 4210                 (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
 4211                 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
 4212         if (!error)
 4213             error = NFSD_RDWR(UIO_WRITE, vp,
 4214                 (caddr_t)sf->nsf_bootvals,
 4215                 sf->nsf_numboots * sizeof (time_t),
 4216                 (off_t)(sizeof (struct nfsf_rec)),
 4217                 UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
 4218         free((caddr_t)sf->nsf_bootvals, M_TEMP);
 4219         sf->nsf_bootvals = NULL;
 4220         if (error) {
 4221                 sf->nsf_flags &= ~NFSNSF_OK;
 4222                 printf("EEK! Can't write NfsV4 stable storage file\n");
 4223                 return;
 4224         }
 4225         sf->nsf_flags |= NFSNSF_OK;
 4226 
 4227         /*
 4228          * Loop through the list and write out timestamp records for
 4229          * any clients that successfully reclaimed state.
 4230          */
 4231         LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
 4232                 if (sp->nst_flag & NFSNST_GOTSTATE) {
 4233                         nfsrv_writestable(sp->nst_client, sp->nst_len,
 4234                                 NFSNST_NEWSTATE, p);
 4235                         sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
 4236                 }
 4237                 LIST_REMOVE(sp, nst_list);
 4238                 free((caddr_t)sp, M_TEMP);
 4239         }
 4240         nfsrv_backupstable();
 4241 }
 4242 
 4243 /*
 4244  * Append a record to the stable storage file.
 4245  */
 4246 APPLESTATIC void
 4247 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
 4248 {
 4249         struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
 4250         struct nfst_rec *sp;
 4251         int error;
 4252 
 4253         if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
 4254                 return;
 4255         sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
 4256                 len - 1, M_TEMP, M_WAITOK);
 4257         sp->len = len;
 4258         NFSBCOPY(client, sp->client, len);
 4259         sp->flag = flag;
 4260         error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
 4261             (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
 4262             UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
 4263         free((caddr_t)sp, M_TEMP);
 4264         if (error) {
 4265                 sf->nsf_flags &= ~NFSNSF_OK;
 4266                 printf("EEK! Can't write NfsV4 stable storage file\n");
 4267         }
 4268 }
 4269 
 4270 /*
 4271  * This function is called during the grace period to mark a client
 4272  * that successfully reclaimed state.
 4273  */
 4274 static void
 4275 nfsrv_markstable(struct nfsclient *clp)
 4276 {
 4277         struct nfsrv_stable *sp;
 4278 
 4279         /*
 4280          * First find the client structure.
 4281          */
 4282         LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
 4283                 if (sp->nst_len == clp->lc_idlen &&
 4284                     !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
 4285                         break;
 4286         }
 4287         if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
 4288                 return;
 4289 
 4290         /*
 4291          * Now, just mark it and set the nfsclient back pointer.
 4292          */
 4293         sp->nst_flag |= NFSNST_GOTSTATE;
 4294         sp->nst_clp = clp;
 4295 }
 4296 
 4297 /*
 4298  * This function is called for a reclaim, to see if it gets grace.
 4299  * It returns 0 if a reclaim is allowed, 1 otherwise.
 4300  */
 4301 static int
 4302 nfsrv_checkstable(struct nfsclient *clp)
 4303 {
 4304         struct nfsrv_stable *sp;
 4305 
 4306         /*
 4307          * First, find the entry for the client.
 4308          */
 4309         LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
 4310                 if (sp->nst_len == clp->lc_idlen &&
 4311                     !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
 4312                         break;
 4313         }
 4314 
 4315         /*
 4316          * If not in the list, state was revoked or no state was issued
 4317          * since the previous reboot, a reclaim is denied.
 4318          */
 4319         if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
 4320             (sp->nst_flag & NFSNST_REVOKE) ||
 4321             !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
 4322                 return (1);
 4323         return (0);
 4324 }
 4325 
 4326 /*
 4327  * Test for and try to clear out a conflicting client. This is called by
 4328  * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
 4329  * a found.
 4330  * The trick here is that it can't revoke a conflicting client with an
 4331  * expired lease unless it holds the v4root lock, so...
 4332  * If no v4root lock, get the lock and return 1 to indicate "try again".
 4333  * Return 0 to indicate the conflict can't be revoked and 1 to indicate
 4334  * the revocation worked and the conflicting client is "bye, bye", so it
 4335  * can be tried again.
 4336  * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
 4337  * Unlocks State before a non-zero value is returned.
 4338  */
 4339 static int
 4340 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
 4341     NFSPROC_T *p)
 4342 {
 4343         int gotlock, lktype = 0;
 4344 
 4345         /*
 4346          * If lease hasn't expired, we can't fix it.
 4347          */
 4348         if (clp->lc_expiry >= NFSD_MONOSEC ||
 4349             !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
 4350                 return (0);
 4351         if (*haslockp == 0) {
 4352                 NFSUNLOCKSTATE();
 4353                 if (vp != NULL) {
 4354                         lktype = NFSVOPISLOCKED(vp);
 4355                         NFSVOPUNLOCK(vp, 0);
 4356                 }
 4357                 NFSLOCKV4ROOTMUTEX();
 4358                 nfsv4_relref(&nfsv4rootfs_lock);
 4359                 do {
 4360                         gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
 4361                             NFSV4ROOTLOCKMUTEXPTR, NULL);
 4362                 } while (!gotlock);
 4363                 NFSUNLOCKV4ROOTMUTEX();
 4364                 *haslockp = 1;
 4365                 if (vp != NULL) {
 4366                         NFSVOPLOCK(vp, lktype | LK_RETRY);
 4367                         if ((vp->v_iflag & VI_DOOMED) != 0)
 4368                                 return (2);
 4369                 }
 4370                 return (1);
 4371         }
 4372         NFSUNLOCKSTATE();
 4373 
 4374         /*
 4375          * Ok, we can expire the conflicting client.
 4376          */
 4377         nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
 4378         nfsrv_backupstable();
 4379         nfsrv_cleanclient(clp, p);
 4380         nfsrv_freedeleglist(&clp->lc_deleg);
 4381         nfsrv_freedeleglist(&clp->lc_olddeleg);
 4382         LIST_REMOVE(clp, lc_hash);
 4383         nfsrv_zapclient(clp, p);
 4384         return (1);
 4385 }
 4386 
 4387 /*
 4388  * Resolve a delegation conflict.
 4389  * Returns 0 to indicate the conflict was resolved without sleeping.
 4390  * Return -1 to indicate that the caller should check for conflicts again.
 4391  * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
 4392  *
 4393  * Also, manipulate the nfsv4root_lock, as required. It isn't changed
 4394  * for a return of 0, since there was no sleep and it could be required
 4395  * later. It is released for a return of NFSERR_DELAY, since the caller
 4396  * will return that error. It is released when a sleep was done waiting
 4397  * for the delegation to be returned or expire (so that other nfsds can
 4398  * handle ops). Then, it must be acquired for the write to stable storage.
 4399  * (This function is somewhat similar to nfsrv_clientconflict(), but
 4400  *  the semantics differ in a couple of subtle ways. The return of 0
 4401  *  indicates the conflict was resolved without sleeping here, not
 4402  *  that the conflict can't be resolved and the handling of nfsv4root_lock
 4403  *  differs, as noted above.)
 4404  * Unlocks State before returning a non-zero value.
 4405  */
 4406 static int
 4407 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
 4408     vnode_t vp)
 4409 {
 4410         struct nfsclient *clp = stp->ls_clp;
 4411         int gotlock, error, lktype = 0, retrycnt, zapped_clp;
 4412         nfsv4stateid_t tstateid;
 4413         fhandle_t tfh;
 4414 
 4415         /*
 4416          * If the conflict is with an old delegation...
 4417          */
 4418         if (stp->ls_flags & NFSLCK_OLDDELEG) {
 4419                 /*
 4420                  * You can delete it, if it has expired.
 4421                  */
 4422                 if (clp->lc_delegtime < NFSD_MONOSEC) {
 4423                         nfsrv_freedeleg(stp);
 4424                         NFSUNLOCKSTATE();
 4425                         error = -1;
 4426                         goto out;
 4427                 }
 4428                 NFSUNLOCKSTATE();
 4429                 /*
 4430                  * During this delay, the old delegation could expire or it
 4431                  * could be recovered by the client via an Open with
 4432                  * CLAIM_DELEGATE_PREV.
 4433                  * Release the nfsv4root_lock, if held.
 4434                  */
 4435                 if (*haslockp) {
 4436                         *haslockp = 0;
 4437                         NFSLOCKV4ROOTMUTEX();
 4438                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 4439                         NFSUNLOCKV4ROOTMUTEX();
 4440                 }
 4441                 error = NFSERR_DELAY;
 4442                 goto out;
 4443         }
 4444 
 4445         /*
 4446          * It's a current delegation, so:
 4447          * - check to see if the delegation has expired
 4448          *   - if so, get the v4root lock and then expire it
 4449          */
 4450         if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
 4451                 /*
 4452                  * - do a recall callback, since not yet done
 4453                  * For now, never allow truncate to be set. To use
 4454                  * truncate safely, it must be guaranteed that the
 4455                  * Remove, Rename or Setattr with size of 0 will
 4456                  * succeed and that would require major changes to
 4457                  * the VFS/Vnode OPs.
 4458                  * Set the expiry time large enough so that it won't expire
 4459                  * until after the callback, then set it correctly, once
 4460                  * the callback is done. (The delegation will now time
 4461                  * out whether or not the Recall worked ok. The timeout
 4462                  * will be extended when ops are done on the delegation
 4463                  * stateid, up to the timelimit.)
 4464                  */
 4465                 stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
 4466                     NFSRV_LEASEDELTA;
 4467                 stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
 4468                     NFSRV_LEASEDELTA;
 4469                 stp->ls_flags |= NFSLCK_DELEGRECALL;
 4470 
 4471                 /*
 4472                  * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
 4473                  * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
 4474                  * in order to try and avoid a race that could happen
 4475                  * when a CBRecall request passed the Open reply with
 4476                  * the delegation in it when transitting the network.
 4477                  * Since nfsrv_docallback will sleep, don't use stp after
 4478                  * the call.
 4479                  */
 4480                 NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
 4481                     sizeof (tstateid));
 4482                 NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
 4483                     sizeof (tfh));
 4484                 NFSUNLOCKSTATE();
 4485                 if (*haslockp) {
 4486                         *haslockp = 0;
 4487                         NFSLOCKV4ROOTMUTEX();
 4488                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 4489                         NFSUNLOCKV4ROOTMUTEX();
 4490                 }
 4491                 retrycnt = 0;
 4492                 do {
 4493                     error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
 4494                         &tstateid, 0, &tfh, NULL, NULL, p);
 4495                     retrycnt++;
 4496                 } while ((error == NFSERR_BADSTATEID ||
 4497                     error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
 4498                 error = NFSERR_DELAY;
 4499                 goto out;
 4500         }
 4501 
 4502         if (clp->lc_expiry >= NFSD_MONOSEC &&
 4503             stp->ls_delegtime >= NFSD_MONOSEC) {
 4504                 NFSUNLOCKSTATE();
 4505                 /*
 4506                  * A recall has been done, but it has not yet expired.
 4507                  * So, RETURN_DELAY.
 4508                  */
 4509                 if (*haslockp) {
 4510                         *haslockp = 0;
 4511                         NFSLOCKV4ROOTMUTEX();
 4512                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 4513                         NFSUNLOCKV4ROOTMUTEX();
 4514                 }
 4515                 error = NFSERR_DELAY;
 4516                 goto out;
 4517         }
 4518 
 4519         /*
 4520          * If we don't yet have the lock, just get it and then return,
 4521          * since we need that before deleting expired state, such as
 4522          * this delegation.
 4523          * When getting the lock, unlock the vnode, so other nfsds that
 4524          * are in progress, won't get stuck waiting for the vnode lock.
 4525          */
 4526         if (*haslockp == 0) {
 4527                 NFSUNLOCKSTATE();
 4528                 if (vp != NULL) {
 4529                         lktype = NFSVOPISLOCKED(vp);
 4530                         NFSVOPUNLOCK(vp, 0);
 4531                 }
 4532                 NFSLOCKV4ROOTMUTEX();
 4533                 nfsv4_relref(&nfsv4rootfs_lock);
 4534                 do {
 4535                         gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
 4536                             NFSV4ROOTLOCKMUTEXPTR, NULL);
 4537                 } while (!gotlock);
 4538                 NFSUNLOCKV4ROOTMUTEX();
 4539                 *haslockp = 1;
 4540                 if (vp != NULL) {
 4541                         NFSVOPLOCK(vp, lktype | LK_RETRY);
 4542                         if ((vp->v_iflag & VI_DOOMED) != 0) {
 4543                                 *haslockp = 0;
 4544                                 NFSLOCKV4ROOTMUTEX();
 4545                                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
 4546                                 NFSUNLOCKV4ROOTMUTEX();
 4547                                 error = NFSERR_PERM;
 4548                                 goto out;
 4549                         }
 4550                 }
 4551                 error = -1;
 4552                 goto out;
 4553         }
 4554 
 4555         NFSUNLOCKSTATE();
 4556         /*
 4557          * Ok, we can delete the expired delegation.
 4558          * First, write the Revoke record to stable storage and then
 4559          * clear out the conflict.
 4560          * Since all other nfsd threads are now blocked, we can safely
 4561          * sleep without the state changing.
 4562          */
 4563         nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
 4564         nfsrv_backupstable();
 4565         if (clp->lc_expiry < NFSD_MONOSEC) {
 4566                 nfsrv_cleanclient(clp, p);
 4567                 nfsrv_freedeleglist(&clp->lc_deleg);
 4568                 nfsrv_freedeleglist(&clp->lc_olddeleg);
 4569                 LIST_REMOVE(clp, lc_hash);
 4570                 zapped_clp = 1;
 4571         } else {
 4572                 nfsrv_freedeleg(stp);
 4573                 zapped_clp = 0;
 4574         }
 4575         if (zapped_clp)
 4576                 nfsrv_zapclient(clp, p);
 4577         error = -1;
 4578 
 4579 out:
 4580         NFSEXITCODE(error);
 4581         return (error);
 4582 }
 4583 
 4584 /*
 4585  * Check for a remove allowed, if remove is set to 1 and get rid of
 4586  * delegations.
 4587  */
 4588 APPLESTATIC int
 4589 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
 4590 {
 4591         struct nfsstate *stp;
 4592         struct nfslockfile *lfp;
 4593         int error, haslock = 0;
 4594         fhandle_t nfh;
 4595 
 4596         /*
 4597          * First, get the lock file structure.
 4598          * (A return of -1 means no associated state, so remove ok.)
 4599          */
 4600         error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
 4601 tryagain:
 4602         NFSLOCKSTATE();
 4603         if (!error)
 4604                 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
 4605         if (error) {
 4606                 NFSUNLOCKSTATE();
 4607                 if (haslock) {
 4608                         NFSLOCKV4ROOTMUTEX();
 4609                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 4610                         NFSUNLOCKV4ROOTMUTEX();
 4611                 }
 4612                 if (error == -1)
 4613                         error = 0;
 4614                 goto out;
 4615         }
 4616 
 4617         /*
 4618          * Now, we must Recall any delegations.
 4619          */
 4620         error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
 4621         if (error) {
 4622                 /*
 4623                  * nfsrv_cleandeleg() unlocks state for non-zero
 4624                  * return.
 4625                  */
 4626                 if (error == -1)
 4627                         goto tryagain;
 4628                 if (haslock) {
 4629                         NFSLOCKV4ROOTMUTEX();
 4630                         nfsv4_unlock(&nfsv4rootfs_lock, 1);
 4631                         NFSUNLOCKV4ROOTMUTEX();
 4632                 }
 4633                 goto out;
 4634         }
 4635 
 4636         /*
 4637          * Now, look for a conflicting open share.
 4638          */
 4639         if (remove) {
 4640                 LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
 4641                         if (stp->ls_flags & NFSLCK_WRITEDENY) {
 4642                                 error = NFSERR_FILEOPEN;
 4643                                 break;
 4644                         }
 4645                 }
 4646         }
 4647 
 4648         NFSUNLOCKSTATE();
 4649         if (haslock) {
 4650                 NFSLOCKV4ROOTMUTEX();
 4651                 nfsv4_unlock(&nfsv4rootfs_lock, 1);
 4652                 NFSUNLOCKV4ROOTMUTEX();
 4653         }
 4654 
 4655 out:
 4656         NFSEXITCODE(error);
 4657         return (error);
 4658 }
 4659 
 4660 /*
 4661  * Clear out all delegations for the file referred to by lfp.
 4662  * May return NFSERR_DELAY, if there will be a delay waiting for
 4663  * delegations to expire.
 4664  * Returns -1 to indicate it slept while recalling a delegation.
 4665  * This function has the side effect of deleting the nfslockfile structure,
 4666  * if it no longer has associated state and didn't have to sleep.
 4667  * Unlocks State before a non-zero value is returned.
 4668  */
 4669 static int
 4670 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
 4671     struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
 4672 {
 4673         struct nfsstate *stp, *nstp;
 4674         int ret = 0;
 4675 
 4676         stp = LIST_FIRST(&lfp->lf_deleg);
 4677         while (stp != LIST_END(&lfp->lf_deleg)) {
 4678                 nstp = LIST_NEXT(stp, ls_file);
 4679                 if (stp->ls_clp != clp) {
 4680                         ret = nfsrv_delegconflict(stp, haslockp, p, vp);
 4681                         if (ret) {
 4682                                 /*
 4683                                  * nfsrv_delegconflict() unlocks state
 4684                                  * when it returns non-zero.
 4685                                  */
 4686                                 goto out;
 4687                         }
 4688                 }
 4689                 stp = nstp;
 4690         }
 4691 out:
 4692         NFSEXITCODE(ret);
 4693         return (ret);
 4694 }
 4695 
 4696 /*
 4697  * There are certain operations that, when being done outside of NFSv4,
 4698  * require that any NFSv4 delegation for the file be recalled.
 4699  * This function is to be called for those cases:
 4700  * VOP_RENAME() - When a delegation is being recalled for any reason,
 4701  *      the client may have to do Opens against the server, using the file's
 4702  *      final component name. If the file has been renamed on the server,
 4703  *      that component name will be incorrect and the Open will fail.
 4704  * VOP_REMOVE() - Theoretically, a client could Open a file after it has
 4705  *      been removed on the server, if there is a delegation issued to
 4706  *      that client for the file. I say "theoretically" since clients
 4707  *      normally do an Access Op before the Open and that Access Op will
 4708  *      fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
 4709  *      they will detect the file's removal in the same manner. (There is
 4710  *      one case where RFC3530 allows a client to do an Open without first
 4711  *      doing an Access Op, which is passage of a check against the ACE
 4712  *      returned with a Write delegation, but current practice is to ignore
 4713  *      the ACE and always do an Access Op.)
 4714  *      Since the functions can only be called with an unlocked vnode, this
 4715  *      can't be done at this time.
 4716  * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
 4717  *      locks locally in the client, which are not visible to the server. To
 4718  *      deal with this, issuing of delegations for a vnode must be disabled
 4719  *      and all delegations for the vnode recalled. This is done via the
 4720  *      second function, using the VV_DISABLEDELEG vflag on the vnode.
 4721  */
 4722 APPLESTATIC void
 4723 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
 4724 {
 4725         time_t starttime;
 4726         int error;
 4727 
 4728         /*
 4729          * First, check to see if the server is currently running and it has
 4730          * been called for a regular file when issuing delegations.
 4731          */
 4732         if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
 4733             nfsrv_issuedelegs == 0)
 4734                 return;
 4735 
 4736         KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
 4737         /*
 4738          * First, get a reference on the nfsv4rootfs_lock so that an
 4739          * exclusive lock cannot be acquired by another thread.
 4740          */
 4741         NFSLOCKV4ROOTMUTEX();
 4742         nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
 4743         NFSUNLOCKV4ROOTMUTEX();
 4744 
 4745         /*
 4746          * Now, call nfsrv_checkremove() in a loop while it returns
 4747          * NFSERR_DELAY. Return upon any other error or when timed out.
 4748          */
 4749         starttime = NFSD_MONOSEC;
 4750         do {
 4751                 if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
 4752                         error = nfsrv_checkremove(vp, 0, p);
 4753                         NFSVOPUNLOCK(vp, 0);
 4754                 } else
 4755                         error = EPERM;
 4756                 if (error == NFSERR_DELAY) {
 4757                         if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
 4758                                 break;
 4759                         /* Sleep for a short period of time */
 4760                         (void) nfs_catnap(PZERO, 0, "nfsremove");
 4761                 }
 4762         } while (error == NFSERR_DELAY);
 4763         NFSLOCKV4ROOTMUTEX();
 4764         nfsv4_relref(&nfsv4rootfs_lock);
 4765         NFSUNLOCKV4ROOTMUTEX();
 4766 }
 4767 
 4768 APPLESTATIC void
 4769 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
 4770 {
 4771 
 4772 #ifdef VV_DISABLEDELEG
 4773         /*
 4774          * First, flag issuance of delegations disabled.
 4775          */
 4776         atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
 4777 #endif
 4778 
 4779         /*
 4780          * Then call nfsd_recalldelegation() to get rid of all extant
 4781          * delegations.
 4782          */
 4783         nfsd_recalldelegation(vp, p);
 4784 }
 4785 
 4786 /*
 4787  * Check for conflicting locks, etc. and then get rid of delegations.
 4788  * (At one point I thought that I should get rid of delegations for any
 4789  *  Setattr, since it could potentially disallow the I/O op (read or write)
 4790  *  allowed by the delegation. However, Setattr Ops that aren't changing
 4791  *  the size get a stateid of all 0s, so you can't tell if it is a delegation
 4792  *  for the same client or a different one, so I decided to only get rid
 4793  *  of delegations for other clients when the size is being changed.)
 4794  * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
 4795  * as Write backs, even if there is no delegation, so it really isn't any
 4796  * different?)
 4797  */
 4798 APPLESTATIC int
 4799 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
 4800     nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
 4801     struct nfsexstuff *exp, NFSPROC_T *p)
 4802 {
 4803         struct nfsstate st, *stp = &st;
 4804         struct nfslock lo, *lop = &lo;
 4805         int error = 0;
 4806         nfsquad_t clientid;
 4807 
 4808         if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
 4809                 stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
 4810                 lop->lo_first = nvap->na_size;
 4811         } else {
 4812                 stp->ls_flags = 0;
 4813                 lop->lo_first = 0;
 4814         }
 4815         if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
 4816             NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
 4817             NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
 4818             NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
 4819                 stp->ls_flags |= NFSLCK_SETATTR;
 4820         if (stp->ls_flags == 0)
 4821                 goto out;
 4822         lop->lo_end = NFS64BITSSET;
 4823         lop->lo_flags = NFSLCK_WRITE;
 4824         stp->ls_ownerlen = 0;
 4825         stp->ls_op = NULL;
 4826         stp->ls_uid = nd->nd_cred->cr_uid;
 4827         stp->ls_stateid.seqid = stateidp->seqid;
 4828         clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
 4829         clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
 4830         stp->ls_stateid.other[2] = stateidp->other[2];
 4831         error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
 4832             stateidp, exp, nd, p);
 4833 
 4834 out:
 4835         NFSEXITCODE2(error, nd);
 4836         return (error);
 4837 }
 4838 
 4839 /*
 4840  * Check for a write delegation and do a CBGETATTR if there is one, updating
 4841  * the attributes, as required.
 4842  * Should I return an error if I can't get the attributes? (For now, I'll
 4843  * just return ok.
 4844  */
 4845 APPLESTATIC int
 4846 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
 4847     struct nfsvattr *nvap, nfsattrbit_t *attrbitp, struct ucred *cred,
 4848     NFSPROC_T *p)
 4849 {
 4850         struct nfsstate *stp;
 4851         struct nfslockfile *lfp;
 4852         struct nfsclient *clp;
 4853         struct nfsvattr nva;
 4854         fhandle_t nfh;
 4855         int error = 0;
 4856         nfsattrbit_t cbbits;
 4857         u_quad_t delegfilerev;
 4858 
 4859         NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
 4860         if (!NFSNONZERO_ATTRBIT(&cbbits))
 4861                 goto out;
 4862 
 4863         /*
 4864          * Get the lock file structure.
 4865          * (A return of -1 means no associated state, so return ok.)
 4866          */
 4867         error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
 4868         NFSLOCKSTATE();
 4869         if (!error)
 4870                 error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
 4871         if (error) {
 4872                 NFSUNLOCKSTATE();
 4873                 if (error == -1)
 4874                         error = 0;
 4875                 goto out;
 4876         }
 4877 
 4878         /*
 4879          * Now, look for a write delegation.
 4880          */
 4881         LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
 4882                 if (stp->ls_flags & NFSLCK_DELEGWRITE)
 4883                         break;
 4884         }
 4885         if (stp == LIST_END(&lfp->lf_deleg)) {
 4886                 NFSUNLOCKSTATE();
 4887                 goto out;
 4888         }
 4889         clp = stp->ls_clp;
 4890         delegfilerev = stp->ls_filerev;
 4891 
 4892         /*
 4893          * If the Write delegation was issued as a part of this Compound RPC
 4894          * or if we have an Implied Clientid (used in a previous Op in this
 4895          * compound) and it is the client the delegation was issued to,
 4896          * just return ok.
 4897          * I also assume that it is from the same client iff the network
 4898          * host IP address is the same as the callback address. (Not
 4899          * exactly correct by the RFC, but avoids a lot of Getattr
 4900          * callbacks.)
 4901          */
 4902         if (nd->nd_compref == stp->ls_compref ||
 4903             ((nd->nd_flag & ND_IMPLIEDCLID) &&
 4904              clp->lc_clientid.qval == nd->nd_clientid.qval) ||
 4905              nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
 4906                 NFSUNLOCKSTATE();
 4907                 goto out;
 4908         }
 4909 
 4910         /*
 4911          * We are now done with the delegation state structure,
 4912          * so the statelock can be released and we can now tsleep().
 4913          */
 4914 
 4915         /*
 4916          * Now, we must do the CB Getattr callback, to see if Change or Size
 4917          * has changed.
 4918          */
 4919         if (clp->lc_expiry >= NFSD_MONOSEC) {
 4920                 NFSUNLOCKSTATE();
 4921                 NFSVNO_ATTRINIT(&nva);
 4922                 nva.na_filerev = NFS64BITSSET;
 4923                 error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
 4924                     0, &nfh, &nva, &cbbits, p);
 4925                 if (!error) {
 4926                         if ((nva.na_filerev != NFS64BITSSET &&
 4927                             nva.na_filerev > delegfilerev) ||
 4928                             (NFSVNO_ISSETSIZE(&nva) &&
 4929                              nva.na_size != nvap->na_size)) {
 4930                                 error = nfsvno_updfilerev(vp, nvap, cred, p);
 4931                                 if (NFSVNO_ISSETSIZE(&nva))
 4932                                         nvap->na_size = nva.na_size;
 4933                         }
 4934                 } else
 4935                         error = 0;      /* Ignore callback errors for now. */
 4936         } else {
 4937                 NFSUNLOCKSTATE();
 4938         }
 4939 
 4940 out:
 4941         NFSEXITCODE2(error, nd);
 4942         return (error);
 4943 }
 4944 
 4945 /*
 4946  * This function looks for openowners that haven't had any opens for
 4947  * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
 4948  * is set.
 4949  */
 4950 APPLESTATIC void
 4951 nfsrv_throwawayopens(NFSPROC_T *p)
 4952 {
 4953         struct nfsclient *clp, *nclp;
 4954         struct nfsstate *stp, *nstp;
 4955         int i;
 4956 
 4957         NFSLOCKSTATE();
 4958         nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
 4959         /*
 4960          * For each client...
 4961          */
 4962         for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
 4963             LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
 4964                 LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
 4965                         if (LIST_EMPTY(&stp->ls_open) &&
 4966                             (stp->ls_noopens > NFSNOOPEN ||
 4967                              (nfsrv_openpluslock * 2) >
 4968                              NFSRV_V4STATELIMIT))
 4969                                 nfsrv_freeopenowner(stp, 0, p);
 4970                 }
 4971             }
 4972         }
 4973         NFSUNLOCKSTATE();
 4974 }
 4975 
 4976 /*
 4977  * This function checks to see if the credentials are the same.
 4978  * Returns 1 for not same, 0 otherwise.
 4979  */
 4980 static int
 4981 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
 4982 {
 4983 
 4984         if (nd->nd_flag & ND_GSS) {
 4985                 if (!(clp->lc_flags & LCL_GSS))
 4986                         return (1);
 4987                 if (clp->lc_flags & LCL_NAME) {
 4988                         if (nd->nd_princlen != clp->lc_namelen ||
 4989                             NFSBCMP(nd->nd_principal, clp->lc_name,
 4990                                 clp->lc_namelen))
 4991                                 return (1);
 4992                         else
 4993                                 return (0);
 4994                 }
 4995                 if (nd->nd_cred->cr_uid == clp->lc_uid)
 4996                         return (0);
 4997                 else
 4998                         return (1);
 4999         } else if (clp->lc_flags & LCL_GSS)
 5000                 return (1);
 5001         /*
 5002          * For AUTH_SYS, allow the same uid or root. (This is underspecified
 5003          * in RFC3530, which talks about principals, but doesn't say anything
 5004          * about uids for AUTH_SYS.)
 5005          */
 5006         if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
 5007                 return (0);
 5008         else
 5009                 return (1);
 5010 }
 5011 
 5012 /*
 5013  * Calculate the lease expiry time.
 5014  */
 5015 static time_t
 5016 nfsrv_leaseexpiry(void)
 5017 {
 5018 
 5019         if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
 5020                 return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
 5021         return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
 5022 }
 5023 
 5024 /*
 5025  * Delay the delegation timeout as far as ls_delegtimelimit, as required.
 5026  */
 5027 static void
 5028 nfsrv_delaydelegtimeout(struct nfsstate *stp)
 5029 {
 5030 
 5031         if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
 5032                 return;
 5033 
 5034         if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
 5035             stp->ls_delegtime < stp->ls_delegtimelimit) {
 5036                 stp->ls_delegtime += nfsrv_lease;
 5037                 if (stp->ls_delegtime > stp->ls_delegtimelimit)
 5038                         stp->ls_delegtime = stp->ls_delegtimelimit;
 5039         }
 5040 }
 5041 
 5042 /*
 5043  * This function checks to see if there is any other state associated
 5044  * with the openowner for this Open.
 5045  * It returns 1 if there is no other state, 0 otherwise.
 5046  */
 5047 static int
 5048 nfsrv_nootherstate(struct nfsstate *stp)
 5049 {
 5050         struct nfsstate *tstp;
 5051 
 5052         LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
 5053                 if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
 5054                         return (0);
 5055         }
 5056         return (1);
 5057 }
 5058 
 5059 /*
 5060  * Create a list of lock deltas (changes to local byte range locking
 5061  * that can be rolled back using the list) and apply the changes via
 5062  * nfsvno_advlock(). Optionally, lock the list. It is expected that either
 5063  * the rollback or update function will be called after this.
 5064  * It returns an error (and rolls back, as required), if any nfsvno_advlock()
 5065  * call fails. If it returns an error, it will unlock the list.
 5066  */
 5067 static int
 5068 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
 5069     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
 5070 {
 5071         struct nfslock *lop, *nlop;
 5072         int error = 0;
 5073 
 5074         /* Loop through the list of locks. */
 5075         lop = LIST_FIRST(&lfp->lf_locallock);
 5076         while (first < end && lop != NULL) {
 5077                 nlop = LIST_NEXT(lop, lo_lckowner);
 5078                 if (first >= lop->lo_end) {
 5079                         /* not there yet */
 5080                         lop = nlop;
 5081                 } else if (first < lop->lo_first) {
 5082                         /* new one starts before entry in list */
 5083                         if (end <= lop->lo_first) {
 5084                                 /* no overlap between old and new */
 5085                                 error = nfsrv_dolocal(vp, lfp, flags,
 5086                                     NFSLCK_UNLOCK, first, end, cfp, p);
 5087                                 if (error != 0)
 5088                                         break;
 5089                                 first = end;
 5090                         } else {
 5091                                 /* handle fragment overlapped with new one */
 5092                                 error = nfsrv_dolocal(vp, lfp, flags,
 5093                                     NFSLCK_UNLOCK, first, lop->lo_first, cfp,
 5094                                     p);
 5095                                 if (error != 0)
 5096                                         break;
 5097                                 first = lop->lo_first;
 5098                         }
 5099                 } else {
 5100                         /* new one overlaps this entry in list */
 5101                         if (end <= lop->lo_end) {
 5102                                 /* overlaps all of new one */
 5103                                 error = nfsrv_dolocal(vp, lfp, flags,
 5104                                     lop->lo_flags, first, end, cfp, p);
 5105                                 if (error != 0)
 5106                                         break;
 5107                                 first = end;
 5108                         } else {
 5109                                 /* handle fragment overlapped with new one */
 5110                                 error = nfsrv_dolocal(vp, lfp, flags,
 5111                                     lop->lo_flags, first, lop->lo_end, cfp, p);
 5112                                 if (error != 0)
 5113                                         break;
 5114                                 first = lop->lo_end;
 5115                                 lop = nlop;
 5116                         }
 5117                 }
 5118         }
 5119         if (first < end && error == 0)
 5120                 /* handle fragment past end of list */
 5121                 error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
 5122                     end, cfp, p);
 5123 
 5124         NFSEXITCODE(error);
 5125         return (error);
 5126 }
 5127 
 5128 /*
 5129  * Local lock unlock. Unlock all byte ranges that are no longer locked
 5130  * by NFSv4. To do this, unlock any subranges of first-->end that
 5131  * do not overlap with the byte ranges of any lock in the lfp->lf_lock
 5132  * list. This list has all locks for the file held by other
 5133  * <clientid, lockowner> tuples. The list is ordered by increasing
 5134  * lo_first value, but may have entries that overlap each other, for
 5135  * the case of read locks.
 5136  */
 5137 static void
 5138 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
 5139     uint64_t init_end, NFSPROC_T *p)
 5140 {
 5141         struct nfslock *lop;
 5142         uint64_t first, end, prevfirst;
 5143 
 5144         first = init_first;
 5145         end = init_end;
 5146         while (first < init_end) {
 5147                 /* Loop through all nfs locks, adjusting first and end */
 5148                 prevfirst = 0;
 5149                 LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
 5150                         KASSERT(prevfirst <= lop->lo_first,
 5151                             ("nfsv4 locks out of order"));
 5152                         KASSERT(lop->lo_first < lop->lo_end,
 5153                             ("nfsv4 bogus lock"));
 5154                         prevfirst = lop->lo_first;
 5155                         if (first >= lop->lo_first &&
 5156                             first < lop->lo_end)
 5157                                 /*
 5158                                  * Overlaps with initial part, so trim
 5159                                  * off that initial part by moving first past
 5160                                  * it.
 5161                                  */
 5162                                 first = lop->lo_end;
 5163                         else if (end > lop->lo_first &&
 5164                             lop->lo_first > first) {
 5165                                 /*
 5166                                  * This lock defines the end of the
 5167                                  * segment to unlock, so set end to the
 5168                                  * start of it and break out of the loop.
 5169                                  */
 5170                                 end = lop->lo_first;
 5171                                 break;
 5172                         }
 5173                         if (first >= end)
 5174                                 /*
 5175                                  * There is no segment left to do, so
 5176                                  * break out of this loop and then exit
 5177                                  * the outer while() since first will be set
 5178                                  * to end, which must equal init_end here.
 5179                                  */
 5180                                 break;
 5181                 }
 5182                 if (first < end) {
 5183                         /* Unlock this segment */
 5184                         (void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
 5185                             NFSLCK_READ, first, end, NULL, p);
 5186                         nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
 5187                             first, end);
 5188                 }
 5189                 /*
 5190                  * Now move past this segment and look for any further
 5191                  * segment in the range, if there is one.
 5192                  */
 5193                 first = end;
 5194                 end = init_end;
 5195         }
 5196 }
 5197 
 5198 /*
 5199  * Do the local lock operation and update the rollback list, as required.
 5200  * Perform the rollback and return the error if nfsvno_advlock() fails.
 5201  */
 5202 static int
 5203 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
 5204     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
 5205 {
 5206         struct nfsrollback *rlp;
 5207         int error = 0, ltype, oldltype;
 5208 
 5209         if (flags & NFSLCK_WRITE)
 5210                 ltype = F_WRLCK;
 5211         else if (flags & NFSLCK_READ)
 5212                 ltype = F_RDLCK;
 5213         else
 5214                 ltype = F_UNLCK;
 5215         if (oldflags & NFSLCK_WRITE)
 5216                 oldltype = F_WRLCK;
 5217         else if (oldflags & NFSLCK_READ)
 5218                 oldltype = F_RDLCK;
 5219         else
 5220                 oldltype = F_UNLCK;
 5221         if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
 5222                 /* nothing to do */
 5223                 goto out;
 5224         error = nfsvno_advlock(vp, ltype, first, end, p);
 5225         if (error != 0) {
 5226                 if (cfp != NULL) {
 5227                         cfp->cl_clientid.lval[0] = 0;
 5228                         cfp->cl_clientid.lval[1] = 0;
 5229                         cfp->cl_first = 0;
 5230                         cfp->cl_end = NFS64BITSSET;
 5231                         cfp->cl_flags = NFSLCK_WRITE;
 5232                         cfp->cl_ownerlen = 5;
 5233                         NFSBCOPY("LOCAL", cfp->cl_owner, 5);
 5234                 }
 5235                 nfsrv_locallock_rollback(vp, lfp, p);
 5236         } else if (ltype != F_UNLCK) {
 5237                 rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
 5238                     M_WAITOK);
 5239                 rlp->rlck_first = first;
 5240                 rlp->rlck_end = end;
 5241                 rlp->rlck_type = oldltype;
 5242                 LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
 5243         }
 5244 
 5245 out:
 5246         NFSEXITCODE(error);
 5247         return (error);
 5248 }
 5249 
 5250 /*
 5251  * Roll back local lock changes and free up the rollback list.
 5252  */
 5253 static void
 5254 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
 5255 {
 5256         struct nfsrollback *rlp, *nrlp;
 5257 
 5258         LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
 5259                 (void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
 5260                     rlp->rlck_end, p);
 5261                 free(rlp, M_NFSDROLLBACK);
 5262         }
 5263         LIST_INIT(&lfp->lf_rollback);
 5264 }
 5265 
 5266 /*
 5267  * Update local lock list and delete rollback list (ie now committed to the
 5268  * local locks). Most of the work is done by the internal function.
 5269  */
 5270 static void
 5271 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
 5272     uint64_t end)
 5273 {
 5274         struct nfsrollback *rlp, *nrlp;
 5275         struct nfslock *new_lop, *other_lop;
 5276 
 5277         new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
 5278         if (flags & (NFSLCK_READ | NFSLCK_WRITE))
 5279                 other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
 5280                     M_WAITOK);
 5281         else
 5282                 other_lop = NULL;
 5283         new_lop->lo_flags = flags;
 5284         new_lop->lo_first = first;
 5285         new_lop->lo_end = end;
 5286         nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
 5287         if (new_lop != NULL)
 5288                 free(new_lop, M_NFSDLOCK);
 5289         if (other_lop != NULL)
 5290                 free(other_lop, M_NFSDLOCK);
 5291 
 5292         /* and get rid of the rollback list */
 5293         LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
 5294                 free(rlp, M_NFSDROLLBACK);
 5295         LIST_INIT(&lfp->lf_rollback);
 5296 }
 5297 
 5298 /*
 5299  * Lock the struct nfslockfile for local lock updating.
 5300  */
 5301 static void
 5302 nfsrv_locklf(struct nfslockfile *lfp)
 5303 {
 5304         int gotlock;
 5305 
 5306         /* lf_usecount ensures *lfp won't be free'd */
 5307         lfp->lf_usecount++;
 5308         do {
 5309                 gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
 5310                     NFSSTATEMUTEXPTR, NULL);
 5311         } while (gotlock == 0);
 5312         lfp->lf_usecount--;
 5313 }
 5314 
 5315 /*
 5316  * Unlock the struct nfslockfile after local lock updating.
 5317  */
 5318 static void
 5319 nfsrv_unlocklf(struct nfslockfile *lfp)
 5320 {
 5321 
 5322         nfsv4_unlock(&lfp->lf_locallock_lck, 0);
 5323 }
 5324 
 5325 /*
 5326  * Clear out all state for the NFSv4 server.
 5327  * Must be called by a thread that can sleep when no nfsds are running.
 5328  */
 5329 void
 5330 nfsrv_throwawayallstate(NFSPROC_T *p)
 5331 {
 5332         struct nfsclient *clp, *nclp;
 5333         struct nfslockfile *lfp, *nlfp;
 5334         int i;
 5335 
 5336         /*
 5337          * For each client, clean out the state and then free the structure.
 5338          */
 5339         for (i = 0; i < NFSCLIENTHASHSIZE; i++) {
 5340                 LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
 5341                         nfsrv_cleanclient(clp, p);
 5342                         nfsrv_freedeleglist(&clp->lc_deleg);
 5343                         nfsrv_freedeleglist(&clp->lc_olddeleg);
 5344                         free(clp, M_NFSDCLIENT);
 5345                 }
 5346         }
 5347 
 5348         /*
 5349          * Also, free up any remaining lock file structures.
 5350          */
 5351         for (i = 0; i < NFSLOCKHASHSIZE; i++) {
 5352                 LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
 5353                         printf("nfsd unload: fnd a lock file struct\n");
 5354                         nfsrv_freenfslockfile(lfp);
 5355                 }
 5356         }
 5357 }
 5358 

Cache object: 22d25a31d182f01ae5c15a842a8d7c37


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


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.