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>
56 #include "delegation.h"
61 #define NFSDBG_FACILITY NFSDBG_STATE
63 #define OPENOWNER_POOL_SIZE 8
65 const nfs4_stateid zero_stateid;
67 static LIST_HEAD(nfs4_clientid_list);
69 int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
71 struct nfs4_setclientid_res clid = {
72 .clientid = clp->cl_clientid,
73 .confirm = clp->cl_confirm,
77 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
79 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
81 port = nn->nfs_callback_tcpport;
82 if (clp->cl_addr.ss_family == AF_INET6)
83 port = nn->nfs_callback_tcpport6;
85 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
88 clp->cl_clientid = clid.clientid;
89 clp->cl_confirm = clid.confirm;
90 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
92 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
95 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
96 nfs4_schedule_state_renewal(clp);
101 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
103 struct rpc_cred *cred = NULL;
105 if (clp->cl_machine_cred != NULL)
106 cred = get_rpccred(clp->cl_machine_cred);
110 static void nfs4_clear_machine_cred(struct nfs_client *clp)
112 struct rpc_cred *cred;
114 spin_lock(&clp->cl_lock);
115 cred = clp->cl_machine_cred;
116 clp->cl_machine_cred = NULL;
117 spin_unlock(&clp->cl_lock);
122 static struct rpc_cred *
123 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
125 struct rpc_cred *cred = NULL;
126 struct nfs4_state_owner *sp;
129 for (pos = rb_first(&server->state_owners);
131 pos = rb_next(pos)) {
132 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
133 if (list_empty(&sp->so_states))
135 cred = get_rpccred(sp->so_cred);
142 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
143 * @clp: client state handle
145 * Returns an rpc_cred with reference count bumped, or NULL.
146 * Caller must hold clp->cl_lock.
148 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
150 struct rpc_cred *cred = NULL;
151 struct nfs_server *server;
153 /* Use machine credentials if available */
154 cred = nfs4_get_machine_cred_locked(clp);
159 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
160 cred = nfs4_get_renew_cred_server_locked(server);
170 #if defined(CONFIG_NFS_V4_1)
172 static int nfs41_setup_state_renewal(struct nfs_client *clp)
175 struct nfs_fsinfo fsinfo;
177 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
178 nfs4_schedule_state_renewal(clp);
182 status = nfs4_proc_get_lease_time(clp, &fsinfo);
184 /* Update lease time and schedule renewal */
185 spin_lock(&clp->cl_lock);
186 clp->cl_lease_time = fsinfo.lease_time * HZ;
187 clp->cl_last_renewal = jiffies;
188 spin_unlock(&clp->cl_lock);
190 nfs4_schedule_state_renewal(clp);
197 * Back channel returns NFS4ERR_DELAY for new requests when
198 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
201 static void nfs4_end_drain_session(struct nfs_client *clp)
203 struct nfs4_session *ses = clp->cl_session;
204 struct nfs4_slot_table *tbl;
209 tbl = &ses->fc_slot_table;
210 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
211 spin_lock(&tbl->slot_tbl_lock);
212 max_slots = tbl->max_slots;
213 while (max_slots--) {
214 if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
215 nfs4_set_task_privileged,
219 spin_unlock(&tbl->slot_tbl_lock);
223 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
225 spin_lock(&tbl->slot_tbl_lock);
226 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
227 INIT_COMPLETION(tbl->complete);
228 spin_unlock(&tbl->slot_tbl_lock);
229 return wait_for_completion_interruptible(&tbl->complete);
231 spin_unlock(&tbl->slot_tbl_lock);
235 static int nfs4_begin_drain_session(struct nfs_client *clp)
237 struct nfs4_session *ses = clp->cl_session;
240 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
242 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
246 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
249 static void nfs41_finish_session_reset(struct nfs_client *clp)
251 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
252 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
253 /* create_session negotiated new slot table */
254 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
255 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
256 nfs41_setup_state_renewal(clp);
259 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
263 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
265 nfs4_begin_drain_session(clp);
266 status = nfs4_proc_exchange_id(clp, cred);
269 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
271 status = nfs4_proc_create_session(clp, cred);
274 nfs41_finish_session_reset(clp);
275 nfs_mark_client_ready(clp, NFS_CS_READY);
280 struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
282 struct rpc_cred *cred;
284 spin_lock(&clp->cl_lock);
285 cred = nfs4_get_machine_cred_locked(clp);
286 spin_unlock(&clp->cl_lock);
290 #endif /* CONFIG_NFS_V4_1 */
292 static struct rpc_cred *
293 nfs4_get_setclientid_cred_server(struct nfs_server *server)
295 struct nfs_client *clp = server->nfs_client;
296 struct rpc_cred *cred = NULL;
297 struct nfs4_state_owner *sp;
300 spin_lock(&clp->cl_lock);
301 pos = rb_first(&server->state_owners);
303 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
304 cred = get_rpccred(sp->so_cred);
306 spin_unlock(&clp->cl_lock);
311 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
312 * @clp: client state handle
314 * Returns an rpc_cred with reference count bumped, or NULL.
316 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
318 struct nfs_server *server;
319 struct rpc_cred *cred;
321 spin_lock(&clp->cl_lock);
322 cred = nfs4_get_machine_cred_locked(clp);
323 spin_unlock(&clp->cl_lock);
328 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
329 cred = nfs4_get_setclientid_cred_server(server);
339 static struct nfs4_state_owner *
340 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
342 struct rb_node **p = &server->state_owners.rb_node,
344 struct nfs4_state_owner *sp;
348 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
350 if (cred < sp->so_cred)
351 p = &parent->rb_left;
352 else if (cred > sp->so_cred)
353 p = &parent->rb_right;
355 if (!list_empty(&sp->so_lru))
356 list_del_init(&sp->so_lru);
357 atomic_inc(&sp->so_count);
364 static struct nfs4_state_owner *
365 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
367 struct nfs_server *server = new->so_server;
368 struct rb_node **p = &server->state_owners.rb_node,
370 struct nfs4_state_owner *sp;
375 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
377 if (new->so_cred < sp->so_cred)
378 p = &parent->rb_left;
379 else if (new->so_cred > sp->so_cred)
380 p = &parent->rb_right;
382 if (!list_empty(&sp->so_lru))
383 list_del_init(&sp->so_lru);
384 atomic_inc(&sp->so_count);
388 err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
391 rb_link_node(&new->so_server_node, parent, p);
392 rb_insert_color(&new->so_server_node, &server->state_owners);
397 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
399 struct nfs_server *server = sp->so_server;
401 if (!RB_EMPTY_NODE(&sp->so_server_node))
402 rb_erase(&sp->so_server_node, &server->state_owners);
403 ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
407 nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
409 sc->create_time = ktime_get();
412 spin_lock_init(&sc->lock);
413 INIT_LIST_HEAD(&sc->list);
414 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
418 nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
420 rpc_destroy_wait_queue(&sc->wait);
424 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
425 * create a new state_owner.
428 static struct nfs4_state_owner *
429 nfs4_alloc_state_owner(struct nfs_server *server,
430 struct rpc_cred *cred,
433 struct nfs4_state_owner *sp;
435 sp = kzalloc(sizeof(*sp), gfp_flags);
438 sp->so_server = server;
439 sp->so_cred = get_rpccred(cred);
440 spin_lock_init(&sp->so_lock);
441 INIT_LIST_HEAD(&sp->so_states);
442 nfs4_init_seqid_counter(&sp->so_seqid);
443 atomic_set(&sp->so_count, 1);
444 INIT_LIST_HEAD(&sp->so_lru);
449 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
451 struct rb_node *rb_node = &sp->so_server_node;
453 if (!RB_EMPTY_NODE(rb_node)) {
454 struct nfs_server *server = sp->so_server;
455 struct nfs_client *clp = server->nfs_client;
457 spin_lock(&clp->cl_lock);
458 if (!RB_EMPTY_NODE(rb_node)) {
459 rb_erase(rb_node, &server->state_owners);
460 RB_CLEAR_NODE(rb_node);
462 spin_unlock(&clp->cl_lock);
466 static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
468 nfs4_destroy_seqid_counter(&sp->so_seqid);
469 put_rpccred(sp->so_cred);
473 static void nfs4_gc_state_owners(struct nfs_server *server)
475 struct nfs_client *clp = server->nfs_client;
476 struct nfs4_state_owner *sp, *tmp;
477 unsigned long time_min, time_max;
480 spin_lock(&clp->cl_lock);
482 time_min = (long)time_max - (long)clp->cl_lease_time;
483 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
484 /* NB: LRU is sorted so that oldest is at the head */
485 if (time_in_range(sp->so_expires, time_min, time_max))
487 list_move(&sp->so_lru, &doomed);
488 nfs4_remove_state_owner_locked(sp);
490 spin_unlock(&clp->cl_lock);
492 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
493 list_del(&sp->so_lru);
494 nfs4_free_state_owner(sp);
499 * nfs4_get_state_owner - Look up a state owner given a credential
500 * @server: nfs_server to search
501 * @cred: RPC credential to match
503 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
505 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
506 struct rpc_cred *cred,
509 struct nfs_client *clp = server->nfs_client;
510 struct nfs4_state_owner *sp, *new;
512 spin_lock(&clp->cl_lock);
513 sp = nfs4_find_state_owner_locked(server, cred);
514 spin_unlock(&clp->cl_lock);
517 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
521 if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
523 spin_lock(&clp->cl_lock);
524 sp = nfs4_insert_state_owner_locked(new);
525 spin_unlock(&clp->cl_lock);
526 } while (sp == ERR_PTR(-EAGAIN));
528 nfs4_free_state_owner(new);
530 nfs4_gc_state_owners(server);
535 * nfs4_put_state_owner - Release a nfs4_state_owner
536 * @sp: state owner data to release
538 * Note that we keep released state owners on an LRU
540 * This caches valid state owners so that they can be
541 * reused, to avoid the OPEN_CONFIRM on minor version 0.
542 * It also pins the uniquifier of dropped state owners for
543 * a while, to ensure that those state owner names are
546 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
548 struct nfs_server *server = sp->so_server;
549 struct nfs_client *clp = server->nfs_client;
551 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
554 sp->so_expires = jiffies;
555 list_add_tail(&sp->so_lru, &server->state_owners_lru);
556 spin_unlock(&clp->cl_lock);
560 * nfs4_purge_state_owners - Release all cached state owners
561 * @server: nfs_server with cached state owners to release
563 * Called at umount time. Remaining state owners will be on
564 * the LRU with ref count of zero.
566 void nfs4_purge_state_owners(struct nfs_server *server)
568 struct nfs_client *clp = server->nfs_client;
569 struct nfs4_state_owner *sp, *tmp;
572 spin_lock(&clp->cl_lock);
573 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
574 list_move(&sp->so_lru, &doomed);
575 nfs4_remove_state_owner_locked(sp);
577 spin_unlock(&clp->cl_lock);
579 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
580 list_del(&sp->so_lru);
581 nfs4_free_state_owner(sp);
585 static struct nfs4_state *
586 nfs4_alloc_open_state(void)
588 struct nfs4_state *state;
590 state = kzalloc(sizeof(*state), GFP_NOFS);
593 atomic_set(&state->count, 1);
594 INIT_LIST_HEAD(&state->lock_states);
595 spin_lock_init(&state->state_lock);
596 seqlock_init(&state->seqlock);
601 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
603 if (state->state == fmode)
605 /* NB! List reordering - see the reclaim code for why. */
606 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
607 if (fmode & FMODE_WRITE)
608 list_move(&state->open_states, &state->owner->so_states);
610 list_move_tail(&state->open_states, &state->owner->so_states);
612 state->state = fmode;
615 static struct nfs4_state *
616 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
618 struct nfs_inode *nfsi = NFS_I(inode);
619 struct nfs4_state *state;
621 list_for_each_entry(state, &nfsi->open_states, inode_states) {
622 if (state->owner != owner)
624 if (atomic_inc_not_zero(&state->count))
631 nfs4_free_open_state(struct nfs4_state *state)
637 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
639 struct nfs4_state *state, *new;
640 struct nfs_inode *nfsi = NFS_I(inode);
642 spin_lock(&inode->i_lock);
643 state = __nfs4_find_state_byowner(inode, owner);
644 spin_unlock(&inode->i_lock);
647 new = nfs4_alloc_open_state();
648 spin_lock(&owner->so_lock);
649 spin_lock(&inode->i_lock);
650 state = __nfs4_find_state_byowner(inode, owner);
651 if (state == NULL && new != NULL) {
653 state->owner = owner;
654 atomic_inc(&owner->so_count);
655 list_add(&state->inode_states, &nfsi->open_states);
657 state->inode = inode;
658 spin_unlock(&inode->i_lock);
659 /* Note: The reclaim code dictates that we add stateless
660 * and read-only stateids to the end of the list */
661 list_add_tail(&state->open_states, &owner->so_states);
662 spin_unlock(&owner->so_lock);
664 spin_unlock(&inode->i_lock);
665 spin_unlock(&owner->so_lock);
667 nfs4_free_open_state(new);
673 void nfs4_put_open_state(struct nfs4_state *state)
675 struct inode *inode = state->inode;
676 struct nfs4_state_owner *owner = state->owner;
678 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
680 spin_lock(&inode->i_lock);
681 list_del(&state->inode_states);
682 list_del(&state->open_states);
683 spin_unlock(&inode->i_lock);
684 spin_unlock(&owner->so_lock);
686 nfs4_free_open_state(state);
687 nfs4_put_state_owner(owner);
691 * Close the current file.
693 static void __nfs4_close(struct nfs4_state *state,
694 fmode_t fmode, gfp_t gfp_mask, int wait)
696 struct nfs4_state_owner *owner = state->owner;
700 atomic_inc(&owner->so_count);
701 /* Protect against nfs4_find_state() */
702 spin_lock(&owner->so_lock);
703 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
710 case FMODE_READ|FMODE_WRITE:
713 newstate = FMODE_READ|FMODE_WRITE;
714 if (state->n_rdwr == 0) {
715 if (state->n_rdonly == 0) {
716 newstate &= ~FMODE_READ;
717 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
718 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
720 if (state->n_wronly == 0) {
721 newstate &= ~FMODE_WRITE;
722 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
723 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
726 clear_bit(NFS_DELEGATED_STATE, &state->flags);
728 nfs4_state_set_mode_locked(state, newstate);
729 spin_unlock(&owner->so_lock);
732 nfs4_put_open_state(state);
733 nfs4_put_state_owner(owner);
735 nfs4_do_close(state, gfp_mask, wait);
738 void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
740 __nfs4_close(state, fmode, GFP_NOFS, 0);
743 void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
745 __nfs4_close(state, fmode, GFP_KERNEL, 1);
749 * Search the state->lock_states for an existing lock_owner
750 * that is compatible with current->files
752 static struct nfs4_lock_state *
753 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
755 struct nfs4_lock_state *pos;
756 list_for_each_entry(pos, &state->lock_states, ls_locks) {
757 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
759 switch (pos->ls_owner.lo_type) {
760 case NFS4_POSIX_LOCK_TYPE:
761 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
764 case NFS4_FLOCK_LOCK_TYPE:
765 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
768 atomic_inc(&pos->ls_count);
775 * Return a compatible lock_state. If no initialized lock_state structure
776 * exists, return an uninitialized one.
779 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)
781 struct nfs4_lock_state *lsp;
782 struct nfs_server *server = state->owner->so_server;
784 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
787 nfs4_init_seqid_counter(&lsp->ls_seqid);
788 atomic_set(&lsp->ls_count, 1);
789 lsp->ls_state = state;
790 lsp->ls_owner.lo_type = type;
791 switch (lsp->ls_owner.lo_type) {
792 case NFS4_FLOCK_LOCK_TYPE:
793 lsp->ls_owner.lo_u.flock_owner = fl_pid;
795 case NFS4_POSIX_LOCK_TYPE:
796 lsp->ls_owner.lo_u.posix_owner = fl_owner;
801 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
802 if (lsp->ls_seqid.owner_id < 0)
804 INIT_LIST_HEAD(&lsp->ls_locks);
811 void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
813 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
814 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
819 * Return a compatible lock_state. If no initialized lock_state structure
820 * exists, return an uninitialized one.
823 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
825 struct nfs4_lock_state *lsp, *new = NULL;
828 spin_lock(&state->state_lock);
829 lsp = __nfs4_find_lock_state(state, owner, pid, type);
833 list_add(&new->ls_locks, &state->lock_states);
834 set_bit(LK_STATE_IN_USE, &state->flags);
839 spin_unlock(&state->state_lock);
840 new = nfs4_alloc_lock_state(state, owner, pid, type);
844 spin_unlock(&state->state_lock);
846 nfs4_free_lock_state(state->owner->so_server, new);
851 * Release reference to lock_state, and free it if we see that
852 * it is no longer in use
854 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
856 struct nfs4_state *state;
860 state = lsp->ls_state;
861 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
863 list_del(&lsp->ls_locks);
864 if (list_empty(&state->lock_states))
865 clear_bit(LK_STATE_IN_USE, &state->flags);
866 spin_unlock(&state->state_lock);
867 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
868 if (nfs4_release_lockowner(lsp) == 0)
871 nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
874 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
876 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
878 dst->fl_u.nfs4_fl.owner = lsp;
879 atomic_inc(&lsp->ls_count);
882 static void nfs4_fl_release_lock(struct file_lock *fl)
884 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
887 static const struct file_lock_operations nfs4_fl_lock_ops = {
888 .fl_copy_lock = nfs4_fl_copy_lock,
889 .fl_release_private = nfs4_fl_release_lock,
892 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
894 struct nfs4_lock_state *lsp;
896 if (fl->fl_ops != NULL)
898 if (fl->fl_flags & FL_POSIX)
899 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
900 else if (fl->fl_flags & FL_FLOCK)
901 lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
902 NFS4_FLOCK_LOCK_TYPE);
907 fl->fl_u.nfs4_fl.owner = lsp;
908 fl->fl_ops = &nfs4_fl_lock_ops;
912 static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
913 const struct nfs_lockowner *lockowner)
915 struct nfs4_lock_state *lsp;
921 if (lockowner == NULL)
924 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
927 fl_owner = lockowner->l_owner;
928 fl_pid = lockowner->l_pid;
929 spin_lock(&state->state_lock);
930 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
931 if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
932 nfs4_stateid_copy(dst, &lsp->ls_stateid);
935 spin_unlock(&state->state_lock);
936 nfs4_put_lock_state(lsp);
941 static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
946 seq = read_seqbegin(&state->seqlock);
947 nfs4_stateid_copy(dst, &state->stateid);
948 } while (read_seqretry(&state->seqlock, seq));
952 * Byte-range lock aware utility to initialize the stateid of read/write
955 void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
956 fmode_t fmode, const struct nfs_lockowner *lockowner)
958 if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
960 if (nfs4_copy_lock_stateid(dst, state, lockowner))
962 nfs4_copy_open_stateid(dst, state);
965 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
967 struct nfs_seqid *new;
969 new = kmalloc(sizeof(*new), gfp_mask);
971 new->sequence = counter;
972 INIT_LIST_HEAD(&new->list);
978 void nfs_release_seqid(struct nfs_seqid *seqid)
980 struct nfs_seqid_counter *sequence;
982 if (list_empty(&seqid->list))
984 sequence = seqid->sequence;
985 spin_lock(&sequence->lock);
986 list_del_init(&seqid->list);
987 if (!list_empty(&sequence->list)) {
988 struct nfs_seqid *next;
990 next = list_first_entry(&sequence->list,
991 struct nfs_seqid, list);
992 rpc_wake_up_queued_task(&sequence->wait, next->task);
994 spin_unlock(&sequence->lock);
997 void nfs_free_seqid(struct nfs_seqid *seqid)
999 nfs_release_seqid(seqid);
1004 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1005 * failed with a seqid incrementing error -
1006 * see comments nfs_fs.h:seqid_mutating_error()
1008 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1010 BUG_ON(list_first_entry(&seqid->sequence->list, struct nfs_seqid, list) != seqid);
1014 case -NFS4ERR_BAD_SEQID:
1015 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1017 pr_warn_ratelimited("NFS: v4 server returned a bad"
1018 " sequence-id error on an"
1019 " unconfirmed sequence %p!\n",
1021 case -NFS4ERR_STALE_CLIENTID:
1022 case -NFS4ERR_STALE_STATEID:
1023 case -NFS4ERR_BAD_STATEID:
1024 case -NFS4ERR_BADXDR:
1025 case -NFS4ERR_RESOURCE:
1026 case -NFS4ERR_NOFILEHANDLE:
1027 /* Non-seqid mutating errors */
1031 * Note: no locking needed as we are guaranteed to be first
1032 * on the sequence list
1034 seqid->sequence->counter++;
1037 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1039 struct nfs4_state_owner *sp = container_of(seqid->sequence,
1040 struct nfs4_state_owner, so_seqid);
1041 struct nfs_server *server = sp->so_server;
1043 if (status == -NFS4ERR_BAD_SEQID)
1044 nfs4_drop_state_owner(sp);
1045 if (!nfs4_has_session(server->nfs_client))
1046 nfs_increment_seqid(status, seqid);
1050 * Increment the seqid if the LOCK/LOCKU succeeded, or
1051 * failed with a seqid incrementing error -
1052 * see comments nfs_fs.h:seqid_mutating_error()
1054 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1056 nfs_increment_seqid(status, seqid);
1059 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1061 struct nfs_seqid_counter *sequence = seqid->sequence;
1064 spin_lock(&sequence->lock);
1066 if (list_empty(&seqid->list))
1067 list_add_tail(&seqid->list, &sequence->list);
1068 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1070 rpc_sleep_on(&sequence->wait, task, NULL);
1073 spin_unlock(&sequence->lock);
1077 static int nfs4_run_state_manager(void *);
1079 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1081 smp_mb__before_clear_bit();
1082 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1083 smp_mb__after_clear_bit();
1084 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
1085 rpc_wake_up(&clp->cl_rpcwaitq);
1089 * Schedule the nfs_client asynchronous state management routine
1091 void nfs4_schedule_state_manager(struct nfs_client *clp)
1093 struct task_struct *task;
1094 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1096 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1098 __module_get(THIS_MODULE);
1099 atomic_inc(&clp->cl_count);
1101 /* The rcu_read_lock() is not strictly necessary, as the state
1102 * manager is the only thread that ever changes the rpc_xprt
1103 * after it's initialized. At this point, we're single threaded. */
1105 snprintf(buf, sizeof(buf), "%s-manager",
1106 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1108 task = kthread_run(nfs4_run_state_manager, clp, buf);
1110 printk(KERN_ERR "%s: kthread_run: %ld\n",
1111 __func__, PTR_ERR(task));
1112 nfs4_clear_state_manager_bit(clp);
1113 nfs_put_client(clp);
1114 module_put(THIS_MODULE);
1119 * Schedule a lease recovery attempt
1121 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1125 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1126 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1127 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1129 nfs4_schedule_state_manager(clp);
1131 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1134 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1135 * @clp: client to process
1137 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1138 * resend of the SETCLIENTID and hence re-establish the
1139 * callback channel. Then return all existing delegations.
1141 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1143 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1144 nfs_expire_all_delegations(clp);
1145 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1149 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1151 nfs40_handle_cb_pathdown(clp);
1152 nfs4_schedule_state_manager(clp);
1155 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1158 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1159 /* Don't recover state that expired before the reboot */
1160 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1161 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1164 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1165 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1169 static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1171 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1172 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1173 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1174 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1178 void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1180 struct nfs_client *clp = server->nfs_client;
1182 nfs4_state_mark_reclaim_nograce(clp, state);
1183 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1185 nfs4_schedule_state_manager(clp);
1187 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1189 void nfs_inode_find_state_and_recover(struct inode *inode,
1190 const nfs4_stateid *stateid)
1192 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1193 struct nfs_inode *nfsi = NFS_I(inode);
1194 struct nfs_open_context *ctx;
1195 struct nfs4_state *state;
1198 spin_lock(&inode->i_lock);
1199 list_for_each_entry(ctx, &nfsi->open_files, list) {
1203 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1205 if (!nfs4_stateid_match(&state->stateid, stateid))
1207 nfs4_state_mark_reclaim_nograce(clp, state);
1210 spin_unlock(&inode->i_lock);
1212 nfs4_schedule_state_manager(clp);
1216 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1218 struct inode *inode = state->inode;
1219 struct nfs_inode *nfsi = NFS_I(inode);
1220 struct file_lock *fl;
1223 if (inode->i_flock == NULL)
1226 /* Guard against delegation returns and new lock/unlock calls */
1227 down_write(&nfsi->rwsem);
1228 /* Protect inode->i_flock using the BKL */
1230 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1231 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1233 if (nfs_file_open_context(fl->fl_file)->state != state)
1236 status = ops->recover_lock(state, fl);
1241 case -NFS4ERR_ADMIN_REVOKED:
1242 case -NFS4ERR_STALE_STATEID:
1243 case -NFS4ERR_BAD_STATEID:
1244 case -NFS4ERR_EXPIRED:
1245 case -NFS4ERR_NO_GRACE:
1246 case -NFS4ERR_STALE_CLIENTID:
1247 case -NFS4ERR_BADSESSION:
1248 case -NFS4ERR_BADSLOT:
1249 case -NFS4ERR_BAD_HIGH_SLOT:
1250 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1253 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1254 "Zeroing state\n", __func__, status);
1256 case -NFS4ERR_DENIED:
1257 case -NFS4ERR_RECLAIM_BAD:
1258 case -NFS4ERR_RECLAIM_CONFLICT:
1259 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1266 up_write(&nfsi->rwsem);
1270 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1272 struct nfs4_state *state;
1273 struct nfs4_lock_state *lock;
1276 /* Note: we rely on the sp->so_states list being ordered
1277 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1279 * This is needed to ensure that the server won't give us any
1280 * read delegations that we have to return if, say, we are
1281 * recovering after a network partition or a reboot from a
1282 * server that doesn't support a grace period.
1285 spin_lock(&sp->so_lock);
1286 list_for_each_entry(state, &sp->so_states, open_states) {
1287 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1289 if (state->state == 0)
1291 atomic_inc(&state->count);
1292 spin_unlock(&sp->so_lock);
1293 status = ops->recover_open(sp, state);
1295 status = nfs4_reclaim_locks(state, ops);
1297 spin_lock(&state->state_lock);
1298 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1299 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1300 pr_warn_ratelimited("NFS: "
1302 "failed!\n", __func__);
1304 spin_unlock(&state->state_lock);
1305 nfs4_put_open_state(state);
1311 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1312 "Zeroing state\n", __func__, status);
1317 * Open state on this file cannot be recovered
1318 * All we can do is revert to using the zero stateid.
1320 memset(&state->stateid, 0,
1321 sizeof(state->stateid));
1322 /* Mark the file as being 'closed' */
1327 * User RPCSEC_GSS context has expired.
1328 * We cannot recover this stateid now, so
1329 * skip it and allow recovery thread to
1333 case -NFS4ERR_ADMIN_REVOKED:
1334 case -NFS4ERR_STALE_STATEID:
1335 case -NFS4ERR_BAD_STATEID:
1336 case -NFS4ERR_RECLAIM_BAD:
1337 case -NFS4ERR_RECLAIM_CONFLICT:
1338 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1340 case -NFS4ERR_EXPIRED:
1341 case -NFS4ERR_NO_GRACE:
1342 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1343 case -NFS4ERR_STALE_CLIENTID:
1344 case -NFS4ERR_BADSESSION:
1345 case -NFS4ERR_BADSLOT:
1346 case -NFS4ERR_BAD_HIGH_SLOT:
1347 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1350 nfs4_put_open_state(state);
1353 spin_unlock(&sp->so_lock);
1356 nfs4_put_open_state(state);
1360 static void nfs4_clear_open_state(struct nfs4_state *state)
1362 struct nfs4_lock_state *lock;
1364 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1365 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1366 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1367 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1368 spin_lock(&state->state_lock);
1369 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1370 lock->ls_seqid.flags = 0;
1371 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1373 spin_unlock(&state->state_lock);
1376 static void nfs4_reset_seqids(struct nfs_server *server,
1377 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1379 struct nfs_client *clp = server->nfs_client;
1380 struct nfs4_state_owner *sp;
1381 struct rb_node *pos;
1382 struct nfs4_state *state;
1384 spin_lock(&clp->cl_lock);
1385 for (pos = rb_first(&server->state_owners);
1387 pos = rb_next(pos)) {
1388 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1389 sp->so_seqid.flags = 0;
1390 spin_lock(&sp->so_lock);
1391 list_for_each_entry(state, &sp->so_states, open_states) {
1392 if (mark_reclaim(clp, state))
1393 nfs4_clear_open_state(state);
1395 spin_unlock(&sp->so_lock);
1397 spin_unlock(&clp->cl_lock);
1400 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1401 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1403 struct nfs_server *server;
1406 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1407 nfs4_reset_seqids(server, mark_reclaim);
1411 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1413 /* Mark all delegations for reclaim */
1414 nfs_delegation_mark_reclaim(clp);
1415 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1418 static void nfs4_reclaim_complete(struct nfs_client *clp,
1419 const struct nfs4_state_recovery_ops *ops)
1421 /* Notify the server we're done reclaiming our state */
1422 if (ops->reclaim_complete)
1423 (void)ops->reclaim_complete(clp);
1426 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1428 struct nfs_client *clp = server->nfs_client;
1429 struct nfs4_state_owner *sp;
1430 struct rb_node *pos;
1431 struct nfs4_state *state;
1433 spin_lock(&clp->cl_lock);
1434 for (pos = rb_first(&server->state_owners);
1436 pos = rb_next(pos)) {
1437 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1438 spin_lock(&sp->so_lock);
1439 list_for_each_entry(state, &sp->so_states, open_states) {
1440 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1443 nfs4_state_mark_reclaim_nograce(clp, state);
1445 spin_unlock(&sp->so_lock);
1447 spin_unlock(&clp->cl_lock);
1450 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1452 struct nfs_server *server;
1454 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1458 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1459 nfs4_clear_reclaim_server(server);
1462 nfs_delegation_reap_unclaimed(clp);
1466 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1468 if (!nfs4_state_clear_reclaim_reboot(clp))
1470 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
1473 static void nfs_delegation_clear_all(struct nfs_client *clp)
1475 nfs_delegation_mark_reclaim(clp);
1476 nfs_delegation_reap_unclaimed(clp);
1479 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1481 nfs_delegation_clear_all(clp);
1482 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1485 static void nfs4_warn_keyexpired(const char *s)
1487 printk_ratelimited(KERN_WARNING "Error: state manager"
1488 " encountered RPCSEC_GSS session"
1489 " expired against NFSv4 server %s.\n",
1493 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1498 case -NFS4ERR_CB_PATH_DOWN:
1499 nfs40_handle_cb_pathdown(clp);
1501 case -NFS4ERR_NO_GRACE:
1502 nfs4_state_end_reclaim_reboot(clp);
1504 case -NFS4ERR_STALE_CLIENTID:
1505 case -NFS4ERR_LEASE_MOVED:
1506 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1507 nfs4_state_clear_reclaim_reboot(clp);
1508 nfs4_state_start_reclaim_reboot(clp);
1510 case -NFS4ERR_EXPIRED:
1511 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1512 nfs4_state_start_reclaim_nograce(clp);
1514 case -NFS4ERR_BADSESSION:
1515 case -NFS4ERR_BADSLOT:
1516 case -NFS4ERR_BAD_HIGH_SLOT:
1517 case -NFS4ERR_DEADSESSION:
1518 case -NFS4ERR_SEQ_FALSE_RETRY:
1519 case -NFS4ERR_SEQ_MISORDERED:
1520 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1521 /* Zero session reset errors */
1523 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1524 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1527 /* Nothing we can do */
1528 nfs4_warn_keyexpired(clp->cl_hostname);
1531 dprintk("%s: failed to handle error %d for server %s\n",
1532 __func__, error, clp->cl_hostname);
1535 dprintk("%s: handled error %d for server %s\n", __func__, error,
1540 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1542 struct nfs4_state_owner *sp;
1543 struct nfs_server *server;
1544 struct rb_node *pos;
1549 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1550 nfs4_purge_state_owners(server);
1551 spin_lock(&clp->cl_lock);
1552 for (pos = rb_first(&server->state_owners);
1554 pos = rb_next(pos)) {
1556 struct nfs4_state_owner, so_server_node);
1557 if (!test_and_clear_bit(ops->owner_flag_bit,
1560 atomic_inc(&sp->so_count);
1561 spin_unlock(&clp->cl_lock);
1564 status = nfs4_reclaim_open_state(sp, ops);
1566 set_bit(ops->owner_flag_bit, &sp->so_flags);
1567 nfs4_put_state_owner(sp);
1568 return nfs4_recovery_handle_error(clp, status);
1571 nfs4_put_state_owner(sp);
1574 spin_unlock(&clp->cl_lock);
1580 static int nfs4_check_lease(struct nfs_client *clp)
1582 struct rpc_cred *cred;
1583 const struct nfs4_state_maintenance_ops *ops =
1584 clp->cl_mvops->state_renewal_ops;
1587 /* Is the client already known to have an expired lease? */
1588 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1590 spin_lock(&clp->cl_lock);
1591 cred = ops->get_state_renewal_cred_locked(clp);
1592 spin_unlock(&clp->cl_lock);
1594 cred = nfs4_get_setclientid_cred(clp);
1599 status = ops->renew_lease(clp, cred);
1602 return nfs4_recovery_handle_error(clp, status);
1605 /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1606 * on EXCHANGE_ID for v4.1
1608 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1611 case -NFS4ERR_SEQ_MISORDERED:
1612 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1613 return -ESERVERFAULT;
1614 /* Lease confirmation error: retry after purging the lease */
1616 case -NFS4ERR_STALE_CLIENTID:
1617 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1619 case -NFS4ERR_CLID_INUSE:
1620 pr_err("NFS: Server %s reports our clientid is in use\n",
1622 nfs_mark_client_ready(clp, -EPERM);
1623 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1626 if (clp->cl_machine_cred == NULL)
1628 /* Handle case where the user hasn't set up machine creds */
1629 nfs4_clear_machine_cred(clp);
1630 case -NFS4ERR_DELAY:
1636 case -NFS4ERR_MINOR_VERS_MISMATCH:
1637 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1638 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1639 dprintk("%s: exit with error %d for server %s\n",
1640 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1641 return -EPROTONOSUPPORT;
1643 nfs4_warn_keyexpired(clp->cl_hostname);
1644 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1645 * in nfs4_exchange_id */
1647 dprintk("%s: exit with error %d for server %s\n", __func__,
1648 status, clp->cl_hostname);
1651 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1652 dprintk("%s: handled error %d for server %s\n", __func__, status,
1657 static int nfs4_establish_lease(struct nfs_client *clp)
1659 struct rpc_cred *cred;
1660 const struct nfs4_state_recovery_ops *ops =
1661 clp->cl_mvops->reboot_recovery_ops;
1664 cred = ops->get_clid_cred(clp);
1667 status = ops->establish_clid(clp, cred);
1671 pnfs_destroy_all_layouts(clp);
1676 * Returns zero or a negative errno. NFS4ERR values are converted
1677 * to local errno values.
1679 static int nfs4_reclaim_lease(struct nfs_client *clp)
1683 status = nfs4_establish_lease(clp);
1685 return nfs4_handle_reclaim_lease_error(clp, status);
1686 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1687 nfs4_state_start_reclaim_nograce(clp);
1688 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1689 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1690 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1691 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1695 static int nfs4_purge_lease(struct nfs_client *clp)
1699 status = nfs4_establish_lease(clp);
1701 return nfs4_handle_reclaim_lease_error(clp, status);
1702 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1703 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1704 nfs4_state_start_reclaim_nograce(clp);
1708 #ifdef CONFIG_NFS_V4_1
1709 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
1711 struct nfs_client *clp = session->clp;
1715 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1717 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1718 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1720 nfs4_schedule_lease_recovery(clp);
1722 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
1724 void nfs41_handle_recall_slot(struct nfs_client *clp)
1726 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1727 dprintk("%s: scheduling slot recall for server %s\n", __func__,
1729 nfs4_schedule_state_manager(clp);
1732 static void nfs4_reset_all_state(struct nfs_client *clp)
1734 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1735 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1736 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1737 nfs4_state_start_reclaim_nograce(clp);
1738 dprintk("%s: scheduling reset of all state for server %s!\n",
1739 __func__, clp->cl_hostname);
1740 nfs4_schedule_state_manager(clp);
1744 static void nfs41_handle_server_reboot(struct nfs_client *clp)
1746 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1747 nfs4_state_start_reclaim_reboot(clp);
1748 dprintk("%s: server %s rebooted!\n", __func__,
1750 nfs4_schedule_state_manager(clp);
1754 static void nfs41_handle_state_revoked(struct nfs_client *clp)
1756 nfs4_reset_all_state(clp);
1757 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
1760 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1762 /* This will need to handle layouts too */
1763 nfs_expire_all_delegations(clp);
1764 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
1768 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
1770 nfs_expire_all_delegations(clp);
1771 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
1772 nfs4_schedule_state_manager(clp);
1773 dprintk("%s: server %s declared a backchannel fault\n", __func__,
1777 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1779 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1780 &clp->cl_state) == 0)
1781 nfs4_schedule_state_manager(clp);
1784 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1789 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
1790 __func__, clp->cl_hostname, clp->cl_clientid, flags);
1792 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1793 nfs41_handle_server_reboot(clp);
1794 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
1795 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1796 SEQ4_STATUS_ADMIN_STATE_REVOKED |
1797 SEQ4_STATUS_LEASE_MOVED))
1798 nfs41_handle_state_revoked(clp);
1799 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1800 nfs41_handle_recallable_state_revoked(clp);
1801 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
1802 nfs41_handle_backchannel_fault(clp);
1803 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
1804 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
1805 nfs41_handle_cb_path_down(clp);
1808 static int nfs4_reset_session(struct nfs_client *clp)
1810 struct rpc_cred *cred;
1813 if (!nfs4_has_session(clp))
1815 nfs4_begin_drain_session(clp);
1816 cred = nfs4_get_exchange_id_cred(clp);
1817 status = nfs4_proc_destroy_session(clp->cl_session, cred);
1818 if (status && status != -NFS4ERR_BADSESSION &&
1819 status != -NFS4ERR_DEADSESSION) {
1820 status = nfs4_recovery_handle_error(clp, status);
1824 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
1825 status = nfs4_proc_create_session(clp, cred);
1827 dprintk("%s: session reset failed with status %d for server %s!\n",
1828 __func__, status, clp->cl_hostname);
1829 status = nfs4_handle_reclaim_lease_error(clp, status);
1832 nfs41_finish_session_reset(clp);
1833 dprintk("%s: session reset was successful for server %s!\n",
1834 __func__, clp->cl_hostname);
1841 static int nfs4_recall_slot(struct nfs_client *clp)
1843 struct nfs4_slot_table *fc_tbl;
1844 struct nfs4_slot *new, *old;
1847 if (!nfs4_has_session(clp))
1849 nfs4_begin_drain_session(clp);
1850 fc_tbl = &clp->cl_session->fc_slot_table;
1851 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
1856 spin_lock(&fc_tbl->slot_tbl_lock);
1857 for (i = 0; i < fc_tbl->target_max_slots; i++)
1858 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1859 old = fc_tbl->slots;
1860 fc_tbl->slots = new;
1861 fc_tbl->max_slots = fc_tbl->target_max_slots;
1862 fc_tbl->target_max_slots = 0;
1863 clp->cl_session->fc_attrs.max_reqs = fc_tbl->max_slots;
1864 spin_unlock(&fc_tbl->slot_tbl_lock);
1870 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
1872 struct rpc_cred *cred;
1875 if (!nfs4_has_session(clp))
1877 nfs4_begin_drain_session(clp);
1878 cred = nfs4_get_exchange_id_cred(clp);
1879 ret = nfs4_proc_bind_conn_to_session(clp, cred);
1882 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1885 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
1886 __func__, clp->cl_hostname);
1888 case -NFS4ERR_DELAY:
1890 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1893 return nfs4_recovery_handle_error(clp, ret);
1897 #else /* CONFIG_NFS_V4_1 */
1898 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
1899 static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
1900 static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
1902 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
1906 #endif /* CONFIG_NFS_V4_1 */
1908 static void nfs4_state_manager(struct nfs_client *clp)
1911 const char *section = "", *section_sep = "";
1913 /* Ensure exclusive access to NFSv4 state */
1915 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
1916 section = "purge state";
1917 status = nfs4_purge_lease(clp);
1923 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1924 section = "lease expired";
1925 /* We're going to have to re-establish a clientid */
1926 status = nfs4_reclaim_lease(clp);
1932 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1933 section = "check lease";
1934 status = nfs4_check_lease(clp);
1937 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1941 /* Initialize or reset the session */
1942 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
1943 section = "reset session";
1944 status = nfs4_reset_session(clp);
1945 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1951 /* Send BIND_CONN_TO_SESSION */
1952 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1954 section = "bind conn to session";
1955 status = nfs4_bind_conn_to_session(clp);
1961 /* Recall session slots */
1962 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)) {
1963 section = "recall slot";
1964 status = nfs4_recall_slot(clp);
1970 /* First recover reboot state... */
1971 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
1972 section = "reclaim reboot";
1973 status = nfs4_do_reclaim(clp,
1974 clp->cl_mvops->reboot_recovery_ops);
1975 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
1976 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
1978 nfs4_state_end_reclaim_reboot(clp);
1979 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1985 /* Now recover expired state... */
1986 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
1987 section = "reclaim nograce";
1988 status = nfs4_do_reclaim(clp,
1989 clp->cl_mvops->nograce_recovery_ops);
1990 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
1991 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
1992 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1998 nfs4_end_drain_session(clp);
1999 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2000 nfs_client_return_marked_delegations(clp);
2004 nfs4_clear_state_manager_bit(clp);
2005 /* Did we race with an attempt to give us more work? */
2006 if (clp->cl_state == 0)
2008 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2010 } while (atomic_read(&clp->cl_count) > 1);
2013 if (strlen(section))
2015 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2016 " with error %d\n", section_sep, section,
2017 clp->cl_hostname, -status);
2019 nfs4_end_drain_session(clp);
2020 nfs4_clear_state_manager_bit(clp);
2023 static int nfs4_run_state_manager(void *ptr)
2025 struct nfs_client *clp = ptr;
2027 allow_signal(SIGKILL);
2028 nfs4_state_manager(clp);
2029 nfs_put_client(clp);
2030 module_put_and_exit(0);