]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/nfs/delegation.c
NFSv4: Ensure the delegation is pinned in nfs_do_return_delegation()
[linux.git] / fs / nfs / delegation.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/fs/nfs/delegation.c
4  *
5  * Copyright (C) 2004 Trond Myklebust
6  *
7  * NFS file delegation management
8  *
9  */
10 #include <linux/completion.h>
11 #include <linux/kthread.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/iversion.h>
17
18 #include <linux/nfs4.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_xdr.h>
21
22 #include "nfs4_fs.h"
23 #include "nfs4session.h"
24 #include "delegation.h"
25 #include "internal.h"
26 #include "nfs4trace.h"
27
28 #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)
29
30 static atomic_long_t nfs_active_delegations;
31 static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK;
32
33 static void __nfs_free_delegation(struct nfs_delegation *delegation)
34 {
35         put_cred(delegation->cred);
36         delegation->cred = NULL;
37         kfree_rcu(delegation, rcu);
38 }
39
40 static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
41 {
42         if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
43                 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
44                 atomic_long_dec(&nfs_active_delegations);
45                 if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
46                         nfs_clear_verifier_delegated(delegation->inode);
47         }
48 }
49
50 static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
51 {
52         refcount_inc(&delegation->refcount);
53         return delegation;
54 }
55
56 static void nfs_put_delegation(struct nfs_delegation *delegation)
57 {
58         if (refcount_dec_and_test(&delegation->refcount))
59                 __nfs_free_delegation(delegation);
60 }
61
62 static void nfs_free_delegation(struct nfs_delegation *delegation)
63 {
64         nfs_mark_delegation_revoked(delegation);
65         nfs_put_delegation(delegation);
66 }
67
68 /**
69  * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
70  * @delegation: delegation to process
71  *
72  */
73 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
74 {
75         set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
76 }
77
78 static bool
79 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
80                 fmode_t flags)
81 {
82         if (delegation != NULL && (delegation->type & flags) == flags &&
83             !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
84             !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
85                 return true;
86         return false;
87 }
88
89 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
90 {
91         struct nfs_delegation *delegation;
92
93         delegation = rcu_dereference(NFS_I(inode)->delegation);
94         if (nfs4_is_valid_delegation(delegation, 0))
95                 return delegation;
96         return NULL;
97 }
98
99 static int
100 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
101 {
102         struct nfs_delegation *delegation;
103         int ret = 0;
104
105         flags &= FMODE_READ|FMODE_WRITE;
106         rcu_read_lock();
107         delegation = rcu_dereference(NFS_I(inode)->delegation);
108         if (nfs4_is_valid_delegation(delegation, flags)) {
109                 if (mark)
110                         nfs_mark_delegation_referenced(delegation);
111                 ret = 1;
112         }
113         rcu_read_unlock();
114         return ret;
115 }
116 /**
117  * nfs_have_delegation - check if inode has a delegation, mark it
118  * NFS_DELEGATION_REFERENCED if there is one.
119  * @inode: inode to check
120  * @flags: delegation types to check for
121  *
122  * Returns one if inode has the indicated delegation, otherwise zero.
123  */
124 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
125 {
126         return nfs4_do_check_delegation(inode, flags, true);
127 }
128
129 /*
130  * nfs4_check_delegation - check if inode has a delegation, do not mark
131  * NFS_DELEGATION_REFERENCED if it has one.
132  */
133 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
134 {
135         return nfs4_do_check_delegation(inode, flags, false);
136 }
137
138 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
139 {
140         struct inode *inode = state->inode;
141         struct file_lock *fl;
142         struct file_lock_context *flctx = inode->i_flctx;
143         struct list_head *list;
144         int status = 0;
145
146         if (flctx == NULL)
147                 goto out;
148
149         list = &flctx->flc_posix;
150         spin_lock(&flctx->flc_lock);
151 restart:
152         list_for_each_entry(fl, list, fl_list) {
153                 if (nfs_file_open_context(fl->fl_file)->state != state)
154                         continue;
155                 spin_unlock(&flctx->flc_lock);
156                 status = nfs4_lock_delegation_recall(fl, state, stateid);
157                 if (status < 0)
158                         goto out;
159                 spin_lock(&flctx->flc_lock);
160         }
161         if (list == &flctx->flc_posix) {
162                 list = &flctx->flc_flock;
163                 goto restart;
164         }
165         spin_unlock(&flctx->flc_lock);
166 out:
167         return status;
168 }
169
170 static int nfs_delegation_claim_opens(struct inode *inode,
171                 const nfs4_stateid *stateid, fmode_t type)
172 {
173         struct nfs_inode *nfsi = NFS_I(inode);
174         struct nfs_open_context *ctx;
175         struct nfs4_state_owner *sp;
176         struct nfs4_state *state;
177         unsigned int seq;
178         int err;
179
180 again:
181         rcu_read_lock();
182         list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
183                 state = ctx->state;
184                 if (state == NULL)
185                         continue;
186                 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
187                         continue;
188                 if (!nfs4_valid_open_stateid(state))
189                         continue;
190                 if (!nfs4_stateid_match(&state->stateid, stateid))
191                         continue;
192                 if (!get_nfs_open_context(ctx))
193                         continue;
194                 rcu_read_unlock();
195                 sp = state->owner;
196                 /* Block nfs4_proc_unlck */
197                 mutex_lock(&sp->so_delegreturn_mutex);
198                 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
199                 err = nfs4_open_delegation_recall(ctx, state, stateid);
200                 if (!err)
201                         err = nfs_delegation_claim_locks(state, stateid);
202                 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
203                         err = -EAGAIN;
204                 mutex_unlock(&sp->so_delegreturn_mutex);
205                 put_nfs_open_context(ctx);
206                 if (err != 0)
207                         return err;
208                 goto again;
209         }
210         rcu_read_unlock();
211         return 0;
212 }
213
214 /**
215  * nfs_inode_reclaim_delegation - process a delegation reclaim request
216  * @inode: inode to process
217  * @cred: credential to use for request
218  * @type: delegation type
219  * @stateid: delegation stateid
220  * @pagemod_limit: write delegation "space_limit"
221  *
222  */
223 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
224                                   fmode_t type,
225                                   const nfs4_stateid *stateid,
226                                   unsigned long pagemod_limit)
227 {
228         struct nfs_delegation *delegation;
229         const struct cred *oldcred = NULL;
230
231         rcu_read_lock();
232         delegation = rcu_dereference(NFS_I(inode)->delegation);
233         if (delegation != NULL) {
234                 spin_lock(&delegation->lock);
235                 if (nfs4_is_valid_delegation(delegation, 0)) {
236                         nfs4_stateid_copy(&delegation->stateid, stateid);
237                         delegation->type = type;
238                         delegation->pagemod_limit = pagemod_limit;
239                         oldcred = delegation->cred;
240                         delegation->cred = get_cred(cred);
241                         clear_bit(NFS_DELEGATION_NEED_RECLAIM,
242                                   &delegation->flags);
243                         spin_unlock(&delegation->lock);
244                         rcu_read_unlock();
245                         put_cred(oldcred);
246                         trace_nfs4_reclaim_delegation(inode, type);
247                         return;
248                 }
249                 /* We appear to have raced with a delegation return. */
250                 spin_unlock(&delegation->lock);
251         }
252         rcu_read_unlock();
253         nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
254 }
255
256 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
257 {
258         int res = 0;
259
260         if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
261                 res = nfs4_proc_delegreturn(inode,
262                                 delegation->cred,
263                                 &delegation->stateid,
264                                 issync);
265         return res;
266 }
267
268 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
269 {
270         struct inode *inode = NULL;
271
272         spin_lock(&delegation->lock);
273         if (delegation->inode != NULL)
274                 inode = igrab(delegation->inode);
275         if (!inode)
276                 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
277         spin_unlock(&delegation->lock);
278         return inode;
279 }
280
281 static struct nfs_delegation *
282 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
283 {
284         struct nfs_delegation *ret = NULL;
285         struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
286
287         if (delegation == NULL)
288                 goto out;
289         spin_lock(&delegation->lock);
290         if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
291                 /* Refcount matched in nfs_end_delegation_return() */
292                 ret = nfs_get_delegation(delegation);
293         }
294         spin_unlock(&delegation->lock);
295         if (ret)
296                 nfs_clear_verifier_delegated(&nfsi->vfs_inode);
297 out:
298         return ret;
299 }
300
301 static struct nfs_delegation *
302 nfs_start_delegation_return(struct nfs_inode *nfsi)
303 {
304         struct nfs_delegation *delegation;
305
306         rcu_read_lock();
307         delegation = nfs_start_delegation_return_locked(nfsi);
308         rcu_read_unlock();
309         return delegation;
310 }
311
312 static void
313 nfs_abort_delegation_return(struct nfs_delegation *delegation,
314                 struct nfs_client *clp)
315 {
316
317         spin_lock(&delegation->lock);
318         clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
319         set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
320         spin_unlock(&delegation->lock);
321         set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
322 }
323
324 static struct nfs_delegation *
325 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
326                 struct nfs_delegation *delegation,
327                 struct nfs_client *clp)
328 {
329         struct nfs_delegation *deleg_cur =
330                 rcu_dereference_protected(nfsi->delegation,
331                                 lockdep_is_held(&clp->cl_lock));
332
333         if (deleg_cur == NULL || delegation != deleg_cur)
334                 return NULL;
335
336         spin_lock(&delegation->lock);
337         if (!delegation->inode) {
338                 spin_unlock(&delegation->lock);
339                 return NULL;
340         }
341         list_del_rcu(&delegation->super_list);
342         delegation->inode = NULL;
343         rcu_assign_pointer(nfsi->delegation, NULL);
344         spin_unlock(&delegation->lock);
345         return delegation;
346 }
347
348 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
349                 struct nfs_delegation *delegation,
350                 struct nfs_server *server)
351 {
352         struct nfs_client *clp = server->nfs_client;
353
354         spin_lock(&clp->cl_lock);
355         delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
356         spin_unlock(&clp->cl_lock);
357         return delegation;
358 }
359
360 static struct nfs_delegation *
361 nfs_inode_detach_delegation(struct inode *inode)
362 {
363         struct nfs_inode *nfsi = NFS_I(inode);
364         struct nfs_server *server = NFS_SERVER(inode);
365         struct nfs_delegation *delegation;
366
367         rcu_read_lock();
368         delegation = rcu_dereference(nfsi->delegation);
369         if (delegation != NULL)
370                 delegation = nfs_detach_delegation(nfsi, delegation, server);
371         rcu_read_unlock();
372         return delegation;
373 }
374
375 static void
376 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
377                 const struct nfs_delegation *update)
378 {
379         if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
380                 delegation->stateid.seqid = update->stateid.seqid;
381                 smp_wmb();
382                 delegation->type = update->type;
383                 if (test_and_clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
384                         atomic_long_inc(&nfs_active_delegations);
385         }
386 }
387
388 /**
389  * nfs_inode_set_delegation - set up a delegation on an inode
390  * @inode: inode to which delegation applies
391  * @cred: cred to use for subsequent delegation processing
392  * @type: delegation type
393  * @stateid: delegation stateid
394  * @pagemod_limit: write delegation "space_limit"
395  *
396  * Returns zero on success, or a negative errno value.
397  */
398 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
399                                   fmode_t type,
400                                   const nfs4_stateid *stateid,
401                                   unsigned long pagemod_limit)
402 {
403         struct nfs_server *server = NFS_SERVER(inode);
404         struct nfs_client *clp = server->nfs_client;
405         struct nfs_inode *nfsi = NFS_I(inode);
406         struct nfs_delegation *delegation, *old_delegation;
407         struct nfs_delegation *freeme = NULL;
408         int status = 0;
409
410         delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
411         if (delegation == NULL)
412                 return -ENOMEM;
413         nfs4_stateid_copy(&delegation->stateid, stateid);
414         refcount_set(&delegation->refcount, 1);
415         delegation->type = type;
416         delegation->pagemod_limit = pagemod_limit;
417         delegation->change_attr = inode_peek_iversion_raw(inode);
418         delegation->cred = get_cred(cred);
419         delegation->inode = inode;
420         delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
421         spin_lock_init(&delegation->lock);
422
423         spin_lock(&clp->cl_lock);
424         old_delegation = rcu_dereference_protected(nfsi->delegation,
425                                         lockdep_is_held(&clp->cl_lock));
426         if (old_delegation == NULL)
427                 goto add_new;
428         /* Is this an update of the existing delegation? */
429         if (nfs4_stateid_match_other(&old_delegation->stateid,
430                                 &delegation->stateid)) {
431                 spin_lock(&old_delegation->lock);
432                 nfs_update_inplace_delegation(old_delegation,
433                                 delegation);
434                 spin_unlock(&old_delegation->lock);
435                 goto out;
436         }
437         if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
438                 /*
439                  * Deal with broken servers that hand out two
440                  * delegations for the same file.
441                  * Allow for upgrades to a WRITE delegation, but
442                  * nothing else.
443                  */
444                 dfprintk(FILE, "%s: server %s handed out "
445                                 "a duplicate delegation!\n",
446                                 __func__, clp->cl_hostname);
447                 if (delegation->type == old_delegation->type ||
448                     !(delegation->type & FMODE_WRITE)) {
449                         freeme = delegation;
450                         delegation = NULL;
451                         goto out;
452                 }
453                 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
454                                         &old_delegation->flags))
455                         goto out;
456         }
457         freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
458         if (freeme == NULL)
459                 goto out;
460 add_new:
461         list_add_tail_rcu(&delegation->super_list, &server->delegations);
462         rcu_assign_pointer(nfsi->delegation, delegation);
463         delegation = NULL;
464
465         atomic_long_inc(&nfs_active_delegations);
466
467         trace_nfs4_set_delegation(inode, type);
468
469         spin_lock(&inode->i_lock);
470         if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
471                 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
472         spin_unlock(&inode->i_lock);
473 out:
474         spin_unlock(&clp->cl_lock);
475         if (delegation != NULL)
476                 __nfs_free_delegation(delegation);
477         if (freeme != NULL) {
478                 nfs_do_return_delegation(inode, freeme, 0);
479                 nfs_free_delegation(freeme);
480         }
481         return status;
482 }
483
484 /*
485  * Basic procedure for returning a delegation to the server
486  */
487 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
488 {
489         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
490         int err = 0;
491
492         if (delegation == NULL)
493                 return 0;
494         do {
495                 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
496                         break;
497                 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
498                                 delegation->type);
499                 if (!issync || err != -EAGAIN)
500                         break;
501                 /*
502                  * Guard against state recovery
503                  */
504                 err = nfs4_wait_clnt_recover(clp);
505         } while (err == 0);
506
507         if (err) {
508                 nfs_abort_delegation_return(delegation, clp);
509                 goto out;
510         }
511
512         err = nfs_do_return_delegation(inode, delegation, issync);
513 out:
514         /* Refcount matched in nfs_start_delegation_return_locked() */
515         nfs_put_delegation(delegation);
516         return err;
517 }
518
519 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
520 {
521         bool ret = false;
522
523         if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
524                 ret = true;
525         else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
526                 struct inode *inode;
527
528                 spin_lock(&delegation->lock);
529                 inode = delegation->inode;
530                 if (inode && list_empty(&NFS_I(inode)->open_files))
531                         ret = true;
532                 spin_unlock(&delegation->lock);
533         }
534         if (ret)
535                 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
536         if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
537             test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
538                 ret = false;
539
540         return ret;
541 }
542
543 /**
544  * nfs_client_return_marked_delegations - return previously marked delegations
545  * @clp: nfs_client to process
546  *
547  * Note that this function is designed to be called by the state
548  * manager thread. For this reason, it cannot flush the dirty data,
549  * since that could deadlock in case of a state recovery error.
550  *
551  * Returns zero on success, or a negative errno value.
552  */
553 int nfs_client_return_marked_delegations(struct nfs_client *clp)
554 {
555         struct nfs_delegation *delegation;
556         struct nfs_delegation *prev;
557         struct nfs_server *server;
558         struct inode *inode;
559         struct inode *place_holder = NULL;
560         struct nfs_delegation *place_holder_deleg = NULL;
561         int err = 0;
562
563 restart:
564         /*
565          * To avoid quadratic looping we hold a reference
566          * to an inode place_holder.  Each time we restart, we
567          * list nfs_servers from the server of that inode, and
568          * delegation in the server from the delegations of that
569          * inode.
570          * prev is an RCU-protected pointer to a delegation which
571          * wasn't marked for return and might be a good choice for
572          * the next place_holder.
573          */
574         rcu_read_lock();
575         prev = NULL;
576         if (place_holder)
577                 server = NFS_SERVER(place_holder);
578         else
579                 server = list_entry_rcu(clp->cl_superblocks.next,
580                                         struct nfs_server, client_link);
581         list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
582                 delegation = NULL;
583                 if (place_holder && server == NFS_SERVER(place_holder))
584                         delegation = rcu_dereference(NFS_I(place_holder)->delegation);
585                 if (!delegation || delegation != place_holder_deleg)
586                         delegation = list_entry_rcu(server->delegations.next,
587                                                     struct nfs_delegation, super_list);
588                 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
589                         struct inode *to_put = NULL;
590
591                         if (!nfs_delegation_need_return(delegation)) {
592                                 prev = delegation;
593                                 continue;
594                         }
595                         if (!nfs_sb_active(server->super))
596                                 break; /* continue in outer loop */
597
598                         if (prev) {
599                                 struct inode *tmp;
600
601                                 tmp = nfs_delegation_grab_inode(prev);
602                                 if (tmp) {
603                                         to_put = place_holder;
604                                         place_holder = tmp;
605                                         place_holder_deleg = prev;
606                                 }
607                         }
608
609                         inode = nfs_delegation_grab_inode(delegation);
610                         if (inode == NULL) {
611                                 rcu_read_unlock();
612                                 if (to_put)
613                                         iput(to_put);
614                                 nfs_sb_deactive(server->super);
615                                 goto restart;
616                         }
617                         delegation = nfs_start_delegation_return_locked(NFS_I(inode));
618                         rcu_read_unlock();
619
620                         if (to_put)
621                                 iput(to_put);
622
623                         err = nfs_end_delegation_return(inode, delegation, 0);
624                         iput(inode);
625                         nfs_sb_deactive(server->super);
626                         cond_resched();
627                         if (!err)
628                                 goto restart;
629                         set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
630                         if (place_holder)
631                                 iput(place_holder);
632                         return err;
633                 }
634         }
635         rcu_read_unlock();
636         if (place_holder)
637                 iput(place_holder);
638         return 0;
639 }
640
641 /**
642  * nfs_inode_evict_delegation - return delegation, don't reclaim opens
643  * @inode: inode to process
644  *
645  * Does not protect against delegation reclaims, therefore really only safe
646  * to be called from nfs4_clear_inode(). Guaranteed to always free
647  * the delegation structure.
648  */
649 void nfs_inode_evict_delegation(struct inode *inode)
650 {
651         struct nfs_delegation *delegation;
652
653         delegation = nfs_inode_detach_delegation(inode);
654         if (delegation != NULL) {
655                 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
656                 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
657                 nfs_do_return_delegation(inode, delegation, 1);
658                 nfs_free_delegation(delegation);
659         }
660 }
661
662 /**
663  * nfs_inode_return_delegation - synchronously return a delegation
664  * @inode: inode to process
665  *
666  * This routine will always flush any dirty data to disk on the
667  * assumption that if we need to return the delegation, then
668  * we should stop caching.
669  *
670  * Returns zero on success, or a negative errno value.
671  */
672 int nfs4_inode_return_delegation(struct inode *inode)
673 {
674         struct nfs_inode *nfsi = NFS_I(inode);
675         struct nfs_delegation *delegation;
676         int err = 0;
677
678         nfs_wb_all(inode);
679         delegation = nfs_start_delegation_return(nfsi);
680         if (delegation != NULL)
681                 err = nfs_end_delegation_return(inode, delegation, 1);
682         return err;
683 }
684
685 /**
686  * nfs_inode_return_delegation_on_close - asynchronously return a delegation
687  * @inode: inode to process
688  *
689  * This routine is called on file close in order to determine if the
690  * inode delegation needs to be returned immediately.
691  */
692 void nfs4_inode_return_delegation_on_close(struct inode *inode)
693 {
694         struct nfs_delegation *delegation;
695         struct nfs_delegation *ret = NULL;
696
697         if (!inode)
698                 return;
699         rcu_read_lock();
700         delegation = nfs4_get_valid_delegation(inode);
701         if (!delegation)
702                 goto out;
703         if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
704             atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
705                 spin_lock(&delegation->lock);
706                 if (delegation->inode &&
707                     list_empty(&NFS_I(inode)->open_files) &&
708                     !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
709                         clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
710                         /* Refcount matched in nfs_end_delegation_return() */
711                         ret = nfs_get_delegation(delegation);
712                 }
713                 spin_unlock(&delegation->lock);
714                 if (ret)
715                         nfs_clear_verifier_delegated(inode);
716         }
717 out:
718         rcu_read_unlock();
719         nfs_end_delegation_return(inode, ret, 0);
720 }
721
722 /**
723  * nfs4_inode_make_writeable
724  * @inode: pointer to inode
725  *
726  * Make the inode writeable by returning the delegation if necessary
727  *
728  * Returns zero on success, or a negative errno value.
729  */
730 int nfs4_inode_make_writeable(struct inode *inode)
731 {
732         struct nfs_delegation *delegation;
733
734         rcu_read_lock();
735         delegation = nfs4_get_valid_delegation(inode);
736         if (delegation == NULL ||
737             (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
738              (delegation->type & FMODE_WRITE))) {
739                 rcu_read_unlock();
740                 return 0;
741         }
742         rcu_read_unlock();
743         return nfs4_inode_return_delegation(inode);
744 }
745
746 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
747                 struct nfs_delegation *delegation)
748 {
749         set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
750         set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
751 }
752
753 static void nfs_mark_return_delegation(struct nfs_server *server,
754                 struct nfs_delegation *delegation)
755 {
756         set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
757         set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
758 }
759
760 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
761 {
762         struct nfs_delegation *delegation;
763         bool ret = false;
764
765         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
766                 nfs_mark_return_delegation(server, delegation);
767                 ret = true;
768         }
769         return ret;
770 }
771
772 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
773 {
774         struct nfs_server *server;
775
776         rcu_read_lock();
777         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
778                 nfs_server_mark_return_all_delegations(server);
779         rcu_read_unlock();
780 }
781
782 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
783 {
784         if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
785                 nfs4_schedule_state_manager(clp);
786 }
787
788 /**
789  * nfs_expire_all_delegations
790  * @clp: client to process
791  *
792  */
793 void nfs_expire_all_delegations(struct nfs_client *clp)
794 {
795         nfs_client_mark_return_all_delegations(clp);
796         nfs_delegation_run_state_manager(clp);
797 }
798
799 /**
800  * nfs_super_return_all_delegations - return delegations for one superblock
801  * @server: pointer to nfs_server to process
802  *
803  */
804 void nfs_server_return_all_delegations(struct nfs_server *server)
805 {
806         struct nfs_client *clp = server->nfs_client;
807         bool need_wait;
808
809         if (clp == NULL)
810                 return;
811
812         rcu_read_lock();
813         need_wait = nfs_server_mark_return_all_delegations(server);
814         rcu_read_unlock();
815
816         if (need_wait) {
817                 nfs4_schedule_state_manager(clp);
818                 nfs4_wait_clnt_recover(clp);
819         }
820 }
821
822 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
823                                                  fmode_t flags)
824 {
825         struct nfs_delegation *delegation;
826
827         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
828                 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
829                         continue;
830                 if (delegation->type & flags)
831                         nfs_mark_return_if_closed_delegation(server, delegation);
832         }
833 }
834
835 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
836                                                         fmode_t flags)
837 {
838         struct nfs_server *server;
839
840         rcu_read_lock();
841         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
842                 nfs_mark_return_unused_delegation_types(server, flags);
843         rcu_read_unlock();
844 }
845
846 static void nfs_revoke_delegation(struct inode *inode,
847                 const nfs4_stateid *stateid)
848 {
849         struct nfs_delegation *delegation;
850         nfs4_stateid tmp;
851         bool ret = false;
852
853         rcu_read_lock();
854         delegation = rcu_dereference(NFS_I(inode)->delegation);
855         if (delegation == NULL)
856                 goto out;
857         if (stateid == NULL) {
858                 nfs4_stateid_copy(&tmp, &delegation->stateid);
859                 stateid = &tmp;
860         } else {
861                 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
862                         goto out;
863                 spin_lock(&delegation->lock);
864                 if (stateid->seqid) {
865                         if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
866                                 spin_unlock(&delegation->lock);
867                                 goto out;
868                         }
869                         delegation->stateid.seqid = stateid->seqid;
870                 }
871                 spin_unlock(&delegation->lock);
872         }
873         nfs_mark_delegation_revoked(delegation);
874         ret = true;
875 out:
876         rcu_read_unlock();
877         if (ret)
878                 nfs_inode_find_state_and_recover(inode, stateid);
879 }
880
881 void nfs_remove_bad_delegation(struct inode *inode,
882                 const nfs4_stateid *stateid)
883 {
884         nfs_revoke_delegation(inode, stateid);
885 }
886 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
887
888 void nfs_delegation_mark_returned(struct inode *inode,
889                 const nfs4_stateid *stateid)
890 {
891         struct nfs_delegation *delegation;
892
893         if (!inode)
894                 return;
895
896         rcu_read_lock();
897         delegation = rcu_dereference(NFS_I(inode)->delegation);
898         if (!delegation)
899                 goto out_rcu_unlock;
900
901         spin_lock(&delegation->lock);
902         if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
903                 goto out_spin_unlock;
904         if (stateid->seqid) {
905                 /* If delegation->stateid is newer, dont mark as returned */
906                 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
907                         goto out_clear_returning;
908                 if (delegation->stateid.seqid != stateid->seqid)
909                         delegation->stateid.seqid = stateid->seqid;
910         }
911
912         nfs_mark_delegation_revoked(delegation);
913
914 out_clear_returning:
915         clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
916 out_spin_unlock:
917         spin_unlock(&delegation->lock);
918 out_rcu_unlock:
919         rcu_read_unlock();
920
921         nfs_inode_find_state_and_recover(inode, stateid);
922 }
923
924 /**
925  * nfs_expire_unused_delegation_types
926  * @clp: client to process
927  * @flags: delegation types to expire
928  *
929  */
930 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
931 {
932         nfs_client_mark_return_unused_delegation_types(clp, flags);
933         nfs_delegation_run_state_manager(clp);
934 }
935
936 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
937 {
938         struct nfs_delegation *delegation;
939
940         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
941                 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
942                         continue;
943                 nfs_mark_return_if_closed_delegation(server, delegation);
944         }
945 }
946
947 /**
948  * nfs_expire_unreferenced_delegations - Eliminate unused delegations
949  * @clp: nfs_client to process
950  *
951  */
952 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
953 {
954         struct nfs_server *server;
955
956         rcu_read_lock();
957         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
958                 nfs_mark_return_unreferenced_delegations(server);
959         rcu_read_unlock();
960
961         nfs_delegation_run_state_manager(clp);
962 }
963
964 /**
965  * nfs_async_inode_return_delegation - asynchronously return a delegation
966  * @inode: inode to process
967  * @stateid: state ID information
968  *
969  * Returns zero on success, or a negative errno value.
970  */
971 int nfs_async_inode_return_delegation(struct inode *inode,
972                                       const nfs4_stateid *stateid)
973 {
974         struct nfs_server *server = NFS_SERVER(inode);
975         struct nfs_client *clp = server->nfs_client;
976         struct nfs_delegation *delegation;
977
978         rcu_read_lock();
979         delegation = nfs4_get_valid_delegation(inode);
980         if (delegation == NULL)
981                 goto out_enoent;
982         if (stateid != NULL &&
983             !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
984                 goto out_enoent;
985         nfs_mark_return_delegation(server, delegation);
986         rcu_read_unlock();
987
988         nfs_delegation_run_state_manager(clp);
989         return 0;
990 out_enoent:
991         rcu_read_unlock();
992         return -ENOENT;
993 }
994
995 static struct inode *
996 nfs_delegation_find_inode_server(struct nfs_server *server,
997                                  const struct nfs_fh *fhandle)
998 {
999         struct nfs_delegation *delegation;
1000         struct inode *freeme, *res = NULL;
1001
1002         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1003                 spin_lock(&delegation->lock);
1004                 if (delegation->inode != NULL &&
1005                     !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1006                     nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1007                         freeme = igrab(delegation->inode);
1008                         if (freeme && nfs_sb_active(freeme->i_sb))
1009                                 res = freeme;
1010                         spin_unlock(&delegation->lock);
1011                         if (res != NULL)
1012                                 return res;
1013                         if (freeme) {
1014                                 rcu_read_unlock();
1015                                 iput(freeme);
1016                                 rcu_read_lock();
1017                         }
1018                         return ERR_PTR(-EAGAIN);
1019                 }
1020                 spin_unlock(&delegation->lock);
1021         }
1022         return ERR_PTR(-ENOENT);
1023 }
1024
1025 /**
1026  * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1027  * @clp: client state handle
1028  * @fhandle: filehandle from a delegation recall
1029  *
1030  * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1031  * cannot be found.
1032  */
1033 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1034                                         const struct nfs_fh *fhandle)
1035 {
1036         struct nfs_server *server;
1037         struct inode *res;
1038
1039         rcu_read_lock();
1040         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1041                 res = nfs_delegation_find_inode_server(server, fhandle);
1042                 if (res != ERR_PTR(-ENOENT)) {
1043                         rcu_read_unlock();
1044                         return res;
1045                 }
1046         }
1047         rcu_read_unlock();
1048         return ERR_PTR(-ENOENT);
1049 }
1050
1051 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1052 {
1053         struct nfs_delegation *delegation;
1054
1055         list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1056                 /*
1057                  * If the delegation may have been admin revoked, then we
1058                  * cannot reclaim it.
1059                  */
1060                 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1061                         continue;
1062                 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1063         }
1064 }
1065
1066 /**
1067  * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1068  * @clp: nfs_client to process
1069  *
1070  */
1071 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1072 {
1073         struct nfs_server *server;
1074
1075         rcu_read_lock();
1076         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1077                 nfs_delegation_mark_reclaim_server(server);
1078         rcu_read_unlock();
1079 }
1080
1081 /**
1082  * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1083  * @clp: nfs_client to process
1084  *
1085  */
1086 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1087 {
1088         struct nfs_delegation *delegation;
1089         struct nfs_server *server;
1090         struct inode *inode;
1091
1092 restart:
1093         rcu_read_lock();
1094         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1095                 list_for_each_entry_rcu(delegation, &server->delegations,
1096                                                                 super_list) {
1097                         if (test_bit(NFS_DELEGATION_INODE_FREEING,
1098                                                 &delegation->flags) ||
1099                             test_bit(NFS_DELEGATION_RETURNING,
1100                                                 &delegation->flags) ||
1101                             test_bit(NFS_DELEGATION_NEED_RECLAIM,
1102                                                 &delegation->flags) == 0)
1103                                 continue;
1104                         if (!nfs_sb_active(server->super))
1105                                 break; /* continue in outer loop */
1106                         inode = nfs_delegation_grab_inode(delegation);
1107                         if (inode == NULL) {
1108                                 rcu_read_unlock();
1109                                 nfs_sb_deactive(server->super);
1110                                 goto restart;
1111                         }
1112                         delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1113                         rcu_read_unlock();
1114                         if (delegation != NULL) {
1115                                 if (nfs_detach_delegation(NFS_I(inode), delegation,
1116                                                         server) != NULL)
1117                                         nfs_free_delegation(delegation);
1118                                 /* Match nfs_start_delegation_return_locked */
1119                                 nfs_put_delegation(delegation);
1120                         }
1121                         iput(inode);
1122                         nfs_sb_deactive(server->super);
1123                         cond_resched();
1124                         goto restart;
1125                 }
1126         }
1127         rcu_read_unlock();
1128 }
1129
1130 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1131 {
1132         return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1133                                 BIT(NFS4CLNT_LEASE_EXPIRED) |
1134                                 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1135 }
1136
1137 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1138             struct nfs_delegation *delegation)
1139 {
1140         if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1141                 return;
1142         clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1143         set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1144         set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1145 }
1146
1147 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1148                 struct inode *inode)
1149 {
1150         struct nfs_delegation *delegation;
1151
1152         rcu_read_lock();
1153         delegation = rcu_dereference(NFS_I(inode)->delegation);
1154         if (delegation)
1155                 nfs_mark_test_expired_delegation(server, delegation);
1156         rcu_read_unlock();
1157
1158 }
1159
1160 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1161 {
1162         struct nfs_delegation *delegation;
1163
1164         list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1165                 nfs_mark_test_expired_delegation(server, delegation);
1166 }
1167
1168 /**
1169  * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1170  * @clp: nfs_client to process
1171  *
1172  * Iterates through all the delegations associated with this server and
1173  * marks them as needing to be checked for validity.
1174  */
1175 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1176 {
1177         struct nfs_server *server;
1178
1179         rcu_read_lock();
1180         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1181                 nfs_delegation_mark_test_expired_server(server);
1182         rcu_read_unlock();
1183 }
1184
1185 /**
1186  * nfs_test_expired_all_delegations - test all delegations for a client
1187  * @clp: nfs_client to process
1188  *
1189  * Helper for handling "recallable state revoked" status from server.
1190  */
1191 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1192 {
1193         nfs_mark_test_expired_all_delegations(clp);
1194         nfs4_schedule_state_manager(clp);
1195 }
1196
1197 static void
1198 nfs_delegation_test_free_expired(struct inode *inode,
1199                 nfs4_stateid *stateid,
1200                 const struct cred *cred)
1201 {
1202         struct nfs_server *server = NFS_SERVER(inode);
1203         const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1204         int status;
1205
1206         if (!cred)
1207                 return;
1208         status = ops->test_and_free_expired(server, stateid, cred);
1209         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1210                 nfs_remove_bad_delegation(inode, stateid);
1211 }
1212
1213 /**
1214  * nfs_reap_expired_delegations - reap expired delegations
1215  * @clp: nfs_client to process
1216  *
1217  * Iterates through all the delegations associated with this server and
1218  * checks if they have may have been revoked. This function is usually
1219  * expected to be called in cases where the server may have lost its
1220  * lease.
1221  */
1222 void nfs_reap_expired_delegations(struct nfs_client *clp)
1223 {
1224         struct nfs_delegation *delegation;
1225         struct nfs_server *server;
1226         struct inode *inode;
1227         const struct cred *cred;
1228         nfs4_stateid stateid;
1229
1230 restart:
1231         rcu_read_lock();
1232         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1233                 list_for_each_entry_rcu(delegation, &server->delegations,
1234                                                                 super_list) {
1235                         if (test_bit(NFS_DELEGATION_INODE_FREEING,
1236                                                 &delegation->flags) ||
1237                             test_bit(NFS_DELEGATION_RETURNING,
1238                                                 &delegation->flags) ||
1239                             test_bit(NFS_DELEGATION_TEST_EXPIRED,
1240                                                 &delegation->flags) == 0)
1241                                 continue;
1242                         if (!nfs_sb_active(server->super))
1243                                 break; /* continue in outer loop */
1244                         inode = nfs_delegation_grab_inode(delegation);
1245                         if (inode == NULL) {
1246                                 rcu_read_unlock();
1247                                 nfs_sb_deactive(server->super);
1248                                 goto restart;
1249                         }
1250                         cred = get_cred_rcu(delegation->cred);
1251                         nfs4_stateid_copy(&stateid, &delegation->stateid);
1252                         clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1253                         rcu_read_unlock();
1254                         nfs_delegation_test_free_expired(inode, &stateid, cred);
1255                         put_cred(cred);
1256                         if (nfs4_server_rebooted(clp)) {
1257                                 nfs_inode_mark_test_expired_delegation(server,inode);
1258                                 iput(inode);
1259                                 nfs_sb_deactive(server->super);
1260                                 return;
1261                         }
1262                         iput(inode);
1263                         nfs_sb_deactive(server->super);
1264                         cond_resched();
1265                         goto restart;
1266                 }
1267         }
1268         rcu_read_unlock();
1269 }
1270
1271 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1272                 const nfs4_stateid *stateid)
1273 {
1274         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1275         struct nfs_delegation *delegation;
1276         bool found = false;
1277
1278         rcu_read_lock();
1279         delegation = rcu_dereference(NFS_I(inode)->delegation);
1280         if (delegation &&
1281             nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1282             !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1283                 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1284                 found = true;
1285         }
1286         rcu_read_unlock();
1287         if (found)
1288                 nfs4_schedule_state_manager(clp);
1289 }
1290
1291 /**
1292  * nfs_delegations_present - check for existence of delegations
1293  * @clp: client state handle
1294  *
1295  * Returns one if there are any nfs_delegation structures attached
1296  * to this nfs_client.
1297  */
1298 int nfs_delegations_present(struct nfs_client *clp)
1299 {
1300         struct nfs_server *server;
1301         int ret = 0;
1302
1303         rcu_read_lock();
1304         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1305                 if (!list_empty(&server->delegations)) {
1306                         ret = 1;
1307                         break;
1308                 }
1309         rcu_read_unlock();
1310         return ret;
1311 }
1312
1313 /**
1314  * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1315  * @dst: stateid to refresh
1316  * @inode: inode to check
1317  *
1318  * Returns "true" and updates "dst->seqid" * if inode had a delegation
1319  * that matches our delegation stateid. Otherwise "false" is returned.
1320  */
1321 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1322 {
1323         struct nfs_delegation *delegation;
1324         bool ret = false;
1325         if (!inode)
1326                 goto out;
1327
1328         rcu_read_lock();
1329         delegation = rcu_dereference(NFS_I(inode)->delegation);
1330         if (delegation != NULL &&
1331             nfs4_stateid_match_other(dst, &delegation->stateid) &&
1332             nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1333             !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1334                 dst->seqid = delegation->stateid.seqid;
1335                 ret = true;
1336         }
1337         rcu_read_unlock();
1338 out:
1339         return ret;
1340 }
1341
1342 /**
1343  * nfs4_copy_delegation_stateid - Copy inode's state ID information
1344  * @inode: inode to check
1345  * @flags: delegation type requirement
1346  * @dst: stateid data structure to fill in
1347  * @cred: optional argument to retrieve credential
1348  *
1349  * Returns "true" and fills in "dst->data" * if inode had a delegation,
1350  * otherwise "false" is returned.
1351  */
1352 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1353                 nfs4_stateid *dst, const struct cred **cred)
1354 {
1355         struct nfs_inode *nfsi = NFS_I(inode);
1356         struct nfs_delegation *delegation;
1357         bool ret;
1358
1359         flags &= FMODE_READ|FMODE_WRITE;
1360         rcu_read_lock();
1361         delegation = rcu_dereference(nfsi->delegation);
1362         ret = nfs4_is_valid_delegation(delegation, flags);
1363         if (ret) {
1364                 nfs4_stateid_copy(dst, &delegation->stateid);
1365                 nfs_mark_delegation_referenced(delegation);
1366                 if (cred)
1367                         *cred = get_cred(delegation->cred);
1368         }
1369         rcu_read_unlock();
1370         return ret;
1371 }
1372
1373 /**
1374  * nfs4_delegation_flush_on_close - Check if we must flush file on close
1375  * @inode: inode to check
1376  *
1377  * This function checks the number of outstanding writes to the file
1378  * against the delegation 'space_limit' field to see if
1379  * the spec requires us to flush the file on close.
1380  */
1381 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1382 {
1383         struct nfs_inode *nfsi = NFS_I(inode);
1384         struct nfs_delegation *delegation;
1385         bool ret = true;
1386
1387         rcu_read_lock();
1388         delegation = rcu_dereference(nfsi->delegation);
1389         if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1390                 goto out;
1391         if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1392                 ret = false;
1393 out:
1394         rcu_read_unlock();
1395         return ret;
1396 }
1397
1398 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);