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