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/random.h>
48 #include <linux/workqueue.h>
49 #include <linux/bitops.h>
53 #include "delegation.h"
56 #define OPENOWNER_POOL_SIZE 8
58 const nfs4_stateid zero_stateid;
60 static LIST_HEAD(nfs4_clientid_list);
62 static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
64 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
65 nfs_callback_tcpport, cred);
67 status = nfs4_proc_setclientid_confirm(clp, cred);
69 nfs4_schedule_state_renewal(clp);
73 struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
75 struct nfs4_state_owner *sp;
77 struct rpc_cred *cred = NULL;
79 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
80 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
81 if (list_empty(&sp->so_states))
83 cred = get_rpccred(sp->so_cred);
89 static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
91 struct nfs4_state_owner *sp;
94 pos = rb_first(&clp->cl_state_owners);
96 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
97 return get_rpccred(sp->so_cred);
102 static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
103 __u64 minval, int maxbits)
105 struct rb_node **p, *parent;
106 struct nfs_unique_id *pos;
110 mask = (1ULL << maxbits) - 1ULL;
112 /* Ensure distribution is more or less flat */
113 get_random_bytes(&new->id, sizeof(new->id));
115 if (new->id < minval)
123 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
125 if (new->id < pos->id)
127 else if (new->id > pos->id)
132 rb_link_node(&new->rb_node, parent, p);
133 rb_insert_color(&new->rb_node, root);
138 if (new->id < minval || (new->id & mask) != new->id) {
142 parent = rb_next(parent);
145 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
146 if (new->id < pos->id)
152 static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
154 rb_erase(&id->rb_node, root);
157 static struct nfs4_state_owner *
158 nfs4_find_state_owner(struct nfs_client *clp, struct rpc_cred *cred)
160 struct rb_node **p = &clp->cl_state_owners.rb_node,
162 struct nfs4_state_owner *sp, *res = NULL;
166 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
168 if (cred < sp->so_cred)
169 p = &parent->rb_left;
170 else if (cred > sp->so_cred)
171 p = &parent->rb_right;
173 atomic_inc(&sp->so_count);
181 static struct nfs4_state_owner *
182 nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
184 struct rb_node **p = &clp->cl_state_owners.rb_node,
186 struct nfs4_state_owner *sp;
190 sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
192 if (new->so_cred < sp->so_cred)
193 p = &parent->rb_left;
194 else if (new->so_cred > sp->so_cred)
195 p = &parent->rb_right;
197 atomic_inc(&sp->so_count);
201 nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
202 rb_link_node(&new->so_client_node, parent, p);
203 rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
208 nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
210 if (!RB_EMPTY_NODE(&sp->so_client_node))
211 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
212 nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
216 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
217 * create a new state_owner.
220 static struct nfs4_state_owner *
221 nfs4_alloc_state_owner(void)
223 struct nfs4_state_owner *sp;
225 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
228 spin_lock_init(&sp->so_lock);
229 INIT_LIST_HEAD(&sp->so_states);
230 INIT_LIST_HEAD(&sp->so_delegations);
231 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
232 sp->so_seqid.sequence = &sp->so_sequence;
233 spin_lock_init(&sp->so_sequence.lock);
234 INIT_LIST_HEAD(&sp->so_sequence.list);
235 atomic_set(&sp->so_count, 1);
240 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
242 if (!RB_EMPTY_NODE(&sp->so_client_node)) {
243 struct nfs_client *clp = sp->so_client;
245 spin_lock(&clp->cl_lock);
246 rb_erase(&sp->so_client_node, &clp->cl_state_owners);
247 RB_CLEAR_NODE(&sp->so_client_node);
248 spin_unlock(&clp->cl_lock);
253 * Note: must be called with clp->cl_sem held in order to prevent races
254 * with reboot recovery!
256 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
258 struct nfs_client *clp = server->nfs_client;
259 struct nfs4_state_owner *sp, *new;
261 spin_lock(&clp->cl_lock);
262 sp = nfs4_find_state_owner(clp, cred);
263 spin_unlock(&clp->cl_lock);
266 new = nfs4_alloc_state_owner();
269 new->so_client = clp;
271 spin_lock(&clp->cl_lock);
272 sp = nfs4_insert_state_owner(clp, new);
273 spin_unlock(&clp->cl_lock);
282 * Must be called with clp->cl_sem held in order to avoid races
283 * with state recovery...
285 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
287 struct nfs_client *clp = sp->so_client;
288 struct rpc_cred *cred = sp->so_cred;
290 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
292 nfs4_remove_state_owner(clp, sp);
293 spin_unlock(&clp->cl_lock);
298 static struct nfs4_state *
299 nfs4_alloc_open_state(void)
301 struct nfs4_state *state;
303 state = kzalloc(sizeof(*state), GFP_KERNEL);
306 atomic_set(&state->count, 1);
307 INIT_LIST_HEAD(&state->lock_states);
308 spin_lock_init(&state->state_lock);
313 nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
315 if (state->state == mode)
317 /* NB! List reordering - see the reclaim code for why. */
318 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
319 if (mode & FMODE_WRITE)
320 list_move(&state->open_states, &state->owner->so_states);
322 list_move_tail(&state->open_states, &state->owner->so_states);
325 list_del_init(&state->inode_states);
329 static struct nfs4_state *
330 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
332 struct nfs_inode *nfsi = NFS_I(inode);
333 struct nfs4_state *state;
335 list_for_each_entry(state, &nfsi->open_states, inode_states) {
336 if (state->owner != owner)
338 if (atomic_inc_not_zero(&state->count))
345 nfs4_free_open_state(struct nfs4_state *state)
351 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
353 struct nfs4_state *state, *new;
354 struct nfs_inode *nfsi = NFS_I(inode);
356 spin_lock(&inode->i_lock);
357 state = __nfs4_find_state_byowner(inode, owner);
358 spin_unlock(&inode->i_lock);
361 new = nfs4_alloc_open_state();
362 spin_lock(&owner->so_lock);
363 spin_lock(&inode->i_lock);
364 state = __nfs4_find_state_byowner(inode, owner);
365 if (state == NULL && new != NULL) {
367 state->owner = owner;
368 atomic_inc(&owner->so_count);
369 list_add(&state->inode_states, &nfsi->open_states);
370 state->inode = igrab(inode);
371 spin_unlock(&inode->i_lock);
372 /* Note: The reclaim code dictates that we add stateless
373 * and read-only stateids to the end of the list */
374 list_add_tail(&state->open_states, &owner->so_states);
375 spin_unlock(&owner->so_lock);
377 spin_unlock(&inode->i_lock);
378 spin_unlock(&owner->so_lock);
380 nfs4_free_open_state(new);
387 * Beware! Caller must be holding exactly one
388 * reference to clp->cl_sem!
390 void nfs4_put_open_state(struct nfs4_state *state)
392 struct inode *inode = state->inode;
393 struct nfs4_state_owner *owner = state->owner;
395 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
397 spin_lock(&inode->i_lock);
398 if (!list_empty(&state->inode_states))
399 list_del(&state->inode_states);
400 list_del(&state->open_states);
401 spin_unlock(&inode->i_lock);
402 spin_unlock(&owner->so_lock);
404 nfs4_free_open_state(state);
405 nfs4_put_state_owner(owner);
409 * Close the current file.
411 void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
413 struct inode *inode = state->inode;
414 struct nfs4_state_owner *owner = state->owner;
415 int oldstate, newstate = 0;
417 atomic_inc(&owner->so_count);
418 /* Protect against nfs4_find_state() */
419 spin_lock(&owner->so_lock);
420 spin_lock(&inode->i_lock);
421 switch (mode & (FMODE_READ | FMODE_WRITE)) {
428 case FMODE_READ|FMODE_WRITE:
431 oldstate = newstate = state->state;
432 if (state->n_rdwr == 0) {
433 if (state->n_rdonly == 0)
434 newstate &= ~FMODE_READ;
435 if (state->n_wronly == 0)
436 newstate &= ~FMODE_WRITE;
438 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
439 nfs4_state_set_mode_locked(state, newstate);
442 spin_unlock(&inode->i_lock);
443 spin_unlock(&owner->so_lock);
445 if (oldstate == newstate) {
446 nfs4_put_open_state(state);
447 nfs4_put_state_owner(owner);
449 nfs4_do_close(path, state);
453 * Search the state->lock_states for an existing lock_owner
454 * that is compatible with current->files
456 static struct nfs4_lock_state *
457 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
459 struct nfs4_lock_state *pos;
460 list_for_each_entry(pos, &state->lock_states, ls_locks) {
461 if (pos->ls_owner != fl_owner)
463 atomic_inc(&pos->ls_count);
470 * Return a compatible lock_state. If no initialized lock_state structure
471 * exists, return an uninitialized one.
474 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
476 struct nfs4_lock_state *lsp;
477 struct nfs_client *clp = state->owner->so_client;
479 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
482 lsp->ls_seqid.sequence = &state->owner->so_sequence;
483 atomic_set(&lsp->ls_count, 1);
484 lsp->ls_owner = fl_owner;
485 spin_lock(&clp->cl_lock);
486 nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
487 spin_unlock(&clp->cl_lock);
488 INIT_LIST_HEAD(&lsp->ls_locks);
492 static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
494 struct nfs_client *clp = lsp->ls_state->owner->so_client;
496 spin_lock(&clp->cl_lock);
497 nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
498 spin_unlock(&clp->cl_lock);
503 * Return a compatible lock_state. If no initialized lock_state structure
504 * exists, return an uninitialized one.
506 * The caller must be holding clp->cl_sem
508 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
510 struct nfs4_lock_state *lsp, *new = NULL;
513 spin_lock(&state->state_lock);
514 lsp = __nfs4_find_lock_state(state, owner);
518 new->ls_state = state;
519 list_add(&new->ls_locks, &state->lock_states);
520 set_bit(LK_STATE_IN_USE, &state->flags);
525 spin_unlock(&state->state_lock);
526 new = nfs4_alloc_lock_state(state, owner);
530 spin_unlock(&state->state_lock);
532 nfs4_free_lock_state(new);
537 * Release reference to lock_state, and free it if we see that
538 * it is no longer in use
540 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
542 struct nfs4_state *state;
546 state = lsp->ls_state;
547 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
549 list_del(&lsp->ls_locks);
550 if (list_empty(&state->lock_states))
551 clear_bit(LK_STATE_IN_USE, &state->flags);
552 spin_unlock(&state->state_lock);
553 nfs4_free_lock_state(lsp);
556 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
558 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
560 dst->fl_u.nfs4_fl.owner = lsp;
561 atomic_inc(&lsp->ls_count);
564 static void nfs4_fl_release_lock(struct file_lock *fl)
566 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
569 static struct file_lock_operations nfs4_fl_lock_ops = {
570 .fl_copy_lock = nfs4_fl_copy_lock,
571 .fl_release_private = nfs4_fl_release_lock,
574 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
576 struct nfs4_lock_state *lsp;
578 if (fl->fl_ops != NULL)
580 lsp = nfs4_get_lock_state(state, fl->fl_owner);
583 fl->fl_u.nfs4_fl.owner = lsp;
584 fl->fl_ops = &nfs4_fl_lock_ops;
589 * Byte-range lock aware utility to initialize the stateid of read/write
592 void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
594 struct nfs4_lock_state *lsp;
596 memcpy(dst, &state->stateid, sizeof(*dst));
597 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
600 spin_lock(&state->state_lock);
601 lsp = __nfs4_find_lock_state(state, fl_owner);
602 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
603 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
604 spin_unlock(&state->state_lock);
605 nfs4_put_lock_state(lsp);
608 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
610 struct rpc_sequence *sequence = counter->sequence;
611 struct nfs_seqid *new;
613 new = kmalloc(sizeof(*new), GFP_KERNEL);
615 new->sequence = counter;
616 spin_lock(&sequence->lock);
617 list_add_tail(&new->list, &sequence->list);
618 spin_unlock(&sequence->lock);
623 void nfs_free_seqid(struct nfs_seqid *seqid)
625 struct rpc_sequence *sequence = seqid->sequence->sequence;
627 spin_lock(&sequence->lock);
628 list_del(&seqid->list);
629 spin_unlock(&sequence->lock);
630 rpc_wake_up(&sequence->wait);
635 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
636 * failed with a seqid incrementing error -
637 * see comments nfs_fs.h:seqid_mutating_error()
639 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
644 case -NFS4ERR_BAD_SEQID:
645 case -NFS4ERR_STALE_CLIENTID:
646 case -NFS4ERR_STALE_STATEID:
647 case -NFS4ERR_BAD_STATEID:
648 case -NFS4ERR_BADXDR:
649 case -NFS4ERR_RESOURCE:
650 case -NFS4ERR_NOFILEHANDLE:
651 /* Non-seqid mutating errors */
655 * Note: no locking needed as we are guaranteed to be first
656 * on the sequence list
658 seqid->sequence->counter++;
661 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
663 if (status == -NFS4ERR_BAD_SEQID) {
664 struct nfs4_state_owner *sp = container_of(seqid->sequence,
665 struct nfs4_state_owner, so_seqid);
666 nfs4_drop_state_owner(sp);
668 nfs_increment_seqid(status, seqid);
672 * Increment the seqid if the LOCK/LOCKU succeeded, or
673 * failed with a seqid incrementing error -
674 * see comments nfs_fs.h:seqid_mutating_error()
676 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
678 nfs_increment_seqid(status, seqid);
681 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
683 struct rpc_sequence *sequence = seqid->sequence->sequence;
686 if (sequence->list.next == &seqid->list)
688 spin_lock(&sequence->lock);
689 if (sequence->list.next != &seqid->list) {
690 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
693 spin_unlock(&sequence->lock);
698 static int reclaimer(void *);
700 static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
702 smp_mb__before_clear_bit();
703 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
704 smp_mb__after_clear_bit();
705 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
706 rpc_wake_up(&clp->cl_rpcwaitq);
710 * State recovery routine
712 static void nfs4_recover_state(struct nfs_client *clp)
714 struct task_struct *task;
716 __module_get(THIS_MODULE);
717 atomic_inc(&clp->cl_count);
718 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
719 NIPQUAD(clp->cl_addr.sin_addr));
722 nfs4_clear_recover_bit(clp);
724 module_put(THIS_MODULE);
728 * Schedule a state recovery attempt
730 void nfs4_schedule_state_recovery(struct nfs_client *clp)
734 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
735 nfs4_recover_state(clp);
738 static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
740 struct inode *inode = state->inode;
741 struct file_lock *fl;
744 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
745 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
747 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
749 status = ops->recover_lock(state, fl);
754 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
755 __FUNCTION__, status);
756 case -NFS4ERR_EXPIRED:
757 case -NFS4ERR_NO_GRACE:
758 case -NFS4ERR_RECLAIM_BAD:
759 case -NFS4ERR_RECLAIM_CONFLICT:
760 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
762 case -NFS4ERR_STALE_CLIENTID:
771 static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
773 struct nfs4_state *state;
774 struct nfs4_lock_state *lock;
777 /* Note: we rely on the sp->so_states list being ordered
778 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
780 * This is needed to ensure that the server won't give us any
781 * read delegations that we have to return if, say, we are
782 * recovering after a network partition or a reboot from a
783 * server that doesn't support a grace period.
785 list_for_each_entry(state, &sp->so_states, open_states) {
786 if (state->state == 0)
788 status = ops->recover_open(sp, state);
790 status = nfs4_reclaim_locks(ops, state);
793 list_for_each_entry(lock, &state->lock_states, ls_locks) {
794 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
795 printk("%s: Lock reclaim failed!\n",
802 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
803 __FUNCTION__, status);
805 case -NFS4ERR_RECLAIM_BAD:
806 case -NFS4ERR_RECLAIM_CONFLICT:
808 * Open state on this file cannot be recovered
809 * All we can do is revert to using the zero stateid.
811 memset(state->stateid.data, 0,
812 sizeof(state->stateid.data));
813 /* Mark the file as being 'closed' */
816 case -NFS4ERR_EXPIRED:
817 case -NFS4ERR_NO_GRACE:
818 case -NFS4ERR_STALE_CLIENTID:
827 static void nfs4_state_mark_reclaim(struct nfs_client *clp)
829 struct nfs4_state_owner *sp;
831 struct nfs4_state *state;
832 struct nfs4_lock_state *lock;
834 /* Reset all sequence ids to zero */
835 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
836 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
837 sp->so_seqid.counter = 0;
838 sp->so_seqid.flags = 0;
839 spin_lock(&sp->so_lock);
840 list_for_each_entry(state, &sp->so_states, open_states) {
841 list_for_each_entry(lock, &state->lock_states, ls_locks) {
842 lock->ls_seqid.counter = 0;
843 lock->ls_seqid.flags = 0;
844 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
847 spin_unlock(&sp->so_lock);
851 static int reclaimer(void *ptr)
853 struct nfs_client *clp = ptr;
854 struct nfs4_state_owner *sp;
856 struct nfs4_state_recovery_ops *ops;
857 struct rpc_cred *cred;
860 allow_signal(SIGKILL);
862 /* Ensure exclusive access to NFSv4 state */
864 down_write(&clp->cl_sem);
865 /* Are there any NFS mounts out there? */
866 if (list_empty(&clp->cl_superblocks))
869 ops = &nfs4_network_partition_recovery_ops;
870 /* Are there any open files on this volume? */
871 cred = nfs4_get_renew_cred(clp);
873 /* Yes there are: try to renew the old lease */
874 status = nfs4_proc_renew(clp, cred);
877 case -NFS4ERR_CB_PATH_DOWN:
880 case -NFS4ERR_STALE_CLIENTID:
881 case -NFS4ERR_LEASE_MOVED:
882 ops = &nfs4_reboot_recovery_ops;
885 /* "reboot" to ensure we clear all state on the server */
886 clp->cl_boot_time = CURRENT_TIME;
887 cred = nfs4_get_setclientid_cred(clp);
889 /* We're going to have to re-establish a clientid */
890 nfs4_state_mark_reclaim(clp);
893 status = nfs4_init_client(clp, cred);
898 /* Mark all delegations for reclaim */
899 nfs_delegation_mark_reclaim(clp);
900 /* Note: list is protected by exclusive lock on cl->cl_sem */
901 for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
902 sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
903 status = nfs4_reclaim_open_state(ops, sp);
905 if (status == -NFS4ERR_NO_GRACE) {
906 ops = &nfs4_network_partition_recovery_ops;
907 status = nfs4_reclaim_open_state(ops, sp);
909 if (status == -NFS4ERR_STALE_CLIENTID)
911 if (status == -NFS4ERR_EXPIRED)
915 nfs_delegation_reap_unclaimed(clp);
917 up_write(&clp->cl_sem);
919 if (status == -NFS4ERR_CB_PATH_DOWN)
920 nfs_handle_cb_pathdown(clp);
921 nfs4_clear_recover_bit(clp);
923 module_put_and_exit(0);
926 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
927 NIPQUAD(clp->cl_addr.sin_addr), -status);
928 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);