GNU Linux-libre 4.14.262-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                 nfs4_clear_state_manager_bit(clp);
1213                 nfs_put_client(clp);
1214                 module_put(THIS_MODULE);
1215         }
1216 }
1217
1218 /*
1219  * Schedule a lease recovery attempt
1220  */
1221 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1222 {
1223         if (!clp)
1224                 return;
1225         if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1226                 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1227         dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1228                         clp->cl_hostname);
1229         nfs4_schedule_state_manager(clp);
1230 }
1231 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1232
1233 /**
1234  * nfs4_schedule_migration_recovery - trigger migration recovery
1235  *
1236  * @server: FSID that is migrating
1237  *
1238  * Returns zero if recovery has started, otherwise a negative NFS4ERR
1239  * value is returned.
1240  */
1241 int nfs4_schedule_migration_recovery(const struct nfs_server *server)
1242 {
1243         struct nfs_client *clp = server->nfs_client;
1244
1245         if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
1246                 pr_err("NFS: volatile file handles not supported (server %s)\n",
1247                                 clp->cl_hostname);
1248                 return -NFS4ERR_IO;
1249         }
1250
1251         if (test_bit(NFS_MIG_FAILED, &server->mig_status))
1252                 return -NFS4ERR_IO;
1253
1254         dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1255                         __func__,
1256                         (unsigned long long)server->fsid.major,
1257                         (unsigned long long)server->fsid.minor,
1258                         clp->cl_hostname);
1259
1260         set_bit(NFS_MIG_IN_TRANSITION,
1261                         &((struct nfs_server *)server)->mig_status);
1262         set_bit(NFS4CLNT_MOVED, &clp->cl_state);
1263
1264         nfs4_schedule_state_manager(clp);
1265         return 0;
1266 }
1267 EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
1268
1269 /**
1270  * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1271  *
1272  * @clp: server to check for moved leases
1273  *
1274  */
1275 void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
1276 {
1277         dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1278                 __func__, clp->cl_clientid, clp->cl_hostname);
1279
1280         set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
1281         nfs4_schedule_state_manager(clp);
1282 }
1283 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
1284
1285 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1286 {
1287         int res;
1288
1289         might_sleep();
1290
1291         atomic_inc(&clp->cl_count);
1292         res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1293                                  nfs_wait_bit_killable, TASK_KILLABLE);
1294         if (res)
1295                 goto out;
1296         if (clp->cl_cons_state < 0)
1297                 res = clp->cl_cons_state;
1298 out:
1299         nfs_put_client(clp);
1300         return res;
1301 }
1302
1303 int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1304 {
1305         unsigned int loop;
1306         int ret;
1307
1308         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1309                 ret = nfs4_wait_clnt_recover(clp);
1310                 if (ret != 0)
1311                         break;
1312                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1313                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1314                         break;
1315                 nfs4_schedule_state_manager(clp);
1316                 ret = -EIO;
1317         }
1318         return ret;
1319 }
1320
1321 /*
1322  * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1323  * @clp: client to process
1324  *
1325  * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1326  * resend of the SETCLIENTID and hence re-establish the
1327  * callback channel. Then return all existing delegations.
1328  */
1329 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1330 {
1331         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1332         nfs_expire_all_delegations(clp);
1333         dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1334                         clp->cl_hostname);
1335 }
1336
1337 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1338 {
1339         nfs40_handle_cb_pathdown(clp);
1340         nfs4_schedule_state_manager(clp);
1341 }
1342
1343 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1344 {
1345
1346         if (!nfs4_valid_open_stateid(state))
1347                 return 0;
1348         set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1349         /* Don't recover state that expired before the reboot */
1350         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1351                 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1352                 return 0;
1353         }
1354         set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1355         set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1356         return 1;
1357 }
1358
1359 int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1360 {
1361         if (!nfs4_valid_open_stateid(state))
1362                 return 0;
1363         set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1364         clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1365         set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1366         set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1367         return 1;
1368 }
1369
1370 int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1371 {
1372         struct nfs_client *clp = server->nfs_client;
1373
1374         if (!nfs4_state_mark_reclaim_nograce(clp, state))
1375                 return -EBADF;
1376         nfs_inode_find_delegation_state_and_recover(state->inode,
1377                         &state->stateid);
1378         dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1379                         clp->cl_hostname);
1380         nfs4_schedule_state_manager(clp);
1381         return 0;
1382 }
1383 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1384
1385 static struct nfs4_lock_state *
1386 nfs_state_find_lock_state_by_stateid(struct nfs4_state *state,
1387                 const nfs4_stateid *stateid)
1388 {
1389         struct nfs4_lock_state *pos;
1390
1391         list_for_each_entry(pos, &state->lock_states, ls_locks) {
1392                 if (!test_bit(NFS_LOCK_INITIALIZED, &pos->ls_flags))
1393                         continue;
1394                 if (nfs4_stateid_match_other(&pos->ls_stateid, stateid))
1395                         return pos;
1396         }
1397         return NULL;
1398 }
1399
1400 static bool nfs_state_lock_state_matches_stateid(struct nfs4_state *state,
1401                 const nfs4_stateid *stateid)
1402 {
1403         bool found = false;
1404
1405         if (test_bit(LK_STATE_IN_USE, &state->flags)) {
1406                 spin_lock(&state->state_lock);
1407                 if (nfs_state_find_lock_state_by_stateid(state, stateid))
1408                         found = true;
1409                 spin_unlock(&state->state_lock);
1410         }
1411         return found;
1412 }
1413
1414 void nfs_inode_find_state_and_recover(struct inode *inode,
1415                 const nfs4_stateid *stateid)
1416 {
1417         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1418         struct nfs_inode *nfsi = NFS_I(inode);
1419         struct nfs_open_context *ctx;
1420         struct nfs4_state *state;
1421         bool found = false;
1422
1423         spin_lock(&inode->i_lock);
1424         list_for_each_entry(ctx, &nfsi->open_files, list) {
1425                 state = ctx->state;
1426                 if (state == NULL)
1427                         continue;
1428                 if (nfs4_stateid_match_other(&state->stateid, stateid) &&
1429                     nfs4_state_mark_reclaim_nograce(clp, state)) {
1430                         found = true;
1431                         continue;
1432                 }
1433                 if (nfs_state_lock_state_matches_stateid(state, stateid) &&
1434                     nfs4_state_mark_reclaim_nograce(clp, state))
1435                         found = true;
1436         }
1437         spin_unlock(&inode->i_lock);
1438
1439         nfs_inode_find_delegation_state_and_recover(inode, stateid);
1440         if (found)
1441                 nfs4_schedule_state_manager(clp);
1442 }
1443
1444 static void nfs4_state_mark_open_context_bad(struct nfs4_state *state)
1445 {
1446         struct inode *inode = state->inode;
1447         struct nfs_inode *nfsi = NFS_I(inode);
1448         struct nfs_open_context *ctx;
1449
1450         spin_lock(&inode->i_lock);
1451         list_for_each_entry(ctx, &nfsi->open_files, list) {
1452                 if (ctx->state != state)
1453                         continue;
1454                 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1455         }
1456         spin_unlock(&inode->i_lock);
1457 }
1458
1459 static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1460 {
1461         set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
1462         nfs4_state_mark_open_context_bad(state);
1463 }
1464
1465
1466 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1467 {
1468         struct inode *inode = state->inode;
1469         struct nfs_inode *nfsi = NFS_I(inode);
1470         struct file_lock *fl;
1471         struct nfs4_lock_state *lsp;
1472         int status = 0;
1473         struct file_lock_context *flctx = inode->i_flctx;
1474         struct list_head *list;
1475
1476         if (flctx == NULL)
1477                 return 0;
1478
1479         list = &flctx->flc_posix;
1480
1481         /* Guard against delegation returns and new lock/unlock calls */
1482         down_write(&nfsi->rwsem);
1483         spin_lock(&flctx->flc_lock);
1484 restart:
1485         list_for_each_entry(fl, list, fl_list) {
1486                 if (nfs_file_open_context(fl->fl_file)->state != state)
1487                         continue;
1488                 spin_unlock(&flctx->flc_lock);
1489                 status = ops->recover_lock(state, fl);
1490                 switch (status) {
1491                 case 0:
1492                         break;
1493                 case -ESTALE:
1494                 case -NFS4ERR_ADMIN_REVOKED:
1495                 case -NFS4ERR_STALE_STATEID:
1496                 case -NFS4ERR_BAD_STATEID:
1497                 case -NFS4ERR_EXPIRED:
1498                 case -NFS4ERR_NO_GRACE:
1499                 case -NFS4ERR_STALE_CLIENTID:
1500                 case -NFS4ERR_BADSESSION:
1501                 case -NFS4ERR_BADSLOT:
1502                 case -NFS4ERR_BAD_HIGH_SLOT:
1503                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1504                         goto out;
1505                 default:
1506                         pr_err("NFS: %s: unhandled error %d\n",
1507                                         __func__, status);
1508                 case -ENOMEM:
1509                 case -NFS4ERR_DENIED:
1510                 case -NFS4ERR_RECLAIM_BAD:
1511                 case -NFS4ERR_RECLAIM_CONFLICT:
1512                         lsp = fl->fl_u.nfs4_fl.owner;
1513                         if (lsp)
1514                                 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1515                         status = 0;
1516                 }
1517                 spin_lock(&flctx->flc_lock);
1518         }
1519         if (list == &flctx->flc_posix) {
1520                 list = &flctx->flc_flock;
1521                 goto restart;
1522         }
1523         spin_unlock(&flctx->flc_lock);
1524 out:
1525         up_write(&nfsi->rwsem);
1526         return status;
1527 }
1528
1529 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1530 {
1531         struct nfs4_state *state;
1532         struct nfs4_lock_state *lock;
1533         int status = 0;
1534
1535         /* Note: we rely on the sp->so_states list being ordered 
1536          * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1537          * states first.
1538          * This is needed to ensure that the server won't give us any
1539          * read delegations that we have to return if, say, we are
1540          * recovering after a network partition or a reboot from a
1541          * server that doesn't support a grace period.
1542          */
1543         spin_lock(&sp->so_lock);
1544         raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
1545 restart:
1546         list_for_each_entry(state, &sp->so_states, open_states) {
1547                 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1548                         continue;
1549                 if (!nfs4_valid_open_stateid(state))
1550                         continue;
1551                 if (state->state == 0)
1552                         continue;
1553                 atomic_inc(&state->count);
1554                 spin_unlock(&sp->so_lock);
1555                 status = ops->recover_open(sp, state);
1556                 if (status >= 0) {
1557                         status = nfs4_reclaim_locks(state, ops);
1558                         if (status >= 0) {
1559                                 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1560                                         spin_lock(&state->state_lock);
1561                                         list_for_each_entry(lock, &state->lock_states, ls_locks) {
1562                                                 if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1563                                                         pr_warn_ratelimited("NFS: "
1564                                                                             "%s: Lock reclaim "
1565                                                                             "failed!\n", __func__);
1566                                         }
1567                                         spin_unlock(&state->state_lock);
1568                                 }
1569                                 clear_bit(NFS_STATE_RECLAIM_NOGRACE,
1570                                         &state->flags);
1571                                 nfs4_put_open_state(state);
1572                                 spin_lock(&sp->so_lock);
1573                                 goto restart;
1574                         }
1575                 }
1576                 switch (status) {
1577                         default:
1578                                 printk(KERN_ERR "NFS: %s: unhandled error %d\n",
1579                                         __func__, status);
1580                         case -ENOENT:
1581                         case -ENOMEM:
1582                         case -EACCES:
1583                         case -EROFS:
1584                         case -EIO:
1585                         case -ESTALE:
1586                                 /* Open state on this file cannot be recovered */
1587                                 nfs4_state_mark_recovery_failed(state, status);
1588                                 break;
1589                         case -EAGAIN:
1590                                 ssleep(1);
1591                         case -NFS4ERR_ADMIN_REVOKED:
1592                         case -NFS4ERR_STALE_STATEID:
1593                         case -NFS4ERR_OLD_STATEID:
1594                         case -NFS4ERR_BAD_STATEID:
1595                         case -NFS4ERR_RECLAIM_BAD:
1596                         case -NFS4ERR_RECLAIM_CONFLICT:
1597                                 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1598                                 break;
1599                         case -NFS4ERR_EXPIRED:
1600                         case -NFS4ERR_NO_GRACE:
1601                                 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1602                         case -NFS4ERR_STALE_CLIENTID:
1603                         case -NFS4ERR_BADSESSION:
1604                         case -NFS4ERR_BADSLOT:
1605                         case -NFS4ERR_BAD_HIGH_SLOT:
1606                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1607                                 goto out_err;
1608                 }
1609                 nfs4_put_open_state(state);
1610                 spin_lock(&sp->so_lock);
1611                 goto restart;
1612         }
1613         raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1614         spin_unlock(&sp->so_lock);
1615         return 0;
1616 out_err:
1617         nfs4_put_open_state(state);
1618         spin_lock(&sp->so_lock);
1619         raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1620         spin_unlock(&sp->so_lock);
1621         return status;
1622 }
1623
1624 static void nfs4_clear_open_state(struct nfs4_state *state)
1625 {
1626         struct nfs4_lock_state *lock;
1627
1628         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1629         clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1630         clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1631         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1632         spin_lock(&state->state_lock);
1633         list_for_each_entry(lock, &state->lock_states, ls_locks) {
1634                 lock->ls_seqid.flags = 0;
1635                 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1636         }
1637         spin_unlock(&state->state_lock);
1638 }
1639
1640 static void nfs4_reset_seqids(struct nfs_server *server,
1641         int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1642 {
1643         struct nfs_client *clp = server->nfs_client;
1644         struct nfs4_state_owner *sp;
1645         struct rb_node *pos;
1646         struct nfs4_state *state;
1647
1648         spin_lock(&clp->cl_lock);
1649         for (pos = rb_first(&server->state_owners);
1650              pos != NULL;
1651              pos = rb_next(pos)) {
1652                 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1653                 sp->so_seqid.flags = 0;
1654                 spin_lock(&sp->so_lock);
1655                 list_for_each_entry(state, &sp->so_states, open_states) {
1656                         if (mark_reclaim(clp, state))
1657                                 nfs4_clear_open_state(state);
1658                 }
1659                 spin_unlock(&sp->so_lock);
1660         }
1661         spin_unlock(&clp->cl_lock);
1662 }
1663
1664 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1665         int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1666 {
1667         struct nfs_server *server;
1668
1669         rcu_read_lock();
1670         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1671                 nfs4_reset_seqids(server, mark_reclaim);
1672         rcu_read_unlock();
1673 }
1674
1675 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1676 {
1677         /* Mark all delegations for reclaim */
1678         nfs_delegation_mark_reclaim(clp);
1679         nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1680 }
1681
1682 static int nfs4_reclaim_complete(struct nfs_client *clp,
1683                                  const struct nfs4_state_recovery_ops *ops,
1684                                  struct rpc_cred *cred)
1685 {
1686         /* Notify the server we're done reclaiming our state */
1687         if (ops->reclaim_complete)
1688                 return ops->reclaim_complete(clp, cred);
1689         return 0;
1690 }
1691
1692 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1693 {
1694         struct nfs_client *clp = server->nfs_client;
1695         struct nfs4_state_owner *sp;
1696         struct rb_node *pos;
1697         struct nfs4_state *state;
1698
1699         spin_lock(&clp->cl_lock);
1700         for (pos = rb_first(&server->state_owners);
1701              pos != NULL;
1702              pos = rb_next(pos)) {
1703                 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1704                 spin_lock(&sp->so_lock);
1705                 list_for_each_entry(state, &sp->so_states, open_states) {
1706                         if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1707                                                 &state->flags))
1708                                 continue;
1709                         nfs4_state_mark_reclaim_nograce(clp, state);
1710                 }
1711                 spin_unlock(&sp->so_lock);
1712         }
1713         spin_unlock(&clp->cl_lock);
1714 }
1715
1716 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1717 {
1718         struct nfs_server *server;
1719
1720         if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1721                 return 0;
1722
1723         rcu_read_lock();
1724         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1725                 nfs4_clear_reclaim_server(server);
1726         rcu_read_unlock();
1727
1728         nfs_delegation_reap_unclaimed(clp);
1729         return 1;
1730 }
1731
1732 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1733 {
1734         const struct nfs4_state_recovery_ops *ops;
1735         struct rpc_cred *cred;
1736         int err;
1737
1738         if (!nfs4_state_clear_reclaim_reboot(clp))
1739                 return;
1740         ops = clp->cl_mvops->reboot_recovery_ops;
1741         cred = nfs4_get_clid_cred(clp);
1742         err = nfs4_reclaim_complete(clp, ops, cred);
1743         put_rpccred(cred);
1744         if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
1745                 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1746 }
1747
1748 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1749 {
1750         nfs_mark_test_expired_all_delegations(clp);
1751         nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1752 }
1753
1754 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1755 {
1756         switch (error) {
1757                 case 0:
1758                         break;
1759                 case -NFS4ERR_CB_PATH_DOWN:
1760                         nfs40_handle_cb_pathdown(clp);
1761                         break;
1762                 case -NFS4ERR_NO_GRACE:
1763                         nfs4_state_end_reclaim_reboot(clp);
1764                         break;
1765                 case -NFS4ERR_STALE_CLIENTID:
1766                         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1767                         nfs4_state_start_reclaim_reboot(clp);
1768                         break;
1769                 case -NFS4ERR_EXPIRED:
1770                         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1771                         nfs4_state_start_reclaim_nograce(clp);
1772                         break;
1773                 case -NFS4ERR_BADSESSION:
1774                 case -NFS4ERR_BADSLOT:
1775                 case -NFS4ERR_BAD_HIGH_SLOT:
1776                 case -NFS4ERR_DEADSESSION:
1777                 case -NFS4ERR_SEQ_FALSE_RETRY:
1778                 case -NFS4ERR_SEQ_MISORDERED:
1779                         set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1780                         /* Zero session reset errors */
1781                         break;
1782                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1783                         set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1784                         break;
1785                 default:
1786                         dprintk("%s: failed to handle error %d for server %s\n",
1787                                         __func__, error, clp->cl_hostname);
1788                         return error;
1789         }
1790         dprintk("%s: handled error %d for server %s\n", __func__, error,
1791                         clp->cl_hostname);
1792         return 0;
1793 }
1794
1795 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1796 {
1797         struct nfs4_state_owner *sp;
1798         struct nfs_server *server;
1799         struct rb_node *pos;
1800         LIST_HEAD(freeme);
1801         int status = 0;
1802
1803 restart:
1804         rcu_read_lock();
1805         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1806                 nfs4_purge_state_owners(server, &freeme);
1807                 spin_lock(&clp->cl_lock);
1808                 for (pos = rb_first(&server->state_owners);
1809                      pos != NULL;
1810                      pos = rb_next(pos)) {
1811                         sp = rb_entry(pos,
1812                                 struct nfs4_state_owner, so_server_node);
1813                         if (!test_and_clear_bit(ops->owner_flag_bit,
1814                                                         &sp->so_flags))
1815                                 continue;
1816                         if (!atomic_inc_not_zero(&sp->so_count))
1817                                 continue;
1818                         spin_unlock(&clp->cl_lock);
1819                         rcu_read_unlock();
1820
1821                         status = nfs4_reclaim_open_state(sp, ops);
1822                         if (status < 0) {
1823                                 set_bit(ops->owner_flag_bit, &sp->so_flags);
1824                                 nfs4_put_state_owner(sp);
1825                                 status = nfs4_recovery_handle_error(clp, status);
1826                                 return (status != 0) ? status : -EAGAIN;
1827                         }
1828
1829                         nfs4_put_state_owner(sp);
1830                         goto restart;
1831                 }
1832                 spin_unlock(&clp->cl_lock);
1833         }
1834         rcu_read_unlock();
1835         nfs4_free_state_owners(&freeme);
1836         return 0;
1837 }
1838
1839 static int nfs4_check_lease(struct nfs_client *clp)
1840 {
1841         struct rpc_cred *cred;
1842         const struct nfs4_state_maintenance_ops *ops =
1843                 clp->cl_mvops->state_renewal_ops;
1844         int status;
1845
1846         /* Is the client already known to have an expired lease? */
1847         if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1848                 return 0;
1849         spin_lock(&clp->cl_lock);
1850         cred = ops->get_state_renewal_cred_locked(clp);
1851         spin_unlock(&clp->cl_lock);
1852         if (cred == NULL) {
1853                 cred = nfs4_get_clid_cred(clp);
1854                 status = -ENOKEY;
1855                 if (cred == NULL)
1856                         goto out;
1857         }
1858         status = ops->renew_lease(clp, cred);
1859         put_rpccred(cred);
1860         if (status == -ETIMEDOUT) {
1861                 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1862                 return 0;
1863         }
1864 out:
1865         return nfs4_recovery_handle_error(clp, status);
1866 }
1867
1868 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1869  * and for recoverable errors on EXCHANGE_ID for v4.1
1870  */
1871 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1872 {
1873         switch (status) {
1874         case -NFS4ERR_SEQ_MISORDERED:
1875                 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1876                         return -ESERVERFAULT;
1877                 /* Lease confirmation error: retry after purging the lease */
1878                 ssleep(1);
1879                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1880                 break;
1881         case -NFS4ERR_STALE_CLIENTID:
1882                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1883                 nfs4_state_start_reclaim_reboot(clp);
1884                 break;
1885         case -NFS4ERR_CLID_INUSE:
1886                 pr_err("NFS: Server %s reports our clientid is in use\n",
1887                         clp->cl_hostname);
1888                 nfs_mark_client_ready(clp, -EPERM);
1889                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1890                 return -EPERM;
1891         case -EACCES:
1892         case -NFS4ERR_DELAY:
1893         case -ETIMEDOUT:
1894         case -EAGAIN:
1895                 ssleep(1);
1896                 break;
1897
1898         case -NFS4ERR_MINOR_VERS_MISMATCH:
1899                 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1900                         nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1901                 dprintk("%s: exit with error %d for server %s\n",
1902                                 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1903                 return -EPROTONOSUPPORT;
1904         case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1905                                  * in nfs4_exchange_id */
1906         default:
1907                 dprintk("%s: exit with error %d for server %s\n", __func__,
1908                                 status, clp->cl_hostname);
1909                 return status;
1910         }
1911         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1912         dprintk("%s: handled error %d for server %s\n", __func__, status,
1913                         clp->cl_hostname);
1914         return 0;
1915 }
1916
1917 static int nfs4_establish_lease(struct nfs_client *clp)
1918 {
1919         struct rpc_cred *cred;
1920         const struct nfs4_state_recovery_ops *ops =
1921                 clp->cl_mvops->reboot_recovery_ops;
1922         int status;
1923
1924         nfs4_begin_drain_session(clp);
1925         cred = nfs4_get_clid_cred(clp);
1926         if (cred == NULL)
1927                 return -ENOENT;
1928         status = ops->establish_clid(clp, cred);
1929         put_rpccred(cred);
1930         if (status != 0)
1931                 return status;
1932         pnfs_destroy_all_layouts(clp);
1933         return 0;
1934 }
1935
1936 /*
1937  * Returns zero or a negative errno.  NFS4ERR values are converted
1938  * to local errno values.
1939  */
1940 static int nfs4_reclaim_lease(struct nfs_client *clp)
1941 {
1942         int status;
1943
1944         status = nfs4_establish_lease(clp);
1945         if (status < 0)
1946                 return nfs4_handle_reclaim_lease_error(clp, status);
1947         if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1948                 nfs4_state_start_reclaim_nograce(clp);
1949         if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1950                 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1951         clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1952         clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1953         return 0;
1954 }
1955
1956 static int nfs4_purge_lease(struct nfs_client *clp)
1957 {
1958         int status;
1959
1960         status = nfs4_establish_lease(clp);
1961         if (status < 0)
1962                 return nfs4_handle_reclaim_lease_error(clp, status);
1963         clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1964         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1965         nfs4_state_start_reclaim_nograce(clp);
1966         return 0;
1967 }
1968
1969 /*
1970  * Try remote migration of one FSID from a source server to a
1971  * destination server.  The source server provides a list of
1972  * potential destinations.
1973  *
1974  * Returns zero or a negative NFS4ERR status code.
1975  */
1976 static int nfs4_try_migration(struct nfs_server *server, struct rpc_cred *cred)
1977 {
1978         struct nfs_client *clp = server->nfs_client;
1979         struct nfs4_fs_locations *locations = NULL;
1980         struct inode *inode;
1981         struct page *page;
1982         int status, result;
1983
1984         dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
1985                         (unsigned long long)server->fsid.major,
1986                         (unsigned long long)server->fsid.minor,
1987                         clp->cl_hostname);
1988
1989         result = 0;
1990         page = alloc_page(GFP_KERNEL);
1991         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1992         if (page == NULL || locations == NULL) {
1993                 dprintk("<-- %s: no memory\n", __func__);
1994                 goto out;
1995         }
1996
1997         inode = d_inode(server->super->s_root);
1998         result = nfs4_proc_get_locations(inode, locations, page, cred);
1999         if (result) {
2000                 dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2001                         __func__, result);
2002                 goto out;
2003         }
2004
2005         result = -NFS4ERR_NXIO;
2006         if (!(locations->fattr.valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
2007                 dprintk("<-- %s: No fs_locations data, migration skipped\n",
2008                         __func__);
2009                 goto out;
2010         }
2011
2012         nfs4_begin_drain_session(clp);
2013
2014         status = nfs4_replace_transport(server, locations);
2015         if (status != 0) {
2016                 dprintk("<-- %s: failed to replace transport: %d\n",
2017                         __func__, status);
2018                 goto out;
2019         }
2020
2021         result = 0;
2022         dprintk("<-- %s: migration succeeded\n", __func__);
2023
2024 out:
2025         if (page != NULL)
2026                 __free_page(page);
2027         kfree(locations);
2028         if (result) {
2029                 pr_err("NFS: migration recovery failed (server %s)\n",
2030                                 clp->cl_hostname);
2031                 set_bit(NFS_MIG_FAILED, &server->mig_status);
2032         }
2033         return result;
2034 }
2035
2036 /*
2037  * Returns zero or a negative NFS4ERR status code.
2038  */
2039 static int nfs4_handle_migration(struct nfs_client *clp)
2040 {
2041         const struct nfs4_state_maintenance_ops *ops =
2042                                 clp->cl_mvops->state_renewal_ops;
2043         struct nfs_server *server;
2044         struct rpc_cred *cred;
2045
2046         dprintk("%s: migration reported on \"%s\"\n", __func__,
2047                         clp->cl_hostname);
2048
2049         spin_lock(&clp->cl_lock);
2050         cred = ops->get_state_renewal_cred_locked(clp);
2051         spin_unlock(&clp->cl_lock);
2052         if (cred == NULL)
2053                 return -NFS4ERR_NOENT;
2054
2055         clp->cl_mig_gen++;
2056 restart:
2057         rcu_read_lock();
2058         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2059                 int status;
2060
2061                 if (server->mig_gen == clp->cl_mig_gen)
2062                         continue;
2063                 server->mig_gen = clp->cl_mig_gen;
2064
2065                 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
2066                                                 &server->mig_status))
2067                         continue;
2068
2069                 rcu_read_unlock();
2070                 status = nfs4_try_migration(server, cred);
2071                 if (status < 0) {
2072                         put_rpccred(cred);
2073                         return status;
2074                 }
2075                 goto restart;
2076         }
2077         rcu_read_unlock();
2078         put_rpccred(cred);
2079         return 0;
2080 }
2081
2082 /*
2083  * Test each nfs_server on the clp's cl_superblocks list to see
2084  * if it's moved to another server.  Stop when the server no longer
2085  * returns NFS4ERR_LEASE_MOVED.
2086  */
2087 static int nfs4_handle_lease_moved(struct nfs_client *clp)
2088 {
2089         const struct nfs4_state_maintenance_ops *ops =
2090                                 clp->cl_mvops->state_renewal_ops;
2091         struct nfs_server *server;
2092         struct rpc_cred *cred;
2093
2094         dprintk("%s: lease moved reported on \"%s\"\n", __func__,
2095                         clp->cl_hostname);
2096
2097         spin_lock(&clp->cl_lock);
2098         cred = ops->get_state_renewal_cred_locked(clp);
2099         spin_unlock(&clp->cl_lock);
2100         if (cred == NULL)
2101                 return -NFS4ERR_NOENT;
2102
2103         clp->cl_mig_gen++;
2104 restart:
2105         rcu_read_lock();
2106         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2107                 struct inode *inode;
2108                 int status;
2109
2110                 if (server->mig_gen == clp->cl_mig_gen)
2111                         continue;
2112                 server->mig_gen = clp->cl_mig_gen;
2113
2114                 rcu_read_unlock();
2115
2116                 inode = d_inode(server->super->s_root);
2117                 status = nfs4_proc_fsid_present(inode, cred);
2118                 if (status != -NFS4ERR_MOVED)
2119                         goto restart;   /* wasn't this one */
2120                 if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
2121                         goto restart;   /* there are more */
2122                 goto out;
2123         }
2124         rcu_read_unlock();
2125
2126 out:
2127         put_rpccred(cred);
2128         return 0;
2129 }
2130
2131 /**
2132  * nfs4_discover_server_trunking - Detect server IP address trunking
2133  *
2134  * @clp: nfs_client under test
2135  * @result: OUT: found nfs_client, or clp
2136  *
2137  * Returns zero or a negative errno.  If zero is returned,
2138  * an nfs_client pointer is planted in "result".
2139  *
2140  * Note: since we are invoked in process context, and
2141  * not from inside the state manager, we cannot use
2142  * nfs4_handle_reclaim_lease_error().
2143  */
2144 int nfs4_discover_server_trunking(struct nfs_client *clp,
2145                                   struct nfs_client **result)
2146 {
2147         const struct nfs4_state_recovery_ops *ops =
2148                                 clp->cl_mvops->reboot_recovery_ops;
2149         struct rpc_clnt *clnt;
2150         struct rpc_cred *cred;
2151         int i, status;
2152
2153         dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
2154
2155         clnt = clp->cl_rpcclient;
2156         i = 0;
2157
2158         mutex_lock(&nfs_clid_init_mutex);
2159 again:
2160         status  = -ENOENT;
2161         cred = nfs4_get_clid_cred(clp);
2162         if (cred == NULL)
2163                 goto out_unlock;
2164
2165         status = ops->detect_trunking(clp, result, cred);
2166         put_rpccred(cred);
2167         switch (status) {
2168         case 0:
2169         case -EINTR:
2170         case -ERESTARTSYS:
2171                 break;
2172         case -ETIMEDOUT:
2173                 if (clnt->cl_softrtry)
2174                         break;
2175         case -NFS4ERR_DELAY:
2176         case -EAGAIN:
2177                 ssleep(1);
2178         case -NFS4ERR_STALE_CLIENTID:
2179                 dprintk("NFS: %s after status %d, retrying\n",
2180                         __func__, status);
2181                 goto again;
2182         case -EACCES:
2183                 if (i++ == 0) {
2184                         nfs4_root_machine_cred(clp);
2185                         goto again;
2186                 }
2187                 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
2188                         break;
2189         case -NFS4ERR_CLID_INUSE:
2190         case -NFS4ERR_WRONGSEC:
2191                 /* No point in retrying if we already used RPC_AUTH_UNIX */
2192                 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
2193                         status = -EPERM;
2194                         break;
2195                 }
2196                 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
2197                 if (IS_ERR(clnt)) {
2198                         status = PTR_ERR(clnt);
2199                         break;
2200                 }
2201                 /* Note: this is safe because we haven't yet marked the
2202                  * client as ready, so we are the only user of
2203                  * clp->cl_rpcclient
2204                  */
2205                 clnt = xchg(&clp->cl_rpcclient, clnt);
2206                 rpc_shutdown_client(clnt);
2207                 clnt = clp->cl_rpcclient;
2208                 goto again;
2209
2210         case -NFS4ERR_MINOR_VERS_MISMATCH:
2211                 status = -EPROTONOSUPPORT;
2212                 break;
2213
2214         case -EKEYEXPIRED:
2215         case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2216                                  * in nfs4_exchange_id */
2217                 status = -EKEYEXPIRED;
2218                 break;
2219         default:
2220                 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2221                                 __func__, status);
2222                 status = -EIO;
2223         }
2224
2225 out_unlock:
2226         mutex_unlock(&nfs_clid_init_mutex);
2227         dprintk("NFS: %s: status = %d\n", __func__, status);
2228         return status;
2229 }
2230
2231 #ifdef CONFIG_NFS_V4_1
2232 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
2233 {
2234         struct nfs_client *clp = session->clp;
2235
2236         switch (err) {
2237         default:
2238                 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2239                 break;
2240         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2241                 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2242         }
2243         nfs4_schedule_state_manager(clp);
2244 }
2245 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
2246
2247 void nfs41_notify_server(struct nfs_client *clp)
2248 {
2249         /* Use CHECK_LEASE to ping the server with a SEQUENCE */
2250         set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2251         nfs4_schedule_state_manager(clp);
2252 }
2253
2254 static void nfs4_reset_all_state(struct nfs_client *clp)
2255 {
2256         if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2257                 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2258                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2259                 nfs4_state_start_reclaim_nograce(clp);
2260                 dprintk("%s: scheduling reset of all state for server %s!\n",
2261                                 __func__, clp->cl_hostname);
2262                 nfs4_schedule_state_manager(clp);
2263         }
2264 }
2265
2266 static void nfs41_handle_server_reboot(struct nfs_client *clp)
2267 {
2268         if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2269                 nfs4_state_start_reclaim_reboot(clp);
2270                 dprintk("%s: server %s rebooted!\n", __func__,
2271                                 clp->cl_hostname);
2272                 nfs4_schedule_state_manager(clp);
2273         }
2274 }
2275
2276 static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
2277 {
2278         nfs4_reset_all_state(clp);
2279         dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2280 }
2281
2282 static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
2283 {
2284         nfs4_state_start_reclaim_nograce(clp);
2285         nfs4_schedule_state_manager(clp);
2286
2287         dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2288 }
2289
2290 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
2291 {
2292         /* FIXME: For now, we destroy all layouts. */
2293         pnfs_destroy_all_layouts(clp);
2294         /* FIXME: For now, we test all delegations+open state+locks. */
2295         nfs41_handle_some_state_revoked(clp);
2296         dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2297                         clp->cl_hostname);
2298 }
2299
2300 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
2301 {
2302         set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2303         nfs4_schedule_state_manager(clp);
2304
2305         dprintk("%s: server %s declared a backchannel fault\n", __func__,
2306                         clp->cl_hostname);
2307 }
2308
2309 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2310 {
2311         if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2312                 &clp->cl_state) == 0)
2313                 nfs4_schedule_state_manager(clp);
2314 }
2315
2316 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
2317                 bool recovery)
2318 {
2319         if (!flags)
2320                 return;
2321
2322         dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2323                 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2324         /*
2325          * If we're called from the state manager thread, then assume we're
2326          * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2327          * Those flags are expected to remain set until we're done
2328          * recovering (see RFC5661, section 18.46.3).
2329          */
2330         if (recovery)
2331                 goto out_recovery;
2332
2333         if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2334                 nfs41_handle_server_reboot(clp);
2335         if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
2336                 nfs41_handle_all_state_revoked(clp);
2337         if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2338                             SEQ4_STATUS_ADMIN_STATE_REVOKED))
2339                 nfs41_handle_some_state_revoked(clp);
2340         if (flags & SEQ4_STATUS_LEASE_MOVED)
2341                 nfs4_schedule_lease_moved_recovery(clp);
2342         if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2343                 nfs41_handle_recallable_state_revoked(clp);
2344 out_recovery:
2345         if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2346                 nfs41_handle_backchannel_fault(clp);
2347         else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2348                                 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2349                 nfs41_handle_cb_path_down(clp);
2350 }
2351
2352 static int nfs4_reset_session(struct nfs_client *clp)
2353 {
2354         struct rpc_cred *cred;
2355         int status;
2356
2357         if (!nfs4_has_session(clp))
2358                 return 0;
2359         nfs4_begin_drain_session(clp);
2360         cred = nfs4_get_clid_cred(clp);
2361         status = nfs4_proc_destroy_session(clp->cl_session, cred);
2362         switch (status) {
2363         case 0:
2364         case -NFS4ERR_BADSESSION:
2365         case -NFS4ERR_DEADSESSION:
2366                 break;
2367         case -NFS4ERR_BACK_CHAN_BUSY:
2368         case -NFS4ERR_DELAY:
2369                 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2370                 status = 0;
2371                 ssleep(1);
2372                 goto out;
2373         default:
2374                 status = nfs4_recovery_handle_error(clp, status);
2375                 goto out;
2376         }
2377
2378         memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2379         status = nfs4_proc_create_session(clp, cred);
2380         if (status) {
2381                 dprintk("%s: session reset failed with status %d for server %s!\n",
2382                         __func__, status, clp->cl_hostname);
2383                 status = nfs4_handle_reclaim_lease_error(clp, status);
2384                 goto out;
2385         }
2386         nfs41_finish_session_reset(clp);
2387         dprintk("%s: session reset was successful for server %s!\n",
2388                         __func__, clp->cl_hostname);
2389 out:
2390         if (cred)
2391                 put_rpccred(cred);
2392         return status;
2393 }
2394
2395 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2396 {
2397         struct rpc_cred *cred;
2398         int ret;
2399
2400         if (!nfs4_has_session(clp))
2401                 return 0;
2402         nfs4_begin_drain_session(clp);
2403         cred = nfs4_get_clid_cred(clp);
2404         ret = nfs4_proc_bind_conn_to_session(clp, cred);
2405         if (cred)
2406                 put_rpccred(cred);
2407         clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2408         switch (ret) {
2409         case 0:
2410                 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2411                         __func__, clp->cl_hostname);
2412                 break;
2413         case -NFS4ERR_DELAY:
2414                 ssleep(1);
2415                 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2416                 break;
2417         default:
2418                 return nfs4_recovery_handle_error(clp, ret);
2419         }
2420         return 0;
2421 }
2422 #else /* CONFIG_NFS_V4_1 */
2423 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2424
2425 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2426 {
2427         return 0;
2428 }
2429 #endif /* CONFIG_NFS_V4_1 */
2430
2431 static void nfs4_state_manager(struct nfs_client *clp)
2432 {
2433         int status = 0;
2434         const char *section = "", *section_sep = "";
2435
2436         /* Ensure exclusive access to NFSv4 state */
2437         do {
2438                 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2439                         section = "purge state";
2440                         status = nfs4_purge_lease(clp);
2441                         if (status < 0)
2442                                 goto out_error;
2443                         continue;
2444                 }
2445
2446                 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2447                         section = "lease expired";
2448                         /* We're going to have to re-establish a clientid */
2449                         status = nfs4_reclaim_lease(clp);
2450                         if (status < 0)
2451                                 goto out_error;
2452                         continue;
2453                 }
2454
2455                 /* Initialize or reset the session */
2456                 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2457                         section = "reset session";
2458                         status = nfs4_reset_session(clp);
2459                         if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2460                                 continue;
2461                         if (status < 0)
2462                                 goto out_error;
2463                 }
2464
2465                 /* Send BIND_CONN_TO_SESSION */
2466                 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2467                                 &clp->cl_state)) {
2468                         section = "bind conn to session";
2469                         status = nfs4_bind_conn_to_session(clp);
2470                         if (status < 0)
2471                                 goto out_error;
2472                         continue;
2473                 }
2474
2475                 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2476                         section = "check lease";
2477                         status = nfs4_check_lease(clp);
2478                         if (status < 0)
2479                                 goto out_error;
2480                         continue;
2481                 }
2482
2483                 if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
2484                         section = "migration";
2485                         status = nfs4_handle_migration(clp);
2486                         if (status < 0)
2487                                 goto out_error;
2488                 }
2489
2490                 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
2491                         section = "lease moved";
2492                         status = nfs4_handle_lease_moved(clp);
2493                         if (status < 0)
2494                                 goto out_error;
2495                 }
2496
2497                 /* First recover reboot state... */
2498                 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2499                         section = "reclaim reboot";
2500                         status = nfs4_do_reclaim(clp,
2501                                 clp->cl_mvops->reboot_recovery_ops);
2502                         if (status == -EAGAIN)
2503                                 continue;
2504                         if (status < 0)
2505                                 goto out_error;
2506                         nfs4_state_end_reclaim_reboot(clp);
2507                 }
2508
2509                 /* Detect expired delegations... */
2510                 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
2511                         section = "detect expired delegations";
2512                         nfs_reap_expired_delegations(clp);
2513                         continue;
2514                 }
2515
2516                 /* Now recover expired state... */
2517                 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2518                         section = "reclaim nograce";
2519                         status = nfs4_do_reclaim(clp,
2520                                 clp->cl_mvops->nograce_recovery_ops);
2521                         if (status == -EAGAIN)
2522                                 continue;
2523                         if (status < 0)
2524                                 goto out_error;
2525                 }
2526
2527                 nfs4_end_drain_session(clp);
2528                 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2529                         nfs_client_return_marked_delegations(clp);
2530                         continue;
2531                 }
2532
2533                 nfs4_clear_state_manager_bit(clp);
2534                 /* Did we race with an attempt to give us more work? */
2535                 if (clp->cl_state == 0)
2536                         break;
2537                 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2538                         break;
2539         } while (atomic_read(&clp->cl_count) > 1);
2540         return;
2541 out_error:
2542         if (strlen(section))
2543                 section_sep = ": ";
2544         pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2545                         " with error %d\n", section_sep, section,
2546                         clp->cl_hostname, -status);
2547         ssleep(1);
2548         nfs4_end_drain_session(clp);
2549         nfs4_clear_state_manager_bit(clp);
2550 }
2551
2552 static int nfs4_run_state_manager(void *ptr)
2553 {
2554         struct nfs_client *clp = ptr;
2555
2556         allow_signal(SIGKILL);
2557         nfs4_state_manager(clp);
2558         nfs_put_client(clp);
2559         module_put_and_exit(0);
2560         return 0;
2561 }
2562
2563 /*
2564  * Local variables:
2565  *  c-basic-offset: 8
2566  * End:
2567  */