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