xref: /openbmc/linux/drivers/md/dm-table.c (revision 11976fe2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2001 Sistina Software (UK) Limited.
4  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5  *
6  * This file is released under the GPL.
7  */
8 
9 #include "dm-core.h"
10 #include "dm-rq.h"
11 
12 #include <linux/module.h>
13 #include <linux/vmalloc.h>
14 #include <linux/blkdev.h>
15 #include <linux/blk-integrity.h>
16 #include <linux/namei.h>
17 #include <linux/ctype.h>
18 #include <linux/string.h>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
21 #include <linux/mutex.h>
22 #include <linux/delay.h>
23 #include <linux/atomic.h>
24 #include <linux/blk-mq.h>
25 #include <linux/mount.h>
26 #include <linux/dax.h>
27 
28 #define DM_MSG_PREFIX "table"
29 
30 #define NODE_SIZE L1_CACHE_BYTES
31 #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
32 #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
33 
34 /*
35  * Similar to ceiling(log_size(n))
36  */
37 static unsigned int int_log(unsigned int n, unsigned int base)
38 {
39 	int result = 0;
40 
41 	while (n > 1) {
42 		n = dm_div_up(n, base);
43 		result++;
44 	}
45 
46 	return result;
47 }
48 
49 /*
50  * Calculate the index of the child node of the n'th node k'th key.
51  */
52 static inline unsigned int get_child(unsigned int n, unsigned int k)
53 {
54 	return (n * CHILDREN_PER_NODE) + k;
55 }
56 
57 /*
58  * Return the n'th node of level l from table t.
59  */
60 static inline sector_t *get_node(struct dm_table *t,
61 				 unsigned int l, unsigned int n)
62 {
63 	return t->index[l] + (n * KEYS_PER_NODE);
64 }
65 
66 /*
67  * Return the highest key that you could lookup from the n'th
68  * node on level l of the btree.
69  */
70 static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
71 {
72 	for (; l < t->depth - 1; l++)
73 		n = get_child(n, CHILDREN_PER_NODE - 1);
74 
75 	if (n >= t->counts[l])
76 		return (sector_t) -1;
77 
78 	return get_node(t, l, n)[KEYS_PER_NODE - 1];
79 }
80 
81 /*
82  * Fills in a level of the btree based on the highs of the level
83  * below it.
84  */
85 static int setup_btree_index(unsigned int l, struct dm_table *t)
86 {
87 	unsigned int n, k;
88 	sector_t *node;
89 
90 	for (n = 0U; n < t->counts[l]; n++) {
91 		node = get_node(t, l, n);
92 
93 		for (k = 0U; k < KEYS_PER_NODE; k++)
94 			node[k] = high(t, l + 1, get_child(n, k));
95 	}
96 
97 	return 0;
98 }
99 
100 /*
101  * highs, and targets are managed as dynamic arrays during a
102  * table load.
103  */
104 static int alloc_targets(struct dm_table *t, unsigned int num)
105 {
106 	sector_t *n_highs;
107 	struct dm_target *n_targets;
108 
109 	/*
110 	 * Allocate both the target array and offset array at once.
111 	 */
112 	n_highs = kvcalloc(num, sizeof(struct dm_target) + sizeof(sector_t),
113 			   GFP_KERNEL);
114 	if (!n_highs)
115 		return -ENOMEM;
116 
117 	n_targets = (struct dm_target *) (n_highs + num);
118 
119 	memset(n_highs, -1, sizeof(*n_highs) * num);
120 	kvfree(t->highs);
121 
122 	t->num_allocated = num;
123 	t->highs = n_highs;
124 	t->targets = n_targets;
125 
126 	return 0;
127 }
128 
129 int dm_table_create(struct dm_table **result, fmode_t mode,
130 		    unsigned int num_targets, struct mapped_device *md)
131 {
132 	struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
133 
134 	if (!t)
135 		return -ENOMEM;
136 
137 	INIT_LIST_HEAD(&t->devices);
138 
139 	if (!num_targets)
140 		num_targets = KEYS_PER_NODE;
141 
142 	num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
143 
144 	if (!num_targets) {
145 		kfree(t);
146 		return -ENOMEM;
147 	}
148 
149 	if (alloc_targets(t, num_targets)) {
150 		kfree(t);
151 		return -ENOMEM;
152 	}
153 
154 	t->type = DM_TYPE_NONE;
155 	t->mode = mode;
156 	t->md = md;
157 	*result = t;
158 	return 0;
159 }
160 
161 static void free_devices(struct list_head *devices, struct mapped_device *md)
162 {
163 	struct list_head *tmp, *next;
164 
165 	list_for_each_safe(tmp, next, devices) {
166 		struct dm_dev_internal *dd =
167 		    list_entry(tmp, struct dm_dev_internal, list);
168 		DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s",
169 		       dm_device_name(md), dd->dm_dev->name);
170 		dm_put_table_device(md, dd->dm_dev);
171 		kfree(dd);
172 	}
173 }
174 
175 static void dm_table_destroy_crypto_profile(struct dm_table *t);
176 
177 void dm_table_destroy(struct dm_table *t)
178 {
179 	if (!t)
180 		return;
181 
182 	/* free the indexes */
183 	if (t->depth >= 2)
184 		kvfree(t->index[t->depth - 2]);
185 
186 	/* free the targets */
187 	for (unsigned int i = 0; i < t->num_targets; i++) {
188 		struct dm_target *ti = dm_table_get_target(t, i);
189 
190 		if (ti->type->dtr)
191 			ti->type->dtr(ti);
192 
193 		dm_put_target_type(ti->type);
194 	}
195 
196 	kvfree(t->highs);
197 
198 	/* free the device list */
199 	free_devices(&t->devices, t->md);
200 
201 	dm_free_md_mempools(t->mempools);
202 
203 	dm_table_destroy_crypto_profile(t);
204 
205 	kfree(t);
206 }
207 
208 /*
209  * See if we've already got a device in the list.
210  */
211 static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
212 {
213 	struct dm_dev_internal *dd;
214 
215 	list_for_each_entry(dd, l, list)
216 		if (dd->dm_dev->bdev->bd_dev == dev)
217 			return dd;
218 
219 	return NULL;
220 }
221 
222 /*
223  * If possible, this checks an area of a destination device is invalid.
224  */
225 static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
226 				  sector_t start, sector_t len, void *data)
227 {
228 	struct queue_limits *limits = data;
229 	struct block_device *bdev = dev->bdev;
230 	sector_t dev_size = bdev_nr_sectors(bdev);
231 	unsigned short logical_block_size_sectors =
232 		limits->logical_block_size >> SECTOR_SHIFT;
233 
234 	if (!dev_size)
235 		return 0;
236 
237 	if ((start >= dev_size) || (start + len > dev_size)) {
238 		DMERR("%s: %pg too small for target: start=%llu, len=%llu, dev_size=%llu",
239 		      dm_device_name(ti->table->md), bdev,
240 		      (unsigned long long)start,
241 		      (unsigned long long)len,
242 		      (unsigned long long)dev_size);
243 		return 1;
244 	}
245 
246 	/*
247 	 * If the target is mapped to zoned block device(s), check
248 	 * that the zones are not partially mapped.
249 	 */
250 	if (bdev_is_zoned(bdev)) {
251 		unsigned int zone_sectors = bdev_zone_sectors(bdev);
252 
253 		if (start & (zone_sectors - 1)) {
254 			DMERR("%s: start=%llu not aligned to h/w zone size %u of %pg",
255 			      dm_device_name(ti->table->md),
256 			      (unsigned long long)start,
257 			      zone_sectors, bdev);
258 			return 1;
259 		}
260 
261 		/*
262 		 * Note: The last zone of a zoned block device may be smaller
263 		 * than other zones. So for a target mapping the end of a
264 		 * zoned block device with such a zone, len would not be zone
265 		 * aligned. We do not allow such last smaller zone to be part
266 		 * of the mapping here to ensure that mappings with multiple
267 		 * devices do not end up with a smaller zone in the middle of
268 		 * the sector range.
269 		 */
270 		if (len & (zone_sectors - 1)) {
271 			DMERR("%s: len=%llu not aligned to h/w zone size %u of %pg",
272 			      dm_device_name(ti->table->md),
273 			      (unsigned long long)len,
274 			      zone_sectors, bdev);
275 			return 1;
276 		}
277 	}
278 
279 	if (logical_block_size_sectors <= 1)
280 		return 0;
281 
282 	if (start & (logical_block_size_sectors - 1)) {
283 		DMERR("%s: start=%llu not aligned to h/w logical block size %u of %pg",
284 		      dm_device_name(ti->table->md),
285 		      (unsigned long long)start,
286 		      limits->logical_block_size, bdev);
287 		return 1;
288 	}
289 
290 	if (len & (logical_block_size_sectors - 1)) {
291 		DMERR("%s: len=%llu not aligned to h/w logical block size %u of %pg",
292 		      dm_device_name(ti->table->md),
293 		      (unsigned long long)len,
294 		      limits->logical_block_size, bdev);
295 		return 1;
296 	}
297 
298 	return 0;
299 }
300 
301 /*
302  * This upgrades the mode on an already open dm_dev, being
303  * careful to leave things as they were if we fail to reopen the
304  * device and not to touch the existing bdev field in case
305  * it is accessed concurrently.
306  */
307 static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
308 			struct mapped_device *md)
309 {
310 	int r;
311 	struct dm_dev *old_dev, *new_dev;
312 
313 	old_dev = dd->dm_dev;
314 
315 	r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
316 				dd->dm_dev->mode | new_mode, &new_dev);
317 	if (r)
318 		return r;
319 
320 	dd->dm_dev = new_dev;
321 	dm_put_table_device(md, old_dev);
322 
323 	return 0;
324 }
325 
326 /*
327  * Convert the path to a device
328  */
329 dev_t dm_get_dev_t(const char *path)
330 {
331 	dev_t dev;
332 
333 	if (lookup_bdev(path, &dev))
334 		dev = name_to_dev_t(path);
335 	return dev;
336 }
337 EXPORT_SYMBOL_GPL(dm_get_dev_t);
338 
339 /*
340  * Add a device to the list, or just increment the usage count if
341  * it's already present.
342  */
343 int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
344 		  struct dm_dev **result)
345 {
346 	int r;
347 	dev_t dev;
348 	unsigned int major, minor;
349 	char dummy;
350 	struct dm_dev_internal *dd;
351 	struct dm_table *t = ti->table;
352 
353 	BUG_ON(!t);
354 
355 	if (sscanf(path, "%u:%u%c", &major, &minor, &dummy) == 2) {
356 		/* Extract the major/minor numbers */
357 		dev = MKDEV(major, minor);
358 		if (MAJOR(dev) != major || MINOR(dev) != minor)
359 			return -EOVERFLOW;
360 	} else {
361 		dev = dm_get_dev_t(path);
362 		if (!dev)
363 			return -ENODEV;
364 	}
365 	if (dev == disk_devt(t->md->disk))
366 		return -EINVAL;
367 
368 	dd = find_device(&t->devices, dev);
369 	if (!dd) {
370 		dd = kmalloc(sizeof(*dd), GFP_KERNEL);
371 		if (!dd)
372 			return -ENOMEM;
373 
374 		r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev);
375 		if (r) {
376 			kfree(dd);
377 			return r;
378 		}
379 
380 		refcount_set(&dd->count, 1);
381 		list_add(&dd->list, &t->devices);
382 		goto out;
383 
384 	} else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
385 		r = upgrade_mode(dd, mode, t->md);
386 		if (r)
387 			return r;
388 	}
389 	refcount_inc(&dd->count);
390 out:
391 	*result = dd->dm_dev;
392 	return 0;
393 }
394 EXPORT_SYMBOL(dm_get_device);
395 
396 static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
397 				sector_t start, sector_t len, void *data)
398 {
399 	struct queue_limits *limits = data;
400 	struct block_device *bdev = dev->bdev;
401 	struct request_queue *q = bdev_get_queue(bdev);
402 
403 	if (unlikely(!q)) {
404 		DMWARN("%s: Cannot set limits for nonexistent device %pg",
405 		       dm_device_name(ti->table->md), bdev);
406 		return 0;
407 	}
408 
409 	if (blk_stack_limits(limits, &q->limits,
410 			get_start_sect(bdev) + start) < 0)
411 		DMWARN("%s: adding target device %pg caused an alignment inconsistency: "
412 		       "physical_block_size=%u, logical_block_size=%u, "
413 		       "alignment_offset=%u, start=%llu",
414 		       dm_device_name(ti->table->md), bdev,
415 		       q->limits.physical_block_size,
416 		       q->limits.logical_block_size,
417 		       q->limits.alignment_offset,
418 		       (unsigned long long) start << SECTOR_SHIFT);
419 	return 0;
420 }
421 
422 /*
423  * Decrement a device's use count and remove it if necessary.
424  */
425 void dm_put_device(struct dm_target *ti, struct dm_dev *d)
426 {
427 	int found = 0;
428 	struct list_head *devices = &ti->table->devices;
429 	struct dm_dev_internal *dd;
430 
431 	list_for_each_entry(dd, devices, list) {
432 		if (dd->dm_dev == d) {
433 			found = 1;
434 			break;
435 		}
436 	}
437 	if (!found) {
438 		DMERR("%s: device %s not in table devices list",
439 		      dm_device_name(ti->table->md), d->name);
440 		return;
441 	}
442 	if (refcount_dec_and_test(&dd->count)) {
443 		dm_put_table_device(ti->table->md, d);
444 		list_del(&dd->list);
445 		kfree(dd);
446 	}
447 }
448 EXPORT_SYMBOL(dm_put_device);
449 
450 /*
451  * Checks to see if the target joins onto the end of the table.
452  */
453 static int adjoin(struct dm_table *t, struct dm_target *ti)
454 {
455 	struct dm_target *prev;
456 
457 	if (!t->num_targets)
458 		return !ti->begin;
459 
460 	prev = &t->targets[t->num_targets - 1];
461 	return (ti->begin == (prev->begin + prev->len));
462 }
463 
464 /*
465  * Used to dynamically allocate the arg array.
466  *
467  * We do first allocation with GFP_NOIO because dm-mpath and dm-thin must
468  * process messages even if some device is suspended. These messages have a
469  * small fixed number of arguments.
470  *
471  * On the other hand, dm-switch needs to process bulk data using messages and
472  * excessive use of GFP_NOIO could cause trouble.
473  */
474 static char **realloc_argv(unsigned int *size, char **old_argv)
475 {
476 	char **argv;
477 	unsigned int new_size;
478 	gfp_t gfp;
479 
480 	if (*size) {
481 		new_size = *size * 2;
482 		gfp = GFP_KERNEL;
483 	} else {
484 		new_size = 8;
485 		gfp = GFP_NOIO;
486 	}
487 	argv = kmalloc_array(new_size, sizeof(*argv), gfp);
488 	if (argv && old_argv) {
489 		memcpy(argv, old_argv, *size * sizeof(*argv));
490 		*size = new_size;
491 	}
492 
493 	kfree(old_argv);
494 	return argv;
495 }
496 
497 /*
498  * Destructively splits up the argument list to pass to ctr.
499  */
500 int dm_split_args(int *argc, char ***argvp, char *input)
501 {
502 	char *start, *end = input, *out, **argv = NULL;
503 	unsigned int array_size = 0;
504 
505 	*argc = 0;
506 
507 	if (!input) {
508 		*argvp = NULL;
509 		return 0;
510 	}
511 
512 	argv = realloc_argv(&array_size, argv);
513 	if (!argv)
514 		return -ENOMEM;
515 
516 	while (1) {
517 		/* Skip whitespace */
518 		start = skip_spaces(end);
519 
520 		if (!*start)
521 			break;	/* success, we hit the end */
522 
523 		/* 'out' is used to remove any back-quotes */
524 		end = out = start;
525 		while (*end) {
526 			/* Everything apart from '\0' can be quoted */
527 			if (*end == '\\' && *(end + 1)) {
528 				*out++ = *(end + 1);
529 				end += 2;
530 				continue;
531 			}
532 
533 			if (isspace(*end))
534 				break;	/* end of token */
535 
536 			*out++ = *end++;
537 		}
538 
539 		/* have we already filled the array ? */
540 		if ((*argc + 1) > array_size) {
541 			argv = realloc_argv(&array_size, argv);
542 			if (!argv)
543 				return -ENOMEM;
544 		}
545 
546 		/* we know this is whitespace */
547 		if (*end)
548 			end++;
549 
550 		/* terminate the string and put it in the array */
551 		*out = '\0';
552 		argv[*argc] = start;
553 		(*argc)++;
554 	}
555 
556 	*argvp = argv;
557 	return 0;
558 }
559 
560 /*
561  * Impose necessary and sufficient conditions on a devices's table such
562  * that any incoming bio which respects its logical_block_size can be
563  * processed successfully.  If it falls across the boundary between
564  * two or more targets, the size of each piece it gets split into must
565  * be compatible with the logical_block_size of the target processing it.
566  */
567 static int validate_hardware_logical_block_alignment(struct dm_table *t,
568 						     struct queue_limits *limits)
569 {
570 	/*
571 	 * This function uses arithmetic modulo the logical_block_size
572 	 * (in units of 512-byte sectors).
573 	 */
574 	unsigned short device_logical_block_size_sects =
575 		limits->logical_block_size >> SECTOR_SHIFT;
576 
577 	/*
578 	 * Offset of the start of the next table entry, mod logical_block_size.
579 	 */
580 	unsigned short next_target_start = 0;
581 
582 	/*
583 	 * Given an aligned bio that extends beyond the end of a
584 	 * target, how many sectors must the next target handle?
585 	 */
586 	unsigned short remaining = 0;
587 
588 	struct dm_target *ti;
589 	struct queue_limits ti_limits;
590 	unsigned int i;
591 
592 	/*
593 	 * Check each entry in the table in turn.
594 	 */
595 	for (i = 0; i < t->num_targets; i++) {
596 		ti = dm_table_get_target(t, i);
597 
598 		blk_set_stacking_limits(&ti_limits);
599 
600 		/* combine all target devices' limits */
601 		if (ti->type->iterate_devices)
602 			ti->type->iterate_devices(ti, dm_set_device_limits,
603 						  &ti_limits);
604 
605 		/*
606 		 * If the remaining sectors fall entirely within this
607 		 * table entry are they compatible with its logical_block_size?
608 		 */
609 		if (remaining < ti->len &&
610 		    remaining & ((ti_limits.logical_block_size >>
611 				  SECTOR_SHIFT) - 1))
612 			break;	/* Error */
613 
614 		next_target_start =
615 		    (unsigned short) ((next_target_start + ti->len) &
616 				      (device_logical_block_size_sects - 1));
617 		remaining = next_target_start ?
618 		    device_logical_block_size_sects - next_target_start : 0;
619 	}
620 
621 	if (remaining) {
622 		DMERR("%s: table line %u (start sect %llu len %llu) "
623 		      "not aligned to h/w logical block size %u",
624 		      dm_device_name(t->md), i,
625 		      (unsigned long long) ti->begin,
626 		      (unsigned long long) ti->len,
627 		      limits->logical_block_size);
628 		return -EINVAL;
629 	}
630 
631 	return 0;
632 }
633 
634 int dm_table_add_target(struct dm_table *t, const char *type,
635 			sector_t start, sector_t len, char *params)
636 {
637 	int r = -EINVAL, argc;
638 	char **argv;
639 	struct dm_target *ti;
640 
641 	if (t->singleton) {
642 		DMERR("%s: target type %s must appear alone in table",
643 		      dm_device_name(t->md), t->targets->type->name);
644 		return -EINVAL;
645 	}
646 
647 	BUG_ON(t->num_targets >= t->num_allocated);
648 
649 	ti = t->targets + t->num_targets;
650 	memset(ti, 0, sizeof(*ti));
651 
652 	if (!len) {
653 		DMERR("%s: zero-length target", dm_device_name(t->md));
654 		return -EINVAL;
655 	}
656 
657 	ti->type = dm_get_target_type(type);
658 	if (!ti->type) {
659 		DMERR("%s: %s: unknown target type", dm_device_name(t->md), type);
660 		return -EINVAL;
661 	}
662 
663 	if (dm_target_needs_singleton(ti->type)) {
664 		if (t->num_targets) {
665 			ti->error = "singleton target type must appear alone in table";
666 			goto bad;
667 		}
668 		t->singleton = true;
669 	}
670 
671 	if (dm_target_always_writeable(ti->type) && !(t->mode & FMODE_WRITE)) {
672 		ti->error = "target type may not be included in a read-only table";
673 		goto bad;
674 	}
675 
676 	if (t->immutable_target_type) {
677 		if (t->immutable_target_type != ti->type) {
678 			ti->error = "immutable target type cannot be mixed with other target types";
679 			goto bad;
680 		}
681 	} else if (dm_target_is_immutable(ti->type)) {
682 		if (t->num_targets) {
683 			ti->error = "immutable target type cannot be mixed with other target types";
684 			goto bad;
685 		}
686 		t->immutable_target_type = ti->type;
687 	}
688 
689 	if (dm_target_has_integrity(ti->type))
690 		t->integrity_added = 1;
691 
692 	ti->table = t;
693 	ti->begin = start;
694 	ti->len = len;
695 	ti->error = "Unknown error";
696 
697 	/*
698 	 * Does this target adjoin the previous one ?
699 	 */
700 	if (!adjoin(t, ti)) {
701 		ti->error = "Gap in table";
702 		goto bad;
703 	}
704 
705 	r = dm_split_args(&argc, &argv, params);
706 	if (r) {
707 		ti->error = "couldn't split parameters";
708 		goto bad;
709 	}
710 
711 	r = ti->type->ctr(ti, argc, argv);
712 	kfree(argv);
713 	if (r)
714 		goto bad;
715 
716 	t->highs[t->num_targets++] = ti->begin + ti->len - 1;
717 
718 	if (!ti->num_discard_bios && ti->discards_supported)
719 		DMWARN("%s: %s: ignoring discards_supported because num_discard_bios is zero.",
720 		       dm_device_name(t->md), type);
721 
722 	if (ti->limit_swap_bios && !static_key_enabled(&swap_bios_enabled.key))
723 		static_branch_enable(&swap_bios_enabled);
724 
725 	return 0;
726 
727  bad:
728 	DMERR("%s: %s: %s (%pe)", dm_device_name(t->md), type, ti->error, ERR_PTR(r));
729 	dm_put_target_type(ti->type);
730 	return r;
731 }
732 
733 /*
734  * Target argument parsing helpers.
735  */
736 static int validate_next_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
737 			     unsigned int *value, char **error, unsigned int grouped)
738 {
739 	const char *arg_str = dm_shift_arg(arg_set);
740 	char dummy;
741 
742 	if (!arg_str ||
743 	    (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
744 	    (*value < arg->min) ||
745 	    (*value > arg->max) ||
746 	    (grouped && arg_set->argc < *value)) {
747 		*error = arg->error;
748 		return -EINVAL;
749 	}
750 
751 	return 0;
752 }
753 
754 int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
755 		unsigned int *value, char **error)
756 {
757 	return validate_next_arg(arg, arg_set, value, error, 0);
758 }
759 EXPORT_SYMBOL(dm_read_arg);
760 
761 int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
762 		      unsigned int *value, char **error)
763 {
764 	return validate_next_arg(arg, arg_set, value, error, 1);
765 }
766 EXPORT_SYMBOL(dm_read_arg_group);
767 
768 const char *dm_shift_arg(struct dm_arg_set *as)
769 {
770 	char *r;
771 
772 	if (as->argc) {
773 		as->argc--;
774 		r = *as->argv;
775 		as->argv++;
776 		return r;
777 	}
778 
779 	return NULL;
780 }
781 EXPORT_SYMBOL(dm_shift_arg);
782 
783 void dm_consume_args(struct dm_arg_set *as, unsigned int num_args)
784 {
785 	BUG_ON(as->argc < num_args);
786 	as->argc -= num_args;
787 	as->argv += num_args;
788 }
789 EXPORT_SYMBOL(dm_consume_args);
790 
791 static bool __table_type_bio_based(enum dm_queue_mode table_type)
792 {
793 	return (table_type == DM_TYPE_BIO_BASED ||
794 		table_type == DM_TYPE_DAX_BIO_BASED);
795 }
796 
797 static bool __table_type_request_based(enum dm_queue_mode table_type)
798 {
799 	return table_type == DM_TYPE_REQUEST_BASED;
800 }
801 
802 void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type)
803 {
804 	t->type = type;
805 }
806 EXPORT_SYMBOL_GPL(dm_table_set_type);
807 
808 /* validate the dax capability of the target device span */
809 static int device_not_dax_capable(struct dm_target *ti, struct dm_dev *dev,
810 			sector_t start, sector_t len, void *data)
811 {
812 	if (dev->dax_dev)
813 		return false;
814 
815 	DMDEBUG("%pg: error: dax unsupported by block device", dev->bdev);
816 	return true;
817 }
818 
819 /* Check devices support synchronous DAX */
820 static int device_not_dax_synchronous_capable(struct dm_target *ti, struct dm_dev *dev,
821 					      sector_t start, sector_t len, void *data)
822 {
823 	return !dev->dax_dev || !dax_synchronous(dev->dax_dev);
824 }
825 
826 static bool dm_table_supports_dax(struct dm_table *t,
827 				  iterate_devices_callout_fn iterate_fn)
828 {
829 	/* Ensure that all targets support DAX. */
830 	for (unsigned int i = 0; i < t->num_targets; i++) {
831 		struct dm_target *ti = dm_table_get_target(t, i);
832 
833 		if (!ti->type->direct_access)
834 			return false;
835 
836 		if (!ti->type->iterate_devices ||
837 		    ti->type->iterate_devices(ti, iterate_fn, NULL))
838 			return false;
839 	}
840 
841 	return true;
842 }
843 
844 static int device_is_rq_stackable(struct dm_target *ti, struct dm_dev *dev,
845 				  sector_t start, sector_t len, void *data)
846 {
847 	struct block_device *bdev = dev->bdev;
848 	struct request_queue *q = bdev_get_queue(bdev);
849 
850 	/* request-based cannot stack on partitions! */
851 	if (bdev_is_partition(bdev))
852 		return false;
853 
854 	return queue_is_mq(q);
855 }
856 
857 static int dm_table_determine_type(struct dm_table *t)
858 {
859 	unsigned int bio_based = 0, request_based = 0, hybrid = 0;
860 	struct dm_target *ti;
861 	struct list_head *devices = dm_table_get_devices(t);
862 	enum dm_queue_mode live_md_type = dm_get_md_type(t->md);
863 
864 	if (t->type != DM_TYPE_NONE) {
865 		/* target already set the table's type */
866 		if (t->type == DM_TYPE_BIO_BASED) {
867 			/* possibly upgrade to a variant of bio-based */
868 			goto verify_bio_based;
869 		}
870 		BUG_ON(t->type == DM_TYPE_DAX_BIO_BASED);
871 		goto verify_rq_based;
872 	}
873 
874 	for (unsigned int i = 0; i < t->num_targets; i++) {
875 		ti = dm_table_get_target(t, i);
876 		if (dm_target_hybrid(ti))
877 			hybrid = 1;
878 		else if (dm_target_request_based(ti))
879 			request_based = 1;
880 		else
881 			bio_based = 1;
882 
883 		if (bio_based && request_based) {
884 			DMERR("Inconsistent table: different target types can't be mixed up");
885 			return -EINVAL;
886 		}
887 	}
888 
889 	if (hybrid && !bio_based && !request_based) {
890 		/*
891 		 * The targets can work either way.
892 		 * Determine the type from the live device.
893 		 * Default to bio-based if device is new.
894 		 */
895 		if (__table_type_request_based(live_md_type))
896 			request_based = 1;
897 		else
898 			bio_based = 1;
899 	}
900 
901 	if (bio_based) {
902 verify_bio_based:
903 		/* We must use this table as bio-based */
904 		t->type = DM_TYPE_BIO_BASED;
905 		if (dm_table_supports_dax(t, device_not_dax_capable) ||
906 		    (list_empty(devices) && live_md_type == DM_TYPE_DAX_BIO_BASED)) {
907 			t->type = DM_TYPE_DAX_BIO_BASED;
908 		}
909 		return 0;
910 	}
911 
912 	BUG_ON(!request_based); /* No targets in this table */
913 
914 	t->type = DM_TYPE_REQUEST_BASED;
915 
916 verify_rq_based:
917 	/*
918 	 * Request-based dm supports only tables that have a single target now.
919 	 * To support multiple targets, request splitting support is needed,
920 	 * and that needs lots of changes in the block-layer.
921 	 * (e.g. request completion process for partial completion.)
922 	 */
923 	if (t->num_targets > 1) {
924 		DMERR("request-based DM doesn't support multiple targets");
925 		return -EINVAL;
926 	}
927 
928 	if (list_empty(devices)) {
929 		int srcu_idx;
930 		struct dm_table *live_table = dm_get_live_table(t->md, &srcu_idx);
931 
932 		/* inherit live table's type */
933 		if (live_table)
934 			t->type = live_table->type;
935 		dm_put_live_table(t->md, srcu_idx);
936 		return 0;
937 	}
938 
939 	ti = dm_table_get_immutable_target(t);
940 	if (!ti) {
941 		DMERR("table load rejected: immutable target is required");
942 		return -EINVAL;
943 	} else if (ti->max_io_len) {
944 		DMERR("table load rejected: immutable target that splits IO is not supported");
945 		return -EINVAL;
946 	}
947 
948 	/* Non-request-stackable devices can't be used for request-based dm */
949 	if (!ti->type->iterate_devices ||
950 	    !ti->type->iterate_devices(ti, device_is_rq_stackable, NULL)) {
951 		DMERR("table load rejected: including non-request-stackable devices");
952 		return -EINVAL;
953 	}
954 
955 	return 0;
956 }
957 
958 enum dm_queue_mode dm_table_get_type(struct dm_table *t)
959 {
960 	return t->type;
961 }
962 
963 struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
964 {
965 	return t->immutable_target_type;
966 }
967 
968 struct dm_target *dm_table_get_immutable_target(struct dm_table *t)
969 {
970 	/* Immutable target is implicitly a singleton */
971 	if (t->num_targets > 1 ||
972 	    !dm_target_is_immutable(t->targets[0].type))
973 		return NULL;
974 
975 	return t->targets;
976 }
977 
978 struct dm_target *dm_table_get_wildcard_target(struct dm_table *t)
979 {
980 	for (unsigned int i = 0; i < t->num_targets; i++) {
981 		struct dm_target *ti = dm_table_get_target(t, i);
982 
983 		if (dm_target_is_wildcard(ti->type))
984 			return ti;
985 	}
986 
987 	return NULL;
988 }
989 
990 bool dm_table_bio_based(struct dm_table *t)
991 {
992 	return __table_type_bio_based(dm_table_get_type(t));
993 }
994 
995 bool dm_table_request_based(struct dm_table *t)
996 {
997 	return __table_type_request_based(dm_table_get_type(t));
998 }
999 
1000 static bool dm_table_supports_poll(struct dm_table *t);
1001 
1002 static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
1003 {
1004 	enum dm_queue_mode type = dm_table_get_type(t);
1005 	unsigned int per_io_data_size = 0, front_pad, io_front_pad;
1006 	unsigned int min_pool_size = 0, pool_size;
1007 	struct dm_md_mempools *pools;
1008 
1009 	if (unlikely(type == DM_TYPE_NONE)) {
1010 		DMERR("no table type is set, can't allocate mempools");
1011 		return -EINVAL;
1012 	}
1013 
1014 	pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
1015 	if (!pools)
1016 		return -ENOMEM;
1017 
1018 	if (type == DM_TYPE_REQUEST_BASED) {
1019 		pool_size = dm_get_reserved_rq_based_ios();
1020 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
1021 		goto init_bs;
1022 	}
1023 
1024 	for (unsigned int i = 0; i < t->num_targets; i++) {
1025 		struct dm_target *ti = dm_table_get_target(t, i);
1026 
1027 		per_io_data_size = max(per_io_data_size, ti->per_io_data_size);
1028 		min_pool_size = max(min_pool_size, ti->num_flush_bios);
1029 	}
1030 	pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
1031 	front_pad = roundup(per_io_data_size,
1032 		__alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET;
1033 
1034 	io_front_pad = roundup(per_io_data_size,
1035 		__alignof__(struct dm_io)) + DM_IO_BIO_OFFSET;
1036 	if (bioset_init(&pools->io_bs, pool_size, io_front_pad,
1037 			dm_table_supports_poll(t) ? BIOSET_PERCPU_CACHE : 0))
1038 		goto out_free_pools;
1039 	if (t->integrity_supported &&
1040 	    bioset_integrity_create(&pools->io_bs, pool_size))
1041 		goto out_free_pools;
1042 init_bs:
1043 	if (bioset_init(&pools->bs, pool_size, front_pad, 0))
1044 		goto out_free_pools;
1045 	if (t->integrity_supported &&
1046 	    bioset_integrity_create(&pools->bs, pool_size))
1047 		goto out_free_pools;
1048 
1049 	t->mempools = pools;
1050 	return 0;
1051 
1052 out_free_pools:
1053 	dm_free_md_mempools(pools);
1054 	return -ENOMEM;
1055 }
1056 
1057 static int setup_indexes(struct dm_table *t)
1058 {
1059 	int i;
1060 	unsigned int total = 0;
1061 	sector_t *indexes;
1062 
1063 	/* allocate the space for *all* the indexes */
1064 	for (i = t->depth - 2; i >= 0; i--) {
1065 		t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
1066 		total += t->counts[i];
1067 	}
1068 
1069 	indexes = kvcalloc(total, NODE_SIZE, GFP_KERNEL);
1070 	if (!indexes)
1071 		return -ENOMEM;
1072 
1073 	/* set up internal nodes, bottom-up */
1074 	for (i = t->depth - 2; i >= 0; i--) {
1075 		t->index[i] = indexes;
1076 		indexes += (KEYS_PER_NODE * t->counts[i]);
1077 		setup_btree_index(i, t);
1078 	}
1079 
1080 	return 0;
1081 }
1082 
1083 /*
1084  * Builds the btree to index the map.
1085  */
1086 static int dm_table_build_index(struct dm_table *t)
1087 {
1088 	int r = 0;
1089 	unsigned int leaf_nodes;
1090 
1091 	/* how many indexes will the btree have ? */
1092 	leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
1093 	t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
1094 
1095 	/* leaf layer has already been set up */
1096 	t->counts[t->depth - 1] = leaf_nodes;
1097 	t->index[t->depth - 1] = t->highs;
1098 
1099 	if (t->depth >= 2)
1100 		r = setup_indexes(t);
1101 
1102 	return r;
1103 }
1104 
1105 static bool integrity_profile_exists(struct gendisk *disk)
1106 {
1107 	return !!blk_get_integrity(disk);
1108 }
1109 
1110 /*
1111  * Get a disk whose integrity profile reflects the table's profile.
1112  * Returns NULL if integrity support was inconsistent or unavailable.
1113  */
1114 static struct gendisk *dm_table_get_integrity_disk(struct dm_table *t)
1115 {
1116 	struct list_head *devices = dm_table_get_devices(t);
1117 	struct dm_dev_internal *dd = NULL;
1118 	struct gendisk *prev_disk = NULL, *template_disk = NULL;
1119 
1120 	for (unsigned int i = 0; i < t->num_targets; i++) {
1121 		struct dm_target *ti = dm_table_get_target(t, i);
1122 
1123 		if (!dm_target_passes_integrity(ti->type))
1124 			goto no_integrity;
1125 	}
1126 
1127 	list_for_each_entry(dd, devices, list) {
1128 		template_disk = dd->dm_dev->bdev->bd_disk;
1129 		if (!integrity_profile_exists(template_disk))
1130 			goto no_integrity;
1131 		else if (prev_disk &&
1132 			 blk_integrity_compare(prev_disk, template_disk) < 0)
1133 			goto no_integrity;
1134 		prev_disk = template_disk;
1135 	}
1136 
1137 	return template_disk;
1138 
1139 no_integrity:
1140 	if (prev_disk)
1141 		DMWARN("%s: integrity not set: %s and %s profile mismatch",
1142 		       dm_device_name(t->md),
1143 		       prev_disk->disk_name,
1144 		       template_disk->disk_name);
1145 	return NULL;
1146 }
1147 
1148 /*
1149  * Register the mapped device for blk_integrity support if the
1150  * underlying devices have an integrity profile.  But all devices may
1151  * not have matching profiles (checking all devices isn't reliable
1152  * during table load because this table may use other DM device(s) which
1153  * must be resumed before they will have an initialized integity
1154  * profile).  Consequently, stacked DM devices force a 2 stage integrity
1155  * profile validation: First pass during table load, final pass during
1156  * resume.
1157  */
1158 static int dm_table_register_integrity(struct dm_table *t)
1159 {
1160 	struct mapped_device *md = t->md;
1161 	struct gendisk *template_disk = NULL;
1162 
1163 	/* If target handles integrity itself do not register it here. */
1164 	if (t->integrity_added)
1165 		return 0;
1166 
1167 	template_disk = dm_table_get_integrity_disk(t);
1168 	if (!template_disk)
1169 		return 0;
1170 
1171 	if (!integrity_profile_exists(dm_disk(md))) {
1172 		t->integrity_supported = true;
1173 		/*
1174 		 * Register integrity profile during table load; we can do
1175 		 * this because the final profile must match during resume.
1176 		 */
1177 		blk_integrity_register(dm_disk(md),
1178 				       blk_get_integrity(template_disk));
1179 		return 0;
1180 	}
1181 
1182 	/*
1183 	 * If DM device already has an initialized integrity
1184 	 * profile the new profile should not conflict.
1185 	 */
1186 	if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
1187 		DMERR("%s: conflict with existing integrity profile: %s profile mismatch",
1188 		      dm_device_name(t->md),
1189 		      template_disk->disk_name);
1190 		return 1;
1191 	}
1192 
1193 	/* Preserve existing integrity profile */
1194 	t->integrity_supported = true;
1195 	return 0;
1196 }
1197 
1198 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1199 
1200 struct dm_crypto_profile {
1201 	struct blk_crypto_profile profile;
1202 	struct mapped_device *md;
1203 };
1204 
1205 static int dm_keyslot_evict_callback(struct dm_target *ti, struct dm_dev *dev,
1206 				     sector_t start, sector_t len, void *data)
1207 {
1208 	const struct blk_crypto_key *key = data;
1209 
1210 	blk_crypto_evict_key(dev->bdev, key);
1211 	return 0;
1212 }
1213 
1214 /*
1215  * When an inline encryption key is evicted from a device-mapper device, evict
1216  * it from all the underlying devices.
1217  */
1218 static int dm_keyslot_evict(struct blk_crypto_profile *profile,
1219 			    const struct blk_crypto_key *key, unsigned int slot)
1220 {
1221 	struct mapped_device *md =
1222 		container_of(profile, struct dm_crypto_profile, profile)->md;
1223 	struct dm_table *t;
1224 	int srcu_idx;
1225 
1226 	t = dm_get_live_table(md, &srcu_idx);
1227 	if (!t)
1228 		return 0;
1229 
1230 	for (unsigned int i = 0; i < t->num_targets; i++) {
1231 		struct dm_target *ti = dm_table_get_target(t, i);
1232 
1233 		if (!ti->type->iterate_devices)
1234 			continue;
1235 		ti->type->iterate_devices(ti, dm_keyslot_evict_callback,
1236 					  (void *)key);
1237 	}
1238 
1239 	dm_put_live_table(md, srcu_idx);
1240 	return 0;
1241 }
1242 
1243 static int
1244 device_intersect_crypto_capabilities(struct dm_target *ti, struct dm_dev *dev,
1245 				     sector_t start, sector_t len, void *data)
1246 {
1247 	struct blk_crypto_profile *parent = data;
1248 	struct blk_crypto_profile *child =
1249 		bdev_get_queue(dev->bdev)->crypto_profile;
1250 
1251 	blk_crypto_intersect_capabilities(parent, child);
1252 	return 0;
1253 }
1254 
1255 void dm_destroy_crypto_profile(struct blk_crypto_profile *profile)
1256 {
1257 	struct dm_crypto_profile *dmcp = container_of(profile,
1258 						      struct dm_crypto_profile,
1259 						      profile);
1260 
1261 	if (!profile)
1262 		return;
1263 
1264 	blk_crypto_profile_destroy(profile);
1265 	kfree(dmcp);
1266 }
1267 
1268 static void dm_table_destroy_crypto_profile(struct dm_table *t)
1269 {
1270 	dm_destroy_crypto_profile(t->crypto_profile);
1271 	t->crypto_profile = NULL;
1272 }
1273 
1274 /*
1275  * Constructs and initializes t->crypto_profile with a crypto profile that
1276  * represents the common set of crypto capabilities of the devices described by
1277  * the dm_table.  However, if the constructed crypto profile doesn't support all
1278  * crypto capabilities that are supported by the current mapped_device, it
1279  * returns an error instead, since we don't support removing crypto capabilities
1280  * on table changes.  Finally, if the constructed crypto profile is "empty" (has
1281  * no crypto capabilities at all), it just sets t->crypto_profile to NULL.
1282  */
1283 static int dm_table_construct_crypto_profile(struct dm_table *t)
1284 {
1285 	struct dm_crypto_profile *dmcp;
1286 	struct blk_crypto_profile *profile;
1287 	unsigned int i;
1288 	bool empty_profile = true;
1289 
1290 	dmcp = kmalloc(sizeof(*dmcp), GFP_KERNEL);
1291 	if (!dmcp)
1292 		return -ENOMEM;
1293 	dmcp->md = t->md;
1294 
1295 	profile = &dmcp->profile;
1296 	blk_crypto_profile_init(profile, 0);
1297 	profile->ll_ops.keyslot_evict = dm_keyslot_evict;
1298 	profile->max_dun_bytes_supported = UINT_MAX;
1299 	memset(profile->modes_supported, 0xFF,
1300 	       sizeof(profile->modes_supported));
1301 
1302 	for (i = 0; i < t->num_targets; i++) {
1303 		struct dm_target *ti = dm_table_get_target(t, i);
1304 
1305 		if (!dm_target_passes_crypto(ti->type)) {
1306 			blk_crypto_intersect_capabilities(profile, NULL);
1307 			break;
1308 		}
1309 		if (!ti->type->iterate_devices)
1310 			continue;
1311 		ti->type->iterate_devices(ti,
1312 					  device_intersect_crypto_capabilities,
1313 					  profile);
1314 	}
1315 
1316 	if (t->md->queue &&
1317 	    !blk_crypto_has_capabilities(profile,
1318 					 t->md->queue->crypto_profile)) {
1319 		DMERR("Inline encryption capabilities of new DM table were more restrictive than the old table's. This is not supported!");
1320 		dm_destroy_crypto_profile(profile);
1321 		return -EINVAL;
1322 	}
1323 
1324 	/*
1325 	 * If the new profile doesn't actually support any crypto capabilities,
1326 	 * we may as well represent it with a NULL profile.
1327 	 */
1328 	for (i = 0; i < ARRAY_SIZE(profile->modes_supported); i++) {
1329 		if (profile->modes_supported[i]) {
1330 			empty_profile = false;
1331 			break;
1332 		}
1333 	}
1334 
1335 	if (empty_profile) {
1336 		dm_destroy_crypto_profile(profile);
1337 		profile = NULL;
1338 	}
1339 
1340 	/*
1341 	 * t->crypto_profile is only set temporarily while the table is being
1342 	 * set up, and it gets set to NULL after the profile has been
1343 	 * transferred to the request_queue.
1344 	 */
1345 	t->crypto_profile = profile;
1346 
1347 	return 0;
1348 }
1349 
1350 static void dm_update_crypto_profile(struct request_queue *q,
1351 				     struct dm_table *t)
1352 {
1353 	if (!t->crypto_profile)
1354 		return;
1355 
1356 	/* Make the crypto profile less restrictive. */
1357 	if (!q->crypto_profile) {
1358 		blk_crypto_register(t->crypto_profile, q);
1359 	} else {
1360 		blk_crypto_update_capabilities(q->crypto_profile,
1361 					       t->crypto_profile);
1362 		dm_destroy_crypto_profile(t->crypto_profile);
1363 	}
1364 	t->crypto_profile = NULL;
1365 }
1366 
1367 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1368 
1369 static int dm_table_construct_crypto_profile(struct dm_table *t)
1370 {
1371 	return 0;
1372 }
1373 
1374 void dm_destroy_crypto_profile(struct blk_crypto_profile *profile)
1375 {
1376 }
1377 
1378 static void dm_table_destroy_crypto_profile(struct dm_table *t)
1379 {
1380 }
1381 
1382 static void dm_update_crypto_profile(struct request_queue *q,
1383 				     struct dm_table *t)
1384 {
1385 }
1386 
1387 #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */
1388 
1389 /*
1390  * Prepares the table for use by building the indices,
1391  * setting the type, and allocating mempools.
1392  */
1393 int dm_table_complete(struct dm_table *t)
1394 {
1395 	int r;
1396 
1397 	r = dm_table_determine_type(t);
1398 	if (r) {
1399 		DMERR("unable to determine table type");
1400 		return r;
1401 	}
1402 
1403 	r = dm_table_build_index(t);
1404 	if (r) {
1405 		DMERR("unable to build btrees");
1406 		return r;
1407 	}
1408 
1409 	r = dm_table_register_integrity(t);
1410 	if (r) {
1411 		DMERR("could not register integrity profile.");
1412 		return r;
1413 	}
1414 
1415 	r = dm_table_construct_crypto_profile(t);
1416 	if (r) {
1417 		DMERR("could not construct crypto profile.");
1418 		return r;
1419 	}
1420 
1421 	r = dm_table_alloc_md_mempools(t, t->md);
1422 	if (r)
1423 		DMERR("unable to allocate mempools");
1424 
1425 	return r;
1426 }
1427 
1428 static DEFINE_MUTEX(_event_lock);
1429 void dm_table_event_callback(struct dm_table *t,
1430 			     void (*fn)(void *), void *context)
1431 {
1432 	mutex_lock(&_event_lock);
1433 	t->event_fn = fn;
1434 	t->event_context = context;
1435 	mutex_unlock(&_event_lock);
1436 }
1437 
1438 void dm_table_event(struct dm_table *t)
1439 {
1440 	mutex_lock(&_event_lock);
1441 	if (t->event_fn)
1442 		t->event_fn(t->event_context);
1443 	mutex_unlock(&_event_lock);
1444 }
1445 EXPORT_SYMBOL(dm_table_event);
1446 
1447 inline sector_t dm_table_get_size(struct dm_table *t)
1448 {
1449 	return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
1450 }
1451 EXPORT_SYMBOL(dm_table_get_size);
1452 
1453 /*
1454  * Search the btree for the correct target.
1455  *
1456  * Caller should check returned pointer for NULL
1457  * to trap I/O beyond end of device.
1458  */
1459 struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
1460 {
1461 	unsigned int l, n = 0, k = 0;
1462 	sector_t *node;
1463 
1464 	if (unlikely(sector >= dm_table_get_size(t)))
1465 		return NULL;
1466 
1467 	for (l = 0; l < t->depth; l++) {
1468 		n = get_child(n, k);
1469 		node = get_node(t, l, n);
1470 
1471 		for (k = 0; k < KEYS_PER_NODE; k++)
1472 			if (node[k] >= sector)
1473 				break;
1474 	}
1475 
1476 	return &t->targets[(KEYS_PER_NODE * n) + k];
1477 }
1478 
1479 static int device_not_poll_capable(struct dm_target *ti, struct dm_dev *dev,
1480 				   sector_t start, sector_t len, void *data)
1481 {
1482 	struct request_queue *q = bdev_get_queue(dev->bdev);
1483 
1484 	return !test_bit(QUEUE_FLAG_POLL, &q->queue_flags);
1485 }
1486 
1487 /*
1488  * type->iterate_devices() should be called when the sanity check needs to
1489  * iterate and check all underlying data devices. iterate_devices() will
1490  * iterate all underlying data devices until it encounters a non-zero return
1491  * code, returned by whether the input iterate_devices_callout_fn, or
1492  * iterate_devices() itself internally.
1493  *
1494  * For some target type (e.g. dm-stripe), one call of iterate_devices() may
1495  * iterate multiple underlying devices internally, in which case a non-zero
1496  * return code returned by iterate_devices_callout_fn will stop the iteration
1497  * in advance.
1498  *
1499  * Cases requiring _any_ underlying device supporting some kind of attribute,
1500  * should use the iteration structure like dm_table_any_dev_attr(), or call
1501  * it directly. @func should handle semantics of positive examples, e.g.
1502  * capable of something.
1503  *
1504  * Cases requiring _all_ underlying devices supporting some kind of attribute,
1505  * should use the iteration structure like dm_table_supports_nowait() or
1506  * dm_table_supports_discards(). Or introduce dm_table_all_devs_attr() that
1507  * uses an @anti_func that handle semantics of counter examples, e.g. not
1508  * capable of something. So: return !dm_table_any_dev_attr(t, anti_func, data);
1509  */
1510 static bool dm_table_any_dev_attr(struct dm_table *t,
1511 				  iterate_devices_callout_fn func, void *data)
1512 {
1513 	for (unsigned int i = 0; i < t->num_targets; i++) {
1514 		struct dm_target *ti = dm_table_get_target(t, i);
1515 
1516 		if (ti->type->iterate_devices &&
1517 		    ti->type->iterate_devices(ti, func, data))
1518 			return true;
1519 	}
1520 
1521 	return false;
1522 }
1523 
1524 static int count_device(struct dm_target *ti, struct dm_dev *dev,
1525 			sector_t start, sector_t len, void *data)
1526 {
1527 	unsigned int *num_devices = data;
1528 
1529 	(*num_devices)++;
1530 
1531 	return 0;
1532 }
1533 
1534 static bool dm_table_supports_poll(struct dm_table *t)
1535 {
1536 	for (unsigned int i = 0; i < t->num_targets; i++) {
1537 		struct dm_target *ti = dm_table_get_target(t, i);
1538 
1539 		if (!ti->type->iterate_devices ||
1540 		    ti->type->iterate_devices(ti, device_not_poll_capable, NULL))
1541 			return false;
1542 	}
1543 
1544 	return true;
1545 }
1546 
1547 /*
1548  * Check whether a table has no data devices attached using each
1549  * target's iterate_devices method.
1550  * Returns false if the result is unknown because a target doesn't
1551  * support iterate_devices.
1552  */
1553 bool dm_table_has_no_data_devices(struct dm_table *t)
1554 {
1555 	for (unsigned int i = 0; i < t->num_targets; i++) {
1556 		struct dm_target *ti = dm_table_get_target(t, i);
1557 		unsigned int num_devices = 0;
1558 
1559 		if (!ti->type->iterate_devices)
1560 			return false;
1561 
1562 		ti->type->iterate_devices(ti, count_device, &num_devices);
1563 		if (num_devices)
1564 			return false;
1565 	}
1566 
1567 	return true;
1568 }
1569 
1570 static int device_not_zoned_model(struct dm_target *ti, struct dm_dev *dev,
1571 				  sector_t start, sector_t len, void *data)
1572 {
1573 	struct request_queue *q = bdev_get_queue(dev->bdev);
1574 	enum blk_zoned_model *zoned_model = data;
1575 
1576 	return blk_queue_zoned_model(q) != *zoned_model;
1577 }
1578 
1579 /*
1580  * Check the device zoned model based on the target feature flag. If the target
1581  * has the DM_TARGET_ZONED_HM feature flag set, host-managed zoned devices are
1582  * also accepted but all devices must have the same zoned model. If the target
1583  * has the DM_TARGET_MIXED_ZONED_MODEL feature set, the devices can have any
1584  * zoned model with all zoned devices having the same zone size.
1585  */
1586 static bool dm_table_supports_zoned_model(struct dm_table *t,
1587 					  enum blk_zoned_model zoned_model)
1588 {
1589 	for (unsigned int i = 0; i < t->num_targets; i++) {
1590 		struct dm_target *ti = dm_table_get_target(t, i);
1591 
1592 		if (dm_target_supports_zoned_hm(ti->type)) {
1593 			if (!ti->type->iterate_devices ||
1594 			    ti->type->iterate_devices(ti, device_not_zoned_model,
1595 						      &zoned_model))
1596 				return false;
1597 		} else if (!dm_target_supports_mixed_zoned_model(ti->type)) {
1598 			if (zoned_model == BLK_ZONED_HM)
1599 				return false;
1600 		}
1601 	}
1602 
1603 	return true;
1604 }
1605 
1606 static int device_not_matches_zone_sectors(struct dm_target *ti, struct dm_dev *dev,
1607 					   sector_t start, sector_t len, void *data)
1608 {
1609 	unsigned int *zone_sectors = data;
1610 
1611 	if (!bdev_is_zoned(dev->bdev))
1612 		return 0;
1613 	return bdev_zone_sectors(dev->bdev) != *zone_sectors;
1614 }
1615 
1616 /*
1617  * Check consistency of zoned model and zone sectors across all targets. For
1618  * zone sectors, if the destination device is a zoned block device, it shall
1619  * have the specified zone_sectors.
1620  */
1621 static int validate_hardware_zoned_model(struct dm_table *t,
1622 					 enum blk_zoned_model zoned_model,
1623 					 unsigned int zone_sectors)
1624 {
1625 	if (zoned_model == BLK_ZONED_NONE)
1626 		return 0;
1627 
1628 	if (!dm_table_supports_zoned_model(t, zoned_model)) {
1629 		DMERR("%s: zoned model is not consistent across all devices",
1630 		      dm_device_name(t->md));
1631 		return -EINVAL;
1632 	}
1633 
1634 	/* Check zone size validity and compatibility */
1635 	if (!zone_sectors || !is_power_of_2(zone_sectors))
1636 		return -EINVAL;
1637 
1638 	if (dm_table_any_dev_attr(t, device_not_matches_zone_sectors, &zone_sectors)) {
1639 		DMERR("%s: zone sectors is not consistent across all zoned devices",
1640 		      dm_device_name(t->md));
1641 		return -EINVAL;
1642 	}
1643 
1644 	return 0;
1645 }
1646 
1647 /*
1648  * Establish the new table's queue_limits and validate them.
1649  */
1650 int dm_calculate_queue_limits(struct dm_table *t,
1651 			      struct queue_limits *limits)
1652 {
1653 	struct queue_limits ti_limits;
1654 	enum blk_zoned_model zoned_model = BLK_ZONED_NONE;
1655 	unsigned int zone_sectors = 0;
1656 
1657 	blk_set_stacking_limits(limits);
1658 
1659 	for (unsigned int i = 0; i < t->num_targets; i++) {
1660 		struct dm_target *ti = dm_table_get_target(t, i);
1661 
1662 		blk_set_stacking_limits(&ti_limits);
1663 
1664 		if (!ti->type->iterate_devices) {
1665 			/* Set I/O hints portion of queue limits */
1666 			if (ti->type->io_hints)
1667 				ti->type->io_hints(ti, &ti_limits);
1668 			goto combine_limits;
1669 		}
1670 
1671 		/*
1672 		 * Combine queue limits of all the devices this target uses.
1673 		 */
1674 		ti->type->iterate_devices(ti, dm_set_device_limits,
1675 					  &ti_limits);
1676 
1677 		if (zoned_model == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
1678 			/*
1679 			 * After stacking all limits, validate all devices
1680 			 * in table support this zoned model and zone sectors.
1681 			 */
1682 			zoned_model = ti_limits.zoned;
1683 			zone_sectors = ti_limits.chunk_sectors;
1684 		}
1685 
1686 		/* Set I/O hints portion of queue limits */
1687 		if (ti->type->io_hints)
1688 			ti->type->io_hints(ti, &ti_limits);
1689 
1690 		/*
1691 		 * Check each device area is consistent with the target's
1692 		 * overall queue limits.
1693 		 */
1694 		if (ti->type->iterate_devices(ti, device_area_is_invalid,
1695 					      &ti_limits))
1696 			return -EINVAL;
1697 
1698 combine_limits:
1699 		/*
1700 		 * Merge this target's queue limits into the overall limits
1701 		 * for the table.
1702 		 */
1703 		if (blk_stack_limits(limits, &ti_limits, 0) < 0)
1704 			DMWARN("%s: adding target device (start sect %llu len %llu) "
1705 			       "caused an alignment inconsistency",
1706 			       dm_device_name(t->md),
1707 			       (unsigned long long) ti->begin,
1708 			       (unsigned long long) ti->len);
1709 	}
1710 
1711 	/*
1712 	 * Verify that the zoned model and zone sectors, as determined before
1713 	 * any .io_hints override, are the same across all devices in the table.
1714 	 * - this is especially relevant if .io_hints is emulating a disk-managed
1715 	 *   zoned model (aka BLK_ZONED_NONE) on host-managed zoned block devices.
1716 	 * BUT...
1717 	 */
1718 	if (limits->zoned != BLK_ZONED_NONE) {
1719 		/*
1720 		 * ...IF the above limits stacking determined a zoned model
1721 		 * validate that all of the table's devices conform to it.
1722 		 */
1723 		zoned_model = limits->zoned;
1724 		zone_sectors = limits->chunk_sectors;
1725 	}
1726 	if (validate_hardware_zoned_model(t, zoned_model, zone_sectors))
1727 		return -EINVAL;
1728 
1729 	return validate_hardware_logical_block_alignment(t, limits);
1730 }
1731 
1732 /*
1733  * Verify that all devices have an integrity profile that matches the
1734  * DM device's registered integrity profile.  If the profiles don't
1735  * match then unregister the DM device's integrity profile.
1736  */
1737 static void dm_table_verify_integrity(struct dm_table *t)
1738 {
1739 	struct gendisk *template_disk = NULL;
1740 
1741 	if (t->integrity_added)
1742 		return;
1743 
1744 	if (t->integrity_supported) {
1745 		/*
1746 		 * Verify that the original integrity profile
1747 		 * matches all the devices in this table.
1748 		 */
1749 		template_disk = dm_table_get_integrity_disk(t);
1750 		if (template_disk &&
1751 		    blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
1752 			return;
1753 	}
1754 
1755 	if (integrity_profile_exists(dm_disk(t->md))) {
1756 		DMWARN("%s: unable to establish an integrity profile",
1757 		       dm_device_name(t->md));
1758 		blk_integrity_unregister(dm_disk(t->md));
1759 	}
1760 }
1761 
1762 static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
1763 				sector_t start, sector_t len, void *data)
1764 {
1765 	unsigned long flush = (unsigned long) data;
1766 	struct request_queue *q = bdev_get_queue(dev->bdev);
1767 
1768 	return (q->queue_flags & flush);
1769 }
1770 
1771 static bool dm_table_supports_flush(struct dm_table *t, unsigned long flush)
1772 {
1773 	/*
1774 	 * Require at least one underlying device to support flushes.
1775 	 * t->devices includes internal dm devices such as mirror logs
1776 	 * so we need to use iterate_devices here, which targets
1777 	 * supporting flushes must provide.
1778 	 */
1779 	for (unsigned int i = 0; i < t->num_targets; i++) {
1780 		struct dm_target *ti = dm_table_get_target(t, i);
1781 
1782 		if (!ti->num_flush_bios)
1783 			continue;
1784 
1785 		if (ti->flush_supported)
1786 			return true;
1787 
1788 		if (ti->type->iterate_devices &&
1789 		    ti->type->iterate_devices(ti, device_flush_capable, (void *) flush))
1790 			return true;
1791 	}
1792 
1793 	return false;
1794 }
1795 
1796 static int device_dax_write_cache_enabled(struct dm_target *ti,
1797 					  struct dm_dev *dev, sector_t start,
1798 					  sector_t len, void *data)
1799 {
1800 	struct dax_device *dax_dev = dev->dax_dev;
1801 
1802 	if (!dax_dev)
1803 		return false;
1804 
1805 	if (dax_write_cache_enabled(dax_dev))
1806 		return true;
1807 	return false;
1808 }
1809 
1810 static int device_is_rotational(struct dm_target *ti, struct dm_dev *dev,
1811 				sector_t start, sector_t len, void *data)
1812 {
1813 	return !bdev_nonrot(dev->bdev);
1814 }
1815 
1816 static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
1817 			     sector_t start, sector_t len, void *data)
1818 {
1819 	struct request_queue *q = bdev_get_queue(dev->bdev);
1820 
1821 	return !blk_queue_add_random(q);
1822 }
1823 
1824 static int device_not_write_zeroes_capable(struct dm_target *ti, struct dm_dev *dev,
1825 					   sector_t start, sector_t len, void *data)
1826 {
1827 	struct request_queue *q = bdev_get_queue(dev->bdev);
1828 
1829 	return !q->limits.max_write_zeroes_sectors;
1830 }
1831 
1832 static bool dm_table_supports_write_zeroes(struct dm_table *t)
1833 {
1834 	for (unsigned int i = 0; i < t->num_targets; i++) {
1835 		struct dm_target *ti = dm_table_get_target(t, i);
1836 
1837 		if (!ti->num_write_zeroes_bios)
1838 			return false;
1839 
1840 		if (!ti->type->iterate_devices ||
1841 		    ti->type->iterate_devices(ti, device_not_write_zeroes_capable, NULL))
1842 			return false;
1843 	}
1844 
1845 	return true;
1846 }
1847 
1848 static int device_not_nowait_capable(struct dm_target *ti, struct dm_dev *dev,
1849 				     sector_t start, sector_t len, void *data)
1850 {
1851 	return !bdev_nowait(dev->bdev);
1852 }
1853 
1854 static bool dm_table_supports_nowait(struct dm_table *t)
1855 {
1856 	for (unsigned int i = 0; i < t->num_targets; i++) {
1857 		struct dm_target *ti = dm_table_get_target(t, i);
1858 
1859 		if (!dm_target_supports_nowait(ti->type))
1860 			return false;
1861 
1862 		if (!ti->type->iterate_devices ||
1863 		    ti->type->iterate_devices(ti, device_not_nowait_capable, NULL))
1864 			return false;
1865 	}
1866 
1867 	return true;
1868 }
1869 
1870 static int device_not_discard_capable(struct dm_target *ti, struct dm_dev *dev,
1871 				      sector_t start, sector_t len, void *data)
1872 {
1873 	return !bdev_max_discard_sectors(dev->bdev);
1874 }
1875 
1876 static bool dm_table_supports_discards(struct dm_table *t)
1877 {
1878 	for (unsigned int i = 0; i < t->num_targets; i++) {
1879 		struct dm_target *ti = dm_table_get_target(t, i);
1880 
1881 		if (!ti->num_discard_bios)
1882 			return false;
1883 
1884 		/*
1885 		 * Either the target provides discard support (as implied by setting
1886 		 * 'discards_supported') or it relies on _all_ data devices having
1887 		 * discard support.
1888 		 */
1889 		if (!ti->discards_supported &&
1890 		    (!ti->type->iterate_devices ||
1891 		     ti->type->iterate_devices(ti, device_not_discard_capable, NULL)))
1892 			return false;
1893 	}
1894 
1895 	return true;
1896 }
1897 
1898 static int device_not_secure_erase_capable(struct dm_target *ti,
1899 					   struct dm_dev *dev, sector_t start,
1900 					   sector_t len, void *data)
1901 {
1902 	return !bdev_max_secure_erase_sectors(dev->bdev);
1903 }
1904 
1905 static bool dm_table_supports_secure_erase(struct dm_table *t)
1906 {
1907 	for (unsigned int i = 0; i < t->num_targets; i++) {
1908 		struct dm_target *ti = dm_table_get_target(t, i);
1909 
1910 		if (!ti->num_secure_erase_bios)
1911 			return false;
1912 
1913 		if (!ti->type->iterate_devices ||
1914 		    ti->type->iterate_devices(ti, device_not_secure_erase_capable, NULL))
1915 			return false;
1916 	}
1917 
1918 	return true;
1919 }
1920 
1921 static int device_requires_stable_pages(struct dm_target *ti,
1922 					struct dm_dev *dev, sector_t start,
1923 					sector_t len, void *data)
1924 {
1925 	return bdev_stable_writes(dev->bdev);
1926 }
1927 
1928 int dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
1929 			      struct queue_limits *limits)
1930 {
1931 	bool wc = false, fua = false;
1932 	int r;
1933 
1934 	/*
1935 	 * Copy table's limits to the DM device's request_queue
1936 	 */
1937 	q->limits = *limits;
1938 
1939 	if (dm_table_supports_nowait(t))
1940 		blk_queue_flag_set(QUEUE_FLAG_NOWAIT, q);
1941 	else
1942 		blk_queue_flag_clear(QUEUE_FLAG_NOWAIT, q);
1943 
1944 	if (!dm_table_supports_discards(t)) {
1945 		q->limits.max_discard_sectors = 0;
1946 		q->limits.max_hw_discard_sectors = 0;
1947 		q->limits.discard_granularity = 0;
1948 		q->limits.discard_alignment = 0;
1949 		q->limits.discard_misaligned = 0;
1950 	}
1951 
1952 	if (!dm_table_supports_secure_erase(t))
1953 		q->limits.max_secure_erase_sectors = 0;
1954 
1955 	if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_WC))) {
1956 		wc = true;
1957 		if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_FUA)))
1958 			fua = true;
1959 	}
1960 	blk_queue_write_cache(q, wc, fua);
1961 
1962 	if (dm_table_supports_dax(t, device_not_dax_capable)) {
1963 		blk_queue_flag_set(QUEUE_FLAG_DAX, q);
1964 		if (dm_table_supports_dax(t, device_not_dax_synchronous_capable))
1965 			set_dax_synchronous(t->md->dax_dev);
1966 	} else
1967 		blk_queue_flag_clear(QUEUE_FLAG_DAX, q);
1968 
1969 	if (dm_table_any_dev_attr(t, device_dax_write_cache_enabled, NULL))
1970 		dax_write_cache(t->md->dax_dev, true);
1971 
1972 	/* Ensure that all underlying devices are non-rotational. */
1973 	if (dm_table_any_dev_attr(t, device_is_rotational, NULL))
1974 		blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
1975 	else
1976 		blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
1977 
1978 	if (!dm_table_supports_write_zeroes(t))
1979 		q->limits.max_write_zeroes_sectors = 0;
1980 
1981 	dm_table_verify_integrity(t);
1982 
1983 	/*
1984 	 * Some devices don't use blk_integrity but still want stable pages
1985 	 * because they do their own checksumming.
1986 	 * If any underlying device requires stable pages, a table must require
1987 	 * them as well.  Only targets that support iterate_devices are considered:
1988 	 * don't want error, zero, etc to require stable pages.
1989 	 */
1990 	if (dm_table_any_dev_attr(t, device_requires_stable_pages, NULL))
1991 		blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q);
1992 	else
1993 		blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q);
1994 
1995 	/*
1996 	 * Determine whether or not this queue's I/O timings contribute
1997 	 * to the entropy pool, Only request-based targets use this.
1998 	 * Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
1999 	 * have it set.
2000 	 */
2001 	if (blk_queue_add_random(q) &&
2002 	    dm_table_any_dev_attr(t, device_is_not_random, NULL))
2003 		blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
2004 
2005 	/*
2006 	 * For a zoned target, setup the zones related queue attributes
2007 	 * and resources necessary for zone append emulation if necessary.
2008 	 */
2009 	if (blk_queue_is_zoned(q)) {
2010 		r = dm_set_zones_restrictions(t, q);
2011 		if (r)
2012 			return r;
2013 		if (!static_key_enabled(&zoned_enabled.key))
2014 			static_branch_enable(&zoned_enabled);
2015 	}
2016 
2017 	dm_update_crypto_profile(q, t);
2018 	disk_update_readahead(t->md->disk);
2019 
2020 	/*
2021 	 * Check for request-based device is left to
2022 	 * dm_mq_init_request_queue()->blk_mq_init_allocated_queue().
2023 	 *
2024 	 * For bio-based device, only set QUEUE_FLAG_POLL when all
2025 	 * underlying devices supporting polling.
2026 	 */
2027 	if (__table_type_bio_based(t->type)) {
2028 		if (dm_table_supports_poll(t))
2029 			blk_queue_flag_set(QUEUE_FLAG_POLL, q);
2030 		else
2031 			blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
2032 	}
2033 
2034 	return 0;
2035 }
2036 
2037 struct list_head *dm_table_get_devices(struct dm_table *t)
2038 {
2039 	return &t->devices;
2040 }
2041 
2042 fmode_t dm_table_get_mode(struct dm_table *t)
2043 {
2044 	return t->mode;
2045 }
2046 EXPORT_SYMBOL(dm_table_get_mode);
2047 
2048 enum suspend_mode {
2049 	PRESUSPEND,
2050 	PRESUSPEND_UNDO,
2051 	POSTSUSPEND,
2052 };
2053 
2054 static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
2055 {
2056 	lockdep_assert_held(&t->md->suspend_lock);
2057 
2058 	for (unsigned int i = 0; i < t->num_targets; i++) {
2059 		struct dm_target *ti = dm_table_get_target(t, i);
2060 
2061 		switch (mode) {
2062 		case PRESUSPEND:
2063 			if (ti->type->presuspend)
2064 				ti->type->presuspend(ti);
2065 			break;
2066 		case PRESUSPEND_UNDO:
2067 			if (ti->type->presuspend_undo)
2068 				ti->type->presuspend_undo(ti);
2069 			break;
2070 		case POSTSUSPEND:
2071 			if (ti->type->postsuspend)
2072 				ti->type->postsuspend(ti);
2073 			break;
2074 		}
2075 	}
2076 }
2077 
2078 void dm_table_presuspend_targets(struct dm_table *t)
2079 {
2080 	if (!t)
2081 		return;
2082 
2083 	suspend_targets(t, PRESUSPEND);
2084 }
2085 
2086 void dm_table_presuspend_undo_targets(struct dm_table *t)
2087 {
2088 	if (!t)
2089 		return;
2090 
2091 	suspend_targets(t, PRESUSPEND_UNDO);
2092 }
2093 
2094 void dm_table_postsuspend_targets(struct dm_table *t)
2095 {
2096 	if (!t)
2097 		return;
2098 
2099 	suspend_targets(t, POSTSUSPEND);
2100 }
2101 
2102 int dm_table_resume_targets(struct dm_table *t)
2103 {
2104 	unsigned int i;
2105 	int r = 0;
2106 
2107 	lockdep_assert_held(&t->md->suspend_lock);
2108 
2109 	for (i = 0; i < t->num_targets; i++) {
2110 		struct dm_target *ti = dm_table_get_target(t, i);
2111 
2112 		if (!ti->type->preresume)
2113 			continue;
2114 
2115 		r = ti->type->preresume(ti);
2116 		if (r) {
2117 			DMERR("%s: %s: preresume failed, error = %d",
2118 			      dm_device_name(t->md), ti->type->name, r);
2119 			return r;
2120 		}
2121 	}
2122 
2123 	for (i = 0; i < t->num_targets; i++) {
2124 		struct dm_target *ti = dm_table_get_target(t, i);
2125 
2126 		if (ti->type->resume)
2127 			ti->type->resume(ti);
2128 	}
2129 
2130 	return 0;
2131 }
2132 
2133 struct mapped_device *dm_table_get_md(struct dm_table *t)
2134 {
2135 	return t->md;
2136 }
2137 EXPORT_SYMBOL(dm_table_get_md);
2138 
2139 const char *dm_table_device_name(struct dm_table *t)
2140 {
2141 	return dm_device_name(t->md);
2142 }
2143 EXPORT_SYMBOL_GPL(dm_table_device_name);
2144 
2145 void dm_table_run_md_queue_async(struct dm_table *t)
2146 {
2147 	if (!dm_table_request_based(t))
2148 		return;
2149 
2150 	if (t->md->queue)
2151 		blk_mq_run_hw_queues(t->md->queue, true);
2152 }
2153 EXPORT_SYMBOL(dm_table_run_md_queue_async);
2154 
2155