multipath.c 21.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
C
Christoph Hellwig 已提交
3
 * Copyright (c) 2017-2018 Christoph Hellwig.
4 5
 */

6
#include <linux/backing-dev.h>
7
#include <linux/moduleparam.h>
8
#include <trace/events/block.h>
9 10 11
#include "nvme.h"

static bool multipath = true;
12
module_param(multipath, bool, 0444);
13 14 15
MODULE_PARM_DESC(multipath,
	"turn on native support for multiple controllers per subsystem");

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
{
	struct nvme_ns_head *h;

	lockdep_assert_held(&subsys->lock);
	list_for_each_entry(h, &subsys->nsheads, entry)
		if (h->disk)
			blk_mq_unfreeze_queue(h->disk->queue);
}

void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
{
	struct nvme_ns_head *h;

	lockdep_assert_held(&subsys->lock);
	list_for_each_entry(h, &subsys->nsheads, entry)
		if (h->disk)
			blk_mq_freeze_queue_wait(h->disk->queue);
}

void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
{
	struct nvme_ns_head *h;

	lockdep_assert_held(&subsys->lock);
	list_for_each_entry(h, &subsys->nsheads, entry)
		if (h->disk)
			blk_freeze_queue_start(h->disk->queue);
}

46 47 48 49 50 51 52
/*
 * 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.
 */
53
bool nvme_mpath_set_disk_name(struct nvme_ns *ns, char *disk_name, int *flags)
54
{
55 56 57 58 59 60
	if (!multipath)
		return false;
	if (!ns->head->disk) {
		sprintf(disk_name, "nvme%dn%d", ns->ctrl->subsys->instance,
			ns->head->instance);
		return true;
61
	}
62 63 64 65
	sprintf(disk_name, "nvme%dc%dn%d", ns->ctrl->subsys->instance,
		ns->ctrl->instance, ns->head->instance);
	*flags = GENHD_FL_HIDDEN;
	return true;
66 67
}

68
void nvme_failover_req(struct request *req)
69 70
{
	struct nvme_ns *ns = req->q->queuedata;
71
	u16 status = nvme_req(req)->status & 0x7ff;
72
	unsigned long flags;
73
	struct bio *bio;
74

75 76 77 78 79 80 81 82 83 84
	nvme_mpath_clear_current_path(ns);

	/*
	 * If we got back an ANA error, we know the controller is alive but not
	 * ready to serve this namespace.  Kick of a re-read of the ANA
	 * information page, and just try any other available path for now.
	 */
	if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) {
		set_bit(NVME_NS_ANA_PENDING, &ns->flags);
		queue_work(nvme_wq, &ns->ctrl->ana_work);
C
Christoph Hellwig 已提交
85 86
	}

87
	spin_lock_irqsave(&ns->head->requeue_lock, flags);
88 89
	for (bio = req->bio; bio; bio = bio->bi_next)
		bio_set_dev(bio, ns->head->disk->part0);
90 91 92
	blk_steal_bios(&ns->head->requeue_list, req);
	spin_unlock_irqrestore(&ns->head->requeue_lock, flags);

93
	blk_mq_end_request(req, 0);
94 95 96 97 98 99 100
	kblockd_schedule_work(&ns->head->requeue_work);
}

void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
{
	struct nvme_ns *ns;

101
	down_read(&ctrl->namespaces_rwsem);
102 103 104 105
	list_for_each_entry(ns, &ctrl->namespaces, list) {
		if (ns->head->disk)
			kblockd_schedule_work(&ns->head->requeue_work);
	}
106
	up_read(&ctrl->namespaces_rwsem);
107 108
}

C
Christoph Hellwig 已提交
109 110 111 112 113 114 115 116 117
static const char *nvme_ana_state_names[] = {
	[0]				= "invalid state",
	[NVME_ANA_OPTIMIZED]		= "optimized",
	[NVME_ANA_NONOPTIMIZED]		= "non-optimized",
	[NVME_ANA_INACCESSIBLE]		= "inaccessible",
	[NVME_ANA_PERSISTENT_LOSS]	= "persistent-loss",
	[NVME_ANA_CHANGE]		= "change",
};

