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