multipath.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright (c) 2017-2018 Christoph Hellwig.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #include <linux/moduleparam.h>
  14. #include <trace/events/block.h>
  15. #include "nvme.h"
  16. static bool multipath = true;
  17. module_param(multipath, bool, 0444);
  18. MODULE_PARM_DESC(multipath,
  19. "turn on native support for multiple controllers per subsystem");
  20. inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
  21. {
  22. return multipath && ctrl->subsys && (ctrl->subsys->cmic & (1 << 3));
  23. }
  24. /*
  25. * If multipathing is enabled we need to always use the subsystem instance
  26. * number for numbering our devices to avoid conflicts between subsystems that
  27. * have multiple controllers and thus use the multipath-aware subsystem node
  28. * and those that have a single controller and use the controller node
  29. * directly.
  30. */
  31. void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
  32. struct nvme_ctrl *ctrl, int *flags)
  33. {
  34. if (!multipath) {
  35. sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
  36. } else if (ns->head->disk) {
  37. sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
  38. ctrl->cntlid, ns->head->instance);
  39. *flags = GENHD_FL_HIDDEN;
  40. } else {
  41. sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
  42. ns->head->instance);
  43. }
  44. }
  45. void nvme_failover_req(struct request *req)
  46. {
  47. struct nvme_ns *ns = req->q->queuedata;
  48. u16 status = nvme_req(req)->status;
  49. unsigned long flags;
  50. spin_lock_irqsave(&ns->head->requeue_lock, flags);
  51. blk_steal_bios(&ns->head->requeue_list, req);
  52. spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
  53. blk_mq_end_request(req, 0);
  54. switch (status & 0x7ff) {
  55. case NVME_SC_ANA_TRANSITION:
  56. case NVME_SC_ANA_INACCESSIBLE:
  57. case NVME_SC_ANA_PERSISTENT_LOSS:
  58. /*
  59. * If we got back an ANA error we know the controller is alive,
  60. * but not ready to serve this namespaces. The spec suggests
  61. * we should update our general state here, but due to the fact
  62. * that the admin and I/O queues are not serialized that is
  63. * fundamentally racy. So instead just clear the current path,
  64. * mark the the path as pending and kick of a re-read of the ANA
  65. * log page ASAP.
  66. */
  67. nvme_mpath_clear_current_path(ns);
  68. if (ns->ctrl->ana_log_buf) {
  69. set_bit(NVME_NS_ANA_PENDING, &ns->flags);
  70. queue_work(nvme_wq, &ns->ctrl->ana_work);
  71. }
  72. break;
  73. case NVME_SC_HOST_PATH_ERROR:
  74. /*
  75. * Temporary transport disruption in talking to the controller.
  76. * Try to send on a new path.
  77. */
  78. nvme_mpath_clear_current_path(ns);
  79. break;
  80. default:
  81. /*
  82. * Reset the controller for any non-ANA error as we don't know
  83. * what caused the error.
  84. */
  85. nvme_reset_ctrl(ns->ctrl);
  86. break;
  87. }
  88. kblockd_schedule_work(&ns->head->requeue_work);
  89. }
  90. void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  91. {
  92. struct nvme_ns *ns;
  93. down_read(&ctrl->namespaces_rwsem);
  94. list_for_each_entry(ns, &ctrl->namespaces, list) {
  95. if (ns->head->disk)
  96. kblockd_schedule_work(&ns->head->requeue_work);
  97. }
  98. up_read(&ctrl->namespaces_rwsem);
  99. }
  100. static const char *nvme_ana_state_names[] = {
  101. [0] = "invalid state",
  102. [NVME_ANA_OPTIMIZED] = "optimized",
  103. [NVME_ANA_NONOPTIMIZED] = "non-optimized",
  104. [NVME_ANA_INACCESSIBLE] = "inaccessible",
  105. [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
  106. [NVME_ANA_CHANGE] = "change",
  107. };
  108. void nvme_mpath_clear_current_path(struct nvme_ns *ns)
  109. {
  110. struct nvme_ns_head *head = ns->head;
  111. int node;
  112. if (!head)
  113. return;
  114. for_each_node(node) {
  115. if (ns == rcu_access_pointer(head->current_path[node]))
  116. rcu_assign_pointer(head->current_path[node], NULL);
  117. }
  118. }
  119. static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
  120. {
  121. int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
  122. struct nvme_ns *found = NULL, *fallback = NULL, *ns;
  123. list_for_each_entry_rcu(ns, &head->list, siblings) {
  124. if (ns->ctrl->state != NVME_CTRL_LIVE ||
  125. test_bit(NVME_NS_ANA_PENDING, &ns->flags))
  126. continue;
  127. distance = node_distance(node, dev_to_node(ns->ctrl->dev));
  128. switch (ns->ana_state) {
  129. case NVME_ANA_OPTIMIZED:
  130. if (distance < found_distance) {
  131. found_distance = distance;
  132. found = ns;
  133. }
  134. break;
  135. case NVME_ANA_NONOPTIMIZED:
  136. if (distance < fallback_distance) {
  137. fallback_distance = distance;
  138. fallback = ns;
  139. }
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. if (!found)
  146. found = fallback;
  147. if (found)
  148. rcu_assign_pointer(head->current_path[node], found);
  149. return found;
  150. }
  151. static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
  152. {
  153. return ns->ctrl->state == NVME_CTRL_LIVE &&
  154. ns->ana_state == NVME_ANA_OPTIMIZED;
  155. }
  156. inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
  157. {
  158. int node = numa_node_id();
  159. struct nvme_ns *ns;
  160. ns = srcu_dereference(head->current_path[node], &head->srcu);
  161. if (unlikely(!ns || !nvme_path_is_optimized(ns)))
  162. ns = __nvme_find_path(head, node);
  163. return ns;
  164. }
  165. static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
  166. struct bio *bio)
  167. {
  168. struct nvme_ns_head *head = q->queuedata;
  169. struct device *dev = disk_to_dev(head->disk);
  170. struct nvme_ns *ns;
  171. blk_qc_t ret = BLK_QC_T_NONE;
  172. int srcu_idx;
  173. srcu_idx = srcu_read_lock(&head->srcu);
  174. ns = nvme_find_path(head);
  175. if (likely(ns)) {
  176. bio->bi_disk = ns->disk;
  177. bio->bi_opf |= REQ_NVME_MPATH;
  178. trace_block_bio_remap(bio->bi_disk->queue, bio,
  179. disk_devt(ns->head->disk),
  180. bio->bi_iter.bi_sector);
  181. ret = direct_make_request(bio);
  182. } else if (!list_empty_careful(&head->list)) {
  183. dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
  184. spin_lock_irq(&head->requeue_lock);
  185. bio_list_add(&head->requeue_list, bio);
  186. spin_unlock_irq(&head->requeue_lock);
  187. } else {
  188. dev_warn_ratelimited(dev, "no path - failing I/O\n");
  189. bio->bi_status = BLK_STS_IOERR;
  190. bio_endio(bio);
  191. }
  192. srcu_read_unlock(&head->srcu, srcu_idx);
  193. return ret;
  194. }
  195. static bool nvme_ns_head_poll(struct request_queue *q, blk_qc_t qc)
  196. {
  197. struct nvme_ns_head *head = q->queuedata;
  198. struct nvme_ns *ns;
  199. bool found = false;
  200. int srcu_idx;
  201. srcu_idx = srcu_read_lock(&head->srcu);
  202. ns = srcu_dereference(head->current_path[numa_node_id()], &head->srcu);
  203. if (likely(ns && nvme_path_is_optimized(ns)))
  204. found = ns->queue->poll_fn(q, qc);
  205. srcu_read_unlock(&head->srcu, srcu_idx);
  206. return found;
  207. }
  208. static void nvme_requeue_work(struct work_struct *work)
  209. {
  210. struct nvme_ns_head *head =
  211. container_of(work, struct nvme_ns_head, requeue_work);
  212. struct bio *bio, *next;
  213. spin_lock_irq(&head->requeue_lock);
  214. next = bio_list_get(&head->requeue_list);
  215. spin_unlock_irq(&head->requeue_lock);
  216. while ((bio = next) != NULL) {
  217. next = bio->bi_next;
  218. bio->bi_next = NULL;
  219. /*
  220. * Reset disk to the mpath node and resubmit to select a new
  221. * path.
  222. */
  223. bio->bi_disk = head->disk;
  224. generic_make_request(bio);
  225. }
  226. }
  227. int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
  228. {
  229. struct request_queue *q;
  230. bool vwc = false;
  231. mutex_init(&head->lock);
  232. bio_list_init(&head->requeue_list);
  233. spin_lock_init(&head->requeue_lock);
  234. INIT_WORK(&head->requeue_work, nvme_requeue_work);
  235. /*
  236. * Add a multipath node if the subsystems supports multiple controllers.
  237. * We also do this for private namespaces as the namespace sharing data could
  238. * change after a rescan.
  239. */
  240. if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
  241. return 0;
  242. q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE, NULL);
  243. if (!q)
  244. goto out;
  245. q->queuedata = head;
  246. blk_queue_make_request(q, nvme_ns_head_make_request);
  247. q->poll_fn = nvme_ns_head_poll;
  248. blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
  249. /* set to a default value for 512 until disk is validated */
  250. blk_queue_logical_block_size(q, 512);
  251. blk_set_stacking_limits(&q->limits);
  252. /* we need to propagate up the VMC settings */
  253. if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
  254. vwc = true;
  255. blk_queue_write_cache(q, vwc, vwc);
  256. head->disk = alloc_disk(0);
  257. if (!head->disk)
  258. goto out_cleanup_queue;
  259. head->disk->fops = &nvme_ns_head_ops;
  260. head->disk->private_data = head;
  261. head->disk->queue = q;
  262. head->disk->flags = GENHD_FL_EXT_DEVT;
  263. sprintf(head->disk->disk_name, "nvme%dn%d",
  264. ctrl->subsys->instance, head->instance);
  265. return 0;
  266. out_cleanup_queue:
  267. blk_cleanup_queue(q);
  268. out:
  269. return -ENOMEM;
  270. }
  271. static void nvme_mpath_set_live(struct nvme_ns *ns)
  272. {
  273. struct nvme_ns_head *head = ns->head;
  274. lockdep_assert_held(&ns->head->lock);
  275. if (!head->disk)
  276. return;
  277. if (!(head->disk->flags & GENHD_FL_UP))
  278. device_add_disk(&head->subsys->dev, head->disk,
  279. nvme_ns_id_attr_groups);
  280. if (nvme_path_is_optimized(ns)) {
  281. int node, srcu_idx;
  282. srcu_idx = srcu_read_lock(&head->srcu);
  283. for_each_node(node)
  284. __nvme_find_path(head, node);
  285. srcu_read_unlock(&head->srcu, srcu_idx);
  286. }
  287. kblockd_schedule_work(&ns->head->requeue_work);
  288. }
  289. static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
  290. int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
  291. void *))
  292. {
  293. void *base = ctrl->ana_log_buf;
  294. size_t offset = sizeof(struct nvme_ana_rsp_hdr);
  295. int error, i;
  296. lockdep_assert_held(&ctrl->ana_lock);
  297. for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
  298. struct nvme_ana_group_desc *desc = base + offset;
  299. u32 nr_nsids = le32_to_cpu(desc->nnsids);
  300. size_t nsid_buf_size = nr_nsids * sizeof(__le32);
  301. if (WARN_ON_ONCE(desc->grpid == 0))
  302. return -EINVAL;
  303. if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
  304. return -EINVAL;
  305. if (WARN_ON_ONCE(desc->state == 0))
  306. return -EINVAL;
  307. if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
  308. return -EINVAL;
  309. offset += sizeof(*desc);
  310. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
  311. return -EINVAL;
  312. error = cb(ctrl, desc, data);
  313. if (error)
  314. return error;
  315. offset += nsid_buf_size;
  316. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
  317. return -EINVAL;
  318. }
  319. return 0;
  320. }
  321. static inline bool nvme_state_is_live(enum nvme_ana_state state)
  322. {
  323. return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
  324. }
  325. static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
  326. struct nvme_ns *ns)
  327. {
  328. enum nvme_ana_state old;
  329. mutex_lock(&ns->head->lock);
  330. old = ns->ana_state;
  331. ns->ana_grpid = le32_to_cpu(desc->grpid);
  332. ns->ana_state = desc->state;
  333. clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
  334. if (nvme_state_is_live(ns->ana_state) && !nvme_state_is_live(old))
  335. nvme_mpath_set_live(ns);
  336. mutex_unlock(&ns->head->lock);
  337. }
  338. static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
  339. struct nvme_ana_group_desc *desc, void *data)
  340. {
  341. u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
  342. unsigned *nr_change_groups = data;
  343. struct nvme_ns *ns;
  344. dev_info(ctrl->device, "ANA group %d: %s.\n",
  345. le32_to_cpu(desc->grpid),
  346. nvme_ana_state_names[desc->state]);
  347. if (desc->state == NVME_ANA_CHANGE)
  348. (*nr_change_groups)++;
  349. if (!nr_nsids)
  350. return 0;
  351. down_write(&ctrl->namespaces_rwsem);
  352. list_for_each_entry(ns, &ctrl->namespaces, list) {
  353. if (ns->head->ns_id != le32_to_cpu(desc->nsids[n]))
  354. continue;
  355. nvme_update_ns_ana_state(desc, ns);
  356. if (++n == nr_nsids)
  357. break;
  358. }
  359. up_write(&ctrl->namespaces_rwsem);
  360. WARN_ON_ONCE(n < nr_nsids);
  361. return 0;
  362. }
  363. static int nvme_read_ana_log(struct nvme_ctrl *ctrl, bool groups_only)
  364. {
  365. u32 nr_change_groups = 0;
  366. int error;
  367. mutex_lock(&ctrl->ana_lock);
  368. error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA,
  369. groups_only ? NVME_ANA_LOG_RGO : 0,
  370. ctrl->ana_log_buf, ctrl->ana_log_size, 0);
  371. if (error) {
  372. dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
  373. goto out_unlock;
  374. }
  375. error = nvme_parse_ana_log(ctrl, &nr_change_groups,
  376. nvme_update_ana_state);
  377. if (error)
  378. goto out_unlock;
  379. /*
  380. * In theory we should have an ANATT timer per group as they might enter
  381. * the change state at different times. But that is a lot of overhead
  382. * just to protect against a target that keeps entering new changes
  383. * states while never finishing previous ones. But we'll still
  384. * eventually time out once all groups are in change state, so this
  385. * isn't a big deal.
  386. *
  387. * We also double the ANATT value to provide some slack for transports
  388. * or AEN processing overhead.
  389. */
  390. if (nr_change_groups)
  391. mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
  392. else
  393. del_timer_sync(&ctrl->anatt_timer);
  394. out_unlock:
  395. mutex_unlock(&ctrl->ana_lock);
  396. return error;
  397. }
  398. static void nvme_ana_work(struct work_struct *work)
  399. {
  400. struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
  401. nvme_read_ana_log(ctrl, false);
  402. }
  403. static void nvme_anatt_timeout(struct timer_list *t)
  404. {
  405. struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
  406. dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
  407. nvme_reset_ctrl(ctrl);
  408. }
  409. void nvme_mpath_stop(struct nvme_ctrl *ctrl)
  410. {
  411. if (!nvme_ctrl_use_ana(ctrl))
  412. return;
  413. del_timer_sync(&ctrl->anatt_timer);
  414. cancel_work_sync(&ctrl->ana_work);
  415. }
  416. static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
  417. char *buf)
  418. {
  419. return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
  420. }
  421. DEVICE_ATTR_RO(ana_grpid);
  422. static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
  423. char *buf)
  424. {
  425. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  426. return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
  427. }
  428. DEVICE_ATTR_RO(ana_state);
  429. static int nvme_set_ns_ana_state(struct nvme_ctrl *ctrl,
  430. struct nvme_ana_group_desc *desc, void *data)
  431. {
  432. struct nvme_ns *ns = data;
  433. if (ns->ana_grpid == le32_to_cpu(desc->grpid)) {
  434. nvme_update_ns_ana_state(desc, ns);
  435. return -ENXIO; /* just break out of the loop */
  436. }
  437. return 0;
  438. }
  439. void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
  440. {
  441. if (nvme_ctrl_use_ana(ns->ctrl)) {
  442. mutex_lock(&ns->ctrl->ana_lock);
  443. ns->ana_grpid = le32_to_cpu(id->anagrpid);
  444. nvme_parse_ana_log(ns->ctrl, ns, nvme_set_ns_ana_state);
  445. mutex_unlock(&ns->ctrl->ana_lock);
  446. } else {
  447. mutex_lock(&ns->head->lock);
  448. ns->ana_state = NVME_ANA_OPTIMIZED;
  449. nvme_mpath_set_live(ns);
  450. mutex_unlock(&ns->head->lock);
  451. }
  452. }
  453. void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  454. {
  455. if (!head->disk)
  456. return;
  457. if (head->disk->flags & GENHD_FL_UP)
  458. del_gendisk(head->disk);
  459. blk_set_queue_dying(head->disk->queue);
  460. /* make sure all pending bios are cleaned up */
  461. kblockd_schedule_work(&head->requeue_work);
  462. flush_work(&head->requeue_work);
  463. blk_cleanup_queue(head->disk->queue);
  464. put_disk(head->disk);
  465. }
  466. int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
  467. {
  468. int error;
  469. if (!nvme_ctrl_use_ana(ctrl))
  470. return 0;
  471. ctrl->anacap = id->anacap;
  472. ctrl->anatt = id->anatt;
  473. ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
  474. ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
  475. mutex_init(&ctrl->ana_lock);
  476. timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
  477. ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
  478. ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
  479. if (!(ctrl->anacap & (1 << 6)))
  480. ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
  481. if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
  482. dev_err(ctrl->device,
  483. "ANA log page size (%zd) larger than MDTS (%d).\n",
  484. ctrl->ana_log_size,
  485. ctrl->max_hw_sectors << SECTOR_SHIFT);
  486. dev_err(ctrl->device, "disabling ANA support.\n");
  487. return 0;
  488. }
  489. INIT_WORK(&ctrl->ana_work, nvme_ana_work);
  490. ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
  491. if (!ctrl->ana_log_buf) {
  492. error = -ENOMEM;
  493. goto out;
  494. }
  495. error = nvme_read_ana_log(ctrl, true);
  496. if (error)
  497. goto out_free_ana_log_buf;
  498. return 0;
  499. out_free_ana_log_buf:
  500. kfree(ctrl->ana_log_buf);
  501. out:
  502. return error;
  503. }
  504. void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
  505. {
  506. kfree(ctrl->ana_log_buf);
  507. }