4 * Client-side XDR for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Implementation of the NFSv4 state model. For the time being,
37 * this is minimal, but will be made much more complex in a
41 #include <linux/kernel.h>
42 #include <linux/slab.h>
44 #include <linux/nfs_fs.h>
45 #include <linux/nfs_idmap.h>
46 #include <linux/kthread.h>
47 #include <linux/module.h>
48 #include <linux/random.h>
49 #include <linux/ratelimit.h>
50 #include <linux/workqueue.h>
51 #include <linux/bitops.h>
52 #include <linux/jiffies.h>
54 #include <linux/sunrpc/clnt.h>
58 #include "delegation.h"
60 #include "nfs4session.h"
64 #define NFSDBG_FACILITY NFSDBG_STATE
66 #define OPENOWNER_POOL_SIZE 8
68 const nfs4_stateid zero_stateid;
69 static DEFINE_MUTEX(nfs_clid_init_mutex);
70 static LIST_HEAD(nfs4_clientid_list);
72 int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
74 struct nfs4_setclientid_res clid = {
75 .clientid = clp->cl_clientid,
76 .confirm = clp->cl_confirm,
80 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
82 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
84 port = nn->nfs_callback_tcpport;
85 if (clp->cl_addr.ss_family == AF_INET6)
86 port = nn->nfs_callback_tcpport6;
88 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
91 clp->cl_clientid = clid.clientid;
92 clp->cl_confirm = clid.confirm;
93 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
95 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
98 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
99 nfs4_schedule_state_renewal(clp);
105 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
107 * @clp: nfs_client under test
108 * @result: OUT: found nfs_client, or clp
109 * @cred: credential to use for trunking test
111 * Returns zero, a negative errno, or a negative NFS4ERR status.
112 * If zero is returned, an nfs_client pointer is planted in
115 * Note: The returned client may not yet be marked ready.
117 int nfs40_discover_server_trunking(struct nfs_client *clp,
118 struct nfs_client **result,
119 struct rpc_cred *cred)
121 struct nfs4_setclientid_res clid = {
122 .clientid = clp->cl_clientid,
123 .confirm = clp->cl_confirm,
125 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
129 port = nn->nfs_callback_tcpport;
130 if (clp->cl_addr.ss_family == AF_INET6)
131 port = nn->nfs_callback_tcpport6;
133 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
136 clp->cl_clientid = clid.clientid;
137 clp->cl_confirm = clid.confirm;
139 status = nfs40_walk_client_list(clp, result, cred);
141 case -NFS4ERR_STALE_CLIENTID:
142 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
144 /* Sustain the lease, even if it's empty. If the clientid4
145 * goes stale it's of no use for trunking discovery. */
146 nfs4_schedule_state_renewal(*result);
154 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
156 struct rpc_cred *cred = NULL;
158 if (clp->cl_machine_cred != NULL)
159 cred = get_rpccred(clp->cl_machine_cred);
163 static void nfs4_clear_machine_cred(struct nfs_client *clp)
165 struct rpc_cred *cred;
167 spin_lock(&clp->cl_lock);
168 cred = clp->cl_machine_cred;
169 clp->cl_machine_cred = NULL;
170 spin_unlock(&clp->cl_lock);
175 static struct rpc_cred *
176 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
178 struct rpc_cred *cred = NULL;
179 struct nfs4_state_owner *sp;
182 for (pos = rb_first(&server->state_owners);
184 pos = rb_next(pos)) {
185 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
186 if (list_empty(&sp->so_states))
188 cred = get_rpccred(sp->so_cred);
195 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
196 * @clp: client state handle
198 * Returns an rpc_cred with reference count bumped, or NULL.
199 * Caller must hold clp->cl_lock.
201 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
203 struct rpc_cred *cred = NULL;
204 struct nfs_server *server;
206 /* Use machine credentials if available */
207 cred = nfs4_get_machine_cred_locked(clp);
212 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
213 cred = nfs4_get_renew_cred_server_locked(server);
223 #if defined(CONFIG_NFS_V4_1)
225 static int nfs41_setup_state_renewal(struct nfs_client *clp)
228 struct nfs_fsinfo fsinfo;
230 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
231 nfs4_schedule_state_renewal(clp);
235 status = nfs4_proc_get_lease_time(clp, &fsinfo);
237 /* Update lease time and schedule renewal */
238 spin_lock(&clp->cl_lock);
239 clp->cl_lease_time = fsinfo.lease_time * HZ;
240 clp->cl_last_renewal = jiffies;
241 spin_unlock(&clp->cl_lock);
243 nfs4_schedule_state_renewal(clp);
250 * Back channel returns NFS4ERR_DELAY for new requests when
251 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
254 static void nfs4_end_drain_session(struct nfs_client *clp)
256 struct nfs4_session *ses = clp->cl_session;
257 struct nfs4_slot_table *tbl;
261 tbl = &ses->fc_slot_table;
262 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
263 spin_lock(&tbl->slot_tbl_lock);
264 nfs41_wake_slot_table(tbl);
265 spin_unlock(&tbl->slot_tbl_lock);
270 * Signal state manager thread if session fore channel is drained
272 void nfs4_session_drain_complete(struct nfs4_session *session,
273 struct nfs4_slot_table *tbl)
275 if (nfs4_session_draining(session))
276 complete(&tbl->complete);
279 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
281 spin_lock(&tbl->slot_tbl_lock);
282 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
283 INIT_COMPLETION(tbl->complete);
284 spin_unlock(&tbl->slot_tbl_lock);
285 return wait_for_completion_interruptible(&tbl->complete);
287 spin_unlock(&tbl->slot_tbl_lock);
291 static int nfs4_begin_drain_session(struct nfs_client *clp)
293 struct nfs4_session *ses = clp->cl_session;
296 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
298 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
302 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
305 static void nfs41_finish_session_reset(struct nfs_client *clp)
307 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
308 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
309 /* create_session negotiated new slot table */
310 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
311 nfs41_setup_state_renewal(clp);
314 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
318 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
320 nfs4_begin_drain_session(clp);
321 status = nfs4_proc_exchange_id(clp, cred);
324 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
326 status = nfs4_proc_create_session(clp, cred);
329 nfs41_finish_session_reset(clp);
330 nfs_mark_client_ready(clp, NFS_CS_READY);
336 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
338 * @clp: nfs_client under test
339 * @result: OUT: found nfs_client, or clp
340 * @cred: credential to use for trunking test
342 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
343 * If NFS4_OK is returned, an nfs_client pointer is planted in
346 * Note: The returned client may not yet be marked ready.
348 int nfs41_discover_server_trunking(struct nfs_client *clp,
349 struct nfs_client **result,
350 struct rpc_cred *cred)
354 status = nfs4_proc_exchange_id(clp, cred);
355 if (status != NFS4_OK)
357 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
359 return nfs41_walk_client_list(clp, result, cred);
362 struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
364 struct rpc_cred *cred;
366 spin_lock(&clp->cl_lock);
367 cred = nfs4_get_machine_cred_locked(clp);
368 spin_unlock(&clp->cl_lock);
372 #endif /* CONFIG_NFS_V4_1 */
374 static struct rpc_cred *
375 nfs4_get_setclientid_cred_server(struct nfs_server *server)
377 struct nfs_client *clp = server->nfs_client;
378 struct rpc_cred *cred = NULL;
379 struct nfs4_state_owner *sp;
382 spin_lock(&clp->cl_lock);
383 pos = rb_first(&server->state_owners);
385 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
386 cred = get_rpccred(sp->so_cred);
388 spin_unlock(&clp->cl_lock);
393 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
394 * @clp: client state handle
396 * Returns an rpc_cred with reference count bumped, or NULL.
398 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
400 struct nfs_server *server;
401 struct rpc_cred *cred;
403 spin_lock(&clp->cl_lock);
404 cred = nfs4_get_machine_cred_locked(clp);
405 spin_unlock(&clp->cl_lock);
410 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
411 cred = nfs4_get_setclientid_cred_server(server);
421 static struct nfs4_state_owner *
422 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
424 struct rb_node **p = &server->state_owners.rb_node,
426 struct nfs4_state_owner *sp;
430 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
432 if (cred < sp->so_cred)
433 p = &parent->rb_left;
434 else if (cred > sp->so_cred)
435 p = &parent->rb_right;
437 if (!list_empty(&sp->so_lru))
438 list_del_init(&sp->so_lru);
439 atomic_inc(&sp->so_count);
446 static struct nfs4_state_owner *
447 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
449 struct nfs_server *server = new->so_server;
450 struct rb_node **p = &server->state_owners.rb_node,
452 struct nfs4_state_owner *sp;
457 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
459 if (new->so_cred < sp->so_cred)
460 p = &parent->rb_left;
461 else if (new->so_cred > sp->so_cred)
462 p = &parent->rb_right;
464 if (!list_empty(&sp->so_lru))
465 list_del_init(&sp->so_lru);
466 atomic_inc(&sp->so_count);
470 err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
473 rb_link_node(&new->so_server_node, parent, p);
474 rb_insert_color(&new->so_server_node, &server->state_owners);
479 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
481 struct nfs_server *server = sp->so_server;
483 if (!RB_EMPTY_NODE(&sp->so_server_node))
484 rb_erase(&sp->so_server_node, &server->state_owners);
485 ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
489 nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
491 sc->create_time = ktime_get();
494 spin_lock_init(&sc->lock);
495 INIT_LIST_HEAD(&sc->list);
496 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
500 nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
502 rpc_destroy_wait_queue(&sc->wait);
506 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
507 * create a new state_owner.
510 static struct nfs4_state_owner *
511 nfs4_alloc_state_owner(struct nfs_server *server,
512 struct rpc_cred *cred,
515 struct nfs4_state_owner *sp;
517 sp = kzalloc(sizeof(*sp), gfp_flags);
520 sp->so_server = server;
521 sp->so_cred = get_rpccred(cred);
522 spin_lock_init(&sp->so_lock);
523 INIT_LIST_HEAD(&sp->so_states);
524 nfs4_init_seqid_counter(&sp->so_seqid);
525 atomic_set(&sp->so_count, 1);
526 INIT_LIST_HEAD(&sp->so_lru);
531 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
533 struct rb_node *rb_node = &sp->so_server_node;
535 if (!RB_EMPTY_NODE(rb_node)) {
536 struct nfs_server *server = sp->so_server;
537 struct nfs_client *clp = server->nfs_client;
539 spin_lock(&clp->cl_lock);
540 if (!RB_EMPTY_NODE(rb_node)) {
541 rb_erase(rb_node, &server->state_owners);
542 RB_CLEAR_NODE(rb_node);
544 spin_unlock(&clp->cl_lock);
548 static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
550 nfs4_destroy_seqid_counter(&sp->so_seqid);
551 put_rpccred(sp->so_cred);
555 static void nfs4_gc_state_owners(struct nfs_server *server)
557 struct nfs_client *clp = server->nfs_client;
558 struct nfs4_state_owner *sp, *tmp;
559 unsigned long time_min, time_max;
562 spin_lock(&clp->cl_lock);
564 time_min = (long)time_max - (long)clp->cl_lease_time;
565 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
566 /* NB: LRU is sorted so that oldest is at the head */
567 if (time_in_range(sp->so_expires, time_min, time_max))
569 list_move(&sp->so_lru, &doomed);
570 nfs4_remove_state_owner_locked(sp);
572 spin_unlock(&clp->cl_lock);
574 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
575 list_del(&sp->so_lru);
576 nfs4_free_state_owner(sp);
581 * nfs4_get_state_owner - Look up a state owner given a credential
582 * @server: nfs_server to search
583 * @cred: RPC credential to match
585 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
587 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
588 struct rpc_cred *cred,
591 struct nfs_client *clp = server->nfs_client;
592 struct nfs4_state_owner *sp, *new;
594 spin_lock(&clp->cl_lock);
595 sp = nfs4_find_state_owner_locked(server, cred);
596 spin_unlock(&clp->cl_lock);
599 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
603 if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
605 spin_lock(&clp->cl_lock);
606 sp = nfs4_insert_state_owner_locked(new);
607 spin_unlock(&clp->cl_lock);
608 } while (sp == ERR_PTR(-EAGAIN));
610 nfs4_free_state_owner(new);
612 nfs4_gc_state_owners(server);
617 * nfs4_put_state_owner - Release a nfs4_state_owner
618 * @sp: state owner data to release
620 * Note that we keep released state owners on an LRU
622 * This caches valid state owners so that they can be
623 * reused, to avoid the OPEN_CONFIRM on minor version 0.
624 * It also pins the uniquifier of dropped state owners for
625 * a while, to ensure that those state owner names are
628 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
630 struct nfs_server *server = sp->so_server;
631 struct nfs_client *clp = server->nfs_client;
633 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
636 sp->so_expires = jiffies;
637 list_add_tail(&sp->so_lru, &server->state_owners_lru);
638 spin_unlock(&clp->cl_lock);
642 * nfs4_purge_state_owners - Release all cached state owners
643 * @server: nfs_server with cached state owners to release
645 * Called at umount time. Remaining state owners will be on
646 * the LRU with ref count of zero.
648 void nfs4_purge_state_owners(struct nfs_server *server)
650 struct nfs_client *clp = server->nfs_client;
651 struct nfs4_state_owner *sp, *tmp;
654 spin_lock(&clp->cl_lock);
655 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
656 list_move(&sp->so_lru, &doomed);
657 nfs4_remove_state_owner_locked(sp);
659 spin_unlock(&clp->cl_lock);
661 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
662 list_del(&sp->so_lru);
663 nfs4_free_state_owner(sp);
667 static struct nfs4_state *
668 nfs4_alloc_open_state(void)
670 struct nfs4_state *state;
672 state = kzalloc(sizeof(*state), GFP_NOFS);
675 atomic_set(&state->count, 1);
676 INIT_LIST_HEAD(&state->lock_states);
677 spin_lock_init(&state->state_lock);
678 seqlock_init(&state->seqlock);
683 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
685 if (state->state == fmode)
687 /* NB! List reordering - see the reclaim code for why. */
688 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
689 if (fmode & FMODE_WRITE)
690 list_move(&state->open_states, &state->owner->so_states);
692 list_move_tail(&state->open_states, &state->owner->so_states);
694 state->state = fmode;
697 static struct nfs4_state *
698 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
700 struct nfs_inode *nfsi = NFS_I(inode);
701 struct nfs4_state *state;
703 list_for_each_entry(state, &nfsi->open_states, inode_states) {
704 if (state->owner != owner)
706 if (atomic_inc_not_zero(&state->count))
713 nfs4_free_open_state(struct nfs4_state *state)
719 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
721 struct nfs4_state *state, *new;
722 struct nfs_inode *nfsi = NFS_I(inode);
724 spin_lock(&inode->i_lock);
725 state = __nfs4_find_state_byowner(inode, owner);
726 spin_unlock(&inode->i_lock);
729 new = nfs4_alloc_open_state();
730 spin_lock(&owner->so_lock);
731 spin_lock(&inode->i_lock);
732 state = __nfs4_find_state_byowner(inode, owner);
733 if (state == NULL && new != NULL) {
735 state->owner = owner;
736 atomic_inc(&owner->so_count);
737 list_add(&state->inode_states, &nfsi->open_states);
739 state->inode = inode;
740 spin_unlock(&inode->i_lock);
741 /* Note: The reclaim code dictates that we add stateless
742 * and read-only stateids to the end of the list */
743 list_add_tail(&state->open_states, &owner->so_states);
744 spin_unlock(&owner->so_lock);
746 spin_unlock(&inode->i_lock);
747 spin_unlock(&owner->so_lock);
749 nfs4_free_open_state(new);
755 void nfs4_put_open_state(struct nfs4_state *state)
757 struct inode *inode = state->inode;
758 struct nfs4_state_owner *owner = state->owner;
760 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
762 spin_lock(&inode->i_lock);
763 list_del(&state->inode_states);
764 list_del(&state->open_states);
765 spin_unlock(&inode->i_lock);
766 spin_unlock(&owner->so_lock);
768 nfs4_free_open_state(state);
769 nfs4_put_state_owner(owner);
773 * Close the current file.
775 static void __nfs4_close(struct nfs4_state *state,
776 fmode_t fmode, gfp_t gfp_mask, int wait)
778 struct nfs4_state_owner *owner = state->owner;
782 atomic_inc(&owner->so_count);
783 /* Protect against nfs4_find_state() */
784 spin_lock(&owner->so_lock);
785 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
792 case FMODE_READ|FMODE_WRITE:
795 newstate = FMODE_READ|FMODE_WRITE;
796 if (state->n_rdwr == 0) {
797 if (state->n_rdonly == 0) {
798 newstate &= ~FMODE_READ;
799 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
800 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
802 if (state->n_wronly == 0) {
803 newstate &= ~FMODE_WRITE;
804 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
805 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
808 clear_bit(NFS_DELEGATED_STATE, &state->flags);
810 nfs4_state_set_mode_locked(state, newstate);
811 spin_unlock(&owner->so_lock);
814 nfs4_put_open_state(state);
815 nfs4_put_state_owner(owner);
817 nfs4_do_close(state, gfp_mask, wait);
820 void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
822 __nfs4_close(state, fmode, GFP_NOFS, 0);
825 void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
827 __nfs4_close(state, fmode, GFP_KERNEL, 1);
831 * Search the state->lock_states for an existing lock_owner
832 * that is compatible with current->files
834 static struct nfs4_lock_state *
835 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
837 struct nfs4_lock_state *pos;
838 list_for_each_entry(pos, &state->lock_states, ls_locks) {
839 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
841 switch (pos->ls_owner.lo_type) {
842 case NFS4_POSIX_LOCK_TYPE:
843 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
846 case NFS4_FLOCK_LOCK_TYPE:
847 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
850 atomic_inc(&pos->ls_count);
857 * Return a compatible lock_state. If no initialized lock_state structure
858 * exists, return an uninitialized one.
861 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)
863 struct nfs4_lock_state *lsp;
864 struct nfs_server *server = state->owner->so_server;
866 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
869 nfs4_init_seqid_counter(&lsp->ls_seqid);
870 atomic_set(&lsp->ls_count, 1);
871 lsp->ls_state = state;
872 lsp->ls_owner.lo_type = type;
873 switch (lsp->ls_owner.lo_type) {
874 case NFS4_FLOCK_LOCK_TYPE:
875 lsp->ls_owner.lo_u.flock_owner = fl_pid;
877 case NFS4_POSIX_LOCK_TYPE:
878 lsp->ls_owner.lo_u.posix_owner = fl_owner;
883 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
884 if (lsp->ls_seqid.owner_id < 0)
886 INIT_LIST_HEAD(&lsp->ls_locks);
893 void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
895 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
896 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
901 * Return a compatible lock_state. If no initialized lock_state structure
902 * exists, return an uninitialized one.
905 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
907 struct nfs4_lock_state *lsp, *new = NULL;
910 spin_lock(&state->state_lock);
911 lsp = __nfs4_find_lock_state(state, owner, pid, type);
915 list_add(&new->ls_locks, &state->lock_states);
916 set_bit(LK_STATE_IN_USE, &state->flags);
921 spin_unlock(&state->state_lock);
922 new = nfs4_alloc_lock_state(state, owner, pid, type);
926 spin_unlock(&state->state_lock);
928 nfs4_free_lock_state(state->owner->so_server, new);
933 * Release reference to lock_state, and free it if we see that
934 * it is no longer in use
936 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
938 struct nfs4_state *state;
942 state = lsp->ls_state;
943 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
945 list_del(&lsp->ls_locks);
946 if (list_empty(&state->lock_states))
947 clear_bit(LK_STATE_IN_USE, &state->flags);
948 spin_unlock(&state->state_lock);
949 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
950 if (nfs4_release_lockowner(lsp) == 0)
953 nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
956 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
958 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
960 dst->fl_u.nfs4_fl.owner = lsp;
961 atomic_inc(&lsp->ls_count);
964 static void nfs4_fl_release_lock(struct file_lock *fl)
966 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
969 static const struct file_lock_operations nfs4_fl_lock_ops = {
970 .fl_copy_lock = nfs4_fl_copy_lock,
971 .fl_release_private = nfs4_fl_release_lock,
974 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
976 struct nfs4_lock_state *lsp;
978 if (fl->fl_ops != NULL)
980 if (fl->fl_flags & FL_POSIX)
981 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
982 else if (fl->fl_flags & FL_FLOCK)
983 lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
984 NFS4_FLOCK_LOCK_TYPE);
989 fl->fl_u.nfs4_fl.owner = lsp;
990 fl->fl_ops = &nfs4_fl_lock_ops;
994 static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
995 const struct nfs_lockowner *lockowner)
997 struct nfs4_lock_state *lsp;
1003 if (lockowner == NULL)
1006 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
1009 fl_owner = lockowner->l_owner;
1010 fl_pid = lockowner->l_pid;
1011 spin_lock(&state->state_lock);
1012 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
1013 if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
1014 nfs4_stateid_copy(dst, &lsp->ls_stateid);
1017 spin_unlock(&state->state_lock);
1018 nfs4_put_lock_state(lsp);
1023 static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1028 seq = read_seqbegin(&state->seqlock);
1029 nfs4_stateid_copy(dst, &state->stateid);
1030 } while (read_seqretry(&state->seqlock, seq));
1034 * Byte-range lock aware utility to initialize the stateid of read/write
1037 void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
1038 fmode_t fmode, const struct nfs_lockowner *lockowner)
1040 if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
1042 if (nfs4_copy_lock_stateid(dst, state, lockowner))
1044 nfs4_copy_open_stateid(dst, state);
1047 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1049 struct nfs_seqid *new;
1051 new = kmalloc(sizeof(*new), gfp_mask);
1053 new->sequence = counter;
1054 INIT_LIST_HEAD(&new->list);
1060 void nfs_release_seqid(struct nfs_seqid *seqid)
1062 struct nfs_seqid_counter *sequence;
1064 if (list_empty(&seqid->list))
1066 sequence = seqid->sequence;
1067 spin_lock(&sequence->lock);
1068 list_del_init(&seqid->list);
1069 if (!list_empty(&sequence->list)) {
1070 struct nfs_seqid *next;
1072 next = list_first_entry(&sequence->list,
1073 struct nfs_seqid, list);
1074 rpc_wake_up_queued_task(&sequence->wait, next->task);
1076 spin_unlock(&sequence->lock);
1079 void nfs_free_seqid(struct nfs_seqid *seqid)
1081 nfs_release_seqid(seqid);
1086 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1087 * failed with a seqid incrementing error -
1088 * see comments nfs_fs.h:seqid_mutating_error()
1090 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1095 case -NFS4ERR_BAD_SEQID:
1096 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1098 pr_warn_ratelimited("NFS: v4 server returned a bad"
1099 " sequence-id error on an"
1100 " unconfirmed sequence %p!\n",
1102 case -NFS4ERR_STALE_CLIENTID:
1103 case -NFS4ERR_STALE_STATEID:
1104 case -NFS4ERR_BAD_STATEID:
1105 case -NFS4ERR_BADXDR:
1106 case -NFS4ERR_RESOURCE:
1107 case -NFS4ERR_NOFILEHANDLE:
1108 /* Non-seqid mutating errors */
1112 * Note: no locking needed as we are guaranteed to be first
1113 * on the sequence list
1115 seqid->sequence->counter++;
1118 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1120 struct nfs4_state_owner *sp = container_of(seqid->sequence,
1121 struct nfs4_state_owner, so_seqid);
1122 struct nfs_server *server = sp->so_server;
1124 if (status == -NFS4ERR_BAD_SEQID)
1125 nfs4_drop_state_owner(sp);
1126 if (!nfs4_has_session(server->nfs_client))
1127 nfs_increment_seqid(status, seqid);
1131 * Increment the seqid if the LOCK/LOCKU succeeded, or
1132 * failed with a seqid incrementing error -
1133 * see comments nfs_fs.h:seqid_mutating_error()
1135 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1137 nfs_increment_seqid(status, seqid);
1140 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1142 struct nfs_seqid_counter *sequence = seqid->sequence;
1145 spin_lock(&sequence->lock);
1147 if (list_empty(&seqid->list))
1148 list_add_tail(&seqid->list, &sequence->list);
1149 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1151 rpc_sleep_on(&sequence->wait, task, NULL);
1154 spin_unlock(&sequence->lock);
1158 static int nfs4_run_state_manager(void *);
1160 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1162 smp_mb__before_clear_bit();
1163 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1164 smp_mb__after_clear_bit();
1165 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
1166 rpc_wake_up(&clp->cl_rpcwaitq);
1170 * Schedule the nfs_client asynchronous state management routine
1172 void nfs4_schedule_state_manager(struct nfs_client *clp)
1174 struct task_struct *task;
1175 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1177 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1179 __module_get(THIS_MODULE);
1180 atomic_inc(&clp->cl_count);
1182 /* The rcu_read_lock() is not strictly necessary, as the state
1183 * manager is the only thread that ever changes the rpc_xprt
1184 * after it's initialized. At this point, we're single threaded. */
1186 snprintf(buf, sizeof(buf), "%s-manager",
1187 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1189 task = kthread_run(nfs4_run_state_manager, clp, buf);
1191 printk(KERN_ERR "%s: kthread_run: %ld\n",
1192 __func__, PTR_ERR(task));
1193 nfs4_clear_state_manager_bit(clp);
1194 nfs_put_client(clp);
1195 module_put(THIS_MODULE);
1200 * Schedule a lease recovery attempt
1202 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1206 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1207 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1208 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1210 nfs4_schedule_state_manager(clp);
1212 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1214 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1220 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1221 nfs_wait_bit_killable, TASK_KILLABLE);
1225 if (clp->cl_cons_state < 0)
1226 return clp->cl_cons_state;
1230 int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1235 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1236 ret = nfs4_wait_clnt_recover(clp);
1239 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1240 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1242 nfs4_schedule_state_manager(clp);
1249 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1250 * @clp: client to process
1252 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1253 * resend of the SETCLIENTID and hence re-establish the
1254 * callback channel. Then return all existing delegations.
1256 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1258 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1259 nfs_expire_all_delegations(clp);
1260 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1264 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1266 nfs40_handle_cb_pathdown(clp);
1267 nfs4_schedule_state_manager(clp);
1270 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1273 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1274 /* Don't recover state that expired before the reboot */
1275 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1276 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1279 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1280 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1284 static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1286 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1287 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1288 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1289 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1293 void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1295 struct nfs_client *clp = server->nfs_client;
1297 nfs4_state_mark_reclaim_nograce(clp, state);
1298 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1300 nfs4_schedule_state_manager(clp);
1302 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1304 void nfs_inode_find_state_and_recover(struct inode *inode,
1305 const nfs4_stateid *stateid)
1307 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1308 struct nfs_inode *nfsi = NFS_I(inode);
1309 struct nfs_open_context *ctx;
1310 struct nfs4_state *state;
1313 spin_lock(&inode->i_lock);
1314 list_for_each_entry(ctx, &nfsi->open_files, list) {
1318 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1320 if (!nfs4_stateid_match(&state->stateid, stateid))
1322 nfs4_state_mark_reclaim_nograce(clp, state);
1325 spin_unlock(&inode->i_lock);
1327 nfs4_schedule_state_manager(clp);
1331 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1333 struct inode *inode = state->inode;
1334 struct nfs_inode *nfsi = NFS_I(inode);
1335 struct file_lock *fl;
1338 if (inode->i_flock == NULL)
1341 /* Guard against delegation returns and new lock/unlock calls */
1342 down_write(&nfsi->rwsem);
1343 /* Protect inode->i_flock using the BKL */
1345 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1346 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1348 if (nfs_file_open_context(fl->fl_file)->state != state)
1351 status = ops->recover_lock(state, fl);
1356 case -NFS4ERR_ADMIN_REVOKED:
1357 case -NFS4ERR_STALE_STATEID:
1358 case -NFS4ERR_BAD_STATEID:
1359 case -NFS4ERR_EXPIRED:
1360 case -NFS4ERR_NO_GRACE:
1361 case -NFS4ERR_STALE_CLIENTID:
1362 case -NFS4ERR_BADSESSION:
1363 case -NFS4ERR_BADSLOT:
1364 case -NFS4ERR_BAD_HIGH_SLOT:
1365 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1368 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1369 "Zeroing state\n", __func__, status);
1371 case -NFS4ERR_DENIED:
1372 case -NFS4ERR_RECLAIM_BAD:
1373 case -NFS4ERR_RECLAIM_CONFLICT:
1374 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1381 up_write(&nfsi->rwsem);
1385 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1387 struct nfs4_state *state;
1388 struct nfs4_lock_state *lock;
1391 /* Note: we rely on the sp->so_states list being ordered
1392 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1394 * This is needed to ensure that the server won't give us any
1395 * read delegations that we have to return if, say, we are
1396 * recovering after a network partition or a reboot from a
1397 * server that doesn't support a grace period.
1400 spin_lock(&sp->so_lock);
1401 list_for_each_entry(state, &sp->so_states, open_states) {
1402 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1404 if (state->state == 0)
1406 atomic_inc(&state->count);
1407 spin_unlock(&sp->so_lock);
1408 status = ops->recover_open(sp, state);
1410 status = nfs4_reclaim_locks(state, ops);
1412 spin_lock(&state->state_lock);
1413 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1414 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1415 pr_warn_ratelimited("NFS: "
1417 "failed!\n", __func__);
1419 spin_unlock(&state->state_lock);
1420 nfs4_put_open_state(state);
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 memset(&state->stateid, 0,
1436 sizeof(state->stateid));
1437 /* Mark the file as being 'closed' */
1440 case -NFS4ERR_ADMIN_REVOKED:
1441 case -NFS4ERR_STALE_STATEID:
1442 case -NFS4ERR_BAD_STATEID:
1443 case -NFS4ERR_RECLAIM_BAD:
1444 case -NFS4ERR_RECLAIM_CONFLICT:
1445 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1447 case -NFS4ERR_EXPIRED:
1448 case -NFS4ERR_NO_GRACE:
1449 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1450 case -NFS4ERR_STALE_CLIENTID:
1451 case -NFS4ERR_BADSESSION:
1452 case -NFS4ERR_BADSLOT:
1453 case -NFS4ERR_BAD_HIGH_SLOT:
1454 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1457 nfs4_put_open_state(state);
1460 spin_unlock(&sp->so_lock);
1463 nfs4_put_open_state(state);
1467 static void nfs4_clear_open_state(struct nfs4_state *state)
1469 struct nfs4_lock_state *lock;
1471 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1472 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1473 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1474 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1475 spin_lock(&state->state_lock);
1476 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1477 lock->ls_seqid.flags = 0;
1478 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1480 spin_unlock(&state->state_lock);
1483 static void nfs4_reset_seqids(struct nfs_server *server,
1484 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1486 struct nfs_client *clp = server->nfs_client;
1487 struct nfs4_state_owner *sp;
1488 struct rb_node *pos;
1489 struct nfs4_state *state;
1491 spin_lock(&clp->cl_lock);
1492 for (pos = rb_first(&server->state_owners);
1494 pos = rb_next(pos)) {
1495 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1496 sp->so_seqid.flags = 0;
1497 spin_lock(&sp->so_lock);
1498 list_for_each_entry(state, &sp->so_states, open_states) {
1499 if (mark_reclaim(clp, state))
1500 nfs4_clear_open_state(state);
1502 spin_unlock(&sp->so_lock);
1504 spin_unlock(&clp->cl_lock);
1507 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1508 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1510 struct nfs_server *server;
1513 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1514 nfs4_reset_seqids(server, mark_reclaim);
1518 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1520 /* Mark all delegations for reclaim */
1521 nfs_delegation_mark_reclaim(clp);
1522 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1525 static void nfs4_reclaim_complete(struct nfs_client *clp,
1526 const struct nfs4_state_recovery_ops *ops)
1528 /* Notify the server we're done reclaiming our state */
1529 if (ops->reclaim_complete)
1530 (void)ops->reclaim_complete(clp);
1533 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1535 struct nfs_client *clp = server->nfs_client;
1536 struct nfs4_state_owner *sp;
1537 struct rb_node *pos;
1538 struct nfs4_state *state;
1540 spin_lock(&clp->cl_lock);
1541 for (pos = rb_first(&server->state_owners);
1543 pos = rb_next(pos)) {
1544 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1545 spin_lock(&sp->so_lock);
1546 list_for_each_entry(state, &sp->so_states, open_states) {
1547 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1550 nfs4_state_mark_reclaim_nograce(clp, state);
1552 spin_unlock(&sp->so_lock);
1554 spin_unlock(&clp->cl_lock);
1557 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1559 struct nfs_server *server;
1561 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1565 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1566 nfs4_clear_reclaim_server(server);
1569 nfs_delegation_reap_unclaimed(clp);
1573 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1575 if (!nfs4_state_clear_reclaim_reboot(clp))
1577 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
1580 static void nfs_delegation_clear_all(struct nfs_client *clp)
1582 nfs_delegation_mark_reclaim(clp);
1583 nfs_delegation_reap_unclaimed(clp);
1586 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1588 nfs_delegation_clear_all(clp);
1589 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1592 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1597 case -NFS4ERR_CB_PATH_DOWN:
1598 nfs40_handle_cb_pathdown(clp);
1600 case -NFS4ERR_NO_GRACE:
1601 nfs4_state_end_reclaim_reboot(clp);
1603 case -NFS4ERR_STALE_CLIENTID:
1604 case -NFS4ERR_LEASE_MOVED:
1605 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1606 nfs4_state_clear_reclaim_reboot(clp);
1607 nfs4_state_start_reclaim_reboot(clp);
1609 case -NFS4ERR_EXPIRED:
1610 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1611 nfs4_state_start_reclaim_nograce(clp);
1613 case -NFS4ERR_BADSESSION:
1614 case -NFS4ERR_BADSLOT:
1615 case -NFS4ERR_BAD_HIGH_SLOT:
1616 case -NFS4ERR_DEADSESSION:
1617 case -NFS4ERR_SEQ_FALSE_RETRY:
1618 case -NFS4ERR_SEQ_MISORDERED:
1619 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1620 /* Zero session reset errors */
1622 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1623 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1626 dprintk("%s: failed to handle error %d for server %s\n",
1627 __func__, error, clp->cl_hostname);
1630 dprintk("%s: handled error %d for server %s\n", __func__, error,
1635 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1637 struct nfs4_state_owner *sp;
1638 struct nfs_server *server;
1639 struct rb_node *pos;
1644 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1645 nfs4_purge_state_owners(server);
1646 spin_lock(&clp->cl_lock);
1647 for (pos = rb_first(&server->state_owners);
1649 pos = rb_next(pos)) {
1651 struct nfs4_state_owner, so_server_node);
1652 if (!test_and_clear_bit(ops->owner_flag_bit,
1655 atomic_inc(&sp->so_count);
1656 spin_unlock(&clp->cl_lock);
1659 status = nfs4_reclaim_open_state(sp, ops);
1661 set_bit(ops->owner_flag_bit, &sp->so_flags);
1662 nfs4_put_state_owner(sp);
1663 return nfs4_recovery_handle_error(clp, status);
1666 nfs4_put_state_owner(sp);
1669 spin_unlock(&clp->cl_lock);
1675 static int nfs4_check_lease(struct nfs_client *clp)
1677 struct rpc_cred *cred;
1678 const struct nfs4_state_maintenance_ops *ops =
1679 clp->cl_mvops->state_renewal_ops;
1682 /* Is the client already known to have an expired lease? */
1683 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1685 spin_lock(&clp->cl_lock);
1686 cred = ops->get_state_renewal_cred_locked(clp);
1687 spin_unlock(&clp->cl_lock);
1689 cred = nfs4_get_setclientid_cred(clp);
1694 status = ops->renew_lease(clp, cred);
1697 return nfs4_recovery_handle_error(clp, status);
1700 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1701 * and for recoverable errors on EXCHANGE_ID for v4.1
1703 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1706 case -NFS4ERR_SEQ_MISORDERED:
1707 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1708 return -ESERVERFAULT;
1709 /* Lease confirmation error: retry after purging the lease */
1711 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1713 case -NFS4ERR_STALE_CLIENTID:
1714 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1715 nfs4_state_clear_reclaim_reboot(clp);
1716 nfs4_state_start_reclaim_reboot(clp);
1718 case -NFS4ERR_CLID_INUSE:
1719 pr_err("NFS: Server %s reports our clientid is in use\n",
1721 nfs_mark_client_ready(clp, -EPERM);
1722 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1725 if (clp->cl_machine_cred == NULL)
1727 /* Handle case where the user hasn't set up machine creds */
1728 nfs4_clear_machine_cred(clp);
1729 case -NFS4ERR_DELAY:
1735 case -NFS4ERR_MINOR_VERS_MISMATCH:
1736 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1737 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1738 dprintk("%s: exit with error %d for server %s\n",
1739 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1740 return -EPROTONOSUPPORT;
1741 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1742 * in nfs4_exchange_id */
1744 dprintk("%s: exit with error %d for server %s\n", __func__,
1745 status, clp->cl_hostname);
1748 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1749 dprintk("%s: handled error %d for server %s\n", __func__, status,
1754 static int nfs4_establish_lease(struct nfs_client *clp)
1756 struct rpc_cred *cred;
1757 const struct nfs4_state_recovery_ops *ops =
1758 clp->cl_mvops->reboot_recovery_ops;
1761 cred = ops->get_clid_cred(clp);
1764 status = ops->establish_clid(clp, cred);
1768 pnfs_destroy_all_layouts(clp);
1773 * Returns zero or a negative errno. NFS4ERR values are converted
1774 * to local errno values.
1776 static int nfs4_reclaim_lease(struct nfs_client *clp)
1780 status = nfs4_establish_lease(clp);
1782 return nfs4_handle_reclaim_lease_error(clp, status);
1783 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1784 nfs4_state_start_reclaim_nograce(clp);
1785 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1786 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1787 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1788 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1792 static int nfs4_purge_lease(struct nfs_client *clp)
1796 status = nfs4_establish_lease(clp);
1798 return nfs4_handle_reclaim_lease_error(clp, status);
1799 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1800 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1801 nfs4_state_start_reclaim_nograce(clp);
1806 * nfs4_discover_server_trunking - Detect server IP address trunking
1808 * @clp: nfs_client under test
1809 * @result: OUT: found nfs_client, or clp
1811 * Returns zero or a negative errno. If zero is returned,
1812 * an nfs_client pointer is planted in "result".
1814 * Note: since we are invoked in process context, and
1815 * not from inside the state manager, we cannot use
1816 * nfs4_handle_reclaim_lease_error().
1818 int nfs4_discover_server_trunking(struct nfs_client *clp,
1819 struct nfs_client **result)
1821 const struct nfs4_state_recovery_ops *ops =
1822 clp->cl_mvops->reboot_recovery_ops;
1823 rpc_authflavor_t *flavors, flav, save;
1824 struct rpc_clnt *clnt;
1825 struct rpc_cred *cred;
1828 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
1830 len = NFS_MAX_SECFLAVORS;
1831 flavors = kcalloc(len, sizeof(*flavors), GFP_KERNEL);
1832 if (flavors == NULL) {
1836 len = rpcauth_list_flavors(flavors, len);
1841 clnt = clp->cl_rpcclient;
1842 save = clnt->cl_auth->au_flavor;
1845 mutex_lock(&nfs_clid_init_mutex);
1848 cred = ops->get_clid_cred(clp);
1852 status = ops->detect_trunking(clp, result, cred);
1859 if (clp->cl_machine_cred == NULL)
1861 /* Handle case where the user hasn't set up machine creds */
1862 nfs4_clear_machine_cred(clp);
1863 case -NFS4ERR_DELAY:
1867 dprintk("NFS: %s after status %d, retrying\n",
1871 case -NFS4ERR_CLID_INUSE:
1872 case -NFS4ERR_WRONGSEC:
1877 flav = flavors[i++];
1879 flav = flavors[i++];
1880 clnt = rpc_clone_client_set_auth(clnt, flav);
1882 status = PTR_ERR(clnt);
1885 clp->cl_rpcclient = clnt;
1888 case -NFS4ERR_MINOR_VERS_MISMATCH:
1889 status = -EPROTONOSUPPORT;
1893 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1894 * in nfs4_exchange_id */
1895 status = -EKEYEXPIRED;
1899 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_exchange_id_cred(clp);
2025 status = nfs4_proc_destroy_session(clp->cl_session, cred);
2026 if (status && status != -NFS4ERR_BADSESSION &&
2027 status != -NFS4ERR_DEADSESSION) {
2028 status = nfs4_recovery_handle_error(clp, status);
2032 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2033 status = nfs4_proc_create_session(clp, cred);
2035 dprintk("%s: session reset failed with status %d for server %s!\n",
2036 __func__, status, clp->cl_hostname);
2037 status = nfs4_handle_reclaim_lease_error(clp, status);
2040 nfs41_finish_session_reset(clp);
2041 dprintk("%s: session reset was successful for server %s!\n",
2042 __func__, clp->cl_hostname);
2049 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2051 struct rpc_cred *cred;
2054 if (!nfs4_has_session(clp))
2056 nfs4_begin_drain_session(clp);
2057 cred = nfs4_get_exchange_id_cred(clp);
2058 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2061 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2064 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2065 __func__, clp->cl_hostname);
2067 case -NFS4ERR_DELAY:
2069 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2072 return nfs4_recovery_handle_error(clp, ret);
2076 #else /* CONFIG_NFS_V4_1 */
2077 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2078 static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
2080 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2084 #endif /* CONFIG_NFS_V4_1 */
2086 static void nfs4_state_manager(struct nfs_client *clp)
2089 const char *section = "", *section_sep = "";
2091 /* Ensure exclusive access to NFSv4 state */
2093 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2094 section = "purge state";
2095 status = nfs4_purge_lease(clp);
2101 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2102 section = "lease expired";
2103 /* We're going to have to re-establish a clientid */
2104 status = nfs4_reclaim_lease(clp);
2110 /* Initialize or reset the session */
2111 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2112 section = "reset session";
2113 status = nfs4_reset_session(clp);
2114 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2120 /* Send BIND_CONN_TO_SESSION */
2121 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2123 section = "bind conn to session";
2124 status = nfs4_bind_conn_to_session(clp);
2130 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2131 section = "check lease";
2132 status = nfs4_check_lease(clp);
2138 /* First recover reboot state... */
2139 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2140 section = "reclaim reboot";
2141 status = nfs4_do_reclaim(clp,
2142 clp->cl_mvops->reboot_recovery_ops);
2143 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
2144 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
2146 nfs4_state_end_reclaim_reboot(clp);
2147 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2153 /* Now recover expired state... */
2154 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2155 section = "reclaim nograce";
2156 status = nfs4_do_reclaim(clp,
2157 clp->cl_mvops->nograce_recovery_ops);
2158 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
2159 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
2160 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
2166 nfs4_end_drain_session(clp);
2167 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2168 nfs_client_return_marked_delegations(clp);
2172 nfs4_clear_state_manager_bit(clp);
2173 /* Did we race with an attempt to give us more work? */
2174 if (clp->cl_state == 0)
2176 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2178 } while (atomic_read(&clp->cl_count) > 1);
2181 if (strlen(section))
2183 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2184 " with error %d\n", section_sep, section,
2185 clp->cl_hostname, -status);
2187 nfs4_end_drain_session(clp);
2188 nfs4_clear_state_manager_bit(clp);
2191 static int nfs4_run_state_manager(void *ptr)
2193 struct nfs_client *clp = ptr;
2195 allow_signal(SIGKILL);
2196 nfs4_state_manager(clp);
2197 nfs_put_client(clp);
2198 module_put_and_exit(0);