blob: 1d38484e0a53b8021e59b4bcb14159a215e1e37f [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
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
41#include <linux/kernel.h>
42#include <linux/slab.h>
43#include <linux/fs.h>
44#include <linux/nfs_fs.h>
45#include <linux/kthread.h>
46#include <linux/module.h>
47#include <linux/random.h>
48#include <linux/ratelimit.h>
49#include <linux/workqueue.h>
50#include <linux/bitops.h>
51#include <linux/jiffies.h>
52#include <linux/sched/mm.h>
53
54#include <linux/sunrpc/clnt.h>
55
56#include "nfs4_fs.h"
57#include "callback.h"
58#include "delegation.h"
59#include "internal.h"
60#include "nfs4idmap.h"
61#include "nfs4session.h"
62#include "pnfs.h"
63#include "netns.h"
64#include "nfs4trace.h"
65
66#define NFSDBG_FACILITY NFSDBG_STATE
67
68#define OPENOWNER_POOL_SIZE 8
69
70static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp);
71
72const nfs4_stateid zero_stateid = {
73 { .data = { 0 } },
74 .type = NFS4_SPECIAL_STATEID_TYPE,
75};
76const nfs4_stateid invalid_stateid = {
77 {
78 /* Funky initialiser keeps older gcc versions happy */
79 .data = { 0xff, 0xff, 0xff, 0xff, 0 },
80 },
81 .type = NFS4_INVALID_STATEID_TYPE,
82};
83
84const nfs4_stateid current_stateid = {
85 {
86 /* Funky initialiser keeps older gcc versions happy */
87 .data = { 0x0, 0x0, 0x0, 0x1, 0 },
88 },
89 .type = NFS4_SPECIAL_STATEID_TYPE,
90};
91
92static DEFINE_MUTEX(nfs_clid_init_mutex);
93
94static int nfs4_setup_state_renewal(struct nfs_client *clp)
95{
96 int status;
97 struct nfs_fsinfo fsinfo;
98
99 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
100 nfs4_schedule_state_renewal(clp);
101 return 0;
102 }
103
104 status = nfs4_proc_get_lease_time(clp, &fsinfo);
105 if (status == 0) {
106 nfs4_set_lease_period(clp, fsinfo.lease_time * HZ);
107 nfs4_schedule_state_renewal(clp);
108 }
109
110 return status;
111}
112
113int nfs4_init_clientid(struct nfs_client *clp, const struct cred *cred)
114{
115 struct nfs4_setclientid_res clid = {
116 .clientid = clp->cl_clientid,
117 .confirm = clp->cl_confirm,
118 };
119 unsigned short port;
120 int status;
121 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
122
123 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
124 goto do_confirm;
125 port = nn->nfs_callback_tcpport;
126 if (clp->cl_addr.ss_family == AF_INET6)
127 port = nn->nfs_callback_tcpport6;
128
129 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
130 if (status != 0)
131 goto out;
132 clp->cl_clientid = clid.clientid;
133 clp->cl_confirm = clid.confirm;
134 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
135do_confirm:
136 status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
137 if (status != 0)
138 goto out;
139 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
140 nfs4_setup_state_renewal(clp);
141out:
142 return status;
143}
144
145/**
146 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
147 *
148 * @clp: nfs_client under test
149 * @result: OUT: found nfs_client, or clp
150 * @cred: credential to use for trunking test
151 *
152 * Returns zero, a negative errno, or a negative NFS4ERR status.
153 * If zero is returned, an nfs_client pointer is planted in
154 * "result".
155 *
156 * Note: The returned client may not yet be marked ready.
157 */
158int nfs40_discover_server_trunking(struct nfs_client *clp,
159 struct nfs_client **result,
160 const struct cred *cred)
161{
162 struct nfs4_setclientid_res clid = {
163 .clientid = clp->cl_clientid,
164 .confirm = clp->cl_confirm,
165 };
166 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
167 unsigned short port;
168 int status;
169
170 port = nn->nfs_callback_tcpport;
171 if (clp->cl_addr.ss_family == AF_INET6)
172 port = nn->nfs_callback_tcpport6;
173
174 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
175 if (status != 0)
176 goto out;
177 clp->cl_clientid = clid.clientid;
178 clp->cl_confirm = clid.confirm;
179
180 status = nfs40_walk_client_list(clp, result, cred);
181 if (status == 0) {
182 /* Sustain the lease, even if it's empty. If the clientid4
183 * goes stale it's of no use for trunking discovery. */
184 nfs4_schedule_state_renewal(*result);
185
186 /* If the client state need to recover, do it. */
187 if (clp->cl_state)
188 nfs4_schedule_state_manager(clp);
189 }
190out:
191 return status;
192}
193
194const struct cred *nfs4_get_machine_cred(struct nfs_client *clp)
195{
196 return get_cred(rpc_machine_cred());
197}
198
199static void nfs4_root_machine_cred(struct nfs_client *clp)
200{
201
202 /* Force root creds instead of machine */
203 clp->cl_principal = NULL;
204 clp->cl_rpcclient->cl_principal = NULL;
205}
206
207static const struct cred *
208nfs4_get_renew_cred_server_locked(struct nfs_server *server)
209{
210 const struct cred *cred = NULL;
211 struct nfs4_state_owner *sp;
212 struct rb_node *pos;
213
214 for (pos = rb_first(&server->state_owners);
215 pos != NULL;
216 pos = rb_next(pos)) {
217 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
218 if (list_empty(&sp->so_states))
219 continue;
220 cred = get_cred(sp->so_cred);
221 break;
222 }
223 return cred;
224}
225
226/**
227 * nfs4_get_renew_cred - Acquire credential for a renew operation
228 * @clp: client state handle
229 *
230 * Returns an rpc_cred with reference count bumped, or NULL.
231 * Caller must hold clp->cl_lock.
232 */
233const struct cred *nfs4_get_renew_cred(struct nfs_client *clp)
234{
235 const struct cred *cred = NULL;
236 struct nfs_server *server;
237
238 /* Use machine credentials if available */
239 cred = nfs4_get_machine_cred(clp);
240 if (cred != NULL)
241 goto out;
242
243 spin_lock(&clp->cl_lock);
244 rcu_read_lock();
245 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
246 cred = nfs4_get_renew_cred_server_locked(server);
247 if (cred != NULL)
248 break;
249 }
250 rcu_read_unlock();
251 spin_unlock(&clp->cl_lock);
252
253out:
254 return cred;
255}
256
257static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
258{
259 if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
260 spin_lock(&tbl->slot_tbl_lock);
261 nfs41_wake_slot_table(tbl);
262 spin_unlock(&tbl->slot_tbl_lock);
263 }
264}
265
266static void nfs4_end_drain_session(struct nfs_client *clp)
267{
268 struct nfs4_session *ses = clp->cl_session;
269
270 if (clp->cl_slot_tbl) {
271 nfs4_end_drain_slot_table(clp->cl_slot_tbl);
272 return;
273 }
274
275 if (ses != NULL) {
276 nfs4_end_drain_slot_table(&ses->bc_slot_table);
277 nfs4_end_drain_slot_table(&ses->fc_slot_table);
278 }
279}
280
281static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
282{
283 set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
284 spin_lock(&tbl->slot_tbl_lock);
285 if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
286 reinit_completion(&tbl->complete);
287 spin_unlock(&tbl->slot_tbl_lock);
288 return wait_for_completion_interruptible(&tbl->complete);
289 }
290 spin_unlock(&tbl->slot_tbl_lock);
291 return 0;
292}
293
294static int nfs4_begin_drain_session(struct nfs_client *clp)
295{
296 struct nfs4_session *ses = clp->cl_session;
297 int ret;
298
299 if (clp->cl_slot_tbl)
300 return nfs4_drain_slot_tbl(clp->cl_slot_tbl);
301
302 /* back channel */
303 ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
304 if (ret)
305 return ret;
306 /* fore channel */
307 return nfs4_drain_slot_tbl(&ses->fc_slot_table);
308}
309
310#if defined(CONFIG_NFS_V4_1)
311
312static void nfs41_finish_session_reset(struct nfs_client *clp)
313{
314 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
315 clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
316 /* create_session negotiated new slot table */
317 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
318 nfs4_setup_state_renewal(clp);
319}
320
321int nfs41_init_clientid(struct nfs_client *clp, const struct cred *cred)
322{
323 int status;
324
325 if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
326 goto do_confirm;
327 status = nfs4_proc_exchange_id(clp, cred);
328 if (status != 0)
329 goto out;
330 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
331do_confirm:
332 status = nfs4_proc_create_session(clp, cred);
333 if (status != 0)
334 goto out;
335 if (!(clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R))
336 nfs4_state_start_reclaim_reboot(clp);
337 nfs41_finish_session_reset(clp);
338 nfs_mark_client_ready(clp, NFS_CS_READY);
339out:
340 return status;
341}
342
343/**
344 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
345 *
346 * @clp: nfs_client under test
347 * @result: OUT: found nfs_client, or clp
348 * @cred: credential to use for trunking test
349 *
350 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
351 * If NFS4_OK is returned, an nfs_client pointer is planted in
352 * "result".
353 *
354 * Note: The returned client may not yet be marked ready.
355 */
356int nfs41_discover_server_trunking(struct nfs_client *clp,
357 struct nfs_client **result,
358 const struct cred *cred)
359{
360 int status;
361
362 status = nfs4_proc_exchange_id(clp, cred);
363 if (status != NFS4_OK)
364 return status;
365
366 status = nfs41_walk_client_list(clp, result, cred);
367 if (status < 0)
368 return status;
369 if (clp != *result)
370 return 0;
371
372 /*
373 * Purge state if the client id was established in a prior
374 * instance and the client id could not have arrived on the
375 * server via Transparent State Migration.
376 */
377 if (clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R) {
378 if (!test_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags))
379 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
380 else
381 set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
382 }
383 nfs4_schedule_state_manager(clp);
384 status = nfs_wait_client_init_complete(clp);
385 if (status < 0)
386 nfs_put_client(clp);
387 return status;
388}
389
390#endif /* CONFIG_NFS_V4_1 */
391
392/**
393 * nfs4_get_clid_cred - Acquire credential for a setclientid operation
394 * @clp: client state handle
395 *
396 * Returns a cred with reference count bumped, or NULL.
397 */
398const struct cred *nfs4_get_clid_cred(struct nfs_client *clp)
399{
400 const struct cred *cred;
401
402 cred = nfs4_get_machine_cred(clp);
403 return cred;
404}
405
406static struct nfs4_state_owner *
407nfs4_find_state_owner_locked(struct nfs_server *server, const struct cred *cred)
408{
409 struct rb_node **p = &server->state_owners.rb_node,
410 *parent = NULL;
411 struct nfs4_state_owner *sp;
412 int cmp;
413
414 while (*p != NULL) {
415 parent = *p;
416 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
417 cmp = cred_fscmp(cred, sp->so_cred);
418
419 if (cmp < 0)
420 p = &parent->rb_left;
421 else if (cmp > 0)
422 p = &parent->rb_right;
423 else {
424 if (!list_empty(&sp->so_lru))
425 list_del_init(&sp->so_lru);
426 atomic_inc(&sp->so_count);
427 return sp;
428 }
429 }
430 return NULL;
431}
432
433static struct nfs4_state_owner *
434nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
435{
436 struct nfs_server *server = new->so_server;
437 struct rb_node **p = &server->state_owners.rb_node,
438 *parent = NULL;
439 struct nfs4_state_owner *sp;
440 int cmp;
441
442 while (*p != NULL) {
443 parent = *p;
444 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
445 cmp = cred_fscmp(new->so_cred, sp->so_cred);
446
447 if (cmp < 0)
448 p = &parent->rb_left;
449 else if (cmp > 0)
450 p = &parent->rb_right;
451 else {
452 if (!list_empty(&sp->so_lru))
453 list_del_init(&sp->so_lru);
454 atomic_inc(&sp->so_count);
455 return sp;
456 }
457 }
458 rb_link_node(&new->so_server_node, parent, p);
459 rb_insert_color(&new->so_server_node, &server->state_owners);
460 return new;
461}
462
463static void
464nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
465{
466 struct nfs_server *server = sp->so_server;
467
468 if (!RB_EMPTY_NODE(&sp->so_server_node))
469 rb_erase(&sp->so_server_node, &server->state_owners);
470}
471
472static void
473nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
474{
475 sc->create_time = ktime_get();
476 sc->flags = 0;
477 sc->counter = 0;
478 spin_lock_init(&sc->lock);
479 INIT_LIST_HEAD(&sc->list);
480 rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
481}
482
483static void
484nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
485{
486 rpc_destroy_wait_queue(&sc->wait);
487}
488
489/*
490 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
491 * create a new state_owner.
492 *
493 */
494static struct nfs4_state_owner *
495nfs4_alloc_state_owner(struct nfs_server *server,
496 const struct cred *cred,
497 gfp_t gfp_flags)
498{
499 struct nfs4_state_owner *sp;
500
501 sp = kzalloc(sizeof(*sp), gfp_flags);
502 if (!sp)
503 return NULL;
504 sp->so_seqid.owner_id = ida_simple_get(&server->openowner_id, 0, 0,
505 gfp_flags);
506 if (sp->so_seqid.owner_id < 0) {
507 kfree(sp);
508 return NULL;
509 }
510 sp->so_server = server;
511 sp->so_cred = get_cred(cred);
512 spin_lock_init(&sp->so_lock);
513 INIT_LIST_HEAD(&sp->so_states);
514 nfs4_init_seqid_counter(&sp->so_seqid);
515 atomic_set(&sp->so_count, 1);
516 INIT_LIST_HEAD(&sp->so_lru);
517 seqcount_init(&sp->so_reclaim_seqcount);
518 mutex_init(&sp->so_delegreturn_mutex);
519 return sp;
520}
521
522static void
523nfs4_reset_state_owner(struct nfs4_state_owner *sp)
524{
525 /* This state_owner is no longer usable, but must
526 * remain in place so that state recovery can find it
527 * and the opens associated with it.
528 * It may also be used for new 'open' request to
529 * return a delegation to the server.
530 * So update the 'create_time' so that it looks like
531 * a new state_owner. This will cause the server to
532 * request an OPEN_CONFIRM to start a new sequence.
533 */
534 sp->so_seqid.create_time = ktime_get();
535}
536
537static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
538{
539 nfs4_destroy_seqid_counter(&sp->so_seqid);
540 put_cred(sp->so_cred);
541 ida_simple_remove(&sp->so_server->openowner_id, sp->so_seqid.owner_id);
542 kfree(sp);
543}
544
545static void nfs4_gc_state_owners(struct nfs_server *server)
546{
547 struct nfs_client *clp = server->nfs_client;
548 struct nfs4_state_owner *sp, *tmp;
549 unsigned long time_min, time_max;
550 LIST_HEAD(doomed);
551
552 spin_lock(&clp->cl_lock);
553 time_max = jiffies;
554 time_min = (long)time_max - (long)clp->cl_lease_time;
555 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
556 /* NB: LRU is sorted so that oldest is at the head */
557 if (time_in_range(sp->so_expires, time_min, time_max))
558 break;
559 list_move(&sp->so_lru, &doomed);
560 nfs4_remove_state_owner_locked(sp);
561 }
562 spin_unlock(&clp->cl_lock);
563
564 list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
565 list_del(&sp->so_lru);
566 nfs4_free_state_owner(sp);
567 }
568}
569
570/**
571 * nfs4_get_state_owner - Look up a state owner given a credential
572 * @server: nfs_server to search
573 * @cred: RPC credential to match
574 * @gfp_flags: allocation mode
575 *
576 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
577 */
578struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
579 const struct cred *cred,
580 gfp_t gfp_flags)
581{
582 struct nfs_client *clp = server->nfs_client;
583 struct nfs4_state_owner *sp, *new;
584
585 spin_lock(&clp->cl_lock);
586 sp = nfs4_find_state_owner_locked(server, cred);
587 spin_unlock(&clp->cl_lock);
588 if (sp != NULL)
589 goto out;
590 new = nfs4_alloc_state_owner(server, cred, gfp_flags);
591 if (new == NULL)
592 goto out;
593 spin_lock(&clp->cl_lock);
594 sp = nfs4_insert_state_owner_locked(new);
595 spin_unlock(&clp->cl_lock);
596 if (sp != new)
597 nfs4_free_state_owner(new);
598out:
599 nfs4_gc_state_owners(server);
600 return sp;
601}
602
603/**
604 * nfs4_put_state_owner - Release a nfs4_state_owner
605 * @sp: state owner data to release
606 *
607 * Note that we keep released state owners on an LRU
608 * list.
609 * This caches valid state owners so that they can be
610 * reused, to avoid the OPEN_CONFIRM on minor version 0.
611 * It also pins the uniquifier of dropped state owners for
612 * a while, to ensure that those state owner names are
613 * never reused.
614 */
615void nfs4_put_state_owner(struct nfs4_state_owner *sp)
616{
617 struct nfs_server *server = sp->so_server;
618 struct nfs_client *clp = server->nfs_client;
619
620 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
621 return;
622
623 sp->so_expires = jiffies;
624 list_add_tail(&sp->so_lru, &server->state_owners_lru);
625 spin_unlock(&clp->cl_lock);
626}
627
628/**
629 * nfs4_purge_state_owners - Release all cached state owners
630 * @server: nfs_server with cached state owners to release
631 * @head: resulting list of state owners
632 *
633 * Called at umount time. Remaining state owners will be on
634 * the LRU with ref count of zero.
635 * Note that the state owners are not freed, but are added
636 * to the list @head, which can later be used as an argument
637 * to nfs4_free_state_owners.
638 */
639void nfs4_purge_state_owners(struct nfs_server *server, struct list_head *head)
640{
641 struct nfs_client *clp = server->nfs_client;
642 struct nfs4_state_owner *sp, *tmp;
643
644 spin_lock(&clp->cl_lock);
645 list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
646 list_move(&sp->so_lru, head);
647 nfs4_remove_state_owner_locked(sp);
648 }
649 spin_unlock(&clp->cl_lock);
650}
651
652/**
653 * nfs4_purge_state_owners - Release all cached state owners
654 * @head: resulting list of state owners
655 *
656 * Frees a list of state owners that was generated by
657 * nfs4_purge_state_owners
658 */
659void nfs4_free_state_owners(struct list_head *head)
660{
661 struct nfs4_state_owner *sp, *tmp;
662
663 list_for_each_entry_safe(sp, tmp, head, so_lru) {
664 list_del(&sp->so_lru);
665 nfs4_free_state_owner(sp);
666 }
667}
668
669static struct nfs4_state *
670nfs4_alloc_open_state(void)
671{
672 struct nfs4_state *state;
673
674 state = kzalloc(sizeof(*state), GFP_NOFS);
675 if (!state)
676 return NULL;
677 refcount_set(&state->count, 1);
678 INIT_LIST_HEAD(&state->lock_states);
679 spin_lock_init(&state->state_lock);
680 seqlock_init(&state->seqlock);
681 init_waitqueue_head(&state->waitq);
682 return state;
683}
684
685void
686nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
687{
688 if (state->state == fmode)
689 return;
690 /* NB! List reordering - see the reclaim code for why. */
691 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
692 if (fmode & FMODE_WRITE)
693 list_move(&state->open_states, &state->owner->so_states);
694 else
695 list_move_tail(&state->open_states, &state->owner->so_states);
696 }
697 state->state = fmode;
698}
699
700static struct nfs4_state *
701__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
702{
703 struct nfs_inode *nfsi = NFS_I(inode);
704 struct nfs4_state *state;
705
706 list_for_each_entry_rcu(state, &nfsi->open_states, inode_states) {
707 if (state->owner != owner)
708 continue;
709 if (!nfs4_valid_open_stateid(state))
710 continue;
711 if (refcount_inc_not_zero(&state->count))
712 return state;
713 }
714 return NULL;
715}
716
717static void
718nfs4_free_open_state(struct nfs4_state *state)
719{
720 kfree_rcu(state, rcu_head);
721}
722
723struct nfs4_state *
724nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
725{
726 struct nfs4_state *state, *new;
727 struct nfs_inode *nfsi = NFS_I(inode);
728
729 rcu_read_lock();
730 state = __nfs4_find_state_byowner(inode, owner);
731 rcu_read_unlock();
732 if (state)
733 goto out;
734 new = nfs4_alloc_open_state();
735 spin_lock(&owner->so_lock);
736 spin_lock(&inode->i_lock);
737 state = __nfs4_find_state_byowner(inode, owner);
738 if (state == NULL && new != NULL) {
739 state = new;
740 state->owner = owner;
741 atomic_inc(&owner->so_count);
742 ihold(inode);
743 state->inode = inode;
744 list_add_rcu(&state->inode_states, &nfsi->open_states);
745 spin_unlock(&inode->i_lock);
746 /* Note: The reclaim code dictates that we add stateless
747 * and read-only stateids to the end of the list */
748 list_add_tail(&state->open_states, &owner->so_states);
749 spin_unlock(&owner->so_lock);
750 } else {
751 spin_unlock(&inode->i_lock);
752 spin_unlock(&owner->so_lock);
753 if (new)
754 nfs4_free_open_state(new);
755 }
756out:
757 return state;
758}
759
760void nfs4_put_open_state(struct nfs4_state *state)
761{
762 struct inode *inode = state->inode;
763 struct nfs4_state_owner *owner = state->owner;
764
765 if (!refcount_dec_and_lock(&state->count, &owner->so_lock))
766 return;
767 spin_lock(&inode->i_lock);
768 list_del_rcu(&state->inode_states);
769 list_del(&state->open_states);
770 spin_unlock(&inode->i_lock);
771 spin_unlock(&owner->so_lock);
772 iput(inode);
773 nfs4_free_open_state(state);
774 nfs4_put_state_owner(owner);
775}
776
777/*
778 * Close the current file.
779 */
780static void __nfs4_close(struct nfs4_state *state,
781 fmode_t fmode, gfp_t gfp_mask, int wait)
782{
783 struct nfs4_state_owner *owner = state->owner;
784 int call_close = 0;
785 fmode_t newstate;
786
787 atomic_inc(&owner->so_count);
788 /* Protect against nfs4_find_state() */
789 spin_lock(&owner->so_lock);
790 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
791 case FMODE_READ:
792 state->n_rdonly--;
793 break;
794 case FMODE_WRITE:
795 state->n_wronly--;
796 break;
797 case FMODE_READ|FMODE_WRITE:
798 state->n_rdwr--;
799 }
800 newstate = FMODE_READ|FMODE_WRITE;
801 if (state->n_rdwr == 0) {
802 if (state->n_rdonly == 0) {
803 newstate &= ~FMODE_READ;
804 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
805 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
806 }
807 if (state->n_wronly == 0) {
808 newstate &= ~FMODE_WRITE;
809 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
810 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
811 }
812 if (newstate == 0)
813 clear_bit(NFS_DELEGATED_STATE, &state->flags);
814 }
815 nfs4_state_set_mode_locked(state, newstate);
816 spin_unlock(&owner->so_lock);
817
818 if (!call_close) {
819 nfs4_put_open_state(state);
820 nfs4_put_state_owner(owner);
821 } else
822 nfs4_do_close(state, gfp_mask, wait);
823}
824
825void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
826{
827 __nfs4_close(state, fmode, GFP_NOFS, 0);
828}
829
830void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
831{
832 __nfs4_close(state, fmode, GFP_KERNEL, 1);
833}
834
835/*
836 * Search the state->lock_states for an existing lock_owner
837 * that is compatible with either of the given owners.
838 * If the second is non-zero, then the first refers to a Posix-lock
839 * owner (current->files) and the second refers to a flock/OFD
840 * owner (struct file*). In that case, prefer a match for the first
841 * owner.
842 * If both sorts of locks are held on the one file we cannot know
843 * which stateid was intended to be used, so a "correct" choice cannot
844 * be made. Failing that, a "consistent" choice is preferable. The
845 * consistent choice we make is to prefer the first owner, that of a
846 * Posix lock.
847 */
848static struct nfs4_lock_state *
849__nfs4_find_lock_state(struct nfs4_state *state,
850 fl_owner_t fl_owner, fl_owner_t fl_owner2)
851{
852 struct nfs4_lock_state *pos, *ret = NULL;
853 list_for_each_entry(pos, &state->lock_states, ls_locks) {
854 if (pos->ls_owner == fl_owner) {
855 ret = pos;
856 break;
857 }
858 if (pos->ls_owner == fl_owner2)
859 ret = pos;
860 }
861 if (ret)
862 refcount_inc(&ret->ls_count);
863 return ret;
864}
865
866/*
867 * Return a compatible lock_state. If no initialized lock_state structure
868 * exists, return an uninitialized one.
869 *
870 */
871static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
872{
873 struct nfs4_lock_state *lsp;
874 struct nfs_server *server = state->owner->so_server;
875
876 lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
877 if (lsp == NULL)
878 return NULL;
879 nfs4_init_seqid_counter(&lsp->ls_seqid);
880 refcount_set(&lsp->ls_count, 1);
881 lsp->ls_state = state;
882 lsp->ls_owner = fl_owner;
883 lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
884 if (lsp->ls_seqid.owner_id < 0)
885 goto out_free;
886 INIT_LIST_HEAD(&lsp->ls_locks);
887 return lsp;
888out_free:
889 kfree(lsp);
890 return NULL;
891}
892
893void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
894{
895 ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
896 nfs4_destroy_seqid_counter(&lsp->ls_seqid);
897 kfree(lsp);
898}
899
900/*
901 * Return a compatible lock_state. If no initialized lock_state structure
902 * exists, return an uninitialized one.
903 *
904 */
905static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
906{
907 struct nfs4_lock_state *lsp, *new = NULL;
908
909 for(;;) {
910 spin_lock(&state->state_lock);
911 lsp = __nfs4_find_lock_state(state, owner, NULL);
912 if (lsp != NULL)
913 break;
914 if (new != NULL) {
915 list_add(&new->ls_locks, &state->lock_states);
916 set_bit(LK_STATE_IN_USE, &state->flags);
917 lsp = new;
918 new = NULL;
919 break;
920 }
921 spin_unlock(&state->state_lock);
922 new = nfs4_alloc_lock_state(state, owner);
923 if (new == NULL)
924 return NULL;
925 }
926 spin_unlock(&state->state_lock);
927 if (new != NULL)
928 nfs4_free_lock_state(state->owner->so_server, new);
929 return lsp;
930}
931
932/*
933 * Release reference to lock_state, and free it if we see that
934 * it is no longer in use
935 */
936void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
937{
938 struct nfs_server *server;
939 struct nfs4_state *state;
940
941 if (lsp == NULL)
942 return;
943 state = lsp->ls_state;
944 if (!refcount_dec_and_lock(&lsp->ls_count, &state->state_lock))
945 return;
946 list_del(&lsp->ls_locks);
947 if (list_empty(&state->lock_states))
948 clear_bit(LK_STATE_IN_USE, &state->flags);
949 spin_unlock(&state->state_lock);
950 server = state->owner->so_server;
951 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
952 struct nfs_client *clp = server->nfs_client;
953
954 clp->cl_mvops->free_lock_state(server, lsp);
955 } else
956 nfs4_free_lock_state(server, lsp);
957}
958
959static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
960{
961 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
962
963 dst->fl_u.nfs4_fl.owner = lsp;
964 refcount_inc(&lsp->ls_count);
965}
966
967static void nfs4_fl_release_lock(struct file_lock *fl)
968{
969 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
970}
971
972static const struct file_lock_operations nfs4_fl_lock_ops = {
973 .fl_copy_lock = nfs4_fl_copy_lock,
974 .fl_release_private = nfs4_fl_release_lock,
975};
976
977int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
978{
979 struct nfs4_lock_state *lsp;
980
981 if (fl->fl_ops != NULL)
982 return 0;
983 lsp = nfs4_get_lock_state(state, fl->fl_owner);
984 if (lsp == NULL)
985 return -ENOMEM;
986 fl->fl_u.nfs4_fl.owner = lsp;
987 fl->fl_ops = &nfs4_fl_lock_ops;
988 return 0;
989}
990
991static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
992 struct nfs4_state *state,
993 const struct nfs_lock_context *l_ctx)
994{
995 struct nfs4_lock_state *lsp;
996 fl_owner_t fl_owner, fl_flock_owner;
997 int ret = -ENOENT;
998
999 if (l_ctx == NULL)
1000 goto out;
1001
1002 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
1003 goto out;
1004
1005 fl_owner = l_ctx->lockowner;
1006 fl_flock_owner = l_ctx->open_context->flock_owner;
1007
1008 spin_lock(&state->state_lock);
1009 lsp = __nfs4_find_lock_state(state, fl_owner, fl_flock_owner);
1010 if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
1011 ret = -EIO;
1012 else if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
1013 nfs4_stateid_copy(dst, &lsp->ls_stateid);
1014 ret = 0;
1015 }
1016 spin_unlock(&state->state_lock);
1017 nfs4_put_lock_state(lsp);
1018out:
1019 return ret;
1020}
1021
1022bool nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1023{
1024 bool ret;
1025 const nfs4_stateid *src;
1026 int seq;
1027
1028 do {
1029 ret = false;
1030 src = &zero_stateid;
1031 seq = read_seqbegin(&state->seqlock);
1032 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1033 src = &state->open_stateid;
1034 ret = true;
1035 }
1036 nfs4_stateid_copy(dst, src);
1037 } while (read_seqretry(&state->seqlock, seq));
1038 return ret;
1039}
1040
1041/*
1042 * Byte-range lock aware utility to initialize the stateid of read/write
1043 * requests.
1044 */
1045int nfs4_select_rw_stateid(struct nfs4_state *state,
1046 fmode_t fmode, const struct nfs_lock_context *l_ctx,
1047 nfs4_stateid *dst, const struct cred **cred)
1048{
1049 int ret;
1050
1051 if (!nfs4_valid_open_stateid(state))
1052 return -EIO;
1053 if (cred != NULL)
1054 *cred = NULL;
1055 ret = nfs4_copy_lock_stateid(dst, state, l_ctx);
1056 if (ret == -EIO)
1057 /* A lost lock - don't even consider delegations */
1058 goto out;
1059 /* returns true if delegation stateid found and copied */
1060 if (nfs4_copy_delegation_stateid(state->inode, fmode, dst, cred)) {
1061 ret = 0;
1062 goto out;
1063 }
1064 if (ret != -ENOENT)
1065 /* nfs4_copy_delegation_stateid() didn't over-write
1066 * dst, so it still has the lock stateid which we now
1067 * choose to use.
1068 */
1069 goto out;
1070 ret = nfs4_copy_open_stateid(dst, state) ? 0 : -EAGAIN;
1071out:
1072 if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
1073 dst->seqid = 0;
1074 return ret;
1075}
1076
1077struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1078{
1079 struct nfs_seqid *new;
1080
1081 new = kmalloc(sizeof(*new), gfp_mask);
1082 if (new == NULL)
1083 return ERR_PTR(-ENOMEM);
1084 new->sequence = counter;
1085 INIT_LIST_HEAD(&new->list);
1086 new->task = NULL;
1087 return new;
1088}
1089
1090void nfs_release_seqid(struct nfs_seqid *seqid)
1091{
1092 struct nfs_seqid_counter *sequence;
1093
1094 if (seqid == NULL || list_empty(&seqid->list))
1095 return;
1096 sequence = seqid->sequence;
1097 spin_lock(&sequence->lock);
1098 list_del_init(&seqid->list);
1099 if (!list_empty(&sequence->list)) {
1100 struct nfs_seqid *next;
1101
1102 next = list_first_entry(&sequence->list,
1103 struct nfs_seqid, list);
1104 rpc_wake_up_queued_task(&sequence->wait, next->task);
1105 }
1106 spin_unlock(&sequence->lock);
1107}
1108
1109void nfs_free_seqid(struct nfs_seqid *seqid)
1110{
1111 nfs_release_seqid(seqid);
1112 kfree(seqid);
1113}
1114
1115/*
1116 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1117 * failed with a seqid incrementing error -
1118 * see comments nfs4.h:seqid_mutating_error()
1119 */
1120static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1121{
1122 switch (status) {
1123 case 0:
1124 break;
1125 case -NFS4ERR_BAD_SEQID:
1126 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1127 return;
1128 pr_warn_ratelimited("NFS: v4 server returned a bad"
1129 " sequence-id error on an"
1130 " unconfirmed sequence %p!\n",
1131 seqid->sequence);
1132 case -NFS4ERR_STALE_CLIENTID:
1133 case -NFS4ERR_STALE_STATEID:
1134 case -NFS4ERR_BAD_STATEID:
1135 case -NFS4ERR_BADXDR:
1136 case -NFS4ERR_RESOURCE:
1137 case -NFS4ERR_NOFILEHANDLE:
1138 case -NFS4ERR_MOVED:
1139 /* Non-seqid mutating errors */
1140 return;
1141 };
1142 /*
1143 * Note: no locking needed as we are guaranteed to be first
1144 * on the sequence list
1145 */
1146 seqid->sequence->counter++;
1147}
1148
1149void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1150{
1151 struct nfs4_state_owner *sp;
1152
1153 if (seqid == NULL)
1154 return;
1155
1156 sp = container_of(seqid->sequence, struct nfs4_state_owner, so_seqid);
1157 if (status == -NFS4ERR_BAD_SEQID)
1158 nfs4_reset_state_owner(sp);
1159 if (!nfs4_has_session(sp->so_server->nfs_client))
1160 nfs_increment_seqid(status, seqid);
1161}
1162
1163/*
1164 * Increment the seqid if the LOCK/LOCKU succeeded, or
1165 * failed with a seqid incrementing error -
1166 * see comments nfs4.h:seqid_mutating_error()
1167 */
1168void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1169{
1170 if (seqid != NULL)
1171 nfs_increment_seqid(status, seqid);
1172}
1173
1174int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1175{
1176 struct nfs_seqid_counter *sequence;
1177 int status = 0;
1178
1179 if (seqid == NULL)
1180 goto out;
1181 sequence = seqid->sequence;
1182 spin_lock(&sequence->lock);
1183 seqid->task = task;
1184 if (list_empty(&seqid->list))
1185 list_add_tail(&seqid->list, &sequence->list);
1186 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1187 goto unlock;
1188 rpc_sleep_on(&sequence->wait, task, NULL);
1189 status = -EAGAIN;
1190unlock:
1191 spin_unlock(&sequence->lock);
1192out:
1193 return status;
1194}
1195
1196static int nfs4_run_state_manager(void *);
1197
1198static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1199{
1200 smp_mb__before_atomic();
1201 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1202 smp_mb__after_atomic();
1203 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
1204 rpc_wake_up(&clp->cl_rpcwaitq);
1205}
1206
1207/*
1208 * Schedule the nfs_client asynchronous state management routine
1209 */
1210void nfs4_schedule_state_manager(struct nfs_client *clp)
1211{
1212 struct task_struct *task;
1213 char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1214
1215 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
1216 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1217 return;
1218 __module_get(THIS_MODULE);
1219 refcount_inc(&clp->cl_count);
1220
1221 /* The rcu_read_lock() is not strictly necessary, as the state
1222 * manager is the only thread that ever changes the rpc_xprt
1223 * after it's initialized. At this point, we're single threaded. */
1224 rcu_read_lock();
1225 snprintf(buf, sizeof(buf), "%s-manager",
1226 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1227 rcu_read_unlock();
1228 task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
1229 if (IS_ERR(task)) {
1230 printk(KERN_ERR "%s: kthread_run: %ld\n",
1231 __func__, PTR_ERR(task));
1232 if (!nfs_client_init_is_complete(clp))
1233 nfs_mark_client_ready(clp, PTR_ERR(task));
1234 nfs4_clear_state_manager_bit(clp);
1235 nfs_put_client(clp);
1236 module_put(THIS_MODULE);
1237 }
1238}
1239
1240/*
1241 * Schedule a lease recovery attempt
1242 */
1243void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1244{
1245 if (!clp)
1246 return;
1247 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1248 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1249 dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1250 clp->cl_hostname);
1251 nfs4_schedule_state_manager(clp);
1252}
1253EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1254
1255/**
1256 * nfs4_schedule_migration_recovery - trigger migration recovery
1257 *
1258 * @server: FSID that is migrating
1259 *
1260 * Returns zero if recovery has started, otherwise a negative NFS4ERR
1261 * value is returned.
1262 */
1263int nfs4_schedule_migration_recovery(const struct nfs_server *server)
1264{
1265 struct nfs_client *clp = server->nfs_client;
1266
1267 if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
1268 pr_err("NFS: volatile file handles not supported (server %s)\n",
1269 clp->cl_hostname);
1270 return -NFS4ERR_IO;
1271 }
1272
1273 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
1274 return -NFS4ERR_IO;
1275
1276 dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1277 __func__,
1278 (unsigned long long)server->fsid.major,
1279 (unsigned long long)server->fsid.minor,
1280 clp->cl_hostname);
1281
1282 set_bit(NFS_MIG_IN_TRANSITION,
1283 &((struct nfs_server *)server)->mig_status);
1284 set_bit(NFS4CLNT_MOVED, &clp->cl_state);
1285
1286 nfs4_schedule_state_manager(clp);
1287 return 0;
1288}
1289EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
1290
1291/**
1292 * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1293 *
1294 * @clp: server to check for moved leases
1295 *
1296 */
1297void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
1298{
1299 dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1300 __func__, clp->cl_clientid, clp->cl_hostname);
1301
1302 set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
1303 nfs4_schedule_state_manager(clp);
1304}
1305EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
1306
1307int nfs4_wait_clnt_recover(struct nfs_client *clp)
1308{
1309 int res;
1310
1311 might_sleep();
1312
1313 refcount_inc(&clp->cl_count);
1314 res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1315 nfs_wait_bit_killable, TASK_KILLABLE);
1316 if (res)
1317 goto out;
1318 if (clp->cl_cons_state < 0)
1319 res = clp->cl_cons_state;
1320out:
1321 nfs_put_client(clp);
1322 return res;
1323}
1324
1325int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1326{
1327 unsigned int loop;
1328 int ret;
1329
1330 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1331 ret = nfs4_wait_clnt_recover(clp);
1332 if (ret != 0)
1333 break;
1334 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1335 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1336 break;
1337 nfs4_schedule_state_manager(clp);
1338 ret = -EIO;
1339 }
1340 return ret;
1341}
1342
1343/*
1344 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1345 * @clp: client to process
1346 *
1347 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1348 * resend of the SETCLIENTID and hence re-establish the
1349 * callback channel. Then return all existing delegations.
1350 */
1351static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1352{
1353 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1354 nfs_expire_all_delegations(clp);
1355 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1356 clp->cl_hostname);
1357}
1358
1359void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1360{
1361 nfs40_handle_cb_pathdown(clp);
1362 nfs4_schedule_state_manager(clp);
1363}
1364
1365static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1366{
1367
1368 if (!nfs4_valid_open_stateid(state))
1369 return 0;
1370 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1371 /* Don't recover state that expired before the reboot */
1372 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1373 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1374 return 0;
1375 }
1376 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1377 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1378 return 1;
1379}
1380
1381int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1382{
1383 if (!nfs4_valid_open_stateid(state))
1384 return 0;
1385 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1386 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1387 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1388 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1389 return 1;
1390}
1391
1392int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1393{
1394 struct nfs_client *clp = server->nfs_client;
1395
1396 if (!nfs4_state_mark_reclaim_nograce(clp, state))
1397 return -EBADF;
1398 nfs_inode_find_delegation_state_and_recover(state->inode,
1399 &state->stateid);
1400 dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1401 clp->cl_hostname);
1402 nfs4_schedule_state_manager(clp);
1403 return 0;
1404}
1405EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1406
1407static struct nfs4_lock_state *
1408nfs_state_find_lock_state_by_stateid(struct nfs4_state *state,
1409 const nfs4_stateid *stateid)
1410{
1411 struct nfs4_lock_state *pos;
1412
1413 list_for_each_entry(pos, &state->lock_states, ls_locks) {
1414 if (!test_bit(NFS_LOCK_INITIALIZED, &pos->ls_flags))
1415 continue;
1416 if (nfs4_stateid_match_other(&pos->ls_stateid, stateid))
1417 return pos;
1418 }
1419 return NULL;
1420}
1421
1422static bool nfs_state_lock_state_matches_stateid(struct nfs4_state *state,
1423 const nfs4_stateid *stateid)
1424{
1425 bool found = false;
1426
1427 if (test_bit(LK_STATE_IN_USE, &state->flags)) {
1428 spin_lock(&state->state_lock);
1429 if (nfs_state_find_lock_state_by_stateid(state, stateid))
1430 found = true;
1431 spin_unlock(&state->state_lock);
1432 }
1433 return found;
1434}
1435
1436void nfs_inode_find_state_and_recover(struct inode *inode,
1437 const nfs4_stateid *stateid)
1438{
1439 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1440 struct nfs_inode *nfsi = NFS_I(inode);
1441 struct nfs_open_context *ctx;
1442 struct nfs4_state *state;
1443 bool found = false;
1444
1445 rcu_read_lock();
1446 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1447 state = ctx->state;
1448 if (state == NULL)
1449 continue;
1450 if (nfs4_stateid_match_other(&state->stateid, stateid) &&
1451 nfs4_state_mark_reclaim_nograce(clp, state)) {
1452 found = true;
1453 continue;
1454 }
1455 if (nfs4_stateid_match_other(&state->open_stateid, stateid) &&
1456 nfs4_state_mark_reclaim_nograce(clp, state)) {
1457 found = true;
1458 continue;
1459 }
1460 if (nfs_state_lock_state_matches_stateid(state, stateid) &&
1461 nfs4_state_mark_reclaim_nograce(clp, state))
1462 found = true;
1463 }
1464 rcu_read_unlock();
1465
1466 nfs_inode_find_delegation_state_and_recover(inode, stateid);
1467 if (found)
1468 nfs4_schedule_state_manager(clp);
1469}
1470
1471static void nfs4_state_mark_open_context_bad(struct nfs4_state *state, int err)
1472{
1473 struct inode *inode = state->inode;
1474 struct nfs_inode *nfsi = NFS_I(inode);
1475 struct nfs_open_context *ctx;
1476
1477 rcu_read_lock();
1478 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1479 if (ctx->state != state)
1480 continue;
1481 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1482 pr_warn("NFSv4: state recovery failed for open file %pd2, "
1483 "error = %d\n", ctx->dentry, err);
1484 }
1485 rcu_read_unlock();
1486}
1487
1488static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1489{
1490 set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
1491 nfs4_state_mark_open_context_bad(state, error);
1492}
1493
1494
1495static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1496{
1497 struct inode *inode = state->inode;
1498 struct nfs_inode *nfsi = NFS_I(inode);
1499 struct file_lock *fl;
1500 struct nfs4_lock_state *lsp;
1501 int status = 0;
1502 struct file_lock_context *flctx = inode->i_flctx;
1503 struct list_head *list;
1504
1505 if (flctx == NULL)
1506 return 0;
1507
1508 list = &flctx->flc_posix;
1509
1510 /* Guard against delegation returns and new lock/unlock calls */
1511 down_write(&nfsi->rwsem);
1512 spin_lock(&flctx->flc_lock);
1513restart:
1514 list_for_each_entry(fl, list, fl_list) {
1515 if (nfs_file_open_context(fl->fl_file)->state != state)
1516 continue;
1517 spin_unlock(&flctx->flc_lock);
1518 status = ops->recover_lock(state, fl);
1519 switch (status) {
1520 case 0:
1521 break;
1522 case -ETIMEDOUT:
1523 case -ESTALE:
1524 case -NFS4ERR_ADMIN_REVOKED:
1525 case -NFS4ERR_STALE_STATEID:
1526 case -NFS4ERR_BAD_STATEID:
1527 case -NFS4ERR_EXPIRED:
1528 case -NFS4ERR_NO_GRACE:
1529 case -NFS4ERR_STALE_CLIENTID:
1530 case -NFS4ERR_BADSESSION:
1531 case -NFS4ERR_BADSLOT:
1532 case -NFS4ERR_BAD_HIGH_SLOT:
1533 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1534 goto out;
1535 default:
1536 pr_err("NFS: %s: unhandled error %d\n",
1537 __func__, status);
1538 /* Fall through */
1539 case -ENOMEM:
1540 case -NFS4ERR_DENIED:
1541 case -NFS4ERR_RECLAIM_BAD:
1542 case -NFS4ERR_RECLAIM_CONFLICT:
1543 lsp = fl->fl_u.nfs4_fl.owner;
1544 if (lsp)
1545 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1546 status = 0;
1547 }
1548 spin_lock(&flctx->flc_lock);
1549 }
1550 if (list == &flctx->flc_posix) {
1551 list = &flctx->flc_flock;
1552 goto restart;
1553 }
1554 spin_unlock(&flctx->flc_lock);
1555out:
1556 up_write(&nfsi->rwsem);
1557 return status;
1558}
1559
1560#ifdef CONFIG_NFS_V4_2
1561static void nfs42_complete_copies(struct nfs4_state_owner *sp, struct nfs4_state *state)
1562{
1563 struct nfs4_copy_state *copy;
1564
1565 if (!test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags))
1566 return;
1567
1568 spin_lock(&sp->so_server->nfs_client->cl_lock);
1569 list_for_each_entry(copy, &sp->so_server->ss_copies, copies) {
1570 if (!nfs4_stateid_match_other(&state->stateid, &copy->parent_state->stateid))
1571 continue;
1572 copy->flags = 1;
1573 complete(&copy->completion);
1574 break;
1575 }
1576 spin_unlock(&sp->so_server->nfs_client->cl_lock);
1577}
1578#else /* !CONFIG_NFS_V4_2 */
1579static inline void nfs42_complete_copies(struct nfs4_state_owner *sp,
1580 struct nfs4_state *state)
1581{
1582}
1583#endif /* CONFIG_NFS_V4_2 */
1584
1585static int __nfs4_reclaim_open_state(struct nfs4_state_owner *sp, struct nfs4_state *state,
1586 const struct nfs4_state_recovery_ops *ops)
1587{
1588 struct nfs4_lock_state *lock;
1589 int status;
1590
1591 status = ops->recover_open(sp, state);
1592 if (status < 0)
1593 return status;
1594
1595 status = nfs4_reclaim_locks(state, ops);
1596 if (status < 0)
1597 return status;
1598
1599 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1600 spin_lock(&state->state_lock);
1601 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1602 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1603 pr_warn_ratelimited("NFS: %s: Lock reclaim failed!\n", __func__);
1604 }
1605 spin_unlock(&state->state_lock);
1606 }
1607
1608 nfs42_complete_copies(sp, state);
1609 clear_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1610 return status;
1611}
1612
1613static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1614{
1615 struct nfs4_state *state;
1616 unsigned int loop = 0;
1617 int status = 0;
1618
1619 /* Note: we rely on the sp->so_states list being ordered
1620 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1621 * states first.
1622 * This is needed to ensure that the server won't give us any
1623 * read delegations that we have to return if, say, we are
1624 * recovering after a network partition or a reboot from a
1625 * server that doesn't support a grace period.
1626 */
1627 spin_lock(&sp->so_lock);
1628 raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
1629restart:
1630 list_for_each_entry(state, &sp->so_states, open_states) {
1631 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1632 continue;
1633 if (!nfs4_valid_open_stateid(state))
1634 continue;
1635 if (state->state == 0)
1636 continue;
1637 refcount_inc(&state->count);
1638 spin_unlock(&sp->so_lock);
1639 status = __nfs4_reclaim_open_state(sp, state, ops);
1640
1641 switch (status) {
1642 default:
1643 if (status >= 0) {
1644 loop = 0;
1645 break;
1646 }
1647 printk(KERN_ERR "NFS: %s: unhandled error %d\n", __func__, status);
1648 /* Fall through */
1649 case -ENOENT:
1650 case -ENOMEM:
1651 case -EACCES:
1652 case -EROFS:
1653 case -EIO:
1654 case -ESTALE:
1655 /* Open state on this file cannot be recovered */
1656 nfs4_state_mark_recovery_failed(state, status);
1657 break;
1658 case -EAGAIN:
1659 ssleep(1);
1660 if (loop++ < 10) {
1661 set_bit(ops->state_flag_bit, &state->flags);
1662 break;
1663 }
1664 /* Fall through */
1665 case -NFS4ERR_ADMIN_REVOKED:
1666 case -NFS4ERR_STALE_STATEID:
1667 case -NFS4ERR_OLD_STATEID:
1668 case -NFS4ERR_BAD_STATEID:
1669 case -NFS4ERR_RECLAIM_BAD:
1670 case -NFS4ERR_RECLAIM_CONFLICT:
1671 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1672 break;
1673 case -NFS4ERR_EXPIRED:
1674 case -NFS4ERR_NO_GRACE:
1675 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1676 /* Fall through */
1677 case -NFS4ERR_STALE_CLIENTID:
1678 case -NFS4ERR_BADSESSION:
1679 case -NFS4ERR_BADSLOT:
1680 case -NFS4ERR_BAD_HIGH_SLOT:
1681 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1682 case -ETIMEDOUT:
1683 goto out_err;
1684 }
1685 nfs4_put_open_state(state);
1686 spin_lock(&sp->so_lock);
1687 goto restart;
1688 }
1689 raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1690 spin_unlock(&sp->so_lock);
1691 return 0;
1692out_err:
1693 nfs4_put_open_state(state);
1694 spin_lock(&sp->so_lock);
1695 raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1696 spin_unlock(&sp->so_lock);
1697 return status;
1698}
1699
1700static void nfs4_clear_open_state(struct nfs4_state *state)
1701{
1702 struct nfs4_lock_state *lock;
1703
1704 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1705 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1706 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1707 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1708 spin_lock(&state->state_lock);
1709 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1710 lock->ls_seqid.flags = 0;
1711 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1712 }
1713 spin_unlock(&state->state_lock);
1714}
1715
1716static void nfs4_reset_seqids(struct nfs_server *server,
1717 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1718{
1719 struct nfs_client *clp = server->nfs_client;
1720 struct nfs4_state_owner *sp;
1721 struct rb_node *pos;
1722 struct nfs4_state *state;
1723
1724 spin_lock(&clp->cl_lock);
1725 for (pos = rb_first(&server->state_owners);
1726 pos != NULL;
1727 pos = rb_next(pos)) {
1728 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1729 sp->so_seqid.flags = 0;
1730 spin_lock(&sp->so_lock);
1731 list_for_each_entry(state, &sp->so_states, open_states) {
1732 if (mark_reclaim(clp, state))
1733 nfs4_clear_open_state(state);
1734 }
1735 spin_unlock(&sp->so_lock);
1736 }
1737 spin_unlock(&clp->cl_lock);
1738}
1739
1740static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1741 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1742{
1743 struct nfs_server *server;
1744
1745 rcu_read_lock();
1746 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1747 nfs4_reset_seqids(server, mark_reclaim);
1748 rcu_read_unlock();
1749}
1750
1751static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1752{
1753 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1754 /* Mark all delegations for reclaim */
1755 nfs_delegation_mark_reclaim(clp);
1756 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1757}
1758
1759static int nfs4_reclaim_complete(struct nfs_client *clp,
1760 const struct nfs4_state_recovery_ops *ops,
1761 const struct cred *cred)
1762{
1763 /* Notify the server we're done reclaiming our state */
1764 if (ops->reclaim_complete)
1765 return ops->reclaim_complete(clp, cred);
1766 return 0;
1767}
1768
1769static void nfs4_clear_reclaim_server(struct nfs_server *server)
1770{
1771 struct nfs_client *clp = server->nfs_client;
1772 struct nfs4_state_owner *sp;
1773 struct rb_node *pos;
1774 struct nfs4_state *state;
1775
1776 spin_lock(&clp->cl_lock);
1777 for (pos = rb_first(&server->state_owners);
1778 pos != NULL;
1779 pos = rb_next(pos)) {
1780 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1781 spin_lock(&sp->so_lock);
1782 list_for_each_entry(state, &sp->so_states, open_states) {
1783 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1784 &state->flags))
1785 continue;
1786 nfs4_state_mark_reclaim_nograce(clp, state);
1787 }
1788 spin_unlock(&sp->so_lock);
1789 }
1790 spin_unlock(&clp->cl_lock);
1791}
1792
1793static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1794{
1795 struct nfs_server *server;
1796
1797 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1798 return 0;
1799
1800 rcu_read_lock();
1801 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1802 nfs4_clear_reclaim_server(server);
1803 rcu_read_unlock();
1804
1805 nfs_delegation_reap_unclaimed(clp);
1806 return 1;
1807}
1808
1809static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1810{
1811 const struct nfs4_state_recovery_ops *ops;
1812 const struct cred *cred;
1813 int err;
1814
1815 if (!nfs4_state_clear_reclaim_reboot(clp))
1816 return;
1817 ops = clp->cl_mvops->reboot_recovery_ops;
1818 cred = nfs4_get_clid_cred(clp);
1819 err = nfs4_reclaim_complete(clp, ops, cred);
1820 put_cred(cred);
1821 if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
1822 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1823}
1824
1825static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1826{
1827 nfs_mark_test_expired_all_delegations(clp);
1828 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1829}
1830
1831static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1832{
1833 switch (error) {
1834 case 0:
1835 break;
1836 case -NFS4ERR_CB_PATH_DOWN:
1837 nfs40_handle_cb_pathdown(clp);
1838 break;
1839 case -NFS4ERR_NO_GRACE:
1840 nfs4_state_end_reclaim_reboot(clp);
1841 break;
1842 case -NFS4ERR_STALE_CLIENTID:
1843 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1844 nfs4_state_start_reclaim_reboot(clp);
1845 break;
1846 case -NFS4ERR_EXPIRED:
1847 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1848 nfs4_state_start_reclaim_nograce(clp);
1849 break;
1850 case -NFS4ERR_BADSESSION:
1851 case -NFS4ERR_BADSLOT:
1852 case -NFS4ERR_BAD_HIGH_SLOT:
1853 case -NFS4ERR_DEADSESSION:
1854 case -NFS4ERR_SEQ_FALSE_RETRY:
1855 case -NFS4ERR_SEQ_MISORDERED:
1856 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1857 /* Zero session reset errors */
1858 break;
1859 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1860 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1861 break;
1862 default:
1863 dprintk("%s: failed to handle error %d for server %s\n",
1864 __func__, error, clp->cl_hostname);
1865 return error;
1866 }
1867 dprintk("%s: handled error %d for server %s\n", __func__, error,
1868 clp->cl_hostname);
1869 return 0;
1870}
1871
1872static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1873{
1874 struct nfs4_state_owner *sp;
1875 struct nfs_server *server;
1876 struct rb_node *pos;
1877 LIST_HEAD(freeme);
1878 int status = 0;
1879
1880restart:
1881 rcu_read_lock();
1882 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1883 nfs4_purge_state_owners(server, &freeme);
1884 spin_lock(&clp->cl_lock);
1885 for (pos = rb_first(&server->state_owners);
1886 pos != NULL;
1887 pos = rb_next(pos)) {
1888 sp = rb_entry(pos,
1889 struct nfs4_state_owner, so_server_node);
1890 if (!test_and_clear_bit(ops->owner_flag_bit,
1891 &sp->so_flags))
1892 continue;
1893 if (!atomic_inc_not_zero(&sp->so_count))
1894 continue;
1895 spin_unlock(&clp->cl_lock);
1896 rcu_read_unlock();
1897
1898 status = nfs4_reclaim_open_state(sp, ops);
1899 if (status < 0) {
1900 set_bit(ops->owner_flag_bit, &sp->so_flags);
1901 nfs4_put_state_owner(sp);
1902 status = nfs4_recovery_handle_error(clp, status);
1903 nfs4_free_state_owners(&freeme);
1904 return (status != 0) ? status : -EAGAIN;
1905 }
1906
1907 nfs4_put_state_owner(sp);
1908 goto restart;
1909 }
1910 spin_unlock(&clp->cl_lock);
1911 }
1912 rcu_read_unlock();
1913 nfs4_free_state_owners(&freeme);
1914 return 0;
1915}
1916
1917static int nfs4_check_lease(struct nfs_client *clp)
1918{
1919 const struct cred *cred;
1920 const struct nfs4_state_maintenance_ops *ops =
1921 clp->cl_mvops->state_renewal_ops;
1922 int status;
1923
1924 /* Is the client already known to have an expired lease? */
1925 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1926 return 0;
1927 cred = ops->get_state_renewal_cred(clp);
1928 if (cred == NULL) {
1929 cred = nfs4_get_clid_cred(clp);
1930 status = -ENOKEY;
1931 if (cred == NULL)
1932 goto out;
1933 }
1934 status = ops->renew_lease(clp, cred);
1935 put_cred(cred);
1936 if (status == -ETIMEDOUT) {
1937 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1938 return 0;
1939 }
1940out:
1941 return nfs4_recovery_handle_error(clp, status);
1942}
1943
1944/* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1945 * and for recoverable errors on EXCHANGE_ID for v4.1
1946 */
1947static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1948{
1949 switch (status) {
1950 case -NFS4ERR_SEQ_MISORDERED:
1951 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1952 return -ESERVERFAULT;
1953 /* Lease confirmation error: retry after purging the lease */
1954 ssleep(1);
1955 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1956 break;
1957 case -NFS4ERR_STALE_CLIENTID:
1958 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1959 nfs4_state_start_reclaim_reboot(clp);
1960 break;
1961 case -NFS4ERR_CLID_INUSE:
1962 pr_err("NFS: Server %s reports our clientid is in use\n",
1963 clp->cl_hostname);
1964 nfs_mark_client_ready(clp, -EPERM);
1965 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1966 return -EPERM;
1967 case -EACCES:
1968 case -NFS4ERR_DELAY:
1969 case -EAGAIN:
1970 ssleep(1);
1971 break;
1972
1973 case -NFS4ERR_MINOR_VERS_MISMATCH:
1974 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1975 nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1976 dprintk("%s: exit with error %d for server %s\n",
1977 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1978 return -EPROTONOSUPPORT;
1979 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1980 * in nfs4_exchange_id */
1981 default:
1982 dprintk("%s: exit with error %d for server %s\n", __func__,
1983 status, clp->cl_hostname);
1984 return status;
1985 }
1986 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1987 dprintk("%s: handled error %d for server %s\n", __func__, status,
1988 clp->cl_hostname);
1989 return 0;
1990}
1991
1992static int nfs4_establish_lease(struct nfs_client *clp)
1993{
1994 const struct cred *cred;
1995 const struct nfs4_state_recovery_ops *ops =
1996 clp->cl_mvops->reboot_recovery_ops;
1997 int status;
1998
1999 status = nfs4_begin_drain_session(clp);
2000 if (status != 0)
2001 return status;
2002 cred = nfs4_get_clid_cred(clp);
2003 if (cred == NULL)
2004 return -ENOENT;
2005 status = ops->establish_clid(clp, cred);
2006 put_cred(cred);
2007 if (status != 0)
2008 return status;
2009 pnfs_destroy_all_layouts(clp);
2010 return 0;
2011}
2012
2013/*
2014 * Returns zero or a negative errno. NFS4ERR values are converted
2015 * to local errno values.
2016 */
2017static int nfs4_reclaim_lease(struct nfs_client *clp)
2018{
2019 int status;
2020
2021 status = nfs4_establish_lease(clp);
2022 if (status < 0)
2023 return nfs4_handle_reclaim_lease_error(clp, status);
2024 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
2025 nfs4_state_start_reclaim_nograce(clp);
2026 if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2027 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
2028 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2029 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2030 return 0;
2031}
2032
2033static int nfs4_purge_lease(struct nfs_client *clp)
2034{
2035 int status;
2036
2037 status = nfs4_establish_lease(clp);
2038 if (status < 0)
2039 return nfs4_handle_reclaim_lease_error(clp, status);
2040 clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2041 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2042 nfs4_state_start_reclaim_nograce(clp);
2043 return 0;
2044}
2045
2046/*
2047 * Try remote migration of one FSID from a source server to a
2048 * destination server. The source server provides a list of
2049 * potential destinations.
2050 *
2051 * Returns zero or a negative NFS4ERR status code.
2052 */
2053static int nfs4_try_migration(struct nfs_server *server, const struct cred *cred)
2054{
2055 struct nfs_client *clp = server->nfs_client;
2056 struct nfs4_fs_locations *locations = NULL;
2057 struct inode *inode;
2058 struct page *page;
2059 int status, result;
2060
2061 dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
2062 (unsigned long long)server->fsid.major,
2063 (unsigned long long)server->fsid.minor,
2064 clp->cl_hostname);
2065
2066 result = 0;
2067 page = alloc_page(GFP_KERNEL);
2068 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2069 if (page == NULL || locations == NULL) {
2070 dprintk("<-- %s: no memory\n", __func__);
2071 goto out;
2072 }
2073
2074 inode = d_inode(server->super->s_root);
2075 result = nfs4_proc_get_locations(inode, locations, page, cred);
2076 if (result) {
2077 dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2078 __func__, result);
2079 goto out;
2080 }
2081
2082 result = -NFS4ERR_NXIO;
2083 if (!locations->nlocations)
2084 goto out;
2085
2086 if (!(locations->fattr.valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
2087 dprintk("<-- %s: No fs_locations data, migration skipped\n",
2088 __func__);
2089 goto out;
2090 }
2091
2092 status = nfs4_begin_drain_session(clp);
2093 if (status != 0) {
2094 result = status;
2095 goto out;
2096 }
2097
2098 status = nfs4_replace_transport(server, locations);
2099 if (status != 0) {
2100 dprintk("<-- %s: failed to replace transport: %d\n",
2101 __func__, status);
2102 goto out;
2103 }
2104
2105 result = 0;
2106 dprintk("<-- %s: migration succeeded\n", __func__);
2107
2108out:
2109 if (page != NULL)
2110 __free_page(page);
2111 kfree(locations);
2112 if (result) {
2113 pr_err("NFS: migration recovery failed (server %s)\n",
2114 clp->cl_hostname);
2115 set_bit(NFS_MIG_FAILED, &server->mig_status);
2116 }
2117 return result;
2118}
2119
2120/*
2121 * Returns zero or a negative NFS4ERR status code.
2122 */
2123static int nfs4_handle_migration(struct nfs_client *clp)
2124{
2125 const struct nfs4_state_maintenance_ops *ops =
2126 clp->cl_mvops->state_renewal_ops;
2127 struct nfs_server *server;
2128 const struct cred *cred;
2129
2130 dprintk("%s: migration reported on \"%s\"\n", __func__,
2131 clp->cl_hostname);
2132
2133 cred = ops->get_state_renewal_cred(clp);
2134 if (cred == NULL)
2135 return -NFS4ERR_NOENT;
2136
2137 clp->cl_mig_gen++;
2138restart:
2139 rcu_read_lock();
2140 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2141 int status;
2142
2143 if (server->mig_gen == clp->cl_mig_gen)
2144 continue;
2145 server->mig_gen = clp->cl_mig_gen;
2146
2147 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
2148 &server->mig_status))
2149 continue;
2150
2151 rcu_read_unlock();
2152 status = nfs4_try_migration(server, cred);
2153 if (status < 0) {
2154 put_cred(cred);
2155 return status;
2156 }
2157 goto restart;
2158 }
2159 rcu_read_unlock();
2160 put_cred(cred);
2161 return 0;
2162}
2163
2164/*
2165 * Test each nfs_server on the clp's cl_superblocks list to see
2166 * if it's moved to another server. Stop when the server no longer
2167 * returns NFS4ERR_LEASE_MOVED.
2168 */
2169static int nfs4_handle_lease_moved(struct nfs_client *clp)
2170{
2171 const struct nfs4_state_maintenance_ops *ops =
2172 clp->cl_mvops->state_renewal_ops;
2173 struct nfs_server *server;
2174 const struct cred *cred;
2175
2176 dprintk("%s: lease moved reported on \"%s\"\n", __func__,
2177 clp->cl_hostname);
2178
2179 cred = ops->get_state_renewal_cred(clp);
2180 if (cred == NULL)
2181 return -NFS4ERR_NOENT;
2182
2183 clp->cl_mig_gen++;
2184restart:
2185 rcu_read_lock();
2186 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2187 struct inode *inode;
2188 int status;
2189
2190 if (server->mig_gen == clp->cl_mig_gen)
2191 continue;
2192 server->mig_gen = clp->cl_mig_gen;
2193
2194 rcu_read_unlock();
2195
2196 inode = d_inode(server->super->s_root);
2197 status = nfs4_proc_fsid_present(inode, cred);
2198 if (status != -NFS4ERR_MOVED)
2199 goto restart; /* wasn't this one */
2200 if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
2201 goto restart; /* there are more */
2202 goto out;
2203 }
2204 rcu_read_unlock();
2205
2206out:
2207 put_cred(cred);
2208 return 0;
2209}
2210
2211/**
2212 * nfs4_discover_server_trunking - Detect server IP address trunking
2213 *
2214 * @clp: nfs_client under test
2215 * @result: OUT: found nfs_client, or clp
2216 *
2217 * Returns zero or a negative errno. If zero is returned,
2218 * an nfs_client pointer is planted in "result".
2219 *
2220 * Note: since we are invoked in process context, and
2221 * not from inside the state manager, we cannot use
2222 * nfs4_handle_reclaim_lease_error().
2223 */
2224int nfs4_discover_server_trunking(struct nfs_client *clp,
2225 struct nfs_client **result)
2226{
2227 const struct nfs4_state_recovery_ops *ops =
2228 clp->cl_mvops->reboot_recovery_ops;
2229 struct rpc_clnt *clnt;
2230 const struct cred *cred;
2231 int i, status;
2232
2233 dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
2234
2235 clnt = clp->cl_rpcclient;
2236 i = 0;
2237
2238 mutex_lock(&nfs_clid_init_mutex);
2239again:
2240 status = -ENOENT;
2241 cred = nfs4_get_clid_cred(clp);
2242 if (cred == NULL)
2243 goto out_unlock;
2244
2245 status = ops->detect_trunking(clp, result, cred);
2246 put_cred(cred);
2247 switch (status) {
2248 case 0:
2249 case -EINTR:
2250 case -ERESTARTSYS:
2251 break;
2252 case -ETIMEDOUT:
2253 if (clnt->cl_softrtry)
2254 break;
2255 /* Fall through */
2256 case -NFS4ERR_DELAY:
2257 case -EAGAIN:
2258 ssleep(1);
2259 /* Fall through */
2260 case -NFS4ERR_STALE_CLIENTID:
2261 dprintk("NFS: %s after status %d, retrying\n",
2262 __func__, status);
2263 goto again;
2264 case -EACCES:
2265 if (i++ == 0) {
2266 nfs4_root_machine_cred(clp);
2267 goto again;
2268 }
2269 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
2270 break;
2271 /* Fall through */
2272 case -NFS4ERR_CLID_INUSE:
2273 case -NFS4ERR_WRONGSEC:
2274 /* No point in retrying if we already used RPC_AUTH_UNIX */
2275 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
2276 status = -EPERM;
2277 break;
2278 }
2279 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
2280 if (IS_ERR(clnt)) {
2281 status = PTR_ERR(clnt);
2282 break;
2283 }
2284 /* Note: this is safe because we haven't yet marked the
2285 * client as ready, so we are the only user of
2286 * clp->cl_rpcclient
2287 */
2288 clnt = xchg(&clp->cl_rpcclient, clnt);
2289 rpc_shutdown_client(clnt);
2290 clnt = clp->cl_rpcclient;
2291 goto again;
2292
2293 case -NFS4ERR_MINOR_VERS_MISMATCH:
2294 status = -EPROTONOSUPPORT;
2295 break;
2296
2297 case -EKEYEXPIRED:
2298 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2299 * in nfs4_exchange_id */
2300 status = -EKEYEXPIRED;
2301 break;
2302 default:
2303 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2304 __func__, status);
2305 status = -EIO;
2306 }
2307
2308out_unlock:
2309 mutex_unlock(&nfs_clid_init_mutex);
2310 dprintk("NFS: %s: status = %d\n", __func__, status);
2311 return status;
2312}
2313
2314#ifdef CONFIG_NFS_V4_1
2315void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
2316{
2317 struct nfs_client *clp = session->clp;
2318
2319 switch (err) {
2320 default:
2321 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2322 break;
2323 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2324 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2325 }
2326 nfs4_schedule_state_manager(clp);
2327}
2328EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
2329
2330void nfs41_notify_server(struct nfs_client *clp)
2331{
2332 /* Use CHECK_LEASE to ping the server with a SEQUENCE */
2333 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2334 nfs4_schedule_state_manager(clp);
2335}
2336
2337static void nfs4_reset_all_state(struct nfs_client *clp)
2338{
2339 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2340 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2341 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2342 nfs4_state_start_reclaim_nograce(clp);
2343 dprintk("%s: scheduling reset of all state for server %s!\n",
2344 __func__, clp->cl_hostname);
2345 nfs4_schedule_state_manager(clp);
2346 }
2347}
2348
2349static void nfs41_handle_server_reboot(struct nfs_client *clp)
2350{
2351 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2352 nfs4_state_start_reclaim_reboot(clp);
2353 dprintk("%s: server %s rebooted!\n", __func__,
2354 clp->cl_hostname);
2355 nfs4_schedule_state_manager(clp);
2356 }
2357}
2358
2359static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
2360{
2361 nfs4_reset_all_state(clp);
2362 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2363}
2364
2365static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
2366{
2367 nfs4_state_start_reclaim_nograce(clp);
2368 nfs4_schedule_state_manager(clp);
2369
2370 dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2371}
2372
2373static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
2374{
2375 /* FIXME: For now, we destroy all layouts. */
2376 pnfs_destroy_all_layouts(clp);
2377 nfs_test_expired_all_delegations(clp);
2378 dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2379 clp->cl_hostname);
2380}
2381
2382static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
2383{
2384 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2385 nfs4_schedule_state_manager(clp);
2386
2387 dprintk("%s: server %s declared a backchannel fault\n", __func__,
2388 clp->cl_hostname);
2389}
2390
2391static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2392{
2393 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2394 &clp->cl_state) == 0)
2395 nfs4_schedule_state_manager(clp);
2396}
2397
2398void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
2399 bool recovery)
2400{
2401 if (!flags)
2402 return;
2403
2404 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2405 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2406 /*
2407 * If we're called from the state manager thread, then assume we're
2408 * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2409 * Those flags are expected to remain set until we're done
2410 * recovering (see RFC5661, section 18.46.3).
2411 */
2412 if (recovery)
2413 goto out_recovery;
2414
2415 if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2416 nfs41_handle_server_reboot(clp);
2417 if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
2418 nfs41_handle_all_state_revoked(clp);
2419 if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2420 SEQ4_STATUS_ADMIN_STATE_REVOKED))
2421 nfs41_handle_some_state_revoked(clp);
2422 if (flags & SEQ4_STATUS_LEASE_MOVED)
2423 nfs4_schedule_lease_moved_recovery(clp);
2424 if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2425 nfs41_handle_recallable_state_revoked(clp);
2426out_recovery:
2427 if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2428 nfs41_handle_backchannel_fault(clp);
2429 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2430 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2431 nfs41_handle_cb_path_down(clp);
2432}
2433
2434static int nfs4_reset_session(struct nfs_client *clp)
2435{
2436 const struct cred *cred;
2437 int status;
2438
2439 if (!nfs4_has_session(clp))
2440 return 0;
2441 status = nfs4_begin_drain_session(clp);
2442 if (status != 0)
2443 return status;
2444 cred = nfs4_get_clid_cred(clp);
2445 status = nfs4_proc_destroy_session(clp->cl_session, cred);
2446 switch (status) {
2447 case 0:
2448 case -NFS4ERR_BADSESSION:
2449 case -NFS4ERR_DEADSESSION:
2450 break;
2451 case -NFS4ERR_BACK_CHAN_BUSY:
2452 case -NFS4ERR_DELAY:
2453 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2454 status = 0;
2455 ssleep(1);
2456 goto out;
2457 default:
2458 status = nfs4_recovery_handle_error(clp, status);
2459 goto out;
2460 }
2461
2462 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2463 status = nfs4_proc_create_session(clp, cred);
2464 if (status) {
2465 dprintk("%s: session reset failed with status %d for server %s!\n",
2466 __func__, status, clp->cl_hostname);
2467 status = nfs4_handle_reclaim_lease_error(clp, status);
2468 goto out;
2469 }
2470 nfs41_finish_session_reset(clp);
2471 dprintk("%s: session reset was successful for server %s!\n",
2472 __func__, clp->cl_hostname);
2473out:
2474 put_cred(cred);
2475 return status;
2476}
2477
2478static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2479{
2480 const struct cred *cred;
2481 int ret;
2482
2483 if (!nfs4_has_session(clp))
2484 return 0;
2485 ret = nfs4_begin_drain_session(clp);
2486 if (ret != 0)
2487 return ret;
2488 cred = nfs4_get_clid_cred(clp);
2489 ret = nfs4_proc_bind_conn_to_session(clp, cred);
2490 put_cred(cred);
2491 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2492 switch (ret) {
2493 case 0:
2494 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2495 __func__, clp->cl_hostname);
2496 break;
2497 case -NFS4ERR_DELAY:
2498 ssleep(1);
2499 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2500 break;
2501 default:
2502 return nfs4_recovery_handle_error(clp, ret);
2503 }
2504 return 0;
2505}
2506#else /* CONFIG_NFS_V4_1 */
2507static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2508
2509static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2510{
2511 return 0;
2512}
2513#endif /* CONFIG_NFS_V4_1 */
2514
2515static void nfs4_state_manager(struct nfs_client *clp)
2516{
2517 unsigned int memflags;
2518 int status = 0;
2519 const char *section = "", *section_sep = "";
2520
2521 /*
2522 * State recovery can deadlock if the direct reclaim code tries
2523 * start NFS writeback. So ensure memory allocations are all
2524 * GFP_NOFS.
2525 */
2526 memflags = memalloc_nofs_save();
2527
2528 /* Ensure exclusive access to NFSv4 state */
2529 do {
2530 trace_nfs4_state_mgr(clp);
2531 clear_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2532 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2533 section = "purge state";
2534 status = nfs4_purge_lease(clp);
2535 if (status < 0)
2536 goto out_error;
2537 continue;
2538 }
2539
2540 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2541 section = "lease expired";
2542 /* We're going to have to re-establish a clientid */
2543 status = nfs4_reclaim_lease(clp);
2544 if (status < 0)
2545 goto out_error;
2546 continue;
2547 }
2548
2549 /* Initialize or reset the session */
2550 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2551 section = "reset session";
2552 status = nfs4_reset_session(clp);
2553 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2554 continue;
2555 if (status < 0)
2556 goto out_error;
2557 }
2558
2559 /* Send BIND_CONN_TO_SESSION */
2560 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2561 &clp->cl_state)) {
2562 section = "bind conn to session";
2563 status = nfs4_bind_conn_to_session(clp);
2564 if (status < 0)
2565 goto out_error;
2566 continue;
2567 }
2568
2569 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2570 section = "check lease";
2571 status = nfs4_check_lease(clp);
2572 if (status < 0)
2573 goto out_error;
2574 continue;
2575 }
2576
2577 if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
2578 section = "migration";
2579 status = nfs4_handle_migration(clp);
2580 if (status < 0)
2581 goto out_error;
2582 }
2583
2584 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
2585 section = "lease moved";
2586 status = nfs4_handle_lease_moved(clp);
2587 if (status < 0)
2588 goto out_error;
2589 }
2590
2591 /* First recover reboot state... */
2592 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2593 section = "reclaim reboot";
2594 status = nfs4_do_reclaim(clp,
2595 clp->cl_mvops->reboot_recovery_ops);
2596 if (status == -EAGAIN)
2597 continue;
2598 if (status < 0)
2599 goto out_error;
2600 nfs4_state_end_reclaim_reboot(clp);
2601 continue;
2602 }
2603
2604 /* Detect expired delegations... */
2605 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
2606 section = "detect expired delegations";
2607 nfs_reap_expired_delegations(clp);
2608 continue;
2609 }
2610
2611 /* Now recover expired state... */
2612 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2613 section = "reclaim nograce";
2614 status = nfs4_do_reclaim(clp,
2615 clp->cl_mvops->nograce_recovery_ops);
2616 if (status == -EAGAIN)
2617 continue;
2618 if (status < 0)
2619 goto out_error;
2620 clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
2621 }
2622
2623 memalloc_nofs_restore(memflags);
2624 nfs4_end_drain_session(clp);
2625 nfs4_clear_state_manager_bit(clp);
2626
2627 if (test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state) &&
2628 !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING,
2629 &clp->cl_state)) {
2630 memflags = memalloc_nofs_save();
2631 continue;
2632 }
2633
2634 if (!test_and_set_bit(NFS4CLNT_DELEGRETURN_RUNNING, &clp->cl_state)) {
2635 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2636 nfs_client_return_marked_delegations(clp);
2637 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2638 }
2639 clear_bit(NFS4CLNT_DELEGRETURN_RUNNING, &clp->cl_state);
2640 }
2641
2642 /* Did we race with an attempt to give us more work? */
2643 if (!test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state))
2644 return;
2645 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2646 return;
2647 memflags = memalloc_nofs_save();
2648 } while (refcount_read(&clp->cl_count) > 1 && !signalled());
2649 goto out_drain;
2650
2651out_error:
2652 if (strlen(section))
2653 section_sep = ": ";
2654 trace_nfs4_state_mgr_failed(clp, section, status);
2655 pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2656 " with error %d\n", section_sep, section,
2657 clp->cl_hostname, -status);
2658 ssleep(1);
2659out_drain:
2660 memalloc_nofs_restore(memflags);
2661 nfs4_end_drain_session(clp);
2662 nfs4_clear_state_manager_bit(clp);
2663}
2664
2665static int nfs4_run_state_manager(void *ptr)
2666{
2667 struct nfs_client *clp = ptr;
2668
2669 allow_signal(SIGKILL);
2670 nfs4_state_manager(clp);
2671 nfs_put_client(clp);
2672 module_put_and_exit(0);
2673 return 0;
2674}
2675
2676/*
2677 * Local variables:
2678 * c-basic-offset: 8
2679 * End:
2680 */