Merge tag 'xtensa-20180225' of git://github.com/jcmvbkbc/linux-xtensa
[cris-mirror.git] / fs / nfs / delegation.c
blobd8b47624fee203de73b26fba07cc2da10ef6c144
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>
15 #include <linux/iversion.h>
17 #include <linux/nfs4.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_xdr.h>
21 #include "nfs4_fs.h"
22 #include "delegation.h"
23 #include "internal.h"
24 #include "nfs4trace.h"
26 static void nfs_free_delegation(struct nfs_delegation *delegation)
28 if (delegation->cred) {
29 put_rpccred(delegation->cred);
30 delegation->cred = NULL;
32 kfree_rcu(delegation, rcu);
35 /**
36 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
37 * @delegation: delegation to process
40 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
42 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
45 static bool
46 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
47 fmode_t flags)
49 if (delegation != NULL && (delegation->type & flags) == flags &&
50 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
51 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
52 return true;
53 return false;
56 static int
57 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
59 struct nfs_delegation *delegation;
60 int ret = 0;
62 flags &= FMODE_READ|FMODE_WRITE;
63 rcu_read_lock();
64 delegation = rcu_dereference(NFS_I(inode)->delegation);
65 if (nfs4_is_valid_delegation(delegation, flags)) {
66 if (mark)
67 nfs_mark_delegation_referenced(delegation);
68 ret = 1;
70 rcu_read_unlock();
71 return ret;
73 /**
74 * nfs_have_delegation - check if inode has a delegation, mark it
75 * NFS_DELEGATION_REFERENCED if there is one.
76 * @inode: inode to check
77 * @flags: delegation types to check for
79 * Returns one if inode has the indicated delegation, otherwise zero.
81 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
83 return nfs4_do_check_delegation(inode, flags, true);
87 * nfs4_check_delegation - check if inode has a delegation, do not mark
88 * NFS_DELEGATION_REFERENCED if it has one.
90 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
92 return nfs4_do_check_delegation(inode, flags, false);
95 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
97 struct inode *inode = state->inode;
98 struct file_lock *fl;
99 struct file_lock_context *flctx = inode->i_flctx;
100 struct list_head *list;
101 int status = 0;
103 if (flctx == NULL)
104 goto out;
106 list = &flctx->flc_posix;
107 spin_lock(&flctx->flc_lock);
108 restart:
109 list_for_each_entry(fl, list, fl_list) {
110 if (nfs_file_open_context(fl->fl_file) != ctx)
111 continue;
112 spin_unlock(&flctx->flc_lock);
113 status = nfs4_lock_delegation_recall(fl, state, stateid);
114 if (status < 0)
115 goto out;
116 spin_lock(&flctx->flc_lock);
118 if (list == &flctx->flc_posix) {
119 list = &flctx->flc_flock;
120 goto restart;
122 spin_unlock(&flctx->flc_lock);
123 out:
124 return status;
127 static int nfs_delegation_claim_opens(struct inode *inode,
128 const nfs4_stateid *stateid, fmode_t type)
130 struct nfs_inode *nfsi = NFS_I(inode);
131 struct nfs_open_context *ctx;
132 struct nfs4_state_owner *sp;
133 struct nfs4_state *state;
134 unsigned int seq;
135 int err;
137 again:
138 spin_lock(&inode->i_lock);
139 list_for_each_entry(ctx, &nfsi->open_files, list) {
140 state = ctx->state;
141 if (state == NULL)
142 continue;
143 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
144 continue;
145 if (!nfs4_valid_open_stateid(state))
146 continue;
147 if (!nfs4_stateid_match(&state->stateid, stateid))
148 continue;
149 get_nfs_open_context(ctx);
150 spin_unlock(&inode->i_lock);
151 sp = state->owner;
152 /* Block nfs4_proc_unlck */
153 mutex_lock(&sp->so_delegreturn_mutex);
154 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
155 err = nfs4_open_delegation_recall(ctx, state, stateid, type);
156 if (!err)
157 err = nfs_delegation_claim_locks(ctx, state, stateid);
158 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
159 err = -EAGAIN;
160 mutex_unlock(&sp->so_delegreturn_mutex);
161 put_nfs_open_context(ctx);
162 if (err != 0)
163 return err;
164 goto again;
166 spin_unlock(&inode->i_lock);
167 return 0;
171 * nfs_inode_reclaim_delegation - process a delegation reclaim request
172 * @inode: inode to process
173 * @cred: credential to use for request
174 * @res: new delegation state from server
177 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
178 struct nfs_openres *res)
180 struct nfs_delegation *delegation;
181 struct rpc_cred *oldcred = NULL;
183 rcu_read_lock();
184 delegation = rcu_dereference(NFS_I(inode)->delegation);
185 if (delegation != NULL) {
186 spin_lock(&delegation->lock);
187 if (delegation->inode != NULL) {
188 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
189 delegation->type = res->delegation_type;
190 delegation->pagemod_limit = res->pagemod_limit;
191 oldcred = delegation->cred;
192 delegation->cred = get_rpccred(cred);
193 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
194 &delegation->flags);
195 spin_unlock(&delegation->lock);
196 rcu_read_unlock();
197 put_rpccred(oldcred);
198 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
199 return;
201 /* We appear to have raced with a delegation return. */
202 spin_unlock(&delegation->lock);
204 rcu_read_unlock();
205 nfs_inode_set_delegation(inode, cred, res);
208 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
210 int res = 0;
212 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
213 res = nfs4_proc_delegreturn(inode,
214 delegation->cred,
215 &delegation->stateid,
216 issync);
217 nfs_free_delegation(delegation);
218 return res;
221 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
223 struct inode *inode = NULL;
225 spin_lock(&delegation->lock);
226 if (delegation->inode != NULL)
227 inode = igrab(delegation->inode);
228 spin_unlock(&delegation->lock);
229 return inode;
232 static struct nfs_delegation *
233 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
235 struct nfs_delegation *ret = NULL;
236 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
238 if (delegation == NULL)
239 goto out;
240 spin_lock(&delegation->lock);
241 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
242 ret = delegation;
243 spin_unlock(&delegation->lock);
244 out:
245 return ret;
248 static struct nfs_delegation *
249 nfs_start_delegation_return(struct nfs_inode *nfsi)
251 struct nfs_delegation *delegation;
253 rcu_read_lock();
254 delegation = nfs_start_delegation_return_locked(nfsi);
255 rcu_read_unlock();
256 return delegation;
259 static void
260 nfs_abort_delegation_return(struct nfs_delegation *delegation,
261 struct nfs_client *clp)
264 spin_lock(&delegation->lock);
265 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
266 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
267 spin_unlock(&delegation->lock);
268 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
271 static struct nfs_delegation *
272 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
273 struct nfs_delegation *delegation,
274 struct nfs_client *clp)
276 struct nfs_delegation *deleg_cur =
277 rcu_dereference_protected(nfsi->delegation,
278 lockdep_is_held(&clp->cl_lock));
280 if (deleg_cur == NULL || delegation != deleg_cur)
281 return NULL;
283 spin_lock(&delegation->lock);
284 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
285 list_del_rcu(&delegation->super_list);
286 delegation->inode = NULL;
287 rcu_assign_pointer(nfsi->delegation, NULL);
288 spin_unlock(&delegation->lock);
289 return delegation;
292 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
293 struct nfs_delegation *delegation,
294 struct nfs_server *server)
296 struct nfs_client *clp = server->nfs_client;
298 spin_lock(&clp->cl_lock);
299 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
300 spin_unlock(&clp->cl_lock);
301 return delegation;
304 static struct nfs_delegation *
305 nfs_inode_detach_delegation(struct inode *inode)
307 struct nfs_inode *nfsi = NFS_I(inode);
308 struct nfs_server *server = NFS_SERVER(inode);
309 struct nfs_delegation *delegation;
311 delegation = nfs_start_delegation_return(nfsi);
312 if (delegation == NULL)
313 return NULL;
314 return nfs_detach_delegation(nfsi, delegation, server);
317 static void
318 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
319 const struct nfs_delegation *update)
321 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
322 delegation->stateid.seqid = update->stateid.seqid;
323 smp_wmb();
324 delegation->type = update->type;
329 * nfs_inode_set_delegation - set up a delegation on an inode
330 * @inode: inode to which delegation applies
331 * @cred: cred to use for subsequent delegation processing
332 * @res: new delegation state from server
334 * Returns zero on success, or a negative errno value.
336 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
338 struct nfs_server *server = NFS_SERVER(inode);
339 struct nfs_client *clp = server->nfs_client;
340 struct nfs_inode *nfsi = NFS_I(inode);
341 struct nfs_delegation *delegation, *old_delegation;
342 struct nfs_delegation *freeme = NULL;
343 int status = 0;
345 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
346 if (delegation == NULL)
347 return -ENOMEM;
348 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
349 delegation->type = res->delegation_type;
350 delegation->pagemod_limit = res->pagemod_limit;
351 delegation->change_attr = inode_peek_iversion_raw(inode);
352 delegation->cred = get_rpccred(cred);
353 delegation->inode = inode;
354 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
355 spin_lock_init(&delegation->lock);
357 spin_lock(&clp->cl_lock);
358 old_delegation = rcu_dereference_protected(nfsi->delegation,
359 lockdep_is_held(&clp->cl_lock));
360 if (old_delegation != NULL) {
361 /* Is this an update of the existing delegation? */
362 if (nfs4_stateid_match_other(&old_delegation->stateid,
363 &delegation->stateid)) {
364 nfs_update_inplace_delegation(old_delegation,
365 delegation);
366 goto out;
369 * Deal with broken servers that hand out two
370 * delegations for the same file.
371 * Allow for upgrades to a WRITE delegation, but
372 * nothing else.
374 dfprintk(FILE, "%s: server %s handed out "
375 "a duplicate delegation!\n",
376 __func__, clp->cl_hostname);
377 if (delegation->type == old_delegation->type ||
378 !(delegation->type & FMODE_WRITE)) {
379 freeme = delegation;
380 delegation = NULL;
381 goto out;
383 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
384 &old_delegation->flags))
385 goto out;
386 freeme = nfs_detach_delegation_locked(nfsi,
387 old_delegation, clp);
388 if (freeme == NULL)
389 goto out;
391 list_add_tail_rcu(&delegation->super_list, &server->delegations);
392 rcu_assign_pointer(nfsi->delegation, delegation);
393 delegation = NULL;
395 trace_nfs4_set_delegation(inode, res->delegation_type);
397 out:
398 spin_unlock(&clp->cl_lock);
399 if (delegation != NULL)
400 nfs_free_delegation(delegation);
401 if (freeme != NULL)
402 nfs_do_return_delegation(inode, freeme, 0);
403 return status;
407 * Basic procedure for returning a delegation to the server
409 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
411 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
412 struct nfs_inode *nfsi = NFS_I(inode);
413 int err = 0;
415 if (delegation == NULL)
416 return 0;
417 do {
418 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
419 break;
420 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
421 delegation->type);
422 if (!issync || err != -EAGAIN)
423 break;
425 * Guard against state recovery
427 err = nfs4_wait_clnt_recover(clp);
428 } while (err == 0);
430 if (err) {
431 nfs_abort_delegation_return(delegation, clp);
432 goto out;
434 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
435 goto out;
437 err = nfs_do_return_delegation(inode, delegation, issync);
438 out:
439 return err;
442 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
444 bool ret = false;
446 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
447 goto out;
448 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
449 ret = true;
450 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
451 struct inode *inode;
453 spin_lock(&delegation->lock);
454 inode = delegation->inode;
455 if (inode && list_empty(&NFS_I(inode)->open_files))
456 ret = true;
457 spin_unlock(&delegation->lock);
459 out:
460 return ret;
464 * nfs_client_return_marked_delegations - return previously marked delegations
465 * @clp: nfs_client to process
467 * Note that this function is designed to be called by the state
468 * manager thread. For this reason, it cannot flush the dirty data,
469 * since that could deadlock in case of a state recovery error.
471 * Returns zero on success, or a negative errno value.
473 int nfs_client_return_marked_delegations(struct nfs_client *clp)
475 struct nfs_delegation *delegation;
476 struct nfs_server *server;
477 struct inode *inode;
478 int err = 0;
480 restart:
481 rcu_read_lock();
482 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
483 list_for_each_entry_rcu(delegation, &server->delegations,
484 super_list) {
485 if (!nfs_delegation_need_return(delegation))
486 continue;
487 if (!nfs_sb_active(server->super))
488 continue;
489 inode = nfs_delegation_grab_inode(delegation);
490 if (inode == NULL) {
491 rcu_read_unlock();
492 nfs_sb_deactive(server->super);
493 goto restart;
495 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
496 rcu_read_unlock();
498 err = nfs_end_delegation_return(inode, delegation, 0);
499 iput(inode);
500 nfs_sb_deactive(server->super);
501 if (!err)
502 goto restart;
503 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
504 return err;
507 rcu_read_unlock();
508 return 0;
512 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
513 * @inode: inode to process
515 * Does not protect against delegation reclaims, therefore really only safe
516 * to be called from nfs4_clear_inode().
518 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
520 struct nfs_delegation *delegation;
522 delegation = nfs_inode_detach_delegation(inode);
523 if (delegation != NULL)
524 nfs_do_return_delegation(inode, delegation, 1);
528 * nfs_inode_return_delegation - synchronously return a delegation
529 * @inode: inode to process
531 * This routine will always flush any dirty data to disk on the
532 * assumption that if we need to return the delegation, then
533 * we should stop caching.
535 * Returns zero on success, or a negative errno value.
537 int nfs4_inode_return_delegation(struct inode *inode)
539 struct nfs_inode *nfsi = NFS_I(inode);
540 struct nfs_delegation *delegation;
541 int err = 0;
543 nfs_wb_all(inode);
544 delegation = nfs_start_delegation_return(nfsi);
545 if (delegation != NULL)
546 err = nfs_end_delegation_return(inode, delegation, 1);
547 return err;
550 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
551 struct nfs_delegation *delegation)
553 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
554 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
557 static void nfs_mark_return_delegation(struct nfs_server *server,
558 struct nfs_delegation *delegation)
560 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
561 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
564 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
566 struct nfs_delegation *delegation;
567 bool ret = false;
569 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
570 nfs_mark_return_delegation(server, delegation);
571 ret = true;
573 return ret;
576 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
578 struct nfs_server *server;
580 rcu_read_lock();
581 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
582 nfs_server_mark_return_all_delegations(server);
583 rcu_read_unlock();
586 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
588 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
589 nfs4_schedule_state_manager(clp);
593 * nfs_expire_all_delegations
594 * @clp: client to process
597 void nfs_expire_all_delegations(struct nfs_client *clp)
599 nfs_client_mark_return_all_delegations(clp);
600 nfs_delegation_run_state_manager(clp);
604 * nfs_super_return_all_delegations - return delegations for one superblock
605 * @sb: sb to process
608 void nfs_server_return_all_delegations(struct nfs_server *server)
610 struct nfs_client *clp = server->nfs_client;
611 bool need_wait;
613 if (clp == NULL)
614 return;
616 rcu_read_lock();
617 need_wait = nfs_server_mark_return_all_delegations(server);
618 rcu_read_unlock();
620 if (need_wait) {
621 nfs4_schedule_state_manager(clp);
622 nfs4_wait_clnt_recover(clp);
626 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
627 fmode_t flags)
629 struct nfs_delegation *delegation;
631 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
632 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
633 continue;
634 if (delegation->type & flags)
635 nfs_mark_return_if_closed_delegation(server, delegation);
639 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
640 fmode_t flags)
642 struct nfs_server *server;
644 rcu_read_lock();
645 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
646 nfs_mark_return_unused_delegation_types(server, flags);
647 rcu_read_unlock();
650 static void nfs_mark_delegation_revoked(struct nfs_server *server,
651 struct nfs_delegation *delegation)
653 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
654 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
655 nfs_mark_return_delegation(server, delegation);
658 static bool nfs_revoke_delegation(struct inode *inode,
659 const nfs4_stateid *stateid)
661 struct nfs_delegation *delegation;
662 nfs4_stateid tmp;
663 bool ret = false;
665 rcu_read_lock();
666 delegation = rcu_dereference(NFS_I(inode)->delegation);
667 if (delegation == NULL)
668 goto out;
669 if (stateid == NULL) {
670 nfs4_stateid_copy(&tmp, &delegation->stateid);
671 stateid = &tmp;
672 } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
673 goto out;
674 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
675 ret = true;
676 out:
677 rcu_read_unlock();
678 if (ret)
679 nfs_inode_find_state_and_recover(inode, stateid);
680 return ret;
683 void nfs_remove_bad_delegation(struct inode *inode,
684 const nfs4_stateid *stateid)
686 struct nfs_delegation *delegation;
688 if (!nfs_revoke_delegation(inode, stateid))
689 return;
690 delegation = nfs_inode_detach_delegation(inode);
691 if (delegation)
692 nfs_free_delegation(delegation);
694 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
697 * nfs_expire_unused_delegation_types
698 * @clp: client to process
699 * @flags: delegation types to expire
702 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
704 nfs_client_mark_return_unused_delegation_types(clp, flags);
705 nfs_delegation_run_state_manager(clp);
708 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
710 struct nfs_delegation *delegation;
712 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
713 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
714 continue;
715 nfs_mark_return_if_closed_delegation(server, delegation);
720 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
721 * @clp: nfs_client to process
724 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
726 struct nfs_server *server;
728 rcu_read_lock();
729 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
730 nfs_mark_return_unreferenced_delegations(server);
731 rcu_read_unlock();
733 nfs_delegation_run_state_manager(clp);
737 * nfs_async_inode_return_delegation - asynchronously return a delegation
738 * @inode: inode to process
739 * @stateid: state ID information
741 * Returns zero on success, or a negative errno value.
743 int nfs_async_inode_return_delegation(struct inode *inode,
744 const nfs4_stateid *stateid)
746 struct nfs_server *server = NFS_SERVER(inode);
747 struct nfs_client *clp = server->nfs_client;
748 struct nfs_delegation *delegation;
750 rcu_read_lock();
751 delegation = rcu_dereference(NFS_I(inode)->delegation);
752 if (delegation == NULL)
753 goto out_enoent;
754 if (stateid != NULL &&
755 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
756 goto out_enoent;
757 nfs_mark_return_delegation(server, delegation);
758 rcu_read_unlock();
760 nfs_delegation_run_state_manager(clp);
761 return 0;
762 out_enoent:
763 rcu_read_unlock();
764 return -ENOENT;
767 static struct inode *
768 nfs_delegation_find_inode_server(struct nfs_server *server,
769 const struct nfs_fh *fhandle)
771 struct nfs_delegation *delegation;
772 struct inode *res = NULL;
774 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
775 spin_lock(&delegation->lock);
776 if (delegation->inode != NULL &&
777 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
778 res = igrab(delegation->inode);
780 spin_unlock(&delegation->lock);
781 if (res != NULL)
782 break;
784 return res;
788 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
789 * @clp: client state handle
790 * @fhandle: filehandle from a delegation recall
792 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
793 * cannot be found.
795 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
796 const struct nfs_fh *fhandle)
798 struct nfs_server *server;
799 struct inode *res = NULL;
801 rcu_read_lock();
802 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
803 res = nfs_delegation_find_inode_server(server, fhandle);
804 if (res != NULL)
805 break;
807 rcu_read_unlock();
808 return res;
811 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
813 struct nfs_delegation *delegation;
815 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
817 * If the delegation may have been admin revoked, then we
818 * cannot reclaim it.
820 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
821 continue;
822 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
827 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
828 * @clp: nfs_client to process
831 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
833 struct nfs_server *server;
835 rcu_read_lock();
836 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
837 nfs_delegation_mark_reclaim_server(server);
838 rcu_read_unlock();
842 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
843 * @clp: nfs_client to process
846 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
848 struct nfs_delegation *delegation;
849 struct nfs_server *server;
850 struct inode *inode;
852 restart:
853 rcu_read_lock();
854 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
855 list_for_each_entry_rcu(delegation, &server->delegations,
856 super_list) {
857 if (test_bit(NFS_DELEGATION_RETURNING,
858 &delegation->flags))
859 continue;
860 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
861 &delegation->flags) == 0)
862 continue;
863 if (!nfs_sb_active(server->super))
864 continue;
865 inode = nfs_delegation_grab_inode(delegation);
866 if (inode == NULL) {
867 rcu_read_unlock();
868 nfs_sb_deactive(server->super);
869 goto restart;
871 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
872 rcu_read_unlock();
873 if (delegation != NULL) {
874 delegation = nfs_detach_delegation(NFS_I(inode),
875 delegation, server);
876 if (delegation != NULL)
877 nfs_free_delegation(delegation);
879 iput(inode);
880 nfs_sb_deactive(server->super);
881 goto restart;
884 rcu_read_unlock();
887 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
889 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
890 BIT(NFS4CLNT_LEASE_EXPIRED) |
891 BIT(NFS4CLNT_SESSION_RESET))) != 0;
894 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
895 struct nfs_delegation *delegation)
897 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
898 return;
899 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
900 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
901 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
904 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
905 struct inode *inode)
907 struct nfs_delegation *delegation;
909 rcu_read_lock();
910 delegation = rcu_dereference(NFS_I(inode)->delegation);
911 if (delegation)
912 nfs_mark_test_expired_delegation(server, delegation);
913 rcu_read_unlock();
917 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
919 struct nfs_delegation *delegation;
921 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
922 nfs_mark_test_expired_delegation(server, delegation);
926 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
927 * @clp: nfs_client to process
929 * Iterates through all the delegations associated with this server and
930 * marks them as needing to be checked for validity.
932 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
934 struct nfs_server *server;
936 rcu_read_lock();
937 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
938 nfs_delegation_mark_test_expired_server(server);
939 rcu_read_unlock();
943 * nfs_reap_expired_delegations - reap expired delegations
944 * @clp: nfs_client to process
946 * Iterates through all the delegations associated with this server and
947 * checks if they have may have been revoked. This function is usually
948 * expected to be called in cases where the server may have lost its
949 * lease.
951 void nfs_reap_expired_delegations(struct nfs_client *clp)
953 const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
954 struct nfs_delegation *delegation;
955 struct nfs_server *server;
956 struct inode *inode;
957 struct rpc_cred *cred;
958 nfs4_stateid stateid;
960 restart:
961 rcu_read_lock();
962 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
963 list_for_each_entry_rcu(delegation, &server->delegations,
964 super_list) {
965 if (test_bit(NFS_DELEGATION_RETURNING,
966 &delegation->flags))
967 continue;
968 if (test_bit(NFS_DELEGATION_TEST_EXPIRED,
969 &delegation->flags) == 0)
970 continue;
971 if (!nfs_sb_active(server->super))
972 continue;
973 inode = nfs_delegation_grab_inode(delegation);
974 if (inode == NULL) {
975 rcu_read_unlock();
976 nfs_sb_deactive(server->super);
977 goto restart;
979 cred = get_rpccred_rcu(delegation->cred);
980 nfs4_stateid_copy(&stateid, &delegation->stateid);
981 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
982 rcu_read_unlock();
983 if (cred != NULL &&
984 ops->test_and_free_expired(server, &stateid, cred) < 0) {
985 nfs_revoke_delegation(inode, &stateid);
986 nfs_inode_find_state_and_recover(inode, &stateid);
988 put_rpccred(cred);
989 if (nfs4_server_rebooted(clp)) {
990 nfs_inode_mark_test_expired_delegation(server,inode);
991 iput(inode);
992 nfs_sb_deactive(server->super);
993 return;
995 iput(inode);
996 nfs_sb_deactive(server->super);
997 goto restart;
1000 rcu_read_unlock();
1003 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1004 const nfs4_stateid *stateid)
1006 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1007 struct nfs_delegation *delegation;
1008 bool found = false;
1010 rcu_read_lock();
1011 delegation = rcu_dereference(NFS_I(inode)->delegation);
1012 if (delegation &&
1013 nfs4_stateid_match_other(&delegation->stateid, stateid)) {
1014 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1015 found = true;
1017 rcu_read_unlock();
1018 if (found)
1019 nfs4_schedule_state_manager(clp);
1023 * nfs_delegations_present - check for existence of delegations
1024 * @clp: client state handle
1026 * Returns one if there are any nfs_delegation structures attached
1027 * to this nfs_client.
1029 int nfs_delegations_present(struct nfs_client *clp)
1031 struct nfs_server *server;
1032 int ret = 0;
1034 rcu_read_lock();
1035 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1036 if (!list_empty(&server->delegations)) {
1037 ret = 1;
1038 break;
1040 rcu_read_unlock();
1041 return ret;
1045 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1046 * @dst: stateid to refresh
1047 * @inode: inode to check
1049 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1050 * that matches our delegation stateid. Otherwise "false" is returned.
1052 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1054 struct nfs_delegation *delegation;
1055 bool ret = false;
1056 if (!inode)
1057 goto out;
1059 rcu_read_lock();
1060 delegation = rcu_dereference(NFS_I(inode)->delegation);
1061 if (delegation != NULL &&
1062 nfs4_stateid_match_other(dst, &delegation->stateid)) {
1063 dst->seqid = delegation->stateid.seqid;
1064 return ret;
1066 rcu_read_unlock();
1067 out:
1068 return ret;
1072 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1073 * @inode: inode to check
1074 * @flags: delegation type requirement
1075 * @dst: stateid data structure to fill in
1076 * @cred: optional argument to retrieve credential
1078 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1079 * otherwise "false" is returned.
1081 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1082 nfs4_stateid *dst, struct rpc_cred **cred)
1084 struct nfs_inode *nfsi = NFS_I(inode);
1085 struct nfs_delegation *delegation;
1086 bool ret;
1088 flags &= FMODE_READ|FMODE_WRITE;
1089 rcu_read_lock();
1090 delegation = rcu_dereference(nfsi->delegation);
1091 ret = nfs4_is_valid_delegation(delegation, flags);
1092 if (ret) {
1093 nfs4_stateid_copy(dst, &delegation->stateid);
1094 nfs_mark_delegation_referenced(delegation);
1095 if (cred)
1096 *cred = get_rpccred(delegation->cred);
1098 rcu_read_unlock();
1099 return ret;
1103 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1104 * @inode: inode to check
1106 * This function checks the number of outstanding writes to the file
1107 * against the delegation 'space_limit' field to see if
1108 * the spec requires us to flush the file on close.
1110 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1112 struct nfs_inode *nfsi = NFS_I(inode);
1113 struct nfs_delegation *delegation;
1114 bool ret = true;
1116 rcu_read_lock();
1117 delegation = rcu_dereference(nfsi->delegation);
1118 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1119 goto out;
1120 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1121 ret = false;
1122 out:
1123 rcu_read_unlock();
1124 return ret;