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