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