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