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