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);
71 int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
73 struct nfs4_setclientid_res clid = {
74 .clientid = clp->cl_clientid,
75 .confirm = clp->cl_confirm,
79 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
81 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
83 port = nn->nfs_callback_tcpport;
84 if (clp->cl_addr.ss_family == AF_INET6)
85 port = nn->nfs_callback_tcpport6;
87 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
90 clp->cl_clientid = clid.clientid;
91 clp->cl_confirm = clid.confirm;
92 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
94 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
97 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
98 nfs4_schedule_state_renewal(clp);
104 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
106 * @clp: nfs_client under test
107 * @result: OUT: found nfs_client, or clp
108 * @cred: credential to use for trunking test
110 * Returns zero, a negative errno, or a negative NFS4ERR status.
111 * If zero is returned, an nfs_client pointer is planted in
114 * Note: The returned client may not yet be marked ready.
116 int nfs40_discover_server_trunking(struct nfs_client *clp,
117 struct nfs_client **result,
118 struct rpc_cred *cred)
120 struct nfs4_setclientid_res clid = {
121 .clientid = clp->cl_clientid,
122 .confirm = clp->cl_confirm,
124 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
128 port = nn->nfs_callback_tcpport;
129 if (clp->cl_addr.ss_family == AF_INET6)
130 port = nn->nfs_callback_tcpport6;
132 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
135 clp->cl_clientid = clid.clientid;
136 clp->cl_confirm = clid.confirm;
138 status = nfs40_walk_client_list(clp, result, cred);
140 /* Sustain the lease, even if it's empty. If the clientid4
141 * goes stale it's of no use for trunking discovery. */
142 nfs4_schedule_state_renewal(*result);
148 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
150 struct rpc_cred *cred = NULL;
152 if (clp->cl_machine_cred != NULL)
153 cred = get_rpccred(clp->cl_machine_cred);
157 static void nfs4_root_machine_cred(struct nfs_client *clp)
159 struct rpc_cred *cred, *new;
161 new = rpc_lookup_machine_cred(NULL);
162 spin_lock(&clp->cl_lock);
163 cred = clp->cl_machine_cred;
164 clp->cl_machine_cred = new;
165 spin_unlock(&clp->cl_lock);
170 static struct rpc_cred *
171 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
173 struct rpc_cred *cred = NULL;
174 struct nfs4_state_owner *sp;
177 for (pos = rb_first(&server->state_owners);
179 pos = rb_next(pos)) {
180 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
181 if (list_empty(&sp->so_states))
183 cred = get_rpccred(sp->so_cred);
190 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
191 * @clp: client state handle
193 * Returns an rpc_cred with reference count bumped, or NULL.
194 * Caller must hold clp->cl_lock.
196 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
198 struct rpc_cred *cred = NULL;
199 struct nfs_server *server;
201 /* Use machine credentials if available */
202 cred = nfs4_get_machine_cred_locked(clp);
207 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
208 cred = nfs4_get_renew_cred_server_locked(server);
218 #if defined(CONFIG_NFS_V4_1)
220 static int nfs41_setup_state_renewal(struct nfs_client *clp)
223 struct nfs_fsinfo fsinfo;
225 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
226 nfs4_schedule_state_renewal(clp);
230 status = nfs4_proc_get_lease_time(clp, &fsinfo);
232 /* Update lease time and schedule renewal */
233 spin_lock(&clp->cl_lock);
234 clp->cl_lease_time = fsinfo.lease_time * HZ;
235 clp->cl_last_renewal = jiffies;
236 spin_unlock(&clp->cl_lock);
238 nfs4_schedule_state_renewal(clp);
244 static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
246 if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
247 spin_lock(&tbl->slot_tbl_lock);
248 nfs41_wake_slot_table(tbl);
249 spin_unlock(&tbl->slot_tbl_lock);
253 static void nfs4_end_drain_session(struct nfs_client *clp)
255 struct nfs4_session *ses = clp->cl_session;
258 nfs4_end_drain_slot_table(&ses->bc_slot_table);
259 nfs4_end_drain_slot_table(&ses->fc_slot_table);
263 static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
265 set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
266 spin_lock(&tbl->slot_tbl_lock);
267 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
268 INIT_COMPLETION(tbl->complete);
269 spin_unlock(&tbl->slot_tbl_lock);
270 return wait_for_completion_interruptible(&tbl->complete);
272 spin_unlock(&tbl->slot_tbl_lock);
276 static int nfs4_begin_drain_session(struct nfs_client *clp)
278 struct nfs4_session *ses = clp->cl_session;
282 ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
286 return nfs4_drain_slot_tbl(&ses->fc_slot_table);
289 static void nfs41_finish_session_reset(struct nfs_client *clp)
291 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
292 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
293 /* create_session negotiated new slot table */
294 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
295 nfs41_setup_state_renewal(clp);
298 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
302 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
304 nfs4_begin_drain_session(clp);
305 status = nfs4_proc_exchange_id(clp, cred);
308 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
310 status = nfs4_proc_create_session(clp, cred);
313 nfs41_finish_session_reset(clp);
314 nfs_mark_client_ready(clp, NFS_CS_READY);
320 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
322 * @clp: nfs_client under test
323 * @result: OUT: found nfs_client, or clp
324 * @cred: credential to use for trunking test
326 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
327 * If NFS4_OK is returned, an nfs_client pointer is planted in
330 * Note: The returned client may not yet be marked ready.
332 int nfs41_discover_server_trunking(struct nfs_client *clp,
333 struct nfs_client **result,
334 struct rpc_cred *cred)
338 status = nfs4_proc_exchange_id(clp, cred);
339 if (status != NFS4_OK)
341 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
343 return nfs41_walk_client_list(clp, result, cred);
346 #endif /* CONFIG_NFS_V4_1 */
349 * nfs4_get_clid_cred - Acquire credential for a setclientid operation
350 * @clp: client state handle
352 * Returns an rpc_cred with reference count bumped, or NULL.
354 struct rpc_cred *nfs4_get_clid_cred(struct nfs_client *clp)
356 struct rpc_cred *cred;
358 spin_lock(&clp->cl_lock);
359 cred = nfs4_get_machine_cred_locked(clp);
360 spin_unlock(&clp->cl_lock);
364 static struct nfs4_state_owner *
365 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
367 struct rb_node **p = &server->state_owners.rb_node,
369 struct nfs4_state_owner *sp;
373 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
375 if (cred < sp->so_cred)
376 p = &parent->rb_left;
377 else if (cred > sp->so_cred)
378 p = &parent->rb_right;
380 if (!list_empty(&sp->so_lru))
381 list_del_init(&sp->so_lru);
382 atomic_inc(&sp->so_count);
389 static struct nfs4_state_owner *
390 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
392 struct nfs_server *server = new->so_server;
393 struct rb_node **p = &server->state_owners.rb_node,
395 struct nfs4_state_owner *sp;
400 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
402 if (new->so_cred < sp->so_cred)
403 p = &parent->rb_left;
404 else if (new->so_cred > sp->so_cred)
405 p = &parent->rb_right;
407 if (!list_empty(&sp->so_lru))
408 list_del_init(&sp->so_lru);
409 atomic_inc(&sp->so_count);
413 err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
416 rb_link_node(&new->so_server_node, parent, p);
417 rb_insert_color(&new->so_server_node, &server->state_owners);
422 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
424 struct nfs_server *server = sp->so_server;
426 if (!RB_EMPTY_NODE(&sp->so_server_node))
427 rb_erase(&sp->so_server_node, &server->state_owners);
428 ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
432 nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
434 sc->create_time = ktime_get();
437 spin_lock_init(&sc->lock);
438 INIT_LIST_HEAD(&sc->list);
439 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
443 nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
445 rpc_destroy_wait_queue(&sc->wait);
449 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
450 * create a new state_owner.
453 static struct nfs4_state_owner *
454 nfs4_alloc_state_owner(struct nfs_server *server,
455 struct rpc_cred *cred,
458 struct nfs4_state_owner *sp;
460 sp = kzalloc(sizeof(*sp), gfp_flags);
463 sp->so_server = server;
464 sp->so_cred = get_rpccred(cred);
465 spin_lock_init(&sp->so_lock);
466 INIT_LIST_HEAD(&sp->so_states);
467 nfs4_init_seqid_counter(&sp->so_seqid);
468 atomic_set(&sp->so_count, 1);
469 INIT_LIST_HEAD(&sp->so_lru);
470 seqcount_init(&sp->so_reclaim_seqcount);
471 mutex_init(&sp->so_delegreturn_mutex);
476 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
478 struct rb_node *rb_node = &sp->so_server_node;
480 if (!RB_EMPTY_NODE(rb_node)) {
481 struct nfs_server *server = sp->so_server;
482 struct nfs_client *clp = server->nfs_client;
484 spin_lock(&clp->cl_lock);
485 if (!RB_EMPTY_NODE(rb_node)) {
486 rb_erase(rb_node, &server->state_owners);
487 RB_CLEAR_NODE(rb_node);
489 spin_unlock(&clp->cl_lock);
493 static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
495 nfs4_destroy_seqid_counter(&sp->so_seqid);
496 put_rpccred(sp->so_cred);
500 static void nfs4_gc_state_owners(struct nfs_server *server)
502 struct nfs_client *clp = server->nfs_client;
503 struct nfs4_state_owner *sp, *tmp;
504 unsigned long time_min, time_max;
507 spin_lock(&clp->cl_lock);
509 time_min = (long)time_max - (long)clp->cl_lease_time;
510 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
511 /* NB: LRU is sorted so that oldest is at the head */
512 if (time_in_range(sp->so_expires, time_min, time_max))
514 list_move(&sp->so_lru, &doomed);
515 nfs4_remove_state_owner_locked(sp);
517 spin_unlock(&clp->cl_lock);
519 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
520 list_del(&sp->so_lru);
521 nfs4_free_state_owner(sp);
526 * nfs4_get_state_owner - Look up a state owner given a credential
527 * @server: nfs_server to search
528 * @cred: RPC credential to match
530 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
532 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
533 struct rpc_cred *cred,
536 struct nfs_client *clp = server->nfs_client;
537 struct nfs4_state_owner *sp, *new;
539 spin_lock(&clp->cl_lock);
540 sp = nfs4_find_state_owner_locked(server, cred);
541 spin_unlock(&clp->cl_lock);
544 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
548 if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
550 spin_lock(&clp->cl_lock);
551 sp = nfs4_insert_state_owner_locked(new);
552 spin_unlock(&clp->cl_lock);
553 } while (sp == ERR_PTR(-EAGAIN));
555 nfs4_free_state_owner(new);
557 nfs4_gc_state_owners(server);
562 * nfs4_put_state_owner - Release a nfs4_state_owner
563 * @sp: state owner data to release
565 * Note that we keep released state owners on an LRU
567 * This caches valid state owners so that they can be
568 * reused, to avoid the OPEN_CONFIRM on minor version 0.
569 * It also pins the uniquifier of dropped state owners for
570 * a while, to ensure that those state owner names are
573 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
575 struct nfs_server *server = sp->so_server;
576 struct nfs_client *clp = server->nfs_client;
578 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
581 sp->so_expires = jiffies;
582 list_add_tail(&sp->so_lru, &server->state_owners_lru);
583 spin_unlock(&clp->cl_lock);
587 * nfs4_purge_state_owners - Release all cached state owners
588 * @server: nfs_server with cached state owners to release
590 * Called at umount time. Remaining state owners will be on
591 * the LRU with ref count of zero.
593 void nfs4_purge_state_owners(struct nfs_server *server)
595 struct nfs_client *clp = server->nfs_client;
596 struct nfs4_state_owner *sp, *tmp;
599 spin_lock(&clp->cl_lock);
600 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
601 list_move(&sp->so_lru, &doomed);
602 nfs4_remove_state_owner_locked(sp);
604 spin_unlock(&clp->cl_lock);
606 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
607 list_del(&sp->so_lru);
608 nfs4_free_state_owner(sp);
612 static struct nfs4_state *
613 nfs4_alloc_open_state(void)
615 struct nfs4_state *state;
617 state = kzalloc(sizeof(*state), GFP_NOFS);
620 atomic_set(&state->count, 1);
621 INIT_LIST_HEAD(&state->lock_states);
622 spin_lock_init(&state->state_lock);
623 seqlock_init(&state->seqlock);
628 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
630 if (state->state == fmode)
632 /* NB! List reordering - see the reclaim code for why. */
633 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
634 if (fmode & FMODE_WRITE)
635 list_move(&state->open_states, &state->owner->so_states);
637 list_move_tail(&state->open_states, &state->owner->so_states);
639 state->state = fmode;
642 static struct nfs4_state *
643 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
645 struct nfs_inode *nfsi = NFS_I(inode);
646 struct nfs4_state *state;
648 list_for_each_entry(state, &nfsi->open_states, inode_states) {
649 if (state->owner != owner)
651 if (!nfs4_valid_open_stateid(state))
653 if (atomic_inc_not_zero(&state->count))
660 nfs4_free_open_state(struct nfs4_state *state)
666 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
668 struct nfs4_state *state, *new;
669 struct nfs_inode *nfsi = NFS_I(inode);
671 spin_lock(&inode->i_lock);
672 state = __nfs4_find_state_byowner(inode, owner);
673 spin_unlock(&inode->i_lock);
676 new = nfs4_alloc_open_state();
677 spin_lock(&owner->so_lock);
678 spin_lock(&inode->i_lock);
679 state = __nfs4_find_state_byowner(inode, owner);
680 if (state == NULL && new != NULL) {
682 state->owner = owner;
683 atomic_inc(&owner->so_count);
684 list_add(&state->inode_states, &nfsi->open_states);
686 state->inode = inode;
687 spin_unlock(&inode->i_lock);
688 /* Note: The reclaim code dictates that we add stateless
689 * and read-only stateids to the end of the list */
690 list_add_tail(&state->open_states, &owner->so_states);
691 spin_unlock(&owner->so_lock);
693 spin_unlock(&inode->i_lock);
694 spin_unlock(&owner->so_lock);
696 nfs4_free_open_state(new);
702 void nfs4_put_open_state(struct nfs4_state *state)
704 struct inode *inode = state->inode;
705 struct nfs4_state_owner *owner = state->owner;
707 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
709 spin_lock(&inode->i_lock);
710 list_del(&state->inode_states);
711 list_del(&state->open_states);
712 spin_unlock(&inode->i_lock);
713 spin_unlock(&owner->so_lock);
715 nfs4_free_open_state(state);
716 nfs4_put_state_owner(owner);
720 * Close the current file.
722 static void __nfs4_close(struct nfs4_state *state,
723 fmode_t fmode, gfp_t gfp_mask, int wait)
725 struct nfs4_state_owner *owner = state->owner;
729 atomic_inc(&owner->so_count);
730 /* Protect against nfs4_find_state() */
731 spin_lock(&owner->so_lock);
732 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
739 case FMODE_READ|FMODE_WRITE:
742 newstate = FMODE_READ|FMODE_WRITE;
743 if (state->n_rdwr == 0) {
744 if (state->n_rdonly == 0) {
745 newstate &= ~FMODE_READ;
746 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
747 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
749 if (state->n_wronly == 0) {
750 newstate &= ~FMODE_WRITE;
751 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
752 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
755 clear_bit(NFS_DELEGATED_STATE, &state->flags);
757 nfs4_state_set_mode_locked(state, newstate);
758 spin_unlock(&owner->so_lock);
761 nfs4_put_open_state(state);
762 nfs4_put_state_owner(owner);
764 nfs4_do_close(state, gfp_mask, wait);
767 void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
769 __nfs4_close(state, fmode, GFP_NOFS, 0);
772 void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
774 __nfs4_close(state, fmode, GFP_KERNEL, 1);
778 * Search the state->lock_states for an existing lock_owner
779 * that is compatible with current->files
781 static struct nfs4_lock_state *
782 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
784 struct nfs4_lock_state *pos;
785 list_for_each_entry(pos, &state->lock_states, ls_locks) {
786 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
788 switch (pos->ls_owner.lo_type) {
789 case NFS4_POSIX_LOCK_TYPE:
790 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
793 case NFS4_FLOCK_LOCK_TYPE:
794 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
797 atomic_inc(&pos->ls_count);
804 * Return a compatible lock_state. If no initialized lock_state structure
805 * exists, return an uninitialized one.
808 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)
810 struct nfs4_lock_state *lsp;
811 struct nfs_server *server = state->owner->so_server;
813 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
816 nfs4_init_seqid_counter(&lsp->ls_seqid);
817 atomic_set(&lsp->ls_count, 1);
818 lsp->ls_state = state;
819 lsp->ls_owner.lo_type = type;
820 switch (lsp->ls_owner.lo_type) {
821 case NFS4_FLOCK_LOCK_TYPE:
822 lsp->ls_owner.lo_u.flock_owner = fl_pid;
824 case NFS4_POSIX_LOCK_TYPE:
825 lsp->ls_owner.lo_u.posix_owner = fl_owner;
830 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
831 if (lsp->ls_seqid.owner_id < 0)
833 INIT_LIST_HEAD(&lsp->ls_locks);
840 void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
842 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
843 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
848 * Return a compatible lock_state. If no initialized lock_state structure
849 * exists, return an uninitialized one.
852 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
854 struct nfs4_lock_state *lsp, *new = NULL;
857 spin_lock(&state->state_lock);
858 lsp = __nfs4_find_lock_state(state, owner, pid, type);
862 list_add(&new->ls_locks, &state->lock_states);
863 set_bit(LK_STATE_IN_USE, &state->flags);
868 spin_unlock(&state->state_lock);
869 new = nfs4_alloc_lock_state(state, owner, pid, type);
873 spin_unlock(&state->state_lock);
875 nfs4_free_lock_state(state->owner->so_server, new);
880 * Release reference to lock_state, and free it if we see that
881 * it is no longer in use
883 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
885 struct nfs_server *server;
886 struct nfs4_state *state;
890 state = lsp->ls_state;
891 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
893 list_del(&lsp->ls_locks);
894 if (list_empty(&state->lock_states))
895 clear_bit(LK_STATE_IN_USE, &state->flags);
896 spin_unlock(&state->state_lock);
897 server = state->owner->so_server;
898 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
899 struct nfs_client *clp = server->nfs_client;
901 clp->cl_mvops->free_lock_state(server, lsp);
903 nfs4_free_lock_state(server, lsp);
906 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
908 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
910 dst->fl_u.nfs4_fl.owner = lsp;
911 atomic_inc(&lsp->ls_count);
914 static void nfs4_fl_release_lock(struct file_lock *fl)
916 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
919 static const struct file_lock_operations nfs4_fl_lock_ops = {
920 .fl_copy_lock = nfs4_fl_copy_lock,
921 .fl_release_private = nfs4_fl_release_lock,
924 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
926 struct nfs4_lock_state *lsp;
928 if (fl->fl_ops != NULL)
930 if (fl->fl_flags & FL_POSIX)
931 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
932 else if (fl->fl_flags & FL_FLOCK)
933 lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
934 NFS4_FLOCK_LOCK_TYPE);
939 fl->fl_u.nfs4_fl.owner = lsp;
940 fl->fl_ops = &nfs4_fl_lock_ops;
944 static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
945 struct nfs4_state *state,
946 const struct nfs_lockowner *lockowner)
948 struct nfs4_lock_state *lsp;
954 if (lockowner == NULL)
957 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
960 fl_owner = lockowner->l_owner;
961 fl_pid = lockowner->l_pid;
962 spin_lock(&state->state_lock);
963 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
964 if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
965 nfs4_stateid_copy(dst, &lsp->ls_stateid);
968 if (!list_empty(&lsp->ls_seqid.list))
971 spin_unlock(&state->state_lock);
972 nfs4_put_lock_state(lsp);
977 static int nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
979 const nfs4_stateid *src;
985 seq = read_seqbegin(&state->seqlock);
986 if (test_bit(NFS_OPEN_STATE, &state->flags))
987 src = &state->open_stateid;
988 nfs4_stateid_copy(dst, src);
991 if (!list_empty(&state->owner->so_seqid.list))
993 } while (read_seqretry(&state->seqlock, seq));
998 * Byte-range lock aware utility to initialize the stateid of read/write
1001 int nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
1002 fmode_t fmode, const struct nfs_lockowner *lockowner)
1005 if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
1007 ret = nfs4_copy_lock_stateid(dst, state, lockowner);
1010 ret = nfs4_copy_open_stateid(dst, state);
1012 if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
1017 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1019 struct nfs_seqid *new;
1021 new = kmalloc(sizeof(*new), gfp_mask);
1023 new->sequence = counter;
1024 INIT_LIST_HEAD(&new->list);
1030 void nfs_release_seqid(struct nfs_seqid *seqid)
1032 struct nfs_seqid_counter *sequence;
1034 if (list_empty(&seqid->list))
1036 sequence = seqid->sequence;
1037 spin_lock(&sequence->lock);
1038 list_del_init(&seqid->list);
1039 if (!list_empty(&sequence->list)) {
1040 struct nfs_seqid *next;
1042 next = list_first_entry(&sequence->list,
1043 struct nfs_seqid, list);
1044 rpc_wake_up_queued_task(&sequence->wait, next->task);
1046 spin_unlock(&sequence->lock);
1049 void nfs_free_seqid(struct nfs_seqid *seqid)
1051 nfs_release_seqid(seqid);
1056 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1057 * failed with a seqid incrementing error -
1058 * see comments nfs_fs.h:seqid_mutating_error()
1060 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1065 case -NFS4ERR_BAD_SEQID:
1066 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1068 pr_warn_ratelimited("NFS: v4 server returned a bad"
1069 " sequence-id error on an"
1070 " unconfirmed sequence %p!\n",
1072 case -NFS4ERR_STALE_CLIENTID:
1073 case -NFS4ERR_STALE_STATEID:
1074 case -NFS4ERR_BAD_STATEID:
1075 case -NFS4ERR_BADXDR:
1076 case -NFS4ERR_RESOURCE:
1077 case -NFS4ERR_NOFILEHANDLE:
1078 /* Non-seqid mutating errors */
1082 * Note: no locking needed as we are guaranteed to be first
1083 * on the sequence list
1085 seqid->sequence->counter++;
1088 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1090 struct nfs4_state_owner *sp = container_of(seqid->sequence,
1091 struct nfs4_state_owner, so_seqid);
1092 struct nfs_server *server = sp->so_server;
1094 if (status == -NFS4ERR_BAD_SEQID)
1095 nfs4_drop_state_owner(sp);
1096 if (!nfs4_has_session(server->nfs_client))
1097 nfs_increment_seqid(status, seqid);
1101 * Increment the seqid if the LOCK/LOCKU succeeded, or
1102 * failed with a seqid incrementing error -
1103 * see comments nfs_fs.h:seqid_mutating_error()
1105 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1107 nfs_increment_seqid(status, seqid);
1110 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1112 struct nfs_seqid_counter *sequence = seqid->sequence;
1115 spin_lock(&sequence->lock);
1117 if (list_empty(&seqid->list))
1118 list_add_tail(&seqid->list, &sequence->list);
1119 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1121 rpc_sleep_on(&sequence->wait, task, NULL);
1124 spin_unlock(&sequence->lock);
1128 static int nfs4_run_state_manager(void *);
1130 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1132 smp_mb__before_clear_bit();
1133 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1134 smp_mb__after_clear_bit();
1135 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
1136 rpc_wake_up(&clp->cl_rpcwaitq);
1140 * Schedule the nfs_client asynchronous state management routine
1142 void nfs4_schedule_state_manager(struct nfs_client *clp)
1144 struct task_struct *task;
1145 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1147 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1149 __module_get(THIS_MODULE);
1150 atomic_inc(&clp->cl_count);
1152 /* The rcu_read_lock() is not strictly necessary, as the state
1153 * manager is the only thread that ever changes the rpc_xprt
1154 * after it's initialized. At this point, we're single threaded. */
1156 snprintf(buf, sizeof(buf), "%s-manager",
1157 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1159 task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
1161 printk(KERN_ERR "%s: kthread_run: %ld\n",
1162 __func__, PTR_ERR(task));
1163 nfs4_clear_state_manager_bit(clp);
1164 nfs_put_client(clp);
1165 module_put(THIS_MODULE);
1170 * Schedule a lease recovery attempt
1172 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1176 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1177 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1178 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1180 nfs4_schedule_state_manager(clp);
1182 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1184 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1190 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1191 nfs_wait_bit_killable, TASK_KILLABLE);
1195 if (clp->cl_cons_state < 0)
1196 return clp->cl_cons_state;
1200 int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1205 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1206 ret = nfs4_wait_clnt_recover(clp);
1209 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1210 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1212 nfs4_schedule_state_manager(clp);
1219 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1220 * @clp: client to process
1222 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1223 * resend of the SETCLIENTID and hence re-establish the
1224 * callback channel. Then return all existing delegations.
1226 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1228 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1229 nfs_expire_all_delegations(clp);
1230 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1234 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1236 nfs40_handle_cb_pathdown(clp);
1237 nfs4_schedule_state_manager(clp);
1240 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1243 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1244 /* Don't recover state that expired before the reboot */
1245 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1246 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1249 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1250 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1254 static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1256 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1257 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1258 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1259 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1263 int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1265 struct nfs_client *clp = server->nfs_client;
1267 if (!nfs4_valid_open_stateid(state))
1269 nfs4_state_mark_reclaim_nograce(clp, state);
1270 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1272 nfs4_schedule_state_manager(clp);
1275 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1277 void nfs_inode_find_state_and_recover(struct inode *inode,
1278 const nfs4_stateid *stateid)
1280 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1281 struct nfs_inode *nfsi = NFS_I(inode);
1282 struct nfs_open_context *ctx;
1283 struct nfs4_state *state;
1286 spin_lock(&inode->i_lock);
1287 list_for_each_entry(ctx, &nfsi->open_files, list) {
1291 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1293 if (!nfs4_stateid_match(&state->stateid, stateid))
1295 nfs4_state_mark_reclaim_nograce(clp, state);
1298 spin_unlock(&inode->i_lock);
1300 nfs4_schedule_state_manager(clp);
1303 static void nfs4_state_mark_open_context_bad(struct nfs4_state *state)
1305 struct inode *inode = state->inode;
1306 struct nfs_inode *nfsi = NFS_I(inode);
1307 struct nfs_open_context *ctx;
1309 spin_lock(&inode->i_lock);
1310 list_for_each_entry(ctx, &nfsi->open_files, list) {
1311 if (ctx->state != state)
1313 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1315 spin_unlock(&inode->i_lock);
1318 static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1320 set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
1321 nfs4_state_mark_open_context_bad(state);
1325 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1327 struct inode *inode = state->inode;
1328 struct nfs_inode *nfsi = NFS_I(inode);
1329 struct file_lock *fl;
1332 if (inode->i_flock == NULL)
1335 /* Guard against delegation returns and new lock/unlock calls */
1336 down_write(&nfsi->rwsem);
1337 /* Protect inode->i_flock using the BKL */
1338 spin_lock(&inode->i_lock);
1339 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1340 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1342 if (nfs_file_open_context(fl->fl_file)->state != state)
1344 spin_unlock(&inode->i_lock);
1345 status = ops->recover_lock(state, fl);
1350 case -NFS4ERR_ADMIN_REVOKED:
1351 case -NFS4ERR_STALE_STATEID:
1352 case -NFS4ERR_BAD_STATEID:
1353 case -NFS4ERR_EXPIRED:
1354 case -NFS4ERR_NO_GRACE:
1355 case -NFS4ERR_STALE_CLIENTID:
1356 case -NFS4ERR_BADSESSION:
1357 case -NFS4ERR_BADSLOT:
1358 case -NFS4ERR_BAD_HIGH_SLOT:
1359 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1362 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1363 "Zeroing state\n", __func__, status);
1365 case -NFS4ERR_DENIED:
1366 case -NFS4ERR_RECLAIM_BAD:
1367 case -NFS4ERR_RECLAIM_CONFLICT:
1368 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1371 spin_lock(&inode->i_lock);
1373 spin_unlock(&inode->i_lock);
1375 up_write(&nfsi->rwsem);
1379 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1381 struct nfs4_state *state;
1382 struct nfs4_lock_state *lock;
1385 /* Note: we rely on the sp->so_states list being ordered
1386 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1388 * This is needed to ensure that the server won't give us any
1389 * read delegations that we have to return if, say, we are
1390 * recovering after a network partition or a reboot from a
1391 * server that doesn't support a grace period.
1393 spin_lock(&sp->so_lock);
1394 write_seqcount_begin(&sp->so_reclaim_seqcount);
1396 list_for_each_entry(state, &sp->so_states, open_states) {
1397 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1399 if (!nfs4_valid_open_stateid(state))
1401 if (state->state == 0)
1403 atomic_inc(&state->count);
1404 spin_unlock(&sp->so_lock);
1405 status = ops->recover_open(sp, state);
1407 status = nfs4_reclaim_locks(state, ops);
1409 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) {
1410 spin_lock(&state->state_lock);
1411 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1412 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1413 pr_warn_ratelimited("NFS: "
1415 "failed!\n", __func__);
1417 spin_unlock(&state->state_lock);
1419 nfs4_put_open_state(state);
1420 spin_lock(&sp->so_lock);
1426 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1427 "Zeroing state\n", __func__, status);
1432 * Open state on this file cannot be recovered
1433 * All we can do is revert to using the zero stateid.
1435 nfs4_state_mark_recovery_failed(state, status);
1439 case -NFS4ERR_ADMIN_REVOKED:
1440 case -NFS4ERR_STALE_STATEID:
1441 case -NFS4ERR_BAD_STATEID:
1442 case -NFS4ERR_RECLAIM_BAD:
1443 case -NFS4ERR_RECLAIM_CONFLICT:
1444 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1446 case -NFS4ERR_EXPIRED:
1447 case -NFS4ERR_NO_GRACE:
1448 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1449 case -NFS4ERR_STALE_CLIENTID:
1450 case -NFS4ERR_BADSESSION:
1451 case -NFS4ERR_BADSLOT:
1452 case -NFS4ERR_BAD_HIGH_SLOT:
1453 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1456 nfs4_put_open_state(state);
1457 spin_lock(&sp->so_lock);
1460 write_seqcount_end(&sp->so_reclaim_seqcount);
1461 spin_unlock(&sp->so_lock);
1464 nfs4_put_open_state(state);
1465 spin_lock(&sp->so_lock);
1466 write_seqcount_end(&sp->so_reclaim_seqcount);
1467 spin_unlock(&sp->so_lock);
1471 static void nfs4_clear_open_state(struct nfs4_state *state)
1473 struct nfs4_lock_state *lock;
1475 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1476 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1477 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1478 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1479 spin_lock(&state->state_lock);
1480 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1481 lock->ls_seqid.flags = 0;
1482 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1484 spin_unlock(&state->state_lock);
1487 static void nfs4_reset_seqids(struct nfs_server *server,
1488 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1490 struct nfs_client *clp = server->nfs_client;
1491 struct nfs4_state_owner *sp;
1492 struct rb_node *pos;
1493 struct nfs4_state *state;
1495 spin_lock(&clp->cl_lock);
1496 for (pos = rb_first(&server->state_owners);
1498 pos = rb_next(pos)) {
1499 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1500 sp->so_seqid.flags = 0;
1501 spin_lock(&sp->so_lock);
1502 list_for_each_entry(state, &sp->so_states, open_states) {
1503 if (mark_reclaim(clp, state))
1504 nfs4_clear_open_state(state);
1506 spin_unlock(&sp->so_lock);
1508 spin_unlock(&clp->cl_lock);
1511 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1512 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1514 struct nfs_server *server;
1517 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1518 nfs4_reset_seqids(server, mark_reclaim);
1522 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1524 /* Mark all delegations for reclaim */
1525 nfs_delegation_mark_reclaim(clp);
1526 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1529 static void nfs4_reclaim_complete(struct nfs_client *clp,
1530 const struct nfs4_state_recovery_ops *ops,
1531 struct rpc_cred *cred)
1533 /* Notify the server we're done reclaiming our state */
1534 if (ops->reclaim_complete)
1535 (void)ops->reclaim_complete(clp, cred);
1538 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1540 struct nfs_client *clp = server->nfs_client;
1541 struct nfs4_state_owner *sp;
1542 struct rb_node *pos;
1543 struct nfs4_state *state;
1545 spin_lock(&clp->cl_lock);
1546 for (pos = rb_first(&server->state_owners);
1548 pos = rb_next(pos)) {
1549 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1550 spin_lock(&sp->so_lock);
1551 list_for_each_entry(state, &sp->so_states, open_states) {
1552 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1555 nfs4_state_mark_reclaim_nograce(clp, state);
1557 spin_unlock(&sp->so_lock);
1559 spin_unlock(&clp->cl_lock);
1562 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1564 struct nfs_server *server;
1566 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1570 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1571 nfs4_clear_reclaim_server(server);
1574 nfs_delegation_reap_unclaimed(clp);
1578 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1580 const struct nfs4_state_recovery_ops *ops;
1581 struct rpc_cred *cred;
1583 if (!nfs4_state_clear_reclaim_reboot(clp))
1585 ops = clp->cl_mvops->reboot_recovery_ops;
1586 cred = nfs4_get_clid_cred(clp);
1587 nfs4_reclaim_complete(clp, ops, cred);
1591 static void nfs_delegation_clear_all(struct nfs_client *clp)
1593 nfs_delegation_mark_reclaim(clp);
1594 nfs_delegation_reap_unclaimed(clp);
1597 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1599 nfs_delegation_clear_all(clp);
1600 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1603 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1608 case -NFS4ERR_CB_PATH_DOWN:
1609 nfs40_handle_cb_pathdown(clp);
1611 case -NFS4ERR_NO_GRACE:
1612 nfs4_state_end_reclaim_reboot(clp);
1614 case -NFS4ERR_STALE_CLIENTID:
1615 case -NFS4ERR_LEASE_MOVED:
1616 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1617 nfs4_state_clear_reclaim_reboot(clp);
1618 nfs4_state_start_reclaim_reboot(clp);
1620 case -NFS4ERR_EXPIRED:
1621 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1622 nfs4_state_start_reclaim_nograce(clp);
1624 case -NFS4ERR_BADSESSION:
1625 case -NFS4ERR_BADSLOT:
1626 case -NFS4ERR_BAD_HIGH_SLOT:
1627 case -NFS4ERR_DEADSESSION:
1628 case -NFS4ERR_SEQ_FALSE_RETRY:
1629 case -NFS4ERR_SEQ_MISORDERED:
1630 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1631 /* Zero session reset errors */
1633 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1634 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1637 dprintk("%s: failed to handle error %d for server %s\n",
1638 __func__, error, clp->cl_hostname);
1641 dprintk("%s: handled error %d for server %s\n", __func__, error,
1646 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1648 struct nfs4_state_owner *sp;
1649 struct nfs_server *server;
1650 struct rb_node *pos;
1655 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1656 nfs4_purge_state_owners(server);
1657 spin_lock(&clp->cl_lock);
1658 for (pos = rb_first(&server->state_owners);
1660 pos = rb_next(pos)) {
1662 struct nfs4_state_owner, so_server_node);
1663 if (!test_and_clear_bit(ops->owner_flag_bit,
1666 atomic_inc(&sp->so_count);
1667 spin_unlock(&clp->cl_lock);
1670 status = nfs4_reclaim_open_state(sp, ops);
1672 set_bit(ops->owner_flag_bit, &sp->so_flags);
1673 nfs4_put_state_owner(sp);
1674 return nfs4_recovery_handle_error(clp, status);
1677 nfs4_put_state_owner(sp);
1680 spin_unlock(&clp->cl_lock);
1686 static int nfs4_check_lease(struct nfs_client *clp)
1688 struct rpc_cred *cred;
1689 const struct nfs4_state_maintenance_ops *ops =
1690 clp->cl_mvops->state_renewal_ops;
1693 /* Is the client already known to have an expired lease? */
1694 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1696 spin_lock(&clp->cl_lock);
1697 cred = ops->get_state_renewal_cred_locked(clp);
1698 spin_unlock(&clp->cl_lock);
1700 cred = nfs4_get_clid_cred(clp);
1705 status = ops->renew_lease(clp, cred);
1707 if (status == -ETIMEDOUT) {
1708 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1712 return nfs4_recovery_handle_error(clp, status);
1715 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1716 * and for recoverable errors on EXCHANGE_ID for v4.1
1718 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1721 case -NFS4ERR_SEQ_MISORDERED:
1722 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1723 return -ESERVERFAULT;
1724 /* Lease confirmation error: retry after purging the lease */
1726 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1728 case -NFS4ERR_STALE_CLIENTID:
1729 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1730 nfs4_state_clear_reclaim_reboot(clp);
1731 nfs4_state_start_reclaim_reboot(clp);
1733 case -NFS4ERR_CLID_INUSE:
1734 pr_err("NFS: Server %s reports our clientid is in use\n",
1736 nfs_mark_client_ready(clp, -EPERM);
1737 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1740 case -NFS4ERR_DELAY:
1746 case -NFS4ERR_MINOR_VERS_MISMATCH:
1747 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1748 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1749 dprintk("%s: exit with error %d for server %s\n",
1750 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1751 return -EPROTONOSUPPORT;
1752 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1753 * in nfs4_exchange_id */
1755 dprintk("%s: exit with error %d for server %s\n", __func__,
1756 status, clp->cl_hostname);
1759 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1760 dprintk("%s: handled error %d for server %s\n", __func__, status,
1765 static int nfs4_establish_lease(struct nfs_client *clp)
1767 struct rpc_cred *cred;
1768 const struct nfs4_state_recovery_ops *ops =
1769 clp->cl_mvops->reboot_recovery_ops;
1772 cred = nfs4_get_clid_cred(clp);
1775 status = ops->establish_clid(clp, cred);
1779 pnfs_destroy_all_layouts(clp);
1784 * Returns zero or a negative errno. NFS4ERR values are converted
1785 * to local errno values.
1787 static int nfs4_reclaim_lease(struct nfs_client *clp)
1791 status = nfs4_establish_lease(clp);
1793 return nfs4_handle_reclaim_lease_error(clp, status);
1794 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1795 nfs4_state_start_reclaim_nograce(clp);
1796 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1797 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1798 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1799 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1803 static int nfs4_purge_lease(struct nfs_client *clp)
1807 status = nfs4_establish_lease(clp);
1809 return nfs4_handle_reclaim_lease_error(clp, status);
1810 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1811 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1812 nfs4_state_start_reclaim_nograce(clp);
1817 * nfs4_discover_server_trunking - Detect server IP address trunking
1819 * @clp: nfs_client under test
1820 * @result: OUT: found nfs_client, or clp
1822 * Returns zero or a negative errno. If zero is returned,
1823 * an nfs_client pointer is planted in "result".
1825 * Note: since we are invoked in process context, and
1826 * not from inside the state manager, we cannot use
1827 * nfs4_handle_reclaim_lease_error().
1829 int nfs4_discover_server_trunking(struct nfs_client *clp,
1830 struct nfs_client **result)
1832 const struct nfs4_state_recovery_ops *ops =
1833 clp->cl_mvops->reboot_recovery_ops;
1834 struct rpc_clnt *clnt;
1835 struct rpc_cred *cred;
1838 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
1840 clnt = clp->cl_rpcclient;
1843 mutex_lock(&nfs_clid_init_mutex);
1846 cred = nfs4_get_clid_cred(clp);
1850 status = ops->detect_trunking(clp, result, cred);
1855 case -NFS4ERR_DELAY:
1859 case -NFS4ERR_STALE_CLIENTID:
1860 dprintk("NFS: %s after status %d, retrying\n",
1865 nfs4_root_machine_cred(clp);
1870 case -NFS4ERR_CLID_INUSE:
1871 case -NFS4ERR_WRONGSEC:
1872 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
1874 status = PTR_ERR(clnt);
1877 /* Note: this is safe because we haven't yet marked the
1878 * client as ready, so we are the only user of
1881 clnt = xchg(&clp->cl_rpcclient, clnt);
1882 rpc_shutdown_client(clnt);
1883 clnt = clp->cl_rpcclient;
1886 case -NFS4ERR_MINOR_VERS_MISMATCH:
1887 status = -EPROTONOSUPPORT;
1891 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1892 * in nfs4_exchange_id */
1893 status = -EKEYEXPIRED;
1896 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
1902 mutex_unlock(&nfs_clid_init_mutex);
1903 dprintk("NFS: %s: status = %d\n", __func__, status);
1907 #ifdef CONFIG_NFS_V4_1
1908 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
1910 struct nfs_client *clp = session->clp;
1914 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1916 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1917 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1919 nfs4_schedule_lease_recovery(clp);
1921 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
1923 static void nfs41_ping_server(struct nfs_client *clp)
1925 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
1926 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1927 nfs4_schedule_state_manager(clp);
1930 void nfs41_server_notify_target_slotid_update(struct nfs_client *clp)
1932 nfs41_ping_server(clp);
1935 void nfs41_server_notify_highest_slotid_update(struct nfs_client *clp)
1937 nfs41_ping_server(clp);
1940 static void nfs4_reset_all_state(struct nfs_client *clp)
1942 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1943 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1944 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1945 nfs4_state_start_reclaim_nograce(clp);
1946 dprintk("%s: scheduling reset of all state for server %s!\n",
1947 __func__, clp->cl_hostname);
1948 nfs4_schedule_state_manager(clp);
1952 static void nfs41_handle_server_reboot(struct nfs_client *clp)
1954 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1955 nfs4_state_start_reclaim_reboot(clp);
1956 dprintk("%s: server %s rebooted!\n", __func__,
1958 nfs4_schedule_state_manager(clp);
1962 static void nfs41_handle_state_revoked(struct nfs_client *clp)
1964 nfs4_reset_all_state(clp);
1965 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
1968 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1970 /* This will need to handle layouts too */
1971 nfs_expire_all_delegations(clp);
1972 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
1976 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
1978 nfs_expire_all_delegations(clp);
1979 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
1980 nfs4_schedule_state_manager(clp);
1981 dprintk("%s: server %s declared a backchannel fault\n", __func__,
1985 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1987 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1988 &clp->cl_state) == 0)
1989 nfs4_schedule_state_manager(clp);
1992 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1997 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
1998 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2000 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2001 nfs41_handle_server_reboot(clp);
2002 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
2003 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2004 SEQ4_STATUS_ADMIN_STATE_REVOKED |
2005 SEQ4_STATUS_LEASE_MOVED))
2006 nfs41_handle_state_revoked(clp);
2007 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2008 nfs41_handle_recallable_state_revoked(clp);
2009 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2010 nfs41_handle_backchannel_fault(clp);
2011 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2012 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2013 nfs41_handle_cb_path_down(clp);
2016 static int nfs4_reset_session(struct nfs_client *clp)
2018 struct rpc_cred *cred;
2021 if (!nfs4_has_session(clp))
2023 nfs4_begin_drain_session(clp);
2024 cred = nfs4_get_clid_cred(clp);
2025 status = nfs4_proc_destroy_session(clp->cl_session, cred);
2028 case -NFS4ERR_BADSESSION:
2029 case -NFS4ERR_DEADSESSION:
2031 case -NFS4ERR_BACK_CHAN_BUSY:
2032 case -NFS4ERR_DELAY:
2033 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2038 status = nfs4_recovery_handle_error(clp, status);
2042 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2043 status = nfs4_proc_create_session(clp, cred);
2045 dprintk("%s: session reset failed with status %d for server %s!\n",
2046 __func__, status, clp->cl_hostname);
2047 status = nfs4_handle_reclaim_lease_error(clp, status);
2050 nfs41_finish_session_reset(clp);
2051 dprintk("%s: session reset was successful for server %s!\n",
2052 __func__, clp->cl_hostname);
2059 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2061 struct rpc_cred *cred;
2064 if (!nfs4_has_session(clp))
2066 nfs4_begin_drain_session(clp);
2067 cred = nfs4_get_clid_cred(clp);
2068 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2071 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2074 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2075 __func__, clp->cl_hostname);
2077 case -NFS4ERR_DELAY:
2079 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2082 return nfs4_recovery_handle_error(clp, ret);
2086 #else /* CONFIG_NFS_V4_1 */
2087 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2088 static void nfs4_end_drain_session(struct nfs_client *clp) { }
2090 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2094 #endif /* CONFIG_NFS_V4_1 */
2096 static void nfs4_state_manager(struct nfs_client *clp)
2099 const char *section = "", *section_sep = "";
2101 /* Ensure exclusive access to NFSv4 state */
2103 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2104 section = "purge state";
2105 status = nfs4_purge_lease(clp);
2111 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2112 section = "lease expired";
2113 /* We're going to have to re-establish a clientid */
2114 status = nfs4_reclaim_lease(clp);
2120 /* Initialize or reset the session */
2121 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2122 section = "reset session";
2123 status = nfs4_reset_session(clp);
2124 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2130 /* Send BIND_CONN_TO_SESSION */
2131 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2133 section = "bind conn to session";
2134 status = nfs4_bind_conn_to_session(clp);
2140 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2141 section = "check lease";
2142 status = nfs4_check_lease(clp);
2148 /* First recover reboot state... */
2149 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2150 section = "reclaim reboot";
2151 status = nfs4_do_reclaim(clp,
2152 clp->cl_mvops->reboot_recovery_ops);
2153 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
2154 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
2156 nfs4_state_end_reclaim_reboot(clp);
2157 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2163 /* Now recover expired state... */
2164 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2165 section = "reclaim nograce";
2166 status = nfs4_do_reclaim(clp,
2167 clp->cl_mvops->nograce_recovery_ops);
2168 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
2169 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
2170 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
2176 nfs4_end_drain_session(clp);
2177 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2178 nfs_client_return_marked_delegations(clp);
2182 nfs4_clear_state_manager_bit(clp);
2183 /* Did we race with an attempt to give us more work? */
2184 if (clp->cl_state == 0)
2186 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2188 } while (atomic_read(&clp->cl_count) > 1);
2191 if (strlen(section))
2193 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2194 " with error %d\n", section_sep, section,
2195 clp->cl_hostname, -status);
2197 nfs4_end_drain_session(clp);
2198 nfs4_clear_state_manager_bit(clp);
2201 static int nfs4_run_state_manager(void *ptr)
2203 struct nfs_client *clp = ptr;
2205 allow_signal(SIGKILL);
2206 nfs4_state_manager(clp);
2207 nfs_put_client(clp);
2208 module_put_and_exit(0);