2 * Copyright (c) 2009 Rick Macklem, University of Guelph
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * These functions implement the client side state handling for NFSv4.
33 * NFSv4 state handling:
34 * - A lockowner is used to determine lock contention, so it
35 * corresponds directly to a Posix pid. (1 to 1 mapping)
36 * - The correct granularity of an OpenOwner is not nearly so
37 * obvious. An OpenOwner does the following:
38 * - provides a serial sequencing of Open/Close/Lock-with-new-lockowner
39 * - is used to check for Open/Share contention (not applicable to
40 * this client, since all Opens are Deny_None)
41 * As such, I considered both extreme.
42 * 1 OpenOwner per ClientID - Simple to manage, but fully serializes
43 * all Open, Close and Lock (with a new lockowner) Ops.
44 * 1 OpenOwner for each Open - This one results in an OpenConfirm for
45 * every Open, for most servers.
46 * So, I chose to use the same mapping as I did for LockOwnwers.
47 * The main concern here is that you can end up with multiple Opens
48 * for the same File Handle, but on different OpenOwners (opens
49 * inherited from parents, grandparents...) and you do not know
50 * which of these the vnodeop close applies to. This is handled by
51 * delaying the Close Op(s) until all of the Opens have been closed.
52 * (It is not yet obvious if this is the correct granularity.)
53 * - How the code handles serialization:
54 * - For the ClientId, it uses an exclusive lock while getting its
55 * SetClientId and during recovery. Otherwise, it uses a shared
56 * lock via a reference count.
57 * - For the rest of the data structures, it uses an SMP mutex
58 * (once the nfs client is SMP safe) and doesn't sleep while
59 * manipulating the linked lists.
60 * - The serialization of Open/Close/Lock/LockU falls out in the
61 * "wash", since OpenOwners and LockOwners are both mapped from
62 * Posix pid. In other words, there is only one Posix pid using
63 * any given owner, so that owner is serialized. (If you change
64 * the granularity of the OpenOwner, then code must be added to
65 * serialize Ops on the OpenOwner.)
66 * - When to get rid of OpenOwners and LockOwners.
67 * - The function nfscl_cleanup_common() is executed after a process exits.
68 * It goes through the client list looking for all Open and Lock Owners.
69 * When one is found, it is marked "defunct" or in the case of
70 * an OpenOwner without any Opens, freed.
71 * The renew thread scans for defunct Owners and gets rid of them,
72 * if it can. The LockOwners will also be deleted when the
73 * associated Open is closed.
74 * - If the LockU or Close Op(s) fail during close in a way
75 * that could be recovered upon retry, they are relinked to the
76 * ClientId's defunct open list and retried by the renew thread
77 * until they succeed or an unmount/recovery occurs.
78 * (Since we are done with them, they do not need to be recovered.)
82 #include <fs/nfs/nfsport.h>
87 extern struct nfsstatsv1 nfsstatsv1;
88 extern struct nfsreqhead nfsd_reqq;
89 extern u_int32_t newnfs_false, newnfs_true;
90 extern int nfscl_debuglevel;
91 extern int nfscl_enablecallb;
92 extern int nfs_numnfscbd;
96 struct nfsclhead nfsclhead; /* Head of clientid list */
97 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
98 int nfscl_layouthighwater = NFSCLLAYOUTHIGHWATER;
99 #endif /* !APPLEKEXT */
101 static int nfscl_delegcnt = 0;
102 static int nfscl_layoutcnt = 0;
103 static int nfscl_getopen(struct nfsclownerhead *, u_int8_t *, int, u_int8_t *,
104 u_int8_t *, u_int32_t, struct nfscllockowner **, struct nfsclopen **);
105 static void nfscl_clrelease(struct nfsclclient *);
106 static void nfscl_cleanclient(struct nfsclclient *);
107 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
108 struct ucred *, NFSPROC_T *);
109 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
110 struct nfsmount *, struct ucred *, NFSPROC_T *);
111 static void nfscl_recover(struct nfsclclient *, struct ucred *, NFSPROC_T *);
112 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
113 struct nfscllock *, int);
114 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
115 struct nfscllock **, int);
116 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *);
117 static u_int32_t nfscl_nextcbident(void);
118 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
119 static struct nfsclclient *nfscl_getclnt(u_int32_t);
120 static struct nfsclclient *nfscl_getclntsess(uint8_t *);
121 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
123 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
124 int, struct nfsclrecalllayout **);
125 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
126 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
128 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
129 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
130 u_int8_t *, struct nfscllock **);
131 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
132 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
133 struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
134 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
135 struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
136 struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *);
137 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
138 struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
139 struct nfscldeleg *, struct ucred *, NFSPROC_T *);
140 static void nfscl_totalrecall(struct nfsclclient *);
141 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
142 struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
143 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
144 u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
145 struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
146 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
147 int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
148 struct ucred *, NFSPROC_T *);
149 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
150 struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
151 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *);
152 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
153 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
154 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
155 struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int);
156 static void nfscl_freeopenowner(struct nfsclowner *, int);
157 static void nfscl_cleandeleg(struct nfscldeleg *);
158 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
159 struct nfsmount *, NFSPROC_T *);
160 static void nfscl_emptylockowner(struct nfscllockowner *,
161 struct nfscllockownerfhhead *);
162 static void nfscl_mergeflayouts(struct nfsclflayouthead *,
163 struct nfsclflayouthead *);
164 static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t,
165 uint64_t, uint32_t, struct nfsclrecalllayout *);
166 static int nfscl_seq(uint32_t, uint32_t);
167 static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *,
168 struct ucred *, NFSPROC_T *);
169 static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *,
170 struct ucred *, NFSPROC_T *);
172 static short nfscberr_null[] = {
177 static short nfscberr_getattr[] = {
186 static short nfscberr_recall[] = {
196 static short *nfscl_cberrmap[] = {
204 #define NETFAMILY(clp) \
205 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
208 * Called for an open operation.
209 * If the nfhp argument is NULL, just get an openowner.
212 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
213 struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
214 struct nfsclopen **opp, int *newonep, int *retp, int lockit)
216 struct nfsclclient *clp;
217 struct nfsclowner *owp, *nowp;
218 struct nfsclopen *op = NULL, *nop = NULL;
219 struct nfscldeleg *dp;
220 struct nfsclownerhead *ohp;
221 u_int8_t own[NFSV4CL_LOCKNAMELEN];
232 * Might need one or both of these, so MALLOC them now, to
233 * avoid a tsleep() in MALLOC later.
235 MALLOC(nowp, struct nfsclowner *, sizeof (struct nfsclowner),
236 M_NFSCLOWNER, M_WAITOK);
238 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
239 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
240 ret = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
242 FREE((caddr_t)nowp, M_NFSCLOWNER);
244 FREE((caddr_t)nop, M_NFSCLOPEN);
249 * Get the Open iff it already exists.
250 * If none found, add the new one or return error, depending upon
255 /* First check the delegation list */
256 if (nfhp != NULL && usedeleg) {
257 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
258 if (dp->nfsdl_fhlen == fhlen &&
259 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
260 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
261 (dp->nfsdl_flags & NFSCLDL_WRITE))
270 nfscl_filllockowner(p->td_proc, own, F_POSIX);
271 ohp = &dp->nfsdl_owner;
273 /* For NFSv4.1 and this option, use a single open_owner. */
274 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
275 nfscl_filllockowner(NULL, own, F_POSIX);
277 nfscl_filllockowner(p->td_proc, own, F_POSIX);
278 ohp = &clp->nfsc_owner;
280 /* Now, search for an openowner */
281 LIST_FOREACH(owp, ohp, nfsow_list) {
282 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
287 * Create a new open, as required.
289 nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
293 * Now, check the mode on the open and return the appropriate
297 if (nfhp != NULL && dp != NULL && nop == NULL)
298 /* new local open on delegation */
299 *retp = NFSCLOPEN_SETCRED;
301 *retp = NFSCLOPEN_OK;
303 if (op != NULL && (amode & ~(op->nfso_mode))) {
304 op->nfso_mode |= amode;
305 if (retp != NULL && dp == NULL)
306 *retp = NFSCLOPEN_DOOPEN;
310 * Serialize modifications to the open owner for multiple threads
311 * within the same process using a read/write sleep lock.
312 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
313 * by acquiring a shared lock. The close operations still use an
314 * exclusive lock for this case.
317 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp)))) {
319 * Get a shared lock on the OpenOwner, but first
320 * wait for any pending exclusive lock, so that the
321 * exclusive locker gets priority.
323 nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
324 NFSCLSTATEMUTEXPTR, NULL);
325 nfsv4_getref(&owp->nfsow_rwlock, NULL,
326 NFSCLSTATEMUTEXPTR, NULL);
328 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
332 FREE((caddr_t)nowp, M_NFSCLOWNER);
334 FREE((caddr_t)nop, M_NFSCLOPEN);
343 * Create a new open, as required.
346 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
347 struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
348 struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
349 struct ucred *cred, int *newonep)
351 struct nfsclowner *owp = *owpp, *nowp;
352 struct nfsclopen *op, *nop;
362 if (owp == NULL && nowp != NULL) {
363 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
364 LIST_INIT(&nowp->nfsow_open);
365 nowp->nfsow_clp = clp;
366 nowp->nfsow_seqid = 0;
367 nowp->nfsow_defunct = 0;
368 nfscl_lockinit(&nowp->nfsow_rwlock);
370 nfsstatsv1.cllocalopenowners++;
371 LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
373 nfsstatsv1.clopenowners++;
374 LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
382 /* If an fhp has been specified, create an Open as well. */
384 /* and look for the correct open, based upon FH */
385 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
386 if (op->nfso_fhlen == fhlen &&
387 !NFSBCMP(op->nfso_fh, fhp, fhlen))
390 if (op == NULL && nop != NULL) {
393 nop->nfso_opencnt = 0;
394 nop->nfso_posixlock = 1;
395 nop->nfso_fhlen = fhlen;
396 NFSBCOPY(fhp, nop->nfso_fh, fhlen);
397 LIST_INIT(&nop->nfso_lock);
398 nop->nfso_stateid.seqid = 0;
399 nop->nfso_stateid.other[0] = 0;
400 nop->nfso_stateid.other[1] = 0;
401 nop->nfso_stateid.other[2] = 0;
402 KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
403 newnfs_copyincred(cred, &nop->nfso_cred);
405 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
406 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
408 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
409 nfsstatsv1.cllocalopens++;
411 nfsstatsv1.clopens++;
413 LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
425 * Called to find/add a delegation to a client.
428 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
429 int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
431 struct nfscldeleg *dp = *dpp, *tdp;
434 * First, if we have received a Read delegation for a file on a
435 * read/write file system, just return it, because they aren't
438 if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
439 (dp->nfsdl_flags & NFSCLDL_READ)) {
440 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
441 FREE((caddr_t)dp, M_NFSCLDELEG);
446 /* Look for the correct deleg, based upon FH */
448 tdp = nfscl_finddeleg(clp, nfhp, fhlen);
452 return (NFSERR_BADSTATEID);
455 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
456 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
458 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
459 nfsstatsv1.cldelegates++;
463 * Delegation already exists, what do we do if a new one??
466 printf("Deleg already exists!\n");
467 FREE((caddr_t)dp, M_NFSCLDELEG);
478 * Find a delegation for this file handle. Return NULL upon failure.
480 static struct nfscldeleg *
481 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
483 struct nfscldeleg *dp;
485 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
486 if (dp->nfsdl_fhlen == fhlen &&
487 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
494 * Get a stateid for an I/O operation. First, look for an open and iff
495 * found, return either a lockowner stateid or the open stateid.
496 * If no Open is found, just return error and the special stateid of all zeros.
499 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
500 int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
503 struct nfsclclient *clp;
504 struct nfsclowner *owp;
505 struct nfsclopen *op = NULL, *top;
506 struct nfscllockowner *lp;
507 struct nfscldeleg *dp;
509 struct nfsmount *nmp;
510 u_int8_t own[NFSV4CL_LOCKNAMELEN];
515 * Initially, just set the special stateid of all zeros.
516 * (Don't do this for a DS, since the special stateid can't be used.)
520 stateidp->other[0] = 0;
521 stateidp->other[1] = 0;
522 stateidp->other[2] = 0;
524 if (vnode_vtype(vp) != VREG)
527 nmp = VFSTONFS(vnode_mount(vp));
529 clp = nfscl_findcl(nmp);
536 * Wait for recovery to complete.
538 while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
539 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
540 PZERO, "nfsrecvr", NULL);
543 * First, look for a delegation.
545 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
546 if (dp->nfsdl_fhlen == fhlen &&
547 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
548 if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
549 (dp->nfsdl_flags & NFSCLDL_WRITE)) {
550 stateidp->seqid = dp->nfsdl_stateid.seqid;
551 stateidp->other[0] = dp->nfsdl_stateid.other[0];
552 stateidp->other[1] = dp->nfsdl_stateid.other[1];
553 stateidp->other[2] = dp->nfsdl_stateid.other[2];
554 if (!(np->n_flag & NDELEGRECALL)) {
555 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
557 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
559 dp->nfsdl_timestamp = NFSD_MONOSEC +
561 dp->nfsdl_rwlock.nfslock_usecnt++;
562 *lckpp = (void *)&dp->nfsdl_rwlock;
573 * If p != NULL, we want to search the parentage tree
574 * for a matching OpenOwner and use that.
576 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
577 nfscl_filllockowner(NULL, own, F_POSIX);
579 nfscl_filllockowner(p->td_proc, own, F_POSIX);
581 error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, own, own,
583 if (error == 0 && lp != NULL && fords == 0) {
584 /* Don't return a lock stateid for a DS. */
586 lp->nfsl_stateid.seqid;
588 lp->nfsl_stateid.other[0];
590 lp->nfsl_stateid.other[1];
592 lp->nfsl_stateid.other[2];
598 /* If not found, just look for any OpenOwner that will work. */
601 owp = LIST_FIRST(&clp->nfsc_owner);
602 while (!done && owp != NULL) {
603 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
604 if (op->nfso_fhlen == fhlen &&
605 !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
606 if (top == NULL && (op->nfso_mode &
607 NFSV4OPEN_ACCESSWRITE) != 0 &&
608 (mode & NFSV4OPEN_ACCESSREAD) != 0)
610 if ((mode & op->nfso_mode) == mode) {
617 owp = LIST_NEXT(owp, nfsow_list);
620 NFSCL_DEBUG(2, "openmode top=%p\n", top);
621 if (top == NULL || NFSHASOPENMODE(nmp)) {
628 * For read aheads or write behinds, use the open cred.
629 * A read ahead or write behind is indicated by p == NULL.
632 newnfs_copycred(&op->nfso_cred, cred);
636 * No lock stateid, so return the open stateid.
638 stateidp->seqid = op->nfso_stateid.seqid;
639 stateidp->other[0] = op->nfso_stateid.other[0];
640 stateidp->other[1] = op->nfso_stateid.other[1];
641 stateidp->other[2] = op->nfso_stateid.other[2];
647 * Search for a matching file, mode and, optionally, lockowner.
650 nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
651 u_int8_t *openown, u_int8_t *lockown, u_int32_t mode,
652 struct nfscllockowner **lpp, struct nfsclopen **opp)
654 struct nfsclowner *owp;
655 struct nfsclopen *op, *rop, *rop2;
656 struct nfscllockowner *lp;
662 * rop will be set to the open to be returned. There are three
663 * variants of this, all for an open of the correct file:
664 * 1 - A match of lockown.
665 * 2 - A match of the openown, when no lockown match exists.
666 * 3 - A match for any open, if no openown or lockown match exists.
667 * Looking for #2 over #3 probably isn't necessary, but since
668 * RFC3530 is vague w.r.t. the relationship between openowners and
669 * lockowners, I think this is the safer way to go.
674 /* Search the client list */
675 owp = LIST_FIRST(ohp);
676 while (owp != NULL && keep_looping != 0) {
677 /* and look for the correct open */
678 op = LIST_FIRST(&owp->nfsow_open);
679 while (op != NULL && keep_looping != 0) {
680 if (op->nfso_fhlen == fhlen &&
681 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
682 && (op->nfso_mode & mode) == mode) {
684 /* Now look for a matching lockowner. */
685 LIST_FOREACH(lp, &op->nfso_lock,
687 if (!NFSBCMP(lp->nfsl_owner,
689 NFSV4CL_LOCKNAMELEN)) {
697 if (rop == NULL && !NFSBCMP(owp->nfsow_owner,
698 openown, NFSV4CL_LOCKNAMELEN)) {
706 op = LIST_NEXT(op, nfso_list);
708 owp = LIST_NEXT(owp, nfsow_list);
719 * Release use of an open owner. Called when open operations are done
720 * with the open owner.
723 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
724 __unused int error, __unused int candelete, int unlocked)
731 if (NFSHASONEOPENOWN(nmp))
732 nfsv4_relref(&owp->nfsow_rwlock);
734 nfscl_lockunlock(&owp->nfsow_rwlock);
736 nfscl_clrelease(owp->nfsow_clp);
741 * Release use of an open structure under an open owner.
744 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
747 struct nfsclclient *clp;
748 struct nfsclowner *owp;
754 if (NFSHASONEOPENOWN(nmp))
755 nfsv4_relref(&owp->nfsow_rwlock);
757 nfscl_lockunlock(&owp->nfsow_rwlock);
758 clp = owp->nfsow_clp;
759 if (error && candelete && op->nfso_opencnt == 0)
760 nfscl_freeopen(op, 0);
761 nfscl_clrelease(clp);
766 * Called to get a clientid structure. It will optionally lock the
767 * client data structures to do the SetClientId/SetClientId_confirm,
768 * but will release that lock and return the clientid with a reference
770 * If the "cred" argument is NULL, a new clientid should not be created.
771 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
773 * The start_renewthread argument tells nfscl_getcl() to start a renew
774 * thread if this creates a new clp.
775 * It always clpp with a reference count on it, unless returning an error.
778 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
779 int start_renewthread, struct nfsclclient **clpp)
781 struct nfsclclient *clp;
782 struct nfsclclient *newclp = NULL;
783 struct nfsmount *nmp;
784 char uuid[HOSTUUIDLEN];
785 int igotlock = 0, error, trystalecnt, clidinusedelay, i;
790 getcredhostuuid(cred, uuid, sizeof uuid);
791 idlen = strlen(uuid);
793 idlen += sizeof (u_int64_t);
795 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
796 MALLOC(newclp, struct nfsclclient *,
797 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
802 * If a forced dismount is already in progress, don't
803 * allocate a new clientid and get out now. For the case where
804 * clp != NULL, this is a harmless optimization.
806 if (NFSCL_FORCEDISM(mp)) {
809 free(newclp, M_NFSCLCLIENT);
814 if (newclp == NULL) {
819 clp->nfsc_idlen = idlen;
820 LIST_INIT(&clp->nfsc_owner);
821 TAILQ_INIT(&clp->nfsc_deleg);
822 TAILQ_INIT(&clp->nfsc_layout);
823 LIST_INIT(&clp->nfsc_devinfo);
824 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
825 LIST_INIT(&clp->nfsc_deleghash[i]);
826 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
827 LIST_INIT(&clp->nfsc_layouthash[i]);
828 clp->nfsc_flags = NFSCLFLAGS_INITED;
829 clp->nfsc_clientidrev = 1;
830 clp->nfsc_cbident = nfscl_nextcbident();
831 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
833 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
837 if (start_renewthread != 0)
838 nfscl_start_renewthread(clp);
842 free(newclp, M_NFSCLCLIENT);
845 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
846 !NFSCL_FORCEDISM(mp))
847 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
848 NFSCLSTATEMUTEXPTR, mp);
851 * Call nfsv4_lock() with "iwantlock == 0" so that it will
852 * wait for a pending exclusive lock request. This gives the
853 * exclusive lock request priority over this shared lock
855 * An exclusive lock on nfsc_lock is used mainly for server
858 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, mp);
859 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
861 if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
863 * Both nfsv4_lock() and nfsv4_getref() know to check
864 * for NFSCL_FORCEDISM() and return without sleeping to
865 * wait for the exclusive lock to be released, since it
866 * might be held by nfscl_umount() and we need to get out
867 * now for that case and not wait until nfscl_umount()
876 * If it needs a clientid, do the setclientid now.
878 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
880 panic("nfscl_clget");
881 if (p == NULL || cred == NULL) {
883 nfsv4_unlock(&clp->nfsc_lock, 0);
888 * If RFC3530 Sec. 14.2.33 is taken literally,
889 * NFSERR_CLIDINUSE will be returned persistently for the
890 * case where a new mount of the same file system is using
891 * a different principal. In practice, NFSERR_CLIDINUSE is
892 * only returned when there is outstanding unexpired state
893 * on the clientid. As such, try for twice the lease
894 * interval, if we know what that is. Otherwise, make a
896 * The case of returning NFSERR_STALECLIENTID is far less
897 * likely, but might occur if there is a significant delay
898 * between doing the SetClientID and SetClientIDConfirm Ops,
899 * such that the server throws away the clientid before
900 * receiving the SetClientIDConfirm.
902 if (clp->nfsc_renew > 0)
903 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
905 clidinusedelay = 120;
908 error = nfsrpc_setclient(nmp, clp, 0, cred, p);
909 if (error == NFSERR_STALECLIENTID ||
910 error == NFSERR_STALEDONTRECOVER ||
911 error == NFSERR_BADSESSION ||
912 error == NFSERR_CLIDINUSE) {
913 (void) nfs_catnap(PZERO, error, "nfs_setcl");
915 } while (((error == NFSERR_STALECLIENTID ||
916 error == NFSERR_BADSESSION ||
917 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
918 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
921 nfsv4_unlock(&clp->nfsc_lock, 0);
925 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
929 nfsv4_unlock(&clp->nfsc_lock, 1);
938 * Get a reference to a clientid and return it, if valid.
940 APPLESTATIC struct nfsclclient *
941 nfscl_findcl(struct nfsmount *nmp)
943 struct nfsclclient *clp;
946 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
952 * Release the clientid structure. It may be locked or reference counted.
955 nfscl_clrelease(struct nfsclclient *clp)
958 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
959 nfsv4_unlock(&clp->nfsc_lock, 0);
961 nfsv4_relref(&clp->nfsc_lock);
965 * External call for nfscl_clrelease.
968 nfscl_clientrelease(struct nfsclclient *clp)
972 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
973 nfsv4_unlock(&clp->nfsc_lock, 0);
975 nfsv4_relref(&clp->nfsc_lock);
980 * Called when wanting to lock a byte region.
983 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
984 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
985 int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
986 struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
988 struct nfscllockowner *lp;
989 struct nfsclopen *op;
990 struct nfsclclient *clp;
991 struct nfscllockowner *nlp;
992 struct nfscllock *nlop, *otherlop;
993 struct nfscldeleg *dp = NULL, *ldp = NULL;
994 struct nfscllockownerhead *lhp = NULL;
996 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
998 int error = 0, ret, donelocally = 0;
1001 /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
1010 * Might need these, so MALLOC them now, to
1011 * avoid a tsleep() in MALLOC later.
1013 MALLOC(nlp, struct nfscllockowner *,
1014 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
1015 MALLOC(otherlop, struct nfscllock *,
1016 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1017 MALLOC(nlop, struct nfscllock *,
1018 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1019 nlop->nfslo_type = type;
1020 nlop->nfslo_first = off;
1021 if (len == NFS64BITSSET) {
1022 nlop->nfslo_end = NFS64BITSSET;
1024 nlop->nfslo_end = off + len;
1025 if (nlop->nfslo_end <= nlop->nfslo_first)
1026 error = NFSERR_INVAL;
1033 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
1036 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1037 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1038 FREE((caddr_t)nlop, M_NFSCLLOCK);
1045 openownp = ropenownp;
1047 nfscl_filllockowner(id, own, flags);
1049 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
1050 nfscl_filllockowner(NULL, openown, F_POSIX);
1052 nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1058 * First, search for a delegation. If one exists for this file,
1059 * the lock can be done locally against it, so long as there
1060 * isn't a local lock conflict.
1062 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1063 np->n_fhp->nfh_len);
1064 /* Just sanity check for correct type of delegation */
1065 if (dp != NULL && ((dp->nfsdl_flags &
1066 (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1068 (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1072 /* Now, find an open and maybe a lockowner. */
1073 ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
1074 np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1076 ret = nfscl_getopen(&clp->nfsc_owner,
1077 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1078 ownp, mode, NULL, &op);
1080 lhp = &dp->nfsdl_lock;
1081 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1082 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1083 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1091 * Get the related Open and maybe lockowner.
1093 error = nfscl_getopen(&clp->nfsc_owner,
1094 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1095 ownp, mode, &lp, &op);
1097 lhp = &op->nfso_lock;
1099 if (!error && !recovery)
1100 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1101 np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1104 nfscl_clrelease(clp);
1107 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1108 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1109 FREE((caddr_t)nlop, M_NFSCLLOCK);
1114 * Ok, see if a lockowner exists and create one, as required.
1117 LIST_FOREACH(lp, lhp, nfsl_list) {
1118 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1122 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1124 NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1125 NFSV4CL_LOCKNAMELEN);
1127 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1128 NFSV4CL_LOCKNAMELEN);
1129 nlp->nfsl_seqid = 0;
1130 nlp->nfsl_lockflags = flags;
1131 nlp->nfsl_inprog = NULL;
1132 nfscl_lockinit(&nlp->nfsl_rwlock);
1133 LIST_INIT(&nlp->nfsl_lock);
1135 nlp->nfsl_open = NULL;
1136 nfsstatsv1.cllocallockowners++;
1138 nlp->nfsl_open = op;
1139 nfsstatsv1.cllockowners++;
1141 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1148 * Now, update the byte ranges for locks.
1150 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1156 nfscl_clrelease(clp);
1159 * Serial modifications on the lock owner for multiple threads
1160 * for the same process using a read/write lock.
1163 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1169 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1171 FREE((caddr_t)nlop, M_NFSCLLOCK);
1173 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1180 * Called to unlock a byte range, for LockU.
1183 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1184 __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1185 struct nfsclclient *clp, void *id, int flags,
1186 struct nfscllockowner **lpp, int *dorpcp)
1188 struct nfscllockowner *lp;
1189 struct nfsclowner *owp;
1190 struct nfsclopen *op;
1191 struct nfscllock *nlop, *other_lop = NULL;
1192 struct nfscldeleg *dp;
1194 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1202 * Might need these, so MALLOC them now, to
1203 * avoid a tsleep() in MALLOC later.
1205 MALLOC(nlop, struct nfscllock *,
1206 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1207 nlop->nfslo_type = F_UNLCK;
1208 nlop->nfslo_first = off;
1209 if (len == NFS64BITSSET) {
1210 nlop->nfslo_end = NFS64BITSSET;
1212 nlop->nfslo_end = off + len;
1213 if (nlop->nfslo_end <= nlop->nfslo_first) {
1214 FREE((caddr_t)nlop, M_NFSCLLOCK);
1215 return (NFSERR_INVAL);
1219 MALLOC(other_lop, struct nfscllock *,
1220 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1223 nfscl_filllockowner(id, own, flags);
1227 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1228 np->n_fhp->nfh_len);
1231 * First, unlock any local regions on a delegation.
1234 /* Look for this lockowner. */
1235 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1236 if (!NFSBCMP(lp->nfsl_owner, own,
1237 NFSV4CL_LOCKNAMELEN))
1241 /* Use other_lop, so nlop is still available */
1242 (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1246 * Now, find a matching open/lockowner that hasn't already been done,
1247 * as marked by nfsl_inprog.
1251 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1252 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1253 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1254 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1255 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1256 if (lp->nfsl_inprog == NULL &&
1257 !NFSBCMP(lp->nfsl_owner, own,
1258 NFSV4CL_LOCKNAMELEN)) {
1272 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1276 * Serial modifications on the lock owner for multiple
1277 * threads for the same process using a read/write lock.
1279 lp->nfsl_inprog = p;
1280 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1285 FREE((caddr_t)nlop, M_NFSCLLOCK);
1287 FREE((caddr_t)other_lop, M_NFSCLLOCK);
1292 * Release all lockowners marked in progess for this process and file.
1295 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1296 void *id, int flags)
1298 struct nfsclowner *owp;
1299 struct nfsclopen *op;
1300 struct nfscllockowner *lp;
1302 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1305 nfscl_filllockowner(id, own, flags);
1307 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1308 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1309 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1310 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1311 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1312 if (lp->nfsl_inprog == p &&
1313 !NFSBCMP(lp->nfsl_owner, own,
1314 NFSV4CL_LOCKNAMELEN)) {
1315 lp->nfsl_inprog = NULL;
1316 nfscl_lockunlock(&lp->nfsl_rwlock);
1322 nfscl_clrelease(clp);
1327 * Called to find out if any bytes within the byte range specified are
1328 * write locked by the calling process. Used to determine if flushing
1329 * is required before a LockU.
1330 * If in doubt, return 1, so the flush will occur.
1333 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1334 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1336 struct nfsclowner *owp;
1337 struct nfscllockowner *lp;
1338 struct nfsclopen *op;
1339 struct nfsclclient *clp;
1340 struct nfscllock *lop;
1341 struct nfscldeleg *dp;
1344 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1348 switch (fl->l_whence) {
1352 * Caller is responsible for adding any necessary offset
1353 * when SEEK_CUR is used.
1358 off = np->n_size + fl->l_start;
1363 if (fl->l_len != 0) {
1364 end = off + fl->l_len;
1371 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
1374 nfscl_filllockowner(id, own, flags);
1378 * First check the delegation locks.
1380 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1382 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1383 if (!NFSBCMP(lp->nfsl_owner, own,
1384 NFSV4CL_LOCKNAMELEN))
1388 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1389 if (lop->nfslo_first >= end)
1391 if (lop->nfslo_end <= off)
1393 if (lop->nfslo_type == F_WRLCK) {
1394 nfscl_clrelease(clp);
1403 * Now, check state against the server.
1405 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1406 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1407 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1408 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1409 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1410 if (!NFSBCMP(lp->nfsl_owner, own,
1411 NFSV4CL_LOCKNAMELEN))
1415 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1416 if (lop->nfslo_first >= end)
1418 if (lop->nfslo_end <= off)
1420 if (lop->nfslo_type == F_WRLCK) {
1421 nfscl_clrelease(clp);
1430 nfscl_clrelease(clp);
1436 * Release a byte range lock owner structure.
1439 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1441 struct nfsclclient *clp;
1446 clp = lp->nfsl_open->nfso_own->nfsow_clp;
1447 if (error != 0 && candelete &&
1448 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1449 nfscl_freelockowner(lp, 0);
1451 nfscl_lockunlock(&lp->nfsl_rwlock);
1452 nfscl_clrelease(clp);
1457 * Free up an open structure and any associated byte range lock structures.
1460 nfscl_freeopen(struct nfsclopen *op, int local)
1463 LIST_REMOVE(op, nfso_list);
1464 nfscl_freealllocks(&op->nfso_lock, local);
1465 FREE((caddr_t)op, M_NFSCLOPEN);
1467 nfsstatsv1.cllocalopens--;
1469 nfsstatsv1.clopens--;
1473 * Free up all lock owners and associated locks.
1476 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1478 struct nfscllockowner *lp, *nlp;
1480 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1481 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1483 nfscl_freelockowner(lp, local);
1488 * Called for an Open when NFSERR_EXPIRED is received from the server.
1489 * If there are no byte range locks nor a Share Deny lost, try to do a
1490 * fresh Open. Otherwise, free the open.
1493 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1494 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1496 struct nfscllockowner *lp;
1497 struct nfscldeleg *dp;
1498 int mustdelete = 0, error;
1501 * Look for any byte range lock(s).
1503 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1504 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1511 * If no byte range lock(s) nor a Share deny, try to re-open.
1513 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1514 newnfs_copycred(&op->nfso_cred, cred);
1516 error = nfsrpc_reopen(nmp, op->nfso_fh,
1517 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1521 FREE((caddr_t)dp, M_NFSCLDELEG);
1526 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1527 op->nfso_fhlen, cred, p, &dp);
1531 * If a byte range lock or Share deny or couldn't re-open, free it.
1534 nfscl_freeopen(op, 0);
1535 return (mustdelete);
1539 * Free up an open owner structure.
1542 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1545 LIST_REMOVE(owp, nfsow_list);
1546 FREE((caddr_t)owp, M_NFSCLOWNER);
1548 nfsstatsv1.cllocalopenowners--;
1550 nfsstatsv1.clopenowners--;
1554 * Free up a byte range lock owner structure.
1557 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1559 struct nfscllock *lop, *nlop;
1561 LIST_REMOVE(lp, nfsl_list);
1562 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1563 nfscl_freelock(lop, local);
1565 FREE((caddr_t)lp, M_NFSCLLOCKOWNER);
1567 nfsstatsv1.cllocallockowners--;
1569 nfsstatsv1.cllockowners--;
1573 * Free up a byte range lock structure.
1576 nfscl_freelock(struct nfscllock *lop, int local)
1579 LIST_REMOVE(lop, nfslo_list);
1580 FREE((caddr_t)lop, M_NFSCLLOCK);
1582 nfsstatsv1.cllocallocks--;
1584 nfsstatsv1.cllocks--;
1588 * Clean out the state related to a delegation.
1591 nfscl_cleandeleg(struct nfscldeleg *dp)
1593 struct nfsclowner *owp, *nowp;
1594 struct nfsclopen *op;
1596 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1597 op = LIST_FIRST(&owp->nfsow_open);
1599 if (LIST_NEXT(op, nfso_list) != NULL)
1600 panic("nfscleandel");
1601 nfscl_freeopen(op, 1);
1603 nfscl_freeopenowner(owp, 1);
1605 nfscl_freealllocks(&dp->nfsdl_lock, 1);
1609 * Free a delegation.
1612 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp)
1615 TAILQ_REMOVE(hdp, dp, nfsdl_list);
1616 LIST_REMOVE(dp, nfsdl_hash);
1617 FREE((caddr_t)dp, M_NFSCLDELEG);
1618 nfsstatsv1.cldelegates--;
1623 * Free up all state related to this client structure.
1626 nfscl_cleanclient(struct nfsclclient *clp)
1628 struct nfsclowner *owp, *nowp;
1629 struct nfsclopen *op, *nop;
1630 struct nfscllayout *lyp, *nlyp;
1631 struct nfscldevinfo *dip, *ndip;
1633 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1634 nfscl_freelayout(lyp);
1636 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
1637 nfscl_freedevinfo(dip);
1639 /* Now, all the OpenOwners, etc. */
1640 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1641 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1642 nfscl_freeopen(op, 0);
1644 nfscl_freeopenowner(owp, 0);
1649 * Called when an NFSERR_EXPIRED is received from the server.
1652 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1653 struct ucred *cred, NFSPROC_T *p)
1655 struct nfsclowner *owp, *nowp, *towp;
1656 struct nfsclopen *op, *nop, *top;
1657 struct nfscldeleg *dp, *ndp;
1658 int ret, printed = 0;
1661 * First, merge locally issued Opens into the list for the server.
1663 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1664 while (dp != NULL) {
1665 ndp = TAILQ_NEXT(dp, nfsdl_list);
1666 owp = LIST_FIRST(&dp->nfsdl_owner);
1667 while (owp != NULL) {
1668 nowp = LIST_NEXT(owp, nfsow_list);
1669 op = LIST_FIRST(&owp->nfsow_open);
1671 if (LIST_NEXT(op, nfso_list) != NULL)
1673 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1674 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1675 NFSV4CL_LOCKNAMELEN))
1679 /* Merge opens in */
1680 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1681 if (top->nfso_fhlen == op->nfso_fhlen &&
1682 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1684 top->nfso_mode |= op->nfso_mode;
1685 top->nfso_opencnt += op->nfso_opencnt;
1690 /* Just add the open to the owner list */
1691 LIST_REMOVE(op, nfso_list);
1692 op->nfso_own = towp;
1693 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1694 nfsstatsv1.cllocalopens--;
1695 nfsstatsv1.clopens++;
1698 /* Just add the openowner to the client list */
1699 LIST_REMOVE(owp, nfsow_list);
1700 owp->nfsow_clp = clp;
1701 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1702 nfsstatsv1.cllocalopenowners--;
1703 nfsstatsv1.clopenowners++;
1704 nfsstatsv1.cllocalopens--;
1705 nfsstatsv1.clopens++;
1710 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1712 printf("nfsv4 expired locks lost\n");
1714 nfscl_cleandeleg(dp);
1715 nfscl_freedeleg(&clp->nfsc_deleg, dp);
1718 if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1722 * Now, try and reopen against the server.
1724 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1725 owp->nfsow_seqid = 0;
1726 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1727 ret = nfscl_expireopen(clp, op, nmp, cred, p);
1728 if (ret && !printed) {
1730 printf("nfsv4 expired locks lost\n");
1733 if (LIST_EMPTY(&owp->nfsow_open))
1734 nfscl_freeopenowner(owp, 0);
1739 * This function must be called after the process represented by "own" has
1740 * exited. Must be called with CLSTATE lock held.
1743 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1745 struct nfsclowner *owp, *nowp;
1746 struct nfscllockowner *lp, *nlp;
1747 struct nfscldeleg *dp;
1749 /* First, get rid of local locks on delegations. */
1750 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1751 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1752 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1753 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1755 nfscl_freelockowner(lp, 1);
1759 owp = LIST_FIRST(&clp->nfsc_owner);
1760 while (owp != NULL) {
1761 nowp = LIST_NEXT(owp, nfsow_list);
1762 if (!NFSBCMP(owp->nfsow_owner, own,
1763 NFSV4CL_LOCKNAMELEN)) {
1765 * If there are children that haven't closed the
1766 * file descriptors yet, the opens will still be
1767 * here. For that case, let the renew thread clear
1768 * out the OpenOwner later.
1770 if (LIST_EMPTY(&owp->nfsow_open))
1771 nfscl_freeopenowner(owp, 0);
1773 owp->nfsow_defunct = 1;
1780 * Find open/lock owners for processes that have exited.
1783 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1785 struct nfsclowner *owp, *nowp;
1786 struct nfsclopen *op;
1787 struct nfscllockowner *lp, *nlp;
1788 struct nfscldeleg *dp;
1792 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1793 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1794 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1795 if (LIST_EMPTY(&lp->nfsl_lock))
1796 nfscl_emptylockowner(lp, lhp);
1799 if (nfscl_procdoesntexist(owp->nfsow_owner))
1800 nfscl_cleanup_common(clp, owp->nfsow_owner);
1804 * For the single open_owner case, these lock owners need to be
1805 * checked to see if they still exist separately.
1806 * This is because nfscl_procdoesntexist() never returns true for
1807 * the single open_owner so that the above doesn't ever call
1808 * nfscl_cleanup_common().
1810 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1811 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1812 if (nfscl_procdoesntexist(lp->nfsl_owner))
1813 nfscl_cleanup_common(clp, lp->nfsl_owner);
1817 NFSPROCLISTUNLOCK();
1821 * Take the empty lock owner and move it to the local lhp list if the
1822 * associated process no longer exists.
1825 nfscl_emptylockowner(struct nfscllockowner *lp,
1826 struct nfscllockownerfhhead *lhp)
1828 struct nfscllockownerfh *lfhp, *mylfhp;
1829 struct nfscllockowner *nlp;
1832 /* If not a Posix lock owner, just return. */
1833 if ((lp->nfsl_lockflags & F_POSIX) == 0)
1839 * First, search to see if this lock owner is already in the list.
1840 * If it is, then the associated process no longer exists.
1842 SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1843 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1844 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
1847 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
1848 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
1849 NFSV4CL_LOCKNAMELEN))
1852 /* If not found, check if process still exists. */
1853 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
1856 /* Move the lock owner over to the local list. */
1857 if (mylfhp == NULL) {
1858 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
1862 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
1863 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
1864 mylfhp->nfslfh_len);
1865 LIST_INIT(&mylfhp->nfslfh_lock);
1866 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
1868 LIST_REMOVE(lp, nfsl_list);
1869 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
1872 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */
1874 * Called from nfs umount to free up the clientid.
1877 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1879 struct nfsclclient *clp;
1884 * For the case that matters, this is the thread that set
1885 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
1886 * done to ensure that any thread executing nfscl_getcl() after
1887 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
1888 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
1889 * explanation, courtesy of Alan Cox.
1890 * What follows is a snippet from Alan Cox's email at:
1891 * http://docs.FreeBSD.org/cgi/
1892 * mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
1894 * 1. Set MNTK_UNMOUNTF
1895 * 2. Acquire a standard FreeBSD mutex "m".
1896 * 3. Update some data structures.
1897 * 4. Release mutex "m".
1899 * Then, other threads that acquire "m" after step 4 has occurred will
1900 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to
1901 * step 2 may or may not see MNTK_UNMOUNTF as set.
1904 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
1912 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1913 panic("nfscl umount");
1916 * First, handshake with the nfscl renew thread, to terminate
1919 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1920 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1921 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
1925 * Now, get the exclusive lock on the client state, so
1926 * that no uses of the state are still in progress.
1929 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1930 NFSCLSTATEMUTEXPTR, NULL);
1931 } while (!igotlock);
1935 * Free up all the state. It will expire on the server, but
1936 * maybe we should do a SetClientId/SetClientIdConfirm so
1937 * the server throws it away?
1939 LIST_REMOVE(clp, nfsc_list);
1940 nfscl_delegreturnall(clp, p);
1941 cred = newnfs_getcred();
1942 if (NFSHASNFSV4N(nmp)) {
1943 (void)nfsrpc_destroysession(nmp, clp, cred, p);
1944 (void)nfsrpc_destroyclient(nmp, clp, cred, p);
1946 (void)nfsrpc_setclient(nmp, clp, 0, cred, p);
1947 nfscl_cleanclient(clp);
1950 free(clp, M_NFSCLCLIENT);
1956 * This function is called when a server replies with NFSERR_STALECLIENTID
1957 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
1958 * doing Opens and Locks with reclaim. If these fail, it deletes the
1959 * corresponding state.
1962 nfscl_recover(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
1964 struct nfsclowner *owp, *nowp;
1965 struct nfsclopen *op, *nop;
1966 struct nfscllockowner *lp, *nlp;
1967 struct nfscllock *lop, *nlop;
1968 struct nfscldeleg *dp, *ndp, *tdp;
1969 struct nfsmount *nmp;
1970 struct ucred *tcred;
1971 struct nfsclopenhead extra_open;
1972 struct nfscldeleghead extra_deleg;
1975 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
1976 int i, igotlock = 0, error, trycnt, firstlock;
1977 struct nfscllayout *lyp, *nlyp;
1980 * First, lock the client structure, so everyone else will
1981 * block when trying to use state.
1984 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
1986 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1987 NFSCLSTATEMUTEXPTR, NULL);
1988 } while (!igotlock);
1991 nmp = clp->nfsc_nmp;
1993 panic("nfscl recover");
1996 * For now, just get rid of all layouts. There may be a need
1997 * to do LayoutCommit Ops with reclaim == true later.
1999 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
2000 nfscl_freelayout(lyp);
2001 TAILQ_INIT(&clp->nfsc_layout);
2002 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
2003 LIST_INIT(&clp->nfsc_layouthash[i]);
2007 error = nfsrpc_setclient(nmp, clp, 1, cred, p);
2008 } while ((error == NFSERR_STALECLIENTID ||
2009 error == NFSERR_BADSESSION ||
2010 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2013 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
2014 NFSCLFLAGS_RECVRINPROG);
2015 wakeup(&clp->nfsc_flags);
2016 nfsv4_unlock(&clp->nfsc_lock, 0);
2020 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2021 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2024 * Mark requests already queued on the server, so that they don't
2025 * initiate another recovery cycle. Any requests already in the
2026 * queue that handle state information will have the old stale
2027 * clientid/stateid and will get a NFSERR_STALESTATEID,
2028 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2029 * This will be translated to NFSERR_STALEDONTRECOVER when
2030 * R_DONTRECOVER is set.
2033 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2034 if (rep->r_nmp == nmp)
2035 rep->r_flags |= R_DONTRECOVER;
2040 * Now, mark all delegations "need reclaim".
2042 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2043 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2045 TAILQ_INIT(&extra_deleg);
2046 LIST_INIT(&extra_open);
2048 * Now traverse the state lists, doing Open and Lock Reclaims.
2050 tcred = newnfs_getcred();
2051 owp = LIST_FIRST(&clp->nfsc_owner);
2052 while (owp != NULL) {
2053 nowp = LIST_NEXT(owp, nfsow_list);
2054 owp->nfsow_seqid = 0;
2055 op = LIST_FIRST(&owp->nfsow_open);
2056 while (op != NULL) {
2057 nop = LIST_NEXT(op, nfso_list);
2058 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2059 /* Search for a delegation to reclaim with the open */
2060 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2061 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2063 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2064 mode = NFSV4OPEN_ACCESSWRITE;
2065 delegtype = NFSV4OPEN_DELEGATEWRITE;
2067 mode = NFSV4OPEN_ACCESSREAD;
2068 delegtype = NFSV4OPEN_DELEGATEREAD;
2070 if ((op->nfso_mode & mode) == mode &&
2071 op->nfso_fhlen == dp->nfsdl_fhlen &&
2072 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2077 delegtype = NFSV4OPEN_DELEGATENONE;
2078 newnfs_copycred(&op->nfso_cred, tcred);
2079 error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2080 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2081 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2084 /* Handle any replied delegation */
2085 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2086 || NFSMNT_RDONLY(nmp->nm_mountp))) {
2087 if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2088 mode = NFSV4OPEN_ACCESSWRITE;
2090 mode = NFSV4OPEN_ACCESSREAD;
2091 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2092 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2094 if ((op->nfso_mode & mode) == mode &&
2095 op->nfso_fhlen == dp->nfsdl_fhlen &&
2096 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2098 dp->nfsdl_stateid = ndp->nfsdl_stateid;
2099 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2100 dp->nfsdl_ace = ndp->nfsdl_ace;
2101 dp->nfsdl_change = ndp->nfsdl_change;
2102 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2103 if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2104 dp->nfsdl_flags |= NFSCLDL_RECALL;
2105 FREE((caddr_t)ndp, M_NFSCLDELEG);
2112 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2114 /* and reclaim all byte range locks */
2115 lp = LIST_FIRST(&op->nfso_lock);
2116 while (lp != NULL) {
2117 nlp = LIST_NEXT(lp, nfsl_list);
2120 lop = LIST_FIRST(&lp->nfsl_lock);
2121 while (lop != NULL) {
2122 nlop = LIST_NEXT(lop, nfslo_list);
2123 if (lop->nfslo_end == NFS64BITSSET)
2126 len = lop->nfslo_end - lop->nfslo_first;
2127 error = nfscl_trylock(nmp, NULL,
2128 op->nfso_fh, op->nfso_fhlen, lp,
2129 firstlock, 1, lop->nfslo_first, len,
2130 lop->nfslo_type, tcred, p);
2132 nfscl_freelock(lop, 0);
2137 /* If no locks, but a lockowner, just delete it. */
2138 if (LIST_EMPTY(&lp->nfsl_lock))
2139 nfscl_freelockowner(lp, 0);
2144 if (error != 0 && error != NFSERR_BADSESSION)
2145 nfscl_freeopen(op, 0);
2152 * Now, try and get any delegations not yet reclaimed by cobbling
2153 * to-gether an appropriate open.
2156 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2157 while (dp != NULL) {
2158 ndp = TAILQ_NEXT(dp, nfsdl_list);
2159 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2161 MALLOC(nowp, struct nfsclowner *,
2162 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2164 * Name must be as long an largest possible
2165 * NFSV4CL_LOCKNAMELEN. 12 for now.
2167 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2168 NFSV4CL_LOCKNAMELEN);
2169 LIST_INIT(&nowp->nfsow_open);
2170 nowp->nfsow_clp = clp;
2171 nowp->nfsow_seqid = 0;
2172 nowp->nfsow_defunct = 0;
2173 nfscl_lockinit(&nowp->nfsow_rwlock);
2176 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2177 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
2178 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2179 nop->nfso_own = nowp;
2180 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2181 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2182 delegtype = NFSV4OPEN_DELEGATEWRITE;
2184 nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2185 delegtype = NFSV4OPEN_DELEGATEREAD;
2187 nop->nfso_opencnt = 0;
2188 nop->nfso_posixlock = 1;
2189 nop->nfso_fhlen = dp->nfsdl_fhlen;
2190 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2191 LIST_INIT(&nop->nfso_lock);
2192 nop->nfso_stateid.seqid = 0;
2193 nop->nfso_stateid.other[0] = 0;
2194 nop->nfso_stateid.other[1] = 0;
2195 nop->nfso_stateid.other[2] = 0;
2196 newnfs_copycred(&dp->nfsdl_cred, tcred);
2197 newnfs_copyincred(tcred, &nop->nfso_cred);
2199 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2200 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2201 nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2202 delegtype, tcred, p);
2204 if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2205 mode = NFSV4OPEN_ACCESSWRITE;
2207 mode = NFSV4OPEN_ACCESSREAD;
2208 if ((nop->nfso_mode & mode) == mode &&
2209 nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2210 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2212 dp->nfsdl_stateid = tdp->nfsdl_stateid;
2213 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2214 dp->nfsdl_ace = tdp->nfsdl_ace;
2215 dp->nfsdl_change = tdp->nfsdl_change;
2216 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2217 if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2218 dp->nfsdl_flags |= NFSCLDL_RECALL;
2219 FREE((caddr_t)tdp, M_NFSCLDELEG);
2221 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2227 FREE((caddr_t)nop, M_NFSCLOPEN);
2229 * Couldn't reclaim it, so throw the state
2232 nfscl_cleandeleg(dp);
2233 nfscl_freedeleg(&clp->nfsc_deleg, dp);
2235 LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2242 * Now, get rid of extra Opens and Delegations.
2244 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2246 newnfs_copycred(&op->nfso_cred, tcred);
2247 error = nfscl_tryclose(op, tcred, nmp, p);
2248 if (error == NFSERR_GRACE)
2249 (void) nfs_catnap(PZERO, error, "nfsexcls");
2250 } while (error == NFSERR_GRACE);
2251 LIST_REMOVE(op, nfso_list);
2252 FREE((caddr_t)op, M_NFSCLOPEN);
2255 FREE((caddr_t)nowp, M_NFSCLOWNER);
2257 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2259 newnfs_copycred(&dp->nfsdl_cred, tcred);
2260 error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2261 if (error == NFSERR_GRACE)
2262 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2263 } while (error == NFSERR_GRACE);
2264 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2265 FREE((caddr_t)dp, M_NFSCLDELEG);
2268 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2269 if (NFSHASNFSV4N(nmp))
2270 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
2273 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2274 wakeup(&clp->nfsc_flags);
2275 nfsv4_unlock(&clp->nfsc_lock, 0);
2281 * This function is called when a server replies with NFSERR_EXPIRED.
2282 * It deletes all state for the client and does a fresh SetClientId/confirm.
2283 * XXX Someday it should post a signal to the process(es) that hold the
2284 * state, so they know that lock state has been lost.
2287 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2289 struct nfsmount *nmp;
2291 int igotlock = 0, error, trycnt;
2294 * If the clientid has gone away or a new SetClientid has already
2295 * been done, just return ok.
2297 if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2301 * First, lock the client structure, so everyone else will
2302 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2303 * that only one thread does the work.
2306 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2308 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2309 NFSCLSTATEMUTEXPTR, NULL);
2310 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2311 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2313 nfsv4_unlock(&clp->nfsc_lock, 0);
2317 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2320 nmp = clp->nfsc_nmp;
2322 panic("nfscl expired");
2323 cred = newnfs_getcred();
2326 error = nfsrpc_setclient(nmp, clp, 0, cred, p);
2327 } while ((error == NFSERR_STALECLIENTID ||
2328 error == NFSERR_BADSESSION ||
2329 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2332 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2335 * Expire the state for the client.
2337 nfscl_expireclient(clp, nmp, cred, p);
2339 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2340 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2342 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2343 wakeup(&clp->nfsc_flags);
2344 nfsv4_unlock(&clp->nfsc_lock, 0);
2351 * This function inserts a lock in the list after insert_lop.
2354 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2355 struct nfscllock *insert_lop, int local)
2358 if ((struct nfscllockowner *)insert_lop == lp)
2359 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2361 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2363 nfsstatsv1.cllocallocks++;
2365 nfsstatsv1.cllocks++;
2369 * This function updates the locking for a lock owner and given file. It
2370 * maintains a list of lock ranges ordered on increasing file offset that
2371 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2372 * It always adds new_lop to the list and sometimes uses the one pointed
2374 * Returns 1 if the locks were modified, 0 otherwise.
2377 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2378 struct nfscllock **other_lopp, int local)
2380 struct nfscllock *new_lop = *new_lopp;
2381 struct nfscllock *lop, *tlop, *ilop;
2382 struct nfscllock *other_lop;
2383 int unlock = 0, modified = 0;
2387 * Work down the list until the lock is merged.
2389 if (new_lop->nfslo_type == F_UNLCK)
2391 ilop = (struct nfscllock *)lp;
2392 lop = LIST_FIRST(&lp->nfsl_lock);
2393 while (lop != NULL) {
2395 * Only check locks for this file that aren't before the start of
2398 if (lop->nfslo_end >= new_lop->nfslo_first) {
2399 if (new_lop->nfslo_end < lop->nfslo_first) {
2401 * If the new lock ends before the start of the
2402 * current lock's range, no merge, just insert
2407 if (new_lop->nfslo_type == lop->nfslo_type ||
2408 (new_lop->nfslo_first <= lop->nfslo_first &&
2409 new_lop->nfslo_end >= lop->nfslo_end)) {
2411 * This lock can be absorbed by the new lock/unlock.
2412 * This happens when it covers the entire range
2413 * of the old lock or is contiguous
2414 * with the old lock and is of the same type or an
2417 if (new_lop->nfslo_type != lop->nfslo_type ||
2418 new_lop->nfslo_first != lop->nfslo_first ||
2419 new_lop->nfslo_end != lop->nfslo_end)
2421 if (lop->nfslo_first < new_lop->nfslo_first)
2422 new_lop->nfslo_first = lop->nfslo_first;
2423 if (lop->nfslo_end > new_lop->nfslo_end)
2424 new_lop->nfslo_end = lop->nfslo_end;
2426 lop = LIST_NEXT(lop, nfslo_list);
2427 nfscl_freelock(tlop, local);
2432 * All these cases are for contiguous locks that are not the
2433 * same type, so they can't be merged.
2435 if (new_lop->nfslo_first <= lop->nfslo_first) {
2437 * This case is where the new lock overlaps with the
2438 * first part of the old lock. Move the start of the
2439 * old lock to just past the end of the new lock. The
2440 * new lock will be inserted in front of the old, since
2441 * ilop hasn't been updated. (We are done now.)
2443 if (lop->nfslo_first != new_lop->nfslo_end) {
2444 lop->nfslo_first = new_lop->nfslo_end;
2449 if (new_lop->nfslo_end >= lop->nfslo_end) {
2451 * This case is where the new lock overlaps with the
2452 * end of the old lock's range. Move the old lock's
2453 * end to just before the new lock's first and insert
2454 * the new lock after the old lock.
2455 * Might not be done yet, since the new lock could
2456 * overlap further locks with higher ranges.
2458 if (lop->nfslo_end != new_lop->nfslo_first) {
2459 lop->nfslo_end = new_lop->nfslo_first;
2463 lop = LIST_NEXT(lop, nfslo_list);
2467 * The final case is where the new lock's range is in the
2468 * middle of the current lock's and splits the current lock
2469 * up. Use *other_lopp to handle the second part of the
2470 * split old lock range. (We are done now.)
2471 * For unlock, we use new_lop as other_lop and tmp, since
2472 * other_lop and new_lop are the same for this case.
2473 * We noted the unlock case above, so we don't need
2474 * new_lop->nfslo_type any longer.
2476 tmp = new_lop->nfslo_first;
2478 other_lop = new_lop;
2481 other_lop = *other_lopp;
2484 other_lop->nfslo_first = new_lop->nfslo_end;
2485 other_lop->nfslo_end = lop->nfslo_end;
2486 other_lop->nfslo_type = lop->nfslo_type;
2487 lop->nfslo_end = tmp;
2488 nfscl_insertlock(lp, other_lop, lop, local);
2494 lop = LIST_NEXT(lop, nfslo_list);
2500 * Insert the new lock in the list at the appropriate place.
2503 nfscl_insertlock(lp, new_lop, ilop, local);
2511 * This function must be run as a kernel thread.
2512 * It does Renew Ops and recovery, when required.
2515 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2517 struct nfsclowner *owp, *nowp;
2518 struct nfsclopen *op;
2519 struct nfscllockowner *lp, *nlp;
2520 struct nfscldeleghead dh;
2521 struct nfscldeleg *dp, *ndp;
2524 int error, cbpathdown, islept, igotlock, ret, clearok;
2525 uint32_t recover_done_time = 0;
2527 static time_t prevsec = 0;
2528 struct nfscllockownerfh *lfhp, *nlfhp;
2529 struct nfscllockownerfhhead lfh;
2530 struct nfscllayout *lyp, *nlyp;
2531 struct nfscldevinfo *dip, *ndip;
2532 struct nfscllayouthead rlh;
2533 struct nfsclrecalllayout *recallp;
2534 struct nfsclds *dsp;
2536 cred = newnfs_getcred();
2538 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2541 newnfs_setroot(cred);
2543 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2545 * Only allow one recover within 1/2 of the lease
2546 * duration (nfsc_renew).
2548 if (recover_done_time < NFSD_MONOSEC) {
2549 recover_done_time = NFSD_MONOSEC +
2551 NFSCL_DEBUG(1, "Doing recovery..\n");
2552 nfscl_recover(clp, cred, p);
2554 NFSCL_DEBUG(1, "Clear Recovery dt=%u ms=%jd\n",
2555 recover_done_time, (intmax_t)NFSD_MONOSEC);
2557 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2561 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2562 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2563 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2564 clidrev = clp->nfsc_clientidrev;
2565 error = nfsrpc_renew(clp, NULL, cred, p);
2566 if (error == NFSERR_CBPATHDOWN)
2568 else if (error == NFSERR_STALECLIENTID ||
2569 error == NFSERR_BADSESSION) {
2571 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2573 } else if (error == NFSERR_EXPIRED)
2574 (void) nfscl_hasexpired(clp, clidrev, p);
2578 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2579 /* Do renews for any DS sessions. */
2580 NFSLOCKMNT(clp->nfsc_nmp);
2581 /* Skip first entry, since the MDS is handled above. */
2582 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2584 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2585 while (dsp != NULL) {
2586 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2587 dsp->nfsclds_sess.nfsess_defunct == 0) {
2588 dsp->nfsclds_expire = NFSD_MONOSEC +
2590 NFSUNLOCKMNT(clp->nfsc_nmp);
2591 (void)nfsrpc_renew(clp, dsp, cred, p);
2594 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2596 NFSUNLOCKMNT(clp->nfsc_nmp);
2602 /* It's a Total Recall! */
2603 nfscl_totalrecall(clp);
2606 * Now, handle defunct owners.
2608 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2609 if (LIST_EMPTY(&owp->nfsow_open)) {
2610 if (owp->nfsow_defunct != 0)
2611 nfscl_freeopenowner(owp, 0);
2616 * Do the recall on any delegations. To avoid trouble, always
2617 * come back up here after having slept.
2621 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2622 while (dp != NULL) {
2623 ndp = TAILQ_NEXT(dp, nfsdl_list);
2624 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2626 * Wait for outstanding I/O ops to be done.
2628 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2630 nfsv4_unlock(&clp->nfsc_lock, 0);
2633 dp->nfsdl_rwlock.nfslock_lock |=
2635 (void) nfsmsleep(&dp->nfsdl_rwlock,
2636 NFSCLSTATEMUTEXPTR, PZERO, "nfscld",
2641 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2642 &islept, NFSCLSTATEMUTEXPTR, NULL);
2647 newnfs_copycred(&dp->nfsdl_cred, cred);
2648 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2651 nfscl_cleandeleg(dp);
2652 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2654 LIST_REMOVE(dp, nfsdl_hash);
2655 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2657 nfsstatsv1.cldelegates--;
2665 * Clear out old delegations, if we are above the high water
2666 * mark. Only clear out ones with no state related to them.
2667 * The tailq list is in LRU order.
2669 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2670 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2671 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2672 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2673 dp->nfsdl_rwlock.nfslock_lock == 0 &&
2674 dp->nfsdl_timestamp < NFSD_MONOSEC &&
2675 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2676 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2678 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2679 op = LIST_FIRST(&owp->nfsow_open);
2686 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2687 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2694 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2695 LIST_REMOVE(dp, nfsdl_hash);
2696 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2698 nfsstatsv1.cldelegates--;
2704 nfsv4_unlock(&clp->nfsc_lock, 0);
2707 * Do the recall on any layouts. To avoid trouble, always
2708 * come back up here after having slept.
2712 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2713 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2715 * Wait for outstanding I/O ops to be done.
2717 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2718 (lyp->nfsly_lock.nfslock_lock &
2719 NFSV4LOCK_LOCK) != 0) {
2720 lyp->nfsly_lock.nfslock_lock |=
2722 (void)nfsmsleep(&lyp->nfsly_lock,
2723 NFSCLSTATEMUTEXPTR, PZERO, "nfslyp",
2727 /* Move the layout to the recall list. */
2728 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2730 LIST_REMOVE(lyp, nfsly_hash);
2731 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2733 /* Handle any layout commits. */
2734 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2735 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2736 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2738 NFSCL_DEBUG(3, "do layoutcommit\n");
2739 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2747 /* Now, look for stale layouts. */
2748 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2749 while (lyp != NULL) {
2750 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2751 if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2752 (lyp->nfsly_flags & NFSLY_RECALL) == 0 &&
2753 lyp->nfsly_lock.nfslock_usecnt == 0 &&
2754 lyp->nfsly_lock.nfslock_lock == 0) {
2755 NFSCL_DEBUG(4, "ret stale lay=%d\n",
2757 recallp = malloc(sizeof(*recallp),
2758 M_NFSLAYRECALL, M_NOWAIT);
2759 if (recallp == NULL)
2761 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2762 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2763 lyp->nfsly_stateid.seqid, recallp);
2769 * Free up any unreferenced device info structures.
2771 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
2772 if (dip->nfsdi_layoutrefs == 0 &&
2773 dip->nfsdi_refcnt == 0) {
2774 NFSCL_DEBUG(4, "freeing devinfo\n");
2775 LIST_REMOVE(dip, nfsdi_list);
2776 nfscl_freedevinfo(dip);
2781 /* Do layout return(s), as required. */
2782 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
2783 TAILQ_REMOVE(&rlh, lyp, nfsly_list);
2784 NFSCL_DEBUG(4, "ret layout\n");
2785 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
2786 nfscl_freelayout(lyp);
2790 * Delegreturn any delegations cleaned out or recalled.
2792 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2793 newnfs_copycred(&dp->nfsdl_cred, cred);
2794 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2795 TAILQ_REMOVE(&dh, dp, nfsdl_list);
2796 FREE((caddr_t)dp, M_NFSCLDELEG);
2801 * Call nfscl_cleanupkext() once per second to check for
2802 * open/lock owners where the process has exited.
2804 mytime = NFSD_MONOSEC;
2805 if (prevsec != mytime) {
2807 nfscl_cleanupkext(clp, &lfh);
2811 * Do a ReleaseLockOwner for all lock owners where the
2812 * associated process no longer exists, as found by
2813 * nfscl_cleanupkext().
2815 newnfs_setroot(cred);
2816 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
2817 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
2819 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
2820 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
2822 nfscl_freelockowner(lp, 0);
2829 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2830 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
2832 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2833 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2836 wakeup((caddr_t)clp);
2844 * Initiate state recovery. Called when NFSERR_STALECLIENTID,
2845 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
2848 nfscl_initiate_recovery(struct nfsclclient *clp)
2854 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2856 wakeup((caddr_t)clp);
2860 * Dump out the state stuff for debugging.
2863 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
2864 int lockowner, int locks)
2866 struct nfsclclient *clp;
2867 struct nfsclowner *owp;
2868 struct nfsclopen *op;
2869 struct nfscllockowner *lp;
2870 struct nfscllock *lop;
2871 struct nfscldeleg *dp;
2875 printf("nfscl dumpstate NULL clp\n");
2879 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2880 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2881 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2882 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2883 owp->nfsow_owner[0], owp->nfsow_owner[1],
2884 owp->nfsow_owner[2], owp->nfsow_owner[3],
2886 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2888 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2889 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2890 op->nfso_stateid.other[2], op->nfso_opencnt,
2892 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2894 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2895 lp->nfsl_owner[0], lp->nfsl_owner[1],
2896 lp->nfsl_owner[2], lp->nfsl_owner[3],
2898 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2899 lp->nfsl_stateid.other[2]);
2900 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2903 printf("lck typ=%d fst=%ju end=%ju\n",
2904 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2905 (intmax_t)lop->nfslo_end);
2907 printf("lck typ=%d fst=%qd end=%qd\n",
2908 lop->nfslo_type, lop->nfslo_first,
2916 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2917 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2918 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2919 owp->nfsow_owner[0], owp->nfsow_owner[1],
2920 owp->nfsow_owner[2], owp->nfsow_owner[3],
2922 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2924 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2925 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2926 op->nfso_stateid.other[2], op->nfso_opencnt,
2928 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2930 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2931 lp->nfsl_owner[0], lp->nfsl_owner[1],
2932 lp->nfsl_owner[2], lp->nfsl_owner[3],
2934 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2935 lp->nfsl_stateid.other[2]);
2936 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2939 printf("lck typ=%d fst=%ju end=%ju\n",
2940 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2941 (intmax_t)lop->nfslo_end);
2943 printf("lck typ=%d fst=%qd end=%qd\n",
2944 lop->nfslo_type, lop->nfslo_first,
2955 * Check for duplicate open owners and opens.
2956 * (Only used as a diagnostic aid.)
2959 nfscl_dupopen(vnode_t vp, int dupopens)
2961 struct nfsclclient *clp;
2962 struct nfsclowner *owp, *owp2;
2963 struct nfsclopen *op, *op2;
2966 clp = VFSTONFS(vnode_mount(vp))->nm_clp;
2968 printf("nfscl dupopen NULL clp\n");
2971 nfhp = VTONFS(vp)->n_fhp;
2975 * First, search for duplicate owners.
2976 * These should never happen!
2978 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2979 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2981 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
2982 NFSV4CL_LOCKNAMELEN)) {
2984 printf("DUP OWNER\n");
2985 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0, 0);
2992 * Now, search for duplicate stateids.
2993 * These shouldn't happen, either.
2995 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2996 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
2997 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2998 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3000 (op->nfso_stateid.other[0] != 0 ||
3001 op->nfso_stateid.other[1] != 0 ||
3002 op->nfso_stateid.other[2] != 0) &&
3003 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
3004 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
3005 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
3007 printf("DUP STATEID\n");
3008 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0,
3018 * Now search for duplicate opens.
3019 * Duplicate opens for the same owner
3020 * should never occur. Other duplicates are
3021 * possible and are checked for if "dupopens"
3024 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3025 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3026 if (nfhp->nfh_len == op2->nfso_fhlen &&
3027 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3028 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3029 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3030 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3031 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3032 (!NFSBCMP(op->nfso_own->nfsow_owner,
3033 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3035 if (!NFSBCMP(op->nfso_own->nfsow_owner,
3036 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3038 printf("BADDUP OPEN\n");
3041 printf("DUP OPEN\n");
3043 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1,
3056 * During close, find an open that needs to be dereferenced and
3057 * dereference it. If there are no more opens for this file,
3058 * log a message to that effect.
3059 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3060 * on the file's vnode.
3061 * This is the safe way, since it is difficult to identify
3062 * which open the close is for and I/O can be performed after the
3063 * close(2) system call when a file is mmap'd.
3064 * If it returns 0 for success, there will be a referenced
3065 * clp returned via clpp.
3068 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3070 struct nfsclclient *clp;
3071 struct nfsclowner *owp;
3072 struct nfsclopen *op;
3073 struct nfscldeleg *dp;
3077 error = nfscl_getcl(vnode_mount(vp), NULL, NULL, 1, &clp);
3082 nfhp = VTONFS(vp)->n_fhp;
3086 * First, look for one under a delegation that was locally issued
3087 * and just decrement the opencnt for it. Since all my Opens against
3088 * the server are DENY_NONE, I don't see a problem with hanging
3089 * onto them. (It is much easier to use one of the extant Opens
3090 * that I already have on the server when a Delegation is recalled
3091 * than to do fresh Opens.) Someday, I might need to rethink this, but.
3093 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3095 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3096 op = LIST_FIRST(&owp->nfsow_open);
3099 * Since a delegation is for a file, there
3100 * should never be more than one open for
3103 if (LIST_NEXT(op, nfso_list) != NULL)
3104 panic("nfscdeleg opens");
3105 if (notdecr && op->nfso_opencnt > 0) {
3114 /* Now process the opens against the server. */
3115 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3116 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3117 if (op->nfso_fhlen == nfhp->nfh_len &&
3118 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3120 /* Found an open, decrement cnt if possible */
3121 if (notdecr && op->nfso_opencnt > 0) {
3126 * There are more opens, so just return.
3128 if (op->nfso_opencnt > 0) {
3137 printf("nfscl: never fnd open\n");
3142 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3144 struct nfsclclient *clp;
3145 struct nfsclowner *owp, *nowp;
3146 struct nfsclopen *op;
3147 struct nfscldeleg *dp;
3149 struct nfsclrecalllayout *recallp;
3152 error = nfscl_getcl(vnode_mount(vp), NULL, NULL, 1, &clp);
3157 nfhp = VTONFS(vp)->n_fhp;
3158 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
3161 * First get rid of the local Open structures, which should be no
3164 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3166 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3167 op = LIST_FIRST(&owp->nfsow_open);
3169 KASSERT((op->nfso_opencnt == 0),
3170 ("nfscl: bad open cnt on deleg"));
3171 nfscl_freeopen(op, 1);
3173 nfscl_freeopenowner(owp, 1);
3177 /* Return any layouts marked return on close. */
3178 nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp);
3180 /* Now process the opens against the server. */
3182 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3183 op = LIST_FIRST(&owp->nfsow_open);
3184 while (op != NULL) {
3185 if (op->nfso_fhlen == nfhp->nfh_len &&
3186 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3188 /* Found an open, close it. */
3189 KASSERT((op->nfso_opencnt == 0),
3190 ("nfscl: bad open cnt on server"));
3192 nfsrpc_doclose(VFSTONFS(vnode_mount(vp)), op,
3197 op = LIST_NEXT(op, nfso_list);
3202 * recallp has been set NULL by nfscl_retoncloselayout() if it was
3203 * used by the function, but calling free() with a NULL pointer is ok.
3205 free(recallp, M_NFSLAYRECALL);
3210 * Return all delegations on this client.
3211 * (Must be called with client sleep lock.)
3214 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
3216 struct nfscldeleg *dp, *ndp;
3219 cred = newnfs_getcred();
3220 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3221 nfscl_cleandeleg(dp);
3222 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3223 nfscl_freedeleg(&clp->nfsc_deleg, dp);
3229 * Do a callback RPC.
3232 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3234 int clist, gotseq_ok, i, j, k, op, rcalls;
3236 struct nfsclclient *clp;
3237 struct nfscldeleg *dp = NULL;
3238 int numops, taglen = -1, error = 0, trunc;
3239 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3240 u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3246 nfsattrbit_t attrbits, rattrbits;
3247 nfsv4stateid_t stateid;
3248 uint32_t seqid, slotid = 0, highslot, cachethis;
3249 uint8_t sessionid[NFSX_V4SESSIONID];
3251 struct nfscllayout *lyp;
3252 uint64_t filesid[2], len, off;
3253 int changed, gotone, laytype, recalltype;
3255 struct nfsclrecalllayout *recallp = NULL;
3256 struct nfsclsession *tsep;
3260 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3261 taglen = fxdr_unsigned(int, *tl);
3266 if (taglen <= NFSV4_SMALLSTR)
3269 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3270 error = nfsrv_mtostr(nd, tagstr, taglen);
3272 if (taglen > NFSV4_SMALLSTR)
3273 free(tagstr, M_TEMP);
3277 (void) nfsm_strtom(nd, tag, taglen);
3278 if (taglen > NFSV4_SMALLSTR) {
3279 free(tagstr, M_TEMP);
3281 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3282 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3283 minorvers = fxdr_unsigned(u_int32_t, *tl++);
3284 if (minorvers != NFSV4_MINORVERSION && minorvers != NFSV41_MINORVERSION)
3285 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3286 cbident = fxdr_unsigned(u_int32_t, *tl++);
3290 numops = fxdr_unsigned(int, *tl);
3292 * Loop around doing the sub ops.
3294 for (i = 0; i < numops; i++) {
3295 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3296 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3298 op = fxdr_unsigned(int, *tl);
3299 if (op < NFSV4OP_CBGETATTR ||
3300 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3301 (op > NFSV4OP_CBNOTIFYDEVID &&
3302 minorvers == NFSV41_MINORVERSION)) {
3303 nd->nd_repstat = NFSERR_OPILLEGAL;
3304 *repp = nfscl_errmap(nd, minorvers);
3308 nd->nd_procnum = op;
3309 if (op < NFSV41_CBNOPS)
3310 nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
3312 case NFSV4OP_CBGETATTR:
3313 NFSCL_DEBUG(4, "cbgetattr\n");
3316 error = nfsm_getfh(nd, &nfhp);
3318 error = nfsrv_getattrbits(nd, &attrbits,
3320 if (error == 0 && i == 0 &&
3321 minorvers != NFSV4_MINORVERSION)
3322 error = NFSERR_OPNOTINSESS;
3324 mp = nfscl_getmnt(minorvers, sessionid, cbident,
3327 error = NFSERR_SERVERFAULT;
3330 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3331 nfhp->nfh_len, p, &np);
3336 NFSZERO_ATTRBIT(&rattrbits);
3338 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3341 if (NFSISSET_ATTRBIT(&attrbits,
3344 va.va_size = np->n_size;
3348 NFSSETBIT_ATTRBIT(&rattrbits,
3351 if (NFSISSET_ATTRBIT(&attrbits,
3352 NFSATTRBIT_CHANGE)) {
3356 (np->n_flag & NDELEGMOD))
3358 NFSSETBIT_ATTRBIT(&rattrbits,
3362 error = NFSERR_SERVERFAULT;
3370 FREE((caddr_t)nfhp, M_NFSFH);
3372 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3373 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3376 case NFSV4OP_CBRECALL:
3377 NFSCL_DEBUG(4, "cbrecall\n");
3378 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3380 stateid.seqid = *tl++;
3381 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3383 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3384 trunc = fxdr_unsigned(int, *tl);
3385 error = nfsm_getfh(nd, &nfhp);
3386 if (error == 0 && i == 0 &&
3387 minorvers != NFSV4_MINORVERSION)
3388 error = NFSERR_OPNOTINSESS;
3391 if (minorvers == NFSV4_MINORVERSION)
3392 clp = nfscl_getclnt(cbident);
3394 clp = nfscl_getclntsess(sessionid);
3396 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3398 if (dp != NULL && (dp->nfsdl_flags &
3399 NFSCLDL_DELEGRET) == 0) {
3402 wakeup((caddr_t)clp);
3405 error = NFSERR_SERVERFAULT;
3410 FREE((caddr_t)nfhp, M_NFSFH);
3412 case NFSV4OP_CBLAYOUTRECALL:
3413 NFSCL_DEBUG(4, "cblayrec\n");
3415 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3416 laytype = fxdr_unsigned(int, *tl++);
3417 iomode = fxdr_unsigned(uint32_t, *tl++);
3418 if (newnfs_true == *tl++)
3422 recalltype = fxdr_unsigned(int, *tl);
3423 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3425 if (laytype != NFSLAYOUT_NFSV4_1_FILES)
3426 error = NFSERR_NOMATCHLAYOUT;
3427 else if (recalltype == NFSLAYOUTRETURN_FILE) {
3428 error = nfsm_getfh(nd, &nfhp);
3429 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3432 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3434 off = fxdr_hyper(tl); tl += 2;
3435 len = fxdr_hyper(tl); tl += 2;
3436 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3437 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3438 if (minorvers == NFSV4_MINORVERSION)
3439 error = NFSERR_NOTSUPP;
3441 error = NFSERR_OPNOTINSESS;
3444 clp = nfscl_getclntsess(sessionid);
3445 NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3447 lyp = nfscl_findlayout(clp,
3450 NFSCL_DEBUG(4, "cblyp=%p\n",
3454 NFSLY_FILES) != 0 &&
3455 !NFSBCMP(stateid.other,
3456 lyp->nfsly_stateid.other,
3457 NFSX_STATEIDOTHER)) {
3471 NFSERR_NOMATCHLAYOUT;
3473 error = NFSERR_NOMATCHLAYOUT;
3476 free(nfhp, M_NFSFH);
3477 } else if (recalltype == NFSLAYOUTRETURN_FSID) {
3478 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3479 filesid[0] = fxdr_hyper(tl); tl += 2;
3480 filesid[1] = fxdr_hyper(tl); tl += 2;
3483 clp = nfscl_getclntsess(sessionid);
3485 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3487 if (lyp->nfsly_filesid[0] ==
3489 lyp->nfsly_filesid[1] ==
3496 lyp->nfsly_stateid.seqid,
3505 error = NFSERR_NOMATCHLAYOUT;
3507 error = NFSERR_NOMATCHLAYOUT;
3509 } else if (recalltype == NFSLAYOUTRETURN_ALL) {
3512 clp = nfscl_getclntsess(sessionid);
3514 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3516 error = nfscl_layoutrecall(
3517 recalltype, lyp, iomode, 0,
3519 lyp->nfsly_stateid.seqid,
3527 error = NFSERR_NOMATCHLAYOUT;
3529 error = NFSERR_NOMATCHLAYOUT;
3532 error = NFSERR_NOMATCHLAYOUT;
3533 if (recallp != NULL) {
3534 free(recallp, M_NFSLAYRECALL);
3538 case NFSV4OP_CBSEQUENCE:
3539 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3541 bcopy(tl, sessionid, NFSX_V4SESSIONID);
3542 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3543 seqid = fxdr_unsigned(uint32_t, *tl++);
3544 slotid = fxdr_unsigned(uint32_t, *tl++);
3545 highslot = fxdr_unsigned(uint32_t, *tl++);
3547 /* Throw away the referring call stuff. */
3548 clist = fxdr_unsigned(int, *tl);
3549 for (j = 0; j < clist; j++) {
3550 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3552 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3553 rcalls = fxdr_unsigned(int, *tl);
3554 for (k = 0; k < rcalls; k++) {
3555 NFSM_DISSECT(tl, uint32_t *,
3561 clp = nfscl_getclntsess(sessionid);
3563 error = NFSERR_SERVERFAULT;
3565 error = NFSERR_SEQUENCEPOS;
3567 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3568 error = nfsv4_seqsession(seqid, slotid,
3569 highslot, tsep->nfsess_cbslots, &rep,
3570 tsep->nfsess_backslots);
3573 if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
3577 * Handle a reply for a retried
3578 * callback. The reply will be
3579 * re-inserted in the session cache
3580 * by the nfsv4_seqsess_cacherep() call
3583 KASSERT(error == NFSERR_REPLYFROMCACHE,
3584 ("cbsequence: non-NULL rep"));
3585 NFSCL_DEBUG(4, "Got cbretry\n");
3586 m_freem(nd->nd_mreq);
3591 NFSM_BUILD(tl, uint32_t *,
3592 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3593 bcopy(sessionid, tl, NFSX_V4SESSIONID);
3594 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3595 *tl++ = txdr_unsigned(seqid);
3596 *tl++ = txdr_unsigned(slotid);
3597 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3598 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3602 if (i == 0 && minorvers == NFSV41_MINORVERSION)
3603 error = NFSERR_OPNOTINSESS;
3605 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3606 error = NFSERR_NOTSUPP;
3611 if (error == EBADRPC || error == NFSERR_BADXDR) {
3612 nd->nd_repstat = NFSERR_BADXDR;
3614 nd->nd_repstat = error;
3619 if (nd->nd_repstat) {
3620 *repp = nfscl_errmap(nd, minorvers);
3623 *repp = 0; /* NFS4_OK */
3626 if (recallp != NULL)
3627 free(recallp, M_NFSLAYRECALL);
3629 if (error == EBADRPC || error == NFSERR_BADXDR)
3630 nd->nd_repstat = NFSERR_BADXDR;
3632 printf("nfsv4 comperr1=%d\n", error);
3635 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3639 *retopsp = txdr_unsigned(retops);
3641 *nd->nd_errp = nfscl_errmap(nd, minorvers);
3643 if (gotseq_ok != 0) {
3644 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3646 clp = nfscl_getclntsess(sessionid);
3648 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3649 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3660 * Generate the next cbident value. Basically just increment a static value
3661 * and then check that it isn't already in the list, if it has wrapped around.
3664 nfscl_nextcbident(void)
3666 struct nfsclclient *clp;
3668 static u_int32_t nextcbident = 0;
3669 static int haswrapped = 0;
3672 if (nextcbident == 0)
3676 * Search the clientid list for one already using this cbident.
3681 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3682 if (clp->nfsc_cbident == nextcbident) {
3692 return (nextcbident);
3696 * Get the mount point related to a given cbident or session and busy it.
3699 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
3700 struct nfsclclient **clpp)
3702 struct nfsclclient *clp;
3705 struct nfsclsession *tsep;
3709 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3710 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3711 if (minorvers == NFSV4_MINORVERSION) {
3712 if (clp->nfsc_cbident == cbident)
3714 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3722 mp = clp->nfsc_nmp->nm_mountp;
3725 error = vfs_busy(mp, 0);
3734 * Get the clientid pointer related to a given cbident.
3736 static struct nfsclclient *
3737 nfscl_getclnt(u_int32_t cbident)
3739 struct nfsclclient *clp;
3741 LIST_FOREACH(clp, &nfsclhead, nfsc_list)
3742 if (clp->nfsc_cbident == cbident)
3748 * Get the clientid pointer related to a given sessionid.
3750 static struct nfsclclient *
3751 nfscl_getclntsess(uint8_t *sessionid)
3753 struct nfsclclient *clp;
3754 struct nfsclsession *tsep;
3756 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3757 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3758 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3766 * Search for a lock conflict locally on the client. A conflict occurs if
3767 * - not same owner and overlapping byte range and at least one of them is
3768 * a write lock or this is an unlock.
3771 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
3772 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
3773 struct nfscllock **lopp)
3775 struct nfsclowner *owp;
3776 struct nfsclopen *op;
3780 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3784 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3785 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3786 if (op->nfso_fhlen == fhlen &&
3787 !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
3788 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
3799 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3800 u_int8_t *own, struct nfscllock **lopp)
3802 struct nfscllockowner *lp;
3803 struct nfscllock *lop;
3805 LIST_FOREACH(lp, lhp, nfsl_list) {
3806 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
3807 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3808 if (lop->nfslo_first >= nlop->nfslo_end)
3810 if (lop->nfslo_end <= nlop->nfslo_first)
3812 if (lop->nfslo_type == F_WRLCK ||
3813 nlop->nfslo_type == F_WRLCK ||
3814 nlop->nfslo_type == F_UNLCK) {
3817 return (NFSERR_DENIED);
3826 * Check for a local conflicting lock.
3829 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
3830 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
3832 struct nfscllock *lop, nlck;
3833 struct nfscldeleg *dp;
3835 u_int8_t own[NFSV4CL_LOCKNAMELEN];
3838 nlck.nfslo_type = fl->l_type;
3839 nlck.nfslo_first = off;
3840 if (len == NFS64BITSSET) {
3841 nlck.nfslo_end = NFS64BITSSET;
3843 nlck.nfslo_end = off + len;
3844 if (nlck.nfslo_end <= nlck.nfslo_first)
3845 return (NFSERR_INVAL);
3848 nfscl_filllockowner(id, own, flags);
3850 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3851 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
3852 &nlck, own, dp, &lop);
3854 fl->l_whence = SEEK_SET;
3855 fl->l_start = lop->nfslo_first;
3856 if (lop->nfslo_end == NFS64BITSSET)
3859 fl->l_len = lop->nfslo_end - lop->nfslo_first;
3860 fl->l_pid = (pid_t)0;
3861 fl->l_type = lop->nfslo_type;
3862 error = -1; /* no RPC required */
3863 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
3864 fl->l_type == F_RDLCK)) {
3866 * The delegation ensures that there isn't a conflicting
3867 * lock on the server, so return -1 to indicate an RPC
3870 fl->l_type = F_UNLCK;
3878 * Handle Recall of a delegation.
3879 * The clp must be exclusive locked when this is called.
3882 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
3883 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
3884 int called_from_renewthread)
3886 struct nfsclowner *owp, *lowp, *nowp;
3887 struct nfsclopen *op, *lop;
3888 struct nfscllockowner *lp;
3889 struct nfscllock *lckp;
3891 int error = 0, ret, gotvp = 0;
3895 * First, get a vnode for the file. This is needed to do RPCs.
3897 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
3898 dp->nfsdl_fhlen, p, &np);
3901 * File isn't open, so nothing to move over to the
3911 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
3914 * Ok, if it's a write delegation, flush data to the server, so
3915 * that close/open consistency is retained.
3919 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
3920 np->n_flag |= NDELEGRECALL;
3922 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
3924 np->n_flag &= ~NDELEGRECALL;
3926 NFSINVALATTRCACHE(np);
3928 if (ret == EIO && called_from_renewthread != 0) {
3930 * If the flush failed with EIO for the renew thread,
3931 * return now, so that the dirty buffer will be flushed
3940 * Now, for each openowner with opens issued locally, move them
3941 * over to state against the server.
3943 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
3944 lop = LIST_FIRST(&lowp->nfsow_open);
3946 if (LIST_NEXT(lop, nfso_list) != NULL)
3947 panic("nfsdlg mult opens");
3949 * Look for the same openowner against the server.
3951 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3952 if (!NFSBCMP(lowp->nfsow_owner,
3953 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3954 newnfs_copycred(&dp->nfsdl_cred, cred);
3955 ret = nfscl_moveopen(vp, clp, nmp, lop,
3957 if (ret == NFSERR_STALECLIENTID ||
3958 ret == NFSERR_STALEDONTRECOVER ||
3959 ret == NFSERR_BADSESSION) {
3965 nfscl_freeopen(lop, 1);
3974 * If no openowner found, create one and get an open
3978 MALLOC(nowp, struct nfsclowner *,
3979 sizeof (struct nfsclowner), M_NFSCLOWNER,
3981 nfscl_newopen(clp, NULL, &owp, &nowp, &op,
3982 NULL, lowp->nfsow_owner, dp->nfsdl_fh,
3983 dp->nfsdl_fhlen, NULL, NULL);
3984 newnfs_copycred(&dp->nfsdl_cred, cred);
3985 ret = nfscl_moveopen(vp, clp, nmp, lop,
3988 nfscl_freeopenowner(owp, 0);
3989 if (ret == NFSERR_STALECLIENTID ||
3990 ret == NFSERR_STALEDONTRECOVER ||
3991 ret == NFSERR_BADSESSION) {
3997 nfscl_freeopen(lop, 1);
4007 * Now, get byte range locks for any locks done locally.
4009 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4010 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
4011 newnfs_copycred(&dp->nfsdl_cred, cred);
4012 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
4013 if (ret == NFSERR_STALESTATEID ||
4014 ret == NFSERR_STALEDONTRECOVER ||
4015 ret == NFSERR_STALECLIENTID ||
4016 ret == NFSERR_BADSESSION) {
4031 * Move a locally issued open over to an owner on the state list.
4032 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
4033 * returns with it unlocked.
4036 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4037 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
4038 struct ucred *cred, NFSPROC_T *p)
4040 struct nfsclopen *op, *nop;
4041 struct nfscldeleg *ndp;
4043 int error = 0, newone;
4046 * First, look for an appropriate open, If found, just increment the
4049 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4050 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4051 op->nfso_fhlen == lop->nfso_fhlen &&
4052 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4053 op->nfso_opencnt += lop->nfso_opencnt;
4054 nfscl_freeopen(lop, 1);
4059 /* No appropriate open, so we have to do one against the server. */
4061 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
4062 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4064 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4065 lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
4067 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4068 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4069 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4072 nfscl_freeopen(op, 0);
4074 op->nfso_mode |= lop->nfso_mode;
4075 op->nfso_opencnt += lop->nfso_opencnt;
4076 nfscl_freeopen(lop, 1);
4079 FREE((caddr_t)nop, M_NFSCLOPEN);
4082 * What should I do with the returned delegation, since the
4083 * delegation is being recalled? For now, just printf and
4086 printf("Moveopen returned deleg\n");
4087 FREE((caddr_t)ndp, M_NFSCLDELEG);
4093 * Recall all delegations on this client.
4096 nfscl_totalrecall(struct nfsclclient *clp)
4098 struct nfscldeleg *dp;
4100 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4101 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4102 dp->nfsdl_flags |= NFSCLDL_RECALL;
4107 * Relock byte ranges. Called for delegation recall and state expiry.
4110 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4111 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4114 struct nfscllockowner *nlp;
4117 u_int32_t clidrev = 0;
4118 int error, newone, donelocally;
4120 off = lop->nfslo_first;
4121 len = lop->nfslo_end - lop->nfslo_first;
4122 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4123 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4124 lp->nfsl_openowner, &nlp, &newone, &donelocally);
4125 if (error || donelocally)
4127 if (nmp->nm_clp != NULL)
4128 clidrev = nmp->nm_clp->nfsc_clientidrev;
4131 nfhp = VTONFS(vp)->n_fhp;
4132 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4133 nfhp->nfh_len, nlp, newone, 0, off,
4134 len, lop->nfslo_type, cred, p);
4136 nfscl_freelockowner(nlp, 0);
4141 * Called to re-open a file. Basically get a vnode for the file handle
4142 * and then call nfsrpc_openrpc() to do the rest.
4145 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4146 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4147 struct ucred *cred, NFSPROC_T *p)
4153 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4157 if (np->n_v4 != NULL) {
4158 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4159 np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4160 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4170 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4171 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4175 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4176 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4177 u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4178 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4183 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4184 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4186 if (error == NFSERR_DELAY)
4187 (void) nfs_catnap(PZERO, error, "nfstryop");
4188 } while (error == NFSERR_DELAY);
4189 if (error == EAUTH || error == EACCES) {
4190 /* Try again using system credentials */
4191 newnfs_setroot(cred);
4193 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4194 newfhlen, mode, op, name, namelen, ndpp, reclaim,
4195 delegtype, cred, p, 1, 0);
4196 if (error == NFSERR_DELAY)
4197 (void) nfs_catnap(PZERO, error, "nfstryop");
4198 } while (error == NFSERR_DELAY);
4204 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4205 * NFSERR_DELAY. Also, retry with system credentials, if the provided
4209 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4210 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4211 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4213 struct nfsrv_descript nfsd, *nd = &nfsd;
4217 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4218 reclaim, off, len, type, cred, p, 0);
4219 if (!error && nd->nd_repstat == NFSERR_DELAY)
4220 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4222 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4224 error = nd->nd_repstat;
4225 if (error == EAUTH || error == EACCES) {
4226 /* Try again using root credentials */
4227 newnfs_setroot(cred);
4229 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4230 newone, reclaim, off, len, type, cred, p, 1);
4231 if (!error && nd->nd_repstat == NFSERR_DELAY)
4232 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4234 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4236 error = nd->nd_repstat;
4242 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4243 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4247 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4248 struct nfsmount *nmp, NFSPROC_T *p)
4253 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4254 if (error == NFSERR_DELAY)
4255 (void) nfs_catnap(PZERO, error, "nfstrydp");
4256 } while (error == NFSERR_DELAY);
4257 if (error == EAUTH || error == EACCES) {
4258 /* Try again using system credentials */
4259 newnfs_setroot(cred);
4261 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4262 if (error == NFSERR_DELAY)
4263 (void) nfs_catnap(PZERO, error, "nfstrydp");
4264 } while (error == NFSERR_DELAY);
4270 * Try a close against the server. Just call nfsrpc_closerpc(),
4271 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4275 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4276 struct nfsmount *nmp, NFSPROC_T *p)
4278 struct nfsrv_descript nfsd, *nd = &nfsd;
4282 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4283 if (error == NFSERR_DELAY)
4284 (void) nfs_catnap(PZERO, error, "nfstrycl");
4285 } while (error == NFSERR_DELAY);
4286 if (error == EAUTH || error == EACCES) {
4287 /* Try again using system credentials */
4288 newnfs_setroot(cred);
4290 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4291 if (error == NFSERR_DELAY)
4292 (void) nfs_catnap(PZERO, error, "nfstrycl");
4293 } while (error == NFSERR_DELAY);
4299 * Decide if a delegation on a file permits close without flushing writes
4300 * to the server. This might be a big performance win in some environments.
4301 * (Not useful until the client does caching on local stable storage.)
4304 nfscl_mustflush(vnode_t vp)
4306 struct nfsclclient *clp;
4307 struct nfscldeleg *dp;
4309 struct nfsmount *nmp;
4312 nmp = VFSTONFS(vnode_mount(vp));
4313 if (!NFSHASNFSV4(nmp))
4316 clp = nfscl_findcl(nmp);
4321 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4322 if (dp != NULL && (dp->nfsdl_flags &
4323 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4325 (dp->nfsdl_sizelimit >= np->n_size ||
4326 !NFSHASSTRICT3530(nmp))) {
4335 * See if a (write) delegation exists for this file.
4338 nfscl_nodeleg(vnode_t vp, int writedeleg)
4340 struct nfsclclient *clp;
4341 struct nfscldeleg *dp;
4343 struct nfsmount *nmp;
4346 nmp = VFSTONFS(vnode_mount(vp));
4347 if (!NFSHASNFSV4(nmp))
4350 clp = nfscl_findcl(nmp);
4355 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4357 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4358 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4368 * Look for an associated delegation that should be DelegReturned.
4371 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4373 struct nfsclclient *clp;
4374 struct nfscldeleg *dp;
4375 struct nfsclowner *owp;
4376 struct nfscllockowner *lp;
4377 struct nfsmount *nmp;
4380 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4382 nmp = VFSTONFS(vnode_mount(vp));
4386 * Loop around waiting for:
4387 * - outstanding I/O operations on delegations to complete
4388 * - for a delegation on vp that has state, lock the client and
4390 * - return delegation with no state
4393 clp = nfscl_findcl(nmp);
4398 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4399 np->n_fhp->nfh_len);
4402 * Wait for outstanding I/O ops to be done.
4404 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4406 nfsv4_unlock(&clp->nfsc_lock, 0);
4409 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4410 (void) nfsmsleep(&dp->nfsdl_rwlock,
4411 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4415 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4416 if (!LIST_EMPTY(&owp->nfsow_open)) {
4422 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4423 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4429 if (needsrecall && !triedrecall) {
4430 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4433 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4434 &islept, NFSCLSTATEMUTEXPTR, NULL);
4441 cred = newnfs_getcred();
4442 newnfs_copycred(&dp->nfsdl_cred, cred);
4443 (void) nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0);
4447 nfsv4_unlock(&clp->nfsc_lock, 0);
4451 *stp = dp->nfsdl_stateid;
4453 nfscl_cleandeleg(dp);
4454 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4457 nfsv4_unlock(&clp->nfsc_lock, 0);
4464 * Look for associated delegation(s) that should be DelegReturned.
4467 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4468 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4470 struct nfsclclient *clp;
4471 struct nfscldeleg *dp;
4472 struct nfsclowner *owp;
4473 struct nfscllockowner *lp;
4474 struct nfsmount *nmp;
4477 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4479 nmp = VFSTONFS(vnode_mount(fvp));
4484 * Loop around waiting for:
4485 * - outstanding I/O operations on delegations to complete
4486 * - for a delegation on fvp that has state, lock the client and
4488 * - return delegation(s) with no state.
4491 clp = nfscl_findcl(nmp);
4497 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4498 np->n_fhp->nfh_len);
4499 if (dp != NULL && *gotfdp == 0) {
4501 * Wait for outstanding I/O ops to be done.
4503 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4505 nfsv4_unlock(&clp->nfsc_lock, 0);
4508 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4509 (void) nfsmsleep(&dp->nfsdl_rwlock,
4510 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4514 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4515 if (!LIST_EMPTY(&owp->nfsow_open)) {
4521 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4522 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4528 if (needsrecall && !triedrecall) {
4529 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4532 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4533 &islept, NFSCLSTATEMUTEXPTR, NULL);
4540 cred = newnfs_getcred();
4541 newnfs_copycred(&dp->nfsdl_cred, cred);
4542 (void) nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0);
4546 nfsv4_unlock(&clp->nfsc_lock, 0);
4550 *fstp = dp->nfsdl_stateid;
4553 nfscl_cleandeleg(dp);
4554 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4557 nfsv4_unlock(&clp->nfsc_lock, 0);
4562 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4563 np->n_fhp->nfh_len);
4564 if (dp != NULL && *gottdp == 0) {
4566 * Wait for outstanding I/O ops to be done.
4568 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4569 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4570 (void) nfsmsleep(&dp->nfsdl_rwlock,
4571 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4574 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4575 if (!LIST_EMPTY(&owp->nfsow_open)) {
4580 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4581 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4586 *tstp = dp->nfsdl_stateid;
4589 nfscl_cleandeleg(dp);
4590 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4599 * Get a reference on the clientid associated with the mount point.
4600 * Return 1 if success, 0 otherwise.
4603 nfscl_getref(struct nfsmount *nmp)
4605 struct nfsclclient *clp;
4608 clp = nfscl_findcl(nmp);
4613 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
4619 * Release a reference on a clientid acquired with the above call.
4622 nfscl_relref(struct nfsmount *nmp)
4624 struct nfsclclient *clp;
4627 clp = nfscl_findcl(nmp);
4632 nfsv4_relref(&clp->nfsc_lock);
4637 * Save the size attribute in the delegation, since the nfsnode
4641 nfscl_reclaimnode(vnode_t vp)
4643 struct nfsclclient *clp;
4644 struct nfscldeleg *dp;
4645 struct nfsnode *np = VTONFS(vp);
4646 struct nfsmount *nmp;
4648 nmp = VFSTONFS(vnode_mount(vp));
4649 if (!NFSHASNFSV4(nmp))
4652 clp = nfscl_findcl(nmp);
4657 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4658 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4659 dp->nfsdl_size = np->n_size;
4664 * Get the saved size attribute in the delegation, since it is a
4665 * newly allocated nfsnode.
4668 nfscl_newnode(vnode_t vp)
4670 struct nfsclclient *clp;
4671 struct nfscldeleg *dp;
4672 struct nfsnode *np = VTONFS(vp);
4673 struct nfsmount *nmp;
4675 nmp = VFSTONFS(vnode_mount(vp));
4676 if (!NFSHASNFSV4(nmp))
4679 clp = nfscl_findcl(nmp);
4684 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4685 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4686 np->n_size = dp->nfsdl_size;
4691 * If there is a valid write delegation for this file, set the modtime
4692 * to the local clock time.
4695 nfscl_delegmodtime(vnode_t vp)
4697 struct nfsclclient *clp;
4698 struct nfscldeleg *dp;
4699 struct nfsnode *np = VTONFS(vp);
4700 struct nfsmount *nmp;
4702 nmp = VFSTONFS(vnode_mount(vp));
4703 if (!NFSHASNFSV4(nmp))
4706 clp = nfscl_findcl(nmp);
4711 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4712 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4713 nanotime(&dp->nfsdl_modtime);
4714 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4720 * If there is a valid write delegation for this file with a modtime set,
4721 * put that modtime in mtime.
4724 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4726 struct nfsclclient *clp;
4727 struct nfscldeleg *dp;
4728 struct nfsnode *np = VTONFS(vp);
4729 struct nfsmount *nmp;
4731 nmp = VFSTONFS(vnode_mount(vp));
4732 if (!NFSHASNFSV4(nmp))
4735 clp = nfscl_findcl(nmp);
4740 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4742 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4743 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4744 *mtime = dp->nfsdl_modtime;
4749 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
4751 short *defaulterrp, *errp;
4753 if (!nd->nd_repstat)
4755 if (nd->nd_procnum == NFSPROC_NOOP)
4756 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4757 if (nd->nd_repstat == EBADRPC)
4758 return (txdr_unsigned(NFSERR_BADXDR));
4759 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4760 nd->nd_repstat == NFSERR_OPILLEGAL)
4761 return (txdr_unsigned(nd->nd_repstat));
4762 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
4763 minorvers > NFSV4_MINORVERSION) {
4764 /* NFSv4.n error. */
4765 return (txdr_unsigned(nd->nd_repstat));
4767 if (nd->nd_procnum < NFSV4OP_CBNOPS)
4768 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4770 return (txdr_unsigned(nd->nd_repstat));
4772 if (*errp == (short)nd->nd_repstat)
4773 return (txdr_unsigned(nd->nd_repstat));
4774 return (txdr_unsigned(*defaulterrp));
4778 * Called to find/add a layout to a client.
4779 * This function returns the layout with a refcnt (shared lock) upon
4780 * success (returns 0) or with no lock/refcnt on the layout when an
4781 * error is returned.
4782 * If a layout is passed in via lypp, it is locked (exclusively locked).
4785 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4786 nfsv4stateid_t *stateidp, int layouttype, int retonclose,
4787 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
4788 struct ucred *cred, NFSPROC_T *p)
4790 struct nfsclclient *clp;
4791 struct nfscllayout *lyp, *tlyp;
4792 struct nfsclflayout *flp;
4793 struct nfsnode *np = VTONFS(vp);
4795 int layout_passed_in;
4797 mp = nmp->nm_mountp;
4798 layout_passed_in = 1;
4802 layout_passed_in = 0;
4803 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
4810 if (layout_passed_in != 0)
4811 nfsv4_unlock(&lyp->nfsly_lock, 0);
4814 free(tlyp, M_NFSLAYOUT);
4819 * Although no lyp was passed in, another thread might have
4820 * allocated one. If one is found, just increment it's ref
4821 * count and return it.
4823 lyp = nfscl_findlayout(clp, fhp, fhlen);
4827 lyp->nfsly_stateid.seqid = stateidp->seqid;
4828 lyp->nfsly_stateid.other[0] = stateidp->other[0];
4829 lyp->nfsly_stateid.other[1] = stateidp->other[1];
4830 lyp->nfsly_stateid.other[2] = stateidp->other[2];
4831 lyp->nfsly_lastbyte = 0;
4832 LIST_INIT(&lyp->nfsly_flayread);
4833 LIST_INIT(&lyp->nfsly_flayrw);
4834 LIST_INIT(&lyp->nfsly_recall);
4835 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
4836 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
4837 lyp->nfsly_clp = clp;
4838 if (layouttype == NFSLAYOUT_FLEXFILE)
4839 lyp->nfsly_flags = NFSLY_FLEXFILE;
4841 lyp->nfsly_flags = NFSLY_FILES;
4842 if (retonclose != 0)
4843 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4844 lyp->nfsly_fhlen = fhlen;
4845 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
4846 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4847 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
4849 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4852 if (retonclose != 0)
4853 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4854 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4855 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4856 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4858 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
4859 if (NFSCL_FORCEDISM(mp)) {
4862 free(tlyp, M_NFSLAYOUT);
4867 lyp->nfsly_stateid.seqid = stateidp->seqid;
4869 /* Merge the new list of File Layouts into the list. */
4870 flp = LIST_FIRST(fhlp);
4872 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
4873 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
4875 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
4877 if (layout_passed_in != 0)
4878 nfsv4_unlock(&lyp->nfsly_lock, 1);
4881 free(tlyp, M_NFSLAYOUT);
4886 * Search for a layout by MDS file handle.
4887 * If one is found, it is returned with a refcnt (shared lock) iff
4888 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
4891 struct nfscllayout *
4892 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
4893 uint64_t off, struct nfsclflayout **retflpp, int *recalledp)
4895 struct nfscllayout *lyp;
4897 int error, igotlock;
4899 mp = clp->nfsc_nmp->nm_mountp;
4903 lyp = nfscl_findlayout(clp, fhp, fhlen);
4905 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
4906 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4907 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4908 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4909 error = nfscl_findlayoutforio(lyp, off,
4910 NFSV4OPEN_ACCESSREAD, retflpp);
4912 nfsv4_getref(&lyp->nfsly_lock, NULL,
4913 NFSCLSTATEMUTEXPTR, mp);
4916 igotlock = nfsv4_lock(&lyp->nfsly_lock,
4917 1, NULL, NFSCLSTATEMUTEXPTR, mp);
4918 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
4921 if (NFSCL_FORCEDISM(mp)) {
4935 * Search for a layout by MDS file handle. If one is found, mark in to be
4936 * recalled, if it already marked "return on close".
4939 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
4940 int fhlen, struct nfsclrecalllayout **recallpp)
4942 struct nfscllayout *lyp;
4945 if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vnode_mount(vp))) ||
4946 nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
4947 (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
4949 lyp = nfscl_findlayout(clp, fhp, fhlen);
4950 if (lyp != NULL && (lyp->nfsly_flags & (NFSLY_RETONCLOSE |
4951 NFSLY_RECALL)) == NFSLY_RETONCLOSE) {
4953 if (!LIST_EMPTY(&lyp->nfsly_flayread))
4954 iomode |= NFSLAYOUTIOMODE_READ;
4955 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
4956 iomode |= NFSLAYOUTIOMODE_RW;
4957 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
4958 0, UINT64_MAX, lyp->nfsly_stateid.seqid, *recallpp);
4959 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
4965 * Dereference a layout.
4968 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
4973 nfsv4_unlock(&lyp->nfsly_lock, 0);
4975 nfsv4_relref(&lyp->nfsly_lock);
4980 * Search for a devinfo by deviceid. If one is found, return it after
4981 * acquiring a reference count on it.
4983 struct nfscldevinfo *
4984 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
4985 struct nfscldevinfo *dip)
4990 dip = nfscl_finddevinfo(clp, deviceid);
4992 dip->nfsdi_refcnt++;
4998 * Dereference a devinfo structure.
5001 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
5004 dip->nfsdi_refcnt--;
5005 if (dip->nfsdi_refcnt == 0)
5006 wakeup(&dip->nfsdi_refcnt);
5010 * Dereference a devinfo structure.
5013 nfscl_reldevinfo(struct nfscldevinfo *dip)
5017 nfscl_reldevinfo_locked(dip);
5022 * Find a layout for this file handle. Return NULL upon failure.
5024 static struct nfscllayout *
5025 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
5027 struct nfscllayout *lyp;
5029 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
5030 if (lyp->nfsly_fhlen == fhlen &&
5031 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
5037 * Find a devinfo for this deviceid. Return NULL upon failure.
5039 static struct nfscldevinfo *
5040 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
5042 struct nfscldevinfo *dip;
5044 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
5045 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5052 * Merge the new file layout list into the main one, maintaining it in
5053 * increasing offset order.
5056 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5057 struct nfsclflayouthead *newfhlp)
5059 struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5061 flp = LIST_FIRST(fhlp);
5063 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5064 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5066 flp = LIST_NEXT(flp, nfsfl_list);
5068 if (prevflp == NULL)
5069 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5071 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5077 * Add this nfscldevinfo to the client, if it doesn't already exist.
5078 * This function consumes the structure pointed at by dip, if not NULL.
5081 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip,
5082 struct nfsclflayout *flp)
5084 struct nfsclclient *clp;
5085 struct nfscldevinfo *tdip;
5093 free(dip, M_NFSDEVINFO);
5096 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5097 dev = flp->nfsfl_dev;
5099 dev = flp->nfsfl_ffm[0].dev;
5100 tdip = nfscl_finddevinfo(clp, dev);
5102 tdip->nfsdi_layoutrefs++;
5103 flp->nfsfl_devp = tdip;
5104 nfscl_reldevinfo_locked(tdip);
5107 free(dip, M_NFSDEVINFO);
5111 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5112 dip->nfsdi_layoutrefs = 1;
5113 flp->nfsfl_devp = dip;
5122 * Free up a layout structure and associated file layout structure(s).
5125 nfscl_freelayout(struct nfscllayout *layp)
5127 struct nfsclflayout *flp, *nflp;
5128 struct nfsclrecalllayout *rp, *nrp;
5130 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5131 LIST_REMOVE(flp, nfsfl_list);
5132 nfscl_freeflayout(flp);
5134 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5135 LIST_REMOVE(flp, nfsfl_list);
5136 nfscl_freeflayout(flp);
5138 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5139 LIST_REMOVE(rp, nfsrecly_list);
5140 free(rp, M_NFSLAYRECALL);
5143 free(layp, M_NFSLAYOUT);
5147 * Free up a file layout structure.
5150 nfscl_freeflayout(struct nfsclflayout *flp)
5154 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5155 for (i = 0; i < flp->nfsfl_fhcnt; i++)
5156 free(flp->nfsfl_fh[i], M_NFSFH);
5157 if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
5158 for (i = 0; i < flp->nfsfl_mirrorcnt; i++)
5159 for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
5160 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
5161 if (flp->nfsfl_devp != NULL)
5162 flp->nfsfl_devp->nfsdi_layoutrefs--;
5163 free(flp, M_NFSFLAYOUT);
5167 * Free up a file layout devinfo structure.
5170 nfscl_freedevinfo(struct nfscldevinfo *dip)
5173 free(dip, M_NFSDEVINFO);
5177 * Mark any layouts that match as recalled.
5180 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5181 uint64_t off, uint64_t len, uint32_t stateseqid,
5182 struct nfsclrecalllayout *recallp)
5184 struct nfsclrecalllayout *rp, *orp;
5186 recallp->nfsrecly_recalltype = recalltype;
5187 recallp->nfsrecly_iomode = iomode;
5188 recallp->nfsrecly_stateseqid = stateseqid;
5189 recallp->nfsrecly_off = off;
5190 recallp->nfsrecly_len = len;
5192 * Order the list as file returns first, followed by fsid and any
5193 * returns, both in increasing stateseqid order.
5194 * Note that the seqids wrap around, so 1 is after 0xffffffff.
5195 * (I'm not sure this is correct because I find RFC5661 confusing
5196 * on this, but hopefully it will work ok.)
5199 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5201 if ((recalltype == NFSLAYOUTRETURN_FILE &&
5202 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5203 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5204 (recalltype != NFSLAYOUTRETURN_FILE &&
5205 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5206 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5207 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5213 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5216 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5218 lyp->nfsly_flags |= NFSLY_RECALL;
5223 * Compare the two seqids for ordering. The trick is that the seqids can
5224 * wrap around from 0xffffffff->0, so check for the cases where one
5225 * has wrapped around.
5226 * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5229 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5232 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5233 /* seqid2 has wrapped around. */
5235 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5236 /* seqid1 has wrapped around. */
5238 if (seqid1 <= seqid2)
5244 * Do a layout return for each of the recalls.
5247 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5248 struct ucred *cred, NFSPROC_T *p)
5250 struct nfsclrecalllayout *rp;
5251 nfsv4stateid_t stateid;
5254 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5255 stateid.seqid = lyp->nfsly_stateid.seqid;
5256 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5257 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5259 layouttype = NFSLAYOUT_FLEXFILE;
5260 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5261 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5262 lyp->nfsly_fhlen, 0, layouttype,
5263 rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5264 rp->nfsrecly_off, rp->nfsrecly_len,
5265 &stateid, cred, p, NULL);
5270 * Do the layout commit for a file layout.
5273 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5274 struct ucred *cred, NFSPROC_T *p)
5276 struct nfsclflayout *flp;
5278 int error, layouttype;
5280 if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5281 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5283 layouttype = NFSLAYOUT_FLEXFILE;
5284 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5285 if (layouttype == NFSLAYOUT_FLEXFILE &&
5286 (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
5287 NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
5288 /* If not supported, don't bother doing it. */
5290 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5293 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5294 len = flp->nfsfl_end - flp->nfsfl_off;
5295 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5296 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5297 lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5298 layouttype, cred, p, NULL);
5299 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5300 if (error == NFSERR_NOTSUPP) {
5301 /* If not supported, don't bother doing it. */
5303 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5312 * Commit all layouts for a file (vnode).
5315 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5317 struct nfsclclient *clp;
5318 struct nfscllayout *lyp;
5319 struct nfsnode *np = VTONFS(vp);
5321 struct nfsmount *nmp;
5323 mp = vnode_mount(vp);
5325 if (NFSHASNOLAYOUTCOMMIT(nmp))
5333 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5338 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5339 if (NFSCL_FORCEDISM(mp)) {
5344 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5345 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5347 NFSCL_DEBUG(4, "do layoutcommit2\n");
5348 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5352 nfsv4_relref(&lyp->nfsly_lock);