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