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