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