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))
271 nfscl_filllockowner(p->td_proc, own, F_POSIX);
272 ohp = &dp->nfsdl_owner;
274 /* For NFSv4.1 and this option, use a single open_owner. */
275 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
276 nfscl_filllockowner(NULL, own, F_POSIX);
278 nfscl_filllockowner(p->td_proc, own, F_POSIX);
279 ohp = &clp->nfsc_owner;
281 /* Now, search for an openowner */
282 LIST_FOREACH(owp, ohp, nfsow_list) {
283 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
288 * Create a new open, as required.
290 nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
294 * Now, check the mode on the open and return the appropriate
298 if (nfhp != NULL && dp != NULL && nop == NULL)
299 /* new local open on delegation */
300 *retp = NFSCLOPEN_SETCRED;
302 *retp = NFSCLOPEN_OK;
304 if (op != NULL && (amode & ~(op->nfso_mode))) {
305 op->nfso_mode |= amode;
306 if (retp != NULL && dp == NULL)
307 *retp = NFSCLOPEN_DOOPEN;
311 * Serialize modifications to the open owner for multiple threads
312 * within the same process using a read/write sleep lock.
313 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
314 * by acquiring a shared lock. The close operations still use an
315 * exclusive lock for this case.
318 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) {
320 * Get a shared lock on the OpenOwner, but first
321 * wait for any pending exclusive lock, so that the
322 * exclusive locker gets priority.
324 nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
325 NFSCLSTATEMUTEXPTR, NULL);
326 nfsv4_getref(&owp->nfsow_rwlock, NULL,
327 NFSCLSTATEMUTEXPTR, NULL);
329 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
333 free(nowp, M_NFSCLOWNER);
335 free(nop, M_NFSCLOPEN);
344 * Create a new open, as required.
347 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
348 struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
349 struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
350 struct ucred *cred, int *newonep)
352 struct nfsclowner *owp = *owpp, *nowp;
353 struct nfsclopen *op, *nop;
363 if (owp == NULL && nowp != NULL) {
364 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
365 LIST_INIT(&nowp->nfsow_open);
366 nowp->nfsow_clp = clp;
367 nowp->nfsow_seqid = 0;
368 nowp->nfsow_defunct = 0;
369 nfscl_lockinit(&nowp->nfsow_rwlock);
371 nfsstatsv1.cllocalopenowners++;
372 LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
374 nfsstatsv1.clopenowners++;
375 LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
383 /* If an fhp has been specified, create an Open as well. */
385 /* and look for the correct open, based upon FH */
386 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
387 if (op->nfso_fhlen == fhlen &&
388 !NFSBCMP(op->nfso_fh, fhp, fhlen))
391 if (op == NULL && nop != NULL) {
394 nop->nfso_opencnt = 0;
395 nop->nfso_posixlock = 1;
396 nop->nfso_fhlen = fhlen;
397 NFSBCOPY(fhp, nop->nfso_fh, fhlen);
398 LIST_INIT(&nop->nfso_lock);
399 nop->nfso_stateid.seqid = 0;
400 nop->nfso_stateid.other[0] = 0;
401 nop->nfso_stateid.other[1] = 0;
402 nop->nfso_stateid.other[2] = 0;
403 KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
404 newnfs_copyincred(cred, &nop->nfso_cred);
406 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
407 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
409 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
410 nfsstatsv1.cllocalopens++;
412 nfsstatsv1.clopens++;
414 LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
426 * Called to find/add a delegation to a client.
429 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
430 int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
432 struct nfscldeleg *dp = *dpp, *tdp;
435 * First, if we have received a Read delegation for a file on a
436 * read/write file system, just return it, because they aren't
439 if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
440 (dp->nfsdl_flags & NFSCLDL_READ)) {
441 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
442 free(dp, M_NFSCLDELEG);
447 /* Look for the correct deleg, based upon FH */
449 tdp = nfscl_finddeleg(clp, nfhp, fhlen);
453 return (NFSERR_BADSTATEID);
456 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
457 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
459 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
460 nfsstatsv1.cldelegates++;
464 * Delegation already exists, what do we do if a new one??
467 printf("Deleg already exists!\n");
468 free(dp, M_NFSCLDELEG);
479 * Find a delegation for this file handle. Return NULL upon failure.
481 static struct nfscldeleg *
482 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
484 struct nfscldeleg *dp;
486 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
487 if (dp->nfsdl_fhlen == fhlen &&
488 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
495 * Get a stateid for an I/O operation. First, look for an open and iff
496 * found, return either a lockowner stateid or the open stateid.
497 * If no Open is found, just return error and the special stateid of all zeros.
500 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
501 int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
504 struct nfsclclient *clp;
505 struct nfsclowner *owp;
506 struct nfsclopen *op = NULL, *top;
507 struct nfscllockowner *lp;
508 struct nfscldeleg *dp;
510 struct nfsmount *nmp;
511 u_int8_t own[NFSV4CL_LOCKNAMELEN];
516 * Initially, just set the special stateid of all zeros.
517 * (Don't do this for a DS, since the special stateid can't be used.)
521 stateidp->other[0] = 0;
522 stateidp->other[1] = 0;
523 stateidp->other[2] = 0;
525 if (vnode_vtype(vp) != VREG)
528 nmp = VFSTONFS(vp->v_mount);
530 clp = nfscl_findcl(nmp);
537 * Wait for recovery to complete.
539 while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
540 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
541 PZERO, "nfsrecvr", NULL);
544 * First, look for a delegation.
546 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
547 if (dp->nfsdl_fhlen == fhlen &&
548 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
549 if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
550 (dp->nfsdl_flags & NFSCLDL_WRITE)) {
551 stateidp->seqid = dp->nfsdl_stateid.seqid;
552 stateidp->other[0] = dp->nfsdl_stateid.other[0];
553 stateidp->other[1] = dp->nfsdl_stateid.other[1];
554 stateidp->other[2] = dp->nfsdl_stateid.other[2];
555 if (!(np->n_flag & NDELEGRECALL)) {
556 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
558 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
560 dp->nfsdl_timestamp = NFSD_MONOSEC +
562 dp->nfsdl_rwlock.nfslock_usecnt++;
563 *lckpp = (void *)&dp->nfsdl_rwlock;
574 * If p != NULL, we want to search the parentage tree
575 * for a matching OpenOwner and use that.
577 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
578 nfscl_filllockowner(NULL, own, F_POSIX);
580 nfscl_filllockowner(p->td_proc, own, F_POSIX);
582 error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, own, own,
584 if (error == 0 && lp != NULL && fords == 0) {
585 /* Don't return a lock stateid for a DS. */
587 lp->nfsl_stateid.seqid;
589 lp->nfsl_stateid.other[0];
591 lp->nfsl_stateid.other[1];
593 lp->nfsl_stateid.other[2];
599 /* If not found, just look for any OpenOwner that will work. */
602 owp = LIST_FIRST(&clp->nfsc_owner);
603 while (!done && owp != NULL) {
604 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
605 if (op->nfso_fhlen == fhlen &&
606 !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
607 if (top == NULL && (op->nfso_mode &
608 NFSV4OPEN_ACCESSWRITE) != 0 &&
609 (mode & NFSV4OPEN_ACCESSREAD) != 0)
611 if ((mode & op->nfso_mode) == mode) {
618 owp = LIST_NEXT(owp, nfsow_list);
621 NFSCL_DEBUG(2, "openmode top=%p\n", top);
622 if (top == NULL || NFSHASOPENMODE(nmp)) {
629 * For read aheads or write behinds, use the open cred.
630 * A read ahead or write behind is indicated by p == NULL.
633 newnfs_copycred(&op->nfso_cred, cred);
637 * No lock stateid, so return the open stateid.
639 stateidp->seqid = op->nfso_stateid.seqid;
640 stateidp->other[0] = op->nfso_stateid.other[0];
641 stateidp->other[1] = op->nfso_stateid.other[1];
642 stateidp->other[2] = op->nfso_stateid.other[2];
648 * Search for a matching file, mode and, optionally, lockowner.
651 nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
652 u_int8_t *openown, u_int8_t *lockown, u_int32_t mode,
653 struct nfscllockowner **lpp, struct nfsclopen **opp)
655 struct nfsclowner *owp;
656 struct nfsclopen *op, *rop, *rop2;
657 struct nfscllockowner *lp;
663 * rop will be set to the open to be returned. There are three
664 * variants of this, all for an open of the correct file:
665 * 1 - A match of lockown.
666 * 2 - A match of the openown, when no lockown match exists.
667 * 3 - A match for any open, if no openown or lockown match exists.
668 * Looking for #2 over #3 probably isn't necessary, but since
669 * RFC3530 is vague w.r.t. the relationship between openowners and
670 * lockowners, I think this is the safer way to go.
675 /* Search the client list */
676 owp = LIST_FIRST(ohp);
677 while (owp != NULL && keep_looping != 0) {
678 /* and look for the correct open */
679 op = LIST_FIRST(&owp->nfsow_open);
680 while (op != NULL && keep_looping != 0) {
681 if (op->nfso_fhlen == fhlen &&
682 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
683 && (op->nfso_mode & mode) == mode) {
685 /* Now look for a matching lockowner. */
686 LIST_FOREACH(lp, &op->nfso_lock,
688 if (!NFSBCMP(lp->nfsl_owner,
690 NFSV4CL_LOCKNAMELEN)) {
698 if (rop == NULL && !NFSBCMP(owp->nfsow_owner,
699 openown, NFSV4CL_LOCKNAMELEN)) {
707 op = LIST_NEXT(op, nfso_list);
709 owp = LIST_NEXT(owp, nfsow_list);
720 * Release use of an open owner. Called when open operations are done
721 * with the open owner.
724 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
725 __unused int error, __unused int candelete, int unlocked)
732 if (NFSHASONEOPENOWN(nmp))
733 nfsv4_relref(&owp->nfsow_rwlock);
735 nfscl_lockunlock(&owp->nfsow_rwlock);
737 nfscl_clrelease(owp->nfsow_clp);
742 * Release use of an open structure under an open owner.
745 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
748 struct nfsclclient *clp;
749 struct nfsclowner *owp;
755 if (NFSHASONEOPENOWN(nmp))
756 nfsv4_relref(&owp->nfsow_rwlock);
758 nfscl_lockunlock(&owp->nfsow_rwlock);
759 clp = owp->nfsow_clp;
760 if (error && candelete && op->nfso_opencnt == 0)
761 nfscl_freeopen(op, 0);
762 nfscl_clrelease(clp);
767 * Called to get a clientid structure. It will optionally lock the
768 * client data structures to do the SetClientId/SetClientId_confirm,
769 * but will release that lock and return the clientid with a reference
771 * If the "cred" argument is NULL, a new clientid should not be created.
772 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
774 * The start_renewthread argument tells nfscl_getcl() to start a renew
775 * thread if this creates a new clp.
776 * It always clpp with a reference count on it, unless returning an error.
779 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
780 int start_renewthread, struct nfsclclient **clpp)
782 struct nfsclclient *clp;
783 struct nfsclclient *newclp = NULL;
784 struct nfsmount *nmp;
785 char uuid[HOSTUUIDLEN];
786 int igotlock = 0, error, trystalecnt, clidinusedelay, i;
791 getcredhostuuid(cred, uuid, sizeof uuid);
792 idlen = strlen(uuid);
794 idlen += sizeof (u_int64_t);
796 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
798 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
803 * If a forced dismount is already in progress, don't
804 * allocate a new clientid and get out now. For the case where
805 * clp != NULL, this is a harmless optimization.
807 if (NFSCL_FORCEDISM(mp)) {
810 free(newclp, M_NFSCLCLIENT);
815 if (newclp == NULL) {
820 clp->nfsc_idlen = idlen;
821 LIST_INIT(&clp->nfsc_owner);
822 TAILQ_INIT(&clp->nfsc_deleg);
823 TAILQ_INIT(&clp->nfsc_layout);
824 LIST_INIT(&clp->nfsc_devinfo);
825 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
826 LIST_INIT(&clp->nfsc_deleghash[i]);
827 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
828 LIST_INIT(&clp->nfsc_layouthash[i]);
829 clp->nfsc_flags = NFSCLFLAGS_INITED;
830 clp->nfsc_clientidrev = 1;
831 clp->nfsc_cbident = nfscl_nextcbident();
832 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
834 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
838 if (start_renewthread != 0)
839 nfscl_start_renewthread(clp);
843 free(newclp, M_NFSCLCLIENT);
846 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
847 !NFSCL_FORCEDISM(mp))
848 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
849 NFSCLSTATEMUTEXPTR, mp);
852 * Call nfsv4_lock() with "iwantlock == 0" so that it will
853 * wait for a pending exclusive lock request. This gives the
854 * exclusive lock request priority over this shared lock
856 * An exclusive lock on nfsc_lock is used mainly for server
859 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, mp);
860 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
862 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
864 * Both nfsv4_lock() and nfsv4_getref() know to check
865 * for NFSCL_FORCEDISM() and return without sleeping to
866 * wait for the exclusive lock to be released, since it
867 * might be held by nfscl_umount() and we need to get out
868 * now for that case and not wait until nfscl_umount()
877 * If it needs a clientid, do the setclientid now.
879 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
881 panic("nfscl_clget");
882 if (p == NULL || cred == NULL) {
884 nfsv4_unlock(&clp->nfsc_lock, 0);
889 * If RFC3530 Sec. 14.2.33 is taken literally,
890 * NFSERR_CLIDINUSE will be returned persistently for the
891 * case where a new mount of the same file system is using
892 * a different principal. In practice, NFSERR_CLIDINUSE is
893 * only returned when there is outstanding unexpired state
894 * on the clientid. As such, try for twice the lease
895 * interval, if we know what that is. Otherwise, make a
897 * The case of returning NFSERR_STALECLIENTID is far less
898 * likely, but might occur if there is a significant delay
899 * between doing the SetClientID and SetClientIDConfirm Ops,
900 * such that the server throws away the clientid before
901 * receiving the SetClientIDConfirm.
903 if (clp->nfsc_renew > 0)
904 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
906 clidinusedelay = 120;
909 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
910 if (error == NFSERR_STALECLIENTID ||
911 error == NFSERR_STALEDONTRECOVER ||
912 error == NFSERR_BADSESSION ||
913 error == NFSERR_CLIDINUSE) {
914 (void) nfs_catnap(PZERO, error, "nfs_setcl");
916 } while (((error == NFSERR_STALECLIENTID ||
917 error == NFSERR_BADSESSION ||
918 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
919 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
922 nfsv4_unlock(&clp->nfsc_lock, 0);
926 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
930 nfsv4_unlock(&clp->nfsc_lock, 1);
939 * Get a reference to a clientid and return it, if valid.
942 nfscl_findcl(struct nfsmount *nmp)
944 struct nfsclclient *clp;
947 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
953 * Release the clientid structure. It may be locked or reference counted.
956 nfscl_clrelease(struct nfsclclient *clp)
959 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
960 nfsv4_unlock(&clp->nfsc_lock, 0);
962 nfsv4_relref(&clp->nfsc_lock);
966 * External call for nfscl_clrelease.
969 nfscl_clientrelease(struct nfsclclient *clp)
973 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
974 nfsv4_unlock(&clp->nfsc_lock, 0);
976 nfsv4_relref(&clp->nfsc_lock);
981 * Called when wanting to lock a byte region.
984 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
985 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
986 int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
987 struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
989 struct nfscllockowner *lp;
990 struct nfsclopen *op;
991 struct nfsclclient *clp;
992 struct nfscllockowner *nlp;
993 struct nfscllock *nlop, *otherlop;
994 struct nfscldeleg *dp = NULL, *ldp = NULL;
995 struct nfscllockownerhead *lhp = NULL;
997 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
999 int error = 0, ret, donelocally = 0;
1002 /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
1011 * Might need these, so MALLOC them now, to
1012 * avoid a tsleep() in MALLOC later.
1015 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
1017 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1019 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1020 nlop->nfslo_type = type;
1021 nlop->nfslo_first = off;
1022 if (len == NFS64BITSSET) {
1023 nlop->nfslo_end = NFS64BITSSET;
1025 nlop->nfslo_end = off + len;
1026 if (nlop->nfslo_end <= nlop->nfslo_first)
1027 error = NFSERR_INVAL;
1034 error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
1037 free(nlp, M_NFSCLLOCKOWNER);
1038 free(otherlop, M_NFSCLLOCK);
1039 free(nlop, M_NFSCLLOCK);
1046 openownp = ropenownp;
1048 nfscl_filllockowner(id, own, flags);
1050 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
1051 nfscl_filllockowner(NULL, openown, F_POSIX);
1053 nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1059 * First, search for a delegation. If one exists for this file,
1060 * the lock can be done locally against it, so long as there
1061 * isn't a local lock conflict.
1063 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1064 np->n_fhp->nfh_len);
1065 /* Just sanity check for correct type of delegation */
1066 if (dp != NULL && ((dp->nfsdl_flags &
1067 (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1069 (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1073 /* Now, find an open and maybe a lockowner. */
1074 ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
1075 np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1077 ret = nfscl_getopen(&clp->nfsc_owner,
1078 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1079 ownp, mode, NULL, &op);
1081 lhp = &dp->nfsdl_lock;
1082 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1083 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1084 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1092 * Get the related Open and maybe lockowner.
1094 error = nfscl_getopen(&clp->nfsc_owner,
1095 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1096 ownp, mode, &lp, &op);
1098 lhp = &op->nfso_lock;
1100 if (!error && !recovery)
1101 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1102 np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1105 nfscl_clrelease(clp);
1108 free(nlp, M_NFSCLLOCKOWNER);
1109 free(otherlop, M_NFSCLLOCK);
1110 free(nlop, M_NFSCLLOCK);
1115 * Ok, see if a lockowner exists and create one, as required.
1118 LIST_FOREACH(lp, lhp, nfsl_list) {
1119 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1123 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1125 NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1126 NFSV4CL_LOCKNAMELEN);
1128 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1129 NFSV4CL_LOCKNAMELEN);
1130 nlp->nfsl_seqid = 0;
1131 nlp->nfsl_lockflags = flags;
1132 nlp->nfsl_inprog = NULL;
1133 nfscl_lockinit(&nlp->nfsl_rwlock);
1134 LIST_INIT(&nlp->nfsl_lock);
1136 nlp->nfsl_open = NULL;
1137 nfsstatsv1.cllocallockowners++;
1139 nlp->nfsl_open = op;
1140 nfsstatsv1.cllockowners++;
1142 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1149 * Now, update the byte ranges for locks.
1151 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1157 nfscl_clrelease(clp);
1160 * Serial modifications on the lock owner for multiple threads
1161 * for the same process using a read/write lock.
1164 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1170 free(nlp, M_NFSCLLOCKOWNER);
1172 free(nlop, M_NFSCLLOCK);
1174 free(otherlop, M_NFSCLLOCK);
1181 * Called to unlock a byte range, for LockU.
1184 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1185 __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1186 struct nfsclclient *clp, void *id, int flags,
1187 struct nfscllockowner **lpp, int *dorpcp)
1189 struct nfscllockowner *lp;
1190 struct nfsclowner *owp;
1191 struct nfsclopen *op;
1192 struct nfscllock *nlop, *other_lop = NULL;
1193 struct nfscldeleg *dp;
1195 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1203 * Might need these, so MALLOC them now, to
1204 * avoid a tsleep() in MALLOC later.
1207 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1208 nlop->nfslo_type = F_UNLCK;
1209 nlop->nfslo_first = off;
1210 if (len == NFS64BITSSET) {
1211 nlop->nfslo_end = NFS64BITSSET;
1213 nlop->nfslo_end = off + len;
1214 if (nlop->nfslo_end <= nlop->nfslo_first) {
1215 free(nlop, M_NFSCLLOCK);
1216 return (NFSERR_INVAL);
1221 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1224 nfscl_filllockowner(id, own, flags);
1228 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1229 np->n_fhp->nfh_len);
1232 * First, unlock any local regions on a delegation.
1235 /* Look for this lockowner. */
1236 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1237 if (!NFSBCMP(lp->nfsl_owner, own,
1238 NFSV4CL_LOCKNAMELEN))
1242 /* Use other_lop, so nlop is still available */
1243 (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1247 * Now, find a matching open/lockowner that hasn't already been done,
1248 * as marked by nfsl_inprog.
1252 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1253 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1254 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1255 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1256 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1257 if (lp->nfsl_inprog == NULL &&
1258 !NFSBCMP(lp->nfsl_owner, own,
1259 NFSV4CL_LOCKNAMELEN)) {
1273 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1277 * Serial modifications on the lock owner for multiple
1278 * threads for the same process using a read/write lock.
1280 lp->nfsl_inprog = p;
1281 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1286 free(nlop, M_NFSCLLOCK);
1288 free(other_lop, M_NFSCLLOCK);
1293 * Release all lockowners marked in progess for this process and file.
1296 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1297 void *id, int flags)
1299 struct nfsclowner *owp;
1300 struct nfsclopen *op;
1301 struct nfscllockowner *lp;
1303 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1306 nfscl_filllockowner(id, own, flags);
1308 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1309 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1310 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1311 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1312 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1313 if (lp->nfsl_inprog == p &&
1314 !NFSBCMP(lp->nfsl_owner, own,
1315 NFSV4CL_LOCKNAMELEN)) {
1316 lp->nfsl_inprog = NULL;
1317 nfscl_lockunlock(&lp->nfsl_rwlock);
1323 nfscl_clrelease(clp);
1328 * Called to find out if any bytes within the byte range specified are
1329 * write locked by the calling process. Used to determine if flushing
1330 * is required before a LockU.
1331 * If in doubt, return 1, so the flush will occur.
1334 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1335 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1337 struct nfsclowner *owp;
1338 struct nfscllockowner *lp;
1339 struct nfsclopen *op;
1340 struct nfsclclient *clp;
1341 struct nfscllock *lop;
1342 struct nfscldeleg *dp;
1345 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1349 switch (fl->l_whence) {
1353 * Caller is responsible for adding any necessary offset
1354 * when SEEK_CUR is used.
1359 off = np->n_size + fl->l_start;
1364 if (fl->l_len != 0) {
1365 end = off + fl->l_len;
1372 error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
1375 nfscl_filllockowner(id, own, flags);
1379 * First check the delegation locks.
1381 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1383 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1384 if (!NFSBCMP(lp->nfsl_owner, own,
1385 NFSV4CL_LOCKNAMELEN))
1389 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1390 if (lop->nfslo_first >= end)
1392 if (lop->nfslo_end <= off)
1394 if (lop->nfslo_type == F_WRLCK) {
1395 nfscl_clrelease(clp);
1404 * Now, check state against the server.
1406 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1407 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1408 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1409 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1410 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1411 if (!NFSBCMP(lp->nfsl_owner, own,
1412 NFSV4CL_LOCKNAMELEN))
1416 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1417 if (lop->nfslo_first >= end)
1419 if (lop->nfslo_end <= off)
1421 if (lop->nfslo_type == F_WRLCK) {
1422 nfscl_clrelease(clp);
1431 nfscl_clrelease(clp);
1437 * Release a byte range lock owner structure.
1440 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1442 struct nfsclclient *clp;
1447 clp = lp->nfsl_open->nfso_own->nfsow_clp;
1448 if (error != 0 && candelete &&
1449 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1450 nfscl_freelockowner(lp, 0);
1452 nfscl_lockunlock(&lp->nfsl_rwlock);
1453 nfscl_clrelease(clp);
1458 * Free up an open structure and any associated byte range lock structures.
1461 nfscl_freeopen(struct nfsclopen *op, int local)
1464 LIST_REMOVE(op, nfso_list);
1465 nfscl_freealllocks(&op->nfso_lock, local);
1466 free(op, M_NFSCLOPEN);
1468 nfsstatsv1.cllocalopens--;
1470 nfsstatsv1.clopens--;
1474 * Free up all lock owners and associated locks.
1477 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1479 struct nfscllockowner *lp, *nlp;
1481 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1482 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1484 nfscl_freelockowner(lp, local);
1489 * Called for an Open when NFSERR_EXPIRED is received from the server.
1490 * If there are no byte range locks nor a Share Deny lost, try to do a
1491 * fresh Open. Otherwise, free the open.
1494 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1495 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1497 struct nfscllockowner *lp;
1498 struct nfscldeleg *dp;
1499 int mustdelete = 0, error;
1502 * Look for any byte range lock(s).
1504 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1505 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1512 * If no byte range lock(s) nor a Share deny, try to re-open.
1514 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1515 newnfs_copycred(&op->nfso_cred, cred);
1517 error = nfsrpc_reopen(nmp, op->nfso_fh,
1518 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1522 free(dp, M_NFSCLDELEG);
1527 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1528 op->nfso_fhlen, cred, p, &dp);
1532 * If a byte range lock or Share deny or couldn't re-open, free it.
1535 nfscl_freeopen(op, 0);
1536 return (mustdelete);
1540 * Free up an open owner structure.
1543 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1546 LIST_REMOVE(owp, nfsow_list);
1547 free(owp, M_NFSCLOWNER);
1549 nfsstatsv1.cllocalopenowners--;
1551 nfsstatsv1.clopenowners--;
1555 * Free up a byte range lock owner structure.
1558 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1560 struct nfscllock *lop, *nlop;
1562 LIST_REMOVE(lp, nfsl_list);
1563 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1564 nfscl_freelock(lop, local);
1566 free(lp, M_NFSCLLOCKOWNER);
1568 nfsstatsv1.cllocallockowners--;
1570 nfsstatsv1.cllockowners--;
1574 * Free up a byte range lock structure.
1577 nfscl_freelock(struct nfscllock *lop, int local)
1580 LIST_REMOVE(lop, nfslo_list);
1581 free(lop, M_NFSCLLOCK);
1583 nfsstatsv1.cllocallocks--;
1585 nfsstatsv1.cllocks--;
1589 * Clean out the state related to a delegation.
1592 nfscl_cleandeleg(struct nfscldeleg *dp)
1594 struct nfsclowner *owp, *nowp;
1595 struct nfsclopen *op;
1597 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1598 op = LIST_FIRST(&owp->nfsow_open);
1600 if (LIST_NEXT(op, nfso_list) != NULL)
1601 panic("nfscleandel");
1602 nfscl_freeopen(op, 1);
1604 nfscl_freeopenowner(owp, 1);
1606 nfscl_freealllocks(&dp->nfsdl_lock, 1);
1610 * Free a delegation.
1613 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp)
1616 TAILQ_REMOVE(hdp, dp, nfsdl_list);
1617 LIST_REMOVE(dp, nfsdl_hash);
1618 free(dp, M_NFSCLDELEG);
1619 nfsstatsv1.cldelegates--;
1624 * Free up all state related to this client structure.
1627 nfscl_cleanclient(struct nfsclclient *clp)
1629 struct nfsclowner *owp, *nowp;
1630 struct nfsclopen *op, *nop;
1631 struct nfscllayout *lyp, *nlyp;
1632 struct nfscldevinfo *dip, *ndip;
1634 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1635 nfscl_freelayout(lyp);
1637 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
1638 nfscl_freedevinfo(dip);
1640 /* Now, all the OpenOwners, etc. */
1641 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1642 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1643 nfscl_freeopen(op, 0);
1645 nfscl_freeopenowner(owp, 0);
1650 * Called when an NFSERR_EXPIRED is received from the server.
1653 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1654 struct ucred *cred, NFSPROC_T *p)
1656 struct nfsclowner *owp, *nowp, *towp;
1657 struct nfsclopen *op, *nop, *top;
1658 struct nfscldeleg *dp, *ndp;
1659 int ret, printed = 0;
1662 * First, merge locally issued Opens into the list for the server.
1664 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1665 while (dp != NULL) {
1666 ndp = TAILQ_NEXT(dp, nfsdl_list);
1667 owp = LIST_FIRST(&dp->nfsdl_owner);
1668 while (owp != NULL) {
1669 nowp = LIST_NEXT(owp, nfsow_list);
1670 op = LIST_FIRST(&owp->nfsow_open);
1672 if (LIST_NEXT(op, nfso_list) != NULL)
1674 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1675 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1676 NFSV4CL_LOCKNAMELEN))
1680 /* Merge opens in */
1681 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1682 if (top->nfso_fhlen == op->nfso_fhlen &&
1683 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1685 top->nfso_mode |= op->nfso_mode;
1686 top->nfso_opencnt += op->nfso_opencnt;
1691 /* Just add the open to the owner list */
1692 LIST_REMOVE(op, nfso_list);
1693 op->nfso_own = towp;
1694 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1695 nfsstatsv1.cllocalopens--;
1696 nfsstatsv1.clopens++;
1699 /* Just add the openowner to the client list */
1700 LIST_REMOVE(owp, nfsow_list);
1701 owp->nfsow_clp = clp;
1702 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1703 nfsstatsv1.cllocalopenowners--;
1704 nfsstatsv1.clopenowners++;
1705 nfsstatsv1.cllocalopens--;
1706 nfsstatsv1.clopens++;
1711 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1713 printf("nfsv4 expired locks lost\n");
1715 nfscl_cleandeleg(dp);
1716 nfscl_freedeleg(&clp->nfsc_deleg, dp);
1719 if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1723 * Now, try and reopen against the server.
1725 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1726 owp->nfsow_seqid = 0;
1727 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1728 ret = nfscl_expireopen(clp, op, nmp, cred, p);
1729 if (ret && !printed) {
1731 printf("nfsv4 expired locks lost\n");
1734 if (LIST_EMPTY(&owp->nfsow_open))
1735 nfscl_freeopenowner(owp, 0);
1740 * This function must be called after the process represented by "own" has
1741 * exited. Must be called with CLSTATE lock held.
1744 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1746 struct nfsclowner *owp, *nowp;
1747 struct nfscllockowner *lp, *nlp;
1748 struct nfscldeleg *dp;
1750 /* First, get rid of local locks on delegations. */
1751 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1752 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1753 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1754 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1756 nfscl_freelockowner(lp, 1);
1760 owp = LIST_FIRST(&clp->nfsc_owner);
1761 while (owp != NULL) {
1762 nowp = LIST_NEXT(owp, nfsow_list);
1763 if (!NFSBCMP(owp->nfsow_owner, own,
1764 NFSV4CL_LOCKNAMELEN)) {
1766 * If there are children that haven't closed the
1767 * file descriptors yet, the opens will still be
1768 * here. For that case, let the renew thread clear
1769 * out the OpenOwner later.
1771 if (LIST_EMPTY(&owp->nfsow_open))
1772 nfscl_freeopenowner(owp, 0);
1774 owp->nfsow_defunct = 1;
1781 * Find open/lock owners for processes that have exited.
1784 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1786 struct nfsclowner *owp, *nowp;
1787 struct nfsclopen *op;
1788 struct nfscllockowner *lp, *nlp;
1789 struct nfscldeleg *dp;
1792 * All the pidhash locks must be acquired, since they are sx locks
1793 * and must be acquired before the mutexes. The pid(s) that will
1794 * be used aren't known yet, so all the locks need to be acquired.
1795 * Fortunately, this function is only performed once/sec.
1799 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1800 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1801 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1802 if (LIST_EMPTY(&lp->nfsl_lock))
1803 nfscl_emptylockowner(lp, lhp);
1806 if (nfscl_procdoesntexist(owp->nfsow_owner))
1807 nfscl_cleanup_common(clp, owp->nfsow_owner);
1811 * For the single open_owner case, these lock owners need to be
1812 * checked to see if they still exist separately.
1813 * This is because nfscl_procdoesntexist() never returns true for
1814 * the single open_owner so that the above doesn't ever call
1815 * nfscl_cleanup_common().
1817 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1818 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1819 if (nfscl_procdoesntexist(lp->nfsl_owner))
1820 nfscl_cleanup_common(clp, lp->nfsl_owner);
1824 pidhash_sunlockall();
1828 * Take the empty lock owner and move it to the local lhp list if the
1829 * associated process no longer exists.
1832 nfscl_emptylockowner(struct nfscllockowner *lp,
1833 struct nfscllockownerfhhead *lhp)
1835 struct nfscllockownerfh *lfhp, *mylfhp;
1836 struct nfscllockowner *nlp;
1839 /* If not a Posix lock owner, just return. */
1840 if ((lp->nfsl_lockflags & F_POSIX) == 0)
1846 * First, search to see if this lock owner is already in the list.
1847 * If it is, then the associated process no longer exists.
1849 SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1850 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1851 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
1854 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
1855 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
1856 NFSV4CL_LOCKNAMELEN))
1859 /* If not found, check if process still exists. */
1860 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
1863 /* Move the lock owner over to the local list. */
1864 if (mylfhp == NULL) {
1865 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
1869 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
1870 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
1871 mylfhp->nfslfh_len);
1872 LIST_INIT(&mylfhp->nfslfh_lock);
1873 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
1875 LIST_REMOVE(lp, nfsl_list);
1876 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
1879 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */
1881 * Called from nfs umount to free up the clientid.
1884 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1886 struct nfsclclient *clp;
1891 * For the case that matters, this is the thread that set
1892 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
1893 * done to ensure that any thread executing nfscl_getcl() after
1894 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
1895 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
1896 * explanation, courtesy of Alan Cox.
1897 * What follows is a snippet from Alan Cox's email at:
1898 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
1900 * 1. Set MNTK_UNMOUNTF
1901 * 2. Acquire a standard FreeBSD mutex "m".
1902 * 3. Update some data structures.
1903 * 4. Release mutex "m".
1905 * Then, other threads that acquire "m" after step 4 has occurred will
1906 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to
1907 * step 2 may or may not see MNTK_UNMOUNTF as set.
1910 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
1918 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1919 panic("nfscl umount");
1922 * First, handshake with the nfscl renew thread, to terminate
1925 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1926 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1927 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
1931 * Now, get the exclusive lock on the client state, so
1932 * that no uses of the state are still in progress.
1935 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1936 NFSCLSTATEMUTEXPTR, NULL);
1937 } while (!igotlock);
1941 * Free up all the state. It will expire on the server, but
1942 * maybe we should do a SetClientId/SetClientIdConfirm so
1943 * the server throws it away?
1945 LIST_REMOVE(clp, nfsc_list);
1946 nfscl_delegreturnall(clp, p);
1947 cred = newnfs_getcred();
1948 if (NFSHASNFSV4N(nmp)) {
1949 (void)nfsrpc_destroysession(nmp, clp, cred, p);
1950 (void)nfsrpc_destroyclient(nmp, clp, cred, p);
1952 (void)nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
1953 nfscl_cleanclient(clp);
1956 free(clp, M_NFSCLCLIENT);
1962 * This function is called when a server replies with NFSERR_STALECLIENTID
1963 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
1964 * doing Opens and Locks with reclaim. If these fail, it deletes the
1965 * corresponding state.
1968 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred,
1971 struct nfsclowner *owp, *nowp;
1972 struct nfsclopen *op, *nop;
1973 struct nfscllockowner *lp, *nlp;
1974 struct nfscllock *lop, *nlop;
1975 struct nfscldeleg *dp, *ndp, *tdp;
1976 struct nfsmount *nmp;
1977 struct ucred *tcred;
1978 struct nfsclopenhead extra_open;
1979 struct nfscldeleghead extra_deleg;
1982 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
1983 int i, igotlock = 0, error, trycnt, firstlock;
1984 struct nfscllayout *lyp, *nlyp;
1987 * First, lock the client structure, so everyone else will
1988 * block when trying to use state.
1991 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
1993 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1994 NFSCLSTATEMUTEXPTR, NULL);
1995 } while (!igotlock);
1998 nmp = clp->nfsc_nmp;
2000 panic("nfscl recover");
2003 * For now, just get rid of all layouts. There may be a need
2004 * to do LayoutCommit Ops with reclaim == true later.
2006 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
2007 nfscl_freelayout(lyp);
2008 TAILQ_INIT(&clp->nfsc_layout);
2009 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
2010 LIST_INIT(&clp->nfsc_layouthash[i]);
2015 error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p);
2016 } while ((error == NFSERR_STALECLIENTID ||
2017 error == NFSERR_BADSESSION ||
2018 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2021 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
2022 NFSCLFLAGS_RECVRINPROG);
2023 wakeup(&clp->nfsc_flags);
2024 nfsv4_unlock(&clp->nfsc_lock, 0);
2028 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2029 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2032 * Mark requests already queued on the server, so that they don't
2033 * initiate another recovery cycle. Any requests already in the
2034 * queue that handle state information will have the old stale
2035 * clientid/stateid and will get a NFSERR_STALESTATEID,
2036 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2037 * This will be translated to NFSERR_STALEDONTRECOVER when
2038 * R_DONTRECOVER is set.
2041 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2042 if (rep->r_nmp == nmp)
2043 rep->r_flags |= R_DONTRECOVER;
2048 * If nfsrpc_setclient() returns *retokp == true,
2049 * no more recovery is needed.
2055 * Now, mark all delegations "need reclaim".
2057 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2058 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2060 TAILQ_INIT(&extra_deleg);
2061 LIST_INIT(&extra_open);
2063 * Now traverse the state lists, doing Open and Lock Reclaims.
2065 tcred = newnfs_getcred();
2066 owp = LIST_FIRST(&clp->nfsc_owner);
2067 while (owp != NULL) {
2068 nowp = LIST_NEXT(owp, nfsow_list);
2069 owp->nfsow_seqid = 0;
2070 op = LIST_FIRST(&owp->nfsow_open);
2071 while (op != NULL) {
2072 nop = LIST_NEXT(op, nfso_list);
2073 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2074 /* Search for a delegation to reclaim with the open */
2075 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2076 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2078 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2079 mode = NFSV4OPEN_ACCESSWRITE;
2080 delegtype = NFSV4OPEN_DELEGATEWRITE;
2082 mode = NFSV4OPEN_ACCESSREAD;
2083 delegtype = NFSV4OPEN_DELEGATEREAD;
2085 if ((op->nfso_mode & mode) == mode &&
2086 op->nfso_fhlen == dp->nfsdl_fhlen &&
2087 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2092 delegtype = NFSV4OPEN_DELEGATENONE;
2093 newnfs_copycred(&op->nfso_cred, tcred);
2094 error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2095 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2096 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2099 /* Handle any replied delegation */
2100 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2101 || NFSMNT_RDONLY(nmp->nm_mountp))) {
2102 if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2103 mode = NFSV4OPEN_ACCESSWRITE;
2105 mode = NFSV4OPEN_ACCESSREAD;
2106 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2107 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2109 if ((op->nfso_mode & mode) == mode &&
2110 op->nfso_fhlen == dp->nfsdl_fhlen &&
2111 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2113 dp->nfsdl_stateid = ndp->nfsdl_stateid;
2114 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2115 dp->nfsdl_ace = ndp->nfsdl_ace;
2116 dp->nfsdl_change = ndp->nfsdl_change;
2117 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2118 if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2119 dp->nfsdl_flags |= NFSCLDL_RECALL;
2120 free(ndp, M_NFSCLDELEG);
2127 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2129 /* and reclaim all byte range locks */
2130 lp = LIST_FIRST(&op->nfso_lock);
2131 while (lp != NULL) {
2132 nlp = LIST_NEXT(lp, nfsl_list);
2135 lop = LIST_FIRST(&lp->nfsl_lock);
2136 while (lop != NULL) {
2137 nlop = LIST_NEXT(lop, nfslo_list);
2138 if (lop->nfslo_end == NFS64BITSSET)
2141 len = lop->nfslo_end - lop->nfslo_first;
2142 error = nfscl_trylock(nmp, NULL,
2143 op->nfso_fh, op->nfso_fhlen, lp,
2144 firstlock, 1, lop->nfslo_first, len,
2145 lop->nfslo_type, tcred, p);
2147 nfscl_freelock(lop, 0);
2152 /* If no locks, but a lockowner, just delete it. */
2153 if (LIST_EMPTY(&lp->nfsl_lock))
2154 nfscl_freelockowner(lp, 0);
2159 if (error != 0 && error != NFSERR_BADSESSION)
2160 nfscl_freeopen(op, 0);
2167 * Now, try and get any delegations not yet reclaimed by cobbling
2168 * to-gether an appropriate open.
2171 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2172 while (dp != NULL) {
2173 ndp = TAILQ_NEXT(dp, nfsdl_list);
2174 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2177 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2179 * Name must be as long an largest possible
2180 * NFSV4CL_LOCKNAMELEN. 12 for now.
2182 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2183 NFSV4CL_LOCKNAMELEN);
2184 LIST_INIT(&nowp->nfsow_open);
2185 nowp->nfsow_clp = clp;
2186 nowp->nfsow_seqid = 0;
2187 nowp->nfsow_defunct = 0;
2188 nfscl_lockinit(&nowp->nfsow_rwlock);
2191 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2192 nop = malloc(sizeof (struct nfsclopen) +
2193 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2194 nop->nfso_own = nowp;
2195 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2196 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2197 delegtype = NFSV4OPEN_DELEGATEWRITE;
2199 nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2200 delegtype = NFSV4OPEN_DELEGATEREAD;
2202 nop->nfso_opencnt = 0;
2203 nop->nfso_posixlock = 1;
2204 nop->nfso_fhlen = dp->nfsdl_fhlen;
2205 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2206 LIST_INIT(&nop->nfso_lock);
2207 nop->nfso_stateid.seqid = 0;
2208 nop->nfso_stateid.other[0] = 0;
2209 nop->nfso_stateid.other[1] = 0;
2210 nop->nfso_stateid.other[2] = 0;
2211 newnfs_copycred(&dp->nfsdl_cred, tcred);
2212 newnfs_copyincred(tcred, &nop->nfso_cred);
2214 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2215 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2216 nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2217 delegtype, tcred, p);
2219 if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2220 mode = NFSV4OPEN_ACCESSWRITE;
2222 mode = NFSV4OPEN_ACCESSREAD;
2223 if ((nop->nfso_mode & mode) == mode &&
2224 nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2225 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2227 dp->nfsdl_stateid = tdp->nfsdl_stateid;
2228 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2229 dp->nfsdl_ace = tdp->nfsdl_ace;
2230 dp->nfsdl_change = tdp->nfsdl_change;
2231 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2232 if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2233 dp->nfsdl_flags |= NFSCLDL_RECALL;
2234 free(tdp, M_NFSCLDELEG);
2236 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2242 free(nop, M_NFSCLOPEN);
2244 * Couldn't reclaim it, so throw the state
2247 nfscl_cleandeleg(dp);
2248 nfscl_freedeleg(&clp->nfsc_deleg, dp);
2250 LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2257 * Now, get rid of extra Opens and Delegations.
2259 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2261 newnfs_copycred(&op->nfso_cred, tcred);
2262 error = nfscl_tryclose(op, tcred, nmp, p);
2263 if (error == NFSERR_GRACE)
2264 (void) nfs_catnap(PZERO, error, "nfsexcls");
2265 } while (error == NFSERR_GRACE);
2266 LIST_REMOVE(op, nfso_list);
2267 free(op, M_NFSCLOPEN);
2270 free(nowp, M_NFSCLOWNER);
2272 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2274 newnfs_copycred(&dp->nfsdl_cred, tcred);
2275 error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2276 if (error == NFSERR_GRACE)
2277 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2278 } while (error == NFSERR_GRACE);
2279 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2280 free(dp, M_NFSCLDELEG);
2283 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2284 if (NFSHASNFSV4N(nmp))
2285 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
2289 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2290 wakeup(&clp->nfsc_flags);
2291 nfsv4_unlock(&clp->nfsc_lock, 0);
2298 * This function is called when a server replies with NFSERR_EXPIRED.
2299 * It deletes all state for the client and does a fresh SetClientId/confirm.
2300 * XXX Someday it should post a signal to the process(es) that hold the
2301 * state, so they know that lock state has been lost.
2304 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2306 struct nfsmount *nmp;
2308 int igotlock = 0, error, trycnt;
2311 * If the clientid has gone away or a new SetClientid has already
2312 * been done, just return ok.
2314 if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2318 * First, lock the client structure, so everyone else will
2319 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2320 * that only one thread does the work.
2323 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2325 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2326 NFSCLSTATEMUTEXPTR, NULL);
2327 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2328 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2330 nfsv4_unlock(&clp->nfsc_lock, 0);
2334 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2337 nmp = clp->nfsc_nmp;
2339 panic("nfscl expired");
2340 cred = newnfs_getcred();
2343 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2344 } while ((error == NFSERR_STALECLIENTID ||
2345 error == NFSERR_BADSESSION ||
2346 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2349 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2352 * Expire the state for the client.
2354 nfscl_expireclient(clp, nmp, cred, p);
2356 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2357 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2359 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2360 wakeup(&clp->nfsc_flags);
2361 nfsv4_unlock(&clp->nfsc_lock, 0);
2368 * This function inserts a lock in the list after insert_lop.
2371 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2372 struct nfscllock *insert_lop, int local)
2375 if ((struct nfscllockowner *)insert_lop == lp)
2376 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2378 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2380 nfsstatsv1.cllocallocks++;
2382 nfsstatsv1.cllocks++;
2386 * This function updates the locking for a lock owner and given file. It
2387 * maintains a list of lock ranges ordered on increasing file offset that
2388 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2389 * It always adds new_lop to the list and sometimes uses the one pointed
2391 * Returns 1 if the locks were modified, 0 otherwise.
2394 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2395 struct nfscllock **other_lopp, int local)
2397 struct nfscllock *new_lop = *new_lopp;
2398 struct nfscllock *lop, *tlop, *ilop;
2399 struct nfscllock *other_lop;
2400 int unlock = 0, modified = 0;
2404 * Work down the list until the lock is merged.
2406 if (new_lop->nfslo_type == F_UNLCK)
2408 ilop = (struct nfscllock *)lp;
2409 lop = LIST_FIRST(&lp->nfsl_lock);
2410 while (lop != NULL) {
2412 * Only check locks for this file that aren't before the start of
2415 if (lop->nfslo_end >= new_lop->nfslo_first) {
2416 if (new_lop->nfslo_end < lop->nfslo_first) {
2418 * If the new lock ends before the start of the
2419 * current lock's range, no merge, just insert
2424 if (new_lop->nfslo_type == lop->nfslo_type ||
2425 (new_lop->nfslo_first <= lop->nfslo_first &&
2426 new_lop->nfslo_end >= lop->nfslo_end)) {
2428 * This lock can be absorbed by the new lock/unlock.
2429 * This happens when it covers the entire range
2430 * of the old lock or is contiguous
2431 * with the old lock and is of the same type or an
2434 if (new_lop->nfslo_type != lop->nfslo_type ||
2435 new_lop->nfslo_first != lop->nfslo_first ||
2436 new_lop->nfslo_end != lop->nfslo_end)
2438 if (lop->nfslo_first < new_lop->nfslo_first)
2439 new_lop->nfslo_first = lop->nfslo_first;
2440 if (lop->nfslo_end > new_lop->nfslo_end)
2441 new_lop->nfslo_end = lop->nfslo_end;
2443 lop = LIST_NEXT(lop, nfslo_list);
2444 nfscl_freelock(tlop, local);
2449 * All these cases are for contiguous locks that are not the
2450 * same type, so they can't be merged.
2452 if (new_lop->nfslo_first <= lop->nfslo_first) {
2454 * This case is where the new lock overlaps with the
2455 * first part of the old lock. Move the start of the
2456 * old lock to just past the end of the new lock. The
2457 * new lock will be inserted in front of the old, since
2458 * ilop hasn't been updated. (We are done now.)
2460 if (lop->nfslo_first != new_lop->nfslo_end) {
2461 lop->nfslo_first = new_lop->nfslo_end;
2466 if (new_lop->nfslo_end >= lop->nfslo_end) {
2468 * This case is where the new lock overlaps with the
2469 * end of the old lock's range. Move the old lock's
2470 * end to just before the new lock's first and insert
2471 * the new lock after the old lock.
2472 * Might not be done yet, since the new lock could
2473 * overlap further locks with higher ranges.
2475 if (lop->nfslo_end != new_lop->nfslo_first) {
2476 lop->nfslo_end = new_lop->nfslo_first;
2480 lop = LIST_NEXT(lop, nfslo_list);
2484 * The final case is where the new lock's range is in the
2485 * middle of the current lock's and splits the current lock
2486 * up. Use *other_lopp to handle the second part of the
2487 * split old lock range. (We are done now.)
2488 * For unlock, we use new_lop as other_lop and tmp, since
2489 * other_lop and new_lop are the same for this case.
2490 * We noted the unlock case above, so we don't need
2491 * new_lop->nfslo_type any longer.
2493 tmp = new_lop->nfslo_first;
2495 other_lop = new_lop;
2498 other_lop = *other_lopp;
2501 other_lop->nfslo_first = new_lop->nfslo_end;
2502 other_lop->nfslo_end = lop->nfslo_end;
2503 other_lop->nfslo_type = lop->nfslo_type;
2504 lop->nfslo_end = tmp;
2505 nfscl_insertlock(lp, other_lop, lop, local);
2511 lop = LIST_NEXT(lop, nfslo_list);
2517 * Insert the new lock in the list at the appropriate place.
2520 nfscl_insertlock(lp, new_lop, ilop, local);
2528 * This function must be run as a kernel thread.
2529 * It does Renew Ops and recovery, when required.
2532 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2534 struct nfsclowner *owp, *nowp;
2535 struct nfsclopen *op;
2536 struct nfscllockowner *lp, *nlp;
2537 struct nfscldeleghead dh;
2538 struct nfscldeleg *dp, *ndp;
2541 int error, cbpathdown, islept, igotlock, ret, clearok;
2542 uint32_t recover_done_time = 0;
2544 static time_t prevsec = 0;
2545 struct nfscllockownerfh *lfhp, *nlfhp;
2546 struct nfscllockownerfhhead lfh;
2547 struct nfscllayout *lyp, *nlyp;
2548 struct nfscldevinfo *dip, *ndip;
2549 struct nfscllayouthead rlh;
2550 struct nfsclrecalllayout *recallp;
2551 struct nfsclds *dsp;
2555 cred = newnfs_getcred();
2557 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2558 mp = clp->nfsc_nmp->nm_mountp;
2561 newnfs_setroot(cred);
2563 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2565 * Only allow one full recover within 1/2 of the lease
2566 * duration (nfsc_renew).
2567 * retok is value/result. If passed in set to true,
2568 * it indicates only a CreateSession operation should
2570 * If it is returned true, it indicates that the
2571 * recovery only required a CreateSession.
2574 if (recover_done_time < NFSD_MONOSEC) {
2575 recover_done_time = NFSD_MONOSEC +
2579 NFSCL_DEBUG(1, "Doing recovery, only "
2580 "createsession=%d\n", retok);
2581 nfscl_recover(clp, &retok, cred, p);
2583 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2584 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2585 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2586 clidrev = clp->nfsc_clientidrev;
2587 error = nfsrpc_renew(clp, NULL, cred, p);
2588 if (error == NFSERR_CBPATHDOWN)
2590 else if (error == NFSERR_STALECLIENTID ||
2591 error == NFSERR_BADSESSION) {
2593 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2595 } else if (error == NFSERR_EXPIRED)
2596 (void) nfscl_hasexpired(clp, clidrev, p);
2600 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2601 /* Do renews for any DS sessions. */
2602 NFSLOCKMNT(clp->nfsc_nmp);
2603 /* Skip first entry, since the MDS is handled above. */
2604 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2606 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2607 while (dsp != NULL) {
2608 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2609 dsp->nfsclds_sess.nfsess_defunct == 0) {
2610 dsp->nfsclds_expire = NFSD_MONOSEC +
2612 NFSUNLOCKMNT(clp->nfsc_nmp);
2613 (void)nfsrpc_renew(clp, dsp, cred, p);
2616 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2618 NFSUNLOCKMNT(clp->nfsc_nmp);
2624 /* It's a Total Recall! */
2625 nfscl_totalrecall(clp);
2628 * Now, handle defunct owners.
2630 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2631 if (LIST_EMPTY(&owp->nfsow_open)) {
2632 if (owp->nfsow_defunct != 0)
2633 nfscl_freeopenowner(owp, 0);
2638 * Do the recall on any delegations. To avoid trouble, always
2639 * come back up here after having slept.
2643 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2644 while (dp != NULL) {
2645 ndp = TAILQ_NEXT(dp, nfsdl_list);
2646 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2648 * Wait for outstanding I/O ops to be done.
2650 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2652 nfsv4_unlock(&clp->nfsc_lock, 0);
2655 dp->nfsdl_rwlock.nfslock_lock |=
2657 msleep(&dp->nfsdl_rwlock,
2658 NFSCLSTATEMUTEXPTR, PVFS, "nfscld",
2660 if (NFSCL_FORCEDISM(mp))
2665 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2666 &islept, NFSCLSTATEMUTEXPTR, mp);
2667 if (igotlock == 0 && NFSCL_FORCEDISM(mp))
2673 newnfs_copycred(&dp->nfsdl_cred, cred);
2674 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2677 nfscl_cleandeleg(dp);
2678 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2680 LIST_REMOVE(dp, nfsdl_hash);
2681 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2683 nfsstatsv1.cldelegates--;
2691 * Clear out old delegations, if we are above the high water
2692 * mark. Only clear out ones with no state related to them.
2693 * The tailq list is in LRU order.
2695 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2696 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2697 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2698 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2699 dp->nfsdl_rwlock.nfslock_lock == 0 &&
2700 dp->nfsdl_timestamp < NFSD_MONOSEC &&
2701 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2702 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2704 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2705 op = LIST_FIRST(&owp->nfsow_open);
2712 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2713 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2720 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2721 LIST_REMOVE(dp, nfsdl_hash);
2722 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2724 nfsstatsv1.cldelegates--;
2730 nfsv4_unlock(&clp->nfsc_lock, 0);
2733 * Do the recall on any layouts. To avoid trouble, always
2734 * come back up here after having slept.
2738 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2739 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2741 * Wait for outstanding I/O ops to be done.
2743 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2744 (lyp->nfsly_lock.nfslock_lock &
2745 NFSV4LOCK_LOCK) != 0) {
2746 lyp->nfsly_lock.nfslock_lock |=
2748 msleep(&lyp->nfsly_lock.nfslock_lock,
2749 NFSCLSTATEMUTEXPTR, PVFS, "nfslyp",
2751 if (NFSCL_FORCEDISM(mp))
2755 /* Move the layout to the recall list. */
2756 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2758 LIST_REMOVE(lyp, nfsly_hash);
2759 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2761 /* Handle any layout commits. */
2762 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2763 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2764 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2766 NFSCL_DEBUG(3, "do layoutcommit\n");
2767 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2775 /* Now, look for stale layouts. */
2776 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2777 while (lyp != NULL) {
2778 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2779 if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2780 (lyp->nfsly_flags & NFSLY_RECALL) == 0 &&
2781 lyp->nfsly_lock.nfslock_usecnt == 0 &&
2782 lyp->nfsly_lock.nfslock_lock == 0) {
2783 NFSCL_DEBUG(4, "ret stale lay=%d\n",
2785 recallp = malloc(sizeof(*recallp),
2786 M_NFSLAYRECALL, M_NOWAIT);
2787 if (recallp == NULL)
2789 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2790 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2791 lyp->nfsly_stateid.seqid, 0, 0, NULL,
2798 * Free up any unreferenced device info structures.
2800 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
2801 if (dip->nfsdi_layoutrefs == 0 &&
2802 dip->nfsdi_refcnt == 0) {
2803 NFSCL_DEBUG(4, "freeing devinfo\n");
2804 LIST_REMOVE(dip, nfsdi_list);
2805 nfscl_freedevinfo(dip);
2810 /* Do layout return(s), as required. */
2811 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
2812 TAILQ_REMOVE(&rlh, lyp, nfsly_list);
2813 NFSCL_DEBUG(4, "ret layout\n");
2814 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
2815 nfscl_freelayout(lyp);
2819 * Delegreturn any delegations cleaned out or recalled.
2821 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2822 newnfs_copycred(&dp->nfsdl_cred, cred);
2823 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2824 TAILQ_REMOVE(&dh, dp, nfsdl_list);
2825 free(dp, M_NFSCLDELEG);
2830 * Call nfscl_cleanupkext() once per second to check for
2831 * open/lock owners where the process has exited.
2833 mytime = NFSD_MONOSEC;
2834 if (prevsec != mytime) {
2836 nfscl_cleanupkext(clp, &lfh);
2840 * Do a ReleaseLockOwner for all lock owners where the
2841 * associated process no longer exists, as found by
2842 * nfscl_cleanupkext().
2844 newnfs_setroot(cred);
2845 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
2846 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
2848 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
2849 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
2851 nfscl_freelockowner(lp, 0);
2858 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2859 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
2862 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2863 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2866 wakeup((caddr_t)clp);
2874 * Initiate state recovery. Called when NFSERR_STALECLIENTID,
2875 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
2878 nfscl_initiate_recovery(struct nfsclclient *clp)
2884 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2886 wakeup((caddr_t)clp);
2890 * Dump out the state stuff for debugging.
2893 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
2894 int lockowner, int locks)
2896 struct nfsclclient *clp;
2897 struct nfsclowner *owp;
2898 struct nfsclopen *op;
2899 struct nfscllockowner *lp;
2900 struct nfscllock *lop;
2901 struct nfscldeleg *dp;
2905 printf("nfscl dumpstate NULL clp\n");
2909 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2910 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2911 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2912 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2913 owp->nfsow_owner[0], owp->nfsow_owner[1],
2914 owp->nfsow_owner[2], owp->nfsow_owner[3],
2916 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2918 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2919 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2920 op->nfso_stateid.other[2], op->nfso_opencnt,
2922 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2924 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2925 lp->nfsl_owner[0], lp->nfsl_owner[1],
2926 lp->nfsl_owner[2], lp->nfsl_owner[3],
2928 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2929 lp->nfsl_stateid.other[2]);
2930 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2933 printf("lck typ=%d fst=%ju end=%ju\n",
2934 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2935 (intmax_t)lop->nfslo_end);
2937 printf("lck typ=%d fst=%qd end=%qd\n",
2938 lop->nfslo_type, lop->nfslo_first,
2946 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2947 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2948 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2949 owp->nfsow_owner[0], owp->nfsow_owner[1],
2950 owp->nfsow_owner[2], owp->nfsow_owner[3],
2952 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2954 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2955 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2956 op->nfso_stateid.other[2], op->nfso_opencnt,
2958 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2960 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2961 lp->nfsl_owner[0], lp->nfsl_owner[1],
2962 lp->nfsl_owner[2], lp->nfsl_owner[3],
2964 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2965 lp->nfsl_stateid.other[2]);
2966 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2969 printf("lck typ=%d fst=%ju end=%ju\n",
2970 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2971 (intmax_t)lop->nfslo_end);
2973 printf("lck typ=%d fst=%qd end=%qd\n",
2974 lop->nfslo_type, lop->nfslo_first,
2985 * Check for duplicate open owners and opens.
2986 * (Only used as a diagnostic aid.)
2989 nfscl_dupopen(vnode_t vp, int dupopens)
2991 struct nfsclclient *clp;
2992 struct nfsclowner *owp, *owp2;
2993 struct nfsclopen *op, *op2;
2996 clp = VFSTONFS(vp->v_mount)->nm_clp;
2998 printf("nfscl dupopen NULL clp\n");
3001 nfhp = VTONFS(vp)->n_fhp;
3005 * First, search for duplicate owners.
3006 * These should never happen!
3008 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3009 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3011 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
3012 NFSV4CL_LOCKNAMELEN)) {
3014 printf("DUP OWNER\n");
3015 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3022 * Now, search for duplicate stateids.
3023 * These shouldn't happen, either.
3025 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3026 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3027 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3028 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3030 (op->nfso_stateid.other[0] != 0 ||
3031 op->nfso_stateid.other[1] != 0 ||
3032 op->nfso_stateid.other[2] != 0) &&
3033 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
3034 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
3035 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
3037 printf("DUP STATEID\n");
3038 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3047 * Now search for duplicate opens.
3048 * Duplicate opens for the same owner
3049 * should never occur. Other duplicates are
3050 * possible and are checked for if "dupopens"
3053 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3054 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3055 if (nfhp->nfh_len == op2->nfso_fhlen &&
3056 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3057 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3058 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3059 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3060 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3061 (!NFSBCMP(op->nfso_own->nfsow_owner,
3062 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3064 if (!NFSBCMP(op->nfso_own->nfsow_owner,
3065 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3067 printf("BADDUP OPEN\n");
3070 printf("DUP OPEN\n");
3072 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0,
3085 * During close, find an open that needs to be dereferenced and
3086 * dereference it. If there are no more opens for this file,
3087 * log a message to that effect.
3088 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3089 * on the file's vnode.
3090 * This is the safe way, since it is difficult to identify
3091 * which open the close is for and I/O can be performed after the
3092 * close(2) system call when a file is mmap'd.
3093 * If it returns 0 for success, there will be a referenced
3094 * clp returned via clpp.
3097 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3099 struct nfsclclient *clp;
3100 struct nfsclowner *owp;
3101 struct nfsclopen *op;
3102 struct nfscldeleg *dp;
3106 error = nfscl_getcl(vp->v_mount, NULL, NULL, 1, &clp);
3111 nfhp = VTONFS(vp)->n_fhp;
3115 * First, look for one under a delegation that was locally issued
3116 * and just decrement the opencnt for it. Since all my Opens against
3117 * the server are DENY_NONE, I don't see a problem with hanging
3118 * onto them. (It is much easier to use one of the extant Opens
3119 * that I already have on the server when a Delegation is recalled
3120 * than to do fresh Opens.) Someday, I might need to rethink this, but.
3122 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3124 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3125 op = LIST_FIRST(&owp->nfsow_open);
3128 * Since a delegation is for a file, there
3129 * should never be more than one open for
3132 if (LIST_NEXT(op, nfso_list) != NULL)
3133 panic("nfscdeleg opens");
3134 if (notdecr && op->nfso_opencnt > 0) {
3143 /* Now process the opens against the server. */
3144 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3145 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3146 if (op->nfso_fhlen == nfhp->nfh_len &&
3147 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3149 /* Found an open, decrement cnt if possible */
3150 if (notdecr && op->nfso_opencnt > 0) {
3155 * There are more opens, so just return.
3157 if (op->nfso_opencnt > 0) {
3166 printf("nfscl: never fnd open\n");
3171 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3173 struct nfsclclient *clp;
3174 struct nfsclowner *owp, *nowp;
3175 struct nfsclopen *op;
3176 struct nfscldeleg *dp;
3178 struct nfsclrecalllayout *recallp;
3181 error = nfscl_getcl(vp->v_mount, NULL, NULL, 1, &clp);
3186 nfhp = VTONFS(vp)->n_fhp;
3187 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
3190 * First get rid of the local Open structures, which should be no
3193 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3195 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3196 op = LIST_FIRST(&owp->nfsow_open);
3198 KASSERT((op->nfso_opencnt == 0),
3199 ("nfscl: bad open cnt on deleg"));
3200 nfscl_freeopen(op, 1);
3202 nfscl_freeopenowner(owp, 1);
3206 /* Return any layouts marked return on close. */
3207 nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp);
3209 /* Now process the opens against the server. */
3211 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3212 op = LIST_FIRST(&owp->nfsow_open);
3213 while (op != NULL) {
3214 if (op->nfso_fhlen == nfhp->nfh_len &&
3215 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3217 /* Found an open, close it. */
3219 KASSERT((op->nfso_opencnt == 0),
3220 ("nfscl: bad open cnt on server (%d)",
3224 nfsrpc_doclose(VFSTONFS(vp->v_mount), op, p);
3228 op = LIST_NEXT(op, nfso_list);
3233 * recallp has been set NULL by nfscl_retoncloselayout() if it was
3234 * used by the function, but calling free() with a NULL pointer is ok.
3236 free(recallp, M_NFSLAYRECALL);
3241 * Return all delegations on this client.
3242 * (Must be called with client sleep lock.)
3245 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
3247 struct nfscldeleg *dp, *ndp;
3250 cred = newnfs_getcred();
3251 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3252 nfscl_cleandeleg(dp);
3253 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3254 nfscl_freedeleg(&clp->nfsc_deleg, dp);
3260 * Do a callback RPC.
3263 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3265 int clist, gotseq_ok, i, j, k, op, rcalls;
3267 struct nfsclclient *clp;
3268 struct nfscldeleg *dp = NULL;
3269 int numops, taglen = -1, error = 0, trunc __unused;
3270 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3271 u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3277 nfsattrbit_t attrbits, rattrbits;
3278 nfsv4stateid_t stateid;
3279 uint32_t seqid, slotid = 0, highslot, cachethis __unused;
3280 uint8_t sessionid[NFSX_V4SESSIONID];
3282 struct nfscllayout *lyp;
3283 uint64_t filesid[2], len, off;
3284 int changed, gotone, laytype, recalltype;
3286 struct nfsclrecalllayout *recallp = NULL;
3287 struct nfsclsession *tsep;
3291 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3292 taglen = fxdr_unsigned(int, *tl);
3297 if (taglen <= NFSV4_SMALLSTR)
3300 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3301 error = nfsrv_mtostr(nd, tagstr, taglen);
3303 if (taglen > NFSV4_SMALLSTR)
3304 free(tagstr, M_TEMP);
3308 (void) nfsm_strtom(nd, tag, taglen);
3309 if (taglen > NFSV4_SMALLSTR) {
3310 free(tagstr, M_TEMP);
3312 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3313 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3314 minorvers = fxdr_unsigned(u_int32_t, *tl++);
3315 if (minorvers != NFSV4_MINORVERSION &&
3316 minorvers != NFSV41_MINORVERSION &&
3317 minorvers != NFSV42_MINORVERSION)
3318 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3319 cbident = fxdr_unsigned(u_int32_t, *tl++);
3323 numops = fxdr_unsigned(int, *tl);
3325 * Loop around doing the sub ops.
3327 for (i = 0; i < numops; i++) {
3328 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3329 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3331 op = fxdr_unsigned(int, *tl);
3332 if (op < NFSV4OP_CBGETATTR ||
3333 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3334 (op > NFSV4OP_CBNOTIFYDEVID &&
3335 minorvers == NFSV41_MINORVERSION) ||
3336 (op > NFSV4OP_CBOFFLOAD &&
3337 minorvers == NFSV42_MINORVERSION)) {
3338 nd->nd_repstat = NFSERR_OPILLEGAL;
3339 *repp = nfscl_errmap(nd, minorvers);
3343 nd->nd_procnum = op;
3344 if (op < NFSV42_CBNOPS)
3345 nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
3347 case NFSV4OP_CBGETATTR:
3348 NFSCL_DEBUG(4, "cbgetattr\n");
3351 error = nfsm_getfh(nd, &nfhp);
3353 error = nfsrv_getattrbits(nd, &attrbits,
3355 if (error == 0 && i == 0 &&
3356 minorvers != NFSV4_MINORVERSION)
3357 error = NFSERR_OPNOTINSESS;
3359 mp = nfscl_getmnt(minorvers, sessionid, cbident,
3362 error = NFSERR_SERVERFAULT;
3365 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3366 nfhp->nfh_len, p, &np);
3371 NFSZERO_ATTRBIT(&rattrbits);
3373 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3376 if (NFSISSET_ATTRBIT(&attrbits,
3379 va.va_size = np->n_size;
3383 NFSSETBIT_ATTRBIT(&rattrbits,
3386 if (NFSISSET_ATTRBIT(&attrbits,
3387 NFSATTRBIT_CHANGE)) {
3391 (np->n_flag & NDELEGMOD))
3393 NFSSETBIT_ATTRBIT(&rattrbits,
3397 error = NFSERR_SERVERFAULT;
3405 free(nfhp, M_NFSFH);
3407 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3408 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3411 case NFSV4OP_CBRECALL:
3412 NFSCL_DEBUG(4, "cbrecall\n");
3413 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3415 stateid.seqid = *tl++;
3416 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3418 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3419 trunc = fxdr_unsigned(int, *tl);
3420 error = nfsm_getfh(nd, &nfhp);
3421 if (error == 0 && i == 0 &&
3422 minorvers != NFSV4_MINORVERSION)
3423 error = NFSERR_OPNOTINSESS;
3426 if (minorvers == NFSV4_MINORVERSION)
3427 clp = nfscl_getclnt(cbident);
3429 clp = nfscl_getclntsess(sessionid);
3431 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3433 if (dp != NULL && (dp->nfsdl_flags &
3434 NFSCLDL_DELEGRET) == 0) {
3437 wakeup((caddr_t)clp);
3440 error = NFSERR_SERVERFAULT;
3445 free(nfhp, M_NFSFH);
3447 case NFSV4OP_CBLAYOUTRECALL:
3448 NFSCL_DEBUG(4, "cblayrec\n");
3450 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3451 laytype = fxdr_unsigned(int, *tl++);
3452 iomode = fxdr_unsigned(uint32_t, *tl++);
3453 if (newnfs_true == *tl++)
3457 recalltype = fxdr_unsigned(int, *tl);
3458 NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
3459 laytype, iomode, changed, recalltype);
3460 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3462 if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
3463 laytype != NFSLAYOUT_FLEXFILE)
3464 error = NFSERR_NOMATCHLAYOUT;
3465 else if (recalltype == NFSLAYOUTRETURN_FILE) {
3466 error = nfsm_getfh(nd, &nfhp);
3467 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3470 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3472 off = fxdr_hyper(tl); tl += 2;
3473 len = fxdr_hyper(tl); tl += 2;
3474 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3475 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3476 if (minorvers == NFSV4_MINORVERSION)
3477 error = NFSERR_NOTSUPP;
3479 error = NFSERR_OPNOTINSESS;
3480 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
3481 (uintmax_t)off, (uintmax_t)len,
3482 stateid.seqid, error);
3485 clp = nfscl_getclntsess(sessionid);
3486 NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3488 lyp = nfscl_findlayout(clp,
3491 NFSCL_DEBUG(4, "cblyp=%p\n",
3496 NFSLY_FLEXFILE)) != 0 &&
3497 !NFSBCMP(stateid.other,
3498 lyp->nfsly_stateid.other,
3499 NFSX_STATEIDOTHER)) {
3509 lyp->nfsly_stateid.seqid)
3510 lyp->nfsly_stateid.seqid =
3518 lyp->nfsly_stateid.seqid);
3521 NFSERR_NOMATCHLAYOUT;
3523 error = NFSERR_NOMATCHLAYOUT;
3526 free(nfhp, M_NFSFH);
3527 } else if (recalltype == NFSLAYOUTRETURN_FSID) {
3528 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3529 filesid[0] = fxdr_hyper(tl); tl += 2;
3530 filesid[1] = fxdr_hyper(tl); tl += 2;
3533 clp = nfscl_getclntsess(sessionid);
3535 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3537 if (lyp->nfsly_filesid[0] ==
3539 lyp->nfsly_filesid[1] ==
3546 lyp->nfsly_stateid.seqid,
3556 error = NFSERR_NOMATCHLAYOUT;
3558 error = NFSERR_NOMATCHLAYOUT;
3560 } else if (recalltype == NFSLAYOUTRETURN_ALL) {
3563 clp = nfscl_getclntsess(sessionid);
3565 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3567 error = nfscl_layoutrecall(
3568 recalltype, lyp, iomode, 0,
3570 lyp->nfsly_stateid.seqid,
3571 0, 0, NULL, recallp);
3578 error = NFSERR_NOMATCHLAYOUT;
3580 error = NFSERR_NOMATCHLAYOUT;
3583 error = NFSERR_NOMATCHLAYOUT;
3584 if (recallp != NULL) {
3585 free(recallp, M_NFSLAYRECALL);
3589 case NFSV4OP_CBSEQUENCE:
3590 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3592 bcopy(tl, sessionid, NFSX_V4SESSIONID);
3593 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3594 seqid = fxdr_unsigned(uint32_t, *tl++);
3595 slotid = fxdr_unsigned(uint32_t, *tl++);
3596 highslot = fxdr_unsigned(uint32_t, *tl++);
3598 /* Throw away the referring call stuff. */
3599 clist = fxdr_unsigned(int, *tl);
3600 for (j = 0; j < clist; j++) {
3601 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3603 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3604 rcalls = fxdr_unsigned(int, *tl);
3605 for (k = 0; k < rcalls; k++) {
3606 NFSM_DISSECT(tl, uint32_t *,
3612 clp = nfscl_getclntsess(sessionid);
3614 error = NFSERR_SERVERFAULT;
3616 error = NFSERR_SEQUENCEPOS;
3618 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3619 error = nfsv4_seqsession(seqid, slotid,
3620 highslot, tsep->nfsess_cbslots, &rep,
3621 tsep->nfsess_backslots);
3624 if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
3628 * Handle a reply for a retried
3629 * callback. The reply will be
3630 * re-inserted in the session cache
3631 * by the nfsv4_seqsess_cacherep() call
3634 KASSERT(error == NFSERR_REPLYFROMCACHE,
3635 ("cbsequence: non-NULL rep"));
3636 NFSCL_DEBUG(4, "Got cbretry\n");
3637 m_freem(nd->nd_mreq);
3642 NFSM_BUILD(tl, uint32_t *,
3643 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3644 bcopy(sessionid, tl, NFSX_V4SESSIONID);
3645 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3646 *tl++ = txdr_unsigned(seqid);
3647 *tl++ = txdr_unsigned(slotid);
3648 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3649 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3653 if (i == 0 && minorvers != NFSV4_MINORVERSION)
3654 error = NFSERR_OPNOTINSESS;
3656 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3657 error = NFSERR_NOTSUPP;
3662 if (error == EBADRPC || error == NFSERR_BADXDR) {
3663 nd->nd_repstat = NFSERR_BADXDR;
3665 nd->nd_repstat = error;
3670 if (nd->nd_repstat) {
3671 *repp = nfscl_errmap(nd, minorvers);
3674 *repp = 0; /* NFS4_OK */
3677 if (recallp != NULL)
3678 free(recallp, M_NFSLAYRECALL);
3680 if (error == EBADRPC || error == NFSERR_BADXDR)
3681 nd->nd_repstat = NFSERR_BADXDR;
3683 printf("nfsv4 comperr1=%d\n", error);
3686 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3690 *retopsp = txdr_unsigned(retops);
3692 *nd->nd_errp = nfscl_errmap(nd, minorvers);
3694 if (gotseq_ok != 0) {
3695 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3697 clp = nfscl_getclntsess(sessionid);
3699 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3700 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3711 * Generate the next cbident value. Basically just increment a static value
3712 * and then check that it isn't already in the list, if it has wrapped around.
3715 nfscl_nextcbident(void)
3717 struct nfsclclient *clp;
3719 static u_int32_t nextcbident = 0;
3720 static int haswrapped = 0;
3723 if (nextcbident == 0)
3727 * Search the clientid list for one already using this cbident.
3732 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3733 if (clp->nfsc_cbident == nextcbident) {
3743 return (nextcbident);
3747 * Get the mount point related to a given cbident or session and busy it.
3750 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
3751 struct nfsclclient **clpp)
3753 struct nfsclclient *clp;
3756 struct nfsclsession *tsep;
3760 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3761 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3762 if (minorvers == NFSV4_MINORVERSION) {
3763 if (clp->nfsc_cbident == cbident)
3765 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3773 mp = clp->nfsc_nmp->nm_mountp;
3776 error = vfs_busy(mp, 0);
3785 * Get the clientid pointer related to a given cbident.
3787 static struct nfsclclient *
3788 nfscl_getclnt(u_int32_t cbident)
3790 struct nfsclclient *clp;
3792 LIST_FOREACH(clp, &nfsclhead, nfsc_list)
3793 if (clp->nfsc_cbident == cbident)
3799 * Get the clientid pointer related to a given sessionid.
3801 static struct nfsclclient *
3802 nfscl_getclntsess(uint8_t *sessionid)
3804 struct nfsclclient *clp;
3805 struct nfsclsession *tsep;
3807 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3808 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3809 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3817 * Search for a lock conflict locally on the client. A conflict occurs if
3818 * - not same owner and overlapping byte range and at least one of them is
3819 * a write lock or this is an unlock.
3822 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
3823 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
3824 struct nfscllock **lopp)
3826 struct nfsclowner *owp;
3827 struct nfsclopen *op;
3831 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3835 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3836 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3837 if (op->nfso_fhlen == fhlen &&
3838 !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
3839 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
3850 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3851 u_int8_t *own, struct nfscllock **lopp)
3853 struct nfscllockowner *lp;
3854 struct nfscllock *lop;
3856 LIST_FOREACH(lp, lhp, nfsl_list) {
3857 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
3858 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3859 if (lop->nfslo_first >= nlop->nfslo_end)
3861 if (lop->nfslo_end <= nlop->nfslo_first)
3863 if (lop->nfslo_type == F_WRLCK ||
3864 nlop->nfslo_type == F_WRLCK ||
3865 nlop->nfslo_type == F_UNLCK) {
3868 return (NFSERR_DENIED);
3877 * Check for a local conflicting lock.
3880 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
3881 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
3883 struct nfscllock *lop, nlck;
3884 struct nfscldeleg *dp;
3886 u_int8_t own[NFSV4CL_LOCKNAMELEN];
3889 nlck.nfslo_type = fl->l_type;
3890 nlck.nfslo_first = off;
3891 if (len == NFS64BITSSET) {
3892 nlck.nfslo_end = NFS64BITSSET;
3894 nlck.nfslo_end = off + len;
3895 if (nlck.nfslo_end <= nlck.nfslo_first)
3896 return (NFSERR_INVAL);
3899 nfscl_filllockowner(id, own, flags);
3901 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3902 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
3903 &nlck, own, dp, &lop);
3905 fl->l_whence = SEEK_SET;
3906 fl->l_start = lop->nfslo_first;
3907 if (lop->nfslo_end == NFS64BITSSET)
3910 fl->l_len = lop->nfslo_end - lop->nfslo_first;
3911 fl->l_pid = (pid_t)0;
3912 fl->l_type = lop->nfslo_type;
3913 error = -1; /* no RPC required */
3914 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
3915 fl->l_type == F_RDLCK)) {
3917 * The delegation ensures that there isn't a conflicting
3918 * lock on the server, so return -1 to indicate an RPC
3921 fl->l_type = F_UNLCK;
3929 * Handle Recall of a delegation.
3930 * The clp must be exclusive locked when this is called.
3933 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
3934 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
3935 int called_from_renewthread)
3937 struct nfsclowner *owp, *lowp, *nowp;
3938 struct nfsclopen *op, *lop;
3939 struct nfscllockowner *lp;
3940 struct nfscllock *lckp;
3942 int error = 0, ret, gotvp = 0;
3946 * First, get a vnode for the file. This is needed to do RPCs.
3948 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
3949 dp->nfsdl_fhlen, p, &np);
3952 * File isn't open, so nothing to move over to the
3962 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
3965 * Ok, if it's a write delegation, flush data to the server, so
3966 * that close/open consistency is retained.
3970 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
3971 np->n_flag |= NDELEGRECALL;
3973 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
3975 np->n_flag &= ~NDELEGRECALL;
3977 NFSINVALATTRCACHE(np);
3979 if (ret == EIO && called_from_renewthread != 0) {
3981 * If the flush failed with EIO for the renew thread,
3982 * return now, so that the dirty buffer will be flushed
3991 * Now, for each openowner with opens issued locally, move them
3992 * over to state against the server.
3994 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
3995 lop = LIST_FIRST(&lowp->nfsow_open);
3997 if (LIST_NEXT(lop, nfso_list) != NULL)
3998 panic("nfsdlg mult opens");
4000 * Look for the same openowner against the server.
4002 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
4003 if (!NFSBCMP(lowp->nfsow_owner,
4004 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
4005 newnfs_copycred(&dp->nfsdl_cred, cred);
4006 ret = nfscl_moveopen(vp, clp, nmp, lop,
4008 if (ret == NFSERR_STALECLIENTID ||
4009 ret == NFSERR_STALEDONTRECOVER ||
4010 ret == NFSERR_BADSESSION) {
4016 nfscl_freeopen(lop, 1);
4025 * If no openowner found, create one and get an open
4030 sizeof (struct nfsclowner), M_NFSCLOWNER,
4032 nfscl_newopen(clp, NULL, &owp, &nowp, &op,
4033 NULL, lowp->nfsow_owner, dp->nfsdl_fh,
4034 dp->nfsdl_fhlen, NULL, NULL);
4035 newnfs_copycred(&dp->nfsdl_cred, cred);
4036 ret = nfscl_moveopen(vp, clp, nmp, lop,
4039 nfscl_freeopenowner(owp, 0);
4040 if (ret == NFSERR_STALECLIENTID ||
4041 ret == NFSERR_STALEDONTRECOVER ||
4042 ret == NFSERR_BADSESSION) {
4048 nfscl_freeopen(lop, 1);
4058 * Now, get byte range locks for any locks done locally.
4060 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4061 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
4062 newnfs_copycred(&dp->nfsdl_cred, cred);
4063 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
4064 if (ret == NFSERR_STALESTATEID ||
4065 ret == NFSERR_STALEDONTRECOVER ||
4066 ret == NFSERR_STALECLIENTID ||
4067 ret == NFSERR_BADSESSION) {
4082 * Move a locally issued open over to an owner on the state list.
4083 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
4084 * returns with it unlocked.
4087 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4088 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
4089 struct ucred *cred, NFSPROC_T *p)
4091 struct nfsclopen *op, *nop;
4092 struct nfscldeleg *ndp;
4094 int error = 0, newone;
4097 * First, look for an appropriate open, If found, just increment the
4100 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4101 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4102 op->nfso_fhlen == lop->nfso_fhlen &&
4103 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4104 op->nfso_opencnt += lop->nfso_opencnt;
4105 nfscl_freeopen(lop, 1);
4110 /* No appropriate open, so we have to do one against the server. */
4112 nop = malloc(sizeof (struct nfsclopen) +
4113 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4115 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4116 lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
4118 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4119 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4120 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4123 nfscl_freeopen(op, 0);
4125 op->nfso_mode |= lop->nfso_mode;
4126 op->nfso_opencnt += lop->nfso_opencnt;
4127 nfscl_freeopen(lop, 1);
4130 free(nop, M_NFSCLOPEN);
4133 * What should I do with the returned delegation, since the
4134 * delegation is being recalled? For now, just printf and
4137 printf("Moveopen returned deleg\n");
4138 free(ndp, M_NFSCLDELEG);
4144 * Recall all delegations on this client.
4147 nfscl_totalrecall(struct nfsclclient *clp)
4149 struct nfscldeleg *dp;
4151 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4152 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4153 dp->nfsdl_flags |= NFSCLDL_RECALL;
4158 * Relock byte ranges. Called for delegation recall and state expiry.
4161 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4162 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4165 struct nfscllockowner *nlp;
4168 int error, newone, donelocally;
4170 off = lop->nfslo_first;
4171 len = lop->nfslo_end - lop->nfslo_first;
4172 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4173 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4174 lp->nfsl_openowner, &nlp, &newone, &donelocally);
4175 if (error || donelocally)
4177 nfhp = VTONFS(vp)->n_fhp;
4178 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4179 nfhp->nfh_len, nlp, newone, 0, off,
4180 len, lop->nfslo_type, cred, p);
4182 nfscl_freelockowner(nlp, 0);
4187 * Called to re-open a file. Basically get a vnode for the file handle
4188 * and then call nfsrpc_openrpc() to do the rest.
4191 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4192 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4193 struct ucred *cred, NFSPROC_T *p)
4199 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4203 if (np->n_v4 != NULL) {
4204 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4205 np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4206 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4216 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4217 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4221 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4222 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4223 u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4224 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4229 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4230 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4232 if (error == NFSERR_DELAY)
4233 (void) nfs_catnap(PZERO, error, "nfstryop");
4234 } while (error == NFSERR_DELAY);
4235 if (error == EAUTH || error == EACCES) {
4236 /* Try again using system credentials */
4237 newnfs_setroot(cred);
4239 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4240 newfhlen, mode, op, name, namelen, ndpp, reclaim,
4241 delegtype, cred, p, 1, 0);
4242 if (error == NFSERR_DELAY)
4243 (void) nfs_catnap(PZERO, error, "nfstryop");
4244 } while (error == NFSERR_DELAY);
4250 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4251 * NFSERR_DELAY. Also, retry with system credentials, if the provided
4255 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4256 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4257 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4259 struct nfsrv_descript nfsd, *nd = &nfsd;
4263 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4264 reclaim, off, len, type, cred, p, 0);
4265 if (!error && nd->nd_repstat == NFSERR_DELAY)
4266 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4268 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4270 error = nd->nd_repstat;
4271 if (error == EAUTH || error == EACCES) {
4272 /* Try again using root credentials */
4273 newnfs_setroot(cred);
4275 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4276 newone, reclaim, off, len, type, cred, p, 1);
4277 if (!error && nd->nd_repstat == NFSERR_DELAY)
4278 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4280 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4282 error = nd->nd_repstat;
4288 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4289 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4293 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4294 struct nfsmount *nmp, NFSPROC_T *p)
4299 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4300 if (error == NFSERR_DELAY)
4301 (void) nfs_catnap(PZERO, error, "nfstrydp");
4302 } while (error == NFSERR_DELAY);
4303 if (error == EAUTH || error == EACCES) {
4304 /* Try again using system credentials */
4305 newnfs_setroot(cred);
4307 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4308 if (error == NFSERR_DELAY)
4309 (void) nfs_catnap(PZERO, error, "nfstrydp");
4310 } while (error == NFSERR_DELAY);
4316 * Try a close against the server. Just call nfsrpc_closerpc(),
4317 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4321 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4322 struct nfsmount *nmp, NFSPROC_T *p)
4324 struct nfsrv_descript nfsd, *nd = &nfsd;
4328 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4329 if (error == NFSERR_DELAY)
4330 (void) nfs_catnap(PZERO, error, "nfstrycl");
4331 } while (error == NFSERR_DELAY);
4332 if (error == EAUTH || error == EACCES) {
4333 /* Try again using system credentials */
4334 newnfs_setroot(cred);
4336 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4337 if (error == NFSERR_DELAY)
4338 (void) nfs_catnap(PZERO, error, "nfstrycl");
4339 } while (error == NFSERR_DELAY);
4345 * Decide if a delegation on a file permits close without flushing writes
4346 * to the server. This might be a big performance win in some environments.
4347 * (Not useful until the client does caching on local stable storage.)
4350 nfscl_mustflush(vnode_t vp)
4352 struct nfsclclient *clp;
4353 struct nfscldeleg *dp;
4355 struct nfsmount *nmp;
4358 nmp = VFSTONFS(vp->v_mount);
4359 if (!NFSHASNFSV4(nmp))
4362 clp = nfscl_findcl(nmp);
4367 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4368 if (dp != NULL && (dp->nfsdl_flags &
4369 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4371 (dp->nfsdl_sizelimit >= np->n_size ||
4372 !NFSHASSTRICT3530(nmp))) {
4381 * See if a (write) delegation exists for this file.
4384 nfscl_nodeleg(vnode_t vp, int writedeleg)
4386 struct nfsclclient *clp;
4387 struct nfscldeleg *dp;
4389 struct nfsmount *nmp;
4392 nmp = VFSTONFS(vp->v_mount);
4393 if (!NFSHASNFSV4(nmp))
4396 clp = nfscl_findcl(nmp);
4401 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4403 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4404 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4414 * Look for an associated delegation that should be DelegReturned.
4417 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4419 struct nfsclclient *clp;
4420 struct nfscldeleg *dp;
4421 struct nfsclowner *owp;
4422 struct nfscllockowner *lp;
4423 struct nfsmount *nmp;
4426 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4428 nmp = VFSTONFS(vp->v_mount);
4432 * Loop around waiting for:
4433 * - outstanding I/O operations on delegations to complete
4434 * - for a delegation on vp that has state, lock the client and
4436 * - return delegation with no state
4439 clp = nfscl_findcl(nmp);
4444 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4445 np->n_fhp->nfh_len);
4448 * Wait for outstanding I/O ops to be done.
4450 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4452 nfsv4_unlock(&clp->nfsc_lock, 0);
4455 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4456 (void) nfsmsleep(&dp->nfsdl_rwlock,
4457 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4461 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4462 if (!LIST_EMPTY(&owp->nfsow_open)) {
4468 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4469 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4475 if (needsrecall && !triedrecall) {
4476 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4479 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4480 &islept, NFSCLSTATEMUTEXPTR, NULL);
4487 cred = newnfs_getcred();
4488 newnfs_copycred(&dp->nfsdl_cred, cred);
4489 (void) nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0);
4493 nfsv4_unlock(&clp->nfsc_lock, 0);
4497 *stp = dp->nfsdl_stateid;
4499 nfscl_cleandeleg(dp);
4500 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4503 nfsv4_unlock(&clp->nfsc_lock, 0);
4510 * Look for associated delegation(s) that should be DelegReturned.
4513 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4514 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4516 struct nfsclclient *clp;
4517 struct nfscldeleg *dp;
4518 struct nfsclowner *owp;
4519 struct nfscllockowner *lp;
4520 struct nfsmount *nmp;
4523 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4525 nmp = VFSTONFS(fvp->v_mount);
4530 * Loop around waiting for:
4531 * - outstanding I/O operations on delegations to complete
4532 * - for a delegation on fvp that has state, lock the client and
4534 * - return delegation(s) with no state.
4537 clp = nfscl_findcl(nmp);
4543 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4544 np->n_fhp->nfh_len);
4545 if (dp != NULL && *gotfdp == 0) {
4547 * Wait for outstanding I/O ops to be done.
4549 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4551 nfsv4_unlock(&clp->nfsc_lock, 0);
4554 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4555 (void) nfsmsleep(&dp->nfsdl_rwlock,
4556 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4560 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4561 if (!LIST_EMPTY(&owp->nfsow_open)) {
4567 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4568 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4574 if (needsrecall && !triedrecall) {
4575 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4578 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4579 &islept, NFSCLSTATEMUTEXPTR, NULL);
4586 cred = newnfs_getcred();
4587 newnfs_copycred(&dp->nfsdl_cred, cred);
4588 (void) nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0);
4592 nfsv4_unlock(&clp->nfsc_lock, 0);
4596 *fstp = dp->nfsdl_stateid;
4599 nfscl_cleandeleg(dp);
4600 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4603 nfsv4_unlock(&clp->nfsc_lock, 0);
4608 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4609 np->n_fhp->nfh_len);
4610 if (dp != NULL && *gottdp == 0) {
4612 * Wait for outstanding I/O ops to be done.
4614 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4615 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4616 (void) nfsmsleep(&dp->nfsdl_rwlock,
4617 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4620 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4621 if (!LIST_EMPTY(&owp->nfsow_open)) {
4626 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4627 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4632 *tstp = dp->nfsdl_stateid;
4635 nfscl_cleandeleg(dp);
4636 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4645 * Get a reference on the clientid associated with the mount point.
4646 * Return 1 if success, 0 otherwise.
4649 nfscl_getref(struct nfsmount *nmp)
4651 struct nfsclclient *clp;
4654 clp = nfscl_findcl(nmp);
4659 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
4665 * Release a reference on a clientid acquired with the above call.
4668 nfscl_relref(struct nfsmount *nmp)
4670 struct nfsclclient *clp;
4673 clp = nfscl_findcl(nmp);
4678 nfsv4_relref(&clp->nfsc_lock);
4683 * Save the size attribute in the delegation, since the nfsnode
4687 nfscl_reclaimnode(vnode_t vp)
4689 struct nfsclclient *clp;
4690 struct nfscldeleg *dp;
4691 struct nfsnode *np = VTONFS(vp);
4692 struct nfsmount *nmp;
4694 nmp = VFSTONFS(vp->v_mount);
4695 if (!NFSHASNFSV4(nmp))
4698 clp = nfscl_findcl(nmp);
4703 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4704 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4705 dp->nfsdl_size = np->n_size;
4710 * Get the saved size attribute in the delegation, since it is a
4711 * newly allocated nfsnode.
4714 nfscl_newnode(vnode_t vp)
4716 struct nfsclclient *clp;
4717 struct nfscldeleg *dp;
4718 struct nfsnode *np = VTONFS(vp);
4719 struct nfsmount *nmp;
4721 nmp = VFSTONFS(vp->v_mount);
4722 if (!NFSHASNFSV4(nmp))
4725 clp = nfscl_findcl(nmp);
4730 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4731 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4732 np->n_size = dp->nfsdl_size;
4737 * If there is a valid write delegation for this file, set the modtime
4738 * to the local clock time.
4741 nfscl_delegmodtime(vnode_t vp)
4743 struct nfsclclient *clp;
4744 struct nfscldeleg *dp;
4745 struct nfsnode *np = VTONFS(vp);
4746 struct nfsmount *nmp;
4748 nmp = VFSTONFS(vp->v_mount);
4749 if (!NFSHASNFSV4(nmp))
4752 clp = nfscl_findcl(nmp);
4757 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4758 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4759 nanotime(&dp->nfsdl_modtime);
4760 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4766 * If there is a valid write delegation for this file with a modtime set,
4767 * put that modtime in mtime.
4770 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4772 struct nfsclclient *clp;
4773 struct nfscldeleg *dp;
4774 struct nfsnode *np = VTONFS(vp);
4775 struct nfsmount *nmp;
4777 nmp = VFSTONFS(vp->v_mount);
4778 if (!NFSHASNFSV4(nmp))
4781 clp = nfscl_findcl(nmp);
4786 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4788 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4789 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4790 *mtime = dp->nfsdl_modtime;
4795 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
4797 short *defaulterrp, *errp;
4799 if (!nd->nd_repstat)
4801 if (nd->nd_procnum == NFSPROC_NOOP)
4802 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4803 if (nd->nd_repstat == EBADRPC)
4804 return (txdr_unsigned(NFSERR_BADXDR));
4805 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4806 nd->nd_repstat == NFSERR_OPILLEGAL)
4807 return (txdr_unsigned(nd->nd_repstat));
4808 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
4809 minorvers > NFSV4_MINORVERSION) {
4810 /* NFSv4.n error. */
4811 return (txdr_unsigned(nd->nd_repstat));
4813 if (nd->nd_procnum < NFSV4OP_CBNOPS)
4814 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4816 return (txdr_unsigned(nd->nd_repstat));
4818 if (*errp == (short)nd->nd_repstat)
4819 return (txdr_unsigned(nd->nd_repstat));
4820 return (txdr_unsigned(*defaulterrp));
4824 * Called to find/add a layout to a client.
4825 * This function returns the layout with a refcnt (shared lock) upon
4826 * success (returns 0) or with no lock/refcnt on the layout when an
4827 * error is returned.
4828 * If a layout is passed in via lypp, it is locked (exclusively locked).
4831 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4832 nfsv4stateid_t *stateidp, int layouttype, int retonclose,
4833 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
4834 struct ucred *cred, NFSPROC_T *p)
4836 struct nfsclclient *clp;
4837 struct nfscllayout *lyp, *tlyp;
4838 struct nfsclflayout *flp;
4839 struct nfsnode *np = VTONFS(vp);
4841 int layout_passed_in;
4843 mp = nmp->nm_mountp;
4844 layout_passed_in = 1;
4848 layout_passed_in = 0;
4849 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
4856 if (layout_passed_in != 0)
4857 nfsv4_unlock(&lyp->nfsly_lock, 0);
4860 free(tlyp, M_NFSLAYOUT);
4865 * Although no lyp was passed in, another thread might have
4866 * allocated one. If one is found, just increment it's ref
4867 * count and return it.
4869 lyp = nfscl_findlayout(clp, fhp, fhlen);
4873 lyp->nfsly_stateid.seqid = stateidp->seqid;
4874 lyp->nfsly_stateid.other[0] = stateidp->other[0];
4875 lyp->nfsly_stateid.other[1] = stateidp->other[1];
4876 lyp->nfsly_stateid.other[2] = stateidp->other[2];
4877 lyp->nfsly_lastbyte = 0;
4878 LIST_INIT(&lyp->nfsly_flayread);
4879 LIST_INIT(&lyp->nfsly_flayrw);
4880 LIST_INIT(&lyp->nfsly_recall);
4881 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
4882 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
4883 lyp->nfsly_clp = clp;
4884 if (layouttype == NFSLAYOUT_FLEXFILE)
4885 lyp->nfsly_flags = NFSLY_FLEXFILE;
4887 lyp->nfsly_flags = NFSLY_FILES;
4888 if (retonclose != 0)
4889 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4890 lyp->nfsly_fhlen = fhlen;
4891 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
4892 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4893 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
4895 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4898 if (retonclose != 0)
4899 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4900 if (stateidp->seqid > lyp->nfsly_stateid.seqid)
4901 lyp->nfsly_stateid.seqid = stateidp->seqid;
4902 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4903 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4904 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4906 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
4907 if (NFSCL_FORCEDISM(mp)) {
4910 free(tlyp, M_NFSLAYOUT);
4914 } else if (stateidp->seqid > lyp->nfsly_stateid.seqid)
4915 lyp->nfsly_stateid.seqid = stateidp->seqid;
4917 /* Merge the new list of File Layouts into the list. */
4918 flp = LIST_FIRST(fhlp);
4920 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
4921 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
4923 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
4925 if (layout_passed_in != 0)
4926 nfsv4_unlock(&lyp->nfsly_lock, 1);
4929 free(tlyp, M_NFSLAYOUT);
4934 * Search for a layout by MDS file handle.
4935 * If one is found, it is returned with a refcnt (shared lock) iff
4936 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
4939 struct nfscllayout *
4940 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
4941 uint64_t off, struct nfsclflayout **retflpp, int *recalledp)
4943 struct nfscllayout *lyp;
4945 int error, igotlock;
4947 mp = clp->nfsc_nmp->nm_mountp;
4951 lyp = nfscl_findlayout(clp, fhp, fhlen);
4953 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
4954 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4955 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4956 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4957 error = nfscl_findlayoutforio(lyp, off,
4958 NFSV4OPEN_ACCESSREAD, retflpp);
4960 nfsv4_getref(&lyp->nfsly_lock, NULL,
4961 NFSCLSTATEMUTEXPTR, mp);
4964 igotlock = nfsv4_lock(&lyp->nfsly_lock,
4965 1, NULL, NFSCLSTATEMUTEXPTR, mp);
4966 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
4969 if (NFSCL_FORCEDISM(mp)) {
4983 * Search for a layout by MDS file handle. If one is found, mark in to be
4984 * recalled, if it already marked "return on close".
4987 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
4988 int fhlen, struct nfsclrecalllayout **recallpp)
4990 struct nfscllayout *lyp;
4993 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) ||
4994 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
4995 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
4997 lyp = nfscl_findlayout(clp, fhp, fhlen);
4998 if (lyp != NULL && (lyp->nfsly_flags & (NFSLY_RETONCLOSE |
4999 NFSLY_RECALL)) == NFSLY_RETONCLOSE) {
5001 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5002 iomode |= NFSLAYOUTIOMODE_READ;
5003 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5004 iomode |= NFSLAYOUTIOMODE_RW;
5005 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5006 0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
5008 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
5014 * Mark the layout to be recalled and with an error.
5015 * Also, disable the dsp from further use.
5018 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
5019 struct nfscllayout *lyp, struct nfsclds *dsp)
5021 struct nfsclrecalllayout *recallp;
5024 printf("DS being disabled, error=%d\n", stat);
5025 /* Set up the return of the layout. */
5026 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
5029 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5030 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5031 iomode |= NFSLAYOUTIOMODE_READ;
5032 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5033 iomode |= NFSLAYOUTIOMODE_RW;
5034 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5035 0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
5036 dp->nfsdi_deviceid, recallp);
5038 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
5041 free(recallp, M_NFSLAYRECALL);
5044 /* And shut the TCP connection down. */
5045 nfscl_cancelreqs(dsp);
5049 * Cancel all RPCs for this "dsp" by closing the connection.
5050 * Also, mark the session as defunct.
5051 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
5052 * cannot be shut down.
5055 nfscl_cancelreqs(struct nfsclds *dsp)
5057 struct __rpc_client *cl;
5058 static int non_event;
5061 if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
5062 dsp->nfsclds_sockp != NULL &&
5063 dsp->nfsclds_sockp->nr_client != NULL) {
5064 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
5065 cl = dsp->nfsclds_sockp->nr_client;
5066 dsp->nfsclds_sess.nfsess_defunct = 1;
5070 * This 1sec sleep is done to reduce the number of reconnect
5071 * attempts made on the DS while it has failed.
5073 tsleep(&non_event, PVFS, "ndscls", hz);
5080 * Dereference a layout.
5083 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
5088 nfsv4_unlock(&lyp->nfsly_lock, 0);
5090 nfsv4_relref(&lyp->nfsly_lock);
5095 * Search for a devinfo by deviceid. If one is found, return it after
5096 * acquiring a reference count on it.
5098 struct nfscldevinfo *
5099 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
5100 struct nfscldevinfo *dip)
5105 dip = nfscl_finddevinfo(clp, deviceid);
5107 dip->nfsdi_refcnt++;
5113 * Dereference a devinfo structure.
5116 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
5119 dip->nfsdi_refcnt--;
5120 if (dip->nfsdi_refcnt == 0)
5121 wakeup(&dip->nfsdi_refcnt);
5125 * Dereference a devinfo structure.
5128 nfscl_reldevinfo(struct nfscldevinfo *dip)
5132 nfscl_reldevinfo_locked(dip);
5137 * Find a layout for this file handle. Return NULL upon failure.
5139 static struct nfscllayout *
5140 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
5142 struct nfscllayout *lyp;
5144 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
5145 if (lyp->nfsly_fhlen == fhlen &&
5146 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
5152 * Find a devinfo for this deviceid. Return NULL upon failure.
5154 static struct nfscldevinfo *
5155 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
5157 struct nfscldevinfo *dip;
5159 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
5160 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5167 * Merge the new file layout list into the main one, maintaining it in
5168 * increasing offset order.
5171 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5172 struct nfsclflayouthead *newfhlp)
5174 struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5176 flp = LIST_FIRST(fhlp);
5178 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5179 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5181 flp = LIST_NEXT(flp, nfsfl_list);
5183 if (prevflp == NULL)
5184 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5186 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5192 * Add this nfscldevinfo to the client, if it doesn't already exist.
5193 * This function consumes the structure pointed at by dip, if not NULL.
5196 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
5197 struct nfsclflayout *flp)
5199 struct nfsclclient *clp;
5200 struct nfscldevinfo *tdip;
5208 free(dip, M_NFSDEVINFO);
5211 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5212 dev = flp->nfsfl_dev;
5214 dev = flp->nfsfl_ffm[ind].dev;
5215 tdip = nfscl_finddevinfo(clp, dev);
5217 tdip->nfsdi_layoutrefs++;
5218 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5219 flp->nfsfl_devp = tdip;
5221 flp->nfsfl_ffm[ind].devp = tdip;
5222 nfscl_reldevinfo_locked(tdip);
5225 free(dip, M_NFSDEVINFO);
5229 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5230 dip->nfsdi_layoutrefs = 1;
5231 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5232 flp->nfsfl_devp = dip;
5234 flp->nfsfl_ffm[ind].devp = dip;
5243 * Free up a layout structure and associated file layout structure(s).
5246 nfscl_freelayout(struct nfscllayout *layp)
5248 struct nfsclflayout *flp, *nflp;
5249 struct nfsclrecalllayout *rp, *nrp;
5251 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5252 LIST_REMOVE(flp, nfsfl_list);
5253 nfscl_freeflayout(flp);
5255 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5256 LIST_REMOVE(flp, nfsfl_list);
5257 nfscl_freeflayout(flp);
5259 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5260 LIST_REMOVE(rp, nfsrecly_list);
5261 free(rp, M_NFSLAYRECALL);
5264 free(layp, M_NFSLAYOUT);
5268 * Free up a file layout structure.
5271 nfscl_freeflayout(struct nfsclflayout *flp)
5275 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) {
5276 for (i = 0; i < flp->nfsfl_fhcnt; i++)
5277 free(flp->nfsfl_fh[i], M_NFSFH);
5278 if (flp->nfsfl_devp != NULL)
5279 flp->nfsfl_devp->nfsdi_layoutrefs--;
5281 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
5282 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) {
5283 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
5284 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
5285 if (flp->nfsfl_ffm[i].devp != NULL)
5286 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--;
5288 free(flp, M_NFSFLAYOUT);
5292 * Free up a file layout devinfo structure.
5295 nfscl_freedevinfo(struct nfscldevinfo *dip)
5298 free(dip, M_NFSDEVINFO);
5302 * Mark any layouts that match as recalled.
5305 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5306 uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op,
5307 char *devid, struct nfsclrecalllayout *recallp)
5309 struct nfsclrecalllayout *rp, *orp;
5311 recallp->nfsrecly_recalltype = recalltype;
5312 recallp->nfsrecly_iomode = iomode;
5313 recallp->nfsrecly_stateseqid = stateseqid;
5314 recallp->nfsrecly_off = off;
5315 recallp->nfsrecly_len = len;
5316 recallp->nfsrecly_stat = stat;
5317 recallp->nfsrecly_op = op;
5319 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID);
5321 * Order the list as file returns first, followed by fsid and any
5322 * returns, both in increasing stateseqid order.
5323 * Note that the seqids wrap around, so 1 is after 0xffffffff.
5324 * (I'm not sure this is correct because I find RFC5661 confusing
5325 * on this, but hopefully it will work ok.)
5328 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5330 if ((recalltype == NFSLAYOUTRETURN_FILE &&
5331 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5332 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5333 (recalltype != NFSLAYOUTRETURN_FILE &&
5334 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5335 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5336 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5341 * Put any error return on all the file returns that will
5344 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE &&
5345 stat != 0 && rp->nfsrecly_stat == 0) {
5346 rp->nfsrecly_stat = stat;
5347 rp->nfsrecly_op = op;
5349 NFSBCOPY(devid, rp->nfsrecly_devid,
5355 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5358 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5360 lyp->nfsly_flags |= NFSLY_RECALL;
5361 wakeup(lyp->nfsly_clp);
5366 * Compare the two seqids for ordering. The trick is that the seqids can
5367 * wrap around from 0xffffffff->0, so check for the cases where one
5368 * has wrapped around.
5369 * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5372 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5375 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5376 /* seqid2 has wrapped around. */
5378 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5379 /* seqid1 has wrapped around. */
5381 if (seqid1 <= seqid2)
5387 * Do a layout return for each of the recalls.
5390 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5391 struct ucred *cred, NFSPROC_T *p)
5393 struct nfsclrecalllayout *rp;
5394 nfsv4stateid_t stateid;
5397 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5398 stateid.seqid = lyp->nfsly_stateid.seqid;
5399 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5400 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5402 layouttype = NFSLAYOUT_FLEXFILE;
5403 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5404 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5405 lyp->nfsly_fhlen, 0, layouttype,
5406 rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5407 rp->nfsrecly_off, rp->nfsrecly_len,
5408 &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op,
5409 rp->nfsrecly_devid);
5414 * Do the layout commit for a file layout.
5417 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5418 struct ucred *cred, NFSPROC_T *p)
5420 struct nfsclflayout *flp;
5422 int error, layouttype;
5424 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5425 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5427 layouttype = NFSLAYOUT_FLEXFILE;
5428 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5429 if (layouttype == NFSLAYOUT_FLEXFILE &&
5430 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
5431 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
5432 /* If not supported, don't bother doing it. */
5434 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5437 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5438 len = flp->nfsfl_end - flp->nfsfl_off;
5439 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5440 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5441 lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5442 layouttype, cred, p, NULL);
5443 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5444 if (error == NFSERR_NOTSUPP) {
5445 /* If not supported, don't bother doing it. */
5447 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5456 * Commit all layouts for a file (vnode).
5459 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5461 struct nfsclclient *clp;
5462 struct nfscllayout *lyp;
5463 struct nfsnode *np = VTONFS(vp);
5465 struct nfsmount *nmp;
5469 if (NFSHASNOLAYOUTCOMMIT(nmp))
5477 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5482 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5483 if (NFSCL_FORCEDISM(mp)) {
5488 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5489 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5491 NFSCL_DEBUG(4, "do layoutcommit2\n");
5492 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5496 nfsv4_relref(&lyp->nfsly_lock);