118
bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
119
{
120
	struct nvme_ns_head *head = ns->head;
121
	bool changed = false;
122 123 124
	int node;

	if (!head)
125
		goto out;
126 127

	for_each_node(node) {
128
		if (ns == rcu_access_pointer(head->current_path[node])) {
129
			rcu_assign_pointer(head->current_path[node], NULL);
130 131
			changed = true;
		}
132
	}
133 134 135 136 137 138 139 140 141
out:
	return changed;
}

void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
{
	struct nvme_ns *ns;

	mutex_lock(&ctrl->scan_lock);
142
	down_read(&ctrl->namespaces_rwsem);
143 144 145
	list_for_each_entry(ns, &ctrl->namespaces, list)
		if (nvme_mpath_clear_current_path(ns))
			kblockd_schedule_work(&ns->head->requeue_work);
146
	up_read(&ctrl->namespaces_rwsem);
147
	mutex_unlock(&ctrl->scan_lock);
148 149
}

150 151
static bool nvme_path_is_disabled(struct nvme_ns *ns)
{
152 153 154 155 156 157 158 159 160 161 162 163
	/*
	 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
	 * still be able to complete assuming that the controller is connected.
	 * Otherwise it will fail immediately and return to the requeue list.
	 */
	if (ns->ctrl->state != NVME_CTRL_LIVE &&
	    ns->ctrl->state != NVME_CTRL_DELETING)
		return true;
	if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
	    test_bit(NVME_NS_REMOVING, &ns->flags))
		return true;
	return false;
164 165
}

166 167 168 169
static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
{
	int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
	struct nvme_ns *found = NULL, *fallback = NULL, *ns;
170 171

	list_for_each_entry_rcu(ns, &head->list, siblings) {
172
		if (nvme_path_is_disabled(ns))
C
Christoph Hellwig 已提交
173
			continue;
174

175 176 177 178
		if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
			distance = node_distance(node, ns->ctrl->numa_node);
		else
			distance = LOCAL_DISTANCE;
179

C
Christoph Hellwig 已提交
180 181
		switch (ns->ana_state) {
		case NVME_ANA_OPTIMIZED:
182 183 184 185 186
			if (distance < found_distance) {
				found_distance = distance;
				found = ns;
			}
			break;
C
Christoph Hellwig 已提交
187
		case NVME_ANA_NONOPTIMIZED:
188 189 190 191
			if (distance < fallback_distance) {
				fallback_distance = distance;
				fallback = ns;
			}
C
Christoph Hellwig 已提交
192 193 194
			break;
		default:
			break;
195 196 197
		}
	}

198 199 200 201 202
	if (!found)
		found = fallback;
	if (found)
		rcu_assign_pointer(head->current_path[node], found);
	return found;
C
Christoph Hellwig 已提交
203 204
}

205 206 207 208 209 210 211 212 213 214 215 216 217
static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
		struct nvme_ns *ns)
{
	ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
			siblings);
	if (ns)
		return ns;
	return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
}

static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
		int node, struct nvme_ns *old)
{
218
	struct nvme_ns *ns, *found = NULL;
219

220 221 222
	if (list_is_singular(&head->list)) {
		if (nvme_path_is_disabled(old))
			return NULL;
223
		return old;
224
	}
225 226

	for (ns = nvme_next_ns(head, old);
227
	     ns && ns != old;
228
	     ns = nvme_next_ns(head, ns)) {
229
		if (nvme_path_is_disabled(ns))
230 231 232 233 234 235 236
			continue;

		if (ns->ana_state == NVME_ANA_OPTIMIZED) {
			found = ns;
			goto out;
		}
		if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
237
			found = ns;
238 239
	}

240 241 242 243 244 245
	/*
	 * The loop above skips the current path for round-robin semantics.
	 * Fall back to the current path if either:
	 *  - no other optimized path found and current is optimized,
	 *  - no other usable path found and current is usable.
	 */
246
	if (!nvme_path_is_disabled(old) &&
247
	    (old->ana_state == NVME_ANA_OPTIMIZED ||
248
	     (!found && old->ana_state == NVME_ANA_NONOPTIMIZED)))
249 250
		return old;

251
	if (!found)
252 253 254 255 256 257
		return NULL;
out:
	rcu_assign_pointer(head->current_path[node], found);
	return found;
}

C
Christoph Hellwig 已提交
258 259 260 261
static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
{
	return ns->ctrl->state == NVME_CTRL_LIVE &&
		ns->ana_state == NVME_ANA_OPTIMIZED;
262 263 264 265
}

inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
{
266 267
	int node = numa_node_id();
	struct nvme_ns *ns;
268

269
	ns = srcu_dereference(head->current_path[node], &head->srcu);
270 271 272 273 274 275 276
	if (unlikely(!ns))
		return __nvme_find_path(head, node);

	if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR)
		return nvme_round_robin_path(head, node, ns);
	if (unlikely(!nvme_path_is_optimized(ns)))
		return __nvme_find_path(head, node);
277 278 279
	return ns;
}

280 281 282 283 284
static bool nvme_available_path(struct nvme_ns_head *head)
{
	struct nvme_ns *ns;

	list_for_each_entry_rcu(ns, &head->list, siblings) {
285 286
		if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
			continue;
287 288 289 290 291 292 293 294 295 296 297 298 299
		switch (ns->ctrl->state) {
		case NVME_CTRL_LIVE:
		case NVME_CTRL_RESETTING:
		case NVME_CTRL_CONNECTING:
			/* fallthru */
			return true;
		default:
			break;
		}
	}
	return false;
}

300
static blk_qc_t nvme_ns_head_submit_bio(struct bio *bio)
301
{
302
	struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data;
303 304 305 306 307
	struct device *dev = disk_to_dev(head->disk);
	struct nvme_ns *ns;
	blk_qc_t ret = BLK_QC_T_NONE;
	int srcu_idx;

308
	/*
309 310 311
	 * The namespace might be going away and the bio might be moved to a
	 * different queue via blk_steal_bios(), so we need to use the bio_split
	 * pool from the original queue to allocate the bvecs from.
312
	 */
313
	blk_queue_split(&bio);
314

315 316 317
	srcu_idx = srcu_read_lock(&head->srcu);
	ns = nvme_find_path(head);
	if (likely(ns)) {
318
		bio_set_dev(bio, ns->disk->part0);
319
		bio->bi_opf |= REQ_NVME_MPATH;
320
		trace_block_bio_remap(bio, disk_devt(ns->head->disk),
321
				      bio->bi_iter.bi_sector);
322
		ret = submit_bio_noacct(bio);
323 324
	} else if (nvme_available_path(head)) {
		dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
325 326 327 328 329

		spin_lock_irq(&head->requeue_lock);
		bio_list_add(&head->requeue_list, bio);
		spin_unlock_irq(&head->requeue_lock);
	} else {
330
		dev_warn_ratelimited(dev, "no available path - failing I/O\n");
331 332 333 334 335 336 337 338 339

		bio->bi_status = BLK_STS_IOERR;
		bio_endio(bio);
	}

	srcu_read_unlock(&head->srcu, srcu_idx);
	return ret;
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
{
	if (!nvme_tryget_ns_head(bdev->bd_disk->private_data))
		return -ENXIO;
	return 0;
}

static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
{
	nvme_put_ns_head(disk->private_data);
}

const struct block_device_operations nvme_ns_head_ops = {
	.owner		= THIS_MODULE,
	.submit_bio	= nvme_ns_head_submit_bio,
	.open		= nvme_ns_head_open,
	.release	= nvme_ns_head_release,
	.ioctl		= nvme_ns_head_ioctl,
	.getgeo		= nvme_getgeo,
	.report_zones	= nvme_report_zones,
	.pr_ops		= &nvme_pr_ops,
};

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev)
{
	return container_of(cdev, struct nvme_ns_head, cdev);
}

static int nvme_ns_head_chr_open(struct inode *inode, struct file *file)
{
	if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev)))
		return -ENXIO;
	return 0;
}

static int nvme_ns_head_chr_release(struct inode *inode, struct file *file)
{
	nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev));
	return 0;
}

static const struct file_operations nvme_ns_head_chr_fops = {
	.owner		= THIS_MODULE,
	.open		= nvme_ns_head_chr_open,
	.release	= nvme_ns_head_chr_release,
	.unlocked_ioctl	= nvme_ns_head_chr_ioctl,
	.compat_ioctl	= compat_ptr_ioctl,
};

