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