6acc11e6ebad4259af0946930399c5477c78ef3c
[platform/adaptation/renesas_rcar/renesas_kernel.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 /**
45  * nfs_have_delegation - check if inode has a delegation
46  * @inode: inode to check
47  * @flags: delegation types to check for
48  *
49  * Returns one if inode has the indicated delegation, otherwise zero.
50  */
51 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
52 {
53         struct nfs_delegation *delegation;
54         int ret = 0;
55
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;
63         }
64         rcu_read_unlock();
65         return ret;
66 }
67
68 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
69 {
70         struct inode *inode = state->inode;
71         struct file_lock *fl;
72         int status = 0;
73
74         if (inode->i_flock == NULL)
75                 goto out;
76
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);
89         }
90         spin_unlock(&inode->i_lock);
91 out:
92         return status;
93 }
94
95 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
96 {
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;
103
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;
132         }
133         spin_unlock(&inode->i_lock);
134         return 0;
135 }
136
137 /**
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
142  *
143  */
144 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
145                                   struct nfs_openres *res)
146 {
147         struct nfs_delegation *delegation;
148         struct rpc_cred *oldcred = NULL;
149
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                         put_rpccred(oldcred);
165                         rcu_read_unlock();
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);
172                 }
173         } else {
174                 rcu_read_unlock();
175         }
176 }
177
178 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
179 {
180         int res = 0;
181
182         res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
183         nfs_free_delegation(delegation);
184         return res;
185 }
186
187 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
188 {
189         struct inode *inode = NULL;
190
191         spin_lock(&delegation->lock);
192         if (delegation->inode != NULL)
193                 inode = igrab(delegation->inode);
194         spin_unlock(&delegation->lock);
195         return inode;
196 }
197
198 static struct nfs_delegation *
199 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
200 {
201         struct nfs_delegation *ret = NULL;
202         struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
203
204         if (delegation == NULL)
205                 goto out;
206         spin_lock(&delegation->lock);
207         if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
208                 ret = delegation;
209         spin_unlock(&delegation->lock);
210 out:
211         return ret;
212 }
213
214 static struct nfs_delegation *
215 nfs_start_delegation_return(struct nfs_inode *nfsi)
216 {
217         struct nfs_delegation *delegation;
218
219         rcu_read_lock();
220         delegation = nfs_start_delegation_return_locked(nfsi);
221         rcu_read_unlock();
222         return delegation;
223 }
224
225 static void
226 nfs_abort_delegation_return(struct nfs_delegation *delegation,
227                 struct nfs_client *clp)
228 {
229
230         spin_lock(&delegation->lock);
231         clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
232         set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
233         spin_unlock(&delegation->lock);
234         set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
235 }
236
237 static struct nfs_delegation *
238 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
239                 struct nfs_delegation *delegation,
240                 struct nfs_client *clp)
241 {
242         struct nfs_delegation *deleg_cur =
243                 rcu_dereference_protected(nfsi->delegation,
244                                 lockdep_is_held(&clp->cl_lock));
245
246         if (deleg_cur == NULL || delegation != deleg_cur)
247                 return NULL;
248
249         spin_lock(&delegation->lock);
250         set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
251         list_del_rcu(&delegation->super_list);
252         delegation->inode = NULL;
253         nfsi->delegation_state = 0;
254         rcu_assign_pointer(nfsi->delegation, NULL);
255         spin_unlock(&delegation->lock);
256         return delegation;
257 }
258
259 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
260                 struct nfs_delegation *delegation,
261                 struct nfs_server *server)
262 {
263         struct nfs_client *clp = server->nfs_client;
264
265         spin_lock(&clp->cl_lock);
266         delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
267         spin_unlock(&clp->cl_lock);
268         return delegation;
269 }
270
271 static struct nfs_delegation *
272 nfs_inode_detach_delegation(struct inode *inode)
273 {
274         struct nfs_inode *nfsi = NFS_I(inode);
275         struct nfs_server *server = NFS_SERVER(inode);
276         struct nfs_delegation *delegation;
277
278         delegation = nfs_start_delegation_return(nfsi);
279         if (delegation == NULL)
280                 return NULL;
281         return nfs_detach_delegation(nfsi, delegation, server);
282 }
283
284 /**
285  * nfs_inode_set_delegation - set up a delegation on an inode
286  * @inode: inode to which delegation applies
287  * @cred: cred to use for subsequent delegation processing
288  * @res: new delegation state from server
289  *
290  * Returns zero on success, or a negative errno value.
291  */
292 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
293 {
294         struct nfs_server *server = NFS_SERVER(inode);
295         struct nfs_client *clp = server->nfs_client;
296         struct nfs_inode *nfsi = NFS_I(inode);
297         struct nfs_delegation *delegation, *old_delegation;
298         struct nfs_delegation *freeme = NULL;
299         int status = 0;
300
301         delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
302         if (delegation == NULL)
303                 return -ENOMEM;
304         nfs4_stateid_copy(&delegation->stateid, &res->delegation);
305         delegation->type = res->delegation_type;
306         delegation->maxsize = res->maxsize;
307         delegation->change_attr = inode->i_version;
308         delegation->cred = get_rpccred(cred);
309         delegation->inode = inode;
310         delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
311         spin_lock_init(&delegation->lock);
312
313         spin_lock(&clp->cl_lock);
314         old_delegation = rcu_dereference_protected(nfsi->delegation,
315                                         lockdep_is_held(&clp->cl_lock));
316         if (old_delegation != NULL) {
317                 if (nfs4_stateid_match(&delegation->stateid,
318                                         &old_delegation->stateid) &&
319                                 delegation->type == old_delegation->type) {
320                         goto out;
321                 }
322                 /*
323                  * Deal with broken servers that hand out two
324                  * delegations for the same file.
325                  * Allow for upgrades to a WRITE delegation, but
326                  * nothing else.
327                  */
328                 dfprintk(FILE, "%s: server %s handed out "
329                                 "a duplicate delegation!\n",
330                                 __func__, clp->cl_hostname);
331                 if (delegation->type == old_delegation->type ||
332                     !(delegation->type & FMODE_WRITE)) {
333                         freeme = delegation;
334                         delegation = NULL;
335                         goto out;
336                 }
337                 freeme = nfs_detach_delegation_locked(nfsi, 
338                                 old_delegation, clp);
339                 if (freeme == NULL)
340                         goto out;
341         }
342         list_add_rcu(&delegation->super_list, &server->delegations);
343         nfsi->delegation_state = delegation->type;
344         rcu_assign_pointer(nfsi->delegation, delegation);
345         delegation = NULL;
346
347         /* Ensure we revalidate the attributes and page cache! */
348         spin_lock(&inode->i_lock);
349         nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
350         spin_unlock(&inode->i_lock);
351         trace_nfs4_set_delegation(inode, res->delegation_type);
352
353 out:
354         spin_unlock(&clp->cl_lock);
355         if (delegation != NULL)
356                 nfs_free_delegation(delegation);
357         if (freeme != NULL)
358                 nfs_do_return_delegation(inode, freeme, 0);
359         return status;
360 }
361
362 /*
363  * Basic procedure for returning a delegation to the server
364  */
365 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
366 {
367         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
368         struct nfs_inode *nfsi = NFS_I(inode);
369         int err;
370
371         if (delegation == NULL)
372                 return 0;
373         do {
374                 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
375                 if (!issync || err != -EAGAIN)
376                         break;
377                 /*
378                  * Guard against state recovery
379                  */
380                 err = nfs4_wait_clnt_recover(clp);
381         } while (err == 0);
382
383         if (err) {
384                 nfs_abort_delegation_return(delegation, clp);
385                 goto out;
386         }
387         if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
388                 goto out;
389
390         err = nfs_do_return_delegation(inode, delegation, issync);
391 out:
392         return err;
393 }
394
395 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
396 {
397         bool ret = false;
398
399         if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
400                 ret = true;
401         if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
402                 struct inode *inode;
403
404                 spin_lock(&delegation->lock);
405                 inode = delegation->inode;
406                 if (inode && list_empty(&NFS_I(inode)->open_files))
407                         ret = true;
408                 spin_unlock(&delegation->lock);
409         }
410         return ret;
411 }
412
413 /**
414  * nfs_client_return_marked_delegations - return previously marked delegations
415  * @clp: nfs_client to process
416  *
417  * Note that this function is designed to be called by the state
418  * manager thread. For this reason, it cannot flush the dirty data,
419  * since that could deadlock in case of a state recovery error.
420  *
421  * Returns zero on success, or a negative errno value.
422  */
423 int nfs_client_return_marked_delegations(struct nfs_client *clp)
424 {
425         struct nfs_delegation *delegation;
426         struct nfs_server *server;
427         struct inode *inode;
428         int err = 0;
429
430 restart:
431         rcu_read_lock();
432         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
433                 list_for_each_entry_rcu(delegation, &server->delegations,
434                                                                 super_list) {
435                         if (!nfs_delegation_need_return(delegation))
436                                 continue;
437                         inode = nfs_delegation_grab_inode(delegation);
438                         if (inode == NULL)
439                                 continue;
440                         delegation = nfs_start_delegation_return_locked(NFS_I(inode));
441                         rcu_read_unlock();
442
443                         err = nfs_end_delegation_return(inode, delegation, 0);
444                         iput(inode);
445                         if (!err)
446                                 goto restart;
447                         set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
448                         return err;
449                 }
450         }
451         rcu_read_unlock();
452         return 0;
453 }
454
455 /**
456  * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
457  * @inode: inode to process
458  *
459  * Does not protect against delegation reclaims, therefore really only safe
460  * to be called from nfs4_clear_inode().
461  */
462 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
463 {
464         struct nfs_delegation *delegation;
465
466         delegation = nfs_inode_detach_delegation(inode);
467         if (delegation != NULL)
468                 nfs_do_return_delegation(inode, delegation, 0);
469 }
470
471 /**
472  * nfs_inode_return_delegation - synchronously return a delegation
473  * @inode: inode to process
474  *
475  * This routine will always flush any dirty data to disk on the
476  * assumption that if we need to return the delegation, then
477  * we should stop caching.
478  *
479  * Returns zero on success, or a negative errno value.
480  */
481 int nfs4_inode_return_delegation(struct inode *inode)
482 {
483         struct nfs_inode *nfsi = NFS_I(inode);
484         struct nfs_delegation *delegation;
485         int err = 0;
486
487         nfs_wb_all(inode);
488         delegation = nfs_start_delegation_return(nfsi);
489         if (delegation != NULL)
490                 err = nfs_end_delegation_return(inode, delegation, 1);
491         return err;
492 }
493
494 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
495                 struct nfs_delegation *delegation)
496 {
497         set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
498         set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
499 }
500
501 static void nfs_mark_return_delegation(struct nfs_server *server,
502                 struct nfs_delegation *delegation)
503 {
504         set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
505         set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
506 }
507
508 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
509 {
510         struct nfs_delegation *delegation;
511         bool ret = false;
512
513         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
514                 nfs_mark_return_delegation(server, delegation);
515                 ret = true;
516         }
517         return ret;
518 }
519
520 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
521 {
522         struct nfs_server *server;
523
524         rcu_read_lock();
525         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
526                 nfs_server_mark_return_all_delegations(server);
527         rcu_read_unlock();
528 }
529
530 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
531 {
532         if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
533                 nfs4_schedule_state_manager(clp);
534 }
535
536 /**
537  * nfs_expire_all_delegations
538  * @clp: client to process
539  *
540  */
541 void nfs_expire_all_delegations(struct nfs_client *clp)
542 {
543         nfs_client_mark_return_all_delegations(clp);
544         nfs_delegation_run_state_manager(clp);
545 }
546
547 /**
548  * nfs_super_return_all_delegations - return delegations for one superblock
549  * @sb: sb to process
550  *
551  */
552 void nfs_server_return_all_delegations(struct nfs_server *server)
553 {
554         struct nfs_client *clp = server->nfs_client;
555         bool need_wait;
556
557         if (clp == NULL)
558                 return;
559
560         rcu_read_lock();
561         need_wait = nfs_server_mark_return_all_delegations(server);
562         rcu_read_unlock();
563
564         if (need_wait) {
565                 nfs4_schedule_state_manager(clp);
566                 nfs4_wait_clnt_recover(clp);
567         }
568 }
569
570 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
571                                                  fmode_t flags)
572 {
573         struct nfs_delegation *delegation;
574
575         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
576                 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
577                         continue;
578                 if (delegation->type & flags)
579                         nfs_mark_return_if_closed_delegation(server, delegation);
580         }
581 }
582
583 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
584                                                         fmode_t flags)
585 {
586         struct nfs_server *server;
587
588         rcu_read_lock();
589         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
590                 nfs_mark_return_unused_delegation_types(server, flags);
591         rcu_read_unlock();
592 }
593
594 void nfs_remove_bad_delegation(struct inode *inode)
595 {
596         struct nfs_delegation *delegation;
597
598         delegation = nfs_inode_detach_delegation(inode);
599         if (delegation) {
600                 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
601                 nfs_free_delegation(delegation);
602         }
603 }
604 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
605
606 /**
607  * nfs_expire_unused_delegation_types
608  * @clp: client to process
609  * @flags: delegation types to expire
610  *
611  */
612 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
613 {
614         nfs_client_mark_return_unused_delegation_types(clp, flags);
615         nfs_delegation_run_state_manager(clp);
616 }
617
618 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
619 {
620         struct nfs_delegation *delegation;
621
622         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
623                 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
624                         continue;
625                 nfs_mark_return_if_closed_delegation(server, delegation);
626         }
627 }
628
629 /**
630  * nfs_expire_unreferenced_delegations - Eliminate unused delegations
631  * @clp: nfs_client to process
632  *
633  */
634 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
635 {
636         struct nfs_server *server;
637
638         rcu_read_lock();
639         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
640                 nfs_mark_return_unreferenced_delegations(server);
641         rcu_read_unlock();
642
643         nfs_delegation_run_state_manager(clp);
644 }
645
646 /**
647  * nfs_async_inode_return_delegation - asynchronously return a delegation
648  * @inode: inode to process
649  * @stateid: state ID information
650  *
651  * Returns zero on success, or a negative errno value.
652  */
653 int nfs_async_inode_return_delegation(struct inode *inode,
654                                       const nfs4_stateid *stateid)
655 {
656         struct nfs_server *server = NFS_SERVER(inode);
657         struct nfs_client *clp = server->nfs_client;
658         struct nfs_delegation *delegation;
659
660         filemap_flush(inode->i_mapping);
661
662         rcu_read_lock();
663         delegation = rcu_dereference(NFS_I(inode)->delegation);
664         if (delegation == NULL)
665                 goto out_enoent;
666
667         if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
668                 goto out_enoent;
669         nfs_mark_return_delegation(server, delegation);
670         rcu_read_unlock();
671
672         nfs_delegation_run_state_manager(clp);
673         return 0;
674 out_enoent:
675         rcu_read_unlock();
676         return -ENOENT;
677 }
678
679 static struct inode *
680 nfs_delegation_find_inode_server(struct nfs_server *server,
681                                  const struct nfs_fh *fhandle)
682 {
683         struct nfs_delegation *delegation;
684         struct inode *res = NULL;
685
686         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
687                 spin_lock(&delegation->lock);
688                 if (delegation->inode != NULL &&
689                     nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
690                         res = igrab(delegation->inode);
691                 }
692                 spin_unlock(&delegation->lock);
693                 if (res != NULL)
694                         break;
695         }
696         return res;
697 }
698
699 /**
700  * nfs_delegation_find_inode - retrieve the inode associated with a delegation
701  * @clp: client state handle
702  * @fhandle: filehandle from a delegation recall
703  *
704  * Returns pointer to inode matching "fhandle," or NULL if a matching inode
705  * cannot be found.
706  */
707 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
708                                         const struct nfs_fh *fhandle)
709 {
710         struct nfs_server *server;
711         struct inode *res = NULL;
712
713         rcu_read_lock();
714         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
715                 res = nfs_delegation_find_inode_server(server, fhandle);
716                 if (res != NULL)
717                         break;
718         }
719         rcu_read_unlock();
720         return res;
721 }
722
723 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
724 {
725         struct nfs_delegation *delegation;
726
727         list_for_each_entry_rcu(delegation, &server->delegations, super_list)
728                 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
729 }
730
731 /**
732  * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
733  * @clp: nfs_client to process
734  *
735  */
736 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
737 {
738         struct nfs_server *server;
739
740         rcu_read_lock();
741         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
742                 nfs_delegation_mark_reclaim_server(server);
743         rcu_read_unlock();
744 }
745
746 /**
747  * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
748  * @clp: nfs_client to process
749  *
750  */
751 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
752 {
753         struct nfs_delegation *delegation;
754         struct nfs_server *server;
755         struct inode *inode;
756
757 restart:
758         rcu_read_lock();
759         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
760                 list_for_each_entry_rcu(delegation, &server->delegations,
761                                                                 super_list) {
762                         if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
763                                                 &delegation->flags) == 0)
764                                 continue;
765                         inode = nfs_delegation_grab_inode(delegation);
766                         if (inode == NULL)
767                                 continue;
768                         delegation = nfs_detach_delegation(NFS_I(inode),
769                                         delegation, server);
770                         rcu_read_unlock();
771
772                         if (delegation != NULL)
773                                 nfs_free_delegation(delegation);
774                         iput(inode);
775                         goto restart;
776                 }
777         }
778         rcu_read_unlock();
779 }
780
781 /**
782  * nfs_delegations_present - check for existence of delegations
783  * @clp: client state handle
784  *
785  * Returns one if there are any nfs_delegation structures attached
786  * to this nfs_client.
787  */
788 int nfs_delegations_present(struct nfs_client *clp)
789 {
790         struct nfs_server *server;
791         int ret = 0;
792
793         rcu_read_lock();
794         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
795                 if (!list_empty(&server->delegations)) {
796                         ret = 1;
797                         break;
798                 }
799         rcu_read_unlock();
800         return ret;
801 }
802
803 /**
804  * nfs4_copy_delegation_stateid - Copy inode's state ID information
805  * @dst: stateid data structure to fill in
806  * @inode: inode to check
807  * @flags: delegation type requirement
808  *
809  * Returns "true" and fills in "dst->data" * if inode had a delegation,
810  * otherwise "false" is returned.
811  */
812 bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
813                 fmode_t flags)
814 {
815         struct nfs_inode *nfsi = NFS_I(inode);
816         struct nfs_delegation *delegation;
817         bool ret;
818
819         flags &= FMODE_READ|FMODE_WRITE;
820         rcu_read_lock();
821         delegation = rcu_dereference(nfsi->delegation);
822         ret = (delegation != NULL && (delegation->type & flags) == flags);
823         if (ret) {
824                 nfs4_stateid_copy(dst, &delegation->stateid);
825                 nfs_mark_delegation_referenced(delegation);
826         }
827         rcu_read_unlock();
828         return ret;
829 }