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 bool nfscl_checkown(struct nfsclowner *, struct nfsclopen *, uint8_t *,
106 uint8_t *, struct nfscllockowner **, struct nfsclopen **,
107 struct nfsclopen **);
108 static void nfscl_clrelease(struct nfsclclient *);
109 static void nfscl_cleanclient(struct nfsclclient *);
110 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
111 struct ucred *, NFSPROC_T *);
112 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
113 struct nfsmount *, struct ucred *, NFSPROC_T *);
114 static void nfscl_recover(struct nfsclclient *, bool *, struct ucred *,
116 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
117 struct nfscllock *, int);
118 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
119 struct nfscllock **, int);
120 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *);
121 static u_int32_t nfscl_nextcbident(void);
122 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
123 static struct nfsclclient *nfscl_getclnt(u_int32_t);
124 static struct nfsclclient *nfscl_getclntsess(uint8_t *);
125 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
127 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
128 int, struct nfsclrecalllayout **);
129 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
130 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
132 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
133 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
134 u_int8_t *, struct nfscllock **);
135 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
136 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
137 struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
138 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
139 struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
140 struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *);
141 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
142 struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
143 struct nfscldeleg *, struct ucred *, NFSPROC_T *);
144 static void nfscl_totalrecall(struct nfsclclient *);
145 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
146 struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
147 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
148 u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
149 struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
150 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
151 int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
152 struct ucred *, NFSPROC_T *);
153 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
154 struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
155 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *,
157 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
158 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
159 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
160 struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int,
162 static void nfscl_freeopenowner(struct nfsclowner *, int);
163 static void nfscl_cleandeleg(struct nfscldeleg *);
164 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
165 struct nfsmount *, NFSPROC_T *);
166 static void nfscl_emptylockowner(struct nfscllockowner *,
167 struct nfscllockownerfhhead *);
168 static void nfscl_mergeflayouts(struct nfsclflayouthead *,
169 struct nfsclflayouthead *);
170 static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t,
171 uint64_t, uint32_t, uint32_t, uint32_t, char *, struct nfsclrecalllayout *);
172 static int nfscl_seq(uint32_t, uint32_t);
173 static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *,
174 struct ucred *, NFSPROC_T *);
175 static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *,
176 struct ucred *, NFSPROC_T *);
178 static short nfscberr_null[] = {
183 static short nfscberr_getattr[] = {
192 static short nfscberr_recall[] = {
202 static short *nfscl_cberrmap[] = {
210 #define NETFAMILY(clp) \
211 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
214 * Called for an open operation.
215 * If the nfhp argument is NULL, just get an openowner.
218 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
219 struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
220 struct nfsclopen **opp, int *newonep, int *retp, int lockit)
222 struct nfsclclient *clp;
223 struct nfsclowner *owp, *nowp;
224 struct nfsclopen *op = NULL, *nop = NULL;
225 struct nfscldeleg *dp;
226 struct nfsclownerhead *ohp;
227 u_int8_t own[NFSV4CL_LOCKNAMELEN];
238 * Might need one or both of these, so MALLOC them now, to
239 * avoid a tsleep() in MALLOC later.
241 nowp = malloc(sizeof (struct nfsclowner),
242 M_NFSCLOWNER, M_WAITOK);
244 nop = malloc(sizeof (struct nfsclopen) +
245 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
246 ret = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
248 free(nowp, M_NFSCLOWNER);
250 free(nop, M_NFSCLOPEN);
255 * Get the Open iff it already exists.
256 * If none found, add the new one or return error, depending upon
261 /* First check the delegation list */
262 if (nfhp != NULL && usedeleg) {
263 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
264 if (dp->nfsdl_fhlen == fhlen &&
265 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
266 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
267 (dp->nfsdl_flags & NFSCLDL_WRITE))
275 /* For NFSv4.1/4.2 and this option, use a single open_owner. */
276 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
277 nfscl_filllockowner(NULL, own, F_POSIX);
279 nfscl_filllockowner(p->td_proc, own, F_POSIX);
281 ohp = &dp->nfsdl_owner;
283 ohp = &clp->nfsc_owner;
284 /* Now, search for an openowner */
285 LIST_FOREACH(owp, ohp, nfsow_list) {
286 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
291 * Create a new open, as required.
293 nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
297 * Now, check the mode on the open and return the appropriate
301 if (nfhp != NULL && dp != NULL && nop == NULL)
302 /* new local open on delegation */
303 *retp = NFSCLOPEN_SETCRED;
305 *retp = NFSCLOPEN_OK;
307 if (op != NULL && (amode & ~(op->nfso_mode))) {
308 op->nfso_mode |= amode;
309 if (retp != NULL && dp == NULL)
310 *retp = NFSCLOPEN_DOOPEN;
314 * Serialize modifications to the open owner for multiple threads
315 * within the same process using a read/write sleep lock.
316 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
317 * by acquiring a shared lock. The close operations still use an
318 * exclusive lock for this case.
321 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) {
323 * Get a shared lock on the OpenOwner, but first
324 * wait for any pending exclusive lock, so that the
325 * exclusive locker gets priority.
327 nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
328 NFSCLSTATEMUTEXPTR, NULL);
329 nfsv4_getref(&owp->nfsow_rwlock, NULL,
330 NFSCLSTATEMUTEXPTR, NULL);
332 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
336 free(nowp, M_NFSCLOWNER);
338 free(nop, M_NFSCLOPEN);
347 * Create a new open, as required.
350 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
351 struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
352 struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
353 struct ucred *cred, int *newonep)
355 struct nfsclowner *owp = *owpp, *nowp;
356 struct nfsclopen *op, *nop;
366 if (owp == NULL && nowp != NULL) {
367 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
368 LIST_INIT(&nowp->nfsow_open);
369 nowp->nfsow_clp = clp;
370 nowp->nfsow_seqid = 0;
371 nowp->nfsow_defunct = 0;
372 nfscl_lockinit(&nowp->nfsow_rwlock);
374 nfsstatsv1.cllocalopenowners++;
375 LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
377 nfsstatsv1.clopenowners++;
378 LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
386 /* If an fhp has been specified, create an Open as well. */
388 /* and look for the correct open, based upon FH */
389 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
390 if (op->nfso_fhlen == fhlen &&
391 !NFSBCMP(op->nfso_fh, fhp, fhlen))
394 if (op == NULL && nop != NULL) {
397 nop->nfso_opencnt = 0;
398 nop->nfso_posixlock = 1;
399 nop->nfso_fhlen = fhlen;
400 NFSBCOPY(fhp, nop->nfso_fh, fhlen);
401 LIST_INIT(&nop->nfso_lock);
402 nop->nfso_stateid.seqid = 0;
403 nop->nfso_stateid.other[0] = 0;
404 nop->nfso_stateid.other[1] = 0;
405 nop->nfso_stateid.other[2] = 0;
406 KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
407 newnfs_copyincred(cred, &nop->nfso_cred);
409 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
410 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
412 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
413 nfsstatsv1.cllocalopens++;
415 nfsstatsv1.clopens++;
417 LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
429 * Called to find/add a delegation to a client.
432 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
433 int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
435 struct nfscldeleg *dp = *dpp, *tdp;
438 * First, if we have received a Read delegation for a file on a
439 * read/write file system, just return it, because they aren't
442 if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
443 (dp->nfsdl_flags & NFSCLDL_READ)) {
444 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
445 free(dp, M_NFSCLDELEG);
450 /* Look for the correct deleg, based upon FH */
452 tdp = nfscl_finddeleg(clp, nfhp, fhlen);
456 return (NFSERR_BADSTATEID);
459 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
460 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
462 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
463 nfsstatsv1.cldelegates++;
467 * Delegation already exists, what do we do if a new one??
470 printf("Deleg already exists!\n");
471 free(dp, M_NFSCLDELEG);
482 * Find a delegation for this file handle. Return NULL upon failure.
484 static struct nfscldeleg *
485 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
487 struct nfscldeleg *dp;
489 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
490 if (dp->nfsdl_fhlen == fhlen &&
491 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
498 * Get a stateid for an I/O operation. First, look for an open and iff
499 * found, return either a lockowner stateid or the open stateid.
500 * If no Open is found, just return error and the special stateid of all zeros.
503 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
504 int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
507 struct nfsclclient *clp;
508 struct nfsclowner *owp;
509 struct nfsclopen *op = NULL, *top;
510 struct nfscllockowner *lp;
511 struct nfscldeleg *dp;
513 struct nfsmount *nmp;
514 u_int8_t own[NFSV4CL_LOCKNAMELEN];
520 * Initially, just set the special stateid of all zeros.
521 * (Don't do this for a DS, since the special stateid can't be used.)
525 stateidp->other[0] = 0;
526 stateidp->other[1] = 0;
527 stateidp->other[2] = 0;
529 if (vnode_vtype(vp) != VREG)
532 nmp = VFSTONFS(vp->v_mount);
534 clp = nfscl_findcl(nmp);
541 * Wait for recovery to complete.
543 while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
544 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
545 PZERO, "nfsrecvr", NULL);
548 * First, look for a delegation.
550 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
551 if (dp->nfsdl_fhlen == fhlen &&
552 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
553 if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
554 (dp->nfsdl_flags & NFSCLDL_WRITE)) {
555 stateidp->seqid = dp->nfsdl_stateid.seqid;
556 stateidp->other[0] = dp->nfsdl_stateid.other[0];
557 stateidp->other[1] = dp->nfsdl_stateid.other[1];
558 stateidp->other[2] = dp->nfsdl_stateid.other[2];
559 if (!(np->n_flag & NDELEGRECALL)) {
560 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
562 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
564 dp->nfsdl_timestamp = NFSD_MONOSEC +
566 dp->nfsdl_rwlock.nfslock_usecnt++;
567 *lckpp = (void *)&dp->nfsdl_rwlock;
578 * If p != NULL, we want to search the parentage tree
579 * for a matching OpenOwner and use that.
581 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
582 nfscl_filllockowner(NULL, own, F_POSIX);
584 nfscl_filllockowner(p->td_proc, own, F_POSIX);
586 error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, own, own,
588 if (error == 0 && lp != NULL && fords == 0) {
589 /* Don't return a lock stateid for a DS. */
591 lp->nfsl_stateid.seqid;
593 lp->nfsl_stateid.other[0];
595 lp->nfsl_stateid.other[1];
597 lp->nfsl_stateid.other[2];
603 /* If not found, just look for any OpenOwner that will work. */
606 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
607 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
608 if (op->nfso_fhlen == fhlen &&
609 !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
610 if (top == NULL && (op->nfso_mode &
611 NFSV4OPEN_ACCESSWRITE) != 0 &&
612 (mode & NFSV4OPEN_ACCESSREAD) != 0)
614 if ((mode & op->nfso_mode) == mode) {
624 NFSCL_DEBUG(2, "openmode top=%p\n", top);
625 if (top == NULL || NFSHASOPENMODE(nmp)) {
632 * For read aheads or write behinds, use the open cred.
633 * A read ahead or write behind is indicated by p == NULL.
636 newnfs_copycred(&op->nfso_cred, cred);
640 * No lock stateid, so return the open stateid.
642 stateidp->seqid = op->nfso_stateid.seqid;
643 stateidp->other[0] = op->nfso_stateid.other[0];
644 stateidp->other[1] = op->nfso_stateid.other[1];
645 stateidp->other[2] = op->nfso_stateid.other[2];
651 * Search for a matching file, mode and, optionally, lockowner.
654 nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
655 u_int8_t *openown, u_int8_t *lockown, u_int32_t mode,
656 struct nfscllockowner **lpp, struct nfsclopen **opp)
658 struct nfsclowner *owp;
659 struct nfsclopen *op, *rop, *rop2;
665 * rop will be set to the open to be returned. There are three
666 * variants of this, all for an open of the correct file:
667 * 1 - A match of lockown.
668 * 2 - A match of the openown, when no lockown match exists.
669 * 3 - A match for any open, if no openown or lockown match exists.
670 * Looking for #2 over #3 probably isn't necessary, but since
671 * RFC3530 is vague w.r.t. the relationship between openowners and
672 * lockowners, I think this is the safer way to go.
677 /* Search the client list */
678 LIST_FOREACH(owp, ohp, nfsow_list) {
679 /* and look for the correct open */
680 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
681 if (op->nfso_fhlen == fhlen &&
682 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
683 && (op->nfso_mode & mode) == mode)
684 keep_looping = nfscl_checkown(owp, op, openown,
685 lockown, lpp, &rop, &rop2);
700 /* Check for an owner match. */
702 nfscl_checkown(struct nfsclowner *owp, struct nfsclopen *op, uint8_t *openown,
703 uint8_t *lockown, struct nfscllockowner **lpp, struct nfsclopen **ropp,
704 struct nfsclopen **ropp2)
706 struct nfscllockowner *lp;
711 /* Now look for a matching lockowner. */
712 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
713 if (!NFSBCMP(lp->nfsl_owner, lockown,
714 NFSV4CL_LOCKNAMELEN)) {
721 if (*ropp == NULL && !NFSBCMP(owp->nfsow_owner, openown,
722 NFSV4CL_LOCKNAMELEN)) {
725 keep_looping = false;
729 return (keep_looping);
733 * Release use of an open owner. Called when open operations are done
734 * with the open owner.
737 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
738 __unused int error, __unused int candelete, int unlocked)
745 if (NFSHASONEOPENOWN(nmp))
746 nfsv4_relref(&owp->nfsow_rwlock);
748 nfscl_lockunlock(&owp->nfsow_rwlock);
750 nfscl_clrelease(owp->nfsow_clp);
755 * Release use of an open structure under an open owner.
758 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
761 struct nfsclclient *clp;
762 struct nfsclowner *owp;
768 if (NFSHASONEOPENOWN(nmp))
769 nfsv4_relref(&owp->nfsow_rwlock);
771 nfscl_lockunlock(&owp->nfsow_rwlock);
772 clp = owp->nfsow_clp;
773 if (error && candelete && op->nfso_opencnt == 0)
774 nfscl_freeopen(op, 0);
775 nfscl_clrelease(clp);
780 * Called to get a clientid structure. It will optionally lock the
781 * client data structures to do the SetClientId/SetClientId_confirm,
782 * but will release that lock and return the clientid with a reference
784 * If the "cred" argument is NULL, a new clientid should not be created.
785 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
787 * The start_renewthread argument tells nfscl_getcl() to start a renew
788 * thread if this creates a new clp.
789 * It always clpp with a reference count on it, unless returning an error.
792 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
793 int start_renewthread, struct nfsclclient **clpp)
795 struct nfsclclient *clp;
796 struct nfsclclient *newclp = NULL;
797 struct nfsmount *nmp;
798 char uuid[HOSTUUIDLEN];
799 int igotlock = 0, error, trystalecnt, clidinusedelay, i;
804 getcredhostuuid(cred, uuid, sizeof uuid);
805 idlen = strlen(uuid);
807 idlen += sizeof (u_int64_t);
809 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
811 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
816 * If a forced dismount is already in progress, don't
817 * allocate a new clientid and get out now. For the case where
818 * clp != NULL, this is a harmless optimization.
820 if (NFSCL_FORCEDISM(mp)) {
823 free(newclp, M_NFSCLCLIENT);
828 if (newclp == NULL) {
833 clp->nfsc_idlen = idlen;
834 LIST_INIT(&clp->nfsc_owner);
835 TAILQ_INIT(&clp->nfsc_deleg);
836 TAILQ_INIT(&clp->nfsc_layout);
837 LIST_INIT(&clp->nfsc_devinfo);
838 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
839 LIST_INIT(&clp->nfsc_deleghash[i]);
840 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
841 LIST_INIT(&clp->nfsc_layouthash[i]);
842 clp->nfsc_flags = NFSCLFLAGS_INITED;
843 clp->nfsc_clientidrev = 1;
844 clp->nfsc_cbident = nfscl_nextcbident();
845 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
847 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
851 if (start_renewthread != 0)
852 nfscl_start_renewthread(clp);
856 free(newclp, M_NFSCLCLIENT);
859 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
860 !NFSCL_FORCEDISM(mp))
861 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
862 NFSCLSTATEMUTEXPTR, mp);
865 * Call nfsv4_lock() with "iwantlock == 0" so that it will
866 * wait for a pending exclusive lock request. This gives the
867 * exclusive lock request priority over this shared lock
869 * An exclusive lock on nfsc_lock is used mainly for server
872 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, mp);
873 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
875 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
877 * Both nfsv4_lock() and nfsv4_getref() know to check
878 * for NFSCL_FORCEDISM() and return without sleeping to
879 * wait for the exclusive lock to be released, since it
880 * might be held by nfscl_umount() and we need to get out
881 * now for that case and not wait until nfscl_umount()
890 * If it needs a clientid, do the setclientid now.
892 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
894 panic("nfscl_clget");
895 if (p == NULL || cred == NULL) {
897 nfsv4_unlock(&clp->nfsc_lock, 0);
902 * If RFC3530 Sec. 14.2.33 is taken literally,
903 * NFSERR_CLIDINUSE will be returned persistently for the
904 * case where a new mount of the same file system is using
905 * a different principal. In practice, NFSERR_CLIDINUSE is
906 * only returned when there is outstanding unexpired state
907 * on the clientid. As such, try for twice the lease
908 * interval, if we know what that is. Otherwise, make a
910 * The case of returning NFSERR_STALECLIENTID is far less
911 * likely, but might occur if there is a significant delay
912 * between doing the SetClientID and SetClientIDConfirm Ops,
913 * such that the server throws away the clientid before
914 * receiving the SetClientIDConfirm.
916 if (clp->nfsc_renew > 0)
917 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
919 clidinusedelay = 120;
922 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
923 if (error == NFSERR_STALECLIENTID ||
924 error == NFSERR_STALEDONTRECOVER ||
925 error == NFSERR_BADSESSION ||
926 error == NFSERR_CLIDINUSE) {
927 (void) nfs_catnap(PZERO, error, "nfs_setcl");
929 } while (((error == NFSERR_STALECLIENTID ||
930 error == NFSERR_BADSESSION ||
931 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
932 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
935 nfsv4_unlock(&clp->nfsc_lock, 0);
939 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
943 nfsv4_unlock(&clp->nfsc_lock, 1);
952 * Get a reference to a clientid and return it, if valid.
955 nfscl_findcl(struct nfsmount *nmp)
957 struct nfsclclient *clp;
960 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
966 * Release the clientid structure. It may be locked or reference counted.
969 nfscl_clrelease(struct nfsclclient *clp)
972 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
973 nfsv4_unlock(&clp->nfsc_lock, 0);
975 nfsv4_relref(&clp->nfsc_lock);
979 * External call for nfscl_clrelease.
982 nfscl_clientrelease(struct nfsclclient *clp)
986 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
987 nfsv4_unlock(&clp->nfsc_lock, 0);
989 nfsv4_relref(&clp->nfsc_lock);
994 * Called when wanting to lock a byte region.
997 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
998 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
999 int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
1000 struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
1002 struct nfscllockowner *lp;
1003 struct nfsclopen *op;
1004 struct nfsclclient *clp;
1005 struct nfscllockowner *nlp;
1006 struct nfscllock *nlop, *otherlop;
1007 struct nfscldeleg *dp = NULL, *ldp = NULL;
1008 struct nfscllockownerhead *lhp = NULL;
1010 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
1012 int error = 0, ret, donelocally = 0;
1015 /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
1024 * Might need these, so MALLOC them now, to
1025 * avoid a tsleep() in MALLOC later.
1028 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
1030 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1032 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1033 nlop->nfslo_type = type;
1034 nlop->nfslo_first = off;
1035 if (len == NFS64BITSSET) {
1036 nlop->nfslo_end = NFS64BITSSET;
1038 nlop->nfslo_end = off + len;
1039 if (nlop->nfslo_end <= nlop->nfslo_first)
1040 error = NFSERR_INVAL;
1047 error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
1050 free(nlp, M_NFSCLLOCKOWNER);
1051 free(otherlop, M_NFSCLLOCK);
1052 free(nlop, M_NFSCLLOCK);
1059 openownp = ropenownp;
1061 nfscl_filllockowner(id, own, flags);
1063 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
1064 nfscl_filllockowner(NULL, openown, F_POSIX);
1066 nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1072 * First, search for a delegation. If one exists for this file,
1073 * the lock can be done locally against it, so long as there
1074 * isn't a local lock conflict.
1076 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1077 np->n_fhp->nfh_len);
1078 /* Just sanity check for correct type of delegation */
1079 if (dp != NULL && ((dp->nfsdl_flags &
1080 (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1082 (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1086 /* Now, find an open and maybe a lockowner. */
1087 ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
1088 np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1090 ret = nfscl_getopen(&clp->nfsc_owner,
1091 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1092 ownp, mode, NULL, &op);
1094 lhp = &dp->nfsdl_lock;
1095 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1096 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1097 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1105 * Get the related Open and maybe lockowner.
1107 error = nfscl_getopen(&clp->nfsc_owner,
1108 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1109 ownp, mode, &lp, &op);
1111 lhp = &op->nfso_lock;
1113 if (!error && !recovery)
1114 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1115 np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1118 nfscl_clrelease(clp);
1121 free(nlp, M_NFSCLLOCKOWNER);
1122 free(otherlop, M_NFSCLLOCK);
1123 free(nlop, M_NFSCLLOCK);
1128 * Ok, see if a lockowner exists and create one, as required.
1131 LIST_FOREACH(lp, lhp, nfsl_list) {
1132 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1136 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1138 NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1139 NFSV4CL_LOCKNAMELEN);
1141 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1142 NFSV4CL_LOCKNAMELEN);
1143 nlp->nfsl_seqid = 0;
1144 nlp->nfsl_lockflags = flags;
1145 nlp->nfsl_inprog = NULL;
1146 nfscl_lockinit(&nlp->nfsl_rwlock);
1147 LIST_INIT(&nlp->nfsl_lock);
1149 nlp->nfsl_open = NULL;
1150 nfsstatsv1.cllocallockowners++;
1152 nlp->nfsl_open = op;
1153 nfsstatsv1.cllockowners++;
1155 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1162 * Now, update the byte ranges for locks.
1164 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1170 nfscl_clrelease(clp);
1173 * Serial modifications on the lock owner for multiple threads
1174 * for the same process using a read/write lock.
1177 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1183 free(nlp, M_NFSCLLOCKOWNER);
1185 free(nlop, M_NFSCLLOCK);
1187 free(otherlop, M_NFSCLLOCK);
1194 * Called to unlock a byte range, for LockU.
1197 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1198 __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1199 struct nfsclclient *clp, void *id, int flags,
1200 struct nfscllockowner **lpp, int *dorpcp)
1202 struct nfscllockowner *lp;
1203 struct nfsclowner *owp;
1204 struct nfsclopen *op;
1205 struct nfscllock *nlop, *other_lop = NULL;
1206 struct nfscldeleg *dp;
1208 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1216 * Might need these, so MALLOC them now, to
1217 * avoid a tsleep() in MALLOC later.
1220 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1221 nlop->nfslo_type = F_UNLCK;
1222 nlop->nfslo_first = off;
1223 if (len == NFS64BITSSET) {
1224 nlop->nfslo_end = NFS64BITSSET;
1226 nlop->nfslo_end = off + len;
1227 if (nlop->nfslo_end <= nlop->nfslo_first) {
1228 free(nlop, M_NFSCLLOCK);
1229 return (NFSERR_INVAL);
1234 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1237 nfscl_filllockowner(id, own, flags);
1241 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1242 np->n_fhp->nfh_len);
1245 * First, unlock any local regions on a delegation.
1248 /* Look for this lockowner. */
1249 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1250 if (!NFSBCMP(lp->nfsl_owner, own,
1251 NFSV4CL_LOCKNAMELEN))
1255 /* Use other_lop, so nlop is still available */
1256 (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1260 * Now, find a matching open/lockowner that hasn't already been done,
1261 * as marked by nfsl_inprog.
1265 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1266 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1267 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1268 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1269 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1270 if (lp->nfsl_inprog == NULL &&
1271 !NFSBCMP(lp->nfsl_owner, own,
1272 NFSV4CL_LOCKNAMELEN)) {
1286 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1290 * Serial modifications on the lock owner for multiple
1291 * threads for the same process using a read/write lock.
1293 lp->nfsl_inprog = p;
1294 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1299 free(nlop, M_NFSCLLOCK);
1301 free(other_lop, M_NFSCLLOCK);
1306 * Release all lockowners marked in progess for this process and file.
1309 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1310 void *id, int flags)
1312 struct nfsclowner *owp;
1313 struct nfsclopen *op;
1314 struct nfscllockowner *lp;
1316 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1319 nfscl_filllockowner(id, own, flags);
1321 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1322 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1323 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1324 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1325 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1326 if (lp->nfsl_inprog == p &&
1327 !NFSBCMP(lp->nfsl_owner, own,
1328 NFSV4CL_LOCKNAMELEN)) {
1329 lp->nfsl_inprog = NULL;
1330 nfscl_lockunlock(&lp->nfsl_rwlock);
1336 nfscl_clrelease(clp);
1341 * Called to find out if any bytes within the byte range specified are
1342 * write locked by the calling process. Used to determine if flushing
1343 * is required before a LockU.
1344 * If in doubt, return 1, so the flush will occur.
1347 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1348 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1350 struct nfsclowner *owp;
1351 struct nfscllockowner *lp;
1352 struct nfsclopen *op;
1353 struct nfsclclient *clp;
1354 struct nfscllock *lop;
1355 struct nfscldeleg *dp;
1358 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1362 switch (fl->l_whence) {
1366 * Caller is responsible for adding any necessary offset
1367 * when SEEK_CUR is used.
1372 off = np->n_size + fl->l_start;
1377 if (fl->l_len != 0) {
1378 end = off + fl->l_len;
1385 error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
1388 nfscl_filllockowner(id, own, flags);
1392 * First check the delegation locks.
1394 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1396 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1397 if (!NFSBCMP(lp->nfsl_owner, own,
1398 NFSV4CL_LOCKNAMELEN))
1402 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1403 if (lop->nfslo_first >= end)
1405 if (lop->nfslo_end <= off)
1407 if (lop->nfslo_type == F_WRLCK) {
1408 nfscl_clrelease(clp);
1417 * Now, check state against the server.
1419 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1420 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1421 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1422 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1423 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1424 if (!NFSBCMP(lp->nfsl_owner, own,
1425 NFSV4CL_LOCKNAMELEN))
1429 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1430 if (lop->nfslo_first >= end)
1432 if (lop->nfslo_end <= off)
1434 if (lop->nfslo_type == F_WRLCK) {
1435 nfscl_clrelease(clp);
1444 nfscl_clrelease(clp);
1450 * Release a byte range lock owner structure.
1453 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1455 struct nfsclclient *clp;
1460 clp = lp->nfsl_open->nfso_own->nfsow_clp;
1461 if (error != 0 && candelete &&
1462 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1463 nfscl_freelockowner(lp, 0);
1465 nfscl_lockunlock(&lp->nfsl_rwlock);
1466 nfscl_clrelease(clp);
1471 * Free up an open structure and any associated byte range lock structures.
1474 nfscl_freeopen(struct nfsclopen *op, int local)
1477 LIST_REMOVE(op, nfso_list);
1478 nfscl_freealllocks(&op->nfso_lock, local);
1479 free(op, M_NFSCLOPEN);
1481 nfsstatsv1.cllocalopens--;
1483 nfsstatsv1.clopens--;
1487 * Free up all lock owners and associated locks.
1490 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1492 struct nfscllockowner *lp, *nlp;
1494 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1495 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1497 nfscl_freelockowner(lp, local);
1502 * Called for an Open when NFSERR_EXPIRED is received from the server.
1503 * If there are no byte range locks nor a Share Deny lost, try to do a
1504 * fresh Open. Otherwise, free the open.
1507 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1508 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1510 struct nfscllockowner *lp;
1511 struct nfscldeleg *dp;
1512 int mustdelete = 0, error;
1515 * Look for any byte range lock(s).
1517 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1518 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1525 * If no byte range lock(s) nor a Share deny, try to re-open.
1527 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1528 newnfs_copycred(&op->nfso_cred, cred);
1530 error = nfsrpc_reopen(nmp, op->nfso_fh,
1531 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1535 free(dp, M_NFSCLDELEG);
1540 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1541 op->nfso_fhlen, cred, p, &dp);
1545 * If a byte range lock or Share deny or couldn't re-open, free it.
1548 nfscl_freeopen(op, 0);
1549 return (mustdelete);
1553 * Free up an open owner structure.
1556 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1559 LIST_REMOVE(owp, nfsow_list);
1560 free(owp, M_NFSCLOWNER);
1562 nfsstatsv1.cllocalopenowners--;
1564 nfsstatsv1.clopenowners--;
1568 * Free up a byte range lock owner structure.
1571 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1573 struct nfscllock *lop, *nlop;
1575 LIST_REMOVE(lp, nfsl_list);
1576 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1577 nfscl_freelock(lop, local);
1579 free(lp, M_NFSCLLOCKOWNER);
1581 nfsstatsv1.cllocallockowners--;
1583 nfsstatsv1.cllockowners--;
1587 * Free up a byte range lock structure.
1590 nfscl_freelock(struct nfscllock *lop, int local)
1593 LIST_REMOVE(lop, nfslo_list);
1594 free(lop, M_NFSCLLOCK);
1596 nfsstatsv1.cllocallocks--;
1598 nfsstatsv1.cllocks--;
1602 * Clean out the state related to a delegation.
1605 nfscl_cleandeleg(struct nfscldeleg *dp)
1607 struct nfsclowner *owp, *nowp;
1608 struct nfsclopen *op;
1610 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1611 op = LIST_FIRST(&owp->nfsow_open);
1613 if (LIST_NEXT(op, nfso_list) != NULL)
1614 panic("nfscleandel");
1615 nfscl_freeopen(op, 1);
1617 nfscl_freeopenowner(owp, 1);
1619 nfscl_freealllocks(&dp->nfsdl_lock, 1);
1623 * Free a delegation.
1626 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp, bool freeit)
1629 TAILQ_REMOVE(hdp, dp, nfsdl_list);
1630 LIST_REMOVE(dp, nfsdl_hash);
1632 free(dp, M_NFSCLDELEG);
1633 nfsstatsv1.cldelegates--;
1638 * Free up all state related to this client structure.
1641 nfscl_cleanclient(struct nfsclclient *clp)
1643 struct nfsclowner *owp, *nowp;
1644 struct nfsclopen *op, *nop;
1645 struct nfscllayout *lyp, *nlyp;
1646 struct nfscldevinfo *dip, *ndip;
1648 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1649 nfscl_freelayout(lyp);
1651 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
1652 nfscl_freedevinfo(dip);
1654 /* Now, all the OpenOwners, etc. */
1655 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1656 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1657 nfscl_freeopen(op, 0);
1659 nfscl_freeopenowner(owp, 0);
1664 * Called when an NFSERR_EXPIRED is received from the server.
1667 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1668 struct ucred *cred, NFSPROC_T *p)
1670 struct nfsclowner *owp, *nowp, *towp;
1671 struct nfsclopen *op, *nop, *top;
1672 struct nfscldeleg *dp, *ndp;
1673 int ret, printed = 0;
1676 * First, merge locally issued Opens into the list for the server.
1678 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1679 while (dp != NULL) {
1680 ndp = TAILQ_NEXT(dp, nfsdl_list);
1681 owp = LIST_FIRST(&dp->nfsdl_owner);
1682 while (owp != NULL) {
1683 nowp = LIST_NEXT(owp, nfsow_list);
1684 op = LIST_FIRST(&owp->nfsow_open);
1686 if (LIST_NEXT(op, nfso_list) != NULL)
1688 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1689 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1690 NFSV4CL_LOCKNAMELEN))
1694 /* Merge opens in */
1695 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1696 if (top->nfso_fhlen == op->nfso_fhlen &&
1697 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1699 top->nfso_mode |= op->nfso_mode;
1700 top->nfso_opencnt += op->nfso_opencnt;
1705 /* Just add the open to the owner list */
1706 LIST_REMOVE(op, nfso_list);
1707 op->nfso_own = towp;
1708 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1709 nfsstatsv1.cllocalopens--;
1710 nfsstatsv1.clopens++;
1713 /* Just add the openowner to the client list */
1714 LIST_REMOVE(owp, nfsow_list);
1715 owp->nfsow_clp = clp;
1716 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1717 nfsstatsv1.cllocalopenowners--;
1718 nfsstatsv1.clopenowners++;
1719 nfsstatsv1.cllocalopens--;
1720 nfsstatsv1.clopens++;
1725 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1727 printf("nfsv4 expired locks lost\n");
1729 nfscl_cleandeleg(dp);
1730 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
1733 if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1737 * Now, try and reopen against the server.
1739 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1740 owp->nfsow_seqid = 0;
1741 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1742 ret = nfscl_expireopen(clp, op, nmp, cred, p);
1743 if (ret && !printed) {
1745 printf("nfsv4 expired locks lost\n");
1748 if (LIST_EMPTY(&owp->nfsow_open))
1749 nfscl_freeopenowner(owp, 0);
1754 * This function must be called after the process represented by "own" has
1755 * exited. Must be called with CLSTATE lock held.
1758 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1760 struct nfsclowner *owp, *nowp;
1761 struct nfscllockowner *lp, *nlp;
1762 struct nfscldeleg *dp;
1764 /* First, get rid of local locks on delegations. */
1765 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1766 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1767 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1768 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1770 nfscl_freelockowner(lp, 1);
1774 owp = LIST_FIRST(&clp->nfsc_owner);
1775 while (owp != NULL) {
1776 nowp = LIST_NEXT(owp, nfsow_list);
1777 if (!NFSBCMP(owp->nfsow_owner, own,
1778 NFSV4CL_LOCKNAMELEN)) {
1780 * If there are children that haven't closed the
1781 * file descriptors yet, the opens will still be
1782 * here. For that case, let the renew thread clear
1783 * out the OpenOwner later.
1785 if (LIST_EMPTY(&owp->nfsow_open))
1786 nfscl_freeopenowner(owp, 0);
1788 owp->nfsow_defunct = 1;
1795 * Find open/lock owners for processes that have exited.
1798 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1800 struct nfsclowner *owp, *nowp;
1801 struct nfsclopen *op;
1802 struct nfscllockowner *lp, *nlp;
1803 struct nfscldeleg *dp;
1806 * All the pidhash locks must be acquired, since they are sx locks
1807 * and must be acquired before the mutexes. The pid(s) that will
1808 * be used aren't known yet, so all the locks need to be acquired.
1809 * Fortunately, this function is only performed once/sec.
1813 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1814 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1815 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1816 if (LIST_EMPTY(&lp->nfsl_lock))
1817 nfscl_emptylockowner(lp, lhp);
1820 if (nfscl_procdoesntexist(owp->nfsow_owner))
1821 nfscl_cleanup_common(clp, owp->nfsow_owner);
1825 * For the single open_owner case, these lock owners need to be
1826 * checked to see if they still exist separately.
1827 * This is because nfscl_procdoesntexist() never returns true for
1828 * the single open_owner so that the above doesn't ever call
1829 * nfscl_cleanup_common().
1831 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1832 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1833 if (nfscl_procdoesntexist(lp->nfsl_owner))
1834 nfscl_cleanup_common(clp, lp->nfsl_owner);
1838 pidhash_sunlockall();
1842 * Take the empty lock owner and move it to the local lhp list if the
1843 * associated process no longer exists.
1846 nfscl_emptylockowner(struct nfscllockowner *lp,
1847 struct nfscllockownerfhhead *lhp)
1849 struct nfscllockownerfh *lfhp, *mylfhp;
1850 struct nfscllockowner *nlp;
1853 /* If not a Posix lock owner, just return. */
1854 if ((lp->nfsl_lockflags & F_POSIX) == 0)
1860 * First, search to see if this lock owner is already in the list.
1861 * If it is, then the associated process no longer exists.
1863 SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1864 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1865 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
1868 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
1869 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
1870 NFSV4CL_LOCKNAMELEN))
1873 /* If not found, check if process still exists. */
1874 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
1877 /* Move the lock owner over to the local list. */
1878 if (mylfhp == NULL) {
1879 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
1883 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
1884 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
1885 mylfhp->nfslfh_len);
1886 LIST_INIT(&mylfhp->nfslfh_lock);
1887 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
1889 LIST_REMOVE(lp, nfsl_list);
1890 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
1893 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */
1895 * Called from nfs umount to free up the clientid.
1898 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1900 struct nfsclclient *clp;
1905 * For the case that matters, this is the thread that set
1906 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
1907 * done to ensure that any thread executing nfscl_getcl() after
1908 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
1909 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
1910 * explanation, courtesy of Alan Cox.
1911 * What follows is a snippet from Alan Cox's email at:
1912 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
1914 * 1. Set MNTK_UNMOUNTF
1915 * 2. Acquire a standard FreeBSD mutex "m".
1916 * 3. Update some data structures.
1917 * 4. Release mutex "m".
1919 * Then, other threads that acquire "m" after step 4 has occurred will
1920 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to
1921 * step 2 may or may not see MNTK_UNMOUNTF as set.
1924 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
1932 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1933 panic("nfscl umount");
1936 * First, handshake with the nfscl renew thread, to terminate
1939 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1940 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1941 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
1945 * Now, get the exclusive lock on the client state, so
1946 * that no uses of the state are still in progress.
1949 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1950 NFSCLSTATEMUTEXPTR, NULL);
1951 } while (!igotlock);
1955 * Free up all the state. It will expire on the server, but
1956 * maybe we should do a SetClientId/SetClientIdConfirm so
1957 * the server throws it away?
1959 LIST_REMOVE(clp, nfsc_list);
1960 nfscl_delegreturnall(clp, p);
1961 cred = newnfs_getcred();
1962 if (NFSHASNFSV4N(nmp)) {
1963 (void)nfsrpc_destroysession(nmp, clp, cred, p);
1964 (void)nfsrpc_destroyclient(nmp, clp, cred, p);
1966 (void)nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
1967 nfscl_cleanclient(clp);
1970 free(clp, M_NFSCLCLIENT);
1976 * This function is called when a server replies with NFSERR_STALECLIENTID
1977 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
1978 * doing Opens and Locks with reclaim. If these fail, it deletes the
1979 * corresponding state.
1982 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred,
1985 struct nfsclowner *owp, *nowp;
1986 struct nfsclopen *op, *nop;
1987 struct nfscllockowner *lp, *nlp;
1988 struct nfscllock *lop, *nlop;
1989 struct nfscldeleg *dp, *ndp, *tdp;
1990 struct nfsmount *nmp;
1991 struct ucred *tcred;
1992 struct nfsclopenhead extra_open;
1993 struct nfscldeleghead extra_deleg;
1996 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
1997 int i, igotlock = 0, error, trycnt, firstlock;
1998 struct nfscllayout *lyp, *nlyp;
2001 * First, lock the client structure, so everyone else will
2002 * block when trying to use state.
2005 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2007 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2008 NFSCLSTATEMUTEXPTR, NULL);
2009 } while (!igotlock);
2012 nmp = clp->nfsc_nmp;
2014 panic("nfscl recover");
2017 * For now, just get rid of all layouts. There may be a need
2018 * to do LayoutCommit Ops with reclaim == true later.
2020 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
2021 nfscl_freelayout(lyp);
2022 TAILQ_INIT(&clp->nfsc_layout);
2023 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
2024 LIST_INIT(&clp->nfsc_layouthash[i]);
2029 error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p);
2030 } while ((error == NFSERR_STALECLIENTID ||
2031 error == NFSERR_BADSESSION ||
2032 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2035 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
2036 NFSCLFLAGS_RECVRINPROG);
2037 wakeup(&clp->nfsc_flags);
2038 nfsv4_unlock(&clp->nfsc_lock, 0);
2042 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2043 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2046 * Mark requests already queued on the server, so that they don't
2047 * initiate another recovery cycle. Any requests already in the
2048 * queue that handle state information will have the old stale
2049 * clientid/stateid and will get a NFSERR_STALESTATEID,
2050 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2051 * This will be translated to NFSERR_STALEDONTRECOVER when
2052 * R_DONTRECOVER is set.
2055 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2056 if (rep->r_nmp == nmp)
2057 rep->r_flags |= R_DONTRECOVER;
2062 * If nfsrpc_setclient() returns *retokp == true,
2063 * no more recovery is needed.
2069 * Now, mark all delegations "need reclaim".
2071 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2072 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2074 TAILQ_INIT(&extra_deleg);
2075 LIST_INIT(&extra_open);
2077 * Now traverse the state lists, doing Open and Lock Reclaims.
2079 tcred = newnfs_getcred();
2080 owp = LIST_FIRST(&clp->nfsc_owner);
2081 while (owp != NULL) {
2082 nowp = LIST_NEXT(owp, nfsow_list);
2083 owp->nfsow_seqid = 0;
2084 op = LIST_FIRST(&owp->nfsow_open);
2085 while (op != NULL) {
2086 nop = LIST_NEXT(op, nfso_list);
2087 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2088 /* Search for a delegation to reclaim with the open */
2089 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2090 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2092 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2093 mode = NFSV4OPEN_ACCESSWRITE;
2094 delegtype = NFSV4OPEN_DELEGATEWRITE;
2096 mode = NFSV4OPEN_ACCESSREAD;
2097 delegtype = NFSV4OPEN_DELEGATEREAD;
2099 if ((op->nfso_mode & mode) == mode &&
2100 op->nfso_fhlen == dp->nfsdl_fhlen &&
2101 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2106 delegtype = NFSV4OPEN_DELEGATENONE;
2107 newnfs_copycred(&op->nfso_cred, tcred);
2108 error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2109 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2110 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2113 /* Handle any replied delegation */
2114 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2115 || NFSMNT_RDONLY(nmp->nm_mountp))) {
2116 if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2117 mode = NFSV4OPEN_ACCESSWRITE;
2119 mode = NFSV4OPEN_ACCESSREAD;
2120 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2121 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2123 if ((op->nfso_mode & mode) == mode &&
2124 op->nfso_fhlen == dp->nfsdl_fhlen &&
2125 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2127 dp->nfsdl_stateid = ndp->nfsdl_stateid;
2128 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2129 dp->nfsdl_ace = ndp->nfsdl_ace;
2130 dp->nfsdl_change = ndp->nfsdl_change;
2131 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2132 if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2133 dp->nfsdl_flags |= NFSCLDL_RECALL;
2134 free(ndp, M_NFSCLDELEG);
2141 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2143 /* and reclaim all byte range locks */
2144 lp = LIST_FIRST(&op->nfso_lock);
2145 while (lp != NULL) {
2146 nlp = LIST_NEXT(lp, nfsl_list);
2149 lop = LIST_FIRST(&lp->nfsl_lock);
2150 while (lop != NULL) {
2151 nlop = LIST_NEXT(lop, nfslo_list);
2152 if (lop->nfslo_end == NFS64BITSSET)
2155 len = lop->nfslo_end - lop->nfslo_first;
2156 error = nfscl_trylock(nmp, NULL,
2157 op->nfso_fh, op->nfso_fhlen, lp,
2158 firstlock, 1, lop->nfslo_first, len,
2159 lop->nfslo_type, tcred, p);
2161 nfscl_freelock(lop, 0);
2166 /* If no locks, but a lockowner, just delete it. */
2167 if (LIST_EMPTY(&lp->nfsl_lock))
2168 nfscl_freelockowner(lp, 0);
2173 if (error != 0 && error != NFSERR_BADSESSION)
2174 nfscl_freeopen(op, 0);
2181 * Now, try and get any delegations not yet reclaimed by cobbling
2182 * to-gether an appropriate open.
2185 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2186 while (dp != NULL) {
2187 ndp = TAILQ_NEXT(dp, nfsdl_list);
2188 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2191 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2193 * Name must be as long an largest possible
2194 * NFSV4CL_LOCKNAMELEN. 12 for now.
2196 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2197 NFSV4CL_LOCKNAMELEN);
2198 LIST_INIT(&nowp->nfsow_open);
2199 nowp->nfsow_clp = clp;
2200 nowp->nfsow_seqid = 0;
2201 nowp->nfsow_defunct = 0;
2202 nfscl_lockinit(&nowp->nfsow_rwlock);
2205 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2206 nop = malloc(sizeof (struct nfsclopen) +
2207 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2208 nop->nfso_own = nowp;
2209 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2210 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2211 delegtype = NFSV4OPEN_DELEGATEWRITE;
2213 nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2214 delegtype = NFSV4OPEN_DELEGATEREAD;
2216 nop->nfso_opencnt = 0;
2217 nop->nfso_posixlock = 1;
2218 nop->nfso_fhlen = dp->nfsdl_fhlen;
2219 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2220 LIST_INIT(&nop->nfso_lock);
2221 nop->nfso_stateid.seqid = 0;
2222 nop->nfso_stateid.other[0] = 0;
2223 nop->nfso_stateid.other[1] = 0;
2224 nop->nfso_stateid.other[2] = 0;
2225 newnfs_copycred(&dp->nfsdl_cred, tcred);
2226 newnfs_copyincred(tcred, &nop->nfso_cred);
2228 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2229 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2230 nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2231 delegtype, tcred, p);
2233 if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2234 mode = NFSV4OPEN_ACCESSWRITE;
2236 mode = NFSV4OPEN_ACCESSREAD;
2237 if ((nop->nfso_mode & mode) == mode &&
2238 nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2239 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2241 dp->nfsdl_stateid = tdp->nfsdl_stateid;
2242 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2243 dp->nfsdl_ace = tdp->nfsdl_ace;
2244 dp->nfsdl_change = tdp->nfsdl_change;
2245 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2246 if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2247 dp->nfsdl_flags |= NFSCLDL_RECALL;
2248 free(tdp, M_NFSCLDELEG);
2250 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2256 free(nop, M_NFSCLOPEN);
2258 * Couldn't reclaim it, so throw the state
2261 nfscl_cleandeleg(dp);
2262 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
2264 LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2271 * Now, get rid of extra Opens and Delegations.
2273 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2275 newnfs_copycred(&op->nfso_cred, tcred);
2276 error = nfscl_tryclose(op, tcred, nmp, p);
2277 if (error == NFSERR_GRACE)
2278 (void) nfs_catnap(PZERO, error, "nfsexcls");
2279 } while (error == NFSERR_GRACE);
2280 LIST_REMOVE(op, nfso_list);
2281 free(op, M_NFSCLOPEN);
2284 free(nowp, M_NFSCLOWNER);
2286 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2288 newnfs_copycred(&dp->nfsdl_cred, tcred);
2289 error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2290 if (error == NFSERR_GRACE)
2291 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2292 } while (error == NFSERR_GRACE);
2293 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2294 free(dp, M_NFSCLDELEG);
2297 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2298 if (NFSHASNFSV4N(nmp))
2299 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
2303 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2304 wakeup(&clp->nfsc_flags);
2305 nfsv4_unlock(&clp->nfsc_lock, 0);
2312 * This function is called when a server replies with NFSERR_EXPIRED.
2313 * It deletes all state for the client and does a fresh SetClientId/confirm.
2314 * XXX Someday it should post a signal to the process(es) that hold the
2315 * state, so they know that lock state has been lost.
2318 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2320 struct nfsmount *nmp;
2322 int igotlock = 0, error, trycnt;
2325 * If the clientid has gone away or a new SetClientid has already
2326 * been done, just return ok.
2328 if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2332 * First, lock the client structure, so everyone else will
2333 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2334 * that only one thread does the work.
2337 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2339 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2340 NFSCLSTATEMUTEXPTR, NULL);
2341 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2342 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2344 nfsv4_unlock(&clp->nfsc_lock, 0);
2348 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2351 nmp = clp->nfsc_nmp;
2353 panic("nfscl expired");
2354 cred = newnfs_getcred();
2357 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2358 } while ((error == NFSERR_STALECLIENTID ||
2359 error == NFSERR_BADSESSION ||
2360 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2363 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2366 * Expire the state for the client.
2368 nfscl_expireclient(clp, nmp, cred, p);
2370 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2371 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2373 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2374 wakeup(&clp->nfsc_flags);
2375 nfsv4_unlock(&clp->nfsc_lock, 0);
2382 * This function inserts a lock in the list after insert_lop.
2385 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2386 struct nfscllock *insert_lop, int local)
2389 if ((struct nfscllockowner *)insert_lop == lp)
2390 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2392 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2394 nfsstatsv1.cllocallocks++;
2396 nfsstatsv1.cllocks++;
2400 * This function updates the locking for a lock owner and given file. It
2401 * maintains a list of lock ranges ordered on increasing file offset that
2402 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2403 * It always adds new_lop to the list and sometimes uses the one pointed
2405 * Returns 1 if the locks were modified, 0 otherwise.
2408 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2409 struct nfscllock **other_lopp, int local)
2411 struct nfscllock *new_lop = *new_lopp;
2412 struct nfscllock *lop, *tlop, *ilop;
2413 struct nfscllock *other_lop;
2414 int unlock = 0, modified = 0;
2418 * Work down the list until the lock is merged.
2420 if (new_lop->nfslo_type == F_UNLCK)
2422 ilop = (struct nfscllock *)lp;
2423 lop = LIST_FIRST(&lp->nfsl_lock);
2424 while (lop != NULL) {
2426 * Only check locks for this file that aren't before the start of
2429 if (lop->nfslo_end >= new_lop->nfslo_first) {
2430 if (new_lop->nfslo_end < lop->nfslo_first) {
2432 * If the new lock ends before the start of the
2433 * current lock's range, no merge, just insert
2438 if (new_lop->nfslo_type == lop->nfslo_type ||
2439 (new_lop->nfslo_first <= lop->nfslo_first &&
2440 new_lop->nfslo_end >= lop->nfslo_end)) {
2442 * This lock can be absorbed by the new lock/unlock.
2443 * This happens when it covers the entire range
2444 * of the old lock or is contiguous
2445 * with the old lock and is of the same type or an
2448 if (new_lop->nfslo_type != lop->nfslo_type ||
2449 new_lop->nfslo_first != lop->nfslo_first ||
2450 new_lop->nfslo_end != lop->nfslo_end)
2452 if (lop->nfslo_first < new_lop->nfslo_first)
2453 new_lop->nfslo_first = lop->nfslo_first;
2454 if (lop->nfslo_end > new_lop->nfslo_end)
2455 new_lop->nfslo_end = lop->nfslo_end;
2457 lop = LIST_NEXT(lop, nfslo_list);
2458 nfscl_freelock(tlop, local);
2463 * All these cases are for contiguous locks that are not the
2464 * same type, so they can't be merged.
2466 if (new_lop->nfslo_first <= lop->nfslo_first) {
2468 * This case is where the new lock overlaps with the
2469 * first part of the old lock. Move the start of the
2470 * old lock to just past the end of the new lock. The
2471 * new lock will be inserted in front of the old, since
2472 * ilop hasn't been updated. (We are done now.)
2474 if (lop->nfslo_first != new_lop->nfslo_end) {
2475 lop->nfslo_first = new_lop->nfslo_end;
2480 if (new_lop->nfslo_end >= lop->nfslo_end) {
2482 * This case is where the new lock overlaps with the
2483 * end of the old lock's range. Move the old lock's
2484 * end to just before the new lock's first and insert
2485 * the new lock after the old lock.
2486 * Might not be done yet, since the new lock could
2487 * overlap further locks with higher ranges.
2489 if (lop->nfslo_end != new_lop->nfslo_first) {
2490 lop->nfslo_end = new_lop->nfslo_first;
2494 lop = LIST_NEXT(lop, nfslo_list);
2498 * The final case is where the new lock's range is in the
2499 * middle of the current lock's and splits the current lock
2500 * up. Use *other_lopp to handle the second part of the
2501 * split old lock range. (We are done now.)
2502 * For unlock, we use new_lop as other_lop and tmp, since
2503 * other_lop and new_lop are the same for this case.
2504 * We noted the unlock case above, so we don't need
2505 * new_lop->nfslo_type any longer.
2507 tmp = new_lop->nfslo_first;
2509 other_lop = new_lop;
2512 other_lop = *other_lopp;
2515 other_lop->nfslo_first = new_lop->nfslo_end;
2516 other_lop->nfslo_end = lop->nfslo_end;
2517 other_lop->nfslo_type = lop->nfslo_type;
2518 lop->nfslo_end = tmp;
2519 nfscl_insertlock(lp, other_lop, lop, local);
2525 lop = LIST_NEXT(lop, nfslo_list);
2531 * Insert the new lock in the list at the appropriate place.
2534 nfscl_insertlock(lp, new_lop, ilop, local);
2542 * This function must be run as a kernel thread.
2543 * It does Renew Ops and recovery, when required.
2546 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2548 struct nfsclowner *owp, *nowp;
2549 struct nfsclopen *op;
2550 struct nfscllockowner *lp, *nlp;
2551 struct nfscldeleghead dh;
2552 struct nfscldeleg *dp, *ndp;
2555 int error, cbpathdown, islept, igotlock, ret, clearok;
2556 uint32_t recover_done_time = 0;
2558 static time_t prevsec = 0;
2559 struct nfscllockownerfh *lfhp, *nlfhp;
2560 struct nfscllockownerfhhead lfh;
2561 struct nfscllayout *lyp, *nlyp;
2562 struct nfscldevinfo *dip, *ndip;
2563 struct nfscllayouthead rlh;
2564 struct nfsclrecalllayout *recallp;
2565 struct nfsclds *dsp;
2570 cred = newnfs_getcred();
2572 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2573 mp = clp->nfsc_nmp->nm_mountp;
2576 newnfs_setroot(cred);
2578 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2580 * Only allow one full recover within 1/2 of the lease
2581 * duration (nfsc_renew).
2582 * retok is value/result. If passed in set to true,
2583 * it indicates only a CreateSession operation should
2585 * If it is returned true, it indicates that the
2586 * recovery only required a CreateSession.
2589 if (recover_done_time < NFSD_MONOSEC) {
2590 recover_done_time = NFSD_MONOSEC +
2594 NFSCL_DEBUG(1, "Doing recovery, only "
2595 "createsession=%d\n", retok);
2596 nfscl_recover(clp, &retok, cred, p);
2598 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2599 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2600 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2601 clidrev = clp->nfsc_clientidrev;
2602 error = nfsrpc_renew(clp, NULL, cred, p);
2603 if (error == NFSERR_CBPATHDOWN)
2605 else if (error == NFSERR_STALECLIENTID ||
2606 error == NFSERR_BADSESSION) {
2608 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2610 } else if (error == NFSERR_EXPIRED)
2611 (void) nfscl_hasexpired(clp, clidrev, p);
2615 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2616 /* Do renews for any DS sessions. */
2617 NFSLOCKMNT(clp->nfsc_nmp);
2618 /* Skip first entry, since the MDS is handled above. */
2619 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2621 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2622 while (dsp != NULL) {
2623 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2624 dsp->nfsclds_sess.nfsess_defunct == 0) {
2625 dsp->nfsclds_expire = NFSD_MONOSEC +
2627 NFSUNLOCKMNT(clp->nfsc_nmp);
2628 (void)nfsrpc_renew(clp, dsp, cred, p);
2631 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2633 NFSUNLOCKMNT(clp->nfsc_nmp);
2639 /* It's a Total Recall! */
2640 nfscl_totalrecall(clp);
2643 * Now, handle defunct owners.
2645 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2646 if (LIST_EMPTY(&owp->nfsow_open)) {
2647 if (owp->nfsow_defunct != 0)
2648 nfscl_freeopenowner(owp, 0);
2653 * Do the recall on any delegations. To avoid trouble, always
2654 * come back up here after having slept.
2658 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2659 while (dp != NULL) {
2660 ndp = TAILQ_NEXT(dp, nfsdl_list);
2661 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2663 * Wait for outstanding I/O ops to be done.
2665 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2667 nfsv4_unlock(&clp->nfsc_lock, 0);
2670 dp->nfsdl_rwlock.nfslock_lock |=
2672 msleep(&dp->nfsdl_rwlock,
2673 NFSCLSTATEMUTEXPTR, PVFS, "nfscld",
2675 if (NFSCL_FORCEDISM(mp))
2680 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2681 &islept, NFSCLSTATEMUTEXPTR, mp);
2682 if (igotlock == 0 && NFSCL_FORCEDISM(mp))
2688 newnfs_copycred(&dp->nfsdl_cred, cred);
2689 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2690 NULL, cred, p, 1, &vp);
2692 nfscl_cleandeleg(dp);
2693 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2695 LIST_REMOVE(dp, nfsdl_hash);
2696 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2698 nfsstatsv1.cldelegates--;
2702 * The nfsc_lock must be released before doing
2703 * vrele(), since it might call nfs_inactive().
2704 * For the unlikely case where the vnode failed
2705 * to be acquired by nfscl_recalldeleg(), a
2706 * VOP_RECLAIM() should be in progress and it
2707 * will return the delegation.
2709 nfsv4_unlock(&clp->nfsc_lock, 0);
2722 * Clear out old delegations, if we are above the high water
2723 * mark. Only clear out ones with no state related to them.
2724 * The tailq list is in LRU order.
2726 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2727 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2728 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2729 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2730 dp->nfsdl_rwlock.nfslock_lock == 0 &&
2731 dp->nfsdl_timestamp < NFSD_MONOSEC &&
2732 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2733 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2735 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2736 op = LIST_FIRST(&owp->nfsow_open);
2743 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2744 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2751 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2752 LIST_REMOVE(dp, nfsdl_hash);
2753 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2755 nfsstatsv1.cldelegates--;
2761 nfsv4_unlock(&clp->nfsc_lock, 0);
2764 * Do the recall on any layouts. To avoid trouble, always
2765 * come back up here after having slept.
2769 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2770 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2772 * Wait for outstanding I/O ops to be done.
2774 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2775 (lyp->nfsly_lock.nfslock_lock &
2776 NFSV4LOCK_LOCK) != 0) {
2777 lyp->nfsly_lock.nfslock_lock |=
2779 msleep(&lyp->nfsly_lock.nfslock_lock,
2780 NFSCLSTATEMUTEXPTR, PVFS, "nfslyp",
2782 if (NFSCL_FORCEDISM(mp))
2786 /* Move the layout to the recall list. */
2787 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2789 LIST_REMOVE(lyp, nfsly_hash);
2790 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2792 /* Handle any layout commits. */
2793 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2794 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2795 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2797 NFSCL_DEBUG(3, "do layoutcommit\n");
2798 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2806 /* Now, look for stale layouts. */
2807 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2808 while (lyp != NULL) {
2809 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2810 if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2811 (lyp->nfsly_flags & NFSLY_RECALL) == 0 &&
2812 lyp->nfsly_lock.nfslock_usecnt == 0 &&
2813 lyp->nfsly_lock.nfslock_lock == 0) {
2814 NFSCL_DEBUG(4, "ret stale lay=%d\n",
2816 recallp = malloc(sizeof(*recallp),
2817 M_NFSLAYRECALL, M_NOWAIT);
2818 if (recallp == NULL)
2820 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2821 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2822 lyp->nfsly_stateid.seqid, 0, 0, NULL,
2829 * Free up any unreferenced device info structures.
2831 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
2832 if (dip->nfsdi_layoutrefs == 0 &&
2833 dip->nfsdi_refcnt == 0) {
2834 NFSCL_DEBUG(4, "freeing devinfo\n");
2835 LIST_REMOVE(dip, nfsdi_list);
2836 nfscl_freedevinfo(dip);
2841 /* Do layout return(s), as required. */
2842 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
2843 TAILQ_REMOVE(&rlh, lyp, nfsly_list);
2844 NFSCL_DEBUG(4, "ret layout\n");
2845 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
2846 nfscl_freelayout(lyp);
2850 * Delegreturn any delegations cleaned out or recalled.
2852 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2853 newnfs_copycred(&dp->nfsdl_cred, cred);
2854 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2855 TAILQ_REMOVE(&dh, dp, nfsdl_list);
2856 free(dp, M_NFSCLDELEG);
2861 * Call nfscl_cleanupkext() once per second to check for
2862 * open/lock owners where the process has exited.
2864 mytime = NFSD_MONOSEC;
2865 if (prevsec != mytime) {
2867 nfscl_cleanupkext(clp, &lfh);
2871 * Do a ReleaseLockOwner for all lock owners where the
2872 * associated process no longer exists, as found by
2873 * nfscl_cleanupkext().
2875 newnfs_setroot(cred);
2876 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
2877 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
2879 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
2880 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
2882 nfscl_freelockowner(lp, 0);
2889 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2890 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
2893 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2894 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2897 wakeup((caddr_t)clp);
2905 * Initiate state recovery. Called when NFSERR_STALECLIENTID,
2906 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
2909 nfscl_initiate_recovery(struct nfsclclient *clp)
2915 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2917 wakeup((caddr_t)clp);
2921 * Dump out the state stuff for debugging.
2924 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
2925 int lockowner, int locks)
2927 struct nfsclclient *clp;
2928 struct nfsclowner *owp;
2929 struct nfsclopen *op;
2930 struct nfscllockowner *lp;
2931 struct nfscllock *lop;
2932 struct nfscldeleg *dp;
2936 printf("nfscl dumpstate NULL clp\n");
2940 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2941 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2942 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2943 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2944 owp->nfsow_owner[0], owp->nfsow_owner[1],
2945 owp->nfsow_owner[2], owp->nfsow_owner[3],
2947 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2949 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2950 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2951 op->nfso_stateid.other[2], op->nfso_opencnt,
2953 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2955 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2956 lp->nfsl_owner[0], lp->nfsl_owner[1],
2957 lp->nfsl_owner[2], lp->nfsl_owner[3],
2959 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2960 lp->nfsl_stateid.other[2]);
2961 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2964 printf("lck typ=%d fst=%ju end=%ju\n",
2965 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2966 (intmax_t)lop->nfslo_end);
2968 printf("lck typ=%d fst=%qd end=%qd\n",
2969 lop->nfslo_type, lop->nfslo_first,
2977 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2978 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2979 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2980 owp->nfsow_owner[0], owp->nfsow_owner[1],
2981 owp->nfsow_owner[2], owp->nfsow_owner[3],
2983 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2985 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2986 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2987 op->nfso_stateid.other[2], op->nfso_opencnt,
2989 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2991 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2992 lp->nfsl_owner[0], lp->nfsl_owner[1],
2993 lp->nfsl_owner[2], lp->nfsl_owner[3],
2995 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2996 lp->nfsl_stateid.other[2]);
2997 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3000 printf("lck typ=%d fst=%ju end=%ju\n",
3001 lop->nfslo_type, (intmax_t)lop->nfslo_first,
3002 (intmax_t)lop->nfslo_end);
3004 printf("lck typ=%d fst=%qd end=%qd\n",
3005 lop->nfslo_type, lop->nfslo_first,
3016 * Check for duplicate open owners and opens.
3017 * (Only used as a diagnostic aid.)
3020 nfscl_dupopen(vnode_t vp, int dupopens)
3022 struct nfsclclient *clp;
3023 struct nfsclowner *owp, *owp2;
3024 struct nfsclopen *op, *op2;
3027 clp = VFSTONFS(vp->v_mount)->nm_clp;
3029 printf("nfscl dupopen NULL clp\n");
3032 nfhp = VTONFS(vp)->n_fhp;
3036 * First, search for duplicate owners.
3037 * These should never happen!
3039 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3040 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3042 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
3043 NFSV4CL_LOCKNAMELEN)) {
3045 printf("DUP OWNER\n");
3046 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3053 * Now, search for duplicate stateids.
3054 * These shouldn't happen, either.
3056 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3057 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3058 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3059 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3061 (op->nfso_stateid.other[0] != 0 ||
3062 op->nfso_stateid.other[1] != 0 ||
3063 op->nfso_stateid.other[2] != 0) &&
3064 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
3065 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
3066 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
3068 printf("DUP STATEID\n");
3069 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3078 * Now search for duplicate opens.
3079 * Duplicate opens for the same owner
3080 * should never occur. Other duplicates are
3081 * possible and are checked for if "dupopens"
3084 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3085 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3086 if (nfhp->nfh_len == op2->nfso_fhlen &&
3087 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3088 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3089 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3090 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3091 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3092 (!NFSBCMP(op->nfso_own->nfsow_owner,
3093 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3095 if (!NFSBCMP(op->nfso_own->nfsow_owner,
3096 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3098 printf("BADDUP OPEN\n");
3101 printf("DUP OPEN\n");
3103 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0,
3116 * During close, find an open that needs to be dereferenced and
3117 * dereference it. If there are no more opens for this file,
3118 * log a message to that effect.
3119 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3120 * on the file's vnode.
3121 * This is the safe way, since it is difficult to identify
3122 * which open the close is for and I/O can be performed after the
3123 * close(2) system call when a file is mmap'd.
3124 * If it returns 0 for success, there will be a referenced
3125 * clp returned via clpp.
3128 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3130 struct nfsclclient *clp;
3131 struct nfsclowner *owp;
3132 struct nfsclopen *op;
3133 struct nfscldeleg *dp;
3137 error = nfscl_getcl(vp->v_mount, NULL, NULL, 1, &clp);
3142 nfhp = VTONFS(vp)->n_fhp;
3146 * First, look for one under a delegation that was locally issued
3147 * and just decrement the opencnt for it. Since all my Opens against
3148 * the server are DENY_NONE, I don't see a problem with hanging
3149 * onto them. (It is much easier to use one of the extant Opens
3150 * that I already have on the server when a Delegation is recalled
3151 * than to do fresh Opens.) Someday, I might need to rethink this, but.
3153 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3155 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3156 op = LIST_FIRST(&owp->nfsow_open);
3159 * Since a delegation is for a file, there
3160 * should never be more than one open for
3163 if (LIST_NEXT(op, nfso_list) != NULL)
3164 panic("nfscdeleg opens");
3165 if (notdecr && op->nfso_opencnt > 0) {
3174 /* Now process the opens against the server. */
3175 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3176 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3177 if (op->nfso_fhlen == nfhp->nfh_len &&
3178 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3180 /* Found an open, decrement cnt if possible */
3181 if (notdecr && op->nfso_opencnt > 0) {
3186 * There are more opens, so just return.
3188 if (op->nfso_opencnt > 0) {
3197 printf("nfscl: never fnd open\n");
3202 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3204 struct nfsclclient *clp;
3205 struct nfsclowner *owp, *nowp;
3206 struct nfsclopen *op;
3207 struct nfscldeleg *dp;
3209 struct nfsclrecalllayout *recallp;
3212 error = nfscl_getcl(vp->v_mount, NULL, NULL, 1, &clp);
3217 nfhp = VTONFS(vp)->n_fhp;
3218 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
3221 * First get rid of the local Open structures, which should be no
3224 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3226 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3227 op = LIST_FIRST(&owp->nfsow_open);
3229 KASSERT((op->nfso_opencnt == 0),
3230 ("nfscl: bad open cnt on deleg"));
3231 nfscl_freeopen(op, 1);
3233 nfscl_freeopenowner(owp, 1);
3237 /* Return any layouts marked return on close. */
3238 nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp);
3240 /* Now process the opens against the server. */
3242 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3243 op = LIST_FIRST(&owp->nfsow_open);
3244 while (op != NULL) {
3245 if (op->nfso_fhlen == nfhp->nfh_len &&
3246 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3248 /* Found an open, close it. */
3250 KASSERT((op->nfso_opencnt == 0),
3251 ("nfscl: bad open cnt on server (%d)",
3255 nfsrpc_doclose(VFSTONFS(vp->v_mount), op, p);
3259 op = LIST_NEXT(op, nfso_list);
3264 * recallp has been set NULL by nfscl_retoncloselayout() if it was
3265 * used by the function, but calling free() with a NULL pointer is ok.
3267 free(recallp, M_NFSLAYRECALL);
3272 * Return all delegations on this client.
3273 * (Must be called with client sleep lock.)
3276 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
3278 struct nfscldeleg *dp, *ndp;
3281 cred = newnfs_getcred();
3282 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3283 nfscl_cleandeleg(dp);
3284 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3285 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
3291 * Return any delegation for this vp.
3294 nfscl_delegreturnvp(vnode_t vp, NFSPROC_T *p)
3296 struct nfsclclient *clp;
3297 struct nfscldeleg *dp;
3302 cred = newnfs_getcred();
3304 clp = VFSTONFS(vp->v_mount)->nm_clp;
3305 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3306 np->n_fhp->nfh_len);
3308 nfscl_cleandeleg(dp);
3309 nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
3311 newnfs_copycred(&dp->nfsdl_cred, cred);
3312 nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3313 free(dp, M_NFSCLDELEG);
3320 * Do a callback RPC.
3323 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3325 int clist, gotseq_ok, i, j, k, op, rcalls;
3327 struct nfsclclient *clp;
3328 struct nfscldeleg *dp = NULL;
3329 int numops, taglen = -1, error = 0, trunc __unused;
3330 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3331 u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3337 nfsattrbit_t attrbits, rattrbits;
3338 nfsv4stateid_t stateid;
3339 uint32_t seqid, slotid = 0, highslot, cachethis __unused;
3340 uint8_t sessionid[NFSX_V4SESSIONID];
3342 struct nfscllayout *lyp;
3343 uint64_t filesid[2], len, off;
3344 int changed, gotone, laytype, recalltype;
3346 struct nfsclrecalllayout *recallp = NULL;
3347 struct nfsclsession *tsep;
3351 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3352 taglen = fxdr_unsigned(int, *tl);
3357 if (taglen <= NFSV4_SMALLSTR)
3360 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3361 error = nfsrv_mtostr(nd, tagstr, taglen);
3363 if (taglen > NFSV4_SMALLSTR)
3364 free(tagstr, M_TEMP);
3368 (void) nfsm_strtom(nd, tag, taglen);
3369 if (taglen > NFSV4_SMALLSTR) {
3370 free(tagstr, M_TEMP);
3372 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3373 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3374 minorvers = fxdr_unsigned(u_int32_t, *tl++);
3375 if (minorvers != NFSV4_MINORVERSION &&
3376 minorvers != NFSV41_MINORVERSION &&
3377 minorvers != NFSV42_MINORVERSION)
3378 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3379 cbident = fxdr_unsigned(u_int32_t, *tl++);
3383 numops = fxdr_unsigned(int, *tl);
3385 * Loop around doing the sub ops.
3387 for (i = 0; i < numops; i++) {
3388 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3389 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3391 op = fxdr_unsigned(int, *tl);
3392 if (op < NFSV4OP_CBGETATTR ||
3393 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3394 (op > NFSV4OP_CBNOTIFYDEVID &&
3395 minorvers == NFSV41_MINORVERSION) ||
3396 (op > NFSV4OP_CBOFFLOAD &&
3397 minorvers == NFSV42_MINORVERSION)) {
3398 nd->nd_repstat = NFSERR_OPILLEGAL;
3399 *repp = nfscl_errmap(nd, minorvers);
3403 nd->nd_procnum = op;
3404 if (op < NFSV42_CBNOPS)
3405 nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
3407 case NFSV4OP_CBGETATTR:
3408 NFSCL_DEBUG(4, "cbgetattr\n");
3411 error = nfsm_getfh(nd, &nfhp);
3413 error = nfsrv_getattrbits(nd, &attrbits,
3415 if (error == 0 && i == 0 &&
3416 minorvers != NFSV4_MINORVERSION)
3417 error = NFSERR_OPNOTINSESS;
3419 mp = nfscl_getmnt(minorvers, sessionid, cbident,
3422 error = NFSERR_SERVERFAULT;
3425 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3426 nfhp->nfh_len, p, &np);
3431 NFSZERO_ATTRBIT(&rattrbits);
3433 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3436 if (NFSISSET_ATTRBIT(&attrbits,
3439 va.va_size = np->n_size;
3443 NFSSETBIT_ATTRBIT(&rattrbits,
3446 if (NFSISSET_ATTRBIT(&attrbits,
3447 NFSATTRBIT_CHANGE)) {
3451 (np->n_flag & NDELEGMOD))
3453 NFSSETBIT_ATTRBIT(&rattrbits,
3457 error = NFSERR_SERVERFAULT;
3465 free(nfhp, M_NFSFH);
3467 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3468 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3471 case NFSV4OP_CBRECALL:
3472 NFSCL_DEBUG(4, "cbrecall\n");
3473 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3475 stateid.seqid = *tl++;
3476 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3478 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3479 trunc = fxdr_unsigned(int, *tl);
3480 error = nfsm_getfh(nd, &nfhp);
3481 if (error == 0 && i == 0 &&
3482 minorvers != NFSV4_MINORVERSION)
3483 error = NFSERR_OPNOTINSESS;
3486 if (minorvers == NFSV4_MINORVERSION)
3487 clp = nfscl_getclnt(cbident);
3489 clp = nfscl_getclntsess(sessionid);
3491 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3493 if (dp != NULL && (dp->nfsdl_flags &
3494 NFSCLDL_DELEGRET) == 0) {
3497 wakeup((caddr_t)clp);
3500 error = NFSERR_SERVERFAULT;
3505 free(nfhp, M_NFSFH);
3507 case NFSV4OP_CBLAYOUTRECALL:
3508 NFSCL_DEBUG(4, "cblayrec\n");
3510 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3511 laytype = fxdr_unsigned(int, *tl++);
3512 iomode = fxdr_unsigned(uint32_t, *tl++);
3513 if (newnfs_true == *tl++)
3517 recalltype = fxdr_unsigned(int, *tl);
3518 NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
3519 laytype, iomode, changed, recalltype);
3520 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3522 if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
3523 laytype != NFSLAYOUT_FLEXFILE)
3524 error = NFSERR_NOMATCHLAYOUT;
3525 else if (recalltype == NFSLAYOUTRETURN_FILE) {
3526 error = nfsm_getfh(nd, &nfhp);
3527 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3530 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3532 off = fxdr_hyper(tl); tl += 2;
3533 len = fxdr_hyper(tl); tl += 2;
3534 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3535 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3536 if (minorvers == NFSV4_MINORVERSION)
3537 error = NFSERR_NOTSUPP;
3539 error = NFSERR_OPNOTINSESS;
3540 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
3541 (uintmax_t)off, (uintmax_t)len,
3542 stateid.seqid, error);
3545 clp = nfscl_getclntsess(sessionid);
3546 NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3548 lyp = nfscl_findlayout(clp,
3551 NFSCL_DEBUG(4, "cblyp=%p\n",
3556 NFSLY_FLEXFILE)) != 0 &&
3557 !NFSBCMP(stateid.other,
3558 lyp->nfsly_stateid.other,
3559 NFSX_STATEIDOTHER)) {
3569 lyp->nfsly_stateid.seqid)
3570 lyp->nfsly_stateid.seqid =
3578 lyp->nfsly_stateid.seqid);
3581 NFSERR_NOMATCHLAYOUT;
3583 error = NFSERR_NOMATCHLAYOUT;
3586 free(nfhp, M_NFSFH);
3587 } else if (recalltype == NFSLAYOUTRETURN_FSID) {
3588 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3589 filesid[0] = fxdr_hyper(tl); tl += 2;
3590 filesid[1] = fxdr_hyper(tl); tl += 2;
3593 clp = nfscl_getclntsess(sessionid);
3595 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3597 if (lyp->nfsly_filesid[0] ==
3599 lyp->nfsly_filesid[1] ==
3606 lyp->nfsly_stateid.seqid,
3616 error = NFSERR_NOMATCHLAYOUT;
3618 error = NFSERR_NOMATCHLAYOUT;
3620 } else if (recalltype == NFSLAYOUTRETURN_ALL) {
3623 clp = nfscl_getclntsess(sessionid);
3625 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3627 error = nfscl_layoutrecall(
3628 recalltype, lyp, iomode, 0,
3630 lyp->nfsly_stateid.seqid,
3631 0, 0, NULL, recallp);
3638 error = NFSERR_NOMATCHLAYOUT;
3640 error = NFSERR_NOMATCHLAYOUT;
3643 error = NFSERR_NOMATCHLAYOUT;
3644 if (recallp != NULL) {
3645 free(recallp, M_NFSLAYRECALL);
3649 case NFSV4OP_CBSEQUENCE:
3650 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3652 bcopy(tl, sessionid, NFSX_V4SESSIONID);
3653 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3654 seqid = fxdr_unsigned(uint32_t, *tl++);
3655 slotid = fxdr_unsigned(uint32_t, *tl++);
3656 highslot = fxdr_unsigned(uint32_t, *tl++);
3658 /* Throw away the referring call stuff. */
3659 clist = fxdr_unsigned(int, *tl);
3660 for (j = 0; j < clist; j++) {
3661 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3663 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3664 rcalls = fxdr_unsigned(int, *tl);
3665 for (k = 0; k < rcalls; k++) {
3666 NFSM_DISSECT(tl, uint32_t *,
3672 clp = nfscl_getclntsess(sessionid);
3674 error = NFSERR_SERVERFAULT;
3676 error = NFSERR_SEQUENCEPOS;
3678 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3679 error = nfsv4_seqsession(seqid, slotid,
3680 highslot, tsep->nfsess_cbslots, &rep,
3681 tsep->nfsess_backslots);
3684 if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
3688 * Handle a reply for a retried
3689 * callback. The reply will be
3690 * re-inserted in the session cache
3691 * by the nfsv4_seqsess_cacherep() call
3694 KASSERT(error == NFSERR_REPLYFROMCACHE,
3695 ("cbsequence: non-NULL rep"));
3696 NFSCL_DEBUG(4, "Got cbretry\n");
3697 m_freem(nd->nd_mreq);
3702 NFSM_BUILD(tl, uint32_t *,
3703 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3704 bcopy(sessionid, tl, NFSX_V4SESSIONID);
3705 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3706 *tl++ = txdr_unsigned(seqid);
3707 *tl++ = txdr_unsigned(slotid);
3708 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3709 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3713 if (i == 0 && minorvers != NFSV4_MINORVERSION)
3714 error = NFSERR_OPNOTINSESS;
3716 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3717 error = NFSERR_NOTSUPP;
3722 if (error == EBADRPC || error == NFSERR_BADXDR) {
3723 nd->nd_repstat = NFSERR_BADXDR;
3725 nd->nd_repstat = error;
3730 if (nd->nd_repstat) {
3731 *repp = nfscl_errmap(nd, minorvers);
3734 *repp = 0; /* NFS4_OK */
3737 if (recallp != NULL)
3738 free(recallp, M_NFSLAYRECALL);
3740 if (error == EBADRPC || error == NFSERR_BADXDR)
3741 nd->nd_repstat = NFSERR_BADXDR;
3743 printf("nfsv4 comperr1=%d\n", error);
3746 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3750 *retopsp = txdr_unsigned(retops);
3752 *nd->nd_errp = nfscl_errmap(nd, minorvers);
3754 if (gotseq_ok != 0) {
3755 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3757 clp = nfscl_getclntsess(sessionid);
3759 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3760 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3771 * Generate the next cbident value. Basically just increment a static value
3772 * and then check that it isn't already in the list, if it has wrapped around.
3775 nfscl_nextcbident(void)
3777 struct nfsclclient *clp;
3779 static u_int32_t nextcbident = 0;
3780 static int haswrapped = 0;
3783 if (nextcbident == 0)
3787 * Search the clientid list for one already using this cbident.
3792 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3793 if (clp->nfsc_cbident == nextcbident) {
3803 return (nextcbident);
3807 * Get the mount point related to a given cbident or session and busy it.
3810 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
3811 struct nfsclclient **clpp)
3813 struct nfsclclient *clp;
3816 struct nfsclsession *tsep;
3820 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3821 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3822 if (minorvers == NFSV4_MINORVERSION) {
3823 if (clp->nfsc_cbident == cbident)
3825 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3833 mp = clp->nfsc_nmp->nm_mountp;
3836 error = vfs_busy(mp, 0);
3845 * Get the clientid pointer related to a given cbident.
3847 static struct nfsclclient *
3848 nfscl_getclnt(u_int32_t cbident)
3850 struct nfsclclient *clp;
3852 LIST_FOREACH(clp, &nfsclhead, nfsc_list)
3853 if (clp->nfsc_cbident == cbident)
3859 * Get the clientid pointer related to a given sessionid.
3861 static struct nfsclclient *
3862 nfscl_getclntsess(uint8_t *sessionid)
3864 struct nfsclclient *clp;
3865 struct nfsclsession *tsep;
3867 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3868 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3869 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3877 * Search for a lock conflict locally on the client. A conflict occurs if
3878 * - not same owner and overlapping byte range and at least one of them is
3879 * a write lock or this is an unlock.
3882 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
3883 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
3884 struct nfscllock **lopp)
3886 struct nfsclowner *owp;
3887 struct nfsclopen *op;
3891 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3895 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3896 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3897 if (op->nfso_fhlen == fhlen &&
3898 !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
3899 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
3910 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3911 u_int8_t *own, struct nfscllock **lopp)
3913 struct nfscllockowner *lp;
3914 struct nfscllock *lop;
3916 LIST_FOREACH(lp, lhp, nfsl_list) {
3917 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
3918 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3919 if (lop->nfslo_first >= nlop->nfslo_end)
3921 if (lop->nfslo_end <= nlop->nfslo_first)
3923 if (lop->nfslo_type == F_WRLCK ||
3924 nlop->nfslo_type == F_WRLCK ||
3925 nlop->nfslo_type == F_UNLCK) {
3928 return (NFSERR_DENIED);
3937 * Check for a local conflicting lock.
3940 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
3941 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
3943 struct nfscllock *lop, nlck;
3944 struct nfscldeleg *dp;
3946 u_int8_t own[NFSV4CL_LOCKNAMELEN];
3949 nlck.nfslo_type = fl->l_type;
3950 nlck.nfslo_first = off;
3951 if (len == NFS64BITSSET) {
3952 nlck.nfslo_end = NFS64BITSSET;
3954 nlck.nfslo_end = off + len;
3955 if (nlck.nfslo_end <= nlck.nfslo_first)
3956 return (NFSERR_INVAL);
3959 nfscl_filllockowner(id, own, flags);
3961 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3962 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
3963 &nlck, own, dp, &lop);
3965 fl->l_whence = SEEK_SET;
3966 fl->l_start = lop->nfslo_first;
3967 if (lop->nfslo_end == NFS64BITSSET)
3970 fl->l_len = lop->nfslo_end - lop->nfslo_first;
3971 fl->l_pid = (pid_t)0;
3972 fl->l_type = lop->nfslo_type;
3973 error = -1; /* no RPC required */
3974 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
3975 fl->l_type == F_RDLCK)) {
3977 * The delegation ensures that there isn't a conflicting
3978 * lock on the server, so return -1 to indicate an RPC
3981 fl->l_type = F_UNLCK;
3989 * Handle Recall of a delegation.
3990 * The clp must be exclusive locked when this is called.
3993 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
3994 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
3995 int called_from_renewthread, vnode_t *vpp)
3997 struct nfsclowner *owp, *lowp, *nowp;
3998 struct nfsclopen *op, *lop;
3999 struct nfscllockowner *lp;
4000 struct nfscllock *lckp;
4005 KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL"));
4008 * First, get a vnode for the file. This is needed to do RPCs.
4010 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
4011 dp->nfsdl_fhlen, p, &np);
4014 * File isn't open, so nothing to move over to the
4024 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
4027 * Ok, if it's a write delegation, flush data to the server, so
4028 * that close/open consistency is retained.
4032 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
4033 np->n_flag |= NDELEGRECALL;
4035 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
4037 np->n_flag &= ~NDELEGRECALL;
4039 NFSINVALATTRCACHE(np);
4041 if (ret == EIO && called_from_renewthread != 0) {
4043 * If the flush failed with EIO for the renew thread,
4044 * return now, so that the dirty buffer will be flushed
4051 * Now, for each openowner with opens issued locally, move them
4052 * over to state against the server.
4054 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
4055 lop = LIST_FIRST(&lowp->nfsow_open);
4057 if (LIST_NEXT(lop, nfso_list) != NULL)
4058 panic("nfsdlg mult opens");
4060 * Look for the same openowner against the server.
4062 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
4063 if (!NFSBCMP(lowp->nfsow_owner,
4064 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
4065 newnfs_copycred(&dp->nfsdl_cred, cred);
4066 ret = nfscl_moveopen(vp, clp, nmp, lop,
4068 if (ret == NFSERR_STALECLIENTID ||
4069 ret == NFSERR_STALEDONTRECOVER ||
4070 ret == NFSERR_BADSESSION)
4073 nfscl_freeopen(lop, 1);
4082 * If no openowner found, create one and get an open
4087 sizeof (struct nfsclowner), M_NFSCLOWNER,
4089 nfscl_newopen(clp, NULL, &owp, &nowp, &op,
4090 NULL, lowp->nfsow_owner, dp->nfsdl_fh,
4091 dp->nfsdl_fhlen, NULL, NULL);
4092 newnfs_copycred(&dp->nfsdl_cred, cred);
4093 ret = nfscl_moveopen(vp, clp, nmp, lop,
4096 nfscl_freeopenowner(owp, 0);
4097 if (ret == NFSERR_STALECLIENTID ||
4098 ret == NFSERR_STALEDONTRECOVER ||
4099 ret == NFSERR_BADSESSION)
4102 nfscl_freeopen(lop, 1);
4112 * Now, get byte range locks for any locks done locally.
4114 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4115 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
4116 newnfs_copycred(&dp->nfsdl_cred, cred);
4117 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
4118 if (ret == NFSERR_STALESTATEID ||
4119 ret == NFSERR_STALEDONTRECOVER ||
4120 ret == NFSERR_STALECLIENTID ||
4121 ret == NFSERR_BADSESSION)
4131 * Move a locally issued open over to an owner on the state list.
4132 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
4133 * returns with it unlocked.
4136 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4137 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
4138 struct ucred *cred, NFSPROC_T *p)
4140 struct nfsclopen *op, *nop;
4141 struct nfscldeleg *ndp;
4143 int error = 0, newone;
4146 * First, look for an appropriate open, If found, just increment the
4149 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4150 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4151 op->nfso_fhlen == lop->nfso_fhlen &&
4152 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4153 op->nfso_opencnt += lop->nfso_opencnt;
4154 nfscl_freeopen(lop, 1);
4159 /* No appropriate open, so we have to do one against the server. */
4161 nop = malloc(sizeof (struct nfsclopen) +
4162 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4164 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4165 lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
4167 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4168 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4169 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4172 nfscl_freeopen(op, 0);
4174 op->nfso_mode |= lop->nfso_mode;
4175 op->nfso_opencnt += lop->nfso_opencnt;
4176 nfscl_freeopen(lop, 1);
4179 free(nop, M_NFSCLOPEN);
4182 * What should I do with the returned delegation, since the
4183 * delegation is being recalled? For now, just printf and
4186 printf("Moveopen returned deleg\n");
4187 free(ndp, M_NFSCLDELEG);
4193 * Recall all delegations on this client.
4196 nfscl_totalrecall(struct nfsclclient *clp)
4198 struct nfscldeleg *dp;
4200 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4201 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4202 dp->nfsdl_flags |= NFSCLDL_RECALL;
4207 * Relock byte ranges. Called for delegation recall and state expiry.
4210 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4211 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4214 struct nfscllockowner *nlp;
4217 int error, newone, donelocally;
4219 off = lop->nfslo_first;
4220 len = lop->nfslo_end - lop->nfslo_first;
4221 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4222 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4223 lp->nfsl_openowner, &nlp, &newone, &donelocally);
4224 if (error || donelocally)
4226 nfhp = VTONFS(vp)->n_fhp;
4227 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4228 nfhp->nfh_len, nlp, newone, 0, off,
4229 len, lop->nfslo_type, cred, p);
4231 nfscl_freelockowner(nlp, 0);
4236 * Called to re-open a file. Basically get a vnode for the file handle
4237 * and then call nfsrpc_openrpc() to do the rest.
4240 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4241 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4242 struct ucred *cred, NFSPROC_T *p)
4248 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4252 if (np->n_v4 != NULL) {
4253 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4254 np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4255 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4265 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4266 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4270 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4271 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4272 u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4273 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4278 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4279 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4281 if (error == NFSERR_DELAY)
4282 (void) nfs_catnap(PZERO, error, "nfstryop");
4283 } while (error == NFSERR_DELAY);
4284 if (error == EAUTH || error == EACCES) {
4285 /* Try again using system credentials */
4286 newnfs_setroot(cred);
4288 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4289 newfhlen, mode, op, name, namelen, ndpp, reclaim,
4290 delegtype, cred, p, 1, 0);
4291 if (error == NFSERR_DELAY)
4292 (void) nfs_catnap(PZERO, error, "nfstryop");
4293 } while (error == NFSERR_DELAY);
4299 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4300 * NFSERR_DELAY. Also, retry with system credentials, if the provided
4304 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4305 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4306 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4308 struct nfsrv_descript nfsd, *nd = &nfsd;
4312 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4313 reclaim, off, len, type, cred, p, 0);
4314 if (!error && nd->nd_repstat == NFSERR_DELAY)
4315 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4317 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4319 error = nd->nd_repstat;
4320 if (error == EAUTH || error == EACCES) {
4321 /* Try again using root credentials */
4322 newnfs_setroot(cred);
4324 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4325 newone, reclaim, off, len, type, cred, p, 1);
4326 if (!error && nd->nd_repstat == NFSERR_DELAY)
4327 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4329 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4331 error = nd->nd_repstat;
4337 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4338 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4342 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4343 struct nfsmount *nmp, NFSPROC_T *p)
4348 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4349 if (error == NFSERR_DELAY)
4350 (void) nfs_catnap(PZERO, error, "nfstrydp");
4351 } while (error == NFSERR_DELAY);
4352 if (error == EAUTH || error == EACCES) {
4353 /* Try again using system credentials */
4354 newnfs_setroot(cred);
4356 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4357 if (error == NFSERR_DELAY)
4358 (void) nfs_catnap(PZERO, error, "nfstrydp");
4359 } while (error == NFSERR_DELAY);
4365 * Try a close against the server. Just call nfsrpc_closerpc(),
4366 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4370 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4371 struct nfsmount *nmp, NFSPROC_T *p)
4373 struct nfsrv_descript nfsd, *nd = &nfsd;
4377 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4378 if (error == NFSERR_DELAY)
4379 (void) nfs_catnap(PZERO, error, "nfstrycl");
4380 } while (error == NFSERR_DELAY);
4381 if (error == EAUTH || error == EACCES) {
4382 /* Try again using system credentials */
4383 newnfs_setroot(cred);
4385 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4386 if (error == NFSERR_DELAY)
4387 (void) nfs_catnap(PZERO, error, "nfstrycl");
4388 } while (error == NFSERR_DELAY);
4394 * Decide if a delegation on a file permits close without flushing writes
4395 * to the server. This might be a big performance win in some environments.
4396 * (Not useful until the client does caching on local stable storage.)
4399 nfscl_mustflush(vnode_t vp)
4401 struct nfsclclient *clp;
4402 struct nfscldeleg *dp;
4404 struct nfsmount *nmp;
4407 nmp = VFSTONFS(vp->v_mount);
4408 if (!NFSHASNFSV4(nmp))
4411 clp = nfscl_findcl(nmp);
4416 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4417 if (dp != NULL && (dp->nfsdl_flags &
4418 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4420 (dp->nfsdl_sizelimit >= np->n_size ||
4421 !NFSHASSTRICT3530(nmp))) {
4430 * See if a (write) delegation exists for this file.
4433 nfscl_nodeleg(vnode_t vp, int writedeleg)
4435 struct nfsclclient *clp;
4436 struct nfscldeleg *dp;
4438 struct nfsmount *nmp;
4441 nmp = VFSTONFS(vp->v_mount);
4442 if (!NFSHASNFSV4(nmp))
4445 clp = nfscl_findcl(nmp);
4450 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4452 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4453 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4463 * Look for an associated delegation that should be DelegReturned.
4466 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4468 struct nfsclclient *clp;
4469 struct nfscldeleg *dp;
4470 struct nfsclowner *owp;
4471 struct nfscllockowner *lp;
4472 struct nfsmount *nmp;
4475 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4477 nmp = VFSTONFS(vp->v_mount);
4481 * Loop around waiting for:
4482 * - outstanding I/O operations on delegations to complete
4483 * - for a delegation on vp that has state, lock the client and
4485 * - return delegation with no state
4488 clp = nfscl_findcl(nmp);
4493 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4494 np->n_fhp->nfh_len);
4497 * Wait for outstanding I/O ops to be done.
4499 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4501 nfsv4_unlock(&clp->nfsc_lock, 0);
4504 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4505 (void) nfsmsleep(&dp->nfsdl_rwlock,
4506 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4510 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4511 if (!LIST_EMPTY(&owp->nfsow_open)) {
4517 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4518 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4524 if (needsrecall && !triedrecall) {
4525 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4528 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4529 &islept, NFSCLSTATEMUTEXPTR, NULL);
4536 cred = newnfs_getcred();
4537 newnfs_copycred(&dp->nfsdl_cred, cred);
4538 nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL);
4542 nfsv4_unlock(&clp->nfsc_lock, 0);
4546 *stp = dp->nfsdl_stateid;
4548 nfscl_cleandeleg(dp);
4549 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4552 nfsv4_unlock(&clp->nfsc_lock, 0);
4559 * Look for associated delegation(s) that should be DelegReturned.
4562 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4563 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4565 struct nfsclclient *clp;
4566 struct nfscldeleg *dp;
4567 struct nfsclowner *owp;
4568 struct nfscllockowner *lp;
4569 struct nfsmount *nmp;
4572 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4574 nmp = VFSTONFS(fvp->v_mount);
4579 * Loop around waiting for:
4580 * - outstanding I/O operations on delegations to complete
4581 * - for a delegation on fvp that has state, lock the client and
4583 * - return delegation(s) with no state.
4586 clp = nfscl_findcl(nmp);
4592 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4593 np->n_fhp->nfh_len);
4594 if (dp != NULL && *gotfdp == 0) {
4596 * Wait for outstanding I/O ops to be done.
4598 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4600 nfsv4_unlock(&clp->nfsc_lock, 0);
4603 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4604 (void) nfsmsleep(&dp->nfsdl_rwlock,
4605 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4609 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4610 if (!LIST_EMPTY(&owp->nfsow_open)) {
4616 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4617 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4623 if (needsrecall && !triedrecall) {
4624 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4627 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4628 &islept, NFSCLSTATEMUTEXPTR, NULL);
4635 cred = newnfs_getcred();
4636 newnfs_copycred(&dp->nfsdl_cred, cred);
4637 nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL);
4641 nfsv4_unlock(&clp->nfsc_lock, 0);
4645 *fstp = dp->nfsdl_stateid;
4648 nfscl_cleandeleg(dp);
4649 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4652 nfsv4_unlock(&clp->nfsc_lock, 0);
4657 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4658 np->n_fhp->nfh_len);
4659 if (dp != NULL && *gottdp == 0) {
4661 * Wait for outstanding I/O ops to be done.
4663 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4664 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4665 (void) nfsmsleep(&dp->nfsdl_rwlock,
4666 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4669 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4670 if (!LIST_EMPTY(&owp->nfsow_open)) {
4675 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4676 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4681 *tstp = dp->nfsdl_stateid;
4684 nfscl_cleandeleg(dp);
4685 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4694 * Get a reference on the clientid associated with the mount point.
4695 * Return 1 if success, 0 otherwise.
4698 nfscl_getref(struct nfsmount *nmp)
4700 struct nfsclclient *clp;
4703 clp = nfscl_findcl(nmp);
4708 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
4714 * Release a reference on a clientid acquired with the above call.
4717 nfscl_relref(struct nfsmount *nmp)
4719 struct nfsclclient *clp;
4722 clp = nfscl_findcl(nmp);
4727 nfsv4_relref(&clp->nfsc_lock);
4732 * Save the size attribute in the delegation, since the nfsnode
4736 nfscl_reclaimnode(vnode_t vp)
4738 struct nfsclclient *clp;
4739 struct nfscldeleg *dp;
4740 struct nfsnode *np = VTONFS(vp);
4741 struct nfsmount *nmp;
4743 nmp = VFSTONFS(vp->v_mount);
4744 if (!NFSHASNFSV4(nmp))
4747 clp = nfscl_findcl(nmp);
4752 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4753 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4754 dp->nfsdl_size = np->n_size;
4759 * Get the saved size attribute in the delegation, since it is a
4760 * newly allocated nfsnode.
4763 nfscl_newnode(vnode_t vp)
4765 struct nfsclclient *clp;
4766 struct nfscldeleg *dp;
4767 struct nfsnode *np = VTONFS(vp);
4768 struct nfsmount *nmp;
4770 nmp = VFSTONFS(vp->v_mount);
4771 if (!NFSHASNFSV4(nmp))
4774 clp = nfscl_findcl(nmp);
4779 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4780 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4781 np->n_size = dp->nfsdl_size;
4786 * If there is a valid write delegation for this file, set the modtime
4787 * to the local clock time.
4790 nfscl_delegmodtime(vnode_t vp)
4792 struct nfsclclient *clp;
4793 struct nfscldeleg *dp;
4794 struct nfsnode *np = VTONFS(vp);
4795 struct nfsmount *nmp;
4797 nmp = VFSTONFS(vp->v_mount);
4798 if (!NFSHASNFSV4(nmp))
4801 clp = nfscl_findcl(nmp);
4806 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4807 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4808 nanotime(&dp->nfsdl_modtime);
4809 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4815 * If there is a valid write delegation for this file with a modtime set,
4816 * put that modtime in mtime.
4819 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4821 struct nfsclclient *clp;
4822 struct nfscldeleg *dp;
4823 struct nfsnode *np = VTONFS(vp);
4824 struct nfsmount *nmp;
4826 nmp = VFSTONFS(vp->v_mount);
4827 if (!NFSHASNFSV4(nmp))
4830 clp = nfscl_findcl(nmp);
4835 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4837 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4838 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4839 *mtime = dp->nfsdl_modtime;
4844 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
4846 short *defaulterrp, *errp;
4848 if (!nd->nd_repstat)
4850 if (nd->nd_procnum == NFSPROC_NOOP)
4851 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4852 if (nd->nd_repstat == EBADRPC)
4853 return (txdr_unsigned(NFSERR_BADXDR));
4854 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4855 nd->nd_repstat == NFSERR_OPILLEGAL)
4856 return (txdr_unsigned(nd->nd_repstat));
4857 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
4858 minorvers > NFSV4_MINORVERSION) {
4859 /* NFSv4.n error. */
4860 return (txdr_unsigned(nd->nd_repstat));
4862 if (nd->nd_procnum < NFSV4OP_CBNOPS)
4863 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4865 return (txdr_unsigned(nd->nd_repstat));
4867 if (*errp == (short)nd->nd_repstat)
4868 return (txdr_unsigned(nd->nd_repstat));
4869 return (txdr_unsigned(*defaulterrp));
4873 * Called to find/add a layout to a client.
4874 * This function returns the layout with a refcnt (shared lock) upon
4875 * success (returns 0) or with no lock/refcnt on the layout when an
4876 * error is returned.
4877 * If a layout is passed in via lypp, it is locked (exclusively locked).
4880 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4881 nfsv4stateid_t *stateidp, int layouttype, int retonclose,
4882 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
4883 struct ucred *cred, NFSPROC_T *p)
4885 struct nfsclclient *clp;
4886 struct nfscllayout *lyp, *tlyp;
4887 struct nfsclflayout *flp;
4888 struct nfsnode *np = VTONFS(vp);
4890 int layout_passed_in;
4892 mp = nmp->nm_mountp;
4893 layout_passed_in = 1;
4897 layout_passed_in = 0;
4898 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
4905 if (layout_passed_in != 0)
4906 nfsv4_unlock(&lyp->nfsly_lock, 0);
4909 free(tlyp, M_NFSLAYOUT);
4914 * Although no lyp was passed in, another thread might have
4915 * allocated one. If one is found, just increment it's ref
4916 * count and return it.
4918 lyp = nfscl_findlayout(clp, fhp, fhlen);
4922 lyp->nfsly_stateid.seqid = stateidp->seqid;
4923 lyp->nfsly_stateid.other[0] = stateidp->other[0];
4924 lyp->nfsly_stateid.other[1] = stateidp->other[1];
4925 lyp->nfsly_stateid.other[2] = stateidp->other[2];
4926 lyp->nfsly_lastbyte = 0;
4927 LIST_INIT(&lyp->nfsly_flayread);
4928 LIST_INIT(&lyp->nfsly_flayrw);
4929 LIST_INIT(&lyp->nfsly_recall);
4930 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
4931 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
4932 lyp->nfsly_clp = clp;
4933 if (layouttype == NFSLAYOUT_FLEXFILE)
4934 lyp->nfsly_flags = NFSLY_FLEXFILE;
4936 lyp->nfsly_flags = NFSLY_FILES;
4937 if (retonclose != 0)
4938 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4939 lyp->nfsly_fhlen = fhlen;
4940 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
4941 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4942 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
4944 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4947 if (retonclose != 0)
4948 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4949 if (stateidp->seqid > lyp->nfsly_stateid.seqid)
4950 lyp->nfsly_stateid.seqid = stateidp->seqid;
4951 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4952 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4953 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4955 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
4956 if (NFSCL_FORCEDISM(mp)) {
4959 free(tlyp, M_NFSLAYOUT);
4963 } else if (stateidp->seqid > lyp->nfsly_stateid.seqid)
4964 lyp->nfsly_stateid.seqid = stateidp->seqid;
4966 /* Merge the new list of File Layouts into the list. */
4967 flp = LIST_FIRST(fhlp);
4969 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
4970 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
4972 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
4974 if (layout_passed_in != 0)
4975 nfsv4_unlock(&lyp->nfsly_lock, 1);
4978 free(tlyp, M_NFSLAYOUT);
4983 * Search for a layout by MDS file handle.
4984 * If one is found, it is returned with a refcnt (shared lock) iff
4985 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
4988 struct nfscllayout *
4989 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
4990 uint64_t off, struct nfsclflayout **retflpp, int *recalledp)
4992 struct nfscllayout *lyp;
4994 int error, igotlock;
4996 mp = clp->nfsc_nmp->nm_mountp;
5000 lyp = nfscl_findlayout(clp, fhp, fhlen);
5002 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5003 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5004 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5005 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5006 error = nfscl_findlayoutforio(lyp, off,
5007 NFSV4OPEN_ACCESSREAD, retflpp);
5009 nfsv4_getref(&lyp->nfsly_lock, NULL,
5010 NFSCLSTATEMUTEXPTR, mp);
5013 igotlock = nfsv4_lock(&lyp->nfsly_lock,
5014 1, NULL, NFSCLSTATEMUTEXPTR, mp);
5015 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
5018 if (NFSCL_FORCEDISM(mp)) {
5032 * Search for a layout by MDS file handle. If one is found, mark in to be
5033 * recalled, if it already marked "return on close".
5036 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
5037 int fhlen, struct nfsclrecalllayout **recallpp)
5039 struct nfscllayout *lyp;
5042 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) ||
5043 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5044 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
5046 lyp = nfscl_findlayout(clp, fhp, fhlen);
5047 if (lyp != NULL && (lyp->nfsly_flags & (NFSLY_RETONCLOSE |
5048 NFSLY_RECALL)) == NFSLY_RETONCLOSE) {
5050 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5051 iomode |= NFSLAYOUTIOMODE_READ;
5052 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5053 iomode |= NFSLAYOUTIOMODE_RW;
5054 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5055 0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
5057 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
5063 * Mark the layout to be recalled and with an error.
5064 * Also, disable the dsp from further use.
5067 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
5068 struct nfscllayout *lyp, struct nfsclds *dsp)
5070 struct nfsclrecalllayout *recallp;
5073 printf("DS being disabled, error=%d\n", stat);
5074 /* Set up the return of the layout. */
5075 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
5078 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5079 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5080 iomode |= NFSLAYOUTIOMODE_READ;
5081 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5082 iomode |= NFSLAYOUTIOMODE_RW;
5083 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5084 0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
5085 dp->nfsdi_deviceid, recallp);
5087 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
5090 free(recallp, M_NFSLAYRECALL);
5093 /* And shut the TCP connection down. */
5094 nfscl_cancelreqs(dsp);
5098 * Cancel all RPCs for this "dsp" by closing the connection.
5099 * Also, mark the session as defunct.
5100 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
5101 * cannot be shut down.
5104 nfscl_cancelreqs(struct nfsclds *dsp)
5106 struct __rpc_client *cl;
5107 static int non_event;
5110 if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
5111 dsp->nfsclds_sockp != NULL &&
5112 dsp->nfsclds_sockp->nr_client != NULL) {
5113 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
5114 cl = dsp->nfsclds_sockp->nr_client;
5115 dsp->nfsclds_sess.nfsess_defunct = 1;
5119 * This 1sec sleep is done to reduce the number of reconnect
5120 * attempts made on the DS while it has failed.
5122 tsleep(&non_event, PVFS, "ndscls", hz);
5129 * Dereference a layout.
5132 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
5137 nfsv4_unlock(&lyp->nfsly_lock, 0);
5139 nfsv4_relref(&lyp->nfsly_lock);
5144 * Search for a devinfo by deviceid. If one is found, return it after
5145 * acquiring a reference count on it.
5147 struct nfscldevinfo *
5148 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
5149 struct nfscldevinfo *dip)
5154 dip = nfscl_finddevinfo(clp, deviceid);
5156 dip->nfsdi_refcnt++;
5162 * Dereference a devinfo structure.
5165 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
5168 dip->nfsdi_refcnt--;
5169 if (dip->nfsdi_refcnt == 0)
5170 wakeup(&dip->nfsdi_refcnt);
5174 * Dereference a devinfo structure.
5177 nfscl_reldevinfo(struct nfscldevinfo *dip)
5181 nfscl_reldevinfo_locked(dip);
5186 * Find a layout for this file handle. Return NULL upon failure.
5188 static struct nfscllayout *
5189 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
5191 struct nfscllayout *lyp;
5193 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
5194 if (lyp->nfsly_fhlen == fhlen &&
5195 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
5201 * Find a devinfo for this deviceid. Return NULL upon failure.
5203 static struct nfscldevinfo *
5204 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
5206 struct nfscldevinfo *dip;
5208 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
5209 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5216 * Merge the new file layout list into the main one, maintaining it in
5217 * increasing offset order.
5220 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5221 struct nfsclflayouthead *newfhlp)
5223 struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5225 flp = LIST_FIRST(fhlp);
5227 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5228 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5230 flp = LIST_NEXT(flp, nfsfl_list);
5232 if (prevflp == NULL)
5233 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5235 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5241 * Add this nfscldevinfo to the client, if it doesn't already exist.
5242 * This function consumes the structure pointed at by dip, if not NULL.
5245 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
5246 struct nfsclflayout *flp)
5248 struct nfsclclient *clp;
5249 struct nfscldevinfo *tdip;
5257 free(dip, M_NFSDEVINFO);
5260 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5261 dev = flp->nfsfl_dev;
5263 dev = flp->nfsfl_ffm[ind].dev;
5264 tdip = nfscl_finddevinfo(clp, dev);
5266 tdip->nfsdi_layoutrefs++;
5267 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5268 flp->nfsfl_devp = tdip;
5270 flp->nfsfl_ffm[ind].devp = tdip;
5271 nfscl_reldevinfo_locked(tdip);
5274 free(dip, M_NFSDEVINFO);
5278 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5279 dip->nfsdi_layoutrefs = 1;
5280 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5281 flp->nfsfl_devp = dip;
5283 flp->nfsfl_ffm[ind].devp = dip;
5292 * Free up a layout structure and associated file layout structure(s).
5295 nfscl_freelayout(struct nfscllayout *layp)
5297 struct nfsclflayout *flp, *nflp;
5298 struct nfsclrecalllayout *rp, *nrp;
5300 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5301 LIST_REMOVE(flp, nfsfl_list);
5302 nfscl_freeflayout(flp);
5304 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5305 LIST_REMOVE(flp, nfsfl_list);
5306 nfscl_freeflayout(flp);
5308 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5309 LIST_REMOVE(rp, nfsrecly_list);
5310 free(rp, M_NFSLAYRECALL);
5313 free(layp, M_NFSLAYOUT);
5317 * Free up a file layout structure.
5320 nfscl_freeflayout(struct nfsclflayout *flp)
5324 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) {
5325 for (i = 0; i < flp->nfsfl_fhcnt; i++)
5326 free(flp->nfsfl_fh[i], M_NFSFH);
5327 if (flp->nfsfl_devp != NULL)
5328 flp->nfsfl_devp->nfsdi_layoutrefs--;
5330 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
5331 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) {
5332 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
5333 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
5334 if (flp->nfsfl_ffm[i].devp != NULL)
5335 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--;
5337 free(flp, M_NFSFLAYOUT);
5341 * Free up a file layout devinfo structure.
5344 nfscl_freedevinfo(struct nfscldevinfo *dip)
5347 free(dip, M_NFSDEVINFO);
5351 * Mark any layouts that match as recalled.
5354 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5355 uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op,
5356 char *devid, struct nfsclrecalllayout *recallp)
5358 struct nfsclrecalllayout *rp, *orp;
5360 recallp->nfsrecly_recalltype = recalltype;
5361 recallp->nfsrecly_iomode = iomode;
5362 recallp->nfsrecly_stateseqid = stateseqid;
5363 recallp->nfsrecly_off = off;
5364 recallp->nfsrecly_len = len;
5365 recallp->nfsrecly_stat = stat;
5366 recallp->nfsrecly_op = op;
5368 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID);
5370 * Order the list as file returns first, followed by fsid and any
5371 * returns, both in increasing stateseqid order.
5372 * Note that the seqids wrap around, so 1 is after 0xffffffff.
5373 * (I'm not sure this is correct because I find RFC5661 confusing
5374 * on this, but hopefully it will work ok.)
5377 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5379 if ((recalltype == NFSLAYOUTRETURN_FILE &&
5380 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5381 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5382 (recalltype != NFSLAYOUTRETURN_FILE &&
5383 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5384 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5385 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5390 * Put any error return on all the file returns that will
5393 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE &&
5394 stat != 0 && rp->nfsrecly_stat == 0) {
5395 rp->nfsrecly_stat = stat;
5396 rp->nfsrecly_op = op;
5398 NFSBCOPY(devid, rp->nfsrecly_devid,
5404 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5407 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5409 lyp->nfsly_flags |= NFSLY_RECALL;
5410 wakeup(lyp->nfsly_clp);
5415 * Compare the two seqids for ordering. The trick is that the seqids can
5416 * wrap around from 0xffffffff->0, so check for the cases where one
5417 * has wrapped around.
5418 * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5421 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5424 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5425 /* seqid2 has wrapped around. */
5427 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5428 /* seqid1 has wrapped around. */
5430 if (seqid1 <= seqid2)
5436 * Do a layout return for each of the recalls.
5439 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5440 struct ucred *cred, NFSPROC_T *p)
5442 struct nfsclrecalllayout *rp;
5443 nfsv4stateid_t stateid;
5446 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5447 stateid.seqid = lyp->nfsly_stateid.seqid;
5448 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5449 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5451 layouttype = NFSLAYOUT_FLEXFILE;
5452 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5453 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5454 lyp->nfsly_fhlen, 0, layouttype,
5455 rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5456 rp->nfsrecly_off, rp->nfsrecly_len,
5457 &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op,
5458 rp->nfsrecly_devid);
5463 * Do the layout commit for a file layout.
5466 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5467 struct ucred *cred, NFSPROC_T *p)
5469 struct nfsclflayout *flp;
5471 int error, layouttype;
5473 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5474 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5476 layouttype = NFSLAYOUT_FLEXFILE;
5477 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5478 if (layouttype == NFSLAYOUT_FLEXFILE &&
5479 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
5480 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
5481 /* If not supported, don't bother doing it. */
5483 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5486 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5487 len = flp->nfsfl_end - flp->nfsfl_off;
5488 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5489 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5490 lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5491 layouttype, cred, p, NULL);
5492 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5493 if (error == NFSERR_NOTSUPP) {
5494 /* If not supported, don't bother doing it. */
5496 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5505 * Commit all layouts for a file (vnode).
5508 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5510 struct nfsclclient *clp;
5511 struct nfscllayout *lyp;
5512 struct nfsnode *np = VTONFS(vp);
5514 struct nfsmount *nmp;
5518 if (NFSHASNOLAYOUTCOMMIT(nmp))
5526 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5531 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5532 if (NFSCL_FORCEDISM(mp)) {
5537 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5538 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5540 NFSCL_DEBUG(4, "do layoutcommit2\n");
5541 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5545 nfsv4_relref(&lyp->nfsly_lock);