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