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