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