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