static int nvme_add_ns_head_cdev(struct nvme_ns_head *head)
{
	int ret;

	head->cdev_device.parent = &head->subsys->dev;
	ret = dev_set_name(&head->cdev_device, "ng%dn%d",
			   head->subsys->instance, head->instance);
	if (ret)
		return ret;
	ret = nvme_cdev_add(&head->cdev, &head->cdev_device,
			    &nvme_ns_head_chr_fops, THIS_MODULE);
	if (ret)
		kfree_const(head->cdev_device.kobj.name);
	return ret;
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
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.
		 */
423
		bio_set_dev(bio, head->disk->part0);
424
		submit_bio_noacct(bio);
425 426 427 428 429 430 431 432
	}
}

int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
{
	struct request_queue *q;
	bool vwc = false;

C
Christoph Hellwig 已提交
433
	mutex_init(&head->lock);
434 435 436 437 438 439 440 441 442
	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.
	 */
443
	if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || !multipath)
444 445
		return 0;

446
	q = blk_alloc_queue(ctrl->numa_node);
447 448
	if (!q)
		goto out;
449
	blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
450 451
	/* set to a default value for 512 until disk is validated */
	blk_queue_logical_block_size(q, 512);
452
	blk_set_stacking_limits(&q->limits);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

	/* 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;
}

C
Christoph Hellwig 已提交
476
static void nvme_mpath_set_live(struct nvme_ns *ns)
477
{
C
Christoph Hellwig 已提交
478 479
	struct nvme_ns_head *head = ns->head;

480 481
	if (!head->disk)
		return;
482

483
	if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
484 485
		device_add_disk(&head->subsys->dev, head->disk,
				nvme_ns_id_attr_groups);
486 487
		nvme_add_ns_head_cdev(head);
	}
C
Christoph Hellwig 已提交
488

489
	mutex_lock(&head->lock);
490 491 492 493 494 495 496 497
	if (nvme_path_is_optimized(ns)) {
		int node, srcu_idx;

		srcu_idx = srcu_read_lock(&head->srcu);
		for_each_node(node)
			__nvme_find_path(head, node);
		srcu_read_unlock(&head->srcu, srcu_idx);
	}
498
	mutex_unlock(&head->lock);
499

500 501
	synchronize_srcu(&head->srcu);
	kblockd_schedule_work(&head->requeue_work);
C
Christoph Hellwig 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515
}

static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
		int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
			void *))
{
	void *base = ctrl->ana_log_buf;
	size_t offset = sizeof(struct nvme_ana_rsp_hdr);
	int error, i;

	lockdep_assert_held(&ctrl->ana_lock);

	for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
		struct nvme_ana_group_desc *desc = base + offset;
516 517 518 519 520 521 522 523
		u32 nr_nsids;
		size_t nsid_buf_size;

		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
			return -EINVAL;

		nr_nsids = le32_to_cpu(desc->nnsids);
		nsid_buf_size = nr_nsids * sizeof(__le32);
C
Christoph Hellwig 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

		if (WARN_ON_ONCE(desc->grpid == 0))
			return -EINVAL;
		if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
			return -EINVAL;
		if (WARN_ON_ONCE(desc->state == 0))
			return -EINVAL;
		if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
			return -EINVAL;

		offset += sizeof(*desc);
		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
			return -EINVAL;

		error = cb(ctrl, desc, data);
		if (error)
			return error;

		offset += nsid_buf_size;
	}

	return 0;
}

static inline bool nvme_state_is_live(enum nvme_ana_state state)
{
	return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
}

static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
		struct nvme_ns *ns)
{
	ns->ana_grpid = le32_to_cpu(desc->grpid);
	ns->ana_state = desc->state;
	clear_bit(NVME_NS_ANA_PENDING, &ns->flags);

560
	if (nvme_state_is_live(ns->ana_state))
C
Christoph Hellwig 已提交
561 562 563 564 565 566 567 568 569 570
		nvme_mpath_set_live(ns);
}

static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
		struct nvme_ana_group_desc *desc, void *data)
{
	u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
	unsigned *nr_change_groups = data;
	struct nvme_ns *ns;

571
	dev_dbg(ctrl->device, "ANA group %d: %s.\n",
C
Christoph Hellwig 已提交
572 573 574 575 576 577 578 579 580
			le32_to_cpu(desc->grpid),
			nvme_ana_state_names[desc->state]);

	if (desc->state == NVME_ANA_CHANGE)
		(*nr_change_groups)++;

	if (!nr_nsids)
		return 0;

581
	down_read(&ctrl->namespaces_rwsem);
C
Christoph Hellwig 已提交
582
	list_for_each_entry(ns, &ctrl->namespaces, list) {
583 584 585
		unsigned nsid = le32_to_cpu(desc->nsids[n]);

		if (ns->head->ns_id < nsid)
C
Christoph Hellwig 已提交
586
			continue;
587 588
		if (ns->head->ns_id == nsid)
			nvme_update_ns_ana_state(desc, ns);
C
Christoph Hellwig 已提交
589 590 591
		if (++n == nr_nsids)
			break;
	}
592
	up_read(&ctrl->namespaces_rwsem);
C
Christoph Hellwig 已提交
593 594 595
	return 0;
}

596
static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
C
Christoph Hellwig 已提交
597 598 599 600 601
{
	u32 nr_change_groups = 0;
	int error;

	mutex_lock(&ctrl->ana_lock);
602
	error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
C
Christoph Hellwig 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
			ctrl->ana_log_buf, ctrl->ana_log_size, 0);
	if (error) {
		dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
		goto out_unlock;
	}

	error = nvme_parse_ana_log(ctrl, &nr_change_groups,
			nvme_update_ana_state);
	if (error)
		goto out_unlock;

	/*
	 * In theory we should have an ANATT timer per group as they might enter
	 * the change state at different times.  But that is a lot of overhead
	 * just to protect against a target that keeps entering new changes
	 * states while never finishing previous ones.  But we'll still
	 * eventually time out once all groups are in change state, so this
	 * isn't a big deal.
	 *
	 * We also double the ANATT value to provide some slack for transports
	 * or AEN processing overhead.
	 */
	if (nr_change_groups)
		mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
	else
		del_timer_sync(&ctrl->anatt_timer);
