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/config.h>
42 #include <linux/slab.h>
43 #include <linux/smp_lock.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/workqueue.h>
49 #include <linux/bitops.h>
53 #include "delegation.h"
55 #define OPENOWNER_POOL_SIZE 8
57 const nfs4_stateid zero_stateid
;
59 static DEFINE_SPINLOCK(state_spinlock
);
60 static LIST_HEAD(nfs4_clientid_list
);
63 init_nfsv4_state(struct nfs_server
*server
)
65 server
->nfs4_state
= NULL
;
66 INIT_LIST_HEAD(&server
->nfs4_siblings
);
70 destroy_nfsv4_state(struct nfs_server
*server
)
72 kfree(server
->mnt_path
);
73 server
->mnt_path
= NULL
;
74 if (server
->nfs4_state
) {
75 nfs4_put_client(server
->nfs4_state
);
76 server
->nfs4_state
= NULL
;
81 * nfs4_get_client(): returns an empty client structure
82 * nfs4_put_client(): drops reference to client structure
84 * Since these are allocated/deallocated very rarely, we don't
85 * bother putting them in a slab cache...
87 static struct nfs4_client
*
88 nfs4_alloc_client(struct in_addr
*addr
)
90 struct nfs4_client
*clp
;
92 if (nfs_callback_up() < 0)
94 if ((clp
= kzalloc(sizeof(*clp
), GFP_KERNEL
)) == NULL
) {
98 memcpy(&clp
->cl_addr
, addr
, sizeof(clp
->cl_addr
));
99 init_rwsem(&clp
->cl_sem
);
100 INIT_LIST_HEAD(&clp
->cl_delegations
);
101 INIT_LIST_HEAD(&clp
->cl_state_owners
);
102 INIT_LIST_HEAD(&clp
->cl_unused
);
103 spin_lock_init(&clp
->cl_lock
);
104 atomic_set(&clp
->cl_count
, 1);
105 INIT_WORK(&clp
->cl_renewd
, nfs4_renew_state
, clp
);
106 INIT_LIST_HEAD(&clp
->cl_superblocks
);
107 rpc_init_wait_queue(&clp
->cl_rpcwaitq
, "NFS4 client");
108 clp
->cl_rpcclient
= ERR_PTR(-EINVAL
);
109 clp
->cl_boot_time
= CURRENT_TIME
;
110 clp
->cl_state
= 1 << NFS4CLNT_LEASE_EXPIRED
;
115 nfs4_free_client(struct nfs4_client
*clp
)
117 struct nfs4_state_owner
*sp
;
119 while (!list_empty(&clp
->cl_unused
)) {
120 sp
= list_entry(clp
->cl_unused
.next
,
121 struct nfs4_state_owner
,
123 list_del(&sp
->so_list
);
126 BUG_ON(!list_empty(&clp
->cl_state_owners
));
127 nfs_idmap_delete(clp
);
128 if (!IS_ERR(clp
->cl_rpcclient
))
129 rpc_shutdown_client(clp
->cl_rpcclient
);
134 static struct nfs4_client
*__nfs4_find_client(struct in_addr
*addr
)
136 struct nfs4_client
*clp
;
137 list_for_each_entry(clp
, &nfs4_clientid_list
, cl_servers
) {
138 if (memcmp(&clp
->cl_addr
, addr
, sizeof(clp
->cl_addr
)) == 0) {
139 atomic_inc(&clp
->cl_count
);
146 struct nfs4_client
*nfs4_find_client(struct in_addr
*addr
)
148 struct nfs4_client
*clp
;
149 spin_lock(&state_spinlock
);
150 clp
= __nfs4_find_client(addr
);
151 spin_unlock(&state_spinlock
);
156 nfs4_get_client(struct in_addr
*addr
)
158 struct nfs4_client
*clp
, *new = NULL
;
160 spin_lock(&state_spinlock
);
162 clp
= __nfs4_find_client(addr
);
167 list_add(&clp
->cl_servers
, &nfs4_clientid_list
);
171 spin_unlock(&state_spinlock
);
172 new = nfs4_alloc_client(addr
);
173 spin_lock(&state_spinlock
);
177 spin_unlock(&state_spinlock
);
179 nfs4_free_client(new);
184 nfs4_put_client(struct nfs4_client
*clp
)
186 if (!atomic_dec_and_lock(&clp
->cl_count
, &state_spinlock
))
188 list_del(&clp
->cl_servers
);
189 spin_unlock(&state_spinlock
);
190 BUG_ON(!list_empty(&clp
->cl_superblocks
));
191 rpc_wake_up(&clp
->cl_rpcwaitq
);
192 nfs4_kill_renewd(clp
);
193 nfs4_free_client(clp
);
196 static int nfs4_init_client(struct nfs4_client
*clp
, struct rpc_cred
*cred
)
198 int status
= nfs4_proc_setclientid(clp
, NFS4_CALLBACK
,
199 nfs_callback_tcpport
, cred
);
201 status
= nfs4_proc_setclientid_confirm(clp
, cred
);
203 nfs4_schedule_state_renewal(clp
);
208 nfs4_alloc_lockowner_id(struct nfs4_client
*clp
)
210 return clp
->cl_lockowner_id
++;
213 static struct nfs4_state_owner
*
214 nfs4_client_grab_unused(struct nfs4_client
*clp
, struct rpc_cred
*cred
)
216 struct nfs4_state_owner
*sp
= NULL
;
218 if (!list_empty(&clp
->cl_unused
)) {
219 sp
= list_entry(clp
->cl_unused
.next
, struct nfs4_state_owner
, so_list
);
220 atomic_inc(&sp
->so_count
);
222 list_move(&sp
->so_list
, &clp
->cl_state_owners
);
228 struct rpc_cred
*nfs4_get_renew_cred(struct nfs4_client
*clp
)
230 struct nfs4_state_owner
*sp
;
231 struct rpc_cred
*cred
= NULL
;
233 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
234 if (list_empty(&sp
->so_states
))
236 cred
= get_rpccred(sp
->so_cred
);
242 struct rpc_cred
*nfs4_get_setclientid_cred(struct nfs4_client
*clp
)
244 struct nfs4_state_owner
*sp
;
246 if (!list_empty(&clp
->cl_state_owners
)) {
247 sp
= list_entry(clp
->cl_state_owners
.next
,
248 struct nfs4_state_owner
, so_list
);
249 return get_rpccred(sp
->so_cred
);
254 static struct nfs4_state_owner
*
255 nfs4_find_state_owner(struct nfs4_client
*clp
, struct rpc_cred
*cred
)
257 struct nfs4_state_owner
*sp
, *res
= NULL
;
259 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
260 if (sp
->so_cred
!= cred
)
262 atomic_inc(&sp
->so_count
);
263 /* Move to the head of the list */
264 list_move(&sp
->so_list
, &clp
->cl_state_owners
);
272 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
273 * create a new state_owner.
276 static struct nfs4_state_owner
*
277 nfs4_alloc_state_owner(void)
279 struct nfs4_state_owner
*sp
;
281 sp
= kzalloc(sizeof(*sp
),GFP_KERNEL
);
284 spin_lock_init(&sp
->so_lock
);
285 INIT_LIST_HEAD(&sp
->so_states
);
286 INIT_LIST_HEAD(&sp
->so_delegations
);
287 rpc_init_wait_queue(&sp
->so_sequence
.wait
, "Seqid_waitqueue");
288 sp
->so_seqid
.sequence
= &sp
->so_sequence
;
289 spin_lock_init(&sp
->so_sequence
.lock
);
290 INIT_LIST_HEAD(&sp
->so_sequence
.list
);
291 atomic_set(&sp
->so_count
, 1);
296 nfs4_drop_state_owner(struct nfs4_state_owner
*sp
)
298 struct nfs4_client
*clp
= sp
->so_client
;
299 spin_lock(&clp
->cl_lock
);
300 list_del_init(&sp
->so_list
);
301 spin_unlock(&clp
->cl_lock
);
305 * Note: must be called with clp->cl_sem held in order to prevent races
306 * with reboot recovery!
308 struct nfs4_state_owner
*nfs4_get_state_owner(struct nfs_server
*server
, struct rpc_cred
*cred
)
310 struct nfs4_client
*clp
= server
->nfs4_state
;
311 struct nfs4_state_owner
*sp
, *new;
314 new = nfs4_alloc_state_owner();
315 spin_lock(&clp
->cl_lock
);
316 sp
= nfs4_find_state_owner(clp
, cred
);
318 sp
= nfs4_client_grab_unused(clp
, cred
);
319 if (sp
== NULL
&& new != NULL
) {
320 list_add(&new->so_list
, &clp
->cl_state_owners
);
321 new->so_client
= clp
;
322 new->so_id
= nfs4_alloc_lockowner_id(clp
);
327 spin_unlock(&clp
->cl_lock
);
336 * Must be called with clp->cl_sem held in order to avoid races
337 * with state recovery...
339 void nfs4_put_state_owner(struct nfs4_state_owner
*sp
)
341 struct nfs4_client
*clp
= sp
->so_client
;
342 struct rpc_cred
*cred
= sp
->so_cred
;
344 if (!atomic_dec_and_lock(&sp
->so_count
, &clp
->cl_lock
))
346 if (clp
->cl_nunused
>= OPENOWNER_POOL_SIZE
)
348 if (list_empty(&sp
->so_list
))
350 list_move(&sp
->so_list
, &clp
->cl_unused
);
352 spin_unlock(&clp
->cl_lock
);
357 list_del(&sp
->so_list
);
358 spin_unlock(&clp
->cl_lock
);
363 static struct nfs4_state
*
364 nfs4_alloc_open_state(void)
366 struct nfs4_state
*state
;
368 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
371 atomic_set(&state
->count
, 1);
372 INIT_LIST_HEAD(&state
->lock_states
);
373 spin_lock_init(&state
->state_lock
);
378 nfs4_state_set_mode_locked(struct nfs4_state
*state
, mode_t mode
)
380 if (state
->state
== mode
)
382 /* NB! List reordering - see the reclaim code for why. */
383 if ((mode
& FMODE_WRITE
) != (state
->state
& FMODE_WRITE
)) {
384 if (mode
& FMODE_WRITE
)
385 list_move(&state
->open_states
, &state
->owner
->so_states
);
387 list_move_tail(&state
->open_states
, &state
->owner
->so_states
);
390 list_del_init(&state
->inode_states
);
394 static struct nfs4_state
*
395 __nfs4_find_state_byowner(struct inode
*inode
, struct nfs4_state_owner
*owner
)
397 struct nfs_inode
*nfsi
= NFS_I(inode
);
398 struct nfs4_state
*state
;
400 list_for_each_entry(state
, &nfsi
->open_states
, inode_states
) {
401 /* Is this in the process of being freed? */
402 if (state
->state
== 0)
404 if (state
->owner
== owner
) {
405 atomic_inc(&state
->count
);
413 nfs4_free_open_state(struct nfs4_state
*state
)
419 nfs4_get_open_state(struct inode
*inode
, struct nfs4_state_owner
*owner
)
421 struct nfs4_state
*state
, *new;
422 struct nfs_inode
*nfsi
= NFS_I(inode
);
424 spin_lock(&inode
->i_lock
);
425 state
= __nfs4_find_state_byowner(inode
, owner
);
426 spin_unlock(&inode
->i_lock
);
429 new = nfs4_alloc_open_state();
430 spin_lock(&owner
->so_lock
);
431 spin_lock(&inode
->i_lock
);
432 state
= __nfs4_find_state_byowner(inode
, owner
);
433 if (state
== NULL
&& new != NULL
) {
435 state
->owner
= owner
;
436 atomic_inc(&owner
->so_count
);
437 list_add(&state
->inode_states
, &nfsi
->open_states
);
438 state
->inode
= igrab(inode
);
439 spin_unlock(&inode
->i_lock
);
440 /* Note: The reclaim code dictates that we add stateless
441 * and read-only stateids to the end of the list */
442 list_add_tail(&state
->open_states
, &owner
->so_states
);
443 spin_unlock(&owner
->so_lock
);
445 spin_unlock(&inode
->i_lock
);
446 spin_unlock(&owner
->so_lock
);
448 nfs4_free_open_state(new);
455 * Beware! Caller must be holding exactly one
456 * reference to clp->cl_sem!
458 void nfs4_put_open_state(struct nfs4_state
*state
)
460 struct inode
*inode
= state
->inode
;
461 struct nfs4_state_owner
*owner
= state
->owner
;
463 if (!atomic_dec_and_lock(&state
->count
, &owner
->so_lock
))
465 spin_lock(&inode
->i_lock
);
466 if (!list_empty(&state
->inode_states
))
467 list_del(&state
->inode_states
);
468 list_del(&state
->open_states
);
469 spin_unlock(&inode
->i_lock
);
470 spin_unlock(&owner
->so_lock
);
472 nfs4_free_open_state(state
);
473 nfs4_put_state_owner(owner
);
477 * Close the current file.
479 void nfs4_close_state(struct nfs4_state
*state
, mode_t mode
)
481 struct inode
*inode
= state
->inode
;
482 struct nfs4_state_owner
*owner
= state
->owner
;
483 int oldstate
, newstate
= 0;
485 atomic_inc(&owner
->so_count
);
486 /* Protect against nfs4_find_state() */
487 spin_lock(&owner
->so_lock
);
488 spin_lock(&inode
->i_lock
);
489 switch (mode
& (FMODE_READ
| FMODE_WRITE
)) {
496 case FMODE_READ
|FMODE_WRITE
:
499 oldstate
= newstate
= state
->state
;
500 if (state
->n_rdwr
== 0) {
501 if (state
->n_rdonly
== 0)
502 newstate
&= ~FMODE_READ
;
503 if (state
->n_wronly
== 0)
504 newstate
&= ~FMODE_WRITE
;
506 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
507 nfs4_state_set_mode_locked(state
, newstate
);
510 spin_unlock(&inode
->i_lock
);
511 spin_unlock(&owner
->so_lock
);
513 if (oldstate
!= newstate
&& nfs4_do_close(inode
, state
) == 0)
515 nfs4_put_open_state(state
);
516 nfs4_put_state_owner(owner
);
520 * Search the state->lock_states for an existing lock_owner
521 * that is compatible with current->files
523 static struct nfs4_lock_state
*
524 __nfs4_find_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
)
526 struct nfs4_lock_state
*pos
;
527 list_for_each_entry(pos
, &state
->lock_states
, ls_locks
) {
528 if (pos
->ls_owner
!= fl_owner
)
530 atomic_inc(&pos
->ls_count
);
537 * Return a compatible lock_state. If no initialized lock_state structure
538 * exists, return an uninitialized one.
541 static struct nfs4_lock_state
*nfs4_alloc_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
)
543 struct nfs4_lock_state
*lsp
;
544 struct nfs4_client
*clp
= state
->owner
->so_client
;
546 lsp
= kzalloc(sizeof(*lsp
), GFP_KERNEL
);
549 lsp
->ls_seqid
.sequence
= &state
->owner
->so_sequence
;
550 atomic_set(&lsp
->ls_count
, 1);
551 lsp
->ls_owner
= fl_owner
;
552 spin_lock(&clp
->cl_lock
);
553 lsp
->ls_id
= nfs4_alloc_lockowner_id(clp
);
554 spin_unlock(&clp
->cl_lock
);
555 INIT_LIST_HEAD(&lsp
->ls_locks
);
560 * Return a compatible lock_state. If no initialized lock_state structure
561 * exists, return an uninitialized one.
563 * The caller must be holding clp->cl_sem
565 static struct nfs4_lock_state
*nfs4_get_lock_state(struct nfs4_state
*state
, fl_owner_t owner
)
567 struct nfs4_lock_state
*lsp
, *new = NULL
;
570 spin_lock(&state
->state_lock
);
571 lsp
= __nfs4_find_lock_state(state
, owner
);
575 new->ls_state
= state
;
576 list_add(&new->ls_locks
, &state
->lock_states
);
577 set_bit(LK_STATE_IN_USE
, &state
->flags
);
582 spin_unlock(&state
->state_lock
);
583 new = nfs4_alloc_lock_state(state
, owner
);
587 spin_unlock(&state
->state_lock
);
593 * Release reference to lock_state, and free it if we see that
594 * it is no longer in use
596 void nfs4_put_lock_state(struct nfs4_lock_state
*lsp
)
598 struct nfs4_state
*state
;
602 state
= lsp
->ls_state
;
603 if (!atomic_dec_and_lock(&lsp
->ls_count
, &state
->state_lock
))
605 list_del(&lsp
->ls_locks
);
606 if (list_empty(&state
->lock_states
))
607 clear_bit(LK_STATE_IN_USE
, &state
->flags
);
608 spin_unlock(&state
->state_lock
);
612 static void nfs4_fl_copy_lock(struct file_lock
*dst
, struct file_lock
*src
)
614 struct nfs4_lock_state
*lsp
= src
->fl_u
.nfs4_fl
.owner
;
616 dst
->fl_u
.nfs4_fl
.owner
= lsp
;
617 atomic_inc(&lsp
->ls_count
);
620 static void nfs4_fl_release_lock(struct file_lock
*fl
)
622 nfs4_put_lock_state(fl
->fl_u
.nfs4_fl
.owner
);
625 static struct file_lock_operations nfs4_fl_lock_ops
= {
626 .fl_copy_lock
= nfs4_fl_copy_lock
,
627 .fl_release_private
= nfs4_fl_release_lock
,
630 int nfs4_set_lock_state(struct nfs4_state
*state
, struct file_lock
*fl
)
632 struct nfs4_lock_state
*lsp
;
634 if (fl
->fl_ops
!= NULL
)
636 lsp
= nfs4_get_lock_state(state
, fl
->fl_owner
);
639 fl
->fl_u
.nfs4_fl
.owner
= lsp
;
640 fl
->fl_ops
= &nfs4_fl_lock_ops
;
645 * Byte-range lock aware utility to initialize the stateid of read/write
648 void nfs4_copy_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
, fl_owner_t fl_owner
)
650 struct nfs4_lock_state
*lsp
;
652 memcpy(dst
, &state
->stateid
, sizeof(*dst
));
653 if (test_bit(LK_STATE_IN_USE
, &state
->flags
) == 0)
656 spin_lock(&state
->state_lock
);
657 lsp
= __nfs4_find_lock_state(state
, fl_owner
);
658 if (lsp
!= NULL
&& (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
659 memcpy(dst
, &lsp
->ls_stateid
, sizeof(*dst
));
660 spin_unlock(&state
->state_lock
);
661 nfs4_put_lock_state(lsp
);
664 struct nfs_seqid
*nfs_alloc_seqid(struct nfs_seqid_counter
*counter
)
666 struct rpc_sequence
*sequence
= counter
->sequence
;
667 struct nfs_seqid
*new;
669 new = kmalloc(sizeof(*new), GFP_KERNEL
);
671 new->sequence
= counter
;
672 spin_lock(&sequence
->lock
);
673 list_add_tail(&new->list
, &sequence
->list
);
674 spin_unlock(&sequence
->lock
);
679 void nfs_free_seqid(struct nfs_seqid
*seqid
)
681 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
683 spin_lock(&sequence
->lock
);
684 list_del(&seqid
->list
);
685 spin_unlock(&sequence
->lock
);
686 rpc_wake_up(&sequence
->wait
);
691 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
692 * failed with a seqid incrementing error -
693 * see comments nfs_fs.h:seqid_mutating_error()
695 static inline void nfs_increment_seqid(int status
, struct nfs_seqid
*seqid
)
700 case -NFS4ERR_BAD_SEQID
:
701 case -NFS4ERR_STALE_CLIENTID
:
702 case -NFS4ERR_STALE_STATEID
:
703 case -NFS4ERR_BAD_STATEID
:
704 case -NFS4ERR_BADXDR
:
705 case -NFS4ERR_RESOURCE
:
706 case -NFS4ERR_NOFILEHANDLE
:
707 /* Non-seqid mutating errors */
711 * Note: no locking needed as we are guaranteed to be first
712 * on the sequence list
714 seqid
->sequence
->counter
++;
717 void nfs_increment_open_seqid(int status
, struct nfs_seqid
*seqid
)
719 if (status
== -NFS4ERR_BAD_SEQID
) {
720 struct nfs4_state_owner
*sp
= container_of(seqid
->sequence
,
721 struct nfs4_state_owner
, so_seqid
);
722 nfs4_drop_state_owner(sp
);
724 return nfs_increment_seqid(status
, seqid
);
728 * Increment the seqid if the LOCK/LOCKU succeeded, or
729 * failed with a seqid incrementing error -
730 * see comments nfs_fs.h:seqid_mutating_error()
732 void nfs_increment_lock_seqid(int status
, struct nfs_seqid
*seqid
)
734 return nfs_increment_seqid(status
, seqid
);
737 int nfs_wait_on_sequence(struct nfs_seqid
*seqid
, struct rpc_task
*task
)
739 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
742 if (sequence
->list
.next
== &seqid
->list
)
744 spin_lock(&sequence
->lock
);
745 if (sequence
->list
.next
!= &seqid
->list
) {
746 rpc_sleep_on(&sequence
->wait
, task
, NULL
, NULL
);
749 spin_unlock(&sequence
->lock
);
754 static int reclaimer(void *);
756 static inline void nfs4_clear_recover_bit(struct nfs4_client
*clp
)
758 smp_mb__before_clear_bit();
759 clear_bit(NFS4CLNT_STATE_RECOVER
, &clp
->cl_state
);
760 smp_mb__after_clear_bit();
761 wake_up_bit(&clp
->cl_state
, NFS4CLNT_STATE_RECOVER
);
762 rpc_wake_up(&clp
->cl_rpcwaitq
);
766 * State recovery routine
768 static void nfs4_recover_state(struct nfs4_client
*clp
)
770 struct task_struct
*task
;
772 __module_get(THIS_MODULE
);
773 atomic_inc(&clp
->cl_count
);
774 task
= kthread_run(reclaimer
, clp
, "%u.%u.%u.%u-reclaim",
775 NIPQUAD(clp
->cl_addr
));
778 nfs4_clear_recover_bit(clp
);
779 nfs4_put_client(clp
);
780 module_put(THIS_MODULE
);
784 * Schedule a state recovery attempt
786 void nfs4_schedule_state_recovery(struct nfs4_client
*clp
)
790 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER
, &clp
->cl_state
) == 0)
791 nfs4_recover_state(clp
);
794 static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops
*ops
, struct nfs4_state
*state
)
796 struct inode
*inode
= state
->inode
;
797 struct file_lock
*fl
;
800 for (fl
= inode
->i_flock
; fl
!= 0; fl
= fl
->fl_next
) {
801 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
803 if (((struct nfs_open_context
*)fl
->fl_file
->private_data
)->state
!= state
)
805 status
= ops
->recover_lock(state
, fl
);
810 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
811 __FUNCTION__
, status
);
812 case -NFS4ERR_EXPIRED
:
813 case -NFS4ERR_NO_GRACE
:
814 case -NFS4ERR_RECLAIM_BAD
:
815 case -NFS4ERR_RECLAIM_CONFLICT
:
816 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
818 case -NFS4ERR_STALE_CLIENTID
:
827 static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops
*ops
, struct nfs4_state_owner
*sp
)
829 struct nfs4_state
*state
;
830 struct nfs4_lock_state
*lock
;
833 /* Note: we rely on the sp->so_states list being ordered
834 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
836 * This is needed to ensure that the server won't give us any
837 * read delegations that we have to return if, say, we are
838 * recovering after a network partition or a reboot from a
839 * server that doesn't support a grace period.
841 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
842 if (state
->state
== 0)
844 status
= ops
->recover_open(sp
, state
);
846 status
= nfs4_reclaim_locks(ops
, state
);
849 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
850 if (!(lock
->ls_flags
& NFS_LOCK_INITIALIZED
))
851 printk("%s: Lock reclaim failed!\n",
858 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
859 __FUNCTION__
, status
);
861 case -NFS4ERR_RECLAIM_BAD
:
862 case -NFS4ERR_RECLAIM_CONFLICT
:
864 * Open state on this file cannot be recovered
865 * All we can do is revert to using the zero stateid.
867 memset(state
->stateid
.data
, 0,
868 sizeof(state
->stateid
.data
));
869 /* Mark the file as being 'closed' */
872 case -NFS4ERR_EXPIRED
:
873 case -NFS4ERR_NO_GRACE
:
874 case -NFS4ERR_STALE_CLIENTID
:
883 static void nfs4_state_mark_reclaim(struct nfs4_client
*clp
)
885 struct nfs4_state_owner
*sp
;
886 struct nfs4_state
*state
;
887 struct nfs4_lock_state
*lock
;
889 /* Reset all sequence ids to zero */
890 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
891 sp
->so_seqid
.counter
= 0;
892 sp
->so_seqid
.flags
= 0;
893 spin_lock(&sp
->so_lock
);
894 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
895 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
896 lock
->ls_seqid
.counter
= 0;
897 lock
->ls_seqid
.flags
= 0;
898 lock
->ls_flags
&= ~NFS_LOCK_INITIALIZED
;
901 spin_unlock(&sp
->so_lock
);
905 static int reclaimer(void *ptr
)
907 struct nfs4_client
*clp
= ptr
;
908 struct nfs4_state_owner
*sp
;
909 struct nfs4_state_recovery_ops
*ops
;
910 struct rpc_cred
*cred
;
913 allow_signal(SIGKILL
);
915 /* Ensure exclusive access to NFSv4 state */
917 down_write(&clp
->cl_sem
);
918 /* Are there any NFS mounts out there? */
919 if (list_empty(&clp
->cl_superblocks
))
922 ops
= &nfs4_network_partition_recovery_ops
;
923 /* Are there any open files on this volume? */
924 cred
= nfs4_get_renew_cred(clp
);
926 /* Yes there are: try to renew the old lease */
927 status
= nfs4_proc_renew(clp
, cred
);
930 case -NFS4ERR_CB_PATH_DOWN
:
933 case -NFS4ERR_STALE_CLIENTID
:
934 case -NFS4ERR_LEASE_MOVED
:
935 ops
= &nfs4_reboot_recovery_ops
;
938 /* "reboot" to ensure we clear all state on the server */
939 clp
->cl_boot_time
= CURRENT_TIME
;
940 cred
= nfs4_get_setclientid_cred(clp
);
942 /* We're going to have to re-establish a clientid */
943 nfs4_state_mark_reclaim(clp
);
946 status
= nfs4_init_client(clp
, cred
);
951 /* Mark all delegations for reclaim */
952 nfs_delegation_mark_reclaim(clp
);
953 /* Note: list is protected by exclusive lock on cl->cl_sem */
954 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
955 status
= nfs4_reclaim_open_state(ops
, sp
);
957 if (status
== -NFS4ERR_NO_GRACE
) {
958 ops
= &nfs4_network_partition_recovery_ops
;
959 status
= nfs4_reclaim_open_state(ops
, sp
);
961 if (status
== -NFS4ERR_STALE_CLIENTID
)
963 if (status
== -NFS4ERR_EXPIRED
)
967 nfs_delegation_reap_unclaimed(clp
);
969 up_write(&clp
->cl_sem
);
971 if (status
== -NFS4ERR_CB_PATH_DOWN
)
972 nfs_handle_cb_pathdown(clp
);
973 nfs4_clear_recover_bit(clp
);
974 nfs4_put_client(clp
);
975 module_put_and_exit(0);
978 printk(KERN_WARNING
"Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
979 NIPQUAD(clp
->cl_addr
.s_addr
), -status
);