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/nfs_idmap.h>
46 #include <linux/kthread.h>
47 #include <linux/module.h>
48 #include <linux/random.h>
49 #include <linux/ratelimit.h>
50 #include <linux/workqueue.h>
51 #include <linux/bitops.h>
52 #include <linux/jiffies.h>
54 #include <linux/sunrpc/clnt.h>
58 #include "delegation.h"
60 #include "nfs4session.h"
64 #define NFSDBG_FACILITY NFSDBG_STATE
66 #define OPENOWNER_POOL_SIZE 8
68 const nfs4_stateid zero_stateid;
69 static DEFINE_MUTEX(nfs_clid_init_mutex);
70 static LIST_HEAD(nfs4_clientid_list);
72 int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
74 struct nfs4_setclientid_res clid = {
75 .clientid = clp->cl_clientid,
76 .confirm = clp->cl_confirm,
80 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
82 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
84 port = nn->nfs_callback_tcpport;
85 if (clp->cl_addr.ss_family == AF_INET6)
86 port = nn->nfs_callback_tcpport6;
88 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
91 clp->cl_clientid = clid.clientid;
92 clp->cl_confirm = clid.confirm;
93 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
95 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
98 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
99 nfs4_schedule_state_renewal(clp);
105 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
107 * @clp: nfs_client under test
108 * @result: OUT: found nfs_client, or clp
109 * @cred: credential to use for trunking test
111 * Returns zero, a negative errno, or a negative NFS4ERR status.
112 * If zero is returned, an nfs_client pointer is planted in
115 * Note: The returned client may not yet be marked ready.
117 int nfs40_discover_server_trunking(struct nfs_client *clp,
118 struct nfs_client **result,
119 struct rpc_cred *cred)
121 struct nfs4_setclientid_res clid = {
122 .clientid = clp->cl_clientid,
123 .confirm = clp->cl_confirm,
125 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
129 port = nn->nfs_callback_tcpport;
130 if (clp->cl_addr.ss_family == AF_INET6)
131 port = nn->nfs_callback_tcpport6;
133 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
136 clp->cl_clientid = clid.clientid;
137 clp->cl_confirm = clid.confirm;
139 status = nfs40_walk_client_list(clp, result, cred);
141 case -NFS4ERR_STALE_CLIENTID:
142 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
144 /* Sustain the lease, even if it's empty. If the clientid4
145 * goes stale it's of no use for trunking discovery. */
146 nfs4_schedule_state_renewal(*result);
154 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
156 struct rpc_cred *cred = NULL;
158 if (clp->cl_machine_cred != NULL)
159 cred = get_rpccred(clp->cl_machine_cred);
163 static void nfs4_clear_machine_cred(struct nfs_client *clp)
165 struct rpc_cred *cred;
167 spin_lock(&clp->cl_lock);
168 cred = clp->cl_machine_cred;
169 clp->cl_machine_cred = NULL;
170 spin_unlock(&clp->cl_lock);
175 static struct rpc_cred *
176 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
178 struct rpc_cred *cred = NULL;
179 struct nfs4_state_owner *sp;
182 for (pos = rb_first(&server->state_owners);
184 pos = rb_next(pos)) {
185 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
186 if (list_empty(&sp->so_states))
188 cred = get_rpccred(sp->so_cred);
195 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
196 * @clp: client state handle
198 * Returns an rpc_cred with reference count bumped, or NULL.
199 * Caller must hold clp->cl_lock.
201 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
203 struct rpc_cred *cred = NULL;
204 struct nfs_server *server;
206 /* Use machine credentials if available */
207 cred = nfs4_get_machine_cred_locked(clp);
212 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
213 cred = nfs4_get_renew_cred_server_locked(server);
223 #if defined(CONFIG_NFS_V4_1)
225 static int nfs41_setup_state_renewal(struct nfs_client *clp)
228 struct nfs_fsinfo fsinfo;
230 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
231 nfs4_schedule_state_renewal(clp);
235 status = nfs4_proc_get_lease_time(clp, &fsinfo);
237 /* Update lease time and schedule renewal */
238 spin_lock(&clp->cl_lock);
239 clp->cl_lease_time = fsinfo.lease_time * HZ;
240 clp->cl_last_renewal = jiffies;
241 spin_unlock(&clp->cl_lock);
243 nfs4_schedule_state_renewal(clp);
250 * Back channel returns NFS4ERR_DELAY for new requests when
251 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
254 static void nfs4_end_drain_session(struct nfs_client *clp)
256 struct nfs4_session *ses = clp->cl_session;
257 struct nfs4_slot_table *tbl;
262 tbl = &ses->fc_slot_table;
263 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
264 spin_lock(&tbl->slot_tbl_lock);
265 for (i = 0; i <= tbl->max_slotid; i++) {
266 if (rpc_wake_up_next(&tbl->slot_tbl_waitq) == NULL)
269 spin_unlock(&tbl->slot_tbl_lock);
274 * Signal state manager thread if session fore channel is drained
276 void nfs4_session_drain_complete(struct nfs4_session *session,
277 struct nfs4_slot_table *tbl)
279 if (nfs4_session_draining(session))
280 complete(&tbl->complete);
283 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
285 spin_lock(&tbl->slot_tbl_lock);
286 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
287 INIT_COMPLETION(tbl->complete);
288 spin_unlock(&tbl->slot_tbl_lock);
289 return wait_for_completion_interruptible(&tbl->complete);
291 spin_unlock(&tbl->slot_tbl_lock);
295 static int nfs4_begin_drain_session(struct nfs_client *clp)
297 struct nfs4_session *ses = clp->cl_session;
300 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
302 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
306 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
309 static void nfs41_finish_session_reset(struct nfs_client *clp)
311 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
312 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
313 /* create_session negotiated new slot table */
314 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
315 nfs41_setup_state_renewal(clp);
318 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
322 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
324 nfs4_begin_drain_session(clp);
325 status = nfs4_proc_exchange_id(clp, cred);
328 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
330 status = nfs4_proc_create_session(clp, cred);
333 nfs41_finish_session_reset(clp);
334 nfs_mark_client_ready(clp, NFS_CS_READY);
340 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
342 * @clp: nfs_client under test
343 * @result: OUT: found nfs_client, or clp
344 * @cred: credential to use for trunking test
346 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
347 * If NFS4_OK is returned, an nfs_client pointer is planted in
350 * Note: The returned client may not yet be marked ready.
352 int nfs41_discover_server_trunking(struct nfs_client *clp,
353 struct nfs_client **result,
354 struct rpc_cred *cred)
358 status = nfs4_proc_exchange_id(clp, cred);
359 if (status != NFS4_OK)
361 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
363 return nfs41_walk_client_list(clp, result, cred);
366 struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
368 struct rpc_cred *cred;
370 spin_lock(&clp->cl_lock);
371 cred = nfs4_get_machine_cred_locked(clp);
372 spin_unlock(&clp->cl_lock);
376 #endif /* CONFIG_NFS_V4_1 */
378 static struct rpc_cred *
379 nfs4_get_setclientid_cred_server(struct nfs_server *server)
381 struct nfs_client *clp = server->nfs_client;
382 struct rpc_cred *cred = NULL;
383 struct nfs4_state_owner *sp;
386 spin_lock(&clp->cl_lock);
387 pos = rb_first(&server->state_owners);
389 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
390 cred = get_rpccred(sp->so_cred);
392 spin_unlock(&clp->cl_lock);
397 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
398 * @clp: client state handle
400 * Returns an rpc_cred with reference count bumped, or NULL.
402 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
404 struct nfs_server *server;
405 struct rpc_cred *cred;
407 spin_lock(&clp->cl_lock);
408 cred = nfs4_get_machine_cred_locked(clp);
409 spin_unlock(&clp->cl_lock);
414 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
415 cred = nfs4_get_setclientid_cred_server(server);
425 static struct nfs4_state_owner *
426 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
428 struct rb_node **p = &server->state_owners.rb_node,
430 struct nfs4_state_owner *sp;
434 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
436 if (cred < sp->so_cred)
437 p = &parent->rb_left;
438 else if (cred > sp->so_cred)
439 p = &parent->rb_right;
441 if (!list_empty(&sp->so_lru))
442 list_del_init(&sp->so_lru);
443 atomic_inc(&sp->so_count);
450 static struct nfs4_state_owner *
451 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
453 struct nfs_server *server = new->so_server;
454 struct rb_node **p = &server->state_owners.rb_node,
456 struct nfs4_state_owner *sp;
461 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
463 if (new->so_cred < sp->so_cred)
464 p = &parent->rb_left;
465 else if (new->so_cred > sp->so_cred)
466 p = &parent->rb_right;
468 if (!list_empty(&sp->so_lru))
469 list_del_init(&sp->so_lru);
470 atomic_inc(&sp->so_count);
474 err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
477 rb_link_node(&new->so_server_node, parent, p);
478 rb_insert_color(&new->so_server_node, &server->state_owners);
483 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
485 struct nfs_server *server = sp->so_server;
487 if (!RB_EMPTY_NODE(&sp->so_server_node))
488 rb_erase(&sp->so_server_node, &server->state_owners);
489 ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
493 nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
495 sc->create_time = ktime_get();
498 spin_lock_init(&sc->lock);
499 INIT_LIST_HEAD(&sc->list);
500 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
504 nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
506 rpc_destroy_wait_queue(&sc->wait);
510 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
511 * create a new state_owner.
514 static struct nfs4_state_owner *
515 nfs4_alloc_state_owner(struct nfs_server *server,
516 struct rpc_cred *cred,
519 struct nfs4_state_owner *sp;
521 sp = kzalloc(sizeof(*sp), gfp_flags);
524 sp->so_server = server;
525 sp->so_cred = get_rpccred(cred);
526 spin_lock_init(&sp->so_lock);
527 INIT_LIST_HEAD(&sp->so_states);
528 nfs4_init_seqid_counter(&sp->so_seqid);
529 atomic_set(&sp->so_count, 1);
530 INIT_LIST_HEAD(&sp->so_lru);
535 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
537 struct rb_node *rb_node = &sp->so_server_node;
539 if (!RB_EMPTY_NODE(rb_node)) {
540 struct nfs_server *server = sp->so_server;
541 struct nfs_client *clp = server->nfs_client;
543 spin_lock(&clp->cl_lock);
544 if (!RB_EMPTY_NODE(rb_node)) {
545 rb_erase(rb_node, &server->state_owners);
546 RB_CLEAR_NODE(rb_node);
548 spin_unlock(&clp->cl_lock);
552 static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
554 nfs4_destroy_seqid_counter(&sp->so_seqid);
555 put_rpccred(sp->so_cred);
559 static void nfs4_gc_state_owners(struct nfs_server *server)
561 struct nfs_client *clp = server->nfs_client;
562 struct nfs4_state_owner *sp, *tmp;
563 unsigned long time_min, time_max;
566 spin_lock(&clp->cl_lock);
568 time_min = (long)time_max - (long)clp->cl_lease_time;
569 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
570 /* NB: LRU is sorted so that oldest is at the head */
571 if (time_in_range(sp->so_expires, time_min, time_max))
573 list_move(&sp->so_lru, &doomed);
574 nfs4_remove_state_owner_locked(sp);
576 spin_unlock(&clp->cl_lock);
578 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
579 list_del(&sp->so_lru);
580 nfs4_free_state_owner(sp);
585 * nfs4_get_state_owner - Look up a state owner given a credential
586 * @server: nfs_server to search
587 * @cred: RPC credential to match
589 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
591 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
592 struct rpc_cred *cred,
595 struct nfs_client *clp = server->nfs_client;
596 struct nfs4_state_owner *sp, *new;
598 spin_lock(&clp->cl_lock);
599 sp = nfs4_find_state_owner_locked(server, cred);
600 spin_unlock(&clp->cl_lock);
603 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
607 if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
609 spin_lock(&clp->cl_lock);
610 sp = nfs4_insert_state_owner_locked(new);
611 spin_unlock(&clp->cl_lock);
612 } while (sp == ERR_PTR(-EAGAIN));
614 nfs4_free_state_owner(new);
616 nfs4_gc_state_owners(server);
621 * nfs4_put_state_owner - Release a nfs4_state_owner
622 * @sp: state owner data to release
624 * Note that we keep released state owners on an LRU
626 * This caches valid state owners so that they can be
627 * reused, to avoid the OPEN_CONFIRM on minor version 0.
628 * It also pins the uniquifier of dropped state owners for
629 * a while, to ensure that those state owner names are
632 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
634 struct nfs_server *server = sp->so_server;
635 struct nfs_client *clp = server->nfs_client;
637 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
640 sp->so_expires = jiffies;
641 list_add_tail(&sp->so_lru, &server->state_owners_lru);
642 spin_unlock(&clp->cl_lock);
646 * nfs4_purge_state_owners - Release all cached state owners
647 * @server: nfs_server with cached state owners to release
649 * Called at umount time. Remaining state owners will be on
650 * the LRU with ref count of zero.
652 void nfs4_purge_state_owners(struct nfs_server *server)
654 struct nfs_client *clp = server->nfs_client;
655 struct nfs4_state_owner *sp, *tmp;
658 spin_lock(&clp->cl_lock);
659 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
660 list_move(&sp->so_lru, &doomed);
661 nfs4_remove_state_owner_locked(sp);
663 spin_unlock(&clp->cl_lock);
665 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
666 list_del(&sp->so_lru);
667 nfs4_free_state_owner(sp);
671 static struct nfs4_state *
672 nfs4_alloc_open_state(void)
674 struct nfs4_state *state;
676 state = kzalloc(sizeof(*state), GFP_NOFS);
679 atomic_set(&state->count, 1);
680 INIT_LIST_HEAD(&state->lock_states);
681 spin_lock_init(&state->state_lock);
682 seqlock_init(&state->seqlock);
687 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
689 if (state->state == fmode)
691 /* NB! List reordering - see the reclaim code for why. */
692 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
693 if (fmode & FMODE_WRITE)
694 list_move(&state->open_states, &state->owner->so_states);
696 list_move_tail(&state->open_states, &state->owner->so_states);
698 state->state = fmode;
701 static struct nfs4_state *
702 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
704 struct nfs_inode *nfsi = NFS_I(inode);
705 struct nfs4_state *state;
707 list_for_each_entry(state, &nfsi->open_states, inode_states) {
708 if (state->owner != owner)
710 if (atomic_inc_not_zero(&state->count))
717 nfs4_free_open_state(struct nfs4_state *state)
723 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
725 struct nfs4_state *state, *new;
726 struct nfs_inode *nfsi = NFS_I(inode);
728 spin_lock(&inode->i_lock);
729 state = __nfs4_find_state_byowner(inode, owner);
730 spin_unlock(&inode->i_lock);
733 new = nfs4_alloc_open_state();
734 spin_lock(&owner->so_lock);
735 spin_lock(&inode->i_lock);
736 state = __nfs4_find_state_byowner(inode, owner);
737 if (state == NULL && new != NULL) {
739 state->owner = owner;
740 atomic_inc(&owner->so_count);
741 list_add(&state->inode_states, &nfsi->open_states);
743 state->inode = inode;
744 spin_unlock(&inode->i_lock);
745 /* Note: The reclaim code dictates that we add stateless
746 * and read-only stateids to the end of the list */
747 list_add_tail(&state->open_states, &owner->so_states);
748 spin_unlock(&owner->so_lock);
750 spin_unlock(&inode->i_lock);
751 spin_unlock(&owner->so_lock);
753 nfs4_free_open_state(new);
759 void nfs4_put_open_state(struct nfs4_state *state)
761 struct inode *inode = state->inode;
762 struct nfs4_state_owner *owner = state->owner;
764 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
766 spin_lock(&inode->i_lock);
767 list_del(&state->inode_states);
768 list_del(&state->open_states);
769 spin_unlock(&inode->i_lock);
770 spin_unlock(&owner->so_lock);
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_NOFS, 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 current->files
838 static struct nfs4_lock_state *
839 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
841 struct nfs4_lock_state *pos;
842 list_for_each_entry(pos, &state->lock_states, ls_locks) {
843 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
845 switch (pos->ls_owner.lo_type) {
846 case NFS4_POSIX_LOCK_TYPE:
847 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
850 case NFS4_FLOCK_LOCK_TYPE:
851 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
854 atomic_inc(&pos->ls_count);
861 * Return a compatible lock_state. If no initialized lock_state structure
862 * exists, return an uninitialized one.
865 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
867 struct nfs4_lock_state *lsp;
868 struct nfs_server *server = state->owner->so_server;
870 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
873 nfs4_init_seqid_counter(&lsp->ls_seqid);
874 atomic_set(&lsp->ls_count, 1);
875 lsp->ls_state = state;
876 lsp->ls_owner.lo_type = type;
877 switch (lsp->ls_owner.lo_type) {
878 case NFS4_FLOCK_LOCK_TYPE:
879 lsp->ls_owner.lo_u.flock_owner = fl_pid;
881 case NFS4_POSIX_LOCK_TYPE:
882 lsp->ls_owner.lo_u.posix_owner = fl_owner;
887 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
888 if (lsp->ls_seqid.owner_id < 0)
890 INIT_LIST_HEAD(&lsp->ls_locks);
897 void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
899 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
900 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
905 * Return a compatible lock_state. If no initialized lock_state structure
906 * exists, return an uninitialized one.
909 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
911 struct nfs4_lock_state *lsp, *new = NULL;
914 spin_lock(&state->state_lock);
915 lsp = __nfs4_find_lock_state(state, owner, pid, type);
919 list_add(&new->ls_locks, &state->lock_states);
920 set_bit(LK_STATE_IN_USE, &state->flags);
925 spin_unlock(&state->state_lock);
926 new = nfs4_alloc_lock_state(state, owner, pid, type);
930 spin_unlock(&state->state_lock);
932 nfs4_free_lock_state(state->owner->so_server, new);
937 * Release reference to lock_state, and free it if we see that
938 * it is no longer in use
940 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
942 struct nfs4_state *state;
946 state = lsp->ls_state;
947 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
949 list_del(&lsp->ls_locks);
950 if (list_empty(&state->lock_states))
951 clear_bit(LK_STATE_IN_USE, &state->flags);
952 spin_unlock(&state->state_lock);
953 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
954 if (nfs4_release_lockowner(lsp) == 0)
957 nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
960 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
962 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
964 dst->fl_u.nfs4_fl.owner = lsp;
965 atomic_inc(&lsp->ls_count);
968 static void nfs4_fl_release_lock(struct file_lock *fl)
970 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
973 static const struct file_lock_operations nfs4_fl_lock_ops = {
974 .fl_copy_lock = nfs4_fl_copy_lock,
975 .fl_release_private = nfs4_fl_release_lock,
978 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
980 struct nfs4_lock_state *lsp;
982 if (fl->fl_ops != NULL)
984 if (fl->fl_flags & FL_POSIX)
985 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
986 else if (fl->fl_flags & FL_FLOCK)
987 lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
988 NFS4_FLOCK_LOCK_TYPE);
993 fl->fl_u.nfs4_fl.owner = lsp;
994 fl->fl_ops = &nfs4_fl_lock_ops;
998 static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
999 const struct nfs_lockowner *lockowner)
1001 struct nfs4_lock_state *lsp;
1002 fl_owner_t fl_owner;
1007 if (lockowner == NULL)
1010 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
1013 fl_owner = lockowner->l_owner;
1014 fl_pid = lockowner->l_pid;
1015 spin_lock(&state->state_lock);
1016 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
1017 if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
1018 nfs4_stateid_copy(dst, &lsp->ls_stateid);
1021 spin_unlock(&state->state_lock);
1022 nfs4_put_lock_state(lsp);
1027 static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1032 seq = read_seqbegin(&state->seqlock);
1033 nfs4_stateid_copy(dst, &state->stateid);
1034 } while (read_seqretry(&state->seqlock, seq));
1038 * Byte-range lock aware utility to initialize the stateid of read/write
1041 void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
1042 fmode_t fmode, const struct nfs_lockowner *lockowner)
1044 if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
1046 if (nfs4_copy_lock_stateid(dst, state, lockowner))
1048 nfs4_copy_open_stateid(dst, state);
1051 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1053 struct nfs_seqid *new;
1055 new = kmalloc(sizeof(*new), gfp_mask);
1057 new->sequence = counter;
1058 INIT_LIST_HEAD(&new->list);
1064 void nfs_release_seqid(struct nfs_seqid *seqid)
1066 struct nfs_seqid_counter *sequence;
1068 if (list_empty(&seqid->list))
1070 sequence = seqid->sequence;
1071 spin_lock(&sequence->lock);
1072 list_del_init(&seqid->list);
1073 if (!list_empty(&sequence->list)) {
1074 struct nfs_seqid *next;
1076 next = list_first_entry(&sequence->list,
1077 struct nfs_seqid, list);
1078 rpc_wake_up_queued_task(&sequence->wait, next->task);
1080 spin_unlock(&sequence->lock);
1083 void nfs_free_seqid(struct nfs_seqid *seqid)
1085 nfs_release_seqid(seqid);
1090 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1091 * failed with a seqid incrementing error -
1092 * see comments nfs_fs.h:seqid_mutating_error()
1094 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1099 case -NFS4ERR_BAD_SEQID:
1100 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1102 pr_warn_ratelimited("NFS: v4 server returned a bad"
1103 " sequence-id error on an"
1104 " unconfirmed sequence %p!\n",
1106 case -NFS4ERR_STALE_CLIENTID:
1107 case -NFS4ERR_STALE_STATEID:
1108 case -NFS4ERR_BAD_STATEID:
1109 case -NFS4ERR_BADXDR:
1110 case -NFS4ERR_RESOURCE:
1111 case -NFS4ERR_NOFILEHANDLE:
1112 /* Non-seqid mutating errors */
1116 * Note: no locking needed as we are guaranteed to be first
1117 * on the sequence list
1119 seqid->sequence->counter++;
1122 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1124 struct nfs4_state_owner *sp = container_of(seqid->sequence,
1125 struct nfs4_state_owner, so_seqid);
1126 struct nfs_server *server = sp->so_server;
1128 if (status == -NFS4ERR_BAD_SEQID)
1129 nfs4_drop_state_owner(sp);
1130 if (!nfs4_has_session(server->nfs_client))
1131 nfs_increment_seqid(status, seqid);
1135 * Increment the seqid if the LOCK/LOCKU succeeded, or
1136 * failed with a seqid incrementing error -
1137 * see comments nfs_fs.h:seqid_mutating_error()
1139 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1141 nfs_increment_seqid(status, seqid);
1144 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1146 struct nfs_seqid_counter *sequence = seqid->sequence;
1149 spin_lock(&sequence->lock);
1151 if (list_empty(&seqid->list))
1152 list_add_tail(&seqid->list, &sequence->list);
1153 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1155 rpc_sleep_on(&sequence->wait, task, NULL);
1158 spin_unlock(&sequence->lock);
1162 static int nfs4_run_state_manager(void *);
1164 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1166 smp_mb__before_clear_bit();
1167 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1168 smp_mb__after_clear_bit();
1169 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
1170 rpc_wake_up(&clp->cl_rpcwaitq);
1174 * Schedule the nfs_client asynchronous state management routine
1176 void nfs4_schedule_state_manager(struct nfs_client *clp)
1178 struct task_struct *task;
1179 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1181 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1183 __module_get(THIS_MODULE);
1184 atomic_inc(&clp->cl_count);
1186 /* The rcu_read_lock() is not strictly necessary, as the state
1187 * manager is the only thread that ever changes the rpc_xprt
1188 * after it's initialized. At this point, we're single threaded. */
1190 snprintf(buf, sizeof(buf), "%s-manager",
1191 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1193 task = kthread_run(nfs4_run_state_manager, clp, buf);
1195 printk(KERN_ERR "%s: kthread_run: %ld\n",
1196 __func__, PTR_ERR(task));
1197 nfs4_clear_state_manager_bit(clp);
1198 nfs_put_client(clp);
1199 module_put(THIS_MODULE);
1204 * Schedule a lease recovery attempt
1206 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1210 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1211 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1212 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1214 nfs4_schedule_state_manager(clp);
1216 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1218 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1224 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1225 nfs_wait_bit_killable, TASK_KILLABLE);
1229 if (clp->cl_cons_state < 0)
1230 return clp->cl_cons_state;
1234 int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1239 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1240 ret = nfs4_wait_clnt_recover(clp);
1243 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1244 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1246 nfs4_schedule_state_manager(clp);
1253 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1254 * @clp: client to process
1256 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1257 * resend of the SETCLIENTID and hence re-establish the
1258 * callback channel. Then return all existing delegations.
1260 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1262 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1263 nfs_expire_all_delegations(clp);
1264 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1268 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1270 nfs40_handle_cb_pathdown(clp);
1271 nfs4_schedule_state_manager(clp);
1274 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1277 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1278 /* Don't recover state that expired before the reboot */
1279 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1280 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1283 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1284 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1288 static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1290 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1291 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1292 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1293 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1297 void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1299 struct nfs_client *clp = server->nfs_client;
1301 nfs4_state_mark_reclaim_nograce(clp, state);
1302 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1304 nfs4_schedule_state_manager(clp);
1306 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1308 void nfs_inode_find_state_and_recover(struct inode *inode,
1309 const nfs4_stateid *stateid)
1311 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1312 struct nfs_inode *nfsi = NFS_I(inode);
1313 struct nfs_open_context *ctx;
1314 struct nfs4_state *state;
1317 spin_lock(&inode->i_lock);
1318 list_for_each_entry(ctx, &nfsi->open_files, list) {
1322 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1324 if (!nfs4_stateid_match(&state->stateid, stateid))
1326 nfs4_state_mark_reclaim_nograce(clp, state);
1329 spin_unlock(&inode->i_lock);
1331 nfs4_schedule_state_manager(clp);
1335 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1337 struct inode *inode = state->inode;
1338 struct nfs_inode *nfsi = NFS_I(inode);
1339 struct file_lock *fl;
1342 if (inode->i_flock == NULL)
1345 /* Guard against delegation returns and new lock/unlock calls */
1346 down_write(&nfsi->rwsem);
1347 /* Protect inode->i_flock using the BKL */
1349 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1350 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1352 if (nfs_file_open_context(fl->fl_file)->state != state)
1355 status = ops->recover_lock(state, fl);
1360 case -NFS4ERR_ADMIN_REVOKED:
1361 case -NFS4ERR_STALE_STATEID:
1362 case -NFS4ERR_BAD_STATEID:
1363 case -NFS4ERR_EXPIRED:
1364 case -NFS4ERR_NO_GRACE:
1365 case -NFS4ERR_STALE_CLIENTID:
1366 case -NFS4ERR_BADSESSION:
1367 case -NFS4ERR_BADSLOT:
1368 case -NFS4ERR_BAD_HIGH_SLOT:
1369 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1372 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1373 "Zeroing state\n", __func__, status);
1375 case -NFS4ERR_DENIED:
1376 case -NFS4ERR_RECLAIM_BAD:
1377 case -NFS4ERR_RECLAIM_CONFLICT:
1378 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1385 up_write(&nfsi->rwsem);
1389 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1391 struct nfs4_state *state;
1392 struct nfs4_lock_state *lock;
1395 /* Note: we rely on the sp->so_states list being ordered
1396 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1398 * This is needed to ensure that the server won't give us any
1399 * read delegations that we have to return if, say, we are
1400 * recovering after a network partition or a reboot from a
1401 * server that doesn't support a grace period.
1404 spin_lock(&sp->so_lock);
1405 list_for_each_entry(state, &sp->so_states, open_states) {
1406 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1408 if (state->state == 0)
1410 atomic_inc(&state->count);
1411 spin_unlock(&sp->so_lock);
1412 status = ops->recover_open(sp, state);
1414 status = nfs4_reclaim_locks(state, ops);
1416 spin_lock(&state->state_lock);
1417 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1418 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1419 pr_warn_ratelimited("NFS: "
1421 "failed!\n", __func__);
1423 spin_unlock(&state->state_lock);
1424 nfs4_put_open_state(state);
1430 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1431 "Zeroing state\n", __func__, status);
1436 * Open state on this file cannot be recovered
1437 * All we can do is revert to using the zero stateid.
1439 memset(&state->stateid, 0,
1440 sizeof(state->stateid));
1441 /* Mark the file as being 'closed' */
1446 * User RPCSEC_GSS context has expired.
1447 * We cannot recover this stateid now, so
1448 * skip it and allow recovery thread to
1452 case -NFS4ERR_ADMIN_REVOKED:
1453 case -NFS4ERR_STALE_STATEID:
1454 case -NFS4ERR_BAD_STATEID:
1455 case -NFS4ERR_RECLAIM_BAD:
1456 case -NFS4ERR_RECLAIM_CONFLICT:
1457 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1459 case -NFS4ERR_EXPIRED:
1460 case -NFS4ERR_NO_GRACE:
1461 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1462 case -NFS4ERR_STALE_CLIENTID:
1463 case -NFS4ERR_BADSESSION:
1464 case -NFS4ERR_BADSLOT:
1465 case -NFS4ERR_BAD_HIGH_SLOT:
1466 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1469 nfs4_put_open_state(state);
1472 spin_unlock(&sp->so_lock);
1475 nfs4_put_open_state(state);
1479 static void nfs4_clear_open_state(struct nfs4_state *state)
1481 struct nfs4_lock_state *lock;
1483 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1484 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1485 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1486 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1487 spin_lock(&state->state_lock);
1488 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1489 lock->ls_seqid.flags = 0;
1490 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1492 spin_unlock(&state->state_lock);
1495 static void nfs4_reset_seqids(struct nfs_server *server,
1496 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1498 struct nfs_client *clp = server->nfs_client;
1499 struct nfs4_state_owner *sp;
1500 struct rb_node *pos;
1501 struct nfs4_state *state;
1503 spin_lock(&clp->cl_lock);
1504 for (pos = rb_first(&server->state_owners);
1506 pos = rb_next(pos)) {
1507 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1508 sp->so_seqid.flags = 0;
1509 spin_lock(&sp->so_lock);
1510 list_for_each_entry(state, &sp->so_states, open_states) {
1511 if (mark_reclaim(clp, state))
1512 nfs4_clear_open_state(state);
1514 spin_unlock(&sp->so_lock);
1516 spin_unlock(&clp->cl_lock);
1519 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1520 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1522 struct nfs_server *server;
1525 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1526 nfs4_reset_seqids(server, mark_reclaim);
1530 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1532 /* Mark all delegations for reclaim */
1533 nfs_delegation_mark_reclaim(clp);
1534 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1537 static void nfs4_reclaim_complete(struct nfs_client *clp,
1538 const struct nfs4_state_recovery_ops *ops)
1540 /* Notify the server we're done reclaiming our state */
1541 if (ops->reclaim_complete)
1542 (void)ops->reclaim_complete(clp);
1545 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1547 struct nfs_client *clp = server->nfs_client;
1548 struct nfs4_state_owner *sp;
1549 struct rb_node *pos;
1550 struct nfs4_state *state;
1552 spin_lock(&clp->cl_lock);
1553 for (pos = rb_first(&server->state_owners);
1555 pos = rb_next(pos)) {
1556 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1557 spin_lock(&sp->so_lock);
1558 list_for_each_entry(state, &sp->so_states, open_states) {
1559 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1562 nfs4_state_mark_reclaim_nograce(clp, state);
1564 spin_unlock(&sp->so_lock);
1566 spin_unlock(&clp->cl_lock);
1569 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1571 struct nfs_server *server;
1573 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1577 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1578 nfs4_clear_reclaim_server(server);
1581 nfs_delegation_reap_unclaimed(clp);
1585 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1587 if (!nfs4_state_clear_reclaim_reboot(clp))
1589 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
1592 static void nfs_delegation_clear_all(struct nfs_client *clp)
1594 nfs_delegation_mark_reclaim(clp);
1595 nfs_delegation_reap_unclaimed(clp);
1598 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1600 nfs_delegation_clear_all(clp);
1601 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1604 static void nfs4_warn_keyexpired(const char *s)
1606 printk_ratelimited(KERN_WARNING "Error: state manager"
1607 " encountered RPCSEC_GSS session"
1608 " expired against NFSv4 server %s.\n",
1612 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1617 case -NFS4ERR_CB_PATH_DOWN:
1618 nfs40_handle_cb_pathdown(clp);
1620 case -NFS4ERR_NO_GRACE:
1621 nfs4_state_end_reclaim_reboot(clp);
1623 case -NFS4ERR_STALE_CLIENTID:
1624 case -NFS4ERR_LEASE_MOVED:
1625 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1626 nfs4_state_clear_reclaim_reboot(clp);
1627 nfs4_state_start_reclaim_reboot(clp);
1629 case -NFS4ERR_EXPIRED:
1630 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1631 nfs4_state_start_reclaim_nograce(clp);
1633 case -NFS4ERR_BADSESSION:
1634 case -NFS4ERR_BADSLOT:
1635 case -NFS4ERR_BAD_HIGH_SLOT:
1636 case -NFS4ERR_DEADSESSION:
1637 case -NFS4ERR_SEQ_FALSE_RETRY:
1638 case -NFS4ERR_SEQ_MISORDERED:
1639 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1640 /* Zero session reset errors */
1642 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1643 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1646 /* Nothing we can do */
1647 nfs4_warn_keyexpired(clp->cl_hostname);
1650 dprintk("%s: failed to handle error %d for server %s\n",
1651 __func__, error, clp->cl_hostname);
1654 dprintk("%s: handled error %d for server %s\n", __func__, error,
1659 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1661 struct nfs4_state_owner *sp;
1662 struct nfs_server *server;
1663 struct rb_node *pos;
1668 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1669 nfs4_purge_state_owners(server);
1670 spin_lock(&clp->cl_lock);
1671 for (pos = rb_first(&server->state_owners);
1673 pos = rb_next(pos)) {
1675 struct nfs4_state_owner, so_server_node);
1676 if (!test_and_clear_bit(ops->owner_flag_bit,
1679 atomic_inc(&sp->so_count);
1680 spin_unlock(&clp->cl_lock);
1683 status = nfs4_reclaim_open_state(sp, ops);
1685 set_bit(ops->owner_flag_bit, &sp->so_flags);
1686 nfs4_put_state_owner(sp);
1687 return nfs4_recovery_handle_error(clp, status);
1690 nfs4_put_state_owner(sp);
1693 spin_unlock(&clp->cl_lock);
1699 static int nfs4_check_lease(struct nfs_client *clp)
1701 struct rpc_cred *cred;
1702 const struct nfs4_state_maintenance_ops *ops =
1703 clp->cl_mvops->state_renewal_ops;
1706 /* Is the client already known to have an expired lease? */
1707 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1709 spin_lock(&clp->cl_lock);
1710 cred = ops->get_state_renewal_cred_locked(clp);
1711 spin_unlock(&clp->cl_lock);
1713 cred = nfs4_get_setclientid_cred(clp);
1718 status = ops->renew_lease(clp, cred);
1721 return nfs4_recovery_handle_error(clp, status);
1724 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1725 * and for recoverable errors on EXCHANGE_ID for v4.1
1727 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1730 case -NFS4ERR_SEQ_MISORDERED:
1731 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1732 return -ESERVERFAULT;
1733 /* Lease confirmation error: retry after purging the lease */
1735 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1737 case -NFS4ERR_STALE_CLIENTID:
1738 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1739 nfs4_state_clear_reclaim_reboot(clp);
1740 nfs4_state_start_reclaim_reboot(clp);
1742 case -NFS4ERR_CLID_INUSE:
1743 pr_err("NFS: Server %s reports our clientid is in use\n",
1745 nfs_mark_client_ready(clp, -EPERM);
1746 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1749 if (clp->cl_machine_cred == NULL)
1751 /* Handle case where the user hasn't set up machine creds */
1752 nfs4_clear_machine_cred(clp);
1753 case -NFS4ERR_DELAY:
1759 case -NFS4ERR_MINOR_VERS_MISMATCH:
1760 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1761 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1762 dprintk("%s: exit with error %d for server %s\n",
1763 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1764 return -EPROTONOSUPPORT;
1766 nfs4_warn_keyexpired(clp->cl_hostname);
1767 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1768 * in nfs4_exchange_id */
1770 dprintk("%s: exit with error %d for server %s\n", __func__,
1771 status, clp->cl_hostname);
1774 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1775 dprintk("%s: handled error %d for server %s\n", __func__, status,
1780 static int nfs4_establish_lease(struct nfs_client *clp)
1782 struct rpc_cred *cred;
1783 const struct nfs4_state_recovery_ops *ops =
1784 clp->cl_mvops->reboot_recovery_ops;
1787 cred = ops->get_clid_cred(clp);
1790 status = ops->establish_clid(clp, cred);
1794 pnfs_destroy_all_layouts(clp);
1799 * Returns zero or a negative errno. NFS4ERR values are converted
1800 * to local errno values.
1802 static int nfs4_reclaim_lease(struct nfs_client *clp)
1806 status = nfs4_establish_lease(clp);
1808 return nfs4_handle_reclaim_lease_error(clp, status);
1809 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1810 nfs4_state_start_reclaim_nograce(clp);
1811 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1812 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1813 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1814 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1818 static int nfs4_purge_lease(struct nfs_client *clp)
1822 status = nfs4_establish_lease(clp);
1824 return nfs4_handle_reclaim_lease_error(clp, status);
1825 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1826 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1827 nfs4_state_start_reclaim_nograce(clp);
1832 * nfs4_discover_server_trunking - Detect server IP address trunking
1834 * @clp: nfs_client under test
1835 * @result: OUT: found nfs_client, or clp
1837 * Returns zero or a negative errno. If zero is returned,
1838 * an nfs_client pointer is planted in "result".
1840 * Note: since we are invoked in process context, and
1841 * not from inside the state manager, we cannot use
1842 * nfs4_handle_reclaim_lease_error().
1844 int nfs4_discover_server_trunking(struct nfs_client *clp,
1845 struct nfs_client **result)
1847 const struct nfs4_state_recovery_ops *ops =
1848 clp->cl_mvops->reboot_recovery_ops;
1849 rpc_authflavor_t *flavors, flav, save;
1850 struct rpc_clnt *clnt;
1851 struct rpc_cred *cred;
1854 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
1856 len = NFS_MAX_SECFLAVORS;
1857 flavors = kcalloc(len, sizeof(*flavors), GFP_KERNEL);
1858 if (flavors == NULL) {
1862 len = rpcauth_list_flavors(flavors, len);
1867 clnt = clp->cl_rpcclient;
1868 save = clnt->cl_auth->au_flavor;
1871 mutex_lock(&nfs_clid_init_mutex);
1874 cred = ops->get_clid_cred(clp);
1878 status = ops->detect_trunking(clp, result, cred);
1885 if (clp->cl_machine_cred == NULL)
1887 /* Handle case where the user hasn't set up machine creds */
1888 nfs4_clear_machine_cred(clp);
1889 case -NFS4ERR_DELAY:
1893 dprintk("NFS: %s after status %d, retrying\n",
1897 case -NFS4ERR_CLID_INUSE:
1898 case -NFS4ERR_WRONGSEC:
1903 flav = flavors[i++];
1905 flav = flavors[i++];
1906 clnt = rpc_clone_client_set_auth(clnt, flav);
1908 status = PTR_ERR(clnt);
1911 clp->cl_rpcclient = clnt;
1914 case -NFS4ERR_MINOR_VERS_MISMATCH:
1915 status = -EPROTONOSUPPORT;
1919 nfs4_warn_keyexpired(clp->cl_hostname);
1920 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1921 * in nfs4_exchange_id */
1922 status = -EKEYEXPIRED;
1926 mutex_unlock(&nfs_clid_init_mutex);
1930 dprintk("NFS: %s: status = %d\n", __func__, status);
1934 #ifdef CONFIG_NFS_V4_1
1935 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
1937 struct nfs_client *clp = session->clp;
1941 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1943 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1944 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1946 nfs4_schedule_lease_recovery(clp);
1948 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
1950 static void nfs41_ping_server(struct nfs_client *clp)
1952 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
1953 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1954 nfs4_schedule_state_manager(clp);
1957 void nfs41_server_notify_target_slotid_update(struct nfs_client *clp)
1959 nfs41_ping_server(clp);
1962 void nfs41_server_notify_highest_slotid_update(struct nfs_client *clp)
1964 nfs41_ping_server(clp);
1967 static void nfs4_reset_all_state(struct nfs_client *clp)
1969 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1970 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1971 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1972 nfs4_state_start_reclaim_nograce(clp);
1973 dprintk("%s: scheduling reset of all state for server %s!\n",
1974 __func__, clp->cl_hostname);
1975 nfs4_schedule_state_manager(clp);
1979 static void nfs41_handle_server_reboot(struct nfs_client *clp)
1981 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1982 nfs4_state_start_reclaim_reboot(clp);
1983 dprintk("%s: server %s rebooted!\n", __func__,
1985 nfs4_schedule_state_manager(clp);
1989 static void nfs41_handle_state_revoked(struct nfs_client *clp)
1991 nfs4_reset_all_state(clp);
1992 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
1995 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1997 /* This will need to handle layouts too */
1998 nfs_expire_all_delegations(clp);
1999 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2003 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
2005 nfs_expire_all_delegations(clp);
2006 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
2007 nfs4_schedule_state_manager(clp);
2008 dprintk("%s: server %s declared a backchannel fault\n", __func__,
2012 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2014 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2015 &clp->cl_state) == 0)
2016 nfs4_schedule_state_manager(clp);
2019 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
2024 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2025 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2027 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2028 nfs41_handle_server_reboot(clp);
2029 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
2030 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2031 SEQ4_STATUS_ADMIN_STATE_REVOKED |
2032 SEQ4_STATUS_LEASE_MOVED))
2033 nfs41_handle_state_revoked(clp);
2034 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2035 nfs41_handle_recallable_state_revoked(clp);
2036 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2037 nfs41_handle_backchannel_fault(clp);
2038 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2039 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2040 nfs41_handle_cb_path_down(clp);
2043 static int nfs4_reset_session(struct nfs_client *clp)
2045 struct rpc_cred *cred;
2048 if (!nfs4_has_session(clp))
2050 nfs4_begin_drain_session(clp);
2051 cred = nfs4_get_exchange_id_cred(clp);
2052 status = nfs4_proc_destroy_session(clp->cl_session, cred);
2053 if (status && status != -NFS4ERR_BADSESSION &&
2054 status != -NFS4ERR_DEADSESSION) {
2055 status = nfs4_recovery_handle_error(clp, status);
2059 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2060 status = nfs4_proc_create_session(clp, cred);
2062 dprintk("%s: session reset failed with status %d for server %s!\n",
2063 __func__, status, clp->cl_hostname);
2064 status = nfs4_handle_reclaim_lease_error(clp, status);
2067 nfs41_finish_session_reset(clp);
2068 dprintk("%s: session reset was successful for server %s!\n",
2069 __func__, clp->cl_hostname);
2076 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2078 struct rpc_cred *cred;
2081 if (!nfs4_has_session(clp))
2083 nfs4_begin_drain_session(clp);
2084 cred = nfs4_get_exchange_id_cred(clp);
2085 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2088 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2091 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2092 __func__, clp->cl_hostname);
2094 case -NFS4ERR_DELAY:
2096 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2099 return nfs4_recovery_handle_error(clp, ret);
2103 #else /* CONFIG_NFS_V4_1 */
2104 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2105 static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
2107 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2111 #endif /* CONFIG_NFS_V4_1 */
2113 static void nfs4_state_manager(struct nfs_client *clp)
2116 const char *section = "", *section_sep = "";
2118 /* Ensure exclusive access to NFSv4 state */
2120 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2121 section = "purge state";
2122 status = nfs4_purge_lease(clp);
2128 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2129 section = "lease expired";
2130 /* We're going to have to re-establish a clientid */
2131 status = nfs4_reclaim_lease(clp);
2137 /* Initialize or reset the session */
2138 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2139 section = "reset session";
2140 status = nfs4_reset_session(clp);
2141 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2147 /* Send BIND_CONN_TO_SESSION */
2148 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2150 section = "bind conn to session";
2151 status = nfs4_bind_conn_to_session(clp);
2157 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2158 section = "check lease";
2159 status = nfs4_check_lease(clp);
2165 /* First recover reboot state... */
2166 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2167 section = "reclaim reboot";
2168 status = nfs4_do_reclaim(clp,
2169 clp->cl_mvops->reboot_recovery_ops);
2170 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
2171 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
2173 nfs4_state_end_reclaim_reboot(clp);
2174 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2180 /* Now recover expired state... */
2181 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2182 section = "reclaim nograce";
2183 status = nfs4_do_reclaim(clp,
2184 clp->cl_mvops->nograce_recovery_ops);
2185 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
2186 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
2187 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
2193 nfs4_end_drain_session(clp);
2194 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2195 nfs_client_return_marked_delegations(clp);
2199 nfs4_clear_state_manager_bit(clp);
2200 /* Did we race with an attempt to give us more work? */
2201 if (clp->cl_state == 0)
2203 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2205 } while (atomic_read(&clp->cl_count) > 1);
2208 if (strlen(section))
2210 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2211 " with error %d\n", section_sep, section,
2212 clp->cl_hostname, -status);
2214 nfs4_end_drain_session(clp);
2215 nfs4_clear_state_manager_bit(clp);
2218 static int nfs4_run_state_manager(void *ptr)
2220 struct nfs_client *clp = ptr;
2222 allow_signal(SIGKILL);
2223 nfs4_state_manager(clp);
2224 nfs_put_client(clp);
2225 module_put_and_exit(0);