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 nfsstats newnfsstats;
88 extern struct nfsreqhead nfsd_reqq;
89 extern u_int32_t newnfs_false, newnfs_true;
90 extern int nfscl_debuglevel;
94 struct nfsclhead nfsclhead; /* Head of clientid list */
95 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
96 int nfscl_layouthighwater = NFSCLLAYOUTHIGHWATER;
97 #endif /* !APPLEKEXT */
99 static int nfscl_delegcnt = 0;
100 static int nfscl_layoutcnt = 0;
101 static int nfscl_getopen(struct nfsclownerhead *, u_int8_t *, int, u_int8_t *,
102 u_int8_t *, u_int32_t, struct nfscllockowner **, struct nfsclopen **);
103 static void nfscl_clrelease(struct nfsclclient *);
104 static void nfscl_cleanclient(struct nfsclclient *);
105 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
106 struct ucred *, NFSPROC_T *);
107 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
108 struct nfsmount *, struct ucred *, NFSPROC_T *);
109 static void nfscl_recover(struct nfsclclient *, struct ucred *, NFSPROC_T *);
110 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
111 struct nfscllock *, int);
112 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
113 struct nfscllock **, int);
114 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *);
115 static u_int32_t nfscl_nextcbident(void);
116 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
117 static struct nfsclclient *nfscl_getclnt(u_int32_t);
118 static struct nfsclclient *nfscl_getclntsess(uint8_t *);
119 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
121 static void nfscl_retoncloselayout(struct nfsclclient *, uint8_t *, int);
122 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
123 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
125 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
126 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
127 u_int8_t *, struct nfscllock **);
128 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
129 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
130 struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
131 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
132 struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
133 struct nfsclopen **, u_int8_t *, u_int8_t *, int, int *);
134 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
135 struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
136 struct nfscldeleg *, struct ucred *, NFSPROC_T *);
137 static void nfscl_totalrecall(struct nfsclclient *);
138 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
139 struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
140 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
141 u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
142 struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
143 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
144 int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
145 struct ucred *, NFSPROC_T *);
146 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
147 struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
148 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *);
149 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
150 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
151 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
152 struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int);
153 static void nfscl_freeopenowner(struct nfsclowner *, int);
154 static void nfscl_cleandeleg(struct nfscldeleg *);
155 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
156 struct nfsmount *, NFSPROC_T *);
157 static void nfscl_emptylockowner(struct nfscllockowner *,
158 struct nfscllockownerfhhead *);
159 static void nfscl_mergeflayouts(struct nfsclflayouthead *,
160 struct nfsclflayouthead *);
161 static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t,
162 uint64_t, uint32_t, struct nfsclrecalllayout *);
163 static int nfscl_seq(uint32_t, uint32_t);
164 static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *,
165 struct ucred *, NFSPROC_T *);
166 static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *,
167 struct ucred *, NFSPROC_T *);
169 static short nfscberr_null[] = {
174 static short nfscberr_getattr[] = {
183 static short nfscberr_recall[] = {
193 static short *nfscl_cberrmap[] = {
201 #define NETFAMILY(clp) \
202 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
205 * Called for an open operation.
206 * If the nfhp argument is NULL, just get an openowner.
209 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
210 struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
211 struct nfsclopen **opp, int *newonep, int *retp, int lockit)
213 struct nfsclclient *clp;
214 struct nfsclowner *owp, *nowp;
215 struct nfsclopen *op = NULL, *nop = NULL;
216 struct nfscldeleg *dp;
217 struct nfsclownerhead *ohp;
218 u_int8_t own[NFSV4CL_LOCKNAMELEN];
229 * Might need one or both of these, so MALLOC them now, to
230 * avoid a tsleep() in MALLOC later.
232 MALLOC(nowp, struct nfsclowner *, sizeof (struct nfsclowner),
233 M_NFSCLOWNER, M_WAITOK);
235 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
236 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
237 ret = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
239 FREE((caddr_t)nowp, M_NFSCLOWNER);
241 FREE((caddr_t)nop, M_NFSCLOPEN);
246 * Get the Open iff it already exists.
247 * If none found, add the new one or return error, depending upon
252 /* First check the delegation list */
253 if (nfhp != NULL && usedeleg) {
254 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
255 if (dp->nfsdl_fhlen == fhlen &&
256 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
257 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
258 (dp->nfsdl_flags & NFSCLDL_WRITE))
267 nfscl_filllockowner(p->td_proc, own, F_POSIX);
268 ohp = &dp->nfsdl_owner;
270 /* For NFSv4.1 and this option, use a single open_owner. */
271 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
272 nfscl_filllockowner(NULL, own, F_POSIX);
274 nfscl_filllockowner(p->td_proc, own, F_POSIX);
275 ohp = &clp->nfsc_owner;
277 /* Now, search for an openowner */
278 LIST_FOREACH(owp, ohp, nfsow_list) {
279 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
284 * Create a new open, as required.
286 nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
290 * Now, check the mode on the open and return the appropriate
294 if (nfhp != NULL && dp != NULL && nop == NULL)
295 /* new local open on delegation */
296 *retp = NFSCLOPEN_SETCRED;
298 *retp = NFSCLOPEN_OK;
300 if (op != NULL && (amode & ~(op->nfso_mode))) {
301 op->nfso_mode |= amode;
302 if (retp != NULL && dp == NULL)
303 *retp = NFSCLOPEN_DOOPEN;
307 * Serialize modifications to the open owner for multiple threads
308 * within the same process using a read/write sleep lock.
309 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
310 * by acquiring a shared lock. The close operations still use an
311 * exclusive lock for this case.
314 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp)))) {
316 * Get a shared lock on the OpenOwner, but first
317 * wait for any pending exclusive lock, so that the
318 * exclusive locker gets priority.
320 nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
321 NFSCLSTATEMUTEXPTR, NULL);
322 nfsv4_getref(&owp->nfsow_rwlock, NULL,
323 NFSCLSTATEMUTEXPTR, NULL);
325 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
329 FREE((caddr_t)nowp, M_NFSCLOWNER);
331 FREE((caddr_t)nop, M_NFSCLOPEN);
340 * Create a new open, as required.
343 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
344 struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
345 struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
348 struct nfsclowner *owp = *owpp, *nowp;
349 struct nfsclopen *op, *nop;
359 if (owp == NULL && nowp != NULL) {
360 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
361 LIST_INIT(&nowp->nfsow_open);
362 nowp->nfsow_clp = clp;
363 nowp->nfsow_seqid = 0;
364 nowp->nfsow_defunct = 0;
365 nfscl_lockinit(&nowp->nfsow_rwlock);
367 newnfsstats.cllocalopenowners++;
368 LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
370 newnfsstats.clopenowners++;
371 LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
379 /* If an fhp has been specified, create an Open as well. */
381 /* and look for the correct open, based upon FH */
382 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
383 if (op->nfso_fhlen == fhlen &&
384 !NFSBCMP(op->nfso_fh, fhp, fhlen))
387 if (op == NULL && nop != NULL) {
390 nop->nfso_opencnt = 0;
391 nop->nfso_posixlock = 1;
392 nop->nfso_fhlen = fhlen;
393 NFSBCOPY(fhp, nop->nfso_fh, fhlen);
394 LIST_INIT(&nop->nfso_lock);
395 nop->nfso_stateid.seqid = 0;
396 nop->nfso_stateid.other[0] = 0;
397 nop->nfso_stateid.other[1] = 0;
398 nop->nfso_stateid.other[2] = 0;
400 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
401 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
403 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
404 newnfsstats.cllocalopens++;
406 newnfsstats.clopens++;
408 LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
420 * Called to find/add a delegation to a client.
423 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
424 int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
426 struct nfscldeleg *dp = *dpp, *tdp;
429 * First, if we have received a Read delegation for a file on a
430 * read/write file system, just return it, because they aren't
433 if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
434 (dp->nfsdl_flags & NFSCLDL_READ)) {
435 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
436 FREE((caddr_t)dp, M_NFSCLDELEG);
441 /* Look for the correct deleg, based upon FH */
443 tdp = nfscl_finddeleg(clp, nfhp, fhlen);
447 return (NFSERR_BADSTATEID);
450 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
451 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
453 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
454 newnfsstats.cldelegates++;
458 * Delegation already exists, what do we do if a new one??
461 printf("Deleg already exists!\n");
462 FREE((caddr_t)dp, M_NFSCLDELEG);
473 * Find a delegation for this file handle. Return NULL upon failure.
475 static struct nfscldeleg *
476 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
478 struct nfscldeleg *dp;
480 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
481 if (dp->nfsdl_fhlen == fhlen &&
482 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
489 * Get a stateid for an I/O operation. First, look for an open and iff
490 * found, return either a lockowner stateid or the open stateid.
491 * If no Open is found, just return error and the special stateid of all zeros.
494 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
495 int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
498 struct nfsclclient *clp;
499 struct nfsclowner *owp;
500 struct nfsclopen *op = NULL;
501 struct nfscllockowner *lp;
502 struct nfscldeleg *dp;
504 u_int8_t own[NFSV4CL_LOCKNAMELEN];
509 * Initially, just set the special stateid of all zeros.
510 * (Don't do this for a DS, since the special stateid can't be used.)
514 stateidp->other[0] = 0;
515 stateidp->other[1] = 0;
516 stateidp->other[2] = 0;
518 if (vnode_vtype(vp) != VREG)
522 clp = nfscl_findcl(VFSTONFS(vnode_mount(vp)));
529 * Wait for recovery to complete.
531 while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
532 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
533 PZERO, "nfsrecvr", NULL);
536 * First, look for a delegation.
538 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
539 if (dp->nfsdl_fhlen == fhlen &&
540 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
541 if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
542 (dp->nfsdl_flags & NFSCLDL_WRITE)) {
543 stateidp->seqid = dp->nfsdl_stateid.seqid;
544 stateidp->other[0] = dp->nfsdl_stateid.other[0];
545 stateidp->other[1] = dp->nfsdl_stateid.other[1];
546 stateidp->other[2] = dp->nfsdl_stateid.other[2];
547 if (!(np->n_flag & NDELEGRECALL)) {
548 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
550 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
552 dp->nfsdl_timestamp = NFSD_MONOSEC +
554 dp->nfsdl_rwlock.nfslock_usecnt++;
555 *lckpp = (void *)&dp->nfsdl_rwlock;
566 * If p != NULL, we want to search the parentage tree
567 * for a matching OpenOwner and use that.
569 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
570 nfscl_filllockowner(NULL, own, F_POSIX);
572 nfscl_filllockowner(p->td_proc, own, F_POSIX);
574 error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, own, own,
576 if (error == 0 && lp != NULL && fords == 0) {
577 /* Don't return a lock stateid for a DS. */
579 lp->nfsl_stateid.seqid;
581 lp->nfsl_stateid.other[0];
583 lp->nfsl_stateid.other[1];
585 lp->nfsl_stateid.other[2];
591 /* If not found, just look for any OpenOwner that will work. */
593 owp = LIST_FIRST(&clp->nfsc_owner);
594 while (!done && owp != NULL) {
595 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
596 if (op->nfso_fhlen == fhlen &&
597 !NFSBCMP(op->nfso_fh, nfhp, fhlen) &&
598 (mode & op->nfso_mode) == mode) {
604 owp = LIST_NEXT(owp, nfsow_list);
611 * For read aheads or write behinds, use the open cred.
612 * A read ahead or write behind is indicated by p == NULL.
615 newnfs_copycred(&op->nfso_cred, cred);
619 * No lock stateid, so return the open stateid.
621 stateidp->seqid = op->nfso_stateid.seqid;
622 stateidp->other[0] = op->nfso_stateid.other[0];
623 stateidp->other[1] = op->nfso_stateid.other[1];
624 stateidp->other[2] = op->nfso_stateid.other[2];
630 * Search for a matching file, mode and, optionally, lockowner.
633 nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
634 u_int8_t *openown, u_int8_t *lockown, u_int32_t mode,
635 struct nfscllockowner **lpp, struct nfsclopen **opp)
637 struct nfsclowner *owp;
638 struct nfsclopen *op, *rop, *rop2;
639 struct nfscllockowner *lp;
645 * rop will be set to the open to be returned. There are three
646 * variants of this, all for an open of the correct file:
647 * 1 - A match of lockown.
648 * 2 - A match of the openown, when no lockown match exists.
649 * 3 - A match for any open, if no openown or lockown match exists.
650 * Looking for #2 over #3 probably isn't necessary, but since
651 * RFC3530 is vague w.r.t. the relationship between openowners and
652 * lockowners, I think this is the safer way to go.
657 /* Search the client list */
658 owp = LIST_FIRST(ohp);
659 while (owp != NULL && keep_looping != 0) {
660 /* and look for the correct open */
661 op = LIST_FIRST(&owp->nfsow_open);
662 while (op != NULL && keep_looping != 0) {
663 if (op->nfso_fhlen == fhlen &&
664 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
665 && (op->nfso_mode & mode) == mode) {
667 /* Now look for a matching lockowner. */
668 LIST_FOREACH(lp, &op->nfso_lock,
670 if (!NFSBCMP(lp->nfsl_owner,
672 NFSV4CL_LOCKNAMELEN)) {
680 if (rop == NULL && !NFSBCMP(owp->nfsow_owner,
681 openown, NFSV4CL_LOCKNAMELEN)) {
689 op = LIST_NEXT(op, nfso_list);
691 owp = LIST_NEXT(owp, nfsow_list);
702 * Release use of an open owner. Called when open operations are done
703 * with the open owner.
706 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
707 __unused int error, __unused int candelete, int unlocked)
714 if (NFSHASONEOPENOWN(nmp))
715 nfsv4_relref(&owp->nfsow_rwlock);
717 nfscl_lockunlock(&owp->nfsow_rwlock);
719 nfscl_clrelease(owp->nfsow_clp);
724 * Release use of an open structure under an open owner.
727 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
730 struct nfsclclient *clp;
731 struct nfsclowner *owp;
737 if (NFSHASONEOPENOWN(nmp))
738 nfsv4_relref(&owp->nfsow_rwlock);
740 nfscl_lockunlock(&owp->nfsow_rwlock);
741 clp = owp->nfsow_clp;
742 if (error && candelete && op->nfso_opencnt == 0)
743 nfscl_freeopen(op, 0);
744 nfscl_clrelease(clp);
749 * Called to get a clientid structure. It will optionally lock the
750 * client data structures to do the SetClientId/SetClientId_confirm,
751 * but will release that lock and return the clientid with a refernce
753 * If the "cred" argument is NULL, a new clientid should not be created.
754 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
756 * The start_renewthread argument tells nfscl_getcl() to start a renew
757 * thread if this creates a new clp.
758 * It always clpp with a reference count on it, unless returning an error.
761 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
762 int start_renewthread, struct nfsclclient **clpp)
764 struct nfsclclient *clp;
765 struct nfsclclient *newclp = NULL;
766 struct nfsmount *nmp;
767 char uuid[HOSTUUIDLEN];
768 int igotlock = 0, error, trystalecnt, clidinusedelay, i;
773 getcredhostuuid(cred, uuid, sizeof uuid);
774 idlen = strlen(uuid);
776 idlen += sizeof (u_int64_t);
778 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
779 MALLOC(newclp, struct nfsclclient *,
780 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
785 * If a forced dismount is already in progress, don't
786 * allocate a new clientid and get out now. For the case where
787 * clp != NULL, this is a harmless optimization.
789 if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
792 free(newclp, M_NFSCLCLIENT);
797 if (newclp == NULL) {
802 clp->nfsc_idlen = idlen;
803 LIST_INIT(&clp->nfsc_owner);
804 TAILQ_INIT(&clp->nfsc_deleg);
805 TAILQ_INIT(&clp->nfsc_layout);
806 LIST_INIT(&clp->nfsc_devinfo);
807 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
808 LIST_INIT(&clp->nfsc_deleghash[i]);
809 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
810 LIST_INIT(&clp->nfsc_layouthash[i]);
811 clp->nfsc_flags = NFSCLFLAGS_INITED;
812 clp->nfsc_clientidrev = 1;
813 clp->nfsc_cbident = nfscl_nextcbident();
814 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
816 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
820 if (start_renewthread != 0)
821 nfscl_start_renewthread(clp);
825 free(newclp, M_NFSCLCLIENT);
828 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
829 (mp->mnt_kern_flag & MNTK_UNMOUNTF) == 0)
830 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
831 NFSCLSTATEMUTEXPTR, mp);
834 * Call nfsv4_lock() with "iwantlock == 0" so that it will
835 * wait for a pending exclusive lock request. This gives the
836 * exclusive lock request priority over this shared lock
838 * An exclusive lock on nfsc_lock is used mainly for server
841 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, mp);
842 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
844 if (igotlock == 0 && (mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
846 * Both nfsv4_lock() and nfsv4_getref() know to check
847 * for MNTK_UNMOUNTF and return without sleeping to
848 * wait for the exclusive lock to be released, since it
849 * might be held by nfscl_umount() and we need to get out
850 * now for that case and not wait until nfscl_umount()
859 * If it needs a clientid, do the setclientid now.
861 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
863 panic("nfscl_clget");
864 if (p == NULL || cred == NULL) {
866 nfsv4_unlock(&clp->nfsc_lock, 0);
871 * If RFC3530 Sec. 14.2.33 is taken literally,
872 * NFSERR_CLIDINUSE will be returned persistently for the
873 * case where a new mount of the same file system is using
874 * a different principal. In practice, NFSERR_CLIDINUSE is
875 * only returned when there is outstanding unexpired state
876 * on the clientid. As such, try for twice the lease
877 * interval, if we know what that is. Otherwise, make a
879 * The case of returning NFSERR_STALECLIENTID is far less
880 * likely, but might occur if there is a significant delay
881 * between doing the SetClientID and SetClientIDConfirm Ops,
882 * such that the server throws away the clientid before
883 * receiving the SetClientIDConfirm.
885 if (clp->nfsc_renew > 0)
886 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
888 clidinusedelay = 120;
891 error = nfsrpc_setclient(nmp, clp, 0, cred, p);
892 if (error == NFSERR_STALECLIENTID ||
893 error == NFSERR_STALEDONTRECOVER ||
894 error == NFSERR_BADSESSION ||
895 error == NFSERR_CLIDINUSE) {
896 (void) nfs_catnap(PZERO, error, "nfs_setcl");
898 } while (((error == NFSERR_STALECLIENTID ||
899 error == NFSERR_BADSESSION ||
900 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
901 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
904 nfsv4_unlock(&clp->nfsc_lock, 0);
908 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
912 nfsv4_unlock(&clp->nfsc_lock, 1);
921 * Get a reference to a clientid and return it, if valid.
923 APPLESTATIC struct nfsclclient *
924 nfscl_findcl(struct nfsmount *nmp)
926 struct nfsclclient *clp;
929 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
935 * Release the clientid structure. It may be locked or reference counted.
938 nfscl_clrelease(struct nfsclclient *clp)
941 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
942 nfsv4_unlock(&clp->nfsc_lock, 0);
944 nfsv4_relref(&clp->nfsc_lock);
948 * External call for nfscl_clrelease.
951 nfscl_clientrelease(struct nfsclclient *clp)
955 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
956 nfsv4_unlock(&clp->nfsc_lock, 0);
958 nfsv4_relref(&clp->nfsc_lock);
963 * Called when wanting to lock a byte region.
966 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
967 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
968 int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
969 struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
971 struct nfscllockowner *lp;
972 struct nfsclopen *op;
973 struct nfsclclient *clp;
974 struct nfscllockowner *nlp;
975 struct nfscllock *nlop, *otherlop;
976 struct nfscldeleg *dp = NULL, *ldp = NULL;
977 struct nfscllockownerhead *lhp = NULL;
979 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
981 int error = 0, ret, donelocally = 0;
984 /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
993 * Might need these, so MALLOC them now, to
994 * avoid a tsleep() in MALLOC later.
996 MALLOC(nlp, struct nfscllockowner *,
997 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
998 MALLOC(otherlop, struct nfscllock *,
999 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1000 MALLOC(nlop, struct nfscllock *,
1001 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1002 nlop->nfslo_type = type;
1003 nlop->nfslo_first = off;
1004 if (len == NFS64BITSSET) {
1005 nlop->nfslo_end = NFS64BITSSET;
1007 nlop->nfslo_end = off + len;
1008 if (nlop->nfslo_end <= nlop->nfslo_first)
1009 error = NFSERR_INVAL;
1016 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
1019 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1020 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1021 FREE((caddr_t)nlop, M_NFSCLLOCK);
1028 openownp = ropenownp;
1030 nfscl_filllockowner(id, own, flags);
1032 if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
1033 nfscl_filllockowner(NULL, openown, F_POSIX);
1035 nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1041 * First, search for a delegation. If one exists for this file,
1042 * the lock can be done locally against it, so long as there
1043 * isn't a local lock conflict.
1045 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1046 np->n_fhp->nfh_len);
1047 /* Just sanity check for correct type of delegation */
1048 if (dp != NULL && ((dp->nfsdl_flags &
1049 (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1051 (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1055 /* Now, find an open and maybe a lockowner. */
1056 ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
1057 np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1059 ret = nfscl_getopen(&clp->nfsc_owner,
1060 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1061 ownp, mode, NULL, &op);
1063 lhp = &dp->nfsdl_lock;
1064 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1065 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1066 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1074 * Get the related Open and maybe lockowner.
1076 error = nfscl_getopen(&clp->nfsc_owner,
1077 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1078 ownp, mode, &lp, &op);
1080 lhp = &op->nfso_lock;
1082 if (!error && !recovery)
1083 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1084 np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1087 nfscl_clrelease(clp);
1090 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1091 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1092 FREE((caddr_t)nlop, M_NFSCLLOCK);
1097 * Ok, see if a lockowner exists and create one, as required.
1100 LIST_FOREACH(lp, lhp, nfsl_list) {
1101 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1105 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1107 NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1108 NFSV4CL_LOCKNAMELEN);
1110 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1111 NFSV4CL_LOCKNAMELEN);
1112 nlp->nfsl_seqid = 0;
1113 nlp->nfsl_lockflags = flags;
1114 nlp->nfsl_inprog = NULL;
1115 nfscl_lockinit(&nlp->nfsl_rwlock);
1116 LIST_INIT(&nlp->nfsl_lock);
1118 nlp->nfsl_open = NULL;
1119 newnfsstats.cllocallockowners++;
1121 nlp->nfsl_open = op;
1122 newnfsstats.cllockowners++;
1124 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1131 * Now, update the byte ranges for locks.
1133 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1139 nfscl_clrelease(clp);
1142 * Serial modifications on the lock owner for multiple threads
1143 * for the same process using a read/write lock.
1146 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1152 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1154 FREE((caddr_t)nlop, M_NFSCLLOCK);
1156 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1163 * Called to unlock a byte range, for LockU.
1166 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1167 __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1168 struct nfsclclient *clp, void *id, int flags,
1169 struct nfscllockowner **lpp, int *dorpcp)
1171 struct nfscllockowner *lp;
1172 struct nfsclowner *owp;
1173 struct nfsclopen *op;
1174 struct nfscllock *nlop, *other_lop = NULL;
1175 struct nfscldeleg *dp;
1177 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1185 * Might need these, so MALLOC them now, to
1186 * avoid a tsleep() in MALLOC later.
1188 MALLOC(nlop, struct nfscllock *,
1189 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1190 nlop->nfslo_type = F_UNLCK;
1191 nlop->nfslo_first = off;
1192 if (len == NFS64BITSSET) {
1193 nlop->nfslo_end = NFS64BITSSET;
1195 nlop->nfslo_end = off + len;
1196 if (nlop->nfslo_end <= nlop->nfslo_first) {
1197 FREE((caddr_t)nlop, M_NFSCLLOCK);
1198 return (NFSERR_INVAL);
1202 MALLOC(other_lop, struct nfscllock *,
1203 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1206 nfscl_filllockowner(id, own, flags);
1210 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1211 np->n_fhp->nfh_len);
1214 * First, unlock any local regions on a delegation.
1217 /* Look for this lockowner. */
1218 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1219 if (!NFSBCMP(lp->nfsl_owner, own,
1220 NFSV4CL_LOCKNAMELEN))
1224 /* Use other_lop, so nlop is still available */
1225 (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1229 * Now, find a matching open/lockowner that hasn't already been done,
1230 * as marked by nfsl_inprog.
1234 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1235 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1236 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1237 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1238 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1239 if (lp->nfsl_inprog == NULL &&
1240 !NFSBCMP(lp->nfsl_owner, own,
1241 NFSV4CL_LOCKNAMELEN)) {
1255 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1259 * Serial modifications on the lock owner for multiple
1260 * threads for the same process using a read/write lock.
1262 lp->nfsl_inprog = p;
1263 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1268 FREE((caddr_t)nlop, M_NFSCLLOCK);
1270 FREE((caddr_t)other_lop, M_NFSCLLOCK);
1275 * Release all lockowners marked in progess for this process and file.
1278 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1279 void *id, int flags)
1281 struct nfsclowner *owp;
1282 struct nfsclopen *op;
1283 struct nfscllockowner *lp;
1285 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1288 nfscl_filllockowner(id, own, flags);
1290 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1291 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1292 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1293 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1294 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1295 if (lp->nfsl_inprog == p &&
1296 !NFSBCMP(lp->nfsl_owner, own,
1297 NFSV4CL_LOCKNAMELEN)) {
1298 lp->nfsl_inprog = NULL;
1299 nfscl_lockunlock(&lp->nfsl_rwlock);
1305 nfscl_clrelease(clp);
1310 * Called to find out if any bytes within the byte range specified are
1311 * write locked by the calling process. Used to determine if flushing
1312 * is required before a LockU.
1313 * If in doubt, return 1, so the flush will occur.
1316 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1317 struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1319 struct nfsclowner *owp;
1320 struct nfscllockowner *lp;
1321 struct nfsclopen *op;
1322 struct nfsclclient *clp;
1323 struct nfscllock *lop;
1324 struct nfscldeleg *dp;
1327 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1331 switch (fl->l_whence) {
1335 * Caller is responsible for adding any necessary offset
1336 * when SEEK_CUR is used.
1341 off = np->n_size + fl->l_start;
1346 if (fl->l_len != 0) {
1347 end = off + fl->l_len;
1354 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
1357 nfscl_filllockowner(id, own, flags);
1361 * First check the delegation locks.
1363 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1365 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1366 if (!NFSBCMP(lp->nfsl_owner, own,
1367 NFSV4CL_LOCKNAMELEN))
1371 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1372 if (lop->nfslo_first >= end)
1374 if (lop->nfslo_end <= off)
1376 if (lop->nfslo_type == F_WRLCK) {
1377 nfscl_clrelease(clp);
1386 * Now, check state against the server.
1388 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1389 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1390 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1391 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1392 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1393 if (!NFSBCMP(lp->nfsl_owner, own,
1394 NFSV4CL_LOCKNAMELEN))
1398 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1399 if (lop->nfslo_first >= end)
1401 if (lop->nfslo_end <= off)
1403 if (lop->nfslo_type == F_WRLCK) {
1404 nfscl_clrelease(clp);
1413 nfscl_clrelease(clp);
1419 * Release a byte range lock owner structure.
1422 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1424 struct nfsclclient *clp;
1429 clp = lp->nfsl_open->nfso_own->nfsow_clp;
1430 if (error != 0 && candelete &&
1431 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1432 nfscl_freelockowner(lp, 0);
1434 nfscl_lockunlock(&lp->nfsl_rwlock);
1435 nfscl_clrelease(clp);
1440 * Free up an open structure and any associated byte range lock structures.
1443 nfscl_freeopen(struct nfsclopen *op, int local)
1446 LIST_REMOVE(op, nfso_list);
1447 nfscl_freealllocks(&op->nfso_lock, local);
1448 FREE((caddr_t)op, M_NFSCLOPEN);
1450 newnfsstats.cllocalopens--;
1452 newnfsstats.clopens--;
1456 * Free up all lock owners and associated locks.
1459 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1461 struct nfscllockowner *lp, *nlp;
1463 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1464 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1466 nfscl_freelockowner(lp, local);
1471 * Called for an Open when NFSERR_EXPIRED is received from the server.
1472 * If there are no byte range locks nor a Share Deny lost, try to do a
1473 * fresh Open. Otherwise, free the open.
1476 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1477 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1479 struct nfscllockowner *lp;
1480 struct nfscldeleg *dp;
1481 int mustdelete = 0, error;
1484 * Look for any byte range lock(s).
1486 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1487 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1494 * If no byte range lock(s) nor a Share deny, try to re-open.
1496 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1497 newnfs_copycred(&op->nfso_cred, cred);
1499 error = nfsrpc_reopen(nmp, op->nfso_fh,
1500 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1504 FREE((caddr_t)dp, M_NFSCLDELEG);
1509 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1510 op->nfso_fhlen, cred, p, &dp);
1514 * If a byte range lock or Share deny or couldn't re-open, free it.
1517 nfscl_freeopen(op, 0);
1518 return (mustdelete);
1522 * Free up an open owner structure.
1525 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1528 LIST_REMOVE(owp, nfsow_list);
1529 FREE((caddr_t)owp, M_NFSCLOWNER);
1531 newnfsstats.cllocalopenowners--;
1533 newnfsstats.clopenowners--;
1537 * Free up a byte range lock owner structure.
1540 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1542 struct nfscllock *lop, *nlop;
1544 LIST_REMOVE(lp, nfsl_list);
1545 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1546 nfscl_freelock(lop, local);
1548 FREE((caddr_t)lp, M_NFSCLLOCKOWNER);
1550 newnfsstats.cllocallockowners--;
1552 newnfsstats.cllockowners--;
1556 * Free up a byte range lock structure.
1559 nfscl_freelock(struct nfscllock *lop, int local)
1562 LIST_REMOVE(lop, nfslo_list);
1563 FREE((caddr_t)lop, M_NFSCLLOCK);
1565 newnfsstats.cllocallocks--;
1567 newnfsstats.cllocks--;
1571 * Clean out the state related to a delegation.
1574 nfscl_cleandeleg(struct nfscldeleg *dp)
1576 struct nfsclowner *owp, *nowp;
1577 struct nfsclopen *op;
1579 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1580 op = LIST_FIRST(&owp->nfsow_open);
1582 if (LIST_NEXT(op, nfso_list) != NULL)
1583 panic("nfscleandel");
1584 nfscl_freeopen(op, 1);
1586 nfscl_freeopenowner(owp, 1);
1588 nfscl_freealllocks(&dp->nfsdl_lock, 1);
1592 * Free a delegation.
1595 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp)
1598 TAILQ_REMOVE(hdp, dp, nfsdl_list);
1599 LIST_REMOVE(dp, nfsdl_hash);
1600 FREE((caddr_t)dp, M_NFSCLDELEG);
1601 newnfsstats.cldelegates--;
1606 * Free up all state related to this client structure.
1609 nfscl_cleanclient(struct nfsclclient *clp)
1611 struct nfsclowner *owp, *nowp;
1612 struct nfsclopen *op, *nop;
1614 /* Now, all the OpenOwners, etc. */
1615 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1616 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1617 nfscl_freeopen(op, 0);
1619 nfscl_freeopenowner(owp, 0);
1624 * Called when an NFSERR_EXPIRED is received from the server.
1627 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1628 struct ucred *cred, NFSPROC_T *p)
1630 struct nfsclowner *owp, *nowp, *towp;
1631 struct nfsclopen *op, *nop, *top;
1632 struct nfscldeleg *dp, *ndp;
1633 int ret, printed = 0;
1636 * First, merge locally issued Opens into the list for the server.
1638 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1639 while (dp != NULL) {
1640 ndp = TAILQ_NEXT(dp, nfsdl_list);
1641 owp = LIST_FIRST(&dp->nfsdl_owner);
1642 while (owp != NULL) {
1643 nowp = LIST_NEXT(owp, nfsow_list);
1644 op = LIST_FIRST(&owp->nfsow_open);
1646 if (LIST_NEXT(op, nfso_list) != NULL)
1648 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1649 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1650 NFSV4CL_LOCKNAMELEN))
1654 /* Merge opens in */
1655 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1656 if (top->nfso_fhlen == op->nfso_fhlen &&
1657 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1659 top->nfso_mode |= op->nfso_mode;
1660 top->nfso_opencnt += op->nfso_opencnt;
1665 /* Just add the open to the owner list */
1666 LIST_REMOVE(op, nfso_list);
1667 op->nfso_own = towp;
1668 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1669 newnfsstats.cllocalopens--;
1670 newnfsstats.clopens++;
1673 /* Just add the openowner to the client list */
1674 LIST_REMOVE(owp, nfsow_list);
1675 owp->nfsow_clp = clp;
1676 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1677 newnfsstats.cllocalopenowners--;
1678 newnfsstats.clopenowners++;
1679 newnfsstats.cllocalopens--;
1680 newnfsstats.clopens++;
1685 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1687 printf("nfsv4 expired locks lost\n");
1689 nfscl_cleandeleg(dp);
1690 nfscl_freedeleg(&clp->nfsc_deleg, dp);
1693 if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1697 * Now, try and reopen against the server.
1699 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1700 owp->nfsow_seqid = 0;
1701 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1702 ret = nfscl_expireopen(clp, op, nmp, cred, p);
1703 if (ret && !printed) {
1705 printf("nfsv4 expired locks lost\n");
1708 if (LIST_EMPTY(&owp->nfsow_open))
1709 nfscl_freeopenowner(owp, 0);
1714 * This function must be called after the process represented by "own" has
1715 * exited. Must be called with CLSTATE lock held.
1718 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1720 struct nfsclowner *owp, *nowp;
1721 struct nfscllockowner *lp, *nlp;
1722 struct nfscldeleg *dp;
1724 /* First, get rid of local locks on delegations. */
1725 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1726 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1727 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1728 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1730 nfscl_freelockowner(lp, 1);
1734 owp = LIST_FIRST(&clp->nfsc_owner);
1735 while (owp != NULL) {
1736 nowp = LIST_NEXT(owp, nfsow_list);
1737 if (!NFSBCMP(owp->nfsow_owner, own,
1738 NFSV4CL_LOCKNAMELEN)) {
1740 * If there are children that haven't closed the
1741 * file descriptors yet, the opens will still be
1742 * here. For that case, let the renew thread clear
1743 * out the OpenOwner later.
1745 if (LIST_EMPTY(&owp->nfsow_open))
1746 nfscl_freeopenowner(owp, 0);
1748 owp->nfsow_defunct = 1;
1755 * Find open/lock owners for processes that have exited.
1758 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1760 struct nfsclowner *owp, *nowp;
1761 struct nfsclopen *op;
1762 struct nfscllockowner *lp, *nlp;
1763 struct nfscldeleg *dp;
1767 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1768 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1769 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1770 if (LIST_EMPTY(&lp->nfsl_lock))
1771 nfscl_emptylockowner(lp, lhp);
1774 if (nfscl_procdoesntexist(owp->nfsow_owner))
1775 nfscl_cleanup_common(clp, owp->nfsow_owner);
1779 * For the single open_owner case, these lock owners need to be
1780 * checked to see if they still exist separately.
1781 * This is because nfscl_procdoesntexist() never returns true for
1782 * the single open_owner so that the above doesn't ever call
1783 * nfscl_cleanup_common().
1785 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1786 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1787 if (nfscl_procdoesntexist(lp->nfsl_owner))
1788 nfscl_cleanup_common(clp, lp->nfsl_owner);
1792 NFSPROCLISTUNLOCK();
1796 * Take the empty lock owner and move it to the local lhp list if the
1797 * associated process no longer exists.
1800 nfscl_emptylockowner(struct nfscllockowner *lp,
1801 struct nfscllockownerfhhead *lhp)
1803 struct nfscllockownerfh *lfhp, *mylfhp;
1804 struct nfscllockowner *nlp;
1807 /* If not a Posix lock owner, just return. */
1808 if ((lp->nfsl_lockflags & F_POSIX) == 0)
1814 * First, search to see if this lock owner is already in the list.
1815 * If it is, then the associated process no longer exists.
1817 SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1818 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1819 !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
1822 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
1823 if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
1824 NFSV4CL_LOCKNAMELEN))
1827 /* If not found, check if process still exists. */
1828 if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
1831 /* Move the lock owner over to the local list. */
1832 if (mylfhp == NULL) {
1833 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
1837 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
1838 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
1839 mylfhp->nfslfh_len);
1840 LIST_INIT(&mylfhp->nfslfh_lock);
1841 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
1843 LIST_REMOVE(lp, nfsl_list);
1844 LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
1847 static int fake_global; /* Used to force visibility of MNTK_UNMOUNTF */
1849 * Called from nfs umount to free up the clientid.
1852 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1854 struct nfsclclient *clp;
1859 * For the case that matters, this is the thread that set
1860 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
1861 * done to ensure that any thread executing nfscl_getcl() after
1862 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
1863 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
1864 * explanation, courtesy of Alan Cox.
1865 * What follows is a snippet from Alan Cox's email at:
1866 * http://docs.FreeBSD.org/cgi/
1867 * mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
1869 * 1. Set MNTK_UNMOUNTF
1870 * 2. Acquire a standard FreeBSD mutex "m".
1871 * 3. Update some data structures.
1872 * 4. Release mutex "m".
1874 * Then, other threads that acquire "m" after step 4 has occurred will
1875 * see MNTK_UNMOUNTF as set. But, other threads that beat thread X to
1876 * step 2 may or may not see MNTK_UNMOUNTF as set.
1879 if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
1887 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1888 panic("nfscl umount");
1891 * First, handshake with the nfscl renew thread, to terminate
1894 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1895 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1896 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
1900 * Now, get the exclusive lock on the client state, so
1901 * that no uses of the state are still in progress.
1904 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1905 NFSCLSTATEMUTEXPTR, NULL);
1906 } while (!igotlock);
1910 * Free up all the state. It will expire on the server, but
1911 * maybe we should do a SetClientId/SetClientIdConfirm so
1912 * the server throws it away?
1914 LIST_REMOVE(clp, nfsc_list);
1915 nfscl_delegreturnall(clp, p);
1916 cred = newnfs_getcred();
1917 if (NFSHASNFSV4N(nmp)) {
1918 (void)nfsrpc_destroysession(nmp, clp, cred, p);
1919 (void)nfsrpc_destroyclient(nmp, clp, cred, p);
1921 (void)nfsrpc_setclient(nmp, clp, 0, cred, p);
1922 nfscl_cleanclient(clp);
1925 free(clp, M_NFSCLCLIENT);
1931 * This function is called when a server replies with NFSERR_STALECLIENTID
1932 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
1933 * doing Opens and Locks with reclaim. If these fail, it deletes the
1934 * corresponding state.
1937 nfscl_recover(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
1939 struct nfsclowner *owp, *nowp;
1940 struct nfsclopen *op, *nop;
1941 struct nfscllockowner *lp, *nlp;
1942 struct nfscllock *lop, *nlop;
1943 struct nfscldeleg *dp, *ndp, *tdp;
1944 struct nfsmount *nmp;
1945 struct ucred *tcred;
1946 struct nfsclopenhead extra_open;
1947 struct nfscldeleghead extra_deleg;
1950 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
1951 int i, igotlock = 0, error, trycnt, firstlock;
1952 struct nfscllayout *lyp, *nlyp;
1955 * First, lock the client structure, so everyone else will
1956 * block when trying to use state.
1959 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
1961 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1962 NFSCLSTATEMUTEXPTR, NULL);
1963 } while (!igotlock);
1966 nmp = clp->nfsc_nmp;
1968 panic("nfscl recover");
1971 * For now, just get rid of all layouts. There may be a need
1972 * to do LayoutCommit Ops with reclaim == true later.
1974 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1975 nfscl_freelayout(lyp);
1976 TAILQ_INIT(&clp->nfsc_layout);
1977 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
1978 LIST_INIT(&clp->nfsc_layouthash[i]);
1982 error = nfsrpc_setclient(nmp, clp, 1, cred, p);
1983 } while ((error == NFSERR_STALECLIENTID ||
1984 error == NFSERR_BADSESSION ||
1985 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
1988 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
1989 NFSCLFLAGS_RECVRINPROG);
1990 wakeup(&clp->nfsc_flags);
1991 nfsv4_unlock(&clp->nfsc_lock, 0);
1995 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
1996 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
1999 * Mark requests already queued on the server, so that they don't
2000 * initiate another recovery cycle. Any requests already in the
2001 * queue that handle state information will have the old stale
2002 * clientid/stateid and will get a NFSERR_STALESTATEID,
2003 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2004 * This will be translated to NFSERR_STALEDONTRECOVER when
2005 * R_DONTRECOVER is set.
2008 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2009 if (rep->r_nmp == nmp)
2010 rep->r_flags |= R_DONTRECOVER;
2015 * Now, mark all delegations "need reclaim".
2017 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2018 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2020 TAILQ_INIT(&extra_deleg);
2021 LIST_INIT(&extra_open);
2023 * Now traverse the state lists, doing Open and Lock Reclaims.
2025 tcred = newnfs_getcred();
2026 owp = LIST_FIRST(&clp->nfsc_owner);
2027 while (owp != NULL) {
2028 nowp = LIST_NEXT(owp, nfsow_list);
2029 owp->nfsow_seqid = 0;
2030 op = LIST_FIRST(&owp->nfsow_open);
2031 while (op != NULL) {
2032 nop = LIST_NEXT(op, nfso_list);
2033 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2034 /* Search for a delegation to reclaim with the open */
2035 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2036 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2038 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2039 mode = NFSV4OPEN_ACCESSWRITE;
2040 delegtype = NFSV4OPEN_DELEGATEWRITE;
2042 mode = NFSV4OPEN_ACCESSREAD;
2043 delegtype = NFSV4OPEN_DELEGATEREAD;
2045 if ((op->nfso_mode & mode) == mode &&
2046 op->nfso_fhlen == dp->nfsdl_fhlen &&
2047 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2052 delegtype = NFSV4OPEN_DELEGATENONE;
2053 newnfs_copycred(&op->nfso_cred, tcred);
2054 error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2055 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2056 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2059 /* Handle any replied delegation */
2060 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2061 || NFSMNT_RDONLY(nmp->nm_mountp))) {
2062 if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2063 mode = NFSV4OPEN_ACCESSWRITE;
2065 mode = NFSV4OPEN_ACCESSREAD;
2066 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2067 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2069 if ((op->nfso_mode & mode) == mode &&
2070 op->nfso_fhlen == dp->nfsdl_fhlen &&
2071 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2073 dp->nfsdl_stateid = ndp->nfsdl_stateid;
2074 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2075 dp->nfsdl_ace = ndp->nfsdl_ace;
2076 dp->nfsdl_change = ndp->nfsdl_change;
2077 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2078 if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2079 dp->nfsdl_flags |= NFSCLDL_RECALL;
2080 FREE((caddr_t)ndp, M_NFSCLDELEG);
2087 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2089 /* and reclaim all byte range locks */
2090 lp = LIST_FIRST(&op->nfso_lock);
2091 while (lp != NULL) {
2092 nlp = LIST_NEXT(lp, nfsl_list);
2095 lop = LIST_FIRST(&lp->nfsl_lock);
2096 while (lop != NULL) {
2097 nlop = LIST_NEXT(lop, nfslo_list);
2098 if (lop->nfslo_end == NFS64BITSSET)
2101 len = lop->nfslo_end - lop->nfslo_first;
2102 error = nfscl_trylock(nmp, NULL,
2103 op->nfso_fh, op->nfso_fhlen, lp,
2104 firstlock, 1, lop->nfslo_first, len,
2105 lop->nfslo_type, tcred, p);
2107 nfscl_freelock(lop, 0);
2112 /* If no locks, but a lockowner, just delete it. */
2113 if (LIST_EMPTY(&lp->nfsl_lock))
2114 nfscl_freelockowner(lp, 0);
2119 if (error != 0 && error != NFSERR_BADSESSION)
2120 nfscl_freeopen(op, 0);
2127 * Now, try and get any delegations not yet reclaimed by cobbling
2128 * to-gether an appropriate open.
2131 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2132 while (dp != NULL) {
2133 ndp = TAILQ_NEXT(dp, nfsdl_list);
2134 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2136 MALLOC(nowp, struct nfsclowner *,
2137 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2139 * Name must be as long an largest possible
2140 * NFSV4CL_LOCKNAMELEN. 12 for now.
2142 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2143 NFSV4CL_LOCKNAMELEN);
2144 LIST_INIT(&nowp->nfsow_open);
2145 nowp->nfsow_clp = clp;
2146 nowp->nfsow_seqid = 0;
2147 nowp->nfsow_defunct = 0;
2148 nfscl_lockinit(&nowp->nfsow_rwlock);
2151 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2152 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
2153 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2154 nop->nfso_own = nowp;
2155 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2156 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2157 delegtype = NFSV4OPEN_DELEGATEWRITE;
2159 nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2160 delegtype = NFSV4OPEN_DELEGATEREAD;
2162 nop->nfso_opencnt = 0;
2163 nop->nfso_posixlock = 1;
2164 nop->nfso_fhlen = dp->nfsdl_fhlen;
2165 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2166 LIST_INIT(&nop->nfso_lock);
2167 nop->nfso_stateid.seqid = 0;
2168 nop->nfso_stateid.other[0] = 0;
2169 nop->nfso_stateid.other[1] = 0;
2170 nop->nfso_stateid.other[2] = 0;
2171 newnfs_copycred(&dp->nfsdl_cred, tcred);
2172 newnfs_copyincred(tcred, &nop->nfso_cred);
2174 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2175 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2176 nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2177 delegtype, tcred, p);
2179 if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2180 mode = NFSV4OPEN_ACCESSWRITE;
2182 mode = NFSV4OPEN_ACCESSREAD;
2183 if ((nop->nfso_mode & mode) == mode &&
2184 nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2185 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2187 dp->nfsdl_stateid = tdp->nfsdl_stateid;
2188 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2189 dp->nfsdl_ace = tdp->nfsdl_ace;
2190 dp->nfsdl_change = tdp->nfsdl_change;
2191 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2192 if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2193 dp->nfsdl_flags |= NFSCLDL_RECALL;
2194 FREE((caddr_t)tdp, M_NFSCLDELEG);
2196 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2202 FREE((caddr_t)nop, M_NFSCLOPEN);
2204 * Couldn't reclaim it, so throw the state
2207 nfscl_cleandeleg(dp);
2208 nfscl_freedeleg(&clp->nfsc_deleg, dp);
2210 LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2217 * Now, get rid of extra Opens and Delegations.
2219 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2221 newnfs_copycred(&op->nfso_cred, tcred);
2222 error = nfscl_tryclose(op, tcred, nmp, p);
2223 if (error == NFSERR_GRACE)
2224 (void) nfs_catnap(PZERO, error, "nfsexcls");
2225 } while (error == NFSERR_GRACE);
2226 LIST_REMOVE(op, nfso_list);
2227 FREE((caddr_t)op, M_NFSCLOPEN);
2230 FREE((caddr_t)nowp, M_NFSCLOWNER);
2232 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2234 newnfs_copycred(&dp->nfsdl_cred, tcred);
2235 error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2236 if (error == NFSERR_GRACE)
2237 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2238 } while (error == NFSERR_GRACE);
2239 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2240 FREE((caddr_t)dp, M_NFSCLDELEG);
2243 /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2244 if (NFSHASNFSV4N(nmp))
2245 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
2248 clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2249 wakeup(&clp->nfsc_flags);
2250 nfsv4_unlock(&clp->nfsc_lock, 0);
2256 * This function is called when a server replies with NFSERR_EXPIRED.
2257 * It deletes all state for the client and does a fresh SetClientId/confirm.
2258 * XXX Someday it should post a signal to the process(es) that hold the
2259 * state, so they know that lock state has been lost.
2262 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2264 struct nfsmount *nmp;
2266 int igotlock = 0, error, trycnt;
2269 * If the clientid has gone away or a new SetClientid has already
2270 * been done, just return ok.
2272 if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2276 * First, lock the client structure, so everyone else will
2277 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2278 * that only one thread does the work.
2281 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2283 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2284 NFSCLSTATEMUTEXPTR, NULL);
2285 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2286 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2288 nfsv4_unlock(&clp->nfsc_lock, 0);
2292 clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2295 nmp = clp->nfsc_nmp;
2297 panic("nfscl expired");
2298 cred = newnfs_getcred();
2301 error = nfsrpc_setclient(nmp, clp, 0, cred, p);
2302 } while ((error == NFSERR_STALECLIENTID ||
2303 error == NFSERR_BADSESSION ||
2304 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2307 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2310 * Expire the state for the client.
2312 nfscl_expireclient(clp, nmp, cred, p);
2314 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2315 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2317 clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2318 wakeup(&clp->nfsc_flags);
2319 nfsv4_unlock(&clp->nfsc_lock, 0);
2326 * This function inserts a lock in the list after insert_lop.
2329 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2330 struct nfscllock *insert_lop, int local)
2333 if ((struct nfscllockowner *)insert_lop == lp)
2334 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2336 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2338 newnfsstats.cllocallocks++;
2340 newnfsstats.cllocks++;
2344 * This function updates the locking for a lock owner and given file. It
2345 * maintains a list of lock ranges ordered on increasing file offset that
2346 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2347 * It always adds new_lop to the list and sometimes uses the one pointed
2349 * Returns 1 if the locks were modified, 0 otherwise.
2352 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2353 struct nfscllock **other_lopp, int local)
2355 struct nfscllock *new_lop = *new_lopp;
2356 struct nfscllock *lop, *tlop, *ilop;
2357 struct nfscllock *other_lop;
2358 int unlock = 0, modified = 0;
2362 * Work down the list until the lock is merged.
2364 if (new_lop->nfslo_type == F_UNLCK)
2366 ilop = (struct nfscllock *)lp;
2367 lop = LIST_FIRST(&lp->nfsl_lock);
2368 while (lop != NULL) {
2370 * Only check locks for this file that aren't before the start of
2373 if (lop->nfslo_end >= new_lop->nfslo_first) {
2374 if (new_lop->nfslo_end < lop->nfslo_first) {
2376 * If the new lock ends before the start of the
2377 * current lock's range, no merge, just insert
2382 if (new_lop->nfslo_type == lop->nfslo_type ||
2383 (new_lop->nfslo_first <= lop->nfslo_first &&
2384 new_lop->nfslo_end >= lop->nfslo_end)) {
2386 * This lock can be absorbed by the new lock/unlock.
2387 * This happens when it covers the entire range
2388 * of the old lock or is contiguous
2389 * with the old lock and is of the same type or an
2392 if (new_lop->nfslo_type != lop->nfslo_type ||
2393 new_lop->nfslo_first != lop->nfslo_first ||
2394 new_lop->nfslo_end != lop->nfslo_end)
2396 if (lop->nfslo_first < new_lop->nfslo_first)
2397 new_lop->nfslo_first = lop->nfslo_first;
2398 if (lop->nfslo_end > new_lop->nfslo_end)
2399 new_lop->nfslo_end = lop->nfslo_end;
2401 lop = LIST_NEXT(lop, nfslo_list);
2402 nfscl_freelock(tlop, local);
2407 * All these cases are for contiguous locks that are not the
2408 * same type, so they can't be merged.
2410 if (new_lop->nfslo_first <= lop->nfslo_first) {
2412 * This case is where the new lock overlaps with the
2413 * first part of the old lock. Move the start of the
2414 * old lock to just past the end of the new lock. The
2415 * new lock will be inserted in front of the old, since
2416 * ilop hasn't been updated. (We are done now.)
2418 if (lop->nfslo_first != new_lop->nfslo_end) {
2419 lop->nfslo_first = new_lop->nfslo_end;
2424 if (new_lop->nfslo_end >= lop->nfslo_end) {
2426 * This case is where the new lock overlaps with the
2427 * end of the old lock's range. Move the old lock's
2428 * end to just before the new lock's first and insert
2429 * the new lock after the old lock.
2430 * Might not be done yet, since the new lock could
2431 * overlap further locks with higher ranges.
2433 if (lop->nfslo_end != new_lop->nfslo_first) {
2434 lop->nfslo_end = new_lop->nfslo_first;
2438 lop = LIST_NEXT(lop, nfslo_list);
2442 * The final case is where the new lock's range is in the
2443 * middle of the current lock's and splits the current lock
2444 * up. Use *other_lopp to handle the second part of the
2445 * split old lock range. (We are done now.)
2446 * For unlock, we use new_lop as other_lop and tmp, since
2447 * other_lop and new_lop are the same for this case.
2448 * We noted the unlock case above, so we don't need
2449 * new_lop->nfslo_type any longer.
2451 tmp = new_lop->nfslo_first;
2453 other_lop = new_lop;
2456 other_lop = *other_lopp;
2459 other_lop->nfslo_first = new_lop->nfslo_end;
2460 other_lop->nfslo_end = lop->nfslo_end;
2461 other_lop->nfslo_type = lop->nfslo_type;
2462 lop->nfslo_end = tmp;
2463 nfscl_insertlock(lp, other_lop, lop, local);
2469 lop = LIST_NEXT(lop, nfslo_list);
2475 * Insert the new lock in the list at the appropriate place.
2478 nfscl_insertlock(lp, new_lop, ilop, local);
2486 * This function must be run as a kernel thread.
2487 * It does Renew Ops and recovery, when required.
2490 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2492 struct nfsclowner *owp, *nowp;
2493 struct nfsclopen *op;
2494 struct nfscllockowner *lp, *nlp;
2495 struct nfscldeleghead dh;
2496 struct nfscldeleg *dp, *ndp;
2499 int error, cbpathdown, islept, igotlock, ret, clearok;
2500 uint32_t recover_done_time = 0;
2502 static time_t prevsec = 0;
2503 struct nfscllockownerfh *lfhp, *nlfhp;
2504 struct nfscllockownerfhhead lfh;
2505 struct nfscllayout *lyp, *nlyp;
2506 struct nfscldevinfo *dip, *ndip;
2507 struct nfscllayouthead rlh;
2508 struct nfsclrecalllayout *recallp;
2509 struct nfsclds *dsp;
2511 cred = newnfs_getcred();
2513 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2516 newnfs_setroot(cred);
2518 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2520 * Only allow one recover within 1/2 of the lease
2521 * duration (nfsc_renew).
2523 if (recover_done_time < NFSD_MONOSEC) {
2524 recover_done_time = NFSD_MONOSEC +
2526 NFSCL_DEBUG(1, "Doing recovery..\n");
2527 nfscl_recover(clp, cred, p);
2529 NFSCL_DEBUG(1, "Clear Recovery dt=%u ms=%jd\n",
2530 recover_done_time, (intmax_t)NFSD_MONOSEC);
2532 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2536 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2537 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2538 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2539 clidrev = clp->nfsc_clientidrev;
2540 error = nfsrpc_renew(clp, NULL, cred, p);
2541 if (error == NFSERR_CBPATHDOWN)
2543 else if (error == NFSERR_STALECLIENTID ||
2544 error == NFSERR_BADSESSION) {
2546 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2548 } else if (error == NFSERR_EXPIRED)
2549 (void) nfscl_hasexpired(clp, clidrev, p);
2553 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2554 /* Do renews for any DS sessions. */
2555 NFSLOCKMNT(clp->nfsc_nmp);
2556 /* Skip first entry, since the MDS is handled above. */
2557 dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2559 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2560 while (dsp != NULL) {
2561 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2562 dsp->nfsclds_sess.nfsess_defunct == 0) {
2563 dsp->nfsclds_expire = NFSD_MONOSEC +
2565 NFSUNLOCKMNT(clp->nfsc_nmp);
2566 (void)nfsrpc_renew(clp, dsp, cred, p);
2569 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2571 NFSUNLOCKMNT(clp->nfsc_nmp);
2577 /* It's a Total Recall! */
2578 nfscl_totalrecall(clp);
2581 * Now, handle defunct owners.
2583 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2584 if (LIST_EMPTY(&owp->nfsow_open)) {
2585 if (owp->nfsow_defunct != 0)
2586 nfscl_freeopenowner(owp, 0);
2591 * Do the recall on any delegations. To avoid trouble, always
2592 * come back up here after having slept.
2596 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2597 while (dp != NULL) {
2598 ndp = TAILQ_NEXT(dp, nfsdl_list);
2599 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2601 * Wait for outstanding I/O ops to be done.
2603 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2605 nfsv4_unlock(&clp->nfsc_lock, 0);
2608 dp->nfsdl_rwlock.nfslock_lock |=
2610 (void) nfsmsleep(&dp->nfsdl_rwlock,
2611 NFSCLSTATEMUTEXPTR, PZERO, "nfscld",
2616 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2617 &islept, NFSCLSTATEMUTEXPTR, NULL);
2622 newnfs_copycred(&dp->nfsdl_cred, cred);
2623 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2626 nfscl_cleandeleg(dp);
2627 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2629 LIST_REMOVE(dp, nfsdl_hash);
2630 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2632 newnfsstats.cldelegates--;
2640 * Clear out old delegations, if we are above the high water
2641 * mark. Only clear out ones with no state related to them.
2642 * The tailq list is in LRU order.
2644 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2645 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2646 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2647 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2648 dp->nfsdl_rwlock.nfslock_lock == 0 &&
2649 dp->nfsdl_timestamp < NFSD_MONOSEC &&
2650 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2651 NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2653 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2654 op = LIST_FIRST(&owp->nfsow_open);
2661 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2662 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2669 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2670 LIST_REMOVE(dp, nfsdl_hash);
2671 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2673 newnfsstats.cldelegates--;
2679 nfsv4_unlock(&clp->nfsc_lock, 0);
2682 * Do the recall on any layouts. To avoid trouble, always
2683 * come back up here after having slept.
2687 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2688 if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2690 * Wait for outstanding I/O ops to be done.
2692 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2693 (lyp->nfsly_lock.nfslock_lock &
2694 NFSV4LOCK_LOCK) != 0) {
2695 lyp->nfsly_lock.nfslock_lock |=
2697 (void)nfsmsleep(&lyp->nfsly_lock,
2698 NFSCLSTATEMUTEXPTR, PZERO, "nfslyp",
2702 /* Move the layout to the recall list. */
2703 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2705 LIST_REMOVE(lyp, nfsly_hash);
2706 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2708 /* Handle any layout commits. */
2709 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2710 (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2711 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2713 NFSCL_DEBUG(3, "do layoutcommit\n");
2714 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2722 /* Now, look for stale layouts. */
2723 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2724 while (lyp != NULL) {
2725 nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2726 if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2727 (lyp->nfsly_flags & NFSLY_RECALL) == 0 &&
2728 lyp->nfsly_lock.nfslock_usecnt == 0 &&
2729 lyp->nfsly_lock.nfslock_lock == 0) {
2730 NFSCL_DEBUG(4, "ret stale lay=%d\n",
2732 recallp = malloc(sizeof(*recallp),
2733 M_NFSLAYRECALL, M_NOWAIT);
2734 if (recallp == NULL)
2736 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2737 lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2738 lyp->nfsly_stateid.seqid, recallp);
2744 * Free up any unreferenced device info structures.
2746 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
2747 if (dip->nfsdi_layoutrefs == 0 &&
2748 dip->nfsdi_refcnt == 0) {
2749 NFSCL_DEBUG(4, "freeing devinfo\n");
2750 LIST_REMOVE(dip, nfsdi_list);
2751 nfscl_freedevinfo(dip);
2756 /* Do layout return(s), as required. */
2757 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
2758 TAILQ_REMOVE(&rlh, lyp, nfsly_list);
2759 NFSCL_DEBUG(4, "ret layout\n");
2760 nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
2761 nfscl_freelayout(lyp);
2765 * Delegreturn any delegations cleaned out or recalled.
2767 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2768 newnfs_copycred(&dp->nfsdl_cred, cred);
2769 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2770 TAILQ_REMOVE(&dh, dp, nfsdl_list);
2771 FREE((caddr_t)dp, M_NFSCLDELEG);
2776 * Call nfscl_cleanupkext() once per second to check for
2777 * open/lock owners where the process has exited.
2779 mytime = NFSD_MONOSEC;
2780 if (prevsec != mytime) {
2782 nfscl_cleanupkext(clp, &lfh);
2786 * Do a ReleaseLockOwner for all lock owners where the
2787 * associated process no longer exists, as found by
2788 * nfscl_cleanupkext().
2790 newnfs_setroot(cred);
2791 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
2792 LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
2794 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
2795 lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
2797 nfscl_freelockowner(lp, 0);
2804 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2805 (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
2807 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2808 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2811 wakeup((caddr_t)clp);
2819 * Initiate state recovery. Called when NFSERR_STALECLIENTID,
2820 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
2823 nfscl_initiate_recovery(struct nfsclclient *clp)
2829 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2831 wakeup((caddr_t)clp);
2835 * Dump out the state stuff for debugging.
2838 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
2839 int lockowner, int locks)
2841 struct nfsclclient *clp;
2842 struct nfsclowner *owp;
2843 struct nfsclopen *op;
2844 struct nfscllockowner *lp;
2845 struct nfscllock *lop;
2846 struct nfscldeleg *dp;
2850 printf("nfscl dumpstate NULL clp\n");
2854 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2855 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2856 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2857 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2858 owp->nfsow_owner[0], owp->nfsow_owner[1],
2859 owp->nfsow_owner[2], owp->nfsow_owner[3],
2861 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2863 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2864 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2865 op->nfso_stateid.other[2], op->nfso_opencnt,
2867 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2869 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2870 lp->nfsl_owner[0], lp->nfsl_owner[1],
2871 lp->nfsl_owner[2], lp->nfsl_owner[3],
2873 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2874 lp->nfsl_stateid.other[2]);
2875 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2878 printf("lck typ=%d fst=%ju end=%ju\n",
2879 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2880 (intmax_t)lop->nfslo_end);
2882 printf("lck typ=%d fst=%qd end=%qd\n",
2883 lop->nfslo_type, lop->nfslo_first,
2891 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2892 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2893 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2894 owp->nfsow_owner[0], owp->nfsow_owner[1],
2895 owp->nfsow_owner[2], owp->nfsow_owner[3],
2897 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2899 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2900 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2901 op->nfso_stateid.other[2], op->nfso_opencnt,
2903 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2905 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2906 lp->nfsl_owner[0], lp->nfsl_owner[1],
2907 lp->nfsl_owner[2], lp->nfsl_owner[3],
2909 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2910 lp->nfsl_stateid.other[2]);
2911 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2914 printf("lck typ=%d fst=%ju end=%ju\n",
2915 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2916 (intmax_t)lop->nfslo_end);
2918 printf("lck typ=%d fst=%qd end=%qd\n",
2919 lop->nfslo_type, lop->nfslo_first,
2930 * Check for duplicate open owners and opens.
2931 * (Only used as a diagnostic aid.)
2934 nfscl_dupopen(vnode_t vp, int dupopens)
2936 struct nfsclclient *clp;
2937 struct nfsclowner *owp, *owp2;
2938 struct nfsclopen *op, *op2;
2941 clp = VFSTONFS(vnode_mount(vp))->nm_clp;
2943 printf("nfscl dupopen NULL clp\n");
2946 nfhp = VTONFS(vp)->n_fhp;
2950 * First, search for duplicate owners.
2951 * These should never happen!
2953 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2954 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2956 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
2957 NFSV4CL_LOCKNAMELEN)) {
2959 printf("DUP OWNER\n");
2960 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0, 0);
2967 * Now, search for duplicate stateids.
2968 * These shouldn't happen, either.
2970 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2971 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
2972 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2973 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2975 (op->nfso_stateid.other[0] != 0 ||
2976 op->nfso_stateid.other[1] != 0 ||
2977 op->nfso_stateid.other[2] != 0) &&
2978 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
2979 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
2980 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
2982 printf("DUP STATEID\n");
2983 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0,
2993 * Now search for duplicate opens.
2994 * Duplicate opens for the same owner
2995 * should never occur. Other duplicates are
2996 * possible and are checked for if "dupopens"
2999 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3000 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3001 if (nfhp->nfh_len == op2->nfso_fhlen &&
3002 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3003 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3004 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3005 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3006 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3007 (!NFSBCMP(op->nfso_own->nfsow_owner,
3008 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3010 if (!NFSBCMP(op->nfso_own->nfsow_owner,
3011 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3013 printf("BADDUP OPEN\n");
3016 printf("DUP OPEN\n");
3018 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1,
3031 * During close, find an open that needs to be dereferenced and
3032 * dereference it. If there are no more opens for this file,
3033 * log a message to that effect.
3034 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3035 * on the file's vnode.
3036 * This is the safe way, since it is difficult to identify
3037 * which open the close is for and I/O can be performed after the
3038 * close(2) system call when a file is mmap'd.
3039 * If it returns 0 for success, there will be a referenced
3040 * clp returned via clpp.
3043 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3045 struct nfsclclient *clp;
3046 struct nfsclowner *owp;
3047 struct nfsclopen *op;
3048 struct nfscldeleg *dp;
3052 error = nfscl_getcl(vnode_mount(vp), NULL, NULL, 1, &clp);
3057 nfhp = VTONFS(vp)->n_fhp;
3061 * First, look for one under a delegation that was locally issued
3062 * and just decrement the opencnt for it. Since all my Opens against
3063 * the server are DENY_NONE, I don't see a problem with hanging
3064 * onto them. (It is much easier to use one of the extant Opens
3065 * that I already have on the server when a Delegation is recalled
3066 * than to do fresh Opens.) Someday, I might need to rethink this, but.
3068 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3070 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3071 op = LIST_FIRST(&owp->nfsow_open);
3074 * Since a delegation is for a file, there
3075 * should never be more than one open for
3078 if (LIST_NEXT(op, nfso_list) != NULL)
3079 panic("nfscdeleg opens");
3080 if (notdecr && op->nfso_opencnt > 0) {
3089 /* Now process the opens against the server. */
3090 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3091 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3092 if (op->nfso_fhlen == nfhp->nfh_len &&
3093 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3095 /* Found an open, decrement cnt if possible */
3096 if (notdecr && op->nfso_opencnt > 0) {
3101 * There are more opens, so just return.
3103 if (op->nfso_opencnt > 0) {
3112 printf("nfscl: never fnd open\n");
3117 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3119 struct nfsclclient *clp;
3120 struct nfsclowner *owp, *nowp;
3121 struct nfsclopen *op;
3122 struct nfscldeleg *dp;
3126 error = nfscl_getcl(vnode_mount(vp), NULL, NULL, 1, &clp);
3131 nfhp = VTONFS(vp)->n_fhp;
3134 * First get rid of the local Open structures, which should be no
3137 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3139 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3140 op = LIST_FIRST(&owp->nfsow_open);
3142 KASSERT((op->nfso_opencnt == 0),
3143 ("nfscl: bad open cnt on deleg"));
3144 nfscl_freeopen(op, 1);
3146 nfscl_freeopenowner(owp, 1);
3150 /* Return any layouts marked return on close. */
3151 nfscl_retoncloselayout(clp, nfhp->nfh_fh, nfhp->nfh_len);
3153 /* Now process the opens against the server. */
3155 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3156 op = LIST_FIRST(&owp->nfsow_open);
3157 while (op != NULL) {
3158 if (op->nfso_fhlen == nfhp->nfh_len &&
3159 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3161 /* Found an open, close it. */
3162 KASSERT((op->nfso_opencnt == 0),
3163 ("nfscl: bad open cnt on server"));
3165 nfsrpc_doclose(VFSTONFS(vnode_mount(vp)), op,
3170 op = LIST_NEXT(op, nfso_list);
3178 * Return all delegations on this client.
3179 * (Must be called with client sleep lock.)
3182 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
3184 struct nfscldeleg *dp, *ndp;
3187 cred = newnfs_getcred();
3188 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3189 nfscl_cleandeleg(dp);
3190 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3191 nfscl_freedeleg(&clp->nfsc_deleg, dp);
3197 * Do a callback RPC.
3200 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3202 int clist, gotseq_ok, i, j, k, op, rcalls;
3204 struct nfsclclient *clp;
3205 struct nfscldeleg *dp = NULL;
3206 int numops, taglen = -1, error = 0, trunc;
3207 u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3208 u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3214 nfsattrbit_t attrbits, rattrbits;
3215 nfsv4stateid_t stateid;
3216 uint32_t seqid, slotid = 0, highslot, cachethis;
3217 uint8_t sessionid[NFSX_V4SESSIONID];
3219 struct nfscllayout *lyp;
3220 uint64_t filesid[2], len, off;
3221 int changed, gotone, laytype, recalltype;
3223 struct nfsclrecalllayout *recallp = NULL;
3224 struct nfsclsession *tsep;
3228 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3229 taglen = fxdr_unsigned(int, *tl);
3234 if (taglen <= NFSV4_SMALLSTR)
3237 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3238 error = nfsrv_mtostr(nd, tagstr, taglen);
3240 if (taglen > NFSV4_SMALLSTR)
3241 free(tagstr, M_TEMP);
3245 (void) nfsm_strtom(nd, tag, taglen);
3246 if (taglen > NFSV4_SMALLSTR) {
3247 free(tagstr, M_TEMP);
3249 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3250 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3251 minorvers = fxdr_unsigned(u_int32_t, *tl++);
3252 if (minorvers != NFSV4_MINORVERSION && minorvers != NFSV41_MINORVERSION)
3253 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3254 cbident = fxdr_unsigned(u_int32_t, *tl++);
3258 numops = fxdr_unsigned(int, *tl);
3260 * Loop around doing the sub ops.
3262 for (i = 0; i < numops; i++) {
3263 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3264 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3266 op = fxdr_unsigned(int, *tl);
3267 if (op < NFSV4OP_CBGETATTR ||
3268 (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3269 (op > NFSV4OP_CBNOTIFYDEVID &&
3270 minorvers == NFSV41_MINORVERSION)) {
3271 nd->nd_repstat = NFSERR_OPILLEGAL;
3272 *repp = nfscl_errmap(nd, minorvers);
3276 nd->nd_procnum = op;
3277 if (op < NFSV4OP_CBNOPS)
3278 newnfsstats.cbrpccnt[nd->nd_procnum]++;
3280 case NFSV4OP_CBGETATTR:
3281 NFSCL_DEBUG(4, "cbgetattr\n");
3284 error = nfsm_getfh(nd, &nfhp);
3286 error = nfsrv_getattrbits(nd, &attrbits,
3288 if (error == 0 && i == 0 &&
3289 minorvers != NFSV4_MINORVERSION)
3290 error = NFSERR_OPNOTINSESS;
3292 mp = nfscl_getmnt(minorvers, sessionid, cbident,
3295 error = NFSERR_SERVERFAULT;
3298 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3299 nfhp->nfh_len, p, &np);
3304 NFSZERO_ATTRBIT(&rattrbits);
3306 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3309 if (NFSISSET_ATTRBIT(&attrbits,
3312 va.va_size = np->n_size;
3316 NFSSETBIT_ATTRBIT(&rattrbits,
3319 if (NFSISSET_ATTRBIT(&attrbits,
3320 NFSATTRBIT_CHANGE)) {
3324 (np->n_flag & NDELEGMOD))
3326 NFSSETBIT_ATTRBIT(&rattrbits,
3330 error = NFSERR_SERVERFAULT;
3338 FREE((caddr_t)nfhp, M_NFSFH);
3340 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3341 NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3344 case NFSV4OP_CBRECALL:
3345 NFSCL_DEBUG(4, "cbrecall\n");
3346 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3348 stateid.seqid = *tl++;
3349 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3351 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3352 trunc = fxdr_unsigned(int, *tl);
3353 error = nfsm_getfh(nd, &nfhp);
3354 if (error == 0 && i == 0 &&
3355 minorvers != NFSV4_MINORVERSION)
3356 error = NFSERR_OPNOTINSESS;
3359 if (minorvers == NFSV4_MINORVERSION)
3360 clp = nfscl_getclnt(cbident);
3362 clp = nfscl_getclntsess(sessionid);
3364 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3366 if (dp != NULL && (dp->nfsdl_flags &
3367 NFSCLDL_DELEGRET) == 0) {
3370 wakeup((caddr_t)clp);
3373 error = NFSERR_SERVERFAULT;
3378 FREE((caddr_t)nfhp, M_NFSFH);
3380 case NFSV4OP_CBLAYOUTRECALL:
3381 NFSCL_DEBUG(4, "cblayrec\n");
3383 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3384 laytype = fxdr_unsigned(int, *tl++);
3385 iomode = fxdr_unsigned(uint32_t, *tl++);
3386 if (newnfs_true == *tl++)
3390 recalltype = fxdr_unsigned(int, *tl);
3391 recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3393 if (laytype != NFSLAYOUT_NFSV4_1_FILES)
3394 error = NFSERR_NOMATCHLAYOUT;
3395 else if (recalltype == NFSLAYOUTRETURN_FILE) {
3396 error = nfsm_getfh(nd, &nfhp);
3397 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3400 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3402 off = fxdr_hyper(tl); tl += 2;
3403 len = fxdr_hyper(tl); tl += 2;
3404 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3405 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3406 if (minorvers == NFSV4_MINORVERSION)
3407 error = NFSERR_NOTSUPP;
3409 error = NFSERR_OPNOTINSESS;
3412 clp = nfscl_getclntsess(sessionid);
3413 NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3415 lyp = nfscl_findlayout(clp,
3418 NFSCL_DEBUG(4, "cblyp=%p\n",
3422 NFSLY_FILES) != 0 &&
3423 !NFSBCMP(stateid.other,
3424 lyp->nfsly_stateid.other,
3425 NFSX_STATEIDOTHER)) {
3439 NFSERR_NOMATCHLAYOUT;
3441 error = NFSERR_NOMATCHLAYOUT;
3444 free(nfhp, M_NFSFH);
3445 } else if (recalltype == NFSLAYOUTRETURN_FSID) {
3446 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3447 filesid[0] = fxdr_hyper(tl); tl += 2;
3448 filesid[1] = fxdr_hyper(tl); tl += 2;
3451 clp = nfscl_getclntsess(sessionid);
3453 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3455 if (lyp->nfsly_filesid[0] ==
3457 lyp->nfsly_filesid[1] ==
3464 lyp->nfsly_stateid.seqid,
3473 error = NFSERR_NOMATCHLAYOUT;
3475 error = NFSERR_NOMATCHLAYOUT;
3477 } else if (recalltype == NFSLAYOUTRETURN_ALL) {
3480 clp = nfscl_getclntsess(sessionid);
3482 TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3484 error = nfscl_layoutrecall(
3485 recalltype, lyp, iomode, 0,
3487 lyp->nfsly_stateid.seqid,
3495 error = NFSERR_NOMATCHLAYOUT;
3497 error = NFSERR_NOMATCHLAYOUT;
3500 error = NFSERR_NOMATCHLAYOUT;
3501 if (recallp != NULL) {
3502 free(recallp, M_NFSLAYRECALL);
3506 case NFSV4OP_CBSEQUENCE:
3507 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3509 bcopy(tl, sessionid, NFSX_V4SESSIONID);
3510 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3511 seqid = fxdr_unsigned(uint32_t, *tl++);
3512 slotid = fxdr_unsigned(uint32_t, *tl++);
3513 highslot = fxdr_unsigned(uint32_t, *tl++);
3515 /* Throw away the referring call stuff. */
3516 clist = fxdr_unsigned(int, *tl);
3517 for (j = 0; j < clist; j++) {
3518 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3520 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3521 rcalls = fxdr_unsigned(int, *tl);
3522 for (k = 0; k < rcalls; k++) {
3523 NFSM_DISSECT(tl, uint32_t *,
3529 clp = nfscl_getclntsess(sessionid);
3531 error = NFSERR_SERVERFAULT;
3533 error = NFSERR_SEQUENCEPOS;
3535 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3536 error = nfsv4_seqsession(seqid, slotid,
3537 highslot, tsep->nfsess_cbslots, &rep,
3538 tsep->nfsess_backslots);
3544 NFSCL_DEBUG(4, "Got cbretry\n");
3545 m_freem(nd->nd_mreq);
3550 NFSM_BUILD(tl, uint32_t *,
3551 NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3552 bcopy(sessionid, tl, NFSX_V4SESSIONID);
3553 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3554 *tl++ = txdr_unsigned(seqid);
3555 *tl++ = txdr_unsigned(slotid);
3556 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3557 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3561 if (i == 0 && minorvers == NFSV41_MINORVERSION)
3562 error = NFSERR_OPNOTINSESS;
3564 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3565 error = NFSERR_NOTSUPP;
3570 if (error == EBADRPC || error == NFSERR_BADXDR) {
3571 nd->nd_repstat = NFSERR_BADXDR;
3573 nd->nd_repstat = error;
3578 if (nd->nd_repstat) {
3579 *repp = nfscl_errmap(nd, minorvers);
3582 *repp = 0; /* NFS4_OK */
3585 if (recallp != NULL)
3586 free(recallp, M_NFSLAYRECALL);
3588 if (error == EBADRPC || error == NFSERR_BADXDR)
3589 nd->nd_repstat = NFSERR_BADXDR;
3591 printf("nfsv4 comperr1=%d\n", error);
3594 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3598 *retopsp = txdr_unsigned(retops);
3600 *nd->nd_errp = nfscl_errmap(nd, minorvers);
3602 if (gotseq_ok != 0) {
3603 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3605 clp = nfscl_getclntsess(sessionid);
3607 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3608 nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3619 * Generate the next cbident value. Basically just increment a static value
3620 * and then check that it isn't already in the list, if it has wrapped around.
3623 nfscl_nextcbident(void)
3625 struct nfsclclient *clp;
3627 static u_int32_t nextcbident = 0;
3628 static int haswrapped = 0;
3631 if (nextcbident == 0)
3635 * Search the clientid list for one already using this cbident.
3640 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3641 if (clp->nfsc_cbident == nextcbident) {
3651 return (nextcbident);
3655 * Get the mount point related to a given cbident or session and busy it.
3658 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
3659 struct nfsclclient **clpp)
3661 struct nfsclclient *clp;
3664 struct nfsclsession *tsep;
3668 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3669 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3670 if (minorvers == NFSV4_MINORVERSION) {
3671 if (clp->nfsc_cbident == cbident)
3673 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3681 mp = clp->nfsc_nmp->nm_mountp;
3684 error = vfs_busy(mp, 0);
3693 * Get the clientid pointer related to a given cbident.
3695 static struct nfsclclient *
3696 nfscl_getclnt(u_int32_t cbident)
3698 struct nfsclclient *clp;
3700 LIST_FOREACH(clp, &nfsclhead, nfsc_list)
3701 if (clp->nfsc_cbident == cbident)
3707 * Get the clientid pointer related to a given sessionid.
3709 static struct nfsclclient *
3710 nfscl_getclntsess(uint8_t *sessionid)
3712 struct nfsclclient *clp;
3713 struct nfsclsession *tsep;
3715 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3716 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3717 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3725 * Search for a lock conflict locally on the client. A conflict occurs if
3726 * - not same owner and overlapping byte range and at least one of them is
3727 * a write lock or this is an unlock.
3730 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
3731 struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
3732 struct nfscllock **lopp)
3734 struct nfsclowner *owp;
3735 struct nfsclopen *op;
3739 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3743 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3744 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3745 if (op->nfso_fhlen == fhlen &&
3746 !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
3747 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
3758 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3759 u_int8_t *own, struct nfscllock **lopp)
3761 struct nfscllockowner *lp;
3762 struct nfscllock *lop;
3764 LIST_FOREACH(lp, lhp, nfsl_list) {
3765 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
3766 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3767 if (lop->nfslo_first >= nlop->nfslo_end)
3769 if (lop->nfslo_end <= nlop->nfslo_first)
3771 if (lop->nfslo_type == F_WRLCK ||
3772 nlop->nfslo_type == F_WRLCK ||
3773 nlop->nfslo_type == F_UNLCK) {
3776 return (NFSERR_DENIED);
3785 * Check for a local conflicting lock.
3788 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
3789 u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
3791 struct nfscllock *lop, nlck;
3792 struct nfscldeleg *dp;
3794 u_int8_t own[NFSV4CL_LOCKNAMELEN];
3797 nlck.nfslo_type = fl->l_type;
3798 nlck.nfslo_first = off;
3799 if (len == NFS64BITSSET) {
3800 nlck.nfslo_end = NFS64BITSSET;
3802 nlck.nfslo_end = off + len;
3803 if (nlck.nfslo_end <= nlck.nfslo_first)
3804 return (NFSERR_INVAL);
3807 nfscl_filllockowner(id, own, flags);
3809 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3810 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
3811 &nlck, own, dp, &lop);
3813 fl->l_whence = SEEK_SET;
3814 fl->l_start = lop->nfslo_first;
3815 if (lop->nfslo_end == NFS64BITSSET)
3818 fl->l_len = lop->nfslo_end - lop->nfslo_first;
3819 fl->l_pid = (pid_t)0;
3820 fl->l_type = lop->nfslo_type;
3821 error = -1; /* no RPC required */
3822 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
3823 fl->l_type == F_RDLCK)) {
3825 * The delegation ensures that there isn't a conflicting
3826 * lock on the server, so return -1 to indicate an RPC
3829 fl->l_type = F_UNLCK;
3837 * Handle Recall of a delegation.
3838 * The clp must be exclusive locked when this is called.
3841 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
3842 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
3843 int called_from_renewthread)
3845 struct nfsclowner *owp, *lowp, *nowp;
3846 struct nfsclopen *op, *lop;
3847 struct nfscllockowner *lp;
3848 struct nfscllock *lckp;
3850 int error = 0, ret, gotvp = 0;
3854 * First, get a vnode for the file. This is needed to do RPCs.
3856 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
3857 dp->nfsdl_fhlen, p, &np);
3860 * File isn't open, so nothing to move over to the
3870 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
3873 * Ok, if it's a write delegation, flush data to the server, so
3874 * that close/open consistency is retained.
3878 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
3879 np->n_flag |= NDELEGRECALL;
3881 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
3883 np->n_flag &= ~NDELEGRECALL;
3885 NFSINVALATTRCACHE(np);
3887 if (ret == EIO && called_from_renewthread != 0) {
3889 * If the flush failed with EIO for the renew thread,
3890 * return now, so that the dirty buffer will be flushed
3899 * Now, for each openowner with opens issued locally, move them
3900 * over to state against the server.
3902 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
3903 lop = LIST_FIRST(&lowp->nfsow_open);
3905 if (LIST_NEXT(lop, nfso_list) != NULL)
3906 panic("nfsdlg mult opens");
3908 * Look for the same openowner against the server.
3910 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3911 if (!NFSBCMP(lowp->nfsow_owner,
3912 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3913 newnfs_copycred(&dp->nfsdl_cred, cred);
3914 ret = nfscl_moveopen(vp, clp, nmp, lop,
3916 if (ret == NFSERR_STALECLIENTID ||
3917 ret == NFSERR_STALEDONTRECOVER ||
3918 ret == NFSERR_BADSESSION) {
3924 nfscl_freeopen(lop, 1);
3933 * If no openowner found, create one and get an open
3937 MALLOC(nowp, struct nfsclowner *,
3938 sizeof (struct nfsclowner), M_NFSCLOWNER,
3940 nfscl_newopen(clp, NULL, &owp, &nowp, &op,
3941 NULL, lowp->nfsow_owner, dp->nfsdl_fh,
3942 dp->nfsdl_fhlen, NULL);
3943 newnfs_copycred(&dp->nfsdl_cred, cred);
3944 ret = nfscl_moveopen(vp, clp, nmp, lop,
3947 nfscl_freeopenowner(owp, 0);
3948 if (ret == NFSERR_STALECLIENTID ||
3949 ret == NFSERR_STALEDONTRECOVER ||
3950 ret == NFSERR_BADSESSION) {
3956 nfscl_freeopen(lop, 1);
3966 * Now, get byte range locks for any locks done locally.
3968 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
3969 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
3970 newnfs_copycred(&dp->nfsdl_cred, cred);
3971 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
3972 if (ret == NFSERR_STALESTATEID ||
3973 ret == NFSERR_STALEDONTRECOVER ||
3974 ret == NFSERR_STALECLIENTID ||
3975 ret == NFSERR_BADSESSION) {
3990 * Move a locally issued open over to an owner on the state list.
3991 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
3992 * returns with it unlocked.
3995 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
3996 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
3997 struct ucred *cred, NFSPROC_T *p)
3999 struct nfsclopen *op, *nop;
4000 struct nfscldeleg *ndp;
4002 int error = 0, newone;
4005 * First, look for an appropriate open, If found, just increment the
4008 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4009 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4010 op->nfso_fhlen == lop->nfso_fhlen &&
4011 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4012 op->nfso_opencnt += lop->nfso_opencnt;
4013 nfscl_freeopen(lop, 1);
4018 /* No appropriate open, so we have to do one against the server. */
4020 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
4021 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4023 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4024 lop->nfso_fh, lop->nfso_fhlen, &newone);
4026 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4027 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4028 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4031 nfscl_freeopen(op, 0);
4034 newnfs_copyincred(cred, &op->nfso_cred);
4035 op->nfso_mode |= lop->nfso_mode;
4036 op->nfso_opencnt += lop->nfso_opencnt;
4037 nfscl_freeopen(lop, 1);
4040 FREE((caddr_t)nop, M_NFSCLOPEN);
4043 * What should I do with the returned delegation, since the
4044 * delegation is being recalled? For now, just printf and
4047 printf("Moveopen returned deleg\n");
4048 FREE((caddr_t)ndp, M_NFSCLDELEG);
4054 * Recall all delegations on this client.
4057 nfscl_totalrecall(struct nfsclclient *clp)
4059 struct nfscldeleg *dp;
4061 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4062 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4063 dp->nfsdl_flags |= NFSCLDL_RECALL;
4068 * Relock byte ranges. Called for delegation recall and state expiry.
4071 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4072 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4075 struct nfscllockowner *nlp;
4078 u_int32_t clidrev = 0;
4079 int error, newone, donelocally;
4081 off = lop->nfslo_first;
4082 len = lop->nfslo_end - lop->nfslo_first;
4083 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4084 clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4085 lp->nfsl_openowner, &nlp, &newone, &donelocally);
4086 if (error || donelocally)
4088 if (nmp->nm_clp != NULL)
4089 clidrev = nmp->nm_clp->nfsc_clientidrev;
4092 nfhp = VTONFS(vp)->n_fhp;
4093 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4094 nfhp->nfh_len, nlp, newone, 0, off,
4095 len, lop->nfslo_type, cred, p);
4097 nfscl_freelockowner(nlp, 0);
4102 * Called to re-open a file. Basically get a vnode for the file handle
4103 * and then call nfsrpc_openrpc() to do the rest.
4106 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4107 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4108 struct ucred *cred, NFSPROC_T *p)
4114 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4118 if (np->n_v4 != NULL) {
4119 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4120 np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4121 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4131 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4132 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4136 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4137 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4138 u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4139 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4144 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4145 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4147 if (error == NFSERR_DELAY)
4148 (void) nfs_catnap(PZERO, error, "nfstryop");
4149 } while (error == NFSERR_DELAY);
4150 if (error == EAUTH || error == EACCES) {
4151 /* Try again using system credentials */
4152 newnfs_setroot(cred);
4154 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4155 newfhlen, mode, op, name, namelen, ndpp, reclaim,
4156 delegtype, cred, p, 1, 0);
4157 if (error == NFSERR_DELAY)
4158 (void) nfs_catnap(PZERO, error, "nfstryop");
4159 } while (error == NFSERR_DELAY);
4165 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4166 * NFSERR_DELAY. Also, retry with system credentials, if the provided
4170 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4171 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4172 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4174 struct nfsrv_descript nfsd, *nd = &nfsd;
4178 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4179 reclaim, off, len, type, cred, p, 0);
4180 if (!error && nd->nd_repstat == NFSERR_DELAY)
4181 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4183 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4185 error = nd->nd_repstat;
4186 if (error == EAUTH || error == EACCES) {
4187 /* Try again using root credentials */
4188 newnfs_setroot(cred);
4190 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4191 newone, reclaim, off, len, type, cred, p, 1);
4192 if (!error && nd->nd_repstat == NFSERR_DELAY)
4193 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4195 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4197 error = nd->nd_repstat;
4203 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4204 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4208 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4209 struct nfsmount *nmp, NFSPROC_T *p)
4214 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4215 if (error == NFSERR_DELAY)
4216 (void) nfs_catnap(PZERO, error, "nfstrydp");
4217 } while (error == NFSERR_DELAY);
4218 if (error == EAUTH || error == EACCES) {
4219 /* Try again using system credentials */
4220 newnfs_setroot(cred);
4222 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4223 if (error == NFSERR_DELAY)
4224 (void) nfs_catnap(PZERO, error, "nfstrydp");
4225 } while (error == NFSERR_DELAY);
4231 * Try a close against the server. Just call nfsrpc_closerpc(),
4232 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4236 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4237 struct nfsmount *nmp, NFSPROC_T *p)
4239 struct nfsrv_descript nfsd, *nd = &nfsd;
4243 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4244 if (error == NFSERR_DELAY)
4245 (void) nfs_catnap(PZERO, error, "nfstrycl");
4246 } while (error == NFSERR_DELAY);
4247 if (error == EAUTH || error == EACCES) {
4248 /* Try again using system credentials */
4249 newnfs_setroot(cred);
4251 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4252 if (error == NFSERR_DELAY)
4253 (void) nfs_catnap(PZERO, error, "nfstrycl");
4254 } while (error == NFSERR_DELAY);
4260 * Decide if a delegation on a file permits close without flushing writes
4261 * to the server. This might be a big performance win in some environments.
4262 * (Not useful until the client does caching on local stable storage.)
4265 nfscl_mustflush(vnode_t vp)
4267 struct nfsclclient *clp;
4268 struct nfscldeleg *dp;
4270 struct nfsmount *nmp;
4273 nmp = VFSTONFS(vnode_mount(vp));
4274 if (!NFSHASNFSV4(nmp))
4277 clp = nfscl_findcl(nmp);
4282 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4283 if (dp != NULL && (dp->nfsdl_flags &
4284 (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4286 (dp->nfsdl_sizelimit >= np->n_size ||
4287 !NFSHASSTRICT3530(nmp))) {
4296 * See if a (write) delegation exists for this file.
4299 nfscl_nodeleg(vnode_t vp, int writedeleg)
4301 struct nfsclclient *clp;
4302 struct nfscldeleg *dp;
4304 struct nfsmount *nmp;
4307 nmp = VFSTONFS(vnode_mount(vp));
4308 if (!NFSHASNFSV4(nmp))
4311 clp = nfscl_findcl(nmp);
4316 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4318 (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4319 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4329 * Look for an associated delegation that should be DelegReturned.
4332 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4334 struct nfsclclient *clp;
4335 struct nfscldeleg *dp;
4336 struct nfsclowner *owp;
4337 struct nfscllockowner *lp;
4338 struct nfsmount *nmp;
4341 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4343 nmp = VFSTONFS(vnode_mount(vp));
4347 * Loop around waiting for:
4348 * - outstanding I/O operations on delegations to complete
4349 * - for a delegation on vp that has state, lock the client and
4351 * - return delegation with no state
4354 clp = nfscl_findcl(nmp);
4359 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4360 np->n_fhp->nfh_len);
4363 * Wait for outstanding I/O ops to be done.
4365 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4367 nfsv4_unlock(&clp->nfsc_lock, 0);
4370 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4371 (void) nfsmsleep(&dp->nfsdl_rwlock,
4372 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4376 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4377 if (!LIST_EMPTY(&owp->nfsow_open)) {
4383 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4384 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4390 if (needsrecall && !triedrecall) {
4391 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4394 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4395 &islept, NFSCLSTATEMUTEXPTR, NULL);
4402 cred = newnfs_getcred();
4403 newnfs_copycred(&dp->nfsdl_cred, cred);
4404 (void) nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0);
4408 nfsv4_unlock(&clp->nfsc_lock, 0);
4412 *stp = dp->nfsdl_stateid;
4414 nfscl_cleandeleg(dp);
4415 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4418 nfsv4_unlock(&clp->nfsc_lock, 0);
4425 * Look for associated delegation(s) that should be DelegReturned.
4428 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4429 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4431 struct nfsclclient *clp;
4432 struct nfscldeleg *dp;
4433 struct nfsclowner *owp;
4434 struct nfscllockowner *lp;
4435 struct nfsmount *nmp;
4438 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4440 nmp = VFSTONFS(vnode_mount(fvp));
4445 * Loop around waiting for:
4446 * - outstanding I/O operations on delegations to complete
4447 * - for a delegation on fvp that has state, lock the client and
4449 * - return delegation(s) with no state.
4452 clp = nfscl_findcl(nmp);
4458 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4459 np->n_fhp->nfh_len);
4460 if (dp != NULL && *gotfdp == 0) {
4462 * Wait for outstanding I/O ops to be done.
4464 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4466 nfsv4_unlock(&clp->nfsc_lock, 0);
4469 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4470 (void) nfsmsleep(&dp->nfsdl_rwlock,
4471 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4475 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4476 if (!LIST_EMPTY(&owp->nfsow_open)) {
4482 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4483 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4489 if (needsrecall && !triedrecall) {
4490 dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4493 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4494 &islept, NFSCLSTATEMUTEXPTR, NULL);
4501 cred = newnfs_getcred();
4502 newnfs_copycred(&dp->nfsdl_cred, cred);
4503 (void) nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0);
4507 nfsv4_unlock(&clp->nfsc_lock, 0);
4511 *fstp = dp->nfsdl_stateid;
4514 nfscl_cleandeleg(dp);
4515 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4518 nfsv4_unlock(&clp->nfsc_lock, 0);
4523 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4524 np->n_fhp->nfh_len);
4525 if (dp != NULL && *gottdp == 0) {
4527 * Wait for outstanding I/O ops to be done.
4529 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4530 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4531 (void) nfsmsleep(&dp->nfsdl_rwlock,
4532 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4535 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4536 if (!LIST_EMPTY(&owp->nfsow_open)) {
4541 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4542 if (!LIST_EMPTY(&lp->nfsl_lock)) {
4547 *tstp = dp->nfsdl_stateid;
4550 nfscl_cleandeleg(dp);
4551 nfscl_freedeleg(&clp->nfsc_deleg, dp);
4560 * Get a reference on the clientid associated with the mount point.
4561 * Return 1 if success, 0 otherwise.
4564 nfscl_getref(struct nfsmount *nmp)
4566 struct nfsclclient *clp;
4569 clp = nfscl_findcl(nmp);
4574 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
4580 * Release a reference on a clientid acquired with the above call.
4583 nfscl_relref(struct nfsmount *nmp)
4585 struct nfsclclient *clp;
4588 clp = nfscl_findcl(nmp);
4593 nfsv4_relref(&clp->nfsc_lock);
4598 * Save the size attribute in the delegation, since the nfsnode
4602 nfscl_reclaimnode(vnode_t vp)
4604 struct nfsclclient *clp;
4605 struct nfscldeleg *dp;
4606 struct nfsnode *np = VTONFS(vp);
4607 struct nfsmount *nmp;
4609 nmp = VFSTONFS(vnode_mount(vp));
4610 if (!NFSHASNFSV4(nmp))
4613 clp = nfscl_findcl(nmp);
4618 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4619 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4620 dp->nfsdl_size = np->n_size;
4625 * Get the saved size attribute in the delegation, since it is a
4626 * newly allocated nfsnode.
4629 nfscl_newnode(vnode_t vp)
4631 struct nfsclclient *clp;
4632 struct nfscldeleg *dp;
4633 struct nfsnode *np = VTONFS(vp);
4634 struct nfsmount *nmp;
4636 nmp = VFSTONFS(vnode_mount(vp));
4637 if (!NFSHASNFSV4(nmp))
4640 clp = nfscl_findcl(nmp);
4645 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4646 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4647 np->n_size = dp->nfsdl_size;
4652 * If there is a valid write delegation for this file, set the modtime
4653 * to the local clock time.
4656 nfscl_delegmodtime(vnode_t vp)
4658 struct nfsclclient *clp;
4659 struct nfscldeleg *dp;
4660 struct nfsnode *np = VTONFS(vp);
4661 struct nfsmount *nmp;
4663 nmp = VFSTONFS(vnode_mount(vp));
4664 if (!NFSHASNFSV4(nmp))
4667 clp = nfscl_findcl(nmp);
4672 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4673 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4674 nanotime(&dp->nfsdl_modtime);
4675 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4681 * If there is a valid write delegation for this file with a modtime set,
4682 * put that modtime in mtime.
4685 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4687 struct nfsclclient *clp;
4688 struct nfscldeleg *dp;
4689 struct nfsnode *np = VTONFS(vp);
4690 struct nfsmount *nmp;
4692 nmp = VFSTONFS(vnode_mount(vp));
4693 if (!NFSHASNFSV4(nmp))
4696 clp = nfscl_findcl(nmp);
4701 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4703 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4704 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4705 *mtime = dp->nfsdl_modtime;
4710 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
4712 short *defaulterrp, *errp;
4714 if (!nd->nd_repstat)
4716 if (nd->nd_procnum == NFSPROC_NOOP)
4717 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4718 if (nd->nd_repstat == EBADRPC)
4719 return (txdr_unsigned(NFSERR_BADXDR));
4720 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4721 nd->nd_repstat == NFSERR_OPILLEGAL)
4722 return (txdr_unsigned(nd->nd_repstat));
4723 if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
4724 minorvers > NFSV4_MINORVERSION) {
4725 /* NFSv4.n error. */
4726 return (txdr_unsigned(nd->nd_repstat));
4728 if (nd->nd_procnum < NFSV4OP_CBNOPS)
4729 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4731 return (txdr_unsigned(nd->nd_repstat));
4733 if (*errp == (short)nd->nd_repstat)
4734 return (txdr_unsigned(nd->nd_repstat));
4735 return (txdr_unsigned(*defaulterrp));
4739 * Called to find/add a layout to a client.
4740 * This function returns the layout with a refcnt (shared lock) upon
4741 * success (returns 0) or with no lock/refcnt on the layout when an
4742 * error is returned.
4743 * If a layout is passed in via lypp, it is locked (exclusively locked).
4746 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4747 nfsv4stateid_t *stateidp, int retonclose,
4748 struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
4749 struct ucred *cred, NFSPROC_T *p)
4751 struct nfsclclient *clp;
4752 struct nfscllayout *lyp, *tlyp;
4753 struct nfsclflayout *flp;
4754 struct nfsnode *np = VTONFS(vp);
4756 int layout_passed_in;
4758 mp = nmp->nm_mountp;
4759 layout_passed_in = 1;
4763 layout_passed_in = 0;
4764 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
4771 if (layout_passed_in != 0)
4772 nfsv4_unlock(&lyp->nfsly_lock, 0);
4775 free(tlyp, M_NFSLAYOUT);
4780 * Although no lyp was passed in, another thread might have
4781 * allocated one. If one is found, just increment it's ref
4782 * count and return it.
4784 lyp = nfscl_findlayout(clp, fhp, fhlen);
4788 lyp->nfsly_stateid.seqid = stateidp->seqid;
4789 lyp->nfsly_stateid.other[0] = stateidp->other[0];
4790 lyp->nfsly_stateid.other[1] = stateidp->other[1];
4791 lyp->nfsly_stateid.other[2] = stateidp->other[2];
4792 lyp->nfsly_lastbyte = 0;
4793 LIST_INIT(&lyp->nfsly_flayread);
4794 LIST_INIT(&lyp->nfsly_flayrw);
4795 LIST_INIT(&lyp->nfsly_recall);
4796 lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
4797 lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
4798 lyp->nfsly_clp = clp;
4799 lyp->nfsly_flags = (retonclose != 0) ?
4800 (NFSLY_FILES | NFSLY_RETONCLOSE) : NFSLY_FILES;
4801 lyp->nfsly_fhlen = fhlen;
4802 NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
4803 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4804 LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
4806 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4809 if (retonclose != 0)
4810 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
4811 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4812 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4813 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4815 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
4816 if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
4819 free(tlyp, M_NFSLAYOUT);
4824 lyp->nfsly_stateid.seqid = stateidp->seqid;
4826 /* Merge the new list of File Layouts into the list. */
4827 flp = LIST_FIRST(fhlp);
4829 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
4830 nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
4832 nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
4834 if (layout_passed_in != 0)
4835 nfsv4_unlock(&lyp->nfsly_lock, 1);
4838 free(tlyp, M_NFSLAYOUT);
4843 * Search for a layout by MDS file handle.
4844 * If one is found, it is returned with a refcnt (shared lock) iff
4845 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
4848 struct nfscllayout *
4849 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
4850 uint64_t off, struct nfsclflayout **retflpp, int *recalledp)
4852 struct nfscllayout *lyp;
4854 int error, igotlock;
4856 mp = clp->nfsc_nmp->nm_mountp;
4860 lyp = nfscl_findlayout(clp, fhp, fhlen);
4862 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
4863 TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
4864 TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
4865 lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
4866 error = nfscl_findlayoutforio(lyp, off,
4867 NFSV4OPEN_ACCESSREAD, retflpp);
4869 nfsv4_getref(&lyp->nfsly_lock, NULL,
4870 NFSCLSTATEMUTEXPTR, mp);
4873 igotlock = nfsv4_lock(&lyp->nfsly_lock,
4874 1, NULL, NFSCLSTATEMUTEXPTR, mp);
4875 } while (igotlock == 0 &&
4876 (mp->mnt_kern_flag & MNTK_UNMOUNTF) == 0);
4879 if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
4893 * Search for a layout by MDS file handle. If one is found that is marked
4894 * "return on close", delete it, since it should now be forgotten.
4897 nfscl_retoncloselayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen)
4899 struct nfscllayout *lyp;
4902 lyp = nfscl_findlayout(clp, fhp, fhlen);
4903 if (lyp != NULL && (lyp->nfsly_flags & NFSLY_RETONCLOSE) != 0) {
4905 * Wait for outstanding I/O ops to be done.
4907 if (lyp->nfsly_lock.nfslock_usecnt != 0 ||
4908 lyp->nfsly_lock.nfslock_lock != 0) {
4909 lyp->nfsly_lock.nfslock_lock |= NFSV4LOCK_WANTED;
4910 (void)mtx_sleep(&lyp->nfsly_lock,
4911 NFSCLSTATEMUTEXPTR, PZERO, "nfslyc", 0);
4914 nfscl_freelayout(lyp);
4919 * Dereference a layout.
4922 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
4927 nfsv4_unlock(&lyp->nfsly_lock, 0);
4929 nfsv4_relref(&lyp->nfsly_lock);
4934 * Search for a devinfo by deviceid. If one is found, return it after
4935 * acquiring a reference count on it.
4937 struct nfscldevinfo *
4938 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
4939 struct nfscldevinfo *dip)
4944 dip = nfscl_finddevinfo(clp, deviceid);
4946 dip->nfsdi_refcnt++;
4952 * Dereference a devinfo structure.
4955 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
4958 dip->nfsdi_refcnt--;
4959 if (dip->nfsdi_refcnt == 0)
4960 wakeup(&dip->nfsdi_refcnt);
4964 * Dereference a devinfo structure.
4967 nfscl_reldevinfo(struct nfscldevinfo *dip)
4971 nfscl_reldevinfo_locked(dip);
4976 * Find a layout for this file handle. Return NULL upon failure.
4978 static struct nfscllayout *
4979 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
4981 struct nfscllayout *lyp;
4983 LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
4984 if (lyp->nfsly_fhlen == fhlen &&
4985 !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
4991 * Find a devinfo for this deviceid. Return NULL upon failure.
4993 static struct nfscldevinfo *
4994 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
4996 struct nfscldevinfo *dip;
4998 LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
4999 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5006 * Merge the new file layout list into the main one, maintaining it in
5007 * increasing offset order.
5010 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5011 struct nfsclflayouthead *newfhlp)
5013 struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5015 flp = LIST_FIRST(fhlp);
5017 LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5018 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5020 flp = LIST_NEXT(flp, nfsfl_list);
5022 if (prevflp == NULL)
5023 LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5025 LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5031 * Add this nfscldevinfo to the client, if it doesn't already exist.
5032 * This function consumes the structure pointed at by dip, if not NULL.
5035 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip,
5036 struct nfsclflayout *flp)
5038 struct nfsclclient *clp;
5039 struct nfscldevinfo *tdip;
5046 free(dip, M_NFSDEVINFO);
5049 tdip = nfscl_finddevinfo(clp, flp->nfsfl_dev);
5051 tdip->nfsdi_layoutrefs++;
5052 flp->nfsfl_devp = tdip;
5053 nfscl_reldevinfo_locked(tdip);
5056 free(dip, M_NFSDEVINFO);
5060 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5061 dip->nfsdi_layoutrefs = 1;
5062 flp->nfsfl_devp = dip;
5071 * Free up a layout structure and associated file layout structure(s).
5074 nfscl_freelayout(struct nfscllayout *layp)
5076 struct nfsclflayout *flp, *nflp;
5077 struct nfsclrecalllayout *rp, *nrp;
5079 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5080 LIST_REMOVE(flp, nfsfl_list);
5081 nfscl_freeflayout(flp);
5083 LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5084 LIST_REMOVE(flp, nfsfl_list);
5085 nfscl_freeflayout(flp);
5087 LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5088 LIST_REMOVE(rp, nfsrecly_list);
5089 free(rp, M_NFSLAYRECALL);
5092 free(layp, M_NFSLAYOUT);
5096 * Free up a file layout structure.
5099 nfscl_freeflayout(struct nfsclflayout *flp)
5103 for (i = 0; i < flp->nfsfl_fhcnt; i++)
5104 free(flp->nfsfl_fh[i], M_NFSFH);
5105 if (flp->nfsfl_devp != NULL)
5106 flp->nfsfl_devp->nfsdi_layoutrefs--;
5107 free(flp, M_NFSFLAYOUT);
5111 * Free up a file layout devinfo structure.
5114 nfscl_freedevinfo(struct nfscldevinfo *dip)
5117 free(dip, M_NFSDEVINFO);
5121 * Mark any layouts that match as recalled.
5124 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5125 uint64_t off, uint64_t len, uint32_t stateseqid,
5126 struct nfsclrecalllayout *recallp)
5128 struct nfsclrecalllayout *rp, *orp;
5130 recallp->nfsrecly_recalltype = recalltype;
5131 recallp->nfsrecly_iomode = iomode;
5132 recallp->nfsrecly_stateseqid = stateseqid;
5133 recallp->nfsrecly_off = off;
5134 recallp->nfsrecly_len = len;
5136 * Order the list as file returns first, followed by fsid and any
5137 * returns, both in increasing stateseqid order.
5138 * Note that the seqids wrap around, so 1 is after 0xffffffff.
5139 * (I'm not sure this is correct because I find RFC5661 confusing
5140 * on this, but hopefully it will work ok.)
5143 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5145 if ((recalltype == NFSLAYOUTRETURN_FILE &&
5146 (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5147 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5148 (recalltype != NFSLAYOUTRETURN_FILE &&
5149 rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5150 nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5151 LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5157 LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5160 LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5162 lyp->nfsly_flags |= NFSLY_RECALL;
5167 * Compare the two seqids for ordering. The trick is that the seqids can
5168 * wrap around from 0xffffffff->0, so check for the cases where one
5169 * has wrapped around.
5170 * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5173 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5176 if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5177 /* seqid2 has wrapped around. */
5179 if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5180 /* seqid1 has wrapped around. */
5182 if (seqid1 <= seqid2)
5188 * Do a layout return for each of the recalls.
5191 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5192 struct ucred *cred, NFSPROC_T *p)
5194 struct nfsclrecalllayout *rp;
5195 nfsv4stateid_t stateid;
5197 NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5198 LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5199 stateid.seqid = rp->nfsrecly_stateseqid;
5200 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5201 lyp->nfsly_fhlen, 0, NFSLAYOUT_NFSV4_1_FILES,
5202 rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5203 rp->nfsrecly_off, rp->nfsrecly_len,
5204 &stateid, 0, NULL, cred, p, NULL);
5209 * Do the layout commit for a file layout.
5212 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5213 struct ucred *cred, NFSPROC_T *p)
5215 struct nfsclflayout *flp;
5219 LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5220 if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5221 len = flp->nfsfl_end - flp->nfsfl_off;
5222 error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5223 lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5224 lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5225 NFSLAYOUT_NFSV4_1_FILES, 0, NULL, cred, p, NULL);
5226 NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5227 if (error == NFSERR_NOTSUPP) {
5228 /* If not supported, don't bother doing it. */
5230 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5239 * Commit all layouts for a file (vnode).
5242 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5244 struct nfsclclient *clp;
5245 struct nfscllayout *lyp;
5246 struct nfsnode *np = VTONFS(vp);
5248 struct nfsmount *nmp;
5250 mp = vnode_mount(vp);
5252 if (NFSHASNOLAYOUTCOMMIT(nmp))
5260 lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5265 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5266 if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
5271 if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5272 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5274 NFSCL_DEBUG(4, "do layoutcommit2\n");
5275 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5279 nfsv4_relref(&lyp->nfsly_lock);