1 /* 2 * fs/nfs/nfs4session.c 3 * 4 * Copyright (c) 2012 Trond Myklebust <Trond.Myklebust@netapp.com> 5 * 6 */ 7 #include <linux/kernel.h> 8 #include <linux/errno.h> 9 #include <linux/string.h> 10 #include <linux/printk.h> 11 #include <linux/slab.h> 12 #include <linux/sunrpc/sched.h> 13 #include <linux/sunrpc/bc_xprt.h> 14 #include <linux/nfs.h> 15 #include <linux/nfs4.h> 16 #include <linux/nfs_fs.h> 17 #include <linux/module.h> 18 19 #include "nfs4_fs.h" 20 #include "internal.h" 21 #include "nfs4session.h" 22 #include "callback.h" 23 24 #define NFSDBG_FACILITY NFSDBG_STATE 25 26 static void nfs4_init_slot_table(struct nfs4_slot_table *tbl, const char *queue) 27 { 28 tbl->highest_used_slotid = NFS4_NO_SLOT; 29 spin_lock_init(&tbl->slot_tbl_lock); 30 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, queue); 31 init_waitqueue_head(&tbl->slot_waitq); 32 init_completion(&tbl->complete); 33 } 34 35 /* 36 * nfs4_shrink_slot_table - free retired slots from the slot table 37 */ 38 static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize) 39 { 40 struct nfs4_slot **p; 41 if (newsize >= tbl->max_slots) 42 return; 43 44 p = &tbl->slots; 45 while (newsize--) 46 p = &(*p)->next; 47 while (*p) { 48 struct nfs4_slot *slot = *p; 49 50 *p = slot->next; 51 kfree(slot); 52 tbl->max_slots--; 53 } 54 } 55 56 /** 57 * nfs4_slot_tbl_drain_complete - wake waiters when drain is complete 58 * @tbl: controlling slot table 59 * 60 */ 61 void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl) 62 { 63 if (nfs4_slot_tbl_draining(tbl)) 64 complete(&tbl->complete); 65 } 66 67 /* 68 * nfs4_free_slot - free a slot and efficiently update slot table. 69 * 70 * freeing a slot is trivially done by clearing its respective bit 71 * in the bitmap. 72 * If the freed slotid equals highest_used_slotid we want to update it 73 * so that the server would be able to size down the slot table if needed, 74 * otherwise we know that the highest_used_slotid is still in use. 75 * When updating highest_used_slotid there may be "holes" in the bitmap 76 * so we need to scan down from highest_used_slotid to 0 looking for the now 77 * highest slotid in use. 78 * If none found, highest_used_slotid is set to NFS4_NO_SLOT. 79 * 80 * Must be called while holding tbl->slot_tbl_lock 81 */ 82 void nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot) 83 { 84 u32 slotid = slot->slot_nr; 85 86 /* clear used bit in bitmap */ 87 __clear_bit(slotid, tbl->used_slots); 88 89 /* update highest_used_slotid when it is freed */ 90 if (slotid == tbl->highest_used_slotid) { 91 u32 new_max = find_last_bit(tbl->used_slots, slotid); 92 if (new_max < slotid) 93 tbl->highest_used_slotid = new_max; 94 else { 95 tbl->highest_used_slotid = NFS4_NO_SLOT; 96 nfs4_slot_tbl_drain_complete(tbl); 97 } 98 } 99 dprintk("%s: slotid %u highest_used_slotid %u\n", __func__, 100 slotid, tbl->highest_used_slotid); 101 } 102 103 static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl, 104 u32 slotid, u32 seq_init, gfp_t gfp_mask) 105 { 106 struct nfs4_slot *slot; 107 108 slot = kzalloc(sizeof(*slot), gfp_mask); 109 if (slot) { 110 slot->table = tbl; 111 slot->slot_nr = slotid; 112 slot->seq_nr = seq_init; 113 slot->seq_nr_highest_sent = seq_init; 114 slot->seq_nr_last_acked = seq_init - 1; 115 } 116 return slot; 117 } 118 119 static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl, 120 u32 slotid, u32 seq_init, gfp_t gfp_mask) 121 { 122 struct nfs4_slot **p, *slot; 123 124 p = &tbl->slots; 125 for (;;) { 126 if (*p == NULL) { 127 *p = nfs4_new_slot(tbl, tbl->max_slots, 128 seq_init, gfp_mask); 129 if (*p == NULL) 130 break; 131 tbl->max_slots++; 132 } 133 slot = *p; 134 if (slot->slot_nr == slotid) 135 return slot; 136 p = &slot->next; 137 } 138 return ERR_PTR(-ENOMEM); 139 } 140 141 static void nfs4_lock_slot(struct nfs4_slot_table *tbl, 142 struct nfs4_slot *slot) 143 { 144 u32 slotid = slot->slot_nr; 145 146 __set_bit(slotid, tbl->used_slots); 147 if (slotid > tbl->highest_used_slotid || 148 tbl->highest_used_slotid == NFS4_NO_SLOT) 149 tbl->highest_used_slotid = slotid; 150 slot->generation = tbl->generation; 151 } 152 153 /* 154 * nfs4_try_to_lock_slot - Given a slot try to allocate it 155 * 156 * Note: must be called with the slot_tbl_lock held. 157 */ 158 bool nfs4_try_to_lock_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot) 159 { 160 if (nfs4_test_locked_slot(tbl, slot->slot_nr)) 161 return false; 162 nfs4_lock_slot(tbl, slot); 163 return true; 164 } 165 166 /* 167 * nfs4_lookup_slot - Find a slot but don't allocate it 168 * 169 * Note: must be called with the slot_tbl_lock held. 170 */ 171 struct nfs4_slot *nfs4_lookup_slot(struct nfs4_slot_table *tbl, u32 slotid) 172 { 173 if (slotid <= tbl->max_slotid) 174 return nfs4_find_or_create_slot(tbl, slotid, 0, GFP_NOWAIT); 175 return ERR_PTR(-E2BIG); 176 } 177 178 static int nfs4_slot_get_seqid(struct nfs4_slot_table *tbl, u32 slotid, 179 u32 *seq_nr) 180 __must_hold(&tbl->slot_tbl_lock) 181 { 182 struct nfs4_slot *slot; 183 int ret; 184 185 slot = nfs4_lookup_slot(tbl, slotid); 186 ret = PTR_ERR_OR_ZERO(slot); 187 if (!ret) 188 *seq_nr = slot->seq_nr; 189 190 return ret; 191 } 192 193 /* 194 * nfs4_slot_seqid_in_use - test if a slot sequence id is still in use 195 * 196 * Given a slot table, slot id and sequence number, determine if the 197 * RPC call in question is still in flight. This function is mainly 198 * intended for use by the callback channel. 199 */ 200 static bool nfs4_slot_seqid_in_use(struct nfs4_slot_table *tbl, 201 u32 slotid, u32 seq_nr) 202 { 203 u32 cur_seq = 0; 204 bool ret = false; 205 206 spin_lock(&tbl->slot_tbl_lock); 207 if (nfs4_slot_get_seqid(tbl, slotid, &cur_seq) == 0 && 208 cur_seq == seq_nr && test_bit(slotid, tbl->used_slots)) 209 ret = true; 210 spin_unlock(&tbl->slot_tbl_lock); 211 return ret; 212 } 213 214 /* 215 * nfs4_slot_wait_on_seqid - wait until a slot sequence id is complete 216 * 217 * Given a slot table, slot id and sequence number, wait until the 218 * corresponding RPC call completes. This function is mainly 219 * intended for use by the callback channel. 220 */ 221 int nfs4_slot_wait_on_seqid(struct nfs4_slot_table *tbl, 222 u32 slotid, u32 seq_nr, 223 unsigned long timeout) 224 { 225 if (wait_event_timeout(tbl->slot_waitq, 226 !nfs4_slot_seqid_in_use(tbl, slotid, seq_nr), 227 timeout) == 0) 228 return -ETIMEDOUT; 229 return 0; 230 } 231 232 /* 233 * nfs4_alloc_slot - efficiently look for a free slot 234 * 235 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap. 236 * If found, we mark the slot as used, update the highest_used_slotid, 237 * and respectively set up the sequence operation args. 238 * 239 * Note: must be called with under the slot_tbl_lock. 240 */ 241 struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl) 242 { 243 struct nfs4_slot *ret = ERR_PTR(-EBUSY); 244 u32 slotid; 245 246 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n", 247 __func__, tbl->used_slots[0], tbl->highest_used_slotid, 248 tbl->max_slotid + 1); 249 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1); 250 if (slotid <= tbl->max_slotid) { 251 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT); 252 if (!IS_ERR(ret)) 253 nfs4_lock_slot(tbl, ret); 254 } 255 dprintk("<-- %s used_slots=%04lx highest_used=%u slotid=%u\n", 256 __func__, tbl->used_slots[0], tbl->highest_used_slotid, 257 !IS_ERR(ret) ? ret->slot_nr : NFS4_NO_SLOT); 258 return ret; 259 } 260 261 static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl, 262 u32 max_reqs, u32 ivalue) 263 { 264 if (max_reqs <= tbl->max_slots) 265 return 0; 266 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS))) 267 return 0; 268 return -ENOMEM; 269 } 270 271 static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl, 272 u32 server_highest_slotid, 273 u32 ivalue) 274 { 275 struct nfs4_slot **p; 276 277 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1); 278 p = &tbl->slots; 279 while (*p) { 280 (*p)->seq_nr = ivalue; 281 (*p)->seq_nr_highest_sent = ivalue; 282 (*p)->seq_nr_last_acked = ivalue - 1; 283 p = &(*p)->next; 284 } 285 tbl->highest_used_slotid = NFS4_NO_SLOT; 286 tbl->target_highest_slotid = server_highest_slotid; 287 tbl->server_highest_slotid = server_highest_slotid; 288 tbl->d_target_highest_slotid = 0; 289 tbl->d2_target_highest_slotid = 0; 290 tbl->max_slotid = server_highest_slotid; 291 } 292 293 /* 294 * (re)Initialise a slot table 295 */ 296 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, 297 u32 max_reqs, u32 ivalue) 298 { 299 int ret; 300 301 dprintk("--> %s: max_reqs=%u, tbl->max_slots %u\n", __func__, 302 max_reqs, tbl->max_slots); 303 304 if (max_reqs > NFS4_MAX_SLOT_TABLE) 305 max_reqs = NFS4_MAX_SLOT_TABLE; 306 307 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue); 308 if (ret) 309 goto out; 310 311 spin_lock(&tbl->slot_tbl_lock); 312 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue); 313 spin_unlock(&tbl->slot_tbl_lock); 314 315 dprintk("%s: tbl=%p slots=%p max_slots=%u\n", __func__, 316 tbl, tbl->slots, tbl->max_slots); 317 out: 318 dprintk("<-- %s: return %d\n", __func__, ret); 319 return ret; 320 } 321 322 /* 323 * nfs4_release_slot_table - release all slot table entries 324 */ 325 static void nfs4_release_slot_table(struct nfs4_slot_table *tbl) 326 { 327 nfs4_shrink_slot_table(tbl, 0); 328 } 329 330 /** 331 * nfs4_shutdown_slot_table - release resources attached to a slot table 332 * @tbl: slot table to shut down 333 * 334 */ 335 void nfs4_shutdown_slot_table(struct nfs4_slot_table *tbl) 336 { 337 nfs4_release_slot_table(tbl); 338 rpc_destroy_wait_queue(&tbl->slot_tbl_waitq); 339 } 340 341 /** 342 * nfs4_setup_slot_table - prepare a stand-alone slot table for use 343 * @tbl: slot table to set up 344 * @max_reqs: maximum number of requests allowed 345 * @queue: name to give RPC wait queue 346 * 347 * Returns zero on success, or a negative errno. 348 */ 349 int nfs4_setup_slot_table(struct nfs4_slot_table *tbl, unsigned int max_reqs, 350 const char *queue) 351 { 352 nfs4_init_slot_table(tbl, queue); 353 return nfs4_realloc_slot_table(tbl, max_reqs, 0); 354 } 355 356 static bool nfs41_assign_slot(struct rpc_task *task, void *pslot) 357 { 358 struct nfs4_sequence_args *args = task->tk_msg.rpc_argp; 359 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp; 360 struct nfs4_slot *slot = pslot; 361 struct nfs4_slot_table *tbl = slot->table; 362 363 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged) 364 return false; 365 slot->generation = tbl->generation; 366 args->sa_slot = slot; 367 res->sr_timestamp = jiffies; 368 res->sr_slot = slot; 369 res->sr_status_flags = 0; 370 res->sr_status = 1; 371 return true; 372 } 373 374 static bool __nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl, 375 struct nfs4_slot *slot) 376 { 377 if (rpc_wake_up_first(&tbl->slot_tbl_waitq, nfs41_assign_slot, slot)) 378 return true; 379 return false; 380 } 381 382 bool nfs41_wake_and_assign_slot(struct nfs4_slot_table *tbl, 383 struct nfs4_slot *slot) 384 { 385 if (slot->slot_nr > tbl->max_slotid) 386 return false; 387 return __nfs41_wake_and_assign_slot(tbl, slot); 388 } 389 390 static bool nfs41_try_wake_next_slot_table_entry(struct nfs4_slot_table *tbl) 391 { 392 struct nfs4_slot *slot = nfs4_alloc_slot(tbl); 393 if (!IS_ERR(slot)) { 394 bool ret = __nfs41_wake_and_assign_slot(tbl, slot); 395 if (ret) 396 return ret; 397 nfs4_free_slot(tbl, slot); 398 } 399 return false; 400 } 401 402 void nfs41_wake_slot_table(struct nfs4_slot_table *tbl) 403 { 404 for (;;) { 405 if (!nfs41_try_wake_next_slot_table_entry(tbl)) 406 break; 407 } 408 } 409 410 #if defined(CONFIG_NFS_V4_1) 411 412 static void nfs41_set_max_slotid_locked(struct nfs4_slot_table *tbl, 413 u32 target_highest_slotid) 414 { 415 u32 max_slotid; 416 417 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, target_highest_slotid); 418 if (max_slotid > tbl->server_highest_slotid) 419 max_slotid = tbl->server_highest_slotid; 420 if (max_slotid > tbl->target_highest_slotid) 421 max_slotid = tbl->target_highest_slotid; 422 tbl->max_slotid = max_slotid; 423 nfs41_wake_slot_table(tbl); 424 } 425 426 /* Update the client's idea of target_highest_slotid */ 427 static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl, 428 u32 target_highest_slotid) 429 { 430 if (tbl->target_highest_slotid == target_highest_slotid) 431 return; 432 tbl->target_highest_slotid = target_highest_slotid; 433 tbl->generation++; 434 } 435 436 void nfs41_set_target_slotid(struct nfs4_slot_table *tbl, 437 u32 target_highest_slotid) 438 { 439 spin_lock(&tbl->slot_tbl_lock); 440 nfs41_set_target_slotid_locked(tbl, target_highest_slotid); 441 tbl->d_target_highest_slotid = 0; 442 tbl->d2_target_highest_slotid = 0; 443 nfs41_set_max_slotid_locked(tbl, target_highest_slotid); 444 spin_unlock(&tbl->slot_tbl_lock); 445 } 446 447 static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl, 448 u32 highest_slotid) 449 { 450 if (tbl->server_highest_slotid == highest_slotid) 451 return; 452 if (tbl->highest_used_slotid > highest_slotid) 453 return; 454 /* Deallocate slots */ 455 nfs4_shrink_slot_table(tbl, highest_slotid + 1); 456 tbl->server_highest_slotid = highest_slotid; 457 } 458 459 static s32 nfs41_derivative_target_slotid(s32 s1, s32 s2) 460 { 461 s1 -= s2; 462 if (s1 == 0) 463 return 0; 464 if (s1 < 0) 465 return (s1 - 1) >> 1; 466 return (s1 + 1) >> 1; 467 } 468 469 static int nfs41_sign_s32(s32 s1) 470 { 471 if (s1 > 0) 472 return 1; 473 if (s1 < 0) 474 return -1; 475 return 0; 476 } 477 478 static bool nfs41_same_sign_or_zero_s32(s32 s1, s32 s2) 479 { 480 if (!s1 || !s2) 481 return true; 482 return nfs41_sign_s32(s1) == nfs41_sign_s32(s2); 483 } 484 485 /* Try to eliminate outliers by checking for sharp changes in the 486 * derivatives and second derivatives 487 */ 488 static bool nfs41_is_outlier_target_slotid(struct nfs4_slot_table *tbl, 489 u32 new_target) 490 { 491 s32 d_target, d2_target; 492 bool ret = true; 493 494 d_target = nfs41_derivative_target_slotid(new_target, 495 tbl->target_highest_slotid); 496 d2_target = nfs41_derivative_target_slotid(d_target, 497 tbl->d_target_highest_slotid); 498 /* Is first derivative same sign? */ 499 if (nfs41_same_sign_or_zero_s32(d_target, tbl->d_target_highest_slotid)) 500 ret = false; 501 /* Is second derivative same sign? */ 502 if (nfs41_same_sign_or_zero_s32(d2_target, tbl->d2_target_highest_slotid)) 503 ret = false; 504 tbl->d_target_highest_slotid = d_target; 505 tbl->d2_target_highest_slotid = d2_target; 506 return ret; 507 } 508 509 void nfs41_update_target_slotid(struct nfs4_slot_table *tbl, 510 struct nfs4_slot *slot, 511 struct nfs4_sequence_res *res) 512 { 513 spin_lock(&tbl->slot_tbl_lock); 514 if (!nfs41_is_outlier_target_slotid(tbl, res->sr_target_highest_slotid)) 515 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid); 516 if (tbl->generation == slot->generation) 517 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid); 518 nfs41_set_max_slotid_locked(tbl, res->sr_target_highest_slotid); 519 spin_unlock(&tbl->slot_tbl_lock); 520 } 521 522 static void nfs4_release_session_slot_tables(struct nfs4_session *session) 523 { 524 nfs4_release_slot_table(&session->fc_slot_table); 525 nfs4_release_slot_table(&session->bc_slot_table); 526 } 527 528 /* 529 * Initialize or reset the forechannel and backchannel tables 530 */ 531 int nfs4_setup_session_slot_tables(struct nfs4_session *ses) 532 { 533 struct nfs4_slot_table *tbl; 534 int status; 535 536 dprintk("--> %s\n", __func__); 537 /* Fore channel */ 538 tbl = &ses->fc_slot_table; 539 tbl->session = ses; 540 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1); 541 if (status || !(ses->flags & SESSION4_BACK_CHAN)) /* -ENOMEM */ 542 return status; 543 /* Back channel */ 544 tbl = &ses->bc_slot_table; 545 tbl->session = ses; 546 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0); 547 if (status && tbl->slots == NULL) 548 /* Fore and back channel share a connection so get 549 * both slot tables or neither */ 550 nfs4_release_session_slot_tables(ses); 551 return status; 552 } 553 554 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp) 555 { 556 struct nfs4_session *session; 557 558 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS); 559 if (!session) 560 return NULL; 561 562 nfs4_init_slot_table(&session->fc_slot_table, "ForeChannel Slot table"); 563 nfs4_init_slot_table(&session->bc_slot_table, "BackChannel Slot table"); 564 session->session_state = 1<<NFS4_SESSION_INITING; 565 566 session->clp = clp; 567 return session; 568 } 569 570 static void nfs4_destroy_session_slot_tables(struct nfs4_session *session) 571 { 572 nfs4_shutdown_slot_table(&session->fc_slot_table); 573 nfs4_shutdown_slot_table(&session->bc_slot_table); 574 } 575 576 void nfs4_destroy_session(struct nfs4_session *session) 577 { 578 struct rpc_xprt *xprt; 579 const struct cred *cred; 580 581 cred = nfs4_get_clid_cred(session->clp); 582 nfs4_proc_destroy_session(session, cred); 583 put_cred(cred); 584 585 rcu_read_lock(); 586 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt); 587 rcu_read_unlock(); 588 dprintk("%s Destroy backchannel for xprt %p\n", 589 __func__, xprt); 590 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS); 591 nfs4_destroy_session_slot_tables(session); 592 kfree(session); 593 } 594 595 /* 596 * With sessions, the client is not marked ready until after a 597 * successful EXCHANGE_ID and CREATE_SESSION. 598 * 599 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate 600 * other versions of NFS can be tried. 601 */ 602 static int nfs41_check_session_ready(struct nfs_client *clp) 603 { 604 int ret; 605 606 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) { 607 ret = nfs4_client_recover_expired_lease(clp); 608 if (ret) 609 return ret; 610 } 611 if (clp->cl_cons_state < NFS_CS_READY) 612 return -EPROTONOSUPPORT; 613 smp_rmb(); 614 return 0; 615 } 616 617 int nfs4_init_session(struct nfs_client *clp) 618 { 619 if (!nfs4_has_session(clp)) 620 return 0; 621 622 clear_bit(NFS4_SESSION_INITING, &clp->cl_session->session_state); 623 return nfs41_check_session_ready(clp); 624 } 625 626 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time) 627 { 628 struct nfs4_session *session = clp->cl_session; 629 int ret; 630 631 spin_lock(&clp->cl_lock); 632 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) { 633 /* 634 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the 635 * DS lease to be equal to the MDS lease. 636 */ 637 clp->cl_lease_time = lease_time; 638 clp->cl_last_renewal = jiffies; 639 } 640 spin_unlock(&clp->cl_lock); 641 642 ret = nfs41_check_session_ready(clp); 643 if (ret) 644 return ret; 645 /* Test for the DS role */ 646 if (!is_ds_client(clp)) 647 return -ENODEV; 648 return 0; 649 } 650 EXPORT_SYMBOL_GPL(nfs4_init_ds_session); 651 652 #endif /* defined(CONFIG_NFS_V4_1) */ 653