10 #include <linux/module.h>
14 #include <linux/ctype.h>
15 #include <linux/string.h>
16 #include <linux/slab.h>
22 #define DM_MSG_PREFIX "table"
25 #define NODE_SIZE L1_CACHE_BYTES
26 #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
27 #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
83 static unsigned int int_log(
unsigned int n,
unsigned int base)
98 static inline unsigned int get_child(
unsigned int n,
unsigned int k)
107 unsigned int l,
unsigned int n)
118 for (; l < t->
depth - 1; l++)
131 static int setup_btree_index(
unsigned int l,
struct dm_table *t)
136 for (n = 0
U; n < t->
counts[
l]; n++) {
137 node = get_node(t, l, n);
140 node[k] =
high(t, l + 1, get_child(n, k));
146 void *
dm_vcalloc(
unsigned long nmemb,
unsigned long elem_size)
157 size = nmemb * elem_size;
168 static int alloc_targets(
struct dm_table *t,
unsigned int num)
184 n_targets = (
struct dm_target *) (n_highs + num);
191 memset(n_highs + n, -1,
sizeof(*n_highs) * (num - n));
216 num_targets =
dm_round_up(num_targets, KEYS_PER_NODE);
218 if (alloc_targets(t, num_targets)) {
237 DMWARN(
"dm_table_destroy: dm_put_device call missing for %s",
297 static inline int check_space(
struct dm_table *t)
325 static char *_claim_ptr =
"I belong to device-mapper";
334 return PTR_ERR(bdev);
336 r = bd_link_disk_holder(bdev,
dm_disk(md));
342 d->dm_dev.bdev = bdev;
362 static int device_area_is_invalid(
struct dm_target *ti,
struct dm_dev *dev,
366 struct queue_limits *limits =
data;
370 unsigned short logical_block_size_sectors =
379 q = bdev_get_queue(bdev);
380 if (!q || !q->make_request_fn) {
381 DMWARN(
"%s: %s is not yet initialised: "
382 "start=%llu, len=%llu, dev_size=%llu",
384 (
unsigned long long)start,
385 (
unsigned long long)len,
386 (
unsigned long long)dev_size);
393 if ((start >= dev_size) || (start + len > dev_size)) {
394 DMWARN(
"%s: %s too small for target: "
395 "start=%llu, len=%llu, dev_size=%llu",
397 (
unsigned long long)start,
398 (
unsigned long long)len,
399 (
unsigned long long)dev_size);
403 if (logical_block_size_sectors <= 1)
406 if (start & (logical_block_size_sectors - 1)) {
407 DMWARN(
"%s: start=%llu not aligned to h/w "
408 "logical block size %u of %s",
410 (
unsigned long long)start,
411 limits->logical_block_size,
bdevname(bdev, b));
415 if (len & (logical_block_size_sectors - 1)) {
416 DMWARN(
"%s: len=%llu not aligned to h/w "
417 "logical block size %u of %s",
419 (
unsigned long long)len,
420 limits->logical_block_size,
bdevname(bdev, b));
439 dd_new = dd_old = *
dd;
441 dd_new.dm_dev.mode |= new_mode;
442 dd_new.dm_dev.bdev =
NULL;
444 r = open_dev(&dd_new, dd->
dm_dev.bdev->bd_dev, md);
448 dd->
dm_dev.mode |= new_mode;
449 close_dev(&dd_old, md);
470 if (
sscanf(path,
"%u:%u%c", &major, &minor, &dummy) == 2) {
472 dev =
MKDEV(major, minor);
473 if (
MAJOR(dev) != major ||
MINOR(dev) != minor)
480 return PTR_ERR(bdev);
485 dd = find_device(&t->
devices, dev);
494 if ((r = open_dev(dd, dev, t->
md))) {
504 }
else if (dd->
dm_dev.mode != (mode | dd->
dm_dev.mode)) {
505 r = upgrade_mode(dd, mode, t->
md);
519 struct queue_limits *limits =
data;
525 DMWARN(
"%s: Cannot set limits for nonexistent device %s",
531 DMWARN(
"%s: adding target device %s caused an alignment inconsistency: "
532 "physical_block_size=%u, logical_block_size=%u, "
533 "alignment_offset=%u, start=%llu",
535 q->limits.physical_block_size,
536 q->limits.logical_block_size,
537 q->limits.alignment_offset,
561 close_dev(dd, ti->
table->md);
585 static char **realloc_argv(
unsigned *array_size,
char **old_argv)
590 new_size = *array_size ? *array_size * 2 : 64;
593 memcpy(argv, old_argv, *array_size *
sizeof(*argv));
594 *array_size = new_size;
607 unsigned array_size = 0;
616 argv = realloc_argv(&array_size, argv);
631 if (*end ==
'\\' && *(end + 1)) {
644 if ((*argc + 1) > array_size) {
645 argv = realloc_argv(&array_size, argv);
671 static int validate_hardware_logical_block_alignment(
struct dm_table *table,
672 struct queue_limits *limits)
678 unsigned short device_logical_block_size_sects =
684 unsigned short next_target_start = 0;
690 unsigned short remaining = 0;
693 struct queue_limits ti_limits;
705 if (ti->
type->iterate_devices)
713 if (remaining < ti->len &&
714 remaining & ((ti_limits.logical_block_size >>
719 (
unsigned short) ((next_target_start + ti->
len) &
720 (device_logical_block_size_sects - 1));
721 remaining = next_target_start ?
722 device_logical_block_size_sects - next_target_start : 0;
726 DMWARN(
"%s: table line %u (start sect %llu len %llu) "
727 "not aligned to h/w logical block size %u",
729 (
unsigned long long) ti->
begin,
730 (
unsigned long long) ti->
len,
731 limits->logical_block_size);
746 DMERR(
"%s: target type %s must appear alone in table",
751 if ((r = check_space(t)))
755 memset(tgt, 0,
sizeof(*tgt));
771 DMERR(
"%s: target type %s must appear alone in table",
779 DMERR(
"%s: target type %s may not be included in read-only tables",
786 DMERR(
"%s: immutable target type %s cannot be mixed with other target types",
792 DMERR(
"%s: immutable target type %s cannot be mixed with other target types",
802 tgt->
error =
"Unknown error";
807 if (!adjoin(t, tgt)) {
808 tgt->
error =
"Gap in table";
815 tgt->
error =
"couldn't split parameters (insufficient memory)";
819 r = tgt->
type->ctr(tgt, argc, argv);
827 DMWARN(
"%s: %s: ignoring discards_supported because num_discard_requests is zero.",
842 unsigned *
value,
char **
error,
unsigned grouped)
848 (
sscanf(arg_str,
"%u%c", value, &dummy) != 1) ||
849 (*value < arg->
min) ||
850 (*value > arg->
max) ||
851 (grouped && arg_set->
argc < *value)) {
860 unsigned *value,
char **error)
862 return validate_next_arg(arg, arg_set, value, error, 0);
867 unsigned *value,
char **error)
869 return validate_next_arg(arg, arg_set, value, error, 1);
891 as->
argc -= num_args;
892 as->
argv += num_args;
896 static int dm_table_set_type(
struct dm_table *t)
899 unsigned bio_based = 0, request_based = 0;
911 if (bio_based && request_based) {
912 DMWARN(
"Inconsistent table: different target types"
913 " can't be mixed up");
929 if (!blk_queue_stackable(bdev_get_queue(dd->
dm_dev.bdev))) {
930 DMWARN(
"table load rejected: including"
931 " non-request-stackable devices");
943 DMWARN(
"Request-based dm doesn't support multiple targets yet");
972 DMWARN(
"no table type is set, can't allocate mempools");
994 static int setup_indexes(
struct dm_table *t)
997 unsigned int total = 0;
1001 for (i = t->
depth - 2; i >= 0; i--) {
1011 for (i = t->
depth - 2; i >= 0; i--) {
1013 indexes += (KEYS_PER_NODE * t->
counts[
i]);
1014 setup_btree_index(i, t);
1023 static int dm_table_build_index(
struct dm_table *t)
1026 unsigned int leaf_nodes;
1037 r = setup_indexes(t);
1049 static struct gendisk * dm_table_get_integrity_disk(
struct dm_table *t,
1054 struct gendisk *prev_disk =
NULL, *template_disk =
NULL;
1057 template_disk = dd->
dm_dev.bdev->bd_disk;
1058 if (!blk_get_integrity(template_disk))
1062 else if (prev_disk &&
1065 prev_disk = template_disk;
1068 return template_disk;
1072 DMWARN(
"%s: integrity not set: %s and %s profile mismatch",
1074 prev_disk->disk_name,
1075 template_disk->disk_name);
1091 struct gendisk *template_disk =
NULL;
1093 template_disk = dm_table_get_integrity_disk(t,
false);
1108 DMWARN(
"%s: conflict with existing integrity profile: "
1109 "%s profile mismatch",
1111 template_disk->disk_name);
1128 r = dm_table_set_type(t);
1130 DMERR(
"unable to set table type");
1134 r = dm_table_build_index(t);
1136 DMERR(
"unable to build btrees");
1140 r = dm_table_prealloc_integrity(t, t->
md);
1142 DMERR(
"could not register integrity profile.");
1148 DMERR(
"unable to allocate mempools");
1200 unsigned int l, n = 0, k = 0;
1203 for (l = 0; l < t->
depth; l++) {
1204 n = get_child(n, k);
1205 node = get_node(t, l, n);
1208 if (node[k] >= sector)
1212 return &t->
targets[(KEYS_PER_NODE *
n) + k];
1239 if (!ti->
type->iterate_devices)
1254 struct queue_limits *limits)
1257 struct queue_limits ti_limits;
1267 if (!ti->
type->iterate_devices)
1268 goto combine_limits;
1277 if (ti->
type->io_hints)
1278 ti->
type->io_hints(ti, &ti_limits);
1284 if (ti->
type->iterate_devices(ti, device_area_is_invalid,
1294 DMWARN(
"%s: adding target device "
1295 "(start sect %llu len %llu) "
1296 "caused an alignment inconsistency",
1299 (
unsigned long long) ti->
len);
1302 return validate_hardware_logical_block_alignment(table, limits);
1312 static void dm_table_set_integrity(
struct dm_table *t)
1314 struct gendisk *template_disk =
NULL;
1319 template_disk = dm_table_get_integrity_disk(t,
true);
1322 blk_get_integrity(template_disk));
1324 DMWARN(
"%s: device no longer has a valid integrity profile",
1327 DMWARN(
"%s: unable to establish an integrity profile",
1331 static int device_flush_capable(
struct dm_target *ti,
struct dm_dev *dev,
1334 unsigned flush = (*(
unsigned *)data);
1337 return q && (q->flush_flags & flush);
1340 static bool dm_table_supports_flush(
struct dm_table *t,
unsigned flush)
1360 if (ti->
type->iterate_devices &&
1361 ti->
type->iterate_devices(ti, device_flush_capable, &flush))
1368 static bool dm_table_discard_zeroes_data(
struct dm_table *t)
1389 return q && blk_queue_nonrot(q);
1392 static int device_is_not_random(
struct dm_target *ti,
struct dm_dev *dev,
1397 return q && !blk_queue_add_random(q);
1400 static bool dm_table_all_devices_attribute(
struct dm_table *t,
1409 if (!ti->
type->iterate_devices ||
1410 !ti->
type->iterate_devices(ti, func, NULL))
1418 struct queue_limits *limits)
1425 q->limits = *limits;
1428 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1430 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1432 if (dm_table_supports_flush(t,
REQ_FLUSH)) {
1434 if (dm_table_supports_flush(t,
REQ_FUA))
1439 if (!dm_table_discard_zeroes_data(t))
1440 q->limits.discard_zeroes_data = 0;
1443 if (dm_table_all_devices_attribute(t, device_is_nonrot))
1444 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
1446 queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
1448 dm_table_set_integrity(t);
1456 if (blk_queue_add_random(q) && dm_table_all_devices_attribute(t, device_is_not_random))
1457 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
1470 queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
1489 static void suspend_targets(
struct dm_table *t,
unsigned postsuspend)
1496 if (ti->
type->postsuspend)
1497 ti->
type->postsuspend(ti);
1498 }
else if (ti->
type->presuspend)
1499 ti->
type->presuspend(ti);
1510 suspend_targets(t, 0);
1518 suspend_targets(t, 1);
1528 if (!ti->
type->preresume)
1531 r = ti->
type->preresume(ti);
1539 if (ti->
type->resume)
1540 ti->
type->resume(ti);
1564 r |= bdi_congested(&q->backing_dev_info, bdi_bits);
1566 DMWARN_LIMIT(
"%s: any_congested: nonexistent device %s",
1585 if (ti->
type->busy && ti->
type->busy(ti))
1598 static int device_discard_capable(
struct dm_target *ti,
struct dm_dev *dev,
1603 return q && blk_queue_discard(q);
1627 if (ti->
type->iterate_devices &&
1628 ti->
type->iterate_devices(ti, device_discard_capable, NULL))