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