]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nfsclient/nfs_clstate.c
nfscl: Use hash lists to improve expected search performance for opens
[FreeBSD/FreeBSD.git] / sys / fs / nfsclient / nfs_clstate.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Rick Macklem, University of Guelph
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34  * These functions implement the client side state handling for NFSv4.
35  * NFSv4 state handling:
36  * - A lockowner is used to determine lock contention, so it
37  *   corresponds directly to a Posix pid. (1 to 1 mapping)
38  * - The correct granularity of an OpenOwner is not nearly so
39  *   obvious. An OpenOwner does the following:
40  *   - provides a serial sequencing of Open/Close/Lock-with-new-lockowner
41  *   - is used to check for Open/Share contention (not applicable to
42  *     this client, since all Opens are Deny_None)
43  *   As such, I considered both extreme.
44  *   1 OpenOwner per ClientID - Simple to manage, but fully serializes
45  *   all Open, Close and Lock (with a new lockowner) Ops.
46  *   1 OpenOwner for each Open - This one results in an OpenConfirm for
47  *   every Open, for most servers.
48  *   So, I chose to use the same mapping as I did for LockOwnwers.
49  *   The main concern here is that you can end up with multiple Opens
50  *   for the same File Handle, but on different OpenOwners (opens
51  *   inherited from parents, grandparents...) and you do not know
52  *   which of these the vnodeop close applies to. This is handled by
53  *   delaying the Close Op(s) until all of the Opens have been closed.
54  *   (It is not yet obvious if this is the correct granularity.)
55  * - How the code handles serialization:
56  *   - For the ClientId, it uses an exclusive lock while getting its
57  *     SetClientId and during recovery. Otherwise, it uses a shared
58  *     lock via a reference count.
59  *   - For the rest of the data structures, it uses an SMP mutex
60  *     (once the nfs client is SMP safe) and doesn't sleep while
61  *     manipulating the linked lists.
62  *   - The serialization of Open/Close/Lock/LockU falls out in the
63  *     "wash", since OpenOwners and LockOwners are both mapped from
64  *     Posix pid. In other words, there is only one Posix pid using
65  *     any given owner, so that owner is serialized. (If you change
66  *     the granularity of the OpenOwner, then code must be added to
67  *     serialize Ops on the OpenOwner.)
68  * - When to get rid of OpenOwners and LockOwners.
69  *   - The function nfscl_cleanup_common() is executed after a process exits.
70  *     It goes through the client list looking for all Open and Lock Owners.
71  *     When one is found, it is marked "defunct" or in the case of
72  *     an OpenOwner without any Opens, freed.
73  *     The renew thread scans for defunct Owners and gets rid of them,
74  *     if it can. The LockOwners will also be deleted when the
75  *     associated Open is closed.
76  *   - If the LockU or Close Op(s) fail during close in a way
77  *     that could be recovered upon retry, they are relinked to the
78  *     ClientId's defunct open list and retried by the renew thread
79  *     until they succeed or an unmount/recovery occurs.
80  *     (Since we are done with them, they do not need to be recovered.)
81  */
82
83 #include <fs/nfs/nfsport.h>
84
85 /*
86  * Global variables
87  */
88 extern struct nfsstatsv1 nfsstatsv1;
89 extern struct nfsreqhead nfsd_reqq;
90 extern u_int32_t newnfs_false, newnfs_true;
91 extern int nfscl_debuglevel;
92 extern int nfscl_enablecallb;
93 extern int nfs_numnfscbd;
94 NFSREQSPINLOCK;
95 NFSCLSTATEMUTEX;
96 int nfscl_inited = 0;
97 struct nfsclhead nfsclhead;     /* Head of clientid list */
98 int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
99 int nfscl_layouthighwater = NFSCLLAYOUTHIGHWATER;
100
101 static int nfscl_delegcnt = 0;
102 static int nfscl_layoutcnt = 0;
103 static int nfscl_getopen(struct nfsclownerhead *, struct nfsclopenhash *,
104     u_int8_t *, int, u_int8_t *, u_int8_t *, u_int32_t,
105     struct nfscllockowner **, struct nfsclopen **);
106 static bool nfscl_checkown(struct nfsclowner *, struct nfsclopen *, uint8_t *,
107     uint8_t *, struct nfscllockowner **, struct nfsclopen **,
108     struct nfsclopen **);
109 static void nfscl_clrelease(struct nfsclclient *);
110 static void nfscl_cleanclient(struct nfsclclient *);
111 static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
112     struct ucred *, NFSPROC_T *);
113 static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
114     struct nfsmount *, struct ucred *, NFSPROC_T *);
115 static void nfscl_recover(struct nfsclclient *, bool *, struct ucred *,
116     NFSPROC_T *);
117 static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
118     struct nfscllock *, int);
119 static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
120     struct nfscllock **, int);
121 static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *);
122 static u_int32_t nfscl_nextcbident(void);
123 static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
124 static struct nfsclclient *nfscl_getclnt(u_int32_t);
125 static struct nfsclclient *nfscl_getclntsess(uint8_t *);
126 static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
127     int);
128 static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
129     int, struct nfsclrecalllayout **);
130 static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
131 static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
132     int);
133 static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
134 static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
135     u_int8_t *, struct nfscllock **);
136 static void nfscl_freealllocks(struct nfscllockownerhead *, int);
137 static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
138     struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
139 static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
140     struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
141     struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *);
142 static int nfscl_moveopen(vnode_t , struct nfsclclient *,
143     struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
144     struct nfscldeleg *, struct ucred *, NFSPROC_T *);
145 static void nfscl_totalrecall(struct nfsclclient *);
146 static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
147     struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
148 static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
149     u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
150     struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
151 static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
152     int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
153     struct ucred *, NFSPROC_T *);
154 static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
155     struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
156 static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *,
157     bool);
158 static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
159 static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
160 static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
161     struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int,
162     vnode_t *);
163 static void nfscl_freeopenowner(struct nfsclowner *, int);
164 static void nfscl_cleandeleg(struct nfscldeleg *);
165 static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
166     struct nfsmount *, NFSPROC_T *);
167 static void nfscl_emptylockowner(struct nfscllockowner *,
168     struct nfscllockownerfhhead *);
169 static void nfscl_mergeflayouts(struct nfsclflayouthead *,
170     struct nfsclflayouthead *);
171 static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t,
172     uint64_t, uint32_t, uint32_t, uint32_t, char *, struct nfsclrecalllayout *);
173 static int nfscl_seq(uint32_t, uint32_t);
174 static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *,
175     struct ucred *, NFSPROC_T *);
176 static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *,
177     struct ucred *, NFSPROC_T *);
178
179 static short nfscberr_null[] = {
180         0,
181         0,
182 };
183
184 static short nfscberr_getattr[] = {
185         NFSERR_RESOURCE,
186         NFSERR_BADHANDLE,
187         NFSERR_BADXDR,
188         NFSERR_RESOURCE,
189         NFSERR_SERVERFAULT,
190         0,
191 };
192
193 static short nfscberr_recall[] = {
194         NFSERR_RESOURCE,
195         NFSERR_BADHANDLE,
196         NFSERR_BADSTATEID,
197         NFSERR_BADXDR,
198         NFSERR_RESOURCE,
199         NFSERR_SERVERFAULT,
200         0,
201 };
202
203 static short *nfscl_cberrmap[] = {
204         nfscberr_null,
205         nfscberr_null,
206         nfscberr_null,
207         nfscberr_getattr,
208         nfscberr_recall
209 };
210
211 #define NETFAMILY(clp) \
212                 (((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)
213
214 /*
215  * Called for an open operation.
216  * If the nfhp argument is NULL, just get an openowner.
217  */
218 int
219 nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
220     struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
221     struct nfsclopen **opp, int *newonep, int *retp, int lockit)
222 {
223         struct nfsclclient *clp;
224         struct nfsclowner *owp, *nowp;
225         struct nfsclopen *op = NULL, *nop = NULL;
226         struct nfscldeleg *dp;
227         struct nfsclownerhead *ohp;
228         u_int8_t own[NFSV4CL_LOCKNAMELEN];
229         int ret;
230
231         if (newonep != NULL)
232                 *newonep = 0;
233         if (opp != NULL)
234                 *opp = NULL;
235         if (owpp != NULL)
236                 *owpp = NULL;
237
238         /*
239          * Might need one or both of these, so MALLOC them now, to
240          * avoid a tsleep() in MALLOC later.
241          */
242         nowp = malloc(sizeof (struct nfsclowner),
243             M_NFSCLOWNER, M_WAITOK);
244         if (nfhp != NULL) {
245             nop = malloc(sizeof (struct nfsclopen) +
246                 fhlen - 1, M_NFSCLOPEN, M_WAITOK);
247             nop->nfso_hash.le_prev = NULL;
248         }
249         ret = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
250         if (ret != 0) {
251                 free(nowp, M_NFSCLOWNER);
252                 if (nop != NULL)
253                         free(nop, M_NFSCLOPEN);
254                 return (ret);
255         }
256
257         /*
258          * Get the Open iff it already exists.
259          * If none found, add the new one or return error, depending upon
260          * "create".
261          */
262         NFSLOCKCLSTATE();
263         dp = NULL;
264         /* First check the delegation list */
265         if (nfhp != NULL && usedeleg) {
266                 LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
267                         if (dp->nfsdl_fhlen == fhlen &&
268                             !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
269                                 if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
270                                     (dp->nfsdl_flags & NFSCLDL_WRITE))
271                                         break;
272                                 dp = NULL;
273                                 break;
274                         }
275                 }
276         }
277
278         /* For NFSv4.1/4.2 and this option, use a single open_owner. */
279         if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
280                 nfscl_filllockowner(NULL, own, F_POSIX);
281         else
282                 nfscl_filllockowner(p->td_proc, own, F_POSIX);
283         if (dp != NULL)
284                 ohp = &dp->nfsdl_owner;
285         else
286                 ohp = &clp->nfsc_owner;
287         /* Now, search for an openowner */
288         LIST_FOREACH(owp, ohp, nfsow_list) {
289                 if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
290                         break;
291         }
292
293         /*
294          * Create a new open, as required.
295          */
296         nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
297             cred, newonep);
298
299         /*
300          * Now, check the mode on the open and return the appropriate
301          * value.
302          */
303         if (retp != NULL) {
304                 if (nfhp != NULL && dp != NULL && nop == NULL)
305                         /* new local open on delegation */
306                         *retp = NFSCLOPEN_SETCRED;
307                 else
308                         *retp = NFSCLOPEN_OK;
309         }
310         if (op != NULL && (amode & ~(op->nfso_mode))) {
311                 op->nfso_mode |= amode;
312                 if (retp != NULL && dp == NULL)
313                         *retp = NFSCLOPEN_DOOPEN;
314         }
315
316         /*
317          * Serialize modifications to the open owner for multiple threads
318          * within the same process using a read/write sleep lock.
319          * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
320          * by acquiring a shared lock.  The close operations still use an
321          * exclusive lock for this case.
322          */
323         if (lockit != 0) {
324                 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount))) {
325                         /*
326                          * Get a shared lock on the OpenOwner, but first
327                          * wait for any pending exclusive lock, so that the
328                          * exclusive locker gets priority.
329                          */
330                         nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
331                             NFSCLSTATEMUTEXPTR, NULL);
332                         nfsv4_getref(&owp->nfsow_rwlock, NULL,
333                             NFSCLSTATEMUTEXPTR, NULL);
334                 } else
335                         nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
336         }
337         NFSUNLOCKCLSTATE();
338         if (nowp != NULL)
339                 free(nowp, M_NFSCLOWNER);
340         if (nop != NULL)
341                 free(nop, M_NFSCLOPEN);
342         if (owpp != NULL)
343                 *owpp = owp;
344         if (opp != NULL)
345                 *opp = op;
346         return (0);
347 }
348
349 /*
350  * Create a new open, as required.
351  */
352 static void
353 nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
354     struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
355     struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
356     struct ucred *cred, int *newonep)
357 {
358         struct nfsclowner *owp = *owpp, *nowp;
359         struct nfsclopen *op, *nop;
360
361         if (nowpp != NULL)
362                 nowp = *nowpp;
363         else
364                 nowp = NULL;
365         if (nopp != NULL)
366                 nop = *nopp;
367         else
368                 nop = NULL;
369         if (owp == NULL && nowp != NULL) {
370                 NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
371                 LIST_INIT(&nowp->nfsow_open);
372                 nowp->nfsow_clp = clp;
373                 nowp->nfsow_seqid = 0;
374                 nowp->nfsow_defunct = 0;
375                 nfscl_lockinit(&nowp->nfsow_rwlock);
376                 if (dp != NULL) {
377                         nfsstatsv1.cllocalopenowners++;
378                         LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
379                 } else {
380                         nfsstatsv1.clopenowners++;
381                         LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
382                 }
383                 owp = *owpp = nowp;
384                 *nowpp = NULL;
385                 if (newonep != NULL)
386                         *newonep = 1;
387         }
388
389          /* If an fhp has been specified, create an Open as well. */
390         if (fhp != NULL) {
391                 /* and look for the correct open, based upon FH */
392                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
393                         if (op->nfso_fhlen == fhlen &&
394                             !NFSBCMP(op->nfso_fh, fhp, fhlen))
395                                 break;
396                 }
397                 if (op == NULL && nop != NULL) {
398                         nop->nfso_own = owp;
399                         nop->nfso_mode = 0;
400                         nop->nfso_opencnt = 0;
401                         nop->nfso_posixlock = 1;
402                         nop->nfso_fhlen = fhlen;
403                         NFSBCOPY(fhp, nop->nfso_fh, fhlen);
404                         LIST_INIT(&nop->nfso_lock);
405                         nop->nfso_stateid.seqid = 0;
406                         nop->nfso_stateid.other[0] = 0;
407                         nop->nfso_stateid.other[1] = 0;
408                         nop->nfso_stateid.other[2] = 0;
409                         KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
410                         newnfs_copyincred(cred, &nop->nfso_cred);
411                         if (dp != NULL) {
412                                 TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
413                                 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
414                                     nfsdl_list);
415                                 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
416                                 nfsstatsv1.cllocalopens++;
417                         } else {
418                                 LIST_INSERT_HEAD(NFSCLOPENHASH(clp, fhp, fhlen),
419                                     nop, nfso_hash);
420                                 nfsstatsv1.clopens++;
421                         }
422                         LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
423                         *opp = nop;
424                         *nopp = NULL;
425                         if (newonep != NULL)
426                                 *newonep = 1;
427                 } else {
428                         *opp = op;
429                 }
430         }
431 }
432
433 /*
434  * Called to find/add a delegation to a client.
435  */
436 int
437 nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
438     int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
439 {
440         struct nfscldeleg *dp = *dpp, *tdp;
441
442         /*
443          * First, if we have received a Read delegation for a file on a
444          * read/write file system, just return it, because they aren't
445          * useful, imho.
446          */
447         if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
448             (dp->nfsdl_flags & NFSCLDL_READ)) {
449                 (void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
450                 free(dp, M_NFSCLDELEG);
451                 *dpp = NULL;
452                 return (0);
453         }
454
455         /* Look for the correct deleg, based upon FH */
456         NFSLOCKCLSTATE();
457         tdp = nfscl_finddeleg(clp, nfhp, fhlen);
458         if (tdp == NULL) {
459                 if (dp == NULL) {
460                         NFSUNLOCKCLSTATE();
461                         return (NFSERR_BADSTATEID);
462                 }
463                 *dpp = NULL;
464                 TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
465                 LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
466                     nfsdl_hash);
467                 dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
468                 nfsstatsv1.cldelegates++;
469                 nfscl_delegcnt++;
470         } else {
471                 /*
472                  * Delegation already exists, what do we do if a new one??
473                  */
474                 if (dp != NULL) {
475                         printf("Deleg already exists!\n");
476                         free(dp, M_NFSCLDELEG);
477                         *dpp = NULL;
478                 } else {
479                         *dpp = tdp;
480                 }
481         }
482         NFSUNLOCKCLSTATE();
483         return (0);
484 }
485
486 /*
487  * Find a delegation for this file handle. Return NULL upon failure.
488  */
489 static struct nfscldeleg *
490 nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
491 {
492         struct nfscldeleg *dp;
493
494         LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
495             if (dp->nfsdl_fhlen == fhlen &&
496                 !NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
497                 break;
498         }
499         return (dp);
500 }
501
502 /*
503  * Get a stateid for an I/O operation. First, look for an open and iff
504  * found, return either a lockowner stateid or the open stateid.
505  * If no Open is found, just return error and the special stateid of all zeros.
506  */
507 int
508 nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
509     int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
510     void **lckpp)
511 {
512         struct nfsclclient *clp;
513         struct nfsclopen *op = NULL, *top;
514         struct nfsclopenhash *oph;
515         struct nfscllockowner *lp;
516         struct nfscldeleg *dp;
517         struct nfsnode *np;
518         struct nfsmount *nmp;
519         u_int8_t own[NFSV4CL_LOCKNAMELEN];
520         int error;
521         bool done;
522
523         *lckpp = NULL;
524         /*
525          * Initially, just set the special stateid of all zeros.
526          * (Don't do this for a DS, since the special stateid can't be used.)
527          */
528         if (fords == 0) {
529                 stateidp->seqid = 0;
530                 stateidp->other[0] = 0;
531                 stateidp->other[1] = 0;
532                 stateidp->other[2] = 0;
533         }
534         if (vnode_vtype(vp) != VREG)
535                 return (EISDIR);
536         np = VTONFS(vp);
537         nmp = VFSTONFS(vp->v_mount);
538         NFSLOCKCLSTATE();
539         clp = nfscl_findcl(nmp);
540         if (clp == NULL) {
541                 NFSUNLOCKCLSTATE();
542                 return (EACCES);
543         }
544
545         /*
546          * Wait for recovery to complete.
547          */
548         while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
549                 (void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
550                     PZERO, "nfsrecvr", NULL);
551
552         /*
553          * First, look for a delegation.
554          */
555         LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
556                 if (dp->nfsdl_fhlen == fhlen &&
557                     !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
558                         if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
559                             (dp->nfsdl_flags & NFSCLDL_WRITE)) {
560                                 stateidp->seqid = dp->nfsdl_stateid.seqid;
561                                 stateidp->other[0] = dp->nfsdl_stateid.other[0];
562                                 stateidp->other[1] = dp->nfsdl_stateid.other[1];
563                                 stateidp->other[2] = dp->nfsdl_stateid.other[2];
564                                 if (!(np->n_flag & NDELEGRECALL)) {
565                                         TAILQ_REMOVE(&clp->nfsc_deleg, dp,
566                                             nfsdl_list);
567                                         TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
568                                             nfsdl_list);
569                                         dp->nfsdl_timestamp = NFSD_MONOSEC +
570                                             120;
571                                         dp->nfsdl_rwlock.nfslock_usecnt++;
572                                         *lckpp = (void *)&dp->nfsdl_rwlock;
573                                 }
574                                 NFSUNLOCKCLSTATE();
575                                 return (0);
576                         }
577                         break;
578                 }
579         }
580
581         if (p != NULL) {
582                 /*
583                  * If p != NULL, we want to search the parentage tree
584                  * for a matching OpenOwner and use that.
585                  */
586                 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
587                         nfscl_filllockowner(NULL, own, F_POSIX);
588                 else
589                         nfscl_filllockowner(p->td_proc, own, F_POSIX);
590                 lp = NULL;
591                 error = nfscl_getopen(NULL, clp->nfsc_openhash, nfhp, fhlen,
592                     own, own, mode, &lp, &op);
593                 if (error == 0 && lp != NULL && fords == 0) {
594                         /* Don't return a lock stateid for a DS. */
595                         stateidp->seqid =
596                             lp->nfsl_stateid.seqid;
597                         stateidp->other[0] =
598                             lp->nfsl_stateid.other[0];
599                         stateidp->other[1] =
600                             lp->nfsl_stateid.other[1];
601                         stateidp->other[2] =
602                             lp->nfsl_stateid.other[2];
603                         NFSUNLOCKCLSTATE();
604                         return (0);
605                 }
606         }
607         if (op == NULL) {
608                 /* If not found, just look for any OpenOwner that will work. */
609                 top = NULL;
610                 done = false;
611                 oph = NFSCLOPENHASH(clp, nfhp, fhlen);
612                 LIST_FOREACH(op, oph, nfso_hash) {
613                         if (op->nfso_fhlen == fhlen &&
614                             !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
615                                 if (top == NULL && (op->nfso_mode &
616                                     NFSV4OPEN_ACCESSWRITE) != 0 &&
617                                     (mode & NFSV4OPEN_ACCESSREAD) != 0)
618                                         top = op;
619                                 if ((mode & op->nfso_mode) == mode) {
620                                         /* LRU order the hash list. */
621                                         LIST_REMOVE(op, nfso_hash);
622                                         LIST_INSERT_HEAD(oph, op, nfso_hash);
623                                         done = true;
624                                         break;
625                                 }
626                         }
627                 }
628                 if (!done) {
629                         NFSCL_DEBUG(2, "openmode top=%p\n", top);
630                         if (top == NULL || NFSHASOPENMODE(nmp)) {
631                                 NFSUNLOCKCLSTATE();
632                                 return (ENOENT);
633                         } else
634                                 op = top;
635                 }
636                 /*
637                  * For read aheads or write behinds, use the open cred.
638                  * A read ahead or write behind is indicated by p == NULL.
639                  */
640                 if (p == NULL)
641                         newnfs_copycred(&op->nfso_cred, cred);
642         }
643
644         /*
645          * No lock stateid, so return the open stateid.
646          */
647         stateidp->seqid = op->nfso_stateid.seqid;
648         stateidp->other[0] = op->nfso_stateid.other[0];
649         stateidp->other[1] = op->nfso_stateid.other[1];
650         stateidp->other[2] = op->nfso_stateid.other[2];
651         NFSUNLOCKCLSTATE();
652         return (0);
653 }
654
655 /*
656  * Search for a matching file, mode and, optionally, lockowner.
657  */
658 static int
659 nfscl_getopen(struct nfsclownerhead *ohp, struct nfsclopenhash *ohashp,
660     u_int8_t *nfhp, int fhlen, u_int8_t *openown, u_int8_t *lockown,
661     u_int32_t mode, struct nfscllockowner **lpp, struct nfsclopen **opp)
662 {
663         struct nfsclowner *owp;
664         struct nfsclopen *op, *rop, *rop2;
665         struct nfsclopenhash *oph;
666         bool keep_looping;
667
668         KASSERT(ohp == NULL || ohashp == NULL, ("nfscl_getopen: "
669             "only one of ohp and ohashp can be set"));
670         if (lpp != NULL)
671                 *lpp = NULL;
672         /*
673          * rop will be set to the open to be returned. There are three
674          * variants of this, all for an open of the correct file:
675          * 1 - A match of lockown.
676          * 2 - A match of the openown, when no lockown match exists.
677          * 3 - A match for any open, if no openown or lockown match exists.
678          * Looking for #2 over #3 probably isn't necessary, but since
679          * RFC3530 is vague w.r.t. the relationship between openowners and
680          * lockowners, I think this is the safer way to go.
681          */
682         rop = NULL;
683         rop2 = NULL;
684         keep_looping = true;
685         /* Search the client list */
686         if (ohashp == NULL) {
687                 /* Search the local opens on the delegation. */
688                 LIST_FOREACH(owp, ohp, nfsow_list) {
689                         /* and look for the correct open */
690                         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
691                                 if (op->nfso_fhlen == fhlen &&
692                                     !NFSBCMP(op->nfso_fh, nfhp, fhlen)
693                                     && (op->nfso_mode & mode) == mode)
694                                         keep_looping = nfscl_checkown(owp, op, openown,
695                                             lockown, lpp, &rop, &rop2);
696                                 if (!keep_looping)
697                                         break;
698                         }
699                         if (!keep_looping)
700                                 break;
701                 }
702         } else {
703                 /* Search for matching opens on the hash list. */
704                 oph = &ohashp[NFSCLOPENHASHFUNC(nfhp, fhlen)];
705                 LIST_FOREACH(op, oph, nfso_hash) {
706                         if (op->nfso_fhlen == fhlen &&
707                             !NFSBCMP(op->nfso_fh, nfhp, fhlen)
708                             && (op->nfso_mode & mode) == mode)
709                                 keep_looping = nfscl_checkown(op->nfso_own, op,
710                                     openown, lockown, lpp, &rop, &rop2);
711                         if (!keep_looping) {
712                                 /* LRU order the hash list. */
713                                 LIST_REMOVE(op, nfso_hash);
714                                 LIST_INSERT_HEAD(oph, op, nfso_hash);
715                                 break;
716                         }
717                 }
718         }
719         if (rop == NULL)
720                 rop = rop2;
721         if (rop == NULL)
722                 return (EBADF);
723         *opp = rop;
724         return (0);
725 }
726
727 /* Check for an owner match. */
728 static bool
729 nfscl_checkown(struct nfsclowner *owp, struct nfsclopen *op, uint8_t *openown,
730     uint8_t *lockown, struct nfscllockowner **lpp, struct nfsclopen **ropp,
731     struct nfsclopen **ropp2)
732 {
733         struct nfscllockowner *lp;
734         bool keep_looping;
735
736         keep_looping = true;
737         if (lpp != NULL) {
738                 /* Now look for a matching lockowner. */
739                 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
740                         if (!NFSBCMP(lp->nfsl_owner, lockown,
741                             NFSV4CL_LOCKNAMELEN)) {
742                                 *lpp = lp;
743                                 *ropp = op;
744                                 return (false);
745                         }
746                 }
747         }
748         if (*ropp == NULL && !NFSBCMP(owp->nfsow_owner, openown,
749             NFSV4CL_LOCKNAMELEN)) {
750                 *ropp = op;
751                 if (lpp == NULL)
752                         keep_looping = false;
753         }
754         if (*ropp2 == NULL)
755                 *ropp2 = op;
756         return (keep_looping);
757 }
758
759 /*
760  * Release use of an open owner. Called when open operations are done
761  * with the open owner.
762  */
763 void
764 nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
765     __unused int error, __unused int candelete, int unlocked)
766 {
767
768         if (owp == NULL)
769                 return;
770         NFSLOCKCLSTATE();
771         if (unlocked == 0) {
772                 if (NFSHASONEOPENOWN(nmp))
773                         nfsv4_relref(&owp->nfsow_rwlock);
774                 else
775                         nfscl_lockunlock(&owp->nfsow_rwlock);
776         }
777         nfscl_clrelease(owp->nfsow_clp);
778         NFSUNLOCKCLSTATE();
779 }
780
781 /*
782  * Release use of an open structure under an open owner.
783  */
784 void
785 nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
786     int candelete)
787 {
788         struct nfsclclient *clp;
789         struct nfsclowner *owp;
790
791         if (op == NULL)
792                 return;
793         NFSLOCKCLSTATE();
794         owp = op->nfso_own;
795         if (NFSHASONEOPENOWN(nmp))
796                 nfsv4_relref(&owp->nfsow_rwlock);
797         else
798                 nfscl_lockunlock(&owp->nfsow_rwlock);
799         clp = owp->nfsow_clp;
800         if (error && candelete && op->nfso_opencnt == 0)
801                 nfscl_freeopen(op, 0);
802         nfscl_clrelease(clp);
803         NFSUNLOCKCLSTATE();
804 }
805
806 /*
807  * Called to get a clientid structure. It will optionally lock the
808  * client data structures to do the SetClientId/SetClientId_confirm,
809  * but will release that lock and return the clientid with a reference
810  * count on it.
811  * If the "cred" argument is NULL, a new clientid should not be created.
812  * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
813  * be done.
814  * The start_renewthread argument tells nfscl_getcl() to start a renew
815  * thread if this creates a new clp.
816  * It always clpp with a reference count on it, unless returning an error.
817  */
818 int
819 nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
820     int start_renewthread, struct nfsclclient **clpp)
821 {
822         struct nfsclclient *clp;
823         struct nfsclclient *newclp = NULL;
824         struct nfsmount *nmp;
825         char uuid[HOSTUUIDLEN];
826         int igotlock = 0, error, trystalecnt, clidinusedelay, i;
827         u_int16_t idlen = 0;
828
829         nmp = VFSTONFS(mp);
830         if (cred != NULL) {
831                 getcredhostuuid(cred, uuid, sizeof uuid);
832                 idlen = strlen(uuid);
833                 if (idlen > 0)
834                         idlen += sizeof (u_int64_t);
835                 else
836                         idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
837                 newclp = malloc(
838                     sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
839                     M_WAITOK | M_ZERO);
840         }
841         NFSLOCKCLSTATE();
842         /*
843          * If a forced dismount is already in progress, don't
844          * allocate a new clientid and get out now. For the case where
845          * clp != NULL, this is a harmless optimization.
846          */
847         if (NFSCL_FORCEDISM(mp)) {
848                 NFSUNLOCKCLSTATE();
849                 if (newclp != NULL)
850                         free(newclp, M_NFSCLCLIENT);
851                 return (EBADF);
852         }
853         clp = nmp->nm_clp;
854         if (clp == NULL) {
855                 if (newclp == NULL) {
856                         NFSUNLOCKCLSTATE();
857                         return (EACCES);
858                 }
859                 clp = newclp;
860                 clp->nfsc_idlen = idlen;
861                 LIST_INIT(&clp->nfsc_owner);
862                 TAILQ_INIT(&clp->nfsc_deleg);
863                 TAILQ_INIT(&clp->nfsc_layout);
864                 LIST_INIT(&clp->nfsc_devinfo);
865                 for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
866                         LIST_INIT(&clp->nfsc_deleghash[i]);
867                 for (i = 0; i < NFSCLOPENHASHSIZE; i++)
868                         LIST_INIT(&clp->nfsc_openhash[i]);
869                 for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
870                         LIST_INIT(&clp->nfsc_layouthash[i]);
871                 clp->nfsc_flags = NFSCLFLAGS_INITED;
872                 clp->nfsc_clientidrev = 1;
873                 clp->nfsc_cbident = nfscl_nextcbident();
874                 nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
875                     clp->nfsc_idlen);
876                 LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
877                 nmp->nm_clp = clp;
878                 clp->nfsc_nmp = nmp;
879                 NFSUNLOCKCLSTATE();
880                 if (start_renewthread != 0)
881                         nfscl_start_renewthread(clp);
882         } else {
883                 NFSUNLOCKCLSTATE();
884                 if (newclp != NULL)
885                         free(newclp, M_NFSCLCLIENT);
886         }
887         NFSLOCKCLSTATE();
888         while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
889             !NFSCL_FORCEDISM(mp))
890                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
891                     NFSCLSTATEMUTEXPTR, mp);
892         if (igotlock == 0) {
893                 /*
894                  * Call nfsv4_lock() with "iwantlock == 0" so that it will
895                  * wait for a pending exclusive lock request.  This gives the
896                  * exclusive lock request priority over this shared lock
897                  * request.
898                  * An exclusive lock on nfsc_lock is used mainly for server
899                  * crash recoveries.
900                  */
901                 nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, mp);
902                 nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
903         }
904         if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
905                 /*
906                  * Both nfsv4_lock() and nfsv4_getref() know to check
907                  * for NFSCL_FORCEDISM() and return without sleeping to
908                  * wait for the exclusive lock to be released, since it
909                  * might be held by nfscl_umount() and we need to get out
910                  * now for that case and not wait until nfscl_umount()
911                  * releases it.
912                  */
913                 NFSUNLOCKCLSTATE();
914                 return (EBADF);
915         }
916         NFSUNLOCKCLSTATE();
917
918         /*
919          * If it needs a clientid, do the setclientid now.
920          */
921         if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
922                 if (!igotlock)
923                         panic("nfscl_clget");
924                 if (p == NULL || cred == NULL) {
925                         NFSLOCKCLSTATE();
926                         nfsv4_unlock(&clp->nfsc_lock, 0);
927                         NFSUNLOCKCLSTATE();
928                         return (EACCES);
929                 }
930                 /*
931                  * If RFC3530 Sec. 14.2.33 is taken literally,
932                  * NFSERR_CLIDINUSE will be returned persistently for the
933                  * case where a new mount of the same file system is using
934                  * a different principal. In practice, NFSERR_CLIDINUSE is
935                  * only returned when there is outstanding unexpired state
936                  * on the clientid. As such, try for twice the lease
937                  * interval, if we know what that is. Otherwise, make a
938                  * wild ass guess.
939                  * The case of returning NFSERR_STALECLIENTID is far less
940                  * likely, but might occur if there is a significant delay
941                  * between doing the SetClientID and SetClientIDConfirm Ops,
942                  * such that the server throws away the clientid before
943                  * receiving the SetClientIDConfirm.
944                  */
945                 if (clp->nfsc_renew > 0)
946                         clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
947                 else
948                         clidinusedelay = 120;
949                 trystalecnt = 3;
950                 do {
951                         error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
952                         if (error == NFSERR_STALECLIENTID ||
953                             error == NFSERR_STALEDONTRECOVER ||
954                             error == NFSERR_BADSESSION ||
955                             error == NFSERR_CLIDINUSE) {
956                                 (void) nfs_catnap(PZERO, error, "nfs_setcl");
957                         }
958                 } while (((error == NFSERR_STALECLIENTID ||
959                      error == NFSERR_BADSESSION ||
960                      error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
961                     (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
962                 if (error) {
963                         NFSLOCKCLSTATE();
964                         nfsv4_unlock(&clp->nfsc_lock, 0);
965                         NFSUNLOCKCLSTATE();
966                         return (error);
967                 }
968                 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
969         }
970         if (igotlock) {
971                 NFSLOCKCLSTATE();
972                 nfsv4_unlock(&clp->nfsc_lock, 1);
973                 NFSUNLOCKCLSTATE();
974         }
975
976         *clpp = clp;
977         return (0);
978 }
979
980 /*
981  * Get a reference to a clientid and return it, if valid.
982  */
983 struct nfsclclient *
984 nfscl_findcl(struct nfsmount *nmp)
985 {
986         struct nfsclclient *clp;
987
988         clp = nmp->nm_clp;
989         if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
990                 return (NULL);
991         return (clp);
992 }
993
994 /*
995  * Release the clientid structure. It may be locked or reference counted.
996  */
997 static void
998 nfscl_clrelease(struct nfsclclient *clp)
999 {
1000
1001         if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
1002                 nfsv4_unlock(&clp->nfsc_lock, 0);
1003         else
1004                 nfsv4_relref(&clp->nfsc_lock);
1005 }
1006
1007 /*
1008  * External call for nfscl_clrelease.
1009  */
1010 void
1011 nfscl_clientrelease(struct nfsclclient *clp)
1012 {
1013
1014         NFSLOCKCLSTATE();
1015         if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
1016                 nfsv4_unlock(&clp->nfsc_lock, 0);
1017         else
1018                 nfsv4_relref(&clp->nfsc_lock);
1019         NFSUNLOCKCLSTATE();
1020 }
1021
1022 /*
1023  * Called when wanting to lock a byte region.
1024  */
1025 int
1026 nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1027     short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
1028     int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
1029     struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
1030 {
1031         struct nfscllockowner *lp;
1032         struct nfsclopen *op;
1033         struct nfsclclient *clp;
1034         struct nfscllockowner *nlp;
1035         struct nfscllock *nlop, *otherlop;
1036         struct nfscldeleg *dp = NULL, *ldp = NULL;
1037         struct nfscllockownerhead *lhp = NULL;
1038         struct nfsnode *np;
1039         u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
1040         u_int8_t *openownp;
1041         int error = 0, ret, donelocally = 0;
1042         u_int32_t mode;
1043
1044         /* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
1045         mode = 0;
1046         np = VTONFS(vp);
1047         *lpp = NULL;
1048         lp = NULL;
1049         *newonep = 0;
1050         *donelocallyp = 0;
1051
1052         /*
1053          * Might need these, so MALLOC them now, to
1054          * avoid a tsleep() in MALLOC later.
1055          */
1056         nlp = malloc(
1057             sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
1058         otherlop = malloc(
1059             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1060         nlop = malloc(
1061             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1062         nlop->nfslo_type = type;
1063         nlop->nfslo_first = off;
1064         if (len == NFS64BITSSET) {
1065                 nlop->nfslo_end = NFS64BITSSET;
1066         } else {
1067                 nlop->nfslo_end = off + len;
1068                 if (nlop->nfslo_end <= nlop->nfslo_first)
1069                         error = NFSERR_INVAL;
1070         }
1071
1072         if (!error) {
1073                 if (recovery)
1074                         clp = rclp;
1075                 else
1076                         error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
1077         }
1078         if (error) {
1079                 free(nlp, M_NFSCLLOCKOWNER);
1080                 free(otherlop, M_NFSCLLOCK);
1081                 free(nlop, M_NFSCLLOCK);
1082                 return (error);
1083         }
1084
1085         op = NULL;
1086         if (recovery) {
1087                 ownp = rownp;
1088                 openownp = ropenownp;
1089         } else {
1090                 nfscl_filllockowner(id, own, flags);
1091                 ownp = own;
1092                 if (NFSHASONEOPENOWN(VFSTONFS(vp->v_mount)))
1093                         nfscl_filllockowner(NULL, openown, F_POSIX);
1094                 else
1095                         nfscl_filllockowner(p->td_proc, openown, F_POSIX);
1096                 openownp = openown;
1097         }
1098         if (!recovery) {
1099                 NFSLOCKCLSTATE();
1100                 /*
1101                  * First, search for a delegation. If one exists for this file,
1102                  * the lock can be done locally against it, so long as there
1103                  * isn't a local lock conflict.
1104                  */
1105                 ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1106                     np->n_fhp->nfh_len);
1107                 /* Just sanity check for correct type of delegation */
1108                 if (dp != NULL && ((dp->nfsdl_flags &
1109                     (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
1110                      (type == F_WRLCK &&
1111                       (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
1112                         dp = NULL;
1113         }
1114         if (dp != NULL) {
1115                 /* Now, find an open and maybe a lockowner. */
1116                 ret = nfscl_getopen(&dp->nfsdl_owner, NULL, np->n_fhp->nfh_fh,
1117                     np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
1118                 if (ret)
1119                         ret = nfscl_getopen(NULL, clp->nfsc_openhash,
1120                             np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1121                             ownp, mode, NULL, &op);
1122                 if (!ret) {
1123                         lhp = &dp->nfsdl_lock;
1124                         TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
1125                         TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
1126                         dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
1127                         donelocally = 1;
1128                 } else {
1129                         dp = NULL;
1130                 }
1131         }
1132         if (!donelocally) {
1133                 /*
1134                  * Get the related Open and maybe lockowner.
1135                  */
1136                 error = nfscl_getopen(NULL, clp->nfsc_openhash,
1137                     np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
1138                     ownp, mode, &lp, &op);
1139                 if (!error)
1140                         lhp = &op->nfso_lock;
1141         }
1142         if (!error && !recovery)
1143                 error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
1144                     np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
1145         if (error) {
1146                 if (!recovery) {
1147                         nfscl_clrelease(clp);
1148                         NFSUNLOCKCLSTATE();
1149                 }
1150                 free(nlp, M_NFSCLLOCKOWNER);
1151                 free(otherlop, M_NFSCLLOCK);
1152                 free(nlop, M_NFSCLLOCK);
1153                 return (error);
1154         }
1155
1156         /*
1157          * Ok, see if a lockowner exists and create one, as required.
1158          */
1159         if (lp == NULL)
1160                 LIST_FOREACH(lp, lhp, nfsl_list) {
1161                         if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
1162                                 break;
1163                 }
1164         if (lp == NULL) {
1165                 NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
1166                 if (recovery)
1167                         NFSBCOPY(ropenownp, nlp->nfsl_openowner,
1168                             NFSV4CL_LOCKNAMELEN);
1169                 else
1170                         NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
1171                             NFSV4CL_LOCKNAMELEN);
1172                 nlp->nfsl_seqid = 0;
1173                 nlp->nfsl_lockflags = flags;
1174                 nlp->nfsl_inprog = NULL;
1175                 nfscl_lockinit(&nlp->nfsl_rwlock);
1176                 LIST_INIT(&nlp->nfsl_lock);
1177                 if (donelocally) {
1178                         nlp->nfsl_open = NULL;
1179                         nfsstatsv1.cllocallockowners++;
1180                 } else {
1181                         nlp->nfsl_open = op;
1182                         nfsstatsv1.cllockowners++;
1183                 }
1184                 LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
1185                 lp = nlp;
1186                 nlp = NULL;
1187                 *newonep = 1;
1188         }
1189
1190         /*
1191          * Now, update the byte ranges for locks.
1192          */
1193         ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
1194         if (!ret)
1195                 donelocally = 1;
1196         if (donelocally) {
1197                 *donelocallyp = 1;
1198                 if (!recovery)
1199                         nfscl_clrelease(clp);
1200         } else {
1201                 /*
1202                  * Serial modifications on the lock owner for multiple threads
1203                  * for the same process using a read/write lock.
1204                  */
1205                 if (!recovery)
1206                         nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1207         }
1208         if (!recovery)
1209                 NFSUNLOCKCLSTATE();
1210
1211         if (nlp)
1212                 free(nlp, M_NFSCLLOCKOWNER);
1213         if (nlop)
1214                 free(nlop, M_NFSCLLOCK);
1215         if (otherlop)
1216                 free(otherlop, M_NFSCLLOCK);
1217
1218         *lpp = lp;
1219         return (0);
1220 }
1221
1222 /*
1223  * Called to unlock a byte range, for LockU.
1224  */
1225 int
1226 nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
1227     __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
1228     struct nfsclclient *clp, void *id, int flags,
1229     struct nfscllockowner **lpp, int *dorpcp)
1230 {
1231         struct nfscllockowner *lp;
1232         struct nfsclowner *owp;
1233         struct nfsclopen *op;
1234         struct nfscllock *nlop, *other_lop = NULL;
1235         struct nfscldeleg *dp;
1236         struct nfsnode *np;
1237         u_int8_t own[NFSV4CL_LOCKNAMELEN];
1238         int ret = 0, fnd;
1239
1240         np = VTONFS(vp);
1241         *lpp = NULL;
1242         *dorpcp = 0;
1243
1244         /*
1245          * Might need these, so MALLOC them now, to
1246          * avoid a tsleep() in MALLOC later.
1247          */
1248         nlop = malloc(
1249             sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1250         nlop->nfslo_type = F_UNLCK;
1251         nlop->nfslo_first = off;
1252         if (len == NFS64BITSSET) {
1253                 nlop->nfslo_end = NFS64BITSSET;
1254         } else {
1255                 nlop->nfslo_end = off + len;
1256                 if (nlop->nfslo_end <= nlop->nfslo_first) {
1257                         free(nlop, M_NFSCLLOCK);
1258                         return (NFSERR_INVAL);
1259                 }
1260         }
1261         if (callcnt == 0) {
1262                 other_lop = malloc(
1263                     sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
1264                 *other_lop = *nlop;
1265         }
1266         nfscl_filllockowner(id, own, flags);
1267         dp = NULL;
1268         NFSLOCKCLSTATE();
1269         if (callcnt == 0)
1270                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
1271                     np->n_fhp->nfh_len);
1272
1273         /*
1274          * First, unlock any local regions on a delegation.
1275          */
1276         if (dp != NULL) {
1277                 /* Look for this lockowner. */
1278                 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1279                         if (!NFSBCMP(lp->nfsl_owner, own,
1280                             NFSV4CL_LOCKNAMELEN))
1281                                 break;
1282                 }
1283                 if (lp != NULL)
1284                         /* Use other_lop, so nlop is still available */
1285                         (void)nfscl_updatelock(lp, &other_lop, NULL, 1);
1286         }
1287
1288         /*
1289          * Now, find a matching open/lockowner that hasn't already been done,
1290          * as marked by nfsl_inprog.
1291          */
1292         lp = NULL;
1293         fnd = 0;
1294         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1295             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1296                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1297                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1298                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1299                         if (lp->nfsl_inprog == NULL &&
1300                             !NFSBCMP(lp->nfsl_owner, own,
1301                              NFSV4CL_LOCKNAMELEN)) {
1302                                 fnd = 1;
1303                                 break;
1304                         }
1305                     }
1306                     if (fnd)
1307                         break;
1308                 }
1309             }
1310             if (fnd)
1311                 break;
1312         }
1313
1314         if (lp != NULL) {
1315                 ret = nfscl_updatelock(lp, &nlop, NULL, 0);
1316                 if (ret)
1317                         *dorpcp = 1;
1318                 /*
1319                  * Serial modifications on the lock owner for multiple
1320                  * threads for the same process using a read/write lock.
1321                  */
1322                 lp->nfsl_inprog = p;
1323                 nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
1324                 *lpp = lp;
1325         }
1326         NFSUNLOCKCLSTATE();
1327         if (nlop)
1328                 free(nlop, M_NFSCLLOCK);
1329         if (other_lop)
1330                 free(other_lop, M_NFSCLLOCK);
1331         return (0);
1332 }
1333
1334 /*
1335  * Release all lockowners marked in progess for this process and file.
1336  */
1337 void
1338 nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
1339     void *id, int flags)
1340 {
1341         struct nfsclowner *owp;
1342         struct nfsclopen *op;
1343         struct nfscllockowner *lp;
1344         struct nfsnode *np;
1345         u_int8_t own[NFSV4CL_LOCKNAMELEN];
1346
1347         np = VTONFS(vp);
1348         nfscl_filllockowner(id, own, flags);
1349         NFSLOCKCLSTATE();
1350         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1351             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1352                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1353                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1354                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1355                         if (lp->nfsl_inprog == p &&
1356                             !NFSBCMP(lp->nfsl_owner, own,
1357                             NFSV4CL_LOCKNAMELEN)) {
1358                             lp->nfsl_inprog = NULL;
1359                             nfscl_lockunlock(&lp->nfsl_rwlock);
1360                         }
1361                     }
1362                 }
1363             }
1364         }
1365         nfscl_clrelease(clp);
1366         NFSUNLOCKCLSTATE();
1367 }
1368
1369 /*
1370  * Called to find out if any bytes within the byte range specified are
1371  * write locked by the calling process. Used to determine if flushing
1372  * is required before a LockU.
1373  * If in doubt, return 1, so the flush will occur.
1374  */
1375 int
1376 nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
1377     struct ucred *cred, NFSPROC_T *p, void *id, int flags)
1378 {
1379         struct nfsclowner *owp;
1380         struct nfscllockowner *lp;
1381         struct nfsclopen *op;
1382         struct nfsclclient *clp;
1383         struct nfscllock *lop;
1384         struct nfscldeleg *dp;
1385         struct nfsnode *np;
1386         u_int64_t off, end;
1387         u_int8_t own[NFSV4CL_LOCKNAMELEN];
1388         int error = 0;
1389
1390         np = VTONFS(vp);
1391         switch (fl->l_whence) {
1392         case SEEK_SET:
1393         case SEEK_CUR:
1394                 /*
1395                  * Caller is responsible for adding any necessary offset
1396                  * when SEEK_CUR is used.
1397                  */
1398                 off = fl->l_start;
1399                 break;
1400         case SEEK_END:
1401                 off = np->n_size + fl->l_start;
1402                 break;
1403         default:
1404                 return (1);
1405         }
1406         if (fl->l_len != 0) {
1407                 end = off + fl->l_len;
1408                 if (end < off)
1409                         return (1);
1410         } else {
1411                 end = NFS64BITSSET;
1412         }
1413
1414         error = nfscl_getcl(vp->v_mount, cred, p, 1, &clp);
1415         if (error)
1416                 return (1);
1417         nfscl_filllockowner(id, own, flags);
1418         NFSLOCKCLSTATE();
1419
1420         /*
1421          * First check the delegation locks.
1422          */
1423         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
1424         if (dp != NULL) {
1425                 LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
1426                         if (!NFSBCMP(lp->nfsl_owner, own,
1427                             NFSV4CL_LOCKNAMELEN))
1428                                 break;
1429                 }
1430                 if (lp != NULL) {
1431                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1432                                 if (lop->nfslo_first >= end)
1433                                         break;
1434                                 if (lop->nfslo_end <= off)
1435                                         continue;
1436                                 if (lop->nfslo_type == F_WRLCK) {
1437                                         nfscl_clrelease(clp);
1438                                         NFSUNLOCKCLSTATE();
1439                                         return (1);
1440                                 }
1441                         }
1442                 }
1443         }
1444
1445         /*
1446          * Now, check state against the server.
1447          */
1448         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
1449             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1450                 if (op->nfso_fhlen == np->n_fhp->nfh_len &&
1451                     !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
1452                     LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1453                         if (!NFSBCMP(lp->nfsl_owner, own,
1454                             NFSV4CL_LOCKNAMELEN))
1455                             break;
1456                     }
1457                     if (lp != NULL) {
1458                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
1459                             if (lop->nfslo_first >= end)
1460                                 break;
1461                             if (lop->nfslo_end <= off)
1462                                 continue;
1463                             if (lop->nfslo_type == F_WRLCK) {
1464                                 nfscl_clrelease(clp);
1465                                 NFSUNLOCKCLSTATE();
1466                                 return (1);
1467                             }
1468                         }
1469                     }
1470                 }
1471             }
1472         }
1473         nfscl_clrelease(clp);
1474         NFSUNLOCKCLSTATE();
1475         return (0);
1476 }
1477
1478 /*
1479  * Release a byte range lock owner structure.
1480  */
1481 void
1482 nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
1483 {
1484         struct nfsclclient *clp;
1485
1486         if (lp == NULL)
1487                 return;
1488         NFSLOCKCLSTATE();
1489         clp = lp->nfsl_open->nfso_own->nfsow_clp;
1490         if (error != 0 && candelete &&
1491             (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
1492                 nfscl_freelockowner(lp, 0);
1493         else
1494                 nfscl_lockunlock(&lp->nfsl_rwlock);
1495         nfscl_clrelease(clp);
1496         NFSUNLOCKCLSTATE();
1497 }
1498
1499 /*
1500  * Free up an open structure and any associated byte range lock structures.
1501  */
1502 void
1503 nfscl_freeopen(struct nfsclopen *op, int local)
1504 {
1505
1506         LIST_REMOVE(op, nfso_list);
1507         if (op->nfso_hash.le_prev != NULL)
1508                 LIST_REMOVE(op, nfso_hash);
1509         nfscl_freealllocks(&op->nfso_lock, local);
1510         free(op, M_NFSCLOPEN);
1511         if (local)
1512                 nfsstatsv1.cllocalopens--;
1513         else
1514                 nfsstatsv1.clopens--;
1515 }
1516
1517 /*
1518  * Free up all lock owners and associated locks.
1519  */
1520 static void
1521 nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
1522 {
1523         struct nfscllockowner *lp, *nlp;
1524
1525         LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
1526                 if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1527                         panic("nfscllckw");
1528                 nfscl_freelockowner(lp, local);
1529         }
1530 }
1531
1532 /*
1533  * Called for an Open when NFSERR_EXPIRED is received from the server.
1534  * If there are no byte range locks nor a Share Deny lost, try to do a
1535  * fresh Open. Otherwise, free the open.
1536  */
1537 static int
1538 nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
1539     struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
1540 {
1541         struct nfscllockowner *lp;
1542         struct nfscldeleg *dp;
1543         int mustdelete = 0, error;
1544
1545         /*
1546          * Look for any byte range lock(s).
1547          */
1548         LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
1549                 if (!LIST_EMPTY(&lp->nfsl_lock)) {
1550                         mustdelete = 1;
1551                         break;
1552                 }
1553         }
1554
1555         /*
1556          * If no byte range lock(s) nor a Share deny, try to re-open.
1557          */
1558         if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
1559                 newnfs_copycred(&op->nfso_cred, cred);
1560                 dp = NULL;
1561                 error = nfsrpc_reopen(nmp, op->nfso_fh,
1562                     op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
1563                 if (error) {
1564                         mustdelete = 1;
1565                         if (dp != NULL) {
1566                                 free(dp, M_NFSCLDELEG);
1567                                 dp = NULL;
1568                         }
1569                 }
1570                 if (dp != NULL)
1571                         nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
1572                             op->nfso_fhlen, cred, p, &dp);
1573         }
1574
1575         /*
1576          * If a byte range lock or Share deny or couldn't re-open, free it.
1577          */
1578         if (mustdelete)
1579                 nfscl_freeopen(op, 0);
1580         return (mustdelete);
1581 }
1582
1583 /*
1584  * Free up an open owner structure.
1585  */
1586 static void
1587 nfscl_freeopenowner(struct nfsclowner *owp, int local)
1588 {
1589
1590         LIST_REMOVE(owp, nfsow_list);
1591         free(owp, M_NFSCLOWNER);
1592         if (local)
1593                 nfsstatsv1.cllocalopenowners--;
1594         else
1595                 nfsstatsv1.clopenowners--;
1596 }
1597
1598 /*
1599  * Free up a byte range lock owner structure.
1600  */
1601 void
1602 nfscl_freelockowner(struct nfscllockowner *lp, int local)
1603 {
1604         struct nfscllock *lop, *nlop;
1605
1606         LIST_REMOVE(lp, nfsl_list);
1607         LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
1608                 nfscl_freelock(lop, local);
1609         }
1610         free(lp, M_NFSCLLOCKOWNER);
1611         if (local)
1612                 nfsstatsv1.cllocallockowners--;
1613         else
1614                 nfsstatsv1.cllockowners--;
1615 }
1616
1617 /*
1618  * Free up a byte range lock structure.
1619  */
1620 void
1621 nfscl_freelock(struct nfscllock *lop, int local)
1622 {
1623
1624         LIST_REMOVE(lop, nfslo_list);
1625         free(lop, M_NFSCLLOCK);
1626         if (local)
1627                 nfsstatsv1.cllocallocks--;
1628         else
1629                 nfsstatsv1.cllocks--;
1630 }
1631
1632 /*
1633  * Clean out the state related to a delegation.
1634  */
1635 static void
1636 nfscl_cleandeleg(struct nfscldeleg *dp)
1637 {
1638         struct nfsclowner *owp, *nowp;
1639         struct nfsclopen *op;
1640
1641         LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
1642                 op = LIST_FIRST(&owp->nfsow_open);
1643                 if (op != NULL) {
1644                         if (LIST_NEXT(op, nfso_list) != NULL)
1645                                 panic("nfscleandel");
1646                         nfscl_freeopen(op, 1);
1647                 }
1648                 nfscl_freeopenowner(owp, 1);
1649         }
1650         nfscl_freealllocks(&dp->nfsdl_lock, 1);
1651 }
1652
1653 /*
1654  * Free a delegation.
1655  */
1656 static void
1657 nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp, bool freeit)
1658 {
1659
1660         TAILQ_REMOVE(hdp, dp, nfsdl_list);
1661         LIST_REMOVE(dp, nfsdl_hash);
1662         if (freeit)
1663                 free(dp, M_NFSCLDELEG);
1664         nfsstatsv1.cldelegates--;
1665         nfscl_delegcnt--;
1666 }
1667
1668 /*
1669  * Free up all state related to this client structure.
1670  */
1671 static void
1672 nfscl_cleanclient(struct nfsclclient *clp)
1673 {
1674         struct nfsclowner *owp, *nowp;
1675         struct nfsclopen *op, *nop;
1676         struct nfscllayout *lyp, *nlyp;
1677         struct nfscldevinfo *dip, *ndip;
1678
1679         TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
1680                 nfscl_freelayout(lyp);
1681
1682         LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
1683                 nfscl_freedevinfo(dip);
1684
1685         /* Now, all the OpenOwners, etc. */
1686         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1687                 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1688                         nfscl_freeopen(op, 0);
1689                 }
1690                 nfscl_freeopenowner(owp, 0);
1691         }
1692 }
1693
1694 /*
1695  * Called when an NFSERR_EXPIRED is received from the server.
1696  */
1697 static void
1698 nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
1699     struct ucred *cred, NFSPROC_T *p)
1700 {
1701         struct nfsclowner *owp, *nowp, *towp;
1702         struct nfsclopen *op, *nop, *top;
1703         struct nfscldeleg *dp, *ndp;
1704         int ret, printed = 0;
1705
1706         /*
1707          * First, merge locally issued Opens into the list for the server.
1708          */
1709         dp = TAILQ_FIRST(&clp->nfsc_deleg);
1710         while (dp != NULL) {
1711             ndp = TAILQ_NEXT(dp, nfsdl_list);
1712             owp = LIST_FIRST(&dp->nfsdl_owner);
1713             while (owp != NULL) {
1714                 nowp = LIST_NEXT(owp, nfsow_list);
1715                 op = LIST_FIRST(&owp->nfsow_open);
1716                 if (op != NULL) {
1717                     if (LIST_NEXT(op, nfso_list) != NULL)
1718                         panic("nfsclexp");
1719                     LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
1720                         if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
1721                             NFSV4CL_LOCKNAMELEN))
1722                             break;
1723                     }
1724                     if (towp != NULL) {
1725                         /* Merge opens in */
1726                         LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
1727                             if (top->nfso_fhlen == op->nfso_fhlen &&
1728                                 !NFSBCMP(top->nfso_fh, op->nfso_fh,
1729                                  op->nfso_fhlen)) {
1730                                 top->nfso_mode |= op->nfso_mode;
1731                                 top->nfso_opencnt += op->nfso_opencnt;
1732                                 break;
1733                             }
1734                         }
1735                         if (top == NULL) {
1736                             /* Just add the open to the owner list */
1737                             LIST_REMOVE(op, nfso_list);
1738                             op->nfso_own = towp;
1739                             LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
1740                             LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh,
1741                                 op->nfso_fhlen), op, nfso_hash);
1742                             nfsstatsv1.cllocalopens--;
1743                             nfsstatsv1.clopens++;
1744                         }
1745                     } else {
1746                         /* Just add the openowner to the client list */
1747                         LIST_REMOVE(owp, nfsow_list);
1748                         owp->nfsow_clp = clp;
1749                         LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
1750                         LIST_INSERT_HEAD(NFSCLOPENHASH(clp, op->nfso_fh,
1751                             op->nfso_fhlen), op, nfso_hash);
1752                         nfsstatsv1.cllocalopenowners--;
1753                         nfsstatsv1.clopenowners++;
1754                         nfsstatsv1.cllocalopens--;
1755                         nfsstatsv1.clopens++;
1756                     }
1757                 }
1758                 owp = nowp;
1759             }
1760             if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
1761                 printed = 1;
1762                 printf("nfsv4 expired locks lost\n");
1763             }
1764             nfscl_cleandeleg(dp);
1765             nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
1766             dp = ndp;
1767         }
1768         if (!TAILQ_EMPTY(&clp->nfsc_deleg))
1769             panic("nfsclexp");
1770
1771         /*
1772          * Now, try and reopen against the server.
1773          */
1774         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1775                 owp->nfsow_seqid = 0;
1776                 LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
1777                         ret = nfscl_expireopen(clp, op, nmp, cred, p);
1778                         if (ret && !printed) {
1779                                 printed = 1;
1780                                 printf("nfsv4 expired locks lost\n");
1781                         }
1782                 }
1783                 if (LIST_EMPTY(&owp->nfsow_open))
1784                         nfscl_freeopenowner(owp, 0);
1785         }
1786 }
1787
1788 /*
1789  * This function must be called after the process represented by "own" has
1790  * exited. Must be called with CLSTATE lock held.
1791  */
1792 static void
1793 nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
1794 {
1795         struct nfsclowner *owp, *nowp;
1796         struct nfscllockowner *lp, *nlp;
1797         struct nfscldeleg *dp;
1798
1799         /* First, get rid of local locks on delegations. */
1800         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1801                 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1802                     if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
1803                         if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
1804                             panic("nfscllckw");
1805                         nfscl_freelockowner(lp, 1);
1806                     }
1807                 }
1808         }
1809         owp = LIST_FIRST(&clp->nfsc_owner);
1810         while (owp != NULL) {
1811                 nowp = LIST_NEXT(owp, nfsow_list);
1812                 if (!NFSBCMP(owp->nfsow_owner, own,
1813                     NFSV4CL_LOCKNAMELEN)) {
1814                         /*
1815                          * If there are children that haven't closed the
1816                          * file descriptors yet, the opens will still be
1817                          * here. For that case, let the renew thread clear
1818                          * out the OpenOwner later.
1819                          */
1820                         if (LIST_EMPTY(&owp->nfsow_open))
1821                                 nfscl_freeopenowner(owp, 0);
1822                         else
1823                                 owp->nfsow_defunct = 1;
1824                 }
1825                 owp = nowp;
1826         }
1827 }
1828
1829 /*
1830  * Find open/lock owners for processes that have exited.
1831  */
1832 static void
1833 nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
1834 {
1835         struct nfsclowner *owp, *nowp;
1836         struct nfsclopen *op;
1837         struct nfscllockowner *lp, *nlp;
1838         struct nfscldeleg *dp;
1839
1840         /*
1841          * All the pidhash locks must be acquired, since they are sx locks
1842          * and must be acquired before the mutexes.  The pid(s) that will
1843          * be used aren't known yet, so all the locks need to be acquired.
1844          * Fortunately, this function is only performed once/sec.
1845          */
1846         pidhash_slockall();
1847         NFSLOCKCLSTATE();
1848         LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
1849                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
1850                         LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
1851                                 if (LIST_EMPTY(&lp->nfsl_lock))
1852                                         nfscl_emptylockowner(lp, lhp);
1853                         }
1854                 }
1855                 if (nfscl_procdoesntexist(owp->nfsow_owner))
1856                         nfscl_cleanup_common(clp, owp->nfsow_owner);
1857         }
1858
1859         /*
1860          * For the single open_owner case, these lock owners need to be
1861          * checked to see if they still exist separately.
1862          * This is because nfscl_procdoesntexist() never returns true for
1863          * the single open_owner so that the above doesn't ever call
1864          * nfscl_cleanup_common().
1865          */
1866         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
1867                 LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
1868                         if (nfscl_procdoesntexist(lp->nfsl_owner))
1869                                 nfscl_cleanup_common(clp, lp->nfsl_owner);
1870                 }
1871         }
1872         NFSUNLOCKCLSTATE();
1873         pidhash_sunlockall();
1874 }
1875
1876 /*
1877  * Take the empty lock owner and move it to the local lhp list if the
1878  * associated process no longer exists.
1879  */
1880 static void
1881 nfscl_emptylockowner(struct nfscllockowner *lp,
1882     struct nfscllockownerfhhead *lhp)
1883 {
1884         struct nfscllockownerfh *lfhp, *mylfhp;
1885         struct nfscllockowner *nlp;
1886         int fnd_it;
1887
1888         /* If not a Posix lock owner, just return. */
1889         if ((lp->nfsl_lockflags & F_POSIX) == 0)
1890                 return;
1891
1892         fnd_it = 0;
1893         mylfhp = NULL;
1894         /*
1895          * First, search to see if this lock owner is already in the list.
1896          * If it is, then the associated process no longer exists.
1897          */
1898         SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
1899                 if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
1900                     !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
1901                     lfhp->nfslfh_len))
1902                         mylfhp = lfhp;
1903                 LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
1904                         if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
1905                             NFSV4CL_LOCKNAMELEN))
1906                                 fnd_it = 1;
1907         }
1908         /* If not found, check if process still exists. */
1909         if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
1910                 return;
1911
1912         /* Move the lock owner over to the local list. */
1913         if (mylfhp == NULL) {
1914                 mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
1915                     M_NOWAIT);
1916                 if (mylfhp == NULL)
1917                         return;
1918                 mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
1919                 NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
1920                     mylfhp->nfslfh_len);
1921                 LIST_INIT(&mylfhp->nfslfh_lock);
1922                 SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
1923         }
1924         LIST_REMOVE(lp, nfsl_list);
1925         LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
1926 }
1927
1928 static int      fake_global;    /* Used to force visibility of MNTK_UNMOUNTF */
1929 /*
1930  * Called from nfs umount to free up the clientid.
1931  */
1932 void
1933 nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
1934 {
1935         struct nfsclclient *clp;
1936         struct ucred *cred;
1937         int igotlock;
1938
1939         /*
1940          * For the case that matters, this is the thread that set
1941          * MNTK_UNMOUNTF, so it will see it set. The code that follows is
1942          * done to ensure that any thread executing nfscl_getcl() after
1943          * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
1944          * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
1945          * explanation, courtesy of Alan Cox.
1946          * What follows is a snippet from Alan Cox's email at:
1947          * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
1948          * 
1949          * 1. Set MNTK_UNMOUNTF
1950          * 2. Acquire a standard FreeBSD mutex "m".
1951          * 3. Update some data structures.
1952          * 4. Release mutex "m".
1953          * 
1954          * Then, other threads that acquire "m" after step 4 has occurred will
1955          * see MNTK_UNMOUNTF as set.  But, other threads that beat thread X to
1956          * step 2 may or may not see MNTK_UNMOUNTF as set.
1957          */
1958         NFSLOCKCLSTATE();
1959         if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
1960                 fake_global++;
1961                 NFSUNLOCKCLSTATE();
1962                 NFSLOCKCLSTATE();
1963         }
1964
1965         clp = nmp->nm_clp;
1966         if (clp != NULL) {
1967                 if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
1968                         panic("nfscl umount");
1969
1970                 /*
1971                  * First, handshake with the nfscl renew thread, to terminate
1972                  * it.
1973                  */
1974                 clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
1975                 while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
1976                         (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
1977                             "nfsclumnt", hz);
1978
1979                 /*
1980                  * Now, get the exclusive lock on the client state, so
1981                  * that no uses of the state are still in progress.
1982                  */
1983                 do {
1984                         igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
1985                             NFSCLSTATEMUTEXPTR, NULL);
1986                 } while (!igotlock);
1987                 NFSUNLOCKCLSTATE();
1988
1989                 /*
1990                  * Free up all the state. It will expire on the server, but
1991                  * maybe we should do a SetClientId/SetClientIdConfirm so
1992                  * the server throws it away?
1993                  */
1994                 LIST_REMOVE(clp, nfsc_list);
1995                 nfscl_delegreturnall(clp, p);
1996                 cred = newnfs_getcred();
1997                 if (NFSHASNFSV4N(nmp)) {
1998                         (void)nfsrpc_destroysession(nmp, clp, cred, p);
1999                         (void)nfsrpc_destroyclient(nmp, clp, cred, p);
2000                 } else
2001                         (void)nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2002                 nfscl_cleanclient(clp);
2003                 nmp->nm_clp = NULL;
2004                 NFSFREECRED(cred);
2005                 free(clp, M_NFSCLCLIENT);
2006         } else
2007                 NFSUNLOCKCLSTATE();
2008 }
2009
2010 /*
2011  * This function is called when a server replies with NFSERR_STALECLIENTID
2012  * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
2013  * doing Opens and Locks with reclaim. If these fail, it deletes the
2014  * corresponding state.
2015  */
2016 static void
2017 nfscl_recover(struct nfsclclient *clp, bool *retokp, struct ucred *cred,
2018     NFSPROC_T *p)
2019 {
2020         struct nfsclowner *owp, *nowp;
2021         struct nfsclopen *op, *nop;
2022         struct nfscllockowner *lp, *nlp;
2023         struct nfscllock *lop, *nlop;
2024         struct nfscldeleg *dp, *ndp, *tdp;
2025         struct nfsmount *nmp;
2026         struct ucred *tcred;
2027         struct nfsclopenhead extra_open;
2028         struct nfscldeleghead extra_deleg;
2029         struct nfsreq *rep;
2030         u_int64_t len;
2031         u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
2032         int i, igotlock = 0, error, trycnt, firstlock;
2033         struct nfscllayout *lyp, *nlyp;
2034         bool recovered_one;
2035
2036         /*
2037          * First, lock the client structure, so everyone else will
2038          * block when trying to use state.
2039          */
2040         NFSLOCKCLSTATE();
2041         clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2042         do {
2043                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2044                     NFSCLSTATEMUTEXPTR, NULL);
2045         } while (!igotlock);
2046         NFSUNLOCKCLSTATE();
2047
2048         nmp = clp->nfsc_nmp;
2049         if (nmp == NULL)
2050                 panic("nfscl recover");
2051
2052         /*
2053          * For now, just get rid of all layouts. There may be a need
2054          * to do LayoutCommit Ops with reclaim == true later.
2055          */
2056         TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
2057                 nfscl_freelayout(lyp);
2058         TAILQ_INIT(&clp->nfsc_layout);
2059         for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
2060                 LIST_INIT(&clp->nfsc_layouthash[i]);
2061
2062         trycnt = 5;
2063         tcred = NULL;
2064         do {
2065                 error = nfsrpc_setclient(nmp, clp, 1, retokp, cred, p);
2066         } while ((error == NFSERR_STALECLIENTID ||
2067              error == NFSERR_BADSESSION ||
2068              error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2069         if (error) {
2070                 NFSLOCKCLSTATE();
2071                 clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
2072                     NFSCLFLAGS_RECVRINPROG);
2073                 wakeup(&clp->nfsc_flags);
2074                 nfsv4_unlock(&clp->nfsc_lock, 0);
2075                 NFSUNLOCKCLSTATE();
2076                 return;
2077         }
2078         clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2079         clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2080
2081         /*
2082          * Mark requests already queued on the server, so that they don't
2083          * initiate another recovery cycle. Any requests already in the
2084          * queue that handle state information will have the old stale
2085          * clientid/stateid and will get a NFSERR_STALESTATEID,
2086          * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
2087          * This will be translated to NFSERR_STALEDONTRECOVER when
2088          * R_DONTRECOVER is set.
2089          */
2090         NFSLOCKREQ();
2091         TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
2092                 if (rep->r_nmp == nmp)
2093                         rep->r_flags |= R_DONTRECOVER;
2094         }
2095         NFSUNLOCKREQ();
2096
2097         /*
2098          * If nfsrpc_setclient() returns *retokp == true,
2099          * no more recovery is needed.
2100          */
2101         if (*retokp)
2102                 goto out;
2103
2104         /*
2105          * Now, mark all delegations "need reclaim".
2106          */
2107         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
2108                 dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;
2109
2110         TAILQ_INIT(&extra_deleg);
2111         LIST_INIT(&extra_open);
2112         /*
2113          * Now traverse the state lists, doing Open and Lock Reclaims.
2114          */
2115         tcred = newnfs_getcred();
2116         recovered_one = false;
2117         owp = LIST_FIRST(&clp->nfsc_owner);
2118         while (owp != NULL) {
2119             nowp = LIST_NEXT(owp, nfsow_list);
2120             owp->nfsow_seqid = 0;
2121             op = LIST_FIRST(&owp->nfsow_open);
2122             while (op != NULL) {
2123                 nop = LIST_NEXT(op, nfso_list);
2124                 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2125                     /* Search for a delegation to reclaim with the open */
2126                     TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2127                         if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2128                             continue;
2129                         if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2130                             mode = NFSV4OPEN_ACCESSWRITE;
2131                             delegtype = NFSV4OPEN_DELEGATEWRITE;
2132                         } else {
2133                             mode = NFSV4OPEN_ACCESSREAD;
2134                             delegtype = NFSV4OPEN_DELEGATEREAD;
2135                         }
2136                         if ((op->nfso_mode & mode) == mode &&
2137                             op->nfso_fhlen == dp->nfsdl_fhlen &&
2138                             !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
2139                             break;
2140                     }
2141                     ndp = dp;
2142                     if (dp == NULL)
2143                         delegtype = NFSV4OPEN_DELEGATENONE;
2144                     newnfs_copycred(&op->nfso_cred, tcred);
2145                     error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
2146                         op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
2147                         op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
2148                         tcred, p);
2149                     if (!error) {
2150                         recovered_one = true;
2151                         /* Handle any replied delegation */
2152                         if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
2153                             || NFSMNT_RDONLY(nmp->nm_mountp))) {
2154                             if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
2155                                 mode = NFSV4OPEN_ACCESSWRITE;
2156                             else
2157                                 mode = NFSV4OPEN_ACCESSREAD;
2158                             TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
2159                                 if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
2160                                     continue;
2161                                 if ((op->nfso_mode & mode) == mode &&
2162                                     op->nfso_fhlen == dp->nfsdl_fhlen &&
2163                                     !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
2164                                     op->nfso_fhlen)) {
2165                                     dp->nfsdl_stateid = ndp->nfsdl_stateid;
2166                                     dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
2167                                     dp->nfsdl_ace = ndp->nfsdl_ace;
2168                                     dp->nfsdl_change = ndp->nfsdl_change;
2169                                     dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2170                                     if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
2171                                         dp->nfsdl_flags |= NFSCLDL_RECALL;
2172                                     free(ndp, M_NFSCLDELEG);
2173                                     ndp = NULL;
2174                                     break;
2175                                 }
2176                             }
2177                         }
2178                         if (ndp != NULL)
2179                             TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);
2180
2181                         /* and reclaim all byte range locks */
2182                         lp = LIST_FIRST(&op->nfso_lock);
2183                         while (lp != NULL) {
2184                             nlp = LIST_NEXT(lp, nfsl_list);
2185                             lp->nfsl_seqid = 0;
2186                             firstlock = 1;
2187                             lop = LIST_FIRST(&lp->nfsl_lock);
2188                             while (lop != NULL) {
2189                                 nlop = LIST_NEXT(lop, nfslo_list);
2190                                 if (lop->nfslo_end == NFS64BITSSET)
2191                                     len = NFS64BITSSET;
2192                                 else
2193                                     len = lop->nfslo_end - lop->nfslo_first;
2194                                 error = nfscl_trylock(nmp, NULL,
2195                                     op->nfso_fh, op->nfso_fhlen, lp,
2196                                     firstlock, 1, lop->nfslo_first, len,
2197                                     lop->nfslo_type, tcred, p);
2198                                 if (error != 0)
2199                                     nfscl_freelock(lop, 0);
2200                                 else
2201                                     firstlock = 0;
2202                                 lop = nlop;
2203                             }
2204                             /* If no locks, but a lockowner, just delete it. */
2205                             if (LIST_EMPTY(&lp->nfsl_lock))
2206                                 nfscl_freelockowner(lp, 0);
2207                             lp = nlp;
2208                         }
2209                     } else if (error == NFSERR_NOGRACE && !recovered_one &&
2210                         NFSHASNFSV4N(nmp)) {
2211                         /*
2212                          * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
2213                          * actually end up here, since the client will do
2214                          * a recovery for NFSERR_BADSESSION, but will get
2215                          * an NFSERR_NOGRACE reply for the first "reclaim"
2216                          * attempt.
2217                          * So, call nfscl_expireclient() to recover the
2218                          * opens as best we can and then do a reclaim
2219                          * complete and return.
2220                          */
2221                         nfsrpc_reclaimcomplete(nmp, cred, p);
2222                         nfscl_expireclient(clp, nmp, tcred, p);
2223                         goto out;
2224                     }
2225                 }
2226                 if (error != 0 && error != NFSERR_BADSESSION)
2227                     nfscl_freeopen(op, 0);
2228                 op = nop;
2229             }
2230             owp = nowp;
2231         }
2232
2233         /*
2234          * Now, try and get any delegations not yet reclaimed by cobbling
2235          * to-gether an appropriate open.
2236          */
2237         nowp = NULL;
2238         dp = TAILQ_FIRST(&clp->nfsc_deleg);
2239         while (dp != NULL) {
2240             ndp = TAILQ_NEXT(dp, nfsdl_list);
2241             if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
2242                 if (nowp == NULL) {
2243                     nowp = malloc(
2244                         sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
2245                     /*
2246                      * Name must be as long an largest possible
2247                      * NFSV4CL_LOCKNAMELEN. 12 for now.
2248                      */
2249                     NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
2250                         NFSV4CL_LOCKNAMELEN);
2251                     LIST_INIT(&nowp->nfsow_open);
2252                     nowp->nfsow_clp = clp;
2253                     nowp->nfsow_seqid = 0;
2254                     nowp->nfsow_defunct = 0;
2255                     nfscl_lockinit(&nowp->nfsow_rwlock);
2256                 }
2257                 nop = NULL;
2258                 if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
2259                     nop = malloc(sizeof (struct nfsclopen) +
2260                         dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
2261                     nop->nfso_own = nowp;
2262                     if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
2263                         nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
2264                         delegtype = NFSV4OPEN_DELEGATEWRITE;
2265                     } else {
2266                         nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
2267                         delegtype = NFSV4OPEN_DELEGATEREAD;
2268                     }
2269                     nop->nfso_opencnt = 0;
2270                     nop->nfso_posixlock = 1;
2271                     nop->nfso_fhlen = dp->nfsdl_fhlen;
2272                     NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
2273                     LIST_INIT(&nop->nfso_lock);
2274                     nop->nfso_stateid.seqid = 0;
2275                     nop->nfso_stateid.other[0] = 0;
2276                     nop->nfso_stateid.other[1] = 0;
2277                     nop->nfso_stateid.other[2] = 0;
2278                     newnfs_copycred(&dp->nfsdl_cred, tcred);
2279                     newnfs_copyincred(tcred, &nop->nfso_cred);
2280                     tdp = NULL;
2281                     error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
2282                         nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
2283                         nop->nfso_mode, nop, NULL, 0, &tdp, 1,
2284                         delegtype, tcred, p);
2285                     if (tdp != NULL) {
2286                         if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
2287                             mode = NFSV4OPEN_ACCESSWRITE;
2288                         else
2289                             mode = NFSV4OPEN_ACCESSREAD;
2290                         if ((nop->nfso_mode & mode) == mode &&
2291                             nop->nfso_fhlen == tdp->nfsdl_fhlen &&
2292                             !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
2293                             nop->nfso_fhlen)) {
2294                             dp->nfsdl_stateid = tdp->nfsdl_stateid;
2295                             dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
2296                             dp->nfsdl_ace = tdp->nfsdl_ace;
2297                             dp->nfsdl_change = tdp->nfsdl_change;
2298                             dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
2299                             if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
2300                                 dp->nfsdl_flags |= NFSCLDL_RECALL;
2301                             free(tdp, M_NFSCLDELEG);
2302                         } else {
2303                             TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
2304                         }
2305                     }
2306                 }
2307                 if (error) {
2308                     if (nop != NULL)
2309                         free(nop, M_NFSCLOPEN);
2310                     if (error == NFSERR_NOGRACE && !recovered_one &&
2311                         NFSHASNFSV4N(nmp)) {
2312                         /*
2313                          * For NFSv4.1/4.2, the NFSERR_EXPIRED case will
2314                          * actually end up here, since the client will do
2315                          * a recovery for NFSERR_BADSESSION, but will get
2316                          * an NFSERR_NOGRACE reply for the first "reclaim"
2317                          * attempt.
2318                          * So, call nfscl_expireclient() to recover the
2319                          * opens as best we can and then do a reclaim
2320                          * complete and return.
2321                          */
2322                         nfsrpc_reclaimcomplete(nmp, cred, p);
2323                         nfscl_expireclient(clp, nmp, tcred, p);
2324                         free(nowp, M_NFSCLOWNER);
2325                         goto out;
2326                     }
2327                     /*
2328                      * Couldn't reclaim it, so throw the state
2329                      * away. Ouch!!
2330                      */
2331                     nfscl_cleandeleg(dp);
2332                     nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
2333                 } else {
2334                     recovered_one = true;
2335                     LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
2336                 }
2337             }
2338             dp = ndp;
2339         }
2340
2341         /*
2342          * Now, get rid of extra Opens and Delegations.
2343          */
2344         LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
2345                 do {
2346                         newnfs_copycred(&op->nfso_cred, tcred);
2347                         error = nfscl_tryclose(op, tcred, nmp, p);
2348                         if (error == NFSERR_GRACE)
2349                                 (void) nfs_catnap(PZERO, error, "nfsexcls");
2350                 } while (error == NFSERR_GRACE);
2351                 LIST_REMOVE(op, nfso_list);
2352                 free(op, M_NFSCLOPEN);
2353         }
2354         if (nowp != NULL)
2355                 free(nowp, M_NFSCLOWNER);
2356
2357         TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
2358                 do {
2359                         newnfs_copycred(&dp->nfsdl_cred, tcred);
2360                         error = nfscl_trydelegreturn(dp, tcred, nmp, p);
2361                         if (error == NFSERR_GRACE)
2362                                 (void) nfs_catnap(PZERO, error, "nfsexdlg");
2363                 } while (error == NFSERR_GRACE);
2364                 TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
2365                 free(dp, M_NFSCLDELEG);
2366         }
2367
2368         /* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
2369         if (NFSHASNFSV4N(nmp))
2370                 (void)nfsrpc_reclaimcomplete(nmp, cred, p);
2371
2372 out:
2373         NFSLOCKCLSTATE();
2374         clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
2375         wakeup(&clp->nfsc_flags);
2376         nfsv4_unlock(&clp->nfsc_lock, 0);
2377         NFSUNLOCKCLSTATE();
2378         if (tcred != NULL)
2379                 NFSFREECRED(tcred);
2380 }
2381
2382 /*
2383  * This function is called when a server replies with NFSERR_EXPIRED.
2384  * It deletes all state for the client and does a fresh SetClientId/confirm.
2385  * XXX Someday it should post a signal to the process(es) that hold the
2386  * state, so they know that lock state has been lost.
2387  */
2388 int
2389 nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
2390 {
2391         struct nfsmount *nmp;
2392         struct ucred *cred;
2393         int igotlock = 0, error, trycnt;
2394
2395         /*
2396          * If the clientid has gone away or a new SetClientid has already
2397          * been done, just return ok.
2398          */
2399         if (clp == NULL || clidrev != clp->nfsc_clientidrev)
2400                 return (0);
2401
2402         /*
2403          * First, lock the client structure, so everyone else will
2404          * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
2405          * that only one thread does the work.
2406          */
2407         NFSLOCKCLSTATE();
2408         clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
2409         do {
2410                 igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
2411                     NFSCLSTATEMUTEXPTR, NULL);
2412         } while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
2413         if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
2414                 if (igotlock)
2415                         nfsv4_unlock(&clp->nfsc_lock, 0);
2416                 NFSUNLOCKCLSTATE();
2417                 return (0);
2418         }
2419         clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
2420         NFSUNLOCKCLSTATE();
2421
2422         nmp = clp->nfsc_nmp;
2423         if (nmp == NULL)
2424                 panic("nfscl expired");
2425         cred = newnfs_getcred();
2426         trycnt = 5;
2427         do {
2428                 error = nfsrpc_setclient(nmp, clp, 0, NULL, cred, p);
2429         } while ((error == NFSERR_STALECLIENTID ||
2430              error == NFSERR_BADSESSION ||
2431              error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
2432         if (error) {
2433                 NFSLOCKCLSTATE();
2434                 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2435         } else {
2436                 /*
2437                  * Expire the state for the client.
2438                  */
2439                 nfscl_expireclient(clp, nmp, cred, p);
2440                 NFSLOCKCLSTATE();
2441                 clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
2442                 clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
2443         }
2444         clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
2445         wakeup(&clp->nfsc_flags);
2446         nfsv4_unlock(&clp->nfsc_lock, 0);
2447         NFSUNLOCKCLSTATE();
2448         NFSFREECRED(cred);
2449         return (error);
2450 }
2451
2452 /*
2453  * This function inserts a lock in the list after insert_lop.
2454  */
2455 static void
2456 nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
2457     struct nfscllock *insert_lop, int local)
2458 {
2459
2460         if ((struct nfscllockowner *)insert_lop == lp)
2461                 LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
2462         else
2463                 LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
2464         if (local)
2465                 nfsstatsv1.cllocallocks++;
2466         else
2467                 nfsstatsv1.cllocks++;
2468 }
2469
2470 /*
2471  * This function updates the locking for a lock owner and given file. It
2472  * maintains a list of lock ranges ordered on increasing file offset that
2473  * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
2474  * It always adds new_lop to the list and sometimes uses the one pointed
2475  * at by other_lopp.
2476  * Returns 1 if the locks were modified, 0 otherwise.
2477  */
2478 static int
2479 nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
2480     struct nfscllock **other_lopp, int local)
2481 {
2482         struct nfscllock *new_lop = *new_lopp;
2483         struct nfscllock *lop, *tlop, *ilop;
2484         struct nfscllock *other_lop;
2485         int unlock = 0, modified = 0;
2486         u_int64_t tmp;
2487
2488         /*
2489          * Work down the list until the lock is merged.
2490          */
2491         if (new_lop->nfslo_type == F_UNLCK)
2492                 unlock = 1;
2493         ilop = (struct nfscllock *)lp;
2494         lop = LIST_FIRST(&lp->nfsl_lock);
2495         while (lop != NULL) {
2496             /*
2497              * Only check locks for this file that aren't before the start of
2498              * new lock's range.
2499              */
2500             if (lop->nfslo_end >= new_lop->nfslo_first) {
2501                 if (new_lop->nfslo_end < lop->nfslo_first) {
2502                     /*
2503                      * If the new lock ends before the start of the
2504                      * current lock's range, no merge, just insert
2505                      * the new lock.
2506                      */
2507                     break;
2508                 }
2509                 if (new_lop->nfslo_type == lop->nfslo_type ||
2510                     (new_lop->nfslo_first <= lop->nfslo_first &&
2511                      new_lop->nfslo_end >= lop->nfslo_end)) {
2512                     /*
2513                      * This lock can be absorbed by the new lock/unlock.
2514                      * This happens when it covers the entire range
2515                      * of the old lock or is contiguous
2516                      * with the old lock and is of the same type or an
2517                      * unlock.
2518                      */
2519                     if (new_lop->nfslo_type != lop->nfslo_type ||
2520                         new_lop->nfslo_first != lop->nfslo_first ||
2521                         new_lop->nfslo_end != lop->nfslo_end)
2522                         modified = 1;
2523                     if (lop->nfslo_first < new_lop->nfslo_first)
2524                         new_lop->nfslo_first = lop->nfslo_first;
2525                     if (lop->nfslo_end > new_lop->nfslo_end)
2526                         new_lop->nfslo_end = lop->nfslo_end;
2527                     tlop = lop;
2528                     lop = LIST_NEXT(lop, nfslo_list);
2529                     nfscl_freelock(tlop, local);
2530                     continue;
2531                 }
2532
2533                 /*
2534                  * All these cases are for contiguous locks that are not the
2535                  * same type, so they can't be merged.
2536                  */
2537                 if (new_lop->nfslo_first <= lop->nfslo_first) {
2538                     /*
2539                      * This case is where the new lock overlaps with the
2540                      * first part of the old lock. Move the start of the
2541                      * old lock to just past the end of the new lock. The
2542                      * new lock will be inserted in front of the old, since
2543                      * ilop hasn't been updated. (We are done now.)
2544                      */
2545                     if (lop->nfslo_first != new_lop->nfslo_end) {
2546                         lop->nfslo_first = new_lop->nfslo_end;
2547                         modified = 1;
2548                     }
2549                     break;
2550                 }
2551                 if (new_lop->nfslo_end >= lop->nfslo_end) {
2552                     /*
2553                      * This case is where the new lock overlaps with the
2554                      * end of the old lock's range. Move the old lock's
2555                      * end to just before the new lock's first and insert
2556                      * the new lock after the old lock.
2557                      * Might not be done yet, since the new lock could
2558                      * overlap further locks with higher ranges.
2559                      */
2560                     if (lop->nfslo_end != new_lop->nfslo_first) {
2561                         lop->nfslo_end = new_lop->nfslo_first;
2562                         modified = 1;
2563                     }
2564                     ilop = lop;
2565                     lop = LIST_NEXT(lop, nfslo_list);
2566                     continue;
2567                 }
2568                 /*
2569                  * The final case is where the new lock's range is in the
2570                  * middle of the current lock's and splits the current lock
2571                  * up. Use *other_lopp to handle the second part of the
2572                  * split old lock range. (We are done now.)
2573                  * For unlock, we use new_lop as other_lop and tmp, since
2574                  * other_lop and new_lop are the same for this case.
2575                  * We noted the unlock case above, so we don't need
2576                  * new_lop->nfslo_type any longer.
2577                  */
2578                 tmp = new_lop->nfslo_first;
2579                 if (unlock) {
2580                     other_lop = new_lop;
2581                     *new_lopp = NULL;
2582                 } else {
2583                     other_lop = *other_lopp;
2584                     *other_lopp = NULL;
2585                 }
2586                 other_lop->nfslo_first = new_lop->nfslo_end;
2587                 other_lop->nfslo_end = lop->nfslo_end;
2588                 other_lop->nfslo_type = lop->nfslo_type;
2589                 lop->nfslo_end = tmp;
2590                 nfscl_insertlock(lp, other_lop, lop, local);
2591                 ilop = lop;
2592                 modified = 1;
2593                 break;
2594             }
2595             ilop = lop;
2596             lop = LIST_NEXT(lop, nfslo_list);
2597             if (lop == NULL)
2598                 break;
2599         }
2600
2601         /*
2602          * Insert the new lock in the list at the appropriate place.
2603          */
2604         if (!unlock) {
2605                 nfscl_insertlock(lp, new_lop, ilop, local);
2606                 *new_lopp = NULL;
2607                 modified = 1;
2608         }
2609         return (modified);
2610 }
2611
2612 /*
2613  * This function must be run as a kernel thread.
2614  * It does Renew Ops and recovery, when required.
2615  */
2616 void
2617 nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
2618 {
2619         struct nfsclowner *owp, *nowp;
2620         struct nfsclopen *op;
2621         struct nfscllockowner *lp, *nlp;
2622         struct nfscldeleghead dh;
2623         struct nfscldeleg *dp, *ndp;
2624         struct ucred *cred;
2625         u_int32_t clidrev;
2626         int error, cbpathdown, islept, igotlock, ret, clearok;
2627         uint32_t recover_done_time = 0;
2628         time_t mytime;
2629         static time_t prevsec = 0;
2630         struct nfscllockownerfh *lfhp, *nlfhp;
2631         struct nfscllockownerfhhead lfh;
2632         struct nfscllayout *lyp, *nlyp;
2633         struct nfscldevinfo *dip, *ndip;
2634         struct nfscllayouthead rlh;
2635         struct nfsclrecalllayout *recallp;
2636         struct nfsclds *dsp;
2637         bool retok;
2638         struct mount *mp;
2639         vnode_t vp;
2640
2641         cred = newnfs_getcred();
2642         NFSLOCKCLSTATE();
2643         clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
2644         mp = clp->nfsc_nmp->nm_mountp;
2645         NFSUNLOCKCLSTATE();
2646         for(;;) {
2647                 newnfs_setroot(cred);
2648                 cbpathdown = 0;
2649                 if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
2650                         /*
2651                          * Only allow one full recover within 1/2 of the lease
2652                          * duration (nfsc_renew).
2653                          * retok is value/result.  If passed in set to true,
2654                          * it indicates only a CreateSession operation should
2655                          * be attempted.
2656                          * If it is returned true, it indicates that the
2657                          * recovery only required a CreateSession.
2658                          */
2659                         retok = true;
2660                         if (recover_done_time < NFSD_MONOSEC) {
2661                                 recover_done_time = NFSD_MONOSEC +
2662                                     clp->nfsc_renew;
2663                                 retok = false;
2664                         }
2665                         NFSCL_DEBUG(1, "Doing recovery, only "
2666                             "createsession=%d\n", retok);
2667                         nfscl_recover(clp, &retok, cred, p);
2668                 }
2669                 if (clp->nfsc_expire <= NFSD_MONOSEC &&
2670                     (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
2671                         clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
2672                         clidrev = clp->nfsc_clientidrev;
2673                         error = nfsrpc_renew(clp, NULL, cred, p);
2674                         if (error == NFSERR_CBPATHDOWN)
2675                             cbpathdown = 1;
2676                         else if (error == NFSERR_STALECLIENTID ||
2677                             error == NFSERR_BADSESSION) {
2678                             NFSLOCKCLSTATE();
2679                             clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2680                             NFSUNLOCKCLSTATE();
2681                         } else if (error == NFSERR_EXPIRED)
2682                             (void) nfscl_hasexpired(clp, clidrev, p);
2683                 }
2684
2685 checkdsrenew:
2686                 if (NFSHASNFSV4N(clp->nfsc_nmp)) {
2687                         /* Do renews for any DS sessions. */
2688                         NFSLOCKMNT(clp->nfsc_nmp);
2689                         /* Skip first entry, since the MDS is handled above. */
2690                         dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
2691                         if (dsp != NULL)
2692                                 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2693                         while (dsp != NULL) {
2694                                 if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
2695                                     dsp->nfsclds_sess.nfsess_defunct == 0) {
2696                                         dsp->nfsclds_expire = NFSD_MONOSEC +
2697                                             clp->nfsc_renew;
2698                                         NFSUNLOCKMNT(clp->nfsc_nmp);
2699                                         (void)nfsrpc_renew(clp, dsp, cred, p);
2700                                         goto checkdsrenew;
2701                                 }
2702                                 dsp = TAILQ_NEXT(dsp, nfsclds_list);
2703                         }
2704                         NFSUNLOCKMNT(clp->nfsc_nmp);
2705                 }
2706
2707                 TAILQ_INIT(&dh);
2708                 NFSLOCKCLSTATE();
2709                 if (cbpathdown)
2710                         /* It's a Total Recall! */
2711                         nfscl_totalrecall(clp);
2712
2713                 /*
2714                  * Now, handle defunct owners.
2715                  */
2716                 LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
2717                         if (LIST_EMPTY(&owp->nfsow_open)) {
2718                                 if (owp->nfsow_defunct != 0)
2719                                         nfscl_freeopenowner(owp, 0);
2720                         }
2721                 }
2722
2723                 /*
2724                  * Do the recall on any delegations. To avoid trouble, always
2725                  * come back up here after having slept.
2726                  */
2727                 igotlock = 0;
2728 tryagain:
2729                 dp = TAILQ_FIRST(&clp->nfsc_deleg);
2730                 while (dp != NULL) {
2731                         ndp = TAILQ_NEXT(dp, nfsdl_list);
2732                         if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
2733                                 /*
2734                                  * Wait for outstanding I/O ops to be done.
2735                                  */
2736                                 if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
2737                                     if (igotlock) {
2738                                         nfsv4_unlock(&clp->nfsc_lock, 0);
2739                                         igotlock = 0;
2740                                     }
2741                                     dp->nfsdl_rwlock.nfslock_lock |=
2742                                         NFSV4LOCK_WANTED;
2743                                     msleep(&dp->nfsdl_rwlock,
2744                                         NFSCLSTATEMUTEXPTR, PVFS, "nfscld",
2745                                         5 * hz);
2746                                     if (NFSCL_FORCEDISM(mp))
2747                                         goto terminate;
2748                                     goto tryagain;
2749                                 }
2750                                 while (!igotlock) {
2751                                     igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
2752                                         &islept, NFSCLSTATEMUTEXPTR, mp);
2753                                     if (igotlock == 0 && NFSCL_FORCEDISM(mp))
2754                                         goto terminate;
2755                                     if (islept)
2756                                         goto tryagain;
2757                                 }
2758                                 NFSUNLOCKCLSTATE();
2759                                 newnfs_copycred(&dp->nfsdl_cred, cred);
2760                                 ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
2761                                     NULL, cred, p, 1, &vp);
2762                                 if (!ret) {
2763                                     nfscl_cleandeleg(dp);
2764                                     TAILQ_REMOVE(&clp->nfsc_deleg, dp,
2765                                         nfsdl_list);
2766                                     LIST_REMOVE(dp, nfsdl_hash);
2767                                     TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2768                                     nfscl_delegcnt--;
2769                                     nfsstatsv1.cldelegates--;
2770                                 }
2771                                 NFSLOCKCLSTATE();
2772                                 /*
2773                                  * The nfsc_lock must be released before doing
2774                                  * vrele(), since it might call nfs_inactive().
2775                                  * For the unlikely case where the vnode failed
2776                                  * to be acquired by nfscl_recalldeleg(), a
2777                                  * VOP_RECLAIM() should be in progress and it
2778                                  * will return the delegation.
2779                                  */
2780                                 nfsv4_unlock(&clp->nfsc_lock, 0);
2781                                 igotlock = 0;
2782                                 if (vp != NULL) {
2783                                         NFSUNLOCKCLSTATE();
2784                                         vrele(vp);
2785                                         NFSLOCKCLSTATE();
2786                                 }
2787                                 goto tryagain;
2788                         }
2789                         dp = ndp;
2790                 }
2791
2792                 /*
2793                  * Clear out old delegations, if we are above the high water
2794                  * mark. Only clear out ones with no state related to them.
2795                  * The tailq list is in LRU order.
2796                  */
2797                 dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
2798                 while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
2799                     ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
2800                     if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
2801                         dp->nfsdl_rwlock.nfslock_lock == 0 &&
2802                         dp->nfsdl_timestamp < NFSD_MONOSEC &&
2803                         (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
2804                           NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
2805                         clearok = 1;
2806                         LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
2807                             op = LIST_FIRST(&owp->nfsow_open);
2808                             if (op != NULL) {
2809                                 clearok = 0;
2810                                 break;
2811                             }
2812                         }
2813                         if (clearok) {
2814                             LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
2815                                 if (!LIST_EMPTY(&lp->nfsl_lock)) {
2816                                     clearok = 0;
2817                                     break;
2818                                 }
2819                             }
2820                         }
2821                         if (clearok) {
2822                             TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
2823                             LIST_REMOVE(dp, nfsdl_hash);
2824                             TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
2825                             nfscl_delegcnt--;
2826                             nfsstatsv1.cldelegates--;
2827                         }
2828                     }
2829                     dp = ndp;
2830                 }
2831                 if (igotlock)
2832                         nfsv4_unlock(&clp->nfsc_lock, 0);
2833
2834                 /*
2835                  * Do the recall on any layouts. To avoid trouble, always
2836                  * come back up here after having slept.
2837                  */
2838                 TAILQ_INIT(&rlh);
2839 tryagain2:
2840                 TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
2841                         if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
2842                                 /*
2843                                  * Wait for outstanding I/O ops to be done.
2844                                  */
2845                                 if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
2846                                     (lyp->nfsly_lock.nfslock_lock &
2847                                      NFSV4LOCK_LOCK) != 0) {
2848                                         lyp->nfsly_lock.nfslock_lock |=
2849                                             NFSV4LOCK_WANTED;
2850                                         msleep(&lyp->nfsly_lock.nfslock_lock,
2851                                             NFSCLSTATEMUTEXPTR, PVFS, "nfslyp",
2852                                             5 * hz);
2853                                         if (NFSCL_FORCEDISM(mp))
2854                                             goto terminate;
2855                                         goto tryagain2;
2856                                 }
2857                                 /* Move the layout to the recall list. */
2858                                 TAILQ_REMOVE(&clp->nfsc_layout, lyp,
2859                                     nfsly_list);
2860                                 LIST_REMOVE(lyp, nfsly_hash);
2861                                 TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);
2862
2863                                 /* Handle any layout commits. */
2864                                 if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
2865                                     (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
2866                                         lyp->nfsly_flags &= ~NFSLY_WRITTEN;
2867                                         NFSUNLOCKCLSTATE();
2868                                         NFSCL_DEBUG(3, "do layoutcommit\n");
2869                                         nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
2870                                             cred, p);
2871                                         NFSLOCKCLSTATE();
2872                                         goto tryagain2;
2873                                 }
2874                         }
2875                 }
2876
2877                 /* Now, look for stale layouts. */
2878                 lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
2879                 while (lyp != NULL) {
2880                         nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
2881                         if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
2882                             (lyp->nfsly_flags & NFSLY_RECALL) == 0 &&
2883                             lyp->nfsly_lock.nfslock_usecnt == 0 &&
2884                             lyp->nfsly_lock.nfslock_lock == 0) {
2885                                 NFSCL_DEBUG(4, "ret stale lay=%d\n",
2886                                     nfscl_layoutcnt);
2887                                 recallp = malloc(sizeof(*recallp),
2888                                     M_NFSLAYRECALL, M_NOWAIT);
2889                                 if (recallp == NULL)
2890                                         break;
2891                                 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
2892                                     lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
2893                                     lyp->nfsly_stateid.seqid, 0, 0, NULL,
2894                                     recallp);
2895                         }
2896                         lyp = nlyp;
2897                 }
2898
2899                 /*
2900                  * Free up any unreferenced device info structures.
2901                  */
2902                 LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
2903                         if (dip->nfsdi_layoutrefs == 0 &&
2904                             dip->nfsdi_refcnt == 0) {
2905                                 NFSCL_DEBUG(4, "freeing devinfo\n");
2906                                 LIST_REMOVE(dip, nfsdi_list);
2907                                 nfscl_freedevinfo(dip);
2908                         }
2909                 }
2910                 NFSUNLOCKCLSTATE();
2911
2912                 /* Do layout return(s), as required. */
2913                 TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
2914                         TAILQ_REMOVE(&rlh, lyp, nfsly_list);
2915                         NFSCL_DEBUG(4, "ret layout\n");
2916                         nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
2917                         nfscl_freelayout(lyp);
2918                 }
2919
2920                 /*
2921                  * Delegreturn any delegations cleaned out or recalled.
2922                  */
2923                 TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
2924                         newnfs_copycred(&dp->nfsdl_cred, cred);
2925                         (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
2926                         TAILQ_REMOVE(&dh, dp, nfsdl_list);
2927                         free(dp, M_NFSCLDELEG);
2928                 }
2929
2930                 SLIST_INIT(&lfh);
2931                 /*
2932                  * Call nfscl_cleanupkext() once per second to check for
2933                  * open/lock owners where the process has exited.
2934                  */
2935                 mytime = NFSD_MONOSEC;
2936                 if (prevsec != mytime) {
2937                         prevsec = mytime;
2938                         nfscl_cleanupkext(clp, &lfh);
2939                 }
2940
2941                 /*
2942                  * Do a ReleaseLockOwner for all lock owners where the
2943                  * associated process no longer exists, as found by
2944                  * nfscl_cleanupkext().
2945                  */
2946                 newnfs_setroot(cred);
2947                 SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
2948                         LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
2949                             nlp) {
2950                                 (void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
2951                                     lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
2952                                     p);
2953                                 nfscl_freelockowner(lp, 0);
2954                         }
2955                         free(lfhp, M_TEMP);
2956                 }
2957                 SLIST_INIT(&lfh);
2958
2959                 NFSLOCKCLSTATE();
2960                 if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
2961                         (void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
2962                             hz);
2963 terminate:
2964                 if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
2965                         clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
2966                         NFSUNLOCKCLSTATE();
2967                         NFSFREECRED(cred);
2968                         wakeup((caddr_t)clp);
2969                         return;
2970                 }
2971                 NFSUNLOCKCLSTATE();
2972         }
2973 }
2974
2975 /*
2976  * Initiate state recovery. Called when NFSERR_STALECLIENTID,
2977  * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
2978  */
2979 void
2980 nfscl_initiate_recovery(struct nfsclclient *clp)
2981 {
2982
2983         if (clp == NULL)
2984                 return;
2985         NFSLOCKCLSTATE();
2986         clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
2987         NFSUNLOCKCLSTATE();
2988         wakeup((caddr_t)clp);
2989 }
2990
2991 /*
2992  * Dump out the state stuff for debugging.
2993  */
2994 void
2995 nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
2996     int lockowner, int locks)
2997 {
2998         struct nfsclclient *clp;
2999         struct nfsclowner *owp;
3000         struct nfsclopen *op;
3001         struct nfscllockowner *lp;
3002         struct nfscllock *lop;
3003         struct nfscldeleg *dp;
3004
3005         clp = nmp->nm_clp;
3006         if (clp == NULL) {
3007                 printf("nfscl dumpstate NULL clp\n");
3008                 return;
3009         }
3010         NFSLOCKCLSTATE();
3011         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
3012           LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3013             if (openowner && !LIST_EMPTY(&owp->nfsow_open))
3014                 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
3015                     owp->nfsow_owner[0], owp->nfsow_owner[1],
3016                     owp->nfsow_owner[2], owp->nfsow_owner[3],
3017                     owp->nfsow_seqid);
3018             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3019                 if (opens)
3020                     printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
3021                         op->nfso_stateid.other[0], op->nfso_stateid.other[1],
3022                         op->nfso_stateid.other[2], op->nfso_opencnt,
3023                         op->nfso_fh[12]);
3024                 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
3025                     if (lockowner)
3026                         printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
3027                             lp->nfsl_owner[0], lp->nfsl_owner[1],
3028                             lp->nfsl_owner[2], lp->nfsl_owner[3],
3029                             lp->nfsl_seqid,
3030                             lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
3031                             lp->nfsl_stateid.other[2]);
3032                     LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3033                         if (locks)
3034 #ifdef __FreeBSD__
3035                             printf("lck typ=%d fst=%ju end=%ju\n",
3036                                 lop->nfslo_type, (intmax_t)lop->nfslo_first,
3037                                 (intmax_t)lop->nfslo_end);
3038 #else
3039                             printf("lck typ=%d fst=%qd end=%qd\n",
3040                                 lop->nfslo_type, lop->nfslo_first,
3041                                 lop->nfslo_end);
3042 #endif
3043                     }
3044                 }
3045             }
3046           }
3047         }
3048         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3049             if (openowner && !LIST_EMPTY(&owp->nfsow_open))
3050                 printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
3051                     owp->nfsow_owner[0], owp->nfsow_owner[1],
3052                     owp->nfsow_owner[2], owp->nfsow_owner[3],
3053                     owp->nfsow_seqid);
3054             LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3055                 if (opens)
3056                     printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
3057                         op->nfso_stateid.other[0], op->nfso_stateid.other[1],
3058                         op->nfso_stateid.other[2], op->nfso_opencnt,
3059                         op->nfso_fh[12]);
3060                 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
3061                     if (lockowner)
3062                         printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
3063                             lp->nfsl_owner[0], lp->nfsl_owner[1],
3064                             lp->nfsl_owner[2], lp->nfsl_owner[3],
3065                             lp->nfsl_seqid,
3066                             lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
3067                             lp->nfsl_stateid.other[2]);
3068                     LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3069                         if (locks)
3070 #ifdef __FreeBSD__
3071                             printf("lck typ=%d fst=%ju end=%ju\n",
3072                                 lop->nfslo_type, (intmax_t)lop->nfslo_first,
3073                                 (intmax_t)lop->nfslo_end);
3074 #else
3075                             printf("lck typ=%d fst=%qd end=%qd\n",
3076                                 lop->nfslo_type, lop->nfslo_first,
3077                                 lop->nfslo_end);
3078 #endif
3079                     }
3080                 }
3081             }
3082         }
3083         NFSUNLOCKCLSTATE();
3084 }
3085
3086 /*
3087  * Check for duplicate open owners and opens.
3088  * (Only used as a diagnostic aid.)
3089  */
3090 void
3091 nfscl_dupopen(vnode_t vp, int dupopens)
3092 {
3093         struct nfsclclient *clp;
3094         struct nfsclowner *owp, *owp2;
3095         struct nfsclopen *op, *op2;
3096         struct nfsfh *nfhp;
3097
3098         clp = VFSTONFS(vp->v_mount)->nm_clp;
3099         if (clp == NULL) {
3100                 printf("nfscl dupopen NULL clp\n");
3101                 return;
3102         }
3103         nfhp = VTONFS(vp)->n_fhp;
3104         NFSLOCKCLSTATE();
3105
3106         /*
3107          * First, search for duplicate owners.
3108          * These should never happen!
3109          */
3110         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3111             LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3112                 if (owp != owp2 &&
3113                     !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
3114                     NFSV4CL_LOCKNAMELEN)) {
3115                         NFSUNLOCKCLSTATE();
3116                         printf("DUP OWNER\n");
3117                         nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3118                         return;
3119                 }
3120             }
3121         }
3122
3123         /*
3124          * Now, search for duplicate stateids.
3125          * These shouldn't happen, either.
3126          */
3127         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3128             LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3129                 LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3130                     LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3131                         if (op != op2 &&
3132                             (op->nfso_stateid.other[0] != 0 ||
3133                              op->nfso_stateid.other[1] != 0 ||
3134                              op->nfso_stateid.other[2] != 0) &&
3135                             op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
3136                             op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
3137                             op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
3138                             NFSUNLOCKCLSTATE();
3139                             printf("DUP STATEID\n");
3140                             nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0, 0);
3141                             return;
3142                         }
3143                     }
3144                 }
3145             }
3146         }
3147
3148         /*
3149          * Now search for duplicate opens.
3150          * Duplicate opens for the same owner
3151          * should never occur. Other duplicates are
3152          * possible and are checked for if "dupopens"
3153          * is true.
3154          */
3155         LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
3156             LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
3157                 if (nfhp->nfh_len == op2->nfso_fhlen &&
3158                     !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
3159                     LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3160                         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3161                             if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
3162                                 !NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
3163                                 (!NFSBCMP(op->nfso_own->nfsow_owner,
3164                                  op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
3165                                  dupopens)) {
3166                                 if (!NFSBCMP(op->nfso_own->nfsow_owner,
3167                                     op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
3168                                     NFSUNLOCKCLSTATE();
3169                                     printf("BADDUP OPEN\n");
3170                                 } else {
3171                                     NFSUNLOCKCLSTATE();
3172                                     printf("DUP OPEN\n");
3173                                 }
3174                                 nfscl_dumpstate(VFSTONFS(vp->v_mount), 1, 1, 0,
3175                                     0);
3176                                 return;
3177                             }
3178                         }
3179                     }
3180                 }
3181             }
3182         }
3183         NFSUNLOCKCLSTATE();
3184 }
3185
3186 /*
3187  * During close, find an open that needs to be dereferenced and
3188  * dereference it. If there are no more opens for this file,
3189  * log a message to that effect.
3190  * Opens aren't actually Close'd until VOP_INACTIVE() is performed
3191  * on the file's vnode.
3192  * This is the safe way, since it is difficult to identify
3193  * which open the close is for and I/O can be performed after the
3194  * close(2) system call when a file is mmap'd.
3195  * If it returns 0 for success, there will be a referenced
3196  * clp returned via clpp.
3197  */
3198 int
3199 nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
3200 {
3201         struct nfsclclient *clp;
3202         struct nfsclowner *owp;
3203         struct nfsclopen *op;
3204         struct nfscldeleg *dp;
3205         struct nfsfh *nfhp;
3206         int error, notdecr;
3207
3208         error = nfscl_getcl(vp->v_mount, NULL, NULL, 1, &clp);
3209         if (error)
3210                 return (error);
3211         *clpp = clp;
3212
3213         nfhp = VTONFS(vp)->n_fhp;
3214         notdecr = 1;
3215         NFSLOCKCLSTATE();
3216         /*
3217          * First, look for one under a delegation that was locally issued
3218          * and just decrement the opencnt for it. Since all my Opens against
3219          * the server are DENY_NONE, I don't see a problem with hanging
3220          * onto them. (It is much easier to use one of the extant Opens
3221          * that I already have on the server when a Delegation is recalled
3222          * than to do fresh Opens.) Someday, I might need to rethink this, but.
3223          */
3224         dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3225         if (dp != NULL) {
3226                 LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
3227                         op = LIST_FIRST(&owp->nfsow_open);
3228                         if (op != NULL) {
3229                                 /*
3230                                  * Since a delegation is for a file, there
3231                                  * should never be more than one open for
3232                                  * each openowner.
3233                                  */
3234                                 if (LIST_NEXT(op, nfso_list) != NULL)
3235                                         panic("nfscdeleg opens");
3236                                 if (notdecr && op->nfso_opencnt > 0) {
3237                                         notdecr = 0;
3238                                         op->nfso_opencnt--;
3239                                         break;
3240                                 }
3241                         }
3242                 }
3243         }
3244
3245         /* Now process the opens against the server. */
3246         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3247                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3248                         if (op->nfso_fhlen == nfhp->nfh_len &&
3249                             !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3250                             nfhp->nfh_len)) {
3251                                 /* Found an open, decrement cnt if possible */
3252                                 if (notdecr && op->nfso_opencnt > 0) {
3253                                         notdecr = 0;
3254                                         op->nfso_opencnt--;
3255                                 }
3256                                 /*
3257                                  * There are more opens, so just return.
3258                                  */
3259                                 if (op->nfso_opencnt > 0) {
3260                                         NFSUNLOCKCLSTATE();
3261                                         return (0);
3262                                 }
3263                         }
3264                 }
3265         }
3266         NFSUNLOCKCLSTATE();
3267         if (notdecr)
3268                 printf("nfscl: never fnd open\n");
3269         return (0);
3270 }
3271
3272 int
3273 nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
3274 {
3275         struct nfsclclient *clp;
3276         struct nfsclowner *owp, *nowp;
3277         struct nfsclopen *op;
3278         struct nfscldeleg *dp;
3279         struct nfsfh *nfhp;
3280         struct nfsclrecalllayout *recallp;
3281         int error;
3282
3283         error = nfscl_getcl(vp->v_mount, NULL, NULL, 1, &clp);
3284         if (error)
3285                 return (error);
3286         *clpp = clp;
3287
3288         nfhp = VTONFS(vp)->n_fhp;
3289         recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
3290         NFSLOCKCLSTATE();
3291         /*
3292          * First get rid of the local Open structures, which should be no
3293          * longer in use.
3294          */
3295         dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
3296         if (dp != NULL) {
3297                 LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
3298                         op = LIST_FIRST(&owp->nfsow_open);
3299                         if (op != NULL) {
3300                                 KASSERT((op->nfso_opencnt == 0),
3301                                     ("nfscl: bad open cnt on deleg"));
3302                                 nfscl_freeopen(op, 1);
3303                         }
3304                         nfscl_freeopenowner(owp, 1);
3305                 }
3306         }
3307
3308         /* Return any layouts marked return on close. */
3309         nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp);
3310
3311         /* Now process the opens against the server. */
3312 lookformore:
3313         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3314                 op = LIST_FIRST(&owp->nfsow_open);
3315                 while (op != NULL) {
3316                         if (op->nfso_fhlen == nfhp->nfh_len &&
3317                             !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
3318                             nfhp->nfh_len)) {
3319                                 /* Found an open, close it. */
3320 #ifdef DIAGNOSTIC
3321                                 KASSERT((op->nfso_opencnt == 0),
3322                                     ("nfscl: bad open cnt on server (%d)",
3323                                      op->nfso_opencnt));
3324 #endif
3325                                 NFSUNLOCKCLSTATE();
3326                                 nfsrpc_doclose(VFSTONFS(vp->v_mount), op, p);
3327                                 NFSLOCKCLSTATE();
3328                                 goto lookformore;
3329                         }
3330                         op = LIST_NEXT(op, nfso_list);
3331                 }
3332         }
3333         NFSUNLOCKCLSTATE();
3334         /*
3335          * recallp has been set NULL by nfscl_retoncloselayout() if it was
3336          * used by the function, but calling free() with a NULL pointer is ok.
3337          */
3338         free(recallp, M_NFSLAYRECALL);
3339         return (0);
3340 }
3341
3342 /*
3343  * Return all delegations on this client.
3344  * (Must be called with client sleep lock.)
3345  */
3346 static void
3347 nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
3348 {
3349         struct nfscldeleg *dp, *ndp;
3350         struct ucred *cred;
3351
3352         cred = newnfs_getcred();
3353         TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
3354                 nfscl_cleandeleg(dp);
3355                 (void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3356                 nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
3357         }
3358         NFSFREECRED(cred);
3359 }
3360
3361 /*
3362  * Return any delegation for this vp.
3363  */
3364 void
3365 nfscl_delegreturnvp(vnode_t vp, NFSPROC_T *p)
3366 {
3367         struct nfsclclient *clp;
3368         struct nfscldeleg *dp;
3369         struct ucred *cred;
3370         struct nfsnode *np;
3371
3372         np = VTONFS(vp);
3373         cred = newnfs_getcred();
3374         dp = NULL;
3375         NFSLOCKCLSTATE();
3376         clp = VFSTONFS(vp->v_mount)->nm_clp;
3377         if (clp != NULL)
3378                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
3379                     np->n_fhp->nfh_len);
3380         if (dp != NULL) {
3381                 nfscl_cleandeleg(dp);
3382                 nfscl_freedeleg(&clp->nfsc_deleg, dp, false);
3383                 NFSUNLOCKCLSTATE();
3384                 newnfs_copycred(&dp->nfsdl_cred, cred);
3385                 nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
3386                 free(dp, M_NFSCLDELEG);
3387         } else
3388                 NFSUNLOCKCLSTATE();
3389         NFSFREECRED(cred);
3390 }
3391
3392 /*
3393  * Do a callback RPC.
3394  */
3395 void
3396 nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
3397 {
3398         int clist, gotseq_ok, i, j, k, op, rcalls;
3399         u_int32_t *tl;
3400         struct nfsclclient *clp;
3401         struct nfscldeleg *dp = NULL;
3402         int numops, taglen = -1, error = 0, trunc __unused;
3403         u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
3404         u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
3405         vnode_t vp = NULL;
3406         struct nfsnode *np;
3407         struct vattr va;
3408         struct nfsfh *nfhp;
3409         mount_t mp;
3410         nfsattrbit_t attrbits, rattrbits;
3411         nfsv4stateid_t stateid;
3412         uint32_t seqid, slotid = 0, highslot, cachethis __unused;
3413         uint8_t sessionid[NFSX_V4SESSIONID];
3414         struct mbuf *rep;
3415         struct nfscllayout *lyp;
3416         uint64_t filesid[2], len, off;
3417         int changed, gotone, laytype, recalltype;
3418         uint32_t iomode;
3419         struct nfsclrecalllayout *recallp = NULL;
3420         struct nfsclsession *tsep;
3421
3422         gotseq_ok = 0;
3423         nfsrvd_rephead(nd);
3424         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3425         taglen = fxdr_unsigned(int, *tl);
3426         if (taglen < 0) {
3427                 error = EBADRPC;
3428                 goto nfsmout;
3429         }
3430         if (taglen <= NFSV4_SMALLSTR)
3431                 tagstr = tag;
3432         else
3433                 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
3434         error = nfsrv_mtostr(nd, tagstr, taglen);
3435         if (error) {
3436                 if (taglen > NFSV4_SMALLSTR)
3437                         free(tagstr, M_TEMP);
3438                 taglen = -1;
3439                 goto nfsmout;
3440         }
3441         (void) nfsm_strtom(nd, tag, taglen);
3442         if (taglen > NFSV4_SMALLSTR) {
3443                 free(tagstr, M_TEMP);
3444         }
3445         NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
3446         NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3447         minorvers = fxdr_unsigned(u_int32_t, *tl++);
3448         if (minorvers != NFSV4_MINORVERSION &&
3449             minorvers != NFSV41_MINORVERSION &&
3450             minorvers != NFSV42_MINORVERSION)
3451                 nd->nd_repstat = NFSERR_MINORVERMISMATCH;
3452         cbident = fxdr_unsigned(u_int32_t, *tl++);
3453         if (nd->nd_repstat)
3454                 numops = 0;
3455         else
3456                 numops = fxdr_unsigned(int, *tl);
3457         /*
3458          * Loop around doing the sub ops.
3459          */
3460         for (i = 0; i < numops; i++) {
3461                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3462                 NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
3463                 *repp++ = *tl;
3464                 op = fxdr_unsigned(int, *tl);
3465                 if (op < NFSV4OP_CBGETATTR ||
3466                    (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
3467                    (op > NFSV4OP_CBNOTIFYDEVID &&
3468                     minorvers == NFSV41_MINORVERSION) ||
3469                    (op > NFSV4OP_CBOFFLOAD &&
3470                     minorvers == NFSV42_MINORVERSION)) {
3471                     nd->nd_repstat = NFSERR_OPILLEGAL;
3472                     *repp = nfscl_errmap(nd, minorvers);
3473                     retops++;
3474                     break;
3475                 }
3476                 nd->nd_procnum = op;
3477                 if (op < NFSV42_CBNOPS)
3478                         nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
3479                 switch (op) {
3480                 case NFSV4OP_CBGETATTR:
3481                         NFSCL_DEBUG(4, "cbgetattr\n");
3482                         mp = NULL;
3483                         vp = NULL;
3484                         error = nfsm_getfh(nd, &nfhp);
3485                         if (!error)
3486                                 error = nfsrv_getattrbits(nd, &attrbits,
3487                                     NULL, NULL);
3488                         if (error == 0 && i == 0 &&
3489                             minorvers != NFSV4_MINORVERSION)
3490                                 error = NFSERR_OPNOTINSESS;
3491                         if (!error) {
3492                                 mp = nfscl_getmnt(minorvers, sessionid, cbident,
3493                                     &clp);
3494                                 if (mp == NULL)
3495                                         error = NFSERR_SERVERFAULT;
3496                         }
3497                         if (!error) {
3498                                 error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
3499                                     nfhp->nfh_len, p, &np);
3500                                 if (!error)
3501                                         vp = NFSTOV(np);
3502                         }
3503                         if (!error) {
3504                                 NFSZERO_ATTRBIT(&rattrbits);
3505                                 NFSLOCKCLSTATE();
3506                                 dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3507                                     nfhp->nfh_len);
3508                                 if (dp != NULL) {
3509                                         if (NFSISSET_ATTRBIT(&attrbits,
3510                                             NFSATTRBIT_SIZE)) {
3511                                                 if (vp != NULL)
3512                                                         va.va_size = np->n_size;
3513                                                 else
3514                                                         va.va_size =
3515                                                             dp->nfsdl_size;
3516                                                 NFSSETBIT_ATTRBIT(&rattrbits,
3517                                                     NFSATTRBIT_SIZE);
3518                                         }
3519                                         if (NFSISSET_ATTRBIT(&attrbits,
3520                                             NFSATTRBIT_CHANGE)) {
3521                                                 va.va_filerev =
3522                                                     dp->nfsdl_change;
3523                                                 if (vp == NULL ||
3524                                                     (np->n_flag & NDELEGMOD))
3525                                                         va.va_filerev++;
3526                                                 NFSSETBIT_ATTRBIT(&rattrbits,
3527                                                     NFSATTRBIT_CHANGE);
3528                                         }
3529                                 } else
3530                                         error = NFSERR_SERVERFAULT;
3531                                 NFSUNLOCKCLSTATE();
3532                         }
3533                         if (vp != NULL)
3534                                 vrele(vp);
3535                         if (mp != NULL)
3536                                 vfs_unbusy(mp);
3537                         if (nfhp != NULL)
3538                                 free(nfhp, M_NFSFH);
3539                         if (!error)
3540                                 (void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
3541                                     NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
3542                                     (uint64_t)0, NULL);
3543                         break;
3544                 case NFSV4OP_CBRECALL:
3545                         NFSCL_DEBUG(4, "cbrecall\n");
3546                         NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
3547                             NFSX_UNSIGNED);
3548                         stateid.seqid = *tl++;
3549                         NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
3550                             NFSX_STATEIDOTHER);
3551                         tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
3552                         trunc = fxdr_unsigned(int, *tl);
3553                         error = nfsm_getfh(nd, &nfhp);
3554                         if (error == 0 && i == 0 &&
3555                             minorvers != NFSV4_MINORVERSION)
3556                                 error = NFSERR_OPNOTINSESS;
3557                         if (!error) {
3558                                 NFSLOCKCLSTATE();
3559                                 if (minorvers == NFSV4_MINORVERSION)
3560                                         clp = nfscl_getclnt(cbident);
3561                                 else
3562                                         clp = nfscl_getclntsess(sessionid);
3563                                 if (clp != NULL) {
3564                                         dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
3565                                             nfhp->nfh_len);
3566                                         if (dp != NULL && (dp->nfsdl_flags &
3567                                             NFSCLDL_DELEGRET) == 0) {
3568                                                 dp->nfsdl_flags |=
3569                                                     NFSCLDL_RECALL;
3570                                                 wakeup((caddr_t)clp);
3571                                         }
3572                                 } else {
3573                                         error = NFSERR_SERVERFAULT;
3574                                 }
3575                                 NFSUNLOCKCLSTATE();
3576                         }
3577                         if (nfhp != NULL)
3578                                 free(nfhp, M_NFSFH);
3579                         break;
3580                 case NFSV4OP_CBLAYOUTRECALL:
3581                         NFSCL_DEBUG(4, "cblayrec\n");
3582                         nfhp = NULL;
3583                         NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
3584                         laytype = fxdr_unsigned(int, *tl++);
3585                         iomode = fxdr_unsigned(uint32_t, *tl++);
3586                         if (newnfs_true == *tl++)
3587                                 changed = 1;
3588                         else
3589                                 changed = 0;
3590                         recalltype = fxdr_unsigned(int, *tl);
3591                         NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
3592                             laytype, iomode, changed, recalltype);
3593                         recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
3594                             M_WAITOK);
3595                         if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
3596                             laytype != NFSLAYOUT_FLEXFILE)
3597                                 error = NFSERR_NOMATCHLAYOUT;
3598                         else if (recalltype == NFSLAYOUTRETURN_FILE) {
3599                                 error = nfsm_getfh(nd, &nfhp);
3600                                 NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
3601                                 if (error != 0)
3602                                         goto nfsmout;
3603                                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
3604                                     NFSX_STATEID);
3605                                 off = fxdr_hyper(tl); tl += 2;
3606                                 len = fxdr_hyper(tl); tl += 2;
3607                                 stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
3608                                 NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
3609                                 if (minorvers == NFSV4_MINORVERSION)
3610                                         error = NFSERR_NOTSUPP;
3611                                 else if (i == 0)
3612                                         error = NFSERR_OPNOTINSESS;
3613                                 NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
3614                                     (uintmax_t)off, (uintmax_t)len,
3615                                     stateid.seqid, error);
3616                                 if (error == 0) {
3617                                         NFSLOCKCLSTATE();
3618                                         clp = nfscl_getclntsess(sessionid);
3619                                         NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
3620                                         if (clp != NULL) {
3621                                                 lyp = nfscl_findlayout(clp,
3622                                                     nfhp->nfh_fh,
3623                                                     nfhp->nfh_len);
3624                                                 NFSCL_DEBUG(4, "cblyp=%p\n",
3625                                                     lyp);
3626                                                 if (lyp != NULL &&
3627                                                     (lyp->nfsly_flags &
3628                                                      (NFSLY_FILES |
3629                                                       NFSLY_FLEXFILE)) != 0 &&
3630                                                     !NFSBCMP(stateid.other,
3631                                                     lyp->nfsly_stateid.other,
3632                                                     NFSX_STATEIDOTHER)) {
3633                                                         error =
3634                                                             nfscl_layoutrecall(
3635                                                             recalltype,
3636                                                             lyp, iomode, off,
3637                                                             len, stateid.seqid,
3638                                                             0, 0, NULL,
3639                                                             recallp);
3640                                                         if (error == 0 &&
3641                                                             stateid.seqid >
3642                                                             lyp->nfsly_stateid.seqid)
3643                                                                 lyp->nfsly_stateid.seqid =
3644                                                                     stateid.seqid;
3645                                                         recallp = NULL;
3646                                                         wakeup(clp);
3647                                                         NFSCL_DEBUG(4,
3648                                                             "aft layrcal=%d "
3649                                                             "layseqid=%d\n",
3650                                                             error,
3651                                                             lyp->nfsly_stateid.seqid);
3652                                                 } else
3653                                                         error =
3654                                                           NFSERR_NOMATCHLAYOUT;
3655                                         } else
3656                                                 error = NFSERR_NOMATCHLAYOUT;
3657                                         NFSUNLOCKCLSTATE();
3658                                 }
3659                                 free(nfhp, M_NFSFH);
3660                         } else if (recalltype == NFSLAYOUTRETURN_FSID) {
3661                                 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
3662                                 filesid[0] = fxdr_hyper(tl); tl += 2;
3663                                 filesid[1] = fxdr_hyper(tl); tl += 2;
3664                                 gotone = 0;
3665                                 NFSLOCKCLSTATE();
3666                                 clp = nfscl_getclntsess(sessionid);
3667                                 if (clp != NULL) {
3668                                         TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3669                                             nfsly_list) {
3670                                                 if (lyp->nfsly_filesid[0] ==
3671                                                     filesid[0] &&
3672                                                     lyp->nfsly_filesid[1] ==
3673                                                     filesid[1]) {
3674                                                         error =
3675                                                             nfscl_layoutrecall(
3676                                                             recalltype,
3677                                                             lyp, iomode, 0,
3678                                                             UINT64_MAX,
3679                                                             lyp->nfsly_stateid.seqid,
3680                                                             0, 0, NULL,
3681                                                             recallp);
3682                                                         recallp = NULL;
3683                                                         gotone = 1;
3684                                                 }
3685                                         }
3686                                         if (gotone != 0)
3687                                                 wakeup(clp);
3688                                         else
3689                                                 error = NFSERR_NOMATCHLAYOUT;
3690                                 } else
3691                                         error = NFSERR_NOMATCHLAYOUT;
3692                                 NFSUNLOCKCLSTATE();
3693                         } else if (recalltype == NFSLAYOUTRETURN_ALL) {
3694                                 gotone = 0;
3695                                 NFSLOCKCLSTATE();
3696                                 clp = nfscl_getclntsess(sessionid);
3697                                 if (clp != NULL) {
3698                                         TAILQ_FOREACH(lyp, &clp->nfsc_layout,
3699                                             nfsly_list) {
3700                                                 error = nfscl_layoutrecall(
3701                                                     recalltype, lyp, iomode, 0,
3702                                                     UINT64_MAX,
3703                                                     lyp->nfsly_stateid.seqid,
3704                                                     0, 0, NULL, recallp);
3705                                                 recallp = NULL;
3706                                                 gotone = 1;
3707                                         }
3708                                         if (gotone != 0)
3709                                                 wakeup(clp);
3710                                         else
3711                                                 error = NFSERR_NOMATCHLAYOUT;
3712                                 } else
3713                                         error = NFSERR_NOMATCHLAYOUT;
3714                                 NFSUNLOCKCLSTATE();
3715                         } else
3716                                 error = NFSERR_NOMATCHLAYOUT;
3717                         if (recallp != NULL) {
3718                                 free(recallp, M_NFSLAYRECALL);
3719                                 recallp = NULL;
3720                         }
3721                         break;
3722                 case NFSV4OP_CBSEQUENCE:
3723                         NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3724                             5 * NFSX_UNSIGNED);
3725                         bcopy(tl, sessionid, NFSX_V4SESSIONID);
3726                         tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3727                         seqid = fxdr_unsigned(uint32_t, *tl++);
3728                         slotid = fxdr_unsigned(uint32_t, *tl++);
3729                         highslot = fxdr_unsigned(uint32_t, *tl++);
3730                         cachethis = *tl++;
3731                         /* Throw away the referring call stuff. */
3732                         clist = fxdr_unsigned(int, *tl);
3733                         for (j = 0; j < clist; j++) {
3734                                 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
3735                                     NFSX_UNSIGNED);
3736                                 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3737                                 rcalls = fxdr_unsigned(int, *tl);
3738                                 for (k = 0; k < rcalls; k++) {
3739                                         NFSM_DISSECT(tl, uint32_t *,
3740                                             2 * NFSX_UNSIGNED);
3741                                 }
3742                         }
3743                         NFSLOCKCLSTATE();
3744                         if (i == 0) {
3745                                 clp = nfscl_getclntsess(sessionid);
3746                                 if (clp == NULL)
3747                                         error = NFSERR_SERVERFAULT;
3748                         } else
3749                                 error = NFSERR_SEQUENCEPOS;
3750                         if (error == 0) {
3751                                 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3752                                 error = nfsv4_seqsession(seqid, slotid,
3753                                     highslot, tsep->nfsess_cbslots, &rep,
3754                                     tsep->nfsess_backslots);
3755                         }
3756                         NFSUNLOCKCLSTATE();
3757                         if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
3758                                 gotseq_ok = 1;
3759                                 if (rep != NULL) {
3760                                         /*
3761                                          * Handle a reply for a retried
3762                                          * callback.  The reply will be
3763                                          * re-inserted in the session cache
3764                                          * by the nfsv4_seqsess_cacherep() call
3765                                          * after out:
3766                                          */
3767                                         KASSERT(error == NFSERR_REPLYFROMCACHE,
3768                                             ("cbsequence: non-NULL rep"));
3769                                         NFSCL_DEBUG(4, "Got cbretry\n");
3770                                         m_freem(nd->nd_mreq);
3771                                         nd->nd_mreq = rep;
3772                                         rep = NULL;
3773                                         goto out;
3774                                 }
3775                                 NFSM_BUILD(tl, uint32_t *,
3776                                     NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
3777                                 bcopy(sessionid, tl, NFSX_V4SESSIONID);
3778                                 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
3779                                 *tl++ = txdr_unsigned(seqid);
3780                                 *tl++ = txdr_unsigned(slotid);
3781                                 *tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
3782                                 *tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
3783                         }
3784                         break;
3785                 default:
3786                         if (i == 0 && minorvers != NFSV4_MINORVERSION)
3787                                 error = NFSERR_OPNOTINSESS;
3788                         else {
3789                                 NFSCL_DEBUG(1, "unsupp callback %d\n", op);
3790                                 error = NFSERR_NOTSUPP;
3791                         }
3792                         break;
3793                 }
3794                 if (error) {
3795                         if (error == EBADRPC || error == NFSERR_BADXDR) {
3796                                 nd->nd_repstat = NFSERR_BADXDR;
3797                         } else {
3798                                 nd->nd_repstat = error;
3799                         }
3800                         error = 0;
3801                 }
3802                 retops++;
3803                 if (nd->nd_repstat) {
3804                         *repp = nfscl_errmap(nd, minorvers);
3805                         break;
3806                 } else
3807                         *repp = 0;      /* NFS4_OK */
3808         }
3809 nfsmout:
3810         if (recallp != NULL)
3811                 free(recallp, M_NFSLAYRECALL);
3812         if (error) {
3813                 if (error == EBADRPC || error == NFSERR_BADXDR)
3814                         nd->nd_repstat = NFSERR_BADXDR;
3815                 else
3816                         printf("nfsv4 comperr1=%d\n", error);
3817         }
3818         if (taglen == -1) {
3819                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3820                 *tl++ = 0;
3821                 *tl = 0;
3822         } else {
3823                 *retopsp = txdr_unsigned(retops);
3824         }
3825         *nd->nd_errp = nfscl_errmap(nd, minorvers);
3826 out:
3827         if (gotseq_ok != 0) {
3828                 rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
3829                 NFSLOCKCLSTATE();
3830                 clp = nfscl_getclntsess(sessionid);
3831                 if (clp != NULL) {
3832                         tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3833                         nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
3834                             NFSERR_OK, &rep);
3835                         NFSUNLOCKCLSTATE();
3836                 } else {
3837                         NFSUNLOCKCLSTATE();
3838                         m_freem(rep);
3839                 }
3840         }
3841 }
3842
3843 /*
3844  * Generate the next cbident value. Basically just increment a static value
3845  * and then check that it isn't already in the list, if it has wrapped around.
3846  */
3847 static u_int32_t
3848 nfscl_nextcbident(void)
3849 {
3850         struct nfsclclient *clp;
3851         int matched;
3852         static u_int32_t nextcbident = 0;
3853         static int haswrapped = 0;
3854
3855         nextcbident++;
3856         if (nextcbident == 0)
3857                 haswrapped = 1;
3858         if (haswrapped) {
3859                 /*
3860                  * Search the clientid list for one already using this cbident.
3861                  */
3862                 do {
3863                         matched = 0;
3864                         NFSLOCKCLSTATE();
3865                         LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3866                                 if (clp->nfsc_cbident == nextcbident) {
3867                                         matched = 1;
3868                                         break;
3869                                 }
3870                         }
3871                         NFSUNLOCKCLSTATE();
3872                         if (matched == 1)
3873                                 nextcbident++;
3874                 } while (matched);
3875         }
3876         return (nextcbident);
3877 }
3878
3879 /*
3880  * Get the mount point related to a given cbident or session and busy it.
3881  */
3882 static mount_t
3883 nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
3884     struct nfsclclient **clpp)
3885 {
3886         struct nfsclclient *clp;
3887         mount_t mp;
3888         int error;
3889         struct nfsclsession *tsep;
3890
3891         *clpp = NULL;
3892         NFSLOCKCLSTATE();
3893         LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3894                 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3895                 if (minorvers == NFSV4_MINORVERSION) {
3896                         if (clp->nfsc_cbident == cbident)
3897                                 break;
3898                 } else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3899                     NFSX_V4SESSIONID))
3900                         break;
3901         }
3902         if (clp == NULL) {
3903                 NFSUNLOCKCLSTATE();
3904                 return (NULL);
3905         }
3906         mp = clp->nfsc_nmp->nm_mountp;
3907         vfs_ref(mp);
3908         NFSUNLOCKCLSTATE();
3909         error = vfs_busy(mp, 0);
3910         vfs_rel(mp);
3911         if (error != 0)
3912                 return (NULL);
3913         *clpp = clp;
3914         return (mp);
3915 }
3916
3917 /*
3918  * Get the clientid pointer related to a given cbident.
3919  */
3920 static struct nfsclclient *
3921 nfscl_getclnt(u_int32_t cbident)
3922 {
3923         struct nfsclclient *clp;
3924
3925         LIST_FOREACH(clp, &nfsclhead, nfsc_list)
3926                 if (clp->nfsc_cbident == cbident)
3927                         break;
3928         return (clp);
3929 }
3930
3931 /*
3932  * Get the clientid pointer related to a given sessionid.
3933  */
3934 static struct nfsclclient *
3935 nfscl_getclntsess(uint8_t *sessionid)
3936 {
3937         struct nfsclclient *clp;
3938         struct nfsclsession *tsep;
3939
3940         LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
3941                 tsep = nfsmnt_mdssession(clp->nfsc_nmp);
3942                 if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
3943                     NFSX_V4SESSIONID))
3944                         break;
3945         }
3946         return (clp);
3947 }
3948
3949 /*
3950  * Search for a lock conflict locally on the client. A conflict occurs if
3951  * - not same owner and overlapping byte range and at least one of them is
3952  *   a write lock or this is an unlock.
3953  */
3954 static int
3955 nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
3956     struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
3957     struct nfscllock **lopp)
3958 {
3959         struct nfsclowner *owp;
3960         struct nfsclopen *op;
3961         int ret;
3962
3963         if (dp != NULL) {
3964                 ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
3965                 if (ret)
3966                         return (ret);
3967         }
3968         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
3969                 LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
3970                         if (op->nfso_fhlen == fhlen &&
3971                             !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
3972                                 ret = nfscl_checkconflict(&op->nfso_lock, nlop,
3973                                     own, lopp);
3974                                 if (ret)
3975                                         return (ret);
3976                         }
3977                 }
3978         }
3979         return (0);
3980 }
3981
3982 static int
3983 nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
3984     u_int8_t *own, struct nfscllock **lopp)
3985 {
3986         struct nfscllockowner *lp;
3987         struct nfscllock *lop;
3988
3989         LIST_FOREACH(lp, lhp, nfsl_list) {
3990                 if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
3991                         LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
3992                                 if (lop->nfslo_first >= nlop->nfslo_end)
3993                                         break;
3994                                 if (lop->nfslo_end <= nlop->nfslo_first)
3995                                         continue;
3996                                 if (lop->nfslo_type == F_WRLCK ||
3997                                     nlop->nfslo_type == F_WRLCK ||
3998                                     nlop->nfslo_type == F_UNLCK) {
3999                                         if (lopp != NULL)
4000                                                 *lopp = lop;
4001                                         return (NFSERR_DENIED);
4002                                 }
4003                         }
4004                 }
4005         }
4006         return (0);
4007 }
4008
4009 /*
4010  * Check for a local conflicting lock.
4011  */
4012 int
4013 nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
4014     u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
4015 {
4016         struct nfscllock *lop, nlck;
4017         struct nfscldeleg *dp;
4018         struct nfsnode *np;
4019         u_int8_t own[NFSV4CL_LOCKNAMELEN];
4020         int error;
4021
4022         nlck.nfslo_type = fl->l_type;
4023         nlck.nfslo_first = off;
4024         if (len == NFS64BITSSET) {
4025                 nlck.nfslo_end = NFS64BITSSET;
4026         } else {
4027                 nlck.nfslo_end = off + len;
4028                 if (nlck.nfslo_end <= nlck.nfslo_first)
4029                         return (NFSERR_INVAL);
4030         }
4031         np = VTONFS(vp);
4032         nfscl_filllockowner(id, own, flags);
4033         NFSLOCKCLSTATE();
4034         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4035         error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
4036             &nlck, own, dp, &lop);
4037         if (error != 0) {
4038                 fl->l_whence = SEEK_SET;
4039                 fl->l_start = lop->nfslo_first;
4040                 if (lop->nfslo_end == NFS64BITSSET)
4041                         fl->l_len = 0;
4042                 else
4043                         fl->l_len = lop->nfslo_end - lop->nfslo_first;
4044                 fl->l_pid = (pid_t)0;
4045                 fl->l_type = lop->nfslo_type;
4046                 error = -1;                     /* no RPC required */
4047         } else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
4048             fl->l_type == F_RDLCK)) {
4049                 /*
4050                  * The delegation ensures that there isn't a conflicting
4051                  * lock on the server, so return -1 to indicate an RPC
4052                  * isn't required.
4053                  */
4054                 fl->l_type = F_UNLCK;
4055                 error = -1;
4056         }
4057         NFSUNLOCKCLSTATE();
4058         return (error);
4059 }
4060
4061 /*
4062  * Handle Recall of a delegation.
4063  * The clp must be exclusive locked when this is called.
4064  */
4065 static int
4066 nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
4067     struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
4068     int called_from_renewthread, vnode_t *vpp)
4069 {
4070         struct nfsclowner *owp, *lowp, *nowp;
4071         struct nfsclopen *op, *lop;
4072         struct nfscllockowner *lp;
4073         struct nfscllock *lckp;
4074         struct nfsnode *np;
4075         int error = 0, ret;
4076
4077         if (vp == NULL) {
4078                 KASSERT(vpp != NULL, ("nfscl_recalldeleg: vpp NULL"));
4079                 *vpp = NULL;
4080                 /*
4081                  * First, get a vnode for the file. This is needed to do RPCs.
4082                  */
4083                 ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
4084                     dp->nfsdl_fhlen, p, &np);
4085                 if (ret) {
4086                         /*
4087                          * File isn't open, so nothing to move over to the
4088                          * server.
4089                          */
4090                         return (0);
4091                 }
4092                 vp = NFSTOV(np);
4093                 *vpp = vp;
4094         } else {
4095                 np = VTONFS(vp);
4096         }
4097         dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;
4098
4099         /*
4100          * Ok, if it's a write delegation, flush data to the server, so
4101          * that close/open consistency is retained.
4102          */
4103         ret = 0;
4104         NFSLOCKNODE(np);
4105         if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
4106                 np->n_flag |= NDELEGRECALL;
4107                 NFSUNLOCKNODE(np);
4108                 ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
4109                 NFSLOCKNODE(np);
4110                 np->n_flag &= ~NDELEGRECALL;
4111         }
4112         NFSINVALATTRCACHE(np);
4113         NFSUNLOCKNODE(np);
4114         if (ret == EIO && called_from_renewthread != 0) {
4115                 /*
4116                  * If the flush failed with EIO for the renew thread,
4117                  * return now, so that the dirty buffer will be flushed
4118                  * later.
4119                  */
4120                 return (ret);
4121         }
4122
4123         /*
4124          * Now, for each openowner with opens issued locally, move them
4125          * over to state against the server.
4126          */
4127         LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
4128                 lop = LIST_FIRST(&lowp->nfsow_open);
4129                 if (lop != NULL) {
4130                         if (LIST_NEXT(lop, nfso_list) != NULL)
4131                                 panic("nfsdlg mult opens");
4132                         /*
4133                          * Look for the same openowner against the server.
4134                          */
4135                         LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
4136                                 if (!NFSBCMP(lowp->nfsow_owner,
4137                                     owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
4138                                         newnfs_copycred(&dp->nfsdl_cred, cred);
4139                                         ret = nfscl_moveopen(vp, clp, nmp, lop,
4140                                             owp, dp, cred, p);
4141                                         if (ret == NFSERR_STALECLIENTID ||
4142                                             ret == NFSERR_STALEDONTRECOVER ||
4143                                             ret == NFSERR_BADSESSION)
4144                                                 return (ret);
4145                                         if (ret) {
4146                                                 nfscl_freeopen(lop, 1);
4147                                                 if (!error)
4148                                                         error = ret;
4149                                         }
4150                                         break;
4151                                 }
4152                         }
4153
4154                         /*
4155                          * If no openowner found, create one and get an open
4156                          * for it.
4157                          */
4158                         if (owp == NULL) {
4159                                 nowp = malloc(
4160                                     sizeof (struct nfsclowner), M_NFSCLOWNER,
4161                                     M_WAITOK);
4162                                 nfscl_newopen(clp, NULL, &owp, &nowp, &op, 
4163                                     NULL, lowp->nfsow_owner, dp->nfsdl_fh,
4164                                     dp->nfsdl_fhlen, NULL, NULL);
4165                                 newnfs_copycred(&dp->nfsdl_cred, cred);
4166                                 ret = nfscl_moveopen(vp, clp, nmp, lop,
4167                                     owp, dp, cred, p);
4168                                 if (ret) {
4169                                         nfscl_freeopenowner(owp, 0);
4170                                         if (ret == NFSERR_STALECLIENTID ||
4171                                             ret == NFSERR_STALEDONTRECOVER ||
4172                                             ret == NFSERR_BADSESSION)
4173                                                 return (ret);
4174                                         if (ret) {
4175                                                 nfscl_freeopen(lop, 1);
4176                                                 if (!error)
4177                                                         error = ret;
4178                                         }
4179                                 }
4180                         }
4181                 }
4182         }
4183
4184         /*
4185          * Now, get byte range locks for any locks done locally.
4186          */
4187         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4188                 LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
4189                         newnfs_copycred(&dp->nfsdl_cred, cred);
4190                         ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
4191                         if (ret == NFSERR_STALESTATEID ||
4192                             ret == NFSERR_STALEDONTRECOVER ||
4193                             ret == NFSERR_STALECLIENTID ||
4194                             ret == NFSERR_BADSESSION)
4195                                 return (ret);
4196                         if (ret && !error)
4197                                 error = ret;
4198                 }
4199         }
4200         return (error);
4201 }
4202
4203 /*
4204  * Move a locally issued open over to an owner on the state list.
4205  * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
4206  * returns with it unlocked.
4207  */
4208 static int
4209 nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4210     struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
4211     struct ucred *cred, NFSPROC_T *p)
4212 {
4213         struct nfsclopen *op, *nop;
4214         struct nfscldeleg *ndp;
4215         struct nfsnode *np;
4216         int error = 0, newone;
4217
4218         /*
4219          * First, look for an appropriate open, If found, just increment the
4220          * opencnt in it.
4221          */
4222         LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
4223                 if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
4224                     op->nfso_fhlen == lop->nfso_fhlen &&
4225                     !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
4226                         op->nfso_opencnt += lop->nfso_opencnt;
4227                         nfscl_freeopen(lop, 1);
4228                         return (0);
4229                 }
4230         }
4231
4232         /* No appropriate open, so we have to do one against the server. */
4233         np = VTONFS(vp);
4234         nop = malloc(sizeof (struct nfsclopen) +
4235             lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
4236         nop->nfso_hash.le_prev = NULL;
4237         newone = 0;
4238         nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
4239             lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
4240         ndp = dp;
4241         error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
4242             lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
4243             NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
4244         if (error) {
4245                 if (newone)
4246                         nfscl_freeopen(op, 0);
4247         } else {
4248                 op->nfso_mode |= lop->nfso_mode;
4249                 op->nfso_opencnt += lop->nfso_opencnt;
4250                 nfscl_freeopen(lop, 1);
4251         }
4252         if (nop != NULL)
4253                 free(nop, M_NFSCLOPEN);
4254         if (ndp != NULL) {
4255                 /*
4256                  * What should I do with the returned delegation, since the
4257                  * delegation is being recalled? For now, just printf and
4258                  * through it away.
4259                  */
4260                 printf("Moveopen returned deleg\n");
4261                 free(ndp, M_NFSCLDELEG);
4262         }
4263         return (error);
4264 }
4265
4266 /*
4267  * Recall all delegations on this client.
4268  */
4269 static void
4270 nfscl_totalrecall(struct nfsclclient *clp)
4271 {
4272         struct nfscldeleg *dp;
4273
4274         TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
4275                 if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
4276                         dp->nfsdl_flags |= NFSCLDL_RECALL;
4277         }
4278 }
4279
4280 /*
4281  * Relock byte ranges. Called for delegation recall and state expiry.
4282  */
4283 static int
4284 nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
4285     struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
4286     NFSPROC_T *p)
4287 {
4288         struct nfscllockowner *nlp;
4289         struct nfsfh *nfhp;
4290         u_int64_t off, len;
4291         int error, newone, donelocally;
4292
4293         off = lop->nfslo_first;
4294         len = lop->nfslo_end - lop->nfslo_first;
4295         error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
4296             clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
4297             lp->nfsl_openowner, &nlp, &newone, &donelocally);
4298         if (error || donelocally)
4299                 return (error);
4300         nfhp = VTONFS(vp)->n_fhp;
4301         error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
4302             nfhp->nfh_len, nlp, newone, 0, off,
4303             len, lop->nfslo_type, cred, p);
4304         if (error)
4305                 nfscl_freelockowner(nlp, 0);
4306         return (error);
4307 }
4308
4309 /*
4310  * Called to re-open a file. Basically get a vnode for the file handle
4311  * and then call nfsrpc_openrpc() to do the rest.
4312  */
4313 static int
4314 nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
4315     u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
4316     struct ucred *cred, NFSPROC_T *p)
4317 {
4318         struct nfsnode *np;
4319         vnode_t vp;
4320         int error;
4321
4322         error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
4323         if (error)
4324                 return (error);
4325         vp = NFSTOV(np);
4326         if (np->n_v4 != NULL) {
4327                 error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
4328                     np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
4329                     NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
4330                     cred, p);
4331         } else {
4332                 error = EINVAL;
4333         }
4334         vrele(vp);
4335         return (error);
4336 }
4337
4338 /*
4339  * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
4340  * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
4341  * fail.
4342  */
4343 static int
4344 nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4345     u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
4346     u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
4347     int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
4348 {
4349         int error;
4350
4351         do {
4352                 error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
4353                     mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
4354                     0, 0);
4355                 if (error == NFSERR_DELAY)
4356                         (void) nfs_catnap(PZERO, error, "nfstryop");
4357         } while (error == NFSERR_DELAY);
4358         if (error == EAUTH || error == EACCES) {
4359                 /* Try again using system credentials */
4360                 newnfs_setroot(cred);
4361                 do {
4362                     error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
4363                         newfhlen, mode, op, name, namelen, ndpp, reclaim,
4364                         delegtype, cred, p, 1, 0);
4365                     if (error == NFSERR_DELAY)
4366                         (void) nfs_catnap(PZERO, error, "nfstryop");
4367                 } while (error == NFSERR_DELAY);
4368         }
4369         return (error);
4370 }
4371
4372 /*
4373  * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
4374  * NFSERR_DELAY. Also, retry with system credentials, if the provided
4375  * cred don't work.
4376  */
4377 static int
4378 nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
4379     int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
4380     u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
4381 {
4382         struct nfsrv_descript nfsd, *nd = &nfsd;
4383         int error;
4384
4385         do {
4386                 error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
4387                     reclaim, off, len, type, cred, p, 0);
4388                 if (!error && nd->nd_repstat == NFSERR_DELAY)
4389                         (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4390                             "nfstrylck");
4391         } while (!error && nd->nd_repstat == NFSERR_DELAY);
4392         if (!error)
4393                 error = nd->nd_repstat;
4394         if (error == EAUTH || error == EACCES) {
4395                 /* Try again using root credentials */
4396                 newnfs_setroot(cred);
4397                 do {
4398                         error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
4399                             newone, reclaim, off, len, type, cred, p, 1);
4400                         if (!error && nd->nd_repstat == NFSERR_DELAY)
4401                                 (void) nfs_catnap(PZERO, (int)nd->nd_repstat,
4402                                     "nfstrylck");
4403                 } while (!error && nd->nd_repstat == NFSERR_DELAY);
4404                 if (!error)
4405                         error = nd->nd_repstat;
4406         }
4407         return (error);
4408 }
4409
4410 /*
4411  * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
4412  * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4413  * credentials fail.
4414  */
4415 static int
4416 nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
4417     struct nfsmount *nmp, NFSPROC_T *p)
4418 {
4419         int error;
4420
4421         do {
4422                 error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
4423                 if (error == NFSERR_DELAY)
4424                         (void) nfs_catnap(PZERO, error, "nfstrydp");
4425         } while (error == NFSERR_DELAY);
4426         if (error == EAUTH || error == EACCES) {
4427                 /* Try again using system credentials */
4428                 newnfs_setroot(cred);
4429                 do {
4430                         error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
4431                         if (error == NFSERR_DELAY)
4432                                 (void) nfs_catnap(PZERO, error, "nfstrydp");
4433                 } while (error == NFSERR_DELAY);
4434         }
4435         return (error);
4436 }
4437
4438 /*
4439  * Try a close against the server. Just call nfsrpc_closerpc(),
4440  * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
4441  * credentials fail.
4442  */
4443 int
4444 nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
4445     struct nfsmount *nmp, NFSPROC_T *p)
4446 {
4447         struct nfsrv_descript nfsd, *nd = &nfsd;
4448         int error;
4449
4450         do {
4451                 error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
4452                 if (error == NFSERR_DELAY)
4453                         (void) nfs_catnap(PZERO, error, "nfstrycl");
4454         } while (error == NFSERR_DELAY);
4455         if (error == EAUTH || error == EACCES) {
4456                 /* Try again using system credentials */
4457                 newnfs_setroot(cred);
4458                 do {
4459                         error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
4460                         if (error == NFSERR_DELAY)
4461                                 (void) nfs_catnap(PZERO, error, "nfstrycl");
4462                 } while (error == NFSERR_DELAY);
4463         }
4464         return (error);
4465 }
4466
4467 /*
4468  * Decide if a delegation on a file permits close without flushing writes
4469  * to the server. This might be a big performance win in some environments.
4470  * (Not useful until the client does caching on local stable storage.)
4471  */
4472 int
4473 nfscl_mustflush(vnode_t vp)
4474 {
4475         struct nfsclclient *clp;
4476         struct nfscldeleg *dp;
4477         struct nfsnode *np;
4478         struct nfsmount *nmp;
4479
4480         np = VTONFS(vp);
4481         nmp = VFSTONFS(vp->v_mount);
4482         if (!NFSHASNFSV4(nmp))
4483                 return (1);
4484         NFSLOCKCLSTATE();
4485         clp = nfscl_findcl(nmp);
4486         if (clp == NULL) {
4487                 NFSUNLOCKCLSTATE();
4488                 return (1);
4489         }
4490         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4491         if (dp != NULL && (dp->nfsdl_flags &
4492             (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
4493              NFSCLDL_WRITE &&
4494             (dp->nfsdl_sizelimit >= np->n_size ||
4495              !NFSHASSTRICT3530(nmp))) {
4496                 NFSUNLOCKCLSTATE();
4497                 return (0);
4498         }
4499         NFSUNLOCKCLSTATE();
4500         return (1);
4501 }
4502
4503 /*
4504  * See if a (write) delegation exists for this file.
4505  */
4506 int
4507 nfscl_nodeleg(vnode_t vp, int writedeleg)
4508 {
4509         struct nfsclclient *clp;
4510         struct nfscldeleg *dp;
4511         struct nfsnode *np;
4512         struct nfsmount *nmp;
4513
4514         np = VTONFS(vp);
4515         nmp = VFSTONFS(vp->v_mount);
4516         if (!NFSHASNFSV4(nmp))
4517                 return (1);
4518         NFSLOCKCLSTATE();
4519         clp = nfscl_findcl(nmp);
4520         if (clp == NULL) {
4521                 NFSUNLOCKCLSTATE();
4522                 return (1);
4523         }
4524         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4525         if (dp != NULL &&
4526             (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
4527             (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
4528              NFSCLDL_WRITE)) {
4529                 NFSUNLOCKCLSTATE();
4530                 return (0);
4531         }
4532         NFSUNLOCKCLSTATE();
4533         return (1);
4534 }
4535
4536 /*
4537  * Look for an associated delegation that should be DelegReturned.
4538  */
4539 int
4540 nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
4541 {
4542         struct nfsclclient *clp;
4543         struct nfscldeleg *dp;
4544         struct nfsclowner *owp;
4545         struct nfscllockowner *lp;
4546         struct nfsmount *nmp;
4547         struct ucred *cred;
4548         struct nfsnode *np;
4549         int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4550
4551         nmp = VFSTONFS(vp->v_mount);
4552         np = VTONFS(vp);
4553         NFSLOCKCLSTATE();
4554         /*
4555          * Loop around waiting for:
4556          * - outstanding I/O operations on delegations to complete
4557          * - for a delegation on vp that has state, lock the client and
4558          *   do a recall
4559          * - return delegation with no state
4560          */
4561         while (1) {
4562                 clp = nfscl_findcl(nmp);
4563                 if (clp == NULL) {
4564                         NFSUNLOCKCLSTATE();
4565                         return (retcnt);
4566                 }
4567                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4568                     np->n_fhp->nfh_len);
4569                 if (dp != NULL) {
4570                     /*
4571                      * Wait for outstanding I/O ops to be done.
4572                      */
4573                     if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4574                         if (igotlock) {
4575                             nfsv4_unlock(&clp->nfsc_lock, 0);
4576                             igotlock = 0;
4577                         }
4578                         dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4579                         (void) nfsmsleep(&dp->nfsdl_rwlock,
4580                             NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4581                         continue;
4582                     }
4583                     needsrecall = 0;
4584                     LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4585                         if (!LIST_EMPTY(&owp->nfsow_open)) {
4586                             needsrecall = 1;
4587                             break;
4588                         }
4589                     }
4590                     if (!needsrecall) {
4591                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4592                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
4593                                 needsrecall = 1;
4594                                 break;
4595                             }
4596                         }
4597                     }
4598                     if (needsrecall && !triedrecall) {
4599                         dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4600                         islept = 0;
4601                         while (!igotlock) {
4602                             igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4603                                 &islept, NFSCLSTATEMUTEXPTR, NULL);
4604                             if (islept)
4605                                 break;
4606                         }
4607                         if (islept)
4608                             continue;
4609                         NFSUNLOCKCLSTATE();
4610                         cred = newnfs_getcred();
4611                         newnfs_copycred(&dp->nfsdl_cred, cred);
4612                         nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0, NULL);
4613                         NFSFREECRED(cred);
4614                         triedrecall = 1;
4615                         NFSLOCKCLSTATE();
4616                         nfsv4_unlock(&clp->nfsc_lock, 0);
4617                         igotlock = 0;
4618                         continue;
4619                     }
4620                     *stp = dp->nfsdl_stateid;
4621                     retcnt = 1;
4622                     nfscl_cleandeleg(dp);
4623                     nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4624                 }
4625                 if (igotlock)
4626                     nfsv4_unlock(&clp->nfsc_lock, 0);
4627                 NFSUNLOCKCLSTATE();
4628                 return (retcnt);
4629         }
4630 }
4631
4632 /*
4633  * Look for associated delegation(s) that should be DelegReturned.
4634  */
4635 int
4636 nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
4637     nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
4638 {
4639         struct nfsclclient *clp;
4640         struct nfscldeleg *dp;
4641         struct nfsclowner *owp;
4642         struct nfscllockowner *lp;
4643         struct nfsmount *nmp;
4644         struct ucred *cred;
4645         struct nfsnode *np;
4646         int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;
4647
4648         nmp = VFSTONFS(fvp->v_mount);
4649         *gotfdp = 0;
4650         *gottdp = 0;
4651         NFSLOCKCLSTATE();
4652         /*
4653          * Loop around waiting for:
4654          * - outstanding I/O operations on delegations to complete
4655          * - for a delegation on fvp that has state, lock the client and
4656          *   do a recall
4657          * - return delegation(s) with no state.
4658          */
4659         while (1) {
4660                 clp = nfscl_findcl(nmp);
4661                 if (clp == NULL) {
4662                         NFSUNLOCKCLSTATE();
4663                         return (retcnt);
4664                 }
4665                 np = VTONFS(fvp);
4666                 dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4667                     np->n_fhp->nfh_len);
4668                 if (dp != NULL && *gotfdp == 0) {
4669                     /*
4670                      * Wait for outstanding I/O ops to be done.
4671                      */
4672                     if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4673                         if (igotlock) {
4674                             nfsv4_unlock(&clp->nfsc_lock, 0);
4675                             igotlock = 0;
4676                         }
4677                         dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4678                         (void) nfsmsleep(&dp->nfsdl_rwlock,
4679                             NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4680                         continue;
4681                     }
4682                     needsrecall = 0;
4683                     LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4684                         if (!LIST_EMPTY(&owp->nfsow_open)) {
4685                             needsrecall = 1;
4686                             break;
4687                         }
4688                     }
4689                     if (!needsrecall) {
4690                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4691                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
4692                                 needsrecall = 1;
4693                                 break;
4694                             }
4695                         }
4696                     }
4697                     if (needsrecall && !triedrecall) {
4698                         dp->nfsdl_flags |= NFSCLDL_DELEGRET;
4699                         islept = 0;
4700                         while (!igotlock) {
4701                             igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
4702                                 &islept, NFSCLSTATEMUTEXPTR, NULL);
4703                             if (islept)
4704                                 break;
4705                         }
4706                         if (islept)
4707                             continue;
4708                         NFSUNLOCKCLSTATE();
4709                         cred = newnfs_getcred();
4710                         newnfs_copycred(&dp->nfsdl_cred, cred);
4711                         nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0, NULL);
4712                         NFSFREECRED(cred);
4713                         triedrecall = 1;
4714                         NFSLOCKCLSTATE();
4715                         nfsv4_unlock(&clp->nfsc_lock, 0);
4716                         igotlock = 0;
4717                         continue;
4718                     }
4719                     *fstp = dp->nfsdl_stateid;
4720                     retcnt++;
4721                     *gotfdp = 1;
4722                     nfscl_cleandeleg(dp);
4723                     nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4724                 }
4725                 if (igotlock) {
4726                     nfsv4_unlock(&clp->nfsc_lock, 0);
4727                     igotlock = 0;
4728                 }
4729                 if (tvp != NULL) {
4730                     np = VTONFS(tvp);
4731                     dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
4732                         np->n_fhp->nfh_len);
4733                     if (dp != NULL && *gottdp == 0) {
4734                         /*
4735                          * Wait for outstanding I/O ops to be done.
4736                          */
4737                         if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
4738                             dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
4739                             (void) nfsmsleep(&dp->nfsdl_rwlock,
4740                                 NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
4741                             continue;
4742                         }
4743                         LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
4744                             if (!LIST_EMPTY(&owp->nfsow_open)) {
4745                                 NFSUNLOCKCLSTATE();
4746                                 return (retcnt);
4747                             }
4748                         }
4749                         LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
4750                             if (!LIST_EMPTY(&lp->nfsl_lock)) {
4751                                 NFSUNLOCKCLSTATE();
4752                                 return (retcnt);
4753                             }
4754                         }
4755                         *tstp = dp->nfsdl_stateid;
4756                         retcnt++;
4757                         *gottdp = 1;
4758                         nfscl_cleandeleg(dp);
4759                         nfscl_freedeleg(&clp->nfsc_deleg, dp, true);
4760                     }
4761                 }
4762                 NFSUNLOCKCLSTATE();
4763                 return (retcnt);
4764         }
4765 }
4766
4767 /*
4768  * Get a reference on the clientid associated with the mount point.
4769  * Return 1 if success, 0 otherwise.
4770  */
4771 int
4772 nfscl_getref(struct nfsmount *nmp)
4773 {
4774         struct nfsclclient *clp;
4775
4776         NFSLOCKCLSTATE();
4777         clp = nfscl_findcl(nmp);
4778         if (clp == NULL) {
4779                 NFSUNLOCKCLSTATE();
4780                 return (0);
4781         }
4782         nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
4783         NFSUNLOCKCLSTATE();
4784         return (1);
4785 }
4786
4787 /*
4788  * Release a reference on a clientid acquired with the above call.
4789  */
4790 void
4791 nfscl_relref(struct nfsmount *nmp)
4792 {
4793         struct nfsclclient *clp;
4794
4795         NFSLOCKCLSTATE();
4796         clp = nfscl_findcl(nmp);
4797         if (clp == NULL) {
4798                 NFSUNLOCKCLSTATE();
4799                 return;
4800         }
4801         nfsv4_relref(&clp->nfsc_lock);
4802         NFSUNLOCKCLSTATE();
4803 }
4804
4805 /*
4806  * Save the size attribute in the delegation, since the nfsnode
4807  * is going away.
4808  */
4809 void
4810 nfscl_reclaimnode(vnode_t vp)
4811 {
4812         struct nfsclclient *clp;
4813         struct nfscldeleg *dp;
4814         struct nfsnode *np = VTONFS(vp);
4815         struct nfsmount *nmp;
4816
4817         nmp = VFSTONFS(vp->v_mount);
4818         if (!NFSHASNFSV4(nmp))
4819                 return;
4820         NFSLOCKCLSTATE();
4821         clp = nfscl_findcl(nmp);
4822         if (clp == NULL) {
4823                 NFSUNLOCKCLSTATE();
4824                 return;
4825         }
4826         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4827         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4828                 dp->nfsdl_size = np->n_size;
4829         NFSUNLOCKCLSTATE();
4830 }
4831
4832 /*
4833  * Get the saved size attribute in the delegation, since it is a
4834  * newly allocated nfsnode.
4835  */
4836 void
4837 nfscl_newnode(vnode_t vp)
4838 {
4839         struct nfsclclient *clp;
4840         struct nfscldeleg *dp;
4841         struct nfsnode *np = VTONFS(vp);
4842         struct nfsmount *nmp;
4843
4844         nmp = VFSTONFS(vp->v_mount);
4845         if (!NFSHASNFSV4(nmp))
4846                 return;
4847         NFSLOCKCLSTATE();
4848         clp = nfscl_findcl(nmp);
4849         if (clp == NULL) {
4850                 NFSUNLOCKCLSTATE();
4851                 return;
4852         }
4853         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4854         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
4855                 np->n_size = dp->nfsdl_size;
4856         NFSUNLOCKCLSTATE();
4857 }
4858
4859 /*
4860  * If there is a valid write delegation for this file, set the modtime
4861  * to the local clock time.
4862  */
4863 void
4864 nfscl_delegmodtime(vnode_t vp)
4865 {
4866         struct nfsclclient *clp;
4867         struct nfscldeleg *dp;
4868         struct nfsnode *np = VTONFS(vp);
4869         struct nfsmount *nmp;
4870
4871         nmp = VFSTONFS(vp->v_mount);
4872         if (!NFSHASNFSV4(nmp))
4873                 return;
4874         NFSLOCKCLSTATE();
4875         clp = nfscl_findcl(nmp);
4876         if (clp == NULL) {
4877                 NFSUNLOCKCLSTATE();
4878                 return;
4879         }
4880         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4881         if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
4882                 nanotime(&dp->nfsdl_modtime);
4883                 dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
4884         }
4885         NFSUNLOCKCLSTATE();
4886 }
4887
4888 /*
4889  * If there is a valid write delegation for this file with a modtime set,
4890  * put that modtime in mtime.
4891  */
4892 void
4893 nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
4894 {
4895         struct nfsclclient *clp;
4896         struct nfscldeleg *dp;
4897         struct nfsnode *np = VTONFS(vp);
4898         struct nfsmount *nmp;
4899
4900         nmp = VFSTONFS(vp->v_mount);
4901         if (!NFSHASNFSV4(nmp))
4902                 return;
4903         NFSLOCKCLSTATE();
4904         clp = nfscl_findcl(nmp);
4905         if (clp == NULL) {
4906                 NFSUNLOCKCLSTATE();
4907                 return;
4908         }
4909         dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
4910         if (dp != NULL &&
4911             (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
4912             (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
4913                 *mtime = dp->nfsdl_modtime;
4914         NFSUNLOCKCLSTATE();
4915 }
4916
4917 static int
4918 nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
4919 {
4920         short *defaulterrp, *errp;
4921
4922         if (!nd->nd_repstat)
4923                 return (0);
4924         if (nd->nd_procnum == NFSPROC_NOOP)
4925                 return (txdr_unsigned(nd->nd_repstat & 0xffff));
4926         if (nd->nd_repstat == EBADRPC)
4927                 return (txdr_unsigned(NFSERR_BADXDR));
4928         if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
4929             nd->nd_repstat == NFSERR_OPILLEGAL)
4930                 return (txdr_unsigned(nd->nd_repstat));
4931         if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
4932             minorvers > NFSV4_MINORVERSION) {
4933                 /* NFSv4.n error. */
4934                 return (txdr_unsigned(nd->nd_repstat));
4935         }
4936         if (nd->nd_procnum < NFSV4OP_CBNOPS)
4937                 errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
4938         else
4939                 return (txdr_unsigned(nd->nd_repstat));
4940         while (*++errp)
4941                 if (*errp == (short)nd->nd_repstat)
4942                         return (txdr_unsigned(nd->nd_repstat));
4943         return (txdr_unsigned(*defaulterrp));
4944 }
4945
4946 /*
4947  * Called to find/add a layout to a client.
4948  * This function returns the layout with a refcnt (shared lock) upon
4949  * success (returns 0) or with no lock/refcnt on the layout when an
4950  * error is returned.
4951  * If a layout is passed in via lypp, it is locked (exclusively locked).
4952  */
4953 int
4954 nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
4955     nfsv4stateid_t *stateidp, int layouttype, int retonclose,
4956     struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
4957     struct ucred *cred, NFSPROC_T *p)
4958 {
4959         struct nfsclclient *clp;
4960         struct nfscllayout *lyp, *tlyp;
4961         struct nfsclflayout *flp;
4962         struct nfsnode *np = VTONFS(vp);
4963         mount_t mp;
4964         int layout_passed_in;
4965
4966         mp = nmp->nm_mountp;
4967         layout_passed_in = 1;
4968         tlyp = NULL;
4969         lyp = *lypp;
4970         if (lyp == NULL) {
4971                 layout_passed_in = 0;
4972                 tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
4973                     M_WAITOK | M_ZERO);
4974         }
4975
4976         NFSLOCKCLSTATE();
4977         clp = nmp->nm_clp;
4978         if (clp == NULL) {
4979                 if (layout_passed_in != 0)
4980                         nfsv4_unlock(&lyp->nfsly_lock, 0);
4981                 NFSUNLOCKCLSTATE();
4982                 if (tlyp != NULL)
4983                         free(tlyp, M_NFSLAYOUT);
4984                 return (EPERM);
4985         }
4986         if (lyp == NULL) {
4987                 /*
4988                  * Although no lyp was passed in, another thread might have
4989                  * allocated one. If one is found, just increment it's ref
4990                  * count and return it.
4991                  */
4992                 lyp = nfscl_findlayout(clp, fhp, fhlen);
4993                 if (lyp == NULL) {
4994                         lyp = tlyp;
4995                         tlyp = NULL;
4996                         lyp->nfsly_stateid.seqid = stateidp->seqid;
4997                         lyp->nfsly_stateid.other[0] = stateidp->other[0];
4998                         lyp->nfsly_stateid.other[1] = stateidp->other[1];
4999                         lyp->nfsly_stateid.other[2] = stateidp->other[2];
5000                         lyp->nfsly_lastbyte = 0;
5001                         LIST_INIT(&lyp->nfsly_flayread);
5002                         LIST_INIT(&lyp->nfsly_flayrw);
5003                         LIST_INIT(&lyp->nfsly_recall);
5004                         lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
5005                         lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
5006                         lyp->nfsly_clp = clp;
5007                         if (layouttype == NFSLAYOUT_FLEXFILE)
5008                                 lyp->nfsly_flags = NFSLY_FLEXFILE;
5009                         else
5010                                 lyp->nfsly_flags = NFSLY_FILES;
5011                         if (retonclose != 0)
5012                                 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
5013                         lyp->nfsly_fhlen = fhlen;
5014                         NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
5015                         TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5016                         LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
5017                             nfsly_hash);
5018                         lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5019                         nfscl_layoutcnt++;
5020                 } else {
5021                         if (retonclose != 0)
5022                                 lyp->nfsly_flags |= NFSLY_RETONCLOSE;
5023                         if (stateidp->seqid > lyp->nfsly_stateid.seqid)
5024                                 lyp->nfsly_stateid.seqid = stateidp->seqid;
5025                         TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5026                         TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5027                         lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5028                 }
5029                 nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5030                 if (NFSCL_FORCEDISM(mp)) {
5031                         NFSUNLOCKCLSTATE();
5032                         if (tlyp != NULL)
5033                                 free(tlyp, M_NFSLAYOUT);
5034                         return (EPERM);
5035                 }
5036                 *lypp = lyp;
5037         } else if (stateidp->seqid > lyp->nfsly_stateid.seqid)
5038                 lyp->nfsly_stateid.seqid = stateidp->seqid;
5039
5040         /* Merge the new list of File Layouts into the list. */
5041         flp = LIST_FIRST(fhlp);
5042         if (flp != NULL) {
5043                 if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
5044                         nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
5045                 else
5046                         nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
5047         }
5048         if (layout_passed_in != 0)
5049                 nfsv4_unlock(&lyp->nfsly_lock, 1);
5050         NFSUNLOCKCLSTATE();
5051         if (tlyp != NULL)
5052                 free(tlyp, M_NFSLAYOUT);
5053         return (0);
5054 }
5055
5056 /*
5057  * Search for a layout by MDS file handle.
5058  * If one is found, it is returned with a refcnt (shared lock) iff
5059  * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
5060  * returned NULL.
5061  */
5062 struct nfscllayout *
5063 nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
5064     uint64_t off, struct nfsclflayout **retflpp, int *recalledp)
5065 {
5066         struct nfscllayout *lyp;
5067         mount_t mp;
5068         int error, igotlock;
5069
5070         mp = clp->nfsc_nmp->nm_mountp;
5071         *recalledp = 0;
5072         *retflpp = NULL;
5073         NFSLOCKCLSTATE();
5074         lyp = nfscl_findlayout(clp, fhp, fhlen);
5075         if (lyp != NULL) {
5076                 if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5077                         TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
5078                         TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
5079                         lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
5080                         error = nfscl_findlayoutforio(lyp, off,
5081                             NFSV4OPEN_ACCESSREAD, retflpp);
5082                         if (error == 0)
5083                                 nfsv4_getref(&lyp->nfsly_lock, NULL,
5084                                     NFSCLSTATEMUTEXPTR, mp);
5085                         else {
5086                                 do {
5087                                         igotlock = nfsv4_lock(&lyp->nfsly_lock,
5088                                             1, NULL, NFSCLSTATEMUTEXPTR, mp);
5089                                 } while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
5090                                 *retflpp = NULL;
5091                         }
5092                         if (NFSCL_FORCEDISM(mp)) {
5093                                 lyp = NULL;
5094                                 *recalledp = 1;
5095                         }
5096                 } else {
5097                         lyp = NULL;
5098                         *recalledp = 1;
5099                 }
5100         }
5101         NFSUNLOCKCLSTATE();
5102         return (lyp);
5103 }
5104
5105 /*
5106  * Search for a layout by MDS file handle. If one is found, mark in to be
5107  * recalled, if it already marked "return on close".
5108  */
5109 static void
5110 nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
5111     int fhlen, struct nfsclrecalllayout **recallpp)
5112 {
5113         struct nfscllayout *lyp;
5114         uint32_t iomode;
5115
5116         if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vp->v_mount)) ||
5117             nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
5118             (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
5119                 return;
5120         lyp = nfscl_findlayout(clp, fhp, fhlen);
5121         if (lyp != NULL && (lyp->nfsly_flags & (NFSLY_RETONCLOSE |
5122             NFSLY_RECALL)) == NFSLY_RETONCLOSE) {
5123                 iomode = 0;
5124                 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5125                         iomode |= NFSLAYOUTIOMODE_READ;
5126                 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5127                         iomode |= NFSLAYOUTIOMODE_RW;
5128                 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5129                     0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
5130                     *recallpp);
5131                 NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
5132                 *recallpp = NULL;
5133         }
5134 }
5135
5136 /*
5137  * Mark the layout to be recalled and with an error.
5138  * Also, disable the dsp from further use.
5139  */
5140 void
5141 nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
5142     struct nfscllayout *lyp, struct nfsclds *dsp)
5143 {
5144         struct nfsclrecalllayout *recallp;
5145         uint32_t iomode;
5146
5147         printf("DS being disabled, error=%d\n", stat);
5148         /* Set up the return of the layout. */
5149         recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
5150         iomode = 0;
5151         NFSLOCKCLSTATE();
5152         if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
5153                 if (!LIST_EMPTY(&lyp->nfsly_flayread))
5154                         iomode |= NFSLAYOUTIOMODE_READ;
5155                 if (!LIST_EMPTY(&lyp->nfsly_flayrw))
5156                         iomode |= NFSLAYOUTIOMODE_RW;
5157                 (void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
5158                     0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
5159                     dp->nfsdi_deviceid, recallp);
5160                 NFSUNLOCKCLSTATE();
5161                 NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
5162         } else {
5163                 NFSUNLOCKCLSTATE();
5164                 free(recallp, M_NFSLAYRECALL);
5165         }
5166
5167         /* And shut the TCP connection down. */
5168         nfscl_cancelreqs(dsp);
5169 }
5170
5171 /*
5172  * Cancel all RPCs for this "dsp" by closing the connection.
5173  * Also, mark the session as defunct.
5174  * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
5175  * cannot be shut down.
5176  */
5177 void
5178 nfscl_cancelreqs(struct nfsclds *dsp)
5179 {
5180         struct __rpc_client *cl;
5181         static int non_event;
5182
5183         NFSLOCKDS(dsp);
5184         if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
5185             dsp->nfsclds_sockp != NULL &&
5186             dsp->nfsclds_sockp->nr_client != NULL) {
5187                 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
5188                 cl = dsp->nfsclds_sockp->nr_client;
5189                 dsp->nfsclds_sess.nfsess_defunct = 1;
5190                 NFSUNLOCKDS(dsp);
5191                 CLNT_CLOSE(cl);
5192                 /*
5193                  * This 1sec sleep is done to reduce the number of reconnect
5194                  * attempts made on the DS while it has failed.
5195                  */
5196                 tsleep(&non_event, PVFS, "ndscls", hz);
5197                 return;
5198         }
5199         NFSUNLOCKDS(dsp);
5200 }
5201
5202 /*
5203  * Dereference a layout.
5204  */
5205 void
5206 nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
5207 {
5208
5209         NFSLOCKCLSTATE();
5210         if (exclocked != 0)
5211                 nfsv4_unlock(&lyp->nfsly_lock, 0);
5212         else
5213                 nfsv4_relref(&lyp->nfsly_lock);
5214         NFSUNLOCKCLSTATE();
5215 }
5216
5217 /*
5218  * Search for a devinfo by deviceid. If one is found, return it after
5219  * acquiring a reference count on it.
5220  */
5221 struct nfscldevinfo *
5222 nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
5223     struct nfscldevinfo *dip)
5224 {
5225
5226         NFSLOCKCLSTATE();
5227         if (dip == NULL)
5228                 dip = nfscl_finddevinfo(clp, deviceid);
5229         if (dip != NULL)
5230                 dip->nfsdi_refcnt++;
5231         NFSUNLOCKCLSTATE();
5232         return (dip);
5233 }
5234
5235 /*
5236  * Dereference a devinfo structure.
5237  */
5238 static void
5239 nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
5240 {
5241
5242         dip->nfsdi_refcnt--;
5243         if (dip->nfsdi_refcnt == 0)
5244                 wakeup(&dip->nfsdi_refcnt);
5245 }
5246
5247 /*
5248  * Dereference a devinfo structure.
5249  */
5250 void
5251 nfscl_reldevinfo(struct nfscldevinfo *dip)
5252 {
5253
5254         NFSLOCKCLSTATE();
5255         nfscl_reldevinfo_locked(dip);
5256         NFSUNLOCKCLSTATE();
5257 }
5258
5259 /*
5260  * Find a layout for this file handle. Return NULL upon failure.
5261  */
5262 static struct nfscllayout *
5263 nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
5264 {
5265         struct nfscllayout *lyp;
5266
5267         LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
5268                 if (lyp->nfsly_fhlen == fhlen &&
5269                     !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
5270                         break;
5271         return (lyp);
5272 }
5273
5274 /*
5275  * Find a devinfo for this deviceid. Return NULL upon failure.
5276  */
5277 static struct nfscldevinfo *
5278 nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
5279 {
5280         struct nfscldevinfo *dip;
5281
5282         LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
5283                 if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
5284                     == 0)
5285                         break;
5286         return (dip);
5287 }
5288
5289 /*
5290  * Merge the new file layout list into the main one, maintaining it in
5291  * increasing offset order.
5292  */
5293 static void
5294 nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
5295     struct nfsclflayouthead *newfhlp)
5296 {
5297         struct nfsclflayout *flp, *nflp, *prevflp, *tflp;
5298
5299         flp = LIST_FIRST(fhlp);
5300         prevflp = NULL;
5301         LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
5302                 while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
5303                         prevflp = flp;
5304                         flp = LIST_NEXT(flp, nfsfl_list);
5305                 }
5306                 if (prevflp == NULL)
5307                         LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
5308                 else
5309                         LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
5310                 prevflp = nflp;
5311         }
5312 }
5313
5314 /*
5315  * Add this nfscldevinfo to the client, if it doesn't already exist.
5316  * This function consumes the structure pointed at by dip, if not NULL.
5317  */
5318 int
5319 nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
5320     struct nfsclflayout *flp)
5321 {
5322         struct nfsclclient *clp;
5323         struct nfscldevinfo *tdip;
5324         uint8_t *dev;
5325
5326         NFSLOCKCLSTATE();
5327         clp = nmp->nm_clp;
5328         if (clp == NULL) {
5329                 NFSUNLOCKCLSTATE();
5330                 if (dip != NULL)
5331                         free(dip, M_NFSDEVINFO);
5332                 return (ENODEV);
5333         }
5334         if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5335                 dev = flp->nfsfl_dev;
5336         else
5337                 dev = flp->nfsfl_ffm[ind].dev;
5338         tdip = nfscl_finddevinfo(clp, dev);
5339         if (tdip != NULL) {
5340                 tdip->nfsdi_layoutrefs++;
5341                 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5342                         flp->nfsfl_devp = tdip;
5343                 else
5344                         flp->nfsfl_ffm[ind].devp = tdip;
5345                 nfscl_reldevinfo_locked(tdip);
5346                 NFSUNLOCKCLSTATE();
5347                 if (dip != NULL)
5348                         free(dip, M_NFSDEVINFO);
5349                 return (0);
5350         }
5351         if (dip != NULL) {
5352                 LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
5353                 dip->nfsdi_layoutrefs = 1;
5354                 if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
5355                         flp->nfsfl_devp = dip;
5356                 else
5357                         flp->nfsfl_ffm[ind].devp = dip;
5358         }
5359         NFSUNLOCKCLSTATE();
5360         if (dip == NULL)
5361                 return (ENODEV);
5362         return (0);
5363 }
5364
5365 /*
5366  * Free up a layout structure and associated file layout structure(s).
5367  */
5368 void
5369 nfscl_freelayout(struct nfscllayout *layp)
5370 {
5371         struct nfsclflayout *flp, *nflp;
5372         struct nfsclrecalllayout *rp, *nrp;
5373
5374         LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
5375                 LIST_REMOVE(flp, nfsfl_list);
5376                 nfscl_freeflayout(flp);
5377         }
5378         LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
5379                 LIST_REMOVE(flp, nfsfl_list);
5380                 nfscl_freeflayout(flp);
5381         }
5382         LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
5383                 LIST_REMOVE(rp, nfsrecly_list);
5384                 free(rp, M_NFSLAYRECALL);
5385         }
5386         nfscl_layoutcnt--;
5387         free(layp, M_NFSLAYOUT);
5388 }
5389
5390 /*
5391  * Free up a file layout structure.
5392  */
5393 void
5394 nfscl_freeflayout(struct nfsclflayout *flp)
5395 {
5396         int i, j;
5397
5398         if ((flp->nfsfl_flags & NFSFL_FILE) != 0) {
5399                 for (i = 0; i < flp->nfsfl_fhcnt; i++)
5400                         free(flp->nfsfl_fh[i], M_NFSFH);
5401                 if (flp->nfsfl_devp != NULL)
5402                         flp->nfsfl_devp->nfsdi_layoutrefs--;
5403         }
5404         if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
5405                 for (i = 0; i < flp->nfsfl_mirrorcnt; i++) {
5406                         for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
5407                                 free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
5408                         if (flp->nfsfl_ffm[i].devp != NULL)     
5409                                 flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--;     
5410                 }
5411         free(flp, M_NFSFLAYOUT);
5412 }
5413
5414 /*
5415  * Free up a file layout devinfo structure.
5416  */
5417 void
5418 nfscl_freedevinfo(struct nfscldevinfo *dip)
5419 {
5420
5421         free(dip, M_NFSDEVINFO);
5422 }
5423
5424 /*
5425  * Mark any layouts that match as recalled.
5426  */
5427 static int
5428 nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
5429     uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op,
5430     char *devid, struct nfsclrecalllayout *recallp)
5431 {
5432         struct nfsclrecalllayout *rp, *orp;
5433
5434         recallp->nfsrecly_recalltype = recalltype;
5435         recallp->nfsrecly_iomode = iomode;
5436         recallp->nfsrecly_stateseqid = stateseqid;
5437         recallp->nfsrecly_off = off;
5438         recallp->nfsrecly_len = len;
5439         recallp->nfsrecly_stat = stat;
5440         recallp->nfsrecly_op = op;
5441         if (devid != NULL)
5442                 NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID);
5443         /*
5444          * Order the list as file returns first, followed by fsid and any
5445          * returns, both in increasing stateseqid order.
5446          * Note that the seqids wrap around, so 1 is after 0xffffffff.
5447          * (I'm not sure this is correct because I find RFC5661 confusing
5448          *  on this, but hopefully it will work ok.)
5449          */
5450         orp = NULL;
5451         LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5452                 orp = rp;
5453                 if ((recalltype == NFSLAYOUTRETURN_FILE &&
5454                      (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
5455                       nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
5456                     (recalltype != NFSLAYOUTRETURN_FILE &&
5457                      rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
5458                      nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
5459                         LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
5460                         break;
5461                 }
5462
5463                 /*
5464                  * Put any error return on all the file returns that will
5465                  * preceed this one.
5466                  */
5467                 if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE &&
5468                    stat != 0 && rp->nfsrecly_stat == 0) {
5469                         rp->nfsrecly_stat = stat;
5470                         rp->nfsrecly_op = op;
5471                         if (devid != NULL)
5472                                 NFSBCOPY(devid, rp->nfsrecly_devid,
5473                                     NFSX_V4DEVICEID);
5474                 }
5475         }
5476         if (rp == NULL) {
5477                 if (orp == NULL)
5478                         LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
5479                             nfsrecly_list);
5480                 else
5481                         LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
5482         }
5483         lyp->nfsly_flags |= NFSLY_RECALL;
5484         wakeup(lyp->nfsly_clp);
5485         return (0);
5486 }
5487
5488 /*
5489  * Compare the two seqids for ordering. The trick is that the seqids can
5490  * wrap around from 0xffffffff->0, so check for the cases where one
5491  * has wrapped around.
5492  * Return 1 if seqid1 comes before seqid2, 0 otherwise.
5493  */
5494 static int
5495 nfscl_seq(uint32_t seqid1, uint32_t seqid2)
5496 {
5497
5498         if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
5499                 /* seqid2 has wrapped around. */
5500                 return (0);
5501         if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
5502                 /* seqid1 has wrapped around. */
5503                 return (1);
5504         if (seqid1 <= seqid2)
5505                 return (1);
5506         return (0);
5507 }
5508
5509 /*
5510  * Do a layout return for each of the recalls.
5511  */
5512 static void
5513 nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
5514     struct ucred *cred, NFSPROC_T *p)
5515 {
5516         struct nfsclrecalllayout *rp;
5517         nfsv4stateid_t stateid;
5518         int layouttype;
5519
5520         NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
5521         stateid.seqid = lyp->nfsly_stateid.seqid;
5522         if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5523                 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5524         else
5525                 layouttype = NFSLAYOUT_FLEXFILE;
5526         LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
5527                 (void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
5528                     lyp->nfsly_fhlen, 0, layouttype,
5529                     rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
5530                     rp->nfsrecly_off, rp->nfsrecly_len,
5531                     &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op,
5532                     rp->nfsrecly_devid);
5533         }
5534 }
5535
5536 /*
5537  * Do the layout commit for a file layout.
5538  */
5539 static void
5540 nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
5541     struct ucred *cred, NFSPROC_T *p)
5542 {
5543         struct nfsclflayout *flp;
5544         uint64_t len;
5545         int error, layouttype;
5546
5547         if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
5548                 layouttype = NFSLAYOUT_NFSV4_1_FILES;
5549         else
5550                 layouttype = NFSLAYOUT_FLEXFILE;
5551         LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
5552                 if (layouttype == NFSLAYOUT_FLEXFILE &&
5553                     (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
5554                         NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
5555                         /* If not supported, don't bother doing it. */
5556                         NFSLOCKMNT(nmp);
5557                         nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5558                         NFSUNLOCKMNT(nmp);
5559                         break;
5560                 } else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
5561                         len = flp->nfsfl_end - flp->nfsfl_off;
5562                         error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
5563                             lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
5564                             lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
5565                             layouttype, cred, p, NULL);
5566                         NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
5567                         if (error == NFSERR_NOTSUPP) {
5568                                 /* If not supported, don't bother doing it. */
5569                                 NFSLOCKMNT(nmp);
5570                                 nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
5571                                 NFSUNLOCKMNT(nmp);
5572                                 break;
5573                         }
5574                 }
5575         }
5576 }
5577
5578 /*
5579  * Commit all layouts for a file (vnode).
5580  */
5581 int
5582 nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
5583 {
5584         struct nfsclclient *clp;
5585         struct nfscllayout *lyp;
5586         struct nfsnode *np = VTONFS(vp);
5587         mount_t mp;
5588         struct nfsmount *nmp;
5589
5590         mp = vp->v_mount;
5591         nmp = VFSTONFS(mp);
5592         if (NFSHASNOLAYOUTCOMMIT(nmp))
5593                 return (0);
5594         NFSLOCKCLSTATE();
5595         clp = nmp->nm_clp;
5596         if (clp == NULL) {
5597                 NFSUNLOCKCLSTATE();
5598                 return (EPERM);
5599         }
5600         lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
5601         if (lyp == NULL) {
5602                 NFSUNLOCKCLSTATE();
5603                 return (EPERM);
5604         }
5605         nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
5606         if (NFSCL_FORCEDISM(mp)) {
5607                 NFSUNLOCKCLSTATE();
5608                 return (EPERM);
5609         }
5610 tryagain:
5611         if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
5612                 lyp->nfsly_flags &= ~NFSLY_WRITTEN;
5613                 NFSUNLOCKCLSTATE();
5614                 NFSCL_DEBUG(4, "do layoutcommit2\n");
5615                 nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
5616                 NFSLOCKCLSTATE();
5617                 goto tryagain;
5618         }
5619         nfsv4_relref(&lyp->nfsly_lock);
5620         NFSUNLOCKCLSTATE();
5621         return (0);
5622 }