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