]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nfsclient/nfs_clstate.c
MFC
[FreeBSD/FreeBSD.git] / sys / fs / nfsclient / nfs_clstate.c
1 /*-
2  * Copyright (c) 2009 Rick Macklem, University of Guelph
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * These functions implement the client side state handling for NFSv4.
33  * NFSv4 state handling:
34  * - A lockowner is used to determine lock contention, so it
35  *   corresponds directly to a Posix pid. (1 to 1 mapping)
36  * - The correct granularity of an OpenOwner is not nearly so
37  *   obvious. An OpenOwner does the following:
38  *   - provides a serial sequencing of Open/Close/Lock-with-new-lockowner
39  *   - is used to check for Open/Share contention (not applicable to
40  *     this client, since all Opens are Deny_None)
41  *   As such, I considered both extreme.
42  *   1 OpenOwner per ClientID - Simple to manage, but fully serializes
43  *   all Open, Close and Lock (with a new lockowner) Ops.
44  *   1 OpenOwner for each Open - This one results in an OpenConfirm for
45  *   every Open, for most servers.
46  *   So, I chose to use the same mapping as I did for LockOwnwers.
47  *   The main concern here is that you can end up with multiple Opens
48  *   for the same File Handle, but on different OpenOwners (opens
49  *   inherited from parents, grandparents...) and you do not know
50  *   which of these the vnodeop close applies to. This is handled by
51  *   delaying the Close Op(s) until all of the Opens have been closed.
52  *   (It is not yet obvious if this is the correct granularity.)
53  * - How the code handles serialization:
54  *   - For the ClientId, it uses an exclusive lock while getting its
55  *     SetClientId and during recovery. Otherwise, it uses a shared
56  *     lock via a reference count.
57  *   - For the rest of the data structures, it uses an SMP mutex
58  *     (once the nfs client is SMP safe) and doesn't sleep while
59  *     manipulating the linked lists.
60  *   - The serialization of Open/Close/Lock/LockU falls out in the
61  *     "wash", since OpenOwners and LockOwners are both mapped from
62  *     Posix pid. In other words, there is only one Posix pid using
63  *     any given owner, so that owner is serialized. (If you change
64  *     the granularity of the OpenOwner, then code must be added to
65  *     serialize Ops on the OpenOwner.)
66  * - When to get rid of OpenOwners and LockOwners.
67  *   - The function nfscl_cleanup_common() is executed after a process exits.
68  *     It goes through the client list looking for all Open and Lock Owners.
69  *     When one is found, it is marked "defunct" or in the case of
70  *     an OpenOwner without any Opens, freed.
71  *     The renew thread scans for defunct Owners and gets rid of them,
72  *     if it can. The LockOwners will also be deleted when the
73  *     associated Open is closed.
74  *   - If the LockU or Close Op(s) fail during close in a way
75  *     that could be recovered upon retry, they are relinked to the
76  *     ClientId's defunct open list and retried by the renew thread
77  *     until they succeed or an unmount/recovery occurs.
78  *     (Since we are done with them, they do not need to be recovered.)
79  */
80
81 #ifndef APPLEKEXT
82 #include <fs/nfs/nfsport.h>
83
84 /*
85  * Global variables
86  */
87 extern struct nfsstats newnfsstats;
88 extern struct nfsreqhead nfsd_reqq;
89 NFSREQSPINLOCK;
90 NFSCLSTATEMUTEX;
91 int nfscl_inited = 0;
92 struct nfsclhead nfsclhead;     /* Head of clientid list */
93 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
94 #endif  /* !APPLEKEXT */
95
96 static int nfscl_delegcnt = 0;
97 static int nfscl_getopen(struct nfsclownerhead *, u_int8_t *, int, u_int8_t *,
98     u_int8_t *, u_int32_t, struct nfscllockowner **, 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 *,
114     int);
115 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
116     u_int8_t *, struct nfscllock **);
117 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
118 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
119     struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
120 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
121     struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
122     struct nfsclopen **, u_int8_t *, u_int8_t *, int, int *);
123 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
124     struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
125     struct nfscldeleg *, struct ucred *, NFSPROC_T *);
126 static void nfscl_totalrecall(struct nfsclclient *);
127 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
128     struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
129 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
130     u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
131     struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
132 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
133     int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
134     struct ucred *, NFSPROC_T *);
135 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
136     struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
137 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *);
138 static int nfscl_errmap(struct nfsrv_descript *);
139 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
140 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
141     struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int);
142 static void nfscl_freeopenowner(struct nfsclowner *, int);
143 static void nfscl_cleandeleg(struct nfscldeleg *);
144 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
145     struct nfsmount *, NFSPROC_T *);
146
147 static short nfscberr_null[] = {
148         0,
149         0,
150 };
151
152 static short nfscberr_getattr[] = {
153         NFSERR_RESOURCE,
154         NFSERR_BADHANDLE,
155         NFSERR_BADXDR,
156         NFSERR_RESOURCE,
157         NFSERR_SERVERFAULT,
158         0,
159 };
160
161 static short nfscberr_recall[] = {
162         NFSERR_RESOURCE,
163         NFSERR_BADHANDLE,
164         NFSERR_BADSTATEID,
165         NFSERR_BADXDR,
166         NFSERR_RESOURCE,
167         NFSERR_SERVERFAULT,
168         0,
169 };
170
171 static short *nfscl_cberrmap[] = {
172         nfscberr_null,
173         nfscberr_null,
174         nfscberr_null,
175         nfscberr_getattr,
176         nfscberr_recall
177 };
178
179 #define NETFAMILY(clp) \
180                 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
181
182 /*
183  * Called for an open operation.
184  * If the nfhp argument is NULL, just get an openowner.
185  */
186 APPLESTATIC int
187 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
188     struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
189     struct nfsclopen **opp, int *newonep, int *retp, int lockit)
190 {
191         struct nfsclclient *clp;
192         struct nfsclowner *owp, *nowp;
193         struct nfsclopen *op = NULL, *nop = NULL;
194         struct nfscldeleg *dp;
195         struct nfsclownerhead *ohp;
196         u_int8_t own[NFSV4CL_LOCKNAMELEN];
197         int ret;
198
199         if (newonep != NULL)
200                 *newonep = 0;
201         if (opp != NULL)
202                 *opp = NULL;
203         if (owpp != NULL)
204                 *owpp = NULL;
205
206         /*
207          * Might need one or both of these, so MALLOC them now, to
208          * avoid a tsleep() in MALLOC later.
209          */
210         MALLOC(nowp, struct nfsclowner *, sizeof (struct nfsclowner),
211             M_NFSCLOWNER, M_WAITOK);
212         if (nfhp != NULL)
213             MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
214                 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
215         ret = nfscl_getcl(vp, cred, p, &clp);
216         if (ret != 0) {
217                 FREE((caddr_t)nowp, M_NFSCLOWNER);
218                 if (nop != NULL)
219                         FREE((caddr_t)nop, M_NFSCLOPEN);
220                 return (ret);
221         }
222
223         /*
224          * Get the Open iff it already exists.
225          * If none found, add the new one or return error, depending upon
226          * "create".
227          */
228         nfscl_filllockowner(p->td_proc, own, F_POSIX);
229         NFSLOCKCLSTATE();
230         dp = NULL;
231         /* First check the delegation list */
232         if (nfhp != NULL && usedeleg) {
233                 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
234                         if (dp->nfsdl_fhlen == fhlen &&
235                             !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
236                                 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
237                                     (dp->nfsdl_flags & NFSCLDL_WRITE))
238                                         break;
239                                 dp = NULL;
240                                 break;
241                         }
242                 }
243         }
244
245         if (dp != NULL)
246                 ohp = &dp->nfsdl_owner;
247         else
248                 ohp = &clp->nfsc_owner;
249         /* Now, search for an openowner */
250         LIST_FOREACH(owp, ohp, nfsow_list) {
251                 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
252                         break;
253         }
254
255         /*
256          * Create a new open, as required.
257          */
258         nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
259             newonep);
260
261         /*
262          * Serialize modifications to the open owner for multiple threads
263          * within the same process using a read/write sleep lock.
264          */
265         if (lockit)
266                 nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
267         NFSUNLOCKCLSTATE();
268         if (nowp != NULL)
269                 FREE((caddr_t)nowp, M_NFSCLOWNER);
270         if (nop != NULL)
271                 FREE((caddr_t)nop, M_NFSCLOPEN);
272         if (owpp != NULL)
273                 *owpp = owp;
274         if (opp != NULL)
275                 *opp = op;
276         if (retp != NULL) {
277                 if (nfhp != NULL && dp != NULL && nop == NULL)
278                         /* new local open on delegation */
279                         *retp = NFSCLOPEN_SETCRED;
280                 else
281                         *retp = NFSCLOPEN_OK;
282         }
283
284         /*
285          * Now, check the mode on the open and return the appropriate
286          * value.
287          */
288         if (op != NULL && (amode & ~(op->nfso_mode))) {
289                 op->nfso_mode |= amode;
290                 if (retp != NULL && dp == NULL)
291                         *retp = NFSCLOPEN_DOOPEN;
292         }
293         return (0);
294 }
295
296 /*
297  * Create a new open, as required.
298  */
299 static void
300 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
301     struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
302     struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
303     int *newonep)
304 {
305         struct nfsclowner *owp = *owpp, *nowp;
306         struct nfsclopen *op, *nop;
307
308         if (nowpp != NULL)
309                 nowp = *nowpp;
310         else
311                 nowp = NULL;
312         if (nopp != NULL)
313                 nop = *nopp;
314         else
315                 nop = NULL;
316         if (owp == NULL && nowp != NULL) {
317                 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
318                 LIST_INIT(&nowp->nfsow_open);
319                 nowp->nfsow_clp = clp;
320                 nowp->nfsow_seqid = 0;
321                 nowp->nfsow_defunct = 0;
322                 nfscl_lockinit(&nowp->nfsow_rwlock);
323                 if (dp != NULL) {
324                         newnfsstats.cllocalopenowners++;
325                         LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
326                 } else {
327                         newnfsstats.clopenowners++;
328                         LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
329                 }
330                 owp = *owpp = nowp;
331                 *nowpp = NULL;
332                 if (newonep != NULL)
333                         *newonep = 1;
334         }
335
336          /* If an fhp has been specified, create an Open as well. */
337         if (fhp != NULL) {
338                 /* and look for the correct open, based upon FH */
339                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
340                         if (op->nfso_fhlen == fhlen &&
341                             !NFSBCMP(op->nfso_fh, fhp, fhlen))
342                                 break;
343                 }
344                 if (op == NULL && nop != NULL) {
345                         nop->nfso_own = owp;
346                         nop->nfso_mode = 0;
347                         nop->nfso_opencnt = 0;
348                         nop->nfso_posixlock = 1;
349                         nop->nfso_fhlen = fhlen;
350                         NFSBCOPY(fhp, nop->nfso_fh, fhlen);
351                         LIST_INIT(&nop->nfso_lock);
352                         nop->nfso_stateid.seqid = 0;
353                         nop->nfso_stateid.other[0] = 0;
354                         nop->nfso_stateid.other[1] = 0;
355                         nop->nfso_stateid.other[2] = 0;
356                         if (dp != NULL) {
357                                 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
358                                 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
359                                     nfsdl_list);
360                                 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
361                                 newnfsstats.cllocalopens++;
362                         } else {
363                                 newnfsstats.clopens++;
364                         }
365                         LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
366                         *opp = nop;
367                         *nopp = NULL;
368                         if (newonep != NULL)
369                                 *newonep = 1;
370                 } else {
371                         *opp = op;
372                 }
373         }
374 }
375
376 /*
377  * Called to find/add a delegation to a client.
378  */
379 APPLESTATIC int
380 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
381     int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
382 {
383         struct nfscldeleg *dp = *dpp, *tdp;
384
385         /*
386          * First, if we have received a Read delegation for a file on a
387          * read/write file system, just return it, because they aren't
388          * useful, imho.
389          */
390         if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
391             (dp->nfsdl_flags & NFSCLDL_READ)) {
392                 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
393                 FREE((caddr_t)dp, M_NFSCLDELEG);
394                 *dpp = NULL;
395                 return (0);
396         }
397
398         /* Look for the correct deleg, based upon FH */
399         NFSLOCKCLSTATE();
400         tdp = nfscl_finddeleg(clp, nfhp, fhlen);
401         if (tdp == NULL) {
402                 if (dp == NULL) {
403                         NFSUNLOCKCLSTATE();
404                         return (NFSERR_BADSTATEID);
405                 }
406                 *dpp = NULL;
407                 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
408                 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
409                     nfsdl_hash);
410                 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
411                 newnfsstats.cldelegates++;
412                 nfscl_delegcnt++;
413         } else {
414                 /*
415                  * Delegation already exists, what do we do if a new one??
416                  */
417                 if (dp != NULL) {
418                         printf("Deleg already exists!\n");
419                         FREE((caddr_t)dp, M_NFSCLDELEG);
420                         *dpp = NULL;
421                 } else {
422                         *dpp = tdp;
423                 }
424         }
425         NFSUNLOCKCLSTATE();
426         return (0);
427 }
428
429 /*
430  * Find a delegation for this file handle. Return NULL upon failure.
431  */
432 static struct nfscldeleg *
433 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
434 {
435         struct nfscldeleg *dp;
436
437         LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
438             if (dp->nfsdl_fhlen == fhlen &&
439                 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
440                 break;
441         }
442         return (dp);
443 }
444
445 /*
446  * Get a stateid for an I/O operation. First, look for an open and iff
447  * found, return either a lockowner stateid or the open stateid.
448  * If no Open is found, just return error and the special stateid of all zeros.
449  */
450 APPLESTATIC int
451 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
452     struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
453     void **lckpp)
454 {
455         struct nfsclclient *clp;
456         struct nfsclowner *owp;
457         struct nfsclopen *op = NULL;
458         struct nfscllockowner *lp;
459         struct nfscldeleg *dp;
460         struct nfsnode *np;
461         u_int8_t own[NFSV4CL_LOCKNAMELEN];
462         int error, done;
463
464         *lckpp = NULL;
465         /*
466          * Initially, just set the special stateid of all zeros.
467          */
468         stateidp->seqid = 0;
469         stateidp->other[0] = 0;
470         stateidp->other[1] = 0;
471         stateidp->other[2] = 0;
472         if (vnode_vtype(vp) != VREG)
473                 return (EISDIR);
474         np = VTONFS(vp);
475         NFSLOCKCLSTATE();
476         clp = nfscl_findcl(VFSTONFS(vnode_mount(vp)));
477         if (clp == NULL) {
478                 NFSUNLOCKCLSTATE();
479                 return (EACCES);
480         }
481
482         /*
483          * Wait for recovery to complete.
484          */
485         while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
486                 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
487                     PZERO, "nfsrecvr", NULL);
488
489         /*
490          * First, look for a delegation.
491          */
492         LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
493                 if (dp->nfsdl_fhlen == fhlen &&
494                     !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
495                         if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
496                             (dp->nfsdl_flags & NFSCLDL_WRITE)) {
497                                 stateidp->seqid = dp->nfsdl_stateid.seqid;
498                                 stateidp->other[0] = dp->nfsdl_stateid.other[0];
499                                 stateidp->other[1] = dp->nfsdl_stateid.other[1];
500                                 stateidp->other[2] = dp->nfsdl_stateid.other[2];
501                                 if (!(np->n_flag & NDELEGRECALL)) {
502                                         TAILQ_REMOVE(&clp->nfsc_deleg, dp,
503                                             nfsdl_list);
504                                         TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
505                                             nfsdl_list);
506                                         dp->nfsdl_timestamp = NFSD_MONOSEC +
507                                             120;
508                                         dp->nfsdl_rwlock.nfslock_usecnt++;
509                                         *lckpp = (void *)&dp->nfsdl_rwlock;
510                                 }
511                                 NFSUNLOCKCLSTATE();
512                                 return (0);
513                         }
514                         break;
515                 }
516         }
517
518         if (p != NULL) {
519                 /*
520                  * If p != NULL, we want to search the parentage tree
521                  * for a matching OpenOwner and use that.
522                  */
523                 nfscl_filllockowner(p->td_proc, own, F_POSIX);
524                 lp = NULL;
525                 error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, own, own,
526                     mode, &lp, &op);
527                 if (error == 0 && lp != NULL) {
528                         stateidp->seqid =
529                             lp->nfsl_stateid.seqid;
530                         stateidp->other[0] =
531                             lp->nfsl_stateid.other[0];
532                         stateidp->other[1] =
533                             lp->nfsl_stateid.other[1];
534                         stateidp->other[2] =
535                             lp->nfsl_stateid.other[2];
536                         NFSUNLOCKCLSTATE();
537                         return (0);
538                 }
539         }
540         if (op == NULL) {
541                 /* If not found, just look for any OpenOwner that will work. */
542                 done = 0;
543                 owp = LIST_FIRST(&clp->nfsc_owner);
544                 while (!done && owp != NULL) {
545                         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
546                                 if (op->nfso_fhlen == fhlen &&
547                                     !NFSBCMP(op->nfso_fh, nfhp, fhlen) &&
548                                     (mode & op->nfso_mode) == mode) {
549                                         done = 1;
550                                         break;
551                                 }
552                         }
553                         if (!done)
554                                 owp = LIST_NEXT(owp, nfsow_list);
555                 }
556                 if (!done) {
557                         NFSUNLOCKCLSTATE();
558                         return (ENOENT);
559                 }
560                 /* for read aheads or write behinds, use the open cred */
561                 newnfs_copycred(&op->nfso_cred, cred);
562         }
563
564         /*
565          * No lock stateid, so return the open stateid.
566          */
567         stateidp->seqid = op->nfso_stateid.seqid;
568         stateidp->other[0] = op->nfso_stateid.other[0];
569         stateidp->other[1] = op->nfso_stateid.other[1];
570         stateidp->other[2] = op->nfso_stateid.other[2];
571         NFSUNLOCKCLSTATE();
572         return (0);
573 }
574
575 /*
576  * Search for a matching file, mode and, optionally, lockowner.
577  */
578 static int
579 nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
580     u_int8_t *openown, u_int8_t *lockown, u_int32_t mode,
581     struct nfscllockowner **lpp, struct nfsclopen **opp)
582 {
583         struct nfsclowner *owp;
584         struct nfsclopen *op, *rop, *rop2;
585         struct nfscllockowner *lp;
586         int keep_looping;
587
588         if (lpp != NULL)
589                 *lpp = NULL;
590         /*
591          * rop will be set to the open to be returned. There are three
592          * variants of this, all for an open of the correct file:
593          * 1 - A match of lockown.
594          * 2 - A match of the openown, when no lockown match exists.
595          * 3 - A match for any open, if no openown or lockown match exists.
596          * Looking for #2 over #3 probably isn't necessary, but since
597          * RFC3530 is vague w.r.t. the relationship between openowners and
598          * lockowners, I think this is the safer way to go.
599          */
600         rop = NULL;
601         rop2 = NULL;
602         keep_looping = 1;
603         /* Search the client list */
604         owp = LIST_FIRST(ohp);
605         while (owp != NULL && keep_looping != 0) {
606                 /* and look for the correct open */
607                 op = LIST_FIRST(&owp->nfsow_open);
608                 while (op != NULL && keep_looping != 0) {
609                         if (op->nfso_fhlen == fhlen &&
610                             !NFSBCMP(op->nfso_fh, nfhp, fhlen)
611                             && (op->nfso_mode & mode) == mode) {
612                                 if (lpp != NULL) {
613                                         /* Now look for a matching lockowner. */
614                                         LIST_FOREACH(lp, &op->nfso_lock,
615                                             nfsl_list) {
616                                                 if (!NFSBCMP(lp->nfsl_owner,
617                                                     lockown,
618                                                     NFSV4CL_LOCKNAMELEN)) {
619                                                         *lpp = lp;
620                                                         rop = op;
621                                                         keep_looping = 0;
622                                                         break;
623                                                 }
624                                         }
625                                 }
626                                 if (rop == NULL && !NFSBCMP(owp->nfsow_owner,
627                                     openown, NFSV4CL_LOCKNAMELEN)) {
628                                         rop = op;
629                                         if (lpp == NULL)
630                                                 keep_looping = 0;
631                                 }
632                                 if (rop2 == NULL)
633                                         rop2 = op;
634                         }
635                         op = LIST_NEXT(op, nfso_list);
636                 }
637                 owp = LIST_NEXT(owp, nfsow_list);
638         }
639         if (rop == NULL)
640                 rop = rop2;
641         if (rop == NULL)
642                 return (EBADF);
643         *opp = rop;
644         return (0);
645 }
646
647 /*
648  * Release use of an open owner. Called when open operations are done
649  * with the open owner.
650  */
651 APPLESTATIC void
652 nfscl_ownerrelease(struct nfsclowner *owp, __unused int error,
653     __unused int candelete, int unlocked)
654 {
655
656         if (owp == NULL)
657                 return;
658         NFSLOCKCLSTATE();
659         if (!unlocked)
660                 nfscl_lockunlock(&owp->nfsow_rwlock);
661         nfscl_clrelease(owp->nfsow_clp);
662         NFSUNLOCKCLSTATE();
663 }
664
665 /*
666  * Release use of an open structure under an open owner.
667  */
668 APPLESTATIC void
669 nfscl_openrelease(struct nfsclopen *op, int error, int candelete)
670 {
671         struct nfsclclient *clp;
672         struct nfsclowner *owp;
673
674         if (op == NULL)
675                 return;
676         NFSLOCKCLSTATE();
677         owp = op->nfso_own;
678         nfscl_lockunlock(&owp->nfsow_rwlock);
679         clp = owp->nfsow_clp;
680         if (error && candelete && op->nfso_opencnt == 0)
681                 nfscl_freeopen(op, 0);
682         nfscl_clrelease(clp);
683         NFSUNLOCKCLSTATE();
684 }
685
686 /*
687  * Called to get a clientid structure. It will optionally lock the
688  * client data structures to do the SetClientId/SetClientId_confirm,
689  * but will release that lock and return the clientid with a refernce
690  * count on it.
691  * If the "cred" argument is NULL, a new clientid should not be created.
692  * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
693  * be done.
694  * It always clpp with a reference count on it, unless returning an error.
695  */
696 APPLESTATIC int
697 nfscl_getcl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
698     struct nfsclclient **clpp)
699 {
700         struct nfsclclient *clp;
701         struct nfsclclient *newclp = NULL;
702         struct mount *mp;
703         struct nfsmount *nmp;
704         char uuid[HOSTUUIDLEN];
705         int igotlock = 0, error, trystalecnt, clidinusedelay, i;
706         u_int16_t idlen = 0;
707
708         mp = vnode_mount(vp);
709         nmp = VFSTONFS(mp);
710         if (cred != NULL) {
711                 getcredhostuuid(cred, uuid, sizeof uuid);
712                 idlen = strlen(uuid);
713                 if (idlen > 0)
714                         idlen += sizeof (u_int64_t);
715                 else
716                         idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
717                 MALLOC(newclp, struct nfsclclient *,
718                     sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
719                     M_WAITOK);
720         }
721         NFSLOCKCLSTATE();
722         /*
723          * If a forced dismount is already in progress, don't
724          * allocate a new clientid and get out now. For the case where
725          * clp != NULL, this is a harmless optimization.
726          */
727         if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
728                 NFSUNLOCKCLSTATE();
729                 if (newclp != NULL)
730                         free(newclp, M_NFSCLCLIENT);
731                 return (EBADF);
732         }
733         clp = nmp->nm_clp;
734         if (clp == NULL) {
735                 if (newclp == NULL) {
736                         NFSUNLOCKCLSTATE();
737                         return (EACCES);
738                 }
739                 clp = newclp;
740                 NFSBZERO((caddr_t)clp, sizeof(struct nfsclclient) + idlen - 1);
741                 clp->nfsc_idlen = idlen;
742                 LIST_INIT(&clp->nfsc_owner);
743                 TAILQ_INIT(&clp->nfsc_deleg);
744                 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
745                         LIST_INIT(&clp->nfsc_deleghash[i]);
746                 clp->nfsc_flags = NFSCLFLAGS_INITED;
747                 clp->nfsc_clientidrev = 1;
748                 clp->nfsc_cbident = nfscl_nextcbident();
749                 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
750                     clp->nfsc_idlen);
751                 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
752                 nmp->nm_clp = clp;
753                 clp->nfsc_nmp = nmp;
754                 NFSUNLOCKCLSTATE();
755                 nfscl_start_renewthread(clp);
756         } else {
757                 NFSUNLOCKCLSTATE();
758                 if (newclp != NULL)
759                         FREE((caddr_t)newclp, M_NFSCLCLIENT);
760         }
761         NFSLOCKCLSTATE();
762         while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
763             (mp->mnt_kern_flag & MNTK_UNMOUNTF) == 0)
764                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
765                     NFSCLSTATEMUTEXPTR, mp);
766         if (!igotlock)
767                 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
768         if (igotlock == 0 && (mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
769                 /*
770                  * Both nfsv4_lock() and nfsv4_getref() know to check
771                  * for MNTK_UNMOUNTF and return without sleeping to
772                  * wait for the exclusive lock to be released, since it
773                  * might be held by nfscl_umount() and we need to get out
774                  * now for that case and not wait until nfscl_umount()
775                  * releases it.
776                  */
777                 NFSUNLOCKCLSTATE();
778                 return (EBADF);
779         }
780         NFSUNLOCKCLSTATE();
781
782         /*
783          * If it needs a clientid, do the setclientid now.
784          */
785         if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
786                 if (!igotlock)
787                         panic("nfscl_clget");
788                 if (p == NULL || cred == NULL) {
789                         NFSLOCKCLSTATE();
790                         nfsv4_unlock(&clp->nfsc_lock, 0);
791                         NFSUNLOCKCLSTATE();
792                         return (EACCES);
793                 }
794                 /*
795                  * If RFC3530 Sec. 14.2.33 is taken literally,
796                  * NFSERR_CLIDINUSE will be returned persistently for the
797                  * case where a new mount of the same file system is using
798                  * a different principal. In practice, NFSERR_CLIDINUSE is
799                  * only returned when there is outstanding unexpired state
800                  * on the clientid. As such, try for twice the lease
801                  * interval, if we know what that is. Otherwise, make a
802                  * wild ass guess.
803                  * The case of returning NFSERR_STALECLIENTID is far less
804                  * likely, but might occur if there is a significant delay
805                  * between doing the SetClientID and SetClientIDConfirm Ops,
806                  * such that the server throws away the clientid before
807                  * receiving the SetClientIDConfirm.
808                  */
809                 if (clp->nfsc_renew > 0)
810                         clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
811                 else
812                         clidinusedelay = 120;
813                 trystalecnt = 3;
814                 do {
815                         error = nfsrpc_setclient(VFSTONFS(vnode_mount(vp)),
816                             clp, cred, p);
817                         if (error == NFSERR_STALECLIENTID ||
818                             error == NFSERR_STALEDONTRECOVER ||
819                             error == NFSERR_CLIDINUSE) {
820                                 (void) nfs_catnap(PZERO, error, "nfs_setcl");
821                         }
822                 } while (((error == NFSERR_STALECLIENTID ||
823                      error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
824                     (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
825                 if (error) {
826                         NFSLOCKCLSTATE();
827                         nfsv4_unlock(&clp->nfsc_lock, 0);
828                         NFSUNLOCKCLSTATE();
829                         return (error);
830                 }
831                 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
832         }
833         if (igotlock) {
834                 NFSLOCKCLSTATE();
835                 nfsv4_unlock(&clp->nfsc_lock, 1);
836                 NFSUNLOCKCLSTATE();
837         }
838
839         *clpp = clp;
840         return (0);
841 }
842
843 /*
844  * Get a reference to a clientid and return it, if valid.
845  */
846 APPLESTATIC struct nfsclclient *
847 nfscl_findcl(struct nfsmount *nmp)
848 {
849         struct nfsclclient *clp;
850
851         clp = nmp->nm_clp;
852         if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
853                 return (NULL);
854         return (clp);
855 }
856
857 /*
858  * Release the clientid structure. It may be locked or reference counted.
859  */
860 static void
861 nfscl_clrelease(struct nfsclclient *clp)
862 {
863
864         if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
865                 nfsv4_unlock(&clp->nfsc_lock, 0);
866         else
867                 nfsv4_relref(&clp->nfsc_lock);
868 }
869
870 /*
871  * External call for nfscl_clrelease.
872  */
873 APPLESTATIC void
874 nfscl_clientrelease(struct nfsclclient *clp)
875 {
876
877         NFSLOCKCLSTATE();
878         if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
879                 nfsv4_unlock(&clp->nfsc_lock, 0);
880         else
881                 nfsv4_relref(&clp->nfsc_lock);
882         NFSUNLOCKCLSTATE();
883 }
884
885 /*
886  * Called when wanting to lock a byte region.
887  */
888 APPLESTATIC int
889 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
890     short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
891     int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
892     struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
893 {
894         struct nfscllockowner *lp;
895         struct nfsclopen *op;
896         struct nfsclclient *clp;
897         struct nfscllockowner *nlp;
898         struct nfscllock *nlop, *otherlop;
899         struct nfscldeleg *dp = NULL, *ldp = NULL;
900         struct nfscllockownerhead *lhp = NULL;
901         struct nfsnode *np;
902         u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
903         u_int8_t *openownp;
904         int error = 0, ret, donelocally = 0;
905         u_int32_t mode;
906
907         /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
908         mode = 0;
909         np = VTONFS(vp);
910         *lpp = NULL;
911         lp = NULL;
912         *newonep = 0;
913         *donelocallyp = 0;
914
915         /*
916          * Might need these, so MALLOC them now, to
917          * avoid a tsleep() in MALLOC later.
918          */
919         MALLOC(nlp, struct nfscllockowner *,
920             sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
921         MALLOC(otherlop, struct nfscllock *,
922             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
923         MALLOC(nlop, struct nfscllock *,
924             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
925         nlop->nfslo_type = type;
926         nlop->nfslo_first = off;
927         if (len == NFS64BITSSET) {
928                 nlop->nfslo_end = NFS64BITSSET;
929         } else {
930                 nlop->nfslo_end = off + len;
931                 if (nlop->nfslo_end <= nlop->nfslo_first)
932                         error = NFSERR_INVAL;
933         }
934
935         if (!error) {
936                 if (recovery)
937                         clp = rclp;
938                 else
939                         error = nfscl_getcl(vp, cred, p, &clp);
940         }
941         if (error) {
942                 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
943                 FREE((caddr_t)otherlop, M_NFSCLLOCK);
944                 FREE((caddr_t)nlop, M_NFSCLLOCK);
945                 return (error);
946         }
947
948         op = NULL;
949         if (recovery) {
950                 ownp = rownp;
951                 openownp = ropenownp;
952         } else {
953                 nfscl_filllockowner(id, own, flags);
954                 ownp = own;
955                 nfscl_filllockowner(p->td_proc, openown, F_POSIX);
956                 openownp = openown;
957         }
958         if (!recovery) {
959                 NFSLOCKCLSTATE();
960                 /*
961                  * First, search for a delegation. If one exists for this file,
962                  * the lock can be done locally against it, so long as there
963                  * isn't a local lock conflict.
964                  */
965                 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
966                     np->n_fhp->nfh_len);
967                 /* Just sanity check for correct type of delegation */
968                 if (dp != NULL && ((dp->nfsdl_flags &
969                     (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
970                      (type == F_WRLCK &&
971                       (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
972                         dp = NULL;
973         }
974         if (dp != NULL) {
975                 /* Now, find an open and maybe a lockowner. */
976                 ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
977                     np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
978                 if (ret)
979                         ret = nfscl_getopen(&clp->nfsc_owner,
980                             np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
981                             ownp, mode, NULL, &op);
982                 if (!ret) {
983                         lhp = &dp->nfsdl_lock;
984                         TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
985                         TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
986                         dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
987                         donelocally = 1;
988                 } else {
989                         dp = NULL;
990                 }
991         }
992         if (!donelocally) {
993                 /*
994                  * Get the related Open and maybe lockowner.
995                  */
996                 error = nfscl_getopen(&clp->nfsc_owner,
997                     np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
998                     ownp, mode, &lp, &op);
999                 if (!error)
1000                         lhp = &op->nfso_lock;
1001         }
1002         if (!error && !recovery)
1003                 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1004                     np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1005         if (error) {
1006                 if (!recovery) {
1007                         nfscl_clrelease(clp);
1008                         NFSUNLOCKCLSTATE();
1009                 }
1010                 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1011                 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1012                 FREE((caddr_t)nlop, M_NFSCLLOCK);
1013                 return (error);
1014         }
1015
1016         /*
1017          * Ok, see if a lockowner exists and create one, as required.
1018          */
1019         if (lp == NULL)
1020                 LIST_FOREACH(lp, lhp, nfsl_list) {
1021                         if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1022                                 break;
1023                 }
1024         if (lp == NULL) {
1025                 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1026                 if (recovery)
1027                         NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1028                             NFSV4CL_LOCKNAMELEN);
1029                 else
1030                         NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1031                             NFSV4CL_LOCKNAMELEN);
1032                 nlp->nfsl_seqid = 0;
1033                 nlp->nfsl_defunct = 0;
1034                 nlp->nfsl_inprog = NULL;
1035                 nfscl_lockinit(&nlp->nfsl_rwlock);
1036                 LIST_INIT(&nlp->nfsl_lock);
1037                 if (donelocally) {
1038                         nlp->nfsl_open = NULL;
1039                         newnfsstats.cllocallockowners++;
1040                 } else {
1041                         nlp->nfsl_open = op;
1042                         newnfsstats.cllockowners++;
1043                 }
1044                 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1045                 lp = nlp;
1046                 nlp = NULL;
1047                 *newonep = 1;
1048         }
1049
1050         /*
1051          * Now, update the byte ranges for locks.
1052          */
1053         ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1054         if (!ret)
1055                 donelocally = 1;
1056         if (donelocally) {
1057                 *donelocallyp = 1;
1058                 if (!recovery)
1059                         nfscl_clrelease(clp);
1060         } else {
1061                 /*
1062                  * Serial modifications on the lock owner for multiple threads
1063                  * for the same process using a read/write lock.
1064                  */
1065                 if (!recovery)
1066                         nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1067         }
1068         if (!recovery)
1069                 NFSUNLOCKCLSTATE();
1070
1071         if (nlp)
1072                 FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
1073         if (nlop)
1074                 FREE((caddr_t)nlop, M_NFSCLLOCK);
1075         if (otherlop)
1076                 FREE((caddr_t)otherlop, M_NFSCLLOCK);
1077
1078         *lpp = lp;
1079         return (0);
1080 }
1081
1082 /*
1083  * Called to unlock a byte range, for LockU.
1084  */
1085 APPLESTATIC int
1086 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1087     __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1088     struct nfsclclient *clp, void *id, int flags,
1089     struct nfscllockowner **lpp, int *dorpcp)
1090 {
1091         struct nfscllockowner *lp;
1092         struct nfsclowner *owp;
1093         struct nfsclopen *op;
1094         struct nfscllock *nlop, *other_lop = NULL;
1095         struct nfscldeleg *dp;
1096         struct nfsnode *np;
1097         u_int8_t own[NFSV4CL_LOCKNAMELEN];
1098         int ret = 0, fnd;
1099
1100         np = VTONFS(vp);
1101         *lpp = NULL;
1102         *dorpcp = 0;
1103
1104         /*
1105          * Might need these, so MALLOC them now, to
1106          * avoid a tsleep() in MALLOC later.
1107          */
1108         MALLOC(nlop, struct nfscllock *,
1109             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1110         nlop->nfslo_type = F_UNLCK;
1111         nlop->nfslo_first = off;
1112         if (len == NFS64BITSSET) {
1113                 nlop->nfslo_end = NFS64BITSSET;
1114         } else {
1115                 nlop->nfslo_end = off + len;
1116                 if (nlop->nfslo_end <= nlop->nfslo_first) {
1117                         FREE((caddr_t)nlop, M_NFSCLLOCK);
1118                         return (NFSERR_INVAL);
1119                 }
1120         }
1121         if (callcnt == 0) {
1122                 MALLOC(other_lop, struct nfscllock *,
1123                     sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1124                 *other_lop = *nlop;
1125         }
1126         nfscl_filllockowner(id, own, flags);
1127         dp = NULL;
1128         NFSLOCKCLSTATE();
1129         if (callcnt == 0)
1130                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1131                     np->n_fhp->nfh_len);
1132
1133         /*
1134          * First, unlock any local regions on a delegation.
1135          */
1136         if (dp != NULL) {
1137                 /* Look for this lockowner. */
1138                 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1139                         if (!NFSBCMP(lp->nfsl_owner, own,
1140                             NFSV4CL_LOCKNAMELEN))
1141                                 break;
1142                 }
1143                 if (lp != NULL)
1144                         /* Use other_lop, so nlop is still available */
1145                         (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1146         }
1147
1148         /*
1149          * Now, find a matching open/lockowner that hasn't already been done,
1150          * as marked by nfsl_inprog.
1151          */
1152         lp = NULL;
1153         fnd = 0;
1154         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1155             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1156                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1157                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1158                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1159                         if (lp->nfsl_inprog == NULL &&
1160                             !NFSBCMP(lp->nfsl_owner, own,
1161                              NFSV4CL_LOCKNAMELEN)) {
1162                                 fnd = 1;
1163                                 break;
1164                         }
1165                     }
1166                     if (fnd)
1167                         break;
1168                 }
1169             }
1170             if (fnd)
1171                 break;
1172         }
1173
1174         if (lp != NULL) {
1175                 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1176                 if (ret)
1177                         *dorpcp = 1;
1178                 /*
1179                  * Serial modifications on the lock owner for multiple
1180                  * threads for the same process using a read/write lock.
1181                  */
1182                 lp->nfsl_inprog = p;
1183                 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1184                 *lpp = lp;
1185         }
1186         NFSUNLOCKCLSTATE();
1187         if (nlop)
1188                 FREE((caddr_t)nlop, M_NFSCLLOCK);
1189         if (other_lop)
1190                 FREE((caddr_t)other_lop, M_NFSCLLOCK);
1191         return (0);
1192 }
1193
1194 /*
1195  * Release all lockowners marked in progess for this process and file.
1196  */
1197 APPLESTATIC void
1198 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1199     void *id, int flags)
1200 {
1201         struct nfsclowner *owp;
1202         struct nfsclopen *op;
1203         struct nfscllockowner *lp;
1204         struct nfsnode *np;
1205         u_int8_t own[NFSV4CL_LOCKNAMELEN];
1206
1207         np = VTONFS(vp);
1208         nfscl_filllockowner(id, own, flags);
1209         NFSLOCKCLSTATE();
1210         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1211             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1212                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1213                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1214                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1215                         if (lp->nfsl_inprog == p &&
1216                             !NFSBCMP(lp->nfsl_owner, own,
1217                             NFSV4CL_LOCKNAMELEN)) {
1218                             lp->nfsl_inprog = NULL;
1219                             nfscl_lockunlock(&lp->nfsl_rwlock);
1220                         }
1221                     }
1222                 }
1223             }
1224         }
1225         nfscl_clrelease(clp);
1226         NFSUNLOCKCLSTATE();
1227 }
1228
1229 /*
1230  * Called to find out if any bytes within the byte range specified are
1231  * write locked by the calling process. Used to determine if flushing
1232  * is required before a LockU.
1233  * If in doubt, return 1, so the flush will occur.
1234  */
1235 APPLESTATIC int
1236 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1237     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1238 {
1239         struct nfsclowner *owp;
1240         struct nfscllockowner *lp;
1241         struct nfsclopen *op;
1242         struct nfsclclient *clp;
1243         struct nfscllock *lop;
1244         struct nfscldeleg *dp;
1245         struct nfsnode *np;
1246         u_int64_t off, end;
1247         u_int8_t own[NFSV4CL_LOCKNAMELEN];
1248         int error = 0;
1249
1250         np = VTONFS(vp);
1251         switch (fl->l_whence) {
1252         case SEEK_SET:
1253         case SEEK_CUR:
1254                 /*
1255                  * Caller is responsible for adding any necessary offset
1256                  * when SEEK_CUR is used.
1257                  */
1258                 off = fl->l_start;
1259                 break;
1260         case SEEK_END:
1261                 off = np->n_size + fl->l_start;
1262                 break;
1263         default:
1264                 return (1);
1265         };
1266         if (fl->l_len != 0) {
1267                 end = off + fl->l_len;
1268                 if (end < off)
1269                         return (1);
1270         } else {
1271                 end = NFS64BITSSET;
1272         }
1273
1274         error = nfscl_getcl(vp, cred, p, &clp);
1275         if (error)
1276                 return (1);
1277         nfscl_filllockowner(id, own, flags);
1278         NFSLOCKCLSTATE();
1279
1280         /*
1281          * First check the delegation locks.
1282          */
1283         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1284         if (dp != NULL) {
1285                 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1286                         if (!NFSBCMP(lp->nfsl_owner, own,
1287                             NFSV4CL_LOCKNAMELEN))
1288                                 break;
1289                 }
1290                 if (lp != NULL) {
1291                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1292                                 if (lop->nfslo_first >= end)
1293                                         break;
1294                                 if (lop->nfslo_end <= off)
1295                                         continue;
1296                                 if (lop->nfslo_type == F_WRLCK) {
1297                                         nfscl_clrelease(clp);
1298                                         NFSUNLOCKCLSTATE();
1299                                         return (1);
1300                                 }
1301                         }
1302                 }
1303         }
1304
1305         /*
1306          * Now, check state against the server.
1307          */
1308         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1309             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1310                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1311                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1312                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1313                         if (!NFSBCMP(lp->nfsl_owner, own,
1314                             NFSV4CL_LOCKNAMELEN))
1315                             break;
1316                     }
1317                     if (lp != NULL) {
1318                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1319                             if (lop->nfslo_first >= end)
1320                                 break;
1321                             if (lop->nfslo_end <= off)
1322                                 continue;
1323                             if (lop->nfslo_type == F_WRLCK) {
1324                                 nfscl_clrelease(clp);
1325                                 NFSUNLOCKCLSTATE();
1326                                 return (1);
1327                             }
1328                         }
1329                     }
1330                 }
1331             }
1332         }
1333         nfscl_clrelease(clp);
1334         NFSUNLOCKCLSTATE();
1335         return (0);
1336 }
1337
1338 /*
1339  * Release a byte range lock owner structure.
1340  */
1341 APPLESTATIC void
1342 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1343 {
1344         struct nfsclclient *clp;
1345
1346         if (lp == NULL)
1347                 return;
1348         NFSLOCKCLSTATE();
1349         clp = lp->nfsl_open->nfso_own->nfsow_clp;
1350         if (error != 0 && candelete &&
1351             (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1352                 nfscl_freelockowner(lp, 0);
1353         else
1354                 nfscl_lockunlock(&lp->nfsl_rwlock);
1355         nfscl_clrelease(clp);
1356         NFSUNLOCKCLSTATE();
1357 }
1358
1359 /*
1360  * Free up an open structure and any associated byte range lock structures.
1361  */
1362 APPLESTATIC void
1363 nfscl_freeopen(struct nfsclopen *op, int local)
1364 {
1365
1366         LIST_REMOVE(op, nfso_list);
1367         nfscl_freealllocks(&op->nfso_lock, local);
1368         FREE((caddr_t)op, M_NFSCLOPEN);
1369         if (local)
1370                 newnfsstats.cllocalopens--;
1371         else
1372                 newnfsstats.clopens--;
1373 }
1374
1375 /*
1376  * Free up all lock owners and associated locks.
1377  */
1378 static void
1379 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1380 {
1381         struct nfscllockowner *lp, *nlp;
1382
1383         LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1384                 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1385                         panic("nfscllckw");
1386                 nfscl_freelockowner(lp, local);
1387         }
1388 }
1389
1390 /*
1391  * Called for an Open when NFSERR_EXPIRED is received from the server.
1392  * If there are no byte range locks nor a Share Deny lost, try to do a
1393  * fresh Open. Otherwise, free the open.
1394  */
1395 static int
1396 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1397     struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1398 {
1399         struct nfscllockowner *lp;
1400         struct nfscldeleg *dp;
1401         int mustdelete = 0, error;
1402
1403         /*
1404          * Look for any byte range lock(s).
1405          */
1406         LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1407                 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1408                         mustdelete = 1;
1409                         break;
1410                 }
1411         }
1412
1413         /*
1414          * If no byte range lock(s) nor a Share deny, try to re-open.
1415          */
1416         if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1417                 newnfs_copycred(&op->nfso_cred, cred);
1418                 dp = NULL;
1419                 error = nfsrpc_reopen(nmp, op->nfso_fh,
1420                     op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1421                 if (error) {
1422                         mustdelete = 1;
1423                         if (dp != NULL) {
1424                                 FREE((caddr_t)dp, M_NFSCLDELEG);
1425                                 dp = NULL;
1426                         }
1427                 }
1428                 if (dp != NULL)
1429                         nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1430                             op->nfso_fhlen, cred, p, &dp);
1431         }
1432
1433         /*
1434          * If a byte range lock or Share deny or couldn't re-open, free it.
1435          */
1436         if (mustdelete)
1437                 nfscl_freeopen(op, 0);
1438         return (mustdelete);
1439 }
1440
1441 /*
1442  * Free up an open owner structure.
1443  */
1444 static void
1445 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1446 {
1447
1448         LIST_REMOVE(owp, nfsow_list);
1449         FREE((caddr_t)owp, M_NFSCLOWNER);
1450         if (local)
1451                 newnfsstats.cllocalopenowners--;
1452         else
1453                 newnfsstats.clopenowners--;
1454 }
1455
1456 /*
1457  * Free up a byte range lock owner structure.
1458  */
1459 APPLESTATIC void
1460 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1461 {
1462         struct nfscllock *lop, *nlop;
1463
1464         LIST_REMOVE(lp, nfsl_list);
1465         LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1466                 nfscl_freelock(lop, local);
1467         }
1468         FREE((caddr_t)lp, M_NFSCLLOCKOWNER);
1469         if (local)
1470                 newnfsstats.cllocallockowners--;
1471         else
1472                 newnfsstats.cllockowners--;
1473 }
1474
1475 /*
1476  * Free up a byte range lock structure.
1477  */
1478 APPLESTATIC void
1479 nfscl_freelock(struct nfscllock *lop, int local)
1480 {
1481
1482         LIST_REMOVE(lop, nfslo_list);
1483         FREE((caddr_t)lop, M_NFSCLLOCK);
1484         if (local)
1485                 newnfsstats.cllocallocks--;
1486         else
1487                 newnfsstats.cllocks--;
1488 }
1489
1490 /*
1491  * Clean out the state related to a delegation.
1492  */
1493 static void
1494 nfscl_cleandeleg(struct nfscldeleg *dp)
1495 {
1496         struct nfsclowner *owp, *nowp;
1497         struct nfsclopen *op;
1498
1499         LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1500                 op = LIST_FIRST(&owp->nfsow_open);
1501                 if (op != NULL) {
1502                         if (LIST_NEXT(op, nfso_list) != NULL)
1503                                 panic("nfscleandel");
1504                         nfscl_freeopen(op, 1);
1505                 }
1506                 nfscl_freeopenowner(owp, 1);
1507         }
1508         nfscl_freealllocks(&dp->nfsdl_lock, 1);
1509 }
1510
1511 /*
1512  * Free a delegation.
1513  */
1514 static void
1515 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp)
1516 {
1517
1518         TAILQ_REMOVE(hdp, dp, nfsdl_list);
1519         LIST_REMOVE(dp, nfsdl_hash);
1520         FREE((caddr_t)dp, M_NFSCLDELEG);
1521         newnfsstats.cldelegates--;
1522         nfscl_delegcnt--;
1523 }
1524
1525 /*
1526  * Free up all state related to this client structure.
1527  */
1528 static void
1529 nfscl_cleanclient(struct nfsclclient *clp)
1530 {
1531         struct nfsclowner *owp, *nowp;
1532         struct nfsclopen *op, *nop;
1533
1534         /* Now, all the OpenOwners, etc. */
1535         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1536                 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1537                         nfscl_freeopen(op, 0);
1538                 }
1539                 nfscl_freeopenowner(owp, 0);
1540         }
1541 }
1542
1543 /*
1544  * Called when an NFSERR_EXPIRED is received from the server.
1545  */
1546 static void
1547 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1548     struct ucred *cred, NFSPROC_T *p)
1549 {
1550         struct nfsclowner *owp, *nowp, *towp;
1551         struct nfsclopen *op, *nop, *top;
1552         struct nfscldeleg *dp, *ndp;
1553         int ret, printed = 0;
1554
1555         /*
1556          * First, merge locally issued Opens into the list for the server.
1557          */
1558         dp = TAILQ_FIRST(&clp->nfsc_deleg);
1559         while (dp != NULL) {
1560             ndp = TAILQ_NEXT(dp, nfsdl_list);
1561             owp = LIST_FIRST(&dp->nfsdl_owner);
1562             while (owp != NULL) {
1563                 nowp = LIST_NEXT(owp, nfsow_list);
1564                 op = LIST_FIRST(&owp->nfsow_open);
1565                 if (op != NULL) {
1566                     if (LIST_NEXT(op, nfso_list) != NULL)
1567                         panic("nfsclexp");
1568                     LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1569                         if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1570                             NFSV4CL_LOCKNAMELEN))
1571                             break;
1572                     }
1573                     if (towp != NULL) {
1574                         /* Merge opens in */
1575                         LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1576                             if (top->nfso_fhlen == op->nfso_fhlen &&
1577                                 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1578                                  op->nfso_fhlen)) {
1579                                 top->nfso_mode |= op->nfso_mode;
1580                                 top->nfso_opencnt += op->nfso_opencnt;
1581                                 break;
1582                             }
1583                         }
1584                         if (top == NULL) {
1585                             /* Just add the open to the owner list */
1586                             LIST_REMOVE(op, nfso_list);
1587                             op->nfso_own = towp;
1588                             LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1589                             newnfsstats.cllocalopens--;
1590                             newnfsstats.clopens++;
1591                         }
1592                     } else {
1593                         /* Just add the openowner to the client list */
1594                         LIST_REMOVE(owp, nfsow_list);
1595                         owp->nfsow_clp = clp;
1596                         LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1597                         newnfsstats.cllocalopenowners--;
1598                         newnfsstats.clopenowners++;
1599                         newnfsstats.cllocalopens--;
1600                         newnfsstats.clopens++;
1601                     }
1602                 }
1603                 owp = nowp;
1604             }
1605             if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1606                 printed = 1;
1607                 printf("nfsv4 expired locks lost\n");
1608             }
1609             nfscl_cleandeleg(dp);
1610             nfscl_freedeleg(&clp->nfsc_deleg, dp);
1611             dp = ndp;
1612         }
1613         if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1614             panic("nfsclexp");
1615
1616         /*
1617          * Now, try and reopen against the server.
1618          */
1619         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1620                 owp->nfsow_seqid = 0;
1621                 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1622                         ret = nfscl_expireopen(clp, op, nmp, cred, p);
1623                         if (ret && !printed) {
1624                                 printed = 1;
1625                                 printf("nfsv4 expired locks lost\n");
1626                         }
1627                 }
1628                 if (LIST_EMPTY(&owp->nfsow_open))
1629                         nfscl_freeopenowner(owp, 0);
1630         }
1631 }
1632
1633 /*
1634  * This function must be called after the process represented by "own" has
1635  * exited. Must be called with CLSTATE lock held.
1636  */
1637 static void
1638 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1639 {
1640         struct nfsclowner *owp, *nowp;
1641         struct nfsclopen *op;
1642         struct nfscllockowner *lp, *nlp;
1643         struct nfscldeleg *dp;
1644
1645         /* First, get rid of local locks on delegations. */
1646         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1647                 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1648                     if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1649                         if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1650                             panic("nfscllckw");
1651                         nfscl_freelockowner(lp, 1);
1652                     }
1653                 }
1654         }
1655         owp = LIST_FIRST(&clp->nfsc_owner);
1656         while (owp != NULL) {
1657                 nowp = LIST_NEXT(owp, nfsow_list);
1658                 if (!NFSBCMP(owp->nfsow_owner, own,
1659                     NFSV4CL_LOCKNAMELEN)) {
1660                         /*
1661                          * If there are children that haven't closed the
1662                          * file descriptors yet, the opens will still be
1663                          * here. For that case, let the renew thread clear
1664                          * out the OpenOwner later.
1665                          */
1666                         if (LIST_EMPTY(&owp->nfsow_open))
1667                                 nfscl_freeopenowner(owp, 0);
1668                         else
1669                                 owp->nfsow_defunct = 1;
1670                 } else {
1671                         /* look for lockowners on other opens */
1672                         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1673                                 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1674                                         if (!NFSBCMP(lp->nfsl_owner, own,
1675                                             NFSV4CL_LOCKNAMELEN))
1676                                                 lp->nfsl_defunct = 1;
1677                                 }
1678                         }
1679                 }
1680                 owp = nowp;
1681         }
1682 }
1683
1684 /*
1685  * Find open/lock owners for processes that have exited.
1686  */
1687 static void
1688 nfscl_cleanupkext(struct nfsclclient *clp)
1689 {
1690         struct nfsclowner *owp, *nowp;
1691
1692         NFSPROCLISTLOCK();
1693         NFSLOCKCLSTATE();
1694         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1695                 if (nfscl_procdoesntexist(owp->nfsow_owner))
1696                         nfscl_cleanup_common(clp, owp->nfsow_owner);
1697         }
1698         NFSUNLOCKCLSTATE();
1699         NFSPROCLISTUNLOCK();
1700 }
1701
1702 static int      fake_global;    /* Used to force visibility of MNTK_UNMOUNTF */
1703 /*
1704  * Called from nfs umount to free up the clientid.
1705  */
1706 APPLESTATIC void
1707 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1708 {
1709         struct nfsclclient *clp;
1710         struct ucred *cred;
1711         int igotlock;
1712
1713         /*
1714          * For the case that matters, this is the thread that set
1715          * MNTK_UNMOUNTF, so it will see it set. The code that follows is
1716          * done to ensure that any thread executing nfscl_getcl() after
1717          * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
1718          * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
1719          * explanation, courtesy of Alan Cox.
1720          * What follows is a snippet from Alan Cox's email at:
1721          * http://docs.FreeBSD.org/cgi/
1722          *     mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
1723          * 
1724          * 1. Set MNTK_UNMOUNTF
1725          * 2. Acquire a standard FreeBSD mutex "m".
1726          * 3. Update some data structures.
1727          * 4. Release mutex "m".
1728          * 
1729          * Then, other threads that acquire "m" after step 4 has occurred will
1730          * see MNTK_UNMOUNTF as set.  But, other threads that beat thread X to
1731          * step 2 may or may not see MNTK_UNMOUNTF as set.
1732          */
1733         NFSLOCKCLSTATE();
1734         if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
1735                 fake_global++;
1736                 NFSUNLOCKCLSTATE();
1737                 NFSLOCKCLSTATE();
1738         }
1739
1740         clp = nmp->nm_clp;
1741         if (clp != NULL) {
1742                 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1743                         panic("nfscl umount");
1744         
1745                 /*
1746                  * First, handshake with the nfscl renew thread, to terminate
1747                  * it.
1748                  */
1749                 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1750                 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1751                         (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
1752                             "nfsclumnt", hz);
1753         
1754                 /*
1755                  * Now, get the exclusive lock on the client state, so
1756                  * that no uses of the state are still in progress.
1757                  */
1758                 do {
1759                         igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1760                             NFSCLSTATEMUTEXPTR, NULL);
1761                 } while (!igotlock);
1762                 NFSUNLOCKCLSTATE();
1763         
1764                 /*
1765                  * Free up all the state. It will expire on the server, but
1766                  * maybe we should do a SetClientId/SetClientIdConfirm so
1767                  * the server throws it away?
1768                  */
1769                 LIST_REMOVE(clp, nfsc_list);
1770                 nfscl_delegreturnall(clp, p);
1771                 cred = newnfs_getcred();
1772                 (void) nfsrpc_setclient(nmp, clp, cred, p);
1773                 nfscl_cleanclient(clp);
1774                 nmp->nm_clp = NULL;
1775                 NFSFREECRED(cred);
1776                 FREE((caddr_t)clp, M_NFSCLCLIENT);
1777         } else
1778                 NFSUNLOCKCLSTATE();
1779 }
1780
1781 /*
1782  * This function is called when a server replies with NFSERR_STALECLIENTID
1783  * or NFSERR_STALESTATEID. It traverses the clientid lists, doing Opens
1784  * and Locks with reclaim. If these fail, it deletes the corresponding state.
1785  */
1786 static void
1787 nfscl_recover(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
1788 {
1789         struct nfsclowner *owp, *nowp;
1790         struct nfsclopen *op, *nop;
1791         struct nfscllockowner *lp, *nlp;
1792         struct nfscllock *lop, *nlop;
1793         struct nfscldeleg *dp, *ndp, *tdp;
1794         struct nfsmount *nmp;
1795         struct ucred *tcred;
1796         struct nfsclopenhead extra_open;
1797         struct nfscldeleghead extra_deleg;
1798         struct nfsreq *rep;
1799         u_int64_t len;
1800         u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
1801         int igotlock = 0, error, trycnt, firstlock, s;
1802
1803         /*
1804          * First, lock the client structure, so everyone else will
1805          * block when trying to use state.
1806          */
1807         NFSLOCKCLSTATE();
1808         clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
1809         do {
1810                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1811                     NFSCLSTATEMUTEXPTR, NULL);
1812         } while (!igotlock);
1813         NFSUNLOCKCLSTATE();
1814
1815         nmp = clp->nfsc_nmp;
1816         if (nmp == NULL)
1817                 panic("nfscl recover");
1818         trycnt = 5;
1819         do {
1820                 error = nfsrpc_setclient(nmp, clp, cred, p);
1821         } while ((error == NFSERR_STALECLIENTID ||
1822              error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
1823         if (error) {
1824                 nfscl_cleanclient(clp);
1825                 NFSLOCKCLSTATE();
1826                 clp->nfsc_flags &= ~(NFSCLFLAGS_HASCLIENTID |
1827                     NFSCLFLAGS_RECOVER | NFSCLFLAGS_RECVRINPROG);
1828                 wakeup(&clp->nfsc_flags);
1829                 nfsv4_unlock(&clp->nfsc_lock, 0);
1830                 NFSUNLOCKCLSTATE();
1831                 return;
1832         }
1833         clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
1834         clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
1835
1836         /*
1837          * Mark requests already queued on the server, so that they don't
1838          * initiate another recovery cycle. Any requests already in the
1839          * queue that handle state information will have the old stale
1840          * clientid/stateid and will get a NFSERR_STALESTATEID or
1841          * NFSERR_STALECLIENTID reply from the server. This will be
1842          * translated to NFSERR_STALEDONTRECOVER when R_DONTRECOVER is set.
1843          */
1844         s = splsoftclock();
1845         NFSLOCKREQ();
1846         TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
1847                 if (rep->r_nmp == nmp)
1848                         rep->r_flags |= R_DONTRECOVER;
1849         }
1850         NFSUNLOCKREQ();
1851         splx(s);
1852
1853         /*
1854          * Now, mark all delegations "need reclaim".
1855          */
1856         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
1857                 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
1858
1859         TAILQ_INIT(&extra_deleg);
1860         LIST_INIT(&extra_open);
1861         /*
1862          * Now traverse the state lists, doing Open and Lock Reclaims.
1863          */
1864         tcred = newnfs_getcred();
1865         owp = LIST_FIRST(&clp->nfsc_owner);
1866         while (owp != NULL) {
1867             nowp = LIST_NEXT(owp, nfsow_list);
1868             owp->nfsow_seqid = 0;
1869             op = LIST_FIRST(&owp->nfsow_open);
1870             while (op != NULL) {
1871                 nop = LIST_NEXT(op, nfso_list);
1872                 if (error != NFSERR_NOGRACE) {
1873                     /* Search for a delegation to reclaim with the open */
1874                     TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1875                         if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
1876                             continue;
1877                         if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
1878                             mode = NFSV4OPEN_ACCESSWRITE;
1879                             delegtype = NFSV4OPEN_DELEGATEWRITE;
1880                         } else {
1881                             mode = NFSV4OPEN_ACCESSREAD;
1882                             delegtype = NFSV4OPEN_DELEGATEREAD;
1883                         }
1884                         if ((op->nfso_mode & mode) == mode &&
1885                             op->nfso_fhlen == dp->nfsdl_fhlen &&
1886                             !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
1887                             break;
1888                     }
1889                     ndp = dp;
1890                     if (dp == NULL)
1891                         delegtype = NFSV4OPEN_DELEGATENONE;
1892                     newnfs_copycred(&op->nfso_cred, tcred);
1893                     error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
1894                         op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
1895                         op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
1896                         tcred, p);
1897                     if (!error) {
1898                         /* Handle any replied delegation */
1899                         if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
1900                             || NFSMNT_RDONLY(nmp->nm_mountp))) {
1901                             if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
1902                                 mode = NFSV4OPEN_ACCESSWRITE;
1903                             else
1904                                 mode = NFSV4OPEN_ACCESSREAD;
1905                             TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1906                                 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
1907                                     continue;
1908                                 if ((op->nfso_mode & mode) == mode &&
1909                                     op->nfso_fhlen == dp->nfsdl_fhlen &&
1910                                     !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
1911                                     op->nfso_fhlen)) {
1912                                     dp->nfsdl_stateid = ndp->nfsdl_stateid;
1913                                     dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
1914                                     dp->nfsdl_ace = ndp->nfsdl_ace;
1915                                     dp->nfsdl_change = ndp->nfsdl_change;
1916                                     dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
1917                                     if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
1918                                         dp->nfsdl_flags |= NFSCLDL_RECALL;
1919                                     FREE((caddr_t)ndp, M_NFSCLDELEG);
1920                                     ndp = NULL;
1921                                     break;
1922                                 }
1923                             }
1924                         }
1925                         if (ndp != NULL)
1926                             TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
1927
1928                         /* and reclaim all byte range locks */
1929                         lp = LIST_FIRST(&op->nfso_lock);
1930                         while (lp != NULL) {
1931                             nlp = LIST_NEXT(lp, nfsl_list);
1932                             lp->nfsl_seqid = 0;
1933                             firstlock = 1;
1934                             lop = LIST_FIRST(&lp->nfsl_lock);
1935                             while (lop != NULL) {
1936                                 nlop = LIST_NEXT(lop, nfslo_list);
1937                                 if (lop->nfslo_end == NFS64BITSSET)
1938                                     len = NFS64BITSSET;
1939                                 else
1940                                     len = lop->nfslo_end - lop->nfslo_first;
1941                                 if (error != NFSERR_NOGRACE)
1942                                     error = nfscl_trylock(nmp, NULL,
1943                                         op->nfso_fh, op->nfso_fhlen, lp,
1944                                         firstlock, 1, lop->nfslo_first, len,
1945                                         lop->nfslo_type, tcred, p);
1946                                 if (error != 0)
1947                                     nfscl_freelock(lop, 0);
1948                                 else
1949                                     firstlock = 0;
1950                                 lop = nlop;
1951                             }
1952                             /* If no locks, but a lockowner, just delete it. */
1953                             if (LIST_EMPTY(&lp->nfsl_lock))
1954                                 nfscl_freelockowner(lp, 0);
1955                             lp = nlp;
1956                         }
1957                     } else {
1958                         nfscl_freeopen(op, 0);
1959                     }
1960                 }
1961                 op = nop;
1962             }
1963             owp = nowp;
1964         }
1965
1966         /*
1967          * Now, try and get any delegations not yet reclaimed by cobbling
1968          * to-gether an appropriate open.
1969          */
1970         nowp = NULL;
1971         dp = TAILQ_FIRST(&clp->nfsc_deleg);
1972         while (dp != NULL) {
1973             ndp = TAILQ_NEXT(dp, nfsdl_list);
1974             if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
1975                 if (nowp == NULL) {
1976                     MALLOC(nowp, struct nfsclowner *,
1977                         sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
1978                     /*
1979                      * Name must be as long an largest possible
1980                      * NFSV4CL_LOCKNAMELEN. 12 for now.
1981                      */
1982                     NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
1983                         NFSV4CL_LOCKNAMELEN);
1984                     LIST_INIT(&nowp->nfsow_open);
1985                     nowp->nfsow_clp = clp;
1986                     nowp->nfsow_seqid = 0;
1987                     nowp->nfsow_defunct = 0;
1988                     nfscl_lockinit(&nowp->nfsow_rwlock);
1989                 }
1990                 nop = NULL;
1991                 if (error != NFSERR_NOGRACE) {
1992                     MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
1993                         dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
1994                     nop->nfso_own = nowp;
1995                     if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
1996                         nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
1997                         delegtype = NFSV4OPEN_DELEGATEWRITE;
1998                     } else {
1999                         nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2000                         delegtype = NFSV4OPEN_DELEGATEREAD;
2001                     }
2002                     nop->nfso_opencnt = 0;
2003                     nop->nfso_posixlock = 1;
2004                     nop->nfso_fhlen = dp->nfsdl_fhlen;
2005                     NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2006                     LIST_INIT(&nop->nfso_lock);
2007                     nop->nfso_stateid.seqid = 0;
2008                     nop->nfso_stateid.other[0] = 0;
2009                     nop->nfso_stateid.other[1] = 0;
2010                     nop->nfso_stateid.other[2] = 0;
2011                     newnfs_copycred(&dp->nfsdl_cred, tcred);
2012                     newnfs_copyincred(tcred, &nop->nfso_cred);
2013                     tdp = NULL;
2014                     error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2015                         nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2016                         nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2017                         delegtype, tcred, p);
2018                     if (tdp != NULL) {
2019                         if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2020                             mode = NFSV4OPEN_ACCESSWRITE;
2021                         else
2022                             mode = NFSV4OPEN_ACCESSREAD;
2023                         if ((nop->nfso_mode & mode) == mode &&
2024                             nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2025                             !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2026                             nop->nfso_fhlen)) {
2027                             dp->nfsdl_stateid = tdp->nfsdl_stateid;
2028                             dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2029                             dp->nfsdl_ace = tdp->nfsdl_ace;
2030                             dp->nfsdl_change = tdp->nfsdl_change;
2031                             dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2032                             if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2033                                 dp->nfsdl_flags |= NFSCLDL_RECALL;
2034                             FREE((caddr_t)tdp, M_NFSCLDELEG);
2035                         } else {
2036                             TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2037                         }
2038                     }
2039                 }
2040                 if (error) {
2041                     if (nop != NULL)
2042                         FREE((caddr_t)nop, M_NFSCLOPEN);
2043                     /*
2044                      * Couldn't reclaim it, so throw the state
2045                      * away. Ouch!!
2046                      */
2047                     nfscl_cleandeleg(dp);
2048                     nfscl_freedeleg(&clp->nfsc_deleg, dp);
2049                 } else {
2050                     LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2051                 }
2052             }
2053             dp = ndp;
2054         }
2055
2056         /*
2057          * Now, get rid of extra Opens and Delegations.
2058          */
2059         LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2060                 do {
2061                         newnfs_copycred(&op->nfso_cred, tcred);
2062                         error = nfscl_tryclose(op, tcred, nmp, p);
2063                         if (error == NFSERR_GRACE)
2064                                 (void) nfs_catnap(PZERO, error, "nfsexcls");
2065                 } while (error == NFSERR_GRACE);
2066                 LIST_REMOVE(op, nfso_list);
2067                 FREE((caddr_t)op, M_NFSCLOPEN);
2068         }
2069         if (nowp != NULL)
2070                 FREE((caddr_t)nowp, M_NFSCLOWNER);
2071
2072         TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2073                 do {
2074                         newnfs_copycred(&dp->nfsdl_cred, tcred);
2075                         error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2076                         if (error == NFSERR_GRACE)
2077                                 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2078                 } while (error == NFSERR_GRACE);
2079                 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2080                 FREE((caddr_t)dp, M_NFSCLDELEG);
2081         }
2082
2083         NFSLOCKCLSTATE();
2084         clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2085         wakeup(&clp->nfsc_flags);
2086         nfsv4_unlock(&clp->nfsc_lock, 0);
2087         NFSUNLOCKCLSTATE();
2088         NFSFREECRED(tcred);
2089 }
2090
2091 /*
2092  * This function is called when a server replies with NFSERR_EXPIRED.
2093  * It deletes all state for the client and does a fresh SetClientId/confirm.
2094  * XXX Someday it should post a signal to the process(es) that hold the
2095  * state, so they know that lock state has been lost.
2096  */
2097 APPLESTATIC int
2098 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2099 {
2100         struct nfsmount *nmp;
2101         struct ucred *cred;
2102         int igotlock = 0, error, trycnt;
2103
2104         /*
2105          * If the clientid has gone away or a new SetClientid has already
2106          * been done, just return ok.
2107          */
2108         if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2109                 return (0);
2110
2111         /*
2112          * First, lock the client structure, so everyone else will
2113          * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2114          * that only one thread does the work.
2115          */
2116         NFSLOCKCLSTATE();
2117         clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2118         do {
2119                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2120                     NFSCLSTATEMUTEXPTR, NULL);
2121         } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2122         if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2123                 if (igotlock)
2124                         nfsv4_unlock(&clp->nfsc_lock, 0);
2125                 NFSUNLOCKCLSTATE();
2126                 return (0);
2127         }
2128         clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2129         NFSUNLOCKCLSTATE();
2130
2131         nmp = clp->nfsc_nmp;
2132         if (nmp == NULL)
2133                 panic("nfscl expired");
2134         cred = newnfs_getcred();
2135         trycnt = 5;
2136         do {
2137                 error = nfsrpc_setclient(nmp, clp, cred, p);
2138         } while ((error == NFSERR_STALECLIENTID ||
2139              error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2140         if (error) {
2141                 /*
2142                  * Clear out any state.
2143                  */
2144                 nfscl_cleanclient(clp);
2145                 NFSLOCKCLSTATE();
2146                 clp->nfsc_flags &= ~(NFSCLFLAGS_HASCLIENTID |
2147                     NFSCLFLAGS_RECOVER);
2148         } else {
2149                 /*
2150                  * Expire the state for the client.
2151                  */
2152                 nfscl_expireclient(clp, nmp, cred, p);
2153                 NFSLOCKCLSTATE();
2154                 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2155                 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2156         }
2157         clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2158         wakeup(&clp->nfsc_flags);
2159         nfsv4_unlock(&clp->nfsc_lock, 0);
2160         NFSUNLOCKCLSTATE();
2161         NFSFREECRED(cred);
2162         return (error);
2163 }
2164
2165 /*
2166  * This function inserts a lock in the list after insert_lop.
2167  */
2168 static void
2169 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2170     struct nfscllock *insert_lop, int local)
2171 {
2172
2173         if ((struct nfscllockowner *)insert_lop == lp)
2174                 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2175         else
2176                 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2177         if (local)
2178                 newnfsstats.cllocallocks++;
2179         else
2180                 newnfsstats.cllocks++;
2181 }
2182
2183 /*
2184  * This function updates the locking for a lock owner and given file. It
2185  * maintains a list of lock ranges ordered on increasing file offset that
2186  * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2187  * It always adds new_lop to the list and sometimes uses the one pointed
2188  * at by other_lopp.
2189  * Returns 1 if the locks were modified, 0 otherwise.
2190  */
2191 static int
2192 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2193     struct nfscllock **other_lopp, int local)
2194 {
2195         struct nfscllock *new_lop = *new_lopp;
2196         struct nfscllock *lop, *tlop, *ilop;
2197         struct nfscllock *other_lop;
2198         int unlock = 0, modified = 0;
2199         u_int64_t tmp;
2200
2201         /*
2202          * Work down the list until the lock is merged.
2203          */
2204         if (new_lop->nfslo_type == F_UNLCK)
2205                 unlock = 1;
2206         ilop = (struct nfscllock *)lp;
2207         lop = LIST_FIRST(&lp->nfsl_lock);
2208         while (lop != NULL) {
2209             /*
2210              * Only check locks for this file that aren't before the start of
2211              * new lock's range.
2212              */
2213             if (lop->nfslo_end >= new_lop->nfslo_first) {
2214                 if (new_lop->nfslo_end < lop->nfslo_first) {
2215                     /*
2216                      * If the new lock ends before the start of the
2217                      * current lock's range, no merge, just insert
2218                      * the new lock.
2219                      */
2220                     break;
2221                 }
2222                 if (new_lop->nfslo_type == lop->nfslo_type ||
2223                     (new_lop->nfslo_first <= lop->nfslo_first &&
2224                      new_lop->nfslo_end >= lop->nfslo_end)) {
2225                     /*
2226                      * This lock can be absorbed by the new lock/unlock.
2227                      * This happens when it covers the entire range
2228                      * of the old lock or is contiguous
2229                      * with the old lock and is of the same type or an
2230                      * unlock.
2231                      */
2232                     if (new_lop->nfslo_type != lop->nfslo_type ||
2233                         new_lop->nfslo_first != lop->nfslo_first ||
2234                         new_lop->nfslo_end != lop->nfslo_end)
2235                         modified = 1;
2236                     if (lop->nfslo_first < new_lop->nfslo_first)
2237                         new_lop->nfslo_first = lop->nfslo_first;
2238                     if (lop->nfslo_end > new_lop->nfslo_end)
2239                         new_lop->nfslo_end = lop->nfslo_end;
2240                     tlop = lop;
2241                     lop = LIST_NEXT(lop, nfslo_list);
2242                     nfscl_freelock(tlop, local);
2243                     continue;
2244                 }
2245
2246                 /*
2247                  * All these cases are for contiguous locks that are not the
2248                  * same type, so they can't be merged.
2249                  */
2250                 if (new_lop->nfslo_first <= lop->nfslo_first) {
2251                     /*
2252                      * This case is where the new lock overlaps with the
2253                      * first part of the old lock. Move the start of the
2254                      * old lock to just past the end of the new lock. The
2255                      * new lock will be inserted in front of the old, since
2256                      * ilop hasn't been updated. (We are done now.)
2257                      */
2258                     if (lop->nfslo_first != new_lop->nfslo_end) {
2259                         lop->nfslo_first = new_lop->nfslo_end;
2260                         modified = 1;
2261                     }
2262                     break;
2263                 }
2264                 if (new_lop->nfslo_end >= lop->nfslo_end) {
2265                     /*
2266                      * This case is where the new lock overlaps with the
2267                      * end of the old lock's range. Move the old lock's
2268                      * end to just before the new lock's first and insert
2269                      * the new lock after the old lock.
2270                      * Might not be done yet, since the new lock could
2271                      * overlap further locks with higher ranges.
2272                      */
2273                     if (lop->nfslo_end != new_lop->nfslo_first) {
2274                         lop->nfslo_end = new_lop->nfslo_first;
2275                         modified = 1;
2276                     }
2277                     ilop = lop;
2278                     lop = LIST_NEXT(lop, nfslo_list);
2279                     continue;
2280                 }
2281                 /*
2282                  * The final case is where the new lock's range is in the
2283                  * middle of the current lock's and splits the current lock
2284                  * up. Use *other_lopp to handle the second part of the
2285                  * split old lock range. (We are done now.)
2286                  * For unlock, we use new_lop as other_lop and tmp, since
2287                  * other_lop and new_lop are the same for this case.
2288                  * We noted the unlock case above, so we don't need
2289                  * new_lop->nfslo_type any longer.
2290                  */
2291                 tmp = new_lop->nfslo_first;
2292                 if (unlock) {
2293                     other_lop = new_lop;
2294                     *new_lopp = NULL;
2295                 } else {
2296                     other_lop = *other_lopp;
2297                     *other_lopp = NULL;
2298                 }
2299                 other_lop->nfslo_first = new_lop->nfslo_end;
2300                 other_lop->nfslo_end = lop->nfslo_end;
2301                 other_lop->nfslo_type = lop->nfslo_type;
2302                 lop->nfslo_end = tmp;
2303                 nfscl_insertlock(lp, other_lop, lop, local);
2304                 ilop = lop;
2305                 modified = 1;
2306                 break;
2307             }
2308             ilop = lop;
2309             lop = LIST_NEXT(lop, nfslo_list);
2310             if (lop == NULL)
2311                 break;
2312         }
2313
2314         /*
2315          * Insert the new lock in the list at the appropriate place.
2316          */
2317         if (!unlock) {
2318                 nfscl_insertlock(lp, new_lop, ilop, local);
2319                 *new_lopp = NULL;
2320                 modified = 1;
2321         }
2322         return (modified);
2323 }
2324
2325 /*
2326  * This function must be run as a kernel thread.
2327  * It does Renew Ops and recovery, when required.
2328  */
2329 APPLESTATIC void
2330 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2331 {
2332         struct nfsclowner *owp, *nowp;
2333         struct nfsclopen *op;
2334         struct nfscllockowner *lp, *nlp, *olp;
2335         struct nfscldeleghead dh;
2336         struct nfscllockownerhead lh;
2337         struct nfscldeleg *dp, *ndp;
2338         struct ucred *cred;
2339         u_int32_t clidrev;
2340         int error, cbpathdown, islept, igotlock, ret, clearok;
2341         uint32_t recover_done_time = 0;
2342         struct timespec mytime;
2343         static time_t prevsec = 0;
2344
2345         cred = newnfs_getcred();
2346         NFSLOCKCLSTATE();
2347         clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2348         NFSUNLOCKCLSTATE();
2349         for(;;) {
2350                 newnfs_setroot(cred);
2351                 cbpathdown = 0;
2352                 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2353                         /*
2354                          * Only allow one recover within 1/2 of the lease
2355                          * duration (nfsc_renew).
2356                          */
2357                         if (recover_done_time < NFSD_MONOSEC) {
2358                                 recover_done_time = NFSD_MONOSEC +
2359                                     clp->nfsc_renew;
2360                                 nfscl_recover(clp, cred, p);
2361                         } else {
2362                                 NFSLOCKCLSTATE();
2363                                 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2364                                 NFSUNLOCKCLSTATE();
2365                         }
2366                 }
2367                 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2368                     (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2369                         clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2370                         clidrev = clp->nfsc_clientidrev;
2371                         error = nfsrpc_renew(clp, cred, p);
2372                         if (error == NFSERR_CBPATHDOWN)
2373                             cbpathdown = 1;
2374                         else if (error == NFSERR_STALECLIENTID) {
2375                             NFSLOCKCLSTATE();
2376                             clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2377                             NFSUNLOCKCLSTATE();
2378                         } else if (error == NFSERR_EXPIRED)
2379                             (void) nfscl_hasexpired(clp, clidrev, p);
2380                 }
2381
2382                 LIST_INIT(&lh);
2383                 TAILQ_INIT(&dh);
2384                 NFSLOCKCLSTATE();
2385                 if (cbpathdown)
2386                         /* It's a Total Recall! */
2387                         nfscl_totalrecall(clp);
2388
2389                 /*
2390                  * Now, handle defunct owners.
2391                  */
2392                 owp = LIST_FIRST(&clp->nfsc_owner);
2393                 while (owp != NULL) {
2394                     nowp = LIST_NEXT(owp, nfsow_list);
2395                     if (LIST_EMPTY(&owp->nfsow_open)) {
2396                         if (owp->nfsow_defunct)
2397                             nfscl_freeopenowner(owp, 0);
2398                     } else {
2399                         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2400                             lp = LIST_FIRST(&op->nfso_lock);
2401                             while (lp != NULL) {
2402                                 nlp = LIST_NEXT(lp, nfsl_list);
2403                                 if (lp->nfsl_defunct &&
2404                                     LIST_EMPTY(&lp->nfsl_lock)) {
2405                                     LIST_FOREACH(olp, &lh, nfsl_list) {
2406                                         if (!NFSBCMP(olp->nfsl_owner,
2407                                             lp->nfsl_owner,NFSV4CL_LOCKNAMELEN))
2408                                             break;
2409                                     }
2410                                     if (olp == NULL) {
2411                                         LIST_REMOVE(lp, nfsl_list);
2412                                         LIST_INSERT_HEAD(&lh, lp, nfsl_list);
2413                                     } else {
2414                                         nfscl_freelockowner(lp, 0);
2415                                     }
2416                                 }
2417                                 lp = nlp;
2418                             }
2419                         }
2420                     }
2421                     owp = nowp;
2422                 }
2423
2424                 /* and release defunct lock owners */
2425                 LIST_FOREACH_SAFE(lp, &lh, nfsl_list, nlp) {
2426                     nfscl_freelockowner(lp, 0);
2427                 }
2428
2429                 /*
2430                  * Do the recall on any delegations. To avoid trouble, always
2431                  * come back up here after having slept.
2432                  */
2433                 igotlock = 0;
2434 tryagain:
2435                 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2436                 while (dp != NULL) {
2437                         ndp = TAILQ_NEXT(dp, nfsdl_list);
2438                         if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2439                                 /*
2440                                  * Wait for outstanding I/O ops to be done.
2441                                  */
2442                                 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2443                                     if (igotlock) {
2444                                         nfsv4_unlock(&clp->nfsc_lock, 0);
2445                                         igotlock = 0;
2446                                     }
2447                                     dp->nfsdl_rwlock.nfslock_lock |=
2448                                         NFSV4LOCK_WANTED;
2449                                     (void) nfsmsleep(&dp->nfsdl_rwlock,
2450                                         NFSCLSTATEMUTEXPTR, PZERO, "nfscld",
2451                                         NULL);
2452                                     goto tryagain;
2453                                 }
2454                                 while (!igotlock) {
2455                                     igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2456                                         &islept, NFSCLSTATEMUTEXPTR, NULL);
2457                                     if (islept)
2458                                         goto tryagain;
2459                                 }
2460                                 NFSUNLOCKCLSTATE();
2461                                 newnfs_copycred(&dp->nfsdl_cred, cred);
2462                                 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2463                                     NULL, cred, p, 1);
2464                                 if (!ret) {
2465                                     nfscl_cleandeleg(dp);
2466                                     TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2467                                         nfsdl_list);
2468                                     LIST_REMOVE(dp, nfsdl_hash);
2469                                     TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2470                                     nfscl_delegcnt--;
2471                                     newnfsstats.cldelegates--;
2472                                 }
2473                                 NFSLOCKCLSTATE();
2474                         }
2475                         dp = ndp;
2476                 }
2477
2478                 /*
2479                  * Clear out old delegations, if we are above the high water
2480                  * mark. Only clear out ones with no state related to them.
2481                  * The tailq list is in LRU order.
2482                  */
2483                 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2484                 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2485                     ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2486                     if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2487                         dp->nfsdl_rwlock.nfslock_lock == 0 &&
2488                         dp->nfsdl_timestamp < NFSD_MONOSEC &&
2489                         (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2490                           NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2491                         clearok = 1;
2492                         LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2493                             op = LIST_FIRST(&owp->nfsow_open);
2494                             if (op != NULL) {
2495                                 clearok = 0;
2496                                 break;
2497                             }
2498                         }
2499                         if (clearok) {
2500                             LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2501                                 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2502                                     clearok = 0;
2503                                     break;
2504                                 }
2505                             }
2506                         }
2507                         if (clearok) {
2508                             TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2509                             LIST_REMOVE(dp, nfsdl_hash);
2510                             TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2511                             nfscl_delegcnt--;
2512                             newnfsstats.cldelegates--;
2513                         }
2514                     }
2515                     dp = ndp;
2516                 }
2517                 if (igotlock)
2518                         nfsv4_unlock(&clp->nfsc_lock, 0);
2519                 NFSUNLOCKCLSTATE();
2520
2521                 /*
2522                  * Delegreturn any delegations cleaned out or recalled.
2523                  */
2524                 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2525                         newnfs_copycred(&dp->nfsdl_cred, cred);
2526                         (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2527                         TAILQ_REMOVE(&dh, dp, nfsdl_list);
2528                         FREE((caddr_t)dp, M_NFSCLDELEG);
2529                 }
2530
2531                 /*
2532                  * Call nfscl_cleanupkext() once per second to check for
2533                  * open/lock owners where the process has exited.
2534                  */
2535                 NFSGETNANOTIME(&mytime);
2536                 if (prevsec != mytime.tv_sec) {
2537                         prevsec = mytime.tv_sec;
2538                         nfscl_cleanupkext(clp);
2539                 }
2540
2541                 NFSLOCKCLSTATE();
2542                 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2543                         (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
2544                             hz);
2545                 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2546                         clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2547                         NFSUNLOCKCLSTATE();
2548                         NFSFREECRED(cred);
2549                         wakeup((caddr_t)clp);
2550                         return;
2551                 }
2552                 NFSUNLOCKCLSTATE();
2553         }
2554 }
2555
2556 /*
2557  * Initiate state recovery. Called when NFSERR_STALECLIENTID or
2558  * NFSERR_STALESTATEID is received.
2559  */
2560 APPLESTATIC void
2561 nfscl_initiate_recovery(struct nfsclclient *clp)
2562 {
2563
2564         if (clp == NULL)
2565                 return;
2566         NFSLOCKCLSTATE();
2567         clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2568         NFSUNLOCKCLSTATE();
2569         wakeup((caddr_t)clp);
2570 }
2571
2572 /*
2573  * Dump out the state stuff for debugging.
2574  */
2575 APPLESTATIC void
2576 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
2577     int lockowner, int locks)
2578 {
2579         struct nfsclclient *clp;
2580         struct nfsclowner *owp;
2581         struct nfsclopen *op;
2582         struct nfscllockowner *lp;
2583         struct nfscllock *lop;
2584         struct nfscldeleg *dp;
2585
2586         clp = nmp->nm_clp;
2587         if (clp == NULL) {
2588                 printf("nfscl dumpstate NULL clp\n");
2589                 return;
2590         }
2591         NFSLOCKCLSTATE();
2592         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2593           LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2594             if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2595                 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2596                     owp->nfsow_owner[0], owp->nfsow_owner[1],
2597                     owp->nfsow_owner[2], owp->nfsow_owner[3],
2598                     owp->nfsow_seqid);
2599             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2600                 if (opens)
2601                     printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2602                         op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2603                         op->nfso_stateid.other[2], op->nfso_opencnt,
2604                         op->nfso_fh[12]);
2605                 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2606                     if (lockowner)
2607                         printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2608                             lp->nfsl_owner[0], lp->nfsl_owner[1],
2609                             lp->nfsl_owner[2], lp->nfsl_owner[3],
2610                             lp->nfsl_seqid,
2611                             lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2612                             lp->nfsl_stateid.other[2]);
2613                     LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2614                         if (locks)
2615 #ifdef __FreeBSD__
2616                             printf("lck typ=%d fst=%ju end=%ju\n",
2617                                 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2618                                 (intmax_t)lop->nfslo_end);
2619 #else
2620                             printf("lck typ=%d fst=%qd end=%qd\n",
2621                                 lop->nfslo_type, lop->nfslo_first,
2622                                 lop->nfslo_end);
2623 #endif
2624                     }
2625                 }
2626             }
2627           }
2628         }
2629         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2630             if (openowner && !LIST_EMPTY(&owp->nfsow_open))
2631                 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
2632                     owp->nfsow_owner[0], owp->nfsow_owner[1],
2633                     owp->nfsow_owner[2], owp->nfsow_owner[3],
2634                     owp->nfsow_seqid);
2635             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2636                 if (opens)
2637                     printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
2638                         op->nfso_stateid.other[0], op->nfso_stateid.other[1],
2639                         op->nfso_stateid.other[2], op->nfso_opencnt,
2640                         op->nfso_fh[12]);
2641                 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
2642                     if (lockowner)
2643                         printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
2644                             lp->nfsl_owner[0], lp->nfsl_owner[1],
2645                             lp->nfsl_owner[2], lp->nfsl_owner[3],
2646                             lp->nfsl_seqid,
2647                             lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
2648                             lp->nfsl_stateid.other[2]);
2649                     LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
2650                         if (locks)
2651 #ifdef __FreeBSD__
2652                             printf("lck typ=%d fst=%ju end=%ju\n",
2653                                 lop->nfslo_type, (intmax_t)lop->nfslo_first,
2654                                 (intmax_t)lop->nfslo_end);
2655 #else
2656                             printf("lck typ=%d fst=%qd end=%qd\n",
2657                                 lop->nfslo_type, lop->nfslo_first,
2658                                 lop->nfslo_end);
2659 #endif
2660                     }
2661                 }
2662             }
2663         }
2664         NFSUNLOCKCLSTATE();
2665 }
2666
2667 /*
2668  * Check for duplicate open owners and opens.
2669  * (Only used as a diagnostic aid.)
2670  */
2671 APPLESTATIC void
2672 nfscl_dupopen(vnode_t vp, int dupopens)
2673 {
2674         struct nfsclclient *clp;
2675         struct nfsclowner *owp, *owp2;
2676         struct nfsclopen *op, *op2;
2677         struct nfsfh *nfhp;
2678
2679         clp = VFSTONFS(vnode_mount(vp))->nm_clp;
2680         if (clp == NULL) {
2681                 printf("nfscl dupopen NULL clp\n");
2682                 return;
2683         }
2684         nfhp = VTONFS(vp)->n_fhp;
2685         NFSLOCKCLSTATE();
2686
2687         /*
2688          * First, search for duplicate owners.
2689          * These should never happen!
2690          */
2691         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2692             LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2693                 if (owp != owp2 &&
2694                     !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
2695                     NFSV4CL_LOCKNAMELEN)) {
2696                         NFSUNLOCKCLSTATE();
2697                         printf("DUP OWNER\n");
2698                         nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0, 0);
2699                         return;
2700                 }
2701             }
2702         }
2703
2704         /*
2705          * Now, search for duplicate stateids.
2706          * These shouldn't happen, either.
2707          */
2708         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2709             LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
2710                 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2711                     LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2712                         if (op != op2 &&
2713                             (op->nfso_stateid.other[0] != 0 ||
2714                              op->nfso_stateid.other[1] != 0 ||
2715                              op->nfso_stateid.other[2] != 0) &&
2716                             op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
2717                             op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
2718                             op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
2719                             NFSUNLOCKCLSTATE();
2720                             printf("DUP STATEID\n");
2721                             nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0,
2722                                 0);
2723                             return;
2724                         }
2725                     }
2726                 }
2727             }
2728         }
2729
2730         /*
2731          * Now search for duplicate opens.
2732          * Duplicate opens for the same owner
2733          * should never occur. Other duplicates are
2734          * possible and are checked for if "dupopens"
2735          * is true.
2736          */
2737         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
2738             LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
2739                 if (nfhp->nfh_len == op2->nfso_fhlen &&
2740                     !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
2741                     LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2742                         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2743                             if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
2744                                 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
2745                                 (!NFSBCMP(op->nfso_own->nfsow_owner,
2746                                  op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
2747                                  dupopens)) {
2748                                 if (!NFSBCMP(op->nfso_own->nfsow_owner,
2749                                     op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
2750                                     NFSUNLOCKCLSTATE();
2751                                     printf("BADDUP OPEN\n");
2752                                 } else {
2753                                     NFSUNLOCKCLSTATE();
2754                                     printf("DUP OPEN\n");
2755                                 }
2756                                 nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1,
2757                                     0, 0);
2758                                 return;
2759                             }
2760                         }
2761                     }
2762                 }
2763             }
2764         }
2765         NFSUNLOCKCLSTATE();
2766 }
2767
2768 /*
2769  * During close, find an open that needs to be dereferenced and
2770  * dereference it. If there are no more opens for this file,
2771  * log a message to that effect.
2772  * Opens aren't actually Close'd until VOP_INACTIVE() is performed
2773  * on the file's vnode.
2774  * This is the safe way, since it is difficult to identify
2775  * which open the close is for and I/O can be performed after the
2776  * close(2) system call when a file is mmap'd.
2777  * If it returns 0 for success, there will be a referenced
2778  * clp returned via clpp.
2779  */
2780 APPLESTATIC int
2781 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
2782 {
2783         struct nfsclclient *clp;
2784         struct nfsclowner *owp;
2785         struct nfsclopen *op;
2786         struct nfscldeleg *dp;
2787         struct nfsfh *nfhp;
2788         int error, notdecr;
2789
2790         error = nfscl_getcl(vp, NULL, NULL, &clp);
2791         if (error)
2792                 return (error);
2793         *clpp = clp;
2794
2795         nfhp = VTONFS(vp)->n_fhp;
2796         notdecr = 1;
2797         NFSLOCKCLSTATE();
2798         /*
2799          * First, look for one under a delegation that was locally issued
2800          * and just decrement the opencnt for it. Since all my Opens against
2801          * the server are DENY_NONE, I don't see a problem with hanging
2802          * onto them. (It is much easier to use one of the extant Opens
2803          * that I already have on the server when a Delegation is recalled
2804          * than to do fresh Opens.) Someday, I might need to rethink this, but.
2805          */
2806         dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
2807         if (dp != NULL) {
2808                 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2809                         op = LIST_FIRST(&owp->nfsow_open);
2810                         if (op != NULL) {
2811                                 /*
2812                                  * Since a delegation is for a file, there
2813                                  * should never be more than one open for
2814                                  * each openowner.
2815                                  */
2816                                 if (LIST_NEXT(op, nfso_list) != NULL)
2817                                         panic("nfscdeleg opens");
2818                                 if (notdecr && op->nfso_opencnt > 0) {
2819                                         notdecr = 0;
2820                                         op->nfso_opencnt--;
2821                                         break;
2822                                 }
2823                         }
2824                 }
2825         }
2826
2827         /* Now process the opens against the server. */
2828         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2829                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
2830                         if (op->nfso_fhlen == nfhp->nfh_len &&
2831                             !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
2832                             nfhp->nfh_len)) {
2833                                 /* Found an open, decrement cnt if possible */
2834                                 if (notdecr && op->nfso_opencnt > 0) {
2835                                         notdecr = 0;
2836                                         op->nfso_opencnt--;
2837                                 }
2838                                 /*
2839                                  * There are more opens, so just return.
2840                                  */
2841                                 if (op->nfso_opencnt > 0) {
2842                                         NFSUNLOCKCLSTATE();
2843                                         return (0);
2844                                 }
2845                         }
2846                 }
2847         }
2848         NFSUNLOCKCLSTATE();
2849         if (notdecr)
2850                 printf("nfscl: never fnd open\n");
2851         return (0);
2852 }
2853
2854 APPLESTATIC int
2855 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
2856 {
2857         struct nfsclclient *clp;
2858         struct nfsclowner *owp, *nowp;
2859         struct nfsclopen *op;
2860         struct nfscldeleg *dp;
2861         struct nfsfh *nfhp;
2862         int error;
2863
2864         error = nfscl_getcl(vp, NULL, NULL, &clp);
2865         if (error)
2866                 return (error);
2867         *clpp = clp;
2868
2869         nfhp = VTONFS(vp)->n_fhp;
2870         NFSLOCKCLSTATE();
2871         /*
2872          * First get rid of the local Open structures, which should be no
2873          * longer in use.
2874          */
2875         dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
2876         if (dp != NULL) {
2877                 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
2878                         op = LIST_FIRST(&owp->nfsow_open);
2879                         if (op != NULL) {
2880                                 KASSERT((op->nfso_opencnt == 0),
2881                                     ("nfscl: bad open cnt on deleg"));
2882                                 nfscl_freeopen(op, 1);
2883                         }
2884                         nfscl_freeopenowner(owp, 1);
2885                 }
2886         }
2887
2888         /* Now process the opens against the server. */
2889 lookformore:
2890         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
2891                 op = LIST_FIRST(&owp->nfsow_open);
2892                 while (op != NULL) {
2893                         if (op->nfso_fhlen == nfhp->nfh_len &&
2894                             !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
2895                             nfhp->nfh_len)) {
2896                                 /* Found an open, close it. */
2897                                 KASSERT((op->nfso_opencnt == 0),
2898                                     ("nfscl: bad open cnt on server"));
2899                                 NFSUNLOCKCLSTATE();
2900                                 nfsrpc_doclose(VFSTONFS(vnode_mount(vp)), op,
2901                                     p);
2902                                 NFSLOCKCLSTATE();
2903                                 goto lookformore;
2904                         }
2905                         op = LIST_NEXT(op, nfso_list);
2906                 }
2907         }
2908         NFSUNLOCKCLSTATE();
2909         return (0);
2910 }
2911
2912 /*
2913  * Return all delegations on this client.
2914  * (Must be called with client sleep lock.)
2915  */
2916 static void
2917 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
2918 {
2919         struct nfscldeleg *dp, *ndp;
2920         struct ucred *cred;
2921
2922         cred = newnfs_getcred();
2923         TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
2924                 nfscl_cleandeleg(dp);
2925                 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2926                 nfscl_freedeleg(&clp->nfsc_deleg, dp);
2927         }
2928         NFSFREECRED(cred);
2929 }
2930
2931 /*
2932  * Do a callback RPC.
2933  */
2934 APPLESTATIC void
2935 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
2936 {
2937         int i, op;
2938         u_int32_t *tl;
2939         struct nfsclclient *clp;
2940         struct nfscldeleg *dp = NULL;
2941         int numops, taglen = -1, error = 0, trunc, ret = 0;
2942         u_int32_t minorvers, retops = 0, *retopsp = NULL, *repp, cbident;
2943         u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
2944         vnode_t vp = NULL;
2945         struct nfsnode *np;
2946         struct vattr va;
2947         struct nfsfh *nfhp;
2948         mount_t mp;
2949         nfsattrbit_t attrbits, rattrbits;
2950         nfsv4stateid_t stateid;
2951
2952         nfsrvd_rephead(nd);
2953         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2954         taglen = fxdr_unsigned(int, *tl);
2955         if (taglen < 0) {
2956                 error = EBADRPC;
2957                 goto nfsmout;
2958         }
2959         if (taglen <= NFSV4_SMALLSTR)
2960                 tagstr = tag;
2961         else
2962                 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2963         error = nfsrv_mtostr(nd, tagstr, taglen);
2964         if (error) {
2965                 if (taglen > NFSV4_SMALLSTR)
2966                         free(tagstr, M_TEMP);
2967                 taglen = -1;
2968                 goto nfsmout;
2969         }
2970         (void) nfsm_strtom(nd, tag, taglen);
2971         if (taglen > NFSV4_SMALLSTR) {
2972                 free(tagstr, M_TEMP);
2973         }
2974         NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
2975         NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2976         minorvers = fxdr_unsigned(u_int32_t, *tl++);
2977         if (minorvers != NFSV4_MINORVERSION)
2978                 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
2979         cbident = fxdr_unsigned(u_int32_t, *tl++);
2980         if (nd->nd_repstat)
2981                 numops = 0;
2982         else
2983                 numops = fxdr_unsigned(int, *tl);
2984         /*
2985          * Loop around doing the sub ops.
2986          */
2987         for (i = 0; i < numops; i++) {
2988                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2989                 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
2990                 *repp++ = *tl;
2991                 op = fxdr_unsigned(int, *tl);
2992                 if (op < NFSV4OP_CBGETATTR || op > NFSV4OP_CBRECALL) {
2993                     nd->nd_repstat = NFSERR_OPILLEGAL;
2994                     *repp = nfscl_errmap(nd);
2995                     retops++;
2996                     break;
2997                 }
2998                 nd->nd_procnum = op;
2999                 newnfsstats.cbrpccnt[nd->nd_procnum]++;
3000                 switch (op) {
3001                 case NFSV4OP_CBGETATTR:
3002                         clp = NULL;
3003                         error = nfsm_getfh(nd, &nfhp);
3004                         if (!error)
3005                                 error = nfsrv_getattrbits(nd, &attrbits,
3006                                     NULL, NULL);
3007                         if (!error) {
3008                                 mp = nfscl_getmnt(cbident);
3009                                 if (mp == NULL)
3010                                         error = NFSERR_SERVERFAULT;
3011                         }
3012                         if (!error) {
3013                                 dp = NULL;
3014                                 NFSLOCKCLSTATE();
3015                                 clp = nfscl_findcl(VFSTONFS(mp));
3016                                 if (clp != NULL)
3017                                         dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3018                                             nfhp->nfh_len);
3019                                 NFSUNLOCKCLSTATE();
3020                                 if (dp == NULL)
3021                                         error = NFSERR_SERVERFAULT;
3022                         }
3023                         if (!error) {
3024                                 ret = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3025                                     nfhp->nfh_len, p, &np);
3026                                 if (!ret)
3027                                         vp = NFSTOV(np);
3028                         }
3029                         if (nfhp != NULL)
3030                                 FREE((caddr_t)nfhp, M_NFSFH);
3031                         if (!error) {
3032                                 NFSZERO_ATTRBIT(&rattrbits);
3033                                 if (NFSISSET_ATTRBIT(&attrbits,
3034                                     NFSATTRBIT_SIZE)) {
3035                                         if (!ret)
3036                                                 va.va_size = np->n_size;
3037                                         else
3038                                                 va.va_size = dp->nfsdl_size;
3039                                         NFSSETBIT_ATTRBIT(&rattrbits,
3040                                             NFSATTRBIT_SIZE);
3041                                 }
3042                                 if (NFSISSET_ATTRBIT(&attrbits,
3043                                     NFSATTRBIT_CHANGE)) {
3044                                         va.va_filerev = dp->nfsdl_change;
3045                                         if (ret || (np->n_flag & NDELEGMOD))
3046                                                 va.va_filerev++;
3047                                         NFSSETBIT_ATTRBIT(&rattrbits,
3048                                             NFSATTRBIT_CHANGE);
3049                                 }
3050                                 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3051                                     NULL, 0, &rattrbits, NULL, NULL, 0, 0, 0, 0,
3052                                     (uint64_t)0);
3053                                 if (!ret)
3054                                         vrele(vp);
3055                         }
3056                         break;
3057                 case NFSV4OP_CBRECALL:
3058                         clp = NULL;
3059                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3060                             NFSX_UNSIGNED);
3061                         stateid.seqid = *tl++;
3062                         NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3063                             NFSX_STATEIDOTHER);
3064                         tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3065                         trunc = fxdr_unsigned(int, *tl);
3066                         error = nfsm_getfh(nd, &nfhp);
3067                         if (!error) {
3068                                 mp = nfscl_getmnt(cbident);
3069                                 if (mp == NULL)
3070                                         error = NFSERR_SERVERFAULT;
3071                         }
3072                         if (!error) {
3073                                 NFSLOCKCLSTATE();
3074                                 clp = nfscl_findcl(VFSTONFS(mp));
3075                                 if (clp != NULL) {
3076                                         dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3077                                             nfhp->nfh_len);
3078                                         if (dp != NULL && (dp->nfsdl_flags &
3079                                             NFSCLDL_DELEGRET) == 0) {
3080                                                 dp->nfsdl_flags |=
3081                                                     NFSCLDL_RECALL;
3082                                                 wakeup((caddr_t)clp);
3083                                         }
3084                                 } else {
3085                                         error = NFSERR_SERVERFAULT;
3086                                 }
3087                                 NFSUNLOCKCLSTATE();
3088                         }
3089                         if (nfhp != NULL)
3090                                 FREE((caddr_t)nfhp, M_NFSFH);
3091                         break;
3092                 };
3093                 if (error) {
3094                         if (error == EBADRPC || error == NFSERR_BADXDR) {
3095                                 nd->nd_repstat = NFSERR_BADXDR;
3096                         } else {
3097                                 nd->nd_repstat = error;
3098                         }
3099                         error = 0;
3100                 }
3101                 retops++;
3102                 if (nd->nd_repstat) {
3103                         *repp = nfscl_errmap(nd);
3104                         break;
3105                 } else
3106                         *repp = 0;      /* NFS4_OK */
3107         }
3108 nfsmout:
3109         if (error) {
3110                 if (error == EBADRPC || error == NFSERR_BADXDR)
3111                         nd->nd_repstat = NFSERR_BADXDR;
3112                 else
3113                         printf("nfsv4 comperr1=%d\n", error);
3114         }
3115         if (taglen == -1) {
3116                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3117                 *tl++ = 0;
3118                 *tl = 0;
3119         } else {
3120                 *retopsp = txdr_unsigned(retops);
3121         }
3122         *nd->nd_errp = nfscl_errmap(nd);
3123 }
3124
3125 /*
3126  * Generate the next cbident value. Basically just increment a static value
3127  * and then check that it isn't already in the list, if it has wrapped around.
3128  */
3129 static u_int32_t
3130 nfscl_nextcbident(void)
3131 {
3132         struct nfsclclient *clp;
3133         int matched;
3134         static u_int32_t nextcbident = 0;
3135         static int haswrapped = 0;
3136
3137         nextcbident++;
3138         if (nextcbident == 0)
3139                 haswrapped = 1;
3140         if (haswrapped) {
3141                 /*
3142                  * Search the clientid list for one already using this cbident.
3143                  */
3144                 do {
3145                         matched = 0;
3146                         NFSLOCKCLSTATE();
3147                         LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3148                                 if (clp->nfsc_cbident == nextcbident) {
3149                                         matched = 1;
3150                                         break;
3151                                 }
3152                         }
3153                         NFSUNLOCKCLSTATE();
3154                         if (matched == 1)
3155                                 nextcbident++;
3156                 } while (matched);
3157         }
3158         return (nextcbident);
3159 }
3160
3161 /*
3162  * Get the mount point related to a given cbident.
3163  */
3164 static mount_t
3165 nfscl_getmnt(u_int32_t cbident)
3166 {
3167         struct nfsclclient *clp;
3168         struct nfsmount *nmp;
3169
3170         NFSLOCKCLSTATE();
3171         LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3172                 if (clp->nfsc_cbident == cbident)
3173                         break;
3174         }
3175         if (clp == NULL) {
3176                 NFSUNLOCKCLSTATE();
3177                 return (NULL);
3178         }
3179         nmp = clp->nfsc_nmp;
3180         NFSUNLOCKCLSTATE();
3181         return (nmp->nm_mountp);
3182 }
3183
3184 /*
3185  * Search for a lock conflict locally on the client. A conflict occurs if
3186  * - not same owner and overlapping byte range and at least one of them is
3187  *   a write lock or this is an unlock.
3188  */
3189 static int
3190 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
3191     struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
3192     struct nfscllock **lopp)
3193 {
3194         struct nfsclowner *owp;
3195         struct nfsclopen *op;
3196         int ret;
3197
3198         if (dp != NULL) {
3199                 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3200                 if (ret)
3201                         return (ret);
3202         }
3203         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3204                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3205                         if (op->nfso_fhlen == fhlen &&
3206                             !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
3207                                 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
3208                                     own, lopp);
3209                                 if (ret)
3210                                         return (ret);
3211                         }
3212                 }
3213         }
3214         return (0);
3215 }
3216
3217 static int
3218 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3219     u_int8_t *own, struct nfscllock **lopp)
3220 {
3221         struct nfscllockowner *lp;
3222         struct nfscllock *lop;
3223
3224         LIST_FOREACH(lp, lhp, nfsl_list) {
3225                 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
3226                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3227                                 if (lop->nfslo_first >= nlop->nfslo_end)
3228                                         break;
3229                                 if (lop->nfslo_end <= nlop->nfslo_first)
3230                                         continue;
3231                                 if (lop->nfslo_type == F_WRLCK ||
3232                                     nlop->nfslo_type == F_WRLCK ||
3233                                     nlop->nfslo_type == F_UNLCK) {
3234                                         if (lopp != NULL)
3235                                                 *lopp = lop;
3236                                         return (NFSERR_DENIED);
3237                                 }
3238                         }
3239                 }
3240         }
3241         return (0);
3242 }
3243
3244 /*
3245  * Check for a local conflicting lock.
3246  */
3247 APPLESTATIC int
3248 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
3249     u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
3250 {
3251         struct nfscllock *lop, nlck;
3252         struct nfscldeleg *dp;
3253         struct nfsnode *np;
3254         u_int8_t own[NFSV4CL_LOCKNAMELEN];
3255         int error;
3256
3257         nlck.nfslo_type = fl->l_type;
3258         nlck.nfslo_first = off;
3259         if (len == NFS64BITSSET) {
3260                 nlck.nfslo_end = NFS64BITSSET;
3261         } else {
3262                 nlck.nfslo_end = off + len;
3263                 if (nlck.nfslo_end <= nlck.nfslo_first)
3264                         return (NFSERR_INVAL);
3265         }
3266         np = VTONFS(vp);
3267         nfscl_filllockowner(id, own, flags);
3268         NFSLOCKCLSTATE();
3269         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3270         error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
3271             &nlck, own, dp, &lop);
3272         if (error != 0) {
3273                 fl->l_whence = SEEK_SET;
3274                 fl->l_start = lop->nfslo_first;
3275                 if (lop->nfslo_end == NFS64BITSSET)
3276                         fl->l_len = 0;
3277                 else
3278                         fl->l_len = lop->nfslo_end - lop->nfslo_first;
3279                 fl->l_pid = (pid_t)0;
3280                 fl->l_type = lop->nfslo_type;
3281                 error = -1;                     /* no RPC required */
3282         } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
3283             fl->l_type == F_RDLCK)) {
3284                 /*
3285                  * The delegation ensures that there isn't a conflicting
3286                  * lock on the server, so return -1 to indicate an RPC
3287                  * isn't required.
3288                  */
3289                 fl->l_type = F_UNLCK;
3290                 error = -1;
3291         }
3292         NFSUNLOCKCLSTATE();
3293         return (error);
3294 }
3295
3296 /*
3297  * Handle Recall of a delegation.
3298  * The clp must be exclusive locked when this is called.
3299  */
3300 static int
3301 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
3302     struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
3303     int called_from_renewthread)
3304 {
3305         struct nfsclowner *owp, *lowp, *nowp;
3306         struct nfsclopen *op, *lop;
3307         struct nfscllockowner *lp;
3308         struct nfscllock *lckp;
3309         struct nfsnode *np;
3310         int error = 0, ret, gotvp = 0;
3311
3312         if (vp == NULL) {
3313                 /*
3314                  * First, get a vnode for the file. This is needed to do RPCs.
3315                  */
3316                 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
3317                     dp->nfsdl_fhlen, p, &np);
3318                 if (ret) {
3319                         /*
3320                          * File isn't open, so nothing to move over to the
3321                          * server.
3322                          */
3323                         return (0);
3324                 }
3325                 vp = NFSTOV(np);
3326                 gotvp = 1;
3327         } else {
3328                 np = VTONFS(vp);
3329         }
3330         dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
3331
3332         /*
3333          * Ok, if it's a write delegation, flush data to the server, so
3334          * that close/open consistency is retained.
3335          */
3336         ret = 0;
3337         NFSLOCKNODE(np);
3338         if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
3339                 np->n_flag |= NDELEGRECALL;
3340                 NFSUNLOCKNODE(np);
3341                 ret = ncl_flush(vp, MNT_WAIT, cred, p, 1,
3342                     called_from_renewthread);
3343                 NFSLOCKNODE(np);
3344                 np->n_flag &= ~NDELEGRECALL;
3345         }
3346         NFSINVALATTRCACHE(np);
3347         NFSUNLOCKNODE(np);
3348         if (ret == EIO && called_from_renewthread != 0) {
3349                 /*
3350                  * If the flush failed with EIO for the renew thread,
3351                  * return now, so that the dirty buffer will be flushed
3352                  * later.
3353                  */
3354                 if (gotvp != 0)
3355                         vrele(vp);
3356                 return (ret);
3357         }
3358
3359         /*
3360          * Now, for each openowner with opens issued locally, move them
3361          * over to state against the server.
3362          */
3363         LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
3364                 lop = LIST_FIRST(&lowp->nfsow_open);
3365                 if (lop != NULL) {
3366                         if (LIST_NEXT(lop, nfso_list) != NULL)
3367                                 panic("nfsdlg mult opens");
3368                         /*
3369                          * Look for the same openowner against the server.
3370                          */
3371                         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3372                                 if (!NFSBCMP(lowp->nfsow_owner,
3373                                     owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3374                                         newnfs_copycred(&dp->nfsdl_cred, cred);
3375                                         ret = nfscl_moveopen(vp, clp, nmp, lop,
3376                                             owp, dp, cred, p);
3377                                         if (ret == NFSERR_STALECLIENTID ||
3378                                             ret == NFSERR_STALEDONTRECOVER) {
3379                                                 if (gotvp)
3380                                                         vrele(vp);
3381                                                 return (ret);
3382                                         }
3383                                         if (ret) {
3384                                                 nfscl_freeopen(lop, 1);
3385                                                 if (!error)
3386                                                         error = ret;
3387                                         }
3388                                         break;
3389                                 }
3390                         }
3391
3392                         /*
3393                          * If no openowner found, create one and get an open
3394                          * for it.
3395                          */
3396                         if (owp == NULL) {
3397                                 MALLOC(nowp, struct nfsclowner *,
3398                                     sizeof (struct nfsclowner), M_NFSCLOWNER,
3399                                     M_WAITOK);
3400                                 nfscl_newopen(clp, NULL, &owp, &nowp, &op, 
3401                                     NULL, lowp->nfsow_owner, dp->nfsdl_fh,
3402                                     dp->nfsdl_fhlen, NULL);
3403                                 newnfs_copycred(&dp->nfsdl_cred, cred);
3404                                 ret = nfscl_moveopen(vp, clp, nmp, lop,
3405                                     owp, dp, cred, p);
3406                                 if (ret) {
3407                                         nfscl_freeopenowner(owp, 0);
3408                                         if (ret == NFSERR_STALECLIENTID ||
3409                                             ret == NFSERR_STALEDONTRECOVER) {
3410                                                 if (gotvp)
3411                                                         vrele(vp);
3412                                                 return (ret);
3413                                         }
3414                                         if (ret) {
3415                                                 nfscl_freeopen(lop, 1);
3416                                                 if (!error)
3417                                                         error = ret;
3418                                         }
3419                                 }
3420                         }
3421                 }
3422         }
3423
3424         /*
3425          * Now, get byte range locks for any locks done locally.
3426          */
3427         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
3428                 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
3429                         newnfs_copycred(&dp->nfsdl_cred, cred);
3430                         ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
3431                         if (ret == NFSERR_STALESTATEID ||
3432                             ret == NFSERR_STALEDONTRECOVER ||
3433                             ret == NFSERR_STALECLIENTID) {
3434                                 if (gotvp)
3435                                         vrele(vp);
3436                                 return (ret);
3437                         }
3438                         if (ret && !error)
3439                                 error = ret;
3440                 }
3441         }
3442         if (gotvp)
3443                 vrele(vp);
3444         return (error);
3445 }
3446
3447 /*
3448  * Move a locally issued open over to an owner on the state list.
3449  * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
3450  * returns with it unlocked.
3451  */
3452 static int
3453 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
3454     struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
3455     struct ucred *cred, NFSPROC_T *p)
3456 {
3457         struct nfsclopen *op, *nop;
3458         struct nfscldeleg *ndp;
3459         struct nfsnode *np;
3460         int error = 0, newone;
3461
3462         /*
3463          * First, look for an appropriate open, If found, just increment the
3464          * opencnt in it.
3465          */
3466         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3467                 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
3468                     op->nfso_fhlen == lop->nfso_fhlen &&
3469                     !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
3470                         op->nfso_opencnt += lop->nfso_opencnt;
3471                         nfscl_freeopen(lop, 1);
3472                         return (0);
3473                 }
3474         }
3475
3476         /* No appropriate open, so we have to do one against the server. */
3477         np = VTONFS(vp);
3478         MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
3479             lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
3480         newone = 0;
3481         nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
3482             lop->nfso_fh, lop->nfso_fhlen, &newone);
3483         ndp = dp;
3484         error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
3485             lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
3486             NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
3487         if (error) {
3488                 if (newone)
3489                         nfscl_freeopen(op, 0);
3490         } else {
3491                 if (newone)
3492                         newnfs_copyincred(cred, &op->nfso_cred);
3493                 op->nfso_mode |= lop->nfso_mode;
3494                 op->nfso_opencnt += lop->nfso_opencnt;
3495                 nfscl_freeopen(lop, 1);
3496         }
3497         if (nop != NULL)
3498                 FREE((caddr_t)nop, M_NFSCLOPEN);
3499         if (ndp != NULL) {
3500                 /*
3501                  * What should I do with the returned delegation, since the
3502                  * delegation is being recalled? For now, just printf and
3503                  * through it away.
3504                  */
3505                 printf("Moveopen returned deleg\n");
3506                 FREE((caddr_t)ndp, M_NFSCLDELEG);
3507         }
3508         return (error);
3509 }
3510
3511 /*
3512  * Recall all delegations on this client.
3513  */
3514 static void
3515 nfscl_totalrecall(struct nfsclclient *clp)
3516 {
3517         struct nfscldeleg *dp;
3518
3519         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
3520                 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
3521                         dp->nfsdl_flags |= NFSCLDL_RECALL;
3522         }
3523 }
3524
3525 /*
3526  * Relock byte ranges. Called for delegation recall and state expiry.
3527  */
3528 static int
3529 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
3530     struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
3531     NFSPROC_T *p)
3532 {
3533         struct nfscllockowner *nlp;
3534         struct nfsfh *nfhp;
3535         u_int64_t off, len;
3536         u_int32_t clidrev = 0;
3537         int error, newone, donelocally;
3538
3539         off = lop->nfslo_first;
3540         len = lop->nfslo_end - lop->nfslo_first;
3541         error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
3542             clp, 1, NULL, 0, lp->nfsl_owner, lp->nfsl_openowner, &nlp, &newone,
3543             &donelocally);
3544         if (error || donelocally)
3545                 return (error);
3546         if (nmp->nm_clp != NULL)
3547                 clidrev = nmp->nm_clp->nfsc_clientidrev;
3548         else
3549                 clidrev = 0;
3550         nfhp = VTONFS(vp)->n_fhp;
3551         error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
3552             nfhp->nfh_len, nlp, newone, 0, off,
3553             len, lop->nfslo_type, cred, p);
3554         if (error)
3555                 nfscl_freelockowner(nlp, 0);
3556         return (error);
3557 }
3558
3559 /*
3560  * Called to re-open a file. Basically get a vnode for the file handle
3561  * and then call nfsrpc_openrpc() to do the rest.
3562  */
3563 static int
3564 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
3565     u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
3566     struct ucred *cred, NFSPROC_T *p)
3567 {
3568         struct nfsnode *np;
3569         vnode_t vp;
3570         int error;
3571
3572         error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
3573         if (error)
3574                 return (error);
3575         vp = NFSTOV(np);
3576         if (np->n_v4 != NULL) {
3577                 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
3578                     np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
3579                     NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
3580                     cred, p);
3581         } else {
3582                 error = EINVAL;
3583         }
3584         vrele(vp);
3585         return (error);
3586 }
3587
3588 /*
3589  * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
3590  * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
3591  * fail.
3592  */
3593 static int
3594 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
3595     u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
3596     u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
3597     int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
3598 {
3599         int error;
3600
3601         do {
3602                 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
3603                     mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
3604                     0, 0);
3605                 if (error == NFSERR_DELAY)
3606                         (void) nfs_catnap(PZERO, error, "nfstryop");
3607         } while (error == NFSERR_DELAY);
3608         if (error == EAUTH || error == EACCES) {
3609                 /* Try again using system credentials */
3610                 newnfs_setroot(cred);
3611                 do {
3612                     error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
3613                         newfhlen, mode, op, name, namelen, ndpp, reclaim,
3614                         delegtype, cred, p, 1, 0);
3615                     if (error == NFSERR_DELAY)
3616                         (void) nfs_catnap(PZERO, error, "nfstryop");
3617                 } while (error == NFSERR_DELAY);
3618         }
3619         return (error);
3620 }
3621
3622 /*
3623  * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
3624  * NFSERR_DELAY. Also, retry with system credentials, if the provided
3625  * cred don't work.
3626  */
3627 static int
3628 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
3629     int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
3630     u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
3631 {
3632         struct nfsrv_descript nfsd, *nd = &nfsd;
3633         int error;
3634
3635         do {
3636                 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
3637                     reclaim, off, len, type, cred, p, 0);
3638                 if (!error && nd->nd_repstat == NFSERR_DELAY)
3639                         (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3640                             "nfstrylck");
3641         } while (!error && nd->nd_repstat == NFSERR_DELAY);
3642         if (!error)
3643                 error = nd->nd_repstat;
3644         if (error == EAUTH || error == EACCES) {
3645                 /* Try again using root credentials */
3646                 newnfs_setroot(cred);
3647                 do {
3648                         error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
3649                             newone, reclaim, off, len, type, cred, p, 1);
3650                         if (!error && nd->nd_repstat == NFSERR_DELAY)
3651                                 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
3652                                     "nfstrylck");
3653                 } while (!error && nd->nd_repstat == NFSERR_DELAY);
3654                 if (!error)
3655                         error = nd->nd_repstat;
3656         }
3657         return (error);
3658 }
3659
3660 /*
3661  * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
3662  * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
3663  * credentials fail.
3664  */
3665 static int
3666 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
3667     struct nfsmount *nmp, NFSPROC_T *p)
3668 {
3669         int error;
3670
3671         do {
3672                 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
3673                 if (error == NFSERR_DELAY)
3674                         (void) nfs_catnap(PZERO, error, "nfstrydp");
3675         } while (error == NFSERR_DELAY);
3676         if (error == EAUTH || error == EACCES) {
3677                 /* Try again using system credentials */
3678                 newnfs_setroot(cred);
3679                 do {
3680                         error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
3681                         if (error == NFSERR_DELAY)
3682                                 (void) nfs_catnap(PZERO, error, "nfstrydp");
3683                 } while (error == NFSERR_DELAY);
3684         }
3685         return (error);
3686 }
3687
3688 /*
3689  * Try a close against the server. Just call nfsrpc_closerpc(),
3690  * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
3691  * credentials fail.
3692  */
3693 APPLESTATIC int
3694 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
3695     struct nfsmount *nmp, NFSPROC_T *p)
3696 {
3697         struct nfsrv_descript nfsd, *nd = &nfsd;
3698         int error;
3699
3700         do {
3701                 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
3702                 if (error == NFSERR_DELAY)
3703                         (void) nfs_catnap(PZERO, error, "nfstrycl");
3704         } while (error == NFSERR_DELAY);
3705         if (error == EAUTH || error == EACCES) {
3706                 /* Try again using system credentials */
3707                 newnfs_setroot(cred);
3708                 do {
3709                         error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
3710                         if (error == NFSERR_DELAY)
3711                                 (void) nfs_catnap(PZERO, error, "nfstrycl");
3712                 } while (error == NFSERR_DELAY);
3713         }
3714         return (error);
3715 }
3716
3717 /*
3718  * Decide if a delegation on a file permits close without flushing writes
3719  * to the server. This might be a big performance win in some environments.
3720  * (Not useful until the client does caching on local stable storage.)
3721  */
3722 APPLESTATIC int
3723 nfscl_mustflush(vnode_t vp)
3724 {
3725         struct nfsclclient *clp;
3726         struct nfscldeleg *dp;
3727         struct nfsnode *np;
3728         struct nfsmount *nmp;
3729
3730         np = VTONFS(vp);
3731         nmp = VFSTONFS(vnode_mount(vp));
3732         if (!NFSHASNFSV4(nmp))
3733                 return (1);
3734         NFSLOCKCLSTATE();
3735         clp = nfscl_findcl(nmp);
3736         if (clp == NULL) {
3737                 NFSUNLOCKCLSTATE();
3738                 return (1);
3739         }
3740         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3741         if (dp != NULL && (dp->nfsdl_flags &
3742             (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
3743              NFSCLDL_WRITE &&
3744             (dp->nfsdl_sizelimit >= np->n_size ||
3745              !NFSHASSTRICT3530(nmp))) {
3746                 NFSUNLOCKCLSTATE();
3747                 return (0);
3748         }
3749         NFSUNLOCKCLSTATE();
3750         return (1);
3751 }
3752
3753 /*
3754  * See if a (write) delegation exists for this file.
3755  */
3756 APPLESTATIC int
3757 nfscl_nodeleg(vnode_t vp, int writedeleg)
3758 {
3759         struct nfsclclient *clp;
3760         struct nfscldeleg *dp;
3761         struct nfsnode *np;
3762         struct nfsmount *nmp;
3763
3764         np = VTONFS(vp);
3765         nmp = VFSTONFS(vnode_mount(vp));
3766         if (!NFSHASNFSV4(nmp))
3767                 return (1);
3768         NFSLOCKCLSTATE();
3769         clp = nfscl_findcl(nmp);
3770         if (clp == NULL) {
3771                 NFSUNLOCKCLSTATE();
3772                 return (1);
3773         }
3774         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
3775         if (dp != NULL &&
3776             (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
3777             (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
3778              NFSCLDL_WRITE)) {
3779                 NFSUNLOCKCLSTATE();
3780                 return (0);
3781         }
3782         NFSUNLOCKCLSTATE();
3783         return (1);
3784 }
3785
3786 /*
3787  * Look for an associated delegation that should be DelegReturned.
3788  */
3789 APPLESTATIC int
3790 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
3791 {
3792         struct nfsclclient *clp;
3793         struct nfscldeleg *dp;
3794         struct nfsclowner *owp;
3795         struct nfscllockowner *lp;
3796         struct nfsmount *nmp;
3797         struct ucred *cred;
3798         struct nfsnode *np;
3799         int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
3800
3801         nmp = VFSTONFS(vnode_mount(vp));
3802         np = VTONFS(vp);
3803         NFSLOCKCLSTATE();
3804         /*
3805          * Loop around waiting for:
3806          * - outstanding I/O operations on delegations to complete
3807          * - for a delegation on vp that has state, lock the client and
3808          *   do a recall
3809          * - return delegation with no state
3810          */
3811         while (1) {
3812                 clp = nfscl_findcl(nmp);
3813                 if (clp == NULL) {
3814                         NFSUNLOCKCLSTATE();
3815                         return (retcnt);
3816                 }
3817                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3818                     np->n_fhp->nfh_len);
3819                 if (dp != NULL) {
3820                     /*
3821                      * Wait for outstanding I/O ops to be done.
3822                      */
3823                     if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
3824                         if (igotlock) {
3825                             nfsv4_unlock(&clp->nfsc_lock, 0);
3826                             igotlock = 0;
3827                         }
3828                         dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
3829                         (void) nfsmsleep(&dp->nfsdl_rwlock,
3830                             NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
3831                         continue;
3832                     }
3833                     needsrecall = 0;
3834                     LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3835                         if (!LIST_EMPTY(&owp->nfsow_open)) {
3836                             needsrecall = 1;
3837                             break;
3838                         }
3839                     }
3840                     if (!needsrecall) {
3841                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
3842                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
3843                                 needsrecall = 1;
3844                                 break;
3845                             }
3846                         }
3847                     }
3848                     if (needsrecall && !triedrecall) {
3849                         dp->nfsdl_flags |= NFSCLDL_DELEGRET;
3850                         islept = 0;
3851                         while (!igotlock) {
3852                             igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
3853                                 &islept, NFSCLSTATEMUTEXPTR, NULL);
3854                             if (islept)
3855                                 break;
3856                         }
3857                         if (islept)
3858                             continue;
3859                         NFSUNLOCKCLSTATE();
3860                         cred = newnfs_getcred();
3861                         newnfs_copycred(&dp->nfsdl_cred, cred);
3862                         (void) nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0);
3863                         NFSFREECRED(cred);
3864                         triedrecall = 1;
3865                         NFSLOCKCLSTATE();
3866                         nfsv4_unlock(&clp->nfsc_lock, 0);
3867                         igotlock = 0;
3868                         continue;
3869                     }
3870                     *stp = dp->nfsdl_stateid;
3871                     retcnt = 1;
3872                     nfscl_cleandeleg(dp);
3873                     nfscl_freedeleg(&clp->nfsc_deleg, dp);
3874                 }
3875                 if (igotlock)
3876                     nfsv4_unlock(&clp->nfsc_lock, 0);
3877                 NFSUNLOCKCLSTATE();
3878                 return (retcnt);
3879         }
3880 }
3881
3882 /*
3883  * Look for associated delegation(s) that should be DelegReturned.
3884  */
3885 APPLESTATIC int
3886 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
3887     nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
3888 {
3889         struct nfsclclient *clp;
3890         struct nfscldeleg *dp;
3891         struct nfsclowner *owp;
3892         struct nfscllockowner *lp;
3893         struct nfsmount *nmp;
3894         struct ucred *cred;
3895         struct nfsnode *np;
3896         int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
3897
3898         nmp = VFSTONFS(vnode_mount(fvp));
3899         *gotfdp = 0;
3900         *gottdp = 0;
3901         NFSLOCKCLSTATE();
3902         /*
3903          * Loop around waiting for:
3904          * - outstanding I/O operations on delegations to complete
3905          * - for a delegation on fvp that has state, lock the client and
3906          *   do a recall
3907          * - return delegation(s) with no state.
3908          */
3909         while (1) {
3910                 clp = nfscl_findcl(nmp);
3911                 if (clp == NULL) {
3912                         NFSUNLOCKCLSTATE();
3913                         return (retcnt);
3914                 }
3915                 np = VTONFS(fvp);
3916                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3917                     np->n_fhp->nfh_len);
3918                 if (dp != NULL && *gotfdp == 0) {
3919                     /*
3920                      * Wait for outstanding I/O ops to be done.
3921                      */
3922                     if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
3923                         if (igotlock) {
3924                             nfsv4_unlock(&clp->nfsc_lock, 0);
3925                             igotlock = 0;
3926                         }
3927                         dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
3928                         (void) nfsmsleep(&dp->nfsdl_rwlock,
3929                             NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
3930                         continue;
3931                     }
3932                     needsrecall = 0;
3933                     LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3934                         if (!LIST_EMPTY(&owp->nfsow_open)) {
3935                             needsrecall = 1;
3936                             break;
3937                         }
3938                     }
3939                     if (!needsrecall) {
3940                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
3941                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
3942                                 needsrecall = 1;
3943                                 break;
3944                             }
3945                         }
3946                     }
3947                     if (needsrecall && !triedrecall) {
3948                         dp->nfsdl_flags |= NFSCLDL_DELEGRET;
3949                         islept = 0;
3950                         while (!igotlock) {
3951                             igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
3952                                 &islept, NFSCLSTATEMUTEXPTR, NULL);
3953                             if (islept)
3954                                 break;
3955                         }
3956                         if (islept)
3957                             continue;
3958                         NFSUNLOCKCLSTATE();
3959                         cred = newnfs_getcred();
3960                         newnfs_copycred(&dp->nfsdl_cred, cred);
3961                         (void) nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0);
3962                         NFSFREECRED(cred);
3963                         triedrecall = 1;
3964                         NFSLOCKCLSTATE();
3965                         nfsv4_unlock(&clp->nfsc_lock, 0);
3966                         igotlock = 0;
3967                         continue;
3968                     }
3969                     *fstp = dp->nfsdl_stateid;
3970                     retcnt++;
3971                     *gotfdp = 1;
3972                     nfscl_cleandeleg(dp);
3973                     nfscl_freedeleg(&clp->nfsc_deleg, dp);
3974                 }
3975                 if (igotlock) {
3976                     nfsv4_unlock(&clp->nfsc_lock, 0);
3977                     igotlock = 0;
3978                 }
3979                 if (tvp != NULL) {
3980                     np = VTONFS(tvp);
3981                     dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3982                         np->n_fhp->nfh_len);
3983                     if (dp != NULL && *gottdp == 0) {
3984                         /*
3985                          * Wait for outstanding I/O ops to be done.
3986                          */
3987                         if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
3988                             dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
3989                             (void) nfsmsleep(&dp->nfsdl_rwlock,
3990                                 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
3991                             continue;
3992                         }
3993                         LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3994                             if (!LIST_EMPTY(&owp->nfsow_open)) {
3995                                 NFSUNLOCKCLSTATE();
3996                                 return (retcnt);
3997                             }
3998                         }
3999                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4000                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
4001                                 NFSUNLOCKCLSTATE();
4002                                 return (retcnt);
4003                             }
4004                         }
4005                         *tstp = dp->nfsdl_stateid;
4006                         retcnt++;
4007                         *gottdp = 1;
4008                         nfscl_cleandeleg(dp);
4009                         nfscl_freedeleg(&clp->nfsc_deleg, dp);
4010                     }
4011                 }
4012                 NFSUNLOCKCLSTATE();
4013                 return (retcnt);
4014         }
4015 }
4016
4017 /*
4018  * Get a reference on the clientid associated with the mount point.
4019  * Return 1 if success, 0 otherwise.
4020  */
4021 APPLESTATIC int
4022 nfscl_getref(struct nfsmount *nmp)
4023 {
4024         struct nfsclclient *clp;
4025
4026         NFSLOCKCLSTATE();
4027         clp = nfscl_findcl(nmp);
4028         if (clp == NULL) {
4029                 NFSUNLOCKCLSTATE();
4030                 return (0);
4031         }
4032         nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
4033         NFSUNLOCKCLSTATE();
4034         return (1);
4035 }
4036
4037 /*
4038  * Release a reference on a clientid acquired with the above call.
4039  */
4040 APPLESTATIC void
4041 nfscl_relref(struct nfsmount *nmp)
4042 {
4043         struct nfsclclient *clp;
4044
4045         NFSLOCKCLSTATE();
4046         clp = nfscl_findcl(nmp);
4047         if (clp == NULL) {
4048                 NFSUNLOCKCLSTATE();
4049                 return;
4050         }
4051         nfsv4_relref(&clp->nfsc_lock);
4052         NFSUNLOCKCLSTATE();
4053 }
4054
4055 /*
4056  * Save the size attribute in the delegation, since the nfsnode
4057  * is going away.
4058  */
4059 APPLESTATIC void
4060 nfscl_reclaimnode(vnode_t vp)
4061 {
4062         struct nfsclclient *clp;
4063         struct nfscldeleg *dp;
4064         struct nfsnode *np = VTONFS(vp);
4065         struct nfsmount *nmp;
4066
4067         nmp = VFSTONFS(vnode_mount(vp));
4068         if (!NFSHASNFSV4(nmp))
4069                 return;
4070         NFSLOCKCLSTATE();
4071         clp = nfscl_findcl(nmp);
4072         if (clp == NULL) {
4073                 NFSUNLOCKCLSTATE();
4074                 return;
4075         }
4076         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4077         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4078                 dp->nfsdl_size = np->n_size;
4079         NFSUNLOCKCLSTATE();
4080 }
4081
4082 /*
4083  * Get the saved size attribute in the delegation, since it is a
4084  * newly allocated nfsnode.
4085  */
4086 APPLESTATIC void
4087 nfscl_newnode(vnode_t vp)
4088 {
4089         struct nfsclclient *clp;
4090         struct nfscldeleg *dp;
4091         struct nfsnode *np = VTONFS(vp);
4092         struct nfsmount *nmp;
4093
4094         nmp = VFSTONFS(vnode_mount(vp));
4095         if (!NFSHASNFSV4(nmp))
4096                 return;
4097         NFSLOCKCLSTATE();
4098         clp = nfscl_findcl(nmp);
4099         if (clp == NULL) {
4100                 NFSUNLOCKCLSTATE();
4101                 return;
4102         }
4103         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4104         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4105                 np->n_size = dp->nfsdl_size;
4106         NFSUNLOCKCLSTATE();
4107 }
4108
4109 /*
4110  * If there is a valid write delegation for this file, set the modtime
4111  * to the local clock time.
4112  */
4113 APPLESTATIC void
4114 nfscl_delegmodtime(vnode_t vp)
4115 {
4116         struct nfsclclient *clp;
4117         struct nfscldeleg *dp;
4118         struct nfsnode *np = VTONFS(vp);
4119         struct nfsmount *nmp;
4120
4121         nmp = VFSTONFS(vnode_mount(vp));
4122         if (!NFSHASNFSV4(nmp))
4123                 return;
4124         NFSLOCKCLSTATE();
4125         clp = nfscl_findcl(nmp);
4126         if (clp == NULL) {
4127                 NFSUNLOCKCLSTATE();
4128                 return;
4129         }
4130         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4131         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4132                 NFSGETNANOTIME(&dp->nfsdl_modtime);
4133                 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4134         }
4135         NFSUNLOCKCLSTATE();
4136 }
4137
4138 /*
4139  * If there is a valid write delegation for this file with a modtime set,
4140  * put that modtime in mtime.
4141  */
4142 APPLESTATIC void
4143 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4144 {
4145         struct nfsclclient *clp;
4146         struct nfscldeleg *dp;
4147         struct nfsnode *np = VTONFS(vp);
4148         struct nfsmount *nmp;
4149
4150         nmp = VFSTONFS(vnode_mount(vp));
4151         if (!NFSHASNFSV4(nmp))
4152                 return;
4153         NFSLOCKCLSTATE();
4154         clp = nfscl_findcl(nmp);
4155         if (clp == NULL) {
4156                 NFSUNLOCKCLSTATE();
4157                 return;
4158         }
4159         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4160         if (dp != NULL &&
4161             (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4162             (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4163                 *mtime = dp->nfsdl_modtime;
4164         NFSUNLOCKCLSTATE();
4165 }
4166
4167 static int
4168 nfscl_errmap(struct nfsrv_descript *nd)
4169 {
4170         short *defaulterrp, *errp;
4171
4172         if (!nd->nd_repstat)
4173                 return (0);
4174         if (nd->nd_procnum == NFSPROC_NOOP)
4175                 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4176         if (nd->nd_repstat == EBADRPC)
4177                 return (txdr_unsigned(NFSERR_BADXDR));
4178         if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4179             nd->nd_repstat == NFSERR_OPILLEGAL)
4180                 return (txdr_unsigned(nd->nd_repstat));
4181         errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4182         while (*++errp)
4183                 if (*errp == (short)nd->nd_repstat)
4184                         return (txdr_unsigned(nd->nd_repstat));
4185         return (txdr_unsigned(*defaulterrp));
4186 }
4187