xref: /linux/drivers/nvme/host/multipath.c (revision 2eb81a3364eada43985efc0641490b73af78d0fa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 #include "nvme.h"
10 
11 static bool multipath = true;
12 module_param(multipath, bool, 0444);
13 MODULE_PARM_DESC(multipath,
14 	"turn on native support for multiple controllers per subsystem");
15 
16 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
17 {
18 	struct nvme_ns_head *h;
19 
20 	lockdep_assert_held(&subsys->lock);
21 	list_for_each_entry(h, &subsys->nsheads, entry)
22 		if (h->disk)
23 			blk_mq_unfreeze_queue(h->disk->queue);
24 }
25 
26 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
27 {
28 	struct nvme_ns_head *h;
29 
30 	lockdep_assert_held(&subsys->lock);
31 	list_for_each_entry(h, &subsys->nsheads, entry)
32 		if (h->disk)
33 			blk_mq_freeze_queue_wait(h->disk->queue);
34 }
35 
36 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
37 {
38 	struct nvme_ns_head *h;
39 
40 	lockdep_assert_held(&subsys->lock);
41 	list_for_each_entry(h, &subsys->nsheads, entry)
42 		if (h->disk)
43 			blk_freeze_queue_start(h->disk->queue);
44 }
45 
46 /*
47  * If multipathing is enabled we need to always use the subsystem instance
48  * number for numbering our devices to avoid conflicts between subsystems that
49  * have multiple controllers and thus use the multipath-aware subsystem node
50  * and those that have a single controller and use the controller node
51  * directly.
52  */
53 void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
54 			struct nvme_ctrl *ctrl, int *flags)
55 {
56 	if (!multipath) {
57 		sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
58 	} else if (ns->head->disk) {
59 		sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
60 				ctrl->instance, ns->head->instance);
61 		*flags = GENHD_FL_HIDDEN;
62 	} else {
63 		sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
64 				ns->head->instance);
65 	}
66 }
67 
68 bool nvme_failover_req(struct request *req)
69 {
70 	struct nvme_ns *ns = req->q->queuedata;
71 	u16 status = nvme_req(req)->status;
72 	unsigned long flags;
73 
74 	switch (status & 0x7ff) {
75 	case NVME_SC_ANA_TRANSITION:
76 	case NVME_SC_ANA_INACCESSIBLE:
77 	case NVME_SC_ANA_PERSISTENT_LOSS:
78 		/*
79 		 * If we got back an ANA error we know the controller is alive,
80 		 * but not ready to serve this namespaces.  The spec suggests
81 		 * we should update our general state here, but due to the fact
82 		 * that the admin and I/O queues are not serialized that is
83 		 * fundamentally racy.  So instead just clear the current path,
84 		 * mark the the path as pending and kick of a re-read of the ANA
85 		 * log page ASAP.
86 		 */
87 		nvme_mpath_clear_current_path(ns);
88 		if (ns->ctrl->ana_log_buf) {
89 			set_bit(NVME_NS_ANA_PENDING, &ns->flags);
90 			queue_work(nvme_wq, &ns->ctrl->ana_work);
91 		}
92 		break;
93 	case NVME_SC_HOST_PATH_ERROR:
94 	case NVME_SC_HOST_ABORTED_CMD:
95 		/*
96 		 * Temporary transport disruption in talking to the controller.
97 		 * Try to send on a new path.
98 		 */
99 		nvme_mpath_clear_current_path(ns);
100 		break;
101 	default:
102 		/* This was a non-ANA error so follow the normal error path. */
103 		return false;
104 	}
105 
106 	spin_lock_irqsave(&ns->head->requeue_lock, flags);
107 	blk_steal_bios(&ns->head->requeue_list, req);
108 	spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
109 	blk_mq_end_request(req, 0);
110 
111 	kblockd_schedule_work(&ns->head->requeue_work);
112 	return true;
113 }
114 
115 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
116 {
117 	struct nvme_ns *ns;
118 
119 	down_read(&ctrl->namespaces_rwsem);
120 	list_for_each_entry(ns, &ctrl->namespaces, list) {
121 		if (ns->head->disk)
122 			kblockd_schedule_work(&ns->head->requeue_work);
123 	}
124 	up_read(&ctrl->namespaces_rwsem);
125 }
126 
127 static const char *nvme_ana_state_names[] = {
128 	[0]				= "invalid state",
129 	[NVME_ANA_OPTIMIZED]		= "optimized",
130 	[NVME_ANA_NONOPTIMIZED]		= "non-optimized",
131 	[NVME_ANA_INACCESSIBLE]		= "inaccessible",
132 	[NVME_ANA_PERSISTENT_LOSS]	= "persistent-loss",
133 	[NVME_ANA_CHANGE]		= "change",
134 };
135 
136 bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
137 {
138 	struct nvme_ns_head *head = ns->head;
139 	bool changed = false;
140 	int node;
141 
142 	if (!head)
143 		goto out;
144 
145 	for_each_node(node) {
146 		if (ns == rcu_access_pointer(head->current_path[node])) {
147 			rcu_assign_pointer(head->current_path[node], NULL);
148 			changed = true;
149 		}
150 	}
151 out:
152 	return changed;
153 }
154 
155 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
156 {
157 	struct nvme_ns *ns;
158 
159 	mutex_lock(&ctrl->scan_lock);
160 	down_read(&ctrl->namespaces_rwsem);
161 	list_for_each_entry(ns, &ctrl->namespaces, list)
162 		if (nvme_mpath_clear_current_path(ns))
163 			kblockd_schedule_work(&ns->head->requeue_work);
164 	up_read(&ctrl->namespaces_rwsem);
165 	mutex_unlock(&ctrl->scan_lock);
166 }
167 
168 static bool nvme_path_is_disabled(struct nvme_ns *ns)
169 {
170 	/*
171 	 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
172 	 * still be able to complete assuming that the controller is connected.
173 	 * Otherwise it will fail immediately and return to the requeue list.
174 	 */
175 	if (ns->ctrl->state != NVME_CTRL_LIVE &&
176 	    ns->ctrl->state != NVME_CTRL_DELETING)
177 		return true;
178 	if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
179 	    test_bit(NVME_NS_REMOVING, &ns->flags))
180 		return true;
181 	return false;
182 }
183 
184 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
185 {
186 	int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
187 	struct nvme_ns *found = NULL, *fallback = NULL, *ns;
188 
189 	list_for_each_entry_rcu(ns, &head->list, siblings) {
190 		if (nvme_path_is_disabled(ns))
191 			continue;
192 
193 		if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
194 			distance = node_distance(node, ns->ctrl->numa_node);
195 		else
196 			distance = LOCAL_DISTANCE;
197 
198 		switch (ns->ana_state) {
199 		case NVME_ANA_OPTIMIZED:
200 			if (distance < found_distance) {
201 				found_distance = distance;
202 				found = ns;
203 			}
204 			break;
205 		case NVME_ANA_NONOPTIMIZED:
206 			if (distance < fallback_distance) {
207 				fallback_distance = distance;
208 				fallback = ns;
209 			}
210 			break;
211 		default:
212 			break;
213 		}
214 	}
215 
216 	if (!found)
217 		found = fallback;
218 	if (found)
219 		rcu_assign_pointer(head->current_path[node], found);
220 	return found;
221 }
222 
223 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
224 		struct nvme_ns *ns)
225 {
226 	ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
227 			siblings);
228 	if (ns)
229 		return ns;
230 	return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
231 }
232 
233 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
234 		int node, struct nvme_ns *old)
235 {
236 	struct nvme_ns *ns, *found = NULL;
237 
238 	if (list_is_singular(&head->list)) {
239 		if (nvme_path_is_disabled(old))
240 			return NULL;
241 		return old;
242 	}
243 
244 	for (ns = nvme_next_ns(head, old);
245 	     ns != old;
246 	     ns = nvme_next_ns(head, ns)) {
247 		if (nvme_path_is_disabled(ns))
248 			continue;
249 
250 		if (ns->ana_state == NVME_ANA_OPTIMIZED) {
251 			found = ns;
252 			goto out;
253 		}
254 		if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
255 			found = ns;
256 	}
257 
258 	/*
259 	 * The loop above skips the current path for round-robin semantics.
260 	 * Fall back to the current path if either:
261 	 *  - no other optimized path found and current is optimized,
262 	 *  - no other usable path found and current is usable.
263 	 */
264 	if (!nvme_path_is_disabled(old) &&
265 	    (old->ana_state == NVME_ANA_OPTIMIZED ||
266 	     (!found && old->ana_state == NVME_ANA_NONOPTIMIZED)))
267 		return old;
268 
269 	if (!found)
270 		return NULL;
271 out:
272 	rcu_assign_pointer(head->current_path[node], found);
273 	return found;
274 }
275 
276 static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
277 {
278 	return ns->ctrl->state == NVME_CTRL_LIVE &&
279 		ns->ana_state == NVME_ANA_OPTIMIZED;
280 }
281 
282 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
283 {
284 	int node = numa_node_id();
285 	struct nvme_ns *ns;
286 
287 	ns = srcu_dereference(head->current_path[node], &head->srcu);
288 	if (unlikely(!ns))
289 		return __nvme_find_path(head, node);
290 
291 	if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR)
292 		return nvme_round_robin_path(head, node, ns);
293 	if (unlikely(!nvme_path_is_optimized(ns)))
294 		return __nvme_find_path(head, node);
295 	return ns;
296 }
297 
298 static bool nvme_available_path(struct nvme_ns_head *head)
299 {
300 	struct nvme_ns *ns;
301 
302 	list_for_each_entry_rcu(ns, &head->list, siblings) {
303 		switch (ns->ctrl->state) {
304 		case NVME_CTRL_LIVE:
305 		case NVME_CTRL_RESETTING:
306 		case NVME_CTRL_CONNECTING:
307 			/* fallthru */
308 			return true;
309 		default:
310 			break;
311 		}
312 	}
313 	return false;
314 }
315 
316 blk_qc_t nvme_ns_head_submit_bio(struct bio *bio)
317 {
318 	struct nvme_ns_head *head = bio->bi_disk->private_data;
319 	struct device *dev = disk_to_dev(head->disk);
320 	struct nvme_ns *ns;
321 	blk_qc_t ret = BLK_QC_T_NONE;
322 	int srcu_idx;
323 
324 	/*
325 	 * The namespace might be going away and the bio might be moved to a
326 	 * different queue via blk_steal_bios(), so we need to use the bio_split
327 	 * pool from the original queue to allocate the bvecs from.
328 	 */
329 	blk_queue_split(&bio);
330 
331 	srcu_idx = srcu_read_lock(&head->srcu);
332 	ns = nvme_find_path(head);
333 	if (likely(ns)) {
334 		bio->bi_disk = ns->disk;
335 		bio->bi_opf |= REQ_NVME_MPATH;
336 		trace_block_bio_remap(bio->bi_disk->queue, bio,
337 				      disk_devt(ns->head->disk),
338 				      bio->bi_iter.bi_sector);
339 		ret = submit_bio_noacct(bio);
340 	} else if (nvme_available_path(head)) {
341 		dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
342 
343 		spin_lock_irq(&head->requeue_lock);
344 		bio_list_add(&head->requeue_list, bio);
345 		spin_unlock_irq(&head->requeue_lock);
346 	} else {
347 		dev_warn_ratelimited(dev, "no available path - failing I/O\n");
348 
349 		bio->bi_status = BLK_STS_IOERR;
350 		bio_endio(bio);
351 	}
352 
353 	srcu_read_unlock(&head->srcu, srcu_idx);
354 	return ret;
355 }
356 
357 static void nvme_requeue_work(struct work_struct *work)
358 {
359 	struct nvme_ns_head *head =
360 		container_of(work, struct nvme_ns_head, requeue_work);
361 	struct bio *bio, *next;
362 
363 	spin_lock_irq(&head->requeue_lock);
364 	next = bio_list_get(&head->requeue_list);
365 	spin_unlock_irq(&head->requeue_lock);
366 
367 	while ((bio = next) != NULL) {
368 		next = bio->bi_next;
369 		bio->bi_next = NULL;
370 
371 		/*
372 		 * Reset disk to the mpath node and resubmit to select a new
373 		 * path.
374 		 */
375 		bio->bi_disk = head->disk;
376 		submit_bio_noacct(bio);
377 	}
378 }
379 
380 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
381 {
382 	struct request_queue *q;
383 	bool vwc = false;
384 
385 	mutex_init(&head->lock);
386 	bio_list_init(&head->requeue_list);
387 	spin_lock_init(&head->requeue_lock);
388 	INIT_WORK(&head->requeue_work, nvme_requeue_work);
389 
390 	/*
391 	 * Add a multipath node if the subsystems supports multiple controllers.
392 	 * We also do this for private namespaces as the namespace sharing data could
393 	 * change after a rescan.
394 	 */
395 	if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || !multipath)
396 		return 0;
397 
398 	q = blk_alloc_queue(ctrl->numa_node);
399 	if (!q)
400 		goto out;
401 	blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
402 	/* set to a default value for 512 until disk is validated */
403 	blk_queue_logical_block_size(q, 512);
404 	blk_set_stacking_limits(&q->limits);
405 
406 	/* we need to propagate up the VMC settings */
407 	if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
408 		vwc = true;
409 	blk_queue_write_cache(q, vwc, vwc);
410 
411 	head->disk = alloc_disk(0);
412 	if (!head->disk)
413 		goto out_cleanup_queue;
414 	head->disk->fops = &nvme_ns_head_ops;
415 	head->disk->private_data = head;
416 	head->disk->queue = q;
417 	head->disk->flags = GENHD_FL_EXT_DEVT;
418 	sprintf(head->disk->disk_name, "nvme%dn%d",
419 			ctrl->subsys->instance, head->instance);
420 	return 0;
421 
422 out_cleanup_queue:
423 	blk_cleanup_queue(q);
424 out:
425 	return -ENOMEM;
426 }
427 
428 static void nvme_mpath_set_live(struct nvme_ns *ns)
429 {
430 	struct nvme_ns_head *head = ns->head;
431 
432 	if (!head->disk)
433 		return;
434 
435 	if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags))
436 		device_add_disk(&head->subsys->dev, head->disk,
437 				nvme_ns_id_attr_groups);
438 
439 	mutex_lock(&head->lock);
440 	if (nvme_path_is_optimized(ns)) {
441 		int node, srcu_idx;
442 
443 		srcu_idx = srcu_read_lock(&head->srcu);
444 		for_each_node(node)
445 			__nvme_find_path(head, node);
446 		srcu_read_unlock(&head->srcu, srcu_idx);
447 	}
448 	mutex_unlock(&head->lock);
449 
450 	synchronize_srcu(&head->srcu);
451 	kblockd_schedule_work(&head->requeue_work);
452 }
453 
454 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
455 		int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
456 			void *))
457 {
458 	void *base = ctrl->ana_log_buf;
459 	size_t offset = sizeof(struct nvme_ana_rsp_hdr);
460 	int error, i;
461 
462 	lockdep_assert_held(&ctrl->ana_lock);
463 
464 	for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
465 		struct nvme_ana_group_desc *desc = base + offset;
466 		u32 nr_nsids;
467 		size_t nsid_buf_size;
468 
469 		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
470 			return -EINVAL;
471 
472 		nr_nsids = le32_to_cpu(desc->nnsids);
473 		nsid_buf_size = nr_nsids * sizeof(__le32);
474 
475 		if (WARN_ON_ONCE(desc->grpid == 0))
476 			return -EINVAL;
477 		if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
478 			return -EINVAL;
479 		if (WARN_ON_ONCE(desc->state == 0))
480 			return -EINVAL;
481 		if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
482 			return -EINVAL;
483 
484 		offset += sizeof(*desc);
485 		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
486 			return -EINVAL;
487 
488 		error = cb(ctrl, desc, data);
489 		if (error)
490 			return error;
491 
492 		offset += nsid_buf_size;
493 	}
494 
495 	return 0;
496 }
497 
498 static inline bool nvme_state_is_live(enum nvme_ana_state state)
499 {
500 	return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
501 }
502 
503 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
504 		struct nvme_ns *ns)
505 {
506 	ns->ana_grpid = le32_to_cpu(desc->grpid);
507 	ns->ana_state = desc->state;
508 	clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
509 
510 	if (nvme_state_is_live(ns->ana_state))
511 		nvme_mpath_set_live(ns);
512 }
513 
514 static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
515 		struct nvme_ana_group_desc *desc, void *data)
516 {
517 	u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
518 	unsigned *nr_change_groups = data;
519 	struct nvme_ns *ns;
520 
521 	dev_dbg(ctrl->device, "ANA group %d: %s.\n",
522 			le32_to_cpu(desc->grpid),
523 			nvme_ana_state_names[desc->state]);
524 
525 	if (desc->state == NVME_ANA_CHANGE)
526 		(*nr_change_groups)++;
527 
528 	if (!nr_nsids)
529 		return 0;
530 
531 	down_read(&ctrl->namespaces_rwsem);
532 	list_for_each_entry(ns, &ctrl->namespaces, list) {
533 		unsigned nsid = le32_to_cpu(desc->nsids[n]);
534 
535 		if (ns->head->ns_id < nsid)
536 			continue;
537 		if (ns->head->ns_id == nsid)
538 			nvme_update_ns_ana_state(desc, ns);
539 		if (++n == nr_nsids)
540 			break;
541 	}
542 	up_read(&ctrl->namespaces_rwsem);
543 	return 0;
544 }
545 
546 static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
547 {
548 	u32 nr_change_groups = 0;
549 	int error;
550 
551 	mutex_lock(&ctrl->ana_lock);
552 	error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
553 			ctrl->ana_log_buf, ctrl->ana_log_size, 0);
554 	if (error) {
555 		dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
556 		goto out_unlock;
557 	}
558 
559 	error = nvme_parse_ana_log(ctrl, &nr_change_groups,
560 			nvme_update_ana_state);
561 	if (error)
562 		goto out_unlock;
563 
564 	/*
565 	 * In theory we should have an ANATT timer per group as they might enter
566 	 * the change state at different times.  But that is a lot of overhead
567 	 * just to protect against a target that keeps entering new changes
568 	 * states while never finishing previous ones.  But we'll still
569 	 * eventually time out once all groups are in change state, so this
570 	 * isn't a big deal.
571 	 *
572 	 * We also double the ANATT value to provide some slack for transports
573 	 * or AEN processing overhead.
574 	 */
575 	if (nr_change_groups)
576 		mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
577 	else
578 		del_timer_sync(&ctrl->anatt_timer);
579 out_unlock:
580 	mutex_unlock(&ctrl->ana_lock);
581 	return error;
582 }
583 
584 static void nvme_ana_work(struct work_struct *work)
585 {
586 	struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
587 
588 	if (ctrl->state != NVME_CTRL_LIVE)
589 		return;
590 
591 	nvme_read_ana_log(ctrl);
592 }
593 
594 static void nvme_anatt_timeout(struct timer_list *t)
595 {
596 	struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer);
597 
598 	dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
599 	nvme_reset_ctrl(ctrl);
600 }
601 
602 void nvme_mpath_stop(struct nvme_ctrl *ctrl)
603 {
604 	if (!nvme_ctrl_use_ana(ctrl))
605 		return;
606 	del_timer_sync(&ctrl->anatt_timer);
607 	cancel_work_sync(&ctrl->ana_work);
608 }
609 
610 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store)  \
611 	struct device_attribute subsys_attr_##_name =	\
612 		__ATTR(_name, _mode, _show, _store)
613 
614 static const char *nvme_iopolicy_names[] = {
615 	[NVME_IOPOLICY_NUMA]	= "numa",
616 	[NVME_IOPOLICY_RR]	= "round-robin",
617 };
618 
619 static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
620 		struct device_attribute *attr, char *buf)
621 {
622 	struct nvme_subsystem *subsys =
623 		container_of(dev, struct nvme_subsystem, dev);
624 
625 	return sprintf(buf, "%s\n",
626 			nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
627 }
628 
629 static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
630 		struct device_attribute *attr, const char *buf, size_t count)
631 {
632 	struct nvme_subsystem *subsys =
633 		container_of(dev, struct nvme_subsystem, dev);
634 	int i;
635 
636 	for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
637 		if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
638 			WRITE_ONCE(subsys->iopolicy, i);
639 			return count;
640 		}
641 	}
642 
643 	return -EINVAL;
644 }
645 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
646 		      nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
647 
648 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
649 		char *buf)
650 {
651 	return sprintf(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
652 }
653 DEVICE_ATTR_RO(ana_grpid);
654 
655 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
656 		char *buf)
657 {
658 	struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
659 
660 	return sprintf(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
661 }
662 DEVICE_ATTR_RO(ana_state);
663 
664 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
665 		struct nvme_ana_group_desc *desc, void *data)
666 {
667 	struct nvme_ana_group_desc *dst = data;
668 
669 	if (desc->grpid != dst->grpid)
670 		return 0;
671 
672 	*dst = *desc;
673 	return -ENXIO; /* just break out of the loop */
674 }
675 
676 void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
677 {
678 	if (nvme_ctrl_use_ana(ns->ctrl)) {
679 		struct nvme_ana_group_desc desc = {
680 			.grpid = id->anagrpid,
681 			.state = 0,
682 		};
683 
684 		mutex_lock(&ns->ctrl->ana_lock);
685 		ns->ana_grpid = le32_to_cpu(id->anagrpid);
686 		nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
687 		mutex_unlock(&ns->ctrl->ana_lock);
688 		if (desc.state) {
689 			/* found the group desc: update */
690 			nvme_update_ns_ana_state(&desc, ns);
691 		}
692 	} else {
693 		ns->ana_state = NVME_ANA_OPTIMIZED;
694 		nvme_mpath_set_live(ns);
695 	}
696 
697 	if (bdi_cap_stable_pages_required(ns->queue->backing_dev_info)) {
698 		struct gendisk *disk = ns->head->disk;
699 
700 		if (disk)
701 			disk->queue->backing_dev_info->capabilities |=
702 					BDI_CAP_STABLE_WRITES;
703 	}
704 }
705 
706 void nvme_mpath_remove_disk(struct nvme_ns_head *head)
707 {
708 	if (!head->disk)
709 		return;
710 	if (head->disk->flags & GENHD_FL_UP)
711 		del_gendisk(head->disk);
712 	blk_set_queue_dying(head->disk->queue);
713 	/* make sure all pending bios are cleaned up */
714 	kblockd_schedule_work(&head->requeue_work);
715 	flush_work(&head->requeue_work);
716 	blk_cleanup_queue(head->disk->queue);
717 	if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
718 		/*
719 		 * if device_add_disk wasn't called, prevent
720 		 * disk release to put a bogus reference on the
721 		 * request queue
722 		 */
723 		head->disk->queue = NULL;
724 	}
725 	put_disk(head->disk);
726 }
727 
728 int nvme_mpath_init(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
729 {
730 	int error;
731 
732 	/* check if multipath is enabled and we have the capability */
733 	if (!multipath || !ctrl->subsys ||
734 	    !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
735 		return 0;
736 
737 	ctrl->anacap = id->anacap;
738 	ctrl->anatt = id->anatt;
739 	ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
740 	ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
741 
742 	mutex_init(&ctrl->ana_lock);
743 	timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
744 	ctrl->ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
745 		ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc);
746 	ctrl->ana_log_size += ctrl->max_namespaces * sizeof(__le32);
747 
748 	if (ctrl->ana_log_size > ctrl->max_hw_sectors << SECTOR_SHIFT) {
749 		dev_err(ctrl->device,
750 			"ANA log page size (%zd) larger than MDTS (%d).\n",
751 			ctrl->ana_log_size,
752 			ctrl->max_hw_sectors << SECTOR_SHIFT);
753 		dev_err(ctrl->device, "disabling ANA support.\n");
754 		return 0;
755 	}
756 
757 	INIT_WORK(&ctrl->ana_work, nvme_ana_work);
758 	kfree(ctrl->ana_log_buf);
759 	ctrl->ana_log_buf = kmalloc(ctrl->ana_log_size, GFP_KERNEL);
760 	if (!ctrl->ana_log_buf) {
761 		error = -ENOMEM;
762 		goto out;
763 	}
764 
765 	error = nvme_read_ana_log(ctrl);
766 	if (error)
767 		goto out_free_ana_log_buf;
768 	return 0;
769 out_free_ana_log_buf:
770 	kfree(ctrl->ana_log_buf);
771 	ctrl->ana_log_buf = NULL;
772 out:
773 	return error;
774 }
775 
776 void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
777 {
778 	kfree(ctrl->ana_log_buf);
779 	ctrl->ana_log_buf = NULL;
780 }
781 
782