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