out_unlock:
	mutex_unlock(&ctrl->ana_lock);
	return error;
}

static void nvme_ana_work(struct work_struct *work)
{
	struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);

638 639 640
	if (ctrl->state != NVME_CTRL_LIVE)
		return;

641
	nvme_read_ana_log(ctrl);
C
Christoph Hellwig 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
}

static void nvme_anatt_timeout(struct timer_list *t)
{
	struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);

	dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
	nvme_reset_ctrl(ctrl);
}

void nvme_mpath_stop(struct nvme_ctrl *ctrl)
{
	if (!nvme_ctrl_use_ana(ctrl))
		return;
	del_timer_sync(&ctrl->anatt_timer);
	cancel_work_sync(&ctrl->ana_work);
}

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
#define SUBSYS_ATTR_RW(_name, _mode, _show, _store)  \
	struct device_attribute subsys_attr_##_name =	\
		__ATTR(_name, _mode, _show, _store)

static const char *nvme_iopolicy_names[] = {
	[NVME_IOPOLICY_NUMA]	= "numa",
	[NVME_IOPOLICY_RR]	= "round-robin",
};

static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct nvme_subsystem *subsys =
		container_of(dev, struct nvme_subsystem, dev);

675 676
	return sysfs_emit(buf, "%s\n",
			  nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
}

static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct nvme_subsystem *subsys =
		container_of(dev, struct nvme_subsystem, dev);
	int i;

	for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
		if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
			WRITE_ONCE(subsys->iopolicy, i);
			return count;
		}
	}

	return -EINVAL;
}
SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
		      nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);

C
Christoph Hellwig 已提交
698 699 700
static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
701
	return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
C
Christoph Hellwig 已提交
702 703 704 705 706 707 708 709
}
DEVICE_ATTR_RO(ana_grpid);

static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	struct nvme_ns *ns = nvme_get_ns_from_dev(dev);

710
	return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
C
Christoph Hellwig 已提交
711 712 713
}
DEVICE_ATTR_RO(ana_state);

714
static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
C
Christoph Hellwig 已提交
715 716
		struct nvme_ana_group_desc *desc, void *data)
{
717
	struct nvme_ana_group_desc *dst = data;
C
Christoph Hellwig 已提交
718

719 720
	if (desc->grpid != dst->grpid)
		return 0;
C
Christoph Hellwig 已提交
721

722 723
	*dst = *desc;
	return -ENXIO; /* just break out of the loop */
C
Christoph Hellwig 已提交
724 725 726 727 728
}

