[PATCH] briq_panel: read() and write() get __user pointers, damnit
[linux-2.6/verdex.git] / fs / nfs / nfs4state.c
blob090a36b07a2231a966eb01d3af19fe59d68b43e6
1 /*
2 * fs/nfs/nfs4state.c
4 * Client-side XDR for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
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
13 * are met:
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
38 * subsequent patch.
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>
50 #include "nfs4_fs.h"
51 #include "callback.h"
52 #include "delegation.h"
54 #define OPENOWNER_POOL_SIZE 8
56 const nfs4_stateid zero_stateid;
58 static DEFINE_SPINLOCK(state_spinlock);
59 static LIST_HEAD(nfs4_clientid_list);
61 void
62 init_nfsv4_state(struct nfs_server *server)
64 server->nfs4_state = NULL;
65 INIT_LIST_HEAD(&server->nfs4_siblings);
68 void
69 destroy_nfsv4_state(struct nfs_server *server)
71 kfree(server->mnt_path);
72 server->mnt_path = NULL;
73 if (server->nfs4_state) {
74 nfs4_put_client(server->nfs4_state);
75 server->nfs4_state = NULL;
80 * nfs4_get_client(): returns an empty client structure
81 * nfs4_put_client(): drops reference to client structure
83 * Since these are allocated/deallocated very rarely, we don't
84 * bother putting them in a slab cache...
86 static struct nfs4_client *
87 nfs4_alloc_client(struct in_addr *addr)
89 struct nfs4_client *clp;
91 if (nfs_callback_up() < 0)
92 return NULL;
93 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) {
94 nfs_callback_down();
95 return NULL;
97 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
98 init_rwsem(&clp->cl_sem);
99 INIT_LIST_HEAD(&clp->cl_delegations);
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_renewd, nfs4_renew_state, clp);
105 INIT_LIST_HEAD(&clp->cl_superblocks);
106 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS4 client");
107 clp->cl_rpcclient = ERR_PTR(-EINVAL);
108 clp->cl_boot_time = CURRENT_TIME;
109 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
110 return clp;
113 static void
114 nfs4_free_client(struct nfs4_client *clp)
116 struct nfs4_state_owner *sp;
118 while (!list_empty(&clp->cl_unused)) {
119 sp = list_entry(clp->cl_unused.next,
120 struct nfs4_state_owner,
121 so_list);
122 list_del(&sp->so_list);
123 kfree(sp);
125 BUG_ON(!list_empty(&clp->cl_state_owners));
126 nfs_idmap_delete(clp);
127 if (!IS_ERR(clp->cl_rpcclient))
128 rpc_shutdown_client(clp->cl_rpcclient);
129 kfree(clp);
130 nfs_callback_down();
133 static struct nfs4_client *__nfs4_find_client(struct in_addr *addr)
135 struct nfs4_client *clp;
136 list_for_each_entry(clp, &nfs4_clientid_list, cl_servers) {
137 if (memcmp(&clp->cl_addr, addr, sizeof(clp->cl_addr)) == 0) {
138 atomic_inc(&clp->cl_count);
139 return clp;
142 return NULL;
145 struct nfs4_client *nfs4_find_client(struct in_addr *addr)
147 struct nfs4_client *clp;
148 spin_lock(&state_spinlock);
149 clp = __nfs4_find_client(addr);
150 spin_unlock(&state_spinlock);
151 return clp;
154 struct nfs4_client *
155 nfs4_get_client(struct in_addr *addr)
157 struct nfs4_client *clp, *new = NULL;
159 spin_lock(&state_spinlock);
160 for (;;) {
161 clp = __nfs4_find_client(addr);
162 if (clp != NULL)
163 break;
164 clp = new;
165 if (clp != NULL) {
166 list_add(&clp->cl_servers, &nfs4_clientid_list);
167 new = NULL;
168 break;
170 spin_unlock(&state_spinlock);
171 new = nfs4_alloc_client(addr);
172 spin_lock(&state_spinlock);
173 if (new == NULL)
174 break;
176 spin_unlock(&state_spinlock);
177 if (new)
178 nfs4_free_client(new);
179 return clp;
182 void
183 nfs4_put_client(struct nfs4_client *clp)
185 if (!atomic_dec_and_lock(&clp->cl_count, &state_spinlock))
186 return;
187 list_del(&clp->cl_servers);
188 spin_unlock(&state_spinlock);
189 BUG_ON(!list_empty(&clp->cl_superblocks));
190 rpc_wake_up(&clp->cl_rpcwaitq);
191 nfs4_kill_renewd(clp);
192 nfs4_free_client(clp);
195 static int nfs4_init_client(struct nfs4_client *clp, struct rpc_cred *cred)
197 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
198 nfs_callback_tcpport, cred);
199 if (status == 0)
200 status = nfs4_proc_setclientid_confirm(clp, cred);
201 if (status == 0)
202 nfs4_schedule_state_renewal(clp);
203 return status;
207 nfs4_alloc_lockowner_id(struct nfs4_client *clp)
209 return clp->cl_lockowner_id ++;
212 static struct nfs4_state_owner *
213 nfs4_client_grab_unused(struct nfs4_client *clp, struct rpc_cred *cred)
215 struct nfs4_state_owner *sp = NULL;
217 if (!list_empty(&clp->cl_unused)) {
218 sp = list_entry(clp->cl_unused.next, struct nfs4_state_owner, so_list);
219 atomic_inc(&sp->so_count);
220 sp->so_cred = cred;
221 list_move(&sp->so_list, &clp->cl_state_owners);
222 clp->cl_nunused--;
224 return sp;
227 struct rpc_cred *nfs4_get_renew_cred(struct nfs4_client *clp)
229 struct nfs4_state_owner *sp;
230 struct rpc_cred *cred = NULL;
232 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
233 if (list_empty(&sp->so_states))
234 continue;
235 cred = get_rpccred(sp->so_cred);
236 break;
238 return cred;
241 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs4_client *clp)
243 struct nfs4_state_owner *sp;
245 if (!list_empty(&clp->cl_state_owners)) {
246 sp = list_entry(clp->cl_state_owners.next,
247 struct nfs4_state_owner, so_list);
248 return get_rpccred(sp->so_cred);
250 return NULL;
253 static struct nfs4_state_owner *
254 nfs4_find_state_owner(struct nfs4_client *clp, struct rpc_cred *cred)
256 struct nfs4_state_owner *sp, *res = NULL;
258 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
259 if (sp->so_cred != cred)
260 continue;
261 atomic_inc(&sp->so_count);
262 /* Move to the head of the list */
263 list_move(&sp->so_list, &clp->cl_state_owners);
264 res = sp;
265 break;
267 return res;
271 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
272 * create a new state_owner.
275 static struct nfs4_state_owner *
276 nfs4_alloc_state_owner(void)
278 struct nfs4_state_owner *sp;
280 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
281 if (!sp)
282 return NULL;
283 spin_lock_init(&sp->so_lock);
284 INIT_LIST_HEAD(&sp->so_states);
285 INIT_LIST_HEAD(&sp->so_delegations);
286 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
287 sp->so_seqid.sequence = &sp->so_sequence;
288 spin_lock_init(&sp->so_sequence.lock);
289 INIT_LIST_HEAD(&sp->so_sequence.list);
290 atomic_set(&sp->so_count, 1);
291 return sp;
294 void
295 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
297 struct nfs4_client *clp = sp->so_client;
298 spin_lock(&clp->cl_lock);
299 list_del_init(&sp->so_list);
300 spin_unlock(&clp->cl_lock);
304 * Note: must be called with clp->cl_sem held in order to prevent races
305 * with reboot recovery!
307 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
309 struct nfs4_client *clp = server->nfs4_state;
310 struct nfs4_state_owner *sp, *new;
312 get_rpccred(cred);
313 new = nfs4_alloc_state_owner();
314 spin_lock(&clp->cl_lock);
315 sp = nfs4_find_state_owner(clp, cred);
316 if (sp == NULL)
317 sp = nfs4_client_grab_unused(clp, cred);
318 if (sp == NULL && new != NULL) {
319 list_add(&new->so_list, &clp->cl_state_owners);
320 new->so_client = clp;
321 new->so_id = nfs4_alloc_lockowner_id(clp);
322 new->so_cred = cred;
323 sp = new;
324 new = NULL;
326 spin_unlock(&clp->cl_lock);
327 kfree(new);
328 if (sp != NULL)
329 return sp;
330 put_rpccred(cred);
331 return NULL;
335 * Must be called with clp->cl_sem held in order to avoid races
336 * with state recovery...
338 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
340 struct nfs4_client *clp = sp->so_client;
341 struct rpc_cred *cred = sp->so_cred;
343 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
344 return;
345 if (clp->cl_nunused >= OPENOWNER_POOL_SIZE)
346 goto out_free;
347 if (list_empty(&sp->so_list))
348 goto out_free;
349 list_move(&sp->so_list, &clp->cl_unused);
350 clp->cl_nunused++;
351 spin_unlock(&clp->cl_lock);
352 put_rpccred(cred);
353 cred = NULL;
354 return;
355 out_free:
356 list_del(&sp->so_list);
357 spin_unlock(&clp->cl_lock);
358 put_rpccred(cred);
359 kfree(sp);
362 static struct nfs4_state *
363 nfs4_alloc_open_state(void)
365 struct nfs4_state *state;
367 state = kzalloc(sizeof(*state), GFP_KERNEL);
368 if (!state)
369 return NULL;
370 atomic_set(&state->count, 1);
371 INIT_LIST_HEAD(&state->lock_states);
372 spin_lock_init(&state->state_lock);
373 return state;
376 void
377 nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
379 if (state->state == mode)
380 return;
381 /* NB! List reordering - see the reclaim code for why. */
382 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
383 if (mode & FMODE_WRITE)
384 list_move(&state->open_states, &state->owner->so_states);
385 else
386 list_move_tail(&state->open_states, &state->owner->so_states);
388 if (mode == 0)
389 list_del_init(&state->inode_states);
390 state->state = mode;
393 static struct nfs4_state *
394 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
396 struct nfs_inode *nfsi = NFS_I(inode);
397 struct nfs4_state *state;
399 list_for_each_entry(state, &nfsi->open_states, inode_states) {
400 /* Is this in the process of being freed? */
401 if (state->state == 0)
402 continue;
403 if (state->owner == owner) {
404 atomic_inc(&state->count);
405 return state;
408 return NULL;
411 static void
412 nfs4_free_open_state(struct nfs4_state *state)
414 kfree(state);
417 struct nfs4_state *
418 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
420 struct nfs4_state *state, *new;
421 struct nfs_inode *nfsi = NFS_I(inode);
423 spin_lock(&inode->i_lock);
424 state = __nfs4_find_state_byowner(inode, owner);
425 spin_unlock(&inode->i_lock);
426 if (state)
427 goto out;
428 new = nfs4_alloc_open_state();
429 spin_lock(&owner->so_lock);
430 spin_lock(&inode->i_lock);
431 state = __nfs4_find_state_byowner(inode, owner);
432 if (state == NULL && new != NULL) {
433 state = new;
434 state->owner = owner;
435 atomic_inc(&owner->so_count);
436 list_add(&state->inode_states, &nfsi->open_states);
437 state->inode = igrab(inode);
438 spin_unlock(&inode->i_lock);
439 /* Note: The reclaim code dictates that we add stateless
440 * and read-only stateids to the end of the list */
441 list_add_tail(&state->open_states, &owner->so_states);
442 spin_unlock(&owner->so_lock);
443 } else {
444 spin_unlock(&inode->i_lock);
445 spin_unlock(&owner->so_lock);
446 if (new)
447 nfs4_free_open_state(new);
449 out:
450 return state;
454 * Beware! Caller must be holding exactly one
455 * reference to clp->cl_sem!
457 void nfs4_put_open_state(struct nfs4_state *state)
459 struct inode *inode = state->inode;
460 struct nfs4_state_owner *owner = state->owner;
462 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
463 return;
464 spin_lock(&inode->i_lock);
465 if (!list_empty(&state->inode_states))
466 list_del(&state->inode_states);
467 list_del(&state->open_states);
468 spin_unlock(&inode->i_lock);
469 spin_unlock(&owner->so_lock);
470 iput(inode);
471 nfs4_free_open_state(state);
472 nfs4_put_state_owner(owner);
476 * Close the current file.
478 void nfs4_close_state(struct nfs4_state *state, mode_t mode)
480 struct inode *inode = state->inode;
481 struct nfs4_state_owner *owner = state->owner;
482 int oldstate, newstate = 0;
484 atomic_inc(&owner->so_count);
485 /* Protect against nfs4_find_state() */
486 spin_lock(&owner->so_lock);
487 spin_lock(&inode->i_lock);
488 switch (mode & (FMODE_READ | FMODE_WRITE)) {
489 case FMODE_READ:
490 state->n_rdonly--;
491 break;
492 case FMODE_WRITE:
493 state->n_wronly--;
494 break;
495 case FMODE_READ|FMODE_WRITE:
496 state->n_rdwr--;
498 oldstate = newstate = state->state;
499 if (state->n_rdwr == 0) {
500 if (state->n_rdonly == 0)
501 newstate &= ~FMODE_READ;
502 if (state->n_wronly == 0)
503 newstate &= ~FMODE_WRITE;
505 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
506 nfs4_state_set_mode_locked(state, newstate);
507 oldstate = newstate;
509 spin_unlock(&inode->i_lock);
510 spin_unlock(&owner->so_lock);
512 if (oldstate != newstate && nfs4_do_close(inode, state) == 0)
513 return;
514 nfs4_put_open_state(state);
515 nfs4_put_state_owner(owner);
519 * Search the state->lock_states for an existing lock_owner
520 * that is compatible with current->files
522 static struct nfs4_lock_state *
523 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
525 struct nfs4_lock_state *pos;
526 list_for_each_entry(pos, &state->lock_states, ls_locks) {
527 if (pos->ls_owner != fl_owner)
528 continue;
529 atomic_inc(&pos->ls_count);
530 return pos;
532 return NULL;
536 * Return a compatible lock_state. If no initialized lock_state structure
537 * exists, return an uninitialized one.
540 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
542 struct nfs4_lock_state *lsp;
543 struct nfs4_client *clp = state->owner->so_client;
545 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
546 if (lsp == NULL)
547 return NULL;
548 lsp->ls_seqid.sequence = &state->owner->so_sequence;
549 atomic_set(&lsp->ls_count, 1);
550 lsp->ls_owner = fl_owner;
551 spin_lock(&clp->cl_lock);
552 lsp->ls_id = nfs4_alloc_lockowner_id(clp);
553 spin_unlock(&clp->cl_lock);
554 INIT_LIST_HEAD(&lsp->ls_locks);
555 return lsp;
559 * Return a compatible lock_state. If no initialized lock_state structure
560 * exists, return an uninitialized one.
562 * The caller must be holding clp->cl_sem
564 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
566 struct nfs4_lock_state *lsp, *new = NULL;
568 for(;;) {
569 spin_lock(&state->state_lock);
570 lsp = __nfs4_find_lock_state(state, owner);
571 if (lsp != NULL)
572 break;
573 if (new != NULL) {
574 new->ls_state = state;
575 list_add(&new->ls_locks, &state->lock_states);
576 set_bit(LK_STATE_IN_USE, &state->flags);
577 lsp = new;
578 new = NULL;
579 break;
581 spin_unlock(&state->state_lock);
582 new = nfs4_alloc_lock_state(state, owner);
583 if (new == NULL)
584 return NULL;
586 spin_unlock(&state->state_lock);
587 kfree(new);
588 return lsp;
592 * Release reference to lock_state, and free it if we see that
593 * it is no longer in use
595 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
597 struct nfs4_state *state;
599 if (lsp == NULL)
600 return;
601 state = lsp->ls_state;
602 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
603 return;
604 list_del(&lsp->ls_locks);
605 if (list_empty(&state->lock_states))
606 clear_bit(LK_STATE_IN_USE, &state->flags);
607 spin_unlock(&state->state_lock);
608 kfree(lsp);
611 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
613 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
615 dst->fl_u.nfs4_fl.owner = lsp;
616 atomic_inc(&lsp->ls_count);
619 static void nfs4_fl_release_lock(struct file_lock *fl)
621 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
624 static struct file_lock_operations nfs4_fl_lock_ops = {
625 .fl_copy_lock = nfs4_fl_copy_lock,
626 .fl_release_private = nfs4_fl_release_lock,
629 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
631 struct nfs4_lock_state *lsp;
633 if (fl->fl_ops != NULL)
634 return 0;
635 lsp = nfs4_get_lock_state(state, fl->fl_owner);
636 if (lsp == NULL)
637 return -ENOMEM;
638 fl->fl_u.nfs4_fl.owner = lsp;
639 fl->fl_ops = &nfs4_fl_lock_ops;
640 return 0;
644 * Byte-range lock aware utility to initialize the stateid of read/write
645 * requests.
647 void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
649 struct nfs4_lock_state *lsp;
651 memcpy(dst, &state->stateid, sizeof(*dst));
652 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
653 return;
655 spin_lock(&state->state_lock);
656 lsp = __nfs4_find_lock_state(state, fl_owner);
657 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
658 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
659 spin_unlock(&state->state_lock);
660 nfs4_put_lock_state(lsp);
663 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
665 struct rpc_sequence *sequence = counter->sequence;
666 struct nfs_seqid *new;
668 new = kmalloc(sizeof(*new), GFP_KERNEL);
669 if (new != NULL) {
670 new->sequence = counter;
671 spin_lock(&sequence->lock);
672 list_add_tail(&new->list, &sequence->list);
673 spin_unlock(&sequence->lock);
675 return new;
678 void nfs_free_seqid(struct nfs_seqid *seqid)
680 struct rpc_sequence *sequence = seqid->sequence->sequence;
682 spin_lock(&sequence->lock);
683 list_del(&seqid->list);
684 spin_unlock(&sequence->lock);
685 rpc_wake_up(&sequence->wait);
686 kfree(seqid);
690 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
691 * failed with a seqid incrementing error -
692 * see comments nfs_fs.h:seqid_mutating_error()
694 static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
696 switch (status) {
697 case 0:
698 break;
699 case -NFS4ERR_BAD_SEQID:
700 case -NFS4ERR_STALE_CLIENTID:
701 case -NFS4ERR_STALE_STATEID:
702 case -NFS4ERR_BAD_STATEID:
703 case -NFS4ERR_BADXDR:
704 case -NFS4ERR_RESOURCE:
705 case -NFS4ERR_NOFILEHANDLE:
706 /* Non-seqid mutating errors */
707 return;
710 * Note: no locking needed as we are guaranteed to be first
711 * on the sequence list
713 seqid->sequence->counter++;
716 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
718 if (status == -NFS4ERR_BAD_SEQID) {
719 struct nfs4_state_owner *sp = container_of(seqid->sequence,
720 struct nfs4_state_owner, so_seqid);
721 nfs4_drop_state_owner(sp);
723 return nfs_increment_seqid(status, seqid);
727 * Increment the seqid if the LOCK/LOCKU succeeded, or
728 * failed with a seqid incrementing error -
729 * see comments nfs_fs.h:seqid_mutating_error()
731 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
733 return nfs_increment_seqid(status, seqid);
736 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
738 struct rpc_sequence *sequence = seqid->sequence->sequence;
739 int status = 0;
741 if (sequence->list.next == &seqid->list)
742 goto out;
743 spin_lock(&sequence->lock);
744 if (sequence->list.next != &seqid->list) {
745 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
746 status = -EAGAIN;
748 spin_unlock(&sequence->lock);
749 out:
750 return status;
753 static int reclaimer(void *);
755 static inline void nfs4_clear_recover_bit(struct nfs4_client *clp)
757 smp_mb__before_clear_bit();
758 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
759 smp_mb__after_clear_bit();
760 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
761 rpc_wake_up(&clp->cl_rpcwaitq);
765 * State recovery routine
767 static void nfs4_recover_state(struct nfs4_client *clp)
769 struct task_struct *task;
771 __module_get(THIS_MODULE);
772 atomic_inc(&clp->cl_count);
773 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
774 NIPQUAD(clp->cl_addr));
775 if (!IS_ERR(task))
776 return;
777 nfs4_clear_recover_bit(clp);
778 nfs4_put_client(clp);
779 module_put(THIS_MODULE);
783 * Schedule a state recovery attempt
785 void nfs4_schedule_state_recovery(struct nfs4_client *clp)
787 if (!clp)
788 return;
789 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
790 nfs4_recover_state(clp);
793 static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
795 struct inode *inode = state->inode;
796 struct file_lock *fl;
797 int status = 0;
799 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
800 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
801 continue;
802 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
803 continue;
804 status = ops->recover_lock(state, fl);
805 if (status >= 0)
806 continue;
807 switch (status) {
808 default:
809 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
810 __FUNCTION__, status);
811 case -NFS4ERR_EXPIRED:
812 case -NFS4ERR_NO_GRACE:
813 case -NFS4ERR_RECLAIM_BAD:
814 case -NFS4ERR_RECLAIM_CONFLICT:
815 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
816 break;
817 case -NFS4ERR_STALE_CLIENTID:
818 goto out_err;
821 return 0;
822 out_err:
823 return status;
826 static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
828 struct nfs4_state *state;
829 struct nfs4_lock_state *lock;
830 int status = 0;
832 /* Note: we rely on the sp->so_states list being ordered
833 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
834 * states first.
835 * This is needed to ensure that the server won't give us any
836 * read delegations that we have to return if, say, we are
837 * recovering after a network partition or a reboot from a
838 * server that doesn't support a grace period.
840 list_for_each_entry(state, &sp->so_states, open_states) {
841 if (state->state == 0)
842 continue;
843 status = ops->recover_open(sp, state);
844 if (status >= 0) {
845 status = nfs4_reclaim_locks(ops, state);
846 if (status < 0)
847 goto out_err;
848 list_for_each_entry(lock, &state->lock_states, ls_locks) {
849 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
850 printk("%s: Lock reclaim failed!\n",
851 __FUNCTION__);
853 continue;
855 switch (status) {
856 default:
857 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
858 __FUNCTION__, status);
859 case -ENOENT:
860 case -NFS4ERR_RECLAIM_BAD:
861 case -NFS4ERR_RECLAIM_CONFLICT:
863 * Open state on this file cannot be recovered
864 * All we can do is revert to using the zero stateid.
866 memset(state->stateid.data, 0,
867 sizeof(state->stateid.data));
868 /* Mark the file as being 'closed' */
869 state->state = 0;
870 break;
871 case -NFS4ERR_EXPIRED:
872 case -NFS4ERR_NO_GRACE:
873 case -NFS4ERR_STALE_CLIENTID:
874 goto out_err;
877 return 0;
878 out_err:
879 return status;
882 static void nfs4_state_mark_reclaim(struct nfs4_client *clp)
884 struct nfs4_state_owner *sp;
885 struct nfs4_state *state;
886 struct nfs4_lock_state *lock;
888 /* Reset all sequence ids to zero */
889 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
890 sp->so_seqid.counter = 0;
891 sp->so_seqid.flags = 0;
892 spin_lock(&sp->so_lock);
893 list_for_each_entry(state, &sp->so_states, open_states) {
894 list_for_each_entry(lock, &state->lock_states, ls_locks) {
895 lock->ls_seqid.counter = 0;
896 lock->ls_seqid.flags = 0;
897 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
900 spin_unlock(&sp->so_lock);
904 static int reclaimer(void *ptr)
906 struct nfs4_client *clp = ptr;
907 struct nfs4_state_owner *sp;
908 struct nfs4_state_recovery_ops *ops;
909 struct rpc_cred *cred;
910 int status = 0;
912 allow_signal(SIGKILL);
914 /* Ensure exclusive access to NFSv4 state */
915 lock_kernel();
916 down_write(&clp->cl_sem);
917 /* Are there any NFS mounts out there? */
918 if (list_empty(&clp->cl_superblocks))
919 goto out;
920 restart_loop:
921 ops = &nfs4_network_partition_recovery_ops;
922 /* Are there any open files on this volume? */
923 cred = nfs4_get_renew_cred(clp);
924 if (cred != NULL) {
925 /* Yes there are: try to renew the old lease */
926 status = nfs4_proc_renew(clp, cred);
927 switch (status) {
928 case 0:
929 case -NFS4ERR_CB_PATH_DOWN:
930 put_rpccred(cred);
931 goto out;
932 case -NFS4ERR_STALE_CLIENTID:
933 case -NFS4ERR_LEASE_MOVED:
934 ops = &nfs4_reboot_recovery_ops;
936 } else {
937 /* "reboot" to ensure we clear all state on the server */
938 clp->cl_boot_time = CURRENT_TIME;
939 cred = nfs4_get_setclientid_cred(clp);
941 /* We're going to have to re-establish a clientid */
942 nfs4_state_mark_reclaim(clp);
943 status = -ENOENT;
944 if (cred != NULL) {
945 status = nfs4_init_client(clp, cred);
946 put_rpccred(cred);
948 if (status)
949 goto out_error;
950 /* Mark all delegations for reclaim */
951 nfs_delegation_mark_reclaim(clp);
952 /* Note: list is protected by exclusive lock on cl->cl_sem */
953 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
954 status = nfs4_reclaim_open_state(ops, sp);
955 if (status < 0) {
956 if (status == -NFS4ERR_NO_GRACE) {
957 ops = &nfs4_network_partition_recovery_ops;
958 status = nfs4_reclaim_open_state(ops, sp);
960 if (status == -NFS4ERR_STALE_CLIENTID)
961 goto restart_loop;
962 if (status == -NFS4ERR_EXPIRED)
963 goto restart_loop;
966 nfs_delegation_reap_unclaimed(clp);
967 out:
968 up_write(&clp->cl_sem);
969 unlock_kernel();
970 if (status == -NFS4ERR_CB_PATH_DOWN)
971 nfs_handle_cb_pathdown(clp);
972 nfs4_clear_recover_bit(clp);
973 nfs4_put_client(clp);
974 module_put_and_exit(0);
975 return 0;
976 out_error:
977 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
978 NIPQUAD(clp->cl_addr.s_addr), -status);
979 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
980 goto out;
984 * Local variables:
985 * c-basic-offset: 8
986 * End: