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