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 extrema.
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 serailization:
54 * - For the ClientId, is 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 * - When a process exits, it calls nfscl_cleanup(), which goes
68 * 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;
92 struct nfsclhead nfsclhead; /* Head of clientid list */
93 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
94 #endif /* !APPLEKEXT */
96 static int nfscl_delegcnt = 0;
97 static int nfscl_getopen(struct nfsclownerhead *, u_int8_t *, int, u_int8_t *,
98 NFSPROC_T *, u_int32_t, struct nfsclowner **, struct nfsclopen **);
99 static void nfscl_clrelease(struct nfsclclient *);
100 static void nfscl_cleanclient(struct nfsclclient *);
101 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
102 struct ucred *, NFSPROC_T *);
103 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
104 struct nfsmount *, struct ucred *, NFSPROC_T *);
105 static void nfscl_recover(struct nfsclclient *, struct ucred *, NFSPROC_T *);
106 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
107 struct nfscllock *, int);
108 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
109 struct nfscllock **, int);
110 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *);
111 static u_int32_t nfscl_nextcbident(void);
112 static mount_t nfscl_getmnt(u_int32_t);
113 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
115 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
116 u_int8_t *, struct nfscllock **);
117 static void nfscl_freelockowner(struct nfscllockowner *, int);
118 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
119 static int nfscl_localconflict(struct nfsclclient *, struct nfscllock *,
120 u_int8_t *, struct nfscldeleg *, struct nfscllock **);
121 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
122 struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
123 struct nfsclopen **, u_int8_t *, u_int8_t *, int, int *);
124 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
125 struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
126 struct nfscldeleg *, struct ucred *, NFSPROC_T *);
127 static void nfscl_totalrecall(struct nfsclclient *);
128 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
129 struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
130 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
131 u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
132 struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
133 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
134 int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
135 struct ucred *, NFSPROC_T *);
136 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
137 struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
138 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *);
139 static int nfscl_errmap(struct nfsrv_descript *);
140 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
141 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
142 struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *);
143 static void nfscl_freeopenowner(struct nfsclowner *, int);
144 static void nfscl_cleandeleg(struct nfscldeleg *);
145 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
146 struct nfsmount *, NFSPROC_T *);
148 static short nfscberr_null[] = {
153 static short nfscberr_getattr[] = {
162 static short nfscberr_recall[] = {
172 static short *nfscl_cberrmap[] = {
180 #define NETFAMILY(clp) \
181 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
184 * Called for an open operation.
185 * If the nfhp argument is NULL, just get an openowner.
188 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
189 struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
190 struct nfsclopen **opp, int *newonep, int *retp, int lockit)
192 struct nfsclclient *clp;
193 struct nfsclowner *owp, *nowp;
194 struct nfsclopen *op = NULL, *nop = NULL;
195 struct nfscldeleg *dp;
196 struct nfsclownerhead *ohp;
197 u_int8_t own[NFSV4CL_LOCKNAMELEN];
208 * Might need one or both of these, so MALLOC them now, to
209 * avoid a tsleep() in MALLOC later.
211 MALLOC(nowp, struct nfsclowner *, sizeof (struct nfsclowner),
212 M_NFSCLOWNER, M_WAITOK);
214 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
215 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
216 ret = nfscl_getcl(vp, cred, p, &clp);
218 FREE((caddr_t)nowp, M_NFSCLOWNER);
220 FREE((caddr_t)nop, M_NFSCLOPEN);
225 * Get the Open iff it already exists.
226 * If none found, add the new one or return error, depending upon
229 nfscl_filllockowner(p, own);
232 /* First check the delegation list */
233 if (nfhp != NULL && usedeleg) {
234 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
235 if (dp->nfsdl_fhlen == fhlen &&
236 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
237 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
238 (dp->nfsdl_flags & NFSCLDL_WRITE))
247 ohp = &dp->nfsdl_owner;
249 ohp = &clp->nfsc_owner;
250 /* Now, search for an openowner */
251 LIST_FOREACH(owp, ohp, nfsow_list) {
252 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
257 * Create a new open, as required.
259 nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
263 * Serialize modifications to the open owner for multiple threads
264 * within the same process using a read/write sleep lock.
267 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
270 FREE((caddr_t)nowp, M_NFSCLOWNER);
272 FREE((caddr_t)nop, M_NFSCLOPEN);
278 *retp = NFSCLOPEN_OK;
281 * Now, check the mode on the open and return the appropriate
284 if (op != NULL && (amode & ~(op->nfso_mode))) {
285 op->nfso_mode |= amode;
286 if (retp != NULL && dp == NULL)
287 *retp = NFSCLOPEN_DOOPEN;
293 * Create a new open, as required.
296 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
297 struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
298 struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
301 struct nfsclowner *owp = *owpp, *nowp;
302 struct nfsclopen *op, *nop;
312 if (owp == NULL && nowp != NULL) {
313 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
314 LIST_INIT(&nowp->nfsow_open);
315 nowp->nfsow_clp = clp;
316 nowp->nfsow_seqid = 0;
317 nowp->nfsow_defunct = 0;
318 nfscl_lockinit(&nowp->nfsow_rwlock);
320 newnfsstats.cllocalopenowners++;
321 LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
323 newnfsstats.clopenowners++;
324 LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
332 /* If an fhp has been specified, create an Open as well. */
334 /* and look for the correct open, based upon FH */
335 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
336 if (op->nfso_fhlen == fhlen &&
337 !NFSBCMP(op->nfso_fh, fhp, fhlen))
340 if (op == NULL && nop != NULL) {
343 nop->nfso_opencnt = 0;
344 nop->nfso_posixlock = 1;
345 nop->nfso_fhlen = fhlen;
346 NFSBCOPY(fhp, nop->nfso_fh, fhlen);
347 LIST_INIT(&nop->nfso_lock);
348 nop->nfso_stateid.seqid = 0;
349 nop->nfso_stateid.other[0] = 0;
350 nop->nfso_stateid.other[1] = 0;
351 nop->nfso_stateid.other[2] = 0;
353 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
354 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
356 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
357 newnfsstats.cllocalopens++;
359 newnfsstats.clopens++;
361 LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
373 * Called to find/add a delegation to a client.
376 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
377 int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
379 struct nfscldeleg *dp = *dpp, *tdp;
382 * First, if we have received a Read delegation for a file on a
383 * read/write file system, just return it, because they aren't
386 if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
387 (dp->nfsdl_flags & NFSCLDL_READ)) {
388 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
389 FREE((caddr_t)dp, M_NFSCLDELEG);
394 /* Look for the correct deleg, based upon FH */
396 tdp = nfscl_finddeleg(clp, nfhp, fhlen);
400 return (NFSERR_BADSTATEID);
403 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
404 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
406 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
407 newnfsstats.cldelegates++;
411 * Delegation already exists, what do we do if a new one??
414 printf("Deleg already exists!\n");
415 FREE((caddr_t)dp, M_NFSCLDELEG);
426 * Find a delegation for this file handle. Return NULL upon failure.
428 static struct nfscldeleg *
429 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
431 struct nfscldeleg *dp;
433 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
434 if (dp->nfsdl_fhlen == fhlen &&
435 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
442 * Get a stateid for an I/O operation. First, look for an open and iff
443 * found, return either a lockowner stateid or the open stateid.
444 * If no Open is found, just return error and the special stateid of all zeros.
447 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
448 struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
451 struct nfsclclient *clp;
452 struct nfsclowner *owp;
453 struct nfsclopen *op = NULL;
454 struct nfscllockowner *lp;
455 struct nfscldeleg *dp;
457 u_int8_t own[NFSV4CL_LOCKNAMELEN];
462 * Initially, just set the special stateid of all zeros.
465 stateidp->other[0] = 0;
466 stateidp->other[1] = 0;
467 stateidp->other[2] = 0;
468 if (vnode_vtype(vp) != VREG)
472 clp = nfscl_findcl(VFSTONFS(vnode_mount(vp)));
479 * First, look for a delegation.
481 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
482 if (dp->nfsdl_fhlen == fhlen &&
483 !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
484 if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
485 (dp->nfsdl_flags & NFSCLDL_WRITE)) {
486 stateidp->seqid = dp->nfsdl_stateid.seqid;
487 stateidp->other[0] = dp->nfsdl_stateid.other[0];
488 stateidp->other[1] = dp->nfsdl_stateid.other[1];
489 stateidp->other[2] = dp->nfsdl_stateid.other[2];
490 if (!(np->n_flag & NDELEGRECALL)) {
491 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
493 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
495 dp->nfsdl_timestamp = NFSD_MONOSEC +
497 dp->nfsdl_rwlock.nfslock_usecnt++;
498 *lckpp = (void *)&dp->nfsdl_rwlock;
509 * If p != NULL, we want to search the parentage tree
510 * for a matching OpenOwner and use that.
512 nfscl_filllockowner(p, own);
513 error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, NULL, p,
516 /* now look for a lockowner */
517 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
518 if (!NFSBCMP(lp->nfsl_owner, own,
519 NFSV4CL_LOCKNAMELEN)) {
521 lp->nfsl_stateid.seqid;
523 lp->nfsl_stateid.other[0];
525 lp->nfsl_stateid.other[1];
527 lp->nfsl_stateid.other[2];
535 /* If not found, just look for any OpenOwner that will work. */
537 owp = LIST_FIRST(&clp->nfsc_owner);
538 while (!done && owp != NULL) {
539 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
540 if (op->nfso_fhlen == fhlen &&
541 !NFSBCMP(op->nfso_fh, nfhp, fhlen) &&
542 (mode & op->nfso_mode) == mode) {
548 owp = LIST_NEXT(owp, nfsow_list);
554 /* for read aheads or write behinds, use the open cred */
555 newnfs_copycred(&op->nfso_cred, cred);
559 * No lock stateid, so return the open stateid.
561 stateidp->seqid = op->nfso_stateid.seqid;
562 stateidp->other[0] = op->nfso_stateid.other[0];
563 stateidp->other[1] = op->nfso_stateid.other[1];
564 stateidp->other[2] = op->nfso_stateid.other[2];
570 * Get an existing open. Search up the parentage tree for a match and
571 * return with the first one found.
574 nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
575 u_int8_t *rown, NFSPROC_T *p, u_int32_t mode, struct nfsclowner **owpp,
576 struct nfsclopen **opp)
578 struct nfsclowner *owp = NULL;
579 struct nfsclopen *op;
581 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp;
585 while (op == NULL && (nproc != NULL || rown != NULL)) {
587 nfscl_filllockowner(nproc, own);
592 /* Search the client list */
593 LIST_FOREACH(owp, ohp, nfsow_list) {
594 if (!NFSBCMP(owp->nfsow_owner, ownp,
595 NFSV4CL_LOCKNAMELEN))
599 /* and look for the correct open */
600 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
601 if (op->nfso_fhlen == fhlen &&
602 !NFSBCMP(op->nfso_fh, nfhp, fhlen)
603 && (op->nfso_mode & mode) == mode) {
611 nproc = nfscl_getparent(nproc);
623 * Release use of an open owner. Called when open operations are done
624 * with the open owner.
627 nfscl_ownerrelease(struct nfsclowner *owp, __unused int error,
628 __unused int candelete, int unlocked)
635 nfscl_lockunlock(&owp->nfsow_rwlock);
636 nfscl_clrelease(owp->nfsow_clp);
641 * Release use of an open structure under an open owner.
644 nfscl_openrelease(struct nfsclopen *op, int error, int candelete)
646 struct nfsclclient *clp;
647 struct nfsclowner *owp;
653 nfscl_lockunlock(&owp->nfsow_rwlock);
654 clp = owp->nfsow_clp;
655 if (error && candelete && op->nfso_opencnt == 0)
656 nfscl_freeopen(op, 0);
657 nfscl_clrelease(clp);
662 * Called to get a clientid structure. It will optionally lock the
663 * client data structures to do the SetClientId/SetClientId_confirm,
664 * but will release that lock and return the clientid with a refernce
666 * If the "cred" argument is NULL, a new clientid should not be created.
667 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
669 * It always clpp with a reference count on it, unless returning an error.
672 nfscl_getcl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
673 struct nfsclclient **clpp)
675 struct nfsclclient *clp;
676 struct nfsclclient *newclp = NULL;
677 struct nfscllockowner *lp, *nlp;
678 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
679 char uuid[HOSTUUIDLEN];
680 int igotlock = 0, error, trystalecnt, clidinusedelay, i;
684 getcredhostuuid(cred, uuid, sizeof uuid);
685 idlen = strlen(uuid);
687 idlen += sizeof (u_int64_t);
689 idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
690 MALLOC(newclp, struct nfsclclient *,
691 sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
697 if (newclp == NULL) {
702 NFSBZERO((caddr_t)clp, sizeof(struct nfsclclient) + idlen - 1);
703 clp->nfsc_idlen = idlen;
704 LIST_INIT(&clp->nfsc_owner);
705 TAILQ_INIT(&clp->nfsc_deleg);
706 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
707 LIST_INIT(&clp->nfsc_deleghash[i]);
708 LIST_INIT(&clp->nfsc_defunctlockowner);
709 clp->nfsc_flags = NFSCLFLAGS_INITED;
710 clp->nfsc_clientidrev = 1;
711 clp->nfsc_cbident = nfscl_nextcbident();
712 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
714 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
718 nfscl_start_renewthread(clp);
722 FREE((caddr_t)newclp, M_NFSCLCLIENT);
725 while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock)
726 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
729 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR);
733 * If it needs a clientid, do the setclientid now.
735 if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
737 panic("nfscl_clget");
738 if (p == NULL || cred == NULL) {
740 nfsv4_unlock(&clp->nfsc_lock, 0);
744 /* get rid of defunct lockowners */
745 LIST_FOREACH_SAFE(lp, &clp->nfsc_defunctlockowner, nfsl_list,
747 nfscl_freelockowner(lp, 0);
750 * If RFC3530 Sec. 14.2.33 is taken literally,
751 * NFSERR_CLIDINUSE will be returned persistently for the
752 * case where a new mount of the same file system is using
753 * a different principal. In practice, NFSERR_CLIDINUSE is
754 * only returned when there is outstanding unexpired state
755 * on the clientid. As such, try for twice the lease
756 * interval, if we know what that is. Otherwise, make a
758 * The case of returning NFSERR_STALECLIENTID is far less
759 * likely, but might occur if there is a significant delay
760 * between doing the SetClientID and SetClientIDConfirm Ops,
761 * such that the server throws away the clientid before
762 * receiving the SetClientIDConfirm.
764 if (clp->nfsc_renew > 0)
765 clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
767 clidinusedelay = 120;
770 error = nfsrpc_setclient(VFSTONFS(vnode_mount(vp)),
772 if (error == NFSERR_STALECLIENTID ||
773 error == NFSERR_STALEDONTRECOVER ||
774 error == NFSERR_CLIDINUSE) {
775 (void) nfs_catnap(PZERO, "nfs_setcl");
777 } while (((error == NFSERR_STALECLIENTID ||
778 error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
779 (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
782 nfsv4_unlock(&clp->nfsc_lock, 0);
786 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
790 nfsv4_unlock(&clp->nfsc_lock, 1);
799 * Get a reference to a clientid and return it, if valid.
801 APPLESTATIC struct nfsclclient *
802 nfscl_findcl(struct nfsmount *nmp)
804 struct nfsclclient *clp;
807 if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
813 * Release the clientid structure. It may be locked or reference counted.
816 nfscl_clrelease(struct nfsclclient *clp)
819 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
820 nfsv4_unlock(&clp->nfsc_lock, 0);
822 nfsv4_relref(&clp->nfsc_lock);
826 * External call for nfscl_clrelease.
829 nfscl_clientrelease(struct nfsclclient *clp)
833 if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
834 nfsv4_unlock(&clp->nfsc_lock, 0);
836 nfsv4_relref(&clp->nfsc_lock);
841 * Called when wanting to lock a byte region.
844 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
845 short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
846 int recovery, u_int8_t *rownp, u_int8_t *ropenownp,
847 struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
849 struct nfscllockowner *lp;
850 struct nfsclopen *op;
851 struct nfsclclient *clp;
852 struct nfscllockowner *nlp;
853 struct nfscllock *nlop, *otherlop;
854 struct nfscldeleg *dp = NULL, *ldp = NULL;
855 struct nfscllockownerhead *lhp = NULL;
857 u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp;
858 int error = 0, ret, donelocally = 0;
862 mode = NFSV4OPEN_ACCESSWRITE;
864 mode = NFSV4OPEN_ACCESSREAD;
871 * Might need these, so MALLOC them now, to
872 * avoid a tsleep() in MALLOC later.
874 MALLOC(nlp, struct nfscllockowner *,
875 sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
876 MALLOC(otherlop, struct nfscllock *,
877 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
878 MALLOC(nlop, struct nfscllock *,
879 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
880 nlop->nfslo_type = type;
881 nlop->nfslo_first = off;
882 if (len == NFS64BITSSET) {
883 nlop->nfslo_end = NFS64BITSSET;
885 nlop->nfslo_end = off + len;
886 if (nlop->nfslo_end <= nlop->nfslo_first)
887 error = NFSERR_INVAL;
894 error = nfscl_getcl(vp, cred, p, &clp);
897 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
898 FREE((caddr_t)otherlop, M_NFSCLLOCK);
899 FREE((caddr_t)nlop, M_NFSCLLOCK);
907 nfscl_filllockowner(p, own);
913 * First, search for a delegation. If one exists for this file,
914 * the lock can be done locally against it, so long as there
915 * isn't a local lock conflict.
917 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
919 /* Just sanity check for correct type of delegation */
920 if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_RECALL) ||
921 (type == F_WRLCK && !(dp->nfsdl_flags & NFSCLDL_WRITE))))
925 /* Now, find the associated open to get the correct openowner */
926 ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
927 np->n_fhp->nfh_len, NULL, p, mode, NULL, &op);
929 ret = nfscl_getopen(&clp->nfsc_owner,
930 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, NULL, p,
933 lhp = &dp->nfsdl_lock;
934 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
935 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
936 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
944 * Get the related Open.
947 error = nfscl_getopen(&clp->nfsc_owner,
948 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, ropenownp,
949 NULL, mode, NULL, &op);
951 error = nfscl_getopen(&clp->nfsc_owner,
952 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, NULL, p,
955 lhp = &op->nfso_lock;
957 if (!error && !recovery)
958 error = nfscl_localconflict(clp, nlop, ownp, ldp, NULL);
961 nfscl_clrelease(clp);
964 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
965 FREE((caddr_t)otherlop, M_NFSCLLOCK);
966 FREE((caddr_t)nlop, M_NFSCLLOCK);
971 * Ok, see if a lockowner exists and create one, as required.
973 LIST_FOREACH(lp, lhp, nfsl_list) {
974 if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
978 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
980 NFSBCOPY(ropenownp, nlp->nfsl_openowner,
981 NFSV4CL_LOCKNAMELEN);
983 NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
984 NFSV4CL_LOCKNAMELEN);
986 nlp->nfsl_defunct = 0;
987 nlp->nfsl_inprog = NULL;
988 nfscl_lockinit(&nlp->nfsl_rwlock);
989 LIST_INIT(&nlp->nfsl_lock);
991 nlp->nfsl_open = NULL;
992 newnfsstats.cllocallockowners++;
995 newnfsstats.cllockowners++;
997 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1004 * Now, update the byte ranges for locks.
1006 ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1012 nfscl_clrelease(clp);
1015 * Serial modifications on the lock owner for multiple threads
1016 * for the same process using a read/write lock.
1019 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1025 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1027 FREE((caddr_t)nlop, M_NFSCLLOCK);
1029 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1036 * Called to unlock a byte range, for LockU.
1039 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1040 __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1041 struct nfsclclient *clp, struct nfscllockowner **lpp, int *dorpcp)
1043 struct nfscllockowner *lp;
1044 struct nfsclowner *owp;
1045 struct nfsclopen *op;
1046 struct nfscllock *nlop, *other_lop = NULL;
1047 struct nfscldeleg *dp;
1049 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1050 int ret = 0, fnd, error;
1057 * Might need these, so MALLOC them now, to
1058 * avoid a tsleep() in MALLOC later.
1060 MALLOC(nlop, struct nfscllock *,
1061 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1062 nlop->nfslo_type = F_UNLCK;
1063 nlop->nfslo_first = off;
1064 if (len == NFS64BITSSET) {
1065 nlop->nfslo_end = NFS64BITSSET;
1067 nlop->nfslo_end = off + len;
1068 if (nlop->nfslo_end <= nlop->nfslo_first) {
1069 FREE((caddr_t)nlop, M_NFSCLLOCK);
1070 return (NFSERR_INVAL);
1074 MALLOC(other_lop, struct nfscllock *,
1075 sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1078 nfscl_filllockowner(p, own);
1082 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1083 np->n_fhp->nfh_len);
1085 /* Search for a local conflict. */
1086 error = nfscl_localconflict(clp, nlop, own, dp, NULL);
1089 FREE((caddr_t)nlop, M_NFSCLLOCK);
1090 if (other_lop != NULL)
1091 FREE((caddr_t)other_lop, M_NFSCLLOCK);
1096 * First, unlock any local regions on a delegation.
1099 /* Look for this lockowner. */
1100 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1101 if (!NFSBCMP(lp->nfsl_owner, own,
1102 NFSV4CL_LOCKNAMELEN))
1106 /* Use other_lop, so nlop is still available */
1107 (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1111 * Now, find a matching open/lockowner that hasn't already been done,
1112 * as marked by nfsl_inprog.
1116 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1117 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1118 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1119 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1120 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1121 if (lp->nfsl_inprog == NULL &&
1122 !NFSBCMP(lp->nfsl_owner, own,
1123 NFSV4CL_LOCKNAMELEN)) {
1137 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1141 * Serial modifications on the lock owner for multiple
1142 * threads for the same process using a read/write lock.
1144 lp->nfsl_inprog = p;
1145 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1150 FREE((caddr_t)nlop, M_NFSCLLOCK);
1152 FREE((caddr_t)other_lop, M_NFSCLLOCK);
1157 * Release all lockowners marked in progess for this process and file.
1160 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p)
1162 struct nfsclowner *owp;
1163 struct nfsclopen *op;
1164 struct nfscllockowner *lp;
1166 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1169 nfscl_filllockowner(p, own);
1171 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1172 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1173 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1174 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1175 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1176 if (lp->nfsl_inprog == p &&
1177 !NFSBCMP(lp->nfsl_owner, own,
1178 NFSV4CL_LOCKNAMELEN)) {
1179 lp->nfsl_inprog = NULL;
1180 nfscl_lockunlock(&lp->nfsl_rwlock);
1186 nfscl_clrelease(clp);
1191 * Called to find out if any bytes within the byte range specified are
1192 * write locked by the calling process. Used to determine if flushing
1193 * is required before a LockU.
1194 * If in doubt, return 1, so the flush will occur.
1197 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1198 struct ucred *cred, NFSPROC_T *p)
1200 struct nfsclowner *owp;
1201 struct nfscllockowner *lp;
1202 struct nfsclopen *op;
1203 struct nfsclclient *clp;
1204 struct nfscllock *lop;
1205 struct nfscldeleg *dp;
1208 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1212 switch (fl->l_whence) {
1216 * Caller is responsible for adding any necessary offset
1217 * when SEEK_CUR is used.
1222 off = np->n_size + fl->l_start;
1227 if (fl->l_len != 0) {
1228 end = off + fl->l_len;
1235 error = nfscl_getcl(vp, cred, p, &clp);
1238 nfscl_filllockowner(p, own);
1242 * First check the delegation locks.
1244 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1246 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1247 if (!NFSBCMP(lp->nfsl_owner, own,
1248 NFSV4CL_LOCKNAMELEN))
1252 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1253 if (lop->nfslo_first >= end)
1255 if (lop->nfslo_end <= off)
1257 if (lop->nfslo_type == F_WRLCK) {
1258 nfscl_clrelease(clp);
1267 * Now, check state against the server.
1269 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1270 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1271 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1272 !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1273 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1274 if (!NFSBCMP(lp->nfsl_owner, own,
1275 NFSV4CL_LOCKNAMELEN))
1279 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1280 if (lop->nfslo_first >= end)
1282 if (lop->nfslo_end <= off)
1284 if (lop->nfslo_type == F_WRLCK) {
1285 nfscl_clrelease(clp);
1294 nfscl_clrelease(clp);
1300 * Release a byte range lock owner structure.
1303 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1305 struct nfsclclient *clp;
1310 clp = lp->nfsl_open->nfso_own->nfsow_clp;
1311 if (error != 0 && candelete &&
1312 (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1313 nfscl_freelockowner(lp, 0);
1315 nfscl_lockunlock(&lp->nfsl_rwlock);
1316 nfscl_clrelease(clp);
1321 * Free up an open structure and any associated byte range lock structures.
1324 nfscl_freeopen(struct nfsclopen *op, int local)
1327 LIST_REMOVE(op, nfso_list);
1328 nfscl_freealllocks(&op->nfso_lock, local);
1329 FREE((caddr_t)op, M_NFSCLOPEN);
1331 newnfsstats.cllocalopens--;
1333 newnfsstats.clopens--;
1337 * Free up all lock owners and associated locks.
1340 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1342 struct nfscllockowner *lp, *nlp;
1344 LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1345 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1347 nfscl_freelockowner(lp, local);
1352 * Called for an Open when NFSERR_EXPIRED is received from the server.
1353 * If there are no byte range locks nor a Share Deny lost, try to do a
1354 * fresh Open. Otherwise, free the open.
1357 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1358 struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1360 struct nfscllockowner *lp;
1361 struct nfscldeleg *dp;
1362 int mustdelete = 0, error;
1365 * Look for any byte range lock(s).
1367 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1368 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1375 * If no byte range lock(s) nor a Share deny, try to re-open.
1377 if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1378 newnfs_copycred(&op->nfso_cred, cred);
1380 error = nfsrpc_reopen(nmp, op->nfso_fh,
1381 op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1385 FREE((caddr_t)dp, M_NFSCLDELEG);
1390 nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1391 op->nfso_fhlen, cred, p, &dp);
1395 * If a byte range lock or Share deny or couldn't re-open, free it.
1398 nfscl_freeopen(op, 0);
1399 return (mustdelete);
1403 * Free up an open owner structure.
1406 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1409 LIST_REMOVE(owp, nfsow_list);
1410 FREE((caddr_t)owp, M_NFSCLOWNER);
1412 newnfsstats.cllocalopenowners--;
1414 newnfsstats.clopenowners--;
1418 * Free up a byte range lock owner structure.
1421 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1423 struct nfscllock *lop, *nlop;
1425 LIST_REMOVE(lp, nfsl_list);
1426 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1427 nfscl_freelock(lop, local);
1429 FREE((caddr_t)lp, M_NFSCLLOCKOWNER);
1431 newnfsstats.cllocallockowners--;
1433 newnfsstats.cllockowners--;
1437 * Free up a byte range lock structure.
1440 nfscl_freelock(struct nfscllock *lop, int local)
1443 LIST_REMOVE(lop, nfslo_list);
1444 FREE((caddr_t)lop, M_NFSCLLOCK);
1446 newnfsstats.cllocallocks--;
1448 newnfsstats.cllocks--;
1452 * Clean out the state related to a delegation.
1455 nfscl_cleandeleg(struct nfscldeleg *dp)
1457 struct nfsclowner *owp, *nowp;
1458 struct nfsclopen *op;
1460 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1461 op = LIST_FIRST(&owp->nfsow_open);
1463 if (LIST_NEXT(op, nfso_list) != NULL)
1464 panic("nfscleandel");
1465 nfscl_freeopen(op, 1);
1467 nfscl_freeopenowner(owp, 1);
1469 nfscl_freealllocks(&dp->nfsdl_lock, 1);
1473 * Free a delegation.
1476 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp)
1479 TAILQ_REMOVE(hdp, dp, nfsdl_list);
1480 LIST_REMOVE(dp, nfsdl_hash);
1481 FREE((caddr_t)dp, M_NFSCLDELEG);
1482 newnfsstats.cldelegates--;
1487 * Free up all state related to this client structure.
1490 nfscl_cleanclient(struct nfsclclient *clp)
1492 struct nfsclowner *owp, *nowp;
1493 struct nfsclopen *op, *nop;
1494 struct nfscllockowner *lp, *nlp;
1497 /* get rid of defunct lockowners */
1498 LIST_FOREACH_SAFE(lp, &clp->nfsc_defunctlockowner, nfsl_list, nlp) {
1499 nfscl_freelockowner(lp, 0);
1502 /* Now, all the OpenOwners, etc. */
1503 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1504 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1505 nfscl_freeopen(op, 0);
1507 nfscl_freeopenowner(owp, 0);
1512 * Called when an NFSERR_EXPIRED is received from the server.
1515 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1516 struct ucred *cred, NFSPROC_T *p)
1518 struct nfsclowner *owp, *nowp, *towp;
1519 struct nfsclopen *op, *nop, *top;
1520 struct nfscldeleg *dp, *ndp;
1521 int ret, printed = 0;
1524 * First, merge locally issued Opens into the list for the server.
1526 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1527 while (dp != NULL) {
1528 ndp = TAILQ_NEXT(dp, nfsdl_list);
1529 owp = LIST_FIRST(&dp->nfsdl_owner);
1530 while (owp != NULL) {
1531 nowp = LIST_NEXT(owp, nfsow_list);
1532 op = LIST_FIRST(&owp->nfsow_open);
1534 if (LIST_NEXT(op, nfso_list) != NULL)
1536 LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1537 if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1538 NFSV4CL_LOCKNAMELEN))
1542 /* Merge opens in */
1543 LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1544 if (top->nfso_fhlen == op->nfso_fhlen &&
1545 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1547 top->nfso_mode |= op->nfso_mode;
1548 top->nfso_opencnt += op->nfso_opencnt;
1553 /* Just add the open to the owner list */
1554 LIST_REMOVE(op, nfso_list);
1555 op->nfso_own = towp;
1556 LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1557 newnfsstats.cllocalopens--;
1558 newnfsstats.clopens++;
1561 /* Just add the openowner to the client list */
1562 LIST_REMOVE(owp, nfsow_list);
1563 owp->nfsow_clp = clp;
1564 LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1565 newnfsstats.cllocalopenowners--;
1566 newnfsstats.clopenowners++;
1567 newnfsstats.cllocalopens--;
1568 newnfsstats.clopens++;
1573 if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1575 printf("nfsv4 expired locks lost\n");
1577 nfscl_cleandeleg(dp);
1578 nfscl_freedeleg(&clp->nfsc_deleg, dp);
1581 if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1585 * Now, try and reopen against the server.
1587 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1588 owp->nfsow_seqid = 0;
1589 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1590 ret = nfscl_expireopen(clp, op, nmp, cred, p);
1591 if (ret && !printed) {
1593 printf("nfsv4 expired locks lost\n");
1596 if (LIST_EMPTY(&owp->nfsow_open))
1597 nfscl_freeopenowner(owp, 0);
1603 * Called from exit() upon process termination.
1606 nfscl_cleanup(NFSPROC_T *p)
1608 struct nfsclclient *clp;
1609 u_int8_t own[NFSV4CL_LOCKNAMELEN];
1613 nfscl_filllockowner(p, own);
1617 * Loop through all the clientids, looking for the OpenOwners.
1619 LIST_FOREACH(clp, &nfsclhead, nfsc_list)
1620 nfscl_cleanup_common(clp, own);
1623 #endif /* !__FreeBSD__ */
1626 * Common code used by nfscl_cleanup() and nfscl_cleanupkext().
1627 * Must be called with CLSTATE lock held.
1630 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1632 struct nfsclowner *owp, *nowp;
1633 struct nfsclopen *op;
1634 struct nfscllockowner *lp, *nlp;
1635 struct nfscldeleg *dp;
1637 /* First, get rid of local locks on delegations. */
1638 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1639 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1640 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1641 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1643 nfscl_freelockowner(lp, 1);
1647 owp = LIST_FIRST(&clp->nfsc_owner);
1648 while (owp != NULL) {
1649 nowp = LIST_NEXT(owp, nfsow_list);
1650 if (!NFSBCMP(owp->nfsow_owner, own,
1651 NFSV4CL_LOCKNAMELEN)) {
1653 * If there are children that haven't closed the
1654 * file descriptors yet, the opens will still be
1655 * here. For that case, let the renew thread clear
1656 * out the OpenOwner later.
1658 if (LIST_EMPTY(&owp->nfsow_open))
1659 nfscl_freeopenowner(owp, 0);
1661 owp->nfsow_defunct = 1;
1663 /* look for lockowners on other opens */
1664 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1665 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1666 if (!NFSBCMP(lp->nfsl_owner, own,
1667 NFSV4CL_LOCKNAMELEN))
1668 lp->nfsl_defunct = 1;
1675 /* and check the defunct list */
1676 LIST_FOREACH(lp, &clp->nfsc_defunctlockowner, nfsl_list) {
1677 if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN))
1678 lp->nfsl_defunct = 1;
1682 #if defined(APPLEKEXT) || defined(__FreeBSD__)
1684 * Simulate the call nfscl_cleanup() by looking for open owners associated
1685 * with processes that no longer exist, since a call to nfscl_cleanup()
1686 * can't be patched into exit().
1689 nfscl_cleanupkext(struct nfsclclient *clp)
1691 struct nfsclowner *owp, *nowp;
1692 struct nfscllockowner *lp;
1696 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1697 if (nfscl_procdoesntexist(owp->nfsow_owner))
1698 nfscl_cleanup_common(clp, owp->nfsow_owner);
1701 /* and check the defunct list */
1702 LIST_FOREACH(lp, &clp->nfsc_defunctlockowner, nfsl_list) {
1703 if (nfscl_procdoesntexist(lp->nfsl_owner))
1704 lp->nfsl_defunct = 1;
1707 NFSPROCLISTUNLOCK();
1709 #endif /* APPLEKEXT || __FreeBSD__ */
1712 * Called from nfs umount to free up the clientid.
1715 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1717 struct nfsclclient *clp;
1723 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1724 panic("nfscl umount");
1727 * First, handshake with the nfscl renew thread, to terminate
1730 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1731 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1732 (void) tsleep((caddr_t)clp, PWAIT, "nfsclumnt", hz);
1736 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1737 NFSCLSTATEMUTEXPTR);
1738 } while (!igotlock);
1742 * Free up all the state. It will expire on the server, but
1743 * maybe we should do a SetClientId/SetClientIdConfirm so
1744 * the server throws it away?
1746 LIST_REMOVE(clp, nfsc_list);
1747 nfscl_delegreturnall(clp, p);
1748 cred = newnfs_getcred();
1749 (void) nfsrpc_setclient(nmp, clp, cred, p);
1750 nfscl_cleanclient(clp);
1753 FREE((caddr_t)clp, M_NFSCLCLIENT);
1759 * This function is called when a server replies with NFSERR_STALECLIENTID
1760 * or NFSERR_STALESTATEID. It traverses the clientid lists, doing Opens
1761 * and Locks with reclaim. If these fail, it deletes the corresponding state.
1764 nfscl_recover(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
1766 struct nfsclowner *owp, *nowp;
1767 struct nfsclopen *op, *nop;
1768 struct nfscllockowner *lp, *nlp;
1769 struct nfscllock *lop, *nlop;
1770 struct nfscldeleg *dp, *ndp, *tdp;
1771 struct nfsmount *nmp;
1772 struct ucred *tcred;
1773 struct nfsclopenhead extra_open;
1774 struct nfscldeleghead extra_deleg;
1777 u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
1778 int igotlock = 0, error, trycnt, firstlock, s;
1781 * First, lock the client structure, so everyone else will
1782 * block when trying to use state.
1786 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1787 NFSCLSTATEMUTEXPTR);
1788 } while (!igotlock);
1791 nmp = clp->nfsc_nmp;
1793 panic("nfscl recover");
1796 error = nfsrpc_setclient(nmp, clp, cred, p);
1797 } while ((error == NFSERR_STALECLIENTID ||
1798 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
1800 nfscl_cleanclient(clp);
1801 clp->nfsc_flags &= ~(NFSCLFLAGS_HASCLIENTID |
1802 NFSCLFLAGS_RECOVER);
1804 nfsv4_unlock(&clp->nfsc_lock, 0);
1808 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
1809 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
1812 * Mark requests already queued on the server, so that they don't
1813 * initiate another recovery cycle. Any requests already in the
1814 * queue that handle state information will have the old stale
1815 * clientid/stateid and will get a NFSERR_STALESTATEID or
1816 * NFSERR_STALECLIENTID reply from the server. This will be
1817 * translated to NFSERR_STALEDONTRECOVER when R_DONTRECOVER is set.
1821 TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
1822 if (rep->r_nmp == nmp)
1823 rep->r_flags |= R_DONTRECOVER;
1828 /* get rid of defunct lockowners */
1829 LIST_FOREACH_SAFE(lp, &clp->nfsc_defunctlockowner, nfsl_list, nlp) {
1830 nfscl_freelockowner(lp, 0);
1834 * Now, mark all delegations "need reclaim".
1836 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
1837 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
1839 TAILQ_INIT(&extra_deleg);
1840 LIST_INIT(&extra_open);
1842 * Now traverse the state lists, doing Open and Lock Reclaims.
1844 tcred = newnfs_getcred();
1845 owp = LIST_FIRST(&clp->nfsc_owner);
1846 while (owp != NULL) {
1847 nowp = LIST_NEXT(owp, nfsow_list);
1848 owp->nfsow_seqid = 0;
1849 op = LIST_FIRST(&owp->nfsow_open);
1850 while (op != NULL) {
1851 nop = LIST_NEXT(op, nfso_list);
1852 if (error != NFSERR_NOGRACE) {
1853 /* Search for a delegation to reclaim with the open */
1854 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1855 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
1857 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
1858 mode = NFSV4OPEN_ACCESSWRITE;
1859 delegtype = NFSV4OPEN_DELEGATEWRITE;
1861 mode = NFSV4OPEN_ACCESSREAD;
1862 delegtype = NFSV4OPEN_DELEGATEREAD;
1864 if ((op->nfso_mode & mode) == mode &&
1865 op->nfso_fhlen == dp->nfsdl_fhlen &&
1866 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
1871 delegtype = NFSV4OPEN_DELEGATENONE;
1872 newnfs_copycred(&op->nfso_cred, tcred);
1873 error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
1874 op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
1875 op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
1878 /* Handle any replied delegation */
1879 if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
1880 || NFSMNT_RDONLY(nmp->nm_mountp))) {
1881 if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
1882 mode = NFSV4OPEN_ACCESSWRITE;
1884 mode = NFSV4OPEN_ACCESSREAD;
1885 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1886 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
1888 if ((op->nfso_mode & mode) == mode &&
1889 op->nfso_fhlen == dp->nfsdl_fhlen &&
1890 !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
1892 dp->nfsdl_stateid = ndp->nfsdl_stateid;
1893 dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
1894 dp->nfsdl_ace = ndp->nfsdl_ace;
1895 dp->nfsdl_change = ndp->nfsdl_change;
1896 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
1897 if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
1898 dp->nfsdl_flags |= NFSCLDL_RECALL;
1899 FREE((caddr_t)ndp, M_NFSCLDELEG);
1906 TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
1908 /* and reclaim all byte range locks */
1909 lp = LIST_FIRST(&op->nfso_lock);
1910 while (lp != NULL) {
1911 nlp = LIST_NEXT(lp, nfsl_list);
1914 lop = LIST_FIRST(&lp->nfsl_lock);
1915 while (lop != NULL) {
1916 nlop = LIST_NEXT(lop, nfslo_list);
1917 if (lop->nfslo_end == NFS64BITSSET)
1920 len = lop->nfslo_end - lop->nfslo_first;
1921 if (error != NFSERR_NOGRACE)
1922 error = nfscl_trylock(nmp, NULL,
1923 op->nfso_fh, op->nfso_fhlen, lp,
1924 firstlock, 1, lop->nfslo_first, len,
1925 lop->nfslo_type, tcred, p);
1927 nfscl_freelock(lop, 0);
1932 /* If no locks, but a lockowner, just delete it. */
1933 if (LIST_EMPTY(&lp->nfsl_lock))
1934 nfscl_freelockowner(lp, 0);
1938 nfscl_freeopen(op, 0);
1947 * Now, try and get any delegations not yet reclaimed by cobbling
1948 * to-gether an appropriate open.
1951 dp = TAILQ_FIRST(&clp->nfsc_deleg);
1952 while (dp != NULL) {
1953 ndp = TAILQ_NEXT(dp, nfsdl_list);
1954 if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
1956 MALLOC(nowp, struct nfsclowner *,
1957 sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
1959 * Name must be as long an largest possible
1960 * NFSV4CL_LOCKNAMELEN. 12 for now.
1962 NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
1963 NFSV4CL_LOCKNAMELEN);
1964 LIST_INIT(&nowp->nfsow_open);
1965 nowp->nfsow_clp = clp;
1966 nowp->nfsow_seqid = 0;
1967 nowp->nfsow_defunct = 0;
1968 nfscl_lockinit(&nowp->nfsow_rwlock);
1971 if (error != NFSERR_NOGRACE) {
1972 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
1973 dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
1974 nop->nfso_own = nowp;
1975 if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
1976 nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
1977 delegtype = NFSV4OPEN_DELEGATEWRITE;
1979 nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
1980 delegtype = NFSV4OPEN_DELEGATEREAD;
1982 nop->nfso_opencnt = 0;
1983 nop->nfso_posixlock = 1;
1984 nop->nfso_fhlen = dp->nfsdl_fhlen;
1985 NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
1986 LIST_INIT(&nop->nfso_lock);
1987 nop->nfso_stateid.seqid = 0;
1988 nop->nfso_stateid.other[0] = 0;
1989 nop->nfso_stateid.other[1] = 0;
1990 nop->nfso_stateid.other[2] = 0;
1991 newnfs_copycred(&dp->nfsdl_cred, tcred);
1992 newnfs_copyincred(tcred, &nop->nfso_cred);
1994 error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
1995 nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
1996 nop->nfso_mode, nop, NULL, 0, &tdp, 1,
1997 delegtype, tcred, p);
1999 if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2000 mode = NFSV4OPEN_ACCESSWRITE;
2002 mode = NFSV4OPEN_ACCESSREAD;
2003 if ((nop->nfso_mode & mode) == mode &&
2004 nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2005 !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2007 dp->nfsdl_stateid = tdp->nfsdl_stateid;
2008 dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2009 dp->nfsdl_ace = tdp->nfsdl_ace;
2010 dp->nfsdl_change = tdp->nfsdl_change;
2011 dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2012 if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2013 dp->nfsdl_flags |= NFSCLDL_RECALL;
2014 FREE((caddr_t)tdp, M_NFSCLDELEG);
2016 TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2022 FREE((caddr_t)nop, M_NFSCLOPEN);
2024 * Couldn't reclaim it, so throw the state
2027 nfscl_cleandeleg(dp);
2028 nfscl_freedeleg(&clp->nfsc_deleg, dp);
2030 LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2037 * Now, get rid of extra Opens and Delegations.
2039 LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2041 newnfs_copycred(&op->nfso_cred, tcred);
2042 error = nfscl_tryclose(op, tcred, nmp, p);
2043 if (error == NFSERR_GRACE)
2044 (void) nfs_catnap(PZERO, "nfsexcls");
2045 } while (error == NFSERR_GRACE);
2046 LIST_REMOVE(op, nfso_list);
2047 FREE((caddr_t)op, M_NFSCLOPEN);
2050 FREE((caddr_t)nowp, M_NFSCLOWNER);
2052 TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2054 newnfs_copycred(&dp->nfsdl_cred, tcred);
2055 error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2056 if (error == NFSERR_GRACE)
2057 (void) nfs_catnap(PZERO, "nfsexdlg");
2058 } while (error == NFSERR_GRACE);
2059 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2060 FREE((caddr_t)dp, M_NFSCLDELEG);
2064 nfsv4_unlock(&clp->nfsc_lock, 0);
2070 * This function is called when a server replies with NFSERR_EXPIRED.
2071 * It deletes all state for the client and does a fresh SetClientId/confirm.
2072 * XXX Someday it should post a signal to the process(es) that hold the
2073 * state, so they know that lock state has been lost.
2076 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2078 struct nfscllockowner *lp, *nlp;
2079 struct nfsmount *nmp;
2081 int igotlock = 0, error, trycnt;
2084 * If the clientid has gone away or a new SetClientid has already
2085 * been done, just return ok.
2087 if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2091 * First, lock the client structure, so everyone else will
2092 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2093 * that only one thread does the work.
2096 clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2098 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2099 NFSCLSTATEMUTEXPTR);
2100 } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2101 if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2103 nfsv4_unlock(&clp->nfsc_lock, 0);
2109 nmp = clp->nfsc_nmp;
2111 panic("nfscl expired");
2112 cred = newnfs_getcred();
2115 error = nfsrpc_setclient(nmp, clp, cred, p);
2116 } while ((error == NFSERR_STALECLIENTID ||
2117 error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2120 * Clear out any state.
2122 nfscl_cleanclient(clp);
2123 clp->nfsc_flags &= ~(NFSCLFLAGS_HASCLIENTID |
2124 NFSCLFLAGS_RECOVER);
2126 /* get rid of defunct lockowners */
2127 LIST_FOREACH_SAFE(lp, &clp->nfsc_defunctlockowner, nfsl_list,
2129 nfscl_freelockowner(lp, 0);
2133 * Expire the state for the client.
2135 nfscl_expireclient(clp, nmp, cred, p);
2136 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2137 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2140 clp->nfsc_flags &= ~NFSCLFLAGS_EXPIREIT;
2142 nfsv4_unlock(&clp->nfsc_lock, 0);
2148 * This function inserts a lock in the list after insert_lop.
2151 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2152 struct nfscllock *insert_lop, int local)
2155 if ((struct nfscllockowner *)insert_lop == lp)
2156 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2158 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2160 newnfsstats.cllocallocks++;
2162 newnfsstats.cllocks++;
2166 * This function updates the locking for a lock owner and given file. It
2167 * maintains a list of lock ranges ordered on increasing file offset that
2168 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2169 * It always adds new_lop to the list and sometimes uses the one pointed
2171 * Returns 1 if the locks were modified, 0 otherwise.
2174 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2175 struct nfscllock **other_lopp, int local)
2177 struct nfscllock *new_lop = *new_lopp;
2178 struct nfscllock *lop, *tlop, *ilop;
2179 struct nfscllock *other_lop;
2180 int unlock = 0, modified = 0;
2184 * Work down the list until the lock is merged.
2186 if (new_lop->nfslo_type == F_UNLCK)
2188 ilop = (struct nfscllock *)lp;
2189 lop = LIST_FIRST(&lp->nfsl_lock);
2190 while (lop != NULL) {
2192 * Only check locks for this file that aren't before the start of
2195 if (lop->nfslo_end >= new_lop->nfslo_first) {
2196 if (new_lop->nfslo_end < lop->nfslo_first) {
2198 * If the new lock ends before the start of the
2199 * current lock's range, no merge, just insert
2204 if (new_lop->nfslo_type == lop->nfslo_type ||
2205 (new_lop->nfslo_first <= lop->nfslo_first &&
2206 new_lop->nfslo_end >= lop->nfslo_end)) {
2208 * This lock can be absorbed by the new lock/unlock.
2209 * This happens when it covers the entire range
2210 * of the old lock or is contiguous
2211 * with the old lock and is of the same type or an
2214 if (new_lop->nfslo_type != lop->nfslo_type ||
2215 new_lop->nfslo_first != lop->nfslo_first ||
2216 new_lop->nfslo_end != lop->nfslo_end)
2218 if (lop->nfslo_first < new_lop->nfslo_first)
2219 new_lop->nfslo_first = lop->nfslo_first;
2220 if (lop->nfslo_end > new_lop->nfslo_end)
2221 new_lop->nfslo_end = lop->nfslo_end;
2223 lop = LIST_NEXT(lop, nfslo_list);
2224 nfscl_freelock(tlop, local);
2229 * All these cases are for contiguous locks that are not the
2230 * same type, so they can't be merged.
2232 if (new_lop->nfslo_first <= lop->nfslo_first) {
2234 * This case is where the new lock overlaps with the
2235 * first part of the old lock. Move the start of the
2236 * old lock to just past the end of the new lock. The
2237 * new lock will be inserted in front of the old, since
2238 * ilop hasn't been updated. (We are done now.)
2240 if (lop->nfslo_first != new_lop->nfslo_end) {
2241 lop->nfslo_first = new_lop->nfslo_end;
2246 if (new_lop->nfslo_end >= lop->nfslo_end) {
2248 * This case is where the new lock overlaps with the
2249 * end of the old lock's range. Move the old lock's
2250 * end to just before the new lock's first and insert
2251 * the new lock after the old lock.
2252 * Might not be done yet, since the new lock could
2253 * overlap further locks with higher ranges.
2255 if (lop->nfslo_end != new_lop->nfslo_first) {
2256 lop->nfslo_end = new_lop->nfslo_first;
2260 lop = LIST_NEXT(lop, nfslo_list);
2264 * The final case is where the new lock's range is in the
2265 * middle of the current lock's and splits the current lock
2266 * up. Use *other_lopp to handle the second part of the
2267 * split old lock range. (We are done now.)
2268 * For unlock, we use new_lop as other_lop and tmp, since
2269 * other_lop and new_lop are the same for this case.
2270 * We noted the unlock case above, so we don't need
2271 * new_lop->nfslo_type any longer.
2273 tmp = new_lop->nfslo_first;
2275 other_lop = new_lop;
2278 other_lop = *other_lopp;
2281 other_lop->nfslo_first = new_lop->nfslo_end;
2282 other_lop->nfslo_end = lop->nfslo_end;
2283 other_lop->nfslo_type = lop->nfslo_type;
2284 lop->nfslo_end = tmp;
2285 nfscl_insertlock(lp, other_lop, lop, local);
2291 lop = LIST_NEXT(lop, nfslo_list);
2297 * Insert the new lock in the list at the appropriate place.
2300 nfscl_insertlock(lp, new_lop, ilop, local);
2308 * This function must be run as a kernel thread.
2309 * It does Renew Ops and recovery, when required.
2312 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2314 struct nfsclowner *owp, *nowp;
2315 struct nfsclopen *op;
2316 struct nfscllockowner *lp, *nlp, *olp;
2317 struct nfscldeleghead dh;
2318 struct nfscllockownerhead lh;
2319 struct nfscldeleg *dp, *ndp;
2322 int error, cbpathdown, islept, igotlock, ret, clearok;
2324 cred = newnfs_getcred();
2325 clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2327 newnfs_setroot(cred);
2329 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER)
2330 nfscl_recover(clp, cred, p);
2331 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2332 (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2333 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2334 clidrev = clp->nfsc_clientidrev;
2335 error = nfsrpc_renew(clp, cred, p);
2336 if (error == NFSERR_CBPATHDOWN)
2338 else if (error == NFSERR_STALECLIENTID)
2339 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2340 else if (error == NFSERR_EXPIRED)
2341 (void) nfscl_hasexpired(clp, clidrev, p);
2348 /* It's a Total Recall! */
2349 nfscl_totalrecall(clp);
2352 * Now, handle defunct owners.
2354 owp = LIST_FIRST(&clp->nfsc_owner);
2355 while (owp != NULL) {
2356 nowp = LIST_NEXT(owp, nfsow_list);
2357 if (LIST_EMPTY(&owp->nfsow_open)) {
2358 if (owp->nfsow_defunct)
2359 nfscl_freeopenowner(owp, 0);
2361 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2362 lp = LIST_FIRST(&op->nfso_lock);
2363 while (lp != NULL) {
2364 nlp = LIST_NEXT(lp, nfsl_list);
2365 if (lp->nfsl_defunct &&
2366 LIST_EMPTY(&lp->nfsl_lock)) {
2367 LIST_FOREACH(olp, &lh, nfsl_list) {
2368 if (!NFSBCMP(olp->nfsl_owner,
2369 lp->nfsl_owner,NFSV4CL_LOCKNAMELEN))
2373 LIST_REMOVE(lp, nfsl_list);
2374 LIST_INSERT_HEAD(&lh, lp, nfsl_list);
2376 nfscl_freelockowner(lp, 0);
2386 /* also search the defunct list */
2387 lp = LIST_FIRST(&clp->nfsc_defunctlockowner);
2388 while (lp != NULL) {
2389 nlp = LIST_NEXT(lp, nfsl_list);
2390 if (lp->nfsl_defunct) {
2391 LIST_FOREACH(olp, &lh, nfsl_list) {
2392 if (!NFSBCMP(olp->nfsl_owner, lp->nfsl_owner,
2393 NFSV4CL_LOCKNAMELEN))
2397 LIST_REMOVE(lp, nfsl_list);
2398 LIST_INSERT_HEAD(&lh, lp, nfsl_list);
2400 nfscl_freelockowner(lp, 0);
2405 /* and release defunct lock owners */
2406 LIST_FOREACH_SAFE(lp, &lh, nfsl_list, nlp) {
2407 nfscl_freelockowner(lp, 0);
2411 * Do the recall on any delegations. To avoid trouble, always
2412 * come back up here after having slept.
2416 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2417 while (dp != NULL) {
2418 ndp = TAILQ_NEXT(dp, nfsdl_list);
2419 if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2421 * Wait for outstanding I/O ops to be done.
2423 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2425 nfsv4_unlock(&clp->nfsc_lock, 0);
2428 dp->nfsdl_rwlock.nfslock_lock |=
2430 (void) nfsmsleep(&dp->nfsdl_rwlock,
2431 NFSCLSTATEMUTEXPTR, PZERO, "nfscld",
2436 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2437 &islept, NFSCLSTATEMUTEXPTR);
2442 newnfs_copycred(&dp->nfsdl_cred, cred);
2443 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2446 nfscl_cleandeleg(dp);
2447 TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2449 LIST_REMOVE(dp, nfsdl_hash);
2450 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2452 newnfsstats.cldelegates--;
2460 * Clear out old delegations, if we are above the high water
2461 * mark. Only clear out ones with no state related to them.
2462 * The tailq list is in LRU order.
2464 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2465 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2466 ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2467 if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2468 dp->nfsdl_rwlock.nfslock_lock == 0 &&
2469 dp->nfsdl_timestamp < NFSD_MONOSEC &&
2470 !(dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2471 NFSCLDL_NEEDRECLAIM))) {
2473 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2474 op = LIST_FIRST(&owp->nfsow_open);
2481 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2482 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2489 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2490 LIST_REMOVE(dp, nfsdl_hash);
2491 TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2493 newnfsstats.cldelegates--;
2499 nfsv4_unlock(&clp->nfsc_lock, 0);
2503 * Delegreturn any delegations cleaned out or recalled.
2505 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2506 newnfs_copycred(&dp->nfsdl_cred, cred);
2507 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2508 TAILQ_REMOVE(&dh, dp, nfsdl_list);
2509 FREE((caddr_t)dp, M_NFSCLDELEG);
2512 #if defined(APPLEKEXT) || defined(__FreeBSD__)
2514 * Simulate the calls to nfscl_cleanup() when a process
2515 * exits, since the call can't be patched into exit().
2518 struct timespec mytime;
2519 static time_t prevsec = 0;
2521 NFSGETNANOTIME(&mytime);
2522 if (prevsec != mytime.tv_sec) {
2523 prevsec = mytime.tv_sec;
2524 nfscl_cleanupkext(clp);
2527 #endif /* APPLEKEXT || __FreeBSD__ */
2529 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2530 (void) tsleep((caddr_t)clp, PWAIT, "nfscl", hz);
2531 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2533 clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2534 wakeup((caddr_t)clp);
2541 * Initiate state recovery. Called when NFSERR_STALECLIENTID or
2542 * NFSERR_STALESTATEID is received.
2545 nfscl_initiate_recovery(struct nfsclclient *clp)
2551 clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2553 wakeup((caddr_t)clp);
2557 * Dump out the state stuff for debugging.
2560 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
2561 int lockowner, int locks)
2563 struct nfsclclient *clp;
2564 struct nfsclowner *owp;
2565 struct nfsclopen *op;
2566 struct nfscllockowner *lp;
2567 struct nfscllock *lop;
2568 struct nfscldeleg *dp;
2572 printf("nfscl dumpstate NULL clp\n");
2576 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2577 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2578 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2579 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2580 owp->nfsow_owner[0], owp->nfsow_owner[1],
2581 owp->nfsow_owner[2], owp->nfsow_owner[3],
2583 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2585 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2586 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2587 op->nfso_stateid.other[2], op->nfso_opencnt,
2589 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2591 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2592 lp->nfsl_owner[0], lp->nfsl_owner[1],
2593 lp->nfsl_owner[2], lp->nfsl_owner[3],
2595 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2596 lp->nfsl_stateid.other[2]);
2597 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2600 printf("lck typ=%d fst=%ju end=%ju\n",
2601 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2602 (intmax_t)lop->nfslo_end);
2604 printf("lck typ=%d fst=%qd end=%qd\n",
2605 lop->nfslo_type, lop->nfslo_first,
2613 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2614 if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2615 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2616 owp->nfsow_owner[0], owp->nfsow_owner[1],
2617 owp->nfsow_owner[2], owp->nfsow_owner[3],
2619 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2621 printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2622 op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2623 op->nfso_stateid.other[2], op->nfso_opencnt,
2625 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2627 printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2628 lp->nfsl_owner[0], lp->nfsl_owner[1],
2629 lp->nfsl_owner[2], lp->nfsl_owner[3],
2631 lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2632 lp->nfsl_stateid.other[2]);
2633 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2636 printf("lck typ=%d fst=%ju end=%ju\n",
2637 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2638 (intmax_t)lop->nfslo_end);
2640 printf("lck typ=%d fst=%qd end=%qd\n",
2641 lop->nfslo_type, lop->nfslo_first,
2652 * Check for duplicate open owners and opens.
2653 * (Only used as a diagnostic aid.)
2656 nfscl_dupopen(vnode_t vp, int dupopens)
2658 struct nfsclclient *clp;
2659 struct nfsclowner *owp, *owp2;
2660 struct nfsclopen *op, *op2;
2663 clp = VFSTONFS(vnode_mount(vp))->nm_clp;
2665 printf("nfscl dupopen NULL clp\n");
2668 nfhp = VTONFS(vp)->n_fhp;
2672 * First, search for duplicate owners.
2673 * These should never happen!
2675 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2676 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2678 !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
2679 NFSV4CL_LOCKNAMELEN)) {
2681 printf("DUP OWNER\n");
2682 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0, 0);
2689 * Now, search for duplicate stateids.
2690 * These shouldn't happen, either.
2692 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2693 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
2694 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2695 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2697 (op->nfso_stateid.other[0] != 0 ||
2698 op->nfso_stateid.other[1] != 0 ||
2699 op->nfso_stateid.other[2] != 0) &&
2700 op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
2701 op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
2702 op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
2704 printf("DUP STATEID\n");
2705 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0,
2715 * Now search for duplicate opens.
2716 * Duplicate opens for the same owner
2717 * should never occur. Other duplicates are
2718 * possible and are checked for if "dupopens"
2721 LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2722 LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
2723 if (nfhp->nfh_len == op2->nfso_fhlen &&
2724 !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
2725 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2726 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2727 if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
2728 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
2729 (!NFSBCMP(op->nfso_own->nfsow_owner,
2730 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
2732 if (!NFSBCMP(op->nfso_own->nfsow_owner,
2733 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
2735 printf("BADDUP OPEN\n");
2738 printf("DUP OPEN\n");
2740 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1,
2753 * During close, find an open that needs to be dereferenced and
2754 * dereference it. If there are no more opens for this file,
2755 * log a message to that effect.
2756 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
2757 * on the file's vnode.
2758 * This is the safe way, since it is difficult to identify
2759 * which open the close is for and I/O can be performed after the
2760 * close(2) system call when a file is mmap'd.
2761 * If it returns 0 for success, there will be a referenced
2762 * clp returned via clpp.
2765 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
2767 struct nfsclclient *clp;
2768 struct nfsclowner *owp;
2769 struct nfsclopen *op;
2770 struct nfscldeleg *dp;
2774 error = nfscl_getcl(vp, NULL, NULL, &clp);
2779 nfhp = VTONFS(vp)->n_fhp;
2783 * First, look for one under a delegation that was locally issued
2784 * and just decrement the opencnt for it. Since all my Opens against
2785 * the server are DENY_NONE, I don't see a problem with hanging
2786 * onto them. (It is much easier to use one of the extant Opens
2787 * that I already have on the server when a Delegation is recalled
2788 * than to do fresh Opens.) Someday, I might need to rethink this, but.
2790 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
2792 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2793 op = LIST_FIRST(&owp->nfsow_open);
2796 * Since a delegation is for a file, there
2797 * should never be more than one open for
2800 if (LIST_NEXT(op, nfso_list) != NULL)
2801 panic("nfscdeleg opens");
2802 if (notdecr && op->nfso_opencnt > 0) {
2811 /* Now process the opens against the server. */
2812 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2813 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2814 if (op->nfso_fhlen == nfhp->nfh_len &&
2815 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
2817 /* Found an open, decrement cnt if possible */
2818 if (notdecr && op->nfso_opencnt > 0) {
2823 * There are more opens, so just return.
2825 if (op->nfso_opencnt > 0) {
2834 printf("nfscl: never fnd open\n");
2839 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
2841 struct nfsclclient *clp;
2842 struct nfsclowner *owp, *nowp;
2843 struct nfsclopen *op;
2844 struct nfscldeleg *dp;
2848 error = nfscl_getcl(vp, NULL, NULL, &clp);
2853 nfhp = VTONFS(vp)->n_fhp;
2856 * First get rid of the local Open structures, which should be no
2859 dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
2861 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
2862 op = LIST_FIRST(&owp->nfsow_open);
2864 KASSERT((op->nfso_opencnt == 0),
2865 ("nfscl: bad open cnt on deleg"));
2866 nfscl_freeopen(op, 1);
2868 nfscl_freeopenowner(owp, 1);
2872 /* Now process the opens against the server. */
2874 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2875 op = LIST_FIRST(&owp->nfsow_open);
2876 while (op != NULL) {
2877 if (op->nfso_fhlen == nfhp->nfh_len &&
2878 !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
2880 /* Found an open, close it. */
2881 KASSERT((op->nfso_opencnt == 0),
2882 ("nfscl: bad open cnt on server"));
2884 nfsrpc_doclose(VFSTONFS(vnode_mount(vp)), op,
2889 op = LIST_NEXT(op, nfso_list);
2897 * Return all delegations on this client.
2898 * (Must be called with client sleep lock.)
2901 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
2903 struct nfscldeleg *dp, *ndp;
2906 cred = newnfs_getcred();
2907 TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
2908 nfscl_cleandeleg(dp);
2909 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2910 nfscl_freedeleg(&clp->nfsc_deleg, dp);
2916 * Do a callback RPC.
2919 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
2923 struct nfsclclient *clp;
2924 struct nfscldeleg *dp = NULL;
2925 int numops, taglen = -1, error = 0, trunc, ret = 0;
2926 u_int32_t minorvers, retops = 0, *retopsp = NULL, *repp, cbident;
2927 u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
2933 nfsattrbit_t attrbits, rattrbits;
2934 nfsv4stateid_t stateid;
2937 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2938 taglen = fxdr_unsigned(int, *tl);
2943 if (taglen <= NFSV4_SMALLSTR)
2946 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2947 error = nfsrv_mtostr(nd, tagstr, taglen);
2949 if (taglen > NFSV4_SMALLSTR)
2950 free(tagstr, M_TEMP);
2954 (void) nfsm_strtom(nd, tag, taglen);
2955 if (taglen > NFSV4_SMALLSTR) {
2956 free(tagstr, M_TEMP);
2958 NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
2959 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2960 minorvers = fxdr_unsigned(u_int32_t, *tl++);
2961 if (minorvers != NFSV4_MINORVERSION)
2962 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
2963 cbident = fxdr_unsigned(u_int32_t, *tl++);
2967 numops = fxdr_unsigned(int, *tl);
2969 * Loop around doing the sub ops.
2971 for (i = 0; i < numops; i++) {
2972 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2973 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
2975 op = fxdr_unsigned(int, *tl);
2976 if (op < NFSV4OP_CBGETATTR || op > NFSV4OP_CBRECALL) {
2977 nd->nd_repstat = NFSERR_OPILLEGAL;
2978 *repp = nfscl_errmap(nd);
2982 nd->nd_procnum = op;
2983 newnfsstats.cbrpccnt[nd->nd_procnum]++;
2985 case NFSV4OP_CBGETATTR:
2987 error = nfsm_getfh(nd, &nfhp);
2989 error = nfsrv_getattrbits(nd, &attrbits,
2992 mp = nfscl_getmnt(cbident);
2994 error = NFSERR_SERVERFAULT;
2999 clp = nfscl_findcl(VFSTONFS(mp));
3001 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3005 error = NFSERR_SERVERFAULT;
3008 ret = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3009 nfhp->nfh_len, p, &np);
3014 FREE((caddr_t)nfhp, M_NFSFH);
3016 NFSZERO_ATTRBIT(&rattrbits);
3017 if (NFSISSET_ATTRBIT(&attrbits,
3020 va.va_size = np->n_size;
3022 va.va_size = dp->nfsdl_size;
3023 NFSSETBIT_ATTRBIT(&rattrbits,
3026 if (NFSISSET_ATTRBIT(&attrbits,
3027 NFSATTRBIT_CHANGE)) {
3028 va.va_filerev = dp->nfsdl_change;
3029 if (ret || (np->n_flag & NDELEGMOD))
3031 NFSSETBIT_ATTRBIT(&rattrbits,
3034 (void) nfsv4_fillattr(nd, NULL, NULL, &va,
3035 NULL, 0, &rattrbits, NULL, NULL, 0, 0);
3040 case NFSV4OP_CBRECALL:
3042 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3044 stateid.seqid = *tl++;
3045 NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3047 tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3048 trunc = fxdr_unsigned(int, *tl);
3049 error = nfsm_getfh(nd, &nfhp);
3051 mp = nfscl_getmnt(cbident);
3053 error = NFSERR_SERVERFAULT;
3057 clp = nfscl_findcl(VFSTONFS(mp));
3059 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3064 wakeup((caddr_t)clp);
3067 error = NFSERR_SERVERFAULT;
3072 FREE((caddr_t)nfhp, M_NFSFH);
3076 if (error == EBADRPC || error == NFSERR_BADXDR) {
3077 nd->nd_repstat = NFSERR_BADXDR;
3079 nd->nd_repstat = error;
3084 if (nd->nd_repstat) {
3085 *repp = nfscl_errmap(nd);
3088 *repp = 0; /* NFS4_OK */
3092 if (error == EBADRPC || error == NFSERR_BADXDR)
3093 nd->nd_repstat = NFSERR_BADXDR;
3095 printf("nfsv4 comperr1=%d\n", error);
3098 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3102 *retopsp = txdr_unsigned(retops);
3104 *nd->nd_errp = nfscl_errmap(nd);
3108 * Generate the next cbident value. Basically just increment a static value
3109 * and then check that it isn't already in the list, if it has wrapped around.
3112 nfscl_nextcbident(void)
3114 struct nfsclclient *clp;
3116 static u_int32_t nextcbident = 0;
3117 static int haswrapped = 0;
3120 if (nextcbident == 0)
3124 * Search the clientid list for one already using this cbident.
3129 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3130 if (clp->nfsc_cbident == nextcbident) {
3140 return (nextcbident);
3144 * Get the mount point related to a given cbident.
3147 nfscl_getmnt(u_int32_t cbident)
3149 struct nfsclclient *clp;
3150 struct nfsmount *nmp;
3153 LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3154 if (clp->nfsc_cbident == cbident)
3161 nmp = clp->nfsc_nmp;
3163 return (nmp->nm_mountp);
3167 * Search for a lock conflict locally on the client. A conflict occurs if
3168 * - not same owner and overlapping byte range and at least one of them is
3169 * a write lock or this is an unlock.
3172 nfscl_localconflict(struct nfsclclient *clp, struct nfscllock *nlop,
3173 u_int8_t *own, struct nfscldeleg *dp, struct nfscllock **lopp)
3175 struct nfsclowner *owp;
3176 struct nfsclopen *op;
3180 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3184 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3185 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3186 ret = nfscl_checkconflict(&op->nfso_lock, nlop, own,
3196 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3197 u_int8_t *own, struct nfscllock **lopp)
3199 struct nfscllockowner *lp;
3200 struct nfscllock *lop;
3202 LIST_FOREACH(lp, lhp, nfsl_list) {
3203 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
3204 LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3205 if (lop->nfslo_first >= nlop->nfslo_end)
3207 if (lop->nfslo_end <= nlop->nfslo_first)
3209 if (lop->nfslo_type == F_WRLCK ||
3210 nlop->nfslo_type == F_WRLCK ||
3211 nlop->nfslo_type == F_UNLCK) {
3214 return (NFSERR_DENIED);
3223 * Check for a local conflicting lock.
3226 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
3227 u_int64_t len, struct flock *fl, NFSPROC_T *p)
3229 struct nfscllock *lop, nlck;
3230 struct nfscldeleg *dp;
3232 u_int8_t own[NFSV4CL_LOCKNAMELEN];
3235 nlck.nfslo_type = fl->l_type;
3236 nlck.nfslo_first = off;
3237 if (len == NFS64BITSSET) {
3238 nlck.nfslo_end = NFS64BITSSET;
3240 nlck.nfslo_end = off + len;
3241 if (nlck.nfslo_end <= nlck.nfslo_first)
3242 return (NFSERR_INVAL);
3245 nfscl_filllockowner(p, own);
3247 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3248 error = nfscl_localconflict(clp, &nlck, own, dp, &lop);
3249 if (error == NFSERR_DENIED)
3252 fl->l_whence = SEEK_SET;
3253 fl->l_start = lop->nfslo_first;
3254 if (lop->nfslo_end == NFS64BITSSET)
3257 fl->l_len = lop->nfslo_end - lop->nfslo_first;
3258 fl->l_pid = (pid_t)0;
3259 fl->l_type = lop->nfslo_type;
3260 } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
3261 fl->l_type == F_RDLCK)) {
3263 * The delegation ensures that there isn't a conflicting
3264 * lock on the server, so return -1 to indicate an RPC
3267 fl->l_type = F_UNLCK;
3275 * Handle Recall of a delegation.
3276 * The clp must be exclusive locked when this is called.
3279 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
3280 struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p)
3282 struct nfsclowner *owp, *lowp, *nowp;
3283 struct nfsclopen *op, *lop;
3284 struct nfscllockowner *lp;
3285 struct nfscllock *lckp;
3287 int error = 0, ret, gotvp = 0;
3291 * First, get a vnode for the file. This is needed to do RPCs.
3293 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
3294 dp->nfsdl_fhlen, p, &np);
3297 * File isn't open, so nothing to move over to the
3307 dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
3308 NFSINVALATTRCACHE(np);
3311 * Ok, if it's a write delegation, flush data to the server, so
3312 * that close/open consistency is retained.
3315 if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
3317 OSBitOrAtomic((u_int32_t)NDELEGRECALL, (UInt32 *)&np->n_flag);
3319 np->n_flag |= NDELEGRECALL;
3322 (void) ncl_flush(vp, MNT_WAIT, cred, p, 1);
3325 OSBitAndAtomic((int32_t)~(NMODIFIED | NDELEGRECALL), (UInt32 *)&np->n_flag);
3327 np->n_flag &= ~(NMODIFIED | NDELEGRECALL);
3333 * Now, for each openowner with opens issued locally, move them
3334 * over to state against the server.
3336 LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
3337 lop = LIST_FIRST(&lowp->nfsow_open);
3339 if (LIST_NEXT(lop, nfso_list) != NULL)
3340 panic("nfsdlg mult opens");
3342 * Look for the same openowner against the server.
3344 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3345 if (!NFSBCMP(lowp->nfsow_owner,
3346 owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3347 newnfs_copycred(&dp->nfsdl_cred, cred);
3348 ret = nfscl_moveopen(vp, clp, nmp, lop,
3350 if (ret == NFSERR_STALECLIENTID ||
3351 ret == NFSERR_STALEDONTRECOVER) {
3357 nfscl_freeopen(lop, 1);
3366 * If no openowner found, create one and get an open
3370 MALLOC(nowp, struct nfsclowner *,
3371 sizeof (struct nfsclowner), M_NFSCLOWNER,
3373 nfscl_newopen(clp, NULL, &owp, &nowp, &op,
3374 NULL, lowp->nfsow_owner, dp->nfsdl_fh,
3375 dp->nfsdl_fhlen, NULL);
3376 newnfs_copycred(&dp->nfsdl_cred, cred);
3377 ret = nfscl_moveopen(vp, clp, nmp, lop,
3380 nfscl_freeopenowner(owp, 0);
3381 if (ret == NFSERR_STALECLIENTID ||
3382 ret == NFSERR_STALEDONTRECOVER) {
3388 nfscl_freeopen(lop, 1);
3398 * Now, get byte range locks for any locks done locally.
3400 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
3401 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
3402 newnfs_copycred(&dp->nfsdl_cred, cred);
3403 ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
3404 if (ret == NFSERR_STALESTATEID ||
3405 ret == NFSERR_STALEDONTRECOVER ||
3406 ret == NFSERR_STALECLIENTID) {
3421 * Move a locally issued open over to an owner on the state list.
3422 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
3423 * returns with it unlocked.
3426 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
3427 struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
3428 struct ucred *cred, NFSPROC_T *p)
3430 struct nfsclopen *op, *nop;
3431 struct nfscldeleg *ndp;
3433 int error = 0, newone;
3436 * First, look for an appropriate open, If found, just increment the
3439 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3440 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
3441 op->nfso_fhlen == lop->nfso_fhlen &&
3442 !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
3443 op->nfso_opencnt += lop->nfso_opencnt;
3444 nfscl_freeopen(lop, 1);
3449 /* No appropriate open, so we have to do one against the server. */
3451 MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
3452 lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
3454 nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
3455 lop->nfso_fh, lop->nfso_fhlen, &newone);
3457 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
3458 lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
3459 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
3462 nfscl_freeopen(op, 0);
3465 newnfs_copyincred(cred, &op->nfso_cred);
3466 op->nfso_mode |= lop->nfso_mode;
3467 op->nfso_opencnt += lop->nfso_opencnt;
3468 nfscl_freeopen(lop, 1);
3471 FREE((caddr_t)nop, M_NFSCLOPEN);
3474 * What should I do with the returned delegation, since the
3475 * delegation is being recalled? For now, just printf and
3478 printf("Moveopen returned deleg\n");
3479 FREE((caddr_t)ndp, M_NFSCLDELEG);
3485 * Recall all delegations on this client.
3488 nfscl_totalrecall(struct nfsclclient *clp)
3490 struct nfscldeleg *dp;
3492 TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
3493 dp->nfsdl_flags |= NFSCLDL_RECALL;
3497 * Relock byte ranges. Called for delegation recall and state expiry.
3500 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
3501 struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
3504 struct nfscllockowner *nlp;
3507 u_int32_t clidrev = 0;
3508 int error, newone, donelocally;
3510 off = lop->nfslo_first;
3511 len = lop->nfslo_end - lop->nfslo_first;
3512 error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
3513 clp, 1, lp->nfsl_owner, lp->nfsl_openowner, &nlp, &newone,
3515 if (error || donelocally)
3517 if (nmp->nm_clp != NULL)
3518 clidrev = nmp->nm_clp->nfsc_clientidrev;
3521 nfhp = VTONFS(vp)->n_fhp;
3522 error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
3523 nfhp->nfh_len, nlp, newone, 0, off,
3524 len, lop->nfslo_type, cred, p);
3526 nfscl_freelockowner(nlp, 0);
3531 * Called to re-open a file. Basically get a vnode for the file handle
3532 * and then call nfsrpc_openrpc() to do the rest.
3535 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
3536 u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
3537 struct ucred *cred, NFSPROC_T *p)
3543 error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
3547 if (np->n_v4 != NULL) {
3548 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
3549 np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
3550 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
3560 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
3561 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
3565 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
3566 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
3567 u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
3568 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
3573 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
3574 mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
3576 if (error == NFSERR_DELAY)
3577 (void) nfs_catnap(PZERO, "nfstryop");
3578 } while (error == NFSERR_DELAY);
3579 if (error == EAUTH || error == EACCES) {
3580 /* Try again using system credentials */
3581 newnfs_setroot(cred);
3583 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
3584 newfhlen, mode, op, name, namelen, ndpp, reclaim,
3585 delegtype, cred, p, 1, 0);
3586 if (error == NFSERR_DELAY)
3587 (void) nfs_catnap(PZERO, "nfstryop");
3588 } while (error == NFSERR_DELAY);
3594 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
3595 * NFSERR_DELAY. Also, retry with system credentials, if the provided
3599 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
3600 int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
3601 u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
3603 struct nfsrv_descript nfsd, *nd = &nfsd;
3607 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
3608 reclaim, off, len, type, cred, p, 0);
3609 if (!error && nd->nd_repstat == NFSERR_DELAY)
3610 (void) nfs_catnap(PZERO, "nfstrylck");
3611 } while (!error && nd->nd_repstat == NFSERR_DELAY);
3613 error = nd->nd_repstat;
3614 if (error == EAUTH || error == EACCES) {
3615 /* Try again using root credentials */
3616 newnfs_setroot(cred);
3618 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
3619 newone, reclaim, off, len, type, cred, p, 1);
3620 if (!error && nd->nd_repstat == NFSERR_DELAY)
3621 (void) nfs_catnap(PZERO, "nfstrylck");
3622 } while (!error && nd->nd_repstat == NFSERR_DELAY);
3624 error = nd->nd_repstat;
3630 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
3631 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
3635 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
3636 struct nfsmount *nmp, NFSPROC_T *p)
3641 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
3642 if (error == NFSERR_DELAY)
3643 (void) nfs_catnap(PZERO, "nfstrydp");
3644 } while (error == NFSERR_DELAY);
3645 if (error == EAUTH || error == EACCES) {
3646 /* Try again using system credentials */
3647 newnfs_setroot(cred);
3649 error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
3650 if (error == NFSERR_DELAY)
3651 (void) nfs_catnap(PZERO, "nfstrydp");
3652 } while (error == NFSERR_DELAY);
3658 * Try a close against the server. Just call nfsrpc_closerpc(),
3659 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
3663 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
3664 struct nfsmount *nmp, NFSPROC_T *p)
3666 struct nfsrv_descript nfsd, *nd = &nfsd;
3670 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
3671 if (error == NFSERR_DELAY)
3672 (void) nfs_catnap(PZERO, "nfstrycl");
3673 } while (error == NFSERR_DELAY);
3674 if (error == EAUTH || error == EACCES) {
3675 /* Try again using system credentials */
3676 newnfs_setroot(cred);
3678 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
3679 if (error == NFSERR_DELAY)
3680 (void) nfs_catnap(PZERO, "nfstrycl");
3681 } while (error == NFSERR_DELAY);
3687 * Decide if a delegation on a file permits close without flushing writes
3688 * to the server. This might be a big performance win in some environments.
3689 * (Not useful until the client does caching on local stable storage.)
3692 nfscl_mustflush(vnode_t vp)
3694 struct nfsclclient *clp;
3695 struct nfscldeleg *dp;
3697 struct nfsmount *nmp;
3700 nmp = VFSTONFS(vnode_mount(vp));
3701 if (!NFSHASNFSV4(nmp))
3704 clp = nfscl_findcl(nmp);
3709 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3710 if (dp != NULL && (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_RECALL))
3712 (dp->nfsdl_sizelimit >= np->n_size ||
3713 !NFSHASSTRICT3530(nmp))) {
3722 * See if a (write) delegation exists for this file.
3725 nfscl_nodeleg(vnode_t vp, int writedeleg)
3727 struct nfsclclient *clp;
3728 struct nfscldeleg *dp;
3730 struct nfsmount *nmp;
3733 nmp = VFSTONFS(vnode_mount(vp));
3734 if (!NFSHASNFSV4(nmp))
3737 clp = nfscl_findcl(nmp);
3742 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3743 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_RECALL) == 0 &&
3744 (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE)
3745 == NFSCLDL_WRITE)) {
3754 * Look for an associated delegation that should be DelegReturned.
3757 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
3759 struct nfsclclient *clp;
3760 struct nfscldeleg *dp;
3761 struct nfsclowner *owp;
3762 struct nfscllockowner *lp;
3763 struct nfsmount *nmp;
3766 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
3768 nmp = VFSTONFS(vnode_mount(vp));
3772 * Loop around waiting for:
3773 * - outstanding I/O operations on delegations to complete
3774 * - for a delegation on vp that has state, lock the client and
3776 * - return delegation with no state
3779 clp = nfscl_findcl(nmp);
3784 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3785 np->n_fhp->nfh_len);
3788 * Wait for outstanding I/O ops to be done.
3790 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
3792 nfsv4_unlock(&clp->nfsc_lock, 0);
3795 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
3796 (void) nfsmsleep(&dp->nfsdl_rwlock,
3797 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
3801 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3802 if (!LIST_EMPTY(&owp->nfsow_open)) {
3808 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
3809 if (!LIST_EMPTY(&lp->nfsl_lock)) {
3815 if (needsrecall && !triedrecall) {
3818 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
3819 &islept, NFSCLSTATEMUTEXPTR);
3826 cred = newnfs_getcred();
3827 newnfs_copycred(&dp->nfsdl_cred, cred);
3828 (void) nfscl_recalldeleg(clp, nmp, dp, vp, cred, p);
3832 nfsv4_unlock(&clp->nfsc_lock, 0);
3836 *stp = dp->nfsdl_stateid;
3838 nfscl_cleandeleg(dp);
3839 nfscl_freedeleg(&clp->nfsc_deleg, dp);
3842 nfsv4_unlock(&clp->nfsc_lock, 0);
3849 * Look for associated delegation(s) that should be DelegReturned.
3852 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
3853 nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
3855 struct nfsclclient *clp;
3856 struct nfscldeleg *dp;
3857 struct nfsclowner *owp;
3858 struct nfscllockowner *lp;
3859 struct nfsmount *nmp;
3862 int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
3864 nmp = VFSTONFS(vnode_mount(fvp));
3869 * Loop around waiting for:
3870 * - outstanding I/O operations on delegations to complete
3871 * - for a delegation on fvp that has state, lock the client and
3873 * - return delegation(s) with no state.
3876 clp = nfscl_findcl(nmp);
3882 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3883 np->n_fhp->nfh_len);
3884 if (dp != NULL && *gotfdp == 0) {
3886 * Wait for outstanding I/O ops to be done.
3888 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
3890 nfsv4_unlock(&clp->nfsc_lock, 0);
3893 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
3894 (void) nfsmsleep(&dp->nfsdl_rwlock,
3895 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
3899 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3900 if (!LIST_EMPTY(&owp->nfsow_open)) {
3906 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
3907 if (!LIST_EMPTY(&lp->nfsl_lock)) {
3913 if (needsrecall && !triedrecall) {
3916 igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
3917 &islept, NFSCLSTATEMUTEXPTR);
3924 cred = newnfs_getcred();
3925 newnfs_copycred(&dp->nfsdl_cred, cred);
3926 (void) nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p);
3930 nfsv4_unlock(&clp->nfsc_lock, 0);
3934 *fstp = dp->nfsdl_stateid;
3937 nfscl_cleandeleg(dp);
3938 nfscl_freedeleg(&clp->nfsc_deleg, dp);
3941 nfsv4_unlock(&clp->nfsc_lock, 0);
3946 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3947 np->n_fhp->nfh_len);
3948 if (dp != NULL && *gottdp == 0) {
3950 * Wait for outstanding I/O ops to be done.
3952 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
3953 dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
3954 (void) nfsmsleep(&dp->nfsdl_rwlock,
3955 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
3958 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3959 if (!LIST_EMPTY(&owp->nfsow_open)) {
3964 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
3965 if (!LIST_EMPTY(&lp->nfsl_lock)) {
3970 *tstp = dp->nfsdl_stateid;
3973 nfscl_cleandeleg(dp);
3974 nfscl_freedeleg(&clp->nfsc_deleg, dp);
3983 * Get a reference on the clientid associated with the mount point.
3984 * Return 1 if success, 0 otherwise.
3987 nfscl_getref(struct nfsmount *nmp)
3989 struct nfsclclient *clp;
3992 clp = nfscl_findcl(nmp);
3997 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR);
4003 * Release a reference on a clientid acquired with the above call.
4006 nfscl_relref(struct nfsmount *nmp)
4008 struct nfsclclient *clp;
4011 clp = nfscl_findcl(nmp);
4016 nfsv4_relref(&clp->nfsc_lock);
4021 * Save the size attribute in the delegation, since the nfsnode
4025 nfscl_reclaimnode(vnode_t vp)
4027 struct nfsclclient *clp;
4028 struct nfscldeleg *dp;
4029 struct nfsnode *np = VTONFS(vp);
4030 struct nfsmount *nmp;
4032 nmp = VFSTONFS(vnode_mount(vp));
4033 if (!NFSHASNFSV4(nmp))
4036 clp = nfscl_findcl(nmp);
4041 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4042 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4043 dp->nfsdl_size = np->n_size;
4048 * Get the saved size attribute in the delegation, since it is a
4049 * newly allocated nfsnode.
4052 nfscl_newnode(vnode_t vp)
4054 struct nfsclclient *clp;
4055 struct nfscldeleg *dp;
4056 struct nfsnode *np = VTONFS(vp);
4057 struct nfsmount *nmp;
4059 nmp = VFSTONFS(vnode_mount(vp));
4060 if (!NFSHASNFSV4(nmp))
4063 clp = nfscl_findcl(nmp);
4068 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4069 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4070 np->n_size = dp->nfsdl_size;
4075 * If there is a valid write delegation for this file, set the modtime
4076 * to the local clock time.
4079 nfscl_delegmodtime(vnode_t vp)
4081 struct nfsclclient *clp;
4082 struct nfscldeleg *dp;
4083 struct nfsnode *np = VTONFS(vp);
4084 struct nfsmount *nmp;
4086 nmp = VFSTONFS(vnode_mount(vp));
4087 if (!NFSHASNFSV4(nmp))
4090 clp = nfscl_findcl(nmp);
4095 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4096 if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4097 NFSGETNANOTIME(&dp->nfsdl_modtime);
4098 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4104 * If there is a valid write delegation for this file with a modtime set,
4105 * put that modtime in mtime.
4108 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4110 struct nfsclclient *clp;
4111 struct nfscldeleg *dp;
4112 struct nfsnode *np = VTONFS(vp);
4113 struct nfsmount *nmp;
4115 nmp = VFSTONFS(vnode_mount(vp));
4116 if (!NFSHASNFSV4(nmp))
4119 clp = nfscl_findcl(nmp);
4124 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4126 (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4127 (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4128 *mtime = dp->nfsdl_modtime;
4133 nfscl_errmap(struct nfsrv_descript *nd)
4135 short *defaulterrp, *errp;
4137 if (!nd->nd_repstat)
4139 if (nd->nd_procnum == NFSPROC_NOOP)
4140 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4141 if (nd->nd_repstat == EBADRPC)
4142 return (txdr_unsigned(NFSERR_BADXDR));
4143 if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4144 nd->nd_repstat == NFSERR_OPILLEGAL)
4145 return (txdr_unsigned(nd->nd_repstat));
4146 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4148 if (*errp == (short)nd->nd_repstat)
4149 return (txdr_unsigned(nd->nd_repstat));
4150 return (txdr_unsigned(*defaulterrp));