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>
52 #include <linux/sched/mm.h>
54 #include <linux/sunrpc/clnt.h>
58 #include "delegation.h"
60 #include "nfs4idmap.h"
61 #include "nfs4session.h"
65 #define NFSDBG_FACILITY NFSDBG_STATE
67 #define OPENOWNER_POOL_SIZE 8
69 const nfs4_stateid zero_stateid = {
71 .type = NFS4_SPECIAL_STATEID_TYPE,
73 const nfs4_stateid invalid_stateid = {
75 /* Funky initialiser keeps older gcc versions happy */
76 .data = { 0xff, 0xff, 0xff, 0xff, 0 },
78 .type = NFS4_INVALID_STATEID_TYPE,
81 const nfs4_stateid current_stateid = {
83 /* Funky initialiser keeps older gcc versions happy */
84 .data = { 0x0, 0x0, 0x0, 0x1, 0 },
86 .type = NFS4_SPECIAL_STATEID_TYPE,
89 static DEFINE_MUTEX(nfs_clid_init_mutex);
91 int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
93 struct nfs4_setclientid_res clid = {
94 .clientid = clp->cl_clientid,
95 .confirm = clp->cl_confirm,
99 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
101 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
103 port = nn->nfs_callback_tcpport;
104 if (clp->cl_addr.ss_family == AF_INET6)
105 port = nn->nfs_callback_tcpport6;
107 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
110 clp->cl_clientid = clid.clientid;
111 clp->cl_confirm = clid.confirm;
112 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
114 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
117 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
118 nfs4_schedule_state_renewal(clp);
124 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
126 * @clp: nfs_client under test
127 * @result: OUT: found nfs_client, or clp
128 * @cred: credential to use for trunking test
130 * Returns zero, a negative errno, or a negative NFS4ERR status.
131 * If zero is returned, an nfs_client pointer is planted in
134 * Note: The returned client may not yet be marked ready.
136 int nfs40_discover_server_trunking(struct nfs_client *clp,
137 struct nfs_client **result,
138 struct rpc_cred *cred)
140 struct nfs4_setclientid_res clid = {
141 .clientid = clp->cl_clientid,
142 .confirm = clp->cl_confirm,
144 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
148 port = nn->nfs_callback_tcpport;
149 if (clp->cl_addr.ss_family == AF_INET6)
150 port = nn->nfs_callback_tcpport6;
152 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
155 clp->cl_clientid = clid.clientid;
156 clp->cl_confirm = clid.confirm;
158 status = nfs40_walk_client_list(clp, result, cred);
160 /* Sustain the lease, even if it's empty. If the clientid4
161 * goes stale it's of no use for trunking discovery. */
162 nfs4_schedule_state_renewal(*result);
164 /* If the client state need to recover, do it. */
166 nfs4_schedule_state_manager(clp);
172 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
174 struct rpc_cred *cred = NULL;
176 if (clp->cl_machine_cred != NULL)
177 cred = get_rpccred(clp->cl_machine_cred);
181 static void nfs4_root_machine_cred(struct nfs_client *clp)
183 struct rpc_cred *cred, *new;
185 new = rpc_lookup_machine_cred(NULL);
186 spin_lock(&clp->cl_lock);
187 cred = clp->cl_machine_cred;
188 clp->cl_machine_cred = new;
189 spin_unlock(&clp->cl_lock);
194 static struct rpc_cred *
195 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
197 struct rpc_cred *cred = NULL;
198 struct nfs4_state_owner *sp;
201 for (pos = rb_first(&server->state_owners);
203 pos = rb_next(pos)) {
204 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
205 if (list_empty(&sp->so_states))
207 cred = get_rpccred(sp->so_cred);
214 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
215 * @clp: client state handle
217 * Returns an rpc_cred with reference count bumped, or NULL.
218 * Caller must hold clp->cl_lock.
220 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
222 struct rpc_cred *cred = NULL;
223 struct nfs_server *server;
225 /* Use machine credentials if available */
226 cred = nfs4_get_machine_cred_locked(clp);
231 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
232 cred = nfs4_get_renew_cred_server_locked(server);
242 static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
244 if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
245 spin_lock(&tbl->slot_tbl_lock);
246 nfs41_wake_slot_table(tbl);
247 spin_unlock(&tbl->slot_tbl_lock);
251 static void nfs4_end_drain_session(struct nfs_client *clp)
253 struct nfs4_session *ses = clp->cl_session;
255 if (clp->cl_slot_tbl) {
256 nfs4_end_drain_slot_table(clp->cl_slot_tbl);
261 nfs4_end_drain_slot_table(&ses->bc_slot_table);
262 nfs4_end_drain_slot_table(&ses->fc_slot_table);
266 static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
268 set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
269 spin_lock(&tbl->slot_tbl_lock);
270 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
271 reinit_completion(&tbl->complete);
272 spin_unlock(&tbl->slot_tbl_lock);
273 return wait_for_completion_interruptible(&tbl->complete);
275 spin_unlock(&tbl->slot_tbl_lock);
279 static int nfs4_begin_drain_session(struct nfs_client *clp)
281 struct nfs4_session *ses = clp->cl_session;
284 if (clp->cl_slot_tbl)
285 return nfs4_drain_slot_tbl(clp->cl_slot_tbl);
288 ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
292 return nfs4_drain_slot_tbl(&ses->fc_slot_table);
295 #if defined(CONFIG_NFS_V4_1)
297 static int nfs41_setup_state_renewal(struct nfs_client *clp)
300 struct nfs_fsinfo fsinfo;
303 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
304 nfs4_schedule_state_renewal(clp);
309 status = nfs4_proc_get_lease_time(clp, &fsinfo);
311 nfs4_set_lease_period(clp, fsinfo.lease_time * HZ, now);
312 nfs4_schedule_state_renewal(clp);
318 static void nfs41_finish_session_reset(struct nfs_client *clp)
320 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
321 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
322 /* create_session negotiated new slot table */
323 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
324 nfs41_setup_state_renewal(clp);
327 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
331 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
333 status = nfs4_proc_exchange_id(clp, cred);
336 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
338 status = nfs4_proc_create_session(clp, cred);
341 nfs41_finish_session_reset(clp);
342 nfs_mark_client_ready(clp, NFS_CS_READY);
348 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
350 * @clp: nfs_client under test
351 * @result: OUT: found nfs_client, or clp
352 * @cred: credential to use for trunking test
354 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
355 * If NFS4_OK is returned, an nfs_client pointer is planted in
358 * Note: The returned client may not yet be marked ready.
360 int nfs41_discover_server_trunking(struct nfs_client *clp,
361 struct nfs_client **result,
362 struct rpc_cred *cred)
366 status = nfs4_proc_exchange_id(clp, cred);
367 if (status != NFS4_OK)
370 status = nfs41_walk_client_list(clp, result, cred);
377 * Purge state if the client id was established in a prior
378 * instance and the client id could not have arrived on the
379 * server via Transparent State Migration.
381 if (clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R) {
382 if (!test_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags))
383 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
385 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
387 nfs4_schedule_state_manager(clp);
388 status = nfs_wait_client_init_complete(clp);
394 #endif /* CONFIG_NFS_V4_1 */
397 * nfs4_get_clid_cred - Acquire credential for a setclientid operation
398 * @clp: client state handle
400 * Returns an rpc_cred with reference count bumped, or NULL.
402 struct rpc_cred *nfs4_get_clid_cred(struct nfs_client *clp)
404 struct rpc_cred *cred;
406 spin_lock(&clp->cl_lock);
407 cred = nfs4_get_machine_cred_locked(clp);
408 spin_unlock(&clp->cl_lock);
412 static struct nfs4_state_owner *
413 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
415 struct rb_node **p = &server->state_owners.rb_node,
417 struct nfs4_state_owner *sp;
421 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
423 if (cred < sp->so_cred)
424 p = &parent->rb_left;
425 else if (cred > sp->so_cred)
426 p = &parent->rb_right;
428 if (!list_empty(&sp->so_lru))
429 list_del_init(&sp->so_lru);
430 atomic_inc(&sp->so_count);
437 static struct nfs4_state_owner *
438 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
440 struct nfs_server *server = new->so_server;
441 struct rb_node **p = &server->state_owners.rb_node,
443 struct nfs4_state_owner *sp;
447 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
449 if (new->so_cred < sp->so_cred)
450 p = &parent->rb_left;
451 else if (new->so_cred > sp->so_cred)
452 p = &parent->rb_right;
454 if (!list_empty(&sp->so_lru))
455 list_del_init(&sp->so_lru);
456 atomic_inc(&sp->so_count);
460 rb_link_node(&new->so_server_node, parent, p);
461 rb_insert_color(&new->so_server_node, &server->state_owners);
466 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
468 struct nfs_server *server = sp->so_server;
470 if (!RB_EMPTY_NODE(&sp->so_server_node))
471 rb_erase(&sp->so_server_node, &server->state_owners);
475 nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
477 sc->create_time = ktime_get();
480 spin_lock_init(&sc->lock);
481 INIT_LIST_HEAD(&sc->list);
482 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
486 nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
488 rpc_destroy_wait_queue(&sc->wait);
492 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
493 * create a new state_owner.
496 static struct nfs4_state_owner *
497 nfs4_alloc_state_owner(struct nfs_server *server,
498 struct rpc_cred *cred,
501 struct nfs4_state_owner *sp;
503 sp = kzalloc(sizeof(*sp), gfp_flags);
506 sp->so_seqid.owner_id = ida_simple_get(&server->openowner_id, 0, 0,
508 if (sp->so_seqid.owner_id < 0) {
512 sp->so_server = server;
513 sp->so_cred = get_rpccred(cred);
514 spin_lock_init(&sp->so_lock);
515 INIT_LIST_HEAD(&sp->so_states);
516 nfs4_init_seqid_counter(&sp->so_seqid);
517 atomic_set(&sp->so_count, 1);
518 INIT_LIST_HEAD(&sp->so_lru);
519 seqcount_init(&sp->so_reclaim_seqcount);
520 mutex_init(&sp->so_delegreturn_mutex);
525 nfs4_reset_state_owner(struct nfs4_state_owner *sp)
527 /* This state_owner is no longer usable, but must
528 * remain in place so that state recovery can find it
529 * and the opens associated with it.
530 * It may also be used for new 'open' request to
531 * return a delegation to the server.
532 * So update the 'create_time' so that it looks like
533 * a new state_owner. This will cause the server to
534 * request an OPEN_CONFIRM to start a new sequence.
536 sp->so_seqid.create_time = ktime_get();
539 static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
541 nfs4_destroy_seqid_counter(&sp->so_seqid);
542 put_rpccred(sp->so_cred);
543 ida_simple_remove(&sp->so_server->openowner_id, sp->so_seqid.owner_id);
547 static void nfs4_gc_state_owners(struct nfs_server *server)
549 struct nfs_client *clp = server->nfs_client;
550 struct nfs4_state_owner *sp, *tmp;
551 unsigned long time_min, time_max;
554 spin_lock(&clp->cl_lock);
556 time_min = (long)time_max - (long)clp->cl_lease_time;
557 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
558 /* NB: LRU is sorted so that oldest is at the head */
559 if (time_in_range(sp->so_expires, time_min, time_max))
561 list_move(&sp->so_lru, &doomed);
562 nfs4_remove_state_owner_locked(sp);
564 spin_unlock(&clp->cl_lock);
566 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
567 list_del(&sp->so_lru);
568 nfs4_free_state_owner(sp);
573 * nfs4_get_state_owner - Look up a state owner given a credential
574 * @server: nfs_server to search
575 * @cred: RPC credential to match
577 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
579 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
580 struct rpc_cred *cred,
583 struct nfs_client *clp = server->nfs_client;
584 struct nfs4_state_owner *sp, *new;
586 spin_lock(&clp->cl_lock);
587 sp = nfs4_find_state_owner_locked(server, cred);
588 spin_unlock(&clp->cl_lock);
591 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
594 spin_lock(&clp->cl_lock);
595 sp = nfs4_insert_state_owner_locked(new);
596 spin_unlock(&clp->cl_lock);
598 nfs4_free_state_owner(new);
600 nfs4_gc_state_owners(server);
605 * nfs4_put_state_owner - Release a nfs4_state_owner
606 * @sp: state owner data to release
608 * Note that we keep released state owners on an LRU
610 * This caches valid state owners so that they can be
611 * reused, to avoid the OPEN_CONFIRM on minor version 0.
612 * It also pins the uniquifier of dropped state owners for
613 * a while, to ensure that those state owner names are
616 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
618 struct nfs_server *server = sp->so_server;
619 struct nfs_client *clp = server->nfs_client;
621 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
624 sp->so_expires = jiffies;
625 list_add_tail(&sp->so_lru, &server->state_owners_lru);
626 spin_unlock(&clp->cl_lock);
630 * nfs4_purge_state_owners - Release all cached state owners
631 * @server: nfs_server with cached state owners to release
632 * @head: resulting list of state owners
634 * Called at umount time. Remaining state owners will be on
635 * the LRU with ref count of zero.
636 * Note that the state owners are not freed, but are added
637 * to the list @head, which can later be used as an argument
638 * to nfs4_free_state_owners.
640 void nfs4_purge_state_owners(struct nfs_server *server, struct list_head *head)
642 struct nfs_client *clp = server->nfs_client;
643 struct nfs4_state_owner *sp, *tmp;
645 spin_lock(&clp->cl_lock);
646 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
647 list_move(&sp->so_lru, head);
648 nfs4_remove_state_owner_locked(sp);
650 spin_unlock(&clp->cl_lock);
654 * nfs4_purge_state_owners - Release all cached state owners
655 * @head: resulting list of state owners
657 * Frees a list of state owners that was generated by
658 * nfs4_purge_state_owners
660 void nfs4_free_state_owners(struct list_head *head)
662 struct nfs4_state_owner *sp, *tmp;
664 list_for_each_entry_safe(sp, tmp, head, so_lru) {
665 list_del(&sp->so_lru);
666 nfs4_free_state_owner(sp);
670 static struct nfs4_state *
671 nfs4_alloc_open_state(void)
673 struct nfs4_state *state;
675 state = kzalloc(sizeof(*state), GFP_NOFS);
678 atomic_set(&state->count, 1);
679 INIT_LIST_HEAD(&state->lock_states);
680 spin_lock_init(&state->state_lock);
681 seqlock_init(&state->seqlock);
682 init_waitqueue_head(&state->waitq);
687 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
689 if (state->state == fmode)
691 /* NB! List reordering - see the reclaim code for why. */
692 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
693 if (fmode & FMODE_WRITE)
694 list_move(&state->open_states, &state->owner->so_states);
696 list_move_tail(&state->open_states, &state->owner->so_states);
698 state->state = fmode;
701 static struct nfs4_state *
702 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
704 struct nfs_inode *nfsi = NFS_I(inode);
705 struct nfs4_state *state;
707 list_for_each_entry(state, &nfsi->open_states, inode_states) {
708 if (state->owner != owner)
710 if (!nfs4_valid_open_stateid(state))
712 if (atomic_inc_not_zero(&state->count))
719 nfs4_free_open_state(struct nfs4_state *state)
725 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
727 struct nfs4_state *state, *new;
728 struct nfs_inode *nfsi = NFS_I(inode);
730 spin_lock(&inode->i_lock);
731 state = __nfs4_find_state_byowner(inode, owner);
732 spin_unlock(&inode->i_lock);
735 new = nfs4_alloc_open_state();
736 spin_lock(&owner->so_lock);
737 spin_lock(&inode->i_lock);
738 state = __nfs4_find_state_byowner(inode, owner);
739 if (state == NULL && new != NULL) {
741 state->owner = owner;
742 atomic_inc(&owner->so_count);
743 list_add(&state->inode_states, &nfsi->open_states);
745 state->inode = inode;
746 spin_unlock(&inode->i_lock);
747 /* Note: The reclaim code dictates that we add stateless
748 * and read-only stateids to the end of the list */
749 list_add_tail(&state->open_states, &owner->so_states);
750 spin_unlock(&owner->so_lock);
752 spin_unlock(&inode->i_lock);
753 spin_unlock(&owner->so_lock);
755 nfs4_free_open_state(new);
761 void nfs4_put_open_state(struct nfs4_state *state)
763 struct inode *inode = state->inode;
764 struct nfs4_state_owner *owner = state->owner;
766 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
768 spin_lock(&inode->i_lock);
769 list_del(&state->inode_states);
770 list_del(&state->open_states);
771 spin_unlock(&inode->i_lock);
772 spin_unlock(&owner->so_lock);
774 nfs4_free_open_state(state);
775 nfs4_put_state_owner(owner);
779 * Close the current file.
781 static void __nfs4_close(struct nfs4_state *state,
782 fmode_t fmode, gfp_t gfp_mask, int wait)
784 struct nfs4_state_owner *owner = state->owner;
788 atomic_inc(&owner->so_count);
789 /* Protect against nfs4_find_state() */
790 spin_lock(&owner->so_lock);
791 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
798 case FMODE_READ|FMODE_WRITE:
801 newstate = FMODE_READ|FMODE_WRITE;
802 if (state->n_rdwr == 0) {
803 if (state->n_rdonly == 0) {
804 newstate &= ~FMODE_READ;
805 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
806 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
808 if (state->n_wronly == 0) {
809 newstate &= ~FMODE_WRITE;
810 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
811 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
814 clear_bit(NFS_DELEGATED_STATE, &state->flags);
816 nfs4_state_set_mode_locked(state, newstate);
817 spin_unlock(&owner->so_lock);
820 nfs4_put_open_state(state);
821 nfs4_put_state_owner(owner);
823 nfs4_do_close(state, gfp_mask, wait);
826 void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
828 __nfs4_close(state, fmode, GFP_NOFS, 0);
831 void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
833 __nfs4_close(state, fmode, GFP_KERNEL, 1);
837 * Search the state->lock_states for an existing lock_owner
838 * that is compatible with either of the given owners.
839 * If the second is non-zero, then the first refers to a Posix-lock
840 * owner (current->files) and the second refers to a flock/OFD
841 * owner (struct file*). In that case, prefer a match for the first
843 * If both sorts of locks are held on the one file we cannot know
844 * which stateid was intended to be used, so a "correct" choice cannot
845 * be made. Failing that, a "consistent" choice is preferable. The
846 * consistent choice we make is to prefer the first owner, that of a
849 static struct nfs4_lock_state *
850 __nfs4_find_lock_state(struct nfs4_state *state,
851 fl_owner_t fl_owner, fl_owner_t fl_owner2)
853 struct nfs4_lock_state *pos, *ret = NULL;
854 list_for_each_entry(pos, &state->lock_states, ls_locks) {
855 if (pos->ls_owner == fl_owner) {
859 if (pos->ls_owner == fl_owner2)
863 refcount_inc(&ret->ls_count);
868 * Return a compatible lock_state. If no initialized lock_state structure
869 * exists, return an uninitialized one.
872 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
874 struct nfs4_lock_state *lsp;
875 struct nfs_server *server = state->owner->so_server;
877 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
880 nfs4_init_seqid_counter(&lsp->ls_seqid);
881 refcount_set(&lsp->ls_count, 1);
882 lsp->ls_state = state;
883 lsp->ls_owner = fl_owner;
884 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
885 if (lsp->ls_seqid.owner_id < 0)
887 INIT_LIST_HEAD(&lsp->ls_locks);
894 void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
896 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
897 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
902 * Return a compatible lock_state. If no initialized lock_state structure
903 * exists, return an uninitialized one.
906 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
908 struct nfs4_lock_state *lsp, *new = NULL;
911 spin_lock(&state->state_lock);
912 lsp = __nfs4_find_lock_state(state, owner, NULL);
916 list_add(&new->ls_locks, &state->lock_states);
917 set_bit(LK_STATE_IN_USE, &state->flags);
922 spin_unlock(&state->state_lock);
923 new = nfs4_alloc_lock_state(state, owner);
927 spin_unlock(&state->state_lock);
929 nfs4_free_lock_state(state->owner->so_server, new);
934 * Release reference to lock_state, and free it if we see that
935 * it is no longer in use
937 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
939 struct nfs_server *server;
940 struct nfs4_state *state;
944 state = lsp->ls_state;
945 if (!refcount_dec_and_lock(&lsp->ls_count, &state->state_lock))
947 list_del(&lsp->ls_locks);
948 if (list_empty(&state->lock_states))
949 clear_bit(LK_STATE_IN_USE, &state->flags);
950 spin_unlock(&state->state_lock);
951 server = state->owner->so_server;
952 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
953 struct nfs_client *clp = server->nfs_client;
955 clp->cl_mvops->free_lock_state(server, lsp);
957 nfs4_free_lock_state(server, lsp);
960 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
962 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
964 dst->fl_u.nfs4_fl.owner = lsp;
965 refcount_inc(&lsp->ls_count);
968 static void nfs4_fl_release_lock(struct file_lock *fl)
970 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
973 static const struct file_lock_operations nfs4_fl_lock_ops = {
974 .fl_copy_lock = nfs4_fl_copy_lock,
975 .fl_release_private = nfs4_fl_release_lock,
978 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
980 struct nfs4_lock_state *lsp;
982 if (fl->fl_ops != NULL)
984 lsp = nfs4_get_lock_state(state, fl->fl_owner);
987 fl->fl_u.nfs4_fl.owner = lsp;
988 fl->fl_ops = &nfs4_fl_lock_ops;
992 static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
993 struct nfs4_state *state,
994 const struct nfs_lock_context *l_ctx)
996 struct nfs4_lock_state *lsp;
997 fl_owner_t fl_owner, fl_flock_owner;
1003 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
1006 fl_owner = l_ctx->lockowner;
1007 fl_flock_owner = l_ctx->open_context->flock_owner;
1009 spin_lock(&state->state_lock);
1010 lsp = __nfs4_find_lock_state(state, fl_owner, fl_flock_owner);
1011 if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
1013 else if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
1014 nfs4_stateid_copy(dst, &lsp->ls_stateid);
1017 spin_unlock(&state->state_lock);
1018 nfs4_put_lock_state(lsp);
1023 bool nfs4_refresh_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1030 seq = read_seqbegin(&state->seqlock);
1031 if (nfs4_state_match_open_stateid_other(state, dst)) {
1032 dst->seqid = state->open_stateid.seqid;
1035 } while (read_seqretry(&state->seqlock, seq));
1039 bool nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1042 const nfs4_stateid *src;
1047 src = &zero_stateid;
1048 seq = read_seqbegin(&state->seqlock);
1049 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1050 src = &state->open_stateid;
1053 nfs4_stateid_copy(dst, src);
1054 } while (read_seqretry(&state->seqlock, seq));
1059 * Byte-range lock aware utility to initialize the stateid of read/write
1062 int nfs4_select_rw_stateid(struct nfs4_state *state,
1063 fmode_t fmode, const struct nfs_lock_context *l_ctx,
1064 nfs4_stateid *dst, struct rpc_cred **cred)
1068 if (!nfs4_valid_open_stateid(state))
1072 ret = nfs4_copy_lock_stateid(dst, state, l_ctx);
1074 /* A lost lock - don't even consider delegations */
1076 /* returns true if delegation stateid found and copied */
1077 if (nfs4_copy_delegation_stateid(state->inode, fmode, dst, cred)) {
1082 /* nfs4_copy_delegation_stateid() didn't over-write
1083 * dst, so it still has the lock stateid which we now
1087 nfs4_copy_open_stateid(dst, state);
1090 if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
1095 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1097 struct nfs_seqid *new;
1099 new = kmalloc(sizeof(*new), gfp_mask);
1101 return ERR_PTR(-ENOMEM);
1102 new->sequence = counter;
1103 INIT_LIST_HEAD(&new->list);
1108 void nfs_release_seqid(struct nfs_seqid *seqid)
1110 struct nfs_seqid_counter *sequence;
1112 if (seqid == NULL || list_empty(&seqid->list))
1114 sequence = seqid->sequence;
1115 spin_lock(&sequence->lock);
1116 list_del_init(&seqid->list);
1117 if (!list_empty(&sequence->list)) {
1118 struct nfs_seqid *next;
1120 next = list_first_entry(&sequence->list,
1121 struct nfs_seqid, list);
1122 rpc_wake_up_queued_task(&sequence->wait, next->task);
1124 spin_unlock(&sequence->lock);
1127 void nfs_free_seqid(struct nfs_seqid *seqid)
1129 nfs_release_seqid(seqid);
1134 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1135 * failed with a seqid incrementing error -
1136 * see comments nfs4.h:seqid_mutating_error()
1138 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1143 case -NFS4ERR_BAD_SEQID:
1144 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1146 pr_warn_ratelimited("NFS: v4 server returned a bad"
1147 " sequence-id error on an"
1148 " unconfirmed sequence %p!\n",
1150 case -NFS4ERR_STALE_CLIENTID:
1151 case -NFS4ERR_STALE_STATEID:
1152 case -NFS4ERR_BAD_STATEID:
1153 case -NFS4ERR_BADXDR:
1154 case -NFS4ERR_RESOURCE:
1155 case -NFS4ERR_NOFILEHANDLE:
1156 case -NFS4ERR_MOVED:
1157 /* Non-seqid mutating errors */
1161 * Note: no locking needed as we are guaranteed to be first
1162 * on the sequence list
1164 seqid->sequence->counter++;
1167 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1169 struct nfs4_state_owner *sp;
1174 sp = container_of(seqid->sequence, struct nfs4_state_owner, so_seqid);
1175 if (status == -NFS4ERR_BAD_SEQID)
1176 nfs4_reset_state_owner(sp);
1177 if (!nfs4_has_session(sp->so_server->nfs_client))
1178 nfs_increment_seqid(status, seqid);
1182 * Increment the seqid if the LOCK/LOCKU succeeded, or
1183 * failed with a seqid incrementing error -
1184 * see comments nfs4.h:seqid_mutating_error()
1186 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1189 nfs_increment_seqid(status, seqid);
1192 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1194 struct nfs_seqid_counter *sequence;
1199 sequence = seqid->sequence;
1200 spin_lock(&sequence->lock);
1202 if (list_empty(&seqid->list))
1203 list_add_tail(&seqid->list, &sequence->list);
1204 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1206 rpc_sleep_on(&sequence->wait, task, NULL);
1209 spin_unlock(&sequence->lock);
1214 static int nfs4_run_state_manager(void *);
1216 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1218 smp_mb__before_atomic();
1219 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1220 smp_mb__after_atomic();
1221 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
1222 rpc_wake_up(&clp->cl_rpcwaitq);
1226 * Schedule the nfs_client asynchronous state management routine
1228 void nfs4_schedule_state_manager(struct nfs_client *clp)
1230 struct task_struct *task;
1231 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1233 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
1234 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1236 __module_get(THIS_MODULE);
1237 refcount_inc(&clp->cl_count);
1239 /* The rcu_read_lock() is not strictly necessary, as the state
1240 * manager is the only thread that ever changes the rpc_xprt
1241 * after it's initialized. At this point, we're single threaded. */
1243 snprintf(buf, sizeof(buf), "%s-manager",
1244 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1246 task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
1248 printk(KERN_ERR "%s: kthread_run: %ld\n",
1249 __func__, PTR_ERR(task));
1250 nfs4_clear_state_manager_bit(clp);
1251 nfs_put_client(clp);
1252 module_put(THIS_MODULE);
1257 * Schedule a lease recovery attempt
1259 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1263 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1264 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1265 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1267 nfs4_schedule_state_manager(clp);
1269 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1272 * nfs4_schedule_migration_recovery - trigger migration recovery
1274 * @server: FSID that is migrating
1276 * Returns zero if recovery has started, otherwise a negative NFS4ERR
1277 * value is returned.
1279 int nfs4_schedule_migration_recovery(const struct nfs_server *server)
1281 struct nfs_client *clp = server->nfs_client;
1283 if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
1284 pr_err("NFS: volatile file handles not supported (server %s)\n",
1289 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
1292 dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1294 (unsigned long long)server->fsid.major,
1295 (unsigned long long)server->fsid.minor,
1298 set_bit(NFS_MIG_IN_TRANSITION,
1299 &((struct nfs_server *)server)->mig_status);
1300 set_bit(NFS4CLNT_MOVED, &clp->cl_state);
1302 nfs4_schedule_state_manager(clp);
1305 EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
1308 * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1310 * @clp: server to check for moved leases
1313 void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
1315 dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1316 __func__, clp->cl_clientid, clp->cl_hostname);
1318 set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
1319 nfs4_schedule_state_manager(clp);
1321 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
1323 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1329 refcount_inc(&clp->cl_count);
1330 res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1331 nfs_wait_bit_killable, TASK_KILLABLE);
1334 if (clp->cl_cons_state < 0)
1335 res = clp->cl_cons_state;
1337 nfs_put_client(clp);
1341 int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1346 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1347 ret = nfs4_wait_clnt_recover(clp);
1350 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1351 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1353 nfs4_schedule_state_manager(clp);
1360 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1361 * @clp: client to process
1363 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1364 * resend of the SETCLIENTID and hence re-establish the
1365 * callback channel. Then return all existing delegations.
1367 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1369 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1370 nfs_expire_all_delegations(clp);
1371 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1375 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1377 nfs40_handle_cb_pathdown(clp);
1378 nfs4_schedule_state_manager(clp);
1381 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1384 if (!nfs4_valid_open_stateid(state))
1386 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1387 /* Don't recover state that expired before the reboot */
1388 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1389 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1392 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1393 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1397 int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1399 if (!nfs4_valid_open_stateid(state))
1401 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1402 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1403 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1404 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1408 int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1410 struct nfs_client *clp = server->nfs_client;
1412 if (!nfs4_state_mark_reclaim_nograce(clp, state))
1414 nfs_inode_find_delegation_state_and_recover(state->inode,
1416 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1418 nfs4_schedule_state_manager(clp);
1421 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1423 static struct nfs4_lock_state *
1424 nfs_state_find_lock_state_by_stateid(struct nfs4_state *state,
1425 const nfs4_stateid *stateid)
1427 struct nfs4_lock_state *pos;
1429 list_for_each_entry(pos, &state->lock_states, ls_locks) {
1430 if (!test_bit(NFS_LOCK_INITIALIZED, &pos->ls_flags))
1432 if (nfs4_stateid_match_other(&pos->ls_stateid, stateid))
1438 static bool nfs_state_lock_state_matches_stateid(struct nfs4_state *state,
1439 const nfs4_stateid *stateid)
1443 if (test_bit(LK_STATE_IN_USE, &state->flags)) {
1444 spin_lock(&state->state_lock);
1445 if (nfs_state_find_lock_state_by_stateid(state, stateid))
1447 spin_unlock(&state->state_lock);
1452 void nfs_inode_find_state_and_recover(struct inode *inode,
1453 const nfs4_stateid *stateid)
1455 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1456 struct nfs_inode *nfsi = NFS_I(inode);
1457 struct nfs_open_context *ctx;
1458 struct nfs4_state *state;
1461 spin_lock(&inode->i_lock);
1462 list_for_each_entry(ctx, &nfsi->open_files, list) {
1466 if (nfs4_stateid_match_other(&state->stateid, stateid) &&
1467 nfs4_state_mark_reclaim_nograce(clp, state)) {
1471 if (nfs4_stateid_match_other(&state->open_stateid, stateid) &&
1472 nfs4_state_mark_reclaim_nograce(clp, state)) {
1476 if (nfs_state_lock_state_matches_stateid(state, stateid) &&
1477 nfs4_state_mark_reclaim_nograce(clp, state))
1480 spin_unlock(&inode->i_lock);
1482 nfs_inode_find_delegation_state_and_recover(inode, stateid);
1484 nfs4_schedule_state_manager(clp);
1487 static void nfs4_state_mark_open_context_bad(struct nfs4_state *state)
1489 struct inode *inode = state->inode;
1490 struct nfs_inode *nfsi = NFS_I(inode);
1491 struct nfs_open_context *ctx;
1493 spin_lock(&inode->i_lock);
1494 list_for_each_entry(ctx, &nfsi->open_files, list) {
1495 if (ctx->state != state)
1497 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1499 spin_unlock(&inode->i_lock);
1502 static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1504 set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
1505 nfs4_state_mark_open_context_bad(state);
1509 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1511 struct inode *inode = state->inode;
1512 struct nfs_inode *nfsi = NFS_I(inode);
1513 struct file_lock *fl;
1514 struct nfs4_lock_state *lsp;
1516 struct file_lock_context *flctx = inode->i_flctx;
1517 struct list_head *list;
1522 list = &flctx->flc_posix;
1524 /* Guard against delegation returns and new lock/unlock calls */
1525 down_write(&nfsi->rwsem);
1526 spin_lock(&flctx->flc_lock);
1528 list_for_each_entry(fl, list, fl_list) {
1529 if (nfs_file_open_context(fl->fl_file)->state != state)
1531 spin_unlock(&flctx->flc_lock);
1532 status = ops->recover_lock(state, fl);
1537 case -NFS4ERR_ADMIN_REVOKED:
1538 case -NFS4ERR_STALE_STATEID:
1539 case -NFS4ERR_BAD_STATEID:
1540 case -NFS4ERR_EXPIRED:
1541 case -NFS4ERR_NO_GRACE:
1542 case -NFS4ERR_STALE_CLIENTID:
1543 case -NFS4ERR_BADSESSION:
1544 case -NFS4ERR_BADSLOT:
1545 case -NFS4ERR_BAD_HIGH_SLOT:
1546 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1549 pr_err("NFS: %s: unhandled error %d\n",
1553 case -NFS4ERR_DENIED:
1554 case -NFS4ERR_RECLAIM_BAD:
1555 case -NFS4ERR_RECLAIM_CONFLICT:
1556 lsp = fl->fl_u.nfs4_fl.owner;
1558 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1561 spin_lock(&flctx->flc_lock);
1563 if (list == &flctx->flc_posix) {
1564 list = &flctx->flc_flock;
1567 spin_unlock(&flctx->flc_lock);
1569 up_write(&nfsi->rwsem);
1573 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1575 struct nfs4_state *state;
1576 struct nfs4_lock_state *lock;
1579 /* Note: we rely on the sp->so_states list being ordered
1580 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1582 * This is needed to ensure that the server won't give us any
1583 * read delegations that we have to return if, say, we are
1584 * recovering after a network partition or a reboot from a
1585 * server that doesn't support a grace period.
1587 spin_lock(&sp->so_lock);
1588 raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
1590 list_for_each_entry(state, &sp->so_states, open_states) {
1591 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1593 if (!nfs4_valid_open_stateid(state))
1595 if (state->state == 0)
1597 atomic_inc(&state->count);
1598 spin_unlock(&sp->so_lock);
1599 status = ops->recover_open(sp, state);
1601 status = nfs4_reclaim_locks(state, ops);
1603 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1604 spin_lock(&state->state_lock);
1605 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1606 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1607 pr_warn_ratelimited("NFS: "
1609 "failed!\n", __func__);
1611 spin_unlock(&state->state_lock);
1613 clear_bit(NFS_STATE_RECLAIM_NOGRACE,
1615 #ifdef CONFIG_NFS_V4_2
1616 if (test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags)) {
1617 struct nfs4_copy_state *copy;
1619 spin_lock(&sp->so_server->nfs_client->cl_lock);
1620 list_for_each_entry(copy, &sp->so_server->ss_copies, copies) {
1621 if (memcmp(&state->stateid.other, ©->parent_state->stateid.other, NFS4_STATEID_SIZE))
1624 complete(©->completion);
1625 printk("AGLO: server rebooted waking up the copy\n");
1628 spin_unlock(&sp->so_server->nfs_client->cl_lock);
1630 #endif /* CONFIG_NFS_V4_2 */
1631 nfs4_put_open_state(state);
1632 spin_lock(&sp->so_lock);
1638 printk(KERN_ERR "NFS: %s: unhandled error %d\n",
1647 /* Open state on this file cannot be recovered */
1648 nfs4_state_mark_recovery_failed(state, status);
1653 case -NFS4ERR_ADMIN_REVOKED:
1654 case -NFS4ERR_STALE_STATEID:
1655 case -NFS4ERR_OLD_STATEID:
1656 case -NFS4ERR_BAD_STATEID:
1657 case -NFS4ERR_RECLAIM_BAD:
1658 case -NFS4ERR_RECLAIM_CONFLICT:
1659 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1661 case -NFS4ERR_EXPIRED:
1662 case -NFS4ERR_NO_GRACE:
1663 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1664 case -NFS4ERR_STALE_CLIENTID:
1665 case -NFS4ERR_BADSESSION:
1666 case -NFS4ERR_BADSLOT:
1667 case -NFS4ERR_BAD_HIGH_SLOT:
1668 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1671 nfs4_put_open_state(state);
1672 spin_lock(&sp->so_lock);
1675 raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1676 spin_unlock(&sp->so_lock);
1679 nfs4_put_open_state(state);
1680 spin_lock(&sp->so_lock);
1681 raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1682 spin_unlock(&sp->so_lock);
1686 static void nfs4_clear_open_state(struct nfs4_state *state)
1688 struct nfs4_lock_state *lock;
1690 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1691 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1692 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1693 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1694 spin_lock(&state->state_lock);
1695 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1696 lock->ls_seqid.flags = 0;
1697 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1699 spin_unlock(&state->state_lock);
1702 static void nfs4_reset_seqids(struct nfs_server *server,
1703 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1705 struct nfs_client *clp = server->nfs_client;
1706 struct nfs4_state_owner *sp;
1707 struct rb_node *pos;
1708 struct nfs4_state *state;
1710 spin_lock(&clp->cl_lock);
1711 for (pos = rb_first(&server->state_owners);
1713 pos = rb_next(pos)) {
1714 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1715 sp->so_seqid.flags = 0;
1716 spin_lock(&sp->so_lock);
1717 list_for_each_entry(state, &sp->so_states, open_states) {
1718 if (mark_reclaim(clp, state))
1719 nfs4_clear_open_state(state);
1721 spin_unlock(&sp->so_lock);
1723 spin_unlock(&clp->cl_lock);
1726 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1727 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1729 struct nfs_server *server;
1732 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1733 nfs4_reset_seqids(server, mark_reclaim);
1737 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1739 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1740 /* Mark all delegations for reclaim */
1741 nfs_delegation_mark_reclaim(clp);
1742 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1745 static int nfs4_reclaim_complete(struct nfs_client *clp,
1746 const struct nfs4_state_recovery_ops *ops,
1747 struct rpc_cred *cred)
1749 /* Notify the server we're done reclaiming our state */
1750 if (ops->reclaim_complete)
1751 return ops->reclaim_complete(clp, cred);
1755 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1757 struct nfs_client *clp = server->nfs_client;
1758 struct nfs4_state_owner *sp;
1759 struct rb_node *pos;
1760 struct nfs4_state *state;
1762 spin_lock(&clp->cl_lock);
1763 for (pos = rb_first(&server->state_owners);
1765 pos = rb_next(pos)) {
1766 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1767 spin_lock(&sp->so_lock);
1768 list_for_each_entry(state, &sp->so_states, open_states) {
1769 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1772 nfs4_state_mark_reclaim_nograce(clp, state);
1774 spin_unlock(&sp->so_lock);
1776 spin_unlock(&clp->cl_lock);
1779 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1781 struct nfs_server *server;
1783 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1787 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1788 nfs4_clear_reclaim_server(server);
1791 nfs_delegation_reap_unclaimed(clp);
1795 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1797 const struct nfs4_state_recovery_ops *ops;
1798 struct rpc_cred *cred;
1801 if (!nfs4_state_clear_reclaim_reboot(clp))
1803 ops = clp->cl_mvops->reboot_recovery_ops;
1804 cred = nfs4_get_clid_cred(clp);
1805 err = nfs4_reclaim_complete(clp, ops, cred);
1807 if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
1808 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1811 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1813 nfs_mark_test_expired_all_delegations(clp);
1814 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1817 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1822 case -NFS4ERR_CB_PATH_DOWN:
1823 nfs40_handle_cb_pathdown(clp);
1825 case -NFS4ERR_NO_GRACE:
1826 nfs4_state_end_reclaim_reboot(clp);
1828 case -NFS4ERR_STALE_CLIENTID:
1829 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1830 nfs4_state_start_reclaim_reboot(clp);
1832 case -NFS4ERR_EXPIRED:
1833 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1834 nfs4_state_start_reclaim_nograce(clp);
1836 case -NFS4ERR_BADSESSION:
1837 case -NFS4ERR_BADSLOT:
1838 case -NFS4ERR_BAD_HIGH_SLOT:
1839 case -NFS4ERR_DEADSESSION:
1840 case -NFS4ERR_SEQ_FALSE_RETRY:
1841 case -NFS4ERR_SEQ_MISORDERED:
1842 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1843 /* Zero session reset errors */
1845 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1846 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1849 dprintk("%s: failed to handle error %d for server %s\n",
1850 __func__, error, clp->cl_hostname);
1853 dprintk("%s: handled error %d for server %s\n", __func__, error,
1858 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1860 struct nfs4_state_owner *sp;
1861 struct nfs_server *server;
1862 struct rb_node *pos;
1868 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1869 nfs4_purge_state_owners(server, &freeme);
1870 spin_lock(&clp->cl_lock);
1871 for (pos = rb_first(&server->state_owners);
1873 pos = rb_next(pos)) {
1875 struct nfs4_state_owner, so_server_node);
1876 if (!test_and_clear_bit(ops->owner_flag_bit,
1879 if (!atomic_inc_not_zero(&sp->so_count))
1881 spin_unlock(&clp->cl_lock);
1884 status = nfs4_reclaim_open_state(sp, ops);
1886 set_bit(ops->owner_flag_bit, &sp->so_flags);
1887 nfs4_put_state_owner(sp);
1888 status = nfs4_recovery_handle_error(clp, status);
1889 return (status != 0) ? status : -EAGAIN;
1892 nfs4_put_state_owner(sp);
1895 spin_unlock(&clp->cl_lock);
1898 nfs4_free_state_owners(&freeme);
1902 static int nfs4_check_lease(struct nfs_client *clp)
1904 struct rpc_cred *cred;
1905 const struct nfs4_state_maintenance_ops *ops =
1906 clp->cl_mvops->state_renewal_ops;
1909 /* Is the client already known to have an expired lease? */
1910 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1912 spin_lock(&clp->cl_lock);
1913 cred = ops->get_state_renewal_cred_locked(clp);
1914 spin_unlock(&clp->cl_lock);
1916 cred = nfs4_get_clid_cred(clp);
1921 status = ops->renew_lease(clp, cred);
1923 if (status == -ETIMEDOUT) {
1924 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1928 return nfs4_recovery_handle_error(clp, status);
1931 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1932 * and for recoverable errors on EXCHANGE_ID for v4.1
1934 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1937 case -NFS4ERR_SEQ_MISORDERED:
1938 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1939 return -ESERVERFAULT;
1940 /* Lease confirmation error: retry after purging the lease */
1942 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1944 case -NFS4ERR_STALE_CLIENTID:
1945 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1946 nfs4_state_start_reclaim_reboot(clp);
1948 case -NFS4ERR_CLID_INUSE:
1949 pr_err("NFS: Server %s reports our clientid is in use\n",
1951 nfs_mark_client_ready(clp, -EPERM);
1952 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1955 case -NFS4ERR_DELAY:
1961 case -NFS4ERR_MINOR_VERS_MISMATCH:
1962 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1963 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1964 dprintk("%s: exit with error %d for server %s\n",
1965 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1966 return -EPROTONOSUPPORT;
1967 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1968 * in nfs4_exchange_id */
1970 dprintk("%s: exit with error %d for server %s\n", __func__,
1971 status, clp->cl_hostname);
1974 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1975 dprintk("%s: handled error %d for server %s\n", __func__, status,
1980 static int nfs4_establish_lease(struct nfs_client *clp)
1982 struct rpc_cred *cred;
1983 const struct nfs4_state_recovery_ops *ops =
1984 clp->cl_mvops->reboot_recovery_ops;
1987 status = nfs4_begin_drain_session(clp);
1990 cred = nfs4_get_clid_cred(clp);
1993 status = ops->establish_clid(clp, cred);
1997 pnfs_destroy_all_layouts(clp);
2002 * Returns zero or a negative errno. NFS4ERR values are converted
2003 * to local errno values.
2005 static int nfs4_reclaim_lease(struct nfs_client *clp)
2009 status = nfs4_establish_lease(clp);
2011 return nfs4_handle_reclaim_lease_error(clp, status);
2012 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
2013 nfs4_state_start_reclaim_nograce(clp);
2014 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2015 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
2016 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2017 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2021 static int nfs4_purge_lease(struct nfs_client *clp)
2025 status = nfs4_establish_lease(clp);
2027 return nfs4_handle_reclaim_lease_error(clp, status);
2028 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2029 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2030 nfs4_state_start_reclaim_nograce(clp);
2035 * Try remote migration of one FSID from a source server to a
2036 * destination server. The source server provides a list of
2037 * potential destinations.
2039 * Returns zero or a negative NFS4ERR status code.
2041 static int nfs4_try_migration(struct nfs_server *server, struct rpc_cred *cred)
2043 struct nfs_client *clp = server->nfs_client;
2044 struct nfs4_fs_locations *locations = NULL;
2045 struct inode *inode;
2049 dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
2050 (unsigned long long)server->fsid.major,
2051 (unsigned long long)server->fsid.minor,
2055 page = alloc_page(GFP_KERNEL);
2056 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2057 if (page == NULL || locations == NULL) {
2058 dprintk("<-- %s: no memory\n", __func__);
2062 inode = d_inode(server->super->s_root);
2063 result = nfs4_proc_get_locations(inode, locations, page, cred);
2065 dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2070 result = -NFS4ERR_NXIO;
2071 if (!locations->nlocations)
2074 if (!(locations->fattr.valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
2075 dprintk("<-- %s: No fs_locations data, migration skipped\n",
2080 status = nfs4_begin_drain_session(clp);
2084 status = nfs4_replace_transport(server, locations);
2086 dprintk("<-- %s: failed to replace transport: %d\n",
2092 dprintk("<-- %s: migration succeeded\n", __func__);
2099 pr_err("NFS: migration recovery failed (server %s)\n",
2101 set_bit(NFS_MIG_FAILED, &server->mig_status);
2107 * Returns zero or a negative NFS4ERR status code.
2109 static int nfs4_handle_migration(struct nfs_client *clp)
2111 const struct nfs4_state_maintenance_ops *ops =
2112 clp->cl_mvops->state_renewal_ops;
2113 struct nfs_server *server;
2114 struct rpc_cred *cred;
2116 dprintk("%s: migration reported on \"%s\"\n", __func__,
2119 spin_lock(&clp->cl_lock);
2120 cred = ops->get_state_renewal_cred_locked(clp);
2121 spin_unlock(&clp->cl_lock);
2123 return -NFS4ERR_NOENT;
2128 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2131 if (server->mig_gen == clp->cl_mig_gen)
2133 server->mig_gen = clp->cl_mig_gen;
2135 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
2136 &server->mig_status))
2140 status = nfs4_try_migration(server, cred);
2153 * Test each nfs_server on the clp's cl_superblocks list to see
2154 * if it's moved to another server. Stop when the server no longer
2155 * returns NFS4ERR_LEASE_MOVED.
2157 static int nfs4_handle_lease_moved(struct nfs_client *clp)
2159 const struct nfs4_state_maintenance_ops *ops =
2160 clp->cl_mvops->state_renewal_ops;
2161 struct nfs_server *server;
2162 struct rpc_cred *cred;
2164 dprintk("%s: lease moved reported on \"%s\"\n", __func__,
2167 spin_lock(&clp->cl_lock);
2168 cred = ops->get_state_renewal_cred_locked(clp);
2169 spin_unlock(&clp->cl_lock);
2171 return -NFS4ERR_NOENT;
2176 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2177 struct inode *inode;
2180 if (server->mig_gen == clp->cl_mig_gen)
2182 server->mig_gen = clp->cl_mig_gen;
2186 inode = d_inode(server->super->s_root);
2187 status = nfs4_proc_fsid_present(inode, cred);
2188 if (status != -NFS4ERR_MOVED)
2189 goto restart; /* wasn't this one */
2190 if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
2191 goto restart; /* there are more */
2202 * nfs4_discover_server_trunking - Detect server IP address trunking
2204 * @clp: nfs_client under test
2205 * @result: OUT: found nfs_client, or clp
2207 * Returns zero or a negative errno. If zero is returned,
2208 * an nfs_client pointer is planted in "result".
2210 * Note: since we are invoked in process context, and
2211 * not from inside the state manager, we cannot use
2212 * nfs4_handle_reclaim_lease_error().
2214 int nfs4_discover_server_trunking(struct nfs_client *clp,
2215 struct nfs_client **result)
2217 const struct nfs4_state_recovery_ops *ops =
2218 clp->cl_mvops->reboot_recovery_ops;
2219 struct rpc_clnt *clnt;
2220 struct rpc_cred *cred;
2223 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
2225 clnt = clp->cl_rpcclient;
2228 mutex_lock(&nfs_clid_init_mutex);
2231 cred = nfs4_get_clid_cred(clp);
2235 status = ops->detect_trunking(clp, result, cred);
2243 if (clnt->cl_softrtry)
2246 case -NFS4ERR_DELAY:
2250 case -NFS4ERR_STALE_CLIENTID:
2251 dprintk("NFS: %s after status %d, retrying\n",
2256 nfs4_root_machine_cred(clp);
2259 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
2262 case -NFS4ERR_CLID_INUSE:
2263 case -NFS4ERR_WRONGSEC:
2264 /* No point in retrying if we already used RPC_AUTH_UNIX */
2265 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
2269 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
2271 status = PTR_ERR(clnt);
2274 /* Note: this is safe because we haven't yet marked the
2275 * client as ready, so we are the only user of
2278 clnt = xchg(&clp->cl_rpcclient, clnt);
2279 rpc_shutdown_client(clnt);
2280 clnt = clp->cl_rpcclient;
2283 case -NFS4ERR_MINOR_VERS_MISMATCH:
2284 status = -EPROTONOSUPPORT;
2288 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2289 * in nfs4_exchange_id */
2290 status = -EKEYEXPIRED;
2293 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2299 mutex_unlock(&nfs_clid_init_mutex);
2300 dprintk("NFS: %s: status = %d\n", __func__, status);
2304 #ifdef CONFIG_NFS_V4_1
2305 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
2307 struct nfs_client *clp = session->clp;
2311 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2313 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2314 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2316 nfs4_schedule_state_manager(clp);
2318 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
2320 void nfs41_notify_server(struct nfs_client *clp)
2322 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
2323 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2324 nfs4_schedule_state_manager(clp);
2327 static void nfs4_reset_all_state(struct nfs_client *clp)
2329 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2330 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2331 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2332 nfs4_state_start_reclaim_nograce(clp);
2333 dprintk("%s: scheduling reset of all state for server %s!\n",
2334 __func__, clp->cl_hostname);
2335 nfs4_schedule_state_manager(clp);
2339 static void nfs41_handle_server_reboot(struct nfs_client *clp)
2341 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2342 nfs4_state_start_reclaim_reboot(clp);
2343 dprintk("%s: server %s rebooted!\n", __func__,
2345 nfs4_schedule_state_manager(clp);
2349 static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
2351 nfs4_reset_all_state(clp);
2352 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2355 static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
2357 nfs4_state_start_reclaim_nograce(clp);
2358 nfs4_schedule_state_manager(clp);
2360 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2363 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
2365 /* FIXME: For now, we destroy all layouts. */
2366 pnfs_destroy_all_layouts(clp);
2367 /* FIXME: For now, we test all delegations+open state+locks. */
2368 nfs41_handle_some_state_revoked(clp);
2369 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2373 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
2375 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2376 nfs4_schedule_state_manager(clp);
2378 dprintk("%s: server %s declared a backchannel fault\n", __func__,
2382 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2384 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2385 &clp->cl_state) == 0)
2386 nfs4_schedule_state_manager(clp);
2389 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
2395 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2396 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2398 * If we're called from the state manager thread, then assume we're
2399 * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2400 * Those flags are expected to remain set until we're done
2401 * recovering (see RFC5661, section 18.46.3).
2406 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2407 nfs41_handle_server_reboot(clp);
2408 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
2409 nfs41_handle_all_state_revoked(clp);
2410 if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2411 SEQ4_STATUS_ADMIN_STATE_REVOKED))
2412 nfs41_handle_some_state_revoked(clp);
2413 if (flags & SEQ4_STATUS_LEASE_MOVED)
2414 nfs4_schedule_lease_moved_recovery(clp);
2415 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2416 nfs41_handle_recallable_state_revoked(clp);
2418 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2419 nfs41_handle_backchannel_fault(clp);
2420 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2421 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2422 nfs41_handle_cb_path_down(clp);
2425 static int nfs4_reset_session(struct nfs_client *clp)
2427 struct rpc_cred *cred;
2430 if (!nfs4_has_session(clp))
2432 status = nfs4_begin_drain_session(clp);
2435 cred = nfs4_get_clid_cred(clp);
2436 status = nfs4_proc_destroy_session(clp->cl_session, cred);
2439 case -NFS4ERR_BADSESSION:
2440 case -NFS4ERR_DEADSESSION:
2442 case -NFS4ERR_BACK_CHAN_BUSY:
2443 case -NFS4ERR_DELAY:
2444 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2449 status = nfs4_recovery_handle_error(clp, status);
2453 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2454 status = nfs4_proc_create_session(clp, cred);
2456 dprintk("%s: session reset failed with status %d for server %s!\n",
2457 __func__, status, clp->cl_hostname);
2458 status = nfs4_handle_reclaim_lease_error(clp, status);
2461 nfs41_finish_session_reset(clp);
2462 dprintk("%s: session reset was successful for server %s!\n",
2463 __func__, clp->cl_hostname);
2470 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2472 struct rpc_cred *cred;
2475 if (!nfs4_has_session(clp))
2477 ret = nfs4_begin_drain_session(clp);
2480 cred = nfs4_get_clid_cred(clp);
2481 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2484 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2487 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2488 __func__, clp->cl_hostname);
2490 case -NFS4ERR_DELAY:
2492 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2495 return nfs4_recovery_handle_error(clp, ret);
2499 #else /* CONFIG_NFS_V4_1 */
2500 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2502 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2506 #endif /* CONFIG_NFS_V4_1 */
2508 static void nfs4_state_manager(struct nfs_client *clp)
2510 unsigned int memflags;
2512 const char *section = "", *section_sep = "";
2515 * State recovery can deadlock if the direct reclaim code tries
2516 * start NFS writeback. So ensure memory allocations are all
2519 memflags = memalloc_nofs_save();
2521 /* Ensure exclusive access to NFSv4 state */
2523 clear_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2524 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2525 section = "purge state";
2526 status = nfs4_purge_lease(clp);
2532 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2533 section = "lease expired";
2534 /* We're going to have to re-establish a clientid */
2535 status = nfs4_reclaim_lease(clp);
2541 /* Initialize or reset the session */
2542 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2543 section = "reset session";
2544 status = nfs4_reset_session(clp);
2545 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2551 /* Send BIND_CONN_TO_SESSION */
2552 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2554 section = "bind conn to session";
2555 status = nfs4_bind_conn_to_session(clp);
2561 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2562 section = "check lease";
2563 status = nfs4_check_lease(clp);
2569 if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
2570 section = "migration";
2571 status = nfs4_handle_migration(clp);
2576 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
2577 section = "lease moved";
2578 status = nfs4_handle_lease_moved(clp);
2583 /* First recover reboot state... */
2584 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2585 section = "reclaim reboot";
2586 status = nfs4_do_reclaim(clp,
2587 clp->cl_mvops->reboot_recovery_ops);
2588 if (status == -EAGAIN)
2592 nfs4_state_end_reclaim_reboot(clp);
2596 /* Detect expired delegations... */
2597 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
2598 section = "detect expired delegations";
2599 nfs_reap_expired_delegations(clp);
2603 /* Now recover expired state... */
2604 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2605 section = "reclaim nograce";
2606 status = nfs4_do_reclaim(clp,
2607 clp->cl_mvops->nograce_recovery_ops);
2608 if (status == -EAGAIN)
2614 memalloc_nofs_restore(memflags);
2615 nfs4_end_drain_session(clp);
2616 nfs4_clear_state_manager_bit(clp);
2618 if (!test_and_set_bit(NFS4CLNT_DELEGRETURN_RUNNING, &clp->cl_state)) {
2619 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2620 nfs_client_return_marked_delegations(clp);
2621 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2623 clear_bit(NFS4CLNT_DELEGRETURN_RUNNING, &clp->cl_state);
2626 /* Did we race with an attempt to give us more work? */
2627 if (!test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state))
2629 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2631 memflags = memalloc_nofs_save();
2632 } while (refcount_read(&clp->cl_count) > 1 && !signalled());
2636 if (strlen(section))
2638 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2639 " with error %d\n", section_sep, section,
2640 clp->cl_hostname, -status);
2643 memalloc_nofs_restore(memflags);
2644 nfs4_end_drain_session(clp);
2645 nfs4_clear_state_manager_bit(clp);
2648 static int nfs4_run_state_manager(void *ptr)
2650 struct nfs_client *clp = ptr;
2652 allow_signal(SIGKILL);
2653 nfs4_state_manager(clp);
2654 nfs_put_client(clp);
2655 module_put_and_exit(0);