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