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"
64 #include "nfs4trace.h"
66 #define NFSDBG_FACILITY NFSDBG_STATE
68 #define OPENOWNER_POOL_SIZE 8
70 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp);
72 const nfs4_stateid zero_stateid = {
74 .type = NFS4_SPECIAL_STATEID_TYPE,
76 const nfs4_stateid invalid_stateid = {
78 /* Funky initialiser keeps older gcc versions happy */
79 .data = { 0xff, 0xff, 0xff, 0xff, 0 },
81 .type = NFS4_INVALID_STATEID_TYPE,
84 const nfs4_stateid current_stateid = {
86 /* Funky initialiser keeps older gcc versions happy */
87 .data = { 0x0, 0x0, 0x0, 0x1, 0 },
89 .type = NFS4_SPECIAL_STATEID_TYPE,
92 static DEFINE_MUTEX(nfs_clid_init_mutex);
94 static int nfs4_setup_state_renewal(struct nfs_client *clp)
97 struct nfs_fsinfo fsinfo;
99 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
100 nfs4_schedule_state_renewal(clp);
104 status = nfs4_proc_get_lease_time(clp, &fsinfo);
106 nfs4_set_lease_period(clp, fsinfo.lease_time * HZ);
107 nfs4_schedule_state_renewal(clp);
113 int nfs4_init_clientid(struct nfs_client *clp, const struct cred *cred)
115 struct nfs4_setclientid_res clid = {
116 .clientid = clp->cl_clientid,
117 .confirm = clp->cl_confirm,
121 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
123 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
125 port = nn->nfs_callback_tcpport;
126 if (clp->cl_addr.ss_family == AF_INET6)
127 port = nn->nfs_callback_tcpport6;
129 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
132 clp->cl_clientid = clid.clientid;
133 clp->cl_confirm = clid.confirm;
134 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
136 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
139 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
140 nfs4_setup_state_renewal(clp);
146 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
148 * @clp: nfs_client under test
149 * @result: OUT: found nfs_client, or clp
150 * @cred: credential to use for trunking test
152 * Returns zero, a negative errno, or a negative NFS4ERR status.
153 * If zero is returned, an nfs_client pointer is planted in
156 * Note: The returned client may not yet be marked ready.
158 int nfs40_discover_server_trunking(struct nfs_client *clp,
159 struct nfs_client **result,
160 const struct cred *cred)
162 struct nfs4_setclientid_res clid = {
163 .clientid = clp->cl_clientid,
164 .confirm = clp->cl_confirm,
166 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
170 port = nn->nfs_callback_tcpport;
171 if (clp->cl_addr.ss_family == AF_INET6)
172 port = nn->nfs_callback_tcpport6;
174 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
177 clp->cl_clientid = clid.clientid;
178 clp->cl_confirm = clid.confirm;
180 status = nfs40_walk_client_list(clp, result, cred);
182 /* Sustain the lease, even if it's empty. If the clientid4
183 * goes stale it's of no use for trunking discovery. */
184 nfs4_schedule_state_renewal(*result);
186 /* If the client state need to recover, do it. */
188 nfs4_schedule_state_manager(clp);
194 const struct cred *nfs4_get_machine_cred(struct nfs_client *clp)
196 return get_cred(rpc_machine_cred());
199 static void nfs4_root_machine_cred(struct nfs_client *clp)
202 /* Force root creds instead of machine */
203 clp->cl_principal = NULL;
204 clp->cl_rpcclient->cl_principal = NULL;
207 static const struct cred *
208 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
210 const struct cred *cred = NULL;
211 struct nfs4_state_owner *sp;
214 for (pos = rb_first(&server->state_owners);
216 pos = rb_next(pos)) {
217 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
218 if (list_empty(&sp->so_states))
220 cred = get_cred(sp->so_cred);
227 * nfs4_get_renew_cred - Acquire credential for a renew operation
228 * @clp: client state handle
230 * Returns an rpc_cred with reference count bumped, or NULL.
231 * Caller must hold clp->cl_lock.
233 const struct cred *nfs4_get_renew_cred(struct nfs_client *clp)
235 const struct cred *cred = NULL;
236 struct nfs_server *server;
238 /* Use machine credentials if available */
239 cred = nfs4_get_machine_cred(clp);
243 spin_lock(&clp->cl_lock);
245 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
246 cred = nfs4_get_renew_cred_server_locked(server);
251 spin_unlock(&clp->cl_lock);
257 static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
259 if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
260 spin_lock(&tbl->slot_tbl_lock);
261 nfs41_wake_slot_table(tbl);
262 spin_unlock(&tbl->slot_tbl_lock);
266 static void nfs4_end_drain_session(struct nfs_client *clp)
268 struct nfs4_session *ses = clp->cl_session;
270 if (clp->cl_slot_tbl) {
271 nfs4_end_drain_slot_table(clp->cl_slot_tbl);
276 nfs4_end_drain_slot_table(&ses->bc_slot_table);
277 nfs4_end_drain_slot_table(&ses->fc_slot_table);
281 static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
283 set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
284 spin_lock(&tbl->slot_tbl_lock);
285 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
286 reinit_completion(&tbl->complete);
287 spin_unlock(&tbl->slot_tbl_lock);
288 return wait_for_completion_interruptible(&tbl->complete);
290 spin_unlock(&tbl->slot_tbl_lock);
294 static int nfs4_begin_drain_session(struct nfs_client *clp)
296 struct nfs4_session *ses = clp->cl_session;
299 if (clp->cl_slot_tbl)
300 return nfs4_drain_slot_tbl(clp->cl_slot_tbl);
303 ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
307 return nfs4_drain_slot_tbl(&ses->fc_slot_table);
310 #if defined(CONFIG_NFS_V4_1)
312 static void nfs41_finish_session_reset(struct nfs_client *clp)
314 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
315 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
316 /* create_session negotiated new slot table */
317 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
318 nfs4_setup_state_renewal(clp);
321 int nfs41_init_clientid(struct nfs_client *clp, const struct cred *cred)
325 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
327 status = nfs4_proc_exchange_id(clp, cred);
330 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
332 status = nfs4_proc_create_session(clp, cred);
335 if (!(clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R))
336 nfs4_state_start_reclaim_reboot(clp);
337 nfs41_finish_session_reset(clp);
338 nfs_mark_client_ready(clp, NFS_CS_READY);
344 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
346 * @clp: nfs_client under test
347 * @result: OUT: found nfs_client, or clp
348 * @cred: credential to use for trunking test
350 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
351 * If NFS4_OK is returned, an nfs_client pointer is planted in
354 * Note: The returned client may not yet be marked ready.
356 int nfs41_discover_server_trunking(struct nfs_client *clp,
357 struct nfs_client **result,
358 const struct cred *cred)
362 status = nfs4_proc_exchange_id(clp, cred);
363 if (status != NFS4_OK)
366 status = nfs41_walk_client_list(clp, result, cred);
373 * Purge state if the client id was established in a prior
374 * instance and the client id could not have arrived on the
375 * server via Transparent State Migration.
377 if (clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R) {
378 if (!test_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags))
379 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
381 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
383 nfs4_schedule_state_manager(clp);
384 status = nfs_wait_client_init_complete(clp);
390 #endif /* CONFIG_NFS_V4_1 */
393 * nfs4_get_clid_cred - Acquire credential for a setclientid operation
394 * @clp: client state handle
396 * Returns a cred with reference count bumped, or NULL.
398 const struct cred *nfs4_get_clid_cred(struct nfs_client *clp)
400 const struct cred *cred;
402 cred = nfs4_get_machine_cred(clp);
406 static struct nfs4_state_owner *
407 nfs4_find_state_owner_locked(struct nfs_server *server, const struct cred *cred)
409 struct rb_node **p = &server->state_owners.rb_node,
411 struct nfs4_state_owner *sp;
416 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
417 cmp = cred_fscmp(cred, sp->so_cred);
420 p = &parent->rb_left;
422 p = &parent->rb_right;
424 if (!list_empty(&sp->so_lru))
425 list_del_init(&sp->so_lru);
426 atomic_inc(&sp->so_count);
433 static struct nfs4_state_owner *
434 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
436 struct nfs_server *server = new->so_server;
437 struct rb_node **p = &server->state_owners.rb_node,
439 struct nfs4_state_owner *sp;
444 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
445 cmp = cred_fscmp(new->so_cred, sp->so_cred);
448 p = &parent->rb_left;
450 p = &parent->rb_right;
452 if (!list_empty(&sp->so_lru))
453 list_del_init(&sp->so_lru);
454 atomic_inc(&sp->so_count);
458 rb_link_node(&new->so_server_node, parent, p);
459 rb_insert_color(&new->so_server_node, &server->state_owners);
464 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
466 struct nfs_server *server = sp->so_server;
468 if (!RB_EMPTY_NODE(&sp->so_server_node))
469 rb_erase(&sp->so_server_node, &server->state_owners);
473 nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
475 sc->create_time = ktime_get();
478 spin_lock_init(&sc->lock);
479 INIT_LIST_HEAD(&sc->list);
480 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
484 nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
486 rpc_destroy_wait_queue(&sc->wait);
490 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
491 * create a new state_owner.
494 static struct nfs4_state_owner *
495 nfs4_alloc_state_owner(struct nfs_server *server,
496 const struct cred *cred,
499 struct nfs4_state_owner *sp;
501 sp = kzalloc(sizeof(*sp), gfp_flags);
504 sp->so_seqid.owner_id = ida_alloc(&server->openowner_id, gfp_flags);
505 if (sp->so_seqid.owner_id < 0) {
509 sp->so_server = server;
510 sp->so_cred = get_cred(cred);
511 spin_lock_init(&sp->so_lock);
512 INIT_LIST_HEAD(&sp->so_states);
513 nfs4_init_seqid_counter(&sp->so_seqid);
514 atomic_set(&sp->so_count, 1);
515 INIT_LIST_HEAD(&sp->so_lru);
516 mutex_init(&sp->so_delegreturn_mutex);
521 nfs4_reset_state_owner(struct nfs4_state_owner *sp)
523 /* This state_owner is no longer usable, but must
524 * remain in place so that state recovery can find it
525 * and the opens associated with it.
526 * It may also be used for new 'open' request to
527 * return a delegation to the server.
528 * So update the 'create_time' so that it looks like
529 * a new state_owner. This will cause the server to
530 * request an OPEN_CONFIRM to start a new sequence.
532 sp->so_seqid.create_time = ktime_get();
535 static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
537 nfs4_destroy_seqid_counter(&sp->so_seqid);
538 put_cred(sp->so_cred);
539 ida_free(&sp->so_server->openowner_id, sp->so_seqid.owner_id);
543 static void nfs4_gc_state_owners(struct nfs_server *server)
545 struct nfs_client *clp = server->nfs_client;
546 struct nfs4_state_owner *sp, *tmp;
547 unsigned long time_min, time_max;
550 spin_lock(&clp->cl_lock);
552 time_min = (long)time_max - (long)clp->cl_lease_time;
553 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
554 /* NB: LRU is sorted so that oldest is at the head */
555 if (time_in_range(sp->so_expires, time_min, time_max))
557 list_move(&sp->so_lru, &doomed);
558 nfs4_remove_state_owner_locked(sp);
560 spin_unlock(&clp->cl_lock);
562 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
563 list_del(&sp->so_lru);
564 nfs4_free_state_owner(sp);
569 * nfs4_get_state_owner - Look up a state owner given a credential
570 * @server: nfs_server to search
571 * @cred: RPC credential to match
572 * @gfp_flags: allocation mode
574 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
576 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
577 const struct cred *cred,
580 struct nfs_client *clp = server->nfs_client;
581 struct nfs4_state_owner *sp, *new;
583 spin_lock(&clp->cl_lock);
584 sp = nfs4_find_state_owner_locked(server, cred);
585 spin_unlock(&clp->cl_lock);
588 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
591 spin_lock(&clp->cl_lock);
592 sp = nfs4_insert_state_owner_locked(new);
593 spin_unlock(&clp->cl_lock);
595 nfs4_free_state_owner(new);
597 nfs4_gc_state_owners(server);
602 * nfs4_put_state_owner - Release a nfs4_state_owner
603 * @sp: state owner data to release
605 * Note that we keep released state owners on an LRU
607 * This caches valid state owners so that they can be
608 * reused, to avoid the OPEN_CONFIRM on minor version 0.
609 * It also pins the uniquifier of dropped state owners for
610 * a while, to ensure that those state owner names are
613 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
615 struct nfs_server *server = sp->so_server;
616 struct nfs_client *clp = server->nfs_client;
618 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
621 sp->so_expires = jiffies;
622 list_add_tail(&sp->so_lru, &server->state_owners_lru);
623 spin_unlock(&clp->cl_lock);
627 * nfs4_purge_state_owners - Release all cached state owners
628 * @server: nfs_server with cached state owners to release
629 * @head: resulting list of state owners
631 * Called at umount time. Remaining state owners will be on
632 * the LRU with ref count of zero.
633 * Note that the state owners are not freed, but are added
634 * to the list @head, which can later be used as an argument
635 * to nfs4_free_state_owners.
637 void nfs4_purge_state_owners(struct nfs_server *server, struct list_head *head)
639 struct nfs_client *clp = server->nfs_client;
640 struct nfs4_state_owner *sp, *tmp;
642 spin_lock(&clp->cl_lock);
643 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
644 list_move(&sp->so_lru, head);
645 nfs4_remove_state_owner_locked(sp);
647 spin_unlock(&clp->cl_lock);
651 * nfs4_free_state_owners - Release all cached state owners
652 * @head: resulting list of state owners
654 * Frees a list of state owners that was generated by
655 * nfs4_purge_state_owners
657 void nfs4_free_state_owners(struct list_head *head)
659 struct nfs4_state_owner *sp, *tmp;
661 list_for_each_entry_safe(sp, tmp, head, so_lru) {
662 list_del(&sp->so_lru);
663 nfs4_free_state_owner(sp);
667 static struct nfs4_state *
668 nfs4_alloc_open_state(void)
670 struct nfs4_state *state;
672 state = kzalloc(sizeof(*state), GFP_KERNEL_ACCOUNT);
675 refcount_set(&state->count, 1);
676 INIT_LIST_HEAD(&state->lock_states);
677 spin_lock_init(&state->state_lock);
678 seqlock_init(&state->seqlock);
679 init_waitqueue_head(&state->waitq);
684 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
686 if (state->state == fmode)
688 /* NB! List reordering - see the reclaim code for why. */
689 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
690 if (fmode & FMODE_WRITE)
691 list_move(&state->open_states, &state->owner->so_states);
693 list_move_tail(&state->open_states, &state->owner->so_states);
695 state->state = fmode;
698 static struct nfs4_state *
699 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
701 struct nfs_inode *nfsi = NFS_I(inode);
702 struct nfs4_state *state;
704 list_for_each_entry_rcu(state, &nfsi->open_states, inode_states) {
705 if (state->owner != owner)
707 if (!nfs4_valid_open_stateid(state))
709 if (refcount_inc_not_zero(&state->count))
716 nfs4_free_open_state(struct nfs4_state *state)
718 kfree_rcu(state, rcu_head);
722 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
724 struct nfs4_state *state, *new;
725 struct nfs_inode *nfsi = NFS_I(inode);
728 state = __nfs4_find_state_byowner(inode, owner);
732 new = nfs4_alloc_open_state();
733 spin_lock(&owner->so_lock);
734 spin_lock(&inode->i_lock);
735 state = __nfs4_find_state_byowner(inode, owner);
736 if (state == NULL && new != NULL) {
738 state->owner = owner;
739 atomic_inc(&owner->so_count);
741 state->inode = inode;
742 list_add_rcu(&state->inode_states, &nfsi->open_states);
743 spin_unlock(&inode->i_lock);
744 /* Note: The reclaim code dictates that we add stateless
745 * and read-only stateids to the end of the list */
746 list_add_tail(&state->open_states, &owner->so_states);
747 spin_unlock(&owner->so_lock);
749 spin_unlock(&inode->i_lock);
750 spin_unlock(&owner->so_lock);
752 nfs4_free_open_state(new);
758 void nfs4_put_open_state(struct nfs4_state *state)
760 struct inode *inode = state->inode;
761 struct nfs4_state_owner *owner = state->owner;
763 if (!refcount_dec_and_lock(&state->count, &owner->so_lock))
765 spin_lock(&inode->i_lock);
766 list_del_rcu(&state->inode_states);
767 list_del(&state->open_states);
768 spin_unlock(&inode->i_lock);
769 spin_unlock(&owner->so_lock);
770 nfs4_inode_return_delegation_on_close(inode);
772 nfs4_free_open_state(state);
773 nfs4_put_state_owner(owner);
777 * Close the current file.
779 static void __nfs4_close(struct nfs4_state *state,
780 fmode_t fmode, gfp_t gfp_mask, int wait)
782 struct nfs4_state_owner *owner = state->owner;
786 atomic_inc(&owner->so_count);
787 /* Protect against nfs4_find_state() */
788 spin_lock(&owner->so_lock);
789 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
796 case FMODE_READ|FMODE_WRITE:
799 newstate = FMODE_READ|FMODE_WRITE;
800 if (state->n_rdwr == 0) {
801 if (state->n_rdonly == 0) {
802 newstate &= ~FMODE_READ;
803 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
804 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
806 if (state->n_wronly == 0) {
807 newstate &= ~FMODE_WRITE;
808 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
809 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
812 clear_bit(NFS_DELEGATED_STATE, &state->flags);
814 nfs4_state_set_mode_locked(state, newstate);
815 spin_unlock(&owner->so_lock);
818 nfs4_put_open_state(state);
819 nfs4_put_state_owner(owner);
821 nfs4_do_close(state, gfp_mask, wait);
824 void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
826 __nfs4_close(state, fmode, GFP_KERNEL, 0);
829 void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
831 __nfs4_close(state, fmode, GFP_KERNEL, 1);
835 * Search the state->lock_states for an existing lock_owner
836 * that is compatible with either of the given owners.
837 * If the second is non-zero, then the first refers to a Posix-lock
838 * owner (current->files) and the second refers to a flock/OFD
839 * owner (struct file*). In that case, prefer a match for the first
841 * If both sorts of locks are held on the one file we cannot know
842 * which stateid was intended to be used, so a "correct" choice cannot
843 * be made. Failing that, a "consistent" choice is preferable. The
844 * consistent choice we make is to prefer the first owner, that of a
847 static struct nfs4_lock_state *
848 __nfs4_find_lock_state(struct nfs4_state *state,
849 fl_owner_t owner, fl_owner_t owner2)
851 struct nfs4_lock_state *pos, *ret = NULL;
852 list_for_each_entry(pos, &state->lock_states, ls_locks) {
853 if (pos->ls_owner == owner) {
857 if (pos->ls_owner == owner2)
861 refcount_inc(&ret->ls_count);
866 * Return a compatible lock_state. If no initialized lock_state structure
867 * exists, return an uninitialized one.
870 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t owner)
872 struct nfs4_lock_state *lsp;
873 struct nfs_server *server = state->owner->so_server;
875 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL_ACCOUNT);
878 nfs4_init_seqid_counter(&lsp->ls_seqid);
879 refcount_set(&lsp->ls_count, 1);
880 lsp->ls_state = state;
881 lsp->ls_owner = owner;
882 lsp->ls_seqid.owner_id = ida_alloc(&server->lockowner_id, GFP_KERNEL_ACCOUNT);
883 if (lsp->ls_seqid.owner_id < 0)
885 INIT_LIST_HEAD(&lsp->ls_locks);
892 void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
894 ida_free(&server->lockowner_id, lsp->ls_seqid.owner_id);
895 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
900 * Return a compatible lock_state. If no initialized lock_state structure
901 * exists, return an uninitialized one.
904 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
906 struct nfs4_lock_state *lsp, *new = NULL;
909 spin_lock(&state->state_lock);
910 lsp = __nfs4_find_lock_state(state, owner, NULL);
914 list_add(&new->ls_locks, &state->lock_states);
915 set_bit(LK_STATE_IN_USE, &state->flags);
920 spin_unlock(&state->state_lock);
921 new = nfs4_alloc_lock_state(state, owner);
925 spin_unlock(&state->state_lock);
927 nfs4_free_lock_state(state->owner->so_server, new);
932 * Release reference to lock_state, and free it if we see that
933 * it is no longer in use
935 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
937 struct nfs_server *server;
938 struct nfs4_state *state;
942 state = lsp->ls_state;
943 if (!refcount_dec_and_lock(&lsp->ls_count, &state->state_lock))
945 list_del(&lsp->ls_locks);
946 if (list_empty(&state->lock_states))
947 clear_bit(LK_STATE_IN_USE, &state->flags);
948 spin_unlock(&state->state_lock);
949 server = state->owner->so_server;
950 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
951 struct nfs_client *clp = server->nfs_client;
953 clp->cl_mvops->free_lock_state(server, lsp);
955 nfs4_free_lock_state(server, lsp);
958 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
960 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
962 dst->fl_u.nfs4_fl.owner = lsp;
963 refcount_inc(&lsp->ls_count);
966 static void nfs4_fl_release_lock(struct file_lock *fl)
968 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
971 static const struct file_lock_operations nfs4_fl_lock_ops = {
972 .fl_copy_lock = nfs4_fl_copy_lock,
973 .fl_release_private = nfs4_fl_release_lock,
976 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
978 struct nfs4_lock_state *lsp;
980 if (fl->fl_ops != NULL)
982 lsp = nfs4_get_lock_state(state, fl->c.flc_owner);
985 fl->fl_u.nfs4_fl.owner = lsp;
986 fl->fl_ops = &nfs4_fl_lock_ops;
990 static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
991 struct nfs4_state *state,
992 const struct nfs_lock_context *l_ctx)
994 struct nfs4_lock_state *lsp;
995 fl_owner_t owner, fl_flock_owner;
1001 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
1004 owner = l_ctx->lockowner;
1005 fl_flock_owner = l_ctx->open_context->flock_owner;
1007 spin_lock(&state->state_lock);
1008 lsp = __nfs4_find_lock_state(state, owner, fl_flock_owner);
1009 if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
1011 else if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
1012 nfs4_stateid_copy(dst, &lsp->ls_stateid);
1015 spin_unlock(&state->state_lock);
1016 nfs4_put_lock_state(lsp);
1021 bool nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1024 const nfs4_stateid *src;
1029 src = &zero_stateid;
1030 seq = read_seqbegin(&state->seqlock);
1031 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1032 src = &state->open_stateid;
1035 nfs4_stateid_copy(dst, src);
1036 } while (read_seqretry(&state->seqlock, seq));
1041 * Byte-range lock aware utility to initialize the stateid of read/write
1044 int nfs4_select_rw_stateid(struct nfs4_state *state,
1045 fmode_t fmode, const struct nfs_lock_context *l_ctx,
1046 nfs4_stateid *dst, const struct cred **cred)
1050 if (!nfs4_valid_open_stateid(state))
1054 ret = nfs4_copy_lock_stateid(dst, state, l_ctx);
1056 /* A lost lock - don't even consider delegations */
1058 /* returns true if delegation stateid found and copied */
1059 if (nfs4_copy_delegation_stateid(state->inode, fmode, dst, cred)) {
1064 /* nfs4_copy_delegation_stateid() didn't over-write
1065 * dst, so it still has the lock stateid which we now
1069 ret = nfs4_copy_open_stateid(dst, state) ? 0 : -EAGAIN;
1071 if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
1076 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1078 struct nfs_seqid *new;
1080 new = kmalloc(sizeof(*new), gfp_mask);
1082 return ERR_PTR(-ENOMEM);
1083 new->sequence = counter;
1084 INIT_LIST_HEAD(&new->list);
1089 void nfs_release_seqid(struct nfs_seqid *seqid)
1091 struct nfs_seqid_counter *sequence;
1093 if (seqid == NULL || list_empty(&seqid->list))
1095 sequence = seqid->sequence;
1096 spin_lock(&sequence->lock);
1097 list_del_init(&seqid->list);
1098 if (!list_empty(&sequence->list)) {
1099 struct nfs_seqid *next;
1101 next = list_first_entry(&sequence->list,
1102 struct nfs_seqid, list);
1103 rpc_wake_up_queued_task(&sequence->wait, next->task);
1105 spin_unlock(&sequence->lock);
1108 void nfs_free_seqid(struct nfs_seqid *seqid)
1110 nfs_release_seqid(seqid);
1115 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1116 * failed with a seqid incrementing error -
1117 * see comments nfs4.h:seqid_mutating_error()
1119 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1124 case -NFS4ERR_BAD_SEQID:
1125 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1127 pr_warn_ratelimited("NFS: v4 server returned a bad"
1128 " sequence-id error on an"
1129 " unconfirmed sequence %p!\n",
1132 case -NFS4ERR_STALE_CLIENTID:
1133 case -NFS4ERR_STALE_STATEID:
1134 case -NFS4ERR_BAD_STATEID:
1135 case -NFS4ERR_BADXDR:
1136 case -NFS4ERR_RESOURCE:
1137 case -NFS4ERR_NOFILEHANDLE:
1138 case -NFS4ERR_MOVED:
1139 /* Non-seqid mutating errors */
1143 * Note: no locking needed as we are guaranteed to be first
1144 * on the sequence list
1146 seqid->sequence->counter++;
1149 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1151 struct nfs4_state_owner *sp;
1156 sp = container_of(seqid->sequence, struct nfs4_state_owner, so_seqid);
1157 if (status == -NFS4ERR_BAD_SEQID)
1158 nfs4_reset_state_owner(sp);
1159 if (!nfs4_has_session(sp->so_server->nfs_client))
1160 nfs_increment_seqid(status, seqid);
1164 * Increment the seqid if the LOCK/LOCKU succeeded, or
1165 * failed with a seqid incrementing error -
1166 * see comments nfs4.h:seqid_mutating_error()
1168 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1171 nfs_increment_seqid(status, seqid);
1174 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1176 struct nfs_seqid_counter *sequence;
1181 sequence = seqid->sequence;
1182 spin_lock(&sequence->lock);
1184 if (list_empty(&seqid->list))
1185 list_add_tail(&seqid->list, &sequence->list);
1186 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1188 rpc_sleep_on(&sequence->wait, task, NULL);
1191 spin_unlock(&sequence->lock);
1196 static int nfs4_run_state_manager(void *);
1198 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1200 clear_and_wake_up_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1201 rpc_wake_up(&clp->cl_rpcwaitq);
1205 * Schedule the nfs_client asynchronous state management routine
1207 void nfs4_schedule_state_manager(struct nfs_client *clp)
1209 struct task_struct *task;
1210 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1211 struct rpc_clnt *clnt = clp->cl_rpcclient;
1212 bool swapon = false;
1214 if (clnt->cl_shutdown)
1217 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
1219 if (atomic_read(&clnt->cl_swapper)) {
1220 swapon = !test_and_set_bit(NFS4CLNT_MANAGER_AVAILABLE,
1223 wake_up_var(&clp->cl_state);
1228 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1231 __module_get(THIS_MODULE);
1232 refcount_inc(&clp->cl_count);
1234 /* The rcu_read_lock() is not strictly necessary, as the state
1235 * manager is the only thread that ever changes the rpc_xprt
1236 * after it's initialized. At this point, we're single threaded. */
1238 snprintf(buf, sizeof(buf), "%s-manager",
1239 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1241 task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
1243 printk(KERN_ERR "%s: kthread_run: %ld\n",
1244 __func__, PTR_ERR(task));
1245 if (!nfs_client_init_is_complete(clp))
1246 nfs_mark_client_ready(clp, PTR_ERR(task));
1248 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
1249 nfs4_clear_state_manager_bit(clp);
1250 nfs_put_client(clp);
1251 module_put(THIS_MODULE);
1256 * Schedule a lease recovery attempt
1258 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1262 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1263 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1264 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1266 nfs4_schedule_state_manager(clp);
1268 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1271 * nfs4_schedule_migration_recovery - trigger migration recovery
1273 * @server: FSID that is migrating
1275 * Returns zero if recovery has started, otherwise a negative NFS4ERR
1276 * value is returned.
1278 int nfs4_schedule_migration_recovery(const struct nfs_server *server)
1280 struct nfs_client *clp = server->nfs_client;
1282 if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
1283 pr_err("NFS: volatile file handles not supported (server %s)\n",
1288 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
1291 dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1293 (unsigned long long)server->fsid.major,
1294 (unsigned long long)server->fsid.minor,
1297 set_bit(NFS_MIG_IN_TRANSITION,
1298 &((struct nfs_server *)server)->mig_status);
1299 set_bit(NFS4CLNT_MOVED, &clp->cl_state);
1301 nfs4_schedule_state_manager(clp);
1304 EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
1307 * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1309 * @clp: server to check for moved leases
1312 void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
1314 dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1315 __func__, clp->cl_clientid, clp->cl_hostname);
1317 set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
1318 nfs4_schedule_state_manager(clp);
1320 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
1322 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1328 refcount_inc(&clp->cl_count);
1329 res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1330 nfs_wait_bit_killable,
1331 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
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_or_older(&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;
1462 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1466 if (nfs4_stateid_match_or_older(&state->stateid, stateid) &&
1467 nfs4_state_mark_reclaim_nograce(clp, state)) {
1471 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1472 nfs4_stateid_match_or_older(&state->open_stateid, stateid) &&
1473 nfs4_state_mark_reclaim_nograce(clp, state)) {
1477 if (nfs_state_lock_state_matches_stateid(state, stateid) &&
1478 nfs4_state_mark_reclaim_nograce(clp, state))
1483 nfs_inode_find_delegation_state_and_recover(inode, stateid);
1485 nfs4_schedule_state_manager(clp);
1488 static void nfs4_state_mark_open_context_bad(struct nfs4_state *state, int err)
1490 struct inode *inode = state->inode;
1491 struct nfs_inode *nfsi = NFS_I(inode);
1492 struct nfs_open_context *ctx;
1495 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1496 if (ctx->state != state)
1498 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1499 pr_warn("NFSv4: state recovery failed for open file %pd2, "
1500 "error = %d\n", ctx->dentry, err);
1505 static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1507 set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
1508 nfs4_state_mark_open_context_bad(state, error);
1512 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1514 struct inode *inode = state->inode;
1515 struct nfs_inode *nfsi = NFS_I(inode);
1516 struct file_lock *fl;
1517 struct nfs4_lock_state *lsp;
1519 struct file_lock_context *flctx = locks_inode_context(inode);
1520 struct list_head *list;
1525 list = &flctx->flc_posix;
1527 /* Guard against delegation returns and new lock/unlock calls */
1528 down_write(&nfsi->rwsem);
1529 spin_lock(&flctx->flc_lock);
1531 for_each_file_lock(fl, list) {
1532 if (nfs_file_open_context(fl->c.flc_file)->state != state)
1534 spin_unlock(&flctx->flc_lock);
1535 status = ops->recover_lock(state, fl);
1541 case -NFS4ERR_ADMIN_REVOKED:
1542 case -NFS4ERR_STALE_STATEID:
1543 case -NFS4ERR_BAD_STATEID:
1544 case -NFS4ERR_EXPIRED:
1545 case -NFS4ERR_NO_GRACE:
1546 case -NFS4ERR_STALE_CLIENTID:
1547 case -NFS4ERR_BADSESSION:
1548 case -NFS4ERR_BADSLOT:
1549 case -NFS4ERR_BAD_HIGH_SLOT:
1550 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1553 pr_err("NFS: %s: unhandled error %d\n",
1557 case -NFS4ERR_DENIED:
1558 case -NFS4ERR_RECLAIM_BAD:
1559 case -NFS4ERR_RECLAIM_CONFLICT:
1560 lsp = fl->fl_u.nfs4_fl.owner;
1562 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1565 spin_lock(&flctx->flc_lock);
1567 if (list == &flctx->flc_posix) {
1568 list = &flctx->flc_flock;
1571 spin_unlock(&flctx->flc_lock);
1573 up_write(&nfsi->rwsem);
1577 #ifdef CONFIG_NFS_V4_2
1578 static void nfs42_complete_copies(struct nfs4_state_owner *sp, struct nfs4_state *state)
1580 struct nfs4_copy_state *copy;
1582 if (!test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags) &&
1583 !test_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags))
1586 spin_lock(&sp->so_server->nfs_client->cl_lock);
1587 list_for_each_entry(copy, &sp->so_server->ss_copies, copies) {
1588 if ((test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags) &&
1589 !nfs4_stateid_match_other(&state->stateid,
1590 ©->parent_dst_state->stateid)))
1593 if (test_and_clear_bit(NFS_CLNT_DST_SSC_COPY_STATE,
1595 clear_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags);
1596 complete(©->completion);
1599 list_for_each_entry(copy, &sp->so_server->ss_copies, src_copies) {
1600 if ((test_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags) &&
1601 !nfs4_stateid_match_other(&state->stateid,
1602 ©->parent_src_state->stateid)))
1605 if (test_and_clear_bit(NFS_CLNT_DST_SSC_COPY_STATE,
1607 complete(©->completion);
1609 spin_unlock(&sp->so_server->nfs_client->cl_lock);
1611 #else /* !CONFIG_NFS_V4_2 */
1612 static inline void nfs42_complete_copies(struct nfs4_state_owner *sp,
1613 struct nfs4_state *state)
1616 #endif /* CONFIG_NFS_V4_2 */
1618 static int __nfs4_reclaim_open_state(struct nfs4_state_owner *sp, struct nfs4_state *state,
1619 const struct nfs4_state_recovery_ops *ops,
1622 struct nfs4_lock_state *lock;
1625 status = ops->recover_open(sp, state);
1629 status = nfs4_reclaim_locks(state, ops);
1633 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1634 spin_lock(&state->state_lock);
1635 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1636 trace_nfs4_state_lock_reclaim(state, lock);
1637 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags) &&
1638 !test_bit(NFS_LOCK_UNLOCKING, &lock->ls_flags))
1641 spin_unlock(&state->state_lock);
1644 nfs42_complete_copies(sp, state);
1645 clear_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1649 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp,
1650 const struct nfs4_state_recovery_ops *ops,
1653 struct nfs4_state *state;
1654 unsigned int loop = 0;
1656 #ifdef CONFIG_NFS_V4_2
1657 bool found_ssc_copy_state = false;
1658 #endif /* CONFIG_NFS_V4_2 */
1660 /* Note: we rely on the sp->so_states list being ordered
1661 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1663 * This is needed to ensure that the server won't give us any
1664 * read delegations that we have to return if, say, we are
1665 * recovering after a network partition or a reboot from a
1666 * server that doesn't support a grace period.
1668 spin_lock(&sp->so_lock);
1670 list_for_each_entry(state, &sp->so_states, open_states) {
1671 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1673 if (!nfs4_valid_open_stateid(state))
1675 if (state->state == 0)
1677 #ifdef CONFIG_NFS_V4_2
1678 if (test_bit(NFS_SRV_SSC_COPY_STATE, &state->flags)) {
1679 nfs4_state_mark_recovery_failed(state, -EIO);
1680 found_ssc_copy_state = true;
1683 #endif /* CONFIG_NFS_V4_2 */
1684 refcount_inc(&state->count);
1685 spin_unlock(&sp->so_lock);
1686 status = __nfs4_reclaim_open_state(sp, state, ops, lost_locks);
1694 printk(KERN_ERR "NFS: %s: unhandled error %d\n", __func__, status);
1702 /* Open state on this file cannot be recovered */
1703 nfs4_state_mark_recovery_failed(state, status);
1708 set_bit(ops->state_flag_bit, &state->flags);
1712 case -NFS4ERR_ADMIN_REVOKED:
1713 case -NFS4ERR_STALE_STATEID:
1714 case -NFS4ERR_OLD_STATEID:
1715 case -NFS4ERR_BAD_STATEID:
1716 case -NFS4ERR_RECLAIM_BAD:
1717 case -NFS4ERR_RECLAIM_CONFLICT:
1718 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1720 case -NFS4ERR_EXPIRED:
1721 case -NFS4ERR_NO_GRACE:
1722 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1724 case -NFS4ERR_STALE_CLIENTID:
1725 case -NFS4ERR_BADSESSION:
1726 case -NFS4ERR_BADSLOT:
1727 case -NFS4ERR_BAD_HIGH_SLOT:
1728 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1732 nfs4_put_open_state(state);
1733 spin_lock(&sp->so_lock);
1736 spin_unlock(&sp->so_lock);
1737 #ifdef CONFIG_NFS_V4_2
1738 if (found_ssc_copy_state)
1740 #endif /* CONFIG_NFS_V4_2 */
1743 nfs4_put_open_state(state);
1744 spin_lock(&sp->so_lock);
1745 spin_unlock(&sp->so_lock);
1749 static void nfs4_clear_open_state(struct nfs4_state *state)
1751 struct nfs4_lock_state *lock;
1753 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1754 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1755 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1756 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1757 spin_lock(&state->state_lock);
1758 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1759 lock->ls_seqid.flags = 0;
1760 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1762 spin_unlock(&state->state_lock);
1765 static void nfs4_reset_seqids(struct nfs_server *server,
1766 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1768 struct nfs_client *clp = server->nfs_client;
1769 struct nfs4_state_owner *sp;
1770 struct rb_node *pos;
1771 struct nfs4_state *state;
1773 spin_lock(&clp->cl_lock);
1774 for (pos = rb_first(&server->state_owners);
1776 pos = rb_next(pos)) {
1777 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1778 sp->so_seqid.flags = 0;
1779 spin_lock(&sp->so_lock);
1780 list_for_each_entry(state, &sp->so_states, open_states) {
1781 if (mark_reclaim(clp, state))
1782 nfs4_clear_open_state(state);
1784 spin_unlock(&sp->so_lock);
1786 spin_unlock(&clp->cl_lock);
1789 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1790 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1792 struct nfs_server *server;
1795 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1796 nfs4_reset_seqids(server, mark_reclaim);
1800 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1802 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1803 /* Mark all delegations for reclaim */
1804 nfs_delegation_mark_reclaim(clp);
1805 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1808 static int nfs4_reclaim_complete(struct nfs_client *clp,
1809 const struct nfs4_state_recovery_ops *ops,
1810 const struct cred *cred)
1812 /* Notify the server we're done reclaiming our state */
1813 if (ops->reclaim_complete)
1814 return ops->reclaim_complete(clp, cred);
1818 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1820 struct nfs_client *clp = server->nfs_client;
1821 struct nfs4_state_owner *sp;
1822 struct rb_node *pos;
1823 struct nfs4_state *state;
1825 spin_lock(&clp->cl_lock);
1826 for (pos = rb_first(&server->state_owners);
1828 pos = rb_next(pos)) {
1829 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1830 spin_lock(&sp->so_lock);
1831 list_for_each_entry(state, &sp->so_states, open_states) {
1832 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1835 nfs4_state_mark_reclaim_nograce(clp, state);
1837 spin_unlock(&sp->so_lock);
1839 spin_unlock(&clp->cl_lock);
1842 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1844 struct nfs_server *server;
1846 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1850 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1851 nfs4_clear_reclaim_server(server);
1854 nfs_delegation_reap_unclaimed(clp);
1858 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1860 const struct nfs4_state_recovery_ops *ops;
1861 const struct cred *cred;
1864 if (!nfs4_state_clear_reclaim_reboot(clp))
1866 ops = clp->cl_mvops->reboot_recovery_ops;
1867 cred = nfs4_get_clid_cred(clp);
1868 err = nfs4_reclaim_complete(clp, ops, cred);
1870 if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
1871 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1874 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1876 nfs_mark_test_expired_all_delegations(clp);
1877 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1880 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1885 case -NFS4ERR_CB_PATH_DOWN:
1886 nfs40_handle_cb_pathdown(clp);
1888 case -NFS4ERR_NO_GRACE:
1889 nfs4_state_end_reclaim_reboot(clp);
1891 case -NFS4ERR_STALE_CLIENTID:
1892 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1893 nfs4_state_start_reclaim_reboot(clp);
1895 case -NFS4ERR_EXPIRED:
1896 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1897 nfs4_state_start_reclaim_nograce(clp);
1899 case -NFS4ERR_BADSESSION:
1900 case -NFS4ERR_BADSLOT:
1901 case -NFS4ERR_BAD_HIGH_SLOT:
1902 case -NFS4ERR_DEADSESSION:
1903 case -NFS4ERR_SEQ_FALSE_RETRY:
1904 case -NFS4ERR_SEQ_MISORDERED:
1905 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1906 /* Zero session reset errors */
1908 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1909 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1912 dprintk("%s: failed to handle error %d for server %s\n",
1913 __func__, error, clp->cl_hostname);
1916 dprintk("%s: handled error %d for server %s\n", __func__, error,
1921 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1923 struct nfs4_state_owner *sp;
1924 struct nfs_server *server;
1925 struct rb_node *pos;
1930 status = nfs4_begin_drain_session(clp);
1935 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1936 nfs4_purge_state_owners(server, &freeme);
1937 spin_lock(&clp->cl_lock);
1938 for (pos = rb_first(&server->state_owners);
1940 pos = rb_next(pos)) {
1942 struct nfs4_state_owner, so_server_node);
1943 if (!test_and_clear_bit(ops->owner_flag_bit,
1946 if (!atomic_inc_not_zero(&sp->so_count))
1948 spin_unlock(&clp->cl_lock);
1951 status = nfs4_reclaim_open_state(sp, ops, &lost_locks);
1954 pr_warn("NFS: %s: lost %d locks\n",
1955 clp->cl_hostname, lost_locks);
1956 set_bit(ops->owner_flag_bit, &sp->so_flags);
1957 nfs4_put_state_owner(sp);
1958 status = nfs4_recovery_handle_error(clp, status);
1959 return (status != 0) ? status : -EAGAIN;
1962 nfs4_put_state_owner(sp);
1965 spin_unlock(&clp->cl_lock);
1968 nfs4_free_state_owners(&freeme);
1970 pr_warn("NFS: %s: lost %d locks\n",
1971 clp->cl_hostname, lost_locks);
1975 static int nfs4_check_lease(struct nfs_client *clp)
1977 const struct cred *cred;
1978 const struct nfs4_state_maintenance_ops *ops =
1979 clp->cl_mvops->state_renewal_ops;
1982 /* Is the client already known to have an expired lease? */
1983 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1985 cred = ops->get_state_renewal_cred(clp);
1987 cred = nfs4_get_clid_cred(clp);
1992 status = ops->renew_lease(clp, cred);
1994 if (status == -ETIMEDOUT) {
1995 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1999 return nfs4_recovery_handle_error(clp, status);
2002 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
2003 * and for recoverable errors on EXCHANGE_ID for v4.1
2005 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
2008 case -NFS4ERR_SEQ_MISORDERED:
2009 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
2010 return -ESERVERFAULT;
2011 /* Lease confirmation error: retry after purging the lease */
2013 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2015 case -NFS4ERR_STALE_CLIENTID:
2016 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2017 nfs4_state_start_reclaim_reboot(clp);
2019 case -NFS4ERR_CLID_INUSE:
2020 pr_err("NFS: Server %s reports our clientid is in use\n",
2022 nfs_mark_client_ready(clp, -EPERM);
2023 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2026 case -NFS4ERR_DELAY:
2031 case -NFS4ERR_MINOR_VERS_MISMATCH:
2032 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
2033 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
2034 dprintk("%s: exit with error %d for server %s\n",
2035 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
2036 return -EPROTONOSUPPORT;
2038 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
2039 nfs_mark_client_ready(clp, -EIO);
2041 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2042 * in nfs4_exchange_id */
2044 dprintk("%s: exit with error %d for server %s\n", __func__,
2045 status, clp->cl_hostname);
2048 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2049 dprintk("%s: handled error %d for server %s\n", __func__, status,
2054 static int nfs4_establish_lease(struct nfs_client *clp)
2056 const struct cred *cred;
2057 const struct nfs4_state_recovery_ops *ops =
2058 clp->cl_mvops->reboot_recovery_ops;
2061 status = nfs4_begin_drain_session(clp);
2064 cred = nfs4_get_clid_cred(clp);
2067 status = ops->establish_clid(clp, cred);
2071 pnfs_destroy_all_layouts(clp);
2076 * Returns zero or a negative errno. NFS4ERR values are converted
2077 * to local errno values.
2079 static int nfs4_reclaim_lease(struct nfs_client *clp)
2083 status = nfs4_establish_lease(clp);
2085 return nfs4_handle_reclaim_lease_error(clp, status);
2086 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
2087 nfs4_state_start_reclaim_nograce(clp);
2088 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2089 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
2090 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2091 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2095 static int nfs4_purge_lease(struct nfs_client *clp)
2099 status = nfs4_establish_lease(clp);
2101 return nfs4_handle_reclaim_lease_error(clp, status);
2102 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2103 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2104 nfs4_state_start_reclaim_nograce(clp);
2109 * Try remote migration of one FSID from a source server to a
2110 * destination server. The source server provides a list of
2111 * potential destinations.
2113 * Returns zero or a negative NFS4ERR status code.
2115 static int nfs4_try_migration(struct nfs_server *server, const struct cred *cred)
2117 struct nfs_client *clp = server->nfs_client;
2118 struct nfs4_fs_locations *locations = NULL;
2119 struct inode *inode;
2123 dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
2124 (unsigned long long)server->fsid.major,
2125 (unsigned long long)server->fsid.minor,
2129 page = alloc_page(GFP_KERNEL);
2130 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2131 if (page == NULL || locations == NULL) {
2132 dprintk("<-- %s: no memory\n", __func__);
2135 locations->fattr = nfs_alloc_fattr();
2136 if (locations->fattr == NULL) {
2137 dprintk("<-- %s: no memory\n", __func__);
2141 inode = d_inode(server->super->s_root);
2142 result = nfs4_proc_get_locations(server, NFS_FH(inode), locations,
2145 dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2150 result = -NFS4ERR_NXIO;
2151 if (!locations->nlocations)
2154 if (!(locations->fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
2155 dprintk("<-- %s: No fs_locations data, migration skipped\n",
2160 status = nfs4_begin_drain_session(clp);
2166 status = nfs4_replace_transport(server, locations);
2168 dprintk("<-- %s: failed to replace transport: %d\n",
2174 dprintk("<-- %s: migration succeeded\n", __func__);
2179 if (locations != NULL)
2180 kfree(locations->fattr);
2183 pr_err("NFS: migration recovery failed (server %s)\n",
2185 set_bit(NFS_MIG_FAILED, &server->mig_status);
2191 * Returns zero or a negative NFS4ERR status code.
2193 static int nfs4_handle_migration(struct nfs_client *clp)
2195 const struct nfs4_state_maintenance_ops *ops =
2196 clp->cl_mvops->state_renewal_ops;
2197 struct nfs_server *server;
2198 const struct cred *cred;
2200 dprintk("%s: migration reported on \"%s\"\n", __func__,
2203 cred = ops->get_state_renewal_cred(clp);
2205 return -NFS4ERR_NOENT;
2210 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2213 if (server->mig_gen == clp->cl_mig_gen)
2215 server->mig_gen = clp->cl_mig_gen;
2217 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
2218 &server->mig_status))
2222 status = nfs4_try_migration(server, cred);
2235 * Test each nfs_server on the clp's cl_superblocks list to see
2236 * if it's moved to another server. Stop when the server no longer
2237 * returns NFS4ERR_LEASE_MOVED.
2239 static int nfs4_handle_lease_moved(struct nfs_client *clp)
2241 const struct nfs4_state_maintenance_ops *ops =
2242 clp->cl_mvops->state_renewal_ops;
2243 struct nfs_server *server;
2244 const struct cred *cred;
2246 dprintk("%s: lease moved reported on \"%s\"\n", __func__,
2249 cred = ops->get_state_renewal_cred(clp);
2251 return -NFS4ERR_NOENT;
2256 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2257 struct inode *inode;
2260 if (server->mig_gen == clp->cl_mig_gen)
2262 server->mig_gen = clp->cl_mig_gen;
2266 inode = d_inode(server->super->s_root);
2267 status = nfs4_proc_fsid_present(inode, cred);
2268 if (status != -NFS4ERR_MOVED)
2269 goto restart; /* wasn't this one */
2270 if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
2271 goto restart; /* there are more */
2282 * nfs4_discover_server_trunking - Detect server IP address trunking
2284 * @clp: nfs_client under test
2285 * @result: OUT: found nfs_client, or clp
2287 * Returns zero or a negative errno. If zero is returned,
2288 * an nfs_client pointer is planted in "result".
2290 * Note: since we are invoked in process context, and
2291 * not from inside the state manager, we cannot use
2292 * nfs4_handle_reclaim_lease_error().
2294 int nfs4_discover_server_trunking(struct nfs_client *clp,
2295 struct nfs_client **result)
2297 const struct nfs4_state_recovery_ops *ops =
2298 clp->cl_mvops->reboot_recovery_ops;
2299 struct rpc_clnt *clnt;
2300 const struct cred *cred;
2303 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
2305 clnt = clp->cl_rpcclient;
2308 mutex_lock(&nfs_clid_init_mutex);
2311 cred = nfs4_get_clid_cred(clp);
2315 status = ops->detect_trunking(clp, result, cred);
2323 if (clnt->cl_softrtry)
2326 case -NFS4ERR_DELAY:
2330 case -NFS4ERR_STALE_CLIENTID:
2331 dprintk("NFS: %s after status %d, retrying\n",
2336 nfs4_root_machine_cred(clp);
2339 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
2342 case -NFS4ERR_CLID_INUSE:
2343 case -NFS4ERR_WRONGSEC:
2344 /* No point in retrying if we already used RPC_AUTH_UNIX */
2345 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
2349 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
2351 status = PTR_ERR(clnt);
2354 /* Note: this is safe because we haven't yet marked the
2355 * client as ready, so we are the only user of
2358 clnt = xchg(&clp->cl_rpcclient, clnt);
2359 rpc_shutdown_client(clnt);
2360 clnt = clp->cl_rpcclient;
2363 case -NFS4ERR_MINOR_VERS_MISMATCH:
2364 status = -EPROTONOSUPPORT;
2368 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2369 * in nfs4_exchange_id */
2370 status = -EKEYEXPIRED;
2373 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2379 mutex_unlock(&nfs_clid_init_mutex);
2380 dprintk("NFS: %s: status = %d\n", __func__, status);
2384 #ifdef CONFIG_NFS_V4_1
2385 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
2387 struct nfs_client *clp = session->clp;
2391 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2393 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2394 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2396 nfs4_schedule_state_manager(clp);
2398 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
2400 void nfs41_notify_server(struct nfs_client *clp)
2402 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
2403 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2404 nfs4_schedule_state_manager(clp);
2407 static void nfs4_reset_all_state(struct nfs_client *clp)
2409 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2410 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2411 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2412 nfs4_state_start_reclaim_nograce(clp);
2413 dprintk("%s: scheduling reset of all state for server %s!\n",
2414 __func__, clp->cl_hostname);
2415 nfs4_schedule_state_manager(clp);
2419 static void nfs41_handle_server_reboot(struct nfs_client *clp)
2421 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2422 nfs4_state_start_reclaim_reboot(clp);
2423 dprintk("%s: server %s rebooted!\n", __func__,
2425 nfs4_schedule_state_manager(clp);
2429 static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
2431 nfs4_reset_all_state(clp);
2432 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2435 static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
2437 nfs4_state_start_reclaim_nograce(clp);
2438 nfs4_schedule_state_manager(clp);
2440 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2443 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
2445 /* FIXME: For now, we destroy all layouts. */
2446 pnfs_destroy_all_layouts(clp);
2447 nfs_test_expired_all_delegations(clp);
2448 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2452 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
2454 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2455 nfs4_schedule_state_manager(clp);
2457 dprintk("%s: server %s declared a backchannel fault\n", __func__,
2461 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2463 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2464 &clp->cl_state) == 0)
2465 nfs4_schedule_state_manager(clp);
2468 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
2474 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2475 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2477 * If we're called from the state manager thread, then assume we're
2478 * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2479 * Those flags are expected to remain set until we're done
2480 * recovering (see RFC5661, section 18.46.3).
2485 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2486 nfs41_handle_server_reboot(clp);
2487 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
2488 nfs41_handle_all_state_revoked(clp);
2489 if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2490 SEQ4_STATUS_ADMIN_STATE_REVOKED))
2491 nfs41_handle_some_state_revoked(clp);
2492 if (flags & SEQ4_STATUS_LEASE_MOVED)
2493 nfs4_schedule_lease_moved_recovery(clp);
2494 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2495 nfs41_handle_recallable_state_revoked(clp);
2497 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2498 nfs41_handle_backchannel_fault(clp);
2499 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2500 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2501 nfs41_handle_cb_path_down(clp);
2504 static int nfs4_reset_session(struct nfs_client *clp)
2506 const struct cred *cred;
2509 if (!nfs4_has_session(clp))
2511 status = nfs4_begin_drain_session(clp);
2514 cred = nfs4_get_clid_cred(clp);
2515 status = nfs4_proc_destroy_session(clp->cl_session, cred);
2518 case -NFS4ERR_BADSESSION:
2519 case -NFS4ERR_DEADSESSION:
2521 case -NFS4ERR_BACK_CHAN_BUSY:
2522 case -NFS4ERR_DELAY:
2523 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2528 status = nfs4_recovery_handle_error(clp, status);
2532 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2533 status = nfs4_proc_create_session(clp, cred);
2535 dprintk("%s: session reset failed with status %d for server %s!\n",
2536 __func__, status, clp->cl_hostname);
2537 status = nfs4_handle_reclaim_lease_error(clp, status);
2540 nfs41_finish_session_reset(clp);
2541 dprintk("%s: session reset was successful for server %s!\n",
2542 __func__, clp->cl_hostname);
2548 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2550 const struct cred *cred;
2553 if (!nfs4_has_session(clp))
2555 ret = nfs4_begin_drain_session(clp);
2558 cred = nfs4_get_clid_cred(clp);
2559 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2561 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2564 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2565 __func__, clp->cl_hostname);
2567 case -NFS4ERR_DELAY:
2569 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2572 return nfs4_recovery_handle_error(clp, ret);
2577 static void nfs4_layoutreturn_any_run(struct nfs_client *clp)
2581 if (test_and_clear_bit(NFS4CLNT_RECALL_ANY_LAYOUT_READ, &clp->cl_state))
2582 iomode += IOMODE_READ;
2583 if (test_and_clear_bit(NFS4CLNT_RECALL_ANY_LAYOUT_RW, &clp->cl_state))
2584 iomode += IOMODE_RW;
2585 /* Note: IOMODE_READ + IOMODE_RW == IOMODE_ANY */
2587 pnfs_layout_return_unused_byclid(clp, iomode);
2588 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2591 #else /* CONFIG_NFS_V4_1 */
2592 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2594 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2599 static void nfs4_layoutreturn_any_run(struct nfs_client *clp)
2602 #endif /* CONFIG_NFS_V4_1 */
2604 static void nfs4_state_manager(struct nfs_client *clp)
2606 unsigned int memflags;
2608 const char *section = "", *section_sep = "";
2611 * State recovery can deadlock if the direct reclaim code tries
2612 * start NFS writeback. So ensure memory allocations are all
2615 memflags = memalloc_nofs_save();
2617 /* Ensure exclusive access to NFSv4 state */
2619 trace_nfs4_state_mgr(clp);
2620 clear_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2621 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2622 section = "purge state";
2623 status = nfs4_purge_lease(clp);
2629 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2630 section = "lease expired";
2631 /* We're going to have to re-establish a clientid */
2632 status = nfs4_reclaim_lease(clp);
2638 /* Initialize or reset the session */
2639 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2640 section = "reset session";
2641 status = nfs4_reset_session(clp);
2642 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2648 /* Send BIND_CONN_TO_SESSION */
2649 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2651 section = "bind conn to session";
2652 status = nfs4_bind_conn_to_session(clp);
2658 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2659 section = "check lease";
2660 status = nfs4_check_lease(clp);
2666 if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
2667 section = "migration";
2668 status = nfs4_handle_migration(clp);
2673 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
2674 section = "lease moved";
2675 status = nfs4_handle_lease_moved(clp);
2680 /* First recover reboot state... */
2681 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2682 section = "reclaim reboot";
2683 status = nfs4_do_reclaim(clp,
2684 clp->cl_mvops->reboot_recovery_ops);
2685 if (status == -EAGAIN)
2689 nfs4_state_end_reclaim_reboot(clp);
2693 /* Detect expired delegations... */
2694 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
2695 section = "detect expired delegations";
2696 status = nfs4_begin_drain_session(clp);
2699 nfs_reap_expired_delegations(clp);
2703 /* Now recover expired state... */
2704 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2705 section = "reclaim nograce";
2706 status = nfs4_do_reclaim(clp,
2707 clp->cl_mvops->nograce_recovery_ops);
2708 if (status == -EAGAIN)
2712 clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
2715 memalloc_nofs_restore(memflags);
2716 nfs4_end_drain_session(clp);
2717 nfs4_clear_state_manager_bit(clp);
2719 if (test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state) &&
2720 !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING,
2722 memflags = memalloc_nofs_save();
2726 if (!test_and_set_bit(NFS4CLNT_RECALL_RUNNING, &clp->cl_state)) {
2727 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2728 nfs_client_return_marked_delegations(clp);
2729 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2731 nfs4_layoutreturn_any_run(clp);
2732 clear_bit(NFS4CLNT_RECALL_RUNNING, &clp->cl_state);
2737 } while (refcount_read(&clp->cl_count) > 1 && !signalled());
2741 if (strlen(section))
2743 trace_nfs4_state_mgr_failed(clp, section, status);
2744 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2745 " with error %d\n", section_sep, section,
2746 clp->cl_hostname, -status);
2749 memalloc_nofs_restore(memflags);
2750 nfs4_end_drain_session(clp);
2751 nfs4_clear_state_manager_bit(clp);
2754 static int nfs4_run_state_manager(void *ptr)
2756 struct nfs_client *clp = ptr;
2757 struct rpc_clnt *cl = clp->cl_rpcclient;
2759 while (cl != cl->cl_parent)
2762 allow_signal(SIGKILL);
2764 nfs4_state_manager(clp);
2766 if (test_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state) &&
2767 !test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state)) {
2768 wait_var_event_interruptible(&clp->cl_state,
2769 test_bit(NFS4CLNT_RUN_MANAGER,
2771 if (!atomic_read(&cl->cl_swapper))
2772 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
2773 if (refcount_read(&clp->cl_count) > 1 && !signalled() &&
2774 !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state))
2776 /* Either no longer a swapper, or were signalled */
2777 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
2780 if (refcount_read(&clp->cl_count) > 1 && !signalled() &&
2781 test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state) &&
2782 !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state))
2785 nfs_put_client(clp);
2786 module_put_and_kthread_exit(0);