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 if (!nfs_client_init_is_complete(clp))
1213 nfs_mark_client_ready(clp, PTR_ERR(task));
1214 nfs4_clear_state_manager_bit(clp);
1215 nfs_put_client(clp);
1216 module_put(THIS_MODULE);
1221 * Schedule a lease recovery attempt
1223 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1227 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1228 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1229 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1231 nfs4_schedule_state_manager(clp);
1233 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1236 * nfs4_schedule_migration_recovery - trigger migration recovery
1238 * @server: FSID that is migrating
1240 * Returns zero if recovery has started, otherwise a negative NFS4ERR
1241 * value is returned.
1243 int nfs4_schedule_migration_recovery(const struct nfs_server *server)
1245 struct nfs_client *clp = server->nfs_client;
1247 if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
1248 pr_err("NFS: volatile file handles not supported (server %s)\n",
1253 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
1256 dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1258 (unsigned long long)server->fsid.major,
1259 (unsigned long long)server->fsid.minor,
1262 set_bit(NFS_MIG_IN_TRANSITION,
1263 &((struct nfs_server *)server)->mig_status);
1264 set_bit(NFS4CLNT_MOVED, &clp->cl_state);
1266 nfs4_schedule_state_manager(clp);
1269 EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
1272 * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1274 * @clp: server to check for moved leases
1277 void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
1279 dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1280 __func__, clp->cl_clientid, clp->cl_hostname);
1282 set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
1283 nfs4_schedule_state_manager(clp);
1285 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
1287 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1293 atomic_inc(&clp->cl_count);
1294 res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1295 nfs_wait_bit_killable, TASK_KILLABLE);
1298 if (clp->cl_cons_state < 0)
1299 res = clp->cl_cons_state;
1301 nfs_put_client(clp);
1305 int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1310 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1311 ret = nfs4_wait_clnt_recover(clp);
1314 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1315 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1317 nfs4_schedule_state_manager(clp);
1324 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1325 * @clp: client to process
1327 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1328 * resend of the SETCLIENTID and hence re-establish the
1329 * callback channel. Then return all existing delegations.
1331 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1333 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1334 nfs_expire_all_delegations(clp);
1335 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1339 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1341 nfs40_handle_cb_pathdown(clp);
1342 nfs4_schedule_state_manager(clp);
1345 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1348 if (!nfs4_valid_open_stateid(state))
1350 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1351 /* Don't recover state that expired before the reboot */
1352 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1353 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1356 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1357 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1361 int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1363 if (!nfs4_valid_open_stateid(state))
1365 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1366 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1367 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1368 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1372 int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1374 struct nfs_client *clp = server->nfs_client;
1376 if (!nfs4_state_mark_reclaim_nograce(clp, state))
1378 nfs_inode_find_delegation_state_and_recover(state->inode,
1380 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1382 nfs4_schedule_state_manager(clp);
1385 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1387 static struct nfs4_lock_state *
1388 nfs_state_find_lock_state_by_stateid(struct nfs4_state *state,
1389 const nfs4_stateid *stateid)
1391 struct nfs4_lock_state *pos;
1393 list_for_each_entry(pos, &state->lock_states, ls_locks) {
1394 if (!test_bit(NFS_LOCK_INITIALIZED, &pos->ls_flags))
1396 if (nfs4_stateid_match_other(&pos->ls_stateid, stateid))
1402 static bool nfs_state_lock_state_matches_stateid(struct nfs4_state *state,
1403 const nfs4_stateid *stateid)
1407 if (test_bit(LK_STATE_IN_USE, &state->flags)) {
1408 spin_lock(&state->state_lock);
1409 if (nfs_state_find_lock_state_by_stateid(state, stateid))
1411 spin_unlock(&state->state_lock);
1416 void nfs_inode_find_state_and_recover(struct inode *inode,
1417 const nfs4_stateid *stateid)
1419 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1420 struct nfs_inode *nfsi = NFS_I(inode);
1421 struct nfs_open_context *ctx;
1422 struct nfs4_state *state;
1425 spin_lock(&inode->i_lock);
1426 list_for_each_entry(ctx, &nfsi->open_files, list) {
1430 if (nfs4_stateid_match_other(&state->stateid, stateid) &&
1431 nfs4_state_mark_reclaim_nograce(clp, state)) {
1435 if (nfs_state_lock_state_matches_stateid(state, stateid) &&
1436 nfs4_state_mark_reclaim_nograce(clp, state))
1439 spin_unlock(&inode->i_lock);
1441 nfs_inode_find_delegation_state_and_recover(inode, stateid);
1443 nfs4_schedule_state_manager(clp);
1446 static void nfs4_state_mark_open_context_bad(struct nfs4_state *state)
1448 struct inode *inode = state->inode;
1449 struct nfs_inode *nfsi = NFS_I(inode);
1450 struct nfs_open_context *ctx;
1452 spin_lock(&inode->i_lock);
1453 list_for_each_entry(ctx, &nfsi->open_files, list) {
1454 if (ctx->state != state)
1456 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1458 spin_unlock(&inode->i_lock);
1461 static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1463 set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
1464 nfs4_state_mark_open_context_bad(state);
1468 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1470 struct inode *inode = state->inode;
1471 struct nfs_inode *nfsi = NFS_I(inode);
1472 struct file_lock *fl;
1473 struct nfs4_lock_state *lsp;
1475 struct file_lock_context *flctx = inode->i_flctx;
1476 struct list_head *list;
1481 list = &flctx->flc_posix;
1483 /* Guard against delegation returns and new lock/unlock calls */
1484 down_write(&nfsi->rwsem);
1485 spin_lock(&flctx->flc_lock);
1487 list_for_each_entry(fl, list, fl_list) {
1488 if (nfs_file_open_context(fl->fl_file)->state != state)
1490 spin_unlock(&flctx->flc_lock);
1491 status = ops->recover_lock(state, fl);
1496 case -NFS4ERR_ADMIN_REVOKED:
1497 case -NFS4ERR_STALE_STATEID:
1498 case -NFS4ERR_BAD_STATEID:
1499 case -NFS4ERR_EXPIRED:
1500 case -NFS4ERR_NO_GRACE:
1501 case -NFS4ERR_STALE_CLIENTID:
1502 case -NFS4ERR_BADSESSION:
1503 case -NFS4ERR_BADSLOT:
1504 case -NFS4ERR_BAD_HIGH_SLOT:
1505 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1508 pr_err("NFS: %s: unhandled error %d\n",
1511 case -NFS4ERR_DENIED:
1512 case -NFS4ERR_RECLAIM_BAD:
1513 case -NFS4ERR_RECLAIM_CONFLICT:
1514 lsp = fl->fl_u.nfs4_fl.owner;
1516 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1519 spin_lock(&flctx->flc_lock);
1521 if (list == &flctx->flc_posix) {
1522 list = &flctx->flc_flock;
1525 spin_unlock(&flctx->flc_lock);
1527 up_write(&nfsi->rwsem);
1531 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1533 struct nfs4_state *state;
1534 struct nfs4_lock_state *lock;
1537 /* Note: we rely on the sp->so_states list being ordered
1538 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1540 * This is needed to ensure that the server won't give us any
1541 * read delegations that we have to return if, say, we are
1542 * recovering after a network partition or a reboot from a
1543 * server that doesn't support a grace period.
1545 spin_lock(&sp->so_lock);
1546 raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
1548 list_for_each_entry(state, &sp->so_states, open_states) {
1549 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1551 if (!nfs4_valid_open_stateid(state))
1553 if (state->state == 0)
1555 atomic_inc(&state->count);
1556 spin_unlock(&sp->so_lock);
1557 status = ops->recover_open(sp, state);
1559 status = nfs4_reclaim_locks(state, ops);
1561 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1562 spin_lock(&state->state_lock);
1563 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1564 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1565 pr_warn_ratelimited("NFS: "
1567 "failed!\n", __func__);
1569 spin_unlock(&state->state_lock);
1571 clear_bit(NFS_STATE_RECLAIM_NOGRACE,
1573 nfs4_put_open_state(state);
1574 spin_lock(&sp->so_lock);
1580 printk(KERN_ERR "NFS: %s: unhandled error %d\n",
1588 /* Open state on this file cannot be recovered */
1589 nfs4_state_mark_recovery_failed(state, status);
1593 case -NFS4ERR_ADMIN_REVOKED:
1594 case -NFS4ERR_STALE_STATEID:
1595 case -NFS4ERR_OLD_STATEID:
1596 case -NFS4ERR_BAD_STATEID:
1597 case -NFS4ERR_RECLAIM_BAD:
1598 case -NFS4ERR_RECLAIM_CONFLICT:
1599 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1601 case -NFS4ERR_EXPIRED:
1602 case -NFS4ERR_NO_GRACE:
1603 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1604 case -NFS4ERR_STALE_CLIENTID:
1605 case -NFS4ERR_BADSESSION:
1606 case -NFS4ERR_BADSLOT:
1607 case -NFS4ERR_BAD_HIGH_SLOT:
1608 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1611 nfs4_put_open_state(state);
1612 spin_lock(&sp->so_lock);
1615 raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1616 spin_unlock(&sp->so_lock);
1619 nfs4_put_open_state(state);
1620 spin_lock(&sp->so_lock);
1621 raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1622 spin_unlock(&sp->so_lock);
1626 static void nfs4_clear_open_state(struct nfs4_state *state)
1628 struct nfs4_lock_state *lock;
1630 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1631 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1632 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1633 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1634 spin_lock(&state->state_lock);
1635 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1636 lock->ls_seqid.flags = 0;
1637 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1639 spin_unlock(&state->state_lock);
1642 static void nfs4_reset_seqids(struct nfs_server *server,
1643 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1645 struct nfs_client *clp = server->nfs_client;
1646 struct nfs4_state_owner *sp;
1647 struct rb_node *pos;
1648 struct nfs4_state *state;
1650 spin_lock(&clp->cl_lock);
1651 for (pos = rb_first(&server->state_owners);
1653 pos = rb_next(pos)) {
1654 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1655 sp->so_seqid.flags = 0;
1656 spin_lock(&sp->so_lock);
1657 list_for_each_entry(state, &sp->so_states, open_states) {
1658 if (mark_reclaim(clp, state))
1659 nfs4_clear_open_state(state);
1661 spin_unlock(&sp->so_lock);
1663 spin_unlock(&clp->cl_lock);
1666 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1667 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1669 struct nfs_server *server;
1672 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1673 nfs4_reset_seqids(server, mark_reclaim);
1677 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1679 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1680 /* Mark all delegations for reclaim */
1681 nfs_delegation_mark_reclaim(clp);
1682 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1685 static int nfs4_reclaim_complete(struct nfs_client *clp,
1686 const struct nfs4_state_recovery_ops *ops,
1687 struct rpc_cred *cred)
1689 /* Notify the server we're done reclaiming our state */
1690 if (ops->reclaim_complete)
1691 return ops->reclaim_complete(clp, cred);
1695 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1697 struct nfs_client *clp = server->nfs_client;
1698 struct nfs4_state_owner *sp;
1699 struct rb_node *pos;
1700 struct nfs4_state *state;
1702 spin_lock(&clp->cl_lock);
1703 for (pos = rb_first(&server->state_owners);
1705 pos = rb_next(pos)) {
1706 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1707 spin_lock(&sp->so_lock);
1708 list_for_each_entry(state, &sp->so_states, open_states) {
1709 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1712 nfs4_state_mark_reclaim_nograce(clp, state);
1714 spin_unlock(&sp->so_lock);
1716 spin_unlock(&clp->cl_lock);
1719 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1721 struct nfs_server *server;
1723 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1727 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1728 nfs4_clear_reclaim_server(server);
1731 nfs_delegation_reap_unclaimed(clp);
1735 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1737 const struct nfs4_state_recovery_ops *ops;
1738 struct rpc_cred *cred;
1741 if (!nfs4_state_clear_reclaim_reboot(clp))
1743 ops = clp->cl_mvops->reboot_recovery_ops;
1744 cred = nfs4_get_clid_cred(clp);
1745 err = nfs4_reclaim_complete(clp, ops, cred);
1747 if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
1748 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1751 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1753 nfs_mark_test_expired_all_delegations(clp);
1754 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1757 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1762 case -NFS4ERR_CB_PATH_DOWN:
1763 nfs40_handle_cb_pathdown(clp);
1765 case -NFS4ERR_NO_GRACE:
1766 nfs4_state_end_reclaim_reboot(clp);
1768 case -NFS4ERR_STALE_CLIENTID:
1769 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1770 nfs4_state_start_reclaim_reboot(clp);
1772 case -NFS4ERR_EXPIRED:
1773 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1774 nfs4_state_start_reclaim_nograce(clp);
1776 case -NFS4ERR_BADSESSION:
1777 case -NFS4ERR_BADSLOT:
1778 case -NFS4ERR_BAD_HIGH_SLOT:
1779 case -NFS4ERR_DEADSESSION:
1780 case -NFS4ERR_SEQ_FALSE_RETRY:
1781 case -NFS4ERR_SEQ_MISORDERED:
1782 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1783 /* Zero session reset errors */
1785 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1786 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1789 dprintk("%s: failed to handle error %d for server %s\n",
1790 __func__, error, clp->cl_hostname);
1793 dprintk("%s: handled error %d for server %s\n", __func__, error,
1798 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1800 struct nfs4_state_owner *sp;
1801 struct nfs_server *server;
1802 struct rb_node *pos;
1808 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1809 nfs4_purge_state_owners(server, &freeme);
1810 spin_lock(&clp->cl_lock);
1811 for (pos = rb_first(&server->state_owners);
1813 pos = rb_next(pos)) {
1815 struct nfs4_state_owner, so_server_node);
1816 if (!test_and_clear_bit(ops->owner_flag_bit,
1819 if (!atomic_inc_not_zero(&sp->so_count))
1821 spin_unlock(&clp->cl_lock);
1824 status = nfs4_reclaim_open_state(sp, ops);
1826 set_bit(ops->owner_flag_bit, &sp->so_flags);
1827 nfs4_put_state_owner(sp);
1828 status = nfs4_recovery_handle_error(clp, status);
1829 return (status != 0) ? status : -EAGAIN;
1832 nfs4_put_state_owner(sp);
1835 spin_unlock(&clp->cl_lock);
1838 nfs4_free_state_owners(&freeme);
1842 static int nfs4_check_lease(struct nfs_client *clp)
1844 struct rpc_cred *cred;
1845 const struct nfs4_state_maintenance_ops *ops =
1846 clp->cl_mvops->state_renewal_ops;
1849 /* Is the client already known to have an expired lease? */
1850 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1852 spin_lock(&clp->cl_lock);
1853 cred = ops->get_state_renewal_cred_locked(clp);
1854 spin_unlock(&clp->cl_lock);
1856 cred = nfs4_get_clid_cred(clp);
1861 status = ops->renew_lease(clp, cred);
1863 if (status == -ETIMEDOUT) {
1864 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1868 return nfs4_recovery_handle_error(clp, status);
1871 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1872 * and for recoverable errors on EXCHANGE_ID for v4.1
1874 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1877 case -NFS4ERR_SEQ_MISORDERED:
1878 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1879 return -ESERVERFAULT;
1880 /* Lease confirmation error: retry after purging the lease */
1882 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1884 case -NFS4ERR_STALE_CLIENTID:
1885 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1886 nfs4_state_start_reclaim_reboot(clp);
1888 case -NFS4ERR_CLID_INUSE:
1889 pr_err("NFS: Server %s reports our clientid is in use\n",
1891 nfs_mark_client_ready(clp, -EPERM);
1892 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1895 case -NFS4ERR_DELAY:
1901 case -NFS4ERR_MINOR_VERS_MISMATCH:
1902 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1903 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1904 dprintk("%s: exit with error %d for server %s\n",
1905 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1906 return -EPROTONOSUPPORT;
1907 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1908 * in nfs4_exchange_id */
1910 dprintk("%s: exit with error %d for server %s\n", __func__,
1911 status, clp->cl_hostname);
1914 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1915 dprintk("%s: handled error %d for server %s\n", __func__, status,
1920 static int nfs4_establish_lease(struct nfs_client *clp)
1922 struct rpc_cred *cred;
1923 const struct nfs4_state_recovery_ops *ops =
1924 clp->cl_mvops->reboot_recovery_ops;
1927 nfs4_begin_drain_session(clp);
1928 cred = nfs4_get_clid_cred(clp);
1931 status = ops->establish_clid(clp, cred);
1935 pnfs_destroy_all_layouts(clp);
1940 * Returns zero or a negative errno. NFS4ERR values are converted
1941 * to local errno values.
1943 static int nfs4_reclaim_lease(struct nfs_client *clp)
1947 status = nfs4_establish_lease(clp);
1949 return nfs4_handle_reclaim_lease_error(clp, status);
1950 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1951 nfs4_state_start_reclaim_nograce(clp);
1952 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1953 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1954 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1955 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1959 static int nfs4_purge_lease(struct nfs_client *clp)
1963 status = nfs4_establish_lease(clp);
1965 return nfs4_handle_reclaim_lease_error(clp, status);
1966 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1967 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1968 nfs4_state_start_reclaim_nograce(clp);
1973 * Try remote migration of one FSID from a source server to a
1974 * destination server. The source server provides a list of
1975 * potential destinations.
1977 * Returns zero or a negative NFS4ERR status code.
1979 static int nfs4_try_migration(struct nfs_server *server, struct rpc_cred *cred)
1981 struct nfs_client *clp = server->nfs_client;
1982 struct nfs4_fs_locations *locations = NULL;
1983 struct inode *inode;
1987 dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
1988 (unsigned long long)server->fsid.major,
1989 (unsigned long long)server->fsid.minor,
1993 page = alloc_page(GFP_KERNEL);
1994 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1995 if (page == NULL || locations == NULL) {
1996 dprintk("<-- %s: no memory\n", __func__);
2000 inode = d_inode(server->super->s_root);
2001 result = nfs4_proc_get_locations(inode, locations, page, cred);
2003 dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2008 result = -NFS4ERR_NXIO;
2009 if (!locations->nlocations)
2012 if (!(locations->fattr.valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
2013 dprintk("<-- %s: No fs_locations data, migration skipped\n",
2018 nfs4_begin_drain_session(clp);
2020 status = nfs4_replace_transport(server, locations);
2022 dprintk("<-- %s: failed to replace transport: %d\n",
2028 dprintk("<-- %s: migration succeeded\n", __func__);
2035 pr_err("NFS: migration recovery failed (server %s)\n",
2037 set_bit(NFS_MIG_FAILED, &server->mig_status);
2043 * Returns zero or a negative NFS4ERR status code.
2045 static int nfs4_handle_migration(struct nfs_client *clp)
2047 const struct nfs4_state_maintenance_ops *ops =
2048 clp->cl_mvops->state_renewal_ops;
2049 struct nfs_server *server;
2050 struct rpc_cred *cred;
2052 dprintk("%s: migration reported on \"%s\"\n", __func__,
2055 spin_lock(&clp->cl_lock);
2056 cred = ops->get_state_renewal_cred_locked(clp);
2057 spin_unlock(&clp->cl_lock);
2059 return -NFS4ERR_NOENT;
2064 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2067 if (server->mig_gen == clp->cl_mig_gen)
2069 server->mig_gen = clp->cl_mig_gen;
2071 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
2072 &server->mig_status))
2076 status = nfs4_try_migration(server, cred);
2089 * Test each nfs_server on the clp's cl_superblocks list to see
2090 * if it's moved to another server. Stop when the server no longer
2091 * returns NFS4ERR_LEASE_MOVED.
2093 static int nfs4_handle_lease_moved(struct nfs_client *clp)
2095 const struct nfs4_state_maintenance_ops *ops =
2096 clp->cl_mvops->state_renewal_ops;
2097 struct nfs_server *server;
2098 struct rpc_cred *cred;
2100 dprintk("%s: lease moved reported on \"%s\"\n", __func__,
2103 spin_lock(&clp->cl_lock);
2104 cred = ops->get_state_renewal_cred_locked(clp);
2105 spin_unlock(&clp->cl_lock);
2107 return -NFS4ERR_NOENT;
2112 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2113 struct inode *inode;
2116 if (server->mig_gen == clp->cl_mig_gen)
2118 server->mig_gen = clp->cl_mig_gen;
2122 inode = d_inode(server->super->s_root);
2123 status = nfs4_proc_fsid_present(inode, cred);
2124 if (status != -NFS4ERR_MOVED)
2125 goto restart; /* wasn't this one */
2126 if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
2127 goto restart; /* there are more */
2138 * nfs4_discover_server_trunking - Detect server IP address trunking
2140 * @clp: nfs_client under test
2141 * @result: OUT: found nfs_client, or clp
2143 * Returns zero or a negative errno. If zero is returned,
2144 * an nfs_client pointer is planted in "result".
2146 * Note: since we are invoked in process context, and
2147 * not from inside the state manager, we cannot use
2148 * nfs4_handle_reclaim_lease_error().
2150 int nfs4_discover_server_trunking(struct nfs_client *clp,
2151 struct nfs_client **result)
2153 const struct nfs4_state_recovery_ops *ops =
2154 clp->cl_mvops->reboot_recovery_ops;
2155 struct rpc_clnt *clnt;
2156 struct rpc_cred *cred;
2159 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
2161 clnt = clp->cl_rpcclient;
2164 mutex_lock(&nfs_clid_init_mutex);
2167 cred = nfs4_get_clid_cred(clp);
2171 status = ops->detect_trunking(clp, result, cred);
2179 if (clnt->cl_softrtry)
2181 case -NFS4ERR_DELAY:
2184 case -NFS4ERR_STALE_CLIENTID:
2185 dprintk("NFS: %s after status %d, retrying\n",
2190 nfs4_root_machine_cred(clp);
2193 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
2195 case -NFS4ERR_CLID_INUSE:
2196 case -NFS4ERR_WRONGSEC:
2197 /* No point in retrying if we already used RPC_AUTH_UNIX */
2198 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
2202 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
2204 status = PTR_ERR(clnt);
2207 /* Note: this is safe because we haven't yet marked the
2208 * client as ready, so we are the only user of
2211 clnt = xchg(&clp->cl_rpcclient, clnt);
2212 rpc_shutdown_client(clnt);
2213 clnt = clp->cl_rpcclient;
2216 case -NFS4ERR_MINOR_VERS_MISMATCH:
2217 status = -EPROTONOSUPPORT;
2221 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2222 * in nfs4_exchange_id */
2223 status = -EKEYEXPIRED;
2226 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2232 mutex_unlock(&nfs_clid_init_mutex);
2233 dprintk("NFS: %s: status = %d\n", __func__, status);
2237 #ifdef CONFIG_NFS_V4_1
2238 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
2240 struct nfs_client *clp = session->clp;
2244 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2246 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2247 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2249 nfs4_schedule_state_manager(clp);
2251 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
2253 void nfs41_notify_server(struct nfs_client *clp)
2255 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
2256 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2257 nfs4_schedule_state_manager(clp);
2260 static void nfs4_reset_all_state(struct nfs_client *clp)
2262 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2263 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2264 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2265 nfs4_state_start_reclaim_nograce(clp);
2266 dprintk("%s: scheduling reset of all state for server %s!\n",
2267 __func__, clp->cl_hostname);
2268 nfs4_schedule_state_manager(clp);
2272 static void nfs41_handle_server_reboot(struct nfs_client *clp)
2274 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2275 nfs4_state_start_reclaim_reboot(clp);
2276 dprintk("%s: server %s rebooted!\n", __func__,
2278 nfs4_schedule_state_manager(clp);
2282 static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
2284 nfs4_reset_all_state(clp);
2285 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2288 static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
2290 nfs4_state_start_reclaim_nograce(clp);
2291 nfs4_schedule_state_manager(clp);
2293 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2296 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
2298 /* FIXME: For now, we destroy all layouts. */
2299 pnfs_destroy_all_layouts(clp);
2300 /* FIXME: For now, we test all delegations+open state+locks. */
2301 nfs41_handle_some_state_revoked(clp);
2302 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2306 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
2308 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2309 nfs4_schedule_state_manager(clp);
2311 dprintk("%s: server %s declared a backchannel fault\n", __func__,
2315 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2317 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2318 &clp->cl_state) == 0)
2319 nfs4_schedule_state_manager(clp);
2322 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
2328 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2329 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2331 * If we're called from the state manager thread, then assume we're
2332 * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2333 * Those flags are expected to remain set until we're done
2334 * recovering (see RFC5661, section 18.46.3).
2339 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2340 nfs41_handle_server_reboot(clp);
2341 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
2342 nfs41_handle_all_state_revoked(clp);
2343 if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2344 SEQ4_STATUS_ADMIN_STATE_REVOKED))
2345 nfs41_handle_some_state_revoked(clp);
2346 if (flags & SEQ4_STATUS_LEASE_MOVED)
2347 nfs4_schedule_lease_moved_recovery(clp);
2348 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2349 nfs41_handle_recallable_state_revoked(clp);
2351 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2352 nfs41_handle_backchannel_fault(clp);
2353 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2354 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2355 nfs41_handle_cb_path_down(clp);
2358 static int nfs4_reset_session(struct nfs_client *clp)
2360 struct rpc_cred *cred;
2363 if (!nfs4_has_session(clp))
2365 nfs4_begin_drain_session(clp);
2366 cred = nfs4_get_clid_cred(clp);
2367 status = nfs4_proc_destroy_session(clp->cl_session, cred);
2370 case -NFS4ERR_BADSESSION:
2371 case -NFS4ERR_DEADSESSION:
2373 case -NFS4ERR_BACK_CHAN_BUSY:
2374 case -NFS4ERR_DELAY:
2375 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2380 status = nfs4_recovery_handle_error(clp, status);
2384 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2385 status = nfs4_proc_create_session(clp, cred);
2387 dprintk("%s: session reset failed with status %d for server %s!\n",
2388 __func__, status, clp->cl_hostname);
2389 status = nfs4_handle_reclaim_lease_error(clp, status);
2392 nfs41_finish_session_reset(clp);
2393 dprintk("%s: session reset was successful for server %s!\n",
2394 __func__, clp->cl_hostname);
2401 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2403 struct rpc_cred *cred;
2406 if (!nfs4_has_session(clp))
2408 nfs4_begin_drain_session(clp);
2409 cred = nfs4_get_clid_cred(clp);
2410 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2413 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2416 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2417 __func__, clp->cl_hostname);
2419 case -NFS4ERR_DELAY:
2421 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2424 return nfs4_recovery_handle_error(clp, ret);
2428 #else /* CONFIG_NFS_V4_1 */
2429 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2431 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2435 #endif /* CONFIG_NFS_V4_1 */
2437 static void nfs4_state_manager(struct nfs_client *clp)
2440 const char *section = "", *section_sep = "";
2442 /* Ensure exclusive access to NFSv4 state */
2444 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2445 section = "purge state";
2446 status = nfs4_purge_lease(clp);
2452 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2453 section = "lease expired";
2454 /* We're going to have to re-establish a clientid */
2455 status = nfs4_reclaim_lease(clp);
2461 /* Initialize or reset the session */
2462 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2463 section = "reset session";
2464 status = nfs4_reset_session(clp);
2465 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2471 /* Send BIND_CONN_TO_SESSION */
2472 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2474 section = "bind conn to session";
2475 status = nfs4_bind_conn_to_session(clp);
2481 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2482 section = "check lease";
2483 status = nfs4_check_lease(clp);
2489 if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
2490 section = "migration";
2491 status = nfs4_handle_migration(clp);
2496 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
2497 section = "lease moved";
2498 status = nfs4_handle_lease_moved(clp);
2503 /* First recover reboot state... */
2504 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2505 section = "reclaim reboot";
2506 status = nfs4_do_reclaim(clp,
2507 clp->cl_mvops->reboot_recovery_ops);
2508 if (status == -EAGAIN)
2512 nfs4_state_end_reclaim_reboot(clp);
2516 /* Detect expired delegations... */
2517 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
2518 section = "detect expired delegations";
2519 nfs_reap_expired_delegations(clp);
2523 /* Now recover expired state... */
2524 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2525 section = "reclaim nograce";
2526 status = nfs4_do_reclaim(clp,
2527 clp->cl_mvops->nograce_recovery_ops);
2528 if (status == -EAGAIN)
2534 nfs4_end_drain_session(clp);
2535 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2536 nfs_client_return_marked_delegations(clp);
2540 nfs4_clear_state_manager_bit(clp);
2541 /* Did we race with an attempt to give us more work? */
2542 if (clp->cl_state == 0)
2544 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2546 } while (atomic_read(&clp->cl_count) > 1);
2549 if (strlen(section))
2551 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2552 " with error %d\n", section_sep, section,
2553 clp->cl_hostname, -status);
2555 nfs4_end_drain_session(clp);
2556 nfs4_clear_state_manager_bit(clp);
2559 static int nfs4_run_state_manager(void *ptr)
2561 struct nfs_client *clp = ptr;
2563 allow_signal(SIGKILL);
2564 nfs4_state_manager(clp);
2565 nfs_put_client(clp);
2566 module_put_and_exit(0);