xref: /linux/fs/nfs/nfs4state.c (revision 8bc7c5e525584903ea83332e18a2118ed3b1985e)
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 
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 
70 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp);
71 
72 const nfs4_stateid zero_stateid = {
73 	{ .data = { 0 } },
74 	.type = NFS4_SPECIAL_STATEID_TYPE,
75 };
76 const 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 
84 const 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 
92 static DEFINE_MUTEX(nfs_clid_init_mutex);
93 
94 static 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 
113 int 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);
135 do_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);
141 out:
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  */
158 int 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 	}
190 out:
191 	return status;
192 }
193 
194 const struct cred *nfs4_get_machine_cred(struct nfs_client *clp)
195 {
196 	return get_cred(rpc_machine_cred());
197 }
198 
199 static 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 
207 static const struct cred *
208 nfs4_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  */
233 const 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 
253 out:
254 	return cred;
255 }
256 
257 static 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 
266 static 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 
281 static 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 
294 static 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 
312 static 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 
321 int 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);
331 do_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);
339 out:
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  */
356 int 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  */
398 const 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 
406 static struct nfs4_state_owner *
407 nfs4_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 
433 static struct nfs4_state_owner *
434 nfs4_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 
463 static void
464 nfs4_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 
472 static void
473 nfs4_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 
483 static void
484 nfs4_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  */
494 static struct nfs4_state_owner *
495 nfs4_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_alloc(&server->openowner_id, gfp_flags);
505 	if (sp->so_seqid.owner_id < 0) {
506 		kfree(sp);
507 		return NULL;
508 	}
509 	sp->so_server = server;
510 	sp->so_cred = get_cred(cred);
511 	spin_lock_init(&sp->so_lock);
512 	INIT_LIST_HEAD(&sp->so_states);
513 	nfs4_init_seqid_counter(&sp->so_seqid);
514 	atomic_set(&sp->so_count, 1);
515 	INIT_LIST_HEAD(&sp->so_lru);
516 	mutex_init(&sp->so_delegreturn_mutex);
517 	return sp;
518 }
519 
520 static void
521 nfs4_reset_state_owner(struct nfs4_state_owner *sp)
522 {
523 	/* This state_owner is no longer usable, but must
524 	 * remain in place so that state recovery can find it
525 	 * and the opens associated with it.
526 	 * It may also be used for new 'open' request to
527 	 * return a delegation to the server.
528 	 * So update the 'create_time' so that it looks like
529 	 * a new state_owner.  This will cause the server to
530 	 * request an OPEN_CONFIRM to start a new sequence.
531 	 */
532 	sp->so_seqid.create_time = ktime_get();
533 }
534 
535 static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
536 {
537 	nfs4_destroy_seqid_counter(&sp->so_seqid);
538 	put_cred(sp->so_cred);
539 	ida_free(&sp->so_server->openowner_id, sp->so_seqid.owner_id);
540 	kfree(sp);
541 }
542 
543 static void nfs4_gc_state_owners(struct nfs_server *server)
544 {
545 	struct nfs_client *clp = server->nfs_client;
546 	struct nfs4_state_owner *sp, *tmp;
547 	unsigned long time_min, time_max;
548 	LIST_HEAD(doomed);
549 
550 	spin_lock(&clp->cl_lock);
551 	time_max = jiffies;
552 	time_min = (long)time_max - (long)clp->cl_lease_time;
553 	list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
554 		/* NB: LRU is sorted so that oldest is at the head */
555 		if (time_in_range(sp->so_expires, time_min, time_max))
556 			break;
557 		list_move(&sp->so_lru, &doomed);
558 		nfs4_remove_state_owner_locked(sp);
559 	}
560 	spin_unlock(&clp->cl_lock);
561 
562 	list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
563 		list_del(&sp->so_lru);
564 		nfs4_free_state_owner(sp);
565 	}
566 }
567 
568 /**
569  * nfs4_get_state_owner - Look up a state owner given a credential
570  * @server: nfs_server to search
571  * @cred: RPC credential to match
572  * @gfp_flags: allocation mode
573  *
574  * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
575  */
576 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
577 					      const struct cred *cred,
578 					      gfp_t gfp_flags)
579 {
580 	struct nfs_client *clp = server->nfs_client;
581 	struct nfs4_state_owner *sp, *new;
582 
583 	spin_lock(&clp->cl_lock);
584 	sp = nfs4_find_state_owner_locked(server, cred);
585 	spin_unlock(&clp->cl_lock);
586 	if (sp != NULL)
587 		goto out;
588 	new = nfs4_alloc_state_owner(server, cred, gfp_flags);
589 	if (new == NULL)
590 		goto out;
591 	spin_lock(&clp->cl_lock);
592 	sp = nfs4_insert_state_owner_locked(new);
593 	spin_unlock(&clp->cl_lock);
594 	if (sp != new)
595 		nfs4_free_state_owner(new);
596 out:
597 	nfs4_gc_state_owners(server);
598 	return sp;
599 }
600 
601 /**
602  * nfs4_put_state_owner - Release a nfs4_state_owner
603  * @sp: state owner data to release
604  *
605  * Note that we keep released state owners on an LRU
606  * list.
607  * This caches valid state owners so that they can be
608  * reused, to avoid the OPEN_CONFIRM on minor version 0.
609  * It also pins the uniquifier of dropped state owners for
610  * a while, to ensure that those state owner names are
611  * never reused.
612  */
613 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
614 {
615 	struct nfs_server *server = sp->so_server;
616 	struct nfs_client *clp = server->nfs_client;
617 
618 	if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
619 		return;
620 
621 	sp->so_expires = jiffies;
622 	list_add_tail(&sp->so_lru, &server->state_owners_lru);
623 	spin_unlock(&clp->cl_lock);
624 }
625 
626 /**
627  * nfs4_purge_state_owners - Release all cached state owners
628  * @server: nfs_server with cached state owners to release
629  * @head: resulting list of state owners
630  *
631  * Called at umount time.  Remaining state owners will be on
632  * the LRU with ref count of zero.
633  * Note that the state owners are not freed, but are added
634  * to the list @head, which can later be used as an argument
635  * to nfs4_free_state_owners.
636  */
637 void nfs4_purge_state_owners(struct nfs_server *server, struct list_head *head)
638 {
639 	struct nfs_client *clp = server->nfs_client;
640 	struct nfs4_state_owner *sp, *tmp;
641 
642 	spin_lock(&clp->cl_lock);
643 	list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
644 		list_move(&sp->so_lru, head);
645 		nfs4_remove_state_owner_locked(sp);
646 	}
647 	spin_unlock(&clp->cl_lock);
648 }
649 
650 /**
651  * nfs4_free_state_owners - Release all cached state owners
652  * @head: resulting list of state owners
653  *
654  * Frees a list of state owners that was generated by
655  * nfs4_purge_state_owners
656  */
657 void nfs4_free_state_owners(struct list_head *head)
658 {
659 	struct nfs4_state_owner *sp, *tmp;
660 
661 	list_for_each_entry_safe(sp, tmp, head, so_lru) {
662 		list_del(&sp->so_lru);
663 		nfs4_free_state_owner(sp);
664 	}
665 }
666 
667 static struct nfs4_state *
668 nfs4_alloc_open_state(void)
669 {
670 	struct nfs4_state *state;
671 
672 	state = kzalloc(sizeof(*state), GFP_KERNEL_ACCOUNT);
673 	if (!state)
674 		return NULL;
675 	refcount_set(&state->count, 1);
676 	INIT_LIST_HEAD(&state->lock_states);
677 	spin_lock_init(&state->state_lock);
678 	seqlock_init(&state->seqlock);
679 	init_waitqueue_head(&state->waitq);
680 	return state;
681 }
682 
683 void
684 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
685 {
686 	if (state->state == fmode)
687 		return;
688 	/* NB! List reordering - see the reclaim code for why.  */
689 	if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
690 		if (fmode & FMODE_WRITE)
691 			list_move(&state->open_states, &state->owner->so_states);
692 		else
693 			list_move_tail(&state->open_states, &state->owner->so_states);
694 	}
695 	state->state = fmode;
696 }
697 
698 static struct nfs4_state *
699 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
700 {
701 	struct nfs_inode *nfsi = NFS_I(inode);
702 	struct nfs4_state *state;
703 
704 	list_for_each_entry_rcu(state, &nfsi->open_states, inode_states) {
705 		if (state->owner != owner)
706 			continue;
707 		if (!nfs4_valid_open_stateid(state))
708 			continue;
709 		if (refcount_inc_not_zero(&state->count))
710 			return state;
711 	}
712 	return NULL;
713 }
714 
715 static void
716 nfs4_free_open_state(struct nfs4_state *state)
717 {
718 	kfree_rcu(state, rcu_head);
719 }
720 
721 struct nfs4_state *
722 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
723 {
724 	struct nfs4_state *state, *new;
725 	struct nfs_inode *nfsi = NFS_I(inode);
726 
727 	rcu_read_lock();
728 	state = __nfs4_find_state_byowner(inode, owner);
729 	rcu_read_unlock();
730 	if (state)
731 		goto out;
732 	new = nfs4_alloc_open_state();
733 	spin_lock(&owner->so_lock);
734 	spin_lock(&inode->i_lock);
735 	state = __nfs4_find_state_byowner(inode, owner);
736 	if (state == NULL && new != NULL) {
737 		state = new;
738 		state->owner = owner;
739 		atomic_inc(&owner->so_count);
740 		ihold(inode);
741 		state->inode = inode;
742 		list_add_rcu(&state->inode_states, &nfsi->open_states);
743 		spin_unlock(&inode->i_lock);
744 		/* Note: The reclaim code dictates that we add stateless
745 		 * and read-only stateids to the end of the list */
746 		list_add_tail(&state->open_states, &owner->so_states);
747 		spin_unlock(&owner->so_lock);
748 	} else {
749 		spin_unlock(&inode->i_lock);
750 		spin_unlock(&owner->so_lock);
751 		if (new)
752 			nfs4_free_open_state(new);
753 	}
754 out:
755 	return state;
756 }
757 
758 void nfs4_put_open_state(struct nfs4_state *state)
759 {
760 	struct inode *inode = state->inode;
761 	struct nfs4_state_owner *owner = state->owner;
762 
763 	if (!refcount_dec_and_lock(&state->count, &owner->so_lock))
764 		return;
765 	spin_lock(&inode->i_lock);
766 	list_del_rcu(&state->inode_states);
767 	list_del(&state->open_states);
768 	spin_unlock(&inode->i_lock);
769 	spin_unlock(&owner->so_lock);
770 	nfs4_inode_return_delegation_on_close(inode);
771 	iput(inode);
772 	nfs4_free_open_state(state);
773 	nfs4_put_state_owner(owner);
774 }
775 
776 /*
777  * Close the current file.
778  */
779 static void __nfs4_close(struct nfs4_state *state,
780 		fmode_t fmode, gfp_t gfp_mask, int wait)
781 {
782 	struct nfs4_state_owner *owner = state->owner;
783 	int call_close = 0;
784 	fmode_t newstate;
785 
786 	atomic_inc(&owner->so_count);
787 	/* Protect against nfs4_find_state() */
788 	spin_lock(&owner->so_lock);
789 	switch (fmode & (FMODE_READ | FMODE_WRITE)) {
790 		case FMODE_READ:
791 			state->n_rdonly--;
792 			break;
793 		case FMODE_WRITE:
794 			state->n_wronly--;
795 			break;
796 		case FMODE_READ|FMODE_WRITE:
797 			state->n_rdwr--;
798 	}
799 	newstate = FMODE_READ|FMODE_WRITE;
800 	if (state->n_rdwr == 0) {
801 		if (state->n_rdonly == 0) {
802 			newstate &= ~FMODE_READ;
803 			call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
804 			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
805 		}
806 		if (state->n_wronly == 0) {
807 			newstate &= ~FMODE_WRITE;
808 			call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
809 			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
810 		}
811 		if (newstate == 0)
812 			clear_bit(NFS_DELEGATED_STATE, &state->flags);
813 	}
814 	nfs4_state_set_mode_locked(state, newstate);
815 	spin_unlock(&owner->so_lock);
816 
817 	if (!call_close) {
818 		nfs4_put_open_state(state);
819 		nfs4_put_state_owner(owner);
820 	} else
821 		nfs4_do_close(state, gfp_mask, wait);
822 }
823 
824 void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
825 {
826 	__nfs4_close(state, fmode, GFP_KERNEL, 0);
827 }
828 
829 void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
830 {
831 	__nfs4_close(state, fmode, GFP_KERNEL, 1);
832 }
833 
834 /*
835  * Search the state->lock_states for an existing lock_owner
836  * that is compatible with either of the given owners.
837  * If the second is non-zero, then the first refers to a Posix-lock
838  * owner (current->files) and the second refers to a flock/OFD
839  * owner (struct file*).  In that case, prefer a match for the first
840  * owner.
841  * If both sorts of locks are held on the one file we cannot know
842  * which stateid was intended to be used, so a "correct" choice cannot
843  * be made.  Failing that, a "consistent" choice is preferable.  The
844  * consistent choice we make is to prefer the first owner, that of a
845  * Posix lock.
846  */
847 static struct nfs4_lock_state *
848 __nfs4_find_lock_state(struct nfs4_state *state,
849 		       fl_owner_t owner, fl_owner_t owner2)
850 {
851 	struct nfs4_lock_state *pos, *ret = NULL;
852 	list_for_each_entry(pos, &state->lock_states, ls_locks) {
853 		if (pos->ls_owner == owner) {
854 			ret = pos;
855 			break;
856 		}
857 		if (pos->ls_owner == owner2)
858 			ret = pos;
859 	}
860 	if (ret)
861 		refcount_inc(&ret->ls_count);
862 	return ret;
863 }
864 
865 /*
866  * Return a compatible lock_state. If no initialized lock_state structure
867  * exists, return an uninitialized one.
868  *
869  */
870 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t owner)
871 {
872 	struct nfs4_lock_state *lsp;
873 	struct nfs_server *server = state->owner->so_server;
874 
875 	lsp = kzalloc(sizeof(*lsp), GFP_KERNEL_ACCOUNT);
876 	if (lsp == NULL)
877 		return NULL;
878 	nfs4_init_seqid_counter(&lsp->ls_seqid);
879 	refcount_set(&lsp->ls_count, 1);
880 	lsp->ls_state = state;
881 	lsp->ls_owner = owner;
882 	lsp->ls_seqid.owner_id = ida_alloc(&server->lockowner_id, GFP_KERNEL_ACCOUNT);
883 	if (lsp->ls_seqid.owner_id < 0)
884 		goto out_free;
885 	INIT_LIST_HEAD(&lsp->ls_locks);
886 	return lsp;
887 out_free:
888 	kfree(lsp);
889 	return NULL;
890 }
891 
892 void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
893 {
894 	ida_free(&server->lockowner_id, lsp->ls_seqid.owner_id);
895 	nfs4_destroy_seqid_counter(&lsp->ls_seqid);
896 	kfree(lsp);
897 }
898 
899 /*
900  * Return a compatible lock_state. If no initialized lock_state structure
901  * exists, return an uninitialized one.
902  *
903  */
904 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
905 {
906 	struct nfs4_lock_state *lsp, *new = NULL;
907 
908 	for(;;) {
909 		spin_lock(&state->state_lock);
910 		lsp = __nfs4_find_lock_state(state, owner, NULL);
911 		if (lsp != NULL)
912 			break;
913 		if (new != NULL) {
914 			list_add(&new->ls_locks, &state->lock_states);
915 			set_bit(LK_STATE_IN_USE, &state->flags);
916 			lsp = new;
917 			new = NULL;
918 			break;
919 		}
920 		spin_unlock(&state->state_lock);
921 		new = nfs4_alloc_lock_state(state, owner);
922 		if (new == NULL)
923 			return NULL;
924 	}
925 	spin_unlock(&state->state_lock);
926 	if (new != NULL)
927 		nfs4_free_lock_state(state->owner->so_server, new);
928 	return lsp;
929 }
930 
931 /*
932  * Release reference to lock_state, and free it if we see that
933  * it is no longer in use
934  */
935 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
936 {
937 	struct nfs_server *server;
938 	struct nfs4_state *state;
939 
940 	if (lsp == NULL)
941 		return;
942 	state = lsp->ls_state;
943 	if (!refcount_dec_and_lock(&lsp->ls_count, &state->state_lock))
944 		return;
945 	list_del(&lsp->ls_locks);
946 	if (list_empty(&state->lock_states))
947 		clear_bit(LK_STATE_IN_USE, &state->flags);
948 	spin_unlock(&state->state_lock);
949 	server = state->owner->so_server;
950 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
951 		struct nfs_client *clp = server->nfs_client;
952 
953 		clp->cl_mvops->free_lock_state(server, lsp);
954 	} else
955 		nfs4_free_lock_state(server, lsp);
956 }
957 
958 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
959 {
960 	struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
961 
962 	dst->fl_u.nfs4_fl.owner = lsp;
963 	refcount_inc(&lsp->ls_count);
964 }
965 
966 static void nfs4_fl_release_lock(struct file_lock *fl)
967 {
968 	nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
969 }
970 
971 static const struct file_lock_operations nfs4_fl_lock_ops = {
972 	.fl_copy_lock = nfs4_fl_copy_lock,
973 	.fl_release_private = nfs4_fl_release_lock,
974 };
975 
976 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
977 {
978 	struct nfs4_lock_state *lsp;
979 
980 	if (fl->fl_ops != NULL)
981 		return 0;
982 	lsp = nfs4_get_lock_state(state, fl->c.flc_owner);
983 	if (lsp == NULL)
984 		return -ENOMEM;
985 	fl->fl_u.nfs4_fl.owner = lsp;
986 	fl->fl_ops = &nfs4_fl_lock_ops;
987 	return 0;
988 }
989 
990 static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
991 		struct nfs4_state *state,
992 		const struct nfs_lock_context *l_ctx)
993 {
994 	struct nfs4_lock_state *lsp;
995 	fl_owner_t owner, fl_flock_owner;
996 	int ret = -ENOENT;
997 
998 	if (l_ctx == NULL)
999 		goto out;
1000 
1001 	if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
1002 		goto out;
1003 
1004 	owner = l_ctx->lockowner;
1005 	fl_flock_owner = l_ctx->open_context->flock_owner;
1006 
1007 	spin_lock(&state->state_lock);
1008 	lsp = __nfs4_find_lock_state(state, owner, fl_flock_owner);
1009 	if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
1010 		ret = -EIO;
1011 	else if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
1012 		nfs4_stateid_copy(dst, &lsp->ls_stateid);
1013 		ret = 0;
1014 	}
1015 	spin_unlock(&state->state_lock);
1016 	nfs4_put_lock_state(lsp);
1017 out:
1018 	return ret;
1019 }
1020 
1021 bool nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1022 {
1023 	bool ret;
1024 	const nfs4_stateid *src;
1025 	int seq;
1026 
1027 	do {
1028 		ret = false;
1029 		src = &zero_stateid;
1030 		seq = read_seqbegin(&state->seqlock);
1031 		if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1032 			src = &state->open_stateid;
1033 			ret = true;
1034 		}
1035 		nfs4_stateid_copy(dst, src);
1036 	} while (read_seqretry(&state->seqlock, seq));
1037 	return ret;
1038 }
1039 
1040 /*
1041  * Byte-range lock aware utility to initialize the stateid of read/write
1042  * requests.
1043  */
1044 int nfs4_select_rw_stateid(struct nfs4_state *state,
1045 		fmode_t fmode, const struct nfs_lock_context *l_ctx,
1046 		nfs4_stateid *dst, const struct cred **cred)
1047 {
1048 	int ret;
1049 
1050 	if (!nfs4_valid_open_stateid(state))
1051 		return -EIO;
1052 	if (cred != NULL)
1053 		*cred = NULL;
1054 	ret = nfs4_copy_lock_stateid(dst, state, l_ctx);
1055 	if (ret == -EIO)
1056 		/* A lost lock - don't even consider delegations */
1057 		goto out;
1058 	/* returns true if delegation stateid found and copied */
1059 	if (nfs4_copy_delegation_stateid(state->inode, fmode, dst, cred)) {
1060 		ret = 0;
1061 		goto out;
1062 	}
1063 	if (ret != -ENOENT)
1064 		/* nfs4_copy_delegation_stateid() didn't over-write
1065 		 * dst, so it still has the lock stateid which we now
1066 		 * choose to use.
1067 		 */
1068 		goto out;
1069 	ret = nfs4_copy_open_stateid(dst, state) ? 0 : -EAGAIN;
1070 out:
1071 	if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
1072 		dst->seqid = 0;
1073 	return ret;
1074 }
1075 
1076 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1077 {
1078 	struct nfs_seqid *new;
1079 
1080 	new = kmalloc(sizeof(*new), gfp_mask);
1081 	if (new == NULL)
1082 		return ERR_PTR(-ENOMEM);
1083 	new->sequence = counter;
1084 	INIT_LIST_HEAD(&new->list);
1085 	new->task = NULL;
1086 	return new;
1087 }
1088 
1089 void nfs_release_seqid(struct nfs_seqid *seqid)
1090 {
1091 	struct nfs_seqid_counter *sequence;
1092 
1093 	if (seqid == NULL || list_empty(&seqid->list))
1094 		return;
1095 	sequence = seqid->sequence;
1096 	spin_lock(&sequence->lock);
1097 	list_del_init(&seqid->list);
1098 	if (!list_empty(&sequence->list)) {
1099 		struct nfs_seqid *next;
1100 
1101 		next = list_first_entry(&sequence->list,
1102 				struct nfs_seqid, list);
1103 		rpc_wake_up_queued_task(&sequence->wait, next->task);
1104 	}
1105 	spin_unlock(&sequence->lock);
1106 }
1107 
1108 void nfs_free_seqid(struct nfs_seqid *seqid)
1109 {
1110 	nfs_release_seqid(seqid);
1111 	kfree(seqid);
1112 }
1113 
1114 /*
1115  * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1116  * failed with a seqid incrementing error -
1117  * see comments nfs4.h:seqid_mutating_error()
1118  */
1119 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1120 {
1121 	switch (status) {
1122 		case 0:
1123 			break;
1124 		case -NFS4ERR_BAD_SEQID:
1125 			if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1126 				return;
1127 			pr_warn_ratelimited("NFS: v4 server returned a bad"
1128 					" sequence-id error on an"
1129 					" unconfirmed sequence %p!\n",
1130 					seqid->sequence);
1131 			return;
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 
1149 void 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  */
1168 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1169 {
1170 	if (seqid != NULL)
1171 		nfs_increment_seqid(status, seqid);
1172 }
1173 
1174 int 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;
1190 unlock:
1191 	spin_unlock(&sequence->lock);
1192 out:
1193 	return status;
1194 }
1195 
1196 static int nfs4_run_state_manager(void *);
1197 
1198 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1199 {
1200 	clear_and_wake_up_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1201 	rpc_wake_up(&clp->cl_rpcwaitq);
1202 }
1203 
1204 /*
1205  * Schedule the nfs_client asynchronous state management routine
1206  */
1207 void nfs4_schedule_state_manager(struct nfs_client *clp)
1208 {
1209 	struct task_struct *task;
1210 	char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1211 	struct rpc_clnt *clnt = clp->cl_rpcclient;
1212 	bool swapon = false;
1213 
1214 	if (clnt->cl_shutdown)
1215 		return;
1216 
1217 	set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
1218 
1219 	if (atomic_read(&clnt->cl_swapper)) {
1220 		swapon = !test_and_set_bit(NFS4CLNT_MANAGER_AVAILABLE,
1221 					   &clp->cl_state);
1222 		if (!swapon) {
1223 			wake_up_var(&clp->cl_state);
1224 			return;
1225 		}
1226 	}
1227 
1228 	if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1229 		return;
1230 
1231 	__module_get(THIS_MODULE);
1232 	refcount_inc(&clp->cl_count);
1233 
1234 	/* The rcu_read_lock() is not strictly necessary, as the state
1235 	 * manager is the only thread that ever changes the rpc_xprt
1236 	 * after it's initialized.  At this point, we're single threaded. */
1237 	rcu_read_lock();
1238 	snprintf(buf, sizeof(buf), "%s-manager",
1239 			rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1240 	rcu_read_unlock();
1241 	task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
1242 	if (IS_ERR(task)) {
1243 		printk(KERN_ERR "%s: kthread_run: %ld\n",
1244 			__func__, PTR_ERR(task));
1245 		if (!nfs_client_init_is_complete(clp))
1246 			nfs_mark_client_ready(clp, PTR_ERR(task));
1247 		if (swapon)
1248 			clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
1249 		nfs4_clear_state_manager_bit(clp);
1250 		nfs_put_client(clp);
1251 		module_put(THIS_MODULE);
1252 	}
1253 }
1254 
1255 /*
1256  * Schedule a lease recovery attempt
1257  */
1258 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1259 {
1260 	if (!clp)
1261 		return;
1262 	if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1263 		set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1264 	dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1265 			clp->cl_hostname);
1266 	nfs4_schedule_state_manager(clp);
1267 }
1268 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1269 
1270 /**
1271  * nfs4_schedule_migration_recovery - trigger migration recovery
1272  *
1273  * @server: FSID that is migrating
1274  *
1275  * Returns zero if recovery has started, otherwise a negative NFS4ERR
1276  * value is returned.
1277  */
1278 int nfs4_schedule_migration_recovery(const struct nfs_server *server)
1279 {
1280 	struct nfs_client *clp = server->nfs_client;
1281 
1282 	if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
1283 		pr_err("NFS: volatile file handles not supported (server %s)\n",
1284 				clp->cl_hostname);
1285 		return -NFS4ERR_IO;
1286 	}
1287 
1288 	if (test_bit(NFS_MIG_FAILED, &server->mig_status))
1289 		return -NFS4ERR_IO;
1290 
1291 	dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1292 			__func__,
1293 			(unsigned long long)server->fsid.major,
1294 			(unsigned long long)server->fsid.minor,
1295 			clp->cl_hostname);
1296 
1297 	set_bit(NFS_MIG_IN_TRANSITION,
1298 			&((struct nfs_server *)server)->mig_status);
1299 	set_bit(NFS4CLNT_MOVED, &clp->cl_state);
1300 
1301 	nfs4_schedule_state_manager(clp);
1302 	return 0;
1303 }
1304 EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
1305 
1306 /**
1307  * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1308  *
1309  * @clp: server to check for moved leases
1310  *
1311  */
1312 void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
1313 {
1314 	dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1315 		__func__, clp->cl_clientid, clp->cl_hostname);
1316 
1317 	set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
1318 	nfs4_schedule_state_manager(clp);
1319 }
1320 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
1321 
1322 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1323 {
1324 	int res;
1325 
1326 	might_sleep();
1327 
1328 	refcount_inc(&clp->cl_count);
1329 	res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1330 				 nfs_wait_bit_killable,
1331 				 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
1332 	if (res)
1333 		goto out;
1334 	if (clp->cl_cons_state < 0)
1335 		res = clp->cl_cons_state;
1336 out:
1337 	nfs_put_client(clp);
1338 	return res;
1339 }
1340 
1341 int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1342 {
1343 	unsigned int loop;
1344 	int ret;
1345 
1346 	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1347 		ret = nfs4_wait_clnt_recover(clp);
1348 		if (ret != 0)
1349 			break;
1350 		if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1351 		    !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1352 			break;
1353 		nfs4_schedule_state_manager(clp);
1354 		ret = -EIO;
1355 	}
1356 	return ret;
1357 }
1358 
1359 /*
1360  * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1361  * @clp: client to process
1362  *
1363  * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1364  * resend of the SETCLIENTID and hence re-establish the
1365  * callback channel. Then return all existing delegations.
1366  */
1367 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1368 {
1369 	set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1370 	nfs_expire_all_delegations(clp);
1371 	dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1372 			clp->cl_hostname);
1373 }
1374 
1375 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1376 {
1377 	nfs40_handle_cb_pathdown(clp);
1378 	nfs4_schedule_state_manager(clp);
1379 }
1380 
1381 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1382 {
1383 
1384 	if (!nfs4_valid_open_stateid(state))
1385 		return 0;
1386 	set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1387 	/* Don't recover state that expired before the reboot */
1388 	if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1389 		clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1390 		return 0;
1391 	}
1392 	set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1393 	set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1394 	return 1;
1395 }
1396 
1397 int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1398 {
1399 	if (!nfs4_valid_open_stateid(state))
1400 		return 0;
1401 	set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1402 	clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1403 	set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1404 	set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1405 	return 1;
1406 }
1407 
1408 int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1409 {
1410 	struct nfs_client *clp = server->nfs_client;
1411 
1412 	if (!nfs4_state_mark_reclaim_nograce(clp, state))
1413 		return -EBADF;
1414 	nfs_inode_find_delegation_state_and_recover(state->inode,
1415 			&state->stateid);
1416 	dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1417 			clp->cl_hostname);
1418 	nfs4_schedule_state_manager(clp);
1419 	return 0;
1420 }
1421 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1422 
1423 static struct nfs4_lock_state *
1424 nfs_state_find_lock_state_by_stateid(struct nfs4_state *state,
1425 		const nfs4_stateid *stateid)
1426 {
1427 	struct nfs4_lock_state *pos;
1428 
1429 	list_for_each_entry(pos, &state->lock_states, ls_locks) {
1430 		if (!test_bit(NFS_LOCK_INITIALIZED, &pos->ls_flags))
1431 			continue;
1432 		if (nfs4_stateid_match_or_older(&pos->ls_stateid, stateid))
1433 			return pos;
1434 	}
1435 	return NULL;
1436 }
1437 
1438 static bool nfs_state_lock_state_matches_stateid(struct nfs4_state *state,
1439 		const nfs4_stateid *stateid)
1440 {
1441 	bool found = false;
1442 
1443 	if (test_bit(LK_STATE_IN_USE, &state->flags)) {
1444 		spin_lock(&state->state_lock);
1445 		if (nfs_state_find_lock_state_by_stateid(state, stateid))
1446 			found = true;
1447 		spin_unlock(&state->state_lock);
1448 	}
1449 	return found;
1450 }
1451 
1452 void nfs_inode_find_state_and_recover(struct inode *inode,
1453 		const nfs4_stateid *stateid)
1454 {
1455 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1456 	struct nfs_inode *nfsi = NFS_I(inode);
1457 	struct nfs_open_context *ctx;
1458 	struct nfs4_state *state;
1459 	bool found = false;
1460 
1461 	rcu_read_lock();
1462 	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1463 		state = ctx->state;
1464 		if (state == NULL)
1465 			continue;
1466 		if (nfs4_stateid_match_or_older(&state->stateid, stateid) &&
1467 		    nfs4_state_mark_reclaim_nograce(clp, state)) {
1468 			found = true;
1469 			continue;
1470 		}
1471 		if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1472 		    nfs4_stateid_match_or_older(&state->open_stateid, stateid) &&
1473 		    nfs4_state_mark_reclaim_nograce(clp, state)) {
1474 			found = true;
1475 			continue;
1476 		}
1477 		if (nfs_state_lock_state_matches_stateid(state, stateid) &&
1478 		    nfs4_state_mark_reclaim_nograce(clp, state))
1479 			found = true;
1480 	}
1481 	rcu_read_unlock();
1482 
1483 	nfs_inode_find_delegation_state_and_recover(inode, stateid);
1484 	if (found)
1485 		nfs4_schedule_state_manager(clp);
1486 }
1487 
1488 static void nfs4_state_mark_open_context_bad(struct nfs4_state *state, int err)
1489 {
1490 	struct inode *inode = state->inode;
1491 	struct nfs_inode *nfsi = NFS_I(inode);
1492 	struct nfs_open_context *ctx;
1493 
1494 	rcu_read_lock();
1495 	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1496 		if (ctx->state != state)
1497 			continue;
1498 		set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1499 		pr_warn("NFSv4: state recovery failed for open file %pd2, "
1500 				"error = %d\n", ctx->dentry, err);
1501 	}
1502 	rcu_read_unlock();
1503 }
1504 
1505 static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1506 {
1507 	set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
1508 	nfs4_state_mark_open_context_bad(state, error);
1509 }
1510 
1511 
1512 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1513 {
1514 	struct inode *inode = state->inode;
1515 	struct nfs_inode *nfsi = NFS_I(inode);
1516 	struct file_lock *fl;
1517 	struct nfs4_lock_state *lsp;
1518 	int status = 0;
1519 	struct file_lock_context *flctx = locks_inode_context(inode);
1520 	struct list_head *list;
1521 
1522 	if (flctx == NULL)
1523 		return 0;
1524 
1525 	list = &flctx->flc_posix;
1526 
1527 	/* Guard against delegation returns and new lock/unlock calls */
1528 	down_write(&nfsi->rwsem);
1529 	spin_lock(&flctx->flc_lock);
1530 restart:
1531 	for_each_file_lock(fl, list) {
1532 		if (nfs_file_open_context(fl->c.flc_file)->state != state)
1533 			continue;
1534 		spin_unlock(&flctx->flc_lock);
1535 		status = ops->recover_lock(state, fl);
1536 		switch (status) {
1537 		case 0:
1538 			break;
1539 		case -ETIMEDOUT:
1540 		case -ESTALE:
1541 		case -NFS4ERR_ADMIN_REVOKED:
1542 		case -NFS4ERR_STALE_STATEID:
1543 		case -NFS4ERR_BAD_STATEID:
1544 		case -NFS4ERR_EXPIRED:
1545 		case -NFS4ERR_NO_GRACE:
1546 		case -NFS4ERR_STALE_CLIENTID:
1547 		case -NFS4ERR_BADSESSION:
1548 		case -NFS4ERR_BADSLOT:
1549 		case -NFS4ERR_BAD_HIGH_SLOT:
1550 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1551 			goto out;
1552 		default:
1553 			pr_err("NFS: %s: unhandled error %d\n",
1554 					__func__, status);
1555 			fallthrough;
1556 		case -ENOMEM:
1557 		case -NFS4ERR_DENIED:
1558 		case -NFS4ERR_RECLAIM_BAD:
1559 		case -NFS4ERR_RECLAIM_CONFLICT:
1560 			lsp = fl->fl_u.nfs4_fl.owner;
1561 			if (lsp)
1562 				set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1563 			status = 0;
1564 		}
1565 		spin_lock(&flctx->flc_lock);
1566 	}
1567 	if (list == &flctx->flc_posix) {
1568 		list = &flctx->flc_flock;
1569 		goto restart;
1570 	}
1571 	spin_unlock(&flctx->flc_lock);
1572 out:
1573 	up_write(&nfsi->rwsem);
1574 	return status;
1575 }
1576 
1577 #ifdef CONFIG_NFS_V4_2
1578 static void nfs42_complete_copies(struct nfs4_state_owner *sp, struct nfs4_state *state)
1579 {
1580 	struct nfs4_copy_state *copy;
1581 
1582 	if (!test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags) &&
1583 		!test_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags))
1584 		return;
1585 
1586 	spin_lock(&sp->so_server->nfs_client->cl_lock);
1587 	list_for_each_entry(copy, &sp->so_server->ss_copies, copies) {
1588 		if ((test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags) &&
1589 				!nfs4_stateid_match_other(&state->stateid,
1590 				&copy->parent_dst_state->stateid)))
1591 				continue;
1592 		copy->flags = 1;
1593 		if (test_and_clear_bit(NFS_CLNT_DST_SSC_COPY_STATE,
1594 				&state->flags)) {
1595 			clear_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags);
1596 			complete(&copy->completion);
1597 		}
1598 	}
1599 	list_for_each_entry(copy, &sp->so_server->ss_copies, src_copies) {
1600 		if ((test_bit(NFS_CLNT_SRC_SSC_COPY_STATE, &state->flags) &&
1601 				!nfs4_stateid_match_other(&state->stateid,
1602 				&copy->parent_src_state->stateid)))
1603 				continue;
1604 		copy->flags = 1;
1605 		if (test_and_clear_bit(NFS_CLNT_DST_SSC_COPY_STATE,
1606 				&state->flags))
1607 			complete(&copy->completion);
1608 	}
1609 	spin_unlock(&sp->so_server->nfs_client->cl_lock);
1610 }
1611 #else /* !CONFIG_NFS_V4_2 */
1612 static inline void nfs42_complete_copies(struct nfs4_state_owner *sp,
1613 					 struct nfs4_state *state)
1614 {
1615 }
1616 #endif /* CONFIG_NFS_V4_2 */
1617 
1618 static int __nfs4_reclaim_open_state(struct nfs4_state_owner *sp, struct nfs4_state *state,
1619 				     const struct nfs4_state_recovery_ops *ops,
1620 				     int *lost_locks)
1621 {
1622 	struct nfs4_lock_state *lock;
1623 	int status;
1624 
1625 	status = ops->recover_open(sp, state);
1626 	if (status < 0)
1627 		return status;
1628 
1629 	status = nfs4_reclaim_locks(state, ops);
1630 	if (status < 0)
1631 		return status;
1632 
1633 	if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1634 		spin_lock(&state->state_lock);
1635 		list_for_each_entry(lock, &state->lock_states, ls_locks) {
1636 			trace_nfs4_state_lock_reclaim(state, lock);
1637 			if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags) &&
1638 			    !test_bit(NFS_LOCK_UNLOCKING, &lock->ls_flags))
1639 				*lost_locks += 1;
1640 		}
1641 		spin_unlock(&state->state_lock);
1642 	}
1643 
1644 	nfs42_complete_copies(sp, state);
1645 	clear_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1646 	return status;
1647 }
1648 
1649 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp,
1650 				   const struct nfs4_state_recovery_ops *ops,
1651 				   int *lost_locks)
1652 {
1653 	struct nfs4_state *state;
1654 	unsigned int loop = 0;
1655 	int status = 0;
1656 #ifdef CONFIG_NFS_V4_2
1657 	bool found_ssc_copy_state = false;
1658 #endif /* CONFIG_NFS_V4_2 */
1659 
1660 	/* Note: we rely on the sp->so_states list being ordered
1661 	 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1662 	 * states first.
1663 	 * This is needed to ensure that the server won't give us any
1664 	 * read delegations that we have to return if, say, we are
1665 	 * recovering after a network partition or a reboot from a
1666 	 * server that doesn't support a grace period.
1667 	 */
1668 	spin_lock(&sp->so_lock);
1669 restart:
1670 	list_for_each_entry(state, &sp->so_states, open_states) {
1671 		if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1672 			continue;
1673 		if (!nfs4_valid_open_stateid(state))
1674 			continue;
1675 		if (state->state == 0)
1676 			continue;
1677 #ifdef CONFIG_NFS_V4_2
1678 		if (test_bit(NFS_SRV_SSC_COPY_STATE, &state->flags)) {
1679 			nfs4_state_mark_recovery_failed(state, -EIO);
1680 			found_ssc_copy_state = true;
1681 			continue;
1682 		}
1683 #endif /* CONFIG_NFS_V4_2 */
1684 		refcount_inc(&state->count);
1685 		spin_unlock(&sp->so_lock);
1686 		status = __nfs4_reclaim_open_state(sp, state, ops, lost_locks);
1687 
1688 		switch (status) {
1689 		default:
1690 			if (status >= 0) {
1691 				loop = 0;
1692 				break;
1693 			}
1694 			printk(KERN_ERR "NFS: %s: unhandled error %d\n", __func__, status);
1695 			fallthrough;
1696 		case -ENOENT:
1697 		case -ENOMEM:
1698 		case -EACCES:
1699 		case -EROFS:
1700 		case -EIO:
1701 		case -ESTALE:
1702 			/* Open state on this file cannot be recovered */
1703 			nfs4_state_mark_recovery_failed(state, status);
1704 			break;
1705 		case -EAGAIN:
1706 			ssleep(1);
1707 			if (loop++ < 10) {
1708 				set_bit(ops->state_flag_bit, &state->flags);
1709 				break;
1710 			}
1711 			fallthrough;
1712 		case -NFS4ERR_ADMIN_REVOKED:
1713 		case -NFS4ERR_STALE_STATEID:
1714 		case -NFS4ERR_OLD_STATEID:
1715 		case -NFS4ERR_BAD_STATEID:
1716 		case -NFS4ERR_RECLAIM_BAD:
1717 		case -NFS4ERR_RECLAIM_CONFLICT:
1718 			nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1719 			break;
1720 		case -NFS4ERR_EXPIRED:
1721 		case -NFS4ERR_NO_GRACE:
1722 			nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1723 			fallthrough;
1724 		case -NFS4ERR_STALE_CLIENTID:
1725 		case -NFS4ERR_BADSESSION:
1726 		case -NFS4ERR_BADSLOT:
1727 		case -NFS4ERR_BAD_HIGH_SLOT:
1728 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1729 		case -ETIMEDOUT:
1730 			goto out_err;
1731 		}
1732 		nfs4_put_open_state(state);
1733 		spin_lock(&sp->so_lock);
1734 		goto restart;
1735 	}
1736 	spin_unlock(&sp->so_lock);
1737 #ifdef CONFIG_NFS_V4_2
1738 	if (found_ssc_copy_state)
1739 		return -EIO;
1740 #endif /* CONFIG_NFS_V4_2 */
1741 	return 0;
1742 out_err:
1743 	nfs4_put_open_state(state);
1744 	spin_lock(&sp->so_lock);
1745 	spin_unlock(&sp->so_lock);
1746 	return status;
1747 }
1748 
1749 static void nfs4_clear_open_state(struct nfs4_state *state)
1750 {
1751 	struct nfs4_lock_state *lock;
1752 
1753 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
1754 	clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1755 	clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1756 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1757 	spin_lock(&state->state_lock);
1758 	list_for_each_entry(lock, &state->lock_states, ls_locks) {
1759 		lock->ls_seqid.flags = 0;
1760 		clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1761 	}
1762 	spin_unlock(&state->state_lock);
1763 }
1764 
1765 static void nfs4_reset_seqids(struct nfs_server *server,
1766 	int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1767 {
1768 	struct nfs_client *clp = server->nfs_client;
1769 	struct nfs4_state_owner *sp;
1770 	struct rb_node *pos;
1771 	struct nfs4_state *state;
1772 
1773 	spin_lock(&clp->cl_lock);
1774 	for (pos = rb_first(&server->state_owners);
1775 	     pos != NULL;
1776 	     pos = rb_next(pos)) {
1777 		sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1778 		sp->so_seqid.flags = 0;
1779 		spin_lock(&sp->so_lock);
1780 		list_for_each_entry(state, &sp->so_states, open_states) {
1781 			if (mark_reclaim(clp, state))
1782 				nfs4_clear_open_state(state);
1783 		}
1784 		spin_unlock(&sp->so_lock);
1785 	}
1786 	spin_unlock(&clp->cl_lock);
1787 }
1788 
1789 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1790 	int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1791 {
1792 	struct nfs_server *server;
1793 
1794 	rcu_read_lock();
1795 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1796 		nfs4_reset_seqids(server, mark_reclaim);
1797 	rcu_read_unlock();
1798 }
1799 
1800 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1801 {
1802 	set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1803 	/* Mark all delegations for reclaim */
1804 	nfs_delegation_mark_reclaim(clp);
1805 	nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1806 }
1807 
1808 static int nfs4_reclaim_complete(struct nfs_client *clp,
1809 				 const struct nfs4_state_recovery_ops *ops,
1810 				 const struct cred *cred)
1811 {
1812 	/* Notify the server we're done reclaiming our state */
1813 	if (ops->reclaim_complete)
1814 		return ops->reclaim_complete(clp, cred);
1815 	return 0;
1816 }
1817 
1818 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1819 {
1820 	struct nfs_client *clp = server->nfs_client;
1821 	struct nfs4_state_owner *sp;
1822 	struct rb_node *pos;
1823 	struct nfs4_state *state;
1824 
1825 	spin_lock(&clp->cl_lock);
1826 	for (pos = rb_first(&server->state_owners);
1827 	     pos != NULL;
1828 	     pos = rb_next(pos)) {
1829 		sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1830 		spin_lock(&sp->so_lock);
1831 		list_for_each_entry(state, &sp->so_states, open_states) {
1832 			if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1833 						&state->flags))
1834 				continue;
1835 			nfs4_state_mark_reclaim_nograce(clp, state);
1836 		}
1837 		spin_unlock(&sp->so_lock);
1838 	}
1839 	spin_unlock(&clp->cl_lock);
1840 }
1841 
1842 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1843 {
1844 	struct nfs_server *server;
1845 
1846 	if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1847 		return 0;
1848 
1849 	rcu_read_lock();
1850 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1851 		nfs4_clear_reclaim_server(server);
1852 	rcu_read_unlock();
1853 
1854 	nfs_delegation_reap_unclaimed(clp);
1855 	return 1;
1856 }
1857 
1858 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1859 {
1860 	const struct nfs4_state_recovery_ops *ops;
1861 	const struct cred *cred;
1862 	int err;
1863 
1864 	if (!nfs4_state_clear_reclaim_reboot(clp))
1865 		return;
1866 	pnfs_destroy_all_layouts(clp);
1867 	ops = clp->cl_mvops->reboot_recovery_ops;
1868 	cred = nfs4_get_clid_cred(clp);
1869 	err = nfs4_reclaim_complete(clp, ops, cred);
1870 	put_cred(cred);
1871 	if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
1872 		set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1873 }
1874 
1875 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1876 {
1877 	nfs_mark_test_expired_all_delegations(clp);
1878 	nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1879 }
1880 
1881 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1882 {
1883 	switch (error) {
1884 	case 0:
1885 		break;
1886 	case -NFS4ERR_CB_PATH_DOWN:
1887 		nfs40_handle_cb_pathdown(clp);
1888 		break;
1889 	case -NFS4ERR_NO_GRACE:
1890 		nfs4_state_end_reclaim_reboot(clp);
1891 		break;
1892 	case -NFS4ERR_STALE_CLIENTID:
1893 		set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1894 		nfs4_state_start_reclaim_reboot(clp);
1895 		break;
1896 	case -NFS4ERR_EXPIRED:
1897 		set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1898 		nfs4_state_start_reclaim_nograce(clp);
1899 		break;
1900 	case -NFS4ERR_BADSESSION:
1901 	case -NFS4ERR_BADSLOT:
1902 	case -NFS4ERR_BAD_HIGH_SLOT:
1903 	case -NFS4ERR_DEADSESSION:
1904 	case -NFS4ERR_SEQ_FALSE_RETRY:
1905 	case -NFS4ERR_SEQ_MISORDERED:
1906 		set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1907 		/* Zero session reset errors */
1908 		break;
1909 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1910 		set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1911 		break;
1912 	default:
1913 		dprintk("%s: failed to handle error %d for server %s\n",
1914 				__func__, error, clp->cl_hostname);
1915 		return error;
1916 	}
1917 	dprintk("%s: handled error %d for server %s\n", __func__, error,
1918 			clp->cl_hostname);
1919 	return 0;
1920 }
1921 
1922 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1923 {
1924 	struct nfs4_state_owner *sp;
1925 	struct nfs_server *server;
1926 	struct rb_node *pos;
1927 	LIST_HEAD(freeme);
1928 	int lost_locks = 0;
1929 	int status;
1930 
1931 	status = nfs4_begin_drain_session(clp);
1932 	if (status < 0)
1933 		return status;
1934 restart:
1935 	rcu_read_lock();
1936 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1937 		nfs4_purge_state_owners(server, &freeme);
1938 		spin_lock(&clp->cl_lock);
1939 		for (pos = rb_first(&server->state_owners);
1940 		     pos != NULL;
1941 		     pos = rb_next(pos)) {
1942 			sp = rb_entry(pos,
1943 				struct nfs4_state_owner, so_server_node);
1944 			if (!test_and_clear_bit(ops->owner_flag_bit,
1945 							&sp->so_flags))
1946 				continue;
1947 			if (!atomic_inc_not_zero(&sp->so_count))
1948 				continue;
1949 			spin_unlock(&clp->cl_lock);
1950 			rcu_read_unlock();
1951 
1952 			status = nfs4_reclaim_open_state(sp, ops, &lost_locks);
1953 			if (status < 0) {
1954 				if (lost_locks)
1955 					pr_warn("NFS: %s: lost %d locks\n",
1956 						clp->cl_hostname, lost_locks);
1957 				set_bit(ops->owner_flag_bit, &sp->so_flags);
1958 				nfs4_put_state_owner(sp);
1959 				status = nfs4_recovery_handle_error(clp, status);
1960 				return (status != 0) ? status : -EAGAIN;
1961 			}
1962 
1963 			nfs4_put_state_owner(sp);
1964 			goto restart;
1965 		}
1966 		spin_unlock(&clp->cl_lock);
1967 	}
1968 	rcu_read_unlock();
1969 	nfs4_free_state_owners(&freeme);
1970 	if (lost_locks)
1971 		pr_warn("NFS: %s: lost %d locks\n",
1972 			clp->cl_hostname, lost_locks);
1973 	return 0;
1974 }
1975 
1976 static int nfs4_check_lease(struct nfs_client *clp)
1977 {
1978 	const struct cred *cred;
1979 	const struct nfs4_state_maintenance_ops *ops =
1980 		clp->cl_mvops->state_renewal_ops;
1981 	int status;
1982 
1983 	/* Is the client already known to have an expired lease? */
1984 	if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1985 		return 0;
1986 	cred = ops->get_state_renewal_cred(clp);
1987 	if (cred == NULL) {
1988 		cred = nfs4_get_clid_cred(clp);
1989 		status = -ENOKEY;
1990 		if (cred == NULL)
1991 			goto out;
1992 	}
1993 	status = ops->renew_lease(clp, cred);
1994 	put_cred(cred);
1995 	if (status == -ETIMEDOUT) {
1996 		set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1997 		return 0;
1998 	}
1999 out:
2000 	return nfs4_recovery_handle_error(clp, status);
2001 }
2002 
2003 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
2004  * and for recoverable errors on EXCHANGE_ID for v4.1
2005  */
2006 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
2007 {
2008 	switch (status) {
2009 	case -NFS4ERR_SEQ_MISORDERED:
2010 		if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
2011 			return -ESERVERFAULT;
2012 		/* Lease confirmation error: retry after purging the lease */
2013 		ssleep(1);
2014 		clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2015 		break;
2016 	case -NFS4ERR_STALE_CLIENTID:
2017 		clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2018 		nfs4_state_start_reclaim_reboot(clp);
2019 		break;
2020 	case -NFS4ERR_CLID_INUSE:
2021 		pr_err("NFS: Server %s reports our clientid is in use\n",
2022 			clp->cl_hostname);
2023 		nfs_mark_client_ready(clp, -EPERM);
2024 		clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2025 		return -EPERM;
2026 	case -EACCES:
2027 	case -NFS4ERR_DELAY:
2028 	case -EAGAIN:
2029 		ssleep(1);
2030 		break;
2031 
2032 	case -NFS4ERR_MINOR_VERS_MISMATCH:
2033 		if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
2034 			nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
2035 		dprintk("%s: exit with error %d for server %s\n",
2036 				__func__, -EPROTONOSUPPORT, clp->cl_hostname);
2037 		return -EPROTONOSUPPORT;
2038 	case -ENOSPC:
2039 		if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
2040 			nfs_mark_client_ready(clp, -EIO);
2041 		return -EIO;
2042 	case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2043 				 * in nfs4_exchange_id */
2044 	default:
2045 		dprintk("%s: exit with error %d for server %s\n", __func__,
2046 				status, clp->cl_hostname);
2047 		return status;
2048 	}
2049 	set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2050 	dprintk("%s: handled error %d for server %s\n", __func__, status,
2051 			clp->cl_hostname);
2052 	return 0;
2053 }
2054 
2055 static int nfs4_establish_lease(struct nfs_client *clp)
2056 {
2057 	const struct cred *cred;
2058 	const struct nfs4_state_recovery_ops *ops =
2059 		clp->cl_mvops->reboot_recovery_ops;
2060 	int status;
2061 
2062 	status = nfs4_begin_drain_session(clp);
2063 	if (status != 0)
2064 		return status;
2065 	cred = nfs4_get_clid_cred(clp);
2066 	if (cred == NULL)
2067 		return -ENOENT;
2068 	status = ops->establish_clid(clp, cred);
2069 	put_cred(cred);
2070 	if (status != 0)
2071 		return status;
2072 	return 0;
2073 }
2074 
2075 /*
2076  * Returns zero or a negative errno.  NFS4ERR values are converted
2077  * to local errno values.
2078  */
2079 static int nfs4_reclaim_lease(struct nfs_client *clp)
2080 {
2081 	int status;
2082 
2083 	status = nfs4_establish_lease(clp);
2084 	if (status < 0)
2085 		return nfs4_handle_reclaim_lease_error(clp, status);
2086 	if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
2087 		nfs4_state_start_reclaim_nograce(clp);
2088 	if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2089 		set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
2090 	clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2091 	clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2092 	return 0;
2093 }
2094 
2095 static int nfs4_purge_lease(struct nfs_client *clp)
2096 {
2097 	int status;
2098 
2099 	status = nfs4_establish_lease(clp);
2100 	if (status < 0)
2101 		return nfs4_handle_reclaim_lease_error(clp, status);
2102 	clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2103 	set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2104 	nfs4_state_start_reclaim_nograce(clp);
2105 	return 0;
2106 }
2107 
2108 /*
2109  * Try remote migration of one FSID from a source server to a
2110  * destination server.  The source server provides a list of
2111  * potential destinations.
2112  *
2113  * Returns zero or a negative NFS4ERR status code.
2114  */
2115 static int nfs4_try_migration(struct nfs_server *server, const struct cred *cred)
2116 {
2117 	struct nfs_client *clp = server->nfs_client;
2118 	struct nfs4_fs_locations *locations = NULL;
2119 	struct nfs_fattr *fattr;
2120 	struct inode *inode;
2121 	struct page *page;
2122 	int status, result;
2123 
2124 	dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
2125 			(unsigned long long)server->fsid.major,
2126 			(unsigned long long)server->fsid.minor,
2127 			clp->cl_hostname);
2128 
2129 	page = alloc_page(GFP_KERNEL);
2130 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2131 	fattr = nfs_alloc_fattr();
2132 	if (page == NULL || locations == NULL || fattr == NULL) {
2133 		dprintk("<-- %s: no memory\n", __func__);
2134 		result = 0;
2135 		goto out;
2136 	}
2137 
2138 	locations->fattr = fattr;
2139 	inode = d_inode(server->super->s_root);
2140 	result = nfs4_proc_get_locations(server, NFS_FH(inode), locations,
2141 					 page, cred);
2142 	if (result) {
2143 		dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2144 			__func__, result);
2145 		goto out;
2146 	}
2147 
2148 	result = -NFS4ERR_NXIO;
2149 	if (!locations->nlocations)
2150 		goto out;
2151 
2152 	if (!(locations->fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
2153 		dprintk("<-- %s: No fs_locations data, migration skipped\n",
2154 			__func__);
2155 		goto out;
2156 	}
2157 
2158 	status = nfs4_begin_drain_session(clp);
2159 	if (status != 0) {
2160 		result = status;
2161 		goto out;
2162 	}
2163 
2164 	status = nfs4_replace_transport(server, locations);
2165 	if (status != 0) {
2166 		dprintk("<-- %s: failed to replace transport: %d\n",
2167 			__func__, status);
2168 		goto out;
2169 	}
2170 
2171 	result = 0;
2172 	dprintk("<-- %s: migration succeeded\n", __func__);
2173 
2174 out:
2175 	if (page != NULL)
2176 		__free_page(page);
2177 	if (locations != NULL)
2178 		kfree(locations->fattr);
2179 	kfree(locations);
2180 	if (result) {
2181 		pr_err("NFS: migration recovery failed (server %s)\n",
2182 				clp->cl_hostname);
2183 		set_bit(NFS_MIG_FAILED, &server->mig_status);
2184 	}
2185 	return result;
2186 }
2187 
2188 /*
2189  * Returns zero or a negative NFS4ERR status code.
2190  */
2191 static int nfs4_handle_migration(struct nfs_client *clp)
2192 {
2193 	const struct nfs4_state_maintenance_ops *ops =
2194 				clp->cl_mvops->state_renewal_ops;
2195 	struct nfs_server *server;
2196 	const struct cred *cred;
2197 
2198 	dprintk("%s: migration reported on \"%s\"\n", __func__,
2199 			clp->cl_hostname);
2200 
2201 	cred = ops->get_state_renewal_cred(clp);
2202 	if (cred == NULL)
2203 		return -NFS4ERR_NOENT;
2204 
2205 	clp->cl_mig_gen++;
2206 restart:
2207 	rcu_read_lock();
2208 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2209 		int status;
2210 
2211 		if (server->mig_gen == clp->cl_mig_gen)
2212 			continue;
2213 		server->mig_gen = clp->cl_mig_gen;
2214 
2215 		if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
2216 						&server->mig_status))
2217 			continue;
2218 
2219 		rcu_read_unlock();
2220 		status = nfs4_try_migration(server, cred);
2221 		if (status < 0) {
2222 			put_cred(cred);
2223 			return status;
2224 		}
2225 		goto restart;
2226 	}
2227 	rcu_read_unlock();
2228 	put_cred(cred);
2229 	return 0;
2230 }
2231 
2232 /*
2233  * Test each nfs_server on the clp's cl_superblocks list to see
2234  * if it's moved to another server.  Stop when the server no longer
2235  * returns NFS4ERR_LEASE_MOVED.
2236  */
2237 static int nfs4_handle_lease_moved(struct nfs_client *clp)
2238 {
2239 	const struct nfs4_state_maintenance_ops *ops =
2240 				clp->cl_mvops->state_renewal_ops;
2241 	struct nfs_server *server;
2242 	const struct cred *cred;
2243 
2244 	dprintk("%s: lease moved reported on \"%s\"\n", __func__,
2245 			clp->cl_hostname);
2246 
2247 	cred = ops->get_state_renewal_cred(clp);
2248 	if (cred == NULL)
2249 		return -NFS4ERR_NOENT;
2250 
2251 	clp->cl_mig_gen++;
2252 restart:
2253 	rcu_read_lock();
2254 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2255 		struct inode *inode;
2256 		int status;
2257 
2258 		if (server->mig_gen == clp->cl_mig_gen)
2259 			continue;
2260 		server->mig_gen = clp->cl_mig_gen;
2261 
2262 		rcu_read_unlock();
2263 
2264 		inode = d_inode(server->super->s_root);
2265 		status = nfs4_proc_fsid_present(inode, cred);
2266 		if (status != -NFS4ERR_MOVED)
2267 			goto restart;	/* wasn't this one */
2268 		if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
2269 			goto restart;	/* there are more */
2270 		goto out;
2271 	}
2272 	rcu_read_unlock();
2273 
2274 out:
2275 	put_cred(cred);
2276 	return 0;
2277 }
2278 
2279 /**
2280  * nfs4_discover_server_trunking - Detect server IP address trunking
2281  *
2282  * @clp: nfs_client under test
2283  * @result: OUT: found nfs_client, or clp
2284  *
2285  * Returns zero or a negative errno.  If zero is returned,
2286  * an nfs_client pointer is planted in "result".
2287  *
2288  * Note: since we are invoked in process context, and
2289  * not from inside the state manager, we cannot use
2290  * nfs4_handle_reclaim_lease_error().
2291  */
2292 int nfs4_discover_server_trunking(struct nfs_client *clp,
2293 				  struct nfs_client **result)
2294 {
2295 	const struct nfs4_state_recovery_ops *ops =
2296 				clp->cl_mvops->reboot_recovery_ops;
2297 	struct rpc_clnt *clnt;
2298 	const struct cred *cred;
2299 	int i, status;
2300 
2301 	dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
2302 
2303 	clnt = clp->cl_rpcclient;
2304 	i = 0;
2305 
2306 	mutex_lock(&nfs_clid_init_mutex);
2307 again:
2308 	status  = -ENOENT;
2309 	cred = nfs4_get_clid_cred(clp);
2310 	if (cred == NULL)
2311 		goto out_unlock;
2312 
2313 	status = ops->detect_trunking(clp, result, cred);
2314 	put_cred(cred);
2315 	switch (status) {
2316 	case 0:
2317 	case -EINTR:
2318 	case -ERESTARTSYS:
2319 		break;
2320 	case -ETIMEDOUT:
2321 		if (clnt->cl_softrtry)
2322 			break;
2323 		fallthrough;
2324 	case -NFS4ERR_DELAY:
2325 	case -EAGAIN:
2326 		ssleep(1);
2327 		fallthrough;
2328 	case -NFS4ERR_STALE_CLIENTID:
2329 		dprintk("NFS: %s after status %d, retrying\n",
2330 			__func__, status);
2331 		goto again;
2332 	case -EACCES:
2333 		if (i++ == 0) {
2334 			nfs4_root_machine_cred(clp);
2335 			goto again;
2336 		}
2337 		if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
2338 			break;
2339 		fallthrough;
2340 	case -NFS4ERR_CLID_INUSE:
2341 	case -NFS4ERR_WRONGSEC:
2342 		/* No point in retrying if we already used RPC_AUTH_UNIX */
2343 		if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
2344 			status = -EPERM;
2345 			break;
2346 		}
2347 		clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
2348 		if (IS_ERR(clnt)) {
2349 			status = PTR_ERR(clnt);
2350 			break;
2351 		}
2352 		/* Note: this is safe because we haven't yet marked the
2353 		 * client as ready, so we are the only user of
2354 		 * clp->cl_rpcclient
2355 		 */
2356 		clnt = xchg(&clp->cl_rpcclient, clnt);
2357 		rpc_shutdown_client(clnt);
2358 		clnt = clp->cl_rpcclient;
2359 		goto again;
2360 
2361 	case -NFS4ERR_MINOR_VERS_MISMATCH:
2362 		status = -EPROTONOSUPPORT;
2363 		break;
2364 
2365 	case -EKEYEXPIRED:
2366 	case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2367 				 * in nfs4_exchange_id */
2368 		status = -EKEYEXPIRED;
2369 		break;
2370 	default:
2371 		pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2372 				__func__, status);
2373 		status = -EIO;
2374 	}
2375 
2376 out_unlock:
2377 	mutex_unlock(&nfs_clid_init_mutex);
2378 	dprintk("NFS: %s: status = %d\n", __func__, status);
2379 	return status;
2380 }
2381 
2382 #ifdef CONFIG_NFS_V4_1
2383 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
2384 {
2385 	struct nfs_client *clp = session->clp;
2386 
2387 	switch (err) {
2388 	default:
2389 		set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2390 		break;
2391 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2392 		set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2393 	}
2394 	nfs4_schedule_state_manager(clp);
2395 }
2396 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
2397 
2398 void nfs41_notify_server(struct nfs_client *clp)
2399 {
2400 	/* Use CHECK_LEASE to ping the server with a SEQUENCE */
2401 	set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2402 	nfs4_schedule_state_manager(clp);
2403 }
2404 
2405 static void nfs4_reset_all_state(struct nfs_client *clp)
2406 {
2407 	if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2408 		set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2409 		clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2410 		nfs4_state_start_reclaim_nograce(clp);
2411 		dprintk("%s: scheduling reset of all state for server %s!\n",
2412 				__func__, clp->cl_hostname);
2413 		nfs4_schedule_state_manager(clp);
2414 	}
2415 }
2416 
2417 static void nfs41_handle_server_reboot(struct nfs_client *clp)
2418 {
2419 	if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2420 		nfs4_state_start_reclaim_reboot(clp);
2421 		dprintk("%s: server %s rebooted!\n", __func__,
2422 				clp->cl_hostname);
2423 		nfs4_schedule_state_manager(clp);
2424 	}
2425 }
2426 
2427 static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
2428 {
2429 	nfs4_reset_all_state(clp);
2430 	dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2431 }
2432 
2433 static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
2434 {
2435 	nfs4_state_start_reclaim_nograce(clp);
2436 	nfs4_schedule_state_manager(clp);
2437 
2438 	dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2439 }
2440 
2441 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
2442 {
2443 	/* FIXME: For now, we destroy all layouts. */
2444 	pnfs_destroy_all_layouts(clp);
2445 	nfs_test_expired_all_delegations(clp);
2446 	dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2447 			clp->cl_hostname);
2448 }
2449 
2450 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
2451 {
2452 	set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2453 	nfs4_schedule_state_manager(clp);
2454 
2455 	dprintk("%s: server %s declared a backchannel fault\n", __func__,
2456 			clp->cl_hostname);
2457 }
2458 
2459 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2460 {
2461 	if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2462 		&clp->cl_state) == 0)
2463 		nfs4_schedule_state_manager(clp);
2464 }
2465 
2466 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
2467 		bool recovery)
2468 {
2469 	if (!flags)
2470 		return;
2471 
2472 	dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2473 		__func__, clp->cl_hostname, clp->cl_clientid, flags);
2474 	/*
2475 	 * If we're called from the state manager thread, then assume we're
2476 	 * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2477 	 * Those flags are expected to remain set until we're done
2478 	 * recovering (see RFC5661, section 18.46.3).
2479 	 */
2480 	if (recovery)
2481 		goto out_recovery;
2482 
2483 	if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2484 		nfs41_handle_server_reboot(clp);
2485 	if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
2486 		nfs41_handle_all_state_revoked(clp);
2487 	if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2488 			    SEQ4_STATUS_ADMIN_STATE_REVOKED))
2489 		nfs41_handle_some_state_revoked(clp);
2490 	if (flags & SEQ4_STATUS_LEASE_MOVED)
2491 		nfs4_schedule_lease_moved_recovery(clp);
2492 	if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2493 		nfs41_handle_recallable_state_revoked(clp);
2494 out_recovery:
2495 	if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2496 		nfs41_handle_backchannel_fault(clp);
2497 	else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2498 				SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2499 		nfs41_handle_cb_path_down(clp);
2500 }
2501 
2502 static int nfs4_reset_session(struct nfs_client *clp)
2503 {
2504 	const struct cred *cred;
2505 	int status;
2506 
2507 	if (!nfs4_has_session(clp))
2508 		return 0;
2509 	status = nfs4_begin_drain_session(clp);
2510 	if (status != 0)
2511 		return status;
2512 	cred = nfs4_get_clid_cred(clp);
2513 	status = nfs4_proc_destroy_session(clp->cl_session, cred);
2514 	switch (status) {
2515 	case 0:
2516 	case -NFS4ERR_BADSESSION:
2517 	case -NFS4ERR_DEADSESSION:
2518 		break;
2519 	case -NFS4ERR_BACK_CHAN_BUSY:
2520 	case -NFS4ERR_DELAY:
2521 		set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2522 		status = 0;
2523 		ssleep(1);
2524 		goto out;
2525 	default:
2526 		status = nfs4_recovery_handle_error(clp, status);
2527 		goto out;
2528 	}
2529 
2530 	memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2531 	status = nfs4_proc_create_session(clp, cred);
2532 	if (status) {
2533 		dprintk("%s: session reset failed with status %d for server %s!\n",
2534 			__func__, status, clp->cl_hostname);
2535 		status = nfs4_handle_reclaim_lease_error(clp, status);
2536 		goto out;
2537 	}
2538 	nfs41_finish_session_reset(clp);
2539 	dprintk("%s: session reset was successful for server %s!\n",
2540 			__func__, clp->cl_hostname);
2541 out:
2542 	put_cred(cred);
2543 	return status;
2544 }
2545 
2546 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2547 {
2548 	const struct cred *cred;
2549 	int ret;
2550 
2551 	if (!nfs4_has_session(clp))
2552 		return 0;
2553 	ret = nfs4_begin_drain_session(clp);
2554 	if (ret != 0)
2555 		return ret;
2556 	cred = nfs4_get_clid_cred(clp);
2557 	ret = nfs4_proc_bind_conn_to_session(clp, cred);
2558 	put_cred(cred);
2559 	clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2560 	switch (ret) {
2561 	case 0:
2562 		dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2563 			__func__, clp->cl_hostname);
2564 		break;
2565 	case -NFS4ERR_DELAY:
2566 		ssleep(1);
2567 		set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2568 		break;
2569 	default:
2570 		return nfs4_recovery_handle_error(clp, ret);
2571 	}
2572 	return 0;
2573 }
2574 
2575 static void nfs4_layoutreturn_any_run(struct nfs_client *clp)
2576 {
2577 	int iomode = 0;
2578 
2579 	if (test_and_clear_bit(NFS4CLNT_RECALL_ANY_LAYOUT_READ, &clp->cl_state))
2580 		iomode += IOMODE_READ;
2581 	if (test_and_clear_bit(NFS4CLNT_RECALL_ANY_LAYOUT_RW, &clp->cl_state))
2582 		iomode += IOMODE_RW;
2583 	/* Note: IOMODE_READ + IOMODE_RW == IOMODE_ANY */
2584 	if (iomode) {
2585 		pnfs_layout_return_unused_byclid(clp, iomode);
2586 		set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2587 	}
2588 }
2589 #else /* CONFIG_NFS_V4_1 */
2590 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2591 
2592 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2593 {
2594 	return 0;
2595 }
2596 
2597 static void nfs4_layoutreturn_any_run(struct nfs_client *clp)
2598 {
2599 }
2600 #endif /* CONFIG_NFS_V4_1 */
2601 
2602 static void nfs4_state_manager(struct nfs_client *clp)
2603 {
2604 	unsigned int memflags;
2605 	int status = 0;
2606 	const char *section = "", *section_sep = "";
2607 
2608 	/*
2609 	 * State recovery can deadlock if the direct reclaim code tries
2610 	 * start NFS writeback. So ensure memory allocations are all
2611 	 * GFP_NOFS.
2612 	 */
2613 	memflags = memalloc_nofs_save();
2614 
2615 	/* Ensure exclusive access to NFSv4 state */
2616 	do {
2617 		trace_nfs4_state_mgr(clp);
2618 		clear_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2619 		if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2620 			section = "purge state";
2621 			status = nfs4_purge_lease(clp);
2622 			if (status < 0)
2623 				goto out_error;
2624 			continue;
2625 		}
2626 
2627 		if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2628 			section = "lease expired";
2629 			/* We're going to have to re-establish a clientid */
2630 			status = nfs4_reclaim_lease(clp);
2631 			if (status < 0)
2632 				goto out_error;
2633 			continue;
2634 		}
2635 
2636 		/* Initialize or reset the session */
2637 		if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2638 			section = "reset session";
2639 			status = nfs4_reset_session(clp);
2640 			if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2641 				continue;
2642 			if (status < 0)
2643 				goto out_error;
2644 		}
2645 
2646 		/* Send BIND_CONN_TO_SESSION */
2647 		if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2648 				&clp->cl_state)) {
2649 			section = "bind conn to session";
2650 			status = nfs4_bind_conn_to_session(clp);
2651 			if (status < 0)
2652 				goto out_error;
2653 			continue;
2654 		}
2655 
2656 		if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2657 			section = "check lease";
2658 			status = nfs4_check_lease(clp);
2659 			if (status < 0)
2660 				goto out_error;
2661 			continue;
2662 		}
2663 
2664 		if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
2665 			section = "migration";
2666 			status = nfs4_handle_migration(clp);
2667 			if (status < 0)
2668 				goto out_error;
2669 		}
2670 
2671 		if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
2672 			section = "lease moved";
2673 			status = nfs4_handle_lease_moved(clp);
2674 			if (status < 0)
2675 				goto out_error;
2676 		}
2677 
2678 		/* First recover reboot state... */
2679 		if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2680 			section = "reclaim reboot";
2681 			status = nfs4_do_reclaim(clp,
2682 				clp->cl_mvops->reboot_recovery_ops);
2683 			if (status == 0)
2684 				status = pnfs_layout_handle_reboot(clp);
2685 			if (status == -EAGAIN)
2686 				continue;
2687 			if (status < 0)
2688 				goto out_error;
2689 			nfs4_state_end_reclaim_reboot(clp);
2690 			continue;
2691 		}
2692 
2693 		/* Detect expired delegations... */
2694 		if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
2695 			section = "detect expired delegations";
2696 			status = nfs4_begin_drain_session(clp);
2697 			if (status < 0)
2698 				goto out_error;
2699 			nfs_reap_expired_delegations(clp);
2700 			continue;
2701 		}
2702 
2703 		/* Now recover expired state... */
2704 		if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2705 			section = "reclaim nograce";
2706 			status = nfs4_do_reclaim(clp,
2707 				clp->cl_mvops->nograce_recovery_ops);
2708 			if (status == -EAGAIN)
2709 				continue;
2710 			if (status < 0)
2711 				goto out_error;
2712 			clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
2713 		}
2714 
2715 		memalloc_nofs_restore(memflags);
2716 		nfs4_end_drain_session(clp);
2717 		nfs4_clear_state_manager_bit(clp);
2718 
2719 		if (test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state) &&
2720 		    !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING,
2721 				      &clp->cl_state)) {
2722 			memflags = memalloc_nofs_save();
2723 			continue;
2724 		}
2725 
2726 		if (!test_and_set_bit(NFS4CLNT_RECALL_RUNNING, &clp->cl_state)) {
2727 			if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2728 				nfs_client_return_marked_delegations(clp);
2729 				set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2730 			}
2731 			nfs4_layoutreturn_any_run(clp);
2732 			clear_bit(NFS4CLNT_RECALL_RUNNING, &clp->cl_state);
2733 		}
2734 
2735 		return;
2736 
2737 	} while (refcount_read(&clp->cl_count) > 1 && !signalled());
2738 	goto out_drain;
2739 
2740 out_error:
2741 	if (strlen(section))
2742 		section_sep = ": ";
2743 	trace_nfs4_state_mgr_failed(clp, section, status);
2744 	pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2745 			" with error %d\n", section_sep, section,
2746 			clp->cl_hostname, -status);
2747 	ssleep(1);
2748 out_drain:
2749 	memalloc_nofs_restore(memflags);
2750 	nfs4_end_drain_session(clp);
2751 	nfs4_clear_state_manager_bit(clp);
2752 }
2753 
2754 static int nfs4_run_state_manager(void *ptr)
2755 {
2756 	struct nfs_client *clp = ptr;
2757 	struct rpc_clnt *cl = clp->cl_rpcclient;
2758 
2759 	while (cl != cl->cl_parent)
2760 		cl = cl->cl_parent;
2761 
2762 	allow_signal(SIGKILL);
2763 again:
2764 	nfs4_state_manager(clp);
2765 
2766 	if (test_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state) &&
2767 	    !test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state)) {
2768 		wait_var_event_interruptible(&clp->cl_state,
2769 					     test_bit(NFS4CLNT_RUN_MANAGER,
2770 						      &clp->cl_state));
2771 		if (!atomic_read(&cl->cl_swapper))
2772 			clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
2773 		if (refcount_read(&clp->cl_count) > 1 && !signalled() &&
2774 		    !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state))
2775 			goto again;
2776 		/* Either no longer a swapper, or were signalled */
2777 		clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
2778 	}
2779 
2780 	if (refcount_read(&clp->cl_count) > 1 && !signalled() &&
2781 	    test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state) &&
2782 	    !test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state))
2783 		goto again;
2784 
2785 	nfs_put_client(clp);
2786 	module_put_and_kthread_exit(0);
2787 	return 0;
2788 }
2789