]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/nfs/nfs4state.c
NFSv4.1: filelayout read
[mirror_ubuntu-jammy-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);
593 state->inode = igrab(inode);
594 spin_unlock(&inode->i_lock);
ec073428
TM
595 /* Note: The reclaim code dictates that we add stateless
596 * and read-only stateids to the end of the list */
597 list_add_tail(&state->open_states, &owner->so_states);
598 spin_unlock(&owner->so_lock);
1da177e4
LT
599 } else {
600 spin_unlock(&inode->i_lock);
ec073428 601 spin_unlock(&owner->so_lock);
1da177e4
LT
602 if (new)
603 nfs4_free_open_state(new);
604 }
605out:
606 return state;
607}
608
1da177e4
LT
609void nfs4_put_open_state(struct nfs4_state *state)
610{
611 struct inode *inode = state->inode;
612 struct nfs4_state_owner *owner = state->owner;
613
ec073428 614 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
1da177e4 615 return;
ec073428 616 spin_lock(&inode->i_lock);
ba683031 617 list_del(&state->inode_states);
1da177e4 618 list_del(&state->open_states);
ec073428
TM
619 spin_unlock(&inode->i_lock);
620 spin_unlock(&owner->so_lock);
1da177e4 621 iput(inode);
1da177e4
LT
622 nfs4_free_open_state(state);
623 nfs4_put_state_owner(owner);
624}
625
626/*
83c9d41e 627 * Close the current file.
1da177e4 628 */
8535b2be
TM
629static void __nfs4_close(struct path *path, struct nfs4_state *state,
630 fmode_t fmode, gfp_t gfp_mask, int wait)
1da177e4 631{
1da177e4 632 struct nfs4_state_owner *owner = state->owner;
003707c7 633 int call_close = 0;
dc0b027d 634 fmode_t newstate;
1da177e4
LT
635
636 atomic_inc(&owner->so_count);
1da177e4 637 /* Protect against nfs4_find_state() */
ec073428 638 spin_lock(&owner->so_lock);
dc0b027d 639 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
e7616923
TM
640 case FMODE_READ:
641 state->n_rdonly--;
642 break;
643 case FMODE_WRITE:
644 state->n_wronly--;
645 break;
646 case FMODE_READ|FMODE_WRITE:
647 state->n_rdwr--;
648 }
003707c7 649 newstate = FMODE_READ|FMODE_WRITE;
e7616923 650 if (state->n_rdwr == 0) {
003707c7 651 if (state->n_rdonly == 0) {
e7616923 652 newstate &= ~FMODE_READ;
003707c7
TM
653 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
654 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
655 }
656 if (state->n_wronly == 0) {
e7616923 657 newstate &= ~FMODE_WRITE;
003707c7
TM
658 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
659 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
660 }
661 if (newstate == 0)
662 clear_bit(NFS_DELEGATED_STATE, &state->flags);
e7616923 663 }
003707c7 664 nfs4_state_set_mode_locked(state, newstate);
ec073428 665 spin_unlock(&owner->so_lock);
4cecb76f 666
003707c7 667 if (!call_close) {
b39e625b
TM
668 nfs4_put_open_state(state);
669 nfs4_put_state_owner(owner);
f7e8917a
FI
670 } else {
671 bool roc = pnfs_roc(state->inode);
672
673 nfs4_do_close(path, state, gfp_mask, wait, roc);
674 }
a49c3c77
TM
675}
676
dc0b027d 677void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
a49c3c77 678{
8535b2be 679 __nfs4_close(path, state, fmode, GFP_NOFS, 0);
a49c3c77
TM
680}
681
dc0b027d 682void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
a49c3c77 683{
8535b2be 684 __nfs4_close(path, state, fmode, GFP_KERNEL, 1);
1da177e4
LT
685}
686
687/*
688 * Search the state->lock_states for an existing lock_owner
689 * that is compatible with current->files
690 */
691static struct nfs4_lock_state *
77041ed9 692__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
1da177e4
LT
693{
694 struct nfs4_lock_state *pos;
695 list_for_each_entry(pos, &state->lock_states, ls_locks) {
77041ed9 696 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
1da177e4 697 continue;
77041ed9
TM
698 switch (pos->ls_owner.lo_type) {
699 case NFS4_POSIX_LOCK_TYPE:
700 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
701 continue;
702 break;
703 case NFS4_FLOCK_LOCK_TYPE:
704 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
705 continue;
706 }
1da177e4
LT
707 atomic_inc(&pos->ls_count);
708 return pos;
709 }
710 return NULL;
711}
712
1da177e4
LT
713/*
714 * Return a compatible lock_state. If no initialized lock_state structure
715 * exists, return an uninitialized one.
716 *
1da177e4 717 */
77041ed9 718static 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
719{
720 struct nfs4_lock_state *lsp;
24d292b8
CL
721 struct nfs_server *server = state->owner->so_server;
722 struct nfs_client *clp = server->nfs_client;
1da177e4 723
8535b2be 724 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
1da177e4
LT
725 if (lsp == NULL)
726 return NULL;
d0dc3701
TM
727 rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
728 spin_lock_init(&lsp->ls_sequence.lock);
729 INIT_LIST_HEAD(&lsp->ls_sequence.list);
730 lsp->ls_seqid.sequence = &lsp->ls_sequence;
1da177e4 731 atomic_set(&lsp->ls_count, 1);
b64aec8d 732 lsp->ls_state = state;
77041ed9
TM
733 lsp->ls_owner.lo_type = type;
734 switch (lsp->ls_owner.lo_type) {
735 case NFS4_FLOCK_LOCK_TYPE:
736 lsp->ls_owner.lo_u.flock_owner = fl_pid;
737 break;
738 case NFS4_POSIX_LOCK_TYPE:
739 lsp->ls_owner.lo_u.posix_owner = fl_owner;
740 break;
741 default:
742 kfree(lsp);
743 return NULL;
744 }
1da177e4 745 spin_lock(&clp->cl_lock);
24d292b8 746 nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
1da177e4 747 spin_unlock(&clp->cl_lock);
8d0a8a9d 748 INIT_LIST_HEAD(&lsp->ls_locks);
1da177e4
LT
749 return lsp;
750}
751
9f958ab8
TM
752static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
753{
24d292b8
CL
754 struct nfs_server *server = lsp->ls_state->owner->so_server;
755 struct nfs_client *clp = server->nfs_client;
9f958ab8
TM
756
757 spin_lock(&clp->cl_lock);
24d292b8 758 nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
9f958ab8 759 spin_unlock(&clp->cl_lock);
f6a1cc89 760 rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
9f958ab8
TM
761 kfree(lsp);
762}
763
1da177e4
LT
764/*
765 * Return a compatible lock_state. If no initialized lock_state structure
766 * exists, return an uninitialized one.
767 *
1da177e4 768 */
77041ed9 769static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
1da177e4 770{
8d0a8a9d 771 struct nfs4_lock_state *lsp, *new = NULL;
1da177e4 772
8d0a8a9d
TM
773 for(;;) {
774 spin_lock(&state->state_lock);
77041ed9 775 lsp = __nfs4_find_lock_state(state, owner, pid, type);
8d0a8a9d
TM
776 if (lsp != NULL)
777 break;
778 if (new != NULL) {
8d0a8a9d
TM
779 list_add(&new->ls_locks, &state->lock_states);
780 set_bit(LK_STATE_IN_USE, &state->flags);
781 lsp = new;
782 new = NULL;
783 break;
784 }
785 spin_unlock(&state->state_lock);
77041ed9 786 new = nfs4_alloc_lock_state(state, owner, pid, type);
8d0a8a9d
TM
787 if (new == NULL)
788 return NULL;
789 }
790 spin_unlock(&state->state_lock);
9f958ab8
TM
791 if (new != NULL)
792 nfs4_free_lock_state(new);
1da177e4
LT
793 return lsp;
794}
795
796/*
8d0a8a9d
TM
797 * Release reference to lock_state, and free it if we see that
798 * it is no longer in use
1da177e4 799 */
faf5f49c 800void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
1da177e4 801{
8d0a8a9d 802 struct nfs4_state *state;
1da177e4 803
8d0a8a9d
TM
804 if (lsp == NULL)
805 return;
806 state = lsp->ls_state;
807 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
808 return;
809 list_del(&lsp->ls_locks);
810 if (list_empty(&state->lock_states))
811 clear_bit(LK_STATE_IN_USE, &state->flags);
812 spin_unlock(&state->state_lock);
d3c7b7cc
TM
813 if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
814 nfs4_release_lockowner(lsp);
9f958ab8 815 nfs4_free_lock_state(lsp);
1da177e4
LT
816}
817
8d0a8a9d 818static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
1da177e4 819{
8d0a8a9d 820 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
1da177e4 821
8d0a8a9d
TM
822 dst->fl_u.nfs4_fl.owner = lsp;
823 atomic_inc(&lsp->ls_count);
824}
1da177e4 825
8d0a8a9d 826static void nfs4_fl_release_lock(struct file_lock *fl)
1da177e4 827{
8d0a8a9d 828 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
1da177e4
LT
829}
830
6aed6285 831static const struct file_lock_operations nfs4_fl_lock_ops = {
8d0a8a9d
TM
832 .fl_copy_lock = nfs4_fl_copy_lock,
833 .fl_release_private = nfs4_fl_release_lock,
834};
835
836int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
1da177e4 837{
8d0a8a9d
TM
838 struct nfs4_lock_state *lsp;
839
840 if (fl->fl_ops != NULL)
841 return 0;
77041ed9
TM
842 if (fl->fl_flags & FL_POSIX)
843 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
844 else if (fl->fl_flags & FL_FLOCK)
845 lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
846 else
847 return -EINVAL;
8d0a8a9d
TM
848 if (lsp == NULL)
849 return -ENOMEM;
850 fl->fl_u.nfs4_fl.owner = lsp;
851 fl->fl_ops = &nfs4_fl_lock_ops;
852 return 0;
1da177e4
LT
853}
854
8d0a8a9d
TM
855/*
856 * Byte-range lock aware utility to initialize the stateid of read/write
857 * requests.
1da177e4 858 */
77041ed9 859void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid)
1da177e4 860{
8d0a8a9d 861 struct nfs4_lock_state *lsp;
8bda4e4c 862 int seq;
1da177e4 863
8bda4e4c
TM
864 do {
865 seq = read_seqbegin(&state->seqlock);
866 memcpy(dst, &state->stateid, sizeof(*dst));
867 } while (read_seqretry(&state->seqlock, seq));
8d0a8a9d
TM
868 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
869 return;
1da177e4 870
8d0a8a9d 871 spin_lock(&state->state_lock);
77041ed9 872 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
8d0a8a9d
TM
873 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
874 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
875 spin_unlock(&state->state_lock);
1da177e4
LT
876 nfs4_put_lock_state(lsp);
877}
878
8535b2be 879struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
cee54fc9 880{
cee54fc9
TM
881 struct nfs_seqid *new;
882
8535b2be 883 new = kmalloc(sizeof(*new), gfp_mask);
cee54fc9
TM
884 if (new != NULL) {
885 new->sequence = counter;
2f74c0a0 886 INIT_LIST_HEAD(&new->list);
cee54fc9
TM
887 }
888 return new;
889}
890
72211dbe 891void nfs_release_seqid(struct nfs_seqid *seqid)
1da177e4 892{
2f74c0a0
TM
893 if (!list_empty(&seqid->list)) {
894 struct rpc_sequence *sequence = seqid->sequence->sequence;
cee54fc9 895
2f74c0a0 896 spin_lock(&sequence->lock);
72211dbe 897 list_del_init(&seqid->list);
2f74c0a0
TM
898 spin_unlock(&sequence->lock);
899 rpc_wake_up(&sequence->wait);
900 }
72211dbe
TM
901}
902
903void nfs_free_seqid(struct nfs_seqid *seqid)
904{
905 nfs_release_seqid(seqid);
cee54fc9 906 kfree(seqid);
1da177e4
LT
907}
908
909/*
cee54fc9
TM
910 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
911 * failed with a seqid incrementing error -
912 * see comments nfs_fs.h:seqid_mutating_error()
913 */
88d90939 914static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
cee54fc9 915{
2f74c0a0 916 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
cee54fc9
TM
917 switch (status) {
918 case 0:
919 break;
920 case -NFS4ERR_BAD_SEQID:
6f43ddcc
TM
921 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
922 return;
923 printk(KERN_WARNING "NFS: v4 server returned a bad"
497799e7
DM
924 " sequence-id error on an"
925 " unconfirmed sequence %p!\n",
6f43ddcc 926 seqid->sequence);
cee54fc9
TM
927 case -NFS4ERR_STALE_CLIENTID:
928 case -NFS4ERR_STALE_STATEID:
929 case -NFS4ERR_BAD_STATEID:
930 case -NFS4ERR_BADXDR:
931 case -NFS4ERR_RESOURCE:
932 case -NFS4ERR_NOFILEHANDLE:
933 /* Non-seqid mutating errors */
934 return;
935 };
936 /*
937 * Note: no locking needed as we are guaranteed to be first
938 * on the sequence list
939 */
940 seqid->sequence->counter++;
941}
942
943void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
944{
34dc1ad7
BH
945 struct nfs4_state_owner *sp = container_of(seqid->sequence,
946 struct nfs4_state_owner, so_seqid);
947 struct nfs_server *server = sp->so_server;
948
949 if (status == -NFS4ERR_BAD_SEQID)
1da177e4 950 nfs4_drop_state_owner(sp);
34dc1ad7
BH
951 if (!nfs4_has_session(server->nfs_client))
952 nfs_increment_seqid(status, seqid);
cee54fc9
TM
953}
954
955/*
cee54fc9
TM
956 * Increment the seqid if the LOCK/LOCKU succeeded, or
957 * failed with a seqid incrementing error -
958 * see comments nfs_fs.h:seqid_mutating_error()
959 */
960void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
961{
88d90939 962 nfs_increment_seqid(status, seqid);
cee54fc9
TM
963}
964
965int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
966{
967 struct rpc_sequence *sequence = seqid->sequence->sequence;
968 int status = 0;
969
970 spin_lock(&sequence->lock);
2f74c0a0
TM
971 if (list_empty(&seqid->list))
972 list_add_tail(&seqid->list, &sequence->list);
973 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
974 goto unlock;
5d00837b 975 rpc_sleep_on(&sequence->wait, task, NULL);
2f74c0a0
TM
976 status = -EAGAIN;
977unlock:
cee54fc9
TM
978 spin_unlock(&sequence->lock);
979 return status;
1da177e4
LT
980}
981
e005e804 982static int nfs4_run_state_manager(void *);
1da177e4 983
e005e804 984static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
433fbe4c
TM
985{
986 smp_mb__before_clear_bit();
e005e804 987 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
433fbe4c 988 smp_mb__after_clear_bit();
e005e804 989 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
433fbe4c
TM
990 rpc_wake_up(&clp->cl_rpcwaitq);
991}
992
1da177e4 993/*
e005e804 994 * Schedule the nfs_client asynchronous state management routine
1da177e4 995 */
b0d3ded1 996void nfs4_schedule_state_manager(struct nfs_client *clp)
1da177e4 997{
5043e900 998 struct task_struct *task;
1da177e4 999
e005e804
TM
1000 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1001 return;
5043e900
TM
1002 __module_get(THIS_MODULE);
1003 atomic_inc(&clp->cl_count);
e005e804 1004 task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
5d8515ca
CL
1005 rpc_peeraddr2str(clp->cl_rpcclient,
1006 RPC_DISPLAY_ADDR));
5043e900
TM
1007 if (!IS_ERR(task))
1008 return;
e005e804 1009 nfs4_clear_state_manager_bit(clp);
24c8dbbb 1010 nfs_put_client(clp);
5043e900 1011 module_put(THIS_MODULE);
1da177e4
LT
1012}
1013
1014/*
0400a6b0 1015 * Schedule a lease recovery attempt
1da177e4 1016 */
0400a6b0 1017void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1da177e4
LT
1018{
1019 if (!clp)
1020 return;
e598d843
TM
1021 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1022 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
e005e804 1023 nfs4_schedule_state_manager(clp);
1da177e4
LT
1024}
1025
f9feab1e 1026static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
b79a4a1b
TM
1027{
1028
1029 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1030 /* Don't recover state that expired before the reboot */
1031 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1032 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1033 return 0;
1034 }
7eff03ae 1035 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
b79a4a1b
TM
1036 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1037 return 1;
1038}
1039
f9feab1e 1040static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
b79a4a1b
TM
1041{
1042 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1043 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
7eff03ae 1044 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
b79a4a1b
TM
1045 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1046 return 1;
1047}
1048
0400a6b0
TM
1049void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1050{
1051 struct nfs_client *clp = server->nfs_client;
1052
1053 nfs4_state_mark_reclaim_nograce(clp, state);
1054 nfs4_schedule_state_manager(clp);
1055}
1056
02860014 1057static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1da177e4
LT
1058{
1059 struct inode *inode = state->inode;
19e03c57 1060 struct nfs_inode *nfsi = NFS_I(inode);
1da177e4
LT
1061 struct file_lock *fl;
1062 int status = 0;
1063
3f09df70
TM
1064 if (inode->i_flock == NULL)
1065 return 0;
1066
1067 /* Guard against delegation returns and new lock/unlock calls */
19e03c57 1068 down_write(&nfsi->rwsem);
3f09df70 1069 /* Protect inode->i_flock using the BKL */
b89f4321 1070 lock_flocks();
90dc7d27 1071 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
43b2a33a 1072 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1da177e4 1073 continue;
cd3758e3 1074 if (nfs_file_open_context(fl->fl_file)->state != state)
1da177e4 1075 continue;
b89f4321 1076 unlock_flocks();
1da177e4 1077 status = ops->recover_lock(state, fl);
1da177e4 1078 switch (status) {
965b5d67
TM
1079 case 0:
1080 break;
1081 case -ESTALE:
1082 case -NFS4ERR_ADMIN_REVOKED:
1083 case -NFS4ERR_STALE_STATEID:
1084 case -NFS4ERR_BAD_STATEID:
1085 case -NFS4ERR_EXPIRED:
1086 case -NFS4ERR_NO_GRACE:
1087 case -NFS4ERR_STALE_CLIENTID:
9c4c761a
TM
1088 case -NFS4ERR_BADSESSION:
1089 case -NFS4ERR_BADSLOT:
1090 case -NFS4ERR_BAD_HIGH_SLOT:
1091 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
965b5d67 1092 goto out;
1da177e4
LT
1093 default:
1094 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
3110ff80 1095 __func__, status);
965b5d67
TM
1096 case -ENOMEM:
1097 case -NFS4ERR_DENIED:
1da177e4
LT
1098 case -NFS4ERR_RECLAIM_BAD:
1099 case -NFS4ERR_RECLAIM_CONFLICT:
43b2a33a 1100 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
965b5d67 1101 status = 0;
1da177e4 1102 }
b89f4321 1103 lock_flocks();
1da177e4 1104 }
b89f4321 1105 unlock_flocks();
965b5d67 1106out:
19e03c57 1107 up_write(&nfsi->rwsem);
1da177e4
LT
1108 return status;
1109}
1110
02860014 1111static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1da177e4
LT
1112{
1113 struct nfs4_state *state;
1114 struct nfs4_lock_state *lock;
1115 int status = 0;
1116
1117 /* Note: we rely on the sp->so_states list being ordered
1118 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1119 * states first.
1120 * This is needed to ensure that the server won't give us any
1121 * read delegations that we have to return if, say, we are
1122 * recovering after a network partition or a reboot from a
1123 * server that doesn't support a grace period.
1124 */
fe1d8195
TM
1125restart:
1126 spin_lock(&sp->so_lock);
1da177e4 1127 list_for_each_entry(state, &sp->so_states, open_states) {
b79a4a1b
TM
1128 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1129 continue;
1da177e4
LT
1130 if (state->state == 0)
1131 continue;
fe1d8195
TM
1132 atomic_inc(&state->count);
1133 spin_unlock(&sp->so_lock);
1da177e4 1134 status = ops->recover_open(sp, state);
1da177e4 1135 if (status >= 0) {
02860014
TM
1136 status = nfs4_reclaim_locks(state, ops);
1137 if (status >= 0) {
1138 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1139 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
1140 printk("%s: Lock reclaim failed!\n",
3110ff80 1141 __func__);
02860014 1142 }
fe1d8195
TM
1143 nfs4_put_open_state(state);
1144 goto restart;
1da177e4 1145 }
1da177e4
LT
1146 }
1147 switch (status) {
1148 default:
1149 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
3110ff80 1150 __func__, status);
1da177e4 1151 case -ENOENT:
965b5d67 1152 case -ENOMEM:
b79a4a1b 1153 case -ESTALE:
1da177e4
LT
1154 /*
1155 * Open state on this file cannot be recovered
1156 * All we can do is revert to using the zero stateid.
1157 */
1158 memset(state->stateid.data, 0,
1159 sizeof(state->stateid.data));
1160 /* Mark the file as being 'closed' */
1161 state->state = 0;
1162 break;
168667c4
TM
1163 case -EKEYEXPIRED:
1164 /*
1165 * User RPCSEC_GSS context has expired.
1166 * We cannot recover this stateid now, so
1167 * skip it and allow recovery thread to
1168 * proceed.
1169 */
1170 break;
965b5d67
TM
1171 case -NFS4ERR_ADMIN_REVOKED:
1172 case -NFS4ERR_STALE_STATEID:
1173 case -NFS4ERR_BAD_STATEID:
b79a4a1b
TM
1174 case -NFS4ERR_RECLAIM_BAD:
1175 case -NFS4ERR_RECLAIM_CONFLICT:
1f0e890d 1176 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
b79a4a1b 1177 break;
1da177e4
LT
1178 case -NFS4ERR_EXPIRED:
1179 case -NFS4ERR_NO_GRACE:
1f0e890d 1180 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1da177e4 1181 case -NFS4ERR_STALE_CLIENTID:
9c4c761a
TM
1182 case -NFS4ERR_BADSESSION:
1183 case -NFS4ERR_BADSLOT:
1184 case -NFS4ERR_BAD_HIGH_SLOT:
1185 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1da177e4
LT
1186 goto out_err;
1187 }
fe1d8195
TM
1188 nfs4_put_open_state(state);
1189 goto restart;
1da177e4 1190 }
fe1d8195 1191 spin_unlock(&sp->so_lock);
1da177e4
LT
1192 return 0;
1193out_err:
fe1d8195 1194 nfs4_put_open_state(state);
1da177e4
LT
1195 return status;
1196}
1197
b79a4a1b
TM
1198static void nfs4_clear_open_state(struct nfs4_state *state)
1199{
1200 struct nfs4_lock_state *lock;
1201
1202 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1203 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1204 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1205 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1206 list_for_each_entry(lock, &state->lock_states, ls_locks) {
b79a4a1b
TM
1207 lock->ls_seqid.flags = 0;
1208 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1209 }
1210}
1211
24d292b8
CL
1212static void nfs4_reset_seqids(struct nfs_server *server,
1213 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
cee54fc9 1214{
24d292b8 1215 struct nfs_client *clp = server->nfs_client;
cee54fc9 1216 struct nfs4_state_owner *sp;
9f958ab8 1217 struct rb_node *pos;
cee54fc9 1218 struct nfs4_state *state;
cee54fc9 1219
24d292b8
CL
1220 spin_lock(&clp->cl_lock);
1221 for (pos = rb_first(&server->state_owners);
1222 pos != NULL;
1223 pos = rb_next(pos)) {
1224 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
cee54fc9 1225 sp->so_seqid.flags = 0;
ec073428 1226 spin_lock(&sp->so_lock);
cee54fc9 1227 list_for_each_entry(state, &sp->so_states, open_states) {
b79a4a1b
TM
1228 if (mark_reclaim(clp, state))
1229 nfs4_clear_open_state(state);
cee54fc9 1230 }
ec073428 1231 spin_unlock(&sp->so_lock);
cee54fc9 1232 }
24d292b8
CL
1233 spin_unlock(&clp->cl_lock);
1234}
1235
1236static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1237 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1238{
1239 struct nfs_server *server;
1240
1241 rcu_read_lock();
1242 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1243 nfs4_reset_seqids(server, mark_reclaim);
1244 rcu_read_unlock();
cee54fc9
TM
1245}
1246
b79a4a1b
TM
1247static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1248{
1249 /* Mark all delegations for reclaim */
1250 nfs_delegation_mark_reclaim(clp);
1251 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1252}
1253
fce5c838
RL
1254static void nfs4_reclaim_complete(struct nfs_client *clp,
1255 const struct nfs4_state_recovery_ops *ops)
1256{
1257 /* Notify the server we're done reclaiming our state */
1258 if (ops->reclaim_complete)
1259 (void)ops->reclaim_complete(clp);
1260}
1261
24d292b8 1262static void nfs4_clear_reclaim_server(struct nfs_server *server)
b79a4a1b 1263{
24d292b8 1264 struct nfs_client *clp = server->nfs_client;
b79a4a1b
TM
1265 struct nfs4_state_owner *sp;
1266 struct rb_node *pos;
1267 struct nfs4_state *state;
1268
24d292b8
CL
1269 spin_lock(&clp->cl_lock);
1270 for (pos = rb_first(&server->state_owners);
1271 pos != NULL;
1272 pos = rb_next(pos)) {
1273 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
b79a4a1b
TM
1274 spin_lock(&sp->so_lock);
1275 list_for_each_entry(state, &sp->so_states, open_states) {
24d292b8
CL
1276 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1277 &state->flags))
b79a4a1b
TM
1278 continue;
1279 nfs4_state_mark_reclaim_nograce(clp, state);
1280 }
1281 spin_unlock(&sp->so_lock);
1282 }
24d292b8
CL
1283 spin_unlock(&clp->cl_lock);
1284}
1285
1286static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1287{
1288 struct nfs_server *server;
1289
1290 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1291 return 0;
1292
1293 rcu_read_lock();
1294 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1295 nfs4_clear_reclaim_server(server);
1296 rcu_read_unlock();
b79a4a1b
TM
1297
1298 nfs_delegation_reap_unclaimed(clp);
6eaa6149
TM
1299 return 1;
1300}
1301
1302static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1303{
1304 if (!nfs4_state_clear_reclaim_reboot(clp))
1305 return;
1306 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
b79a4a1b
TM
1307}
1308
1309static void nfs_delegation_clear_all(struct nfs_client *clp)
1310{
1311 nfs_delegation_mark_reclaim(clp);
1312 nfs_delegation_reap_unclaimed(clp);
1313}
1314
1315static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1316{
1317 nfs_delegation_clear_all(clp);
1318 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1319}
1320
168667c4
TM
1321static void nfs4_warn_keyexpired(const char *s)
1322{
1323 printk_ratelimited(KERN_WARNING "Error: state manager"
1324 " encountered RPCSEC_GSS session"
1325 " expired against NFSv4 server %s.\n",
1326 s);
1327}
1328
4f7cdf18 1329static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
e598d843
TM
1330{
1331 switch (error) {
1332 case -NFS4ERR_CB_PATH_DOWN:
707fb4b3 1333 nfs_handle_cb_pathdown(clp);
4f7cdf18 1334 return 0;
c8b7ae3d
TM
1335 case -NFS4ERR_NO_GRACE:
1336 nfs4_state_end_reclaim_reboot(clp);
1337 return 0;
e598d843
TM
1338 case -NFS4ERR_STALE_CLIENTID:
1339 case -NFS4ERR_LEASE_MOVED:
1340 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
6eaa6149 1341 nfs4_state_clear_reclaim_reboot(clp);
e598d843
TM
1342 nfs4_state_start_reclaim_reboot(clp);
1343 break;
1344 case -NFS4ERR_EXPIRED:
1345 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1346 nfs4_state_start_reclaim_nograce(clp);
8ba9bf8e 1347 break;
c3fad1b1
AA
1348 case -NFS4ERR_BADSESSION:
1349 case -NFS4ERR_BADSLOT:
1350 case -NFS4ERR_BAD_HIGH_SLOT:
1351 case -NFS4ERR_DEADSESSION:
1352 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1353 case -NFS4ERR_SEQ_FALSE_RETRY:
1354 case -NFS4ERR_SEQ_MISORDERED:
6df08189 1355 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
0b9e2d41
AA
1356 /* Zero session reset errors */
1357 return 0;
168667c4
TM
1358 case -EKEYEXPIRED:
1359 /* Nothing we can do */
1360 nfs4_warn_keyexpired(clp->cl_hostname);
1361 return 0;
e598d843 1362 }
4f7cdf18 1363 return error;
e598d843
TM
1364}
1365
02860014 1366static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1da177e4 1367{
24d292b8
CL
1368 struct nfs4_state_owner *sp;
1369 struct nfs_server *server;
9f958ab8 1370 struct rb_node *pos;
1da177e4
LT
1371 int status = 0;
1372
7eff03ae 1373restart:
24d292b8
CL
1374 rcu_read_lock();
1375 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1376 spin_lock(&clp->cl_lock);
1377 for (pos = rb_first(&server->state_owners);
1378 pos != NULL;
1379 pos = rb_next(pos)) {
1380 sp = rb_entry(pos,
1381 struct nfs4_state_owner, so_server_node);
1382 if (!test_and_clear_bit(ops->owner_flag_bit,
1383 &sp->so_flags))
1384 continue;
1385 atomic_inc(&sp->so_count);
1386 spin_unlock(&clp->cl_lock);
1387 rcu_read_unlock();
1388
1389 status = nfs4_reclaim_open_state(sp, ops);
1390 if (status < 0) {
1391 set_bit(ops->owner_flag_bit, &sp->so_flags);
1392 nfs4_put_state_owner(sp);
1393 return nfs4_recovery_handle_error(clp, status);
1394 }
1395
7eff03ae 1396 nfs4_put_state_owner(sp);
24d292b8 1397 goto restart;
7eff03ae 1398 }
24d292b8 1399 spin_unlock(&clp->cl_lock);
02860014 1400 }
24d292b8 1401 rcu_read_unlock();
02860014
TM
1402 return status;
1403}
1404
1405static int nfs4_check_lease(struct nfs_client *clp)
1406{
1407 struct rpc_cred *cred;
c48f4f35
TM
1408 const struct nfs4_state_maintenance_ops *ops =
1409 clp->cl_mvops->state_renewal_ops;
02860014 1410 int status = -NFS4ERR_EXPIRED;
1da177e4 1411
0f605b56
TM
1412 /* Is the client already known to have an expired lease? */
1413 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1414 return 0;
a7b72103
AA
1415 spin_lock(&clp->cl_lock);
1416 cred = ops->get_state_renewal_cred_locked(clp);
1417 spin_unlock(&clp->cl_lock);
0f605b56
TM
1418 if (cred == NULL) {
1419 cred = nfs4_get_setclientid_cred(clp);
1420 if (cred == NULL)
1421 goto out;
286d7d6a 1422 }
8e69514f 1423 status = ops->renew_lease(clp, cred);
0f605b56
TM
1424 put_rpccred(cred);
1425out:
4f7cdf18 1426 return nfs4_recovery_handle_error(clp, status);
02860014
TM
1427}
1428
1429static int nfs4_reclaim_lease(struct nfs_client *clp)
1430{
1431 struct rpc_cred *cred;
c48f4f35
TM
1432 const struct nfs4_state_recovery_ops *ops =
1433 clp->cl_mvops->reboot_recovery_ops;
02860014
TM
1434 int status = -ENOENT;
1435
90a16617 1436 cred = ops->get_clid_cred(clp);
286d7d6a 1437 if (cred != NULL) {
591d71cb 1438 status = ops->establish_clid(clp, cred);
286d7d6a 1439 put_rpccred(cred);
a2b2bb88
TM
1440 /* Handle case where the user hasn't set up machine creds */
1441 if (status == -EACCES && cred == clp->cl_machine_cred) {
1442 nfs4_clear_machine_cred(clp);
02860014 1443 status = -EAGAIN;
a2b2bb88 1444 }
c2e713dd
BH
1445 if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1446 status = -EPROTONOSUPPORT;
286d7d6a 1447 }
02860014
TM
1448 return status;
1449}
1450
76db6d95 1451#ifdef CONFIG_NFS_V4_1
0400a6b0
TM
1452void nfs4_schedule_session_recovery(struct nfs4_session *session)
1453{
1454 nfs4_schedule_lease_recovery(session->clp);
1455}
1456
b9efa1b2
AA
1457void nfs41_handle_recall_slot(struct nfs_client *clp)
1458{
1459 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
0400a6b0 1460 nfs4_schedule_state_manager(clp);
b9efa1b2
AA
1461}
1462
0f79fd6f
TM
1463static void nfs4_reset_all_state(struct nfs_client *clp)
1464{
1465 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1466 clp->cl_boot_time = CURRENT_TIME;
1467 nfs4_state_start_reclaim_nograce(clp);
0400a6b0 1468 nfs4_schedule_state_manager(clp);
0f79fd6f
TM
1469 }
1470}
1471
1472static void nfs41_handle_server_reboot(struct nfs_client *clp)
1473{
1474 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1475 nfs4_state_start_reclaim_reboot(clp);
0400a6b0 1476 nfs4_schedule_state_manager(clp);
0f79fd6f
TM
1477 }
1478}
1479
1480static void nfs41_handle_state_revoked(struct nfs_client *clp)
1481{
1482 /* Temporary */
1483 nfs4_reset_all_state(clp);
1484}
1485
1486static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1487{
1488 /* This will need to handle layouts too */
1489 nfs_expire_all_delegations(clp);
1490}
1491
1492static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1493{
1494 nfs_expire_all_delegations(clp);
1495 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
0400a6b0 1496 nfs4_schedule_state_manager(clp);
0f79fd6f
TM
1497}
1498
0629e370
AB
1499void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1500{
1501 if (!flags)
1502 return;
0f79fd6f
TM
1503 else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1504 nfs41_handle_server_reboot(clp);
1505 else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
0629e370
AB
1506 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1507 SEQ4_STATUS_ADMIN_STATE_REVOKED |
0f79fd6f
TM
1508 SEQ4_STATUS_LEASE_MOVED))
1509 nfs41_handle_state_revoked(clp);
1510 else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1511 nfs41_handle_recallable_state_revoked(clp);
1512 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
0629e370
AB
1513 SEQ4_STATUS_BACKCHANNEL_FAULT |
1514 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
0f79fd6f 1515 nfs41_handle_cb_path_down(clp);
0629e370
AB
1516}
1517
c3fad1b1
AA
1518static int nfs4_reset_session(struct nfs_client *clp)
1519{
1520 int status;
1521
38045412 1522 nfs4_begin_drain_session(clp);
c3fad1b1
AA
1523 status = nfs4_proc_destroy_session(clp->cl_session);
1524 if (status && status != -NFS4ERR_BADSESSION &&
1525 status != -NFS4ERR_DEADSESSION) {
f455848a 1526 status = nfs4_recovery_handle_error(clp, status);
c3fad1b1
AA
1527 goto out;
1528 }
1529
1530 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
f26468fb 1531 status = nfs4_proc_create_session(clp);
41f54a55 1532 if (status) {
f455848a 1533 status = nfs4_recovery_handle_error(clp, status);
41f54a55
AA
1534 goto out;
1535 }
1536 /* create_session negotiated new slot table */
1537 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
9dfdf404 1538
41f54a55
AA
1539 /* Let the state manager reestablish state */
1540 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
5601a00d 1541 nfs41_setup_state_renewal(clp);
41f54a55 1542out:
c3fad1b1
AA
1543 return status;
1544}
76db6d95 1545
b9efa1b2
AA
1546static int nfs4_recall_slot(struct nfs_client *clp)
1547{
1548 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1549 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1550 struct nfs4_slot *new, *old;
1551 int i;
1552
1553 nfs4_begin_drain_session(clp);
1554 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
8535b2be 1555 GFP_NOFS);
b9efa1b2
AA
1556 if (!new)
1557 return -ENOMEM;
1558
1559 spin_lock(&fc_tbl->slot_tbl_lock);
1560 for (i = 0; i < fc_tbl->target_max_slots; i++)
1561 new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1562 old = fc_tbl->slots;
1563 fc_tbl->slots = new;
1564 fc_tbl->max_slots = fc_tbl->target_max_slots;
1565 fc_tbl->target_max_slots = 0;
1566 fc_attrs->max_reqs = fc_tbl->max_slots;
1567 spin_unlock(&fc_tbl->slot_tbl_lock);
1568
1569 kfree(old);
1570 nfs4_end_drain_session(clp);
1571 return 0;
1572}
1573
76db6d95 1574#else /* CONFIG_NFS_V4_1 */
c3fad1b1 1575static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
5601a00d 1576static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
b9efa1b2 1577static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
76db6d95
AA
1578#endif /* CONFIG_NFS_V4_1 */
1579
78722e9c
AA
1580/* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1581 * on EXCHANGE_ID for v4.1
1582 */
1583static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1584{
1585 if (nfs4_has_session(clp)) {
1586 switch (status) {
1587 case -NFS4ERR_DELAY:
1588 case -NFS4ERR_CLID_INUSE:
1589 case -EAGAIN:
1590 break;
1591
168667c4
TM
1592 case -EKEYEXPIRED:
1593 nfs4_warn_keyexpired(clp->cl_hostname);
78722e9c
AA
1594 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1595 * in nfs4_exchange_id */
1596 default:
1597 return;
1598 }
1599 }
1600 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1601}
1602
e005e804 1603static void nfs4_state_manager(struct nfs_client *clp)
02860014 1604{
02860014
TM
1605 int status = 0;
1606
02860014 1607 /* Ensure exclusive access to NFSv4 state */
f3c76491 1608 for(;;) {
b79a4a1b
TM
1609 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1610 /* We're going to have to re-establish a clientid */
1611 status = nfs4_reclaim_lease(clp);
1612 if (status) {
78722e9c 1613 nfs4_set_lease_expired(clp, status);
b6d408ba
TM
1614 if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1615 &clp->cl_state))
b79a4a1b 1616 continue;
76db6d95
AA
1617 if (clp->cl_cons_state ==
1618 NFS_CS_SESSION_INITING)
1619 nfs_mark_client_ready(clp, status);
b79a4a1b
TM
1620 goto out_error;
1621 }
e598d843 1622 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
7cab89b2 1623 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
974cec8c 1624 pnfs_destroy_all_layouts(clp);
e598d843
TM
1625 }
1626
1627 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1628 status = nfs4_check_lease(clp);
b6d408ba 1629 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
e598d843 1630 continue;
b6d408ba
TM
1631 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1632 goto out_error;
b79a4a1b 1633 }
b6d408ba 1634
c3fad1b1 1635 /* Initialize or reset the session */
6df08189 1636 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
7111dc73 1637 && nfs4_has_session(clp)) {
4d643d1d 1638 status = nfs4_reset_session(clp);
b6d408ba
TM
1639 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1640 continue;
1641 if (status < 0)
76db6d95 1642 goto out_error;
76db6d95 1643 }
b6d408ba 1644
b79a4a1b 1645 /* First recover reboot state... */
e345e88a 1646 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
591d71cb 1647 status = nfs4_do_reclaim(clp,
c48f4f35 1648 clp->cl_mvops->reboot_recovery_ops);
b6d408ba 1649 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
6df08189 1650 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
c3fad1b1 1651 continue;
b79a4a1b 1652 nfs4_state_end_reclaim_reboot(clp);
b6d408ba
TM
1653 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1654 continue;
1655 if (status < 0)
1656 goto out_error;
02860014
TM
1657 }
1658
b79a4a1b
TM
1659 /* Now recover expired state... */
1660 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
591d71cb 1661 status = nfs4_do_reclaim(clp,
c48f4f35 1662 clp->cl_mvops->nograce_recovery_ops);
b6d408ba 1663 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
6df08189 1664 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
b6d408ba
TM
1665 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1666 continue;
1667 if (status < 0)
b79a4a1b 1668 goto out_error;
1da177e4 1669 }
707fb4b3 1670
5601a00d 1671 nfs4_end_drain_session(clp);
707fb4b3
TM
1672 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1673 nfs_client_return_marked_delegations(clp);
1674 continue;
1675 }
b9efa1b2
AA
1676 /* Recall session slots */
1677 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1678 && nfs4_has_session(clp)) {
1679 status = nfs4_recall_slot(clp);
1680 if (status < 0)
1681 goto out_error;
1682 continue;
1683 }
1684
e005e804
TM
1685
1686 nfs4_clear_state_manager_bit(clp);
f3c76491
TM
1687 /* Did we race with an attempt to give us more work? */
1688 if (clp->cl_state == 0)
1689 break;
1690 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1691 break;
1da177e4 1692 }
e005e804 1693 return;
1da177e4 1694out_error:
e005e804 1695 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
5d8515ca 1696 " with error %d\n", clp->cl_hostname, -status);
5601a00d 1697 nfs4_end_drain_session(clp);
e005e804
TM
1698 nfs4_clear_state_manager_bit(clp);
1699}
1700
1701static int nfs4_run_state_manager(void *ptr)
1702{
1703 struct nfs_client *clp = ptr;
1704
1705 allow_signal(SIGKILL);
1706 nfs4_state_manager(clp);
1707 nfs_put_client(clp);
1708 module_put_and_exit(0);
1709 return 0;
1da177e4
LT
1710}
1711
1712/*
1713 * Local variables:
1714 * c-basic-offset: 8
1715 * End:
1716 */