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