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/slab.h>
42 #include <linux/smp_lock.h>
43 #include <linux/nfs_fs.h>
44 #include <linux/nfs_idmap.h>
45 #include <linux/kthread.h>
46 #include <linux/module.h>
47 #include <linux/workqueue.h>
48 #include <linux/bitops.h>
52 #include "delegation.h"
54 #define OPENOWNER_POOL_SIZE 8
56 const nfs4_stateid zero_stateid
;
58 static DEFINE_SPINLOCK(state_spinlock
);
59 static LIST_HEAD(nfs4_clientid_list
);
62 init_nfsv4_state(struct nfs_server
*server
)
64 server
->nfs4_state
= NULL
;
65 INIT_LIST_HEAD(&server
->nfs4_siblings
);
69 destroy_nfsv4_state(struct nfs_server
*server
)
71 kfree(server
->mnt_path
);
72 server
->mnt_path
= NULL
;
73 if (server
->nfs4_state
) {
74 nfs4_put_client(server
->nfs4_state
);
75 server
->nfs4_state
= NULL
;
80 * nfs4_get_client(): returns an empty client structure
81 * nfs4_put_client(): drops reference to client structure
83 * Since these are allocated/deallocated very rarely, we don't
84 * bother putting them in a slab cache...
86 static struct nfs4_client
*
87 nfs4_alloc_client(struct in_addr
*addr
)
89 struct nfs4_client
*clp
;
91 if (nfs_callback_up() < 0)
93 if ((clp
= kzalloc(sizeof(*clp
), GFP_KERNEL
)) == NULL
) {
97 memcpy(&clp
->cl_addr
, addr
, sizeof(clp
->cl_addr
));
98 init_rwsem(&clp
->cl_sem
);
99 INIT_LIST_HEAD(&clp
->cl_delegations
);
100 INIT_LIST_HEAD(&clp
->cl_state_owners
);
101 INIT_LIST_HEAD(&clp
->cl_unused
);
102 spin_lock_init(&clp
->cl_lock
);
103 atomic_set(&clp
->cl_count
, 1);
104 INIT_WORK(&clp
->cl_renewd
, nfs4_renew_state
, clp
);
105 INIT_LIST_HEAD(&clp
->cl_superblocks
);
106 rpc_init_wait_queue(&clp
->cl_rpcwaitq
, "NFS4 client");
107 clp
->cl_rpcclient
= ERR_PTR(-EINVAL
);
108 clp
->cl_boot_time
= CURRENT_TIME
;
109 clp
->cl_state
= 1 << NFS4CLNT_LEASE_EXPIRED
;
114 nfs4_free_client(struct nfs4_client
*clp
)
116 struct nfs4_state_owner
*sp
;
118 while (!list_empty(&clp
->cl_unused
)) {
119 sp
= list_entry(clp
->cl_unused
.next
,
120 struct nfs4_state_owner
,
122 list_del(&sp
->so_list
);
125 BUG_ON(!list_empty(&clp
->cl_state_owners
));
126 nfs_idmap_delete(clp
);
127 if (!IS_ERR(clp
->cl_rpcclient
))
128 rpc_shutdown_client(clp
->cl_rpcclient
);
133 static struct nfs4_client
*__nfs4_find_client(struct in_addr
*addr
)
135 struct nfs4_client
*clp
;
136 list_for_each_entry(clp
, &nfs4_clientid_list
, cl_servers
) {
137 if (memcmp(&clp
->cl_addr
, addr
, sizeof(clp
->cl_addr
)) == 0) {
138 atomic_inc(&clp
->cl_count
);
145 struct nfs4_client
*nfs4_find_client(struct in_addr
*addr
)
147 struct nfs4_client
*clp
;
148 spin_lock(&state_spinlock
);
149 clp
= __nfs4_find_client(addr
);
150 spin_unlock(&state_spinlock
);
155 nfs4_get_client(struct in_addr
*addr
)
157 struct nfs4_client
*clp
, *new = NULL
;
159 spin_lock(&state_spinlock
);
161 clp
= __nfs4_find_client(addr
);
166 list_add(&clp
->cl_servers
, &nfs4_clientid_list
);
170 spin_unlock(&state_spinlock
);
171 new = nfs4_alloc_client(addr
);
172 spin_lock(&state_spinlock
);
176 spin_unlock(&state_spinlock
);
178 nfs4_free_client(new);
183 nfs4_put_client(struct nfs4_client
*clp
)
185 if (!atomic_dec_and_lock(&clp
->cl_count
, &state_spinlock
))
187 list_del(&clp
->cl_servers
);
188 spin_unlock(&state_spinlock
);
189 BUG_ON(!list_empty(&clp
->cl_superblocks
));
190 rpc_wake_up(&clp
->cl_rpcwaitq
);
191 nfs4_kill_renewd(clp
);
192 nfs4_free_client(clp
);
195 static int nfs4_init_client(struct nfs4_client
*clp
, struct rpc_cred
*cred
)
197 int status
= nfs4_proc_setclientid(clp
, NFS4_CALLBACK
,
198 nfs_callback_tcpport
, cred
);
200 status
= nfs4_proc_setclientid_confirm(clp
, cred
);
202 nfs4_schedule_state_renewal(clp
);
207 nfs4_alloc_lockowner_id(struct nfs4_client
*clp
)
209 return clp
->cl_lockowner_id
++;
212 static struct nfs4_state_owner
*
213 nfs4_client_grab_unused(struct nfs4_client
*clp
, struct rpc_cred
*cred
)
215 struct nfs4_state_owner
*sp
= NULL
;
217 if (!list_empty(&clp
->cl_unused
)) {
218 sp
= list_entry(clp
->cl_unused
.next
, struct nfs4_state_owner
, so_list
);
219 atomic_inc(&sp
->so_count
);
221 list_move(&sp
->so_list
, &clp
->cl_state_owners
);
227 struct rpc_cred
*nfs4_get_renew_cred(struct nfs4_client
*clp
)
229 struct nfs4_state_owner
*sp
;
230 struct rpc_cred
*cred
= NULL
;
232 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
233 if (list_empty(&sp
->so_states
))
235 cred
= get_rpccred(sp
->so_cred
);
241 struct rpc_cred
*nfs4_get_setclientid_cred(struct nfs4_client
*clp
)
243 struct nfs4_state_owner
*sp
;
245 if (!list_empty(&clp
->cl_state_owners
)) {
246 sp
= list_entry(clp
->cl_state_owners
.next
,
247 struct nfs4_state_owner
, so_list
);
248 return get_rpccred(sp
->so_cred
);
253 static struct nfs4_state_owner
*
254 nfs4_find_state_owner(struct nfs4_client
*clp
, struct rpc_cred
*cred
)
256 struct nfs4_state_owner
*sp
, *res
= NULL
;
258 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
259 if (sp
->so_cred
!= cred
)
261 atomic_inc(&sp
->so_count
);
262 /* Move to the head of the list */
263 list_move(&sp
->so_list
, &clp
->cl_state_owners
);
271 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
272 * create a new state_owner.
275 static struct nfs4_state_owner
*
276 nfs4_alloc_state_owner(void)
278 struct nfs4_state_owner
*sp
;
280 sp
= kzalloc(sizeof(*sp
),GFP_KERNEL
);
283 spin_lock_init(&sp
->so_lock
);
284 INIT_LIST_HEAD(&sp
->so_states
);
285 INIT_LIST_HEAD(&sp
->so_delegations
);
286 rpc_init_wait_queue(&sp
->so_sequence
.wait
, "Seqid_waitqueue");
287 sp
->so_seqid
.sequence
= &sp
->so_sequence
;
288 spin_lock_init(&sp
->so_sequence
.lock
);
289 INIT_LIST_HEAD(&sp
->so_sequence
.list
);
290 atomic_set(&sp
->so_count
, 1);
295 nfs4_drop_state_owner(struct nfs4_state_owner
*sp
)
297 struct nfs4_client
*clp
= sp
->so_client
;
298 spin_lock(&clp
->cl_lock
);
299 list_del_init(&sp
->so_list
);
300 spin_unlock(&clp
->cl_lock
);
304 * Note: must be called with clp->cl_sem held in order to prevent races
305 * with reboot recovery!
307 struct nfs4_state_owner
*nfs4_get_state_owner(struct nfs_server
*server
, struct rpc_cred
*cred
)
309 struct nfs4_client
*clp
= server
->nfs4_state
;
310 struct nfs4_state_owner
*sp
, *new;
313 new = nfs4_alloc_state_owner();
314 spin_lock(&clp
->cl_lock
);
315 sp
= nfs4_find_state_owner(clp
, cred
);
317 sp
= nfs4_client_grab_unused(clp
, cred
);
318 if (sp
== NULL
&& new != NULL
) {
319 list_add(&new->so_list
, &clp
->cl_state_owners
);
320 new->so_client
= clp
;
321 new->so_id
= nfs4_alloc_lockowner_id(clp
);
326 spin_unlock(&clp
->cl_lock
);
335 * Must be called with clp->cl_sem held in order to avoid races
336 * with state recovery...
338 void nfs4_put_state_owner(struct nfs4_state_owner
*sp
)
340 struct nfs4_client
*clp
= sp
->so_client
;
341 struct rpc_cred
*cred
= sp
->so_cred
;
343 if (!atomic_dec_and_lock(&sp
->so_count
, &clp
->cl_lock
))
345 if (clp
->cl_nunused
>= OPENOWNER_POOL_SIZE
)
347 if (list_empty(&sp
->so_list
))
349 list_move(&sp
->so_list
, &clp
->cl_unused
);
351 spin_unlock(&clp
->cl_lock
);
356 list_del(&sp
->so_list
);
357 spin_unlock(&clp
->cl_lock
);
362 static struct nfs4_state
*
363 nfs4_alloc_open_state(void)
365 struct nfs4_state
*state
;
367 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
370 atomic_set(&state
->count
, 1);
371 INIT_LIST_HEAD(&state
->lock_states
);
372 spin_lock_init(&state
->state_lock
);
377 nfs4_state_set_mode_locked(struct nfs4_state
*state
, mode_t mode
)
379 if (state
->state
== mode
)
381 /* NB! List reordering - see the reclaim code for why. */
382 if ((mode
& FMODE_WRITE
) != (state
->state
& FMODE_WRITE
)) {
383 if (mode
& FMODE_WRITE
)
384 list_move(&state
->open_states
, &state
->owner
->so_states
);
386 list_move_tail(&state
->open_states
, &state
->owner
->so_states
);
389 list_del_init(&state
->inode_states
);
393 static struct nfs4_state
*
394 __nfs4_find_state_byowner(struct inode
*inode
, struct nfs4_state_owner
*owner
)
396 struct nfs_inode
*nfsi
= NFS_I(inode
);
397 struct nfs4_state
*state
;
399 list_for_each_entry(state
, &nfsi
->open_states
, inode_states
) {
400 /* Is this in the process of being freed? */
401 if (state
->state
== 0)
403 if (state
->owner
== owner
) {
404 atomic_inc(&state
->count
);
412 nfs4_free_open_state(struct nfs4_state
*state
)
418 nfs4_get_open_state(struct inode
*inode
, struct nfs4_state_owner
*owner
)
420 struct nfs4_state
*state
, *new;
421 struct nfs_inode
*nfsi
= NFS_I(inode
);
423 spin_lock(&inode
->i_lock
);
424 state
= __nfs4_find_state_byowner(inode
, owner
);
425 spin_unlock(&inode
->i_lock
);
428 new = nfs4_alloc_open_state();
429 spin_lock(&owner
->so_lock
);
430 spin_lock(&inode
->i_lock
);
431 state
= __nfs4_find_state_byowner(inode
, owner
);
432 if (state
== NULL
&& new != NULL
) {
434 state
->owner
= owner
;
435 atomic_inc(&owner
->so_count
);
436 list_add(&state
->inode_states
, &nfsi
->open_states
);
437 state
->inode
= igrab(inode
);
438 spin_unlock(&inode
->i_lock
);
439 /* Note: The reclaim code dictates that we add stateless
440 * and read-only stateids to the end of the list */
441 list_add_tail(&state
->open_states
, &owner
->so_states
);
442 spin_unlock(&owner
->so_lock
);
444 spin_unlock(&inode
->i_lock
);
445 spin_unlock(&owner
->so_lock
);
447 nfs4_free_open_state(new);
454 * Beware! Caller must be holding exactly one
455 * reference to clp->cl_sem!
457 void nfs4_put_open_state(struct nfs4_state
*state
)
459 struct inode
*inode
= state
->inode
;
460 struct nfs4_state_owner
*owner
= state
->owner
;
462 if (!atomic_dec_and_lock(&state
->count
, &owner
->so_lock
))
464 spin_lock(&inode
->i_lock
);
465 if (!list_empty(&state
->inode_states
))
466 list_del(&state
->inode_states
);
467 list_del(&state
->open_states
);
468 spin_unlock(&inode
->i_lock
);
469 spin_unlock(&owner
->so_lock
);
471 nfs4_free_open_state(state
);
472 nfs4_put_state_owner(owner
);
476 * Close the current file.
478 void nfs4_close_state(struct nfs4_state
*state
, mode_t mode
)
480 struct inode
*inode
= state
->inode
;
481 struct nfs4_state_owner
*owner
= state
->owner
;
482 int oldstate
, newstate
= 0;
484 atomic_inc(&owner
->so_count
);
485 /* Protect against nfs4_find_state() */
486 spin_lock(&owner
->so_lock
);
487 spin_lock(&inode
->i_lock
);
488 switch (mode
& (FMODE_READ
| FMODE_WRITE
)) {
495 case FMODE_READ
|FMODE_WRITE
:
498 oldstate
= newstate
= state
->state
;
499 if (state
->n_rdwr
== 0) {
500 if (state
->n_rdonly
== 0)
501 newstate
&= ~FMODE_READ
;
502 if (state
->n_wronly
== 0)
503 newstate
&= ~FMODE_WRITE
;
505 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
506 nfs4_state_set_mode_locked(state
, newstate
);
509 spin_unlock(&inode
->i_lock
);
510 spin_unlock(&owner
->so_lock
);
512 if (oldstate
!= newstate
&& nfs4_do_close(inode
, state
) == 0)
514 nfs4_put_open_state(state
);
515 nfs4_put_state_owner(owner
);
519 * Search the state->lock_states for an existing lock_owner
520 * that is compatible with current->files
522 static struct nfs4_lock_state
*
523 __nfs4_find_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
)
525 struct nfs4_lock_state
*pos
;
526 list_for_each_entry(pos
, &state
->lock_states
, ls_locks
) {
527 if (pos
->ls_owner
!= fl_owner
)
529 atomic_inc(&pos
->ls_count
);
536 * Return a compatible lock_state. If no initialized lock_state structure
537 * exists, return an uninitialized one.
540 static struct nfs4_lock_state
*nfs4_alloc_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
)
542 struct nfs4_lock_state
*lsp
;
543 struct nfs4_client
*clp
= state
->owner
->so_client
;
545 lsp
= kzalloc(sizeof(*lsp
), GFP_KERNEL
);
548 lsp
->ls_seqid
.sequence
= &state
->owner
->so_sequence
;
549 atomic_set(&lsp
->ls_count
, 1);
550 lsp
->ls_owner
= fl_owner
;
551 spin_lock(&clp
->cl_lock
);
552 lsp
->ls_id
= nfs4_alloc_lockowner_id(clp
);
553 spin_unlock(&clp
->cl_lock
);
554 INIT_LIST_HEAD(&lsp
->ls_locks
);
559 * Return a compatible lock_state. If no initialized lock_state structure
560 * exists, return an uninitialized one.
562 * The caller must be holding clp->cl_sem
564 static struct nfs4_lock_state
*nfs4_get_lock_state(struct nfs4_state
*state
, fl_owner_t owner
)
566 struct nfs4_lock_state
*lsp
, *new = NULL
;
569 spin_lock(&state
->state_lock
);
570 lsp
= __nfs4_find_lock_state(state
, owner
);
574 new->ls_state
= state
;
575 list_add(&new->ls_locks
, &state
->lock_states
);
576 set_bit(LK_STATE_IN_USE
, &state
->flags
);
581 spin_unlock(&state
->state_lock
);
582 new = nfs4_alloc_lock_state(state
, owner
);
586 spin_unlock(&state
->state_lock
);
592 * Release reference to lock_state, and free it if we see that
593 * it is no longer in use
595 void nfs4_put_lock_state(struct nfs4_lock_state
*lsp
)
597 struct nfs4_state
*state
;
601 state
= lsp
->ls_state
;
602 if (!atomic_dec_and_lock(&lsp
->ls_count
, &state
->state_lock
))
604 list_del(&lsp
->ls_locks
);
605 if (list_empty(&state
->lock_states
))
606 clear_bit(LK_STATE_IN_USE
, &state
->flags
);
607 spin_unlock(&state
->state_lock
);
611 static void nfs4_fl_copy_lock(struct file_lock
*dst
, struct file_lock
*src
)
613 struct nfs4_lock_state
*lsp
= src
->fl_u
.nfs4_fl
.owner
;
615 dst
->fl_u
.nfs4_fl
.owner
= lsp
;
616 atomic_inc(&lsp
->ls_count
);
619 static void nfs4_fl_release_lock(struct file_lock
*fl
)
621 nfs4_put_lock_state(fl
->fl_u
.nfs4_fl
.owner
);
624 static struct file_lock_operations nfs4_fl_lock_ops
= {
625 .fl_copy_lock
= nfs4_fl_copy_lock
,
626 .fl_release_private
= nfs4_fl_release_lock
,
629 int nfs4_set_lock_state(struct nfs4_state
*state
, struct file_lock
*fl
)
631 struct nfs4_lock_state
*lsp
;
633 if (fl
->fl_ops
!= NULL
)
635 lsp
= nfs4_get_lock_state(state
, fl
->fl_owner
);
638 fl
->fl_u
.nfs4_fl
.owner
= lsp
;
639 fl
->fl_ops
= &nfs4_fl_lock_ops
;
644 * Byte-range lock aware utility to initialize the stateid of read/write
647 void nfs4_copy_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
, fl_owner_t fl_owner
)
649 struct nfs4_lock_state
*lsp
;
651 memcpy(dst
, &state
->stateid
, sizeof(*dst
));
652 if (test_bit(LK_STATE_IN_USE
, &state
->flags
) == 0)
655 spin_lock(&state
->state_lock
);
656 lsp
= __nfs4_find_lock_state(state
, fl_owner
);
657 if (lsp
!= NULL
&& (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
658 memcpy(dst
, &lsp
->ls_stateid
, sizeof(*dst
));
659 spin_unlock(&state
->state_lock
);
660 nfs4_put_lock_state(lsp
);
663 struct nfs_seqid
*nfs_alloc_seqid(struct nfs_seqid_counter
*counter
)
665 struct rpc_sequence
*sequence
= counter
->sequence
;
666 struct nfs_seqid
*new;
668 new = kmalloc(sizeof(*new), GFP_KERNEL
);
670 new->sequence
= counter
;
671 spin_lock(&sequence
->lock
);
672 list_add_tail(&new->list
, &sequence
->list
);
673 spin_unlock(&sequence
->lock
);
678 void nfs_free_seqid(struct nfs_seqid
*seqid
)
680 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
682 spin_lock(&sequence
->lock
);
683 list_del(&seqid
->list
);
684 spin_unlock(&sequence
->lock
);
685 rpc_wake_up(&sequence
->wait
);
690 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
691 * failed with a seqid incrementing error -
692 * see comments nfs_fs.h:seqid_mutating_error()
694 static inline void nfs_increment_seqid(int status
, struct nfs_seqid
*seqid
)
699 case -NFS4ERR_BAD_SEQID
:
700 case -NFS4ERR_STALE_CLIENTID
:
701 case -NFS4ERR_STALE_STATEID
:
702 case -NFS4ERR_BAD_STATEID
:
703 case -NFS4ERR_BADXDR
:
704 case -NFS4ERR_RESOURCE
:
705 case -NFS4ERR_NOFILEHANDLE
:
706 /* Non-seqid mutating errors */
710 * Note: no locking needed as we are guaranteed to be first
711 * on the sequence list
713 seqid
->sequence
->counter
++;
716 void nfs_increment_open_seqid(int status
, struct nfs_seqid
*seqid
)
718 if (status
== -NFS4ERR_BAD_SEQID
) {
719 struct nfs4_state_owner
*sp
= container_of(seqid
->sequence
,
720 struct nfs4_state_owner
, so_seqid
);
721 nfs4_drop_state_owner(sp
);
723 return nfs_increment_seqid(status
, seqid
);
727 * Increment the seqid if the LOCK/LOCKU succeeded, or
728 * failed with a seqid incrementing error -
729 * see comments nfs_fs.h:seqid_mutating_error()
731 void nfs_increment_lock_seqid(int status
, struct nfs_seqid
*seqid
)
733 return nfs_increment_seqid(status
, seqid
);
736 int nfs_wait_on_sequence(struct nfs_seqid
*seqid
, struct rpc_task
*task
)
738 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
741 if (sequence
->list
.next
== &seqid
->list
)
743 spin_lock(&sequence
->lock
);
744 if (sequence
->list
.next
!= &seqid
->list
) {
745 rpc_sleep_on(&sequence
->wait
, task
, NULL
, NULL
);
748 spin_unlock(&sequence
->lock
);
753 static int reclaimer(void *);
755 static inline void nfs4_clear_recover_bit(struct nfs4_client
*clp
)
757 smp_mb__before_clear_bit();
758 clear_bit(NFS4CLNT_STATE_RECOVER
, &clp
->cl_state
);
759 smp_mb__after_clear_bit();
760 wake_up_bit(&clp
->cl_state
, NFS4CLNT_STATE_RECOVER
);
761 rpc_wake_up(&clp
->cl_rpcwaitq
);
765 * State recovery routine
767 static void nfs4_recover_state(struct nfs4_client
*clp
)
769 struct task_struct
*task
;
771 __module_get(THIS_MODULE
);
772 atomic_inc(&clp
->cl_count
);
773 task
= kthread_run(reclaimer
, clp
, "%u.%u.%u.%u-reclaim",
774 NIPQUAD(clp
->cl_addr
));
777 nfs4_clear_recover_bit(clp
);
778 nfs4_put_client(clp
);
779 module_put(THIS_MODULE
);
783 * Schedule a state recovery attempt
785 void nfs4_schedule_state_recovery(struct nfs4_client
*clp
)
789 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER
, &clp
->cl_state
) == 0)
790 nfs4_recover_state(clp
);
793 static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops
*ops
, struct nfs4_state
*state
)
795 struct inode
*inode
= state
->inode
;
796 struct file_lock
*fl
;
799 for (fl
= inode
->i_flock
; fl
!= 0; fl
= fl
->fl_next
) {
800 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
802 if (((struct nfs_open_context
*)fl
->fl_file
->private_data
)->state
!= state
)
804 status
= ops
->recover_lock(state
, fl
);
809 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
810 __FUNCTION__
, status
);
811 case -NFS4ERR_EXPIRED
:
812 case -NFS4ERR_NO_GRACE
:
813 case -NFS4ERR_RECLAIM_BAD
:
814 case -NFS4ERR_RECLAIM_CONFLICT
:
815 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
817 case -NFS4ERR_STALE_CLIENTID
:
826 static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops
*ops
, struct nfs4_state_owner
*sp
)
828 struct nfs4_state
*state
;
829 struct nfs4_lock_state
*lock
;
832 /* Note: we rely on the sp->so_states list being ordered
833 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
835 * This is needed to ensure that the server won't give us any
836 * read delegations that we have to return if, say, we are
837 * recovering after a network partition or a reboot from a
838 * server that doesn't support a grace period.
840 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
841 if (state
->state
== 0)
843 status
= ops
->recover_open(sp
, state
);
845 status
= nfs4_reclaim_locks(ops
, state
);
848 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
849 if (!(lock
->ls_flags
& NFS_LOCK_INITIALIZED
))
850 printk("%s: Lock reclaim failed!\n",
857 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
858 __FUNCTION__
, status
);
860 case -NFS4ERR_RECLAIM_BAD
:
861 case -NFS4ERR_RECLAIM_CONFLICT
:
863 * Open state on this file cannot be recovered
864 * All we can do is revert to using the zero stateid.
866 memset(state
->stateid
.data
, 0,
867 sizeof(state
->stateid
.data
));
868 /* Mark the file as being 'closed' */
871 case -NFS4ERR_EXPIRED
:
872 case -NFS4ERR_NO_GRACE
:
873 case -NFS4ERR_STALE_CLIENTID
:
882 static void nfs4_state_mark_reclaim(struct nfs4_client
*clp
)
884 struct nfs4_state_owner
*sp
;
885 struct nfs4_state
*state
;
886 struct nfs4_lock_state
*lock
;
888 /* Reset all sequence ids to zero */
889 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
890 sp
->so_seqid
.counter
= 0;
891 sp
->so_seqid
.flags
= 0;
892 spin_lock(&sp
->so_lock
);
893 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
894 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
895 lock
->ls_seqid
.counter
= 0;
896 lock
->ls_seqid
.flags
= 0;
897 lock
->ls_flags
&= ~NFS_LOCK_INITIALIZED
;
900 spin_unlock(&sp
->so_lock
);
904 static int reclaimer(void *ptr
)
906 struct nfs4_client
*clp
= ptr
;
907 struct nfs4_state_owner
*sp
;
908 struct nfs4_state_recovery_ops
*ops
;
909 struct rpc_cred
*cred
;
912 allow_signal(SIGKILL
);
914 /* Ensure exclusive access to NFSv4 state */
916 down_write(&clp
->cl_sem
);
917 /* Are there any NFS mounts out there? */
918 if (list_empty(&clp
->cl_superblocks
))
921 ops
= &nfs4_network_partition_recovery_ops
;
922 /* Are there any open files on this volume? */
923 cred
= nfs4_get_renew_cred(clp
);
925 /* Yes there are: try to renew the old lease */
926 status
= nfs4_proc_renew(clp
, cred
);
929 case -NFS4ERR_CB_PATH_DOWN
:
932 case -NFS4ERR_STALE_CLIENTID
:
933 case -NFS4ERR_LEASE_MOVED
:
934 ops
= &nfs4_reboot_recovery_ops
;
937 /* "reboot" to ensure we clear all state on the server */
938 clp
->cl_boot_time
= CURRENT_TIME
;
939 cred
= nfs4_get_setclientid_cred(clp
);
941 /* We're going to have to re-establish a clientid */
942 nfs4_state_mark_reclaim(clp
);
945 status
= nfs4_init_client(clp
, cred
);
950 /* Mark all delegations for reclaim */
951 nfs_delegation_mark_reclaim(clp
);
952 /* Note: list is protected by exclusive lock on cl->cl_sem */
953 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
954 status
= nfs4_reclaim_open_state(ops
, sp
);
956 if (status
== -NFS4ERR_NO_GRACE
) {
957 ops
= &nfs4_network_partition_recovery_ops
;
958 status
= nfs4_reclaim_open_state(ops
, sp
);
960 if (status
== -NFS4ERR_STALE_CLIENTID
)
962 if (status
== -NFS4ERR_EXPIRED
)
966 nfs_delegation_reap_unclaimed(clp
);
968 up_write(&clp
->cl_sem
);
970 if (status
== -NFS4ERR_CB_PATH_DOWN
)
971 nfs_handle_cb_pathdown(clp
);
972 nfs4_clear_recover_bit(clp
);
973 nfs4_put_client(clp
);
974 module_put_and_exit(0);
977 printk(KERN_WARNING
"Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
978 NIPQUAD(clp
->cl_addr
.s_addr
), -status
);
979 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);