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