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