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