]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfs/nfs4state.c
NFS: Enable slot table helpers for NFSv4.0
[mirror_ubuntu-artful-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>
0aaaf5c4 52#include <linux/jiffies.h>
1da177e4 53
05f4c350
CL
54#include <linux/sunrpc/clnt.h>
55
4ce79717 56#include "nfs4_fs.h"
1da177e4
LT
57#include "callback.h"
58#include "delegation.h"
24c8dbbb 59#include "internal.h"
76e697ba 60#include "nfs4session.h"
974cec8c 61#include "pnfs.h"
bbe0a3aa 62#include "netns.h"
1da177e4 63
e3c0fb7e
CL
64#define NFSDBG_FACILITY NFSDBG_STATE
65
1da177e4
LT
66#define OPENOWNER_POOL_SIZE 8
67
4ce79717 68const nfs4_stateid zero_stateid;
05f4c350 69static DEFINE_MUTEX(nfs_clid_init_mutex);
1da177e4 70
591d71cb 71int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
1da177e4 72{
fd954ae1
TM
73 struct nfs4_setclientid_res clid = {
74 .clientid = clp->cl_clientid,
75 .confirm = clp->cl_confirm,
76 };
f738f517
CL
77 unsigned short port;
78 int status;
bbe0a3aa 79 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
f738f517 80
fd954ae1
TM
81 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
82 goto do_confirm;
bbe0a3aa 83 port = nn->nfs_callback_tcpport;
f738f517 84 if (clp->cl_addr.ss_family == AF_INET6)
29dcc16a 85 port = nn->nfs_callback_tcpport6;
f738f517 86
bb8b27e5
TM
87 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
88 if (status != 0)
89 goto out;
fd954ae1
TM
90 clp->cl_clientid = clid.clientid;
91 clp->cl_confirm = clid.confirm;
92 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
93do_confirm:
bb8b27e5
TM
94 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
95 if (status != 0)
96 goto out;
fd954ae1 97 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
bb8b27e5
TM
98 nfs4_schedule_state_renewal(clp);
99out:
1da177e4
LT
100 return status;
101}
102
05f4c350
CL
103/**
104 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
105 *
106 * @clp: nfs_client under test
107 * @result: OUT: found nfs_client, or clp
108 * @cred: credential to use for trunking test
109 *
110 * Returns zero, a negative errno, or a negative NFS4ERR status.
111 * If zero is returned, an nfs_client pointer is planted in
112 * "result".
113 *
114 * Note: The returned client may not yet be marked ready.
115 */
116int nfs40_discover_server_trunking(struct nfs_client *clp,
117 struct nfs_client **result,
118 struct rpc_cred *cred)
119{
120 struct nfs4_setclientid_res clid = {
121 .clientid = clp->cl_clientid,
122 .confirm = clp->cl_confirm,
123 };
9f62387d 124 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
05f4c350
CL
125 unsigned short port;
126 int status;
127
9f62387d 128 port = nn->nfs_callback_tcpport;
05f4c350 129 if (clp->cl_addr.ss_family == AF_INET6)
9f62387d 130 port = nn->nfs_callback_tcpport6;
05f4c350
CL
131
132 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
133 if (status != 0)
134 goto out;
135 clp->cl_clientid = clid.clientid;
136 clp->cl_confirm = clid.confirm;
137
138 status = nfs40_walk_client_list(clp, result, cred);
202c312d 139 if (status == 0) {
05f4c350
CL
140 /* Sustain the lease, even if it's empty. If the clientid4
141 * goes stale it's of no use for trunking discovery. */
142 nfs4_schedule_state_renewal(*result);
05f4c350 143 }
05f4c350
CL
144out:
145 return status;
146}
147
a7b72103 148struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
a2b2bb88
TM
149{
150 struct rpc_cred *cred = NULL;
151
a2b2bb88
TM
152 if (clp->cl_machine_cred != NULL)
153 cred = get_rpccred(clp->cl_machine_cred);
a2b2bb88
TM
154 return cred;
155}
156
d688f7b8
CL
157static void nfs4_root_machine_cred(struct nfs_client *clp)
158{
159 struct rpc_cred *cred, *new;
160
161 new = rpc_lookup_machine_cred(NULL);
162 spin_lock(&clp->cl_lock);
163 cred = clp->cl_machine_cred;
164 clp->cl_machine_cred = new;
165 spin_unlock(&clp->cl_lock);
166 if (cred != NULL)
167 put_rpccred(cred);
168}
169
24d292b8
CL
170static struct rpc_cred *
171nfs4_get_renew_cred_server_locked(struct nfs_server *server)
b4454fe1 172{
24d292b8 173 struct rpc_cred *cred = NULL;
b4454fe1 174 struct nfs4_state_owner *sp;
9f958ab8 175 struct rb_node *pos;
b4454fe1 176
24d292b8
CL
177 for (pos = rb_first(&server->state_owners);
178 pos != NULL;
179 pos = rb_next(pos)) {
180 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
b4454fe1
TM
181 if (list_empty(&sp->so_states))
182 continue;
183 cred = get_rpccred(sp->so_cred);
184 break;
185 }
186 return cred;
187}
188
24d292b8
CL
189/**
190 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
191 * @clp: client state handle
192 *
193 * Returns an rpc_cred with reference count bumped, or NULL.
194 * Caller must hold clp->cl_lock.
195 */
196struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
197{
198 struct rpc_cred *cred = NULL;
199 struct nfs_server *server;
200
e49a29bd
SP
201 /* Use machine credentials if available */
202 cred = nfs4_get_machine_cred_locked(clp);
203 if (cred != NULL)
204 goto out;
205
24d292b8
CL
206 rcu_read_lock();
207 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
208 cred = nfs4_get_renew_cred_server_locked(server);
209 if (cred != NULL)
210 break;
211 }
212 rcu_read_unlock();
e49a29bd
SP
213
214out:
24d292b8
CL
215 return cred;
216}
217
b4b82607
AA
218#if defined(CONFIG_NFS_V4_1)
219
9430fb6b
RL
220static int nfs41_setup_state_renewal(struct nfs_client *clp)
221{
222 int status;
223 struct nfs_fsinfo fsinfo;
224
d6fb79d4
AA
225 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
226 nfs4_schedule_state_renewal(clp);
227 return 0;
228 }
229
9430fb6b
RL
230 status = nfs4_proc_get_lease_time(clp, &fsinfo);
231 if (status == 0) {
232 /* Update lease time and schedule renewal */
233 spin_lock(&clp->cl_lock);
234 clp->cl_lease_time = fsinfo.lease_time * HZ;
235 clp->cl_last_renewal = jiffies;
236 spin_unlock(&clp->cl_lock);
237
238 nfs4_schedule_state_renewal(clp);
239 }
240
241 return status;
242}
243
62f288a0 244static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
9dfdf404 245{
774d5f14 246 if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
961a828d 247 spin_lock(&tbl->slot_tbl_lock);
b75ad4cd 248 nfs41_wake_slot_table(tbl);
961a828d 249 spin_unlock(&tbl->slot_tbl_lock);
689cf5c1 250 }
9dfdf404
RL
251}
252
62f288a0
AA
253static void nfs4_end_drain_session(struct nfs_client *clp)
254{
255 struct nfs4_session *ses = clp->cl_session;
256
257 if (ses != NULL) {
258 nfs4_end_drain_slot_table(&ses->bc_slot_table);
259 nfs4_end_drain_slot_table(&ses->fc_slot_table);
260 }
261}
262
774d5f14 263static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
9dfdf404 264{
774d5f14 265 set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
9dfdf404 266 spin_lock(&tbl->slot_tbl_lock);
b6bf6e7d 267 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
42acd021 268 INIT_COMPLETION(tbl->complete);
9dfdf404 269 spin_unlock(&tbl->slot_tbl_lock);
42acd021 270 return wait_for_completion_interruptible(&tbl->complete);
9dfdf404
RL
271 }
272 spin_unlock(&tbl->slot_tbl_lock);
273 return 0;
274}
275
42acd021
AA
276static int nfs4_begin_drain_session(struct nfs_client *clp)
277{
278 struct nfs4_session *ses = clp->cl_session;
279 int ret = 0;
280
42acd021 281 /* back channel */
774d5f14 282 ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
42acd021
AA
283 if (ret)
284 return ret;
285 /* fore channel */
774d5f14 286 return nfs4_drain_slot_tbl(&ses->fc_slot_table);
42acd021
AA
287}
288
bda197f5
TM
289static void nfs41_finish_session_reset(struct nfs_client *clp)
290{
291 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
292 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
293 /* create_session negotiated new slot table */
bda197f5
TM
294 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
295 nfs41_setup_state_renewal(clp);
296}
297
4d643d1d
AA
298int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
299{
300 int status;
301
fd954ae1
TM
302 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
303 goto do_confirm;
38045412 304 nfs4_begin_drain_session(clp);
4d643d1d 305 status = nfs4_proc_exchange_id(clp, cred);
9430fb6b
RL
306 if (status != 0)
307 goto out;
fd954ae1
TM
308 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
309do_confirm:
848f5bda 310 status = nfs4_proc_create_session(clp, cred);
9430fb6b
RL
311 if (status != 0)
312 goto out;
bda197f5 313 nfs41_finish_session_reset(clp);
9430fb6b
RL
314 nfs_mark_client_ready(clp, NFS_CS_READY);
315out:
4d643d1d
AA
316 return status;
317}
318
05f4c350
CL
319/**
320 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
321 *
322 * @clp: nfs_client under test
323 * @result: OUT: found nfs_client, or clp
324 * @cred: credential to use for trunking test
325 *
326 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
327 * If NFS4_OK is returned, an nfs_client pointer is planted in
328 * "result".
329 *
330 * Note: The returned client may not yet be marked ready.
331 */
332int nfs41_discover_server_trunking(struct nfs_client *clp,
333 struct nfs_client **result,
334 struct rpc_cred *cred)
335{
336 int status;
337
338 status = nfs4_proc_exchange_id(clp, cred);
339 if (status != NFS4_OK)
340 return status;
fd483570 341 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
05f4c350
CL
342
343 return nfs41_walk_client_list(clp, result, cred);
344}
345
b4b82607
AA
346#endif /* CONFIG_NFS_V4_1 */
347
24d292b8 348/**
73d8bde5 349 * nfs4_get_clid_cred - Acquire credential for a setclientid operation
24d292b8
CL
350 * @clp: client state handle
351 *
352 * Returns an rpc_cred with reference count bumped, or NULL.
353 */
73d8bde5 354struct rpc_cred *nfs4_get_clid_cred(struct nfs_client *clp)
24d292b8 355{
a2b2bb88 356 struct rpc_cred *cred;
286d7d6a 357
6dc9d57a
TM
358 spin_lock(&clp->cl_lock);
359 cred = nfs4_get_machine_cred_locked(clp);
24d292b8 360 spin_unlock(&clp->cl_lock);
a2b2bb88 361 return cred;
286d7d6a
TM
362}
363
1da177e4 364static struct nfs4_state_owner *
24d292b8 365nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
1da177e4 366{
24d292b8 367 struct rb_node **p = &server->state_owners.rb_node,
9f958ab8 368 *parent = NULL;
414adf14 369 struct nfs4_state_owner *sp;
1da177e4 370
9f958ab8
TM
371 while (*p != NULL) {
372 parent = *p;
24d292b8 373 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
9f958ab8
TM
374
375 if (cred < sp->so_cred)
376 p = &parent->rb_left;
377 else if (cred > sp->so_cred)
378 p = &parent->rb_right;
379 else {
0aaaf5c4
CL
380 if (!list_empty(&sp->so_lru))
381 list_del_init(&sp->so_lru);
9f958ab8 382 atomic_inc(&sp->so_count);
414adf14 383 return sp;
9f958ab8 384 }
1da177e4 385 }
414adf14 386 return NULL;
1da177e4
LT
387}
388
9f958ab8 389static struct nfs4_state_owner *
24d292b8 390nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
9f958ab8 391{
24d292b8
CL
392 struct nfs_server *server = new->so_server;
393 struct rb_node **p = &server->state_owners.rb_node,
9f958ab8
TM
394 *parent = NULL;
395 struct nfs4_state_owner *sp;
9157c31d 396 int err;
9f958ab8
TM
397
398 while (*p != NULL) {
399 parent = *p;
24d292b8 400 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
9f958ab8
TM
401
402 if (new->so_cred < sp->so_cred)
403 p = &parent->rb_left;
404 else if (new->so_cred > sp->so_cred)
405 p = &parent->rb_right;
406 else {
0aaaf5c4
CL
407 if (!list_empty(&sp->so_lru))
408 list_del_init(&sp->so_lru);
9f958ab8
TM
409 atomic_inc(&sp->so_count);
410 return sp;
411 }
412 }
48c22eb2 413 err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
9157c31d
TM
414 if (err)
415 return ERR_PTR(err);
24d292b8
CL
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);
48c22eb2 428 ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
9f958ab8
TM
429}
430
7ba127ab
TM
431static void
432nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
433{
95b72eb0 434 sc->create_time = ktime_get();
7ba127ab
TM
435 sc->flags = 0;
436 sc->counter = 0;
437 spin_lock_init(&sc->lock);
438 INIT_LIST_HEAD(&sc->list);
439 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
440}
441
442static void
443nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
444{
445 rpc_destroy_wait_queue(&sc->wait);
446}
447
1da177e4
LT
448/*
449 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
450 * create a new state_owner.
451 *
452 */
453static struct nfs4_state_owner *
d1e284d5
TM
454nfs4_alloc_state_owner(struct nfs_server *server,
455 struct rpc_cred *cred,
456 gfp_t gfp_flags)
1da177e4
LT
457{
458 struct nfs4_state_owner *sp;
459
d1e284d5 460 sp = kzalloc(sizeof(*sp), gfp_flags);
1da177e4
LT
461 if (!sp)
462 return NULL;
d1e284d5
TM
463 sp->so_server = server;
464 sp->so_cred = get_rpccred(cred);
ec073428 465 spin_lock_init(&sp->so_lock);
1da177e4 466 INIT_LIST_HEAD(&sp->so_states);
7ba127ab 467 nfs4_init_seqid_counter(&sp->so_seqid);
1da177e4 468 atomic_set(&sp->so_count, 1);
0aaaf5c4 469 INIT_LIST_HEAD(&sp->so_lru);
c137afab 470 seqcount_init(&sp->so_reclaim_seqcount);
65b62a29 471 mutex_init(&sp->so_delegreturn_mutex);
1da177e4
LT
472 return sp;
473}
474
1d2e88e7 475static void
1da177e4
LT
476nfs4_drop_state_owner(struct nfs4_state_owner *sp)
477{
c77365c9
TM
478 struct rb_node *rb_node = &sp->so_server_node;
479
480 if (!RB_EMPTY_NODE(rb_node)) {
24d292b8
CL
481 struct nfs_server *server = sp->so_server;
482 struct nfs_client *clp = server->nfs_client;
9f958ab8
TM
483
484 spin_lock(&clp->cl_lock);
c77365c9
TM
485 if (!RB_EMPTY_NODE(rb_node)) {
486 rb_erase(rb_node, &server->state_owners);
487 RB_CLEAR_NODE(rb_node);
488 }
9f958ab8
TM
489 spin_unlock(&clp->cl_lock);
490 }
1da177e4
LT
491}
492
0aaaf5c4
CL
493static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
494{
7ba127ab 495 nfs4_destroy_seqid_counter(&sp->so_seqid);
0aaaf5c4
CL
496 put_rpccred(sp->so_cred);
497 kfree(sp);
498}
499
500static void nfs4_gc_state_owners(struct nfs_server *server)
501{
502 struct nfs_client *clp = server->nfs_client;
503 struct nfs4_state_owner *sp, *tmp;
504 unsigned long time_min, time_max;
505 LIST_HEAD(doomed);
506
507 spin_lock(&clp->cl_lock);
508 time_max = jiffies;
509 time_min = (long)time_max - (long)clp->cl_lease_time;
510 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
511 /* NB: LRU is sorted so that oldest is at the head */
512 if (time_in_range(sp->so_expires, time_min, time_max))
513 break;
514 list_move(&sp->so_lru, &doomed);
515 nfs4_remove_state_owner_locked(sp);
516 }
517 spin_unlock(&clp->cl_lock);
518
519 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
520 list_del(&sp->so_lru);
521 nfs4_free_state_owner(sp);
522 }
523}
524
24d292b8
CL
525/**
526 * nfs4_get_state_owner - Look up a state owner given a credential
527 * @server: nfs_server to search
528 * @cred: RPC credential to match
529 *
530 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
531 */
532struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
d1e284d5
TM
533 struct rpc_cred *cred,
534 gfp_t gfp_flags)
1da177e4 535{
7539bbab 536 struct nfs_client *clp = server->nfs_client;
1da177e4
LT
537 struct nfs4_state_owner *sp, *new;
538
1da177e4 539 spin_lock(&clp->cl_lock);
24d292b8 540 sp = nfs4_find_state_owner_locked(server, cred);
1da177e4 541 spin_unlock(&clp->cl_lock);
1da177e4 542 if (sp != NULL)
0aaaf5c4 543 goto out;
d1e284d5 544 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
9f958ab8 545 if (new == NULL)
0aaaf5c4 546 goto out;
9157c31d
TM
547 do {
548 if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
549 break;
550 spin_lock(&clp->cl_lock);
551 sp = nfs4_insert_state_owner_locked(new);
552 spin_unlock(&clp->cl_lock);
553 } while (sp == ERR_PTR(-EAGAIN));
d1e284d5
TM
554 if (sp != new)
555 nfs4_free_state_owner(new);
0aaaf5c4
CL
556out:
557 nfs4_gc_state_owners(server);
9f958ab8 558 return sp;
1da177e4
LT
559}
560
24d292b8
CL
561/**
562 * nfs4_put_state_owner - Release a nfs4_state_owner
563 * @sp: state owner data to release
7bf97bc2
TM
564 *
565 * Note that we keep released state owners on an LRU
566 * list.
567 * This caches valid state owners so that they can be
568 * reused, to avoid the OPEN_CONFIRM on minor version 0.
569 * It also pins the uniquifier of dropped state owners for
570 * a while, to ensure that those state owner names are
571 * never reused.
24d292b8 572 */
1da177e4
LT
573void nfs4_put_state_owner(struct nfs4_state_owner *sp)
574{
0aaaf5c4
CL
575 struct nfs_server *server = sp->so_server;
576 struct nfs_client *clp = server->nfs_client;
1da177e4
LT
577
578 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
579 return;
0aaaf5c4 580
7bf97bc2
TM
581 sp->so_expires = jiffies;
582 list_add_tail(&sp->so_lru, &server->state_owners_lru);
583 spin_unlock(&clp->cl_lock);
0aaaf5c4
CL
584}
585
586/**
587 * nfs4_purge_state_owners - Release all cached state owners
588 * @server: nfs_server with cached state owners to release
589 *
590 * Called at umount time. Remaining state owners will be on
591 * the LRU with ref count of zero.
592 */
593void nfs4_purge_state_owners(struct nfs_server *server)
594{
595 struct nfs_client *clp = server->nfs_client;
596 struct nfs4_state_owner *sp, *tmp;
597 LIST_HEAD(doomed);
598
599 spin_lock(&clp->cl_lock);
600 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
601 list_move(&sp->so_lru, &doomed);
602 nfs4_remove_state_owner_locked(sp);
603 }
1da177e4 604 spin_unlock(&clp->cl_lock);
0aaaf5c4
CL
605
606 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
607 list_del(&sp->so_lru);
608 nfs4_free_state_owner(sp);
609 }
1da177e4
LT
610}
611
612static struct nfs4_state *
613nfs4_alloc_open_state(void)
614{
615 struct nfs4_state *state;
616
8535b2be 617 state = kzalloc(sizeof(*state), GFP_NOFS);
1da177e4
LT
618 if (!state)
619 return NULL;
1da177e4
LT
620 atomic_set(&state->count, 1);
621 INIT_LIST_HEAD(&state->lock_states);
8d0a8a9d 622 spin_lock_init(&state->state_lock);
8bda4e4c 623 seqlock_init(&state->seqlock);
1da177e4
LT
624 return state;
625}
626
4cecb76f 627void
dc0b027d 628nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
4cecb76f 629{
dc0b027d 630 if (state->state == fmode)
4cecb76f
TM
631 return;
632 /* NB! List reordering - see the reclaim code for why. */
dc0b027d
TM
633 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
634 if (fmode & FMODE_WRITE)
4cecb76f
TM
635 list_move(&state->open_states, &state->owner->so_states);
636 else
637 list_move_tail(&state->open_states, &state->owner->so_states);
638 }
dc0b027d 639 state->state = fmode;
4cecb76f
TM
640}
641
1da177e4
LT
642static struct nfs4_state *
643__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
644{
645 struct nfs_inode *nfsi = NFS_I(inode);
646 struct nfs4_state *state;
647
648 list_for_each_entry(state, &nfsi->open_states, inode_states) {
1c816efa 649 if (state->owner != owner)
1da177e4 650 continue;
5d422301
TM
651 if (!nfs4_valid_open_stateid(state))
652 continue;
1c816efa 653 if (atomic_inc_not_zero(&state->count))
1da177e4 654 return state;
1da177e4
LT
655 }
656 return NULL;
657}
658
1da177e4
LT
659static void
660nfs4_free_open_state(struct nfs4_state *state)
661{
662 kfree(state);
663}
664
665struct nfs4_state *
666nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
667{
668 struct nfs4_state *state, *new;
669 struct nfs_inode *nfsi = NFS_I(inode);
670
671 spin_lock(&inode->i_lock);
672 state = __nfs4_find_state_byowner(inode, owner);
673 spin_unlock(&inode->i_lock);
674 if (state)
675 goto out;
676 new = nfs4_alloc_open_state();
ec073428 677 spin_lock(&owner->so_lock);
1da177e4
LT
678 spin_lock(&inode->i_lock);
679 state = __nfs4_find_state_byowner(inode, owner);
680 if (state == NULL && new != NULL) {
681 state = new;
1da177e4
LT
682 state->owner = owner;
683 atomic_inc(&owner->so_count);
684 list_add(&state->inode_states, &nfsi->open_states);
0444d76a
DC
685 ihold(inode);
686 state->inode = inode;
1da177e4 687 spin_unlock(&inode->i_lock);
ec073428
TM
688 /* Note: The reclaim code dictates that we add stateless
689 * and read-only stateids to the end of the list */
690 list_add_tail(&state->open_states, &owner->so_states);
691 spin_unlock(&owner->so_lock);
1da177e4
LT
692 } else {
693 spin_unlock(&inode->i_lock);
ec073428 694 spin_unlock(&owner->so_lock);
1da177e4
LT
695 if (new)
696 nfs4_free_open_state(new);
697 }
698out:
699 return state;
700}
701
1da177e4
LT
702void nfs4_put_open_state(struct nfs4_state *state)
703{
704 struct inode *inode = state->inode;
705 struct nfs4_state_owner *owner = state->owner;
706
ec073428 707 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
1da177e4 708 return;
ec073428 709 spin_lock(&inode->i_lock);
ba683031 710 list_del(&state->inode_states);
1da177e4 711 list_del(&state->open_states);
ec073428
TM
712 spin_unlock(&inode->i_lock);
713 spin_unlock(&owner->so_lock);
1da177e4 714 iput(inode);
1da177e4
LT
715 nfs4_free_open_state(state);
716 nfs4_put_state_owner(owner);
717}
718
719/*
83c9d41e 720 * Close the current file.
1da177e4 721 */
643168c2 722static void __nfs4_close(struct nfs4_state *state,
8535b2be 723 fmode_t fmode, gfp_t gfp_mask, int wait)
1da177e4 724{
1da177e4 725 struct nfs4_state_owner *owner = state->owner;
003707c7 726 int call_close = 0;
dc0b027d 727 fmode_t newstate;
1da177e4
LT
728
729 atomic_inc(&owner->so_count);
1da177e4 730 /* Protect against nfs4_find_state() */
ec073428 731 spin_lock(&owner->so_lock);
dc0b027d 732 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
e7616923
TM
733 case FMODE_READ:
734 state->n_rdonly--;
735 break;
736 case FMODE_WRITE:
737 state->n_wronly--;
738 break;
739 case FMODE_READ|FMODE_WRITE:
740 state->n_rdwr--;
741 }
003707c7 742 newstate = FMODE_READ|FMODE_WRITE;
e7616923 743 if (state->n_rdwr == 0) {
003707c7 744 if (state->n_rdonly == 0) {
e7616923 745 newstate &= ~FMODE_READ;
003707c7
TM
746 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
747 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
748 }
749 if (state->n_wronly == 0) {
e7616923 750 newstate &= ~FMODE_WRITE;
003707c7
TM
751 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
752 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
753 }
754 if (newstate == 0)
755 clear_bit(NFS_DELEGATED_STATE, &state->flags);
e7616923 756 }
003707c7 757 nfs4_state_set_mode_locked(state, newstate);
ec073428 758 spin_unlock(&owner->so_lock);
4cecb76f 759
003707c7 760 if (!call_close) {
b39e625b
TM
761 nfs4_put_open_state(state);
762 nfs4_put_state_owner(owner);
1f7977c1
TM
763 } else
764 nfs4_do_close(state, gfp_mask, wait);
a49c3c77
TM
765}
766
643168c2 767void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
a49c3c77 768{
643168c2 769 __nfs4_close(state, fmode, GFP_NOFS, 0);
a49c3c77
TM
770}
771
643168c2 772void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
a49c3c77 773{
643168c2 774 __nfs4_close(state, fmode, GFP_KERNEL, 1);
1da177e4
LT
775}
776
777/*
778 * Search the state->lock_states for an existing lock_owner
779 * that is compatible with current->files
780 */
781static struct nfs4_lock_state *
77041ed9 782__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
1da177e4
LT
783{
784 struct nfs4_lock_state *pos;
785 list_for_each_entry(pos, &state->lock_states, ls_locks) {
77041ed9 786 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
1da177e4 787 continue;
77041ed9
TM
788 switch (pos->ls_owner.lo_type) {
789 case NFS4_POSIX_LOCK_TYPE:
790 if (pos->ls_owner.lo_u.posix_owner != fl_owner)
791 continue;
792 break;
793 case NFS4_FLOCK_LOCK_TYPE:
794 if (pos->ls_owner.lo_u.flock_owner != fl_pid)
795 continue;
796 }
1da177e4
LT
797 atomic_inc(&pos->ls_count);
798 return pos;
799 }
800 return NULL;
801}
802
1da177e4
LT
803/*
804 * Return a compatible lock_state. If no initialized lock_state structure
805 * exists, return an uninitialized one.
806 *
1da177e4 807 */
77041ed9 808static 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
809{
810 struct nfs4_lock_state *lsp;
24d292b8 811 struct nfs_server *server = state->owner->so_server;
1da177e4 812
8535b2be 813 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
1da177e4
LT
814 if (lsp == NULL)
815 return NULL;
7ba127ab 816 nfs4_init_seqid_counter(&lsp->ls_seqid);
1da177e4 817 atomic_set(&lsp->ls_count, 1);
b64aec8d 818 lsp->ls_state = state;
77041ed9
TM
819 lsp->ls_owner.lo_type = type;
820 switch (lsp->ls_owner.lo_type) {
821 case NFS4_FLOCK_LOCK_TYPE:
822 lsp->ls_owner.lo_u.flock_owner = fl_pid;
823 break;
824 case NFS4_POSIX_LOCK_TYPE:
825 lsp->ls_owner.lo_u.posix_owner = fl_owner;
826 break;
827 default:
d2d7ce28 828 goto out_free;
77041ed9 829 }
48c22eb2
TM
830 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
831 if (lsp->ls_seqid.owner_id < 0)
d2d7ce28 832 goto out_free;
8d0a8a9d 833 INIT_LIST_HEAD(&lsp->ls_locks);
1da177e4 834 return lsp;
d2d7ce28
TM
835out_free:
836 kfree(lsp);
837 return NULL;
1da177e4
LT
838}
839
5ae67c4f 840void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9f958ab8 841{
48c22eb2 842 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
7ba127ab 843 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
9f958ab8
TM
844 kfree(lsp);
845}
846
1da177e4
LT
847/*
848 * Return a compatible lock_state. If no initialized lock_state structure
849 * exists, return an uninitialized one.
850 *
1da177e4 851 */
77041ed9 852static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
1da177e4 853{
8d0a8a9d 854 struct nfs4_lock_state *lsp, *new = NULL;
1da177e4 855
8d0a8a9d
TM
856 for(;;) {
857 spin_lock(&state->state_lock);
77041ed9 858 lsp = __nfs4_find_lock_state(state, owner, pid, type);
8d0a8a9d
TM
859 if (lsp != NULL)
860 break;
861 if (new != NULL) {
8d0a8a9d
TM
862 list_add(&new->ls_locks, &state->lock_states);
863 set_bit(LK_STATE_IN_USE, &state->flags);
864 lsp = new;
865 new = NULL;
866 break;
867 }
868 spin_unlock(&state->state_lock);
77041ed9 869 new = nfs4_alloc_lock_state(state, owner, pid, type);
8d0a8a9d
TM
870 if (new == NULL)
871 return NULL;
872 }
873 spin_unlock(&state->state_lock);
9f958ab8 874 if (new != NULL)
5ae67c4f 875 nfs4_free_lock_state(state->owner->so_server, new);
1da177e4
LT
876 return lsp;
877}
878
879/*
8d0a8a9d
TM
880 * Release reference to lock_state, and free it if we see that
881 * it is no longer in use
1da177e4 882 */
faf5f49c 883void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
1da177e4 884{
c8b2d0bf 885 struct nfs_server *server;
8d0a8a9d 886 struct nfs4_state *state;
1da177e4 887
8d0a8a9d
TM
888 if (lsp == NULL)
889 return;
890 state = lsp->ls_state;
891 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
892 return;
893 list_del(&lsp->ls_locks);
894 if (list_empty(&state->lock_states))
895 clear_bit(LK_STATE_IN_USE, &state->flags);
896 spin_unlock(&state->state_lock);
c8b2d0bf 897 server = state->owner->so_server;
795a88c9 898 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
c8b2d0bf
TM
899 struct nfs_client *clp = server->nfs_client;
900
901 clp->cl_mvops->free_lock_state(server, lsp);
902 } else
903 nfs4_free_lock_state(server, lsp);
1da177e4
LT
904}
905
8d0a8a9d 906static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
1da177e4 907{
8d0a8a9d 908 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
1da177e4 909
8d0a8a9d
TM
910 dst->fl_u.nfs4_fl.owner = lsp;
911 atomic_inc(&lsp->ls_count);
912}
1da177e4 913
8d0a8a9d 914static void nfs4_fl_release_lock(struct file_lock *fl)
1da177e4 915{
8d0a8a9d 916 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
1da177e4
LT
917}
918
6aed6285 919static const struct file_lock_operations nfs4_fl_lock_ops = {
8d0a8a9d
TM
920 .fl_copy_lock = nfs4_fl_copy_lock,
921 .fl_release_private = nfs4_fl_release_lock,
922};
923
924int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
1da177e4 925{
8d0a8a9d
TM
926 struct nfs4_lock_state *lsp;
927
928 if (fl->fl_ops != NULL)
929 return 0;
77041ed9
TM
930 if (fl->fl_flags & FL_POSIX)
931 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
932 else if (fl->fl_flags & FL_FLOCK)
17280175
TM
933 lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
934 NFS4_FLOCK_LOCK_TYPE);
77041ed9
TM
935 else
936 return -EINVAL;
8d0a8a9d
TM
937 if (lsp == NULL)
938 return -ENOMEM;
939 fl->fl_u.nfs4_fl.owner = lsp;
940 fl->fl_ops = &nfs4_fl_lock_ops;
941 return 0;
1da177e4
LT
942}
943
5521abfd
TM
944static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
945 struct nfs4_state *state,
2a369153 946 const struct nfs_lockowner *lockowner)
1da177e4 947{
8d0a8a9d 948 struct nfs4_lock_state *lsp;
2a369153
TM
949 fl_owner_t fl_owner;
950 pid_t fl_pid;
5521abfd 951 int ret = -ENOENT;
1da177e4 952
2a369153
TM
953
954 if (lockowner == NULL)
955 goto out;
956
8d0a8a9d 957 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
4fc8796d 958 goto out;
1da177e4 959
2a369153
TM
960 fl_owner = lockowner->l_owner;
961 fl_pid = lockowner->l_pid;
8d0a8a9d 962 spin_lock(&state->state_lock);
77041ed9 963 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
795a88c9 964 if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
f597c537 965 nfs4_stateid_copy(dst, &lsp->ls_stateid);
5521abfd
TM
966 ret = 0;
967 smp_rmb();
968 if (!list_empty(&lsp->ls_seqid.list))
969 ret = -EWOULDBLOCK;
4fc8796d 970 }
8d0a8a9d 971 spin_unlock(&state->state_lock);
1da177e4 972 nfs4_put_lock_state(lsp);
4fc8796d
TM
973out:
974 return ret;
975}
976
5521abfd 977static int nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
4fc8796d 978{
92b40e93 979 const nfs4_stateid *src;
5521abfd 980 int ret;
4fc8796d
TM
981 int seq;
982
983 do {
92b40e93 984 src = &zero_stateid;
4fc8796d 985 seq = read_seqbegin(&state->seqlock);
92b40e93
TM
986 if (test_bit(NFS_OPEN_STATE, &state->flags))
987 src = &state->open_stateid;
988 nfs4_stateid_copy(dst, src);
5521abfd
TM
989 ret = 0;
990 smp_rmb();
991 if (!list_empty(&state->owner->so_seqid.list))
992 ret = -EWOULDBLOCK;
4fc8796d 993 } while (read_seqretry(&state->seqlock, seq));
5521abfd 994 return ret;
4fc8796d
TM
995}
996
997/*
998 * Byte-range lock aware utility to initialize the stateid of read/write
999 * requests.
1000 */
5521abfd 1001int nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
2a369153 1002 fmode_t fmode, const struct nfs_lockowner *lockowner)
4fc8796d 1003{
5521abfd 1004 int ret = 0;
4fc8796d 1005 if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
5521abfd
TM
1006 goto out;
1007 ret = nfs4_copy_lock_stateid(dst, state, lockowner);
1008 if (ret != -ENOENT)
1009 goto out;
1010 ret = nfs4_copy_open_stateid(dst, state);
1011out:
3b66486c
TM
1012 if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
1013 dst->seqid = 0;
5521abfd 1014 return ret;
1da177e4
LT
1015}
1016
8535b2be 1017struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
cee54fc9 1018{
cee54fc9
TM
1019 struct nfs_seqid *new;
1020
8535b2be 1021 new = kmalloc(sizeof(*new), gfp_mask);
cee54fc9
TM
1022 if (new != NULL) {
1023 new->sequence = counter;
2f74c0a0 1024 INIT_LIST_HEAD(&new->list);
4601df20 1025 new->task = NULL;
cee54fc9
TM
1026 }
1027 return new;
1028}
1029
72211dbe 1030void nfs_release_seqid(struct nfs_seqid *seqid)
1da177e4 1031{
4601df20 1032 struct nfs_seqid_counter *sequence;
cee54fc9 1033
4601df20
TM
1034 if (list_empty(&seqid->list))
1035 return;
1036 sequence = seqid->sequence;
1037 spin_lock(&sequence->lock);
1038 list_del_init(&seqid->list);
1039 if (!list_empty(&sequence->list)) {
1040 struct nfs_seqid *next;
1041
1042 next = list_first_entry(&sequence->list,
1043 struct nfs_seqid, list);
1044 rpc_wake_up_queued_task(&sequence->wait, next->task);
2f74c0a0 1045 }
4601df20 1046 spin_unlock(&sequence->lock);
72211dbe
TM
1047}
1048
1049void nfs_free_seqid(struct nfs_seqid *seqid)
1050{
1051 nfs_release_seqid(seqid);
cee54fc9 1052 kfree(seqid);
1da177e4
LT
1053}
1054
1055/*
cee54fc9
TM
1056 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1057 * failed with a seqid incrementing error -
1058 * see comments nfs_fs.h:seqid_mutating_error()
1059 */
88d90939 1060static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
cee54fc9
TM
1061{
1062 switch (status) {
1063 case 0:
1064 break;
1065 case -NFS4ERR_BAD_SEQID:
6f43ddcc
TM
1066 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1067 return;
9a3ba432 1068 pr_warn_ratelimited("NFS: v4 server returned a bad"
497799e7
DM
1069 " sequence-id error on an"
1070 " unconfirmed sequence %p!\n",
6f43ddcc 1071 seqid->sequence);
cee54fc9
TM
1072 case -NFS4ERR_STALE_CLIENTID:
1073 case -NFS4ERR_STALE_STATEID:
1074 case -NFS4ERR_BAD_STATEID:
1075 case -NFS4ERR_BADXDR:
1076 case -NFS4ERR_RESOURCE:
1077 case -NFS4ERR_NOFILEHANDLE:
1078 /* Non-seqid mutating errors */
1079 return;
1080 };
1081 /*
1082 * Note: no locking needed as we are guaranteed to be first
1083 * on the sequence list
1084 */
1085 seqid->sequence->counter++;
1086}
1087
1088void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1089{
34dc1ad7
BH
1090 struct nfs4_state_owner *sp = container_of(seqid->sequence,
1091 struct nfs4_state_owner, so_seqid);
1092 struct nfs_server *server = sp->so_server;
1093
1094 if (status == -NFS4ERR_BAD_SEQID)
1da177e4 1095 nfs4_drop_state_owner(sp);
34dc1ad7
BH
1096 if (!nfs4_has_session(server->nfs_client))
1097 nfs_increment_seqid(status, seqid);
cee54fc9
TM
1098}
1099
1100/*
cee54fc9
TM
1101 * Increment the seqid if the LOCK/LOCKU succeeded, or
1102 * failed with a seqid incrementing error -
1103 * see comments nfs_fs.h:seqid_mutating_error()
1104 */
1105void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1106{
88d90939 1107 nfs_increment_seqid(status, seqid);
cee54fc9
TM
1108}
1109
1110int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1111{
7ba127ab 1112 struct nfs_seqid_counter *sequence = seqid->sequence;
cee54fc9
TM
1113 int status = 0;
1114
1115 spin_lock(&sequence->lock);
4601df20 1116 seqid->task = task;
2f74c0a0
TM
1117 if (list_empty(&seqid->list))
1118 list_add_tail(&seqid->list, &sequence->list);
1119 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1120 goto unlock;
5d00837b 1121 rpc_sleep_on(&sequence->wait, task, NULL);
2f74c0a0
TM
1122 status = -EAGAIN;
1123unlock:
cee54fc9
TM
1124 spin_unlock(&sequence->lock);
1125 return status;
1da177e4
LT
1126}
1127
e005e804 1128static int nfs4_run_state_manager(void *);
1da177e4 1129
e005e804 1130static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
433fbe4c
TM
1131{
1132 smp_mb__before_clear_bit();
e005e804 1133 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
433fbe4c 1134 smp_mb__after_clear_bit();
e005e804 1135 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
433fbe4c
TM
1136 rpc_wake_up(&clp->cl_rpcwaitq);
1137}
1138
1da177e4 1139/*
e005e804 1140 * Schedule the nfs_client asynchronous state management routine
1da177e4 1141 */
b0d3ded1 1142void nfs4_schedule_state_manager(struct nfs_client *clp)
1da177e4 1143{
5043e900 1144 struct task_struct *task;
2446ab60 1145 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1da177e4 1146
e005e804
TM
1147 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1148 return;
5043e900
TM
1149 __module_get(THIS_MODULE);
1150 atomic_inc(&clp->cl_count);
2446ab60
TM
1151
1152 /* The rcu_read_lock() is not strictly necessary, as the state
1153 * manager is the only thread that ever changes the rpc_xprt
1154 * after it's initialized. At this point, we're single threaded. */
1155 rcu_read_lock();
1156 snprintf(buf, sizeof(buf), "%s-manager",
1157 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1158 rcu_read_unlock();
f170168b 1159 task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
2446ab60
TM
1160 if (IS_ERR(task)) {
1161 printk(KERN_ERR "%s: kthread_run: %ld\n",
1162 __func__, PTR_ERR(task));
1163 nfs4_clear_state_manager_bit(clp);
1164 nfs_put_client(clp);
1165 module_put(THIS_MODULE);
1166 }
1da177e4
LT
1167}
1168
1169/*
0400a6b0 1170 * Schedule a lease recovery attempt
1da177e4 1171 */
0400a6b0 1172void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1da177e4
LT
1173{
1174 if (!clp)
1175 return;
e598d843
TM
1176 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1177 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
cc0a9843
TM
1178 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1179 clp->cl_hostname);
e005e804 1180 nfs4_schedule_state_manager(clp);
1da177e4 1181}
9cb81968 1182EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1da177e4 1183
33021279
TM
1184int nfs4_wait_clnt_recover(struct nfs_client *clp)
1185{
1186 int res;
1187
1188 might_sleep();
1189
1190 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1191 nfs_wait_bit_killable, TASK_KILLABLE);
1192 if (res)
1193 return res;
1194
1195 if (clp->cl_cons_state < 0)
1196 return clp->cl_cons_state;
1197 return 0;
1198}
1199
1200int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1201{
1202 unsigned int loop;
1203 int ret;
1204
1205 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1206 ret = nfs4_wait_clnt_recover(clp);
1207 if (ret != 0)
1208 break;
1209 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1210 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1211 break;
1212 nfs4_schedule_state_manager(clp);
1213 ret = -EIO;
1214 }
1215 return ret;
1216}
1217
ad1e3968
TM
1218/*
1219 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1220 * @clp: client to process
1221 *
1222 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1223 * resend of the SETCLIENTID and hence re-establish the
1224 * callback channel. Then return all existing delegations.
1225 */
1226static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1227{
1228 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1229 nfs_expire_all_delegations(clp);
cc0a9843
TM
1230 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1231 clp->cl_hostname);
ad1e3968
TM
1232}
1233
042b60be
TM
1234void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1235{
ad1e3968 1236 nfs40_handle_cb_pathdown(clp);
042b60be
TM
1237 nfs4_schedule_state_manager(clp);
1238}
1239
f9feab1e 1240static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
b79a4a1b
TM
1241{
1242
1243 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1244 /* Don't recover state that expired before the reboot */
1245 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1246 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1247 return 0;
1248 }
7eff03ae 1249 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
b79a4a1b
TM
1250 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1251 return 1;
1252}
1253
f9feab1e 1254static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
b79a4a1b
TM
1255{
1256 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1257 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
7eff03ae 1258 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
b79a4a1b
TM
1259 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1260 return 1;
1261}
1262
5d422301 1263int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
0400a6b0
TM
1264{
1265 struct nfs_client *clp = server->nfs_client;
1266
5d422301
TM
1267 if (!nfs4_valid_open_stateid(state))
1268 return -EBADF;
0400a6b0 1269 nfs4_state_mark_reclaim_nograce(clp, state);
cc0a9843
TM
1270 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1271 clp->cl_hostname);
0400a6b0 1272 nfs4_schedule_state_manager(clp);
5d422301 1273 return 0;
0400a6b0 1274}
9cb81968 1275EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
0400a6b0 1276
a1d0b5ee
TM
1277void nfs_inode_find_state_and_recover(struct inode *inode,
1278 const nfs4_stateid *stateid)
1279{
1280 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1281 struct nfs_inode *nfsi = NFS_I(inode);
1282 struct nfs_open_context *ctx;
1283 struct nfs4_state *state;
1284 bool found = false;
1285
1286 spin_lock(&inode->i_lock);
1287 list_for_each_entry(ctx, &nfsi->open_files, list) {
1288 state = ctx->state;
1289 if (state == NULL)
1290 continue;
1291 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1292 continue;
f597c537 1293 if (!nfs4_stateid_match(&state->stateid, stateid))
a1d0b5ee
TM
1294 continue;
1295 nfs4_state_mark_reclaim_nograce(clp, state);
1296 found = true;
1297 }
1298 spin_unlock(&inode->i_lock);
1299 if (found)
1300 nfs4_schedule_state_manager(clp);
1301}
1302
c58c8441
TM
1303static void nfs4_state_mark_open_context_bad(struct nfs4_state *state)
1304{
1305 struct inode *inode = state->inode;
1306 struct nfs_inode *nfsi = NFS_I(inode);
1307 struct nfs_open_context *ctx;
1308
1309 spin_lock(&inode->i_lock);
1310 list_for_each_entry(ctx, &nfsi->open_files, list) {
1311 if (ctx->state != state)
1312 continue;
1313 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1314 }
1315 spin_unlock(&inode->i_lock);
1316}
1317
5d422301
TM
1318static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1319{
1320 set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
c58c8441 1321 nfs4_state_mark_open_context_bad(state);
5d422301
TM
1322}
1323
a1d0b5ee 1324
02860014 1325static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1da177e4
LT
1326{
1327 struct inode *inode = state->inode;
19e03c57 1328 struct nfs_inode *nfsi = NFS_I(inode);
1da177e4
LT
1329 struct file_lock *fl;
1330 int status = 0;
1331
3f09df70
TM
1332 if (inode->i_flock == NULL)
1333 return 0;
1334
1335 /* Guard against delegation returns and new lock/unlock calls */
19e03c57 1336 down_write(&nfsi->rwsem);
3f09df70 1337 /* Protect inode->i_flock using the BKL */
1c8c601a 1338 spin_lock(&inode->i_lock);
90dc7d27 1339 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
43b2a33a 1340 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1da177e4 1341 continue;
cd3758e3 1342 if (nfs_file_open_context(fl->fl_file)->state != state)
1da177e4 1343 continue;
1c8c601a 1344 spin_unlock(&inode->i_lock);
1da177e4 1345 status = ops->recover_lock(state, fl);
1da177e4 1346 switch (status) {
965b5d67
TM
1347 case 0:
1348 break;
1349 case -ESTALE:
1350 case -NFS4ERR_ADMIN_REVOKED:
1351 case -NFS4ERR_STALE_STATEID:
1352 case -NFS4ERR_BAD_STATEID:
1353 case -NFS4ERR_EXPIRED:
1354 case -NFS4ERR_NO_GRACE:
1355 case -NFS4ERR_STALE_CLIENTID:
9c4c761a
TM
1356 case -NFS4ERR_BADSESSION:
1357 case -NFS4ERR_BADSLOT:
1358 case -NFS4ERR_BAD_HIGH_SLOT:
1359 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
965b5d67 1360 goto out;
1da177e4 1361 default:
a030889a
WAA
1362 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1363 "Zeroing state\n", __func__, status);
965b5d67
TM
1364 case -ENOMEM:
1365 case -NFS4ERR_DENIED:
1da177e4
LT
1366 case -NFS4ERR_RECLAIM_BAD:
1367 case -NFS4ERR_RECLAIM_CONFLICT:
43b2a33a 1368 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
965b5d67 1369 status = 0;
1da177e4 1370 }
1c8c601a 1371 spin_lock(&inode->i_lock);
1da177e4 1372 }
1c8c601a 1373 spin_unlock(&inode->i_lock);
965b5d67 1374out:
19e03c57 1375 up_write(&nfsi->rwsem);
1da177e4
LT
1376 return status;
1377}
1378
02860014 1379static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1da177e4
LT
1380{
1381 struct nfs4_state *state;
1382 struct nfs4_lock_state *lock;
1383 int status = 0;
1384
1385 /* Note: we rely on the sp->so_states list being ordered
1386 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1387 * states first.
1388 * This is needed to ensure that the server won't give us any
1389 * read delegations that we have to return if, say, we are
1390 * recovering after a network partition or a reboot from a
1391 * server that doesn't support a grace period.
1392 */
fe1d8195 1393 spin_lock(&sp->so_lock);
c137afab
TM
1394 write_seqcount_begin(&sp->so_reclaim_seqcount);
1395restart:
1da177e4 1396 list_for_each_entry(state, &sp->so_states, open_states) {
b79a4a1b
TM
1397 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1398 continue;
5d422301
TM
1399 if (!nfs4_valid_open_stateid(state))
1400 continue;
1da177e4
LT
1401 if (state->state == 0)
1402 continue;
fe1d8195
TM
1403 atomic_inc(&state->count);
1404 spin_unlock(&sp->so_lock);
1da177e4 1405 status = ops->recover_open(sp, state);
1da177e4 1406 if (status >= 0) {
02860014
TM
1407 status = nfs4_reclaim_locks(state, ops);
1408 if (status >= 0) {
6686390b
N
1409 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) {
1410 spin_lock(&state->state_lock);
1411 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1412 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1413 pr_warn_ratelimited("NFS: "
1414 "%s: Lock reclaim "
1415 "failed!\n", __func__);
1416 }
1417 spin_unlock(&state->state_lock);
02860014 1418 }
fe1d8195 1419 nfs4_put_open_state(state);
c137afab 1420 spin_lock(&sp->so_lock);
fe1d8195 1421 goto restart;
1da177e4 1422 }
1da177e4
LT
1423 }
1424 switch (status) {
1425 default:
a030889a
WAA
1426 printk(KERN_ERR "NFS: %s: unhandled error %d. "
1427 "Zeroing state\n", __func__, status);
1da177e4 1428 case -ENOENT:
965b5d67 1429 case -ENOMEM:
b79a4a1b 1430 case -ESTALE:
1da177e4
LT
1431 /*
1432 * Open state on this file cannot be recovered
1433 * All we can do is revert to using the zero stateid.
1434 */
5d422301 1435 nfs4_state_mark_recovery_failed(state, status);
1da177e4 1436 break;
91876b13
TM
1437 case -EAGAIN:
1438 ssleep(1);
965b5d67
TM
1439 case -NFS4ERR_ADMIN_REVOKED:
1440 case -NFS4ERR_STALE_STATEID:
1441 case -NFS4ERR_BAD_STATEID:
b79a4a1b
TM
1442 case -NFS4ERR_RECLAIM_BAD:
1443 case -NFS4ERR_RECLAIM_CONFLICT:
1f0e890d 1444 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
b79a4a1b 1445 break;
1da177e4
LT
1446 case -NFS4ERR_EXPIRED:
1447 case -NFS4ERR_NO_GRACE:
1f0e890d 1448 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1da177e4 1449 case -NFS4ERR_STALE_CLIENTID:
9c4c761a
TM
1450 case -NFS4ERR_BADSESSION:
1451 case -NFS4ERR_BADSLOT:
1452 case -NFS4ERR_BAD_HIGH_SLOT:
1453 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1da177e4
LT
1454 goto out_err;
1455 }
fe1d8195 1456 nfs4_put_open_state(state);
c137afab 1457 spin_lock(&sp->so_lock);
fe1d8195 1458 goto restart;
1da177e4 1459 }
c137afab 1460 write_seqcount_end(&sp->so_reclaim_seqcount);
fe1d8195 1461 spin_unlock(&sp->so_lock);
1da177e4
LT
1462 return 0;
1463out_err:
fe1d8195 1464 nfs4_put_open_state(state);
c137afab
TM
1465 spin_lock(&sp->so_lock);
1466 write_seqcount_end(&sp->so_reclaim_seqcount);
1467 spin_unlock(&sp->so_lock);
1da177e4
LT
1468 return status;
1469}
1470
b79a4a1b
TM
1471static void nfs4_clear_open_state(struct nfs4_state *state)
1472{
1473 struct nfs4_lock_state *lock;
1474
1475 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1476 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1477 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1478 clear_bit(NFS_O_RDWR_STATE, &state->flags);
4b44b40e 1479 spin_lock(&state->state_lock);
b79a4a1b 1480 list_for_each_entry(lock, &state->lock_states, ls_locks) {
b79a4a1b 1481 lock->ls_seqid.flags = 0;
795a88c9 1482 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
b79a4a1b 1483 }
4b44b40e 1484 spin_unlock(&state->state_lock);
b79a4a1b
TM
1485}
1486
24d292b8
CL
1487static void nfs4_reset_seqids(struct nfs_server *server,
1488 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
cee54fc9 1489{
24d292b8 1490 struct nfs_client *clp = server->nfs_client;
cee54fc9 1491 struct nfs4_state_owner *sp;
9f958ab8 1492 struct rb_node *pos;
cee54fc9 1493 struct nfs4_state *state;
cee54fc9 1494
24d292b8
CL
1495 spin_lock(&clp->cl_lock);
1496 for (pos = rb_first(&server->state_owners);
1497 pos != NULL;
1498 pos = rb_next(pos)) {
1499 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
cee54fc9 1500 sp->so_seqid.flags = 0;
ec073428 1501 spin_lock(&sp->so_lock);
cee54fc9 1502 list_for_each_entry(state, &sp->so_states, open_states) {
b79a4a1b
TM
1503 if (mark_reclaim(clp, state))
1504 nfs4_clear_open_state(state);
cee54fc9 1505 }
ec073428 1506 spin_unlock(&sp->so_lock);
cee54fc9 1507 }
24d292b8
CL
1508 spin_unlock(&clp->cl_lock);
1509}
1510
1511static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1512 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1513{
1514 struct nfs_server *server;
1515
1516 rcu_read_lock();
1517 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1518 nfs4_reset_seqids(server, mark_reclaim);
1519 rcu_read_unlock();
cee54fc9
TM
1520}
1521
b79a4a1b
TM
1522static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1523{
1524 /* Mark all delegations for reclaim */
1525 nfs_delegation_mark_reclaim(clp);
1526 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1527}
1528
fce5c838 1529static void nfs4_reclaim_complete(struct nfs_client *clp,
965e9c23
TM
1530 const struct nfs4_state_recovery_ops *ops,
1531 struct rpc_cred *cred)
fce5c838
RL
1532{
1533 /* Notify the server we're done reclaiming our state */
1534 if (ops->reclaim_complete)
965e9c23 1535 (void)ops->reclaim_complete(clp, cred);
fce5c838
RL
1536}
1537
24d292b8 1538static void nfs4_clear_reclaim_server(struct nfs_server *server)
b79a4a1b 1539{
24d292b8 1540 struct nfs_client *clp = server->nfs_client;
b79a4a1b
TM
1541 struct nfs4_state_owner *sp;
1542 struct rb_node *pos;
1543 struct nfs4_state *state;
1544
24d292b8
CL
1545 spin_lock(&clp->cl_lock);
1546 for (pos = rb_first(&server->state_owners);
1547 pos != NULL;
1548 pos = rb_next(pos)) {
1549 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
b79a4a1b
TM
1550 spin_lock(&sp->so_lock);
1551 list_for_each_entry(state, &sp->so_states, open_states) {
24d292b8
CL
1552 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1553 &state->flags))
b79a4a1b
TM
1554 continue;
1555 nfs4_state_mark_reclaim_nograce(clp, state);
1556 }
1557 spin_unlock(&sp->so_lock);
1558 }
24d292b8
CL
1559 spin_unlock(&clp->cl_lock);
1560}
1561
1562static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1563{
1564 struct nfs_server *server;
1565
1566 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1567 return 0;
1568
1569 rcu_read_lock();
1570 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1571 nfs4_clear_reclaim_server(server);
1572 rcu_read_unlock();
b79a4a1b
TM
1573
1574 nfs_delegation_reap_unclaimed(clp);
6eaa6149
TM
1575 return 1;
1576}
1577
1578static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1579{
965e9c23
TM
1580 const struct nfs4_state_recovery_ops *ops;
1581 struct rpc_cred *cred;
1582
6eaa6149
TM
1583 if (!nfs4_state_clear_reclaim_reboot(clp))
1584 return;
965e9c23 1585 ops = clp->cl_mvops->reboot_recovery_ops;
73d8bde5 1586 cred = nfs4_get_clid_cred(clp);
965e9c23
TM
1587 nfs4_reclaim_complete(clp, ops, cred);
1588 put_rpccred(cred);
b79a4a1b
TM
1589}
1590
1591static void nfs_delegation_clear_all(struct nfs_client *clp)
1592{
1593 nfs_delegation_mark_reclaim(clp);
1594 nfs_delegation_reap_unclaimed(clp);
1595}
1596
1597static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1598{
1599 nfs_delegation_clear_all(clp);
1600 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1601}
1602
4f7cdf18 1603static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
e598d843
TM
1604{
1605 switch (error) {
4f38e4aa
TM
1606 case 0:
1607 break;
e598d843 1608 case -NFS4ERR_CB_PATH_DOWN:
ad1e3968 1609 nfs40_handle_cb_pathdown(clp);
4f38e4aa 1610 break;
c8b7ae3d
TM
1611 case -NFS4ERR_NO_GRACE:
1612 nfs4_state_end_reclaim_reboot(clp);
4f38e4aa 1613 break;
e598d843
TM
1614 case -NFS4ERR_STALE_CLIENTID:
1615 case -NFS4ERR_LEASE_MOVED:
1616 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
6eaa6149 1617 nfs4_state_clear_reclaim_reboot(clp);
e598d843
TM
1618 nfs4_state_start_reclaim_reboot(clp);
1619 break;
1620 case -NFS4ERR_EXPIRED:
1621 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1622 nfs4_state_start_reclaim_nograce(clp);
8ba9bf8e 1623 break;
c3fad1b1
AA
1624 case -NFS4ERR_BADSESSION:
1625 case -NFS4ERR_BADSLOT:
1626 case -NFS4ERR_BAD_HIGH_SLOT:
1627 case -NFS4ERR_DEADSESSION:
c3fad1b1
AA
1628 case -NFS4ERR_SEQ_FALSE_RETRY:
1629 case -NFS4ERR_SEQ_MISORDERED:
6df08189 1630 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
0b9e2d41 1631 /* Zero session reset errors */
4f38e4aa 1632 break;
7c5d7256
TM
1633 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1634 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1635 break;
4f38e4aa 1636 default:
cc0a9843
TM
1637 dprintk("%s: failed to handle error %d for server %s\n",
1638 __func__, error, clp->cl_hostname);
4f38e4aa 1639 return error;
e598d843 1640 }
cc0a9843
TM
1641 dprintk("%s: handled error %d for server %s\n", __func__, error,
1642 clp->cl_hostname);
4f38e4aa 1643 return 0;
e598d843
TM
1644}
1645
02860014 1646static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1da177e4 1647{
24d292b8
CL
1648 struct nfs4_state_owner *sp;
1649 struct nfs_server *server;
9f958ab8 1650 struct rb_node *pos;
1da177e4
LT
1651 int status = 0;
1652
7eff03ae 1653restart:
24d292b8
CL
1654 rcu_read_lock();
1655 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
0aaaf5c4 1656 nfs4_purge_state_owners(server);
24d292b8
CL
1657 spin_lock(&clp->cl_lock);
1658 for (pos = rb_first(&server->state_owners);
1659 pos != NULL;
1660 pos = rb_next(pos)) {
1661 sp = rb_entry(pos,
1662 struct nfs4_state_owner, so_server_node);
1663 if (!test_and_clear_bit(ops->owner_flag_bit,
1664 &sp->so_flags))
1665 continue;
1666 atomic_inc(&sp->so_count);
1667 spin_unlock(&clp->cl_lock);
1668 rcu_read_unlock();
1669
1670 status = nfs4_reclaim_open_state(sp, ops);
1671 if (status < 0) {
1672 set_bit(ops->owner_flag_bit, &sp->so_flags);
1673 nfs4_put_state_owner(sp);
1674 return nfs4_recovery_handle_error(clp, status);
1675 }
1676
7eff03ae 1677 nfs4_put_state_owner(sp);
24d292b8 1678 goto restart;
7eff03ae 1679 }
24d292b8 1680 spin_unlock(&clp->cl_lock);
02860014 1681 }
24d292b8 1682 rcu_read_unlock();
02860014
TM
1683 return status;
1684}
1685
1686static int nfs4_check_lease(struct nfs_client *clp)
1687{
1688 struct rpc_cred *cred;
c48f4f35
TM
1689 const struct nfs4_state_maintenance_ops *ops =
1690 clp->cl_mvops->state_renewal_ops;
4f38e4aa 1691 int status;
1da177e4 1692
0f605b56
TM
1693 /* Is the client already known to have an expired lease? */
1694 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1695 return 0;
a7b72103
AA
1696 spin_lock(&clp->cl_lock);
1697 cred = ops->get_state_renewal_cred_locked(clp);
1698 spin_unlock(&clp->cl_lock);
0f605b56 1699 if (cred == NULL) {
73d8bde5 1700 cred = nfs4_get_clid_cred(clp);
4f38e4aa 1701 status = -ENOKEY;
0f605b56
TM
1702 if (cred == NULL)
1703 goto out;
286d7d6a 1704 }
8e69514f 1705 status = ops->renew_lease(clp, cred);
0f605b56 1706 put_rpccred(cred);
bc7a05ca
TM
1707 if (status == -ETIMEDOUT) {
1708 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1709 return 0;
1710 }
0f605b56 1711out:
4f7cdf18 1712 return nfs4_recovery_handle_error(clp, status);
02860014
TM
1713}
1714
47b803c8
AA
1715/* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1716 * and for recoverable errors on EXCHANGE_ID for v4.1
2a6ee6aa
TM
1717 */
1718static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1719{
1720 switch (status) {
89a21736
TM
1721 case -NFS4ERR_SEQ_MISORDERED:
1722 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1723 return -ESERVERFAULT;
1724 /* Lease confirmation error: retry after purging the lease */
1725 ssleep(1);
47b803c8
AA
1726 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1727 break;
2a6ee6aa
TM
1728 case -NFS4ERR_STALE_CLIENTID:
1729 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
47b803c8
AA
1730 nfs4_state_clear_reclaim_reboot(clp);
1731 nfs4_state_start_reclaim_reboot(clp);
2a6ee6aa 1732 break;
de734831
CL
1733 case -NFS4ERR_CLID_INUSE:
1734 pr_err("NFS: Server %s reports our clientid is in use\n",
1735 clp->cl_hostname);
1736 nfs_mark_client_ready(clp, -EPERM);
1737 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1738 return -EPERM;
2a6ee6aa 1739 case -EACCES:
2a6ee6aa
TM
1740 case -NFS4ERR_DELAY:
1741 case -ETIMEDOUT:
1742 case -EAGAIN:
1743 ssleep(1);
1744 break;
1745
1746 case -NFS4ERR_MINOR_VERS_MISMATCH:
1747 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1748 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
cc0a9843
TM
1749 dprintk("%s: exit with error %d for server %s\n",
1750 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
2a6ee6aa 1751 return -EPROTONOSUPPORT;
2a6ee6aa
TM
1752 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1753 * in nfs4_exchange_id */
1754 default:
cc0a9843
TM
1755 dprintk("%s: exit with error %d for server %s\n", __func__,
1756 status, clp->cl_hostname);
2a6ee6aa
TM
1757 return status;
1758 }
1759 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
cc0a9843
TM
1760 dprintk("%s: handled error %d for server %s\n", __func__, status,
1761 clp->cl_hostname);
2a6ee6aa
TM
1762 return 0;
1763}
1764
b42353ff 1765static int nfs4_establish_lease(struct nfs_client *clp)
02860014
TM
1766{
1767 struct rpc_cred *cred;
c48f4f35
TM
1768 const struct nfs4_state_recovery_ops *ops =
1769 clp->cl_mvops->reboot_recovery_ops;
2a6ee6aa 1770 int status;
02860014 1771
73d8bde5 1772 cred = nfs4_get_clid_cred(clp);
2a6ee6aa
TM
1773 if (cred == NULL)
1774 return -ENOENT;
1775 status = ops->establish_clid(clp, cred);
1776 put_rpccred(cred);
1777 if (status != 0)
b42353ff
TM
1778 return status;
1779 pnfs_destroy_all_layouts(clp);
1780 return 0;
1781}
1782
6bbb4ae8
CL
1783/*
1784 * Returns zero or a negative errno. NFS4ERR values are converted
1785 * to local errno values.
1786 */
b42353ff
TM
1787static int nfs4_reclaim_lease(struct nfs_client *clp)
1788{
1789 int status;
1790
1791 status = nfs4_establish_lease(clp);
1792 if (status < 0)
1793 return nfs4_handle_reclaim_lease_error(clp, status);
1794 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1795 nfs4_state_start_reclaim_nograce(clp);
1796 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1797 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1798 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1799 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1800 return 0;
1801}
1802
1803static int nfs4_purge_lease(struct nfs_client *clp)
1804{
1805 int status;
1806
1807 status = nfs4_establish_lease(clp);
1808 if (status < 0)
2a6ee6aa 1809 return nfs4_handle_reclaim_lease_error(clp, status);
b42353ff
TM
1810 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1811 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1812 nfs4_state_start_reclaim_nograce(clp);
2a6ee6aa 1813 return 0;
02860014
TM
1814}
1815
05f4c350
CL
1816/**
1817 * nfs4_discover_server_trunking - Detect server IP address trunking
1818 *
1819 * @clp: nfs_client under test
1820 * @result: OUT: found nfs_client, or clp
1821 *
1822 * Returns zero or a negative errno. If zero is returned,
1823 * an nfs_client pointer is planted in "result".
1824 *
1825 * Note: since we are invoked in process context, and
1826 * not from inside the state manager, we cannot use
1827 * nfs4_handle_reclaim_lease_error().
1828 */
1829int nfs4_discover_server_trunking(struct nfs_client *clp,
1830 struct nfs_client **result)
1831{
1832 const struct nfs4_state_recovery_ops *ops =
1833 clp->cl_mvops->reboot_recovery_ops;
05f4c350
CL
1834 struct rpc_clnt *clnt;
1835 struct rpc_cred *cred;
4edaa308 1836 int i, status;
05f4c350
CL
1837
1838 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
1839
05f4c350 1840 clnt = clp->cl_rpcclient;
05f4c350
CL
1841 i = 0;
1842
1843 mutex_lock(&nfs_clid_init_mutex);
05f4c350 1844again:
ea33e6c3 1845 status = -ENOENT;
73d8bde5 1846 cred = nfs4_get_clid_cred(clp);
05f4c350
CL
1847 if (cred == NULL)
1848 goto out_unlock;
1849
1850 status = ops->detect_trunking(clp, result, cred);
1851 put_rpccred(cred);
1852 switch (status) {
1853 case 0:
1854 break;
05f4c350
CL
1855 case -NFS4ERR_DELAY:
1856 case -ETIMEDOUT:
1857 case -EAGAIN:
1858 ssleep(1);
202c312d 1859 case -NFS4ERR_STALE_CLIENTID:
05f4c350
CL
1860 dprintk("NFS: %s after status %d, retrying\n",
1861 __func__, status);
1862 goto again;
4edaa308 1863 case -EACCES:
d688f7b8
CL
1864 if (i++ == 0) {
1865 nfs4_root_machine_cred(clp);
1866 goto again;
1867 }
1868 if (i > 2)
4edaa308 1869 break;
05f4c350
CL
1870 case -NFS4ERR_CLID_INUSE:
1871 case -NFS4ERR_WRONGSEC:
79d852bf 1872 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
05f4c350
CL
1873 if (IS_ERR(clnt)) {
1874 status = PTR_ERR(clnt);
1875 break;
1876 }
b193d59a
TM
1877 /* Note: this is safe because we haven't yet marked the
1878 * client as ready, so we are the only user of
1879 * clp->cl_rpcclient
1880 */
1881 clnt = xchg(&clp->cl_rpcclient, clnt);
1882 rpc_shutdown_client(clnt);
1883 clnt = clp->cl_rpcclient;
05f4c350
CL
1884 goto again;
1885
1886 case -NFS4ERR_MINOR_VERS_MISMATCH:
1887 status = -EPROTONOSUPPORT;
1888 break;
1889
1890 case -EKEYEXPIRED:
05f4c350
CL
1891 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1892 * in nfs4_exchange_id */
1893 status = -EKEYEXPIRED;
ea33e6c3
TM
1894 break;
1895 default:
1896 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
1897 __func__, status);
1898 status = -EIO;
05f4c350
CL
1899 }
1900
1901out_unlock:
1902 mutex_unlock(&nfs_clid_init_mutex);
05f4c350
CL
1903 dprintk("NFS: %s: status = %d\n", __func__, status);
1904 return status;
1905}
1906
76db6d95 1907#ifdef CONFIG_NFS_V4_1
9f594791 1908void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
0400a6b0 1909{
444f72fe
TM
1910 struct nfs_client *clp = session->clp;
1911
9f594791
TM
1912 switch (err) {
1913 default:
1914 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1915 break;
1916 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1917 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1918 }
444f72fe 1919 nfs4_schedule_lease_recovery(clp);
0400a6b0 1920}
cbdabc7f 1921EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
0400a6b0 1922
ac074835
TM
1923static void nfs41_ping_server(struct nfs_client *clp)
1924{
1925 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
1926 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1927 nfs4_schedule_state_manager(clp);
1928}
1929
1930void nfs41_server_notify_target_slotid_update(struct nfs_client *clp)
1931{
1932 nfs41_ping_server(clp);
1933}
1934
c10e4498
TM
1935void nfs41_server_notify_highest_slotid_update(struct nfs_client *clp)
1936{
1937 nfs41_ping_server(clp);
1938}
1939
0f79fd6f
TM
1940static void nfs4_reset_all_state(struct nfs_client *clp)
1941{
1942 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2c820d9a 1943 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
be0bfed0 1944 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
0f79fd6f 1945 nfs4_state_start_reclaim_nograce(clp);
cc0a9843
TM
1946 dprintk("%s: scheduling reset of all state for server %s!\n",
1947 __func__, clp->cl_hostname);
0400a6b0 1948 nfs4_schedule_state_manager(clp);
0f79fd6f
TM
1949 }
1950}
1951
1952static void nfs41_handle_server_reboot(struct nfs_client *clp)
1953{
1954 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1955 nfs4_state_start_reclaim_reboot(clp);
cc0a9843
TM
1956 dprintk("%s: server %s rebooted!\n", __func__,
1957 clp->cl_hostname);
0400a6b0 1958 nfs4_schedule_state_manager(clp);
0f79fd6f
TM
1959 }
1960}
1961
1962static void nfs41_handle_state_revoked(struct nfs_client *clp)
1963{
0f79fd6f 1964 nfs4_reset_all_state(clp);
cc0a9843 1965 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
0f79fd6f
TM
1966}
1967
1968static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1969{
1970 /* This will need to handle layouts too */
1971 nfs_expire_all_delegations(clp);
cc0a9843
TM
1972 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
1973 clp->cl_hostname);
0f79fd6f
TM
1974}
1975
a9e64442 1976static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
0f79fd6f
TM
1977{
1978 nfs_expire_all_delegations(clp);
1979 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
0400a6b0 1980 nfs4_schedule_state_manager(clp);
cc0a9843
TM
1981 dprintk("%s: server %s declared a backchannel fault\n", __func__,
1982 clp->cl_hostname);
0f79fd6f
TM
1983}
1984
a9e64442
WAA
1985static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1986{
1987 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1988 &clp->cl_state) == 0)
1989 nfs4_schedule_state_manager(clp);
1990}
1991
0629e370
AB
1992void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1993{
1994 if (!flags)
1995 return;
2c820d9a
CL
1996
1997 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
1998 __func__, clp->cl_hostname, clp->cl_clientid, flags);
1999
111d489f 2000 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
0f79fd6f 2001 nfs41_handle_server_reboot(clp);
111d489f 2002 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
0629e370
AB
2003 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2004 SEQ4_STATUS_ADMIN_STATE_REVOKED |
0f79fd6f
TM
2005 SEQ4_STATUS_LEASE_MOVED))
2006 nfs41_handle_state_revoked(clp);
111d489f 2007 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
0f79fd6f 2008 nfs41_handle_recallable_state_revoked(clp);
a9e64442
WAA
2009 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2010 nfs41_handle_backchannel_fault(clp);
2011 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2012 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
0f79fd6f 2013 nfs41_handle_cb_path_down(clp);
0629e370
AB
2014}
2015
c3fad1b1
AA
2016static int nfs4_reset_session(struct nfs_client *clp)
2017{
848f5bda 2018 struct rpc_cred *cred;
c3fad1b1
AA
2019 int status;
2020
1a47e7a6
TM
2021 if (!nfs4_has_session(clp))
2022 return 0;
38045412 2023 nfs4_begin_drain_session(clp);
73d8bde5 2024 cred = nfs4_get_clid_cred(clp);
848f5bda 2025 status = nfs4_proc_destroy_session(clp->cl_session, cred);
c489ee29
TM
2026 switch (status) {
2027 case 0:
2028 case -NFS4ERR_BADSESSION:
2029 case -NFS4ERR_DEADSESSION:
2030 break;
2031 case -NFS4ERR_BACK_CHAN_BUSY:
2032 case -NFS4ERR_DELAY:
2033 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2034 status = 0;
2035 ssleep(1);
2036 goto out;
2037 default:
f455848a 2038 status = nfs4_recovery_handle_error(clp, status);
c3fad1b1
AA
2039 goto out;
2040 }
2041
2042 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
848f5bda 2043 status = nfs4_proc_create_session(clp, cred);
41f54a55 2044 if (status) {
cc0a9843
TM
2045 dprintk("%s: session reset failed with status %d for server %s!\n",
2046 __func__, status, clp->cl_hostname);
f2c1b510 2047 status = nfs4_handle_reclaim_lease_error(clp, status);
41f54a55
AA
2048 goto out;
2049 }
bda197f5 2050 nfs41_finish_session_reset(clp);
cc0a9843
TM
2051 dprintk("%s: session reset was successful for server %s!\n",
2052 __func__, clp->cl_hostname);
41f54a55 2053out:
848f5bda
TM
2054 if (cred)
2055 put_rpccred(cred);
c3fad1b1
AA
2056 return status;
2057}
76db6d95 2058
a9e64442
WAA
2059static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2060{
2cf047c9
TM
2061 struct rpc_cred *cred;
2062 int ret;
2063
1a47e7a6
TM
2064 if (!nfs4_has_session(clp))
2065 return 0;
43ac544c 2066 nfs4_begin_drain_session(clp);
73d8bde5 2067 cred = nfs4_get_clid_cred(clp);
2cf047c9
TM
2068 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2069 if (cred)
2070 put_rpccred(cred);
43ac544c 2071 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
bf674c82
TM
2072 switch (ret) {
2073 case 0:
cc0a9843
TM
2074 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2075 __func__, clp->cl_hostname);
bf674c82
TM
2076 break;
2077 case -NFS4ERR_DELAY:
2078 ssleep(1);
2079 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2080 break;
2081 default:
2082 return nfs4_recovery_handle_error(clp, ret);
2083 }
2084 return 0;
a9e64442 2085}
76db6d95 2086#else /* CONFIG_NFS_V4_1 */
c3fad1b1 2087static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
b14b7979 2088static void nfs4_end_drain_session(struct nfs_client *clp) { }
a9e64442
WAA
2089
2090static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2091{
2092 return 0;
2093}
76db6d95
AA
2094#endif /* CONFIG_NFS_V4_1 */
2095
e005e804 2096static void nfs4_state_manager(struct nfs_client *clp)
02860014 2097{
02860014 2098 int status = 0;
8ed27d4f 2099 const char *section = "", *section_sep = "";
02860014 2100
02860014 2101 /* Ensure exclusive access to NFSv4 state */
47c2199b 2102 do {
2c820d9a 2103 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
8ed27d4f 2104 section = "purge state";
b42353ff 2105 status = nfs4_purge_lease(clp);
2a6ee6aa
TM
2106 if (status < 0)
2107 goto out_error;
b42353ff 2108 continue;
2c820d9a
CL
2109 }
2110
b42353ff 2111 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
8ed27d4f 2112 section = "lease expired";
b79a4a1b
TM
2113 /* We're going to have to re-establish a clientid */
2114 status = nfs4_reclaim_lease(clp);
2a6ee6aa 2115 if (status < 0)
b79a4a1b 2116 goto out_error;
b42353ff 2117 continue;
e598d843
TM
2118 }
2119
c3fad1b1 2120 /* Initialize or reset the session */
1a47e7a6 2121 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
8ed27d4f 2122 section = "reset session";
4d643d1d 2123 status = nfs4_reset_session(clp);
b6d408ba
TM
2124 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2125 continue;
2126 if (status < 0)
76db6d95 2127 goto out_error;
76db6d95 2128 }
b6d408ba 2129
a9e64442
WAA
2130 /* Send BIND_CONN_TO_SESSION */
2131 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1a47e7a6 2132 &clp->cl_state)) {
8ed27d4f 2133 section = "bind conn to session";
a9e64442
WAA
2134 status = nfs4_bind_conn_to_session(clp);
2135 if (status < 0)
2136 goto out_error;
bf674c82 2137 continue;
a9e64442
WAA
2138 }
2139
5df904ae
TM
2140 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2141 section = "check lease";
2142 status = nfs4_check_lease(clp);
2143 if (status < 0)
2144 goto out_error;
2145 continue;
2146 }
2147
b79a4a1b 2148 /* First recover reboot state... */
e345e88a 2149 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
8ed27d4f 2150 section = "reclaim reboot";
591d71cb 2151 status = nfs4_do_reclaim(clp,
c48f4f35 2152 clp->cl_mvops->reboot_recovery_ops);
b6d408ba 2153 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
6df08189 2154 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
c3fad1b1 2155 continue;
b79a4a1b 2156 nfs4_state_end_reclaim_reboot(clp);
b6d408ba
TM
2157 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2158 continue;
2159 if (status < 0)
2160 goto out_error;
02860014
TM
2161 }
2162
b79a4a1b
TM
2163 /* Now recover expired state... */
2164 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
8ed27d4f 2165 section = "reclaim nograce";
591d71cb 2166 status = nfs4_do_reclaim(clp,
c48f4f35 2167 clp->cl_mvops->nograce_recovery_ops);
b6d408ba 2168 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
6df08189 2169 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
b6d408ba
TM
2170 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
2171 continue;
2172 if (status < 0)
b79a4a1b 2173 goto out_error;
1da177e4 2174 }
707fb4b3 2175
5601a00d 2176 nfs4_end_drain_session(clp);
707fb4b3
TM
2177 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2178 nfs_client_return_marked_delegations(clp);
2179 continue;
2180 }
e005e804
TM
2181
2182 nfs4_clear_state_manager_bit(clp);
f3c76491
TM
2183 /* Did we race with an attempt to give us more work? */
2184 if (clp->cl_state == 0)
2185 break;
2186 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2187 break;
47c2199b 2188 } while (atomic_read(&clp->cl_count) > 1);
e005e804 2189 return;
1da177e4 2190out_error:
8ed27d4f
WAA
2191 if (strlen(section))
2192 section_sep = ": ";
2193 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2194 " with error %d\n", section_sep, section,
2195 clp->cl_hostname, -status);
ffe5a830 2196 ssleep(1);
5601a00d 2197 nfs4_end_drain_session(clp);
e005e804
TM
2198 nfs4_clear_state_manager_bit(clp);
2199}
2200
2201static int nfs4_run_state_manager(void *ptr)
2202{
2203 struct nfs_client *clp = ptr;
2204
2205 allow_signal(SIGKILL);
2206 nfs4_state_manager(clp);
2207 nfs_put_client(clp);
2208 module_put_and_exit(0);
2209 return 0;
1da177e4
LT
2210}
2211
2212/*
2213 * Local variables:
2214 * c-basic-offset: 8
2215 * End:
2216 */