xref: /freebsd-13-stable/sys/fs/nfsclient/nfs_clstate.c (revision 319b59fbde2fb90bd4e41d1f66aa59e56f66f1fe)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Rick Macklem, University of Guelph
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 /*
32  * These functions implement the client side state handling for NFSv4.
33  * NFSv4 state handling:
34  * - A lockowner is used to determine lock contention, so it
35  *   corresponds directly to a Posix pid. (1 to 1 mapping)
36  * - The correct granularity of an OpenOwner is not nearly so
37  *   obvious. An OpenOwner does the following:
38  *   - provides a serial sequencing of Open/Close/Lock-with-new-lockowner
39  *   - is used to check for Open/Share contention (not applicable to
40  *     this client, since all Opens are Deny_None)
41  *   As such, I considered both extreme.
42  *   1 OpenOwner per ClientID - Simple to manage, but fully serializes
43  *   all Open, Close and Lock (with a new lockowner) Ops.
44  *   1 OpenOwner for each Open - This one results in an OpenConfirm for
45  *   every Open, for most servers.
46  *   So, I chose to use the same mapping as I did for LockOwnwers.
47  *   The main concern here is that you can end up with multiple Opens
48  *   for the same File Handle, but on different OpenOwners (opens
49  *   inherited from parents, grandparents...) and you do not know
50  *   which of these the vnodeop close applies to. This is handled by
51  *   delaying the Close Op(s) until all of the Opens have been closed.
52  *   (It is not yet obvious if this is the correct granularity.)
53  * - How the code handles serialization:
54  *   - For the ClientId, it uses an exclusive lock while getting its
55  *     SetClientId and during recovery. Otherwise, it uses a shared
56  *     lock via a reference count.
57  *   - For the rest of the data structures, it uses an SMP mutex
58  *     (once the nfs client is SMP safe) and doesn't sleep while
59  *     manipulating the linked lists.
60  *   - The serialization of Open/Close/Lock/LockU falls out in the
61  *     "wash", since OpenOwners and LockOwners are both mapped from
62  *     Posix pid. In other words, there is only one Posix pid using
63  *     any given owner, so that owner is serialized. (If you change
64  *     the granularity of the OpenOwner, then code must be added to
65  *     serialize Ops on the OpenOwner.)
66  * - When to get rid of OpenOwners and LockOwners.
67  *   - The function nfscl_cleanup_common() is executed after a process exits.
68  *     It goes through the client list looking for all Open and Lock Owners.
69  *     When one is found, it is marked "defunct" or in the case of
70  *     an OpenOwner without any Opens, freed.
71  *     The renew thread scans for defunct Owners and gets rid of them,
72  *     if it can. The LockOwners will also be deleted when the
73  *     associated Open is closed.
74  *   - If the LockU or Close Op(s) fail during close in a way
75  *     that could be recovered upon retry, they are relinked to the
76  *     ClientId's defunct open list and retried by the renew thread
77  *     until they succeed or an unmount/recovery occurs.
78  *     (Since we are done with them, they do not need to be recovered.)
79  */
80 
81 #include <fs/nfs/nfsport.h>
82 
83 /*
84  * Global variables
85  */
86 extern struct nfsstatsv1 nfsstatsv1;
87 extern struct nfsreqhead nfsd_reqq;
88 extern u_int32_t newnfs_false, newnfs_true;
89 extern int nfscl_debuglevel;
90 extern int nfscl_enablecallb;
91 extern int nfs_numnfscbd;
92 NFSREQSPINLOCK;
93 NFSCLSTATEMUTEX;
94 int nfscl_inited = 0;
95 struct nfsclhead nfsclhead;	/* Head of clientid list */
96 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
97 int nfscl_layouthighwater = NFSCLLAYOUTHIGHWATER;
98 
99 static int nfscl_delegcnt = 0;
100 static int nfscl_layoutcnt = 0;
101 static int nfscl_getopen(struct nfsclownerhead *, struct nfsclopenhash *,
102     u_int8_t *, int, u_int8_t *, u_int8_t *, u_int32_t,
103     struct nfscllockowner **, struct nfsclopen **);
104 static bool nfscl_checkown(struct nfsclowner *, struct nfsclopen *, uint8_t *,
105     uint8_t *, struct nfscllockowner **, struct nfsclopen **,
106     struct nfsclopen **);
107 static void nfscl_clrelease(struct nfsclclient *);
108 static void nfscl_unlinkopen(struct nfsclopen *);
109 static void nfscl_cleanclient(struct nfsclclient *);
110 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
111     struct ucred *, NFSPROC_T *);
112 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
113     struct nfsmount *, struct ucred *, NFSPROC_T *);
114 static void nfscl_recover(struct nfsclclient *, bool *, struct ucred *,
115     NFSPROC_T *);
116 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
117     struct nfscllock *, int);
118 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
119     struct nfscllock **, int);
120 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *,
121     struct nfscldeleghead *);
122 static u_int32_t nfscl_nextcbident(void);
123 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
124 static struct nfsclclient *nfscl_getclnt(u_int32_t);
125 static struct nfsclclient *nfscl_getclntsess(uint8_t *);
126 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
127     int);
128 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
129     int, struct nfsclrecalllayout **, struct nfscllayout **);
130 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
131 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
132     int);
133 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
134 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
135     u_int8_t *, struct nfscllock **);
136 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
137 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
138     struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
139 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
140     struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
141     struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *);
142 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
143     struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
144     struct nfscldeleg *, struct ucred *, NFSPROC_T *);
145 static void nfscl_totalrecall(struct nfsclclient *);
146 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
147     struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
148 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
149     u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
150     struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
151 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
152     int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
153     struct ucred *, NFSPROC_T *);
154 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
155     struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
156 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *,
157     bool);
158 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
159 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
160 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
161     struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int,
162     vnode_t *);
163 static void nfscl_freeopenowner(struct nfsclowner *, int);
164 static void nfscl_cleandeleg(struct nfscldeleg *);
165 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
166     struct nfsmount *, NFSPROC_T *);
167 static void nfscl_emptylockowner(struct nfscllockowner *,
168     struct nfscllockownerfhhead *);
169 static void nfscl_mergeflayouts(struct nfsclflayouthead *,
170     struct nfsclflayouthead *);
171 static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t,
172     uint64_t, uint32_t, uint32_t, uint32_t, char *, struct nfsclrecalllayout *);
173 static int nfscl_seq(uint32_t, uint32_t);
174 static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *,
175     struct ucred *, NFSPROC_T *);
176 static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *,
177     struct ucred *, NFSPROC_T *);
178 
179 static short nfscberr_null[] = {
180 	0,
181 	0,
182 };
183 
184 static short nfscberr_getattr[] = {
185 	NFSERR_RESOURCE,
186 	NFSERR_BADHANDLE,
187 	NFSERR_BADXDR,
188 	NFSERR_RESOURCE,
189 	NFSERR_SERVERFAULT,
190 	0,
191 };
192 
193 static short nfscberr_recall[] = {
194 	NFSERR_RESOURCE,
195 	NFSERR_BADHANDLE,
196 	NFSERR_BADSTATEID,
197 	NFSERR_BADXDR,
198 	NFSERR_RESOURCE,
199 	NFSERR_SERVERFAULT,
200 	0,
201 };
202 
203 static short *nfscl_cberrmap[] = {
204 	nfscberr_null,
205 	nfscberr_null,
206 	nfscberr_null,
207 	nfscberr_getattr,
208 	nfscberr_recall
209 };
210 
211 #define	NETFAMILY(clp) \
212 		(((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
213 
214 /*
215  * Called for an open operation.
216  * If the nfhp argument is NULL, just get an openowner.
217  */
218 int
nfscl_open(vnode_t vp,u_int8_t * nfhp,int fhlen,u_int32_t amode,int usedeleg,struct ucred * cred,NFSPROC_T * p,struct nfsclowner ** owpp,struct nfsclopen ** opp,int * newonep,int * retp,int lockit,bool firstref)219 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
220     struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
221     struct nfsclopen **opp, int *newonep, int *retp, int lockit, bool firstref)
222 {
223 	struct nfsclclient *clp;
224 	struct nfsclowner *owp, *nowp;
225 	struct nfsclopen *op = NULL, *nop = NULL;
226 	struct nfscldeleg *dp;
227 	struct nfsclownerhead *ohp;
228 	u_int8_t own[NFSV4CL_LOCKNAMELEN];
229 	int ret;
230 
231 	if (newonep != NULL)
232 		*newonep = 0;
233 	if (opp != NULL)
234 		*opp = NULL;
235 	if (owpp != NULL)
236 		*owpp = NULL;
237 
238 	/*
239 	 * Might need one or both of these, so MALLOC them now, to
240 	 * avoid a tsleep() in MALLOC later.
241 	 */
242 	nowp = malloc(sizeof (struct nfsclowner),
243 	    M_NFSCLOWNER, M_WAITOK);
244 	if (nfhp != NULL) {
245 	    nop = malloc(sizeof (struct nfsclopen) +
246 		fhlen - 1, M_NFSCLOPEN, M_WAITOK);
247 	    nop->nfso_hash.le_prev = NULL;
248 	}
249 	ret = nfscl_getcl(vp->v_mount, cred, p, false, firstref, &clp);
250 	if (ret != 0) {
251 		free(nowp, M_NFSCLOWNER);
252 		if (nop != NULL)
253 			free(nop, M_NFSCLOPEN);
254 		return (ret);
255 	}
256 
257 	/*
258 	 * Get the Open iff it already exists.
259 	 * If none found, add the new one or return error, depending upon
260 	 * "create".
261 	 */
262 	NFSLOCKCLSTATE();
263 	dp = NULL;
264 	/* First check the delegation list */
265 	if (nfhp != NULL && usedeleg) {
266 		LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
267 			if (dp->nfsdl_fhlen == fhlen &&
268 			    !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
269 				if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
270 				    (dp->nfsdl_flags & NFSCLDL_WRITE))
271 					break;
272 				dp = NULL;
273 				break;
274 			}
275 		}
276 	}
277 
278 	/* For NFSv4.1/4.2 and this option, use a single open_owner. */
279 	if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
280 		nfscl_filllockowner(NULL, own, F_POSIX);
281 	else
282 		nfscl_filllockowner(p->td_proc, own, F_POSIX);
283 	if (dp != NULL)
284 		ohp = &dp->nfsdl_owner;
285 	else
286 		ohp = &clp->nfsc_owner;
287 	/* Now, search for an openowner */
288 	LIST_FOREACH(owp, ohp, nfsow_list) {
289 		if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
290 			break;
291 	}
292 
293 	/*
294 	 * Create a new open, as required.
295 	 */
296 	nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
297 	    cred, newonep);
298 
299 	/*
300 	 * Now, check the mode on the open and return the appropriate
301 	 * value.
302 	 */
303 	if (retp != NULL) {
304 		if (nfhp != NULL && dp != NULL && nop == NULL)
305 			/* new local open on delegation */
306 			*retp = NFSCLOPEN_SETCRED;
307 		else
308 			*retp = NFSCLOPEN_OK;
309 	}
310 	if (op != NULL && (amode & ~(op->nfso_mode))) {
311 		op->nfso_mode |= amode;
312 		if (retp != NULL && dp == NULL)
313 			*retp = NFSCLOPEN_DOOPEN;
314 	}
315 
316 	/*
317 	 * Serialize modifications to the open owner for multiple threads
318 	 * within the same process using a read/write sleep lock.
319 	 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
320 	 * by acquiring a shared lock.  The close operations still use an
321 	 * exclusive lock for this case.
322 	 */
323 	if (lockit != 0) {
324 		if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) {
325 			/*
326 			 * Get a shared lock on the OpenOwner, but first
327 			 * wait for any pending exclusive lock, so that the
328 			 * exclusive locker gets priority.
329 			 */
330 			nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
331 			    NFSCLSTATEMUTEXPTR, NULL);
332 			nfsv4_getref(&owp->nfsow_rwlock, NULL,
333 			    NFSCLSTATEMUTEXPTR, NULL);
334 		} else
335 			nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
336 	}
337 	NFSUNLOCKCLSTATE();
338 	if (nowp != NULL)
339 		free(nowp, M_NFSCLOWNER);
340 	if (nop != NULL)
341 		free(nop, M_NFSCLOPEN);
342 	if (owpp != NULL)
343 		*owpp = owp;
344 	if (opp != NULL)
345 		*opp = op;
346 	return (0);
347 }
348 
349 /*
350  * Create a new open, as required.
351  */
352 static void
nfscl_newopen(struct nfsclclient * clp,struct nfscldeleg * dp,struct nfsclowner ** owpp,struct nfsclowner ** nowpp,struct nfsclopen ** opp,struct nfsclopen ** nopp,u_int8_t * own,u_int8_t * fhp,int fhlen,struct ucred * cred,int * newonep)353 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
354     struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
355     struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
356     struct ucred *cred, int *newonep)
357 {
358 	struct nfsclowner *owp = *owpp, *nowp;
359 	struct nfsclopen *op, *nop;
360 
361 	if (nowpp != NULL)
362 		nowp = *nowpp;
363 	else
364 		nowp = NULL;
365 	if (nopp != NULL)
366 		nop = *nopp;
367 	else
368 		nop = NULL;
369 	if (owp == NULL && nowp != NULL) {
370 		NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
371 		LIST_INIT(&nowp->nfsow_open);
372 		nowp->nfsow_clp = clp;
373 		nowp->nfsow_seqid = 0;
374 		nowp->nfsow_defunct = 0;
375 		nfscl_lockinit(&nowp->nfsow_rwlock);
376 		if (dp != NULL) {
377 			nfsstatsv1.cllocalopenowners++;
378 			LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
379 		} else {
380 			nfsstatsv1.clopenowners++;
381 			LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
382 		}
383 		owp = *owpp = nowp;
384 		*nowpp = NULL;
385 		if (newonep != NULL)
386 			*newonep = 1;
387 	}
388 
389 	 /* If an fhp has been specified, create an Open as well. */
390 	if (fhp != NULL) {
391 		/* and look for the correct open, based upon FH */
392 		LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
393 			if (op->nfso_fhlen == fhlen &&
394 			    !NFSBCMP(op->nfso_fh, fhp, fhlen))
395 				break;
396 		}
397 		if (op == NULL && nop != NULL) {
398 			nop->nfso_own = owp;
399 			nop->nfso_mode = 0;
400 			nop->nfso_opencnt = 0;
401 			nop->nfso_posixlock = 1;
402 			nop->nfso_fhlen = fhlen;
403 			NFSBCOPY(fhp, nop->nfso_fh, fhlen);
404 			LIST_INIT(&nop->nfso_lock);
405 			nop->nfso_stateid.seqid = 0;
406 			nop->nfso_stateid.other[0] = 0;
407 			nop->nfso_stateid.other[1] = 0;
408 			nop->nfso_stateid.other[2] = 0;
409 			KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
410 			newnfs_copyincred(cred, &nop->nfso_cred);
411 			if (dp != NULL) {
412 				TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
413 				TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
414 				    nfsdl_list);
415 				dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
416 				nfsstatsv1.cllocalopens++;
417 			} else {
418 				LIST_INSERT_HEAD(NFSCLOPENHASH(clp, fhp, fhlen),
419 				    nop, nfso_hash);
420 				nfsstatsv1.clopens++;
421 			}
422 			LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
423 			*opp = nop;
424 			*nopp = NULL;
425 			if (newonep != NULL)
426 				*newonep = 1;
427 		} else {
428 			*opp = op;
429 		}
430 	}
431 }
432 
433 /*
434  * Called to find/add a delegation to a client.
435  */
436 int
nfscl_deleg(mount_t mp,struct nfsclclient * clp,u_int8_t * nfhp,int fhlen,struct ucred * cred,NFSPROC_T * p,struct nfscldeleg ** dpp)437 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
438     int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
439 {
440 	struct nfscldeleg *dp = *dpp, *tdp;
441 	struct nfsmount *nmp;
442 
443 	KASSERT(mp != NULL, ("nfscl_deleg: mp NULL"));
444 	nmp = VFSTONFS(mp);
445 	/*
446 	 * First, if we have received a Read delegation for a file on a
447 	 * read/write file system, just return it, because they aren't
448 	 * useful, imho.
449 	 */
450 	if (dp != NULL && !NFSMNT_RDONLY(mp) &&
451 	    (dp->nfsdl_flags & NFSCLDL_READ)) {
452 		nfscl_trydelegreturn(dp, cred, nmp, p);
453 		free(dp, M_NFSCLDELEG);
454 		*dpp = NULL;
455 		return (0);
456 	}
457 
458 	/*
459 	 * Since a delegation might be added to the mount,
460 	 * set NFSMNTP_DELEGISSUED now.  If a delegation already
461 	 * exagain ists, setting this flag is harmless.
462 	 */
463 	NFSLOCKMNT(nmp);
464 	nmp->nm_privflag |= NFSMNTP_DELEGISSUED;
465 	NFSUNLOCKMNT(nmp);
466 
467 	/* Look for the correct deleg, based upon FH */
468 	NFSLOCKCLSTATE();
469 	tdp = nfscl_finddeleg(clp, nfhp, fhlen);
470 	if (tdp == NULL) {
471 		if (dp == NULL) {
472 			NFSUNLOCKCLSTATE();
473 			return (NFSERR_BADSTATEID);
474 		}
475 		*dpp = NULL;
476 		TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
477 		LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
478 		    nfsdl_hash);
479 		dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
480 		nfsstatsv1.cldelegates++;
481 		nfscl_delegcnt++;
482 	} else {
483 		/*
484 		 * Delegation already exists, what do we do if a new one??
485 		 */
486 		if (dp != NULL) {
487 			printf("Deleg already exists!\n");
488 			free(dp, M_NFSCLDELEG);
489 			*dpp = NULL;
490 		} else {
491 			*dpp = tdp;
492 		}
493 	}
494 	NFSUNLOCKCLSTATE();
495 	return (0);
496 }
497 
498 /*
499  * Find a delegation for this file handle. Return NULL upon failure.
500  */
501 static struct nfscldeleg *
nfscl_finddeleg(struct nfsclclient * clp,u_int8_t * fhp,int fhlen)502 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
503 {
504 	struct nfscldeleg *dp;
505 
506 	LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
507 	    if (dp->nfsdl_fhlen == fhlen &&
508 		!NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
509 		break;
510 	}
511 	return (dp);
512 }
513 
514 /*
515  * Get a stateid for an I/O operation. First, look for an open and iff
516  * found, return either a lockowner stateid or the open stateid.
517  * If no Open is found, just return error and the special stateid of all zeros.
518  */
519 int
nfscl_getstateid(vnode_t vp,u_int8_t * nfhp,int fhlen,u_int32_t mode,int fords,struct ucred * cred,NFSPROC_T * p,nfsv4stateid_t * stateidp,void ** lckpp)520 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
521     int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
522     void **lckpp)
523 {
524 	struct nfsclclient *clp;
525 	struct nfsclopen *op = NULL, *top;
526 	struct nfsclopenhash *oph;
527 	struct nfscllockowner *lp;
528 	struct nfscldeleg *dp;
529 	struct nfsnode *np;
530 	struct nfsmount *nmp;
531 	struct nfscred ncr;
532 	u_int8_t own[NFSV4CL_LOCKNAMELEN], lockown[NFSV4CL_LOCKNAMELEN];
533 	int error;
534 	bool done;
535 
536 	*lckpp = NULL;
537 	/*
538 	 * Initially, just set the special stateid of all zeros.
539 	 * (Don't do this for a DS, since the special stateid can't be used.)
540 	 */
541 	if (fords == 0) {
542 		stateidp->seqid = 0;
543 		stateidp->other[0] = 0;
544 		stateidp->other[1] = 0;
545 		stateidp->other[2] = 0;
546 	}
547 	if (vnode_vtype(vp) != VREG)
548 		return (EISDIR);
549 	np = VTONFS(vp);
550 	nmp = VFSTONFS(vp->v_mount);
551 
552 	/*
553 	 * For "oneopenown" mounts, first check for a cached open in the
554 	 * NFS vnode, that can be used as a stateid.  This can only be
555 	 * done if no delegations have been issued to the mount and no
556 	 * byte range file locking has been done for the file.
557 	 */
558 	if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && fords == 0) {
559 		NFSLOCKMNT(nmp);
560 		NFSLOCKNODE(np);
561 		if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
562 		    (np->n_flag & NMIGHTBELOCKED) == 0 &&
563 		    np->n_openstateid != NULL) {
564 			stateidp->seqid = 0;
565 			stateidp->other[0] =
566 			    np->n_openstateid->nfso_stateid.other[0];
567 			stateidp->other[1] =
568 			    np->n_openstateid->nfso_stateid.other[1];
569 			stateidp->other[2] =
570 			    np->n_openstateid->nfso_stateid.other[2];
571 			NFSUNLOCKNODE(np);
572 			NFSUNLOCKMNT(nmp);
573 			return (0);
574 		}
575 		NFSUNLOCKNODE(np);
576 		NFSUNLOCKMNT(nmp);
577 	}
578 
579 	NFSLOCKCLSTATE();
580 	clp = nfscl_findcl(nmp);
581 	if (clp == NULL) {
582 		NFSUNLOCKCLSTATE();
583 		return (EACCES);
584 	}
585 
586 	/*
587 	 * Wait for recovery to complete.
588 	 */
589 	while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
590 		(void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
591 		    PZERO, "nfsrecvr", NULL);
592 
593 	/*
594 	 * First, look for a delegation.
595 	 */
596 	LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
597 		if (dp->nfsdl_fhlen == fhlen &&
598 		    !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
599 			if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
600 			    (dp->nfsdl_flags & NFSCLDL_WRITE)) {
601 				if (NFSHASNFSV4N(nmp))
602 					stateidp->seqid = 0;
603 				else
604 					stateidp->seqid =
605 					    dp->nfsdl_stateid.seqid;
606 				stateidp->other[0] = dp->nfsdl_stateid.other[0];
607 				stateidp->other[1] = dp->nfsdl_stateid.other[1];
608 				stateidp->other[2] = dp->nfsdl_stateid.other[2];
609 				if (!(np->n_flag & NDELEGRECALL)) {
610 					TAILQ_REMOVE(&clp->nfsc_deleg, dp,
611 					    nfsdl_list);
612 					TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
613 					    nfsdl_list);
614 					dp->nfsdl_timestamp = NFSD_MONOSEC +
615 					    120;
616 					dp->nfsdl_rwlock.nfslock_usecnt++;
617 					*lckpp = (void *)&dp->nfsdl_rwlock;
618 				}
619 				NFSUNLOCKCLSTATE();
620 				return (0);
621 			}
622 			break;
623 		}
624 	}
625 
626 	if (p != NULL) {
627 		/*
628 		 * If p != NULL, we want to search the parentage tree
629 		 * for a matching OpenOwner and use that.
630 		 */
631 		if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
632 			nfscl_filllockowner(NULL, own, F_POSIX);
633 		else
634 			nfscl_filllockowner(p->td_proc, own, F_POSIX);
635 		nfscl_filllockowner(p->td_proc, lockown, F_POSIX);
636 		lp = NULL;
637 		error = nfscl_getopen(NULL, clp->nfsc_openhash, nfhp, fhlen,
638 		    own, lockown, mode, &lp, &op);
639 		if (error == 0 && lp != NULL && fords == 0) {
640 			/* Don't return a lock stateid for a DS. */
641 			if (NFSHASNFSV4N(nmp))
642 				stateidp->seqid = 0;
643 			else
644 				stateidp->seqid = lp->nfsl_stateid.seqid;
645 			stateidp->other[0] =
646 			    lp->nfsl_stateid.other[0];
647 			stateidp->other[1] =
648 			    lp->nfsl_stateid.other[1];
649 			stateidp->other[2] =
650 			    lp->nfsl_stateid.other[2];
651 			NFSUNLOCKCLSTATE();
652 			return (0);
653 		}
654 	}
655 	if (op == NULL) {
656 		/* If not found, just look for any OpenOwner that will work. */
657 		top = NULL;
658 		done = false;
659 		oph = NFSCLOPENHASH(clp, nfhp, fhlen);
660 		LIST_FOREACH(op, oph, nfso_hash) {
661 			if (op->nfso_fhlen == fhlen &&
662 			    !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
663 				if (top == NULL && (op->nfso_mode &
664 				    NFSV4OPEN_ACCESSWRITE) != 0 &&
665 				    (mode & NFSV4OPEN_ACCESSREAD) != 0)
666 					top = op;
667 				if ((mode & op->nfso_mode) == mode) {
668 					/* LRU order the hash list. */
669 					LIST_REMOVE(op, nfso_hash);
670 					LIST_INSERT_HEAD(oph, op, nfso_hash);
671 					done = true;
672 					break;
673 				}
674 			}
675 		}
676 		if (!done) {
677 			NFSCL_DEBUG(2, "openmode top=%p\n", top);
678 			if (top == NULL || NFSHASOPENMODE(nmp)) {
679 				NFSUNLOCKCLSTATE();
680 				return (ENOENT);
681 			} else
682 				op = top;
683 		}
684 		/*
685 		 * For read aheads or write behinds, use the open cred.
686 		 * A read ahead or write behind is indicated by p == NULL.
687 		 */
688 		if (p == NULL)
689 			memcpy(&ncr, &op->nfso_cred, sizeof(ncr));
690 	}
691 
692 	/*
693 	 * No lock stateid, so return the open stateid.
694 	 */
695 	if (NFSHASNFSV4N(nmp))
696 		stateidp->seqid = 0;
697 	else
698 		stateidp->seqid = op->nfso_stateid.seqid;
699 	stateidp->other[0] = op->nfso_stateid.other[0];
700 	stateidp->other[1] = op->nfso_stateid.other[1];
701 	stateidp->other[2] = op->nfso_stateid.other[2];
702 	NFSUNLOCKCLSTATE();
703 	if (p == NULL)
704 		newnfs_copycred(&ncr, cred);
705 	return (0);
706 }
707 
708 /*
709  * Search for a matching file, mode and, optionally, lockowner.
710  */
711 static int
nfscl_getopen(struct nfsclownerhead * ohp,struct nfsclopenhash * ohashp,u_int8_t * nfhp,int fhlen,u_int8_t * openown,u_int8_t * lockown,u_int32_t mode,struct nfscllockowner ** lpp,struct nfsclopen ** opp)712 nfscl_getopen(struct nfsclownerhead *ohp, struct nfsclopenhash *ohashp,
713     u_int8_t *nfhp, int fhlen, u_int8_t *openown, u_int8_t *lockown,
714     u_int32_t mode, struct nfscllockowner **lpp, struct nfsclopen **opp)
715 {
716 	struct nfsclowner *owp;
717 	struct nfsclopen *op, *rop, *rop2;
718 	struct nfsclopenhash *oph;
719 	bool keep_looping;
720 
721 	KASSERT(ohp == NULL || ohashp == NULL, ("nfscl_getopen: "
722 	    "only one of ohp and ohashp can be set"));
723 	if (lpp != NULL)
724 		*lpp = NULL;
725 	/*
726 	 * rop will be set to the open to be returned. There are three
727 	 * variants of this, all for an open of the correct file:
728 	 * 1 - A match of lockown.
729 	 * 2 - A match of the openown, when no lockown match exists.
730 	 * 3 - A match for any open, if no openown or lockown match exists.
731 	 * Looking for #2 over #3 probably isn't necessary, but since
732 	 * RFC3530 is vague w.r.t. the relationship between openowners and
733 	 * lockowners, I think this is the safer way to go.
734 	 */
735 	rop = NULL;
736 	rop2 = NULL;
737 	keep_looping = true;
738 	/* Search the client list */
739 	if (ohashp == NULL) {
740 		/* Search the local opens on the delegation. */
741 		LIST_FOREACH(owp, ohp, nfsow_list) {
742 			/* and look for the correct open */
743 			LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
744 				if (op->nfso_fhlen == fhlen &&
745 				    !NFSBCMP(op->nfso_fh, nfhp, fhlen)
746 				    && (op->nfso_mode & mode) == mode)
747 					keep_looping = nfscl_checkown(owp, op, openown,
748 					    lockown, lpp, &rop, &rop2);
749 				if (!keep_looping)
750 					break;
751 			}
752 			if (!keep_looping)
753 				break;
754 		}
755 	} else {
756 		/* Search for matching opens on the hash list. */
757 		oph = &ohashp[NFSCLOPENHASHFUNC(nfhp, fhlen)];
758 		LIST_FOREACH(op, oph, nfso_hash) {
759 			if (op->nfso_fhlen == fhlen &&
760 			    !NFSBCMP(op->nfso_fh, nfhp, fhlen)
761 			    && (op->nfso_mode & mode) == mode)
762 				keep_looping = nfscl_checkown(op->nfso_own, op,
763 				    openown, lockown, lpp, &rop, &rop2);
764 			if (!keep_looping) {
765 				/* LRU order the hash list. */
766 				LIST_REMOVE(op, nfso_hash);
767 				LIST_INSERT_HEAD(oph, op, nfso_hash);
768 				break;
769 			}
770 		}
771 	}
772 	if (rop == NULL)
773 		rop = rop2;
774 	if (rop == NULL)
775 		return (EBADF);
776 	*opp = rop;
777 	return (0);
778 }
779 
780 /* Check for an owner match. */
781 static bool
nfscl_checkown(struct nfsclowner * owp,struct nfsclopen * op,uint8_t * openown,uint8_t * lockown,struct nfscllockowner ** lpp,struct nfsclopen ** ropp,struct nfsclopen ** ropp2)782 nfscl_checkown(struct nfsclowner *owp, struct nfsclopen *op, uint8_t *openown,
783     uint8_t *lockown, struct nfscllockowner **lpp, struct nfsclopen **ropp,
784     struct nfsclopen **ropp2)
785 {
786 	struct nfscllockowner *lp;
787 	bool keep_looping;
788 
789 	keep_looping = true;
790 	if (lpp != NULL) {
791 		/* Now look for a matching lockowner. */
792 		LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
793 			if (!NFSBCMP(lp->nfsl_owner, lockown,
794 			    NFSV4CL_LOCKNAMELEN)) {
795 				*lpp = lp;
796 				*ropp = op;
797 				return (false);
798 			}
799 		}
800 	}
801 	if (*ropp == NULL && !NFSBCMP(owp->nfsow_owner, openown,
802 	    NFSV4CL_LOCKNAMELEN)) {
803 		*ropp = op;
804 		if (lpp == NULL)
805 			keep_looping = false;
806 	}
807 	if (*ropp2 == NULL)
808 		*ropp2 = op;
809 	return (keep_looping);
810 }
811 
812 /*
813  * Release use of an open owner. Called when open operations are done
814  * with the open owner.
815  */
816 void
nfscl_ownerrelease(struct nfsmount * nmp,struct nfsclowner * owp,__unused int error,__unused int candelete,int unlocked)817 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
818     __unused int error, __unused int candelete, int unlocked)
819 {
820 
821 	if (owp == NULL)
822 		return;
823 	NFSLOCKCLSTATE();
824 	if (unlocked == 0) {
825 		if (NFSHASONEOPENOWN(nmp))
826 			nfsv4_relref(&owp->nfsow_rwlock);
827 		else
828 			nfscl_lockunlock(&owp->nfsow_rwlock);
829 	}
830 	nfscl_clrelease(owp->nfsow_clp);
831 	NFSUNLOCKCLSTATE();
832 }
833 
834 /*
835  * Release use of an open structure under an open owner.
836  */
837 void
nfscl_openrelease(struct nfsmount * nmp,struct nfsclopen * op,int error,int candelete)838 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
839     int candelete)
840 {
841 	struct nfsclclient *clp;
842 	struct nfsclowner *owp;
843 
844 	if (op == NULL)
845 		return;
846 	NFSLOCKCLSTATE();
847 	owp = op->nfso_own;
848 	if (NFSHASONEOPENOWN(nmp))
849 		nfsv4_relref(&owp->nfsow_rwlock);
850 	else
851 		nfscl_lockunlock(&owp->nfsow_rwlock);
852 	clp = owp->nfsow_clp;
853 	if (error && candelete && op->nfso_opencnt == 0)
854 		nfscl_freeopen(op, 0, true);
855 	nfscl_clrelease(clp);
856 	NFSUNLOCKCLSTATE();
857 }
858 
859 /*
860  * Called to get a clientid structure. It will optionally lock the
861  * client data structures to do the SetClientId/SetClientId_confirm,
862  * but will release that lock and return the clientid with a reference
863  * count on it.
864  * If the "cred" argument is NULL, a new clientid should not be created.
865  * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
866  * be done.
867  * It always clpp with a reference count on it, unless returning an error.
868  */
869 int
nfscl_getcl(struct mount * mp,struct ucred * cred,NFSPROC_T * p,bool tryminvers,bool firstref,struct nfsclclient ** clpp)870 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
871     bool tryminvers, bool firstref, struct nfsclclient **clpp)
872 {
873 	struct nfsclclient *clp;
874 	struct nfsclclient *newclp = NULL;
875 	struct nfsmount *nmp;
876 	char uuid[HOSTUUIDLEN];
877 	int igotlock = 0, error, trystalecnt, clidinusedelay, i;
878 	u_int16_t idlen = 0;
879 
880 	nmp = VFSTONFS(mp);
881 	if (cred != NULL) {
882 		getcredhostuuid(cred, uuid, sizeof uuid);
883 		idlen = strlen(uuid);
884 		if (idlen > 0)
885 			idlen += sizeof (u_int64_t);
886 		else
887 			idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
888 		newclp = malloc(
889 		    sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
890 		    M_WAITOK | M_ZERO);
891 	}
892 	NFSLOCKCLSTATE();
893 	/*
894 	 * If a forced dismount is already in progress, don't
895 	 * allocate a new clientid and get out now. For the case where
896 	 * clp != NULL, this is a harmless optimization.
897 	 */
898 	if (NFSCL_FORCEDISM(mp)) {
899 		NFSUNLOCKCLSTATE();
900 		if (newclp != NULL)
901 			free(newclp, M_NFSCLCLIENT);
902 		return (EBADF);
903 	}
904 	clp = nmp->nm_clp;
905 	if (clp == NULL) {
906 		if (newclp == NULL) {
907 			NFSUNLOCKCLSTATE();
908 			return (EACCES);
909 		}
910 		clp = newclp;
911 		clp->nfsc_idlen = idlen;
912 		LIST_INIT(&clp->nfsc_owner);
913 		TAILQ_INIT(&clp->nfsc_deleg);
914 		TAILQ_INIT(&clp->nfsc_layout);
915 		LIST_INIT(&clp->nfsc_devinfo);
916 		for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
917 			LIST_INIT(&clp->nfsc_deleghash[i]);
918 		for (i = 0; i < NFSCLOPENHASHSIZE; i++)
919 			LIST_INIT(&clp->nfsc_openhash[i]);
920 		for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
921 			LIST_INIT(&clp->nfsc_layouthash[i]);
922 		clp->nfsc_flags = NFSCLFLAGS_INITED;
923 		clp->nfsc_clientidrev = 1;
924 		clp->nfsc_cbident = nfscl_nextcbident();
925 		nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
926 		    clp->nfsc_idlen);
927 		LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
928 		nmp->nm_clp = clp;
929 		clp->nfsc_nmp = nmp;
930 	} else {
931 		if (newclp != NULL)
932 			free(newclp, M_NFSCLCLIENT);
933 	}
934 	while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
935 	    !NFSCL_FORCEDISM(mp))
936 		igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
937 		    NFSCLSTATEMUTEXPTR, mp);
938 	if (igotlock == 0) {
939 		/*
940 		 * Call nfsv4_lock() with "iwantlock == 0" on the firstref so
941 		 * that it will wait for a pending exclusive lock request.
942 		 * This gives the exclusive lock request priority over this
943 		 * shared lock request.
944 		 * An exclusive lock on nfsc_lock is used mainly for server
945 		 * crash recoveries and delegation recalls.
946 		 */
947 		if (firstref)
948 			nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR,
949 			    mp);
950 		nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
951 	}
952 	if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
953 		/*
954 		 * Both nfsv4_lock() and nfsv4_getref() know to check
955 		 * for NFSCL_FORCEDISM() and return without sleeping to
956 		 * wait for the exclusive lock to be released, since it
957 		 * might be held by nfscl_umount() and we need to get out
958 		 * now for that case and not wait until nfscl_umount()
959 		 * releases it.
960 		 */
961 		NFSUNLOCKCLSTATE();
962 		return (EBADF);
963 	}
964 	NFSUNLOCKCLSTATE();
965 
966 	/*
967 	 * If it needs a clientid, do the setclientid now.
968 	 */
969 	if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
970 		if (!igotlock)
971 			panic("nfscl_clget");
972 		if (p == NULL || cred == NULL) {
973 			NFSLOCKCLSTATE();
974 			nfsv4_unlock(&clp->nfsc_lock, 0);
975 			NFSUNLOCKCLSTATE();
976 			return (EACCES);
977 		}
978 		/*
979 		 * If RFC3530 Sec. 14.2.33 is taken literally,
980 		 * NFSERR_CLIDINUSE will be returned persistently for the
981 		 * case where a new mount of the same file system is using
982 		 * a different principal. In practice, NFSERR_CLIDINUSE is
983 		 * only returned when there is outstanding unexpired state
984 		 * on the clientid. As such, try for twice the lease
985 		 * interval, if we know what that is. Otherwise, make a
986 		 * wild ass guess.
987 		 * The case of returning NFSERR_STALECLIENTID is far less
988 		 * likely, but might occur if there is a significant delay
989 		 * between doing the SetClientID and SetClientIDConfirm Ops,
990 		 * such that the server throws away the clientid before
991 		 * receiving the SetClientIDConfirm.
992 		 */
993 		if (clp->nfsc_renew > 0)
994 			clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
995 		else
996 			clidinusedelay = 120;
997 		trystalecnt = 3;
998 		do {
999 			error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
1000 			if (error == NFSERR_STALECLIENTID ||
1001 			    error == NFSERR_STALEDONTRECOVER ||
1002 			    error == NFSERR_BADSESSION ||
1003 			    error == NFSERR_CLIDINUSE) {
1004 				(void) nfs_catnap(PZERO, error, "nfs_setcl");
1005 			} else if (error == NFSERR_MINORVERMISMATCH &&
1006 			    tryminvers) {
1007 				if (nmp->nm_minorvers > 0)
1008 					nmp->nm_minorvers--;
1009 				else
1010 					tryminvers = false;
1011 			}
1012 		} while (((error == NFSERR_STALECLIENTID ||
1013 		     error == NFSERR_BADSESSION ||
1014 		     error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
1015 		    (error == NFSERR_CLIDINUSE && --clidinusedelay > 0) ||
1016 		    (error == NFSERR_MINORVERMISMATCH && tryminvers));
1017 		if (error) {
1018 			NFSLOCKCLSTATE();
1019 			nfsv4_unlock(&clp->nfsc_lock, 0);
1020 			NFSUNLOCKCLSTATE();
1021 			return (error);
1022 		}
1023 		clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
1024 	}
1025 	if (igotlock) {
1026 		NFSLOCKCLSTATE();
1027 		nfsv4_unlock(&clp->nfsc_lock, 1);
1028 		NFSUNLOCKCLSTATE();
1029 	}
1030 
1031 	*clpp = clp;
1032 	return (0);
1033 }
1034 
1035 /*
1036  * Get a reference to a clientid and return it, if valid.
1037  */
1038 struct nfsclclient *
nfscl_findcl(struct nfsmount * nmp)1039 nfscl_findcl(struct nfsmount *nmp)
1040 {
1041 	struct nfsclclient *clp;
1042 
1043 	clp = nmp->nm_clp;
1044 	if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
1045 		return (NULL);
1046 	return (clp);
1047 }
1048 
1049 /*
1050  * Release the clientid structure. It may be locked or reference counted.
1051  */
1052 static void
nfscl_clrelease(struct nfsclclient * clp)1053 nfscl_clrelease(struct nfsclclient *clp)
1054 {
1055 
1056 	if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
1057 		nfsv4_unlock(&clp->nfsc_lock, 0);
1058 	else
1059 		nfsv4_relref(&clp->nfsc_lock);
1060 }
1061 
1062 /*
1063  * External call for nfscl_clrelease.
1064  */
1065 void
nfscl_clientrelease(struct nfsclclient * clp)1066 nfscl_clientrelease(struct nfsclclient *clp)
1067 {
1068 
1069 	NFSLOCKCLSTATE();
1070 	if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
1071 		nfsv4_unlock(&clp->nfsc_lock, 0);
1072 	else
1073 		nfsv4_relref(&clp->nfsc_lock);
1074 	NFSUNLOCKCLSTATE();
1075 }
1076 
1077 /*
1078  * Called when wanting to lock a byte region.
1079  */
1080 int
nfscl_getbytelock(vnode_t vp,u_int64_t off,u_int64_t len,short type,struct ucred * cred,NFSPROC_T * p,struct nfsclclient * rclp,int recovery,void * id,int flags,u_int8_t * rownp,u_int8_t * ropenownp,struct nfscllockowner ** lpp,int * newonep,int * donelocallyp)1081 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1082     short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
1083     int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
1084     struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
1085 {
1086 	struct nfscllockowner *lp;
1087 	struct nfsclopen *op;
1088 	struct nfsclclient *clp;
1089 	struct nfscllockowner *nlp;
1090 	struct nfscllock *nlop, *otherlop;
1091 	struct nfscldeleg *dp = NULL, *ldp = NULL;
1092 	struct nfscllockownerhead *lhp = NULL;
1093 	struct nfsnode *np;
1094 	u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
1095 	u_int8_t *openownp;
1096 	int error = 0, ret, donelocally = 0;
1097 	u_int32_t mode;
1098 
1099 	/* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
1100 	mode = 0;
1101 	np = VTONFS(vp);
1102 	*lpp = NULL;
1103 	lp = NULL;
1104 	*newonep = 0;
1105 	*donelocallyp = 0;
1106 
1107 	/*
1108 	 * Might need these, so MALLOC them now, to
1109 	 * avoid a tsleep() in MALLOC later.
1110 	 */
1111 	nlp = malloc(
1112 	    sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
1113 	otherlop = malloc(
1114 	    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1115 	nlop = malloc(
1116 	    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1117 	nlop->nfslo_type = type;
1118 	nlop->nfslo_first = off;
1119 	if (len == NFS64BITSSET) {
1120 		nlop->nfslo_end = NFS64BITSSET;
1121 	} else {
1122 		nlop->nfslo_end = off + len;
1123 		if (nlop->nfslo_end <= nlop->nfslo_first)
1124 			error = NFSERR_INVAL;
1125 	}
1126 
1127 	if (!error) {
1128 		if (recovery)
1129 			clp = rclp;
1130 		else
1131 			error = nfscl_getcl(vp->v_mount, cred, p, false, true,
1132 			    &clp);
1133 	}
1134 	if (error) {
1135 		free(nlp, M_NFSCLLOCKOWNER);
1136 		free(otherlop, M_NFSCLLOCK);
1137 		free(nlop, M_NFSCLLOCK);
1138 		return (error);
1139 	}
1140 
1141 	op = NULL;
1142 	if (recovery) {
1143 		ownp = rownp;
1144 		openownp = ropenownp;
1145 	} else {
1146 		nfscl_filllockowner(id, own, flags);
1147 		ownp = own;
1148 		if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
1149 			nfscl_filllockowner(NULL, openown, F_POSIX);
1150 		else
1151 			nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1152 		openownp = openown;
1153 	}
1154 	if (!recovery) {
1155 		NFSLOCKCLSTATE();
1156 		/*
1157 		 * First, search for a delegation. If one exists for this file,
1158 		 * the lock can be done locally against it, so long as there
1159 		 * isn't a local lock conflict.
1160 		 */
1161 		ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1162 		    np->n_fhp->nfh_len);
1163 		/* Just sanity check for correct type of delegation */
1164 		if (dp != NULL && ((dp->nfsdl_flags &
1165 		    (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1166 		     (type == F_WRLCK &&
1167 		      (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1168 			dp = NULL;
1169 	}
1170 	if (dp != NULL) {
1171 		/* Now, find an open and maybe a lockowner. */
1172 		ret = nfscl_getopen(&dp->nfsdl_owner, NULL, np->n_fhp->nfh_fh,
1173 		    np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1174 		if (ret)
1175 			ret = nfscl_getopen(NULL, clp->nfsc_openhash,
1176 			    np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1177 			    ownp, mode, NULL, &op);
1178 		if (!ret) {
1179 			lhp = &dp->nfsdl_lock;
1180 			TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1181 			TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1182 			dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1183 			donelocally = 1;
1184 		} else {
1185 			dp = NULL;
1186 		}
1187 	}
1188 	if (!donelocally) {
1189 		/*
1190 		 * Get the related Open and maybe lockowner.
1191 		 */
1192 		error = nfscl_getopen(NULL, clp->nfsc_openhash,
1193 		    np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1194 		    ownp, mode, &lp, &op);
1195 		if (!error)
1196 			lhp = &op->nfso_lock;
1197 	}
1198 	if (!error && !recovery)
1199 		error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1200 		    np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1201 	if (error) {
1202 		if (!recovery) {
1203 			nfscl_clrelease(clp);
1204 			NFSUNLOCKCLSTATE();
1205 		}
1206 		free(nlp, M_NFSCLLOCKOWNER);
1207 		free(otherlop, M_NFSCLLOCK);
1208 		free(nlop, M_NFSCLLOCK);
1209 		return (error);
1210 	}
1211 
1212 	/*
1213 	 * Ok, see if a lockowner exists and create one, as required.
1214 	 */
1215 	if (lp == NULL)
1216 		LIST_FOREACH(lp, lhp, nfsl_list) {
1217 			if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1218 				break;
1219 		}
1220 	if (lp == NULL) {
1221 		NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1222 		if (recovery)
1223 			NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1224 			    NFSV4CL_LOCKNAMELEN);
1225 		else
1226 			NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1227 			    NFSV4CL_LOCKNAMELEN);
1228 		nlp->nfsl_seqid = 0;
1229 		nlp->nfsl_lockflags = flags;
1230 		nlp->nfsl_inprog = NULL;
1231 		nfscl_lockinit(&nlp->nfsl_rwlock);
1232 		LIST_INIT(&nlp->nfsl_lock);
1233 		if (donelocally) {
1234 			nlp->nfsl_open = NULL;
1235 			nfsstatsv1.cllocallockowners++;
1236 		} else {
1237 			nlp->nfsl_open = op;
1238 			nfsstatsv1.cllockowners++;
1239 		}
1240 		LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1241 		lp = nlp;
1242 		nlp = NULL;
1243 		*newonep = 1;
1244 	}
1245 
1246 	/*
1247 	 * Now, update the byte ranges for locks.
1248 	 */
1249 	ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1250 	if (!ret)
1251 		donelocally = 1;
1252 	if (donelocally) {
1253 		*donelocallyp = 1;
1254 		if (!recovery)
1255 			nfscl_clrelease(clp);
1256 	} else {
1257 		/*
1258 		 * Serial modifications on the lock owner for multiple threads
1259 		 * for the same process using a read/write lock.
1260 		 */
1261 		if (!recovery)
1262 			nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1263 	}
1264 	if (!recovery)
1265 		NFSUNLOCKCLSTATE();
1266 
1267 	if (nlp)
1268 		free(nlp, M_NFSCLLOCKOWNER);
1269 	if (nlop)
1270 		free(nlop, M_NFSCLLOCK);
1271 	if (otherlop)
1272 		free(otherlop, M_NFSCLLOCK);
1273 
1274 	*lpp = lp;
1275 	return (0);
1276 }
1277 
1278 /*
1279  * Called to unlock a byte range, for LockU.
1280  */
1281 int
nfscl_relbytelock(vnode_t vp,u_int64_t off,u_int64_t len,__unused struct ucred * cred,NFSPROC_T * p,int callcnt,struct nfsclclient * clp,void * id,int flags,struct nfscllockowner ** lpp,int * dorpcp)1282 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1283     __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1284     struct nfsclclient *clp, void *id, int flags,
1285     struct nfscllockowner **lpp, int *dorpcp)
1286 {
1287 	struct nfscllockowner *lp;
1288 	struct nfsclopen *op;
1289 	struct nfscllock *nlop, *other_lop = NULL;
1290 	struct nfscldeleg *dp;
1291 	struct nfsnode *np;
1292 	u_int8_t own[NFSV4CL_LOCKNAMELEN];
1293 	int ret = 0, fnd;
1294 
1295 	np = VTONFS(vp);
1296 	*lpp = NULL;
1297 	*dorpcp = 0;
1298 
1299 	/*
1300 	 * Might need these, so MALLOC them now, to
1301 	 * avoid a tsleep() in MALLOC later.
1302 	 */
1303 	nlop = malloc(
1304 	    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1305 	nlop->nfslo_type = F_UNLCK;
1306 	nlop->nfslo_first = off;
1307 	if (len == NFS64BITSSET) {
1308 		nlop->nfslo_end = NFS64BITSSET;
1309 	} else {
1310 		nlop->nfslo_end = off + len;
1311 		if (nlop->nfslo_end <= nlop->nfslo_first) {
1312 			free(nlop, M_NFSCLLOCK);
1313 			return (NFSERR_INVAL);
1314 		}
1315 	}
1316 	if (callcnt == 0) {
1317 		other_lop = malloc(
1318 		    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1319 		*other_lop = *nlop;
1320 	}
1321 	nfscl_filllockowner(id, own, flags);
1322 	dp = NULL;
1323 	NFSLOCKCLSTATE();
1324 	if (callcnt == 0)
1325 		dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1326 		    np->n_fhp->nfh_len);
1327 
1328 	/*
1329 	 * First, unlock any local regions on a delegation.
1330 	 */
1331 	if (dp != NULL) {
1332 		/* Look for this lockowner. */
1333 		LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1334 			if (!NFSBCMP(lp->nfsl_owner, own,
1335 			    NFSV4CL_LOCKNAMELEN))
1336 				break;
1337 		}
1338 		if (lp != NULL)
1339 			/* Use other_lop, so nlop is still available */
1340 			(void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1341 	}
1342 
1343 	/*
1344 	 * Now, find a matching open/lockowner that hasn't already been done,
1345 	 * as marked by nfsl_inprog.
1346 	 */
1347 	lp = NULL;
1348 	fnd = 0;
1349 	LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1350 	    np->n_fhp->nfh_len), nfso_hash) {
1351 		if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1352 		    !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1353 			LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1354 				if (lp->nfsl_inprog == NULL &&
1355 				    !NFSBCMP(lp->nfsl_owner, own,
1356 				     NFSV4CL_LOCKNAMELEN)) {
1357 					fnd = 1;
1358 					break;
1359 				}
1360 			}
1361 		}
1362 		if (fnd)
1363 			break;
1364 	}
1365 
1366 	if (lp != NULL) {
1367 		ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1368 		if (ret)
1369 			*dorpcp = 1;
1370 		/*
1371 		 * Serial modifications on the lock owner for multiple
1372 		 * threads for the same process using a read/write lock.
1373 		 */
1374 		lp->nfsl_inprog = p;
1375 		nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1376 		*lpp = lp;
1377 	}
1378 	NFSUNLOCKCLSTATE();
1379 	if (nlop)
1380 		free(nlop, M_NFSCLLOCK);
1381 	if (other_lop)
1382 		free(other_lop, M_NFSCLLOCK);
1383 	return (0);
1384 }
1385 
1386 /*
1387  * Release all lockowners marked in progess for this process and file.
1388  */
1389 void
nfscl_releasealllocks(struct nfsclclient * clp,vnode_t vp,NFSPROC_T * p,void * id,int flags)1390 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1391     void *id, int flags)
1392 {
1393 	struct nfsclopen *op;
1394 	struct nfscllockowner *lp;
1395 	struct nfsnode *np;
1396 	u_int8_t own[NFSV4CL_LOCKNAMELEN];
1397 
1398 	np = VTONFS(vp);
1399 	nfscl_filllockowner(id, own, flags);
1400 	NFSLOCKCLSTATE();
1401 	LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1402 	    np->n_fhp->nfh_len), nfso_hash) {
1403 		if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1404 		    !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1405 			LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1406 				if (lp->nfsl_inprog == p &&
1407 				    !NFSBCMP(lp->nfsl_owner, own,
1408 				    NFSV4CL_LOCKNAMELEN)) {
1409 					lp->nfsl_inprog = NULL;
1410 					nfscl_lockunlock(&lp->nfsl_rwlock);
1411 				}
1412 			}
1413 		}
1414 	}
1415 	nfscl_clrelease(clp);
1416 	NFSUNLOCKCLSTATE();
1417 }
1418 
1419 /*
1420  * Called to find out if any bytes within the byte range specified are
1421  * write locked by the calling process. Used to determine if flushing
1422  * is required before a LockU.
1423  * If in doubt, return 1, so the flush will occur.
1424  */
1425 int
nfscl_checkwritelocked(vnode_t vp,struct flock * fl,struct ucred * cred,NFSPROC_T * p,void * id,int flags)1426 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1427     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1428 {
1429 	struct nfscllockowner *lp;
1430 	struct nfsclopen *op;
1431 	struct nfsclclient *clp;
1432 	struct nfscllock *lop;
1433 	struct nfscldeleg *dp;
1434 	struct nfsnode *np;
1435 	u_int64_t off, end;
1436 	u_int8_t own[NFSV4CL_LOCKNAMELEN];
1437 	int error = 0;
1438 
1439 	np = VTONFS(vp);
1440 	switch (fl->l_whence) {
1441 	case SEEK_SET:
1442 	case SEEK_CUR:
1443 		/*
1444 		 * Caller is responsible for adding any necessary offset
1445 		 * when SEEK_CUR is used.
1446 		 */
1447 		off = fl->l_start;
1448 		break;
1449 	case SEEK_END:
1450 		off = np->n_size + fl->l_start;
1451 		break;
1452 	default:
1453 		return (1);
1454 	}
1455 	if (fl->l_len != 0) {
1456 		end = off + fl->l_len;
1457 		if (end < off)
1458 			return (1);
1459 	} else {
1460 		end = NFS64BITSSET;
1461 	}
1462 
1463 	error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp);
1464 	if (error)
1465 		return (1);
1466 	nfscl_filllockowner(id, own, flags);
1467 	NFSLOCKCLSTATE();
1468 
1469 	/*
1470 	 * First check the delegation locks.
1471 	 */
1472 	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1473 	if (dp != NULL) {
1474 		LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1475 			if (!NFSBCMP(lp->nfsl_owner, own,
1476 			    NFSV4CL_LOCKNAMELEN))
1477 				break;
1478 		}
1479 		if (lp != NULL) {
1480 			LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1481 				if (lop->nfslo_first >= end)
1482 					break;
1483 				if (lop->nfslo_end <= off)
1484 					continue;
1485 				if (lop->nfslo_type == F_WRLCK) {
1486 					nfscl_clrelease(clp);
1487 					NFSUNLOCKCLSTATE();
1488 					return (1);
1489 				}
1490 			}
1491 		}
1492 	}
1493 
1494 	/*
1495 	 * Now, check state against the server.
1496 	 */
1497 	LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1498 	    np->n_fhp->nfh_len), nfso_hash) {
1499 		if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1500 		    !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1501 			LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1502 				if (!NFSBCMP(lp->nfsl_owner, own,
1503 				    NFSV4CL_LOCKNAMELEN))
1504 					break;
1505 			}
1506 			if (lp != NULL) {
1507 				LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1508 					if (lop->nfslo_first >= end)
1509 						break;
1510 					if (lop->nfslo_end <= off)
1511 						continue;
1512 					if (lop->nfslo_type == F_WRLCK) {
1513 						nfscl_clrelease(clp);
1514 						NFSUNLOCKCLSTATE();
1515 						return (1);
1516 					}
1517 				}
1518 			}
1519 		}
1520 	}
1521 	nfscl_clrelease(clp);
1522 	NFSUNLOCKCLSTATE();
1523 	return (0);
1524 }
1525 
1526 /*
1527  * Release a byte range lock owner structure.
1528  */
1529 void
nfscl_lockrelease(struct nfscllockowner * lp,int error,int candelete)1530 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1531 {
1532 	struct nfsclclient *clp;
1533 
1534 	if (lp == NULL)
1535 		return;
1536 	NFSLOCKCLSTATE();
1537 	clp = lp->nfsl_open->nfso_own->nfsow_clp;
1538 	if (error != 0 && candelete &&
1539 	    (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1540 		nfscl_freelockowner(lp, 0);
1541 	else
1542 		nfscl_lockunlock(&lp->nfsl_rwlock);
1543 	nfscl_clrelease(clp);
1544 	NFSUNLOCKCLSTATE();
1545 }
1546 
1547 /*
1548  * Unlink the open structure.
1549  */
1550 static void
nfscl_unlinkopen(struct nfsclopen * op)1551 nfscl_unlinkopen(struct nfsclopen *op)
1552 {
1553 
1554 	LIST_REMOVE(op, nfso_list);
1555 	if (op->nfso_hash.le_prev != NULL)
1556 		LIST_REMOVE(op, nfso_hash);
1557 }
1558 
1559 /*
1560  * Free up an open structure and any associated byte range lock structures.
1561  */
1562 void
nfscl_freeopen(struct nfsclopen * op,int local,bool unlink)1563 nfscl_freeopen(struct nfsclopen *op, int local, bool unlink)
1564 {
1565 
1566 	if (unlink)
1567 		nfscl_unlinkopen(op);
1568 	nfscl_freealllocks(&op->nfso_lock, local);
1569 	free(op, M_NFSCLOPEN);
1570 	if (local)
1571 		nfsstatsv1.cllocalopens--;
1572 	else
1573 		nfsstatsv1.clopens--;
1574 }
1575 
1576 /*
1577  * Free up all lock owners and associated locks.
1578  */
1579 static void
nfscl_freealllocks(struct nfscllockownerhead * lhp,int local)1580 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1581 {
1582 	struct nfscllockowner *lp, *nlp;
1583 
1584 	LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1585 		if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1586 			panic("nfscllckw");
1587 		nfscl_freelockowner(lp, local);
1588 	}
1589 }
1590 
1591 /*
1592  * Called for an Open when NFSERR_EXPIRED is received from the server.
1593  * If there are no byte range locks nor a Share Deny lost, try to do a
1594  * fresh Open. Otherwise, free the open.
1595  */
1596 static int
nfscl_expireopen(struct nfsclclient * clp,struct nfsclopen * op,struct nfsmount * nmp,struct ucred * cred,NFSPROC_T * p)1597 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1598     struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1599 {
1600 	struct nfscllockowner *lp;
1601 	struct nfscldeleg *dp;
1602 	int mustdelete = 0, error;
1603 
1604 	/*
1605 	 * Look for any byte range lock(s).
1606 	 */
1607 	LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1608 		if (!LIST_EMPTY(&lp->nfsl_lock)) {
1609 			mustdelete = 1;
1610 			break;
1611 		}
1612 	}
1613 
1614 	/*
1615 	 * If no byte range lock(s) nor a Share deny, try to re-open.
1616 	 */
1617 	if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1618 		newnfs_copycred(&op->nfso_cred, cred);
1619 		dp = NULL;
1620 		error = nfsrpc_reopen(nmp, op->nfso_fh,
1621 		    op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1622 		if (error) {
1623 			mustdelete = 1;
1624 			if (dp != NULL) {
1625 				free(dp, M_NFSCLDELEG);
1626 				dp = NULL;
1627 			}
1628 		}
1629 		if (dp != NULL)
1630 			nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1631 			    op->nfso_fhlen, cred, p, &dp);
1632 	}
1633 
1634 	/*
1635 	 * If a byte range lock or Share deny or couldn't re-open, free it.
1636 	 */
1637 	if (mustdelete)
1638 		nfscl_freeopen(op, 0, true);
1639 	return (mustdelete);
1640 }
1641 
1642 /*
1643  * Free up an open owner structure.
1644  */
1645 static void
nfscl_freeopenowner(struct nfsclowner * owp,int local)1646 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1647 {
1648 	int owned;
1649 
1650 	/*
1651 	 * Make sure the NFSCLSTATE mutex is held, to avoid races with
1652 	 * calls in nfscl_renewthread() that do not hold a reference
1653 	 * count on the nfsclclient and just the mutex.
1654 	 * The mutex will not be held for calls done with the exclusive
1655 	 * nfsclclient lock held, in particular, nfscl_hasexpired()
1656 	 * and nfscl_recalldeleg() might do this.
1657 	 */
1658 	owned = mtx_owned(NFSCLSTATEMUTEXPTR);
1659 	if (owned == 0)
1660 		NFSLOCKCLSTATE();
1661 	LIST_REMOVE(owp, nfsow_list);
1662 	if (owned == 0)
1663 		NFSUNLOCKCLSTATE();
1664 	free(owp, M_NFSCLOWNER);
1665 	if (local)
1666 		nfsstatsv1.cllocalopenowners--;
1667 	else
1668 		nfsstatsv1.clopenowners--;
1669 }
1670 
1671 /*
1672  * Free up a byte range lock owner structure.
1673  */
1674 void
nfscl_freelockowner(struct nfscllockowner * lp,int local)1675 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1676 {
1677 	struct nfscllock *lop, *nlop;
1678 	int owned;
1679 
1680 	/*
1681 	 * Make sure the NFSCLSTATE mutex is held, to avoid races with
1682 	 * calls in nfscl_renewthread() that do not hold a reference
1683 	 * count on the nfsclclient and just the mutex.
1684 	 * The mutex will not be held for calls done with the exclusive
1685 	 * nfsclclient lock held, in particular, nfscl_hasexpired()
1686 	 * and nfscl_recalldeleg() might do this.
1687 	 */
1688 	owned = mtx_owned(NFSCLSTATEMUTEXPTR);
1689 	if (owned == 0)
1690 		NFSLOCKCLSTATE();
1691 	LIST_REMOVE(lp, nfsl_list);
1692 	if (owned == 0)
1693 		NFSUNLOCKCLSTATE();
1694 	LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1695 		nfscl_freelock(lop, local);
1696 	}
1697 	free(lp, M_NFSCLLOCKOWNER);
1698 	if (local)
1699 		nfsstatsv1.cllocallockowners--;
1700 	else
1701 		nfsstatsv1.cllockowners--;
1702 }
1703 
1704 /*
1705  * Free up a byte range lock structure.
1706  */
1707 void
nfscl_freelock(struct nfscllock * lop,int local)1708 nfscl_freelock(struct nfscllock *lop, int local)
1709 {
1710 
1711 	LIST_REMOVE(lop, nfslo_list);
1712 	free(lop, M_NFSCLLOCK);
1713 	if (local)
1714 		nfsstatsv1.cllocallocks--;
1715 	else
1716 		nfsstatsv1.cllocks--;
1717 }
1718 
1719 /*
1720  * Clean out the state related to a delegation.
1721  */
1722 static void
nfscl_cleandeleg(struct nfscldeleg * dp)1723 nfscl_cleandeleg(struct nfscldeleg *dp)
1724 {
1725 	struct nfsclowner *owp, *nowp;
1726 	struct nfsclopen *op;
1727 
1728 	LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1729 		op = LIST_FIRST(&owp->nfsow_open);
1730 		if (op != NULL) {
1731 			if (LIST_NEXT(op, nfso_list) != NULL)
1732 				panic("nfscleandel");
1733 			nfscl_freeopen(op, 1, true);
1734 		}
1735 		nfscl_freeopenowner(owp, 1);
1736 	}
1737 	nfscl_freealllocks(&dp->nfsdl_lock, 1);
1738 }
1739 
1740 /*
1741  * Free a delegation.
1742  */
1743 static void
nfscl_freedeleg(struct nfscldeleghead * hdp,struct nfscldeleg * dp,bool freeit)1744 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp, bool freeit)
1745 {
1746 
1747 	TAILQ_REMOVE(hdp, dp, nfsdl_list);
1748 	LIST_REMOVE(dp, nfsdl_hash);
1749 	if (freeit)
1750 		free(dp, M_NFSCLDELEG);
1751 	nfsstatsv1.cldelegates--;
1752 	nfscl_delegcnt--;
1753 }
1754 
1755 /*
1756  * Free up all state related to this client structure.
1757  */
1758 static void
nfscl_cleanclient(struct nfsclclient * clp)1759 nfscl_cleanclient(struct nfsclclient *clp)
1760 {
1761 	struct nfsclowner *owp, *nowp;
1762 	struct nfsclopen *op, *nop;
1763 	struct nfscllayout *lyp, *nlyp;
1764 	struct nfscldevinfo *dip, *ndip;
1765 
1766 	TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1767 		nfscl_freelayout(lyp);
1768 
1769 	LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
1770 		nfscl_freedevinfo(dip);
1771 
1772 	/* Now, all the OpenOwners, etc. */
1773 	LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1774 		LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1775 			nfscl_freeopen(op, 0, true);
1776 		}
1777 		nfscl_freeopenowner(owp, 0);
1778 	}
1779 }
1780 
1781 /*
1782  * Called when an NFSERR_EXPIRED is received from the server.
1783  */
1784 static void
nfscl_expireclient(struct nfsclclient * clp,struct nfsmount * nmp,struct ucred * cred,NFSPROC_T * p)1785 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1786     struct ucred *cred, NFSPROC_T *p)
1787 {
1788 	struct nfsclowner *owp, *nowp, *towp;
1789 	struct nfsclopen *op, *nop, *top;
1790 	struct nfscldeleg *dp, *ndp;
1791 	int ret, printed = 0;
1792 
1793 	/*
1794 	 * First, merge locally issued Opens into the list for the server.
1795 	 */
1796 	dp = TAILQ_FIRST(&clp->nfsc_deleg);
1797 	while (dp != NULL) {
1798 	    ndp = TAILQ_NEXT(dp, nfsdl_list);
1799 	    owp = LIST_FIRST(&dp->nfsdl_owner);
1800 	    while (owp != NULL) {
1801 		nowp = LIST_NEXT(owp, nfsow_list);
1802 		op = LIST_FIRST(&owp->nfsow_open);
1803 		if (op != NULL) {
1804 		    if (LIST_NEXT(op, nfso_list) != NULL)
1805 			panic("nfsclexp");
1806 		    LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1807 			if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1808 			    NFSV4CL_LOCKNAMELEN))
1809 			    break;
1810 		    }
1811 		    if (towp != NULL) {
1812 			/* Merge opens in */
1813 			LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1814 			    if (top->nfso_fhlen == op->nfso_fhlen &&
1815 				!NFSBCMP(top->nfso_fh, op->nfso_fh,
1816 				 op->nfso_fhlen)) {
1817 				top->nfso_mode |= op->nfso_mode;
1818 				top->nfso_opencnt += op->nfso_opencnt;
1819 				break;
1820 			    }
1821 			}
1822 			if (top == NULL) {
1823 			    /* Just add the open to the owner list */
1824 			    LIST_REMOVE(op, nfso_list);
1825 			    op->nfso_own = towp;
1826 			    LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1827 			    LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh,
1828 				op->nfso_fhlen), op, nfso_hash);
1829 			    nfsstatsv1.cllocalopens--;
1830 			    nfsstatsv1.clopens++;
1831 			}
1832 		    } else {
1833 			/* Just add the openowner to the client list */
1834 			LIST_REMOVE(owp, nfsow_list);
1835 			owp->nfsow_clp = clp;
1836 			LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1837 			LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh,
1838 			    op->nfso_fhlen), op, nfso_hash);
1839 			nfsstatsv1.cllocalopenowners--;
1840 			nfsstatsv1.clopenowners++;
1841 			nfsstatsv1.cllocalopens--;
1842 			nfsstatsv1.clopens++;
1843 		    }
1844 		}
1845 		owp = nowp;
1846 	    }
1847 	    if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1848 		printed = 1;
1849 		printf("nfsv4 expired locks lost\n");
1850 	    }
1851 	    nfscl_cleandeleg(dp);
1852 	    nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
1853 	    dp = ndp;
1854 	}
1855 	if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1856 	    panic("nfsclexp");
1857 
1858 	/*
1859 	 * Now, try and reopen against the server.
1860 	 */
1861 	LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1862 		owp->nfsow_seqid = 0;
1863 		LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1864 			ret = nfscl_expireopen(clp, op, nmp, cred, p);
1865 			if (ret && !printed) {
1866 				printed = 1;
1867 				printf("nfsv4 expired locks lost\n");
1868 			}
1869 		}
1870 		if (LIST_EMPTY(&owp->nfsow_open))
1871 			nfscl_freeopenowner(owp, 0);
1872 	}
1873 }
1874 
1875 /*
1876  * This function must be called after the process represented by "own" has
1877  * exited. Must be called with CLSTATE lock held.
1878  */
1879 static void
nfscl_cleanup_common(struct nfsclclient * clp,u_int8_t * own)1880 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1881 {
1882 	struct nfsclowner *owp, *nowp;
1883 	struct nfscllockowner *lp;
1884 	struct nfscldeleg *dp;
1885 
1886 	/* First, get rid of local locks on delegations. */
1887 	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1888 		LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1889 		    if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1890 			if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1891 			    panic("nfscllckw");
1892 			nfscl_freelockowner(lp, 1);
1893 			break;
1894 		    }
1895 		}
1896 	}
1897 	owp = LIST_FIRST(&clp->nfsc_owner);
1898 	while (owp != NULL) {
1899 		nowp = LIST_NEXT(owp, nfsow_list);
1900 		if (!NFSBCMP(owp->nfsow_owner, own,
1901 		    NFSV4CL_LOCKNAMELEN)) {
1902 			/*
1903 			 * If there are children that haven't closed the
1904 			 * file descriptors yet, the opens will still be
1905 			 * here. For that case, let the renew thread clear
1906 			 * out the OpenOwner later.
1907 			 */
1908 			if (LIST_EMPTY(&owp->nfsow_open))
1909 				nfscl_freeopenowner(owp, 0);
1910 			else
1911 				owp->nfsow_defunct = 1;
1912 			break;
1913 		}
1914 		owp = nowp;
1915 	}
1916 }
1917 
1918 /*
1919  * Find open/lock owners for processes that have exited.
1920  */
1921 static void
nfscl_cleanupkext(struct nfsclclient * clp,struct nfscllockownerfhhead * lhp)1922 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1923 {
1924 	struct nfsclowner *owp, *nowp;
1925 	struct nfsclopen *op;
1926 	struct nfscllockowner *lp, *nlp;
1927 	struct nfscldeleg *dp;
1928 	uint8_t own[NFSV4CL_LOCKNAMELEN];
1929 
1930 	/*
1931 	 * All the pidhash locks must be acquired, since they are sx locks
1932 	 * and must be acquired before the mutexes.  The pid(s) that will
1933 	 * be used aren't known yet, so all the locks need to be acquired.
1934 	 * Fortunately, this function is only performed once/sec.
1935 	 */
1936 	pidhash_slockall();
1937 	NFSLOCKCLSTATE();
1938 	LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1939 		LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1940 			LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1941 				if (LIST_EMPTY(&lp->nfsl_lock))
1942 					nfscl_emptylockowner(lp, lhp);
1943 			}
1944 		}
1945 		if (nfscl_procdoesntexist(owp->nfsow_owner)) {
1946 			memcpy(own, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1947 			nfscl_cleanup_common(clp, own);
1948 		}
1949 	}
1950 
1951 	/*
1952 	 * For the single open_owner case, these lock owners need to be
1953 	 * checked to see if they still exist separately.
1954 	 * This is because nfscl_procdoesntexist() never returns true for
1955 	 * the single open_owner so that the above doesn't ever call
1956 	 * nfscl_cleanup_common().
1957 	 */
1958 	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1959 		LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1960 			if (nfscl_procdoesntexist(lp->nfsl_owner)) {
1961 				memcpy(own, lp->nfsl_owner,
1962 				    NFSV4CL_LOCKNAMELEN);
1963 				nfscl_cleanup_common(clp, own);
1964 			}
1965 		}
1966 	}
1967 	NFSUNLOCKCLSTATE();
1968 	pidhash_sunlockall();
1969 }
1970 
1971 /*
1972  * Take the empty lock owner and move it to the local lhp list if the
1973  * associated process no longer exists.
1974  */
1975 static void
nfscl_emptylockowner(struct nfscllockowner * lp,struct nfscllockownerfhhead * lhp)1976 nfscl_emptylockowner(struct nfscllockowner *lp,
1977     struct nfscllockownerfhhead *lhp)
1978 {
1979 	struct nfscllockownerfh *lfhp, *mylfhp;
1980 	struct nfscllockowner *nlp;
1981 	int fnd_it;
1982 
1983 	/* If not a Posix lock owner, just return. */
1984 	if ((lp->nfsl_lockflags & F_POSIX) == 0)
1985 		return;
1986 
1987 	fnd_it = 0;
1988 	mylfhp = NULL;
1989 	/*
1990 	 * First, search to see if this lock owner is already in the list.
1991 	 * If it is, then the associated process no longer exists.
1992 	 */
1993 	SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1994 		if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1995 		    !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
1996 		    lfhp->nfslfh_len))
1997 			mylfhp = lfhp;
1998 		LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
1999 			if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
2000 			    NFSV4CL_LOCKNAMELEN))
2001 				fnd_it = 1;
2002 	}
2003 	/* If not found, check if process still exists. */
2004 	if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
2005 		return;
2006 
2007 	/* Move the lock owner over to the local list. */
2008 	if (mylfhp == NULL) {
2009 		mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
2010 		    M_NOWAIT);
2011 		if (mylfhp == NULL)
2012 			return;
2013 		mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
2014 		NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
2015 		    mylfhp->nfslfh_len);
2016 		LIST_INIT(&mylfhp->nfslfh_lock);
2017 		SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
2018 	}
2019 	LIST_REMOVE(lp, nfsl_list);
2020 	LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
2021 }
2022 
2023 static int	fake_global;	/* Used to force visibility of MNTK_UNMOUNTF */
2024 /*
2025  * Called from nfs umount to free up the clientid.
2026  */
2027 void
nfscl_umount(struct nfsmount * nmp,NFSPROC_T * p,struct nfscldeleghead * dhp)2028 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p, struct nfscldeleghead *dhp)
2029 {
2030 	struct nfsclclient *clp;
2031 	struct ucred *cred;
2032 	int igotlock;
2033 
2034 	/*
2035 	 * For the case that matters, this is the thread that set
2036 	 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
2037 	 * done to ensure that any thread executing nfscl_getcl() after
2038 	 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
2039 	 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
2040 	 * explanation, courtesy of Alan Cox.
2041 	 * What follows is a snippet from Alan Cox's email at:
2042 	 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
2043 	 *
2044 	 * 1. Set MNTK_UNMOUNTF
2045 	 * 2. Acquire a standard FreeBSD mutex "m".
2046 	 * 3. Update some data structures.
2047 	 * 4. Release mutex "m".
2048 	 *
2049 	 * Then, other threads that acquire "m" after step 4 has occurred will
2050 	 * see MNTK_UNMOUNTF as set.  But, other threads that beat thread X to
2051 	 * step 2 may or may not see MNTK_UNMOUNTF as set.
2052 	 */
2053 	NFSLOCKCLSTATE();
2054 	if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
2055 		fake_global++;
2056 		NFSUNLOCKCLSTATE();
2057 		NFSLOCKCLSTATE();
2058 	}
2059 
2060 	clp = nmp->nm_clp;
2061 	if (clp != NULL) {
2062 		if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
2063 			panic("nfscl umount");
2064 
2065 		/*
2066 		 * First, handshake with the nfscl renew thread, to terminate
2067 		 * it.
2068 		 */
2069 		clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
2070 		while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
2071 			(void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
2072 			    "nfsclumnt", hz);
2073 
2074 		/*
2075 		 * Now, get the exclusive lock on the client state, so
2076 		 * that no uses of the state are still in progress.
2077 		 */
2078 		do {
2079 			igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2080 			    NFSCLSTATEMUTEXPTR, NULL);
2081 		} while (!igotlock);
2082 		NFSUNLOCKCLSTATE();
2083 
2084 		/*
2085 		 * Free up all the state. It will expire on the server, but
2086 		 * maybe we should do a SetClientId/SetClientIdConfirm so
2087 		 * the server throws it away?
2088 		 */
2089 		LIST_REMOVE(clp, nfsc_list);
2090 		nfscl_delegreturnall(clp, p, dhp);
2091 		cred = newnfs_getcred();
2092 		if (NFSHASNFSV4N(nmp)) {
2093 			nfsrpc_destroysession(nmp, NULL, cred, p);
2094 			nfsrpc_destroyclient(nmp, clp, cred, p);
2095 		} else
2096 			nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2097 		nfscl_cleanclient(clp);
2098 		nmp->nm_clp = NULL;
2099 		NFSFREECRED(cred);
2100 		free(clp, M_NFSCLCLIENT);
2101 	} else
2102 		NFSUNLOCKCLSTATE();
2103 }
2104 
2105 /*
2106  * This function is called when a server replies with NFSERR_STALECLIENTID
2107  * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
2108  * doing Opens and Locks with reclaim. If these fail, it deletes the
2109  * corresponding state.
2110  */
2111 static void
nfscl_recover(struct nfsclclient * clp,bool * retokp,struct ucred * cred,NFSPROC_T * p)2112 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred,
2113     NFSPROC_T *p)
2114 {
2115 	struct nfsclowner *owp, *nowp;
2116 	struct nfsclopen *op, *nop;
2117 	struct nfscllockowner *lp, *nlp;
2118 	struct nfscllock *lop, *nlop;
2119 	struct nfscldeleg *dp, *ndp, *tdp;
2120 	struct nfsmount *nmp;
2121 	struct ucred *tcred;
2122 	struct nfsclopenhead extra_open;
2123 	struct nfscldeleghead extra_deleg;
2124 	struct nfsreq *rep;
2125 	u_int64_t len;
2126 	u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
2127 	int i, igotlock = 0, error, trycnt, firstlock;
2128 	struct nfscllayout *lyp, *nlyp;
2129 	bool recovered_one;
2130 
2131 	/*
2132 	 * First, lock the client structure, so everyone else will
2133 	 * block when trying to use state.
2134 	 */
2135 	NFSLOCKCLSTATE();
2136 	clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2137 	do {
2138 		igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2139 		    NFSCLSTATEMUTEXPTR, NULL);
2140 	} while (!igotlock);
2141 	NFSUNLOCKCLSTATE();
2142 
2143 	nmp = clp->nfsc_nmp;
2144 	if (nmp == NULL)
2145 		panic("nfscl recover");
2146 
2147 	/*
2148 	 * For now, just get rid of all layouts. There may be a need
2149 	 * to do LayoutCommit Ops with reclaim == true later.
2150 	 */
2151 	TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
2152 		nfscl_freelayout(lyp);
2153 	TAILQ_INIT(&clp->nfsc_layout);
2154 	for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
2155 		LIST_INIT(&clp->nfsc_layouthash[i]);
2156 
2157 	trycnt = 5;
2158 	tcred = NULL;
2159 	do {
2160 		error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p);
2161 	} while ((error == NFSERR_STALECLIENTID ||
2162 	     error == NFSERR_BADSESSION ||
2163 	     error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2164 	if (error) {
2165 		NFSLOCKCLSTATE();
2166 		clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
2167 		    NFSCLFLAGS_RECVRINPROG);
2168 		wakeup(&clp->nfsc_flags);
2169 		nfsv4_unlock(&clp->nfsc_lock, 0);
2170 		NFSUNLOCKCLSTATE();
2171 		return;
2172 	}
2173 	clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2174 	clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2175 
2176 	/*
2177 	 * Mark requests already queued on the server, so that they don't
2178 	 * initiate another recovery cycle. Any requests already in the
2179 	 * queue that handle state information will have the old stale
2180 	 * clientid/stateid and will get a NFSERR_STALESTATEID,
2181 	 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2182 	 * This will be translated to NFSERR_STALEDONTRECOVER when
2183 	 * R_DONTRECOVER is set.
2184 	 */
2185 	NFSLOCKREQ();
2186 	TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2187 		if (rep->r_nmp == nmp)
2188 			rep->r_flags |= R_DONTRECOVER;
2189 	}
2190 	NFSUNLOCKREQ();
2191 
2192 	/*
2193 	 * If nfsrpc_setclient() returns *retokp == true,
2194 	 * no more recovery is needed.
2195 	 */
2196 	if (*retokp)
2197 		goto out;
2198 
2199 	/*
2200 	 * Now, mark all delegations "need reclaim".
2201 	 */
2202 	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2203 		dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2204 
2205 	TAILQ_INIT(&extra_deleg);
2206 	LIST_INIT(&extra_open);
2207 	/*
2208 	 * Now traverse the state lists, doing Open and Lock Reclaims.
2209 	 */
2210 	tcred = newnfs_getcred();
2211 	recovered_one = false;
2212 	owp = LIST_FIRST(&clp->nfsc_owner);
2213 	while (owp != NULL) {
2214 	    nowp = LIST_NEXT(owp, nfsow_list);
2215 	    owp->nfsow_seqid = 0;
2216 	    op = LIST_FIRST(&owp->nfsow_open);
2217 	    while (op != NULL) {
2218 		nop = LIST_NEXT(op, nfso_list);
2219 		if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2220 		    /* Search for a delegation to reclaim with the open */
2221 		    TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2222 			if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2223 			    continue;
2224 			if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2225 			    mode = NFSV4OPEN_ACCESSWRITE;
2226 			    delegtype = NFSV4OPEN_DELEGATEWRITE;
2227 			} else {
2228 			    mode = NFSV4OPEN_ACCESSREAD;
2229 			    delegtype = NFSV4OPEN_DELEGATEREAD;
2230 			}
2231 			if ((op->nfso_mode & mode) == mode &&
2232 			    op->nfso_fhlen == dp->nfsdl_fhlen &&
2233 			    !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2234 			    break;
2235 		    }
2236 		    ndp = dp;
2237 		    if (dp == NULL)
2238 			delegtype = NFSV4OPEN_DELEGATENONE;
2239 		    newnfs_copycred(&op->nfso_cred, tcred);
2240 		    error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2241 			op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2242 			op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2243 			tcred, p);
2244 		    if (!error) {
2245 			recovered_one = true;
2246 			/* Handle any replied delegation */
2247 			if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2248 			    || NFSMNT_RDONLY(nmp->nm_mountp))) {
2249 			    if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2250 				mode = NFSV4OPEN_ACCESSWRITE;
2251 			    else
2252 				mode = NFSV4OPEN_ACCESSREAD;
2253 			    TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2254 				if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2255 				    continue;
2256 				if ((op->nfso_mode & mode) == mode &&
2257 				    op->nfso_fhlen == dp->nfsdl_fhlen &&
2258 				    !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2259 				    op->nfso_fhlen)) {
2260 				    dp->nfsdl_stateid = ndp->nfsdl_stateid;
2261 				    dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2262 				    dp->nfsdl_ace = ndp->nfsdl_ace;
2263 				    dp->nfsdl_change = ndp->nfsdl_change;
2264 				    dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2265 				    if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2266 					dp->nfsdl_flags |= NFSCLDL_RECALL;
2267 				    free(ndp, M_NFSCLDELEG);
2268 				    ndp = NULL;
2269 				    break;
2270 				}
2271 			    }
2272 			}
2273 			if (ndp != NULL)
2274 			    TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2275 
2276 			/* and reclaim all byte range locks */
2277 			lp = LIST_FIRST(&op->nfso_lock);
2278 			while (lp != NULL) {
2279 			    nlp = LIST_NEXT(lp, nfsl_list);
2280 			    lp->nfsl_seqid = 0;
2281 			    firstlock = 1;
2282 			    lop = LIST_FIRST(&lp->nfsl_lock);
2283 			    while (lop != NULL) {
2284 				nlop = LIST_NEXT(lop, nfslo_list);
2285 				if (lop->nfslo_end == NFS64BITSSET)
2286 				    len = NFS64BITSSET;
2287 				else
2288 				    len = lop->nfslo_end - lop->nfslo_first;
2289 				error = nfscl_trylock(nmp, NULL,
2290 				    op->nfso_fh, op->nfso_fhlen, lp,
2291 				    firstlock, 1, lop->nfslo_first, len,
2292 				    lop->nfslo_type, tcred, p);
2293 				if (error != 0)
2294 				    nfscl_freelock(lop, 0);
2295 				else
2296 				    firstlock = 0;
2297 				lop = nlop;
2298 			    }
2299 			    /* If no locks, but a lockowner, just delete it. */
2300 			    if (LIST_EMPTY(&lp->nfsl_lock))
2301 				nfscl_freelockowner(lp, 0);
2302 			    lp = nlp;
2303 			}
2304 		    } else if (error == NFSERR_NOGRACE && !recovered_one &&
2305 			NFSHASNFSV4N(nmp)) {
2306 			/*
2307 			 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
2308 			 * actually end up here, since the client will do
2309 			 * a recovery for NFSERR_BADSESSION, but will get
2310 			 * an NFSERR_NOGRACE reply for the first "reclaim"
2311 			 * attempt.
2312 			 * So, call nfscl_expireclient() to recover the
2313 			 * opens as best we can and then do a reclaim
2314 			 * complete and return.
2315 			 */
2316 			nfsrpc_reclaimcomplete(nmp, cred, p);
2317 			nfscl_expireclient(clp, nmp, tcred, p);
2318 			goto out;
2319 		    }
2320 		}
2321 		if (error != 0 && error != NFSERR_BADSESSION)
2322 		    nfscl_freeopen(op, 0, true);
2323 		op = nop;
2324 	    }
2325 	    owp = nowp;
2326 	}
2327 
2328 	/*
2329 	 * Now, try and get any delegations not yet reclaimed by cobbling
2330 	 * to-gether an appropriate open.
2331 	 */
2332 	nowp = NULL;
2333 	dp = TAILQ_FIRST(&clp->nfsc_deleg);
2334 	while (dp != NULL) {
2335 	    ndp = TAILQ_NEXT(dp, nfsdl_list);
2336 	    if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2337 		if (nowp == NULL) {
2338 		    nowp = malloc(
2339 			sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2340 		    /*
2341 		     * Name must be as long an largest possible
2342 		     * NFSV4CL_LOCKNAMELEN. 12 for now.
2343 		     */
2344 		    NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2345 			NFSV4CL_LOCKNAMELEN);
2346 		    LIST_INIT(&nowp->nfsow_open);
2347 		    nowp->nfsow_clp = clp;
2348 		    nowp->nfsow_seqid = 0;
2349 		    nowp->nfsow_defunct = 0;
2350 		    nfscl_lockinit(&nowp->nfsow_rwlock);
2351 		}
2352 		nop = NULL;
2353 		if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2354 		    nop = malloc(sizeof (struct nfsclopen) +
2355 			dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2356 		    nop->nfso_own = nowp;
2357 		    if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2358 			nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2359 			delegtype = NFSV4OPEN_DELEGATEWRITE;
2360 		    } else {
2361 			nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2362 			delegtype = NFSV4OPEN_DELEGATEREAD;
2363 		    }
2364 		    nop->nfso_opencnt = 0;
2365 		    nop->nfso_posixlock = 1;
2366 		    nop->nfso_fhlen = dp->nfsdl_fhlen;
2367 		    NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2368 		    LIST_INIT(&nop->nfso_lock);
2369 		    nop->nfso_stateid.seqid = 0;
2370 		    nop->nfso_stateid.other[0] = 0;
2371 		    nop->nfso_stateid.other[1] = 0;
2372 		    nop->nfso_stateid.other[2] = 0;
2373 		    newnfs_copycred(&dp->nfsdl_cred, tcred);
2374 		    newnfs_copyincred(tcred, &nop->nfso_cred);
2375 		    tdp = NULL;
2376 		    error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2377 			nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2378 			nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2379 			delegtype, tcred, p);
2380 		    if (tdp != NULL) {
2381 			if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2382 			    mode = NFSV4OPEN_ACCESSWRITE;
2383 			else
2384 			    mode = NFSV4OPEN_ACCESSREAD;
2385 			if ((nop->nfso_mode & mode) == mode &&
2386 			    nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2387 			    !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2388 			    nop->nfso_fhlen)) {
2389 			    dp->nfsdl_stateid = tdp->nfsdl_stateid;
2390 			    dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2391 			    dp->nfsdl_ace = tdp->nfsdl_ace;
2392 			    dp->nfsdl_change = tdp->nfsdl_change;
2393 			    dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2394 			    if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2395 				dp->nfsdl_flags |= NFSCLDL_RECALL;
2396 			    free(tdp, M_NFSCLDELEG);
2397 			} else {
2398 			    TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2399 			}
2400 		    }
2401 		}
2402 		if (error) {
2403 		    if (nop != NULL)
2404 			free(nop, M_NFSCLOPEN);
2405 		    if (error == NFSERR_NOGRACE && !recovered_one &&
2406 			NFSHASNFSV4N(nmp)) {
2407 			/*
2408 			 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
2409 			 * actually end up here, since the client will do
2410 			 * a recovery for NFSERR_BADSESSION, but will get
2411 			 * an NFSERR_NOGRACE reply for the first "reclaim"
2412 			 * attempt.
2413 			 * So, call nfscl_expireclient() to recover the
2414 			 * opens as best we can and then do a reclaim
2415 			 * complete and return.
2416 			 */
2417 			nfsrpc_reclaimcomplete(nmp, cred, p);
2418 			nfscl_expireclient(clp, nmp, tcred, p);
2419 			free(nowp, M_NFSCLOWNER);
2420 			goto out;
2421 		    }
2422 		    /*
2423 		     * Couldn't reclaim it, so throw the state
2424 		     * away. Ouch!!
2425 		     */
2426 		    nfscl_cleandeleg(dp);
2427 		    nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
2428 		} else {
2429 		    recovered_one = true;
2430 		    LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2431 		}
2432 	    }
2433 	    dp = ndp;
2434 	}
2435 
2436 	/*
2437 	 * Now, get rid of extra Opens and Delegations.
2438 	 */
2439 	LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2440 		do {
2441 			newnfs_copycred(&op->nfso_cred, tcred);
2442 			error = nfscl_tryclose(op, tcred, nmp, p, true);
2443 			if (error == NFSERR_GRACE)
2444 				(void) nfs_catnap(PZERO, error, "nfsexcls");
2445 		} while (error == NFSERR_GRACE);
2446 		LIST_REMOVE(op, nfso_list);
2447 		free(op, M_NFSCLOPEN);
2448 	}
2449 	if (nowp != NULL)
2450 		free(nowp, M_NFSCLOWNER);
2451 
2452 	TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2453 		do {
2454 			newnfs_copycred(&dp->nfsdl_cred, tcred);
2455 			error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2456 			if (error == NFSERR_GRACE)
2457 				(void) nfs_catnap(PZERO, error, "nfsexdlg");
2458 		} while (error == NFSERR_GRACE);
2459 		TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2460 		free(dp, M_NFSCLDELEG);
2461 	}
2462 
2463 	/* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2464 	if (NFSHASNFSV4N(nmp))
2465 		(void)nfsrpc_reclaimcomplete(nmp, cred, p);
2466 
2467 out:
2468 	NFSLOCKCLSTATE();
2469 	clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2470 	wakeup(&clp->nfsc_flags);
2471 	nfsv4_unlock(&clp->nfsc_lock, 0);
2472 	NFSUNLOCKCLSTATE();
2473 	if (tcred != NULL)
2474 		NFSFREECRED(tcred);
2475 }
2476 
2477 /*
2478  * This function is called when a server replies with NFSERR_EXPIRED.
2479  * It deletes all state for the client and does a fresh SetClientId/confirm.
2480  * XXX Someday it should post a signal to the process(es) that hold the
2481  * state, so they know that lock state has been lost.
2482  */
2483 int
nfscl_hasexpired(struct nfsclclient * clp,u_int32_t clidrev,NFSPROC_T * p)2484 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2485 {
2486 	struct nfsmount *nmp;
2487 	struct ucred *cred;
2488 	int igotlock = 0, error, trycnt;
2489 
2490 	/*
2491 	 * If the clientid has gone away or a new SetClientid has already
2492 	 * been done, just return ok.
2493 	 */
2494 	if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2495 		return (0);
2496 
2497 	/*
2498 	 * First, lock the client structure, so everyone else will
2499 	 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2500 	 * that only one thread does the work.
2501 	 */
2502 	NFSLOCKCLSTATE();
2503 	clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2504 	do {
2505 		igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2506 		    NFSCLSTATEMUTEXPTR, NULL);
2507 	} while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2508 	if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2509 		if (igotlock)
2510 			nfsv4_unlock(&clp->nfsc_lock, 0);
2511 		NFSUNLOCKCLSTATE();
2512 		return (0);
2513 	}
2514 	clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2515 	NFSUNLOCKCLSTATE();
2516 
2517 	nmp = clp->nfsc_nmp;
2518 	if (nmp == NULL)
2519 		panic("nfscl expired");
2520 	cred = newnfs_getcred();
2521 	trycnt = 5;
2522 	do {
2523 		error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2524 	} while ((error == NFSERR_STALECLIENTID ||
2525 	     error == NFSERR_BADSESSION ||
2526 	     error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2527 	if (error) {
2528 		NFSLOCKCLSTATE();
2529 		clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2530 	} else {
2531 		/*
2532 		 * Expire the state for the client.
2533 		 */
2534 		nfscl_expireclient(clp, nmp, cred, p);
2535 		NFSLOCKCLSTATE();
2536 		clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2537 		clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2538 	}
2539 	clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2540 	wakeup(&clp->nfsc_flags);
2541 	nfsv4_unlock(&clp->nfsc_lock, 0);
2542 	NFSUNLOCKCLSTATE();
2543 	NFSFREECRED(cred);
2544 	return (error);
2545 }
2546 
2547 /*
2548  * This function inserts a lock in the list after insert_lop.
2549  */
2550 static void
nfscl_insertlock(struct nfscllockowner * lp,struct nfscllock * new_lop,struct nfscllock * insert_lop,int local)2551 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2552     struct nfscllock *insert_lop, int local)
2553 {
2554 
2555 	if ((struct nfscllockowner *)insert_lop == lp)
2556 		LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2557 	else
2558 		LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2559 	if (local)
2560 		nfsstatsv1.cllocallocks++;
2561 	else
2562 		nfsstatsv1.cllocks++;
2563 }
2564 
2565 /*
2566  * This function updates the locking for a lock owner and given file. It
2567  * maintains a list of lock ranges ordered on increasing file offset that
2568  * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2569  * It always adds new_lop to the list and sometimes uses the one pointed
2570  * at by other_lopp.
2571  * Returns 1 if the locks were modified, 0 otherwise.
2572  */
2573 static int
nfscl_updatelock(struct nfscllockowner * lp,struct nfscllock ** new_lopp,struct nfscllock ** other_lopp,int local)2574 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2575     struct nfscllock **other_lopp, int local)
2576 {
2577 	struct nfscllock *new_lop = *new_lopp;
2578 	struct nfscllock *lop, *tlop, *ilop;
2579 	struct nfscllock *other_lop;
2580 	int unlock = 0, modified = 0;
2581 	u_int64_t tmp;
2582 
2583 	/*
2584 	 * Work down the list until the lock is merged.
2585 	 */
2586 	if (new_lop->nfslo_type == F_UNLCK)
2587 		unlock = 1;
2588 	ilop = (struct nfscllock *)lp;
2589 	lop = LIST_FIRST(&lp->nfsl_lock);
2590 	while (lop != NULL) {
2591 	    /*
2592 	     * Only check locks for this file that aren't before the start of
2593 	     * new lock's range.
2594 	     */
2595 	    if (lop->nfslo_end >= new_lop->nfslo_first) {
2596 		if (new_lop->nfslo_end < lop->nfslo_first) {
2597 		    /*
2598 		     * If the new lock ends before the start of the
2599 		     * current lock's range, no merge, just insert
2600 		     * the new lock.
2601 		     */
2602 		    break;
2603 		}
2604 		if (new_lop->nfslo_type == lop->nfslo_type ||
2605 		    (new_lop->nfslo_first <= lop->nfslo_first &&
2606 		     new_lop->nfslo_end >= lop->nfslo_end)) {
2607 		    /*
2608 		     * This lock can be absorbed by the new lock/unlock.
2609 		     * This happens when it covers the entire range
2610 		     * of the old lock or is contiguous
2611 		     * with the old lock and is of the same type or an
2612 		     * unlock.
2613 		     */
2614 		    if (new_lop->nfslo_type != lop->nfslo_type ||
2615 			new_lop->nfslo_first != lop->nfslo_first ||
2616 			new_lop->nfslo_end != lop->nfslo_end)
2617 			modified = 1;
2618 		    if (lop->nfslo_first < new_lop->nfslo_first)
2619 			new_lop->nfslo_first = lop->nfslo_first;
2620 		    if (lop->nfslo_end > new_lop->nfslo_end)
2621 			new_lop->nfslo_end = lop->nfslo_end;
2622 		    tlop = lop;
2623 		    lop = LIST_NEXT(lop, nfslo_list);
2624 		    nfscl_freelock(tlop, local);
2625 		    continue;
2626 		}
2627 
2628 		/*
2629 		 * All these cases are for contiguous locks that are not the
2630 		 * same type, so they can't be merged.
2631 		 */
2632 		if (new_lop->nfslo_first <= lop->nfslo_first) {
2633 		    /*
2634 		     * This case is where the new lock overlaps with the
2635 		     * first part of the old lock. Move the start of the
2636 		     * old lock to just past the end of the new lock. The
2637 		     * new lock will be inserted in front of the old, since
2638 		     * ilop hasn't been updated. (We are done now.)
2639 		     */
2640 		    if (lop->nfslo_first != new_lop->nfslo_end) {
2641 			lop->nfslo_first = new_lop->nfslo_end;
2642 			modified = 1;
2643 		    }
2644 		    break;
2645 		}
2646 		if (new_lop->nfslo_end >= lop->nfslo_end) {
2647 		    /*
2648 		     * This case is where the new lock overlaps with the
2649 		     * end of the old lock's range. Move the old lock's
2650 		     * end to just before the new lock's first and insert
2651 		     * the new lock after the old lock.
2652 		     * Might not be done yet, since the new lock could
2653 		     * overlap further locks with higher ranges.
2654 		     */
2655 		    if (lop->nfslo_end != new_lop->nfslo_first) {
2656 			lop->nfslo_end = new_lop->nfslo_first;
2657 			modified = 1;
2658 		    }
2659 		    ilop = lop;
2660 		    lop = LIST_NEXT(lop, nfslo_list);
2661 		    continue;
2662 		}
2663 		/*
2664 		 * The final case is where the new lock's range is in the
2665 		 * middle of the current lock's and splits the current lock
2666 		 * up. Use *other_lopp to handle the second part of the
2667 		 * split old lock range. (We are done now.)
2668 		 * For unlock, we use new_lop as other_lop and tmp, since
2669 		 * other_lop and new_lop are the same for this case.
2670 		 * We noted the unlock case above, so we don't need
2671 		 * new_lop->nfslo_type any longer.
2672 		 */
2673 		tmp = new_lop->nfslo_first;
2674 		if (unlock) {
2675 		    other_lop = new_lop;
2676 		    *new_lopp = NULL;
2677 		} else {
2678 		    other_lop = *other_lopp;
2679 		    *other_lopp = NULL;
2680 		}
2681 		other_lop->nfslo_first = new_lop->nfslo_end;
2682 		other_lop->nfslo_end = lop->nfslo_end;
2683 		other_lop->nfslo_type = lop->nfslo_type;
2684 		lop->nfslo_end = tmp;
2685 		nfscl_insertlock(lp, other_lop, lop, local);
2686 		ilop = lop;
2687 		modified = 1;
2688 		break;
2689 	    }
2690 	    ilop = lop;
2691 	    lop = LIST_NEXT(lop, nfslo_list);
2692 	    if (lop == NULL)
2693 		break;
2694 	}
2695 
2696 	/*
2697 	 * Insert the new lock in the list at the appropriate place.
2698 	 */
2699 	if (!unlock) {
2700 		nfscl_insertlock(lp, new_lop, ilop, local);
2701 		*new_lopp = NULL;
2702 		modified = 1;
2703 	}
2704 	return (modified);
2705 }
2706 
2707 /*
2708  * This function must be run as a kernel thread.
2709  * It does Renew Ops and recovery, when required.
2710  */
2711 void
nfscl_renewthread(struct nfsclclient * clp,NFSPROC_T * p)2712 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2713 {
2714 	struct nfsclowner *owp, *nowp;
2715 	struct nfsclopen *op;
2716 	struct nfscllockowner *lp, *nlp;
2717 	struct nfscldeleghead dh;
2718 	struct nfscldeleg *dp, *ndp;
2719 	struct ucred *cred;
2720 	u_int32_t clidrev;
2721 	int error, cbpathdown, islept, igotlock, ret, clearok;
2722 	uint32_t recover_done_time = 0;
2723 	time_t mytime;
2724 	static time_t prevsec = 0;
2725 	struct nfscllockownerfh *lfhp, *nlfhp;
2726 	struct nfscllockownerfhhead lfh;
2727 	struct nfscllayout *lyp, *nlyp;
2728 	struct nfscldevinfo *dip, *ndip;
2729 	struct nfscllayouthead rlh;
2730 	struct nfsclrecalllayout *recallp;
2731 	struct nfsclds *dsp;
2732 	bool retok;
2733 	struct mount *mp;
2734 	vnode_t vp;
2735 
2736 	cred = newnfs_getcred();
2737 	NFSLOCKCLSTATE();
2738 	clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2739 	mp = clp->nfsc_nmp->nm_mountp;
2740 	NFSUNLOCKCLSTATE();
2741 	for(;;) {
2742 		newnfs_setroot(cred);
2743 		cbpathdown = 0;
2744 		if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2745 			/*
2746 			 * Only allow one full recover within 1/2 of the lease
2747 			 * duration (nfsc_renew).
2748 			 * retok is value/result.  If passed in set to true,
2749 			 * it indicates only a CreateSession operation should
2750 			 * be attempted.
2751 			 * If it is returned true, it indicates that the
2752 			 * recovery only required a CreateSession.
2753 			 */
2754 			retok = true;
2755 			if (recover_done_time < NFSD_MONOSEC) {
2756 				recover_done_time = NFSD_MONOSEC +
2757 				    clp->nfsc_renew;
2758 				retok = false;
2759 			}
2760 			NFSCL_DEBUG(1, "Doing recovery, only "
2761 			    "createsession=%d\n", retok);
2762 			nfscl_recover(clp, &retok, cred, p);
2763 		}
2764 		if (clp->nfsc_expire <= NFSD_MONOSEC &&
2765 		    (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2766 			clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2767 			clidrev = clp->nfsc_clientidrev;
2768 			error = nfsrpc_renew(clp, NULL, cred, p);
2769 			if (error == NFSERR_CBPATHDOWN)
2770 			    cbpathdown = 1;
2771 			else if (error == NFSERR_STALECLIENTID) {
2772 			    NFSLOCKCLSTATE();
2773 			    clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2774 			    NFSUNLOCKCLSTATE();
2775 			} else if (error == NFSERR_EXPIRED)
2776 			    (void) nfscl_hasexpired(clp, clidrev, p);
2777 		}
2778 
2779 checkdsrenew:
2780 		if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2781 			/* Do renews for any DS sessions. */
2782 			NFSLOCKMNT(clp->nfsc_nmp);
2783 			/* Skip first entry, since the MDS is handled above. */
2784 			dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2785 			if (dsp != NULL)
2786 				dsp = TAILQ_NEXT(dsp, nfsclds_list);
2787 			while (dsp != NULL) {
2788 				if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2789 				    dsp->nfsclds_sess.nfsess_defunct == 0) {
2790 					dsp->nfsclds_expire = NFSD_MONOSEC +
2791 					    clp->nfsc_renew;
2792 					NFSUNLOCKMNT(clp->nfsc_nmp);
2793 					(void)nfsrpc_renew(clp, dsp, cred, p);
2794 					goto checkdsrenew;
2795 				}
2796 				dsp = TAILQ_NEXT(dsp, nfsclds_list);
2797 			}
2798 			NFSUNLOCKMNT(clp->nfsc_nmp);
2799 		}
2800 
2801 		TAILQ_INIT(&dh);
2802 		NFSLOCKCLSTATE();
2803 		if (cbpathdown)
2804 			/* It's a Total Recall! */
2805 			nfscl_totalrecall(clp);
2806 
2807 		/*
2808 		 * Now, handle defunct owners.
2809 		 */
2810 		LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2811 			if (LIST_EMPTY(&owp->nfsow_open)) {
2812 				if (owp->nfsow_defunct != 0)
2813 					nfscl_freeopenowner(owp, 0);
2814 			}
2815 		}
2816 
2817 		/*
2818 		 * Do the recall on any delegations. To avoid trouble, always
2819 		 * come back up here after having slept.
2820 		 */
2821 		igotlock = 0;
2822 tryagain:
2823 		dp = TAILQ_FIRST(&clp->nfsc_deleg);
2824 		while (dp != NULL) {
2825 			ndp = TAILQ_NEXT(dp, nfsdl_list);
2826 			if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2827 				/*
2828 				 * Wait for outstanding I/O ops to be done.
2829 				 */
2830 				if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2831 				    if (igotlock) {
2832 					nfsv4_unlock(&clp->nfsc_lock, 0);
2833 					igotlock = 0;
2834 				    }
2835 				    dp->nfsdl_rwlock.nfslock_lock |=
2836 					NFSV4LOCK_WANTED;
2837 				    msleep(&dp->nfsdl_rwlock,
2838 					NFSCLSTATEMUTEXPTR, PVFS, "nfscld",
2839 					5 * hz);
2840 				    if (NFSCL_FORCEDISM(mp))
2841 					goto terminate;
2842 				    goto tryagain;
2843 				}
2844 				while (!igotlock) {
2845 				    igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2846 					&islept, NFSCLSTATEMUTEXPTR, mp);
2847 				    if (igotlock == 0 && NFSCL_FORCEDISM(mp))
2848 					goto terminate;
2849 				    if (islept)
2850 					goto tryagain;
2851 				}
2852 				NFSUNLOCKCLSTATE();
2853 				newnfs_copycred(&dp->nfsdl_cred, cred);
2854 				ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2855 				    NULL, cred, p, 1, &vp);
2856 				if (!ret) {
2857 				    nfscl_cleandeleg(dp);
2858 				    TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2859 					nfsdl_list);
2860 				    LIST_REMOVE(dp, nfsdl_hash);
2861 				    TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2862 				    nfscl_delegcnt--;
2863 				    nfsstatsv1.cldelegates--;
2864 				}
2865 				NFSLOCKCLSTATE();
2866 				/*
2867 				 * The nfsc_lock must be released before doing
2868 				 * vrele(), since it might call nfs_inactive().
2869 				 * For the unlikely case where the vnode failed
2870 				 * to be acquired by nfscl_recalldeleg(), a
2871 				 * VOP_RECLAIM() should be in progress and it
2872 				 * will return the delegation.
2873 				 */
2874 				nfsv4_unlock(&clp->nfsc_lock, 0);
2875 				igotlock = 0;
2876 				if (vp != NULL) {
2877 					NFSUNLOCKCLSTATE();
2878 					vrele(vp);
2879 					NFSLOCKCLSTATE();
2880 				}
2881 				goto tryagain;
2882 			}
2883 			dp = ndp;
2884 		}
2885 
2886 		/*
2887 		 * Clear out old delegations, if we are above the high water
2888 		 * mark. Only clear out ones with no state related to them.
2889 		 * The tailq list is in LRU order.
2890 		 */
2891 		dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2892 		while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2893 		    ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2894 		    if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2895 			dp->nfsdl_rwlock.nfslock_lock == 0 &&
2896 			dp->nfsdl_timestamp < NFSD_MONOSEC &&
2897 			(dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2898 			  NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2899 			clearok = 1;
2900 			LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2901 			    op = LIST_FIRST(&owp->nfsow_open);
2902 			    if (op != NULL) {
2903 				clearok = 0;
2904 				break;
2905 			    }
2906 			}
2907 			if (clearok) {
2908 			    LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2909 				if (!LIST_EMPTY(&lp->nfsl_lock)) {
2910 				    clearok = 0;
2911 				    break;
2912 				}
2913 			    }
2914 			}
2915 			if (clearok) {
2916 			    TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2917 			    LIST_REMOVE(dp, nfsdl_hash);
2918 			    TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2919 			    nfscl_delegcnt--;
2920 			    nfsstatsv1.cldelegates--;
2921 			}
2922 		    }
2923 		    dp = ndp;
2924 		}
2925 		if (igotlock)
2926 			nfsv4_unlock(&clp->nfsc_lock, 0);
2927 
2928 		/*
2929 		 * Do the recall on any layouts. To avoid trouble, always
2930 		 * come back up here after having slept.
2931 		 */
2932 		TAILQ_INIT(&rlh);
2933 tryagain2:
2934 		TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2935 			if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2936 				/*
2937 				 * Wait for outstanding I/O ops to be done.
2938 				 */
2939 				if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2940 				    (lyp->nfsly_lock.nfslock_lock &
2941 				     NFSV4LOCK_LOCK) != 0) {
2942 					lyp->nfsly_lock.nfslock_lock |=
2943 					    NFSV4LOCK_WANTED;
2944 					msleep(&lyp->nfsly_lock.nfslock_lock,
2945 					    NFSCLSTATEMUTEXPTR, PVFS, "nfslyp",
2946 					    5 * hz);
2947 					if (NFSCL_FORCEDISM(mp))
2948 					    goto terminate;
2949 					goto tryagain2;
2950 				}
2951 				/* Move the layout to the recall list. */
2952 				TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2953 				    nfsly_list);
2954 				LIST_REMOVE(lyp, nfsly_hash);
2955 				TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2956 
2957 				/* Handle any layout commits. */
2958 				if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2959 				    (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2960 					lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2961 					NFSUNLOCKCLSTATE();
2962 					NFSCL_DEBUG(3, "do layoutcommit\n");
2963 					nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2964 					    cred, p);
2965 					NFSLOCKCLSTATE();
2966 					goto tryagain2;
2967 				}
2968 			}
2969 		}
2970 
2971 		/* Now, look for stale layouts. */
2972 		lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2973 		while (lyp != NULL) {
2974 			nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2975 			if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2976 			    (lyp->nfsly_flags & (NFSLY_RECALL |
2977 			     NFSLY_RETONCLOSE)) == 0 &&
2978 			    lyp->nfsly_lock.nfslock_usecnt == 0 &&
2979 			    lyp->nfsly_lock.nfslock_lock == 0) {
2980 				NFSCL_DEBUG(4, "ret stale lay=%d\n",
2981 				    nfscl_layoutcnt);
2982 				recallp = malloc(sizeof(*recallp),
2983 				    M_NFSLAYRECALL, M_NOWAIT);
2984 				if (recallp == NULL)
2985 					break;
2986 				(void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2987 				    lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2988 				    lyp->nfsly_stateid.seqid, 0, 0, NULL,
2989 				    recallp);
2990 			}
2991 			lyp = nlyp;
2992 		}
2993 
2994 		/*
2995 		 * Free up any unreferenced device info structures.
2996 		 */
2997 		LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
2998 			if (dip->nfsdi_layoutrefs == 0 &&
2999 			    dip->nfsdi_refcnt == 0) {
3000 				NFSCL_DEBUG(4, "freeing devinfo\n");
3001 				LIST_REMOVE(dip, nfsdi_list);
3002 				nfscl_freedevinfo(dip);
3003 			}
3004 		}
3005 		NFSUNLOCKCLSTATE();
3006 
3007 		/* Do layout return(s), as required. */
3008 		TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
3009 			TAILQ_REMOVE(&rlh, lyp, nfsly_list);
3010 			NFSCL_DEBUG(4, "ret layout\n");
3011 			nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
3012 			if ((lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) {
3013 				NFSLOCKCLSTATE();
3014 				lyp->nfsly_flags |= NFSLY_RETURNED;
3015 				wakeup(lyp);
3016 				NFSUNLOCKCLSTATE();
3017 			} else
3018 				nfscl_freelayout(lyp);
3019 		}
3020 
3021 		/*
3022 		 * Delegreturn any delegations cleaned out or recalled.
3023 		 */
3024 		TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
3025 			newnfs_copycred(&dp->nfsdl_cred, cred);
3026 			(void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3027 			TAILQ_REMOVE(&dh, dp, nfsdl_list);
3028 			free(dp, M_NFSCLDELEG);
3029 		}
3030 
3031 		SLIST_INIT(&lfh);
3032 		/*
3033 		 * Call nfscl_cleanupkext() once per second to check for
3034 		 * open/lock owners where the process has exited.
3035 		 */
3036 		mytime = NFSD_MONOSEC;
3037 		if (prevsec != mytime) {
3038 			prevsec = mytime;
3039 			nfscl_cleanupkext(clp, &lfh);
3040 		}
3041 
3042 		/*
3043 		 * Do a ReleaseLockOwner for all lock owners where the
3044 		 * associated process no longer exists, as found by
3045 		 * nfscl_cleanupkext().
3046 		 */
3047 		newnfs_setroot(cred);
3048 		SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
3049 			LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
3050 			    nlp) {
3051 				(void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
3052 				    lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
3053 				    p);
3054 				nfscl_freelockowner(lp, 0);
3055 			}
3056 			free(lfhp, M_TEMP);
3057 		}
3058 		SLIST_INIT(&lfh);
3059 
3060 		NFSLOCKCLSTATE();
3061 		if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
3062 			(void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
3063 			    hz);
3064 terminate:
3065 		if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
3066 			clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
3067 			NFSUNLOCKCLSTATE();
3068 			NFSFREECRED(cred);
3069 			wakeup((caddr_t)clp);
3070 			return;
3071 		}
3072 		NFSUNLOCKCLSTATE();
3073 	}
3074 }
3075 
3076 /*
3077  * Initiate state recovery. Called when NFSERR_STALECLIENTID,
3078  * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
3079  */
3080 void
nfscl_initiate_recovery(struct nfsclclient * clp)3081 nfscl_initiate_recovery(struct nfsclclient *clp)
3082 {
3083 
3084 	if (clp == NULL)
3085 		return;
3086 	NFSLOCKCLSTATE();
3087 	clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
3088 	NFSUNLOCKCLSTATE();
3089 	wakeup((caddr_t)clp);
3090 }
3091 
3092 /*
3093  * Dump out the state stuff for debugging.
3094  */
3095 void
nfscl_dumpstate(struct nfsmount * nmp,int openowner,int opens,int lockowner,int locks)3096 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
3097     int lockowner, int locks)
3098 {
3099 	struct nfsclclient *clp;
3100 	struct nfsclowner *owp;
3101 	struct nfsclopen *op;
3102 	struct nfscllockowner *lp;
3103 	struct nfscllock *lop;
3104 	struct nfscldeleg *dp;
3105 
3106 	clp = nmp->nm_clp;
3107 	if (clp == NULL) {
3108 		printf("nfscl dumpstate NULL clp\n");
3109 		return;
3110 	}
3111 	NFSLOCKCLSTATE();
3112 	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
3113 	  LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3114 	    if (openowner && !LIST_EMPTY(&owp->nfsow_open))
3115 		printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
3116 		    owp->nfsow_owner[0], owp->nfsow_owner[1],
3117 		    owp->nfsow_owner[2], owp->nfsow_owner[3],
3118 		    owp->nfsow_seqid);
3119 	    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3120 		if (opens)
3121 		    printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
3122 			op->nfso_stateid.other[0], op->nfso_stateid.other[1],
3123 			op->nfso_stateid.other[2], op->nfso_opencnt,
3124 			op->nfso_fh[12]);
3125 		LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
3126 		    if (lockowner)
3127 			printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
3128 			    lp->nfsl_owner[0], lp->nfsl_owner[1],
3129 			    lp->nfsl_owner[2], lp->nfsl_owner[3],
3130 			    lp->nfsl_seqid,
3131 			    lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
3132 			    lp->nfsl_stateid.other[2]);
3133 		    LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3134 			if (locks)
3135 #ifdef __FreeBSD__
3136 			    printf("lck typ=%d fst=%ju end=%ju\n",
3137 				lop->nfslo_type, (intmax_t)lop->nfslo_first,
3138 				(intmax_t)lop->nfslo_end);
3139 #else
3140 			    printf("lck typ=%d fst=%qd end=%qd\n",
3141 				lop->nfslo_type, lop->nfslo_first,
3142 				lop->nfslo_end);
3143 #endif
3144 		    }
3145 		}
3146 	    }
3147 	  }
3148 	}
3149 	LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3150 	    if (openowner && !LIST_EMPTY(&owp->nfsow_open))
3151 		printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
3152 		    owp->nfsow_owner[0], owp->nfsow_owner[1],
3153 		    owp->nfsow_owner[2], owp->nfsow_owner[3],
3154 		    owp->nfsow_seqid);
3155 	    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3156 		if (opens)
3157 		    printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
3158 			op->nfso_stateid.other[0], op->nfso_stateid.other[1],
3159 			op->nfso_stateid.other[2], op->nfso_opencnt,
3160 			op->nfso_fh[12]);
3161 		LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
3162 		    if (lockowner)
3163 			printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
3164 			    lp->nfsl_owner[0], lp->nfsl_owner[1],
3165 			    lp->nfsl_owner[2], lp->nfsl_owner[3],
3166 			    lp->nfsl_seqid,
3167 			    lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
3168 			    lp->nfsl_stateid.other[2]);
3169 		    LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3170 			if (locks)
3171 #ifdef __FreeBSD__
3172 			    printf("lck typ=%d fst=%ju end=%ju\n",
3173 				lop->nfslo_type, (intmax_t)lop->nfslo_first,
3174 				(intmax_t)lop->nfslo_end);
3175 #else
3176 			    printf("lck typ=%d fst=%qd end=%qd\n",
3177 				lop->nfslo_type, lop->nfslo_first,
3178 				lop->nfslo_end);
3179 #endif
3180 		    }
3181 		}
3182 	    }
3183 	}
3184 	NFSUNLOCKCLSTATE();
3185 }
3186 
3187 /*
3188  * Check for duplicate open owners and opens.
3189  * (Only used as a diagnostic aid.)
3190  */
3191 void
nfscl_dupopen(vnode_t vp,int dupopens)3192 nfscl_dupopen(vnode_t vp, int dupopens)
3193 {
3194 	struct nfsclclient *clp;
3195 	struct nfsclowner *owp, *owp2;
3196 	struct nfsclopen *op, *op2;
3197 	struct nfsfh *nfhp;
3198 
3199 	clp = VFSTONFS(vp->v_mount)->nm_clp;
3200 	if (clp == NULL) {
3201 		printf("nfscl dupopen NULL clp\n");
3202 		return;
3203 	}
3204 	nfhp = VTONFS(vp)->n_fhp;
3205 	NFSLOCKCLSTATE();
3206 
3207 	/*
3208 	 * First, search for duplicate owners.
3209 	 * These should never happen!
3210 	 */
3211 	LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3212 	    LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3213 		if (owp != owp2 &&
3214 		    !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
3215 		    NFSV4CL_LOCKNAMELEN)) {
3216 			NFSUNLOCKCLSTATE();
3217 			printf("DUP OWNER\n");
3218 			nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3219 			return;
3220 		}
3221 	    }
3222 	}
3223 
3224 	/*
3225 	 * Now, search for duplicate stateids.
3226 	 * These shouldn't happen, either.
3227 	 */
3228 	LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3229 	    LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3230 		LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3231 		    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3232 			if (op != op2 &&
3233 			    (op->nfso_stateid.other[0] != 0 ||
3234 			     op->nfso_stateid.other[1] != 0 ||
3235 			     op->nfso_stateid.other[2] != 0) &&
3236 			    op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
3237 			    op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
3238 			    op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
3239 			    NFSUNLOCKCLSTATE();
3240 			    printf("DUP STATEID\n");
3241 			    nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3242 			    return;
3243 			}
3244 		    }
3245 		}
3246 	    }
3247 	}
3248 
3249 	/*
3250 	 * Now search for duplicate opens.
3251 	 * Duplicate opens for the same owner
3252 	 * should never occur. Other duplicates are
3253 	 * possible and are checked for if "dupopens"
3254 	 * is true.
3255 	 */
3256 	LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3257 	    LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3258 		if (nfhp->nfh_len == op2->nfso_fhlen &&
3259 		    !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3260 		    LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3261 			LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3262 			    if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3263 				!NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3264 				(!NFSBCMP(op->nfso_own->nfsow_owner,
3265 				 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3266 				 dupopens)) {
3267 				if (!NFSBCMP(op->nfso_own->nfsow_owner,
3268 				    op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3269 				    NFSUNLOCKCLSTATE();
3270 				    printf("BADDUP OPEN\n");
3271 				} else {
3272 				    NFSUNLOCKCLSTATE();
3273 				    printf("DUP OPEN\n");
3274 				}
3275 				nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0,
3276 				    0);
3277 				return;
3278 			    }
3279 			}
3280 		    }
3281 		}
3282 	    }
3283 	}
3284 	NFSUNLOCKCLSTATE();
3285 }
3286 
3287 /*
3288  * During close, find an open that needs to be dereferenced and
3289  * dereference it. If there are no more opens for this file,
3290  * log a message to that effect.
3291  * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3292  * on the file's vnode.
3293  * This is the safe way, since it is difficult to identify
3294  * which open the close is for and I/O can be performed after the
3295  * close(2) system call when a file is mmap'd.
3296  * If it returns 0 for success, there will be a referenced
3297  * clp returned via clpp.
3298  */
3299 int
nfscl_getclose(vnode_t vp,struct nfsclclient ** clpp)3300 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3301 {
3302 	struct nfsclclient *clp;
3303 	struct nfsclowner *owp;
3304 	struct nfsclopen *op;
3305 	struct nfscldeleg *dp;
3306 	struct nfsfh *nfhp;
3307 	int error, notdecr;
3308 
3309 	error = nfscl_getcl(vp->v_mount, NULL, NULL, false, true, &clp);
3310 	if (error)
3311 		return (error);
3312 	*clpp = clp;
3313 
3314 	nfhp = VTONFS(vp)->n_fhp;
3315 	notdecr = 1;
3316 	NFSLOCKCLSTATE();
3317 	/*
3318 	 * First, look for one under a delegation that was locally issued
3319 	 * and just decrement the opencnt for it. Since all my Opens against
3320 	 * the server are DENY_NONE, I don't see a problem with hanging
3321 	 * onto them. (It is much easier to use one of the extant Opens
3322 	 * that I already have on the server when a Delegation is recalled
3323 	 * than to do fresh Opens.) Someday, I might need to rethink this, but.
3324 	 */
3325 	dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3326 	if (dp != NULL) {
3327 		LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3328 			op = LIST_FIRST(&owp->nfsow_open);
3329 			if (op != NULL) {
3330 				/*
3331 				 * Since a delegation is for a file, there
3332 				 * should never be more than one open for
3333 				 * each openowner.
3334 				 */
3335 				if (LIST_NEXT(op, nfso_list) != NULL)
3336 					panic("nfscdeleg opens");
3337 				if (notdecr && op->nfso_opencnt > 0) {
3338 					notdecr = 0;
3339 					op->nfso_opencnt--;
3340 					break;
3341 				}
3342 			}
3343 		}
3344 	}
3345 
3346 	/* Now process the opens against the server. */
3347 	LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len),
3348 	    nfso_hash) {
3349 		if (op->nfso_fhlen == nfhp->nfh_len &&
3350 		    !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3351 		    nfhp->nfh_len)) {
3352 			/* Found an open, decrement cnt if possible */
3353 			if (notdecr && op->nfso_opencnt > 0) {
3354 				notdecr = 0;
3355 				op->nfso_opencnt--;
3356 			}
3357 			/*
3358 			 * There are more opens, so just return.
3359 			 */
3360 			if (op->nfso_opencnt > 0) {
3361 				NFSUNLOCKCLSTATE();
3362 				return (0);
3363 			}
3364 		}
3365 	}
3366 	NFSUNLOCKCLSTATE();
3367 	if (notdecr)
3368 		printf("nfscl: never fnd open\n");
3369 	return (0);
3370 }
3371 
3372 int
nfscl_doclose(vnode_t vp,struct nfsclclient ** clpp,NFSPROC_T * p)3373 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3374 {
3375 	struct nfsclclient *clp;
3376 	struct nfsmount *nmp;
3377 	struct nfsclowner *owp, *nowp;
3378 	struct nfsclopen *op, *nop;
3379 	struct nfsclopenhead delayed;
3380 	struct nfscldeleg *dp;
3381 	struct nfsfh *nfhp;
3382 	struct nfsclrecalllayout *recallp;
3383 	struct nfscllayout *lyp;
3384 	int error;
3385 
3386 	error = nfscl_getcl(vp->v_mount, NULL, NULL, false, true, &clp);
3387 	if (error)
3388 		return (error);
3389 	*clpp = clp;
3390 
3391 	nmp = VFSTONFS(vp->v_mount);
3392 	nfhp = VTONFS(vp)->n_fhp;
3393 	recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
3394 	NFSLOCKCLSTATE();
3395 	/*
3396 	 * First get rid of the local Open structures, which should be no
3397 	 * longer in use.
3398 	 */
3399 	dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3400 	if (dp != NULL) {
3401 		LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3402 			op = LIST_FIRST(&owp->nfsow_open);
3403 			if (op != NULL) {
3404 				KASSERT((op->nfso_opencnt == 0),
3405 				    ("nfscl: bad open cnt on deleg"));
3406 				nfscl_freeopen(op, 1, true);
3407 			}
3408 			nfscl_freeopenowner(owp, 1);
3409 		}
3410 	}
3411 
3412 	/* Return any layouts marked return on close. */
3413 	nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp,
3414 	    &lyp);
3415 
3416 	/* Now process the opens against the server. */
3417 	LIST_INIT(&delayed);
3418 lookformore:
3419 	LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len),
3420 	    nfso_hash) {
3421 		if (op->nfso_fhlen == nfhp->nfh_len &&
3422 		    !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3423 		    nfhp->nfh_len)) {
3424 			/* Found an open, close it. */
3425 #ifdef DIAGNOSTIC
3426 			KASSERT((op->nfso_opencnt == 0),
3427 			    ("nfscl: bad open cnt on server (%d)",
3428 			     op->nfso_opencnt));
3429 #endif
3430 			NFSUNLOCKCLSTATE();
3431 			if (NFSHASNFSV4N(nmp))
3432 				error = nfsrpc_doclose(nmp, op, p, false, true);
3433 			else
3434 				error = nfsrpc_doclose(nmp, op, p, true, true);
3435 			NFSLOCKCLSTATE();
3436 			if (error == NFSERR_DELAY) {
3437 				nfscl_unlinkopen(op);
3438 				op->nfso_own = NULL;
3439 				LIST_INSERT_HEAD(&delayed, op, nfso_list);
3440 			}
3441 			goto lookformore;
3442 		}
3443 	}
3444 	nfscl_clrelease(clp);
3445 
3446 	/* Now, wait for any layout that is returned upon close. */
3447 	if (lyp != NULL) {
3448 		while ((lyp->nfsly_flags & NFSLY_RETURNED) == 0) {
3449 			if (NFSCL_FORCEDISM(nmp->nm_mountp)) {
3450 				lyp = NULL;
3451 				break;
3452 			}
3453 			msleep(lyp, NFSCLSTATEMUTEXPTR, PZERO, "nfslroc", hz);
3454 		}
3455 		if (lyp != NULL)
3456 			nfscl_freelayout(lyp);
3457 	}
3458 
3459 	NFSUNLOCKCLSTATE();
3460 	/*
3461 	 * recallp has been set NULL by nfscl_retoncloselayout() if it was
3462 	 * used by the function, but calling free() with a NULL pointer is ok.
3463 	 */
3464 	free(recallp, M_NFSLAYRECALL);
3465 
3466 	/* Now, loop retrying the delayed closes. */
3467 	LIST_FOREACH_SAFE(op, &delayed, nfso_list, nop) {
3468 		nfsrpc_doclose(nmp, op, p, true, false);
3469 		LIST_REMOVE(op, nfso_list);
3470 		nfscl_freeopen(op, 0, false);
3471 	}
3472 	return (0);
3473 }
3474 
3475 /*
3476  * Return all delegations on this client.
3477  * (Must be called with client sleep lock.)
3478  */
3479 static void
nfscl_delegreturnall(struct nfsclclient * clp,NFSPROC_T * p,struct nfscldeleghead * dhp)3480 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p,
3481     struct nfscldeleghead *dhp)
3482 {
3483 	struct nfscldeleg *dp, *ndp;
3484 	struct ucred *cred;
3485 
3486 	cred = newnfs_getcred();
3487 	TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3488 		nfscl_cleandeleg(dp);
3489 		(void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3490 		if (dhp != NULL) {
3491 			nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
3492 			TAILQ_INSERT_HEAD(dhp, dp, nfsdl_list);
3493 		} else
3494 			nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
3495 	}
3496 	NFSFREECRED(cred);
3497 }
3498 
3499 /*
3500  * Return any delegation for this vp.
3501  */
3502 void
nfscl_delegreturnvp(vnode_t vp,NFSPROC_T * p)3503 nfscl_delegreturnvp(vnode_t vp, NFSPROC_T *p)
3504 {
3505 	struct nfsclclient *clp;
3506 	struct nfscldeleg *dp;
3507 	struct ucred *cred;
3508 	struct nfsnode *np;
3509 	struct nfsmount *nmp;
3510 
3511 	nmp = VFSTONFS(vp->v_mount);
3512 	NFSLOCKMNT(nmp);
3513 	if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
3514 		NFSUNLOCKMNT(nmp);
3515 		return;
3516 	}
3517 	NFSUNLOCKMNT(nmp);
3518 	np = VTONFS(vp);
3519 	cred = newnfs_getcred();
3520 	dp = NULL;
3521 	NFSLOCKCLSTATE();
3522 	clp = nmp->nm_clp;
3523 	if (clp != NULL)
3524 		dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3525 		    np->n_fhp->nfh_len);
3526 	if (dp != NULL) {
3527 		nfscl_cleandeleg(dp);
3528 		nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
3529 		NFSUNLOCKCLSTATE();
3530 		newnfs_copycred(&dp->nfsdl_cred, cred);
3531 		nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3532 		free(dp, M_NFSCLDELEG);
3533 	} else
3534 		NFSUNLOCKCLSTATE();
3535 	NFSFREECRED(cred);
3536 }
3537 
3538 /*
3539  * Do a callback RPC.
3540  */
3541 void
nfscl_docb(struct nfsrv_descript * nd,NFSPROC_T * p)3542 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3543 {
3544 	int clist, gotseq_ok, i, j, k, op, rcalls;
3545 	u_int32_t *tl;
3546 	struct nfsclclient *clp;
3547 	struct nfscldeleg *dp = NULL;
3548 	int numops, taglen = -1, error = 0, trunc __unused;
3549 	u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3550 	u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3551 	vnode_t vp = NULL;
3552 	struct nfsnode *np;
3553 	struct vattr va;
3554 	struct nfsfh *nfhp;
3555 	mount_t mp;
3556 	nfsattrbit_t attrbits, rattrbits;
3557 	nfsv4stateid_t stateid;
3558 	uint32_t seqid, slotid = 0, highslot, cachethis __unused;
3559 	uint8_t sessionid[NFSX_V4SESSIONID];
3560 	struct mbuf *rep;
3561 	struct nfscllayout *lyp;
3562 	uint64_t filesid[2], len, off;
3563 	int changed, gotone, laytype, recalltype;
3564 	uint32_t iomode;
3565 	struct nfsclrecalllayout *recallp = NULL;
3566 	struct nfsclsession *tsep;
3567 
3568 	gotseq_ok = 0;
3569 	nfsrvd_rephead(nd);
3570 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3571 	taglen = fxdr_unsigned(int, *tl);
3572 	if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
3573 		error = EBADRPC;
3574 		taglen = -1;
3575 		goto nfsmout;
3576 	}
3577 	if (taglen <= NFSV4_SMALLSTR)
3578 		tagstr = tag;
3579 	else
3580 		tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3581 	error = nfsrv_mtostr(nd, tagstr, taglen);
3582 	if (error) {
3583 		if (taglen > NFSV4_SMALLSTR)
3584 			free(tagstr, M_TEMP);
3585 		taglen = -1;
3586 		goto nfsmout;
3587 	}
3588 	(void) nfsm_strtom(nd, tag, taglen);
3589 	if (taglen > NFSV4_SMALLSTR) {
3590 		free(tagstr, M_TEMP);
3591 	}
3592 	NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3593 	NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3594 	minorvers = fxdr_unsigned(u_int32_t, *tl++);
3595 	if (minorvers != NFSV4_MINORVERSION &&
3596 	    minorvers != NFSV41_MINORVERSION &&
3597 	    minorvers != NFSV42_MINORVERSION)
3598 		nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3599 	cbident = fxdr_unsigned(u_int32_t, *tl++);
3600 	if (nd->nd_repstat)
3601 		numops = 0;
3602 	else
3603 		numops = fxdr_unsigned(int, *tl);
3604 	/*
3605 	 * Loop around doing the sub ops.
3606 	 */
3607 	for (i = 0; i < numops; i++) {
3608 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3609 		NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3610 		*repp++ = *tl;
3611 		op = fxdr_unsigned(int, *tl);
3612 		nd->nd_procnum = op;
3613 		if (i == 0 && op != NFSV4OP_CBSEQUENCE && minorvers !=
3614 		    NFSV4_MINORVERSION) {
3615 		    nd->nd_repstat = NFSERR_OPNOTINSESS;
3616 		    *repp = nfscl_errmap(nd, minorvers);
3617 		    retops++;
3618 		    break;
3619 		}
3620 		if (op < NFSV4OP_CBGETATTR ||
3621 		   (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3622 		   (op > NFSV4OP_CBNOTIFYDEVID &&
3623 		    minorvers == NFSV41_MINORVERSION) ||
3624 		   (op > NFSV4OP_CBOFFLOAD &&
3625 		    minorvers == NFSV42_MINORVERSION)) {
3626 		    nd->nd_repstat = NFSERR_OPILLEGAL;
3627 		    *repp = nfscl_errmap(nd, minorvers);
3628 		    retops++;
3629 		    break;
3630 		}
3631 		if (op < NFSV42_CBNOPS)
3632 			nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
3633 		switch (op) {
3634 		case NFSV4OP_CBGETATTR:
3635 			NFSCL_DEBUG(4, "cbgetattr\n");
3636 			mp = NULL;
3637 			vp = NULL;
3638 			error = nfsm_getfh(nd, &nfhp);
3639 			if (!error)
3640 				error = nfsrv_getattrbits(nd, &attrbits,
3641 				    NULL, NULL);
3642 			if (!error) {
3643 				mp = nfscl_getmnt(minorvers, sessionid, cbident,
3644 				    &clp);
3645 				if (mp == NULL)
3646 					error = NFSERR_SERVERFAULT;
3647 			}
3648 			if (!error) {
3649 				error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3650 				    nfhp->nfh_len, p, &np);
3651 				if (!error)
3652 					vp = NFSTOV(np);
3653 			}
3654 			if (!error) {
3655 				NFSZERO_ATTRBIT(&rattrbits);
3656 				NFSLOCKCLSTATE();
3657 				dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3658 				    nfhp->nfh_len);
3659 				if (dp != NULL) {
3660 					if (NFSISSET_ATTRBIT(&attrbits,
3661 					    NFSATTRBIT_SIZE)) {
3662 						if (vp != NULL)
3663 							va.va_size = np->n_size;
3664 						else
3665 							va.va_size =
3666 							    dp->nfsdl_size;
3667 						NFSSETBIT_ATTRBIT(&rattrbits,
3668 						    NFSATTRBIT_SIZE);
3669 					}
3670 					if (NFSISSET_ATTRBIT(&attrbits,
3671 					    NFSATTRBIT_CHANGE)) {
3672 						va.va_filerev =
3673 						    dp->nfsdl_change;
3674 						if (vp == NULL ||
3675 						    (np->n_flag & NDELEGMOD))
3676 							va.va_filerev++;
3677 						NFSSETBIT_ATTRBIT(&rattrbits,
3678 						    NFSATTRBIT_CHANGE);
3679 					}
3680 				} else
3681 					error = NFSERR_SERVERFAULT;
3682 				NFSUNLOCKCLSTATE();
3683 			}
3684 			if (vp != NULL)
3685 				vrele(vp);
3686 			if (mp != NULL)
3687 				vfs_unbusy(mp);
3688 			if (nfhp != NULL)
3689 				free(nfhp, M_NFSFH);
3690 			if (!error)
3691 				(void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3692 				    NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3693 				    (uint64_t)0, NULL);
3694 			break;
3695 		case NFSV4OP_CBRECALL:
3696 			NFSCL_DEBUG(4, "cbrecall\n");
3697 			NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3698 			    NFSX_UNSIGNED);
3699 			stateid.seqid = *tl++;
3700 			NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3701 			    NFSX_STATEIDOTHER);
3702 			tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3703 			trunc = fxdr_unsigned(int, *tl);
3704 			error = nfsm_getfh(nd, &nfhp);
3705 			if (!error) {
3706 				NFSLOCKCLSTATE();
3707 				if (minorvers == NFSV4_MINORVERSION)
3708 					clp = nfscl_getclnt(cbident);
3709 				else
3710 					clp = nfscl_getclntsess(sessionid);
3711 				if (clp != NULL) {
3712 					dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3713 					    nfhp->nfh_len);
3714 					if (dp != NULL && (dp->nfsdl_flags &
3715 					    NFSCLDL_DELEGRET) == 0) {
3716 						dp->nfsdl_flags |=
3717 						    NFSCLDL_RECALL;
3718 						wakeup((caddr_t)clp);
3719 					}
3720 				} else {
3721 					error = NFSERR_SERVERFAULT;
3722 				}
3723 				NFSUNLOCKCLSTATE();
3724 			}
3725 			if (nfhp != NULL)
3726 				free(nfhp, M_NFSFH);
3727 			break;
3728 		case NFSV4OP_CBLAYOUTRECALL:
3729 			NFSCL_DEBUG(4, "cblayrec\n");
3730 			nfhp = NULL;
3731 			NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3732 			laytype = fxdr_unsigned(int, *tl++);
3733 			iomode = fxdr_unsigned(uint32_t, *tl++);
3734 			if (newnfs_true == *tl++)
3735 				changed = 1;
3736 			else
3737 				changed = 0;
3738 			recalltype = fxdr_unsigned(int, *tl);
3739 			NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
3740 			    laytype, iomode, changed, recalltype);
3741 			recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3742 			    M_WAITOK);
3743 			if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
3744 			    laytype != NFSLAYOUT_FLEXFILE)
3745 				error = NFSERR_NOMATCHLAYOUT;
3746 			else if (recalltype == NFSLAYOUTRETURN_FILE) {
3747 				error = nfsm_getfh(nd, &nfhp);
3748 				NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3749 				if (error != 0)
3750 					goto nfsmout;
3751 				NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3752 				    NFSX_STATEID);
3753 				off = fxdr_hyper(tl); tl += 2;
3754 				len = fxdr_hyper(tl); tl += 2;
3755 				stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3756 				NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3757 				if (minorvers == NFSV4_MINORVERSION)
3758 					error = NFSERR_NOTSUPP;
3759 				NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
3760 				    (uintmax_t)off, (uintmax_t)len,
3761 				    stateid.seqid, error);
3762 				if (error == 0) {
3763 					NFSLOCKCLSTATE();
3764 					clp = nfscl_getclntsess(sessionid);
3765 					NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3766 					if (clp != NULL) {
3767 						lyp = nfscl_findlayout(clp,
3768 						    nfhp->nfh_fh,
3769 						    nfhp->nfh_len);
3770 						NFSCL_DEBUG(4, "cblyp=%p\n",
3771 						    lyp);
3772 						if (lyp != NULL &&
3773 						    (lyp->nfsly_flags &
3774 						     (NFSLY_FILES |
3775 						      NFSLY_FLEXFILE)) != 0 &&
3776 						    !NFSBCMP(stateid.other,
3777 						    lyp->nfsly_stateid.other,
3778 						    NFSX_STATEIDOTHER)) {
3779 							error =
3780 							    nfscl_layoutrecall(
3781 							    recalltype,
3782 							    lyp, iomode, off,
3783 							    len, stateid.seqid,
3784 							    0, 0, NULL,
3785 							    recallp);
3786 							if (error == 0 &&
3787 							    stateid.seqid >
3788 							    lyp->nfsly_stateid.seqid)
3789 								lyp->nfsly_stateid.seqid =
3790 								    stateid.seqid;
3791 							recallp = NULL;
3792 							wakeup(clp);
3793 							NFSCL_DEBUG(4,
3794 							    "aft layrcal=%d "
3795 							    "layseqid=%d\n",
3796 							    error,
3797 							    lyp->nfsly_stateid.seqid);
3798 						} else
3799 							error =
3800 							  NFSERR_NOMATCHLAYOUT;
3801 					} else
3802 						error = NFSERR_NOMATCHLAYOUT;
3803 					NFSUNLOCKCLSTATE();
3804 				}
3805 				free(nfhp, M_NFSFH);
3806 			} else if (recalltype == NFSLAYOUTRETURN_FSID) {
3807 				NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3808 				filesid[0] = fxdr_hyper(tl); tl += 2;
3809 				filesid[1] = fxdr_hyper(tl); tl += 2;
3810 				gotone = 0;
3811 				NFSLOCKCLSTATE();
3812 				clp = nfscl_getclntsess(sessionid);
3813 				if (clp != NULL) {
3814 					TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3815 					    nfsly_list) {
3816 						if (lyp->nfsly_filesid[0] ==
3817 						    filesid[0] &&
3818 						    lyp->nfsly_filesid[1] ==
3819 						    filesid[1]) {
3820 							error =
3821 							    nfscl_layoutrecall(
3822 							    recalltype,
3823 							    lyp, iomode, 0,
3824 							    UINT64_MAX,
3825 							    lyp->nfsly_stateid.seqid,
3826 							    0, 0, NULL,
3827 							    recallp);
3828 							recallp = NULL;
3829 							gotone = 1;
3830 						}
3831 					}
3832 					if (gotone != 0)
3833 						wakeup(clp);
3834 					else
3835 						error = NFSERR_NOMATCHLAYOUT;
3836 				} else
3837 					error = NFSERR_NOMATCHLAYOUT;
3838 				NFSUNLOCKCLSTATE();
3839 			} else if (recalltype == NFSLAYOUTRETURN_ALL) {
3840 				gotone = 0;
3841 				NFSLOCKCLSTATE();
3842 				clp = nfscl_getclntsess(sessionid);
3843 				if (clp != NULL) {
3844 					TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3845 					    nfsly_list) {
3846 						error = nfscl_layoutrecall(
3847 						    recalltype, lyp, iomode, 0,
3848 						    UINT64_MAX,
3849 						    lyp->nfsly_stateid.seqid,
3850 						    0, 0, NULL, recallp);
3851 						recallp = NULL;
3852 						gotone = 1;
3853 					}
3854 					if (gotone != 0)
3855 						wakeup(clp);
3856 					else
3857 						error = NFSERR_NOMATCHLAYOUT;
3858 				} else
3859 					error = NFSERR_NOMATCHLAYOUT;
3860 				NFSUNLOCKCLSTATE();
3861 			} else
3862 				error = NFSERR_NOMATCHLAYOUT;
3863 			if (recallp != NULL) {
3864 				free(recallp, M_NFSLAYRECALL);
3865 				recallp = NULL;
3866 			}
3867 			break;
3868 		case NFSV4OP_CBSEQUENCE:
3869 			if (i != 0) {
3870 			    error = NFSERR_SEQUENCEPOS;
3871 			    break;
3872 			}
3873 			NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3874 			    5 * NFSX_UNSIGNED);
3875 			bcopy(tl, sessionid, NFSX_V4SESSIONID);
3876 			tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3877 			seqid = fxdr_unsigned(uint32_t, *tl++);
3878 			slotid = fxdr_unsigned(uint32_t, *tl++);
3879 			highslot = fxdr_unsigned(uint32_t, *tl++);
3880 			cachethis = *tl++;
3881 			/* Throw away the referring call stuff. */
3882 			clist = fxdr_unsigned(int, *tl);
3883 			for (j = 0; j < clist; j++) {
3884 				NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3885 				    NFSX_UNSIGNED);
3886 				tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3887 				rcalls = fxdr_unsigned(int, *tl);
3888 				for (k = 0; k < rcalls; k++) {
3889 					NFSM_DISSECT(tl, uint32_t *,
3890 					    2 * NFSX_UNSIGNED);
3891 				}
3892 			}
3893 			NFSLOCKCLSTATE();
3894 			clp = nfscl_getclntsess(sessionid);
3895 			if (clp == NULL)
3896 				error = NFSERR_SERVERFAULT;
3897 			if (error == 0) {
3898 				tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3899 				error = nfsv4_seqsession(seqid, slotid,
3900 				    highslot, tsep->nfsess_cbslots, &rep,
3901 				    tsep->nfsess_backslots);
3902 			}
3903 			NFSUNLOCKCLSTATE();
3904 			if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
3905 				gotseq_ok = 1;
3906 				if (rep != NULL) {
3907 					/*
3908 					 * Handle a reply for a retried
3909 					 * callback.  The reply will be
3910 					 * re-inserted in the session cache
3911 					 * by the nfsv4_seqsess_cacherep() call
3912 					 * after out:
3913 					 */
3914 					KASSERT(error == NFSERR_REPLYFROMCACHE,
3915 					    ("cbsequence: non-NULL rep"));
3916 					NFSCL_DEBUG(4, "Got cbretry\n");
3917 					m_freem(nd->nd_mreq);
3918 					nd->nd_mreq = rep;
3919 					rep = NULL;
3920 					goto out;
3921 				}
3922 				NFSM_BUILD(tl, uint32_t *,
3923 				    NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3924 				bcopy(sessionid, tl, NFSX_V4SESSIONID);
3925 				tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3926 				*tl++ = txdr_unsigned(seqid);
3927 				*tl++ = txdr_unsigned(slotid);
3928 				*tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3929 				*tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3930 			}
3931 			break;
3932 		default:
3933 			if (i == 0 && minorvers != NFSV4_MINORVERSION)
3934 				error = NFSERR_OPNOTINSESS;
3935 			else {
3936 				NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3937 				error = NFSERR_NOTSUPP;
3938 			}
3939 			break;
3940 		}
3941 		if (error) {
3942 			if (error == EBADRPC || error == NFSERR_BADXDR) {
3943 				nd->nd_repstat = NFSERR_BADXDR;
3944 			} else {
3945 				nd->nd_repstat = error;
3946 			}
3947 			error = 0;
3948 		}
3949 		retops++;
3950 		if (nd->nd_repstat) {
3951 			*repp = nfscl_errmap(nd, minorvers);
3952 			break;
3953 		} else
3954 			*repp = 0;	/* NFS4_OK */
3955 	}
3956 nfsmout:
3957 	if (recallp != NULL)
3958 		free(recallp, M_NFSLAYRECALL);
3959 	if (error) {
3960 		if (error == EBADRPC || error == NFSERR_BADXDR)
3961 			nd->nd_repstat = NFSERR_BADXDR;
3962 		else
3963 			printf("nfsv4 comperr1=%d\n", error);
3964 	}
3965 	if (taglen == -1) {
3966 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3967 		*tl++ = 0;
3968 		*tl = 0;
3969 	} else {
3970 		*retopsp = txdr_unsigned(retops);
3971 	}
3972 	*nd->nd_errp = nfscl_errmap(nd, minorvers);
3973 out:
3974 	if (gotseq_ok != 0) {
3975 		rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3976 		NFSLOCKCLSTATE();
3977 		clp = nfscl_getclntsess(sessionid);
3978 		if (clp != NULL) {
3979 			tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3980 			nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3981 			    NFSERR_OK, &rep);
3982 			NFSUNLOCKCLSTATE();
3983 		} else {
3984 			NFSUNLOCKCLSTATE();
3985 			m_freem(rep);
3986 		}
3987 	}
3988 }
3989 
3990 /*
3991  * Generate the next cbident value. Basically just increment a static value
3992  * and then check that it isn't already in the list, if it has wrapped around.
3993  */
3994 static u_int32_t
nfscl_nextcbident(void)3995 nfscl_nextcbident(void)
3996 {
3997 	struct nfsclclient *clp;
3998 	int matched;
3999 	static u_int32_t nextcbident = 0;
4000 	static int haswrapped = 0;
4001 
4002 	nextcbident++;
4003 	if (nextcbident == 0)
4004 		haswrapped = 1;
4005 	if (haswrapped) {
4006 		/*
4007 		 * Search the clientid list for one already using this cbident.
4008 		 */
4009 		do {
4010 			matched = 0;
4011 			NFSLOCKCLSTATE();
4012 			LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
4013 				if (clp->nfsc_cbident == nextcbident) {
4014 					matched = 1;
4015 					break;
4016 				}
4017 			}
4018 			NFSUNLOCKCLSTATE();
4019 			if (matched == 1)
4020 				nextcbident++;
4021 		} while (matched);
4022 	}
4023 	return (nextcbident);
4024 }
4025 
4026 /*
4027  * Get the mount point related to a given cbident or session and busy it.
4028  */
4029 static mount_t
nfscl_getmnt(int minorvers,uint8_t * sessionid,u_int32_t cbident,struct nfsclclient ** clpp)4030 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
4031     struct nfsclclient **clpp)
4032 {
4033 	struct nfsclclient *clp;
4034 	mount_t mp;
4035 	int error;
4036 	struct nfsclsession *tsep;
4037 
4038 	*clpp = NULL;
4039 	NFSLOCKCLSTATE();
4040 	LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
4041 		tsep = nfsmnt_mdssession(clp->nfsc_nmp);
4042 		if (minorvers == NFSV4_MINORVERSION) {
4043 			if (clp->nfsc_cbident == cbident)
4044 				break;
4045 		} else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
4046 		    NFSX_V4SESSIONID))
4047 			break;
4048 	}
4049 	if (clp == NULL) {
4050 		NFSUNLOCKCLSTATE();
4051 		return (NULL);
4052 	}
4053 	mp = clp->nfsc_nmp->nm_mountp;
4054 	vfs_ref(mp);
4055 	NFSUNLOCKCLSTATE();
4056 	error = vfs_busy(mp, 0);
4057 	vfs_rel(mp);
4058 	if (error != 0)
4059 		return (NULL);
4060 	*clpp = clp;
4061 	return (mp);
4062 }
4063 
4064 /*
4065  * Get the clientid pointer related to a given cbident.
4066  */
4067 static struct nfsclclient *
nfscl_getclnt(u_int32_t cbident)4068 nfscl_getclnt(u_int32_t cbident)
4069 {
4070 	struct nfsclclient *clp;
4071 
4072 	LIST_FOREACH(clp, &nfsclhead, nfsc_list)
4073 		if (clp->nfsc_cbident == cbident)
4074 			break;
4075 	return (clp);
4076 }
4077 
4078 /*
4079  * Get the clientid pointer related to a given sessionid.
4080  */
4081 static struct nfsclclient *
nfscl_getclntsess(uint8_t * sessionid)4082 nfscl_getclntsess(uint8_t *sessionid)
4083 {
4084 	struct nfsclclient *clp;
4085 	struct nfsclsession *tsep;
4086 
4087 	LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
4088 		tsep = nfsmnt_mdssession(clp->nfsc_nmp);
4089 		if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
4090 		    NFSX_V4SESSIONID))
4091 			break;
4092 	}
4093 	return (clp);
4094 }
4095 
4096 /*
4097  * Search for a lock conflict locally on the client. A conflict occurs if
4098  * - not same owner and overlapping byte range and at least one of them is
4099  *   a write lock or this is an unlock.
4100  */
4101 static int
nfscl_localconflict(struct nfsclclient * clp,u_int8_t * fhp,int fhlen,struct nfscllock * nlop,u_int8_t * own,struct nfscldeleg * dp,struct nfscllock ** lopp)4102 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
4103     struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
4104     struct nfscllock **lopp)
4105 {
4106 	struct nfsclopen *op;
4107 	int ret;
4108 
4109 	if (dp != NULL) {
4110 		ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
4111 		if (ret)
4112 			return (ret);
4113 	}
4114 	LIST_FOREACH(op, NFSCLOPENHASH(clp, fhp, fhlen), nfso_hash) {
4115 		if (op->nfso_fhlen == fhlen &&
4116 		    !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
4117 			ret = nfscl_checkconflict(&op->nfso_lock, nlop,
4118 			    own, lopp);
4119 			if (ret)
4120 				return (ret);
4121 		}
4122 	}
4123 	return (0);
4124 }
4125 
4126 static int
nfscl_checkconflict(struct nfscllockownerhead * lhp,struct nfscllock * nlop,u_int8_t * own,struct nfscllock ** lopp)4127 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
4128     u_int8_t *own, struct nfscllock **lopp)
4129 {
4130 	struct nfscllockowner *lp;
4131 	struct nfscllock *lop;
4132 
4133 	LIST_FOREACH(lp, lhp, nfsl_list) {
4134 		if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
4135 			LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
4136 				if (lop->nfslo_first >= nlop->nfslo_end)
4137 					break;
4138 				if (lop->nfslo_end <= nlop->nfslo_first)
4139 					continue;
4140 				if (lop->nfslo_type == F_WRLCK ||
4141 				    nlop->nfslo_type == F_WRLCK ||
4142 				    nlop->nfslo_type == F_UNLCK) {
4143 					if (lopp != NULL)
4144 						*lopp = lop;
4145 					return (NFSERR_DENIED);
4146 				}
4147 			}
4148 		}
4149 	}
4150 	return (0);
4151 }
4152 
4153 /*
4154  * Check for a local conflicting lock.
4155  */
4156 int
nfscl_lockt(vnode_t vp,struct nfsclclient * clp,u_int64_t off,u_int64_t len,struct flock * fl,NFSPROC_T * p,void * id,int flags)4157 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
4158     u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
4159 {
4160 	struct nfscllock *lop, nlck;
4161 	struct nfscldeleg *dp;
4162 	struct nfsnode *np;
4163 	u_int8_t own[NFSV4CL_LOCKNAMELEN];
4164 	int error;
4165 
4166 	nlck.nfslo_type = fl->l_type;
4167 	nlck.nfslo_first = off;
4168 	if (len == NFS64BITSSET) {
4169 		nlck.nfslo_end = NFS64BITSSET;
4170 	} else {
4171 		nlck.nfslo_end = off + len;
4172 		if (nlck.nfslo_end <= nlck.nfslo_first)
4173 			return (NFSERR_INVAL);
4174 	}
4175 	np = VTONFS(vp);
4176 	nfscl_filllockowner(id, own, flags);
4177 	NFSLOCKCLSTATE();
4178 	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4179 	error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
4180 	    &nlck, own, dp, &lop);
4181 	if (error != 0) {
4182 		fl->l_whence = SEEK_SET;
4183 		fl->l_start = lop->nfslo_first;
4184 		if (lop->nfslo_end == NFS64BITSSET)
4185 			fl->l_len = 0;
4186 		else
4187 			fl->l_len = lop->nfslo_end - lop->nfslo_first;
4188 		fl->l_pid = (pid_t)0;
4189 		fl->l_type = lop->nfslo_type;
4190 		error = -1;			/* no RPC required */
4191 	} else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
4192 	    fl->l_type == F_RDLCK)) {
4193 		/*
4194 		 * The delegation ensures that there isn't a conflicting
4195 		 * lock on the server, so return -1 to indicate an RPC
4196 		 * isn't required.
4197 		 */
4198 		fl->l_type = F_UNLCK;
4199 		error = -1;
4200 	}
4201 	NFSUNLOCKCLSTATE();
4202 	return (error);
4203 }
4204 
4205 /*
4206  * Handle Recall of a delegation.
4207  * The clp must be exclusive locked when this is called.
4208  */
4209 static int
nfscl_recalldeleg(struct nfsclclient * clp,struct nfsmount * nmp,struct nfscldeleg * dp,vnode_t vp,struct ucred * cred,NFSPROC_T * p,int called_from_renewthread,vnode_t * vpp)4210 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
4211     struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4212     int called_from_renewthread, vnode_t *vpp)
4213 {
4214 	struct nfsclowner *owp, *lowp, *nowp;
4215 	struct nfsclopen *op, *lop;
4216 	struct nfscllockowner *lp;
4217 	struct nfscllock *lckp;
4218 	struct nfsnode *np;
4219 	int error = 0, ret;
4220 
4221 	if (vp == NULL) {
4222 		KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL"));
4223 		*vpp = NULL;
4224 		/*
4225 		 * First, get a vnode for the file. This is needed to do RPCs.
4226 		 */
4227 		ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
4228 		    dp->nfsdl_fhlen, p, &np);
4229 		if (ret) {
4230 			/*
4231 			 * File isn't open, so nothing to move over to the
4232 			 * server.
4233 			 */
4234 			return (0);
4235 		}
4236 		vp = NFSTOV(np);
4237 		*vpp = vp;
4238 	} else {
4239 		np = VTONFS(vp);
4240 	}
4241 	dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
4242 
4243 	/*
4244 	 * Ok, if it's a write delegation, flush data to the server, so
4245 	 * that close/open consistency is retained.
4246 	 */
4247 	ret = 0;
4248 	NFSLOCKNODE(np);
4249 	if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
4250 		np->n_flag |= NDELEGRECALL;
4251 		NFSUNLOCKNODE(np);
4252 		ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
4253 		NFSLOCKNODE(np);
4254 		np->n_flag &= ~NDELEGRECALL;
4255 	}
4256 	NFSINVALATTRCACHE(np);
4257 	NFSUNLOCKNODE(np);
4258 	if (ret == EIO && called_from_renewthread != 0) {
4259 		/*
4260 		 * If the flush failed with EIO for the renew thread,
4261 		 * return now, so that the dirty buffer will be flushed
4262 		 * later.
4263 		 */
4264 		return (ret);
4265 	}
4266 
4267 	/*
4268 	 * Now, for each openowner with opens issued locally, move them
4269 	 * over to state against the server.
4270 	 */
4271 	LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
4272 		lop = LIST_FIRST(&lowp->nfsow_open);
4273 		if (lop != NULL) {
4274 			if (LIST_NEXT(lop, nfso_list) != NULL)
4275 				panic("nfsdlg mult opens");
4276 			/*
4277 			 * Look for the same openowner against the server.
4278 			 */
4279 			LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
4280 				if (!NFSBCMP(lowp->nfsow_owner,
4281 				    owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
4282 					newnfs_copycred(&dp->nfsdl_cred, cred);
4283 					ret = nfscl_moveopen(vp, clp, nmp, lop,
4284 					    owp, dp, cred, p);
4285 					if (ret == NFSERR_STALECLIENTID ||
4286 					    ret == NFSERR_STALEDONTRECOVER ||
4287 					    ret == NFSERR_BADSESSION)
4288 						return (ret);
4289 					if (ret) {
4290 						nfscl_freeopen(lop, 1, true);
4291 						if (!error)
4292 							error = ret;
4293 					}
4294 					break;
4295 				}
4296 			}
4297 
4298 			/*
4299 			 * If no openowner found, create one and get an open
4300 			 * for it.
4301 			 */
4302 			if (owp == NULL) {
4303 				nowp = malloc(
4304 				    sizeof (struct nfsclowner), M_NFSCLOWNER,
4305 				    M_WAITOK);
4306 				nfscl_newopen(clp, NULL, &owp, &nowp, &op,
4307 				    NULL, lowp->nfsow_owner, dp->nfsdl_fh,
4308 				    dp->nfsdl_fhlen, NULL, NULL);
4309 				newnfs_copycred(&dp->nfsdl_cred, cred);
4310 				ret = nfscl_moveopen(vp, clp, nmp, lop,
4311 				    owp, dp, cred, p);
4312 				if (ret) {
4313 					nfscl_freeopenowner(owp, 0);
4314 					if (ret == NFSERR_STALECLIENTID ||
4315 					    ret == NFSERR_STALEDONTRECOVER ||
4316 					    ret == NFSERR_BADSESSION)
4317 						return (ret);
4318 					if (ret) {
4319 						nfscl_freeopen(lop, 1, true);
4320 						if (!error)
4321 							error = ret;
4322 					}
4323 				}
4324 			}
4325 		}
4326 	}
4327 
4328 	/*
4329 	 * Now, get byte range locks for any locks done locally.
4330 	 */
4331 	LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4332 		LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
4333 			newnfs_copycred(&dp->nfsdl_cred, cred);
4334 			ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
4335 			if (ret == NFSERR_STALESTATEID ||
4336 			    ret == NFSERR_STALEDONTRECOVER ||
4337 			    ret == NFSERR_STALECLIENTID ||
4338 			    ret == NFSERR_BADSESSION)
4339 				return (ret);
4340 			if (ret && !error)
4341 				error = ret;
4342 		}
4343 	}
4344 	return (error);
4345 }
4346 
4347 /*
4348  * Move a locally issued open over to an owner on the state list.
4349  * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
4350  * returns with it unlocked.
4351  */
4352 static int
nfscl_moveopen(vnode_t vp,struct nfsclclient * clp,struct nfsmount * nmp,struct nfsclopen * lop,struct nfsclowner * owp,struct nfscldeleg * dp,struct ucred * cred,NFSPROC_T * p)4353 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4354     struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
4355     struct ucred *cred, NFSPROC_T *p)
4356 {
4357 	struct nfsclopen *op, *nop;
4358 	struct nfscldeleg *ndp;
4359 	struct nfsnode *np;
4360 	int error = 0, newone;
4361 
4362 	/*
4363 	 * First, look for an appropriate open, If found, just increment the
4364 	 * opencnt in it.
4365 	 */
4366 	LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4367 		if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4368 		    op->nfso_fhlen == lop->nfso_fhlen &&
4369 		    !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4370 			op->nfso_opencnt += lop->nfso_opencnt;
4371 			nfscl_freeopen(lop, 1, true);
4372 			return (0);
4373 		}
4374 	}
4375 
4376 	/* No appropriate open, so we have to do one against the server. */
4377 	np = VTONFS(vp);
4378 	nop = malloc(sizeof (struct nfsclopen) +
4379 	    lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4380 	nop->nfso_hash.le_prev = NULL;
4381 	newone = 0;
4382 	nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4383 	    lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
4384 	ndp = dp;
4385 	error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4386 	    lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4387 	    NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4388 	if (error) {
4389 		if (newone)
4390 			nfscl_freeopen(op, 0, true);
4391 	} else {
4392 		op->nfso_mode |= lop->nfso_mode;
4393 		op->nfso_opencnt += lop->nfso_opencnt;
4394 		nfscl_freeopen(lop, 1, true);
4395 	}
4396 	if (nop != NULL)
4397 		free(nop, M_NFSCLOPEN);
4398 	if (ndp != NULL) {
4399 		/*
4400 		 * What should I do with the returned delegation, since the
4401 		 * delegation is being recalled? For now, just printf and
4402 		 * through it away.
4403 		 */
4404 		printf("Moveopen returned deleg\n");
4405 		free(ndp, M_NFSCLDELEG);
4406 	}
4407 	return (error);
4408 }
4409 
4410 /*
4411  * Recall all delegations on this client.
4412  */
4413 static void
nfscl_totalrecall(struct nfsclclient * clp)4414 nfscl_totalrecall(struct nfsclclient *clp)
4415 {
4416 	struct nfscldeleg *dp;
4417 
4418 	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4419 		if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4420 			dp->nfsdl_flags |= NFSCLDL_RECALL;
4421 	}
4422 }
4423 
4424 /*
4425  * Relock byte ranges. Called for delegation recall and state expiry.
4426  */
4427 static int
nfscl_relock(vnode_t vp,struct nfsclclient * clp,struct nfsmount * nmp,struct nfscllockowner * lp,struct nfscllock * lop,struct ucred * cred,NFSPROC_T * p)4428 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4429     struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4430     NFSPROC_T *p)
4431 {
4432 	struct nfscllockowner *nlp;
4433 	struct nfsfh *nfhp;
4434 	struct nfsnode *np;
4435 	u_int64_t off, len;
4436 	int error, newone, donelocally;
4437 
4438 	if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
4439 		np = VTONFS(vp);
4440 		NFSLOCKNODE(np);
4441 		np->n_flag |= NMIGHTBELOCKED;
4442 		NFSUNLOCKNODE(np);
4443 	}
4444 
4445 	off = lop->nfslo_first;
4446 	len = lop->nfslo_end - lop->nfslo_first;
4447 	error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4448 	    clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4449 	    lp->nfsl_openowner, &nlp, &newone, &donelocally);
4450 	if (error || donelocally)
4451 		return (error);
4452 	nfhp = VTONFS(vp)->n_fhp;
4453 	error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4454 	    nfhp->nfh_len, nlp, newone, 0, off,
4455 	    len, lop->nfslo_type, cred, p);
4456 	if (error)
4457 		nfscl_freelockowner(nlp, 0);
4458 	return (error);
4459 }
4460 
4461 /*
4462  * Called to re-open a file. Basically get a vnode for the file handle
4463  * and then call nfsrpc_openrpc() to do the rest.
4464  */
4465 static int
nfsrpc_reopen(struct nfsmount * nmp,u_int8_t * fhp,int fhlen,u_int32_t mode,struct nfsclopen * op,struct nfscldeleg ** dpp,struct ucred * cred,NFSPROC_T * p)4466 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4467     u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4468     struct ucred *cred, NFSPROC_T *p)
4469 {
4470 	struct nfsnode *np;
4471 	vnode_t vp;
4472 	int error;
4473 
4474 	error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4475 	if (error)
4476 		return (error);
4477 	vp = NFSTOV(np);
4478 	if (np->n_v4 != NULL) {
4479 		error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4480 		    np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4481 		    NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4482 		    cred, p);
4483 	} else {
4484 		error = EINVAL;
4485 	}
4486 	vrele(vp);
4487 	return (error);
4488 }
4489 
4490 /*
4491  * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4492  * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4493  * fail.
4494  */
4495 static int
nfscl_tryopen(struct nfsmount * nmp,vnode_t vp,u_int8_t * fhp,int fhlen,u_int8_t * newfhp,int newfhlen,u_int32_t mode,struct nfsclopen * op,u_int8_t * name,int namelen,struct nfscldeleg ** ndpp,int reclaim,u_int32_t delegtype,struct ucred * cred,NFSPROC_T * p)4496 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4497     u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4498     u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4499     int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4500 {
4501 	int error;
4502 
4503 	do {
4504 		error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4505 		    mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4506 		    0, 0);
4507 		if (error == NFSERR_DELAY)
4508 			(void) nfs_catnap(PZERO, error, "nfstryop");
4509 	} while (error == NFSERR_DELAY);
4510 	if (error == EAUTH || error == EACCES) {
4511 		/* Try again using system credentials */
4512 		newnfs_setroot(cred);
4513 		do {
4514 		    error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4515 			newfhlen, mode, op, name, namelen, ndpp, reclaim,
4516 			delegtype, cred, p, 1, 0);
4517 		    if (error == NFSERR_DELAY)
4518 			(void) nfs_catnap(PZERO, error, "nfstryop");
4519 		} while (error == NFSERR_DELAY);
4520 	}
4521 	return (error);
4522 }
4523 
4524 /*
4525  * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4526  * NFSERR_DELAY. Also, retry with system credentials, if the provided
4527  * cred don't work.
4528  */
4529 static int
nfscl_trylock(struct nfsmount * nmp,vnode_t vp,u_int8_t * fhp,int fhlen,struct nfscllockowner * nlp,int newone,int reclaim,u_int64_t off,u_int64_t len,short type,struct ucred * cred,NFSPROC_T * p)4530 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4531     int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4532     u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4533 {
4534 	struct nfsrv_descript nfsd, *nd = &nfsd;
4535 	int error;
4536 
4537 	do {
4538 		error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4539 		    reclaim, off, len, type, cred, p, 0);
4540 		if (!error && nd->nd_repstat == NFSERR_DELAY)
4541 			(void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4542 			    "nfstrylck");
4543 	} while (!error && nd->nd_repstat == NFSERR_DELAY);
4544 	if (!error)
4545 		error = nd->nd_repstat;
4546 	if (error == EAUTH || error == EACCES) {
4547 		/* Try again using root credentials */
4548 		newnfs_setroot(cred);
4549 		do {
4550 			error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4551 			    newone, reclaim, off, len, type, cred, p, 1);
4552 			if (!error && nd->nd_repstat == NFSERR_DELAY)
4553 				(void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4554 				    "nfstrylck");
4555 		} while (!error && nd->nd_repstat == NFSERR_DELAY);
4556 		if (!error)
4557 			error = nd->nd_repstat;
4558 	}
4559 	return (error);
4560 }
4561 
4562 /*
4563  * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4564  * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4565  * credentials fail.
4566  */
4567 static int
nfscl_trydelegreturn(struct nfscldeleg * dp,struct ucred * cred,struct nfsmount * nmp,NFSPROC_T * p)4568 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4569     struct nfsmount *nmp, NFSPROC_T *p)
4570 {
4571 	int error;
4572 
4573 	do {
4574 		error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4575 		if (error == NFSERR_DELAY)
4576 			(void) nfs_catnap(PZERO, error, "nfstrydp");
4577 	} while (error == NFSERR_DELAY);
4578 	if (error == EAUTH || error == EACCES) {
4579 		/* Try again using system credentials */
4580 		newnfs_setroot(cred);
4581 		do {
4582 			error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4583 			if (error == NFSERR_DELAY)
4584 				(void) nfs_catnap(PZERO, error, "nfstrydp");
4585 		} while (error == NFSERR_DELAY);
4586 	}
4587 	return (error);
4588 }
4589 
4590 /*
4591  * Try a close against the server. Just call nfsrpc_closerpc(),
4592  * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4593  * credentials fail.
4594  */
4595 int
nfscl_tryclose(struct nfsclopen * op,struct ucred * cred,struct nfsmount * nmp,NFSPROC_T * p,bool loop_on_delayed)4596 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4597     struct nfsmount *nmp, NFSPROC_T *p, bool loop_on_delayed)
4598 {
4599 	struct nfsrv_descript nfsd, *nd = &nfsd;
4600 	int error;
4601 
4602 	do {
4603 		error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4604 		if (loop_on_delayed && error == NFSERR_DELAY)
4605 			(void) nfs_catnap(PZERO, error, "nfstrycl");
4606 	} while (loop_on_delayed && error == NFSERR_DELAY);
4607 	if (error == EAUTH || error == EACCES) {
4608 		/* Try again using system credentials */
4609 		newnfs_setroot(cred);
4610 		do {
4611 			error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4612 			if (loop_on_delayed && error == NFSERR_DELAY)
4613 				(void) nfs_catnap(PZERO, error, "nfstrycl");
4614 		} while (loop_on_delayed && error == NFSERR_DELAY);
4615 	}
4616 	return (error);
4617 }
4618 
4619 /*
4620  * Decide if a delegation on a file permits close without flushing writes
4621  * to the server. This might be a big performance win in some environments.
4622  * (Not useful until the client does caching on local stable storage.)
4623  */
4624 int
nfscl_mustflush(vnode_t vp)4625 nfscl_mustflush(vnode_t vp)
4626 {
4627 	struct nfsclclient *clp;
4628 	struct nfscldeleg *dp;
4629 	struct nfsnode *np;
4630 	struct nfsmount *nmp;
4631 
4632 	np = VTONFS(vp);
4633 	nmp = VFSTONFS(vp->v_mount);
4634 	if (!NFSHASNFSV4(nmp))
4635 		return (1);
4636 	NFSLOCKMNT(nmp);
4637 	if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4638 		NFSUNLOCKMNT(nmp);
4639 		return (1);
4640 	}
4641 	NFSUNLOCKMNT(nmp);
4642 	NFSLOCKCLSTATE();
4643 	clp = nfscl_findcl(nmp);
4644 	if (clp == NULL) {
4645 		NFSUNLOCKCLSTATE();
4646 		return (1);
4647 	}
4648 	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4649 	if (dp != NULL && (dp->nfsdl_flags &
4650 	    (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4651 	     NFSCLDL_WRITE &&
4652 	    (dp->nfsdl_sizelimit >= np->n_size ||
4653 	     !NFSHASSTRICT3530(nmp))) {
4654 		NFSUNLOCKCLSTATE();
4655 		return (0);
4656 	}
4657 	NFSUNLOCKCLSTATE();
4658 	return (1);
4659 }
4660 
4661 /*
4662  * See if a (write) delegation exists for this file.
4663  */
4664 int
nfscl_nodeleg(vnode_t vp,int writedeleg)4665 nfscl_nodeleg(vnode_t vp, int writedeleg)
4666 {
4667 	struct nfsclclient *clp;
4668 	struct nfscldeleg *dp;
4669 	struct nfsnode *np;
4670 	struct nfsmount *nmp;
4671 
4672 	np = VTONFS(vp);
4673 	nmp = VFSTONFS(vp->v_mount);
4674 	if (!NFSHASNFSV4(nmp))
4675 		return (1);
4676 	NFSLOCKMNT(nmp);
4677 	if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4678 		NFSUNLOCKMNT(nmp);
4679 		return (1);
4680 	}
4681 	NFSUNLOCKMNT(nmp);
4682 	NFSLOCKCLSTATE();
4683 	clp = nfscl_findcl(nmp);
4684 	if (clp == NULL) {
4685 		NFSUNLOCKCLSTATE();
4686 		return (1);
4687 	}
4688 	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4689 	if (dp != NULL &&
4690 	    (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4691 	    (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4692 	     NFSCLDL_WRITE)) {
4693 		NFSUNLOCKCLSTATE();
4694 		return (0);
4695 	}
4696 	NFSUNLOCKCLSTATE();
4697 	return (1);
4698 }
4699 
4700 /*
4701  * Look for an associated delegation that should be DelegReturned.
4702  */
4703 int
nfscl_removedeleg(vnode_t vp,NFSPROC_T * p,nfsv4stateid_t * stp)4704 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4705 {
4706 	struct nfsclclient *clp;
4707 	struct nfscldeleg *dp;
4708 	struct nfsclowner *owp;
4709 	struct nfscllockowner *lp;
4710 	struct nfsmount *nmp;
4711 	struct mount *mp;
4712 	struct ucred *cred;
4713 	struct nfsnode *np;
4714 	int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4715 
4716 	nmp = VFSTONFS(vp->v_mount);
4717 	if (NFSHASPNFS(nmp))
4718 		return (retcnt);
4719 	NFSLOCKMNT(nmp);
4720 	if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4721 		NFSUNLOCKMNT(nmp);
4722 		return (retcnt);
4723 	}
4724 	NFSUNLOCKMNT(nmp);
4725 	np = VTONFS(vp);
4726 	mp = nmp->nm_mountp;
4727 	NFSLOCKCLSTATE();
4728 	/*
4729 	 * Loop around waiting for:
4730 	 * - outstanding I/O operations on delegations to complete
4731 	 * - for a delegation on vp that has state, lock the client and
4732 	 *   do a recall
4733 	 * - return delegation with no state
4734 	 */
4735 	while (1) {
4736 		clp = nfscl_findcl(nmp);
4737 		if (clp == NULL) {
4738 			NFSUNLOCKCLSTATE();
4739 			return (retcnt);
4740 		}
4741 		dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4742 		    np->n_fhp->nfh_len);
4743 		if (dp != NULL) {
4744 		    /*
4745 		     * Wait for outstanding I/O ops to be done.
4746 		     */
4747 		    if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4748 			if (igotlock) {
4749 			    nfsv4_unlock(&clp->nfsc_lock, 0);
4750 			    igotlock = 0;
4751 			}
4752 			dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4753 			msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
4754 			    "nfscld", hz);
4755 			if (NFSCL_FORCEDISM(mp)) {
4756 			    dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
4757 			    NFSUNLOCKCLSTATE();
4758 			    return (0);
4759 			}
4760 			continue;
4761 		    }
4762 		    needsrecall = 0;
4763 		    LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4764 			if (!LIST_EMPTY(&owp->nfsow_open)) {
4765 			    needsrecall = 1;
4766 			    break;
4767 			}
4768 		    }
4769 		    if (!needsrecall) {
4770 			LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4771 			    if (!LIST_EMPTY(&lp->nfsl_lock)) {
4772 				needsrecall = 1;
4773 				break;
4774 			    }
4775 			}
4776 		    }
4777 		    if (needsrecall && !triedrecall) {
4778 			dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4779 			islept = 0;
4780 			while (!igotlock) {
4781 			    igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4782 				&islept, NFSCLSTATEMUTEXPTR, mp);
4783 			    if (NFSCL_FORCEDISM(mp)) {
4784 				dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
4785 				if (igotlock)
4786 				    nfsv4_unlock(&clp->nfsc_lock, 0);
4787 				NFSUNLOCKCLSTATE();
4788 				return (0);
4789 			    }
4790 			    if (islept)
4791 				break;
4792 			}
4793 			if (islept)
4794 			    continue;
4795 			NFSUNLOCKCLSTATE();
4796 			cred = newnfs_getcred();
4797 			newnfs_copycred(&dp->nfsdl_cred, cred);
4798 			nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL);
4799 			NFSFREECRED(cred);
4800 			triedrecall = 1;
4801 			NFSLOCKCLSTATE();
4802 			nfsv4_unlock(&clp->nfsc_lock, 0);
4803 			igotlock = 0;
4804 			continue;
4805 		    }
4806 		    *stp = dp->nfsdl_stateid;
4807 		    retcnt = 1;
4808 		    nfscl_cleandeleg(dp);
4809 		    nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4810 		}
4811 		if (igotlock)
4812 		    nfsv4_unlock(&clp->nfsc_lock, 0);
4813 		NFSUNLOCKCLSTATE();
4814 		return (retcnt);
4815 	}
4816 }
4817 
4818 /*
4819  * Look for associated delegation(s) that should be DelegReturned.
4820  */
4821 int
nfscl_renamedeleg(vnode_t fvp,nfsv4stateid_t * fstp,int * gotfdp,vnode_t tvp,nfsv4stateid_t * tstp,int * gottdp,NFSPROC_T * p)4822 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4823     nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4824 {
4825 	struct nfsclclient *clp;
4826 	struct nfscldeleg *dp;
4827 	struct nfsclowner *owp;
4828 	struct nfscllockowner *lp;
4829 	struct nfsmount *nmp;
4830 	struct mount *mp;
4831 	struct ucred *cred;
4832 	struct nfsnode *np;
4833 	int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4834 
4835 	nmp = VFSTONFS(fvp->v_mount);
4836 	*gotfdp = 0;
4837 	*gottdp = 0;
4838 	if (NFSHASPNFS(nmp))
4839 		return (retcnt);
4840 	NFSLOCKMNT(nmp);
4841 	if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4842 		NFSUNLOCKMNT(nmp);
4843 		return (retcnt);
4844 	}
4845 	NFSUNLOCKMNT(nmp);
4846 	mp = nmp->nm_mountp;
4847 	NFSLOCKCLSTATE();
4848 	/*
4849 	 * Loop around waiting for:
4850 	 * - outstanding I/O operations on delegations to complete
4851 	 * - for a delegation on fvp that has state, lock the client and
4852 	 *   do a recall
4853 	 * - return delegation(s) with no state.
4854 	 */
4855 	while (1) {
4856 		clp = nfscl_findcl(nmp);
4857 		if (clp == NULL) {
4858 			NFSUNLOCKCLSTATE();
4859 			return (retcnt);
4860 		}
4861 		np = VTONFS(fvp);
4862 		dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4863 		    np->n_fhp->nfh_len);
4864 		if (dp != NULL && *gotfdp == 0) {
4865 		    /*
4866 		     * Wait for outstanding I/O ops to be done.
4867 		     */
4868 		    if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4869 			if (igotlock) {
4870 			    nfsv4_unlock(&clp->nfsc_lock, 0);
4871 			    igotlock = 0;
4872 			}
4873 			dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4874 			msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
4875 			    "nfscld", hz);
4876 			if (NFSCL_FORCEDISM(mp)) {
4877 			    dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
4878 			    NFSUNLOCKCLSTATE();
4879 			    *gotfdp = 0;
4880 			    *gottdp = 0;
4881 			    return (0);
4882 			}
4883 			continue;
4884 		    }
4885 		    needsrecall = 0;
4886 		    LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4887 			if (!LIST_EMPTY(&owp->nfsow_open)) {
4888 			    needsrecall = 1;
4889 			    break;
4890 			}
4891 		    }
4892 		    if (!needsrecall) {
4893 			LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4894 			    if (!LIST_EMPTY(&lp->nfsl_lock)) {
4895 				needsrecall = 1;
4896 				break;
4897 			    }
4898 			}
4899 		    }
4900 		    if (needsrecall && !triedrecall) {
4901 			dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4902 			islept = 0;
4903 			while (!igotlock) {
4904 			    igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4905 				&islept, NFSCLSTATEMUTEXPTR, mp);
4906 			    if (NFSCL_FORCEDISM(mp)) {
4907 				dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
4908 				if (igotlock)
4909 				    nfsv4_unlock(&clp->nfsc_lock, 0);
4910 				NFSUNLOCKCLSTATE();
4911 				*gotfdp = 0;
4912 				*gottdp = 0;
4913 				return (0);
4914 			    }
4915 			    if (islept)
4916 				break;
4917 			}
4918 			if (islept)
4919 			    continue;
4920 			NFSUNLOCKCLSTATE();
4921 			cred = newnfs_getcred();
4922 			newnfs_copycred(&dp->nfsdl_cred, cred);
4923 			nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL);
4924 			NFSFREECRED(cred);
4925 			triedrecall = 1;
4926 			NFSLOCKCLSTATE();
4927 			nfsv4_unlock(&clp->nfsc_lock, 0);
4928 			igotlock = 0;
4929 			continue;
4930 		    }
4931 		    *fstp = dp->nfsdl_stateid;
4932 		    retcnt++;
4933 		    *gotfdp = 1;
4934 		    nfscl_cleandeleg(dp);
4935 		    nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4936 		}
4937 		if (igotlock) {
4938 		    nfsv4_unlock(&clp->nfsc_lock, 0);
4939 		    igotlock = 0;
4940 		}
4941 		if (tvp != NULL) {
4942 		    np = VTONFS(tvp);
4943 		    dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4944 			np->n_fhp->nfh_len);
4945 		    if (dp != NULL && *gottdp == 0) {
4946 			/*
4947 			 * Wait for outstanding I/O ops to be done.
4948 			 */
4949 			if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4950 			    dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4951 			    msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
4952 				"nfscld", hz);
4953 			    if (NFSCL_FORCEDISM(mp)) {
4954 				NFSUNLOCKCLSTATE();
4955 				*gotfdp = 0;
4956 				*gottdp = 0;
4957 				return (0);
4958 			    }
4959 			    continue;
4960 			}
4961 			LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4962 			    if (!LIST_EMPTY(&owp->nfsow_open)) {
4963 				NFSUNLOCKCLSTATE();
4964 				return (retcnt);
4965 			    }
4966 			}
4967 			LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4968 			    if (!LIST_EMPTY(&lp->nfsl_lock)) {
4969 				NFSUNLOCKCLSTATE();
4970 				return (retcnt);
4971 			    }
4972 			}
4973 			*tstp = dp->nfsdl_stateid;
4974 			retcnt++;
4975 			*gottdp = 1;
4976 			nfscl_cleandeleg(dp);
4977 			nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4978 		    }
4979 		}
4980 		NFSUNLOCKCLSTATE();
4981 		return (retcnt);
4982 	}
4983 }
4984 
4985 /*
4986  * Get a reference on the clientid associated with the mount point.
4987  * Return 1 if success, 0 otherwise.
4988  */
4989 int
nfscl_getref(struct nfsmount * nmp)4990 nfscl_getref(struct nfsmount *nmp)
4991 {
4992 	struct nfsclclient *clp;
4993 	int ret;
4994 
4995 	NFSLOCKCLSTATE();
4996 	clp = nfscl_findcl(nmp);
4997 	if (clp == NULL) {
4998 		NFSUNLOCKCLSTATE();
4999 		return (0);
5000 	}
5001 	nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, nmp->nm_mountp);
5002 	ret = 1;
5003 	if (NFSCL_FORCEDISM(nmp->nm_mountp))
5004 		ret = 0;
5005 	NFSUNLOCKCLSTATE();
5006 	return (ret);
5007 }
5008 
5009 /*
5010  * Release a reference on a clientid acquired with the above call.
5011  */
5012 void
nfscl_relref(struct nfsmount * nmp)5013 nfscl_relref(struct nfsmount *nmp)
5014 {
5015 	struct nfsclclient *clp;
5016 
5017 	NFSLOCKCLSTATE();
5018 	clp = nfscl_findcl(nmp);
5019 	if (clp == NULL) {
5020 		NFSUNLOCKCLSTATE();
5021 		return;
5022 	}
5023 	nfsv4_relref(&clp->nfsc_lock);
5024 	NFSUNLOCKCLSTATE();
5025 }
5026 
5027 /*
5028  * Save the size attribute in the delegation, since the nfsnode
5029  * is going away.
5030  */
5031 void
nfscl_reclaimnode(vnode_t vp)5032 nfscl_reclaimnode(vnode_t vp)
5033 {
5034 	struct nfsclclient *clp;
5035 	struct nfscldeleg *dp;
5036 	struct nfsnode *np = VTONFS(vp);
5037 	struct nfsmount *nmp;
5038 
5039 	nmp = VFSTONFS(vp->v_mount);
5040 	if (!NFSHASNFSV4(nmp))
5041 		return;
5042 	NFSLOCKCLSTATE();
5043 	clp = nfscl_findcl(nmp);
5044 	if (clp == NULL) {
5045 		NFSUNLOCKCLSTATE();
5046 		return;
5047 	}
5048 	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5049 	if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
5050 		dp->nfsdl_size = np->n_size;
5051 	NFSUNLOCKCLSTATE();
5052 }
5053 
5054 /*
5055  * Get the saved size attribute in the delegation, since it is a
5056  * newly allocated nfsnode.
5057  */
5058 void
nfscl_newnode(vnode_t vp)5059 nfscl_newnode(vnode_t vp)
5060 {
5061 	struct nfsclclient *clp;
5062 	struct nfscldeleg *dp;
5063 	struct nfsnode *np = VTONFS(vp);
5064 	struct nfsmount *nmp;
5065 
5066 	nmp = VFSTONFS(vp->v_mount);
5067 	if (!NFSHASNFSV4(nmp))
5068 		return;
5069 	NFSLOCKCLSTATE();
5070 	clp = nfscl_findcl(nmp);
5071 	if (clp == NULL) {
5072 		NFSUNLOCKCLSTATE();
5073 		return;
5074 	}
5075 	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5076 	if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
5077 		np->n_size = dp->nfsdl_size;
5078 	NFSUNLOCKCLSTATE();
5079 }
5080 
5081 /*
5082  * If there is a valid write delegation for this file, set the modtime
5083  * to the local clock time.
5084  */
5085 void
nfscl_delegmodtime(struct vnode * vp,struct timespec * mtime)5086 nfscl_delegmodtime(struct vnode *vp, struct timespec *mtime)
5087 {
5088 	struct nfsclclient *clp;
5089 	struct nfscldeleg *dp;
5090 	struct nfsnode *np = VTONFS(vp);
5091 	struct nfsmount *nmp;
5092 
5093 	nmp = VFSTONFS(vp->v_mount);
5094 	if (!NFSHASNFSV4(nmp))
5095 		return;
5096 	NFSLOCKMNT(nmp);
5097 	if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
5098 		NFSUNLOCKMNT(nmp);
5099 		return;
5100 	}
5101 	NFSUNLOCKMNT(nmp);
5102 	NFSLOCKCLSTATE();
5103 	clp = nfscl_findcl(nmp);
5104 	if (clp == NULL) {
5105 		NFSUNLOCKCLSTATE();
5106 		return;
5107 	}
5108 	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5109 	if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
5110 		if (mtime != NULL)
5111 			dp->nfsdl_modtime = *mtime;
5112 		else
5113 			nanotime(&dp->nfsdl_modtime);
5114 		dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
5115 	}
5116 	NFSUNLOCKCLSTATE();
5117 }
5118 
5119 /*
5120  * If there is a valid write delegation for this file with a modtime set,
5121  * put that modtime in mtime.
5122  */
5123 void
nfscl_deleggetmodtime(vnode_t vp,struct timespec * mtime)5124 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
5125 {
5126 	struct nfsclclient *clp;
5127 	struct nfscldeleg *dp;
5128 	struct nfsnode *np = VTONFS(vp);
5129 	struct nfsmount *nmp;
5130 
5131 	nmp = VFSTONFS(vp->v_mount);
5132 	if (!NFSHASNFSV4(nmp))
5133 		return;
5134 	NFSLOCKMNT(nmp);
5135 	if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
5136 		NFSUNLOCKMNT(nmp);
5137 		return;
5138 	}
5139 	NFSUNLOCKMNT(nmp);
5140 	NFSLOCKCLSTATE();
5141 	clp = nfscl_findcl(nmp);
5142 	if (clp == NULL) {
5143 		NFSUNLOCKCLSTATE();
5144 		return;
5145 	}
5146 	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5147 	if (dp != NULL &&
5148 	    (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
5149 	    (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
5150 		*mtime = dp->nfsdl_modtime;
5151 	NFSUNLOCKCLSTATE();
5152 }
5153 
5154 static int
nfscl_errmap(struct nfsrv_descript * nd,u_int32_t minorvers)5155 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
5156 {
5157 	short *defaulterrp, *errp;
5158 
5159 	if (!nd->nd_repstat)
5160 		return (0);
5161 	if (nd->nd_procnum == NFSPROC_NOOP)
5162 		return (txdr_unsigned(nd->nd_repstat & 0xffff));
5163 	if (nd->nd_repstat == EBADRPC)
5164 		return (txdr_unsigned(NFSERR_BADXDR));
5165 	if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
5166 	    nd->nd_repstat == NFSERR_OPILLEGAL)
5167 		return (txdr_unsigned(nd->nd_repstat));
5168 	if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
5169 	    minorvers > NFSV4_MINORVERSION) {
5170 		/* NFSv4.n error. */
5171 		return (txdr_unsigned(nd->nd_repstat));
5172 	}
5173 	if (nd->nd_procnum < NFSV4OP_CBNOPS)
5174 		errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
5175 	else
5176 		return (txdr_unsigned(nd->nd_repstat));
5177 	while (*++errp)
5178 		if (*errp == (short)nd->nd_repstat)
5179 			return (txdr_unsigned(nd->nd_repstat));
5180 	return (txdr_unsigned(*defaulterrp));
5181 }
5182 
5183 /*
5184  * Called to find/add a layout to a client.
5185  * This function returns the layout with a refcnt (shared lock) upon
5186  * success (returns 0) or with no lock/refcnt on the layout when an
5187  * error is returned.
5188  * If a layout is passed in via lypp, it is locked (exclusively locked).
5189  */
5190 int
nfscl_layout(struct nfsmount * nmp,vnode_t vp,u_int8_t * fhp,int fhlen,nfsv4stateid_t * stateidp,int layouttype,int retonclose,struct nfsclflayouthead * fhlp,struct nfscllayout ** lypp,struct ucred * cred,NFSPROC_T * p)5191 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
5192     nfsv4stateid_t *stateidp, int layouttype, int retonclose,
5193     struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
5194     struct ucred *cred, NFSPROC_T *p)
5195 {
5196 	struct nfsclclient *clp;
5197 	struct nfscllayout *lyp, *tlyp;
5198 	struct nfsclflayout *flp;
5199 	struct nfsnode *np = VTONFS(vp);
5200 	mount_t mp;
5201 	int layout_passed_in;
5202 
5203 	mp = nmp->nm_mountp;
5204 	layout_passed_in = 1;
5205 	tlyp = NULL;
5206 	lyp = *lypp;
5207 	if (lyp == NULL) {
5208 		layout_passed_in = 0;
5209 		tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
5210 		    M_WAITOK | M_ZERO);
5211 	}
5212 
5213 	NFSLOCKCLSTATE();
5214 	clp = nmp->nm_clp;
5215 	if (clp == NULL) {
5216 		if (layout_passed_in != 0)
5217 			nfsv4_unlock(&lyp->nfsly_lock, 0);
5218 		NFSUNLOCKCLSTATE();
5219 		if (tlyp != NULL)
5220 			free(tlyp, M_NFSLAYOUT);
5221 		return (EPERM);
5222 	}
5223 	if (lyp == NULL) {
5224 		/*
5225 		 * Although no lyp was passed in, another thread might have
5226 		 * allocated one. If one is found, just increment it's ref
5227 		 * count and return it.
5228 		 */
5229 		lyp = nfscl_findlayout(clp, fhp, fhlen);
5230 		if (lyp == NULL) {
5231 			lyp = tlyp;
5232 			tlyp = NULL;
5233 			lyp->nfsly_stateid.seqid = stateidp->seqid;
5234 			lyp->nfsly_stateid.other[0] = stateidp->other[0];
5235 			lyp->nfsly_stateid.other[1] = stateidp->other[1];
5236 			lyp->nfsly_stateid.other[2] = stateidp->other[2];
5237 			lyp->nfsly_lastbyte = 0;
5238 			LIST_INIT(&lyp->nfsly_flayread);
5239 			LIST_INIT(&lyp->nfsly_flayrw);
5240 			LIST_INIT(&lyp->nfsly_recall);
5241 			lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
5242 			lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
5243 			lyp->nfsly_clp = clp;
5244 			if (layouttype == NFSLAYOUT_FLEXFILE)
5245 				lyp->nfsly_flags = NFSLY_FLEXFILE;
5246 			else
5247 				lyp->nfsly_flags = NFSLY_FILES;
5248 			if (retonclose != 0)
5249 				lyp->nfsly_flags |= NFSLY_RETONCLOSE;
5250 			lyp->nfsly_fhlen = fhlen;
5251 			NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
5252 			TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5253 			LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
5254 			    nfsly_hash);
5255 			lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5256 			nfscl_layoutcnt++;
5257 			nfsstatsv1.cllayouts++;
5258 		} else {
5259 			if (retonclose != 0)
5260 				lyp->nfsly_flags |= NFSLY_RETONCLOSE;
5261 			if (stateidp->seqid > lyp->nfsly_stateid.seqid)
5262 				lyp->nfsly_stateid.seqid = stateidp->seqid;
5263 			TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5264 			TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5265 			lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5266 		}
5267 		nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5268 		if (NFSCL_FORCEDISM(mp)) {
5269 			NFSUNLOCKCLSTATE();
5270 			if (tlyp != NULL)
5271 				free(tlyp, M_NFSLAYOUT);
5272 			return (EPERM);
5273 		}
5274 		*lypp = lyp;
5275 	} else if (stateidp->seqid > lyp->nfsly_stateid.seqid)
5276 		lyp->nfsly_stateid.seqid = stateidp->seqid;
5277 
5278 	/* Merge the new list of File Layouts into the list. */
5279 	flp = LIST_FIRST(fhlp);
5280 	if (flp != NULL) {
5281 		if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
5282 			nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
5283 		else
5284 			nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
5285 	}
5286 	if (layout_passed_in != 0)
5287 		nfsv4_unlock(&lyp->nfsly_lock, 1);
5288 	NFSUNLOCKCLSTATE();
5289 	if (tlyp != NULL)
5290 		free(tlyp, M_NFSLAYOUT);
5291 	return (0);
5292 }
5293 
5294 /*
5295  * Search for a layout by MDS file handle.
5296  * If one is found, it is returned with a refcnt (shared lock) iff
5297  * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
5298  * returned NULL.
5299  */
5300 struct nfscllayout *
nfscl_getlayout(struct nfsclclient * clp,uint8_t * fhp,int fhlen,uint64_t off,uint32_t rwaccess,struct nfsclflayout ** retflpp,int * recalledp)5301 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
5302     uint64_t off, uint32_t rwaccess, struct nfsclflayout **retflpp,
5303     int *recalledp)
5304 {
5305 	struct nfscllayout *lyp;
5306 	mount_t mp;
5307 	int error, igotlock;
5308 
5309 	mp = clp->nfsc_nmp->nm_mountp;
5310 	*recalledp = 0;
5311 	*retflpp = NULL;
5312 	NFSLOCKCLSTATE();
5313 	lyp = nfscl_findlayout(clp, fhp, fhlen);
5314 	if (lyp != NULL) {
5315 		if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5316 			TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5317 			TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5318 			lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5319 			error = nfscl_findlayoutforio(lyp, off, rwaccess,
5320 			    retflpp);
5321 			if (error == 0)
5322 				nfsv4_getref(&lyp->nfsly_lock, NULL,
5323 				    NFSCLSTATEMUTEXPTR, mp);
5324 			else {
5325 				do {
5326 					igotlock = nfsv4_lock(&lyp->nfsly_lock,
5327 					    1, NULL, NFSCLSTATEMUTEXPTR, mp);
5328 				} while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
5329 				*retflpp = NULL;
5330 			}
5331 			if (NFSCL_FORCEDISM(mp)) {
5332 				lyp = NULL;
5333 				*recalledp = 1;
5334 			}
5335 		} else {
5336 			lyp = NULL;
5337 			*recalledp = 1;
5338 		}
5339 	}
5340 	NFSUNLOCKCLSTATE();
5341 	return (lyp);
5342 }
5343 
5344 /*
5345  * Search for a layout by MDS file handle. If one is found, mark in to be
5346  * recalled, if it already marked "return on close".
5347  */
5348 static void
nfscl_retoncloselayout(vnode_t vp,struct nfsclclient * clp,uint8_t * fhp,int fhlen,struct nfsclrecalllayout ** recallpp,struct nfscllayout ** lypp)5349 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
5350     int fhlen, struct nfsclrecalllayout **recallpp, struct nfscllayout **lypp)
5351 {
5352 	struct nfscllayout *lyp;
5353 	uint32_t iomode;
5354 
5355 	*lypp = NULL;
5356 	if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) ||
5357 	    nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5358 	    (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
5359 		return;
5360 	lyp = nfscl_findlayout(clp, fhp, fhlen);
5361 	if (lyp != NULL && (lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) {
5362 		if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5363 			iomode = 0;
5364 			if (!LIST_EMPTY(&lyp->nfsly_flayread))
5365 				iomode |= NFSLAYOUTIOMODE_READ;
5366 			if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5367 				iomode |= NFSLAYOUTIOMODE_RW;
5368 			nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5369 			    0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
5370 			    *recallpp);
5371 			NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
5372 			*recallpp = NULL;
5373 		}
5374 
5375 		/* Now, wake up renew thread to do LayoutReturn. */
5376 		wakeup(clp);
5377 		*lypp = lyp;
5378 	}
5379 }
5380 
5381 /*
5382  * Mark the layout to be recalled and with an error.
5383  * Also, disable the dsp from further use.
5384  */
5385 void
nfscl_dserr(uint32_t op,uint32_t stat,struct nfscldevinfo * dp,struct nfscllayout * lyp,struct nfsclds * dsp)5386 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
5387     struct nfscllayout *lyp, struct nfsclds *dsp)
5388 {
5389 	struct nfsclrecalllayout *recallp;
5390 	uint32_t iomode;
5391 
5392 	printf("DS being disabled, error=%d\n", stat);
5393 	/* Set up the return of the layout. */
5394 	recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
5395 	iomode = 0;
5396 	NFSLOCKCLSTATE();
5397 	if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5398 		if (!LIST_EMPTY(&lyp->nfsly_flayread))
5399 			iomode |= NFSLAYOUTIOMODE_READ;
5400 		if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5401 			iomode |= NFSLAYOUTIOMODE_RW;
5402 		(void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5403 		    0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
5404 		    dp->nfsdi_deviceid, recallp);
5405 		NFSUNLOCKCLSTATE();
5406 		NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
5407 	} else {
5408 		NFSUNLOCKCLSTATE();
5409 		free(recallp, M_NFSLAYRECALL);
5410 	}
5411 
5412 	/* And shut the TCP connection down. */
5413 	nfscl_cancelreqs(dsp);
5414 }
5415 
5416 /*
5417  * Cancel all RPCs for this "dsp" by closing the connection.
5418  * Also, mark the session as defunct.
5419  * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
5420  * cannot be shut down.
5421  */
5422 void
nfscl_cancelreqs(struct nfsclds * dsp)5423 nfscl_cancelreqs(struct nfsclds *dsp)
5424 {
5425 	struct __rpc_client *cl;
5426 	static int non_event;
5427 
5428 	NFSLOCKDS(dsp);
5429 	if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
5430 	    dsp->nfsclds_sockp != NULL &&
5431 	    dsp->nfsclds_sockp->nr_client != NULL) {
5432 		dsp->nfsclds_flags |= NFSCLDS_CLOSED;
5433 		cl = dsp->nfsclds_sockp->nr_client;
5434 		dsp->nfsclds_sess.nfsess_defunct = 1;
5435 		NFSUNLOCKDS(dsp);
5436 		CLNT_CLOSE(cl);
5437 		/*
5438 		 * This 1sec sleep is done to reduce the number of reconnect
5439 		 * attempts made on the DS while it has failed.
5440 		 */
5441 		tsleep(&non_event, PVFS, "ndscls", hz);
5442 		return;
5443 	}
5444 	NFSUNLOCKDS(dsp);
5445 }
5446 
5447 /*
5448  * Dereference a layout.
5449  */
5450 void
nfscl_rellayout(struct nfscllayout * lyp,int exclocked)5451 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
5452 {
5453 
5454 	NFSLOCKCLSTATE();
5455 	if (exclocked != 0)
5456 		nfsv4_unlock(&lyp->nfsly_lock, 0);
5457 	else
5458 		nfsv4_relref(&lyp->nfsly_lock);
5459 	NFSUNLOCKCLSTATE();
5460 }
5461 
5462 /*
5463  * Search for a devinfo by deviceid. If one is found, return it after
5464  * acquiring a reference count on it.
5465  */
5466 struct nfscldevinfo *
nfscl_getdevinfo(struct nfsclclient * clp,uint8_t * deviceid,struct nfscldevinfo * dip)5467 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
5468     struct nfscldevinfo *dip)
5469 {
5470 
5471 	NFSLOCKCLSTATE();
5472 	if (dip == NULL)
5473 		dip = nfscl_finddevinfo(clp, deviceid);
5474 	if (dip != NULL)
5475 		dip->nfsdi_refcnt++;
5476 	NFSUNLOCKCLSTATE();
5477 	return (dip);
5478 }
5479 
5480 /*
5481  * Dereference a devinfo structure.
5482  */
5483 static void
nfscl_reldevinfo_locked(struct nfscldevinfo * dip)5484 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
5485 {
5486 
5487 	dip->nfsdi_refcnt--;
5488 	if (dip->nfsdi_refcnt == 0)
5489 		wakeup(&dip->nfsdi_refcnt);
5490 }
5491 
5492 /*
5493  * Dereference a devinfo structure.
5494  */
5495 void
nfscl_reldevinfo(struct nfscldevinfo * dip)5496 nfscl_reldevinfo(struct nfscldevinfo *dip)
5497 {
5498 
5499 	NFSLOCKCLSTATE();
5500 	nfscl_reldevinfo_locked(dip);
5501 	NFSUNLOCKCLSTATE();
5502 }
5503 
5504 /*
5505  * Find a layout for this file handle. Return NULL upon failure.
5506  */
5507 static struct nfscllayout *
nfscl_findlayout(struct nfsclclient * clp,u_int8_t * fhp,int fhlen)5508 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
5509 {
5510 	struct nfscllayout *lyp;
5511 
5512 	LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
5513 		if (lyp->nfsly_fhlen == fhlen &&
5514 		    !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
5515 			break;
5516 	return (lyp);
5517 }
5518 
5519 /*
5520  * Find a devinfo for this deviceid. Return NULL upon failure.
5521  */
5522 static struct nfscldevinfo *
nfscl_finddevinfo(struct nfsclclient * clp,uint8_t * deviceid)5523 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
5524 {
5525 	struct nfscldevinfo *dip;
5526 
5527 	LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
5528 		if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5529 		    == 0)
5530 			break;
5531 	return (dip);
5532 }
5533 
5534 /*
5535  * Merge the new file layout list into the main one, maintaining it in
5536  * increasing offset order.
5537  */
5538 static void
nfscl_mergeflayouts(struct nfsclflayouthead * fhlp,struct nfsclflayouthead * newfhlp)5539 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5540     struct nfsclflayouthead *newfhlp)
5541 {
5542 	struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5543 
5544 	flp = LIST_FIRST(fhlp);
5545 	prevflp = NULL;
5546 	LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5547 		while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5548 			prevflp = flp;
5549 			flp = LIST_NEXT(flp, nfsfl_list);
5550 		}
5551 		if (prevflp == NULL)
5552 			LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5553 		else
5554 			LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5555 		prevflp = nflp;
5556 	}
5557 }
5558 
5559 /*
5560  * Add this nfscldevinfo to the client, if it doesn't already exist.
5561  * This function consumes the structure pointed at by dip, if not NULL.
5562  */
5563 int
nfscl_adddevinfo(struct nfsmount * nmp,struct nfscldevinfo * dip,int ind,struct nfsclflayout * flp)5564 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
5565     struct nfsclflayout *flp)
5566 {
5567 	struct nfsclclient *clp;
5568 	struct nfscldevinfo *tdip;
5569 	uint8_t *dev;
5570 
5571 	NFSLOCKCLSTATE();
5572 	clp = nmp->nm_clp;
5573 	if (clp == NULL) {
5574 		NFSUNLOCKCLSTATE();
5575 		if (dip != NULL)
5576 			free(dip, M_NFSDEVINFO);
5577 		return (ENODEV);
5578 	}
5579 	if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5580 		dev = flp->nfsfl_dev;
5581 	else
5582 		dev = flp->nfsfl_ffm[ind].dev;
5583 	tdip = nfscl_finddevinfo(clp, dev);
5584 	if (tdip != NULL) {
5585 		tdip->nfsdi_layoutrefs++;
5586 		if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5587 			flp->nfsfl_devp = tdip;
5588 		else
5589 			flp->nfsfl_ffm[ind].devp = tdip;
5590 		nfscl_reldevinfo_locked(tdip);
5591 		NFSUNLOCKCLSTATE();
5592 		if (dip != NULL)
5593 			free(dip, M_NFSDEVINFO);
5594 		return (0);
5595 	}
5596 	if (dip != NULL) {
5597 		LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5598 		dip->nfsdi_layoutrefs = 1;
5599 		if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5600 			flp->nfsfl_devp = dip;
5601 		else
5602 			flp->nfsfl_ffm[ind].devp = dip;
5603 	}
5604 	NFSUNLOCKCLSTATE();
5605 	if (dip == NULL)
5606 		return (ENODEV);
5607 	return (0);
5608 }
5609 
5610 /*
5611  * Free up a layout structure and associated file layout structure(s).
5612  */
5613 void
nfscl_freelayout(struct nfscllayout * layp)5614 nfscl_freelayout(struct nfscllayout *layp)
5615 {
5616 	struct nfsclflayout *flp, *nflp;
5617 	struct nfsclrecalllayout *rp, *nrp;
5618 
5619 	LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5620 		LIST_REMOVE(flp, nfsfl_list);
5621 		nfscl_freeflayout(flp);
5622 	}
5623 	LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5624 		LIST_REMOVE(flp, nfsfl_list);
5625 		nfscl_freeflayout(flp);
5626 	}
5627 	LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5628 		LIST_REMOVE(rp, nfsrecly_list);
5629 		free(rp, M_NFSLAYRECALL);
5630 	}
5631 	nfscl_layoutcnt--;
5632 	nfsstatsv1.cllayouts--;
5633 	free(layp, M_NFSLAYOUT);
5634 }
5635 
5636 /*
5637  * Free up a file layout structure.
5638  */
5639 void
nfscl_freeflayout(struct nfsclflayout * flp)5640 nfscl_freeflayout(struct nfsclflayout *flp)
5641 {
5642 	int i, j;
5643 
5644 	if ((flp->nfsfl_flags & NFSFL_FILE) != 0) {
5645 		for (i = 0; i < flp->nfsfl_fhcnt; i++)
5646 			free(flp->nfsfl_fh[i], M_NFSFH);
5647 		if (flp->nfsfl_devp != NULL)
5648 			flp->nfsfl_devp->nfsdi_layoutrefs--;
5649 	}
5650 	if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
5651 		for (i = 0; i < flp->nfsfl_mirrorcnt; i++) {
5652 			for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
5653 				free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
5654 			if (flp->nfsfl_ffm[i].devp != NULL)
5655 				flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--;
5656 		}
5657 	free(flp, M_NFSFLAYOUT);
5658 }
5659 
5660 /*
5661  * Free up a file layout devinfo structure.
5662  */
5663 void
nfscl_freedevinfo(struct nfscldevinfo * dip)5664 nfscl_freedevinfo(struct nfscldevinfo *dip)
5665 {
5666 
5667 	free(dip, M_NFSDEVINFO);
5668 }
5669 
5670 /*
5671  * Mark any layouts that match as recalled.
5672  */
5673 static int
nfscl_layoutrecall(int recalltype,struct nfscllayout * lyp,uint32_t iomode,uint64_t off,uint64_t len,uint32_t stateseqid,uint32_t stat,uint32_t op,char * devid,struct nfsclrecalllayout * recallp)5674 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5675     uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op,
5676     char *devid, struct nfsclrecalllayout *recallp)
5677 {
5678 	struct nfsclrecalllayout *rp, *orp;
5679 
5680 	recallp->nfsrecly_recalltype = recalltype;
5681 	recallp->nfsrecly_iomode = iomode;
5682 	recallp->nfsrecly_stateseqid = stateseqid;
5683 	recallp->nfsrecly_off = off;
5684 	recallp->nfsrecly_len = len;
5685 	recallp->nfsrecly_stat = stat;
5686 	recallp->nfsrecly_op = op;
5687 	if (devid != NULL)
5688 		NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID);
5689 	/*
5690 	 * Order the list as file returns first, followed by fsid and any
5691 	 * returns, both in increasing stateseqid order.
5692 	 * Note that the seqids wrap around, so 1 is after 0xffffffff.
5693 	 * (I'm not sure this is correct because I find RFC5661 confusing
5694 	 *  on this, but hopefully it will work ok.)
5695 	 */
5696 	orp = NULL;
5697 	LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5698 		orp = rp;
5699 		if ((recalltype == NFSLAYOUTRETURN_FILE &&
5700 		     (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5701 		      nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5702 		    (recalltype != NFSLAYOUTRETURN_FILE &&
5703 		     rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5704 		     nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5705 			LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5706 			break;
5707 		}
5708 
5709 		/*
5710 		 * Put any error return on all the file returns that will
5711 		 * preceed this one.
5712 		 */
5713 		if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE &&
5714 		   stat != 0 && rp->nfsrecly_stat == 0) {
5715 			rp->nfsrecly_stat = stat;
5716 			rp->nfsrecly_op = op;
5717 			if (devid != NULL)
5718 				NFSBCOPY(devid, rp->nfsrecly_devid,
5719 				    NFSX_V4DEVICEID);
5720 		}
5721 	}
5722 	if (rp == NULL) {
5723 		if (orp == NULL)
5724 			LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5725 			    nfsrecly_list);
5726 		else
5727 			LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5728 	}
5729 	lyp->nfsly_flags |= NFSLY_RECALL;
5730 	wakeup(lyp->nfsly_clp);
5731 	return (0);
5732 }
5733 
5734 /*
5735  * Compare the two seqids for ordering. The trick is that the seqids can
5736  * wrap around from 0xffffffff->0, so check for the cases where one
5737  * has wrapped around.
5738  * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5739  */
5740 static int
nfscl_seq(uint32_t seqid1,uint32_t seqid2)5741 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5742 {
5743 
5744 	if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5745 		/* seqid2 has wrapped around. */
5746 		return (0);
5747 	if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5748 		/* seqid1 has wrapped around. */
5749 		return (1);
5750 	if (seqid1 <= seqid2)
5751 		return (1);
5752 	return (0);
5753 }
5754 
5755 /*
5756  * Do a layout return for each of the recalls.
5757  */
5758 static void
nfscl_layoutreturn(struct nfsmount * nmp,struct nfscllayout * lyp,struct ucred * cred,NFSPROC_T * p)5759 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5760     struct ucred *cred, NFSPROC_T *p)
5761 {
5762 	struct nfsclrecalllayout *rp;
5763 	nfsv4stateid_t stateid;
5764 	int layouttype;
5765 
5766 	NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5767 	stateid.seqid = lyp->nfsly_stateid.seqid;
5768 	if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5769 		layouttype = NFSLAYOUT_NFSV4_1_FILES;
5770 	else
5771 		layouttype = NFSLAYOUT_FLEXFILE;
5772 	LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5773 		(void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5774 		    lyp->nfsly_fhlen, 0, layouttype,
5775 		    rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5776 		    rp->nfsrecly_off, rp->nfsrecly_len,
5777 		    &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op,
5778 		    rp->nfsrecly_devid);
5779 	}
5780 }
5781 
5782 /*
5783  * Do the layout commit for a file layout.
5784  */
5785 static void
nfscl_dolayoutcommit(struct nfsmount * nmp,struct nfscllayout * lyp,struct ucred * cred,NFSPROC_T * p)5786 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5787     struct ucred *cred, NFSPROC_T *p)
5788 {
5789 	struct nfsclflayout *flp;
5790 	uint64_t len;
5791 	int error, layouttype;
5792 
5793 	if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5794 		layouttype = NFSLAYOUT_NFSV4_1_FILES;
5795 	else
5796 		layouttype = NFSLAYOUT_FLEXFILE;
5797 	LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5798 		if (layouttype == NFSLAYOUT_FLEXFILE &&
5799 		    (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
5800 			NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
5801 			/* If not supported, don't bother doing it. */
5802 			NFSLOCKMNT(nmp);
5803 			nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5804 			NFSUNLOCKMNT(nmp);
5805 			break;
5806 		} else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5807 			len = flp->nfsfl_end - flp->nfsfl_off;
5808 			error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5809 			    lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5810 			    lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5811 			    layouttype, cred, p, NULL);
5812 			NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5813 			if (error == NFSERR_NOTSUPP) {
5814 				/* If not supported, don't bother doing it. */
5815 				NFSLOCKMNT(nmp);
5816 				nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5817 				NFSUNLOCKMNT(nmp);
5818 				break;
5819 			}
5820 		}
5821 	}
5822 }
5823 
5824 /*
5825  * Commit all layouts for a file (vnode).
5826  */
5827 int
nfscl_layoutcommit(vnode_t vp,NFSPROC_T * p)5828 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5829 {
5830 	struct nfsclclient *clp;
5831 	struct nfscllayout *lyp;
5832 	struct nfsnode *np = VTONFS(vp);
5833 	mount_t mp;
5834 	struct nfsmount *nmp;
5835 
5836 	mp = vp->v_mount;
5837 	nmp = VFSTONFS(mp);
5838 	if (NFSHASNOLAYOUTCOMMIT(nmp))
5839 		return (0);
5840 	NFSLOCKCLSTATE();
5841 	clp = nmp->nm_clp;
5842 	if (clp == NULL) {
5843 		NFSUNLOCKCLSTATE();
5844 		return (EPERM);
5845 	}
5846 	lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5847 	if (lyp == NULL) {
5848 		NFSUNLOCKCLSTATE();
5849 		return (EPERM);
5850 	}
5851 	nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5852 	if (NFSCL_FORCEDISM(mp)) {
5853 		NFSUNLOCKCLSTATE();
5854 		return (EPERM);
5855 	}
5856 tryagain:
5857 	if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5858 		lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5859 		NFSUNLOCKCLSTATE();
5860 		NFSCL_DEBUG(4, "do layoutcommit2\n");
5861 		nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5862 		NFSLOCKCLSTATE();
5863 		goto tryagain;
5864 	}
5865 	nfsv4_relref(&lyp->nfsly_lock);
5866 	NFSUNLOCKCLSTATE();
5867 	return (0);
5868 }
5869