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