1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2001 Sistina Software (UK) Limited.
4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
6 * This file is released under the GPL.
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>
28 #define DM_MSG_PREFIX "table"
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)
35 * Similar to ceiling(log_size(n))
37 static unsigned int int_log(unsigned int n, unsigned int base)
42 n = dm_div_up(n, base);
50 * Calculate the index of the child node of the n'th node k'th key.
52 static inline unsigned int get_child(unsigned int n, unsigned int k)
54 return (n * CHILDREN_PER_NODE) + k;
58 * Return the n'th node of level l from table t.
60 static inline sector_t *get_node(struct dm_table *t,
61 unsigned int l, unsigned int n)
63 return t->index[l] + (n * KEYS_PER_NODE);
67 * Return the highest key that you could lookup from the n'th
68 * node on level l of the btree.
70 static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
72 for (; l < t->depth - 1; l++)
73 n = get_child(n, CHILDREN_PER_NODE - 1);
75 if (n >= t->counts[l])
78 return get_node(t, l, n)[KEYS_PER_NODE - 1];
82 * Fills in a level of the btree based on the highs of the level
85 static int setup_btree_index(unsigned int l, struct dm_table *t)
90 for (n = 0U; n < t->counts[l]; n++) {
91 node = get_node(t, l, n);
93 for (k = 0U; k < KEYS_PER_NODE; k++)
94 node[k] = high(t, l + 1, get_child(n, k));
101 * highs, and targets are managed as dynamic arrays during a
104 static int alloc_targets(struct dm_table *t, unsigned int num)
107 struct dm_target *n_targets;
110 * Allocate both the target array and offset array at once.
112 n_highs = kvcalloc(num, sizeof(struct dm_target) + sizeof(sector_t),
117 n_targets = (struct dm_target *) (n_highs + num);
119 memset(n_highs, -1, sizeof(*n_highs) * num);
122 t->num_allocated = num;
124 t->targets = n_targets;
129 int dm_table_create(struct dm_table **result, blk_mode_t mode,
130 unsigned int num_targets, struct mapped_device *md)
132 struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
137 INIT_LIST_HEAD(&t->devices);
140 num_targets = KEYS_PER_NODE;
142 num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
149 if (alloc_targets(t, num_targets)) {
154 t->type = DM_TYPE_NONE;
161 static void free_devices(struct list_head *devices, struct mapped_device *md)
163 struct list_head *tmp, *next;
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);
175 static void dm_table_destroy_crypto_profile(struct dm_table *t);
177 void dm_table_destroy(struct dm_table *t)
182 /* free the indexes */
184 kvfree(t->index[t->depth - 2]);
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);
193 dm_put_target_type(ti->type);
198 /* free the device list */
199 free_devices(&t->devices, t->md);
201 dm_free_md_mempools(t->mempools);
203 dm_table_destroy_crypto_profile(t);
209 * See if we've already got a device in the list.
211 static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
213 struct dm_dev_internal *dd;
215 list_for_each_entry(dd, l, list)
216 if (dd->dm_dev->bdev->bd_dev == dev)
223 * If possible, this checks an area of a destination device is invalid.
225 static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
226 sector_t start, sector_t len, void *data)
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;
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);
247 * If the target is mapped to zoned block device(s), check
248 * that the zones are not partially mapped.
250 if (bdev_is_zoned(bdev)) {
251 unsigned int zone_sectors = bdev_zone_sectors(bdev);
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,
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
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,
279 if (logical_block_size_sectors <= 1)
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);
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);
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.
307 static int upgrade_mode(struct dm_dev_internal *dd, blk_mode_t new_mode,
308 struct mapped_device *md)
311 struct dm_dev *old_dev, *new_dev;
313 old_dev = dd->dm_dev;
315 r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
316 dd->dm_dev->mode | new_mode, &new_dev);
320 dd->dm_dev = new_dev;
321 dm_put_table_device(md, old_dev);
327 * Add a device to the list, or just increment the usage count if
328 * it's already present.
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.
333 int __ref dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
334 struct dm_dev **result)
338 unsigned int major, minor;
340 struct dm_dev_internal *dd;
341 struct dm_table *t = ti->table;
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)
351 r = lookup_bdev(path, &dev);
353 if (r && system_state < SYSTEM_RUNNING)
354 r = early_lookup_bdev(path, &dev);
359 if (dev == disk_devt(t->md->disk))
362 dd = find_device(&t->devices, dev);
364 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
368 r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev);
374 refcount_set(&dd->count, 1);
375 list_add(&dd->list, &t->devices);
378 } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
379 r = upgrade_mode(dd, mode, t->md);
383 refcount_inc(&dd->count);
385 *result = dd->dm_dev;
388 EXPORT_SYMBOL(dm_get_device);
390 static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
391 sector_t start, sector_t len, void *data)
393 struct queue_limits *limits = data;
394 struct block_device *bdev = dev->bdev;
395 struct request_queue *q = bdev_get_queue(bdev);
398 DMWARN("%s: Cannot set limits for nonexistent device %pg",
399 dm_device_name(ti->table->md), bdev);
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);
417 * Decrement a device's use count and remove it if necessary.
419 void dm_put_device(struct dm_target *ti, struct dm_dev *d)
422 struct list_head *devices = &ti->table->devices;
423 struct dm_dev_internal *dd;
425 list_for_each_entry(dd, devices, list) {
426 if (dd->dm_dev == d) {
432 DMERR("%s: device %s not in table devices list",
433 dm_device_name(ti->table->md), d->name);
436 if (refcount_dec_and_test(&dd->count)) {
437 dm_put_table_device(ti->table->md, d);
442 EXPORT_SYMBOL(dm_put_device);
445 * Checks to see if the target joins onto the end of the table.
447 static int adjoin(struct dm_table *t, struct dm_target *ti)
449 struct dm_target *prev;
454 prev = &t->targets[t->num_targets - 1];
455 return (ti->begin == (prev->begin + prev->len));
459 * Used to dynamically allocate the arg array.
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.
465 * On the other hand, dm-switch needs to process bulk data using messages and
466 * excessive use of GFP_NOIO could cause trouble.
468 static char **realloc_argv(unsigned int *size, char **old_argv)
471 unsigned int new_size;
475 new_size = *size * 2;
481 argv = kmalloc_array(new_size, sizeof(*argv), gfp);
482 if (argv && old_argv) {
483 memcpy(argv, old_argv, *size * sizeof(*argv));
492 * Destructively splits up the argument list to pass to ctr.
494 int dm_split_args(int *argc, char ***argvp, char *input)
496 char *start, *end = input, *out, **argv = NULL;
497 unsigned int array_size = 0;
506 argv = realloc_argv(&array_size, argv);
511 /* Skip whitespace */
512 start = skip_spaces(end);
515 break; /* success, we hit the end */
517 /* 'out' is used to remove any back-quotes */
520 /* Everything apart from '\0' can be quoted */
521 if (*end == '\\' && *(end + 1)) {
528 break; /* end of token */
533 /* have we already filled the array ? */
534 if ((*argc + 1) > array_size) {
535 argv = realloc_argv(&array_size, argv);
540 /* we know this is whitespace */
544 /* terminate the string and put it in the array */
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.
561 static int validate_hardware_logical_block_alignment(struct dm_table *t,
562 struct queue_limits *limits)
565 * This function uses arithmetic modulo the logical_block_size
566 * (in units of 512-byte sectors).
568 unsigned short device_logical_block_size_sects =
569 limits->logical_block_size >> SECTOR_SHIFT;
572 * Offset of the start of the next table entry, mod logical_block_size.
574 unsigned short next_target_start = 0;
577 * Given an aligned bio that extends beyond the end of a
578 * target, how many sectors must the next target handle?
580 unsigned short remaining = 0;
582 struct dm_target *ti;
583 struct queue_limits ti_limits;
587 * Check each entry in the table in turn.
589 for (i = 0; i < t->num_targets; i++) {
590 ti = dm_table_get_target(t, i);
592 blk_set_stacking_limits(&ti_limits);
594 /* combine all target devices' limits */
595 if (ti->type->iterate_devices)
596 ti->type->iterate_devices(ti, dm_set_device_limits,
600 * If the remaining sectors fall entirely within this
601 * table entry are they compatible with its logical_block_size?
603 if (remaining < ti->len &&
604 remaining & ((ti_limits.logical_block_size >>
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;
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);
628 int dm_table_add_target(struct dm_table *t, const char *type,
629 sector_t start, sector_t len, char *params)
631 int r = -EINVAL, argc;
633 struct dm_target *ti;
636 DMERR("%s: target type %s must appear alone in table",
637 dm_device_name(t->md), t->targets->type->name);
641 BUG_ON(t->num_targets >= t->num_allocated);
643 ti = t->targets + t->num_targets;
644 memset(ti, 0, sizeof(*ti));
647 DMERR("%s: zero-length target", dm_device_name(t->md));
651 ti->type = dm_get_target_type(type);
653 DMERR("%s: %s: unknown target type", dm_device_name(t->md), type);
657 if (dm_target_needs_singleton(ti->type)) {
658 if (t->num_targets) {
659 ti->error = "singleton target type must appear alone in table";
665 if (dm_target_always_writeable(ti->type) &&
666 !(t->mode & BLK_OPEN_WRITE)) {
667 ti->error = "target type may not be included in a read-only table";
671 if (t->immutable_target_type) {
672 if (t->immutable_target_type != ti->type) {
673 ti->error = "immutable target type cannot be mixed with other target types";
676 } else if (dm_target_is_immutable(ti->type)) {
677 if (t->num_targets) {
678 ti->error = "immutable target type cannot be mixed with other target types";
681 t->immutable_target_type = ti->type;
684 if (dm_target_has_integrity(ti->type))
685 t->integrity_added = 1;
690 ti->error = "Unknown error";
693 * Does this target adjoin the previous one ?
695 if (!adjoin(t, ti)) {
696 ti->error = "Gap in table";
700 r = dm_split_args(&argc, &argv, params);
702 ti->error = "couldn't split parameters";
706 r = ti->type->ctr(ti, argc, argv);
711 t->highs[t->num_targets++] = ti->begin + ti->len - 1;
713 if (!ti->num_discard_bios && ti->discards_supported)
714 DMWARN("%s: %s: ignoring discards_supported because num_discard_bios is zero.",
715 dm_device_name(t->md), type);
717 if (ti->limit_swap_bios && !static_key_enabled(&swap_bios_enabled.key))
718 static_branch_enable(&swap_bios_enabled);
723 DMERR("%s: %s: %s (%pe)", dm_device_name(t->md), type, ti->error, ERR_PTR(r));
724 dm_put_target_type(ti->type);
729 * Target argument parsing helpers.
731 static int validate_next_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
732 unsigned int *value, char **error, unsigned int grouped)
734 const char *arg_str = dm_shift_arg(arg_set);
738 (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
739 (*value < arg->min) ||
740 (*value > arg->max) ||
741 (grouped && arg_set->argc < *value)) {
749 int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
750 unsigned int *value, char **error)
752 return validate_next_arg(arg, arg_set, value, error, 0);
754 EXPORT_SYMBOL(dm_read_arg);
756 int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
757 unsigned int *value, char **error)
759 return validate_next_arg(arg, arg_set, value, error, 1);
761 EXPORT_SYMBOL(dm_read_arg_group);
763 const char *dm_shift_arg(struct dm_arg_set *as)
776 EXPORT_SYMBOL(dm_shift_arg);
778 void dm_consume_args(struct dm_arg_set *as, unsigned int num_args)
780 BUG_ON(as->argc < num_args);
781 as->argc -= num_args;
782 as->argv += num_args;
784 EXPORT_SYMBOL(dm_consume_args);
786 static bool __table_type_bio_based(enum dm_queue_mode table_type)
788 return (table_type == DM_TYPE_BIO_BASED ||
789 table_type == DM_TYPE_DAX_BIO_BASED);
792 static bool __table_type_request_based(enum dm_queue_mode table_type)
794 return table_type == DM_TYPE_REQUEST_BASED;
797 void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type)
801 EXPORT_SYMBOL_GPL(dm_table_set_type);
803 /* validate the dax capability of the target device span */
804 static int device_not_dax_capable(struct dm_target *ti, struct dm_dev *dev,
805 sector_t start, sector_t len, void *data)
810 DMDEBUG("%pg: error: dax unsupported by block device", dev->bdev);
814 /* Check devices support synchronous DAX */
815 static int device_not_dax_synchronous_capable(struct dm_target *ti, struct dm_dev *dev,
816 sector_t start, sector_t len, void *data)
818 return !dev->dax_dev || !dax_synchronous(dev->dax_dev);
821 static bool dm_table_supports_dax(struct dm_table *t,
822 iterate_devices_callout_fn iterate_fn)
824 /* Ensure that all targets support DAX. */
825 for (unsigned int i = 0; i < t->num_targets; i++) {
826 struct dm_target *ti = dm_table_get_target(t, i);
828 if (!ti->type->direct_access)
831 if (!ti->type->iterate_devices ||
832 ti->type->iterate_devices(ti, iterate_fn, NULL))
839 static int device_is_rq_stackable(struct dm_target *ti, struct dm_dev *dev,
840 sector_t start, sector_t len, void *data)
842 struct block_device *bdev = dev->bdev;
843 struct request_queue *q = bdev_get_queue(bdev);
845 /* request-based cannot stack on partitions! */
846 if (bdev_is_partition(bdev))
849 return queue_is_mq(q);
852 static int dm_table_determine_type(struct dm_table *t)
854 unsigned int bio_based = 0, request_based = 0, hybrid = 0;
855 struct dm_target *ti;
856 struct list_head *devices = dm_table_get_devices(t);
857 enum dm_queue_mode live_md_type = dm_get_md_type(t->md);
859 if (t->type != DM_TYPE_NONE) {
860 /* target already set the table's type */
861 if (t->type == DM_TYPE_BIO_BASED) {
862 /* possibly upgrade to a variant of bio-based */
863 goto verify_bio_based;
865 BUG_ON(t->type == DM_TYPE_DAX_BIO_BASED);
866 goto verify_rq_based;
869 for (unsigned int i = 0; i < t->num_targets; i++) {
870 ti = dm_table_get_target(t, i);
871 if (dm_target_hybrid(ti))
873 else if (dm_target_request_based(ti))
878 if (bio_based && request_based) {
879 DMERR("Inconsistent table: different target types can't be mixed up");
884 if (hybrid && !bio_based && !request_based) {
886 * The targets can work either way.
887 * Determine the type from the live device.
888 * Default to bio-based if device is new.
890 if (__table_type_request_based(live_md_type))
898 /* We must use this table as bio-based */
899 t->type = DM_TYPE_BIO_BASED;
900 if (dm_table_supports_dax(t, device_not_dax_capable) ||
901 (list_empty(devices) && live_md_type == DM_TYPE_DAX_BIO_BASED)) {
902 t->type = DM_TYPE_DAX_BIO_BASED;
907 BUG_ON(!request_based); /* No targets in this table */
909 t->type = DM_TYPE_REQUEST_BASED;
913 * Request-based dm supports only tables that have a single target now.
914 * To support multiple targets, request splitting support is needed,
915 * and that needs lots of changes in the block-layer.
916 * (e.g. request completion process for partial completion.)
918 if (t->num_targets > 1) {
919 DMERR("request-based DM doesn't support multiple targets");
923 if (list_empty(devices)) {
925 struct dm_table *live_table = dm_get_live_table(t->md, &srcu_idx);
927 /* inherit live table's type */
929 t->type = live_table->type;
930 dm_put_live_table(t->md, srcu_idx);
934 ti = dm_table_get_immutable_target(t);
936 DMERR("table load rejected: immutable target is required");
938 } else if (ti->max_io_len) {
939 DMERR("table load rejected: immutable target that splits IO is not supported");
943 /* Non-request-stackable devices can't be used for request-based dm */
944 if (!ti->type->iterate_devices ||
945 !ti->type->iterate_devices(ti, device_is_rq_stackable, NULL)) {
946 DMERR("table load rejected: including non-request-stackable devices");
953 enum dm_queue_mode dm_table_get_type(struct dm_table *t)
958 struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
960 return t->immutable_target_type;
963 struct dm_target *dm_table_get_immutable_target(struct dm_table *t)
965 /* Immutable target is implicitly a singleton */
966 if (t->num_targets > 1 ||
967 !dm_target_is_immutable(t->targets[0].type))
973 struct dm_target *dm_table_get_wildcard_target(struct dm_table *t)
975 for (unsigned int i = 0; i < t->num_targets; i++) {
976 struct dm_target *ti = dm_table_get_target(t, i);
978 if (dm_target_is_wildcard(ti->type))
985 bool dm_table_bio_based(struct dm_table *t)
987 return __table_type_bio_based(dm_table_get_type(t));
990 bool dm_table_request_based(struct dm_table *t)
992 return __table_type_request_based(dm_table_get_type(t));
995 static bool dm_table_supports_poll(struct dm_table *t);
997 static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
999 enum dm_queue_mode type = dm_table_get_type(t);
1000 unsigned int per_io_data_size = 0, front_pad, io_front_pad;
1001 unsigned int min_pool_size = 0, pool_size;
1002 struct dm_md_mempools *pools;
1004 if (unlikely(type == DM_TYPE_NONE)) {
1005 DMERR("no table type is set, can't allocate mempools");
1009 pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
1013 if (type == DM_TYPE_REQUEST_BASED) {
1014 pool_size = dm_get_reserved_rq_based_ios();
1015 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
1019 for (unsigned int i = 0; i < t->num_targets; i++) {
1020 struct dm_target *ti = dm_table_get_target(t, i);
1022 per_io_data_size = max(per_io_data_size, ti->per_io_data_size);
1023 min_pool_size = max(min_pool_size, ti->num_flush_bios);
1025 pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
1026 front_pad = roundup(per_io_data_size,
1027 __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET;
1029 io_front_pad = roundup(per_io_data_size,
1030 __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET;
1031 if (bioset_init(&pools->io_bs, pool_size, io_front_pad,
1032 dm_table_supports_poll(t) ? BIOSET_PERCPU_CACHE : 0))
1033 goto out_free_pools;
1034 if (t->integrity_supported &&
1035 bioset_integrity_create(&pools->io_bs, pool_size))
1036 goto out_free_pools;
1038 if (bioset_init(&pools->bs, pool_size, front_pad, 0))
1039 goto out_free_pools;
1040 if (t->integrity_supported &&
1041 bioset_integrity_create(&pools->bs, pool_size))
1042 goto out_free_pools;
1044 t->mempools = pools;
1048 dm_free_md_mempools(pools);
1052 static int setup_indexes(struct dm_table *t)
1055 unsigned int total = 0;
1058 /* allocate the space for *all* the indexes */
1059 for (i = t->depth - 2; i >= 0; i--) {
1060 t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
1061 total += t->counts[i];
1064 indexes = kvcalloc(total, NODE_SIZE, GFP_KERNEL);
1068 /* set up internal nodes, bottom-up */
1069 for (i = t->depth - 2; i >= 0; i--) {
1070 t->index[i] = indexes;
1071 indexes += (KEYS_PER_NODE * t->counts[i]);
1072 setup_btree_index(i, t);
1079 * Builds the btree to index the map.
1081 static int dm_table_build_index(struct dm_table *t)
1084 unsigned int leaf_nodes;
1086 /* how many indexes will the btree have ? */
1087 leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
1088 t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
1090 /* leaf layer has already been set up */
1091 t->counts[t->depth - 1] = leaf_nodes;
1092 t->index[t->depth - 1] = t->highs;
1095 r = setup_indexes(t);
1100 static bool integrity_profile_exists(struct gendisk *disk)
1102 return !!blk_get_integrity(disk);
1106 * Get a disk whose integrity profile reflects the table's profile.
1107 * Returns NULL if integrity support was inconsistent or unavailable.
1109 static struct gendisk *dm_table_get_integrity_disk(struct dm_table *t)
1111 struct list_head *devices = dm_table_get_devices(t);
1112 struct dm_dev_internal *dd = NULL;
1113 struct gendisk *prev_disk = NULL, *template_disk = NULL;
1115 for (unsigned int i = 0; i < t->num_targets; i++) {
1116 struct dm_target *ti = dm_table_get_target(t, i);
1118 if (!dm_target_passes_integrity(ti->type))
1122 list_for_each_entry(dd, devices, list) {
1123 template_disk = dd->dm_dev->bdev->bd_disk;
1124 if (!integrity_profile_exists(template_disk))
1126 else if (prev_disk &&
1127 blk_integrity_compare(prev_disk, template_disk) < 0)
1129 prev_disk = template_disk;
1132 return template_disk;
1136 DMWARN("%s: integrity not set: %s and %s profile mismatch",
1137 dm_device_name(t->md),
1138 prev_disk->disk_name,
1139 template_disk->disk_name);
1144 * Register the mapped device for blk_integrity support if the
1145 * underlying devices have an integrity profile. But all devices may
1146 * not have matching profiles (checking all devices isn't reliable
1147 * during table load because this table may use other DM device(s) which
1148 * must be resumed before they will have an initialized integity
1149 * profile). Consequently, stacked DM devices force a 2 stage integrity
1150 * profile validation: First pass during table load, final pass during
1153 static int dm_table_register_integrity(struct dm_table *t)
1155 struct mapped_device *md = t->md;
1156 struct gendisk *template_disk = NULL;
1158 /* If target handles integrity itself do not register it here. */
1159 if (t->integrity_added)
1162 template_disk = dm_table_get_integrity_disk(t);
1166 if (!integrity_profile_exists(dm_disk(md))) {
1167 t->integrity_supported = true;
1169 * Register integrity profile during table load; we can do
1170 * this because the final profile must match during resume.
1172 blk_integrity_register(dm_disk(md),
1173 blk_get_integrity(template_disk));
1178 * If DM device already has an initialized integrity
1179 * profile the new profile should not conflict.
1181 if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
1182 DMERR("%s: conflict with existing integrity profile: %s profile mismatch",
1183 dm_device_name(t->md),
1184 template_disk->disk_name);
1188 /* Preserve existing integrity profile */
1189 t->integrity_supported = true;
1193 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1195 struct dm_crypto_profile {
1196 struct blk_crypto_profile profile;
1197 struct mapped_device *md;
1200 static int dm_keyslot_evict_callback(struct dm_target *ti, struct dm_dev *dev,
1201 sector_t start, sector_t len, void *data)
1203 const struct blk_crypto_key *key = data;
1205 blk_crypto_evict_key(dev->bdev, key);
1210 * When an inline encryption key is evicted from a device-mapper device, evict
1211 * it from all the underlying devices.
1213 static int dm_keyslot_evict(struct blk_crypto_profile *profile,
1214 const struct blk_crypto_key *key, unsigned int slot)
1216 struct mapped_device *md =
1217 container_of(profile, struct dm_crypto_profile, profile)->md;
1221 t = dm_get_live_table(md, &srcu_idx);
1225 for (unsigned int i = 0; i < t->num_targets; i++) {
1226 struct dm_target *ti = dm_table_get_target(t, i);
1228 if (!ti->type->iterate_devices)
1230 ti->type->iterate_devices(ti, dm_keyslot_evict_callback,
1234 dm_put_live_table(md, srcu_idx);
1239 device_intersect_crypto_capabilities(struct dm_target *ti, struct dm_dev *dev,
1240 sector_t start, sector_t len, void *data)
1242 struct blk_crypto_profile *parent = data;
1243 struct blk_crypto_profile *child =
1244 bdev_get_queue(dev->bdev)->crypto_profile;
1246 blk_crypto_intersect_capabilities(parent, child);
1250 void dm_destroy_crypto_profile(struct blk_crypto_profile *profile)
1252 struct dm_crypto_profile *dmcp = container_of(profile,
1253 struct dm_crypto_profile,
1259 blk_crypto_profile_destroy(profile);
1263 static void dm_table_destroy_crypto_profile(struct dm_table *t)
1265 dm_destroy_crypto_profile(t->crypto_profile);
1266 t->crypto_profile = NULL;
1270 * Constructs and initializes t->crypto_profile with a crypto profile that
1271 * represents the common set of crypto capabilities of the devices described by
1272 * the dm_table. However, if the constructed crypto profile doesn't support all
1273 * crypto capabilities that are supported by the current mapped_device, it
1274 * returns an error instead, since we don't support removing crypto capabilities
1275 * on table changes. Finally, if the constructed crypto profile is "empty" (has
1276 * no crypto capabilities at all), it just sets t->crypto_profile to NULL.
1278 static int dm_table_construct_crypto_profile(struct dm_table *t)
1280 struct dm_crypto_profile *dmcp;
1281 struct blk_crypto_profile *profile;
1283 bool empty_profile = true;
1285 dmcp = kmalloc(sizeof(*dmcp), GFP_KERNEL);
1290 profile = &dmcp->profile;
1291 blk_crypto_profile_init(profile, 0);
1292 profile->ll_ops.keyslot_evict = dm_keyslot_evict;
1293 profile->max_dun_bytes_supported = UINT_MAX;
1294 memset(profile->modes_supported, 0xFF,
1295 sizeof(profile->modes_supported));
1297 for (i = 0; i < t->num_targets; i++) {
1298 struct dm_target *ti = dm_table_get_target(t, i);
1300 if (!dm_target_passes_crypto(ti->type)) {
1301 blk_crypto_intersect_capabilities(profile, NULL);
1304 if (!ti->type->iterate_devices)
1306 ti->type->iterate_devices(ti,
1307 device_intersect_crypto_capabilities,
1312 !blk_crypto_has_capabilities(profile,
1313 t->md->queue->crypto_profile)) {
1314 DMERR("Inline encryption capabilities of new DM table were more restrictive than the old table's. This is not supported!");
1315 dm_destroy_crypto_profile(profile);
1320 * If the new profile doesn't actually support any crypto capabilities,
1321 * we may as well represent it with a NULL profile.
1323 for (i = 0; i < ARRAY_SIZE(profile->modes_supported); i++) {
1324 if (profile->modes_supported[i]) {
1325 empty_profile = false;
1330 if (empty_profile) {
1331 dm_destroy_crypto_profile(profile);
1336 * t->crypto_profile is only set temporarily while the table is being
1337 * set up, and it gets set to NULL after the profile has been
1338 * transferred to the request_queue.
1340 t->crypto_profile = profile;
1345 static void dm_update_crypto_profile(struct request_queue *q,
1348 if (!t->crypto_profile)
1351 /* Make the crypto profile less restrictive. */
1352 if (!q->crypto_profile) {
1353 blk_crypto_register(t->crypto_profile, q);
1355 blk_crypto_update_capabilities(q->crypto_profile,
1357 dm_destroy_crypto_profile(t->crypto_profile);
1359 t->crypto_profile = NULL;
1362 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1364 static int dm_table_construct_crypto_profile(struct dm_table *t)
1369 void dm_destroy_crypto_profile(struct blk_crypto_profile *profile)
1373 static void dm_table_destroy_crypto_profile(struct dm_table *t)
1377 static void dm_update_crypto_profile(struct request_queue *q,
1382 #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */
1385 * Prepares the table for use by building the indices,
1386 * setting the type, and allocating mempools.
1388 int dm_table_complete(struct dm_table *t)
1392 r = dm_table_determine_type(t);
1394 DMERR("unable to determine table type");
1398 r = dm_table_build_index(t);
1400 DMERR("unable to build btrees");
1404 r = dm_table_register_integrity(t);
1406 DMERR("could not register integrity profile.");
1410 r = dm_table_construct_crypto_profile(t);
1412 DMERR("could not construct crypto profile.");
1416 r = dm_table_alloc_md_mempools(t, t->md);
1418 DMERR("unable to allocate mempools");
1423 static DEFINE_MUTEX(_event_lock);
1424 void dm_table_event_callback(struct dm_table *t,
1425 void (*fn)(void *), void *context)
1427 mutex_lock(&_event_lock);
1429 t->event_context = context;
1430 mutex_unlock(&_event_lock);
1433 void dm_table_event(struct dm_table *t)
1435 mutex_lock(&_event_lock);
1437 t->event_fn(t->event_context);
1438 mutex_unlock(&_event_lock);
1440 EXPORT_SYMBOL(dm_table_event);
1442 inline sector_t dm_table_get_size(struct dm_table *t)
1444 return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
1446 EXPORT_SYMBOL(dm_table_get_size);
1449 * Search the btree for the correct target.
1451 * Caller should check returned pointer for NULL
1452 * to trap I/O beyond end of device.
1454 struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
1456 unsigned int l, n = 0, k = 0;
1459 if (unlikely(sector >= dm_table_get_size(t)))
1462 for (l = 0; l < t->depth; l++) {
1463 n = get_child(n, k);
1464 node = get_node(t, l, n);
1466 for (k = 0; k < KEYS_PER_NODE; k++)
1467 if (node[k] >= sector)
1471 return &t->targets[(KEYS_PER_NODE * n) + k];
1474 static int device_not_poll_capable(struct dm_target *ti, struct dm_dev *dev,
1475 sector_t start, sector_t len, void *data)
1477 struct request_queue *q = bdev_get_queue(dev->bdev);
1479 return !test_bit(QUEUE_FLAG_POLL, &q->queue_flags);
1483 * type->iterate_devices() should be called when the sanity check needs to
1484 * iterate and check all underlying data devices. iterate_devices() will
1485 * iterate all underlying data devices until it encounters a non-zero return
1486 * code, returned by whether the input iterate_devices_callout_fn, or
1487 * iterate_devices() itself internally.
1489 * For some target type (e.g. dm-stripe), one call of iterate_devices() may
1490 * iterate multiple underlying devices internally, in which case a non-zero
1491 * return code returned by iterate_devices_callout_fn will stop the iteration
1494 * Cases requiring _any_ underlying device supporting some kind of attribute,
1495 * should use the iteration structure like dm_table_any_dev_attr(), or call
1496 * it directly. @func should handle semantics of positive examples, e.g.
1497 * capable of something.
1499 * Cases requiring _all_ underlying devices supporting some kind of attribute,
1500 * should use the iteration structure like dm_table_supports_nowait() or
1501 * dm_table_supports_discards(). Or introduce dm_table_all_devs_attr() that
1502 * uses an @anti_func that handle semantics of counter examples, e.g. not
1503 * capable of something. So: return !dm_table_any_dev_attr(t, anti_func, data);
1505 static bool dm_table_any_dev_attr(struct dm_table *t,
1506 iterate_devices_callout_fn func, void *data)
1508 for (unsigned int i = 0; i < t->num_targets; i++) {
1509 struct dm_target *ti = dm_table_get_target(t, i);
1511 if (ti->type->iterate_devices &&
1512 ti->type->iterate_devices(ti, func, data))
1519 static int count_device(struct dm_target *ti, struct dm_dev *dev,
1520 sector_t start, sector_t len, void *data)
1522 unsigned int *num_devices = data;
1529 static bool dm_table_supports_poll(struct dm_table *t)
1531 for (unsigned int i = 0; i < t->num_targets; i++) {
1532 struct dm_target *ti = dm_table_get_target(t, i);
1534 if (!ti->type->iterate_devices ||
1535 ti->type->iterate_devices(ti, device_not_poll_capable, NULL))
1543 * Check whether a table has no data devices attached using each
1544 * target's iterate_devices method.
1545 * Returns false if the result is unknown because a target doesn't
1546 * support iterate_devices.
1548 bool dm_table_has_no_data_devices(struct dm_table *t)
1550 for (unsigned int i = 0; i < t->num_targets; i++) {
1551 struct dm_target *ti = dm_table_get_target(t, i);
1552 unsigned int num_devices = 0;
1554 if (!ti->type->iterate_devices)
1557 ti->type->iterate_devices(ti, count_device, &num_devices);
1565 static int device_not_zoned_model(struct dm_target *ti, struct dm_dev *dev,
1566 sector_t start, sector_t len, void *data)
1568 struct request_queue *q = bdev_get_queue(dev->bdev);
1569 enum blk_zoned_model *zoned_model = data;
1571 return blk_queue_zoned_model(q) != *zoned_model;
1575 * Check the device zoned model based on the target feature flag. If the target
1576 * has the DM_TARGET_ZONED_HM feature flag set, host-managed zoned devices are
1577 * also accepted but all devices must have the same zoned model. If the target
1578 * has the DM_TARGET_MIXED_ZONED_MODEL feature set, the devices can have any
1579 * zoned model with all zoned devices having the same zone size.
1581 static bool dm_table_supports_zoned_model(struct dm_table *t,
1582 enum blk_zoned_model zoned_model)
1584 for (unsigned int i = 0; i < t->num_targets; i++) {
1585 struct dm_target *ti = dm_table_get_target(t, i);
1587 if (dm_target_supports_zoned_hm(ti->type)) {
1588 if (!ti->type->iterate_devices ||
1589 ti->type->iterate_devices(ti, device_not_zoned_model,
1592 } else if (!dm_target_supports_mixed_zoned_model(ti->type)) {
1593 if (zoned_model == BLK_ZONED_HM)
1601 static int device_not_matches_zone_sectors(struct dm_target *ti, struct dm_dev *dev,
1602 sector_t start, sector_t len, void *data)
1604 unsigned int *zone_sectors = data;
1606 if (!bdev_is_zoned(dev->bdev))
1608 return bdev_zone_sectors(dev->bdev) != *zone_sectors;
1612 * Check consistency of zoned model and zone sectors across all targets. For
1613 * zone sectors, if the destination device is a zoned block device, it shall
1614 * have the specified zone_sectors.
1616 static int validate_hardware_zoned_model(struct dm_table *t,
1617 enum blk_zoned_model zoned_model,
1618 unsigned int zone_sectors)
1620 if (zoned_model == BLK_ZONED_NONE)
1623 if (!dm_table_supports_zoned_model(t, zoned_model)) {
1624 DMERR("%s: zoned model is not consistent across all devices",
1625 dm_device_name(t->md));
1629 /* Check zone size validity and compatibility */
1630 if (!zone_sectors || !is_power_of_2(zone_sectors))
1633 if (dm_table_any_dev_attr(t, device_not_matches_zone_sectors, &zone_sectors)) {
1634 DMERR("%s: zone sectors is not consistent across all zoned devices",
1635 dm_device_name(t->md));
1643 * Establish the new table's queue_limits and validate them.
1645 int dm_calculate_queue_limits(struct dm_table *t,
1646 struct queue_limits *limits)
1648 struct queue_limits ti_limits;
1649 enum blk_zoned_model zoned_model = BLK_ZONED_NONE;
1650 unsigned int zone_sectors = 0;
1652 blk_set_stacking_limits(limits);
1654 for (unsigned int i = 0; i < t->num_targets; i++) {
1655 struct dm_target *ti = dm_table_get_target(t, i);
1657 blk_set_stacking_limits(&ti_limits);
1659 if (!ti->type->iterate_devices) {
1660 /* Set I/O hints portion of queue limits */
1661 if (ti->type->io_hints)
1662 ti->type->io_hints(ti, &ti_limits);
1663 goto combine_limits;
1667 * Combine queue limits of all the devices this target uses.
1669 ti->type->iterate_devices(ti, dm_set_device_limits,
1672 if (zoned_model == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
1674 * After stacking all limits, validate all devices
1675 * in table support this zoned model and zone sectors.
1677 zoned_model = ti_limits.zoned;
1678 zone_sectors = ti_limits.chunk_sectors;
1681 /* Set I/O hints portion of queue limits */
1682 if (ti->type->io_hints)
1683 ti->type->io_hints(ti, &ti_limits);
1686 * Check each device area is consistent with the target's
1687 * overall queue limits.
1689 if (ti->type->iterate_devices(ti, device_area_is_invalid,
1695 * Merge this target's queue limits into the overall limits
1698 if (blk_stack_limits(limits, &ti_limits, 0) < 0)
1699 DMWARN("%s: adding target device (start sect %llu len %llu) "
1700 "caused an alignment inconsistency",
1701 dm_device_name(t->md),
1702 (unsigned long long) ti->begin,
1703 (unsigned long long) ti->len);
1707 * Verify that the zoned model and zone sectors, as determined before
1708 * any .io_hints override, are the same across all devices in the table.
1709 * - this is especially relevant if .io_hints is emulating a disk-managed
1710 * zoned model (aka BLK_ZONED_NONE) on host-managed zoned block devices.
1713 if (limits->zoned != BLK_ZONED_NONE) {
1715 * ...IF the above limits stacking determined a zoned model
1716 * validate that all of the table's devices conform to it.
1718 zoned_model = limits->zoned;
1719 zone_sectors = limits->chunk_sectors;
1721 if (validate_hardware_zoned_model(t, zoned_model, zone_sectors))
1724 return validate_hardware_logical_block_alignment(t, limits);
1728 * Verify that all devices have an integrity profile that matches the
1729 * DM device's registered integrity profile. If the profiles don't
1730 * match then unregister the DM device's integrity profile.
1732 static void dm_table_verify_integrity(struct dm_table *t)
1734 struct gendisk *template_disk = NULL;
1736 if (t->integrity_added)
1739 if (t->integrity_supported) {
1741 * Verify that the original integrity profile
1742 * matches all the devices in this table.
1744 template_disk = dm_table_get_integrity_disk(t);
1745 if (template_disk &&
1746 blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
1750 if (integrity_profile_exists(dm_disk(t->md))) {
1751 DMWARN("%s: unable to establish an integrity profile",
1752 dm_device_name(t->md));
1753 blk_integrity_unregister(dm_disk(t->md));
1757 static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
1758 sector_t start, sector_t len, void *data)
1760 unsigned long flush = (unsigned long) data;
1761 struct request_queue *q = bdev_get_queue(dev->bdev);
1763 return (q->queue_flags & flush);
1766 static bool dm_table_supports_flush(struct dm_table *t, unsigned long flush)
1769 * Require at least one underlying device to support flushes.
1770 * t->devices includes internal dm devices such as mirror logs
1771 * so we need to use iterate_devices here, which targets
1772 * supporting flushes must provide.
1774 for (unsigned int i = 0; i < t->num_targets; i++) {
1775 struct dm_target *ti = dm_table_get_target(t, i);
1777 if (!ti->num_flush_bios)
1780 if (ti->flush_supported)
1783 if (ti->type->iterate_devices &&
1784 ti->type->iterate_devices(ti, device_flush_capable, (void *) flush))
1791 static int device_dax_write_cache_enabled(struct dm_target *ti,
1792 struct dm_dev *dev, sector_t start,
1793 sector_t len, void *data)
1795 struct dax_device *dax_dev = dev->dax_dev;
1800 if (dax_write_cache_enabled(dax_dev))
1805 static int device_is_rotational(struct dm_target *ti, struct dm_dev *dev,
1806 sector_t start, sector_t len, void *data)
1808 return !bdev_nonrot(dev->bdev);
1811 static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
1812 sector_t start, sector_t len, void *data)
1814 struct request_queue *q = bdev_get_queue(dev->bdev);
1816 return !blk_queue_add_random(q);
1819 static int device_not_write_zeroes_capable(struct dm_target *ti, struct dm_dev *dev,
1820 sector_t start, sector_t len, void *data)
1822 struct request_queue *q = bdev_get_queue(dev->bdev);
1824 return !q->limits.max_write_zeroes_sectors;
1827 static bool dm_table_supports_write_zeroes(struct dm_table *t)
1829 for (unsigned int i = 0; i < t->num_targets; i++) {
1830 struct dm_target *ti = dm_table_get_target(t, i);
1832 if (!ti->num_write_zeroes_bios)
1835 if (!ti->type->iterate_devices ||
1836 ti->type->iterate_devices(ti, device_not_write_zeroes_capable, NULL))
1843 static int device_not_nowait_capable(struct dm_target *ti, struct dm_dev *dev,
1844 sector_t start, sector_t len, void *data)
1846 return !bdev_nowait(dev->bdev);
1849 static bool dm_table_supports_nowait(struct dm_table *t)
1851 for (unsigned int i = 0; i < t->num_targets; i++) {
1852 struct dm_target *ti = dm_table_get_target(t, i);
1854 if (!dm_target_supports_nowait(ti->type))
1857 if (!ti->type->iterate_devices ||
1858 ti->type->iterate_devices(ti, device_not_nowait_capable, NULL))
1865 static int device_not_discard_capable(struct dm_target *ti, struct dm_dev *dev,
1866 sector_t start, sector_t len, void *data)
1868 return !bdev_max_discard_sectors(dev->bdev);
1871 static bool dm_table_supports_discards(struct dm_table *t)
1873 for (unsigned int i = 0; i < t->num_targets; i++) {
1874 struct dm_target *ti = dm_table_get_target(t, i);
1876 if (!ti->num_discard_bios)
1880 * Either the target provides discard support (as implied by setting
1881 * 'discards_supported') or it relies on _all_ data devices having
1884 if (!ti->discards_supported &&
1885 (!ti->type->iterate_devices ||
1886 ti->type->iterate_devices(ti, device_not_discard_capable, NULL)))
1893 static int device_not_secure_erase_capable(struct dm_target *ti,
1894 struct dm_dev *dev, sector_t start,
1895 sector_t len, void *data)
1897 return !bdev_max_secure_erase_sectors(dev->bdev);
1900 static bool dm_table_supports_secure_erase(struct dm_table *t)
1902 for (unsigned int i = 0; i < t->num_targets; i++) {
1903 struct dm_target *ti = dm_table_get_target(t, i);
1905 if (!ti->num_secure_erase_bios)
1908 if (!ti->type->iterate_devices ||
1909 ti->type->iterate_devices(ti, device_not_secure_erase_capable, NULL))
1916 static int device_requires_stable_pages(struct dm_target *ti,
1917 struct dm_dev *dev, sector_t start,
1918 sector_t len, void *data)
1920 return bdev_stable_writes(dev->bdev);
1923 int dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
1924 struct queue_limits *limits)
1926 bool wc = false, fua = false;
1930 * Copy table's limits to the DM device's request_queue
1932 q->limits = *limits;
1934 if (dm_table_supports_nowait(t))
1935 blk_queue_flag_set(QUEUE_FLAG_NOWAIT, q);
1937 blk_queue_flag_clear(QUEUE_FLAG_NOWAIT, q);
1939 if (!dm_table_supports_discards(t)) {
1940 q->limits.max_discard_sectors = 0;
1941 q->limits.max_hw_discard_sectors = 0;
1942 q->limits.discard_granularity = 0;
1943 q->limits.discard_alignment = 0;
1944 q->limits.discard_misaligned = 0;
1947 if (!dm_table_supports_secure_erase(t))
1948 q->limits.max_secure_erase_sectors = 0;
1950 if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_WC))) {
1952 if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_FUA)))
1955 blk_queue_write_cache(q, wc, fua);
1957 if (dm_table_supports_dax(t, device_not_dax_capable)) {
1958 blk_queue_flag_set(QUEUE_FLAG_DAX, q);
1959 if (dm_table_supports_dax(t, device_not_dax_synchronous_capable))
1960 set_dax_synchronous(t->md->dax_dev);
1962 blk_queue_flag_clear(QUEUE_FLAG_DAX, q);
1964 if (dm_table_any_dev_attr(t, device_dax_write_cache_enabled, NULL))
1965 dax_write_cache(t->md->dax_dev, true);
1967 /* Ensure that all underlying devices are non-rotational. */
1968 if (dm_table_any_dev_attr(t, device_is_rotational, NULL))
1969 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
1971 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
1973 if (!dm_table_supports_write_zeroes(t))
1974 q->limits.max_write_zeroes_sectors = 0;
1976 dm_table_verify_integrity(t);
1979 * Some devices don't use blk_integrity but still want stable pages
1980 * because they do their own checksumming.
1981 * If any underlying device requires stable pages, a table must require
1982 * them as well. Only targets that support iterate_devices are considered:
1983 * don't want error, zero, etc to require stable pages.
1985 if (dm_table_any_dev_attr(t, device_requires_stable_pages, NULL))
1986 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q);
1988 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q);
1991 * Determine whether or not this queue's I/O timings contribute
1992 * to the entropy pool, Only request-based targets use this.
1993 * Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
1996 if (blk_queue_add_random(q) &&
1997 dm_table_any_dev_attr(t, device_is_not_random, NULL))
1998 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
2001 * For a zoned target, setup the zones related queue attributes
2002 * and resources necessary for zone append emulation if necessary.
2004 if (blk_queue_is_zoned(q)) {
2005 r = dm_set_zones_restrictions(t, q);
2008 if (!static_key_enabled(&zoned_enabled.key))
2009 static_branch_enable(&zoned_enabled);
2012 dm_update_crypto_profile(q, t);
2013 disk_update_readahead(t->md->disk);
2016 * Check for request-based device is left to
2017 * dm_mq_init_request_queue()->blk_mq_init_allocated_queue().
2019 * For bio-based device, only set QUEUE_FLAG_POLL when all
2020 * underlying devices supporting polling.
2022 if (__table_type_bio_based(t->type)) {
2023 if (dm_table_supports_poll(t))
2024 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
2026 blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
2032 struct list_head *dm_table_get_devices(struct dm_table *t)
2037 blk_mode_t dm_table_get_mode(struct dm_table *t)
2041 EXPORT_SYMBOL(dm_table_get_mode);
2049 static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
2051 lockdep_assert_held(&t->md->suspend_lock);
2053 for (unsigned int i = 0; i < t->num_targets; i++) {
2054 struct dm_target *ti = dm_table_get_target(t, i);
2058 if (ti->type->presuspend)
2059 ti->type->presuspend(ti);
2061 case PRESUSPEND_UNDO:
2062 if (ti->type->presuspend_undo)
2063 ti->type->presuspend_undo(ti);
2066 if (ti->type->postsuspend)
2067 ti->type->postsuspend(ti);
2073 void dm_table_presuspend_targets(struct dm_table *t)
2078 suspend_targets(t, PRESUSPEND);
2081 void dm_table_presuspend_undo_targets(struct dm_table *t)
2086 suspend_targets(t, PRESUSPEND_UNDO);
2089 void dm_table_postsuspend_targets(struct dm_table *t)
2094 suspend_targets(t, POSTSUSPEND);
2097 int dm_table_resume_targets(struct dm_table *t)
2102 lockdep_assert_held(&t->md->suspend_lock);
2104 for (i = 0; i < t->num_targets; i++) {
2105 struct dm_target *ti = dm_table_get_target(t, i);
2107 if (!ti->type->preresume)
2110 r = ti->type->preresume(ti);
2112 DMERR("%s: %s: preresume failed, error = %d",
2113 dm_device_name(t->md), ti->type->name, r);
2118 for (i = 0; i < t->num_targets; i++) {
2119 struct dm_target *ti = dm_table_get_target(t, i);
2121 if (ti->type->resume)
2122 ti->type->resume(ti);
2128 struct mapped_device *dm_table_get_md(struct dm_table *t)
2132 EXPORT_SYMBOL(dm_table_get_md);
2134 const char *dm_table_device_name(struct dm_table *t)
2136 return dm_device_name(t->md);
2138 EXPORT_SYMBOL_GPL(dm_table_device_name);
2140 void dm_table_run_md_queue_async(struct dm_table *t)
2142 if (!dm_table_request_based(t))
2146 blk_mq_run_hw_queues(t->md->queue, true);
2148 EXPORT_SYMBOL(dm_table_run_md_queue_async);