xen: cleancache shim to Xen Transcendent Memory
[linux-2.6/next.git] / fs / nfs / nfs4state.c
bloba6804f704d9d8656868b6492c66232020132fe6d
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 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
157 nfs4_schedule_state_renewal(clp);
158 return 0;
161 status = nfs4_proc_get_lease_time(clp, &fsinfo);
162 if (status == 0) {
163 /* Update lease time and schedule renewal */
164 spin_lock(&clp->cl_lock);
165 clp->cl_lease_time = fsinfo.lease_time * HZ;
166 clp->cl_last_renewal = jiffies;
167 spin_unlock(&clp->cl_lock);
169 nfs4_schedule_state_renewal(clp);
172 return status;
176 * Back channel returns NFS4ERR_DELAY for new requests when
177 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
178 * is ended.
180 static void nfs4_end_drain_session(struct nfs_client *clp)
182 struct nfs4_session *ses = clp->cl_session;
183 int max_slots;
185 if (ses == NULL)
186 return;
187 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
188 spin_lock(&ses->fc_slot_table.slot_tbl_lock);
189 max_slots = ses->fc_slot_table.max_slots;
190 while (max_slots--) {
191 struct rpc_task *task;
193 task = rpc_wake_up_next(&ses->fc_slot_table.
194 slot_tbl_waitq);
195 if (!task)
196 break;
197 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
199 spin_unlock(&ses->fc_slot_table.slot_tbl_lock);
203 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
205 spin_lock(&tbl->slot_tbl_lock);
206 if (tbl->highest_used_slotid != -1) {
207 INIT_COMPLETION(tbl->complete);
208 spin_unlock(&tbl->slot_tbl_lock);
209 return wait_for_completion_interruptible(&tbl->complete);
211 spin_unlock(&tbl->slot_tbl_lock);
212 return 0;
215 static int nfs4_begin_drain_session(struct nfs_client *clp)
217 struct nfs4_session *ses = clp->cl_session;
218 int ret = 0;
220 set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
221 /* back channel */
222 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
223 if (ret)
224 return ret;
225 /* fore channel */
226 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
229 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
231 int status;
233 nfs4_begin_drain_session(clp);
234 status = nfs4_proc_exchange_id(clp, cred);
235 if (status != 0)
236 goto out;
237 status = nfs4_proc_create_session(clp);
238 if (status != 0)
239 goto out;
240 nfs41_setup_state_renewal(clp);
241 nfs_mark_client_ready(clp, NFS_CS_READY);
242 out:
243 return status;
246 struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
248 struct rpc_cred *cred;
250 spin_lock(&clp->cl_lock);
251 cred = nfs4_get_machine_cred_locked(clp);
252 spin_unlock(&clp->cl_lock);
253 return cred;
256 #endif /* CONFIG_NFS_V4_1 */
258 static struct rpc_cred *
259 nfs4_get_setclientid_cred_server(struct nfs_server *server)
261 struct nfs_client *clp = server->nfs_client;
262 struct rpc_cred *cred = NULL;
263 struct nfs4_state_owner *sp;
264 struct rb_node *pos;
266 spin_lock(&clp->cl_lock);
267 pos = rb_first(&server->state_owners);
268 if (pos != NULL) {
269 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
270 cred = get_rpccred(sp->so_cred);
272 spin_unlock(&clp->cl_lock);
273 return cred;
277 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
278 * @clp: client state handle
280 * Returns an rpc_cred with reference count bumped, or NULL.
282 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
284 struct nfs_server *server;
285 struct rpc_cred *cred;
287 spin_lock(&clp->cl_lock);
288 cred = nfs4_get_machine_cred_locked(clp);
289 spin_unlock(&clp->cl_lock);
290 if (cred != NULL)
291 goto out;
293 rcu_read_lock();
294 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
295 cred = nfs4_get_setclientid_cred_server(server);
296 if (cred != NULL)
297 break;
299 rcu_read_unlock();
301 out:
302 return cred;
305 static void nfs_alloc_unique_id_locked(struct rb_root *root,
306 struct nfs_unique_id *new,
307 __u64 minval, int maxbits)
309 struct rb_node **p, *parent;
310 struct nfs_unique_id *pos;
311 __u64 mask = ~0ULL;
313 if (maxbits < 64)
314 mask = (1ULL << maxbits) - 1ULL;
316 /* Ensure distribution is more or less flat */
317 get_random_bytes(&new->id, sizeof(new->id));
318 new->id &= mask;
319 if (new->id < minval)
320 new->id += minval;
321 retry:
322 p = &root->rb_node;
323 parent = NULL;
325 while (*p != NULL) {
326 parent = *p;
327 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
329 if (new->id < pos->id)
330 p = &(*p)->rb_left;
331 else if (new->id > pos->id)
332 p = &(*p)->rb_right;
333 else
334 goto id_exists;
336 rb_link_node(&new->rb_node, parent, p);
337 rb_insert_color(&new->rb_node, root);
338 return;
339 id_exists:
340 for (;;) {
341 new->id++;
342 if (new->id < minval || (new->id & mask) != new->id) {
343 new->id = minval;
344 break;
346 parent = rb_next(parent);
347 if (parent == NULL)
348 break;
349 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
350 if (new->id < pos->id)
351 break;
353 goto retry;
356 static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
358 rb_erase(&id->rb_node, root);
361 static struct nfs4_state_owner *
362 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
364 struct rb_node **p = &server->state_owners.rb_node,
365 *parent = NULL;
366 struct nfs4_state_owner *sp, *res = NULL;
368 while (*p != NULL) {
369 parent = *p;
370 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
372 if (server < sp->so_server) {
373 p = &parent->rb_left;
374 continue;
376 if (server > sp->so_server) {
377 p = &parent->rb_right;
378 continue;
380 if (cred < sp->so_cred)
381 p = &parent->rb_left;
382 else if (cred > sp->so_cred)
383 p = &parent->rb_right;
384 else {
385 atomic_inc(&sp->so_count);
386 res = sp;
387 break;
390 return res;
393 static struct nfs4_state_owner *
394 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
396 struct nfs_server *server = new->so_server;
397 struct rb_node **p = &server->state_owners.rb_node,
398 *parent = NULL;
399 struct nfs4_state_owner *sp;
401 while (*p != NULL) {
402 parent = *p;
403 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
405 if (new->so_cred < sp->so_cred)
406 p = &parent->rb_left;
407 else if (new->so_cred > sp->so_cred)
408 p = &parent->rb_right;
409 else {
410 atomic_inc(&sp->so_count);
411 return sp;
414 nfs_alloc_unique_id_locked(&server->openowner_id,
415 &new->so_owner_id, 1, 64);
416 rb_link_node(&new->so_server_node, parent, p);
417 rb_insert_color(&new->so_server_node, &server->state_owners);
418 return new;
421 static void
422 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
424 struct nfs_server *server = sp->so_server;
426 if (!RB_EMPTY_NODE(&sp->so_server_node))
427 rb_erase(&sp->so_server_node, &server->state_owners);
428 nfs_free_unique_id(&server->openowner_id, &sp->so_owner_id);
432 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
433 * create a new state_owner.
436 static struct nfs4_state_owner *
437 nfs4_alloc_state_owner(void)
439 struct nfs4_state_owner *sp;
441 sp = kzalloc(sizeof(*sp),GFP_NOFS);
442 if (!sp)
443 return NULL;
444 spin_lock_init(&sp->so_lock);
445 INIT_LIST_HEAD(&sp->so_states);
446 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
447 sp->so_seqid.sequence = &sp->so_sequence;
448 spin_lock_init(&sp->so_sequence.lock);
449 INIT_LIST_HEAD(&sp->so_sequence.list);
450 atomic_set(&sp->so_count, 1);
451 return sp;
454 static void
455 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
457 if (!RB_EMPTY_NODE(&sp->so_server_node)) {
458 struct nfs_server *server = sp->so_server;
459 struct nfs_client *clp = server->nfs_client;
461 spin_lock(&clp->cl_lock);
462 rb_erase(&sp->so_server_node, &server->state_owners);
463 RB_CLEAR_NODE(&sp->so_server_node);
464 spin_unlock(&clp->cl_lock);
469 * nfs4_get_state_owner - Look up a state owner given a credential
470 * @server: nfs_server to search
471 * @cred: RPC credential to match
473 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
475 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
476 struct rpc_cred *cred)
478 struct nfs_client *clp = server->nfs_client;
479 struct nfs4_state_owner *sp, *new;
481 spin_lock(&clp->cl_lock);
482 sp = nfs4_find_state_owner_locked(server, cred);
483 spin_unlock(&clp->cl_lock);
484 if (sp != NULL)
485 return sp;
486 new = nfs4_alloc_state_owner();
487 if (new == NULL)
488 return NULL;
489 new->so_server = server;
490 new->so_cred = cred;
491 spin_lock(&clp->cl_lock);
492 sp = nfs4_insert_state_owner_locked(new);
493 spin_unlock(&clp->cl_lock);
494 if (sp == new)
495 get_rpccred(cred);
496 else {
497 rpc_destroy_wait_queue(&new->so_sequence.wait);
498 kfree(new);
500 return sp;
504 * nfs4_put_state_owner - Release a nfs4_state_owner
505 * @sp: state owner data to release
508 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
510 struct nfs_client *clp = sp->so_server->nfs_client;
511 struct rpc_cred *cred = sp->so_cred;
513 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
514 return;
515 nfs4_remove_state_owner_locked(sp);
516 spin_unlock(&clp->cl_lock);
517 rpc_destroy_wait_queue(&sp->so_sequence.wait);
518 put_rpccred(cred);
519 kfree(sp);
522 static struct nfs4_state *
523 nfs4_alloc_open_state(void)
525 struct nfs4_state *state;
527 state = kzalloc(sizeof(*state), GFP_NOFS);
528 if (!state)
529 return NULL;
530 atomic_set(&state->count, 1);
531 INIT_LIST_HEAD(&state->lock_states);
532 spin_lock_init(&state->state_lock);
533 seqlock_init(&state->seqlock);
534 return state;
537 void
538 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
540 if (state->state == fmode)
541 return;
542 /* NB! List reordering - see the reclaim code for why. */
543 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
544 if (fmode & FMODE_WRITE)
545 list_move(&state->open_states, &state->owner->so_states);
546 else
547 list_move_tail(&state->open_states, &state->owner->so_states);
549 state->state = fmode;
552 static struct nfs4_state *
553 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
555 struct nfs_inode *nfsi = NFS_I(inode);
556 struct nfs4_state *state;
558 list_for_each_entry(state, &nfsi->open_states, inode_states) {
559 if (state->owner != owner)
560 continue;
561 if (atomic_inc_not_zero(&state->count))
562 return state;
564 return NULL;
567 static void
568 nfs4_free_open_state(struct nfs4_state *state)
570 kfree(state);
573 struct nfs4_state *
574 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
576 struct nfs4_state *state, *new;
577 struct nfs_inode *nfsi = NFS_I(inode);
579 spin_lock(&inode->i_lock);
580 state = __nfs4_find_state_byowner(inode, owner);
581 spin_unlock(&inode->i_lock);
582 if (state)
583 goto out;
584 new = nfs4_alloc_open_state();
585 spin_lock(&owner->so_lock);
586 spin_lock(&inode->i_lock);
587 state = __nfs4_find_state_byowner(inode, owner);
588 if (state == NULL && new != NULL) {
589 state = new;
590 state->owner = owner;
591 atomic_inc(&owner->so_count);
592 list_add(&state->inode_states, &nfsi->open_states);
593 ihold(inode);
594 state->inode = inode;
595 spin_unlock(&inode->i_lock);
596 /* Note: The reclaim code dictates that we add stateless
597 * and read-only stateids to the end of the list */
598 list_add_tail(&state->open_states, &owner->so_states);
599 spin_unlock(&owner->so_lock);
600 } else {
601 spin_unlock(&inode->i_lock);
602 spin_unlock(&owner->so_lock);
603 if (new)
604 nfs4_free_open_state(new);
606 out:
607 return state;
610 void nfs4_put_open_state(struct nfs4_state *state)
612 struct inode *inode = state->inode;
613 struct nfs4_state_owner *owner = state->owner;
615 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
616 return;
617 spin_lock(&inode->i_lock);
618 list_del(&state->inode_states);
619 list_del(&state->open_states);
620 spin_unlock(&inode->i_lock);
621 spin_unlock(&owner->so_lock);
622 iput(inode);
623 nfs4_free_open_state(state);
624 nfs4_put_state_owner(owner);
628 * Close the current file.
630 static void __nfs4_close(struct path *path, struct nfs4_state *state,
631 fmode_t fmode, gfp_t gfp_mask, int wait)
633 struct nfs4_state_owner *owner = state->owner;
634 int call_close = 0;
635 fmode_t newstate;
637 atomic_inc(&owner->so_count);
638 /* Protect against nfs4_find_state() */
639 spin_lock(&owner->so_lock);
640 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
641 case FMODE_READ:
642 state->n_rdonly--;
643 break;
644 case FMODE_WRITE:
645 state->n_wronly--;
646 break;
647 case FMODE_READ|FMODE_WRITE:
648 state->n_rdwr--;
650 newstate = FMODE_READ|FMODE_WRITE;
651 if (state->n_rdwr == 0) {
652 if (state->n_rdonly == 0) {
653 newstate &= ~FMODE_READ;
654 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
655 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
657 if (state->n_wronly == 0) {
658 newstate &= ~FMODE_WRITE;
659 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
660 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
662 if (newstate == 0)
663 clear_bit(NFS_DELEGATED_STATE, &state->flags);
665 nfs4_state_set_mode_locked(state, newstate);
666 spin_unlock(&owner->so_lock);
668 if (!call_close) {
669 nfs4_put_open_state(state);
670 nfs4_put_state_owner(owner);
671 } else {
672 bool roc = pnfs_roc(state->inode);
674 nfs4_do_close(path, state, gfp_mask, wait, roc);
678 void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
680 __nfs4_close(path, state, fmode, GFP_NOFS, 0);
683 void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
685 __nfs4_close(path, state, fmode, GFP_KERNEL, 1);
689 * Search the state->lock_states for an existing lock_owner
690 * that is compatible with current->files
692 static struct nfs4_lock_state *
693 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
695 struct nfs4_lock_state *pos;
696 list_for_each_entry(pos, &state->lock_states, ls_locks) {
697 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
698 continue;
699 switch (pos->ls_owner.lo_type) {
700 case NFS4_POSIX_LOCK_TYPE:
701 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
702 continue;
703 break;
704 case NFS4_FLOCK_LOCK_TYPE:
705 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
706 continue;
708 atomic_inc(&pos->ls_count);
709 return pos;
711 return NULL;
715 * Return a compatible lock_state. If no initialized lock_state structure
716 * exists, return an uninitialized one.
719 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)
721 struct nfs4_lock_state *lsp;
722 struct nfs_server *server = state->owner->so_server;
723 struct nfs_client *clp = server->nfs_client;
725 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
726 if (lsp == NULL)
727 return NULL;
728 rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
729 spin_lock_init(&lsp->ls_sequence.lock);
730 INIT_LIST_HEAD(&lsp->ls_sequence.list);
731 lsp->ls_seqid.sequence = &lsp->ls_sequence;
732 atomic_set(&lsp->ls_count, 1);
733 lsp->ls_state = state;
734 lsp->ls_owner.lo_type = type;
735 switch (lsp->ls_owner.lo_type) {
736 case NFS4_FLOCK_LOCK_TYPE:
737 lsp->ls_owner.lo_u.flock_owner = fl_pid;
738 break;
739 case NFS4_POSIX_LOCK_TYPE:
740 lsp->ls_owner.lo_u.posix_owner = fl_owner;
741 break;
742 default:
743 kfree(lsp);
744 return NULL;
746 spin_lock(&clp->cl_lock);
747 nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
748 spin_unlock(&clp->cl_lock);
749 INIT_LIST_HEAD(&lsp->ls_locks);
750 return lsp;
753 static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
755 struct nfs_server *server = lsp->ls_state->owner->so_server;
756 struct nfs_client *clp = server->nfs_client;
758 spin_lock(&clp->cl_lock);
759 nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
760 spin_unlock(&clp->cl_lock);
761 rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
762 kfree(lsp);
766 * Return a compatible lock_state. If no initialized lock_state structure
767 * exists, return an uninitialized one.
770 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
772 struct nfs4_lock_state *lsp, *new = NULL;
774 for(;;) {
775 spin_lock(&state->state_lock);
776 lsp = __nfs4_find_lock_state(state, owner, pid, type);
777 if (lsp != NULL)
778 break;
779 if (new != NULL) {
780 list_add(&new->ls_locks, &state->lock_states);
781 set_bit(LK_STATE_IN_USE, &state->flags);
782 lsp = new;
783 new = NULL;
784 break;
786 spin_unlock(&state->state_lock);
787 new = nfs4_alloc_lock_state(state, owner, pid, type);
788 if (new == NULL)
789 return NULL;
791 spin_unlock(&state->state_lock);
792 if (new != NULL)
793 nfs4_free_lock_state(new);
794 return lsp;
798 * Release reference to lock_state, and free it if we see that
799 * it is no longer in use
801 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
803 struct nfs4_state *state;
805 if (lsp == NULL)
806 return;
807 state = lsp->ls_state;
808 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
809 return;
810 list_del(&lsp->ls_locks);
811 if (list_empty(&state->lock_states))
812 clear_bit(LK_STATE_IN_USE, &state->flags);
813 spin_unlock(&state->state_lock);
814 if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
815 nfs4_release_lockowner(lsp);
816 nfs4_free_lock_state(lsp);
819 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
821 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
823 dst->fl_u.nfs4_fl.owner = lsp;
824 atomic_inc(&lsp->ls_count);
827 static void nfs4_fl_release_lock(struct file_lock *fl)
829 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
832 static const struct file_lock_operations nfs4_fl_lock_ops = {
833 .fl_copy_lock = nfs4_fl_copy_lock,
834 .fl_release_private = nfs4_fl_release_lock,
837 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
839 struct nfs4_lock_state *lsp;
841 if (fl->fl_ops != NULL)
842 return 0;
843 if (fl->fl_flags & FL_POSIX)
844 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
845 else if (fl->fl_flags & FL_FLOCK)
846 lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
847 else
848 return -EINVAL;
849 if (lsp == NULL)
850 return -ENOMEM;
851 fl->fl_u.nfs4_fl.owner = lsp;
852 fl->fl_ops = &nfs4_fl_lock_ops;
853 return 0;
857 * Byte-range lock aware utility to initialize the stateid of read/write
858 * requests.
860 void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid)
862 struct nfs4_lock_state *lsp;
863 int seq;
865 do {
866 seq = read_seqbegin(&state->seqlock);
867 memcpy(dst, &state->stateid, sizeof(*dst));
868 } while (read_seqretry(&state->seqlock, seq));
869 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
870 return;
872 spin_lock(&state->state_lock);
873 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
874 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
875 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
876 spin_unlock(&state->state_lock);
877 nfs4_put_lock_state(lsp);
880 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
882 struct nfs_seqid *new;
884 new = kmalloc(sizeof(*new), gfp_mask);
885 if (new != NULL) {
886 new->sequence = counter;
887 INIT_LIST_HEAD(&new->list);
889 return new;
892 void nfs_release_seqid(struct nfs_seqid *seqid)
894 if (!list_empty(&seqid->list)) {
895 struct rpc_sequence *sequence = seqid->sequence->sequence;
897 spin_lock(&sequence->lock);
898 list_del_init(&seqid->list);
899 spin_unlock(&sequence->lock);
900 rpc_wake_up(&sequence->wait);
904 void nfs_free_seqid(struct nfs_seqid *seqid)
906 nfs_release_seqid(seqid);
907 kfree(seqid);
911 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
912 * failed with a seqid incrementing error -
913 * see comments nfs_fs.h:seqid_mutating_error()
915 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
917 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
918 switch (status) {
919 case 0:
920 break;
921 case -NFS4ERR_BAD_SEQID:
922 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
923 return;
924 printk(KERN_WARNING "NFS: v4 server returned a bad"
925 " sequence-id error on an"
926 " unconfirmed sequence %p!\n",
927 seqid->sequence);
928 case -NFS4ERR_STALE_CLIENTID:
929 case -NFS4ERR_STALE_STATEID:
930 case -NFS4ERR_BAD_STATEID:
931 case -NFS4ERR_BADXDR:
932 case -NFS4ERR_RESOURCE:
933 case -NFS4ERR_NOFILEHANDLE:
934 /* Non-seqid mutating errors */
935 return;
938 * Note: no locking needed as we are guaranteed to be first
939 * on the sequence list
941 seqid->sequence->counter++;
944 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
946 struct nfs4_state_owner *sp = container_of(seqid->sequence,
947 struct nfs4_state_owner, so_seqid);
948 struct nfs_server *server = sp->so_server;
950 if (status == -NFS4ERR_BAD_SEQID)
951 nfs4_drop_state_owner(sp);
952 if (!nfs4_has_session(server->nfs_client))
953 nfs_increment_seqid(status, seqid);
957 * Increment the seqid if the LOCK/LOCKU succeeded, or
958 * failed with a seqid incrementing error -
959 * see comments nfs_fs.h:seqid_mutating_error()
961 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
963 nfs_increment_seqid(status, seqid);
966 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
968 struct rpc_sequence *sequence = seqid->sequence->sequence;
969 int status = 0;
971 spin_lock(&sequence->lock);
972 if (list_empty(&seqid->list))
973 list_add_tail(&seqid->list, &sequence->list);
974 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
975 goto unlock;
976 rpc_sleep_on(&sequence->wait, task, NULL);
977 status = -EAGAIN;
978 unlock:
979 spin_unlock(&sequence->lock);
980 return status;
983 static int nfs4_run_state_manager(void *);
985 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
987 smp_mb__before_clear_bit();
988 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
989 smp_mb__after_clear_bit();
990 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
991 rpc_wake_up(&clp->cl_rpcwaitq);
995 * Schedule the nfs_client asynchronous state management routine
997 void nfs4_schedule_state_manager(struct nfs_client *clp)
999 struct task_struct *task;
1001 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1002 return;
1003 __module_get(THIS_MODULE);
1004 atomic_inc(&clp->cl_count);
1005 task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
1006 rpc_peeraddr2str(clp->cl_rpcclient,
1007 RPC_DISPLAY_ADDR));
1008 if (!IS_ERR(task))
1009 return;
1010 nfs4_clear_state_manager_bit(clp);
1011 nfs_put_client(clp);
1012 module_put(THIS_MODULE);
1016 * Schedule a lease recovery attempt
1018 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1020 if (!clp)
1021 return;
1022 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1023 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1024 nfs4_schedule_state_manager(clp);
1027 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1030 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1031 /* Don't recover state that expired before the reboot */
1032 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1033 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1034 return 0;
1036 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1037 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1038 return 1;
1041 static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1043 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1044 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1045 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1046 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1047 return 1;
1050 void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1052 struct nfs_client *clp = server->nfs_client;
1054 nfs4_state_mark_reclaim_nograce(clp, state);
1055 nfs4_schedule_state_manager(clp);
1058 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1060 struct inode *inode = state->inode;
1061 struct nfs_inode *nfsi = NFS_I(inode);
1062 struct file_lock *fl;
1063 int status = 0;
1065 if (inode->i_flock == NULL)
1066 return 0;
1068 /* Guard against delegation returns and new lock/unlock calls */
1069 down_write(&nfsi->rwsem);
1070 /* Protect inode->i_flock using the BKL */
1071 lock_flocks();
1072 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1073 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1074 continue;
1075 if (nfs_file_open_context(fl->fl_file)->state != state)
1076 continue;
1077 unlock_flocks();
1078 status = ops->recover_lock(state, fl);
1079 switch (status) {
1080 case 0:
1081 break;
1082 case -ESTALE:
1083 case -NFS4ERR_ADMIN_REVOKED:
1084 case -NFS4ERR_STALE_STATEID:
1085 case -NFS4ERR_BAD_STATEID:
1086 case -NFS4ERR_EXPIRED:
1087 case -NFS4ERR_NO_GRACE:
1088 case -NFS4ERR_STALE_CLIENTID:
1089 case -NFS4ERR_BADSESSION:
1090 case -NFS4ERR_BADSLOT:
1091 case -NFS4ERR_BAD_HIGH_SLOT:
1092 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1093 goto out;
1094 default:
1095 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
1096 __func__, status);
1097 case -ENOMEM:
1098 case -NFS4ERR_DENIED:
1099 case -NFS4ERR_RECLAIM_BAD:
1100 case -NFS4ERR_RECLAIM_CONFLICT:
1101 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1102 status = 0;
1104 lock_flocks();
1106 unlock_flocks();
1107 out:
1108 up_write(&nfsi->rwsem);
1109 return status;
1112 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1114 struct nfs4_state *state;
1115 struct nfs4_lock_state *lock;
1116 int status = 0;
1118 /* Note: we rely on the sp->so_states list being ordered
1119 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1120 * states first.
1121 * This is needed to ensure that the server won't give us any
1122 * read delegations that we have to return if, say, we are
1123 * recovering after a network partition or a reboot from a
1124 * server that doesn't support a grace period.
1126 restart:
1127 spin_lock(&sp->so_lock);
1128 list_for_each_entry(state, &sp->so_states, open_states) {
1129 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1130 continue;
1131 if (state->state == 0)
1132 continue;
1133 atomic_inc(&state->count);
1134 spin_unlock(&sp->so_lock);
1135 status = ops->recover_open(sp, state);
1136 if (status >= 0) {
1137 status = nfs4_reclaim_locks(state, ops);
1138 if (status >= 0) {
1139 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1140 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
1141 printk("%s: Lock reclaim failed!\n",
1142 __func__);
1144 nfs4_put_open_state(state);
1145 goto restart;
1148 switch (status) {
1149 default:
1150 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
1151 __func__, status);
1152 case -ENOENT:
1153 case -ENOMEM:
1154 case -ESTALE:
1156 * Open state on this file cannot be recovered
1157 * All we can do is revert to using the zero stateid.
1159 memset(state->stateid.data, 0,
1160 sizeof(state->stateid.data));
1161 /* Mark the file as being 'closed' */
1162 state->state = 0;
1163 break;
1164 case -EKEYEXPIRED:
1166 * User RPCSEC_GSS context has expired.
1167 * We cannot recover this stateid now, so
1168 * skip it and allow recovery thread to
1169 * proceed.
1171 break;
1172 case -NFS4ERR_ADMIN_REVOKED:
1173 case -NFS4ERR_STALE_STATEID:
1174 case -NFS4ERR_BAD_STATEID:
1175 case -NFS4ERR_RECLAIM_BAD:
1176 case -NFS4ERR_RECLAIM_CONFLICT:
1177 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1178 break;
1179 case -NFS4ERR_EXPIRED:
1180 case -NFS4ERR_NO_GRACE:
1181 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1182 case -NFS4ERR_STALE_CLIENTID:
1183 case -NFS4ERR_BADSESSION:
1184 case -NFS4ERR_BADSLOT:
1185 case -NFS4ERR_BAD_HIGH_SLOT:
1186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1187 goto out_err;
1189 nfs4_put_open_state(state);
1190 goto restart;
1192 spin_unlock(&sp->so_lock);
1193 return 0;
1194 out_err:
1195 nfs4_put_open_state(state);
1196 return status;
1199 static void nfs4_clear_open_state(struct nfs4_state *state)
1201 struct nfs4_lock_state *lock;
1203 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1204 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1205 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1206 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1207 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1208 lock->ls_seqid.flags = 0;
1209 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1213 static void nfs4_reset_seqids(struct nfs_server *server,
1214 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1216 struct nfs_client *clp = server->nfs_client;
1217 struct nfs4_state_owner *sp;
1218 struct rb_node *pos;
1219 struct nfs4_state *state;
1221 spin_lock(&clp->cl_lock);
1222 for (pos = rb_first(&server->state_owners);
1223 pos != NULL;
1224 pos = rb_next(pos)) {
1225 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1226 sp->so_seqid.flags = 0;
1227 spin_lock(&sp->so_lock);
1228 list_for_each_entry(state, &sp->so_states, open_states) {
1229 if (mark_reclaim(clp, state))
1230 nfs4_clear_open_state(state);
1232 spin_unlock(&sp->so_lock);
1234 spin_unlock(&clp->cl_lock);
1237 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1238 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1240 struct nfs_server *server;
1242 rcu_read_lock();
1243 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1244 nfs4_reset_seqids(server, mark_reclaim);
1245 rcu_read_unlock();
1248 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1250 /* Mark all delegations for reclaim */
1251 nfs_delegation_mark_reclaim(clp);
1252 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1255 static void nfs4_reclaim_complete(struct nfs_client *clp,
1256 const struct nfs4_state_recovery_ops *ops)
1258 /* Notify the server we're done reclaiming our state */
1259 if (ops->reclaim_complete)
1260 (void)ops->reclaim_complete(clp);
1263 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1265 struct nfs_client *clp = server->nfs_client;
1266 struct nfs4_state_owner *sp;
1267 struct rb_node *pos;
1268 struct nfs4_state *state;
1270 spin_lock(&clp->cl_lock);
1271 for (pos = rb_first(&server->state_owners);
1272 pos != NULL;
1273 pos = rb_next(pos)) {
1274 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1275 spin_lock(&sp->so_lock);
1276 list_for_each_entry(state, &sp->so_states, open_states) {
1277 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1278 &state->flags))
1279 continue;
1280 nfs4_state_mark_reclaim_nograce(clp, state);
1282 spin_unlock(&sp->so_lock);
1284 spin_unlock(&clp->cl_lock);
1287 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1289 struct nfs_server *server;
1291 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1292 return 0;
1294 rcu_read_lock();
1295 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1296 nfs4_clear_reclaim_server(server);
1297 rcu_read_unlock();
1299 nfs_delegation_reap_unclaimed(clp);
1300 return 1;
1303 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1305 if (!nfs4_state_clear_reclaim_reboot(clp))
1306 return;
1307 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
1310 static void nfs_delegation_clear_all(struct nfs_client *clp)
1312 nfs_delegation_mark_reclaim(clp);
1313 nfs_delegation_reap_unclaimed(clp);
1316 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1318 nfs_delegation_clear_all(clp);
1319 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1322 static void nfs4_warn_keyexpired(const char *s)
1324 printk_ratelimited(KERN_WARNING "Error: state manager"
1325 " encountered RPCSEC_GSS session"
1326 " expired against NFSv4 server %s.\n",
1330 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1332 switch (error) {
1333 case -NFS4ERR_CB_PATH_DOWN:
1334 nfs_handle_cb_pathdown(clp);
1335 return 0;
1336 case -NFS4ERR_NO_GRACE:
1337 nfs4_state_end_reclaim_reboot(clp);
1338 return 0;
1339 case -NFS4ERR_STALE_CLIENTID:
1340 case -NFS4ERR_LEASE_MOVED:
1341 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1342 nfs4_state_clear_reclaim_reboot(clp);
1343 nfs4_state_start_reclaim_reboot(clp);
1344 break;
1345 case -NFS4ERR_EXPIRED:
1346 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1347 nfs4_state_start_reclaim_nograce(clp);
1348 break;
1349 case -NFS4ERR_BADSESSION:
1350 case -NFS4ERR_BADSLOT:
1351 case -NFS4ERR_BAD_HIGH_SLOT:
1352 case -NFS4ERR_DEADSESSION:
1353 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1354 case -NFS4ERR_SEQ_FALSE_RETRY:
1355 case -NFS4ERR_SEQ_MISORDERED:
1356 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1357 /* Zero session reset errors */
1358 return 0;
1359 case -EKEYEXPIRED:
1360 /* Nothing we can do */
1361 nfs4_warn_keyexpired(clp->cl_hostname);
1362 return 0;
1364 return error;
1367 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1369 struct nfs4_state_owner *sp;
1370 struct nfs_server *server;
1371 struct rb_node *pos;
1372 int status = 0;
1374 restart:
1375 rcu_read_lock();
1376 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1377 spin_lock(&clp->cl_lock);
1378 for (pos = rb_first(&server->state_owners);
1379 pos != NULL;
1380 pos = rb_next(pos)) {
1381 sp = rb_entry(pos,
1382 struct nfs4_state_owner, so_server_node);
1383 if (!test_and_clear_bit(ops->owner_flag_bit,
1384 &sp->so_flags))
1385 continue;
1386 atomic_inc(&sp->so_count);
1387 spin_unlock(&clp->cl_lock);
1388 rcu_read_unlock();
1390 status = nfs4_reclaim_open_state(sp, ops);
1391 if (status < 0) {
1392 set_bit(ops->owner_flag_bit, &sp->so_flags);
1393 nfs4_put_state_owner(sp);
1394 return nfs4_recovery_handle_error(clp, status);
1397 nfs4_put_state_owner(sp);
1398 goto restart;
1400 spin_unlock(&clp->cl_lock);
1402 rcu_read_unlock();
1403 return status;
1406 static int nfs4_check_lease(struct nfs_client *clp)
1408 struct rpc_cred *cred;
1409 const struct nfs4_state_maintenance_ops *ops =
1410 clp->cl_mvops->state_renewal_ops;
1411 int status = -NFS4ERR_EXPIRED;
1413 /* Is the client already known to have an expired lease? */
1414 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1415 return 0;
1416 spin_lock(&clp->cl_lock);
1417 cred = ops->get_state_renewal_cred_locked(clp);
1418 spin_unlock(&clp->cl_lock);
1419 if (cred == NULL) {
1420 cred = nfs4_get_setclientid_cred(clp);
1421 if (cred == NULL)
1422 goto out;
1424 status = ops->renew_lease(clp, cred);
1425 put_rpccred(cred);
1426 out:
1427 return nfs4_recovery_handle_error(clp, status);
1430 static int nfs4_reclaim_lease(struct nfs_client *clp)
1432 struct rpc_cred *cred;
1433 const struct nfs4_state_recovery_ops *ops =
1434 clp->cl_mvops->reboot_recovery_ops;
1435 int status = -ENOENT;
1437 cred = ops->get_clid_cred(clp);
1438 if (cred != NULL) {
1439 status = ops->establish_clid(clp, cred);
1440 put_rpccred(cred);
1441 /* Handle case where the user hasn't set up machine creds */
1442 if (status == -EACCES && cred == clp->cl_machine_cred) {
1443 nfs4_clear_machine_cred(clp);
1444 status = -EAGAIN;
1446 if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1447 status = -EPROTONOSUPPORT;
1449 return status;
1452 #ifdef CONFIG_NFS_V4_1
1453 void nfs4_schedule_session_recovery(struct nfs4_session *session)
1455 nfs4_schedule_lease_recovery(session->clp);
1457 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
1459 void nfs41_handle_recall_slot(struct nfs_client *clp)
1461 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1462 nfs4_schedule_state_manager(clp);
1465 static void nfs4_reset_all_state(struct nfs_client *clp)
1467 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1468 clp->cl_boot_time = CURRENT_TIME;
1469 nfs4_state_start_reclaim_nograce(clp);
1470 nfs4_schedule_state_manager(clp);
1474 static void nfs41_handle_server_reboot(struct nfs_client *clp)
1476 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1477 nfs4_state_start_reclaim_reboot(clp);
1478 nfs4_schedule_state_manager(clp);
1482 static void nfs41_handle_state_revoked(struct nfs_client *clp)
1484 /* Temporary */
1485 nfs4_reset_all_state(clp);
1488 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1490 /* This will need to handle layouts too */
1491 nfs_expire_all_delegations(clp);
1494 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1496 nfs_expire_all_delegations(clp);
1497 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
1498 nfs4_schedule_state_manager(clp);
1501 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1503 if (!flags)
1504 return;
1505 else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1506 nfs41_handle_server_reboot(clp);
1507 else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
1508 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1509 SEQ4_STATUS_ADMIN_STATE_REVOKED |
1510 SEQ4_STATUS_LEASE_MOVED))
1511 nfs41_handle_state_revoked(clp);
1512 else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1513 nfs41_handle_recallable_state_revoked(clp);
1514 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
1515 SEQ4_STATUS_BACKCHANNEL_FAULT |
1516 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
1517 nfs41_handle_cb_path_down(clp);
1520 static int nfs4_reset_session(struct nfs_client *clp)
1522 int status;
1524 nfs4_begin_drain_session(clp);
1525 status = nfs4_proc_destroy_session(clp->cl_session);
1526 if (status && status != -NFS4ERR_BADSESSION &&
1527 status != -NFS4ERR_DEADSESSION) {
1528 status = nfs4_recovery_handle_error(clp, status);
1529 goto out;
1532 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
1533 status = nfs4_proc_create_session(clp);
1534 if (status) {
1535 status = nfs4_recovery_handle_error(clp, status);
1536 goto out;
1538 /* create_session negotiated new slot table */
1539 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1541 /* Let the state manager reestablish state */
1542 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1543 nfs41_setup_state_renewal(clp);
1544 out:
1545 return status;
1548 static int nfs4_recall_slot(struct nfs_client *clp)
1550 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1551 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1552 struct nfs4_slot *new, *old;
1553 int i;
1555 nfs4_begin_drain_session(clp);
1556 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
1557 GFP_NOFS);
1558 if (!new)
1559 return -ENOMEM;
1561 spin_lock(&fc_tbl->slot_tbl_lock);
1562 for (i = 0; i < fc_tbl->target_max_slots; i++)
1563 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1564 old = fc_tbl->slots;
1565 fc_tbl->slots = new;
1566 fc_tbl->max_slots = fc_tbl->target_max_slots;
1567 fc_tbl->target_max_slots = 0;
1568 fc_attrs->max_reqs = fc_tbl->max_slots;
1569 spin_unlock(&fc_tbl->slot_tbl_lock);
1571 kfree(old);
1572 nfs4_end_drain_session(clp);
1573 return 0;
1576 #else /* CONFIG_NFS_V4_1 */
1577 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
1578 static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
1579 static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
1580 #endif /* CONFIG_NFS_V4_1 */
1582 /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1583 * on EXCHANGE_ID for v4.1
1585 static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1587 if (nfs4_has_session(clp)) {
1588 switch (status) {
1589 case -NFS4ERR_DELAY:
1590 case -NFS4ERR_CLID_INUSE:
1591 case -EAGAIN:
1592 break;
1594 case -EKEYEXPIRED:
1595 nfs4_warn_keyexpired(clp->cl_hostname);
1596 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1597 * in nfs4_exchange_id */
1598 default:
1599 return;
1602 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1605 static void nfs4_state_manager(struct nfs_client *clp)
1607 int status = 0;
1609 /* Ensure exclusive access to NFSv4 state */
1610 for(;;) {
1611 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1612 /* We're going to have to re-establish a clientid */
1613 status = nfs4_reclaim_lease(clp);
1614 if (status) {
1615 nfs4_set_lease_expired(clp, status);
1616 if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1617 &clp->cl_state))
1618 continue;
1619 if (clp->cl_cons_state ==
1620 NFS_CS_SESSION_INITING)
1621 nfs_mark_client_ready(clp, status);
1622 goto out_error;
1624 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1625 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1626 pnfs_destroy_all_layouts(clp);
1629 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1630 status = nfs4_check_lease(clp);
1631 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1632 continue;
1633 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1634 goto out_error;
1637 /* Initialize or reset the session */
1638 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
1639 && nfs4_has_session(clp)) {
1640 status = nfs4_reset_session(clp);
1641 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1642 continue;
1643 if (status < 0)
1644 goto out_error;
1647 /* First recover reboot state... */
1648 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
1649 status = nfs4_do_reclaim(clp,
1650 clp->cl_mvops->reboot_recovery_ops);
1651 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
1652 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
1653 continue;
1654 nfs4_state_end_reclaim_reboot(clp);
1655 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1656 continue;
1657 if (status < 0)
1658 goto out_error;
1661 /* Now recover expired state... */
1662 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
1663 status = nfs4_do_reclaim(clp,
1664 clp->cl_mvops->nograce_recovery_ops);
1665 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
1666 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
1667 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1668 continue;
1669 if (status < 0)
1670 goto out_error;
1673 nfs4_end_drain_session(clp);
1674 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1675 nfs_client_return_marked_delegations(clp);
1676 continue;
1678 /* Recall session slots */
1679 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1680 && nfs4_has_session(clp)) {
1681 status = nfs4_recall_slot(clp);
1682 if (status < 0)
1683 goto out_error;
1684 continue;
1688 nfs4_clear_state_manager_bit(clp);
1689 /* Did we race with an attempt to give us more work? */
1690 if (clp->cl_state == 0)
1691 break;
1692 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1693 break;
1695 return;
1696 out_error:
1697 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
1698 " with error %d\n", clp->cl_hostname, -status);
1699 nfs4_end_drain_session(clp);
1700 nfs4_clear_state_manager_bit(clp);
1703 static int nfs4_run_state_manager(void *ptr)
1705 struct nfs_client *clp = ptr;
1707 allow_signal(SIGKILL);
1708 nfs4_state_manager(clp);
1709 nfs_put_client(clp);
1710 module_put_and_exit(0);
1711 return 0;
1715 * Local variables:
1716 * c-basic-offset: 8
1717 * End: