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