multipath.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2017 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. /*
  21. * If multipathing is enabled we need to always use the subsystem instance
  22. * number for numbering our devices to avoid conflicts between subsystems that
  23. * have multiple controllers and thus use the multipath-aware subsystem node
  24. * and those that have a single controller and use the controller node
  25. * directly.
  26. */
  27. void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
  28. struct nvme_ctrl *ctrl, int *flags)
  29. {
  30. if (!multipath) {
  31. sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
  32. } else if (ns->head->disk) {
  33. sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
  34. ctrl->cntlid, ns->head->instance);
  35. *flags = GENHD_FL_HIDDEN;
  36. } else {
  37. sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
  38. ns->head->instance);
  39. }
  40. }
  41. void nvme_failover_req(struct request *req)
  42. {
  43. struct nvme_ns *ns = req->q->queuedata;
  44. unsigned long flags;
  45. spin_lock_irqsave(&ns->head->requeue_lock, flags);
  46. blk_steal_bios(&ns->head->requeue_list, req);
  47. spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
  48. blk_mq_end_request(req, 0);
  49. nvme_reset_ctrl(ns->ctrl);
  50. kblockd_schedule_work(&ns->head->requeue_work);
  51. }
  52. bool nvme_req_needs_failover(struct request *req, blk_status_t error)
  53. {
  54. if (!(req->cmd_flags & REQ_NVME_MPATH))
  55. return false;
  56. return blk_path_error(error);
  57. }
  58. void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  59. {
  60. struct nvme_ns *ns;
  61. down_read(&ctrl->namespaces_rwsem);
  62. list_for_each_entry(ns, &ctrl->namespaces, list) {
  63. if (ns->head->disk)
  64. kblockd_schedule_work(&ns->head->requeue_work);
  65. }
  66. up_read(&ctrl->namespaces_rwsem);
  67. }
  68. static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head)
  69. {
  70. struct nvme_ns *ns;
  71. list_for_each_entry_rcu(ns, &head->list, siblings) {
  72. if (ns->ctrl->state == NVME_CTRL_LIVE) {
  73. rcu_assign_pointer(head->current_path, ns);
  74. return ns;
  75. }
  76. }
  77. return NULL;
  78. }
  79. inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
  80. {
  81. struct nvme_ns *ns = srcu_dereference(head->current_path, &head->srcu);
  82. if (unlikely(!ns || ns->ctrl->state != NVME_CTRL_LIVE))
  83. ns = __nvme_find_path(head);
  84. return ns;
  85. }
  86. static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
  87. struct bio *bio)
  88. {
  89. struct nvme_ns_head *head = q->queuedata;
  90. struct device *dev = disk_to_dev(head->disk);
  91. struct nvme_ns *ns;
  92. blk_qc_t ret = BLK_QC_T_NONE;
  93. int srcu_idx;
  94. srcu_idx = srcu_read_lock(&head->srcu);
  95. ns = nvme_find_path(head);
  96. if (likely(ns)) {
  97. bio->bi_disk = ns->disk;
  98. bio->bi_opf |= REQ_NVME_MPATH;
  99. trace_block_bio_remap(bio->bi_disk->queue, bio,
  100. disk_devt(ns->head->disk),
  101. bio->bi_iter.bi_sector);
  102. ret = direct_make_request(bio);
  103. } else if (!list_empty_careful(&head->list)) {
  104. dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
  105. spin_lock_irq(&head->requeue_lock);
  106. bio_list_add(&head->requeue_list, bio);
  107. spin_unlock_irq(&head->requeue_lock);
  108. } else {
  109. dev_warn_ratelimited(dev, "no path - failing I/O\n");
  110. bio->bi_status = BLK_STS_IOERR;
  111. bio_endio(bio);
  112. }
  113. srcu_read_unlock(&head->srcu, srcu_idx);
  114. return ret;
  115. }
  116. static bool nvme_ns_head_poll(struct request_queue *q, blk_qc_t qc)
  117. {
  118. struct nvme_ns_head *head = q->queuedata;
  119. struct nvme_ns *ns;
  120. bool found = false;
  121. int srcu_idx;
  122. srcu_idx = srcu_read_lock(&head->srcu);
  123. ns = srcu_dereference(head->current_path, &head->srcu);
  124. if (likely(ns && ns->ctrl->state == NVME_CTRL_LIVE))
  125. found = ns->queue->poll_fn(q, qc);
  126. srcu_read_unlock(&head->srcu, srcu_idx);
  127. return found;
  128. }
  129. static void nvme_requeue_work(struct work_struct *work)
  130. {
  131. struct nvme_ns_head *head =
  132. container_of(work, struct nvme_ns_head, requeue_work);
  133. struct bio *bio, *next;
  134. spin_lock_irq(&head->requeue_lock);
  135. next = bio_list_get(&head->requeue_list);
  136. spin_unlock_irq(&head->requeue_lock);
  137. while ((bio = next) != NULL) {
  138. next = bio->bi_next;
  139. bio->bi_next = NULL;
  140. /*
  141. * Reset disk to the mpath node and resubmit to select a new
  142. * path.
  143. */
  144. bio->bi_disk = head->disk;
  145. generic_make_request(bio);
  146. }
  147. }
  148. int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
  149. {
  150. struct request_queue *q;
  151. bool vwc = false;
  152. bio_list_init(&head->requeue_list);
  153. spin_lock_init(&head->requeue_lock);
  154. INIT_WORK(&head->requeue_work, nvme_requeue_work);
  155. /*
  156. * Add a multipath node if the subsystems supports multiple controllers.
  157. * We also do this for private namespaces as the namespace sharing data could
  158. * change after a rescan.
  159. */
  160. if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
  161. return 0;
  162. q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE, NULL);
  163. if (!q)
  164. goto out;
  165. q->queuedata = head;
  166. blk_queue_make_request(q, nvme_ns_head_make_request);
  167. q->poll_fn = nvme_ns_head_poll;
  168. blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
  169. /* set to a default value for 512 until disk is validated */
  170. blk_queue_logical_block_size(q, 512);
  171. /* we need to propagate up the VMC settings */
  172. if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
  173. vwc = true;
  174. blk_queue_write_cache(q, vwc, vwc);
  175. head->disk = alloc_disk(0);
  176. if (!head->disk)
  177. goto out_cleanup_queue;
  178. head->disk->fops = &nvme_ns_head_ops;
  179. head->disk->private_data = head;
  180. head->disk->queue = q;
  181. head->disk->flags = GENHD_FL_EXT_DEVT;
  182. sprintf(head->disk->disk_name, "nvme%dn%d",
  183. ctrl->subsys->instance, head->instance);
  184. return 0;
  185. out_cleanup_queue:
  186. blk_cleanup_queue(q);
  187. out:
  188. return -ENOMEM;
  189. }
  190. void nvme_mpath_add_disk(struct nvme_ns_head *head)
  191. {
  192. if (!head->disk)
  193. return;
  194. mutex_lock(&head->subsys->lock);
  195. if (!(head->disk->flags & GENHD_FL_UP)) {
  196. device_add_disk(&head->subsys->dev, head->disk);
  197. if (sysfs_create_group(&disk_to_dev(head->disk)->kobj,
  198. &nvme_ns_id_attr_group))
  199. pr_warn("%s: failed to create sysfs group for identification\n",
  200. head->disk->disk_name);
  201. }
  202. mutex_unlock(&head->subsys->lock);
  203. }
  204. void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  205. {
  206. if (!head->disk)
  207. return;
  208. sysfs_remove_group(&disk_to_dev(head->disk)->kobj,
  209. &nvme_ns_id_attr_group);
  210. del_gendisk(head->disk);
  211. blk_set_queue_dying(head->disk->queue);
  212. /* make sure all pending bios are cleaned up */
  213. kblockd_schedule_work(&head->requeue_work);
  214. flush_work(&head->requeue_work);
  215. blk_cleanup_queue(head->disk->queue);
  216. put_disk(head->disk);
  217. }