1 /* 2 * fs/nfs/nfs4state.c 3 * 4 * Client-side XDR for NFSv4. 5 * 6 * Copyright (c) 2002 The Regents of the University of Michigan. 7 * All rights reserved. 8 * 9 * Kendrick Smith <kmsmith@umich.edu> 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its 21 * contributors may be used to endorse or promote products derived 22 * from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 * 36 * Implementation of the NFSv4 state model. For the time being, 37 * this is minimal, but will be made much more complex in a 38 * subsequent patch. 39 */ 40 41 #include <linux/kernel.h> 42 #include <linux/slab.h> 43 #include <linux/fs.h> 44 #include <linux/nfs_fs.h> 45 #include <linux/nfs_idmap.h> 46 #include <linux/kthread.h> 47 #include <linux/module.h> 48 #include <linux/random.h> 49 #include <linux/ratelimit.h> 50 #include <linux/workqueue.h> 51 #include <linux/bitops.h> 52 53 #include "nfs4_fs.h" 54 #include "callback.h" 55 #include "delegation.h" 56 #include "internal.h" 57 #include "pnfs.h" 58 59 #define OPENOWNER_POOL_SIZE 8 60 61 const nfs4_stateid zero_stateid; 62 63 static LIST_HEAD(nfs4_clientid_list); 64 65 int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred) 66 { 67 struct nfs4_setclientid_res clid; 68 unsigned short port; 69 int status; 70 71 port = nfs_callback_tcpport; 72 if (clp->cl_addr.ss_family == AF_INET6) 73 port = nfs_callback_tcpport6; 74 75 status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid); 76 if (status != 0) 77 goto out; 78 status = nfs4_proc_setclientid_confirm(clp, &clid, cred); 79 if (status != 0) 80 goto out; 81 clp->cl_clientid = clid.clientid; 82 nfs4_schedule_state_renewal(clp); 83 out: 84 return status; 85 } 86 87 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp) 88 { 89 struct rpc_cred *cred = NULL; 90 91 if (clp->cl_machine_cred != NULL) 92 cred = get_rpccred(clp->cl_machine_cred); 93 return cred; 94 } 95 96 static void nfs4_clear_machine_cred(struct nfs_client *clp) 97 { 98 struct rpc_cred *cred; 99 100 spin_lock(&clp->cl_lock); 101 cred = clp->cl_machine_cred; 102 clp->cl_machine_cred = NULL; 103 spin_unlock(&clp->cl_lock); 104 if (cred != NULL) 105 put_rpccred(cred); 106 } 107 108 static struct rpc_cred * 109 nfs4_get_renew_cred_server_locked(struct nfs_server *server) 110 { 111 struct rpc_cred *cred = NULL; 112 struct nfs4_state_owner *sp; 113 struct rb_node *pos; 114 115 for (pos = rb_first(&server->state_owners); 116 pos != NULL; 117 pos = rb_next(pos)) { 118 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node); 119 if (list_empty(&sp->so_states)) 120 continue; 121 cred = get_rpccred(sp->so_cred); 122 break; 123 } 124 return cred; 125 } 126 127 /** 128 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation 129 * @clp: client state handle 130 * 131 * Returns an rpc_cred with reference count bumped, or NULL. 132 * Caller must hold clp->cl_lock. 133 */ 134 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp) 135 { 136 struct rpc_cred *cred = NULL; 137 struct nfs_server *server; 138 139 rcu_read_lock(); 140 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 141 cred = nfs4_get_renew_cred_server_locked(server); 142 if (cred != NULL) 143 break; 144 } 145 rcu_read_unlock(); 146 return cred; 147 } 148 149 #if defined(CONFIG_NFS_V4_1) 150 151 static int nfs41_setup_state_renewal(struct nfs_client *clp) 152 { 153 int status; 154 struct nfs_fsinfo fsinfo; 155 156 if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) { 157 nfs4_schedule_state_renewal(clp); 158 return 0; 159 } 160 161 status = nfs4_proc_get_lease_time(clp, &fsinfo); 162 if (status == 0) { 163 /* Update lease time and schedule renewal */ 164 spin_lock(&clp->cl_lock); 165 clp->cl_lease_time = fsinfo.lease_time * HZ; 166 clp->cl_last_renewal = jiffies; 167 spin_unlock(&clp->cl_lock); 168 169 nfs4_schedule_state_renewal(clp); 170 } 171 172 return status; 173 } 174 175 /* 176 * Back channel returns NFS4ERR_DELAY for new requests when 177 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining 178 * is ended. 179 */ 180 static void nfs4_end_drain_session(struct nfs_client *clp) 181 { 182 struct nfs4_session *ses = clp->cl_session; 183 int max_slots; 184 185 if (ses == NULL) 186 return; 187 if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) { 188 spin_lock(&ses->fc_slot_table.slot_tbl_lock); 189 max_slots = ses->fc_slot_table.max_slots; 190 while (max_slots--) { 191 struct rpc_task *task; 192 193 task = rpc_wake_up_next(&ses->fc_slot_table. 194 slot_tbl_waitq); 195 if (!task) 196 break; 197 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED); 198 } 199 spin_unlock(&ses->fc_slot_table.slot_tbl_lock); 200 } 201 } 202 203 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl) 204 { 205 spin_lock(&tbl->slot_tbl_lock); 206 if (tbl->highest_used_slotid != -1) { 207 INIT_COMPLETION(tbl->complete); 208 spin_unlock(&tbl->slot_tbl_lock); 209 return wait_for_completion_interruptible(&tbl->complete); 210 } 211 spin_unlock(&tbl->slot_tbl_lock); 212 return 0; 213 } 214 215 static int nfs4_begin_drain_session(struct nfs_client *clp) 216 { 217 struct nfs4_session *ses = clp->cl_session; 218 int ret = 0; 219 220 set_bit(NFS4_SESSION_DRAINING, &ses->session_state); 221 /* back channel */ 222 ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table); 223 if (ret) 224 return ret; 225 /* fore channel */ 226 return nfs4_wait_on_slot_tbl(&ses->fc_slot_table); 227 } 228 229 int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred) 230 { 231 int status; 232 233 nfs4_begin_drain_session(clp); 234 status = nfs4_proc_exchange_id(clp, cred); 235 if (status != 0) 236 goto out; 237 status = nfs4_proc_create_session(clp); 238 if (status != 0) 239 goto out; 240 nfs41_setup_state_renewal(clp); 241 nfs_mark_client_ready(clp, NFS_CS_READY); 242 out: 243 return status; 244 } 245 246 struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp) 247 { 248 struct rpc_cred *cred; 249 250 spin_lock(&clp->cl_lock); 251 cred = nfs4_get_machine_cred_locked(clp); 252 spin_unlock(&clp->cl_lock); 253 return cred; 254 } 255 256 #endif /* CONFIG_NFS_V4_1 */ 257 258 static struct rpc_cred * 259 nfs4_get_setclientid_cred_server(struct nfs_server *server) 260 { 261 struct nfs_client *clp = server->nfs_client; 262 struct rpc_cred *cred = NULL; 263 struct nfs4_state_owner *sp; 264 struct rb_node *pos; 265 266 spin_lock(&clp->cl_lock); 267 pos = rb_first(&server->state_owners); 268 if (pos != NULL) { 269 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node); 270 cred = get_rpccred(sp->so_cred); 271 } 272 spin_unlock(&clp->cl_lock); 273 return cred; 274 } 275 276 /** 277 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation 278 * @clp: client state handle 279 * 280 * Returns an rpc_cred with reference count bumped, or NULL. 281 */ 282 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp) 283 { 284 struct nfs_server *server; 285 struct rpc_cred *cred; 286 287 spin_lock(&clp->cl_lock); 288 cred = nfs4_get_machine_cred_locked(clp); 289 spin_unlock(&clp->cl_lock); 290 if (cred != NULL) 291 goto out; 292 293 rcu_read_lock(); 294 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 295 cred = nfs4_get_setclientid_cred_server(server); 296 if (cred != NULL) 297 break; 298 } 299 rcu_read_unlock(); 300 301 out: 302 return cred; 303 } 304 305 static void nfs_alloc_unique_id_locked(struct rb_root *root, 306 struct nfs_unique_id *new, 307 __u64 minval, int maxbits) 308 { 309 struct rb_node **p, *parent; 310 struct nfs_unique_id *pos; 311 __u64 mask = ~0ULL; 312 313 if (maxbits < 64) 314 mask = (1ULL << maxbits) - 1ULL; 315 316 /* Ensure distribution is more or less flat */ 317 get_random_bytes(&new->id, sizeof(new->id)); 318 new->id &= mask; 319 if (new->id < minval) 320 new->id += minval; 321 retry: 322 p = &root->rb_node; 323 parent = NULL; 324 325 while (*p != NULL) { 326 parent = *p; 327 pos = rb_entry(parent, struct nfs_unique_id, rb_node); 328 329 if (new->id < pos->id) 330 p = &(*p)->rb_left; 331 else if (new->id > pos->id) 332 p = &(*p)->rb_right; 333 else 334 goto id_exists; 335 } 336 rb_link_node(&new->rb_node, parent, p); 337 rb_insert_color(&new->rb_node, root); 338 return; 339 id_exists: 340 for (;;) { 341 new->id++; 342 if (new->id < minval || (new->id & mask) != new->id) { 343 new->id = minval; 344 break; 345 } 346 parent = rb_next(parent); 347 if (parent == NULL) 348 break; 349 pos = rb_entry(parent, struct nfs_unique_id, rb_node); 350 if (new->id < pos->id) 351 break; 352 } 353 goto retry; 354 } 355 356 static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id) 357 { 358 rb_erase(&id->rb_node, root); 359 } 360 361 static struct nfs4_state_owner * 362 nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred) 363 { 364 struct rb_node **p = &server->state_owners.rb_node, 365 *parent = NULL; 366 struct nfs4_state_owner *sp, *res = NULL; 367 368 while (*p != NULL) { 369 parent = *p; 370 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node); 371 372 if (server < sp->so_server) { 373 p = &parent->rb_left; 374 continue; 375 } 376 if (server > sp->so_server) { 377 p = &parent->rb_right; 378 continue; 379 } 380 if (cred < sp->so_cred) 381 p = &parent->rb_left; 382 else if (cred > sp->so_cred) 383 p = &parent->rb_right; 384 else { 385 atomic_inc(&sp->so_count); 386 res = sp; 387 break; 388 } 389 } 390 return res; 391 } 392 393 static struct nfs4_state_owner * 394 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new) 395 { 396 struct nfs_server *server = new->so_server; 397 struct rb_node **p = &server->state_owners.rb_node, 398 *parent = NULL; 399 struct nfs4_state_owner *sp; 400 401 while (*p != NULL) { 402 parent = *p; 403 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node); 404 405 if (new->so_cred < sp->so_cred) 406 p = &parent->rb_left; 407 else if (new->so_cred > sp->so_cred) 408 p = &parent->rb_right; 409 else { 410 atomic_inc(&sp->so_count); 411 return sp; 412 } 413 } 414 nfs_alloc_unique_id_locked(&server->openowner_id, 415 &new->so_owner_id, 1, 64); 416 rb_link_node(&new->so_server_node, parent, p); 417 rb_insert_color(&new->so_server_node, &server->state_owners); 418 return new; 419 } 420 421 static void 422 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp) 423 { 424 struct nfs_server *server = sp->so_server; 425 426 if (!RB_EMPTY_NODE(&sp->so_server_node)) 427 rb_erase(&sp->so_server_node, &server->state_owners); 428 nfs_free_unique_id(&server->openowner_id, &sp->so_owner_id); 429 } 430 431 /* 432 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to 433 * create a new state_owner. 434 * 435 */ 436 static struct nfs4_state_owner * 437 nfs4_alloc_state_owner(void) 438 { 439 struct nfs4_state_owner *sp; 440 441 sp = kzalloc(sizeof(*sp),GFP_NOFS); 442 if (!sp) 443 return NULL; 444 spin_lock_init(&sp->so_lock); 445 INIT_LIST_HEAD(&sp->so_states); 446 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue"); 447 sp->so_seqid.sequence = &sp->so_sequence; 448 spin_lock_init(&sp->so_sequence.lock); 449 INIT_LIST_HEAD(&sp->so_sequence.list); 450 atomic_set(&sp->so_count, 1); 451 return sp; 452 } 453 454 static void 455 nfs4_drop_state_owner(struct nfs4_state_owner *sp) 456 { 457 if (!RB_EMPTY_NODE(&sp->so_server_node)) { 458 struct nfs_server *server = sp->so_server; 459 struct nfs_client *clp = server->nfs_client; 460 461 spin_lock(&clp->cl_lock); 462 rb_erase(&sp->so_server_node, &server->state_owners); 463 RB_CLEAR_NODE(&sp->so_server_node); 464 spin_unlock(&clp->cl_lock); 465 } 466 } 467 468 /** 469 * nfs4_get_state_owner - Look up a state owner given a credential 470 * @server: nfs_server to search 471 * @cred: RPC credential to match 472 * 473 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL. 474 */ 475 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, 476 struct rpc_cred *cred) 477 { 478 struct nfs_client *clp = server->nfs_client; 479 struct nfs4_state_owner *sp, *new; 480 481 spin_lock(&clp->cl_lock); 482 sp = nfs4_find_state_owner_locked(server, cred); 483 spin_unlock(&clp->cl_lock); 484 if (sp != NULL) 485 return sp; 486 new = nfs4_alloc_state_owner(); 487 if (new == NULL) 488 return NULL; 489 new->so_server = server; 490 new->so_cred = cred; 491 spin_lock(&clp->cl_lock); 492 sp = nfs4_insert_state_owner_locked(new); 493 spin_unlock(&clp->cl_lock); 494 if (sp == new) 495 get_rpccred(cred); 496 else { 497 rpc_destroy_wait_queue(&new->so_sequence.wait); 498 kfree(new); 499 } 500 return sp; 501 } 502 503 /** 504 * nfs4_put_state_owner - Release a nfs4_state_owner 505 * @sp: state owner data to release 506 * 507 */ 508 void nfs4_put_state_owner(struct nfs4_state_owner *sp) 509 { 510 struct nfs_client *clp = sp->so_server->nfs_client; 511 struct rpc_cred *cred = sp->so_cred; 512 513 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock)) 514 return; 515 nfs4_remove_state_owner_locked(sp); 516 spin_unlock(&clp->cl_lock); 517 rpc_destroy_wait_queue(&sp->so_sequence.wait); 518 put_rpccred(cred); 519 kfree(sp); 520 } 521 522 static struct nfs4_state * 523 nfs4_alloc_open_state(void) 524 { 525 struct nfs4_state *state; 526 527 state = kzalloc(sizeof(*state), GFP_NOFS); 528 if (!state) 529 return NULL; 530 atomic_set(&state->count, 1); 531 INIT_LIST_HEAD(&state->lock_states); 532 spin_lock_init(&state->state_lock); 533 seqlock_init(&state->seqlock); 534 return state; 535 } 536 537 void 538 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode) 539 { 540 if (state->state == fmode) 541 return; 542 /* NB! List reordering - see the reclaim code for why. */ 543 if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) { 544 if (fmode & FMODE_WRITE) 545 list_move(&state->open_states, &state->owner->so_states); 546 else 547 list_move_tail(&state->open_states, &state->owner->so_states); 548 } 549 state->state = fmode; 550 } 551 552 static struct nfs4_state * 553 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner) 554 { 555 struct nfs_inode *nfsi = NFS_I(inode); 556 struct nfs4_state *state; 557 558 list_for_each_entry(state, &nfsi->open_states, inode_states) { 559 if (state->owner != owner) 560 continue; 561 if (atomic_inc_not_zero(&state->count)) 562 return state; 563 } 564 return NULL; 565 } 566 567 static void 568 nfs4_free_open_state(struct nfs4_state *state) 569 { 570 kfree(state); 571 } 572 573 struct nfs4_state * 574 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner) 575 { 576 struct nfs4_state *state, *new; 577 struct nfs_inode *nfsi = NFS_I(inode); 578 579 spin_lock(&inode->i_lock); 580 state = __nfs4_find_state_byowner(inode, owner); 581 spin_unlock(&inode->i_lock); 582 if (state) 583 goto out; 584 new = nfs4_alloc_open_state(); 585 spin_lock(&owner->so_lock); 586 spin_lock(&inode->i_lock); 587 state = __nfs4_find_state_byowner(inode, owner); 588 if (state == NULL && new != NULL) { 589 state = new; 590 state->owner = owner; 591 atomic_inc(&owner->so_count); 592 list_add(&state->inode_states, &nfsi->open_states); 593 state->inode = igrab(inode); 594 spin_unlock(&inode->i_lock); 595 /* Note: The reclaim code dictates that we add stateless 596 * and read-only stateids to the end of the list */ 597 list_add_tail(&state->open_states, &owner->so_states); 598 spin_unlock(&owner->so_lock); 599 } else { 600 spin_unlock(&inode->i_lock); 601 spin_unlock(&owner->so_lock); 602 if (new) 603 nfs4_free_open_state(new); 604 } 605 out: 606 return state; 607 } 608 609 void nfs4_put_open_state(struct nfs4_state *state) 610 { 611 struct inode *inode = state->inode; 612 struct nfs4_state_owner *owner = state->owner; 613 614 if (!atomic_dec_and_lock(&state->count, &owner->so_lock)) 615 return; 616 spin_lock(&inode->i_lock); 617 list_del(&state->inode_states); 618 list_del(&state->open_states); 619 spin_unlock(&inode->i_lock); 620 spin_unlock(&owner->so_lock); 621 iput(inode); 622 nfs4_free_open_state(state); 623 nfs4_put_state_owner(owner); 624 } 625 626 /* 627 * Close the current file. 628 */ 629 static void __nfs4_close(struct path *path, struct nfs4_state *state, 630 fmode_t fmode, gfp_t gfp_mask, int wait) 631 { 632 struct nfs4_state_owner *owner = state->owner; 633 int call_close = 0; 634 fmode_t newstate; 635 636 atomic_inc(&owner->so_count); 637 /* Protect against nfs4_find_state() */ 638 spin_lock(&owner->so_lock); 639 switch (fmode & (FMODE_READ | FMODE_WRITE)) { 640 case FMODE_READ: 641 state->n_rdonly--; 642 break; 643 case FMODE_WRITE: 644 state->n_wronly--; 645 break; 646 case FMODE_READ|FMODE_WRITE: 647 state->n_rdwr--; 648 } 649 newstate = FMODE_READ|FMODE_WRITE; 650 if (state->n_rdwr == 0) { 651 if (state->n_rdonly == 0) { 652 newstate &= ~FMODE_READ; 653 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags); 654 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags); 655 } 656 if (state->n_wronly == 0) { 657 newstate &= ~FMODE_WRITE; 658 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags); 659 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags); 660 } 661 if (newstate == 0) 662 clear_bit(NFS_DELEGATED_STATE, &state->flags); 663 } 664 nfs4_state_set_mode_locked(state, newstate); 665 spin_unlock(&owner->so_lock); 666 667 if (!call_close) { 668 nfs4_put_open_state(state); 669 nfs4_put_state_owner(owner); 670 } else { 671 bool roc = pnfs_roc(state->inode); 672 673 nfs4_do_close(path, state, gfp_mask, wait, roc); 674 } 675 } 676 677 void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode) 678 { 679 __nfs4_close(path, state, fmode, GFP_NOFS, 0); 680 } 681 682 void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode) 683 { 684 __nfs4_close(path, state, fmode, GFP_KERNEL, 1); 685 } 686 687 /* 688 * Search the state->lock_states for an existing lock_owner 689 * that is compatible with current->files 690 */ 691 static struct nfs4_lock_state * 692 __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type) 693 { 694 struct nfs4_lock_state *pos; 695 list_for_each_entry(pos, &state->lock_states, ls_locks) { 696 if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type) 697 continue; 698 switch (pos->ls_owner.lo_type) { 699 case NFS4_POSIX_LOCK_TYPE: 700 if (pos->ls_owner.lo_u.posix_owner != fl_owner) 701 continue; 702 break; 703 case NFS4_FLOCK_LOCK_TYPE: 704 if (pos->ls_owner.lo_u.flock_owner != fl_pid) 705 continue; 706 } 707 atomic_inc(&pos->ls_count); 708 return pos; 709 } 710 return NULL; 711 } 712 713 /* 714 * Return a compatible lock_state. If no initialized lock_state structure 715 * exists, return an uninitialized one. 716 * 717 */ 718 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type) 719 { 720 struct nfs4_lock_state *lsp; 721 struct nfs_server *server = state->owner->so_server; 722 struct nfs_client *clp = server->nfs_client; 723 724 lsp = kzalloc(sizeof(*lsp), GFP_NOFS); 725 if (lsp == NULL) 726 return NULL; 727 rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue"); 728 spin_lock_init(&lsp->ls_sequence.lock); 729 INIT_LIST_HEAD(&lsp->ls_sequence.list); 730 lsp->ls_seqid.sequence = &lsp->ls_sequence; 731 atomic_set(&lsp->ls_count, 1); 732 lsp->ls_state = state; 733 lsp->ls_owner.lo_type = type; 734 switch (lsp->ls_owner.lo_type) { 735 case NFS4_FLOCK_LOCK_TYPE: 736 lsp->ls_owner.lo_u.flock_owner = fl_pid; 737 break; 738 case NFS4_POSIX_LOCK_TYPE: 739 lsp->ls_owner.lo_u.posix_owner = fl_owner; 740 break; 741 default: 742 kfree(lsp); 743 return NULL; 744 } 745 spin_lock(&clp->cl_lock); 746 nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64); 747 spin_unlock(&clp->cl_lock); 748 INIT_LIST_HEAD(&lsp->ls_locks); 749 return lsp; 750 } 751 752 static void nfs4_free_lock_state(struct nfs4_lock_state *lsp) 753 { 754 struct nfs_server *server = lsp->ls_state->owner->so_server; 755 struct nfs_client *clp = server->nfs_client; 756 757 spin_lock(&clp->cl_lock); 758 nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id); 759 spin_unlock(&clp->cl_lock); 760 rpc_destroy_wait_queue(&lsp->ls_sequence.wait); 761 kfree(lsp); 762 } 763 764 /* 765 * Return a compatible lock_state. If no initialized lock_state structure 766 * exists, return an uninitialized one. 767 * 768 */ 769 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type) 770 { 771 struct nfs4_lock_state *lsp, *new = NULL; 772 773 for(;;) { 774 spin_lock(&state->state_lock); 775 lsp = __nfs4_find_lock_state(state, owner, pid, type); 776 if (lsp != NULL) 777 break; 778 if (new != NULL) { 779 list_add(&new->ls_locks, &state->lock_states); 780 set_bit(LK_STATE_IN_USE, &state->flags); 781 lsp = new; 782 new = NULL; 783 break; 784 } 785 spin_unlock(&state->state_lock); 786 new = nfs4_alloc_lock_state(state, owner, pid, type); 787 if (new == NULL) 788 return NULL; 789 } 790 spin_unlock(&state->state_lock); 791 if (new != NULL) 792 nfs4_free_lock_state(new); 793 return lsp; 794 } 795 796 /* 797 * Release reference to lock_state, and free it if we see that 798 * it is no longer in use 799 */ 800 void nfs4_put_lock_state(struct nfs4_lock_state *lsp) 801 { 802 struct nfs4_state *state; 803 804 if (lsp == NULL) 805 return; 806 state = lsp->ls_state; 807 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock)) 808 return; 809 list_del(&lsp->ls_locks); 810 if (list_empty(&state->lock_states)) 811 clear_bit(LK_STATE_IN_USE, &state->flags); 812 spin_unlock(&state->state_lock); 813 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) 814 nfs4_release_lockowner(lsp); 815 nfs4_free_lock_state(lsp); 816 } 817 818 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src) 819 { 820 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner; 821 822 dst->fl_u.nfs4_fl.owner = lsp; 823 atomic_inc(&lsp->ls_count); 824 } 825 826 static void nfs4_fl_release_lock(struct file_lock *fl) 827 { 828 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner); 829 } 830 831 static const struct file_lock_operations nfs4_fl_lock_ops = { 832 .fl_copy_lock = nfs4_fl_copy_lock, 833 .fl_release_private = nfs4_fl_release_lock, 834 }; 835 836 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl) 837 { 838 struct nfs4_lock_state *lsp; 839 840 if (fl->fl_ops != NULL) 841 return 0; 842 if (fl->fl_flags & FL_POSIX) 843 lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE); 844 else if (fl->fl_flags & FL_FLOCK) 845 lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE); 846 else 847 return -EINVAL; 848 if (lsp == NULL) 849 return -ENOMEM; 850 fl->fl_u.nfs4_fl.owner = lsp; 851 fl->fl_ops = &nfs4_fl_lock_ops; 852 return 0; 853 } 854 855 /* 856 * Byte-range lock aware utility to initialize the stateid of read/write 857 * requests. 858 */ 859 void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid) 860 { 861 struct nfs4_lock_state *lsp; 862 int seq; 863 864 do { 865 seq = read_seqbegin(&state->seqlock); 866 memcpy(dst, &state->stateid, sizeof(*dst)); 867 } while (read_seqretry(&state->seqlock, seq)); 868 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0) 869 return; 870 871 spin_lock(&state->state_lock); 872 lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE); 873 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) 874 memcpy(dst, &lsp->ls_stateid, sizeof(*dst)); 875 spin_unlock(&state->state_lock); 876 nfs4_put_lock_state(lsp); 877 } 878 879 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask) 880 { 881 struct nfs_seqid *new; 882 883 new = kmalloc(sizeof(*new), gfp_mask); 884 if (new != NULL) { 885 new->sequence = counter; 886 INIT_LIST_HEAD(&new->list); 887 } 888 return new; 889 } 890 891 void nfs_release_seqid(struct nfs_seqid *seqid) 892 { 893 if (!list_empty(&seqid->list)) { 894 struct rpc_sequence *sequence = seqid->sequence->sequence; 895 896 spin_lock(&sequence->lock); 897 list_del_init(&seqid->list); 898 spin_unlock(&sequence->lock); 899 rpc_wake_up(&sequence->wait); 900 } 901 } 902 903 void nfs_free_seqid(struct nfs_seqid *seqid) 904 { 905 nfs_release_seqid(seqid); 906 kfree(seqid); 907 } 908 909 /* 910 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or 911 * failed with a seqid incrementing error - 912 * see comments nfs_fs.h:seqid_mutating_error() 913 */ 914 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid) 915 { 916 BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid); 917 switch (status) { 918 case 0: 919 break; 920 case -NFS4ERR_BAD_SEQID: 921 if (seqid->sequence->flags & NFS_SEQID_CONFIRMED) 922 return; 923 printk(KERN_WARNING "NFS: v4 server returned a bad" 924 " sequence-id error on an" 925 " unconfirmed sequence %p!\n", 926 seqid->sequence); 927 case -NFS4ERR_STALE_CLIENTID: 928 case -NFS4ERR_STALE_STATEID: 929 case -NFS4ERR_BAD_STATEID: 930 case -NFS4ERR_BADXDR: 931 case -NFS4ERR_RESOURCE: 932 case -NFS4ERR_NOFILEHANDLE: 933 /* Non-seqid mutating errors */ 934 return; 935 }; 936 /* 937 * Note: no locking needed as we are guaranteed to be first 938 * on the sequence list 939 */ 940 seqid->sequence->counter++; 941 } 942 943 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid) 944 { 945 struct nfs4_state_owner *sp = container_of(seqid->sequence, 946 struct nfs4_state_owner, so_seqid); 947 struct nfs_server *server = sp->so_server; 948 949 if (status == -NFS4ERR_BAD_SEQID) 950 nfs4_drop_state_owner(sp); 951 if (!nfs4_has_session(server->nfs_client)) 952 nfs_increment_seqid(status, seqid); 953 } 954 955 /* 956 * Increment the seqid if the LOCK/LOCKU succeeded, or 957 * failed with a seqid incrementing error - 958 * see comments nfs_fs.h:seqid_mutating_error() 959 */ 960 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid) 961 { 962 nfs_increment_seqid(status, seqid); 963 } 964 965 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task) 966 { 967 struct rpc_sequence *sequence = seqid->sequence->sequence; 968 int status = 0; 969 970 spin_lock(&sequence->lock); 971 if (list_empty(&seqid->list)) 972 list_add_tail(&seqid->list, &sequence->list); 973 if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid) 974 goto unlock; 975 rpc_sleep_on(&sequence->wait, task, NULL); 976 status = -EAGAIN; 977 unlock: 978 spin_unlock(&sequence->lock); 979 return status; 980 } 981 982 static int nfs4_run_state_manager(void *); 983 984 static void nfs4_clear_state_manager_bit(struct nfs_client *clp) 985 { 986 smp_mb__before_clear_bit(); 987 clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state); 988 smp_mb__after_clear_bit(); 989 wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING); 990 rpc_wake_up(&clp->cl_rpcwaitq); 991 } 992 993 /* 994 * Schedule the nfs_client asynchronous state management routine 995 */ 996 void nfs4_schedule_state_manager(struct nfs_client *clp) 997 { 998 struct task_struct *task; 999 1000 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0) 1001 return; 1002 __module_get(THIS_MODULE); 1003 atomic_inc(&clp->cl_count); 1004 task = kthread_run(nfs4_run_state_manager, clp, "%s-manager", 1005 rpc_peeraddr2str(clp->cl_rpcclient, 1006 RPC_DISPLAY_ADDR)); 1007 if (!IS_ERR(task)) 1008 return; 1009 nfs4_clear_state_manager_bit(clp); 1010 nfs_put_client(clp); 1011 module_put(THIS_MODULE); 1012 } 1013 1014 /* 1015 * Schedule a lease recovery attempt 1016 */ 1017 void nfs4_schedule_lease_recovery(struct nfs_client *clp) 1018 { 1019 if (!clp) 1020 return; 1021 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) 1022 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state); 1023 nfs4_schedule_state_manager(clp); 1024 } 1025 1026 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state) 1027 { 1028 1029 set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags); 1030 /* Don't recover state that expired before the reboot */ 1031 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) { 1032 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags); 1033 return 0; 1034 } 1035 set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags); 1036 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state); 1037 return 1; 1038 } 1039 1040 static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state) 1041 { 1042 set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags); 1043 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags); 1044 set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags); 1045 set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state); 1046 return 1; 1047 } 1048 1049 void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state) 1050 { 1051 struct nfs_client *clp = server->nfs_client; 1052 1053 nfs4_state_mark_reclaim_nograce(clp, state); 1054 nfs4_schedule_state_manager(clp); 1055 } 1056 1057 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops) 1058 { 1059 struct inode *inode = state->inode; 1060 struct nfs_inode *nfsi = NFS_I(inode); 1061 struct file_lock *fl; 1062 int status = 0; 1063 1064 if (inode->i_flock == NULL) 1065 return 0; 1066 1067 /* Guard against delegation returns and new lock/unlock calls */ 1068 down_write(&nfsi->rwsem); 1069 /* Protect inode->i_flock using the BKL */ 1070 lock_flocks(); 1071 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { 1072 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) 1073 continue; 1074 if (nfs_file_open_context(fl->fl_file)->state != state) 1075 continue; 1076 unlock_flocks(); 1077 status = ops->recover_lock(state, fl); 1078 switch (status) { 1079 case 0: 1080 break; 1081 case -ESTALE: 1082 case -NFS4ERR_ADMIN_REVOKED: 1083 case -NFS4ERR_STALE_STATEID: 1084 case -NFS4ERR_BAD_STATEID: 1085 case -NFS4ERR_EXPIRED: 1086 case -NFS4ERR_NO_GRACE: 1087 case -NFS4ERR_STALE_CLIENTID: 1088 case -NFS4ERR_BADSESSION: 1089 case -NFS4ERR_BADSLOT: 1090 case -NFS4ERR_BAD_HIGH_SLOT: 1091 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 1092 goto out; 1093 default: 1094 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n", 1095 __func__, status); 1096 case -ENOMEM: 1097 case -NFS4ERR_DENIED: 1098 case -NFS4ERR_RECLAIM_BAD: 1099 case -NFS4ERR_RECLAIM_CONFLICT: 1100 /* kill_proc(fl->fl_pid, SIGLOST, 1); */ 1101 status = 0; 1102 } 1103 lock_flocks(); 1104 } 1105 unlock_flocks(); 1106 out: 1107 up_write(&nfsi->rwsem); 1108 return status; 1109 } 1110 1111 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops) 1112 { 1113 struct nfs4_state *state; 1114 struct nfs4_lock_state *lock; 1115 int status = 0; 1116 1117 /* Note: we rely on the sp->so_states list being ordered 1118 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE) 1119 * states first. 1120 * This is needed to ensure that the server won't give us any 1121 * read delegations that we have to return if, say, we are 1122 * recovering after a network partition or a reboot from a 1123 * server that doesn't support a grace period. 1124 */ 1125 restart: 1126 spin_lock(&sp->so_lock); 1127 list_for_each_entry(state, &sp->so_states, open_states) { 1128 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags)) 1129 continue; 1130 if (state->state == 0) 1131 continue; 1132 atomic_inc(&state->count); 1133 spin_unlock(&sp->so_lock); 1134 status = ops->recover_open(sp, state); 1135 if (status >= 0) { 1136 status = nfs4_reclaim_locks(state, ops); 1137 if (status >= 0) { 1138 list_for_each_entry(lock, &state->lock_states, ls_locks) { 1139 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED)) 1140 printk("%s: Lock reclaim failed!\n", 1141 __func__); 1142 } 1143 nfs4_put_open_state(state); 1144 goto restart; 1145 } 1146 } 1147 switch (status) { 1148 default: 1149 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n", 1150 __func__, status); 1151 case -ENOENT: 1152 case -ENOMEM: 1153 case -ESTALE: 1154 /* 1155 * Open state on this file cannot be recovered 1156 * All we can do is revert to using the zero stateid. 1157 */ 1158 memset(state->stateid.data, 0, 1159 sizeof(state->stateid.data)); 1160 /* Mark the file as being 'closed' */ 1161 state->state = 0; 1162 break; 1163 case -EKEYEXPIRED: 1164 /* 1165 * User RPCSEC_GSS context has expired. 1166 * We cannot recover this stateid now, so 1167 * skip it and allow recovery thread to 1168 * proceed. 1169 */ 1170 break; 1171 case -NFS4ERR_ADMIN_REVOKED: 1172 case -NFS4ERR_STALE_STATEID: 1173 case -NFS4ERR_BAD_STATEID: 1174 case -NFS4ERR_RECLAIM_BAD: 1175 case -NFS4ERR_RECLAIM_CONFLICT: 1176 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state); 1177 break; 1178 case -NFS4ERR_EXPIRED: 1179 case -NFS4ERR_NO_GRACE: 1180 nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state); 1181 case -NFS4ERR_STALE_CLIENTID: 1182 case -NFS4ERR_BADSESSION: 1183 case -NFS4ERR_BADSLOT: 1184 case -NFS4ERR_BAD_HIGH_SLOT: 1185 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 1186 goto out_err; 1187 } 1188 nfs4_put_open_state(state); 1189 goto restart; 1190 } 1191 spin_unlock(&sp->so_lock); 1192 return 0; 1193 out_err: 1194 nfs4_put_open_state(state); 1195 return status; 1196 } 1197 1198 static void nfs4_clear_open_state(struct nfs4_state *state) 1199 { 1200 struct nfs4_lock_state *lock; 1201 1202 clear_bit(NFS_DELEGATED_STATE, &state->flags); 1203 clear_bit(NFS_O_RDONLY_STATE, &state->flags); 1204 clear_bit(NFS_O_WRONLY_STATE, &state->flags); 1205 clear_bit(NFS_O_RDWR_STATE, &state->flags); 1206 list_for_each_entry(lock, &state->lock_states, ls_locks) { 1207 lock->ls_seqid.flags = 0; 1208 lock->ls_flags &= ~NFS_LOCK_INITIALIZED; 1209 } 1210 } 1211 1212 static void nfs4_reset_seqids(struct nfs_server *server, 1213 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state)) 1214 { 1215 struct nfs_client *clp = server->nfs_client; 1216 struct nfs4_state_owner *sp; 1217 struct rb_node *pos; 1218 struct nfs4_state *state; 1219 1220 spin_lock(&clp->cl_lock); 1221 for (pos = rb_first(&server->state_owners); 1222 pos != NULL; 1223 pos = rb_next(pos)) { 1224 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node); 1225 sp->so_seqid.flags = 0; 1226 spin_lock(&sp->so_lock); 1227 list_for_each_entry(state, &sp->so_states, open_states) { 1228 if (mark_reclaim(clp, state)) 1229 nfs4_clear_open_state(state); 1230 } 1231 spin_unlock(&sp->so_lock); 1232 } 1233 spin_unlock(&clp->cl_lock); 1234 } 1235 1236 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp, 1237 int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state)) 1238 { 1239 struct nfs_server *server; 1240 1241 rcu_read_lock(); 1242 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1243 nfs4_reset_seqids(server, mark_reclaim); 1244 rcu_read_unlock(); 1245 } 1246 1247 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp) 1248 { 1249 /* Mark all delegations for reclaim */ 1250 nfs_delegation_mark_reclaim(clp); 1251 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot); 1252 } 1253 1254 static void nfs4_reclaim_complete(struct nfs_client *clp, 1255 const struct nfs4_state_recovery_ops *ops) 1256 { 1257 /* Notify the server we're done reclaiming our state */ 1258 if (ops->reclaim_complete) 1259 (void)ops->reclaim_complete(clp); 1260 } 1261 1262 static void nfs4_clear_reclaim_server(struct nfs_server *server) 1263 { 1264 struct nfs_client *clp = server->nfs_client; 1265 struct nfs4_state_owner *sp; 1266 struct rb_node *pos; 1267 struct nfs4_state *state; 1268 1269 spin_lock(&clp->cl_lock); 1270 for (pos = rb_first(&server->state_owners); 1271 pos != NULL; 1272 pos = rb_next(pos)) { 1273 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node); 1274 spin_lock(&sp->so_lock); 1275 list_for_each_entry(state, &sp->so_states, open_states) { 1276 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT, 1277 &state->flags)) 1278 continue; 1279 nfs4_state_mark_reclaim_nograce(clp, state); 1280 } 1281 spin_unlock(&sp->so_lock); 1282 } 1283 spin_unlock(&clp->cl_lock); 1284 } 1285 1286 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp) 1287 { 1288 struct nfs_server *server; 1289 1290 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) 1291 return 0; 1292 1293 rcu_read_lock(); 1294 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1295 nfs4_clear_reclaim_server(server); 1296 rcu_read_unlock(); 1297 1298 nfs_delegation_reap_unclaimed(clp); 1299 return 1; 1300 } 1301 1302 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp) 1303 { 1304 if (!nfs4_state_clear_reclaim_reboot(clp)) 1305 return; 1306 nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops); 1307 } 1308 1309 static void nfs_delegation_clear_all(struct nfs_client *clp) 1310 { 1311 nfs_delegation_mark_reclaim(clp); 1312 nfs_delegation_reap_unclaimed(clp); 1313 } 1314 1315 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp) 1316 { 1317 nfs_delegation_clear_all(clp); 1318 nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce); 1319 } 1320 1321 static void nfs4_warn_keyexpired(const char *s) 1322 { 1323 printk_ratelimited(KERN_WARNING "Error: state manager" 1324 " encountered RPCSEC_GSS session" 1325 " expired against NFSv4 server %s.\n", 1326 s); 1327 } 1328 1329 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error) 1330 { 1331 switch (error) { 1332 case -NFS4ERR_CB_PATH_DOWN: 1333 nfs_handle_cb_pathdown(clp); 1334 return 0; 1335 case -NFS4ERR_NO_GRACE: 1336 nfs4_state_end_reclaim_reboot(clp); 1337 return 0; 1338 case -NFS4ERR_STALE_CLIENTID: 1339 case -NFS4ERR_LEASE_MOVED: 1340 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); 1341 nfs4_state_clear_reclaim_reboot(clp); 1342 nfs4_state_start_reclaim_reboot(clp); 1343 break; 1344 case -NFS4ERR_EXPIRED: 1345 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); 1346 nfs4_state_start_reclaim_nograce(clp); 1347 break; 1348 case -NFS4ERR_BADSESSION: 1349 case -NFS4ERR_BADSLOT: 1350 case -NFS4ERR_BAD_HIGH_SLOT: 1351 case -NFS4ERR_DEADSESSION: 1352 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 1353 case -NFS4ERR_SEQ_FALSE_RETRY: 1354 case -NFS4ERR_SEQ_MISORDERED: 1355 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state); 1356 /* Zero session reset errors */ 1357 return 0; 1358 case -EKEYEXPIRED: 1359 /* Nothing we can do */ 1360 nfs4_warn_keyexpired(clp->cl_hostname); 1361 return 0; 1362 } 1363 return error; 1364 } 1365 1366 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops) 1367 { 1368 struct nfs4_state_owner *sp; 1369 struct nfs_server *server; 1370 struct rb_node *pos; 1371 int status = 0; 1372 1373 restart: 1374 rcu_read_lock(); 1375 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 1376 spin_lock(&clp->cl_lock); 1377 for (pos = rb_first(&server->state_owners); 1378 pos != NULL; 1379 pos = rb_next(pos)) { 1380 sp = rb_entry(pos, 1381 struct nfs4_state_owner, so_server_node); 1382 if (!test_and_clear_bit(ops->owner_flag_bit, 1383 &sp->so_flags)) 1384 continue; 1385 atomic_inc(&sp->so_count); 1386 spin_unlock(&clp->cl_lock); 1387 rcu_read_unlock(); 1388 1389 status = nfs4_reclaim_open_state(sp, ops); 1390 if (status < 0) { 1391 set_bit(ops->owner_flag_bit, &sp->so_flags); 1392 nfs4_put_state_owner(sp); 1393 return nfs4_recovery_handle_error(clp, status); 1394 } 1395 1396 nfs4_put_state_owner(sp); 1397 goto restart; 1398 } 1399 spin_unlock(&clp->cl_lock); 1400 } 1401 rcu_read_unlock(); 1402 return status; 1403 } 1404 1405 static int nfs4_check_lease(struct nfs_client *clp) 1406 { 1407 struct rpc_cred *cred; 1408 const struct nfs4_state_maintenance_ops *ops = 1409 clp->cl_mvops->state_renewal_ops; 1410 int status = -NFS4ERR_EXPIRED; 1411 1412 /* Is the client already known to have an expired lease? */ 1413 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) 1414 return 0; 1415 spin_lock(&clp->cl_lock); 1416 cred = ops->get_state_renewal_cred_locked(clp); 1417 spin_unlock(&clp->cl_lock); 1418 if (cred == NULL) { 1419 cred = nfs4_get_setclientid_cred(clp); 1420 if (cred == NULL) 1421 goto out; 1422 } 1423 status = ops->renew_lease(clp, cred); 1424 put_rpccred(cred); 1425 out: 1426 return nfs4_recovery_handle_error(clp, status); 1427 } 1428 1429 static int nfs4_reclaim_lease(struct nfs_client *clp) 1430 { 1431 struct rpc_cred *cred; 1432 const struct nfs4_state_recovery_ops *ops = 1433 clp->cl_mvops->reboot_recovery_ops; 1434 int status = -ENOENT; 1435 1436 cred = ops->get_clid_cred(clp); 1437 if (cred != NULL) { 1438 status = ops->establish_clid(clp, cred); 1439 put_rpccred(cred); 1440 /* Handle case where the user hasn't set up machine creds */ 1441 if (status == -EACCES && cred == clp->cl_machine_cred) { 1442 nfs4_clear_machine_cred(clp); 1443 status = -EAGAIN; 1444 } 1445 if (status == -NFS4ERR_MINOR_VERS_MISMATCH) 1446 status = -EPROTONOSUPPORT; 1447 } 1448 return status; 1449 } 1450 1451 #ifdef CONFIG_NFS_V4_1 1452 void nfs4_schedule_session_recovery(struct nfs4_session *session) 1453 { 1454 nfs4_schedule_lease_recovery(session->clp); 1455 } 1456 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery); 1457 1458 void nfs41_handle_recall_slot(struct nfs_client *clp) 1459 { 1460 set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state); 1461 nfs4_schedule_state_manager(clp); 1462 } 1463 1464 static void nfs4_reset_all_state(struct nfs_client *clp) 1465 { 1466 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) { 1467 clp->cl_boot_time = CURRENT_TIME; 1468 nfs4_state_start_reclaim_nograce(clp); 1469 nfs4_schedule_state_manager(clp); 1470 } 1471 } 1472 1473 static void nfs41_handle_server_reboot(struct nfs_client *clp) 1474 { 1475 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) { 1476 nfs4_state_start_reclaim_reboot(clp); 1477 nfs4_schedule_state_manager(clp); 1478 } 1479 } 1480 1481 static void nfs41_handle_state_revoked(struct nfs_client *clp) 1482 { 1483 /* Temporary */ 1484 nfs4_reset_all_state(clp); 1485 } 1486 1487 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp) 1488 { 1489 /* This will need to handle layouts too */ 1490 nfs_expire_all_delegations(clp); 1491 } 1492 1493 static void nfs41_handle_cb_path_down(struct nfs_client *clp) 1494 { 1495 nfs_expire_all_delegations(clp); 1496 if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0) 1497 nfs4_schedule_state_manager(clp); 1498 } 1499 1500 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags) 1501 { 1502 if (!flags) 1503 return; 1504 else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED) 1505 nfs41_handle_server_reboot(clp); 1506 else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED | 1507 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED | 1508 SEQ4_STATUS_ADMIN_STATE_REVOKED | 1509 SEQ4_STATUS_LEASE_MOVED)) 1510 nfs41_handle_state_revoked(clp); 1511 else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED) 1512 nfs41_handle_recallable_state_revoked(clp); 1513 else if (flags & (SEQ4_STATUS_CB_PATH_DOWN | 1514 SEQ4_STATUS_BACKCHANNEL_FAULT | 1515 SEQ4_STATUS_CB_PATH_DOWN_SESSION)) 1516 nfs41_handle_cb_path_down(clp); 1517 } 1518 1519 static int nfs4_reset_session(struct nfs_client *clp) 1520 { 1521 int status; 1522 1523 nfs4_begin_drain_session(clp); 1524 status = nfs4_proc_destroy_session(clp->cl_session); 1525 if (status && status != -NFS4ERR_BADSESSION && 1526 status != -NFS4ERR_DEADSESSION) { 1527 status = nfs4_recovery_handle_error(clp, status); 1528 goto out; 1529 } 1530 1531 memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN); 1532 status = nfs4_proc_create_session(clp); 1533 if (status) { 1534 status = nfs4_recovery_handle_error(clp, status); 1535 goto out; 1536 } 1537 /* create_session negotiated new slot table */ 1538 clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state); 1539 1540 /* Let the state manager reestablish state */ 1541 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) 1542 nfs41_setup_state_renewal(clp); 1543 out: 1544 return status; 1545 } 1546 1547 static int nfs4_recall_slot(struct nfs_client *clp) 1548 { 1549 struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table; 1550 struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs; 1551 struct nfs4_slot *new, *old; 1552 int i; 1553 1554 nfs4_begin_drain_session(clp); 1555 new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot), 1556 GFP_NOFS); 1557 if (!new) 1558 return -ENOMEM; 1559 1560 spin_lock(&fc_tbl->slot_tbl_lock); 1561 for (i = 0; i < fc_tbl->target_max_slots; i++) 1562 new[i].seq_nr = fc_tbl->slots[i].seq_nr; 1563 old = fc_tbl->slots; 1564 fc_tbl->slots = new; 1565 fc_tbl->max_slots = fc_tbl->target_max_slots; 1566 fc_tbl->target_max_slots = 0; 1567 fc_attrs->max_reqs = fc_tbl->max_slots; 1568 spin_unlock(&fc_tbl->slot_tbl_lock); 1569 1570 kfree(old); 1571 nfs4_end_drain_session(clp); 1572 return 0; 1573 } 1574 1575 #else /* CONFIG_NFS_V4_1 */ 1576 static int nfs4_reset_session(struct nfs_client *clp) { return 0; } 1577 static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; } 1578 static int nfs4_recall_slot(struct nfs_client *clp) { return 0; } 1579 #endif /* CONFIG_NFS_V4_1 */ 1580 1581 /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors 1582 * on EXCHANGE_ID for v4.1 1583 */ 1584 static void nfs4_set_lease_expired(struct nfs_client *clp, int status) 1585 { 1586 if (nfs4_has_session(clp)) { 1587 switch (status) { 1588 case -NFS4ERR_DELAY: 1589 case -NFS4ERR_CLID_INUSE: 1590 case -EAGAIN: 1591 break; 1592 1593 case -EKEYEXPIRED: 1594 nfs4_warn_keyexpired(clp->cl_hostname); 1595 case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery 1596 * in nfs4_exchange_id */ 1597 default: 1598 return; 1599 } 1600 } 1601 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); 1602 } 1603 1604 static void nfs4_state_manager(struct nfs_client *clp) 1605 { 1606 int status = 0; 1607 1608 /* Ensure exclusive access to NFSv4 state */ 1609 for(;;) { 1610 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) { 1611 /* We're going to have to re-establish a clientid */ 1612 status = nfs4_reclaim_lease(clp); 1613 if (status) { 1614 nfs4_set_lease_expired(clp, status); 1615 if (test_bit(NFS4CLNT_LEASE_EXPIRED, 1616 &clp->cl_state)) 1617 continue; 1618 if (clp->cl_cons_state == 1619 NFS_CS_SESSION_INITING) 1620 nfs_mark_client_ready(clp, status); 1621 goto out_error; 1622 } 1623 clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state); 1624 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state); 1625 pnfs_destroy_all_layouts(clp); 1626 } 1627 1628 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) { 1629 status = nfs4_check_lease(clp); 1630 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) 1631 continue; 1632 if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN) 1633 goto out_error; 1634 } 1635 1636 /* Initialize or reset the session */ 1637 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) 1638 && nfs4_has_session(clp)) { 1639 status = nfs4_reset_session(clp); 1640 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) 1641 continue; 1642 if (status < 0) 1643 goto out_error; 1644 } 1645 1646 /* First recover reboot state... */ 1647 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) { 1648 status = nfs4_do_reclaim(clp, 1649 clp->cl_mvops->reboot_recovery_ops); 1650 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) || 1651 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) 1652 continue; 1653 nfs4_state_end_reclaim_reboot(clp); 1654 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) 1655 continue; 1656 if (status < 0) 1657 goto out_error; 1658 } 1659 1660 /* Now recover expired state... */ 1661 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) { 1662 status = nfs4_do_reclaim(clp, 1663 clp->cl_mvops->nograce_recovery_ops); 1664 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) || 1665 test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) || 1666 test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) 1667 continue; 1668 if (status < 0) 1669 goto out_error; 1670 } 1671 1672 nfs4_end_drain_session(clp); 1673 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) { 1674 nfs_client_return_marked_delegations(clp); 1675 continue; 1676 } 1677 /* Recall session slots */ 1678 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state) 1679 && nfs4_has_session(clp)) { 1680 status = nfs4_recall_slot(clp); 1681 if (status < 0) 1682 goto out_error; 1683 continue; 1684 } 1685 1686 1687 nfs4_clear_state_manager_bit(clp); 1688 /* Did we race with an attempt to give us more work? */ 1689 if (clp->cl_state == 0) 1690 break; 1691 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0) 1692 break; 1693 } 1694 return; 1695 out_error: 1696 printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s" 1697 " with error %d\n", clp->cl_hostname, -status); 1698 nfs4_end_drain_session(clp); 1699 nfs4_clear_state_manager_bit(clp); 1700 } 1701 1702 static int nfs4_run_state_manager(void *ptr) 1703 { 1704 struct nfs_client *clp = ptr; 1705 1706 allow_signal(SIGKILL); 1707 nfs4_state_manager(clp); 1708 nfs_put_client(clp); 1709 module_put_and_exit(0); 1710 return 0; 1711 } 1712 1713 /* 1714 * Local variables: 1715 * c-basic-offset: 8 1716 * End: 1717 */ 1718