multipath.c 6.7 KB

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