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