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