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_unlinkopen(struct nfsclopen *);
111 static void nfscl_cleanclient(struct nfsclclient *);
112 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
113 struct ucred *, NFSPROC_T *);
114 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
115 struct nfsmount *, struct ucred *, NFSPROC_T *);
116 static void nfscl_recover(struct nfsclclient *, bool *, struct ucred *,
118 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
119 struct nfscllock *, int);
120 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
121 struct nfscllock **, int);
122 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *,
123 struct nfscldeleghead *);
124 static u_int32_t nfscl_nextcbident(void);
125 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
126 static struct nfsclclient *nfscl_getclnt(u_int32_t);
127 static struct nfsclclient *nfscl_getclntsess(uint8_t *);
128 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
130 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
131 int, struct nfsclrecalllayout **, struct nfscllayout **);
132 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
133 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
135 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
136 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
137 u_int8_t *, struct nfscllock **);
138 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
139 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
140 struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
141 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
142 struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
143 struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *);
144 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
145 struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
146 struct nfscldeleg *, struct ucred *, NFSPROC_T *);
147 static void nfscl_totalrecall(struct nfsclclient *);
148 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
149 struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
150 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
151 u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
152 struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
153 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
154 int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
155 struct ucred *, NFSPROC_T *);
156 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
157 struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
158 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *,
160 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
161 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
162 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
163 struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int,
165 static void nfscl_freeopenowner(struct nfsclowner *, int);
166 static void nfscl_cleandeleg(struct nfscldeleg *);
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, bool firstref)
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, firstref, &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], lockown[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 (vp->v_type != VREG)
549 nmp = VFSTONFS(vp->v_mount);
552 * For "oneopenown" mounts, first check for a cached open in the
553 * NFS vnode, that can be used as a stateid. This can only be
554 * done if no delegations have been issued to the mount and no
555 * byte range file locking has been done for the file.
557 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && fords == 0) {
560 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
561 (np->n_flag & NMIGHTBELOCKED) == 0 &&
562 np->n_openstateid != NULL) {
565 np->n_openstateid->nfso_stateid.other[0];
567 np->n_openstateid->nfso_stateid.other[1];
569 np->n_openstateid->nfso_stateid.other[2];
579 clp = nfscl_findcl(nmp);
586 * Wait for recovery to complete.
588 while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
589 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
590 PZERO, "nfsrecvr", NULL);
593 * First, look for a delegation.
595 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
596 if (dp->nfsdl_fhlen == fhlen &&
597 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
598 if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
599 (dp->nfsdl_flags & NFSCLDL_WRITE)) {
600 if (NFSHASNFSV4N(nmp))
604 dp->nfsdl_stateid.seqid;
605 stateidp->other[0] = dp->nfsdl_stateid.other[0];
606 stateidp->other[1] = dp->nfsdl_stateid.other[1];
607 stateidp->other[2] = dp->nfsdl_stateid.other[2];
608 if (!(np->n_flag & NDELEGRECALL)) {
609 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
611 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
613 dp->nfsdl_timestamp = NFSD_MONOSEC +
615 dp->nfsdl_rwlock.nfslock_usecnt++;
616 *lckpp = (void *)&dp->nfsdl_rwlock;
627 * If p != NULL, we want to search the parentage tree
628 * for a matching OpenOwner and use that.
630 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
631 nfscl_filllockowner(NULL, own, F_POSIX);
633 nfscl_filllockowner(p->td_proc, own, F_POSIX);
634 nfscl_filllockowner(p->td_proc, lockown, F_POSIX);
636 error = nfscl_getopen(NULL, clp->nfsc_openhash, nfhp, fhlen,
637 own, lockown, mode, &lp, &op);
638 if (error == 0 && lp != NULL && fords == 0) {
639 /* Don't return a lock stateid for a DS. */
640 if (NFSHASNFSV4N(nmp))
643 stateidp->seqid = lp->nfsl_stateid.seqid;
645 lp->nfsl_stateid.other[0];
647 lp->nfsl_stateid.other[1];
649 lp->nfsl_stateid.other[2];
655 /* If not found, just look for any OpenOwner that will work. */
658 oph = NFSCLOPENHASH(clp, nfhp, fhlen);
659 LIST_FOREACH(op, oph, nfso_hash) {
660 if (op->nfso_fhlen == fhlen &&
661 !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
662 if (top == NULL && (op->nfso_mode &
663 NFSV4OPEN_ACCESSWRITE) != 0 &&
664 (mode & NFSV4OPEN_ACCESSREAD) != 0)
666 if ((mode & op->nfso_mode) == mode) {
667 /* LRU order the hash list. */
668 LIST_REMOVE(op, nfso_hash);
669 LIST_INSERT_HEAD(oph, op, nfso_hash);
676 NFSCL_DEBUG(2, "openmode top=%p\n", top);
677 if (top == NULL || NFSHASOPENMODE(nmp)) {
684 * For read aheads or write behinds, use the open cred.
685 * A read ahead or write behind is indicated by p == NULL.
688 newnfs_copycred(&op->nfso_cred, cred);
692 * No lock stateid, so return the open stateid.
694 if (NFSHASNFSV4N(nmp))
697 stateidp->seqid = op->nfso_stateid.seqid;
698 stateidp->other[0] = op->nfso_stateid.other[0];
699 stateidp->other[1] = op->nfso_stateid.other[1];
700 stateidp->other[2] = op->nfso_stateid.other[2];
706 * Search for a matching file, mode and, optionally, lockowner.
709 nfscl_getopen(struct nfsclownerhead *ohp, struct nfsclopenhash *ohashp,
710 u_int8_t *nfhp, int fhlen, u_int8_t *openown, u_int8_t *lockown,
711 u_int32_t mode, struct nfscllockowner **lpp, struct nfsclopen **opp)
713 struct nfsclowner *owp;
714 struct nfsclopen *op, *rop, *rop2;
715 struct nfsclopenhash *oph;
718 KASSERT(ohp == NULL || ohashp == NULL, ("nfscl_getopen: "
719 "only one of ohp and ohashp can be set"));
723 * rop will be set to the open to be returned. There are three
724 * variants of this, all for an open of the correct file:
725 * 1 - A match of lockown.
726 * 2 - A match of the openown, when no lockown match exists.
727 * 3 - A match for any open, if no openown or lockown match exists.
728 * Looking for #2 over #3 probably isn't necessary, but since
729 * RFC3530 is vague w.r.t. the relationship between openowners and
730 * lockowners, I think this is the safer way to go.
735 /* Search the client list */
736 if (ohashp == NULL) {
737 /* Search the local opens on the delegation. */
738 LIST_FOREACH(owp, ohp, nfsow_list) {
739 /* and look for the correct open */
740 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
741 if (op->nfso_fhlen == fhlen &&
742 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
743 && (op->nfso_mode & mode) == mode)
744 keep_looping = nfscl_checkown(owp, op, openown,
745 lockown, lpp, &rop, &rop2);
753 /* Search for matching opens on the hash list. */
754 oph = &ohashp[NFSCLOPENHASHFUNC(nfhp, fhlen)];
755 LIST_FOREACH(op, oph, nfso_hash) {
756 if (op->nfso_fhlen == fhlen &&
757 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
758 && (op->nfso_mode & mode) == mode)
759 keep_looping = nfscl_checkown(op->nfso_own, op,
760 openown, lockown, lpp, &rop, &rop2);
762 /* LRU order the hash list. */
763 LIST_REMOVE(op, nfso_hash);
764 LIST_INSERT_HEAD(oph, op, nfso_hash);
777 /* Check for an owner match. */
779 nfscl_checkown(struct nfsclowner *owp, struct nfsclopen *op, uint8_t *openown,
780 uint8_t *lockown, struct nfscllockowner **lpp, struct nfsclopen **ropp,
781 struct nfsclopen **ropp2)
783 struct nfscllockowner *lp;
788 /* Now look for a matching lockowner. */
789 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
790 if (!NFSBCMP(lp->nfsl_owner, lockown,
791 NFSV4CL_LOCKNAMELEN)) {
798 if (*ropp == NULL && !NFSBCMP(owp->nfsow_owner, openown,
799 NFSV4CL_LOCKNAMELEN)) {
802 keep_looping = false;
806 return (keep_looping);
810 * Release use of an open owner. Called when open operations are done
811 * with the open owner.
814 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
815 __unused int error, __unused int candelete, int unlocked)
822 if (NFSHASONEOPENOWN(nmp))
823 nfsv4_relref(&owp->nfsow_rwlock);
825 nfscl_lockunlock(&owp->nfsow_rwlock);
827 nfscl_clrelease(owp->nfsow_clp);
832 * Release use of an open structure under an open owner.
835 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
838 struct nfsclclient *clp;
839 struct nfsclowner *owp;
845 if (NFSHASONEOPENOWN(nmp))
846 nfsv4_relref(&owp->nfsow_rwlock);
848 nfscl_lockunlock(&owp->nfsow_rwlock);
849 clp = owp->nfsow_clp;
850 if (error && candelete && op->nfso_opencnt == 0)
851 nfscl_freeopen(op, 0, true);
852 nfscl_clrelease(clp);
857 * Called to get a clientid structure. It will optionally lock the
858 * client data structures to do the SetClientId/SetClientId_confirm,
859 * but will release that lock and return the clientid with a reference
861 * If the "cred" argument is NULL, a new clientid should not be created.
862 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
864 * It always clpp with a reference count on it, unless returning an error.
867 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
868 bool tryminvers, bool firstref, struct nfsclclient **clpp)
870 struct nfsclclient *clp;
871 struct nfsclclient *newclp = NULL;
872 struct nfsmount *nmp;
873 char uuid[HOSTUUIDLEN];
874 int igotlock = 0, error, trystalecnt, clidinusedelay, i;
879 getcredhostuuid(cred, uuid, sizeof uuid);
880 idlen = strlen(uuid);
882 idlen += sizeof (u_int64_t);
884 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
886 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
891 * If a forced dismount is already in progress, don't
892 * allocate a new clientid and get out now. For the case where
893 * clp != NULL, this is a harmless optimization.
895 if (NFSCL_FORCEDISM(mp)) {
898 free(newclp, M_NFSCLCLIENT);
903 if (newclp == NULL) {
908 clp->nfsc_idlen = idlen;
909 LIST_INIT(&clp->nfsc_owner);
910 TAILQ_INIT(&clp->nfsc_deleg);
911 TAILQ_INIT(&clp->nfsc_layout);
912 LIST_INIT(&clp->nfsc_devinfo);
913 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
914 LIST_INIT(&clp->nfsc_deleghash[i]);
915 for (i = 0; i < NFSCLOPENHASHSIZE; i++)
916 LIST_INIT(&clp->nfsc_openhash[i]);
917 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
918 LIST_INIT(&clp->nfsc_layouthash[i]);
919 clp->nfsc_flags = NFSCLFLAGS_INITED;
920 clp->nfsc_clientidrev = 1;
921 clp->nfsc_cbident = nfscl_nextcbident();
922 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
924 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
929 free(newclp, M_NFSCLCLIENT);
931 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
932 !NFSCL_FORCEDISM(mp))
933 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
934 NFSCLSTATEMUTEXPTR, mp);
937 * Call nfsv4_lock() with "iwantlock == 0" on the firstref so
938 * that it will wait for a pending exclusive lock request.
939 * This gives the exclusive lock request priority over this
940 * shared lock request.
941 * An exclusive lock on nfsc_lock is used mainly for server
942 * crash recoveries and delegation recalls.
945 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR,
947 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
949 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
951 * Both nfsv4_lock() and nfsv4_getref() know to check
952 * for NFSCL_FORCEDISM() and return without sleeping to
953 * wait for the exclusive lock to be released, since it
954 * might be held by nfscl_umount() and we need to get out
955 * now for that case and not wait until nfscl_umount()
964 * If it needs a clientid, do the setclientid now.
966 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
968 panic("nfscl_clget");
969 if (p == NULL || cred == NULL) {
971 nfsv4_unlock(&clp->nfsc_lock, 0);
976 * If RFC3530 Sec. 14.2.33 is taken literally,
977 * NFSERR_CLIDINUSE will be returned persistently for the
978 * case where a new mount of the same file system is using
979 * a different principal. In practice, NFSERR_CLIDINUSE is
980 * only returned when there is outstanding unexpired state
981 * on the clientid. As such, try for twice the lease
982 * interval, if we know what that is. Otherwise, make a
984 * The case of returning NFSERR_STALECLIENTID is far less
985 * likely, but might occur if there is a significant delay
986 * between doing the SetClientID and SetClientIDConfirm Ops,
987 * such that the server throws away the clientid before
988 * receiving the SetClientIDConfirm.
990 if (clp->nfsc_renew > 0)
991 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
993 clidinusedelay = 120;
996 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
997 if (error == NFSERR_STALECLIENTID ||
998 error == NFSERR_STALEDONTRECOVER ||
999 error == NFSERR_BADSESSION ||
1000 error == NFSERR_CLIDINUSE) {
1001 (void) nfs_catnap(PZERO, error, "nfs_setcl");
1002 } else if (error == NFSERR_MINORVERMISMATCH &&
1004 if (nmp->nm_minorvers > 0)
1005 nmp->nm_minorvers--;
1009 } while (((error == NFSERR_STALECLIENTID ||
1010 error == NFSERR_BADSESSION ||
1011 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
1012 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0) ||
1013 (error == NFSERR_MINORVERMISMATCH && tryminvers));
1016 nfsv4_unlock(&clp->nfsc_lock, 0);
1020 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
1024 nfsv4_unlock(&clp->nfsc_lock, 1);
1033 * Get a reference to a clientid and return it, if valid.
1035 struct nfsclclient *
1036 nfscl_findcl(struct nfsmount *nmp)
1038 struct nfsclclient *clp;
1041 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
1047 * Release the clientid structure. It may be locked or reference counted.
1050 nfscl_clrelease(struct nfsclclient *clp)
1053 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
1054 nfsv4_unlock(&clp->nfsc_lock, 0);
1056 nfsv4_relref(&clp->nfsc_lock);
1060 * External call for nfscl_clrelease.
1063 nfscl_clientrelease(struct nfsclclient *clp)
1067 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
1068 nfsv4_unlock(&clp->nfsc_lock, 0);
1070 nfsv4_relref(&clp->nfsc_lock);
1075 * Called when wanting to lock a byte region.
1078 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1079 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
1080 int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
1081 struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
1083 struct nfscllockowner *lp;
1084 struct nfsclopen *op;
1085 struct nfsclclient *clp;
1086 struct nfscllockowner *nlp;
1087 struct nfscllock *nlop, *otherlop;
1088 struct nfscldeleg *dp = NULL, *ldp = NULL;
1089 struct nfscllockownerhead *lhp = NULL;
1091 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
1093 int error = 0, ret, donelocally = 0;
1096 /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
1105 * Might need these, so MALLOC them now, to
1106 * avoid a tsleep() in MALLOC later.
1109 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
1111 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1113 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1114 nlop->nfslo_type = type;
1115 nlop->nfslo_first = off;
1116 if (len == NFS64BITSSET) {
1117 nlop->nfslo_end = NFS64BITSSET;
1119 nlop->nfslo_end = off + len;
1120 if (nlop->nfslo_end <= nlop->nfslo_first)
1121 error = NFSERR_INVAL;
1128 error = nfscl_getcl(vp->v_mount, cred, p, false, true,
1132 free(nlp, M_NFSCLLOCKOWNER);
1133 free(otherlop, M_NFSCLLOCK);
1134 free(nlop, M_NFSCLLOCK);
1141 openownp = ropenownp;
1143 nfscl_filllockowner(id, own, flags);
1145 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
1146 nfscl_filllockowner(NULL, openown, F_POSIX);
1148 nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1154 * First, search for a delegation. If one exists for this file,
1155 * the lock can be done locally against it, so long as there
1156 * isn't a local lock conflict.
1158 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1159 np->n_fhp->nfh_len);
1160 /* Just sanity check for correct type of delegation */
1161 if (dp != NULL && ((dp->nfsdl_flags &
1162 (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1164 (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1168 /* Now, find an open and maybe a lockowner. */
1169 ret = nfscl_getopen(&dp->nfsdl_owner, NULL, np->n_fhp->nfh_fh,
1170 np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1172 ret = nfscl_getopen(NULL, clp->nfsc_openhash,
1173 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1174 ownp, mode, NULL, &op);
1176 lhp = &dp->nfsdl_lock;
1177 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1178 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1179 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1187 * Get the related Open and maybe lockowner.
1189 error = nfscl_getopen(NULL, clp->nfsc_openhash,
1190 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1191 ownp, mode, &lp, &op);
1193 lhp = &op->nfso_lock;
1195 if (!error && !recovery)
1196 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1197 np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1200 nfscl_clrelease(clp);
1203 free(nlp, M_NFSCLLOCKOWNER);
1204 free(otherlop, M_NFSCLLOCK);
1205 free(nlop, M_NFSCLLOCK);
1210 * Ok, see if a lockowner exists and create one, as required.
1213 LIST_FOREACH(lp, lhp, nfsl_list) {
1214 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1218 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1220 NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1221 NFSV4CL_LOCKNAMELEN);
1223 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1224 NFSV4CL_LOCKNAMELEN);
1225 nlp->nfsl_seqid = 0;
1226 nlp->nfsl_lockflags = flags;
1227 nlp->nfsl_inprog = NULL;
1228 nfscl_lockinit(&nlp->nfsl_rwlock);
1229 LIST_INIT(&nlp->nfsl_lock);
1231 nlp->nfsl_open = NULL;
1232 nfsstatsv1.cllocallockowners++;
1234 nlp->nfsl_open = op;
1235 nfsstatsv1.cllockowners++;
1237 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1244 * Now, update the byte ranges for locks.
1246 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1252 nfscl_clrelease(clp);
1255 * Serial modifications on the lock owner for multiple threads
1256 * for the same process using a read/write lock.
1259 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1265 free(nlp, M_NFSCLLOCKOWNER);
1267 free(nlop, M_NFSCLLOCK);
1269 free(otherlop, M_NFSCLLOCK);
1276 * Called to unlock a byte range, for LockU.
1279 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1280 __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1281 struct nfsclclient *clp, void *id, int flags,
1282 struct nfscllockowner **lpp, int *dorpcp)
1284 struct nfscllockowner *lp;
1285 struct nfsclopen *op;
1286 struct nfscllock *nlop, *other_lop = NULL;
1287 struct nfscldeleg *dp;
1289 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1297 * Might need these, so MALLOC them now, to
1298 * avoid a tsleep() in MALLOC later.
1301 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1302 nlop->nfslo_type = F_UNLCK;
1303 nlop->nfslo_first = off;
1304 if (len == NFS64BITSSET) {
1305 nlop->nfslo_end = NFS64BITSSET;
1307 nlop->nfslo_end = off + len;
1308 if (nlop->nfslo_end <= nlop->nfslo_first) {
1309 free(nlop, M_NFSCLLOCK);
1310 return (NFSERR_INVAL);
1315 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1318 nfscl_filllockowner(id, own, flags);
1322 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1323 np->n_fhp->nfh_len);
1326 * First, unlock any local regions on a delegation.
1329 /* Look for this lockowner. */
1330 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1331 if (!NFSBCMP(lp->nfsl_owner, own,
1332 NFSV4CL_LOCKNAMELEN))
1336 /* Use other_lop, so nlop is still available */
1337 (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1341 * Now, find a matching open/lockowner that hasn't already been done,
1342 * as marked by nfsl_inprog.
1346 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1347 np->n_fhp->nfh_len), nfso_hash) {
1348 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1349 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1350 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1351 if (lp->nfsl_inprog == NULL &&
1352 !NFSBCMP(lp->nfsl_owner, own,
1353 NFSV4CL_LOCKNAMELEN)) {
1364 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1368 * Serial modifications on the lock owner for multiple
1369 * threads for the same process using a read/write lock.
1371 lp->nfsl_inprog = p;
1372 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1377 free(nlop, M_NFSCLLOCK);
1379 free(other_lop, M_NFSCLLOCK);
1384 * Release all lockowners marked in progess for this process and file.
1387 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1388 void *id, int flags)
1390 struct nfsclopen *op;
1391 struct nfscllockowner *lp;
1393 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1396 nfscl_filllockowner(id, own, flags);
1398 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1399 np->n_fhp->nfh_len), nfso_hash) {
1400 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1401 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1402 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1403 if (lp->nfsl_inprog == p &&
1404 !NFSBCMP(lp->nfsl_owner, own,
1405 NFSV4CL_LOCKNAMELEN)) {
1406 lp->nfsl_inprog = NULL;
1407 nfscl_lockunlock(&lp->nfsl_rwlock);
1412 nfscl_clrelease(clp);
1417 * Called to find out if any bytes within the byte range specified are
1418 * write locked by the calling process. Used to determine if flushing
1419 * is required before a LockU.
1420 * If in doubt, return 1, so the flush will occur.
1423 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1424 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1426 struct nfscllockowner *lp;
1427 struct nfsclopen *op;
1428 struct nfsclclient *clp;
1429 struct nfscllock *lop;
1430 struct nfscldeleg *dp;
1433 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1437 switch (fl->l_whence) {
1441 * Caller is responsible for adding any necessary offset
1442 * when SEEK_CUR is used.
1447 off = np->n_size + fl->l_start;
1452 if (fl->l_len != 0) {
1453 end = off + fl->l_len;
1460 error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp);
1463 nfscl_filllockowner(id, own, flags);
1467 * First check the delegation locks.
1469 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1471 /* No need to flush if it is a write delegation. */
1472 if ((dp->nfsdl_flags & NFSCLDL_WRITE) != 0) {
1473 nfscl_clrelease(clp);
1477 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1478 if (!NFSBCMP(lp->nfsl_owner, own,
1479 NFSV4CL_LOCKNAMELEN))
1483 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1484 if (lop->nfslo_first >= end)
1486 if (lop->nfslo_end <= off)
1488 if (lop->nfslo_type == F_WRLCK) {
1489 nfscl_clrelease(clp);
1498 * Now, check state against the server.
1500 LIST_FOREACH(op, NFSCLOPENHASH(clp, np->n_fhp->nfh_fh,
1501 np->n_fhp->nfh_len), nfso_hash) {
1502 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1503 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1504 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1505 if (!NFSBCMP(lp->nfsl_owner, own,
1506 NFSV4CL_LOCKNAMELEN))
1510 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1511 if (lop->nfslo_first >= end)
1513 if (lop->nfslo_end <= off)
1515 if (lop->nfslo_type == F_WRLCK) {
1516 nfscl_clrelease(clp);
1524 nfscl_clrelease(clp);
1530 * Release a byte range lock owner structure.
1533 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1535 struct nfsclclient *clp;
1540 clp = lp->nfsl_open->nfso_own->nfsow_clp;
1541 if (error != 0 && candelete &&
1542 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1543 nfscl_freelockowner(lp, 0);
1545 nfscl_lockunlock(&lp->nfsl_rwlock);
1546 nfscl_clrelease(clp);
1551 * Unlink the open structure.
1554 nfscl_unlinkopen(struct nfsclopen *op)
1557 LIST_REMOVE(op, nfso_list);
1558 if (op->nfso_hash.le_prev != NULL)
1559 LIST_REMOVE(op, nfso_hash);
1563 * Free up an open structure and any associated byte range lock structures.
1566 nfscl_freeopen(struct nfsclopen *op, int local, bool unlink)
1570 nfscl_unlinkopen(op);
1571 nfscl_freealllocks(&op->nfso_lock, local);
1572 free(op, M_NFSCLOPEN);
1574 nfsstatsv1.cllocalopens--;
1576 nfsstatsv1.clopens--;
1580 * Free up all lock owners and associated locks.
1583 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1585 struct nfscllockowner *lp, *nlp;
1587 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1588 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1590 nfscl_freelockowner(lp, local);
1595 * Called for an Open when NFSERR_EXPIRED is received from the server.
1596 * If there are no byte range locks nor a Share Deny lost, try to do a
1597 * fresh Open. Otherwise, free the open.
1600 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1601 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1603 struct nfscllockowner *lp;
1604 struct nfscldeleg *dp;
1605 int mustdelete = 0, error;
1608 * Look for any byte range lock(s).
1610 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1611 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1618 * If no byte range lock(s) nor a Share deny, try to re-open.
1620 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1621 newnfs_copycred(&op->nfso_cred, cred);
1623 error = nfsrpc_reopen(nmp, op->nfso_fh,
1624 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1628 free(dp, M_NFSCLDELEG);
1633 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1634 op->nfso_fhlen, cred, p, &dp);
1638 * If a byte range lock or Share deny or couldn't re-open, free it.
1641 nfscl_freeopen(op, 0, true);
1642 return (mustdelete);
1646 * Free up an open owner structure.
1649 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1654 * Make sure the NFSCLSTATE mutex is held, to avoid races with
1655 * calls in nfscl_renewthread() that do not hold a reference
1656 * count on the nfsclclient and just the mutex.
1657 * The mutex will not be held for calls done with the exclusive
1658 * nfsclclient lock held, in particular, nfscl_hasexpired()
1659 * and nfscl_recalldeleg() might do this.
1661 owned = mtx_owned(NFSCLSTATEMUTEXPTR);
1664 LIST_REMOVE(owp, nfsow_list);
1667 free(owp, M_NFSCLOWNER);
1669 nfsstatsv1.cllocalopenowners--;
1671 nfsstatsv1.clopenowners--;
1675 * Free up a byte range lock owner structure.
1678 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1680 struct nfscllock *lop, *nlop;
1684 * Make sure the NFSCLSTATE mutex is held, to avoid races with
1685 * calls in nfscl_renewthread() that do not hold a reference
1686 * count on the nfsclclient and just the mutex.
1687 * The mutex will not be held for calls done with the exclusive
1688 * nfsclclient lock held, in particular, nfscl_hasexpired()
1689 * and nfscl_recalldeleg() might do this.
1691 owned = mtx_owned(NFSCLSTATEMUTEXPTR);
1694 LIST_REMOVE(lp, nfsl_list);
1697 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1698 nfscl_freelock(lop, local);
1700 free(lp, M_NFSCLLOCKOWNER);
1702 nfsstatsv1.cllocallockowners--;
1704 nfsstatsv1.cllockowners--;
1708 * Free up a byte range lock structure.
1711 nfscl_freelock(struct nfscllock *lop, int local)
1714 LIST_REMOVE(lop, nfslo_list);
1715 free(lop, M_NFSCLLOCK);
1717 nfsstatsv1.cllocallocks--;
1719 nfsstatsv1.cllocks--;
1723 * Clean out the state related to a delegation.
1726 nfscl_cleandeleg(struct nfscldeleg *dp)
1728 struct nfsclowner *owp, *nowp;
1729 struct nfsclopen *op;
1731 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1732 op = LIST_FIRST(&owp->nfsow_open);
1734 if (LIST_NEXT(op, nfso_list) != NULL)
1735 panic("nfscleandel");
1736 nfscl_freeopen(op, 1, true);
1738 nfscl_freeopenowner(owp, 1);
1740 nfscl_freealllocks(&dp->nfsdl_lock, 1);
1744 * Free a delegation.
1747 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp, bool freeit)
1750 TAILQ_REMOVE(hdp, dp, nfsdl_list);
1751 LIST_REMOVE(dp, nfsdl_hash);
1753 free(dp, M_NFSCLDELEG);
1754 nfsstatsv1.cldelegates--;
1759 * Free up all state related to this client structure.
1762 nfscl_cleanclient(struct nfsclclient *clp)
1764 struct nfsclowner *owp, *nowp;
1765 struct nfsclopen *op, *nop;
1766 struct nfscllayout *lyp, *nlyp;
1767 struct nfscldevinfo *dip, *ndip;
1769 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1770 nfscl_freelayout(lyp);
1772 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
1773 nfscl_freedevinfo(dip);
1775 /* Now, all the OpenOwners, etc. */
1776 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1777 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1778 nfscl_freeopen(op, 0, true);
1780 nfscl_freeopenowner(owp, 0);
1785 * Called when an NFSERR_EXPIRED is received from the server.
1788 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1789 struct ucred *cred, NFSPROC_T *p)
1791 struct nfsclowner *owp, *nowp, *towp;
1792 struct nfsclopen *op, *nop, *top;
1793 struct nfscldeleg *dp, *ndp;
1794 int ret, printed = 0;
1797 * First, merge locally issued Opens into the list for the server.
1799 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1800 while (dp != NULL) {
1801 ndp = TAILQ_NEXT(dp, nfsdl_list);
1802 owp = LIST_FIRST(&dp->nfsdl_owner);
1803 while (owp != NULL) {
1804 nowp = LIST_NEXT(owp, nfsow_list);
1805 op = LIST_FIRST(&owp->nfsow_open);
1807 if (LIST_NEXT(op, nfso_list) != NULL)
1809 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1810 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1811 NFSV4CL_LOCKNAMELEN))
1815 /* Merge opens in */
1816 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1817 if (top->nfso_fhlen == op->nfso_fhlen &&
1818 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1820 top->nfso_mode |= op->nfso_mode;
1821 top->nfso_opencnt += op->nfso_opencnt;
1826 /* Just add the open to the owner list */
1827 LIST_REMOVE(op, nfso_list);
1828 op->nfso_own = towp;
1829 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1830 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh,
1831 op->nfso_fhlen), op, nfso_hash);
1832 nfsstatsv1.cllocalopens--;
1833 nfsstatsv1.clopens++;
1836 /* Just add the openowner to the client list */
1837 LIST_REMOVE(owp, nfsow_list);
1838 owp->nfsow_clp = clp;
1839 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1840 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh,
1841 op->nfso_fhlen), op, nfso_hash);
1842 nfsstatsv1.cllocalopenowners--;
1843 nfsstatsv1.clopenowners++;
1844 nfsstatsv1.cllocalopens--;
1845 nfsstatsv1.clopens++;
1850 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1852 printf("nfsv4 expired locks lost\n");
1854 nfscl_cleandeleg(dp);
1855 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
1858 if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1862 * Now, try and reopen against the server.
1864 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1865 owp->nfsow_seqid = 0;
1866 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1867 ret = nfscl_expireopen(clp, op, nmp, cred, p);
1868 if (ret && !printed) {
1870 printf("nfsv4 expired locks lost\n");
1873 if (LIST_EMPTY(&owp->nfsow_open))
1874 nfscl_freeopenowner(owp, 0);
1879 * This function must be called after the process represented by "own" has
1880 * exited. Must be called with CLSTATE lock held.
1883 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1885 struct nfsclowner *owp, *nowp;
1886 struct nfscllockowner *lp;
1887 struct nfscldeleg *dp;
1889 /* First, get rid of local locks on delegations. */
1890 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1891 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1892 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1893 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1895 nfscl_freelockowner(lp, 1);
1900 owp = LIST_FIRST(&clp->nfsc_owner);
1901 while (owp != NULL) {
1902 nowp = LIST_NEXT(owp, nfsow_list);
1903 if (!NFSBCMP(owp->nfsow_owner, own,
1904 NFSV4CL_LOCKNAMELEN)) {
1906 * If there are children that haven't closed the
1907 * file descriptors yet, the opens will still be
1908 * here. For that case, let the renew thread clear
1909 * out the OpenOwner later.
1911 if (LIST_EMPTY(&owp->nfsow_open))
1912 nfscl_freeopenowner(owp, 0);
1914 owp->nfsow_defunct = 1;
1922 * Find open/lock owners for processes that have exited.
1925 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1927 struct nfsclowner *owp, *nowp;
1928 struct nfsclopen *op;
1929 struct nfscllockowner *lp, *nlp;
1930 struct nfscldeleg *dp;
1931 uint8_t own[NFSV4CL_LOCKNAMELEN];
1934 * All the pidhash locks must be acquired, since they are sx locks
1935 * and must be acquired before the mutexes. The pid(s) that will
1936 * be used aren't known yet, so all the locks need to be acquired.
1937 * Fortunately, this function is only performed once/sec.
1941 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1942 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1943 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1944 if (LIST_EMPTY(&lp->nfsl_lock))
1945 nfscl_emptylockowner(lp, lhp);
1948 if (nfscl_procdoesntexist(owp->nfsow_owner)) {
1949 memcpy(own, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
1950 nfscl_cleanup_common(clp, own);
1955 * For the single open_owner case, these lock owners need to be
1956 * checked to see if they still exist separately.
1957 * This is because nfscl_procdoesntexist() never returns true for
1958 * the single open_owner so that the above doesn't ever call
1959 * nfscl_cleanup_common().
1961 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1962 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1963 if (nfscl_procdoesntexist(lp->nfsl_owner)) {
1964 memcpy(own, lp->nfsl_owner,
1965 NFSV4CL_LOCKNAMELEN);
1966 nfscl_cleanup_common(clp, own);
1971 pidhash_sunlockall();
1975 * Take the empty lock owner and move it to the local lhp list if the
1976 * associated process no longer exists.
1979 nfscl_emptylockowner(struct nfscllockowner *lp,
1980 struct nfscllockownerfhhead *lhp)
1982 struct nfscllockownerfh *lfhp, *mylfhp;
1983 struct nfscllockowner *nlp;
1986 /* If not a Posix lock owner, just return. */
1987 if ((lp->nfsl_lockflags & F_POSIX) == 0)
1993 * First, search to see if this lock owner is already in the list.
1994 * If it is, then the associated process no longer exists.
1996 SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1997 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1998 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
2001 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
2002 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
2003 NFSV4CL_LOCKNAMELEN))
2006 /* If not found, check if process still exists. */
2007 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
2010 /* Move the lock owner over to the local list. */
2011 if (mylfhp == NULL) {
2012 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
2016 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
2017 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
2018 mylfhp->nfslfh_len);
2019 LIST_INIT(&mylfhp->nfslfh_lock);
2020 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
2022 LIST_REMOVE(lp, nfsl_list);
2023 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
2026 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */
2028 * Called from nfs umount to free up the clientid.
2031 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p, struct nfscldeleghead *dhp)
2033 struct nfsclclient *clp;
2038 * For the case that matters, this is the thread that set
2039 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
2040 * done to ensure that any thread executing nfscl_getcl() after
2041 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
2042 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
2043 * explanation, courtesy of Alan Cox.
2044 * What follows is a snippet from Alan Cox's email at:
2045 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
2047 * 1. Set MNTK_UNMOUNTF
2048 * 2. Acquire a standard FreeBSD mutex "m".
2049 * 3. Update some data structures.
2050 * 4. Release mutex "m".
2052 * Then, other threads that acquire "m" after step 4 has occurred will
2053 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to
2054 * step 2 may or may not see MNTK_UNMOUNTF as set.
2057 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
2065 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
2066 panic("nfscl umount");
2069 * First, handshake with the nfscl renew thread, to terminate
2072 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
2073 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
2074 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
2078 * Now, get the exclusive lock on the client state, so
2079 * that no uses of the state are still in progress.
2082 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2083 NFSCLSTATEMUTEXPTR, NULL);
2084 } while (!igotlock);
2088 * Free up all the state. It will expire on the server, but
2089 * maybe we should do a SetClientId/SetClientIdConfirm so
2090 * the server throws it away?
2092 LIST_REMOVE(clp, nfsc_list);
2093 nfscl_delegreturnall(clp, p, dhp);
2094 cred = newnfs_getcred();
2095 if (NFSHASNFSV4N(nmp)) {
2096 nfsrpc_destroysession(nmp, NULL, cred, p);
2097 nfsrpc_destroyclient(nmp, clp, cred, p);
2099 nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2100 nfscl_cleanclient(clp);
2103 free(clp, M_NFSCLCLIENT);
2109 * This function is called when a server replies with NFSERR_STALECLIENTID
2110 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
2111 * doing Opens and Locks with reclaim. If these fail, it deletes the
2112 * corresponding state.
2115 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred,
2118 struct nfsclowner *owp, *nowp;
2119 struct nfsclopen *op, *nop;
2120 struct nfscllockowner *lp, *nlp;
2121 struct nfscllock *lop, *nlop;
2122 struct nfscldeleg *dp, *ndp, *tdp;
2123 struct nfsmount *nmp;
2124 struct ucred *tcred;
2125 struct nfsclopenhead extra_open;
2126 struct nfscldeleghead extra_deleg;
2129 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
2130 int i, igotlock = 0, error, trycnt, firstlock;
2131 struct nfscllayout *lyp, *nlyp;
2135 * First, lock the client structure, so everyone else will
2136 * block when trying to use state.
2139 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2141 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2142 NFSCLSTATEMUTEXPTR, NULL);
2143 } while (!igotlock);
2146 nmp = clp->nfsc_nmp;
2148 panic("nfscl recover");
2151 * For now, just get rid of all layouts. There may be a need
2152 * to do LayoutCommit Ops with reclaim == true later.
2154 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
2155 nfscl_freelayout(lyp);
2156 TAILQ_INIT(&clp->nfsc_layout);
2157 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
2158 LIST_INIT(&clp->nfsc_layouthash[i]);
2163 error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p);
2164 } while ((error == NFSERR_STALECLIENTID ||
2165 error == NFSERR_BADSESSION ||
2166 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2169 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
2170 NFSCLFLAGS_RECVRINPROG);
2171 wakeup(&clp->nfsc_flags);
2172 nfsv4_unlock(&clp->nfsc_lock, 0);
2176 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2177 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2180 * Mark requests already queued on the server, so that they don't
2181 * initiate another recovery cycle. Any requests already in the
2182 * queue that handle state information will have the old stale
2183 * clientid/stateid and will get a NFSERR_STALESTATEID,
2184 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2185 * This will be translated to NFSERR_STALEDONTRECOVER when
2186 * R_DONTRECOVER is set.
2189 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2190 if (rep->r_nmp == nmp)
2191 rep->r_flags |= R_DONTRECOVER;
2196 * If nfsrpc_setclient() returns *retokp == true,
2197 * no more recovery is needed.
2203 * Now, mark all delegations "need reclaim".
2205 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2206 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2208 TAILQ_INIT(&extra_deleg);
2209 LIST_INIT(&extra_open);
2211 * Now traverse the state lists, doing Open and Lock Reclaims.
2213 tcred = newnfs_getcred();
2214 recovered_one = false;
2215 owp = LIST_FIRST(&clp->nfsc_owner);
2216 while (owp != NULL) {
2217 nowp = LIST_NEXT(owp, nfsow_list);
2218 owp->nfsow_seqid = 0;
2219 op = LIST_FIRST(&owp->nfsow_open);
2220 while (op != NULL) {
2221 nop = LIST_NEXT(op, nfso_list);
2222 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2223 /* Search for a delegation to reclaim with the open */
2224 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2225 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2227 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2228 mode = NFSV4OPEN_ACCESSWRITE;
2229 delegtype = NFSV4OPEN_DELEGATEWRITE;
2231 mode = NFSV4OPEN_ACCESSREAD;
2232 delegtype = NFSV4OPEN_DELEGATEREAD;
2234 if ((op->nfso_mode & mode) == mode &&
2235 op->nfso_fhlen == dp->nfsdl_fhlen &&
2236 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2241 delegtype = NFSV4OPEN_DELEGATENONE;
2242 newnfs_copycred(&op->nfso_cred, tcred);
2243 error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2244 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2245 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2248 recovered_one = true;
2249 /* Handle any replied delegation */
2250 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2251 || NFSMNT_RDONLY(nmp->nm_mountp))) {
2252 if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2253 mode = NFSV4OPEN_ACCESSWRITE;
2255 mode = NFSV4OPEN_ACCESSREAD;
2256 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2257 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2259 if ((op->nfso_mode & mode) == mode &&
2260 op->nfso_fhlen == dp->nfsdl_fhlen &&
2261 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2263 dp->nfsdl_stateid = ndp->nfsdl_stateid;
2264 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2265 dp->nfsdl_ace = ndp->nfsdl_ace;
2266 dp->nfsdl_change = ndp->nfsdl_change;
2267 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2268 if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2269 dp->nfsdl_flags |= NFSCLDL_RECALL;
2270 free(ndp, M_NFSCLDELEG);
2277 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2279 /* and reclaim all byte range locks */
2280 lp = LIST_FIRST(&op->nfso_lock);
2281 while (lp != NULL) {
2282 nlp = LIST_NEXT(lp, nfsl_list);
2285 lop = LIST_FIRST(&lp->nfsl_lock);
2286 while (lop != NULL) {
2287 nlop = LIST_NEXT(lop, nfslo_list);
2288 if (lop->nfslo_end == NFS64BITSSET)
2291 len = lop->nfslo_end - lop->nfslo_first;
2292 error = nfscl_trylock(nmp, NULL,
2293 op->nfso_fh, op->nfso_fhlen, lp,
2294 firstlock, 1, lop->nfslo_first, len,
2295 lop->nfslo_type, tcred, p);
2297 nfscl_freelock(lop, 0);
2302 /* If no locks, but a lockowner, just delete it. */
2303 if (LIST_EMPTY(&lp->nfsl_lock))
2304 nfscl_freelockowner(lp, 0);
2307 } else if (error == NFSERR_NOGRACE && !recovered_one &&
2308 NFSHASNFSV4N(nmp)) {
2310 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
2311 * actually end up here, since the client will do
2312 * a recovery for NFSERR_BADSESSION, but will get
2313 * an NFSERR_NOGRACE reply for the first "reclaim"
2315 * So, call nfscl_expireclient() to recover the
2316 * opens as best we can and then do a reclaim
2317 * complete and return.
2319 nfsrpc_reclaimcomplete(nmp, cred, p);
2320 nfscl_expireclient(clp, nmp, tcred, p);
2324 if (error != 0 && error != NFSERR_BADSESSION)
2325 nfscl_freeopen(op, 0, true);
2332 * Now, try and get any delegations not yet reclaimed by cobbling
2333 * to-gether an appropriate open.
2336 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2337 while (dp != NULL) {
2338 ndp = TAILQ_NEXT(dp, nfsdl_list);
2339 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2342 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2344 * Name must be as long an largest possible
2345 * NFSV4CL_LOCKNAMELEN. 12 for now.
2347 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2348 NFSV4CL_LOCKNAMELEN);
2349 LIST_INIT(&nowp->nfsow_open);
2350 nowp->nfsow_clp = clp;
2351 nowp->nfsow_seqid = 0;
2352 nowp->nfsow_defunct = 0;
2353 nfscl_lockinit(&nowp->nfsow_rwlock);
2356 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2357 nop = malloc(sizeof (struct nfsclopen) +
2358 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2359 nop->nfso_own = nowp;
2360 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2361 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2362 delegtype = NFSV4OPEN_DELEGATEWRITE;
2364 nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2365 delegtype = NFSV4OPEN_DELEGATEREAD;
2367 nop->nfso_opencnt = 0;
2368 nop->nfso_posixlock = 1;
2369 nop->nfso_fhlen = dp->nfsdl_fhlen;
2370 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2371 LIST_INIT(&nop->nfso_lock);
2372 nop->nfso_stateid.seqid = 0;
2373 nop->nfso_stateid.other[0] = 0;
2374 nop->nfso_stateid.other[1] = 0;
2375 nop->nfso_stateid.other[2] = 0;
2376 newnfs_copycred(&dp->nfsdl_cred, tcred);
2377 newnfs_copyincred(tcred, &nop->nfso_cred);
2379 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2380 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2381 nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2382 delegtype, tcred, p);
2384 if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2385 mode = NFSV4OPEN_ACCESSWRITE;
2387 mode = NFSV4OPEN_ACCESSREAD;
2388 if ((nop->nfso_mode & mode) == mode &&
2389 nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2390 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2392 dp->nfsdl_stateid = tdp->nfsdl_stateid;
2393 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2394 dp->nfsdl_ace = tdp->nfsdl_ace;
2395 dp->nfsdl_change = tdp->nfsdl_change;
2396 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2397 if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2398 dp->nfsdl_flags |= NFSCLDL_RECALL;
2399 free(tdp, M_NFSCLDELEG);
2401 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2407 free(nop, M_NFSCLOPEN);
2408 if (error == NFSERR_NOGRACE && !recovered_one &&
2409 NFSHASNFSV4N(nmp)) {
2411 * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
2412 * actually end up here, since the client will do
2413 * a recovery for NFSERR_BADSESSION, but will get
2414 * an NFSERR_NOGRACE reply for the first "reclaim"
2416 * So, call nfscl_expireclient() to recover the
2417 * opens as best we can and then do a reclaim
2418 * complete and return.
2420 nfsrpc_reclaimcomplete(nmp, cred, p);
2421 nfscl_expireclient(clp, nmp, tcred, p);
2422 free(nowp, M_NFSCLOWNER);
2426 * Couldn't reclaim it, so throw the state
2429 nfscl_cleandeleg(dp);
2430 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
2432 recovered_one = true;
2433 LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2440 * Now, get rid of extra Opens and Delegations.
2442 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2444 newnfs_copycred(&op->nfso_cred, tcred);
2445 error = nfscl_tryclose(op, tcred, nmp, p, true);
2446 if (error == NFSERR_GRACE)
2447 (void) nfs_catnap(PZERO, error, "nfsexcls");
2448 } while (error == NFSERR_GRACE);
2449 LIST_REMOVE(op, nfso_list);
2450 free(op, M_NFSCLOPEN);
2453 free(nowp, M_NFSCLOWNER);
2455 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2457 newnfs_copycred(&dp->nfsdl_cred, tcred);
2458 error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2459 if (error == NFSERR_GRACE)
2460 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2461 } while (error == NFSERR_GRACE);
2462 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2463 free(dp, M_NFSCLDELEG);
2466 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2467 if (NFSHASNFSV4N(nmp))
2468 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
2472 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2473 wakeup(&clp->nfsc_flags);
2474 nfsv4_unlock(&clp->nfsc_lock, 0);
2481 * This function is called when a server replies with NFSERR_EXPIRED.
2482 * It deletes all state for the client and does a fresh SetClientId/confirm.
2483 * XXX Someday it should post a signal to the process(es) that hold the
2484 * state, so they know that lock state has been lost.
2487 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2489 struct nfsmount *nmp;
2491 int igotlock = 0, error, trycnt;
2494 * If the clientid has gone away or a new SetClientid has already
2495 * been done, just return ok.
2497 if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2501 * First, lock the client structure, so everyone else will
2502 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2503 * that only one thread does the work.
2506 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2508 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2509 NFSCLSTATEMUTEXPTR, NULL);
2510 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2511 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2513 nfsv4_unlock(&clp->nfsc_lock, 0);
2517 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2520 nmp = clp->nfsc_nmp;
2522 panic("nfscl expired");
2523 cred = newnfs_getcred();
2526 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2527 } while ((error == NFSERR_STALECLIENTID ||
2528 error == NFSERR_BADSESSION ||
2529 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2532 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2535 * Expire the state for the client.
2537 nfscl_expireclient(clp, nmp, cred, p);
2539 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2540 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2542 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2543 wakeup(&clp->nfsc_flags);
2544 nfsv4_unlock(&clp->nfsc_lock, 0);
2551 * This function inserts a lock in the list after insert_lop.
2554 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2555 struct nfscllock *insert_lop, int local)
2558 if ((struct nfscllockowner *)insert_lop == lp)
2559 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2561 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2563 nfsstatsv1.cllocallocks++;
2565 nfsstatsv1.cllocks++;
2569 * This function updates the locking for a lock owner and given file. It
2570 * maintains a list of lock ranges ordered on increasing file offset that
2571 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2572 * It always adds new_lop to the list and sometimes uses the one pointed
2574 * Returns 1 if the locks were modified, 0 otherwise.
2577 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2578 struct nfscllock **other_lopp, int local)
2580 struct nfscllock *new_lop = *new_lopp;
2581 struct nfscllock *lop, *tlop, *ilop;
2582 struct nfscllock *other_lop;
2583 int unlock = 0, modified = 0;
2587 * Work down the list until the lock is merged.
2589 if (new_lop->nfslo_type == F_UNLCK)
2591 ilop = (struct nfscllock *)lp;
2592 lop = LIST_FIRST(&lp->nfsl_lock);
2593 while (lop != NULL) {
2595 * Only check locks for this file that aren't before the start of
2598 if (lop->nfslo_end >= new_lop->nfslo_first) {
2599 if (new_lop->nfslo_end < lop->nfslo_first) {
2601 * If the new lock ends before the start of the
2602 * current lock's range, no merge, just insert
2607 if (new_lop->nfslo_type == lop->nfslo_type ||
2608 (new_lop->nfslo_first <= lop->nfslo_first &&
2609 new_lop->nfslo_end >= lop->nfslo_end)) {
2611 * This lock can be absorbed by the new lock/unlock.
2612 * This happens when it covers the entire range
2613 * of the old lock or is contiguous
2614 * with the old lock and is of the same type or an
2617 if (new_lop->nfslo_type != lop->nfslo_type ||
2618 new_lop->nfslo_first != lop->nfslo_first ||
2619 new_lop->nfslo_end != lop->nfslo_end)
2621 if (lop->nfslo_first < new_lop->nfslo_first)
2622 new_lop->nfslo_first = lop->nfslo_first;
2623 if (lop->nfslo_end > new_lop->nfslo_end)
2624 new_lop->nfslo_end = lop->nfslo_end;
2626 lop = LIST_NEXT(lop, nfslo_list);
2627 nfscl_freelock(tlop, local);
2632 * All these cases are for contiguous locks that are not the
2633 * same type, so they can't be merged.
2635 if (new_lop->nfslo_first <= lop->nfslo_first) {
2637 * This case is where the new lock overlaps with the
2638 * first part of the old lock. Move the start of the
2639 * old lock to just past the end of the new lock. The
2640 * new lock will be inserted in front of the old, since
2641 * ilop hasn't been updated. (We are done now.)
2643 if (lop->nfslo_first != new_lop->nfslo_end) {
2644 lop->nfslo_first = new_lop->nfslo_end;
2649 if (new_lop->nfslo_end >= lop->nfslo_end) {
2651 * This case is where the new lock overlaps with the
2652 * end of the old lock's range. Move the old lock's
2653 * end to just before the new lock's first and insert
2654 * the new lock after the old lock.
2655 * Might not be done yet, since the new lock could
2656 * overlap further locks with higher ranges.
2658 if (lop->nfslo_end != new_lop->nfslo_first) {
2659 lop->nfslo_end = new_lop->nfslo_first;
2663 lop = LIST_NEXT(lop, nfslo_list);
2667 * The final case is where the new lock's range is in the
2668 * middle of the current lock's and splits the current lock
2669 * up. Use *other_lopp to handle the second part of the
2670 * split old lock range. (We are done now.)
2671 * For unlock, we use new_lop as other_lop and tmp, since
2672 * other_lop and new_lop are the same for this case.
2673 * We noted the unlock case above, so we don't need
2674 * new_lop->nfslo_type any longer.
2676 tmp = new_lop->nfslo_first;
2678 other_lop = new_lop;
2681 other_lop = *other_lopp;
2684 other_lop->nfslo_first = new_lop->nfslo_end;
2685 other_lop->nfslo_end = lop->nfslo_end;
2686 other_lop->nfslo_type = lop->nfslo_type;
2687 lop->nfslo_end = tmp;
2688 nfscl_insertlock(lp, other_lop, lop, local);
2694 lop = LIST_NEXT(lop, nfslo_list);
2700 * Insert the new lock in the list at the appropriate place.
2703 nfscl_insertlock(lp, new_lop, ilop, local);
2711 * This function must be run as a kernel thread.
2712 * It does Renew Ops and recovery, when required.
2715 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2717 struct nfsclowner *owp, *nowp;
2718 struct nfsclopen *op;
2719 struct nfscllockowner *lp, *nlp;
2720 struct nfscldeleghead dh;
2721 struct nfscldeleg *dp, *ndp;
2724 int error, cbpathdown, islept, igotlock, ret, clearok;
2725 uint32_t recover_done_time = 0;
2727 static time_t prevsec = 0;
2728 struct nfscllockownerfh *lfhp, *nlfhp;
2729 struct nfscllockownerfhhead lfh;
2730 struct nfscllayout *lyp, *nlyp;
2731 struct nfscldevinfo *dip, *ndip;
2732 struct nfscllayouthead rlh;
2733 struct nfsclrecalllayout *recallp;
2734 struct nfsclds *dsp;
2739 cred = newnfs_getcred();
2741 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2742 mp = clp->nfsc_nmp->nm_mountp;
2745 newnfs_setroot(cred);
2747 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2749 * Only allow one full recover within 1/2 of the lease
2750 * duration (nfsc_renew).
2751 * retok is value/result. If passed in set to true,
2752 * it indicates only a CreateSession operation should
2754 * If it is returned true, it indicates that the
2755 * recovery only required a CreateSession.
2758 if (recover_done_time < NFSD_MONOSEC) {
2759 recover_done_time = NFSD_MONOSEC +
2763 NFSCL_DEBUG(1, "Doing recovery, only "
2764 "createsession=%d\n", retok);
2765 nfscl_recover(clp, &retok, cred, p);
2767 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2768 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2769 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2770 clidrev = clp->nfsc_clientidrev;
2771 error = nfsrpc_renew(clp, NULL, cred, p);
2772 if (error == NFSERR_CBPATHDOWN)
2774 else if (error == NFSERR_STALECLIENTID) {
2776 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2778 } else if (error == NFSERR_EXPIRED)
2779 (void) nfscl_hasexpired(clp, clidrev, p);
2783 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2784 /* Do renews for any DS sessions. */
2785 NFSLOCKMNT(clp->nfsc_nmp);
2786 /* Skip first entry, since the MDS is handled above. */
2787 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2789 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2790 while (dsp != NULL) {
2791 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2792 dsp->nfsclds_sess.nfsess_defunct == 0) {
2793 dsp->nfsclds_expire = NFSD_MONOSEC +
2795 NFSUNLOCKMNT(clp->nfsc_nmp);
2796 (void)nfsrpc_renew(clp, dsp, cred, p);
2799 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2801 NFSUNLOCKMNT(clp->nfsc_nmp);
2807 /* It's a Total Recall! */
2808 nfscl_totalrecall(clp);
2811 * Now, handle defunct owners.
2813 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2814 if (LIST_EMPTY(&owp->nfsow_open)) {
2815 if (owp->nfsow_defunct != 0)
2816 nfscl_freeopenowner(owp, 0);
2821 * Do the recall on any delegations. To avoid trouble, always
2822 * come back up here after having slept.
2826 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2827 while (dp != NULL) {
2828 ndp = TAILQ_NEXT(dp, nfsdl_list);
2829 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2831 * Wait for outstanding I/O ops to be done.
2833 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2835 nfsv4_unlock(&clp->nfsc_lock, 0);
2838 dp->nfsdl_rwlock.nfslock_lock |=
2840 msleep(&dp->nfsdl_rwlock,
2841 NFSCLSTATEMUTEXPTR, PVFS, "nfscld",
2843 if (NFSCL_FORCEDISM(mp))
2848 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2849 &islept, NFSCLSTATEMUTEXPTR, mp);
2850 if (igotlock == 0 && NFSCL_FORCEDISM(mp))
2856 newnfs_copycred(&dp->nfsdl_cred, cred);
2857 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2858 NULL, cred, p, 1, &vp);
2860 nfscl_cleandeleg(dp);
2861 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2863 LIST_REMOVE(dp, nfsdl_hash);
2864 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2866 nfsstatsv1.cldelegates--;
2870 * The nfsc_lock must be released before doing
2871 * vrele(), since it might call nfs_inactive().
2872 * For the unlikely case where the vnode failed
2873 * to be acquired by nfscl_recalldeleg(), a
2874 * VOP_RECLAIM() should be in progress and it
2875 * will return the delegation.
2877 nfsv4_unlock(&clp->nfsc_lock, 0);
2890 * Clear out old delegations, if we are above the high water
2891 * mark. Only clear out ones with no state related to them.
2892 * The tailq list is in LRU order.
2894 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2895 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2896 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2897 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2898 dp->nfsdl_rwlock.nfslock_lock == 0 &&
2899 dp->nfsdl_timestamp < NFSD_MONOSEC &&
2900 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2901 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2903 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2904 op = LIST_FIRST(&owp->nfsow_open);
2911 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2912 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2919 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2920 LIST_REMOVE(dp, nfsdl_hash);
2921 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2923 nfsstatsv1.cldelegates--;
2929 nfsv4_unlock(&clp->nfsc_lock, 0);
2932 * Do the recall on any layouts. To avoid trouble, always
2933 * come back up here after having slept.
2937 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2938 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2940 * Wait for outstanding I/O ops to be done.
2942 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2943 (lyp->nfsly_lock.nfslock_lock &
2944 NFSV4LOCK_LOCK) != 0) {
2945 lyp->nfsly_lock.nfslock_lock |=
2947 msleep(&lyp->nfsly_lock.nfslock_lock,
2948 NFSCLSTATEMUTEXPTR, PVFS, "nfslyp",
2950 if (NFSCL_FORCEDISM(mp))
2954 /* Move the layout to the recall list. */
2955 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2957 LIST_REMOVE(lyp, nfsly_hash);
2958 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2960 /* Handle any layout commits. */
2961 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2962 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2963 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2965 NFSCL_DEBUG(3, "do layoutcommit\n");
2966 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2974 /* Now, look for stale layouts. */
2975 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2976 while (lyp != NULL) {
2977 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2978 if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2979 (lyp->nfsly_flags & (NFSLY_RECALL |
2980 NFSLY_RETONCLOSE)) == 0 &&
2981 lyp->nfsly_lock.nfslock_usecnt == 0 &&
2982 lyp->nfsly_lock.nfslock_lock == 0) {
2983 NFSCL_DEBUG(4, "ret stale lay=%d\n",
2985 recallp = malloc(sizeof(*recallp),
2986 M_NFSLAYRECALL, M_NOWAIT);
2987 if (recallp == NULL)
2989 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2990 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2991 lyp->nfsly_stateid.seqid, 0, 0, NULL,
2998 * Free up any unreferenced device info structures.
3000 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
3001 if (dip->nfsdi_layoutrefs == 0 &&
3002 dip->nfsdi_refcnt == 0) {
3003 NFSCL_DEBUG(4, "freeing devinfo\n");
3004 LIST_REMOVE(dip, nfsdi_list);
3005 nfscl_freedevinfo(dip);
3010 /* Do layout return(s), as required. */
3011 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
3012 TAILQ_REMOVE(&rlh, lyp, nfsly_list);
3013 NFSCL_DEBUG(4, "ret layout\n");
3014 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
3015 if ((lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) {
3017 lyp->nfsly_flags |= NFSLY_RETURNED;
3021 nfscl_freelayout(lyp);
3025 * Delegreturn any delegations cleaned out or recalled.
3027 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
3028 newnfs_copycred(&dp->nfsdl_cred, cred);
3029 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3030 TAILQ_REMOVE(&dh, dp, nfsdl_list);
3031 free(dp, M_NFSCLDELEG);
3036 * Call nfscl_cleanupkext() once per second to check for
3037 * open/lock owners where the process has exited.
3039 mytime = NFSD_MONOSEC;
3040 if (prevsec != mytime) {
3042 nfscl_cleanupkext(clp, &lfh);
3046 * Do a ReleaseLockOwner for all lock owners where the
3047 * associated process no longer exists, as found by
3048 * nfscl_cleanupkext().
3050 newnfs_setroot(cred);
3051 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
3052 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
3054 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
3055 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
3057 nfscl_freelockowner(lp, 0);
3064 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
3065 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
3068 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
3069 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
3072 wakeup((caddr_t)clp);
3080 * Initiate state recovery. Called when NFSERR_STALECLIENTID,
3081 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
3084 nfscl_initiate_recovery(struct nfsclclient *clp)
3090 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
3092 wakeup((caddr_t)clp);
3096 * Dump out the state stuff for debugging.
3099 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
3100 int lockowner, int locks)
3102 struct nfsclclient *clp;
3103 struct nfsclowner *owp;
3104 struct nfsclopen *op;
3105 struct nfscllockowner *lp;
3106 struct nfscllock *lop;
3107 struct nfscldeleg *dp;
3111 printf("nfscl dumpstate NULL clp\n");
3115 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
3116 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3117 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
3118 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
3119 owp->nfsow_owner[0], owp->nfsow_owner[1],
3120 owp->nfsow_owner[2], owp->nfsow_owner[3],
3122 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3124 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
3125 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
3126 op->nfso_stateid.other[2], op->nfso_opencnt,
3128 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
3130 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
3131 lp->nfsl_owner[0], lp->nfsl_owner[1],
3132 lp->nfsl_owner[2], lp->nfsl_owner[3],
3134 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
3135 lp->nfsl_stateid.other[2]);
3136 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3139 printf("lck typ=%d fst=%ju end=%ju\n",
3140 lop->nfslo_type, (intmax_t)lop->nfslo_first,
3141 (intmax_t)lop->nfslo_end);
3143 printf("lck typ=%d fst=%qd end=%qd\n",
3144 lop->nfslo_type, lop->nfslo_first,
3152 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3153 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
3154 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
3155 owp->nfsow_owner[0], owp->nfsow_owner[1],
3156 owp->nfsow_owner[2], owp->nfsow_owner[3],
3158 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3160 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
3161 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
3162 op->nfso_stateid.other[2], op->nfso_opencnt,
3164 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
3166 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
3167 lp->nfsl_owner[0], lp->nfsl_owner[1],
3168 lp->nfsl_owner[2], lp->nfsl_owner[3],
3170 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
3171 lp->nfsl_stateid.other[2]);
3172 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3175 printf("lck typ=%d fst=%ju end=%ju\n",
3176 lop->nfslo_type, (intmax_t)lop->nfslo_first,
3177 (intmax_t)lop->nfslo_end);
3179 printf("lck typ=%d fst=%qd end=%qd\n",
3180 lop->nfslo_type, lop->nfslo_first,
3191 * Check for duplicate open owners and opens.
3192 * (Only used as a diagnostic aid.)
3195 nfscl_dupopen(vnode_t vp, int dupopens)
3197 struct nfsclclient *clp;
3198 struct nfsclowner *owp, *owp2;
3199 struct nfsclopen *op, *op2;
3202 clp = VFSTONFS(vp->v_mount)->nm_clp;
3204 printf("nfscl dupopen NULL clp\n");
3207 nfhp = VTONFS(vp)->n_fhp;
3211 * First, search for duplicate owners.
3212 * These should never happen!
3214 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3215 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3217 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
3218 NFSV4CL_LOCKNAMELEN)) {
3220 printf("DUP OWNER\n");
3221 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3228 * Now, search for duplicate stateids.
3229 * These shouldn't happen, either.
3231 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3232 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3233 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3234 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3236 (op->nfso_stateid.other[0] != 0 ||
3237 op->nfso_stateid.other[1] != 0 ||
3238 op->nfso_stateid.other[2] != 0) &&
3239 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
3240 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
3241 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
3243 printf("DUP STATEID\n");
3244 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3253 * Now search for duplicate opens.
3254 * Duplicate opens for the same owner
3255 * should never occur. Other duplicates are
3256 * possible and are checked for if "dupopens"
3259 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3260 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3261 if (nfhp->nfh_len == op2->nfso_fhlen &&
3262 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3263 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3264 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3265 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3266 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3267 (!NFSBCMP(op->nfso_own->nfsow_owner,
3268 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3270 if (!NFSBCMP(op->nfso_own->nfsow_owner,
3271 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3273 printf("BADDUP OPEN\n");
3276 printf("DUP OPEN\n");
3278 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0,
3291 * During close, find an open that needs to be dereferenced and
3292 * dereference it. If there are no more opens for this file,
3293 * log a message to that effect.
3294 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3295 * on the file's vnode.
3296 * This is the safe way, since it is difficult to identify
3297 * which open the close is for and I/O can be performed after the
3298 * close(2) system call when a file is mmap'd.
3299 * If it returns 0 for success, there will be a referenced
3300 * clp returned via clpp.
3303 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3305 struct nfsclclient *clp;
3306 struct nfsclowner *owp;
3307 struct nfsclopen *op;
3308 struct nfscldeleg *dp;
3312 error = nfscl_getcl(vp->v_mount, NULL, NULL, false, true, &clp);
3317 nfhp = VTONFS(vp)->n_fhp;
3321 * First, look for one under a delegation that was locally issued
3322 * and just decrement the opencnt for it. Since all my Opens against
3323 * the server are DENY_NONE, I don't see a problem with hanging
3324 * onto them. (It is much easier to use one of the extant Opens
3325 * that I already have on the server when a Delegation is recalled
3326 * than to do fresh Opens.) Someday, I might need to rethink this, but.
3328 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3330 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3331 op = LIST_FIRST(&owp->nfsow_open);
3334 * Since a delegation is for a file, there
3335 * should never be more than one open for
3338 if (LIST_NEXT(op, nfso_list) != NULL)
3339 panic("nfscdeleg opens");
3340 if (notdecr && op->nfso_opencnt > 0) {
3349 /* Now process the opens against the server. */
3350 LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len),
3352 if (op->nfso_fhlen == nfhp->nfh_len &&
3353 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3355 /* Found an open, decrement cnt if possible */
3356 if (notdecr && op->nfso_opencnt > 0) {
3361 * There are more opens, so just return.
3363 if (op->nfso_opencnt > 0) {
3371 printf("nfscl: never fnd open\n");
3376 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3378 struct nfsclclient *clp;
3379 struct nfsmount *nmp;
3380 struct nfsclowner *owp, *nowp;
3381 struct nfsclopen *op, *nop;
3382 struct nfsclopenhead delayed;
3383 struct nfscldeleg *dp;
3385 struct nfsclrecalllayout *recallp;
3386 struct nfscllayout *lyp;
3389 error = nfscl_getcl(vp->v_mount, NULL, NULL, false, true, &clp);
3394 nmp = VFSTONFS(vp->v_mount);
3395 nfhp = VTONFS(vp)->n_fhp;
3396 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
3399 * First get rid of the local Open structures, which should be no
3402 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3404 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3405 op = LIST_FIRST(&owp->nfsow_open);
3407 KASSERT((op->nfso_opencnt == 0),
3408 ("nfscl: bad open cnt on deleg"));
3409 nfscl_freeopen(op, 1, true);
3411 nfscl_freeopenowner(owp, 1);
3415 /* Return any layouts marked return on close. */
3416 nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp,
3419 /* Now process the opens against the server. */
3420 LIST_INIT(&delayed);
3422 LIST_FOREACH(op, NFSCLOPENHASH(clp, nfhp->nfh_fh, nfhp->nfh_len),
3424 if (op->nfso_fhlen == nfhp->nfh_len &&
3425 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3427 /* Found an open, close it. */
3429 KASSERT((op->nfso_opencnt == 0),
3430 ("nfscl: bad open cnt on server (%d)",
3434 if (NFSHASNFSV4N(nmp))
3435 error = nfsrpc_doclose(nmp, op, p, false, true);
3437 error = nfsrpc_doclose(nmp, op, p, true, true);
3439 if (error == NFSERR_DELAY) {
3440 nfscl_unlinkopen(op);
3441 op->nfso_own = NULL;
3442 LIST_INSERT_HEAD(&delayed, op, nfso_list);
3447 nfscl_clrelease(clp);
3449 /* Now, wait for any layout that is returned upon close. */
3451 while ((lyp->nfsly_flags & NFSLY_RETURNED) == 0) {
3452 if (NFSCL_FORCEDISM(nmp->nm_mountp)) {
3456 msleep(lyp, NFSCLSTATEMUTEXPTR, PZERO, "nfslroc", hz);
3459 nfscl_freelayout(lyp);
3464 * recallp has been set NULL by nfscl_retoncloselayout() if it was
3465 * used by the function, but calling free() with a NULL pointer is ok.
3467 free(recallp, M_NFSLAYRECALL);
3469 /* Now, loop retrying the delayed closes. */
3470 LIST_FOREACH_SAFE(op, &delayed, nfso_list, nop) {
3471 nfsrpc_doclose(nmp, op, p, true, false);
3472 LIST_REMOVE(op, nfso_list);
3473 nfscl_freeopen(op, 0, false);
3479 * Return all delegations on this client.
3480 * (Must be called with client sleep lock.)
3483 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p,
3484 struct nfscldeleghead *dhp)
3486 struct nfscldeleg *dp, *ndp;
3489 cred = newnfs_getcred();
3490 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3491 nfscl_cleandeleg(dp);
3492 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3494 nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
3495 TAILQ_INSERT_HEAD(dhp, dp, nfsdl_list);
3497 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
3503 * Return any delegation for this vp.
3506 nfscl_delegreturnvp(vnode_t vp, NFSPROC_T *p)
3508 struct nfsclclient *clp;
3509 struct nfscldeleg *dp;
3512 struct nfsmount *nmp;
3514 nmp = VFSTONFS(vp->v_mount);
3516 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
3522 cred = newnfs_getcred();
3527 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3528 np->n_fhp->nfh_len);
3530 nfscl_cleandeleg(dp);
3531 nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
3533 newnfs_copycred(&dp->nfsdl_cred, cred);
3534 nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3535 free(dp, M_NFSCLDELEG);
3542 * Do a callback RPC.
3545 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3547 int clist, gotseq_ok, i, j, k, op, rcalls;
3549 struct nfsclclient *clp;
3550 struct nfscldeleg *dp = NULL;
3551 int numops, taglen = -1, error = 0, trunc __unused;
3552 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3553 u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3559 nfsattrbit_t attrbits, rattrbits;
3560 nfsv4stateid_t stateid;
3561 uint32_t seqid, slotid = 0, highslot, cachethis __unused;
3562 uint8_t sessionid[NFSX_V4SESSIONID];
3564 struct nfscllayout *lyp;
3565 uint64_t filesid[2], len, off;
3566 int changed, gotone, laytype, recalltype;
3568 struct nfsclrecalllayout *recallp = NULL;
3569 struct nfsclsession *tsep;
3573 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3574 taglen = fxdr_unsigned(int, *tl);
3575 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
3580 if (taglen <= NFSV4_SMALLSTR)
3583 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3584 error = nfsrv_mtostr(nd, tagstr, taglen);
3586 if (taglen > NFSV4_SMALLSTR)
3587 free(tagstr, M_TEMP);
3591 (void) nfsm_strtom(nd, tag, taglen);
3592 if (taglen > NFSV4_SMALLSTR) {
3593 free(tagstr, M_TEMP);
3595 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3596 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3597 minorvers = fxdr_unsigned(u_int32_t, *tl++);
3598 if (minorvers != NFSV4_MINORVERSION &&
3599 minorvers != NFSV41_MINORVERSION &&
3600 minorvers != NFSV42_MINORVERSION)
3601 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3602 cbident = fxdr_unsigned(u_int32_t, *tl++);
3606 numops = fxdr_unsigned(int, *tl);
3608 * Loop around doing the sub ops.
3610 for (i = 0; i < numops; i++) {
3611 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3612 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3614 op = fxdr_unsigned(int, *tl);
3615 nd->nd_procnum = op;
3616 if (i == 0 && op != NFSV4OP_CBSEQUENCE && minorvers !=
3617 NFSV4_MINORVERSION) {
3618 nd->nd_repstat = NFSERR_OPNOTINSESS;
3619 *repp = nfscl_errmap(nd, minorvers);
3623 if (op < NFSV4OP_CBGETATTR ||
3624 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3625 (op > NFSV4OP_CBNOTIFYDEVID &&
3626 minorvers == NFSV41_MINORVERSION) ||
3627 (op > NFSV4OP_CBOFFLOAD &&
3628 minorvers == NFSV42_MINORVERSION)) {
3629 nd->nd_repstat = NFSERR_OPILLEGAL;
3630 *repp = nfscl_errmap(nd, minorvers);
3634 if (op < NFSV42_CBNOPS)
3635 nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
3637 case NFSV4OP_CBGETATTR:
3638 NFSCL_DEBUG(4, "cbgetattr\n");
3641 error = nfsm_getfh(nd, &nfhp);
3643 error = nfsrv_getattrbits(nd, &attrbits,
3646 mp = nfscl_getmnt(minorvers, sessionid, cbident,
3649 error = NFSERR_SERVERFAULT;
3652 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3653 nfhp->nfh_len, p, &np);
3658 NFSZERO_ATTRBIT(&rattrbits);
3660 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3663 if (NFSISSET_ATTRBIT(&attrbits,
3666 va.va_size = np->n_size;
3670 NFSSETBIT_ATTRBIT(&rattrbits,
3673 if (NFSISSET_ATTRBIT(&attrbits,
3674 NFSATTRBIT_CHANGE)) {
3678 (np->n_flag & NDELEGMOD))
3680 NFSSETBIT_ATTRBIT(&rattrbits,
3684 error = NFSERR_SERVERFAULT;
3692 free(nfhp, M_NFSFH);
3694 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3695 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3698 case NFSV4OP_CBRECALL:
3699 NFSCL_DEBUG(4, "cbrecall\n");
3700 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3702 stateid.seqid = *tl++;
3703 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3705 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3706 trunc = fxdr_unsigned(int, *tl);
3707 error = nfsm_getfh(nd, &nfhp);
3710 if (minorvers == NFSV4_MINORVERSION)
3711 clp = nfscl_getclnt(cbident);
3713 clp = nfscl_getclntsess(sessionid);
3715 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3717 if (dp != NULL && (dp->nfsdl_flags &
3718 NFSCLDL_DELEGRET) == 0) {
3721 wakeup((caddr_t)clp);
3724 error = NFSERR_SERVERFAULT;
3729 free(nfhp, M_NFSFH);
3731 case NFSV4OP_CBLAYOUTRECALL:
3732 NFSCL_DEBUG(4, "cblayrec\n");
3734 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3735 laytype = fxdr_unsigned(int, *tl++);
3736 iomode = fxdr_unsigned(uint32_t, *tl++);
3737 if (newnfs_true == *tl++)
3741 recalltype = fxdr_unsigned(int, *tl);
3742 NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
3743 laytype, iomode, changed, recalltype);
3744 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3746 if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
3747 laytype != NFSLAYOUT_FLEXFILE)
3748 error = NFSERR_NOMATCHLAYOUT;
3749 else if (recalltype == NFSLAYOUTRETURN_FILE) {
3750 error = nfsm_getfh(nd, &nfhp);
3751 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3754 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3756 off = fxdr_hyper(tl); tl += 2;
3757 len = fxdr_hyper(tl); tl += 2;
3758 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3759 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3760 if (minorvers == NFSV4_MINORVERSION)
3761 error = NFSERR_NOTSUPP;
3762 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
3763 (uintmax_t)off, (uintmax_t)len,
3764 stateid.seqid, error);
3767 clp = nfscl_getclntsess(sessionid);
3768 NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3770 lyp = nfscl_findlayout(clp,
3773 NFSCL_DEBUG(4, "cblyp=%p\n",
3778 NFSLY_FLEXFILE)) != 0 &&
3779 !NFSBCMP(stateid.other,
3780 lyp->nfsly_stateid.other,
3781 NFSX_STATEIDOTHER)) {
3791 lyp->nfsly_stateid.seqid)
3792 lyp->nfsly_stateid.seqid =
3800 lyp->nfsly_stateid.seqid);
3803 NFSERR_NOMATCHLAYOUT;
3805 error = NFSERR_NOMATCHLAYOUT;
3808 free(nfhp, M_NFSFH);
3809 } else if (recalltype == NFSLAYOUTRETURN_FSID) {
3810 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3811 filesid[0] = fxdr_hyper(tl); tl += 2;
3812 filesid[1] = fxdr_hyper(tl); tl += 2;
3815 clp = nfscl_getclntsess(sessionid);
3817 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3819 if (lyp->nfsly_filesid[0] ==
3821 lyp->nfsly_filesid[1] ==
3828 lyp->nfsly_stateid.seqid,
3838 error = NFSERR_NOMATCHLAYOUT;
3840 error = NFSERR_NOMATCHLAYOUT;
3842 } else if (recalltype == NFSLAYOUTRETURN_ALL) {
3845 clp = nfscl_getclntsess(sessionid);
3847 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3849 error = nfscl_layoutrecall(
3850 recalltype, lyp, iomode, 0,
3852 lyp->nfsly_stateid.seqid,
3853 0, 0, NULL, recallp);
3860 error = NFSERR_NOMATCHLAYOUT;
3862 error = NFSERR_NOMATCHLAYOUT;
3865 error = NFSERR_NOMATCHLAYOUT;
3866 if (recallp != NULL) {
3867 free(recallp, M_NFSLAYRECALL);
3871 case NFSV4OP_CBSEQUENCE:
3873 error = NFSERR_SEQUENCEPOS;
3876 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3878 bcopy(tl, sessionid, NFSX_V4SESSIONID);
3879 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3880 seqid = fxdr_unsigned(uint32_t, *tl++);
3881 slotid = fxdr_unsigned(uint32_t, *tl++);
3882 highslot = fxdr_unsigned(uint32_t, *tl++);
3884 /* Throw away the referring call stuff. */
3885 clist = fxdr_unsigned(int, *tl);
3886 for (j = 0; j < clist; j++) {
3887 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3889 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3890 rcalls = fxdr_unsigned(int, *tl);
3891 for (k = 0; k < rcalls; k++) {
3892 NFSM_DISSECT(tl, uint32_t *,
3897 clp = nfscl_getclntsess(sessionid);
3899 error = NFSERR_SERVERFAULT;
3901 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3902 error = nfsv4_seqsession(seqid, slotid,
3903 highslot, tsep->nfsess_cbslots, &rep,
3904 tsep->nfsess_backslots);
3907 if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
3911 * Handle a reply for a retried
3912 * callback. The reply will be
3913 * re-inserted in the session cache
3914 * by the nfsv4_seqsess_cacherep() call
3917 KASSERT(error == NFSERR_REPLYFROMCACHE,
3918 ("cbsequence: non-NULL rep"));
3919 NFSCL_DEBUG(4, "Got cbretry\n");
3920 m_freem(nd->nd_mreq);
3925 NFSM_BUILD(tl, uint32_t *,
3926 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3927 bcopy(sessionid, tl, NFSX_V4SESSIONID);
3928 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3929 *tl++ = txdr_unsigned(seqid);
3930 *tl++ = txdr_unsigned(slotid);
3931 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3932 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3936 if (i == 0 && minorvers != NFSV4_MINORVERSION)
3937 error = NFSERR_OPNOTINSESS;
3939 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3940 error = NFSERR_NOTSUPP;
3945 if (error == EBADRPC || error == NFSERR_BADXDR) {
3946 nd->nd_repstat = NFSERR_BADXDR;
3948 nd->nd_repstat = error;
3953 if (nd->nd_repstat) {
3954 *repp = nfscl_errmap(nd, minorvers);
3957 *repp = 0; /* NFS4_OK */
3960 if (recallp != NULL)
3961 free(recallp, M_NFSLAYRECALL);
3963 if (error == EBADRPC || error == NFSERR_BADXDR)
3964 nd->nd_repstat = NFSERR_BADXDR;
3966 printf("nfsv4 comperr1=%d\n", error);
3969 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3973 *retopsp = txdr_unsigned(retops);
3975 *nd->nd_errp = nfscl_errmap(nd, minorvers);
3977 if (gotseq_ok != 0) {
3978 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3980 clp = nfscl_getclntsess(sessionid);
3982 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3983 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3994 * Generate the next cbident value. Basically just increment a static value
3995 * and then check that it isn't already in the list, if it has wrapped around.
3998 nfscl_nextcbident(void)
4000 struct nfsclclient *clp;
4002 static u_int32_t nextcbident = 0;
4003 static int haswrapped = 0;
4006 if (nextcbident == 0)
4010 * Search the clientid list for one already using this cbident.
4015 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
4016 if (clp->nfsc_cbident == nextcbident) {
4026 return (nextcbident);
4030 * Get the mount point related to a given cbident or session and busy it.
4033 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
4034 struct nfsclclient **clpp)
4036 struct nfsclclient *clp;
4039 struct nfsclsession *tsep;
4043 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
4044 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
4045 if (minorvers == NFSV4_MINORVERSION) {
4046 if (clp->nfsc_cbident == cbident)
4048 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
4056 mp = clp->nfsc_nmp->nm_mountp;
4059 error = vfs_busy(mp, 0);
4068 * Get the clientid pointer related to a given cbident.
4070 static struct nfsclclient *
4071 nfscl_getclnt(u_int32_t cbident)
4073 struct nfsclclient *clp;
4075 LIST_FOREACH(clp, &nfsclhead, nfsc_list)
4076 if (clp->nfsc_cbident == cbident)
4082 * Get the clientid pointer related to a given sessionid.
4084 static struct nfsclclient *
4085 nfscl_getclntsess(uint8_t *sessionid)
4087 struct nfsclclient *clp;
4088 struct nfsclsession *tsep;
4090 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
4091 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
4092 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
4100 * Search for a lock conflict locally on the client. A conflict occurs if
4101 * - not same owner and overlapping byte range and at least one of them is
4102 * a write lock or this is an unlock.
4105 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
4106 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
4107 struct nfscllock **lopp)
4109 struct nfsclopen *op;
4113 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
4117 LIST_FOREACH(op, NFSCLOPENHASH(clp, fhp, fhlen), nfso_hash) {
4118 if (op->nfso_fhlen == fhlen &&
4119 !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
4120 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
4130 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
4131 u_int8_t *own, struct nfscllock **lopp)
4133 struct nfscllockowner *lp;
4134 struct nfscllock *lop;
4136 LIST_FOREACH(lp, lhp, nfsl_list) {
4137 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
4138 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
4139 if (lop->nfslo_first >= nlop->nfslo_end)
4141 if (lop->nfslo_end <= nlop->nfslo_first)
4143 if (lop->nfslo_type == F_WRLCK ||
4144 nlop->nfslo_type == F_WRLCK ||
4145 nlop->nfslo_type == F_UNLCK) {
4148 return (NFSERR_DENIED);
4157 * Check for a local conflicting lock.
4160 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
4161 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
4163 struct nfscllock *lop, nlck;
4164 struct nfscldeleg *dp;
4166 u_int8_t own[NFSV4CL_LOCKNAMELEN];
4169 nlck.nfslo_type = fl->l_type;
4170 nlck.nfslo_first = off;
4171 if (len == NFS64BITSSET) {
4172 nlck.nfslo_end = NFS64BITSSET;
4174 nlck.nfslo_end = off + len;
4175 if (nlck.nfslo_end <= nlck.nfslo_first)
4176 return (NFSERR_INVAL);
4179 nfscl_filllockowner(id, own, flags);
4181 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4182 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
4183 &nlck, own, dp, &lop);
4185 fl->l_whence = SEEK_SET;
4186 fl->l_start = lop->nfslo_first;
4187 if (lop->nfslo_end == NFS64BITSSET)
4190 fl->l_len = lop->nfslo_end - lop->nfslo_first;
4191 fl->l_pid = (pid_t)0;
4192 fl->l_type = lop->nfslo_type;
4193 error = -1; /* no RPC required */
4194 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
4195 fl->l_type == F_RDLCK)) {
4197 * The delegation ensures that there isn't a conflicting
4198 * lock on the server, so return -1 to indicate an RPC
4201 fl->l_type = F_UNLCK;
4209 * Handle Recall of a delegation.
4210 * The clp must be exclusive locked when this is called.
4213 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
4214 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4215 int called_from_renewthread, vnode_t *vpp)
4217 struct nfsclowner *owp, *lowp, *nowp;
4218 struct nfsclopen *op, *lop;
4219 struct nfscllockowner *lp;
4220 struct nfscllock *lckp;
4225 KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL"));
4228 * First, get a vnode for the file. This is needed to do RPCs.
4230 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
4231 dp->nfsdl_fhlen, p, &np);
4234 * File isn't open, so nothing to move over to the
4244 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
4247 * Ok, if it's a write delegation, flush data to the server, so
4248 * that close/open consistency is retained.
4252 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
4253 np->n_flag |= NDELEGRECALL;
4255 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
4257 np->n_flag &= ~NDELEGRECALL;
4259 NFSINVALATTRCACHE(np);
4261 if (ret == EIO && called_from_renewthread != 0) {
4263 * If the flush failed with EIO for the renew thread,
4264 * return now, so that the dirty buffer will be flushed
4271 * Now, for each openowner with opens issued locally, move them
4272 * over to state against the server.
4274 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
4275 lop = LIST_FIRST(&lowp->nfsow_open);
4277 if (LIST_NEXT(lop, nfso_list) != NULL)
4278 panic("nfsdlg mult opens");
4280 * Look for the same openowner against the server.
4282 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
4283 if (!NFSBCMP(lowp->nfsow_owner,
4284 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
4285 newnfs_copycred(&dp->nfsdl_cred, cred);
4286 ret = nfscl_moveopen(vp, clp, nmp, lop,
4288 if (ret == NFSERR_STALECLIENTID ||
4289 ret == NFSERR_STALEDONTRECOVER ||
4290 ret == NFSERR_BADSESSION)
4293 nfscl_freeopen(lop, 1, true);
4302 * If no openowner found, create one and get an open
4307 sizeof (struct nfsclowner), M_NFSCLOWNER,
4309 nfscl_newopen(clp, NULL, &owp, &nowp, &op,
4310 NULL, lowp->nfsow_owner, dp->nfsdl_fh,
4311 dp->nfsdl_fhlen, NULL, NULL);
4312 newnfs_copycred(&dp->nfsdl_cred, cred);
4313 ret = nfscl_moveopen(vp, clp, nmp, lop,
4316 nfscl_freeopenowner(owp, 0);
4317 if (ret == NFSERR_STALECLIENTID ||
4318 ret == NFSERR_STALEDONTRECOVER ||
4319 ret == NFSERR_BADSESSION)
4322 nfscl_freeopen(lop, 1, true);
4332 * Now, get byte range locks for any locks done locally.
4334 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4335 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
4336 newnfs_copycred(&dp->nfsdl_cred, cred);
4337 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
4338 if (ret == NFSERR_STALESTATEID ||
4339 ret == NFSERR_STALEDONTRECOVER ||
4340 ret == NFSERR_STALECLIENTID ||
4341 ret == NFSERR_BADSESSION)
4351 * Move a locally issued open over to an owner on the state list.
4352 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
4353 * returns with it unlocked.
4356 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4357 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
4358 struct ucred *cred, NFSPROC_T *p)
4360 struct nfsclopen *op, *nop;
4361 struct nfscldeleg *ndp;
4363 int error = 0, newone;
4366 * First, look for an appropriate open, If found, just increment the
4369 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4370 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4371 op->nfso_fhlen == lop->nfso_fhlen &&
4372 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4373 op->nfso_opencnt += lop->nfso_opencnt;
4374 nfscl_freeopen(lop, 1, true);
4379 /* No appropriate open, so we have to do one against the server. */
4381 nop = malloc(sizeof (struct nfsclopen) +
4382 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4383 nop->nfso_hash.le_prev = NULL;
4385 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4386 lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
4388 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4389 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4390 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4393 nfscl_freeopen(op, 0, true);
4395 op->nfso_mode |= lop->nfso_mode;
4396 op->nfso_opencnt += lop->nfso_opencnt;
4397 nfscl_freeopen(lop, 1, true);
4400 free(nop, M_NFSCLOPEN);
4403 * What should I do with the returned delegation, since the
4404 * delegation is being recalled? For now, just printf and
4407 printf("Moveopen returned deleg\n");
4408 free(ndp, M_NFSCLDELEG);
4414 * Recall all delegations on this client.
4417 nfscl_totalrecall(struct nfsclclient *clp)
4419 struct nfscldeleg *dp;
4421 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4422 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4423 dp->nfsdl_flags |= NFSCLDL_RECALL;
4428 * Relock byte ranges. Called for delegation recall and state expiry.
4431 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4432 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4435 struct nfscllockowner *nlp;
4439 int error, newone, donelocally;
4441 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
4444 np->n_flag |= NMIGHTBELOCKED;
4448 off = lop->nfslo_first;
4449 len = lop->nfslo_end - lop->nfslo_first;
4450 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4451 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4452 lp->nfsl_openowner, &nlp, &newone, &donelocally);
4453 if (error || donelocally)
4455 nfhp = VTONFS(vp)->n_fhp;
4456 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4457 nfhp->nfh_len, nlp, newone, 0, off,
4458 len, lop->nfslo_type, cred, p);
4460 nfscl_freelockowner(nlp, 0);
4465 * Called to re-open a file. Basically get a vnode for the file handle
4466 * and then call nfsrpc_openrpc() to do the rest.
4469 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4470 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4471 struct ucred *cred, NFSPROC_T *p)
4477 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4481 if (np->n_v4 != NULL) {
4482 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4483 np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4484 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4494 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4495 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4499 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4500 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4501 u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4502 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4507 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4508 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4510 if (error == NFSERR_DELAY)
4511 (void) nfs_catnap(PZERO, error, "nfstryop");
4512 } while (error == NFSERR_DELAY);
4513 if (error == EAUTH || error == EACCES) {
4514 /* Try again using system credentials */
4515 newnfs_setroot(cred);
4517 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4518 newfhlen, mode, op, name, namelen, ndpp, reclaim,
4519 delegtype, cred, p, 1, 0);
4520 if (error == NFSERR_DELAY)
4521 (void) nfs_catnap(PZERO, error, "nfstryop");
4522 } while (error == NFSERR_DELAY);
4528 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4529 * NFSERR_DELAY. Also, retry with system credentials, if the provided
4533 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4534 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4535 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4537 struct nfsrv_descript nfsd, *nd = &nfsd;
4541 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4542 reclaim, off, len, type, cred, p, 0);
4543 if (!error && nd->nd_repstat == NFSERR_DELAY)
4544 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4546 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4548 error = nd->nd_repstat;
4549 if (error == EAUTH || error == EACCES) {
4550 /* Try again using root credentials */
4551 newnfs_setroot(cred);
4553 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4554 newone, reclaim, off, len, type, cred, p, 1);
4555 if (!error && nd->nd_repstat == NFSERR_DELAY)
4556 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4558 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4560 error = nd->nd_repstat;
4566 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4567 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4571 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4572 struct nfsmount *nmp, NFSPROC_T *p)
4577 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4578 if (error == NFSERR_DELAY)
4579 (void) nfs_catnap(PZERO, error, "nfstrydp");
4580 } while (error == NFSERR_DELAY);
4581 if (error == EAUTH || error == EACCES) {
4582 /* Try again using system credentials */
4583 newnfs_setroot(cred);
4585 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4586 if (error == NFSERR_DELAY)
4587 (void) nfs_catnap(PZERO, error, "nfstrydp");
4588 } while (error == NFSERR_DELAY);
4594 * Try a close against the server. Just call nfsrpc_closerpc(),
4595 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4599 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4600 struct nfsmount *nmp, NFSPROC_T *p, bool loop_on_delayed)
4602 struct nfsrv_descript nfsd, *nd = &nfsd;
4606 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4607 if (loop_on_delayed && error == NFSERR_DELAY)
4608 (void) nfs_catnap(PZERO, error, "nfstrycl");
4609 } while (loop_on_delayed && error == NFSERR_DELAY);
4610 if (error == EAUTH || error == EACCES) {
4611 /* Try again using system credentials */
4612 newnfs_setroot(cred);
4614 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4615 if (loop_on_delayed && error == NFSERR_DELAY)
4616 (void) nfs_catnap(PZERO, error, "nfstrycl");
4617 } while (loop_on_delayed && error == NFSERR_DELAY);
4623 * Decide if a delegation on a file permits close without flushing writes
4624 * to the server. This might be a big performance win in some environments.
4625 * (Not useful until the client does caching on local stable storage.)
4628 nfscl_mustflush(vnode_t vp)
4630 struct nfsclclient *clp;
4631 struct nfscldeleg *dp;
4633 struct nfsmount *nmp;
4636 nmp = VFSTONFS(vp->v_mount);
4637 if (!NFSHASNFSV4(nmp))
4640 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4646 clp = nfscl_findcl(nmp);
4651 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4652 if (dp != NULL && (dp->nfsdl_flags &
4653 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4655 (dp->nfsdl_sizelimit >= np->n_size ||
4656 !NFSHASSTRICT3530(nmp))) {
4665 * See if a (write) delegation exists for this file.
4668 nfscl_nodeleg(vnode_t vp, int writedeleg)
4670 struct nfsclclient *clp;
4671 struct nfscldeleg *dp;
4673 struct nfsmount *nmp;
4676 nmp = VFSTONFS(vp->v_mount);
4677 if (!NFSHASNFSV4(nmp))
4680 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4686 clp = nfscl_findcl(nmp);
4691 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4693 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4694 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4704 * Look for an associated delegation that should be DelegReturned.
4707 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4709 struct nfsclclient *clp;
4710 struct nfscldeleg *dp;
4711 struct nfsclowner *owp;
4712 struct nfscllockowner *lp;
4713 struct nfsmount *nmp;
4717 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4719 nmp = VFSTONFS(vp->v_mount);
4720 if (NFSHASPNFS(nmp))
4723 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4729 mp = nmp->nm_mountp;
4732 * Loop around waiting for:
4733 * - outstanding I/O operations on delegations to complete
4734 * - for a delegation on vp that has state, lock the client and
4736 * - return delegation with no state
4739 clp = nfscl_findcl(nmp);
4744 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4745 np->n_fhp->nfh_len);
4748 * Wait for outstanding I/O ops to be done.
4750 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4752 nfsv4_unlock(&clp->nfsc_lock, 0);
4755 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4756 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
4758 if (NFSCL_FORCEDISM(mp)) {
4759 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
4766 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4767 if (!LIST_EMPTY(&owp->nfsow_open)) {
4773 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4774 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4780 if (needsrecall && !triedrecall) {
4781 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4784 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4785 &islept, NFSCLSTATEMUTEXPTR, mp);
4786 if (NFSCL_FORCEDISM(mp)) {
4787 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
4789 nfsv4_unlock(&clp->nfsc_lock, 0);
4799 cred = newnfs_getcred();
4800 newnfs_copycred(&dp->nfsdl_cred, cred);
4801 nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL);
4805 nfsv4_unlock(&clp->nfsc_lock, 0);
4809 *stp = dp->nfsdl_stateid;
4811 nfscl_cleandeleg(dp);
4812 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4815 nfsv4_unlock(&clp->nfsc_lock, 0);
4822 * Look for associated delegation(s) that should be DelegReturned.
4825 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4826 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4828 struct nfsclclient *clp;
4829 struct nfscldeleg *dp;
4830 struct nfsclowner *owp;
4831 struct nfscllockowner *lp;
4832 struct nfsmount *nmp;
4836 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4838 nmp = VFSTONFS(fvp->v_mount);
4841 if (NFSHASPNFS(nmp))
4844 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
4849 mp = nmp->nm_mountp;
4852 * Loop around waiting for:
4853 * - outstanding I/O operations on delegations to complete
4854 * - for a delegation on fvp that has state, lock the client and
4856 * - return delegation(s) with no state.
4859 clp = nfscl_findcl(nmp);
4865 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4866 np->n_fhp->nfh_len);
4867 if (dp != NULL && *gotfdp == 0) {
4869 * Wait for outstanding I/O ops to be done.
4871 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4873 nfsv4_unlock(&clp->nfsc_lock, 0);
4876 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4877 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
4879 if (NFSCL_FORCEDISM(mp)) {
4880 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
4889 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4890 if (!LIST_EMPTY(&owp->nfsow_open)) {
4896 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4897 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4903 if (needsrecall && !triedrecall) {
4904 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4907 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4908 &islept, NFSCLSTATEMUTEXPTR, mp);
4909 if (NFSCL_FORCEDISM(mp)) {
4910 dp->nfsdl_flags &= ~NFSCLDL_DELEGRET;
4912 nfsv4_unlock(&clp->nfsc_lock, 0);
4924 cred = newnfs_getcred();
4925 newnfs_copycred(&dp->nfsdl_cred, cred);
4926 nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL);
4930 nfsv4_unlock(&clp->nfsc_lock, 0);
4934 *fstp = dp->nfsdl_stateid;
4937 nfscl_cleandeleg(dp);
4938 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4941 nfsv4_unlock(&clp->nfsc_lock, 0);
4946 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4947 np->n_fhp->nfh_len);
4948 if (dp != NULL && *gottdp == 0) {
4950 * Wait for outstanding I/O ops to be done.
4952 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4953 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4954 msleep(&dp->nfsdl_rwlock, NFSCLSTATEMUTEXPTR, PZERO,
4956 if (NFSCL_FORCEDISM(mp)) {
4964 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4965 if (!LIST_EMPTY(&owp->nfsow_open)) {
4970 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4971 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4976 *tstp = dp->nfsdl_stateid;
4979 nfscl_cleandeleg(dp);
4980 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4989 * Get a reference on the clientid associated with the mount point.
4990 * Return 1 if success, 0 otherwise.
4993 nfscl_getref(struct nfsmount *nmp)
4995 struct nfsclclient *clp;
4999 clp = nfscl_findcl(nmp);
5004 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, nmp->nm_mountp);
5006 if (NFSCL_FORCEDISM(nmp->nm_mountp))
5013 * Release a reference on a clientid acquired with the above call.
5016 nfscl_relref(struct nfsmount *nmp)
5018 struct nfsclclient *clp;
5021 clp = nfscl_findcl(nmp);
5026 nfsv4_relref(&clp->nfsc_lock);
5031 * Save the size attribute in the delegation, since the nfsnode
5035 nfscl_reclaimnode(vnode_t vp)
5037 struct nfsclclient *clp;
5038 struct nfscldeleg *dp;
5039 struct nfsnode *np = VTONFS(vp);
5040 struct nfsmount *nmp;
5042 nmp = VFSTONFS(vp->v_mount);
5043 if (!NFSHASNFSV4(nmp))
5046 clp = nfscl_findcl(nmp);
5051 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5052 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
5053 dp->nfsdl_size = np->n_size;
5058 * Get the saved size attribute in the delegation, since it is a
5059 * newly allocated nfsnode.
5062 nfscl_newnode(vnode_t vp)
5064 struct nfsclclient *clp;
5065 struct nfscldeleg *dp;
5066 struct nfsnode *np = VTONFS(vp);
5067 struct nfsmount *nmp;
5069 nmp = VFSTONFS(vp->v_mount);
5070 if (!NFSHASNFSV4(nmp))
5073 clp = nfscl_findcl(nmp);
5078 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5079 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
5080 np->n_size = dp->nfsdl_size;
5085 * If there is a valid write delegation for this file, set the modtime
5086 * to the local clock time.
5089 nfscl_delegmodtime(vnode_t vp)
5091 struct nfsclclient *clp;
5092 struct nfscldeleg *dp;
5093 struct nfsnode *np = VTONFS(vp);
5094 struct nfsmount *nmp;
5096 nmp = VFSTONFS(vp->v_mount);
5097 if (!NFSHASNFSV4(nmp))
5100 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
5106 clp = nfscl_findcl(nmp);
5111 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5112 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
5113 nanotime(&dp->nfsdl_modtime);
5114 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
5120 * If there is a valid write delegation for this file with a modtime set,
5121 * put that modtime in mtime.
5124 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
5126 struct nfsclclient *clp;
5127 struct nfscldeleg *dp;
5128 struct nfsnode *np = VTONFS(vp);
5129 struct nfsmount *nmp;
5131 nmp = VFSTONFS(vp->v_mount);
5132 if (!NFSHASNFSV4(nmp))
5135 if ((nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0) {
5141 clp = nfscl_findcl(nmp);
5146 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5148 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
5149 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
5150 *mtime = dp->nfsdl_modtime;
5155 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
5157 short *defaulterrp, *errp;
5159 if (!nd->nd_repstat)
5161 if (nd->nd_procnum == NFSPROC_NOOP)
5162 return (txdr_unsigned(nd->nd_repstat & 0xffff));
5163 if (nd->nd_repstat == EBADRPC)
5164 return (txdr_unsigned(NFSERR_BADXDR));
5165 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
5166 nd->nd_repstat == NFSERR_OPILLEGAL)
5167 return (txdr_unsigned(nd->nd_repstat));
5168 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
5169 minorvers > NFSV4_MINORVERSION) {
5170 /* NFSv4.n error. */
5171 return (txdr_unsigned(nd->nd_repstat));
5173 if (nd->nd_procnum < NFSV4OP_CBNOPS)
5174 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
5176 return (txdr_unsigned(nd->nd_repstat));
5178 if (*errp == (short)nd->nd_repstat)
5179 return (txdr_unsigned(nd->nd_repstat));
5180 return (txdr_unsigned(*defaulterrp));
5184 * Called to find/add a layout to a client.
5185 * This function returns the layout with a refcnt (shared lock) upon
5186 * success (returns 0) or with no lock/refcnt on the layout when an
5187 * error is returned.
5188 * If a layout is passed in via lypp, it is locked (exclusively locked).
5191 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
5192 nfsv4stateid_t *stateidp, int layouttype, int retonclose,
5193 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
5194 struct ucred *cred, NFSPROC_T *p)
5196 struct nfsclclient *clp;
5197 struct nfscllayout *lyp, *tlyp;
5198 struct nfsclflayout *flp;
5199 struct nfsnode *np = VTONFS(vp);
5201 int layout_passed_in;
5203 mp = nmp->nm_mountp;
5204 layout_passed_in = 1;
5208 layout_passed_in = 0;
5209 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
5216 if (layout_passed_in != 0)
5217 nfsv4_unlock(&lyp->nfsly_lock, 0);
5220 free(tlyp, M_NFSLAYOUT);
5225 * Although no lyp was passed in, another thread might have
5226 * allocated one. If one is found, just increment it's ref
5227 * count and return it.
5229 lyp = nfscl_findlayout(clp, fhp, fhlen);
5233 lyp->nfsly_stateid.seqid = stateidp->seqid;
5234 lyp->nfsly_stateid.other[0] = stateidp->other[0];
5235 lyp->nfsly_stateid.other[1] = stateidp->other[1];
5236 lyp->nfsly_stateid.other[2] = stateidp->other[2];
5237 lyp->nfsly_lastbyte = 0;
5238 LIST_INIT(&lyp->nfsly_flayread);
5239 LIST_INIT(&lyp->nfsly_flayrw);
5240 LIST_INIT(&lyp->nfsly_recall);
5241 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
5242 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
5243 lyp->nfsly_clp = clp;
5244 if (layouttype == NFSLAYOUT_FLEXFILE)
5245 lyp->nfsly_flags = NFSLY_FLEXFILE;
5247 lyp->nfsly_flags = NFSLY_FILES;
5248 if (retonclose != 0)
5249 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
5250 lyp->nfsly_fhlen = fhlen;
5251 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
5252 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5253 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
5255 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5257 nfsstatsv1.cllayouts++;
5259 if (retonclose != 0)
5260 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
5261 if (stateidp->seqid > lyp->nfsly_stateid.seqid)
5262 lyp->nfsly_stateid.seqid = stateidp->seqid;
5263 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5264 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5265 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5267 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5268 if (NFSCL_FORCEDISM(mp)) {
5271 free(tlyp, M_NFSLAYOUT);
5275 } else if (stateidp->seqid > lyp->nfsly_stateid.seqid)
5276 lyp->nfsly_stateid.seqid = stateidp->seqid;
5278 /* Merge the new list of File Layouts into the list. */
5279 flp = LIST_FIRST(fhlp);
5281 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
5282 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
5284 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
5286 if (layout_passed_in != 0)
5287 nfsv4_unlock(&lyp->nfsly_lock, 1);
5290 free(tlyp, M_NFSLAYOUT);
5295 * Search for a layout by MDS file handle.
5296 * If one is found, it is returned with a refcnt (shared lock) iff
5297 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
5300 struct nfscllayout *
5301 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
5302 uint64_t off, uint32_t rwaccess, struct nfsclflayout **retflpp,
5305 struct nfscllayout *lyp;
5307 int error, igotlock;
5309 mp = clp->nfsc_nmp->nm_mountp;
5313 lyp = nfscl_findlayout(clp, fhp, fhlen);
5315 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5316 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5317 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5318 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5319 error = nfscl_findlayoutforio(lyp, off, rwaccess,
5322 nfsv4_getref(&lyp->nfsly_lock, NULL,
5323 NFSCLSTATEMUTEXPTR, mp);
5326 igotlock = nfsv4_lock(&lyp->nfsly_lock,
5327 1, NULL, NFSCLSTATEMUTEXPTR, mp);
5328 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
5331 if (NFSCL_FORCEDISM(mp)) {
5345 * Search for a layout by MDS file handle. If one is found, mark in to be
5346 * recalled, if it already marked "return on close".
5349 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
5350 int fhlen, struct nfsclrecalllayout **recallpp, struct nfscllayout **lypp)
5352 struct nfscllayout *lyp;
5356 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) ||
5357 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5358 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
5360 lyp = nfscl_findlayout(clp, fhp, fhlen);
5361 if (lyp != NULL && (lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) {
5362 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5364 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5365 iomode |= NFSLAYOUTIOMODE_READ;
5366 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5367 iomode |= NFSLAYOUTIOMODE_RW;
5368 nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5369 0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
5371 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
5375 /* Now, wake up renew thread to do LayoutReturn. */
5382 * Mark the layout to be recalled and with an error.
5383 * Also, disable the dsp from further use.
5386 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
5387 struct nfscllayout *lyp, struct nfsclds *dsp)
5389 struct nfsclrecalllayout *recallp;
5392 printf("DS being disabled, error=%d\n", stat);
5393 /* Set up the return of the layout. */
5394 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
5397 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5398 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5399 iomode |= NFSLAYOUTIOMODE_READ;
5400 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5401 iomode |= NFSLAYOUTIOMODE_RW;
5402 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5403 0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
5404 dp->nfsdi_deviceid, recallp);
5406 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
5409 free(recallp, M_NFSLAYRECALL);
5412 /* And shut the TCP connection down. */
5413 nfscl_cancelreqs(dsp);
5417 * Cancel all RPCs for this "dsp" by closing the connection.
5418 * Also, mark the session as defunct.
5419 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
5420 * cannot be shut down.
5423 nfscl_cancelreqs(struct nfsclds *dsp)
5425 struct __rpc_client *cl;
5426 static int non_event;
5429 if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
5430 dsp->nfsclds_sockp != NULL &&
5431 dsp->nfsclds_sockp->nr_client != NULL) {
5432 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
5433 cl = dsp->nfsclds_sockp->nr_client;
5434 dsp->nfsclds_sess.nfsess_defunct = 1;
5438 * This 1sec sleep is done to reduce the number of reconnect
5439 * attempts made on the DS while it has failed.
5441 tsleep(&non_event, PVFS, "ndscls", hz);
5448 * Dereference a layout.
5451 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
5456 nfsv4_unlock(&lyp->nfsly_lock, 0);
5458 nfsv4_relref(&lyp->nfsly_lock);
5463 * Search for a devinfo by deviceid. If one is found, return it after
5464 * acquiring a reference count on it.
5466 struct nfscldevinfo *
5467 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
5468 struct nfscldevinfo *dip)
5473 dip = nfscl_finddevinfo(clp, deviceid);
5475 dip->nfsdi_refcnt++;
5481 * Dereference a devinfo structure.
5484 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
5487 dip->nfsdi_refcnt--;
5488 if (dip->nfsdi_refcnt == 0)
5489 wakeup(&dip->nfsdi_refcnt);
5493 * Dereference a devinfo structure.
5496 nfscl_reldevinfo(struct nfscldevinfo *dip)
5500 nfscl_reldevinfo_locked(dip);
5505 * Find a layout for this file handle. Return NULL upon failure.
5507 static struct nfscllayout *
5508 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
5510 struct nfscllayout *lyp;
5512 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
5513 if (lyp->nfsly_fhlen == fhlen &&
5514 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
5520 * Find a devinfo for this deviceid. Return NULL upon failure.
5522 static struct nfscldevinfo *
5523 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
5525 struct nfscldevinfo *dip;
5527 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
5528 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5535 * Merge the new file layout list into the main one, maintaining it in
5536 * increasing offset order.
5539 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5540 struct nfsclflayouthead *newfhlp)
5542 struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5544 flp = LIST_FIRST(fhlp);
5546 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5547 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5549 flp = LIST_NEXT(flp, nfsfl_list);
5551 if (prevflp == NULL)
5552 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5554 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5560 * Add this nfscldevinfo to the client, if it doesn't already exist.
5561 * This function consumes the structure pointed at by dip, if not NULL.
5564 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
5565 struct nfsclflayout *flp)
5567 struct nfsclclient *clp;
5568 struct nfscldevinfo *tdip;
5576 free(dip, M_NFSDEVINFO);
5579 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5580 dev = flp->nfsfl_dev;
5582 dev = flp->nfsfl_ffm[ind].dev;
5583 tdip = nfscl_finddevinfo(clp, dev);
5585 tdip->nfsdi_layoutrefs++;
5586 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5587 flp->nfsfl_devp = tdip;
5589 flp->nfsfl_ffm[ind].devp = tdip;
5590 nfscl_reldevinfo_locked(tdip);
5593 free(dip, M_NFSDEVINFO);
5597 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5598 dip->nfsdi_layoutrefs = 1;
5599 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5600 flp->nfsfl_devp = dip;
5602 flp->nfsfl_ffm[ind].devp = dip;
5611 * Free up a layout structure and associated file layout structure(s).
5614 nfscl_freelayout(struct nfscllayout *layp)
5616 struct nfsclflayout *flp, *nflp;
5617 struct nfsclrecalllayout *rp, *nrp;
5619 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5620 LIST_REMOVE(flp, nfsfl_list);
5621 nfscl_freeflayout(flp);
5623 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5624 LIST_REMOVE(flp, nfsfl_list);
5625 nfscl_freeflayout(flp);
5627 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5628 LIST_REMOVE(rp, nfsrecly_list);
5629 free(rp, M_NFSLAYRECALL);
5632 nfsstatsv1.cllayouts--;
5633 free(layp, M_NFSLAYOUT);
5637 * Free up a file layout structure.
5640 nfscl_freeflayout(struct nfsclflayout *flp)
5644 if ((flp->nfsfl_flags & NFSFL_FILE) != 0) {
5645 for (i = 0; i < flp->nfsfl_fhcnt; i++)
5646 free(flp->nfsfl_fh[i], M_NFSFH);
5647 if (flp->nfsfl_devp != NULL)
5648 flp->nfsfl_devp->nfsdi_layoutrefs--;
5650 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
5651 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) {
5652 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
5653 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
5654 if (flp->nfsfl_ffm[i].devp != NULL)
5655 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--;
5657 free(flp, M_NFSFLAYOUT);
5661 * Free up a file layout devinfo structure.
5664 nfscl_freedevinfo(struct nfscldevinfo *dip)
5667 free(dip, M_NFSDEVINFO);
5671 * Mark any layouts that match as recalled.
5674 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5675 uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op,
5676 char *devid, struct nfsclrecalllayout *recallp)
5678 struct nfsclrecalllayout *rp, *orp;
5680 recallp->nfsrecly_recalltype = recalltype;
5681 recallp->nfsrecly_iomode = iomode;
5682 recallp->nfsrecly_stateseqid = stateseqid;
5683 recallp->nfsrecly_off = off;
5684 recallp->nfsrecly_len = len;
5685 recallp->nfsrecly_stat = stat;
5686 recallp->nfsrecly_op = op;
5688 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID);
5690 * Order the list as file returns first, followed by fsid and any
5691 * returns, both in increasing stateseqid order.
5692 * Note that the seqids wrap around, so 1 is after 0xffffffff.
5693 * (I'm not sure this is correct because I find RFC5661 confusing
5694 * on this, but hopefully it will work ok.)
5697 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5699 if ((recalltype == NFSLAYOUTRETURN_FILE &&
5700 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5701 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5702 (recalltype != NFSLAYOUTRETURN_FILE &&
5703 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5704 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5705 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5710 * Put any error return on all the file returns that will
5713 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE &&
5714 stat != 0 && rp->nfsrecly_stat == 0) {
5715 rp->nfsrecly_stat = stat;
5716 rp->nfsrecly_op = op;
5718 NFSBCOPY(devid, rp->nfsrecly_devid,
5724 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5727 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5729 lyp->nfsly_flags |= NFSLY_RECALL;
5730 wakeup(lyp->nfsly_clp);
5735 * Compare the two seqids for ordering. The trick is that the seqids can
5736 * wrap around from 0xffffffff->0, so check for the cases where one
5737 * has wrapped around.
5738 * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5741 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5744 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5745 /* seqid2 has wrapped around. */
5747 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5748 /* seqid1 has wrapped around. */
5750 if (seqid1 <= seqid2)
5756 * Do a layout return for each of the recalls.
5759 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5760 struct ucred *cred, NFSPROC_T *p)
5762 struct nfsclrecalllayout *rp;
5763 nfsv4stateid_t stateid;
5766 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5767 stateid.seqid = lyp->nfsly_stateid.seqid;
5768 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5769 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5771 layouttype = NFSLAYOUT_FLEXFILE;
5772 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5773 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5774 lyp->nfsly_fhlen, 0, layouttype,
5775 rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5776 rp->nfsrecly_off, rp->nfsrecly_len,
5777 &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op,
5778 rp->nfsrecly_devid);
5783 * Do the layout commit for a file layout.
5786 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5787 struct ucred *cred, NFSPROC_T *p)
5789 struct nfsclflayout *flp;
5791 int error, layouttype;
5793 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5794 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5796 layouttype = NFSLAYOUT_FLEXFILE;
5797 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5798 if (layouttype == NFSLAYOUT_FLEXFILE &&
5799 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
5800 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
5801 /* If not supported, don't bother doing it. */
5803 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5806 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5807 len = flp->nfsfl_end - flp->nfsfl_off;
5808 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5809 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5810 lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5811 layouttype, cred, p);
5812 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5813 if (error == NFSERR_NOTSUPP) {
5814 /* If not supported, don't bother doing it. */
5816 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5825 * Commit all layouts for a file (vnode).
5828 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5830 struct nfsclclient *clp;
5831 struct nfscllayout *lyp;
5832 struct nfsnode *np = VTONFS(vp);
5834 struct nfsmount *nmp;
5838 if (NFSHASNOLAYOUTCOMMIT(nmp))
5846 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5851 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5852 if (NFSCL_FORCEDISM(mp)) {
5857 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5858 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5860 NFSCL_DEBUG(4, "do layoutcommit2\n");
5861 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5865 nfsv4_relref(&lyp->nfsly_lock);