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