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