1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2017-2018 Christoph Hellwig. 4 */ 5 6 #include <linux/backing-dev.h> 7 #include <linux/moduleparam.h> 8 #include <linux/vmalloc.h> 9 #include <trace/events/block.h> 10 #include "nvme.h" 11 12 bool multipath = true; 13 static bool multipath_always_on; 14 15 static int multipath_param_set(const char *val, const struct kernel_param *kp) 16 { 17 int ret; 18 bool *arg = kp->arg; 19 20 ret = param_set_bool(val, kp); 21 if (ret) 22 return ret; 23 24 if (multipath_always_on && !*arg) { 25 pr_err("Can't disable multipath when multipath_always_on is configured.\n"); 26 *arg = true; 27 return -EINVAL; 28 } 29 30 return 0; 31 } 32 33 static const struct kernel_param_ops multipath_param_ops = { 34 .set = multipath_param_set, 35 .get = param_get_bool, 36 }; 37 38 module_param_cb(multipath, &multipath_param_ops, &multipath, 0444); 39 MODULE_PARM_DESC(multipath, 40 "turn on native support for multiple controllers per subsystem"); 41 42 static int multipath_always_on_set(const char *val, 43 const struct kernel_param *kp) 44 { 45 int ret; 46 bool *arg = kp->arg; 47 48 ret = param_set_bool(val, kp); 49 if (ret < 0) 50 return ret; 51 52 if (*arg) 53 multipath = true; 54 55 return 0; 56 } 57 58 static const struct kernel_param_ops multipath_always_on_ops = { 59 .set = multipath_always_on_set, 60 .get = param_get_bool, 61 }; 62 63 module_param_cb(multipath_always_on, &multipath_always_on_ops, 64 &multipath_always_on, 0444); 65 MODULE_PARM_DESC(multipath_always_on, 66 "create multipath node always except for private namespace with non-unique nsid; note that this also implicitly enables native multipath support"); 67 68 static const char *nvme_iopolicy_names[] = { 69 [NVME_IOPOLICY_NUMA] = "numa", 70 [NVME_IOPOLICY_RR] = "round-robin", 71 [NVME_IOPOLICY_QD] = "queue-depth", 72 }; 73 74 static int iopolicy = NVME_IOPOLICY_NUMA; 75 76 static int nvme_iopolicy_parse(const char *str) 77 { 78 int i; 79 80 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) { 81 if (sysfs_streq(str, nvme_iopolicy_names[i])) 82 return i; 83 } 84 return -EINVAL; 85 } 86 87 static int nvme_set_iopolicy(const char *val, const struct kernel_param *kp) 88 { 89 int policy; 90 91 if (!val) 92 return -EINVAL; 93 94 policy = nvme_iopolicy_parse(val); 95 if (policy < 0) 96 return policy; 97 98 iopolicy = policy; 99 return 0; 100 } 101 102 static int nvme_get_iopolicy(char *buf, const struct kernel_param *kp) 103 { 104 return sprintf(buf, "%s\n", nvme_iopolicy_names[iopolicy]); 105 } 106 107 module_param_call(iopolicy, nvme_set_iopolicy, nvme_get_iopolicy, 108 &iopolicy, 0644); 109 MODULE_PARM_DESC(iopolicy, 110 "Default multipath I/O policy; 'numa' (default), 'round-robin' or 'queue-depth'"); 111 112 void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys) 113 { 114 subsys->iopolicy = iopolicy; 115 } 116 117 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys) 118 { 119 struct nvme_ns_head *h; 120 121 lockdep_assert_held(&subsys->lock); 122 list_for_each_entry(h, &subsys->nsheads, entry) 123 if (h->disk) 124 blk_mq_unfreeze_queue_nomemrestore(h->disk->queue); 125 } 126 127 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys) 128 { 129 struct nvme_ns_head *h; 130 131 lockdep_assert_held(&subsys->lock); 132 list_for_each_entry(h, &subsys->nsheads, entry) 133 if (h->disk) 134 blk_mq_freeze_queue_wait(h->disk->queue); 135 } 136 137 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys) 138 { 139 struct nvme_ns_head *h; 140 141 lockdep_assert_held(&subsys->lock); 142 list_for_each_entry(h, &subsys->nsheads, entry) 143 if (h->disk) 144 blk_freeze_queue_start(h->disk->queue); 145 } 146 147 void nvme_failover_req(struct request *req) 148 { 149 struct nvme_ns *ns = req->q->queuedata; 150 u16 status = nvme_req(req)->status & NVME_SCT_SC_MASK; 151 unsigned long flags; 152 struct bio *bio; 153 154 nvme_mpath_clear_current_path(ns); 155 atomic_long_inc(&ns->failover); 156 157 /* 158 * If we got back an ANA error, we know the controller is alive but not 159 * ready to serve this namespace. Kick of a re-read of the ANA 160 * information page, and just try any other available path for now. 161 */ 162 if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) { 163 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 164 queue_work(nvme_wq, &ns->ctrl->ana_work); 165 } 166 167 spin_lock_irqsave(&ns->head->requeue_lock, flags); 168 for (bio = req->bio; bio; bio = bio->bi_next) 169 bio_set_dev(bio, ns->head->disk->part0); 170 blk_steal_bios(&ns->head->requeue_list, req); 171 spin_unlock_irqrestore(&ns->head->requeue_lock, flags); 172 173 nvme_req(req)->status = 0; 174 nvme_end_req(req); 175 kblockd_schedule_work(&ns->head->requeue_work); 176 } 177 178 void nvme_mpath_start_request(struct request *rq) 179 { 180 struct nvme_ns *ns = rq->q->queuedata; 181 struct gendisk *disk = ns->head->disk; 182 183 if ((READ_ONCE(ns->head->subsys->iopolicy) == NVME_IOPOLICY_QD) && 184 !(nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)) { 185 atomic_inc(&ns->ctrl->nr_active); 186 nvme_req(rq)->flags |= NVME_MPATH_CNT_ACTIVE; 187 } 188 189 if (!blk_queue_io_stat(disk->queue) || 190 (nvme_req(rq)->flags & NVME_MPATH_IO_STATS)) 191 return; 192 if (blk_rq_is_passthrough(rq) && 193 !blk_rq_passthrough_stats(rq, disk->queue)) 194 return; 195 196 nvme_req(rq)->flags |= NVME_MPATH_IO_STATS; 197 nvme_req(rq)->start_time = bdev_start_io_acct(disk->part0, req_op(rq), 198 jiffies); 199 } 200 EXPORT_SYMBOL_GPL(nvme_mpath_start_request); 201 202 void nvme_mpath_end_request(struct request *rq) 203 { 204 struct nvme_ns *ns = rq->q->queuedata; 205 206 if (nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE) 207 atomic_dec_if_positive(&ns->ctrl->nr_active); 208 209 if (!(nvme_req(rq)->flags & NVME_MPATH_IO_STATS)) 210 return; 211 bdev_end_io_acct(ns->head->disk->part0, req_op(rq), 212 blk_rq_bytes(rq) >> SECTOR_SHIFT, 213 nvme_req(rq)->start_time); 214 } 215 216 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl) 217 { 218 struct nvme_ns *ns; 219 int srcu_idx; 220 221 srcu_idx = srcu_read_lock(&ctrl->srcu); 222 list_for_each_entry_srcu(ns, &ctrl->namespaces, list, 223 srcu_read_lock_held(&ctrl->srcu)) { 224 if (!ns->head->disk) 225 continue; 226 kblockd_schedule_work(&ns->head->requeue_work); 227 if (nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE) 228 disk_uevent(ns->head->disk, KOBJ_CHANGE); 229 } 230 srcu_read_unlock(&ctrl->srcu, srcu_idx); 231 } 232 233 static const char *nvme_ana_state_names[] = { 234 [0] = "invalid state", 235 [NVME_ANA_OPTIMIZED] = "optimized", 236 [NVME_ANA_NONOPTIMIZED] = "non-optimized", 237 [NVME_ANA_INACCESSIBLE] = "inaccessible", 238 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss", 239 [NVME_ANA_CHANGE] = "change", 240 }; 241 242 bool nvme_mpath_clear_current_path(struct nvme_ns *ns) 243 { 244 struct nvme_ns_head *head = ns->head; 245 bool changed = false; 246 int node; 247 248 for_each_node(node) { 249 if (ns == rcu_access_pointer(head->current_path[node])) { 250 rcu_assign_pointer(head->current_path[node], NULL); 251 changed = true; 252 } 253 } 254 return changed; 255 } 256 257 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl) 258 { 259 struct nvme_ns *ns; 260 int srcu_idx; 261 262 srcu_idx = srcu_read_lock(&ctrl->srcu); 263 list_for_each_entry_srcu(ns, &ctrl->namespaces, list, 264 srcu_read_lock_held(&ctrl->srcu)) { 265 nvme_mpath_clear_current_path(ns); 266 kblockd_schedule_work(&ns->head->requeue_work); 267 } 268 srcu_read_unlock(&ctrl->srcu, srcu_idx); 269 } 270 271 void nvme_mpath_revalidate_paths(struct nvme_ns_head *head) 272 { 273 sector_t capacity = get_capacity(head->disk); 274 struct nvme_ns *ns; 275 int node; 276 int srcu_idx; 277 278 srcu_idx = srcu_read_lock(&head->srcu); 279 list_for_each_entry_srcu(ns, &head->list, siblings, 280 srcu_read_lock_held(&head->srcu)) { 281 if (capacity != get_capacity(ns->disk)) 282 clear_bit(NVME_NS_READY, &ns->flags); 283 } 284 srcu_read_unlock(&head->srcu, srcu_idx); 285 286 for_each_node(node) 287 rcu_assign_pointer(head->current_path[node], NULL); 288 kblockd_schedule_work(&head->requeue_work); 289 } 290 291 #ifdef CONFIG_BLK_DEV_ZONED 292 int nvme_mpath_revalidate_zones(struct nvme_ns_head *head) 293 { 294 struct gendisk *disk = head->disk; 295 int ret; 296 297 if (!disk || !blk_queue_is_zoned(disk->queue) || 298 !test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) 299 return 0; 300 301 ret = blk_revalidate_disk_zones(disk); 302 if (ret) 303 dev_warn_ratelimited(disk_to_dev(disk), 304 "failed to revalidate zoned namespace head: %d\n", 305 ret); 306 return ret; 307 } 308 #endif /* CONFIG_BLK_DEV_ZONED */ 309 310 enum nvme_path_state { 311 NVME_PATH_ENABLED, 312 NVME_PATH_DISABLED_CTRL_DOWN, 313 NVME_PATH_DISABLED_ANA_PENDING, 314 NVME_PATH_DISABLED_NS_NOT_READY, 315 }; 316 317 static enum nvme_path_state nvme_path_get_state(struct nvme_ns *ns) 318 { 319 enum nvme_ctrl_state state = nvme_ctrl_state(ns->ctrl); 320 321 /* 322 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should 323 * still be able to complete assuming that the controller is connected. 324 * Otherwise it will fail immediately and return to the requeue list. 325 */ 326 if (state != NVME_CTRL_LIVE && state != NVME_CTRL_DELETING) 327 return NVME_PATH_DISABLED_CTRL_DOWN; 328 if (test_bit(NVME_NS_ANA_PENDING, &ns->flags)) 329 return NVME_PATH_DISABLED_ANA_PENDING; 330 if (!test_bit(NVME_NS_READY, &ns->flags)) 331 return NVME_PATH_DISABLED_NS_NOT_READY; 332 return NVME_PATH_ENABLED; 333 } 334 335 static bool nvme_path_is_disabled(struct nvme_ns *ns) 336 { 337 return nvme_path_get_state(ns) != NVME_PATH_ENABLED; 338 } 339 340 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node) 341 __must_hold_shared(&head->srcu) 342 { 343 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance; 344 struct nvme_ns *found = NULL, *fallback = NULL, *ns; 345 346 list_for_each_entry_srcu(ns, &head->list, siblings, 347 srcu_read_lock_held(&head->srcu)) { 348 if (nvme_path_is_disabled(ns)) 349 continue; 350 351 if (ns->ctrl->numa_node != NUMA_NO_NODE && 352 READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA) 353 distance = node_distance(node, ns->ctrl->numa_node); 354 else 355 distance = LOCAL_DISTANCE; 356 357 switch (ns->ana_state) { 358 case NVME_ANA_OPTIMIZED: 359 if (distance < found_distance) { 360 found_distance = distance; 361 found = ns; 362 } 363 break; 364 case NVME_ANA_NONOPTIMIZED: 365 if (distance < fallback_distance) { 366 fallback_distance = distance; 367 fallback = ns; 368 } 369 break; 370 default: 371 break; 372 } 373 } 374 375 if (!found) 376 found = fallback; 377 if (found) 378 rcu_assign_pointer(head->current_path[node], found); 379 return found; 380 } 381 382 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head, 383 struct nvme_ns *ns) 384 __must_hold_shared(&head->srcu) 385 { 386 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns, 387 siblings); 388 if (ns) 389 return ns; 390 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings); 391 } 392 393 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head) 394 __must_hold_shared(&head->srcu) 395 { 396 struct nvme_ns *ns, *found = NULL; 397 int node = numa_node_id(); 398 struct nvme_ns *old = srcu_dereference(head->current_path[node], 399 &head->srcu); 400 401 if (unlikely(!old)) 402 return __nvme_find_path(head, node); 403 404 if (list_is_singular(&head->list)) { 405 if (nvme_path_is_disabled(old)) 406 return NULL; 407 return old; 408 } 409 410 for (ns = nvme_next_ns(head, old); 411 ns && ns != old; 412 ns = nvme_next_ns(head, ns)) { 413 if (nvme_path_is_disabled(ns)) 414 continue; 415 416 if (ns->ana_state == NVME_ANA_OPTIMIZED) { 417 found = ns; 418 goto out; 419 } 420 if (ns->ana_state == NVME_ANA_NONOPTIMIZED) 421 found = ns; 422 } 423 424 /* 425 * The loop above skips the current path for round-robin semantics. 426 * Fall back to the current path if either: 427 * - no other optimized path found and current is optimized, 428 * - no other usable path found and current is usable. 429 */ 430 if (!nvme_path_is_disabled(old) && 431 (old->ana_state == NVME_ANA_OPTIMIZED || 432 (!found && old->ana_state == NVME_ANA_NONOPTIMIZED))) 433 return old; 434 435 if (!found) 436 return NULL; 437 out: 438 rcu_assign_pointer(head->current_path[node], found); 439 return found; 440 } 441 442 static struct nvme_ns *nvme_queue_depth_path(struct nvme_ns_head *head) 443 __must_hold_shared(&head->srcu) 444 { 445 struct nvme_ns *best_opt = NULL, *best_nonopt = NULL, *ns; 446 unsigned int min_depth_opt = UINT_MAX, min_depth_nonopt = UINT_MAX; 447 unsigned int depth; 448 449 list_for_each_entry_srcu(ns, &head->list, siblings, 450 srcu_read_lock_held(&head->srcu)) { 451 if (nvme_path_is_disabled(ns)) 452 continue; 453 454 depth = atomic_read(&ns->ctrl->nr_active); 455 456 switch (ns->ana_state) { 457 case NVME_ANA_OPTIMIZED: 458 if (depth < min_depth_opt) { 459 min_depth_opt = depth; 460 best_opt = ns; 461 } 462 break; 463 case NVME_ANA_NONOPTIMIZED: 464 if (depth < min_depth_nonopt) { 465 min_depth_nonopt = depth; 466 best_nonopt = ns; 467 } 468 break; 469 default: 470 break; 471 } 472 473 if (min_depth_opt == 0) 474 return best_opt; 475 } 476 477 return best_opt ? best_opt : best_nonopt; 478 } 479 480 static inline bool nvme_path_is_optimized(struct nvme_ns *ns) 481 { 482 return nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE && 483 ns->ana_state == NVME_ANA_OPTIMIZED; 484 } 485 486 static struct nvme_ns *nvme_numa_path(struct nvme_ns_head *head) 487 __must_hold_shared(&head->srcu) 488 { 489 int node = numa_node_id(); 490 struct nvme_ns *ns; 491 492 ns = srcu_dereference(head->current_path[node], &head->srcu); 493 if (unlikely(!ns)) 494 return __nvme_find_path(head, node); 495 if (unlikely(!nvme_path_is_optimized(ns))) 496 return __nvme_find_path(head, node); 497 return ns; 498 } 499 500 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head) 501 { 502 switch (READ_ONCE(head->subsys->iopolicy)) { 503 case NVME_IOPOLICY_QD: 504 return nvme_queue_depth_path(head); 505 case NVME_IOPOLICY_RR: 506 return nvme_round_robin_path(head); 507 default: 508 return nvme_numa_path(head); 509 } 510 } 511 512 static inline bool nvme_state_is_live(enum nvme_ana_state state) 513 { 514 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED; 515 } 516 517 static bool nvme_available_path(struct nvme_ns_head *head) 518 __must_hold_shared(&head->srcu) 519 { 520 bool fail_on_ctrl_loss = test_bit(NVME_NSHEAD_FAIL_ON_CTRL_LOSS, 521 &head->flags); 522 bool failfast = false; 523 struct nvme_ns *ns; 524 525 if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) 526 return false; 527 528 list_for_each_entry_srcu(ns, &head->list, siblings, 529 srcu_read_lock_held(&head->srcu)) { 530 if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags)) { 531 failfast = true; 532 continue; 533 } 534 switch (nvme_ctrl_state(ns->ctrl)) { 535 case NVME_CTRL_LIVE: 536 if (!nvme_state_is_live(ns->ana_state) && 537 ns->ana_state != NVME_ANA_CHANGE) 538 continue; 539 return true; 540 case NVME_CTRL_RESETTING: 541 return true; 542 case NVME_CTRL_CONNECTING: 543 if (!fail_on_ctrl_loss) 544 return true; 545 continue; 546 default: 547 break; 548 } 549 } 550 551 if (failfast || fail_on_ctrl_loss) 552 return false; 553 554 /* 555 * If "head->delayed_removal_secs" is configured (i.e., non-zero), do 556 * not immediately fail I/O. Instead, requeue the I/O for the configured 557 * duration, anticipating that if there's a transient link failure then 558 * it may recover within this time window. This parameter is exported to 559 * userspace via sysfs, and its default value is zero. It is internally 560 * mapped to NVME_NSHEAD_QUEUE_IF_NO_PATH. When delayed_removal_secs is 561 * non-zero, this flag is set to true. When zero, the flag is cleared. 562 */ 563 return nvme_mpath_queue_if_no_path(head); 564 } 565 566 static void nvme_ns_head_submit_bio(struct bio *bio) 567 { 568 struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data; 569 struct device *dev = disk_to_dev(head->disk); 570 struct nvme_ns *ns; 571 int srcu_idx; 572 573 /* 574 * The namespace might be going away and the bio might be moved to a 575 * different queue via blk_steal_bios(), so we need to use the bio_split 576 * pool from the original queue to allocate the bvecs from. 577 */ 578 bio = bio_split_to_limits(bio); 579 if (!bio) 580 return; 581 582 srcu_idx = srcu_read_lock(&head->srcu); 583 ns = nvme_find_path(head); 584 if (likely(ns)) { 585 bio_set_dev(bio, ns->disk->part0); 586 /* 587 * Use BIO_REMAPPED to skip bio_check_eod() when this bio 588 * enters submit_bio_noacct() for the per-path device. The EOD 589 * check already passed on the multipath head. 590 */ 591 bio_set_flag(bio, BIO_REMAPPED); 592 bio->bi_opf |= REQ_NVME_MPATH; 593 trace_block_bio_remap(bio, disk_devt(ns->head->disk), 594 bio->bi_iter.bi_sector); 595 submit_bio_noacct(bio); 596 } else if (nvme_available_path(head)) { 597 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n"); 598 599 spin_lock_irq(&head->requeue_lock); 600 bio_list_add(&head->requeue_list, bio); 601 spin_unlock_irq(&head->requeue_lock); 602 atomic_long_inc(&head->io_requeue_no_usable_path_count); 603 } else { 604 dev_warn_ratelimited(dev, "no available path - failing I/O\n"); 605 606 bio_io_error(bio); 607 atomic_long_inc(&head->io_fail_no_available_path_count); 608 } 609 610 srcu_read_unlock(&head->srcu, srcu_idx); 611 } 612 613 static int nvme_ns_head_open(struct gendisk *disk, blk_mode_t mode) 614 { 615 if (!nvme_tryget_ns_head(disk->private_data)) 616 return -ENXIO; 617 return 0; 618 } 619 620 static void nvme_ns_head_release(struct gendisk *disk) 621 { 622 nvme_put_ns_head(disk->private_data); 623 } 624 625 static int nvme_ns_head_get_unique_id(struct gendisk *disk, u8 id[16], 626 enum blk_unique_id type) 627 { 628 struct nvme_ns_head *head = disk->private_data; 629 struct nvme_ns *ns; 630 int srcu_idx, ret = -EWOULDBLOCK; 631 632 srcu_idx = srcu_read_lock(&head->srcu); 633 ns = nvme_find_path(head); 634 if (ns) 635 ret = nvme_ns_get_unique_id(ns, id, type); 636 srcu_read_unlock(&head->srcu, srcu_idx); 637 return ret; 638 } 639 640 #ifdef CONFIG_BLK_DEV_ZONED 641 static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector, 642 unsigned int nr_zones, struct blk_report_zones_args *args) 643 { 644 struct nvme_ns_head *head = disk->private_data; 645 struct nvme_ns *ns; 646 int srcu_idx, ret = -EWOULDBLOCK; 647 648 srcu_idx = srcu_read_lock(&head->srcu); 649 ns = nvme_find_path(head); 650 if (ns) 651 ret = nvme_ns_report_zones(ns, sector, nr_zones, args); 652 srcu_read_unlock(&head->srcu, srcu_idx); 653 return ret; 654 } 655 #else 656 #define nvme_ns_head_report_zones NULL 657 #endif /* CONFIG_BLK_DEV_ZONED */ 658 659 const struct block_device_operations nvme_ns_head_ops = { 660 .owner = THIS_MODULE, 661 .submit_bio = nvme_ns_head_submit_bio, 662 .open = nvme_ns_head_open, 663 .release = nvme_ns_head_release, 664 .ioctl = nvme_ns_head_ioctl, 665 .compat_ioctl = blkdev_compat_ptr_ioctl, 666 .getgeo = nvme_getgeo, 667 .get_unique_id = nvme_ns_head_get_unique_id, 668 .report_zones = nvme_ns_head_report_zones, 669 .pr_ops = &nvme_pr_ops, 670 }; 671 672 static const struct file_operations nvme_ns_head_chr_fops = { 673 .owner = THIS_MODULE, 674 .unlocked_ioctl = nvme_ns_head_chr_ioctl, 675 .compat_ioctl = compat_ptr_ioctl, 676 .uring_cmd = nvme_ns_head_chr_uring_cmd, 677 .uring_cmd_iopoll = nvme_ns_chr_uring_cmd_iopoll, 678 }; 679 680 static void nvme_add_ns_head_cdev(struct nvme_ns_head *head) 681 { 682 head->cdev_device.parent = &head->subsys->dev; 683 684 nvme_get_ns_head(head); /* Undone in nvme_cdev_rel() */ 685 if (nvme_cdev_add(&head->cdev, &head->cdev_device, 686 &nvme_ns_head_chr_fops, THIS_MODULE, 687 head->subsys->instance, head->instance)) { 688 dev_err(disk_to_dev(head->disk), 689 "Unable to create the ng%dn%d device\n", 690 head->subsys->instance, head->instance); 691 nvme_put_ns_head(head); 692 return; 693 } 694 set_bit(NVME_NSHEAD_CDEV_LIVE, &head->flags); 695 } 696 697 static void nvme_partition_scan_work(struct work_struct *work) 698 { 699 struct nvme_ns_head *head = 700 container_of(work, struct nvme_ns_head, partition_scan_work); 701 702 if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN, 703 &head->disk->state))) 704 return; 705 706 mutex_lock(&head->disk->open_mutex); 707 bdev_disk_changed(head->disk, false); 708 mutex_unlock(&head->disk->open_mutex); 709 } 710 711 static void nvme_requeue_work(struct work_struct *work) 712 { 713 struct nvme_ns_head *head = 714 container_of(work, struct nvme_ns_head, requeue_work); 715 struct bio *bio, *next; 716 717 spin_lock_irq(&head->requeue_lock); 718 next = bio_list_get(&head->requeue_list); 719 spin_unlock_irq(&head->requeue_lock); 720 721 while ((bio = next) != NULL) { 722 next = bio->bi_next; 723 bio->bi_next = NULL; 724 725 submit_bio_noacct(bio); 726 } 727 } 728 729 static void nvme_remove_head(struct nvme_ns_head *head) 730 { 731 if (test_and_clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 732 /* 733 * Requeue I/O after NVME_NSHEAD_DISK_LIVE has been cleared 734 * to allow multipath to fail all I/O. First synchronize to 735 * add any bios to the requeue list. 736 */ 737 synchronize_srcu(&head->srcu); 738 kblockd_schedule_work(&head->requeue_work); 739 740 if (test_and_clear_bit(NVME_NSHEAD_CDEV_LIVE, &head->flags)) 741 nvme_cdev_del(&head->cdev, &head->cdev_device); 742 del_gendisk(head->disk); 743 } 744 nvme_put_ns_head(head); 745 } 746 747 static void nvme_remove_head_work(struct work_struct *work) 748 { 749 struct nvme_ns_head *head = container_of(to_delayed_work(work), 750 struct nvme_ns_head, remove_work); 751 bool remove = false; 752 753 mutex_lock(&head->subsys->lock); 754 if (list_empty(&head->list)) { 755 list_del_init(&head->entry); 756 remove = true; 757 } 758 mutex_unlock(&head->subsys->lock); 759 if (remove) 760 nvme_remove_head(head); 761 762 module_put(THIS_MODULE); 763 } 764 765 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head) 766 { 767 struct queue_limits lim; 768 769 mutex_init(&head->lock); 770 spin_lock_init(&head->requeue_lock); 771 INIT_WORK(&head->requeue_work, nvme_requeue_work); 772 INIT_WORK(&head->partition_scan_work, nvme_partition_scan_work); 773 INIT_DELAYED_WORK(&head->remove_work, nvme_remove_head_work); 774 775 /* 776 * If "multipath_always_on" is enabled, a multipath node is added 777 * regardless of whether the disk is single/multi ported, and whether 778 * the namespace is shared or private. If "multipath_always_on" is not 779 * enabled, a multipath node is added only if the subsystem supports 780 * multiple controllers and the "multipath" option is configured. In 781 * either case, for private namespaces, we ensure that the NSID is 782 * unique. 783 */ 784 if (!multipath_always_on) { 785 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || 786 !multipath) 787 return 0; 788 } 789 790 if (!nvme_is_unique_nsid(ctrl, head)) 791 return 0; 792 793 blk_set_stacking_limits(&lim); 794 lim.dma_alignment = 3; 795 lim.features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT | 796 BLK_FEAT_POLL | BLK_FEAT_ATOMIC_WRITES | BLK_FEAT_PCI_P2PDMA; 797 798 head->disk = blk_alloc_disk(&lim, ctrl->numa_node); 799 if (IS_ERR(head->disk)) 800 return PTR_ERR(head->disk); 801 head->disk->fops = &nvme_ns_head_ops; 802 head->disk->private_data = head; 803 804 /* 805 * We need to suppress the partition scan from occuring within the 806 * controller's scan_work context. If a path error occurs here, the IO 807 * will wait until a path becomes available or all paths are torn down, 808 * but that action also occurs within scan_work, so it would deadlock. 809 * Defer the partition scan to a different context that does not block 810 * scan_work. 811 */ 812 set_bit(GD_SUPPRESS_PART_SCAN, &head->disk->state); 813 sprintf(head->disk->disk_name, "nvme%dn%d", 814 ctrl->subsys->instance, head->instance); 815 nvme_get_ns_head(head); 816 return 0; 817 } 818 819 static void nvme_mpath_set_live(struct nvme_ns *ns) 820 { 821 struct nvme_ns_head *head = ns->head; 822 int rc; 823 824 if (!head->disk) 825 return; 826 827 /* 828 * test_and_set_bit() is used because it is protecting against two nvme 829 * paths simultaneously calling device_add_disk() on the same namespace 830 * head. 831 */ 832 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 833 rc = device_add_disk(&head->subsys->dev, head->disk, 834 nvme_ns_attr_groups); 835 if (rc) { 836 clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags); 837 return; 838 } 839 nvme_add_ns_head_cdev(head); 840 queue_work(nvme_wq, &head->partition_scan_work); 841 } 842 843 nvme_mpath_add_sysfs_link(ns->head); 844 845 mutex_lock(&head->lock); 846 if (nvme_path_is_optimized(ns)) { 847 int node, srcu_idx; 848 849 srcu_idx = srcu_read_lock(&head->srcu); 850 for_each_online_node(node) 851 __nvme_find_path(head, node); 852 srcu_read_unlock(&head->srcu, srcu_idx); 853 } 854 mutex_unlock(&head->lock); 855 856 synchronize_srcu(&head->srcu); 857 nvme_mpath_revalidate_zones(head); 858 kblockd_schedule_work(&head->requeue_work); 859 } 860 861 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data, 862 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *, 863 void *)) 864 __must_hold(&ctrl->ana_lock) 865 { 866 void *base = ctrl->ana_log_buf; 867 size_t offset = sizeof(struct nvme_ana_rsp_hdr); 868 int error, i; 869 870 lockdep_assert_held(&ctrl->ana_lock); 871 872 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) { 873 struct nvme_ana_group_desc *desc = base + offset; 874 u32 nr_nsids; 875 size_t nsid_buf_size; 876 877 if (WARN_ON_ONCE(offset > ctrl->ana_log_size || 878 sizeof(*desc) > ctrl->ana_log_size - offset)) 879 return -EINVAL; 880 881 nr_nsids = le32_to_cpu(desc->nnsids); 882 nsid_buf_size = flex_array_size(desc, nsids, nr_nsids); 883 884 if (WARN_ON_ONCE(desc->grpid == 0)) 885 return -EINVAL; 886 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax)) 887 return -EINVAL; 888 if (WARN_ON_ONCE(desc->state == 0)) 889 return -EINVAL; 890 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE)) 891 return -EINVAL; 892 893 offset += sizeof(*desc); 894 if (WARN_ON_ONCE(nsid_buf_size > ctrl->ana_log_size - offset)) 895 return -EINVAL; 896 897 error = cb(ctrl, desc, data); 898 if (error) 899 return error; 900 901 offset += nsid_buf_size; 902 } 903 904 return 0; 905 } 906 907 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc, 908 struct nvme_ns *ns) 909 { 910 ns->ana_grpid = le32_to_cpu(desc->grpid); 911 ns->ana_state = desc->state; 912 clear_bit(NVME_NS_ANA_PENDING, &ns->flags); 913 /* 914 * nvme_mpath_set_live() will trigger I/O to the multipath path device 915 * and in turn to this path device. However we cannot accept this I/O 916 * if the controller is not live. This may deadlock if called from 917 * nvme_mpath_init_identify() and the ctrl will never complete 918 * initialization, preventing I/O from completing. For this case we 919 * will reprocess the ANA log page in nvme_mpath_update() once the 920 * controller is ready. 921 */ 922 if (nvme_state_is_live(ns->ana_state) && 923 nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE) 924 nvme_mpath_set_live(ns); 925 else { 926 /* 927 * Add sysfs link from multipath head gendisk node to path 928 * device gendisk node. 929 * If path's ana state is live (i.e. state is either optimized 930 * or non-optimized) while we alloc the ns then sysfs link would 931 * be created from nvme_mpath_set_live(). In that case we would 932 * not fallthrough this code path. However for the path's ana 933 * state other than live, we call nvme_mpath_set_live() only 934 * after ana state transitioned to the live state. But we still 935 * want to create the sysfs link from head node to a path device 936 * irrespctive of the path's ana state. 937 * If we reach through here then it means that path's ana state 938 * is not live but still create the sysfs link to this path from 939 * head node if head node of the path has already come alive. 940 */ 941 if (test_bit(NVME_NSHEAD_DISK_LIVE, &ns->head->flags)) { 942 nvme_mpath_add_sysfs_link(ns->head); 943 kblockd_schedule_work(&ns->head->requeue_work); 944 } 945 } 946 } 947 948 static int nvme_update_ana_state(struct nvme_ctrl *ctrl, 949 struct nvme_ana_group_desc *desc, void *data) 950 { 951 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0; 952 unsigned *nr_change_groups = data; 953 struct nvme_ns *ns; 954 int srcu_idx; 955 956 dev_dbg(ctrl->device, "ANA group %d: %s.\n", 957 le32_to_cpu(desc->grpid), 958 nvme_ana_state_names[desc->state]); 959 960 if (desc->state == NVME_ANA_CHANGE) 961 (*nr_change_groups)++; 962 963 if (!nr_nsids) 964 return 0; 965 966 srcu_idx = srcu_read_lock(&ctrl->srcu); 967 list_for_each_entry_srcu(ns, &ctrl->namespaces, list, 968 srcu_read_lock_held(&ctrl->srcu)) { 969 unsigned nsid; 970 again: 971 nsid = le32_to_cpu(desc->nsids[n]); 972 if (ns->head->ns_id < nsid) 973 continue; 974 if (ns->head->ns_id == nsid) 975 nvme_update_ns_ana_state(desc, ns); 976 if (++n == nr_nsids) 977 break; 978 if (ns->head->ns_id > nsid) 979 goto again; 980 } 981 srcu_read_unlock(&ctrl->srcu, srcu_idx); 982 return 0; 983 } 984 985 static int nvme_read_ana_log(struct nvme_ctrl *ctrl) 986 { 987 u32 nr_change_groups = 0; 988 int error; 989 990 mutex_lock(&ctrl->ana_lock); 991 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM, 992 ctrl->ana_log_buf, ctrl->ana_log_size, 0); 993 if (error) { 994 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error); 995 goto out_unlock; 996 } 997 998 error = nvme_parse_ana_log(ctrl, &nr_change_groups, 999 nvme_update_ana_state); 1000 if (error) 1001 goto out_unlock; 1002 1003 /* 1004 * In theory we should have an ANATT timer per group as they might enter 1005 * the change state at different times. But that is a lot of overhead 1006 * just to protect against a target that keeps entering new changes 1007 * states while never finishing previous ones. But we'll still 1008 * eventually time out once all groups are in change state, so this 1009 * isn't a big deal. 1010 * 1011 * We also double the ANATT value to provide some slack for transports 1012 * or AEN processing overhead. 1013 */ 1014 if (nr_change_groups) 1015 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies); 1016 else 1017 timer_delete_sync(&ctrl->anatt_timer); 1018 out_unlock: 1019 mutex_unlock(&ctrl->ana_lock); 1020 return error; 1021 } 1022 1023 static void nvme_ana_work(struct work_struct *work) 1024 { 1025 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work); 1026 1027 if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) 1028 return; 1029 1030 nvme_read_ana_log(ctrl); 1031 } 1032 1033 void nvme_mpath_update(struct nvme_ctrl *ctrl) 1034 { 1035 u32 nr_change_groups = 0; 1036 1037 if (!ctrl->ana_log_buf) 1038 return; 1039 1040 mutex_lock(&ctrl->ana_lock); 1041 nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state); 1042 mutex_unlock(&ctrl->ana_lock); 1043 } 1044 1045 static void nvme_anatt_timeout(struct timer_list *t) 1046 { 1047 struct nvme_ctrl *ctrl = timer_container_of(ctrl, t, anatt_timer); 1048 1049 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n"); 1050 nvme_reset_ctrl(ctrl); 1051 } 1052 1053 void nvme_mpath_stop(struct nvme_ctrl *ctrl) 1054 { 1055 if (!nvme_ctrl_use_ana(ctrl)) 1056 return; 1057 timer_delete_sync(&ctrl->anatt_timer); 1058 cancel_work_sync(&ctrl->ana_work); 1059 } 1060 1061 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \ 1062 struct device_attribute subsys_attr_##_name = \ 1063 __ATTR(_name, _mode, _show, _store) 1064 1065 static ssize_t nvme_subsys_iopolicy_show(struct device *dev, 1066 struct device_attribute *attr, char *buf) 1067 { 1068 struct nvme_subsystem *subsys = 1069 container_of(dev, struct nvme_subsystem, dev); 1070 1071 /* A discovery subsystem has no namespaces and hence no I/O paths */ 1072 if (subsys->subtype == NVME_NQN_DISC) 1073 return sysfs_emit(buf, "none\n"); 1074 1075 return sysfs_emit(buf, "%s\n", 1076 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]); 1077 } 1078 1079 static void nvme_subsys_iopolicy_update(struct nvme_subsystem *subsys, 1080 int iopolicy) 1081 { 1082 struct nvme_ctrl *ctrl; 1083 int old_iopolicy = READ_ONCE(subsys->iopolicy); 1084 1085 if (old_iopolicy == iopolicy) 1086 return; 1087 1088 WRITE_ONCE(subsys->iopolicy, iopolicy); 1089 1090 /* iopolicy changes clear the mpath by design */ 1091 mutex_lock(&nvme_subsystems_lock); 1092 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) 1093 nvme_mpath_clear_ctrl_paths(ctrl); 1094 mutex_unlock(&nvme_subsystems_lock); 1095 1096 pr_notice("subsysnqn %s iopolicy changed from %s to %s\n", 1097 subsys->subnqn, 1098 nvme_iopolicy_names[old_iopolicy], 1099 nvme_iopolicy_names[iopolicy]); 1100 } 1101 1102 static ssize_t nvme_subsys_iopolicy_store(struct device *dev, 1103 struct device_attribute *attr, const char *buf, size_t count) 1104 { 1105 struct nvme_subsystem *subsys = 1106 container_of(dev, struct nvme_subsystem, dev); 1107 int policy; 1108 1109 if (subsys->subtype == NVME_NQN_DISC) 1110 return -EOPNOTSUPP; 1111 1112 policy = nvme_iopolicy_parse(buf); 1113 if (policy < 0) 1114 return policy; 1115 1116 nvme_subsys_iopolicy_update(subsys, policy); 1117 return count; 1118 } 1119 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR, 1120 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store); 1121 1122 static ssize_t iopolicies_show(struct device *dev, 1123 struct device_attribute *attr, char *buf) 1124 { 1125 struct nvme_subsystem *subsys = 1126 container_of(dev, struct nvme_subsystem, dev); 1127 int i, len = 0; 1128 1129 if (subsys->subtype == NVME_NQN_DISC) 1130 return sysfs_emit(buf, "none\n"); 1131 1132 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) 1133 len += sysfs_emit_at(buf, len, "%s%s", i ? " " : "", 1134 nvme_iopolicy_names[i]); 1135 1136 len += sysfs_emit_at(buf, len, "\n"); 1137 return len; 1138 } 1139 1140 struct device_attribute subsys_attr_iopolicies = __ATTR_RO(iopolicies); 1141 1142 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr, 1143 char *buf) 1144 { 1145 return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid); 1146 } 1147 DEVICE_ATTR_RO(ana_grpid); 1148 1149 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr, 1150 char *buf) 1151 { 1152 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1153 1154 return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]); 1155 } 1156 DEVICE_ATTR_RO(ana_state); 1157 1158 static ssize_t queue_depth_show(struct device *dev, 1159 struct device_attribute *attr, char *buf) 1160 { 1161 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1162 1163 if (ns->head->subsys->iopolicy != NVME_IOPOLICY_QD) 1164 return 0; 1165 1166 return sysfs_emit(buf, "%d\n", atomic_read(&ns->ctrl->nr_active)); 1167 } 1168 DEVICE_ATTR_RO(queue_depth); 1169 1170 static const char * const nvme_path_state_names[] = { 1171 [NVME_PATH_ENABLED] = "enabled", 1172 [NVME_PATH_DISABLED_CTRL_DOWN] = "ctrl-down", 1173 [NVME_PATH_DISABLED_ANA_PENDING] = "ana-pending", 1174 [NVME_PATH_DISABLED_NS_NOT_READY] = "ns-not-ready", 1175 }; 1176 1177 static ssize_t path_state_show(struct device *dev, 1178 struct device_attribute *attr, char *buf) 1179 { 1180 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1181 enum nvme_path_state state = nvme_path_get_state(ns); 1182 const char *name = "unknown"; 1183 1184 if (state < ARRAY_SIZE(nvme_path_state_names) && 1185 nvme_path_state_names[state]) 1186 name = nvme_path_state_names[state]; 1187 1188 return sysfs_emit(buf, "%s\n", name); 1189 } 1190 DEVICE_ATTR_RO(path_state); 1191 1192 static ssize_t numa_nodes_show(struct device *dev, struct device_attribute *attr, 1193 char *buf) 1194 { 1195 int node, srcu_idx; 1196 nodemask_t numa_nodes; 1197 struct nvme_ns *current_ns; 1198 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1199 struct nvme_ns_head *head = ns->head; 1200 1201 if (head->subsys->iopolicy != NVME_IOPOLICY_NUMA) 1202 return 0; 1203 1204 nodes_clear(numa_nodes); 1205 1206 srcu_idx = srcu_read_lock(&head->srcu); 1207 for_each_node(node) { 1208 current_ns = srcu_dereference(head->current_path[node], 1209 &head->srcu); 1210 if (ns == current_ns) 1211 node_set(node, numa_nodes); 1212 } 1213 srcu_read_unlock(&head->srcu, srcu_idx); 1214 1215 return sysfs_emit(buf, "%*pbl\n", nodemask_pr_args(&numa_nodes)); 1216 } 1217 DEVICE_ATTR_RO(numa_nodes); 1218 1219 static ssize_t delayed_removal_secs_show(struct device *dev, 1220 struct device_attribute *attr, char *buf) 1221 { 1222 struct gendisk *disk = dev_to_disk(dev); 1223 struct nvme_ns_head *head = disk->private_data; 1224 int ret; 1225 1226 mutex_lock(&head->subsys->lock); 1227 ret = sysfs_emit(buf, "%u\n", head->delayed_removal_secs); 1228 mutex_unlock(&head->subsys->lock); 1229 return ret; 1230 } 1231 1232 static ssize_t delayed_removal_secs_store(struct device *dev, 1233 struct device_attribute *attr, const char *buf, size_t count) 1234 { 1235 struct gendisk *disk = dev_to_disk(dev); 1236 struct nvme_ns_head *head = disk->private_data; 1237 unsigned int sec; 1238 int ret; 1239 1240 ret = kstrtouint(buf, 0, &sec); 1241 if (ret < 0) 1242 return ret; 1243 1244 mutex_lock(&head->subsys->lock); 1245 head->delayed_removal_secs = sec; 1246 assign_bit(NVME_NSHEAD_QUEUE_IF_NO_PATH, &head->flags, sec); 1247 mutex_unlock(&head->subsys->lock); 1248 /* 1249 * Ensure that update to NVME_NSHEAD_QUEUE_IF_NO_PATH is seen 1250 * by its reader. 1251 */ 1252 synchronize_srcu(&head->srcu); 1253 1254 return count; 1255 } 1256 1257 DEVICE_ATTR_RW(delayed_removal_secs); 1258 1259 static ssize_t fail_on_ctrl_loss_show(struct device *dev, 1260 struct device_attribute *attr, char *buf) 1261 { 1262 struct gendisk *disk = dev_to_disk(dev); 1263 struct nvme_ns_head *head = disk->private_data; 1264 1265 return sysfs_emit(buf, test_bit(NVME_NSHEAD_FAIL_ON_CTRL_LOSS, 1266 &head->flags) ? "on\n" : "off\n"); 1267 } 1268 1269 static ssize_t fail_on_ctrl_loss_store(struct device *dev, 1270 struct device_attribute *attr, const char *buf, size_t count) 1271 { 1272 struct gendisk *disk = dev_to_disk(dev); 1273 struct nvme_ns_head *head = disk->private_data; 1274 bool enable; 1275 int ret; 1276 1277 ret = kstrtobool(buf, &enable); 1278 if (ret < 0) 1279 return ret; 1280 1281 if (enable) { 1282 if (test_and_set_bit(NVME_NSHEAD_FAIL_ON_CTRL_LOSS, &head->flags)) 1283 return count; 1284 } else { 1285 if (!test_and_clear_bit(NVME_NSHEAD_FAIL_ON_CTRL_LOSS, 1286 &head->flags)) 1287 return count; 1288 } 1289 1290 synchronize_srcu(&head->srcu); 1291 1292 if (enable) 1293 kblockd_schedule_work(&head->requeue_work); 1294 1295 return count; 1296 } 1297 1298 DEVICE_ATTR_RW(fail_on_ctrl_loss); 1299 1300 static ssize_t multipath_failover_count_show(struct device *dev, 1301 struct device_attribute *attr, char *buf) 1302 { 1303 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1304 1305 return sysfs_emit(buf, "%lu\n", atomic_long_read(&ns->failover)); 1306 } 1307 1308 static ssize_t multipath_failover_count_store(struct device *dev, 1309 struct device_attribute *attr, const char *buf, size_t count) 1310 { 1311 unsigned long failover; 1312 int ret; 1313 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1314 1315 ret = kstrtoul(buf, 0, &failover); 1316 if (ret) 1317 return -EINVAL; 1318 1319 atomic_long_set(&ns->failover, failover); 1320 1321 return count; 1322 } 1323 1324 DEVICE_ATTR_RW(multipath_failover_count); 1325 1326 static ssize_t io_requeue_no_usable_path_count_show(struct device *dev, 1327 struct device_attribute *attr, char *buf) 1328 { 1329 struct gendisk *disk = dev_to_disk(dev); 1330 struct nvme_ns_head *head = disk->private_data; 1331 1332 return sysfs_emit(buf, "%lu\n", 1333 atomic_long_read(&head->io_requeue_no_usable_path_count)); 1334 } 1335 1336 static ssize_t io_requeue_no_usable_path_count_store(struct device *dev, 1337 struct device_attribute *attr, const char *buf, size_t count) 1338 { 1339 int err; 1340 unsigned long requeue_cnt; 1341 struct gendisk *disk = dev_to_disk(dev); 1342 struct nvme_ns_head *head = disk->private_data; 1343 1344 err = kstrtoul(buf, 0, &requeue_cnt); 1345 if (err) 1346 return -EINVAL; 1347 1348 atomic_long_set(&head->io_requeue_no_usable_path_count, requeue_cnt); 1349 1350 return count; 1351 } 1352 1353 DEVICE_ATTR_RW(io_requeue_no_usable_path_count); 1354 1355 static ssize_t io_fail_no_available_path_count_show(struct device *dev, 1356 struct device_attribute *attr, char *buf) 1357 { 1358 struct gendisk *disk = dev_to_disk(dev); 1359 struct nvme_ns_head *head = disk->private_data; 1360 1361 return sysfs_emit(buf, "%lu\n", 1362 atomic_long_read(&head->io_fail_no_available_path_count)); 1363 } 1364 1365 static ssize_t io_fail_no_available_path_count_store(struct device *dev, 1366 struct device_attribute *attr, const char *buf, size_t count) 1367 { 1368 int err; 1369 unsigned long fail_cnt; 1370 struct gendisk *disk = dev_to_disk(dev); 1371 struct nvme_ns_head *head = disk->private_data; 1372 1373 err = kstrtoul(buf, 0, &fail_cnt); 1374 if (err) 1375 return -EINVAL; 1376 1377 atomic_long_set(&head->io_fail_no_available_path_count, fail_cnt); 1378 1379 return count; 1380 } 1381 1382 DEVICE_ATTR_RW(io_fail_no_available_path_count); 1383 1384 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl, 1385 struct nvme_ana_group_desc *desc, void *data) 1386 { 1387 struct nvme_ana_group_desc *dst = data; 1388 1389 if (desc->grpid != dst->grpid) 1390 return 0; 1391 1392 *dst = *desc; 1393 return -ENXIO; /* just break out of the loop */ 1394 } 1395 1396 void nvme_mpath_add_sysfs_link(struct nvme_ns_head *head) 1397 { 1398 struct device *target; 1399 int rc, srcu_idx; 1400 struct nvme_ns *ns; 1401 struct kobject *kobj; 1402 1403 /* 1404 * Ensure head disk node is already added otherwise we may get invalid 1405 * kobj for head disk node 1406 */ 1407 if (!test_bit(GD_ADDED, &head->disk->state)) 1408 return; 1409 1410 kobj = &disk_to_dev(head->disk)->kobj; 1411 1412 /* 1413 * loop through each ns chained through the head->list and create the 1414 * sysfs link from head node to the ns path node 1415 */ 1416 srcu_idx = srcu_read_lock(&head->srcu); 1417 1418 list_for_each_entry_srcu(ns, &head->list, siblings, 1419 srcu_read_lock_held(&head->srcu)) { 1420 /* 1421 * Ensure that ns path disk node is already added otherwise we 1422 * may get invalid kobj name for target 1423 */ 1424 if (!test_bit(GD_ADDED, &ns->disk->state)) 1425 continue; 1426 1427 /* 1428 * Avoid creating link if it already exists for the given path. 1429 * When path ana state transitions from optimized to non- 1430 * optimized or vice-versa, the nvme_mpath_set_live() is 1431 * invoked which in truns call this function. Now if the sysfs 1432 * link already exists for the given path and we attempt to re- 1433 * create the link then sysfs code would warn about it loudly. 1434 * So we evaluate NVME_NS_SYSFS_ATTR_LINK flag here to ensure 1435 * that we're not creating duplicate link. 1436 * The test_and_set_bit() is used because it is protecting 1437 * against multiple nvme paths being simultaneously added. 1438 */ 1439 if (test_and_set_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags)) 1440 continue; 1441 1442 target = disk_to_dev(ns->disk); 1443 /* 1444 * Create sysfs link from head gendisk kobject @kobj to the 1445 * ns path gendisk kobject @target->kobj. 1446 */ 1447 rc = sysfs_add_link_to_group(kobj, nvme_ns_mpath_attr_group.name, 1448 &target->kobj, dev_name(target)); 1449 if (unlikely(rc)) { 1450 dev_err(disk_to_dev(ns->head->disk), 1451 "failed to create link to %s\n", 1452 dev_name(target)); 1453 clear_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags); 1454 } 1455 } 1456 1457 srcu_read_unlock(&head->srcu, srcu_idx); 1458 } 1459 1460 void nvme_mpath_remove_sysfs_link(struct nvme_ns *ns) 1461 { 1462 struct device *target; 1463 struct kobject *kobj; 1464 1465 if (!test_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags)) 1466 return; 1467 1468 target = disk_to_dev(ns->disk); 1469 kobj = &disk_to_dev(ns->head->disk)->kobj; 1470 sysfs_remove_link_from_group(kobj, nvme_ns_mpath_attr_group.name, 1471 dev_name(target)); 1472 clear_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags); 1473 } 1474 1475 void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid) 1476 { 1477 if (nvme_ctrl_use_ana(ns->ctrl)) { 1478 struct nvme_ana_group_desc desc = { 1479 .grpid = anagrpid, 1480 .state = 0, 1481 }; 1482 1483 mutex_lock(&ns->ctrl->ana_lock); 1484 ns->ana_grpid = le32_to_cpu(anagrpid); 1485 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc); 1486 mutex_unlock(&ns->ctrl->ana_lock); 1487 if (desc.state) { 1488 /* found the group desc: update */ 1489 nvme_update_ns_ana_state(&desc, ns); 1490 } else { 1491 /* group desc not found: trigger a re-read */ 1492 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 1493 queue_work(nvme_wq, &ns->ctrl->ana_work); 1494 } 1495 } else { 1496 ns->ana_state = NVME_ANA_OPTIMIZED; 1497 nvme_mpath_set_live(ns); 1498 } 1499 1500 } 1501 1502 void nvme_mpath_remove_disk(struct nvme_ns_head *head) 1503 { 1504 bool remove = false; 1505 1506 if (!head->disk) 1507 return; 1508 1509 mutex_lock(&head->subsys->lock); 1510 /* 1511 * We are called when all paths have been removed, and at that point 1512 * head->list is expected to be empty. However, nvme_ns_remove() and 1513 * nvme_init_ns_head() can run concurrently and so if head->delayed_ 1514 * removal_secs is configured, it is possible that by the time we reach 1515 * this point, head->list may no longer be empty. Therefore, we recheck 1516 * head->list here. If it is no longer empty then we skip enqueuing the 1517 * delayed head removal work. 1518 */ 1519 if (!list_empty(&head->list)) 1520 goto out; 1521 1522 /* 1523 * Ensure that no one could remove this module while the head 1524 * remove work is pending. 1525 */ 1526 if (head->delayed_removal_secs && try_module_get(THIS_MODULE)) { 1527 mod_delayed_work(nvme_wq, &head->remove_work, 1528 head->delayed_removal_secs * HZ); 1529 } else { 1530 list_del_init(&head->entry); 1531 remove = true; 1532 } 1533 out: 1534 mutex_unlock(&head->subsys->lock); 1535 if (remove) 1536 nvme_remove_head(head); 1537 } 1538 1539 void nvme_mpath_put_disk(struct nvme_ns_head *head) 1540 { 1541 if (!head->disk) 1542 return; 1543 /* make sure all pending bios are cleaned up */ 1544 kblockd_schedule_work(&head->requeue_work); 1545 flush_work(&head->requeue_work); 1546 flush_work(&head->partition_scan_work); 1547 put_disk(head->disk); 1548 } 1549 1550 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl) 1551 { 1552 mutex_init(&ctrl->ana_lock); 1553 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0); 1554 INIT_WORK(&ctrl->ana_work, nvme_ana_work); 1555 } 1556 1557 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 1558 { 1559 size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT; 1560 size_t ana_log_size; 1561 int error = 0; 1562 1563 /* check if multipath is enabled and we have the capability */ 1564 if (!multipath || !ctrl->subsys || 1565 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)) 1566 return 0; 1567 1568 /* initialize this in the identify path to cover controller resets */ 1569 atomic_set(&ctrl->nr_active, 0); 1570 1571 if (!ctrl->max_namespaces || 1572 ctrl->max_namespaces > le32_to_cpu(id->nn)) { 1573 dev_err(ctrl->device, 1574 "Invalid MNAN value %u\n", ctrl->max_namespaces); 1575 return -EINVAL; 1576 } 1577 1578 ctrl->anacap = id->anacap; 1579 ctrl->anatt = id->anatt; 1580 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid); 1581 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax); 1582 1583 ana_log_size = sizeof(struct nvme_ana_rsp_hdr) + 1584 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) + 1585 ctrl->max_namespaces * sizeof(__le32); 1586 if (ana_log_size > max_transfer_size) { 1587 dev_err(ctrl->device, 1588 "ANA log page size (%zd) larger than MDTS (%zd).\n", 1589 ana_log_size, max_transfer_size); 1590 dev_err(ctrl->device, "disabling ANA support.\n"); 1591 goto out_uninit; 1592 } 1593 if (ana_log_size > ctrl->ana_log_size) { 1594 nvme_mpath_stop(ctrl); 1595 nvme_mpath_uninit(ctrl); 1596 ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL); 1597 if (!ctrl->ana_log_buf) 1598 return -ENOMEM; 1599 } 1600 ctrl->ana_log_size = ana_log_size; 1601 error = nvme_read_ana_log(ctrl); 1602 if (error) 1603 goto out_uninit; 1604 return 0; 1605 1606 out_uninit: 1607 nvme_mpath_uninit(ctrl); 1608 return error; 1609 } 1610 1611 void nvme_mpath_uninit(struct nvme_ctrl *ctrl) 1612 { 1613 kvfree(ctrl->ana_log_buf); 1614 ctrl->ana_log_buf = NULL; 1615 ctrl->ana_log_size = 0; 1616 } 1617