void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
{
	if (nvme_ctrl_use_ana(ns->ctrl)) {
729 730 731 732 733
		struct nvme_ana_group_desc desc = {
			.grpid = id->anagrpid,
			.state = 0,
		};

C
Christoph Hellwig 已提交
734 735
		mutex_lock(&ns->ctrl->ana_lock);
		ns->ana_grpid = le32_to_cpu(id->anagrpid);
736
		nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
C
Christoph Hellwig 已提交
737
		mutex_unlock(&ns->ctrl->ana_lock);
738 739 740
		if (desc.state) {
			/* found the group desc: update */
			nvme_update_ns_ana_state(&desc, ns);
741 742 743 744
		} else {
			/* group desc not found: trigger a re-read */
			set_bit(NVME_NS_ANA_PENDING, &ns->flags);
			queue_work(nvme_wq, &ns->ctrl->ana_work);
745
		}
C
Christoph Hellwig 已提交
746
	} else {
747
		ns->ana_state = NVME_ANA_OPTIMIZED;
C
Christoph Hellwig 已提交
748
		nvme_mpath_set_live(ns);
749
	}
750

751 752 753
	if (blk_queue_stable_writes(ns->queue) && ns->head->disk)
		blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES,
				   ns->head->disk->queue);
754 755 756 757
#ifdef CONFIG_BLK_DEV_ZONED
	if (blk_queue_is_zoned(ns->queue) && ns->head->disk)
		ns->head->disk->queue->nr_zones = ns->queue->nr_zones;
#endif
758 759 760 761 762 763
}

void nvme_mpath_remove_disk(struct nvme_ns_head *head)
{
	if (!head->disk)
		return;
764 765
	if (head->disk->flags & GENHD_FL_UP) {
		nvme_cdev_del(&head->cdev, &head->cdev_device);
C
Christoph Hellwig 已提交
766
		del_gendisk(head->disk);
767
	}
768 769 770 771 772
	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);
773 774 775 776 777 778 779 780
	if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
		/*
		 * if device_add_disk wasn't called, prevent
		 * disk release to put a bogus reference on the
		 * request queue
		 */
		head->disk->queue = NULL;
	}
781 782
	put_disk(head->disk);
}
C
Christoph Hellwig 已提交
783

784
void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
C
Christoph Hellwig 已提交
785
{
786 787 788 789 790 791 792 793 794 795
	mutex_init(&ctrl->ana_lock);
	timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
	INIT_WORK(&ctrl->ana_work, nvme_ana_work);
}

int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
{
	size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT;
	size_t ana_log_size;
	int error = 0;
C
Christoph Hellwig 已提交
796

797
	/* check if multipath is enabled and we have the capability */
798 799
	if (!multipath || !ctrl->subsys ||
	    !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
C
Christoph Hellwig 已提交
800 801 802 803 804 805 806
		return 0;

	ctrl->anacap = id->anacap;
	ctrl->anatt = id->anatt;
	ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
	ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);

807 808 809 810
	ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
		ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
		ctrl->max_namespaces * sizeof(__le32);
	if (ana_log_size > max_transfer_size) {
C
Christoph Hellwig 已提交
811
		dev_err(ctrl->device,
812 813
			"ANA log page size (%zd) larger than MDTS (%zd).\n",
			ana_log_size, max_transfer_size);
C
Christoph Hellwig 已提交
814
		dev_err(ctrl->device, "disabling ANA support.\n");
815
		goto out_uninit;
C
Christoph Hellwig 已提交
816
	}
817 818 819
	if (ana_log_size > ctrl->ana_log_size) {
		nvme_mpath_stop(ctrl);
		kfree(ctrl->ana_log_buf);
820
		ctrl->ana_log_buf = kmalloc(ana_log_size, GFP_KERNEL);
821 822
		if (!ctrl->ana_log_buf)
			return -ENOMEM;
823
	}
824
	ctrl->ana_log_size = ana_log_size;
825
	error = nvme_read_ana_log(ctrl);
C
Christoph Hellwig 已提交
826
	if (error)
827
		goto out_uninit;
C
Christoph Hellwig 已提交
828
	return 0;
829 830 831

out_uninit:
	nvme_mpath_uninit(ctrl);
832
	return error;
C
Christoph Hellwig 已提交
833 834 835 836 837
}

void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
{
	kfree(ctrl->ana_log_buf);
838
	ctrl->ana_log_buf = NULL;
C
Christoph Hellwig 已提交
839
}