4 * Client-side XDR for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Implementation of the NFSv4 state model. For the time being,
37 * this is minimal, but will be made much more complex in a
41 #include <linux/kernel.h>
42 #include <linux/slab.h>
44 #include <linux/nfs_fs.h>
45 #include <linux/kthread.h>
46 #include <linux/module.h>
47 #include <linux/random.h>
48 #include <linux/ratelimit.h>
49 #include <linux/workqueue.h>
50 #include <linux/bitops.h>
51 #include <linux/jiffies.h>
53 #include <linux/sunrpc/clnt.h>
57 #include "delegation.h"
59 #include "nfs4idmap.h"
60 #include "nfs4session.h"
64 #define NFSDBG_FACILITY NFSDBG_STATE
66 #define OPENOWNER_POOL_SIZE 8
68 const nfs4_stateid zero_stateid
= {
70 .type
= NFS4_SPECIAL_STATEID_TYPE
,
72 static DEFINE_MUTEX(nfs_clid_init_mutex
);
74 int nfs4_init_clientid(struct nfs_client
*clp
, struct rpc_cred
*cred
)
76 struct nfs4_setclientid_res clid
= {
77 .clientid
= clp
->cl_clientid
,
78 .confirm
= clp
->cl_confirm
,
82 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
84 if (test_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
))
86 port
= nn
->nfs_callback_tcpport
;
87 if (clp
->cl_addr
.ss_family
== AF_INET6
)
88 port
= nn
->nfs_callback_tcpport6
;
90 status
= nfs4_proc_setclientid(clp
, NFS4_CALLBACK
, port
, cred
, &clid
);
93 clp
->cl_clientid
= clid
.clientid
;
94 clp
->cl_confirm
= clid
.confirm
;
95 set_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
97 status
= nfs4_proc_setclientid_confirm(clp
, &clid
, cred
);
100 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
101 nfs4_schedule_state_renewal(clp
);
107 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
109 * @clp: nfs_client under test
110 * @result: OUT: found nfs_client, or clp
111 * @cred: credential to use for trunking test
113 * Returns zero, a negative errno, or a negative NFS4ERR status.
114 * If zero is returned, an nfs_client pointer is planted in
117 * Note: The returned client may not yet be marked ready.
119 int nfs40_discover_server_trunking(struct nfs_client
*clp
,
120 struct nfs_client
**result
,
121 struct rpc_cred
*cred
)
123 struct nfs4_setclientid_res clid
= {
124 .clientid
= clp
->cl_clientid
,
125 .confirm
= clp
->cl_confirm
,
127 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
131 port
= nn
->nfs_callback_tcpport
;
132 if (clp
->cl_addr
.ss_family
== AF_INET6
)
133 port
= nn
->nfs_callback_tcpport6
;
135 status
= nfs4_proc_setclientid(clp
, NFS4_CALLBACK
, port
, cred
, &clid
);
138 clp
->cl_clientid
= clid
.clientid
;
139 clp
->cl_confirm
= clid
.confirm
;
141 status
= nfs40_walk_client_list(clp
, result
, cred
);
143 /* Sustain the lease, even if it's empty. If the clientid4
144 * goes stale it's of no use for trunking discovery. */
145 nfs4_schedule_state_renewal(*result
);
147 /* If the client state need to recover, do it. */
149 nfs4_schedule_state_manager(clp
);
155 struct rpc_cred
*nfs4_get_machine_cred_locked(struct nfs_client
*clp
)
157 struct rpc_cred
*cred
= NULL
;
159 if (clp
->cl_machine_cred
!= NULL
)
160 cred
= get_rpccred(clp
->cl_machine_cred
);
164 static void nfs4_root_machine_cred(struct nfs_client
*clp
)
166 struct rpc_cred
*cred
, *new;
168 new = rpc_lookup_machine_cred(NULL
);
169 spin_lock(&clp
->cl_lock
);
170 cred
= clp
->cl_machine_cred
;
171 clp
->cl_machine_cred
= new;
172 spin_unlock(&clp
->cl_lock
);
177 static struct rpc_cred
*
178 nfs4_get_renew_cred_server_locked(struct nfs_server
*server
)
180 struct rpc_cred
*cred
= NULL
;
181 struct nfs4_state_owner
*sp
;
184 for (pos
= rb_first(&server
->state_owners
);
186 pos
= rb_next(pos
)) {
187 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
188 if (list_empty(&sp
->so_states
))
190 cred
= get_rpccred(sp
->so_cred
);
197 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
198 * @clp: client state handle
200 * Returns an rpc_cred with reference count bumped, or NULL.
201 * Caller must hold clp->cl_lock.
203 struct rpc_cred
*nfs4_get_renew_cred_locked(struct nfs_client
*clp
)
205 struct rpc_cred
*cred
= NULL
;
206 struct nfs_server
*server
;
208 /* Use machine credentials if available */
209 cred
= nfs4_get_machine_cred_locked(clp
);
214 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
215 cred
= nfs4_get_renew_cred_server_locked(server
);
225 static void nfs4_end_drain_slot_table(struct nfs4_slot_table
*tbl
)
227 if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
)) {
228 spin_lock(&tbl
->slot_tbl_lock
);
229 nfs41_wake_slot_table(tbl
);
230 spin_unlock(&tbl
->slot_tbl_lock
);
234 static void nfs4_end_drain_session(struct nfs_client
*clp
)
236 struct nfs4_session
*ses
= clp
->cl_session
;
238 if (clp
->cl_slot_tbl
) {
239 nfs4_end_drain_slot_table(clp
->cl_slot_tbl
);
244 nfs4_end_drain_slot_table(&ses
->bc_slot_table
);
245 nfs4_end_drain_slot_table(&ses
->fc_slot_table
);
249 static int nfs4_drain_slot_tbl(struct nfs4_slot_table
*tbl
)
251 set_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
);
252 spin_lock(&tbl
->slot_tbl_lock
);
253 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
) {
254 reinit_completion(&tbl
->complete
);
255 spin_unlock(&tbl
->slot_tbl_lock
);
256 return wait_for_completion_interruptible(&tbl
->complete
);
258 spin_unlock(&tbl
->slot_tbl_lock
);
262 static int nfs4_begin_drain_session(struct nfs_client
*clp
)
264 struct nfs4_session
*ses
= clp
->cl_session
;
267 if (clp
->cl_slot_tbl
)
268 return nfs4_drain_slot_tbl(clp
->cl_slot_tbl
);
271 ret
= nfs4_drain_slot_tbl(&ses
->bc_slot_table
);
275 return nfs4_drain_slot_tbl(&ses
->fc_slot_table
);
278 #if defined(CONFIG_NFS_V4_1)
280 static int nfs41_setup_state_renewal(struct nfs_client
*clp
)
283 struct nfs_fsinfo fsinfo
;
286 if (!test_bit(NFS_CS_CHECK_LEASE_TIME
, &clp
->cl_res_state
)) {
287 nfs4_schedule_state_renewal(clp
);
292 status
= nfs4_proc_get_lease_time(clp
, &fsinfo
);
294 nfs4_set_lease_period(clp
, fsinfo
.lease_time
* HZ
, now
);
295 nfs4_schedule_state_renewal(clp
);
301 static void nfs41_finish_session_reset(struct nfs_client
*clp
)
303 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
304 clear_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
305 /* create_session negotiated new slot table */
306 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
307 nfs41_setup_state_renewal(clp
);
310 int nfs41_init_clientid(struct nfs_client
*clp
, struct rpc_cred
*cred
)
314 if (test_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
))
316 status
= nfs4_proc_exchange_id(clp
, cred
);
319 set_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
321 status
= nfs4_proc_create_session(clp
, cred
);
324 nfs41_finish_session_reset(clp
);
325 nfs_mark_client_ready(clp
, NFS_CS_READY
);
331 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
333 * @clp: nfs_client under test
334 * @result: OUT: found nfs_client, or clp
335 * @cred: credential to use for trunking test
337 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
338 * If NFS4_OK is returned, an nfs_client pointer is planted in
341 * Note: The returned client may not yet be marked ready.
343 int nfs41_discover_server_trunking(struct nfs_client
*clp
,
344 struct nfs_client
**result
,
345 struct rpc_cred
*cred
)
349 status
= nfs4_proc_exchange_id(clp
, cred
);
350 if (status
!= NFS4_OK
)
353 status
= nfs41_walk_client_list(clp
, result
, cred
);
360 * Purge state if the client id was established in a prior
361 * instance and the client id could not have arrived on the
362 * server via Transparent State Migration.
364 if (clp
->cl_exchange_flags
& EXCHGID4_FLAG_CONFIRMED_R
) {
365 if (!test_bit(NFS_CS_TSM_POSSIBLE
, &clp
->cl_flags
))
366 set_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
);
368 set_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
370 nfs4_schedule_state_manager(clp
);
371 status
= nfs_wait_client_init_complete(clp
);
377 #endif /* CONFIG_NFS_V4_1 */
380 * nfs4_get_clid_cred - Acquire credential for a setclientid operation
381 * @clp: client state handle
383 * Returns an rpc_cred with reference count bumped, or NULL.
385 struct rpc_cred
*nfs4_get_clid_cred(struct nfs_client
*clp
)
387 struct rpc_cred
*cred
;
389 spin_lock(&clp
->cl_lock
);
390 cred
= nfs4_get_machine_cred_locked(clp
);
391 spin_unlock(&clp
->cl_lock
);
395 static struct nfs4_state_owner
*
396 nfs4_find_state_owner_locked(struct nfs_server
*server
, struct rpc_cred
*cred
)
398 struct rb_node
**p
= &server
->state_owners
.rb_node
,
400 struct nfs4_state_owner
*sp
;
404 sp
= rb_entry(parent
, struct nfs4_state_owner
, so_server_node
);
406 if (cred
< sp
->so_cred
)
407 p
= &parent
->rb_left
;
408 else if (cred
> sp
->so_cred
)
409 p
= &parent
->rb_right
;
411 if (!list_empty(&sp
->so_lru
))
412 list_del_init(&sp
->so_lru
);
413 atomic_inc(&sp
->so_count
);
420 static struct nfs4_state_owner
*
421 nfs4_insert_state_owner_locked(struct nfs4_state_owner
*new)
423 struct nfs_server
*server
= new->so_server
;
424 struct rb_node
**p
= &server
->state_owners
.rb_node
,
426 struct nfs4_state_owner
*sp
;
431 sp
= rb_entry(parent
, struct nfs4_state_owner
, so_server_node
);
433 if (new->so_cred
< sp
->so_cred
)
434 p
= &parent
->rb_left
;
435 else if (new->so_cred
> sp
->so_cred
)
436 p
= &parent
->rb_right
;
438 if (!list_empty(&sp
->so_lru
))
439 list_del_init(&sp
->so_lru
);
440 atomic_inc(&sp
->so_count
);
444 err
= ida_get_new(&server
->openowner_id
, &new->so_seqid
.owner_id
);
447 rb_link_node(&new->so_server_node
, parent
, p
);
448 rb_insert_color(&new->so_server_node
, &server
->state_owners
);
453 nfs4_remove_state_owner_locked(struct nfs4_state_owner
*sp
)
455 struct nfs_server
*server
= sp
->so_server
;
457 if (!RB_EMPTY_NODE(&sp
->so_server_node
))
458 rb_erase(&sp
->so_server_node
, &server
->state_owners
);
459 ida_remove(&server
->openowner_id
, sp
->so_seqid
.owner_id
);
463 nfs4_init_seqid_counter(struct nfs_seqid_counter
*sc
)
465 sc
->create_time
= ktime_get();
468 spin_lock_init(&sc
->lock
);
469 INIT_LIST_HEAD(&sc
->list
);
470 rpc_init_wait_queue(&sc
->wait
, "Seqid_waitqueue");
474 nfs4_destroy_seqid_counter(struct nfs_seqid_counter
*sc
)
476 rpc_destroy_wait_queue(&sc
->wait
);
480 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
481 * create a new state_owner.
484 static struct nfs4_state_owner
*
485 nfs4_alloc_state_owner(struct nfs_server
*server
,
486 struct rpc_cred
*cred
,
489 struct nfs4_state_owner
*sp
;
491 sp
= kzalloc(sizeof(*sp
), gfp_flags
);
494 sp
->so_server
= server
;
495 sp
->so_cred
= get_rpccred(cred
);
496 spin_lock_init(&sp
->so_lock
);
497 INIT_LIST_HEAD(&sp
->so_states
);
498 nfs4_init_seqid_counter(&sp
->so_seqid
);
499 atomic_set(&sp
->so_count
, 1);
500 INIT_LIST_HEAD(&sp
->so_lru
);
501 seqcount_init(&sp
->so_reclaim_seqcount
);
502 mutex_init(&sp
->so_delegreturn_mutex
);
507 nfs4_reset_state_owner(struct nfs4_state_owner
*sp
)
509 /* This state_owner is no longer usable, but must
510 * remain in place so that state recovery can find it
511 * and the opens associated with it.
512 * It may also be used for new 'open' request to
513 * return a delegation to the server.
514 * So update the 'create_time' so that it looks like
515 * a new state_owner. This will cause the server to
516 * request an OPEN_CONFIRM to start a new sequence.
518 sp
->so_seqid
.create_time
= ktime_get();
521 static void nfs4_free_state_owner(struct nfs4_state_owner
*sp
)
523 nfs4_destroy_seqid_counter(&sp
->so_seqid
);
524 put_rpccred(sp
->so_cred
);
528 static void nfs4_gc_state_owners(struct nfs_server
*server
)
530 struct nfs_client
*clp
= server
->nfs_client
;
531 struct nfs4_state_owner
*sp
, *tmp
;
532 unsigned long time_min
, time_max
;
535 spin_lock(&clp
->cl_lock
);
537 time_min
= (long)time_max
- (long)clp
->cl_lease_time
;
538 list_for_each_entry_safe(sp
, tmp
, &server
->state_owners_lru
, so_lru
) {
539 /* NB: LRU is sorted so that oldest is at the head */
540 if (time_in_range(sp
->so_expires
, time_min
, time_max
))
542 list_move(&sp
->so_lru
, &doomed
);
543 nfs4_remove_state_owner_locked(sp
);
545 spin_unlock(&clp
->cl_lock
);
547 list_for_each_entry_safe(sp
, tmp
, &doomed
, so_lru
) {
548 list_del(&sp
->so_lru
);
549 nfs4_free_state_owner(sp
);
554 * nfs4_get_state_owner - Look up a state owner given a credential
555 * @server: nfs_server to search
556 * @cred: RPC credential to match
558 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
560 struct nfs4_state_owner
*nfs4_get_state_owner(struct nfs_server
*server
,
561 struct rpc_cred
*cred
,
564 struct nfs_client
*clp
= server
->nfs_client
;
565 struct nfs4_state_owner
*sp
, *new;
567 spin_lock(&clp
->cl_lock
);
568 sp
= nfs4_find_state_owner_locked(server
, cred
);
569 spin_unlock(&clp
->cl_lock
);
572 new = nfs4_alloc_state_owner(server
, cred
, gfp_flags
);
576 if (ida_pre_get(&server
->openowner_id
, gfp_flags
) == 0)
578 spin_lock(&clp
->cl_lock
);
579 sp
= nfs4_insert_state_owner_locked(new);
580 spin_unlock(&clp
->cl_lock
);
581 } while (sp
== ERR_PTR(-EAGAIN
));
583 nfs4_free_state_owner(new);
585 nfs4_gc_state_owners(server
);
590 * nfs4_put_state_owner - Release a nfs4_state_owner
591 * @sp: state owner data to release
593 * Note that we keep released state owners on an LRU
595 * This caches valid state owners so that they can be
596 * reused, to avoid the OPEN_CONFIRM on minor version 0.
597 * It also pins the uniquifier of dropped state owners for
598 * a while, to ensure that those state owner names are
601 void nfs4_put_state_owner(struct nfs4_state_owner
*sp
)
603 struct nfs_server
*server
= sp
->so_server
;
604 struct nfs_client
*clp
= server
->nfs_client
;
606 if (!atomic_dec_and_lock(&sp
->so_count
, &clp
->cl_lock
))
609 sp
->so_expires
= jiffies
;
610 list_add_tail(&sp
->so_lru
, &server
->state_owners_lru
);
611 spin_unlock(&clp
->cl_lock
);
615 * nfs4_purge_state_owners - Release all cached state owners
616 * @server: nfs_server with cached state owners to release
617 * @head: resulting list of state owners
619 * Called at umount time. Remaining state owners will be on
620 * the LRU with ref count of zero.
621 * Note that the state owners are not freed, but are added
622 * to the list @head, which can later be used as an argument
623 * to nfs4_free_state_owners.
625 void nfs4_purge_state_owners(struct nfs_server
*server
, struct list_head
*head
)
627 struct nfs_client
*clp
= server
->nfs_client
;
628 struct nfs4_state_owner
*sp
, *tmp
;
630 spin_lock(&clp
->cl_lock
);
631 list_for_each_entry_safe(sp
, tmp
, &server
->state_owners_lru
, so_lru
) {
632 list_move(&sp
->so_lru
, head
);
633 nfs4_remove_state_owner_locked(sp
);
635 spin_unlock(&clp
->cl_lock
);
639 * nfs4_purge_state_owners - Release all cached state owners
640 * @head: resulting list of state owners
642 * Frees a list of state owners that was generated by
643 * nfs4_purge_state_owners
645 void nfs4_free_state_owners(struct list_head
*head
)
647 struct nfs4_state_owner
*sp
, *tmp
;
649 list_for_each_entry_safe(sp
, tmp
, head
, so_lru
) {
650 list_del(&sp
->so_lru
);
651 nfs4_free_state_owner(sp
);
655 static struct nfs4_state
*
656 nfs4_alloc_open_state(void)
658 struct nfs4_state
*state
;
660 state
= kzalloc(sizeof(*state
), GFP_NOFS
);
663 atomic_set(&state
->count
, 1);
664 INIT_LIST_HEAD(&state
->lock_states
);
665 spin_lock_init(&state
->state_lock
);
666 seqlock_init(&state
->seqlock
);
671 nfs4_state_set_mode_locked(struct nfs4_state
*state
, fmode_t fmode
)
673 if (state
->state
== fmode
)
675 /* NB! List reordering - see the reclaim code for why. */
676 if ((fmode
& FMODE_WRITE
) != (state
->state
& FMODE_WRITE
)) {
677 if (fmode
& FMODE_WRITE
)
678 list_move(&state
->open_states
, &state
->owner
->so_states
);
680 list_move_tail(&state
->open_states
, &state
->owner
->so_states
);
682 state
->state
= fmode
;
685 static struct nfs4_state
*
686 __nfs4_find_state_byowner(struct inode
*inode
, struct nfs4_state_owner
*owner
)
688 struct nfs_inode
*nfsi
= NFS_I(inode
);
689 struct nfs4_state
*state
;
691 list_for_each_entry(state
, &nfsi
->open_states
, inode_states
) {
692 if (state
->owner
!= owner
)
694 if (!nfs4_valid_open_stateid(state
))
696 if (atomic_inc_not_zero(&state
->count
))
703 nfs4_free_open_state(struct nfs4_state
*state
)
709 nfs4_get_open_state(struct inode
*inode
, struct nfs4_state_owner
*owner
)
711 struct nfs4_state
*state
, *new;
712 struct nfs_inode
*nfsi
= NFS_I(inode
);
714 spin_lock(&inode
->i_lock
);
715 state
= __nfs4_find_state_byowner(inode
, owner
);
716 spin_unlock(&inode
->i_lock
);
719 new = nfs4_alloc_open_state();
720 spin_lock(&owner
->so_lock
);
721 spin_lock(&inode
->i_lock
);
722 state
= __nfs4_find_state_byowner(inode
, owner
);
723 if (state
== NULL
&& new != NULL
) {
725 state
->owner
= owner
;
726 atomic_inc(&owner
->so_count
);
727 list_add(&state
->inode_states
, &nfsi
->open_states
);
729 state
->inode
= inode
;
730 spin_unlock(&inode
->i_lock
);
731 /* Note: The reclaim code dictates that we add stateless
732 * and read-only stateids to the end of the list */
733 list_add_tail(&state
->open_states
, &owner
->so_states
);
734 spin_unlock(&owner
->so_lock
);
736 spin_unlock(&inode
->i_lock
);
737 spin_unlock(&owner
->so_lock
);
739 nfs4_free_open_state(new);
745 void nfs4_put_open_state(struct nfs4_state
*state
)
747 struct inode
*inode
= state
->inode
;
748 struct nfs4_state_owner
*owner
= state
->owner
;
750 if (!atomic_dec_and_lock(&state
->count
, &owner
->so_lock
))
752 spin_lock(&inode
->i_lock
);
753 list_del(&state
->inode_states
);
754 list_del(&state
->open_states
);
755 spin_unlock(&inode
->i_lock
);
756 spin_unlock(&owner
->so_lock
);
758 nfs4_free_open_state(state
);
759 nfs4_put_state_owner(owner
);
763 * Close the current file.
765 static void __nfs4_close(struct nfs4_state
*state
,
766 fmode_t fmode
, gfp_t gfp_mask
, int wait
)
768 struct nfs4_state_owner
*owner
= state
->owner
;
772 atomic_inc(&owner
->so_count
);
773 /* Protect against nfs4_find_state() */
774 spin_lock(&owner
->so_lock
);
775 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
782 case FMODE_READ
|FMODE_WRITE
:
785 newstate
= FMODE_READ
|FMODE_WRITE
;
786 if (state
->n_rdwr
== 0) {
787 if (state
->n_rdonly
== 0) {
788 newstate
&= ~FMODE_READ
;
789 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
790 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
792 if (state
->n_wronly
== 0) {
793 newstate
&= ~FMODE_WRITE
;
794 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
795 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
798 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
800 nfs4_state_set_mode_locked(state
, newstate
);
801 spin_unlock(&owner
->so_lock
);
804 nfs4_put_open_state(state
);
805 nfs4_put_state_owner(owner
);
807 nfs4_do_close(state
, gfp_mask
, wait
);
810 void nfs4_close_state(struct nfs4_state
*state
, fmode_t fmode
)
812 __nfs4_close(state
, fmode
, GFP_NOFS
, 0);
815 void nfs4_close_sync(struct nfs4_state
*state
, fmode_t fmode
)
817 __nfs4_close(state
, fmode
, GFP_KERNEL
, 1);
821 * Search the state->lock_states for an existing lock_owner
822 * that is compatible with either of the given owners.
823 * If the second is non-zero, then the first refers to a Posix-lock
824 * owner (current->files) and the second refers to a flock/OFD
825 * owner (struct file*). In that case, prefer a match for the first
827 * If both sorts of locks are held on the one file we cannot know
828 * which stateid was intended to be used, so a "correct" choice cannot
829 * be made. Failing that, a "consistent" choice is preferable. The
830 * consistent choice we make is to prefer the first owner, that of a
833 static struct nfs4_lock_state
*
834 __nfs4_find_lock_state(struct nfs4_state
*state
,
835 fl_owner_t fl_owner
, fl_owner_t fl_owner2
)
837 struct nfs4_lock_state
*pos
, *ret
= NULL
;
838 list_for_each_entry(pos
, &state
->lock_states
, ls_locks
) {
839 if (pos
->ls_owner
== fl_owner
) {
843 if (pos
->ls_owner
== fl_owner2
)
847 atomic_inc(&ret
->ls_count
);
852 * Return a compatible lock_state. If no initialized lock_state structure
853 * exists, return an uninitialized one.
856 static struct nfs4_lock_state
*nfs4_alloc_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
)
858 struct nfs4_lock_state
*lsp
;
859 struct nfs_server
*server
= state
->owner
->so_server
;
861 lsp
= kzalloc(sizeof(*lsp
), GFP_NOFS
);
864 nfs4_init_seqid_counter(&lsp
->ls_seqid
);
865 atomic_set(&lsp
->ls_count
, 1);
866 lsp
->ls_state
= state
;
867 lsp
->ls_owner
= fl_owner
;
868 lsp
->ls_seqid
.owner_id
= ida_simple_get(&server
->lockowner_id
, 0, 0, GFP_NOFS
);
869 if (lsp
->ls_seqid
.owner_id
< 0)
871 INIT_LIST_HEAD(&lsp
->ls_locks
);
878 void nfs4_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
880 ida_simple_remove(&server
->lockowner_id
, lsp
->ls_seqid
.owner_id
);
881 nfs4_destroy_seqid_counter(&lsp
->ls_seqid
);
886 * Return a compatible lock_state. If no initialized lock_state structure
887 * exists, return an uninitialized one.
890 static struct nfs4_lock_state
*nfs4_get_lock_state(struct nfs4_state
*state
, fl_owner_t owner
)
892 struct nfs4_lock_state
*lsp
, *new = NULL
;
895 spin_lock(&state
->state_lock
);
896 lsp
= __nfs4_find_lock_state(state
, owner
, NULL
);
900 list_add(&new->ls_locks
, &state
->lock_states
);
901 set_bit(LK_STATE_IN_USE
, &state
->flags
);
906 spin_unlock(&state
->state_lock
);
907 new = nfs4_alloc_lock_state(state
, owner
);
911 spin_unlock(&state
->state_lock
);
913 nfs4_free_lock_state(state
->owner
->so_server
, new);
918 * Release reference to lock_state, and free it if we see that
919 * it is no longer in use
921 void nfs4_put_lock_state(struct nfs4_lock_state
*lsp
)
923 struct nfs_server
*server
;
924 struct nfs4_state
*state
;
928 state
= lsp
->ls_state
;
929 if (!atomic_dec_and_lock(&lsp
->ls_count
, &state
->state_lock
))
931 list_del(&lsp
->ls_locks
);
932 if (list_empty(&state
->lock_states
))
933 clear_bit(LK_STATE_IN_USE
, &state
->flags
);
934 spin_unlock(&state
->state_lock
);
935 server
= state
->owner
->so_server
;
936 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
937 struct nfs_client
*clp
= server
->nfs_client
;
939 clp
->cl_mvops
->free_lock_state(server
, lsp
);
941 nfs4_free_lock_state(server
, lsp
);
944 static void nfs4_fl_copy_lock(struct file_lock
*dst
, struct file_lock
*src
)
946 struct nfs4_lock_state
*lsp
= src
->fl_u
.nfs4_fl
.owner
;
948 dst
->fl_u
.nfs4_fl
.owner
= lsp
;
949 atomic_inc(&lsp
->ls_count
);
952 static void nfs4_fl_release_lock(struct file_lock
*fl
)
954 nfs4_put_lock_state(fl
->fl_u
.nfs4_fl
.owner
);
957 static const struct file_lock_operations nfs4_fl_lock_ops
= {
958 .fl_copy_lock
= nfs4_fl_copy_lock
,
959 .fl_release_private
= nfs4_fl_release_lock
,
962 int nfs4_set_lock_state(struct nfs4_state
*state
, struct file_lock
*fl
)
964 struct nfs4_lock_state
*lsp
;
966 if (fl
->fl_ops
!= NULL
)
968 lsp
= nfs4_get_lock_state(state
, fl
->fl_owner
);
971 fl
->fl_u
.nfs4_fl
.owner
= lsp
;
972 fl
->fl_ops
= &nfs4_fl_lock_ops
;
976 static int nfs4_copy_lock_stateid(nfs4_stateid
*dst
,
977 struct nfs4_state
*state
,
978 const struct nfs_lock_context
*l_ctx
)
980 struct nfs4_lock_state
*lsp
;
981 fl_owner_t fl_owner
, fl_flock_owner
;
987 if (test_bit(LK_STATE_IN_USE
, &state
->flags
) == 0)
990 fl_owner
= l_ctx
->lockowner
;
991 fl_flock_owner
= l_ctx
->open_context
->flock_owner
;
993 spin_lock(&state
->state_lock
);
994 lsp
= __nfs4_find_lock_state(state
, fl_owner
, fl_flock_owner
);
995 if (lsp
&& test_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
))
997 else if (lsp
!= NULL
&& test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0) {
998 nfs4_stateid_copy(dst
, &lsp
->ls_stateid
);
1001 spin_unlock(&state
->state_lock
);
1002 nfs4_put_lock_state(lsp
);
1007 static void nfs4_copy_open_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
)
1009 const nfs4_stateid
*src
;
1013 src
= &zero_stateid
;
1014 seq
= read_seqbegin(&state
->seqlock
);
1015 if (test_bit(NFS_OPEN_STATE
, &state
->flags
))
1016 src
= &state
->open_stateid
;
1017 nfs4_stateid_copy(dst
, src
);
1018 } while (read_seqretry(&state
->seqlock
, seq
));
1022 * Byte-range lock aware utility to initialize the stateid of read/write
1025 int nfs4_select_rw_stateid(struct nfs4_state
*state
,
1026 fmode_t fmode
, const struct nfs_lock_context
*l_ctx
,
1027 nfs4_stateid
*dst
, struct rpc_cred
**cred
)
1031 if (!nfs4_valid_open_stateid(state
))
1035 ret
= nfs4_copy_lock_stateid(dst
, state
, l_ctx
);
1037 /* A lost lock - don't even consider delegations */
1039 /* returns true if delegation stateid found and copied */
1040 if (nfs4_copy_delegation_stateid(state
->inode
, fmode
, dst
, cred
)) {
1045 /* nfs4_copy_delegation_stateid() didn't over-write
1046 * dst, so it still has the lock stateid which we now
1050 nfs4_copy_open_stateid(dst
, state
);
1053 if (nfs_server_capable(state
->inode
, NFS_CAP_STATEID_NFSV41
))
1058 struct nfs_seqid
*nfs_alloc_seqid(struct nfs_seqid_counter
*counter
, gfp_t gfp_mask
)
1060 struct nfs_seqid
*new;
1062 new = kmalloc(sizeof(*new), gfp_mask
);
1064 return ERR_PTR(-ENOMEM
);
1065 new->sequence
= counter
;
1066 INIT_LIST_HEAD(&new->list
);
1071 void nfs_release_seqid(struct nfs_seqid
*seqid
)
1073 struct nfs_seqid_counter
*sequence
;
1075 if (seqid
== NULL
|| list_empty(&seqid
->list
))
1077 sequence
= seqid
->sequence
;
1078 spin_lock(&sequence
->lock
);
1079 list_del_init(&seqid
->list
);
1080 if (!list_empty(&sequence
->list
)) {
1081 struct nfs_seqid
*next
;
1083 next
= list_first_entry(&sequence
->list
,
1084 struct nfs_seqid
, list
);
1085 rpc_wake_up_queued_task(&sequence
->wait
, next
->task
);
1087 spin_unlock(&sequence
->lock
);
1090 void nfs_free_seqid(struct nfs_seqid
*seqid
)
1092 nfs_release_seqid(seqid
);
1097 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1098 * failed with a seqid incrementing error -
1099 * see comments nfs4.h:seqid_mutating_error()
1101 static void nfs_increment_seqid(int status
, struct nfs_seqid
*seqid
)
1106 case -NFS4ERR_BAD_SEQID
:
1107 if (seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)
1109 pr_warn_ratelimited("NFS: v4 server returned a bad"
1110 " sequence-id error on an"
1111 " unconfirmed sequence %p!\n",
1113 case -NFS4ERR_STALE_CLIENTID
:
1114 case -NFS4ERR_STALE_STATEID
:
1115 case -NFS4ERR_BAD_STATEID
:
1116 case -NFS4ERR_BADXDR
:
1117 case -NFS4ERR_RESOURCE
:
1118 case -NFS4ERR_NOFILEHANDLE
:
1119 case -NFS4ERR_MOVED
:
1120 /* Non-seqid mutating errors */
1124 * Note: no locking needed as we are guaranteed to be first
1125 * on the sequence list
1127 seqid
->sequence
->counter
++;
1130 void nfs_increment_open_seqid(int status
, struct nfs_seqid
*seqid
)
1132 struct nfs4_state_owner
*sp
;
1137 sp
= container_of(seqid
->sequence
, struct nfs4_state_owner
, so_seqid
);
1138 if (status
== -NFS4ERR_BAD_SEQID
)
1139 nfs4_reset_state_owner(sp
);
1140 if (!nfs4_has_session(sp
->so_server
->nfs_client
))
1141 nfs_increment_seqid(status
, seqid
);
1145 * Increment the seqid if the LOCK/LOCKU succeeded, or
1146 * failed with a seqid incrementing error -
1147 * see comments nfs4.h:seqid_mutating_error()
1149 void nfs_increment_lock_seqid(int status
, struct nfs_seqid
*seqid
)
1152 nfs_increment_seqid(status
, seqid
);
1155 int nfs_wait_on_sequence(struct nfs_seqid
*seqid
, struct rpc_task
*task
)
1157 struct nfs_seqid_counter
*sequence
;
1162 sequence
= seqid
->sequence
;
1163 spin_lock(&sequence
->lock
);
1165 if (list_empty(&seqid
->list
))
1166 list_add_tail(&seqid
->list
, &sequence
->list
);
1167 if (list_first_entry(&sequence
->list
, struct nfs_seqid
, list
) == seqid
)
1169 rpc_sleep_on(&sequence
->wait
, task
, NULL
);
1172 spin_unlock(&sequence
->lock
);
1177 static int nfs4_run_state_manager(void *);
1179 static void nfs4_clear_state_manager_bit(struct nfs_client
*clp
)
1181 smp_mb__before_atomic();
1182 clear_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
);
1183 smp_mb__after_atomic();
1184 wake_up_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
);
1185 rpc_wake_up(&clp
->cl_rpcwaitq
);
1189 * Schedule the nfs_client asynchronous state management routine
1191 void nfs4_schedule_state_manager(struct nfs_client
*clp
)
1193 struct task_struct
*task
;
1194 char buf
[INET6_ADDRSTRLEN
+ sizeof("-manager") + 1];
1196 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) != 0)
1198 __module_get(THIS_MODULE
);
1199 atomic_inc(&clp
->cl_count
);
1201 /* The rcu_read_lock() is not strictly necessary, as the state
1202 * manager is the only thread that ever changes the rpc_xprt
1203 * after it's initialized. At this point, we're single threaded. */
1205 snprintf(buf
, sizeof(buf
), "%s-manager",
1206 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
));
1208 task
= kthread_run(nfs4_run_state_manager
, clp
, "%s", buf
);
1210 printk(KERN_ERR
"%s: kthread_run: %ld\n",
1211 __func__
, PTR_ERR(task
));
1212 nfs4_clear_state_manager_bit(clp
);
1213 nfs_put_client(clp
);
1214 module_put(THIS_MODULE
);
1219 * Schedule a lease recovery attempt
1221 void nfs4_schedule_lease_recovery(struct nfs_client
*clp
)
1225 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1226 set_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1227 dprintk("%s: scheduling lease recovery for server %s\n", __func__
,
1229 nfs4_schedule_state_manager(clp
);
1231 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery
);
1234 * nfs4_schedule_migration_recovery - trigger migration recovery
1236 * @server: FSID that is migrating
1238 * Returns zero if recovery has started, otherwise a negative NFS4ERR
1239 * value is returned.
1241 int nfs4_schedule_migration_recovery(const struct nfs_server
*server
)
1243 struct nfs_client
*clp
= server
->nfs_client
;
1245 if (server
->fh_expire_type
!= NFS4_FH_PERSISTENT
) {
1246 pr_err("NFS: volatile file handles not supported (server %s)\n",
1251 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
1254 dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1256 (unsigned long long)server
->fsid
.major
,
1257 (unsigned long long)server
->fsid
.minor
,
1260 set_bit(NFS_MIG_IN_TRANSITION
,
1261 &((struct nfs_server
*)server
)->mig_status
);
1262 set_bit(NFS4CLNT_MOVED
, &clp
->cl_state
);
1264 nfs4_schedule_state_manager(clp
);
1267 EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery
);
1270 * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1272 * @clp: server to check for moved leases
1275 void nfs4_schedule_lease_moved_recovery(struct nfs_client
*clp
)
1277 dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1278 __func__
, clp
->cl_clientid
, clp
->cl_hostname
);
1280 set_bit(NFS4CLNT_LEASE_MOVED
, &clp
->cl_state
);
1281 nfs4_schedule_state_manager(clp
);
1283 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery
);
1285 int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
1291 atomic_inc(&clp
->cl_count
);
1292 res
= wait_on_bit_action(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
1293 nfs_wait_bit_killable
, TASK_KILLABLE
);
1296 if (clp
->cl_cons_state
< 0)
1297 res
= clp
->cl_cons_state
;
1299 nfs_put_client(clp
);
1303 int nfs4_client_recover_expired_lease(struct nfs_client
*clp
)
1308 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
1309 ret
= nfs4_wait_clnt_recover(clp
);
1312 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1313 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1315 nfs4_schedule_state_manager(clp
);
1322 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1323 * @clp: client to process
1325 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1326 * resend of the SETCLIENTID and hence re-establish the
1327 * callback channel. Then return all existing delegations.
1329 static void nfs40_handle_cb_pathdown(struct nfs_client
*clp
)
1331 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1332 nfs_expire_all_delegations(clp
);
1333 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__
,
1337 void nfs4_schedule_path_down_recovery(struct nfs_client
*clp
)
1339 nfs40_handle_cb_pathdown(clp
);
1340 nfs4_schedule_state_manager(clp
);
1343 static int nfs4_state_mark_reclaim_reboot(struct nfs_client
*clp
, struct nfs4_state
*state
)
1346 if (!nfs4_valid_open_stateid(state
))
1348 set_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1349 /* Don't recover state that expired before the reboot */
1350 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
)) {
1351 clear_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1354 set_bit(NFS_OWNER_RECLAIM_REBOOT
, &state
->owner
->so_flags
);
1355 set_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
);
1359 int nfs4_state_mark_reclaim_nograce(struct nfs_client
*clp
, struct nfs4_state
*state
)
1361 if (!nfs4_valid_open_stateid(state
))
1363 set_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
);
1364 clear_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1365 set_bit(NFS_OWNER_RECLAIM_NOGRACE
, &state
->owner
->so_flags
);
1366 set_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
);
1370 int nfs4_schedule_stateid_recovery(const struct nfs_server
*server
, struct nfs4_state
*state
)
1372 struct nfs_client
*clp
= server
->nfs_client
;
1374 if (!nfs4_state_mark_reclaim_nograce(clp
, state
))
1376 nfs_inode_find_delegation_state_and_recover(state
->inode
,
1378 dprintk("%s: scheduling stateid recovery for server %s\n", __func__
,
1380 nfs4_schedule_state_manager(clp
);
1383 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery
);
1385 static struct nfs4_lock_state
*
1386 nfs_state_find_lock_state_by_stateid(struct nfs4_state
*state
,
1387 const nfs4_stateid
*stateid
)
1389 struct nfs4_lock_state
*pos
;
1391 list_for_each_entry(pos
, &state
->lock_states
, ls_locks
) {
1392 if (!test_bit(NFS_LOCK_INITIALIZED
, &pos
->ls_flags
))
1394 if (nfs4_stateid_match_other(&pos
->ls_stateid
, stateid
))
1400 static bool nfs_state_lock_state_matches_stateid(struct nfs4_state
*state
,
1401 const nfs4_stateid
*stateid
)
1405 if (test_bit(LK_STATE_IN_USE
, &state
->flags
)) {
1406 spin_lock(&state
->state_lock
);
1407 if (nfs_state_find_lock_state_by_stateid(state
, stateid
))
1409 spin_unlock(&state
->state_lock
);
1414 void nfs_inode_find_state_and_recover(struct inode
*inode
,
1415 const nfs4_stateid
*stateid
)
1417 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
1418 struct nfs_inode
*nfsi
= NFS_I(inode
);
1419 struct nfs_open_context
*ctx
;
1420 struct nfs4_state
*state
;
1423 spin_lock(&inode
->i_lock
);
1424 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1428 if (nfs4_stateid_match_other(&state
->stateid
, stateid
) &&
1429 nfs4_state_mark_reclaim_nograce(clp
, state
)) {
1433 if (nfs_state_lock_state_matches_stateid(state
, stateid
) &&
1434 nfs4_state_mark_reclaim_nograce(clp
, state
))
1437 spin_unlock(&inode
->i_lock
);
1439 nfs_inode_find_delegation_state_and_recover(inode
, stateid
);
1441 nfs4_schedule_state_manager(clp
);
1444 static void nfs4_state_mark_open_context_bad(struct nfs4_state
*state
)
1446 struct inode
*inode
= state
->inode
;
1447 struct nfs_inode
*nfsi
= NFS_I(inode
);
1448 struct nfs_open_context
*ctx
;
1450 spin_lock(&inode
->i_lock
);
1451 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1452 if (ctx
->state
!= state
)
1454 set_bit(NFS_CONTEXT_BAD
, &ctx
->flags
);
1456 spin_unlock(&inode
->i_lock
);
1459 static void nfs4_state_mark_recovery_failed(struct nfs4_state
*state
, int error
)
1461 set_bit(NFS_STATE_RECOVERY_FAILED
, &state
->flags
);
1462 nfs4_state_mark_open_context_bad(state
);
1466 static int nfs4_reclaim_locks(struct nfs4_state
*state
, const struct nfs4_state_recovery_ops
*ops
)
1468 struct inode
*inode
= state
->inode
;
1469 struct nfs_inode
*nfsi
= NFS_I(inode
);
1470 struct file_lock
*fl
;
1471 struct nfs4_lock_state
*lsp
;
1473 struct file_lock_context
*flctx
= inode
->i_flctx
;
1474 struct list_head
*list
;
1479 list
= &flctx
->flc_posix
;
1481 /* Guard against delegation returns and new lock/unlock calls */
1482 down_write(&nfsi
->rwsem
);
1483 spin_lock(&flctx
->flc_lock
);
1485 list_for_each_entry(fl
, list
, fl_list
) {
1486 if (nfs_file_open_context(fl
->fl_file
)->state
!= state
)
1488 spin_unlock(&flctx
->flc_lock
);
1489 status
= ops
->recover_lock(state
, fl
);
1494 case -NFS4ERR_ADMIN_REVOKED
:
1495 case -NFS4ERR_STALE_STATEID
:
1496 case -NFS4ERR_BAD_STATEID
:
1497 case -NFS4ERR_EXPIRED
:
1498 case -NFS4ERR_NO_GRACE
:
1499 case -NFS4ERR_STALE_CLIENTID
:
1500 case -NFS4ERR_BADSESSION
:
1501 case -NFS4ERR_BADSLOT
:
1502 case -NFS4ERR_BAD_HIGH_SLOT
:
1503 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1506 pr_err("NFS: %s: unhandled error %d\n",
1509 case -NFS4ERR_DENIED
:
1510 case -NFS4ERR_RECLAIM_BAD
:
1511 case -NFS4ERR_RECLAIM_CONFLICT
:
1512 lsp
= fl
->fl_u
.nfs4_fl
.owner
;
1514 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
1517 spin_lock(&flctx
->flc_lock
);
1519 if (list
== &flctx
->flc_posix
) {
1520 list
= &flctx
->flc_flock
;
1523 spin_unlock(&flctx
->flc_lock
);
1525 up_write(&nfsi
->rwsem
);
1529 static int nfs4_reclaim_open_state(struct nfs4_state_owner
*sp
, const struct nfs4_state_recovery_ops
*ops
)
1531 struct nfs4_state
*state
;
1532 struct nfs4_lock_state
*lock
;
1535 /* Note: we rely on the sp->so_states list being ordered
1536 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1538 * This is needed to ensure that the server won't give us any
1539 * read delegations that we have to return if, say, we are
1540 * recovering after a network partition or a reboot from a
1541 * server that doesn't support a grace period.
1543 spin_lock(&sp
->so_lock
);
1544 raw_write_seqcount_begin(&sp
->so_reclaim_seqcount
);
1546 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1547 if (!test_and_clear_bit(ops
->state_flag_bit
, &state
->flags
))
1549 if (!nfs4_valid_open_stateid(state
))
1551 if (state
->state
== 0)
1553 atomic_inc(&state
->count
);
1554 spin_unlock(&sp
->so_lock
);
1555 status
= ops
->recover_open(sp
, state
);
1557 status
= nfs4_reclaim_locks(state
, ops
);
1559 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
1560 spin_lock(&state
->state_lock
);
1561 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
1562 if (!test_bit(NFS_LOCK_INITIALIZED
, &lock
->ls_flags
))
1563 pr_warn_ratelimited("NFS: "
1565 "failed!\n", __func__
);
1567 spin_unlock(&state
->state_lock
);
1569 clear_bit(NFS_STATE_RECLAIM_NOGRACE
,
1571 nfs4_put_open_state(state
);
1572 spin_lock(&sp
->so_lock
);
1578 printk(KERN_ERR
"NFS: %s: unhandled error %d\n",
1586 /* Open state on this file cannot be recovered */
1587 nfs4_state_mark_recovery_failed(state
, status
);
1591 case -NFS4ERR_ADMIN_REVOKED
:
1592 case -NFS4ERR_STALE_STATEID
:
1593 case -NFS4ERR_OLD_STATEID
:
1594 case -NFS4ERR_BAD_STATEID
:
1595 case -NFS4ERR_RECLAIM_BAD
:
1596 case -NFS4ERR_RECLAIM_CONFLICT
:
1597 nfs4_state_mark_reclaim_nograce(sp
->so_server
->nfs_client
, state
);
1599 case -NFS4ERR_EXPIRED
:
1600 case -NFS4ERR_NO_GRACE
:
1601 nfs4_state_mark_reclaim_nograce(sp
->so_server
->nfs_client
, state
);
1602 case -NFS4ERR_STALE_CLIENTID
:
1603 case -NFS4ERR_BADSESSION
:
1604 case -NFS4ERR_BADSLOT
:
1605 case -NFS4ERR_BAD_HIGH_SLOT
:
1606 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1609 nfs4_put_open_state(state
);
1610 spin_lock(&sp
->so_lock
);
1613 raw_write_seqcount_end(&sp
->so_reclaim_seqcount
);
1614 spin_unlock(&sp
->so_lock
);
1617 nfs4_put_open_state(state
);
1618 spin_lock(&sp
->so_lock
);
1619 raw_write_seqcount_end(&sp
->so_reclaim_seqcount
);
1620 spin_unlock(&sp
->so_lock
);
1624 static void nfs4_clear_open_state(struct nfs4_state
*state
)
1626 struct nfs4_lock_state
*lock
;
1628 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1629 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1630 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1631 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1632 spin_lock(&state
->state_lock
);
1633 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
1634 lock
->ls_seqid
.flags
= 0;
1635 clear_bit(NFS_LOCK_INITIALIZED
, &lock
->ls_flags
);
1637 spin_unlock(&state
->state_lock
);
1640 static void nfs4_reset_seqids(struct nfs_server
*server
,
1641 int (*mark_reclaim
)(struct nfs_client
*clp
, struct nfs4_state
*state
))
1643 struct nfs_client
*clp
= server
->nfs_client
;
1644 struct nfs4_state_owner
*sp
;
1645 struct rb_node
*pos
;
1646 struct nfs4_state
*state
;
1648 spin_lock(&clp
->cl_lock
);
1649 for (pos
= rb_first(&server
->state_owners
);
1651 pos
= rb_next(pos
)) {
1652 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
1653 sp
->so_seqid
.flags
= 0;
1654 spin_lock(&sp
->so_lock
);
1655 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1656 if (mark_reclaim(clp
, state
))
1657 nfs4_clear_open_state(state
);
1659 spin_unlock(&sp
->so_lock
);
1661 spin_unlock(&clp
->cl_lock
);
1664 static void nfs4_state_mark_reclaim_helper(struct nfs_client
*clp
,
1665 int (*mark_reclaim
)(struct nfs_client
*clp
, struct nfs4_state
*state
))
1667 struct nfs_server
*server
;
1670 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1671 nfs4_reset_seqids(server
, mark_reclaim
);
1675 static void nfs4_state_start_reclaim_reboot(struct nfs_client
*clp
)
1677 /* Mark all delegations for reclaim */
1678 nfs_delegation_mark_reclaim(clp
);
1679 nfs4_state_mark_reclaim_helper(clp
, nfs4_state_mark_reclaim_reboot
);
1682 static int nfs4_reclaim_complete(struct nfs_client
*clp
,
1683 const struct nfs4_state_recovery_ops
*ops
,
1684 struct rpc_cred
*cred
)
1686 /* Notify the server we're done reclaiming our state */
1687 if (ops
->reclaim_complete
)
1688 return ops
->reclaim_complete(clp
, cred
);
1692 static void nfs4_clear_reclaim_server(struct nfs_server
*server
)
1694 struct nfs_client
*clp
= server
->nfs_client
;
1695 struct nfs4_state_owner
*sp
;
1696 struct rb_node
*pos
;
1697 struct nfs4_state
*state
;
1699 spin_lock(&clp
->cl_lock
);
1700 for (pos
= rb_first(&server
->state_owners
);
1702 pos
= rb_next(pos
)) {
1703 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
1704 spin_lock(&sp
->so_lock
);
1705 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1706 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT
,
1709 nfs4_state_mark_reclaim_nograce(clp
, state
);
1711 spin_unlock(&sp
->so_lock
);
1713 spin_unlock(&clp
->cl_lock
);
1716 static int nfs4_state_clear_reclaim_reboot(struct nfs_client
*clp
)
1718 struct nfs_server
*server
;
1720 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
))
1724 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1725 nfs4_clear_reclaim_server(server
);
1728 nfs_delegation_reap_unclaimed(clp
);
1732 static void nfs4_state_end_reclaim_reboot(struct nfs_client
*clp
)
1734 const struct nfs4_state_recovery_ops
*ops
;
1735 struct rpc_cred
*cred
;
1738 if (!nfs4_state_clear_reclaim_reboot(clp
))
1740 ops
= clp
->cl_mvops
->reboot_recovery_ops
;
1741 cred
= nfs4_get_clid_cred(clp
);
1742 err
= nfs4_reclaim_complete(clp
, ops
, cred
);
1744 if (err
== -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
)
1745 set_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
);
1748 static void nfs4_state_start_reclaim_nograce(struct nfs_client
*clp
)
1750 nfs_mark_test_expired_all_delegations(clp
);
1751 nfs4_state_mark_reclaim_helper(clp
, nfs4_state_mark_reclaim_nograce
);
1754 static int nfs4_recovery_handle_error(struct nfs_client
*clp
, int error
)
1759 case -NFS4ERR_CB_PATH_DOWN
:
1760 nfs40_handle_cb_pathdown(clp
);
1762 case -NFS4ERR_NO_GRACE
:
1763 nfs4_state_end_reclaim_reboot(clp
);
1765 case -NFS4ERR_STALE_CLIENTID
:
1766 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1767 nfs4_state_start_reclaim_reboot(clp
);
1769 case -NFS4ERR_EXPIRED
:
1770 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1771 nfs4_state_start_reclaim_nograce(clp
);
1773 case -NFS4ERR_BADSESSION
:
1774 case -NFS4ERR_BADSLOT
:
1775 case -NFS4ERR_BAD_HIGH_SLOT
:
1776 case -NFS4ERR_DEADSESSION
:
1777 case -NFS4ERR_SEQ_FALSE_RETRY
:
1778 case -NFS4ERR_SEQ_MISORDERED
:
1779 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
1780 /* Zero session reset errors */
1782 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1783 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
1786 dprintk("%s: failed to handle error %d for server %s\n",
1787 __func__
, error
, clp
->cl_hostname
);
1790 dprintk("%s: handled error %d for server %s\n", __func__
, error
,
1795 static int nfs4_do_reclaim(struct nfs_client
*clp
, const struct nfs4_state_recovery_ops
*ops
)
1797 struct nfs4_state_owner
*sp
;
1798 struct nfs_server
*server
;
1799 struct rb_node
*pos
;
1805 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1806 nfs4_purge_state_owners(server
, &freeme
);
1807 spin_lock(&clp
->cl_lock
);
1808 for (pos
= rb_first(&server
->state_owners
);
1810 pos
= rb_next(pos
)) {
1812 struct nfs4_state_owner
, so_server_node
);
1813 if (!test_and_clear_bit(ops
->owner_flag_bit
,
1816 if (!atomic_inc_not_zero(&sp
->so_count
))
1818 spin_unlock(&clp
->cl_lock
);
1821 status
= nfs4_reclaim_open_state(sp
, ops
);
1823 set_bit(ops
->owner_flag_bit
, &sp
->so_flags
);
1824 nfs4_put_state_owner(sp
);
1825 status
= nfs4_recovery_handle_error(clp
, status
);
1826 return (status
!= 0) ? status
: -EAGAIN
;
1829 nfs4_put_state_owner(sp
);
1832 spin_unlock(&clp
->cl_lock
);
1835 nfs4_free_state_owners(&freeme
);
1839 static int nfs4_check_lease(struct nfs_client
*clp
)
1841 struct rpc_cred
*cred
;
1842 const struct nfs4_state_maintenance_ops
*ops
=
1843 clp
->cl_mvops
->state_renewal_ops
;
1846 /* Is the client already known to have an expired lease? */
1847 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1849 spin_lock(&clp
->cl_lock
);
1850 cred
= ops
->get_state_renewal_cred_locked(clp
);
1851 spin_unlock(&clp
->cl_lock
);
1853 cred
= nfs4_get_clid_cred(clp
);
1858 status
= ops
->renew_lease(clp
, cred
);
1860 if (status
== -ETIMEDOUT
) {
1861 set_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1865 return nfs4_recovery_handle_error(clp
, status
);
1868 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1869 * and for recoverable errors on EXCHANGE_ID for v4.1
1871 static int nfs4_handle_reclaim_lease_error(struct nfs_client
*clp
, int status
)
1874 case -NFS4ERR_SEQ_MISORDERED
:
1875 if (test_and_set_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
))
1876 return -ESERVERFAULT
;
1877 /* Lease confirmation error: retry after purging the lease */
1879 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
1881 case -NFS4ERR_STALE_CLIENTID
:
1882 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
1883 nfs4_state_start_reclaim_reboot(clp
);
1885 case -NFS4ERR_CLID_INUSE
:
1886 pr_err("NFS: Server %s reports our clientid is in use\n",
1888 nfs_mark_client_ready(clp
, -EPERM
);
1889 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
1892 case -NFS4ERR_DELAY
:
1898 case -NFS4ERR_MINOR_VERS_MISMATCH
:
1899 if (clp
->cl_cons_state
== NFS_CS_SESSION_INITING
)
1900 nfs_mark_client_ready(clp
, -EPROTONOSUPPORT
);
1901 dprintk("%s: exit with error %d for server %s\n",
1902 __func__
, -EPROTONOSUPPORT
, clp
->cl_hostname
);
1903 return -EPROTONOSUPPORT
;
1904 case -NFS4ERR_NOT_SAME
: /* FixMe: implement recovery
1905 * in nfs4_exchange_id */
1907 dprintk("%s: exit with error %d for server %s\n", __func__
,
1908 status
, clp
->cl_hostname
);
1911 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1912 dprintk("%s: handled error %d for server %s\n", __func__
, status
,
1917 static int nfs4_establish_lease(struct nfs_client
*clp
)
1919 struct rpc_cred
*cred
;
1920 const struct nfs4_state_recovery_ops
*ops
=
1921 clp
->cl_mvops
->reboot_recovery_ops
;
1924 nfs4_begin_drain_session(clp
);
1925 cred
= nfs4_get_clid_cred(clp
);
1928 status
= ops
->establish_clid(clp
, cred
);
1932 pnfs_destroy_all_layouts(clp
);
1937 * Returns zero or a negative errno. NFS4ERR values are converted
1938 * to local errno values.
1940 static int nfs4_reclaim_lease(struct nfs_client
*clp
)
1944 status
= nfs4_establish_lease(clp
);
1946 return nfs4_handle_reclaim_lease_error(clp
, status
);
1947 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
))
1948 nfs4_state_start_reclaim_nograce(clp
);
1949 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
))
1950 set_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
);
1951 clear_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1952 clear_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1956 static int nfs4_purge_lease(struct nfs_client
*clp
)
1960 status
= nfs4_establish_lease(clp
);
1962 return nfs4_handle_reclaim_lease_error(clp
, status
);
1963 clear_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
);
1964 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1965 nfs4_state_start_reclaim_nograce(clp
);
1970 * Try remote migration of one FSID from a source server to a
1971 * destination server. The source server provides a list of
1972 * potential destinations.
1974 * Returns zero or a negative NFS4ERR status code.
1976 static int nfs4_try_migration(struct nfs_server
*server
, struct rpc_cred
*cred
)
1978 struct nfs_client
*clp
= server
->nfs_client
;
1979 struct nfs4_fs_locations
*locations
= NULL
;
1980 struct inode
*inode
;
1984 dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__
,
1985 (unsigned long long)server
->fsid
.major
,
1986 (unsigned long long)server
->fsid
.minor
,
1990 page
= alloc_page(GFP_KERNEL
);
1991 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
1992 if (page
== NULL
|| locations
== NULL
) {
1993 dprintk("<-- %s: no memory\n", __func__
);
1997 inode
= d_inode(server
->super
->s_root
);
1998 result
= nfs4_proc_get_locations(inode
, locations
, page
, cred
);
2000 dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2005 result
= -NFS4ERR_NXIO
;
2006 if (!(locations
->fattr
.valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)) {
2007 dprintk("<-- %s: No fs_locations data, migration skipped\n",
2012 nfs4_begin_drain_session(clp
);
2014 status
= nfs4_replace_transport(server
, locations
);
2016 dprintk("<-- %s: failed to replace transport: %d\n",
2022 dprintk("<-- %s: migration succeeded\n", __func__
);
2029 pr_err("NFS: migration recovery failed (server %s)\n",
2031 set_bit(NFS_MIG_FAILED
, &server
->mig_status
);
2037 * Returns zero or a negative NFS4ERR status code.
2039 static int nfs4_handle_migration(struct nfs_client
*clp
)
2041 const struct nfs4_state_maintenance_ops
*ops
=
2042 clp
->cl_mvops
->state_renewal_ops
;
2043 struct nfs_server
*server
;
2044 struct rpc_cred
*cred
;
2046 dprintk("%s: migration reported on \"%s\"\n", __func__
,
2049 spin_lock(&clp
->cl_lock
);
2050 cred
= ops
->get_state_renewal_cred_locked(clp
);
2051 spin_unlock(&clp
->cl_lock
);
2053 return -NFS4ERR_NOENT
;
2058 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
2061 if (server
->mig_gen
== clp
->cl_mig_gen
)
2063 server
->mig_gen
= clp
->cl_mig_gen
;
2065 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION
,
2066 &server
->mig_status
))
2070 status
= nfs4_try_migration(server
, cred
);
2083 * Test each nfs_server on the clp's cl_superblocks list to see
2084 * if it's moved to another server. Stop when the server no longer
2085 * returns NFS4ERR_LEASE_MOVED.
2087 static int nfs4_handle_lease_moved(struct nfs_client
*clp
)
2089 const struct nfs4_state_maintenance_ops
*ops
=
2090 clp
->cl_mvops
->state_renewal_ops
;
2091 struct nfs_server
*server
;
2092 struct rpc_cred
*cred
;
2094 dprintk("%s: lease moved reported on \"%s\"\n", __func__
,
2097 spin_lock(&clp
->cl_lock
);
2098 cred
= ops
->get_state_renewal_cred_locked(clp
);
2099 spin_unlock(&clp
->cl_lock
);
2101 return -NFS4ERR_NOENT
;
2106 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
2107 struct inode
*inode
;
2110 if (server
->mig_gen
== clp
->cl_mig_gen
)
2112 server
->mig_gen
= clp
->cl_mig_gen
;
2116 inode
= d_inode(server
->super
->s_root
);
2117 status
= nfs4_proc_fsid_present(inode
, cred
);
2118 if (status
!= -NFS4ERR_MOVED
)
2119 goto restart
; /* wasn't this one */
2120 if (nfs4_try_migration(server
, cred
) == -NFS4ERR_LEASE_MOVED
)
2121 goto restart
; /* there are more */
2132 * nfs4_discover_server_trunking - Detect server IP address trunking
2134 * @clp: nfs_client under test
2135 * @result: OUT: found nfs_client, or clp
2137 * Returns zero or a negative errno. If zero is returned,
2138 * an nfs_client pointer is planted in "result".
2140 * Note: since we are invoked in process context, and
2141 * not from inside the state manager, we cannot use
2142 * nfs4_handle_reclaim_lease_error().
2144 int nfs4_discover_server_trunking(struct nfs_client
*clp
,
2145 struct nfs_client
**result
)
2147 const struct nfs4_state_recovery_ops
*ops
=
2148 clp
->cl_mvops
->reboot_recovery_ops
;
2149 struct rpc_clnt
*clnt
;
2150 struct rpc_cred
*cred
;
2153 dprintk("NFS: %s: testing '%s'\n", __func__
, clp
->cl_hostname
);
2155 clnt
= clp
->cl_rpcclient
;
2158 mutex_lock(&nfs_clid_init_mutex
);
2161 cred
= nfs4_get_clid_cred(clp
);
2165 status
= ops
->detect_trunking(clp
, result
, cred
);
2173 if (clnt
->cl_softrtry
)
2175 case -NFS4ERR_DELAY
:
2178 case -NFS4ERR_STALE_CLIENTID
:
2179 dprintk("NFS: %s after status %d, retrying\n",
2184 nfs4_root_machine_cred(clp
);
2187 if (clnt
->cl_auth
->au_flavor
== RPC_AUTH_UNIX
)
2189 case -NFS4ERR_CLID_INUSE
:
2190 case -NFS4ERR_WRONGSEC
:
2191 /* No point in retrying if we already used RPC_AUTH_UNIX */
2192 if (clnt
->cl_auth
->au_flavor
== RPC_AUTH_UNIX
) {
2196 clnt
= rpc_clone_client_set_auth(clnt
, RPC_AUTH_UNIX
);
2198 status
= PTR_ERR(clnt
);
2201 /* Note: this is safe because we haven't yet marked the
2202 * client as ready, so we are the only user of
2205 clnt
= xchg(&clp
->cl_rpcclient
, clnt
);
2206 rpc_shutdown_client(clnt
);
2207 clnt
= clp
->cl_rpcclient
;
2210 case -NFS4ERR_MINOR_VERS_MISMATCH
:
2211 status
= -EPROTONOSUPPORT
;
2215 case -NFS4ERR_NOT_SAME
: /* FixMe: implement recovery
2216 * in nfs4_exchange_id */
2217 status
= -EKEYEXPIRED
;
2220 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2226 mutex_unlock(&nfs_clid_init_mutex
);
2227 dprintk("NFS: %s: status = %d\n", __func__
, status
);
2231 #ifdef CONFIG_NFS_V4_1
2232 void nfs4_schedule_session_recovery(struct nfs4_session
*session
, int err
)
2234 struct nfs_client
*clp
= session
->clp
;
2238 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
2240 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
2241 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
2243 nfs4_schedule_state_manager(clp
);
2245 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery
);
2247 void nfs41_notify_server(struct nfs_client
*clp
)
2249 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
2250 set_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
2251 nfs4_schedule_state_manager(clp
);
2254 static void nfs4_reset_all_state(struct nfs_client
*clp
)
2256 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0) {
2257 set_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
);
2258 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
2259 nfs4_state_start_reclaim_nograce(clp
);
2260 dprintk("%s: scheduling reset of all state for server %s!\n",
2261 __func__
, clp
->cl_hostname
);
2262 nfs4_schedule_state_manager(clp
);
2266 static void nfs41_handle_server_reboot(struct nfs_client
*clp
)
2268 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0) {
2269 nfs4_state_start_reclaim_reboot(clp
);
2270 dprintk("%s: server %s rebooted!\n", __func__
,
2272 nfs4_schedule_state_manager(clp
);
2276 static void nfs41_handle_all_state_revoked(struct nfs_client
*clp
)
2278 nfs4_reset_all_state(clp
);
2279 dprintk("%s: state revoked on server %s\n", __func__
, clp
->cl_hostname
);
2282 static void nfs41_handle_some_state_revoked(struct nfs_client
*clp
)
2284 nfs4_state_start_reclaim_nograce(clp
);
2285 nfs4_schedule_state_manager(clp
);
2287 dprintk("%s: state revoked on server %s\n", __func__
, clp
->cl_hostname
);
2290 static void nfs41_handle_recallable_state_revoked(struct nfs_client
*clp
)
2292 /* FIXME: For now, we destroy all layouts. */
2293 pnfs_destroy_all_layouts(clp
);
2294 /* FIXME: For now, we test all delegations+open state+locks. */
2295 nfs41_handle_some_state_revoked(clp
);
2296 dprintk("%s: Recallable state revoked on server %s!\n", __func__
,
2300 static void nfs41_handle_backchannel_fault(struct nfs_client
*clp
)
2302 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
2303 nfs4_schedule_state_manager(clp
);
2305 dprintk("%s: server %s declared a backchannel fault\n", __func__
,
2309 static void nfs41_handle_cb_path_down(struct nfs_client
*clp
)
2311 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION
,
2312 &clp
->cl_state
) == 0)
2313 nfs4_schedule_state_manager(clp
);
2316 void nfs41_handle_sequence_flag_errors(struct nfs_client
*clp
, u32 flags
,
2322 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2323 __func__
, clp
->cl_hostname
, clp
->cl_clientid
, flags
);
2325 * If we're called from the state manager thread, then assume we're
2326 * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2327 * Those flags are expected to remain set until we're done
2328 * recovering (see RFC5661, section 18.46.3).
2333 if (flags
& SEQ4_STATUS_RESTART_RECLAIM_NEEDED
)
2334 nfs41_handle_server_reboot(clp
);
2335 if (flags
& (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED
))
2336 nfs41_handle_all_state_revoked(clp
);
2337 if (flags
& (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED
|
2338 SEQ4_STATUS_ADMIN_STATE_REVOKED
))
2339 nfs41_handle_some_state_revoked(clp
);
2340 if (flags
& SEQ4_STATUS_LEASE_MOVED
)
2341 nfs4_schedule_lease_moved_recovery(clp
);
2342 if (flags
& SEQ4_STATUS_RECALLABLE_STATE_REVOKED
)
2343 nfs41_handle_recallable_state_revoked(clp
);
2345 if (flags
& SEQ4_STATUS_BACKCHANNEL_FAULT
)
2346 nfs41_handle_backchannel_fault(clp
);
2347 else if (flags
& (SEQ4_STATUS_CB_PATH_DOWN
|
2348 SEQ4_STATUS_CB_PATH_DOWN_SESSION
))
2349 nfs41_handle_cb_path_down(clp
);
2352 static int nfs4_reset_session(struct nfs_client
*clp
)
2354 struct rpc_cred
*cred
;
2357 if (!nfs4_has_session(clp
))
2359 nfs4_begin_drain_session(clp
);
2360 cred
= nfs4_get_clid_cred(clp
);
2361 status
= nfs4_proc_destroy_session(clp
->cl_session
, cred
);
2364 case -NFS4ERR_BADSESSION
:
2365 case -NFS4ERR_DEADSESSION
:
2367 case -NFS4ERR_BACK_CHAN_BUSY
:
2368 case -NFS4ERR_DELAY
:
2369 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
2374 status
= nfs4_recovery_handle_error(clp
, status
);
2378 memset(clp
->cl_session
->sess_id
.data
, 0, NFS4_MAX_SESSIONID_LEN
);
2379 status
= nfs4_proc_create_session(clp
, cred
);
2381 dprintk("%s: session reset failed with status %d for server %s!\n",
2382 __func__
, status
, clp
->cl_hostname
);
2383 status
= nfs4_handle_reclaim_lease_error(clp
, status
);
2386 nfs41_finish_session_reset(clp
);
2387 dprintk("%s: session reset was successful for server %s!\n",
2388 __func__
, clp
->cl_hostname
);
2395 static int nfs4_bind_conn_to_session(struct nfs_client
*clp
)
2397 struct rpc_cred
*cred
;
2400 if (!nfs4_has_session(clp
))
2402 nfs4_begin_drain_session(clp
);
2403 cred
= nfs4_get_clid_cred(clp
);
2404 ret
= nfs4_proc_bind_conn_to_session(clp
, cred
);
2407 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
2410 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2411 __func__
, clp
->cl_hostname
);
2413 case -NFS4ERR_DELAY
:
2415 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
2418 return nfs4_recovery_handle_error(clp
, ret
);
2422 #else /* CONFIG_NFS_V4_1 */
2423 static int nfs4_reset_session(struct nfs_client
*clp
) { return 0; }
2425 static int nfs4_bind_conn_to_session(struct nfs_client
*clp
)
2429 #endif /* CONFIG_NFS_V4_1 */
2431 static void nfs4_state_manager(struct nfs_client
*clp
)
2434 const char *section
= "", *section_sep
= "";
2436 /* Ensure exclusive access to NFSv4 state */
2438 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
2439 section
= "purge state";
2440 status
= nfs4_purge_lease(clp
);
2446 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
)) {
2447 section
= "lease expired";
2448 /* We're going to have to re-establish a clientid */
2449 status
= nfs4_reclaim_lease(clp
);
2455 /* Initialize or reset the session */
2456 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
)) {
2457 section
= "reset session";
2458 status
= nfs4_reset_session(clp
);
2459 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
2465 /* Send BIND_CONN_TO_SESSION */
2466 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION
,
2468 section
= "bind conn to session";
2469 status
= nfs4_bind_conn_to_session(clp
);
2475 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
)) {
2476 section
= "check lease";
2477 status
= nfs4_check_lease(clp
);
2483 if (test_and_clear_bit(NFS4CLNT_MOVED
, &clp
->cl_state
)) {
2484 section
= "migration";
2485 status
= nfs4_handle_migration(clp
);
2490 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED
, &clp
->cl_state
)) {
2491 section
= "lease moved";
2492 status
= nfs4_handle_lease_moved(clp
);
2497 /* First recover reboot state... */
2498 if (test_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
)) {
2499 section
= "reclaim reboot";
2500 status
= nfs4_do_reclaim(clp
,
2501 clp
->cl_mvops
->reboot_recovery_ops
);
2502 if (status
== -EAGAIN
)
2506 nfs4_state_end_reclaim_reboot(clp
);
2509 /* Detect expired delegations... */
2510 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED
, &clp
->cl_state
)) {
2511 section
= "detect expired delegations";
2512 nfs_reap_expired_delegations(clp
);
2516 /* Now recover expired state... */
2517 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
)) {
2518 section
= "reclaim nograce";
2519 status
= nfs4_do_reclaim(clp
,
2520 clp
->cl_mvops
->nograce_recovery_ops
);
2521 if (status
== -EAGAIN
)
2527 nfs4_end_drain_session(clp
);
2528 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
)) {
2529 nfs_client_return_marked_delegations(clp
);
2533 nfs4_clear_state_manager_bit(clp
);
2534 /* Did we race with an attempt to give us more work? */
2535 if (clp
->cl_state
== 0)
2537 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) != 0)
2539 } while (atomic_read(&clp
->cl_count
) > 1);
2542 if (strlen(section
))
2544 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2545 " with error %d\n", section_sep
, section
,
2546 clp
->cl_hostname
, -status
);
2548 nfs4_end_drain_session(clp
);
2549 nfs4_clear_state_manager_bit(clp
);
2552 static int nfs4_run_state_manager(void *ptr
)
2554 struct nfs_client
*clp
= ptr
;
2556 allow_signal(SIGKILL
);
2557 nfs4_state_manager(clp
);
2558 nfs_put_client(clp
);
2559 module_put_and_exit(0);