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