xref: /linux/fs/nfs/delegation.c (revision 42422993cf28d456778ee9168d73758ec037cd51)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/fs/nfs/delegation.c
4  *
5  * Copyright (C) 2004 Trond Myklebust
6  *
7  * NFS file delegation management
8  *
9  */
10 #include <linux/completion.h>
11 #include <linux/kthread.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/iversion.h>
17 
18 #include <linux/nfs4.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_xdr.h>
21 
22 #include "nfs4_fs.h"
23 #include "nfs4session.h"
24 #include "delegation.h"
25 #include "internal.h"
26 #include "nfs4trace.h"
27 
28 #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)
29 
30 static atomic_long_t nfs_active_delegations;
31 static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK;
32 
33 static void __nfs_free_delegation(struct nfs_delegation *delegation)
34 {
35 	put_cred(delegation->cred);
36 	delegation->cred = NULL;
37 	kfree_rcu(delegation, rcu);
38 }
39 
40 static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
41 {
42 	if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
43 		delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
44 		atomic_long_dec(&nfs_active_delegations);
45 		if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
46 			nfs_clear_verifier_delegated(delegation->inode);
47 	}
48 }
49 
50 static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
51 {
52 	refcount_inc(&delegation->refcount);
53 	return delegation;
54 }
55 
56 static void nfs_put_delegation(struct nfs_delegation *delegation)
57 {
58 	if (refcount_dec_and_test(&delegation->refcount))
59 		__nfs_free_delegation(delegation);
60 }
61 
62 static void nfs_free_delegation(struct nfs_delegation *delegation)
63 {
64 	nfs_mark_delegation_revoked(delegation);
65 	nfs_put_delegation(delegation);
66 }
67 
68 /**
69  * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
70  * @delegation: delegation to process
71  *
72  */
73 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
74 {
75 	set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
76 }
77 
78 static void nfs_mark_return_delegation(struct nfs_server *server,
79 				       struct nfs_delegation *delegation)
80 {
81 	set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
82 	set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
83 }
84 
85 static bool
86 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
87 		fmode_t flags)
88 {
89 	if (delegation != NULL && (delegation->type & flags) == flags &&
90 	    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
91 	    !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
92 		return true;
93 	return false;
94 }
95 
96 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
97 {
98 	struct nfs_delegation *delegation;
99 
100 	delegation = rcu_dereference(NFS_I(inode)->delegation);
101 	if (nfs4_is_valid_delegation(delegation, 0))
102 		return delegation;
103 	return NULL;
104 }
105 
106 static int
107 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
108 {
109 	struct nfs_delegation *delegation;
110 	int ret = 0;
111 
112 	flags &= FMODE_READ|FMODE_WRITE;
113 	rcu_read_lock();
114 	delegation = rcu_dereference(NFS_I(inode)->delegation);
115 	if (nfs4_is_valid_delegation(delegation, flags)) {
116 		if (mark)
117 			nfs_mark_delegation_referenced(delegation);
118 		ret = 1;
119 	}
120 	rcu_read_unlock();
121 	return ret;
122 }
123 /**
124  * nfs4_have_delegation - check if inode has a delegation, mark it
125  * NFS_DELEGATION_REFERENCED if there is one.
126  * @inode: inode to check
127  * @flags: delegation types to check for
128  *
129  * Returns one if inode has the indicated delegation, otherwise zero.
130  */
131 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
132 {
133 	return nfs4_do_check_delegation(inode, flags, true);
134 }
135 
136 /*
137  * nfs4_check_delegation - check if inode has a delegation, do not mark
138  * NFS_DELEGATION_REFERENCED if it has one.
139  */
140 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
141 {
142 	return nfs4_do_check_delegation(inode, flags, false);
143 }
144 
145 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
146 {
147 	struct inode *inode = state->inode;
148 	struct file_lock *fl;
149 	struct file_lock_context *flctx = locks_inode_context(inode);
150 	struct list_head *list;
151 	int status = 0;
152 
153 	if (flctx == NULL)
154 		goto out;
155 
156 	list = &flctx->flc_posix;
157 	spin_lock(&flctx->flc_lock);
158 restart:
159 	list_for_each_entry(fl, list, fl_list) {
160 		if (nfs_file_open_context(fl->fl_file)->state != state)
161 			continue;
162 		spin_unlock(&flctx->flc_lock);
163 		status = nfs4_lock_delegation_recall(fl, state, stateid);
164 		if (status < 0)
165 			goto out;
166 		spin_lock(&flctx->flc_lock);
167 	}
168 	if (list == &flctx->flc_posix) {
169 		list = &flctx->flc_flock;
170 		goto restart;
171 	}
172 	spin_unlock(&flctx->flc_lock);
173 out:
174 	return status;
175 }
176 
177 static int nfs_delegation_claim_opens(struct inode *inode,
178 		const nfs4_stateid *stateid, fmode_t type)
179 {
180 	struct nfs_inode *nfsi = NFS_I(inode);
181 	struct nfs_open_context *ctx;
182 	struct nfs4_state_owner *sp;
183 	struct nfs4_state *state;
184 	unsigned int seq;
185 	int err;
186 
187 again:
188 	rcu_read_lock();
189 	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
190 		state = ctx->state;
191 		if (state == NULL)
192 			continue;
193 		if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
194 			continue;
195 		if (!nfs4_valid_open_stateid(state))
196 			continue;
197 		if (!nfs4_stateid_match(&state->stateid, stateid))
198 			continue;
199 		if (!get_nfs_open_context(ctx))
200 			continue;
201 		rcu_read_unlock();
202 		sp = state->owner;
203 		/* Block nfs4_proc_unlck */
204 		mutex_lock(&sp->so_delegreturn_mutex);
205 		seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
206 		err = nfs4_open_delegation_recall(ctx, state, stateid);
207 		if (!err)
208 			err = nfs_delegation_claim_locks(state, stateid);
209 		if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
210 			err = -EAGAIN;
211 		mutex_unlock(&sp->so_delegreturn_mutex);
212 		put_nfs_open_context(ctx);
213 		if (err != 0)
214 			return err;
215 		goto again;
216 	}
217 	rcu_read_unlock();
218 	return 0;
219 }
220 
221 /**
222  * nfs_inode_reclaim_delegation - process a delegation reclaim request
223  * @inode: inode to process
224  * @cred: credential to use for request
225  * @type: delegation type
226  * @stateid: delegation stateid
227  * @pagemod_limit: write delegation "space_limit"
228  *
229  */
230 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
231 				  fmode_t type, const nfs4_stateid *stateid,
232 				  unsigned long pagemod_limit)
233 {
234 	struct nfs_delegation *delegation;
235 	const struct cred *oldcred = NULL;
236 
237 	rcu_read_lock();
238 	delegation = rcu_dereference(NFS_I(inode)->delegation);
239 	if (delegation != NULL) {
240 		spin_lock(&delegation->lock);
241 		nfs4_stateid_copy(&delegation->stateid, stateid);
242 		delegation->type = type;
243 		delegation->pagemod_limit = pagemod_limit;
244 		oldcred = delegation->cred;
245 		delegation->cred = get_cred(cred);
246 		clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
247 		if (test_and_clear_bit(NFS_DELEGATION_REVOKED,
248 				       &delegation->flags))
249 			atomic_long_inc(&nfs_active_delegations);
250 		spin_unlock(&delegation->lock);
251 		rcu_read_unlock();
252 		put_cred(oldcred);
253 		trace_nfs4_reclaim_delegation(inode, type);
254 	} else {
255 		rcu_read_unlock();
256 		nfs_inode_set_delegation(inode, cred, type, stateid,
257 					 pagemod_limit);
258 	}
259 }
260 
261 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
262 {
263 	const struct cred *cred;
264 	int res = 0;
265 
266 	if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
267 		spin_lock(&delegation->lock);
268 		cred = get_cred(delegation->cred);
269 		spin_unlock(&delegation->lock);
270 		res = nfs4_proc_delegreturn(inode, cred,
271 				&delegation->stateid,
272 				issync);
273 		put_cred(cred);
274 	}
275 	return res;
276 }
277 
278 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
279 {
280 	struct inode *inode = NULL;
281 
282 	spin_lock(&delegation->lock);
283 	if (delegation->inode != NULL)
284 		inode = igrab(delegation->inode);
285 	if (!inode)
286 		set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
287 	spin_unlock(&delegation->lock);
288 	return inode;
289 }
290 
291 static struct nfs_delegation *
292 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
293 {
294 	struct nfs_delegation *ret = NULL;
295 	struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
296 
297 	if (delegation == NULL)
298 		goto out;
299 	spin_lock(&delegation->lock);
300 	if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
301 		clear_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
302 		/* Refcount matched in nfs_end_delegation_return() */
303 		ret = nfs_get_delegation(delegation);
304 	}
305 	spin_unlock(&delegation->lock);
306 	if (ret)
307 		nfs_clear_verifier_delegated(&nfsi->vfs_inode);
308 out:
309 	return ret;
310 }
311 
312 static struct nfs_delegation *
313 nfs_start_delegation_return(struct nfs_inode *nfsi)
314 {
315 	struct nfs_delegation *delegation;
316 
317 	rcu_read_lock();
318 	delegation = nfs_start_delegation_return_locked(nfsi);
319 	rcu_read_unlock();
320 	return delegation;
321 }
322 
323 static void nfs_abort_delegation_return(struct nfs_delegation *delegation,
324 					struct nfs_client *clp, int err)
325 {
326 
327 	spin_lock(&delegation->lock);
328 	clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
329 	if (err == -EAGAIN) {
330 		set_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
331 		set_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state);
332 	}
333 	spin_unlock(&delegation->lock);
334 }
335 
336 static struct nfs_delegation *
337 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
338 		struct nfs_delegation *delegation,
339 		struct nfs_client *clp)
340 {
341 	struct nfs_delegation *deleg_cur =
342 		rcu_dereference_protected(nfsi->delegation,
343 				lockdep_is_held(&clp->cl_lock));
344 
345 	if (deleg_cur == NULL || delegation != deleg_cur)
346 		return NULL;
347 
348 	spin_lock(&delegation->lock);
349 	if (!delegation->inode) {
350 		spin_unlock(&delegation->lock);
351 		return NULL;
352 	}
353 	list_del_rcu(&delegation->super_list);
354 	delegation->inode = NULL;
355 	rcu_assign_pointer(nfsi->delegation, NULL);
356 	spin_unlock(&delegation->lock);
357 	return delegation;
358 }
359 
360 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
361 		struct nfs_delegation *delegation,
362 		struct nfs_server *server)
363 {
364 	struct nfs_client *clp = server->nfs_client;
365 
366 	spin_lock(&clp->cl_lock);
367 	delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
368 	spin_unlock(&clp->cl_lock);
369 	return delegation;
370 }
371 
372 static struct nfs_delegation *
373 nfs_inode_detach_delegation(struct inode *inode)
374 {
375 	struct nfs_inode *nfsi = NFS_I(inode);
376 	struct nfs_server *server = NFS_SERVER(inode);
377 	struct nfs_delegation *delegation;
378 
379 	rcu_read_lock();
380 	delegation = rcu_dereference(nfsi->delegation);
381 	if (delegation != NULL)
382 		delegation = nfs_detach_delegation(nfsi, delegation, server);
383 	rcu_read_unlock();
384 	return delegation;
385 }
386 
387 static void
388 nfs_update_delegation_cred(struct nfs_delegation *delegation,
389 		const struct cred *cred)
390 {
391 	const struct cred *old;
392 
393 	if (cred_fscmp(delegation->cred, cred) != 0) {
394 		old = xchg(&delegation->cred, get_cred(cred));
395 		put_cred(old);
396 	}
397 }
398 
399 static void
400 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
401 		const struct nfs_delegation *update)
402 {
403 	if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
404 		delegation->stateid.seqid = update->stateid.seqid;
405 		smp_wmb();
406 		delegation->type = update->type;
407 		delegation->pagemod_limit = update->pagemod_limit;
408 		if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
409 			delegation->change_attr = update->change_attr;
410 			nfs_update_delegation_cred(delegation, update->cred);
411 			/* smp_mb__before_atomic() is implicit due to xchg() */
412 			clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
413 			atomic_long_inc(&nfs_active_delegations);
414 		}
415 	}
416 }
417 
418 /**
419  * nfs_inode_set_delegation - set up a delegation on an inode
420  * @inode: inode to which delegation applies
421  * @cred: cred to use for subsequent delegation processing
422  * @type: delegation type
423  * @stateid: delegation stateid
424  * @pagemod_limit: write delegation "space_limit"
425  *
426  * Returns zero on success, or a negative errno value.
427  */
428 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
429 				  fmode_t type,
430 				  const nfs4_stateid *stateid,
431 				  unsigned long pagemod_limit)
432 {
433 	struct nfs_server *server = NFS_SERVER(inode);
434 	struct nfs_client *clp = server->nfs_client;
435 	struct nfs_inode *nfsi = NFS_I(inode);
436 	struct nfs_delegation *delegation, *old_delegation;
437 	struct nfs_delegation *freeme = NULL;
438 	int status = 0;
439 
440 	delegation = kmalloc(sizeof(*delegation), GFP_KERNEL_ACCOUNT);
441 	if (delegation == NULL)
442 		return -ENOMEM;
443 	nfs4_stateid_copy(&delegation->stateid, stateid);
444 	refcount_set(&delegation->refcount, 1);
445 	delegation->type = type;
446 	delegation->pagemod_limit = pagemod_limit;
447 	delegation->change_attr = inode_peek_iversion_raw(inode);
448 	delegation->cred = get_cred(cred);
449 	delegation->inode = inode;
450 	delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
451 	delegation->test_gen = 0;
452 	spin_lock_init(&delegation->lock);
453 
454 	spin_lock(&clp->cl_lock);
455 	old_delegation = rcu_dereference_protected(nfsi->delegation,
456 					lockdep_is_held(&clp->cl_lock));
457 	if (old_delegation == NULL)
458 		goto add_new;
459 	/* Is this an update of the existing delegation? */
460 	if (nfs4_stateid_match_other(&old_delegation->stateid,
461 				&delegation->stateid)) {
462 		spin_lock(&old_delegation->lock);
463 		nfs_update_inplace_delegation(old_delegation,
464 				delegation);
465 		spin_unlock(&old_delegation->lock);
466 		goto out;
467 	}
468 	if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
469 		/*
470 		 * Deal with broken servers that hand out two
471 		 * delegations for the same file.
472 		 * Allow for upgrades to a WRITE delegation, but
473 		 * nothing else.
474 		 */
475 		dfprintk(FILE, "%s: server %s handed out "
476 				"a duplicate delegation!\n",
477 				__func__, clp->cl_hostname);
478 		if (delegation->type == old_delegation->type ||
479 		    !(delegation->type & FMODE_WRITE)) {
480 			freeme = delegation;
481 			delegation = NULL;
482 			goto out;
483 		}
484 		if (test_and_set_bit(NFS_DELEGATION_RETURNING,
485 					&old_delegation->flags))
486 			goto out;
487 	}
488 	freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
489 	if (freeme == NULL)
490 		goto out;
491 add_new:
492 	/*
493 	 * If we didn't revalidate the change attribute before setting
494 	 * the delegation, then pre-emptively ask for a full attribute
495 	 * cache revalidation.
496 	 */
497 	spin_lock(&inode->i_lock);
498 	if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_CHANGE)
499 		nfs_set_cache_invalid(inode,
500 			NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
501 			NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
502 			NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
503 			NFS_INO_INVALID_OTHER | NFS_INO_INVALID_DATA |
504 			NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
505 			NFS_INO_INVALID_XATTR);
506 	spin_unlock(&inode->i_lock);
507 
508 	list_add_tail_rcu(&delegation->super_list, &server->delegations);
509 	rcu_assign_pointer(nfsi->delegation, delegation);
510 	delegation = NULL;
511 
512 	atomic_long_inc(&nfs_active_delegations);
513 
514 	trace_nfs4_set_delegation(inode, type);
515 out:
516 	spin_unlock(&clp->cl_lock);
517 	if (delegation != NULL)
518 		__nfs_free_delegation(delegation);
519 	if (freeme != NULL) {
520 		nfs_do_return_delegation(inode, freeme, 0);
521 		nfs_free_delegation(freeme);
522 	}
523 	return status;
524 }
525 
526 /*
527  * Basic procedure for returning a delegation to the server
528  */
529 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
530 {
531 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
532 	unsigned int mode = O_WRONLY | O_RDWR;
533 	int err = 0;
534 
535 	if (delegation == NULL)
536 		return 0;
537 
538 	if (!issync)
539 		mode |= O_NONBLOCK;
540 	/* Recall of any remaining application leases */
541 	err = break_lease(inode, mode);
542 
543 	while (err == 0) {
544 		if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
545 			break;
546 		err = nfs_delegation_claim_opens(inode, &delegation->stateid,
547 				delegation->type);
548 		if (!issync || err != -EAGAIN)
549 			break;
550 		/*
551 		 * Guard against state recovery
552 		 */
553 		err = nfs4_wait_clnt_recover(clp);
554 	}
555 
556 	if (err) {
557 		nfs_abort_delegation_return(delegation, clp, err);
558 		goto out;
559 	}
560 
561 	err = nfs_do_return_delegation(inode, delegation, issync);
562 out:
563 	/* Refcount matched in nfs_start_delegation_return_locked() */
564 	nfs_put_delegation(delegation);
565 	return err;
566 }
567 
568 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
569 {
570 	bool ret = false;
571 
572 	if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
573 		ret = true;
574 	else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
575 		struct inode *inode;
576 
577 		spin_lock(&delegation->lock);
578 		inode = delegation->inode;
579 		if (inode && list_empty(&NFS_I(inode)->open_files))
580 			ret = true;
581 		spin_unlock(&delegation->lock);
582 	}
583 	if (ret)
584 		clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
585 	if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
586 	    test_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags) ||
587 	    test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
588 		ret = false;
589 
590 	return ret;
591 }
592 
593 static int nfs_server_return_marked_delegations(struct nfs_server *server,
594 		void __always_unused *data)
595 {
596 	struct nfs_delegation *delegation;
597 	struct nfs_delegation *prev;
598 	struct inode *inode;
599 	struct inode *place_holder = NULL;
600 	struct nfs_delegation *place_holder_deleg = NULL;
601 	int err = 0;
602 
603 restart:
604 	/*
605 	 * To avoid quadratic looping we hold a reference
606 	 * to an inode place_holder.  Each time we restart, we
607 	 * list delegation in the server from the delegations
608 	 * of that inode.
609 	 * prev is an RCU-protected pointer to a delegation which
610 	 * wasn't marked for return and might be a good choice for
611 	 * the next place_holder.
612 	 */
613 	prev = NULL;
614 	delegation = NULL;
615 	rcu_read_lock();
616 	if (place_holder)
617 		delegation = rcu_dereference(NFS_I(place_holder)->delegation);
618 	if (!delegation || delegation != place_holder_deleg)
619 		delegation = list_entry_rcu(server->delegations.next,
620 					    struct nfs_delegation, super_list);
621 	list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
622 		struct inode *to_put = NULL;
623 
624 		if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags))
625 			continue;
626 		if (!nfs_delegation_need_return(delegation)) {
627 			if (nfs4_is_valid_delegation(delegation, 0))
628 				prev = delegation;
629 			continue;
630 		}
631 
632 		if (prev) {
633 			struct inode *tmp = nfs_delegation_grab_inode(prev);
634 			if (tmp) {
635 				to_put = place_holder;
636 				place_holder = tmp;
637 				place_holder_deleg = prev;
638 			}
639 		}
640 
641 		inode = nfs_delegation_grab_inode(delegation);
642 		if (inode == NULL) {
643 			rcu_read_unlock();
644 			iput(to_put);
645 			goto restart;
646 		}
647 		delegation = nfs_start_delegation_return_locked(NFS_I(inode));
648 		rcu_read_unlock();
649 
650 		iput(to_put);
651 
652 		err = nfs_end_delegation_return(inode, delegation, 0);
653 		iput(inode);
654 		cond_resched();
655 		if (!err)
656 			goto restart;
657 		set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
658 		goto out;
659 	}
660 	rcu_read_unlock();
661 out:
662 	iput(place_holder);
663 	return err;
664 }
665 
666 static bool nfs_server_clear_delayed_delegations(struct nfs_server *server)
667 {
668 	struct nfs_delegation *d;
669 	bool ret = false;
670 
671 	list_for_each_entry_rcu (d, &server->delegations, super_list) {
672 		if (!test_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags))
673 			continue;
674 		nfs_mark_return_delegation(server, d);
675 		clear_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags);
676 		ret = true;
677 	}
678 	return ret;
679 }
680 
681 static bool nfs_client_clear_delayed_delegations(struct nfs_client *clp)
682 {
683 	struct nfs_server *server;
684 	bool ret = false;
685 
686 	if (!test_and_clear_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state))
687 		goto out;
688 	rcu_read_lock();
689 	list_for_each_entry_rcu (server, &clp->cl_superblocks, client_link) {
690 		if (nfs_server_clear_delayed_delegations(server))
691 			ret = true;
692 	}
693 	rcu_read_unlock();
694 out:
695 	return ret;
696 }
697 
698 /**
699  * nfs_client_return_marked_delegations - return previously marked delegations
700  * @clp: nfs_client to process
701  *
702  * Note that this function is designed to be called by the state
703  * manager thread. For this reason, it cannot flush the dirty data,
704  * since that could deadlock in case of a state recovery error.
705  *
706  * Returns zero on success, or a negative errno value.
707  */
708 int nfs_client_return_marked_delegations(struct nfs_client *clp)
709 {
710 	int err = nfs_client_for_each_server(
711 		clp, nfs_server_return_marked_delegations, NULL);
712 	if (err)
713 		return err;
714 	/* If a return was delayed, sleep to prevent hard looping */
715 	if (nfs_client_clear_delayed_delegations(clp))
716 		ssleep(1);
717 	return 0;
718 }
719 
720 /**
721  * nfs_inode_evict_delegation - return delegation, don't reclaim opens
722  * @inode: inode to process
723  *
724  * Does not protect against delegation reclaims, therefore really only safe
725  * to be called from nfs4_clear_inode(). Guaranteed to always free
726  * the delegation structure.
727  */
728 void nfs_inode_evict_delegation(struct inode *inode)
729 {
730 	struct nfs_delegation *delegation;
731 
732 	delegation = nfs_inode_detach_delegation(inode);
733 	if (delegation != NULL) {
734 		set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
735 		set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
736 		nfs_do_return_delegation(inode, delegation, 1);
737 		nfs_free_delegation(delegation);
738 	}
739 }
740 
741 /**
742  * nfs4_inode_return_delegation - synchronously return a delegation
743  * @inode: inode to process
744  *
745  * This routine will always flush any dirty data to disk on the
746  * assumption that if we need to return the delegation, then
747  * we should stop caching.
748  *
749  * Returns zero on success, or a negative errno value.
750  */
751 int nfs4_inode_return_delegation(struct inode *inode)
752 {
753 	struct nfs_inode *nfsi = NFS_I(inode);
754 	struct nfs_delegation *delegation;
755 
756 	delegation = nfs_start_delegation_return(nfsi);
757 	if (delegation != NULL) {
758 		/* Synchronous recall of any application leases */
759 		break_lease(inode, O_WRONLY | O_RDWR);
760 		if (S_ISREG(inode->i_mode))
761 			nfs_wb_all(inode);
762 		return nfs_end_delegation_return(inode, delegation, 1);
763 	}
764 	return 0;
765 }
766 
767 /**
768  * nfs4_inode_return_delegation_on_close - asynchronously return a delegation
769  * @inode: inode to process
770  *
771  * This routine is called on file close in order to determine if the
772  * inode delegation needs to be returned immediately.
773  */
774 void nfs4_inode_return_delegation_on_close(struct inode *inode)
775 {
776 	struct nfs_delegation *delegation;
777 	struct nfs_delegation *ret = NULL;
778 
779 	if (!inode)
780 		return;
781 	rcu_read_lock();
782 	delegation = nfs4_get_valid_delegation(inode);
783 	if (!delegation)
784 		goto out;
785 	if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
786 	    atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
787 		spin_lock(&delegation->lock);
788 		if (delegation->inode &&
789 		    list_empty(&NFS_I(inode)->open_files) &&
790 		    !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
791 			clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
792 			/* Refcount matched in nfs_end_delegation_return() */
793 			ret = nfs_get_delegation(delegation);
794 		}
795 		spin_unlock(&delegation->lock);
796 		if (ret)
797 			nfs_clear_verifier_delegated(inode);
798 	}
799 out:
800 	rcu_read_unlock();
801 	nfs_end_delegation_return(inode, ret, 0);
802 }
803 
804 /**
805  * nfs4_inode_make_writeable
806  * @inode: pointer to inode
807  *
808  * Make the inode writeable by returning the delegation if necessary
809  *
810  * Returns zero on success, or a negative errno value.
811  */
812 int nfs4_inode_make_writeable(struct inode *inode)
813 {
814 	struct nfs_delegation *delegation;
815 
816 	rcu_read_lock();
817 	delegation = nfs4_get_valid_delegation(inode);
818 	if (delegation == NULL ||
819 	    (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
820 	     (delegation->type & FMODE_WRITE))) {
821 		rcu_read_unlock();
822 		return 0;
823 	}
824 	rcu_read_unlock();
825 	return nfs4_inode_return_delegation(inode);
826 }
827 
828 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
829 		struct nfs_delegation *delegation)
830 {
831 	set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
832 	set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
833 }
834 
835 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
836 {
837 	struct nfs_delegation *delegation;
838 	bool ret = false;
839 
840 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
841 		nfs_mark_return_delegation(server, delegation);
842 		ret = true;
843 	}
844 	return ret;
845 }
846 
847 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
848 {
849 	struct nfs_server *server;
850 
851 	rcu_read_lock();
852 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
853 		nfs_server_mark_return_all_delegations(server);
854 	rcu_read_unlock();
855 }
856 
857 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
858 {
859 	if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
860 		nfs4_schedule_state_manager(clp);
861 }
862 
863 /**
864  * nfs_expire_all_delegations
865  * @clp: client to process
866  *
867  */
868 void nfs_expire_all_delegations(struct nfs_client *clp)
869 {
870 	nfs_client_mark_return_all_delegations(clp);
871 	nfs_delegation_run_state_manager(clp);
872 }
873 
874 /**
875  * nfs_server_return_all_delegations - return delegations for one superblock
876  * @server: pointer to nfs_server to process
877  *
878  */
879 void nfs_server_return_all_delegations(struct nfs_server *server)
880 {
881 	struct nfs_client *clp = server->nfs_client;
882 	bool need_wait;
883 
884 	if (clp == NULL)
885 		return;
886 
887 	rcu_read_lock();
888 	need_wait = nfs_server_mark_return_all_delegations(server);
889 	rcu_read_unlock();
890 
891 	if (need_wait) {
892 		nfs4_schedule_state_manager(clp);
893 		nfs4_wait_clnt_recover(clp);
894 	}
895 }
896 
897 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
898 						 fmode_t flags)
899 {
900 	struct nfs_delegation *delegation;
901 
902 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
903 		if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
904 			continue;
905 		if (delegation->type & flags)
906 			nfs_mark_return_if_closed_delegation(server, delegation);
907 	}
908 }
909 
910 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
911 							fmode_t flags)
912 {
913 	struct nfs_server *server;
914 
915 	rcu_read_lock();
916 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
917 		nfs_mark_return_unused_delegation_types(server, flags);
918 	rcu_read_unlock();
919 }
920 
921 static void nfs_revoke_delegation(struct inode *inode,
922 		const nfs4_stateid *stateid)
923 {
924 	struct nfs_delegation *delegation;
925 	nfs4_stateid tmp;
926 	bool ret = false;
927 
928 	rcu_read_lock();
929 	delegation = rcu_dereference(NFS_I(inode)->delegation);
930 	if (delegation == NULL)
931 		goto out;
932 	if (stateid == NULL) {
933 		nfs4_stateid_copy(&tmp, &delegation->stateid);
934 		stateid = &tmp;
935 	} else {
936 		if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
937 			goto out;
938 		spin_lock(&delegation->lock);
939 		if (stateid->seqid) {
940 			if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
941 				spin_unlock(&delegation->lock);
942 				goto out;
943 			}
944 			delegation->stateid.seqid = stateid->seqid;
945 		}
946 		spin_unlock(&delegation->lock);
947 	}
948 	nfs_mark_delegation_revoked(delegation);
949 	ret = true;
950 out:
951 	rcu_read_unlock();
952 	if (ret)
953 		nfs_inode_find_state_and_recover(inode, stateid);
954 }
955 
956 void nfs_remove_bad_delegation(struct inode *inode,
957 		const nfs4_stateid *stateid)
958 {
959 	nfs_revoke_delegation(inode, stateid);
960 }
961 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
962 
963 void nfs_delegation_mark_returned(struct inode *inode,
964 		const nfs4_stateid *stateid)
965 {
966 	struct nfs_delegation *delegation;
967 
968 	if (!inode)
969 		return;
970 
971 	rcu_read_lock();
972 	delegation = rcu_dereference(NFS_I(inode)->delegation);
973 	if (!delegation)
974 		goto out_rcu_unlock;
975 
976 	spin_lock(&delegation->lock);
977 	if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
978 		goto out_spin_unlock;
979 	if (stateid->seqid) {
980 		/* If delegation->stateid is newer, dont mark as returned */
981 		if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
982 			goto out_clear_returning;
983 		if (delegation->stateid.seqid != stateid->seqid)
984 			delegation->stateid.seqid = stateid->seqid;
985 	}
986 
987 	nfs_mark_delegation_revoked(delegation);
988 
989 out_clear_returning:
990 	clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
991 out_spin_unlock:
992 	spin_unlock(&delegation->lock);
993 out_rcu_unlock:
994 	rcu_read_unlock();
995 
996 	nfs_inode_find_state_and_recover(inode, stateid);
997 }
998 
999 /**
1000  * nfs_expire_unused_delegation_types
1001  * @clp: client to process
1002  * @flags: delegation types to expire
1003  *
1004  */
1005 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
1006 {
1007 	nfs_client_mark_return_unused_delegation_types(clp, flags);
1008 	nfs_delegation_run_state_manager(clp);
1009 }
1010 
1011 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
1012 {
1013 	struct nfs_delegation *delegation;
1014 
1015 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1016 		if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
1017 			continue;
1018 		nfs_mark_return_if_closed_delegation(server, delegation);
1019 	}
1020 }
1021 
1022 /**
1023  * nfs_expire_unreferenced_delegations - Eliminate unused delegations
1024  * @clp: nfs_client to process
1025  *
1026  */
1027 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
1028 {
1029 	struct nfs_server *server;
1030 
1031 	rcu_read_lock();
1032 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1033 		nfs_mark_return_unreferenced_delegations(server);
1034 	rcu_read_unlock();
1035 
1036 	nfs_delegation_run_state_manager(clp);
1037 }
1038 
1039 /**
1040  * nfs_async_inode_return_delegation - asynchronously return a delegation
1041  * @inode: inode to process
1042  * @stateid: state ID information
1043  *
1044  * Returns zero on success, or a negative errno value.
1045  */
1046 int nfs_async_inode_return_delegation(struct inode *inode,
1047 				      const nfs4_stateid *stateid)
1048 {
1049 	struct nfs_server *server = NFS_SERVER(inode);
1050 	struct nfs_client *clp = server->nfs_client;
1051 	struct nfs_delegation *delegation;
1052 
1053 	rcu_read_lock();
1054 	delegation = nfs4_get_valid_delegation(inode);
1055 	if (delegation == NULL)
1056 		goto out_enoent;
1057 	if (stateid != NULL &&
1058 	    !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
1059 		goto out_enoent;
1060 	nfs_mark_return_delegation(server, delegation);
1061 	rcu_read_unlock();
1062 
1063 	/* If there are any application leases or delegations, recall them */
1064 	break_lease(inode, O_WRONLY | O_RDWR | O_NONBLOCK);
1065 
1066 	nfs_delegation_run_state_manager(clp);
1067 	return 0;
1068 out_enoent:
1069 	rcu_read_unlock();
1070 	return -ENOENT;
1071 }
1072 
1073 static struct inode *
1074 nfs_delegation_find_inode_server(struct nfs_server *server,
1075 				 const struct nfs_fh *fhandle)
1076 {
1077 	struct nfs_delegation *delegation;
1078 	struct super_block *freeme = NULL;
1079 	struct inode *res = NULL;
1080 
1081 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1082 		spin_lock(&delegation->lock);
1083 		if (delegation->inode != NULL &&
1084 		    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1085 		    nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1086 			if (nfs_sb_active(server->super)) {
1087 				freeme = server->super;
1088 				res = igrab(delegation->inode);
1089 			}
1090 			spin_unlock(&delegation->lock);
1091 			if (res != NULL)
1092 				return res;
1093 			if (freeme) {
1094 				rcu_read_unlock();
1095 				nfs_sb_deactive(freeme);
1096 				rcu_read_lock();
1097 			}
1098 			return ERR_PTR(-EAGAIN);
1099 		}
1100 		spin_unlock(&delegation->lock);
1101 	}
1102 	return ERR_PTR(-ENOENT);
1103 }
1104 
1105 /**
1106  * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1107  * @clp: client state handle
1108  * @fhandle: filehandle from a delegation recall
1109  *
1110  * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1111  * cannot be found.
1112  */
1113 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1114 					const struct nfs_fh *fhandle)
1115 {
1116 	struct nfs_server *server;
1117 	struct inode *res;
1118 
1119 	rcu_read_lock();
1120 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1121 		res = nfs_delegation_find_inode_server(server, fhandle);
1122 		if (res != ERR_PTR(-ENOENT)) {
1123 			rcu_read_unlock();
1124 			return res;
1125 		}
1126 	}
1127 	rcu_read_unlock();
1128 	return ERR_PTR(-ENOENT);
1129 }
1130 
1131 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1132 {
1133 	struct nfs_delegation *delegation;
1134 
1135 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1136 		/*
1137 		 * If the delegation may have been admin revoked, then we
1138 		 * cannot reclaim it.
1139 		 */
1140 		if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1141 			continue;
1142 		set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1143 	}
1144 }
1145 
1146 /**
1147  * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1148  * @clp: nfs_client to process
1149  *
1150  */
1151 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1152 {
1153 	struct nfs_server *server;
1154 
1155 	rcu_read_lock();
1156 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1157 		nfs_delegation_mark_reclaim_server(server);
1158 	rcu_read_unlock();
1159 }
1160 
1161 static int nfs_server_reap_unclaimed_delegations(struct nfs_server *server,
1162 		void __always_unused *data)
1163 {
1164 	struct nfs_delegation *delegation;
1165 	struct inode *inode;
1166 restart:
1167 	rcu_read_lock();
1168 restart_locked:
1169 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1170 		if (test_bit(NFS_DELEGATION_INODE_FREEING,
1171 					&delegation->flags) ||
1172 		    test_bit(NFS_DELEGATION_RETURNING,
1173 					&delegation->flags) ||
1174 		    test_bit(NFS_DELEGATION_NEED_RECLAIM,
1175 					&delegation->flags) == 0)
1176 			continue;
1177 		inode = nfs_delegation_grab_inode(delegation);
1178 		if (inode == NULL)
1179 			goto restart_locked;
1180 		delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1181 		rcu_read_unlock();
1182 		if (delegation != NULL) {
1183 			if (nfs_detach_delegation(NFS_I(inode), delegation,
1184 						server) != NULL)
1185 				nfs_free_delegation(delegation);
1186 			/* Match nfs_start_delegation_return_locked */
1187 			nfs_put_delegation(delegation);
1188 		}
1189 		iput(inode);
1190 		cond_resched();
1191 		goto restart;
1192 	}
1193 	rcu_read_unlock();
1194 	return 0;
1195 }
1196 
1197 /**
1198  * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1199  * @clp: nfs_client to process
1200  *
1201  */
1202 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1203 {
1204 	nfs_client_for_each_server(clp, nfs_server_reap_unclaimed_delegations,
1205 			NULL);
1206 }
1207 
1208 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1209 {
1210 	return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1211 				BIT(NFS4CLNT_LEASE_EXPIRED) |
1212 				BIT(NFS4CLNT_SESSION_RESET))) != 0;
1213 }
1214 
1215 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1216 	    struct nfs_delegation *delegation)
1217 {
1218 	if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1219 		return;
1220 	clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1221 	set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1222 	set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1223 }
1224 
1225 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1226 		struct inode *inode)
1227 {
1228 	struct nfs_delegation *delegation;
1229 
1230 	rcu_read_lock();
1231 	delegation = rcu_dereference(NFS_I(inode)->delegation);
1232 	if (delegation)
1233 		nfs_mark_test_expired_delegation(server, delegation);
1234 	rcu_read_unlock();
1235 
1236 }
1237 
1238 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1239 {
1240 	struct nfs_delegation *delegation;
1241 
1242 	list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1243 		nfs_mark_test_expired_delegation(server, delegation);
1244 }
1245 
1246 /**
1247  * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1248  * @clp: nfs_client to process
1249  *
1250  * Iterates through all the delegations associated with this server and
1251  * marks them as needing to be checked for validity.
1252  */
1253 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1254 {
1255 	struct nfs_server *server;
1256 
1257 	rcu_read_lock();
1258 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1259 		nfs_delegation_mark_test_expired_server(server);
1260 	rcu_read_unlock();
1261 }
1262 
1263 /**
1264  * nfs_test_expired_all_delegations - test all delegations for a client
1265  * @clp: nfs_client to process
1266  *
1267  * Helper for handling "recallable state revoked" status from server.
1268  */
1269 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1270 {
1271 	nfs_mark_test_expired_all_delegations(clp);
1272 	nfs4_schedule_state_manager(clp);
1273 }
1274 
1275 static void
1276 nfs_delegation_test_free_expired(struct inode *inode,
1277 		nfs4_stateid *stateid,
1278 		const struct cred *cred)
1279 {
1280 	struct nfs_server *server = NFS_SERVER(inode);
1281 	const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1282 	int status;
1283 
1284 	if (!cred)
1285 		return;
1286 	status = ops->test_and_free_expired(server, stateid, cred);
1287 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1288 		nfs_remove_bad_delegation(inode, stateid);
1289 }
1290 
1291 static int nfs_server_reap_expired_delegations(struct nfs_server *server,
1292 		void __always_unused *data)
1293 {
1294 	struct nfs_delegation *delegation;
1295 	struct inode *inode;
1296 	const struct cred *cred;
1297 	nfs4_stateid stateid;
1298 	unsigned long gen = ++server->delegation_gen;
1299 
1300 restart:
1301 	rcu_read_lock();
1302 restart_locked:
1303 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1304 		if (test_bit(NFS_DELEGATION_INODE_FREEING,
1305 					&delegation->flags) ||
1306 		    test_bit(NFS_DELEGATION_RETURNING,
1307 					&delegation->flags) ||
1308 		    test_bit(NFS_DELEGATION_TEST_EXPIRED,
1309 					&delegation->flags) == 0 ||
1310 			delegation->test_gen == gen)
1311 			continue;
1312 		inode = nfs_delegation_grab_inode(delegation);
1313 		if (inode == NULL)
1314 			goto restart_locked;
1315 		spin_lock(&delegation->lock);
1316 		cred = get_cred_rcu(delegation->cred);
1317 		nfs4_stateid_copy(&stateid, &delegation->stateid);
1318 		spin_unlock(&delegation->lock);
1319 		delegation->test_gen = gen;
1320 		clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1321 		rcu_read_unlock();
1322 		nfs_delegation_test_free_expired(inode, &stateid, cred);
1323 		put_cred(cred);
1324 		if (!nfs4_server_rebooted(server->nfs_client)) {
1325 			iput(inode);
1326 			cond_resched();
1327 			goto restart;
1328 		}
1329 		nfs_inode_mark_test_expired_delegation(server,inode);
1330 		iput(inode);
1331 		return -EAGAIN;
1332 	}
1333 	rcu_read_unlock();
1334 	return 0;
1335 }
1336 
1337 /**
1338  * nfs_reap_expired_delegations - reap expired delegations
1339  * @clp: nfs_client to process
1340  *
1341  * Iterates through all the delegations associated with this server and
1342  * checks if they have may have been revoked. This function is usually
1343  * expected to be called in cases where the server may have lost its
1344  * lease.
1345  */
1346 void nfs_reap_expired_delegations(struct nfs_client *clp)
1347 {
1348 	nfs_client_for_each_server(clp, nfs_server_reap_expired_delegations,
1349 			NULL);
1350 }
1351 
1352 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1353 		const nfs4_stateid *stateid)
1354 {
1355 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1356 	struct nfs_delegation *delegation;
1357 	bool found = false;
1358 
1359 	rcu_read_lock();
1360 	delegation = rcu_dereference(NFS_I(inode)->delegation);
1361 	if (delegation &&
1362 	    nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1363 	    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1364 		nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1365 		found = true;
1366 	}
1367 	rcu_read_unlock();
1368 	if (found)
1369 		nfs4_schedule_state_manager(clp);
1370 }
1371 
1372 /**
1373  * nfs_delegations_present - check for existence of delegations
1374  * @clp: client state handle
1375  *
1376  * Returns one if there are any nfs_delegation structures attached
1377  * to this nfs_client.
1378  */
1379 int nfs_delegations_present(struct nfs_client *clp)
1380 {
1381 	struct nfs_server *server;
1382 	int ret = 0;
1383 
1384 	rcu_read_lock();
1385 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1386 		if (!list_empty(&server->delegations)) {
1387 			ret = 1;
1388 			break;
1389 		}
1390 	rcu_read_unlock();
1391 	return ret;
1392 }
1393 
1394 /**
1395  * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1396  * @dst: stateid to refresh
1397  * @inode: inode to check
1398  *
1399  * Returns "true" and updates "dst->seqid" * if inode had a delegation
1400  * that matches our delegation stateid. Otherwise "false" is returned.
1401  */
1402 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1403 {
1404 	struct nfs_delegation *delegation;
1405 	bool ret = false;
1406 	if (!inode)
1407 		goto out;
1408 
1409 	rcu_read_lock();
1410 	delegation = rcu_dereference(NFS_I(inode)->delegation);
1411 	if (delegation != NULL &&
1412 	    nfs4_stateid_match_other(dst, &delegation->stateid) &&
1413 	    nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1414 	    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1415 		dst->seqid = delegation->stateid.seqid;
1416 		ret = true;
1417 	}
1418 	rcu_read_unlock();
1419 out:
1420 	return ret;
1421 }
1422 
1423 /**
1424  * nfs4_copy_delegation_stateid - Copy inode's state ID information
1425  * @inode: inode to check
1426  * @flags: delegation type requirement
1427  * @dst: stateid data structure to fill in
1428  * @cred: optional argument to retrieve credential
1429  *
1430  * Returns "true" and fills in "dst->data" * if inode had a delegation,
1431  * otherwise "false" is returned.
1432  */
1433 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1434 		nfs4_stateid *dst, const struct cred **cred)
1435 {
1436 	struct nfs_inode *nfsi = NFS_I(inode);
1437 	struct nfs_delegation *delegation;
1438 	bool ret = false;
1439 
1440 	flags &= FMODE_READ|FMODE_WRITE;
1441 	rcu_read_lock();
1442 	delegation = rcu_dereference(nfsi->delegation);
1443 	if (!delegation)
1444 		goto out;
1445 	spin_lock(&delegation->lock);
1446 	ret = nfs4_is_valid_delegation(delegation, flags);
1447 	if (ret) {
1448 		nfs4_stateid_copy(dst, &delegation->stateid);
1449 		nfs_mark_delegation_referenced(delegation);
1450 		if (cred)
1451 			*cred = get_cred(delegation->cred);
1452 	}
1453 	spin_unlock(&delegation->lock);
1454 out:
1455 	rcu_read_unlock();
1456 	return ret;
1457 }
1458 
1459 /**
1460  * nfs4_delegation_flush_on_close - Check if we must flush file on close
1461  * @inode: inode to check
1462  *
1463  * This function checks the number of outstanding writes to the file
1464  * against the delegation 'space_limit' field to see if
1465  * the spec requires us to flush the file on close.
1466  */
1467 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1468 {
1469 	struct nfs_inode *nfsi = NFS_I(inode);
1470 	struct nfs_delegation *delegation;
1471 	bool ret = true;
1472 
1473 	rcu_read_lock();
1474 	delegation = rcu_dereference(nfsi->delegation);
1475 	if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1476 		goto out;
1477 	if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1478 		ret = false;
1479 out:
1480 	rcu_read_unlock();
1481 	return ret;
1482 }
1483 
1484 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);
1485