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