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