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