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/workqueue.h>
47 #include <linux/bitops.h>
51 #include "delegation.h"
53 #define OPENOWNER_POOL_SIZE 8
55 const nfs4_stateid zero_stateid
;
57 static DEFINE_SPINLOCK(state_spinlock
);
58 static LIST_HEAD(nfs4_clientid_list
);
60 static void nfs4_recover_state(void *);
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 if (server
->mnt_path
) {
73 kfree(server
->mnt_path
);
74 server
->mnt_path
= NULL
;
76 if (server
->nfs4_state
) {
77 nfs4_put_client(server
->nfs4_state
);
78 server
->nfs4_state
= NULL
;
83 * nfs4_get_client(): returns an empty client structure
84 * nfs4_put_client(): drops reference to client structure
86 * Since these are allocated/deallocated very rarely, we don't
87 * bother putting them in a slab cache...
89 static struct nfs4_client
*
90 nfs4_alloc_client(struct in_addr
*addr
)
92 struct nfs4_client
*clp
;
94 if (nfs_callback_up() < 0)
96 if ((clp
= kmalloc(sizeof(*clp
), GFP_KERNEL
)) == NULL
) {
100 memset(clp
, 0, sizeof(*clp
));
101 memcpy(&clp
->cl_addr
, addr
, sizeof(clp
->cl_addr
));
102 init_rwsem(&clp
->cl_sem
);
103 INIT_LIST_HEAD(&clp
->cl_delegations
);
104 INIT_LIST_HEAD(&clp
->cl_state_owners
);
105 INIT_LIST_HEAD(&clp
->cl_unused
);
106 spin_lock_init(&clp
->cl_lock
);
107 atomic_set(&clp
->cl_count
, 1);
108 INIT_WORK(&clp
->cl_recoverd
, nfs4_recover_state
, clp
);
109 INIT_WORK(&clp
->cl_renewd
, nfs4_renew_state
, clp
);
110 INIT_LIST_HEAD(&clp
->cl_superblocks
);
111 init_waitqueue_head(&clp
->cl_waitq
);
112 rpc_init_wait_queue(&clp
->cl_rpcwaitq
, "NFS4 client");
113 clp
->cl_rpcclient
= ERR_PTR(-EINVAL
);
114 clp
->cl_boot_time
= CURRENT_TIME
;
115 clp
->cl_state
= 1 << NFS4CLNT_OK
;
120 nfs4_free_client(struct nfs4_client
*clp
)
122 struct nfs4_state_owner
*sp
;
124 while (!list_empty(&clp
->cl_unused
)) {
125 sp
= list_entry(clp
->cl_unused
.next
,
126 struct nfs4_state_owner
,
128 list_del(&sp
->so_list
);
131 BUG_ON(!list_empty(&clp
->cl_state_owners
));
133 put_rpccred(clp
->cl_cred
);
134 nfs_idmap_delete(clp
);
135 if (!IS_ERR(clp
->cl_rpcclient
))
136 rpc_shutdown_client(clp
->cl_rpcclient
);
141 static struct nfs4_client
*__nfs4_find_client(struct in_addr
*addr
)
143 struct nfs4_client
*clp
;
144 list_for_each_entry(clp
, &nfs4_clientid_list
, cl_servers
) {
145 if (memcmp(&clp
->cl_addr
, addr
, sizeof(clp
->cl_addr
)) == 0) {
146 atomic_inc(&clp
->cl_count
);
153 struct nfs4_client
*nfs4_find_client(struct in_addr
*addr
)
155 struct nfs4_client
*clp
;
156 spin_lock(&state_spinlock
);
157 clp
= __nfs4_find_client(addr
);
158 spin_unlock(&state_spinlock
);
163 nfs4_get_client(struct in_addr
*addr
)
165 struct nfs4_client
*clp
, *new = NULL
;
167 spin_lock(&state_spinlock
);
169 clp
= __nfs4_find_client(addr
);
174 list_add(&clp
->cl_servers
, &nfs4_clientid_list
);
178 spin_unlock(&state_spinlock
);
179 new = nfs4_alloc_client(addr
);
180 spin_lock(&state_spinlock
);
184 spin_unlock(&state_spinlock
);
186 nfs4_free_client(new);
191 nfs4_put_client(struct nfs4_client
*clp
)
193 if (!atomic_dec_and_lock(&clp
->cl_count
, &state_spinlock
))
195 list_del(&clp
->cl_servers
);
196 spin_unlock(&state_spinlock
);
197 BUG_ON(!list_empty(&clp
->cl_superblocks
));
198 wake_up_all(&clp
->cl_waitq
);
199 rpc_wake_up(&clp
->cl_rpcwaitq
);
200 nfs4_kill_renewd(clp
);
201 nfs4_free_client(clp
);
204 static int __nfs4_init_client(struct nfs4_client
*clp
)
206 int status
= nfs4_proc_setclientid(clp
, NFS4_CALLBACK
, nfs_callback_tcpport
);
208 status
= nfs4_proc_setclientid_confirm(clp
);
210 nfs4_schedule_state_renewal(clp
);
214 int nfs4_init_client(struct nfs4_client
*clp
)
216 return nfs4_map_errors(__nfs4_init_client(clp
));
220 nfs4_alloc_lockowner_id(struct nfs4_client
*clp
)
222 return clp
->cl_lockowner_id
++;
225 static struct nfs4_state_owner
*
226 nfs4_client_grab_unused(struct nfs4_client
*clp
, struct rpc_cred
*cred
)
228 struct nfs4_state_owner
*sp
= NULL
;
230 if (!list_empty(&clp
->cl_unused
)) {
231 sp
= list_entry(clp
->cl_unused
.next
, struct nfs4_state_owner
, so_list
);
232 atomic_inc(&sp
->so_count
);
234 list_move(&sp
->so_list
, &clp
->cl_state_owners
);
240 static struct nfs4_state_owner
*
241 nfs4_find_state_owner(struct nfs4_client
*clp
, struct rpc_cred
*cred
)
243 struct nfs4_state_owner
*sp
, *res
= NULL
;
245 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
246 if (sp
->so_cred
!= cred
)
248 atomic_inc(&sp
->so_count
);
249 /* Move to the head of the list */
250 list_move(&sp
->so_list
, &clp
->cl_state_owners
);
258 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
259 * create a new state_owner.
262 static struct nfs4_state_owner
*
263 nfs4_alloc_state_owner(void)
265 struct nfs4_state_owner
*sp
;
267 sp
= kzalloc(sizeof(*sp
),GFP_KERNEL
);
270 spin_lock_init(&sp
->so_lock
);
271 INIT_LIST_HEAD(&sp
->so_states
);
272 INIT_LIST_HEAD(&sp
->so_delegations
);
273 rpc_init_wait_queue(&sp
->so_sequence
.wait
, "Seqid_waitqueue");
274 sp
->so_seqid
.sequence
= &sp
->so_sequence
;
275 spin_lock_init(&sp
->so_sequence
.lock
);
276 INIT_LIST_HEAD(&sp
->so_sequence
.list
);
277 atomic_set(&sp
->so_count
, 1);
282 nfs4_drop_state_owner(struct nfs4_state_owner
*sp
)
284 struct nfs4_client
*clp
= sp
->so_client
;
285 spin_lock(&clp
->cl_lock
);
286 list_del_init(&sp
->so_list
);
287 spin_unlock(&clp
->cl_lock
);
291 * Note: must be called with clp->cl_sem held in order to prevent races
292 * with reboot recovery!
294 struct nfs4_state_owner
*nfs4_get_state_owner(struct nfs_server
*server
, struct rpc_cred
*cred
)
296 struct nfs4_client
*clp
= server
->nfs4_state
;
297 struct nfs4_state_owner
*sp
, *new;
300 new = nfs4_alloc_state_owner();
301 spin_lock(&clp
->cl_lock
);
302 sp
= nfs4_find_state_owner(clp
, cred
);
304 sp
= nfs4_client_grab_unused(clp
, cred
);
305 if (sp
== NULL
&& new != NULL
) {
306 list_add(&new->so_list
, &clp
->cl_state_owners
);
307 new->so_client
= clp
;
308 new->so_id
= nfs4_alloc_lockowner_id(clp
);
313 spin_unlock(&clp
->cl_lock
);
323 * Must be called with clp->cl_sem held in order to avoid races
324 * with state recovery...
326 void nfs4_put_state_owner(struct nfs4_state_owner
*sp
)
328 struct nfs4_client
*clp
= sp
->so_client
;
329 struct rpc_cred
*cred
= sp
->so_cred
;
331 if (!atomic_dec_and_lock(&sp
->so_count
, &clp
->cl_lock
))
333 if (clp
->cl_nunused
>= OPENOWNER_POOL_SIZE
)
335 if (list_empty(&sp
->so_list
))
337 list_move(&sp
->so_list
, &clp
->cl_unused
);
339 spin_unlock(&clp
->cl_lock
);
344 list_del(&sp
->so_list
);
345 spin_unlock(&clp
->cl_lock
);
350 static struct nfs4_state
*
351 nfs4_alloc_open_state(void)
353 struct nfs4_state
*state
;
355 state
= kmalloc(sizeof(*state
), GFP_KERNEL
);
362 memset(state
->stateid
.data
, 0, sizeof(state
->stateid
.data
));
363 atomic_set(&state
->count
, 1);
364 INIT_LIST_HEAD(&state
->lock_states
);
365 spin_lock_init(&state
->state_lock
);
369 static struct nfs4_state
*
370 __nfs4_find_state(struct inode
*inode
, struct rpc_cred
*cred
, mode_t mode
)
372 struct nfs_inode
*nfsi
= NFS_I(inode
);
373 struct nfs4_state
*state
;
375 mode
&= (FMODE_READ
|FMODE_WRITE
);
376 list_for_each_entry(state
, &nfsi
->open_states
, inode_states
) {
377 if (state
->owner
->so_cred
!= cred
)
379 if ((mode
& FMODE_READ
) != 0 && state
->nreaders
== 0)
381 if ((mode
& FMODE_WRITE
) != 0 && state
->nwriters
== 0)
383 if ((state
->state
& mode
) != mode
)
385 atomic_inc(&state
->count
);
386 if (mode
& FMODE_READ
)
388 if (mode
& FMODE_WRITE
)
395 static struct nfs4_state
*
396 __nfs4_find_state_byowner(struct inode
*inode
, struct nfs4_state_owner
*owner
)
398 struct nfs_inode
*nfsi
= NFS_I(inode
);
399 struct nfs4_state
*state
;
401 list_for_each_entry(state
, &nfsi
->open_states
, inode_states
) {
402 /* Is this in the process of being freed? */
403 if (state
->nreaders
== 0 && state
->nwriters
== 0)
405 if (state
->owner
== owner
) {
406 atomic_inc(&state
->count
);
414 nfs4_find_state(struct inode
*inode
, struct rpc_cred
*cred
, mode_t mode
)
416 struct nfs4_state
*state
;
418 spin_lock(&inode
->i_lock
);
419 state
= __nfs4_find_state(inode
, cred
, mode
);
420 spin_unlock(&inode
->i_lock
);
425 nfs4_free_open_state(struct nfs4_state
*state
)
431 nfs4_get_open_state(struct inode
*inode
, struct nfs4_state_owner
*owner
)
433 struct nfs4_state
*state
, *new;
434 struct nfs_inode
*nfsi
= NFS_I(inode
);
436 spin_lock(&inode
->i_lock
);
437 state
= __nfs4_find_state_byowner(inode
, owner
);
438 spin_unlock(&inode
->i_lock
);
441 new = nfs4_alloc_open_state();
442 spin_lock(&owner
->so_lock
);
443 spin_lock(&inode
->i_lock
);
444 state
= __nfs4_find_state_byowner(inode
, owner
);
445 if (state
== NULL
&& new != NULL
) {
447 state
->owner
= owner
;
448 atomic_inc(&owner
->so_count
);
449 list_add(&state
->inode_states
, &nfsi
->open_states
);
450 state
->inode
= igrab(inode
);
451 spin_unlock(&inode
->i_lock
);
452 /* Note: The reclaim code dictates that we add stateless
453 * and read-only stateids to the end of the list */
454 list_add_tail(&state
->open_states
, &owner
->so_states
);
455 spin_unlock(&owner
->so_lock
);
457 spin_unlock(&inode
->i_lock
);
458 spin_unlock(&owner
->so_lock
);
460 nfs4_free_open_state(new);
467 * Beware! Caller must be holding exactly one
468 * reference to clp->cl_sem!
470 void nfs4_put_open_state(struct nfs4_state
*state
)
472 struct inode
*inode
= state
->inode
;
473 struct nfs4_state_owner
*owner
= state
->owner
;
475 if (!atomic_dec_and_lock(&state
->count
, &owner
->so_lock
))
477 spin_lock(&inode
->i_lock
);
478 if (!list_empty(&state
->inode_states
))
479 list_del(&state
->inode_states
);
480 list_del(&state
->open_states
);
481 spin_unlock(&inode
->i_lock
);
482 spin_unlock(&owner
->so_lock
);
484 BUG_ON (state
->state
!= 0);
485 nfs4_free_open_state(state
);
486 nfs4_put_state_owner(owner
);
490 * Close the current file.
492 void nfs4_close_state(struct nfs4_state
*state
, mode_t mode
)
494 struct inode
*inode
= state
->inode
;
495 struct nfs4_state_owner
*owner
= state
->owner
;
498 atomic_inc(&owner
->so_count
);
499 /* Protect against nfs4_find_state() */
500 spin_lock(&owner
->so_lock
);
501 spin_lock(&inode
->i_lock
);
502 if (mode
& FMODE_READ
)
504 if (mode
& FMODE_WRITE
)
506 if (state
->nwriters
== 0) {
507 if (state
->nreaders
== 0)
508 list_del_init(&state
->inode_states
);
509 /* See reclaim code */
510 list_move_tail(&state
->open_states
, &owner
->so_states
);
512 spin_unlock(&inode
->i_lock
);
513 spin_unlock(&owner
->so_lock
);
515 if (state
->state
!= 0) {
517 newstate
|= FMODE_READ
;
519 newstate
|= FMODE_WRITE
;
520 if (state
->state
== newstate
)
522 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
523 state
->state
= newstate
;
526 if (nfs4_do_close(inode
, state
, newstate
) == 0)
530 nfs4_put_open_state(state
);
531 nfs4_put_state_owner(owner
);
535 * Search the state->lock_states for an existing lock_owner
536 * that is compatible with current->files
538 static struct nfs4_lock_state
*
539 __nfs4_find_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
)
541 struct nfs4_lock_state
*pos
;
542 list_for_each_entry(pos
, &state
->lock_states
, ls_locks
) {
543 if (pos
->ls_owner
!= fl_owner
)
545 atomic_inc(&pos
->ls_count
);
552 * Return a compatible lock_state. If no initialized lock_state structure
553 * exists, return an uninitialized one.
556 static struct nfs4_lock_state
*nfs4_alloc_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
)
558 struct nfs4_lock_state
*lsp
;
559 struct nfs4_client
*clp
= state
->owner
->so_client
;
561 lsp
= kzalloc(sizeof(*lsp
), GFP_KERNEL
);
564 lsp
->ls_seqid
.sequence
= &state
->owner
->so_sequence
;
565 atomic_set(&lsp
->ls_count
, 1);
566 lsp
->ls_owner
= fl_owner
;
567 spin_lock(&clp
->cl_lock
);
568 lsp
->ls_id
= nfs4_alloc_lockowner_id(clp
);
569 spin_unlock(&clp
->cl_lock
);
570 INIT_LIST_HEAD(&lsp
->ls_locks
);
575 * Return a compatible lock_state. If no initialized lock_state structure
576 * exists, return an uninitialized one.
578 * The caller must be holding clp->cl_sem
580 static struct nfs4_lock_state
*nfs4_get_lock_state(struct nfs4_state
*state
, fl_owner_t owner
)
582 struct nfs4_lock_state
*lsp
, *new = NULL
;
585 spin_lock(&state
->state_lock
);
586 lsp
= __nfs4_find_lock_state(state
, owner
);
590 new->ls_state
= state
;
591 list_add(&new->ls_locks
, &state
->lock_states
);
592 set_bit(LK_STATE_IN_USE
, &state
->flags
);
597 spin_unlock(&state
->state_lock
);
598 new = nfs4_alloc_lock_state(state
, owner
);
602 spin_unlock(&state
->state_lock
);
608 * Release reference to lock_state, and free it if we see that
609 * it is no longer in use
611 void nfs4_put_lock_state(struct nfs4_lock_state
*lsp
)
613 struct nfs4_state
*state
;
617 state
= lsp
->ls_state
;
618 if (!atomic_dec_and_lock(&lsp
->ls_count
, &state
->state_lock
))
620 list_del(&lsp
->ls_locks
);
621 if (list_empty(&state
->lock_states
))
622 clear_bit(LK_STATE_IN_USE
, &state
->flags
);
623 spin_unlock(&state
->state_lock
);
627 static void nfs4_fl_copy_lock(struct file_lock
*dst
, struct file_lock
*src
)
629 struct nfs4_lock_state
*lsp
= src
->fl_u
.nfs4_fl
.owner
;
631 dst
->fl_u
.nfs4_fl
.owner
= lsp
;
632 atomic_inc(&lsp
->ls_count
);
635 static void nfs4_fl_release_lock(struct file_lock
*fl
)
637 nfs4_put_lock_state(fl
->fl_u
.nfs4_fl
.owner
);
640 static struct file_lock_operations nfs4_fl_lock_ops
= {
641 .fl_copy_lock
= nfs4_fl_copy_lock
,
642 .fl_release_private
= nfs4_fl_release_lock
,
645 int nfs4_set_lock_state(struct nfs4_state
*state
, struct file_lock
*fl
)
647 struct nfs4_lock_state
*lsp
;
649 if (fl
->fl_ops
!= NULL
)
651 lsp
= nfs4_get_lock_state(state
, fl
->fl_owner
);
654 fl
->fl_u
.nfs4_fl
.owner
= lsp
;
655 fl
->fl_ops
= &nfs4_fl_lock_ops
;
660 * Byte-range lock aware utility to initialize the stateid of read/write
663 void nfs4_copy_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
, fl_owner_t fl_owner
)
665 struct nfs4_lock_state
*lsp
;
667 memcpy(dst
, &state
->stateid
, sizeof(*dst
));
668 if (test_bit(LK_STATE_IN_USE
, &state
->flags
) == 0)
671 spin_lock(&state
->state_lock
);
672 lsp
= __nfs4_find_lock_state(state
, fl_owner
);
673 if (lsp
!= NULL
&& (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
674 memcpy(dst
, &lsp
->ls_stateid
, sizeof(*dst
));
675 spin_unlock(&state
->state_lock
);
676 nfs4_put_lock_state(lsp
);
679 struct nfs_seqid
*nfs_alloc_seqid(struct nfs_seqid_counter
*counter
)
681 struct nfs_seqid
*new;
683 new = kmalloc(sizeof(*new), GFP_KERNEL
);
685 new->sequence
= counter
;
686 INIT_LIST_HEAD(&new->list
);
691 void nfs_free_seqid(struct nfs_seqid
*seqid
)
693 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
695 if (!list_empty(&seqid
->list
)) {
696 spin_lock(&sequence
->lock
);
697 list_del(&seqid
->list
);
698 spin_unlock(&sequence
->lock
);
700 rpc_wake_up_next(&sequence
->wait
);
705 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
706 * failed with a seqid incrementing error -
707 * see comments nfs_fs.h:seqid_mutating_error()
709 static inline void nfs_increment_seqid(int status
, struct nfs_seqid
*seqid
)
714 case -NFS4ERR_BAD_SEQID
:
715 case -NFS4ERR_STALE_CLIENTID
:
716 case -NFS4ERR_STALE_STATEID
:
717 case -NFS4ERR_BAD_STATEID
:
718 case -NFS4ERR_BADXDR
:
719 case -NFS4ERR_RESOURCE
:
720 case -NFS4ERR_NOFILEHANDLE
:
721 /* Non-seqid mutating errors */
725 * Note: no locking needed as we are guaranteed to be first
726 * on the sequence list
728 seqid
->sequence
->counter
++;
731 void nfs_increment_open_seqid(int status
, struct nfs_seqid
*seqid
)
733 if (status
== -NFS4ERR_BAD_SEQID
) {
734 struct nfs4_state_owner
*sp
= container_of(seqid
->sequence
,
735 struct nfs4_state_owner
, so_seqid
);
736 nfs4_drop_state_owner(sp
);
738 return nfs_increment_seqid(status
, seqid
);
742 * Increment the seqid if the LOCK/LOCKU succeeded, or
743 * failed with a seqid incrementing error -
744 * see comments nfs_fs.h:seqid_mutating_error()
746 void nfs_increment_lock_seqid(int status
, struct nfs_seqid
*seqid
)
748 return nfs_increment_seqid(status
, seqid
);
751 int nfs_wait_on_sequence(struct nfs_seqid
*seqid
, struct rpc_task
*task
)
753 struct rpc_sequence
*sequence
= seqid
->sequence
->sequence
;
756 if (sequence
->list
.next
== &seqid
->list
)
758 spin_lock(&sequence
->lock
);
759 if (!list_empty(&sequence
->list
)) {
760 rpc_sleep_on(&sequence
->wait
, task
, NULL
, NULL
);
763 list_add(&seqid
->list
, &sequence
->list
);
764 spin_unlock(&sequence
->lock
);
769 static int reclaimer(void *);
770 struct reclaimer_args
{
771 struct nfs4_client
*clp
;
772 struct completion complete
;
776 * State recovery routine
779 nfs4_recover_state(void *data
)
781 struct nfs4_client
*clp
= (struct nfs4_client
*)data
;
782 struct reclaimer_args args
= {
787 init_completion(&args
.complete
);
789 if (kernel_thread(reclaimer
, &args
, CLONE_KERNEL
) < 0)
790 goto out_failed_clear
;
791 wait_for_completion(&args
.complete
);
794 set_bit(NFS4CLNT_OK
, &clp
->cl_state
);
795 wake_up_all(&clp
->cl_waitq
);
796 rpc_wake_up(&clp
->cl_rpcwaitq
);
800 * Schedule a state recovery attempt
803 nfs4_schedule_state_recovery(struct nfs4_client
*clp
)
807 if (test_and_clear_bit(NFS4CLNT_OK
, &clp
->cl_state
))
808 schedule_work(&clp
->cl_recoverd
);
811 static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops
*ops
, struct nfs4_state
*state
)
813 struct inode
*inode
= state
->inode
;
814 struct file_lock
*fl
;
817 for (fl
= inode
->i_flock
; fl
!= 0; fl
= fl
->fl_next
) {
818 if (!(fl
->fl_flags
& FL_POSIX
))
820 if (((struct nfs_open_context
*)fl
->fl_file
->private_data
)->state
!= state
)
822 status
= ops
->recover_lock(state
, fl
);
827 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
828 __FUNCTION__
, status
);
829 case -NFS4ERR_EXPIRED
:
830 case -NFS4ERR_NO_GRACE
:
831 case -NFS4ERR_RECLAIM_BAD
:
832 case -NFS4ERR_RECLAIM_CONFLICT
:
833 /* kill_proc(fl->fl_owner, SIGLOST, 1); */
835 case -NFS4ERR_STALE_CLIENTID
:
844 static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops
*ops
, struct nfs4_state_owner
*sp
)
846 struct nfs4_state
*state
;
847 struct nfs4_lock_state
*lock
;
850 /* Note: we rely on the sp->so_states list being ordered
851 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
853 * This is needed to ensure that the server won't give us any
854 * read delegations that we have to return if, say, we are
855 * recovering after a network partition or a reboot from a
856 * server that doesn't support a grace period.
858 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
859 if (state
->state
== 0)
861 status
= ops
->recover_open(sp
, state
);
863 status
= nfs4_reclaim_locks(ops
, state
);
866 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
867 if (!(lock
->ls_flags
& NFS_LOCK_INITIALIZED
))
868 printk("%s: Lock reclaim failed!\n",
875 printk(KERN_ERR
"%s: unhandled error %d. Zeroing state\n",
876 __FUNCTION__
, status
);
878 case -NFS4ERR_RECLAIM_BAD
:
879 case -NFS4ERR_RECLAIM_CONFLICT
:
881 * Open state on this file cannot be recovered
882 * All we can do is revert to using the zero stateid.
884 memset(state
->stateid
.data
, 0,
885 sizeof(state
->stateid
.data
));
886 /* Mark the file as being 'closed' */
889 case -NFS4ERR_EXPIRED
:
890 case -NFS4ERR_NO_GRACE
:
891 case -NFS4ERR_STALE_CLIENTID
:
900 static void nfs4_state_mark_reclaim(struct nfs4_client
*clp
)
902 struct nfs4_state_owner
*sp
;
903 struct nfs4_state
*state
;
904 struct nfs4_lock_state
*lock
;
906 /* Reset all sequence ids to zero */
907 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
908 sp
->so_seqid
.counter
= 0;
909 sp
->so_seqid
.flags
= 0;
910 spin_lock(&sp
->so_lock
);
911 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
912 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
913 lock
->ls_seqid
.counter
= 0;
914 lock
->ls_seqid
.flags
= 0;
915 lock
->ls_flags
&= ~NFS_LOCK_INITIALIZED
;
918 spin_unlock(&sp
->so_lock
);
922 static int reclaimer(void *ptr
)
924 struct reclaimer_args
*args
= (struct reclaimer_args
*)ptr
;
925 struct nfs4_client
*clp
= args
->clp
;
926 struct nfs4_state_owner
*sp
;
927 struct nfs4_state_recovery_ops
*ops
;
930 daemonize("%u.%u.%u.%u-reclaim", NIPQUAD(clp
->cl_addr
));
931 allow_signal(SIGKILL
);
933 atomic_inc(&clp
->cl_count
);
934 complete(&args
->complete
);
936 /* Ensure exclusive access to NFSv4 state */
938 down_write(&clp
->cl_sem
);
939 /* Are there any NFS mounts out there? */
940 if (list_empty(&clp
->cl_superblocks
))
943 status
= nfs4_proc_renew(clp
);
946 case -NFS4ERR_CB_PATH_DOWN
:
948 case -NFS4ERR_STALE_CLIENTID
:
949 case -NFS4ERR_LEASE_MOVED
:
950 ops
= &nfs4_reboot_recovery_ops
;
953 ops
= &nfs4_network_partition_recovery_ops
;
955 nfs4_state_mark_reclaim(clp
);
956 status
= __nfs4_init_client(clp
);
959 /* Mark all delegations for reclaim */
960 nfs_delegation_mark_reclaim(clp
);
961 /* Note: list is protected by exclusive lock on cl->cl_sem */
962 list_for_each_entry(sp
, &clp
->cl_state_owners
, so_list
) {
963 status
= nfs4_reclaim_open_state(ops
, sp
);
965 if (status
== -NFS4ERR_NO_GRACE
) {
966 ops
= &nfs4_network_partition_recovery_ops
;
967 status
= nfs4_reclaim_open_state(ops
, sp
);
969 if (status
== -NFS4ERR_STALE_CLIENTID
)
971 if (status
== -NFS4ERR_EXPIRED
)
975 nfs_delegation_reap_unclaimed(clp
);
977 set_bit(NFS4CLNT_OK
, &clp
->cl_state
);
978 up_write(&clp
->cl_sem
);
980 wake_up_all(&clp
->cl_waitq
);
981 rpc_wake_up(&clp
->cl_rpcwaitq
);
982 if (status
== -NFS4ERR_CB_PATH_DOWN
)
983 nfs_handle_cb_pathdown(clp
);
984 nfs4_put_client(clp
);
987 printk(KERN_WARNING
"Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
988 NIPQUAD(clp
->cl_addr
.s_addr
), -status
);