]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/nfs/delegation.c
NFSv4: Don't report revoked delegations as valid in nfs_have_delegation()
[karo-tx-linux.git] / fs / nfs / delegation.c
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 #include "nfs4trace.h"
24
25 static void nfs_free_delegation(struct nfs_delegation *delegation)
26 {
27         if (delegation->cred) {
28                 put_rpccred(delegation->cred);
29                 delegation->cred = NULL;
30         }
31         kfree_rcu(delegation, rcu);
32 }
33
34 /**
35  * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
36  * @delegation: delegation to process
37  *
38  */
39 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
40 {
41         set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
42 }
43
44 static int
45 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
46 {
47         struct nfs_delegation *delegation;
48         int ret = 0;
49
50         flags &= FMODE_READ|FMODE_WRITE;
51         rcu_read_lock();
52         delegation = rcu_dereference(NFS_I(inode)->delegation);
53         if (delegation != NULL && (delegation->type & flags) == flags &&
54             !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
55             !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
56                 if (mark)
57                         nfs_mark_delegation_referenced(delegation);
58                 ret = 1;
59         }
60         rcu_read_unlock();
61         return ret;
62 }
63 /**
64  * nfs_have_delegation - check if inode has a delegation, mark it
65  * NFS_DELEGATION_REFERENCED if there is one.
66  * @inode: inode to check
67  * @flags: delegation types to check for
68  *
69  * Returns one if inode has the indicated delegation, otherwise zero.
70  */
71 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
72 {
73         return nfs4_do_check_delegation(inode, flags, true);
74 }
75
76 /*
77  * nfs4_check_delegation - check if inode has a delegation, do not mark
78  * NFS_DELEGATION_REFERENCED if it has one.
79  */
80 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
81 {
82         return nfs4_do_check_delegation(inode, flags, false);
83 }
84
85 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
86 {
87         struct inode *inode = state->inode;
88         struct file_lock *fl;
89         struct file_lock_context *flctx = inode->i_flctx;
90         struct list_head *list;
91         int status = 0;
92
93         if (flctx == NULL)
94                 goto out;
95
96         list = &flctx->flc_posix;
97         spin_lock(&flctx->flc_lock);
98 restart:
99         list_for_each_entry(fl, list, fl_list) {
100                 if (nfs_file_open_context(fl->fl_file) != ctx)
101                         continue;
102                 spin_unlock(&flctx->flc_lock);
103                 status = nfs4_lock_delegation_recall(fl, state, stateid);
104                 if (status < 0)
105                         goto out;
106                 spin_lock(&flctx->flc_lock);
107         }
108         if (list == &flctx->flc_posix) {
109                 list = &flctx->flc_flock;
110                 goto restart;
111         }
112         spin_unlock(&flctx->flc_lock);
113 out:
114         return status;
115 }
116
117 static int nfs_delegation_claim_opens(struct inode *inode,
118                 const nfs4_stateid *stateid, fmode_t type)
119 {
120         struct nfs_inode *nfsi = NFS_I(inode);
121         struct nfs_open_context *ctx;
122         struct nfs4_state_owner *sp;
123         struct nfs4_state *state;
124         unsigned int seq;
125         int err;
126
127 again:
128         spin_lock(&inode->i_lock);
129         list_for_each_entry(ctx, &nfsi->open_files, list) {
130                 state = ctx->state;
131                 if (state == NULL)
132                         continue;
133                 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
134                         continue;
135                 if (!nfs4_valid_open_stateid(state))
136                         continue;
137                 if (!nfs4_stateid_match(&state->stateid, stateid))
138                         continue;
139                 get_nfs_open_context(ctx);
140                 spin_unlock(&inode->i_lock);
141                 sp = state->owner;
142                 /* Block nfs4_proc_unlck */
143                 mutex_lock(&sp->so_delegreturn_mutex);
144                 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
145                 err = nfs4_open_delegation_recall(ctx, state, stateid, type);
146                 if (!err)
147                         err = nfs_delegation_claim_locks(ctx, state, stateid);
148                 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
149                         err = -EAGAIN;
150                 mutex_unlock(&sp->so_delegreturn_mutex);
151                 put_nfs_open_context(ctx);
152                 if (err != 0)
153                         return err;
154                 goto again;
155         }
156         spin_unlock(&inode->i_lock);
157         return 0;
158 }
159
160 /**
161  * nfs_inode_reclaim_delegation - process a delegation reclaim request
162  * @inode: inode to process
163  * @cred: credential to use for request
164  * @res: new delegation state from server
165  *
166  */
167 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
168                                   struct nfs_openres *res)
169 {
170         struct nfs_delegation *delegation;
171         struct rpc_cred *oldcred = NULL;
172
173         rcu_read_lock();
174         delegation = rcu_dereference(NFS_I(inode)->delegation);
175         if (delegation != NULL) {
176                 spin_lock(&delegation->lock);
177                 if (delegation->inode != NULL) {
178                         nfs4_stateid_copy(&delegation->stateid, &res->delegation);
179                         delegation->type = res->delegation_type;
180                         delegation->pagemod_limit = res->pagemod_limit;
181                         oldcred = delegation->cred;
182                         delegation->cred = get_rpccred(cred);
183                         clear_bit(NFS_DELEGATION_NEED_RECLAIM,
184                                   &delegation->flags);
185                         spin_unlock(&delegation->lock);
186                         rcu_read_unlock();
187                         put_rpccred(oldcred);
188                         trace_nfs4_reclaim_delegation(inode, res->delegation_type);
189                 } else {
190                         /* We appear to have raced with a delegation return. */
191                         spin_unlock(&delegation->lock);
192                         rcu_read_unlock();
193                         nfs_inode_set_delegation(inode, cred, res);
194                 }
195         } else {
196                 rcu_read_unlock();
197         }
198 }
199
200 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
201 {
202         int res = 0;
203
204         if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
205                 res = nfs4_proc_delegreturn(inode,
206                                 delegation->cred,
207                                 &delegation->stateid,
208                                 issync);
209         nfs_free_delegation(delegation);
210         return res;
211 }
212
213 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
214 {
215         struct inode *inode = NULL;
216
217         spin_lock(&delegation->lock);
218         if (delegation->inode != NULL)
219                 inode = igrab(delegation->inode);
220         spin_unlock(&delegation->lock);
221         return inode;
222 }
223
224 static struct nfs_delegation *
225 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
226 {
227         struct nfs_delegation *ret = NULL;
228         struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
229
230         if (delegation == NULL)
231                 goto out;
232         spin_lock(&delegation->lock);
233         if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
234                 ret = delegation;
235         spin_unlock(&delegation->lock);
236 out:
237         return ret;
238 }
239
240 static struct nfs_delegation *
241 nfs_start_delegation_return(struct nfs_inode *nfsi)
242 {
243         struct nfs_delegation *delegation;
244
245         rcu_read_lock();
246         delegation = nfs_start_delegation_return_locked(nfsi);
247         rcu_read_unlock();
248         return delegation;
249 }
250
251 static void
252 nfs_abort_delegation_return(struct nfs_delegation *delegation,
253                 struct nfs_client *clp)
254 {
255
256         spin_lock(&delegation->lock);
257         clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
258         set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
259         spin_unlock(&delegation->lock);
260         set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
261 }
262
263 static struct nfs_delegation *
264 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
265                 struct nfs_delegation *delegation,
266                 struct nfs_client *clp)
267 {
268         struct nfs_delegation *deleg_cur =
269                 rcu_dereference_protected(nfsi->delegation,
270                                 lockdep_is_held(&clp->cl_lock));
271
272         if (deleg_cur == NULL || delegation != deleg_cur)
273                 return NULL;
274
275         spin_lock(&delegation->lock);
276         set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
277         list_del_rcu(&delegation->super_list);
278         delegation->inode = NULL;
279         rcu_assign_pointer(nfsi->delegation, NULL);
280         spin_unlock(&delegation->lock);
281         return delegation;
282 }
283
284 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
285                 struct nfs_delegation *delegation,
286                 struct nfs_server *server)
287 {
288         struct nfs_client *clp = server->nfs_client;
289
290         spin_lock(&clp->cl_lock);
291         delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
292         spin_unlock(&clp->cl_lock);
293         return delegation;
294 }
295
296 static struct nfs_delegation *
297 nfs_inode_detach_delegation(struct inode *inode)
298 {
299         struct nfs_inode *nfsi = NFS_I(inode);
300         struct nfs_server *server = NFS_SERVER(inode);
301         struct nfs_delegation *delegation;
302
303         delegation = nfs_start_delegation_return(nfsi);
304         if (delegation == NULL)
305                 return NULL;
306         return nfs_detach_delegation(nfsi, delegation, server);
307 }
308
309 static void
310 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
311                 const struct nfs_delegation *update)
312 {
313         if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
314                 delegation->stateid.seqid = update->stateid.seqid;
315                 smp_wmb();
316                 delegation->type = update->type;
317         }
318 }
319
320 /**
321  * nfs_inode_set_delegation - set up a delegation on an inode
322  * @inode: inode to which delegation applies
323  * @cred: cred to use for subsequent delegation processing
324  * @res: new delegation state from server
325  *
326  * Returns zero on success, or a negative errno value.
327  */
328 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
329 {
330         struct nfs_server *server = NFS_SERVER(inode);
331         struct nfs_client *clp = server->nfs_client;
332         struct nfs_inode *nfsi = NFS_I(inode);
333         struct nfs_delegation *delegation, *old_delegation;
334         struct nfs_delegation *freeme = NULL;
335         int status = 0;
336
337         delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
338         if (delegation == NULL)
339                 return -ENOMEM;
340         nfs4_stateid_copy(&delegation->stateid, &res->delegation);
341         delegation->type = res->delegation_type;
342         delegation->pagemod_limit = res->pagemod_limit;
343         delegation->change_attr = inode->i_version;
344         delegation->cred = get_rpccred(cred);
345         delegation->inode = inode;
346         delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
347         spin_lock_init(&delegation->lock);
348
349         spin_lock(&clp->cl_lock);
350         old_delegation = rcu_dereference_protected(nfsi->delegation,
351                                         lockdep_is_held(&clp->cl_lock));
352         if (old_delegation != NULL) {
353                 /* Is this an update of the existing delegation? */
354                 if (nfs4_stateid_match_other(&old_delegation->stateid,
355                                         &delegation->stateid)) {
356                         nfs_update_inplace_delegation(old_delegation,
357                                         delegation);
358                         goto out;
359                 }
360                 /*
361                  * Deal with broken servers that hand out two
362                  * delegations for the same file.
363                  * Allow for upgrades to a WRITE delegation, but
364                  * nothing else.
365                  */
366                 dfprintk(FILE, "%s: server %s handed out "
367                                 "a duplicate delegation!\n",
368                                 __func__, clp->cl_hostname);
369                 if (delegation->type == old_delegation->type ||
370                     !(delegation->type & FMODE_WRITE)) {
371                         freeme = delegation;
372                         delegation = NULL;
373                         goto out;
374                 }
375                 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
376                                         &old_delegation->flags))
377                         goto out;
378                 freeme = nfs_detach_delegation_locked(nfsi,
379                                 old_delegation, clp);
380                 if (freeme == NULL)
381                         goto out;
382         }
383         list_add_tail_rcu(&delegation->super_list, &server->delegations);
384         rcu_assign_pointer(nfsi->delegation, delegation);
385         delegation = NULL;
386
387         /* Ensure we revalidate the attributes and page cache! */
388         spin_lock(&inode->i_lock);
389         nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
390         spin_unlock(&inode->i_lock);
391         trace_nfs4_set_delegation(inode, res->delegation_type);
392
393 out:
394         spin_unlock(&clp->cl_lock);
395         if (delegation != NULL)
396                 nfs_free_delegation(delegation);
397         if (freeme != NULL)
398                 nfs_do_return_delegation(inode, freeme, 0);
399         return status;
400 }
401
402 /*
403  * Basic procedure for returning a delegation to the server
404  */
405 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
406 {
407         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
408         struct nfs_inode *nfsi = NFS_I(inode);
409         int err = 0;
410
411         if (delegation == NULL)
412                 return 0;
413         do {
414                 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
415                         break;
416                 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
417                                 delegation->type);
418                 if (!issync || err != -EAGAIN)
419                         break;
420                 /*
421                  * Guard against state recovery
422                  */
423                 err = nfs4_wait_clnt_recover(clp);
424         } while (err == 0);
425
426         if (err) {
427                 nfs_abort_delegation_return(delegation, clp);
428                 goto out;
429         }
430         if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
431                 goto out;
432
433         err = nfs_do_return_delegation(inode, delegation, issync);
434 out:
435         return err;
436 }
437
438 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
439 {
440         bool ret = false;
441
442         if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
443                 goto out;
444         if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
445                 ret = true;
446         if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
447                 struct inode *inode;
448
449                 spin_lock(&delegation->lock);
450                 inode = delegation->inode;
451                 if (inode && list_empty(&NFS_I(inode)->open_files))
452                         ret = true;
453                 spin_unlock(&delegation->lock);
454         }
455 out:
456         return ret;
457 }
458
459 /**
460  * nfs_client_return_marked_delegations - return previously marked delegations
461  * @clp: nfs_client to process
462  *
463  * Note that this function is designed to be called by the state
464  * manager thread. For this reason, it cannot flush the dirty data,
465  * since that could deadlock in case of a state recovery error.
466  *
467  * Returns zero on success, or a negative errno value.
468  */
469 int nfs_client_return_marked_delegations(struct nfs_client *clp)
470 {
471         struct nfs_delegation *delegation;
472         struct nfs_server *server;
473         struct inode *inode;
474         int err = 0;
475
476 restart:
477         rcu_read_lock();
478         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
479                 list_for_each_entry_rcu(delegation, &server->delegations,
480                                                                 super_list) {
481                         if (!nfs_delegation_need_return(delegation))
482                                 continue;
483                         if (!nfs_sb_active(server->super))
484                                 continue;
485                         inode = nfs_delegation_grab_inode(delegation);
486                         if (inode == NULL) {
487                                 rcu_read_unlock();
488                                 nfs_sb_deactive(server->super);
489                                 goto restart;
490                         }
491                         delegation = nfs_start_delegation_return_locked(NFS_I(inode));
492                         rcu_read_unlock();
493
494                         err = nfs_end_delegation_return(inode, delegation, 0);
495                         iput(inode);
496                         nfs_sb_deactive(server->super);
497                         if (!err)
498                                 goto restart;
499                         set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
500                         return err;
501                 }
502         }
503         rcu_read_unlock();
504         return 0;
505 }
506
507 /**
508  * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
509  * @inode: inode to process
510  *
511  * Does not protect against delegation reclaims, therefore really only safe
512  * to be called from nfs4_clear_inode().
513  */
514 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
515 {
516         struct nfs_delegation *delegation;
517
518         delegation = nfs_inode_detach_delegation(inode);
519         if (delegation != NULL)
520                 nfs_do_return_delegation(inode, delegation, 1);
521 }
522
523 /**
524  * nfs_inode_return_delegation - synchronously return a delegation
525  * @inode: inode to process
526  *
527  * This routine will always flush any dirty data to disk on the
528  * assumption that if we need to return the delegation, then
529  * we should stop caching.
530  *
531  * Returns zero on success, or a negative errno value.
532  */
533 int nfs4_inode_return_delegation(struct inode *inode)
534 {
535         struct nfs_inode *nfsi = NFS_I(inode);
536         struct nfs_delegation *delegation;
537         int err = 0;
538
539         nfs_wb_all(inode);
540         delegation = nfs_start_delegation_return(nfsi);
541         if (delegation != NULL)
542                 err = nfs_end_delegation_return(inode, delegation, 1);
543         return err;
544 }
545
546 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
547                 struct nfs_delegation *delegation)
548 {
549         set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
550         set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
551 }
552
553 static void nfs_mark_return_delegation(struct nfs_server *server,
554                 struct nfs_delegation *delegation)
555 {
556         set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
557         set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
558 }
559
560 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
561 {
562         struct nfs_delegation *delegation;
563         bool ret = false;
564
565         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
566                 nfs_mark_return_delegation(server, delegation);
567                 ret = true;
568         }
569         return ret;
570 }
571
572 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
573 {
574         struct nfs_server *server;
575
576         rcu_read_lock();
577         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
578                 nfs_server_mark_return_all_delegations(server);
579         rcu_read_unlock();
580 }
581
582 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
583 {
584         if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
585                 nfs4_schedule_state_manager(clp);
586 }
587
588 /**
589  * nfs_expire_all_delegations
590  * @clp: client to process
591  *
592  */
593 void nfs_expire_all_delegations(struct nfs_client *clp)
594 {
595         nfs_client_mark_return_all_delegations(clp);
596         nfs_delegation_run_state_manager(clp);
597 }
598
599 /**
600  * nfs_super_return_all_delegations - return delegations for one superblock
601  * @sb: sb to process
602  *
603  */
604 void nfs_server_return_all_delegations(struct nfs_server *server)
605 {
606         struct nfs_client *clp = server->nfs_client;
607         bool need_wait;
608
609         if (clp == NULL)
610                 return;
611
612         rcu_read_lock();
613         need_wait = nfs_server_mark_return_all_delegations(server);
614         rcu_read_unlock();
615
616         if (need_wait) {
617                 nfs4_schedule_state_manager(clp);
618                 nfs4_wait_clnt_recover(clp);
619         }
620 }
621
622 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
623                                                  fmode_t flags)
624 {
625         struct nfs_delegation *delegation;
626
627         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
628                 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
629                         continue;
630                 if (delegation->type & flags)
631                         nfs_mark_return_if_closed_delegation(server, delegation);
632         }
633 }
634
635 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
636                                                         fmode_t flags)
637 {
638         struct nfs_server *server;
639
640         rcu_read_lock();
641         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
642                 nfs_mark_return_unused_delegation_types(server, flags);
643         rcu_read_unlock();
644 }
645
646 static void nfs_revoke_delegation(struct inode *inode)
647 {
648         struct nfs_delegation *delegation;
649         rcu_read_lock();
650         delegation = rcu_dereference(NFS_I(inode)->delegation);
651         if (delegation != NULL) {
652                 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
653                 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
654         }
655         rcu_read_unlock();
656 }
657
658 void nfs_remove_bad_delegation(struct inode *inode)
659 {
660         struct nfs_delegation *delegation;
661
662         nfs_revoke_delegation(inode);
663         delegation = nfs_inode_detach_delegation(inode);
664         if (delegation) {
665                 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
666                 nfs_free_delegation(delegation);
667         }
668 }
669 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
670
671 /**
672  * nfs_expire_unused_delegation_types
673  * @clp: client to process
674  * @flags: delegation types to expire
675  *
676  */
677 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
678 {
679         nfs_client_mark_return_unused_delegation_types(clp, flags);
680         nfs_delegation_run_state_manager(clp);
681 }
682
683 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
684 {
685         struct nfs_delegation *delegation;
686
687         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
688                 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
689                         continue;
690                 nfs_mark_return_if_closed_delegation(server, delegation);
691         }
692 }
693
694 /**
695  * nfs_expire_unreferenced_delegations - Eliminate unused delegations
696  * @clp: nfs_client to process
697  *
698  */
699 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
700 {
701         struct nfs_server *server;
702
703         rcu_read_lock();
704         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
705                 nfs_mark_return_unreferenced_delegations(server);
706         rcu_read_unlock();
707
708         nfs_delegation_run_state_manager(clp);
709 }
710
711 /**
712  * nfs_async_inode_return_delegation - asynchronously return a delegation
713  * @inode: inode to process
714  * @stateid: state ID information
715  *
716  * Returns zero on success, or a negative errno value.
717  */
718 int nfs_async_inode_return_delegation(struct inode *inode,
719                                       const nfs4_stateid *stateid)
720 {
721         struct nfs_server *server = NFS_SERVER(inode);
722         struct nfs_client *clp = server->nfs_client;
723         struct nfs_delegation *delegation;
724
725         rcu_read_lock();
726         delegation = rcu_dereference(NFS_I(inode)->delegation);
727         if (delegation == NULL)
728                 goto out_enoent;
729         if (stateid != NULL &&
730             !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
731                 goto out_enoent;
732         nfs_mark_return_delegation(server, delegation);
733         rcu_read_unlock();
734
735         nfs_delegation_run_state_manager(clp);
736         return 0;
737 out_enoent:
738         rcu_read_unlock();
739         return -ENOENT;
740 }
741
742 static struct inode *
743 nfs_delegation_find_inode_server(struct nfs_server *server,
744                                  const struct nfs_fh *fhandle)
745 {
746         struct nfs_delegation *delegation;
747         struct inode *res = NULL;
748
749         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
750                 spin_lock(&delegation->lock);
751                 if (delegation->inode != NULL &&
752                     nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
753                         res = igrab(delegation->inode);
754                 }
755                 spin_unlock(&delegation->lock);
756                 if (res != NULL)
757                         break;
758         }
759         return res;
760 }
761
762 /**
763  * nfs_delegation_find_inode - retrieve the inode associated with a delegation
764  * @clp: client state handle
765  * @fhandle: filehandle from a delegation recall
766  *
767  * Returns pointer to inode matching "fhandle," or NULL if a matching inode
768  * cannot be found.
769  */
770 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
771                                         const struct nfs_fh *fhandle)
772 {
773         struct nfs_server *server;
774         struct inode *res = NULL;
775
776         rcu_read_lock();
777         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
778                 res = nfs_delegation_find_inode_server(server, fhandle);
779                 if (res != NULL)
780                         break;
781         }
782         rcu_read_unlock();
783         return res;
784 }
785
786 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
787 {
788         struct nfs_delegation *delegation;
789
790         list_for_each_entry_rcu(delegation, &server->delegations, super_list)
791                 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
792 }
793
794 /**
795  * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
796  * @clp: nfs_client to process
797  *
798  */
799 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
800 {
801         struct nfs_server *server;
802
803         rcu_read_lock();
804         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
805                 nfs_delegation_mark_reclaim_server(server);
806         rcu_read_unlock();
807 }
808
809 /**
810  * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
811  * @clp: nfs_client to process
812  *
813  */
814 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
815 {
816         struct nfs_delegation *delegation;
817         struct nfs_server *server;
818         struct inode *inode;
819
820 restart:
821         rcu_read_lock();
822         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
823                 list_for_each_entry_rcu(delegation, &server->delegations,
824                                                                 super_list) {
825                         if (test_bit(NFS_DELEGATION_RETURNING,
826                                                 &delegation->flags))
827                                 continue;
828                         if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
829                                                 &delegation->flags) == 0)
830                                 continue;
831                         if (!nfs_sb_active(server->super))
832                                 continue;
833                         inode = nfs_delegation_grab_inode(delegation);
834                         if (inode == NULL) {
835                                 rcu_read_unlock();
836                                 nfs_sb_deactive(server->super);
837                                 goto restart;
838                         }
839                         delegation = nfs_start_delegation_return_locked(NFS_I(inode));
840                         rcu_read_unlock();
841                         if (delegation != NULL) {
842                                 delegation = nfs_detach_delegation(NFS_I(inode),
843                                         delegation, server);
844                                 if (delegation != NULL)
845                                         nfs_free_delegation(delegation);
846                         }
847                         iput(inode);
848                         nfs_sb_deactive(server->super);
849                         goto restart;
850                 }
851         }
852         rcu_read_unlock();
853 }
854
855 /**
856  * nfs_delegations_present - check for existence of delegations
857  * @clp: client state handle
858  *
859  * Returns one if there are any nfs_delegation structures attached
860  * to this nfs_client.
861  */
862 int nfs_delegations_present(struct nfs_client *clp)
863 {
864         struct nfs_server *server;
865         int ret = 0;
866
867         rcu_read_lock();
868         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
869                 if (!list_empty(&server->delegations)) {
870                         ret = 1;
871                         break;
872                 }
873         rcu_read_unlock();
874         return ret;
875 }
876
877 /**
878  * nfs4_copy_delegation_stateid - Copy inode's state ID information
879  * @inode: inode to check
880  * @flags: delegation type requirement
881  * @dst: stateid data structure to fill in
882  * @cred: optional argument to retrieve credential
883  *
884  * Returns "true" and fills in "dst->data" * if inode had a delegation,
885  * otherwise "false" is returned.
886  */
887 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
888                 nfs4_stateid *dst, struct rpc_cred **cred)
889 {
890         struct nfs_inode *nfsi = NFS_I(inode);
891         struct nfs_delegation *delegation;
892         bool ret;
893
894         flags &= FMODE_READ|FMODE_WRITE;
895         rcu_read_lock();
896         delegation = rcu_dereference(nfsi->delegation);
897         ret = (delegation != NULL && (delegation->type & flags) == flags);
898         if (ret) {
899                 nfs4_stateid_copy(dst, &delegation->stateid);
900                 nfs_mark_delegation_referenced(delegation);
901                 if (cred)
902                         *cred = get_rpccred(delegation->cred);
903         }
904         rcu_read_unlock();
905         return ret;
906 }
907
908 /**
909  * nfs4_delegation_flush_on_close - Check if we must flush file on close
910  * @inode: inode to check
911  *
912  * This function checks the number of outstanding writes to the file
913  * against the delegation 'space_limit' field to see if
914  * the spec requires us to flush the file on close.
915  */
916 bool nfs4_delegation_flush_on_close(const struct inode *inode)
917 {
918         struct nfs_inode *nfsi = NFS_I(inode);
919         struct nfs_delegation *delegation;
920         bool ret = true;
921
922         rcu_read_lock();
923         delegation = rcu_dereference(nfsi->delegation);
924         if (delegation == NULL || !(delegation->type & FMODE_WRITE))
925                 goto out;
926         if (nfsi->nrequests < delegation->pagemod_limit)
927                 ret = false;
928 out:
929         rcu_read_unlock();
930         return ret;
931 }