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"
55 #define OPENOWNER_POOL_SIZE 8
57 const nfs4_stateid zero_stateid;
59 static LIST_HEAD(nfs4_clientid_list);
61 static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
63 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
64 nfs_callback_tcpport, cred);
66 status = nfs4_proc_setclientid_confirm(clp, cred);
68 nfs4_schedule_state_renewal(clp);
73 nfs4_alloc_lockowner_id(struct nfs_client *clp)
75 return clp->cl_lockowner_id ++;
78 struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
80 struct nfs4_state_owner *sp;
81 struct rpc_cred *cred = NULL;
83 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
84 if (list_empty(&sp->so_states))
86 cred = get_rpccred(sp->so_cred);
92 static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
94 struct nfs4_state_owner *sp;
96 if (!list_empty(&clp->cl_state_owners)) {
97 sp = list_entry(clp->cl_state_owners.next,
98 struct nfs4_state_owner, so_list);
99 return get_rpccred(sp->so_cred);
104 static struct nfs4_state_owner *
105 nfs4_find_state_owner(struct nfs_client *clp, struct rpc_cred *cred)
107 struct nfs4_state_owner *sp, *res = NULL;
109 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
110 if (sp->so_cred != cred)
112 atomic_inc(&sp->so_count);
113 /* Move to the head of the list */
114 list_move(&sp->so_list, &clp->cl_state_owners);
122 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
123 * create a new state_owner.
126 static struct nfs4_state_owner *
127 nfs4_alloc_state_owner(void)
129 struct nfs4_state_owner *sp;
131 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
134 spin_lock_init(&sp->so_lock);
135 INIT_LIST_HEAD(&sp->so_states);
136 INIT_LIST_HEAD(&sp->so_delegations);
137 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
138 sp->so_seqid.sequence = &sp->so_sequence;
139 spin_lock_init(&sp->so_sequence.lock);
140 INIT_LIST_HEAD(&sp->so_sequence.list);
141 atomic_set(&sp->so_count, 1);
146 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
148 struct nfs_client *clp = sp->so_client;
149 spin_lock(&clp->cl_lock);
150 list_del_init(&sp->so_list);
151 spin_unlock(&clp->cl_lock);
155 * Note: must be called with clp->cl_sem held in order to prevent races
156 * with reboot recovery!
158 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
160 struct nfs_client *clp = server->nfs_client;
161 struct nfs4_state_owner *sp, *new;
163 new = nfs4_alloc_state_owner();
164 spin_lock(&clp->cl_lock);
165 sp = nfs4_find_state_owner(clp, cred);
166 if (sp == NULL && new != NULL) {
167 list_add(&new->so_list, &clp->cl_state_owners);
168 new->so_client = clp;
169 new->so_id = nfs4_alloc_lockowner_id(clp);
170 new->so_cred = get_rpccred(cred);
174 spin_unlock(&clp->cl_lock);
182 * Must be called with clp->cl_sem held in order to avoid races
183 * with state recovery...
185 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
187 struct nfs_client *clp = sp->so_client;
188 struct rpc_cred *cred = sp->so_cred;
190 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
192 list_del(&sp->so_list);
193 spin_unlock(&clp->cl_lock);
198 static struct nfs4_state *
199 nfs4_alloc_open_state(void)
201 struct nfs4_state *state;
203 state = kzalloc(sizeof(*state), GFP_KERNEL);
206 atomic_set(&state->count, 1);
207 INIT_LIST_HEAD(&state->lock_states);
208 spin_lock_init(&state->state_lock);
213 nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
215 if (state->state == mode)
217 /* NB! List reordering - see the reclaim code for why. */
218 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
219 if (mode & FMODE_WRITE)
220 list_move(&state->open_states, &state->owner->so_states);
222 list_move_tail(&state->open_states, &state->owner->so_states);
225 list_del_init(&state->inode_states);
229 static struct nfs4_state *
230 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
232 struct nfs_inode *nfsi = NFS_I(inode);
233 struct nfs4_state *state;
235 list_for_each_entry(state, &nfsi->open_states, inode_states) {
236 /* Is this in the process of being freed? */
237 if (state->state == 0)
239 if (state->owner == owner) {
240 atomic_inc(&state->count);
248 nfs4_free_open_state(struct nfs4_state *state)
254 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
256 struct nfs4_state *state, *new;
257 struct nfs_inode *nfsi = NFS_I(inode);
259 spin_lock(&inode->i_lock);
260 state = __nfs4_find_state_byowner(inode, owner);
261 spin_unlock(&inode->i_lock);
264 new = nfs4_alloc_open_state();
265 spin_lock(&owner->so_lock);
266 spin_lock(&inode->i_lock);
267 state = __nfs4_find_state_byowner(inode, owner);
268 if (state == NULL && new != NULL) {
270 state->owner = owner;
271 atomic_inc(&owner->so_count);
272 list_add(&state->inode_states, &nfsi->open_states);
273 state->inode = igrab(inode);
274 spin_unlock(&inode->i_lock);
275 /* Note: The reclaim code dictates that we add stateless
276 * and read-only stateids to the end of the list */
277 list_add_tail(&state->open_states, &owner->so_states);
278 spin_unlock(&owner->so_lock);
280 spin_unlock(&inode->i_lock);
281 spin_unlock(&owner->so_lock);
283 nfs4_free_open_state(new);
290 * Beware! Caller must be holding exactly one
291 * reference to clp->cl_sem!
293 void nfs4_put_open_state(struct nfs4_state *state)
295 struct inode *inode = state->inode;
296 struct nfs4_state_owner *owner = state->owner;
298 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
300 spin_lock(&inode->i_lock);
301 if (!list_empty(&state->inode_states))
302 list_del(&state->inode_states);
303 list_del(&state->open_states);
304 spin_unlock(&inode->i_lock);
305 spin_unlock(&owner->so_lock);
307 nfs4_free_open_state(state);
308 nfs4_put_state_owner(owner);
312 * Close the current file.
314 void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
316 struct inode *inode = state->inode;
317 struct nfs4_state_owner *owner = state->owner;
318 int oldstate, newstate = 0;
320 atomic_inc(&owner->so_count);
321 /* Protect against nfs4_find_state() */
322 spin_lock(&owner->so_lock);
323 spin_lock(&inode->i_lock);
324 switch (mode & (FMODE_READ | FMODE_WRITE)) {
331 case FMODE_READ|FMODE_WRITE:
334 oldstate = newstate = state->state;
335 if (state->n_rdwr == 0) {
336 if (state->n_rdonly == 0)
337 newstate &= ~FMODE_READ;
338 if (state->n_wronly == 0)
339 newstate &= ~FMODE_WRITE;
341 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
342 nfs4_state_set_mode_locked(state, newstate);
345 spin_unlock(&inode->i_lock);
346 spin_unlock(&owner->so_lock);
348 if (oldstate == newstate) {
349 nfs4_put_open_state(state);
350 nfs4_put_state_owner(owner);
352 nfs4_do_close(path, state);
356 * Search the state->lock_states for an existing lock_owner
357 * that is compatible with current->files
359 static struct nfs4_lock_state *
360 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
362 struct nfs4_lock_state *pos;
363 list_for_each_entry(pos, &state->lock_states, ls_locks) {
364 if (pos->ls_owner != fl_owner)
366 atomic_inc(&pos->ls_count);
373 * Return a compatible lock_state. If no initialized lock_state structure
374 * exists, return an uninitialized one.
377 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
379 struct nfs4_lock_state *lsp;
380 struct nfs_client *clp = state->owner->so_client;
382 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
385 lsp->ls_seqid.sequence = &state->owner->so_sequence;
386 atomic_set(&lsp->ls_count, 1);
387 lsp->ls_owner = fl_owner;
388 spin_lock(&clp->cl_lock);
389 lsp->ls_id = nfs4_alloc_lockowner_id(clp);
390 spin_unlock(&clp->cl_lock);
391 INIT_LIST_HEAD(&lsp->ls_locks);
396 * Return a compatible lock_state. If no initialized lock_state structure
397 * exists, return an uninitialized one.
399 * The caller must be holding clp->cl_sem
401 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
403 struct nfs4_lock_state *lsp, *new = NULL;
406 spin_lock(&state->state_lock);
407 lsp = __nfs4_find_lock_state(state, owner);
411 new->ls_state = state;
412 list_add(&new->ls_locks, &state->lock_states);
413 set_bit(LK_STATE_IN_USE, &state->flags);
418 spin_unlock(&state->state_lock);
419 new = nfs4_alloc_lock_state(state, owner);
423 spin_unlock(&state->state_lock);
429 * Release reference to lock_state, and free it if we see that
430 * it is no longer in use
432 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
434 struct nfs4_state *state;
438 state = lsp->ls_state;
439 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
441 list_del(&lsp->ls_locks);
442 if (list_empty(&state->lock_states))
443 clear_bit(LK_STATE_IN_USE, &state->flags);
444 spin_unlock(&state->state_lock);
448 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
450 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
452 dst->fl_u.nfs4_fl.owner = lsp;
453 atomic_inc(&lsp->ls_count);
456 static void nfs4_fl_release_lock(struct file_lock *fl)
458 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
461 static struct file_lock_operations nfs4_fl_lock_ops = {
462 .fl_copy_lock = nfs4_fl_copy_lock,
463 .fl_release_private = nfs4_fl_release_lock,
466 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
468 struct nfs4_lock_state *lsp;
470 if (fl->fl_ops != NULL)
472 lsp = nfs4_get_lock_state(state, fl->fl_owner);
475 fl->fl_u.nfs4_fl.owner = lsp;
476 fl->fl_ops = &nfs4_fl_lock_ops;
481 * Byte-range lock aware utility to initialize the stateid of read/write
484 void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
486 struct nfs4_lock_state *lsp;
488 memcpy(dst, &state->stateid, sizeof(*dst));
489 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
492 spin_lock(&state->state_lock);
493 lsp = __nfs4_find_lock_state(state, fl_owner);
494 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
495 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
496 spin_unlock(&state->state_lock);
497 nfs4_put_lock_state(lsp);
500 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
502 struct rpc_sequence *sequence = counter->sequence;
503 struct nfs_seqid *new;
505 new = kmalloc(sizeof(*new), GFP_KERNEL);
507 new->sequence = counter;
508 spin_lock(&sequence->lock);
509 list_add_tail(&new->list, &sequence->list);
510 spin_unlock(&sequence->lock);
515 void nfs_free_seqid(struct nfs_seqid *seqid)
517 struct rpc_sequence *sequence = seqid->sequence->sequence;
519 spin_lock(&sequence->lock);
520 list_del(&seqid->list);
521 spin_unlock(&sequence->lock);
522 rpc_wake_up(&sequence->wait);
527 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
528 * failed with a seqid incrementing error -
529 * see comments nfs_fs.h:seqid_mutating_error()
531 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
536 case -NFS4ERR_BAD_SEQID:
537 case -NFS4ERR_STALE_CLIENTID:
538 case -NFS4ERR_STALE_STATEID:
539 case -NFS4ERR_BAD_STATEID:
540 case -NFS4ERR_BADXDR:
541 case -NFS4ERR_RESOURCE:
542 case -NFS4ERR_NOFILEHANDLE:
543 /* Non-seqid mutating errors */
547 * Note: no locking needed as we are guaranteed to be first
548 * on the sequence list
550 seqid->sequence->counter++;
553 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
555 if (status == -NFS4ERR_BAD_SEQID) {
556 struct nfs4_state_owner *sp = container_of(seqid->sequence,
557 struct nfs4_state_owner, so_seqid);
558 nfs4_drop_state_owner(sp);
560 nfs_increment_seqid(status, seqid);
564 * Increment the seqid if the LOCK/LOCKU succeeded, or
565 * failed with a seqid incrementing error -
566 * see comments nfs_fs.h:seqid_mutating_error()
568 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
570 nfs_increment_seqid(status, seqid);
573 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
575 struct rpc_sequence *sequence = seqid->sequence->sequence;
578 if (sequence->list.next == &seqid->list)
580 spin_lock(&sequence->lock);
581 if (sequence->list.next != &seqid->list) {
582 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
585 spin_unlock(&sequence->lock);
590 static int reclaimer(void *);
592 static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
594 smp_mb__before_clear_bit();
595 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
596 smp_mb__after_clear_bit();
597 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
598 rpc_wake_up(&clp->cl_rpcwaitq);
602 * State recovery routine
604 static void nfs4_recover_state(struct nfs_client *clp)
606 struct task_struct *task;
608 __module_get(THIS_MODULE);
609 atomic_inc(&clp->cl_count);
610 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
611 NIPQUAD(clp->cl_addr.sin_addr));
614 nfs4_clear_recover_bit(clp);
616 module_put(THIS_MODULE);
620 * Schedule a state recovery attempt
622 void nfs4_schedule_state_recovery(struct nfs_client *clp)
626 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
627 nfs4_recover_state(clp);
630 static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
632 struct inode *inode = state->inode;
633 struct file_lock *fl;
636 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
637 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
639 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
641 status = ops->recover_lock(state, fl);
646 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
647 __FUNCTION__, status);
648 case -NFS4ERR_EXPIRED:
649 case -NFS4ERR_NO_GRACE:
650 case -NFS4ERR_RECLAIM_BAD:
651 case -NFS4ERR_RECLAIM_CONFLICT:
652 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
654 case -NFS4ERR_STALE_CLIENTID:
663 static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
665 struct nfs4_state *state;
666 struct nfs4_lock_state *lock;
669 /* Note: we rely on the sp->so_states list being ordered
670 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
672 * This is needed to ensure that the server won't give us any
673 * read delegations that we have to return if, say, we are
674 * recovering after a network partition or a reboot from a
675 * server that doesn't support a grace period.
677 list_for_each_entry(state, &sp->so_states, open_states) {
678 if (state->state == 0)
680 status = ops->recover_open(sp, state);
682 status = nfs4_reclaim_locks(ops, state);
685 list_for_each_entry(lock, &state->lock_states, ls_locks) {
686 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
687 printk("%s: Lock reclaim failed!\n",
694 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
695 __FUNCTION__, status);
697 case -NFS4ERR_RECLAIM_BAD:
698 case -NFS4ERR_RECLAIM_CONFLICT:
700 * Open state on this file cannot be recovered
701 * All we can do is revert to using the zero stateid.
703 memset(state->stateid.data, 0,
704 sizeof(state->stateid.data));
705 /* Mark the file as being 'closed' */
708 case -NFS4ERR_EXPIRED:
709 case -NFS4ERR_NO_GRACE:
710 case -NFS4ERR_STALE_CLIENTID:
719 static void nfs4_state_mark_reclaim(struct nfs_client *clp)
721 struct nfs4_state_owner *sp;
722 struct nfs4_state *state;
723 struct nfs4_lock_state *lock;
725 /* Reset all sequence ids to zero */
726 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
727 sp->so_seqid.counter = 0;
728 sp->so_seqid.flags = 0;
729 spin_lock(&sp->so_lock);
730 list_for_each_entry(state, &sp->so_states, open_states) {
731 list_for_each_entry(lock, &state->lock_states, ls_locks) {
732 lock->ls_seqid.counter = 0;
733 lock->ls_seqid.flags = 0;
734 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
737 spin_unlock(&sp->so_lock);
741 static int reclaimer(void *ptr)
743 struct nfs_client *clp = ptr;
744 struct nfs4_state_owner *sp;
745 struct nfs4_state_recovery_ops *ops;
746 struct rpc_cred *cred;
749 allow_signal(SIGKILL);
751 /* Ensure exclusive access to NFSv4 state */
753 down_write(&clp->cl_sem);
754 /* Are there any NFS mounts out there? */
755 if (list_empty(&clp->cl_superblocks))
758 ops = &nfs4_network_partition_recovery_ops;
759 /* Are there any open files on this volume? */
760 cred = nfs4_get_renew_cred(clp);
762 /* Yes there are: try to renew the old lease */
763 status = nfs4_proc_renew(clp, cred);
766 case -NFS4ERR_CB_PATH_DOWN:
769 case -NFS4ERR_STALE_CLIENTID:
770 case -NFS4ERR_LEASE_MOVED:
771 ops = &nfs4_reboot_recovery_ops;
774 /* "reboot" to ensure we clear all state on the server */
775 clp->cl_boot_time = CURRENT_TIME;
776 cred = nfs4_get_setclientid_cred(clp);
778 /* We're going to have to re-establish a clientid */
779 nfs4_state_mark_reclaim(clp);
782 status = nfs4_init_client(clp, cred);
787 /* Mark all delegations for reclaim */
788 nfs_delegation_mark_reclaim(clp);
789 /* Note: list is protected by exclusive lock on cl->cl_sem */
790 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
791 status = nfs4_reclaim_open_state(ops, sp);
793 if (status == -NFS4ERR_NO_GRACE) {
794 ops = &nfs4_network_partition_recovery_ops;
795 status = nfs4_reclaim_open_state(ops, sp);
797 if (status == -NFS4ERR_STALE_CLIENTID)
799 if (status == -NFS4ERR_EXPIRED)
803 nfs_delegation_reap_unclaimed(clp);
805 up_write(&clp->cl_sem);
807 if (status == -NFS4ERR_CB_PATH_DOWN)
808 nfs_handle_cb_pathdown(clp);
809 nfs4_clear_recover_bit(clp);
811 module_put_and_exit(0);
814 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
815 NIPQUAD(clp->cl_addr.sin_addr), -status);
816 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);