2017-11-02 19:59:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Christoph Hellwig.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/moduleparam.h>
|
2018-06-07 16:38:47 +08:00
|
|
|
#include <trace/events/block.h>
|
2017-11-02 19:59:30 +08:00
|
|
|
#include "nvme.h"
|
|
|
|
|
|
|
|
static bool multipath = true;
|
2018-04-27 04:24:29 +08:00
|
|
|
module_param(multipath, bool, 0444);
|
2017-11-02 19:59:30 +08:00
|
|
|
MODULE_PARM_DESC(multipath,
|
|
|
|
"turn on native support for multiple controllers per subsystem");
|
|
|
|
|
2018-04-27 04:22:41 +08:00
|
|
|
/*
|
|
|
|
* If multipathing is enabled we need to always use the subsystem instance
|
|
|
|
* number for numbering our devices to avoid conflicts between subsystems that
|
|
|
|
* have multiple controllers and thus use the multipath-aware subsystem node
|
|
|
|
* and those that have a single controller and use the controller node
|
|
|
|
* directly.
|
|
|
|
*/
|
|
|
|
void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
|
|
|
|
struct nvme_ctrl *ctrl, int *flags)
|
|
|
|
{
|
|
|
|
if (!multipath) {
|
|
|
|
sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
|
|
|
|
} else if (ns->head->disk) {
|
|
|
|
sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
|
|
|
|
ctrl->cntlid, ns->head->instance);
|
|
|
|
*flags = GENHD_FL_HIDDEN;
|
|
|
|
} else {
|
|
|
|
sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
|
|
|
|
ns->head->instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:59:30 +08:00
|
|
|
void nvme_failover_req(struct request *req)
|
|
|
|
{
|
|
|
|
struct nvme_ns *ns = req->q->queuedata;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&ns->head->requeue_lock, flags);
|
|
|
|
blk_steal_bios(&ns->head->requeue_list, req);
|
|
|
|
spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
|
|
|
|
blk_mq_end_request(req, 0);
|
|
|
|
|
|
|
|
nvme_reset_ctrl(ns->ctrl);
|
|
|
|
kblockd_schedule_work(&ns->head->requeue_work);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
|
|
|
|
{
|
|
|
|
struct nvme_ns *ns;
|
|
|
|
|
2018-02-12 20:54:46 +08:00
|
|
|
down_read(&ctrl->namespaces_rwsem);
|
2017-11-02 19:59:30 +08:00
|
|
|
list_for_each_entry(ns, &ctrl->namespaces, list) {
|
|
|
|
if (ns->head->disk)
|
|
|
|
kblockd_schedule_work(&ns->head->requeue_work);
|
|
|
|
}
|
2018-02-12 20:54:46 +08:00
|
|
|
up_read(&ctrl->namespaces_rwsem);
|
2017-11-02 19:59:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head)
|
|
|
|
{
|
|
|
|
struct nvme_ns *ns;
|
|
|
|
|
|
|
|
list_for_each_entry_rcu(ns, &head->list, siblings) {
|
|
|
|
if (ns->ctrl->state == NVME_CTRL_LIVE) {
|
|
|
|
rcu_assign_pointer(head->current_path, ns);
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
|
|
|
|
{
|
|
|
|
struct nvme_ns *ns = srcu_dereference(head->current_path, &head->srcu);
|
|
|
|
|
|
|
|
if (unlikely(!ns || ns->ctrl->state != NVME_CTRL_LIVE))
|
|
|
|
ns = __nvme_find_path(head);
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
static blk_qc_t nvme_ns_head_make_request(struct request_queue *q,
|
|
|
|
struct bio *bio)
|
|
|
|
{
|
|
|
|
struct nvme_ns_head *head = q->queuedata;
|
|
|
|
struct device *dev = disk_to_dev(head->disk);
|
|
|
|
struct nvme_ns *ns;
|
|
|
|
blk_qc_t ret = BLK_QC_T_NONE;
|
|
|
|
int srcu_idx;
|
|
|
|
|
|
|
|
srcu_idx = srcu_read_lock(&head->srcu);
|
|
|
|
ns = nvme_find_path(head);
|
|
|
|
if (likely(ns)) {
|
|
|
|
bio->bi_disk = ns->disk;
|
|
|
|
bio->bi_opf |= REQ_NVME_MPATH;
|
2018-06-07 16:38:47 +08:00
|
|
|
trace_block_bio_remap(bio->bi_disk->queue, bio,
|
|
|
|
disk_devt(ns->head->disk),
|
|
|
|
bio->bi_iter.bi_sector);
|
2017-11-02 19:59:30 +08:00
|
|
|
ret = direct_make_request(bio);
|
|
|
|
} else if (!list_empty_careful(&head->list)) {
|
2017-11-14 22:26:27 +08:00
|
|
|
dev_warn_ratelimited(dev, "no path available - requeuing I/O\n");
|
2017-11-02 19:59:30 +08:00
|
|
|
|
|
|
|
spin_lock_irq(&head->requeue_lock);
|
|
|
|
bio_list_add(&head->requeue_list, bio);
|
|
|
|
spin_unlock_irq(&head->requeue_lock);
|
|
|
|
} else {
|
|
|
|
dev_warn_ratelimited(dev, "no path - failing I/O\n");
|
|
|
|
|
|
|
|
bio->bi_status = BLK_STS_IOERR;
|
|
|
|
bio_endio(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
srcu_read_unlock(&head->srcu, srcu_idx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nvme_ns_head_poll(struct request_queue *q, blk_qc_t qc)
|
|
|
|
{
|
|
|
|
struct nvme_ns_head *head = q->queuedata;
|
|
|
|
struct nvme_ns *ns;
|
|
|
|
bool found = false;
|
|
|
|
int srcu_idx;
|
|
|
|
|
|
|
|
srcu_idx = srcu_read_lock(&head->srcu);
|
|
|
|
ns = srcu_dereference(head->current_path, &head->srcu);
|
|
|
|
if (likely(ns && ns->ctrl->state == NVME_CTRL_LIVE))
|
|
|
|
found = ns->queue->poll_fn(q, qc);
|
|
|
|
srcu_read_unlock(&head->srcu, srcu_idx);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_requeue_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct nvme_ns_head *head =
|
|
|
|
container_of(work, struct nvme_ns_head, requeue_work);
|
|
|
|
struct bio *bio, *next;
|
|
|
|
|
|
|
|
spin_lock_irq(&head->requeue_lock);
|
|
|
|
next = bio_list_get(&head->requeue_list);
|
|
|
|
spin_unlock_irq(&head->requeue_lock);
|
|
|
|
|
|
|
|
while ((bio = next) != NULL) {
|
|
|
|
next = bio->bi_next;
|
|
|
|
bio->bi_next = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset disk to the mpath node and resubmit to select a new
|
|
|
|
* path.
|
|
|
|
*/
|
|
|
|
bio->bi_disk = head->disk;
|
|
|
|
generic_make_request(bio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
|
|
|
|
{
|
|
|
|
struct request_queue *q;
|
|
|
|
bool vwc = false;
|
|
|
|
|
|
|
|
bio_list_init(&head->requeue_list);
|
|
|
|
spin_lock_init(&head->requeue_lock);
|
|
|
|
INIT_WORK(&head->requeue_work, nvme_requeue_work);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a multipath node if the subsystems supports multiple controllers.
|
|
|
|
* We also do this for private namespaces as the namespace sharing data could
|
|
|
|
* change after a rescan.
|
|
|
|
*/
|
|
|
|
if (!(ctrl->subsys->cmic & (1 << 1)) || !multipath)
|
|
|
|
return 0;
|
|
|
|
|
2018-03-01 02:15:31 +08:00
|
|
|
q = blk_alloc_queue_node(GFP_KERNEL, NUMA_NO_NODE, NULL);
|
2017-11-02 19:59:30 +08:00
|
|
|
if (!q)
|
|
|
|
goto out;
|
|
|
|
q->queuedata = head;
|
|
|
|
blk_queue_make_request(q, nvme_ns_head_make_request);
|
|
|
|
q->poll_fn = nvme_ns_head_poll;
|
2018-03-08 09:10:10 +08:00
|
|
|
blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
|
2017-11-02 19:59:30 +08:00
|
|
|
/* set to a default value for 512 until disk is validated */
|
|
|
|
blk_queue_logical_block_size(q, 512);
|
|
|
|
|
|
|
|
/* we need to propagate up the VMC settings */
|
|
|
|
if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
|
|
|
|
vwc = true;
|
|
|
|
blk_queue_write_cache(q, vwc, vwc);
|
|
|
|
|
|
|
|
head->disk = alloc_disk(0);
|
|
|
|
if (!head->disk)
|
|
|
|
goto out_cleanup_queue;
|
|
|
|
head->disk->fops = &nvme_ns_head_ops;
|
|
|
|
head->disk->private_data = head;
|
|
|
|
head->disk->queue = q;
|
|
|
|
head->disk->flags = GENHD_FL_EXT_DEVT;
|
|
|
|
sprintf(head->disk->disk_name, "nvme%dn%d",
|
|
|
|
ctrl->subsys->instance, head->instance);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_cleanup_queue:
|
|
|
|
blk_cleanup_queue(q);
|
|
|
|
out:
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nvme_mpath_add_disk(struct nvme_ns_head *head)
|
|
|
|
{
|
|
|
|
if (!head->disk)
|
|
|
|
return;
|
2018-02-28 15:06:04 +08:00
|
|
|
|
|
|
|
mutex_lock(&head->subsys->lock);
|
|
|
|
if (!(head->disk->flags & GENHD_FL_UP)) {
|
|
|
|
device_add_disk(&head->subsys->dev, head->disk);
|
|
|
|
if (sysfs_create_group(&disk_to_dev(head->disk)->kobj,
|
|
|
|
&nvme_ns_id_attr_group))
|
|
|
|
pr_warn("%s: failed to create sysfs group for identification\n",
|
|
|
|
head->disk->disk_name);
|
|
|
|
}
|
|
|
|
mutex_unlock(&head->subsys->lock);
|
2017-11-02 19:59:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void nvme_mpath_remove_disk(struct nvme_ns_head *head)
|
|
|
|
{
|
|
|
|
if (!head->disk)
|
|
|
|
return;
|
2017-11-09 20:51:03 +08:00
|
|
|
sysfs_remove_group(&disk_to_dev(head->disk)->kobj,
|
|
|
|
&nvme_ns_id_attr_group);
|
2017-11-02 19:59:30 +08:00
|
|
|
del_gendisk(head->disk);
|
|
|
|
blk_set_queue_dying(head->disk->queue);
|
|
|
|
/* make sure all pending bios are cleaned up */
|
|
|
|
kblockd_schedule_work(&head->requeue_work);
|
|
|
|
flush_work(&head->requeue_work);
|
|
|
|
blk_cleanup_queue(head->disk->queue);
|
|
|
|
put_disk(head->disk);
|
|
|
|
}
|