multipath.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. /* we need to propagate up the VMC settings */
  252. if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
  253. vwc = true;
  254. blk_queue_write_cache(q, vwc, vwc);
  255. head->disk = alloc_disk(0);
  256. if (!head->disk)
  257. goto out_cleanup_queue;
  258. head->disk->fops = &nvme_ns_head_ops;
  259. head->disk->private_data = head;
  260. head->disk->queue = q;
  261. head->disk->flags = GENHD_FL_EXT_DEVT;
  262. sprintf(head->disk->disk_name, "nvme%dn%d",
  263. ctrl->subsys->instance, head->instance);
  264. return 0;
  265. out_cleanup_queue:
  266. blk_cleanup_queue(q);
  267. out:
  268. return -ENOMEM;
  269. }
  270. static void nvme_mpath_set_live(struct nvme_ns *ns)
  271. {
  272. struct nvme_ns_head *head = ns->head;
  273. lockdep_assert_held(&ns->head->lock);
  274. if (!head->disk)
  275. return;
  276. if (!(head->disk->flags & GENHD_FL_UP))
  277. device_add_disk(&head->subsys->dev, head->disk,
  278. nvme_ns_id_attr_groups);
  279. kblockd_schedule_work(&ns->head->requeue_work);
  280. }
  281. static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
  282. int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
  283. void *))
  284. {
  285. void *base = ctrl->ana_log_buf;
  286. size_t offset = sizeof(struct nvme_ana_rsp_hdr);
  287. int error, i;
  288. lockdep_assert_held(&ctrl->ana_lock);
  289. for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
  290. struct nvme_ana_group_desc *desc = base + offset;
  291. u32 nr_nsids = le32_to_cpu(desc->nnsids);
  292. size_t nsid_buf_size = nr_nsids * sizeof(__le32);
  293. if (WARN_ON_ONCE(desc->grpid == 0))
  294. return -EINVAL;
  295. if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
  296. return -EINVAL;
  297. if (WARN_ON_ONCE(desc->state == 0))
  298. return -EINVAL;
  299. if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
  300. return -EINVAL;
  301. offset += sizeof(*desc);
  302. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
  303. return -EINVAL;
  304. error = cb(ctrl, desc, data);
  305. if (error)
  306. return error;
  307. offset += nsid_buf_size;
  308. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
  309. return -EINVAL;
  310. }
  311. return 0;
  312. }
  313. static inline bool nvme_state_is_live(enum nvme_ana_state state)
  314. {
  315. return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
  316. }
  317. static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
  318. struct nvme_ns *ns)
  319. {
  320. enum nvme_ana_state old;
  321. mutex_lock(&ns->head->lock);
  322. old = ns->ana_state;
  323. ns->ana_grpid = le32_to_cpu(desc->grpid);
  324. ns->ana_state = desc->state;
  325. clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
  326. if (nvme_state_is_live(ns->ana_state) && !nvme_state_is_live(old))
  327. nvme_mpath_set_live(ns);
  328. mutex_unlock(&ns->head->lock);
  329. }
  330. static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
  331. struct nvme_ana_group_desc *desc, void *data)
  332. {
  333. u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
  334. unsigned *nr_change_groups = data;
  335. struct nvme_ns *ns;
  336. dev_info(ctrl->device, "ANA group %d: %s.\n",
  337. le32_to_cpu(desc->grpid),
  338. nvme_ana_state_names[desc->state]);
  339. if (desc->state == NVME_ANA_CHANGE)
  340. (*nr_change_groups)++;
  341. if (!nr_nsids)
  342. return 0;
  343. down_write(&ctrl->namespaces_rwsem);
  344. list_for_each_entry(ns, &ctrl->namespaces, list) {
  345. if (ns->head->ns_id != le32_to_cpu(desc->nsids[n]))
  346. continue;
  347. nvme_update_ns_ana_state(desc, ns);
  348. if (++n == nr_nsids)
  349. break;
  350. }
  351. up_write(&ctrl->namespaces_rwsem);
  352. WARN_ON_ONCE(n < nr_nsids);
  353. return 0;
  354. }
  355. static int nvme_read_ana_log(struct nvme_ctrl *ctrl, bool groups_only)
  356. {
  357. u32 nr_change_groups = 0;
  358. int error;
  359. mutex_lock(&ctrl->ana_lock);
  360. error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA,
  361. groups_only ? NVME_ANA_LOG_RGO : 0,
  362. ctrl->ana_log_buf, ctrl->ana_log_size, 0);
  363. if (error) {
  364. dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
  365. goto out_unlock;
  366. }
  367. error = nvme_parse_ana_log(ctrl, &nr_change_groups,
  368. nvme_update_ana_state);
  369. if (error)
  370. goto out_unlock;
  371. /*
  372. * In theory we should have an ANATT timer per group as they might enter
  373. * the change state at different times. But that is a lot of overhead
  374. * just to protect against a target that keeps entering new changes
  375. * states while never finishing previous ones. But we'll still
  376. * eventually time out once all groups are in change state, so this
  377. * isn't a big deal.
  378. *
  379. * We also double the ANATT value to provide some slack for transports
  380. * or AEN processing overhead.
  381. */
  382. if (nr_change_groups)
  383. mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
  384. else
  385. del_timer_sync(&ctrl->anatt_timer);
  386. out_unlock:
  387. mutex_unlock(&ctrl->ana_lock);
  388. return error;
  389. }
  390. static void nvme_ana_work(struct work_struct *work)
  391. {
  392. struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
  393. nvme_read_ana_log(ctrl, false);
  394. }
  395. static void nvme_anatt_timeout(struct timer_list *t)
  396. {
  397. struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
  398. dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
  399. nvme_reset_ctrl(ctrl);
  400. }
  401. void nvme_mpath_stop(struct nvme_ctrl *ctrl)
  402. {
  403. if (!nvme_ctrl_use_ana(ctrl))
  404. return;
  405. del_timer_sync(&ctrl->anatt_timer);
  406. cancel_work_sync(&ctrl->ana_work);
  407. }
  408. static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
  409. char *buf)
  410. {
  411. return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
  412. }
  413. DEVICE_ATTR_RO(ana_grpid);
  414. static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
  415. char *buf)
  416. {
  417. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  418. return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
  419. }
  420. DEVICE_ATTR_RO(ana_state);
  421. static int nvme_set_ns_ana_state(struct nvme_ctrl *ctrl,
  422. struct nvme_ana_group_desc *desc, void *data)
  423. {
  424. struct nvme_ns *ns = data;
  425. if (ns->ana_grpid == le32_to_cpu(desc->grpid)) {
  426. nvme_update_ns_ana_state(desc, ns);
  427. return -ENXIO; /* just break out of the loop */
  428. }
  429. return 0;
  430. }
  431. void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
  432. {
  433. if (nvme_ctrl_use_ana(ns->ctrl)) {
  434. mutex_lock(&ns->ctrl->ana_lock);
  435. ns->ana_grpid = le32_to_cpu(id->anagrpid);
  436. nvme_parse_ana_log(ns->ctrl, ns, nvme_set_ns_ana_state);
  437. mutex_unlock(&ns->ctrl->ana_lock);
  438. } else {
  439. mutex_lock(&ns->head->lock);
  440. ns->ana_state = NVME_ANA_OPTIMIZED;
  441. nvme_mpath_set_live(ns);
  442. mutex_unlock(&ns->head->lock);
  443. }
  444. }
  445. void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  446. {
  447. if (!head->disk)
  448. return;
  449. if (head->disk->flags & GENHD_FL_UP)
  450. del_gendisk(head->disk);
  451. blk_set_queue_dying(head->disk->queue);
  452. /* make sure all pending bios are cleaned up */
  453. kblockd_schedule_work(&head->requeue_work);
  454. flush_work(&head->requeue_work);
  455. blk_cleanup_queue(head->disk->queue);
  456. put_disk(head->disk);
  457. }
  458. int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
  459. {
  460. int error;
  461. if (!nvme_ctrl_use_ana(ctrl))
  462. return 0;
  463. ctrl->anacap = id->anacap;
  464. ctrl->anatt = id->anatt;
  465. ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
  466. ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
  467. mutex_init(&ctrl->ana_lock);
  468. timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
  469. ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
  470. ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
  471. if (!(ctrl->anacap & (1 << 6)))
  472. ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
  473. if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
  474. dev_err(ctrl->device,
  475. "ANA log page size (%zd) larger than MDTS (%d).\n",
  476. ctrl->ana_log_size,
  477. ctrl->max_hw_sectors << SECTOR_SHIFT);
  478. dev_err(ctrl->device, "disabling ANA support.\n");
  479. return 0;
  480. }
  481. INIT_WORK(&ctrl->ana_work, nvme_ana_work);
  482. ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
  483. if (!ctrl->ana_log_buf) {
  484. error = -ENOMEM;
  485. goto out;
  486. }
  487. error = nvme_read_ana_log(ctrl, true);
  488. if (error)
  489. goto out_free_ana_log_buf;
  490. return 0;
  491. out_free_ana_log_buf:
  492. kfree(ctrl->ana_log_buf);
  493. out:
  494. return error;
  495. }
  496. void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
  497. {
  498. kfree(ctrl->ana_log_buf);
  499. }