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/nfs_fs.h>
44 #include <linux/nfs_idmap.h>
45 #include <linux/workqueue.h>
46 #include <linux/bitops.h>
48 #define OPENOWNER_POOL_SIZE 8
50 static spinlock_t state_spinlock = SPIN_LOCK_UNLOCKED;
52 nfs4_stateid zero_stateid;
55 nfs4_stateid one_stateid =
56 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
59 static LIST_HEAD(nfs4_clientid_list);
61 static void nfs4_recover_state(void *);
62 extern void nfs4_renew_state(void *);
65 init_nfsv4_state(struct nfs_server *server)
67 server->nfs4_state = NULL;
68 INIT_LIST_HEAD(&server->nfs4_siblings);
72 destroy_nfsv4_state(struct nfs_server *server)
74 if (server->mnt_path) {
75 kfree(server->mnt_path);
76 server->mnt_path = NULL;
78 if (server->nfs4_state) {
79 nfs4_put_client(server->nfs4_state);
80 server->nfs4_state = NULL;
85 * nfs4_get_client(): returns an empty client structure
86 * nfs4_put_client(): drops reference to client structure
88 * Since these are allocated/deallocated very rarely, we don't
89 * bother putting them in a slab cache...
91 static struct nfs4_client *
92 nfs4_alloc_client(struct in_addr *addr)
94 struct nfs4_client *clp;
96 if ((clp = kmalloc(sizeof(*clp), GFP_KERNEL))) {
97 memset(clp, 0, sizeof(*clp));
98 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
99 init_rwsem(&clp->cl_sem);
100 INIT_LIST_HEAD(&clp->cl_state_owners);
101 INIT_LIST_HEAD(&clp->cl_unused);
102 spin_lock_init(&clp->cl_lock);
103 atomic_set(&clp->cl_count, 1);
104 INIT_WORK(&clp->cl_recoverd, nfs4_recover_state, clp);
105 INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
106 INIT_LIST_HEAD(&clp->cl_superblocks);
107 init_waitqueue_head(&clp->cl_waitq);
108 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS4 client");
109 clp->cl_state = 1 << NFS4CLNT_NEW;
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));
128 put_rpccred(clp->cl_cred);
129 nfs_idmap_delete(clp);
130 if (clp->cl_rpcclient)
131 rpc_shutdown_client(clp->cl_rpcclient);
136 nfs4_get_client(struct in_addr *addr)
138 struct nfs4_client *new, *clp = NULL;
140 new = nfs4_alloc_client(addr);
141 spin_lock(&state_spinlock);
142 list_for_each_entry(clp, &nfs4_clientid_list, cl_servers) {
143 if (memcmp(&clp->cl_addr, addr, sizeof(clp->cl_addr)) == 0)
147 list_add(&new->cl_servers, &nfs4_clientid_list);
148 spin_unlock(&state_spinlock);
151 atomic_inc(&clp->cl_count);
152 spin_unlock(&state_spinlock);
154 nfs4_free_client(new);
159 nfs4_put_client(struct nfs4_client *clp)
161 if (!atomic_dec_and_lock(&clp->cl_count, &state_spinlock))
163 list_del(&clp->cl_servers);
164 spin_unlock(&state_spinlock);
165 BUG_ON(!list_empty(&clp->cl_superblocks));
166 wake_up_all(&clp->cl_waitq);
167 rpc_wake_up(&clp->cl_rpcwaitq);
168 nfs4_kill_renewd(clp);
169 nfs4_free_client(clp);
173 nfs4_alloc_lockowner_id(struct nfs4_client *clp)
175 return clp->cl_lockowner_id ++;
178 static struct nfs4_state_owner *
179 nfs4_client_grab_unused(struct nfs4_client *clp, struct rpc_cred *cred)
181 struct nfs4_state_owner *sp = NULL;
183 if (!list_empty(&clp->cl_unused)) {
184 sp = list_entry(clp->cl_unused.next, struct nfs4_state_owner, so_list);
185 atomic_inc(&sp->so_count);
187 list_move(&sp->so_list, &clp->cl_state_owners);
188 sp->so_generation = clp->cl_generation;
194 static struct nfs4_state_owner *
195 nfs4_find_state_owner(struct nfs4_client *clp, struct rpc_cred *cred)
197 struct nfs4_state_owner *sp, *res = NULL;
199 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
200 if (sp->so_cred != cred)
202 atomic_inc(&sp->so_count);
203 /* Move to the head of the list */
204 list_move(&sp->so_list, &clp->cl_state_owners);
212 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
213 * create a new state_owner.
216 static struct nfs4_state_owner *
217 nfs4_alloc_state_owner(void)
219 struct nfs4_state_owner *sp;
221 sp = kmalloc(sizeof(*sp),GFP_KERNEL);
224 init_MUTEX(&sp->so_sema);
225 sp->so_seqid = 0; /* arbitrary */
226 INIT_LIST_HEAD(&sp->so_states);
227 atomic_set(&sp->so_count, 1);
232 nfs4_unhash_state_owner(struct nfs4_state_owner *sp)
234 struct nfs4_client *clp = sp->so_client;
235 spin_lock(&clp->cl_lock);
236 list_del_init(&sp->so_list);
237 spin_unlock(&clp->cl_lock);
240 struct nfs4_state_owner *
241 nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
243 struct nfs4_client *clp = server->nfs4_state;
244 struct nfs4_state_owner *sp, *new;
247 new = nfs4_alloc_state_owner();
248 spin_lock(&clp->cl_lock);
249 sp = nfs4_find_state_owner(clp, cred);
251 sp = nfs4_client_grab_unused(clp, cred);
252 if (sp == NULL && new != NULL) {
253 list_add(&new->so_list, &clp->cl_state_owners);
254 new->so_client = clp;
255 new->so_id = nfs4_alloc_lockowner_id(clp);
257 new->so_generation = clp->cl_generation;
261 spin_unlock(&clp->cl_lock);
265 if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
266 nfs4_wait_clnt_recover(server->client, clp);
273 nfs4_put_state_owner(struct nfs4_state_owner *sp)
275 struct nfs4_client *clp = sp->so_client;
276 struct rpc_cred *cred = sp->so_cred;
278 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
280 if (clp->cl_nunused >= OPENOWNER_POOL_SIZE)
282 if (list_empty(&sp->so_list))
284 list_move(&sp->so_list, &clp->cl_unused);
286 spin_unlock(&clp->cl_lock);
291 list_del(&sp->so_list);
292 spin_unlock(&clp->cl_lock);
297 static struct nfs4_state *
298 nfs4_alloc_open_state(void)
300 struct nfs4_state *state;
302 state = kmalloc(sizeof(*state), GFP_KERNEL);
309 memset(state->stateid.data, 0, sizeof(state->stateid.data));
310 atomic_set(&state->count, 1);
311 INIT_LIST_HEAD(&state->lock_states);
312 init_MUTEX(&state->lock_sema);
313 rwlock_init(&state->state_lock);
317 static struct nfs4_state *
318 __nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode)
320 struct nfs_inode *nfsi = NFS_I(inode);
321 struct nfs4_state *state;
323 mode &= (FMODE_READ|FMODE_WRITE);
324 list_for_each_entry(state, &nfsi->open_states, inode_states) {
325 if (state->owner->so_cred != cred)
327 if ((mode & FMODE_READ) != 0 && state->nreaders == 0)
329 if ((mode & FMODE_WRITE) != 0 && state->nwriters == 0)
331 if ((state->state & mode) != mode)
333 /* Add the state to the head of the inode's list */
334 list_move(&state->inode_states, &nfsi->open_states);
335 atomic_inc(&state->count);
336 if (mode & FMODE_READ)
338 if (mode & FMODE_WRITE)
345 static struct nfs4_state *
346 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
348 struct nfs_inode *nfsi = NFS_I(inode);
349 struct nfs4_state *state;
351 list_for_each_entry(state, &nfsi->open_states, inode_states) {
352 /* Is this in the process of being freed? */
353 if (state->nreaders == 0 && state->nwriters == 0)
355 if (state->owner == owner) {
356 /* Add the state to the head of the inode's list */
357 list_move(&state->inode_states, &nfsi->open_states);
358 atomic_inc(&state->count);
366 nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode)
368 struct nfs4_state *state;
370 spin_lock(&inode->i_lock);
371 state = __nfs4_find_state(inode, cred, mode);
372 spin_unlock(&inode->i_lock);
377 nfs4_free_open_state(struct nfs4_state *state)
383 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
385 struct nfs4_state *state, *new;
386 struct nfs_inode *nfsi = NFS_I(inode);
388 spin_lock(&inode->i_lock);
389 state = __nfs4_find_state_byowner(inode, owner);
390 spin_unlock(&inode->i_lock);
393 new = nfs4_alloc_open_state();
394 spin_lock(&inode->i_lock);
395 state = __nfs4_find_state_byowner(inode, owner);
396 if (state == NULL && new != NULL) {
398 /* Caller *must* be holding owner->so_sem */
399 list_add(&state->open_states, &owner->so_states);
400 state->owner = owner;
401 atomic_inc(&owner->so_count);
402 list_add(&state->inode_states, &nfsi->open_states);
403 state->inode = inode;
404 spin_unlock(&inode->i_lock);
406 spin_unlock(&inode->i_lock);
408 nfs4_free_open_state(new);
415 __nfs4_put_open_state(struct nfs4_state *state)
417 struct inode *inode = state->inode;
418 struct nfs4_state_owner *owner = state->owner;
421 if (!atomic_dec_and_lock(&state->count, &inode->i_lock)) {
425 if (!list_empty(&state->inode_states))
426 list_del(&state->inode_states);
427 spin_unlock(&inode->i_lock);
428 list_del(&state->open_states);
429 if (state->state != 0) {
431 status = nfs4_do_close(inode, state);
435 status = nfs4_handle_error(NFS_SERVER(inode), status);
436 down(&owner->so_sema);
440 nfs4_free_open_state(state);
441 nfs4_put_state_owner(owner);
445 nfs4_put_open_state(struct nfs4_state *state)
447 down(&state->owner->so_sema);
448 __nfs4_put_open_state(state);
452 nfs4_close_state(struct nfs4_state *state, mode_t mode)
454 struct inode *inode = state->inode;
455 struct nfs4_state_owner *owner = state->owner;
459 down(&owner->so_sema);
460 /* Protect against nfs4_find_state() */
461 spin_lock(&inode->i_lock);
462 if (mode & FMODE_READ)
464 if (mode & FMODE_WRITE)
466 if (state->nwriters == 0 && state->nreaders == 0)
467 list_del_init(&state->inode_states);
468 spin_unlock(&inode->i_lock);
471 if (state->state == 0)
474 newstate |= FMODE_READ;
476 newstate |= FMODE_WRITE;
477 if (state->state == newstate)
480 status = nfs4_do_downgrade(inode, state, newstate);
482 status = nfs4_do_close(inode, state);
484 state->state = newstate;
488 status = nfs4_handle_error(NFS_SERVER(inode), status);
489 down(&owner->so_sema);
491 __nfs4_put_open_state(state);
495 * Search the state->lock_states for an existing lock_owner
496 * that is compatible with current->files
498 static struct nfs4_lock_state *
499 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
501 struct nfs4_lock_state *pos;
502 list_for_each_entry(pos, &state->lock_states, ls_locks) {
503 if (pos->ls_owner != fl_owner)
505 atomic_inc(&pos->ls_count);
511 struct nfs4_lock_state *
512 nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
514 struct nfs4_lock_state *lsp;
515 read_lock(&state->state_lock);
516 lsp = __nfs4_find_lock_state(state, fl_owner);
517 read_unlock(&state->state_lock);
522 * Return a compatible lock_state. If no initialized lock_state structure
523 * exists, return an uninitialized one.
525 * The caller must be holding state->lock_sema
527 struct nfs4_lock_state *
528 nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
530 struct nfs4_lock_state *lsp;
531 struct nfs4_client *clp = state->owner->so_client;
533 lsp = kmalloc(sizeof(*lsp), GFP_KERNEL);
536 lsp->ls_seqid = 0; /* arbitrary */
538 memset(lsp->ls_stateid.data, 0, sizeof(lsp->ls_stateid.data));
539 atomic_set(&lsp->ls_count, 1);
540 lsp->ls_owner = fl_owner;
541 lsp->ls_parent = state;
542 INIT_LIST_HEAD(&lsp->ls_locks);
543 spin_lock(&clp->cl_lock);
544 lsp->ls_id = nfs4_alloc_lockowner_id(clp);
545 spin_unlock(&clp->cl_lock);
550 * Byte-range lock aware utility to initialize the stateid of read/write
554 nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
556 if (test_bit(LK_STATE_IN_USE, &state->flags)) {
557 struct nfs4_lock_state *lsp;
559 lsp = nfs4_find_lock_state(state, fl_owner);
561 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
562 nfs4_put_lock_state(lsp);
566 memcpy(dst, &state->stateid, sizeof(*dst));
570 * Called with state->lock_sema held.
573 nfs4_increment_lock_seqid(int status, struct nfs4_lock_state *lsp)
575 if (status == NFS_OK || seqid_mutating_err(-status))
580 * Check to see if the request lock (type FL_UNLK) effects the fl lock.
582 * fl and request must have the same posix owner
585 * 0 -> fl not effected by request
586 * 1 -> fl consumed by request
590 nfs4_check_unlock(struct file_lock *fl, struct file_lock *request)
592 if (fl->fl_start >= request->fl_start && fl->fl_end <= request->fl_end)
598 * Post an initialized lock_state on the state->lock_states list.
601 nfs4_notify_setlk(struct inode *inode, struct file_lock *request, struct nfs4_lock_state *lsp)
603 struct nfs4_state *state = lsp->ls_parent;
605 if (!list_empty(&lsp->ls_locks))
607 write_lock(&state->state_lock);
608 list_add(&lsp->ls_locks, &state->lock_states);
609 set_bit(LK_STATE_IN_USE, &state->flags);
610 write_unlock(&state->state_lock);
614 * to decide to 'reap' lock state:
615 * 1) search i_flock for file_locks with fl.lock_state = to ls.
616 * 2) determine if unlock will consume found lock.
623 nfs4_notify_unlck(struct inode *inode, struct file_lock *request, struct nfs4_lock_state *lsp)
625 struct nfs4_state *state = lsp->ls_parent;
626 struct file_lock *fl;
628 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
629 if (!(fl->fl_flags & FL_POSIX))
631 if (fl->fl_owner != lsp->ls_owner)
633 /* Exit if we find at least one lock which is not consumed */
634 if (nfs4_check_unlock(fl,request) == 0)
638 write_lock(&state->state_lock);
639 list_del_init(&lsp->ls_locks);
640 if (list_empty(&state->lock_states))
641 clear_bit(LK_STATE_IN_USE, &state->flags);
642 write_unlock(&state->state_lock);
646 * Release reference to lock_state, and free it if we see that
647 * it is no longer in use
650 nfs4_put_lock_state(struct nfs4_lock_state *lsp)
652 if (!atomic_dec_and_test(&lsp->ls_count))
654 if (!list_empty(&lsp->ls_locks))
660 * Called with sp->so_sema held.
662 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
663 * failed with a seqid incrementing error -
664 * see comments nfs_fs.h:seqid_mutating_error()
667 nfs4_increment_seqid(int status, struct nfs4_state_owner *sp)
669 if (status == NFS_OK || seqid_mutating_err(-status))
671 /* If the server returns BAD_SEQID, unhash state_owner here */
672 if (status == -NFS4ERR_BAD_SEQID)
673 nfs4_unhash_state_owner(sp);
676 static int reclaimer(void *);
677 struct reclaimer_args {
678 struct nfs4_client *clp;
679 struct completion complete;
683 * State recovery routine
686 nfs4_recover_state(void *data)
688 struct nfs4_client *clp = (struct nfs4_client *)data;
689 struct reclaimer_args args = {
694 init_completion(&args.complete);
696 down_read(&clp->cl_sem);
697 if (test_and_set_bit(NFS4CLNT_SETUP_STATE, &clp->cl_state))
699 if (kernel_thread(reclaimer, &args, CLONE_KERNEL) < 0)
700 goto out_failed_clear;
701 wait_for_completion(&args.complete);
704 smp_mb__before_clear_bit();
705 clear_bit(NFS4CLNT_SETUP_STATE, &clp->cl_state);
706 smp_mb__after_clear_bit();
707 wake_up_all(&clp->cl_waitq);
708 rpc_wake_up(&clp->cl_rpcwaitq);
710 up_read(&clp->cl_sem);
714 * Schedule a state recovery attempt
717 nfs4_schedule_state_recovery(struct nfs4_client *clp)
721 smp_mb__before_clear_bit();
722 clear_bit(NFS4CLNT_OK, &clp->cl_state);
723 smp_mb__after_clear_bit();
724 schedule_work(&clp->cl_recoverd);
728 nfs4_reclaim_open_state(struct nfs4_state_owner *sp)
730 struct nfs4_state *state;
733 list_for_each_entry(state, &sp->so_states, open_states) {
734 status = nfs4_open_reclaim(sp, state);
739 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
740 __FUNCTION__, status);
741 case -NFS4ERR_EXPIRED:
742 case -NFS4ERR_NO_GRACE:
743 case -NFS4ERR_RECLAIM_BAD:
744 case -NFS4ERR_RECLAIM_CONFLICT:
746 * Open state on this file cannot be recovered
747 * All we can do is revert to using the zero stateid.
749 memset(state->stateid.data, 0,
750 sizeof(state->stateid.data));
751 /* Mark the file as being 'closed' */
754 case -NFS4ERR_STALE_CLIENTID:
766 struct reclaimer_args *args = (struct reclaimer_args *)ptr;
767 struct nfs4_client *clp = args->clp;
768 struct nfs4_state_owner *sp;
772 daemonize("%u.%u.%u.%u-reclaim", NIPQUAD(clp->cl_addr));
773 allow_signal(SIGKILL);
775 complete(&args->complete);
777 /* Are there any NFS mounts out there? */
778 if (list_empty(&clp->cl_superblocks))
780 if (!test_bit(NFS4CLNT_NEW, &clp->cl_state)) {
781 status = nfs4_proc_renew(clp);
783 set_bit(NFS4CLNT_OK, &clp->cl_state);
787 status = nfs4_proc_setclientid(clp, 0, 0);
790 status = nfs4_proc_setclientid_confirm(clp);
793 generation = ++(clp->cl_generation);
794 clear_bit(NFS4CLNT_NEW, &clp->cl_state);
795 set_bit(NFS4CLNT_OK, &clp->cl_state);
796 up_read(&clp->cl_sem);
797 nfs4_schedule_state_renewal(clp);
799 spin_lock(&clp->cl_lock);
800 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
801 if (sp->so_generation - generation >= 0)
803 atomic_inc(&sp->so_count);
804 spin_unlock(&clp->cl_lock);
806 if (sp->so_generation - generation < 0) {
808 sp->so_generation = clp->cl_generation;
809 status = nfs4_reclaim_open_state(sp);
812 nfs4_put_state_owner(sp);
814 if (status == -NFS4ERR_STALE_CLIENTID)
815 nfs4_schedule_state_recovery(clp);
820 spin_unlock(&clp->cl_lock);
822 smp_mb__before_clear_bit();
823 clear_bit(NFS4CLNT_SETUP_STATE, &clp->cl_state);
824 smp_mb__after_clear_bit();
825 wake_up_all(&clp->cl_waitq);
826 rpc_wake_up(&clp->cl_rpcwaitq);
829 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u\n",
830 NIPQUAD(clp->cl_addr.s_addr));
831 up_read(&clp->cl_sem);