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