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