]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/nfs/nfs4state.c
SUNRPC: Fix the SUNRPC Kerberos V RPCSEC_GSS module dependencies
[mirror_ubuntu-zesty-kernel.git] / fs / nfs / nfs4state.c
CommitLineData
1da177e4
LT
1/*
2 * fs/nfs/nfs4state.c
3 *
4 * Client-side XDR for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
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.
23 *
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.
35 *
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.
39 */
40
6f43ddcc 41#include <linux/kernel.h>
1da177e4 42#include <linux/slab.h>
b89f4321 43#include <linux/fs.h>
1da177e4
LT
44#include <linux/nfs_fs.h>
45#include <linux/nfs_idmap.h>
5043e900
TM
46#include <linux/kthread.h>
47#include <linux/module.h>
9f958ab8 48#include <linux/random.h>
8c7597f6 49#include <linux/ratelimit.h>
1da177e4
LT
50#include <linux/workqueue.h>
51#include <linux/bitops.h>
52
4ce79717 53#include "nfs4_fs.h"
1da177e4
LT
54#include "callback.h"
55#include "delegation.h"
24c8dbbb 56#include "internal.h"
974cec8c 57#include "pnfs.h"
1da177e4
LT
58
59#define OPENOWNER_POOL_SIZE 8
60
4ce79717 61const nfs4_stateid zero_stateid;
1da177e4
LT
62
63static LIST_HEAD(nfs4_clientid_list);
64
591d71cb 65int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
1da177e4 66{
bb8b27e5 67 struct nfs4_setclientid_res clid;
f738f517
CL
68 unsigned short port;
69 int status;
70
71 port = nfs_callback_tcpport;
72 if (clp->cl_addr.ss_family == AF_INET6)
73 port = nfs_callback_tcpport6;
74
bb8b27e5
TM
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);
83out:
1da177e4
LT
84 return status;
85}
86
a7b72103 87struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
a2b2bb88
TM
88{
89 struct rpc_cred *cred = NULL;
90
a2b2bb88
TM
91 if (clp->cl_machine_cred != NULL)
92 cred = get_rpccred(clp->cl_machine_cred);
a2b2bb88
TM
93 return cred;
94}
95
96static void nfs4_clear_machine_cred(struct nfs_client *clp)
97{
98 struct rpc_cred *cred;
99
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);
106}
107
24d292b8
CL
108static struct rpc_cred *
109nfs4_get_renew_cred_server_locked(struct nfs_server *server)
b4454fe1 110{
24d292b8 111 struct rpc_cred *cred = NULL;
b4454fe1 112 struct nfs4_state_owner *sp;
9f958ab8 113 struct rb_node *pos;
b4454fe1 114
24d292b8
CL
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);
b4454fe1
TM
119 if (list_empty(&sp->so_states))
120 continue;
121 cred = get_rpccred(sp->so_cred);
122 break;
123 }
124 return cred;
125}
126
24d292b8
CL
127/**
128 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
129 * @clp: client state handle
130 *
131 * Returns an rpc_cred with reference count bumped, or NULL.
132 * Caller must hold clp->cl_lock.
133 */
134struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
135{
136 struct rpc_cred *cred = NULL;
137 struct nfs_server *server;
138
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;
144 }
145 rcu_read_unlock();
146 return cred;
147}
148
b4b82607
AA
149#if defined(CONFIG_NFS_V4_1)
150
9430fb6b
RL
151static int nfs41_setup_state_renewal(struct nfs_client *clp)
152{
153 int status;
154 struct nfs_fsinfo fsinfo;
155
d6fb79d4
AA
156 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
157 nfs4_schedule_state_renewal(clp);
158 return 0;
159 }
160
9430fb6b
RL
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);
168
169 nfs4_schedule_state_renewal(clp);
170 }
171
172 return status;
173}
174
42acd021
AA
175/*
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.
179 */
5601a00d 180static void nfs4_end_drain_session(struct nfs_client *clp)
9dfdf404 181{
5601a00d 182 struct nfs4_session *ses = clp->cl_session;
689cf5c1
AB
183 int max_slots;
184
a2118c33
TM
185 if (ses == NULL)
186 return;
187 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
689cf5c1
AB
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;
192
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);
198 }
199 spin_unlock(&ses->fc_slot_table.slot_tbl_lock);
200 }
9dfdf404
RL
201}
202
42acd021 203static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
9dfdf404 204{
9dfdf404 205 spin_lock(&tbl->slot_tbl_lock);
9dfdf404 206 if (tbl->highest_used_slotid != -1) {
42acd021 207 INIT_COMPLETION(tbl->complete);
9dfdf404 208 spin_unlock(&tbl->slot_tbl_lock);
42acd021 209 return wait_for_completion_interruptible(&tbl->complete);
9dfdf404
RL
210 }
211 spin_unlock(&tbl->slot_tbl_lock);
212 return 0;
213}
214
42acd021
AA
215static int nfs4_begin_drain_session(struct nfs_client *clp)
216{
217 struct nfs4_session *ses = clp->cl_session;
218 int ret = 0;
219
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);
227}
228
4d643d1d
AA
229int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
230{
231 int status;
232
38045412 233 nfs4_begin_drain_session(clp);
4d643d1d 234 status = nfs4_proc_exchange_id(clp, cred);
9430fb6b
RL
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);
242out:
4d643d1d
AA
243 return status;
244}
245
b4b82607
AA
246struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
247{
248 struct rpc_cred *cred;
249
250 spin_lock(&clp->cl_lock);
251 cred = nfs4_get_machine_cred_locked(clp);
252 spin_unlock(&clp->cl_lock);
253 return cred;
254}
255
256#endif /* CONFIG_NFS_V4_1 */
257
24d292b8
CL
258static struct rpc_cred *
259nfs4_get_setclientid_cred_server(struct nfs_server *server)
286d7d6a 260{
24d292b8
CL
261 struct nfs_client *clp = server->nfs_client;
262 struct rpc_cred *cred = NULL;
286d7d6a 263 struct nfs4_state_owner *sp;
9f958ab8 264 struct rb_node *pos;
24d292b8
CL
265
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);
271 }
272 spin_unlock(&clp->cl_lock);
273 return cred;
274}
275
276/**
277 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
278 * @clp: client state handle
279 *
280 * Returns an rpc_cred with reference count bumped, or NULL.
281 */
282struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
283{
284 struct nfs_server *server;
a2b2bb88 285 struct rpc_cred *cred;
286d7d6a 286
6dc9d57a
TM
287 spin_lock(&clp->cl_lock);
288 cred = nfs4_get_machine_cred_locked(clp);
24d292b8 289 spin_unlock(&clp->cl_lock);
a2b2bb88
TM
290 if (cred != NULL)
291 goto out;
24d292b8
CL
292
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;
286d7d6a 298 }
24d292b8
CL
299 rcu_read_unlock();
300
a2b2bb88
TM
301out:
302 return cred;
286d7d6a
TM
303}
304
24d292b8
CL
305static void nfs_alloc_unique_id_locked(struct rb_root *root,
306 struct nfs_unique_id *new,
307 __u64 minval, int maxbits)
9f958ab8
TM
308{
309 struct rb_node **p, *parent;
310 struct nfs_unique_id *pos;
311 __u64 mask = ~0ULL;
312
313 if (maxbits < 64)
314 mask = (1ULL << maxbits) - 1ULL;
315
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;
321retry:
322 p = &root->rb_node;
323 parent = NULL;
324
325 while (*p != NULL) {
326 parent = *p;
327 pos = rb_entry(parent, struct nfs_unique_id, rb_node);
328
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;
335 }
336 rb_link_node(&new->rb_node, parent, p);
337 rb_insert_color(&new->rb_node, root);
338 return;
339id_exists:
340 for (;;) {
341 new->id++;
342 if (new->id < minval || (new->id & mask) != new->id) {
343 new->id = minval;
344 break;
345 }
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;
352 }
353 goto retry;
354}
355
356static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
357{
358 rb_erase(&id->rb_node, root);
359}
360
1da177e4 361static struct nfs4_state_owner *
24d292b8 362nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
1da177e4 363{
24d292b8 364 struct rb_node **p = &server->state_owners.rb_node,
9f958ab8 365 *parent = NULL;
1da177e4
LT
366 struct nfs4_state_owner *sp, *res = NULL;
367
9f958ab8
TM
368 while (*p != NULL) {
369 parent = *p;
24d292b8 370 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
9f958ab8 371
6f2e64d3
TM
372 if (server < sp->so_server) {
373 p = &parent->rb_left;
374 continue;
375 }
376 if (server > sp->so_server) {
377 p = &parent->rb_right;
378 continue;
379 }
9f958ab8
TM
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;
388 }
1da177e4
LT
389 }
390 return res;
391}
392
9f958ab8 393static struct nfs4_state_owner *
24d292b8 394nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
9f958ab8 395{
24d292b8
CL
396 struct nfs_server *server = new->so_server;
397 struct rb_node **p = &server->state_owners.rb_node,
9f958ab8
TM
398 *parent = NULL;
399 struct nfs4_state_owner *sp;
400
401 while (*p != NULL) {
402 parent = *p;
24d292b8 403 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
9f958ab8
TM
404
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;
412 }
413 }
24d292b8
CL
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);
9f958ab8
TM
418 return new;
419}
420
421static void
24d292b8 422nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
9f958ab8 423{
24d292b8
CL
424 struct nfs_server *server = sp->so_server;
425
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);
9f958ab8
TM
429}
430
1da177e4
LT
431/*
432 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
433 * create a new state_owner.
434 *
435 */
436static struct nfs4_state_owner *
437nfs4_alloc_state_owner(void)
438{
439 struct nfs4_state_owner *sp;
440
8535b2be 441 sp = kzalloc(sizeof(*sp),GFP_NOFS);
1da177e4
LT
442 if (!sp)
443 return NULL;
ec073428 444 spin_lock_init(&sp->so_lock);
1da177e4 445 INIT_LIST_HEAD(&sp->so_states);
cee54fc9
TM
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);
1da177e4
LT
450 atomic_set(&sp->so_count, 1);
451 return sp;
452}
453
1d2e88e7 454static void
1da177e4
LT
455nfs4_drop_state_owner(struct nfs4_state_owner *sp)
456{
24d292b8
CL
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;
9f958ab8
TM
460
461 spin_lock(&clp->cl_lock);
24d292b8
CL
462 rb_erase(&sp->so_server_node, &server->state_owners);
463 RB_CLEAR_NODE(&sp->so_server_node);
9f958ab8
TM
464 spin_unlock(&clp->cl_lock);
465 }
1da177e4
LT
466}
467
24d292b8
CL
468/**
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
472 *
473 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
474 */
475struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
476 struct rpc_cred *cred)
1da177e4 477{
7539bbab 478 struct nfs_client *clp = server->nfs_client;
1da177e4
LT
479 struct nfs4_state_owner *sp, *new;
480
1da177e4 481 spin_lock(&clp->cl_lock);
24d292b8 482 sp = nfs4_find_state_owner_locked(server, cred);
1da177e4 483 spin_unlock(&clp->cl_lock);
1da177e4
LT
484 if (sp != NULL)
485 return sp;
9f958ab8
TM
486 new = nfs4_alloc_state_owner();
487 if (new == NULL)
488 return NULL;
6f2e64d3 489 new->so_server = server;
9f958ab8
TM
490 new->so_cred = cred;
491 spin_lock(&clp->cl_lock);
24d292b8 492 sp = nfs4_insert_state_owner_locked(new);
9f958ab8
TM
493 spin_unlock(&clp->cl_lock);
494 if (sp == new)
495 get_rpccred(cred);
f6a1cc89
TM
496 else {
497 rpc_destroy_wait_queue(&new->so_sequence.wait);
9f958ab8 498 kfree(new);
f6a1cc89 499 }
9f958ab8 500 return sp;
1da177e4
LT
501}
502
24d292b8
CL
503/**
504 * nfs4_put_state_owner - Release a nfs4_state_owner
505 * @sp: state owner data to release
506 *
507 */
1da177e4
LT
508void nfs4_put_state_owner(struct nfs4_state_owner *sp)
509{
1f0e890d 510 struct nfs_client *clp = sp->so_server->nfs_client;
1da177e4
LT
511 struct rpc_cred *cred = sp->so_cred;
512
513 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
514 return;
24d292b8 515 nfs4_remove_state_owner_locked(sp);
1da177e4 516 spin_unlock(&clp->cl_lock);
f6a1cc89 517 rpc_destroy_wait_queue(&sp->so_sequence.wait);
1da177e4
LT
518 put_rpccred(cred);
519 kfree(sp);
520}
521
522static struct nfs4_state *
523nfs4_alloc_open_state(void)
524{
525 struct nfs4_state *state;
526
8535b2be 527 state = kzalloc(sizeof(*state), GFP_NOFS);
1da177e4
LT
528 if (!state)
529 return NULL;
1da177e4
LT
530 atomic_set(&state->count, 1);
531 INIT_LIST_HEAD(&state->lock_states);
8d0a8a9d 532 spin_lock_init(&state->state_lock);
8bda4e4c 533 seqlock_init(&state->seqlock);
1da177e4
LT
534 return state;
535}
536
4cecb76f 537void
dc0b027d 538nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
4cecb76f 539{
dc0b027d 540 if (state->state == fmode)
4cecb76f
TM
541 return;
542 /* NB! List reordering - see the reclaim code for why. */
dc0b027d
TM
543 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
544 if (fmode & FMODE_WRITE)
4cecb76f
TM
545 list_move(&state->open_states, &state->owner->so_states);
546 else
547 list_move_tail(&state->open_states, &state->owner->so_states);
548 }
dc0b027d 549 state->state = fmode;
4cecb76f
TM
550}
551
1da177e4
LT
552static struct nfs4_state *
553__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
554{
555 struct nfs_inode *nfsi = NFS_I(inode);
556 struct nfs4_state *state;
557
558 list_for_each_entry(state, &nfsi->open_states, inode_states) {
1c816efa 559 if (state->owner != owner)
1da177e4 560 continue;
1c816efa 561 if (atomic_inc_not_zero(&state->count))
1da177e4 562 return state;
1da177e4
LT
563 }
564 return NULL;
565}
566
1da177e4
LT
567static void
568nfs4_free_open_state(struct nfs4_state *state)
569{
570 kfree(state);
571}
572
573struct nfs4_state *
574nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
575{
576 struct nfs4_state *state, *new;
577 struct nfs_inode *nfsi = NFS_I(inode);
578
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();
ec073428 585 spin_lock(&owner->so_lock);
1da177e4
LT
586 spin_lock(&inode->i_lock);
587 state = __nfs4_find_state_byowner(inode, owner);
588 if (state == NULL && new != NULL) {
589 state = new;
1da177e4
LT
590 state->owner = owner;
591 atomic_inc(&owner->so_count);
592 list_add(&state->inode_states, &nfsi->open_states);
0444d76a
DC
593 ihold(inode);
594 state->inode = inode;
1da177e4 595 spin_unlock(&inode->i_lock);
ec073428
TM
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);
1da177e4
LT
600 } else {
601 spin_unlock(&inode->i_lock);
ec073428 602 spin_unlock(&owner->so_lock);
1da177e4
LT
603 if (new)
604 nfs4_free_open_state(new);
605 }
606out:
607 return state;
608}
609
1da177e4
LT
610void nfs4_put_open_state(struct nfs4_state *state)
611{
612 struct inode *inode = state->inode;
613 struct nfs4_state_owner *owner = state->owner;
614
ec073428 615 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
1da177e4 616 return;
ec073428 617 spin_lock(&inode->i_lock);
ba683031 618 list_del(&state->inode_states);
1da177e4 619 list_del(&state->open_states);
ec073428
TM
620 spin_unlock(&inode->i_lock);
621 spin_unlock(&owner->so_lock);
1da177e4 622 iput(inode);
1da177e4
LT
623 nfs4_free_open_state(state);
624 nfs4_put_state_owner(owner);
625}
626
627/*
83c9d41e 628 * Close the current file.
1da177e4 629 */
8535b2be
TM
630static void __nfs4_close(struct path *path, struct nfs4_state *state,
631 fmode_t fmode, gfp_t gfp_mask, int wait)
1da177e4 632{
1da177e4 633 struct nfs4_state_owner *owner = state->owner;
003707c7 634 int call_close = 0;
dc0b027d 635 fmode_t newstate;
1da177e4
LT
636
637 atomic_inc(&owner->so_count);
1da177e4 638 /* Protect against nfs4_find_state() */
ec073428 639 spin_lock(&owner->so_lock);
dc0b027d 640 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
e7616923
TM
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--;
649 }
003707c7 650 newstate = FMODE_READ|FMODE_WRITE;
e7616923 651 if (state->n_rdwr == 0) {
003707c7 652 if (state->n_rdonly == 0) {
e7616923 653 newstate &= ~FMODE_READ;
003707c7
TM
654 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
655 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
656 }
657 if (state->n_wronly == 0) {
e7616923 658 newstate &= ~FMODE_WRITE;
003707c7
TM
659 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
660 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
661 }
662 if (newstate == 0)
663 clear_bit(NFS_DELEGATED_STATE, &state->flags);
e7616923 664 }
003707c7 665 nfs4_state_set_mode_locked(state, newstate);
ec073428 666 spin_unlock(&owner->so_lock);
4cecb76f 667
003707c7 668 if (!call_close) {
b39e625b
TM
669 nfs4_put_open_state(state);
670 nfs4_put_state_owner(owner);
f7e8917a
FI
671 } else {
672 bool roc = pnfs_roc(state->inode);
673
674 nfs4_do_close(path, state, gfp_mask, wait, roc);
675 }
a49c3c77
TM
676}
677
dc0b027d 678void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
a49c3c77 679{
8535b2be 680 __nfs4_close(path, state, fmode, GFP_NOFS, 0);
a49c3c77
TM
681}
682
dc0b027d 683void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
a49c3c77 684{
8535b2be 685 __nfs4_close(path, state, fmode, GFP_KERNEL, 1);
1da177e4
LT
686}
687
688/*
689 * Search the state->lock_states for an existing lock_owner
690 * that is compatible with current->files
691 */
692static struct nfs4_lock_state *
77041ed9 693__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
1da177e4
LT
694{
695 struct nfs4_lock_state *pos;
696 list_for_each_entry(pos, &state->lock_states, ls_locks) {
77041ed9 697 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
1da177e4 698 continue;
77041ed9
TM
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;
707 }
1da177e4
LT
708 atomic_inc(&pos->ls_count);
709 return pos;
710 }
711 return NULL;
712}
713
1da177e4
LT
714/*
715 * Return a compatible lock_state. If no initialized lock_state structure
716 * exists, return an uninitialized one.
717 *
1da177e4 718 */
77041ed9 719static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
1da177e4
LT
720{
721 struct nfs4_lock_state *lsp;
24d292b8
CL
722 struct nfs_server *server = state->owner->so_server;
723 struct nfs_client *clp = server->nfs_client;
1da177e4 724
8535b2be 725 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
1da177e4
LT
726 if (lsp == NULL)
727 return NULL;
d0dc3701
TM
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;
1da177e4 732 atomic_set(&lsp->ls_count, 1);
b64aec8d 733 lsp->ls_state = state;
77041ed9
TM
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;
745 }
1da177e4 746 spin_lock(&clp->cl_lock);
24d292b8 747 nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
1da177e4 748 spin_unlock(&clp->cl_lock);
8d0a8a9d 749 INIT_LIST_HEAD(&lsp->ls_locks);
1da177e4
LT
750 return lsp;
751}
752
9f958ab8
TM
753static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
754{
24d292b8
CL
755 struct nfs_server *server = lsp->ls_state->owner->so_server;
756 struct nfs_client *clp = server->nfs_client;
9f958ab8
TM
757
758 spin_lock(&clp->cl_lock);
24d292b8 759 nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
9f958ab8 760 spin_unlock(&clp->cl_lock);
f6a1cc89 761 rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
9f958ab8
TM
762 kfree(lsp);
763}
764
1da177e4
LT
765/*
766 * Return a compatible lock_state. If no initialized lock_state structure
767 * exists, return an uninitialized one.
768 *
1da177e4 769 */
77041ed9 770static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
1da177e4 771{
8d0a8a9d 772 struct nfs4_lock_state *lsp, *new = NULL;
1da177e4 773
8d0a8a9d
TM
774 for(;;) {
775 spin_lock(&state->state_lock);
77041ed9 776 lsp = __nfs4_find_lock_state(state, owner, pid, type);
8d0a8a9d
TM
777 if (lsp != NULL)
778 break;
779 if (new != NULL) {
8d0a8a9d
TM
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;
785 }
786 spin_unlock(&state->state_lock);
77041ed9 787 new = nfs4_alloc_lock_state(state, owner, pid, type);
8d0a8a9d
TM
788 if (new == NULL)
789 return NULL;
790 }
791 spin_unlock(&state->state_lock);
9f958ab8
TM
792 if (new != NULL)
793 nfs4_free_lock_state(new);
1da177e4
LT
794 return lsp;
795}
796
797/*
8d0a8a9d
TM
798 * Release reference to lock_state, and free it if we see that
799 * it is no longer in use
1da177e4 800 */
faf5f49c 801void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
1da177e4 802{
8d0a8a9d 803 struct nfs4_state *state;
1da177e4 804
8d0a8a9d
TM
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);
d3c7b7cc
TM
814 if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
815 nfs4_release_lockowner(lsp);
9f958ab8 816 nfs4_free_lock_state(lsp);
1da177e4
LT
817}
818
8d0a8a9d 819static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
1da177e4 820{
8d0a8a9d 821 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
1da177e4 822
8d0a8a9d
TM
823 dst->fl_u.nfs4_fl.owner = lsp;
824 atomic_inc(&lsp->ls_count);
825}
1da177e4 826
8d0a8a9d 827static void nfs4_fl_release_lock(struct file_lock *fl)
1da177e4 828{
8d0a8a9d 829 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
1da177e4
LT
830}
831
6aed6285 832static const struct file_lock_operations nfs4_fl_lock_ops = {
8d0a8a9d
TM
833 .fl_copy_lock = nfs4_fl_copy_lock,
834 .fl_release_private = nfs4_fl_release_lock,
835};
836
837int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
1da177e4 838{
8d0a8a9d
TM
839 struct nfs4_lock_state *lsp;
840
841 if (fl->fl_ops != NULL)
842 return 0;
77041ed9
TM
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;
8d0a8a9d
TM
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;
1da177e4
LT
854}
855
8d0a8a9d
TM
856/*
857 * Byte-range lock aware utility to initialize the stateid of read/write
858 * requests.
1da177e4 859 */
77041ed9 860void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid)
1da177e4 861{
8d0a8a9d 862 struct nfs4_lock_state *lsp;
8bda4e4c 863 int seq;
1da177e4 864
8bda4e4c
TM
865 do {
866 seq = read_seqbegin(&state->seqlock);
867 memcpy(dst, &state->stateid, sizeof(*dst));
868 } while (read_seqretry(&state->seqlock, seq));
8d0a8a9d
TM
869 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
870 return;
1da177e4 871
8d0a8a9d 872 spin_lock(&state->state_lock);
77041ed9 873 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
8d0a8a9d
TM
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);
1da177e4
LT
877 nfs4_put_lock_state(lsp);
878}
879
8535b2be 880struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
cee54fc9 881{
cee54fc9
TM
882 struct nfs_seqid *new;
883
8535b2be 884 new = kmalloc(sizeof(*new), gfp_mask);
cee54fc9
TM
885 if (new != NULL) {
886 new->sequence = counter;
2f74c0a0 887 INIT_LIST_HEAD(&new->list);
cee54fc9
TM
888 }
889 return new;
890}
891
72211dbe 892void nfs_release_seqid(struct nfs_seqid *seqid)
1da177e4 893{
2f74c0a0
TM
894 if (!list_empty(&seqid->list)) {
895 struct rpc_sequence *sequence = seqid->sequence->sequence;
cee54fc9 896
2f74c0a0 897 spin_lock(&sequence->lock);
72211dbe 898 list_del_init(&seqid->list);
2f74c0a0
TM
899 spin_unlock(&sequence->lock);
900 rpc_wake_up(&sequence->wait);
901 }
72211dbe
TM
902}
903
904void nfs_free_seqid(struct nfs_seqid *seqid)
905{
906 nfs_release_seqid(seqid);
cee54fc9 907 kfree(seqid);
1da177e4
LT
908}
909
910/*
cee54fc9
TM
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()
914 */
88d90939 915static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
cee54fc9 916{
2f74c0a0 917 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
cee54fc9
TM
918 switch (status) {
919 case 0:
920 break;
921 case -NFS4ERR_BAD_SEQID:
6f43ddcc
TM
922 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
923 return;
924 printk(KERN_WARNING "NFS: v4 server returned a bad"
497799e7
DM
925 " sequence-id error on an"
926 " unconfirmed sequence %p!\n",
6f43ddcc 927 seqid->sequence);
cee54fc9
TM
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;
936 };
937 /*
938 * Note: no locking needed as we are guaranteed to be first
939 * on the sequence list
940 */
941 seqid->sequence->counter++;
942}
943
944void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
945{
34dc1ad7
BH
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;
949
950 if (status == -NFS4ERR_BAD_SEQID)
1da177e4 951 nfs4_drop_state_owner(sp);
34dc1ad7
BH
952 if (!nfs4_has_session(server->nfs_client))
953 nfs_increment_seqid(status, seqid);
cee54fc9
TM
954}
955
956/*
cee54fc9
TM
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()
960 */
961void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
962{
88d90939 963 nfs_increment_seqid(status, seqid);
cee54fc9
TM
964}
965
966int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
967{
968 struct rpc_sequence *sequence = seqid->sequence->sequence;
969 int status = 0;
970
971 spin_lock(&sequence->lock);
2f74c0a0
TM
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;
5d00837b 976 rpc_sleep_on(&sequence->wait, task, NULL);
2f74c0a0
TM
977 status = -EAGAIN;
978unlock:
cee54fc9
TM
979 spin_unlock(&sequence->lock);
980 return status;
1da177e4
LT
981}
982
e005e804 983static int nfs4_run_state_manager(void *);
1da177e4 984
e005e804 985static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
433fbe4c
TM
986{
987 smp_mb__before_clear_bit();
e005e804 988 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
433fbe4c 989 smp_mb__after_clear_bit();
e005e804 990 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
433fbe4c
TM
991 rpc_wake_up(&clp->cl_rpcwaitq);
992}
993
1da177e4 994/*
e005e804 995 * Schedule the nfs_client asynchronous state management routine
1da177e4 996 */
b0d3ded1 997void nfs4_schedule_state_manager(struct nfs_client *clp)
1da177e4 998{
5043e900 999 struct task_struct *task;
1da177e4 1000
e005e804
TM
1001 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1002 return;
5043e900
TM
1003 __module_get(THIS_MODULE);
1004 atomic_inc(&clp->cl_count);
e005e804 1005 task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
5d8515ca
CL
1006 rpc_peeraddr2str(clp->cl_rpcclient,
1007 RPC_DISPLAY_ADDR));
5043e900
TM
1008 if (!IS_ERR(task))
1009 return;
e005e804 1010 nfs4_clear_state_manager_bit(clp);
24c8dbbb 1011 nfs_put_client(clp);
5043e900 1012 module_put(THIS_MODULE);
1da177e4
LT
1013}
1014
1015/*
0400a6b0 1016 * Schedule a lease recovery attempt
1da177e4 1017 */
0400a6b0 1018void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1da177e4
LT
1019{
1020 if (!clp)
1021 return;
e598d843
TM
1022 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1023 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
e005e804 1024 nfs4_schedule_state_manager(clp);
1da177e4
LT
1025}
1026
f9feab1e 1027static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
b79a4a1b
TM
1028{
1029
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;
1035 }
7eff03ae 1036 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
b79a4a1b
TM
1037 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1038 return 1;
1039}
1040
f9feab1e 1041static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
b79a4a1b
TM
1042{
1043 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1044 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
7eff03ae 1045 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
b79a4a1b
TM
1046 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1047 return 1;
1048}
1049
0400a6b0
TM
1050void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1051{
1052 struct nfs_client *clp = server->nfs_client;
1053
1054 nfs4_state_mark_reclaim_nograce(clp, state);
1055 nfs4_schedule_state_manager(clp);
1056}
1057
02860014 1058static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1da177e4
LT
1059{
1060 struct inode *inode = state->inode;
19e03c57 1061 struct nfs_inode *nfsi = NFS_I(inode);
1da177e4
LT
1062 struct file_lock *fl;
1063 int status = 0;
1064
3f09df70
TM
1065 if (inode->i_flock == NULL)
1066 return 0;
1067
1068 /* Guard against delegation returns and new lock/unlock calls */
19e03c57 1069 down_write(&nfsi->rwsem);
3f09df70 1070 /* Protect inode->i_flock using the BKL */
b89f4321 1071 lock_flocks();
90dc7d27 1072 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
43b2a33a 1073 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1da177e4 1074 continue;
cd3758e3 1075 if (nfs_file_open_context(fl->fl_file)->state != state)
1da177e4 1076 continue;
b89f4321 1077 unlock_flocks();
1da177e4 1078 status = ops->recover_lock(state, fl);
1da177e4 1079 switch (status) {
965b5d67
TM
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:
9c4c761a
TM
1089 case -NFS4ERR_BADSESSION:
1090 case -NFS4ERR_BADSLOT:
1091 case -NFS4ERR_BAD_HIGH_SLOT:
1092 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
965b5d67 1093 goto out;
1da177e4
LT
1094 default:
1095 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
3110ff80 1096 __func__, status);
965b5d67
TM
1097 case -ENOMEM:
1098 case -NFS4ERR_DENIED:
1da177e4
LT
1099 case -NFS4ERR_RECLAIM_BAD:
1100 case -NFS4ERR_RECLAIM_CONFLICT:
43b2a33a 1101 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
965b5d67 1102 status = 0;
1da177e4 1103 }
b89f4321 1104 lock_flocks();
1da177e4 1105 }
b89f4321 1106 unlock_flocks();
965b5d67 1107out:
19e03c57 1108 up_write(&nfsi->rwsem);
1da177e4
LT
1109 return status;
1110}
1111
02860014 1112static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1da177e4
LT
1113{
1114 struct nfs4_state *state;
1115 struct nfs4_lock_state *lock;
1116 int status = 0;
1117
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.
1125 */
fe1d8195
TM
1126restart:
1127 spin_lock(&sp->so_lock);
1da177e4 1128 list_for_each_entry(state, &sp->so_states, open_states) {
b79a4a1b
TM
1129 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1130 continue;
1da177e4
LT
1131 if (state->state == 0)
1132 continue;
fe1d8195
TM
1133 atomic_inc(&state->count);
1134 spin_unlock(&sp->so_lock);
1da177e4 1135 status = ops->recover_open(sp, state);
1da177e4 1136 if (status >= 0) {
02860014
TM
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",
3110ff80 1142 __func__);
02860014 1143 }
fe1d8195
TM
1144 nfs4_put_open_state(state);
1145 goto restart;
1da177e4 1146 }
1da177e4
LT
1147 }
1148 switch (status) {
1149 default:
1150 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
3110ff80 1151 __func__, status);
1da177e4 1152 case -ENOENT:
965b5d67 1153 case -ENOMEM:
b79a4a1b 1154 case -ESTALE:
1da177e4
LT
1155 /*
1156 * Open state on this file cannot be recovered
1157 * All we can do is revert to using the zero stateid.
1158 */
1159 memset(state->stateid.data, 0,
1160 sizeof(state->stateid.data));
1161 /* Mark the file as being 'closed' */
1162 state->state = 0;
1163 break;
168667c4
TM
1164 case -EKEYEXPIRED:
1165 /*
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.
1170 */
1171 break;
965b5d67
TM
1172 case -NFS4ERR_ADMIN_REVOKED:
1173 case -NFS4ERR_STALE_STATEID:
1174 case -NFS4ERR_BAD_STATEID:
b79a4a1b
TM
1175 case -NFS4ERR_RECLAIM_BAD:
1176 case -NFS4ERR_RECLAIM_CONFLICT:
1f0e890d 1177 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
b79a4a1b 1178 break;
1da177e4
LT
1179 case -NFS4ERR_EXPIRED:
1180 case -NFS4ERR_NO_GRACE:
1f0e890d 1181 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1da177e4 1182 case -NFS4ERR_STALE_CLIENTID:
9c4c761a
TM
1183 case -NFS4ERR_BADSESSION:
1184 case -NFS4ERR_BADSLOT:
1185 case -NFS4ERR_BAD_HIGH_SLOT:
1186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1da177e4
LT
1187 goto out_err;
1188 }
fe1d8195
TM
1189 nfs4_put_open_state(state);
1190 goto restart;
1da177e4 1191 }
fe1d8195 1192 spin_unlock(&sp->so_lock);
1da177e4
LT
1193 return 0;
1194out_err:
fe1d8195 1195 nfs4_put_open_state(state);
1da177e4
LT
1196 return status;
1197}
1198
b79a4a1b
TM
1199static void nfs4_clear_open_state(struct nfs4_state *state)
1200{
1201 struct nfs4_lock_state *lock;
1202
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) {
b79a4a1b
TM
1208 lock->ls_seqid.flags = 0;
1209 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1210 }
1211}
1212
24d292b8
CL
1213static void nfs4_reset_seqids(struct nfs_server *server,
1214 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
cee54fc9 1215{
24d292b8 1216 struct nfs_client *clp = server->nfs_client;
cee54fc9 1217 struct nfs4_state_owner *sp;
9f958ab8 1218 struct rb_node *pos;
cee54fc9 1219 struct nfs4_state *state;
cee54fc9 1220
24d292b8
CL
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);
cee54fc9 1226 sp->so_seqid.flags = 0;
ec073428 1227 spin_lock(&sp->so_lock);
cee54fc9 1228 list_for_each_entry(state, &sp->so_states, open_states) {
b79a4a1b
TM
1229 if (mark_reclaim(clp, state))
1230 nfs4_clear_open_state(state);
cee54fc9 1231 }
ec073428 1232 spin_unlock(&sp->so_lock);
cee54fc9 1233 }
24d292b8
CL
1234 spin_unlock(&clp->cl_lock);
1235}
1236
1237static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1238 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1239{
1240 struct nfs_server *server;
1241
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();
cee54fc9
TM
1246}
1247
b79a4a1b
TM
1248static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1249{
1250 /* Mark all delegations for reclaim */
1251 nfs_delegation_mark_reclaim(clp);
1252 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1253}
1254
fce5c838
RL
1255static void nfs4_reclaim_complete(struct nfs_client *clp,
1256 const struct nfs4_state_recovery_ops *ops)
1257{
1258 /* Notify the server we're done reclaiming our state */
1259 if (ops->reclaim_complete)
1260 (void)ops->reclaim_complete(clp);
1261}
1262
24d292b8 1263static void nfs4_clear_reclaim_server(struct nfs_server *server)
b79a4a1b 1264{
24d292b8 1265 struct nfs_client *clp = server->nfs_client;
b79a4a1b
TM
1266 struct nfs4_state_owner *sp;
1267 struct rb_node *pos;
1268 struct nfs4_state *state;
1269
24d292b8
CL
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);
b79a4a1b
TM
1275 spin_lock(&sp->so_lock);
1276 list_for_each_entry(state, &sp->so_states, open_states) {
24d292b8
CL
1277 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1278 &state->flags))
b79a4a1b
TM
1279 continue;
1280 nfs4_state_mark_reclaim_nograce(clp, state);
1281 }
1282 spin_unlock(&sp->so_lock);
1283 }
24d292b8
CL
1284 spin_unlock(&clp->cl_lock);
1285}
1286
1287static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1288{
1289 struct nfs_server *server;
1290
1291 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1292 return 0;
1293
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();
b79a4a1b
TM
1298
1299 nfs_delegation_reap_unclaimed(clp);
6eaa6149
TM
1300 return 1;
1301}
1302
1303static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1304{
1305 if (!nfs4_state_clear_reclaim_reboot(clp))
1306 return;
1307 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
b79a4a1b
TM
1308}
1309
1310static void nfs_delegation_clear_all(struct nfs_client *clp)
1311{
1312 nfs_delegation_mark_reclaim(clp);
1313 nfs_delegation_reap_unclaimed(clp);
1314}
1315
1316static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1317{
1318 nfs_delegation_clear_all(clp);
1319 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1320}
1321
168667c4
TM
1322static void nfs4_warn_keyexpired(const char *s)
1323{
1324 printk_ratelimited(KERN_WARNING "Error: state manager"
1325 " encountered RPCSEC_GSS session"
1326 " expired against NFSv4 server %s.\n",
1327 s);
1328}
1329
4f7cdf18 1330static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
e598d843
TM
1331{
1332 switch (error) {
1333 case -NFS4ERR_CB_PATH_DOWN:
707fb4b3 1334 nfs_handle_cb_pathdown(clp);
4f7cdf18 1335 return 0;
c8b7ae3d
TM
1336 case -NFS4ERR_NO_GRACE:
1337 nfs4_state_end_reclaim_reboot(clp);
1338 return 0;
e598d843
TM
1339 case -NFS4ERR_STALE_CLIENTID:
1340 case -NFS4ERR_LEASE_MOVED:
1341 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
6eaa6149 1342 nfs4_state_clear_reclaim_reboot(clp);
e598d843
TM
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);
8ba9bf8e 1348 break;
c3fad1b1
AA
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:
6df08189 1356 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
0b9e2d41
AA
1357 /* Zero session reset errors */
1358 return 0;
168667c4
TM
1359 case -EKEYEXPIRED:
1360 /* Nothing we can do */
1361 nfs4_warn_keyexpired(clp->cl_hostname);
1362 return 0;
e598d843 1363 }
4f7cdf18 1364 return error;
e598d843
TM
1365}
1366
02860014 1367static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1da177e4 1368{
24d292b8
CL
1369 struct nfs4_state_owner *sp;
1370 struct nfs_server *server;
9f958ab8 1371 struct rb_node *pos;
1da177e4
LT
1372 int status = 0;
1373
7eff03ae 1374restart:
24d292b8
CL
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();
1389
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);
1395 }
1396
7eff03ae 1397 nfs4_put_state_owner(sp);
24d292b8 1398 goto restart;
7eff03ae 1399 }
24d292b8 1400 spin_unlock(&clp->cl_lock);
02860014 1401 }
24d292b8 1402 rcu_read_unlock();
02860014
TM
1403 return status;
1404}
1405
1406static int nfs4_check_lease(struct nfs_client *clp)
1407{
1408 struct rpc_cred *cred;
c48f4f35
TM
1409 const struct nfs4_state_maintenance_ops *ops =
1410 clp->cl_mvops->state_renewal_ops;
02860014 1411 int status = -NFS4ERR_EXPIRED;
1da177e4 1412
0f605b56
TM
1413 /* Is the client already known to have an expired lease? */
1414 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1415 return 0;
a7b72103
AA
1416 spin_lock(&clp->cl_lock);
1417 cred = ops->get_state_renewal_cred_locked(clp);
1418 spin_unlock(&clp->cl_lock);
0f605b56
TM
1419 if (cred == NULL) {
1420 cred = nfs4_get_setclientid_cred(clp);
1421 if (cred == NULL)
1422 goto out;
286d7d6a 1423 }
8e69514f 1424 status = ops->renew_lease(clp, cred);
0f605b56
TM
1425 put_rpccred(cred);
1426out:
4f7cdf18 1427 return nfs4_recovery_handle_error(clp, status);
02860014
TM
1428}
1429
1430static int nfs4_reclaim_lease(struct nfs_client *clp)
1431{
1432 struct rpc_cred *cred;
c48f4f35
TM
1433 const struct nfs4_state_recovery_ops *ops =
1434 clp->cl_mvops->reboot_recovery_ops;
02860014
TM
1435 int status = -ENOENT;
1436
90a16617 1437 cred = ops->get_clid_cred(clp);
286d7d6a 1438 if (cred != NULL) {
591d71cb 1439 status = ops->establish_clid(clp, cred);
286d7d6a 1440 put_rpccred(cred);
a2b2bb88
TM
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);
02860014 1444 status = -EAGAIN;
a2b2bb88 1445 }
c2e713dd
BH
1446 if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1447 status = -EPROTONOSUPPORT;
286d7d6a 1448 }
02860014
TM
1449 return status;
1450}
1451
76db6d95 1452#ifdef CONFIG_NFS_V4_1
0400a6b0
TM
1453void nfs4_schedule_session_recovery(struct nfs4_session *session)
1454{
1455 nfs4_schedule_lease_recovery(session->clp);
1456}
cbdabc7f 1457EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
0400a6b0 1458
b9efa1b2
AA
1459void nfs41_handle_recall_slot(struct nfs_client *clp)
1460{
1461 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
0400a6b0 1462 nfs4_schedule_state_manager(clp);
b9efa1b2
AA
1463}
1464
0f79fd6f
TM
1465static void nfs4_reset_all_state(struct nfs_client *clp)
1466{
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);
0400a6b0 1470 nfs4_schedule_state_manager(clp);
0f79fd6f
TM
1471 }
1472}
1473
1474static void nfs41_handle_server_reboot(struct nfs_client *clp)
1475{
1476 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1477 nfs4_state_start_reclaim_reboot(clp);
0400a6b0 1478 nfs4_schedule_state_manager(clp);
0f79fd6f
TM
1479 }
1480}
1481
1482static void nfs41_handle_state_revoked(struct nfs_client *clp)
1483{
1484 /* Temporary */
1485 nfs4_reset_all_state(clp);
1486}
1487
1488static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1489{
1490 /* This will need to handle layouts too */
1491 nfs_expire_all_delegations(clp);
1492}
1493
1494static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1495{
1496 nfs_expire_all_delegations(clp);
1497 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
0400a6b0 1498 nfs4_schedule_state_manager(clp);
0f79fd6f
TM
1499}
1500
0629e370
AB
1501void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1502{
1503 if (!flags)
1504 return;
0f79fd6f
TM
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 |
0629e370
AB
1508 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1509 SEQ4_STATUS_ADMIN_STATE_REVOKED |
0f79fd6f
TM
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 |
0629e370
AB
1515 SEQ4_STATUS_BACKCHANNEL_FAULT |
1516 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
0f79fd6f 1517 nfs41_handle_cb_path_down(clp);
0629e370
AB
1518}
1519
c3fad1b1
AA
1520static int nfs4_reset_session(struct nfs_client *clp)
1521{
1522 int status;
1523
38045412 1524 nfs4_begin_drain_session(clp);
c3fad1b1
AA
1525 status = nfs4_proc_destroy_session(clp->cl_session);
1526 if (status && status != -NFS4ERR_BADSESSION &&
1527 status != -NFS4ERR_DEADSESSION) {
f455848a 1528 status = nfs4_recovery_handle_error(clp, status);
c3fad1b1
AA
1529 goto out;
1530 }
1531
1532 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
f26468fb 1533 status = nfs4_proc_create_session(clp);
41f54a55 1534 if (status) {
f455848a 1535 status = nfs4_recovery_handle_error(clp, status);
41f54a55
AA
1536 goto out;
1537 }
1538 /* create_session negotiated new slot table */
1539 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
9dfdf404 1540
41f54a55
AA
1541 /* Let the state manager reestablish state */
1542 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
5601a00d 1543 nfs41_setup_state_renewal(clp);
41f54a55 1544out:
c3fad1b1
AA
1545 return status;
1546}
76db6d95 1547
b9efa1b2
AA
1548static int nfs4_recall_slot(struct nfs_client *clp)
1549{
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;
1554
1555 nfs4_begin_drain_session(clp);
1556 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
8535b2be 1557 GFP_NOFS);
b9efa1b2
AA
1558 if (!new)
1559 return -ENOMEM;
1560
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);
1570
1571 kfree(old);
1572 nfs4_end_drain_session(clp);
1573 return 0;
1574}
1575
76db6d95 1576#else /* CONFIG_NFS_V4_1 */
c3fad1b1 1577static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
5601a00d 1578static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
b9efa1b2 1579static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
76db6d95
AA
1580#endif /* CONFIG_NFS_V4_1 */
1581
78722e9c
AA
1582/* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1583 * on EXCHANGE_ID for v4.1
1584 */
1585static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1586{
1587 if (nfs4_has_session(clp)) {
1588 switch (status) {
1589 case -NFS4ERR_DELAY:
1590 case -NFS4ERR_CLID_INUSE:
1591 case -EAGAIN:
1592 break;
1593
168667c4
TM
1594 case -EKEYEXPIRED:
1595 nfs4_warn_keyexpired(clp->cl_hostname);
78722e9c
AA
1596 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1597 * in nfs4_exchange_id */
1598 default:
1599 return;
1600 }
1601 }
1602 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1603}
1604
e005e804 1605static void nfs4_state_manager(struct nfs_client *clp)
02860014 1606{
02860014
TM
1607 int status = 0;
1608
02860014 1609 /* Ensure exclusive access to NFSv4 state */
f3c76491 1610 for(;;) {
b79a4a1b
TM
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) {
78722e9c 1615 nfs4_set_lease_expired(clp, status);
b6d408ba
TM
1616 if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1617 &clp->cl_state))
b79a4a1b 1618 continue;
76db6d95
AA
1619 if (clp->cl_cons_state ==
1620 NFS_CS_SESSION_INITING)
1621 nfs_mark_client_ready(clp, status);
b79a4a1b
TM
1622 goto out_error;
1623 }
e598d843 1624 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
7cab89b2 1625 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
974cec8c 1626 pnfs_destroy_all_layouts(clp);
e598d843
TM
1627 }
1628
1629 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1630 status = nfs4_check_lease(clp);
b6d408ba 1631 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
e598d843 1632 continue;
b6d408ba
TM
1633 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1634 goto out_error;
b79a4a1b 1635 }
b6d408ba 1636
c3fad1b1 1637 /* Initialize or reset the session */
6df08189 1638 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
7111dc73 1639 && nfs4_has_session(clp)) {
4d643d1d 1640 status = nfs4_reset_session(clp);
b6d408ba
TM
1641 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1642 continue;
1643 if (status < 0)
76db6d95 1644 goto out_error;
76db6d95 1645 }
b6d408ba 1646
b79a4a1b 1647 /* First recover reboot state... */
e345e88a 1648 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
591d71cb 1649 status = nfs4_do_reclaim(clp,
c48f4f35 1650 clp->cl_mvops->reboot_recovery_ops);
b6d408ba 1651 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
6df08189 1652 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
c3fad1b1 1653 continue;
b79a4a1b 1654 nfs4_state_end_reclaim_reboot(clp);
b6d408ba
TM
1655 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1656 continue;
1657 if (status < 0)
1658 goto out_error;
02860014
TM
1659 }
1660
b79a4a1b
TM
1661 /* Now recover expired state... */
1662 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
591d71cb 1663 status = nfs4_do_reclaim(clp,
c48f4f35 1664 clp->cl_mvops->nograce_recovery_ops);
b6d408ba 1665 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
6df08189 1666 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
b6d408ba
TM
1667 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1668 continue;
1669 if (status < 0)
b79a4a1b 1670 goto out_error;
1da177e4 1671 }
707fb4b3 1672
5601a00d 1673 nfs4_end_drain_session(clp);
707fb4b3
TM
1674 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1675 nfs_client_return_marked_delegations(clp);
1676 continue;
1677 }
b9efa1b2
AA
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;
1685 }
1686
e005e804
TM
1687
1688 nfs4_clear_state_manager_bit(clp);
f3c76491
TM
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;
1da177e4 1694 }
e005e804 1695 return;
1da177e4 1696out_error:
e005e804 1697 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
5d8515ca 1698 " with error %d\n", clp->cl_hostname, -status);
5601a00d 1699 nfs4_end_drain_session(clp);
e005e804
TM
1700 nfs4_clear_state_manager_bit(clp);
1701}
1702
1703static int nfs4_run_state_manager(void *ptr)
1704{
1705 struct nfs_client *clp = ptr;
1706
1707 allow_signal(SIGKILL);
1708 nfs4_state_manager(clp);
1709 nfs_put_client(clp);
1710 module_put_and_exit(0);
1711 return 0;
1da177e4
LT
1712}
1713
1714/*
1715 * Local variables:
1716 * c-basic-offset: 8
1717 * End:
1718 */