Staging: hv: mouse_vsc.c: fix brace coding style issues
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfs4state.c
blobe6742b57a04c725aeebd07ab6a2ad0e27bc95a9e
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/kernel.h>
42 #include <linux/slab.h>
43 #include <linux/fs.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/ratelimit.h>
50 #include <linux/workqueue.h>
51 #include <linux/bitops.h>
53 #include "nfs4_fs.h"
54 #include "callback.h"
55 #include "delegation.h"
56 #include "internal.h"
57 #include "pnfs.h"
59 #define OPENOWNER_POOL_SIZE 8
61 const nfs4_stateid zero_stateid;
63 static LIST_HEAD(nfs4_clientid_list);
65 int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
67 struct nfs4_setclientid_res clid;
68 unsigned short port;
69 int status;
71 port = nfs_callback_tcpport;
72 if (clp->cl_addr.ss_family == AF_INET6)
73 port = nfs_callback_tcpport6;
75 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
76 if (status != 0)
77 goto out;
78 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
79 if (status != 0)
80 goto out;
81 clp->cl_clientid = clid.clientid;
82 nfs4_schedule_state_renewal(clp);
83 out:
84 return status;
87 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
89 struct rpc_cred *cred = NULL;
91 if (clp->cl_machine_cred != NULL)
92 cred = get_rpccred(clp->cl_machine_cred);
93 return cred;
96 static void nfs4_clear_machine_cred(struct nfs_client *clp)
98 struct rpc_cred *cred;
100 spin_lock(&clp->cl_lock);
101 cred = clp->cl_machine_cred;
102 clp->cl_machine_cred = NULL;
103 spin_unlock(&clp->cl_lock);
104 if (cred != NULL)
105 put_rpccred(cred);
108 static struct rpc_cred *
109 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
111 struct rpc_cred *cred = NULL;
112 struct nfs4_state_owner *sp;
113 struct rb_node *pos;
115 for (pos = rb_first(&server->state_owners);
116 pos != NULL;
117 pos = rb_next(pos)) {
118 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
119 if (list_empty(&sp->so_states))
120 continue;
121 cred = get_rpccred(sp->so_cred);
122 break;
124 return cred;
128 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
129 * @clp: client state handle
131 * Returns an rpc_cred with reference count bumped, or NULL.
132 * Caller must hold clp->cl_lock.
134 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
136 struct rpc_cred *cred = NULL;
137 struct nfs_server *server;
139 rcu_read_lock();
140 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
141 cred = nfs4_get_renew_cred_server_locked(server);
142 if (cred != NULL)
143 break;
145 rcu_read_unlock();
146 return cred;
149 #if defined(CONFIG_NFS_V4_1)
151 static int nfs41_setup_state_renewal(struct nfs_client *clp)
153 int status;
154 struct nfs_fsinfo fsinfo;
156 status = nfs4_proc_get_lease_time(clp, &fsinfo);
157 if (status == 0) {
158 /* Update lease time and schedule renewal */
159 spin_lock(&clp->cl_lock);
160 clp->cl_lease_time = fsinfo.lease_time * HZ;
161 clp->cl_last_renewal = jiffies;
162 spin_unlock(&clp->cl_lock);
164 nfs4_schedule_state_renewal(clp);
167 return status;
171 * Back channel returns NFS4ERR_DELAY for new requests when
172 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
173 * is ended.
175 static void nfs4_end_drain_session(struct nfs_client *clp)
177 struct nfs4_session *ses = clp->cl_session;
178 int max_slots;
180 if (ses == NULL)
181 return;
182 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
183 spin_lock(&ses->fc_slot_table.slot_tbl_lock);
184 max_slots = ses->fc_slot_table.max_slots;
185 while (max_slots--) {
186 struct rpc_task *task;
188 task = rpc_wake_up_next(&ses->fc_slot_table.
189 slot_tbl_waitq);
190 if (!task)
191 break;
192 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
194 spin_unlock(&ses->fc_slot_table.slot_tbl_lock);
198 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
200 spin_lock(&tbl->slot_tbl_lock);
201 if (tbl->highest_used_slotid != -1) {
202 INIT_COMPLETION(tbl->complete);
203 spin_unlock(&tbl->slot_tbl_lock);
204 return wait_for_completion_interruptible(&tbl->complete);
206 spin_unlock(&tbl->slot_tbl_lock);
207 return 0;
210 static int nfs4_begin_drain_session(struct nfs_client *clp)
212 struct nfs4_session *ses = clp->cl_session;
213 int ret = 0;
215 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
216 /* back channel */
217 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
218 if (ret)
219 return ret;
220 /* fore channel */
221 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
224 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
226 int status;
228 nfs4_begin_drain_session(clp);
229 status = nfs4_proc_exchange_id(clp, cred);
230 if (status != 0)
231 goto out;
232 status = nfs4_proc_create_session(clp);
233 if (status != 0)
234 goto out;
235 nfs41_setup_state_renewal(clp);
236 nfs_mark_client_ready(clp, NFS_CS_READY);
237 out:
238 return status;
241 struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
243 struct rpc_cred *cred;
245 spin_lock(&clp->cl_lock);
246 cred = nfs4_get_machine_cred_locked(clp);
247 spin_unlock(&clp->cl_lock);
248 return cred;
251 #endif /* CONFIG_NFS_V4_1 */
253 static struct rpc_cred *
254 nfs4_get_setclientid_cred_server(struct nfs_server *server)
256 struct nfs_client *clp = server->nfs_client;
257 struct rpc_cred *cred = NULL;
258 struct nfs4_state_owner *sp;
259 struct rb_node *pos;
261 spin_lock(&clp->cl_lock);
262 pos = rb_first(&server->state_owners);
263 if (pos != NULL) {
264 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
265 cred = get_rpccred(sp->so_cred);
267 spin_unlock(&clp->cl_lock);
268 return cred;
272 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
273 * @clp: client state handle
275 * Returns an rpc_cred with reference count bumped, or NULL.
277 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
279 struct nfs_server *server;
280 struct rpc_cred *cred;
282 spin_lock(&clp->cl_lock);
283 cred = nfs4_get_machine_cred_locked(clp);
284 spin_unlock(&clp->cl_lock);
285 if (cred != NULL)
286 goto out;
288 rcu_read_lock();
289 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
290 cred = nfs4_get_setclientid_cred_server(server);
291 if (cred != NULL)
292 break;
294 rcu_read_unlock();
296 out:
297 return cred;
300 static void nfs_alloc_unique_id_locked(struct rb_root *root,
301 struct nfs_unique_id *new,
302 __u64 minval, int maxbits)
304 struct rb_node **p, *parent;
305 struct nfs_unique_id *pos;
306 __u64 mask = ~0ULL;
308 if (maxbits < 64)
309 mask = (1ULL << maxbits) - 1ULL;
311 /* Ensure distribution is more or less flat */
312 get_random_bytes(&new->id, sizeof(new->id));
313 new->id &= mask;
314 if (new->id < minval)
315 new->id += minval;
316 retry:
317 p = &root->rb_node;
318 parent = NULL;
320 while (*p != NULL) {
321 parent = *p;
322 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
324 if (new->id < pos->id)
325 p = &(*p)->rb_left;
326 else if (new->id > pos->id)
327 p = &(*p)->rb_right;
328 else
329 goto id_exists;
331 rb_link_node(&new->rb_node, parent, p);
332 rb_insert_color(&new->rb_node, root);
333 return;
334 id_exists:
335 for (;;) {
336 new->id++;
337 if (new->id < minval || (new->id & mask) != new->id) {
338 new->id = minval;
339 break;
341 parent = rb_next(parent);
342 if (parent == NULL)
343 break;
344 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
345 if (new->id < pos->id)
346 break;
348 goto retry;
351 static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
353 rb_erase(&id->rb_node, root);
356 static struct nfs4_state_owner *
357 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
359 struct rb_node **p = &server->state_owners.rb_node,
360 *parent = NULL;
361 struct nfs4_state_owner *sp, *res = NULL;
363 while (*p != NULL) {
364 parent = *p;
365 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
367 if (server < sp->so_server) {
368 p = &parent->rb_left;
369 continue;
371 if (server > sp->so_server) {
372 p = &parent->rb_right;
373 continue;
375 if (cred < sp->so_cred)
376 p = &parent->rb_left;
377 else if (cred > sp->so_cred)
378 p = &parent->rb_right;
379 else {
380 atomic_inc(&sp->so_count);
381 res = sp;
382 break;
385 return res;
388 static struct nfs4_state_owner *
389 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
391 struct nfs_server *server = new->so_server;
392 struct rb_node **p = &server->state_owners.rb_node,
393 *parent = NULL;
394 struct nfs4_state_owner *sp;
396 while (*p != NULL) {
397 parent = *p;
398 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
400 if (new->so_cred < sp->so_cred)
401 p = &parent->rb_left;
402 else if (new->so_cred > sp->so_cred)
403 p = &parent->rb_right;
404 else {
405 atomic_inc(&sp->so_count);
406 return sp;
409 nfs_alloc_unique_id_locked(&server->openowner_id,
410 &new->so_owner_id, 1, 64);
411 rb_link_node(&new->so_server_node, parent, p);
412 rb_insert_color(&new->so_server_node, &server->state_owners);
413 return new;
416 static void
417 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
419 struct nfs_server *server = sp->so_server;
421 if (!RB_EMPTY_NODE(&sp->so_server_node))
422 rb_erase(&sp->so_server_node, &server->state_owners);
423 nfs_free_unique_id(&server->openowner_id, &sp->so_owner_id);
427 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
428 * create a new state_owner.
431 static struct nfs4_state_owner *
432 nfs4_alloc_state_owner(void)
434 struct nfs4_state_owner *sp;
436 sp = kzalloc(sizeof(*sp),GFP_NOFS);
437 if (!sp)
438 return NULL;
439 spin_lock_init(&sp->so_lock);
440 INIT_LIST_HEAD(&sp->so_states);
441 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
442 sp->so_seqid.sequence = &sp->so_sequence;
443 spin_lock_init(&sp->so_sequence.lock);
444 INIT_LIST_HEAD(&sp->so_sequence.list);
445 atomic_set(&sp->so_count, 1);
446 return sp;
449 static void
450 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
452 if (!RB_EMPTY_NODE(&sp->so_server_node)) {
453 struct nfs_server *server = sp->so_server;
454 struct nfs_client *clp = server->nfs_client;
456 spin_lock(&clp->cl_lock);
457 rb_erase(&sp->so_server_node, &server->state_owners);
458 RB_CLEAR_NODE(&sp->so_server_node);
459 spin_unlock(&clp->cl_lock);
464 * nfs4_get_state_owner - Look up a state owner given a credential
465 * @server: nfs_server to search
466 * @cred: RPC credential to match
468 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
470 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
471 struct rpc_cred *cred)
473 struct nfs_client *clp = server->nfs_client;
474 struct nfs4_state_owner *sp, *new;
476 spin_lock(&clp->cl_lock);
477 sp = nfs4_find_state_owner_locked(server, cred);
478 spin_unlock(&clp->cl_lock);
479 if (sp != NULL)
480 return sp;
481 new = nfs4_alloc_state_owner();
482 if (new == NULL)
483 return NULL;
484 new->so_server = server;
485 new->so_cred = cred;
486 spin_lock(&clp->cl_lock);
487 sp = nfs4_insert_state_owner_locked(new);
488 spin_unlock(&clp->cl_lock);
489 if (sp == new)
490 get_rpccred(cred);
491 else {
492 rpc_destroy_wait_queue(&new->so_sequence.wait);
493 kfree(new);
495 return sp;
499 * nfs4_put_state_owner - Release a nfs4_state_owner
500 * @sp: state owner data to release
503 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
505 struct nfs_client *clp = sp->so_server->nfs_client;
506 struct rpc_cred *cred = sp->so_cred;
508 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
509 return;
510 nfs4_remove_state_owner_locked(sp);
511 spin_unlock(&clp->cl_lock);
512 rpc_destroy_wait_queue(&sp->so_sequence.wait);
513 put_rpccred(cred);
514 kfree(sp);
517 static struct nfs4_state *
518 nfs4_alloc_open_state(void)
520 struct nfs4_state *state;
522 state = kzalloc(sizeof(*state), GFP_NOFS);
523 if (!state)
524 return NULL;
525 atomic_set(&state->count, 1);
526 INIT_LIST_HEAD(&state->lock_states);
527 spin_lock_init(&state->state_lock);
528 seqlock_init(&state->seqlock);
529 return state;
532 void
533 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
535 if (state->state == fmode)
536 return;
537 /* NB! List reordering - see the reclaim code for why. */
538 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
539 if (fmode & FMODE_WRITE)
540 list_move(&state->open_states, &state->owner->so_states);
541 else
542 list_move_tail(&state->open_states, &state->owner->so_states);
544 state->state = fmode;
547 static struct nfs4_state *
548 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
550 struct nfs_inode *nfsi = NFS_I(inode);
551 struct nfs4_state *state;
553 list_for_each_entry(state, &nfsi->open_states, inode_states) {
554 if (state->owner != owner)
555 continue;
556 if (atomic_inc_not_zero(&state->count))
557 return state;
559 return NULL;
562 static void
563 nfs4_free_open_state(struct nfs4_state *state)
565 kfree(state);
568 struct nfs4_state *
569 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
571 struct nfs4_state *state, *new;
572 struct nfs_inode *nfsi = NFS_I(inode);
574 spin_lock(&inode->i_lock);
575 state = __nfs4_find_state_byowner(inode, owner);
576 spin_unlock(&inode->i_lock);
577 if (state)
578 goto out;
579 new = nfs4_alloc_open_state();
580 spin_lock(&owner->so_lock);
581 spin_lock(&inode->i_lock);
582 state = __nfs4_find_state_byowner(inode, owner);
583 if (state == NULL && new != NULL) {
584 state = new;
585 state->owner = owner;
586 atomic_inc(&owner->so_count);
587 list_add(&state->inode_states, &nfsi->open_states);
588 state->inode = igrab(inode);
589 spin_unlock(&inode->i_lock);
590 /* Note: The reclaim code dictates that we add stateless
591 * and read-only stateids to the end of the list */
592 list_add_tail(&state->open_states, &owner->so_states);
593 spin_unlock(&owner->so_lock);
594 } else {
595 spin_unlock(&inode->i_lock);
596 spin_unlock(&owner->so_lock);
597 if (new)
598 nfs4_free_open_state(new);
600 out:
601 return state;
604 void nfs4_put_open_state(struct nfs4_state *state)
606 struct inode *inode = state->inode;
607 struct nfs4_state_owner *owner = state->owner;
609 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
610 return;
611 spin_lock(&inode->i_lock);
612 list_del(&state->inode_states);
613 list_del(&state->open_states);
614 spin_unlock(&inode->i_lock);
615 spin_unlock(&owner->so_lock);
616 iput(inode);
617 nfs4_free_open_state(state);
618 nfs4_put_state_owner(owner);
622 * Close the current file.
624 static void __nfs4_close(struct path *path, struct nfs4_state *state,
625 fmode_t fmode, gfp_t gfp_mask, int wait)
627 struct nfs4_state_owner *owner = state->owner;
628 int call_close = 0;
629 fmode_t newstate;
631 atomic_inc(&owner->so_count);
632 /* Protect against nfs4_find_state() */
633 spin_lock(&owner->so_lock);
634 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
635 case FMODE_READ:
636 state->n_rdonly--;
637 break;
638 case FMODE_WRITE:
639 state->n_wronly--;
640 break;
641 case FMODE_READ|FMODE_WRITE:
642 state->n_rdwr--;
644 newstate = FMODE_READ|FMODE_WRITE;
645 if (state->n_rdwr == 0) {
646 if (state->n_rdonly == 0) {
647 newstate &= ~FMODE_READ;
648 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
649 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
651 if (state->n_wronly == 0) {
652 newstate &= ~FMODE_WRITE;
653 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
654 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
656 if (newstate == 0)
657 clear_bit(NFS_DELEGATED_STATE, &state->flags);
659 nfs4_state_set_mode_locked(state, newstate);
660 spin_unlock(&owner->so_lock);
662 if (!call_close) {
663 nfs4_put_open_state(state);
664 nfs4_put_state_owner(owner);
665 } else {
666 bool roc = pnfs_roc(state->inode);
668 nfs4_do_close(path, state, gfp_mask, wait, roc);
672 void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
674 __nfs4_close(path, state, fmode, GFP_NOFS, 0);
677 void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
679 __nfs4_close(path, state, fmode, GFP_KERNEL, 1);
683 * Search the state->lock_states for an existing lock_owner
684 * that is compatible with current->files
686 static struct nfs4_lock_state *
687 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
689 struct nfs4_lock_state *pos;
690 list_for_each_entry(pos, &state->lock_states, ls_locks) {
691 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
692 continue;
693 switch (pos->ls_owner.lo_type) {
694 case NFS4_POSIX_LOCK_TYPE:
695 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
696 continue;
697 break;
698 case NFS4_FLOCK_LOCK_TYPE:
699 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
700 continue;
702 atomic_inc(&pos->ls_count);
703 return pos;
705 return NULL;
709 * Return a compatible lock_state. If no initialized lock_state structure
710 * exists, return an uninitialized one.
713 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
715 struct nfs4_lock_state *lsp;
716 struct nfs_server *server = state->owner->so_server;
717 struct nfs_client *clp = server->nfs_client;
719 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
720 if (lsp == NULL)
721 return NULL;
722 rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
723 spin_lock_init(&lsp->ls_sequence.lock);
724 INIT_LIST_HEAD(&lsp->ls_sequence.list);
725 lsp->ls_seqid.sequence = &lsp->ls_sequence;
726 atomic_set(&lsp->ls_count, 1);
727 lsp->ls_state = state;
728 lsp->ls_owner.lo_type = type;
729 switch (lsp->ls_owner.lo_type) {
730 case NFS4_FLOCK_LOCK_TYPE:
731 lsp->ls_owner.lo_u.flock_owner = fl_pid;
732 break;
733 case NFS4_POSIX_LOCK_TYPE:
734 lsp->ls_owner.lo_u.posix_owner = fl_owner;
735 break;
736 default:
737 kfree(lsp);
738 return NULL;
740 spin_lock(&clp->cl_lock);
741 nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
742 spin_unlock(&clp->cl_lock);
743 INIT_LIST_HEAD(&lsp->ls_locks);
744 return lsp;
747 static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
749 struct nfs_server *server = lsp->ls_state->owner->so_server;
750 struct nfs_client *clp = server->nfs_client;
752 spin_lock(&clp->cl_lock);
753 nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
754 spin_unlock(&clp->cl_lock);
755 rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
756 kfree(lsp);
760 * Return a compatible lock_state. If no initialized lock_state structure
761 * exists, return an uninitialized one.
764 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
766 struct nfs4_lock_state *lsp, *new = NULL;
768 for(;;) {
769 spin_lock(&state->state_lock);
770 lsp = __nfs4_find_lock_state(state, owner, pid, type);
771 if (lsp != NULL)
772 break;
773 if (new != NULL) {
774 list_add(&new->ls_locks, &state->lock_states);
775 set_bit(LK_STATE_IN_USE, &state->flags);
776 lsp = new;
777 new = NULL;
778 break;
780 spin_unlock(&state->state_lock);
781 new = nfs4_alloc_lock_state(state, owner, pid, type);
782 if (new == NULL)
783 return NULL;
785 spin_unlock(&state->state_lock);
786 if (new != NULL)
787 nfs4_free_lock_state(new);
788 return lsp;
792 * Release reference to lock_state, and free it if we see that
793 * it is no longer in use
795 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
797 struct nfs4_state *state;
799 if (lsp == NULL)
800 return;
801 state = lsp->ls_state;
802 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
803 return;
804 list_del(&lsp->ls_locks);
805 if (list_empty(&state->lock_states))
806 clear_bit(LK_STATE_IN_USE, &state->flags);
807 spin_unlock(&state->state_lock);
808 if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
809 nfs4_release_lockowner(lsp);
810 nfs4_free_lock_state(lsp);
813 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
815 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
817 dst->fl_u.nfs4_fl.owner = lsp;
818 atomic_inc(&lsp->ls_count);
821 static void nfs4_fl_release_lock(struct file_lock *fl)
823 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
826 static const struct file_lock_operations nfs4_fl_lock_ops = {
827 .fl_copy_lock = nfs4_fl_copy_lock,
828 .fl_release_private = nfs4_fl_release_lock,
831 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
833 struct nfs4_lock_state *lsp;
835 if (fl->fl_ops != NULL)
836 return 0;
837 if (fl->fl_flags & FL_POSIX)
838 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
839 else if (fl->fl_flags & FL_FLOCK)
840 lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
841 else
842 return -EINVAL;
843 if (lsp == NULL)
844 return -ENOMEM;
845 fl->fl_u.nfs4_fl.owner = lsp;
846 fl->fl_ops = &nfs4_fl_lock_ops;
847 return 0;
851 * Byte-range lock aware utility to initialize the stateid of read/write
852 * requests.
854 void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid)
856 struct nfs4_lock_state *lsp;
857 int seq;
859 do {
860 seq = read_seqbegin(&state->seqlock);
861 memcpy(dst, &state->stateid, sizeof(*dst));
862 } while (read_seqretry(&state->seqlock, seq));
863 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
864 return;
866 spin_lock(&state->state_lock);
867 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
868 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
869 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
870 spin_unlock(&state->state_lock);
871 nfs4_put_lock_state(lsp);
874 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
876 struct nfs_seqid *new;
878 new = kmalloc(sizeof(*new), gfp_mask);
879 if (new != NULL) {
880 new->sequence = counter;
881 INIT_LIST_HEAD(&new->list);
883 return new;
886 void nfs_release_seqid(struct nfs_seqid *seqid)
888 if (!list_empty(&seqid->list)) {
889 struct rpc_sequence *sequence = seqid->sequence->sequence;
891 spin_lock(&sequence->lock);
892 list_del_init(&seqid->list);
893 spin_unlock(&sequence->lock);
894 rpc_wake_up(&sequence->wait);
898 void nfs_free_seqid(struct nfs_seqid *seqid)
900 nfs_release_seqid(seqid);
901 kfree(seqid);
905 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
906 * failed with a seqid incrementing error -
907 * see comments nfs_fs.h:seqid_mutating_error()
909 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
911 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
912 switch (status) {
913 case 0:
914 break;
915 case -NFS4ERR_BAD_SEQID:
916 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
917 return;
918 printk(KERN_WARNING "NFS: v4 server returned a bad"
919 " sequence-id error on an"
920 " unconfirmed sequence %p!\n",
921 seqid->sequence);
922 case -NFS4ERR_STALE_CLIENTID:
923 case -NFS4ERR_STALE_STATEID:
924 case -NFS4ERR_BAD_STATEID:
925 case -NFS4ERR_BADXDR:
926 case -NFS4ERR_RESOURCE:
927 case -NFS4ERR_NOFILEHANDLE:
928 /* Non-seqid mutating errors */
929 return;
932 * Note: no locking needed as we are guaranteed to be first
933 * on the sequence list
935 seqid->sequence->counter++;
938 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
940 struct nfs4_state_owner *sp = container_of(seqid->sequence,
941 struct nfs4_state_owner, so_seqid);
942 struct nfs_server *server = sp->so_server;
944 if (status == -NFS4ERR_BAD_SEQID)
945 nfs4_drop_state_owner(sp);
946 if (!nfs4_has_session(server->nfs_client))
947 nfs_increment_seqid(status, seqid);
951 * Increment the seqid if the LOCK/LOCKU succeeded, or
952 * failed with a seqid incrementing error -
953 * see comments nfs_fs.h:seqid_mutating_error()
955 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
957 nfs_increment_seqid(status, seqid);
960 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
962 struct rpc_sequence *sequence = seqid->sequence->sequence;
963 int status = 0;
965 spin_lock(&sequence->lock);
966 if (list_empty(&seqid->list))
967 list_add_tail(&seqid->list, &sequence->list);
968 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
969 goto unlock;
970 rpc_sleep_on(&sequence->wait, task, NULL);
971 status = -EAGAIN;
972 unlock:
973 spin_unlock(&sequence->lock);
974 return status;
977 static int nfs4_run_state_manager(void *);
979 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
981 smp_mb__before_clear_bit();
982 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
983 smp_mb__after_clear_bit();
984 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
985 rpc_wake_up(&clp->cl_rpcwaitq);
989 * Schedule the nfs_client asynchronous state management routine
991 void nfs4_schedule_state_manager(struct nfs_client *clp)
993 struct task_struct *task;
995 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
996 return;
997 __module_get(THIS_MODULE);
998 atomic_inc(&clp->cl_count);
999 task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
1000 rpc_peeraddr2str(clp->cl_rpcclient,
1001 RPC_DISPLAY_ADDR));
1002 if (!IS_ERR(task))
1003 return;
1004 nfs4_clear_state_manager_bit(clp);
1005 nfs_put_client(clp);
1006 module_put(THIS_MODULE);
1010 * Schedule a state recovery attempt
1012 void nfs4_schedule_state_recovery(struct nfs_client *clp)
1014 if (!clp)
1015 return;
1016 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1017 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1018 nfs4_schedule_state_manager(clp);
1021 int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1024 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1025 /* Don't recover state that expired before the reboot */
1026 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1027 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1028 return 0;
1030 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1031 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1032 return 1;
1035 int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1037 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1038 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1039 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1040 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1041 return 1;
1044 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1046 struct inode *inode = state->inode;
1047 struct nfs_inode *nfsi = NFS_I(inode);
1048 struct file_lock *fl;
1049 int status = 0;
1051 if (inode->i_flock == NULL)
1052 return 0;
1054 /* Guard against delegation returns and new lock/unlock calls */
1055 down_write(&nfsi->rwsem);
1056 /* Protect inode->i_flock using the BKL */
1057 lock_flocks();
1058 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1059 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1060 continue;
1061 if (nfs_file_open_context(fl->fl_file)->state != state)
1062 continue;
1063 unlock_flocks();
1064 status = ops->recover_lock(state, fl);
1065 switch (status) {
1066 case 0:
1067 break;
1068 case -ESTALE:
1069 case -NFS4ERR_ADMIN_REVOKED:
1070 case -NFS4ERR_STALE_STATEID:
1071 case -NFS4ERR_BAD_STATEID:
1072 case -NFS4ERR_EXPIRED:
1073 case -NFS4ERR_NO_GRACE:
1074 case -NFS4ERR_STALE_CLIENTID:
1075 case -NFS4ERR_BADSESSION:
1076 case -NFS4ERR_BADSLOT:
1077 case -NFS4ERR_BAD_HIGH_SLOT:
1078 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1079 goto out;
1080 default:
1081 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
1082 __func__, status);
1083 case -ENOMEM:
1084 case -NFS4ERR_DENIED:
1085 case -NFS4ERR_RECLAIM_BAD:
1086 case -NFS4ERR_RECLAIM_CONFLICT:
1087 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1088 status = 0;
1090 lock_flocks();
1092 unlock_flocks();
1093 out:
1094 up_write(&nfsi->rwsem);
1095 return status;
1098 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1100 struct nfs4_state *state;
1101 struct nfs4_lock_state *lock;
1102 int status = 0;
1104 /* Note: we rely on the sp->so_states list being ordered
1105 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1106 * states first.
1107 * This is needed to ensure that the server won't give us any
1108 * read delegations that we have to return if, say, we are
1109 * recovering after a network partition or a reboot from a
1110 * server that doesn't support a grace period.
1112 restart:
1113 spin_lock(&sp->so_lock);
1114 list_for_each_entry(state, &sp->so_states, open_states) {
1115 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1116 continue;
1117 if (state->state == 0)
1118 continue;
1119 atomic_inc(&state->count);
1120 spin_unlock(&sp->so_lock);
1121 status = ops->recover_open(sp, state);
1122 if (status >= 0) {
1123 status = nfs4_reclaim_locks(state, ops);
1124 if (status >= 0) {
1125 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1126 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
1127 printk("%s: Lock reclaim failed!\n",
1128 __func__);
1130 nfs4_put_open_state(state);
1131 goto restart;
1134 switch (status) {
1135 default:
1136 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
1137 __func__, status);
1138 case -ENOENT:
1139 case -ENOMEM:
1140 case -ESTALE:
1142 * Open state on this file cannot be recovered
1143 * All we can do is revert to using the zero stateid.
1145 memset(state->stateid.data, 0,
1146 sizeof(state->stateid.data));
1147 /* Mark the file as being 'closed' */
1148 state->state = 0;
1149 break;
1150 case -EKEYEXPIRED:
1152 * User RPCSEC_GSS context has expired.
1153 * We cannot recover this stateid now, so
1154 * skip it and allow recovery thread to
1155 * proceed.
1157 break;
1158 case -NFS4ERR_ADMIN_REVOKED:
1159 case -NFS4ERR_STALE_STATEID:
1160 case -NFS4ERR_BAD_STATEID:
1161 case -NFS4ERR_RECLAIM_BAD:
1162 case -NFS4ERR_RECLAIM_CONFLICT:
1163 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1164 break;
1165 case -NFS4ERR_EXPIRED:
1166 case -NFS4ERR_NO_GRACE:
1167 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1168 case -NFS4ERR_STALE_CLIENTID:
1169 case -NFS4ERR_BADSESSION:
1170 case -NFS4ERR_BADSLOT:
1171 case -NFS4ERR_BAD_HIGH_SLOT:
1172 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1173 goto out_err;
1175 nfs4_put_open_state(state);
1176 goto restart;
1178 spin_unlock(&sp->so_lock);
1179 return 0;
1180 out_err:
1181 nfs4_put_open_state(state);
1182 return status;
1185 static void nfs4_clear_open_state(struct nfs4_state *state)
1187 struct nfs4_lock_state *lock;
1189 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1190 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1191 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1192 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1193 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1194 lock->ls_seqid.flags = 0;
1195 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1199 static void nfs4_reset_seqids(struct nfs_server *server,
1200 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1202 struct nfs_client *clp = server->nfs_client;
1203 struct nfs4_state_owner *sp;
1204 struct rb_node *pos;
1205 struct nfs4_state *state;
1207 spin_lock(&clp->cl_lock);
1208 for (pos = rb_first(&server->state_owners);
1209 pos != NULL;
1210 pos = rb_next(pos)) {
1211 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1212 sp->so_seqid.flags = 0;
1213 spin_lock(&sp->so_lock);
1214 list_for_each_entry(state, &sp->so_states, open_states) {
1215 if (mark_reclaim(clp, state))
1216 nfs4_clear_open_state(state);
1218 spin_unlock(&sp->so_lock);
1220 spin_unlock(&clp->cl_lock);
1223 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1224 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1226 struct nfs_server *server;
1228 rcu_read_lock();
1229 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1230 nfs4_reset_seqids(server, mark_reclaim);
1231 rcu_read_unlock();
1234 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1236 /* Mark all delegations for reclaim */
1237 nfs_delegation_mark_reclaim(clp);
1238 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1241 static void nfs4_reclaim_complete(struct nfs_client *clp,
1242 const struct nfs4_state_recovery_ops *ops)
1244 /* Notify the server we're done reclaiming our state */
1245 if (ops->reclaim_complete)
1246 (void)ops->reclaim_complete(clp);
1249 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1251 struct nfs_client *clp = server->nfs_client;
1252 struct nfs4_state_owner *sp;
1253 struct rb_node *pos;
1254 struct nfs4_state *state;
1256 spin_lock(&clp->cl_lock);
1257 for (pos = rb_first(&server->state_owners);
1258 pos != NULL;
1259 pos = rb_next(pos)) {
1260 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1261 spin_lock(&sp->so_lock);
1262 list_for_each_entry(state, &sp->so_states, open_states) {
1263 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1264 &state->flags))
1265 continue;
1266 nfs4_state_mark_reclaim_nograce(clp, state);
1268 spin_unlock(&sp->so_lock);
1270 spin_unlock(&clp->cl_lock);
1273 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1275 struct nfs_server *server;
1277 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1278 return 0;
1280 rcu_read_lock();
1281 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1282 nfs4_clear_reclaim_server(server);
1283 rcu_read_unlock();
1285 nfs_delegation_reap_unclaimed(clp);
1286 return 1;
1289 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1291 if (!nfs4_state_clear_reclaim_reboot(clp))
1292 return;
1293 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
1296 static void nfs_delegation_clear_all(struct nfs_client *clp)
1298 nfs_delegation_mark_reclaim(clp);
1299 nfs_delegation_reap_unclaimed(clp);
1302 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1304 nfs_delegation_clear_all(clp);
1305 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1308 static void nfs4_warn_keyexpired(const char *s)
1310 printk_ratelimited(KERN_WARNING "Error: state manager"
1311 " encountered RPCSEC_GSS session"
1312 " expired against NFSv4 server %s.\n",
1316 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1318 switch (error) {
1319 case -NFS4ERR_CB_PATH_DOWN:
1320 nfs_handle_cb_pathdown(clp);
1321 return 0;
1322 case -NFS4ERR_NO_GRACE:
1323 nfs4_state_end_reclaim_reboot(clp);
1324 return 0;
1325 case -NFS4ERR_STALE_CLIENTID:
1326 case -NFS4ERR_LEASE_MOVED:
1327 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1328 nfs4_state_clear_reclaim_reboot(clp);
1329 nfs4_state_start_reclaim_reboot(clp);
1330 break;
1331 case -NFS4ERR_EXPIRED:
1332 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1333 nfs4_state_start_reclaim_nograce(clp);
1334 break;
1335 case -NFS4ERR_BADSESSION:
1336 case -NFS4ERR_BADSLOT:
1337 case -NFS4ERR_BAD_HIGH_SLOT:
1338 case -NFS4ERR_DEADSESSION:
1339 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1340 case -NFS4ERR_SEQ_FALSE_RETRY:
1341 case -NFS4ERR_SEQ_MISORDERED:
1342 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1343 /* Zero session reset errors */
1344 return 0;
1345 case -EKEYEXPIRED:
1346 /* Nothing we can do */
1347 nfs4_warn_keyexpired(clp->cl_hostname);
1348 return 0;
1350 return error;
1353 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1355 struct nfs4_state_owner *sp;
1356 struct nfs_server *server;
1357 struct rb_node *pos;
1358 int status = 0;
1360 restart:
1361 rcu_read_lock();
1362 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1363 spin_lock(&clp->cl_lock);
1364 for (pos = rb_first(&server->state_owners);
1365 pos != NULL;
1366 pos = rb_next(pos)) {
1367 sp = rb_entry(pos,
1368 struct nfs4_state_owner, so_server_node);
1369 if (!test_and_clear_bit(ops->owner_flag_bit,
1370 &sp->so_flags))
1371 continue;
1372 atomic_inc(&sp->so_count);
1373 spin_unlock(&clp->cl_lock);
1374 rcu_read_unlock();
1376 status = nfs4_reclaim_open_state(sp, ops);
1377 if (status < 0) {
1378 set_bit(ops->owner_flag_bit, &sp->so_flags);
1379 nfs4_put_state_owner(sp);
1380 return nfs4_recovery_handle_error(clp, status);
1383 nfs4_put_state_owner(sp);
1384 goto restart;
1386 spin_unlock(&clp->cl_lock);
1388 rcu_read_unlock();
1389 return status;
1392 static int nfs4_check_lease(struct nfs_client *clp)
1394 struct rpc_cred *cred;
1395 const struct nfs4_state_maintenance_ops *ops =
1396 clp->cl_mvops->state_renewal_ops;
1397 int status = -NFS4ERR_EXPIRED;
1399 /* Is the client already known to have an expired lease? */
1400 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1401 return 0;
1402 spin_lock(&clp->cl_lock);
1403 cred = ops->get_state_renewal_cred_locked(clp);
1404 spin_unlock(&clp->cl_lock);
1405 if (cred == NULL) {
1406 cred = nfs4_get_setclientid_cred(clp);
1407 if (cred == NULL)
1408 goto out;
1410 status = ops->renew_lease(clp, cred);
1411 put_rpccred(cred);
1412 out:
1413 return nfs4_recovery_handle_error(clp, status);
1416 static int nfs4_reclaim_lease(struct nfs_client *clp)
1418 struct rpc_cred *cred;
1419 const struct nfs4_state_recovery_ops *ops =
1420 clp->cl_mvops->reboot_recovery_ops;
1421 int status = -ENOENT;
1423 cred = ops->get_clid_cred(clp);
1424 if (cred != NULL) {
1425 status = ops->establish_clid(clp, cred);
1426 put_rpccred(cred);
1427 /* Handle case where the user hasn't set up machine creds */
1428 if (status == -EACCES && cred == clp->cl_machine_cred) {
1429 nfs4_clear_machine_cred(clp);
1430 status = -EAGAIN;
1432 if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1433 status = -EPROTONOSUPPORT;
1435 return status;
1438 #ifdef CONFIG_NFS_V4_1
1439 void nfs41_handle_recall_slot(struct nfs_client *clp)
1441 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1442 nfs4_schedule_state_recovery(clp);
1445 static void nfs4_reset_all_state(struct nfs_client *clp)
1447 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1448 clp->cl_boot_time = CURRENT_TIME;
1449 nfs4_state_start_reclaim_nograce(clp);
1450 nfs4_schedule_state_recovery(clp);
1454 static void nfs41_handle_server_reboot(struct nfs_client *clp)
1456 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1457 nfs4_state_start_reclaim_reboot(clp);
1458 nfs4_schedule_state_recovery(clp);
1462 static void nfs41_handle_state_revoked(struct nfs_client *clp)
1464 /* Temporary */
1465 nfs4_reset_all_state(clp);
1468 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1470 /* This will need to handle layouts too */
1471 nfs_expire_all_delegations(clp);
1474 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1476 nfs_expire_all_delegations(clp);
1477 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
1478 nfs4_schedule_state_recovery(clp);
1481 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1483 if (!flags)
1484 return;
1485 else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1486 nfs41_handle_server_reboot(clp);
1487 else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
1488 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1489 SEQ4_STATUS_ADMIN_STATE_REVOKED |
1490 SEQ4_STATUS_LEASE_MOVED))
1491 nfs41_handle_state_revoked(clp);
1492 else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1493 nfs41_handle_recallable_state_revoked(clp);
1494 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
1495 SEQ4_STATUS_BACKCHANNEL_FAULT |
1496 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
1497 nfs41_handle_cb_path_down(clp);
1500 static int nfs4_reset_session(struct nfs_client *clp)
1502 int status;
1504 nfs4_begin_drain_session(clp);
1505 status = nfs4_proc_destroy_session(clp->cl_session);
1506 if (status && status != -NFS4ERR_BADSESSION &&
1507 status != -NFS4ERR_DEADSESSION) {
1508 status = nfs4_recovery_handle_error(clp, status);
1509 goto out;
1512 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
1513 status = nfs4_proc_create_session(clp);
1514 if (status) {
1515 status = nfs4_recovery_handle_error(clp, status);
1516 goto out;
1518 /* create_session negotiated new slot table */
1519 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1521 /* Let the state manager reestablish state */
1522 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1523 nfs41_setup_state_renewal(clp);
1524 out:
1525 return status;
1528 static int nfs4_recall_slot(struct nfs_client *clp)
1530 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1531 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1532 struct nfs4_slot *new, *old;
1533 int i;
1535 nfs4_begin_drain_session(clp);
1536 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
1537 GFP_NOFS);
1538 if (!new)
1539 return -ENOMEM;
1541 spin_lock(&fc_tbl->slot_tbl_lock);
1542 for (i = 0; i < fc_tbl->target_max_slots; i++)
1543 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1544 old = fc_tbl->slots;
1545 fc_tbl->slots = new;
1546 fc_tbl->max_slots = fc_tbl->target_max_slots;
1547 fc_tbl->target_max_slots = 0;
1548 fc_attrs->max_reqs = fc_tbl->max_slots;
1549 spin_unlock(&fc_tbl->slot_tbl_lock);
1551 kfree(old);
1552 nfs4_end_drain_session(clp);
1553 return 0;
1556 #else /* CONFIG_NFS_V4_1 */
1557 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
1558 static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
1559 static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
1560 #endif /* CONFIG_NFS_V4_1 */
1562 /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1563 * on EXCHANGE_ID for v4.1
1565 static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1567 if (nfs4_has_session(clp)) {
1568 switch (status) {
1569 case -NFS4ERR_DELAY:
1570 case -NFS4ERR_CLID_INUSE:
1571 case -EAGAIN:
1572 break;
1574 case -EKEYEXPIRED:
1575 nfs4_warn_keyexpired(clp->cl_hostname);
1576 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1577 * in nfs4_exchange_id */
1578 default:
1579 return;
1582 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1585 static void nfs4_state_manager(struct nfs_client *clp)
1587 int status = 0;
1589 /* Ensure exclusive access to NFSv4 state */
1590 for(;;) {
1591 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1592 /* We're going to have to re-establish a clientid */
1593 status = nfs4_reclaim_lease(clp);
1594 if (status) {
1595 nfs4_set_lease_expired(clp, status);
1596 if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1597 &clp->cl_state))
1598 continue;
1599 if (clp->cl_cons_state ==
1600 NFS_CS_SESSION_INITING)
1601 nfs_mark_client_ready(clp, status);
1602 goto out_error;
1604 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1605 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1606 pnfs_destroy_all_layouts(clp);
1609 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1610 status = nfs4_check_lease(clp);
1611 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1612 continue;
1613 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1614 goto out_error;
1617 /* Initialize or reset the session */
1618 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
1619 && nfs4_has_session(clp)) {
1620 status = nfs4_reset_session(clp);
1621 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1622 continue;
1623 if (status < 0)
1624 goto out_error;
1627 /* First recover reboot state... */
1628 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
1629 status = nfs4_do_reclaim(clp,
1630 clp->cl_mvops->reboot_recovery_ops);
1631 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
1632 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
1633 continue;
1634 nfs4_state_end_reclaim_reboot(clp);
1635 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1636 continue;
1637 if (status < 0)
1638 goto out_error;
1641 /* Now recover expired state... */
1642 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
1643 status = nfs4_do_reclaim(clp,
1644 clp->cl_mvops->nograce_recovery_ops);
1645 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
1646 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
1647 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1648 continue;
1649 if (status < 0)
1650 goto out_error;
1653 nfs4_end_drain_session(clp);
1654 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1655 nfs_client_return_marked_delegations(clp);
1656 continue;
1658 /* Recall session slots */
1659 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1660 && nfs4_has_session(clp)) {
1661 status = nfs4_recall_slot(clp);
1662 if (status < 0)
1663 goto out_error;
1664 continue;
1668 nfs4_clear_state_manager_bit(clp);
1669 /* Did we race with an attempt to give us more work? */
1670 if (clp->cl_state == 0)
1671 break;
1672 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1673 break;
1675 return;
1676 out_error:
1677 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
1678 " with error %d\n", clp->cl_hostname, -status);
1679 nfs4_end_drain_session(clp);
1680 nfs4_clear_state_manager_bit(clp);
1683 static int nfs4_run_state_manager(void *ptr)
1685 struct nfs_client *clp = ptr;
1687 allow_signal(SIGKILL);
1688 nfs4_state_manager(clp);
1689 nfs_put_client(clp);
1690 module_put_and_exit(0);
1691 return 0;
1695 * Local variables:
1696 * c-basic-offset: 8
1697 * End: