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 *, struct nfsclopenhash *,
104 u_int8_t *, int, u_int8_t *, u_int8_t *, u_int32_t,
105 struct nfscllockowner **, struct nfsclopen **);
106 static bool nfscl_checkown(struct nfsclowner *, struct nfsclopen *, uint8_t *,
107 uint8_t *, struct nfscllockowner **, struct nfsclopen **,
108 struct nfsclopen **);
109 static void nfscl_clrelease(struct nfsclclient *);
110 static void nfscl_cleanclient(struct nfsclclient *);
111 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
112 struct ucred *, NFSPROC_T *);
113 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
114 struct nfsmount *, struct ucred *, NFSPROC_T *);
115 static void nfscl_recover(struct nfsclclient *, bool *, struct ucred *,
117 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
118 struct nfscllock *, int);
119 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
120 struct nfscllock **, int);
121 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *);
122 static u_int32_t nfscl_nextcbident(void);
123 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
124 static struct nfsclclient *nfscl_getclnt(u_int32_t);
125 static struct nfsclclient *nfscl_getclntsess(uint8_t *);
126 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
128 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
129 int, struct nfsclrecalllayout **);
130 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
131 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
133 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
134 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
135 u_int8_t *, struct nfscllock **);
136 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
137 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
138 struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
139 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
140 struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
141 struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *);
142 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
143 struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
144 struct nfscldeleg *, struct ucred *, NFSPROC_T *);
145 static void nfscl_totalrecall(struct nfsclclient *);
146 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
147 struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
148 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
149 u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
150 struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
151 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
152 int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
153 struct ucred *, NFSPROC_T *);
154 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
155 struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
156 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *,
158 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
159 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
160 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
161 struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int,
163 static void nfscl_freeopenowner(struct nfsclowner *, int);
164 static void nfscl_cleandeleg(struct nfscldeleg *);
165 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
166 struct nfsmount *, NFSPROC_T *);
167 static void nfscl_emptylockowner(struct nfscllockowner *,
168 struct nfscllockownerfhhead *);
169 static void nfscl_mergeflayouts(struct nfsclflayouthead *,
170 struct nfsclflayouthead *);
171 static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t,
172 uint64_t, uint32_t, uint32_t, uint32_t, char *, struct nfsclrecalllayout *);
173 static int nfscl_seq(uint32_t, uint32_t);
174 static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *,
175 struct ucred *, NFSPROC_T *);
176 static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *,
177 struct ucred *, NFSPROC_T *);
179 static short nfscberr_null[] = {
184 static short nfscberr_getattr[] = {
193 static short nfscberr_recall[] = {
203 static short *nfscl_cberrmap[] = {
211 #define NETFAMILY(clp) \
212 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
215 * Called for an open operation.
216 * If the nfhp argument is NULL, just get an openowner.
219 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
220 struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
221 struct nfsclopen **opp, int *newonep, int *retp, int lockit)
223 struct nfsclclient *clp;
224 struct nfsclowner *owp, *nowp;
225 struct nfsclopen *op = NULL, *nop = NULL;
226 struct nfscldeleg *dp;
227 struct nfsclownerhead *ohp;
228 u_int8_t own[NFSV4CL_LOCKNAMELEN];
239 * Might need one or both of these, so MALLOC them now, to
240 * avoid a tsleep() in MALLOC later.
242 nowp = malloc(sizeof (struct nfsclowner),
243 M_NFSCLOWNER, M_WAITOK);
245 nop = malloc(sizeof (struct nfsclopen) +
246 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
247 nop->nfso_hash.le_prev = NULL;
249 ret = nfscl_getcl(vp->v_mount, cred, p, false, &clp);
251 free(nowp, M_NFSCLOWNER);
253 free(nop, M_NFSCLOPEN);
258 * Get the Open iff it already exists.
259 * If none found, add the new one or return error, depending upon
264 /* First check the delegation list */
265 if (nfhp != NULL && usedeleg) {
266 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
267 if (dp->nfsdl_fhlen == fhlen &&
268 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
269 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
270 (dp->nfsdl_flags & NFSCLDL_WRITE))
278 /* For NFSv4.1/4.2 and this option, use a single open_owner. */
279 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
280 nfscl_filllockowner(NULL, own, F_POSIX);
282 nfscl_filllockowner(p->td_proc, own, F_POSIX);
284 ohp = &dp->nfsdl_owner;
286 ohp = &clp->nfsc_owner;
287 /* Now, search for an openowner */
288 LIST_FOREACH(owp, ohp, nfsow_list) {
289 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
294 * Create a new open, as required.
296 nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
300 * Now, check the mode on the open and return the appropriate
304 if (nfhp != NULL && dp != NULL && nop == NULL)
305 /* new local open on delegation */
306 *retp = NFSCLOPEN_SETCRED;
308 *retp = NFSCLOPEN_OK;
310 if (op != NULL && (amode & ~(op->nfso_mode))) {
311 op->nfso_mode |= amode;
312 if (retp != NULL && dp == NULL)
313 *retp = NFSCLOPEN_DOOPEN;
317 * Serialize modifications to the open owner for multiple threads
318 * within the same process using a read/write sleep lock.
319 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
320 * by acquiring a shared lock. The close operations still use an
321 * exclusive lock for this case.
324 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) {
326 * Get a shared lock on the OpenOwner, but first
327 * wait for any pending exclusive lock, so that the
328 * exclusive locker gets priority.
330 nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
331 NFSCLSTATEMUTEXPTR, NULL);
332 nfsv4_getref(&owp->nfsow_rwlock, NULL,
333 NFSCLSTATEMUTEXPTR, NULL);
335 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
339 free(nowp, M_NFSCLOWNER);
341 free(nop, M_NFSCLOPEN);
350 * Create a new open, as required.
353 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
354 struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
355 struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
356 struct ucred *cred, int *newonep)
358 struct nfsclowner *owp = *owpp, *nowp;
359 struct nfsclopen *op, *nop;
369 if (owp == NULL && nowp != NULL) {
370 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
371 LIST_INIT(&nowp->nfsow_open);
372 nowp->nfsow_clp = clp;
373 nowp->nfsow_seqid = 0;
374 nowp->nfsow_defunct = 0;
375 nfscl_lockinit(&nowp->nfsow_rwlock);
377 nfsstatsv1.cllocalopenowners++;
378 LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
380 nfsstatsv1.clopenowners++;
381 LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
389 /* If an fhp has been specified, create an Open as well. */
391 /* and look for the correct open, based upon FH */
392 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
393 if (op->nfso_fhlen == fhlen &&
394 !NFSBCMP(op->nfso_fh, fhp, fhlen))
397 if (op == NULL && nop != NULL) {
400 nop->nfso_opencnt = 0;
401 nop->nfso_posixlock = 1;
402 nop->nfso_fhlen = fhlen;
403 NFSBCOPY(fhp, nop->nfso_fh, fhlen);
404 LIST_INIT(&nop->nfso_lock);
405 nop->nfso_stateid.seqid = 0;
406 nop->nfso_stateid.other[0] = 0;
407 nop->nfso_stateid.other[1] = 0;
408 nop->nfso_stateid.other[2] = 0;
409 KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
410 newnfs_copyincred(cred, &nop->nfso_cred);
412 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
413 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
415 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
416 nfsstatsv1.cllocalopens++;
418 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, fhp, fhlen),
420 nfsstatsv1.clopens++;
422 LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
434 * Called to find/add a delegation to a client.
437 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
438 int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
440 struct nfscldeleg *dp = *dpp, *tdp;
441 struct nfsmount *nmp;
443 KASSERT(mp != NULL, ("nfscl_deleg: mp NULL"));
446 * First, if we have received a Read delegation for a file on a
447 * read/write file system, just return it, because they aren't
450 if (dp != NULL && !NFSMNT_RDONLY(mp) &&
451 (dp->nfsdl_flags & NFSCLDL_READ)) {
452 nfscl_trydelegreturn(dp, cred, nmp, p);
453 free(dp, M_NFSCLDELEG);
459 * Since a delegation might be added to the mount,
460 * set NFSMNTP_DELEGISSUED now. If a delegation already
461 * exagain ists, setting this flag is harmless.
464 nmp->nm_privflag |= NFSMNTP_DELEGISSUED;
467 /* Look for the correct deleg, based upon FH */
469 tdp = nfscl_finddeleg(clp, nfhp, fhlen);
473 return (NFSERR_BADSTATEID);
476 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
477 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
479 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
480 nfsstatsv1.cldelegates++;
484 * Delegation already exists, what do we do if a new one??
487 printf("Deleg already exists!\n");
488 free(dp, M_NFSCLDELEG);
499 * Find a delegation for this file handle. Return NULL upon failure.
501 static struct nfscldeleg *
502 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
504 struct nfscldeleg *dp;
506 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
507 if (dp->nfsdl_fhlen == fhlen &&
508 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
515 * Get a stateid for an I/O operation. First, look for an open and iff
516 * found, return either a lockowner stateid or the open stateid.
517 * If no Open is found, just return error and the special stateid of all zeros.
520 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
521 int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
524 struct nfsclclient *clp;
525 struct nfsclopen *op = NULL, *top;
526 struct nfsclopenhash *oph;
527 struct nfscllockowner *lp;
528 struct nfscldeleg *dp;
530 struct nfsmount *nmp;
531 u_int8_t own[NFSV4CL_LOCKNAMELEN];
537 * Initially, just set the special stateid of all zeros.
538 * (Don't do this for a DS, since the special stateid can't be used.)
542 stateidp->other[0] = 0;
543 stateidp->other[1] = 0;
544 stateidp->other[2] = 0;
546 if (vnode_vtype(vp) != VREG)
549 nmp = VFSTONFS(vp->v_mount);
551 clp = nfscl_findcl(nmp);
558 * Wait for recovery to complete.
560 while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
561 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
562 PZERO, "nfsrecvr", NULL);
565 * First, look for a delegation.
567 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
568 if (dp->nfsdl_fhlen == fhlen &&
569 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
570 if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
571 (dp->nfsdl_flags & NFSCLDL_WRITE)) {
572 if (NFSHASNFSV4N(nmp))
576 dp->nfsdl_stateid.seqid;
577 stateidp->other[0] = dp->nfsdl_stateid.other[0];
578 stateidp->other[1] = dp->nfsdl_stateid.other[1];
579 stateidp->other[2] = dp->nfsdl_stateid.other[2];
580 if (!(np->n_flag & NDELEGRECALL)) {
581 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
583 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
585 dp->nfsdl_timestamp = NFSD_MONOSEC +
587 dp->nfsdl_rwlock.nfslock_usecnt++;
588 *lckpp = (void *)&dp->nfsdl_rwlock;
599 * If p != NULL, we want to search the parentage tree
600 * for a matching OpenOwner and use that.
602 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
603 nfscl_filllockowner(NULL, own, F_POSIX);
605 nfscl_filllockowner(p->td_proc, own, F_POSIX);
607 error = nfscl_getopen(NULL, clp->nfsc_openhash, nfhp, fhlen,
608 own, own, mode, &lp, &op);
609 if (error == 0 && lp != NULL && fords == 0) {
610 /* Don't return a lock stateid for a DS. */
611 if (NFSHASNFSV4N(nmp))
614 stateidp->seqid = lp->nfsl_stateid.seqid;
616 lp->nfsl_stateid.other[0];
618 lp->nfsl_stateid.other[1];
620 lp->nfsl_stateid.other[2];
626 /* If not found, just look for any OpenOwner that will work. */
629 oph = NFSCLOPENHASH(clp, nfhp, fhlen);
630 LIST_FOREACH(op, oph, nfso_hash) {
631 if (op->nfso_fhlen == fhlen &&
632 !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
633 if (top == NULL && (op->nfso_mode &
634 NFSV4OPEN_ACCESSWRITE) != 0 &&
635 (mode & NFSV4OPEN_ACCESSREAD) != 0)
637 if ((mode & op->nfso_mode) == mode) {
638 /* LRU order the hash list. */
639 LIST_REMOVE(op, nfso_hash);
640 LIST_INSERT_HEAD(oph, op, nfso_hash);
647 NFSCL_DEBUG(2, "openmode top=%p\n", top);
648 if (top == NULL || NFSHASOPENMODE(nmp)) {
655 * For read aheads or write behinds, use the open cred.
656 * A read ahead or write behind is indicated by p == NULL.
659 newnfs_copycred(&op->nfso_cred, cred);
663 * No lock stateid, so return the open stateid.
665 if (NFSHASNFSV4N(nmp))
668 stateidp->seqid = op->nfso_stateid.seqid;
669 stateidp->other[0] = op->nfso_stateid.other[0];
670 stateidp->other[1] = op->nfso_stateid.other[1];
671 stateidp->other[2] = op->nfso_stateid.other[2];
677 * Search for a matching file, mode and, optionally, lockowner.
680 nfscl_getopen(struct nfsclownerhead *ohp, struct nfsclopenhash *ohashp,
681 u_int8_t *nfhp, int fhlen, u_int8_t *openown, u_int8_t *lockown,
682 u_int32_t mode, struct nfscllockowner **lpp, struct nfsclopen **opp)
684 struct nfsclowner *owp;
685 struct nfsclopen *op, *rop, *rop2;
686 struct nfsclopenhash *oph;
689 KASSERT(ohp == NULL || ohashp == NULL, ("nfscl_getopen: "
690 "only one of ohp and ohashp can be set"));
694 * rop will be set to the open to be returned. There are three
695 * variants of this, all for an open of the correct file:
696 * 1 - A match of lockown.
697 * 2 - A match of the openown, when no lockown match exists.
698 * 3 - A match for any open, if no openown or lockown match exists.
699 * Looking for #2 over #3 probably isn't necessary, but since
700 * RFC3530 is vague w.r.t. the relationship between openowners and
701 * lockowners, I think this is the safer way to go.
706 /* Search the client list */
707 if (ohashp == NULL) {
708 /* Search the local opens on the delegation. */
709 LIST_FOREACH(owp, ohp, nfsow_list) {
710 /* and look for the correct open */
711 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
712 if (op->nfso_fhlen == fhlen &&
713 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
714 && (op->nfso_mode & mode) == mode)
715 keep_looping = nfscl_checkown(owp, op, openown,
716 lockown, lpp, &rop, &rop2);
724 /* Search for matching opens on the hash list. */
725 oph = &ohashp[NFSCLOPENHASHFUNC(nfhp, fhlen)];
726 LIST_FOREACH(op, oph, nfso_hash) {
727 if (op->nfso_fhlen == fhlen &&
728 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
729 && (op->nfso_mode & mode) == mode)
730 keep_looping = nfscl_checkown(op->nfso_own, op,
731 openown, lockown, lpp, &rop, &rop2);
733 /* LRU order the hash list. */
734 LIST_REMOVE(op, nfso_hash);
735 LIST_INSERT_HEAD(oph, op, nfso_hash);
748 /* Check for an owner match. */
750 nfscl_checkown(struct nfsclowner *owp, struct nfsclopen *op, uint8_t *openown,
751 uint8_t *lockown, struct nfscllockowner **lpp, struct nfsclopen **ropp,
752 struct nfsclopen **ropp2)
754 struct nfscllockowner *lp;
759 /* Now look for a matching lockowner. */
760 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
761 if (!NFSBCMP(lp->nfsl_owner, lockown,
762 NFSV4CL_LOCKNAMELEN)) {
769 if (*ropp == NULL && !NFSBCMP(owp->nfsow_owner, openown,
770 NFSV4CL_LOCKNAMELEN)) {
773 keep_looping = false;
777 return (keep_looping);
781 * Release use of an open owner. Called when open operations are done
782 * with the open owner.
785 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
786 __unused int error, __unused int candelete, int unlocked)
793 if (NFSHASONEOPENOWN(nmp))
794 nfsv4_relref(&owp->nfsow_rwlock);
796 nfscl_lockunlock(&owp->nfsow_rwlock);
798 nfscl_clrelease(owp->nfsow_clp);
803 * Release use of an open structure under an open owner.
806 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
809 struct nfsclclient *clp;
810 struct nfsclowner *owp;
816 if (NFSHASONEOPENOWN(nmp))
817 nfsv4_relref(&owp->nfsow_rwlock);
819 nfscl_lockunlock(&owp->nfsow_rwlock);
820 clp = owp->nfsow_clp;
821 if (error && candelete && op->nfso_opencnt == 0)
822 nfscl_freeopen(op, 0);
823 nfscl_clrelease(clp);
828 * Called to get a clientid structure. It will optionally lock the
829 * client data structures to do the SetClientId/SetClientId_confirm,
830 * but will release that lock and return the clientid with a reference
832 * If the "cred" argument is NULL, a new clientid should not be created.
833 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
835 * It always clpp with a reference count on it, unless returning an error.
838 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
839 bool tryminvers, struct nfsclclient **clpp)
841 struct nfsclclient *clp;
842 struct nfsclclient *newclp = NULL;
843 struct nfsmount *nmp;
844 char uuid[HOSTUUIDLEN];
845 int igotlock = 0, error, trystalecnt, clidinusedelay, i;
850 getcredhostuuid(cred, uuid, sizeof uuid);
851 idlen = strlen(uuid);
853 idlen += sizeof (u_int64_t);
855 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
857 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
862 * If a forced dismount is already in progress, don't
863 * allocate a new clientid and get out now. For the case where
864 * clp != NULL, this is a harmless optimization.
866 if (NFSCL_FORCEDISM(mp)) {
869 free(newclp, M_NFSCLCLIENT);
874 if (newclp == NULL) {
879 clp->nfsc_idlen = idlen;
880 LIST_INIT(&clp->nfsc_owner);
881 TAILQ_INIT(&clp->nfsc_deleg);
882 TAILQ_INIT(&clp->nfsc_layout);
883 LIST_INIT(&clp->nfsc_devinfo);
884 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
885 LIST_INIT(&clp->nfsc_deleghash[i]);
886 for (i = 0; i < NFSCLOPENHASHSIZE; i++)
887 LIST_INIT(&clp->nfsc_openhash[i]);
888 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
889 LIST_INIT(&clp->nfsc_layouthash[i]);
890 clp->nfsc_flags = NFSCLFLAGS_INITED;
891 clp->nfsc_clientidrev = 1;
892 clp->nfsc_cbident = nfscl_nextcbident();
893 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
895 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
900 free(newclp, M_NFSCLCLIENT);
902 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
903 !NFSCL_FORCEDISM(mp))
904 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
905 NFSCLSTATEMUTEXPTR, mp);
908 * Call nfsv4_lock() with "iwantlock == 0" so that it will
909 * wait for a pending exclusive lock request. This gives the
910 * exclusive lock request priority over this shared lock
912 * An exclusive lock on nfsc_lock is used mainly for server
915 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, mp);
916 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
918 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
920 * Both nfsv4_lock() and nfsv4_getref() know to check
921 * for NFSCL_FORCEDISM() and return without sleeping to
922 * wait for the exclusive lock to be released, since it
923 * might be held by nfscl_umount() and we need to get out
924 * now for that case and not wait until nfscl_umount()
933 * If it needs a clientid, do the setclientid now.
935 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
937 panic("nfscl_clget");
938 if (p == NULL || cred == NULL) {
940 nfsv4_unlock(&clp->nfsc_lock, 0);
945 * If RFC3530 Sec. 14.2.33 is taken literally,
946 * NFSERR_CLIDINUSE will be returned persistently for the
947 * case where a new mount of the same file system is using
948 * a different principal. In practice, NFSERR_CLIDINUSE is
949 * only returned when there is outstanding unexpired state
950 * on the clientid. As such, try for twice the lease
951 * interval, if we know what that is. Otherwise, make a
953 * The case of returning NFSERR_STALECLIENTID is far less
954 * likely, but might occur if there is a significant delay
955 * between doing the SetClientID and SetClientIDConfirm Ops,
956 * such that the server throws away the clientid before
957 * receiving the SetClientIDConfirm.
959 if (clp->nfsc_renew > 0)
960 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
962 clidinusedelay = 120;
965 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
966 if (error == NFSERR_STALECLIENTID ||
967 error == NFSERR_STALEDONTRECOVER ||
968 error == NFSERR_BADSESSION ||
969 error == NFSERR_CLIDINUSE) {
970 (void) nfs_catnap(PZERO, error, "nfs_setcl");
971 } else if (error == NFSERR_MINORVERMISMATCH &&
973 if (nmp->nm_minorvers > 0)
978 } while (((error == NFSERR_STALECLIENTID ||
979 error == NFSERR_BADSESSION ||
980 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
981 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0) ||
982 (error == NFSERR_MINORVERMISMATCH && tryminvers));
985 nfsv4_unlock(&clp->nfsc_lock, 0);
989 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
993 nfsv4_unlock(&clp->nfsc_lock, 1);
1002 * Get a reference to a clientid and return it, if valid.
1004 struct nfsclclient *
1005 nfscl_findcl(struct nfsmount *nmp)
1007 struct nfsclclient *clp;
1010 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
1016 * Release the clientid structure. It may be locked or reference counted.
1019 nfscl_clrelease(struct nfsclclient *clp)
1022 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
1023 nfsv4_unlock(&clp->nfsc_lock, 0);
1025 nfsv4_relref(&clp->nfsc_lock);
1029 * External call for nfscl_clrelease.
1032 nfscl_clientrelease(struct nfsclclient *clp)
1036 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
1037 nfsv4_unlock(&clp->nfsc_lock, 0);
1039 nfsv4_relref(&clp->nfsc_lock);
1044 * Called when wanting to lock a byte region.
1047 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1048 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
1049 int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
1050 struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
1052 struct nfscllockowner *lp;
1053 struct nfsclopen *op;
1054 struct nfsclclient *clp;
1055 struct nfscllockowner *nlp;
1056 struct nfscllock *nlop, *otherlop;
1057 struct nfscldeleg *dp = NULL, *ldp = NULL;
1058 struct nfscllockownerhead *lhp = NULL;
1060 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
1062 int error = 0, ret, donelocally = 0;
1065 /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
1074 * Might need these, so MALLOC them now, to
1075 * avoid a tsleep() in MALLOC later.
1078 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
1080 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1082 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1083 nlop->nfslo_type = type;
1084 nlop->nfslo_first = off;
1085 if (len == NFS64BITSSET) {
1086 nlop->nfslo_end = NFS64BITSSET;
1088 nlop->nfslo_end = off + len;
1089 if (nlop->nfslo_end <= nlop->nfslo_first)
1090 error = NFSERR_INVAL;
1097 error = nfscl_getcl(vp->v_mount, cred, p, false, &clp);
1100 free(nlp, M_NFSCLLOCKOWNER);
1101 free(otherlop, M_NFSCLLOCK);
1102 free(nlop, M_NFSCLLOCK);
1109 openownp = ropenownp;
1111 nfscl_filllockowner(id, own, flags);
1113 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
1114 nfscl_filllockowner(NULL, openown, F_POSIX);
1116 nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1122 * First, search for a delegation. If one exists for this file,
1123 * the lock can be done locally against it, so long as there
1124 * isn't a local lock conflict.
1126 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1127 np->n_fhp->nfh_len);
1128 /* Just sanity check for correct type of delegation */
1129 if (dp != NULL && ((dp->nfsdl_flags &
1130 (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1132 (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1136 /* Now, find an open and maybe a lockowner. */
1137 ret = nfscl_getopen(&dp->nfsdl_owner, NULL, np->n_fhp->nfh_fh,
1138 np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1140 ret = nfscl_getopen(NULL, clp->nfsc_openhash,
1141 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1142 ownp, mode, NULL, &op);
1144 lhp = &dp->nfsdl_lock;
1145 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1146 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1147 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1155 * Get the related Open and maybe lockowner.
1157 error = nfscl_getopen(NULL, clp->nfsc_openhash,
1158 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1159 ownp, mode, &lp, &op);
1161 lhp = &op->nfso_lock;
1163 if (!error && !recovery)
1164 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1165 np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1168 nfscl_clrelease(clp);
1171 free(nlp, M_NFSCLLOCKOWNER);
1172 free(otherlop, M_NFSCLLOCK);
1173 free(nlop, M_NFSCLLOCK);
1178 * Ok, see if a lockowner exists and create one, as required.
1181 LIST_FOREACH(lp, lhp, nfsl_list) {
1182 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1186 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1188 NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1189 NFSV4CL_LOCKNAMELEN);
1191 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1192 NFSV4CL_LOCKNAMELEN);
1193 nlp->nfsl_seqid = 0;
1194 nlp->nfsl_lockflags = flags;
1195 nlp->nfsl_inprog = NULL;
1196 nfscl_lockinit(&nlp->nfsl_rwlock);
1197 LIST_INIT(&nlp->nfsl_lock);
1199 nlp->nfsl_open = NULL;
1200 nfsstatsv1.cllocallockowners++;
1202 nlp->nfsl_open = op;
1203 nfsstatsv1.cllockowners++;
1205 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1212 * Now, update the byte ranges for locks.
1214 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1220 nfscl_clrelease(clp);
1223 * Serial modifications on the lock owner for multiple threads
1224 * for the same process using a read/write lock.
1227 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1233 free(nlp, M_NFSCLLOCKOWNER);
1235 free(nlop, M_NFSCLLOCK);
1237 free(otherlop, M_NFSCLLOCK);
1244 * Called to unlock a byte range, for LockU.
1247 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1248 __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1249 struct nfsclclient *clp, void *id, int flags,
1250 struct nfscllockowner **lpp, int *dorpcp)
1252 struct nfscllockowner *lp;
1253 struct nfsclopen *op;
1254 struct nfscllock *nlop, *other_lop = NULL;
1255 struct nfscldeleg *dp;
1257 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1265 * Might need these, so MALLOC them now, to
1266 * avoid a tsleep() in MALLOC later.
1269 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1270 nlop->nfslo_type = F_UNLCK;
1271 nlop->nfslo_first = off;
1272 if (len == NFS64BITSSET) {
1273 nlop->nfslo_end = NFS64BITSSET;
1275 nlop->nfslo_end = off + len;
1276 if (nlop->nfslo_end <= nlop->nfslo_first) {
1277 free(nlop, M_NFSCLLOCK);
1278 return (NFSERR_INVAL);
1283 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1286 nfscl_filllockowner(id, own, flags);
1290 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1291 np->n_fhp->nfh_len);
1294 * First, unlock any local regions on a delegation.
1297 /* Look for this lockowner. */
1298 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1299 if (!NFSBCMP(lp->nfsl_owner, own,
1300 NFSV4CL_LOCKNAMELEN))
1304 /* Use other_lop, so nlop is still available */
1305 (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1309 * Now, find a matching open/lockowner that hasn't already been done,
1310 * as marked by nfsl_inprog.
1314 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1315 np->n_fhp->nfh_len), nfso_hash) {
1316 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1317 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1318 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1319 if (lp->nfsl_inprog == NULL &&
1320 !NFSBCMP(lp->nfsl_owner, own,
1321 NFSV4CL_LOCKNAMELEN)) {
1332 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1336 * Serial modifications on the lock owner for multiple
1337 * threads for the same process using a read/write lock.
1339 lp->nfsl_inprog = p;
1340 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1345 free(nlop, M_NFSCLLOCK);
1347 free(other_lop, M_NFSCLLOCK);
1352 * Release all lockowners marked in progess for this process and file.
1355 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1356 void *id, int flags)
1358 struct nfsclopen *op;
1359 struct nfscllockowner *lp;
1361 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1364 nfscl_filllockowner(id, own, flags);
1366 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1367 np->n_fhp->nfh_len), nfso_hash) {
1368 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1369 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1370 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1371 if (lp->nfsl_inprog == p &&
1372 !NFSBCMP(lp->nfsl_owner, own,
1373 NFSV4CL_LOCKNAMELEN)) {
1374 lp->nfsl_inprog = NULL;
1375 nfscl_lockunlock(&lp->nfsl_rwlock);
1380 nfscl_clrelease(clp);
1385 * Called to find out if any bytes within the byte range specified are
1386 * write locked by the calling process. Used to determine if flushing
1387 * is required before a LockU.
1388 * If in doubt, return 1, so the flush will occur.
1391 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1392 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1394 struct nfscllockowner *lp;
1395 struct nfsclopen *op;
1396 struct nfsclclient *clp;
1397 struct nfscllock *lop;
1398 struct nfscldeleg *dp;
1401 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1405 switch (fl->l_whence) {
1409 * Caller is responsible for adding any necessary offset
1410 * when SEEK_CUR is used.
1415 off = np->n_size + fl->l_start;
1420 if (fl->l_len != 0) {
1421 end = off + fl->l_len;
1428 error = nfscl_getcl(vp->v_mount, cred, p, false, &clp);
1431 nfscl_filllockowner(id, own, flags);
1435 * First check the delegation locks.
1437 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1439 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1440 if (!NFSBCMP(lp->nfsl_owner, own,
1441 NFSV4CL_LOCKNAMELEN))
1445 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1446 if (lop->nfslo_first >= end)
1448 if (lop->nfslo_end <= off)
1450 if (lop->nfslo_type == F_WRLCK) {
1451 nfscl_clrelease(clp);
1460 * Now, check state against the server.
1462 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1463 np->n_fhp->nfh_len), nfso_hash) {
1464 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1465 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1466 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1467 if (!NFSBCMP(lp->nfsl_owner, own,
1468 NFSV4CL_LOCKNAMELEN))
1472 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1473 if (lop->nfslo_first >= end)
1475 if (lop->nfslo_end <= off)
1477 if (lop->nfslo_type == F_WRLCK) {
1478 nfscl_clrelease(clp);
1486 nfscl_clrelease(clp);
1492 * Release a byte range lock owner structure.
1495 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1497 struct nfsclclient *clp;
1502 clp = lp->nfsl_open->nfso_own->nfsow_clp;
1503 if (error != 0 && candelete &&
1504 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1505 nfscl_freelockowner(lp, 0);
1507 nfscl_lockunlock(&lp->nfsl_rwlock);
1508 nfscl_clrelease(clp);
1513 * Free up an open structure and any associated byte range lock structures.
1516 nfscl_freeopen(struct nfsclopen *op, int local)
1519 LIST_REMOVE(op, nfso_list);
1520 if (op->nfso_hash.le_prev != NULL)
1521 LIST_REMOVE(op, nfso_hash);
1522 nfscl_freealllocks(&op->nfso_lock, local);
1523 free(op, M_NFSCLOPEN);
1525 nfsstatsv1.cllocalopens--;
1527 nfsstatsv1.clopens--;
1531 * Free up all lock owners and associated locks.
1534 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1536 struct nfscllockowner *lp, *nlp;
1538 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1539 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1541 nfscl_freelockowner(lp, local);
1546 * Called for an Open when NFSERR_EXPIRED is received from the server.
1547 * If there are no byte range locks nor a Share Deny lost, try to do a
1548 * fresh Open. Otherwise, free the open.
1551 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1552 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1554 struct nfscllockowner *lp;
1555 struct nfscldeleg *dp;
1556 int mustdelete = 0, error;
1559 * Look for any byte range lock(s).
1561 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1562 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1569 * If no byte range lock(s) nor a Share deny, try to re-open.
1571 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1572 newnfs_copycred(&op->nfso_cred, cred);
1574 error = nfsrpc_reopen(nmp, op->nfso_fh,
1575 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1579 free(dp, M_NFSCLDELEG);
1584 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1585 op->nfso_fhlen, cred, p, &dp);
1589 * If a byte range lock or Share deny or couldn't re-open, free it.
1592 nfscl_freeopen(op, 0);
1593 return (mustdelete);
1597 * Free up an open owner structure.
1600 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1603 LIST_REMOVE(owp, nfsow_list);
1604 free(owp, M_NFSCLOWNER);
1606 nfsstatsv1.cllocalopenowners--;
1608 nfsstatsv1.clopenowners--;
1612 * Free up a byte range lock owner structure.
1615 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1617 struct nfscllock *lop, *nlop;
1619 LIST_REMOVE(lp, nfsl_list);
1620 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1621 nfscl_freelock(lop, local);
1623 free(lp, M_NFSCLLOCKOWNER);
1625 nfsstatsv1.cllocallockowners--;
1627 nfsstatsv1.cllockowners--;
1631 * Free up a byte range lock structure.
1634 nfscl_freelock(struct nfscllock *lop, int local)
1637 LIST_REMOVE(lop, nfslo_list);
1638 free(lop, M_NFSCLLOCK);
1640 nfsstatsv1.cllocallocks--;
1642 nfsstatsv1.cllocks--;
1646 * Clean out the state related to a delegation.
1649 nfscl_cleandeleg(struct nfscldeleg *dp)
1651 struct nfsclowner *owp, *nowp;
1652 struct nfsclopen *op;
1654 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1655 op = LIST_FIRST(&owp->nfsow_open);
1657 if (LIST_NEXT(op, nfso_list) != NULL)
1658 panic("nfscleandel");
1659 nfscl_freeopen(op, 1);
1661 nfscl_freeopenowner(owp, 1);
1663 nfscl_freealllocks(&dp->nfsdl_lock, 1);
1667 * Free a delegation.
1670 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp, bool freeit)
1673 TAILQ_REMOVE(hdp, dp, nfsdl_list);
1674 LIST_REMOVE(dp, nfsdl_hash);
1676 free(dp, M_NFSCLDELEG);
1677 nfsstatsv1.cldelegates--;
1682 * Free up all state related to this client structure.
1685 nfscl_cleanclient(struct nfsclclient *clp)
1687 struct nfsclowner *owp, *nowp;
1688 struct nfsclopen *op, *nop;
1689 struct nfscllayout *lyp, *nlyp;
1690 struct nfscldevinfo *dip, *ndip;
1692 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1693 nfscl_freelayout(lyp);
1695 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
1696 nfscl_freedevinfo(dip);
1698 /* Now, all the OpenOwners, etc. */
1699 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1700 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1701 nfscl_freeopen(op, 0);
1703 nfscl_freeopenowner(owp, 0);
1708 * Called when an NFSERR_EXPIRED is received from the server.
1711 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1712 struct ucred *cred, NFSPROC_T *p)
1714 struct nfsclowner *owp, *nowp, *towp;
1715 struct nfsclopen *op, *nop, *top;
1716 struct nfscldeleg *dp, *ndp;
1717 int ret, printed = 0;
1720 * First, merge locally issued Opens into the list for the server.
1722 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1723 while (dp != NULL) {
1724 ndp = TAILQ_NEXT(dp, nfsdl_list);
1725 owp = LIST_FIRST(&dp->nfsdl_owner);
1726 while (owp != NULL) {
1727 nowp = LIST_NEXT(owp, nfsow_list);
1728 op = LIST_FIRST(&owp->nfsow_open);
1730 if (LIST_NEXT(op, nfso_list) != NULL)
1732 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1733 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1734 NFSV4CL_LOCKNAMELEN))
1738 /* Merge opens in */
1739 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1740 if (top->nfso_fhlen == op->nfso_fhlen &&
1741 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1743 top->nfso_mode |= op->nfso_mode;
1744 top->nfso_opencnt += op->nfso_opencnt;
1749 /* Just add the open to the owner list */
1750 LIST_REMOVE(op, nfso_list);
1751 op->nfso_own = towp;
1752 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1753 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh,
1754 op->nfso_fhlen), op, nfso_hash);
1755 nfsstatsv1.cllocalopens--;
1756 nfsstatsv1.clopens++;
1759 /* Just add the openowner to the client list */
1760 LIST_REMOVE(owp, nfsow_list);
1761 owp->nfsow_clp = clp;
1762 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1763 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh,
1764 op->nfso_fhlen), op, nfso_hash);
1765 nfsstatsv1.cllocalopenowners--;
1766 nfsstatsv1.clopenowners++;
1767 nfsstatsv1.cllocalopens--;
1768 nfsstatsv1.clopens++;
1773 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1775 printf("nfsv4 expired locks lost\n");
1777 nfscl_cleandeleg(dp);
1778 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
1781 if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1785 * Now, try and reopen against the server.
1787 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1788 owp->nfsow_seqid = 0;
1789 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1790 ret = nfscl_expireopen(clp, op, nmp, cred, p);
1791 if (ret && !printed) {
1793 printf("nfsv4 expired locks lost\n");
1796 if (LIST_EMPTY(&owp->nfsow_open))
1797 nfscl_freeopenowner(owp, 0);
1802 * This function must be called after the process represented by "own" has
1803 * exited. Must be called with CLSTATE lock held.
1806 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1808 struct nfsclowner *owp, *nowp;
1809 struct nfscllockowner *lp, *nlp;
1810 struct nfscldeleg *dp;
1812 /* First, get rid of local locks on delegations. */
1813 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1814 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1815 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1816 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1818 nfscl_freelockowner(lp, 1);
1822 owp = LIST_FIRST(&clp->nfsc_owner);
1823 while (owp != NULL) {
1824 nowp = LIST_NEXT(owp, nfsow_list);
1825 if (!NFSBCMP(owp->nfsow_owner, own,
1826 NFSV4CL_LOCKNAMELEN)) {
1828 * If there are children that haven't closed the
1829 * file descriptors yet, the opens will still be
1830 * here. For that case, let the renew thread clear
1831 * out the OpenOwner later.
1833 if (LIST_EMPTY(&owp->nfsow_open))
1834 nfscl_freeopenowner(owp, 0);
1836 owp->nfsow_defunct = 1;
1843 * Find open/lock owners for processes that have exited.
1846 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1848 struct nfsclowner *owp, *nowp;
1849 struct nfsclopen *op;
1850 struct nfscllockowner *lp, *nlp;
1851 struct nfscldeleg *dp;
1854 * All the pidhash locks must be acquired, since they are sx locks
1855 * and must be acquired before the mutexes. The pid(s) that will
1856 * be used aren't known yet, so all the locks need to be acquired.
1857 * Fortunately, this function is only performed once/sec.
1861 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1862 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1863 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1864 if (LIST_EMPTY(&lp->nfsl_lock))
1865 nfscl_emptylockowner(lp, lhp);
1868 if (nfscl_procdoesntexist(owp->nfsow_owner))
1869 nfscl_cleanup_common(clp, owp->nfsow_owner);
1873 * For the single open_owner case, these lock owners need to be
1874 * checked to see if they still exist separately.
1875 * This is because nfscl_procdoesntexist() never returns true for
1876 * the single open_owner so that the above doesn't ever call
1877 * nfscl_cleanup_common().
1879 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1880 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1881 if (nfscl_procdoesntexist(lp->nfsl_owner))
1882 nfscl_cleanup_common(clp, lp->nfsl_owner);
1886 pidhash_sunlockall();
1890 * Take the empty lock owner and move it to the local lhp list if the
1891 * associated process no longer exists.
1894 nfscl_emptylockowner(struct nfscllockowner *lp,
1895 struct nfscllockownerfhhead *lhp)
1897 struct nfscllockownerfh *lfhp, *mylfhp;
1898 struct nfscllockowner *nlp;
1901 /* If not a Posix lock owner, just return. */
1902 if ((lp->nfsl_lockflags & F_POSIX) == 0)
1908 * First, search to see if this lock owner is already in the list.
1909 * If it is, then the associated process no longer exists.
1911 SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1912 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1913 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
1916 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
1917 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
1918 NFSV4CL_LOCKNAMELEN))
1921 /* If not found, check if process still exists. */
1922 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
1925 /* Move the lock owner over to the local list. */
1926 if (mylfhp == NULL) {
1927 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
1931 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
1932 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
1933 mylfhp->nfslfh_len);
1934 LIST_INIT(&mylfhp->nfslfh_lock);
1935 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
1937 LIST_REMOVE(lp, nfsl_list);
1938 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
1941 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */
1943 * Called from nfs umount to free up the clientid.
1946 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1948 struct nfsclclient *clp;
1953 * For the case that matters, this is the thread that set
1954 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
1955 * done to ensure that any thread executing nfscl_getcl() after
1956 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
1957 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
1958 * explanation, courtesy of Alan Cox.
1959 * What follows is a snippet from Alan Cox's email at:
1960 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
1962 * 1. Set MNTK_UNMOUNTF
1963 * 2. Acquire a standard FreeBSD mutex "m".
1964 * 3. Update some data structures.
1965 * 4. Release mutex "m".
1967 * Then, other threads that acquire "m" after step 4 has occurred will
1968 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to
1969 * step 2 may or may not see MNTK_UNMOUNTF as set.
1972 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
1980 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1981 panic("nfscl umount");
1984 * First, handshake with the nfscl renew thread, to terminate
1987 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1988 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1989 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
1993 * Now, get the exclusive lock on the client state, so
1994 * that no uses of the state are still in progress.
1997 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1998 NFSCLSTATEMUTEXPTR, NULL);
1999 } while (!igotlock);
2003 * Free up all the state. It will expire on the server, but
2004 * maybe we should do a SetClientId/SetClientIdConfirm so
2005 * the server throws it away?
2007 LIST_REMOVE(clp, nfsc_list);
2008 nfscl_delegreturnall(clp, p);
2009 cred = newnfs_getcred();
2010 if (NFSHASNFSV4N(nmp)) {
2011 (void)nfsrpc_destroysession(nmp, clp, cred, p);
2012 (void)nfsrpc_destroyclient(nmp, clp, cred, p);
2014 (void)nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2015 nfscl_cleanclient(clp);
2018 free(clp, M_NFSCLCLIENT);
2024 * This function is called when a server replies with NFSERR_STALECLIENTID
2025 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
2026 * doing Opens and Locks with reclaim. If these fail, it deletes the
2027 * corresponding state.
2030 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred,
2033 struct nfsclowner *owp, *nowp;
2034 struct nfsclopen *op, *nop;
2035 struct nfscllockowner *lp, *nlp;
2036 struct nfscllock *lop, *nlop;
2037 struct nfscldeleg *dp, *ndp, *tdp;
2038 struct nfsmount *nmp;
2039 struct ucred *tcred;
2040 struct nfsclopenhead extra_open;
2041 struct nfscldeleghead extra_deleg;
2044 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
2045 int i, igotlock = 0, error, trycnt, firstlock;
2046 struct nfscllayout *lyp, *nlyp;
2050 * First, lock the client structure, so everyone else will
2051 * block when trying to use state.
2054 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2056 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2057 NFSCLSTATEMUTEXPTR, NULL);
2058 } while (!igotlock);
2061 nmp = clp->nfsc_nmp;
2063 panic("nfscl recover");
2066 * For now, just get rid of all layouts. There may be a need
2067 * to do LayoutCommit Ops with reclaim == true later.
2069 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
2070 nfscl_freelayout(lyp);
2071 TAILQ_INIT(&clp->nfsc_layout);
2072 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
2073 LIST_INIT(&clp->nfsc_layouthash[i]);
2078 error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p);
2079 } while ((error == NFSERR_STALECLIENTID ||
2080 error == NFSERR_BADSESSION ||
2081 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2084 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
2085 NFSCLFLAGS_RECVRINPROG);
2086 wakeup(&clp->nfsc_flags);
2087 nfsv4_unlock(&clp->nfsc_lock, 0);
2091 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2092 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2095 * Mark requests already queued on the server, so that they don't
2096 * initiate another recovery cycle. Any requests already in the
2097 * queue that handle state information will have the old stale
2098 * clientid/stateid and will get a NFSERR_STALESTATEID,
2099 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2100 * This will be translated to NFSERR_STALEDONTRECOVER when
2101 * R_DONTRECOVER is set.
2104 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2105 if (rep->r_nmp == nmp)
2106 rep->r_flags |= R_DONTRECOVER;
2111 * If nfsrpc_setclient() returns *retokp == true,
2112 * no more recovery is needed.
2118 * Now, mark all delegations "need reclaim".
2120 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2121 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2123 TAILQ_INIT(&extra_deleg);
2124 LIST_INIT(&extra_open);
2126 * Now traverse the state lists, doing Open and Lock Reclaims.
2128 tcred = newnfs_getcred();
2129 recovered_one = false;
2130 owp = LIST_FIRST(&clp->nfsc_owner);
2131 while (owp != NULL) {
2132 nowp = LIST_NEXT(owp, nfsow_list);
2133 owp->nfsow_seqid = 0;
2134 op = LIST_FIRST(&owp->nfsow_open);
2135 while (op != NULL) {
2136 nop = LIST_NEXT(op, nfso_list);
2137 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2138 /* Search for a delegation to reclaim with the open */
2139 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2140 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2142 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2143 mode = NFSV4OPEN_ACCESSWRITE;
2144 delegtype = NFSV4OPEN_DELEGATEWRITE;
2146 mode = NFSV4OPEN_ACCESSREAD;
2147 delegtype = NFSV4OPEN_DELEGATEREAD;
2149 if ((op->nfso_mode & mode) == mode &&
2150 op->nfso_fhlen == dp->nfsdl_fhlen &&
2151 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2156 delegtype = NFSV4OPEN_DELEGATENONE;
2157 newnfs_copycred(&op->nfso_cred, tcred);
2158 error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2159 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2160 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2163 recovered_one = true;
2164 /* Handle any replied delegation */
2165 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2166 || NFSMNT_RDONLY(nmp->nm_mountp))) {
2167 if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2168 mode = NFSV4OPEN_ACCESSWRITE;
2170 mode = NFSV4OPEN_ACCESSREAD;
2171 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2172 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2174 if ((op->nfso_mode & mode) == mode &&
2175 op->nfso_fhlen == dp->nfsdl_fhlen &&
2176 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2178 dp->nfsdl_stateid = ndp->nfsdl_stateid;
2179 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2180 dp->nfsdl_ace = ndp->nfsdl_ace;
2181 dp->nfsdl_change = ndp->nfsdl_change;
2182 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2183 if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2184 dp->nfsdl_flags |= NFSCLDL_RECALL;
2185 free(ndp, M_NFSCLDELEG);
2192 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2194 /* and reclaim all byte range locks */
2195 lp = LIST_FIRST(&op->nfso_lock);
2196 while (lp != NULL) {
2197 nlp = LIST_NEXT(lp, nfsl_list);
2200 lop = LIST_FIRST(&lp->nfsl_lock);
2201 while (lop != NULL) {
2202 nlop = LIST_NEXT(lop, nfslo_list);
2203 if (lop->nfslo_end == NFS64BITSSET)
2206 len = lop->nfslo_end - lop->nfslo_first;
2207 error = nfscl_trylock(nmp, NULL,
2208 op->nfso_fh, op->nfso_fhlen, lp,
2209 firstlock, 1, lop->nfslo_first, len,
2210 lop->nfslo_type, tcred, p);
2212 nfscl_freelock(lop, 0);
2217 /* If no locks, but a lockowner, just delete it. */
2218 if (LIST_EMPTY(&lp->nfsl_lock))
2219 nfscl_freelockowner(lp, 0);
2222 } else if (error == NFSERR_NOGRACE && !recovered_one &&
2223 NFSHASNFSV4N(nmp)) {
2225 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
2226 * actually end up here, since the client will do
2227 * a recovery for NFSERR_BADSESSION, but will get
2228 * an NFSERR_NOGRACE reply for the first "reclaim"
2230 * So, call nfscl_expireclient() to recover the
2231 * opens as best we can and then do a reclaim
2232 * complete and return.
2234 nfsrpc_reclaimcomplete(nmp, cred, p);
2235 nfscl_expireclient(clp, nmp, tcred, p);
2239 if (error != 0 && error != NFSERR_BADSESSION)
2240 nfscl_freeopen(op, 0);
2247 * Now, try and get any delegations not yet reclaimed by cobbling
2248 * to-gether an appropriate open.
2251 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2252 while (dp != NULL) {
2253 ndp = TAILQ_NEXT(dp, nfsdl_list);
2254 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2257 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2259 * Name must be as long an largest possible
2260 * NFSV4CL_LOCKNAMELEN. 12 for now.
2262 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2263 NFSV4CL_LOCKNAMELEN);
2264 LIST_INIT(&nowp->nfsow_open);
2265 nowp->nfsow_clp = clp;
2266 nowp->nfsow_seqid = 0;
2267 nowp->nfsow_defunct = 0;
2268 nfscl_lockinit(&nowp->nfsow_rwlock);
2271 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2272 nop = malloc(sizeof (struct nfsclopen) +
2273 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2274 nop->nfso_own = nowp;
2275 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2276 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2277 delegtype = NFSV4OPEN_DELEGATEWRITE;
2279 nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2280 delegtype = NFSV4OPEN_DELEGATEREAD;
2282 nop->nfso_opencnt = 0;
2283 nop->nfso_posixlock = 1;
2284 nop->nfso_fhlen = dp->nfsdl_fhlen;
2285 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2286 LIST_INIT(&nop->nfso_lock);
2287 nop->nfso_stateid.seqid = 0;
2288 nop->nfso_stateid.other[0] = 0;
2289 nop->nfso_stateid.other[1] = 0;
2290 nop->nfso_stateid.other[2] = 0;
2291 newnfs_copycred(&dp->nfsdl_cred, tcred);
2292 newnfs_copyincred(tcred, &nop->nfso_cred);
2294 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2295 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2296 nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2297 delegtype, tcred, p);
2299 if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2300 mode = NFSV4OPEN_ACCESSWRITE;
2302 mode = NFSV4OPEN_ACCESSREAD;
2303 if ((nop->nfso_mode & mode) == mode &&
2304 nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2305 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2307 dp->nfsdl_stateid = tdp->nfsdl_stateid;
2308 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2309 dp->nfsdl_ace = tdp->nfsdl_ace;
2310 dp->nfsdl_change = tdp->nfsdl_change;
2311 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2312 if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2313 dp->nfsdl_flags |= NFSCLDL_RECALL;
2314 free(tdp, M_NFSCLDELEG);
2316 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2322 free(nop, M_NFSCLOPEN);
2323 if (error == NFSERR_NOGRACE && !recovered_one &&
2324 NFSHASNFSV4N(nmp)) {
2326 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
2327 * actually end up here, since the client will do
2328 * a recovery for NFSERR_BADSESSION, but will get
2329 * an NFSERR_NOGRACE reply for the first "reclaim"
2331 * So, call nfscl_expireclient() to recover the
2332 * opens as best we can and then do a reclaim
2333 * complete and return.
2335 nfsrpc_reclaimcomplete(nmp, cred, p);
2336 nfscl_expireclient(clp, nmp, tcred, p);
2337 free(nowp, M_NFSCLOWNER);
2341 * Couldn't reclaim it, so throw the state
2344 nfscl_cleandeleg(dp);
2345 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
2347 recovered_one = true;
2348 LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2355 * Now, get rid of extra Opens and Delegations.
2357 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2359 newnfs_copycred(&op->nfso_cred, tcred);
2360 error = nfscl_tryclose(op, tcred, nmp, p);
2361 if (error == NFSERR_GRACE)
2362 (void) nfs_catnap(PZERO, error, "nfsexcls");
2363 } while (error == NFSERR_GRACE);
2364 LIST_REMOVE(op, nfso_list);
2365 free(op, M_NFSCLOPEN);
2368 free(nowp, M_NFSCLOWNER);
2370 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2372 newnfs_copycred(&dp->nfsdl_cred, tcred);
2373 error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2374 if (error == NFSERR_GRACE)
2375 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2376 } while (error == NFSERR_GRACE);
2377 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2378 free(dp, M_NFSCLDELEG);
2381 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2382 if (NFSHASNFSV4N(nmp))
2383 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
2387 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2388 wakeup(&clp->nfsc_flags);
2389 nfsv4_unlock(&clp->nfsc_lock, 0);
2396 * This function is called when a server replies with NFSERR_EXPIRED.
2397 * It deletes all state for the client and does a fresh SetClientId/confirm.
2398 * XXX Someday it should post a signal to the process(es) that hold the
2399 * state, so they know that lock state has been lost.
2402 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2404 struct nfsmount *nmp;
2406 int igotlock = 0, error, trycnt;
2409 * If the clientid has gone away or a new SetClientid has already
2410 * been done, just return ok.
2412 if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2416 * First, lock the client structure, so everyone else will
2417 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2418 * that only one thread does the work.
2421 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2423 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2424 NFSCLSTATEMUTEXPTR, NULL);
2425 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2426 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2428 nfsv4_unlock(&clp->nfsc_lock, 0);
2432 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2435 nmp = clp->nfsc_nmp;
2437 panic("nfscl expired");
2438 cred = newnfs_getcred();
2441 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2442 } while ((error == NFSERR_STALECLIENTID ||
2443 error == NFSERR_BADSESSION ||
2444 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2447 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2450 * Expire the state for the client.
2452 nfscl_expireclient(clp, nmp, cred, p);
2454 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2455 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2457 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2458 wakeup(&clp->nfsc_flags);
2459 nfsv4_unlock(&clp->nfsc_lock, 0);
2466 * This function inserts a lock in the list after insert_lop.
2469 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2470 struct nfscllock *insert_lop, int local)
2473 if ((struct nfscllockowner *)insert_lop == lp)
2474 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2476 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2478 nfsstatsv1.cllocallocks++;
2480 nfsstatsv1.cllocks++;
2484 * This function updates the locking for a lock owner and given file. It
2485 * maintains a list of lock ranges ordered on increasing file offset that
2486 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2487 * It always adds new_lop to the list and sometimes uses the one pointed
2489 * Returns 1 if the locks were modified, 0 otherwise.
2492 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2493 struct nfscllock **other_lopp, int local)
2495 struct nfscllock *new_lop = *new_lopp;
2496 struct nfscllock *lop, *tlop, *ilop;
2497 struct nfscllock *other_lop;
2498 int unlock = 0, modified = 0;
2502 * Work down the list until the lock is merged.
2504 if (new_lop->nfslo_type == F_UNLCK)
2506 ilop = (struct nfscllock *)lp;
2507 lop = LIST_FIRST(&lp->nfsl_lock);
2508 while (lop != NULL) {
2510 * Only check locks for this file that aren't before the start of
2513 if (lop->nfslo_end >= new_lop->nfslo_first) {
2514 if (new_lop->nfslo_end < lop->nfslo_first) {
2516 * If the new lock ends before the start of the
2517 * current lock's range, no merge, just insert
2522 if (new_lop->nfslo_type == lop->nfslo_type ||
2523 (new_lop->nfslo_first <= lop->nfslo_first &&
2524 new_lop->nfslo_end >= lop->nfslo_end)) {
2526 * This lock can be absorbed by the new lock/unlock.
2527 * This happens when it covers the entire range
2528 * of the old lock or is contiguous
2529 * with the old lock and is of the same type or an
2532 if (new_lop->nfslo_type != lop->nfslo_type ||
2533 new_lop->nfslo_first != lop->nfslo_first ||
2534 new_lop->nfslo_end != lop->nfslo_end)
2536 if (lop->nfslo_first < new_lop->nfslo_first)
2537 new_lop->nfslo_first = lop->nfslo_first;
2538 if (lop->nfslo_end > new_lop->nfslo_end)
2539 new_lop->nfslo_end = lop->nfslo_end;
2541 lop = LIST_NEXT(lop, nfslo_list);
2542 nfscl_freelock(tlop, local);
2547 * All these cases are for contiguous locks that are not the
2548 * same type, so they can't be merged.
2550 if (new_lop->nfslo_first <= lop->nfslo_first) {
2552 * This case is where the new lock overlaps with the
2553 * first part of the old lock. Move the start of the
2554 * old lock to just past the end of the new lock. The
2555 * new lock will be inserted in front of the old, since
2556 * ilop hasn't been updated. (We are done now.)
2558 if (lop->nfslo_first != new_lop->nfslo_end) {
2559 lop->nfslo_first = new_lop->nfslo_end;
2564 if (new_lop->nfslo_end >= lop->nfslo_end) {
2566 * This case is where the new lock overlaps with the
2567 * end of the old lock's range. Move the old lock's
2568 * end to just before the new lock's first and insert
2569 * the new lock after the old lock.
2570 * Might not be done yet, since the new lock could
2571 * overlap further locks with higher ranges.
2573 if (lop->nfslo_end != new_lop->nfslo_first) {
2574 lop->nfslo_end = new_lop->nfslo_first;
2578 lop = LIST_NEXT(lop, nfslo_list);
2582 * The final case is where the new lock's range is in the
2583 * middle of the current lock's and splits the current lock
2584 * up. Use *other_lopp to handle the second part of the
2585 * split old lock range. (We are done now.)
2586 * For unlock, we use new_lop as other_lop and tmp, since
2587 * other_lop and new_lop are the same for this case.
2588 * We noted the unlock case above, so we don't need
2589 * new_lop->nfslo_type any longer.
2591 tmp = new_lop->nfslo_first;
2593 other_lop = new_lop;
2596 other_lop = *other_lopp;
2599 other_lop->nfslo_first = new_lop->nfslo_end;
2600 other_lop->nfslo_end = lop->nfslo_end;
2601 other_lop->nfslo_type = lop->nfslo_type;
2602 lop->nfslo_end = tmp;
2603 nfscl_insertlock(lp, other_lop, lop, local);
2609 lop = LIST_NEXT(lop, nfslo_list);
2615 * Insert the new lock in the list at the appropriate place.
2618 nfscl_insertlock(lp, new_lop, ilop, local);
2626 * This function must be run as a kernel thread.
2627 * It does Renew Ops and recovery, when required.
2630 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2632 struct nfsclowner *owp, *nowp;
2633 struct nfsclopen *op;
2634 struct nfscllockowner *lp, *nlp;
2635 struct nfscldeleghead dh;
2636 struct nfscldeleg *dp, *ndp;
2639 int error, cbpathdown, islept, igotlock, ret, clearok;
2640 uint32_t recover_done_time = 0;
2642 static time_t prevsec = 0;
2643 struct nfscllockownerfh *lfhp, *nlfhp;
2644 struct nfscllockownerfhhead lfh;
2645 struct nfscllayout *lyp, *nlyp;
2646 struct nfscldevinfo *dip, *ndip;
2647 struct nfscllayouthead rlh;
2648 struct nfsclrecalllayout *recallp;
2649 struct nfsclds *dsp;
2654 cred = newnfs_getcred();
2656 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2657 mp = clp->nfsc_nmp->nm_mountp;
2660 newnfs_setroot(cred);
2662 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2664 * Only allow one full recover within 1/2 of the lease
2665 * duration (nfsc_renew).
2666 * retok is value/result. If passed in set to true,
2667 * it indicates only a CreateSession operation should
2669 * If it is returned true, it indicates that the
2670 * recovery only required a CreateSession.
2673 if (recover_done_time < NFSD_MONOSEC) {
2674 recover_done_time = NFSD_MONOSEC +
2678 NFSCL_DEBUG(1, "Doing recovery, only "
2679 "createsession=%d\n", retok);
2680 nfscl_recover(clp, &retok, cred, p);
2682 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2683 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2684 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2685 clidrev = clp->nfsc_clientidrev;
2686 error = nfsrpc_renew(clp, NULL, cred, p);
2687 if (error == NFSERR_CBPATHDOWN)
2689 else if (error == NFSERR_STALECLIENTID ||
2690 error == NFSERR_BADSESSION) {
2692 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2694 } else if (error == NFSERR_EXPIRED)
2695 (void) nfscl_hasexpired(clp, clidrev, p);
2699 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2700 /* Do renews for any DS sessions. */
2701 NFSLOCKMNT(clp->nfsc_nmp);
2702 /* Skip first entry, since the MDS is handled above. */
2703 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2705 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2706 while (dsp != NULL) {
2707 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2708 dsp->nfsclds_sess.nfsess_defunct == 0) {
2709 dsp->nfsclds_expire = NFSD_MONOSEC +
2711 NFSUNLOCKMNT(clp->nfsc_nmp);
2712 (void)nfsrpc_renew(clp, dsp, cred, p);
2715 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2717 NFSUNLOCKMNT(clp->nfsc_nmp);
2723 /* It's a Total Recall! */
2724 nfscl_totalrecall(clp);
2727 * Now, handle defunct owners.
2729 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2730 if (LIST_EMPTY(&owp->nfsow_open)) {
2731 if (owp->nfsow_defunct != 0)
2732 nfscl_freeopenowner(owp, 0);
2737 * Do the recall on any delegations. To avoid trouble, always
2738 * come back up here after having slept.
2742 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2743 while (dp != NULL) {
2744 ndp = TAILQ_NEXT(dp, nfsdl_list);
2745 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2747 * Wait for outstanding I/O ops to be done.
2749 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2751 nfsv4_unlock(&clp->nfsc_lock, 0);
2754 dp->nfsdl_rwlock.nfslock_lock |=
2756 msleep(&dp->nfsdl_rwlock,
2757 NFSCLSTATEMUTEXPTR, PVFS, "nfscld",
2759 if (NFSCL_FORCEDISM(mp))
2764 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2765 &islept, NFSCLSTATEMUTEXPTR, mp);
2766 if (igotlock == 0 && NFSCL_FORCEDISM(mp))
2772 newnfs_copycred(&dp->nfsdl_cred, cred);
2773 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2774 NULL, cred, p, 1, &vp);
2776 nfscl_cleandeleg(dp);
2777 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2779 LIST_REMOVE(dp, nfsdl_hash);
2780 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2782 nfsstatsv1.cldelegates--;
2786 * The nfsc_lock must be released before doing
2787 * vrele(), since it might call nfs_inactive().
2788 * For the unlikely case where the vnode failed
2789 * to be acquired by nfscl_recalldeleg(), a
2790 * VOP_RECLAIM() should be in progress and it
2791 * will return the delegation.
2793 nfsv4_unlock(&clp->nfsc_lock, 0);
2806 * Clear out old delegations, if we are above the high water
2807 * mark. Only clear out ones with no state related to them.
2808 * The tailq list is in LRU order.
2810 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2811 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2812 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2813 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2814 dp->nfsdl_rwlock.nfslock_lock == 0 &&
2815 dp->nfsdl_timestamp < NFSD_MONOSEC &&
2816 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2817 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2819 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2820 op = LIST_FIRST(&owp->nfsow_open);
2827 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2828 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2835 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2836 LIST_REMOVE(dp, nfsdl_hash);
2837 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2839 nfsstatsv1.cldelegates--;
2845 nfsv4_unlock(&clp->nfsc_lock, 0);
2848 * Do the recall on any layouts. To avoid trouble, always
2849 * come back up here after having slept.
2853 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2854 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2856 * Wait for outstanding I/O ops to be done.
2858 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2859 (lyp->nfsly_lock.nfslock_lock &
2860 NFSV4LOCK_LOCK) != 0) {
2861 lyp->nfsly_lock.nfslock_lock |=
2863 msleep(&lyp->nfsly_lock.nfslock_lock,
2864 NFSCLSTATEMUTEXPTR, PVFS, "nfslyp",
2866 if (NFSCL_FORCEDISM(mp))
2870 /* Move the layout to the recall list. */
2871 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2873 LIST_REMOVE(lyp, nfsly_hash);
2874 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2876 /* Handle any layout commits. */
2877 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2878 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2879 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2881 NFSCL_DEBUG(3, "do layoutcommit\n");
2882 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2890 /* Now, look for stale layouts. */
2891 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2892 while (lyp != NULL) {
2893 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2894 if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2895 (lyp->nfsly_flags & NFSLY_RECALL) == 0 &&
2896 lyp->nfsly_lock.nfslock_usecnt == 0 &&
2897 lyp->nfsly_lock.nfslock_lock == 0) {
2898 NFSCL_DEBUG(4, "ret stale lay=%d\n",
2900 recallp = malloc(sizeof(*recallp),
2901 M_NFSLAYRECALL, M_NOWAIT);
2902 if (recallp == NULL)
2904 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2905 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2906 lyp->nfsly_stateid.seqid, 0, 0, NULL,
2913 * Free up any unreferenced device info structures.
2915 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
2916 if (dip->nfsdi_layoutrefs == 0 &&
2917 dip->nfsdi_refcnt == 0) {
2918 NFSCL_DEBUG(4, "freeing devinfo\n");
2919 LIST_REMOVE(dip, nfsdi_list);
2920 nfscl_freedevinfo(dip);
2925 /* Do layout return(s), as required. */
2926 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
2927 TAILQ_REMOVE(&rlh, lyp, nfsly_list);
2928 NFSCL_DEBUG(4, "ret layout\n");
2929 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
2930 nfscl_freelayout(lyp);
2934 * Delegreturn any delegations cleaned out or recalled.
2936 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2937 newnfs_copycred(&dp->nfsdl_cred, cred);
2938 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2939 TAILQ_REMOVE(&dh, dp, nfsdl_list);
2940 free(dp, M_NFSCLDELEG);
2945 * Call nfscl_cleanupkext() once per second to check for
2946 * open/lock owners where the process has exited.
2948 mytime = NFSD_MONOSEC;
2949 if (prevsec != mytime) {
2951 nfscl_cleanupkext(clp, &lfh);
2955 * Do a ReleaseLockOwner for all lock owners where the
2956 * associated process no longer exists, as found by
2957 * nfscl_cleanupkext().
2959 newnfs_setroot(cred);
2960 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
2961 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
2963 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
2964 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
2966 nfscl_freelockowner(lp, 0);
2973 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2974 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
2977 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2978 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2981 wakeup((caddr_t)clp);
2989 * Initiate state recovery. Called when NFSERR_STALECLIENTID,
2990 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
2993 nfscl_initiate_recovery(struct nfsclclient *clp)
2999 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
3001 wakeup((caddr_t)clp);
3005 * Dump out the state stuff for debugging.
3008 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
3009 int lockowner, int locks)
3011 struct nfsclclient *clp;
3012 struct nfsclowner *owp;
3013 struct nfsclopen *op;
3014 struct nfscllockowner *lp;
3015 struct nfscllock *lop;
3016 struct nfscldeleg *dp;
3020 printf("nfscl dumpstate NULL clp\n");
3024 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
3025 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3026 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
3027 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
3028 owp->nfsow_owner[0], owp->nfsow_owner[1],
3029 owp->nfsow_owner[2], owp->nfsow_owner[3],
3031 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3033 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
3034 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
3035 op->nfso_stateid.other[2], op->nfso_opencnt,
3037 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
3039 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
3040 lp->nfsl_owner[0], lp->nfsl_owner[1],
3041 lp->nfsl_owner[2], lp->nfsl_owner[3],
3043 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
3044 lp->nfsl_stateid.other[2]);
3045 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3048 printf("lck typ=%d fst=%ju end=%ju\n",
3049 lop->nfslo_type, (intmax_t)lop->nfslo_first,
3050 (intmax_t)lop->nfslo_end);
3052 printf("lck typ=%d fst=%qd end=%qd\n",
3053 lop->nfslo_type, lop->nfslo_first,
3061 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3062 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
3063 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
3064 owp->nfsow_owner[0], owp->nfsow_owner[1],
3065 owp->nfsow_owner[2], owp->nfsow_owner[3],
3067 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3069 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
3070 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
3071 op->nfso_stateid.other[2], op->nfso_opencnt,
3073 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
3075 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
3076 lp->nfsl_owner[0], lp->nfsl_owner[1],
3077 lp->nfsl_owner[2], lp->nfsl_owner[3],
3079 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
3080 lp->nfsl_stateid.other[2]);
3081 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3084 printf("lck typ=%d fst=%ju end=%ju\n",
3085 lop->nfslo_type, (intmax_t)lop->nfslo_first,
3086 (intmax_t)lop->nfslo_end);
3088 printf("lck typ=%d fst=%qd end=%qd\n",
3089 lop->nfslo_type, lop->nfslo_first,
3100 * Check for duplicate open owners and opens.
3101 * (Only used as a diagnostic aid.)
3104 nfscl_dupopen(vnode_t vp, int dupopens)
3106 struct nfsclclient *clp;
3107 struct nfsclowner *owp, *owp2;
3108 struct nfsclopen *op, *op2;
3111 clp = VFSTONFS(vp->v_mount)->nm_clp;
3113 printf("nfscl dupopen NULL clp\n");
3116 nfhp = VTONFS(vp)->n_fhp;
3120 * First, search for duplicate owners.
3121 * These should never happen!
3123 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3124 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3126 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
3127 NFSV4CL_LOCKNAMELEN)) {
3129 printf("DUP OWNER\n");
3130 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3137 * Now, search for duplicate stateids.
3138 * These shouldn't happen, either.
3140 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3141 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3142 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3143 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3145 (op->nfso_stateid.other[0] != 0 ||
3146 op->nfso_stateid.other[1] != 0 ||
3147 op->nfso_stateid.other[2] != 0) &&
3148 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
3149 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
3150 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
3152 printf("DUP STATEID\n");
3153 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3162 * Now search for duplicate opens.
3163 * Duplicate opens for the same owner
3164 * should never occur. Other duplicates are
3165 * possible and are checked for if "dupopens"
3168 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3169 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3170 if (nfhp->nfh_len == op2->nfso_fhlen &&
3171 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3172 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3173 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3174 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3175 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3176 (!NFSBCMP(op->nfso_own->nfsow_owner,
3177 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3179 if (!NFSBCMP(op->nfso_own->nfsow_owner,
3180 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3182 printf("BADDUP OPEN\n");
3185 printf("DUP OPEN\n");
3187 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0,
3200 * During close, find an open that needs to be dereferenced and
3201 * dereference it. If there are no more opens for this file,
3202 * log a message to that effect.
3203 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3204 * on the file's vnode.
3205 * This is the safe way, since it is difficult to identify
3206 * which open the close is for and I/O can be performed after the
3207 * close(2) system call when a file is mmap'd.
3208 * If it returns 0 for success, there will be a referenced
3209 * clp returned via clpp.
3212 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3214 struct nfsclclient *clp;
3215 struct nfsclowner *owp;
3216 struct nfsclopen *op;
3217 struct nfscldeleg *dp;
3221 error = nfscl_getcl(vp->v_mount, NULL, NULL, false, &clp);
3226 nfhp = VTONFS(vp)->n_fhp;
3230 * First, look for one under a delegation that was locally issued
3231 * and just decrement the opencnt for it. Since all my Opens against
3232 * the server are DENY_NONE, I don't see a problem with hanging
3233 * onto them. (It is much easier to use one of the extant Opens
3234 * that I already have on the server when a Delegation is recalled
3235 * than to do fresh Opens.) Someday, I might need to rethink this, but.
3237 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3239 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3240 op = LIST_FIRST(&owp->nfsow_open);
3243 * Since a delegation is for a file, there
3244 * should never be more than one open for
3247 if (LIST_NEXT(op, nfso_list) != NULL)
3248 panic("nfscdeleg opens");
3249 if (notdecr && op->nfso_opencnt > 0) {
3258 /* Now process the opens against the server. */
3259 LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len),
3261 if (op->nfso_fhlen == nfhp->nfh_len &&
3262 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3264 /* Found an open, decrement cnt if possible */
3265 if (notdecr && op->nfso_opencnt > 0) {
3270 * There are more opens, so just return.
3272 if (op->nfso_opencnt > 0) {
3280 printf("nfscl: never fnd open\n");
3285 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3287 struct nfsclclient *clp;
3288 struct nfsclowner *owp, *nowp;
3289 struct nfsclopen *op;
3290 struct nfscldeleg *dp;
3292 struct nfsclrecalllayout *recallp;
3295 error = nfscl_getcl(vp->v_mount, NULL, NULL, false, &clp);
3300 nfhp = VTONFS(vp)->n_fhp;
3301 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
3304 * First get rid of the local Open structures, which should be no
3307 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3309 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3310 op = LIST_FIRST(&owp->nfsow_open);
3312 KASSERT((op->nfso_opencnt == 0),
3313 ("nfscl: bad open cnt on deleg"));
3314 nfscl_freeopen(op, 1);
3316 nfscl_freeopenowner(owp, 1);
3320 /* Return any layouts marked return on close. */
3321 nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp);
3323 /* Now process the opens against the server. */
3325 LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len),
3327 if (op->nfso_fhlen == nfhp->nfh_len &&
3328 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3330 /* Found an open, close it. */
3332 KASSERT((op->nfso_opencnt == 0),
3333 ("nfscl: bad open cnt on server (%d)",
3337 nfsrpc_doclose(VFSTONFS(vp->v_mount), op, p);
3344 * recallp has been set NULL by nfscl_retoncloselayout() if it was
3345 * used by the function, but calling free() with a NULL pointer is ok.
3347 free(recallp, M_NFSLAYRECALL);
3352 * Return all delegations on this client.
3353 * (Must be called with client sleep lock.)
3356 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
3358 struct nfscldeleg *dp, *ndp;
3361 cred = newnfs_getcred();
3362 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3363 nfscl_cleandeleg(dp);
3364 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3365 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
3371 * Return any delegation for this vp.
3374 nfscl_delegreturnvp(vnode_t vp, NFSPROC_T *p)
3376 struct nfsclclient *clp;
3377 struct nfscldeleg *dp;
3380 struct nfsmount *nmp;
3382 nmp = VFSTONFS(vp->v_mount);
3384 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
3390 cred = newnfs_getcred();
3395 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3396 np->n_fhp->nfh_len);
3398 nfscl_cleandeleg(dp);
3399 nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
3401 newnfs_copycred(&dp->nfsdl_cred, cred);
3402 nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3403 free(dp, M_NFSCLDELEG);
3410 * Do a callback RPC.
3413 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3415 int clist, gotseq_ok, i, j, k, op, rcalls;
3417 struct nfsclclient *clp;
3418 struct nfscldeleg *dp = NULL;
3419 int numops, taglen = -1, error = 0, trunc __unused;
3420 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3421 u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3427 nfsattrbit_t attrbits, rattrbits;
3428 nfsv4stateid_t stateid;
3429 uint32_t seqid, slotid = 0, highslot, cachethis __unused;
3430 uint8_t sessionid[NFSX_V4SESSIONID];
3432 struct nfscllayout *lyp;
3433 uint64_t filesid[2], len, off;
3434 int changed, gotone, laytype, recalltype;
3436 struct nfsclrecalllayout *recallp = NULL;
3437 struct nfsclsession *tsep;
3441 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3442 taglen = fxdr_unsigned(int, *tl);
3447 if (taglen <= NFSV4_SMALLSTR)
3450 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3451 error = nfsrv_mtostr(nd, tagstr, taglen);
3453 if (taglen > NFSV4_SMALLSTR)
3454 free(tagstr, M_TEMP);
3458 (void) nfsm_strtom(nd, tag, taglen);
3459 if (taglen > NFSV4_SMALLSTR) {
3460 free(tagstr, M_TEMP);
3462 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3463 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3464 minorvers = fxdr_unsigned(u_int32_t, *tl++);
3465 if (minorvers != NFSV4_MINORVERSION &&
3466 minorvers != NFSV41_MINORVERSION &&
3467 minorvers != NFSV42_MINORVERSION)
3468 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3469 cbident = fxdr_unsigned(u_int32_t, *tl++);
3473 numops = fxdr_unsigned(int, *tl);
3475 * Loop around doing the sub ops.
3477 for (i = 0; i < numops; i++) {
3478 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3479 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3481 op = fxdr_unsigned(int, *tl);
3482 if (op < NFSV4OP_CBGETATTR ||
3483 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3484 (op > NFSV4OP_CBNOTIFYDEVID &&
3485 minorvers == NFSV41_MINORVERSION) ||
3486 (op > NFSV4OP_CBOFFLOAD &&
3487 minorvers == NFSV42_MINORVERSION)) {
3488 nd->nd_repstat = NFSERR_OPILLEGAL;
3489 *repp = nfscl_errmap(nd, minorvers);
3493 nd->nd_procnum = op;
3494 if (op < NFSV42_CBNOPS)
3495 nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
3497 case NFSV4OP_CBGETATTR:
3498 NFSCL_DEBUG(4, "cbgetattr\n");
3501 error = nfsm_getfh(nd, &nfhp);
3503 error = nfsrv_getattrbits(nd, &attrbits,
3505 if (error == 0 && i == 0 &&
3506 minorvers != NFSV4_MINORVERSION)
3507 error = NFSERR_OPNOTINSESS;
3509 mp = nfscl_getmnt(minorvers, sessionid, cbident,
3512 error = NFSERR_SERVERFAULT;
3515 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3516 nfhp->nfh_len, p, &np);
3521 NFSZERO_ATTRBIT(&rattrbits);
3523 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3526 if (NFSISSET_ATTRBIT(&attrbits,
3529 va.va_size = np->n_size;
3533 NFSSETBIT_ATTRBIT(&rattrbits,
3536 if (NFSISSET_ATTRBIT(&attrbits,
3537 NFSATTRBIT_CHANGE)) {
3541 (np->n_flag & NDELEGMOD))
3543 NFSSETBIT_ATTRBIT(&rattrbits,
3547 error = NFSERR_SERVERFAULT;
3555 free(nfhp, M_NFSFH);
3557 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3558 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3561 case NFSV4OP_CBRECALL:
3562 NFSCL_DEBUG(4, "cbrecall\n");
3563 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3565 stateid.seqid = *tl++;
3566 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3568 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3569 trunc = fxdr_unsigned(int, *tl);
3570 error = nfsm_getfh(nd, &nfhp);
3571 if (error == 0 && i == 0 &&
3572 minorvers != NFSV4_MINORVERSION)
3573 error = NFSERR_OPNOTINSESS;
3576 if (minorvers == NFSV4_MINORVERSION)
3577 clp = nfscl_getclnt(cbident);
3579 clp = nfscl_getclntsess(sessionid);
3581 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3583 if (dp != NULL && (dp->nfsdl_flags &
3584 NFSCLDL_DELEGRET) == 0) {
3587 wakeup((caddr_t)clp);
3590 error = NFSERR_SERVERFAULT;
3595 free(nfhp, M_NFSFH);
3597 case NFSV4OP_CBLAYOUTRECALL:
3598 NFSCL_DEBUG(4, "cblayrec\n");
3600 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3601 laytype = fxdr_unsigned(int, *tl++);
3602 iomode = fxdr_unsigned(uint32_t, *tl++);
3603 if (newnfs_true == *tl++)
3607 recalltype = fxdr_unsigned(int, *tl);
3608 NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
3609 laytype, iomode, changed, recalltype);
3610 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3612 if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
3613 laytype != NFSLAYOUT_FLEXFILE)
3614 error = NFSERR_NOMATCHLAYOUT;
3615 else if (recalltype == NFSLAYOUTRETURN_FILE) {
3616 error = nfsm_getfh(nd, &nfhp);
3617 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3620 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3622 off = fxdr_hyper(tl); tl += 2;
3623 len = fxdr_hyper(tl); tl += 2;
3624 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3625 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3626 if (minorvers == NFSV4_MINORVERSION)
3627 error = NFSERR_NOTSUPP;
3629 error = NFSERR_OPNOTINSESS;
3630 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
3631 (uintmax_t)off, (uintmax_t)len,
3632 stateid.seqid, error);
3635 clp = nfscl_getclntsess(sessionid);
3636 NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3638 lyp = nfscl_findlayout(clp,
3641 NFSCL_DEBUG(4, "cblyp=%p\n",
3646 NFSLY_FLEXFILE)) != 0 &&
3647 !NFSBCMP(stateid.other,
3648 lyp->nfsly_stateid.other,
3649 NFSX_STATEIDOTHER)) {
3659 lyp->nfsly_stateid.seqid)
3660 lyp->nfsly_stateid.seqid =
3668 lyp->nfsly_stateid.seqid);
3671 NFSERR_NOMATCHLAYOUT;
3673 error = NFSERR_NOMATCHLAYOUT;
3676 free(nfhp, M_NFSFH);
3677 } else if (recalltype == NFSLAYOUTRETURN_FSID) {
3678 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3679 filesid[0] = fxdr_hyper(tl); tl += 2;
3680 filesid[1] = fxdr_hyper(tl); tl += 2;
3683 clp = nfscl_getclntsess(sessionid);
3685 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3687 if (lyp->nfsly_filesid[0] ==
3689 lyp->nfsly_filesid[1] ==
3696 lyp->nfsly_stateid.seqid,
3706 error = NFSERR_NOMATCHLAYOUT;
3708 error = NFSERR_NOMATCHLAYOUT;
3710 } else if (recalltype == NFSLAYOUTRETURN_ALL) {
3713 clp = nfscl_getclntsess(sessionid);
3715 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3717 error = nfscl_layoutrecall(
3718 recalltype, lyp, iomode, 0,
3720 lyp->nfsly_stateid.seqid,
3721 0, 0, NULL, recallp);
3728 error = NFSERR_NOMATCHLAYOUT;
3730 error = NFSERR_NOMATCHLAYOUT;
3733 error = NFSERR_NOMATCHLAYOUT;
3734 if (recallp != NULL) {
3735 free(recallp, M_NFSLAYRECALL);
3739 case NFSV4OP_CBSEQUENCE:
3740 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3742 bcopy(tl, sessionid, NFSX_V4SESSIONID);
3743 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3744 seqid = fxdr_unsigned(uint32_t, *tl++);
3745 slotid = fxdr_unsigned(uint32_t, *tl++);
3746 highslot = fxdr_unsigned(uint32_t, *tl++);
3748 /* Throw away the referring call stuff. */
3749 clist = fxdr_unsigned(int, *tl);
3750 for (j = 0; j < clist; j++) {
3751 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3753 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3754 rcalls = fxdr_unsigned(int, *tl);
3755 for (k = 0; k < rcalls; k++) {
3756 NFSM_DISSECT(tl, uint32_t *,
3762 clp = nfscl_getclntsess(sessionid);
3764 error = NFSERR_SERVERFAULT;
3766 error = NFSERR_SEQUENCEPOS;
3768 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3769 error = nfsv4_seqsession(seqid, slotid,
3770 highslot, tsep->nfsess_cbslots, &rep,
3771 tsep->nfsess_backslots);
3774 if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
3778 * Handle a reply for a retried
3779 * callback. The reply will be
3780 * re-inserted in the session cache
3781 * by the nfsv4_seqsess_cacherep() call
3784 KASSERT(error == NFSERR_REPLYFROMCACHE,
3785 ("cbsequence: non-NULL rep"));
3786 NFSCL_DEBUG(4, "Got cbretry\n");
3787 m_freem(nd->nd_mreq);
3792 NFSM_BUILD(tl, uint32_t *,
3793 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3794 bcopy(sessionid, tl, NFSX_V4SESSIONID);
3795 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3796 *tl++ = txdr_unsigned(seqid);
3797 *tl++ = txdr_unsigned(slotid);
3798 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3799 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3803 if (i == 0 && minorvers != NFSV4_MINORVERSION)
3804 error = NFSERR_OPNOTINSESS;
3806 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3807 error = NFSERR_NOTSUPP;
3812 if (error == EBADRPC || error == NFSERR_BADXDR) {
3813 nd->nd_repstat = NFSERR_BADXDR;
3815 nd->nd_repstat = error;
3820 if (nd->nd_repstat) {
3821 *repp = nfscl_errmap(nd, minorvers);
3824 *repp = 0; /* NFS4_OK */
3827 if (recallp != NULL)
3828 free(recallp, M_NFSLAYRECALL);
3830 if (error == EBADRPC || error == NFSERR_BADXDR)
3831 nd->nd_repstat = NFSERR_BADXDR;
3833 printf("nfsv4 comperr1=%d\n", error);
3836 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3840 *retopsp = txdr_unsigned(retops);
3842 *nd->nd_errp = nfscl_errmap(nd, minorvers);
3844 if (gotseq_ok != 0) {
3845 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3847 clp = nfscl_getclntsess(sessionid);
3849 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3850 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3861 * Generate the next cbident value. Basically just increment a static value
3862 * and then check that it isn't already in the list, if it has wrapped around.
3865 nfscl_nextcbident(void)
3867 struct nfsclclient *clp;
3869 static u_int32_t nextcbident = 0;
3870 static int haswrapped = 0;
3873 if (nextcbident == 0)
3877 * Search the clientid list for one already using this cbident.
3882 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3883 if (clp->nfsc_cbident == nextcbident) {
3893 return (nextcbident);
3897 * Get the mount point related to a given cbident or session and busy it.
3900 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
3901 struct nfsclclient **clpp)
3903 struct nfsclclient *clp;
3906 struct nfsclsession *tsep;
3910 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3911 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3912 if (minorvers == NFSV4_MINORVERSION) {
3913 if (clp->nfsc_cbident == cbident)
3915 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3923 mp = clp->nfsc_nmp->nm_mountp;
3926 error = vfs_busy(mp, 0);
3935 * Get the clientid pointer related to a given cbident.
3937 static struct nfsclclient *
3938 nfscl_getclnt(u_int32_t cbident)
3940 struct nfsclclient *clp;
3942 LIST_FOREACH(clp, &nfsclhead, nfsc_list)
3943 if (clp->nfsc_cbident == cbident)
3949 * Get the clientid pointer related to a given sessionid.
3951 static struct nfsclclient *
3952 nfscl_getclntsess(uint8_t *sessionid)
3954 struct nfsclclient *clp;
3955 struct nfsclsession *tsep;
3957 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3958 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3959 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3967 * Search for a lock conflict locally on the client. A conflict occurs if
3968 * - not same owner and overlapping byte range and at least one of them is
3969 * a write lock or this is an unlock.
3972 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
3973 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
3974 struct nfscllock **lopp)
3976 struct nfsclopen *op;
3980 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3984 LIST_FOREACH(op, NFSCLOPENHASH(clp, fhp, fhlen), nfso_hash) {
3985 if (op->nfso_fhlen == fhlen &&
3986 !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
3987 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
3997 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3998 u_int8_t *own, struct nfscllock **lopp)
4000 struct nfscllockowner *lp;
4001 struct nfscllock *lop;
4003 LIST_FOREACH(lp, lhp, nfsl_list) {
4004 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
4005 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
4006 if (lop->nfslo_first >= nlop->nfslo_end)
4008 if (lop->nfslo_end <= nlop->nfslo_first)
4010 if (lop->nfslo_type == F_WRLCK ||
4011 nlop->nfslo_type == F_WRLCK ||
4012 nlop->nfslo_type == F_UNLCK) {
4015 return (NFSERR_DENIED);
4024 * Check for a local conflicting lock.
4027 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
4028 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
4030 struct nfscllock *lop, nlck;
4031 struct nfscldeleg *dp;
4033 u_int8_t own[NFSV4CL_LOCKNAMELEN];
4036 nlck.nfslo_type = fl->l_type;
4037 nlck.nfslo_first = off;
4038 if (len == NFS64BITSSET) {
4039 nlck.nfslo_end = NFS64BITSSET;
4041 nlck.nfslo_end = off + len;
4042 if (nlck.nfslo_end <= nlck.nfslo_first)
4043 return (NFSERR_INVAL);
4046 nfscl_filllockowner(id, own, flags);
4048 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4049 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
4050 &nlck, own, dp, &lop);
4052 fl->l_whence = SEEK_SET;
4053 fl->l_start = lop->nfslo_first;
4054 if (lop->nfslo_end == NFS64BITSSET)
4057 fl->l_len = lop->nfslo_end - lop->nfslo_first;
4058 fl->l_pid = (pid_t)0;
4059 fl->l_type = lop->nfslo_type;
4060 error = -1; /* no RPC required */
4061 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
4062 fl->l_type == F_RDLCK)) {
4064 * The delegation ensures that there isn't a conflicting
4065 * lock on the server, so return -1 to indicate an RPC
4068 fl->l_type = F_UNLCK;
4076 * Handle Recall of a delegation.
4077 * The clp must be exclusive locked when this is called.
4080 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
4081 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4082 int called_from_renewthread, vnode_t *vpp)
4084 struct nfsclowner *owp, *lowp, *nowp;
4085 struct nfsclopen *op, *lop;
4086 struct nfscllockowner *lp;
4087 struct nfscllock *lckp;
4092 KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL"));
4095 * First, get a vnode for the file. This is needed to do RPCs.
4097 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
4098 dp->nfsdl_fhlen, p, &np);
4101 * File isn't open, so nothing to move over to the
4111 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
4114 * Ok, if it's a write delegation, flush data to the server, so
4115 * that close/open consistency is retained.
4119 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
4120 np->n_flag |= NDELEGRECALL;
4122 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
4124 np->n_flag &= ~NDELEGRECALL;
4126 NFSINVALATTRCACHE(np);
4128 if (ret == EIO && called_from_renewthread != 0) {
4130 * If the flush failed with EIO for the renew thread,
4131 * return now, so that the dirty buffer will be flushed
4138 * Now, for each openowner with opens issued locally, move them
4139 * over to state against the server.
4141 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
4142 lop = LIST_FIRST(&lowp->nfsow_open);
4144 if (LIST_NEXT(lop, nfso_list) != NULL)
4145 panic("nfsdlg mult opens");
4147 * Look for the same openowner against the server.
4149 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
4150 if (!NFSBCMP(lowp->nfsow_owner,
4151 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
4152 newnfs_copycred(&dp->nfsdl_cred, cred);
4153 ret = nfscl_moveopen(vp, clp, nmp, lop,
4155 if (ret == NFSERR_STALECLIENTID ||
4156 ret == NFSERR_STALEDONTRECOVER ||
4157 ret == NFSERR_BADSESSION)
4160 nfscl_freeopen(lop, 1);
4169 * If no openowner found, create one and get an open
4174 sizeof (struct nfsclowner), M_NFSCLOWNER,
4176 nfscl_newopen(clp, NULL, &owp, &nowp, &op,
4177 NULL, lowp->nfsow_owner, dp->nfsdl_fh,
4178 dp->nfsdl_fhlen, NULL, NULL);
4179 newnfs_copycred(&dp->nfsdl_cred, cred);
4180 ret = nfscl_moveopen(vp, clp, nmp, lop,
4183 nfscl_freeopenowner(owp, 0);
4184 if (ret == NFSERR_STALECLIENTID ||
4185 ret == NFSERR_STALEDONTRECOVER ||
4186 ret == NFSERR_BADSESSION)
4189 nfscl_freeopen(lop, 1);
4199 * Now, get byte range locks for any locks done locally.
4201 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4202 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
4203 newnfs_copycred(&dp->nfsdl_cred, cred);
4204 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
4205 if (ret == NFSERR_STALESTATEID ||
4206 ret == NFSERR_STALEDONTRECOVER ||
4207 ret == NFSERR_STALECLIENTID ||
4208 ret == NFSERR_BADSESSION)
4218 * Move a locally issued open over to an owner on the state list.
4219 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
4220 * returns with it unlocked.
4223 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4224 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
4225 struct ucred *cred, NFSPROC_T *p)
4227 struct nfsclopen *op, *nop;
4228 struct nfscldeleg *ndp;
4230 int error = 0, newone;
4233 * First, look for an appropriate open, If found, just increment the
4236 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4237 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4238 op->nfso_fhlen == lop->nfso_fhlen &&
4239 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4240 op->nfso_opencnt += lop->nfso_opencnt;
4241 nfscl_freeopen(lop, 1);
4246 /* No appropriate open, so we have to do one against the server. */
4248 nop = malloc(sizeof (struct nfsclopen) +
4249 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4250 nop->nfso_hash.le_prev = NULL;
4252 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4253 lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
4255 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4256 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4257 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4260 nfscl_freeopen(op, 0);
4262 op->nfso_mode |= lop->nfso_mode;
4263 op->nfso_opencnt += lop->nfso_opencnt;
4264 nfscl_freeopen(lop, 1);
4267 free(nop, M_NFSCLOPEN);
4270 * What should I do with the returned delegation, since the
4271 * delegation is being recalled? For now, just printf and
4274 printf("Moveopen returned deleg\n");
4275 free(ndp, M_NFSCLDELEG);
4281 * Recall all delegations on this client.
4284 nfscl_totalrecall(struct nfsclclient *clp)
4286 struct nfscldeleg *dp;
4288 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4289 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4290 dp->nfsdl_flags |= NFSCLDL_RECALL;
4295 * Relock byte ranges. Called for delegation recall and state expiry.
4298 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4299 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4302 struct nfscllockowner *nlp;
4305 int error, newone, donelocally;
4307 off = lop->nfslo_first;
4308 len = lop->nfslo_end - lop->nfslo_first;
4309 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4310 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4311 lp->nfsl_openowner, &nlp, &newone, &donelocally);
4312 if (error || donelocally)
4314 nfhp = VTONFS(vp)->n_fhp;
4315 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4316 nfhp->nfh_len, nlp, newone, 0, off,
4317 len, lop->nfslo_type, cred, p);
4319 nfscl_freelockowner(nlp, 0);
4324 * Called to re-open a file. Basically get a vnode for the file handle
4325 * and then call nfsrpc_openrpc() to do the rest.
4328 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4329 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4330 struct ucred *cred, NFSPROC_T *p)
4336 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4340 if (np->n_v4 != NULL) {
4341 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4342 np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4343 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4353 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4354 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4358 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4359 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4360 u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4361 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4366 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4367 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4369 if (error == NFSERR_DELAY)
4370 (void) nfs_catnap(PZERO, error, "nfstryop");
4371 } while (error == NFSERR_DELAY);
4372 if (error == EAUTH || error == EACCES) {
4373 /* Try again using system credentials */
4374 newnfs_setroot(cred);
4376 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4377 newfhlen, mode, op, name, namelen, ndpp, reclaim,
4378 delegtype, cred, p, 1, 0);
4379 if (error == NFSERR_DELAY)
4380 (void) nfs_catnap(PZERO, error, "nfstryop");
4381 } while (error == NFSERR_DELAY);
4387 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4388 * NFSERR_DELAY. Also, retry with system credentials, if the provided
4392 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4393 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4394 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4396 struct nfsrv_descript nfsd, *nd = &nfsd;
4400 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4401 reclaim, off, len, type, cred, p, 0);
4402 if (!error && nd->nd_repstat == NFSERR_DELAY)
4403 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4405 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4407 error = nd->nd_repstat;
4408 if (error == EAUTH || error == EACCES) {
4409 /* Try again using root credentials */
4410 newnfs_setroot(cred);
4412 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4413 newone, reclaim, off, len, type, cred, p, 1);
4414 if (!error && nd->nd_repstat == NFSERR_DELAY)
4415 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4417 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4419 error = nd->nd_repstat;
4425 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4426 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4430 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4431 struct nfsmount *nmp, NFSPROC_T *p)
4436 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4437 if (error == NFSERR_DELAY)
4438 (void) nfs_catnap(PZERO, error, "nfstrydp");
4439 } while (error == NFSERR_DELAY);
4440 if (error == EAUTH || error == EACCES) {
4441 /* Try again using system credentials */
4442 newnfs_setroot(cred);
4444 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4445 if (error == NFSERR_DELAY)
4446 (void) nfs_catnap(PZERO, error, "nfstrydp");
4447 } while (error == NFSERR_DELAY);
4453 * Try a close against the server. Just call nfsrpc_closerpc(),
4454 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4458 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4459 struct nfsmount *nmp, NFSPROC_T *p)
4461 struct nfsrv_descript nfsd, *nd = &nfsd;
4465 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4466 if (error == NFSERR_DELAY)
4467 (void) nfs_catnap(PZERO, error, "nfstrycl");
4468 } while (error == NFSERR_DELAY);
4469 if (error == EAUTH || error == EACCES) {
4470 /* Try again using system credentials */
4471 newnfs_setroot(cred);
4473 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4474 if (error == NFSERR_DELAY)
4475 (void) nfs_catnap(PZERO, error, "nfstrycl");
4476 } while (error == NFSERR_DELAY);
4482 * Decide if a delegation on a file permits close without flushing writes
4483 * to the server. This might be a big performance win in some environments.
4484 * (Not useful until the client does caching on local stable storage.)
4487 nfscl_mustflush(vnode_t vp)
4489 struct nfsclclient *clp;
4490 struct nfscldeleg *dp;
4492 struct nfsmount *nmp;
4495 nmp = VFSTONFS(vp->v_mount);
4496 if (!NFSHASNFSV4(nmp))
4499 clp = nfscl_findcl(nmp);
4504 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4505 if (dp != NULL && (dp->nfsdl_flags &
4506 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4508 (dp->nfsdl_sizelimit >= np->n_size ||
4509 !NFSHASSTRICT3530(nmp))) {
4518 * See if a (write) delegation exists for this file.
4521 nfscl_nodeleg(vnode_t vp, int writedeleg)
4523 struct nfsclclient *clp;
4524 struct nfscldeleg *dp;
4526 struct nfsmount *nmp;
4529 nmp = VFSTONFS(vp->v_mount);
4530 if (!NFSHASNFSV4(nmp))
4533 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4539 clp = nfscl_findcl(nmp);
4544 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4546 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4547 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4557 * Look for an associated delegation that should be DelegReturned.
4560 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4562 struct nfsclclient *clp;
4563 struct nfscldeleg *dp;
4564 struct nfsclowner *owp;
4565 struct nfscllockowner *lp;
4566 struct nfsmount *nmp;
4569 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4571 nmp = VFSTONFS(vp->v_mount);
4575 * Loop around waiting for:
4576 * - outstanding I/O operations on delegations to complete
4577 * - for a delegation on vp that has state, lock the client and
4579 * - return delegation with no state
4582 clp = nfscl_findcl(nmp);
4587 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4588 np->n_fhp->nfh_len);
4591 * Wait for outstanding I/O ops to be done.
4593 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4595 nfsv4_unlock(&clp->nfsc_lock, 0);
4598 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4599 (void) nfsmsleep(&dp->nfsdl_rwlock,
4600 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4604 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4605 if (!LIST_EMPTY(&owp->nfsow_open)) {
4611 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4612 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4618 if (needsrecall && !triedrecall) {
4619 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4622 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4623 &islept, NFSCLSTATEMUTEXPTR, NULL);
4630 cred = newnfs_getcred();
4631 newnfs_copycred(&dp->nfsdl_cred, cred);
4632 nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL);
4636 nfsv4_unlock(&clp->nfsc_lock, 0);
4640 *stp = dp->nfsdl_stateid;
4642 nfscl_cleandeleg(dp);
4643 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4646 nfsv4_unlock(&clp->nfsc_lock, 0);
4653 * Look for associated delegation(s) that should be DelegReturned.
4656 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4657 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4659 struct nfsclclient *clp;
4660 struct nfscldeleg *dp;
4661 struct nfsclowner *owp;
4662 struct nfscllockowner *lp;
4663 struct nfsmount *nmp;
4666 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4668 nmp = VFSTONFS(fvp->v_mount);
4673 * Loop around waiting for:
4674 * - outstanding I/O operations on delegations to complete
4675 * - for a delegation on fvp that has state, lock the client and
4677 * - return delegation(s) with no state.
4680 clp = nfscl_findcl(nmp);
4686 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4687 np->n_fhp->nfh_len);
4688 if (dp != NULL && *gotfdp == 0) {
4690 * Wait for outstanding I/O ops to be done.
4692 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4694 nfsv4_unlock(&clp->nfsc_lock, 0);
4697 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4698 (void) nfsmsleep(&dp->nfsdl_rwlock,
4699 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4703 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4704 if (!LIST_EMPTY(&owp->nfsow_open)) {
4710 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4711 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4717 if (needsrecall && !triedrecall) {
4718 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4721 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4722 &islept, NFSCLSTATEMUTEXPTR, NULL);
4729 cred = newnfs_getcred();
4730 newnfs_copycred(&dp->nfsdl_cred, cred);
4731 nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL);
4735 nfsv4_unlock(&clp->nfsc_lock, 0);
4739 *fstp = dp->nfsdl_stateid;
4742 nfscl_cleandeleg(dp);
4743 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4746 nfsv4_unlock(&clp->nfsc_lock, 0);
4751 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4752 np->n_fhp->nfh_len);
4753 if (dp != NULL && *gottdp == 0) {
4755 * Wait for outstanding I/O ops to be done.
4757 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4758 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4759 (void) nfsmsleep(&dp->nfsdl_rwlock,
4760 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4763 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4764 if (!LIST_EMPTY(&owp->nfsow_open)) {
4769 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4770 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4775 *tstp = dp->nfsdl_stateid;
4778 nfscl_cleandeleg(dp);
4779 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4788 * Get a reference on the clientid associated with the mount point.
4789 * Return 1 if success, 0 otherwise.
4792 nfscl_getref(struct nfsmount *nmp)
4794 struct nfsclclient *clp;
4797 clp = nfscl_findcl(nmp);
4802 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
4808 * Release a reference on a clientid acquired with the above call.
4811 nfscl_relref(struct nfsmount *nmp)
4813 struct nfsclclient *clp;
4816 clp = nfscl_findcl(nmp);
4821 nfsv4_relref(&clp->nfsc_lock);
4826 * Save the size attribute in the delegation, since the nfsnode
4830 nfscl_reclaimnode(vnode_t vp)
4832 struct nfsclclient *clp;
4833 struct nfscldeleg *dp;
4834 struct nfsnode *np = VTONFS(vp);
4835 struct nfsmount *nmp;
4837 nmp = VFSTONFS(vp->v_mount);
4838 if (!NFSHASNFSV4(nmp))
4841 clp = nfscl_findcl(nmp);
4846 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4847 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4848 dp->nfsdl_size = np->n_size;
4853 * Get the saved size attribute in the delegation, since it is a
4854 * newly allocated nfsnode.
4857 nfscl_newnode(vnode_t vp)
4859 struct nfsclclient *clp;
4860 struct nfscldeleg *dp;
4861 struct nfsnode *np = VTONFS(vp);
4862 struct nfsmount *nmp;
4864 nmp = VFSTONFS(vp->v_mount);
4865 if (!NFSHASNFSV4(nmp))
4868 clp = nfscl_findcl(nmp);
4873 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4874 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4875 np->n_size = dp->nfsdl_size;
4880 * If there is a valid write delegation for this file, set the modtime
4881 * to the local clock time.
4884 nfscl_delegmodtime(vnode_t vp)
4886 struct nfsclclient *clp;
4887 struct nfscldeleg *dp;
4888 struct nfsnode *np = VTONFS(vp);
4889 struct nfsmount *nmp;
4891 nmp = VFSTONFS(vp->v_mount);
4892 if (!NFSHASNFSV4(nmp))
4895 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4901 clp = nfscl_findcl(nmp);
4906 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4907 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4908 nanotime(&dp->nfsdl_modtime);
4909 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4915 * If there is a valid write delegation for this file with a modtime set,
4916 * put that modtime in mtime.
4919 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4921 struct nfsclclient *clp;
4922 struct nfscldeleg *dp;
4923 struct nfsnode *np = VTONFS(vp);
4924 struct nfsmount *nmp;
4926 nmp = VFSTONFS(vp->v_mount);
4927 if (!NFSHASNFSV4(nmp))
4930 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4936 clp = nfscl_findcl(nmp);
4941 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4943 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4944 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4945 *mtime = dp->nfsdl_modtime;
4950 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
4952 short *defaulterrp, *errp;
4954 if (!nd->nd_repstat)
4956 if (nd->nd_procnum == NFSPROC_NOOP)
4957 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4958 if (nd->nd_repstat == EBADRPC)
4959 return (txdr_unsigned(NFSERR_BADXDR));
4960 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4961 nd->nd_repstat == NFSERR_OPILLEGAL)
4962 return (txdr_unsigned(nd->nd_repstat));
4963 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
4964 minorvers > NFSV4_MINORVERSION) {
4965 /* NFSv4.n error. */
4966 return (txdr_unsigned(nd->nd_repstat));
4968 if (nd->nd_procnum < NFSV4OP_CBNOPS)
4969 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4971 return (txdr_unsigned(nd->nd_repstat));
4973 if (*errp == (short)nd->nd_repstat)
4974 return (txdr_unsigned(nd->nd_repstat));
4975 return (txdr_unsigned(*defaulterrp));
4979 * Called to find/add a layout to a client.
4980 * This function returns the layout with a refcnt (shared lock) upon
4981 * success (returns 0) or with no lock/refcnt on the layout when an
4982 * error is returned.
4983 * If a layout is passed in via lypp, it is locked (exclusively locked).
4986 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4987 nfsv4stateid_t *stateidp, int layouttype, int retonclose,
4988 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
4989 struct ucred *cred, NFSPROC_T *p)
4991 struct nfsclclient *clp;
4992 struct nfscllayout *lyp, *tlyp;
4993 struct nfsclflayout *flp;
4994 struct nfsnode *np = VTONFS(vp);
4996 int layout_passed_in;
4998 mp = nmp->nm_mountp;
4999 layout_passed_in = 1;
5003 layout_passed_in = 0;
5004 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
5011 if (layout_passed_in != 0)
5012 nfsv4_unlock(&lyp->nfsly_lock, 0);
5015 free(tlyp, M_NFSLAYOUT);
5020 * Although no lyp was passed in, another thread might have
5021 * allocated one. If one is found, just increment it's ref
5022 * count and return it.
5024 lyp = nfscl_findlayout(clp, fhp, fhlen);
5028 lyp->nfsly_stateid.seqid = stateidp->seqid;
5029 lyp->nfsly_stateid.other[0] = stateidp->other[0];
5030 lyp->nfsly_stateid.other[1] = stateidp->other[1];
5031 lyp->nfsly_stateid.other[2] = stateidp->other[2];
5032 lyp->nfsly_lastbyte = 0;
5033 LIST_INIT(&lyp->nfsly_flayread);
5034 LIST_INIT(&lyp->nfsly_flayrw);
5035 LIST_INIT(&lyp->nfsly_recall);
5036 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
5037 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
5038 lyp->nfsly_clp = clp;
5039 if (layouttype == NFSLAYOUT_FLEXFILE)
5040 lyp->nfsly_flags = NFSLY_FLEXFILE;
5042 lyp->nfsly_flags = NFSLY_FILES;
5043 if (retonclose != 0)
5044 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
5045 lyp->nfsly_fhlen = fhlen;
5046 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
5047 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5048 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
5050 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5053 if (retonclose != 0)
5054 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
5055 if (stateidp->seqid > lyp->nfsly_stateid.seqid)
5056 lyp->nfsly_stateid.seqid = stateidp->seqid;
5057 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5058 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5059 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5061 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5062 if (NFSCL_FORCEDISM(mp)) {
5065 free(tlyp, M_NFSLAYOUT);
5069 } else if (stateidp->seqid > lyp->nfsly_stateid.seqid)
5070 lyp->nfsly_stateid.seqid = stateidp->seqid;
5072 /* Merge the new list of File Layouts into the list. */
5073 flp = LIST_FIRST(fhlp);
5075 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
5076 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
5078 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
5080 if (layout_passed_in != 0)
5081 nfsv4_unlock(&lyp->nfsly_lock, 1);
5084 free(tlyp, M_NFSLAYOUT);
5089 * Search for a layout by MDS file handle.
5090 * If one is found, it is returned with a refcnt (shared lock) iff
5091 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
5094 struct nfscllayout *
5095 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
5096 uint64_t off, struct nfsclflayout **retflpp, int *recalledp)
5098 struct nfscllayout *lyp;
5100 int error, igotlock;
5102 mp = clp->nfsc_nmp->nm_mountp;
5106 lyp = nfscl_findlayout(clp, fhp, fhlen);
5108 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5109 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5110 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5111 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5112 error = nfscl_findlayoutforio(lyp, off,
5113 NFSV4OPEN_ACCESSREAD, retflpp);
5115 nfsv4_getref(&lyp->nfsly_lock, NULL,
5116 NFSCLSTATEMUTEXPTR, mp);
5119 igotlock = nfsv4_lock(&lyp->nfsly_lock,
5120 1, NULL, NFSCLSTATEMUTEXPTR, mp);
5121 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
5124 if (NFSCL_FORCEDISM(mp)) {
5138 * Search for a layout by MDS file handle. If one is found, mark in to be
5139 * recalled, if it already marked "return on close".
5142 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
5143 int fhlen, struct nfsclrecalllayout **recallpp)
5145 struct nfscllayout *lyp;
5148 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) ||
5149 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5150 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
5152 lyp = nfscl_findlayout(clp, fhp, fhlen);
5153 if (lyp != NULL && (lyp->nfsly_flags & (NFSLY_RETONCLOSE |
5154 NFSLY_RECALL)) == NFSLY_RETONCLOSE) {
5156 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5157 iomode |= NFSLAYOUTIOMODE_READ;
5158 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5159 iomode |= NFSLAYOUTIOMODE_RW;
5160 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5161 0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
5163 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
5169 * Mark the layout to be recalled and with an error.
5170 * Also, disable the dsp from further use.
5173 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
5174 struct nfscllayout *lyp, struct nfsclds *dsp)
5176 struct nfsclrecalllayout *recallp;
5179 printf("DS being disabled, error=%d\n", stat);
5180 /* Set up the return of the layout. */
5181 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
5184 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5185 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5186 iomode |= NFSLAYOUTIOMODE_READ;
5187 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5188 iomode |= NFSLAYOUTIOMODE_RW;
5189 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5190 0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
5191 dp->nfsdi_deviceid, recallp);
5193 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
5196 free(recallp, M_NFSLAYRECALL);
5199 /* And shut the TCP connection down. */
5200 nfscl_cancelreqs(dsp);
5204 * Cancel all RPCs for this "dsp" by closing the connection.
5205 * Also, mark the session as defunct.
5206 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
5207 * cannot be shut down.
5210 nfscl_cancelreqs(struct nfsclds *dsp)
5212 struct __rpc_client *cl;
5213 static int non_event;
5216 if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
5217 dsp->nfsclds_sockp != NULL &&
5218 dsp->nfsclds_sockp->nr_client != NULL) {
5219 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
5220 cl = dsp->nfsclds_sockp->nr_client;
5221 dsp->nfsclds_sess.nfsess_defunct = 1;
5225 * This 1sec sleep is done to reduce the number of reconnect
5226 * attempts made on the DS while it has failed.
5228 tsleep(&non_event, PVFS, "ndscls", hz);
5235 * Dereference a layout.
5238 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
5243 nfsv4_unlock(&lyp->nfsly_lock, 0);
5245 nfsv4_relref(&lyp->nfsly_lock);
5250 * Search for a devinfo by deviceid. If one is found, return it after
5251 * acquiring a reference count on it.
5253 struct nfscldevinfo *
5254 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
5255 struct nfscldevinfo *dip)
5260 dip = nfscl_finddevinfo(clp, deviceid);
5262 dip->nfsdi_refcnt++;
5268 * Dereference a devinfo structure.
5271 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
5274 dip->nfsdi_refcnt--;
5275 if (dip->nfsdi_refcnt == 0)
5276 wakeup(&dip->nfsdi_refcnt);
5280 * Dereference a devinfo structure.
5283 nfscl_reldevinfo(struct nfscldevinfo *dip)
5287 nfscl_reldevinfo_locked(dip);
5292 * Find a layout for this file handle. Return NULL upon failure.
5294 static struct nfscllayout *
5295 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
5297 struct nfscllayout *lyp;
5299 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
5300 if (lyp->nfsly_fhlen == fhlen &&
5301 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
5307 * Find a devinfo for this deviceid. Return NULL upon failure.
5309 static struct nfscldevinfo *
5310 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
5312 struct nfscldevinfo *dip;
5314 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
5315 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5322 * Merge the new file layout list into the main one, maintaining it in
5323 * increasing offset order.
5326 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5327 struct nfsclflayouthead *newfhlp)
5329 struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5331 flp = LIST_FIRST(fhlp);
5333 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5334 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5336 flp = LIST_NEXT(flp, nfsfl_list);
5338 if (prevflp == NULL)
5339 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5341 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5347 * Add this nfscldevinfo to the client, if it doesn't already exist.
5348 * This function consumes the structure pointed at by dip, if not NULL.
5351 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
5352 struct nfsclflayout *flp)
5354 struct nfsclclient *clp;
5355 struct nfscldevinfo *tdip;
5363 free(dip, M_NFSDEVINFO);
5366 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5367 dev = flp->nfsfl_dev;
5369 dev = flp->nfsfl_ffm[ind].dev;
5370 tdip = nfscl_finddevinfo(clp, dev);
5372 tdip->nfsdi_layoutrefs++;
5373 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5374 flp->nfsfl_devp = tdip;
5376 flp->nfsfl_ffm[ind].devp = tdip;
5377 nfscl_reldevinfo_locked(tdip);
5380 free(dip, M_NFSDEVINFO);
5384 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5385 dip->nfsdi_layoutrefs = 1;
5386 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5387 flp->nfsfl_devp = dip;
5389 flp->nfsfl_ffm[ind].devp = dip;
5398 * Free up a layout structure and associated file layout structure(s).
5401 nfscl_freelayout(struct nfscllayout *layp)
5403 struct nfsclflayout *flp, *nflp;
5404 struct nfsclrecalllayout *rp, *nrp;
5406 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5407 LIST_REMOVE(flp, nfsfl_list);
5408 nfscl_freeflayout(flp);
5410 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5411 LIST_REMOVE(flp, nfsfl_list);
5412 nfscl_freeflayout(flp);
5414 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5415 LIST_REMOVE(rp, nfsrecly_list);
5416 free(rp, M_NFSLAYRECALL);
5419 free(layp, M_NFSLAYOUT);
5423 * Free up a file layout structure.
5426 nfscl_freeflayout(struct nfsclflayout *flp)
5430 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) {
5431 for (i = 0; i < flp->nfsfl_fhcnt; i++)
5432 free(flp->nfsfl_fh[i], M_NFSFH);
5433 if (flp->nfsfl_devp != NULL)
5434 flp->nfsfl_devp->nfsdi_layoutrefs--;
5436 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
5437 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) {
5438 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
5439 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
5440 if (flp->nfsfl_ffm[i].devp != NULL)
5441 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--;
5443 free(flp, M_NFSFLAYOUT);
5447 * Free up a file layout devinfo structure.
5450 nfscl_freedevinfo(struct nfscldevinfo *dip)
5453 free(dip, M_NFSDEVINFO);
5457 * Mark any layouts that match as recalled.
5460 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5461 uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op,
5462 char *devid, struct nfsclrecalllayout *recallp)
5464 struct nfsclrecalllayout *rp, *orp;
5466 recallp->nfsrecly_recalltype = recalltype;
5467 recallp->nfsrecly_iomode = iomode;
5468 recallp->nfsrecly_stateseqid = stateseqid;
5469 recallp->nfsrecly_off = off;
5470 recallp->nfsrecly_len = len;
5471 recallp->nfsrecly_stat = stat;
5472 recallp->nfsrecly_op = op;
5474 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID);
5476 * Order the list as file returns first, followed by fsid and any
5477 * returns, both in increasing stateseqid order.
5478 * Note that the seqids wrap around, so 1 is after 0xffffffff.
5479 * (I'm not sure this is correct because I find RFC5661 confusing
5480 * on this, but hopefully it will work ok.)
5483 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5485 if ((recalltype == NFSLAYOUTRETURN_FILE &&
5486 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5487 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5488 (recalltype != NFSLAYOUTRETURN_FILE &&
5489 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5490 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5491 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5496 * Put any error return on all the file returns that will
5499 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE &&
5500 stat != 0 && rp->nfsrecly_stat == 0) {
5501 rp->nfsrecly_stat = stat;
5502 rp->nfsrecly_op = op;
5504 NFSBCOPY(devid, rp->nfsrecly_devid,
5510 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5513 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5515 lyp->nfsly_flags |= NFSLY_RECALL;
5516 wakeup(lyp->nfsly_clp);
5521 * Compare the two seqids for ordering. The trick is that the seqids can
5522 * wrap around from 0xffffffff->0, so check for the cases where one
5523 * has wrapped around.
5524 * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5527 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5530 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5531 /* seqid2 has wrapped around. */
5533 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5534 /* seqid1 has wrapped around. */
5536 if (seqid1 <= seqid2)
5542 * Do a layout return for each of the recalls.
5545 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5546 struct ucred *cred, NFSPROC_T *p)
5548 struct nfsclrecalllayout *rp;
5549 nfsv4stateid_t stateid;
5552 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5553 stateid.seqid = lyp->nfsly_stateid.seqid;
5554 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5555 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5557 layouttype = NFSLAYOUT_FLEXFILE;
5558 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5559 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5560 lyp->nfsly_fhlen, 0, layouttype,
5561 rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5562 rp->nfsrecly_off, rp->nfsrecly_len,
5563 &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op,
5564 rp->nfsrecly_devid);
5569 * Do the layout commit for a file layout.
5572 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5573 struct ucred *cred, NFSPROC_T *p)
5575 struct nfsclflayout *flp;
5577 int error, layouttype;
5579 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5580 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5582 layouttype = NFSLAYOUT_FLEXFILE;
5583 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5584 if (layouttype == NFSLAYOUT_FLEXFILE &&
5585 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
5586 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
5587 /* If not supported, don't bother doing it. */
5589 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5592 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5593 len = flp->nfsfl_end - flp->nfsfl_off;
5594 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5595 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5596 lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5597 layouttype, cred, p, NULL);
5598 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5599 if (error == NFSERR_NOTSUPP) {
5600 /* If not supported, don't bother doing it. */
5602 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5611 * Commit all layouts for a file (vnode).
5614 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5616 struct nfsclclient *clp;
5617 struct nfscllayout *lyp;
5618 struct nfsnode *np = VTONFS(vp);
5620 struct nfsmount *nmp;
5624 if (NFSHASNOLAYOUTCOMMIT(nmp))
5632 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5637 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5638 if (NFSCL_FORCEDISM(mp)) {
5643 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5644 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5646 NFSCL_DEBUG(4, "do layoutcommit2\n");
5647 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5651 nfsv4_relref(&lyp->nfsly_lock);