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