xref: /freebsd/sys/fs/nfsserver/nfs_nfsdstate.c (revision e4c66ddabdb470bab319705c1834a4867c508a43)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Rick Macklem, University of Guelph
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #ifndef APPLEKEXT
34 #include <sys/extattr.h>
35 #include <fs/nfs/nfsport.h>
36 
37 struct nfsrv_stablefirst nfsrv_stablefirst;
38 int nfsrv_issuedelegs = 0;
39 int nfsrv_dolocallocks = 0;
40 struct nfsv4lock nfsv4rootfs_lock;
41 time_t nfsdev_time = 0;
42 int nfsrv_layouthashsize;
43 volatile int nfsrv_layoutcnt = 0;
44 
45 extern int newnfs_numnfsd;
46 extern struct nfsstatsv1 nfsstatsv1;
47 extern int nfsrv_lease;
48 extern struct timeval nfsboottime;
49 extern u_int32_t newnfs_true, newnfs_false;
50 extern struct mtx nfsrv_dslock_mtx;
51 extern struct mtx nfsrv_recalllock_mtx;
52 extern struct mtx nfsrv_dontlistlock_mtx;
53 extern int nfsd_debuglevel;
54 extern u_int nfsrv_dsdirsize;
55 extern struct nfsdevicehead nfsrv_devidhead;
56 extern int nfsrv_doflexfile;
57 extern int nfsrv_maxpnfsmirror;
58 NFSV4ROOTLOCKMUTEX;
59 NFSSTATESPINLOCK;
60 extern struct nfsdontlisthead nfsrv_dontlisthead;
61 extern volatile int nfsrv_devidcnt;
62 extern struct nfslayouthead nfsrv_recalllisthead;
63 extern char *nfsrv_zeropnfsdat;
64 
65 SYSCTL_DECL(_vfs_nfsd);
66 int	nfsrv_statehashsize = NFSSTATEHASHSIZE;
67 SYSCTL_INT(_vfs_nfsd, OID_AUTO, statehashsize, CTLFLAG_RDTUN,
68     &nfsrv_statehashsize, 0,
69     "Size of state hash table set via loader.conf");
70 
71 int	nfsrv_clienthashsize = NFSCLIENTHASHSIZE;
72 SYSCTL_INT(_vfs_nfsd, OID_AUTO, clienthashsize, CTLFLAG_RDTUN,
73     &nfsrv_clienthashsize, 0,
74     "Size of client hash table set via loader.conf");
75 
76 int	nfsrv_lockhashsize = NFSLOCKHASHSIZE;
77 SYSCTL_INT(_vfs_nfsd, OID_AUTO, fhhashsize, CTLFLAG_RDTUN,
78     &nfsrv_lockhashsize, 0,
79     "Size of file handle hash table set via loader.conf");
80 
81 int	nfsrv_sessionhashsize = NFSSESSIONHASHSIZE;
82 SYSCTL_INT(_vfs_nfsd, OID_AUTO, sessionhashsize, CTLFLAG_RDTUN,
83     &nfsrv_sessionhashsize, 0,
84     "Size of session hash table set via loader.conf");
85 
86 int	nfsrv_layouthighwater = NFSLAYOUTHIGHWATER;
87 SYSCTL_INT(_vfs_nfsd, OID_AUTO, layouthighwater, CTLFLAG_RDTUN,
88     &nfsrv_layouthighwater, 0,
89     "High water mark for number of layouts set via loader.conf");
90 
91 static int	nfsrv_v4statelimit = NFSRV_V4STATELIMIT;
92 SYSCTL_INT(_vfs_nfsd, OID_AUTO, v4statelimit, CTLFLAG_RWTUN,
93     &nfsrv_v4statelimit, 0,
94     "High water limit for NFSv4 opens+locks+delegations");
95 
96 static int	nfsrv_writedelegifpos = 0;
97 SYSCTL_INT(_vfs_nfsd, OID_AUTO, writedelegifpos, CTLFLAG_RW,
98     &nfsrv_writedelegifpos, 0,
99     "Issue a write delegation for read opens if possible");
100 
101 static int	nfsrv_allowreadforwriteopen = 1;
102 SYSCTL_INT(_vfs_nfsd, OID_AUTO, allowreadforwriteopen, CTLFLAG_RW,
103     &nfsrv_allowreadforwriteopen, 0,
104     "Allow Reads to be done with Write Access StateIDs");
105 
106 int	nfsrv_pnfsatime = 0;
107 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsstrictatime, CTLFLAG_RW,
108     &nfsrv_pnfsatime, 0,
109     "For pNFS service, do Getattr ops to keep atime up-to-date");
110 
111 int	nfsrv_flexlinuxhack = 0;
112 SYSCTL_INT(_vfs_nfsd, OID_AUTO, flexlinuxhack, CTLFLAG_RW,
113     &nfsrv_flexlinuxhack, 0,
114     "For Linux clients, hack around Flex File Layout bug");
115 
116 /*
117  * Hash lists for nfs V4.
118  */
119 struct nfsclienthashhead	*nfsclienthash;
120 struct nfslockhashhead		*nfslockhash;
121 struct nfssessionhash		*nfssessionhash;
122 struct nfslayouthash		*nfslayouthash;
123 volatile int nfsrv_dontlistlen = 0;
124 #endif	/* !APPLEKEXT */
125 
126 static u_int32_t nfsrv_openpluslock = 0, nfsrv_delegatecnt = 0;
127 static time_t nfsrvboottime;
128 static int nfsrv_returnoldstateid = 0, nfsrv_clients = 0;
129 static int nfsrv_clienthighwater = NFSRV_CLIENTHIGHWATER;
130 static int nfsrv_nogsscallback = 0;
131 static volatile int nfsrv_writedelegcnt = 0;
132 static int nfsrv_faildscnt;
133 
134 /* local functions */
135 static void nfsrv_dumpaclient(struct nfsclient *clp,
136     struct nfsd_dumpclients *dumpp);
137 static void nfsrv_freeopenowner(struct nfsstate *stp, int cansleep,
138     NFSPROC_T *p);
139 static int nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep,
140     NFSPROC_T *p);
141 static void nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
142     NFSPROC_T *p);
143 static void nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp,
144     int cansleep, NFSPROC_T *p);
145 static void nfsrv_freenfslock(struct nfslock *lop);
146 static void nfsrv_freenfslockfile(struct nfslockfile *lfp);
147 static void nfsrv_freedeleg(struct nfsstate *);
148 static int nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp,
149     u_int32_t flags, struct nfsstate **stpp);
150 static void nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
151     struct nfsstate **stpp);
152 static int nfsrv_getlockfh(vnode_t vp, u_short flags,
153     struct nfslockfile *new_lfp, fhandle_t *nfhp, NFSPROC_T *p);
154 static int nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
155     struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit);
156 static void nfsrv_insertlock(struct nfslock *new_lop,
157     struct nfslock *insert_lop, struct nfsstate *stp, struct nfslockfile *lfp);
158 static void nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
159     struct nfslock **other_lopp, struct nfslockfile *lfp);
160 static int nfsrv_getipnumber(u_char *cp);
161 static int nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
162     nfsv4stateid_t *stateidp, int specialid);
163 static int nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
164     u_int32_t flags);
165 static int nfsrv_docallback(struct nfsclient *clp, int procnum,
166     nfsv4stateid_t *stateidp, int trunc, fhandle_t *fhp,
167     struct nfsvattr *nap, nfsattrbit_t *attrbitp, int laytype, NFSPROC_T *p);
168 static int nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
169     uint32_t callback, int op, const char *optag, struct nfsdsession **sepp);
170 static u_int32_t nfsrv_nextclientindex(void);
171 static u_int32_t nfsrv_nextstateindex(struct nfsclient *clp);
172 static void nfsrv_markstable(struct nfsclient *clp);
173 static void nfsrv_markreclaim(struct nfsclient *clp);
174 static int nfsrv_checkstable(struct nfsclient *clp);
175 static int nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, struct
176     vnode *vp, NFSPROC_T *p);
177 static int nfsrv_delegconflict(struct nfsstate *stp, int *haslockp,
178     NFSPROC_T *p, vnode_t vp);
179 static int nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
180     struct nfsclient *clp, int *haslockp, NFSPROC_T *p);
181 static int nfsrv_notsamecredname(struct nfsrv_descript *nd,
182     struct nfsclient *clp);
183 static time_t nfsrv_leaseexpiry(void);
184 static void nfsrv_delaydelegtimeout(struct nfsstate *stp);
185 static int nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
186     struct nfsstate *stp, struct nfsrvcache *op);
187 static int nfsrv_nootherstate(struct nfsstate *stp);
188 static int nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
189     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p);
190 static void nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp,
191     uint64_t init_first, uint64_t init_end, NFSPROC_T *p);
192 static int nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags,
193     int oldflags, uint64_t first, uint64_t end, struct nfslockconflict *cfp,
194     NFSPROC_T *p);
195 static void nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp,
196     NFSPROC_T *p);
197 static void nfsrv_locallock_commit(struct nfslockfile *lfp, int flags,
198     uint64_t first, uint64_t end);
199 static void nfsrv_locklf(struct nfslockfile *lfp);
200 static void nfsrv_unlocklf(struct nfslockfile *lfp);
201 static struct nfsdsession *nfsrv_findsession(uint8_t *sessionid);
202 static int nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid);
203 static int nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
204     int dont_replycache, struct nfsdsession **sepp);
205 static int nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp);
206 static int nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
207     nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p);
208 static void nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp);
209 static void nfsrv_freelayoutlist(nfsquad_t clientid);
210 static void nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype,
211     int iomode);
212 static void nfsrv_freealllayouts(void);
213 static void nfsrv_freedevid(struct nfsdevice *ds);
214 static int nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
215     struct nfsdevice **dsp);
216 static int nfsrv_delds(char *devid, NFSPROC_T *p);
217 static void nfsrv_deleteds(struct nfsdevice *fndds);
218 static void nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost);
219 static void nfsrv_freealldevids(void);
220 static void nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp,
221     int maxcnt, NFSPROC_T *p);
222 static int nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp,
223     fhandle_t *fhp, struct nfslayout *lyp, struct nfslayouthead *lyheadp,
224     int laytype, NFSPROC_T *p);
225 static int nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
226     NFSPROC_T *, struct nfslayout **lypp);
227 static int nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt);
228 static struct nfslayout *nfsrv_filelayout(struct nfsrv_descript *nd, int iomode,
229     fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
230 static struct nfslayout *nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode,
231     int mirrorcnt, fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs);
232 static int nfsrv_dontlayout(fhandle_t *fhp);
233 static int nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
234     vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
235     vnode_t *tvpp);
236 static struct nfsdevice *nfsrv_findmirroredds(struct nfsmount *nmp);
237 
238 /*
239  * Scan the client list for a match and either return the current one,
240  * create a new entry or return an error.
241  * If returning a non-error, the clp structure must either be linked into
242  * the client list or free'd.
243  */
244 APPLESTATIC int
245 nfsrv_setclient(struct nfsrv_descript *nd, struct nfsclient **new_clpp,
246     nfsquad_t *clientidp, nfsquad_t *confirmp, NFSPROC_T *p)
247 {
248 	struct nfsclient *clp = NULL, *new_clp = *new_clpp;
249 	int i, error = 0, ret;
250 	struct nfsstate *stp, *tstp;
251 	struct sockaddr_in *sad, *rad;
252 	struct nfsdsession *sep, *nsep;
253 	int zapit = 0, gotit, hasstate = 0, igotlock;
254 	static u_int64_t confirm_index = 0;
255 
256 	/*
257 	 * Check for state resource limit exceeded.
258 	 */
259 	if (nfsrv_openpluslock > nfsrv_v4statelimit) {
260 		error = NFSERR_RESOURCE;
261 		goto out;
262 	}
263 
264 	if (nfsrv_issuedelegs == 0 ||
265 	    ((nd->nd_flag & ND_GSS) != 0 && nfsrv_nogsscallback != 0))
266 		/*
267 		 * Don't do callbacks when delegations are disabled or
268 		 * for AUTH_GSS unless enabled via nfsrv_nogsscallback.
269 		 * If establishing a callback connection is attempted
270 		 * when a firewall is blocking the callback path, the
271 		 * server may wait too long for the connect attempt to
272 		 * succeed during the Open. Some clients, such as Linux,
273 		 * may timeout and give up on the Open before the server
274 		 * replies. Also, since AUTH_GSS callbacks are not
275 		 * yet interoperability tested, they might cause the
276 		 * server to crap out, if they get past the Init call to
277 		 * the client.
278 		 */
279 		new_clp->lc_program = 0;
280 
281 	/* Lock out other nfsd threads */
282 	NFSLOCKV4ROOTMUTEX();
283 	nfsv4_relref(&nfsv4rootfs_lock);
284 	do {
285 		igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
286 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
287 	} while (!igotlock);
288 	NFSUNLOCKV4ROOTMUTEX();
289 
290 	/*
291 	 * Search for a match in the client list.
292 	 */
293 	gotit = i = 0;
294 	while (i < nfsrv_clienthashsize && !gotit) {
295 	    LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
296 		if (new_clp->lc_idlen == clp->lc_idlen &&
297 		    !NFSBCMP(new_clp->lc_id, clp->lc_id, clp->lc_idlen)) {
298 			gotit = 1;
299 			break;
300 		}
301 	    }
302 	    if (gotit == 0)
303 		i++;
304 	}
305 	if (!gotit ||
306 	    (clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_ADMINREVOKED))) {
307 		if ((nd->nd_flag & ND_NFSV41) != 0 && confirmp->lval[1] != 0) {
308 			/*
309 			 * For NFSv4.1, if confirmp->lval[1] is non-zero, the
310 			 * client is trying to update a confirmed clientid.
311 			 */
312 			NFSLOCKV4ROOTMUTEX();
313 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
314 			NFSUNLOCKV4ROOTMUTEX();
315 			confirmp->lval[1] = 0;
316 			error = NFSERR_NOENT;
317 			goto out;
318 		}
319 		/*
320 		 * Get rid of the old one.
321 		 */
322 		if (i != nfsrv_clienthashsize) {
323 			LIST_REMOVE(clp, lc_hash);
324 			nfsrv_cleanclient(clp, p);
325 			nfsrv_freedeleglist(&clp->lc_deleg);
326 			nfsrv_freedeleglist(&clp->lc_olddeleg);
327 			zapit = 1;
328 		}
329 		/*
330 		 * Add it after assigning a client id to it.
331 		 */
332 		new_clp->lc_flags |= LCL_NEEDSCONFIRM;
333 		if ((nd->nd_flag & ND_NFSV41) != 0)
334 			new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
335 			    ++confirm_index;
336 		else
337 			confirmp->qval = new_clp->lc_confirm.qval =
338 			    ++confirm_index;
339 		clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
340 		    (u_int32_t)nfsrvboottime;
341 		clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
342 		    nfsrv_nextclientindex();
343 		new_clp->lc_stateindex = 0;
344 		new_clp->lc_statemaxindex = 0;
345 		new_clp->lc_cbref = 0;
346 		new_clp->lc_expiry = nfsrv_leaseexpiry();
347 		LIST_INIT(&new_clp->lc_open);
348 		LIST_INIT(&new_clp->lc_deleg);
349 		LIST_INIT(&new_clp->lc_olddeleg);
350 		LIST_INIT(&new_clp->lc_session);
351 		for (i = 0; i < nfsrv_statehashsize; i++)
352 			LIST_INIT(&new_clp->lc_stateid[i]);
353 		LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
354 		    lc_hash);
355 		nfsstatsv1.srvclients++;
356 		nfsrv_openpluslock++;
357 		nfsrv_clients++;
358 		NFSLOCKV4ROOTMUTEX();
359 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
360 		NFSUNLOCKV4ROOTMUTEX();
361 		if (zapit)
362 			nfsrv_zapclient(clp, p);
363 		*new_clpp = NULL;
364 		goto out;
365 	}
366 
367 	/*
368 	 * Now, handle the cases where the id is already issued.
369 	 */
370 	if (nfsrv_notsamecredname(nd, clp)) {
371 	    /*
372 	     * Check to see if there is expired state that should go away.
373 	     */
374 	    if (clp->lc_expiry < NFSD_MONOSEC &&
375 	        (!LIST_EMPTY(&clp->lc_open) || !LIST_EMPTY(&clp->lc_deleg))) {
376 		nfsrv_cleanclient(clp, p);
377 		nfsrv_freedeleglist(&clp->lc_deleg);
378 	    }
379 
380 	    /*
381 	     * If there is outstanding state, then reply NFSERR_CLIDINUSE per
382 	     * RFC3530 Sec. 8.1.2 last para.
383 	     */
384 	    if (!LIST_EMPTY(&clp->lc_deleg)) {
385 		hasstate = 1;
386 	    } else if (LIST_EMPTY(&clp->lc_open)) {
387 		hasstate = 0;
388 	    } else {
389 		hasstate = 0;
390 		/* Look for an Open on the OpenOwner */
391 		LIST_FOREACH(stp, &clp->lc_open, ls_list) {
392 		    if (!LIST_EMPTY(&stp->ls_open)) {
393 			hasstate = 1;
394 			break;
395 		    }
396 		}
397 	    }
398 	    if (hasstate) {
399 		/*
400 		 * If the uid doesn't match, return NFSERR_CLIDINUSE after
401 		 * filling out the correct ipaddr and portnum.
402 		 */
403 		sad = NFSSOCKADDR(new_clp->lc_req.nr_nam, struct sockaddr_in *);
404 		rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
405 		sad->sin_addr.s_addr = rad->sin_addr.s_addr;
406 		sad->sin_port = rad->sin_port;
407 		NFSLOCKV4ROOTMUTEX();
408 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
409 		NFSUNLOCKV4ROOTMUTEX();
410 		error = NFSERR_CLIDINUSE;
411 		goto out;
412 	    }
413 	}
414 
415 	if (NFSBCMP(new_clp->lc_verf, clp->lc_verf, NFSX_VERF)) {
416 		/*
417 		 * If the verifier has changed, the client has rebooted
418 		 * and a new client id is issued. The old state info
419 		 * can be thrown away once the SETCLIENTID_CONFIRM occurs.
420 		 */
421 		LIST_REMOVE(clp, lc_hash);
422 
423 		/* Get rid of all sessions on this clientid. */
424 		LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep) {
425 			ret = nfsrv_freesession(sep, NULL);
426 			if (ret != 0)
427 				printf("nfsrv_setclient: verifier changed free"
428 				    " session failed=%d\n", ret);
429 		}
430 
431 		new_clp->lc_flags |= LCL_NEEDSCONFIRM;
432 		if ((nd->nd_flag & ND_NFSV41) != 0)
433 			new_clp->lc_confirm.lval[0] = confirmp->lval[0] =
434 			    ++confirm_index;
435 		else
436 			confirmp->qval = new_clp->lc_confirm.qval =
437 			    ++confirm_index;
438 		clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
439 		    nfsrvboottime;
440 		clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
441 		    nfsrv_nextclientindex();
442 		new_clp->lc_stateindex = 0;
443 		new_clp->lc_statemaxindex = 0;
444 		new_clp->lc_cbref = 0;
445 		new_clp->lc_expiry = nfsrv_leaseexpiry();
446 
447 		/*
448 		 * Save the state until confirmed.
449 		 */
450 		LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
451 		LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
452 			tstp->ls_clp = new_clp;
453 		LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
454 		LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
455 			tstp->ls_clp = new_clp;
456 		LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg,
457 		    ls_list);
458 		LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
459 			tstp->ls_clp = new_clp;
460 		for (i = 0; i < nfsrv_statehashsize; i++) {
461 			LIST_NEWHEAD(&new_clp->lc_stateid[i],
462 			    &clp->lc_stateid[i], ls_hash);
463 			LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
464 				tstp->ls_clp = new_clp;
465 		}
466 		LIST_INIT(&new_clp->lc_session);
467 		LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
468 		    lc_hash);
469 		nfsstatsv1.srvclients++;
470 		nfsrv_openpluslock++;
471 		nfsrv_clients++;
472 		NFSLOCKV4ROOTMUTEX();
473 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
474 		NFSUNLOCKV4ROOTMUTEX();
475 
476 		/*
477 		 * Must wait until any outstanding callback on the old clp
478 		 * completes.
479 		 */
480 		NFSLOCKSTATE();
481 		while (clp->lc_cbref) {
482 			clp->lc_flags |= LCL_WAKEUPWANTED;
483 			(void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
484 			    "nfsd clp", 10 * hz);
485 		}
486 		NFSUNLOCKSTATE();
487 		nfsrv_zapclient(clp, p);
488 		*new_clpp = NULL;
489 		goto out;
490 	}
491 
492 	/* For NFSv4.1, mark that we found a confirmed clientid. */
493 	if ((nd->nd_flag & ND_NFSV41) != 0) {
494 		clientidp->lval[0] = clp->lc_clientid.lval[0];
495 		clientidp->lval[1] = clp->lc_clientid.lval[1];
496 		confirmp->lval[0] = 0;	/* Ignored by client */
497 		confirmp->lval[1] = 1;
498 	} else {
499 		/*
500 		 * id and verifier match, so update the net address info
501 		 * and get rid of any existing callback authentication
502 		 * handle, so a new one will be acquired.
503 		 */
504 		LIST_REMOVE(clp, lc_hash);
505 		new_clp->lc_flags |= (LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
506 		new_clp->lc_expiry = nfsrv_leaseexpiry();
507 		confirmp->qval = new_clp->lc_confirm.qval = ++confirm_index;
508 		clientidp->lval[0] = new_clp->lc_clientid.lval[0] =
509 		    clp->lc_clientid.lval[0];
510 		clientidp->lval[1] = new_clp->lc_clientid.lval[1] =
511 		    clp->lc_clientid.lval[1];
512 		new_clp->lc_delegtime = clp->lc_delegtime;
513 		new_clp->lc_stateindex = clp->lc_stateindex;
514 		new_clp->lc_statemaxindex = clp->lc_statemaxindex;
515 		new_clp->lc_cbref = 0;
516 		LIST_NEWHEAD(&new_clp->lc_open, &clp->lc_open, ls_list);
517 		LIST_FOREACH(tstp, &new_clp->lc_open, ls_list)
518 			tstp->ls_clp = new_clp;
519 		LIST_NEWHEAD(&new_clp->lc_deleg, &clp->lc_deleg, ls_list);
520 		LIST_FOREACH(tstp, &new_clp->lc_deleg, ls_list)
521 			tstp->ls_clp = new_clp;
522 		LIST_NEWHEAD(&new_clp->lc_olddeleg, &clp->lc_olddeleg, ls_list);
523 		LIST_FOREACH(tstp, &new_clp->lc_olddeleg, ls_list)
524 			tstp->ls_clp = new_clp;
525 		for (i = 0; i < nfsrv_statehashsize; i++) {
526 			LIST_NEWHEAD(&new_clp->lc_stateid[i],
527 			    &clp->lc_stateid[i], ls_hash);
528 			LIST_FOREACH(tstp, &new_clp->lc_stateid[i], ls_hash)
529 				tstp->ls_clp = new_clp;
530 		}
531 		LIST_INIT(&new_clp->lc_session);
532 		LIST_INSERT_HEAD(NFSCLIENTHASH(new_clp->lc_clientid), new_clp,
533 		    lc_hash);
534 		nfsstatsv1.srvclients++;
535 		nfsrv_openpluslock++;
536 		nfsrv_clients++;
537 	}
538 	NFSLOCKV4ROOTMUTEX();
539 	nfsv4_unlock(&nfsv4rootfs_lock, 1);
540 	NFSUNLOCKV4ROOTMUTEX();
541 
542 	if ((nd->nd_flag & ND_NFSV41) == 0) {
543 		/*
544 		 * Must wait until any outstanding callback on the old clp
545 		 * completes.
546 		 */
547 		NFSLOCKSTATE();
548 		while (clp->lc_cbref) {
549 			clp->lc_flags |= LCL_WAKEUPWANTED;
550 			(void)mtx_sleep(clp, NFSSTATEMUTEXPTR, PZERO - 1,
551 			    "nfsdclp", 10 * hz);
552 		}
553 		NFSUNLOCKSTATE();
554 		nfsrv_zapclient(clp, p);
555 		*new_clpp = NULL;
556 	}
557 
558 out:
559 	NFSEXITCODE2(error, nd);
560 	return (error);
561 }
562 
563 /*
564  * Check to see if the client id exists and optionally confirm it.
565  */
566 APPLESTATIC int
567 nfsrv_getclient(nfsquad_t clientid, int opflags, struct nfsclient **clpp,
568     struct nfsdsession *nsep, nfsquad_t confirm, uint32_t cbprogram,
569     struct nfsrv_descript *nd, NFSPROC_T *p)
570 {
571 	struct nfsclient *clp;
572 	struct nfsstate *stp;
573 	int i;
574 	struct nfsclienthashhead *hp;
575 	int error = 0, igotlock, doneok;
576 	struct nfssessionhash *shp;
577 	struct nfsdsession *sep;
578 	uint64_t sessid[2];
579 	static uint64_t next_sess = 0;
580 
581 	if (clpp)
582 		*clpp = NULL;
583 	if ((nd == NULL || (nd->nd_flag & ND_NFSV41) == 0 ||
584 	    opflags != CLOPS_RENEW) && nfsrvboottime != clientid.lval[0]) {
585 		error = NFSERR_STALECLIENTID;
586 		goto out;
587 	}
588 
589 	/*
590 	 * If called with opflags == CLOPS_RENEW, the State Lock is
591 	 * already held. Otherwise, we need to get either that or,
592 	 * for the case of Confirm, lock out the nfsd threads.
593 	 */
594 	if (opflags & CLOPS_CONFIRM) {
595 		NFSLOCKV4ROOTMUTEX();
596 		nfsv4_relref(&nfsv4rootfs_lock);
597 		do {
598 			igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
599 			    NFSV4ROOTLOCKMUTEXPTR, NULL);
600 		} while (!igotlock);
601 		/*
602 		 * Create a new sessionid here, since we need to do it where
603 		 * there is a mutex held to serialize update of next_sess.
604 		 */
605 		if ((nd->nd_flag & ND_NFSV41) != 0) {
606 			sessid[0] = ++next_sess;
607 			sessid[1] = clientid.qval;
608 		}
609 		NFSUNLOCKV4ROOTMUTEX();
610 	} else if (opflags != CLOPS_RENEW) {
611 		NFSLOCKSTATE();
612 	}
613 
614 	/* For NFSv4.1, the clp is acquired from the associated session. */
615 	if (nd != NULL && (nd->nd_flag & ND_NFSV41) != 0 &&
616 	    opflags == CLOPS_RENEW) {
617 		clp = NULL;
618 		if ((nd->nd_flag & ND_HASSEQUENCE) != 0) {
619 			shp = NFSSESSIONHASH(nd->nd_sessionid);
620 			NFSLOCKSESSION(shp);
621 			sep = nfsrv_findsession(nd->nd_sessionid);
622 			if (sep != NULL)
623 				clp = sep->sess_clp;
624 			NFSUNLOCKSESSION(shp);
625 		}
626 	} else {
627 		hp = NFSCLIENTHASH(clientid);
628 		LIST_FOREACH(clp, hp, lc_hash) {
629 			if (clp->lc_clientid.lval[1] == clientid.lval[1])
630 				break;
631 		}
632 	}
633 	if (clp == NULL) {
634 		if (opflags & CLOPS_CONFIRM)
635 			error = NFSERR_STALECLIENTID;
636 		else
637 			error = NFSERR_EXPIRED;
638 	} else if (clp->lc_flags & LCL_ADMINREVOKED) {
639 		/*
640 		 * If marked admin revoked, just return the error.
641 		 */
642 		error = NFSERR_ADMINREVOKED;
643 	}
644 	if (error) {
645 		if (opflags & CLOPS_CONFIRM) {
646 			NFSLOCKV4ROOTMUTEX();
647 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
648 			NFSUNLOCKV4ROOTMUTEX();
649 		} else if (opflags != CLOPS_RENEW) {
650 			NFSUNLOCKSTATE();
651 		}
652 		goto out;
653 	}
654 
655 	/*
656 	 * Perform any operations specified by the opflags.
657 	 */
658 	if (opflags & CLOPS_CONFIRM) {
659 		if (((nd->nd_flag & ND_NFSV41) != 0 &&
660 		     clp->lc_confirm.lval[0] != confirm.lval[0]) ||
661 		    ((nd->nd_flag & ND_NFSV41) == 0 &&
662 		     clp->lc_confirm.qval != confirm.qval))
663 			error = NFSERR_STALECLIENTID;
664 		else if (nfsrv_notsamecredname(nd, clp))
665 			error = NFSERR_CLIDINUSE;
666 
667 		if (!error) {
668 		    if ((clp->lc_flags & (LCL_NEEDSCONFIRM | LCL_DONTCLEAN)) ==
669 			LCL_NEEDSCONFIRM) {
670 			/*
671 			 * Hang onto the delegations (as old delegations)
672 			 * for an Open with CLAIM_DELEGATE_PREV unless in
673 			 * grace, but get rid of the rest of the state.
674 			 */
675 			nfsrv_cleanclient(clp, p);
676 			nfsrv_freedeleglist(&clp->lc_olddeleg);
677 			if (nfsrv_checkgrace(nd, clp, 0)) {
678 			    /* In grace, so just delete delegations */
679 			    nfsrv_freedeleglist(&clp->lc_deleg);
680 			} else {
681 			    LIST_FOREACH(stp, &clp->lc_deleg, ls_list)
682 				stp->ls_flags |= NFSLCK_OLDDELEG;
683 			    clp->lc_delegtime = NFSD_MONOSEC +
684 				nfsrv_lease + NFSRV_LEASEDELTA;
685 			    LIST_NEWHEAD(&clp->lc_olddeleg, &clp->lc_deleg,
686 				ls_list);
687 			}
688 			if ((nd->nd_flag & ND_NFSV41) != 0)
689 			    clp->lc_program = cbprogram;
690 		    }
691 		    clp->lc_flags &= ~(LCL_NEEDSCONFIRM | LCL_DONTCLEAN);
692 		    if (clp->lc_program)
693 			clp->lc_flags |= LCL_NEEDSCBNULL;
694 		    /* For NFSv4.1, link the session onto the client. */
695 		    if (nsep != NULL) {
696 			/* Hold a reference on the xprt for a backchannel. */
697 			if ((nsep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN)
698 			    != 0) {
699 			    if (clp->lc_req.nr_client == NULL)
700 				clp->lc_req.nr_client = (struct __rpc_client *)
701 				    clnt_bck_create(nd->nd_xprt->xp_socket,
702 				    cbprogram, NFSV4_CBVERS);
703 			    if (clp->lc_req.nr_client != NULL) {
704 				SVC_ACQUIRE(nd->nd_xprt);
705 				nd->nd_xprt->xp_p2 =
706 				    clp->lc_req.nr_client->cl_private;
707 				/* Disable idle timeout. */
708 				nd->nd_xprt->xp_idletimeout = 0;
709 				nsep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
710 			    } else
711 				nsep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
712 			}
713 			NFSBCOPY(sessid, nsep->sess_sessionid,
714 			    NFSX_V4SESSIONID);
715 			NFSBCOPY(sessid, nsep->sess_cbsess.nfsess_sessionid,
716 			    NFSX_V4SESSIONID);
717 			shp = NFSSESSIONHASH(nsep->sess_sessionid);
718 			NFSLOCKSTATE();
719 			NFSLOCKSESSION(shp);
720 			LIST_INSERT_HEAD(&shp->list, nsep, sess_hash);
721 			LIST_INSERT_HEAD(&clp->lc_session, nsep, sess_list);
722 			nsep->sess_clp = clp;
723 			NFSUNLOCKSESSION(shp);
724 			NFSUNLOCKSTATE();
725 		    }
726 		}
727 	} else if (clp->lc_flags & LCL_NEEDSCONFIRM) {
728 		error = NFSERR_EXPIRED;
729 	}
730 
731 	/*
732 	 * If called by the Renew Op, we must check the principal.
733 	 */
734 	if (!error && (opflags & CLOPS_RENEWOP)) {
735 	    if (nfsrv_notsamecredname(nd, clp)) {
736 		doneok = 0;
737 		for (i = 0; i < nfsrv_statehashsize && doneok == 0; i++) {
738 		    LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
739 			if ((stp->ls_flags & NFSLCK_OPEN) &&
740 			    stp->ls_uid == nd->nd_cred->cr_uid) {
741 				doneok = 1;
742 				break;
743 			}
744 		    }
745 		}
746 		if (!doneok)
747 			error = NFSERR_ACCES;
748 	    }
749 	    if (!error && (clp->lc_flags & LCL_CBDOWN))
750 		error = NFSERR_CBPATHDOWN;
751 	}
752 	if ((!error || error == NFSERR_CBPATHDOWN) &&
753 	     (opflags & CLOPS_RENEW)) {
754 		clp->lc_expiry = nfsrv_leaseexpiry();
755 	}
756 	if (opflags & CLOPS_CONFIRM) {
757 		NFSLOCKV4ROOTMUTEX();
758 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
759 		NFSUNLOCKV4ROOTMUTEX();
760 	} else if (opflags != CLOPS_RENEW) {
761 		NFSUNLOCKSTATE();
762 	}
763 	if (clpp)
764 		*clpp = clp;
765 
766 out:
767 	NFSEXITCODE2(error, nd);
768 	return (error);
769 }
770 
771 /*
772  * Perform the NFSv4.1 destroy clientid.
773  */
774 int
775 nfsrv_destroyclient(nfsquad_t clientid, NFSPROC_T *p)
776 {
777 	struct nfsclient *clp;
778 	struct nfsclienthashhead *hp;
779 	int error = 0, i, igotlock;
780 
781 	if (nfsrvboottime != clientid.lval[0]) {
782 		error = NFSERR_STALECLIENTID;
783 		goto out;
784 	}
785 
786 	/* Lock out other nfsd threads */
787 	NFSLOCKV4ROOTMUTEX();
788 	nfsv4_relref(&nfsv4rootfs_lock);
789 	do {
790 		igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
791 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
792 	} while (igotlock == 0);
793 	NFSUNLOCKV4ROOTMUTEX();
794 
795 	hp = NFSCLIENTHASH(clientid);
796 	LIST_FOREACH(clp, hp, lc_hash) {
797 		if (clp->lc_clientid.lval[1] == clientid.lval[1])
798 			break;
799 	}
800 	if (clp == NULL) {
801 		NFSLOCKV4ROOTMUTEX();
802 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
803 		NFSUNLOCKV4ROOTMUTEX();
804 		/* Just return ok, since it is gone. */
805 		goto out;
806 	}
807 
808 	/*
809 	 * Free up all layouts on the clientid.  Should the client return the
810 	 * layouts?
811 	 */
812 	nfsrv_freelayoutlist(clientid);
813 
814 	/* Scan for state on the clientid. */
815 	for (i = 0; i < nfsrv_statehashsize; i++)
816 		if (!LIST_EMPTY(&clp->lc_stateid[i])) {
817 			NFSLOCKV4ROOTMUTEX();
818 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
819 			NFSUNLOCKV4ROOTMUTEX();
820 			error = NFSERR_CLIENTIDBUSY;
821 			goto out;
822 		}
823 	if (!LIST_EMPTY(&clp->lc_session) || !LIST_EMPTY(&clp->lc_deleg)) {
824 		NFSLOCKV4ROOTMUTEX();
825 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
826 		NFSUNLOCKV4ROOTMUTEX();
827 		error = NFSERR_CLIENTIDBUSY;
828 		goto out;
829 	}
830 
831 	/* Destroy the clientid and return ok. */
832 	nfsrv_cleanclient(clp, p);
833 	nfsrv_freedeleglist(&clp->lc_deleg);
834 	nfsrv_freedeleglist(&clp->lc_olddeleg);
835 	LIST_REMOVE(clp, lc_hash);
836 	NFSLOCKV4ROOTMUTEX();
837 	nfsv4_unlock(&nfsv4rootfs_lock, 1);
838 	NFSUNLOCKV4ROOTMUTEX();
839 	nfsrv_zapclient(clp, p);
840 out:
841 	NFSEXITCODE2(error, nd);
842 	return (error);
843 }
844 
845 /*
846  * Called from the new nfssvc syscall to admin revoke a clientid.
847  * Returns 0 for success, error otherwise.
848  */
849 APPLESTATIC int
850 nfsrv_adminrevoke(struct nfsd_clid *revokep, NFSPROC_T *p)
851 {
852 	struct nfsclient *clp = NULL;
853 	int i, error = 0;
854 	int gotit, igotlock;
855 
856 	/*
857 	 * First, lock out the nfsd so that state won't change while the
858 	 * revocation record is being written to the stable storage restart
859 	 * file.
860 	 */
861 	NFSLOCKV4ROOTMUTEX();
862 	do {
863 		igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
864 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
865 	} while (!igotlock);
866 	NFSUNLOCKV4ROOTMUTEX();
867 
868 	/*
869 	 * Search for a match in the client list.
870 	 */
871 	gotit = i = 0;
872 	while (i < nfsrv_clienthashsize && !gotit) {
873 	    LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
874 		if (revokep->nclid_idlen == clp->lc_idlen &&
875 		    !NFSBCMP(revokep->nclid_id, clp->lc_id, clp->lc_idlen)) {
876 			gotit = 1;
877 			break;
878 		}
879 	    }
880 	    i++;
881 	}
882 	if (!gotit) {
883 		NFSLOCKV4ROOTMUTEX();
884 		nfsv4_unlock(&nfsv4rootfs_lock, 0);
885 		NFSUNLOCKV4ROOTMUTEX();
886 		error = EPERM;
887 		goto out;
888 	}
889 
890 	/*
891 	 * Now, write out the revocation record
892 	 */
893 	nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
894 	nfsrv_backupstable();
895 
896 	/*
897 	 * and clear out the state, marking the clientid revoked.
898 	 */
899 	clp->lc_flags &= ~LCL_CALLBACKSON;
900 	clp->lc_flags |= LCL_ADMINREVOKED;
901 	nfsrv_cleanclient(clp, p);
902 	nfsrv_freedeleglist(&clp->lc_deleg);
903 	nfsrv_freedeleglist(&clp->lc_olddeleg);
904 	NFSLOCKV4ROOTMUTEX();
905 	nfsv4_unlock(&nfsv4rootfs_lock, 0);
906 	NFSUNLOCKV4ROOTMUTEX();
907 
908 out:
909 	NFSEXITCODE(error);
910 	return (error);
911 }
912 
913 /*
914  * Dump out stats for all clients. Called from nfssvc(2), that is used
915  * nfsstatsv1.
916  */
917 APPLESTATIC void
918 nfsrv_dumpclients(struct nfsd_dumpclients *dumpp, int maxcnt)
919 {
920 	struct nfsclient *clp;
921 	int i = 0, cnt = 0;
922 
923 	/*
924 	 * First, get a reference on the nfsv4rootfs_lock so that an
925 	 * exclusive lock cannot be acquired while dumping the clients.
926 	 */
927 	NFSLOCKV4ROOTMUTEX();
928 	nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
929 	NFSUNLOCKV4ROOTMUTEX();
930 	NFSLOCKSTATE();
931 	/*
932 	 * Rattle through the client lists until done.
933 	 */
934 	while (i < nfsrv_clienthashsize && cnt < maxcnt) {
935 	    clp = LIST_FIRST(&nfsclienthash[i]);
936 	    while (clp != LIST_END(&nfsclienthash[i]) && cnt < maxcnt) {
937 		nfsrv_dumpaclient(clp, &dumpp[cnt]);
938 		cnt++;
939 		clp = LIST_NEXT(clp, lc_hash);
940 	    }
941 	    i++;
942 	}
943 	if (cnt < maxcnt)
944 	    dumpp[cnt].ndcl_clid.nclid_idlen = 0;
945 	NFSUNLOCKSTATE();
946 	NFSLOCKV4ROOTMUTEX();
947 	nfsv4_relref(&nfsv4rootfs_lock);
948 	NFSUNLOCKV4ROOTMUTEX();
949 }
950 
951 /*
952  * Dump stats for a client. Must be called with the NFSSTATELOCK and spl'd.
953  */
954 static void
955 nfsrv_dumpaclient(struct nfsclient *clp, struct nfsd_dumpclients *dumpp)
956 {
957 	struct nfsstate *stp, *openstp, *lckownstp;
958 	struct nfslock *lop;
959 	struct sockaddr *sad;
960 	struct sockaddr_in *rad;
961 	struct sockaddr_in6 *rad6;
962 
963 	dumpp->ndcl_nopenowners = dumpp->ndcl_nlockowners = 0;
964 	dumpp->ndcl_nopens = dumpp->ndcl_nlocks = 0;
965 	dumpp->ndcl_ndelegs = dumpp->ndcl_nolddelegs = 0;
966 	dumpp->ndcl_flags = clp->lc_flags;
967 	dumpp->ndcl_clid.nclid_idlen = clp->lc_idlen;
968 	NFSBCOPY(clp->lc_id, dumpp->ndcl_clid.nclid_id, clp->lc_idlen);
969 	sad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr *);
970 	dumpp->ndcl_addrfam = sad->sa_family;
971 	if (sad->sa_family == AF_INET) {
972 		rad = (struct sockaddr_in *)sad;
973 		dumpp->ndcl_cbaddr.sin_addr = rad->sin_addr;
974 	} else {
975 		rad6 = (struct sockaddr_in6 *)sad;
976 		dumpp->ndcl_cbaddr.sin6_addr = rad6->sin6_addr;
977 	}
978 
979 	/*
980 	 * Now, scan the state lists and total up the opens and locks.
981 	 */
982 	LIST_FOREACH(stp, &clp->lc_open, ls_list) {
983 	    dumpp->ndcl_nopenowners++;
984 	    LIST_FOREACH(openstp, &stp->ls_open, ls_list) {
985 		dumpp->ndcl_nopens++;
986 		LIST_FOREACH(lckownstp, &openstp->ls_open, ls_list) {
987 		    dumpp->ndcl_nlockowners++;
988 		    LIST_FOREACH(lop, &lckownstp->ls_lock, lo_lckowner) {
989 			dumpp->ndcl_nlocks++;
990 		    }
991 		}
992 	    }
993 	}
994 
995 	/*
996 	 * and the delegation lists.
997 	 */
998 	LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
999 	    dumpp->ndcl_ndelegs++;
1000 	}
1001 	LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
1002 	    dumpp->ndcl_nolddelegs++;
1003 	}
1004 }
1005 
1006 /*
1007  * Dump out lock stats for a file.
1008  */
1009 APPLESTATIC void
1010 nfsrv_dumplocks(vnode_t vp, struct nfsd_dumplocks *ldumpp, int maxcnt,
1011     NFSPROC_T *p)
1012 {
1013 	struct nfsstate *stp;
1014 	struct nfslock *lop;
1015 	int cnt = 0;
1016 	struct nfslockfile *lfp;
1017 	struct sockaddr *sad;
1018 	struct sockaddr_in *rad;
1019 	struct sockaddr_in6 *rad6;
1020 	int ret;
1021 	fhandle_t nfh;
1022 
1023 	ret = nfsrv_getlockfh(vp, 0, NULL, &nfh, p);
1024 	/*
1025 	 * First, get a reference on the nfsv4rootfs_lock so that an
1026 	 * exclusive lock on it cannot be acquired while dumping the locks.
1027 	 */
1028 	NFSLOCKV4ROOTMUTEX();
1029 	nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
1030 	NFSUNLOCKV4ROOTMUTEX();
1031 	NFSLOCKSTATE();
1032 	if (!ret)
1033 		ret = nfsrv_getlockfile(0, NULL, &lfp, &nfh, 0);
1034 	if (ret) {
1035 		ldumpp[0].ndlck_clid.nclid_idlen = 0;
1036 		NFSUNLOCKSTATE();
1037 		NFSLOCKV4ROOTMUTEX();
1038 		nfsv4_relref(&nfsv4rootfs_lock);
1039 		NFSUNLOCKV4ROOTMUTEX();
1040 		return;
1041 	}
1042 
1043 	/*
1044 	 * For each open share on file, dump it out.
1045 	 */
1046 	stp = LIST_FIRST(&lfp->lf_open);
1047 	while (stp != LIST_END(&lfp->lf_open) && cnt < maxcnt) {
1048 		ldumpp[cnt].ndlck_flags = stp->ls_flags;
1049 		ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1050 		ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1051 		ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1052 		ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1053 		ldumpp[cnt].ndlck_owner.nclid_idlen =
1054 		    stp->ls_openowner->ls_ownerlen;
1055 		NFSBCOPY(stp->ls_openowner->ls_owner,
1056 		    ldumpp[cnt].ndlck_owner.nclid_id,
1057 		    stp->ls_openowner->ls_ownerlen);
1058 		ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1059 		NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1060 		    stp->ls_clp->lc_idlen);
1061 		sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1062 		ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1063 		if (sad->sa_family == AF_INET) {
1064 			rad = (struct sockaddr_in *)sad;
1065 			ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1066 		} else {
1067 			rad6 = (struct sockaddr_in6 *)sad;
1068 			ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1069 		}
1070 		stp = LIST_NEXT(stp, ls_file);
1071 		cnt++;
1072 	}
1073 
1074 	/*
1075 	 * and all locks.
1076 	 */
1077 	lop = LIST_FIRST(&lfp->lf_lock);
1078 	while (lop != LIST_END(&lfp->lf_lock) && cnt < maxcnt) {
1079 		stp = lop->lo_stp;
1080 		ldumpp[cnt].ndlck_flags = lop->lo_flags;
1081 		ldumpp[cnt].ndlck_first = lop->lo_first;
1082 		ldumpp[cnt].ndlck_end = lop->lo_end;
1083 		ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1084 		ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1085 		ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1086 		ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1087 		ldumpp[cnt].ndlck_owner.nclid_idlen = stp->ls_ownerlen;
1088 		NFSBCOPY(stp->ls_owner, ldumpp[cnt].ndlck_owner.nclid_id,
1089 		    stp->ls_ownerlen);
1090 		ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1091 		NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1092 		    stp->ls_clp->lc_idlen);
1093 		sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1094 		ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1095 		if (sad->sa_family == AF_INET) {
1096 			rad = (struct sockaddr_in *)sad;
1097 			ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1098 		} else {
1099 			rad6 = (struct sockaddr_in6 *)sad;
1100 			ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1101 		}
1102 		lop = LIST_NEXT(lop, lo_lckfile);
1103 		cnt++;
1104 	}
1105 
1106 	/*
1107 	 * and the delegations.
1108 	 */
1109 	stp = LIST_FIRST(&lfp->lf_deleg);
1110 	while (stp != LIST_END(&lfp->lf_deleg) && cnt < maxcnt) {
1111 		ldumpp[cnt].ndlck_flags = stp->ls_flags;
1112 		ldumpp[cnt].ndlck_stateid.seqid = stp->ls_stateid.seqid;
1113 		ldumpp[cnt].ndlck_stateid.other[0] = stp->ls_stateid.other[0];
1114 		ldumpp[cnt].ndlck_stateid.other[1] = stp->ls_stateid.other[1];
1115 		ldumpp[cnt].ndlck_stateid.other[2] = stp->ls_stateid.other[2];
1116 		ldumpp[cnt].ndlck_owner.nclid_idlen = 0;
1117 		ldumpp[cnt].ndlck_clid.nclid_idlen = stp->ls_clp->lc_idlen;
1118 		NFSBCOPY(stp->ls_clp->lc_id, ldumpp[cnt].ndlck_clid.nclid_id,
1119 		    stp->ls_clp->lc_idlen);
1120 		sad=NFSSOCKADDR(stp->ls_clp->lc_req.nr_nam, struct sockaddr *);
1121 		ldumpp[cnt].ndlck_addrfam = sad->sa_family;
1122 		if (sad->sa_family == AF_INET) {
1123 			rad = (struct sockaddr_in *)sad;
1124 			ldumpp[cnt].ndlck_cbaddr.sin_addr = rad->sin_addr;
1125 		} else {
1126 			rad6 = (struct sockaddr_in6 *)sad;
1127 			ldumpp[cnt].ndlck_cbaddr.sin6_addr = rad6->sin6_addr;
1128 		}
1129 		stp = LIST_NEXT(stp, ls_file);
1130 		cnt++;
1131 	}
1132 
1133 	/*
1134 	 * If list isn't full, mark end of list by setting the client name
1135 	 * to zero length.
1136 	 */
1137 	if (cnt < maxcnt)
1138 		ldumpp[cnt].ndlck_clid.nclid_idlen = 0;
1139 	NFSUNLOCKSTATE();
1140 	NFSLOCKV4ROOTMUTEX();
1141 	nfsv4_relref(&nfsv4rootfs_lock);
1142 	NFSUNLOCKV4ROOTMUTEX();
1143 }
1144 
1145 /*
1146  * Server timer routine. It can scan any linked list, so long
1147  * as it holds the spin/mutex lock and there is no exclusive lock on
1148  * nfsv4rootfs_lock.
1149  * (For OpenBSD, a kthread is ok. For FreeBSD, I think it is ok
1150  *  to do this from a callout, since the spin locks work. For
1151  *  Darwin, I'm not sure what will work correctly yet.)
1152  * Should be called once per second.
1153  */
1154 APPLESTATIC void
1155 nfsrv_servertimer(void)
1156 {
1157 	struct nfsclient *clp, *nclp;
1158 	struct nfsstate *stp, *nstp;
1159 	int got_ref, i;
1160 
1161 	/*
1162 	 * Make sure nfsboottime is set. This is used by V3 as well
1163 	 * as V4. Note that nfsboottime is not nfsrvboottime, which is
1164 	 * only used by the V4 server for leases.
1165 	 */
1166 	if (nfsboottime.tv_sec == 0)
1167 		NFSSETBOOTTIME(nfsboottime);
1168 
1169 	/*
1170 	 * If server hasn't started yet, just return.
1171 	 */
1172 	NFSLOCKSTATE();
1173 	if (nfsrv_stablefirst.nsf_eograce == 0) {
1174 		NFSUNLOCKSTATE();
1175 		return;
1176 	}
1177 	if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE)) {
1178 		if (!(nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) &&
1179 		    NFSD_MONOSEC > nfsrv_stablefirst.nsf_eograce)
1180 			nfsrv_stablefirst.nsf_flags |=
1181 			    (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
1182 		NFSUNLOCKSTATE();
1183 		return;
1184 	}
1185 
1186 	/*
1187 	 * Try and get a reference count on the nfsv4rootfs_lock so that
1188 	 * no nfsd thread can acquire an exclusive lock on it before this
1189 	 * call is done. If it is already exclusively locked, just return.
1190 	 */
1191 	NFSLOCKV4ROOTMUTEX();
1192 	got_ref = nfsv4_getref_nonblock(&nfsv4rootfs_lock);
1193 	NFSUNLOCKV4ROOTMUTEX();
1194 	if (got_ref == 0) {
1195 		NFSUNLOCKSTATE();
1196 		return;
1197 	}
1198 
1199 	/*
1200 	 * For each client...
1201 	 */
1202 	for (i = 0; i < nfsrv_clienthashsize; i++) {
1203 	    clp = LIST_FIRST(&nfsclienthash[i]);
1204 	    while (clp != LIST_END(&nfsclienthash[i])) {
1205 		nclp = LIST_NEXT(clp, lc_hash);
1206 		if (!(clp->lc_flags & LCL_EXPIREIT)) {
1207 		    if (((clp->lc_expiry + NFSRV_STALELEASE) < NFSD_MONOSEC
1208 			 && ((LIST_EMPTY(&clp->lc_deleg)
1209 			      && LIST_EMPTY(&clp->lc_open)) ||
1210 			     nfsrv_clients > nfsrv_clienthighwater)) ||
1211 			(clp->lc_expiry + NFSRV_MOULDYLEASE) < NFSD_MONOSEC ||
1212 			(clp->lc_expiry < NFSD_MONOSEC &&
1213 			 (nfsrv_openpluslock * 10 / 9) > nfsrv_v4statelimit)) {
1214 			/*
1215 			 * Lease has expired several nfsrv_lease times ago:
1216 			 * PLUS
1217 			 *    - no state is associated with it
1218 			 *    OR
1219 			 *    - above high water mark for number of clients
1220 			 *      (nfsrv_clienthighwater should be large enough
1221 			 *       that this only occurs when clients fail to
1222 			 *       use the same nfs_client_id4.id. Maybe somewhat
1223 			 *       higher that the maximum number of clients that
1224 			 *       will mount this server?)
1225 			 * OR
1226 			 * Lease has expired a very long time ago
1227 			 * OR
1228 			 * Lease has expired PLUS the number of opens + locks
1229 			 * has exceeded 90% of capacity
1230 			 *
1231 			 * --> Mark for expiry. The actual expiry will be done
1232 			 *     by an nfsd sometime soon.
1233 			 */
1234 			clp->lc_flags |= LCL_EXPIREIT;
1235 			nfsrv_stablefirst.nsf_flags |=
1236 			    (NFSNSF_NEEDLOCK | NFSNSF_EXPIREDCLIENT);
1237 		    } else {
1238 			/*
1239 			 * If there are no opens, increment no open tick cnt
1240 			 * If time exceeds NFSNOOPEN, mark it to be thrown away
1241 			 * otherwise, if there is an open, reset no open time
1242 			 * Hopefully, this will avoid excessive re-creation
1243 			 * of open owners and subsequent open confirms.
1244 			 */
1245 			stp = LIST_FIRST(&clp->lc_open);
1246 			while (stp != LIST_END(&clp->lc_open)) {
1247 				nstp = LIST_NEXT(stp, ls_list);
1248 				if (LIST_EMPTY(&stp->ls_open)) {
1249 					stp->ls_noopens++;
1250 					if (stp->ls_noopens > NFSNOOPEN ||
1251 					    (nfsrv_openpluslock * 2) >
1252 					    nfsrv_v4statelimit)
1253 						nfsrv_stablefirst.nsf_flags |=
1254 							NFSNSF_NOOPENS;
1255 				} else {
1256 					stp->ls_noopens = 0;
1257 				}
1258 				stp = nstp;
1259 			}
1260 		    }
1261 		}
1262 		clp = nclp;
1263 	    }
1264 	}
1265 	NFSUNLOCKSTATE();
1266 	NFSLOCKV4ROOTMUTEX();
1267 	nfsv4_relref(&nfsv4rootfs_lock);
1268 	NFSUNLOCKV4ROOTMUTEX();
1269 }
1270 
1271 /*
1272  * The following set of functions free up the various data structures.
1273  */
1274 /*
1275  * Clear out all open/lock state related to this nfsclient.
1276  * Caller must hold an exclusive lock on nfsv4rootfs_lock, so that
1277  * there are no other active nfsd threads.
1278  */
1279 APPLESTATIC void
1280 nfsrv_cleanclient(struct nfsclient *clp, NFSPROC_T *p)
1281 {
1282 	struct nfsstate *stp, *nstp;
1283 	struct nfsdsession *sep, *nsep;
1284 
1285 	LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp)
1286 		nfsrv_freeopenowner(stp, 1, p);
1287 	if ((clp->lc_flags & LCL_ADMINREVOKED) == 0)
1288 		LIST_FOREACH_SAFE(sep, &clp->lc_session, sess_list, nsep)
1289 			(void)nfsrv_freesession(sep, NULL);
1290 }
1291 
1292 /*
1293  * Free a client that has been cleaned. It should also already have been
1294  * removed from the lists.
1295  * (Just to be safe w.r.t. newnfs_disconnect(), call this function when
1296  *  softclock interrupts are enabled.)
1297  */
1298 APPLESTATIC void
1299 nfsrv_zapclient(struct nfsclient *clp, NFSPROC_T *p)
1300 {
1301 
1302 #ifdef notyet
1303 	if ((clp->lc_flags & (LCL_GSS | LCL_CALLBACKSON)) ==
1304 	     (LCL_GSS | LCL_CALLBACKSON) &&
1305 	    (clp->lc_hand.nfsh_flag & NFSG_COMPLETE) &&
1306 	    clp->lc_handlelen > 0) {
1307 		clp->lc_hand.nfsh_flag &= ~NFSG_COMPLETE;
1308 		clp->lc_hand.nfsh_flag |= NFSG_DESTROYED;
1309 		(void) nfsrv_docallback(clp, NFSV4PROC_CBNULL,
1310 			NULL, 0, NULL, NULL, NULL, 0, p);
1311 	}
1312 #endif
1313 	newnfs_disconnect(&clp->lc_req);
1314 	free(clp->lc_req.nr_nam, M_SONAME);
1315 	NFSFREEMUTEX(&clp->lc_req.nr_mtx);
1316 	free(clp->lc_stateid, M_NFSDCLIENT);
1317 	free(clp, M_NFSDCLIENT);
1318 	NFSLOCKSTATE();
1319 	nfsstatsv1.srvclients--;
1320 	nfsrv_openpluslock--;
1321 	nfsrv_clients--;
1322 	NFSUNLOCKSTATE();
1323 }
1324 
1325 /*
1326  * Free a list of delegation state structures.
1327  * (This function will also free all nfslockfile structures that no
1328  *  longer have associated state.)
1329  */
1330 APPLESTATIC void
1331 nfsrv_freedeleglist(struct nfsstatehead *sthp)
1332 {
1333 	struct nfsstate *stp, *nstp;
1334 
1335 	LIST_FOREACH_SAFE(stp, sthp, ls_list, nstp) {
1336 		nfsrv_freedeleg(stp);
1337 	}
1338 	LIST_INIT(sthp);
1339 }
1340 
1341 /*
1342  * Free up a delegation.
1343  */
1344 static void
1345 nfsrv_freedeleg(struct nfsstate *stp)
1346 {
1347 	struct nfslockfile *lfp;
1348 
1349 	LIST_REMOVE(stp, ls_hash);
1350 	LIST_REMOVE(stp, ls_list);
1351 	LIST_REMOVE(stp, ls_file);
1352 	if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
1353 		nfsrv_writedelegcnt--;
1354 	lfp = stp->ls_lfp;
1355 	if (LIST_EMPTY(&lfp->lf_open) &&
1356 	    LIST_EMPTY(&lfp->lf_lock) && LIST_EMPTY(&lfp->lf_deleg) &&
1357 	    LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1358 	    lfp->lf_usecount == 0 &&
1359 	    nfsv4_testlock(&lfp->lf_locallock_lck) == 0)
1360 		nfsrv_freenfslockfile(lfp);
1361 	free(stp, M_NFSDSTATE);
1362 	nfsstatsv1.srvdelegates--;
1363 	nfsrv_openpluslock--;
1364 	nfsrv_delegatecnt--;
1365 }
1366 
1367 /*
1368  * This function frees an open owner and all associated opens.
1369  */
1370 static void
1371 nfsrv_freeopenowner(struct nfsstate *stp, int cansleep, NFSPROC_T *p)
1372 {
1373 	struct nfsstate *nstp, *tstp;
1374 
1375 	LIST_REMOVE(stp, ls_list);
1376 	/*
1377 	 * Now, free all associated opens.
1378 	 */
1379 	nstp = LIST_FIRST(&stp->ls_open);
1380 	while (nstp != LIST_END(&stp->ls_open)) {
1381 		tstp = nstp;
1382 		nstp = LIST_NEXT(nstp, ls_list);
1383 		(void) nfsrv_freeopen(tstp, NULL, cansleep, p);
1384 	}
1385 	if (stp->ls_op)
1386 		nfsrvd_derefcache(stp->ls_op);
1387 	free(stp, M_NFSDSTATE);
1388 	nfsstatsv1.srvopenowners--;
1389 	nfsrv_openpluslock--;
1390 }
1391 
1392 /*
1393  * This function frees an open (nfsstate open structure) with all associated
1394  * lock_owners and locks. It also frees the nfslockfile structure iff there
1395  * are no other opens on the file.
1396  * Returns 1 if it free'd the nfslockfile, 0 otherwise.
1397  */
1398 static int
1399 nfsrv_freeopen(struct nfsstate *stp, vnode_t vp, int cansleep, NFSPROC_T *p)
1400 {
1401 	struct nfsstate *nstp, *tstp;
1402 	struct nfslockfile *lfp;
1403 	int ret;
1404 
1405 	LIST_REMOVE(stp, ls_hash);
1406 	LIST_REMOVE(stp, ls_list);
1407 	LIST_REMOVE(stp, ls_file);
1408 
1409 	lfp = stp->ls_lfp;
1410 	/*
1411 	 * Now, free all lockowners associated with this open.
1412 	 */
1413 	LIST_FOREACH_SAFE(tstp, &stp->ls_open, ls_list, nstp)
1414 		nfsrv_freelockowner(tstp, vp, cansleep, p);
1415 
1416 	/*
1417 	 * The nfslockfile is freed here if there are no locks
1418 	 * associated with the open.
1419 	 * If there are locks associated with the open, the
1420 	 * nfslockfile structure can be freed via nfsrv_freelockowner().
1421 	 * Acquire the state mutex to avoid races with calls to
1422 	 * nfsrv_getlockfile().
1423 	 */
1424 	if (cansleep != 0)
1425 		NFSLOCKSTATE();
1426 	if (lfp != NULL && LIST_EMPTY(&lfp->lf_open) &&
1427 	    LIST_EMPTY(&lfp->lf_deleg) && LIST_EMPTY(&lfp->lf_lock) &&
1428 	    LIST_EMPTY(&lfp->lf_locallock) && LIST_EMPTY(&lfp->lf_rollback) &&
1429 	    lfp->lf_usecount == 0 &&
1430 	    (cansleep != 0 || nfsv4_testlock(&lfp->lf_locallock_lck) == 0)) {
1431 		nfsrv_freenfslockfile(lfp);
1432 		ret = 1;
1433 	} else
1434 		ret = 0;
1435 	if (cansleep != 0)
1436 		NFSUNLOCKSTATE();
1437 	free(stp, M_NFSDSTATE);
1438 	nfsstatsv1.srvopens--;
1439 	nfsrv_openpluslock--;
1440 	return (ret);
1441 }
1442 
1443 /*
1444  * Frees a lockowner and all associated locks.
1445  */
1446 static void
1447 nfsrv_freelockowner(struct nfsstate *stp, vnode_t vp, int cansleep,
1448     NFSPROC_T *p)
1449 {
1450 
1451 	LIST_REMOVE(stp, ls_hash);
1452 	LIST_REMOVE(stp, ls_list);
1453 	nfsrv_freeallnfslocks(stp, vp, cansleep, p);
1454 	if (stp->ls_op)
1455 		nfsrvd_derefcache(stp->ls_op);
1456 	free(stp, M_NFSDSTATE);
1457 	nfsstatsv1.srvlockowners--;
1458 	nfsrv_openpluslock--;
1459 }
1460 
1461 /*
1462  * Free all the nfs locks on a lockowner.
1463  */
1464 static void
1465 nfsrv_freeallnfslocks(struct nfsstate *stp, vnode_t vp, int cansleep,
1466     NFSPROC_T *p)
1467 {
1468 	struct nfslock *lop, *nlop;
1469 	struct nfsrollback *rlp, *nrlp;
1470 	struct nfslockfile *lfp = NULL;
1471 	int gottvp = 0;
1472 	vnode_t tvp = NULL;
1473 	uint64_t first, end;
1474 
1475 	if (vp != NULL)
1476 		ASSERT_VOP_UNLOCKED(vp, "nfsrv_freeallnfslocks: vnode locked");
1477 	lop = LIST_FIRST(&stp->ls_lock);
1478 	while (lop != LIST_END(&stp->ls_lock)) {
1479 		nlop = LIST_NEXT(lop, lo_lckowner);
1480 		/*
1481 		 * Since all locks should be for the same file, lfp should
1482 		 * not change.
1483 		 */
1484 		if (lfp == NULL)
1485 			lfp = lop->lo_lfp;
1486 		else if (lfp != lop->lo_lfp)
1487 			panic("allnfslocks");
1488 		/*
1489 		 * If vp is NULL and cansleep != 0, a vnode must be acquired
1490 		 * from the file handle. This only occurs when called from
1491 		 * nfsrv_cleanclient().
1492 		 */
1493 		if (gottvp == 0) {
1494 			if (nfsrv_dolocallocks == 0)
1495 				tvp = NULL;
1496 			else if (vp == NULL && cansleep != 0) {
1497 				tvp = nfsvno_getvp(&lfp->lf_fh);
1498 				NFSVOPUNLOCK(tvp, 0);
1499 			} else
1500 				tvp = vp;
1501 			gottvp = 1;
1502 		}
1503 
1504 		if (tvp != NULL) {
1505 			if (cansleep == 0)
1506 				panic("allnfs2");
1507 			first = lop->lo_first;
1508 			end = lop->lo_end;
1509 			nfsrv_freenfslock(lop);
1510 			nfsrv_localunlock(tvp, lfp, first, end, p);
1511 			LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list,
1512 			    nrlp)
1513 				free(rlp, M_NFSDROLLBACK);
1514 			LIST_INIT(&lfp->lf_rollback);
1515 		} else
1516 			nfsrv_freenfslock(lop);
1517 		lop = nlop;
1518 	}
1519 	if (vp == NULL && tvp != NULL)
1520 		vrele(tvp);
1521 }
1522 
1523 /*
1524  * Free an nfslock structure.
1525  */
1526 static void
1527 nfsrv_freenfslock(struct nfslock *lop)
1528 {
1529 
1530 	if (lop->lo_lckfile.le_prev != NULL) {
1531 		LIST_REMOVE(lop, lo_lckfile);
1532 		nfsstatsv1.srvlocks--;
1533 		nfsrv_openpluslock--;
1534 	}
1535 	LIST_REMOVE(lop, lo_lckowner);
1536 	free(lop, M_NFSDLOCK);
1537 }
1538 
1539 /*
1540  * This function frees an nfslockfile structure.
1541  */
1542 static void
1543 nfsrv_freenfslockfile(struct nfslockfile *lfp)
1544 {
1545 
1546 	LIST_REMOVE(lfp, lf_hash);
1547 	free(lfp, M_NFSDLOCKFILE);
1548 }
1549 
1550 /*
1551  * This function looks up an nfsstate structure via stateid.
1552  */
1553 static int
1554 nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
1555     struct nfsstate **stpp)
1556 {
1557 	struct nfsstate *stp;
1558 	struct nfsstatehead *hp;
1559 	int error = 0;
1560 
1561 	*stpp = NULL;
1562 	hp = NFSSTATEHASH(clp, *stateidp);
1563 	LIST_FOREACH(stp, hp, ls_hash) {
1564 		if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
1565 			NFSX_STATEIDOTHER))
1566 			break;
1567 	}
1568 
1569 	/*
1570 	 * If no state id in list, return NFSERR_BADSTATEID.
1571 	 */
1572 	if (stp == LIST_END(hp)) {
1573 		error = NFSERR_BADSTATEID;
1574 		goto out;
1575 	}
1576 	*stpp = stp;
1577 
1578 out:
1579 	NFSEXITCODE(error);
1580 	return (error);
1581 }
1582 
1583 /*
1584  * This function gets an nfsstate structure via owner string.
1585  */
1586 static void
1587 nfsrv_getowner(struct nfsstatehead *hp, struct nfsstate *new_stp,
1588     struct nfsstate **stpp)
1589 {
1590 	struct nfsstate *stp;
1591 
1592 	*stpp = NULL;
1593 	LIST_FOREACH(stp, hp, ls_list) {
1594 		if (new_stp->ls_ownerlen == stp->ls_ownerlen &&
1595 		  !NFSBCMP(new_stp->ls_owner,stp->ls_owner,stp->ls_ownerlen)) {
1596 			*stpp = stp;
1597 			return;
1598 		}
1599 	}
1600 }
1601 
1602 /*
1603  * Lock control function called to update lock status.
1604  * Returns 0 upon success, -1 if there is no lock and the flags indicate
1605  * that one isn't to be created and an NFSERR_xxx for other errors.
1606  * The structures new_stp and new_lop are passed in as pointers that should
1607  * be set to NULL if the structure is used and shouldn't be free'd.
1608  * For the NFSLCK_TEST and NFSLCK_CHECK cases, the structures are
1609  * never used and can safely be allocated on the stack. For all other
1610  * cases, *new_stpp and *new_lopp should be malloc'd before the call,
1611  * in case they are used.
1612  */
1613 APPLESTATIC int
1614 nfsrv_lockctrl(vnode_t vp, struct nfsstate **new_stpp,
1615     struct nfslock **new_lopp, struct nfslockconflict *cfp,
1616     nfsquad_t clientid, nfsv4stateid_t *stateidp,
1617     __unused struct nfsexstuff *exp,
1618     struct nfsrv_descript *nd, NFSPROC_T *p)
1619 {
1620 	struct nfslock *lop;
1621 	struct nfsstate *new_stp = *new_stpp;
1622 	struct nfslock *new_lop = *new_lopp;
1623 	struct nfsstate *tstp, *mystp, *nstp;
1624 	int specialid = 0;
1625 	struct nfslockfile *lfp;
1626 	struct nfslock *other_lop = NULL;
1627 	struct nfsstate *stp, *lckstp = NULL;
1628 	struct nfsclient *clp = NULL;
1629 	u_int32_t bits;
1630 	int error = 0, haslock = 0, ret, reterr;
1631 	int getlckret, delegation = 0, filestruct_locked, vnode_unlocked = 0;
1632 	fhandle_t nfh;
1633 	uint64_t first, end;
1634 	uint32_t lock_flags;
1635 
1636 	if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1637 		/*
1638 		 * Note the special cases of "all 1s" or "all 0s" stateids and
1639 		 * let reads with all 1s go ahead.
1640 		 */
1641 		if (new_stp->ls_stateid.seqid == 0x0 &&
1642 		    new_stp->ls_stateid.other[0] == 0x0 &&
1643 		    new_stp->ls_stateid.other[1] == 0x0 &&
1644 		    new_stp->ls_stateid.other[2] == 0x0)
1645 			specialid = 1;
1646 		else if (new_stp->ls_stateid.seqid == 0xffffffff &&
1647 		    new_stp->ls_stateid.other[0] == 0xffffffff &&
1648 		    new_stp->ls_stateid.other[1] == 0xffffffff &&
1649 		    new_stp->ls_stateid.other[2] == 0xffffffff)
1650 			specialid = 2;
1651 	}
1652 
1653 	/*
1654 	 * Check for restart conditions (client and server).
1655 	 */
1656 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
1657 	    &new_stp->ls_stateid, specialid);
1658 	if (error)
1659 		goto out;
1660 
1661 	/*
1662 	 * Check for state resource limit exceeded.
1663 	 */
1664 	if ((new_stp->ls_flags & NFSLCK_LOCK) &&
1665 	    nfsrv_openpluslock > nfsrv_v4statelimit) {
1666 		error = NFSERR_RESOURCE;
1667 		goto out;
1668 	}
1669 
1670 	/*
1671 	 * For the lock case, get another nfslock structure,
1672 	 * just in case we need it.
1673 	 * Malloc now, before we start sifting through the linked lists,
1674 	 * in case we have to wait for memory.
1675 	 */
1676 tryagain:
1677 	if (new_stp->ls_flags & NFSLCK_LOCK)
1678 		other_lop = malloc(sizeof (struct nfslock),
1679 		    M_NFSDLOCK, M_WAITOK);
1680 	filestruct_locked = 0;
1681 	reterr = 0;
1682 	lfp = NULL;
1683 
1684 	/*
1685 	 * Get the lockfile structure for CFH now, so we can do a sanity
1686 	 * check against the stateid, before incrementing the seqid#, since
1687 	 * we want to return NFSERR_BADSTATEID on failure and the seqid#
1688 	 * shouldn't be incremented for this case.
1689 	 * If nfsrv_getlockfile() returns -1, it means "not found", which
1690 	 * will be handled later.
1691 	 * If we are doing Lock/LockU and local locking is enabled, sleep
1692 	 * lock the nfslockfile structure.
1693 	 */
1694 	getlckret = nfsrv_getlockfh(vp, new_stp->ls_flags, NULL, &nfh, p);
1695 	NFSLOCKSTATE();
1696 	if (getlckret == 0) {
1697 		if ((new_stp->ls_flags & (NFSLCK_LOCK | NFSLCK_UNLOCK)) != 0 &&
1698 		    nfsrv_dolocallocks != 0 && nd->nd_repstat == 0) {
1699 			getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1700 			    &lfp, &nfh, 1);
1701 			if (getlckret == 0)
1702 				filestruct_locked = 1;
1703 		} else
1704 			getlckret = nfsrv_getlockfile(new_stp->ls_flags, NULL,
1705 			    &lfp, &nfh, 0);
1706 	}
1707 	if (getlckret != 0 && getlckret != -1)
1708 		reterr = getlckret;
1709 
1710 	if (filestruct_locked != 0) {
1711 		LIST_INIT(&lfp->lf_rollback);
1712 		if ((new_stp->ls_flags & NFSLCK_LOCK)) {
1713 			/*
1714 			 * For local locking, do the advisory locking now, so
1715 			 * that any conflict can be detected. A failure later
1716 			 * can be rolled back locally. If an error is returned,
1717 			 * struct nfslockfile has been unlocked and any local
1718 			 * locking rolled back.
1719 			 */
1720 			NFSUNLOCKSTATE();
1721 			if (vnode_unlocked == 0) {
1722 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl1");
1723 				vnode_unlocked = 1;
1724 				NFSVOPUNLOCK(vp, 0);
1725 			}
1726 			reterr = nfsrv_locallock(vp, lfp,
1727 			    (new_lop->lo_flags & (NFSLCK_READ | NFSLCK_WRITE)),
1728 			    new_lop->lo_first, new_lop->lo_end, cfp, p);
1729 			NFSLOCKSTATE();
1730 		}
1731 	}
1732 
1733 	if (specialid == 0) {
1734 	    if (new_stp->ls_flags & NFSLCK_TEST) {
1735 		/*
1736 		 * RFC 3530 does not list LockT as an op that renews a
1737 		 * lease, but the consensus seems to be that it is ok
1738 		 * for a server to do so.
1739 		 */
1740 		error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1741 		    (nfsquad_t)((u_quad_t)0), 0, nd, p);
1742 
1743 		/*
1744 		 * Since NFSERR_EXPIRED, NFSERR_ADMINREVOKED are not valid
1745 		 * error returns for LockT, just go ahead and test for a lock,
1746 		 * since there are no locks for this client, but other locks
1747 		 * can conflict. (ie. same client will always be false)
1748 		 */
1749 		if (error == NFSERR_EXPIRED || error == NFSERR_ADMINREVOKED)
1750 		    error = 0;
1751 		lckstp = new_stp;
1752 	    } else {
1753 	      error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
1754 		(nfsquad_t)((u_quad_t)0), 0, nd, p);
1755 	      if (error == 0)
1756 		/*
1757 		 * Look up the stateid
1758 		 */
1759 		error = nfsrv_getstate(clp, &new_stp->ls_stateid,
1760 		  new_stp->ls_flags, &stp);
1761 	      /*
1762 	       * do some sanity checks for an unconfirmed open or a
1763 	       * stateid that refers to the wrong file, for an open stateid
1764 	       */
1765 	      if (error == 0 && (stp->ls_flags & NFSLCK_OPEN) &&
1766 		  ((stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM) ||
1767 		   (getlckret == 0 && stp->ls_lfp != lfp))){
1768 		      /*
1769 		       * NFSLCK_SETATTR should return OK rather than NFSERR_BADSTATEID
1770 		       * The only exception is using SETATTR with SIZE.
1771 		       * */
1772                     if ((new_stp->ls_flags &
1773                          (NFSLCK_SETATTR | NFSLCK_CHECK)) != NFSLCK_SETATTR)
1774 			     error = NFSERR_BADSTATEID;
1775 	      }
1776 
1777 		if (error == 0 &&
1778 		  (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) &&
1779 		  getlckret == 0 && stp->ls_lfp != lfp)
1780 			error = NFSERR_BADSTATEID;
1781 
1782 	      /*
1783 	       * If the lockowner stateid doesn't refer to the same file,
1784 	       * I believe that is considered ok, since some clients will
1785 	       * only create a single lockowner and use that for all locks
1786 	       * on all files.
1787 	       * For now, log it as a diagnostic, instead of considering it
1788 	       * a BadStateid.
1789 	       */
1790 	      if (error == 0 && (stp->ls_flags &
1791 		  (NFSLCK_OPEN | NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0 &&
1792 		  getlckret == 0 && stp->ls_lfp != lfp) {
1793 #ifdef DIAGNOSTIC
1794 		  printf("Got a lock statid for different file open\n");
1795 #endif
1796 		  /*
1797 		  error = NFSERR_BADSTATEID;
1798 		  */
1799 	      }
1800 
1801 	      if (error == 0) {
1802 		    if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
1803 			/*
1804 			 * If haslock set, we've already checked the seqid.
1805 			 */
1806 			if (!haslock) {
1807 			    if (stp->ls_flags & NFSLCK_OPEN)
1808 				error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1809 				    stp->ls_openowner, new_stp->ls_op);
1810 			    else
1811 				error = NFSERR_BADSTATEID;
1812 			}
1813 			if (!error)
1814 			    nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
1815 			if (lckstp)
1816 			    /*
1817 			     * I believe this should be an error, but it
1818 			     * isn't obvious what NFSERR_xxx would be
1819 			     * appropriate, so I'll use NFSERR_INVAL for now.
1820 			     */
1821 			    error = NFSERR_INVAL;
1822 			else
1823 			    lckstp = new_stp;
1824 		    } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
1825 			/*
1826 			 * If haslock set, ditto above.
1827 			 */
1828 			if (!haslock) {
1829 			    if (stp->ls_flags & NFSLCK_OPEN)
1830 				error = NFSERR_BADSTATEID;
1831 			    else
1832 				error = nfsrv_checkseqid(nd, new_stp->ls_seq,
1833 				    stp, new_stp->ls_op);
1834 			}
1835 			lckstp = stp;
1836 		    } else {
1837 			lckstp = stp;
1838 		    }
1839 	      }
1840 	      /*
1841 	       * If the seqid part of the stateid isn't the same, return
1842 	       * NFSERR_OLDSTATEID for cases other than I/O Ops.
1843 	       * For I/O Ops, only return NFSERR_OLDSTATEID if
1844 	       * nfsrv_returnoldstateid is set. (The consensus on the email
1845 	       * list was that most clients would prefer to not receive
1846 	       * NFSERR_OLDSTATEID for I/O Ops, but the RFC suggests that that
1847 	       * is what will happen, so I use the nfsrv_returnoldstateid to
1848 	       * allow for either server configuration.)
1849 	       */
1850 	      if (!error && stp->ls_stateid.seqid!=new_stp->ls_stateid.seqid &&
1851 		  (((nd->nd_flag & ND_NFSV41) == 0 &&
1852 		   (!(new_stp->ls_flags & NFSLCK_CHECK) ||
1853 		    nfsrv_returnoldstateid)) ||
1854 		   ((nd->nd_flag & ND_NFSV41) != 0 &&
1855 		    new_stp->ls_stateid.seqid != 0)))
1856 		    error = NFSERR_OLDSTATEID;
1857 	    }
1858 	}
1859 
1860 	/*
1861 	 * Now we can check for grace.
1862 	 */
1863 	if (!error)
1864 		error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
1865 	if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
1866 		nfsrv_checkstable(clp))
1867 		error = NFSERR_NOGRACE;
1868 	/*
1869 	 * If we successfully Reclaimed state, note that.
1870 	 */
1871 	if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error)
1872 		nfsrv_markstable(clp);
1873 
1874 	/*
1875 	 * At this point, either error == NFSERR_BADSTATEID or the
1876 	 * seqid# has been updated, so we can return any error.
1877 	 * If error == 0, there may be an error in:
1878 	 *    nd_repstat - Set by the calling function.
1879 	 *    reterr - Set above, if getting the nfslockfile structure
1880 	 *       or acquiring the local lock failed.
1881 	 *    (If both of these are set, nd_repstat should probably be
1882 	 *     returned, since that error was detected before this
1883 	 *     function call.)
1884 	 */
1885 	if (error != 0 || nd->nd_repstat != 0 || reterr != 0) {
1886 		if (error == 0) {
1887 			if (nd->nd_repstat != 0)
1888 				error = nd->nd_repstat;
1889 			else
1890 				error = reterr;
1891 		}
1892 		if (filestruct_locked != 0) {
1893 			/* Roll back local locks. */
1894 			NFSUNLOCKSTATE();
1895 			if (vnode_unlocked == 0) {
1896 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl2");
1897 				vnode_unlocked = 1;
1898 				NFSVOPUNLOCK(vp, 0);
1899 			}
1900 			nfsrv_locallock_rollback(vp, lfp, p);
1901 			NFSLOCKSTATE();
1902 			nfsrv_unlocklf(lfp);
1903 		}
1904 		NFSUNLOCKSTATE();
1905 		goto out;
1906 	}
1907 
1908 	/*
1909 	 * Check the nfsrv_getlockfile return.
1910 	 * Returned -1 if no structure found.
1911 	 */
1912 	if (getlckret == -1) {
1913 		error = NFSERR_EXPIRED;
1914 		/*
1915 		 * Called from lockt, so no lock is OK.
1916 		 */
1917 		if (new_stp->ls_flags & NFSLCK_TEST) {
1918 			error = 0;
1919 		} else if (new_stp->ls_flags &
1920 		    (NFSLCK_CHECK | NFSLCK_SETATTR)) {
1921 			/*
1922 			 * Called to check for a lock, OK if the stateid is all
1923 			 * 1s or all 0s, but there should be an nfsstate
1924 			 * otherwise.
1925 			 * (ie. If there is no open, I'll assume no share
1926 			 *  deny bits.)
1927 			 */
1928 			if (specialid)
1929 				error = 0;
1930 			else
1931 				error = NFSERR_BADSTATEID;
1932 		}
1933 		NFSUNLOCKSTATE();
1934 		goto out;
1935 	}
1936 
1937 	/*
1938 	 * For NFSLCK_CHECK and NFSLCK_LOCK, test for a share conflict.
1939 	 * For NFSLCK_CHECK, allow a read if write access is granted,
1940 	 * but check for a deny. For NFSLCK_LOCK, require correct access,
1941 	 * which implies a conflicting deny can't exist.
1942 	 */
1943 	if (new_stp->ls_flags & (NFSLCK_CHECK | NFSLCK_LOCK)) {
1944 	    /*
1945 	     * Four kinds of state id:
1946 	     * - specialid (all 0s or all 1s), only for NFSLCK_CHECK
1947 	     * - stateid for an open
1948 	     * - stateid for a delegation
1949 	     * - stateid for a lock owner
1950 	     */
1951 	    if (!specialid) {
1952 		if (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
1953 		    delegation = 1;
1954 		    mystp = stp;
1955 		    nfsrv_delaydelegtimeout(stp);
1956 	        } else if (stp->ls_flags & NFSLCK_OPEN) {
1957 		    mystp = stp;
1958 		} else {
1959 		    mystp = stp->ls_openstp;
1960 		}
1961 		/*
1962 		 * If locking or checking, require correct access
1963 		 * bit set.
1964 		 */
1965 		if (((new_stp->ls_flags & NFSLCK_LOCK) &&
1966 		     !((new_lop->lo_flags >> NFSLCK_LOCKSHIFT) &
1967 		       mystp->ls_flags & NFSLCK_ACCESSBITS)) ||
1968 		    ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_READACCESS)) ==
1969 		      (NFSLCK_CHECK | NFSLCK_READACCESS) &&
1970 		     !(mystp->ls_flags & NFSLCK_READACCESS) &&
1971 		     nfsrv_allowreadforwriteopen == 0) ||
1972 		    ((new_stp->ls_flags & (NFSLCK_CHECK|NFSLCK_WRITEACCESS)) ==
1973 		      (NFSLCK_CHECK | NFSLCK_WRITEACCESS) &&
1974 		     !(mystp->ls_flags & NFSLCK_WRITEACCESS))) {
1975 			if (filestruct_locked != 0) {
1976 				/* Roll back local locks. */
1977 				NFSUNLOCKSTATE();
1978 				if (vnode_unlocked == 0) {
1979 					ASSERT_VOP_ELOCKED(vp,
1980 					    "nfsrv_lockctrl3");
1981 					vnode_unlocked = 1;
1982 					NFSVOPUNLOCK(vp, 0);
1983 				}
1984 				nfsrv_locallock_rollback(vp, lfp, p);
1985 				NFSLOCKSTATE();
1986 				nfsrv_unlocklf(lfp);
1987 			}
1988 			NFSUNLOCKSTATE();
1989 			error = NFSERR_OPENMODE;
1990 			goto out;
1991 		}
1992 	    } else
1993 		mystp = NULL;
1994 	    if ((new_stp->ls_flags & NFSLCK_CHECK) && !delegation) {
1995 		/*
1996 		 * Check for a conflicting deny bit.
1997 		 */
1998 		LIST_FOREACH(tstp, &lfp->lf_open, ls_file) {
1999 		    if (tstp != mystp) {
2000 			bits = tstp->ls_flags;
2001 			bits >>= NFSLCK_SHIFT;
2002 			if (new_stp->ls_flags & bits & NFSLCK_ACCESSBITS) {
2003 			    KASSERT(vnode_unlocked == 0,
2004 				("nfsrv_lockctrl: vnode unlocked1"));
2005 			    ret = nfsrv_clientconflict(tstp->ls_clp, &haslock,
2006 				vp, p);
2007 			    if (ret == 1) {
2008 				/*
2009 				* nfsrv_clientconflict unlocks state
2010 				 * when it returns non-zero.
2011 				 */
2012 				lckstp = NULL;
2013 				goto tryagain;
2014 			    }
2015 			    if (ret == 0)
2016 				NFSUNLOCKSTATE();
2017 			    if (ret == 2)
2018 				error = NFSERR_PERM;
2019 			    else
2020 				error = NFSERR_OPENMODE;
2021 			    goto out;
2022 			}
2023 		    }
2024 		}
2025 
2026 		/* We're outta here */
2027 		NFSUNLOCKSTATE();
2028 		goto out;
2029 	    }
2030 	}
2031 
2032 	/*
2033 	 * For setattr, just get rid of all the Delegations for other clients.
2034 	 */
2035 	if (new_stp->ls_flags & NFSLCK_SETATTR) {
2036 		KASSERT(vnode_unlocked == 0,
2037 		    ("nfsrv_lockctrl: vnode unlocked2"));
2038 		ret = nfsrv_cleandeleg(vp, lfp, clp, &haslock, p);
2039 		if (ret) {
2040 			/*
2041 			 * nfsrv_cleandeleg() unlocks state when it
2042 			 * returns non-zero.
2043 			 */
2044 			if (ret == -1) {
2045 				lckstp = NULL;
2046 				goto tryagain;
2047 			}
2048 			error = ret;
2049 			goto out;
2050 		}
2051 		if (!(new_stp->ls_flags & NFSLCK_CHECK) ||
2052 		    (LIST_EMPTY(&lfp->lf_open) && LIST_EMPTY(&lfp->lf_lock) &&
2053 		     LIST_EMPTY(&lfp->lf_deleg))) {
2054 			NFSUNLOCKSTATE();
2055 			goto out;
2056 		}
2057 	}
2058 
2059 	/*
2060 	 * Check for a conflicting delegation. If one is found, call
2061 	 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2062 	 * been set yet, it will get the lock. Otherwise, it will recall
2063 	 * the delegation. Then, we try try again...
2064 	 * I currently believe the conflict algorithm to be:
2065 	 * For Lock Ops (Lock/LockT/LockU)
2066 	 * - there is a conflict iff a different client has a write delegation
2067 	 * For Reading (Read Op)
2068 	 * - there is a conflict iff a different client has a write delegation
2069 	 *   (the specialids are always a different client)
2070 	 * For Writing (Write/Setattr of size)
2071 	 * - there is a conflict if a different client has any delegation
2072 	 * - there is a conflict if the same client has a read delegation
2073 	 *   (I don't understand why this isn't allowed, but that seems to be
2074 	 *    the current consensus?)
2075 	 */
2076 	tstp = LIST_FIRST(&lfp->lf_deleg);
2077 	while (tstp != LIST_END(&lfp->lf_deleg)) {
2078 	    nstp = LIST_NEXT(tstp, ls_file);
2079 	    if ((((new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK|NFSLCK_TEST))||
2080 		 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2081 		  (new_lop->lo_flags & NFSLCK_READ))) &&
2082 		  clp != tstp->ls_clp &&
2083 		 (tstp->ls_flags & NFSLCK_DELEGWRITE)) ||
2084 		 ((new_stp->ls_flags & NFSLCK_CHECK) &&
2085 		   (new_lop->lo_flags & NFSLCK_WRITE) &&
2086 		  (clp != tstp->ls_clp ||
2087 		   (tstp->ls_flags & NFSLCK_DELEGREAD)))) {
2088 		ret = 0;
2089 		if (filestruct_locked != 0) {
2090 			/* Roll back local locks. */
2091 			NFSUNLOCKSTATE();
2092 			if (vnode_unlocked == 0) {
2093 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl4");
2094 				NFSVOPUNLOCK(vp, 0);
2095 			}
2096 			nfsrv_locallock_rollback(vp, lfp, p);
2097 			NFSLOCKSTATE();
2098 			nfsrv_unlocklf(lfp);
2099 			NFSUNLOCKSTATE();
2100 			NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2101 			vnode_unlocked = 0;
2102 			if ((vp->v_iflag & VI_DOOMED) != 0)
2103 				ret = NFSERR_SERVERFAULT;
2104 			NFSLOCKSTATE();
2105 		}
2106 		if (ret == 0)
2107 			ret = nfsrv_delegconflict(tstp, &haslock, p, vp);
2108 		if (ret) {
2109 		    /*
2110 		     * nfsrv_delegconflict unlocks state when it
2111 		     * returns non-zero, which it always does.
2112 		     */
2113 		    if (other_lop) {
2114 			free(other_lop, M_NFSDLOCK);
2115 			other_lop = NULL;
2116 		    }
2117 		    if (ret == -1) {
2118 			lckstp = NULL;
2119 			goto tryagain;
2120 		    }
2121 		    error = ret;
2122 		    goto out;
2123 		}
2124 		/* Never gets here. */
2125 	    }
2126 	    tstp = nstp;
2127 	}
2128 
2129 	/*
2130 	 * Handle the unlock case by calling nfsrv_updatelock().
2131 	 * (Should I have done some access checking above for unlock? For now,
2132 	 *  just let it happen.)
2133 	 */
2134 	if (new_stp->ls_flags & NFSLCK_UNLOCK) {
2135 		first = new_lop->lo_first;
2136 		end = new_lop->lo_end;
2137 		nfsrv_updatelock(stp, new_lopp, &other_lop, lfp);
2138 		stateidp->seqid = ++(stp->ls_stateid.seqid);
2139 		if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2140 			stateidp->seqid = stp->ls_stateid.seqid = 1;
2141 		stateidp->other[0] = stp->ls_stateid.other[0];
2142 		stateidp->other[1] = stp->ls_stateid.other[1];
2143 		stateidp->other[2] = stp->ls_stateid.other[2];
2144 		if (filestruct_locked != 0) {
2145 			NFSUNLOCKSTATE();
2146 			if (vnode_unlocked == 0) {
2147 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl5");
2148 				vnode_unlocked = 1;
2149 				NFSVOPUNLOCK(vp, 0);
2150 			}
2151 			/* Update the local locks. */
2152 			nfsrv_localunlock(vp, lfp, first, end, p);
2153 			NFSLOCKSTATE();
2154 			nfsrv_unlocklf(lfp);
2155 		}
2156 		NFSUNLOCKSTATE();
2157 		goto out;
2158 	}
2159 
2160 	/*
2161 	 * Search for a conflicting lock. A lock conflicts if:
2162 	 * - the lock range overlaps and
2163 	 * - at least one lock is a write lock and
2164 	 * - it is not owned by the same lock owner
2165 	 */
2166 	if (!delegation) {
2167 	  LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
2168 	    if (new_lop->lo_end > lop->lo_first &&
2169 		new_lop->lo_first < lop->lo_end &&
2170 		(new_lop->lo_flags == NFSLCK_WRITE ||
2171 		 lop->lo_flags == NFSLCK_WRITE) &&
2172 		lckstp != lop->lo_stp &&
2173 		(clp != lop->lo_stp->ls_clp ||
2174 		 lckstp->ls_ownerlen != lop->lo_stp->ls_ownerlen ||
2175 		 NFSBCMP(lckstp->ls_owner, lop->lo_stp->ls_owner,
2176 		    lckstp->ls_ownerlen))) {
2177 		if (other_lop) {
2178 		    free(other_lop, M_NFSDLOCK);
2179 		    other_lop = NULL;
2180 		}
2181 		if (vnode_unlocked != 0)
2182 		    ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2183 			NULL, p);
2184 		else
2185 		    ret = nfsrv_clientconflict(lop->lo_stp->ls_clp, &haslock,
2186 			vp, p);
2187 		if (ret == 1) {
2188 		    if (filestruct_locked != 0) {
2189 			if (vnode_unlocked == 0) {
2190 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl6");
2191 				NFSVOPUNLOCK(vp, 0);
2192 			}
2193 			/* Roll back local locks. */
2194 			nfsrv_locallock_rollback(vp, lfp, p);
2195 			NFSLOCKSTATE();
2196 			nfsrv_unlocklf(lfp);
2197 			NFSUNLOCKSTATE();
2198 			NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2199 			vnode_unlocked = 0;
2200 			if ((vp->v_iflag & VI_DOOMED) != 0) {
2201 				error = NFSERR_SERVERFAULT;
2202 				goto out;
2203 			}
2204 		    }
2205 		    /*
2206 		     * nfsrv_clientconflict() unlocks state when it
2207 		     * returns non-zero.
2208 		     */
2209 		    lckstp = NULL;
2210 		    goto tryagain;
2211 		}
2212 		/*
2213 		 * Found a conflicting lock, so record the conflict and
2214 		 * return the error.
2215 		 */
2216 		if (cfp != NULL && ret == 0) {
2217 		    cfp->cl_clientid.lval[0]=lop->lo_stp->ls_stateid.other[0];
2218 		    cfp->cl_clientid.lval[1]=lop->lo_stp->ls_stateid.other[1];
2219 		    cfp->cl_first = lop->lo_first;
2220 		    cfp->cl_end = lop->lo_end;
2221 		    cfp->cl_flags = lop->lo_flags;
2222 		    cfp->cl_ownerlen = lop->lo_stp->ls_ownerlen;
2223 		    NFSBCOPY(lop->lo_stp->ls_owner, cfp->cl_owner,
2224 			cfp->cl_ownerlen);
2225 		}
2226 		if (ret == 2)
2227 		    error = NFSERR_PERM;
2228 		else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2229 		    error = NFSERR_RECLAIMCONFLICT;
2230 		else if (new_stp->ls_flags & NFSLCK_CHECK)
2231 		    error = NFSERR_LOCKED;
2232 		else
2233 		    error = NFSERR_DENIED;
2234 		if (filestruct_locked != 0 && ret == 0) {
2235 			/* Roll back local locks. */
2236 			NFSUNLOCKSTATE();
2237 			if (vnode_unlocked == 0) {
2238 				ASSERT_VOP_ELOCKED(vp, "nfsrv_lockctrl7");
2239 				vnode_unlocked = 1;
2240 				NFSVOPUNLOCK(vp, 0);
2241 			}
2242 			nfsrv_locallock_rollback(vp, lfp, p);
2243 			NFSLOCKSTATE();
2244 			nfsrv_unlocklf(lfp);
2245 		}
2246 		if (ret == 0)
2247 			NFSUNLOCKSTATE();
2248 		goto out;
2249 	    }
2250 	  }
2251 	}
2252 
2253 	/*
2254 	 * We only get here if there was no lock that conflicted.
2255 	 */
2256 	if (new_stp->ls_flags & (NFSLCK_TEST | NFSLCK_CHECK)) {
2257 		NFSUNLOCKSTATE();
2258 		goto out;
2259 	}
2260 
2261 	/*
2262 	 * We only get here when we are creating or modifying a lock.
2263 	 * There are two variants:
2264 	 * - exist_lock_owner where lock_owner exists
2265 	 * - open_to_lock_owner with new lock_owner
2266 	 */
2267 	first = new_lop->lo_first;
2268 	end = new_lop->lo_end;
2269 	lock_flags = new_lop->lo_flags;
2270 	if (!(new_stp->ls_flags & NFSLCK_OPENTOLOCK)) {
2271 		nfsrv_updatelock(lckstp, new_lopp, &other_lop, lfp);
2272 		stateidp->seqid = ++(lckstp->ls_stateid.seqid);
2273 		if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
2274 			stateidp->seqid = lckstp->ls_stateid.seqid = 1;
2275 		stateidp->other[0] = lckstp->ls_stateid.other[0];
2276 		stateidp->other[1] = lckstp->ls_stateid.other[1];
2277 		stateidp->other[2] = lckstp->ls_stateid.other[2];
2278 	} else {
2279 		/*
2280 		 * The new open_to_lock_owner case.
2281 		 * Link the new nfsstate into the lists.
2282 		 */
2283 		new_stp->ls_seq = new_stp->ls_opentolockseq;
2284 		nfsrvd_refcache(new_stp->ls_op);
2285 		stateidp->seqid = new_stp->ls_stateid.seqid = 1;
2286 		stateidp->other[0] = new_stp->ls_stateid.other[0] =
2287 		    clp->lc_clientid.lval[0];
2288 		stateidp->other[1] = new_stp->ls_stateid.other[1] =
2289 		    clp->lc_clientid.lval[1];
2290 		stateidp->other[2] = new_stp->ls_stateid.other[2] =
2291 		    nfsrv_nextstateindex(clp);
2292 		new_stp->ls_clp = clp;
2293 		LIST_INIT(&new_stp->ls_lock);
2294 		new_stp->ls_openstp = stp;
2295 		new_stp->ls_lfp = lfp;
2296 		nfsrv_insertlock(new_lop, (struct nfslock *)new_stp, new_stp,
2297 		    lfp);
2298 		LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_stp->ls_stateid),
2299 		    new_stp, ls_hash);
2300 		LIST_INSERT_HEAD(&stp->ls_open, new_stp, ls_list);
2301 		*new_lopp = NULL;
2302 		*new_stpp = NULL;
2303 		nfsstatsv1.srvlockowners++;
2304 		nfsrv_openpluslock++;
2305 	}
2306 	if (filestruct_locked != 0) {
2307 		NFSUNLOCKSTATE();
2308 		nfsrv_locallock_commit(lfp, lock_flags, first, end);
2309 		NFSLOCKSTATE();
2310 		nfsrv_unlocklf(lfp);
2311 	}
2312 	NFSUNLOCKSTATE();
2313 
2314 out:
2315 	if (haslock) {
2316 		NFSLOCKV4ROOTMUTEX();
2317 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
2318 		NFSUNLOCKV4ROOTMUTEX();
2319 	}
2320 	if (vnode_unlocked != 0) {
2321 		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2322 		if (error == 0 && (vp->v_iflag & VI_DOOMED) != 0)
2323 			error = NFSERR_SERVERFAULT;
2324 	}
2325 	if (other_lop)
2326 		free(other_lop, M_NFSDLOCK);
2327 	NFSEXITCODE2(error, nd);
2328 	return (error);
2329 }
2330 
2331 /*
2332  * Check for state errors for Open.
2333  * repstat is passed back out as an error if more critical errors
2334  * are not detected.
2335  */
2336 APPLESTATIC int
2337 nfsrv_opencheck(nfsquad_t clientid, nfsv4stateid_t *stateidp,
2338     struct nfsstate *new_stp, vnode_t vp, struct nfsrv_descript *nd,
2339     NFSPROC_T *p, int repstat)
2340 {
2341 	struct nfsstate *stp, *nstp;
2342 	struct nfsclient *clp;
2343 	struct nfsstate *ownerstp;
2344 	struct nfslockfile *lfp, *new_lfp;
2345 	int error = 0, haslock = 0, ret, readonly = 0, getfhret = 0;
2346 
2347 	if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2348 		readonly = 1;
2349 	/*
2350 	 * Check for restart conditions (client and server).
2351 	 */
2352 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2353 		&new_stp->ls_stateid, 0);
2354 	if (error)
2355 		goto out;
2356 
2357 	/*
2358 	 * Check for state resource limit exceeded.
2359 	 * Technically this should be SMP protected, but the worst
2360 	 * case error is "out by one or two" on the count when it
2361 	 * returns NFSERR_RESOURCE and the limit is just a rather
2362 	 * arbitrary high water mark, so no harm is done.
2363 	 */
2364 	if (nfsrv_openpluslock > nfsrv_v4statelimit) {
2365 		error = NFSERR_RESOURCE;
2366 		goto out;
2367 	}
2368 
2369 tryagain:
2370 	new_lfp = malloc(sizeof (struct nfslockfile),
2371 	    M_NFSDLOCKFILE, M_WAITOK);
2372 	if (vp)
2373 		getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2374 		    NULL, p);
2375 	NFSLOCKSTATE();
2376 	/*
2377 	 * Get the nfsclient structure.
2378 	 */
2379 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2380 	    (nfsquad_t)((u_quad_t)0), 0, nd, p);
2381 
2382 	/*
2383 	 * Look up the open owner. See if it needs confirmation and
2384 	 * check the seq#, as required.
2385 	 */
2386 	if (!error)
2387 		nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2388 
2389 	if (!error && ownerstp) {
2390 		error = nfsrv_checkseqid(nd, new_stp->ls_seq, ownerstp,
2391 		    new_stp->ls_op);
2392 		/*
2393 		 * If the OpenOwner hasn't been confirmed, assume the
2394 		 * old one was a replay and this one is ok.
2395 		 * See: RFC3530 Sec. 14.2.18.
2396 		 */
2397 		if (error == NFSERR_BADSEQID &&
2398 		    (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM))
2399 			error = 0;
2400 	}
2401 
2402 	/*
2403 	 * Check for grace.
2404 	 */
2405 	if (!error)
2406 		error = nfsrv_checkgrace(nd, clp, new_stp->ls_flags);
2407 	if ((new_stp->ls_flags & NFSLCK_RECLAIM) && !error &&
2408 		nfsrv_checkstable(clp))
2409 		error = NFSERR_NOGRACE;
2410 
2411 	/*
2412 	 * If none of the above errors occurred, let repstat be
2413 	 * returned.
2414 	 */
2415 	if (repstat && !error)
2416 		error = repstat;
2417 	if (error) {
2418 		NFSUNLOCKSTATE();
2419 		if (haslock) {
2420 			NFSLOCKV4ROOTMUTEX();
2421 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2422 			NFSUNLOCKV4ROOTMUTEX();
2423 		}
2424 		free(new_lfp, M_NFSDLOCKFILE);
2425 		goto out;
2426 	}
2427 
2428 	/*
2429 	 * If vp == NULL, the file doesn't exist yet, so return ok.
2430 	 * (This always happens on the first pass, so haslock must be 0.)
2431 	 */
2432 	if (vp == NULL) {
2433 		NFSUNLOCKSTATE();
2434 		free(new_lfp, M_NFSDLOCKFILE);
2435 		goto out;
2436 	}
2437 
2438 	/*
2439 	 * Get the structure for the underlying file.
2440 	 */
2441 	if (getfhret)
2442 		error = getfhret;
2443 	else
2444 		error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2445 		    NULL, 0);
2446 	if (new_lfp)
2447 		free(new_lfp, M_NFSDLOCKFILE);
2448 	if (error) {
2449 		NFSUNLOCKSTATE();
2450 		if (haslock) {
2451 			NFSLOCKV4ROOTMUTEX();
2452 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2453 			NFSUNLOCKV4ROOTMUTEX();
2454 		}
2455 		goto out;
2456 	}
2457 
2458 	/*
2459 	 * Search for a conflicting open/share.
2460 	 */
2461 	if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2462 	    /*
2463 	     * For Delegate_Cur, search for the matching Delegation,
2464 	     * which indicates no conflict.
2465 	     * An old delegation should have been recovered by the
2466 	     * client doing a Claim_DELEGATE_Prev, so I won't let
2467 	     * it match and return NFSERR_EXPIRED. Should I let it
2468 	     * match?
2469 	     */
2470 	    LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2471 		if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2472 		    (((nd->nd_flag & ND_NFSV41) != 0 &&
2473 		    stateidp->seqid == 0) ||
2474 		    stateidp->seqid == stp->ls_stateid.seqid) &&
2475 		    !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2476 			  NFSX_STATEIDOTHER))
2477 			break;
2478 	    }
2479 	    if (stp == LIST_END(&lfp->lf_deleg) ||
2480 		((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2481 		 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2482 		NFSUNLOCKSTATE();
2483 		if (haslock) {
2484 			NFSLOCKV4ROOTMUTEX();
2485 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2486 			NFSUNLOCKV4ROOTMUTEX();
2487 		}
2488 		error = NFSERR_EXPIRED;
2489 		goto out;
2490 	    }
2491 	}
2492 
2493 	/*
2494 	 * Check for access/deny bit conflicts. I check for the same
2495 	 * owner as well, in case the client didn't bother.
2496 	 */
2497 	LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2498 		if (!(new_stp->ls_flags & NFSLCK_DELEGCUR) &&
2499 		    (((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2500 		      ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2501 		     ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2502 		      ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS)))){
2503 			ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2504 			if (ret == 1) {
2505 				/*
2506 				 * nfsrv_clientconflict() unlocks
2507 				 * state when it returns non-zero.
2508 				 */
2509 				goto tryagain;
2510 			}
2511 			if (ret == 2)
2512 				error = NFSERR_PERM;
2513 			else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2514 				error = NFSERR_RECLAIMCONFLICT;
2515 			else
2516 				error = NFSERR_SHAREDENIED;
2517 			if (ret == 0)
2518 				NFSUNLOCKSTATE();
2519 			if (haslock) {
2520 				NFSLOCKV4ROOTMUTEX();
2521 				nfsv4_unlock(&nfsv4rootfs_lock, 1);
2522 				NFSUNLOCKV4ROOTMUTEX();
2523 			}
2524 			goto out;
2525 		}
2526 	}
2527 
2528 	/*
2529 	 * Check for a conflicting delegation. If one is found, call
2530 	 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2531 	 * been set yet, it will get the lock. Otherwise, it will recall
2532 	 * the delegation. Then, we try try again...
2533 	 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2534 	 *  isn't a conflict.)
2535 	 * I currently believe the conflict algorithm to be:
2536 	 * For Open with Read Access and Deny None
2537 	 * - there is a conflict iff a different client has a write delegation
2538 	 * For Open with other Write Access or any Deny except None
2539 	 * - there is a conflict if a different client has any delegation
2540 	 * - there is a conflict if the same client has a read delegation
2541 	 *   (The current consensus is that this last case should be
2542 	 *    considered a conflict since the client with a read delegation
2543 	 *    could have done an Open with ReadAccess and WriteDeny
2544 	 *    locally and then not have checked for the WriteDeny.)
2545 	 * Don't check for a Reclaim, since that will be dealt with
2546 	 * by nfsrv_openctrl().
2547 	 */
2548 	if (!(new_stp->ls_flags &
2549 		(NFSLCK_DELEGPREV | NFSLCK_DELEGCUR | NFSLCK_RECLAIM))) {
2550 	    stp = LIST_FIRST(&lfp->lf_deleg);
2551 	    while (stp != LIST_END(&lfp->lf_deleg)) {
2552 		nstp = LIST_NEXT(stp, ls_file);
2553 		if ((readonly && stp->ls_clp != clp &&
2554 		       (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2555 		    (!readonly && (stp->ls_clp != clp ||
2556 		         (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2557 			ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2558 			if (ret) {
2559 			    /*
2560 			     * nfsrv_delegconflict() unlocks state
2561 			     * when it returns non-zero.
2562 			     */
2563 			    if (ret == -1)
2564 				goto tryagain;
2565 			    error = ret;
2566 			    goto out;
2567 			}
2568 		}
2569 		stp = nstp;
2570 	    }
2571 	}
2572 	NFSUNLOCKSTATE();
2573 	if (haslock) {
2574 		NFSLOCKV4ROOTMUTEX();
2575 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
2576 		NFSUNLOCKV4ROOTMUTEX();
2577 	}
2578 
2579 out:
2580 	NFSEXITCODE2(error, nd);
2581 	return (error);
2582 }
2583 
2584 /*
2585  * Open control function to create/update open state for an open.
2586  */
2587 APPLESTATIC int
2588 nfsrv_openctrl(struct nfsrv_descript *nd, vnode_t vp,
2589     struct nfsstate **new_stpp, nfsquad_t clientid, nfsv4stateid_t *stateidp,
2590     nfsv4stateid_t *delegstateidp, u_int32_t *rflagsp, struct nfsexstuff *exp,
2591     NFSPROC_T *p, u_quad_t filerev)
2592 {
2593 	struct nfsstate *new_stp = *new_stpp;
2594 	struct nfsstate *stp, *nstp;
2595 	struct nfsstate *openstp = NULL, *new_open, *ownerstp, *new_deleg;
2596 	struct nfslockfile *lfp, *new_lfp;
2597 	struct nfsclient *clp;
2598 	int error = 0, haslock = 0, ret, delegate = 1, writedeleg = 1;
2599 	int readonly = 0, cbret = 1, getfhret = 0;
2600 	int gotstate = 0, len = 0;
2601 	u_char *clidp = NULL;
2602 
2603 	if ((new_stp->ls_flags & NFSLCK_SHAREBITS) == NFSLCK_READACCESS)
2604 		readonly = 1;
2605 	/*
2606 	 * Check for restart conditions (client and server).
2607 	 * (Paranoia, should have been detected by nfsrv_opencheck().)
2608 	 * If an error does show up, return NFSERR_EXPIRED, since the
2609 	 * the seqid# has already been incremented.
2610 	 */
2611 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
2612 	    &new_stp->ls_stateid, 0);
2613 	if (error) {
2614 		printf("Nfsd: openctrl unexpected restart err=%d\n",
2615 		    error);
2616 		error = NFSERR_EXPIRED;
2617 		goto out;
2618 	}
2619 
2620 	clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
2621 tryagain:
2622 	new_lfp = malloc(sizeof (struct nfslockfile),
2623 	    M_NFSDLOCKFILE, M_WAITOK);
2624 	new_open = malloc(sizeof (struct nfsstate),
2625 	    M_NFSDSTATE, M_WAITOK);
2626 	new_deleg = malloc(sizeof (struct nfsstate),
2627 	    M_NFSDSTATE, M_WAITOK);
2628 	getfhret = nfsrv_getlockfh(vp, new_stp->ls_flags, new_lfp,
2629 	    NULL, p);
2630 	NFSLOCKSTATE();
2631 	/*
2632 	 * Get the client structure. Since the linked lists could be changed
2633 	 * by other nfsd processes if this process does a tsleep(), one of
2634 	 * two things must be done.
2635 	 * 1 - don't tsleep()
2636 	 * or
2637 	 * 2 - get the nfsv4_lock() { indicated by haslock == 1 }
2638 	 *     before using the lists, since this lock stops the other
2639 	 *     nfsd. This should only be used for rare cases, since it
2640 	 *     essentially single threads the nfsd.
2641 	 *     At this time, it is only done for cases where the stable
2642 	 *     storage file must be written prior to completion of state
2643 	 *     expiration.
2644 	 */
2645 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
2646 	    (nfsquad_t)((u_quad_t)0), 0, nd, p);
2647 	if (!error && (clp->lc_flags & LCL_NEEDSCBNULL) &&
2648 	    clp->lc_program) {
2649 		/*
2650 		 * This happens on the first open for a client
2651 		 * that supports callbacks.
2652 		 */
2653 		NFSUNLOCKSTATE();
2654 		/*
2655 		 * Although nfsrv_docallback() will sleep, clp won't
2656 		 * go away, since they are only removed when the
2657 		 * nfsv4_lock() has blocked the nfsd threads. The
2658 		 * fields in clp can change, but having multiple
2659 		 * threads do this Null callback RPC should be
2660 		 * harmless.
2661 		 */
2662 		cbret = nfsrv_docallback(clp, NFSV4PROC_CBNULL,
2663 		    NULL, 0, NULL, NULL, NULL, 0, p);
2664 		NFSLOCKSTATE();
2665 		clp->lc_flags &= ~LCL_NEEDSCBNULL;
2666 		if (!cbret)
2667 			clp->lc_flags |= LCL_CALLBACKSON;
2668 	}
2669 
2670 	/*
2671 	 * Look up the open owner. See if it needs confirmation and
2672 	 * check the seq#, as required.
2673 	 */
2674 	if (!error)
2675 		nfsrv_getowner(&clp->lc_open, new_stp, &ownerstp);
2676 
2677 	if (error) {
2678 		NFSUNLOCKSTATE();
2679 		printf("Nfsd: openctrl unexpected state err=%d\n",
2680 			error);
2681 		free(new_lfp, M_NFSDLOCKFILE);
2682 		free(new_open, M_NFSDSTATE);
2683 		free(new_deleg, M_NFSDSTATE);
2684 		if (haslock) {
2685 			NFSLOCKV4ROOTMUTEX();
2686 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2687 			NFSUNLOCKV4ROOTMUTEX();
2688 		}
2689 		error = NFSERR_EXPIRED;
2690 		goto out;
2691 	}
2692 
2693 	if (new_stp->ls_flags & NFSLCK_RECLAIM)
2694 		nfsrv_markstable(clp);
2695 
2696 	/*
2697 	 * Get the structure for the underlying file.
2698 	 */
2699 	if (getfhret)
2700 		error = getfhret;
2701 	else
2702 		error = nfsrv_getlockfile(new_stp->ls_flags, &new_lfp, &lfp,
2703 		    NULL, 0);
2704 	if (new_lfp)
2705 		free(new_lfp, M_NFSDLOCKFILE);
2706 	if (error) {
2707 		NFSUNLOCKSTATE();
2708 		printf("Nfsd openctrl unexpected getlockfile err=%d\n",
2709 		    error);
2710 		free(new_open, M_NFSDSTATE);
2711 		free(new_deleg, M_NFSDSTATE);
2712 		if (haslock) {
2713 			NFSLOCKV4ROOTMUTEX();
2714 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2715 			NFSUNLOCKV4ROOTMUTEX();
2716 		}
2717 		goto out;
2718 	}
2719 
2720 	/*
2721 	 * Search for a conflicting open/share.
2722 	 */
2723 	if (new_stp->ls_flags & NFSLCK_DELEGCUR) {
2724 	    /*
2725 	     * For Delegate_Cur, search for the matching Delegation,
2726 	     * which indicates no conflict.
2727 	     * An old delegation should have been recovered by the
2728 	     * client doing a Claim_DELEGATE_Prev, so I won't let
2729 	     * it match and return NFSERR_EXPIRED. Should I let it
2730 	     * match?
2731 	     */
2732 	    LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2733 		if (!(stp->ls_flags & NFSLCK_OLDDELEG) &&
2734 		    (((nd->nd_flag & ND_NFSV41) != 0 &&
2735 		    stateidp->seqid == 0) ||
2736 		    stateidp->seqid == stp->ls_stateid.seqid) &&
2737 		    !NFSBCMP(stateidp->other, stp->ls_stateid.other,
2738 			NFSX_STATEIDOTHER))
2739 			break;
2740 	    }
2741 	    if (stp == LIST_END(&lfp->lf_deleg) ||
2742 		((new_stp->ls_flags & NFSLCK_WRITEACCESS) &&
2743 		 (stp->ls_flags & NFSLCK_DELEGREAD))) {
2744 		NFSUNLOCKSTATE();
2745 		printf("Nfsd openctrl unexpected expiry\n");
2746 		free(new_open, M_NFSDSTATE);
2747 		free(new_deleg, M_NFSDSTATE);
2748 		if (haslock) {
2749 			NFSLOCKV4ROOTMUTEX();
2750 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
2751 			NFSUNLOCKV4ROOTMUTEX();
2752 		}
2753 		error = NFSERR_EXPIRED;
2754 		goto out;
2755 	    }
2756 
2757 	    /*
2758 	     * Don't issue a Delegation, since one already exists and
2759 	     * delay delegation timeout, as required.
2760 	     */
2761 	    delegate = 0;
2762 	    nfsrv_delaydelegtimeout(stp);
2763 	}
2764 
2765 	/*
2766 	 * Check for access/deny bit conflicts. I also check for the
2767 	 * same owner, since the client might not have bothered to check.
2768 	 * Also, note an open for the same file and owner, if found,
2769 	 * which is all we do here for Delegate_Cur, since conflict
2770 	 * checking is already done.
2771 	 */
2772 	LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
2773 		if (ownerstp && stp->ls_openowner == ownerstp)
2774 			openstp = stp;
2775 		if (!(new_stp->ls_flags & NFSLCK_DELEGCUR)) {
2776 		    /*
2777 		     * If another client has the file open, the only
2778 		     * delegation that can be issued is a Read delegation
2779 		     * and only if it is a Read open with Deny none.
2780 		     */
2781 		    if (clp != stp->ls_clp) {
2782 			if ((stp->ls_flags & NFSLCK_SHAREBITS) ==
2783 			    NFSLCK_READACCESS)
2784 			    writedeleg = 0;
2785 			else
2786 			    delegate = 0;
2787 		    }
2788 		    if(((new_stp->ls_flags & NFSLCK_ACCESSBITS) &
2789 		        ((stp->ls_flags>>NFSLCK_SHIFT) & NFSLCK_ACCESSBITS))||
2790 		       ((stp->ls_flags & NFSLCK_ACCESSBITS) &
2791 		        ((new_stp->ls_flags>>NFSLCK_SHIFT)&NFSLCK_ACCESSBITS))){
2792 			ret = nfsrv_clientconflict(stp->ls_clp,&haslock,vp,p);
2793 			if (ret == 1) {
2794 				/*
2795 				 * nfsrv_clientconflict() unlocks state
2796 				 * when it returns non-zero.
2797 				 */
2798 				free(new_open, M_NFSDSTATE);
2799 				free(new_deleg, M_NFSDSTATE);
2800 				openstp = NULL;
2801 				goto tryagain;
2802 			}
2803 			if (ret == 2)
2804 				error = NFSERR_PERM;
2805 			else if (new_stp->ls_flags & NFSLCK_RECLAIM)
2806 				error = NFSERR_RECLAIMCONFLICT;
2807 			else
2808 				error = NFSERR_SHAREDENIED;
2809 			if (ret == 0)
2810 				NFSUNLOCKSTATE();
2811 			if (haslock) {
2812 				NFSLOCKV4ROOTMUTEX();
2813 				nfsv4_unlock(&nfsv4rootfs_lock, 1);
2814 				NFSUNLOCKV4ROOTMUTEX();
2815 			}
2816 			free(new_open, M_NFSDSTATE);
2817 			free(new_deleg, M_NFSDSTATE);
2818 			printf("nfsd openctrl unexpected client cnfl\n");
2819 			goto out;
2820 		    }
2821 		}
2822 	}
2823 
2824 	/*
2825 	 * Check for a conflicting delegation. If one is found, call
2826 	 * nfsrv_delegconflict() to handle it. If the v4root lock hasn't
2827 	 * been set yet, it will get the lock. Otherwise, it will recall
2828 	 * the delegation. Then, we try try again...
2829 	 * (If NFSLCK_DELEGCUR is set, it has a delegation, so there
2830 	 *  isn't a conflict.)
2831 	 * I currently believe the conflict algorithm to be:
2832 	 * For Open with Read Access and Deny None
2833 	 * - there is a conflict iff a different client has a write delegation
2834 	 * For Open with other Write Access or any Deny except None
2835 	 * - there is a conflict if a different client has any delegation
2836 	 * - there is a conflict if the same client has a read delegation
2837 	 *   (The current consensus is that this last case should be
2838 	 *    considered a conflict since the client with a read delegation
2839 	 *    could have done an Open with ReadAccess and WriteDeny
2840 	 *    locally and then not have checked for the WriteDeny.)
2841 	 */
2842 	if (!(new_stp->ls_flags & (NFSLCK_DELEGPREV | NFSLCK_DELEGCUR))) {
2843 	    stp = LIST_FIRST(&lfp->lf_deleg);
2844 	    while (stp != LIST_END(&lfp->lf_deleg)) {
2845 		nstp = LIST_NEXT(stp, ls_file);
2846 		if (stp->ls_clp != clp && (stp->ls_flags & NFSLCK_DELEGREAD))
2847 			writedeleg = 0;
2848 		else
2849 			delegate = 0;
2850 		if ((readonly && stp->ls_clp != clp &&
2851 		       (stp->ls_flags & NFSLCK_DELEGWRITE)) ||
2852 		    (!readonly && (stp->ls_clp != clp ||
2853 		         (stp->ls_flags & NFSLCK_DELEGREAD)))) {
2854 		    if (new_stp->ls_flags & NFSLCK_RECLAIM) {
2855 			delegate = 2;
2856 		    } else {
2857 			ret = nfsrv_delegconflict(stp, &haslock, p, vp);
2858 			if (ret) {
2859 			    /*
2860 			     * nfsrv_delegconflict() unlocks state
2861 			     * when it returns non-zero.
2862 			     */
2863 			    printf("Nfsd openctrl unexpected deleg cnfl\n");
2864 			    free(new_open, M_NFSDSTATE);
2865 			    free(new_deleg, M_NFSDSTATE);
2866 			    if (ret == -1) {
2867 				openstp = NULL;
2868 				goto tryagain;
2869 			    }
2870 			    error = ret;
2871 			    goto out;
2872 			}
2873 		    }
2874 		}
2875 		stp = nstp;
2876 	    }
2877 	}
2878 
2879 	/*
2880 	 * We only get here if there was no open that conflicted.
2881 	 * If an open for the owner exists, or in the access/deny bits.
2882 	 * Otherwise it is a new open. If the open_owner hasn't been
2883 	 * confirmed, replace the open with the new one needing confirmation,
2884 	 * otherwise add the open.
2885 	 */
2886 	if (new_stp->ls_flags & NFSLCK_DELEGPREV) {
2887 	    /*
2888 	     * Handle NFSLCK_DELEGPREV by searching the old delegations for
2889 	     * a match. If found, just move the old delegation to the current
2890 	     * delegation list and issue open. If not found, return
2891 	     * NFSERR_EXPIRED.
2892 	     */
2893 	    LIST_FOREACH(stp, &clp->lc_olddeleg, ls_list) {
2894 		if (stp->ls_lfp == lfp) {
2895 		    /* Found it */
2896 		    if (stp->ls_clp != clp)
2897 			panic("olddeleg clp");
2898 		    LIST_REMOVE(stp, ls_list);
2899 		    LIST_REMOVE(stp, ls_hash);
2900 		    stp->ls_flags &= ~NFSLCK_OLDDELEG;
2901 		    stp->ls_stateid.seqid = delegstateidp->seqid = 1;
2902 		    stp->ls_stateid.other[0] = delegstateidp->other[0] =
2903 			clp->lc_clientid.lval[0];
2904 		    stp->ls_stateid.other[1] = delegstateidp->other[1] =
2905 			clp->lc_clientid.lval[1];
2906 		    stp->ls_stateid.other[2] = delegstateidp->other[2] =
2907 			nfsrv_nextstateindex(clp);
2908 		    stp->ls_compref = nd->nd_compref;
2909 		    LIST_INSERT_HEAD(&clp->lc_deleg, stp, ls_list);
2910 		    LIST_INSERT_HEAD(NFSSTATEHASH(clp,
2911 			stp->ls_stateid), stp, ls_hash);
2912 		    if (stp->ls_flags & NFSLCK_DELEGWRITE)
2913 			*rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2914 		    else
2915 			*rflagsp |= NFSV4OPEN_READDELEGATE;
2916 		    clp->lc_delegtime = NFSD_MONOSEC +
2917 			nfsrv_lease + NFSRV_LEASEDELTA;
2918 
2919 		    /*
2920 		     * Now, do the associated open.
2921 		     */
2922 		    new_open->ls_stateid.seqid = 1;
2923 		    new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
2924 		    new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
2925 		    new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
2926 		    new_open->ls_flags = (new_stp->ls_flags&NFSLCK_DENYBITS)|
2927 			NFSLCK_OPEN;
2928 		    if (stp->ls_flags & NFSLCK_DELEGWRITE)
2929 			new_open->ls_flags |= (NFSLCK_READACCESS |
2930 			    NFSLCK_WRITEACCESS);
2931 		    else
2932 			new_open->ls_flags |= NFSLCK_READACCESS;
2933 		    new_open->ls_uid = new_stp->ls_uid;
2934 		    new_open->ls_lfp = lfp;
2935 		    new_open->ls_clp = clp;
2936 		    LIST_INIT(&new_open->ls_open);
2937 		    LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
2938 		    LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
2939 			new_open, ls_hash);
2940 		    /*
2941 		     * and handle the open owner
2942 		     */
2943 		    if (ownerstp) {
2944 			new_open->ls_openowner = ownerstp;
2945 			LIST_INSERT_HEAD(&ownerstp->ls_open,new_open,ls_list);
2946 		    } else {
2947 			new_open->ls_openowner = new_stp;
2948 			new_stp->ls_flags = 0;
2949 			nfsrvd_refcache(new_stp->ls_op);
2950 			new_stp->ls_noopens = 0;
2951 			LIST_INIT(&new_stp->ls_open);
2952 			LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
2953 			LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
2954 			*new_stpp = NULL;
2955 			nfsstatsv1.srvopenowners++;
2956 			nfsrv_openpluslock++;
2957 		    }
2958 		    openstp = new_open;
2959 		    new_open = NULL;
2960 		    nfsstatsv1.srvopens++;
2961 		    nfsrv_openpluslock++;
2962 		    break;
2963 		}
2964 	    }
2965 	    if (stp == LIST_END(&clp->lc_olddeleg))
2966 		error = NFSERR_EXPIRED;
2967 	} else if (new_stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) {
2968 	    /*
2969 	     * Scan to see that no delegation for this client and file
2970 	     * doesn't already exist.
2971 	     * There also shouldn't yet be an Open for this file and
2972 	     * openowner.
2973 	     */
2974 	    LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
2975 		if (stp->ls_clp == clp)
2976 		    break;
2977 	    }
2978 	    if (stp == LIST_END(&lfp->lf_deleg) && openstp == NULL) {
2979 		/*
2980 		 * This is the Claim_Previous case with a delegation
2981 		 * type != Delegate_None.
2982 		 */
2983 		/*
2984 		 * First, add the delegation. (Although we must issue the
2985 		 * delegation, we can also ask for an immediate return.)
2986 		 */
2987 		new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
2988 		new_deleg->ls_stateid.other[0] = delegstateidp->other[0] =
2989 		    clp->lc_clientid.lval[0];
2990 		new_deleg->ls_stateid.other[1] = delegstateidp->other[1] =
2991 		    clp->lc_clientid.lval[1];
2992 		new_deleg->ls_stateid.other[2] = delegstateidp->other[2] =
2993 		    nfsrv_nextstateindex(clp);
2994 		if (new_stp->ls_flags & NFSLCK_DELEGWRITE) {
2995 		    new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
2996 			NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
2997 		    *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
2998 		    nfsrv_writedelegcnt++;
2999 		} else {
3000 		    new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3001 			NFSLCK_READACCESS);
3002 		    *rflagsp |= NFSV4OPEN_READDELEGATE;
3003 		}
3004 		new_deleg->ls_uid = new_stp->ls_uid;
3005 		new_deleg->ls_lfp = lfp;
3006 		new_deleg->ls_clp = clp;
3007 		new_deleg->ls_filerev = filerev;
3008 		new_deleg->ls_compref = nd->nd_compref;
3009 		LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3010 		LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3011 		    new_deleg->ls_stateid), new_deleg, ls_hash);
3012 		LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3013 		new_deleg = NULL;
3014 		if (delegate == 2 || nfsrv_issuedelegs == 0 ||
3015 		    (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3016 		     LCL_CALLBACKSON ||
3017 		    NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) ||
3018 		    !NFSVNO_DELEGOK(vp))
3019 		    *rflagsp |= NFSV4OPEN_RECALL;
3020 		nfsstatsv1.srvdelegates++;
3021 		nfsrv_openpluslock++;
3022 		nfsrv_delegatecnt++;
3023 
3024 		/*
3025 		 * Now, do the associated open.
3026 		 */
3027 		new_open->ls_stateid.seqid = 1;
3028 		new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3029 		new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3030 		new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3031 		new_open->ls_flags = (new_stp->ls_flags & NFSLCK_DENYBITS) |
3032 		    NFSLCK_OPEN;
3033 		if (new_stp->ls_flags & NFSLCK_DELEGWRITE)
3034 			new_open->ls_flags |= (NFSLCK_READACCESS |
3035 			    NFSLCK_WRITEACCESS);
3036 		else
3037 			new_open->ls_flags |= NFSLCK_READACCESS;
3038 		new_open->ls_uid = new_stp->ls_uid;
3039 		new_open->ls_lfp = lfp;
3040 		new_open->ls_clp = clp;
3041 		LIST_INIT(&new_open->ls_open);
3042 		LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3043 		LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3044 		   new_open, ls_hash);
3045 		/*
3046 		 * and handle the open owner
3047 		 */
3048 		if (ownerstp) {
3049 		    new_open->ls_openowner = ownerstp;
3050 		    LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3051 		} else {
3052 		    new_open->ls_openowner = new_stp;
3053 		    new_stp->ls_flags = 0;
3054 		    nfsrvd_refcache(new_stp->ls_op);
3055 		    new_stp->ls_noopens = 0;
3056 		    LIST_INIT(&new_stp->ls_open);
3057 		    LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3058 		    LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3059 		    *new_stpp = NULL;
3060 		    nfsstatsv1.srvopenowners++;
3061 		    nfsrv_openpluslock++;
3062 		}
3063 		openstp = new_open;
3064 		new_open = NULL;
3065 		nfsstatsv1.srvopens++;
3066 		nfsrv_openpluslock++;
3067 	    } else {
3068 		error = NFSERR_RECLAIMCONFLICT;
3069 	    }
3070 	} else if (ownerstp) {
3071 		if (ownerstp->ls_flags & NFSLCK_NEEDSCONFIRM) {
3072 		    /* Replace the open */
3073 		    if (ownerstp->ls_op)
3074 			nfsrvd_derefcache(ownerstp->ls_op);
3075 		    ownerstp->ls_op = new_stp->ls_op;
3076 		    nfsrvd_refcache(ownerstp->ls_op);
3077 		    ownerstp->ls_seq = new_stp->ls_seq;
3078 		    *rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3079 		    stp = LIST_FIRST(&ownerstp->ls_open);
3080 		    stp->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3081 			NFSLCK_OPEN;
3082 		    stp->ls_stateid.seqid = 1;
3083 		    stp->ls_uid = new_stp->ls_uid;
3084 		    if (lfp != stp->ls_lfp) {
3085 			LIST_REMOVE(stp, ls_file);
3086 			LIST_INSERT_HEAD(&lfp->lf_open, stp, ls_file);
3087 			stp->ls_lfp = lfp;
3088 		    }
3089 		    openstp = stp;
3090 		} else if (openstp) {
3091 		    openstp->ls_flags |= (new_stp->ls_flags & NFSLCK_SHAREBITS);
3092 		    openstp->ls_stateid.seqid++;
3093 		    if ((nd->nd_flag & ND_NFSV41) != 0 &&
3094 			openstp->ls_stateid.seqid == 0)
3095 			openstp->ls_stateid.seqid = 1;
3096 
3097 		    /*
3098 		     * This is where we can choose to issue a delegation.
3099 		     */
3100 		    if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3101 			*rflagsp |= NFSV4OPEN_WDNOTWANTED;
3102 		    else if (nfsrv_issuedelegs == 0)
3103 			*rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3104 		    else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3105 			*rflagsp |= NFSV4OPEN_WDRESOURCE;
3106 		    else if (delegate == 0 || writedeleg == 0 ||
3107 			NFSVNO_EXRDONLY(exp) || (readonly != 0 &&
3108 			nfsrv_writedelegifpos == 0) ||
3109 			!NFSVNO_DELEGOK(vp) ||
3110 			(new_stp->ls_flags & NFSLCK_WANTRDELEG) != 0 ||
3111 			(clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3112 			 LCL_CALLBACKSON)
3113 			*rflagsp |= NFSV4OPEN_WDCONTENTION;
3114 		    else {
3115 			new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3116 			new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3117 			    = clp->lc_clientid.lval[0];
3118 			new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3119 			    = clp->lc_clientid.lval[1];
3120 			new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3121 			    = nfsrv_nextstateindex(clp);
3122 			new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3123 			    NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3124 			*rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3125 			new_deleg->ls_uid = new_stp->ls_uid;
3126 			new_deleg->ls_lfp = lfp;
3127 			new_deleg->ls_clp = clp;
3128 			new_deleg->ls_filerev = filerev;
3129 			new_deleg->ls_compref = nd->nd_compref;
3130 			nfsrv_writedelegcnt++;
3131 			LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3132 			LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3133 			    new_deleg->ls_stateid), new_deleg, ls_hash);
3134 			LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3135 			new_deleg = NULL;
3136 			nfsstatsv1.srvdelegates++;
3137 			nfsrv_openpluslock++;
3138 			nfsrv_delegatecnt++;
3139 		    }
3140 		} else {
3141 		    new_open->ls_stateid.seqid = 1;
3142 		    new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3143 		    new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3144 		    new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3145 		    new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS)|
3146 			NFSLCK_OPEN;
3147 		    new_open->ls_uid = new_stp->ls_uid;
3148 		    new_open->ls_openowner = ownerstp;
3149 		    new_open->ls_lfp = lfp;
3150 		    new_open->ls_clp = clp;
3151 		    LIST_INIT(&new_open->ls_open);
3152 		    LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3153 		    LIST_INSERT_HEAD(&ownerstp->ls_open, new_open, ls_list);
3154 		    LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3155 			new_open, ls_hash);
3156 		    openstp = new_open;
3157 		    new_open = NULL;
3158 		    nfsstatsv1.srvopens++;
3159 		    nfsrv_openpluslock++;
3160 
3161 		    /*
3162 		     * This is where we can choose to issue a delegation.
3163 		     */
3164 		    if ((new_stp->ls_flags & NFSLCK_WANTNODELEG) != 0)
3165 			*rflagsp |= NFSV4OPEN_WDNOTWANTED;
3166 		    else if (nfsrv_issuedelegs == 0)
3167 			*rflagsp |= NFSV4OPEN_WDSUPPFTYPE;
3168 		    else if (NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt))
3169 			*rflagsp |= NFSV4OPEN_WDRESOURCE;
3170 		    else if (delegate == 0 || (writedeleg == 0 &&
3171 			readonly == 0) || !NFSVNO_DELEGOK(vp) ||
3172 			(clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) !=
3173 			 LCL_CALLBACKSON)
3174 			*rflagsp |= NFSV4OPEN_WDCONTENTION;
3175 		    else {
3176 			new_deleg->ls_stateid.seqid = delegstateidp->seqid = 1;
3177 			new_deleg->ls_stateid.other[0] = delegstateidp->other[0]
3178 			    = clp->lc_clientid.lval[0];
3179 			new_deleg->ls_stateid.other[1] = delegstateidp->other[1]
3180 			    = clp->lc_clientid.lval[1];
3181 			new_deleg->ls_stateid.other[2] = delegstateidp->other[2]
3182 			    = nfsrv_nextstateindex(clp);
3183 			if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3184 			    (nfsrv_writedelegifpos || !readonly) &&
3185 			    (new_stp->ls_flags & NFSLCK_WANTRDELEG) == 0) {
3186 			    new_deleg->ls_flags = (NFSLCK_DELEGWRITE |
3187 				NFSLCK_READACCESS | NFSLCK_WRITEACCESS);
3188 			    *rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3189 			    nfsrv_writedelegcnt++;
3190 			} else {
3191 			    new_deleg->ls_flags = (NFSLCK_DELEGREAD |
3192 				NFSLCK_READACCESS);
3193 			    *rflagsp |= NFSV4OPEN_READDELEGATE;
3194 			}
3195 			new_deleg->ls_uid = new_stp->ls_uid;
3196 			new_deleg->ls_lfp = lfp;
3197 			new_deleg->ls_clp = clp;
3198 			new_deleg->ls_filerev = filerev;
3199 			new_deleg->ls_compref = nd->nd_compref;
3200 			LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg, ls_file);
3201 			LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3202 			    new_deleg->ls_stateid), new_deleg, ls_hash);
3203 			LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg, ls_list);
3204 			new_deleg = NULL;
3205 			nfsstatsv1.srvdelegates++;
3206 			nfsrv_openpluslock++;
3207 			nfsrv_delegatecnt++;
3208 		    }
3209 		}
3210 	} else {
3211 		/*
3212 		 * New owner case. Start the open_owner sequence with a
3213 		 * Needs confirmation (unless a reclaim) and hang the
3214 		 * new open off it.
3215 		 */
3216 		new_open->ls_stateid.seqid = 1;
3217 		new_open->ls_stateid.other[0] = clp->lc_clientid.lval[0];
3218 		new_open->ls_stateid.other[1] = clp->lc_clientid.lval[1];
3219 		new_open->ls_stateid.other[2] = nfsrv_nextstateindex(clp);
3220 		new_open->ls_flags = (new_stp->ls_flags & NFSLCK_SHAREBITS) |
3221 		    NFSLCK_OPEN;
3222 		new_open->ls_uid = new_stp->ls_uid;
3223 		LIST_INIT(&new_open->ls_open);
3224 		new_open->ls_openowner = new_stp;
3225 		new_open->ls_lfp = lfp;
3226 		new_open->ls_clp = clp;
3227 		LIST_INSERT_HEAD(&lfp->lf_open, new_open, ls_file);
3228 		if (new_stp->ls_flags & NFSLCK_RECLAIM) {
3229 			new_stp->ls_flags = 0;
3230 		} else if ((nd->nd_flag & ND_NFSV41) != 0) {
3231 			/* NFSv4.1 never needs confirmation. */
3232 			new_stp->ls_flags = 0;
3233 
3234 			/*
3235 			 * This is where we can choose to issue a delegation.
3236 			 */
3237 			if (delegate && nfsrv_issuedelegs &&
3238 			    (writedeleg || readonly) &&
3239 			    (clp->lc_flags & (LCL_CALLBACKSON | LCL_CBDOWN)) ==
3240 			     LCL_CALLBACKSON &&
3241 			    !NFSRV_V4DELEGLIMIT(nfsrv_delegatecnt) &&
3242 			    NFSVNO_DELEGOK(vp) &&
3243 			    ((nd->nd_flag & ND_NFSV41) == 0 ||
3244 			     (new_stp->ls_flags & NFSLCK_WANTNODELEG) == 0)) {
3245 				new_deleg->ls_stateid.seqid =
3246 				    delegstateidp->seqid = 1;
3247 				new_deleg->ls_stateid.other[0] =
3248 				    delegstateidp->other[0]
3249 				    = clp->lc_clientid.lval[0];
3250 				new_deleg->ls_stateid.other[1] =
3251 				    delegstateidp->other[1]
3252 				    = clp->lc_clientid.lval[1];
3253 				new_deleg->ls_stateid.other[2] =
3254 				    delegstateidp->other[2]
3255 				    = nfsrv_nextstateindex(clp);
3256 				if (writedeleg && !NFSVNO_EXRDONLY(exp) &&
3257 				    (nfsrv_writedelegifpos || !readonly) &&
3258 				    ((nd->nd_flag & ND_NFSV41) == 0 ||
3259 				     (new_stp->ls_flags & NFSLCK_WANTRDELEG) ==
3260 				     0)) {
3261 					new_deleg->ls_flags =
3262 					    (NFSLCK_DELEGWRITE |
3263 					     NFSLCK_READACCESS |
3264 					     NFSLCK_WRITEACCESS);
3265 					*rflagsp |= NFSV4OPEN_WRITEDELEGATE;
3266 					nfsrv_writedelegcnt++;
3267 				} else {
3268 					new_deleg->ls_flags =
3269 					    (NFSLCK_DELEGREAD |
3270 					     NFSLCK_READACCESS);
3271 					*rflagsp |= NFSV4OPEN_READDELEGATE;
3272 				}
3273 				new_deleg->ls_uid = new_stp->ls_uid;
3274 				new_deleg->ls_lfp = lfp;
3275 				new_deleg->ls_clp = clp;
3276 				new_deleg->ls_filerev = filerev;
3277 				new_deleg->ls_compref = nd->nd_compref;
3278 				LIST_INSERT_HEAD(&lfp->lf_deleg, new_deleg,
3279 				    ls_file);
3280 				LIST_INSERT_HEAD(NFSSTATEHASH(clp,
3281 				    new_deleg->ls_stateid), new_deleg, ls_hash);
3282 				LIST_INSERT_HEAD(&clp->lc_deleg, new_deleg,
3283 				    ls_list);
3284 				new_deleg = NULL;
3285 				nfsstatsv1.srvdelegates++;
3286 				nfsrv_openpluslock++;
3287 				nfsrv_delegatecnt++;
3288 			}
3289 			/*
3290 			 * Since NFSv4.1 never does an OpenConfirm, the first
3291 			 * open state will be acquired here.
3292 			 */
3293 			if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3294 				clp->lc_flags |= LCL_STAMPEDSTABLE;
3295 				len = clp->lc_idlen;
3296 				NFSBCOPY(clp->lc_id, clidp, len);
3297 				gotstate = 1;
3298 			}
3299 		} else {
3300 			*rflagsp |= NFSV4OPEN_RESULTCONFIRM;
3301 			new_stp->ls_flags = NFSLCK_NEEDSCONFIRM;
3302 		}
3303 		nfsrvd_refcache(new_stp->ls_op);
3304 		new_stp->ls_noopens = 0;
3305 		LIST_INIT(&new_stp->ls_open);
3306 		LIST_INSERT_HEAD(&new_stp->ls_open, new_open, ls_list);
3307 		LIST_INSERT_HEAD(&clp->lc_open, new_stp, ls_list);
3308 		LIST_INSERT_HEAD(NFSSTATEHASH(clp, new_open->ls_stateid),
3309 		    new_open, ls_hash);
3310 		openstp = new_open;
3311 		new_open = NULL;
3312 		*new_stpp = NULL;
3313 		nfsstatsv1.srvopens++;
3314 		nfsrv_openpluslock++;
3315 		nfsstatsv1.srvopenowners++;
3316 		nfsrv_openpluslock++;
3317 	}
3318 	if (!error) {
3319 		stateidp->seqid = openstp->ls_stateid.seqid;
3320 		stateidp->other[0] = openstp->ls_stateid.other[0];
3321 		stateidp->other[1] = openstp->ls_stateid.other[1];
3322 		stateidp->other[2] = openstp->ls_stateid.other[2];
3323 	}
3324 	NFSUNLOCKSTATE();
3325 	if (haslock) {
3326 		NFSLOCKV4ROOTMUTEX();
3327 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
3328 		NFSUNLOCKV4ROOTMUTEX();
3329 	}
3330 	if (new_open)
3331 		free(new_open, M_NFSDSTATE);
3332 	if (new_deleg)
3333 		free(new_deleg, M_NFSDSTATE);
3334 
3335 	/*
3336 	 * If the NFSv4.1 client just acquired its first open, write a timestamp
3337 	 * to the stable storage file.
3338 	 */
3339 	if (gotstate != 0) {
3340 		nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3341 		nfsrv_backupstable();
3342 	}
3343 
3344 out:
3345 	free(clidp, M_TEMP);
3346 	NFSEXITCODE2(error, nd);
3347 	return (error);
3348 }
3349 
3350 /*
3351  * Open update. Does the confirm, downgrade and close.
3352  */
3353 APPLESTATIC int
3354 nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
3355     nfsv4stateid_t *stateidp, struct nfsrv_descript *nd, NFSPROC_T *p,
3356     int *retwriteaccessp)
3357 {
3358 	struct nfsstate *stp;
3359 	struct nfsclient *clp;
3360 	struct nfslockfile *lfp;
3361 	u_int32_t bits;
3362 	int error = 0, gotstate = 0, len = 0;
3363 	u_char *clidp = NULL;
3364 
3365 	/*
3366 	 * Check for restart conditions (client and server).
3367 	 */
3368 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3369 	    &new_stp->ls_stateid, 0);
3370 	if (error)
3371 		goto out;
3372 
3373 	clidp = malloc(NFSV4_OPAQUELIMIT, M_TEMP, M_WAITOK);
3374 	NFSLOCKSTATE();
3375 	/*
3376 	 * Get the open structure via clientid and stateid.
3377 	 */
3378 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3379 	    (nfsquad_t)((u_quad_t)0), 0, nd, p);
3380 	if (!error)
3381 		error = nfsrv_getstate(clp, &new_stp->ls_stateid,
3382 		    new_stp->ls_flags, &stp);
3383 
3384 	/*
3385 	 * Sanity check the open.
3386 	 */
3387 	if (!error && (!(stp->ls_flags & NFSLCK_OPEN) ||
3388 		(!(new_stp->ls_flags & NFSLCK_CONFIRM) &&
3389 		 (stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)) ||
3390 		((new_stp->ls_flags & NFSLCK_CONFIRM) &&
3391 		 (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM)))))
3392 		error = NFSERR_BADSTATEID;
3393 
3394 	if (!error)
3395 		error = nfsrv_checkseqid(nd, new_stp->ls_seq,
3396 		    stp->ls_openowner, new_stp->ls_op);
3397 	if (!error && stp->ls_stateid.seqid != new_stp->ls_stateid.seqid &&
3398 	    (((nd->nd_flag & ND_NFSV41) == 0 &&
3399 	      !(new_stp->ls_flags & NFSLCK_CONFIRM)) ||
3400 	     ((nd->nd_flag & ND_NFSV41) != 0 &&
3401 	      new_stp->ls_stateid.seqid != 0)))
3402 		error = NFSERR_OLDSTATEID;
3403 	if (!error && vnode_vtype(vp) != VREG) {
3404 		if (vnode_vtype(vp) == VDIR)
3405 			error = NFSERR_ISDIR;
3406 		else
3407 			error = NFSERR_INVAL;
3408 	}
3409 
3410 	if (error) {
3411 		/*
3412 		 * If a client tries to confirm an Open with a bad
3413 		 * seqid# and there are no byte range locks or other Opens
3414 		 * on the openowner, just throw it away, so the next use of the
3415 		 * openowner will start a fresh seq#.
3416 		 */
3417 		if (error == NFSERR_BADSEQID &&
3418 		    (new_stp->ls_flags & NFSLCK_CONFIRM) &&
3419 		    nfsrv_nootherstate(stp))
3420 			nfsrv_freeopenowner(stp->ls_openowner, 0, p);
3421 		NFSUNLOCKSTATE();
3422 		goto out;
3423 	}
3424 
3425 	/*
3426 	 * Set the return stateid.
3427 	 */
3428 	stateidp->seqid = stp->ls_stateid.seqid + 1;
3429 	if ((nd->nd_flag & ND_NFSV41) != 0 && stateidp->seqid == 0)
3430 		stateidp->seqid = 1;
3431 	stateidp->other[0] = stp->ls_stateid.other[0];
3432 	stateidp->other[1] = stp->ls_stateid.other[1];
3433 	stateidp->other[2] = stp->ls_stateid.other[2];
3434 	/*
3435 	 * Now, handle the three cases.
3436 	 */
3437 	if (new_stp->ls_flags & NFSLCK_CONFIRM) {
3438 		/*
3439 		 * If the open doesn't need confirmation, it seems to me that
3440 		 * there is a client error, but I'll just log it and keep going?
3441 		 */
3442 		if (!(stp->ls_openowner->ls_flags & NFSLCK_NEEDSCONFIRM))
3443 			printf("Nfsv4d: stray open confirm\n");
3444 		stp->ls_openowner->ls_flags = 0;
3445 		stp->ls_stateid.seqid++;
3446 		if ((nd->nd_flag & ND_NFSV41) != 0 &&
3447 		    stp->ls_stateid.seqid == 0)
3448 			stp->ls_stateid.seqid = 1;
3449 		if (!(clp->lc_flags & LCL_STAMPEDSTABLE)) {
3450 			clp->lc_flags |= LCL_STAMPEDSTABLE;
3451 			len = clp->lc_idlen;
3452 			NFSBCOPY(clp->lc_id, clidp, len);
3453 			gotstate = 1;
3454 		}
3455 		NFSUNLOCKSTATE();
3456 	} else if (new_stp->ls_flags & NFSLCK_CLOSE) {
3457 		lfp = stp->ls_lfp;
3458 		if (retwriteaccessp != NULL) {
3459 			if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0)
3460 				*retwriteaccessp = 1;
3461 			else
3462 				*retwriteaccessp = 0;
3463 		}
3464 		if (nfsrv_dolocallocks != 0 && !LIST_EMPTY(&stp->ls_open)) {
3465 			/* Get the lf lock */
3466 			nfsrv_locklf(lfp);
3467 			NFSUNLOCKSTATE();
3468 			ASSERT_VOP_ELOCKED(vp, "nfsrv_openupdate");
3469 			NFSVOPUNLOCK(vp, 0);
3470 			if (nfsrv_freeopen(stp, vp, 1, p) == 0) {
3471 				NFSLOCKSTATE();
3472 				nfsrv_unlocklf(lfp);
3473 				NFSUNLOCKSTATE();
3474 			}
3475 			NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3476 		} else {
3477 			(void) nfsrv_freeopen(stp, NULL, 0, p);
3478 			NFSUNLOCKSTATE();
3479 		}
3480 	} else {
3481 		/*
3482 		 * Update the share bits, making sure that the new set are a
3483 		 * subset of the old ones.
3484 		 */
3485 		bits = (new_stp->ls_flags & NFSLCK_SHAREBITS);
3486 		if (~(stp->ls_flags) & bits) {
3487 			NFSUNLOCKSTATE();
3488 			error = NFSERR_INVAL;
3489 			goto out;
3490 		}
3491 		stp->ls_flags = (bits | NFSLCK_OPEN);
3492 		stp->ls_stateid.seqid++;
3493 		if ((nd->nd_flag & ND_NFSV41) != 0 &&
3494 		    stp->ls_stateid.seqid == 0)
3495 			stp->ls_stateid.seqid = 1;
3496 		NFSUNLOCKSTATE();
3497 	}
3498 
3499 	/*
3500 	 * If the client just confirmed its first open, write a timestamp
3501 	 * to the stable storage file.
3502 	 */
3503 	if (gotstate != 0) {
3504 		nfsrv_writestable(clidp, len, NFSNST_NEWSTATE, p);
3505 		nfsrv_backupstable();
3506 	}
3507 
3508 out:
3509 	free(clidp, M_TEMP);
3510 	NFSEXITCODE2(error, nd);
3511 	return (error);
3512 }
3513 
3514 /*
3515  * Delegation update. Does the purge and return.
3516  */
3517 APPLESTATIC int
3518 nfsrv_delegupdate(struct nfsrv_descript *nd, nfsquad_t clientid,
3519     nfsv4stateid_t *stateidp, vnode_t vp, int op, struct ucred *cred,
3520     NFSPROC_T *p, int *retwriteaccessp)
3521 {
3522 	struct nfsstate *stp;
3523 	struct nfsclient *clp;
3524 	int error = 0;
3525 	fhandle_t fh;
3526 
3527 	/*
3528 	 * Do a sanity check against the file handle for DelegReturn.
3529 	 */
3530 	if (vp) {
3531 		error = nfsvno_getfh(vp, &fh, p);
3532 		if (error)
3533 			goto out;
3534 	}
3535 	/*
3536 	 * Check for restart conditions (client and server).
3537 	 */
3538 	if (op == NFSV4OP_DELEGRETURN)
3539 		error = nfsrv_checkrestart(clientid, NFSLCK_DELEGRETURN,
3540 			stateidp, 0);
3541 	else
3542 		error = nfsrv_checkrestart(clientid, NFSLCK_DELEGPURGE,
3543 			stateidp, 0);
3544 
3545 	NFSLOCKSTATE();
3546 	/*
3547 	 * Get the open structure via clientid and stateid.
3548 	 */
3549 	if (!error)
3550 	    error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3551 		(nfsquad_t)((u_quad_t)0), 0, nd, p);
3552 	if (error) {
3553 		if (error == NFSERR_CBPATHDOWN)
3554 			error = 0;
3555 		if (error == NFSERR_STALECLIENTID && op == NFSV4OP_DELEGRETURN)
3556 			error = NFSERR_STALESTATEID;
3557 	}
3558 	if (!error && op == NFSV4OP_DELEGRETURN) {
3559 	    error = nfsrv_getstate(clp, stateidp, NFSLCK_DELEGRETURN, &stp);
3560 	    if (!error && stp->ls_stateid.seqid != stateidp->seqid &&
3561 		((nd->nd_flag & ND_NFSV41) == 0 || stateidp->seqid != 0))
3562 		error = NFSERR_OLDSTATEID;
3563 	}
3564 	/*
3565 	 * NFSERR_EXPIRED means that the state has gone away,
3566 	 * so Delegations have been purged. Just return ok.
3567 	 */
3568 	if (error == NFSERR_EXPIRED && op == NFSV4OP_DELEGPURGE) {
3569 		NFSUNLOCKSTATE();
3570 		error = 0;
3571 		goto out;
3572 	}
3573 	if (error) {
3574 		NFSUNLOCKSTATE();
3575 		goto out;
3576 	}
3577 
3578 	if (op == NFSV4OP_DELEGRETURN) {
3579 		if (NFSBCMP((caddr_t)&fh, (caddr_t)&stp->ls_lfp->lf_fh,
3580 		    sizeof (fhandle_t))) {
3581 			NFSUNLOCKSTATE();
3582 			error = NFSERR_BADSTATEID;
3583 			goto out;
3584 		}
3585 		if (retwriteaccessp != NULL) {
3586 			if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0)
3587 				*retwriteaccessp = 1;
3588 			else
3589 				*retwriteaccessp = 0;
3590 		}
3591 		nfsrv_freedeleg(stp);
3592 	} else {
3593 		nfsrv_freedeleglist(&clp->lc_olddeleg);
3594 	}
3595 	NFSUNLOCKSTATE();
3596 	error = 0;
3597 
3598 out:
3599 	NFSEXITCODE(error);
3600 	return (error);
3601 }
3602 
3603 /*
3604  * Release lock owner.
3605  */
3606 APPLESTATIC int
3607 nfsrv_releaselckown(struct nfsstate *new_stp, nfsquad_t clientid,
3608     NFSPROC_T *p)
3609 {
3610 	struct nfsstate *stp, *nstp, *openstp, *ownstp;
3611 	struct nfsclient *clp;
3612 	int error = 0;
3613 
3614 	/*
3615 	 * Check for restart conditions (client and server).
3616 	 */
3617 	error = nfsrv_checkrestart(clientid, new_stp->ls_flags,
3618 	    &new_stp->ls_stateid, 0);
3619 	if (error)
3620 		goto out;
3621 
3622 	NFSLOCKSTATE();
3623 	/*
3624 	 * Get the lock owner by name.
3625 	 */
3626 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
3627 	    (nfsquad_t)((u_quad_t)0), 0, NULL, p);
3628 	if (error) {
3629 		NFSUNLOCKSTATE();
3630 		goto out;
3631 	}
3632 	LIST_FOREACH(ownstp, &clp->lc_open, ls_list) {
3633 	    LIST_FOREACH(openstp, &ownstp->ls_open, ls_list) {
3634 		stp = LIST_FIRST(&openstp->ls_open);
3635 		while (stp != LIST_END(&openstp->ls_open)) {
3636 		    nstp = LIST_NEXT(stp, ls_list);
3637 		    /*
3638 		     * If the owner matches, check for locks and
3639 		     * then free or return an error.
3640 		     */
3641 		    if (stp->ls_ownerlen == new_stp->ls_ownerlen &&
3642 			!NFSBCMP(stp->ls_owner, new_stp->ls_owner,
3643 			 stp->ls_ownerlen)){
3644 			if (LIST_EMPTY(&stp->ls_lock)) {
3645 			    nfsrv_freelockowner(stp, NULL, 0, p);
3646 			} else {
3647 			    NFSUNLOCKSTATE();
3648 			    error = NFSERR_LOCKSHELD;
3649 			    goto out;
3650 			}
3651 		    }
3652 		    stp = nstp;
3653 		}
3654 	    }
3655 	}
3656 	NFSUNLOCKSTATE();
3657 
3658 out:
3659 	NFSEXITCODE(error);
3660 	return (error);
3661 }
3662 
3663 /*
3664  * Get the file handle for a lock structure.
3665  */
3666 static int
3667 nfsrv_getlockfh(vnode_t vp, u_short flags, struct nfslockfile *new_lfp,
3668     fhandle_t *nfhp, NFSPROC_T *p)
3669 {
3670 	fhandle_t *fhp = NULL;
3671 	int error;
3672 
3673 	/*
3674 	 * For lock, use the new nfslock structure, otherwise just
3675 	 * a fhandle_t on the stack.
3676 	 */
3677 	if (flags & NFSLCK_OPEN) {
3678 		KASSERT(new_lfp != NULL, ("nfsrv_getlockfh: new_lfp NULL"));
3679 		fhp = &new_lfp->lf_fh;
3680 	} else if (nfhp) {
3681 		fhp = nfhp;
3682 	} else {
3683 		panic("nfsrv_getlockfh");
3684 	}
3685 	error = nfsvno_getfh(vp, fhp, p);
3686 	NFSEXITCODE(error);
3687 	return (error);
3688 }
3689 
3690 /*
3691  * Get an nfs lock structure. Allocate one, as required, and return a
3692  * pointer to it.
3693  * Returns an NFSERR_xxx upon failure or -1 to indicate no current lock.
3694  */
3695 static int
3696 nfsrv_getlockfile(u_short flags, struct nfslockfile **new_lfpp,
3697     struct nfslockfile **lfpp, fhandle_t *nfhp, int lockit)
3698 {
3699 	struct nfslockfile *lfp;
3700 	fhandle_t *fhp = NULL, *tfhp;
3701 	struct nfslockhashhead *hp;
3702 	struct nfslockfile *new_lfp = NULL;
3703 
3704 	/*
3705 	 * For lock, use the new nfslock structure, otherwise just
3706 	 * a fhandle_t on the stack.
3707 	 */
3708 	if (flags & NFSLCK_OPEN) {
3709 		new_lfp = *new_lfpp;
3710 		fhp = &new_lfp->lf_fh;
3711 	} else if (nfhp) {
3712 		fhp = nfhp;
3713 	} else {
3714 		panic("nfsrv_getlockfile");
3715 	}
3716 
3717 	hp = NFSLOCKHASH(fhp);
3718 	LIST_FOREACH(lfp, hp, lf_hash) {
3719 		tfhp = &lfp->lf_fh;
3720 		if (NFSVNO_CMPFH(fhp, tfhp)) {
3721 			if (lockit)
3722 				nfsrv_locklf(lfp);
3723 			*lfpp = lfp;
3724 			return (0);
3725 		}
3726 	}
3727 	if (!(flags & NFSLCK_OPEN))
3728 		return (-1);
3729 
3730 	/*
3731 	 * No match, so chain the new one into the list.
3732 	 */
3733 	LIST_INIT(&new_lfp->lf_open);
3734 	LIST_INIT(&new_lfp->lf_lock);
3735 	LIST_INIT(&new_lfp->lf_deleg);
3736 	LIST_INIT(&new_lfp->lf_locallock);
3737 	LIST_INIT(&new_lfp->lf_rollback);
3738 	new_lfp->lf_locallock_lck.nfslock_usecnt = 0;
3739 	new_lfp->lf_locallock_lck.nfslock_lock = 0;
3740 	new_lfp->lf_usecount = 0;
3741 	LIST_INSERT_HEAD(hp, new_lfp, lf_hash);
3742 	*lfpp = new_lfp;
3743 	*new_lfpp = NULL;
3744 	return (0);
3745 }
3746 
3747 /*
3748  * This function adds a nfslock lock structure to the list for the associated
3749  * nfsstate and nfslockfile structures. It will be inserted after the
3750  * entry pointed at by insert_lop.
3751  */
3752 static void
3753 nfsrv_insertlock(struct nfslock *new_lop, struct nfslock *insert_lop,
3754     struct nfsstate *stp, struct nfslockfile *lfp)
3755 {
3756 	struct nfslock *lop, *nlop;
3757 
3758 	new_lop->lo_stp = stp;
3759 	new_lop->lo_lfp = lfp;
3760 
3761 	if (stp != NULL) {
3762 		/* Insert in increasing lo_first order */
3763 		lop = LIST_FIRST(&lfp->lf_lock);
3764 		if (lop == LIST_END(&lfp->lf_lock) ||
3765 		    new_lop->lo_first <= lop->lo_first) {
3766 			LIST_INSERT_HEAD(&lfp->lf_lock, new_lop, lo_lckfile);
3767 		} else {
3768 			nlop = LIST_NEXT(lop, lo_lckfile);
3769 			while (nlop != LIST_END(&lfp->lf_lock) &&
3770 			       nlop->lo_first < new_lop->lo_first) {
3771 				lop = nlop;
3772 				nlop = LIST_NEXT(lop, lo_lckfile);
3773 			}
3774 			LIST_INSERT_AFTER(lop, new_lop, lo_lckfile);
3775 		}
3776 	} else {
3777 		new_lop->lo_lckfile.le_prev = NULL;	/* list not used */
3778 	}
3779 
3780 	/*
3781 	 * Insert after insert_lop, which is overloaded as stp or lfp for
3782 	 * an empty list.
3783 	 */
3784 	if (stp == NULL && (struct nfslockfile *)insert_lop == lfp)
3785 		LIST_INSERT_HEAD(&lfp->lf_locallock, new_lop, lo_lckowner);
3786 	else if ((struct nfsstate *)insert_lop == stp)
3787 		LIST_INSERT_HEAD(&stp->ls_lock, new_lop, lo_lckowner);
3788 	else
3789 		LIST_INSERT_AFTER(insert_lop, new_lop, lo_lckowner);
3790 	if (stp != NULL) {
3791 		nfsstatsv1.srvlocks++;
3792 		nfsrv_openpluslock++;
3793 	}
3794 }
3795 
3796 /*
3797  * This function updates the locking for a lock owner and given file. It
3798  * maintains a list of lock ranges ordered on increasing file offset that
3799  * are NFSLCK_READ or NFSLCK_WRITE and non-overlapping (aka POSIX style).
3800  * It always adds new_lop to the list and sometimes uses the one pointed
3801  * at by other_lopp.
3802  */
3803 static void
3804 nfsrv_updatelock(struct nfsstate *stp, struct nfslock **new_lopp,
3805     struct nfslock **other_lopp, struct nfslockfile *lfp)
3806 {
3807 	struct nfslock *new_lop = *new_lopp;
3808 	struct nfslock *lop, *tlop, *ilop;
3809 	struct nfslock *other_lop = *other_lopp;
3810 	int unlock = 0, myfile = 0;
3811 	u_int64_t tmp;
3812 
3813 	/*
3814 	 * Work down the list until the lock is merged.
3815 	 */
3816 	if (new_lop->lo_flags & NFSLCK_UNLOCK)
3817 		unlock = 1;
3818 	if (stp != NULL) {
3819 		ilop = (struct nfslock *)stp;
3820 		lop = LIST_FIRST(&stp->ls_lock);
3821 	} else {
3822 		ilop = (struct nfslock *)lfp;
3823 		lop = LIST_FIRST(&lfp->lf_locallock);
3824 	}
3825 	while (lop != NULL) {
3826 	    /*
3827 	     * Only check locks for this file that aren't before the start of
3828 	     * new lock's range.
3829 	     */
3830 	    if (lop->lo_lfp == lfp) {
3831 	      myfile = 1;
3832 	      if (lop->lo_end >= new_lop->lo_first) {
3833 		if (new_lop->lo_end < lop->lo_first) {
3834 			/*
3835 			 * If the new lock ends before the start of the
3836 			 * current lock's range, no merge, just insert
3837 			 * the new lock.
3838 			 */
3839 			break;
3840 		}
3841 		if (new_lop->lo_flags == lop->lo_flags ||
3842 		    (new_lop->lo_first <= lop->lo_first &&
3843 		     new_lop->lo_end >= lop->lo_end)) {
3844 			/*
3845 			 * This lock can be absorbed by the new lock/unlock.
3846 			 * This happens when it covers the entire range
3847 			 * of the old lock or is contiguous
3848 			 * with the old lock and is of the same type or an
3849 			 * unlock.
3850 			 */
3851 			if (lop->lo_first < new_lop->lo_first)
3852 				new_lop->lo_first = lop->lo_first;
3853 			if (lop->lo_end > new_lop->lo_end)
3854 				new_lop->lo_end = lop->lo_end;
3855 			tlop = lop;
3856 			lop = LIST_NEXT(lop, lo_lckowner);
3857 			nfsrv_freenfslock(tlop);
3858 			continue;
3859 		}
3860 
3861 		/*
3862 		 * All these cases are for contiguous locks that are not the
3863 		 * same type, so they can't be merged.
3864 		 */
3865 		if (new_lop->lo_first <= lop->lo_first) {
3866 			/*
3867 			 * This case is where the new lock overlaps with the
3868 			 * first part of the old lock. Move the start of the
3869 			 * old lock to just past the end of the new lock. The
3870 			 * new lock will be inserted in front of the old, since
3871 			 * ilop hasn't been updated. (We are done now.)
3872 			 */
3873 			lop->lo_first = new_lop->lo_end;
3874 			break;
3875 		}
3876 		if (new_lop->lo_end >= lop->lo_end) {
3877 			/*
3878 			 * This case is where the new lock overlaps with the
3879 			 * end of the old lock's range. Move the old lock's
3880 			 * end to just before the new lock's first and insert
3881 			 * the new lock after the old lock.
3882 			 * Might not be done yet, since the new lock could
3883 			 * overlap further locks with higher ranges.
3884 			 */
3885 			lop->lo_end = new_lop->lo_first;
3886 			ilop = lop;
3887 			lop = LIST_NEXT(lop, lo_lckowner);
3888 			continue;
3889 		}
3890 		/*
3891 		 * The final case is where the new lock's range is in the
3892 		 * middle of the current lock's and splits the current lock
3893 		 * up. Use *other_lopp to handle the second part of the
3894 		 * split old lock range. (We are done now.)
3895 		 * For unlock, we use new_lop as other_lop and tmp, since
3896 		 * other_lop and new_lop are the same for this case.
3897 		 * We noted the unlock case above, so we don't need
3898 		 * new_lop->lo_flags any longer.
3899 		 */
3900 		tmp = new_lop->lo_first;
3901 		if (other_lop == NULL) {
3902 			if (!unlock)
3903 				panic("nfsd srv update unlock");
3904 			other_lop = new_lop;
3905 			*new_lopp = NULL;
3906 		}
3907 		other_lop->lo_first = new_lop->lo_end;
3908 		other_lop->lo_end = lop->lo_end;
3909 		other_lop->lo_flags = lop->lo_flags;
3910 		other_lop->lo_stp = stp;
3911 		other_lop->lo_lfp = lfp;
3912 		lop->lo_end = tmp;
3913 		nfsrv_insertlock(other_lop, lop, stp, lfp);
3914 		*other_lopp = NULL;
3915 		ilop = lop;
3916 		break;
3917 	      }
3918 	    }
3919 	    ilop = lop;
3920 	    lop = LIST_NEXT(lop, lo_lckowner);
3921 	    if (myfile && (lop == NULL || lop->lo_lfp != lfp))
3922 		break;
3923 	}
3924 
3925 	/*
3926 	 * Insert the new lock in the list at the appropriate place.
3927 	 */
3928 	if (!unlock) {
3929 		nfsrv_insertlock(new_lop, ilop, stp, lfp);
3930 		*new_lopp = NULL;
3931 	}
3932 }
3933 
3934 /*
3935  * This function handles sequencing of locks, etc.
3936  * It returns an error that indicates what the caller should do.
3937  */
3938 static int
3939 nfsrv_checkseqid(struct nfsrv_descript *nd, u_int32_t seqid,
3940     struct nfsstate *stp, struct nfsrvcache *op)
3941 {
3942 	int error = 0;
3943 
3944 	if ((nd->nd_flag & ND_NFSV41) != 0)
3945 		/* NFSv4.1 ignores the open_seqid and lock_seqid. */
3946 		goto out;
3947 	if (op != nd->nd_rp)
3948 		panic("nfsrvstate checkseqid");
3949 	if (!(op->rc_flag & RC_INPROG))
3950 		panic("nfsrvstate not inprog");
3951 	if (stp->ls_op && stp->ls_op->rc_refcnt <= 0) {
3952 		printf("refcnt=%d\n", stp->ls_op->rc_refcnt);
3953 		panic("nfsrvstate op refcnt");
3954 	}
3955 	if ((stp->ls_seq + 1) == seqid) {
3956 		if (stp->ls_op)
3957 			nfsrvd_derefcache(stp->ls_op);
3958 		stp->ls_op = op;
3959 		nfsrvd_refcache(op);
3960 		stp->ls_seq = seqid;
3961 		goto out;
3962 	} else if (stp->ls_seq == seqid && stp->ls_op &&
3963 		op->rc_xid == stp->ls_op->rc_xid &&
3964 		op->rc_refcnt == 0 &&
3965 		op->rc_reqlen == stp->ls_op->rc_reqlen &&
3966 		op->rc_cksum == stp->ls_op->rc_cksum) {
3967 		if (stp->ls_op->rc_flag & RC_INPROG) {
3968 			error = NFSERR_DONTREPLY;
3969 			goto out;
3970 		}
3971 		nd->nd_rp = stp->ls_op;
3972 		nd->nd_rp->rc_flag |= RC_INPROG;
3973 		nfsrvd_delcache(op);
3974 		error = NFSERR_REPLYFROMCACHE;
3975 		goto out;
3976 	}
3977 	error = NFSERR_BADSEQID;
3978 
3979 out:
3980 	NFSEXITCODE2(error, nd);
3981 	return (error);
3982 }
3983 
3984 /*
3985  * Get the client ip address for callbacks. If the strings can't be parsed,
3986  * just set lc_program to 0 to indicate no callbacks are possible.
3987  * (For cases where the address can't be parsed or is 0.0.0.0.0.0, set
3988  *  the address to the client's transport address. This won't be used
3989  *  for callbacks, but can be printed out by nfsstats for info.)
3990  * Return error if the xdr can't be parsed, 0 otherwise.
3991  */
3992 APPLESTATIC int
3993 nfsrv_getclientipaddr(struct nfsrv_descript *nd, struct nfsclient *clp)
3994 {
3995 	u_int32_t *tl;
3996 	u_char *cp, *cp2;
3997 	int i, j;
3998 	struct sockaddr_in *rad, *sad;
3999 	u_char protocol[5], addr[24];
4000 	int error = 0, cantparse = 0;
4001 	union {
4002 		in_addr_t ival;
4003 		u_char cval[4];
4004 	} ip;
4005 	union {
4006 		in_port_t sval;
4007 		u_char cval[2];
4008 	} port;
4009 
4010 	rad = NFSSOCKADDR(clp->lc_req.nr_nam, struct sockaddr_in *);
4011 	rad->sin_family = AF_INET;
4012 	rad->sin_len = sizeof (struct sockaddr_in);
4013 	rad->sin_addr.s_addr = 0;
4014 	rad->sin_port = 0;
4015 	clp->lc_req.nr_client = NULL;
4016 	clp->lc_req.nr_lock = 0;
4017 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4018 	i = fxdr_unsigned(int, *tl);
4019 	if (i >= 3 && i <= 4) {
4020 		error = nfsrv_mtostr(nd, protocol, i);
4021 		if (error)
4022 			goto nfsmout;
4023 		if (!strcmp(protocol, "tcp")) {
4024 			clp->lc_flags |= LCL_TCPCALLBACK;
4025 			clp->lc_req.nr_sotype = SOCK_STREAM;
4026 			clp->lc_req.nr_soproto = IPPROTO_TCP;
4027 		} else if (!strcmp(protocol, "udp")) {
4028 			clp->lc_req.nr_sotype = SOCK_DGRAM;
4029 			clp->lc_req.nr_soproto = IPPROTO_UDP;
4030 		} else {
4031 			cantparse = 1;
4032 		}
4033 	} else {
4034 		cantparse = 1;
4035 		if (i > 0) {
4036 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4037 			if (error)
4038 				goto nfsmout;
4039 		}
4040 	}
4041 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4042 	i = fxdr_unsigned(int, *tl);
4043 	if (i < 0) {
4044 		error = NFSERR_BADXDR;
4045 		goto nfsmout;
4046 	} else if (i == 0) {
4047 		cantparse = 1;
4048 	} else if (!cantparse && i <= 23 && i >= 11) {
4049 		error = nfsrv_mtostr(nd, addr, i);
4050 		if (error)
4051 			goto nfsmout;
4052 
4053 		/*
4054 		 * Parse out the address fields. We expect 6 decimal numbers
4055 		 * separated by '.'s.
4056 		 */
4057 		cp = addr;
4058 		i = 0;
4059 		while (*cp && i < 6) {
4060 			cp2 = cp;
4061 			while (*cp2 && *cp2 != '.')
4062 				cp2++;
4063 			if (*cp2)
4064 				*cp2++ = '\0';
4065 			else if (i != 5) {
4066 				cantparse = 1;
4067 				break;
4068 			}
4069 			j = nfsrv_getipnumber(cp);
4070 			if (j >= 0) {
4071 				if (i < 4)
4072 					ip.cval[3 - i] = j;
4073 				else
4074 					port.cval[5 - i] = j;
4075 			} else {
4076 				cantparse = 1;
4077 				break;
4078 			}
4079 			cp = cp2;
4080 			i++;
4081 		}
4082 		if (!cantparse) {
4083 			if (ip.ival != 0x0) {
4084 				rad->sin_addr.s_addr = htonl(ip.ival);
4085 				rad->sin_port = htons(port.sval);
4086 			} else {
4087 				cantparse = 1;
4088 			}
4089 		}
4090 	} else {
4091 		cantparse = 1;
4092 		if (i > 0) {
4093 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
4094 			if (error)
4095 				goto nfsmout;
4096 		}
4097 	}
4098 	if (cantparse) {
4099 		sad = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
4100 		if (sad->sin_family == AF_INET) {
4101 			rad->sin_addr.s_addr = sad->sin_addr.s_addr;
4102 			rad->sin_port = 0x0;
4103 		}
4104 		clp->lc_program = 0;
4105 	}
4106 nfsmout:
4107 	NFSEXITCODE2(error, nd);
4108 	return (error);
4109 }
4110 
4111 /*
4112  * Turn a string of up to three decimal digits into a number. Return -1 upon
4113  * error.
4114  */
4115 static int
4116 nfsrv_getipnumber(u_char *cp)
4117 {
4118 	int i = 0, j = 0;
4119 
4120 	while (*cp) {
4121 		if (j > 2 || *cp < '0' || *cp > '9')
4122 			return (-1);
4123 		i *= 10;
4124 		i += (*cp - '0');
4125 		cp++;
4126 		j++;
4127 	}
4128 	if (i < 256)
4129 		return (i);
4130 	return (-1);
4131 }
4132 
4133 /*
4134  * This function checks for restart conditions.
4135  */
4136 static int
4137 nfsrv_checkrestart(nfsquad_t clientid, u_int32_t flags,
4138     nfsv4stateid_t *stateidp, int specialid)
4139 {
4140 	int ret = 0;
4141 
4142 	/*
4143 	 * First check for a server restart. Open, LockT, ReleaseLockOwner
4144 	 * and DelegPurge have a clientid, the rest a stateid.
4145 	 */
4146 	if (flags &
4147 	    (NFSLCK_OPEN | NFSLCK_TEST | NFSLCK_RELEASE | NFSLCK_DELEGPURGE)) {
4148 		if (clientid.lval[0] != nfsrvboottime) {
4149 			ret = NFSERR_STALECLIENTID;
4150 			goto out;
4151 		}
4152 	} else if (stateidp->other[0] != nfsrvboottime &&
4153 		specialid == 0) {
4154 		ret = NFSERR_STALESTATEID;
4155 		goto out;
4156 	}
4157 
4158 	/*
4159 	 * Read, Write, Setattr and LockT can return NFSERR_GRACE and do
4160 	 * not use a lock/open owner seqid#, so the check can be done now.
4161 	 * (The others will be checked, as required, later.)
4162 	 */
4163 	if (!(flags & (NFSLCK_CHECK | NFSLCK_TEST)))
4164 		goto out;
4165 
4166 	NFSLOCKSTATE();
4167 	ret = nfsrv_checkgrace(NULL, NULL, flags);
4168 	NFSUNLOCKSTATE();
4169 
4170 out:
4171 	NFSEXITCODE(ret);
4172 	return (ret);
4173 }
4174 
4175 /*
4176  * Check for grace.
4177  */
4178 static int
4179 nfsrv_checkgrace(struct nfsrv_descript *nd, struct nfsclient *clp,
4180     u_int32_t flags)
4181 {
4182 	int error = 0, notreclaimed;
4183 	struct nfsrv_stable *sp;
4184 
4185 	if ((nfsrv_stablefirst.nsf_flags & (NFSNSF_UPDATEDONE |
4186 	     NFSNSF_GRACEOVER)) == 0) {
4187 		/*
4188 		 * First, check to see if all of the clients have done a
4189 		 * ReclaimComplete.  If so, grace can end now.
4190 		 */
4191 		notreclaimed = 0;
4192 		LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4193 			if ((sp->nst_flag & NFSNST_RECLAIMED) == 0) {
4194 				notreclaimed = 1;
4195 				break;
4196 			}
4197 		}
4198 		if (notreclaimed == 0)
4199 			nfsrv_stablefirst.nsf_flags |= (NFSNSF_GRACEOVER |
4200 			    NFSNSF_NEEDLOCK);
4201 	}
4202 
4203 	if ((nfsrv_stablefirst.nsf_flags & NFSNSF_GRACEOVER) != 0) {
4204 		if (flags & NFSLCK_RECLAIM) {
4205 			error = NFSERR_NOGRACE;
4206 			goto out;
4207 		}
4208 	} else {
4209 		if (!(flags & NFSLCK_RECLAIM)) {
4210 			error = NFSERR_GRACE;
4211 			goto out;
4212 		}
4213 		if (nd != NULL && clp != NULL &&
4214 		    (nd->nd_flag & ND_NFSV41) != 0 &&
4215 		    (clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0) {
4216 			error = NFSERR_NOGRACE;
4217 			goto out;
4218 		}
4219 
4220 		/*
4221 		 * If grace is almost over and we are still getting Reclaims,
4222 		 * extend grace a bit.
4223 		 */
4224 		if ((NFSD_MONOSEC + NFSRV_LEASEDELTA) >
4225 		    nfsrv_stablefirst.nsf_eograce)
4226 			nfsrv_stablefirst.nsf_eograce = NFSD_MONOSEC +
4227 				NFSRV_LEASEDELTA;
4228 	}
4229 
4230 out:
4231 	NFSEXITCODE(error);
4232 	return (error);
4233 }
4234 
4235 /*
4236  * Do a server callback.
4237  */
4238 static int
4239 nfsrv_docallback(struct nfsclient *clp, int procnum, nfsv4stateid_t *stateidp,
4240     int trunc, fhandle_t *fhp, struct nfsvattr *nap, nfsattrbit_t *attrbitp,
4241     int laytype, NFSPROC_T *p)
4242 {
4243 	mbuf_t m;
4244 	u_int32_t *tl;
4245 	struct nfsrv_descript *nd;
4246 	struct ucred *cred;
4247 	int error = 0;
4248 	u_int32_t callback;
4249 	struct nfsdsession *sep = NULL;
4250 	uint64_t tval;
4251 
4252 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4253 	cred = newnfs_getcred();
4254 	NFSLOCKSTATE();	/* mostly for lc_cbref++ */
4255 	if (clp->lc_flags & LCL_NEEDSCONFIRM) {
4256 		NFSUNLOCKSTATE();
4257 		panic("docallb");
4258 	}
4259 	clp->lc_cbref++;
4260 
4261 	/*
4262 	 * Fill the callback program# and version into the request
4263 	 * structure for newnfs_connect() to use.
4264 	 */
4265 	clp->lc_req.nr_prog = clp->lc_program;
4266 #ifdef notnow
4267 	if ((clp->lc_flags & LCL_NFSV41) != 0)
4268 		clp->lc_req.nr_vers = NFSV41_CBVERS;
4269 	else
4270 #endif
4271 		clp->lc_req.nr_vers = NFSV4_CBVERS;
4272 
4273 	/*
4274 	 * First, fill in some of the fields of nd and cr.
4275 	 */
4276 	nd->nd_flag = ND_NFSV4;
4277 	if (clp->lc_flags & LCL_GSS)
4278 		nd->nd_flag |= ND_KERBV;
4279 	if ((clp->lc_flags & LCL_NFSV41) != 0)
4280 		nd->nd_flag |= ND_NFSV41;
4281 	nd->nd_repstat = 0;
4282 	cred->cr_uid = clp->lc_uid;
4283 	cred->cr_gid = clp->lc_gid;
4284 	callback = clp->lc_callback;
4285 	NFSUNLOCKSTATE();
4286 	cred->cr_ngroups = 1;
4287 
4288 	/*
4289 	 * Get the first mbuf for the request.
4290 	 */
4291 	MGET(m, M_WAITOK, MT_DATA);
4292 	mbuf_setlen(m, 0);
4293 	nd->nd_mreq = nd->nd_mb = m;
4294 	nd->nd_bpos = NFSMTOD(m, caddr_t);
4295 
4296 	/*
4297 	 * and build the callback request.
4298 	 */
4299 	if (procnum == NFSV4OP_CBGETATTR) {
4300 		nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4301 		error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBGETATTR,
4302 		    "CB Getattr", &sep);
4303 		if (error != 0) {
4304 			mbuf_freem(nd->nd_mreq);
4305 			goto errout;
4306 		}
4307 		(void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4308 		(void)nfsrv_putattrbit(nd, attrbitp);
4309 	} else if (procnum == NFSV4OP_CBRECALL) {
4310 		nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4311 		error = nfsrv_cbcallargs(nd, clp, callback, NFSV4OP_CBRECALL,
4312 		    "CB Recall", &sep);
4313 		if (error != 0) {
4314 			mbuf_freem(nd->nd_mreq);
4315 			goto errout;
4316 		}
4317 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID);
4318 		*tl++ = txdr_unsigned(stateidp->seqid);
4319 		NFSBCOPY((caddr_t)stateidp->other, (caddr_t)tl,
4320 		    NFSX_STATEIDOTHER);
4321 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4322 		if (trunc)
4323 			*tl = newnfs_true;
4324 		else
4325 			*tl = newnfs_false;
4326 		(void)nfsm_fhtom(nd, (u_int8_t *)fhp, NFSX_MYFH, 0);
4327 	} else if (procnum == NFSV4OP_CBLAYOUTRECALL) {
4328 		NFSD_DEBUG(4, "docallback layout recall\n");
4329 		nd->nd_procnum = NFSV4PROC_CBCOMPOUND;
4330 		error = nfsrv_cbcallargs(nd, clp, callback,
4331 		    NFSV4OP_CBLAYOUTRECALL, "CB Reclayout", &sep);
4332 		NFSD_DEBUG(4, "aft cbcallargs=%d\n", error);
4333 		if (error != 0) {
4334 			mbuf_freem(nd->nd_mreq);
4335 			goto errout;
4336 		}
4337 		NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
4338 		*tl++ = txdr_unsigned(laytype);
4339 		*tl++ = txdr_unsigned(NFSLAYOUTIOMODE_ANY);
4340 		*tl++ = newnfs_true;
4341 		*tl = txdr_unsigned(NFSV4LAYOUTRET_FILE);
4342 		nfsm_fhtom(nd, (uint8_t *)fhp, NFSX_MYFH, 0);
4343 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_HYPER + NFSX_STATEID);
4344 		tval = 0;
4345 		txdr_hyper(tval, tl); tl += 2;
4346 		tval = UINT64_MAX;
4347 		txdr_hyper(tval, tl); tl += 2;
4348 		*tl++ = txdr_unsigned(stateidp->seqid);
4349 		NFSBCOPY(stateidp->other, tl, NFSX_STATEIDOTHER);
4350 		tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
4351 		NFSD_DEBUG(4, "aft args\n");
4352 	} else if (procnum == NFSV4PROC_CBNULL) {
4353 		nd->nd_procnum = NFSV4PROC_CBNULL;
4354 		if ((clp->lc_flags & LCL_NFSV41) != 0) {
4355 			error = nfsv4_getcbsession(clp, &sep);
4356 			if (error != 0) {
4357 				mbuf_freem(nd->nd_mreq);
4358 				goto errout;
4359 			}
4360 		}
4361 	} else {
4362 		error = NFSERR_SERVERFAULT;
4363 		mbuf_freem(nd->nd_mreq);
4364 		goto errout;
4365 	}
4366 
4367 	/*
4368 	 * Call newnfs_connect(), as required, and then newnfs_request().
4369 	 */
4370 	(void) newnfs_sndlock(&clp->lc_req.nr_lock);
4371 	if (clp->lc_req.nr_client == NULL) {
4372 		if ((clp->lc_flags & LCL_NFSV41) != 0) {
4373 			error = ECONNREFUSED;
4374 			nfsrv_freesession(sep, NULL);
4375 		} else if (nd->nd_procnum == NFSV4PROC_CBNULL)
4376 			error = newnfs_connect(NULL, &clp->lc_req, cred,
4377 			    NULL, 1);
4378 		else
4379 			error = newnfs_connect(NULL, &clp->lc_req, cred,
4380 			    NULL, 3);
4381 	}
4382 	newnfs_sndunlock(&clp->lc_req.nr_lock);
4383 	NFSD_DEBUG(4, "aft sndunlock=%d\n", error);
4384 	if (!error) {
4385 		if ((nd->nd_flag & ND_NFSV41) != 0) {
4386 			KASSERT(sep != NULL, ("sep NULL"));
4387 			if (sep->sess_cbsess.nfsess_xprt != NULL)
4388 				error = newnfs_request(nd, NULL, clp,
4389 				    &clp->lc_req, NULL, NULL, cred,
4390 				    clp->lc_program, clp->lc_req.nr_vers, NULL,
4391 				    1, NULL, &sep->sess_cbsess);
4392 			else {
4393 				/*
4394 				 * This should probably never occur, but if a
4395 				 * client somehow does an RPC without a
4396 				 * SequenceID Op that causes a callback just
4397 				 * after the nfsd threads have been terminated
4398 				 * and restared we could conceivably get here
4399 				 * without a backchannel xprt.
4400 				 */
4401 				printf("nfsrv_docallback: no xprt\n");
4402 				error = ECONNREFUSED;
4403 			}
4404 			NFSD_DEBUG(4, "aft newnfs_request=%d\n", error);
4405 			nfsrv_freesession(sep, NULL);
4406 		} else
4407 			error = newnfs_request(nd, NULL, clp, &clp->lc_req,
4408 			    NULL, NULL, cred, clp->lc_program,
4409 			    clp->lc_req.nr_vers, NULL, 1, NULL, NULL);
4410 	}
4411 errout:
4412 	NFSFREECRED(cred);
4413 
4414 	/*
4415 	 * If error is set here, the Callback path isn't working
4416 	 * properly, so twiddle the appropriate LCL_ flags.
4417 	 * (nd_repstat != 0 indicates the Callback path is working,
4418 	 *  but the callback failed on the client.)
4419 	 */
4420 	if (error) {
4421 		/*
4422 		 * Mark the callback pathway down, which disabled issuing
4423 		 * of delegations and gets Renew to return NFSERR_CBPATHDOWN.
4424 		 */
4425 		NFSLOCKSTATE();
4426 		clp->lc_flags |= LCL_CBDOWN;
4427 		NFSUNLOCKSTATE();
4428 	} else {
4429 		/*
4430 		 * Callback worked. If the callback path was down, disable
4431 		 * callbacks, so no more delegations will be issued. (This
4432 		 * is done on the assumption that the callback pathway is
4433 		 * flakey.)
4434 		 */
4435 		NFSLOCKSTATE();
4436 		if (clp->lc_flags & LCL_CBDOWN)
4437 			clp->lc_flags &= ~(LCL_CBDOWN | LCL_CALLBACKSON);
4438 		NFSUNLOCKSTATE();
4439 		if (nd->nd_repstat) {
4440 			error = nd->nd_repstat;
4441 			NFSD_DEBUG(1, "nfsrv_docallback op=%d err=%d\n",
4442 			    procnum, error);
4443 		} else if (error == 0 && procnum == NFSV4OP_CBGETATTR)
4444 			error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
4445 			    NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
4446 			    p, NULL);
4447 		mbuf_freem(nd->nd_mrep);
4448 	}
4449 	NFSLOCKSTATE();
4450 	clp->lc_cbref--;
4451 	if ((clp->lc_flags & LCL_WAKEUPWANTED) && clp->lc_cbref == 0) {
4452 		clp->lc_flags &= ~LCL_WAKEUPWANTED;
4453 		wakeup(clp);
4454 	}
4455 	NFSUNLOCKSTATE();
4456 
4457 	free(nd, M_TEMP);
4458 	NFSEXITCODE(error);
4459 	return (error);
4460 }
4461 
4462 /*
4463  * Set up the compound RPC for the callback.
4464  */
4465 static int
4466 nfsrv_cbcallargs(struct nfsrv_descript *nd, struct nfsclient *clp,
4467     uint32_t callback, int op, const char *optag, struct nfsdsession **sepp)
4468 {
4469 	uint32_t *tl;
4470 	int error, len;
4471 
4472 	len = strlen(optag);
4473 	(void)nfsm_strtom(nd, optag, len);
4474 	NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED);
4475 	if ((nd->nd_flag & ND_NFSV41) != 0) {
4476 		*tl++ = txdr_unsigned(NFSV41_MINORVERSION);
4477 		*tl++ = txdr_unsigned(callback);
4478 		*tl++ = txdr_unsigned(2);
4479 		*tl = txdr_unsigned(NFSV4OP_CBSEQUENCE);
4480 		error = nfsv4_setcbsequence(nd, clp, 1, sepp);
4481 		if (error != 0)
4482 			return (error);
4483 		NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4484 		*tl = txdr_unsigned(op);
4485 	} else {
4486 		*tl++ = txdr_unsigned(NFSV4_MINORVERSION);
4487 		*tl++ = txdr_unsigned(callback);
4488 		*tl++ = txdr_unsigned(1);
4489 		*tl = txdr_unsigned(op);
4490 	}
4491 	return (0);
4492 }
4493 
4494 /*
4495  * Return the next index# for a clientid. Mostly just increment and return
4496  * the next one, but... if the 32bit unsigned does actually wrap around,
4497  * it should be rebooted.
4498  * At an average rate of one new client per second, it will wrap around in
4499  * approximately 136 years. (I think the server will have been shut
4500  * down or rebooted before then.)
4501  */
4502 static u_int32_t
4503 nfsrv_nextclientindex(void)
4504 {
4505 	static u_int32_t client_index = 0;
4506 
4507 	client_index++;
4508 	if (client_index != 0)
4509 		return (client_index);
4510 
4511 	printf("%s: out of clientids\n", __func__);
4512 	return (client_index);
4513 }
4514 
4515 /*
4516  * Return the next index# for a stateid. Mostly just increment and return
4517  * the next one, but... if the 32bit unsigned does actually wrap around
4518  * (will a BSD server stay up that long?), find
4519  * new start and end values.
4520  */
4521 static u_int32_t
4522 nfsrv_nextstateindex(struct nfsclient *clp)
4523 {
4524 	struct nfsstate *stp;
4525 	int i;
4526 	u_int32_t canuse, min_index, max_index;
4527 
4528 	if (!(clp->lc_flags & LCL_INDEXNOTOK)) {
4529 		clp->lc_stateindex++;
4530 		if (clp->lc_stateindex != clp->lc_statemaxindex)
4531 			return (clp->lc_stateindex);
4532 	}
4533 
4534 	/*
4535 	 * Yuck, we've hit the end.
4536 	 * Look for a new min and max.
4537 	 */
4538 	min_index = 0;
4539 	max_index = 0xffffffff;
4540 	for (i = 0; i < nfsrv_statehashsize; i++) {
4541 	    LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4542 		if (stp->ls_stateid.other[2] > 0x80000000) {
4543 		    if (stp->ls_stateid.other[2] < max_index)
4544 			max_index = stp->ls_stateid.other[2];
4545 		} else {
4546 		    if (stp->ls_stateid.other[2] > min_index)
4547 			min_index = stp->ls_stateid.other[2];
4548 		}
4549 	    }
4550 	}
4551 
4552 	/*
4553 	 * Yikes, highly unlikely, but I'll handle it anyhow.
4554 	 */
4555 	if (min_index == 0x80000000 && max_index == 0x80000001) {
4556 	    canuse = 0;
4557 	    /*
4558 	     * Loop around until we find an unused entry. Return that
4559 	     * and set LCL_INDEXNOTOK, so the search will continue next time.
4560 	     * (This is one of those rare cases where a goto is the
4561 	     *  cleanest way to code the loop.)
4562 	     */
4563 tryagain:
4564 	    for (i = 0; i < nfsrv_statehashsize; i++) {
4565 		LIST_FOREACH(stp, &clp->lc_stateid[i], ls_hash) {
4566 		    if (stp->ls_stateid.other[2] == canuse) {
4567 			canuse++;
4568 			goto tryagain;
4569 		    }
4570 		}
4571 	    }
4572 	    clp->lc_flags |= LCL_INDEXNOTOK;
4573 	    return (canuse);
4574 	}
4575 
4576 	/*
4577 	 * Ok to start again from min + 1.
4578 	 */
4579 	clp->lc_stateindex = min_index + 1;
4580 	clp->lc_statemaxindex = max_index;
4581 	clp->lc_flags &= ~LCL_INDEXNOTOK;
4582 	return (clp->lc_stateindex);
4583 }
4584 
4585 /*
4586  * The following functions handle the stable storage file that deals with
4587  * the edge conditions described in RFC3530 Sec. 8.6.3.
4588  * The file is as follows:
4589  * - a single record at the beginning that has the lease time of the
4590  *   previous server instance (before the last reboot) and the nfsrvboottime
4591  *   values for the previous server boots.
4592  *   These previous boot times are used to ensure that the current
4593  *   nfsrvboottime does not, somehow, get set to a previous one.
4594  *   (This is important so that Stale ClientIDs and StateIDs can
4595  *    be recognized.)
4596  *   The number of previous nfsvrboottime values precedes the list.
4597  * - followed by some number of appended records with:
4598  *   - client id string
4599  *   - flag that indicates it is a record revoking state via lease
4600  *     expiration or similar
4601  *     OR has successfully acquired state.
4602  * These structures vary in length, with the client string at the end, up
4603  * to NFSV4_OPAQUELIMIT in size.
4604  *
4605  * At the end of the grace period, the file is truncated, the first
4606  * record is rewritten with updated information and any acquired state
4607  * records for successful reclaims of state are written.
4608  *
4609  * Subsequent records are appended when the first state is issued to
4610  * a client and when state is revoked for a client.
4611  *
4612  * When reading the file in, state issued records that come later in
4613  * the file override older ones, since the append log is in cronological order.
4614  * If, for some reason, the file can't be read, the grace period is
4615  * immediately terminated and all reclaims get NFSERR_NOGRACE.
4616  */
4617 
4618 /*
4619  * Read in the stable storage file. Called by nfssvc() before the nfsd
4620  * processes start servicing requests.
4621  */
4622 APPLESTATIC void
4623 nfsrv_setupstable(NFSPROC_T *p)
4624 {
4625 	struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4626 	struct nfsrv_stable *sp, *nsp;
4627 	struct nfst_rec *tsp;
4628 	int error, i, tryagain;
4629 	off_t off = 0;
4630 	ssize_t aresid, len;
4631 
4632 	/*
4633 	 * If NFSNSF_UPDATEDONE is set, this is a restart of the nfsds without
4634 	 * a reboot, so state has not been lost.
4635 	 */
4636 	if (sf->nsf_flags & NFSNSF_UPDATEDONE)
4637 		return;
4638 	/*
4639 	 * Set Grace over just until the file reads successfully.
4640 	 */
4641 	nfsrvboottime = time_second;
4642 	LIST_INIT(&sf->nsf_head);
4643 	sf->nsf_flags = (NFSNSF_GRACEOVER | NFSNSF_NEEDLOCK);
4644 	sf->nsf_eograce = NFSD_MONOSEC + NFSRV_LEASEDELTA;
4645 	if (sf->nsf_fp == NULL)
4646 		return;
4647 	error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4648 	    (caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), off, UIO_SYSSPACE,
4649 	    0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4650 	if (error || aresid || sf->nsf_numboots == 0 ||
4651 		sf->nsf_numboots > NFSNSF_MAXNUMBOOTS)
4652 		return;
4653 
4654 	/*
4655 	 * Now, read in the boottimes.
4656 	 */
4657 	sf->nsf_bootvals = (time_t *)malloc((sf->nsf_numboots + 1) *
4658 		sizeof (time_t), M_TEMP, M_WAITOK);
4659 	off = sizeof (struct nfsf_rec);
4660 	error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4661 	    (caddr_t)sf->nsf_bootvals, sf->nsf_numboots * sizeof (time_t), off,
4662 	    UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4663 	if (error || aresid) {
4664 		free(sf->nsf_bootvals, M_TEMP);
4665 		sf->nsf_bootvals = NULL;
4666 		return;
4667 	}
4668 
4669 	/*
4670 	 * Make sure this nfsrvboottime is different from all recorded
4671 	 * previous ones.
4672 	 */
4673 	do {
4674 		tryagain = 0;
4675 		for (i = 0; i < sf->nsf_numboots; i++) {
4676 			if (nfsrvboottime == sf->nsf_bootvals[i]) {
4677 				nfsrvboottime++;
4678 				tryagain = 1;
4679 				break;
4680 			}
4681 		}
4682 	} while (tryagain);
4683 
4684 	sf->nsf_flags |= NFSNSF_OK;
4685 	off += (sf->nsf_numboots * sizeof (time_t));
4686 
4687 	/*
4688 	 * Read through the file, building a list of records for grace
4689 	 * checking.
4690 	 * Each record is between sizeof (struct nfst_rec) and
4691 	 * sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1
4692 	 * and is actually sizeof (struct nfst_rec) + nst_len - 1.
4693 	 */
4694 	tsp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4695 		NFSV4_OPAQUELIMIT - 1, M_TEMP, M_WAITOK);
4696 	do {
4697 	    error = NFSD_RDWR(UIO_READ, NFSFPVNODE(sf->nsf_fp),
4698 	        (caddr_t)tsp, sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1,
4699 	        off, UIO_SYSSPACE, 0, NFSFPCRED(sf->nsf_fp), &aresid, p);
4700 	    len = (sizeof (struct nfst_rec) + NFSV4_OPAQUELIMIT - 1) - aresid;
4701 	    if (error || (len > 0 && (len < sizeof (struct nfst_rec) ||
4702 		len < (sizeof (struct nfst_rec) + tsp->len - 1)))) {
4703 		/*
4704 		 * Yuck, the file has been corrupted, so just return
4705 		 * after clearing out any restart state, so the grace period
4706 		 * is over.
4707 		 */
4708 		LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4709 			LIST_REMOVE(sp, nst_list);
4710 			free(sp, M_TEMP);
4711 		}
4712 		free(tsp, M_TEMP);
4713 		sf->nsf_flags &= ~NFSNSF_OK;
4714 		free(sf->nsf_bootvals, M_TEMP);
4715 		sf->nsf_bootvals = NULL;
4716 		return;
4717 	    }
4718 	    if (len > 0) {
4719 		off += sizeof (struct nfst_rec) + tsp->len - 1;
4720 		/*
4721 		 * Search the list for a matching client.
4722 		 */
4723 		LIST_FOREACH(sp, &sf->nsf_head, nst_list) {
4724 			if (tsp->len == sp->nst_len &&
4725 			    !NFSBCMP(tsp->client, sp->nst_client, tsp->len))
4726 				break;
4727 		}
4728 		if (sp == LIST_END(&sf->nsf_head)) {
4729 			sp = (struct nfsrv_stable *)malloc(tsp->len +
4730 				sizeof (struct nfsrv_stable) - 1, M_TEMP,
4731 				M_WAITOK);
4732 			NFSBCOPY((caddr_t)tsp, (caddr_t)&sp->nst_rec,
4733 				sizeof (struct nfst_rec) + tsp->len - 1);
4734 			LIST_INSERT_HEAD(&sf->nsf_head, sp, nst_list);
4735 		} else {
4736 			if (tsp->flag == NFSNST_REVOKE)
4737 				sp->nst_flag |= NFSNST_REVOKE;
4738 			else
4739 				/*
4740 				 * A subsequent timestamp indicates the client
4741 				 * did a setclientid/confirm and any previous
4742 				 * revoke is no longer relevant.
4743 				 */
4744 				sp->nst_flag &= ~NFSNST_REVOKE;
4745 		}
4746 	    }
4747 	} while (len > 0);
4748 	free(tsp, M_TEMP);
4749 	sf->nsf_flags = NFSNSF_OK;
4750 	sf->nsf_eograce = NFSD_MONOSEC + sf->nsf_lease +
4751 		NFSRV_LEASEDELTA;
4752 }
4753 
4754 /*
4755  * Update the stable storage file, now that the grace period is over.
4756  */
4757 APPLESTATIC void
4758 nfsrv_updatestable(NFSPROC_T *p)
4759 {
4760 	struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4761 	struct nfsrv_stable *sp, *nsp;
4762 	int i;
4763 	struct nfsvattr nva;
4764 	vnode_t vp;
4765 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 500000)
4766 	mount_t mp = NULL;
4767 #endif
4768 	int error;
4769 
4770 	if (sf->nsf_fp == NULL || (sf->nsf_flags & NFSNSF_UPDATEDONE))
4771 		return;
4772 	sf->nsf_flags |= NFSNSF_UPDATEDONE;
4773 	/*
4774 	 * Ok, we need to rewrite the stable storage file.
4775 	 * - truncate to 0 length
4776 	 * - write the new first structure
4777 	 * - loop through the data structures, writing out any that
4778 	 *   have timestamps older than the old boot
4779 	 */
4780 	if (sf->nsf_bootvals) {
4781 		sf->nsf_numboots++;
4782 		for (i = sf->nsf_numboots - 2; i >= 0; i--)
4783 			sf->nsf_bootvals[i + 1] = sf->nsf_bootvals[i];
4784 	} else {
4785 		sf->nsf_numboots = 1;
4786 		sf->nsf_bootvals = (time_t *)malloc(sizeof (time_t),
4787 			M_TEMP, M_WAITOK);
4788 	}
4789 	sf->nsf_bootvals[0] = nfsrvboottime;
4790 	sf->nsf_lease = nfsrv_lease;
4791 	NFSVNO_ATTRINIT(&nva);
4792 	NFSVNO_SETATTRVAL(&nva, size, 0);
4793 	vp = NFSFPVNODE(sf->nsf_fp);
4794 	vn_start_write(vp, &mp, V_WAIT);
4795 	if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
4796 		error = nfsvno_setattr(vp, &nva, NFSFPCRED(sf->nsf_fp), p,
4797 		    NULL);
4798 		NFSVOPUNLOCK(vp, 0);
4799 	} else
4800 		error = EPERM;
4801 	vn_finished_write(mp);
4802 	if (!error)
4803 	    error = NFSD_RDWR(UIO_WRITE, vp,
4804 		(caddr_t)&sf->nsf_rec, sizeof (struct nfsf_rec), (off_t)0,
4805 		UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4806 	if (!error)
4807 	    error = NFSD_RDWR(UIO_WRITE, vp,
4808 		(caddr_t)sf->nsf_bootvals,
4809 		sf->nsf_numboots * sizeof (time_t),
4810 		(off_t)(sizeof (struct nfsf_rec)),
4811 		UIO_SYSSPACE, IO_SYNC, NFSFPCRED(sf->nsf_fp), NULL, p);
4812 	free(sf->nsf_bootvals, M_TEMP);
4813 	sf->nsf_bootvals = NULL;
4814 	if (error) {
4815 		sf->nsf_flags &= ~NFSNSF_OK;
4816 		printf("EEK! Can't write NfsV4 stable storage file\n");
4817 		return;
4818 	}
4819 	sf->nsf_flags |= NFSNSF_OK;
4820 
4821 	/*
4822 	 * Loop through the list and write out timestamp records for
4823 	 * any clients that successfully reclaimed state.
4824 	 */
4825 	LIST_FOREACH_SAFE(sp, &sf->nsf_head, nst_list, nsp) {
4826 		if (sp->nst_flag & NFSNST_GOTSTATE) {
4827 			nfsrv_writestable(sp->nst_client, sp->nst_len,
4828 				NFSNST_NEWSTATE, p);
4829 			sp->nst_clp->lc_flags |= LCL_STAMPEDSTABLE;
4830 		}
4831 		LIST_REMOVE(sp, nst_list);
4832 		free(sp, M_TEMP);
4833 	}
4834 	nfsrv_backupstable();
4835 }
4836 
4837 /*
4838  * Append a record to the stable storage file.
4839  */
4840 APPLESTATIC void
4841 nfsrv_writestable(u_char *client, int len, int flag, NFSPROC_T *p)
4842 {
4843 	struct nfsrv_stablefirst *sf = &nfsrv_stablefirst;
4844 	struct nfst_rec *sp;
4845 	int error;
4846 
4847 	if (!(sf->nsf_flags & NFSNSF_OK) || sf->nsf_fp == NULL)
4848 		return;
4849 	sp = (struct nfst_rec *)malloc(sizeof (struct nfst_rec) +
4850 		len - 1, M_TEMP, M_WAITOK);
4851 	sp->len = len;
4852 	NFSBCOPY(client, sp->client, len);
4853 	sp->flag = flag;
4854 	error = NFSD_RDWR(UIO_WRITE, NFSFPVNODE(sf->nsf_fp),
4855 	    (caddr_t)sp, sizeof (struct nfst_rec) + len - 1, (off_t)0,
4856 	    UIO_SYSSPACE, (IO_SYNC | IO_APPEND), NFSFPCRED(sf->nsf_fp), NULL, p);
4857 	free(sp, M_TEMP);
4858 	if (error) {
4859 		sf->nsf_flags &= ~NFSNSF_OK;
4860 		printf("EEK! Can't write NfsV4 stable storage file\n");
4861 	}
4862 }
4863 
4864 /*
4865  * This function is called during the grace period to mark a client
4866  * that successfully reclaimed state.
4867  */
4868 static void
4869 nfsrv_markstable(struct nfsclient *clp)
4870 {
4871 	struct nfsrv_stable *sp;
4872 
4873 	/*
4874 	 * First find the client structure.
4875 	 */
4876 	LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4877 		if (sp->nst_len == clp->lc_idlen &&
4878 		    !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4879 			break;
4880 	}
4881 	if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4882 		return;
4883 
4884 	/*
4885 	 * Now, just mark it and set the nfsclient back pointer.
4886 	 */
4887 	sp->nst_flag |= NFSNST_GOTSTATE;
4888 	sp->nst_clp = clp;
4889 }
4890 
4891 /*
4892  * This function is called when a NFSv4.1 client does a ReclaimComplete.
4893  * Very similar to nfsrv_markstable(), except for the flag being set.
4894  */
4895 static void
4896 nfsrv_markreclaim(struct nfsclient *clp)
4897 {
4898 	struct nfsrv_stable *sp;
4899 
4900 	/*
4901 	 * First find the client structure.
4902 	 */
4903 	LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4904 		if (sp->nst_len == clp->lc_idlen &&
4905 		    !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4906 			break;
4907 	}
4908 	if (sp == LIST_END(&nfsrv_stablefirst.nsf_head))
4909 		return;
4910 
4911 	/*
4912 	 * Now, just set the flag.
4913 	 */
4914 	sp->nst_flag |= NFSNST_RECLAIMED;
4915 }
4916 
4917 /*
4918  * This function is called for a reclaim, to see if it gets grace.
4919  * It returns 0 if a reclaim is allowed, 1 otherwise.
4920  */
4921 static int
4922 nfsrv_checkstable(struct nfsclient *clp)
4923 {
4924 	struct nfsrv_stable *sp;
4925 
4926 	/*
4927 	 * First, find the entry for the client.
4928 	 */
4929 	LIST_FOREACH(sp, &nfsrv_stablefirst.nsf_head, nst_list) {
4930 		if (sp->nst_len == clp->lc_idlen &&
4931 		    !NFSBCMP(sp->nst_client, clp->lc_id, sp->nst_len))
4932 			break;
4933 	}
4934 
4935 	/*
4936 	 * If not in the list, state was revoked or no state was issued
4937 	 * since the previous reboot, a reclaim is denied.
4938 	 */
4939 	if (sp == LIST_END(&nfsrv_stablefirst.nsf_head) ||
4940 	    (sp->nst_flag & NFSNST_REVOKE) ||
4941 	    !(nfsrv_stablefirst.nsf_flags & NFSNSF_OK))
4942 		return (1);
4943 	return (0);
4944 }
4945 
4946 /*
4947  * Test for and try to clear out a conflicting client. This is called by
4948  * nfsrv_lockctrl() and nfsrv_openctrl() when conflicts with other clients
4949  * a found.
4950  * The trick here is that it can't revoke a conflicting client with an
4951  * expired lease unless it holds the v4root lock, so...
4952  * If no v4root lock, get the lock and return 1 to indicate "try again".
4953  * Return 0 to indicate the conflict can't be revoked and 1 to indicate
4954  * the revocation worked and the conflicting client is "bye, bye", so it
4955  * can be tried again.
4956  * Return 2 to indicate that the vnode is VI_DOOMED after NFSVOPLOCK().
4957  * Unlocks State before a non-zero value is returned.
4958  */
4959 static int
4960 nfsrv_clientconflict(struct nfsclient *clp, int *haslockp, vnode_t vp,
4961     NFSPROC_T *p)
4962 {
4963 	int gotlock, lktype = 0;
4964 
4965 	/*
4966 	 * If lease hasn't expired, we can't fix it.
4967 	 */
4968 	if (clp->lc_expiry >= NFSD_MONOSEC ||
4969 	    !(nfsrv_stablefirst.nsf_flags & NFSNSF_UPDATEDONE))
4970 		return (0);
4971 	if (*haslockp == 0) {
4972 		NFSUNLOCKSTATE();
4973 		if (vp != NULL) {
4974 			lktype = NFSVOPISLOCKED(vp);
4975 			NFSVOPUNLOCK(vp, 0);
4976 		}
4977 		NFSLOCKV4ROOTMUTEX();
4978 		nfsv4_relref(&nfsv4rootfs_lock);
4979 		do {
4980 			gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
4981 			    NFSV4ROOTLOCKMUTEXPTR, NULL);
4982 		} while (!gotlock);
4983 		NFSUNLOCKV4ROOTMUTEX();
4984 		*haslockp = 1;
4985 		if (vp != NULL) {
4986 			NFSVOPLOCK(vp, lktype | LK_RETRY);
4987 			if ((vp->v_iflag & VI_DOOMED) != 0)
4988 				return (2);
4989 		}
4990 		return (1);
4991 	}
4992 	NFSUNLOCKSTATE();
4993 
4994 	/*
4995 	 * Ok, we can expire the conflicting client.
4996 	 */
4997 	nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
4998 	nfsrv_backupstable();
4999 	nfsrv_cleanclient(clp, p);
5000 	nfsrv_freedeleglist(&clp->lc_deleg);
5001 	nfsrv_freedeleglist(&clp->lc_olddeleg);
5002 	LIST_REMOVE(clp, lc_hash);
5003 	nfsrv_zapclient(clp, p);
5004 	return (1);
5005 }
5006 
5007 /*
5008  * Resolve a delegation conflict.
5009  * Returns 0 to indicate the conflict was resolved without sleeping.
5010  * Return -1 to indicate that the caller should check for conflicts again.
5011  * Return > 0 for an error that should be returned, normally NFSERR_DELAY.
5012  *
5013  * Also, manipulate the nfsv4root_lock, as required. It isn't changed
5014  * for a return of 0, since there was no sleep and it could be required
5015  * later. It is released for a return of NFSERR_DELAY, since the caller
5016  * will return that error. It is released when a sleep was done waiting
5017  * for the delegation to be returned or expire (so that other nfsds can
5018  * handle ops). Then, it must be acquired for the write to stable storage.
5019  * (This function is somewhat similar to nfsrv_clientconflict(), but
5020  *  the semantics differ in a couple of subtle ways. The return of 0
5021  *  indicates the conflict was resolved without sleeping here, not
5022  *  that the conflict can't be resolved and the handling of nfsv4root_lock
5023  *  differs, as noted above.)
5024  * Unlocks State before returning a non-zero value.
5025  */
5026 static int
5027 nfsrv_delegconflict(struct nfsstate *stp, int *haslockp, NFSPROC_T *p,
5028     vnode_t vp)
5029 {
5030 	struct nfsclient *clp = stp->ls_clp;
5031 	int gotlock, error, lktype = 0, retrycnt, zapped_clp;
5032 	nfsv4stateid_t tstateid;
5033 	fhandle_t tfh;
5034 
5035 	/*
5036 	 * If the conflict is with an old delegation...
5037 	 */
5038 	if (stp->ls_flags & NFSLCK_OLDDELEG) {
5039 		/*
5040 		 * You can delete it, if it has expired.
5041 		 */
5042 		if (clp->lc_delegtime < NFSD_MONOSEC) {
5043 			nfsrv_freedeleg(stp);
5044 			NFSUNLOCKSTATE();
5045 			error = -1;
5046 			goto out;
5047 		}
5048 		NFSUNLOCKSTATE();
5049 		/*
5050 		 * During this delay, the old delegation could expire or it
5051 		 * could be recovered by the client via an Open with
5052 		 * CLAIM_DELEGATE_PREV.
5053 		 * Release the nfsv4root_lock, if held.
5054 		 */
5055 		if (*haslockp) {
5056 			*haslockp = 0;
5057 			NFSLOCKV4ROOTMUTEX();
5058 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5059 			NFSUNLOCKV4ROOTMUTEX();
5060 		}
5061 		error = NFSERR_DELAY;
5062 		goto out;
5063 	}
5064 
5065 	/*
5066 	 * It's a current delegation, so:
5067 	 * - check to see if the delegation has expired
5068 	 *   - if so, get the v4root lock and then expire it
5069 	 */
5070 	if (!(stp->ls_flags & NFSLCK_DELEGRECALL)) {
5071 		/*
5072 		 * - do a recall callback, since not yet done
5073 		 * For now, never allow truncate to be set. To use
5074 		 * truncate safely, it must be guaranteed that the
5075 		 * Remove, Rename or Setattr with size of 0 will
5076 		 * succeed and that would require major changes to
5077 		 * the VFS/Vnode OPs.
5078 		 * Set the expiry time large enough so that it won't expire
5079 		 * until after the callback, then set it correctly, once
5080 		 * the callback is done. (The delegation will now time
5081 		 * out whether or not the Recall worked ok. The timeout
5082 		 * will be extended when ops are done on the delegation
5083 		 * stateid, up to the timelimit.)
5084 		 */
5085 		stp->ls_delegtime = NFSD_MONOSEC + (2 * nfsrv_lease) +
5086 		    NFSRV_LEASEDELTA;
5087 		stp->ls_delegtimelimit = NFSD_MONOSEC + (6 * nfsrv_lease) +
5088 		    NFSRV_LEASEDELTA;
5089 		stp->ls_flags |= NFSLCK_DELEGRECALL;
5090 
5091 		/*
5092 		 * Loop NFSRV_CBRETRYCNT times while the CBRecall replies
5093 		 * NFSERR_BADSTATEID or NFSERR_BADHANDLE. This is done
5094 		 * in order to try and avoid a race that could happen
5095 		 * when a CBRecall request passed the Open reply with
5096 		 * the delegation in it when transitting the network.
5097 		 * Since nfsrv_docallback will sleep, don't use stp after
5098 		 * the call.
5099 		 */
5100 		NFSBCOPY((caddr_t)&stp->ls_stateid, (caddr_t)&tstateid,
5101 		    sizeof (tstateid));
5102 		NFSBCOPY((caddr_t)&stp->ls_lfp->lf_fh, (caddr_t)&tfh,
5103 		    sizeof (tfh));
5104 		NFSUNLOCKSTATE();
5105 		if (*haslockp) {
5106 			*haslockp = 0;
5107 			NFSLOCKV4ROOTMUTEX();
5108 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5109 			NFSUNLOCKV4ROOTMUTEX();
5110 		}
5111 		retrycnt = 0;
5112 		do {
5113 		    error = nfsrv_docallback(clp, NFSV4OP_CBRECALL,
5114 			&tstateid, 0, &tfh, NULL, NULL, 0, p);
5115 		    retrycnt++;
5116 		} while ((error == NFSERR_BADSTATEID ||
5117 		    error == NFSERR_BADHANDLE) && retrycnt < NFSV4_CBRETRYCNT);
5118 		error = NFSERR_DELAY;
5119 		goto out;
5120 	}
5121 
5122 	if (clp->lc_expiry >= NFSD_MONOSEC &&
5123 	    stp->ls_delegtime >= NFSD_MONOSEC) {
5124 		NFSUNLOCKSTATE();
5125 		/*
5126 		 * A recall has been done, but it has not yet expired.
5127 		 * So, RETURN_DELAY.
5128 		 */
5129 		if (*haslockp) {
5130 			*haslockp = 0;
5131 			NFSLOCKV4ROOTMUTEX();
5132 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5133 			NFSUNLOCKV4ROOTMUTEX();
5134 		}
5135 		error = NFSERR_DELAY;
5136 		goto out;
5137 	}
5138 
5139 	/*
5140 	 * If we don't yet have the lock, just get it and then return,
5141 	 * since we need that before deleting expired state, such as
5142 	 * this delegation.
5143 	 * When getting the lock, unlock the vnode, so other nfsds that
5144 	 * are in progress, won't get stuck waiting for the vnode lock.
5145 	 */
5146 	if (*haslockp == 0) {
5147 		NFSUNLOCKSTATE();
5148 		if (vp != NULL) {
5149 			lktype = NFSVOPISLOCKED(vp);
5150 			NFSVOPUNLOCK(vp, 0);
5151 		}
5152 		NFSLOCKV4ROOTMUTEX();
5153 		nfsv4_relref(&nfsv4rootfs_lock);
5154 		do {
5155 			gotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
5156 			    NFSV4ROOTLOCKMUTEXPTR, NULL);
5157 		} while (!gotlock);
5158 		NFSUNLOCKV4ROOTMUTEX();
5159 		*haslockp = 1;
5160 		if (vp != NULL) {
5161 			NFSVOPLOCK(vp, lktype | LK_RETRY);
5162 			if ((vp->v_iflag & VI_DOOMED) != 0) {
5163 				*haslockp = 0;
5164 				NFSLOCKV4ROOTMUTEX();
5165 				nfsv4_unlock(&nfsv4rootfs_lock, 1);
5166 				NFSUNLOCKV4ROOTMUTEX();
5167 				error = NFSERR_PERM;
5168 				goto out;
5169 			}
5170 		}
5171 		error = -1;
5172 		goto out;
5173 	}
5174 
5175 	NFSUNLOCKSTATE();
5176 	/*
5177 	 * Ok, we can delete the expired delegation.
5178 	 * First, write the Revoke record to stable storage and then
5179 	 * clear out the conflict.
5180 	 * Since all other nfsd threads are now blocked, we can safely
5181 	 * sleep without the state changing.
5182 	 */
5183 	nfsrv_writestable(clp->lc_id, clp->lc_idlen, NFSNST_REVOKE, p);
5184 	nfsrv_backupstable();
5185 	if (clp->lc_expiry < NFSD_MONOSEC) {
5186 		nfsrv_cleanclient(clp, p);
5187 		nfsrv_freedeleglist(&clp->lc_deleg);
5188 		nfsrv_freedeleglist(&clp->lc_olddeleg);
5189 		LIST_REMOVE(clp, lc_hash);
5190 		zapped_clp = 1;
5191 	} else {
5192 		nfsrv_freedeleg(stp);
5193 		zapped_clp = 0;
5194 	}
5195 	if (zapped_clp)
5196 		nfsrv_zapclient(clp, p);
5197 	error = -1;
5198 
5199 out:
5200 	NFSEXITCODE(error);
5201 	return (error);
5202 }
5203 
5204 /*
5205  * Check for a remove allowed, if remove is set to 1 and get rid of
5206  * delegations.
5207  */
5208 APPLESTATIC int
5209 nfsrv_checkremove(vnode_t vp, int remove, NFSPROC_T *p)
5210 {
5211 	struct nfsstate *stp;
5212 	struct nfslockfile *lfp;
5213 	int error, haslock = 0;
5214 	fhandle_t nfh;
5215 
5216 	/*
5217 	 * First, get the lock file structure.
5218 	 * (A return of -1 means no associated state, so remove ok.)
5219 	 */
5220 	error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5221 tryagain:
5222 	NFSLOCKSTATE();
5223 	if (!error)
5224 		error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5225 	if (error) {
5226 		NFSUNLOCKSTATE();
5227 		if (haslock) {
5228 			NFSLOCKV4ROOTMUTEX();
5229 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5230 			NFSUNLOCKV4ROOTMUTEX();
5231 		}
5232 		if (error == -1)
5233 			error = 0;
5234 		goto out;
5235 	}
5236 
5237 	/*
5238 	 * Now, we must Recall any delegations.
5239 	 */
5240 	error = nfsrv_cleandeleg(vp, lfp, NULL, &haslock, p);
5241 	if (error) {
5242 		/*
5243 		 * nfsrv_cleandeleg() unlocks state for non-zero
5244 		 * return.
5245 		 */
5246 		if (error == -1)
5247 			goto tryagain;
5248 		if (haslock) {
5249 			NFSLOCKV4ROOTMUTEX();
5250 			nfsv4_unlock(&nfsv4rootfs_lock, 1);
5251 			NFSUNLOCKV4ROOTMUTEX();
5252 		}
5253 		goto out;
5254 	}
5255 
5256 	/*
5257 	 * Now, look for a conflicting open share.
5258 	 */
5259 	if (remove) {
5260 		/*
5261 		 * If the entry in the directory was the last reference to the
5262 		 * corresponding filesystem object, the object can be destroyed
5263 		 * */
5264 		if(lfp->lf_usecount>1)
5265 			LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
5266 				if (stp->ls_flags & NFSLCK_WRITEDENY) {
5267 					error = NFSERR_FILEOPEN;
5268 					break;
5269 				}
5270 			}
5271 	}
5272 
5273 	NFSUNLOCKSTATE();
5274 	if (haslock) {
5275 		NFSLOCKV4ROOTMUTEX();
5276 		nfsv4_unlock(&nfsv4rootfs_lock, 1);
5277 		NFSUNLOCKV4ROOTMUTEX();
5278 	}
5279 
5280 out:
5281 	NFSEXITCODE(error);
5282 	return (error);
5283 }
5284 
5285 /*
5286  * Clear out all delegations for the file referred to by lfp.
5287  * May return NFSERR_DELAY, if there will be a delay waiting for
5288  * delegations to expire.
5289  * Returns -1 to indicate it slept while recalling a delegation.
5290  * This function has the side effect of deleting the nfslockfile structure,
5291  * if it no longer has associated state and didn't have to sleep.
5292  * Unlocks State before a non-zero value is returned.
5293  */
5294 static int
5295 nfsrv_cleandeleg(vnode_t vp, struct nfslockfile *lfp,
5296     struct nfsclient *clp, int *haslockp, NFSPROC_T *p)
5297 {
5298 	struct nfsstate *stp, *nstp;
5299 	int ret = 0;
5300 
5301 	stp = LIST_FIRST(&lfp->lf_deleg);
5302 	while (stp != LIST_END(&lfp->lf_deleg)) {
5303 		nstp = LIST_NEXT(stp, ls_file);
5304 		if (stp->ls_clp != clp) {
5305 			ret = nfsrv_delegconflict(stp, haslockp, p, vp);
5306 			if (ret) {
5307 				/*
5308 				 * nfsrv_delegconflict() unlocks state
5309 				 * when it returns non-zero.
5310 				 */
5311 				goto out;
5312 			}
5313 		}
5314 		stp = nstp;
5315 	}
5316 out:
5317 	NFSEXITCODE(ret);
5318 	return (ret);
5319 }
5320 
5321 /*
5322  * There are certain operations that, when being done outside of NFSv4,
5323  * require that any NFSv4 delegation for the file be recalled.
5324  * This function is to be called for those cases:
5325  * VOP_RENAME() - When a delegation is being recalled for any reason,
5326  *	the client may have to do Opens against the server, using the file's
5327  *	final component name. If the file has been renamed on the server,
5328  *	that component name will be incorrect and the Open will fail.
5329  * VOP_REMOVE() - Theoretically, a client could Open a file after it has
5330  *	been removed on the server, if there is a delegation issued to
5331  *	that client for the file. I say "theoretically" since clients
5332  *	normally do an Access Op before the Open and that Access Op will
5333  *	fail with ESTALE. Note that NFSv2 and 3 don't even do Opens, so
5334  *	they will detect the file's removal in the same manner. (There is
5335  *	one case where RFC3530 allows a client to do an Open without first
5336  *	doing an Access Op, which is passage of a check against the ACE
5337  *	returned with a Write delegation, but current practice is to ignore
5338  *	the ACE and always do an Access Op.)
5339  *	Since the functions can only be called with an unlocked vnode, this
5340  *	can't be done at this time.
5341  * VOP_ADVLOCK() - When a client holds a delegation, it can issue byte range
5342  *	locks locally in the client, which are not visible to the server. To
5343  *	deal with this, issuing of delegations for a vnode must be disabled
5344  *	and all delegations for the vnode recalled. This is done via the
5345  *	second function, using the VV_DISABLEDELEG vflag on the vnode.
5346  */
5347 APPLESTATIC void
5348 nfsd_recalldelegation(vnode_t vp, NFSPROC_T *p)
5349 {
5350 	time_t starttime;
5351 	int error;
5352 
5353 	/*
5354 	 * First, check to see if the server is currently running and it has
5355 	 * been called for a regular file when issuing delegations.
5356 	 */
5357 	if (newnfs_numnfsd == 0 || vp->v_type != VREG ||
5358 	    nfsrv_issuedelegs == 0)
5359 		return;
5360 
5361 	KASSERT((NFSVOPISLOCKED(vp) != LK_EXCLUSIVE), ("vp %p is locked", vp));
5362 	/*
5363 	 * First, get a reference on the nfsv4rootfs_lock so that an
5364 	 * exclusive lock cannot be acquired by another thread.
5365 	 */
5366 	NFSLOCKV4ROOTMUTEX();
5367 	nfsv4_getref(&nfsv4rootfs_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
5368 	NFSUNLOCKV4ROOTMUTEX();
5369 
5370 	/*
5371 	 * Now, call nfsrv_checkremove() in a loop while it returns
5372 	 * NFSERR_DELAY. Return upon any other error or when timed out.
5373 	 */
5374 	starttime = NFSD_MONOSEC;
5375 	do {
5376 		if (NFSVOPLOCK(vp, LK_EXCLUSIVE) == 0) {
5377 			error = nfsrv_checkremove(vp, 0, p);
5378 			NFSVOPUNLOCK(vp, 0);
5379 		} else
5380 			error = EPERM;
5381 		if (error == NFSERR_DELAY) {
5382 			if (NFSD_MONOSEC - starttime > NFS_REMOVETIMEO)
5383 				break;
5384 			/* Sleep for a short period of time */
5385 			(void) nfs_catnap(PZERO, 0, "nfsremove");
5386 		}
5387 	} while (error == NFSERR_DELAY);
5388 	NFSLOCKV4ROOTMUTEX();
5389 	nfsv4_relref(&nfsv4rootfs_lock);
5390 	NFSUNLOCKV4ROOTMUTEX();
5391 }
5392 
5393 APPLESTATIC void
5394 nfsd_disabledelegation(vnode_t vp, NFSPROC_T *p)
5395 {
5396 
5397 #ifdef VV_DISABLEDELEG
5398 	/*
5399 	 * First, flag issuance of delegations disabled.
5400 	 */
5401 	atomic_set_long(&vp->v_vflag, VV_DISABLEDELEG);
5402 #endif
5403 
5404 	/*
5405 	 * Then call nfsd_recalldelegation() to get rid of all extant
5406 	 * delegations.
5407 	 */
5408 	nfsd_recalldelegation(vp, p);
5409 }
5410 
5411 /*
5412  * Check for conflicting locks, etc. and then get rid of delegations.
5413  * (At one point I thought that I should get rid of delegations for any
5414  *  Setattr, since it could potentially disallow the I/O op (read or write)
5415  *  allowed by the delegation. However, Setattr Ops that aren't changing
5416  *  the size get a stateid of all 0s, so you can't tell if it is a delegation
5417  *  for the same client or a different one, so I decided to only get rid
5418  *  of delegations for other clients when the size is being changed.)
5419  * In general, a Setattr can disable NFS I/O Ops that are outstanding, such
5420  * as Write backs, even if there is no delegation, so it really isn't any
5421  * different?)
5422  */
5423 APPLESTATIC int
5424 nfsrv_checksetattr(vnode_t vp, struct nfsrv_descript *nd,
5425     nfsv4stateid_t *stateidp, struct nfsvattr *nvap, nfsattrbit_t *attrbitp,
5426     struct nfsexstuff *exp, NFSPROC_T *p)
5427 {
5428 	struct nfsstate st, *stp = &st;
5429 	struct nfslock lo, *lop = &lo;
5430 	int error = 0;
5431 	nfsquad_t clientid;
5432 
5433 	if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE)) {
5434 		stp->ls_flags = (NFSLCK_CHECK | NFSLCK_WRITEACCESS);
5435 		lop->lo_first = nvap->na_size;
5436 	} else {
5437 		stp->ls_flags = 0;
5438 		lop->lo_first = 0;
5439 	}
5440 	if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) ||
5441 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) ||
5442 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_MODE) ||
5443 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL))
5444 		stp->ls_flags |= NFSLCK_SETATTR;
5445 	if (stp->ls_flags == 0)
5446 		goto out;
5447 	lop->lo_end = NFS64BITSSET;
5448 	lop->lo_flags = NFSLCK_WRITE;
5449 	stp->ls_ownerlen = 0;
5450 	stp->ls_op = NULL;
5451 	stp->ls_uid = nd->nd_cred->cr_uid;
5452 	stp->ls_stateid.seqid = stateidp->seqid;
5453 	clientid.lval[0] = stp->ls_stateid.other[0] = stateidp->other[0];
5454 	clientid.lval[1] = stp->ls_stateid.other[1] = stateidp->other[1];
5455 	stp->ls_stateid.other[2] = stateidp->other[2];
5456 	error = nfsrv_lockctrl(vp, &stp, &lop, NULL, clientid,
5457 	    stateidp, exp, nd, p);
5458 
5459 out:
5460 	NFSEXITCODE2(error, nd);
5461 	return (error);
5462 }
5463 
5464 /*
5465  * Check for a write delegation and do a CBGETATTR if there is one, updating
5466  * the attributes, as required.
5467  * Should I return an error if I can't get the attributes? (For now, I'll
5468  * just return ok.
5469  */
5470 APPLESTATIC int
5471 nfsrv_checkgetattr(struct nfsrv_descript *nd, vnode_t vp,
5472     struct nfsvattr *nvap, nfsattrbit_t *attrbitp, NFSPROC_T *p)
5473 {
5474 	struct nfsstate *stp;
5475 	struct nfslockfile *lfp;
5476 	struct nfsclient *clp;
5477 	struct nfsvattr nva;
5478 	fhandle_t nfh;
5479 	int error = 0;
5480 	nfsattrbit_t cbbits;
5481 	u_quad_t delegfilerev;
5482 
5483 	NFSCBGETATTR_ATTRBIT(attrbitp, &cbbits);
5484 	if (!NFSNONZERO_ATTRBIT(&cbbits))
5485 		goto out;
5486 	if (nfsrv_writedelegcnt == 0)
5487 		goto out;
5488 
5489 	/*
5490 	 * Get the lock file structure.
5491 	 * (A return of -1 means no associated state, so return ok.)
5492 	 */
5493 	error = nfsrv_getlockfh(vp, NFSLCK_CHECK, NULL, &nfh, p);
5494 	NFSLOCKSTATE();
5495 	if (!error)
5496 		error = nfsrv_getlockfile(NFSLCK_CHECK, NULL, &lfp, &nfh, 0);
5497 	if (error) {
5498 		NFSUNLOCKSTATE();
5499 		if (error == -1)
5500 			error = 0;
5501 		goto out;
5502 	}
5503 
5504 	/*
5505 	 * Now, look for a write delegation.
5506 	 */
5507 	LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
5508 		if (stp->ls_flags & NFSLCK_DELEGWRITE)
5509 			break;
5510 	}
5511 	if (stp == LIST_END(&lfp->lf_deleg)) {
5512 		NFSUNLOCKSTATE();
5513 		goto out;
5514 	}
5515 	clp = stp->ls_clp;
5516 	delegfilerev = stp->ls_filerev;
5517 
5518 	/*
5519 	 * If the Write delegation was issued as a part of this Compound RPC
5520 	 * or if we have an Implied Clientid (used in a previous Op in this
5521 	 * compound) and it is the client the delegation was issued to,
5522 	 * just return ok.
5523 	 * I also assume that it is from the same client iff the network
5524 	 * host IP address is the same as the callback address. (Not
5525 	 * exactly correct by the RFC, but avoids a lot of Getattr
5526 	 * callbacks.)
5527 	 */
5528 	if (nd->nd_compref == stp->ls_compref ||
5529 	    ((nd->nd_flag & ND_IMPLIEDCLID) &&
5530 	     clp->lc_clientid.qval == nd->nd_clientid.qval) ||
5531 	     nfsaddr2_match(clp->lc_req.nr_nam, nd->nd_nam)) {
5532 		NFSUNLOCKSTATE();
5533 		goto out;
5534 	}
5535 
5536 	/*
5537 	 * We are now done with the delegation state structure,
5538 	 * so the statelock can be released and we can now tsleep().
5539 	 */
5540 
5541 	/*
5542 	 * Now, we must do the CB Getattr callback, to see if Change or Size
5543 	 * has changed.
5544 	 */
5545 	if (clp->lc_expiry >= NFSD_MONOSEC) {
5546 		NFSUNLOCKSTATE();
5547 		NFSVNO_ATTRINIT(&nva);
5548 		nva.na_filerev = NFS64BITSSET;
5549 		error = nfsrv_docallback(clp, NFSV4OP_CBGETATTR, NULL,
5550 		    0, &nfh, &nva, &cbbits, 0, p);
5551 		if (!error) {
5552 			if ((nva.na_filerev != NFS64BITSSET &&
5553 			    nva.na_filerev > delegfilerev) ||
5554 			    (NFSVNO_ISSETSIZE(&nva) &&
5555 			     nva.na_size != nvap->na_size)) {
5556 				error = nfsvno_updfilerev(vp, nvap, nd, p);
5557 				if (NFSVNO_ISSETSIZE(&nva))
5558 					nvap->na_size = nva.na_size;
5559 			}
5560 		} else
5561 			error = 0;	/* Ignore callback errors for now. */
5562 	} else {
5563 		NFSUNLOCKSTATE();
5564 	}
5565 
5566 out:
5567 	NFSEXITCODE2(error, nd);
5568 	return (error);
5569 }
5570 
5571 /*
5572  * This function looks for openowners that haven't had any opens for
5573  * a while and throws them away. Called by an nfsd when NFSNSF_NOOPENS
5574  * is set.
5575  */
5576 APPLESTATIC void
5577 nfsrv_throwawayopens(NFSPROC_T *p)
5578 {
5579 	struct nfsclient *clp, *nclp;
5580 	struct nfsstate *stp, *nstp;
5581 	int i;
5582 
5583 	NFSLOCKSTATE();
5584 	nfsrv_stablefirst.nsf_flags &= ~NFSNSF_NOOPENS;
5585 	/*
5586 	 * For each client...
5587 	 */
5588 	for (i = 0; i < nfsrv_clienthashsize; i++) {
5589 	    LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5590 		LIST_FOREACH_SAFE(stp, &clp->lc_open, ls_list, nstp) {
5591 			if (LIST_EMPTY(&stp->ls_open) &&
5592 			    (stp->ls_noopens > NFSNOOPEN ||
5593 			     (nfsrv_openpluslock * 2) >
5594 			     nfsrv_v4statelimit))
5595 				nfsrv_freeopenowner(stp, 0, p);
5596 		}
5597 	    }
5598 	}
5599 	NFSUNLOCKSTATE();
5600 }
5601 
5602 /*
5603  * This function checks to see if the credentials are the same.
5604  * Returns 1 for not same, 0 otherwise.
5605  */
5606 static int
5607 nfsrv_notsamecredname(struct nfsrv_descript *nd, struct nfsclient *clp)
5608 {
5609 
5610 	if (nd->nd_flag & ND_GSS) {
5611 		if (!(clp->lc_flags & LCL_GSS))
5612 			return (1);
5613 		if (clp->lc_flags & LCL_NAME) {
5614 			if (nd->nd_princlen != clp->lc_namelen ||
5615 			    NFSBCMP(nd->nd_principal, clp->lc_name,
5616 				clp->lc_namelen))
5617 				return (1);
5618 			else
5619 				return (0);
5620 		}
5621 		if (nd->nd_cred->cr_uid == clp->lc_uid)
5622 			return (0);
5623 		else
5624 			return (1);
5625 	} else if (clp->lc_flags & LCL_GSS)
5626 		return (1);
5627 	/*
5628 	 * For AUTH_SYS, allow the same uid or root. (This is underspecified
5629 	 * in RFC3530, which talks about principals, but doesn't say anything
5630 	 * about uids for AUTH_SYS.)
5631 	 */
5632 	if (nd->nd_cred->cr_uid == clp->lc_uid || nd->nd_cred->cr_uid == 0)
5633 		return (0);
5634 	else
5635 		return (1);
5636 }
5637 
5638 /*
5639  * Calculate the lease expiry time.
5640  */
5641 static time_t
5642 nfsrv_leaseexpiry(void)
5643 {
5644 
5645 	if (nfsrv_stablefirst.nsf_eograce > NFSD_MONOSEC)
5646 		return (NFSD_MONOSEC + 2 * (nfsrv_lease + NFSRV_LEASEDELTA));
5647 	return (NFSD_MONOSEC + nfsrv_lease + NFSRV_LEASEDELTA);
5648 }
5649 
5650 /*
5651  * Delay the delegation timeout as far as ls_delegtimelimit, as required.
5652  */
5653 static void
5654 nfsrv_delaydelegtimeout(struct nfsstate *stp)
5655 {
5656 
5657 	if ((stp->ls_flags & NFSLCK_DELEGRECALL) == 0)
5658 		return;
5659 
5660 	if ((stp->ls_delegtime + 15) > NFSD_MONOSEC &&
5661 	    stp->ls_delegtime < stp->ls_delegtimelimit) {
5662 		stp->ls_delegtime += nfsrv_lease;
5663 		if (stp->ls_delegtime > stp->ls_delegtimelimit)
5664 			stp->ls_delegtime = stp->ls_delegtimelimit;
5665 	}
5666 }
5667 
5668 /*
5669  * This function checks to see if there is any other state associated
5670  * with the openowner for this Open.
5671  * It returns 1 if there is no other state, 0 otherwise.
5672  */
5673 static int
5674 nfsrv_nootherstate(struct nfsstate *stp)
5675 {
5676 	struct nfsstate *tstp;
5677 
5678 	LIST_FOREACH(tstp, &stp->ls_openowner->ls_open, ls_list) {
5679 		if (tstp != stp || !LIST_EMPTY(&tstp->ls_lock))
5680 			return (0);
5681 	}
5682 	return (1);
5683 }
5684 
5685 /*
5686  * Create a list of lock deltas (changes to local byte range locking
5687  * that can be rolled back using the list) and apply the changes via
5688  * nfsvno_advlock(). Optionally, lock the list. It is expected that either
5689  * the rollback or update function will be called after this.
5690  * It returns an error (and rolls back, as required), if any nfsvno_advlock()
5691  * call fails. If it returns an error, it will unlock the list.
5692  */
5693 static int
5694 nfsrv_locallock(vnode_t vp, struct nfslockfile *lfp, int flags,
5695     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5696 {
5697 	struct nfslock *lop, *nlop;
5698 	int error = 0;
5699 
5700 	/* Loop through the list of locks. */
5701 	lop = LIST_FIRST(&lfp->lf_locallock);
5702 	while (first < end && lop != NULL) {
5703 		nlop = LIST_NEXT(lop, lo_lckowner);
5704 		if (first >= lop->lo_end) {
5705 			/* not there yet */
5706 			lop = nlop;
5707 		} else if (first < lop->lo_first) {
5708 			/* new one starts before entry in list */
5709 			if (end <= lop->lo_first) {
5710 				/* no overlap between old and new */
5711 				error = nfsrv_dolocal(vp, lfp, flags,
5712 				    NFSLCK_UNLOCK, first, end, cfp, p);
5713 				if (error != 0)
5714 					break;
5715 				first = end;
5716 			} else {
5717 				/* handle fragment overlapped with new one */
5718 				error = nfsrv_dolocal(vp, lfp, flags,
5719 				    NFSLCK_UNLOCK, first, lop->lo_first, cfp,
5720 				    p);
5721 				if (error != 0)
5722 					break;
5723 				first = lop->lo_first;
5724 			}
5725 		} else {
5726 			/* new one overlaps this entry in list */
5727 			if (end <= lop->lo_end) {
5728 				/* overlaps all of new one */
5729 				error = nfsrv_dolocal(vp, lfp, flags,
5730 				    lop->lo_flags, first, end, cfp, p);
5731 				if (error != 0)
5732 					break;
5733 				first = end;
5734 			} else {
5735 				/* handle fragment overlapped with new one */
5736 				error = nfsrv_dolocal(vp, lfp, flags,
5737 				    lop->lo_flags, first, lop->lo_end, cfp, p);
5738 				if (error != 0)
5739 					break;
5740 				first = lop->lo_end;
5741 				lop = nlop;
5742 			}
5743 		}
5744 	}
5745 	if (first < end && error == 0)
5746 		/* handle fragment past end of list */
5747 		error = nfsrv_dolocal(vp, lfp, flags, NFSLCK_UNLOCK, first,
5748 		    end, cfp, p);
5749 
5750 	NFSEXITCODE(error);
5751 	return (error);
5752 }
5753 
5754 /*
5755  * Local lock unlock. Unlock all byte ranges that are no longer locked
5756  * by NFSv4. To do this, unlock any subranges of first-->end that
5757  * do not overlap with the byte ranges of any lock in the lfp->lf_lock
5758  * list. This list has all locks for the file held by other
5759  * <clientid, lockowner> tuples. The list is ordered by increasing
5760  * lo_first value, but may have entries that overlap each other, for
5761  * the case of read locks.
5762  */
5763 static void
5764 nfsrv_localunlock(vnode_t vp, struct nfslockfile *lfp, uint64_t init_first,
5765     uint64_t init_end, NFSPROC_T *p)
5766 {
5767 	struct nfslock *lop;
5768 	uint64_t first, end, prevfirst;
5769 
5770 	first = init_first;
5771 	end = init_end;
5772 	while (first < init_end) {
5773 		/* Loop through all nfs locks, adjusting first and end */
5774 		prevfirst = 0;
5775 		LIST_FOREACH(lop, &lfp->lf_lock, lo_lckfile) {
5776 			KASSERT(prevfirst <= lop->lo_first,
5777 			    ("nfsv4 locks out of order"));
5778 			KASSERT(lop->lo_first < lop->lo_end,
5779 			    ("nfsv4 bogus lock"));
5780 			prevfirst = lop->lo_first;
5781 			if (first >= lop->lo_first &&
5782 			    first < lop->lo_end)
5783 				/*
5784 				 * Overlaps with initial part, so trim
5785 				 * off that initial part by moving first past
5786 				 * it.
5787 				 */
5788 				first = lop->lo_end;
5789 			else if (end > lop->lo_first &&
5790 			    lop->lo_first > first) {
5791 				/*
5792 				 * This lock defines the end of the
5793 				 * segment to unlock, so set end to the
5794 				 * start of it and break out of the loop.
5795 				 */
5796 				end = lop->lo_first;
5797 				break;
5798 			}
5799 			if (first >= end)
5800 				/*
5801 				 * There is no segment left to do, so
5802 				 * break out of this loop and then exit
5803 				 * the outer while() since first will be set
5804 				 * to end, which must equal init_end here.
5805 				 */
5806 				break;
5807 		}
5808 		if (first < end) {
5809 			/* Unlock this segment */
5810 			(void) nfsrv_dolocal(vp, lfp, NFSLCK_UNLOCK,
5811 			    NFSLCK_READ, first, end, NULL, p);
5812 			nfsrv_locallock_commit(lfp, NFSLCK_UNLOCK,
5813 			    first, end);
5814 		}
5815 		/*
5816 		 * Now move past this segment and look for any further
5817 		 * segment in the range, if there is one.
5818 		 */
5819 		first = end;
5820 		end = init_end;
5821 	}
5822 }
5823 
5824 /*
5825  * Do the local lock operation and update the rollback list, as required.
5826  * Perform the rollback and return the error if nfsvno_advlock() fails.
5827  */
5828 static int
5829 nfsrv_dolocal(vnode_t vp, struct nfslockfile *lfp, int flags, int oldflags,
5830     uint64_t first, uint64_t end, struct nfslockconflict *cfp, NFSPROC_T *p)
5831 {
5832 	struct nfsrollback *rlp;
5833 	int error = 0, ltype, oldltype;
5834 
5835 	if (flags & NFSLCK_WRITE)
5836 		ltype = F_WRLCK;
5837 	else if (flags & NFSLCK_READ)
5838 		ltype = F_RDLCK;
5839 	else
5840 		ltype = F_UNLCK;
5841 	if (oldflags & NFSLCK_WRITE)
5842 		oldltype = F_WRLCK;
5843 	else if (oldflags & NFSLCK_READ)
5844 		oldltype = F_RDLCK;
5845 	else
5846 		oldltype = F_UNLCK;
5847 	if (ltype == oldltype || (oldltype == F_WRLCK && ltype == F_RDLCK))
5848 		/* nothing to do */
5849 		goto out;
5850 	error = nfsvno_advlock(vp, ltype, first, end, p);
5851 	if (error != 0) {
5852 		if (cfp != NULL) {
5853 			cfp->cl_clientid.lval[0] = 0;
5854 			cfp->cl_clientid.lval[1] = 0;
5855 			cfp->cl_first = 0;
5856 			cfp->cl_end = NFS64BITSSET;
5857 			cfp->cl_flags = NFSLCK_WRITE;
5858 			cfp->cl_ownerlen = 5;
5859 			NFSBCOPY("LOCAL", cfp->cl_owner, 5);
5860 		}
5861 		nfsrv_locallock_rollback(vp, lfp, p);
5862 	} else if (ltype != F_UNLCK) {
5863 		rlp = malloc(sizeof (struct nfsrollback), M_NFSDROLLBACK,
5864 		    M_WAITOK);
5865 		rlp->rlck_first = first;
5866 		rlp->rlck_end = end;
5867 		rlp->rlck_type = oldltype;
5868 		LIST_INSERT_HEAD(&lfp->lf_rollback, rlp, rlck_list);
5869 	}
5870 
5871 out:
5872 	NFSEXITCODE(error);
5873 	return (error);
5874 }
5875 
5876 /*
5877  * Roll back local lock changes and free up the rollback list.
5878  */
5879 static void
5880 nfsrv_locallock_rollback(vnode_t vp, struct nfslockfile *lfp, NFSPROC_T *p)
5881 {
5882 	struct nfsrollback *rlp, *nrlp;
5883 
5884 	LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp) {
5885 		(void) nfsvno_advlock(vp, rlp->rlck_type, rlp->rlck_first,
5886 		    rlp->rlck_end, p);
5887 		free(rlp, M_NFSDROLLBACK);
5888 	}
5889 	LIST_INIT(&lfp->lf_rollback);
5890 }
5891 
5892 /*
5893  * Update local lock list and delete rollback list (ie now committed to the
5894  * local locks). Most of the work is done by the internal function.
5895  */
5896 static void
5897 nfsrv_locallock_commit(struct nfslockfile *lfp, int flags, uint64_t first,
5898     uint64_t end)
5899 {
5900 	struct nfsrollback *rlp, *nrlp;
5901 	struct nfslock *new_lop, *other_lop;
5902 
5903 	new_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK, M_WAITOK);
5904 	if (flags & (NFSLCK_READ | NFSLCK_WRITE))
5905 		other_lop = malloc(sizeof (struct nfslock), M_NFSDLOCK,
5906 		    M_WAITOK);
5907 	else
5908 		other_lop = NULL;
5909 	new_lop->lo_flags = flags;
5910 	new_lop->lo_first = first;
5911 	new_lop->lo_end = end;
5912 	nfsrv_updatelock(NULL, &new_lop, &other_lop, lfp);
5913 	if (new_lop != NULL)
5914 		free(new_lop, M_NFSDLOCK);
5915 	if (other_lop != NULL)
5916 		free(other_lop, M_NFSDLOCK);
5917 
5918 	/* and get rid of the rollback list */
5919 	LIST_FOREACH_SAFE(rlp, &lfp->lf_rollback, rlck_list, nrlp)
5920 		free(rlp, M_NFSDROLLBACK);
5921 	LIST_INIT(&lfp->lf_rollback);
5922 }
5923 
5924 /*
5925  * Lock the struct nfslockfile for local lock updating.
5926  */
5927 static void
5928 nfsrv_locklf(struct nfslockfile *lfp)
5929 {
5930 	int gotlock;
5931 
5932 	/* lf_usecount ensures *lfp won't be free'd */
5933 	lfp->lf_usecount++;
5934 	do {
5935 		gotlock = nfsv4_lock(&lfp->lf_locallock_lck, 1, NULL,
5936 		    NFSSTATEMUTEXPTR, NULL);
5937 	} while (gotlock == 0);
5938 	lfp->lf_usecount--;
5939 }
5940 
5941 /*
5942  * Unlock the struct nfslockfile after local lock updating.
5943  */
5944 static void
5945 nfsrv_unlocklf(struct nfslockfile *lfp)
5946 {
5947 
5948 	nfsv4_unlock(&lfp->lf_locallock_lck, 0);
5949 }
5950 
5951 /*
5952  * Clear out all state for the NFSv4 server.
5953  * Must be called by a thread that can sleep when no nfsds are running.
5954  */
5955 void
5956 nfsrv_throwawayallstate(NFSPROC_T *p)
5957 {
5958 	struct nfsclient *clp, *nclp;
5959 	struct nfslockfile *lfp, *nlfp;
5960 	int i;
5961 
5962 	/*
5963 	 * For each client, clean out the state and then free the structure.
5964 	 */
5965 	for (i = 0; i < nfsrv_clienthashsize; i++) {
5966 		LIST_FOREACH_SAFE(clp, &nfsclienthash[i], lc_hash, nclp) {
5967 			nfsrv_cleanclient(clp, p);
5968 			nfsrv_freedeleglist(&clp->lc_deleg);
5969 			nfsrv_freedeleglist(&clp->lc_olddeleg);
5970 			free(clp->lc_stateid, M_NFSDCLIENT);
5971 			free(clp, M_NFSDCLIENT);
5972 		}
5973 	}
5974 
5975 	/*
5976 	 * Also, free up any remaining lock file structures.
5977 	 */
5978 	for (i = 0; i < nfsrv_lockhashsize; i++) {
5979 		LIST_FOREACH_SAFE(lfp, &nfslockhash[i], lf_hash, nlfp) {
5980 			printf("nfsd unload: fnd a lock file struct\n");
5981 			nfsrv_freenfslockfile(lfp);
5982 		}
5983 	}
5984 
5985 	/* And get rid of the deviceid structures and layouts. */
5986 	nfsrv_freealllayoutsanddevids();
5987 }
5988 
5989 /*
5990  * Check the sequence# for the session and slot provided as an argument.
5991  * Also, renew the lease if the session will return NFS_OK.
5992  */
5993 int
5994 nfsrv_checksequence(struct nfsrv_descript *nd, uint32_t sequenceid,
5995     uint32_t *highest_slotidp, uint32_t *target_highest_slotidp, int cache_this,
5996     uint32_t *sflagsp, NFSPROC_T *p)
5997 {
5998 	struct nfsdsession *sep;
5999 	struct nfssessionhash *shp;
6000 	int error;
6001 	SVCXPRT *savxprt;
6002 
6003 	shp = NFSSESSIONHASH(nd->nd_sessionid);
6004 	NFSLOCKSESSION(shp);
6005 	sep = nfsrv_findsession(nd->nd_sessionid);
6006 	if (sep == NULL) {
6007 		NFSUNLOCKSESSION(shp);
6008 		return (NFSERR_BADSESSION);
6009 	}
6010 	error = nfsv4_seqsession(sequenceid, nd->nd_slotid, *highest_slotidp,
6011 	    sep->sess_slots, NULL, NFSV4_SLOTS - 1);
6012 	if (error != 0) {
6013 		NFSUNLOCKSESSION(shp);
6014 		return (error);
6015 	}
6016 	if (cache_this != 0)
6017 		nd->nd_flag |= ND_SAVEREPLY;
6018 	/* Renew the lease. */
6019 	sep->sess_clp->lc_expiry = nfsrv_leaseexpiry();
6020 	nd->nd_clientid.qval = sep->sess_clp->lc_clientid.qval;
6021 	nd->nd_flag |= ND_IMPLIEDCLID;
6022 
6023 	/*
6024 	 * If this session handles the backchannel, save the nd_xprt for this
6025 	 * RPC, since this is the one being used.
6026 	 * RFC-5661 specifies that the fore channel will be implicitly
6027 	 * bound by a Sequence operation.  However, since some NFSv4.1 clients
6028 	 * erroneously assumed that the back channel would be implicitly
6029 	 * bound as well, do the implicit binding unless a
6030 	 * BindConnectiontoSession has already been done on the session.
6031 	 */
6032 	if (sep->sess_clp->lc_req.nr_client != NULL &&
6033 	    sep->sess_cbsess.nfsess_xprt != nd->nd_xprt &&
6034 	    (sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0 &&
6035 	    (sep->sess_clp->lc_flags & LCL_DONEBINDCONN) == 0) {
6036 		NFSD_DEBUG(2,
6037 		    "nfsrv_checksequence: implicit back channel bind\n");
6038 		savxprt = sep->sess_cbsess.nfsess_xprt;
6039 		SVC_ACQUIRE(nd->nd_xprt);
6040 		nd->nd_xprt->xp_p2 =
6041 		    sep->sess_clp->lc_req.nr_client->cl_private;
6042 		nd->nd_xprt->xp_idletimeout = 0;	/* Disable timeout. */
6043 		sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6044 		if (savxprt != NULL)
6045 			SVC_RELEASE(savxprt);
6046 	}
6047 
6048 	*sflagsp = 0;
6049 	if (sep->sess_clp->lc_req.nr_client == NULL)
6050 		*sflagsp |= NFSV4SEQ_CBPATHDOWN;
6051 	NFSUNLOCKSESSION(shp);
6052 	if (error == NFSERR_EXPIRED) {
6053 		*sflagsp |= NFSV4SEQ_EXPIREDALLSTATEREVOKED;
6054 		error = 0;
6055 	} else if (error == NFSERR_ADMINREVOKED) {
6056 		*sflagsp |= NFSV4SEQ_ADMINSTATEREVOKED;
6057 		error = 0;
6058 	}
6059 	*highest_slotidp = *target_highest_slotidp = NFSV4_SLOTS - 1;
6060 	return (0);
6061 }
6062 
6063 /*
6064  * Check/set reclaim complete for this session/clientid.
6065  */
6066 int
6067 nfsrv_checkreclaimcomplete(struct nfsrv_descript *nd)
6068 {
6069 	struct nfsdsession *sep;
6070 	struct nfssessionhash *shp;
6071 	int error = 0;
6072 
6073 	shp = NFSSESSIONHASH(nd->nd_sessionid);
6074 	NFSLOCKSTATE();
6075 	NFSLOCKSESSION(shp);
6076 	sep = nfsrv_findsession(nd->nd_sessionid);
6077 	if (sep == NULL) {
6078 		NFSUNLOCKSESSION(shp);
6079 		NFSUNLOCKSTATE();
6080 		return (NFSERR_BADSESSION);
6081 	}
6082 
6083 	/* Check to see if reclaim complete has already happened. */
6084 	if ((sep->sess_clp->lc_flags & LCL_RECLAIMCOMPLETE) != 0)
6085 		error = NFSERR_COMPLETEALREADY;
6086 	else {
6087 		sep->sess_clp->lc_flags |= LCL_RECLAIMCOMPLETE;
6088 		nfsrv_markreclaim(sep->sess_clp);
6089 	}
6090 	NFSUNLOCKSESSION(shp);
6091 	NFSUNLOCKSTATE();
6092 	return (error);
6093 }
6094 
6095 /*
6096  * Cache the reply in a session slot.
6097  */
6098 void
6099 nfsrv_cache_session(uint8_t *sessionid, uint32_t slotid, int repstat,
6100    struct mbuf **m)
6101 {
6102 	struct nfsdsession *sep;
6103 	struct nfssessionhash *shp;
6104 
6105 	shp = NFSSESSIONHASH(sessionid);
6106 	NFSLOCKSESSION(shp);
6107 	sep = nfsrv_findsession(sessionid);
6108 	if (sep == NULL) {
6109 		NFSUNLOCKSESSION(shp);
6110 		printf("nfsrv_cache_session: no session\n");
6111 		m_freem(*m);
6112 		return;
6113 	}
6114 	nfsv4_seqsess_cacherep(slotid, sep->sess_slots, repstat, m);
6115 	NFSUNLOCKSESSION(shp);
6116 }
6117 
6118 /*
6119  * Search for a session that matches the sessionid.
6120  */
6121 static struct nfsdsession *
6122 nfsrv_findsession(uint8_t *sessionid)
6123 {
6124 	struct nfsdsession *sep;
6125 	struct nfssessionhash *shp;
6126 
6127 	shp = NFSSESSIONHASH(sessionid);
6128 	LIST_FOREACH(sep, &shp->list, sess_hash) {
6129 		if (!NFSBCMP(sessionid, sep->sess_sessionid, NFSX_V4SESSIONID))
6130 			break;
6131 	}
6132 	return (sep);
6133 }
6134 
6135 /*
6136  * Destroy a session.
6137  */
6138 int
6139 nfsrv_destroysession(struct nfsrv_descript *nd, uint8_t *sessionid)
6140 {
6141 	int error, igotlock, samesess;
6142 
6143 	samesess = 0;
6144 	if (!NFSBCMP(sessionid, nd->nd_sessionid, NFSX_V4SESSIONID) &&
6145 	    (nd->nd_flag & ND_HASSEQUENCE) != 0) {
6146 		samesess = 1;
6147 		if ((nd->nd_flag & ND_LASTOP) == 0)
6148 			return (NFSERR_BADSESSION);
6149 	}
6150 
6151 	/* Lock out other nfsd threads */
6152 	NFSLOCKV4ROOTMUTEX();
6153 	nfsv4_relref(&nfsv4rootfs_lock);
6154 	do {
6155 		igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
6156 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
6157 	} while (igotlock == 0);
6158 	NFSUNLOCKV4ROOTMUTEX();
6159 
6160 	error = nfsrv_freesession(NULL, sessionid);
6161 	if (error == 0 && samesess != 0)
6162 		nd->nd_flag &= ~ND_HASSEQUENCE;
6163 
6164 	NFSLOCKV4ROOTMUTEX();
6165 	nfsv4_unlock(&nfsv4rootfs_lock, 1);
6166 	NFSUNLOCKV4ROOTMUTEX();
6167 	return (error);
6168 }
6169 
6170 /*
6171  * Bind a connection to a session.
6172  * For now, only certain variants are supported, since the current session
6173  * structure can only handle a single backchannel entry, which will be
6174  * applied to all connections if it is set.
6175  */
6176 int
6177 nfsrv_bindconnsess(struct nfsrv_descript *nd, uint8_t *sessionid, int *foreaftp)
6178 {
6179 	struct nfssessionhash *shp;
6180 	struct nfsdsession *sep;
6181 	struct nfsclient *clp;
6182 	SVCXPRT *savxprt;
6183 	int error;
6184 
6185 	error = 0;
6186 	shp = NFSSESSIONHASH(sessionid);
6187 	NFSLOCKSTATE();
6188 	NFSLOCKSESSION(shp);
6189 	sep = nfsrv_findsession(sessionid);
6190 	if (sep != NULL) {
6191 		clp = sep->sess_clp;
6192 		if (*foreaftp == NFSCDFC4_BACK ||
6193 		    *foreaftp == NFSCDFC4_BACK_OR_BOTH ||
6194 		    *foreaftp == NFSCDFC4_FORE_OR_BOTH) {
6195 			/* Try to set up a backchannel. */
6196 			if (clp->lc_req.nr_client == NULL) {
6197 				NFSD_DEBUG(2, "nfsrv_bindconnsess: acquire "
6198 				    "backchannel\n");
6199 				clp->lc_req.nr_client = (struct __rpc_client *)
6200 				    clnt_bck_create(nd->nd_xprt->xp_socket,
6201 				    sep->sess_cbprogram, NFSV4_CBVERS);
6202 			}
6203 			if (clp->lc_req.nr_client != NULL) {
6204 				NFSD_DEBUG(2, "nfsrv_bindconnsess: set up "
6205 				    "backchannel\n");
6206 				savxprt = sep->sess_cbsess.nfsess_xprt;
6207 				SVC_ACQUIRE(nd->nd_xprt);
6208 				nd->nd_xprt->xp_p2 =
6209 				    clp->lc_req.nr_client->cl_private;
6210 				/* Disable idle timeout. */
6211 				nd->nd_xprt->xp_idletimeout = 0;
6212 				sep->sess_cbsess.nfsess_xprt = nd->nd_xprt;
6213 				if (savxprt != NULL)
6214 					SVC_RELEASE(savxprt);
6215 				sep->sess_crflags |= NFSV4CRSESS_CONNBACKCHAN;
6216 				clp->lc_flags |= LCL_DONEBINDCONN;
6217 				if (*foreaftp == NFSCDFS4_BACK)
6218 					*foreaftp = NFSCDFS4_BACK;
6219 				else
6220 					*foreaftp = NFSCDFS4_BOTH;
6221 			} else if (*foreaftp != NFSCDFC4_BACK) {
6222 				NFSD_DEBUG(2, "nfsrv_bindconnsess: can't set "
6223 				    "up backchannel\n");
6224 				sep->sess_crflags &= ~NFSV4CRSESS_CONNBACKCHAN;
6225 				clp->lc_flags |= LCL_DONEBINDCONN;
6226 				*foreaftp = NFSCDFS4_FORE;
6227 			} else {
6228 				error = NFSERR_NOTSUPP;
6229 				printf("nfsrv_bindconnsess: Can't add "
6230 				    "backchannel\n");
6231 			}
6232 		} else {
6233 			NFSD_DEBUG(2, "nfsrv_bindconnsess: Set forechannel\n");
6234 			clp->lc_flags |= LCL_DONEBINDCONN;
6235 			*foreaftp = NFSCDFS4_FORE;
6236 		}
6237 	} else
6238 		error = NFSERR_BADSESSION;
6239 	NFSUNLOCKSESSION(shp);
6240 	NFSUNLOCKSTATE();
6241 	return (error);
6242 }
6243 
6244 /*
6245  * Free up a session structure.
6246  */
6247 static int
6248 nfsrv_freesession(struct nfsdsession *sep, uint8_t *sessionid)
6249 {
6250 	struct nfssessionhash *shp;
6251 	int i;
6252 
6253 	NFSLOCKSTATE();
6254 	if (sep == NULL) {
6255 		shp = NFSSESSIONHASH(sessionid);
6256 		NFSLOCKSESSION(shp);
6257 		sep = nfsrv_findsession(sessionid);
6258 	} else {
6259 		shp = NFSSESSIONHASH(sep->sess_sessionid);
6260 		NFSLOCKSESSION(shp);
6261 	}
6262 	if (sep != NULL) {
6263 		sep->sess_refcnt--;
6264 		if (sep->sess_refcnt > 0) {
6265 			NFSUNLOCKSESSION(shp);
6266 			NFSUNLOCKSTATE();
6267 			return (NFSERR_BACKCHANBUSY);
6268 		}
6269 		LIST_REMOVE(sep, sess_hash);
6270 		LIST_REMOVE(sep, sess_list);
6271 	}
6272 	NFSUNLOCKSESSION(shp);
6273 	NFSUNLOCKSTATE();
6274 	if (sep == NULL)
6275 		return (NFSERR_BADSESSION);
6276 	for (i = 0; i < NFSV4_SLOTS; i++)
6277 		if (sep->sess_slots[i].nfssl_reply != NULL)
6278 			m_freem(sep->sess_slots[i].nfssl_reply);
6279 	if (sep->sess_cbsess.nfsess_xprt != NULL)
6280 		SVC_RELEASE(sep->sess_cbsess.nfsess_xprt);
6281 	free(sep, M_NFSDSESSION);
6282 	return (0);
6283 }
6284 
6285 /*
6286  * Free a stateid.
6287  * RFC5661 says that it should fail when there are associated opens, locks
6288  * or delegations. Since stateids represent opens, I don't see how you can
6289  * free an open stateid (it will be free'd when closed), so this function
6290  * only works for lock stateids (freeing the lock_owner) or delegations.
6291  */
6292 int
6293 nfsrv_freestateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6294     NFSPROC_T *p)
6295 {
6296 	struct nfsclient *clp;
6297 	struct nfsstate *stp;
6298 	int error;
6299 
6300 	NFSLOCKSTATE();
6301 	/*
6302 	 * Look up the stateid
6303 	 */
6304 	error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6305 	    NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6306 	if (error == 0) {
6307 		/* First, check for a delegation. */
6308 		LIST_FOREACH(stp, &clp->lc_deleg, ls_list) {
6309 			if (!NFSBCMP(stp->ls_stateid.other, stateidp->other,
6310 			    NFSX_STATEIDOTHER))
6311 				break;
6312 		}
6313 		if (stp != NULL) {
6314 			nfsrv_freedeleg(stp);
6315 			NFSUNLOCKSTATE();
6316 			return (error);
6317 		}
6318 	}
6319 	/* Not a delegation, try for a lock_owner. */
6320 	if (error == 0)
6321 		error = nfsrv_getstate(clp, stateidp, 0, &stp);
6322 	if (error == 0 && ((stp->ls_flags & (NFSLCK_OPEN | NFSLCK_DELEGREAD |
6323 	    NFSLCK_DELEGWRITE)) != 0 || (stp->ls_flags & NFSLCK_LOCK) == 0))
6324 		/* Not a lock_owner stateid. */
6325 		error = NFSERR_LOCKSHELD;
6326 	if (error == 0 && !LIST_EMPTY(&stp->ls_lock))
6327 		error = NFSERR_LOCKSHELD;
6328 	if (error == 0)
6329 		nfsrv_freelockowner(stp, NULL, 0, p);
6330 	NFSUNLOCKSTATE();
6331 	return (error);
6332 }
6333 
6334 /*
6335  * Test a stateid.
6336  */
6337 int
6338 nfsrv_teststateid(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp,
6339     NFSPROC_T *p)
6340 {
6341 	struct nfsclient *clp;
6342 	struct nfsstate *stp;
6343 	int error;
6344 
6345 	NFSLOCKSTATE();
6346 	/*
6347 	 * Look up the stateid
6348 	 */
6349 	error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
6350 	    NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
6351 	if (error == 0)
6352 		error = nfsrv_getstate(clp, stateidp, 0, &stp);
6353 	if (error == 0 && stateidp->seqid != 0 &&
6354 	    SEQ_LT(stateidp->seqid, stp->ls_stateid.seqid))
6355 		error = NFSERR_OLDSTATEID;
6356 	NFSUNLOCKSTATE();
6357 	return (error);
6358 }
6359 
6360 /*
6361  * Generate the xdr for an NFSv4.1 CBSequence Operation.
6362  */
6363 static int
6364 nfsv4_setcbsequence(struct nfsrv_descript *nd, struct nfsclient *clp,
6365     int dont_replycache, struct nfsdsession **sepp)
6366 {
6367 	struct nfsdsession *sep;
6368 	uint32_t *tl, slotseq = 0;
6369 	int maxslot, slotpos;
6370 	uint8_t sessionid[NFSX_V4SESSIONID];
6371 	int error;
6372 
6373 	error = nfsv4_getcbsession(clp, sepp);
6374 	if (error != 0)
6375 		return (error);
6376 	sep = *sepp;
6377 	(void)nfsv4_sequencelookup(NULL, &sep->sess_cbsess, &slotpos, &maxslot,
6378 	    &slotseq, sessionid);
6379 	KASSERT(maxslot >= 0, ("nfsv4_setcbsequence neg maxslot"));
6380 
6381 	/* Build the Sequence arguments. */
6382 	NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 5 * NFSX_UNSIGNED);
6383 	bcopy(sessionid, tl, NFSX_V4SESSIONID);
6384 	tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
6385 	nd->nd_slotseq = tl;
6386 	*tl++ = txdr_unsigned(slotseq);
6387 	*tl++ = txdr_unsigned(slotpos);
6388 	*tl++ = txdr_unsigned(maxslot);
6389 	if (dont_replycache == 0)
6390 		*tl++ = newnfs_true;
6391 	else
6392 		*tl++ = newnfs_false;
6393 	*tl = 0;			/* No referring call list, for now. */
6394 	nd->nd_flag |= ND_HASSEQUENCE;
6395 	return (0);
6396 }
6397 
6398 /*
6399  * Get a session for the callback.
6400  */
6401 static int
6402 nfsv4_getcbsession(struct nfsclient *clp, struct nfsdsession **sepp)
6403 {
6404 	struct nfsdsession *sep;
6405 
6406 	NFSLOCKSTATE();
6407 	LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6408 		if ((sep->sess_crflags & NFSV4CRSESS_CONNBACKCHAN) != 0)
6409 			break;
6410 	}
6411 	if (sep == NULL) {
6412 		NFSUNLOCKSTATE();
6413 		return (NFSERR_BADSESSION);
6414 	}
6415 	sep->sess_refcnt++;
6416 	*sepp = sep;
6417 	NFSUNLOCKSTATE();
6418 	return (0);
6419 }
6420 
6421 /*
6422  * Free up all backchannel xprts.  This needs to be done when the nfsd threads
6423  * exit, since those transports will all be going away.
6424  * This is only called after all the nfsd threads are done performing RPCs,
6425  * so locking shouldn't be an issue.
6426  */
6427 APPLESTATIC void
6428 nfsrv_freeallbackchannel_xprts(void)
6429 {
6430 	struct nfsdsession *sep;
6431 	struct nfsclient *clp;
6432 	SVCXPRT *xprt;
6433 	int i;
6434 
6435 	for (i = 0; i < nfsrv_clienthashsize; i++) {
6436 		LIST_FOREACH(clp, &nfsclienthash[i], lc_hash) {
6437 			LIST_FOREACH(sep, &clp->lc_session, sess_list) {
6438 				xprt = sep->sess_cbsess.nfsess_xprt;
6439 				sep->sess_cbsess.nfsess_xprt = NULL;
6440 				if (xprt != NULL)
6441 					SVC_RELEASE(xprt);
6442 			}
6443 		}
6444 	}
6445 }
6446 
6447 /*
6448  * Do a layout commit.  Actually just call nfsrv_updatemdsattr().
6449  * I have no idea if the rest of these arguments will ever be useful?
6450  */
6451 int
6452 nfsrv_layoutcommit(struct nfsrv_descript *nd, vnode_t vp, int layouttype,
6453     int hasnewoff, uint64_t newoff, uint64_t offset, uint64_t len,
6454     int hasnewmtime, struct timespec *newmtimep, int reclaim,
6455     nfsv4stateid_t *stateidp, int maxcnt, char *layp, int *hasnewsizep,
6456     uint64_t *newsizep, struct ucred *cred, NFSPROC_T *p)
6457 {
6458 	struct nfsvattr na;
6459 	int error;
6460 
6461 	error = nfsrv_updatemdsattr(vp, &na, p);
6462 	if (error == 0) {
6463 		*hasnewsizep = 1;
6464 		*newsizep = na.na_size;
6465 	}
6466 	return (error);
6467 }
6468 
6469 /*
6470  * Try and get a layout.
6471  */
6472 int
6473 nfsrv_layoutget(struct nfsrv_descript *nd, vnode_t vp, struct nfsexstuff *exp,
6474     int layouttype, int *iomode, uint64_t *offset, uint64_t *len,
6475     uint64_t minlen, nfsv4stateid_t *stateidp, int maxcnt, int *retonclose,
6476     int *layoutlenp, char *layp, struct ucred *cred, NFSPROC_T *p)
6477 {
6478 	struct nfslayouthash *lhyp;
6479 	struct nfslayout *lyp;
6480 	char *devid;
6481 	fhandle_t fh, *dsfhp;
6482 	int error, mirrorcnt;
6483 
6484 	if (nfsrv_devidcnt == 0)
6485 		return (NFSERR_UNKNLAYOUTTYPE);
6486 
6487 	if (*offset != 0)
6488 		printf("nfsrv_layoutget: off=%ju len=%ju\n", (uintmax_t)*offset,
6489 		    (uintmax_t)*len);
6490 	error = nfsvno_getfh(vp, &fh, p);
6491 	NFSD_DEBUG(4, "layoutget getfh=%d\n", error);
6492 	if (error != 0)
6493 		return (error);
6494 
6495 	/*
6496 	 * For now, all layouts are for entire files.
6497 	 * Only issue Read/Write layouts if requested for a non-readonly fs.
6498 	 */
6499 	if (NFSVNO_EXRDONLY(exp)) {
6500 		if (*iomode == NFSLAYOUTIOMODE_RW)
6501 			return (NFSERR_LAYOUTTRYLATER);
6502 		*iomode = NFSLAYOUTIOMODE_READ;
6503 	}
6504 	if (*iomode != NFSLAYOUTIOMODE_RW)
6505 		*iomode = NFSLAYOUTIOMODE_READ;
6506 
6507 	/*
6508 	 * Check to see if a write layout can be issued for this file.
6509 	 * This is used during mirror recovery to avoid RW layouts being
6510 	 * issued for a file while it is being copied to the recovered
6511 	 * mirror.
6512 	 */
6513 	if (*iomode == NFSLAYOUTIOMODE_RW && nfsrv_dontlayout(&fh) != 0)
6514 		return (NFSERR_LAYOUTTRYLATER);
6515 
6516 	*retonclose = 0;
6517 	*offset = 0;
6518 	*len = UINT64_MAX;
6519 
6520 	/* First, see if a layout already exists and return if found. */
6521 	lhyp = NFSLAYOUTHASH(&fh);
6522 	NFSLOCKLAYOUT(lhyp);
6523 	error = nfsrv_findlayout(&nd->nd_clientid, &fh, layouttype, p, &lyp);
6524 	NFSD_DEBUG(4, "layoutget findlay=%d\n", error);
6525 	/*
6526 	 * Not sure if the seqid must be the same, so I won't check it.
6527 	 */
6528 	if (error == 0 && (stateidp->other[0] != lyp->lay_stateid.other[0] ||
6529 	    stateidp->other[1] != lyp->lay_stateid.other[1] ||
6530 	    stateidp->other[2] != lyp->lay_stateid.other[2])) {
6531 		if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
6532 			NFSUNLOCKLAYOUT(lhyp);
6533 			NFSD_DEBUG(1, "ret bad stateid\n");
6534 			return (NFSERR_BADSTATEID);
6535 		}
6536 		/*
6537 		 * I believe we get here because there is a race between
6538 		 * the client processing the CBLAYOUTRECALL and the layout
6539 		 * being deleted here on the server.
6540 		 * The client has now done a LayoutGet with a non-layout
6541 		 * stateid, as it would when there is no layout.
6542 		 * As such, free this layout and set error == NFSERR_BADSTATEID
6543 		 * so the code below will create a new layout structure as
6544 		 * would happen if no layout was found.
6545 		 * "lyp" will be set before being used below, but set it NULL
6546 		 * as a safety belt.
6547 		 */
6548 		nfsrv_freelayout(&lhyp->list, lyp);
6549 		lyp = NULL;
6550 		error = NFSERR_BADSTATEID;
6551 	}
6552 	if (error == 0) {
6553 		if (lyp->lay_layoutlen > maxcnt) {
6554 			NFSUNLOCKLAYOUT(lhyp);
6555 			NFSD_DEBUG(1, "ret layout too small\n");
6556 			return (NFSERR_TOOSMALL);
6557 		}
6558 		if (*iomode == NFSLAYOUTIOMODE_RW)
6559 			lyp->lay_flags |= NFSLAY_RW;
6560 		else
6561 			lyp->lay_flags |= NFSLAY_READ;
6562 		NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
6563 		*layoutlenp = lyp->lay_layoutlen;
6564 		if (++lyp->lay_stateid.seqid == 0)
6565 			lyp->lay_stateid.seqid = 1;
6566 		stateidp->seqid = lyp->lay_stateid.seqid;
6567 		NFSUNLOCKLAYOUT(lhyp);
6568 		NFSD_DEBUG(4, "ret fnd layout\n");
6569 		return (0);
6570 	}
6571 	NFSUNLOCKLAYOUT(lhyp);
6572 
6573 	/* Find the device id and file handle. */
6574 	dsfhp = malloc(sizeof(fhandle_t) * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6575 	devid = malloc(NFSX_V4DEVICEID * NFSDEV_MAXMIRRORS, M_TEMP, M_WAITOK);
6576 	error = nfsrv_dsgetdevandfh(vp, p, &mirrorcnt, dsfhp, devid);
6577 	NFSD_DEBUG(4, "layoutget devandfh=%d\n", error);
6578 	if (error == 0) {
6579 		if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
6580 			if (NFSX_V4FILELAYOUT > maxcnt)
6581 				error = NFSERR_TOOSMALL;
6582 			else
6583 				lyp = nfsrv_filelayout(nd, *iomode, &fh, dsfhp,
6584 				    devid, vp->v_mount->mnt_stat.f_fsid);
6585 		} else {
6586 			if (NFSX_V4FLEXLAYOUT(mirrorcnt) > maxcnt)
6587 				error = NFSERR_TOOSMALL;
6588 			else
6589 				lyp = nfsrv_flexlayout(nd, *iomode, mirrorcnt,
6590 				    &fh, dsfhp, devid,
6591 				    vp->v_mount->mnt_stat.f_fsid);
6592 		}
6593 	}
6594 	free(dsfhp, M_TEMP);
6595 	free(devid, M_TEMP);
6596 	if (error != 0)
6597 		return (error);
6598 
6599 	/*
6600 	 * Now, add this layout to the list.
6601 	 */
6602 	error = nfsrv_addlayout(nd, &lyp, stateidp, layp, layoutlenp, p);
6603 	NFSD_DEBUG(4, "layoutget addl=%d\n", error);
6604 	/*
6605 	 * The lyp will be set to NULL by nfsrv_addlayout() if it
6606 	 * linked the new structure into the lists.
6607 	 */
6608 	free(lyp, M_NFSDSTATE);
6609 	return (error);
6610 }
6611 
6612 /*
6613  * Generate a File Layout.
6614  */
6615 static struct nfslayout *
6616 nfsrv_filelayout(struct nfsrv_descript *nd, int iomode, fhandle_t *fhp,
6617     fhandle_t *dsfhp, char *devid, fsid_t fs)
6618 {
6619 	uint32_t *tl;
6620 	struct nfslayout *lyp;
6621 	uint64_t pattern_offset;
6622 
6623 	lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FILELAYOUT, M_NFSDSTATE,
6624 	    M_WAITOK | M_ZERO);
6625 	lyp->lay_type = NFSLAYOUT_NFSV4_1_FILES;
6626 	if (iomode == NFSLAYOUTIOMODE_RW)
6627 		lyp->lay_flags = NFSLAY_RW;
6628 	else
6629 		lyp->lay_flags = NFSLAY_READ;
6630 	NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6631 	lyp->lay_clientid.qval = nd->nd_clientid.qval;
6632 	lyp->lay_fsid = fs;
6633 
6634 	/* Fill in the xdr for the files layout. */
6635 	tl = (uint32_t *)lyp->lay_xdr;
6636 	NFSBCOPY(devid, tl, NFSX_V4DEVICEID);		/* Device ID. */
6637 	tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6638 
6639 	/*
6640 	 * Make the stripe size as many 64K blocks as will fit in the stripe
6641 	 * mask. Since there is only one stripe, the stripe size doesn't really
6642 	 * matter, except that the Linux client will only handle an exact
6643 	 * multiple of their PAGE_SIZE (usually 4K).  I chose 64K as a value
6644 	 * that should cover most/all arches w.r.t. PAGE_SIZE.
6645 	 */
6646 	*tl++ = txdr_unsigned(NFSFLAYUTIL_STRIPE_MASK & ~0xffff);
6647 	*tl++ = 0;					/* 1st stripe index. */
6648 	pattern_offset = 0;
6649 	txdr_hyper(pattern_offset, tl); tl += 2;	/* Pattern offset. */
6650 	*tl++ = txdr_unsigned(1);			/* 1 file handle. */
6651 	*tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6652 	NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6653 	lyp->lay_layoutlen = NFSX_V4FILELAYOUT;
6654 	return (lyp);
6655 }
6656 
6657 #define	FLEX_OWNERID	"999"
6658 #define	FLEX_UID0	"0"
6659 /*
6660  * Generate a Flex File Layout.
6661  * The FLEX_OWNERID can be any string of 3 decimal digits. Although this
6662  * string goes on the wire, it isn't supposed to be used by the client,
6663  * since this server uses tight coupling.
6664  * Although not recommended by the spec., if vfs.nfsd.flexlinuxhack=1 use
6665  * a string of "0". This works around the Linux Flex File Layout driver bug
6666  * which uses the synthetic uid/gid strings for the "tightly coupled" case.
6667  */
6668 static struct nfslayout *
6669 nfsrv_flexlayout(struct nfsrv_descript *nd, int iomode, int mirrorcnt,
6670     fhandle_t *fhp, fhandle_t *dsfhp, char *devid, fsid_t fs)
6671 {
6672 	uint32_t *tl;
6673 	struct nfslayout *lyp;
6674 	uint64_t lenval;
6675 	int i;
6676 
6677 	lyp = malloc(sizeof(struct nfslayout) + NFSX_V4FLEXLAYOUT(mirrorcnt),
6678 	    M_NFSDSTATE, M_WAITOK | M_ZERO);
6679 	lyp->lay_type = NFSLAYOUT_FLEXFILE;
6680 	if (iomode == NFSLAYOUTIOMODE_RW)
6681 		lyp->lay_flags = NFSLAY_RW;
6682 	else
6683 		lyp->lay_flags = NFSLAY_READ;
6684 	NFSBCOPY(fhp, &lyp->lay_fh, sizeof(*fhp));
6685 	lyp->lay_clientid.qval = nd->nd_clientid.qval;
6686 	lyp->lay_fsid = fs;
6687 	lyp->lay_mirrorcnt = mirrorcnt;
6688 
6689 	/* Fill in the xdr for the files layout. */
6690 	tl = (uint32_t *)lyp->lay_xdr;
6691 	lenval = 0;
6692 	txdr_hyper(lenval, tl); tl += 2;		/* Stripe unit. */
6693 	*tl++ = txdr_unsigned(mirrorcnt);		/* # of mirrors. */
6694 	for (i = 0; i < mirrorcnt; i++) {
6695 		*tl++ = txdr_unsigned(1);		/* One stripe. */
6696 		NFSBCOPY(devid, tl, NFSX_V4DEVICEID);	/* Device ID. */
6697 		tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6698 		devid += NFSX_V4DEVICEID;
6699 		*tl++ = txdr_unsigned(1);		/* Efficiency. */
6700 		*tl++ = 0;				/* Proxy Stateid. */
6701 		*tl++ = 0x55555555;
6702 		*tl++ = 0x55555555;
6703 		*tl++ = 0x55555555;
6704 		*tl++ = txdr_unsigned(1);		/* 1 file handle. */
6705 		*tl++ = txdr_unsigned(NFSX_V4PNFSFH);
6706 		NFSBCOPY(dsfhp, tl, sizeof(*dsfhp));
6707 		tl += (NFSM_RNDUP(NFSX_V4PNFSFH) / NFSX_UNSIGNED);
6708 		dsfhp++;
6709 		if (nfsrv_flexlinuxhack != 0) {
6710 			*tl++ = txdr_unsigned(strlen(FLEX_UID0));
6711 			*tl = 0;		/* 0 pad string. */
6712 			NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
6713 			*tl++ = txdr_unsigned(strlen(FLEX_UID0));
6714 			*tl = 0;		/* 0 pad string. */
6715 			NFSBCOPY(FLEX_UID0, tl++, strlen(FLEX_UID0));
6716 		} else {
6717 			*tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
6718 			NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
6719 			*tl++ = txdr_unsigned(strlen(FLEX_OWNERID));
6720 			NFSBCOPY(FLEX_OWNERID, tl++, NFSX_UNSIGNED);
6721 		}
6722 	}
6723 	*tl++ = txdr_unsigned(0);		/* ff_flags. */
6724 	*tl = txdr_unsigned(60);		/* Status interval hint. */
6725 	lyp->lay_layoutlen = NFSX_V4FLEXLAYOUT(mirrorcnt);
6726 	return (lyp);
6727 }
6728 
6729 /*
6730  * Parse and process Flex File errors returned via LayoutReturn.
6731  */
6732 static void
6733 nfsrv_flexlayouterr(struct nfsrv_descript *nd, uint32_t *layp, int maxcnt,
6734     NFSPROC_T *p)
6735 {
6736 	uint32_t *tl;
6737 	int cnt, errcnt, i, j, opnum, stat;
6738 	char devid[NFSX_V4DEVICEID];
6739 
6740 	tl = layp;
6741 	cnt = fxdr_unsigned(int, *tl++);
6742 	NFSD_DEBUG(4, "flexlayouterr cnt=%d\n", cnt);
6743 	for (i = 0; i < cnt; i++) {
6744 		/* Skip offset, length and stateid for now. */
6745 		tl += (4 + NFSX_STATEID / NFSX_UNSIGNED);
6746 		errcnt = fxdr_unsigned(int, *tl++);
6747 		NFSD_DEBUG(4, "flexlayouterr errcnt=%d\n", errcnt);
6748 		for (j = 0; j < errcnt; j++) {
6749 			NFSBCOPY(tl, devid, NFSX_V4DEVICEID);
6750 			tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED);
6751 			stat = fxdr_unsigned(int, *tl++);
6752 			opnum = fxdr_unsigned(int, *tl++);
6753 			NFSD_DEBUG(4, "flexlayouterr op=%d stat=%d\n", opnum,
6754 			    stat);
6755 			/*
6756 			 * Except for NFSERR_ACCES and NFSERR_STALE errors,
6757 			 * disable the mirror.
6758 			 */
6759 			if (stat != NFSERR_ACCES && stat != NFSERR_STALE)
6760 				nfsrv_delds(devid, p);
6761 		}
6762 	}
6763 }
6764 
6765 /*
6766  * This function removes all flex file layouts which has a mirror with
6767  * a device id that matches the argument.
6768  * Called when the DS represented by the device id has failed.
6769  */
6770 void
6771 nfsrv_flexmirrordel(char *devid, NFSPROC_T *p)
6772 {
6773 	uint32_t *tl;
6774 	struct nfslayout *lyp, *nlyp;
6775 	struct nfslayouthash *lhyp;
6776 	struct nfslayouthead loclyp;
6777 	int i, j;
6778 
6779 	NFSD_DEBUG(4, "flexmirrordel\n");
6780 	/* Move all layouts found onto a local list. */
6781 	TAILQ_INIT(&loclyp);
6782 	for (i = 0; i < nfsrv_layouthashsize; i++) {
6783 		lhyp = &nfslayouthash[i];
6784 		NFSLOCKLAYOUT(lhyp);
6785 		TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
6786 			if (lyp->lay_type == NFSLAYOUT_FLEXFILE &&
6787 			    lyp->lay_mirrorcnt > 1) {
6788 				NFSD_DEBUG(4, "possible match\n");
6789 				tl = lyp->lay_xdr;
6790 				tl += 3;
6791 				for (j = 0; j < lyp->lay_mirrorcnt; j++) {
6792 					tl++;
6793 					if (NFSBCMP(devid, tl, NFSX_V4DEVICEID)
6794 					    == 0) {
6795 						/* Found one. */
6796 						NFSD_DEBUG(4, "fnd one\n");
6797 						TAILQ_REMOVE(&lhyp->list, lyp,
6798 						    lay_list);
6799 						TAILQ_INSERT_HEAD(&loclyp, lyp,
6800 						    lay_list);
6801 						break;
6802 					}
6803 					tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED +
6804 					    NFSM_RNDUP(NFSX_V4PNFSFH) /
6805 					    NFSX_UNSIGNED + 11 * NFSX_UNSIGNED);
6806 				}
6807 			}
6808 		}
6809 		NFSUNLOCKLAYOUT(lhyp);
6810 	}
6811 
6812 	/* Now, try to do a Layout recall for each one found. */
6813 	TAILQ_FOREACH_SAFE(lyp, &loclyp, lay_list, nlyp) {
6814 		NFSD_DEBUG(4, "do layout recall\n");
6815 		/*
6816 		 * The layout stateid.seqid needs to be incremented
6817 		 * before doing a LAYOUT_RECALL callback.
6818 		 * Set lay_trycnt to UINT16_MAX so it won't set up a retry.
6819 		 */
6820 		if (++lyp->lay_stateid.seqid == 0)
6821 			lyp->lay_stateid.seqid = 1;
6822 		lyp->lay_trycnt = UINT16_MAX;
6823 		nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
6824 		    &lyp->lay_fh, lyp, &loclyp, lyp->lay_type, p);
6825 		nfsrv_freelayout(&loclyp, lyp);
6826 	}
6827 }
6828 
6829 /*
6830  * Do a recall callback to the client for this layout.
6831  */
6832 static int
6833 nfsrv_recalllayout(nfsquad_t clid, nfsv4stateid_t *stateidp, fhandle_t *fhp,
6834     struct nfslayout *lyp, struct nfslayouthead *lyheadp, int laytype,
6835     NFSPROC_T *p)
6836 {
6837 	struct nfsclient *clp;
6838 	int error;
6839 
6840 	NFSD_DEBUG(4, "nfsrv_recalllayout\n");
6841 	error = nfsrv_getclient(clid, 0, &clp, NULL, (nfsquad_t)((u_quad_t)0),
6842 	    0, NULL, p);
6843 	NFSD_DEBUG(4, "aft nfsrv_getclient=%d\n", error);
6844 	if (error != 0)
6845 		return (error);
6846 	if ((clp->lc_flags & LCL_NFSV41) != 0) {
6847 		error = nfsrv_docallback(clp, NFSV4OP_CBLAYOUTRECALL,
6848 		    stateidp, 0, fhp, NULL, NULL, laytype, p);
6849 		/* If lyp != NULL, handle an error return here. */
6850 		if (error != 0 && lyp != NULL) {
6851 			NFSDRECALLLOCK();
6852 			if (error == NFSERR_NOMATCHLAYOUT) {
6853 				/*
6854 				 * Mark it returned, since there is no layout.
6855 				 */
6856 				if ((lyp->lay_flags & NFSLAY_RECALL) != 0) {
6857 					lyp->lay_flags |= NFSLAY_RETURNED;
6858 					wakeup(lyp);
6859 				}
6860 				NFSDRECALLUNLOCK();
6861 			} else if ((lyp->lay_flags & NFSLAY_RETURNED) == 0 &&
6862 			    lyp->lay_trycnt < 10) {
6863 				/*
6864 				 * Clear recall, so it can be tried again
6865 				 * and put it at the end of the list to
6866 				 * delay the retry a little longer.
6867 				 */
6868 				lyp->lay_flags &= ~NFSLAY_RECALL;
6869 				lyp->lay_trycnt++;
6870 				TAILQ_REMOVE(lyheadp, lyp, lay_list);
6871 				TAILQ_INSERT_TAIL(lyheadp, lyp, lay_list);
6872 				NFSDRECALLUNLOCK();
6873 				nfs_catnap(PVFS, 0, "nfsrclay");
6874 			} else
6875 				NFSDRECALLUNLOCK();
6876 		}
6877 	} else
6878 		printf("nfsrv_recalllayout: clp not NFSv4.1\n");
6879 	return (error);
6880 }
6881 
6882 /*
6883  * Find a layout to recall when we exceed our high water mark.
6884  */
6885 void
6886 nfsrv_recalloldlayout(NFSPROC_T *p)
6887 {
6888 	struct nfslayouthash *lhyp;
6889 	struct nfslayout *lyp;
6890 	nfsquad_t clientid;
6891 	nfsv4stateid_t stateid;
6892 	fhandle_t fh;
6893 	int error, laytype, ret;
6894 
6895 	lhyp = &nfslayouthash[arc4random() % nfsrv_layouthashsize];
6896 	NFSLOCKLAYOUT(lhyp);
6897 	TAILQ_FOREACH_REVERSE(lyp, &lhyp->list, nfslayouthead, lay_list) {
6898 		if ((lyp->lay_flags & NFSLAY_CALLB) == 0) {
6899 			lyp->lay_flags |= NFSLAY_CALLB;
6900 			/*
6901 			 * The layout stateid.seqid needs to be incremented
6902 			 * before doing a LAYOUT_RECALL callback.
6903 			 */
6904 			if (++lyp->lay_stateid.seqid == 0)
6905 				lyp->lay_stateid.seqid = 1;
6906 			clientid = lyp->lay_clientid;
6907 			stateid = lyp->lay_stateid;
6908 			fh = lyp->lay_fh;
6909 			laytype = lyp->lay_type;
6910 			break;
6911 		}
6912 	}
6913 	NFSUNLOCKLAYOUT(lhyp);
6914 	if (lyp != NULL) {
6915 		error = nfsrv_recalllayout(clientid, &stateid, &fh, NULL, NULL,
6916 		    laytype, p);
6917 		if (error != 0 && error != NFSERR_NOMATCHLAYOUT)
6918 			printf("recallold=%d\n", error);
6919 		if (error != 0) {
6920 			NFSLOCKLAYOUT(lhyp);
6921 			/*
6922 			 * Since the hash list was unlocked, we need to
6923 			 * find it again.
6924 			 */
6925 			ret = nfsrv_findlayout(&clientid, &fh, laytype, p,
6926 			    &lyp);
6927 			if (ret == 0 &&
6928 			    (lyp->lay_flags & NFSLAY_CALLB) != 0 &&
6929 			    lyp->lay_stateid.other[0] == stateid.other[0] &&
6930 			    lyp->lay_stateid.other[1] == stateid.other[1] &&
6931 			    lyp->lay_stateid.other[2] == stateid.other[2]) {
6932 				/*
6933 				 * The client no longer knows this layout, so
6934 				 * it can be free'd now.
6935 				 */
6936 				if (error == NFSERR_NOMATCHLAYOUT)
6937 					nfsrv_freelayout(&lhyp->list, lyp);
6938 				else {
6939 					/*
6940 					 * Leave it to be tried later by
6941 					 * clearing NFSLAY_CALLB and moving
6942 					 * it to the head of the list, so it
6943 					 * won't be tried again for a while.
6944 					 */
6945 					lyp->lay_flags &= ~NFSLAY_CALLB;
6946 					TAILQ_REMOVE(&lhyp->list, lyp,
6947 					    lay_list);
6948 					TAILQ_INSERT_HEAD(&lhyp->list, lyp,
6949 					    lay_list);
6950 				}
6951 			}
6952 			NFSUNLOCKLAYOUT(lhyp);
6953 		}
6954 	}
6955 }
6956 
6957 /*
6958  * Try and return layout(s).
6959  */
6960 int
6961 nfsrv_layoutreturn(struct nfsrv_descript *nd, vnode_t vp,
6962     int layouttype, int iomode, uint64_t offset, uint64_t len, int reclaim,
6963     int kind, nfsv4stateid_t *stateidp, int maxcnt, uint32_t *layp, int *fndp,
6964     struct ucred *cred, NFSPROC_T *p)
6965 {
6966 	struct nfsvattr na;
6967 	struct nfslayouthash *lhyp;
6968 	struct nfslayout *lyp;
6969 	fhandle_t fh;
6970 	int error = 0;
6971 
6972 	*fndp = 0;
6973 	if (kind == NFSV4LAYOUTRET_FILE) {
6974 		error = nfsvno_getfh(vp, &fh, p);
6975 		if (error == 0) {
6976 			error = nfsrv_updatemdsattr(vp, &na, p);
6977 			if (error != 0)
6978 				printf("nfsrv_layoutreturn: updatemdsattr"
6979 				    " failed=%d\n", error);
6980 		}
6981 		if (error == 0) {
6982 			if (reclaim == newnfs_true) {
6983 				error = nfsrv_checkgrace(NULL, NULL,
6984 				    NFSLCK_RECLAIM);
6985 				if (error != NFSERR_NOGRACE)
6986 					error = 0;
6987 				return (error);
6988 			}
6989 			lhyp = NFSLAYOUTHASH(&fh);
6990 			NFSDRECALLLOCK();
6991 			NFSLOCKLAYOUT(lhyp);
6992 			error = nfsrv_findlayout(&nd->nd_clientid, &fh,
6993 			    layouttype, p, &lyp);
6994 			NFSD_DEBUG(4, "layoutret findlay=%d\n", error);
6995 			if (error == 0 &&
6996 			    stateidp->other[0] == lyp->lay_stateid.other[0] &&
6997 			    stateidp->other[1] == lyp->lay_stateid.other[1] &&
6998 			    stateidp->other[2] == lyp->lay_stateid.other[2]) {
6999 				NFSD_DEBUG(4, "nfsrv_layoutreturn: stateid %d"
7000 				    " %x %x %x laystateid %d %x %x %x"
7001 				    " off=%ju len=%ju flgs=0x%x\n",
7002 				    stateidp->seqid, stateidp->other[0],
7003 				    stateidp->other[1], stateidp->other[2],
7004 				    lyp->lay_stateid.seqid,
7005 				    lyp->lay_stateid.other[0],
7006 				    lyp->lay_stateid.other[1],
7007 				    lyp->lay_stateid.other[2],
7008 				    (uintmax_t)offset, (uintmax_t)len,
7009 				    lyp->lay_flags);
7010 				if (++lyp->lay_stateid.seqid == 0)
7011 					lyp->lay_stateid.seqid = 1;
7012 				stateidp->seqid = lyp->lay_stateid.seqid;
7013 				if (offset == 0 && len == UINT64_MAX) {
7014 					if ((iomode & NFSLAYOUTIOMODE_READ) !=
7015 					    0)
7016 						lyp->lay_flags &= ~NFSLAY_READ;
7017 					if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7018 						lyp->lay_flags &= ~NFSLAY_RW;
7019 					if ((lyp->lay_flags & (NFSLAY_READ |
7020 					    NFSLAY_RW)) == 0)
7021 						nfsrv_freelayout(&lhyp->list,
7022 						    lyp);
7023 					else
7024 						*fndp = 1;
7025 				} else
7026 					*fndp = 1;
7027 			}
7028 			NFSUNLOCKLAYOUT(lhyp);
7029 			/* Search the nfsrv_recalllist for a match. */
7030 			TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
7031 				if (NFSBCMP(&lyp->lay_fh, &fh,
7032 				    sizeof(fh)) == 0 &&
7033 				    lyp->lay_clientid.qval ==
7034 				    nd->nd_clientid.qval &&
7035 				    stateidp->other[0] ==
7036 				    lyp->lay_stateid.other[0] &&
7037 				    stateidp->other[1] ==
7038 				    lyp->lay_stateid.other[1] &&
7039 				    stateidp->other[2] ==
7040 				    lyp->lay_stateid.other[2]) {
7041 					lyp->lay_flags |= NFSLAY_RETURNED;
7042 					wakeup(lyp);
7043 					error = 0;
7044 				}
7045 			}
7046 			NFSDRECALLUNLOCK();
7047 		}
7048 		if (layouttype == NFSLAYOUT_FLEXFILE)
7049 			nfsrv_flexlayouterr(nd, layp, maxcnt, p);
7050 	} else if (kind == NFSV4LAYOUTRET_FSID)
7051 		nfsrv_freelayouts(&nd->nd_clientid,
7052 		    &vp->v_mount->mnt_stat.f_fsid, layouttype, iomode);
7053 	else if (kind == NFSV4LAYOUTRET_ALL)
7054 		nfsrv_freelayouts(&nd->nd_clientid, NULL, layouttype, iomode);
7055 	else
7056 		error = NFSERR_INVAL;
7057 	if (error == -1)
7058 		error = 0;
7059 	return (error);
7060 }
7061 
7062 /*
7063  * Look for an existing layout.
7064  */
7065 static int
7066 nfsrv_findlayout(nfsquad_t *clientidp, fhandle_t *fhp, int laytype,
7067     NFSPROC_T *p, struct nfslayout **lypp)
7068 {
7069 	struct nfslayouthash *lhyp;
7070 	struct nfslayout *lyp;
7071 	int ret;
7072 
7073 	*lypp = NULL;
7074 	ret = 0;
7075 	lhyp = NFSLAYOUTHASH(fhp);
7076 	TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
7077 		if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7078 		    lyp->lay_clientid.qval == clientidp->qval &&
7079 		    lyp->lay_type == laytype)
7080 			break;
7081 	}
7082 	if (lyp != NULL)
7083 		*lypp = lyp;
7084 	else
7085 		ret = -1;
7086 	return (ret);
7087 }
7088 
7089 /*
7090  * Add the new layout, as required.
7091  */
7092 static int
7093 nfsrv_addlayout(struct nfsrv_descript *nd, struct nfslayout **lypp,
7094     nfsv4stateid_t *stateidp, char *layp, int *layoutlenp, NFSPROC_T *p)
7095 {
7096 	struct nfsclient *clp;
7097 	struct nfslayouthash *lhyp;
7098 	struct nfslayout *lyp, *nlyp;
7099 	fhandle_t *fhp;
7100 	int error;
7101 
7102 	KASSERT((nd->nd_flag & ND_IMPLIEDCLID) != 0,
7103 	    ("nfsrv_layoutget: no nd_clientid\n"));
7104 	lyp = *lypp;
7105 	fhp = &lyp->lay_fh;
7106 	NFSLOCKSTATE();
7107 	error = nfsrv_getclient((nfsquad_t)((u_quad_t)0), CLOPS_RENEW, &clp,
7108 	    NULL, (nfsquad_t)((u_quad_t)0), 0, nd, p);
7109 	if (error != 0) {
7110 		NFSUNLOCKSTATE();
7111 		return (error);
7112 	}
7113 	lyp->lay_stateid.seqid = stateidp->seqid = 1;
7114 	lyp->lay_stateid.other[0] = stateidp->other[0] =
7115 	    clp->lc_clientid.lval[0];
7116 	lyp->lay_stateid.other[1] = stateidp->other[1] =
7117 	    clp->lc_clientid.lval[1];
7118 	lyp->lay_stateid.other[2] = stateidp->other[2] =
7119 	    nfsrv_nextstateindex(clp);
7120 	NFSUNLOCKSTATE();
7121 
7122 	lhyp = NFSLAYOUTHASH(fhp);
7123 	NFSLOCKLAYOUT(lhyp);
7124 	TAILQ_FOREACH(nlyp, &lhyp->list, lay_list) {
7125 		if (NFSBCMP(&nlyp->lay_fh, fhp, sizeof(*fhp)) == 0 &&
7126 		    nlyp->lay_clientid.qval == nd->nd_clientid.qval)
7127 			break;
7128 	}
7129 	if (nlyp != NULL) {
7130 		/* A layout already exists, so use it. */
7131 		nlyp->lay_flags |= (lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW));
7132 		NFSBCOPY(nlyp->lay_xdr, layp, nlyp->lay_layoutlen);
7133 		*layoutlenp = nlyp->lay_layoutlen;
7134 		if (++nlyp->lay_stateid.seqid == 0)
7135 			nlyp->lay_stateid.seqid = 1;
7136 		stateidp->seqid = nlyp->lay_stateid.seqid;
7137 		stateidp->other[0] = nlyp->lay_stateid.other[0];
7138 		stateidp->other[1] = nlyp->lay_stateid.other[1];
7139 		stateidp->other[2] = nlyp->lay_stateid.other[2];
7140 		NFSUNLOCKLAYOUT(lhyp);
7141 		return (0);
7142 	}
7143 
7144 	/* Insert the new layout in the lists. */
7145 	*lypp = NULL;
7146 	atomic_add_int(&nfsrv_layoutcnt, 1);
7147 	NFSBCOPY(lyp->lay_xdr, layp, lyp->lay_layoutlen);
7148 	*layoutlenp = lyp->lay_layoutlen;
7149 	TAILQ_INSERT_HEAD(&lhyp->list, lyp, lay_list);
7150 	NFSUNLOCKLAYOUT(lhyp);
7151 	return (0);
7152 }
7153 
7154 /*
7155  * Get the devinfo for a deviceid.
7156  */
7157 int
7158 nfsrv_getdevinfo(char *devid, int layouttype, uint32_t *maxcnt,
7159     uint32_t *notify, int *devaddrlen, char **devaddr)
7160 {
7161 	struct nfsdevice *ds;
7162 
7163 	if ((layouttype != NFSLAYOUT_NFSV4_1_FILES && layouttype !=
7164 	     NFSLAYOUT_FLEXFILE) ||
7165 	    (nfsrv_maxpnfsmirror > 1 && layouttype == NFSLAYOUT_NFSV4_1_FILES))
7166 		return (NFSERR_UNKNLAYOUTTYPE);
7167 
7168 	/*
7169 	 * Now, search for the device id.  Note that the structures won't go
7170 	 * away, but the order changes in the list.  As such, the lock only
7171 	 * needs to be held during the search through the list.
7172 	 */
7173 	NFSDDSLOCK();
7174 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7175 		if (NFSBCMP(devid, ds->nfsdev_deviceid, NFSX_V4DEVICEID) == 0 &&
7176 		    ds->nfsdev_nmp != NULL)
7177 			break;
7178 	}
7179 	NFSDDSUNLOCK();
7180 	if (ds == NULL)
7181 		return (NFSERR_NOENT);
7182 
7183 	/* If the correct nfsdev_XXXXaddrlen is > 0, we have the device info. */
7184 	*devaddrlen = 0;
7185 	if (layouttype == NFSLAYOUT_NFSV4_1_FILES) {
7186 		*devaddrlen = ds->nfsdev_fileaddrlen;
7187 		*devaddr = ds->nfsdev_fileaddr;
7188 	} else if (layouttype == NFSLAYOUT_FLEXFILE) {
7189 		*devaddrlen = ds->nfsdev_flexaddrlen;
7190 		*devaddr = ds->nfsdev_flexaddr;
7191 	}
7192 	if (*devaddrlen == 0)
7193 		return (NFSERR_UNKNLAYOUTTYPE);
7194 
7195 	/*
7196 	 * The XDR overhead is 3 unsigned values: layout_type,
7197 	 * length_of_address and notify bitmap.
7198 	 * If the notify array is changed to not all zeros, the
7199 	 * count of unsigned values must be increased.
7200 	 */
7201 	if (*maxcnt > 0 && *maxcnt < NFSM_RNDUP(*devaddrlen) +
7202 	    3 * NFSX_UNSIGNED) {
7203 		*maxcnt = NFSM_RNDUP(*devaddrlen) + 3 * NFSX_UNSIGNED;
7204 		return (NFSERR_TOOSMALL);
7205 	}
7206 	return (0);
7207 }
7208 
7209 /*
7210  * Free a list of layout state structures.
7211  */
7212 static void
7213 nfsrv_freelayoutlist(nfsquad_t clientid)
7214 {
7215 	struct nfslayouthash *lhyp;
7216 	struct nfslayout *lyp, *nlyp;
7217 	int i;
7218 
7219 	for (i = 0; i < nfsrv_layouthashsize; i++) {
7220 		lhyp = &nfslayouthash[i];
7221 		NFSLOCKLAYOUT(lhyp);
7222 		TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7223 			if (lyp->lay_clientid.qval == clientid.qval)
7224 				nfsrv_freelayout(&lhyp->list, lyp);
7225 		}
7226 		NFSUNLOCKLAYOUT(lhyp);
7227 	}
7228 }
7229 
7230 /*
7231  * Free up a layout.
7232  */
7233 static void
7234 nfsrv_freelayout(struct nfslayouthead *lhp, struct nfslayout *lyp)
7235 {
7236 
7237 	NFSD_DEBUG(4, "Freelayout=%p\n", lyp);
7238 	atomic_add_int(&nfsrv_layoutcnt, -1);
7239 	TAILQ_REMOVE(lhp, lyp, lay_list);
7240 	free(lyp, M_NFSDSTATE);
7241 }
7242 
7243 /*
7244  * Free up a device id.
7245  */
7246 void
7247 nfsrv_freeonedevid(struct nfsdevice *ds)
7248 {
7249 	int i;
7250 
7251 	atomic_add_int(&nfsrv_devidcnt, -1);
7252 	vrele(ds->nfsdev_dvp);
7253 	for (i = 0; i < nfsrv_dsdirsize; i++)
7254 		if (ds->nfsdev_dsdir[i] != NULL)
7255 			vrele(ds->nfsdev_dsdir[i]);
7256 	free(ds->nfsdev_fileaddr, M_NFSDSTATE);
7257 	free(ds->nfsdev_flexaddr, M_NFSDSTATE);
7258 	free(ds->nfsdev_host, M_NFSDSTATE);
7259 	free(ds, M_NFSDSTATE);
7260 }
7261 
7262 /*
7263  * Free up a device id and its mirrors.
7264  */
7265 static void
7266 nfsrv_freedevid(struct nfsdevice *ds)
7267 {
7268 
7269 	TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
7270 	nfsrv_freeonedevid(ds);
7271 }
7272 
7273 /*
7274  * Free all layouts and device ids.
7275  * Done when the nfsd threads are shut down since there may be a new
7276  * modified device id list created when the nfsd is restarted.
7277  */
7278 void
7279 nfsrv_freealllayoutsanddevids(void)
7280 {
7281 	struct nfsdontlist *mrp, *nmrp;
7282 	struct nfslayout *lyp, *nlyp;
7283 
7284 	/* Get rid of the deviceid structures. */
7285 	nfsrv_freealldevids();
7286 	TAILQ_INIT(&nfsrv_devidhead);
7287 	nfsrv_devidcnt = 0;
7288 
7289 	/* Get rid of all layouts. */
7290 	nfsrv_freealllayouts();
7291 
7292 	/* Get rid of any nfsdontlist entries. */
7293 	LIST_FOREACH_SAFE(mrp, &nfsrv_dontlisthead, nfsmr_list, nmrp)
7294 		free(mrp, M_NFSDSTATE);
7295 	LIST_INIT(&nfsrv_dontlisthead);
7296 	nfsrv_dontlistlen = 0;
7297 
7298 	/* Free layouts in the recall list. */
7299 	TAILQ_FOREACH_SAFE(lyp, &nfsrv_recalllisthead, lay_list, nlyp)
7300 		nfsrv_freelayout(&nfsrv_recalllisthead, lyp);
7301 	TAILQ_INIT(&nfsrv_recalllisthead);
7302 }
7303 
7304 /*
7305  * Free layouts that match the arguments.
7306  */
7307 static void
7308 nfsrv_freelayouts(nfsquad_t *clid, fsid_t *fs, int laytype, int iomode)
7309 {
7310 	struct nfslayouthash *lhyp;
7311 	struct nfslayout *lyp, *nlyp;
7312 	int i;
7313 
7314 	for (i = 0; i < nfsrv_layouthashsize; i++) {
7315 		lhyp = &nfslayouthash[i];
7316 		NFSLOCKLAYOUT(lhyp);
7317 		TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7318 			if (clid->qval != lyp->lay_clientid.qval)
7319 				continue;
7320 			if (fs != NULL && (fs->val[0] != lyp->lay_fsid.val[0] ||
7321 			    fs->val[1] != lyp->lay_fsid.val[1]))
7322 				continue;
7323 			if (laytype != lyp->lay_type)
7324 				continue;
7325 			if ((iomode & NFSLAYOUTIOMODE_READ) != 0)
7326 				lyp->lay_flags &= ~NFSLAY_READ;
7327 			if ((iomode & NFSLAYOUTIOMODE_RW) != 0)
7328 				lyp->lay_flags &= ~NFSLAY_RW;
7329 			if ((lyp->lay_flags & (NFSLAY_READ | NFSLAY_RW)) == 0)
7330 				nfsrv_freelayout(&lhyp->list, lyp);
7331 		}
7332 		NFSUNLOCKLAYOUT(lhyp);
7333 	}
7334 }
7335 
7336 /*
7337  * Free all layouts for the argument file.
7338  */
7339 void
7340 nfsrv_freefilelayouts(fhandle_t *fhp)
7341 {
7342 	struct nfslayouthash *lhyp;
7343 	struct nfslayout *lyp, *nlyp;
7344 
7345 	lhyp = NFSLAYOUTHASH(fhp);
7346 	NFSLOCKLAYOUT(lhyp);
7347 	TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
7348 		if (NFSBCMP(&lyp->lay_fh, fhp, sizeof(*fhp)) == 0)
7349 			nfsrv_freelayout(&lhyp->list, lyp);
7350 	}
7351 	NFSUNLOCKLAYOUT(lhyp);
7352 }
7353 
7354 /*
7355  * Free all layouts.
7356  */
7357 static void
7358 nfsrv_freealllayouts(void)
7359 {
7360 	struct nfslayouthash *lhyp;
7361 	struct nfslayout *lyp, *nlyp;
7362 	int i;
7363 
7364 	for (i = 0; i < nfsrv_layouthashsize; i++) {
7365 		lhyp = &nfslayouthash[i];
7366 		NFSLOCKLAYOUT(lhyp);
7367 		TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp)
7368 			nfsrv_freelayout(&lhyp->list, lyp);
7369 		NFSUNLOCKLAYOUT(lhyp);
7370 	}
7371 }
7372 
7373 /*
7374  * Look up the mount path for the DS server.
7375  */
7376 static int
7377 nfsrv_setdsserver(char *dspathp, char *mdspathp, NFSPROC_T *p,
7378     struct nfsdevice **dsp)
7379 {
7380 	struct nameidata nd;
7381 	struct nfsdevice *ds;
7382 	struct mount *mp;
7383 	int error, i;
7384 	char *dsdirpath;
7385 	size_t dsdirsize;
7386 
7387 	NFSD_DEBUG(4, "setdssrv path=%s\n", dspathp);
7388 	*dsp = NULL;
7389 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
7390 	    dspathp, p);
7391 	error = namei(&nd);
7392 	NFSD_DEBUG(4, "lookup=%d\n", error);
7393 	if (error != 0)
7394 		return (error);
7395 	if (nd.ni_vp->v_type != VDIR) {
7396 		vput(nd.ni_vp);
7397 		NFSD_DEBUG(4, "dspath not dir\n");
7398 		return (ENOTDIR);
7399 	}
7400 	if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7401 		vput(nd.ni_vp);
7402 		NFSD_DEBUG(4, "dspath not an NFS mount\n");
7403 		return (ENXIO);
7404 	}
7405 
7406 	/*
7407 	 * Allocate a DS server structure with the NFS mounted directory
7408 	 * vnode reference counted, so that a non-forced dismount will
7409 	 * fail with EBUSY.
7410 	 * This structure is always linked into the list, even if an error
7411 	 * is being returned.  The caller will free the entire list upon
7412 	 * an error return.
7413 	 */
7414 	*dsp = ds = malloc(sizeof(*ds) + nfsrv_dsdirsize * sizeof(vnode_t),
7415 	    M_NFSDSTATE, M_WAITOK | M_ZERO);
7416 	ds->nfsdev_dvp = nd.ni_vp;
7417 	ds->nfsdev_nmp = VFSTONFS(nd.ni_vp->v_mount);
7418 	NFSVOPUNLOCK(nd.ni_vp, 0);
7419 
7420 	dsdirsize = strlen(dspathp) + 16;
7421 	dsdirpath = malloc(dsdirsize, M_TEMP, M_WAITOK);
7422 	/* Now, create the DS directory structures. */
7423 	for (i = 0; i < nfsrv_dsdirsize; i++) {
7424 		snprintf(dsdirpath, dsdirsize, "%s/ds%d", dspathp, i);
7425 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7426 		    UIO_SYSSPACE, dsdirpath, p);
7427 		error = namei(&nd);
7428 		NFSD_DEBUG(4, "dsdirpath=%s lookup=%d\n", dsdirpath, error);
7429 		if (error != 0)
7430 			break;
7431 		if (nd.ni_vp->v_type != VDIR) {
7432 			vput(nd.ni_vp);
7433 			error = ENOTDIR;
7434 			NFSD_DEBUG(4, "dsdirpath not a VDIR\n");
7435 			break;
7436 		}
7437 		if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
7438 			vput(nd.ni_vp);
7439 			error = ENXIO;
7440 			NFSD_DEBUG(4, "dsdirpath not an NFS mount\n");
7441 			break;
7442 		}
7443 		ds->nfsdev_dsdir[i] = nd.ni_vp;
7444 		NFSVOPUNLOCK(nd.ni_vp, 0);
7445 	}
7446 	free(dsdirpath, M_TEMP);
7447 
7448 	if (strlen(mdspathp) > 0) {
7449 		/*
7450 		 * This DS stores file for a specific MDS exported file
7451 		 * system.
7452 		 */
7453 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
7454 		    UIO_SYSSPACE, mdspathp, p);
7455 		error = namei(&nd);
7456 		NFSD_DEBUG(4, "mds lookup=%d\n", error);
7457 		if (error != 0)
7458 			goto out;
7459 		if (nd.ni_vp->v_type != VDIR) {
7460 			vput(nd.ni_vp);
7461 			error = ENOTDIR;
7462 			NFSD_DEBUG(4, "mdspath not dir\n");
7463 			goto out;
7464 		}
7465 		mp = nd.ni_vp->v_mount;
7466 		if ((mp->mnt_flag & MNT_EXPORTED) == 0) {
7467 			vput(nd.ni_vp);
7468 			error = ENXIO;
7469 			NFSD_DEBUG(4, "mdspath not an exported fs\n");
7470 			goto out;
7471 		}
7472 		ds->nfsdev_mdsfsid = mp->mnt_stat.f_fsid;
7473 		ds->nfsdev_mdsisset = 1;
7474 		vput(nd.ni_vp);
7475 	}
7476 
7477 out:
7478 	TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
7479 	atomic_add_int(&nfsrv_devidcnt, 1);
7480 	return (error);
7481 }
7482 
7483 /*
7484  * Look up the mount path for the DS server and delete it.
7485  */
7486 int
7487 nfsrv_deldsserver(int op, char *dspathp, NFSPROC_T *p)
7488 {
7489 	struct mount *mp;
7490 	struct nfsmount *nmp;
7491 	struct nfsdevice *ds;
7492 	int error;
7493 
7494 	NFSD_DEBUG(4, "deldssrv path=%s\n", dspathp);
7495 	/*
7496 	 * Search for the path in the mount list.  Avoid looking the path
7497 	 * up, since this mount point may be hung, with associated locked
7498 	 * vnodes, etc.
7499 	 * Set NFSMNTP_CANCELRPCS so that any forced dismount will be blocked
7500 	 * until this completes.
7501 	 * As noted in the man page, this should be done before any forced
7502 	 * dismount on the mount point, but at least the handshake on
7503 	 * NFSMNTP_CANCELRPCS should make it safe.
7504 	 */
7505 	error = 0;
7506 	ds = NULL;
7507 	nmp = NULL;
7508 	mtx_lock(&mountlist_mtx);
7509 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
7510 		if (strcmp(mp->mnt_stat.f_mntonname, dspathp) == 0 &&
7511 		    strcmp(mp->mnt_stat.f_fstypename, "nfs") == 0 &&
7512 		    mp->mnt_data != NULL) {
7513 			nmp = VFSTONFS(mp);
7514 			NFSLOCKMNT(nmp);
7515 			if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7516 			     NFSMNTP_CANCELRPCS)) == 0) {
7517 				nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7518 				NFSUNLOCKMNT(nmp);
7519 			} else {
7520 				NFSUNLOCKMNT(nmp);
7521 				nmp = NULL;
7522 			}
7523 			break;
7524 		}
7525 	}
7526 	mtx_unlock(&mountlist_mtx);
7527 
7528 	if (nmp != NULL) {
7529 		ds = nfsrv_deldsnmp(op, nmp, p);
7530 		NFSD_DEBUG(4, "deldsnmp=%p\n", ds);
7531 		if (ds != NULL) {
7532 			nfsrv_killrpcs(nmp);
7533 			NFSD_DEBUG(4, "aft killrpcs\n");
7534 		} else
7535 			error = ENXIO;
7536 		NFSLOCKMNT(nmp);
7537 		nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7538 		wakeup(nmp);
7539 		NFSUNLOCKMNT(nmp);
7540 	} else
7541 		error = EINVAL;
7542 	return (error);
7543 }
7544 
7545 /*
7546  * Search for and remove a DS entry which matches the "nmp" argument.
7547  * The nfsdevice structure pointer is returned so that the caller can
7548  * free it via nfsrv_freeonedevid().
7549  * For the forced case, do not try to do LayoutRecalls, since the server
7550  * must be shut down now anyhow.
7551  */
7552 struct nfsdevice *
7553 nfsrv_deldsnmp(int op, struct nfsmount *nmp, NFSPROC_T *p)
7554 {
7555 	struct nfsdevice *fndds;
7556 
7557 	NFSD_DEBUG(4, "deldsdvp\n");
7558 	NFSDDSLOCK();
7559 	if (op == PNFSDOP_FORCEDELDS)
7560 		fndds = nfsv4_findmirror(nmp);
7561 	else
7562 		fndds = nfsrv_findmirroredds(nmp);
7563 	if (fndds != NULL)
7564 		nfsrv_deleteds(fndds);
7565 	NFSDDSUNLOCK();
7566 	if (fndds != NULL) {
7567 		if (op != PNFSDOP_FORCEDELDS)
7568 			nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7569 		printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7570 	}
7571 	return (fndds);
7572 }
7573 
7574 /*
7575  * Similar to nfsrv_deldsnmp(), except that the DS is indicated by deviceid.
7576  * This function also calls nfsrv_killrpcs() to unblock RPCs on the mount
7577  * point.
7578  * Also, returns an error instead of the nfsdevice found.
7579  */
7580 static int
7581 nfsrv_delds(char *devid, NFSPROC_T *p)
7582 {
7583 	struct nfsdevice *ds, *fndds;
7584 	struct nfsmount *nmp;
7585 	int fndmirror;
7586 
7587 	NFSD_DEBUG(4, "delds\n");
7588 	/*
7589 	 * Search the DS server list for a match with devid.
7590 	 * Remove the DS entry if found and there is a mirror.
7591 	 */
7592 	fndds = NULL;
7593 	nmp = NULL;
7594 	fndmirror = 0;
7595 	NFSDDSLOCK();
7596 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7597 		if (NFSBCMP(ds->nfsdev_deviceid, devid, NFSX_V4DEVICEID) == 0 &&
7598 		    ds->nfsdev_nmp != NULL) {
7599 			NFSD_DEBUG(4, "fnd main ds\n");
7600 			fndds = ds;
7601 			break;
7602 		}
7603 	}
7604 	if (fndds == NULL) {
7605 		NFSDDSUNLOCK();
7606 		return (ENXIO);
7607 	}
7608 	if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
7609 		fndmirror = 1;
7610 	else if (fndds->nfsdev_mdsisset != 0) {
7611 		/* For the fsid is set case, search for a mirror. */
7612 		TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7613 			if (ds != fndds && ds->nfsdev_nmp != NULL &&
7614 			    ds->nfsdev_mdsisset != 0 &&
7615 			    ds->nfsdev_mdsfsid.val[0] ==
7616 			    fndds->nfsdev_mdsfsid.val[0] &&
7617 			    ds->nfsdev_mdsfsid.val[1] ==
7618 			    fndds->nfsdev_mdsfsid.val[1]) {
7619 				fndmirror = 1;
7620 				break;
7621 			}
7622 		}
7623 	}
7624 	if (fndmirror != 0) {
7625 		nmp = fndds->nfsdev_nmp;
7626 		NFSLOCKMNT(nmp);
7627 		if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
7628 		     NFSMNTP_CANCELRPCS)) == 0) {
7629 			nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
7630 			NFSUNLOCKMNT(nmp);
7631 			nfsrv_deleteds(fndds);
7632 		} else {
7633 			NFSUNLOCKMNT(nmp);
7634 			nmp = NULL;
7635 		}
7636 	}
7637 	NFSDDSUNLOCK();
7638 	if (nmp != NULL) {
7639 		nfsrv_flexmirrordel(fndds->nfsdev_deviceid, p);
7640 		printf("pNFS server: mirror %s failed\n", fndds->nfsdev_host);
7641 		nfsrv_killrpcs(nmp);
7642 		NFSLOCKMNT(nmp);
7643 		nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
7644 		wakeup(nmp);
7645 		NFSUNLOCKMNT(nmp);
7646 		return (0);
7647 	}
7648 	return (ENXIO);
7649 }
7650 
7651 /*
7652  * Mark a DS as disabled by setting nfsdev_nmp = NULL.
7653  */
7654 static void
7655 nfsrv_deleteds(struct nfsdevice *fndds)
7656 {
7657 
7658 	NFSD_DEBUG(4, "deleteds: deleting a mirror\n");
7659 	fndds->nfsdev_nmp = NULL;
7660 	if (fndds->nfsdev_mdsisset == 0)
7661 		nfsrv_faildscnt--;
7662 }
7663 
7664 /*
7665  * Fill in the addr structures for the File and Flex File layouts.
7666  */
7667 static void
7668 nfsrv_allocdevid(struct nfsdevice *ds, char *addr, char *dnshost)
7669 {
7670 	uint32_t *tl;
7671 	char *netprot;
7672 	int addrlen;
7673 	static uint64_t new_devid = 0;
7674 
7675 	if (strchr(addr, ':') != NULL)
7676 		netprot = "tcp6";
7677 	else
7678 		netprot = "tcp";
7679 
7680 	/* Fill in the device id. */
7681 	NFSBCOPY(&nfsdev_time, ds->nfsdev_deviceid, sizeof(nfsdev_time));
7682 	new_devid++;
7683 	NFSBCOPY(&new_devid, &ds->nfsdev_deviceid[sizeof(nfsdev_time)],
7684 	    sizeof(new_devid));
7685 
7686 	/*
7687 	 * Fill in the file addr (actually the nfsv4_file_layout_ds_addr4
7688 	 * as defined in RFC5661) in XDR.
7689 	 */
7690 	addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7691 	    6 * NFSX_UNSIGNED;
7692 	NFSD_DEBUG(4, "hn=%s addr=%s netprot=%s\n", dnshost, addr, netprot);
7693 	ds->nfsdev_fileaddrlen = addrlen;
7694 	tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7695 	ds->nfsdev_fileaddr = (char *)tl;
7696 	*tl++ = txdr_unsigned(1);		/* One stripe with index 0. */
7697 	*tl++ = 0;
7698 	*tl++ = txdr_unsigned(1);		/* One multipath list */
7699 	*tl++ = txdr_unsigned(1);		/* with one entry in it. */
7700 	/* The netaddr for this one entry. */
7701 	*tl++ = txdr_unsigned(strlen(netprot));
7702 	NFSBCOPY(netprot, tl, strlen(netprot));
7703 	tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
7704 	*tl++ = txdr_unsigned(strlen(addr));
7705 	NFSBCOPY(addr, tl, strlen(addr));
7706 
7707 	/*
7708 	 * Fill in the flex file addr (actually the ff_device_addr4
7709 	 * as defined for Flexible File Layout) in XDR.
7710 	 */
7711 	addrlen = NFSM_RNDUP(strlen(addr)) + NFSM_RNDUP(strlen(netprot)) +
7712 	    9 * NFSX_UNSIGNED;
7713 	ds->nfsdev_flexaddrlen = addrlen;
7714 	tl = malloc(addrlen, M_NFSDSTATE, M_WAITOK | M_ZERO);
7715 	ds->nfsdev_flexaddr = (char *)tl;
7716 	*tl++ = txdr_unsigned(1);		/* One multipath entry. */
7717 	/* The netaddr for this one entry. */
7718 	*tl++ = txdr_unsigned(strlen(netprot));
7719 	NFSBCOPY(netprot, tl, strlen(netprot));
7720 	tl += (NFSM_RNDUP(strlen(netprot)) / NFSX_UNSIGNED);
7721 	*tl++ = txdr_unsigned(strlen(addr));
7722 	NFSBCOPY(addr, tl, strlen(addr));
7723 	tl += (NFSM_RNDUP(strlen(addr)) / NFSX_UNSIGNED);
7724 	*tl++ = txdr_unsigned(1);		/* One NFS Version. */
7725 	*tl++ = txdr_unsigned(NFS_VER4);	/* NFSv4. */
7726 	*tl++ = txdr_unsigned(NFSV41_MINORVERSION); /* Minor version 1. */
7727 	*tl++ = txdr_unsigned(NFS_SRVMAXIO);	/* DS max rsize. */
7728 	*tl++ = txdr_unsigned(NFS_SRVMAXIO);	/* DS max wsize. */
7729 	*tl = newnfs_true;			/* Tightly coupled. */
7730 
7731 	ds->nfsdev_hostnamelen = strlen(dnshost);
7732 	ds->nfsdev_host = malloc(ds->nfsdev_hostnamelen + 1, M_NFSDSTATE,
7733 	    M_WAITOK);
7734 	NFSBCOPY(dnshost, ds->nfsdev_host, ds->nfsdev_hostnamelen + 1);
7735 }
7736 
7737 
7738 /*
7739  * Create the device id list.
7740  * Return 0 if the nfsd threads are to run and ENXIO if the "-p" argument
7741  * is misconfigured.
7742  */
7743 int
7744 nfsrv_createdevids(struct nfsd_nfsd_args *args, NFSPROC_T *p)
7745 {
7746 	struct nfsdevice *ds;
7747 	char *addrp, *dnshostp, *dspathp, *mdspathp;
7748 	int error, i;
7749 
7750 	addrp = args->addr;
7751 	dnshostp = args->dnshost;
7752 	dspathp = args->dspath;
7753 	mdspathp = args->mdspath;
7754 	nfsrv_maxpnfsmirror = args->mirrorcnt;
7755 	if (addrp == NULL || dnshostp == NULL || dspathp == NULL ||
7756 	    mdspathp == NULL)
7757 		return (0);
7758 
7759 	/*
7760 	 * Loop around for each nul-terminated string in args->addr,
7761 	 * args->dnshost, args->dnspath and args->mdspath.
7762 	 */
7763 	while (addrp < (args->addr + args->addrlen) &&
7764 	    dnshostp < (args->dnshost + args->dnshostlen) &&
7765 	    dspathp < (args->dspath + args->dspathlen) &&
7766 	    mdspathp < (args->mdspath + args->mdspathlen)) {
7767 		error = nfsrv_setdsserver(dspathp, mdspathp, p, &ds);
7768 		if (error != 0) {
7769 			/* Free all DS servers. */
7770 			nfsrv_freealldevids();
7771 			nfsrv_devidcnt = 0;
7772 			return (ENXIO);
7773 		}
7774 		nfsrv_allocdevid(ds, addrp, dnshostp);
7775 		addrp += (strlen(addrp) + 1);
7776 		dnshostp += (strlen(dnshostp) + 1);
7777 		dspathp += (strlen(dspathp) + 1);
7778 		mdspathp += (strlen(mdspathp) + 1);
7779 	}
7780 	if (nfsrv_devidcnt < nfsrv_maxpnfsmirror) {
7781 		/* Free all DS servers. */
7782 		nfsrv_freealldevids();
7783 		nfsrv_devidcnt = 0;
7784 		nfsrv_maxpnfsmirror = 1;
7785 		return (ENXIO);
7786 	}
7787 	/* We can fail at most one less DS than the mirror level. */
7788 	nfsrv_faildscnt = nfsrv_maxpnfsmirror - 1;
7789 
7790 	/*
7791 	 * Allocate the nfslayout hash table now, since this is a pNFS server.
7792 	 * Make it 1% of the high water mark and at least 100.
7793 	 */
7794 	if (nfslayouthash == NULL) {
7795 		nfsrv_layouthashsize = nfsrv_layouthighwater / 100;
7796 		if (nfsrv_layouthashsize < 100)
7797 			nfsrv_layouthashsize = 100;
7798 		nfslayouthash = mallocarray(nfsrv_layouthashsize,
7799 		    sizeof(struct nfslayouthash), M_NFSDSESSION, M_WAITOK |
7800 		    M_ZERO);
7801 		for (i = 0; i < nfsrv_layouthashsize; i++) {
7802 			mtx_init(&nfslayouthash[i].mtx, "nfslm", NULL, MTX_DEF);
7803 			TAILQ_INIT(&nfslayouthash[i].list);
7804 		}
7805 	}
7806 	return (0);
7807 }
7808 
7809 /*
7810  * Free all device ids.
7811  */
7812 static void
7813 nfsrv_freealldevids(void)
7814 {
7815 	struct nfsdevice *ds, *nds;
7816 
7817 	TAILQ_FOREACH_SAFE(ds, &nfsrv_devidhead, nfsdev_list, nds)
7818 		nfsrv_freedevid(ds);
7819 }
7820 
7821 /*
7822  * Check to see if there is a Read/Write Layout plus either:
7823  * - A Write Delegation
7824  * or
7825  * - An Open with Write_access.
7826  * Return 1 if this is the case and 0 otherwise.
7827  * This function is used by nfsrv_proxyds() to decide if doing a Proxy
7828  * Getattr RPC to the Data Server (DS) is necessary.
7829  */
7830 #define	NFSCLIDVECSIZE	6
7831 APPLESTATIC int
7832 nfsrv_checkdsattr(struct nfsrv_descript *nd, vnode_t vp, NFSPROC_T *p)
7833 {
7834 	fhandle_t fh, *tfhp;
7835 	struct nfsstate *stp;
7836 	struct nfslayout *lyp;
7837 	struct nfslayouthash *lhyp;
7838 	struct nfslockhashhead *hp;
7839 	struct nfslockfile *lfp;
7840 	nfsquad_t clid[NFSCLIDVECSIZE];
7841 	int clidcnt, ret;
7842 
7843 	ret = nfsvno_getfh(vp, &fh, p);
7844 	if (ret != 0)
7845 		return (0);
7846 
7847 	/* First check for a Read/Write Layout. */
7848 	clidcnt = 0;
7849 	lhyp = NFSLAYOUTHASH(&fh);
7850 	NFSLOCKLAYOUT(lhyp);
7851 	TAILQ_FOREACH(lyp, &lhyp->list, lay_list) {
7852 		if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
7853 		    ((lyp->lay_flags & NFSLAY_RW) != 0 ||
7854 		     ((lyp->lay_flags & NFSLAY_READ) != 0 &&
7855 		      nfsrv_pnfsatime != 0))) {
7856 			if (clidcnt < NFSCLIDVECSIZE)
7857 				clid[clidcnt].qval = lyp->lay_clientid.qval;
7858 			clidcnt++;
7859 		}
7860 	}
7861 	NFSUNLOCKLAYOUT(lhyp);
7862 	if (clidcnt == 0) {
7863 		/* None found, so return 0. */
7864 		return (0);
7865 	}
7866 
7867 	/* Get the nfslockfile for this fh. */
7868 	NFSLOCKSTATE();
7869 	hp = NFSLOCKHASH(&fh);
7870 	LIST_FOREACH(lfp, hp, lf_hash) {
7871 		tfhp = &lfp->lf_fh;
7872 		if (NFSVNO_CMPFH(&fh, tfhp))
7873 			break;
7874 	}
7875 	if (lfp == NULL) {
7876 		/* None found, so return 0. */
7877 		NFSUNLOCKSTATE();
7878 		return (0);
7879 	}
7880 
7881 	/* Now, look for a Write delegation for this clientid. */
7882 	LIST_FOREACH(stp, &lfp->lf_deleg, ls_file) {
7883 		if ((stp->ls_flags & NFSLCK_DELEGWRITE) != 0 &&
7884 		    nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
7885 			break;
7886 	}
7887 	if (stp != NULL) {
7888 		/* Found one, so return 1. */
7889 		NFSUNLOCKSTATE();
7890 		return (1);
7891 	}
7892 
7893 	/* No Write delegation, so look for an Open with Write_access. */
7894 	LIST_FOREACH(stp, &lfp->lf_open, ls_file) {
7895 		KASSERT((stp->ls_flags & NFSLCK_OPEN) != 0,
7896 		    ("nfsrv_checkdsattr: Non-open in Open list\n"));
7897 		if ((stp->ls_flags & NFSLCK_WRITEACCESS) != 0 &&
7898 		    nfsrv_fndclid(clid, stp->ls_clp->lc_clientid, clidcnt) != 0)
7899 			break;
7900 	}
7901 	NFSUNLOCKSTATE();
7902 	if (stp != NULL)
7903 		return (1);
7904 	return (0);
7905 }
7906 
7907 /*
7908  * Look for a matching clientid in the vector. Return 1 if one might match.
7909  */
7910 static int
7911 nfsrv_fndclid(nfsquad_t *clidvec, nfsquad_t clid, int clidcnt)
7912 {
7913 	int i;
7914 
7915 	/* If too many for the vector, return 1 since there might be a match. */
7916 	if (clidcnt > NFSCLIDVECSIZE)
7917 		return (1);
7918 
7919 	for (i = 0; i < clidcnt; i++)
7920 		if (clidvec[i].qval == clid.qval)
7921 			return (1);
7922 	return (0);
7923 }
7924 
7925 /*
7926  * Check the don't list for "vp" and see if issuing an rw layout is allowed.
7927  * Return 1 if issuing an rw layout isn't allowed, 0 otherwise.
7928  */
7929 static int
7930 nfsrv_dontlayout(fhandle_t *fhp)
7931 {
7932 	struct nfsdontlist *mrp;
7933 	int ret;
7934 
7935 	if (nfsrv_dontlistlen == 0)
7936 		return (0);
7937 	ret = 0;
7938 	NFSDDONTLISTLOCK();
7939 	LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
7940 		if (NFSBCMP(fhp, &mrp->nfsmr_fh, sizeof(*fhp)) == 0 &&
7941 		    (mrp->nfsmr_flags & NFSMR_DONTLAYOUT) != 0) {
7942 			ret = 1;
7943 			break;
7944 		}
7945 	}
7946 	NFSDDONTLISTUNLOCK();
7947 	return (ret);
7948 }
7949 
7950 #define	PNFSDS_COPYSIZ	65536
7951 /*
7952  * Create a new file on a DS and copy the contents of an extant DS file to it.
7953  * This can be used for recovery of a DS file onto a recovered DS.
7954  * The steps are:
7955  * - When called, the MDS file's vnode is locked, blocking LayoutGet operations.
7956  * - Disable issuing of read/write layouts for the file via the nfsdontlist,
7957  *   so that they will be disabled after the MDS file's vnode is unlocked.
7958  * - Set up the nfsrv_recalllist so that recall of read/write layouts can
7959  *   be done.
7960  * - Unlock the MDS file's vnode, so that the client(s) can perform proxied
7961  *   writes, LayoutCommits and LayoutReturns for the file when completing the
7962  *   LayoutReturn requested by the LayoutRecall callback.
7963  * - Issue a LayoutRecall callback for all read/write layouts and wait for
7964  *   them to be returned. (If the LayoutRecall callback replies
7965  *   NFSERR_NOMATCHLAYOUT, they are gone and no LayoutReturn is needed.)
7966  * - Exclusively lock the MDS file's vnode.  This ensures that no proxied
7967  *   writes are in progress or can occur during the DS file copy.
7968  *   It also blocks Setattr operations.
7969  * - Create the file on the recovered mirror.
7970  * - Copy the file from the operational DS.
7971  * - Copy any ACL from the MDS file to the new DS file.
7972  * - Set the modify time of the new DS file to that of the MDS file.
7973  * - Update the extended attribute for the MDS file.
7974  * - Enable issuing of rw layouts by deleting the nfsdontlist entry.
7975  * - The caller will unlock the MDS file's vnode allowing operations
7976  *   to continue normally, since it is now on the mirror again.
7977  */
7978 int
7979 nfsrv_copymr(vnode_t vp, vnode_t fvp, vnode_t dvp, struct nfsdevice *ds,
7980     struct pnfsdsfile *pf, struct pnfsdsfile *wpf, int mirrorcnt,
7981     struct ucred *cred, NFSPROC_T *p)
7982 {
7983 	struct nfsdontlist *mrp, *nmrp;
7984 	struct nfslayouthash *lhyp;
7985 	struct nfslayout *lyp, *nlyp;
7986 	struct nfslayouthead thl;
7987 	struct mount *mp;
7988 	struct acl *aclp;
7989 	struct vattr va;
7990 	struct timespec mtime;
7991 	fhandle_t fh;
7992 	vnode_t tvp;
7993 	off_t rdpos, wrpos;
7994 	ssize_t aresid;
7995 	char *dat;
7996 	int didprintf, ret, retacl, xfer;
7997 
7998 	ASSERT_VOP_LOCKED(fvp, "nfsrv_copymr fvp");
7999 	ASSERT_VOP_LOCKED(vp, "nfsrv_copymr vp");
8000 	/*
8001 	 * Allocate a nfsdontlist entry and set the NFSMR_DONTLAYOUT flag
8002 	 * so that no more RW layouts will get issued.
8003 	 */
8004 	ret = nfsvno_getfh(vp, &fh, p);
8005 	if (ret != 0) {
8006 		NFSD_DEBUG(4, "nfsrv_copymr: getfh=%d\n", ret);
8007 		return (ret);
8008 	}
8009 	nmrp = malloc(sizeof(*nmrp), M_NFSDSTATE, M_WAITOK);
8010 	nmrp->nfsmr_flags = NFSMR_DONTLAYOUT;
8011 	NFSBCOPY(&fh, &nmrp->nfsmr_fh, sizeof(fh));
8012 	NFSDDONTLISTLOCK();
8013 	LIST_FOREACH(mrp, &nfsrv_dontlisthead, nfsmr_list) {
8014 		if (NFSBCMP(&fh, &mrp->nfsmr_fh, sizeof(fh)) == 0)
8015 			break;
8016 	}
8017 	if (mrp == NULL) {
8018 		LIST_INSERT_HEAD(&nfsrv_dontlisthead, nmrp, nfsmr_list);
8019 		mrp = nmrp;
8020 		nmrp = NULL;
8021 		nfsrv_dontlistlen++;
8022 		NFSD_DEBUG(4, "nfsrv_copymr: in dontlist\n");
8023 	} else {
8024 		NFSDDONTLISTUNLOCK();
8025 		free(nmrp, M_NFSDSTATE);
8026 		NFSD_DEBUG(4, "nfsrv_copymr: dup dontlist\n");
8027 		return (ENXIO);
8028 	}
8029 	NFSDDONTLISTUNLOCK();
8030 
8031 	/*
8032 	 * Search for all RW layouts for this file.  Move them to the
8033 	 * recall list, so they can be recalled and their return noted.
8034 	 */
8035 	lhyp = NFSLAYOUTHASH(&fh);
8036 	NFSDRECALLLOCK();
8037 	NFSLOCKLAYOUT(lhyp);
8038 	TAILQ_FOREACH_SAFE(lyp, &lhyp->list, lay_list, nlyp) {
8039 		if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8040 		    (lyp->lay_flags & NFSLAY_RW) != 0) {
8041 			TAILQ_REMOVE(&lhyp->list, lyp, lay_list);
8042 			TAILQ_INSERT_HEAD(&nfsrv_recalllisthead, lyp, lay_list);
8043 			lyp->lay_trycnt = 0;
8044 		}
8045 	}
8046 	NFSUNLOCKLAYOUT(lhyp);
8047 	NFSDRECALLUNLOCK();
8048 
8049 	ret = 0;
8050 	didprintf = 0;
8051 	TAILQ_INIT(&thl);
8052 	/* Unlock the MDS vp, so that a LayoutReturn can be done on it. */
8053 	NFSVOPUNLOCK(vp, 0);
8054 	/* Now, do a recall for all layouts not yet recalled. */
8055 tryagain:
8056 	NFSDRECALLLOCK();
8057 	TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8058 		if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0 &&
8059 		    (lyp->lay_flags & NFSLAY_RECALL) == 0) {
8060 			lyp->lay_flags |= NFSLAY_RECALL;
8061 			/*
8062 			 * The layout stateid.seqid needs to be incremented
8063 			 * before doing a LAYOUT_RECALL callback.
8064 			 */
8065 			if (++lyp->lay_stateid.seqid == 0)
8066 				lyp->lay_stateid.seqid = 1;
8067 			NFSDRECALLUNLOCK();
8068 			nfsrv_recalllayout(lyp->lay_clientid, &lyp->lay_stateid,
8069 			    &lyp->lay_fh, lyp, &nfsrv_recalllisthead,
8070 			    lyp->lay_type, p);
8071 			NFSD_DEBUG(4, "nfsrv_copymr: recalled layout\n");
8072 			goto tryagain;
8073 		}
8074 	}
8075 
8076 	/* Now wait for them to be returned. */
8077 tryagain2:
8078 	TAILQ_FOREACH(lyp, &nfsrv_recalllisthead, lay_list) {
8079 		if (NFSBCMP(&lyp->lay_fh, &fh, sizeof(fh)) == 0) {
8080 			if ((lyp->lay_flags & NFSLAY_RETURNED) != 0) {
8081 				TAILQ_REMOVE(&nfsrv_recalllisthead, lyp,
8082 				    lay_list);
8083 				TAILQ_INSERT_HEAD(&thl, lyp, lay_list);
8084 				NFSD_DEBUG(4,
8085 				    "nfsrv_copymr: layout returned\n");
8086 			} else {
8087 				ret = mtx_sleep(lyp, NFSDRECALLMUTEXPTR,
8088 				    PVFS | PCATCH, "nfsmrl", hz);
8089 				NFSD_DEBUG(4, "nfsrv_copymr: aft sleep=%d\n",
8090 				    ret);
8091 				if (ret == EINTR || ret == ERESTART)
8092 					break;
8093 				if ((lyp->lay_flags & NFSLAY_RETURNED) == 0 &&
8094 				    didprintf == 0) {
8095 					printf("nfsrv_copymr: layout not "
8096 					    "returned\n");
8097 					didprintf = 1;
8098 				}
8099 			}
8100 			goto tryagain2;
8101 		}
8102 	}
8103 	NFSDRECALLUNLOCK();
8104 	/* We can now get rid of the layouts that have been returned. */
8105 	TAILQ_FOREACH_SAFE(lyp, &thl, lay_list, nlyp)
8106 		nfsrv_freelayout(&thl, lyp);
8107 
8108 	/*
8109 	 * LK_EXCLUSIVE lock the MDS vnode, so that any
8110 	 * proxied writes through the MDS will be blocked until we have
8111 	 * completed the copy and update of the extended attributes.
8112 	 * This will also ensure that any attributes and ACL will not be
8113 	 * changed until the copy is complete.
8114 	 */
8115 	NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
8116 	if ((vp->v_iflag & VI_DOOMED) != 0) {
8117 		NFSD_DEBUG(4, "nfsrv_copymr: lk_exclusive doomed\n");
8118 		ret = ESTALE;
8119 	}
8120 
8121 	/* Create the data file on the recovered DS. */
8122 	if (ret == 0)
8123 		ret = nfsrv_createdsfile(vp, &fh, pf, dvp, ds, cred, p, &tvp);
8124 
8125 	/* Copy the DS file, if created successfully. */
8126 	if (ret == 0) {
8127 		/*
8128 		 * Get any NFSv4 ACL on the MDS file, so that it can be set
8129 		 * on the new DS file.
8130 		 */
8131 		aclp = acl_alloc(M_WAITOK | M_ZERO);
8132 		retacl = VOP_GETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
8133 		if (retacl != 0 && retacl != ENOATTR)
8134 			NFSD_DEBUG(1, "nfsrv_copymr: vop_getacl=%d\n", retacl);
8135 		dat = malloc(PNFSDS_COPYSIZ, M_TEMP, M_WAITOK);
8136 		/* Malloc a block of 0s used to check for holes. */
8137 		if (nfsrv_zeropnfsdat == NULL)
8138 			nfsrv_zeropnfsdat = malloc(PNFSDS_COPYSIZ, M_TEMP,
8139 			    M_WAITOK | M_ZERO);
8140 		rdpos = wrpos = 0;
8141 		mp = NULL;
8142 		ret = vn_start_write(tvp, &mp, V_WAIT | PCATCH);
8143 		if (ret == 0)
8144 			ret = VOP_GETATTR(fvp, &va, cred);
8145 		aresid = 0;
8146 		while (ret == 0 && aresid == 0) {
8147 			ret = vn_rdwr(UIO_READ, fvp, dat, PNFSDS_COPYSIZ,
8148 			    rdpos, UIO_SYSSPACE, IO_NODELOCKED, cred, NULL,
8149 			    &aresid, p);
8150 			xfer = PNFSDS_COPYSIZ - aresid;
8151 			if (ret == 0 && xfer > 0) {
8152 				rdpos += xfer;
8153 				/*
8154 				 * Skip the write for holes, except for the
8155 				 * last block.
8156 				 */
8157 				if (xfer < PNFSDS_COPYSIZ || rdpos ==
8158 				    va.va_size || NFSBCMP(dat,
8159 				    nfsrv_zeropnfsdat, PNFSDS_COPYSIZ) != 0)
8160 					ret = vn_rdwr(UIO_WRITE, tvp, dat, xfer,
8161 					    wrpos, UIO_SYSSPACE, IO_NODELOCKED,
8162 					    cred, NULL, NULL, p);
8163 				if (ret == 0)
8164 					wrpos += xfer;
8165 			}
8166 		}
8167 
8168 		/* If there is an ACL and the copy succeeded, set the ACL. */
8169 		if (ret == 0 && retacl == 0) {
8170 			ret = VOP_SETACL(tvp, ACL_TYPE_NFS4, aclp, cred, p);
8171 			/*
8172 			 * Don't consider these as errors, since VOP_GETACL()
8173 			 * can return an ACL when they are not actually
8174 			 * supported.  For example, for UFS, VOP_GETACL()
8175 			 * will return a trivial ACL based on the uid/gid/mode
8176 			 * when there is no ACL on the file.
8177 			 * This case should be recognized as a trivial ACL
8178 			 * by UFS's VOP_SETACL() and succeed, but...
8179 			 */
8180 			if (ret == ENOATTR || ret == EOPNOTSUPP || ret == EPERM)
8181 				ret = 0;
8182 		}
8183 
8184 		if (mp != NULL)
8185 			vn_finished_write(mp);
8186 		if (ret == 0)
8187 			ret = VOP_FSYNC(tvp, MNT_WAIT, p);
8188 
8189 		/* Set the DS data file's modify time that of the MDS file. */
8190 		if (ret == 0)
8191 			ret = VOP_GETATTR(vp, &va, cred);
8192 		if (ret == 0) {
8193 			mtime = va.va_mtime;
8194 			VATTR_NULL(&va);
8195 			va.va_mtime = mtime;
8196 			ret = VOP_SETATTR(tvp, &va, cred);
8197 		}
8198 
8199 		vput(tvp);
8200 		acl_free(aclp);
8201 		free(dat, M_TEMP);
8202 	}
8203 
8204 	/* Update the extended attributes for the newly created DS file. */
8205 	if (ret == 0) {
8206 		mp = NULL;
8207 		ret = vn_start_write(vp, &mp, V_WAIT | PCATCH);
8208 		if (ret == 0)
8209 			ret = vn_extattr_set(vp, IO_NODELOCKED,
8210 			    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
8211 			    sizeof(*wpf) * mirrorcnt, (char *)wpf, p);
8212 		if (mp != NULL)
8213 			vn_finished_write(mp);
8214 	}
8215 
8216 	/* Get rid of the dontlist entry, so that Layouts can be issued. */
8217 	NFSDDONTLISTLOCK();
8218 	LIST_REMOVE(mrp, nfsmr_list);
8219 	NFSDDONTLISTUNLOCK();
8220 	free(mrp, M_NFSDSTATE);
8221 	return (ret);
8222 }
8223 
8224 /*
8225  * Create a data storage file on the recovered DS.
8226  */
8227 static int
8228 nfsrv_createdsfile(vnode_t vp, fhandle_t *fhp, struct pnfsdsfile *pf,
8229     vnode_t dvp, struct nfsdevice *ds, struct ucred *cred, NFSPROC_T *p,
8230     vnode_t *tvpp)
8231 {
8232 	struct vattr va, nva;
8233 	int error;
8234 
8235 	/* Make data file name based on FH. */
8236 	error = VOP_GETATTR(vp, &va, cred);
8237 	if (error == 0) {
8238 		/* Set the attributes for "vp" to Setattr the DS vp. */
8239 		VATTR_NULL(&nva);
8240 		nva.va_uid = va.va_uid;
8241 		nva.va_gid = va.va_gid;
8242 		nva.va_mode = va.va_mode;
8243 		nva.va_size = 0;
8244 		VATTR_NULL(&va);
8245 		va.va_type = VREG;
8246 		va.va_mode = nva.va_mode;
8247 		NFSD_DEBUG(4, "nfsrv_dscreatefile: dvp=%p pf=%p\n", dvp, pf);
8248 		error = nfsrv_dscreate(dvp, &va, &nva, fhp, pf, NULL,
8249 		    pf->dsf_filename, cred, p, tvpp);
8250 	}
8251 	return (error);
8252 }
8253 
8254 /*
8255  * Look up the MDS file shared locked, and then get the extended attribute
8256  * to find the extant DS file to be copied to the new mirror.
8257  * If successful, *vpp is set to the MDS file's vp and *nvpp is
8258  * set to a DS data file for the MDS file, both exclusively locked.
8259  * The "buf" argument has the pnfsdsfile structure from the MDS file
8260  * in it and buflen is set to its length.
8261  */
8262 int
8263 nfsrv_mdscopymr(char *mdspathp, char *dspathp, char *curdspathp, char *buf,
8264     int *buflenp, char *fname, NFSPROC_T *p, struct vnode **vpp,
8265     struct vnode **nvpp, struct pnfsdsfile **pfp, struct nfsdevice **dsp,
8266     struct nfsdevice **fdsp)
8267 {
8268 	struct nameidata nd;
8269 	struct vnode *vp, *curvp;
8270 	struct pnfsdsfile *pf;
8271 	struct nfsmount *nmp, *curnmp;
8272 	int dsdir, error, mirrorcnt, ippos;
8273 
8274 	vp = NULL;
8275 	curvp = NULL;
8276 	curnmp = NULL;
8277 	*dsp = NULL;
8278 	*fdsp = NULL;
8279 	if (dspathp == NULL && curdspathp != NULL)
8280 		return (EPERM);
8281 
8282 	/*
8283 	 * Look up the MDS file shared locked.  The lock will be upgraded
8284 	 * to an exclusive lock after any rw layouts have been returned.
8285 	 */
8286 	NFSD_DEBUG(4, "mdsopen path=%s\n", mdspathp);
8287 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF, UIO_SYSSPACE,
8288 	    mdspathp, p);
8289 	error = namei(&nd);
8290 	NFSD_DEBUG(4, "lookup=%d\n", error);
8291 	if (error != 0)
8292 		return (error);
8293 	if (nd.ni_vp->v_type != VREG) {
8294 		vput(nd.ni_vp);
8295 		NFSD_DEBUG(4, "mdspath not reg\n");
8296 		return (EISDIR);
8297 	}
8298 	vp = nd.ni_vp;
8299 
8300 	if (curdspathp != NULL) {
8301 		/*
8302 		 * Look up the current DS path and find the nfsdev structure for
8303 		 * it.
8304 		 */
8305 		NFSD_DEBUG(4, "curmdsdev path=%s\n", curdspathp);
8306 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8307 		    UIO_SYSSPACE, curdspathp, p);
8308 		error = namei(&nd);
8309 		NFSD_DEBUG(4, "ds lookup=%d\n", error);
8310 		if (error != 0) {
8311 			vput(vp);
8312 			return (error);
8313 		}
8314 		if (nd.ni_vp->v_type != VDIR) {
8315 			vput(nd.ni_vp);
8316 			vput(vp);
8317 			NFSD_DEBUG(4, "curdspath not dir\n");
8318 			return (ENOTDIR);
8319 		}
8320 		if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8321 			vput(nd.ni_vp);
8322 			vput(vp);
8323 			NFSD_DEBUG(4, "curdspath not an NFS mount\n");
8324 			return (ENXIO);
8325 		}
8326 		curnmp = VFSTONFS(nd.ni_vp->v_mount);
8327 
8328 		/* Search the nfsdev list for a match. */
8329 		NFSDDSLOCK();
8330 		*fdsp = nfsv4_findmirror(curnmp);
8331 		NFSDDSUNLOCK();
8332 		if (*fdsp == NULL)
8333 			curnmp = NULL;
8334 		if (curnmp == NULL) {
8335 			vput(nd.ni_vp);
8336 			vput(vp);
8337 			NFSD_DEBUG(4, "mdscopymr: no current ds\n");
8338 			return (ENXIO);
8339 		}
8340 		curvp = nd.ni_vp;
8341 	}
8342 
8343 	if (dspathp != NULL) {
8344 		/* Look up the nfsdev path and find the nfsdev structure. */
8345 		NFSD_DEBUG(4, "mdsdev path=%s\n", dspathp);
8346 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
8347 		    UIO_SYSSPACE, dspathp, p);
8348 		error = namei(&nd);
8349 		NFSD_DEBUG(4, "ds lookup=%d\n", error);
8350 		if (error != 0) {
8351 			vput(vp);
8352 			if (curvp != NULL)
8353 				vput(curvp);
8354 			return (error);
8355 		}
8356 		if (nd.ni_vp->v_type != VDIR || nd.ni_vp == curvp) {
8357 			vput(nd.ni_vp);
8358 			vput(vp);
8359 			if (curvp != NULL)
8360 				vput(curvp);
8361 			NFSD_DEBUG(4, "dspath not dir\n");
8362 			if (nd.ni_vp == curvp)
8363 				return (EPERM);
8364 			return (ENOTDIR);
8365 		}
8366 		if (strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, "nfs") != 0) {
8367 			vput(nd.ni_vp);
8368 			vput(vp);
8369 			if (curvp != NULL)
8370 				vput(curvp);
8371 			NFSD_DEBUG(4, "dspath not an NFS mount\n");
8372 			return (ENXIO);
8373 		}
8374 		nmp = VFSTONFS(nd.ni_vp->v_mount);
8375 
8376 		/*
8377 		 * Search the nfsdevice list for a match.  If curnmp == NULL,
8378 		 * this is a recovery and there must be a mirror.
8379 		 */
8380 		NFSDDSLOCK();
8381 		if (curnmp == NULL)
8382 			*dsp = nfsrv_findmirroredds(nmp);
8383 		else
8384 			*dsp = nfsv4_findmirror(nmp);
8385 		NFSDDSUNLOCK();
8386 		if (*dsp == NULL) {
8387 			vput(nd.ni_vp);
8388 			vput(vp);
8389 			if (curvp != NULL)
8390 				vput(curvp);
8391 			NFSD_DEBUG(4, "mdscopymr: no ds\n");
8392 			return (ENXIO);
8393 		}
8394 	} else {
8395 		nd.ni_vp = NULL;
8396 		nmp = NULL;
8397 	}
8398 
8399 	/*
8400 	 * Get a vp for an available DS data file using the extended
8401 	 * attribute on the MDS file.
8402 	 * If there is a valid entry for the new DS in the extended attribute
8403 	 * on the MDS file (as checked via the nmp argument),
8404 	 * nfsrv_dsgetsockmnt() returns EEXIST, so no copying will occur.
8405 	 */
8406 	error = nfsrv_dsgetsockmnt(vp, 0, buf, buflenp, &mirrorcnt, p,
8407 	    NULL, NULL, NULL, fname, nvpp, &nmp, curnmp, &ippos, &dsdir);
8408 	if (curvp != NULL)
8409 		vput(curvp);
8410 	if (nd.ni_vp == NULL) {
8411 		if (error == 0 && nmp != NULL) {
8412 			/* Search the nfsdev list for a match. */
8413 			NFSDDSLOCK();
8414 			*dsp = nfsrv_findmirroredds(nmp);
8415 			NFSDDSUNLOCK();
8416 		}
8417 		if (error == 0 && (nmp == NULL || *dsp == NULL)) {
8418 			if (nvpp != NULL && *nvpp != NULL) {
8419 				vput(*nvpp);
8420 				*nvpp = NULL;
8421 			}
8422 			error = ENXIO;
8423 		}
8424 	} else
8425 		vput(nd.ni_vp);
8426 
8427 	/*
8428 	 * When dspathp != NULL and curdspathp == NULL, this is a recovery
8429 	 * and is only allowed if there is a 0.0.0.0 IP address entry.
8430 	 * When curdspathp != NULL, the ippos will be set to that entry.
8431 	 */
8432 	if (error == 0 && dspathp != NULL && ippos == -1) {
8433 		if (nvpp != NULL && *nvpp != NULL) {
8434 			vput(*nvpp);
8435 			*nvpp = NULL;
8436 		}
8437 		error = ENXIO;
8438 	}
8439 	if (error == 0) {
8440 		*vpp = vp;
8441 
8442 		pf = (struct pnfsdsfile *)buf;
8443 		if (ippos == -1) {
8444 			/* If no zeroip pnfsdsfile, add one. */
8445 			ippos = *buflenp / sizeof(*pf);
8446 			*buflenp += sizeof(*pf);
8447 			pf += ippos;
8448 			pf->dsf_dir = dsdir;
8449 			strlcpy(pf->dsf_filename, fname,
8450 			    sizeof(pf->dsf_filename));
8451 		} else
8452 			pf += ippos;
8453 		*pfp = pf;
8454 	} else
8455 		vput(vp);
8456 	return (error);
8457 }
8458 
8459 /*
8460  * Search for a matching pnfsd mirror device structure, base on the nmp arg.
8461  * Return one if found, NULL otherwise.
8462  */
8463 static struct nfsdevice *
8464 nfsrv_findmirroredds(struct nfsmount *nmp)
8465 {
8466 	struct nfsdevice *ds, *fndds;
8467 	int fndmirror;
8468 
8469 	mtx_assert(NFSDDSMUTEXPTR, MA_OWNED);
8470 	/*
8471 	 * Search the DS server list for a match with nmp.
8472 	 * Remove the DS entry if found and there is a mirror.
8473 	 */
8474 	fndds = NULL;
8475 	fndmirror = 0;
8476 	if (nfsrv_devidcnt == 0)
8477 		return (fndds);
8478 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8479 		if (ds->nfsdev_nmp == nmp) {
8480 			NFSD_DEBUG(4, "nfsrv_findmirroredds: fnd main ds\n");
8481 			fndds = ds;
8482 			break;
8483 		}
8484 	}
8485 	if (fndds == NULL)
8486 		return (fndds);
8487 	if (fndds->nfsdev_mdsisset == 0 && nfsrv_faildscnt > 0)
8488 		fndmirror = 1;
8489 	else if (fndds->nfsdev_mdsisset != 0) {
8490 		/* For the fsid is set case, search for a mirror. */
8491 		TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
8492 			if (ds != fndds && ds->nfsdev_nmp != NULL &&
8493 			    ds->nfsdev_mdsisset != 0 &&
8494 			    ds->nfsdev_mdsfsid.val[0] ==
8495 			    fndds->nfsdev_mdsfsid.val[0] &&
8496 			    ds->nfsdev_mdsfsid.val[1] ==
8497 			    fndds->nfsdev_mdsfsid.val[1]) {
8498 				fndmirror = 1;
8499 				break;
8500 			}
8501 		}
8502 	}
8503 	if (fndmirror == 0) {
8504 		NFSD_DEBUG(4, "nfsrv_findmirroredds: no mirror for DS\n");
8505 		return (NULL);
8506 	}
8507 	return (fndds);
8508 }
8509 
8510