blob: ae9abb600c0f3265a259eb793cee51877cc78891 [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;
18module_param(multipath, bool, 0644);
19MODULE_PARM_DESC(multipath,
20 "turn on native support for multiple controllers per subsystem");
21
22void nvme_failover_req(struct request *req)
23{
24 struct nvme_ns *ns = req->q->queuedata;
25 unsigned long flags;
26
27 spin_lock_irqsave(&ns->head->requeue_lock, flags);
28 blk_steal_bios(&ns->head->requeue_list, req);
29 spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
30 blk_mq_end_request(req, 0);
31
32 nvme_reset_ctrl(ns->ctrl);
33 kblockd_schedule_work(&ns->head->requeue_work);
34}
35
Keith Busch908e4562018-01-09 12:04:15 -070036bool nvme_req_needs_failover(struct request *req, blk_status_t error)
Christoph Hellwig32acab32017-11-02 12:59:30 +010037{
38 if (!(req->cmd_flags & REQ_NVME_MPATH))
39 return false;
40
Keith Busch908e4562018-01-09 12:04:15 -070041 switch (error) {
42 case BLK_STS_NOTSUPP:
43 case BLK_STS_NOSPC:
44 case BLK_STS_TARGET:
45 case BLK_STS_NEXUS:
46 case BLK_STS_MEDIUM:
47 case BLK_STS_PROTECTION:
Christoph Hellwig32acab32017-11-02 12:59:30 +010048 return false;
49 }
50
51 /* Everything else could be a path failure, so should be retried */
52 return true;
53}
54
55void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
56{
57 struct nvme_ns *ns;
58
59 mutex_lock(&ctrl->namespaces_mutex);
60 list_for_each_entry(ns, &ctrl->namespaces, list) {
61 if (ns->head->disk)
62 kblockd_schedule_work(&ns->head->requeue_work);
63 }
64 mutex_unlock(&ctrl->namespaces_mutex);
65}
66
67static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head)
68{
69 struct nvme_ns *ns;
70
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
78 return NULL;
79}
80
81inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
82{
83 struct nvme_ns *ns = srcu_dereference(head->current_path, &head->srcu);
84
85 if (unlikely(!ns || ns->ctrl->state != NVME_CTRL_LIVE))
86 ns = __nvme_find_path(head);
87 return ns;
88}
89
90static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
91 struct bio *bio)
92{
93 struct nvme_ns_head *head = q->queuedata;
94 struct device *dev = disk_to_dev(head->disk);
95 struct nvme_ns *ns;
96 blk_qc_t ret = BLK_QC_T_NONE;
97 int srcu_idx;
98
99 srcu_idx = srcu_read_lock(&head->srcu);
100 ns = nvme_find_path(head);
101 if (likely(ns)) {
102 bio->bi_disk = ns->disk;
103 bio->bi_opf |= REQ_NVME_MPATH;
104 ret = direct_make_request(bio);
105 } else if (!list_empty_careful(&head->list)) {
Colin Ian King89c4aff2017-11-14 14:26:27 +0000106 dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
Christoph Hellwig32acab32017-11-02 12:59:30 +0100107
108 spin_lock_irq(&head->requeue_lock);
109 bio_list_add(&head->requeue_list, bio);
110 spin_unlock_irq(&head->requeue_lock);
111 } else {
112 dev_warn_ratelimited(dev, "no path - failing I/O\n");
113
114 bio->bi_status = BLK_STS_IOERR;
115 bio_endio(bio);
116 }
117
118 srcu_read_unlock(&head->srcu, srcu_idx);
119 return ret;
120}
121
122static bool nvme_ns_head_poll(struct request_queue *q, blk_qc_t qc)
123{
124 struct nvme_ns_head *head = q->queuedata;
125 struct nvme_ns *ns;
126 bool found = false;
127 int srcu_idx;
128
129 srcu_idx = srcu_read_lock(&head->srcu);
130 ns = srcu_dereference(head->current_path, &head->srcu);
131 if (likely(ns && ns->ctrl->state == NVME_CTRL_LIVE))
132 found = ns->queue->poll_fn(q, qc);
133 srcu_read_unlock(&head->srcu, srcu_idx);
134 return found;
135}
136
137static void nvme_requeue_work(struct work_struct *work)
138{
139 struct nvme_ns_head *head =
140 container_of(work, struct nvme_ns_head, requeue_work);
141 struct bio *bio, *next;
142
143 spin_lock_irq(&head->requeue_lock);
144 next = bio_list_get(&head->requeue_list);
145 spin_unlock_irq(&head->requeue_lock);
146
147 while ((bio = next) != NULL) {
148 next = bio->bi_next;
149 bio->bi_next = NULL;
150
151 /*
152 * Reset disk to the mpath node and resubmit to select a new
153 * path.
154 */
155 bio->bi_disk = head->disk;
156 generic_make_request(bio);
157 }
158}
159
160int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
161{
162 struct request_queue *q;
163 bool vwc = false;
164
165 bio_list_init(&head->requeue_list);
166 spin_lock_init(&head->requeue_lock);
167 INIT_WORK(&head->requeue_work, nvme_requeue_work);
168
169 /*
170 * Add a multipath node if the subsystems supports multiple controllers.
171 * We also do this for private namespaces as the namespace sharing data could
172 * change after a rescan.
173 */
174 if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
175 return 0;
176
177 q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE);
178 if (!q)
179 goto out;
180 q->queuedata = head;
181 blk_queue_make_request(q, nvme_ns_head_make_request);
182 q->poll_fn = nvme_ns_head_poll;
183 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
184 /* set to a default value for 512 until disk is validated */
185 blk_queue_logical_block_size(q, 512);
186
187 /* we need to propagate up the VMC settings */
188 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
189 vwc = true;
190 blk_queue_write_cache(q, vwc, vwc);
191
192 head->disk = alloc_disk(0);
193 if (!head->disk)
194 goto out_cleanup_queue;
195 head->disk->fops = &nvme_ns_head_ops;
196 head->disk->private_data = head;
197 head->disk->queue = q;
198 head->disk->flags = GENHD_FL_EXT_DEVT;
199 sprintf(head->disk->disk_name, "nvme%dn%d",
200 ctrl->subsys->instance, head->instance);
201 return 0;
202
203out_cleanup_queue:
204 blk_cleanup_queue(q);
205out:
206 return -ENOMEM;
207}
208
209void nvme_mpath_add_disk(struct nvme_ns_head *head)
210{
211 if (!head->disk)
212 return;
213 device_add_disk(&head->subsys->dev, head->disk);
Christoph Hellwig5b85b822017-11-09 13:51:03 +0100214 if (sysfs_create_group(&disk_to_dev(head->disk)->kobj,
215 &nvme_ns_id_attr_group))
216 pr_warn("%s: failed to create sysfs group for identification\n",
217 head->disk->disk_name);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100218}
219
Hannes Reineckee9a48032017-11-09 17:57:06 +0100220void nvme_mpath_add_disk_links(struct nvme_ns *ns)
221{
222 struct kobject *slave_disk_kobj, *holder_disk_kobj;
223
224 if (!ns->head->disk)
225 return;
226
227 slave_disk_kobj = &disk_to_dev(ns->disk)->kobj;
228 if (sysfs_create_link(ns->head->disk->slave_dir, slave_disk_kobj,
229 kobject_name(slave_disk_kobj)))
230 return;
231
232 holder_disk_kobj = &disk_to_dev(ns->head->disk)->kobj;
233 if (sysfs_create_link(ns->disk->part0.holder_dir, holder_disk_kobj,
234 kobject_name(holder_disk_kobj)))
235 sysfs_remove_link(ns->head->disk->slave_dir,
236 kobject_name(slave_disk_kobj));
237}
238
Christoph Hellwig32acab32017-11-02 12:59:30 +0100239void nvme_mpath_remove_disk(struct nvme_ns_head *head)
240{
241 if (!head->disk)
242 return;
Christoph Hellwig5b85b822017-11-09 13:51:03 +0100243 sysfs_remove_group(&disk_to_dev(head->disk)->kobj,
244 &nvme_ns_id_attr_group);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100245 del_gendisk(head->disk);
246 blk_set_queue_dying(head->disk->queue);
247 /* make sure all pending bios are cleaned up */
248 kblockd_schedule_work(&head->requeue_work);
249 flush_work(&head->requeue_work);
250 blk_cleanup_queue(head->disk->queue);
251 put_disk(head->disk);
252}
Hannes Reineckee9a48032017-11-09 17:57:06 +0100253
254void nvme_mpath_remove_disk_links(struct nvme_ns *ns)
255{
256 if (!ns->head->disk)
257 return;
258
259 sysfs_remove_link(ns->disk->part0.holder_dir,
260 kobject_name(&disk_to_dev(ns->head->disk)->kobj));
261 sysfs_remove_link(ns->head->disk->slave_dir,
262 kobject_name(&disk_to_dev(ns->disk)->kobj));
263}