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