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