blob: d7b664ae5923e1217a493d68f1dac96c8a3cce4c [file] [log] [blame]
Christoph Hellwig32acab32017-11-02 12:59:30 +01001/*
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
14#include <linux/moduleparam.h>
15#include "nvme.h"
16
17static bool multipath = true;
Keith Busch5cadde82018-04-26 14:24:29 -060018module_param(multipath, bool, 0444);
Christoph Hellwig32acab32017-11-02 12:59:30 +010019MODULE_PARM_DESC(multipath,
20 "turn on native support for multiple controllers per subsystem");
21
Keith Buscha785dbc2018-04-26 14:22:41 -060022/*
23 * If multipathing is enabled we need to always use the subsystem instance
24 * number for numbering our devices to avoid conflicts between subsystems that
25 * have multiple controllers and thus use the multipath-aware subsystem node
26 * and those that have a single controller and use the controller node
27 * directly.
28 */
29void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
30 struct nvme_ctrl *ctrl, int *flags)
31{
32 if (!multipath) {
33 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
34 } else if (ns->head->disk) {
35 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
36 ctrl->cntlid, ns->head->instance);
37 *flags = GENHD_FL_HIDDEN;
38 } else {
39 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
40 ns->head->instance);
41 }
42}
43
Christoph Hellwig32acab32017-11-02 12:59:30 +010044void nvme_failover_req(struct request *req)
45{
46 struct nvme_ns *ns = req->q->queuedata;
47 unsigned long flags;
48
49 spin_lock_irqsave(&ns->head->requeue_lock, flags);
50 blk_steal_bios(&ns->head->requeue_list, req);
51 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
52 blk_mq_end_request(req, 0);
53
54 nvme_reset_ctrl(ns->ctrl);
55 kblockd_schedule_work(&ns->head->requeue_work);
56}
57
Keith Busch908e4562018-01-09 12:04:15 -070058bool nvme_req_needs_failover(struct request *req, blk_status_t error)
Christoph Hellwig32acab32017-11-02 12:59:30 +010059{
60 if (!(req->cmd_flags & REQ_NVME_MPATH))
61 return false;
Keith Busche1f425e2018-01-09 12:04:17 -070062 return blk_path_error(error);
Christoph Hellwig32acab32017-11-02 12:59:30 +010063}
64
65void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
66{
67 struct nvme_ns *ns;
68
Jianchao Wang765cc0312018-02-12 20:54:46 +080069 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwig32acab32017-11-02 12:59:30 +010070 list_for_each_entry(ns, &ctrl->namespaces, list) {
71 if (ns->head->disk)
72 kblockd_schedule_work(&ns->head->requeue_work);
73 }
Jianchao Wang765cc0312018-02-12 20:54:46 +080074 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwig32acab32017-11-02 12:59:30 +010075}
76
77static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head)
78{
79 struct nvme_ns *ns;
80
81 list_for_each_entry_rcu(ns, &head->list, siblings) {
82 if (ns->ctrl->state == NVME_CTRL_LIVE) {
83 rcu_assign_pointer(head->current_path, ns);
84 return ns;
85 }
86 }
87
88 return NULL;
89}
90
91inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
92{
93 struct nvme_ns *ns = srcu_dereference(head->current_path, &head->srcu);
94
95 if (unlikely(!ns || ns->ctrl->state != NVME_CTRL_LIVE))
96 ns = __nvme_find_path(head);
97 return ns;
98}
99
100static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
101 struct bio *bio)
102{
103 struct nvme_ns_head *head = q->queuedata;
104 struct device *dev = disk_to_dev(head->disk);
105 struct nvme_ns *ns;
106 blk_qc_t ret = BLK_QC_T_NONE;
107 int srcu_idx;
108
109 srcu_idx = srcu_read_lock(&head->srcu);
110 ns = nvme_find_path(head);
111 if (likely(ns)) {
112 bio->bi_disk = ns->disk;
113 bio->bi_opf |= REQ_NVME_MPATH;
114 ret = direct_make_request(bio);
115 } else if (!list_empty_careful(&head->list)) {
Colin Ian King89c4aff2017-11-14 14:26:27 +0000116 dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
Christoph Hellwig32acab32017-11-02 12:59:30 +0100117
118 spin_lock_irq(&head->requeue_lock);
119 bio_list_add(&head->requeue_list, bio);
120 spin_unlock_irq(&head->requeue_lock);
121 } else {
122 dev_warn_ratelimited(dev, "no path - failing I/O\n");
123
124 bio->bi_status = BLK_STS_IOERR;
125 bio_endio(bio);
126 }
127
128 srcu_read_unlock(&head->srcu, srcu_idx);
129 return ret;
130}
131
132static bool nvme_ns_head_poll(struct request_queue *q, blk_qc_t qc)
133{
134 struct nvme_ns_head *head = q->queuedata;
135 struct nvme_ns *ns;
136 bool found = false;
137 int srcu_idx;
138
139 srcu_idx = srcu_read_lock(&head->srcu);
140 ns = srcu_dereference(head->current_path, &head->srcu);
141 if (likely(ns && ns->ctrl->state == NVME_CTRL_LIVE))
142 found = ns->queue->poll_fn(q, qc);
143 srcu_read_unlock(&head->srcu, srcu_idx);
144 return found;
145}
146
147static void nvme_requeue_work(struct work_struct *work)
148{
149 struct nvme_ns_head *head =
150 container_of(work, struct nvme_ns_head, requeue_work);
151 struct bio *bio, *next;
152
153 spin_lock_irq(&head->requeue_lock);
154 next = bio_list_get(&head->requeue_list);
155 spin_unlock_irq(&head->requeue_lock);
156
157 while ((bio = next) != NULL) {
158 next = bio->bi_next;
159 bio->bi_next = NULL;
160
161 /*
162 * Reset disk to the mpath node and resubmit to select a new
163 * path.
164 */
165 bio->bi_disk = head->disk;
166 generic_make_request(bio);
167 }
168}
169
170int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
171{
172 struct request_queue *q;
173 bool vwc = false;
174
175 bio_list_init(&head->requeue_list);
176 spin_lock_init(&head->requeue_lock);
177 INIT_WORK(&head->requeue_work, nvme_requeue_work);
178
179 /*
180 * Add a multipath node if the subsystems supports multiple controllers.
181 * We also do this for private namespaces as the namespace sharing data could
182 * change after a rescan.
183 */
184 if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
185 return 0;
186
Bart Van Assche5ee05242018-02-28 10:15:31 -0800187 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE, NULL);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100188 if (!q)
189 goto out;
190 q->queuedata = head;
191 blk_queue_make_request(q, nvme_ns_head_make_request);
192 q->poll_fn = nvme_ns_head_poll;
Bart Van Assche8b904b52018-03-07 17:10:10 -0800193 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100194 /* set to a default value for 512 until disk is validated */
195 blk_queue_logical_block_size(q, 512);
196
197 /* we need to propagate up the VMC settings */
198 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
199 vwc = true;
200 blk_queue_write_cache(q, vwc, vwc);
201
202 head->disk = alloc_disk(0);
203 if (!head->disk)
204 goto out_cleanup_queue;
205 head->disk->fops = &nvme_ns_head_ops;
206 head->disk->private_data = head;
207 head->disk->queue = q;
208 head->disk->flags = GENHD_FL_EXT_DEVT;
209 sprintf(head->disk->disk_name, "nvme%dn%d",
210 ctrl->subsys->instance, head->instance);
211 return 0;
212
213out_cleanup_queue:
214 blk_cleanup_queue(q);
215out:
216 return -ENOMEM;
217}
218
219void nvme_mpath_add_disk(struct nvme_ns_head *head)
220{
221 if (!head->disk)
222 return;
Baegjae Sung9bd82b12018-02-28 16:06:04 +0900223
224 mutex_lock(&head->subsys->lock);
225 if (!(head->disk->flags & GENHD_FL_UP)) {
226 device_add_disk(&head->subsys->dev, head->disk);
227 if (sysfs_create_group(&disk_to_dev(head->disk)->kobj,
228 &nvme_ns_id_attr_group))
229 pr_warn("%s: failed to create sysfs group for identification\n",
230 head->disk->disk_name);
231 }
232 mutex_unlock(&head->subsys->lock);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100233}
234
235void nvme_mpath_remove_disk(struct nvme_ns_head *head)
236{
237 if (!head->disk)
238 return;
Christoph Hellwig5b85b822017-11-09 13:51:03 +0100239 sysfs_remove_group(&disk_to_dev(head->disk)->kobj,
240 &nvme_ns_id_attr_group);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100241 del_gendisk(head->disk);
242 blk_set_queue_dying(head->disk->queue);
243 /* make sure all pending bios are cleaned up */
244 kblockd_schedule_work(&head->requeue_work);
245 flush_work(&head->requeue_work);
246 blk_cleanup_queue(head->disk->queue);
247 put_disk(head->disk);
248}