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