1 /*
2  * linux/fs/nfs/delegation.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFS file delegation management
7  *
8  */
9 #include <linux/completion.h>
10 #include <linux/kthread.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 
16 #include <linux/nfs4.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_xdr.h>
19 
20 #include "nfs4_fs.h"
21 #include "delegation.h"
22 #include "internal.h"
23 
nfs_do_free_delegation(struct nfs_delegation * delegation)24 static void nfs_do_free_delegation(struct nfs_delegation *delegation)
25 {
26 	kfree(delegation);
27 }
28 
nfs_free_delegation_callback(struct rcu_head * head)29 static void nfs_free_delegation_callback(struct rcu_head *head)
30 {
31 	struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
32 
33 	nfs_do_free_delegation(delegation);
34 }
35 
nfs_free_delegation(struct nfs_delegation * delegation)36 static void nfs_free_delegation(struct nfs_delegation *delegation)
37 {
38 	if (delegation->cred) {
39 		put_rpccred(delegation->cred);
40 		delegation->cred = NULL;
41 	}
42 	call_rcu(&delegation->rcu, nfs_free_delegation_callback);
43 }
44 
45 /**
46  * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
47  * @delegation: delegation to process
48  *
49  */
nfs_mark_delegation_referenced(struct nfs_delegation * delegation)50 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
51 {
52 	set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
53 }
54 
55 /**
56  * nfs_have_delegation - check if inode has a delegation
57  * @inode: inode to check
58  * @flags: delegation types to check for
59  *
60  * Returns one if inode has the indicated delegation, otherwise zero.
61  */
nfs_have_delegation(struct inode * inode,fmode_t flags)62 int nfs_have_delegation(struct inode *inode, fmode_t flags)
63 {
64 	struct nfs_delegation *delegation;
65 	int ret = 0;
66 
67 	flags &= FMODE_READ|FMODE_WRITE;
68 	rcu_read_lock();
69 	delegation = rcu_dereference(NFS_I(inode)->delegation);
70 	if (delegation != NULL && (delegation->type & flags) == flags) {
71 		nfs_mark_delegation_referenced(delegation);
72 		ret = 1;
73 	}
74 	rcu_read_unlock();
75 	return ret;
76 }
77 
nfs_delegation_claim_locks(struct nfs_open_context * ctx,struct nfs4_state * state)78 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
79 {
80 	struct inode *inode = state->inode;
81 	struct file_lock *fl;
82 	int status = 0;
83 
84 	if (inode->i_flock == NULL)
85 		goto out;
86 
87 	/* Protect inode->i_flock using the file locks lock */
88 	lock_flocks();
89 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
90 		if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
91 			continue;
92 		if (nfs_file_open_context(fl->fl_file) != ctx)
93 			continue;
94 		unlock_flocks();
95 		status = nfs4_lock_delegation_recall(state, fl);
96 		if (status < 0)
97 			goto out;
98 		lock_flocks();
99 	}
100 	unlock_flocks();
101 out:
102 	return status;
103 }
104 
nfs_delegation_claim_opens(struct inode * inode,const nfs4_stateid * stateid)105 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
106 {
107 	struct nfs_inode *nfsi = NFS_I(inode);
108 	struct nfs_open_context *ctx;
109 	struct nfs4_state *state;
110 	int err;
111 
112 again:
113 	spin_lock(&inode->i_lock);
114 	list_for_each_entry(ctx, &nfsi->open_files, list) {
115 		state = ctx->state;
116 		if (state == NULL)
117 			continue;
118 		if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
119 			continue;
120 		if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
121 			continue;
122 		get_nfs_open_context(ctx);
123 		spin_unlock(&inode->i_lock);
124 		err = nfs4_open_delegation_recall(ctx, state, stateid);
125 		if (err >= 0)
126 			err = nfs_delegation_claim_locks(ctx, state);
127 		put_nfs_open_context(ctx);
128 		if (err != 0)
129 			return err;
130 		goto again;
131 	}
132 	spin_unlock(&inode->i_lock);
133 	return 0;
134 }
135 
136 /**
137  * nfs_inode_reclaim_delegation - process a delegation reclaim request
138  * @inode: inode to process
139  * @cred: credential to use for request
140  * @res: new delegation state from server
141  *
142  */
nfs_inode_reclaim_delegation(struct inode * inode,struct rpc_cred * cred,struct nfs_openres * res)143 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
144 				  struct nfs_openres *res)
145 {
146 	struct nfs_delegation *delegation;
147 	struct rpc_cred *oldcred = NULL;
148 
149 	rcu_read_lock();
150 	delegation = rcu_dereference(NFS_I(inode)->delegation);
151 	if (delegation != NULL) {
152 		spin_lock(&delegation->lock);
153 		if (delegation->inode != NULL) {
154 			memcpy(delegation->stateid.data, res->delegation.data,
155 			       sizeof(delegation->stateid.data));
156 			delegation->type = res->delegation_type;
157 			delegation->maxsize = res->maxsize;
158 			oldcred = delegation->cred;
159 			delegation->cred = get_rpccred(cred);
160 			clear_bit(NFS_DELEGATION_NEED_RECLAIM,
161 				  &delegation->flags);
162 			NFS_I(inode)->delegation_state = delegation->type;
163 			spin_unlock(&delegation->lock);
164 			put_rpccred(oldcred);
165 			rcu_read_unlock();
166 		} else {
167 			/* We appear to have raced with a delegation return. */
168 			spin_unlock(&delegation->lock);
169 			rcu_read_unlock();
170 			nfs_inode_set_delegation(inode, cred, res);
171 		}
172 	} else {
173 		rcu_read_unlock();
174 	}
175 }
176 
nfs_do_return_delegation(struct inode * inode,struct nfs_delegation * delegation,int issync)177 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
178 {
179 	int res = 0;
180 
181 	res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
182 	nfs_free_delegation(delegation);
183 	return res;
184 }
185 
nfs_delegation_grab_inode(struct nfs_delegation * delegation)186 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
187 {
188 	struct inode *inode = NULL;
189 
190 	spin_lock(&delegation->lock);
191 	if (delegation->inode != NULL)
192 		inode = igrab(delegation->inode);
193 	spin_unlock(&delegation->lock);
194 	return inode;
195 }
196 
197 static struct nfs_delegation *
nfs_detach_delegation_locked(struct nfs_inode * nfsi,struct nfs_server * server)198 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
199 			     struct nfs_server *server)
200 {
201 	struct nfs_delegation *delegation =
202 		rcu_dereference_protected(nfsi->delegation,
203 				lockdep_is_held(&server->nfs_client->cl_lock));
204 
205 	if (delegation == NULL)
206 		goto nomatch;
207 
208 	spin_lock(&delegation->lock);
209 	list_del_rcu(&delegation->super_list);
210 	delegation->inode = NULL;
211 	nfsi->delegation_state = 0;
212 	rcu_assign_pointer(nfsi->delegation, NULL);
213 	spin_unlock(&delegation->lock);
214 	return delegation;
215 nomatch:
216 	return NULL;
217 }
218 
nfs_detach_delegation(struct nfs_inode * nfsi,struct nfs_server * server)219 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
220 						    struct nfs_server *server)
221 {
222 	struct nfs_client *clp = server->nfs_client;
223 	struct nfs_delegation *delegation;
224 
225 	spin_lock(&clp->cl_lock);
226 	delegation = nfs_detach_delegation_locked(nfsi, server);
227 	spin_unlock(&clp->cl_lock);
228 	return delegation;
229 }
230 
231 /**
232  * nfs_inode_set_delegation - set up a delegation on an inode
233  * @inode: inode to which delegation applies
234  * @cred: cred to use for subsequent delegation processing
235  * @res: new delegation state from server
236  *
237  * Returns zero on success, or a negative errno value.
238  */
nfs_inode_set_delegation(struct inode * inode,struct rpc_cred * cred,struct nfs_openres * res)239 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
240 {
241 	struct nfs_server *server = NFS_SERVER(inode);
242 	struct nfs_client *clp = server->nfs_client;
243 	struct nfs_inode *nfsi = NFS_I(inode);
244 	struct nfs_delegation *delegation, *old_delegation;
245 	struct nfs_delegation *freeme = NULL;
246 	int status = 0;
247 
248 	delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
249 	if (delegation == NULL)
250 		return -ENOMEM;
251 	memcpy(delegation->stateid.data, res->delegation.data,
252 			sizeof(delegation->stateid.data));
253 	delegation->type = res->delegation_type;
254 	delegation->maxsize = res->maxsize;
255 	delegation->change_attr = nfsi->change_attr;
256 	delegation->cred = get_rpccred(cred);
257 	delegation->inode = inode;
258 	delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
259 	spin_lock_init(&delegation->lock);
260 
261 	spin_lock(&clp->cl_lock);
262 	old_delegation = rcu_dereference_protected(nfsi->delegation,
263 					lockdep_is_held(&clp->cl_lock));
264 	if (old_delegation != NULL) {
265 		if (memcmp(&delegation->stateid, &old_delegation->stateid,
266 					sizeof(old_delegation->stateid)) == 0 &&
267 				delegation->type == old_delegation->type) {
268 			goto out;
269 		}
270 		/*
271 		 * Deal with broken servers that hand out two
272 		 * delegations for the same file.
273 		 */
274 		dfprintk(FILE, "%s: server %s handed out "
275 				"a duplicate delegation!\n",
276 				__func__, clp->cl_hostname);
277 		if (delegation->type <= old_delegation->type) {
278 			freeme = delegation;
279 			delegation = NULL;
280 			goto out;
281 		}
282 		freeme = nfs_detach_delegation_locked(nfsi, server);
283 	}
284 	list_add_rcu(&delegation->super_list, &server->delegations);
285 	nfsi->delegation_state = delegation->type;
286 	rcu_assign_pointer(nfsi->delegation, delegation);
287 	delegation = NULL;
288 
289 	/* Ensure we revalidate the attributes and page cache! */
290 	spin_lock(&inode->i_lock);
291 	nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
292 	spin_unlock(&inode->i_lock);
293 
294 out:
295 	spin_unlock(&clp->cl_lock);
296 	if (delegation != NULL)
297 		nfs_free_delegation(delegation);
298 	if (freeme != NULL)
299 		nfs_do_return_delegation(inode, freeme, 0);
300 	return status;
301 }
302 
303 /*
304  * Basic procedure for returning a delegation to the server
305  */
__nfs_inode_return_delegation(struct inode * inode,struct nfs_delegation * delegation,int issync)306 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
307 {
308 	struct nfs_inode *nfsi = NFS_I(inode);
309 	int err;
310 
311 	/*
312 	 * Guard against new delegated open/lock/unlock calls and against
313 	 * state recovery
314 	 */
315 	down_write(&nfsi->rwsem);
316 	err = nfs_delegation_claim_opens(inode, &delegation->stateid);
317 	up_write(&nfsi->rwsem);
318 	if (err)
319 		goto out;
320 
321 	err = nfs_do_return_delegation(inode, delegation, issync);
322 out:
323 	return err;
324 }
325 
326 /**
327  * nfs_client_return_marked_delegations - return previously marked delegations
328  * @clp: nfs_client to process
329  *
330  * Returns zero on success, or a negative errno value.
331  */
nfs_client_return_marked_delegations(struct nfs_client * clp)332 int nfs_client_return_marked_delegations(struct nfs_client *clp)
333 {
334 	struct nfs_delegation *delegation;
335 	struct nfs_server *server;
336 	struct inode *inode;
337 	int err = 0;
338 
339 restart:
340 	rcu_read_lock();
341 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
342 		list_for_each_entry_rcu(delegation, &server->delegations,
343 								super_list) {
344 			if (!test_and_clear_bit(NFS_DELEGATION_RETURN,
345 							&delegation->flags))
346 				continue;
347 			inode = nfs_delegation_grab_inode(delegation);
348 			if (inode == NULL)
349 				continue;
350 			delegation = nfs_detach_delegation(NFS_I(inode),
351 								server);
352 			rcu_read_unlock();
353 
354 			if (delegation != NULL) {
355 				filemap_flush(inode->i_mapping);
356 				err = __nfs_inode_return_delegation(inode,
357 								delegation, 0);
358 			}
359 			iput(inode);
360 			if (!err)
361 				goto restart;
362 			set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
363 			return err;
364 		}
365 	}
366 	rcu_read_unlock();
367 	return 0;
368 }
369 
370 /**
371  * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
372  * @inode: inode to process
373  *
374  * Does not protect against delegation reclaims, therefore really only safe
375  * to be called from nfs4_clear_inode().
376  */
nfs_inode_return_delegation_noreclaim(struct inode * inode)377 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
378 {
379 	struct nfs_server *server = NFS_SERVER(inode);
380 	struct nfs_inode *nfsi = NFS_I(inode);
381 	struct nfs_delegation *delegation;
382 
383 	if (rcu_access_pointer(nfsi->delegation) != NULL) {
384 		delegation = nfs_detach_delegation(nfsi, server);
385 		if (delegation != NULL)
386 			nfs_do_return_delegation(inode, delegation, 0);
387 	}
388 }
389 
390 /**
391  * nfs_inode_return_delegation - synchronously return a delegation
392  * @inode: inode to process
393  *
394  * Returns zero on success, or a negative errno value.
395  */
nfs_inode_return_delegation(struct inode * inode)396 int nfs_inode_return_delegation(struct inode *inode)
397 {
398 	struct nfs_server *server = NFS_SERVER(inode);
399 	struct nfs_inode *nfsi = NFS_I(inode);
400 	struct nfs_delegation *delegation;
401 	int err = 0;
402 
403 	if (rcu_access_pointer(nfsi->delegation) != NULL) {
404 		delegation = nfs_detach_delegation(nfsi, server);
405 		if (delegation != NULL) {
406 			nfs_wb_all(inode);
407 			err = __nfs_inode_return_delegation(inode, delegation, 1);
408 		}
409 	}
410 	return err;
411 }
412 
nfs_mark_return_delegation(struct nfs_delegation * delegation)413 static void nfs_mark_return_delegation(struct nfs_delegation *delegation)
414 {
415 	struct nfs_client *clp = NFS_SERVER(delegation->inode)->nfs_client;
416 
417 	set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
418 	set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
419 }
420 
421 /**
422  * nfs_super_return_all_delegations - return delegations for one superblock
423  * @sb: sb to process
424  *
425  */
nfs_super_return_all_delegations(struct super_block * sb)426 void nfs_super_return_all_delegations(struct super_block *sb)
427 {
428 	struct nfs_server *server = NFS_SB(sb);
429 	struct nfs_client *clp = server->nfs_client;
430 	struct nfs_delegation *delegation;
431 
432 	if (clp == NULL)
433 		return;
434 
435 	rcu_read_lock();
436 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
437 		spin_lock(&delegation->lock);
438 		set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
439 		spin_unlock(&delegation->lock);
440 	}
441 	rcu_read_unlock();
442 
443 	if (nfs_client_return_marked_delegations(clp) != 0)
444 		nfs4_schedule_state_manager(clp);
445 }
446 
nfs_mark_return_all_delegation_types(struct nfs_server * server,fmode_t flags)447 static void nfs_mark_return_all_delegation_types(struct nfs_server *server,
448 						 fmode_t flags)
449 {
450 	struct nfs_delegation *delegation;
451 
452 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
453 		if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
454 			continue;
455 		if (delegation->type & flags)
456 			nfs_mark_return_delegation(delegation);
457 	}
458 }
459 
nfs_client_mark_return_all_delegation_types(struct nfs_client * clp,fmode_t flags)460 static void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp,
461 							fmode_t flags)
462 {
463 	struct nfs_server *server;
464 
465 	rcu_read_lock();
466 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
467 		nfs_mark_return_all_delegation_types(server, flags);
468 	rcu_read_unlock();
469 }
470 
nfs_client_mark_return_all_delegations(struct nfs_client * clp)471 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
472 {
473 	nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
474 }
475 
nfs_delegation_run_state_manager(struct nfs_client * clp)476 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
477 {
478 	if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
479 		nfs4_schedule_state_manager(clp);
480 }
481 
482 /**
483  * nfs_expire_all_delegation_types
484  * @clp: client to process
485  * @flags: delegation types to expire
486  *
487  */
nfs_expire_all_delegation_types(struct nfs_client * clp,fmode_t flags)488 void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
489 {
490 	nfs_client_mark_return_all_delegation_types(clp, flags);
491 	nfs_delegation_run_state_manager(clp);
492 }
493 
494 /**
495  * nfs_expire_all_delegations
496  * @clp: client to process
497  *
498  */
nfs_expire_all_delegations(struct nfs_client * clp)499 void nfs_expire_all_delegations(struct nfs_client *clp)
500 {
501 	nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
502 }
503 
504 /**
505  * nfs_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
506  * @clp: client to process
507  *
508  */
nfs_handle_cb_pathdown(struct nfs_client * clp)509 void nfs_handle_cb_pathdown(struct nfs_client *clp)
510 {
511 	if (clp == NULL)
512 		return;
513 	nfs_client_mark_return_all_delegations(clp);
514 }
515 
nfs_mark_return_unreferenced_delegations(struct nfs_server * server)516 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
517 {
518 	struct nfs_delegation *delegation;
519 
520 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
521 		if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
522 			continue;
523 		nfs_mark_return_delegation(delegation);
524 	}
525 }
526 
527 /**
528  * nfs_expire_unreferenced_delegations - Eliminate unused delegations
529  * @clp: nfs_client to process
530  *
531  */
nfs_expire_unreferenced_delegations(struct nfs_client * clp)532 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
533 {
534 	struct nfs_server *server;
535 
536 	rcu_read_lock();
537 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
538 		nfs_mark_return_unreferenced_delegations(server);
539 	rcu_read_unlock();
540 
541 	nfs_delegation_run_state_manager(clp);
542 }
543 
544 /**
545  * nfs_async_inode_return_delegation - asynchronously return a delegation
546  * @inode: inode to process
547  * @stateid: state ID information from CB_RECALL arguments
548  *
549  * Returns zero on success, or a negative errno value.
550  */
nfs_async_inode_return_delegation(struct inode * inode,const nfs4_stateid * stateid)551 int nfs_async_inode_return_delegation(struct inode *inode,
552 				      const nfs4_stateid *stateid)
553 {
554 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
555 	struct nfs_delegation *delegation;
556 
557 	rcu_read_lock();
558 	delegation = rcu_dereference(NFS_I(inode)->delegation);
559 
560 	if (!clp->cl_mvops->validate_stateid(delegation, stateid)) {
561 		rcu_read_unlock();
562 		return -ENOENT;
563 	}
564 	nfs_mark_return_delegation(delegation);
565 	rcu_read_unlock();
566 
567 	nfs_delegation_run_state_manager(clp);
568 	return 0;
569 }
570 
571 static struct inode *
nfs_delegation_find_inode_server(struct nfs_server * server,const struct nfs_fh * fhandle)572 nfs_delegation_find_inode_server(struct nfs_server *server,
573 				 const struct nfs_fh *fhandle)
574 {
575 	struct nfs_delegation *delegation;
576 	struct inode *res = NULL;
577 
578 	list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
579 		spin_lock(&delegation->lock);
580 		if (delegation->inode != NULL &&
581 		    nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
582 			res = igrab(delegation->inode);
583 		}
584 		spin_unlock(&delegation->lock);
585 		if (res != NULL)
586 			break;
587 	}
588 	return res;
589 }
590 
591 /**
592  * nfs_delegation_find_inode - retrieve the inode associated with a delegation
593  * @clp: client state handle
594  * @fhandle: filehandle from a delegation recall
595  *
596  * Returns pointer to inode matching "fhandle," or NULL if a matching inode
597  * cannot be found.
598  */
nfs_delegation_find_inode(struct nfs_client * clp,const struct nfs_fh * fhandle)599 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
600 					const struct nfs_fh *fhandle)
601 {
602 	struct nfs_server *server;
603 	struct inode *res = NULL;
604 
605 	rcu_read_lock();
606 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
607 		res = nfs_delegation_find_inode_server(server, fhandle);
608 		if (res != NULL)
609 			break;
610 	}
611 	rcu_read_unlock();
612 	return res;
613 }
614 
nfs_delegation_mark_reclaim_server(struct nfs_server * server)615 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
616 {
617 	struct nfs_delegation *delegation;
618 
619 	list_for_each_entry_rcu(delegation, &server->delegations, super_list)
620 		set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
621 }
622 
623 /**
624  * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
625  * @clp: nfs_client to process
626  *
627  */
nfs_delegation_mark_reclaim(struct nfs_client * clp)628 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
629 {
630 	struct nfs_server *server;
631 
632 	rcu_read_lock();
633 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
634 		nfs_delegation_mark_reclaim_server(server);
635 	rcu_read_unlock();
636 }
637 
638 /**
639  * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
640  * @clp: nfs_client to process
641  *
642  */
nfs_delegation_reap_unclaimed(struct nfs_client * clp)643 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
644 {
645 	struct nfs_delegation *delegation;
646 	struct nfs_server *server;
647 	struct inode *inode;
648 
649 restart:
650 	rcu_read_lock();
651 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
652 		list_for_each_entry_rcu(delegation, &server->delegations,
653 								super_list) {
654 			if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
655 						&delegation->flags) == 0)
656 				continue;
657 			inode = nfs_delegation_grab_inode(delegation);
658 			if (inode == NULL)
659 				continue;
660 			delegation = nfs_detach_delegation(NFS_I(inode),
661 								server);
662 			rcu_read_unlock();
663 
664 			if (delegation != NULL)
665 				nfs_free_delegation(delegation);
666 			iput(inode);
667 			goto restart;
668 		}
669 	}
670 	rcu_read_unlock();
671 }
672 
673 /**
674  * nfs_delegations_present - check for existence of delegations
675  * @clp: client state handle
676  *
677  * Returns one if there are any nfs_delegation structures attached
678  * to this nfs_client.
679  */
nfs_delegations_present(struct nfs_client * clp)680 int nfs_delegations_present(struct nfs_client *clp)
681 {
682 	struct nfs_server *server;
683 	int ret = 0;
684 
685 	rcu_read_lock();
686 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
687 		if (!list_empty(&server->delegations)) {
688 			ret = 1;
689 			break;
690 		}
691 	rcu_read_unlock();
692 	return ret;
693 }
694 
695 /**
696  * nfs4_copy_delegation_stateid - Copy inode's state ID information
697  * @dst: stateid data structure to fill in
698  * @inode: inode to check
699  *
700  * Returns one and fills in "dst->data" * if inode had a delegation,
701  * otherwise zero is returned.
702  */
nfs4_copy_delegation_stateid(nfs4_stateid * dst,struct inode * inode)703 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
704 {
705 	struct nfs_inode *nfsi = NFS_I(inode);
706 	struct nfs_delegation *delegation;
707 	int ret = 0;
708 
709 	rcu_read_lock();
710 	delegation = rcu_dereference(nfsi->delegation);
711 	if (delegation != NULL) {
712 		memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
713 		ret = 1;
714 	}
715 	rcu_read_unlock();
716 	return ret;
717 }
718