multipath.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. default:
  74. /*
  75. * Reset the controller for any non-ANA error as we don't know
  76. * what caused the error.
  77. */
  78. nvme_reset_ctrl(ns->ctrl);
  79. break;
  80. }
  81. kblockd_schedule_work(&ns->head->requeue_work);
  82. }
  83. void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  84. {
  85. struct nvme_ns *ns;
  86. down_read(&ctrl->namespaces_rwsem);
  87. list_for_each_entry(ns, &ctrl->namespaces, list) {
  88. if (ns->head->disk)
  89. kblockd_schedule_work(&ns->head->requeue_work);
  90. }
  91. up_read(&ctrl->namespaces_rwsem);
  92. }
  93. static const char *nvme_ana_state_names[] = {
  94. [0] = "invalid state",
  95. [NVME_ANA_OPTIMIZED] = "optimized",
  96. [NVME_ANA_NONOPTIMIZED] = "non-optimized",
  97. [NVME_ANA_INACCESSIBLE] = "inaccessible",
  98. [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
  99. [NVME_ANA_CHANGE] = "change",
  100. };
  101. static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head)
  102. {
  103. struct nvme_ns *ns, *fallback = NULL;
  104. list_for_each_entry_rcu(ns, &head->list, siblings) {
  105. if (ns->ctrl->state != NVME_CTRL_LIVE ||
  106. test_bit(NVME_NS_ANA_PENDING, &ns->flags))
  107. continue;
  108. switch (ns->ana_state) {
  109. case NVME_ANA_OPTIMIZED:
  110. rcu_assign_pointer(head->current_path, ns);
  111. return ns;
  112. case NVME_ANA_NONOPTIMIZED:
  113. fallback = ns;
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. if (fallback)
  120. rcu_assign_pointer(head->current_path, fallback);
  121. return fallback;
  122. }
  123. static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
  124. {
  125. return ns->ctrl->state == NVME_CTRL_LIVE &&
  126. ns->ana_state == NVME_ANA_OPTIMIZED;
  127. }
  128. inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
  129. {
  130. struct nvme_ns *ns = srcu_dereference(head->current_path, &head->srcu);
  131. if (unlikely(!ns || !nvme_path_is_optimized(ns)))
  132. ns = __nvme_find_path(head);
  133. return ns;
  134. }
  135. static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
  136. struct bio *bio)
  137. {
  138. struct nvme_ns_head *head = q->queuedata;
  139. struct device *dev = disk_to_dev(head->disk);
  140. struct nvme_ns *ns;
  141. blk_qc_t ret = BLK_QC_T_NONE;
  142. int srcu_idx;
  143. srcu_idx = srcu_read_lock(&head->srcu);
  144. ns = nvme_find_path(head);
  145. if (likely(ns)) {
  146. bio->bi_disk = ns->disk;
  147. bio->bi_opf |= REQ_NVME_MPATH;
  148. trace_block_bio_remap(bio->bi_disk->queue, bio,
  149. disk_devt(ns->head->disk),
  150. bio->bi_iter.bi_sector);
  151. ret = direct_make_request(bio);
  152. } else if (!list_empty_careful(&head->list)) {
  153. dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
  154. spin_lock_irq(&head->requeue_lock);
  155. bio_list_add(&head->requeue_list, bio);
  156. spin_unlock_irq(&head->requeue_lock);
  157. } else {
  158. dev_warn_ratelimited(dev, "no path - failing I/O\n");
  159. bio->bi_status = BLK_STS_IOERR;
  160. bio_endio(bio);
  161. }
  162. srcu_read_unlock(&head->srcu, srcu_idx);
  163. return ret;
  164. }
  165. static bool nvme_ns_head_poll(struct request_queue *q, blk_qc_t qc)
  166. {
  167. struct nvme_ns_head *head = q->queuedata;
  168. struct nvme_ns *ns;
  169. bool found = false;
  170. int srcu_idx;
  171. srcu_idx = srcu_read_lock(&head->srcu);
  172. ns = srcu_dereference(head->current_path, &head->srcu);
  173. if (likely(ns && nvme_path_is_optimized(ns)))
  174. found = ns->queue->poll_fn(q, qc);
  175. srcu_read_unlock(&head->srcu, srcu_idx);
  176. return found;
  177. }
  178. static void nvme_requeue_work(struct work_struct *work)
  179. {
  180. struct nvme_ns_head *head =
  181. container_of(work, struct nvme_ns_head, requeue_work);
  182. struct bio *bio, *next;
  183. spin_lock_irq(&head->requeue_lock);
  184. next = bio_list_get(&head->requeue_list);
  185. spin_unlock_irq(&head->requeue_lock);
  186. while ((bio = next) != NULL) {
  187. next = bio->bi_next;
  188. bio->bi_next = NULL;
  189. /*
  190. * Reset disk to the mpath node and resubmit to select a new
  191. * path.
  192. */
  193. bio->bi_disk = head->disk;
  194. generic_make_request(bio);
  195. }
  196. }
  197. int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
  198. {
  199. struct request_queue *q;
  200. bool vwc = false;
  201. mutex_init(&head->lock);
  202. bio_list_init(&head->requeue_list);
  203. spin_lock_init(&head->requeue_lock);
  204. INIT_WORK(&head->requeue_work, nvme_requeue_work);
  205. /*
  206. * Add a multipath node if the subsystems supports multiple controllers.
  207. * We also do this for private namespaces as the namespace sharing data could
  208. * change after a rescan.
  209. */
  210. if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
  211. return 0;
  212. q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE, NULL);
  213. if (!q)
  214. goto out;
  215. q->queuedata = head;
  216. blk_queue_make_request(q, nvme_ns_head_make_request);
  217. q->poll_fn = nvme_ns_head_poll;
  218. blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
  219. /* set to a default value for 512 until disk is validated */
  220. blk_queue_logical_block_size(q, 512);
  221. /* we need to propagate up the VMC settings */
  222. if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
  223. vwc = true;
  224. blk_queue_write_cache(q, vwc, vwc);
  225. head->disk = alloc_disk(0);
  226. if (!head->disk)
  227. goto out_cleanup_queue;
  228. head->disk->fops = &nvme_ns_head_ops;
  229. head->disk->private_data = head;
  230. head->disk->queue = q;
  231. head->disk->flags = GENHD_FL_EXT_DEVT;
  232. sprintf(head->disk->disk_name, "nvme%dn%d",
  233. ctrl->subsys->instance, head->instance);
  234. return 0;
  235. out_cleanup_queue:
  236. blk_cleanup_queue(q);
  237. out:
  238. return -ENOMEM;
  239. }
  240. static void nvme_mpath_set_live(struct nvme_ns *ns)
  241. {
  242. struct nvme_ns_head *head = ns->head;
  243. lockdep_assert_held(&ns->head->lock);
  244. if (!head->disk)
  245. return;
  246. if (!(head->disk->flags & GENHD_FL_UP)) {
  247. device_add_disk(&head->subsys->dev, head->disk);
  248. if (sysfs_create_group(&disk_to_dev(head->disk)->kobj,
  249. &nvme_ns_id_attr_group))
  250. dev_warn(&head->subsys->dev,
  251. "failed to create id group.\n");
  252. }
  253. kblockd_schedule_work(&ns->head->requeue_work);
  254. }
  255. static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
  256. int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
  257. void *))
  258. {
  259. void *base = ctrl->ana_log_buf;
  260. size_t offset = sizeof(struct nvme_ana_rsp_hdr);
  261. int error, i;
  262. lockdep_assert_held(&ctrl->ana_lock);
  263. for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
  264. struct nvme_ana_group_desc *desc = base + offset;
  265. u32 nr_nsids = le32_to_cpu(desc->nnsids);
  266. size_t nsid_buf_size = nr_nsids * sizeof(__le32);
  267. if (WARN_ON_ONCE(desc->grpid == 0))
  268. return -EINVAL;
  269. if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
  270. return -EINVAL;
  271. if (WARN_ON_ONCE(desc->state == 0))
  272. return -EINVAL;
  273. if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
  274. return -EINVAL;
  275. offset += sizeof(*desc);
  276. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
  277. return -EINVAL;
  278. error = cb(ctrl, desc, data);
  279. if (error)
  280. return error;
  281. offset += nsid_buf_size;
  282. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
  283. return -EINVAL;
  284. }
  285. return 0;
  286. }
  287. static inline bool nvme_state_is_live(enum nvme_ana_state state)
  288. {
  289. return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
  290. }
  291. static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
  292. struct nvme_ns *ns)
  293. {
  294. enum nvme_ana_state old;
  295. mutex_lock(&ns->head->lock);
  296. old = ns->ana_state;
  297. ns->ana_grpid = le32_to_cpu(desc->grpid);
  298. ns->ana_state = desc->state;
  299. clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
  300. if (nvme_state_is_live(ns->ana_state) && !nvme_state_is_live(old))
  301. nvme_mpath_set_live(ns);
  302. mutex_unlock(&ns->head->lock);
  303. }
  304. static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
  305. struct nvme_ana_group_desc *desc, void *data)
  306. {
  307. u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
  308. unsigned *nr_change_groups = data;
  309. struct nvme_ns *ns;
  310. dev_info(ctrl->device, "ANA group %d: %s.\n",
  311. le32_to_cpu(desc->grpid),
  312. nvme_ana_state_names[desc->state]);
  313. if (desc->state == NVME_ANA_CHANGE)
  314. (*nr_change_groups)++;
  315. if (!nr_nsids)
  316. return 0;
  317. down_write(&ctrl->namespaces_rwsem);
  318. list_for_each_entry(ns, &ctrl->namespaces, list) {
  319. if (ns->head->ns_id != le32_to_cpu(desc->nsids[n]))
  320. continue;
  321. nvme_update_ns_ana_state(desc, ns);
  322. if (++n == nr_nsids)
  323. break;
  324. }
  325. up_write(&ctrl->namespaces_rwsem);
  326. WARN_ON_ONCE(n < nr_nsids);
  327. return 0;
  328. }
  329. static int nvme_read_ana_log(struct nvme_ctrl *ctrl, bool groups_only)
  330. {
  331. u32 nr_change_groups = 0;
  332. int error;
  333. mutex_lock(&ctrl->ana_lock);
  334. error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA,
  335. groups_only ? NVME_ANA_LOG_RGO : 0,
  336. ctrl->ana_log_buf, ctrl->ana_log_size, 0);
  337. if (error) {
  338. dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
  339. goto out_unlock;
  340. }
  341. error = nvme_parse_ana_log(ctrl, &nr_change_groups,
  342. nvme_update_ana_state);
  343. if (error)
  344. goto out_unlock;
  345. /*
  346. * In theory we should have an ANATT timer per group as they might enter
  347. * the change state at different times. But that is a lot of overhead
  348. * just to protect against a target that keeps entering new changes
  349. * states while never finishing previous ones. But we'll still
  350. * eventually time out once all groups are in change state, so this
  351. * isn't a big deal.
  352. *
  353. * We also double the ANATT value to provide some slack for transports
  354. * or AEN processing overhead.
  355. */
  356. if (nr_change_groups)
  357. mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
  358. else
  359. del_timer_sync(&ctrl->anatt_timer);
  360. out_unlock:
  361. mutex_unlock(&ctrl->ana_lock);
  362. return error;
  363. }
  364. static void nvme_ana_work(struct work_struct *work)
  365. {
  366. struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
  367. nvme_read_ana_log(ctrl, false);
  368. }
  369. static void nvme_anatt_timeout(struct timer_list *t)
  370. {
  371. struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
  372. dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
  373. nvme_reset_ctrl(ctrl);
  374. }
  375. void nvme_mpath_stop(struct nvme_ctrl *ctrl)
  376. {
  377. if (!nvme_ctrl_use_ana(ctrl))
  378. return;
  379. del_timer_sync(&ctrl->anatt_timer);
  380. cancel_work_sync(&ctrl->ana_work);
  381. }
  382. static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
  383. char *buf)
  384. {
  385. return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
  386. }
  387. DEVICE_ATTR_RO(ana_grpid);
  388. static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
  389. char *buf)
  390. {
  391. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  392. return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
  393. }
  394. DEVICE_ATTR_RO(ana_state);
  395. static int nvme_set_ns_ana_state(struct nvme_ctrl *ctrl,
  396. struct nvme_ana_group_desc *desc, void *data)
  397. {
  398. struct nvme_ns *ns = data;
  399. if (ns->ana_grpid == le32_to_cpu(desc->grpid)) {
  400. nvme_update_ns_ana_state(desc, ns);
  401. return -ENXIO; /* just break out of the loop */
  402. }
  403. return 0;
  404. }
  405. void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
  406. {
  407. if (nvme_ctrl_use_ana(ns->ctrl)) {
  408. mutex_lock(&ns->ctrl->ana_lock);
  409. ns->ana_grpid = le32_to_cpu(id->anagrpid);
  410. nvme_parse_ana_log(ns->ctrl, ns, nvme_set_ns_ana_state);
  411. mutex_unlock(&ns->ctrl->ana_lock);
  412. } else {
  413. mutex_lock(&ns->head->lock);
  414. ns->ana_state = NVME_ANA_OPTIMIZED;
  415. nvme_mpath_set_live(ns);
  416. mutex_unlock(&ns->head->lock);
  417. }
  418. }
  419. void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  420. {
  421. if (!head->disk)
  422. return;
  423. if (head->disk->flags & GENHD_FL_UP) {
  424. sysfs_remove_group(&disk_to_dev(head->disk)->kobj,
  425. &nvme_ns_id_attr_group);
  426. del_gendisk(head->disk);
  427. }
  428. blk_set_queue_dying(head->disk->queue);
  429. /* make sure all pending bios are cleaned up */
  430. kblockd_schedule_work(&head->requeue_work);
  431. flush_work(&head->requeue_work);
  432. blk_cleanup_queue(head->disk->queue);
  433. put_disk(head->disk);
  434. }
  435. int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
  436. {
  437. int error;
  438. if (!nvme_ctrl_use_ana(ctrl))
  439. return 0;
  440. ctrl->anacap = id->anacap;
  441. ctrl->anatt = id->anatt;
  442. ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
  443. ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
  444. mutex_init(&ctrl->ana_lock);
  445. timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
  446. ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
  447. ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
  448. if (!(ctrl->anacap & (1 << 6)))
  449. ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
  450. if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
  451. dev_err(ctrl->device,
  452. "ANA log page size (%zd) larger than MDTS (%d).\n",
  453. ctrl->ana_log_size,
  454. ctrl->max_hw_sectors << SECTOR_SHIFT);
  455. dev_err(ctrl->device, "disabling ANA support.\n");
  456. return 0;
  457. }
  458. INIT_WORK(&ctrl->ana_work, nvme_ana_work);
  459. ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
  460. if (!ctrl->ana_log_buf) {
  461. error = -ENOMEM;
  462. goto out;
  463. }
  464. error = nvme_read_ana_log(ctrl, true);
  465. if (error)
  466. goto out_free_ana_log_buf;
  467. return 0;
  468. out_free_ana_log_buf:
  469. kfree(ctrl->ana_log_buf);
  470. out:
  471. return error;
  472. }
  473. void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
  474. {
  475. kfree(ctrl->ana_log_buf);
  476. }