Linux 3.12.39
[linux/fpc-iii.git] / fs / nfs / delegation.c
blob2ea3537b8bdee61421f1a3f3606760ffd7fed199
1 /*
2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
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>
16 #include <linux/nfs4.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_xdr.h>
20 #include "nfs4_fs.h"
21 #include "delegation.h"
22 #include "internal.h"
23 #include "nfs4trace.h"
25 static void nfs_free_delegation(struct nfs_delegation *delegation)
27 if (delegation->cred) {
28 put_rpccred(delegation->cred);
29 delegation->cred = NULL;
31 kfree_rcu(delegation, rcu);
34 /**
35 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
36 * @delegation: delegation to process
39 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
41 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
44 /**
45 * nfs_have_delegation - check if inode has a delegation
46 * @inode: inode to check
47 * @flags: delegation types to check for
49 * Returns one if inode has the indicated delegation, otherwise zero.
51 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
53 struct nfs_delegation *delegation;
54 int ret = 0;
56 flags &= FMODE_READ|FMODE_WRITE;
57 rcu_read_lock();
58 delegation = rcu_dereference(NFS_I(inode)->delegation);
59 if (delegation != NULL && (delegation->type & flags) == flags &&
60 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
61 nfs_mark_delegation_referenced(delegation);
62 ret = 1;
64 rcu_read_unlock();
65 return ret;
68 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
70 struct inode *inode = state->inode;
71 struct file_lock *fl;
72 int status = 0;
74 if (inode->i_flock == NULL)
75 goto out;
77 /* Protect inode->i_flock using the i_lock */
78 spin_lock(&inode->i_lock);
79 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
80 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
81 continue;
82 if (nfs_file_open_context(fl->fl_file) != ctx)
83 continue;
84 spin_unlock(&inode->i_lock);
85 status = nfs4_lock_delegation_recall(fl, state, stateid);
86 if (status < 0)
87 goto out;
88 spin_lock(&inode->i_lock);
90 spin_unlock(&inode->i_lock);
91 out:
92 return status;
95 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
97 struct nfs_inode *nfsi = NFS_I(inode);
98 struct nfs_open_context *ctx;
99 struct nfs4_state_owner *sp;
100 struct nfs4_state *state;
101 unsigned int seq;
102 int err;
104 again:
105 spin_lock(&inode->i_lock);
106 list_for_each_entry(ctx, &nfsi->open_files, list) {
107 state = ctx->state;
108 if (state == NULL)
109 continue;
110 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
111 continue;
112 if (!nfs4_valid_open_stateid(state))
113 continue;
114 if (!nfs4_stateid_match(&state->stateid, stateid))
115 continue;
116 get_nfs_open_context(ctx);
117 spin_unlock(&inode->i_lock);
118 sp = state->owner;
119 /* Block nfs4_proc_unlck */
120 mutex_lock(&sp->so_delegreturn_mutex);
121 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
122 err = nfs4_open_delegation_recall(ctx, state, stateid);
123 if (!err)
124 err = nfs_delegation_claim_locks(ctx, state, stateid);
125 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
126 err = -EAGAIN;
127 mutex_unlock(&sp->so_delegreturn_mutex);
128 put_nfs_open_context(ctx);
129 if (err != 0)
130 return err;
131 goto again;
133 spin_unlock(&inode->i_lock);
134 return 0;
138 * nfs_inode_reclaim_delegation - process a delegation reclaim request
139 * @inode: inode to process
140 * @cred: credential to use for request
141 * @res: new delegation state from server
144 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
145 struct nfs_openres *res)
147 struct nfs_delegation *delegation;
148 struct rpc_cred *oldcred = NULL;
150 rcu_read_lock();
151 delegation = rcu_dereference(NFS_I(inode)->delegation);
152 if (delegation != NULL) {
153 spin_lock(&delegation->lock);
154 if (delegation->inode != NULL) {
155 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
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 rcu_read_unlock();
165 put_rpccred(oldcred);
166 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
167 } else {
168 /* We appear to have raced with a delegation return. */
169 spin_unlock(&delegation->lock);
170 rcu_read_unlock();
171 nfs_inode_set_delegation(inode, cred, res);
173 } else {
174 rcu_read_unlock();
178 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
180 int res = 0;
182 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
183 res = nfs4_proc_delegreturn(inode,
184 delegation->cred,
185 &delegation->stateid,
186 issync);
187 nfs_free_delegation(delegation);
188 return res;
191 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
193 struct inode *inode = NULL;
195 spin_lock(&delegation->lock);
196 if (delegation->inode != NULL)
197 inode = igrab(delegation->inode);
198 spin_unlock(&delegation->lock);
199 return inode;
202 static struct nfs_delegation *
203 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
205 struct nfs_delegation *ret = NULL;
206 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
208 if (delegation == NULL)
209 goto out;
210 spin_lock(&delegation->lock);
211 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
212 ret = delegation;
213 spin_unlock(&delegation->lock);
214 out:
215 return ret;
218 static struct nfs_delegation *
219 nfs_start_delegation_return(struct nfs_inode *nfsi)
221 struct nfs_delegation *delegation;
223 rcu_read_lock();
224 delegation = nfs_start_delegation_return_locked(nfsi);
225 rcu_read_unlock();
226 return delegation;
229 static void
230 nfs_abort_delegation_return(struct nfs_delegation *delegation,
231 struct nfs_client *clp)
234 spin_lock(&delegation->lock);
235 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
236 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
237 spin_unlock(&delegation->lock);
238 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
241 static struct nfs_delegation *
242 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
243 struct nfs_delegation *delegation,
244 struct nfs_client *clp)
246 struct nfs_delegation *deleg_cur =
247 rcu_dereference_protected(nfsi->delegation,
248 lockdep_is_held(&clp->cl_lock));
250 if (deleg_cur == NULL || delegation != deleg_cur)
251 return NULL;
253 spin_lock(&delegation->lock);
254 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
255 list_del_rcu(&delegation->super_list);
256 delegation->inode = NULL;
257 nfsi->delegation_state = 0;
258 rcu_assign_pointer(nfsi->delegation, NULL);
259 spin_unlock(&delegation->lock);
260 return delegation;
263 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
264 struct nfs_delegation *delegation,
265 struct nfs_server *server)
267 struct nfs_client *clp = server->nfs_client;
269 spin_lock(&clp->cl_lock);
270 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
271 spin_unlock(&clp->cl_lock);
272 return delegation;
275 static struct nfs_delegation *
276 nfs_inode_detach_delegation(struct inode *inode)
278 struct nfs_inode *nfsi = NFS_I(inode);
279 struct nfs_server *server = NFS_SERVER(inode);
280 struct nfs_delegation *delegation;
282 delegation = nfs_start_delegation_return(nfsi);
283 if (delegation == NULL)
284 return NULL;
285 return nfs_detach_delegation(nfsi, delegation, server);
289 * nfs_inode_set_delegation - set up a delegation on an inode
290 * @inode: inode to which delegation applies
291 * @cred: cred to use for subsequent delegation processing
292 * @res: new delegation state from server
294 * Returns zero on success, or a negative errno value.
296 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
298 struct nfs_server *server = NFS_SERVER(inode);
299 struct nfs_client *clp = server->nfs_client;
300 struct nfs_inode *nfsi = NFS_I(inode);
301 struct nfs_delegation *delegation, *old_delegation;
302 struct nfs_delegation *freeme = NULL;
303 int status = 0;
305 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
306 if (delegation == NULL)
307 return -ENOMEM;
308 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
309 delegation->type = res->delegation_type;
310 delegation->maxsize = res->maxsize;
311 delegation->change_attr = inode->i_version;
312 delegation->cred = get_rpccred(cred);
313 delegation->inode = inode;
314 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
315 spin_lock_init(&delegation->lock);
317 spin_lock(&clp->cl_lock);
318 old_delegation = rcu_dereference_protected(nfsi->delegation,
319 lockdep_is_held(&clp->cl_lock));
320 if (old_delegation != NULL) {
321 if (nfs4_stateid_match(&delegation->stateid,
322 &old_delegation->stateid) &&
323 delegation->type == old_delegation->type) {
324 goto out;
327 * Deal with broken servers that hand out two
328 * delegations for the same file.
329 * Allow for upgrades to a WRITE delegation, but
330 * nothing else.
332 dfprintk(FILE, "%s: server %s handed out "
333 "a duplicate delegation!\n",
334 __func__, clp->cl_hostname);
335 if (delegation->type == old_delegation->type ||
336 !(delegation->type & FMODE_WRITE)) {
337 freeme = delegation;
338 delegation = NULL;
339 goto out;
341 freeme = nfs_detach_delegation_locked(nfsi,
342 old_delegation, clp);
343 if (freeme == NULL)
344 goto out;
346 list_add_rcu(&delegation->super_list, &server->delegations);
347 nfsi->delegation_state = delegation->type;
348 rcu_assign_pointer(nfsi->delegation, delegation);
349 delegation = NULL;
351 /* Ensure we revalidate the attributes and page cache! */
352 spin_lock(&inode->i_lock);
353 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
354 spin_unlock(&inode->i_lock);
355 trace_nfs4_set_delegation(inode, res->delegation_type);
357 out:
358 spin_unlock(&clp->cl_lock);
359 if (delegation != NULL)
360 nfs_free_delegation(delegation);
361 if (freeme != NULL)
362 nfs_do_return_delegation(inode, freeme, 0);
363 return status;
367 * Basic procedure for returning a delegation to the server
369 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
371 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
372 struct nfs_inode *nfsi = NFS_I(inode);
373 int err = 0;
375 if (delegation == NULL)
376 return 0;
377 do {
378 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
379 break;
380 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
381 if (!issync || err != -EAGAIN)
382 break;
384 * Guard against state recovery
386 err = nfs4_wait_clnt_recover(clp);
387 } while (err == 0);
389 if (err) {
390 nfs_abort_delegation_return(delegation, clp);
391 goto out;
393 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
394 goto out;
396 err = nfs_do_return_delegation(inode, delegation, issync);
397 out:
398 return err;
401 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
403 bool ret = false;
405 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
406 ret = true;
407 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
408 struct inode *inode;
410 spin_lock(&delegation->lock);
411 inode = delegation->inode;
412 if (inode && list_empty(&NFS_I(inode)->open_files))
413 ret = true;
414 spin_unlock(&delegation->lock);
416 return ret;
420 * nfs_client_return_marked_delegations - return previously marked delegations
421 * @clp: nfs_client to process
423 * Note that this function is designed to be called by the state
424 * manager thread. For this reason, it cannot flush the dirty data,
425 * since that could deadlock in case of a state recovery error.
427 * Returns zero on success, or a negative errno value.
429 int nfs_client_return_marked_delegations(struct nfs_client *clp)
431 struct nfs_delegation *delegation;
432 struct nfs_server *server;
433 struct inode *inode;
434 int err = 0;
436 restart:
437 rcu_read_lock();
438 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
439 list_for_each_entry_rcu(delegation, &server->delegations,
440 super_list) {
441 if (!nfs_delegation_need_return(delegation))
442 continue;
443 inode = nfs_delegation_grab_inode(delegation);
444 if (inode == NULL)
445 continue;
446 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
447 rcu_read_unlock();
449 err = nfs_end_delegation_return(inode, delegation, 0);
450 iput(inode);
451 if (!err)
452 goto restart;
453 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
454 return err;
457 rcu_read_unlock();
458 return 0;
462 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
463 * @inode: inode to process
465 * Does not protect against delegation reclaims, therefore really only safe
466 * to be called from nfs4_clear_inode().
468 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
470 struct nfs_delegation *delegation;
472 delegation = nfs_inode_detach_delegation(inode);
473 if (delegation != NULL)
474 nfs_do_return_delegation(inode, delegation, 0);
478 * nfs_inode_return_delegation - synchronously return a delegation
479 * @inode: inode to process
481 * This routine will always flush any dirty data to disk on the
482 * assumption that if we need to return the delegation, then
483 * we should stop caching.
485 * Returns zero on success, or a negative errno value.
487 int nfs4_inode_return_delegation(struct inode *inode)
489 struct nfs_inode *nfsi = NFS_I(inode);
490 struct nfs_delegation *delegation;
491 int err = 0;
493 nfs_wb_all(inode);
494 delegation = nfs_start_delegation_return(nfsi);
495 if (delegation != NULL)
496 err = nfs_end_delegation_return(inode, delegation, 1);
497 return err;
500 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
501 struct nfs_delegation *delegation)
503 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
504 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
507 static void nfs_mark_return_delegation(struct nfs_server *server,
508 struct nfs_delegation *delegation)
510 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
511 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
514 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
516 struct nfs_delegation *delegation;
517 bool ret = false;
519 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
520 nfs_mark_return_delegation(server, delegation);
521 ret = true;
523 return ret;
526 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
528 struct nfs_server *server;
530 rcu_read_lock();
531 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
532 nfs_server_mark_return_all_delegations(server);
533 rcu_read_unlock();
536 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
538 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
539 nfs4_schedule_state_manager(clp);
543 * nfs_expire_all_delegations
544 * @clp: client to process
547 void nfs_expire_all_delegations(struct nfs_client *clp)
549 nfs_client_mark_return_all_delegations(clp);
550 nfs_delegation_run_state_manager(clp);
554 * nfs_super_return_all_delegations - return delegations for one superblock
555 * @sb: sb to process
558 void nfs_server_return_all_delegations(struct nfs_server *server)
560 struct nfs_client *clp = server->nfs_client;
561 bool need_wait;
563 if (clp == NULL)
564 return;
566 rcu_read_lock();
567 need_wait = nfs_server_mark_return_all_delegations(server);
568 rcu_read_unlock();
570 if (need_wait) {
571 nfs4_schedule_state_manager(clp);
572 nfs4_wait_clnt_recover(clp);
576 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
577 fmode_t flags)
579 struct nfs_delegation *delegation;
581 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
582 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
583 continue;
584 if (delegation->type & flags)
585 nfs_mark_return_if_closed_delegation(server, delegation);
589 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
590 fmode_t flags)
592 struct nfs_server *server;
594 rcu_read_lock();
595 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
596 nfs_mark_return_unused_delegation_types(server, flags);
597 rcu_read_unlock();
600 static void nfs_revoke_delegation(struct inode *inode)
602 struct nfs_delegation *delegation;
603 rcu_read_lock();
604 delegation = rcu_dereference(NFS_I(inode)->delegation);
605 if (delegation != NULL) {
606 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
607 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
609 rcu_read_unlock();
612 void nfs_remove_bad_delegation(struct inode *inode)
614 struct nfs_delegation *delegation;
616 nfs_revoke_delegation(inode);
617 delegation = nfs_inode_detach_delegation(inode);
618 if (delegation) {
619 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
620 nfs_free_delegation(delegation);
623 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
626 * nfs_expire_unused_delegation_types
627 * @clp: client to process
628 * @flags: delegation types to expire
631 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
633 nfs_client_mark_return_unused_delegation_types(clp, flags);
634 nfs_delegation_run_state_manager(clp);
637 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
639 struct nfs_delegation *delegation;
641 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
642 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
643 continue;
644 nfs_mark_return_if_closed_delegation(server, delegation);
649 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
650 * @clp: nfs_client to process
653 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
655 struct nfs_server *server;
657 rcu_read_lock();
658 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
659 nfs_mark_return_unreferenced_delegations(server);
660 rcu_read_unlock();
662 nfs_delegation_run_state_manager(clp);
666 * nfs_async_inode_return_delegation - asynchronously return a delegation
667 * @inode: inode to process
668 * @stateid: state ID information
670 * Returns zero on success, or a negative errno value.
672 int nfs_async_inode_return_delegation(struct inode *inode,
673 const nfs4_stateid *stateid)
675 struct nfs_server *server = NFS_SERVER(inode);
676 struct nfs_client *clp = server->nfs_client;
677 struct nfs_delegation *delegation;
679 filemap_flush(inode->i_mapping);
681 rcu_read_lock();
682 delegation = rcu_dereference(NFS_I(inode)->delegation);
683 if (delegation == NULL)
684 goto out_enoent;
686 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
687 goto out_enoent;
688 nfs_mark_return_delegation(server, delegation);
689 rcu_read_unlock();
691 nfs_delegation_run_state_manager(clp);
692 return 0;
693 out_enoent:
694 rcu_read_unlock();
695 return -ENOENT;
698 static struct inode *
699 nfs_delegation_find_inode_server(struct nfs_server *server,
700 const struct nfs_fh *fhandle)
702 struct nfs_delegation *delegation;
703 struct inode *res = NULL;
705 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
706 spin_lock(&delegation->lock);
707 if (delegation->inode != NULL &&
708 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
709 res = igrab(delegation->inode);
711 spin_unlock(&delegation->lock);
712 if (res != NULL)
713 break;
715 return res;
719 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
720 * @clp: client state handle
721 * @fhandle: filehandle from a delegation recall
723 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
724 * cannot be found.
726 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
727 const struct nfs_fh *fhandle)
729 struct nfs_server *server;
730 struct inode *res = NULL;
732 rcu_read_lock();
733 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
734 res = nfs_delegation_find_inode_server(server, fhandle);
735 if (res != NULL)
736 break;
738 rcu_read_unlock();
739 return res;
742 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
744 struct nfs_delegation *delegation;
746 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
747 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
751 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
752 * @clp: nfs_client to process
755 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
757 struct nfs_server *server;
759 rcu_read_lock();
760 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
761 nfs_delegation_mark_reclaim_server(server);
762 rcu_read_unlock();
766 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
767 * @clp: nfs_client to process
770 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
772 struct nfs_delegation *delegation;
773 struct nfs_server *server;
774 struct inode *inode;
776 restart:
777 rcu_read_lock();
778 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
779 list_for_each_entry_rcu(delegation, &server->delegations,
780 super_list) {
781 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
782 &delegation->flags) == 0)
783 continue;
784 inode = nfs_delegation_grab_inode(delegation);
785 if (inode == NULL)
786 continue;
787 delegation = nfs_detach_delegation(NFS_I(inode),
788 delegation, server);
789 rcu_read_unlock();
791 if (delegation != NULL)
792 nfs_free_delegation(delegation);
793 iput(inode);
794 goto restart;
797 rcu_read_unlock();
801 * nfs_delegations_present - check for existence of delegations
802 * @clp: client state handle
804 * Returns one if there are any nfs_delegation structures attached
805 * to this nfs_client.
807 int nfs_delegations_present(struct nfs_client *clp)
809 struct nfs_server *server;
810 int ret = 0;
812 rcu_read_lock();
813 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
814 if (!list_empty(&server->delegations)) {
815 ret = 1;
816 break;
818 rcu_read_unlock();
819 return ret;
823 * nfs4_copy_delegation_stateid - Copy inode's state ID information
824 * @dst: stateid data structure to fill in
825 * @inode: inode to check
826 * @flags: delegation type requirement
828 * Returns "true" and fills in "dst->data" * if inode had a delegation,
829 * otherwise "false" is returned.
831 bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
832 fmode_t flags)
834 struct nfs_inode *nfsi = NFS_I(inode);
835 struct nfs_delegation *delegation;
836 bool ret;
838 flags &= FMODE_READ|FMODE_WRITE;
839 rcu_read_lock();
840 delegation = rcu_dereference(nfsi->delegation);
841 ret = (delegation != NULL && (delegation->type & flags) == flags);
842 if (ret) {
843 nfs4_stateid_copy(dst, &delegation->stateid);
844 nfs_mark_delegation_referenced(delegation);
846 rcu_read_unlock();
847 return ret;