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