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 if (!test_bit(NFS_CS_CHECK_LEASE_TIME
, &clp
->cl_res_state
)) {
157 nfs4_schedule_state_renewal(clp
);
161 status
= nfs4_proc_get_lease_time(clp
, &fsinfo
);
163 /* Update lease time and schedule renewal */
164 spin_lock(&clp
->cl_lock
);
165 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
166 clp
->cl_last_renewal
= jiffies
;
167 spin_unlock(&clp
->cl_lock
);
169 nfs4_schedule_state_renewal(clp
);
176 * Back channel returns NFS4ERR_DELAY for new requests when
177 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
180 static void nfs4_end_drain_session(struct nfs_client
*clp
)
182 struct nfs4_session
*ses
= clp
->cl_session
;
187 if (test_and_clear_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
)) {
188 spin_lock(&ses
->fc_slot_table
.slot_tbl_lock
);
189 max_slots
= ses
->fc_slot_table
.max_slots
;
190 while (max_slots
--) {
191 struct rpc_task
*task
;
193 task
= rpc_wake_up_next(&ses
->fc_slot_table
.
197 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
199 spin_unlock(&ses
->fc_slot_table
.slot_tbl_lock
);
203 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table
*tbl
)
205 spin_lock(&tbl
->slot_tbl_lock
);
206 if (tbl
->highest_used_slotid
!= -1) {
207 INIT_COMPLETION(tbl
->complete
);
208 spin_unlock(&tbl
->slot_tbl_lock
);
209 return wait_for_completion_interruptible(&tbl
->complete
);
211 spin_unlock(&tbl
->slot_tbl_lock
);
215 static int nfs4_begin_drain_session(struct nfs_client
*clp
)
217 struct nfs4_session
*ses
= clp
->cl_session
;
220 set_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
);
222 ret
= nfs4_wait_on_slot_tbl(&ses
->bc_slot_table
);
226 return nfs4_wait_on_slot_tbl(&ses
->fc_slot_table
);
229 int nfs41_init_clientid(struct nfs_client
*clp
, struct rpc_cred
*cred
)
233 nfs4_begin_drain_session(clp
);
234 status
= nfs4_proc_exchange_id(clp
, cred
);
237 status
= nfs4_proc_create_session(clp
);
240 nfs41_setup_state_renewal(clp
);
241 nfs_mark_client_ready(clp
, NFS_CS_READY
);
246 struct rpc_cred
*nfs4_get_exchange_id_cred(struct nfs_client
*clp
)
248 struct rpc_cred
*cred
;
250 spin_lock(&clp
->cl_lock
);
251 cred
= nfs4_get_machine_cred_locked(clp
);
252 spin_unlock(&clp
->cl_lock
);
256 #endif /* CONFIG_NFS_V4_1 */
258 static struct rpc_cred
*
259 nfs4_get_setclientid_cred_server(struct nfs_server
*server
)
261 struct nfs_client
*clp
= server
->nfs_client
;
262 struct rpc_cred
*cred
= NULL
;
263 struct nfs4_state_owner
*sp
;
266 spin_lock(&clp
->cl_lock
);
267 pos
= rb_first(&server
->state_owners
);
269 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
270 cred
= get_rpccred(sp
->so_cred
);
272 spin_unlock(&clp
->cl_lock
);
277 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
278 * @clp: client state handle
280 * Returns an rpc_cred with reference count bumped, or NULL.
282 struct rpc_cred
*nfs4_get_setclientid_cred(struct nfs_client
*clp
)
284 struct nfs_server
*server
;
285 struct rpc_cred
*cred
;
287 spin_lock(&clp
->cl_lock
);
288 cred
= nfs4_get_machine_cred_locked(clp
);
289 spin_unlock(&clp
->cl_lock
);
294 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
295 cred
= nfs4_get_setclientid_cred_server(server
);
305 static void nfs_alloc_unique_id_locked(struct rb_root
*root
,
306 struct nfs_unique_id
*new,
307 __u64 minval
, int maxbits
)
309 struct rb_node
**p
, *parent
;
310 struct nfs_unique_id
*pos
;
314 mask
= (1ULL << maxbits
) - 1ULL;
316 /* Ensure distribution is more or less flat */
317 get_random_bytes(&new->id
, sizeof(new->id
));
319 if (new->id
< minval
)
327 pos
= rb_entry(parent
, struct nfs_unique_id
, rb_node
);
329 if (new->id
< pos
->id
)
331 else if (new->id
> pos
->id
)
336 rb_link_node(&new->rb_node
, parent
, p
);
337 rb_insert_color(&new->rb_node
, root
);
342 if (new->id
< minval
|| (new->id
& mask
) != new->id
) {
346 parent
= rb_next(parent
);
349 pos
= rb_entry(parent
, struct nfs_unique_id
, rb_node
);
350 if (new->id
< pos
->id
)
356 static void nfs_free_unique_id(struct rb_root
*root
, struct nfs_unique_id
*id
)
358 rb_erase(&id
->rb_node
, root
);
361 static struct nfs4_state_owner
*
362 nfs4_find_state_owner_locked(struct nfs_server
*server
, struct rpc_cred
*cred
)
364 struct rb_node
**p
= &server
->state_owners
.rb_node
,
366 struct nfs4_state_owner
*sp
, *res
= NULL
;
370 sp
= rb_entry(parent
, struct nfs4_state_owner
, so_server_node
);
372 if (server
< sp
->so_server
) {
373 p
= &parent
->rb_left
;
376 if (server
> sp
->so_server
) {
377 p
= &parent
->rb_right
;
380 if (cred
< sp
->so_cred
)
381 p
= &parent
->rb_left
;
382 else if (cred
> sp
->so_cred
)
383 p
= &parent
->rb_right
;
385 atomic_inc(&sp
->so_count
);
393 static struct nfs4_state_owner
*
394 nfs4_insert_state_owner_locked(struct nfs4_state_owner
*new)
396 struct nfs_server
*server
= new->so_server
;
397 struct rb_node
**p
= &server
->state_owners
.rb_node
,
399 struct nfs4_state_owner
*sp
;
403 sp
= rb_entry(parent
, struct nfs4_state_owner
, so_server_node
);
405 if (new->so_cred
< sp
->so_cred
)
406 p
= &parent
->rb_left
;
407 else if (new->so_cred
> sp
->so_cred
)
408 p
= &parent
->rb_right
;
410 atomic_inc(&sp
->so_count
);
414 nfs_alloc_unique_id_locked(&server
->openowner_id
,
415 &new->so_owner_id
, 1, 64);
416 rb_link_node(&new->so_server_node
, parent
, p
);
417 rb_insert_color(&new->so_server_node
, &server
->state_owners
);
422 nfs4_remove_state_owner_locked(struct nfs4_state_owner
*sp
)
424 struct nfs_server
*server
= sp
->so_server
;
426 if (!RB_EMPTY_NODE(&sp
->so_server_node
))
427 rb_erase(&sp
->so_server_node
, &server
->state_owners
);
428 nfs_free_unique_id(&server
->openowner_id
, &sp
->so_owner_id
);
432 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
433 * create a new state_owner.
436 static struct nfs4_state_owner
*
437 nfs4_alloc_state_owner(void)
439 struct nfs4_state_owner
*sp
;
441 sp
= kzalloc(sizeof(*sp
),GFP_NOFS
);
444 spin_lock_init(&sp
->so_lock
);
445 INIT_LIST_HEAD(&sp
->so_states
);
446 rpc_init_wait_queue(&sp
->so_sequence
.wait
, "Seqid_waitqueue");
447 sp
->so_seqid
.sequence
= &sp
->so_sequence
;
448 spin_lock_init(&sp
->so_sequence
.lock
);
449 INIT_LIST_HEAD(&sp
->so_sequence
.list
);
450 atomic_set(&sp
->so_count
, 1);
455 nfs4_drop_state_owner(struct nfs4_state_owner
*sp
)
457 if (!RB_EMPTY_NODE(&sp
->so_server_node
)) {
458 struct nfs_server
*server
= sp
->so_server
;
459 struct nfs_client
*clp
= server
->nfs_client
;
461 spin_lock(&clp
->cl_lock
);
462 rb_erase(&sp
->so_server_node
, &server
->state_owners
);
463 RB_CLEAR_NODE(&sp
->so_server_node
);
464 spin_unlock(&clp
->cl_lock
);
469 * nfs4_get_state_owner - Look up a state owner given a credential
470 * @server: nfs_server to search
471 * @cred: RPC credential to match
473 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
475 struct nfs4_state_owner
*nfs4_get_state_owner(struct nfs_server
*server
,
476 struct rpc_cred
*cred
)
478 struct nfs_client
*clp
= server
->nfs_client
;
479 struct nfs4_state_owner
*sp
, *new;
481 spin_lock(&clp
->cl_lock
);
482 sp
= nfs4_find_state_owner_locked(server
, cred
);
483 spin_unlock(&clp
->cl_lock
);
486 new = nfs4_alloc_state_owner();
489 new->so_server
= server
;
491 spin_lock(&clp
->cl_lock
);
492 sp
= nfs4_insert_state_owner_locked(new);
493 spin_unlock(&clp
->cl_lock
);
497 rpc_destroy_wait_queue(&new->so_sequence
.wait
);
504 * nfs4_put_state_owner - Release a nfs4_state_owner
505 * @sp: state owner data to release
508 void nfs4_put_state_owner(struct nfs4_state_owner
*sp
)
510 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
511 struct rpc_cred
*cred
= sp
->so_cred
;
513 if (!atomic_dec_and_lock(&sp
->so_count
, &clp
->cl_lock
))
515 nfs4_remove_state_owner_locked(sp
);
516 spin_unlock(&clp
->cl_lock
);
517 rpc_destroy_wait_queue(&sp
->so_sequence
.wait
);
522 static struct nfs4_state
*
523 nfs4_alloc_open_state(void)
525 struct nfs4_state
*state
;
527 state
= kzalloc(sizeof(*state
), GFP_NOFS
);
530 atomic_set(&state
->count
, 1);
531 INIT_LIST_HEAD(&state
->lock_states
);
532 spin_lock_init(&state
->state_lock
);
533 seqlock_init(&state
->seqlock
);
538 nfs4_state_set_mode_locked(struct nfs4_state
*state
, fmode_t fmode
)
540 if (state
->state
== fmode
)
542 /* NB! List reordering - see the reclaim code for why. */
543 if ((fmode
& FMODE_WRITE
) != (state
->state
& FMODE_WRITE
)) {
544 if (fmode
& FMODE_WRITE
)
545 list_move(&state
->open_states
, &state
->owner
->so_states
);
547 list_move_tail(&state
->open_states
, &state
->owner
->so_states
);
549 state
->state
= fmode
;
552 static struct nfs4_state
*
553 __nfs4_find_state_byowner(struct inode
*inode
, struct nfs4_state_owner
*owner
)
555 struct nfs_inode
*nfsi
= NFS_I(inode
);
556 struct nfs4_state
*state
;
558 list_for_each_entry(state
, &nfsi
->open_states
, inode_states
) {
559 if (state
->owner
!= owner
)
561 if (atomic_inc_not_zero(&state
->count
))
568 nfs4_free_open_state(struct nfs4_state
*state
)
574 nfs4_get_open_state(struct inode
*inode
, struct nfs4_state_owner
*owner
)
576 struct nfs4_state
*state
, *new;
577 struct nfs_inode
*nfsi
= NFS_I(inode
);
579 spin_lock(&inode
->i_lock
);
580 state
= __nfs4_find_state_byowner(inode
, owner
);
581 spin_unlock(&inode
->i_lock
);
584 new = nfs4_alloc_open_state();
585 spin_lock(&owner
->so_lock
);
586 spin_lock(&inode
->i_lock
);
587 state
= __nfs4_find_state_byowner(inode
, owner
);
588 if (state
== NULL
&& new != NULL
) {
590 state
->owner
= owner
;
591 atomic_inc(&owner
->so_count
);
592 list_add(&state
->inode_states
, &nfsi
->open_states
);
593 state
->inode
= igrab(inode
);
594 spin_unlock(&inode
->i_lock
);
595 /* Note: The reclaim code dictates that we add stateless
596 * and read-only stateids to the end of the list */
597 list_add_tail(&state
->open_states
, &owner
->so_states
);
598 spin_unlock(&owner
->so_lock
);
600 spin_unlock(&inode
->i_lock
);
601 spin_unlock(&owner
->so_lock
);
603 nfs4_free_open_state(new);
609 void nfs4_put_open_state(struct nfs4_state
*state
)
611 struct inode
*inode
= state
->inode
;
612 struct nfs4_state_owner
*owner
= state
->owner
;
614 if (!atomic_dec_and_lock(&state
->count
, &owner
->so_lock
))
616 spin_lock(&inode
->i_lock
);
617 list_del(&state
->inode_states
);
618 list_del(&state
->open_states
);
619 spin_unlock(&inode
->i_lock
);
620 spin_unlock(&owner
->so_lock
);
622 nfs4_free_open_state(state
);
623 nfs4_put_state_owner(owner
);
627 * Close the current file.
629 static void __nfs4_close(struct path
*path
, struct nfs4_state
*state
,
630 fmode_t fmode
, gfp_t gfp_mask
, int wait
)
632 struct nfs4_state_owner
*owner
= state
->owner
;
636 atomic_inc(&owner
->so_count
);
637 /* Protect against nfs4_find_state() */
638 spin_lock(&owner
->so_lock
);
639 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
646 case FMODE_READ
|FMODE_WRITE
:
649 newstate
= FMODE_READ
|FMODE_WRITE
;
650 if (state
->n_rdwr
== 0) {
651 if (state
->n_rdonly
== 0) {
652 newstate
&= ~FMODE_READ
;
653 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
654 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
656 if (state
->n_wronly
== 0) {
657 newstate
&= ~FMODE_WRITE
;
658 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
659 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
662 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
664 nfs4_state_set_mode_locked(state
, newstate
);
665 spin_unlock(&owner
->so_lock
);
668 nfs4_put_open_state(state
);
669 nfs4_put_state_owner(owner
);
671 bool roc
= pnfs_roc(state
->inode
);
673 nfs4_do_close(path
, state
, gfp_mask
, wait
, roc
);
677 void nfs4_close_state(struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
679 __nfs4_close(path
, state
, fmode
, GFP_NOFS
, 0);
682 void nfs4_close_sync(struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
684 __nfs4_close(path
, state
, fmode
, GFP_KERNEL
, 1);
688 * Search the state->lock_states for an existing lock_owner
689 * that is compatible with current->files
691 static struct nfs4_lock_state
*
692 __nfs4_find_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
, pid_t fl_pid
, unsigned int type
)
694 struct nfs4_lock_state
*pos
;
695 list_for_each_entry(pos
, &state
->lock_states
, ls_locks
) {
696 if (type
!= NFS4_ANY_LOCK_TYPE
&& pos
->ls_owner
.lo_type
!= type
)
698 switch (pos
->ls_owner
.lo_type
) {
699 case NFS4_POSIX_LOCK_TYPE
:
700 if (pos
->ls_owner
.lo_u
.posix_owner
!= fl_owner
)
703 case NFS4_FLOCK_LOCK_TYPE
:
704 if (pos
->ls_owner
.lo_u
.flock_owner
!= fl_pid
)
707 atomic_inc(&pos
->ls_count
);
714 * Return a compatible lock_state. If no initialized lock_state structure
715 * exists, return an uninitialized one.
718 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
)
720 struct nfs4_lock_state
*lsp
;
721 struct nfs_server
*server
= state
->owner
->so_server
;
722 struct nfs_client
*clp
= server
->nfs_client
;
724 lsp
= kzalloc(sizeof(*lsp
), GFP_NOFS
);
727 rpc_init_wait_queue(&lsp
->ls_sequence
.wait
, "lock_seqid_waitqueue");
728 spin_lock_init(&lsp
->ls_sequence
.lock
);
729 INIT_LIST_HEAD(&lsp
->ls_sequence
.list
);
730 lsp
->ls_seqid
.sequence
= &lsp
->ls_sequence
;
731 atomic_set(&lsp
->ls_count
, 1);
732 lsp
->ls_state
= state
;
733 lsp
->ls_owner
.lo_type
= type
;
734 switch (lsp
->ls_owner
.lo_type
) {
735 case NFS4_FLOCK_LOCK_TYPE
:
736 lsp
->ls_owner
.lo_u
.flock_owner
= fl_pid
;
738 case NFS4_POSIX_LOCK_TYPE
:
739 lsp
->ls_owner
.lo_u
.posix_owner
= fl_owner
;
745 spin_lock(&clp
->cl_lock
);
746 nfs_alloc_unique_id_locked(&server
->lockowner_id
, &lsp
->ls_id
, 1, 64);
747 spin_unlock(&clp
->cl_lock
);
748 INIT_LIST_HEAD(&lsp
->ls_locks
);
752 static void nfs4_free_lock_state(struct nfs4_lock_state
*lsp
)
754 struct nfs_server
*server
= lsp
->ls_state
->owner
->so_server
;
755 struct nfs_client
*clp
= server
->nfs_client
;
757 spin_lock(&clp
->cl_lock
);
758 nfs_free_unique_id(&server
->lockowner_id
, &lsp
->ls_id
);
759 spin_unlock(&clp
->cl_lock
);
760 rpc_destroy_wait_queue(&lsp
->ls_sequence
.wait
);
765 * Return a compatible lock_state. If no initialized lock_state structure
766 * exists, return an uninitialized one.
769 static struct nfs4_lock_state
*nfs4_get_lock_state(struct nfs4_state
*state
, fl_owner_t owner
, pid_t pid
, unsigned int type
)
771 struct nfs4_lock_state
*lsp
, *new = NULL
;
774 spin_lock(&state
->state_lock
);
775 lsp
= __nfs4_find_lock_state(state
, owner
, pid
, type
);
779 list_add(&new->ls_locks
, &state
->lock_states
);
780 set_bit(LK_STATE_IN_USE
, &state
->flags
);
785 spin_unlock(&state
->state_lock
);
786 new = nfs4_alloc_lock_state(state
, owner
, pid
, type
);
790 spin_unlock(&state
->state_lock
);
792 nfs4_free_lock_state(new);
797 * Release reference to lock_state, and free it if we see that
798 * it is no longer in use
800 void nfs4_put_lock_state(struct nfs4_lock_state
*lsp
)
802 struct nfs4_state
*state
;
806 state
= lsp
->ls_state
;
807 if (!atomic_dec_and_lock(&lsp
->ls_count
, &state
->state_lock
))
809 list_del(&lsp
->ls_locks
);
810 if (list_empty(&state
->lock_states
))
811 clear_bit(LK_STATE_IN_USE
, &state
->flags
);
812 spin_unlock(&state
->state_lock
);
813 if (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
)
814 nfs4_release_lockowner(lsp
);
815 nfs4_free_lock_state(lsp
);
818 static void nfs4_fl_copy_lock(struct file_lock
*dst
, struct file_lock
*src
)
820 struct nfs4_lock_state
*lsp
= src
->fl_u
.nfs4_fl
.owner
;
822 dst
->fl_u
.nfs4_fl
.owner
= lsp
;
823 atomic_inc(&lsp
->ls_count
);
826 static void nfs4_fl_release_lock(struct file_lock
*fl
)
828 nfs4_put_lock_state(fl
->fl_u
.nfs4_fl
.owner
);
831 static const struct file_lock_operations nfs4_fl_lock_ops
= {
832 .fl_copy_lock
= nfs4_fl_copy_lock
,
833 .fl_release_private
= nfs4_fl_release_lock
,
836 int nfs4_set_lock_state(struct nfs4_state
*state
, struct file_lock
*fl
)
838 struct nfs4_lock_state
*lsp
;
840 if (fl
->fl_ops
!= NULL
)
842 if (fl
->fl_flags
& FL_POSIX
)
843 lsp
= nfs4_get_lock_state(state
, fl
->fl_owner
, 0, NFS4_POSIX_LOCK_TYPE
);
844 else if (fl
->fl_flags
& FL_FLOCK
)
845 lsp
= nfs4_get_lock_state(state
, 0, fl
->fl_pid
, NFS4_FLOCK_LOCK_TYPE
);
850 fl
->fl_u
.nfs4_fl
.owner
= lsp
;
851 fl
->fl_ops
= &nfs4_fl_lock_ops
;
856 * Byte-range lock aware utility to initialize the stateid of read/write
859 void nfs4_copy_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
, fl_owner_t fl_owner
, pid_t fl_pid
)
861 struct nfs4_lock_state
*lsp
;
865 seq
= read_seqbegin(&state
->seqlock
);
866 memcpy(dst
, &state
->stateid
, sizeof(*dst
));
867 } while (read_seqretry(&state
->seqlock
, seq
));
868 if (test_bit(LK_STATE_IN_USE
, &state
->flags
) == 0)
871 spin_lock(&state
->state_lock
);
872 lsp
= __nfs4_find_lock_state(state
, fl_owner
, fl_pid
, NFS4_ANY_LOCK_TYPE
);
873 if (lsp
!= NULL
&& (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
874 memcpy(dst
, &lsp
->ls_stateid
, sizeof(*dst
));
875 spin_unlock(&state
->state_lock
);
876 nfs4_put_lock_state(lsp
);
879 struct nfs_seqid
*nfs_alloc_seqid(struct nfs_seqid_counter
*counter
, gfp_t gfp_mask
)
881 struct nfs_seqid
*new;
883 new = kmalloc(sizeof(*new), gfp_mask
);
885 new->sequence
= counter
;
886 INIT_LIST_HEAD(&new->list
);
891 void nfs_release_seqid(struct nfs_seqid
*seqid
)
893 if (!list_empty(&seqid
->list
)) {
894 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
896 spin_lock(&sequence
->lock
);
897 list_del_init(&seqid
->list
);
898 spin_unlock(&sequence
->lock
);
899 rpc_wake_up(&sequence
->wait
);
903 void nfs_free_seqid(struct nfs_seqid
*seqid
)
905 nfs_release_seqid(seqid
);
910 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
911 * failed with a seqid incrementing error -
912 * see comments nfs_fs.h:seqid_mutating_error()
914 static void nfs_increment_seqid(int status
, struct nfs_seqid
*seqid
)
916 BUG_ON(list_first_entry(&seqid
->sequence
->sequence
->list
, struct nfs_seqid
, list
) != seqid
);
920 case -NFS4ERR_BAD_SEQID
:
921 if (seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)
923 printk(KERN_WARNING
"NFS: v4 server returned a bad"
924 " sequence-id error on an"
925 " unconfirmed sequence %p!\n",
927 case -NFS4ERR_STALE_CLIENTID
:
928 case -NFS4ERR_STALE_STATEID
:
929 case -NFS4ERR_BAD_STATEID
:
930 case -NFS4ERR_BADXDR
:
931 case -NFS4ERR_RESOURCE
:
932 case -NFS4ERR_NOFILEHANDLE
:
933 /* Non-seqid mutating errors */
937 * Note: no locking needed as we are guaranteed to be first
938 * on the sequence list
940 seqid
->sequence
->counter
++;
943 void nfs_increment_open_seqid(int status
, struct nfs_seqid
*seqid
)
945 struct nfs4_state_owner
*sp
= container_of(seqid
->sequence
,
946 struct nfs4_state_owner
, so_seqid
);
947 struct nfs_server
*server
= sp
->so_server
;
949 if (status
== -NFS4ERR_BAD_SEQID
)
950 nfs4_drop_state_owner(sp
);
951 if (!nfs4_has_session(server
->nfs_client
))
952 nfs_increment_seqid(status
, seqid
);
956 * Increment the seqid if the LOCK/LOCKU succeeded, or
957 * failed with a seqid incrementing error -
958 * see comments nfs_fs.h:seqid_mutating_error()
960 void nfs_increment_lock_seqid(int status
, struct nfs_seqid
*seqid
)
962 nfs_increment_seqid(status
, seqid
);
965 int nfs_wait_on_sequence(struct nfs_seqid
*seqid
, struct rpc_task
*task
)
967 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
970 spin_lock(&sequence
->lock
);
971 if (list_empty(&seqid
->list
))
972 list_add_tail(&seqid
->list
, &sequence
->list
);
973 if (list_first_entry(&sequence
->list
, struct nfs_seqid
, list
) == seqid
)
975 rpc_sleep_on(&sequence
->wait
, task
, NULL
);
978 spin_unlock(&sequence
->lock
);
982 static int nfs4_run_state_manager(void *);
984 static void nfs4_clear_state_manager_bit(struct nfs_client
*clp
)
986 smp_mb__before_clear_bit();
987 clear_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
);
988 smp_mb__after_clear_bit();
989 wake_up_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
);
990 rpc_wake_up(&clp
->cl_rpcwaitq
);
994 * Schedule the nfs_client asynchronous state management routine
996 void nfs4_schedule_state_manager(struct nfs_client
*clp
)
998 struct task_struct
*task
;
1000 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) != 0)
1002 __module_get(THIS_MODULE
);
1003 atomic_inc(&clp
->cl_count
);
1004 task
= kthread_run(nfs4_run_state_manager
, clp
, "%s-manager",
1005 rpc_peeraddr2str(clp
->cl_rpcclient
,
1009 nfs4_clear_state_manager_bit(clp
);
1010 nfs_put_client(clp
);
1011 module_put(THIS_MODULE
);
1015 * Schedule a lease recovery attempt
1017 void nfs4_schedule_lease_recovery(struct nfs_client
*clp
)
1021 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1022 set_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1023 nfs4_schedule_state_manager(clp
);
1026 static int nfs4_state_mark_reclaim_reboot(struct nfs_client
*clp
, struct nfs4_state
*state
)
1029 set_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1030 /* Don't recover state that expired before the reboot */
1031 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
)) {
1032 clear_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1035 set_bit(NFS_OWNER_RECLAIM_REBOOT
, &state
->owner
->so_flags
);
1036 set_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
);
1040 static int nfs4_state_mark_reclaim_nograce(struct nfs_client
*clp
, struct nfs4_state
*state
)
1042 set_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
);
1043 clear_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1044 set_bit(NFS_OWNER_RECLAIM_NOGRACE
, &state
->owner
->so_flags
);
1045 set_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
);
1049 void nfs4_schedule_stateid_recovery(const struct nfs_server
*server
, struct nfs4_state
*state
)
1051 struct nfs_client
*clp
= server
->nfs_client
;
1053 nfs4_state_mark_reclaim_nograce(clp
, state
);
1054 nfs4_schedule_state_manager(clp
);
1057 static int nfs4_reclaim_locks(struct nfs4_state
*state
, const struct nfs4_state_recovery_ops
*ops
)
1059 struct inode
*inode
= state
->inode
;
1060 struct nfs_inode
*nfsi
= NFS_I(inode
);
1061 struct file_lock
*fl
;
1064 if (inode
->i_flock
== NULL
)
1067 /* Guard against delegation returns and new lock/unlock calls */
1068 down_write(&nfsi
->rwsem
);
1069 /* Protect inode->i_flock using the BKL */
1071 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
1072 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
1074 if (nfs_file_open_context(fl
->fl_file
)->state
!= state
)
1077 status
= ops
->recover_lock(state
, fl
);
1082 case -NFS4ERR_ADMIN_REVOKED
:
1083 case -NFS4ERR_STALE_STATEID
:
1084 case -NFS4ERR_BAD_STATEID
:
1085 case -NFS4ERR_EXPIRED
:
1086 case -NFS4ERR_NO_GRACE
:
1087 case -NFS4ERR_STALE_CLIENTID
:
1088 case -NFS4ERR_BADSESSION
:
1089 case -NFS4ERR_BADSLOT
:
1090 case -NFS4ERR_BAD_HIGH_SLOT
:
1091 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1094 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
1097 case -NFS4ERR_DENIED
:
1098 case -NFS4ERR_RECLAIM_BAD
:
1099 case -NFS4ERR_RECLAIM_CONFLICT
:
1100 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1107 up_write(&nfsi
->rwsem
);
1111 static int nfs4_reclaim_open_state(struct nfs4_state_owner
*sp
, const struct nfs4_state_recovery_ops
*ops
)
1113 struct nfs4_state
*state
;
1114 struct nfs4_lock_state
*lock
;
1117 /* Note: we rely on the sp->so_states list being ordered
1118 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1120 * This is needed to ensure that the server won't give us any
1121 * read delegations that we have to return if, say, we are
1122 * recovering after a network partition or a reboot from a
1123 * server that doesn't support a grace period.
1126 spin_lock(&sp
->so_lock
);
1127 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1128 if (!test_and_clear_bit(ops
->state_flag_bit
, &state
->flags
))
1130 if (state
->state
== 0)
1132 atomic_inc(&state
->count
);
1133 spin_unlock(&sp
->so_lock
);
1134 status
= ops
->recover_open(sp
, state
);
1136 status
= nfs4_reclaim_locks(state
, ops
);
1138 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
1139 if (!(lock
->ls_flags
& NFS_LOCK_INITIALIZED
))
1140 printk("%s: Lock reclaim failed!\n",
1143 nfs4_put_open_state(state
);
1149 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
1155 * Open state on this file cannot be recovered
1156 * All we can do is revert to using the zero stateid.
1158 memset(state
->stateid
.data
, 0,
1159 sizeof(state
->stateid
.data
));
1160 /* Mark the file as being 'closed' */
1165 * User RPCSEC_GSS context has expired.
1166 * We cannot recover this stateid now, so
1167 * skip it and allow recovery thread to
1171 case -NFS4ERR_ADMIN_REVOKED
:
1172 case -NFS4ERR_STALE_STATEID
:
1173 case -NFS4ERR_BAD_STATEID
:
1174 case -NFS4ERR_RECLAIM_BAD
:
1175 case -NFS4ERR_RECLAIM_CONFLICT
:
1176 nfs4_state_mark_reclaim_nograce(sp
->so_server
->nfs_client
, state
);
1178 case -NFS4ERR_EXPIRED
:
1179 case -NFS4ERR_NO_GRACE
:
1180 nfs4_state_mark_reclaim_nograce(sp
->so_server
->nfs_client
, state
);
1181 case -NFS4ERR_STALE_CLIENTID
:
1182 case -NFS4ERR_BADSESSION
:
1183 case -NFS4ERR_BADSLOT
:
1184 case -NFS4ERR_BAD_HIGH_SLOT
:
1185 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1188 nfs4_put_open_state(state
);
1191 spin_unlock(&sp
->so_lock
);
1194 nfs4_put_open_state(state
);
1198 static void nfs4_clear_open_state(struct nfs4_state
*state
)
1200 struct nfs4_lock_state
*lock
;
1202 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1203 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1204 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1205 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1206 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
1207 lock
->ls_seqid
.flags
= 0;
1208 lock
->ls_flags
&= ~NFS_LOCK_INITIALIZED
;
1212 static void nfs4_reset_seqids(struct nfs_server
*server
,
1213 int (*mark_reclaim
)(struct nfs_client
*clp
, struct nfs4_state
*state
))
1215 struct nfs_client
*clp
= server
->nfs_client
;
1216 struct nfs4_state_owner
*sp
;
1217 struct rb_node
*pos
;
1218 struct nfs4_state
*state
;
1220 spin_lock(&clp
->cl_lock
);
1221 for (pos
= rb_first(&server
->state_owners
);
1223 pos
= rb_next(pos
)) {
1224 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
1225 sp
->so_seqid
.flags
= 0;
1226 spin_lock(&sp
->so_lock
);
1227 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1228 if (mark_reclaim(clp
, state
))
1229 nfs4_clear_open_state(state
);
1231 spin_unlock(&sp
->so_lock
);
1233 spin_unlock(&clp
->cl_lock
);
1236 static void nfs4_state_mark_reclaim_helper(struct nfs_client
*clp
,
1237 int (*mark_reclaim
)(struct nfs_client
*clp
, struct nfs4_state
*state
))
1239 struct nfs_server
*server
;
1242 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1243 nfs4_reset_seqids(server
, mark_reclaim
);
1247 static void nfs4_state_start_reclaim_reboot(struct nfs_client
*clp
)
1249 /* Mark all delegations for reclaim */
1250 nfs_delegation_mark_reclaim(clp
);
1251 nfs4_state_mark_reclaim_helper(clp
, nfs4_state_mark_reclaim_reboot
);
1254 static void nfs4_reclaim_complete(struct nfs_client
*clp
,
1255 const struct nfs4_state_recovery_ops
*ops
)
1257 /* Notify the server we're done reclaiming our state */
1258 if (ops
->reclaim_complete
)
1259 (void)ops
->reclaim_complete(clp
);
1262 static void nfs4_clear_reclaim_server(struct nfs_server
*server
)
1264 struct nfs_client
*clp
= server
->nfs_client
;
1265 struct nfs4_state_owner
*sp
;
1266 struct rb_node
*pos
;
1267 struct nfs4_state
*state
;
1269 spin_lock(&clp
->cl_lock
);
1270 for (pos
= rb_first(&server
->state_owners
);
1272 pos
= rb_next(pos
)) {
1273 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
1274 spin_lock(&sp
->so_lock
);
1275 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1276 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT
,
1279 nfs4_state_mark_reclaim_nograce(clp
, state
);
1281 spin_unlock(&sp
->so_lock
);
1283 spin_unlock(&clp
->cl_lock
);
1286 static int nfs4_state_clear_reclaim_reboot(struct nfs_client
*clp
)
1288 struct nfs_server
*server
;
1290 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
))
1294 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1295 nfs4_clear_reclaim_server(server
);
1298 nfs_delegation_reap_unclaimed(clp
);
1302 static void nfs4_state_end_reclaim_reboot(struct nfs_client
*clp
)
1304 if (!nfs4_state_clear_reclaim_reboot(clp
))
1306 nfs4_reclaim_complete(clp
, clp
->cl_mvops
->reboot_recovery_ops
);
1309 static void nfs_delegation_clear_all(struct nfs_client
*clp
)
1311 nfs_delegation_mark_reclaim(clp
);
1312 nfs_delegation_reap_unclaimed(clp
);
1315 static void nfs4_state_start_reclaim_nograce(struct nfs_client
*clp
)
1317 nfs_delegation_clear_all(clp
);
1318 nfs4_state_mark_reclaim_helper(clp
, nfs4_state_mark_reclaim_nograce
);
1321 static void nfs4_warn_keyexpired(const char *s
)
1323 printk_ratelimited(KERN_WARNING
"Error: state manager"
1324 " encountered RPCSEC_GSS session"
1325 " expired against NFSv4 server %s.\n",
1329 static int nfs4_recovery_handle_error(struct nfs_client
*clp
, int error
)
1332 case -NFS4ERR_CB_PATH_DOWN
:
1333 nfs_handle_cb_pathdown(clp
);
1335 case -NFS4ERR_NO_GRACE
:
1336 nfs4_state_end_reclaim_reboot(clp
);
1338 case -NFS4ERR_STALE_CLIENTID
:
1339 case -NFS4ERR_LEASE_MOVED
:
1340 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1341 nfs4_state_clear_reclaim_reboot(clp
);
1342 nfs4_state_start_reclaim_reboot(clp
);
1344 case -NFS4ERR_EXPIRED
:
1345 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1346 nfs4_state_start_reclaim_nograce(clp
);
1348 case -NFS4ERR_BADSESSION
:
1349 case -NFS4ERR_BADSLOT
:
1350 case -NFS4ERR_BAD_HIGH_SLOT
:
1351 case -NFS4ERR_DEADSESSION
:
1352 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1353 case -NFS4ERR_SEQ_FALSE_RETRY
:
1354 case -NFS4ERR_SEQ_MISORDERED
:
1355 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
1356 /* Zero session reset errors */
1359 /* Nothing we can do */
1360 nfs4_warn_keyexpired(clp
->cl_hostname
);
1366 static int nfs4_do_reclaim(struct nfs_client
*clp
, const struct nfs4_state_recovery_ops
*ops
)
1368 struct nfs4_state_owner
*sp
;
1369 struct nfs_server
*server
;
1370 struct rb_node
*pos
;
1375 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1376 spin_lock(&clp
->cl_lock
);
1377 for (pos
= rb_first(&server
->state_owners
);
1379 pos
= rb_next(pos
)) {
1381 struct nfs4_state_owner
, so_server_node
);
1382 if (!test_and_clear_bit(ops
->owner_flag_bit
,
1385 atomic_inc(&sp
->so_count
);
1386 spin_unlock(&clp
->cl_lock
);
1389 status
= nfs4_reclaim_open_state(sp
, ops
);
1391 set_bit(ops
->owner_flag_bit
, &sp
->so_flags
);
1392 nfs4_put_state_owner(sp
);
1393 return nfs4_recovery_handle_error(clp
, status
);
1396 nfs4_put_state_owner(sp
);
1399 spin_unlock(&clp
->cl_lock
);
1405 static int nfs4_check_lease(struct nfs_client
*clp
)
1407 struct rpc_cred
*cred
;
1408 const struct nfs4_state_maintenance_ops
*ops
=
1409 clp
->cl_mvops
->state_renewal_ops
;
1410 int status
= -NFS4ERR_EXPIRED
;
1412 /* Is the client already known to have an expired lease? */
1413 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1415 spin_lock(&clp
->cl_lock
);
1416 cred
= ops
->get_state_renewal_cred_locked(clp
);
1417 spin_unlock(&clp
->cl_lock
);
1419 cred
= nfs4_get_setclientid_cred(clp
);
1423 status
= ops
->renew_lease(clp
, cred
);
1426 return nfs4_recovery_handle_error(clp
, status
);
1429 static int nfs4_reclaim_lease(struct nfs_client
*clp
)
1431 struct rpc_cred
*cred
;
1432 const struct nfs4_state_recovery_ops
*ops
=
1433 clp
->cl_mvops
->reboot_recovery_ops
;
1434 int status
= -ENOENT
;
1436 cred
= ops
->get_clid_cred(clp
);
1438 status
= ops
->establish_clid(clp
, cred
);
1440 /* Handle case where the user hasn't set up machine creds */
1441 if (status
== -EACCES
&& cred
== clp
->cl_machine_cred
) {
1442 nfs4_clear_machine_cred(clp
);
1445 if (status
== -NFS4ERR_MINOR_VERS_MISMATCH
)
1446 status
= -EPROTONOSUPPORT
;
1451 #ifdef CONFIG_NFS_V4_1
1452 void nfs4_schedule_session_recovery(struct nfs4_session
*session
)
1454 nfs4_schedule_lease_recovery(session
->clp
);
1456 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery
);
1458 void nfs41_handle_recall_slot(struct nfs_client
*clp
)
1460 set_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
);
1461 nfs4_schedule_state_manager(clp
);
1464 static void nfs4_reset_all_state(struct nfs_client
*clp
)
1466 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0) {
1467 clp
->cl_boot_time
= CURRENT_TIME
;
1468 nfs4_state_start_reclaim_nograce(clp
);
1469 nfs4_schedule_state_manager(clp
);
1473 static void nfs41_handle_server_reboot(struct nfs_client
*clp
)
1475 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0) {
1476 nfs4_state_start_reclaim_reboot(clp
);
1477 nfs4_schedule_state_manager(clp
);
1481 static void nfs41_handle_state_revoked(struct nfs_client
*clp
)
1484 nfs4_reset_all_state(clp
);
1487 static void nfs41_handle_recallable_state_revoked(struct nfs_client
*clp
)
1489 /* This will need to handle layouts too */
1490 nfs_expire_all_delegations(clp
);
1493 static void nfs41_handle_cb_path_down(struct nfs_client
*clp
)
1495 nfs_expire_all_delegations(clp
);
1496 if (test_and_set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
) == 0)
1497 nfs4_schedule_state_manager(clp
);
1500 void nfs41_handle_sequence_flag_errors(struct nfs_client
*clp
, u32 flags
)
1504 else if (flags
& SEQ4_STATUS_RESTART_RECLAIM_NEEDED
)
1505 nfs41_handle_server_reboot(clp
);
1506 else if (flags
& (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED
|
1507 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED
|
1508 SEQ4_STATUS_ADMIN_STATE_REVOKED
|
1509 SEQ4_STATUS_LEASE_MOVED
))
1510 nfs41_handle_state_revoked(clp
);
1511 else if (flags
& SEQ4_STATUS_RECALLABLE_STATE_REVOKED
)
1512 nfs41_handle_recallable_state_revoked(clp
);
1513 else if (flags
& (SEQ4_STATUS_CB_PATH_DOWN
|
1514 SEQ4_STATUS_BACKCHANNEL_FAULT
|
1515 SEQ4_STATUS_CB_PATH_DOWN_SESSION
))
1516 nfs41_handle_cb_path_down(clp
);
1519 static int nfs4_reset_session(struct nfs_client
*clp
)
1523 nfs4_begin_drain_session(clp
);
1524 status
= nfs4_proc_destroy_session(clp
->cl_session
);
1525 if (status
&& status
!= -NFS4ERR_BADSESSION
&&
1526 status
!= -NFS4ERR_DEADSESSION
) {
1527 status
= nfs4_recovery_handle_error(clp
, status
);
1531 memset(clp
->cl_session
->sess_id
.data
, 0, NFS4_MAX_SESSIONID_LEN
);
1532 status
= nfs4_proc_create_session(clp
);
1534 status
= nfs4_recovery_handle_error(clp
, status
);
1537 /* create_session negotiated new slot table */
1538 clear_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
);
1540 /* Let the state manager reestablish state */
1541 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1542 nfs41_setup_state_renewal(clp
);
1547 static int nfs4_recall_slot(struct nfs_client
*clp
)
1549 struct nfs4_slot_table
*fc_tbl
= &clp
->cl_session
->fc_slot_table
;
1550 struct nfs4_channel_attrs
*fc_attrs
= &clp
->cl_session
->fc_attrs
;
1551 struct nfs4_slot
*new, *old
;
1554 nfs4_begin_drain_session(clp
);
1555 new = kmalloc(fc_tbl
->target_max_slots
* sizeof(struct nfs4_slot
),
1560 spin_lock(&fc_tbl
->slot_tbl_lock
);
1561 for (i
= 0; i
< fc_tbl
->target_max_slots
; i
++)
1562 new[i
].seq_nr
= fc_tbl
->slots
[i
].seq_nr
;
1563 old
= fc_tbl
->slots
;
1564 fc_tbl
->slots
= new;
1565 fc_tbl
->max_slots
= fc_tbl
->target_max_slots
;
1566 fc_tbl
->target_max_slots
= 0;
1567 fc_attrs
->max_reqs
= fc_tbl
->max_slots
;
1568 spin_unlock(&fc_tbl
->slot_tbl_lock
);
1571 nfs4_end_drain_session(clp
);
1575 #else /* CONFIG_NFS_V4_1 */
1576 static int nfs4_reset_session(struct nfs_client
*clp
) { return 0; }
1577 static int nfs4_end_drain_session(struct nfs_client
*clp
) { return 0; }
1578 static int nfs4_recall_slot(struct nfs_client
*clp
) { return 0; }
1579 #endif /* CONFIG_NFS_V4_1 */
1581 /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1582 * on EXCHANGE_ID for v4.1
1584 static void nfs4_set_lease_expired(struct nfs_client
*clp
, int status
)
1586 if (nfs4_has_session(clp
)) {
1588 case -NFS4ERR_DELAY
:
1589 case -NFS4ERR_CLID_INUSE
:
1594 nfs4_warn_keyexpired(clp
->cl_hostname
);
1595 case -NFS4ERR_NOT_SAME
: /* FixMe: implement recovery
1596 * in nfs4_exchange_id */
1601 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1604 static void nfs4_state_manager(struct nfs_client
*clp
)
1608 /* Ensure exclusive access to NFSv4 state */
1610 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
)) {
1611 /* We're going to have to re-establish a clientid */
1612 status
= nfs4_reclaim_lease(clp
);
1614 nfs4_set_lease_expired(clp
, status
);
1615 if (test_bit(NFS4CLNT_LEASE_EXPIRED
,
1618 if (clp
->cl_cons_state
==
1619 NFS_CS_SESSION_INITING
)
1620 nfs_mark_client_ready(clp
, status
);
1623 clear_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1624 set_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
);
1625 pnfs_destroy_all_layouts(clp
);
1628 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
)) {
1629 status
= nfs4_check_lease(clp
);
1630 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1632 if (status
< 0 && status
!= -NFS4ERR_CB_PATH_DOWN
)
1636 /* Initialize or reset the session */
1637 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
)
1638 && nfs4_has_session(clp
)) {
1639 status
= nfs4_reset_session(clp
);
1640 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1646 /* First recover reboot state... */
1647 if (test_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
)) {
1648 status
= nfs4_do_reclaim(clp
,
1649 clp
->cl_mvops
->reboot_recovery_ops
);
1650 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) ||
1651 test_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
))
1653 nfs4_state_end_reclaim_reboot(clp
);
1654 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
))
1660 /* Now recover expired state... */
1661 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
)) {
1662 status
= nfs4_do_reclaim(clp
,
1663 clp
->cl_mvops
->nograce_recovery_ops
);
1664 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) ||
1665 test_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
) ||
1666 test_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
))
1672 nfs4_end_drain_session(clp
);
1673 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
)) {
1674 nfs_client_return_marked_delegations(clp
);
1677 /* Recall session slots */
1678 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
)
1679 && nfs4_has_session(clp
)) {
1680 status
= nfs4_recall_slot(clp
);
1687 nfs4_clear_state_manager_bit(clp
);
1688 /* Did we race with an attempt to give us more work? */
1689 if (clp
->cl_state
== 0)
1691 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) != 0)
1696 printk(KERN_WARNING
"Error: state manager failed on NFSv4 server %s"
1697 " with error %d\n", clp
->cl_hostname
, -status
);
1698 nfs4_end_drain_session(clp
);
1699 nfs4_clear_state_manager_bit(clp
);
1702 static int nfs4_run_state_manager(void *ptr
)
1704 struct nfs_client
*clp
= ptr
;
1706 allow_signal(SIGKILL
);
1707 nfs4_state_manager(clp
);
1708 nfs_put_client(clp
);
1709 module_put_and_exit(0);