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>
55 #include "delegation.h"
59 #define OPENOWNER_POOL_SIZE 8
61 const nfs4_stateid zero_stateid
;
63 static LIST_HEAD(nfs4_clientid_list
);
65 int nfs4_init_clientid(struct nfs_client
*clp
, struct rpc_cred
*cred
)
67 struct nfs4_setclientid_res clid
;
71 port
= nfs_callback_tcpport
;
72 if (clp
->cl_addr
.ss_family
== AF_INET6
)
73 port
= nfs_callback_tcpport6
;
75 status
= nfs4_proc_setclientid(clp
, NFS4_CALLBACK
, port
, cred
, &clid
);
78 status
= nfs4_proc_setclientid_confirm(clp
, &clid
, cred
);
81 clp
->cl_clientid
= clid
.clientid
;
82 nfs4_schedule_state_renewal(clp
);
87 struct rpc_cred
*nfs4_get_machine_cred_locked(struct nfs_client
*clp
)
89 struct rpc_cred
*cred
= NULL
;
91 if (clp
->cl_machine_cred
!= NULL
)
92 cred
= get_rpccred(clp
->cl_machine_cred
);
96 static void nfs4_clear_machine_cred(struct nfs_client
*clp
)
98 struct rpc_cred
*cred
;
100 spin_lock(&clp
->cl_lock
);
101 cred
= clp
->cl_machine_cred
;
102 clp
->cl_machine_cred
= NULL
;
103 spin_unlock(&clp
->cl_lock
);
108 static struct rpc_cred
*
109 nfs4_get_renew_cred_server_locked(struct nfs_server
*server
)
111 struct rpc_cred
*cred
= NULL
;
112 struct nfs4_state_owner
*sp
;
115 for (pos
= rb_first(&server
->state_owners
);
117 pos
= rb_next(pos
)) {
118 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
119 if (list_empty(&sp
->so_states
))
121 cred
= get_rpccred(sp
->so_cred
);
128 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
129 * @clp: client state handle
131 * Returns an rpc_cred with reference count bumped, or NULL.
132 * Caller must hold clp->cl_lock.
134 struct rpc_cred
*nfs4_get_renew_cred_locked(struct nfs_client
*clp
)
136 struct rpc_cred
*cred
= NULL
;
137 struct nfs_server
*server
;
140 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
141 cred
= nfs4_get_renew_cred_server_locked(server
);
149 #if defined(CONFIG_NFS_V4_1)
151 static int nfs41_setup_state_renewal(struct nfs_client
*clp
)
154 struct nfs_fsinfo fsinfo
;
156 status
= nfs4_proc_get_lease_time(clp
, &fsinfo
);
158 /* Update lease time and schedule renewal */
159 spin_lock(&clp
->cl_lock
);
160 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
161 clp
->cl_last_renewal
= jiffies
;
162 spin_unlock(&clp
->cl_lock
);
164 nfs4_schedule_state_renewal(clp
);
171 * Back channel returns NFS4ERR_DELAY for new requests when
172 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
175 static void nfs4_end_drain_session(struct nfs_client
*clp
)
177 struct nfs4_session
*ses
= clp
->cl_session
;
182 if (test_and_clear_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
)) {
183 spin_lock(&ses
->fc_slot_table
.slot_tbl_lock
);
184 max_slots
= ses
->fc_slot_table
.max_slots
;
185 while (max_slots
--) {
186 struct rpc_task
*task
;
188 task
= rpc_wake_up_next(&ses
->fc_slot_table
.
192 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
194 spin_unlock(&ses
->fc_slot_table
.slot_tbl_lock
);
198 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table
*tbl
)
200 spin_lock(&tbl
->slot_tbl_lock
);
201 if (tbl
->highest_used_slotid
!= -1) {
202 INIT_COMPLETION(tbl
->complete
);
203 spin_unlock(&tbl
->slot_tbl_lock
);
204 return wait_for_completion_interruptible(&tbl
->complete
);
206 spin_unlock(&tbl
->slot_tbl_lock
);
210 static int nfs4_begin_drain_session(struct nfs_client
*clp
)
212 struct nfs4_session
*ses
= clp
->cl_session
;
215 set_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
);
217 ret
= nfs4_wait_on_slot_tbl(&ses
->bc_slot_table
);
221 return nfs4_wait_on_slot_tbl(&ses
->fc_slot_table
);
224 int nfs41_init_clientid(struct nfs_client
*clp
, struct rpc_cred
*cred
)
228 nfs4_begin_drain_session(clp
);
229 status
= nfs4_proc_exchange_id(clp
, cred
);
232 status
= nfs4_proc_create_session(clp
);
235 status
= nfs4_set_callback_sessionid(clp
);
237 printk(KERN_WARNING
"Sessionid not set. No callback service\n");
238 nfs_callback_down(1);
241 nfs41_setup_state_renewal(clp
);
242 nfs_mark_client_ready(clp
, NFS_CS_READY
);
247 struct rpc_cred
*nfs4_get_exchange_id_cred(struct nfs_client
*clp
)
249 struct rpc_cred
*cred
;
251 spin_lock(&clp
->cl_lock
);
252 cred
= nfs4_get_machine_cred_locked(clp
);
253 spin_unlock(&clp
->cl_lock
);
257 #endif /* CONFIG_NFS_V4_1 */
259 static struct rpc_cred
*
260 nfs4_get_setclientid_cred_server(struct nfs_server
*server
)
262 struct nfs_client
*clp
= server
->nfs_client
;
263 struct rpc_cred
*cred
= NULL
;
264 struct nfs4_state_owner
*sp
;
267 spin_lock(&clp
->cl_lock
);
268 pos
= rb_first(&server
->state_owners
);
270 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
271 cred
= get_rpccred(sp
->so_cred
);
273 spin_unlock(&clp
->cl_lock
);
278 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
279 * @clp: client state handle
281 * Returns an rpc_cred with reference count bumped, or NULL.
283 struct rpc_cred
*nfs4_get_setclientid_cred(struct nfs_client
*clp
)
285 struct nfs_server
*server
;
286 struct rpc_cred
*cred
;
288 spin_lock(&clp
->cl_lock
);
289 cred
= nfs4_get_machine_cred_locked(clp
);
290 spin_unlock(&clp
->cl_lock
);
295 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
296 cred
= nfs4_get_setclientid_cred_server(server
);
306 static void nfs_alloc_unique_id_locked(struct rb_root
*root
,
307 struct nfs_unique_id
*new,
308 __u64 minval
, int maxbits
)
310 struct rb_node
**p
, *parent
;
311 struct nfs_unique_id
*pos
;
315 mask
= (1ULL << maxbits
) - 1ULL;
317 /* Ensure distribution is more or less flat */
318 get_random_bytes(&new->id
, sizeof(new->id
));
320 if (new->id
< minval
)
328 pos
= rb_entry(parent
, struct nfs_unique_id
, rb_node
);
330 if (new->id
< pos
->id
)
332 else if (new->id
> pos
->id
)
337 rb_link_node(&new->rb_node
, parent
, p
);
338 rb_insert_color(&new->rb_node
, root
);
343 if (new->id
< minval
|| (new->id
& mask
) != new->id
) {
347 parent
= rb_next(parent
);
350 pos
= rb_entry(parent
, struct nfs_unique_id
, rb_node
);
351 if (new->id
< pos
->id
)
357 static void nfs_free_unique_id(struct rb_root
*root
, struct nfs_unique_id
*id
)
359 rb_erase(&id
->rb_node
, root
);
362 static struct nfs4_state_owner
*
363 nfs4_find_state_owner_locked(struct nfs_server
*server
, struct rpc_cred
*cred
)
365 struct rb_node
**p
= &server
->state_owners
.rb_node
,
367 struct nfs4_state_owner
*sp
, *res
= NULL
;
371 sp
= rb_entry(parent
, struct nfs4_state_owner
, so_server_node
);
373 if (server
< sp
->so_server
) {
374 p
= &parent
->rb_left
;
377 if (server
> sp
->so_server
) {
378 p
= &parent
->rb_right
;
381 if (cred
< sp
->so_cred
)
382 p
= &parent
->rb_left
;
383 else if (cred
> sp
->so_cred
)
384 p
= &parent
->rb_right
;
386 atomic_inc(&sp
->so_count
);
394 static struct nfs4_state_owner
*
395 nfs4_insert_state_owner_locked(struct nfs4_state_owner
*new)
397 struct nfs_server
*server
= new->so_server
;
398 struct rb_node
**p
= &server
->state_owners
.rb_node
,
400 struct nfs4_state_owner
*sp
;
404 sp
= rb_entry(parent
, struct nfs4_state_owner
, so_server_node
);
406 if (new->so_cred
< sp
->so_cred
)
407 p
= &parent
->rb_left
;
408 else if (new->so_cred
> sp
->so_cred
)
409 p
= &parent
->rb_right
;
411 atomic_inc(&sp
->so_count
);
415 nfs_alloc_unique_id_locked(&server
->openowner_id
,
416 &new->so_owner_id
, 1, 64);
417 rb_link_node(&new->so_server_node
, parent
, p
);
418 rb_insert_color(&new->so_server_node
, &server
->state_owners
);
423 nfs4_remove_state_owner_locked(struct nfs4_state_owner
*sp
)
425 struct nfs_server
*server
= sp
->so_server
;
427 if (!RB_EMPTY_NODE(&sp
->so_server_node
))
428 rb_erase(&sp
->so_server_node
, &server
->state_owners
);
429 nfs_free_unique_id(&server
->openowner_id
, &sp
->so_owner_id
);
433 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
434 * create a new state_owner.
437 static struct nfs4_state_owner
*
438 nfs4_alloc_state_owner(void)
440 struct nfs4_state_owner
*sp
;
442 sp
= kzalloc(sizeof(*sp
),GFP_NOFS
);
445 spin_lock_init(&sp
->so_lock
);
446 INIT_LIST_HEAD(&sp
->so_states
);
447 rpc_init_wait_queue(&sp
->so_sequence
.wait
, "Seqid_waitqueue");
448 sp
->so_seqid
.sequence
= &sp
->so_sequence
;
449 spin_lock_init(&sp
->so_sequence
.lock
);
450 INIT_LIST_HEAD(&sp
->so_sequence
.list
);
451 atomic_set(&sp
->so_count
, 1);
456 nfs4_drop_state_owner(struct nfs4_state_owner
*sp
)
458 if (!RB_EMPTY_NODE(&sp
->so_server_node
)) {
459 struct nfs_server
*server
= sp
->so_server
;
460 struct nfs_client
*clp
= server
->nfs_client
;
462 spin_lock(&clp
->cl_lock
);
463 rb_erase(&sp
->so_server_node
, &server
->state_owners
);
464 RB_CLEAR_NODE(&sp
->so_server_node
);
465 spin_unlock(&clp
->cl_lock
);
470 * nfs4_get_state_owner - Look up a state owner given a credential
471 * @server: nfs_server to search
472 * @cred: RPC credential to match
474 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
476 struct nfs4_state_owner
*nfs4_get_state_owner(struct nfs_server
*server
,
477 struct rpc_cred
*cred
)
479 struct nfs_client
*clp
= server
->nfs_client
;
480 struct nfs4_state_owner
*sp
, *new;
482 spin_lock(&clp
->cl_lock
);
483 sp
= nfs4_find_state_owner_locked(server
, cred
);
484 spin_unlock(&clp
->cl_lock
);
487 new = nfs4_alloc_state_owner();
490 new->so_server
= server
;
492 spin_lock(&clp
->cl_lock
);
493 sp
= nfs4_insert_state_owner_locked(new);
494 spin_unlock(&clp
->cl_lock
);
498 rpc_destroy_wait_queue(&new->so_sequence
.wait
);
505 * nfs4_put_state_owner - Release a nfs4_state_owner
506 * @sp: state owner data to release
509 void nfs4_put_state_owner(struct nfs4_state_owner
*sp
)
511 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
512 struct rpc_cred
*cred
= sp
->so_cred
;
514 if (!atomic_dec_and_lock(&sp
->so_count
, &clp
->cl_lock
))
516 nfs4_remove_state_owner_locked(sp
);
517 spin_unlock(&clp
->cl_lock
);
518 rpc_destroy_wait_queue(&sp
->so_sequence
.wait
);
523 static struct nfs4_state
*
524 nfs4_alloc_open_state(void)
526 struct nfs4_state
*state
;
528 state
= kzalloc(sizeof(*state
), GFP_NOFS
);
531 atomic_set(&state
->count
, 1);
532 INIT_LIST_HEAD(&state
->lock_states
);
533 spin_lock_init(&state
->state_lock
);
534 seqlock_init(&state
->seqlock
);
539 nfs4_state_set_mode_locked(struct nfs4_state
*state
, fmode_t fmode
)
541 if (state
->state
== fmode
)
543 /* NB! List reordering - see the reclaim code for why. */
544 if ((fmode
& FMODE_WRITE
) != (state
->state
& FMODE_WRITE
)) {
545 if (fmode
& FMODE_WRITE
)
546 list_move(&state
->open_states
, &state
->owner
->so_states
);
548 list_move_tail(&state
->open_states
, &state
->owner
->so_states
);
550 state
->state
= fmode
;
553 static struct nfs4_state
*
554 __nfs4_find_state_byowner(struct inode
*inode
, struct nfs4_state_owner
*owner
)
556 struct nfs_inode
*nfsi
= NFS_I(inode
);
557 struct nfs4_state
*state
;
559 list_for_each_entry(state
, &nfsi
->open_states
, inode_states
) {
560 if (state
->owner
!= owner
)
562 if (atomic_inc_not_zero(&state
->count
))
569 nfs4_free_open_state(struct nfs4_state
*state
)
575 nfs4_get_open_state(struct inode
*inode
, struct nfs4_state_owner
*owner
)
577 struct nfs4_state
*state
, *new;
578 struct nfs_inode
*nfsi
= NFS_I(inode
);
580 spin_lock(&inode
->i_lock
);
581 state
= __nfs4_find_state_byowner(inode
, owner
);
582 spin_unlock(&inode
->i_lock
);
585 new = nfs4_alloc_open_state();
586 spin_lock(&owner
->so_lock
);
587 spin_lock(&inode
->i_lock
);
588 state
= __nfs4_find_state_byowner(inode
, owner
);
589 if (state
== NULL
&& new != NULL
) {
591 state
->owner
= owner
;
592 atomic_inc(&owner
->so_count
);
593 list_add(&state
->inode_states
, &nfsi
->open_states
);
594 state
->inode
= igrab(inode
);
595 spin_unlock(&inode
->i_lock
);
596 /* Note: The reclaim code dictates that we add stateless
597 * and read-only stateids to the end of the list */
598 list_add_tail(&state
->open_states
, &owner
->so_states
);
599 spin_unlock(&owner
->so_lock
);
601 spin_unlock(&inode
->i_lock
);
602 spin_unlock(&owner
->so_lock
);
604 nfs4_free_open_state(new);
610 void nfs4_put_open_state(struct nfs4_state
*state
)
612 struct inode
*inode
= state
->inode
;
613 struct nfs4_state_owner
*owner
= state
->owner
;
615 if (!atomic_dec_and_lock(&state
->count
, &owner
->so_lock
))
617 spin_lock(&inode
->i_lock
);
618 list_del(&state
->inode_states
);
619 list_del(&state
->open_states
);
620 spin_unlock(&inode
->i_lock
);
621 spin_unlock(&owner
->so_lock
);
623 nfs4_free_open_state(state
);
624 nfs4_put_state_owner(owner
);
628 * Close the current file.
630 static void __nfs4_close(struct path
*path
, struct nfs4_state
*state
,
631 fmode_t fmode
, gfp_t gfp_mask
, int wait
)
633 struct nfs4_state_owner
*owner
= state
->owner
;
637 atomic_inc(&owner
->so_count
);
638 /* Protect against nfs4_find_state() */
639 spin_lock(&owner
->so_lock
);
640 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
647 case FMODE_READ
|FMODE_WRITE
:
650 newstate
= FMODE_READ
|FMODE_WRITE
;
651 if (state
->n_rdwr
== 0) {
652 if (state
->n_rdonly
== 0) {
653 newstate
&= ~FMODE_READ
;
654 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
655 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
657 if (state
->n_wronly
== 0) {
658 newstate
&= ~FMODE_WRITE
;
659 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
660 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
663 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
665 nfs4_state_set_mode_locked(state
, newstate
);
666 spin_unlock(&owner
->so_lock
);
669 nfs4_put_open_state(state
);
670 nfs4_put_state_owner(owner
);
672 bool roc
= pnfs_roc(state
->inode
);
674 nfs4_do_close(path
, state
, gfp_mask
, wait
, roc
);
678 void nfs4_close_state(struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
680 __nfs4_close(path
, state
, fmode
, GFP_NOFS
, 0);
683 void nfs4_close_sync(struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
685 __nfs4_close(path
, state
, fmode
, GFP_KERNEL
, 1);
689 * Search the state->lock_states for an existing lock_owner
690 * that is compatible with current->files
692 static struct nfs4_lock_state
*
693 __nfs4_find_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
, pid_t fl_pid
, unsigned int type
)
695 struct nfs4_lock_state
*pos
;
696 list_for_each_entry(pos
, &state
->lock_states
, ls_locks
) {
697 if (type
!= NFS4_ANY_LOCK_TYPE
&& pos
->ls_owner
.lo_type
!= type
)
699 switch (pos
->ls_owner
.lo_type
) {
700 case NFS4_POSIX_LOCK_TYPE
:
701 if (pos
->ls_owner
.lo_u
.posix_owner
!= fl_owner
)
704 case NFS4_FLOCK_LOCK_TYPE
:
705 if (pos
->ls_owner
.lo_u
.flock_owner
!= fl_pid
)
708 atomic_inc(&pos
->ls_count
);
715 * Return a compatible lock_state. If no initialized lock_state structure
716 * exists, return an uninitialized one.
719 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
)
721 struct nfs4_lock_state
*lsp
;
722 struct nfs_server
*server
= state
->owner
->so_server
;
723 struct nfs_client
*clp
= server
->nfs_client
;
725 lsp
= kzalloc(sizeof(*lsp
), GFP_NOFS
);
728 rpc_init_wait_queue(&lsp
->ls_sequence
.wait
, "lock_seqid_waitqueue");
729 spin_lock_init(&lsp
->ls_sequence
.lock
);
730 INIT_LIST_HEAD(&lsp
->ls_sequence
.list
);
731 lsp
->ls_seqid
.sequence
= &lsp
->ls_sequence
;
732 atomic_set(&lsp
->ls_count
, 1);
733 lsp
->ls_state
= state
;
734 lsp
->ls_owner
.lo_type
= type
;
735 switch (lsp
->ls_owner
.lo_type
) {
736 case NFS4_FLOCK_LOCK_TYPE
:
737 lsp
->ls_owner
.lo_u
.flock_owner
= fl_pid
;
739 case NFS4_POSIX_LOCK_TYPE
:
740 lsp
->ls_owner
.lo_u
.posix_owner
= fl_owner
;
746 spin_lock(&clp
->cl_lock
);
747 nfs_alloc_unique_id_locked(&server
->lockowner_id
, &lsp
->ls_id
, 1, 64);
748 spin_unlock(&clp
->cl_lock
);
749 INIT_LIST_HEAD(&lsp
->ls_locks
);
753 static void nfs4_free_lock_state(struct nfs4_lock_state
*lsp
)
755 struct nfs_server
*server
= lsp
->ls_state
->owner
->so_server
;
756 struct nfs_client
*clp
= server
->nfs_client
;
758 spin_lock(&clp
->cl_lock
);
759 nfs_free_unique_id(&server
->lockowner_id
, &lsp
->ls_id
);
760 spin_unlock(&clp
->cl_lock
);
761 rpc_destroy_wait_queue(&lsp
->ls_sequence
.wait
);
766 * Return a compatible lock_state. If no initialized lock_state structure
767 * exists, return an uninitialized one.
770 static struct nfs4_lock_state
*nfs4_get_lock_state(struct nfs4_state
*state
, fl_owner_t owner
, pid_t pid
, unsigned int type
)
772 struct nfs4_lock_state
*lsp
, *new = NULL
;
775 spin_lock(&state
->state_lock
);
776 lsp
= __nfs4_find_lock_state(state
, owner
, pid
, type
);
780 list_add(&new->ls_locks
, &state
->lock_states
);
781 set_bit(LK_STATE_IN_USE
, &state
->flags
);
786 spin_unlock(&state
->state_lock
);
787 new = nfs4_alloc_lock_state(state
, owner
, pid
, type
);
791 spin_unlock(&state
->state_lock
);
793 nfs4_free_lock_state(new);
798 * Release reference to lock_state, and free it if we see that
799 * it is no longer in use
801 void nfs4_put_lock_state(struct nfs4_lock_state
*lsp
)
803 struct nfs4_state
*state
;
807 state
= lsp
->ls_state
;
808 if (!atomic_dec_and_lock(&lsp
->ls_count
, &state
->state_lock
))
810 list_del(&lsp
->ls_locks
);
811 if (list_empty(&state
->lock_states
))
812 clear_bit(LK_STATE_IN_USE
, &state
->flags
);
813 spin_unlock(&state
->state_lock
);
814 if (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
)
815 nfs4_release_lockowner(lsp
);
816 nfs4_free_lock_state(lsp
);
819 static void nfs4_fl_copy_lock(struct file_lock
*dst
, struct file_lock
*src
)
821 struct nfs4_lock_state
*lsp
= src
->fl_u
.nfs4_fl
.owner
;
823 dst
->fl_u
.nfs4_fl
.owner
= lsp
;
824 atomic_inc(&lsp
->ls_count
);
827 static void nfs4_fl_release_lock(struct file_lock
*fl
)
829 nfs4_put_lock_state(fl
->fl_u
.nfs4_fl
.owner
);
832 static const struct file_lock_operations nfs4_fl_lock_ops
= {
833 .fl_copy_lock
= nfs4_fl_copy_lock
,
834 .fl_release_private
= nfs4_fl_release_lock
,
837 int nfs4_set_lock_state(struct nfs4_state
*state
, struct file_lock
*fl
)
839 struct nfs4_lock_state
*lsp
;
841 if (fl
->fl_ops
!= NULL
)
843 if (fl
->fl_flags
& FL_POSIX
)
844 lsp
= nfs4_get_lock_state(state
, fl
->fl_owner
, 0, NFS4_POSIX_LOCK_TYPE
);
845 else if (fl
->fl_flags
& FL_FLOCK
)
846 lsp
= nfs4_get_lock_state(state
, 0, fl
->fl_pid
, NFS4_FLOCK_LOCK_TYPE
);
851 fl
->fl_u
.nfs4_fl
.owner
= lsp
;
852 fl
->fl_ops
= &nfs4_fl_lock_ops
;
857 * Byte-range lock aware utility to initialize the stateid of read/write
860 void nfs4_copy_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
, fl_owner_t fl_owner
, pid_t fl_pid
)
862 struct nfs4_lock_state
*lsp
;
866 seq
= read_seqbegin(&state
->seqlock
);
867 memcpy(dst
, &state
->stateid
, sizeof(*dst
));
868 } while (read_seqretry(&state
->seqlock
, seq
));
869 if (test_bit(LK_STATE_IN_USE
, &state
->flags
) == 0)
872 spin_lock(&state
->state_lock
);
873 lsp
= __nfs4_find_lock_state(state
, fl_owner
, fl_pid
, NFS4_ANY_LOCK_TYPE
);
874 if (lsp
!= NULL
&& (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
875 memcpy(dst
, &lsp
->ls_stateid
, sizeof(*dst
));
876 spin_unlock(&state
->state_lock
);
877 nfs4_put_lock_state(lsp
);
880 struct nfs_seqid
*nfs_alloc_seqid(struct nfs_seqid_counter
*counter
, gfp_t gfp_mask
)
882 struct nfs_seqid
*new;
884 new = kmalloc(sizeof(*new), gfp_mask
);
886 new->sequence
= counter
;
887 INIT_LIST_HEAD(&new->list
);
892 void nfs_release_seqid(struct nfs_seqid
*seqid
)
894 if (!list_empty(&seqid
->list
)) {
895 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
897 spin_lock(&sequence
->lock
);
898 list_del_init(&seqid
->list
);
899 spin_unlock(&sequence
->lock
);
900 rpc_wake_up(&sequence
->wait
);
904 void nfs_free_seqid(struct nfs_seqid
*seqid
)
906 nfs_release_seqid(seqid
);
911 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
912 * failed with a seqid incrementing error -
913 * see comments nfs_fs.h:seqid_mutating_error()
915 static void nfs_increment_seqid(int status
, struct nfs_seqid
*seqid
)
917 BUG_ON(list_first_entry(&seqid
->sequence
->sequence
->list
, struct nfs_seqid
, list
) != seqid
);
921 case -NFS4ERR_BAD_SEQID
:
922 if (seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)
924 printk(KERN_WARNING
"NFS: v4 server returned a bad"
925 " sequence-id error on an"
926 " unconfirmed sequence %p!\n",
928 case -NFS4ERR_STALE_CLIENTID
:
929 case -NFS4ERR_STALE_STATEID
:
930 case -NFS4ERR_BAD_STATEID
:
931 case -NFS4ERR_BADXDR
:
932 case -NFS4ERR_RESOURCE
:
933 case -NFS4ERR_NOFILEHANDLE
:
934 /* Non-seqid mutating errors */
938 * Note: no locking needed as we are guaranteed to be first
939 * on the sequence list
941 seqid
->sequence
->counter
++;
944 void nfs_increment_open_seqid(int status
, struct nfs_seqid
*seqid
)
946 struct nfs4_state_owner
*sp
= container_of(seqid
->sequence
,
947 struct nfs4_state_owner
, so_seqid
);
948 struct nfs_server
*server
= sp
->so_server
;
950 if (status
== -NFS4ERR_BAD_SEQID
)
951 nfs4_drop_state_owner(sp
);
952 if (!nfs4_has_session(server
->nfs_client
))
953 nfs_increment_seqid(status
, seqid
);
957 * Increment the seqid if the LOCK/LOCKU succeeded, or
958 * failed with a seqid incrementing error -
959 * see comments nfs_fs.h:seqid_mutating_error()
961 void nfs_increment_lock_seqid(int status
, struct nfs_seqid
*seqid
)
963 nfs_increment_seqid(status
, seqid
);
966 int nfs_wait_on_sequence(struct nfs_seqid
*seqid
, struct rpc_task
*task
)
968 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
971 spin_lock(&sequence
->lock
);
972 if (list_empty(&seqid
->list
))
973 list_add_tail(&seqid
->list
, &sequence
->list
);
974 if (list_first_entry(&sequence
->list
, struct nfs_seqid
, list
) == seqid
)
976 rpc_sleep_on(&sequence
->wait
, task
, NULL
);
979 spin_unlock(&sequence
->lock
);
983 static int nfs4_run_state_manager(void *);
985 static void nfs4_clear_state_manager_bit(struct nfs_client
*clp
)
987 smp_mb__before_clear_bit();
988 clear_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
);
989 smp_mb__after_clear_bit();
990 wake_up_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
);
991 rpc_wake_up(&clp
->cl_rpcwaitq
);
995 * Schedule the nfs_client asynchronous state management routine
997 void nfs4_schedule_state_manager(struct nfs_client
*clp
)
999 struct task_struct
*task
;
1001 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) != 0)
1003 __module_get(THIS_MODULE
);
1004 atomic_inc(&clp
->cl_count
);
1005 task
= kthread_run(nfs4_run_state_manager
, clp
, "%s-manager",
1006 rpc_peeraddr2str(clp
->cl_rpcclient
,
1010 nfs4_clear_state_manager_bit(clp
);
1011 nfs_put_client(clp
);
1012 module_put(THIS_MODULE
);
1016 * Schedule a state recovery attempt
1018 void nfs4_schedule_state_recovery(struct nfs_client
*clp
)
1022 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1023 set_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1024 nfs4_schedule_state_manager(clp
);
1027 int nfs4_state_mark_reclaim_reboot(struct nfs_client
*clp
, struct nfs4_state
*state
)
1030 set_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1031 /* Don't recover state that expired before the reboot */
1032 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
)) {
1033 clear_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1036 set_bit(NFS_OWNER_RECLAIM_REBOOT
, &state
->owner
->so_flags
);
1037 set_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
);
1041 int nfs4_state_mark_reclaim_nograce(struct nfs_client
*clp
, struct nfs4_state
*state
)
1043 set_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
);
1044 clear_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1045 set_bit(NFS_OWNER_RECLAIM_NOGRACE
, &state
->owner
->so_flags
);
1046 set_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
);
1050 static int nfs4_reclaim_locks(struct nfs4_state
*state
, const struct nfs4_state_recovery_ops
*ops
)
1052 struct inode
*inode
= state
->inode
;
1053 struct nfs_inode
*nfsi
= NFS_I(inode
);
1054 struct file_lock
*fl
;
1057 if (inode
->i_flock
== NULL
)
1060 /* Guard against delegation returns and new lock/unlock calls */
1061 down_write(&nfsi
->rwsem
);
1062 /* Protect inode->i_flock using the BKL */
1064 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
1065 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
1067 if (nfs_file_open_context(fl
->fl_file
)->state
!= state
)
1070 status
= ops
->recover_lock(state
, fl
);
1075 case -NFS4ERR_ADMIN_REVOKED
:
1076 case -NFS4ERR_STALE_STATEID
:
1077 case -NFS4ERR_BAD_STATEID
:
1078 case -NFS4ERR_EXPIRED
:
1079 case -NFS4ERR_NO_GRACE
:
1080 case -NFS4ERR_STALE_CLIENTID
:
1081 case -NFS4ERR_BADSESSION
:
1082 case -NFS4ERR_BADSLOT
:
1083 case -NFS4ERR_BAD_HIGH_SLOT
:
1084 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1087 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
1090 case -NFS4ERR_DENIED
:
1091 case -NFS4ERR_RECLAIM_BAD
:
1092 case -NFS4ERR_RECLAIM_CONFLICT
:
1093 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1100 up_write(&nfsi
->rwsem
);
1104 static int nfs4_reclaim_open_state(struct nfs4_state_owner
*sp
, const struct nfs4_state_recovery_ops
*ops
)
1106 struct nfs4_state
*state
;
1107 struct nfs4_lock_state
*lock
;
1110 /* Note: we rely on the sp->so_states list being ordered
1111 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1113 * This is needed to ensure that the server won't give us any
1114 * read delegations that we have to return if, say, we are
1115 * recovering after a network partition or a reboot from a
1116 * server that doesn't support a grace period.
1119 spin_lock(&sp
->so_lock
);
1120 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1121 if (!test_and_clear_bit(ops
->state_flag_bit
, &state
->flags
))
1123 if (state
->state
== 0)
1125 atomic_inc(&state
->count
);
1126 spin_unlock(&sp
->so_lock
);
1127 status
= ops
->recover_open(sp
, state
);
1129 status
= nfs4_reclaim_locks(state
, ops
);
1131 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
1132 if (!(lock
->ls_flags
& NFS_LOCK_INITIALIZED
))
1133 printk("%s: Lock reclaim failed!\n",
1136 nfs4_put_open_state(state
);
1142 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
1148 * Open state on this file cannot be recovered
1149 * All we can do is revert to using the zero stateid.
1151 memset(state
->stateid
.data
, 0,
1152 sizeof(state
->stateid
.data
));
1153 /* Mark the file as being 'closed' */
1158 * User RPCSEC_GSS context has expired.
1159 * We cannot recover this stateid now, so
1160 * skip it and allow recovery thread to
1164 case -NFS4ERR_ADMIN_REVOKED
:
1165 case -NFS4ERR_STALE_STATEID
:
1166 case -NFS4ERR_BAD_STATEID
:
1167 case -NFS4ERR_RECLAIM_BAD
:
1168 case -NFS4ERR_RECLAIM_CONFLICT
:
1169 nfs4_state_mark_reclaim_nograce(sp
->so_server
->nfs_client
, state
);
1171 case -NFS4ERR_EXPIRED
:
1172 case -NFS4ERR_NO_GRACE
:
1173 nfs4_state_mark_reclaim_nograce(sp
->so_server
->nfs_client
, state
);
1174 case -NFS4ERR_STALE_CLIENTID
:
1175 case -NFS4ERR_BADSESSION
:
1176 case -NFS4ERR_BADSLOT
:
1177 case -NFS4ERR_BAD_HIGH_SLOT
:
1178 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1181 nfs4_put_open_state(state
);
1184 spin_unlock(&sp
->so_lock
);
1187 nfs4_put_open_state(state
);
1191 static void nfs4_clear_open_state(struct nfs4_state
*state
)
1193 struct nfs4_lock_state
*lock
;
1195 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1196 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1197 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1198 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1199 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
1200 lock
->ls_seqid
.flags
= 0;
1201 lock
->ls_flags
&= ~NFS_LOCK_INITIALIZED
;
1205 static void nfs4_reset_seqids(struct nfs_server
*server
,
1206 int (*mark_reclaim
)(struct nfs_client
*clp
, struct nfs4_state
*state
))
1208 struct nfs_client
*clp
= server
->nfs_client
;
1209 struct nfs4_state_owner
*sp
;
1210 struct rb_node
*pos
;
1211 struct nfs4_state
*state
;
1213 spin_lock(&clp
->cl_lock
);
1214 for (pos
= rb_first(&server
->state_owners
);
1216 pos
= rb_next(pos
)) {
1217 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
1218 sp
->so_seqid
.flags
= 0;
1219 spin_lock(&sp
->so_lock
);
1220 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1221 if (mark_reclaim(clp
, state
))
1222 nfs4_clear_open_state(state
);
1224 spin_unlock(&sp
->so_lock
);
1226 spin_unlock(&clp
->cl_lock
);
1229 static void nfs4_state_mark_reclaim_helper(struct nfs_client
*clp
,
1230 int (*mark_reclaim
)(struct nfs_client
*clp
, struct nfs4_state
*state
))
1232 struct nfs_server
*server
;
1235 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1236 nfs4_reset_seqids(server
, mark_reclaim
);
1240 static void nfs4_state_start_reclaim_reboot(struct nfs_client
*clp
)
1242 /* Mark all delegations for reclaim */
1243 nfs_delegation_mark_reclaim(clp
);
1244 nfs4_state_mark_reclaim_helper(clp
, nfs4_state_mark_reclaim_reboot
);
1247 static void nfs4_reclaim_complete(struct nfs_client
*clp
,
1248 const struct nfs4_state_recovery_ops
*ops
)
1250 /* Notify the server we're done reclaiming our state */
1251 if (ops
->reclaim_complete
)
1252 (void)ops
->reclaim_complete(clp
);
1255 static void nfs4_clear_reclaim_server(struct nfs_server
*server
)
1257 struct nfs_client
*clp
= server
->nfs_client
;
1258 struct nfs4_state_owner
*sp
;
1259 struct rb_node
*pos
;
1260 struct nfs4_state
*state
;
1262 spin_lock(&clp
->cl_lock
);
1263 for (pos
= rb_first(&server
->state_owners
);
1265 pos
= rb_next(pos
)) {
1266 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
1267 spin_lock(&sp
->so_lock
);
1268 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1269 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT
,
1272 nfs4_state_mark_reclaim_nograce(clp
, state
);
1274 spin_unlock(&sp
->so_lock
);
1276 spin_unlock(&clp
->cl_lock
);
1279 static int nfs4_state_clear_reclaim_reboot(struct nfs_client
*clp
)
1281 struct nfs_server
*server
;
1283 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
))
1287 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1288 nfs4_clear_reclaim_server(server
);
1291 nfs_delegation_reap_unclaimed(clp
);
1295 static void nfs4_state_end_reclaim_reboot(struct nfs_client
*clp
)
1297 if (!nfs4_state_clear_reclaim_reboot(clp
))
1299 nfs4_reclaim_complete(clp
, clp
->cl_mvops
->reboot_recovery_ops
);
1302 static void nfs_delegation_clear_all(struct nfs_client
*clp
)
1304 nfs_delegation_mark_reclaim(clp
);
1305 nfs_delegation_reap_unclaimed(clp
);
1308 static void nfs4_state_start_reclaim_nograce(struct nfs_client
*clp
)
1310 nfs_delegation_clear_all(clp
);
1311 nfs4_state_mark_reclaim_helper(clp
, nfs4_state_mark_reclaim_nograce
);
1314 static void nfs4_warn_keyexpired(const char *s
)
1316 printk_ratelimited(KERN_WARNING
"Error: state manager"
1317 " encountered RPCSEC_GSS session"
1318 " expired against NFSv4 server %s.\n",
1322 static int nfs4_recovery_handle_error(struct nfs_client
*clp
, int error
)
1325 case -NFS4ERR_CB_PATH_DOWN
:
1326 nfs_handle_cb_pathdown(clp
);
1328 case -NFS4ERR_NO_GRACE
:
1329 nfs4_state_end_reclaim_reboot(clp
);
1331 case -NFS4ERR_STALE_CLIENTID
:
1332 case -NFS4ERR_LEASE_MOVED
:
1333 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1334 nfs4_state_clear_reclaim_reboot(clp
);
1335 nfs4_state_start_reclaim_reboot(clp
);
1337 case -NFS4ERR_EXPIRED
:
1338 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1339 nfs4_state_start_reclaim_nograce(clp
);
1341 case -NFS4ERR_BADSESSION
:
1342 case -NFS4ERR_BADSLOT
:
1343 case -NFS4ERR_BAD_HIGH_SLOT
:
1344 case -NFS4ERR_DEADSESSION
:
1345 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1346 case -NFS4ERR_SEQ_FALSE_RETRY
:
1347 case -NFS4ERR_SEQ_MISORDERED
:
1348 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
1349 /* Zero session reset errors */
1352 /* Nothing we can do */
1353 nfs4_warn_keyexpired(clp
->cl_hostname
);
1359 static int nfs4_do_reclaim(struct nfs_client
*clp
, const struct nfs4_state_recovery_ops
*ops
)
1361 struct nfs4_state_owner
*sp
;
1362 struct nfs_server
*server
;
1363 struct rb_node
*pos
;
1368 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1369 spin_lock(&clp
->cl_lock
);
1370 for (pos
= rb_first(&server
->state_owners
);
1372 pos
= rb_next(pos
)) {
1374 struct nfs4_state_owner
, so_server_node
);
1375 if (!test_and_clear_bit(ops
->owner_flag_bit
,
1378 atomic_inc(&sp
->so_count
);
1379 spin_unlock(&clp
->cl_lock
);
1382 status
= nfs4_reclaim_open_state(sp
, ops
);
1384 set_bit(ops
->owner_flag_bit
, &sp
->so_flags
);
1385 nfs4_put_state_owner(sp
);
1386 return nfs4_recovery_handle_error(clp
, status
);
1389 nfs4_put_state_owner(sp
);
1392 spin_unlock(&clp
->cl_lock
);
1398 static int nfs4_check_lease(struct nfs_client
*clp
)
1400 struct rpc_cred
*cred
;
1401 const struct nfs4_state_maintenance_ops
*ops
=
1402 clp
->cl_mvops
->state_renewal_ops
;
1403 int status
= -NFS4ERR_EXPIRED
;
1405 /* Is the client already known to have an expired lease? */
1406 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1408 spin_lock(&clp
->cl_lock
);
1409 cred
= ops
->get_state_renewal_cred_locked(clp
);
1410 spin_unlock(&clp
->cl_lock
);
1412 cred
= nfs4_get_setclientid_cred(clp
);
1416 status
= ops
->renew_lease(clp
, cred
);
1419 return nfs4_recovery_handle_error(clp
, status
);
1422 static int nfs4_reclaim_lease(struct nfs_client
*clp
)
1424 struct rpc_cred
*cred
;
1425 const struct nfs4_state_recovery_ops
*ops
=
1426 clp
->cl_mvops
->reboot_recovery_ops
;
1427 int status
= -ENOENT
;
1429 cred
= ops
->get_clid_cred(clp
);
1431 status
= ops
->establish_clid(clp
, cred
);
1433 /* Handle case where the user hasn't set up machine creds */
1434 if (status
== -EACCES
&& cred
== clp
->cl_machine_cred
) {
1435 nfs4_clear_machine_cred(clp
);
1438 if (status
== -NFS4ERR_MINOR_VERS_MISMATCH
)
1439 status
= -EPROTONOSUPPORT
;
1444 #ifdef CONFIG_NFS_V4_1
1445 void nfs41_handle_recall_slot(struct nfs_client
*clp
)
1447 set_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
);
1448 nfs4_schedule_state_recovery(clp
);
1451 static void nfs4_reset_all_state(struct nfs_client
*clp
)
1453 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0) {
1454 clp
->cl_boot_time
= CURRENT_TIME
;
1455 nfs4_state_start_reclaim_nograce(clp
);
1456 nfs4_schedule_state_recovery(clp
);
1460 static void nfs41_handle_server_reboot(struct nfs_client
*clp
)
1462 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0) {
1463 nfs4_state_start_reclaim_reboot(clp
);
1464 nfs4_schedule_state_recovery(clp
);
1468 static void nfs41_handle_state_revoked(struct nfs_client
*clp
)
1471 nfs4_reset_all_state(clp
);
1474 static void nfs41_handle_recallable_state_revoked(struct nfs_client
*clp
)
1476 /* This will need to handle layouts too */
1477 nfs_expire_all_delegations(clp
);
1480 static void nfs41_handle_cb_path_down(struct nfs_client
*clp
)
1482 nfs_expire_all_delegations(clp
);
1483 if (test_and_set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
) == 0)
1484 nfs4_schedule_state_recovery(clp
);
1487 void nfs41_handle_sequence_flag_errors(struct nfs_client
*clp
, u32 flags
)
1491 else if (flags
& SEQ4_STATUS_RESTART_RECLAIM_NEEDED
)
1492 nfs41_handle_server_reboot(clp
);
1493 else if (flags
& (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED
|
1494 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED
|
1495 SEQ4_STATUS_ADMIN_STATE_REVOKED
|
1496 SEQ4_STATUS_LEASE_MOVED
))
1497 nfs41_handle_state_revoked(clp
);
1498 else if (flags
& SEQ4_STATUS_RECALLABLE_STATE_REVOKED
)
1499 nfs41_handle_recallable_state_revoked(clp
);
1500 else if (flags
& (SEQ4_STATUS_CB_PATH_DOWN
|
1501 SEQ4_STATUS_BACKCHANNEL_FAULT
|
1502 SEQ4_STATUS_CB_PATH_DOWN_SESSION
))
1503 nfs41_handle_cb_path_down(clp
);
1506 static int nfs4_reset_session(struct nfs_client
*clp
)
1510 nfs4_begin_drain_session(clp
);
1511 status
= nfs4_proc_destroy_session(clp
->cl_session
);
1512 if (status
&& status
!= -NFS4ERR_BADSESSION
&&
1513 status
!= -NFS4ERR_DEADSESSION
) {
1514 status
= nfs4_recovery_handle_error(clp
, status
);
1518 memset(clp
->cl_session
->sess_id
.data
, 0, NFS4_MAX_SESSIONID_LEN
);
1519 status
= nfs4_proc_create_session(clp
);
1521 status
= nfs4_recovery_handle_error(clp
, status
);
1524 /* create_session negotiated new slot table */
1525 clear_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
);
1527 /* Let the state manager reestablish state */
1528 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1529 nfs41_setup_state_renewal(clp
);
1534 static int nfs4_recall_slot(struct nfs_client
*clp
)
1536 struct nfs4_slot_table
*fc_tbl
= &clp
->cl_session
->fc_slot_table
;
1537 struct nfs4_channel_attrs
*fc_attrs
= &clp
->cl_session
->fc_attrs
;
1538 struct nfs4_slot
*new, *old
;
1541 nfs4_begin_drain_session(clp
);
1542 new = kmalloc(fc_tbl
->target_max_slots
* sizeof(struct nfs4_slot
),
1547 spin_lock(&fc_tbl
->slot_tbl_lock
);
1548 for (i
= 0; i
< fc_tbl
->target_max_slots
; i
++)
1549 new[i
].seq_nr
= fc_tbl
->slots
[i
].seq_nr
;
1550 old
= fc_tbl
->slots
;
1551 fc_tbl
->slots
= new;
1552 fc_tbl
->max_slots
= fc_tbl
->target_max_slots
;
1553 fc_tbl
->target_max_slots
= 0;
1554 fc_attrs
->max_reqs
= fc_tbl
->max_slots
;
1555 spin_unlock(&fc_tbl
->slot_tbl_lock
);
1558 nfs4_end_drain_session(clp
);
1562 #else /* CONFIG_NFS_V4_1 */
1563 static int nfs4_reset_session(struct nfs_client
*clp
) { return 0; }
1564 static int nfs4_end_drain_session(struct nfs_client
*clp
) { return 0; }
1565 static int nfs4_recall_slot(struct nfs_client
*clp
) { return 0; }
1566 #endif /* CONFIG_NFS_V4_1 */
1568 /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1569 * on EXCHANGE_ID for v4.1
1571 static void nfs4_set_lease_expired(struct nfs_client
*clp
, int status
)
1573 if (nfs4_has_session(clp
)) {
1575 case -NFS4ERR_DELAY
:
1576 case -NFS4ERR_CLID_INUSE
:
1581 nfs4_warn_keyexpired(clp
->cl_hostname
);
1582 case -NFS4ERR_NOT_SAME
: /* FixMe: implement recovery
1583 * in nfs4_exchange_id */
1588 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1591 static void nfs4_state_manager(struct nfs_client
*clp
)
1595 /* Ensure exclusive access to NFSv4 state */
1597 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
)) {
1598 /* We're going to have to re-establish a clientid */
1599 status
= nfs4_reclaim_lease(clp
);
1601 nfs4_set_lease_expired(clp
, status
);
1602 if (test_bit(NFS4CLNT_LEASE_EXPIRED
,
1605 if (clp
->cl_cons_state
==
1606 NFS_CS_SESSION_INITING
)
1607 nfs_mark_client_ready(clp
, status
);
1610 clear_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1611 set_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
);
1612 pnfs_destroy_all_layouts(clp
);
1615 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
)) {
1616 status
= nfs4_check_lease(clp
);
1617 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1619 if (status
< 0 && status
!= -NFS4ERR_CB_PATH_DOWN
)
1623 /* Initialize or reset the session */
1624 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
)
1625 && nfs4_has_session(clp
)) {
1626 status
= nfs4_reset_session(clp
);
1627 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1633 /* First recover reboot state... */
1634 if (test_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
)) {
1635 status
= nfs4_do_reclaim(clp
,
1636 clp
->cl_mvops
->reboot_recovery_ops
);
1637 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) ||
1638 test_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
))
1640 nfs4_state_end_reclaim_reboot(clp
);
1641 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
))
1647 /* Now recover expired state... */
1648 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
)) {
1649 status
= nfs4_do_reclaim(clp
,
1650 clp
->cl_mvops
->nograce_recovery_ops
);
1651 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) ||
1652 test_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
) ||
1653 test_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
))
1659 nfs4_end_drain_session(clp
);
1660 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
)) {
1661 nfs_client_return_marked_delegations(clp
);
1664 /* Recall session slots */
1665 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
)
1666 && nfs4_has_session(clp
)) {
1667 status
= nfs4_recall_slot(clp
);
1674 nfs4_clear_state_manager_bit(clp
);
1675 /* Did we race with an attempt to give us more work? */
1676 if (clp
->cl_state
== 0)
1678 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) != 0)
1683 printk(KERN_WARNING
"Error: state manager failed on NFSv4 server %s"
1684 " with error %d\n", clp
->cl_hostname
, -status
);
1685 nfs4_end_drain_session(clp
);
1686 nfs4_clear_state_manager_bit(clp
);
1689 static int nfs4_run_state_manager(void *ptr
)
1691 struct nfs_client
*clp
= ptr
;
1693 allow_signal(SIGKILL
);
1694 nfs4_state_manager(clp
);
1695 nfs_put_client(clp
);
1696 module_put_and_exit(0);