2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Rick Macklem, University of Guelph
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * These functions implement the client side state handling for NFSv4.
35 * NFSv4 state handling:
36 * - A lockowner is used to determine lock contention, so it
37 * corresponds directly to a Posix pid. (1 to 1 mapping)
38 * - The correct granularity of an OpenOwner is not nearly so
39 * obvious. An OpenOwner does the following:
40 * - provides a serial sequencing of Open/Close/Lock-with-new-lockowner
41 * - is used to check for Open/Share contention (not applicable to
42 * this client, since all Opens are Deny_None)
43 * As such, I considered both extreme.
44 * 1 OpenOwner per ClientID - Simple to manage, but fully serializes
45 * all Open, Close and Lock (with a new lockowner) Ops.
46 * 1 OpenOwner for each Open - This one results in an OpenConfirm for
47 * every Open, for most servers.
48 * So, I chose to use the same mapping as I did for LockOwnwers.
49 * The main concern here is that you can end up with multiple Opens
50 * for the same File Handle, but on different OpenOwners (opens
51 * inherited from parents, grandparents...) and you do not know
52 * which of these the vnodeop close applies to. This is handled by
53 * delaying the Close Op(s) until all of the Opens have been closed.
54 * (It is not yet obvious if this is the correct granularity.)
55 * - How the code handles serialization:
56 * - For the ClientId, it uses an exclusive lock while getting its
57 * SetClientId and during recovery. Otherwise, it uses a shared
58 * lock via a reference count.
59 * - For the rest of the data structures, it uses an SMP mutex
60 * (once the nfs client is SMP safe) and doesn't sleep while
61 * manipulating the linked lists.
62 * - The serialization of Open/Close/Lock/LockU falls out in the
63 * "wash", since OpenOwners and LockOwners are both mapped from
64 * Posix pid. In other words, there is only one Posix pid using
65 * any given owner, so that owner is serialized. (If you change
66 * the granularity of the OpenOwner, then code must be added to
67 * serialize Ops on the OpenOwner.)
68 * - When to get rid of OpenOwners and LockOwners.
69 * - The function nfscl_cleanup_common() is executed after a process exits.
70 * It goes through the client list looking for all Open and Lock Owners.
71 * When one is found, it is marked "defunct" or in the case of
72 * an OpenOwner without any Opens, freed.
73 * The renew thread scans for defunct Owners and gets rid of them,
74 * if it can. The LockOwners will also be deleted when the
75 * associated Open is closed.
76 * - If the LockU or Close Op(s) fail during close in a way
77 * that could be recovered upon retry, they are relinked to the
78 * ClientId's defunct open list and retried by the renew thread
79 * until they succeed or an unmount/recovery occurs.
80 * (Since we are done with them, they do not need to be recovered.)
83 #include <fs/nfs/nfsport.h>
88 extern struct nfsstatsv1 nfsstatsv1;
89 extern struct nfsreqhead nfsd_reqq;
90 extern u_int32_t newnfs_false, newnfs_true;
91 extern int nfscl_debuglevel;
92 extern int nfscl_enablecallb;
93 extern int nfs_numnfscbd;
97 struct nfsclhead nfsclhead; /* Head of clientid list */
98 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
99 int nfscl_layouthighwater = NFSCLLAYOUTHIGHWATER;
101 static int nfscl_delegcnt = 0;
102 static int nfscl_layoutcnt = 0;
103 static int nfscl_getopen(struct nfsclownerhead *, u_int8_t *, int, u_int8_t *,
104 u_int8_t *, u_int32_t, struct nfscllockowner **, struct nfsclopen **);
105 static void nfscl_clrelease(struct nfsclclient *);
106 static void nfscl_cleanclient(struct nfsclclient *);
107 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
108 struct ucred *, NFSPROC_T *);
109 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
110 struct nfsmount *, struct ucred *, NFSPROC_T *);
111 static void nfscl_recover(struct nfsclclient *, bool *, struct ucred *,
113 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
114 struct nfscllock *, int);
115 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
116 struct nfscllock **, int);
117 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *);
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 *,
124 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
125 int, struct nfsclrecalllayout **);
126 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
127 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
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 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
154 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
155 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
156 struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int);
157 static void nfscl_freeopenowner(struct nfsclowner *, int);
158 static void nfscl_cleandeleg(struct nfscldeleg *);
159 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
160 struct nfsmount *, NFSPROC_T *);
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 *);
173 static short nfscberr_null[] = {
178 static short nfscberr_getattr[] = {
187 static short nfscberr_recall[] = {
197 static short *nfscl_cberrmap[] = {
205 #define NETFAMILY(clp) \
206 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
209 * Called for an open operation.
210 * If the nfhp argument is NULL, just get an openowner.
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)
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];
233 * Might need one or both of these, so MALLOC them now, to
234 * avoid a tsleep() in MALLOC later.
236 nowp = malloc(sizeof (struct nfsclowner),
237 M_NFSCLOWNER, M_WAITOK);
239 nop = malloc(sizeof (struct nfsclopen) +
240 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
241 ret = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
243 free(nowp, M_NFSCLOWNER);
245 free(nop, M_NFSCLOPEN);
250 * Get the Open iff it already exists.
251 * If none found, add the new one or return error, depending upon
256 /* First check the delegation list */
257 if (nfhp != NULL && usedeleg) {
258 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
259 if (dp->nfsdl_fhlen == fhlen &&
260 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
261 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
262 (dp->nfsdl_flags & NFSCLDL_WRITE))
270 /* For NFSv4.1/4.2 and this option, use a single open_owner. */
271 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
272 nfscl_filllockowner(NULL, own, F_POSIX);
274 nfscl_filllockowner(p->td_proc, own, F_POSIX);
276 ohp = &dp->nfsdl_owner;
278 ohp = &clp->nfsc_owner;
279 /* Now, search for an openowner */
280 LIST_FOREACH(owp, ohp, nfsow_list) {
281 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
286 * Create a new open, as required.
288 nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
292 * Now, check the mode on the open and return the appropriate
296 if (nfhp != NULL && dp != NULL && nop == NULL)
297 /* new local open on delegation */
298 *retp = NFSCLOPEN_SETCRED;
300 *retp = NFSCLOPEN_OK;
302 if (op != NULL && (amode & ~(op->nfso_mode))) {
303 op->nfso_mode |= amode;
304 if (retp != NULL && dp == NULL)
305 *retp = NFSCLOPEN_DOOPEN;
309 * Serialize modifications to the open owner for multiple threads
310 * within the same process using a read/write sleep lock.
311 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
312 * by acquiring a shared lock. The close operations still use an
313 * exclusive lock for this case.
316 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) {
318 * Get a shared lock on the OpenOwner, but first
319 * wait for any pending exclusive lock, so that the
320 * exclusive locker gets priority.
322 nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
323 NFSCLSTATEMUTEXPTR, NULL);
324 nfsv4_getref(&owp->nfsow_rwlock, NULL,
325 NFSCLSTATEMUTEXPTR, NULL);
327 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
331 free(nowp, M_NFSCLOWNER);
333 free(nop, M_NFSCLOPEN);
342 * Create a new open, as required.
345 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
346 struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
347 struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
348 struct ucred *cred, int *newonep)
350 struct nfsclowner *owp = *owpp, *nowp;
351 struct nfsclopen *op, *nop;
361 if (owp == NULL && nowp != NULL) {
362 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
363 LIST_INIT(&nowp->nfsow_open);
364 nowp->nfsow_clp = clp;
365 nowp->nfsow_seqid = 0;
366 nowp->nfsow_defunct = 0;
367 nfscl_lockinit(&nowp->nfsow_rwlock);
369 nfsstatsv1.cllocalopenowners++;
370 LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
372 nfsstatsv1.clopenowners++;
373 LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
381 /* If an fhp has been specified, create an Open as well. */
383 /* and look for the correct open, based upon FH */
384 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
385 if (op->nfso_fhlen == fhlen &&
386 !NFSBCMP(op->nfso_fh, fhp, fhlen))
389 if (op == NULL && nop != NULL) {
392 nop->nfso_opencnt = 0;
393 nop->nfso_posixlock = 1;
394 nop->nfso_fhlen = fhlen;
395 NFSBCOPY(fhp, nop->nfso_fh, fhlen);
396 LIST_INIT(&nop->nfso_lock);
397 nop->nfso_stateid.seqid = 0;
398 nop->nfso_stateid.other[0] = 0;
399 nop->nfso_stateid.other[1] = 0;
400 nop->nfso_stateid.other[2] = 0;
401 KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
402 newnfs_copyincred(cred, &nop->nfso_cred);
404 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
405 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
407 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
408 nfsstatsv1.cllocalopens++;
410 nfsstatsv1.clopens++;
412 LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
424 * Called to find/add a delegation to a client.
427 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
428 int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
430 struct nfscldeleg *dp = *dpp, *tdp;
433 * First, if we have received a Read delegation for a file on a
434 * read/write file system, just return it, because they aren't
437 if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
438 (dp->nfsdl_flags & NFSCLDL_READ)) {
439 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
440 free(dp, M_NFSCLDELEG);
445 /* Look for the correct deleg, based upon FH */
447 tdp = nfscl_finddeleg(clp, nfhp, fhlen);
451 return (NFSERR_BADSTATEID);
454 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
455 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
457 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
458 nfsstatsv1.cldelegates++;
462 * Delegation already exists, what do we do if a new one??
465 printf("Deleg already exists!\n");
466 free(dp, M_NFSCLDELEG);
477 * Find a delegation for this file handle. Return NULL upon failure.
479 static struct nfscldeleg *
480 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
482 struct nfscldeleg *dp;
484 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
485 if (dp->nfsdl_fhlen == fhlen &&
486 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
493 * Get a stateid for an I/O operation. First, look for an open and iff
494 * found, return either a lockowner stateid or the open stateid.
495 * If no Open is found, just return error and the special stateid of all zeros.
498 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
499 int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
502 struct nfsclclient *clp;
503 struct nfsclowner *owp;
504 struct nfsclopen *op = NULL, *top;
505 struct nfscllockowner *lp;
506 struct nfscldeleg *dp;
508 struct nfsmount *nmp;
509 u_int8_t own[NFSV4CL_LOCKNAMELEN];
515 * Initially, just set the special stateid of all zeros.
516 * (Don't do this for a DS, since the special stateid can't be used.)
520 stateidp->other[0] = 0;
521 stateidp->other[1] = 0;
522 stateidp->other[2] = 0;
524 if (vnode_vtype(vp) != VREG)
527 nmp = VFSTONFS(vp->v_mount);
529 clp = nfscl_findcl(nmp);
536 * Wait for recovery to complete.
538 while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
539 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
540 PZERO, "nfsrecvr", NULL);
543 * First, look for a delegation.
545 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
546 if (dp->nfsdl_fhlen == fhlen &&
547 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
548 if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
549 (dp->nfsdl_flags & NFSCLDL_WRITE)) {
550 stateidp->seqid = dp->nfsdl_stateid.seqid;
551 stateidp->other[0] = dp->nfsdl_stateid.other[0];
552 stateidp->other[1] = dp->nfsdl_stateid.other[1];
553 stateidp->other[2] = dp->nfsdl_stateid.other[2];
554 if (!(np->n_flag & NDELEGRECALL)) {
555 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
557 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
559 dp->nfsdl_timestamp = NFSD_MONOSEC +
561 dp->nfsdl_rwlock.nfslock_usecnt++;
562 *lckpp = (void *)&dp->nfsdl_rwlock;
573 * If p != NULL, we want to search the parentage tree
574 * for a matching OpenOwner and use that.
576 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
577 nfscl_filllockowner(NULL, own, F_POSIX);
579 nfscl_filllockowner(p->td_proc, own, F_POSIX);
581 error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, own, own,
583 if (error == 0 && lp != NULL && fords == 0) {
584 /* Don't return a lock stateid for a DS. */
586 lp->nfsl_stateid.seqid;
588 lp->nfsl_stateid.other[0];
590 lp->nfsl_stateid.other[1];
592 lp->nfsl_stateid.other[2];
598 /* If not found, just look for any OpenOwner that will work. */
601 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
602 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
603 if (op->nfso_fhlen == fhlen &&
604 !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
605 if (top == NULL && (op->nfso_mode &
606 NFSV4OPEN_ACCESSWRITE) != 0 &&
607 (mode & NFSV4OPEN_ACCESSREAD) != 0)
609 if ((mode & op->nfso_mode) == mode) {
619 NFSCL_DEBUG(2, "openmode top=%p\n", top);
620 if (top == NULL || NFSHASOPENMODE(nmp)) {
627 * For read aheads or write behinds, use the open cred.
628 * A read ahead or write behind is indicated by p == NULL.
631 newnfs_copycred(&op->nfso_cred, cred);
635 * No lock stateid, so return the open stateid.
637 stateidp->seqid = op->nfso_stateid.seqid;
638 stateidp->other[0] = op->nfso_stateid.other[0];
639 stateidp->other[1] = op->nfso_stateid.other[1];
640 stateidp->other[2] = op->nfso_stateid.other[2];
646 * Search for a matching file, mode and, optionally, lockowner.
649 nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
650 u_int8_t *openown, u_int8_t *lockown, u_int32_t mode,
651 struct nfscllockowner **lpp, struct nfsclopen **opp)
653 struct nfsclowner *owp;
654 struct nfsclopen *op, *rop, *rop2;
655 struct nfscllockowner *lp;
661 * rop will be set to the open to be returned. There are three
662 * variants of this, all for an open of the correct file:
663 * 1 - A match of lockown.
664 * 2 - A match of the openown, when no lockown match exists.
665 * 3 - A match for any open, if no openown or lockown match exists.
666 * Looking for #2 over #3 probably isn't necessary, but since
667 * RFC3530 is vague w.r.t. the relationship between openowners and
668 * lockowners, I think this is the safer way to go.
673 /* Search the client list */
674 LIST_FOREACH(owp, ohp, nfsow_list) {
675 /* and look for the correct open */
676 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
677 if (op->nfso_fhlen == fhlen &&
678 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
679 && (op->nfso_mode & mode) == mode) {
681 /* Now look for a matching lockowner. */
682 LIST_FOREACH(lp, &op->nfso_lock,
684 if (!NFSBCMP(lp->nfsl_owner,
686 NFSV4CL_LOCKNAMELEN)) {
689 keep_looping = false;
694 if (rop == NULL && !NFSBCMP(owp->nfsow_owner,
695 openown, NFSV4CL_LOCKNAMELEN)) {
698 keep_looping = false;
718 * Release use of an open owner. Called when open operations are done
719 * with the open owner.
722 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
723 __unused int error, __unused int candelete, int unlocked)
730 if (NFSHASONEOPENOWN(nmp))
731 nfsv4_relref(&owp->nfsow_rwlock);
733 nfscl_lockunlock(&owp->nfsow_rwlock);
735 nfscl_clrelease(owp->nfsow_clp);
740 * Release use of an open structure under an open owner.
743 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
746 struct nfsclclient *clp;
747 struct nfsclowner *owp;
753 if (NFSHASONEOPENOWN(nmp))
754 nfsv4_relref(&owp->nfsow_rwlock);
756 nfscl_lockunlock(&owp->nfsow_rwlock);
757 clp = owp->nfsow_clp;
758 if (error && candelete && op->nfso_opencnt == 0)
759 nfscl_freeopen(op, 0);
760 nfscl_clrelease(clp);
765 * Called to get a clientid structure. It will optionally lock the
766 * client data structures to do the SetClientId/SetClientId_confirm,
767 * but will release that lock and return the clientid with a reference
769 * If the "cred" argument is NULL, a new clientid should not be created.
770 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
772 * The start_renewthread argument tells nfscl_getcl() to start a renew
773 * thread if this creates a new clp.
774 * It always clpp with a reference count on it, unless returning an error.
777 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
778 int start_renewthread, struct nfsclclient **clpp)
780 struct nfsclclient *clp;
781 struct nfsclclient *newclp = NULL;
782 struct nfsmount *nmp;
783 char uuid[HOSTUUIDLEN];
784 int igotlock = 0, error, trystalecnt, clidinusedelay, i;
789 getcredhostuuid(cred, uuid, sizeof uuid);
790 idlen = strlen(uuid);
792 idlen += sizeof (u_int64_t);
794 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
796 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
801 * If a forced dismount is already in progress, don't
802 * allocate a new clientid and get out now. For the case where
803 * clp != NULL, this is a harmless optimization.
805 if (NFSCL_FORCEDISM(mp)) {
808 free(newclp, M_NFSCLCLIENT);
813 if (newclp == NULL) {
818 clp->nfsc_idlen = idlen;
819 LIST_INIT(&clp->nfsc_owner);
820 TAILQ_INIT(&clp->nfsc_deleg);
821 TAILQ_INIT(&clp->nfsc_layout);
822 LIST_INIT(&clp->nfsc_devinfo);
823 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
824 LIST_INIT(&clp->nfsc_deleghash[i]);
825 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
826 LIST_INIT(&clp->nfsc_layouthash[i]);
827 clp->nfsc_flags = NFSCLFLAGS_INITED;
828 clp->nfsc_clientidrev = 1;
829 clp->nfsc_cbident = nfscl_nextcbident();
830 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
832 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
836 if (start_renewthread != 0)
837 nfscl_start_renewthread(clp);
841 free(newclp, M_NFSCLCLIENT);
844 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
845 !NFSCL_FORCEDISM(mp))
846 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
847 NFSCLSTATEMUTEXPTR, mp);
850 * Call nfsv4_lock() with "iwantlock == 0" so that it will
851 * wait for a pending exclusive lock request. This gives the
852 * exclusive lock request priority over this shared lock
854 * An exclusive lock on nfsc_lock is used mainly for server
857 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, mp);
858 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
860 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
862 * Both nfsv4_lock() and nfsv4_getref() know to check
863 * for NFSCL_FORCEDISM() and return without sleeping to
864 * wait for the exclusive lock to be released, since it
865 * might be held by nfscl_umount() and we need to get out
866 * now for that case and not wait until nfscl_umount()
875 * If it needs a clientid, do the setclientid now.
877 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
879 panic("nfscl_clget");
880 if (p == NULL || cred == NULL) {
882 nfsv4_unlock(&clp->nfsc_lock, 0);
887 * If RFC3530 Sec. 14.2.33 is taken literally,
888 * NFSERR_CLIDINUSE will be returned persistently for the
889 * case where a new mount of the same file system is using
890 * a different principal. In practice, NFSERR_CLIDINUSE is
891 * only returned when there is outstanding unexpired state
892 * on the clientid. As such, try for twice the lease
893 * interval, if we know what that is. Otherwise, make a
895 * The case of returning NFSERR_STALECLIENTID is far less
896 * likely, but might occur if there is a significant delay
897 * between doing the SetClientID and SetClientIDConfirm Ops,
898 * such that the server throws away the clientid before
899 * receiving the SetClientIDConfirm.
901 if (clp->nfsc_renew > 0)
902 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
904 clidinusedelay = 120;
907 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
908 if (error == NFSERR_STALECLIENTID ||
909 error == NFSERR_STALEDONTRECOVER ||
910 error == NFSERR_BADSESSION ||
911 error == NFSERR_CLIDINUSE) {
912 (void) nfs_catnap(PZERO, error, "nfs_setcl");
914 } while (((error == NFSERR_STALECLIENTID ||
915 error == NFSERR_BADSESSION ||
916 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
917 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
920 nfsv4_unlock(&clp->nfsc_lock, 0);
924 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
928 nfsv4_unlock(&clp->nfsc_lock, 1);
937 * Get a reference to a clientid and return it, if valid.
940 nfscl_findcl(struct nfsmount *nmp)
942 struct nfsclclient *clp;
945 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
951 * Release the clientid structure. It may be locked or reference counted.
954 nfscl_clrelease(struct nfsclclient *clp)
957 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
958 nfsv4_unlock(&clp->nfsc_lock, 0);
960 nfsv4_relref(&clp->nfsc_lock);
964 * External call for nfscl_clrelease.
967 nfscl_clientrelease(struct nfsclclient *clp)
971 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
972 nfsv4_unlock(&clp->nfsc_lock, 0);
974 nfsv4_relref(&clp->nfsc_lock);
979 * Called when wanting to lock a byte region.
982 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
983 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
984 int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
985 struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
987 struct nfscllockowner *lp;
988 struct nfsclopen *op;
989 struct nfsclclient *clp;
990 struct nfscllockowner *nlp;
991 struct nfscllock *nlop, *otherlop;
992 struct nfscldeleg *dp = NULL, *ldp = NULL;
993 struct nfscllockownerhead *lhp = NULL;
995 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
997 int error = 0, ret, donelocally = 0;
1000 /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
1009 * Might need these, so MALLOC them now, to
1010 * avoid a tsleep() in MALLOC later.
1013 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
1015 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1017 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1018 nlop->nfslo_type = type;
1019 nlop->nfslo_first = off;
1020 if (len == NFS64BITSSET) {
1021 nlop->nfslo_end = NFS64BITSSET;
1023 nlop->nfslo_end = off + len;
1024 if (nlop->nfslo_end <= nlop->nfslo_first)
1025 error = NFSERR_INVAL;
1032 error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
1035 free(nlp, M_NFSCLLOCKOWNER);
1036 free(otherlop, M_NFSCLLOCK);
1037 free(nlop, M_NFSCLLOCK);
1044 openownp = ropenownp;
1046 nfscl_filllockowner(id, own, flags);
1048 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
1049 nfscl_filllockowner(NULL, openown, F_POSIX);
1051 nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1057 * First, search for a delegation. If one exists for this file,
1058 * the lock can be done locally against it, so long as there
1059 * isn't a local lock conflict.
1061 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1062 np->n_fhp->nfh_len);
1063 /* Just sanity check for correct type of delegation */
1064 if (dp != NULL && ((dp->nfsdl_flags &
1065 (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1067 (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1071 /* Now, find an open and maybe a lockowner. */
1072 ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
1073 np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1075 ret = nfscl_getopen(&clp->nfsc_owner,
1076 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1077 ownp, mode, NULL, &op);
1079 lhp = &dp->nfsdl_lock;
1080 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1081 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1082 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1090 * Get the related Open and maybe lockowner.
1092 error = nfscl_getopen(&clp->nfsc_owner,
1093 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1094 ownp, mode, &lp, &op);
1096 lhp = &op->nfso_lock;
1098 if (!error && !recovery)
1099 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1100 np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1103 nfscl_clrelease(clp);
1106 free(nlp, M_NFSCLLOCKOWNER);
1107 free(otherlop, M_NFSCLLOCK);
1108 free(nlop, M_NFSCLLOCK);
1113 * Ok, see if a lockowner exists and create one, as required.
1116 LIST_FOREACH(lp, lhp, nfsl_list) {
1117 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1121 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1123 NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1124 NFSV4CL_LOCKNAMELEN);
1126 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1127 NFSV4CL_LOCKNAMELEN);
1128 nlp->nfsl_seqid = 0;
1129 nlp->nfsl_lockflags = flags;
1130 nlp->nfsl_inprog = NULL;
1131 nfscl_lockinit(&nlp->nfsl_rwlock);
1132 LIST_INIT(&nlp->nfsl_lock);
1134 nlp->nfsl_open = NULL;
1135 nfsstatsv1.cllocallockowners++;
1137 nlp->nfsl_open = op;
1138 nfsstatsv1.cllockowners++;
1140 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1147 * Now, update the byte ranges for locks.
1149 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1155 nfscl_clrelease(clp);
1158 * Serial modifications on the lock owner for multiple threads
1159 * for the same process using a read/write lock.
1162 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1168 free(nlp, M_NFSCLLOCKOWNER);
1170 free(nlop, M_NFSCLLOCK);
1172 free(otherlop, M_NFSCLLOCK);
1179 * Called to unlock a byte range, for LockU.
1182 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1183 __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1184 struct nfsclclient *clp, void *id, int flags,
1185 struct nfscllockowner **lpp, int *dorpcp)
1187 struct nfscllockowner *lp;
1188 struct nfsclowner *owp;
1189 struct nfsclopen *op;
1190 struct nfscllock *nlop, *other_lop = NULL;
1191 struct nfscldeleg *dp;
1193 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1201 * Might need these, so MALLOC them now, to
1202 * avoid a tsleep() in MALLOC later.
1205 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1206 nlop->nfslo_type = F_UNLCK;
1207 nlop->nfslo_first = off;
1208 if (len == NFS64BITSSET) {
1209 nlop->nfslo_end = NFS64BITSSET;
1211 nlop->nfslo_end = off + len;
1212 if (nlop->nfslo_end <= nlop->nfslo_first) {
1213 free(nlop, M_NFSCLLOCK);
1214 return (NFSERR_INVAL);
1219 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1222 nfscl_filllockowner(id, own, flags);
1226 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1227 np->n_fhp->nfh_len);
1230 * First, unlock any local regions on a delegation.
1233 /* Look for this lockowner. */
1234 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1235 if (!NFSBCMP(lp->nfsl_owner, own,
1236 NFSV4CL_LOCKNAMELEN))
1240 /* Use other_lop, so nlop is still available */
1241 (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1245 * Now, find a matching open/lockowner that hasn't already been done,
1246 * as marked by nfsl_inprog.
1250 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1251 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1252 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1253 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1254 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1255 if (lp->nfsl_inprog == NULL &&
1256 !NFSBCMP(lp->nfsl_owner, own,
1257 NFSV4CL_LOCKNAMELEN)) {
1271 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1275 * Serial modifications on the lock owner for multiple
1276 * threads for the same process using a read/write lock.
1278 lp->nfsl_inprog = p;
1279 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1284 free(nlop, M_NFSCLLOCK);
1286 free(other_lop, M_NFSCLLOCK);
1291 * Release all lockowners marked in progess for this process and file.
1294 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1295 void *id, int flags)
1297 struct nfsclowner *owp;
1298 struct nfsclopen *op;
1299 struct nfscllockowner *lp;
1301 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1304 nfscl_filllockowner(id, own, flags);
1306 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1307 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1308 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1309 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1310 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1311 if (lp->nfsl_inprog == p &&
1312 !NFSBCMP(lp->nfsl_owner, own,
1313 NFSV4CL_LOCKNAMELEN)) {
1314 lp->nfsl_inprog = NULL;
1315 nfscl_lockunlock(&lp->nfsl_rwlock);
1321 nfscl_clrelease(clp);
1326 * Called to find out if any bytes within the byte range specified are
1327 * write locked by the calling process. Used to determine if flushing
1328 * is required before a LockU.
1329 * If in doubt, return 1, so the flush will occur.
1332 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1333 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1335 struct nfsclowner *owp;
1336 struct nfscllockowner *lp;
1337 struct nfsclopen *op;
1338 struct nfsclclient *clp;
1339 struct nfscllock *lop;
1340 struct nfscldeleg *dp;
1343 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1347 switch (fl->l_whence) {
1351 * Caller is responsible for adding any necessary offset
1352 * when SEEK_CUR is used.
1357 off = np->n_size + fl->l_start;
1362 if (fl->l_len != 0) {
1363 end = off + fl->l_len;
1370 error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
1373 nfscl_filllockowner(id, own, flags);
1377 * First check the delegation locks.
1379 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1381 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1382 if (!NFSBCMP(lp->nfsl_owner, own,
1383 NFSV4CL_LOCKNAMELEN))
1387 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1388 if (lop->nfslo_first >= end)
1390 if (lop->nfslo_end <= off)
1392 if (lop->nfslo_type == F_WRLCK) {
1393 nfscl_clrelease(clp);
1402 * Now, check state against the server.
1404 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1405 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1406 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1407 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1408 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1409 if (!NFSBCMP(lp->nfsl_owner, own,
1410 NFSV4CL_LOCKNAMELEN))
1414 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1415 if (lop->nfslo_first >= end)
1417 if (lop->nfslo_end <= off)
1419 if (lop->nfslo_type == F_WRLCK) {
1420 nfscl_clrelease(clp);
1429 nfscl_clrelease(clp);
1435 * Release a byte range lock owner structure.
1438 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1440 struct nfsclclient *clp;
1445 clp = lp->nfsl_open->nfso_own->nfsow_clp;
1446 if (error != 0 && candelete &&
1447 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1448 nfscl_freelockowner(lp, 0);
1450 nfscl_lockunlock(&lp->nfsl_rwlock);
1451 nfscl_clrelease(clp);
1456 * Free up an open structure and any associated byte range lock structures.
1459 nfscl_freeopen(struct nfsclopen *op, int local)
1462 LIST_REMOVE(op, nfso_list);
1463 nfscl_freealllocks(&op->nfso_lock, local);
1464 free(op, M_NFSCLOPEN);
1466 nfsstatsv1.cllocalopens--;
1468 nfsstatsv1.clopens--;
1472 * Free up all lock owners and associated locks.
1475 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1477 struct nfscllockowner *lp, *nlp;
1479 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1480 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1482 nfscl_freelockowner(lp, local);
1487 * Called for an Open when NFSERR_EXPIRED is received from the server.
1488 * If there are no byte range locks nor a Share Deny lost, try to do a
1489 * fresh Open. Otherwise, free the open.
1492 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1493 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1495 struct nfscllockowner *lp;
1496 struct nfscldeleg *dp;
1497 int mustdelete = 0, error;
1500 * Look for any byte range lock(s).
1502 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1503 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1510 * If no byte range lock(s) nor a Share deny, try to re-open.
1512 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1513 newnfs_copycred(&op->nfso_cred, cred);
1515 error = nfsrpc_reopen(nmp, op->nfso_fh,
1516 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1520 free(dp, M_NFSCLDELEG);
1525 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1526 op->nfso_fhlen, cred, p, &dp);
1530 * If a byte range lock or Share deny or couldn't re-open, free it.
1533 nfscl_freeopen(op, 0);
1534 return (mustdelete);
1538 * Free up an open owner structure.
1541 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1544 LIST_REMOVE(owp, nfsow_list);
1545 free(owp, M_NFSCLOWNER);
1547 nfsstatsv1.cllocalopenowners--;
1549 nfsstatsv1.clopenowners--;
1553 * Free up a byte range lock owner structure.
1556 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1558 struct nfscllock *lop, *nlop;
1560 LIST_REMOVE(lp, nfsl_list);
1561 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1562 nfscl_freelock(lop, local);
1564 free(lp, M_NFSCLLOCKOWNER);
1566 nfsstatsv1.cllocallockowners--;
1568 nfsstatsv1.cllockowners--;
1572 * Free up a byte range lock structure.
1575 nfscl_freelock(struct nfscllock *lop, int local)
1578 LIST_REMOVE(lop, nfslo_list);
1579 free(lop, M_NFSCLLOCK);
1581 nfsstatsv1.cllocallocks--;
1583 nfsstatsv1.cllocks--;
1587 * Clean out the state related to a delegation.
1590 nfscl_cleandeleg(struct nfscldeleg *dp)
1592 struct nfsclowner *owp, *nowp;
1593 struct nfsclopen *op;
1595 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1596 op = LIST_FIRST(&owp->nfsow_open);
1598 if (LIST_NEXT(op, nfso_list) != NULL)
1599 panic("nfscleandel");
1600 nfscl_freeopen(op, 1);
1602 nfscl_freeopenowner(owp, 1);
1604 nfscl_freealllocks(&dp->nfsdl_lock, 1);
1608 * Free a delegation.
1611 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp)
1614 TAILQ_REMOVE(hdp, dp, nfsdl_list);
1615 LIST_REMOVE(dp, nfsdl_hash);
1616 free(dp, M_NFSCLDELEG);
1617 nfsstatsv1.cldelegates--;
1622 * Free up all state related to this client structure.
1625 nfscl_cleanclient(struct nfsclclient *clp)
1627 struct nfsclowner *owp, *nowp;
1628 struct nfsclopen *op, *nop;
1629 struct nfscllayout *lyp, *nlyp;
1630 struct nfscldevinfo *dip, *ndip;
1632 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1633 nfscl_freelayout(lyp);
1635 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
1636 nfscl_freedevinfo(dip);
1638 /* Now, all the OpenOwners, etc. */
1639 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1640 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1641 nfscl_freeopen(op, 0);
1643 nfscl_freeopenowner(owp, 0);
1648 * Called when an NFSERR_EXPIRED is received from the server.
1651 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1652 struct ucred *cred, NFSPROC_T *p)
1654 struct nfsclowner *owp, *nowp, *towp;
1655 struct nfsclopen *op, *nop, *top;
1656 struct nfscldeleg *dp, *ndp;
1657 int ret, printed = 0;
1660 * First, merge locally issued Opens into the list for the server.
1662 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1663 while (dp != NULL) {
1664 ndp = TAILQ_NEXT(dp, nfsdl_list);
1665 owp = LIST_FIRST(&dp->nfsdl_owner);
1666 while (owp != NULL) {
1667 nowp = LIST_NEXT(owp, nfsow_list);
1668 op = LIST_FIRST(&owp->nfsow_open);
1670 if (LIST_NEXT(op, nfso_list) != NULL)
1672 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1673 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1674 NFSV4CL_LOCKNAMELEN))
1678 /* Merge opens in */
1679 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1680 if (top->nfso_fhlen == op->nfso_fhlen &&
1681 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1683 top->nfso_mode |= op->nfso_mode;
1684 top->nfso_opencnt += op->nfso_opencnt;
1689 /* Just add the open to the owner list */
1690 LIST_REMOVE(op, nfso_list);
1691 op->nfso_own = towp;
1692 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1693 nfsstatsv1.cllocalopens--;
1694 nfsstatsv1.clopens++;
1697 /* Just add the openowner to the client list */
1698 LIST_REMOVE(owp, nfsow_list);
1699 owp->nfsow_clp = clp;
1700 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1701 nfsstatsv1.cllocalopenowners--;
1702 nfsstatsv1.clopenowners++;
1703 nfsstatsv1.cllocalopens--;
1704 nfsstatsv1.clopens++;
1709 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1711 printf("nfsv4 expired locks lost\n");
1713 nfscl_cleandeleg(dp);
1714 nfscl_freedeleg(&clp->nfsc_deleg, dp);
1717 if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1721 * Now, try and reopen against the server.
1723 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1724 owp->nfsow_seqid = 0;
1725 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1726 ret = nfscl_expireopen(clp, op, nmp, cred, p);
1727 if (ret && !printed) {
1729 printf("nfsv4 expired locks lost\n");
1732 if (LIST_EMPTY(&owp->nfsow_open))
1733 nfscl_freeopenowner(owp, 0);
1738 * This function must be called after the process represented by "own" has
1739 * exited. Must be called with CLSTATE lock held.
1742 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1744 struct nfsclowner *owp, *nowp;
1745 struct nfscllockowner *lp, *nlp;
1746 struct nfscldeleg *dp;
1748 /* First, get rid of local locks on delegations. */
1749 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1750 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1751 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1752 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1754 nfscl_freelockowner(lp, 1);
1758 owp = LIST_FIRST(&clp->nfsc_owner);
1759 while (owp != NULL) {
1760 nowp = LIST_NEXT(owp, nfsow_list);
1761 if (!NFSBCMP(owp->nfsow_owner, own,
1762 NFSV4CL_LOCKNAMELEN)) {
1764 * If there are children that haven't closed the
1765 * file descriptors yet, the opens will still be
1766 * here. For that case, let the renew thread clear
1767 * out the OpenOwner later.
1769 if (LIST_EMPTY(&owp->nfsow_open))
1770 nfscl_freeopenowner(owp, 0);
1772 owp->nfsow_defunct = 1;
1779 * Find open/lock owners for processes that have exited.
1782 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1784 struct nfsclowner *owp, *nowp;
1785 struct nfsclopen *op;
1786 struct nfscllockowner *lp, *nlp;
1787 struct nfscldeleg *dp;
1790 * All the pidhash locks must be acquired, since they are sx locks
1791 * and must be acquired before the mutexes. The pid(s) that will
1792 * be used aren't known yet, so all the locks need to be acquired.
1793 * Fortunately, this function is only performed once/sec.
1797 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1798 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1799 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1800 if (LIST_EMPTY(&lp->nfsl_lock))
1801 nfscl_emptylockowner(lp, lhp);
1804 if (nfscl_procdoesntexist(owp->nfsow_owner))
1805 nfscl_cleanup_common(clp, owp->nfsow_owner);
1809 * For the single open_owner case, these lock owners need to be
1810 * checked to see if they still exist separately.
1811 * This is because nfscl_procdoesntexist() never returns true for
1812 * the single open_owner so that the above doesn't ever call
1813 * nfscl_cleanup_common().
1815 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1816 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1817 if (nfscl_procdoesntexist(lp->nfsl_owner))
1818 nfscl_cleanup_common(clp, lp->nfsl_owner);
1822 pidhash_sunlockall();
1826 * Take the empty lock owner and move it to the local lhp list if the
1827 * associated process no longer exists.
1830 nfscl_emptylockowner(struct nfscllockowner *lp,
1831 struct nfscllockownerfhhead *lhp)
1833 struct nfscllockownerfh *lfhp, *mylfhp;
1834 struct nfscllockowner *nlp;
1837 /* If not a Posix lock owner, just return. */
1838 if ((lp->nfsl_lockflags & F_POSIX) == 0)
1844 * First, search to see if this lock owner is already in the list.
1845 * If it is, then the associated process no longer exists.
1847 SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1848 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1849 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
1852 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
1853 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
1854 NFSV4CL_LOCKNAMELEN))
1857 /* If not found, check if process still exists. */
1858 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
1861 /* Move the lock owner over to the local list. */
1862 if (mylfhp == NULL) {
1863 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
1867 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
1868 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
1869 mylfhp->nfslfh_len);
1870 LIST_INIT(&mylfhp->nfslfh_lock);
1871 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
1873 LIST_REMOVE(lp, nfsl_list);
1874 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
1877 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */
1879 * Called from nfs umount to free up the clientid.
1882 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1884 struct nfsclclient *clp;
1889 * For the case that matters, this is the thread that set
1890 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
1891 * done to ensure that any thread executing nfscl_getcl() after
1892 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
1893 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
1894 * explanation, courtesy of Alan Cox.
1895 * What follows is a snippet from Alan Cox's email at:
1896 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
1898 * 1. Set MNTK_UNMOUNTF
1899 * 2. Acquire a standard FreeBSD mutex "m".
1900 * 3. Update some data structures.
1901 * 4. Release mutex "m".
1903 * Then, other threads that acquire "m" after step 4 has occurred will
1904 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to
1905 * step 2 may or may not see MNTK_UNMOUNTF as set.
1908 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
1916 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1917 panic("nfscl umount");
1920 * First, handshake with the nfscl renew thread, to terminate
1923 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1924 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1925 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
1929 * Now, get the exclusive lock on the client state, so
1930 * that no uses of the state are still in progress.
1933 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1934 NFSCLSTATEMUTEXPTR, NULL);
1935 } while (!igotlock);
1939 * Free up all the state. It will expire on the server, but
1940 * maybe we should do a SetClientId/SetClientIdConfirm so
1941 * the server throws it away?
1943 LIST_REMOVE(clp, nfsc_list);
1944 nfscl_delegreturnall(clp, p);
1945 cred = newnfs_getcred();
1946 if (NFSHASNFSV4N(nmp)) {
1947 (void)nfsrpc_destroysession(nmp, clp, cred, p);
1948 (void)nfsrpc_destroyclient(nmp, clp, cred, p);
1950 (void)nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
1951 nfscl_cleanclient(clp);
1954 free(clp, M_NFSCLCLIENT);
1960 * This function is called when a server replies with NFSERR_STALECLIENTID
1961 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
1962 * doing Opens and Locks with reclaim. If these fail, it deletes the
1963 * corresponding state.
1966 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred,
1969 struct nfsclowner *owp, *nowp;
1970 struct nfsclopen *op, *nop;
1971 struct nfscllockowner *lp, *nlp;
1972 struct nfscllock *lop, *nlop;
1973 struct nfscldeleg *dp, *ndp, *tdp;
1974 struct nfsmount *nmp;
1975 struct ucred *tcred;
1976 struct nfsclopenhead extra_open;
1977 struct nfscldeleghead extra_deleg;
1980 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
1981 int i, igotlock = 0, error, trycnt, firstlock;
1982 struct nfscllayout *lyp, *nlyp;
1985 * First, lock the client structure, so everyone else will
1986 * block when trying to use state.
1989 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
1991 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1992 NFSCLSTATEMUTEXPTR, NULL);
1993 } while (!igotlock);
1996 nmp = clp->nfsc_nmp;
1998 panic("nfscl recover");
2001 * For now, just get rid of all layouts. There may be a need
2002 * to do LayoutCommit Ops with reclaim == true later.
2004 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
2005 nfscl_freelayout(lyp);
2006 TAILQ_INIT(&clp->nfsc_layout);
2007 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
2008 LIST_INIT(&clp->nfsc_layouthash[i]);
2013 error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p);
2014 } while ((error == NFSERR_STALECLIENTID ||
2015 error == NFSERR_BADSESSION ||
2016 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2019 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
2020 NFSCLFLAGS_RECVRINPROG);
2021 wakeup(&clp->nfsc_flags);
2022 nfsv4_unlock(&clp->nfsc_lock, 0);
2026 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2027 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2030 * Mark requests already queued on the server, so that they don't
2031 * initiate another recovery cycle. Any requests already in the
2032 * queue that handle state information will have the old stale
2033 * clientid/stateid and will get a NFSERR_STALESTATEID,
2034 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2035 * This will be translated to NFSERR_STALEDONTRECOVER when
2036 * R_DONTRECOVER is set.
2039 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2040 if (rep->r_nmp == nmp)
2041 rep->r_flags |= R_DONTRECOVER;
2046 * If nfsrpc_setclient() returns *retokp == true,
2047 * no more recovery is needed.
2053 * Now, mark all delegations "need reclaim".
2055 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2056 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2058 TAILQ_INIT(&extra_deleg);
2059 LIST_INIT(&extra_open);
2061 * Now traverse the state lists, doing Open and Lock Reclaims.
2063 tcred = newnfs_getcred();
2064 owp = LIST_FIRST(&clp->nfsc_owner);
2065 while (owp != NULL) {
2066 nowp = LIST_NEXT(owp, nfsow_list);
2067 owp->nfsow_seqid = 0;
2068 op = LIST_FIRST(&owp->nfsow_open);
2069 while (op != NULL) {
2070 nop = LIST_NEXT(op, nfso_list);
2071 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2072 /* Search for a delegation to reclaim with the open */
2073 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2074 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2076 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2077 mode = NFSV4OPEN_ACCESSWRITE;
2078 delegtype = NFSV4OPEN_DELEGATEWRITE;
2080 mode = NFSV4OPEN_ACCESSREAD;
2081 delegtype = NFSV4OPEN_DELEGATEREAD;
2083 if ((op->nfso_mode & mode) == mode &&
2084 op->nfso_fhlen == dp->nfsdl_fhlen &&
2085 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2090 delegtype = NFSV4OPEN_DELEGATENONE;
2091 newnfs_copycred(&op->nfso_cred, tcred);
2092 error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2093 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2094 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2097 /* Handle any replied delegation */
2098 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2099 || NFSMNT_RDONLY(nmp->nm_mountp))) {
2100 if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2101 mode = NFSV4OPEN_ACCESSWRITE;
2103 mode = NFSV4OPEN_ACCESSREAD;
2104 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2105 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2107 if ((op->nfso_mode & mode) == mode &&
2108 op->nfso_fhlen == dp->nfsdl_fhlen &&
2109 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2111 dp->nfsdl_stateid = ndp->nfsdl_stateid;
2112 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2113 dp->nfsdl_ace = ndp->nfsdl_ace;
2114 dp->nfsdl_change = ndp->nfsdl_change;
2115 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2116 if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2117 dp->nfsdl_flags |= NFSCLDL_RECALL;
2118 free(ndp, M_NFSCLDELEG);
2125 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2127 /* and reclaim all byte range locks */
2128 lp = LIST_FIRST(&op->nfso_lock);
2129 while (lp != NULL) {
2130 nlp = LIST_NEXT(lp, nfsl_list);
2133 lop = LIST_FIRST(&lp->nfsl_lock);
2134 while (lop != NULL) {
2135 nlop = LIST_NEXT(lop, nfslo_list);
2136 if (lop->nfslo_end == NFS64BITSSET)
2139 len = lop->nfslo_end - lop->nfslo_first;
2140 error = nfscl_trylock(nmp, NULL,
2141 op->nfso_fh, op->nfso_fhlen, lp,
2142 firstlock, 1, lop->nfslo_first, len,
2143 lop->nfslo_type, tcred, p);
2145 nfscl_freelock(lop, 0);
2150 /* If no locks, but a lockowner, just delete it. */
2151 if (LIST_EMPTY(&lp->nfsl_lock))
2152 nfscl_freelockowner(lp, 0);
2157 if (error != 0 && error != NFSERR_BADSESSION)
2158 nfscl_freeopen(op, 0);
2165 * Now, try and get any delegations not yet reclaimed by cobbling
2166 * to-gether an appropriate open.
2169 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2170 while (dp != NULL) {
2171 ndp = TAILQ_NEXT(dp, nfsdl_list);
2172 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2175 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2177 * Name must be as long an largest possible
2178 * NFSV4CL_LOCKNAMELEN. 12 for now.
2180 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2181 NFSV4CL_LOCKNAMELEN);
2182 LIST_INIT(&nowp->nfsow_open);
2183 nowp->nfsow_clp = clp;
2184 nowp->nfsow_seqid = 0;
2185 nowp->nfsow_defunct = 0;
2186 nfscl_lockinit(&nowp->nfsow_rwlock);
2189 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2190 nop = malloc(sizeof (struct nfsclopen) +
2191 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2192 nop->nfso_own = nowp;
2193 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2194 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2195 delegtype = NFSV4OPEN_DELEGATEWRITE;
2197 nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2198 delegtype = NFSV4OPEN_DELEGATEREAD;
2200 nop->nfso_opencnt = 0;
2201 nop->nfso_posixlock = 1;
2202 nop->nfso_fhlen = dp->nfsdl_fhlen;
2203 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2204 LIST_INIT(&nop->nfso_lock);
2205 nop->nfso_stateid.seqid = 0;
2206 nop->nfso_stateid.other[0] = 0;
2207 nop->nfso_stateid.other[1] = 0;
2208 nop->nfso_stateid.other[2] = 0;
2209 newnfs_copycred(&dp->nfsdl_cred, tcred);
2210 newnfs_copyincred(tcred, &nop->nfso_cred);
2212 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2213 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2214 nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2215 delegtype, tcred, p);
2217 if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2218 mode = NFSV4OPEN_ACCESSWRITE;
2220 mode = NFSV4OPEN_ACCESSREAD;
2221 if ((nop->nfso_mode & mode) == mode &&
2222 nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2223 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2225 dp->nfsdl_stateid = tdp->nfsdl_stateid;
2226 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2227 dp->nfsdl_ace = tdp->nfsdl_ace;
2228 dp->nfsdl_change = tdp->nfsdl_change;
2229 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2230 if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2231 dp->nfsdl_flags |= NFSCLDL_RECALL;
2232 free(tdp, M_NFSCLDELEG);
2234 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2240 free(nop, M_NFSCLOPEN);
2242 * Couldn't reclaim it, so throw the state
2245 nfscl_cleandeleg(dp);
2246 nfscl_freedeleg(&clp->nfsc_deleg, dp);
2248 LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2255 * Now, get rid of extra Opens and Delegations.
2257 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2259 newnfs_copycred(&op->nfso_cred, tcred);
2260 error = nfscl_tryclose(op, tcred, nmp, p);
2261 if (error == NFSERR_GRACE)
2262 (void) nfs_catnap(PZERO, error, "nfsexcls");
2263 } while (error == NFSERR_GRACE);
2264 LIST_REMOVE(op, nfso_list);
2265 free(op, M_NFSCLOPEN);
2268 free(nowp, M_NFSCLOWNER);
2270 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2272 newnfs_copycred(&dp->nfsdl_cred, tcred);
2273 error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2274 if (error == NFSERR_GRACE)
2275 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2276 } while (error == NFSERR_GRACE);
2277 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2278 free(dp, M_NFSCLDELEG);
2281 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2282 if (NFSHASNFSV4N(nmp))
2283 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
2287 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2288 wakeup(&clp->nfsc_flags);
2289 nfsv4_unlock(&clp->nfsc_lock, 0);
2296 * This function is called when a server replies with NFSERR_EXPIRED.
2297 * It deletes all state for the client and does a fresh SetClientId/confirm.
2298 * XXX Someday it should post a signal to the process(es) that hold the
2299 * state, so they know that lock state has been lost.
2302 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2304 struct nfsmount *nmp;
2306 int igotlock = 0, error, trycnt;
2309 * If the clientid has gone away or a new SetClientid has already
2310 * been done, just return ok.
2312 if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2316 * First, lock the client structure, so everyone else will
2317 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2318 * that only one thread does the work.
2321 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2323 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2324 NFSCLSTATEMUTEXPTR, NULL);
2325 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2326 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2328 nfsv4_unlock(&clp->nfsc_lock, 0);
2332 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2335 nmp = clp->nfsc_nmp;
2337 panic("nfscl expired");
2338 cred = newnfs_getcred();
2341 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2342 } while ((error == NFSERR_STALECLIENTID ||
2343 error == NFSERR_BADSESSION ||
2344 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2347 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2350 * Expire the state for the client.
2352 nfscl_expireclient(clp, nmp, cred, p);
2354 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2355 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2357 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2358 wakeup(&clp->nfsc_flags);
2359 nfsv4_unlock(&clp->nfsc_lock, 0);
2366 * This function inserts a lock in the list after insert_lop.
2369 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2370 struct nfscllock *insert_lop, int local)
2373 if ((struct nfscllockowner *)insert_lop == lp)
2374 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2376 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2378 nfsstatsv1.cllocallocks++;
2380 nfsstatsv1.cllocks++;
2384 * This function updates the locking for a lock owner and given file. It
2385 * maintains a list of lock ranges ordered on increasing file offset that
2386 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2387 * It always adds new_lop to the list and sometimes uses the one pointed
2389 * Returns 1 if the locks were modified, 0 otherwise.
2392 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2393 struct nfscllock **other_lopp, int local)
2395 struct nfscllock *new_lop = *new_lopp;
2396 struct nfscllock *lop, *tlop, *ilop;
2397 struct nfscllock *other_lop;
2398 int unlock = 0, modified = 0;
2402 * Work down the list until the lock is merged.
2404 if (new_lop->nfslo_type == F_UNLCK)
2406 ilop = (struct nfscllock *)lp;
2407 lop = LIST_FIRST(&lp->nfsl_lock);
2408 while (lop != NULL) {
2410 * Only check locks for this file that aren't before the start of
2413 if (lop->nfslo_end >= new_lop->nfslo_first) {
2414 if (new_lop->nfslo_end < lop->nfslo_first) {
2416 * If the new lock ends before the start of the
2417 * current lock's range, no merge, just insert
2422 if (new_lop->nfslo_type == lop->nfslo_type ||
2423 (new_lop->nfslo_first <= lop->nfslo_first &&
2424 new_lop->nfslo_end >= lop->nfslo_end)) {
2426 * This lock can be absorbed by the new lock/unlock.
2427 * This happens when it covers the entire range
2428 * of the old lock or is contiguous
2429 * with the old lock and is of the same type or an
2432 if (new_lop->nfslo_type != lop->nfslo_type ||
2433 new_lop->nfslo_first != lop->nfslo_first ||
2434 new_lop->nfslo_end != lop->nfslo_end)
2436 if (lop->nfslo_first < new_lop->nfslo_first)
2437 new_lop->nfslo_first = lop->nfslo_first;
2438 if (lop->nfslo_end > new_lop->nfslo_end)
2439 new_lop->nfslo_end = lop->nfslo_end;
2441 lop = LIST_NEXT(lop, nfslo_list);
2442 nfscl_freelock(tlop, local);
2447 * All these cases are for contiguous locks that are not the
2448 * same type, so they can't be merged.
2450 if (new_lop->nfslo_first <= lop->nfslo_first) {
2452 * This case is where the new lock overlaps with the
2453 * first part of the old lock. Move the start of the
2454 * old lock to just past the end of the new lock. The
2455 * new lock will be inserted in front of the old, since
2456 * ilop hasn't been updated. (We are done now.)
2458 if (lop->nfslo_first != new_lop->nfslo_end) {
2459 lop->nfslo_first = new_lop->nfslo_end;
2464 if (new_lop->nfslo_end >= lop->nfslo_end) {
2466 * This case is where the new lock overlaps with the
2467 * end of the old lock's range. Move the old lock's
2468 * end to just before the new lock's first and insert
2469 * the new lock after the old lock.
2470 * Might not be done yet, since the new lock could
2471 * overlap further locks with higher ranges.
2473 if (lop->nfslo_end != new_lop->nfslo_first) {
2474 lop->nfslo_end = new_lop->nfslo_first;
2478 lop = LIST_NEXT(lop, nfslo_list);
2482 * The final case is where the new lock's range is in the
2483 * middle of the current lock's and splits the current lock
2484 * up. Use *other_lopp to handle the second part of the
2485 * split old lock range. (We are done now.)
2486 * For unlock, we use new_lop as other_lop and tmp, since
2487 * other_lop and new_lop are the same for this case.
2488 * We noted the unlock case above, so we don't need
2489 * new_lop->nfslo_type any longer.
2491 tmp = new_lop->nfslo_first;
2493 other_lop = new_lop;
2496 other_lop = *other_lopp;
2499 other_lop->nfslo_first = new_lop->nfslo_end;
2500 other_lop->nfslo_end = lop->nfslo_end;
2501 other_lop->nfslo_type = lop->nfslo_type;
2502 lop->nfslo_end = tmp;
2503 nfscl_insertlock(lp, other_lop, lop, local);
2509 lop = LIST_NEXT(lop, nfslo_list);
2515 * Insert the new lock in the list at the appropriate place.
2518 nfscl_insertlock(lp, new_lop, ilop, local);
2526 * This function must be run as a kernel thread.
2527 * It does Renew Ops and recovery, when required.
2530 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2532 struct nfsclowner *owp, *nowp;
2533 struct nfsclopen *op;
2534 struct nfscllockowner *lp, *nlp;
2535 struct nfscldeleghead dh;
2536 struct nfscldeleg *dp, *ndp;
2539 int error, cbpathdown, islept, igotlock, ret, clearok;
2540 uint32_t recover_done_time = 0;
2542 static time_t prevsec = 0;
2543 struct nfscllockownerfh *lfhp, *nlfhp;
2544 struct nfscllockownerfhhead lfh;
2545 struct nfscllayout *lyp, *nlyp;
2546 struct nfscldevinfo *dip, *ndip;
2547 struct nfscllayouthead rlh;
2548 struct nfsclrecalllayout *recallp;
2549 struct nfsclds *dsp;
2553 cred = newnfs_getcred();
2555 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2556 mp = clp->nfsc_nmp->nm_mountp;
2559 newnfs_setroot(cred);
2561 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2563 * Only allow one full recover within 1/2 of the lease
2564 * duration (nfsc_renew).
2565 * retok is value/result. If passed in set to true,
2566 * it indicates only a CreateSession operation should
2568 * If it is returned true, it indicates that the
2569 * recovery only required a CreateSession.
2572 if (recover_done_time < NFSD_MONOSEC) {
2573 recover_done_time = NFSD_MONOSEC +
2577 NFSCL_DEBUG(1, "Doing recovery, only "
2578 "createsession=%d\n", retok);
2579 nfscl_recover(clp, &retok, cred, p);
2581 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2582 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2583 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2584 clidrev = clp->nfsc_clientidrev;
2585 error = nfsrpc_renew(clp, NULL, cred, p);
2586 if (error == NFSERR_CBPATHDOWN)
2588 else if (error == NFSERR_STALECLIENTID ||
2589 error == NFSERR_BADSESSION) {
2591 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2593 } else if (error == NFSERR_EXPIRED)
2594 (void) nfscl_hasexpired(clp, clidrev, p);
2598 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2599 /* Do renews for any DS sessions. */
2600 NFSLOCKMNT(clp->nfsc_nmp);
2601 /* Skip first entry, since the MDS is handled above. */
2602 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2604 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2605 while (dsp != NULL) {
2606 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2607 dsp->nfsclds_sess.nfsess_defunct == 0) {
2608 dsp->nfsclds_expire = NFSD_MONOSEC +
2610 NFSUNLOCKMNT(clp->nfsc_nmp);
2611 (void)nfsrpc_renew(clp, dsp, cred, p);
2614 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2616 NFSUNLOCKMNT(clp->nfsc_nmp);
2622 /* It's a Total Recall! */
2623 nfscl_totalrecall(clp);
2626 * Now, handle defunct owners.
2628 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2629 if (LIST_EMPTY(&owp->nfsow_open)) {
2630 if (owp->nfsow_defunct != 0)
2631 nfscl_freeopenowner(owp, 0);
2636 * Do the recall on any delegations. To avoid trouble, always
2637 * come back up here after having slept.
2641 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2642 while (dp != NULL) {
2643 ndp = TAILQ_NEXT(dp, nfsdl_list);
2644 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2646 * Wait for outstanding I/O ops to be done.
2648 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2650 nfsv4_unlock(&clp->nfsc_lock, 0);
2653 dp->nfsdl_rwlock.nfslock_lock |=
2655 msleep(&dp->nfsdl_rwlock,
2656 NFSCLSTATEMUTEXPTR, PVFS, "nfscld",
2658 if (NFSCL_FORCEDISM(mp))
2663 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2664 &islept, NFSCLSTATEMUTEXPTR, mp);
2665 if (igotlock == 0 && NFSCL_FORCEDISM(mp))
2671 newnfs_copycred(&dp->nfsdl_cred, cred);
2672 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2675 nfscl_cleandeleg(dp);
2676 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2678 LIST_REMOVE(dp, nfsdl_hash);
2679 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2681 nfsstatsv1.cldelegates--;
2689 * Clear out old delegations, if we are above the high water
2690 * mark. Only clear out ones with no state related to them.
2691 * The tailq list is in LRU order.
2693 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2694 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2695 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2696 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2697 dp->nfsdl_rwlock.nfslock_lock == 0 &&
2698 dp->nfsdl_timestamp < NFSD_MONOSEC &&
2699 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2700 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2702 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2703 op = LIST_FIRST(&owp->nfsow_open);
2710 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2711 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2718 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2719 LIST_REMOVE(dp, nfsdl_hash);
2720 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2722 nfsstatsv1.cldelegates--;
2728 nfsv4_unlock(&clp->nfsc_lock, 0);
2731 * Do the recall on any layouts. To avoid trouble, always
2732 * come back up here after having slept.
2736 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2737 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2739 * Wait for outstanding I/O ops to be done.
2741 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2742 (lyp->nfsly_lock.nfslock_lock &
2743 NFSV4LOCK_LOCK) != 0) {
2744 lyp->nfsly_lock.nfslock_lock |=
2746 msleep(&lyp->nfsly_lock.nfslock_lock,
2747 NFSCLSTATEMUTEXPTR, PVFS, "nfslyp",
2749 if (NFSCL_FORCEDISM(mp))
2753 /* Move the layout to the recall list. */
2754 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2756 LIST_REMOVE(lyp, nfsly_hash);
2757 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2759 /* Handle any layout commits. */
2760 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2761 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2762 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2764 NFSCL_DEBUG(3, "do layoutcommit\n");
2765 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2773 /* Now, look for stale layouts. */
2774 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2775 while (lyp != NULL) {
2776 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2777 if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2778 (lyp->nfsly_flags & NFSLY_RECALL) == 0 &&
2779 lyp->nfsly_lock.nfslock_usecnt == 0 &&
2780 lyp->nfsly_lock.nfslock_lock == 0) {
2781 NFSCL_DEBUG(4, "ret stale lay=%d\n",
2783 recallp = malloc(sizeof(*recallp),
2784 M_NFSLAYRECALL, M_NOWAIT);
2785 if (recallp == NULL)
2787 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2788 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2789 lyp->nfsly_stateid.seqid, 0, 0, NULL,
2796 * Free up any unreferenced device info structures.
2798 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
2799 if (dip->nfsdi_layoutrefs == 0 &&
2800 dip->nfsdi_refcnt == 0) {
2801 NFSCL_DEBUG(4, "freeing devinfo\n");
2802 LIST_REMOVE(dip, nfsdi_list);
2803 nfscl_freedevinfo(dip);
2808 /* Do layout return(s), as required. */
2809 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
2810 TAILQ_REMOVE(&rlh, lyp, nfsly_list);
2811 NFSCL_DEBUG(4, "ret layout\n");
2812 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
2813 nfscl_freelayout(lyp);
2817 * Delegreturn any delegations cleaned out or recalled.
2819 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2820 newnfs_copycred(&dp->nfsdl_cred, cred);
2821 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2822 TAILQ_REMOVE(&dh, dp, nfsdl_list);
2823 free(dp, M_NFSCLDELEG);
2828 * Call nfscl_cleanupkext() once per second to check for
2829 * open/lock owners where the process has exited.
2831 mytime = NFSD_MONOSEC;
2832 if (prevsec != mytime) {
2834 nfscl_cleanupkext(clp, &lfh);
2838 * Do a ReleaseLockOwner for all lock owners where the
2839 * associated process no longer exists, as found by
2840 * nfscl_cleanupkext().
2842 newnfs_setroot(cred);
2843 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
2844 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
2846 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
2847 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
2849 nfscl_freelockowner(lp, 0);
2856 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2857 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
2860 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2861 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2864 wakeup((caddr_t)clp);
2872 * Initiate state recovery. Called when NFSERR_STALECLIENTID,
2873 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
2876 nfscl_initiate_recovery(struct nfsclclient *clp)
2882 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2884 wakeup((caddr_t)clp);
2888 * Dump out the state stuff for debugging.
2891 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
2892 int lockowner, int locks)
2894 struct nfsclclient *clp;
2895 struct nfsclowner *owp;
2896 struct nfsclopen *op;
2897 struct nfscllockowner *lp;
2898 struct nfscllock *lop;
2899 struct nfscldeleg *dp;
2903 printf("nfscl dumpstate NULL clp\n");
2907 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2908 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2909 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2910 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2911 owp->nfsow_owner[0], owp->nfsow_owner[1],
2912 owp->nfsow_owner[2], owp->nfsow_owner[3],
2914 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2916 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2917 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2918 op->nfso_stateid.other[2], op->nfso_opencnt,
2920 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2922 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2923 lp->nfsl_owner[0], lp->nfsl_owner[1],
2924 lp->nfsl_owner[2], lp->nfsl_owner[3],
2926 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2927 lp->nfsl_stateid.other[2]);
2928 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2931 printf("lck typ=%d fst=%ju end=%ju\n",
2932 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2933 (intmax_t)lop->nfslo_end);
2935 printf("lck typ=%d fst=%qd end=%qd\n",
2936 lop->nfslo_type, lop->nfslo_first,
2944 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2945 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2946 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2947 owp->nfsow_owner[0], owp->nfsow_owner[1],
2948 owp->nfsow_owner[2], owp->nfsow_owner[3],
2950 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2952 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2953 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2954 op->nfso_stateid.other[2], op->nfso_opencnt,
2956 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2958 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2959 lp->nfsl_owner[0], lp->nfsl_owner[1],
2960 lp->nfsl_owner[2], lp->nfsl_owner[3],
2962 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2963 lp->nfsl_stateid.other[2]);
2964 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2967 printf("lck typ=%d fst=%ju end=%ju\n",
2968 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2969 (intmax_t)lop->nfslo_end);
2971 printf("lck typ=%d fst=%qd end=%qd\n",
2972 lop->nfslo_type, lop->nfslo_first,
2983 * Check for duplicate open owners and opens.
2984 * (Only used as a diagnostic aid.)
2987 nfscl_dupopen(vnode_t vp, int dupopens)
2989 struct nfsclclient *clp;
2990 struct nfsclowner *owp, *owp2;
2991 struct nfsclopen *op, *op2;
2994 clp = VFSTONFS(vp->v_mount)->nm_clp;
2996 printf("nfscl dupopen NULL clp\n");
2999 nfhp = VTONFS(vp)->n_fhp;
3003 * First, search for duplicate owners.
3004 * These should never happen!
3006 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3007 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3009 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
3010 NFSV4CL_LOCKNAMELEN)) {
3012 printf("DUP OWNER\n");
3013 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3020 * Now, search for duplicate stateids.
3021 * These shouldn't happen, either.
3023 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3024 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3025 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3026 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3028 (op->nfso_stateid.other[0] != 0 ||
3029 op->nfso_stateid.other[1] != 0 ||
3030 op->nfso_stateid.other[2] != 0) &&
3031 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
3032 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
3033 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
3035 printf("DUP STATEID\n");
3036 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3045 * Now search for duplicate opens.
3046 * Duplicate opens for the same owner
3047 * should never occur. Other duplicates are
3048 * possible and are checked for if "dupopens"
3051 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3052 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3053 if (nfhp->nfh_len == op2->nfso_fhlen &&
3054 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3055 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3056 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3057 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3058 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3059 (!NFSBCMP(op->nfso_own->nfsow_owner,
3060 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3062 if (!NFSBCMP(op->nfso_own->nfsow_owner,
3063 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3065 printf("BADDUP OPEN\n");
3068 printf("DUP OPEN\n");
3070 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0,
3083 * During close, find an open that needs to be dereferenced and
3084 * dereference it. If there are no more opens for this file,
3085 * log a message to that effect.
3086 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3087 * on the file's vnode.
3088 * This is the safe way, since it is difficult to identify
3089 * which open the close is for and I/O can be performed after the
3090 * close(2) system call when a file is mmap'd.
3091 * If it returns 0 for success, there will be a referenced
3092 * clp returned via clpp.
3095 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3097 struct nfsclclient *clp;
3098 struct nfsclowner *owp;
3099 struct nfsclopen *op;
3100 struct nfscldeleg *dp;
3104 error = nfscl_getcl(vp->v_mount, NULL, NULL, 1, &clp);
3109 nfhp = VTONFS(vp)->n_fhp;
3113 * First, look for one under a delegation that was locally issued
3114 * and just decrement the opencnt for it. Since all my Opens against
3115 * the server are DENY_NONE, I don't see a problem with hanging
3116 * onto them. (It is much easier to use one of the extant Opens
3117 * that I already have on the server when a Delegation is recalled
3118 * than to do fresh Opens.) Someday, I might need to rethink this, but.
3120 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3122 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3123 op = LIST_FIRST(&owp->nfsow_open);
3126 * Since a delegation is for a file, there
3127 * should never be more than one open for
3130 if (LIST_NEXT(op, nfso_list) != NULL)
3131 panic("nfscdeleg opens");
3132 if (notdecr && op->nfso_opencnt > 0) {
3141 /* Now process the opens against the server. */
3142 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3143 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3144 if (op->nfso_fhlen == nfhp->nfh_len &&
3145 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3147 /* Found an open, decrement cnt if possible */
3148 if (notdecr && op->nfso_opencnt > 0) {
3153 * There are more opens, so just return.
3155 if (op->nfso_opencnt > 0) {
3164 printf("nfscl: never fnd open\n");
3169 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3171 struct nfsclclient *clp;
3172 struct nfsclowner *owp, *nowp;
3173 struct nfsclopen *op;
3174 struct nfscldeleg *dp;
3176 struct nfsclrecalllayout *recallp;
3179 error = nfscl_getcl(vp->v_mount, NULL, NULL, 1, &clp);
3184 nfhp = VTONFS(vp)->n_fhp;
3185 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
3188 * First get rid of the local Open structures, which should be no
3191 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3193 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3194 op = LIST_FIRST(&owp->nfsow_open);
3196 KASSERT((op->nfso_opencnt == 0),
3197 ("nfscl: bad open cnt on deleg"));
3198 nfscl_freeopen(op, 1);
3200 nfscl_freeopenowner(owp, 1);
3204 /* Return any layouts marked return on close. */
3205 nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp);
3207 /* Now process the opens against the server. */
3209 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3210 op = LIST_FIRST(&owp->nfsow_open);
3211 while (op != NULL) {
3212 if (op->nfso_fhlen == nfhp->nfh_len &&
3213 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3215 /* Found an open, close it. */
3217 KASSERT((op->nfso_opencnt == 0),
3218 ("nfscl: bad open cnt on server (%d)",
3222 nfsrpc_doclose(VFSTONFS(vp->v_mount), op, p);
3226 op = LIST_NEXT(op, nfso_list);
3231 * recallp has been set NULL by nfscl_retoncloselayout() if it was
3232 * used by the function, but calling free() with a NULL pointer is ok.
3234 free(recallp, M_NFSLAYRECALL);
3239 * Return all delegations on this client.
3240 * (Must be called with client sleep lock.)
3243 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
3245 struct nfscldeleg *dp, *ndp;
3248 cred = newnfs_getcred();
3249 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3250 nfscl_cleandeleg(dp);
3251 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3252 nfscl_freedeleg(&clp->nfsc_deleg, dp);
3258 * Do a callback RPC.
3261 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3263 int clist, gotseq_ok, i, j, k, op, rcalls;
3265 struct nfsclclient *clp;
3266 struct nfscldeleg *dp = NULL;
3267 int numops, taglen = -1, error = 0, trunc __unused;
3268 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3269 u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3275 nfsattrbit_t attrbits, rattrbits;
3276 nfsv4stateid_t stateid;
3277 uint32_t seqid, slotid = 0, highslot, cachethis __unused;
3278 uint8_t sessionid[NFSX_V4SESSIONID];
3280 struct nfscllayout *lyp;
3281 uint64_t filesid[2], len, off;
3282 int changed, gotone, laytype, recalltype;
3284 struct nfsclrecalllayout *recallp = NULL;
3285 struct nfsclsession *tsep;
3289 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3290 taglen = fxdr_unsigned(int, *tl);
3295 if (taglen <= NFSV4_SMALLSTR)
3298 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3299 error = nfsrv_mtostr(nd, tagstr, taglen);
3301 if (taglen > NFSV4_SMALLSTR)
3302 free(tagstr, M_TEMP);
3306 (void) nfsm_strtom(nd, tag, taglen);
3307 if (taglen > NFSV4_SMALLSTR) {
3308 free(tagstr, M_TEMP);
3310 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3311 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3312 minorvers = fxdr_unsigned(u_int32_t, *tl++);
3313 if (minorvers != NFSV4_MINORVERSION &&
3314 minorvers != NFSV41_MINORVERSION &&
3315 minorvers != NFSV42_MINORVERSION)
3316 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3317 cbident = fxdr_unsigned(u_int32_t, *tl++);
3321 numops = fxdr_unsigned(int, *tl);
3323 * Loop around doing the sub ops.
3325 for (i = 0; i < numops; i++) {
3326 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3327 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3329 op = fxdr_unsigned(int, *tl);
3330 if (op < NFSV4OP_CBGETATTR ||
3331 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3332 (op > NFSV4OP_CBNOTIFYDEVID &&
3333 minorvers == NFSV41_MINORVERSION) ||
3334 (op > NFSV4OP_CBOFFLOAD &&
3335 minorvers == NFSV42_MINORVERSION)) {
3336 nd->nd_repstat = NFSERR_OPILLEGAL;
3337 *repp = nfscl_errmap(nd, minorvers);
3341 nd->nd_procnum = op;
3342 if (op < NFSV42_CBNOPS)
3343 nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
3345 case NFSV4OP_CBGETATTR:
3346 NFSCL_DEBUG(4, "cbgetattr\n");
3349 error = nfsm_getfh(nd, &nfhp);
3351 error = nfsrv_getattrbits(nd, &attrbits,
3353 if (error == 0 && i == 0 &&
3354 minorvers != NFSV4_MINORVERSION)
3355 error = NFSERR_OPNOTINSESS;
3357 mp = nfscl_getmnt(minorvers, sessionid, cbident,
3360 error = NFSERR_SERVERFAULT;
3363 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3364 nfhp->nfh_len, p, &np);
3369 NFSZERO_ATTRBIT(&rattrbits);
3371 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3374 if (NFSISSET_ATTRBIT(&attrbits,
3377 va.va_size = np->n_size;
3381 NFSSETBIT_ATTRBIT(&rattrbits,
3384 if (NFSISSET_ATTRBIT(&attrbits,
3385 NFSATTRBIT_CHANGE)) {
3389 (np->n_flag & NDELEGMOD))
3391 NFSSETBIT_ATTRBIT(&rattrbits,
3395 error = NFSERR_SERVERFAULT;
3403 free(nfhp, M_NFSFH);
3405 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3406 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3409 case NFSV4OP_CBRECALL:
3410 NFSCL_DEBUG(4, "cbrecall\n");
3411 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3413 stateid.seqid = *tl++;
3414 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3416 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3417 trunc = fxdr_unsigned(int, *tl);
3418 error = nfsm_getfh(nd, &nfhp);
3419 if (error == 0 && i == 0 &&
3420 minorvers != NFSV4_MINORVERSION)
3421 error = NFSERR_OPNOTINSESS;
3424 if (minorvers == NFSV4_MINORVERSION)
3425 clp = nfscl_getclnt(cbident);
3427 clp = nfscl_getclntsess(sessionid);
3429 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3431 if (dp != NULL && (dp->nfsdl_flags &
3432 NFSCLDL_DELEGRET) == 0) {
3435 wakeup((caddr_t)clp);
3438 error = NFSERR_SERVERFAULT;
3443 free(nfhp, M_NFSFH);
3445 case NFSV4OP_CBLAYOUTRECALL:
3446 NFSCL_DEBUG(4, "cblayrec\n");
3448 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3449 laytype = fxdr_unsigned(int, *tl++);
3450 iomode = fxdr_unsigned(uint32_t, *tl++);
3451 if (newnfs_true == *tl++)
3455 recalltype = fxdr_unsigned(int, *tl);
3456 NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
3457 laytype, iomode, changed, recalltype);
3458 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3460 if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
3461 laytype != NFSLAYOUT_FLEXFILE)
3462 error = NFSERR_NOMATCHLAYOUT;
3463 else if (recalltype == NFSLAYOUTRETURN_FILE) {
3464 error = nfsm_getfh(nd, &nfhp);
3465 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3468 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3470 off = fxdr_hyper(tl); tl += 2;
3471 len = fxdr_hyper(tl); tl += 2;
3472 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3473 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3474 if (minorvers == NFSV4_MINORVERSION)
3475 error = NFSERR_NOTSUPP;
3477 error = NFSERR_OPNOTINSESS;
3478 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
3479 (uintmax_t)off, (uintmax_t)len,
3480 stateid.seqid, error);
3483 clp = nfscl_getclntsess(sessionid);
3484 NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3486 lyp = nfscl_findlayout(clp,
3489 NFSCL_DEBUG(4, "cblyp=%p\n",
3494 NFSLY_FLEXFILE)) != 0 &&
3495 !NFSBCMP(stateid.other,
3496 lyp->nfsly_stateid.other,
3497 NFSX_STATEIDOTHER)) {
3507 lyp->nfsly_stateid.seqid)
3508 lyp->nfsly_stateid.seqid =
3516 lyp->nfsly_stateid.seqid);
3519 NFSERR_NOMATCHLAYOUT;
3521 error = NFSERR_NOMATCHLAYOUT;
3524 free(nfhp, M_NFSFH);
3525 } else if (recalltype == NFSLAYOUTRETURN_FSID) {
3526 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3527 filesid[0] = fxdr_hyper(tl); tl += 2;
3528 filesid[1] = fxdr_hyper(tl); tl += 2;
3531 clp = nfscl_getclntsess(sessionid);
3533 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3535 if (lyp->nfsly_filesid[0] ==
3537 lyp->nfsly_filesid[1] ==
3544 lyp->nfsly_stateid.seqid,
3554 error = NFSERR_NOMATCHLAYOUT;
3556 error = NFSERR_NOMATCHLAYOUT;
3558 } else if (recalltype == NFSLAYOUTRETURN_ALL) {
3561 clp = nfscl_getclntsess(sessionid);
3563 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3565 error = nfscl_layoutrecall(
3566 recalltype, lyp, iomode, 0,
3568 lyp->nfsly_stateid.seqid,
3569 0, 0, NULL, recallp);
3576 error = NFSERR_NOMATCHLAYOUT;
3578 error = NFSERR_NOMATCHLAYOUT;
3581 error = NFSERR_NOMATCHLAYOUT;
3582 if (recallp != NULL) {
3583 free(recallp, M_NFSLAYRECALL);
3587 case NFSV4OP_CBSEQUENCE:
3588 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3590 bcopy(tl, sessionid, NFSX_V4SESSIONID);
3591 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3592 seqid = fxdr_unsigned(uint32_t, *tl++);
3593 slotid = fxdr_unsigned(uint32_t, *tl++);
3594 highslot = fxdr_unsigned(uint32_t, *tl++);
3596 /* Throw away the referring call stuff. */
3597 clist = fxdr_unsigned(int, *tl);
3598 for (j = 0; j < clist; j++) {
3599 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3601 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3602 rcalls = fxdr_unsigned(int, *tl);
3603 for (k = 0; k < rcalls; k++) {
3604 NFSM_DISSECT(tl, uint32_t *,
3610 clp = nfscl_getclntsess(sessionid);
3612 error = NFSERR_SERVERFAULT;
3614 error = NFSERR_SEQUENCEPOS;
3616 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3617 error = nfsv4_seqsession(seqid, slotid,
3618 highslot, tsep->nfsess_cbslots, &rep,
3619 tsep->nfsess_backslots);
3622 if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
3626 * Handle a reply for a retried
3627 * callback. The reply will be
3628 * re-inserted in the session cache
3629 * by the nfsv4_seqsess_cacherep() call
3632 KASSERT(error == NFSERR_REPLYFROMCACHE,
3633 ("cbsequence: non-NULL rep"));
3634 NFSCL_DEBUG(4, "Got cbretry\n");
3635 m_freem(nd->nd_mreq);
3640 NFSM_BUILD(tl, uint32_t *,
3641 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3642 bcopy(sessionid, tl, NFSX_V4SESSIONID);
3643 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3644 *tl++ = txdr_unsigned(seqid);
3645 *tl++ = txdr_unsigned(slotid);
3646 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3647 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3651 if (i == 0 && minorvers != NFSV4_MINORVERSION)
3652 error = NFSERR_OPNOTINSESS;
3654 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3655 error = NFSERR_NOTSUPP;
3660 if (error == EBADRPC || error == NFSERR_BADXDR) {
3661 nd->nd_repstat = NFSERR_BADXDR;
3663 nd->nd_repstat = error;
3668 if (nd->nd_repstat) {
3669 *repp = nfscl_errmap(nd, minorvers);
3672 *repp = 0; /* NFS4_OK */
3675 if (recallp != NULL)
3676 free(recallp, M_NFSLAYRECALL);
3678 if (error == EBADRPC || error == NFSERR_BADXDR)
3679 nd->nd_repstat = NFSERR_BADXDR;
3681 printf("nfsv4 comperr1=%d\n", error);
3684 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3688 *retopsp = txdr_unsigned(retops);
3690 *nd->nd_errp = nfscl_errmap(nd, minorvers);
3692 if (gotseq_ok != 0) {
3693 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3695 clp = nfscl_getclntsess(sessionid);
3697 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3698 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3709 * Generate the next cbident value. Basically just increment a static value
3710 * and then check that it isn't already in the list, if it has wrapped around.
3713 nfscl_nextcbident(void)
3715 struct nfsclclient *clp;
3717 static u_int32_t nextcbident = 0;
3718 static int haswrapped = 0;
3721 if (nextcbident == 0)
3725 * Search the clientid list for one already using this cbident.
3730 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3731 if (clp->nfsc_cbident == nextcbident) {
3741 return (nextcbident);
3745 * Get the mount point related to a given cbident or session and busy it.
3748 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
3749 struct nfsclclient **clpp)
3751 struct nfsclclient *clp;
3754 struct nfsclsession *tsep;
3758 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3759 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3760 if (minorvers == NFSV4_MINORVERSION) {
3761 if (clp->nfsc_cbident == cbident)
3763 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3771 mp = clp->nfsc_nmp->nm_mountp;
3774 error = vfs_busy(mp, 0);
3783 * Get the clientid pointer related to a given cbident.
3785 static struct nfsclclient *
3786 nfscl_getclnt(u_int32_t cbident)
3788 struct nfsclclient *clp;
3790 LIST_FOREACH(clp, &nfsclhead, nfsc_list)
3791 if (clp->nfsc_cbident == cbident)
3797 * Get the clientid pointer related to a given sessionid.
3799 static struct nfsclclient *
3800 nfscl_getclntsess(uint8_t *sessionid)
3802 struct nfsclclient *clp;
3803 struct nfsclsession *tsep;
3805 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3806 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3807 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3815 * Search for a lock conflict locally on the client. A conflict occurs if
3816 * - not same owner and overlapping byte range and at least one of them is
3817 * a write lock or this is an unlock.
3820 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
3821 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
3822 struct nfscllock **lopp)
3824 struct nfsclowner *owp;
3825 struct nfsclopen *op;
3829 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3833 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3834 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3835 if (op->nfso_fhlen == fhlen &&
3836 !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
3837 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
3848 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3849 u_int8_t *own, struct nfscllock **lopp)
3851 struct nfscllockowner *lp;
3852 struct nfscllock *lop;
3854 LIST_FOREACH(lp, lhp, nfsl_list) {
3855 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
3856 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3857 if (lop->nfslo_first >= nlop->nfslo_end)
3859 if (lop->nfslo_end <= nlop->nfslo_first)
3861 if (lop->nfslo_type == F_WRLCK ||
3862 nlop->nfslo_type == F_WRLCK ||
3863 nlop->nfslo_type == F_UNLCK) {
3866 return (NFSERR_DENIED);
3875 * Check for a local conflicting lock.
3878 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
3879 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
3881 struct nfscllock *lop, nlck;
3882 struct nfscldeleg *dp;
3884 u_int8_t own[NFSV4CL_LOCKNAMELEN];
3887 nlck.nfslo_type = fl->l_type;
3888 nlck.nfslo_first = off;
3889 if (len == NFS64BITSSET) {
3890 nlck.nfslo_end = NFS64BITSSET;
3892 nlck.nfslo_end = off + len;
3893 if (nlck.nfslo_end <= nlck.nfslo_first)
3894 return (NFSERR_INVAL);
3897 nfscl_filllockowner(id, own, flags);
3899 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3900 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
3901 &nlck, own, dp, &lop);
3903 fl->l_whence = SEEK_SET;
3904 fl->l_start = lop->nfslo_first;
3905 if (lop->nfslo_end == NFS64BITSSET)
3908 fl->l_len = lop->nfslo_end - lop->nfslo_first;
3909 fl->l_pid = (pid_t)0;
3910 fl->l_type = lop->nfslo_type;
3911 error = -1; /* no RPC required */
3912 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
3913 fl->l_type == F_RDLCK)) {
3915 * The delegation ensures that there isn't a conflicting
3916 * lock on the server, so return -1 to indicate an RPC
3919 fl->l_type = F_UNLCK;
3927 * Handle Recall of a delegation.
3928 * The clp must be exclusive locked when this is called.
3931 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
3932 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
3933 int called_from_renewthread)
3935 struct nfsclowner *owp, *lowp, *nowp;
3936 struct nfsclopen *op, *lop;
3937 struct nfscllockowner *lp;
3938 struct nfscllock *lckp;
3940 int error = 0, ret, gotvp = 0;
3944 * First, get a vnode for the file. This is needed to do RPCs.
3946 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
3947 dp->nfsdl_fhlen, p, &np);
3950 * File isn't open, so nothing to move over to the
3960 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
3963 * Ok, if it's a write delegation, flush data to the server, so
3964 * that close/open consistency is retained.
3968 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
3969 np->n_flag |= NDELEGRECALL;
3971 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
3973 np->n_flag &= ~NDELEGRECALL;
3975 NFSINVALATTRCACHE(np);
3977 if (ret == EIO && called_from_renewthread != 0) {
3979 * If the flush failed with EIO for the renew thread,
3980 * return now, so that the dirty buffer will be flushed
3989 * Now, for each openowner with opens issued locally, move them
3990 * over to state against the server.
3992 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
3993 lop = LIST_FIRST(&lowp->nfsow_open);
3995 if (LIST_NEXT(lop, nfso_list) != NULL)
3996 panic("nfsdlg mult opens");
3998 * Look for the same openowner against the server.
4000 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
4001 if (!NFSBCMP(lowp->nfsow_owner,
4002 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
4003 newnfs_copycred(&dp->nfsdl_cred, cred);
4004 ret = nfscl_moveopen(vp, clp, nmp, lop,
4006 if (ret == NFSERR_STALECLIENTID ||
4007 ret == NFSERR_STALEDONTRECOVER ||
4008 ret == NFSERR_BADSESSION) {
4014 nfscl_freeopen(lop, 1);
4023 * If no openowner found, create one and get an open
4028 sizeof (struct nfsclowner), M_NFSCLOWNER,
4030 nfscl_newopen(clp, NULL, &owp, &nowp, &op,
4031 NULL, lowp->nfsow_owner, dp->nfsdl_fh,
4032 dp->nfsdl_fhlen, NULL, NULL);
4033 newnfs_copycred(&dp->nfsdl_cred, cred);
4034 ret = nfscl_moveopen(vp, clp, nmp, lop,
4037 nfscl_freeopenowner(owp, 0);
4038 if (ret == NFSERR_STALECLIENTID ||
4039 ret == NFSERR_STALEDONTRECOVER ||
4040 ret == NFSERR_BADSESSION) {
4046 nfscl_freeopen(lop, 1);
4056 * Now, get byte range locks for any locks done locally.
4058 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4059 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
4060 newnfs_copycred(&dp->nfsdl_cred, cred);
4061 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
4062 if (ret == NFSERR_STALESTATEID ||
4063 ret == NFSERR_STALEDONTRECOVER ||
4064 ret == NFSERR_STALECLIENTID ||
4065 ret == NFSERR_BADSESSION) {
4080 * Move a locally issued open over to an owner on the state list.
4081 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
4082 * returns with it unlocked.
4085 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4086 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
4087 struct ucred *cred, NFSPROC_T *p)
4089 struct nfsclopen *op, *nop;
4090 struct nfscldeleg *ndp;
4092 int error = 0, newone;
4095 * First, look for an appropriate open, If found, just increment the
4098 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4099 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4100 op->nfso_fhlen == lop->nfso_fhlen &&
4101 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4102 op->nfso_opencnt += lop->nfso_opencnt;
4103 nfscl_freeopen(lop, 1);
4108 /* No appropriate open, so we have to do one against the server. */
4110 nop = malloc(sizeof (struct nfsclopen) +
4111 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4113 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4114 lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
4116 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4117 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4118 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4121 nfscl_freeopen(op, 0);
4123 op->nfso_mode |= lop->nfso_mode;
4124 op->nfso_opencnt += lop->nfso_opencnt;
4125 nfscl_freeopen(lop, 1);
4128 free(nop, M_NFSCLOPEN);
4131 * What should I do with the returned delegation, since the
4132 * delegation is being recalled? For now, just printf and
4135 printf("Moveopen returned deleg\n");
4136 free(ndp, M_NFSCLDELEG);
4142 * Recall all delegations on this client.
4145 nfscl_totalrecall(struct nfsclclient *clp)
4147 struct nfscldeleg *dp;
4149 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4150 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4151 dp->nfsdl_flags |= NFSCLDL_RECALL;
4156 * Relock byte ranges. Called for delegation recall and state expiry.
4159 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4160 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4163 struct nfscllockowner *nlp;
4166 int error, newone, donelocally;
4168 off = lop->nfslo_first;
4169 len = lop->nfslo_end - lop->nfslo_first;
4170 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4171 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4172 lp->nfsl_openowner, &nlp, &newone, &donelocally);
4173 if (error || donelocally)
4175 nfhp = VTONFS(vp)->n_fhp;
4176 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4177 nfhp->nfh_len, nlp, newone, 0, off,
4178 len, lop->nfslo_type, cred, p);
4180 nfscl_freelockowner(nlp, 0);
4185 * Called to re-open a file. Basically get a vnode for the file handle
4186 * and then call nfsrpc_openrpc() to do the rest.
4189 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4190 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4191 struct ucred *cred, NFSPROC_T *p)
4197 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4201 if (np->n_v4 != NULL) {
4202 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4203 np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4204 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4214 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4215 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4219 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4220 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4221 u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4222 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4227 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4228 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4230 if (error == NFSERR_DELAY)
4231 (void) nfs_catnap(PZERO, error, "nfstryop");
4232 } while (error == NFSERR_DELAY);
4233 if (error == EAUTH || error == EACCES) {
4234 /* Try again using system credentials */
4235 newnfs_setroot(cred);
4237 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4238 newfhlen, mode, op, name, namelen, ndpp, reclaim,
4239 delegtype, cred, p, 1, 0);
4240 if (error == NFSERR_DELAY)
4241 (void) nfs_catnap(PZERO, error, "nfstryop");
4242 } while (error == NFSERR_DELAY);
4248 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4249 * NFSERR_DELAY. Also, retry with system credentials, if the provided
4253 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4254 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4255 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4257 struct nfsrv_descript nfsd, *nd = &nfsd;
4261 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4262 reclaim, off, len, type, cred, p, 0);
4263 if (!error && nd->nd_repstat == NFSERR_DELAY)
4264 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4266 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4268 error = nd->nd_repstat;
4269 if (error == EAUTH || error == EACCES) {
4270 /* Try again using root credentials */
4271 newnfs_setroot(cred);
4273 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4274 newone, reclaim, off, len, type, cred, p, 1);
4275 if (!error && nd->nd_repstat == NFSERR_DELAY)
4276 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4278 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4280 error = nd->nd_repstat;
4286 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4287 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4291 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4292 struct nfsmount *nmp, NFSPROC_T *p)
4297 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4298 if (error == NFSERR_DELAY)
4299 (void) nfs_catnap(PZERO, error, "nfstrydp");
4300 } while (error == NFSERR_DELAY);
4301 if (error == EAUTH || error == EACCES) {
4302 /* Try again using system credentials */
4303 newnfs_setroot(cred);
4305 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4306 if (error == NFSERR_DELAY)
4307 (void) nfs_catnap(PZERO, error, "nfstrydp");
4308 } while (error == NFSERR_DELAY);
4314 * Try a close against the server. Just call nfsrpc_closerpc(),
4315 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4319 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4320 struct nfsmount *nmp, NFSPROC_T *p)
4322 struct nfsrv_descript nfsd, *nd = &nfsd;
4326 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4327 if (error == NFSERR_DELAY)
4328 (void) nfs_catnap(PZERO, error, "nfstrycl");
4329 } while (error == NFSERR_DELAY);
4330 if (error == EAUTH || error == EACCES) {
4331 /* Try again using system credentials */
4332 newnfs_setroot(cred);
4334 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4335 if (error == NFSERR_DELAY)
4336 (void) nfs_catnap(PZERO, error, "nfstrycl");
4337 } while (error == NFSERR_DELAY);
4343 * Decide if a delegation on a file permits close without flushing writes
4344 * to the server. This might be a big performance win in some environments.
4345 * (Not useful until the client does caching on local stable storage.)
4348 nfscl_mustflush(vnode_t vp)
4350 struct nfsclclient *clp;
4351 struct nfscldeleg *dp;
4353 struct nfsmount *nmp;
4356 nmp = VFSTONFS(vp->v_mount);
4357 if (!NFSHASNFSV4(nmp))
4360 clp = nfscl_findcl(nmp);
4365 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4366 if (dp != NULL && (dp->nfsdl_flags &
4367 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4369 (dp->nfsdl_sizelimit >= np->n_size ||
4370 !NFSHASSTRICT3530(nmp))) {
4379 * See if a (write) delegation exists for this file.
4382 nfscl_nodeleg(vnode_t vp, int writedeleg)
4384 struct nfsclclient *clp;
4385 struct nfscldeleg *dp;
4387 struct nfsmount *nmp;
4390 nmp = VFSTONFS(vp->v_mount);
4391 if (!NFSHASNFSV4(nmp))
4394 clp = nfscl_findcl(nmp);
4399 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4401 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4402 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4412 * Look for an associated delegation that should be DelegReturned.
4415 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4417 struct nfsclclient *clp;
4418 struct nfscldeleg *dp;
4419 struct nfsclowner *owp;
4420 struct nfscllockowner *lp;
4421 struct nfsmount *nmp;
4424 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4426 nmp = VFSTONFS(vp->v_mount);
4430 * Loop around waiting for:
4431 * - outstanding I/O operations on delegations to complete
4432 * - for a delegation on vp that has state, lock the client and
4434 * - return delegation with no state
4437 clp = nfscl_findcl(nmp);
4442 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4443 np->n_fhp->nfh_len);
4446 * Wait for outstanding I/O ops to be done.
4448 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4450 nfsv4_unlock(&clp->nfsc_lock, 0);
4453 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4454 (void) nfsmsleep(&dp->nfsdl_rwlock,
4455 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4459 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4460 if (!LIST_EMPTY(&owp->nfsow_open)) {
4466 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4467 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4473 if (needsrecall && !triedrecall) {
4474 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4477 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4478 &islept, NFSCLSTATEMUTEXPTR, NULL);
4485 cred = newnfs_getcred();
4486 newnfs_copycred(&dp->nfsdl_cred, cred);
4487 (void) nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0);
4491 nfsv4_unlock(&clp->nfsc_lock, 0);
4495 *stp = dp->nfsdl_stateid;
4497 nfscl_cleandeleg(dp);
4498 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4501 nfsv4_unlock(&clp->nfsc_lock, 0);
4508 * Look for associated delegation(s) that should be DelegReturned.
4511 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4512 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4514 struct nfsclclient *clp;
4515 struct nfscldeleg *dp;
4516 struct nfsclowner *owp;
4517 struct nfscllockowner *lp;
4518 struct nfsmount *nmp;
4521 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4523 nmp = VFSTONFS(fvp->v_mount);
4528 * Loop around waiting for:
4529 * - outstanding I/O operations on delegations to complete
4530 * - for a delegation on fvp that has state, lock the client and
4532 * - return delegation(s) with no state.
4535 clp = nfscl_findcl(nmp);
4541 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4542 np->n_fhp->nfh_len);
4543 if (dp != NULL && *gotfdp == 0) {
4545 * Wait for outstanding I/O ops to be done.
4547 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4549 nfsv4_unlock(&clp->nfsc_lock, 0);
4552 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4553 (void) nfsmsleep(&dp->nfsdl_rwlock,
4554 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4558 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4559 if (!LIST_EMPTY(&owp->nfsow_open)) {
4565 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4566 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4572 if (needsrecall && !triedrecall) {
4573 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4576 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4577 &islept, NFSCLSTATEMUTEXPTR, NULL);
4584 cred = newnfs_getcred();
4585 newnfs_copycred(&dp->nfsdl_cred, cred);
4586 (void) nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0);
4590 nfsv4_unlock(&clp->nfsc_lock, 0);
4594 *fstp = dp->nfsdl_stateid;
4597 nfscl_cleandeleg(dp);
4598 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4601 nfsv4_unlock(&clp->nfsc_lock, 0);
4606 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4607 np->n_fhp->nfh_len);
4608 if (dp != NULL && *gottdp == 0) {
4610 * Wait for outstanding I/O ops to be done.
4612 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4613 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4614 (void) nfsmsleep(&dp->nfsdl_rwlock,
4615 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4618 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4619 if (!LIST_EMPTY(&owp->nfsow_open)) {
4624 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4625 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4630 *tstp = dp->nfsdl_stateid;
4633 nfscl_cleandeleg(dp);
4634 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4643 * Get a reference on the clientid associated with the mount point.
4644 * Return 1 if success, 0 otherwise.
4647 nfscl_getref(struct nfsmount *nmp)
4649 struct nfsclclient *clp;
4652 clp = nfscl_findcl(nmp);
4657 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
4663 * Release a reference on a clientid acquired with the above call.
4666 nfscl_relref(struct nfsmount *nmp)
4668 struct nfsclclient *clp;
4671 clp = nfscl_findcl(nmp);
4676 nfsv4_relref(&clp->nfsc_lock);
4681 * Save the size attribute in the delegation, since the nfsnode
4685 nfscl_reclaimnode(vnode_t vp)
4687 struct nfsclclient *clp;
4688 struct nfscldeleg *dp;
4689 struct nfsnode *np = VTONFS(vp);
4690 struct nfsmount *nmp;
4692 nmp = VFSTONFS(vp->v_mount);
4693 if (!NFSHASNFSV4(nmp))
4696 clp = nfscl_findcl(nmp);
4701 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4702 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4703 dp->nfsdl_size = np->n_size;
4708 * Get the saved size attribute in the delegation, since it is a
4709 * newly allocated nfsnode.
4712 nfscl_newnode(vnode_t vp)
4714 struct nfsclclient *clp;
4715 struct nfscldeleg *dp;
4716 struct nfsnode *np = VTONFS(vp);
4717 struct nfsmount *nmp;
4719 nmp = VFSTONFS(vp->v_mount);
4720 if (!NFSHASNFSV4(nmp))
4723 clp = nfscl_findcl(nmp);
4728 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4729 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4730 np->n_size = dp->nfsdl_size;
4735 * If there is a valid write delegation for this file, set the modtime
4736 * to the local clock time.
4739 nfscl_delegmodtime(vnode_t vp)
4741 struct nfsclclient *clp;
4742 struct nfscldeleg *dp;
4743 struct nfsnode *np = VTONFS(vp);
4744 struct nfsmount *nmp;
4746 nmp = VFSTONFS(vp->v_mount);
4747 if (!NFSHASNFSV4(nmp))
4750 clp = nfscl_findcl(nmp);
4755 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4756 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4757 nanotime(&dp->nfsdl_modtime);
4758 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4764 * If there is a valid write delegation for this file with a modtime set,
4765 * put that modtime in mtime.
4768 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4770 struct nfsclclient *clp;
4771 struct nfscldeleg *dp;
4772 struct nfsnode *np = VTONFS(vp);
4773 struct nfsmount *nmp;
4775 nmp = VFSTONFS(vp->v_mount);
4776 if (!NFSHASNFSV4(nmp))
4779 clp = nfscl_findcl(nmp);
4784 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4786 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4787 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4788 *mtime = dp->nfsdl_modtime;
4793 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
4795 short *defaulterrp, *errp;
4797 if (!nd->nd_repstat)
4799 if (nd->nd_procnum == NFSPROC_NOOP)
4800 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4801 if (nd->nd_repstat == EBADRPC)
4802 return (txdr_unsigned(NFSERR_BADXDR));
4803 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4804 nd->nd_repstat == NFSERR_OPILLEGAL)
4805 return (txdr_unsigned(nd->nd_repstat));
4806 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
4807 minorvers > NFSV4_MINORVERSION) {
4808 /* NFSv4.n error. */
4809 return (txdr_unsigned(nd->nd_repstat));
4811 if (nd->nd_procnum < NFSV4OP_CBNOPS)
4812 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4814 return (txdr_unsigned(nd->nd_repstat));
4816 if (*errp == (short)nd->nd_repstat)
4817 return (txdr_unsigned(nd->nd_repstat));
4818 return (txdr_unsigned(*defaulterrp));
4822 * Called to find/add a layout to a client.
4823 * This function returns the layout with a refcnt (shared lock) upon
4824 * success (returns 0) or with no lock/refcnt on the layout when an
4825 * error is returned.
4826 * If a layout is passed in via lypp, it is locked (exclusively locked).
4829 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4830 nfsv4stateid_t *stateidp, int layouttype, int retonclose,
4831 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
4832 struct ucred *cred, NFSPROC_T *p)
4834 struct nfsclclient *clp;
4835 struct nfscllayout *lyp, *tlyp;
4836 struct nfsclflayout *flp;
4837 struct nfsnode *np = VTONFS(vp);
4839 int layout_passed_in;
4841 mp = nmp->nm_mountp;
4842 layout_passed_in = 1;
4846 layout_passed_in = 0;
4847 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
4854 if (layout_passed_in != 0)
4855 nfsv4_unlock(&lyp->nfsly_lock, 0);
4858 free(tlyp, M_NFSLAYOUT);
4863 * Although no lyp was passed in, another thread might have
4864 * allocated one. If one is found, just increment it's ref
4865 * count and return it.
4867 lyp = nfscl_findlayout(clp, fhp, fhlen);
4871 lyp->nfsly_stateid.seqid = stateidp->seqid;
4872 lyp->nfsly_stateid.other[0] = stateidp->other[0];
4873 lyp->nfsly_stateid.other[1] = stateidp->other[1];
4874 lyp->nfsly_stateid.other[2] = stateidp->other[2];
4875 lyp->nfsly_lastbyte = 0;
4876 LIST_INIT(&lyp->nfsly_flayread);
4877 LIST_INIT(&lyp->nfsly_flayrw);
4878 LIST_INIT(&lyp->nfsly_recall);
4879 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
4880 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
4881 lyp->nfsly_clp = clp;
4882 if (layouttype == NFSLAYOUT_FLEXFILE)
4883 lyp->nfsly_flags = NFSLY_FLEXFILE;
4885 lyp->nfsly_flags = NFSLY_FILES;
4886 if (retonclose != 0)
4887 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4888 lyp->nfsly_fhlen = fhlen;
4889 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
4890 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4891 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
4893 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4896 if (retonclose != 0)
4897 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4898 if (stateidp->seqid > lyp->nfsly_stateid.seqid)
4899 lyp->nfsly_stateid.seqid = stateidp->seqid;
4900 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4901 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4902 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4904 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
4905 if (NFSCL_FORCEDISM(mp)) {
4908 free(tlyp, M_NFSLAYOUT);
4912 } else if (stateidp->seqid > lyp->nfsly_stateid.seqid)
4913 lyp->nfsly_stateid.seqid = stateidp->seqid;
4915 /* Merge the new list of File Layouts into the list. */
4916 flp = LIST_FIRST(fhlp);
4918 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
4919 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
4921 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
4923 if (layout_passed_in != 0)
4924 nfsv4_unlock(&lyp->nfsly_lock, 1);
4927 free(tlyp, M_NFSLAYOUT);
4932 * Search for a layout by MDS file handle.
4933 * If one is found, it is returned with a refcnt (shared lock) iff
4934 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
4937 struct nfscllayout *
4938 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
4939 uint64_t off, struct nfsclflayout **retflpp, int *recalledp)
4941 struct nfscllayout *lyp;
4943 int error, igotlock;
4945 mp = clp->nfsc_nmp->nm_mountp;
4949 lyp = nfscl_findlayout(clp, fhp, fhlen);
4951 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
4952 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4953 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4954 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4955 error = nfscl_findlayoutforio(lyp, off,
4956 NFSV4OPEN_ACCESSREAD, retflpp);
4958 nfsv4_getref(&lyp->nfsly_lock, NULL,
4959 NFSCLSTATEMUTEXPTR, mp);
4962 igotlock = nfsv4_lock(&lyp->nfsly_lock,
4963 1, NULL, NFSCLSTATEMUTEXPTR, mp);
4964 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
4967 if (NFSCL_FORCEDISM(mp)) {
4981 * Search for a layout by MDS file handle. If one is found, mark in to be
4982 * recalled, if it already marked "return on close".
4985 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
4986 int fhlen, struct nfsclrecalllayout **recallpp)
4988 struct nfscllayout *lyp;
4991 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) ||
4992 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
4993 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
4995 lyp = nfscl_findlayout(clp, fhp, fhlen);
4996 if (lyp != NULL && (lyp->nfsly_flags & (NFSLY_RETONCLOSE |
4997 NFSLY_RECALL)) == NFSLY_RETONCLOSE) {
4999 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5000 iomode |= NFSLAYOUTIOMODE_READ;
5001 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5002 iomode |= NFSLAYOUTIOMODE_RW;
5003 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5004 0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
5006 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
5012 * Mark the layout to be recalled and with an error.
5013 * Also, disable the dsp from further use.
5016 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
5017 struct nfscllayout *lyp, struct nfsclds *dsp)
5019 struct nfsclrecalllayout *recallp;
5022 printf("DS being disabled, error=%d\n", stat);
5023 /* Set up the return of the layout. */
5024 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
5027 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5028 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5029 iomode |= NFSLAYOUTIOMODE_READ;
5030 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5031 iomode |= NFSLAYOUTIOMODE_RW;
5032 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5033 0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
5034 dp->nfsdi_deviceid, recallp);
5036 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
5039 free(recallp, M_NFSLAYRECALL);
5042 /* And shut the TCP connection down. */
5043 nfscl_cancelreqs(dsp);
5047 * Cancel all RPCs for this "dsp" by closing the connection.
5048 * Also, mark the session as defunct.
5049 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
5050 * cannot be shut down.
5053 nfscl_cancelreqs(struct nfsclds *dsp)
5055 struct __rpc_client *cl;
5056 static int non_event;
5059 if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
5060 dsp->nfsclds_sockp != NULL &&
5061 dsp->nfsclds_sockp->nr_client != NULL) {
5062 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
5063 cl = dsp->nfsclds_sockp->nr_client;
5064 dsp->nfsclds_sess.nfsess_defunct = 1;
5068 * This 1sec sleep is done to reduce the number of reconnect
5069 * attempts made on the DS while it has failed.
5071 tsleep(&non_event, PVFS, "ndscls", hz);
5078 * Dereference a layout.
5081 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
5086 nfsv4_unlock(&lyp->nfsly_lock, 0);
5088 nfsv4_relref(&lyp->nfsly_lock);
5093 * Search for a devinfo by deviceid. If one is found, return it after
5094 * acquiring a reference count on it.
5096 struct nfscldevinfo *
5097 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
5098 struct nfscldevinfo *dip)
5103 dip = nfscl_finddevinfo(clp, deviceid);
5105 dip->nfsdi_refcnt++;
5111 * Dereference a devinfo structure.
5114 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
5117 dip->nfsdi_refcnt--;
5118 if (dip->nfsdi_refcnt == 0)
5119 wakeup(&dip->nfsdi_refcnt);
5123 * Dereference a devinfo structure.
5126 nfscl_reldevinfo(struct nfscldevinfo *dip)
5130 nfscl_reldevinfo_locked(dip);
5135 * Find a layout for this file handle. Return NULL upon failure.
5137 static struct nfscllayout *
5138 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
5140 struct nfscllayout *lyp;
5142 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
5143 if (lyp->nfsly_fhlen == fhlen &&
5144 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
5150 * Find a devinfo for this deviceid. Return NULL upon failure.
5152 static struct nfscldevinfo *
5153 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
5155 struct nfscldevinfo *dip;
5157 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
5158 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5165 * Merge the new file layout list into the main one, maintaining it in
5166 * increasing offset order.
5169 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5170 struct nfsclflayouthead *newfhlp)
5172 struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5174 flp = LIST_FIRST(fhlp);
5176 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5177 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5179 flp = LIST_NEXT(flp, nfsfl_list);
5181 if (prevflp == NULL)
5182 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5184 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5190 * Add this nfscldevinfo to the client, if it doesn't already exist.
5191 * This function consumes the structure pointed at by dip, if not NULL.
5194 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
5195 struct nfsclflayout *flp)
5197 struct nfsclclient *clp;
5198 struct nfscldevinfo *tdip;
5206 free(dip, M_NFSDEVINFO);
5209 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5210 dev = flp->nfsfl_dev;
5212 dev = flp->nfsfl_ffm[ind].dev;
5213 tdip = nfscl_finddevinfo(clp, dev);
5215 tdip->nfsdi_layoutrefs++;
5216 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5217 flp->nfsfl_devp = tdip;
5219 flp->nfsfl_ffm[ind].devp = tdip;
5220 nfscl_reldevinfo_locked(tdip);
5223 free(dip, M_NFSDEVINFO);
5227 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5228 dip->nfsdi_layoutrefs = 1;
5229 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5230 flp->nfsfl_devp = dip;
5232 flp->nfsfl_ffm[ind].devp = dip;
5241 * Free up a layout structure and associated file layout structure(s).
5244 nfscl_freelayout(struct nfscllayout *layp)
5246 struct nfsclflayout *flp, *nflp;
5247 struct nfsclrecalllayout *rp, *nrp;
5249 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5250 LIST_REMOVE(flp, nfsfl_list);
5251 nfscl_freeflayout(flp);
5253 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5254 LIST_REMOVE(flp, nfsfl_list);
5255 nfscl_freeflayout(flp);
5257 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5258 LIST_REMOVE(rp, nfsrecly_list);
5259 free(rp, M_NFSLAYRECALL);
5262 free(layp, M_NFSLAYOUT);
5266 * Free up a file layout structure.
5269 nfscl_freeflayout(struct nfsclflayout *flp)
5273 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) {
5274 for (i = 0; i < flp->nfsfl_fhcnt; i++)
5275 free(flp->nfsfl_fh[i], M_NFSFH);
5276 if (flp->nfsfl_devp != NULL)
5277 flp->nfsfl_devp->nfsdi_layoutrefs--;
5279 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
5280 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) {
5281 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
5282 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
5283 if (flp->nfsfl_ffm[i].devp != NULL)
5284 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--;
5286 free(flp, M_NFSFLAYOUT);
5290 * Free up a file layout devinfo structure.
5293 nfscl_freedevinfo(struct nfscldevinfo *dip)
5296 free(dip, M_NFSDEVINFO);
5300 * Mark any layouts that match as recalled.
5303 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5304 uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op,
5305 char *devid, struct nfsclrecalllayout *recallp)
5307 struct nfsclrecalllayout *rp, *orp;
5309 recallp->nfsrecly_recalltype = recalltype;
5310 recallp->nfsrecly_iomode = iomode;
5311 recallp->nfsrecly_stateseqid = stateseqid;
5312 recallp->nfsrecly_off = off;
5313 recallp->nfsrecly_len = len;
5314 recallp->nfsrecly_stat = stat;
5315 recallp->nfsrecly_op = op;
5317 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID);
5319 * Order the list as file returns first, followed by fsid and any
5320 * returns, both in increasing stateseqid order.
5321 * Note that the seqids wrap around, so 1 is after 0xffffffff.
5322 * (I'm not sure this is correct because I find RFC5661 confusing
5323 * on this, but hopefully it will work ok.)
5326 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5328 if ((recalltype == NFSLAYOUTRETURN_FILE &&
5329 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5330 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5331 (recalltype != NFSLAYOUTRETURN_FILE &&
5332 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5333 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5334 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5339 * Put any error return on all the file returns that will
5342 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE &&
5343 stat != 0 && rp->nfsrecly_stat == 0) {
5344 rp->nfsrecly_stat = stat;
5345 rp->nfsrecly_op = op;
5347 NFSBCOPY(devid, rp->nfsrecly_devid,
5353 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5356 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5358 lyp->nfsly_flags |= NFSLY_RECALL;
5359 wakeup(lyp->nfsly_clp);
5364 * Compare the two seqids for ordering. The trick is that the seqids can
5365 * wrap around from 0xffffffff->0, so check for the cases where one
5366 * has wrapped around.
5367 * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5370 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5373 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5374 /* seqid2 has wrapped around. */
5376 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5377 /* seqid1 has wrapped around. */
5379 if (seqid1 <= seqid2)
5385 * Do a layout return for each of the recalls.
5388 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5389 struct ucred *cred, NFSPROC_T *p)
5391 struct nfsclrecalllayout *rp;
5392 nfsv4stateid_t stateid;
5395 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5396 stateid.seqid = lyp->nfsly_stateid.seqid;
5397 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5398 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5400 layouttype = NFSLAYOUT_FLEXFILE;
5401 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5402 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5403 lyp->nfsly_fhlen, 0, layouttype,
5404 rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5405 rp->nfsrecly_off, rp->nfsrecly_len,
5406 &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op,
5407 rp->nfsrecly_devid);
5412 * Do the layout commit for a file layout.
5415 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5416 struct ucred *cred, NFSPROC_T *p)
5418 struct nfsclflayout *flp;
5420 int error, layouttype;
5422 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5423 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5425 layouttype = NFSLAYOUT_FLEXFILE;
5426 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5427 if (layouttype == NFSLAYOUT_FLEXFILE &&
5428 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
5429 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
5430 /* If not supported, don't bother doing it. */
5432 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5435 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5436 len = flp->nfsfl_end - flp->nfsfl_off;
5437 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5438 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5439 lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5440 layouttype, cred, p, NULL);
5441 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5442 if (error == NFSERR_NOTSUPP) {
5443 /* If not supported, don't bother doing it. */
5445 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5454 * Commit all layouts for a file (vnode).
5457 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5459 struct nfsclclient *clp;
5460 struct nfscllayout *lyp;
5461 struct nfsnode *np = VTONFS(vp);
5463 struct nfsmount *nmp;
5467 if (NFSHASNOLAYOUTCOMMIT(nmp))
5475 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5480 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5481 if (NFSCL_FORCEDISM(mp)) {
5486 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5487 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5489 NFSCL_DEBUG(4, "do layoutcommit2\n");
5490 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5494 nfsv4_relref(&lyp->nfsly_lock);