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