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 static bool nvme_path_is_disabled(struct nvme_ns *ns) 311 { 312 enum nvme_ctrl_state state = nvme_ctrl_state(ns->ctrl); 313 314 /* 315 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should 316 * still be able to complete assuming that the controller is connected. 317 * Otherwise it will fail immediately and return to the requeue list. 318 */ 319 if (state != NVME_CTRL_LIVE && state != NVME_CTRL_DELETING) 320 return true; 321 if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) || 322 !test_bit(NVME_NS_READY, &ns->flags)) 323 return true; 324 return false; 325 } 326 327 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node) 328 __must_hold_shared(&head->srcu) 329 { 330 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance; 331 struct nvme_ns *found = NULL, *fallback = NULL, *ns; 332 333 list_for_each_entry_srcu(ns, &head->list, siblings, 334 srcu_read_lock_held(&head->srcu)) { 335 if (nvme_path_is_disabled(ns)) 336 continue; 337 338 if (ns->ctrl->numa_node != NUMA_NO_NODE && 339 READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA) 340 distance = node_distance(node, ns->ctrl->numa_node); 341 else 342 distance = LOCAL_DISTANCE; 343 344 switch (ns->ana_state) { 345 case NVME_ANA_OPTIMIZED: 346 if (distance < found_distance) { 347 found_distance = distance; 348 found = ns; 349 } 350 break; 351 case NVME_ANA_NONOPTIMIZED: 352 if (distance < fallback_distance) { 353 fallback_distance = distance; 354 fallback = ns; 355 } 356 break; 357 default: 358 break; 359 } 360 } 361 362 if (!found) 363 found = fallback; 364 if (found) 365 rcu_assign_pointer(head->current_path[node], found); 366 return found; 367 } 368 369 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head, 370 struct nvme_ns *ns) 371 __must_hold_shared(&head->srcu) 372 { 373 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns, 374 siblings); 375 if (ns) 376 return ns; 377 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings); 378 } 379 380 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head) 381 __must_hold_shared(&head->srcu) 382 { 383 struct nvme_ns *ns, *found = NULL; 384 int node = numa_node_id(); 385 struct nvme_ns *old = srcu_dereference(head->current_path[node], 386 &head->srcu); 387 388 if (unlikely(!old)) 389 return __nvme_find_path(head, node); 390 391 if (list_is_singular(&head->list)) { 392 if (nvme_path_is_disabled(old)) 393 return NULL; 394 return old; 395 } 396 397 for (ns = nvme_next_ns(head, old); 398 ns && ns != old; 399 ns = nvme_next_ns(head, ns)) { 400 if (nvme_path_is_disabled(ns)) 401 continue; 402 403 if (ns->ana_state == NVME_ANA_OPTIMIZED) { 404 found = ns; 405 goto out; 406 } 407 if (ns->ana_state == NVME_ANA_NONOPTIMIZED) 408 found = ns; 409 } 410 411 /* 412 * The loop above skips the current path for round-robin semantics. 413 * Fall back to the current path if either: 414 * - no other optimized path found and current is optimized, 415 * - no other usable path found and current is usable. 416 */ 417 if (!nvme_path_is_disabled(old) && 418 (old->ana_state == NVME_ANA_OPTIMIZED || 419 (!found && old->ana_state == NVME_ANA_NONOPTIMIZED))) 420 return old; 421 422 if (!found) 423 return NULL; 424 out: 425 rcu_assign_pointer(head->current_path[node], found); 426 return found; 427 } 428 429 static struct nvme_ns *nvme_queue_depth_path(struct nvme_ns_head *head) 430 __must_hold_shared(&head->srcu) 431 { 432 struct nvme_ns *best_opt = NULL, *best_nonopt = NULL, *ns; 433 unsigned int min_depth_opt = UINT_MAX, min_depth_nonopt = UINT_MAX; 434 unsigned int depth; 435 436 list_for_each_entry_srcu(ns, &head->list, siblings, 437 srcu_read_lock_held(&head->srcu)) { 438 if (nvme_path_is_disabled(ns)) 439 continue; 440 441 depth = atomic_read(&ns->ctrl->nr_active); 442 443 switch (ns->ana_state) { 444 case NVME_ANA_OPTIMIZED: 445 if (depth < min_depth_opt) { 446 min_depth_opt = depth; 447 best_opt = ns; 448 } 449 break; 450 case NVME_ANA_NONOPTIMIZED: 451 if (depth < min_depth_nonopt) { 452 min_depth_nonopt = depth; 453 best_nonopt = ns; 454 } 455 break; 456 default: 457 break; 458 } 459 460 if (min_depth_opt == 0) 461 return best_opt; 462 } 463 464 return best_opt ? best_opt : best_nonopt; 465 } 466 467 static inline bool nvme_path_is_optimized(struct nvme_ns *ns) 468 { 469 return nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE && 470 ns->ana_state == NVME_ANA_OPTIMIZED; 471 } 472 473 static struct nvme_ns *nvme_numa_path(struct nvme_ns_head *head) 474 __must_hold_shared(&head->srcu) 475 { 476 int node = numa_node_id(); 477 struct nvme_ns *ns; 478 479 ns = srcu_dereference(head->current_path[node], &head->srcu); 480 if (unlikely(!ns)) 481 return __nvme_find_path(head, node); 482 if (unlikely(!nvme_path_is_optimized(ns))) 483 return __nvme_find_path(head, node); 484 return ns; 485 } 486 487 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head) 488 { 489 switch (READ_ONCE(head->subsys->iopolicy)) { 490 case NVME_IOPOLICY_QD: 491 return nvme_queue_depth_path(head); 492 case NVME_IOPOLICY_RR: 493 return nvme_round_robin_path(head); 494 default: 495 return nvme_numa_path(head); 496 } 497 } 498 499 static bool nvme_available_path(struct nvme_ns_head *head) 500 __must_hold_shared(&head->srcu) 501 { 502 struct nvme_ns *ns; 503 504 if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) 505 return false; 506 507 list_for_each_entry_srcu(ns, &head->list, siblings, 508 srcu_read_lock_held(&head->srcu)) { 509 if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags)) 510 continue; 511 switch (nvme_ctrl_state(ns->ctrl)) { 512 case NVME_CTRL_LIVE: 513 case NVME_CTRL_RESETTING: 514 case NVME_CTRL_CONNECTING: 515 return true; 516 default: 517 break; 518 } 519 } 520 521 /* 522 * If "head->delayed_removal_secs" is configured (i.e., non-zero), do 523 * not immediately fail I/O. Instead, requeue the I/O for the configured 524 * duration, anticipating that if there's a transient link failure then 525 * it may recover within this time window. This parameter is exported to 526 * userspace via sysfs, and its default value is zero. It is internally 527 * mapped to NVME_NSHEAD_QUEUE_IF_NO_PATH. When delayed_removal_secs is 528 * non-zero, this flag is set to true. When zero, the flag is cleared. 529 */ 530 return nvme_mpath_queue_if_no_path(head); 531 } 532 533 static void nvme_ns_head_submit_bio(struct bio *bio) 534 { 535 struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data; 536 struct device *dev = disk_to_dev(head->disk); 537 struct nvme_ns *ns; 538 int srcu_idx; 539 540 /* 541 * The namespace might be going away and the bio might be moved to a 542 * different queue via blk_steal_bios(), so we need to use the bio_split 543 * pool from the original queue to allocate the bvecs from. 544 */ 545 bio = bio_split_to_limits(bio); 546 if (!bio) 547 return; 548 549 srcu_idx = srcu_read_lock(&head->srcu); 550 ns = nvme_find_path(head); 551 if (likely(ns)) { 552 bio_set_dev(bio, ns->disk->part0); 553 /* 554 * Use BIO_REMAPPED to skip bio_check_eod() when this bio 555 * enters submit_bio_noacct() for the per-path device. The EOD 556 * check already passed on the multipath head. 557 */ 558 bio_set_flag(bio, BIO_REMAPPED); 559 bio->bi_opf |= REQ_NVME_MPATH; 560 trace_block_bio_remap(bio, disk_devt(ns->head->disk), 561 bio->bi_iter.bi_sector); 562 submit_bio_noacct(bio); 563 } else if (nvme_available_path(head)) { 564 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n"); 565 566 spin_lock_irq(&head->requeue_lock); 567 bio_list_add(&head->requeue_list, bio); 568 spin_unlock_irq(&head->requeue_lock); 569 atomic_long_inc(&head->io_requeue_no_usable_path_count); 570 } else { 571 dev_warn_ratelimited(dev, "no available path - failing I/O\n"); 572 573 bio_io_error(bio); 574 atomic_long_inc(&head->io_fail_no_available_path_count); 575 } 576 577 srcu_read_unlock(&head->srcu, srcu_idx); 578 } 579 580 static int nvme_ns_head_open(struct gendisk *disk, blk_mode_t mode) 581 { 582 if (!nvme_tryget_ns_head(disk->private_data)) 583 return -ENXIO; 584 return 0; 585 } 586 587 static void nvme_ns_head_release(struct gendisk *disk) 588 { 589 nvme_put_ns_head(disk->private_data); 590 } 591 592 static int nvme_ns_head_get_unique_id(struct gendisk *disk, u8 id[16], 593 enum blk_unique_id type) 594 { 595 struct nvme_ns_head *head = disk->private_data; 596 struct nvme_ns *ns; 597 int srcu_idx, ret = -EWOULDBLOCK; 598 599 srcu_idx = srcu_read_lock(&head->srcu); 600 ns = nvme_find_path(head); 601 if (ns) 602 ret = nvme_ns_get_unique_id(ns, id, type); 603 srcu_read_unlock(&head->srcu, srcu_idx); 604 return ret; 605 } 606 607 #ifdef CONFIG_BLK_DEV_ZONED 608 static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector, 609 unsigned int nr_zones, struct blk_report_zones_args *args) 610 { 611 struct nvme_ns_head *head = disk->private_data; 612 struct nvme_ns *ns; 613 int srcu_idx, ret = -EWOULDBLOCK; 614 615 srcu_idx = srcu_read_lock(&head->srcu); 616 ns = nvme_find_path(head); 617 if (ns) 618 ret = nvme_ns_report_zones(ns, sector, nr_zones, args); 619 srcu_read_unlock(&head->srcu, srcu_idx); 620 return ret; 621 } 622 #else 623 #define nvme_ns_head_report_zones NULL 624 #endif /* CONFIG_BLK_DEV_ZONED */ 625 626 const struct block_device_operations nvme_ns_head_ops = { 627 .owner = THIS_MODULE, 628 .submit_bio = nvme_ns_head_submit_bio, 629 .open = nvme_ns_head_open, 630 .release = nvme_ns_head_release, 631 .ioctl = nvme_ns_head_ioctl, 632 .compat_ioctl = blkdev_compat_ptr_ioctl, 633 .getgeo = nvme_getgeo, 634 .get_unique_id = nvme_ns_head_get_unique_id, 635 .report_zones = nvme_ns_head_report_zones, 636 .pr_ops = &nvme_pr_ops, 637 }; 638 639 static const struct file_operations nvme_ns_head_chr_fops = { 640 .owner = THIS_MODULE, 641 .unlocked_ioctl = nvme_ns_head_chr_ioctl, 642 .compat_ioctl = compat_ptr_ioctl, 643 .uring_cmd = nvme_ns_head_chr_uring_cmd, 644 .uring_cmd_iopoll = nvme_ns_chr_uring_cmd_iopoll, 645 }; 646 647 static void nvme_add_ns_head_cdev(struct nvme_ns_head *head) 648 { 649 char name[32]; 650 651 head->cdev_device.parent = &head->subsys->dev; 652 snprintf(name, sizeof(name), "ng%dn%d", head->subsys->instance, 653 head->instance); 654 655 nvme_get_ns_head(head); /* Undone in nvme_cdev_rel() */ 656 if (nvme_cdev_add(name, &head->cdev, &head->cdev_device, 657 &nvme_ns_head_chr_fops, THIS_MODULE)) { 658 dev_err(disk_to_dev(head->disk), 659 "Unable to create the %s device\n", name); 660 nvme_put_ns_head(head); 661 return; 662 } 663 set_bit(NVME_NSHEAD_CDEV_LIVE, &head->flags); 664 } 665 666 static void nvme_partition_scan_work(struct work_struct *work) 667 { 668 struct nvme_ns_head *head = 669 container_of(work, struct nvme_ns_head, partition_scan_work); 670 671 if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN, 672 &head->disk->state))) 673 return; 674 675 mutex_lock(&head->disk->open_mutex); 676 bdev_disk_changed(head->disk, false); 677 mutex_unlock(&head->disk->open_mutex); 678 } 679 680 static void nvme_requeue_work(struct work_struct *work) 681 { 682 struct nvme_ns_head *head = 683 container_of(work, struct nvme_ns_head, requeue_work); 684 struct bio *bio, *next; 685 686 spin_lock_irq(&head->requeue_lock); 687 next = bio_list_get(&head->requeue_list); 688 spin_unlock_irq(&head->requeue_lock); 689 690 while ((bio = next) != NULL) { 691 next = bio->bi_next; 692 bio->bi_next = NULL; 693 694 submit_bio_noacct(bio); 695 } 696 } 697 698 static void nvme_remove_head(struct nvme_ns_head *head) 699 { 700 if (test_and_clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 701 /* 702 * Requeue I/O after NVME_NSHEAD_DISK_LIVE has been cleared 703 * to allow multipath to fail all I/O. First synchronize to 704 * add any bios to the requeue list. 705 */ 706 synchronize_srcu(&head->srcu); 707 kblockd_schedule_work(&head->requeue_work); 708 709 if (test_and_clear_bit(NVME_NSHEAD_CDEV_LIVE, &head->flags)) 710 nvme_cdev_del(&head->cdev, &head->cdev_device); 711 del_gendisk(head->disk); 712 } 713 nvme_put_ns_head(head); 714 } 715 716 static void nvme_remove_head_work(struct work_struct *work) 717 { 718 struct nvme_ns_head *head = container_of(to_delayed_work(work), 719 struct nvme_ns_head, remove_work); 720 bool remove = false; 721 722 mutex_lock(&head->subsys->lock); 723 if (list_empty(&head->list)) { 724 list_del_init(&head->entry); 725 remove = true; 726 } 727 mutex_unlock(&head->subsys->lock); 728 if (remove) 729 nvme_remove_head(head); 730 731 module_put(THIS_MODULE); 732 } 733 734 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head) 735 { 736 struct queue_limits lim; 737 738 mutex_init(&head->lock); 739 spin_lock_init(&head->requeue_lock); 740 INIT_WORK(&head->requeue_work, nvme_requeue_work); 741 INIT_WORK(&head->partition_scan_work, nvme_partition_scan_work); 742 INIT_DELAYED_WORK(&head->remove_work, nvme_remove_head_work); 743 744 /* 745 * If "multipath_always_on" is enabled, a multipath node is added 746 * regardless of whether the disk is single/multi ported, and whether 747 * the namespace is shared or private. If "multipath_always_on" is not 748 * enabled, a multipath node is added only if the subsystem supports 749 * multiple controllers and the "multipath" option is configured. In 750 * either case, for private namespaces, we ensure that the NSID is 751 * unique. 752 */ 753 if (!multipath_always_on) { 754 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || 755 !multipath) 756 return 0; 757 } 758 759 if (!nvme_is_unique_nsid(ctrl, head)) 760 return 0; 761 762 blk_set_stacking_limits(&lim); 763 lim.dma_alignment = 3; 764 lim.features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT | 765 BLK_FEAT_POLL | BLK_FEAT_ATOMIC_WRITES | BLK_FEAT_PCI_P2PDMA; 766 if (head->ids.csi == NVME_CSI_ZNS) 767 lim.features |= BLK_FEAT_ZONED; 768 769 head->disk = blk_alloc_disk(&lim, ctrl->numa_node); 770 if (IS_ERR(head->disk)) 771 return PTR_ERR(head->disk); 772 head->disk->fops = &nvme_ns_head_ops; 773 head->disk->private_data = head; 774 775 /* 776 * We need to suppress the partition scan from occuring within the 777 * controller's scan_work context. If a path error occurs here, the IO 778 * will wait until a path becomes available or all paths are torn down, 779 * but that action also occurs within scan_work, so it would deadlock. 780 * Defer the partition scan to a different context that does not block 781 * scan_work. 782 */ 783 set_bit(GD_SUPPRESS_PART_SCAN, &head->disk->state); 784 sprintf(head->disk->disk_name, "nvme%dn%d", 785 ctrl->subsys->instance, head->instance); 786 nvme_get_ns_head(head); 787 return 0; 788 } 789 790 static void nvme_mpath_set_live(struct nvme_ns *ns) 791 { 792 struct nvme_ns_head *head = ns->head; 793 int rc; 794 795 if (!head->disk) 796 return; 797 798 /* 799 * test_and_set_bit() is used because it is protecting against two nvme 800 * paths simultaneously calling device_add_disk() on the same namespace 801 * head. 802 */ 803 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 804 rc = device_add_disk(&head->subsys->dev, head->disk, 805 nvme_ns_attr_groups); 806 if (rc) { 807 clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags); 808 return; 809 } 810 nvme_add_ns_head_cdev(head); 811 queue_work(nvme_wq, &head->partition_scan_work); 812 } 813 814 nvme_mpath_add_sysfs_link(ns->head); 815 816 mutex_lock(&head->lock); 817 if (nvme_path_is_optimized(ns)) { 818 int node, srcu_idx; 819 820 srcu_idx = srcu_read_lock(&head->srcu); 821 for_each_online_node(node) 822 __nvme_find_path(head, node); 823 srcu_read_unlock(&head->srcu, srcu_idx); 824 } 825 mutex_unlock(&head->lock); 826 827 synchronize_srcu(&head->srcu); 828 nvme_mpath_revalidate_zones(head); 829 kblockd_schedule_work(&head->requeue_work); 830 } 831 832 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data, 833 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *, 834 void *)) 835 __must_hold(&ctrl->ana_lock) 836 { 837 void *base = ctrl->ana_log_buf; 838 size_t offset = sizeof(struct nvme_ana_rsp_hdr); 839 int error, i; 840 841 lockdep_assert_held(&ctrl->ana_lock); 842 843 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) { 844 struct nvme_ana_group_desc *desc = base + offset; 845 u32 nr_nsids; 846 size_t nsid_buf_size; 847 848 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc))) 849 return -EINVAL; 850 851 nr_nsids = le32_to_cpu(desc->nnsids); 852 nsid_buf_size = flex_array_size(desc, nsids, nr_nsids); 853 854 if (WARN_ON_ONCE(desc->grpid == 0)) 855 return -EINVAL; 856 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax)) 857 return -EINVAL; 858 if (WARN_ON_ONCE(desc->state == 0)) 859 return -EINVAL; 860 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE)) 861 return -EINVAL; 862 863 offset += sizeof(*desc); 864 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size)) 865 return -EINVAL; 866 867 error = cb(ctrl, desc, data); 868 if (error) 869 return error; 870 871 offset += nsid_buf_size; 872 } 873 874 return 0; 875 } 876 877 static inline bool nvme_state_is_live(enum nvme_ana_state state) 878 { 879 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED; 880 } 881 882 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc, 883 struct nvme_ns *ns) 884 { 885 ns->ana_grpid = le32_to_cpu(desc->grpid); 886 ns->ana_state = desc->state; 887 clear_bit(NVME_NS_ANA_PENDING, &ns->flags); 888 /* 889 * nvme_mpath_set_live() will trigger I/O to the multipath path device 890 * and in turn to this path device. However we cannot accept this I/O 891 * if the controller is not live. This may deadlock if called from 892 * nvme_mpath_init_identify() and the ctrl will never complete 893 * initialization, preventing I/O from completing. For this case we 894 * will reprocess the ANA log page in nvme_mpath_update() once the 895 * controller is ready. 896 */ 897 if (nvme_state_is_live(ns->ana_state) && 898 nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE) 899 nvme_mpath_set_live(ns); 900 else { 901 /* 902 * Add sysfs link from multipath head gendisk node to path 903 * device gendisk node. 904 * If path's ana state is live (i.e. state is either optimized 905 * or non-optimized) while we alloc the ns then sysfs link would 906 * be created from nvme_mpath_set_live(). In that case we would 907 * not fallthrough this code path. However for the path's ana 908 * state other than live, we call nvme_mpath_set_live() only 909 * after ana state transitioned to the live state. But we still 910 * want to create the sysfs link from head node to a path device 911 * irrespctive of the path's ana state. 912 * If we reach through here then it means that path's ana state 913 * is not live but still create the sysfs link to this path from 914 * head node if head node of the path has already come alive. 915 */ 916 if (test_bit(NVME_NSHEAD_DISK_LIVE, &ns->head->flags)) 917 nvme_mpath_add_sysfs_link(ns->head); 918 } 919 } 920 921 static int nvme_update_ana_state(struct nvme_ctrl *ctrl, 922 struct nvme_ana_group_desc *desc, void *data) 923 { 924 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0; 925 unsigned *nr_change_groups = data; 926 struct nvme_ns *ns; 927 int srcu_idx; 928 929 dev_dbg(ctrl->device, "ANA group %d: %s.\n", 930 le32_to_cpu(desc->grpid), 931 nvme_ana_state_names[desc->state]); 932 933 if (desc->state == NVME_ANA_CHANGE) 934 (*nr_change_groups)++; 935 936 if (!nr_nsids) 937 return 0; 938 939 srcu_idx = srcu_read_lock(&ctrl->srcu); 940 list_for_each_entry_srcu(ns, &ctrl->namespaces, list, 941 srcu_read_lock_held(&ctrl->srcu)) { 942 unsigned nsid; 943 again: 944 nsid = le32_to_cpu(desc->nsids[n]); 945 if (ns->head->ns_id < nsid) 946 continue; 947 if (ns->head->ns_id == nsid) 948 nvme_update_ns_ana_state(desc, ns); 949 if (++n == nr_nsids) 950 break; 951 if (ns->head->ns_id > nsid) 952 goto again; 953 } 954 srcu_read_unlock(&ctrl->srcu, srcu_idx); 955 return 0; 956 } 957 958 static int nvme_read_ana_log(struct nvme_ctrl *ctrl) 959 { 960 u32 nr_change_groups = 0; 961 int error; 962 963 mutex_lock(&ctrl->ana_lock); 964 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM, 965 ctrl->ana_log_buf, ctrl->ana_log_size, 0); 966 if (error) { 967 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error); 968 goto out_unlock; 969 } 970 971 error = nvme_parse_ana_log(ctrl, &nr_change_groups, 972 nvme_update_ana_state); 973 if (error) 974 goto out_unlock; 975 976 /* 977 * In theory we should have an ANATT timer per group as they might enter 978 * the change state at different times. But that is a lot of overhead 979 * just to protect against a target that keeps entering new changes 980 * states while never finishing previous ones. But we'll still 981 * eventually time out once all groups are in change state, so this 982 * isn't a big deal. 983 * 984 * We also double the ANATT value to provide some slack for transports 985 * or AEN processing overhead. 986 */ 987 if (nr_change_groups) 988 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies); 989 else 990 timer_delete_sync(&ctrl->anatt_timer); 991 out_unlock: 992 mutex_unlock(&ctrl->ana_lock); 993 return error; 994 } 995 996 static void nvme_ana_work(struct work_struct *work) 997 { 998 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work); 999 1000 if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) 1001 return; 1002 1003 nvme_read_ana_log(ctrl); 1004 } 1005 1006 void nvme_mpath_update(struct nvme_ctrl *ctrl) 1007 { 1008 u32 nr_change_groups = 0; 1009 1010 if (!ctrl->ana_log_buf) 1011 return; 1012 1013 mutex_lock(&ctrl->ana_lock); 1014 nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state); 1015 mutex_unlock(&ctrl->ana_lock); 1016 } 1017 1018 static void nvme_anatt_timeout(struct timer_list *t) 1019 { 1020 struct nvme_ctrl *ctrl = timer_container_of(ctrl, t, anatt_timer); 1021 1022 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n"); 1023 nvme_reset_ctrl(ctrl); 1024 } 1025 1026 void nvme_mpath_stop(struct nvme_ctrl *ctrl) 1027 { 1028 if (!nvme_ctrl_use_ana(ctrl)) 1029 return; 1030 timer_delete_sync(&ctrl->anatt_timer); 1031 cancel_work_sync(&ctrl->ana_work); 1032 } 1033 1034 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \ 1035 struct device_attribute subsys_attr_##_name = \ 1036 __ATTR(_name, _mode, _show, _store) 1037 1038 static ssize_t nvme_subsys_iopolicy_show(struct device *dev, 1039 struct device_attribute *attr, char *buf) 1040 { 1041 struct nvme_subsystem *subsys = 1042 container_of(dev, struct nvme_subsystem, dev); 1043 1044 return sysfs_emit(buf, "%s\n", 1045 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]); 1046 } 1047 1048 static void nvme_subsys_iopolicy_update(struct nvme_subsystem *subsys, 1049 int iopolicy) 1050 { 1051 struct nvme_ctrl *ctrl; 1052 int old_iopolicy = READ_ONCE(subsys->iopolicy); 1053 1054 if (old_iopolicy == iopolicy) 1055 return; 1056 1057 WRITE_ONCE(subsys->iopolicy, iopolicy); 1058 1059 /* iopolicy changes clear the mpath by design */ 1060 mutex_lock(&nvme_subsystems_lock); 1061 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) 1062 nvme_mpath_clear_ctrl_paths(ctrl); 1063 mutex_unlock(&nvme_subsystems_lock); 1064 1065 pr_notice("subsysnqn %s iopolicy changed from %s to %s\n", 1066 subsys->subnqn, 1067 nvme_iopolicy_names[old_iopolicy], 1068 nvme_iopolicy_names[iopolicy]); 1069 } 1070 1071 static ssize_t nvme_subsys_iopolicy_store(struct device *dev, 1072 struct device_attribute *attr, const char *buf, size_t count) 1073 { 1074 struct nvme_subsystem *subsys = 1075 container_of(dev, struct nvme_subsystem, dev); 1076 int policy; 1077 1078 policy = nvme_iopolicy_parse(buf); 1079 if (policy < 0) 1080 return policy; 1081 1082 nvme_subsys_iopolicy_update(subsys, policy); 1083 return count; 1084 } 1085 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR, 1086 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store); 1087 1088 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr, 1089 char *buf) 1090 { 1091 return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid); 1092 } 1093 DEVICE_ATTR_RO(ana_grpid); 1094 1095 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr, 1096 char *buf) 1097 { 1098 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1099 1100 return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]); 1101 } 1102 DEVICE_ATTR_RO(ana_state); 1103 1104 static ssize_t queue_depth_show(struct device *dev, 1105 struct device_attribute *attr, char *buf) 1106 { 1107 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1108 1109 if (ns->head->subsys->iopolicy != NVME_IOPOLICY_QD) 1110 return 0; 1111 1112 return sysfs_emit(buf, "%d\n", atomic_read(&ns->ctrl->nr_active)); 1113 } 1114 DEVICE_ATTR_RO(queue_depth); 1115 1116 static ssize_t numa_nodes_show(struct device *dev, struct device_attribute *attr, 1117 char *buf) 1118 { 1119 int node, srcu_idx; 1120 nodemask_t numa_nodes; 1121 struct nvme_ns *current_ns; 1122 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1123 struct nvme_ns_head *head = ns->head; 1124 1125 if (head->subsys->iopolicy != NVME_IOPOLICY_NUMA) 1126 return 0; 1127 1128 nodes_clear(numa_nodes); 1129 1130 srcu_idx = srcu_read_lock(&head->srcu); 1131 for_each_node(node) { 1132 current_ns = srcu_dereference(head->current_path[node], 1133 &head->srcu); 1134 if (ns == current_ns) 1135 node_set(node, numa_nodes); 1136 } 1137 srcu_read_unlock(&head->srcu, srcu_idx); 1138 1139 return sysfs_emit(buf, "%*pbl\n", nodemask_pr_args(&numa_nodes)); 1140 } 1141 DEVICE_ATTR_RO(numa_nodes); 1142 1143 static ssize_t delayed_removal_secs_show(struct device *dev, 1144 struct device_attribute *attr, char *buf) 1145 { 1146 struct gendisk *disk = dev_to_disk(dev); 1147 struct nvme_ns_head *head = disk->private_data; 1148 int ret; 1149 1150 mutex_lock(&head->subsys->lock); 1151 ret = sysfs_emit(buf, "%u\n", head->delayed_removal_secs); 1152 mutex_unlock(&head->subsys->lock); 1153 return ret; 1154 } 1155 1156 static ssize_t delayed_removal_secs_store(struct device *dev, 1157 struct device_attribute *attr, const char *buf, size_t count) 1158 { 1159 struct gendisk *disk = dev_to_disk(dev); 1160 struct nvme_ns_head *head = disk->private_data; 1161 unsigned int sec; 1162 int ret; 1163 1164 ret = kstrtouint(buf, 0, &sec); 1165 if (ret < 0) 1166 return ret; 1167 1168 mutex_lock(&head->subsys->lock); 1169 head->delayed_removal_secs = sec; 1170 if (sec) 1171 set_bit(NVME_NSHEAD_QUEUE_IF_NO_PATH, &head->flags); 1172 else 1173 clear_bit(NVME_NSHEAD_QUEUE_IF_NO_PATH, &head->flags); 1174 mutex_unlock(&head->subsys->lock); 1175 /* 1176 * Ensure that update to NVME_NSHEAD_QUEUE_IF_NO_PATH is seen 1177 * by its reader. 1178 */ 1179 synchronize_srcu(&head->srcu); 1180 1181 return count; 1182 } 1183 1184 DEVICE_ATTR_RW(delayed_removal_secs); 1185 1186 static ssize_t multipath_failover_count_show(struct device *dev, 1187 struct device_attribute *attr, char *buf) 1188 { 1189 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1190 1191 return sysfs_emit(buf, "%lu\n", atomic_long_read(&ns->failover)); 1192 } 1193 1194 static ssize_t multipath_failover_count_store(struct device *dev, 1195 struct device_attribute *attr, const char *buf, size_t count) 1196 { 1197 unsigned long failover; 1198 int ret; 1199 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 1200 1201 ret = kstrtoul(buf, 0, &failover); 1202 if (ret) 1203 return -EINVAL; 1204 1205 atomic_long_set(&ns->failover, failover); 1206 1207 return count; 1208 } 1209 1210 DEVICE_ATTR_RW(multipath_failover_count); 1211 1212 static ssize_t io_requeue_no_usable_path_count_show(struct device *dev, 1213 struct device_attribute *attr, char *buf) 1214 { 1215 struct gendisk *disk = dev_to_disk(dev); 1216 struct nvme_ns_head *head = disk->private_data; 1217 1218 return sysfs_emit(buf, "%lu\n", 1219 atomic_long_read(&head->io_requeue_no_usable_path_count)); 1220 } 1221 1222 static ssize_t io_requeue_no_usable_path_count_store(struct device *dev, 1223 struct device_attribute *attr, const char *buf, size_t count) 1224 { 1225 int err; 1226 unsigned long requeue_cnt; 1227 struct gendisk *disk = dev_to_disk(dev); 1228 struct nvme_ns_head *head = disk->private_data; 1229 1230 err = kstrtoul(buf, 0, &requeue_cnt); 1231 if (err) 1232 return -EINVAL; 1233 1234 atomic_long_set(&head->io_requeue_no_usable_path_count, requeue_cnt); 1235 1236 return count; 1237 } 1238 1239 DEVICE_ATTR_RW(io_requeue_no_usable_path_count); 1240 1241 static ssize_t io_fail_no_available_path_count_show(struct device *dev, 1242 struct device_attribute *attr, char *buf) 1243 { 1244 struct gendisk *disk = dev_to_disk(dev); 1245 struct nvme_ns_head *head = disk->private_data; 1246 1247 return sysfs_emit(buf, "%lu\n", 1248 atomic_long_read(&head->io_fail_no_available_path_count)); 1249 } 1250 1251 static ssize_t io_fail_no_available_path_count_store(struct device *dev, 1252 struct device_attribute *attr, const char *buf, size_t count) 1253 { 1254 int err; 1255 unsigned long fail_cnt; 1256 struct gendisk *disk = dev_to_disk(dev); 1257 struct nvme_ns_head *head = disk->private_data; 1258 1259 err = kstrtoul(buf, 0, &fail_cnt); 1260 if (err) 1261 return -EINVAL; 1262 1263 atomic_long_set(&head->io_fail_no_available_path_count, fail_cnt); 1264 1265 return count; 1266 } 1267 1268 DEVICE_ATTR_RW(io_fail_no_available_path_count); 1269 1270 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl, 1271 struct nvme_ana_group_desc *desc, void *data) 1272 { 1273 struct nvme_ana_group_desc *dst = data; 1274 1275 if (desc->grpid != dst->grpid) 1276 return 0; 1277 1278 *dst = *desc; 1279 return -ENXIO; /* just break out of the loop */ 1280 } 1281 1282 void nvme_mpath_add_sysfs_link(struct nvme_ns_head *head) 1283 { 1284 struct device *target; 1285 int rc, srcu_idx; 1286 struct nvme_ns *ns; 1287 struct kobject *kobj; 1288 1289 /* 1290 * Ensure head disk node is already added otherwise we may get invalid 1291 * kobj for head disk node 1292 */ 1293 if (!test_bit(GD_ADDED, &head->disk->state)) 1294 return; 1295 1296 kobj = &disk_to_dev(head->disk)->kobj; 1297 1298 /* 1299 * loop through each ns chained through the head->list and create the 1300 * sysfs link from head node to the ns path node 1301 */ 1302 srcu_idx = srcu_read_lock(&head->srcu); 1303 1304 list_for_each_entry_srcu(ns, &head->list, siblings, 1305 srcu_read_lock_held(&head->srcu)) { 1306 /* 1307 * Ensure that ns path disk node is already added otherwise we 1308 * may get invalid kobj name for target 1309 */ 1310 if (!test_bit(GD_ADDED, &ns->disk->state)) 1311 continue; 1312 1313 /* 1314 * Avoid creating link if it already exists for the given path. 1315 * When path ana state transitions from optimized to non- 1316 * optimized or vice-versa, the nvme_mpath_set_live() is 1317 * invoked which in truns call this function. Now if the sysfs 1318 * link already exists for the given path and we attempt to re- 1319 * create the link then sysfs code would warn about it loudly. 1320 * So we evaluate NVME_NS_SYSFS_ATTR_LINK flag here to ensure 1321 * that we're not creating duplicate link. 1322 * The test_and_set_bit() is used because it is protecting 1323 * against multiple nvme paths being simultaneously added. 1324 */ 1325 if (test_and_set_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags)) 1326 continue; 1327 1328 target = disk_to_dev(ns->disk); 1329 /* 1330 * Create sysfs link from head gendisk kobject @kobj to the 1331 * ns path gendisk kobject @target->kobj. 1332 */ 1333 rc = sysfs_add_link_to_group(kobj, nvme_ns_mpath_attr_group.name, 1334 &target->kobj, dev_name(target)); 1335 if (unlikely(rc)) { 1336 dev_err(disk_to_dev(ns->head->disk), 1337 "failed to create link to %s\n", 1338 dev_name(target)); 1339 clear_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags); 1340 } 1341 } 1342 1343 srcu_read_unlock(&head->srcu, srcu_idx); 1344 } 1345 1346 void nvme_mpath_remove_sysfs_link(struct nvme_ns *ns) 1347 { 1348 struct device *target; 1349 struct kobject *kobj; 1350 1351 if (!test_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags)) 1352 return; 1353 1354 target = disk_to_dev(ns->disk); 1355 kobj = &disk_to_dev(ns->head->disk)->kobj; 1356 sysfs_remove_link_from_group(kobj, nvme_ns_mpath_attr_group.name, 1357 dev_name(target)); 1358 clear_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags); 1359 } 1360 1361 void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid) 1362 { 1363 if (nvme_ctrl_use_ana(ns->ctrl)) { 1364 struct nvme_ana_group_desc desc = { 1365 .grpid = anagrpid, 1366 .state = 0, 1367 }; 1368 1369 mutex_lock(&ns->ctrl->ana_lock); 1370 ns->ana_grpid = le32_to_cpu(anagrpid); 1371 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc); 1372 mutex_unlock(&ns->ctrl->ana_lock); 1373 if (desc.state) { 1374 /* found the group desc: update */ 1375 nvme_update_ns_ana_state(&desc, ns); 1376 } else { 1377 /* group desc not found: trigger a re-read */ 1378 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 1379 queue_work(nvme_wq, &ns->ctrl->ana_work); 1380 } 1381 } else { 1382 ns->ana_state = NVME_ANA_OPTIMIZED; 1383 nvme_mpath_set_live(ns); 1384 } 1385 1386 } 1387 1388 void nvme_mpath_remove_disk(struct nvme_ns_head *head) 1389 { 1390 bool remove = false; 1391 1392 if (!head->disk) 1393 return; 1394 1395 mutex_lock(&head->subsys->lock); 1396 /* 1397 * We are called when all paths have been removed, and at that point 1398 * head->list is expected to be empty. However, nvme_ns_remove() and 1399 * nvme_init_ns_head() can run concurrently and so if head->delayed_ 1400 * removal_secs is configured, it is possible that by the time we reach 1401 * this point, head->list may no longer be empty. Therefore, we recheck 1402 * head->list here. If it is no longer empty then we skip enqueuing the 1403 * delayed head removal work. 1404 */ 1405 if (!list_empty(&head->list)) 1406 goto out; 1407 1408 /* 1409 * Ensure that no one could remove this module while the head 1410 * remove work is pending. 1411 */ 1412 if (head->delayed_removal_secs && try_module_get(THIS_MODULE)) { 1413 mod_delayed_work(nvme_wq, &head->remove_work, 1414 head->delayed_removal_secs * HZ); 1415 } else { 1416 list_del_init(&head->entry); 1417 remove = true; 1418 } 1419 out: 1420 mutex_unlock(&head->subsys->lock); 1421 if (remove) 1422 nvme_remove_head(head); 1423 } 1424 1425 void nvme_mpath_put_disk(struct nvme_ns_head *head) 1426 { 1427 if (!head->disk) 1428 return; 1429 /* make sure all pending bios are cleaned up */ 1430 kblockd_schedule_work(&head->requeue_work); 1431 flush_work(&head->requeue_work); 1432 flush_work(&head->partition_scan_work); 1433 put_disk(head->disk); 1434 } 1435 1436 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl) 1437 { 1438 mutex_init(&ctrl->ana_lock); 1439 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0); 1440 INIT_WORK(&ctrl->ana_work, nvme_ana_work); 1441 } 1442 1443 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 1444 { 1445 size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT; 1446 size_t ana_log_size; 1447 int error = 0; 1448 1449 /* check if multipath is enabled and we have the capability */ 1450 if (!multipath || !ctrl->subsys || 1451 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)) 1452 return 0; 1453 1454 /* initialize this in the identify path to cover controller resets */ 1455 atomic_set(&ctrl->nr_active, 0); 1456 1457 if (!ctrl->max_namespaces || 1458 ctrl->max_namespaces > le32_to_cpu(id->nn)) { 1459 dev_err(ctrl->device, 1460 "Invalid MNAN value %u\n", ctrl->max_namespaces); 1461 return -EINVAL; 1462 } 1463 1464 ctrl->anacap = id->anacap; 1465 ctrl->anatt = id->anatt; 1466 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid); 1467 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax); 1468 1469 ana_log_size = sizeof(struct nvme_ana_rsp_hdr) + 1470 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) + 1471 ctrl->max_namespaces * sizeof(__le32); 1472 if (ana_log_size > max_transfer_size) { 1473 dev_err(ctrl->device, 1474 "ANA log page size (%zd) larger than MDTS (%zd).\n", 1475 ana_log_size, max_transfer_size); 1476 dev_err(ctrl->device, "disabling ANA support.\n"); 1477 goto out_uninit; 1478 } 1479 if (ana_log_size > ctrl->ana_log_size) { 1480 nvme_mpath_stop(ctrl); 1481 nvme_mpath_uninit(ctrl); 1482 ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL); 1483 if (!ctrl->ana_log_buf) 1484 return -ENOMEM; 1485 } 1486 ctrl->ana_log_size = ana_log_size; 1487 error = nvme_read_ana_log(ctrl); 1488 if (error) 1489 goto out_uninit; 1490 return 0; 1491 1492 out_uninit: 1493 nvme_mpath_uninit(ctrl); 1494 return error; 1495 } 1496 1497 void nvme_mpath_uninit(struct nvme_ctrl *ctrl) 1498 { 1499 kvfree(ctrl->ana_log_buf); 1500 ctrl->ana_log_buf = NULL; 1501 ctrl->ana_log_size = 0; 1502 } 1503