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