xref: /openbmc/linux/fs/nfs/nfs4state.c (revision e53f276beb655c711a5d1f25f800b61aa976e34f)
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/nfs_idmap.h>
46 #include <linux/kthread.h>
47 #include <linux/module.h>
48 #include <linux/random.h>
49 #include <linux/ratelimit.h>
50 #include <linux/workqueue.h>
51 #include <linux/bitops.h>
52 
53 #include "nfs4_fs.h"
54 #include "callback.h"
55 #include "delegation.h"
56 #include "internal.h"
57 #include "pnfs.h"
58 
59 #define OPENOWNER_POOL_SIZE	8
60 
61 const nfs4_stateid zero_stateid;
62 
63 static LIST_HEAD(nfs4_clientid_list);
64 
65 int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
66 {
67 	struct nfs4_setclientid_res clid;
68 	unsigned short port;
69 	int status;
70 
71 	port = nfs_callback_tcpport;
72 	if (clp->cl_addr.ss_family == AF_INET6)
73 		port = nfs_callback_tcpport6;
74 
75 	status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
76 	if (status != 0)
77 		goto out;
78 	status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
79 	if (status != 0)
80 		goto out;
81 	clp->cl_clientid = clid.clientid;
82 	nfs4_schedule_state_renewal(clp);
83 out:
84 	return status;
85 }
86 
87 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
88 {
89 	struct rpc_cred *cred = NULL;
90 
91 	if (clp->cl_machine_cred != NULL)
92 		cred = get_rpccred(clp->cl_machine_cred);
93 	return cred;
94 }
95 
96 static void nfs4_clear_machine_cred(struct nfs_client *clp)
97 {
98 	struct rpc_cred *cred;
99 
100 	spin_lock(&clp->cl_lock);
101 	cred = clp->cl_machine_cred;
102 	clp->cl_machine_cred = NULL;
103 	spin_unlock(&clp->cl_lock);
104 	if (cred != NULL)
105 		put_rpccred(cred);
106 }
107 
108 static struct rpc_cred *
109 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
110 {
111 	struct rpc_cred *cred = NULL;
112 	struct nfs4_state_owner *sp;
113 	struct rb_node *pos;
114 
115 	for (pos = rb_first(&server->state_owners);
116 	     pos != NULL;
117 	     pos = rb_next(pos)) {
118 		sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
119 		if (list_empty(&sp->so_states))
120 			continue;
121 		cred = get_rpccred(sp->so_cred);
122 		break;
123 	}
124 	return cred;
125 }
126 
127 /**
128  * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
129  * @clp: client state handle
130  *
131  * Returns an rpc_cred with reference count bumped, or NULL.
132  * Caller must hold clp->cl_lock.
133  */
134 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
135 {
136 	struct rpc_cred *cred = NULL;
137 	struct nfs_server *server;
138 
139 	rcu_read_lock();
140 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
141 		cred = nfs4_get_renew_cred_server_locked(server);
142 		if (cred != NULL)
143 			break;
144 	}
145 	rcu_read_unlock();
146 	return cred;
147 }
148 
149 #if defined(CONFIG_NFS_V4_1)
150 
151 static int nfs41_setup_state_renewal(struct nfs_client *clp)
152 {
153 	int status;
154 	struct nfs_fsinfo fsinfo;
155 
156 	status = nfs4_proc_get_lease_time(clp, &fsinfo);
157 	if (status == 0) {
158 		/* Update lease time and schedule renewal */
159 		spin_lock(&clp->cl_lock);
160 		clp->cl_lease_time = fsinfo.lease_time * HZ;
161 		clp->cl_last_renewal = jiffies;
162 		spin_unlock(&clp->cl_lock);
163 
164 		nfs4_schedule_state_renewal(clp);
165 	}
166 
167 	return status;
168 }
169 
170 /*
171  * Back channel returns NFS4ERR_DELAY for new requests when
172  * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
173  * is ended.
174  */
175 static void nfs4_end_drain_session(struct nfs_client *clp)
176 {
177 	struct nfs4_session *ses = clp->cl_session;
178 	int max_slots;
179 
180 	if (ses == NULL)
181 		return;
182 	if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
183 		spin_lock(&ses->fc_slot_table.slot_tbl_lock);
184 		max_slots = ses->fc_slot_table.max_slots;
185 		while (max_slots--) {
186 			struct rpc_task *task;
187 
188 			task = rpc_wake_up_next(&ses->fc_slot_table.
189 						slot_tbl_waitq);
190 			if (!task)
191 				break;
192 			rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
193 		}
194 		spin_unlock(&ses->fc_slot_table.slot_tbl_lock);
195 	}
196 }
197 
198 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
199 {
200 	spin_lock(&tbl->slot_tbl_lock);
201 	if (tbl->highest_used_slotid != -1) {
202 		INIT_COMPLETION(tbl->complete);
203 		spin_unlock(&tbl->slot_tbl_lock);
204 		return wait_for_completion_interruptible(&tbl->complete);
205 	}
206 	spin_unlock(&tbl->slot_tbl_lock);
207 	return 0;
208 }
209 
210 static int nfs4_begin_drain_session(struct nfs_client *clp)
211 {
212 	struct nfs4_session *ses = clp->cl_session;
213 	int ret = 0;
214 
215 	set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
216 	/* back channel */
217 	ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
218 	if (ret)
219 		return ret;
220 	/* fore channel */
221 	return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
222 }
223 
224 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
225 {
226 	int status;
227 
228 	nfs4_begin_drain_session(clp);
229 	status = nfs4_proc_exchange_id(clp, cred);
230 	if (status != 0)
231 		goto out;
232 	status = nfs4_proc_create_session(clp);
233 	if (status != 0)
234 		goto out;
235 	status = nfs4_set_callback_sessionid(clp);
236 	if (status != 0) {
237 		printk(KERN_WARNING "Sessionid not set. No callback service\n");
238 		nfs_callback_down(1);
239 		status = 0;
240 	}
241 	nfs41_setup_state_renewal(clp);
242 	nfs_mark_client_ready(clp, NFS_CS_READY);
243 out:
244 	return status;
245 }
246 
247 struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
248 {
249 	struct rpc_cred *cred;
250 
251 	spin_lock(&clp->cl_lock);
252 	cred = nfs4_get_machine_cred_locked(clp);
253 	spin_unlock(&clp->cl_lock);
254 	return cred;
255 }
256 
257 #endif /* CONFIG_NFS_V4_1 */
258 
259 static struct rpc_cred *
260 nfs4_get_setclientid_cred_server(struct nfs_server *server)
261 {
262 	struct nfs_client *clp = server->nfs_client;
263 	struct rpc_cred *cred = NULL;
264 	struct nfs4_state_owner *sp;
265 	struct rb_node *pos;
266 
267 	spin_lock(&clp->cl_lock);
268 	pos = rb_first(&server->state_owners);
269 	if (pos != NULL) {
270 		sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
271 		cred = get_rpccred(sp->so_cred);
272 	}
273 	spin_unlock(&clp->cl_lock);
274 	return cred;
275 }
276 
277 /**
278  * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
279  * @clp: client state handle
280  *
281  * Returns an rpc_cred with reference count bumped, or NULL.
282  */
283 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
284 {
285 	struct nfs_server *server;
286 	struct rpc_cred *cred;
287 
288 	spin_lock(&clp->cl_lock);
289 	cred = nfs4_get_machine_cred_locked(clp);
290 	spin_unlock(&clp->cl_lock);
291 	if (cred != NULL)
292 		goto out;
293 
294 	rcu_read_lock();
295 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
296 		cred = nfs4_get_setclientid_cred_server(server);
297 		if (cred != NULL)
298 			break;
299 	}
300 	rcu_read_unlock();
301 
302 out:
303 	return cred;
304 }
305 
306 static void nfs_alloc_unique_id_locked(struct rb_root *root,
307 				       struct nfs_unique_id *new,
308 				       __u64 minval, int maxbits)
309 {
310 	struct rb_node **p, *parent;
311 	struct nfs_unique_id *pos;
312 	__u64 mask = ~0ULL;
313 
314 	if (maxbits < 64)
315 		mask = (1ULL << maxbits) - 1ULL;
316 
317 	/* Ensure distribution is more or less flat */
318 	get_random_bytes(&new->id, sizeof(new->id));
319 	new->id &= mask;
320 	if (new->id < minval)
321 		new->id += minval;
322 retry:
323 	p = &root->rb_node;
324 	parent = NULL;
325 
326 	while (*p != NULL) {
327 		parent = *p;
328 		pos = rb_entry(parent, struct nfs_unique_id, rb_node);
329 
330 		if (new->id < pos->id)
331 			p = &(*p)->rb_left;
332 		else if (new->id > pos->id)
333 			p = &(*p)->rb_right;
334 		else
335 			goto id_exists;
336 	}
337 	rb_link_node(&new->rb_node, parent, p);
338 	rb_insert_color(&new->rb_node, root);
339 	return;
340 id_exists:
341 	for (;;) {
342 		new->id++;
343 		if (new->id < minval || (new->id & mask) != new->id) {
344 			new->id = minval;
345 			break;
346 		}
347 		parent = rb_next(parent);
348 		if (parent == NULL)
349 			break;
350 		pos = rb_entry(parent, struct nfs_unique_id, rb_node);
351 		if (new->id < pos->id)
352 			break;
353 	}
354 	goto retry;
355 }
356 
357 static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
358 {
359 	rb_erase(&id->rb_node, root);
360 }
361 
362 static struct nfs4_state_owner *
363 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
364 {
365 	struct rb_node **p = &server->state_owners.rb_node,
366 		       *parent = NULL;
367 	struct nfs4_state_owner *sp, *res = NULL;
368 
369 	while (*p != NULL) {
370 		parent = *p;
371 		sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
372 
373 		if (server < sp->so_server) {
374 			p = &parent->rb_left;
375 			continue;
376 		}
377 		if (server > sp->so_server) {
378 			p = &parent->rb_right;
379 			continue;
380 		}
381 		if (cred < sp->so_cred)
382 			p = &parent->rb_left;
383 		else if (cred > sp->so_cred)
384 			p = &parent->rb_right;
385 		else {
386 			atomic_inc(&sp->so_count);
387 			res = sp;
388 			break;
389 		}
390 	}
391 	return res;
392 }
393 
394 static struct nfs4_state_owner *
395 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
396 {
397 	struct nfs_server *server = new->so_server;
398 	struct rb_node **p = &server->state_owners.rb_node,
399 		       *parent = NULL;
400 	struct nfs4_state_owner *sp;
401 
402 	while (*p != NULL) {
403 		parent = *p;
404 		sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
405 
406 		if (new->so_cred < sp->so_cred)
407 			p = &parent->rb_left;
408 		else if (new->so_cred > sp->so_cred)
409 			p = &parent->rb_right;
410 		else {
411 			atomic_inc(&sp->so_count);
412 			return sp;
413 		}
414 	}
415 	nfs_alloc_unique_id_locked(&server->openowner_id,
416 					&new->so_owner_id, 1, 64);
417 	rb_link_node(&new->so_server_node, parent, p);
418 	rb_insert_color(&new->so_server_node, &server->state_owners);
419 	return new;
420 }
421 
422 static void
423 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
424 {
425 	struct nfs_server *server = sp->so_server;
426 
427 	if (!RB_EMPTY_NODE(&sp->so_server_node))
428 		rb_erase(&sp->so_server_node, &server->state_owners);
429 	nfs_free_unique_id(&server->openowner_id, &sp->so_owner_id);
430 }
431 
432 /*
433  * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
434  * create a new state_owner.
435  *
436  */
437 static struct nfs4_state_owner *
438 nfs4_alloc_state_owner(void)
439 {
440 	struct nfs4_state_owner *sp;
441 
442 	sp = kzalloc(sizeof(*sp),GFP_NOFS);
443 	if (!sp)
444 		return NULL;
445 	spin_lock_init(&sp->so_lock);
446 	INIT_LIST_HEAD(&sp->so_states);
447 	rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
448 	sp->so_seqid.sequence = &sp->so_sequence;
449 	spin_lock_init(&sp->so_sequence.lock);
450 	INIT_LIST_HEAD(&sp->so_sequence.list);
451 	atomic_set(&sp->so_count, 1);
452 	return sp;
453 }
454 
455 static void
456 nfs4_drop_state_owner(struct nfs4_state_owner *sp)
457 {
458 	if (!RB_EMPTY_NODE(&sp->so_server_node)) {
459 		struct nfs_server *server = sp->so_server;
460 		struct nfs_client *clp = server->nfs_client;
461 
462 		spin_lock(&clp->cl_lock);
463 		rb_erase(&sp->so_server_node, &server->state_owners);
464 		RB_CLEAR_NODE(&sp->so_server_node);
465 		spin_unlock(&clp->cl_lock);
466 	}
467 }
468 
469 /**
470  * nfs4_get_state_owner - Look up a state owner given a credential
471  * @server: nfs_server to search
472  * @cred: RPC credential to match
473  *
474  * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
475  */
476 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
477 					      struct rpc_cred *cred)
478 {
479 	struct nfs_client *clp = server->nfs_client;
480 	struct nfs4_state_owner *sp, *new;
481 
482 	spin_lock(&clp->cl_lock);
483 	sp = nfs4_find_state_owner_locked(server, cred);
484 	spin_unlock(&clp->cl_lock);
485 	if (sp != NULL)
486 		return sp;
487 	new = nfs4_alloc_state_owner();
488 	if (new == NULL)
489 		return NULL;
490 	new->so_server = server;
491 	new->so_cred = cred;
492 	spin_lock(&clp->cl_lock);
493 	sp = nfs4_insert_state_owner_locked(new);
494 	spin_unlock(&clp->cl_lock);
495 	if (sp == new)
496 		get_rpccred(cred);
497 	else {
498 		rpc_destroy_wait_queue(&new->so_sequence.wait);
499 		kfree(new);
500 	}
501 	return sp;
502 }
503 
504 /**
505  * nfs4_put_state_owner - Release a nfs4_state_owner
506  * @sp: state owner data to release
507  *
508  */
509 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
510 {
511 	struct nfs_client *clp = sp->so_server->nfs_client;
512 	struct rpc_cred *cred = sp->so_cred;
513 
514 	if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
515 		return;
516 	nfs4_remove_state_owner_locked(sp);
517 	spin_unlock(&clp->cl_lock);
518 	rpc_destroy_wait_queue(&sp->so_sequence.wait);
519 	put_rpccred(cred);
520 	kfree(sp);
521 }
522 
523 static struct nfs4_state *
524 nfs4_alloc_open_state(void)
525 {
526 	struct nfs4_state *state;
527 
528 	state = kzalloc(sizeof(*state), GFP_NOFS);
529 	if (!state)
530 		return NULL;
531 	atomic_set(&state->count, 1);
532 	INIT_LIST_HEAD(&state->lock_states);
533 	spin_lock_init(&state->state_lock);
534 	seqlock_init(&state->seqlock);
535 	return state;
536 }
537 
538 void
539 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
540 {
541 	if (state->state == fmode)
542 		return;
543 	/* NB! List reordering - see the reclaim code for why.  */
544 	if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
545 		if (fmode & FMODE_WRITE)
546 			list_move(&state->open_states, &state->owner->so_states);
547 		else
548 			list_move_tail(&state->open_states, &state->owner->so_states);
549 	}
550 	state->state = fmode;
551 }
552 
553 static struct nfs4_state *
554 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
555 {
556 	struct nfs_inode *nfsi = NFS_I(inode);
557 	struct nfs4_state *state;
558 
559 	list_for_each_entry(state, &nfsi->open_states, inode_states) {
560 		if (state->owner != owner)
561 			continue;
562 		if (atomic_inc_not_zero(&state->count))
563 			return state;
564 	}
565 	return NULL;
566 }
567 
568 static void
569 nfs4_free_open_state(struct nfs4_state *state)
570 {
571 	kfree(state);
572 }
573 
574 struct nfs4_state *
575 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
576 {
577 	struct nfs4_state *state, *new;
578 	struct nfs_inode *nfsi = NFS_I(inode);
579 
580 	spin_lock(&inode->i_lock);
581 	state = __nfs4_find_state_byowner(inode, owner);
582 	spin_unlock(&inode->i_lock);
583 	if (state)
584 		goto out;
585 	new = nfs4_alloc_open_state();
586 	spin_lock(&owner->so_lock);
587 	spin_lock(&inode->i_lock);
588 	state = __nfs4_find_state_byowner(inode, owner);
589 	if (state == NULL && new != NULL) {
590 		state = new;
591 		state->owner = owner;
592 		atomic_inc(&owner->so_count);
593 		list_add(&state->inode_states, &nfsi->open_states);
594 		state->inode = igrab(inode);
595 		spin_unlock(&inode->i_lock);
596 		/* Note: The reclaim code dictates that we add stateless
597 		 * and read-only stateids to the end of the list */
598 		list_add_tail(&state->open_states, &owner->so_states);
599 		spin_unlock(&owner->so_lock);
600 	} else {
601 		spin_unlock(&inode->i_lock);
602 		spin_unlock(&owner->so_lock);
603 		if (new)
604 			nfs4_free_open_state(new);
605 	}
606 out:
607 	return state;
608 }
609 
610 void nfs4_put_open_state(struct nfs4_state *state)
611 {
612 	struct inode *inode = state->inode;
613 	struct nfs4_state_owner *owner = state->owner;
614 
615 	if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
616 		return;
617 	spin_lock(&inode->i_lock);
618 	list_del(&state->inode_states);
619 	list_del(&state->open_states);
620 	spin_unlock(&inode->i_lock);
621 	spin_unlock(&owner->so_lock);
622 	iput(inode);
623 	nfs4_free_open_state(state);
624 	nfs4_put_state_owner(owner);
625 }
626 
627 /*
628  * Close the current file.
629  */
630 static void __nfs4_close(struct path *path, struct nfs4_state *state,
631 		fmode_t fmode, gfp_t gfp_mask, int wait)
632 {
633 	struct nfs4_state_owner *owner = state->owner;
634 	int call_close = 0;
635 	fmode_t newstate;
636 
637 	atomic_inc(&owner->so_count);
638 	/* Protect against nfs4_find_state() */
639 	spin_lock(&owner->so_lock);
640 	switch (fmode & (FMODE_READ | FMODE_WRITE)) {
641 		case FMODE_READ:
642 			state->n_rdonly--;
643 			break;
644 		case FMODE_WRITE:
645 			state->n_wronly--;
646 			break;
647 		case FMODE_READ|FMODE_WRITE:
648 			state->n_rdwr--;
649 	}
650 	newstate = FMODE_READ|FMODE_WRITE;
651 	if (state->n_rdwr == 0) {
652 		if (state->n_rdonly == 0) {
653 			newstate &= ~FMODE_READ;
654 			call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
655 			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
656 		}
657 		if (state->n_wronly == 0) {
658 			newstate &= ~FMODE_WRITE;
659 			call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
660 			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
661 		}
662 		if (newstate == 0)
663 			clear_bit(NFS_DELEGATED_STATE, &state->flags);
664 	}
665 	nfs4_state_set_mode_locked(state, newstate);
666 	spin_unlock(&owner->so_lock);
667 
668 	if (!call_close) {
669 		nfs4_put_open_state(state);
670 		nfs4_put_state_owner(owner);
671 	} else {
672 		bool roc = pnfs_roc(state->inode);
673 
674 		nfs4_do_close(path, state, gfp_mask, wait, roc);
675 	}
676 }
677 
678 void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
679 {
680 	__nfs4_close(path, state, fmode, GFP_NOFS, 0);
681 }
682 
683 void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
684 {
685 	__nfs4_close(path, state, fmode, GFP_KERNEL, 1);
686 }
687 
688 /*
689  * Search the state->lock_states for an existing lock_owner
690  * that is compatible with current->files
691  */
692 static struct nfs4_lock_state *
693 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
694 {
695 	struct nfs4_lock_state *pos;
696 	list_for_each_entry(pos, &state->lock_states, ls_locks) {
697 		if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
698 			continue;
699 		switch (pos->ls_owner.lo_type) {
700 		case NFS4_POSIX_LOCK_TYPE:
701 			if (pos->ls_owner.lo_u.posix_owner != fl_owner)
702 				continue;
703 			break;
704 		case NFS4_FLOCK_LOCK_TYPE:
705 			if (pos->ls_owner.lo_u.flock_owner != fl_pid)
706 				continue;
707 		}
708 		atomic_inc(&pos->ls_count);
709 		return pos;
710 	}
711 	return NULL;
712 }
713 
714 /*
715  * Return a compatible lock_state. If no initialized lock_state structure
716  * exists, return an uninitialized one.
717  *
718  */
719 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
720 {
721 	struct nfs4_lock_state *lsp;
722 	struct nfs_server *server = state->owner->so_server;
723 	struct nfs_client *clp = server->nfs_client;
724 
725 	lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
726 	if (lsp == NULL)
727 		return NULL;
728 	rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
729 	spin_lock_init(&lsp->ls_sequence.lock);
730 	INIT_LIST_HEAD(&lsp->ls_sequence.list);
731 	lsp->ls_seqid.sequence = &lsp->ls_sequence;
732 	atomic_set(&lsp->ls_count, 1);
733 	lsp->ls_state = state;
734 	lsp->ls_owner.lo_type = type;
735 	switch (lsp->ls_owner.lo_type) {
736 	case NFS4_FLOCK_LOCK_TYPE:
737 		lsp->ls_owner.lo_u.flock_owner = fl_pid;
738 		break;
739 	case NFS4_POSIX_LOCK_TYPE:
740 		lsp->ls_owner.lo_u.posix_owner = fl_owner;
741 		break;
742 	default:
743 		kfree(lsp);
744 		return NULL;
745 	}
746 	spin_lock(&clp->cl_lock);
747 	nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
748 	spin_unlock(&clp->cl_lock);
749 	INIT_LIST_HEAD(&lsp->ls_locks);
750 	return lsp;
751 }
752 
753 static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
754 {
755 	struct nfs_server *server = lsp->ls_state->owner->so_server;
756 	struct nfs_client *clp = server->nfs_client;
757 
758 	spin_lock(&clp->cl_lock);
759 	nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
760 	spin_unlock(&clp->cl_lock);
761 	rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
762 	kfree(lsp);
763 }
764 
765 /*
766  * Return a compatible lock_state. If no initialized lock_state structure
767  * exists, return an uninitialized one.
768  *
769  */
770 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
771 {
772 	struct nfs4_lock_state *lsp, *new = NULL;
773 
774 	for(;;) {
775 		spin_lock(&state->state_lock);
776 		lsp = __nfs4_find_lock_state(state, owner, pid, type);
777 		if (lsp != NULL)
778 			break;
779 		if (new != NULL) {
780 			list_add(&new->ls_locks, &state->lock_states);
781 			set_bit(LK_STATE_IN_USE, &state->flags);
782 			lsp = new;
783 			new = NULL;
784 			break;
785 		}
786 		spin_unlock(&state->state_lock);
787 		new = nfs4_alloc_lock_state(state, owner, pid, type);
788 		if (new == NULL)
789 			return NULL;
790 	}
791 	spin_unlock(&state->state_lock);
792 	if (new != NULL)
793 		nfs4_free_lock_state(new);
794 	return lsp;
795 }
796 
797 /*
798  * Release reference to lock_state, and free it if we see that
799  * it is no longer in use
800  */
801 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
802 {
803 	struct nfs4_state *state;
804 
805 	if (lsp == NULL)
806 		return;
807 	state = lsp->ls_state;
808 	if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
809 		return;
810 	list_del(&lsp->ls_locks);
811 	if (list_empty(&state->lock_states))
812 		clear_bit(LK_STATE_IN_USE, &state->flags);
813 	spin_unlock(&state->state_lock);
814 	if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
815 		nfs4_release_lockowner(lsp);
816 	nfs4_free_lock_state(lsp);
817 }
818 
819 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
820 {
821 	struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
822 
823 	dst->fl_u.nfs4_fl.owner = lsp;
824 	atomic_inc(&lsp->ls_count);
825 }
826 
827 static void nfs4_fl_release_lock(struct file_lock *fl)
828 {
829 	nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
830 }
831 
832 static const struct file_lock_operations nfs4_fl_lock_ops = {
833 	.fl_copy_lock = nfs4_fl_copy_lock,
834 	.fl_release_private = nfs4_fl_release_lock,
835 };
836 
837 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
838 {
839 	struct nfs4_lock_state *lsp;
840 
841 	if (fl->fl_ops != NULL)
842 		return 0;
843 	if (fl->fl_flags & FL_POSIX)
844 		lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
845 	else if (fl->fl_flags & FL_FLOCK)
846 		lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
847 	else
848 		return -EINVAL;
849 	if (lsp == NULL)
850 		return -ENOMEM;
851 	fl->fl_u.nfs4_fl.owner = lsp;
852 	fl->fl_ops = &nfs4_fl_lock_ops;
853 	return 0;
854 }
855 
856 /*
857  * Byte-range lock aware utility to initialize the stateid of read/write
858  * requests.
859  */
860 void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid)
861 {
862 	struct nfs4_lock_state *lsp;
863 	int seq;
864 
865 	do {
866 		seq = read_seqbegin(&state->seqlock);
867 		memcpy(dst, &state->stateid, sizeof(*dst));
868 	} while (read_seqretry(&state->seqlock, seq));
869 	if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
870 		return;
871 
872 	spin_lock(&state->state_lock);
873 	lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
874 	if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
875 		memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
876 	spin_unlock(&state->state_lock);
877 	nfs4_put_lock_state(lsp);
878 }
879 
880 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
881 {
882 	struct nfs_seqid *new;
883 
884 	new = kmalloc(sizeof(*new), gfp_mask);
885 	if (new != NULL) {
886 		new->sequence = counter;
887 		INIT_LIST_HEAD(&new->list);
888 	}
889 	return new;
890 }
891 
892 void nfs_release_seqid(struct nfs_seqid *seqid)
893 {
894 	if (!list_empty(&seqid->list)) {
895 		struct rpc_sequence *sequence = seqid->sequence->sequence;
896 
897 		spin_lock(&sequence->lock);
898 		list_del_init(&seqid->list);
899 		spin_unlock(&sequence->lock);
900 		rpc_wake_up(&sequence->wait);
901 	}
902 }
903 
904 void nfs_free_seqid(struct nfs_seqid *seqid)
905 {
906 	nfs_release_seqid(seqid);
907 	kfree(seqid);
908 }
909 
910 /*
911  * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
912  * failed with a seqid incrementing error -
913  * see comments nfs_fs.h:seqid_mutating_error()
914  */
915 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
916 {
917 	BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
918 	switch (status) {
919 		case 0:
920 			break;
921 		case -NFS4ERR_BAD_SEQID:
922 			if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
923 				return;
924 			printk(KERN_WARNING "NFS: v4 server returned a bad"
925 					" sequence-id error on an"
926 					" unconfirmed sequence %p!\n",
927 					seqid->sequence);
928 		case -NFS4ERR_STALE_CLIENTID:
929 		case -NFS4ERR_STALE_STATEID:
930 		case -NFS4ERR_BAD_STATEID:
931 		case -NFS4ERR_BADXDR:
932 		case -NFS4ERR_RESOURCE:
933 		case -NFS4ERR_NOFILEHANDLE:
934 			/* Non-seqid mutating errors */
935 			return;
936 	};
937 	/*
938 	 * Note: no locking needed as we are guaranteed to be first
939 	 * on the sequence list
940 	 */
941 	seqid->sequence->counter++;
942 }
943 
944 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
945 {
946 	struct nfs4_state_owner *sp = container_of(seqid->sequence,
947 					struct nfs4_state_owner, so_seqid);
948 	struct nfs_server *server = sp->so_server;
949 
950 	if (status == -NFS4ERR_BAD_SEQID)
951 		nfs4_drop_state_owner(sp);
952 	if (!nfs4_has_session(server->nfs_client))
953 		nfs_increment_seqid(status, seqid);
954 }
955 
956 /*
957  * Increment the seqid if the LOCK/LOCKU succeeded, or
958  * failed with a seqid incrementing error -
959  * see comments nfs_fs.h:seqid_mutating_error()
960  */
961 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
962 {
963 	nfs_increment_seqid(status, seqid);
964 }
965 
966 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
967 {
968 	struct rpc_sequence *sequence = seqid->sequence->sequence;
969 	int status = 0;
970 
971 	spin_lock(&sequence->lock);
972 	if (list_empty(&seqid->list))
973 		list_add_tail(&seqid->list, &sequence->list);
974 	if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
975 		goto unlock;
976 	rpc_sleep_on(&sequence->wait, task, NULL);
977 	status = -EAGAIN;
978 unlock:
979 	spin_unlock(&sequence->lock);
980 	return status;
981 }
982 
983 static int nfs4_run_state_manager(void *);
984 
985 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
986 {
987 	smp_mb__before_clear_bit();
988 	clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
989 	smp_mb__after_clear_bit();
990 	wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
991 	rpc_wake_up(&clp->cl_rpcwaitq);
992 }
993 
994 /*
995  * Schedule the nfs_client asynchronous state management routine
996  */
997 void nfs4_schedule_state_manager(struct nfs_client *clp)
998 {
999 	struct task_struct *task;
1000 
1001 	if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1002 		return;
1003 	__module_get(THIS_MODULE);
1004 	atomic_inc(&clp->cl_count);
1005 	task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
1006 				rpc_peeraddr2str(clp->cl_rpcclient,
1007 							RPC_DISPLAY_ADDR));
1008 	if (!IS_ERR(task))
1009 		return;
1010 	nfs4_clear_state_manager_bit(clp);
1011 	nfs_put_client(clp);
1012 	module_put(THIS_MODULE);
1013 }
1014 
1015 /*
1016  * Schedule a state recovery attempt
1017  */
1018 void nfs4_schedule_state_recovery(struct nfs_client *clp)
1019 {
1020 	if (!clp)
1021 		return;
1022 	if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1023 		set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1024 	nfs4_schedule_state_manager(clp);
1025 }
1026 
1027 int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1028 {
1029 
1030 	set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1031 	/* Don't recover state that expired before the reboot */
1032 	if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1033 		clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1034 		return 0;
1035 	}
1036 	set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1037 	set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1038 	return 1;
1039 }
1040 
1041 int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1042 {
1043 	set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1044 	clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1045 	set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1046 	set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1047 	return 1;
1048 }
1049 
1050 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1051 {
1052 	struct inode *inode = state->inode;
1053 	struct nfs_inode *nfsi = NFS_I(inode);
1054 	struct file_lock *fl;
1055 	int status = 0;
1056 
1057 	if (inode->i_flock == NULL)
1058 		return 0;
1059 
1060 	/* Guard against delegation returns and new lock/unlock calls */
1061 	down_write(&nfsi->rwsem);
1062 	/* Protect inode->i_flock using the BKL */
1063 	lock_flocks();
1064 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1065 		if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1066 			continue;
1067 		if (nfs_file_open_context(fl->fl_file)->state != state)
1068 			continue;
1069 		unlock_flocks();
1070 		status = ops->recover_lock(state, fl);
1071 		switch (status) {
1072 			case 0:
1073 				break;
1074 			case -ESTALE:
1075 			case -NFS4ERR_ADMIN_REVOKED:
1076 			case -NFS4ERR_STALE_STATEID:
1077 			case -NFS4ERR_BAD_STATEID:
1078 			case -NFS4ERR_EXPIRED:
1079 			case -NFS4ERR_NO_GRACE:
1080 			case -NFS4ERR_STALE_CLIENTID:
1081 			case -NFS4ERR_BADSESSION:
1082 			case -NFS4ERR_BADSLOT:
1083 			case -NFS4ERR_BAD_HIGH_SLOT:
1084 			case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1085 				goto out;
1086 			default:
1087 				printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
1088 						__func__, status);
1089 			case -ENOMEM:
1090 			case -NFS4ERR_DENIED:
1091 			case -NFS4ERR_RECLAIM_BAD:
1092 			case -NFS4ERR_RECLAIM_CONFLICT:
1093 				/* kill_proc(fl->fl_pid, SIGLOST, 1); */
1094 				status = 0;
1095 		}
1096 		lock_flocks();
1097 	}
1098 	unlock_flocks();
1099 out:
1100 	up_write(&nfsi->rwsem);
1101 	return status;
1102 }
1103 
1104 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1105 {
1106 	struct nfs4_state *state;
1107 	struct nfs4_lock_state *lock;
1108 	int status = 0;
1109 
1110 	/* Note: we rely on the sp->so_states list being ordered
1111 	 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1112 	 * states first.
1113 	 * This is needed to ensure that the server won't give us any
1114 	 * read delegations that we have to return if, say, we are
1115 	 * recovering after a network partition or a reboot from a
1116 	 * server that doesn't support a grace period.
1117 	 */
1118 restart:
1119 	spin_lock(&sp->so_lock);
1120 	list_for_each_entry(state, &sp->so_states, open_states) {
1121 		if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1122 			continue;
1123 		if (state->state == 0)
1124 			continue;
1125 		atomic_inc(&state->count);
1126 		spin_unlock(&sp->so_lock);
1127 		status = ops->recover_open(sp, state);
1128 		if (status >= 0) {
1129 			status = nfs4_reclaim_locks(state, ops);
1130 			if (status >= 0) {
1131 				list_for_each_entry(lock, &state->lock_states, ls_locks) {
1132 					if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
1133 						printk("%s: Lock reclaim failed!\n",
1134 							__func__);
1135 				}
1136 				nfs4_put_open_state(state);
1137 				goto restart;
1138 			}
1139 		}
1140 		switch (status) {
1141 			default:
1142 				printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
1143 						__func__, status);
1144 			case -ENOENT:
1145 			case -ENOMEM:
1146 			case -ESTALE:
1147 				/*
1148 				 * Open state on this file cannot be recovered
1149 				 * All we can do is revert to using the zero stateid.
1150 				 */
1151 				memset(state->stateid.data, 0,
1152 					sizeof(state->stateid.data));
1153 				/* Mark the file as being 'closed' */
1154 				state->state = 0;
1155 				break;
1156 			case -EKEYEXPIRED:
1157 				/*
1158 				 * User RPCSEC_GSS context has expired.
1159 				 * We cannot recover this stateid now, so
1160 				 * skip it and allow recovery thread to
1161 				 * proceed.
1162 				 */
1163 				break;
1164 			case -NFS4ERR_ADMIN_REVOKED:
1165 			case -NFS4ERR_STALE_STATEID:
1166 			case -NFS4ERR_BAD_STATEID:
1167 			case -NFS4ERR_RECLAIM_BAD:
1168 			case -NFS4ERR_RECLAIM_CONFLICT:
1169 				nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1170 				break;
1171 			case -NFS4ERR_EXPIRED:
1172 			case -NFS4ERR_NO_GRACE:
1173 				nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1174 			case -NFS4ERR_STALE_CLIENTID:
1175 			case -NFS4ERR_BADSESSION:
1176 			case -NFS4ERR_BADSLOT:
1177 			case -NFS4ERR_BAD_HIGH_SLOT:
1178 			case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1179 				goto out_err;
1180 		}
1181 		nfs4_put_open_state(state);
1182 		goto restart;
1183 	}
1184 	spin_unlock(&sp->so_lock);
1185 	return 0;
1186 out_err:
1187 	nfs4_put_open_state(state);
1188 	return status;
1189 }
1190 
1191 static void nfs4_clear_open_state(struct nfs4_state *state)
1192 {
1193 	struct nfs4_lock_state *lock;
1194 
1195 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
1196 	clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1197 	clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1198 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1199 	list_for_each_entry(lock, &state->lock_states, ls_locks) {
1200 		lock->ls_seqid.flags = 0;
1201 		lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
1202 	}
1203 }
1204 
1205 static void nfs4_reset_seqids(struct nfs_server *server,
1206 	int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1207 {
1208 	struct nfs_client *clp = server->nfs_client;
1209 	struct nfs4_state_owner *sp;
1210 	struct rb_node *pos;
1211 	struct nfs4_state *state;
1212 
1213 	spin_lock(&clp->cl_lock);
1214 	for (pos = rb_first(&server->state_owners);
1215 	     pos != NULL;
1216 	     pos = rb_next(pos)) {
1217 		sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1218 		sp->so_seqid.flags = 0;
1219 		spin_lock(&sp->so_lock);
1220 		list_for_each_entry(state, &sp->so_states, open_states) {
1221 			if (mark_reclaim(clp, state))
1222 				nfs4_clear_open_state(state);
1223 		}
1224 		spin_unlock(&sp->so_lock);
1225 	}
1226 	spin_unlock(&clp->cl_lock);
1227 }
1228 
1229 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1230 	int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1231 {
1232 	struct nfs_server *server;
1233 
1234 	rcu_read_lock();
1235 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1236 		nfs4_reset_seqids(server, mark_reclaim);
1237 	rcu_read_unlock();
1238 }
1239 
1240 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1241 {
1242 	/* Mark all delegations for reclaim */
1243 	nfs_delegation_mark_reclaim(clp);
1244 	nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1245 }
1246 
1247 static void nfs4_reclaim_complete(struct nfs_client *clp,
1248 				 const struct nfs4_state_recovery_ops *ops)
1249 {
1250 	/* Notify the server we're done reclaiming our state */
1251 	if (ops->reclaim_complete)
1252 		(void)ops->reclaim_complete(clp);
1253 }
1254 
1255 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1256 {
1257 	struct nfs_client *clp = server->nfs_client;
1258 	struct nfs4_state_owner *sp;
1259 	struct rb_node *pos;
1260 	struct nfs4_state *state;
1261 
1262 	spin_lock(&clp->cl_lock);
1263 	for (pos = rb_first(&server->state_owners);
1264 	     pos != NULL;
1265 	     pos = rb_next(pos)) {
1266 		sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1267 		spin_lock(&sp->so_lock);
1268 		list_for_each_entry(state, &sp->so_states, open_states) {
1269 			if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1270 						&state->flags))
1271 				continue;
1272 			nfs4_state_mark_reclaim_nograce(clp, state);
1273 		}
1274 		spin_unlock(&sp->so_lock);
1275 	}
1276 	spin_unlock(&clp->cl_lock);
1277 }
1278 
1279 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1280 {
1281 	struct nfs_server *server;
1282 
1283 	if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1284 		return 0;
1285 
1286 	rcu_read_lock();
1287 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1288 		nfs4_clear_reclaim_server(server);
1289 	rcu_read_unlock();
1290 
1291 	nfs_delegation_reap_unclaimed(clp);
1292 	return 1;
1293 }
1294 
1295 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1296 {
1297 	if (!nfs4_state_clear_reclaim_reboot(clp))
1298 		return;
1299 	nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
1300 }
1301 
1302 static void nfs_delegation_clear_all(struct nfs_client *clp)
1303 {
1304 	nfs_delegation_mark_reclaim(clp);
1305 	nfs_delegation_reap_unclaimed(clp);
1306 }
1307 
1308 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1309 {
1310 	nfs_delegation_clear_all(clp);
1311 	nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1312 }
1313 
1314 static void nfs4_warn_keyexpired(const char *s)
1315 {
1316 	printk_ratelimited(KERN_WARNING "Error: state manager"
1317 			" encountered RPCSEC_GSS session"
1318 			" expired against NFSv4 server %s.\n",
1319 			s);
1320 }
1321 
1322 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1323 {
1324 	switch (error) {
1325 		case -NFS4ERR_CB_PATH_DOWN:
1326 			nfs_handle_cb_pathdown(clp);
1327 			return 0;
1328 		case -NFS4ERR_NO_GRACE:
1329 			nfs4_state_end_reclaim_reboot(clp);
1330 			return 0;
1331 		case -NFS4ERR_STALE_CLIENTID:
1332 		case -NFS4ERR_LEASE_MOVED:
1333 			set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1334 			nfs4_state_clear_reclaim_reboot(clp);
1335 			nfs4_state_start_reclaim_reboot(clp);
1336 			break;
1337 		case -NFS4ERR_EXPIRED:
1338 			set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1339 			nfs4_state_start_reclaim_nograce(clp);
1340 			break;
1341 		case -NFS4ERR_BADSESSION:
1342 		case -NFS4ERR_BADSLOT:
1343 		case -NFS4ERR_BAD_HIGH_SLOT:
1344 		case -NFS4ERR_DEADSESSION:
1345 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1346 		case -NFS4ERR_SEQ_FALSE_RETRY:
1347 		case -NFS4ERR_SEQ_MISORDERED:
1348 			set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1349 			/* Zero session reset errors */
1350 			return 0;
1351 		case -EKEYEXPIRED:
1352 			/* Nothing we can do */
1353 			nfs4_warn_keyexpired(clp->cl_hostname);
1354 			return 0;
1355 	}
1356 	return error;
1357 }
1358 
1359 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1360 {
1361 	struct nfs4_state_owner *sp;
1362 	struct nfs_server *server;
1363 	struct rb_node *pos;
1364 	int status = 0;
1365 
1366 restart:
1367 	rcu_read_lock();
1368 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1369 		spin_lock(&clp->cl_lock);
1370 		for (pos = rb_first(&server->state_owners);
1371 		     pos != NULL;
1372 		     pos = rb_next(pos)) {
1373 			sp = rb_entry(pos,
1374 				struct nfs4_state_owner, so_server_node);
1375 			if (!test_and_clear_bit(ops->owner_flag_bit,
1376 							&sp->so_flags))
1377 				continue;
1378 			atomic_inc(&sp->so_count);
1379 			spin_unlock(&clp->cl_lock);
1380 			rcu_read_unlock();
1381 
1382 			status = nfs4_reclaim_open_state(sp, ops);
1383 			if (status < 0) {
1384 				set_bit(ops->owner_flag_bit, &sp->so_flags);
1385 				nfs4_put_state_owner(sp);
1386 				return nfs4_recovery_handle_error(clp, status);
1387 			}
1388 
1389 			nfs4_put_state_owner(sp);
1390 			goto restart;
1391 		}
1392 		spin_unlock(&clp->cl_lock);
1393 	}
1394 	rcu_read_unlock();
1395 	return status;
1396 }
1397 
1398 static int nfs4_check_lease(struct nfs_client *clp)
1399 {
1400 	struct rpc_cred *cred;
1401 	const struct nfs4_state_maintenance_ops *ops =
1402 		clp->cl_mvops->state_renewal_ops;
1403 	int status = -NFS4ERR_EXPIRED;
1404 
1405 	/* Is the client already known to have an expired lease? */
1406 	if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1407 		return 0;
1408 	spin_lock(&clp->cl_lock);
1409 	cred = ops->get_state_renewal_cred_locked(clp);
1410 	spin_unlock(&clp->cl_lock);
1411 	if (cred == NULL) {
1412 		cred = nfs4_get_setclientid_cred(clp);
1413 		if (cred == NULL)
1414 			goto out;
1415 	}
1416 	status = ops->renew_lease(clp, cred);
1417 	put_rpccred(cred);
1418 out:
1419 	return nfs4_recovery_handle_error(clp, status);
1420 }
1421 
1422 static int nfs4_reclaim_lease(struct nfs_client *clp)
1423 {
1424 	struct rpc_cred *cred;
1425 	const struct nfs4_state_recovery_ops *ops =
1426 		clp->cl_mvops->reboot_recovery_ops;
1427 	int status = -ENOENT;
1428 
1429 	cred = ops->get_clid_cred(clp);
1430 	if (cred != NULL) {
1431 		status = ops->establish_clid(clp, cred);
1432 		put_rpccred(cred);
1433 		/* Handle case where the user hasn't set up machine creds */
1434 		if (status == -EACCES && cred == clp->cl_machine_cred) {
1435 			nfs4_clear_machine_cred(clp);
1436 			status = -EAGAIN;
1437 		}
1438 		if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
1439 			status = -EPROTONOSUPPORT;
1440 	}
1441 	return status;
1442 }
1443 
1444 #ifdef CONFIG_NFS_V4_1
1445 void nfs41_handle_recall_slot(struct nfs_client *clp)
1446 {
1447 	set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1448 	nfs4_schedule_state_recovery(clp);
1449 }
1450 
1451 static void nfs4_reset_all_state(struct nfs_client *clp)
1452 {
1453 	if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1454 		clp->cl_boot_time = CURRENT_TIME;
1455 		nfs4_state_start_reclaim_nograce(clp);
1456 		nfs4_schedule_state_recovery(clp);
1457 	}
1458 }
1459 
1460 static void nfs41_handle_server_reboot(struct nfs_client *clp)
1461 {
1462 	if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1463 		nfs4_state_start_reclaim_reboot(clp);
1464 		nfs4_schedule_state_recovery(clp);
1465 	}
1466 }
1467 
1468 static void nfs41_handle_state_revoked(struct nfs_client *clp)
1469 {
1470 	/* Temporary */
1471 	nfs4_reset_all_state(clp);
1472 }
1473 
1474 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1475 {
1476 	/* This will need to handle layouts too */
1477 	nfs_expire_all_delegations(clp);
1478 }
1479 
1480 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1481 {
1482 	nfs_expire_all_delegations(clp);
1483 	if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
1484 		nfs4_schedule_state_recovery(clp);
1485 }
1486 
1487 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1488 {
1489 	if (!flags)
1490 		return;
1491 	else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1492 		nfs41_handle_server_reboot(clp);
1493 	else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
1494 			    SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1495 			    SEQ4_STATUS_ADMIN_STATE_REVOKED |
1496 			    SEQ4_STATUS_LEASE_MOVED))
1497 		nfs41_handle_state_revoked(clp);
1498 	else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1499 		nfs41_handle_recallable_state_revoked(clp);
1500 	else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
1501 			    SEQ4_STATUS_BACKCHANNEL_FAULT |
1502 			    SEQ4_STATUS_CB_PATH_DOWN_SESSION))
1503 		nfs41_handle_cb_path_down(clp);
1504 }
1505 
1506 static int nfs4_reset_session(struct nfs_client *clp)
1507 {
1508 	int status;
1509 
1510 	nfs4_begin_drain_session(clp);
1511 	status = nfs4_proc_destroy_session(clp->cl_session);
1512 	if (status && status != -NFS4ERR_BADSESSION &&
1513 	    status != -NFS4ERR_DEADSESSION) {
1514 		status = nfs4_recovery_handle_error(clp, status);
1515 		goto out;
1516 	}
1517 
1518 	memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
1519 	status = nfs4_proc_create_session(clp);
1520 	if (status) {
1521 		status = nfs4_recovery_handle_error(clp, status);
1522 		goto out;
1523 	}
1524 	/* create_session negotiated new slot table */
1525 	clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1526 
1527 	 /* Let the state manager reestablish state */
1528 	if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1529 		nfs41_setup_state_renewal(clp);
1530 out:
1531 	return status;
1532 }
1533 
1534 static int nfs4_recall_slot(struct nfs_client *clp)
1535 {
1536 	struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
1537 	struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
1538 	struct nfs4_slot *new, *old;
1539 	int i;
1540 
1541 	nfs4_begin_drain_session(clp);
1542 	new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
1543 		      GFP_NOFS);
1544         if (!new)
1545 		return -ENOMEM;
1546 
1547 	spin_lock(&fc_tbl->slot_tbl_lock);
1548 	for (i = 0; i < fc_tbl->target_max_slots; i++)
1549 		new[i].seq_nr = fc_tbl->slots[i].seq_nr;
1550 	old = fc_tbl->slots;
1551 	fc_tbl->slots = new;
1552 	fc_tbl->max_slots = fc_tbl->target_max_slots;
1553 	fc_tbl->target_max_slots = 0;
1554 	fc_attrs->max_reqs = fc_tbl->max_slots;
1555 	spin_unlock(&fc_tbl->slot_tbl_lock);
1556 
1557 	kfree(old);
1558 	nfs4_end_drain_session(clp);
1559 	return 0;
1560 }
1561 
1562 #else /* CONFIG_NFS_V4_1 */
1563 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
1564 static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
1565 static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
1566 #endif /* CONFIG_NFS_V4_1 */
1567 
1568 /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1569  * on EXCHANGE_ID for v4.1
1570  */
1571 static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
1572 {
1573 	if (nfs4_has_session(clp)) {
1574 		switch (status) {
1575 		case -NFS4ERR_DELAY:
1576 		case -NFS4ERR_CLID_INUSE:
1577 		case -EAGAIN:
1578 			break;
1579 
1580 		case -EKEYEXPIRED:
1581 			nfs4_warn_keyexpired(clp->cl_hostname);
1582 		case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1583 					 * in nfs4_exchange_id */
1584 		default:
1585 			return;
1586 		}
1587 	}
1588 	set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1589 }
1590 
1591 static void nfs4_state_manager(struct nfs_client *clp)
1592 {
1593 	int status = 0;
1594 
1595 	/* Ensure exclusive access to NFSv4 state */
1596 	for(;;) {
1597 		if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
1598 			/* We're going to have to re-establish a clientid */
1599 			status = nfs4_reclaim_lease(clp);
1600 			if (status) {
1601 				nfs4_set_lease_expired(clp, status);
1602 				if (test_bit(NFS4CLNT_LEASE_EXPIRED,
1603 							&clp->cl_state))
1604 					continue;
1605 				if (clp->cl_cons_state ==
1606 							NFS_CS_SESSION_INITING)
1607 					nfs_mark_client_ready(clp, status);
1608 				goto out_error;
1609 			}
1610 			clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1611 			set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1612 			pnfs_destroy_all_layouts(clp);
1613 		}
1614 
1615 		if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
1616 			status = nfs4_check_lease(clp);
1617 			if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1618 				continue;
1619 			if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
1620 				goto out_error;
1621 		}
1622 
1623 		/* Initialize or reset the session */
1624 		if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
1625 		   && nfs4_has_session(clp)) {
1626 			status = nfs4_reset_session(clp);
1627 			if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1628 				continue;
1629 			if (status < 0)
1630 				goto out_error;
1631 		}
1632 
1633 		/* First recover reboot state... */
1634 		if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
1635 			status = nfs4_do_reclaim(clp,
1636 				clp->cl_mvops->reboot_recovery_ops);
1637 			if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
1638 			    test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
1639 				continue;
1640 			nfs4_state_end_reclaim_reboot(clp);
1641 			if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1642 				continue;
1643 			if (status < 0)
1644 				goto out_error;
1645 		}
1646 
1647 		/* Now recover expired state... */
1648 		if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
1649 			status = nfs4_do_reclaim(clp,
1650 				clp->cl_mvops->nograce_recovery_ops);
1651 			if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
1652 			    test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
1653 			    test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1654 				continue;
1655 			if (status < 0)
1656 				goto out_error;
1657 		}
1658 
1659 		nfs4_end_drain_session(clp);
1660 		if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
1661 			nfs_client_return_marked_delegations(clp);
1662 			continue;
1663 		}
1664 		/* Recall session slots */
1665 		if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
1666 		   && nfs4_has_session(clp)) {
1667 			status = nfs4_recall_slot(clp);
1668 			if (status < 0)
1669 				goto out_error;
1670 			continue;
1671 		}
1672 
1673 
1674 		nfs4_clear_state_manager_bit(clp);
1675 		/* Did we race with an attempt to give us more work? */
1676 		if (clp->cl_state == 0)
1677 			break;
1678 		if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1679 			break;
1680 	}
1681 	return;
1682 out_error:
1683 	printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
1684 			" with error %d\n", clp->cl_hostname, -status);
1685 	nfs4_end_drain_session(clp);
1686 	nfs4_clear_state_manager_bit(clp);
1687 }
1688 
1689 static int nfs4_run_state_manager(void *ptr)
1690 {
1691 	struct nfs_client *clp = ptr;
1692 
1693 	allow_signal(SIGKILL);
1694 	nfs4_state_manager(clp);
1695 	nfs_put_client(clp);
1696 	module_put_and_exit(0);
1697 	return 0;
1698 }
1699 
1700 /*
1701  * Local variables:
1702  *  c-basic-offset: 8
1703  * End:
1704  */
1705