4 * Client-side XDR for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
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>
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/random.h>
49 #include <linux/workqueue.h>
50 #include <linux/bitops.h>
54 #include "delegation.h"
57 #define OPENOWNER_POOL_SIZE 8
59 const nfs4_stateid zero_stateid;
61 static LIST_HEAD(nfs4_clientid_list);
63 static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
65 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
66 nfs_callback_tcpport, cred);
68 status = nfs4_proc_setclientid_confirm(clp, cred);
70 nfs4_schedule_state_renewal(clp);
74 struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
76 struct nfs4_state_owner *sp;
78 struct rpc_cred *cred = NULL;
80 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
81 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
82 if (list_empty(&sp->so_states))
84 cred = get_rpccred(sp->so_cred);
90 static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
92 struct nfs4_state_owner *sp;
95 pos = rb_first(&clp->cl_state_owners);
97 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
98 return get_rpccred(sp->so_cred);
103 static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
104 __u64 minval, int maxbits)
106 struct rb_node **p, *parent;
107 struct nfs_unique_id *pos;
111 mask = (1ULL << maxbits) - 1ULL;
113 /* Ensure distribution is more or less flat */
114 get_random_bytes(&new->id, sizeof(new->id));
116 if (new->id < minval)
124 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
126 if (new->id < pos->id)
128 else if (new->id > pos->id)
133 rb_link_node(&new->rb_node, parent, p);
134 rb_insert_color(&new->rb_node, root);
139 if (new->id < minval || (new->id & mask) != new->id) {
143 parent = rb_next(parent);
146 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
147 if (new->id < pos->id)
153 static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
155 rb_erase(&id->rb_node, root);
158 static struct nfs4_state_owner *
159 nfs4_find_state_owner(struct nfs_client *clp, struct rpc_cred *cred)
161 struct rb_node **p = &clp->cl_state_owners.rb_node,
163 struct nfs4_state_owner *sp, *res = NULL;
167 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
169 if (cred < sp->so_cred)
170 p = &parent->rb_left;
171 else if (cred > sp->so_cred)
172 p = &parent->rb_right;
174 atomic_inc(&sp->so_count);
182 static struct nfs4_state_owner *
183 nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
185 struct rb_node **p = &clp->cl_state_owners.rb_node,
187 struct nfs4_state_owner *sp;
191 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
193 if (new->so_cred < sp->so_cred)
194 p = &parent->rb_left;
195 else if (new->so_cred > sp->so_cred)
196 p = &parent->rb_right;
198 atomic_inc(&sp->so_count);
202 nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
203 rb_link_node(&new->so_client_node, parent, p);
204 rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
209 nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
211 if (!RB_EMPTY_NODE(&sp->so_client_node))
212 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
213 nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
217 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
218 * create a new state_owner.
221 static struct nfs4_state_owner *
222 nfs4_alloc_state_owner(void)
224 struct nfs4_state_owner *sp;
226 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
229 spin_lock_init(&sp->so_lock);
230 INIT_LIST_HEAD(&sp->so_states);
231 INIT_LIST_HEAD(&sp->so_delegations);
232 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
233 sp->so_seqid.sequence = &sp->so_sequence;
234 spin_lock_init(&sp->so_sequence.lock);
235 INIT_LIST_HEAD(&sp->so_sequence.list);
236 atomic_set(&sp->so_count, 1);
241 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
243 if (!RB_EMPTY_NODE(&sp->so_client_node)) {
244 struct nfs_client *clp = sp->so_client;
246 spin_lock(&clp->cl_lock);
247 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
248 RB_CLEAR_NODE(&sp->so_client_node);
249 spin_unlock(&clp->cl_lock);
254 * Note: must be called with clp->cl_sem held in order to prevent races
255 * with reboot recovery!
257 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
259 struct nfs_client *clp = server->nfs_client;
260 struct nfs4_state_owner *sp, *new;
262 spin_lock(&clp->cl_lock);
263 sp = nfs4_find_state_owner(clp, cred);
264 spin_unlock(&clp->cl_lock);
267 new = nfs4_alloc_state_owner();
270 new->so_client = clp;
272 spin_lock(&clp->cl_lock);
273 sp = nfs4_insert_state_owner(clp, new);
274 spin_unlock(&clp->cl_lock);
283 * Must be called with clp->cl_sem held in order to avoid races
284 * with state recovery...
286 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
288 struct nfs_client *clp = sp->so_client;
289 struct rpc_cred *cred = sp->so_cred;
291 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
293 nfs4_remove_state_owner(clp, sp);
294 spin_unlock(&clp->cl_lock);
299 static struct nfs4_state *
300 nfs4_alloc_open_state(void)
302 struct nfs4_state *state;
304 state = kzalloc(sizeof(*state), GFP_KERNEL);
307 atomic_set(&state->count, 1);
308 INIT_LIST_HEAD(&state->lock_states);
309 spin_lock_init(&state->state_lock);
314 nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
316 if (state->state == mode)
318 /* NB! List reordering - see the reclaim code for why. */
319 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
320 if (mode & FMODE_WRITE)
321 list_move(&state->open_states, &state->owner->so_states);
323 list_move_tail(&state->open_states, &state->owner->so_states);
326 list_del_init(&state->inode_states);
330 static struct nfs4_state *
331 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
333 struct nfs_inode *nfsi = NFS_I(inode);
334 struct nfs4_state *state;
336 list_for_each_entry(state, &nfsi->open_states, inode_states) {
337 if (state->owner != owner)
339 if (atomic_inc_not_zero(&state->count))
346 nfs4_free_open_state(struct nfs4_state *state)
352 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
354 struct nfs4_state *state, *new;
355 struct nfs_inode *nfsi = NFS_I(inode);
357 spin_lock(&inode->i_lock);
358 state = __nfs4_find_state_byowner(inode, owner);
359 spin_unlock(&inode->i_lock);
362 new = nfs4_alloc_open_state();
363 spin_lock(&owner->so_lock);
364 spin_lock(&inode->i_lock);
365 state = __nfs4_find_state_byowner(inode, owner);
366 if (state == NULL && new != NULL) {
368 state->owner = owner;
369 atomic_inc(&owner->so_count);
370 list_add(&state->inode_states, &nfsi->open_states);
371 state->inode = igrab(inode);
372 spin_unlock(&inode->i_lock);
373 /* Note: The reclaim code dictates that we add stateless
374 * and read-only stateids to the end of the list */
375 list_add_tail(&state->open_states, &owner->so_states);
376 spin_unlock(&owner->so_lock);
378 spin_unlock(&inode->i_lock);
379 spin_unlock(&owner->so_lock);
381 nfs4_free_open_state(new);
388 * Beware! Caller must be holding exactly one
389 * reference to clp->cl_sem!
391 void nfs4_put_open_state(struct nfs4_state *state)
393 struct inode *inode = state->inode;
394 struct nfs4_state_owner *owner = state->owner;
396 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
398 spin_lock(&inode->i_lock);
399 if (!list_empty(&state->inode_states))
400 list_del(&state->inode_states);
401 list_del(&state->open_states);
402 spin_unlock(&inode->i_lock);
403 spin_unlock(&owner->so_lock);
405 nfs4_free_open_state(state);
406 nfs4_put_state_owner(owner);
410 * Close the current file.
412 void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
414 struct inode *inode = state->inode;
415 struct nfs4_state_owner *owner = state->owner;
419 atomic_inc(&owner->so_count);
420 /* Protect against nfs4_find_state() */
421 spin_lock(&owner->so_lock);
422 spin_lock(&inode->i_lock);
423 switch (mode & (FMODE_READ | FMODE_WRITE)) {
430 case FMODE_READ|FMODE_WRITE:
433 newstate = FMODE_READ|FMODE_WRITE;
434 if (state->n_rdwr == 0) {
435 if (state->n_rdonly == 0) {
436 newstate &= ~FMODE_READ;
437 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
438 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
440 if (state->n_wronly == 0) {
441 newstate &= ~FMODE_WRITE;
442 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
443 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
446 clear_bit(NFS_DELEGATED_STATE, &state->flags);
448 nfs4_state_set_mode_locked(state, newstate);
449 spin_unlock(&inode->i_lock);
450 spin_unlock(&owner->so_lock);
453 nfs4_put_open_state(state);
454 nfs4_put_state_owner(owner);
456 nfs4_do_close(path, state);
460 * Search the state->lock_states for an existing lock_owner
461 * that is compatible with current->files
463 static struct nfs4_lock_state *
464 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
466 struct nfs4_lock_state *pos;
467 list_for_each_entry(pos, &state->lock_states, ls_locks) {
468 if (pos->ls_owner != fl_owner)
470 atomic_inc(&pos->ls_count);
477 * Return a compatible lock_state. If no initialized lock_state structure
478 * exists, return an uninitialized one.
481 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
483 struct nfs4_lock_state *lsp;
484 struct nfs_client *clp = state->owner->so_client;
486 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
489 lsp->ls_seqid.sequence = &state->owner->so_sequence;
490 atomic_set(&lsp->ls_count, 1);
491 lsp->ls_owner = fl_owner;
492 spin_lock(&clp->cl_lock);
493 nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
494 spin_unlock(&clp->cl_lock);
495 INIT_LIST_HEAD(&lsp->ls_locks);
499 static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
501 struct nfs_client *clp = lsp->ls_state->owner->so_client;
503 spin_lock(&clp->cl_lock);
504 nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
505 spin_unlock(&clp->cl_lock);
510 * Return a compatible lock_state. If no initialized lock_state structure
511 * exists, return an uninitialized one.
513 * The caller must be holding clp->cl_sem
515 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
517 struct nfs4_lock_state *lsp, *new = NULL;
520 spin_lock(&state->state_lock);
521 lsp = __nfs4_find_lock_state(state, owner);
525 new->ls_state = state;
526 list_add(&new->ls_locks, &state->lock_states);
527 set_bit(LK_STATE_IN_USE, &state->flags);
532 spin_unlock(&state->state_lock);
533 new = nfs4_alloc_lock_state(state, owner);
537 spin_unlock(&state->state_lock);
539 nfs4_free_lock_state(new);
544 * Release reference to lock_state, and free it if we see that
545 * it is no longer in use
547 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
549 struct nfs4_state *state;
553 state = lsp->ls_state;
554 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
556 list_del(&lsp->ls_locks);
557 if (list_empty(&state->lock_states))
558 clear_bit(LK_STATE_IN_USE, &state->flags);
559 spin_unlock(&state->state_lock);
560 nfs4_free_lock_state(lsp);
563 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
565 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
567 dst->fl_u.nfs4_fl.owner = lsp;
568 atomic_inc(&lsp->ls_count);
571 static void nfs4_fl_release_lock(struct file_lock *fl)
573 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
576 static struct file_lock_operations nfs4_fl_lock_ops = {
577 .fl_copy_lock = nfs4_fl_copy_lock,
578 .fl_release_private = nfs4_fl_release_lock,
581 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
583 struct nfs4_lock_state *lsp;
585 if (fl->fl_ops != NULL)
587 lsp = nfs4_get_lock_state(state, fl->fl_owner);
590 fl->fl_u.nfs4_fl.owner = lsp;
591 fl->fl_ops = &nfs4_fl_lock_ops;
596 * Byte-range lock aware utility to initialize the stateid of read/write
599 void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
601 struct nfs4_lock_state *lsp;
603 memcpy(dst, &state->stateid, sizeof(*dst));
604 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
607 spin_lock(&state->state_lock);
608 lsp = __nfs4_find_lock_state(state, fl_owner);
609 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
610 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
611 spin_unlock(&state->state_lock);
612 nfs4_put_lock_state(lsp);
615 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
617 struct rpc_sequence *sequence = counter->sequence;
618 struct nfs_seqid *new;
620 new = kmalloc(sizeof(*new), GFP_KERNEL);
622 new->sequence = counter;
623 spin_lock(&sequence->lock);
624 list_add_tail(&new->list, &sequence->list);
625 spin_unlock(&sequence->lock);
630 void nfs_free_seqid(struct nfs_seqid *seqid)
632 struct rpc_sequence *sequence = seqid->sequence->sequence;
634 spin_lock(&sequence->lock);
635 list_del(&seqid->list);
636 spin_unlock(&sequence->lock);
637 rpc_wake_up(&sequence->wait);
642 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
643 * failed with a seqid incrementing error -
644 * see comments nfs_fs.h:seqid_mutating_error()
646 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
651 case -NFS4ERR_BAD_SEQID:
652 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
654 printk(KERN_WARNING "NFS: v4 server returned a bad"
655 "sequence-id error on an"
656 "unconfirmed sequence %p!\n",
658 case -NFS4ERR_STALE_CLIENTID:
659 case -NFS4ERR_STALE_STATEID:
660 case -NFS4ERR_BAD_STATEID:
661 case -NFS4ERR_BADXDR:
662 case -NFS4ERR_RESOURCE:
663 case -NFS4ERR_NOFILEHANDLE:
664 /* Non-seqid mutating errors */
668 * Note: no locking needed as we are guaranteed to be first
669 * on the sequence list
671 seqid->sequence->counter++;
674 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
676 if (status == -NFS4ERR_BAD_SEQID) {
677 struct nfs4_state_owner *sp = container_of(seqid->sequence,
678 struct nfs4_state_owner, so_seqid);
679 nfs4_drop_state_owner(sp);
681 nfs_increment_seqid(status, seqid);
685 * Increment the seqid if the LOCK/LOCKU succeeded, or
686 * failed with a seqid incrementing error -
687 * see comments nfs_fs.h:seqid_mutating_error()
689 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
691 nfs_increment_seqid(status, seqid);
694 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
696 struct rpc_sequence *sequence = seqid->sequence->sequence;
699 if (sequence->list.next == &seqid->list)
701 spin_lock(&sequence->lock);
702 if (sequence->list.next != &seqid->list) {
703 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
706 spin_unlock(&sequence->lock);
711 static int reclaimer(void *);
713 static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
715 smp_mb__before_clear_bit();
716 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
717 smp_mb__after_clear_bit();
718 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
719 rpc_wake_up(&clp->cl_rpcwaitq);
723 * State recovery routine
725 static void nfs4_recover_state(struct nfs_client *clp)
727 struct task_struct *task;
729 __module_get(THIS_MODULE);
730 atomic_inc(&clp->cl_count);
731 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
732 NIPQUAD(clp->cl_addr.sin_addr));
735 nfs4_clear_recover_bit(clp);
737 module_put(THIS_MODULE);
741 * Schedule a state recovery attempt
743 void nfs4_schedule_state_recovery(struct nfs_client *clp)
747 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
748 nfs4_recover_state(clp);
751 static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
753 struct inode *inode = state->inode;
754 struct file_lock *fl;
757 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
758 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
760 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
762 status = ops->recover_lock(state, fl);
767 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
768 __FUNCTION__, status);
769 case -NFS4ERR_EXPIRED:
770 case -NFS4ERR_NO_GRACE:
771 case -NFS4ERR_RECLAIM_BAD:
772 case -NFS4ERR_RECLAIM_CONFLICT:
773 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
775 case -NFS4ERR_STALE_CLIENTID:
784 static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
786 struct nfs4_state *state;
787 struct nfs4_lock_state *lock;
790 /* Note: we rely on the sp->so_states list being ordered
791 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
793 * This is needed to ensure that the server won't give us any
794 * read delegations that we have to return if, say, we are
795 * recovering after a network partition or a reboot from a
796 * server that doesn't support a grace period.
798 list_for_each_entry(state, &sp->so_states, open_states) {
799 if (state->state == 0)
801 status = ops->recover_open(sp, state);
803 status = nfs4_reclaim_locks(ops, state);
806 list_for_each_entry(lock, &state->lock_states, ls_locks) {
807 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
808 printk("%s: Lock reclaim failed!\n",
815 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
816 __FUNCTION__, status);
818 case -NFS4ERR_RECLAIM_BAD:
819 case -NFS4ERR_RECLAIM_CONFLICT:
821 * Open state on this file cannot be recovered
822 * All we can do is revert to using the zero stateid.
824 memset(state->stateid.data, 0,
825 sizeof(state->stateid.data));
826 /* Mark the file as being 'closed' */
829 case -NFS4ERR_EXPIRED:
830 case -NFS4ERR_NO_GRACE:
831 case -NFS4ERR_STALE_CLIENTID:
840 static void nfs4_state_mark_reclaim(struct nfs_client *clp)
842 struct nfs4_state_owner *sp;
844 struct nfs4_state *state;
845 struct nfs4_lock_state *lock;
847 /* Reset all sequence ids to zero */
848 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
849 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
850 sp->so_seqid.counter = 0;
851 sp->so_seqid.flags = 0;
852 spin_lock(&sp->so_lock);
853 list_for_each_entry(state, &sp->so_states, open_states) {
854 clear_bit(NFS_DELEGATED_STATE, &state->flags);
855 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
856 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
857 clear_bit(NFS_O_RDWR_STATE, &state->flags);
858 list_for_each_entry(lock, &state->lock_states, ls_locks) {
859 lock->ls_seqid.counter = 0;
860 lock->ls_seqid.flags = 0;
861 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
864 spin_unlock(&sp->so_lock);
868 static int reclaimer(void *ptr)
870 struct nfs_client *clp = ptr;
871 struct nfs4_state_owner *sp;
873 struct nfs4_state_recovery_ops *ops;
874 struct rpc_cred *cred;
877 allow_signal(SIGKILL);
879 /* Ensure exclusive access to NFSv4 state */
881 down_write(&clp->cl_sem);
882 /* Are there any NFS mounts out there? */
883 if (list_empty(&clp->cl_superblocks))
886 ops = &nfs4_network_partition_recovery_ops;
887 /* Are there any open files on this volume? */
888 cred = nfs4_get_renew_cred(clp);
890 /* Yes there are: try to renew the old lease */
891 status = nfs4_proc_renew(clp, cred);
894 case -NFS4ERR_CB_PATH_DOWN:
897 case -NFS4ERR_STALE_CLIENTID:
898 case -NFS4ERR_LEASE_MOVED:
899 ops = &nfs4_reboot_recovery_ops;
902 /* "reboot" to ensure we clear all state on the server */
903 clp->cl_boot_time = CURRENT_TIME;
904 cred = nfs4_get_setclientid_cred(clp);
906 /* We're going to have to re-establish a clientid */
907 nfs4_state_mark_reclaim(clp);
910 status = nfs4_init_client(clp, cred);
915 /* Mark all delegations for reclaim */
916 nfs_delegation_mark_reclaim(clp);
917 /* Note: list is protected by exclusive lock on cl->cl_sem */
918 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
919 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
920 status = nfs4_reclaim_open_state(ops, sp);
922 if (status == -NFS4ERR_NO_GRACE) {
923 ops = &nfs4_network_partition_recovery_ops;
924 status = nfs4_reclaim_open_state(ops, sp);
926 if (status == -NFS4ERR_STALE_CLIENTID)
928 if (status == -NFS4ERR_EXPIRED)
932 nfs_delegation_reap_unclaimed(clp);
934 up_write(&clp->cl_sem);
936 if (status == -NFS4ERR_CB_PATH_DOWN)
937 nfs_handle_cb_pathdown(clp);
938 nfs4_clear_recover_bit(clp);
940 module_put_and_exit(0);
943 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
944 NIPQUAD(clp->cl_addr.sin_addr), -status);
945 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);