xref: /openbmc/linux/fs/nfs/delegation.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
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 
__nfs_free_delegation(struct nfs_delegation * delegation)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 
nfs_mark_delegation_revoked(struct nfs_delegation * delegation)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 
nfs_get_delegation(struct nfs_delegation * delegation)50 static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
51 {
52 	refcount_inc(&delegation->refcount);
53 	return delegation;
54 }
55 
nfs_put_delegation(struct nfs_delegation * delegation)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 
nfs_free_delegation(struct nfs_delegation * delegation)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  */
nfs_mark_delegation_referenced(struct nfs_delegation * delegation)73 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
74 {
75 	set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
76 }
77 
nfs_mark_return_delegation(struct nfs_server * server,struct nfs_delegation * delegation)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
nfs4_is_valid_delegation(const struct nfs_delegation * delegation,fmode_t flags)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 
nfs4_get_valid_delegation(const struct inode * inode)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
nfs4_do_check_delegation(struct inode * inode,fmode_t flags,bool mark)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  */
nfs4_have_delegation(struct inode * inode,fmode_t flags)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  */
nfs4_check_delegation(struct inode * inode,fmode_t flags)140 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
141 {
142 	return nfs4_do_check_delegation(inode, flags, false);
143 }
144 
nfs_delegation_claim_locks(struct nfs4_state * state,const nfs4_stateid * stateid)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 
nfs_delegation_claim_opens(struct inode * inode,const nfs4_stateid * stateid,fmode_t type)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  */
nfs_inode_reclaim_delegation(struct inode * inode,const struct cred * cred,fmode_t type,const nfs4_stateid * stateid,unsigned long pagemod_limit)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 
nfs_do_return_delegation(struct inode * inode,struct nfs_delegation * delegation,int issync)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 
nfs_delegation_grab_inode(struct nfs_delegation * delegation)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 *
nfs_start_delegation_return_locked(struct nfs_inode * nfsi)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 (delegation->inode &&
301 	    !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
302 		clear_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
303 		/* Refcount matched in nfs_end_delegation_return() */
304 		ret = nfs_get_delegation(delegation);
305 	}
306 	spin_unlock(&delegation->lock);
307 	if (ret)
308 		nfs_clear_verifier_delegated(&nfsi->vfs_inode);
309 out:
310 	return ret;
311 }
312 
313 static struct nfs_delegation *
nfs_start_delegation_return(struct nfs_inode * nfsi)314 nfs_start_delegation_return(struct nfs_inode *nfsi)
315 {
316 	struct nfs_delegation *delegation;
317 
318 	rcu_read_lock();
319 	delegation = nfs_start_delegation_return_locked(nfsi);
320 	rcu_read_unlock();
321 	return delegation;
322 }
323 
nfs_abort_delegation_return(struct nfs_delegation * delegation,struct nfs_client * clp,int err)324 static void nfs_abort_delegation_return(struct nfs_delegation *delegation,
325 					struct nfs_client *clp, int err)
326 {
327 
328 	spin_lock(&delegation->lock);
329 	clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
330 	if (err == -EAGAIN) {
331 		set_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
332 		set_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state);
333 	}
334 	spin_unlock(&delegation->lock);
335 }
336 
337 static struct nfs_delegation *
nfs_detach_delegation_locked(struct nfs_inode * nfsi,struct nfs_delegation * delegation,struct nfs_client * clp)338 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
339 		struct nfs_delegation *delegation,
340 		struct nfs_client *clp)
341 {
342 	struct nfs_delegation *deleg_cur =
343 		rcu_dereference_protected(nfsi->delegation,
344 				lockdep_is_held(&clp->cl_lock));
345 
346 	if (deleg_cur == NULL || delegation != deleg_cur)
347 		return NULL;
348 
349 	spin_lock(&delegation->lock);
350 	if (!delegation->inode) {
351 		spin_unlock(&delegation->lock);
352 		return NULL;
353 	}
354 	list_del_rcu(&delegation->super_list);
355 	delegation->inode = NULL;
356 	rcu_assign_pointer(nfsi->delegation, NULL);
357 	spin_unlock(&delegation->lock);
358 	return delegation;
359 }
360 
nfs_detach_delegation(struct nfs_inode * nfsi,struct nfs_delegation * delegation,struct nfs_server * server)361 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
362 		struct nfs_delegation *delegation,
363 		struct nfs_server *server)
364 {
365 	struct nfs_client *clp = server->nfs_client;
366 
367 	spin_lock(&clp->cl_lock);
368 	delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
369 	spin_unlock(&clp->cl_lock);
370 	return delegation;
371 }
372 
373 static struct nfs_delegation *
nfs_inode_detach_delegation(struct inode * inode)374 nfs_inode_detach_delegation(struct inode *inode)
375 {
376 	struct nfs_inode *nfsi = NFS_I(inode);
377 	struct nfs_server *server = NFS_SERVER(inode);
378 	struct nfs_delegation *delegation;
379 
380 	rcu_read_lock();
381 	delegation = rcu_dereference(nfsi->delegation);
382 	if (delegation != NULL)
383 		delegation = nfs_detach_delegation(nfsi, delegation, server);
384 	rcu_read_unlock();
385 	return delegation;
386 }
387 
388 static void
nfs_update_delegation_cred(struct nfs_delegation * delegation,const struct cred * cred)389 nfs_update_delegation_cred(struct nfs_delegation *delegation,
390 		const struct cred *cred)
391 {
392 	const struct cred *old;
393 
394 	if (cred_fscmp(delegation->cred, cred) != 0) {
395 		old = xchg(&delegation->cred, get_cred(cred));
396 		put_cred(old);
397 	}
398 }
399 
400 static void
nfs_update_inplace_delegation(struct nfs_delegation * delegation,const struct nfs_delegation * update)401 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
402 		const struct nfs_delegation *update)
403 {
404 	if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
405 		delegation->stateid.seqid = update->stateid.seqid;
406 		smp_wmb();
407 		delegation->type = update->type;
408 		delegation->pagemod_limit = update->pagemod_limit;
409 		if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
410 			delegation->change_attr = update->change_attr;
411 			nfs_update_delegation_cred(delegation, update->cred);
412 			/* smp_mb__before_atomic() is implicit due to xchg() */
413 			clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
414 			atomic_long_inc(&nfs_active_delegations);
415 		}
416 	}
417 }
418 
419 /**
420  * nfs_inode_set_delegation - set up a delegation on an inode
421  * @inode: inode to which delegation applies
422  * @cred: cred to use for subsequent delegation processing
423  * @type: delegation type
424  * @stateid: delegation stateid
425  * @pagemod_limit: write delegation "space_limit"
426  *
427  * Returns zero on success, or a negative errno value.
428  */
nfs_inode_set_delegation(struct inode * inode,const struct cred * cred,fmode_t type,const nfs4_stateid * stateid,unsigned long pagemod_limit)429 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
430 				  fmode_t type,
431 				  const nfs4_stateid *stateid,
432 				  unsigned long pagemod_limit)
433 {
434 	struct nfs_server *server = NFS_SERVER(inode);
435 	struct nfs_client *clp = server->nfs_client;
436 	struct nfs_inode *nfsi = NFS_I(inode);
437 	struct nfs_delegation *delegation, *old_delegation;
438 	struct nfs_delegation *freeme = NULL;
439 	int status = 0;
440 
441 	delegation = kmalloc(sizeof(*delegation), GFP_KERNEL_ACCOUNT);
442 	if (delegation == NULL)
443 		return -ENOMEM;
444 	nfs4_stateid_copy(&delegation->stateid, stateid);
445 	refcount_set(&delegation->refcount, 1);
446 	delegation->type = type;
447 	delegation->pagemod_limit = pagemod_limit;
448 	delegation->change_attr = inode_peek_iversion_raw(inode);
449 	delegation->cred = get_cred(cred);
450 	delegation->inode = inode;
451 	delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
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  */
nfs_end_delegation_return(struct inode * inode,struct nfs_delegation * delegation,int issync)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 
nfs_delegation_need_return(struct nfs_delegation * delegation)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 	if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
575 	    test_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags) ||
576 	    test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
577 		ret = false;
578 
579 	return ret;
580 }
581 
nfs_server_return_marked_delegations(struct nfs_server * server,void __always_unused * data)582 static int nfs_server_return_marked_delegations(struct nfs_server *server,
583 		void __always_unused *data)
584 {
585 	struct nfs_delegation *delegation;
586 	struct nfs_delegation *prev;
587 	struct inode *inode;
588 	struct inode *place_holder = NULL;
589 	struct nfs_delegation *place_holder_deleg = NULL;
590 	int err = 0;
591 
592 restart:
593 	/*
594 	 * To avoid quadratic looping we hold a reference
595 	 * to an inode place_holder.  Each time we restart, we
596 	 * list delegation in the server from the delegations
597 	 * of that inode.
598 	 * prev is an RCU-protected pointer to a delegation which
599 	 * wasn't marked for return and might be a good choice for
600 	 * the next place_holder.
601 	 */
602 	prev = NULL;
603 	delegation = NULL;
604 	rcu_read_lock();
605 	if (place_holder)
606 		delegation = rcu_dereference(NFS_I(place_holder)->delegation);
607 	if (!delegation || delegation != place_holder_deleg)
608 		delegation = list_entry_rcu(server->delegations.next,
609 					    struct nfs_delegation, super_list);
610 	list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
611 		struct inode *to_put = NULL;
612 
613 		if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags))
614 			continue;
615 		if (!nfs_delegation_need_return(delegation)) {
616 			if (nfs4_is_valid_delegation(delegation, 0))
617 				prev = delegation;
618 			continue;
619 		}
620 		inode = nfs_delegation_grab_inode(delegation);
621 		if (inode == NULL)
622 			continue;
623 
624 		if (prev) {
625 			struct inode *tmp = nfs_delegation_grab_inode(prev);
626 			if (tmp) {
627 				to_put = place_holder;
628 				place_holder = tmp;
629 				place_holder_deleg = prev;
630 			}
631 		}
632 
633 		delegation = nfs_start_delegation_return_locked(NFS_I(inode));
634 		rcu_read_unlock();
635 
636 		iput(to_put);
637 
638 		err = nfs_end_delegation_return(inode, delegation, 0);
639 		iput(inode);
640 		cond_resched();
641 		if (!err)
642 			goto restart;
643 		set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
644 		goto out;
645 	}
646 	rcu_read_unlock();
647 out:
648 	iput(place_holder);
649 	return err;
650 }
651 
nfs_server_clear_delayed_delegations(struct nfs_server * server)652 static bool nfs_server_clear_delayed_delegations(struct nfs_server *server)
653 {
654 	struct nfs_delegation *d;
655 	bool ret = false;
656 
657 	list_for_each_entry_rcu (d, &server->delegations, super_list) {
658 		if (!test_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags))
659 			continue;
660 		nfs_mark_return_delegation(server, d);
661 		clear_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags);
662 		ret = true;
663 	}
664 	return ret;
665 }
666 
nfs_client_clear_delayed_delegations(struct nfs_client * clp)667 static bool nfs_client_clear_delayed_delegations(struct nfs_client *clp)
668 {
669 	struct nfs_server *server;
670 	bool ret = false;
671 
672 	if (!test_and_clear_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state))
673 		goto out;
674 	rcu_read_lock();
675 	list_for_each_entry_rcu (server, &clp->cl_superblocks, client_link) {
676 		if (nfs_server_clear_delayed_delegations(server))
677 			ret = true;
678 	}
679 	rcu_read_unlock();
680 out:
681 	return ret;
682 }
683 
684 /**
685  * nfs_client_return_marked_delegations - return previously marked delegations
686  * @clp: nfs_client to process
687  *
688  * Note that this function is designed to be called by the state
689  * manager thread. For this reason, it cannot flush the dirty data,
690  * since that could deadlock in case of a state recovery error.
691  *
692  * Returns zero on success, or a negative errno value.
693  */
nfs_client_return_marked_delegations(struct nfs_client * clp)694 int nfs_client_return_marked_delegations(struct nfs_client *clp)
695 {
696 	int err = nfs_client_for_each_server(
697 		clp, nfs_server_return_marked_delegations, NULL);
698 	if (err)
699 		return err;
700 	/* If a return was delayed, sleep to prevent hard looping */
701 	if (nfs_client_clear_delayed_delegations(clp))
702 		ssleep(1);
703 	return 0;
704 }
705 
706 /**
707  * nfs_inode_evict_delegation - return delegation, don't reclaim opens
708  * @inode: inode to process
709  *
710  * Does not protect against delegation reclaims, therefore really only safe
711  * to be called from nfs4_clear_inode(). Guaranteed to always free
712  * the delegation structure.
713  */
nfs_inode_evict_delegation(struct inode * inode)714 void nfs_inode_evict_delegation(struct inode *inode)
715 {
716 	struct nfs_delegation *delegation;
717 
718 	delegation = nfs_inode_detach_delegation(inode);
719 	if (delegation != NULL) {
720 		set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
721 		set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
722 		nfs_do_return_delegation(inode, delegation, 1);
723 		nfs_free_delegation(delegation);
724 	}
725 }
726 
727 /**
728  * nfs4_inode_return_delegation - synchronously return a delegation
729  * @inode: inode to process
730  *
731  * This routine will always flush any dirty data to disk on the
732  * assumption that if we need to return the delegation, then
733  * we should stop caching.
734  *
735  * Returns zero on success, or a negative errno value.
736  */
nfs4_inode_return_delegation(struct inode * inode)737 int nfs4_inode_return_delegation(struct inode *inode)
738 {
739 	struct nfs_inode *nfsi = NFS_I(inode);
740 	struct nfs_delegation *delegation;
741 
742 	delegation = nfs_start_delegation_return(nfsi);
743 	if (delegation != NULL) {
744 		/* Synchronous recall of any application leases */
745 		break_lease(inode, O_WRONLY | O_RDWR);
746 		if (S_ISREG(inode->i_mode))
747 			nfs_wb_all(inode);
748 		return nfs_end_delegation_return(inode, delegation, 1);
749 	}
750 	return 0;
751 }
752 
753 /**
754  * nfs4_inode_return_delegation_on_close - asynchronously return a delegation
755  * @inode: inode to process
756  *
757  * This routine is called on file close in order to determine if the
758  * inode delegation needs to be returned immediately.
759  */
nfs4_inode_return_delegation_on_close(struct inode * inode)760 void nfs4_inode_return_delegation_on_close(struct inode *inode)
761 {
762 	struct nfs_delegation *delegation;
763 	struct nfs_delegation *ret = NULL;
764 
765 	if (!inode)
766 		return;
767 	rcu_read_lock();
768 	delegation = nfs4_get_valid_delegation(inode);
769 	if (!delegation)
770 		goto out;
771 	if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
772 	    atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
773 		spin_lock(&delegation->lock);
774 		if (delegation->inode &&
775 		    list_empty(&NFS_I(inode)->open_files) &&
776 		    !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
777 			clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
778 			/* Refcount matched in nfs_end_delegation_return() */
779 			ret = nfs_get_delegation(delegation);
780 		}
781 		spin_unlock(&delegation->lock);
782 		if (ret)
783 			nfs_clear_verifier_delegated(inode);
784 	}
785 out:
786 	rcu_read_unlock();
787 	nfs_end_delegation_return(inode, ret, 0);
788 }
789 
790 /**
791  * nfs4_inode_make_writeable
792  * @inode: pointer to inode
793  *
794  * Make the inode writeable by returning the delegation if necessary
795  *
796  * Returns zero on success, or a negative errno value.
797  */
nfs4_inode_make_writeable(struct inode * inode)798 int nfs4_inode_make_writeable(struct inode *inode)
799 {
800 	struct nfs_delegation *delegation;
801 
802 	rcu_read_lock();
803 	delegation = nfs4_get_valid_delegation(inode);
804 	if (delegation == NULL ||
805 	    (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
806 	     (delegation->type & FMODE_WRITE))) {
807 		rcu_read_unlock();
808 		return 0;
809 	}
810 	rcu_read_unlock();
811 	return nfs4_inode_return_delegation(inode);
812 }
813 
814 static void
nfs_mark_return_if_closed_delegation(struct nfs_server * server,struct nfs_delegation * delegation)815 nfs_mark_return_if_closed_delegation(struct nfs_server *server,
816 				     struct nfs_delegation *delegation)
817 {
818 	struct inode *inode;
819 
820 	if (test_bit(NFS_DELEGATION_RETURN, &delegation->flags) ||
821 	    test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags))
822 		return;
823 	spin_lock(&delegation->lock);
824 	inode = delegation->inode;
825 	if (!inode)
826 		goto out;
827 	if (list_empty(&NFS_I(inode)->open_files))
828 		nfs_mark_return_delegation(server, delegation);
829 	else
830 		set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
831 out:
832 	spin_unlock(&delegation->lock);
833 }
834 
nfs_server_mark_return_all_delegations(struct nfs_server * server)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 
nfs_client_mark_return_all_delegations(struct nfs_client * clp)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 
nfs_delegation_run_state_manager(struct nfs_client * clp)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  */
nfs_expire_all_delegations(struct nfs_client * clp)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  */
nfs_server_return_all_delegations(struct nfs_server * server)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 
nfs_mark_return_unused_delegation_types(struct nfs_server * server,fmode_t flags)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 
nfs_client_mark_return_unused_delegation_types(struct nfs_client * clp,fmode_t flags)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 
nfs_revoke_delegation(struct inode * inode,const nfs4_stateid * stateid)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 
nfs_remove_bad_delegation(struct inode * inode,const nfs4_stateid * stateid)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 
nfs_delegation_mark_returned(struct inode * inode,const nfs4_stateid * stateid)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 	clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
989 	spin_unlock(&delegation->lock);
990 	if (nfs_detach_delegation(NFS_I(inode), delegation, NFS_SERVER(inode)))
991 		nfs_put_delegation(delegation);
992 	goto out_rcu_unlock;
993 
994 out_clear_returning:
995 	clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
996 out_spin_unlock:
997 	spin_unlock(&delegation->lock);
998 out_rcu_unlock:
999 	rcu_read_unlock();
1000 
1001 	nfs_inode_find_state_and_recover(inode, stateid);
1002 }
1003 
1004 /**
1005  * nfs_expire_unused_delegation_types
1006  * @clp: client to process
1007  * @flags: delegation types to expire
1008  *
1009  */
nfs_expire_unused_delegation_types(struct nfs_client * clp,fmode_t flags)1010 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
1011 {
1012 	nfs_client_mark_return_unused_delegation_types(clp, flags);
1013 	nfs_delegation_run_state_manager(clp);
1014 }
1015 
nfs_mark_return_unreferenced_delegations(struct nfs_server * server)1016 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
1017 {
1018 	struct nfs_delegation *delegation;
1019 
1020 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1021 		if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
1022 			continue;
1023 		nfs_mark_return_if_closed_delegation(server, delegation);
1024 	}
1025 }
1026 
1027 /**
1028  * nfs_expire_unreferenced_delegations - Eliminate unused delegations
1029  * @clp: nfs_client to process
1030  *
1031  */
nfs_expire_unreferenced_delegations(struct nfs_client * clp)1032 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
1033 {
1034 	struct nfs_server *server;
1035 
1036 	rcu_read_lock();
1037 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1038 		nfs_mark_return_unreferenced_delegations(server);
1039 	rcu_read_unlock();
1040 
1041 	nfs_delegation_run_state_manager(clp);
1042 }
1043 
1044 /**
1045  * nfs_async_inode_return_delegation - asynchronously return a delegation
1046  * @inode: inode to process
1047  * @stateid: state ID information
1048  *
1049  * Returns zero on success, or a negative errno value.
1050  */
nfs_async_inode_return_delegation(struct inode * inode,const nfs4_stateid * stateid)1051 int nfs_async_inode_return_delegation(struct inode *inode,
1052 				      const nfs4_stateid *stateid)
1053 {
1054 	struct nfs_server *server = NFS_SERVER(inode);
1055 	struct nfs_client *clp = server->nfs_client;
1056 	struct nfs_delegation *delegation;
1057 
1058 	rcu_read_lock();
1059 	delegation = nfs4_get_valid_delegation(inode);
1060 	if (delegation == NULL)
1061 		goto out_enoent;
1062 	if (stateid != NULL &&
1063 	    !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
1064 		goto out_enoent;
1065 	nfs_mark_return_delegation(server, delegation);
1066 	rcu_read_unlock();
1067 
1068 	/* If there are any application leases or delegations, recall them */
1069 	break_lease(inode, O_WRONLY | O_RDWR | O_NONBLOCK);
1070 
1071 	nfs_delegation_run_state_manager(clp);
1072 	return 0;
1073 out_enoent:
1074 	rcu_read_unlock();
1075 	return -ENOENT;
1076 }
1077 
1078 static struct inode *
nfs_delegation_find_inode_server(struct nfs_server * server,const struct nfs_fh * fhandle)1079 nfs_delegation_find_inode_server(struct nfs_server *server,
1080 				 const struct nfs_fh *fhandle)
1081 {
1082 	struct nfs_delegation *delegation;
1083 	struct super_block *freeme = NULL;
1084 	struct inode *res = NULL;
1085 
1086 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1087 		spin_lock(&delegation->lock);
1088 		if (delegation->inode != NULL &&
1089 		    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1090 		    nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1091 			if (nfs_sb_active(server->super)) {
1092 				freeme = server->super;
1093 				res = igrab(delegation->inode);
1094 			}
1095 			spin_unlock(&delegation->lock);
1096 			if (res != NULL)
1097 				return res;
1098 			if (freeme) {
1099 				rcu_read_unlock();
1100 				nfs_sb_deactive(freeme);
1101 				rcu_read_lock();
1102 			}
1103 			return ERR_PTR(-EAGAIN);
1104 		}
1105 		spin_unlock(&delegation->lock);
1106 	}
1107 	return ERR_PTR(-ENOENT);
1108 }
1109 
1110 /**
1111  * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1112  * @clp: client state handle
1113  * @fhandle: filehandle from a delegation recall
1114  *
1115  * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1116  * cannot be found.
1117  */
nfs_delegation_find_inode(struct nfs_client * clp,const struct nfs_fh * fhandle)1118 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1119 					const struct nfs_fh *fhandle)
1120 {
1121 	struct nfs_server *server;
1122 	struct inode *res;
1123 
1124 	rcu_read_lock();
1125 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1126 		res = nfs_delegation_find_inode_server(server, fhandle);
1127 		if (res != ERR_PTR(-ENOENT)) {
1128 			rcu_read_unlock();
1129 			return res;
1130 		}
1131 	}
1132 	rcu_read_unlock();
1133 	return ERR_PTR(-ENOENT);
1134 }
1135 
nfs_delegation_mark_reclaim_server(struct nfs_server * server)1136 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1137 {
1138 	struct nfs_delegation *delegation;
1139 
1140 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1141 		/*
1142 		 * If the delegation may have been admin revoked, then we
1143 		 * cannot reclaim it.
1144 		 */
1145 		if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1146 			continue;
1147 		set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1148 	}
1149 }
1150 
1151 /**
1152  * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1153  * @clp: nfs_client to process
1154  *
1155  */
nfs_delegation_mark_reclaim(struct nfs_client * clp)1156 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1157 {
1158 	struct nfs_server *server;
1159 
1160 	rcu_read_lock();
1161 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1162 		nfs_delegation_mark_reclaim_server(server);
1163 	rcu_read_unlock();
1164 }
1165 
nfs_server_reap_unclaimed_delegations(struct nfs_server * server,void __always_unused * data)1166 static int nfs_server_reap_unclaimed_delegations(struct nfs_server *server,
1167 		void __always_unused *data)
1168 {
1169 	struct nfs_delegation *delegation;
1170 	struct inode *inode;
1171 restart:
1172 	rcu_read_lock();
1173 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1174 		if (test_bit(NFS_DELEGATION_INODE_FREEING,
1175 					&delegation->flags) ||
1176 		    test_bit(NFS_DELEGATION_RETURNING,
1177 					&delegation->flags) ||
1178 		    test_bit(NFS_DELEGATION_NEED_RECLAIM,
1179 					&delegation->flags) == 0)
1180 			continue;
1181 		inode = nfs_delegation_grab_inode(delegation);
1182 		if (inode == NULL)
1183 			continue;
1184 		delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1185 		rcu_read_unlock();
1186 		if (delegation != NULL) {
1187 			if (nfs_detach_delegation(NFS_I(inode), delegation,
1188 						server) != NULL)
1189 				nfs_free_delegation(delegation);
1190 			/* Match nfs_start_delegation_return_locked */
1191 			nfs_put_delegation(delegation);
1192 		}
1193 		iput(inode);
1194 		cond_resched();
1195 		goto restart;
1196 	}
1197 	rcu_read_unlock();
1198 	return 0;
1199 }
1200 
1201 /**
1202  * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1203  * @clp: nfs_client to process
1204  *
1205  */
nfs_delegation_reap_unclaimed(struct nfs_client * clp)1206 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1207 {
1208 	nfs_client_for_each_server(clp, nfs_server_reap_unclaimed_delegations,
1209 			NULL);
1210 }
1211 
nfs4_server_rebooted(const struct nfs_client * clp)1212 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1213 {
1214 	return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1215 				BIT(NFS4CLNT_LEASE_EXPIRED) |
1216 				BIT(NFS4CLNT_SESSION_RESET))) != 0;
1217 }
1218 
nfs_mark_test_expired_delegation(struct nfs_server * server,struct nfs_delegation * delegation)1219 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1220 	    struct nfs_delegation *delegation)
1221 {
1222 	if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1223 		return;
1224 	clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1225 	set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1226 	set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1227 }
1228 
nfs_inode_mark_test_expired_delegation(struct nfs_server * server,struct inode * inode)1229 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1230 		struct inode *inode)
1231 {
1232 	struct nfs_delegation *delegation;
1233 
1234 	rcu_read_lock();
1235 	delegation = rcu_dereference(NFS_I(inode)->delegation);
1236 	if (delegation)
1237 		nfs_mark_test_expired_delegation(server, delegation);
1238 	rcu_read_unlock();
1239 
1240 }
1241 
nfs_delegation_mark_test_expired_server(struct nfs_server * server)1242 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1243 {
1244 	struct nfs_delegation *delegation;
1245 
1246 	list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1247 		nfs_mark_test_expired_delegation(server, delegation);
1248 }
1249 
1250 /**
1251  * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1252  * @clp: nfs_client to process
1253  *
1254  * Iterates through all the delegations associated with this server and
1255  * marks them as needing to be checked for validity.
1256  */
nfs_mark_test_expired_all_delegations(struct nfs_client * clp)1257 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1258 {
1259 	struct nfs_server *server;
1260 
1261 	rcu_read_lock();
1262 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1263 		nfs_delegation_mark_test_expired_server(server);
1264 	rcu_read_unlock();
1265 }
1266 
1267 /**
1268  * nfs_test_expired_all_delegations - test all delegations for a client
1269  * @clp: nfs_client to process
1270  *
1271  * Helper for handling "recallable state revoked" status from server.
1272  */
nfs_test_expired_all_delegations(struct nfs_client * clp)1273 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1274 {
1275 	nfs_mark_test_expired_all_delegations(clp);
1276 	nfs4_schedule_state_manager(clp);
1277 }
1278 
1279 static void
nfs_delegation_test_free_expired(struct inode * inode,nfs4_stateid * stateid,const struct cred * cred)1280 nfs_delegation_test_free_expired(struct inode *inode,
1281 		nfs4_stateid *stateid,
1282 		const struct cred *cred)
1283 {
1284 	struct nfs_server *server = NFS_SERVER(inode);
1285 	const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1286 	int status;
1287 
1288 	if (!cred)
1289 		return;
1290 	status = ops->test_and_free_expired(server, stateid, cred);
1291 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1292 		nfs_remove_bad_delegation(inode, stateid);
1293 }
1294 
nfs_server_reap_expired_delegations(struct nfs_server * server,void __always_unused * data)1295 static int nfs_server_reap_expired_delegations(struct nfs_server *server,
1296 		void __always_unused *data)
1297 {
1298 	struct nfs_delegation *delegation;
1299 	struct inode *inode;
1300 	const struct cred *cred;
1301 	nfs4_stateid stateid;
1302 restart:
1303 	rcu_read_lock();
1304 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1305 		if (test_bit(NFS_DELEGATION_INODE_FREEING,
1306 					&delegation->flags) ||
1307 		    test_bit(NFS_DELEGATION_RETURNING,
1308 					&delegation->flags) ||
1309 		    test_bit(NFS_DELEGATION_TEST_EXPIRED,
1310 					&delegation->flags) == 0)
1311 			continue;
1312 		inode = nfs_delegation_grab_inode(delegation);
1313 		if (inode == NULL)
1314 			continue;
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 		clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1320 		rcu_read_unlock();
1321 		nfs_delegation_test_free_expired(inode, &stateid, cred);
1322 		put_cred(cred);
1323 		if (!nfs4_server_rebooted(server->nfs_client)) {
1324 			iput(inode);
1325 			cond_resched();
1326 			goto restart;
1327 		}
1328 		nfs_inode_mark_test_expired_delegation(server,inode);
1329 		iput(inode);
1330 		return -EAGAIN;
1331 	}
1332 	rcu_read_unlock();
1333 	return 0;
1334 }
1335 
1336 /**
1337  * nfs_reap_expired_delegations - reap expired delegations
1338  * @clp: nfs_client to process
1339  *
1340  * Iterates through all the delegations associated with this server and
1341  * checks if they have may have been revoked. This function is usually
1342  * expected to be called in cases where the server may have lost its
1343  * lease.
1344  */
nfs_reap_expired_delegations(struct nfs_client * clp)1345 void nfs_reap_expired_delegations(struct nfs_client *clp)
1346 {
1347 	nfs_client_for_each_server(clp, nfs_server_reap_expired_delegations,
1348 			NULL);
1349 }
1350 
nfs_inode_find_delegation_state_and_recover(struct inode * inode,const nfs4_stateid * stateid)1351 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1352 		const nfs4_stateid *stateid)
1353 {
1354 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1355 	struct nfs_delegation *delegation;
1356 	bool found = false;
1357 
1358 	rcu_read_lock();
1359 	delegation = rcu_dereference(NFS_I(inode)->delegation);
1360 	if (delegation &&
1361 	    nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1362 	    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1363 		nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1364 		found = true;
1365 	}
1366 	rcu_read_unlock();
1367 	if (found)
1368 		nfs4_schedule_state_manager(clp);
1369 }
1370 
1371 /**
1372  * nfs_delegations_present - check for existence of delegations
1373  * @clp: client state handle
1374  *
1375  * Returns one if there are any nfs_delegation structures attached
1376  * to this nfs_client.
1377  */
nfs_delegations_present(struct nfs_client * clp)1378 int nfs_delegations_present(struct nfs_client *clp)
1379 {
1380 	struct nfs_server *server;
1381 	int ret = 0;
1382 
1383 	rcu_read_lock();
1384 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1385 		if (!list_empty(&server->delegations)) {
1386 			ret = 1;
1387 			break;
1388 		}
1389 	rcu_read_unlock();
1390 	return ret;
1391 }
1392 
1393 /**
1394  * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1395  * @dst: stateid to refresh
1396  * @inode: inode to check
1397  *
1398  * Returns "true" and updates "dst->seqid" * if inode had a delegation
1399  * that matches our delegation stateid. Otherwise "false" is returned.
1400  */
nfs4_refresh_delegation_stateid(nfs4_stateid * dst,struct inode * inode)1401 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1402 {
1403 	struct nfs_delegation *delegation;
1404 	bool ret = false;
1405 	if (!inode)
1406 		goto out;
1407 
1408 	rcu_read_lock();
1409 	delegation = rcu_dereference(NFS_I(inode)->delegation);
1410 	if (delegation != NULL &&
1411 	    nfs4_stateid_match_other(dst, &delegation->stateid) &&
1412 	    nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1413 	    !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1414 		dst->seqid = delegation->stateid.seqid;
1415 		ret = true;
1416 	}
1417 	rcu_read_unlock();
1418 out:
1419 	return ret;
1420 }
1421 
1422 /**
1423  * nfs4_copy_delegation_stateid - Copy inode's state ID information
1424  * @inode: inode to check
1425  * @flags: delegation type requirement
1426  * @dst: stateid data structure to fill in
1427  * @cred: optional argument to retrieve credential
1428  *
1429  * Returns "true" and fills in "dst->data" * if inode had a delegation,
1430  * otherwise "false" is returned.
1431  */
nfs4_copy_delegation_stateid(struct inode * inode,fmode_t flags,nfs4_stateid * dst,const struct cred ** cred)1432 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1433 		nfs4_stateid *dst, const struct cred **cred)
1434 {
1435 	struct nfs_inode *nfsi = NFS_I(inode);
1436 	struct nfs_delegation *delegation;
1437 	bool ret = false;
1438 
1439 	flags &= FMODE_READ|FMODE_WRITE;
1440 	rcu_read_lock();
1441 	delegation = rcu_dereference(nfsi->delegation);
1442 	if (!delegation)
1443 		goto out;
1444 	spin_lock(&delegation->lock);
1445 	ret = nfs4_is_valid_delegation(delegation, flags);
1446 	if (ret) {
1447 		nfs4_stateid_copy(dst, &delegation->stateid);
1448 		nfs_mark_delegation_referenced(delegation);
1449 		if (cred)
1450 			*cred = get_cred(delegation->cred);
1451 	}
1452 	spin_unlock(&delegation->lock);
1453 out:
1454 	rcu_read_unlock();
1455 	return ret;
1456 }
1457 
1458 /**
1459  * nfs4_delegation_flush_on_close - Check if we must flush file on close
1460  * @inode: inode to check
1461  *
1462  * This function checks the number of outstanding writes to the file
1463  * against the delegation 'space_limit' field to see if
1464  * the spec requires us to flush the file on close.
1465  */
nfs4_delegation_flush_on_close(const struct inode * inode)1466 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1467 {
1468 	struct nfs_inode *nfsi = NFS_I(inode);
1469 	struct nfs_delegation *delegation;
1470 	bool ret = true;
1471 
1472 	rcu_read_lock();
1473 	delegation = rcu_dereference(nfsi->delegation);
1474 	if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1475 		goto out;
1476 	if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1477 		ret = false;
1478 out:
1479 	rcu_read_unlock();
1480 	return ret;
1481 }
1482 
1483 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);
1484