blob: 06b382304d926d70f4a633de4ffbc2550b2434eb [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050 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
71/*
72 * Similar to ceiling(log_size(n))
73 */
74static unsigned int int_log(unsigned int n, unsigned int base)
75{
76 int result = 0;
77
78 while (n > 1) {
79 n = dm_div_up(n, base);
80 result++;
81 }
82
83 return result;
84}
85
86/*
87 * Calculate the index of the child node of the n'th node k'th key.
88 */
89static inline unsigned int get_child(unsigned int n, unsigned int k)
90{
91 return (n * CHILDREN_PER_NODE) + k;
92}
93
94/*
95 * Return the n'th node of level l from table t.
96 */
97static inline sector_t *get_node(struct dm_table *t,
98 unsigned int l, unsigned int n)
99{
100 return t->index[l] + (n * KEYS_PER_NODE);
101}
102
103/*
104 * Return the highest key that you could lookup from the n'th
105 * node on level l of the btree.
106 */
107static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
108{
109 for (; l < t->depth - 1; l++)
110 n = get_child(n, CHILDREN_PER_NODE - 1);
111
112 if (n >= t->counts[l])
113 return (sector_t) - 1;
114
115 return get_node(t, l, n)[KEYS_PER_NODE - 1];
116}
117
118/*
119 * Fills in a level of the btree based on the highs of the level
120 * below it.
121 */
122static int setup_btree_index(unsigned int l, struct dm_table *t)
123{
124 unsigned int n, k;
125 sector_t *node;
126
127 for (n = 0U; n < t->counts[l]; n++) {
128 node = get_node(t, l, n);
129
130 for (k = 0U; k < KEYS_PER_NODE; k++)
131 node[k] = high(t, l + 1, get_child(n, k));
132 }
133
134 return 0;
135}
136
137void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
138{
139 unsigned long size;
140 void *addr;
141
142 /*
143 * Check that we're not going to overflow.
144 */
145 if (nmemb > (ULONG_MAX / elem_size))
146 return NULL;
147
148 size = nmemb * elem_size;
149 addr = vzalloc(size);
150
151 return addr;
152}
153EXPORT_SYMBOL(dm_vcalloc);
154
155/*
156 * highs, and targets are managed as dynamic arrays during a
157 * table load.
158 */
159static int alloc_targets(struct dm_table *t, unsigned int num)
160{
161 sector_t *n_highs;
162 struct dm_target *n_targets;
163
164 /*
165 * Allocate both the target array and offset array at once.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000166 */
David Brazdil0f672f62019-12-10 10:32:29 +0000167 n_highs = (sector_t *) dm_vcalloc(num, sizeof(struct dm_target) +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000168 sizeof(sector_t));
169 if (!n_highs)
170 return -ENOMEM;
171
172 n_targets = (struct dm_target *) (n_highs + num);
173
174 memset(n_highs, -1, sizeof(*n_highs) * num);
175 vfree(t->highs);
176
177 t->num_allocated = num;
178 t->highs = n_highs;
179 t->targets = n_targets;
180
181 return 0;
182}
183
184int dm_table_create(struct dm_table **result, fmode_t mode,
185 unsigned num_targets, struct mapped_device *md)
186{
187 struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
188
189 if (!t)
190 return -ENOMEM;
191
192 INIT_LIST_HEAD(&t->devices);
193 INIT_LIST_HEAD(&t->target_callbacks);
194
195 if (!num_targets)
196 num_targets = KEYS_PER_NODE;
197
198 num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
199
200 if (!num_targets) {
201 kfree(t);
202 return -ENOMEM;
203 }
204
205 if (alloc_targets(t, num_targets)) {
206 kfree(t);
207 return -ENOMEM;
208 }
209
210 t->type = DM_TYPE_NONE;
211 t->mode = mode;
212 t->md = md;
213 *result = t;
214 return 0;
215}
216
217static void free_devices(struct list_head *devices, struct mapped_device *md)
218{
219 struct list_head *tmp, *next;
220
221 list_for_each_safe(tmp, next, devices) {
222 struct dm_dev_internal *dd =
223 list_entry(tmp, struct dm_dev_internal, list);
224 DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s",
225 dm_device_name(md), dd->dm_dev->name);
226 dm_put_table_device(md, dd->dm_dev);
227 kfree(dd);
228 }
229}
230
231void dm_table_destroy(struct dm_table *t)
232{
233 unsigned int i;
234
235 if (!t)
236 return;
237
238 /* free the indexes */
239 if (t->depth >= 2)
240 vfree(t->index[t->depth - 2]);
241
242 /* free the targets */
243 for (i = 0; i < t->num_targets; i++) {
244 struct dm_target *tgt = t->targets + i;
245
246 if (tgt->type->dtr)
247 tgt->type->dtr(tgt);
248
249 dm_put_target_type(tgt->type);
250 }
251
252 vfree(t->highs);
253
254 /* free the device list */
255 free_devices(&t->devices, t->md);
256
257 dm_free_md_mempools(t->mempools);
258
259 kfree(t);
260}
261
262/*
263 * See if we've already got a device in the list.
264 */
265static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
266{
267 struct dm_dev_internal *dd;
268
269 list_for_each_entry (dd, l, list)
270 if (dd->dm_dev->bdev->bd_dev == dev)
271 return dd;
272
273 return NULL;
274}
275
276/*
277 * If possible, this checks an area of a destination device is invalid.
278 */
279static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
280 sector_t start, sector_t len, void *data)
281{
282 struct request_queue *q;
283 struct queue_limits *limits = data;
284 struct block_device *bdev = dev->bdev;
285 sector_t dev_size =
286 i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
287 unsigned short logical_block_size_sectors =
288 limits->logical_block_size >> SECTOR_SHIFT;
289 char b[BDEVNAME_SIZE];
290
291 /*
292 * Some devices exist without request functions,
293 * such as loop devices not yet bound to backing files.
294 * Forbid the use of such devices.
295 */
296 q = bdev_get_queue(bdev);
297 if (!q || !q->make_request_fn) {
298 DMWARN("%s: %s is not yet initialised: "
299 "start=%llu, len=%llu, dev_size=%llu",
300 dm_device_name(ti->table->md), bdevname(bdev, b),
301 (unsigned long long)start,
302 (unsigned long long)len,
303 (unsigned long long)dev_size);
304 return 1;
305 }
306
307 if (!dev_size)
308 return 0;
309
310 if ((start >= dev_size) || (start + len > dev_size)) {
311 DMWARN("%s: %s too small for target: "
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 /*
321 * If the target is mapped to zoned block device(s), check
322 * that the zones are not partially mapped.
323 */
324 if (bdev_zoned_model(bdev) != BLK_ZONED_NONE) {
325 unsigned int zone_sectors = bdev_zone_sectors(bdev);
326
327 if (start & (zone_sectors - 1)) {
328 DMWARN("%s: start=%llu not aligned to h/w zone size %u of %s",
329 dm_device_name(ti->table->md),
330 (unsigned long long)start,
331 zone_sectors, bdevname(bdev, b));
332 return 1;
333 }
334
335 /*
336 * Note: The last zone of a zoned block device may be smaller
337 * than other zones. So for a target mapping the end of a
338 * zoned block device with such a zone, len would not be zone
339 * aligned. We do not allow such last smaller zone to be part
340 * of the mapping here to ensure that mappings with multiple
341 * devices do not end up with a smaller zone in the middle of
342 * the sector range.
343 */
344 if (len & (zone_sectors - 1)) {
345 DMWARN("%s: len=%llu not aligned to h/w zone size %u of %s",
346 dm_device_name(ti->table->md),
347 (unsigned long long)len,
348 zone_sectors, bdevname(bdev, b));
349 return 1;
350 }
351 }
352
353 if (logical_block_size_sectors <= 1)
354 return 0;
355
356 if (start & (logical_block_size_sectors - 1)) {
357 DMWARN("%s: start=%llu not aligned to h/w "
358 "logical block size %u of %s",
359 dm_device_name(ti->table->md),
360 (unsigned long long)start,
361 limits->logical_block_size, bdevname(bdev, b));
362 return 1;
363 }
364
365 if (len & (logical_block_size_sectors - 1)) {
366 DMWARN("%s: len=%llu not aligned to h/w "
367 "logical block size %u of %s",
368 dm_device_name(ti->table->md),
369 (unsigned long long)len,
370 limits->logical_block_size, bdevname(bdev, b));
371 return 1;
372 }
373
374 return 0;
375}
376
377/*
378 * This upgrades the mode on an already open dm_dev, being
379 * careful to leave things as they were if we fail to reopen the
380 * device and not to touch the existing bdev field in case
381 * it is accessed concurrently inside dm_table_any_congested().
382 */
383static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
384 struct mapped_device *md)
385{
386 int r;
387 struct dm_dev *old_dev, *new_dev;
388
389 old_dev = dd->dm_dev;
390
391 r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
392 dd->dm_dev->mode | new_mode, &new_dev);
393 if (r)
394 return r;
395
396 dd->dm_dev = new_dev;
397 dm_put_table_device(md, old_dev);
398
399 return 0;
400}
401
402/*
403 * Convert the path to a device
404 */
405dev_t dm_get_dev_t(const char *path)
406{
407 dev_t dev;
408 struct block_device *bdev;
409
410 bdev = lookup_bdev(path);
411 if (IS_ERR(bdev))
412 dev = name_to_dev_t(path);
413 else {
414 dev = bdev->bd_dev;
415 bdput(bdev);
416 }
417
418 return dev;
419}
420EXPORT_SYMBOL_GPL(dm_get_dev_t);
421
422/*
423 * Add a device to the list, or just increment the usage count if
424 * it's already present.
425 */
426int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
427 struct dm_dev **result)
428{
429 int r;
430 dev_t dev;
Olivier Deprez0e641232021-09-23 10:07:05 +0200431 unsigned int major, minor;
432 char dummy;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000433 struct dm_dev_internal *dd;
434 struct dm_table *t = ti->table;
435
436 BUG_ON(!t);
437
Olivier Deprez0e641232021-09-23 10:07:05 +0200438 if (sscanf(path, "%u:%u%c", &major, &minor, &dummy) == 2) {
439 /* Extract the major/minor numbers */
440 dev = MKDEV(major, minor);
441 if (MAJOR(dev) != major || MINOR(dev) != minor)
442 return -EOVERFLOW;
443 } else {
444 dev = dm_get_dev_t(path);
445 if (!dev)
446 return -ENODEV;
447 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000448
449 dd = find_device(&t->devices, dev);
450 if (!dd) {
451 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
452 if (!dd)
453 return -ENOMEM;
454
455 if ((r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev))) {
456 kfree(dd);
457 return r;
458 }
459
460 refcount_set(&dd->count, 1);
461 list_add(&dd->list, &t->devices);
462 goto out;
463
464 } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
465 r = upgrade_mode(dd, mode, t->md);
466 if (r)
467 return r;
468 }
469 refcount_inc(&dd->count);
470out:
471 *result = dd->dm_dev;
472 return 0;
473}
474EXPORT_SYMBOL(dm_get_device);
475
476static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
477 sector_t start, sector_t len, void *data)
478{
479 struct queue_limits *limits = data;
480 struct block_device *bdev = dev->bdev;
481 struct request_queue *q = bdev_get_queue(bdev);
482 char b[BDEVNAME_SIZE];
483
484 if (unlikely(!q)) {
485 DMWARN("%s: Cannot set limits for nonexistent device %s",
486 dm_device_name(ti->table->md), bdevname(bdev, b));
487 return 0;
488 }
489
490 if (bdev_stack_limits(limits, bdev, start) < 0)
491 DMWARN("%s: adding target device %s caused an alignment inconsistency: "
492 "physical_block_size=%u, logical_block_size=%u, "
493 "alignment_offset=%u, start=%llu",
494 dm_device_name(ti->table->md), bdevname(bdev, b),
495 q->limits.physical_block_size,
496 q->limits.logical_block_size,
497 q->limits.alignment_offset,
498 (unsigned long long) start << SECTOR_SHIFT);
499
500 limits->zoned = blk_queue_zoned_model(q);
501
502 return 0;
503}
504
505/*
506 * Decrement a device's use count and remove it if necessary.
507 */
508void dm_put_device(struct dm_target *ti, struct dm_dev *d)
509{
510 int found = 0;
511 struct list_head *devices = &ti->table->devices;
512 struct dm_dev_internal *dd;
513
514 list_for_each_entry(dd, devices, list) {
515 if (dd->dm_dev == d) {
516 found = 1;
517 break;
518 }
519 }
520 if (!found) {
521 DMWARN("%s: device %s not in table devices list",
522 dm_device_name(ti->table->md), d->name);
523 return;
524 }
525 if (refcount_dec_and_test(&dd->count)) {
526 dm_put_table_device(ti->table->md, d);
527 list_del(&dd->list);
528 kfree(dd);
529 }
530}
531EXPORT_SYMBOL(dm_put_device);
532
533/*
534 * Checks to see if the target joins onto the end of the table.
535 */
536static int adjoin(struct dm_table *table, struct dm_target *ti)
537{
538 struct dm_target *prev;
539
540 if (!table->num_targets)
541 return !ti->begin;
542
543 prev = &table->targets[table->num_targets - 1];
544 return (ti->begin == (prev->begin + prev->len));
545}
546
547/*
548 * Used to dynamically allocate the arg array.
549 *
550 * We do first allocation with GFP_NOIO because dm-mpath and dm-thin must
551 * process messages even if some device is suspended. These messages have a
552 * small fixed number of arguments.
553 *
554 * On the other hand, dm-switch needs to process bulk data using messages and
555 * excessive use of GFP_NOIO could cause trouble.
556 */
557static char **realloc_argv(unsigned *size, char **old_argv)
558{
559 char **argv;
560 unsigned new_size;
561 gfp_t gfp;
562
563 if (*size) {
564 new_size = *size * 2;
565 gfp = GFP_KERNEL;
566 } else {
567 new_size = 8;
568 gfp = GFP_NOIO;
569 }
570 argv = kmalloc_array(new_size, sizeof(*argv), gfp);
David Brazdil0f672f62019-12-10 10:32:29 +0000571 if (argv && old_argv) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572 memcpy(argv, old_argv, *size * sizeof(*argv));
573 *size = new_size;
574 }
575
576 kfree(old_argv);
577 return argv;
578}
579
580/*
581 * Destructively splits up the argument list to pass to ctr.
582 */
583int dm_split_args(int *argc, char ***argvp, char *input)
584{
585 char *start, *end = input, *out, **argv = NULL;
586 unsigned array_size = 0;
587
588 *argc = 0;
589
590 if (!input) {
591 *argvp = NULL;
592 return 0;
593 }
594
595 argv = realloc_argv(&array_size, argv);
596 if (!argv)
597 return -ENOMEM;
598
599 while (1) {
600 /* Skip whitespace */
601 start = skip_spaces(end);
602
603 if (!*start)
604 break; /* success, we hit the end */
605
606 /* 'out' is used to remove any back-quotes */
607 end = out = start;
608 while (*end) {
609 /* Everything apart from '\0' can be quoted */
610 if (*end == '\\' && *(end + 1)) {
611 *out++ = *(end + 1);
612 end += 2;
613 continue;
614 }
615
616 if (isspace(*end))
617 break; /* end of token */
618
619 *out++ = *end++;
620 }
621
622 /* have we already filled the array ? */
623 if ((*argc + 1) > array_size) {
624 argv = realloc_argv(&array_size, argv);
625 if (!argv)
626 return -ENOMEM;
627 }
628
629 /* we know this is whitespace */
630 if (*end)
631 end++;
632
633 /* terminate the string and put it in the array */
634 *out = '\0';
635 argv[*argc] = start;
636 (*argc)++;
637 }
638
639 *argvp = argv;
640 return 0;
641}
642
643/*
644 * Impose necessary and sufficient conditions on a devices's table such
645 * that any incoming bio which respects its logical_block_size can be
646 * processed successfully. If it falls across the boundary between
647 * two or more targets, the size of each piece it gets split into must
648 * be compatible with the logical_block_size of the target processing it.
649 */
650static int validate_hardware_logical_block_alignment(struct dm_table *table,
651 struct queue_limits *limits)
652{
653 /*
654 * This function uses arithmetic modulo the logical_block_size
655 * (in units of 512-byte sectors).
656 */
657 unsigned short device_logical_block_size_sects =
658 limits->logical_block_size >> SECTOR_SHIFT;
659
660 /*
661 * Offset of the start of the next table entry, mod logical_block_size.
662 */
663 unsigned short next_target_start = 0;
664
665 /*
666 * Given an aligned bio that extends beyond the end of a
667 * target, how many sectors must the next target handle?
668 */
669 unsigned short remaining = 0;
670
671 struct dm_target *uninitialized_var(ti);
672 struct queue_limits ti_limits;
673 unsigned i;
674
675 /*
676 * Check each entry in the table in turn.
677 */
678 for (i = 0; i < dm_table_get_num_targets(table); i++) {
679 ti = dm_table_get_target(table, i);
680
681 blk_set_stacking_limits(&ti_limits);
682
683 /* combine all target devices' limits */
684 if (ti->type->iterate_devices)
685 ti->type->iterate_devices(ti, dm_set_device_limits,
686 &ti_limits);
687
688 /*
689 * If the remaining sectors fall entirely within this
690 * table entry are they compatible with its logical_block_size?
691 */
692 if (remaining < ti->len &&
693 remaining & ((ti_limits.logical_block_size >>
694 SECTOR_SHIFT) - 1))
695 break; /* Error */
696
697 next_target_start =
698 (unsigned short) ((next_target_start + ti->len) &
699 (device_logical_block_size_sects - 1));
700 remaining = next_target_start ?
701 device_logical_block_size_sects - next_target_start : 0;
702 }
703
704 if (remaining) {
705 DMWARN("%s: table line %u (start sect %llu len %llu) "
706 "not aligned to h/w logical block size %u",
707 dm_device_name(table->md), i,
708 (unsigned long long) ti->begin,
709 (unsigned long long) ti->len,
710 limits->logical_block_size);
711 return -EINVAL;
712 }
713
714 return 0;
715}
716
717int dm_table_add_target(struct dm_table *t, const char *type,
718 sector_t start, sector_t len, char *params)
719{
720 int r = -EINVAL, argc;
721 char **argv;
722 struct dm_target *tgt;
723
724 if (t->singleton) {
725 DMERR("%s: target type %s must appear alone in table",
726 dm_device_name(t->md), t->targets->type->name);
727 return -EINVAL;
728 }
729
730 BUG_ON(t->num_targets >= t->num_allocated);
731
732 tgt = t->targets + t->num_targets;
733 memset(tgt, 0, sizeof(*tgt));
734
735 if (!len) {
736 DMERR("%s: zero-length target", dm_device_name(t->md));
737 return -EINVAL;
738 }
739
740 tgt->type = dm_get_target_type(type);
741 if (!tgt->type) {
742 DMERR("%s: %s: unknown target type", dm_device_name(t->md), type);
743 return -EINVAL;
744 }
745
746 if (dm_target_needs_singleton(tgt->type)) {
747 if (t->num_targets) {
748 tgt->error = "singleton target type must appear alone in table";
749 goto bad;
750 }
751 t->singleton = true;
752 }
753
754 if (dm_target_always_writeable(tgt->type) && !(t->mode & FMODE_WRITE)) {
755 tgt->error = "target type may not be included in a read-only table";
756 goto bad;
757 }
758
759 if (t->immutable_target_type) {
760 if (t->immutable_target_type != tgt->type) {
761 tgt->error = "immutable target type cannot be mixed with other target types";
762 goto bad;
763 }
764 } else if (dm_target_is_immutable(tgt->type)) {
765 if (t->num_targets) {
766 tgt->error = "immutable target type cannot be mixed with other target types";
767 goto bad;
768 }
769 t->immutable_target_type = tgt->type;
770 }
771
772 if (dm_target_has_integrity(tgt->type))
773 t->integrity_added = 1;
774
775 tgt->table = t;
776 tgt->begin = start;
777 tgt->len = len;
778 tgt->error = "Unknown error";
779
780 /*
781 * Does this target adjoin the previous one ?
782 */
783 if (!adjoin(t, tgt)) {
784 tgt->error = "Gap in table";
785 goto bad;
786 }
787
788 r = dm_split_args(&argc, &argv, params);
789 if (r) {
790 tgt->error = "couldn't split parameters (insufficient memory)";
791 goto bad;
792 }
793
794 r = tgt->type->ctr(tgt, argc, argv);
795 kfree(argv);
796 if (r)
797 goto bad;
798
799 t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
800
801 if (!tgt->num_discard_bios && tgt->discards_supported)
802 DMWARN("%s: %s: ignoring discards_supported because num_discard_bios is zero.",
803 dm_device_name(t->md), type);
804
805 return 0;
806
807 bad:
808 DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
809 dm_put_target_type(tgt->type);
810 return r;
811}
812
813/*
814 * Target argument parsing helpers.
815 */
816static int validate_next_arg(const struct dm_arg *arg,
817 struct dm_arg_set *arg_set,
818 unsigned *value, char **error, unsigned grouped)
819{
820 const char *arg_str = dm_shift_arg(arg_set);
821 char dummy;
822
823 if (!arg_str ||
824 (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
825 (*value < arg->min) ||
826 (*value > arg->max) ||
827 (grouped && arg_set->argc < *value)) {
828 *error = arg->error;
829 return -EINVAL;
830 }
831
832 return 0;
833}
834
835int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
836 unsigned *value, char **error)
837{
838 return validate_next_arg(arg, arg_set, value, error, 0);
839}
840EXPORT_SYMBOL(dm_read_arg);
841
842int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
843 unsigned *value, char **error)
844{
845 return validate_next_arg(arg, arg_set, value, error, 1);
846}
847EXPORT_SYMBOL(dm_read_arg_group);
848
849const char *dm_shift_arg(struct dm_arg_set *as)
850{
851 char *r;
852
853 if (as->argc) {
854 as->argc--;
855 r = *as->argv;
856 as->argv++;
857 return r;
858 }
859
860 return NULL;
861}
862EXPORT_SYMBOL(dm_shift_arg);
863
864void dm_consume_args(struct dm_arg_set *as, unsigned num_args)
865{
866 BUG_ON(as->argc < num_args);
867 as->argc -= num_args;
868 as->argv += num_args;
869}
870EXPORT_SYMBOL(dm_consume_args);
871
872static bool __table_type_bio_based(enum dm_queue_mode table_type)
873{
874 return (table_type == DM_TYPE_BIO_BASED ||
875 table_type == DM_TYPE_DAX_BIO_BASED ||
876 table_type == DM_TYPE_NVME_BIO_BASED);
877}
878
879static bool __table_type_request_based(enum dm_queue_mode table_type)
880{
David Brazdil0f672f62019-12-10 10:32:29 +0000881 return table_type == DM_TYPE_REQUEST_BASED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000882}
883
884void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type)
885{
886 t->type = type;
887}
888EXPORT_SYMBOL_GPL(dm_table_set_type);
889
David Brazdil0f672f62019-12-10 10:32:29 +0000890/* validate the dax capability of the target device span */
Olivier Deprez0e641232021-09-23 10:07:05 +0200891int device_not_dax_capable(struct dm_target *ti, struct dm_dev *dev,
David Brazdil0f672f62019-12-10 10:32:29 +0000892 sector_t start, sector_t len, void *data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000893{
Olivier Deprez0e641232021-09-23 10:07:05 +0200894 int blocksize = *(int *) data, id;
895 bool rc;
David Brazdil0f672f62019-12-10 10:32:29 +0000896
Olivier Deprez0e641232021-09-23 10:07:05 +0200897 id = dax_read_lock();
898 rc = !dax_supported(dev->dax_dev, dev->bdev, blocksize, start, len);
899 dax_read_unlock(id);
900
901 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000902}
903
David Brazdil0f672f62019-12-10 10:32:29 +0000904/* Check devices support synchronous DAX */
Olivier Deprez0e641232021-09-23 10:07:05 +0200905static int device_not_dax_synchronous_capable(struct dm_target *ti, struct dm_dev *dev,
906 sector_t start, sector_t len, void *data)
David Brazdil0f672f62019-12-10 10:32:29 +0000907{
Olivier Deprez0e641232021-09-23 10:07:05 +0200908 return !dev->dax_dev || !dax_synchronous(dev->dax_dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000909}
910
911bool dm_table_supports_dax(struct dm_table *t,
912 iterate_devices_callout_fn iterate_fn, int *blocksize)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913{
914 struct dm_target *ti;
915 unsigned i;
916
917 /* Ensure that all targets support DAX. */
918 for (i = 0; i < dm_table_get_num_targets(t); i++) {
919 ti = dm_table_get_target(t, i);
920
921 if (!ti->type->direct_access)
922 return false;
923
924 if (!ti->type->iterate_devices ||
Olivier Deprez0e641232021-09-23 10:07:05 +0200925 ti->type->iterate_devices(ti, iterate_fn, blocksize))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926 return false;
927 }
928
929 return true;
930}
931
932static bool dm_table_does_not_support_partial_completion(struct dm_table *t);
933
Olivier Deprez0e641232021-09-23 10:07:05 +0200934static int device_is_rq_stackable(struct dm_target *ti, struct dm_dev *dev,
935 sector_t start, sector_t len, void *data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000936{
Olivier Deprez0e641232021-09-23 10:07:05 +0200937 struct block_device *bdev = dev->bdev;
938 struct request_queue *q = bdev_get_queue(bdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939
Olivier Deprez0e641232021-09-23 10:07:05 +0200940 /* request-based cannot stack on partitions! */
941 if (bdev != bdev->bd_contains)
942 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000943
David Brazdil0f672f62019-12-10 10:32:29 +0000944 return queue_is_mq(q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000945}
946
947static int dm_table_determine_type(struct dm_table *t)
948{
949 unsigned i;
950 unsigned bio_based = 0, request_based = 0, hybrid = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951 struct dm_target *tgt;
952 struct list_head *devices = dm_table_get_devices(t);
953 enum dm_queue_mode live_md_type = dm_get_md_type(t->md);
David Brazdil0f672f62019-12-10 10:32:29 +0000954 int page_size = PAGE_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000955
956 if (t->type != DM_TYPE_NONE) {
957 /* target already set the table's type */
958 if (t->type == DM_TYPE_BIO_BASED) {
959 /* possibly upgrade to a variant of bio-based */
960 goto verify_bio_based;
961 }
962 BUG_ON(t->type == DM_TYPE_DAX_BIO_BASED);
963 BUG_ON(t->type == DM_TYPE_NVME_BIO_BASED);
964 goto verify_rq_based;
965 }
966
967 for (i = 0; i < t->num_targets; i++) {
968 tgt = t->targets + i;
969 if (dm_target_hybrid(tgt))
970 hybrid = 1;
971 else if (dm_target_request_based(tgt))
972 request_based = 1;
973 else
974 bio_based = 1;
975
976 if (bio_based && request_based) {
977 DMERR("Inconsistent table: different target types"
978 " can't be mixed up");
979 return -EINVAL;
980 }
981 }
982
983 if (hybrid && !bio_based && !request_based) {
984 /*
985 * The targets can work either way.
986 * Determine the type from the live device.
987 * Default to bio-based if device is new.
988 */
989 if (__table_type_request_based(live_md_type))
990 request_based = 1;
991 else
992 bio_based = 1;
993 }
994
995 if (bio_based) {
996verify_bio_based:
997 /* We must use this table as bio-based */
998 t->type = DM_TYPE_BIO_BASED;
Olivier Deprez0e641232021-09-23 10:07:05 +0200999 if (dm_table_supports_dax(t, device_not_dax_capable, &page_size) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001000 (list_empty(devices) && live_md_type == DM_TYPE_DAX_BIO_BASED)) {
1001 t->type = DM_TYPE_DAX_BIO_BASED;
1002 } else {
1003 /* Check if upgrading to NVMe bio-based is valid or required */
1004 tgt = dm_table_get_immutable_target(t);
1005 if (tgt && !tgt->max_io_len && dm_table_does_not_support_partial_completion(t)) {
1006 t->type = DM_TYPE_NVME_BIO_BASED;
1007 goto verify_rq_based; /* must be stacked directly on NVMe (blk-mq) */
1008 } else if (list_empty(devices) && live_md_type == DM_TYPE_NVME_BIO_BASED) {
1009 t->type = DM_TYPE_NVME_BIO_BASED;
1010 }
1011 }
1012 return 0;
1013 }
1014
1015 BUG_ON(!request_based); /* No targets in this table */
1016
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001017 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("%s DM doesn't support multiple targets",
1028 t->type == DM_TYPE_NVME_BIO_BASED ? "nvme bio-based" : "request-based");
1029 return -EINVAL;
1030 }
1031
1032 if (list_empty(devices)) {
1033 int srcu_idx;
1034 struct dm_table *live_table = dm_get_live_table(t->md, &srcu_idx);
1035
David Brazdil0f672f62019-12-10 10:32:29 +00001036 /* inherit live table's type */
1037 if (live_table)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 t->type = live_table->type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001039 dm_put_live_table(t->md, srcu_idx);
1040 return 0;
1041 }
1042
1043 tgt = dm_table_get_immutable_target(t);
1044 if (!tgt) {
1045 DMERR("table load rejected: immutable target is required");
1046 return -EINVAL;
1047 } else if (tgt->max_io_len) {
1048 DMERR("table load rejected: immutable target that splits IO is not supported");
1049 return -EINVAL;
1050 }
1051
1052 /* Non-request-stackable devices can't be used for request-based dm */
1053 if (!tgt->type->iterate_devices ||
Olivier Deprez0e641232021-09-23 10:07:05 +02001054 !tgt->type->iterate_devices(tgt, device_is_rq_stackable, NULL)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001055 DMERR("table load rejected: including non-request-stackable devices");
1056 return -EINVAL;
1057 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001058
1059 return 0;
1060}
1061
1062enum dm_queue_mode dm_table_get_type(struct dm_table *t)
1063{
1064 return t->type;
1065}
1066
1067struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
1068{
1069 return t->immutable_target_type;
1070}
1071
1072struct dm_target *dm_table_get_immutable_target(struct dm_table *t)
1073{
1074 /* Immutable target is implicitly a singleton */
1075 if (t->num_targets > 1 ||
1076 !dm_target_is_immutable(t->targets[0].type))
1077 return NULL;
1078
1079 return t->targets;
1080}
1081
1082struct dm_target *dm_table_get_wildcard_target(struct dm_table *t)
1083{
1084 struct dm_target *ti;
1085 unsigned i;
1086
1087 for (i = 0; i < dm_table_get_num_targets(t); i++) {
1088 ti = dm_table_get_target(t, i);
1089 if (dm_target_is_wildcard(ti->type))
1090 return ti;
1091 }
1092
1093 return NULL;
1094}
1095
1096bool dm_table_bio_based(struct dm_table *t)
1097{
1098 return __table_type_bio_based(dm_table_get_type(t));
1099}
1100
1101bool dm_table_request_based(struct dm_table *t)
1102{
1103 return __table_type_request_based(dm_table_get_type(t));
1104}
1105
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001106static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
1107{
1108 enum dm_queue_mode type = dm_table_get_type(t);
1109 unsigned per_io_data_size = 0;
1110 unsigned min_pool_size = 0;
1111 struct dm_target *ti;
1112 unsigned i;
1113
1114 if (unlikely(type == DM_TYPE_NONE)) {
1115 DMWARN("no table type is set, can't allocate mempools");
1116 return -EINVAL;
1117 }
1118
1119 if (__table_type_bio_based(type))
1120 for (i = 0; i < t->num_targets; i++) {
1121 ti = t->targets + i;
1122 per_io_data_size = max(per_io_data_size, ti->per_io_data_size);
1123 min_pool_size = max(min_pool_size, ti->num_flush_bios);
1124 }
1125
1126 t->mempools = dm_alloc_md_mempools(md, type, t->integrity_supported,
1127 per_io_data_size, min_pool_size);
1128 if (!t->mempools)
1129 return -ENOMEM;
1130
1131 return 0;
1132}
1133
1134void dm_table_free_md_mempools(struct dm_table *t)
1135{
1136 dm_free_md_mempools(t->mempools);
1137 t->mempools = NULL;
1138}
1139
1140struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
1141{
1142 return t->mempools;
1143}
1144
1145static int setup_indexes(struct dm_table *t)
1146{
1147 int i;
1148 unsigned int total = 0;
1149 sector_t *indexes;
1150
1151 /* allocate the space for *all* the indexes */
1152 for (i = t->depth - 2; i >= 0; i--) {
1153 t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
1154 total += t->counts[i];
1155 }
1156
1157 indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
1158 if (!indexes)
1159 return -ENOMEM;
1160
1161 /* set up internal nodes, bottom-up */
1162 for (i = t->depth - 2; i >= 0; i--) {
1163 t->index[i] = indexes;
1164 indexes += (KEYS_PER_NODE * t->counts[i]);
1165 setup_btree_index(i, t);
1166 }
1167
1168 return 0;
1169}
1170
1171/*
1172 * Builds the btree to index the map.
1173 */
1174static int dm_table_build_index(struct dm_table *t)
1175{
1176 int r = 0;
1177 unsigned int leaf_nodes;
1178
1179 /* how many indexes will the btree have ? */
1180 leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
1181 t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
1182
1183 /* leaf layer has already been set up */
1184 t->counts[t->depth - 1] = leaf_nodes;
1185 t->index[t->depth - 1] = t->highs;
1186
1187 if (t->depth >= 2)
1188 r = setup_indexes(t);
1189
1190 return r;
1191}
1192
1193static bool integrity_profile_exists(struct gendisk *disk)
1194{
1195 return !!blk_get_integrity(disk);
1196}
1197
1198/*
1199 * Get a disk whose integrity profile reflects the table's profile.
1200 * Returns NULL if integrity support was inconsistent or unavailable.
1201 */
1202static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t)
1203{
1204 struct list_head *devices = dm_table_get_devices(t);
1205 struct dm_dev_internal *dd = NULL;
1206 struct gendisk *prev_disk = NULL, *template_disk = NULL;
1207 unsigned i;
1208
1209 for (i = 0; i < dm_table_get_num_targets(t); i++) {
1210 struct dm_target *ti = dm_table_get_target(t, i);
1211 if (!dm_target_passes_integrity(ti->type))
1212 goto no_integrity;
1213 }
1214
1215 list_for_each_entry(dd, devices, list) {
1216 template_disk = dd->dm_dev->bdev->bd_disk;
1217 if (!integrity_profile_exists(template_disk))
1218 goto no_integrity;
1219 else if (prev_disk &&
1220 blk_integrity_compare(prev_disk, template_disk) < 0)
1221 goto no_integrity;
1222 prev_disk = template_disk;
1223 }
1224
1225 return template_disk;
1226
1227no_integrity:
1228 if (prev_disk)
1229 DMWARN("%s: integrity not set: %s and %s profile mismatch",
1230 dm_device_name(t->md),
1231 prev_disk->disk_name,
1232 template_disk->disk_name);
1233 return NULL;
1234}
1235
1236/*
1237 * Register the mapped device for blk_integrity support if the
1238 * underlying devices have an integrity profile. But all devices may
1239 * not have matching profiles (checking all devices isn't reliable
1240 * during table load because this table may use other DM device(s) which
1241 * must be resumed before they will have an initialized integity
1242 * profile). Consequently, stacked DM devices force a 2 stage integrity
1243 * profile validation: First pass during table load, final pass during
1244 * resume.
1245 */
1246static int dm_table_register_integrity(struct dm_table *t)
1247{
1248 struct mapped_device *md = t->md;
1249 struct gendisk *template_disk = NULL;
1250
1251 /* If target handles integrity itself do not register it here. */
1252 if (t->integrity_added)
1253 return 0;
1254
1255 template_disk = dm_table_get_integrity_disk(t);
1256 if (!template_disk)
1257 return 0;
1258
1259 if (!integrity_profile_exists(dm_disk(md))) {
1260 t->integrity_supported = true;
1261 /*
1262 * Register integrity profile during table load; we can do
1263 * this because the final profile must match during resume.
1264 */
1265 blk_integrity_register(dm_disk(md),
1266 blk_get_integrity(template_disk));
1267 return 0;
1268 }
1269
1270 /*
1271 * If DM device already has an initialized integrity
1272 * profile the new profile should not conflict.
1273 */
1274 if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
1275 DMWARN("%s: conflict with existing integrity profile: "
1276 "%s profile mismatch",
1277 dm_device_name(t->md),
1278 template_disk->disk_name);
1279 return 1;
1280 }
1281
1282 /* Preserve existing integrity profile */
1283 t->integrity_supported = true;
1284 return 0;
1285}
1286
1287/*
1288 * Prepares the table for use by building the indices,
1289 * setting the type, and allocating mempools.
1290 */
1291int dm_table_complete(struct dm_table *t)
1292{
1293 int r;
1294
1295 r = dm_table_determine_type(t);
1296 if (r) {
1297 DMERR("unable to determine table type");
1298 return r;
1299 }
1300
1301 r = dm_table_build_index(t);
1302 if (r) {
1303 DMERR("unable to build btrees");
1304 return r;
1305 }
1306
1307 r = dm_table_register_integrity(t);
1308 if (r) {
1309 DMERR("could not register integrity profile.");
1310 return r;
1311 }
1312
1313 r = dm_table_alloc_md_mempools(t, t->md);
1314 if (r)
1315 DMERR("unable to allocate mempools");
1316
1317 return r;
1318}
1319
1320static DEFINE_MUTEX(_event_lock);
1321void dm_table_event_callback(struct dm_table *t,
1322 void (*fn)(void *), void *context)
1323{
1324 mutex_lock(&_event_lock);
1325 t->event_fn = fn;
1326 t->event_context = context;
1327 mutex_unlock(&_event_lock);
1328}
1329
1330void dm_table_event(struct dm_table *t)
1331{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001332 mutex_lock(&_event_lock);
1333 if (t->event_fn)
1334 t->event_fn(t->event_context);
1335 mutex_unlock(&_event_lock);
1336}
1337EXPORT_SYMBOL(dm_table_event);
1338
David Brazdil0f672f62019-12-10 10:32:29 +00001339inline sector_t dm_table_get_size(struct dm_table *t)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001340{
1341 return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
1342}
1343EXPORT_SYMBOL(dm_table_get_size);
1344
1345struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
1346{
1347 if (index >= t->num_targets)
1348 return NULL;
1349
1350 return t->targets + index;
1351}
1352
1353/*
1354 * Search the btree for the correct target.
1355 *
David Brazdil0f672f62019-12-10 10:32:29 +00001356 * Caller should check returned pointer for NULL
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001357 * to trap I/O beyond end of device.
1358 */
1359struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
1360{
1361 unsigned int l, n = 0, k = 0;
1362 sector_t *node;
1363
David Brazdil0f672f62019-12-10 10:32:29 +00001364 if (unlikely(sector >= dm_table_get_size(t)))
1365 return NULL;
1366
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001367 for (l = 0; l < t->depth; l++) {
1368 n = get_child(n, k);
1369 node = get_node(t, l, n);
1370
1371 for (k = 0; k < KEYS_PER_NODE; k++)
1372 if (node[k] >= sector)
1373 break;
1374 }
1375
1376 return &t->targets[(KEYS_PER_NODE * n) + k];
1377}
1378
Olivier Deprez0e641232021-09-23 10:07:05 +02001379/*
1380 * type->iterate_devices() should be called when the sanity check needs to
1381 * iterate and check all underlying data devices. iterate_devices() will
1382 * iterate all underlying data devices until it encounters a non-zero return
1383 * code, returned by whether the input iterate_devices_callout_fn, or
1384 * iterate_devices() itself internally.
1385 *
1386 * For some target type (e.g. dm-stripe), one call of iterate_devices() may
1387 * iterate multiple underlying devices internally, in which case a non-zero
1388 * return code returned by iterate_devices_callout_fn will stop the iteration
1389 * in advance.
1390 *
1391 * Cases requiring _any_ underlying device supporting some kind of attribute,
1392 * should use the iteration structure like dm_table_any_dev_attr(), or call
1393 * it directly. @func should handle semantics of positive examples, e.g.
1394 * capable of something.
1395 *
1396 * Cases requiring _all_ underlying devices supporting some kind of attribute,
1397 * should use the iteration structure like dm_table_supports_nowait() or
1398 * dm_table_supports_discards(). Or introduce dm_table_all_devs_attr() that
1399 * uses an @anti_func that handle semantics of counter examples, e.g. not
1400 * capable of something. So: return !dm_table_any_dev_attr(t, anti_func, data);
1401 */
1402static bool dm_table_any_dev_attr(struct dm_table *t,
1403 iterate_devices_callout_fn func, void *data)
1404{
1405 struct dm_target *ti;
1406 unsigned int i;
1407
1408 for (i = 0; i < dm_table_get_num_targets(t); i++) {
1409 ti = dm_table_get_target(t, i);
1410
1411 if (ti->type->iterate_devices &&
1412 ti->type->iterate_devices(ti, func, data))
1413 return true;
1414 }
1415
1416 return false;
1417}
1418
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001419static int count_device(struct dm_target *ti, struct dm_dev *dev,
1420 sector_t start, sector_t len, void *data)
1421{
1422 unsigned *num_devices = data;
1423
1424 (*num_devices)++;
1425
1426 return 0;
1427}
1428
1429/*
1430 * Check whether a table has no data devices attached using each
1431 * target's iterate_devices method.
1432 * Returns false if the result is unknown because a target doesn't
1433 * support iterate_devices.
1434 */
1435bool dm_table_has_no_data_devices(struct dm_table *table)
1436{
1437 struct dm_target *ti;
1438 unsigned i, num_devices;
1439
1440 for (i = 0; i < dm_table_get_num_targets(table); i++) {
1441 ti = dm_table_get_target(table, i);
1442
1443 if (!ti->type->iterate_devices)
1444 return false;
1445
1446 num_devices = 0;
1447 ti->type->iterate_devices(ti, count_device, &num_devices);
1448 if (num_devices)
1449 return false;
1450 }
1451
1452 return true;
1453}
1454
Olivier Deprez0e641232021-09-23 10:07:05 +02001455static int device_not_zoned_model(struct dm_target *ti, struct dm_dev *dev,
1456 sector_t start, sector_t len, void *data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001457{
1458 struct request_queue *q = bdev_get_queue(dev->bdev);
1459 enum blk_zoned_model *zoned_model = data;
1460
Olivier Deprez0e641232021-09-23 10:07:05 +02001461 return !q || blk_queue_zoned_model(q) != *zoned_model;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001462}
1463
1464static bool dm_table_supports_zoned_model(struct dm_table *t,
1465 enum blk_zoned_model zoned_model)
1466{
1467 struct dm_target *ti;
1468 unsigned i;
1469
1470 for (i = 0; i < dm_table_get_num_targets(t); i++) {
1471 ti = dm_table_get_target(t, i);
1472
1473 if (zoned_model == BLK_ZONED_HM &&
1474 !dm_target_supports_zoned_hm(ti->type))
1475 return false;
1476
1477 if (!ti->type->iterate_devices ||
Olivier Deprez0e641232021-09-23 10:07:05 +02001478 ti->type->iterate_devices(ti, device_not_zoned_model, &zoned_model))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001479 return false;
1480 }
1481
1482 return true;
1483}
1484
Olivier Deprez0e641232021-09-23 10:07:05 +02001485static int device_not_matches_zone_sectors(struct dm_target *ti, struct dm_dev *dev,
1486 sector_t start, sector_t len, void *data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001487{
1488 struct request_queue *q = bdev_get_queue(dev->bdev);
1489 unsigned int *zone_sectors = data;
1490
Olivier Deprez0e641232021-09-23 10:07:05 +02001491 return !q || blk_queue_zone_sectors(q) != *zone_sectors;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001492}
1493
1494static int validate_hardware_zoned_model(struct dm_table *table,
1495 enum blk_zoned_model zoned_model,
1496 unsigned int zone_sectors)
1497{
1498 if (zoned_model == BLK_ZONED_NONE)
1499 return 0;
1500
1501 if (!dm_table_supports_zoned_model(table, zoned_model)) {
1502 DMERR("%s: zoned model is not consistent across all devices",
1503 dm_device_name(table->md));
1504 return -EINVAL;
1505 }
1506
1507 /* Check zone size validity and compatibility */
1508 if (!zone_sectors || !is_power_of_2(zone_sectors))
1509 return -EINVAL;
1510
Olivier Deprez0e641232021-09-23 10:07:05 +02001511 if (dm_table_any_dev_attr(table, device_not_matches_zone_sectors, &zone_sectors)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001512 DMERR("%s: zone sectors is not consistent across all devices",
1513 dm_device_name(table->md));
1514 return -EINVAL;
1515 }
1516
1517 return 0;
1518}
1519
1520/*
1521 * Establish the new table's queue_limits and validate them.
1522 */
1523int dm_calculate_queue_limits(struct dm_table *table,
1524 struct queue_limits *limits)
1525{
1526 struct dm_target *ti;
1527 struct queue_limits ti_limits;
1528 unsigned i;
1529 enum blk_zoned_model zoned_model = BLK_ZONED_NONE;
1530 unsigned int zone_sectors = 0;
1531
1532 blk_set_stacking_limits(limits);
1533
1534 for (i = 0; i < dm_table_get_num_targets(table); i++) {
1535 blk_set_stacking_limits(&ti_limits);
1536
1537 ti = dm_table_get_target(table, i);
1538
1539 if (!ti->type->iterate_devices)
1540 goto combine_limits;
1541
1542 /*
1543 * Combine queue limits of all the devices this target uses.
1544 */
1545 ti->type->iterate_devices(ti, dm_set_device_limits,
1546 &ti_limits);
1547
1548 if (zoned_model == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
1549 /*
1550 * After stacking all limits, validate all devices
1551 * in table support this zoned model and zone sectors.
1552 */
1553 zoned_model = ti_limits.zoned;
1554 zone_sectors = ti_limits.chunk_sectors;
1555 }
1556
1557 /* Set I/O hints portion of queue limits */
1558 if (ti->type->io_hints)
1559 ti->type->io_hints(ti, &ti_limits);
1560
1561 /*
1562 * Check each device area is consistent with the target's
1563 * overall queue limits.
1564 */
1565 if (ti->type->iterate_devices(ti, device_area_is_invalid,
1566 &ti_limits))
1567 return -EINVAL;
1568
1569combine_limits:
1570 /*
1571 * Merge this target's queue limits into the overall limits
1572 * for the table.
1573 */
1574 if (blk_stack_limits(limits, &ti_limits, 0) < 0)
1575 DMWARN("%s: adding target device "
1576 "(start sect %llu len %llu) "
1577 "caused an alignment inconsistency",
1578 dm_device_name(table->md),
1579 (unsigned long long) ti->begin,
1580 (unsigned long long) ti->len);
1581
1582 /*
1583 * FIXME: this should likely be moved to blk_stack_limits(), would
1584 * also eliminate limits->zoned stacking hack in dm_set_device_limits()
1585 */
1586 if (limits->zoned == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
1587 /*
1588 * By default, the stacked limits zoned model is set to
1589 * BLK_ZONED_NONE in blk_set_stacking_limits(). Update
1590 * this model using the first target model reported
1591 * that is not BLK_ZONED_NONE. This will be either the
1592 * first target device zoned model or the model reported
1593 * by the target .io_hints.
1594 */
1595 limits->zoned = ti_limits.zoned;
1596 }
1597 }
1598
1599 /*
1600 * Verify that the zoned model and zone sectors, as determined before
1601 * any .io_hints override, are the same across all devices in the table.
1602 * - this is especially relevant if .io_hints is emulating a disk-managed
1603 * zoned model (aka BLK_ZONED_NONE) on host-managed zoned block devices.
1604 * BUT...
1605 */
1606 if (limits->zoned != BLK_ZONED_NONE) {
1607 /*
1608 * ...IF the above limits stacking determined a zoned model
1609 * validate that all of the table's devices conform to it.
1610 */
1611 zoned_model = limits->zoned;
1612 zone_sectors = limits->chunk_sectors;
1613 }
1614 if (validate_hardware_zoned_model(table, zoned_model, zone_sectors))
1615 return -EINVAL;
1616
1617 return validate_hardware_logical_block_alignment(table, limits);
1618}
1619
1620/*
1621 * Verify that all devices have an integrity profile that matches the
1622 * DM device's registered integrity profile. If the profiles don't
1623 * match then unregister the DM device's integrity profile.
1624 */
1625static void dm_table_verify_integrity(struct dm_table *t)
1626{
1627 struct gendisk *template_disk = NULL;
1628
1629 if (t->integrity_added)
1630 return;
1631
1632 if (t->integrity_supported) {
1633 /*
1634 * Verify that the original integrity profile
1635 * matches all the devices in this table.
1636 */
1637 template_disk = dm_table_get_integrity_disk(t);
1638 if (template_disk &&
1639 blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
1640 return;
1641 }
1642
1643 if (integrity_profile_exists(dm_disk(t->md))) {
1644 DMWARN("%s: unable to establish an integrity profile",
1645 dm_device_name(t->md));
1646 blk_integrity_unregister(dm_disk(t->md));
1647 }
1648}
1649
1650static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
1651 sector_t start, sector_t len, void *data)
1652{
1653 unsigned long flush = (unsigned long) data;
1654 struct request_queue *q = bdev_get_queue(dev->bdev);
1655
1656 return q && (q->queue_flags & flush);
1657}
1658
1659static bool dm_table_supports_flush(struct dm_table *t, unsigned long flush)
1660{
1661 struct dm_target *ti;
1662 unsigned i;
1663
1664 /*
1665 * Require at least one underlying device to support flushes.
1666 * t->devices includes internal dm devices such as mirror logs
1667 * so we need to use iterate_devices here, which targets
1668 * supporting flushes must provide.
1669 */
1670 for (i = 0; i < dm_table_get_num_targets(t); i++) {
1671 ti = dm_table_get_target(t, i);
1672
1673 if (!ti->num_flush_bios)
1674 continue;
1675
1676 if (ti->flush_supported)
1677 return true;
1678
1679 if (ti->type->iterate_devices &&
1680 ti->type->iterate_devices(ti, device_flush_capable, (void *) flush))
1681 return true;
1682 }
1683
1684 return false;
1685}
1686
1687static int device_dax_write_cache_enabled(struct dm_target *ti,
1688 struct dm_dev *dev, sector_t start,
1689 sector_t len, void *data)
1690{
1691 struct dax_device *dax_dev = dev->dax_dev;
1692
1693 if (!dax_dev)
1694 return false;
1695
1696 if (dax_write_cache_enabled(dax_dev))
1697 return true;
1698 return false;
1699}
1700
Olivier Deprez0e641232021-09-23 10:07:05 +02001701static int device_is_rotational(struct dm_target *ti, struct dm_dev *dev,
1702 sector_t start, sector_t len, void *data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001703{
1704 struct request_queue *q = bdev_get_queue(dev->bdev);
1705
Olivier Deprez0e641232021-09-23 10:07:05 +02001706 return q && !blk_queue_nonrot(q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001707}
1708
1709static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
1710 sector_t start, sector_t len, void *data)
1711{
1712 struct request_queue *q = bdev_get_queue(dev->bdev);
1713
1714 return q && !blk_queue_add_random(q);
1715}
1716
Olivier Deprez0e641232021-09-23 10:07:05 +02001717static int device_is_partial_completion(struct dm_target *ti, struct dm_dev *dev,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001718 sector_t start, sector_t len, void *data)
1719{
1720 char b[BDEVNAME_SIZE];
1721
1722 /* For now, NVMe devices are the only devices of this class */
Olivier Deprez0e641232021-09-23 10:07:05 +02001723 return (strncmp(bdevname(dev->bdev, b), "nvme", 4) != 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001724}
1725
1726static bool dm_table_does_not_support_partial_completion(struct dm_table *t)
1727{
Olivier Deprez0e641232021-09-23 10:07:05 +02001728 return !dm_table_any_dev_attr(t, device_is_partial_completion, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001729}
1730
1731static int device_not_write_same_capable(struct dm_target *ti, struct dm_dev *dev,
1732 sector_t start, sector_t len, void *data)
1733{
1734 struct request_queue *q = bdev_get_queue(dev->bdev);
1735
1736 return q && !q->limits.max_write_same_sectors;
1737}
1738
1739static bool dm_table_supports_write_same(struct dm_table *t)
1740{
1741 struct dm_target *ti;
1742 unsigned i;
1743
1744 for (i = 0; i < dm_table_get_num_targets(t); i++) {
1745 ti = dm_table_get_target(t, i);
1746
1747 if (!ti->num_write_same_bios)
1748 return false;
1749
1750 if (!ti->type->iterate_devices ||
1751 ti->type->iterate_devices(ti, device_not_write_same_capable, NULL))
1752 return false;
1753 }
1754
1755 return true;
1756}
1757
1758static int device_not_write_zeroes_capable(struct dm_target *ti, struct dm_dev *dev,
1759 sector_t start, sector_t len, void *data)
1760{
1761 struct request_queue *q = bdev_get_queue(dev->bdev);
1762
1763 return q && !q->limits.max_write_zeroes_sectors;
1764}
1765
1766static bool dm_table_supports_write_zeroes(struct dm_table *t)
1767{
1768 struct dm_target *ti;
1769 unsigned i = 0;
1770
1771 while (i < dm_table_get_num_targets(t)) {
1772 ti = dm_table_get_target(t, i++);
1773
1774 if (!ti->num_write_zeroes_bios)
1775 return false;
1776
1777 if (!ti->type->iterate_devices ||
1778 ti->type->iterate_devices(ti, device_not_write_zeroes_capable, NULL))
1779 return false;
1780 }
1781
1782 return true;
1783}
1784
1785static int device_not_discard_capable(struct dm_target *ti, struct dm_dev *dev,
1786 sector_t start, sector_t len, void *data)
1787{
1788 struct request_queue *q = bdev_get_queue(dev->bdev);
1789
1790 return q && !blk_queue_discard(q);
1791}
1792
1793static bool dm_table_supports_discards(struct dm_table *t)
1794{
1795 struct dm_target *ti;
1796 unsigned i;
1797
1798 for (i = 0; i < dm_table_get_num_targets(t); i++) {
1799 ti = dm_table_get_target(t, i);
1800
1801 if (!ti->num_discard_bios)
1802 return false;
1803
1804 /*
1805 * Either the target provides discard support (as implied by setting
1806 * 'discards_supported') or it relies on _all_ data devices having
1807 * discard support.
1808 */
1809 if (!ti->discards_supported &&
1810 (!ti->type->iterate_devices ||
1811 ti->type->iterate_devices(ti, device_not_discard_capable, NULL)))
1812 return false;
1813 }
1814
1815 return true;
1816}
1817
1818static int device_not_secure_erase_capable(struct dm_target *ti,
1819 struct dm_dev *dev, sector_t start,
1820 sector_t len, void *data)
1821{
1822 struct request_queue *q = bdev_get_queue(dev->bdev);
1823
1824 return q && !blk_queue_secure_erase(q);
1825}
1826
1827static bool dm_table_supports_secure_erase(struct dm_table *t)
1828{
1829 struct dm_target *ti;
1830 unsigned int i;
1831
1832 for (i = 0; i < dm_table_get_num_targets(t); i++) {
1833 ti = dm_table_get_target(t, i);
1834
1835 if (!ti->num_secure_erase_bios)
1836 return false;
1837
1838 if (!ti->type->iterate_devices ||
1839 ti->type->iterate_devices(ti, device_not_secure_erase_capable, NULL))
1840 return false;
1841 }
1842
1843 return true;
1844}
1845
David Brazdil0f672f62019-12-10 10:32:29 +00001846static int device_requires_stable_pages(struct dm_target *ti,
1847 struct dm_dev *dev, sector_t start,
1848 sector_t len, void *data)
1849{
1850 struct request_queue *q = bdev_get_queue(dev->bdev);
1851
1852 return q && bdi_cap_stable_pages_required(q->backing_dev_info);
1853}
1854
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001855void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
1856 struct queue_limits *limits)
1857{
1858 bool wc = false, fua = false;
David Brazdil0f672f62019-12-10 10:32:29 +00001859 int page_size = PAGE_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001860
1861 /*
1862 * Copy table's limits to the DM device's request_queue
1863 */
1864 q->limits = *limits;
1865
1866 if (!dm_table_supports_discards(t)) {
1867 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
1868 /* Must also clear discard limits... */
1869 q->limits.max_discard_sectors = 0;
1870 q->limits.max_hw_discard_sectors = 0;
1871 q->limits.discard_granularity = 0;
1872 q->limits.discard_alignment = 0;
1873 q->limits.discard_misaligned = 0;
1874 } else
1875 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
1876
1877 if (dm_table_supports_secure_erase(t))
1878 blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
1879
1880 if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_WC))) {
1881 wc = true;
1882 if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_FUA)))
1883 fua = true;
1884 }
1885 blk_queue_write_cache(q, wc, fua);
1886
Olivier Deprez0e641232021-09-23 10:07:05 +02001887 if (dm_table_supports_dax(t, device_not_dax_capable, &page_size)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001888 blk_queue_flag_set(QUEUE_FLAG_DAX, q);
Olivier Deprez0e641232021-09-23 10:07:05 +02001889 if (dm_table_supports_dax(t, device_not_dax_synchronous_capable, NULL))
David Brazdil0f672f62019-12-10 10:32:29 +00001890 set_dax_synchronous(t->md->dax_dev);
1891 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001892 else
1893 blk_queue_flag_clear(QUEUE_FLAG_DAX, q);
1894
Olivier Deprez0e641232021-09-23 10:07:05 +02001895 if (dm_table_any_dev_attr(t, device_dax_write_cache_enabled, NULL))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001896 dax_write_cache(t->md->dax_dev, true);
1897
1898 /* Ensure that all underlying devices are non-rotational. */
Olivier Deprez0e641232021-09-23 10:07:05 +02001899 if (dm_table_any_dev_attr(t, device_is_rotational, NULL))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001900 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
Olivier Deprez0e641232021-09-23 10:07:05 +02001901 else
1902 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001903
1904 if (!dm_table_supports_write_same(t))
1905 q->limits.max_write_same_sectors = 0;
1906 if (!dm_table_supports_write_zeroes(t))
1907 q->limits.max_write_zeroes_sectors = 0;
1908
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001909 dm_table_verify_integrity(t);
1910
1911 /*
David Brazdil0f672f62019-12-10 10:32:29 +00001912 * Some devices don't use blk_integrity but still want stable pages
1913 * because they do their own checksumming.
Olivier Deprez0e641232021-09-23 10:07:05 +02001914 * If any underlying device requires stable pages, a table must require
1915 * them as well. Only targets that support iterate_devices are considered:
1916 * don't want error, zero, etc to require stable pages.
David Brazdil0f672f62019-12-10 10:32:29 +00001917 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001918 if (dm_table_any_dev_attr(t, device_requires_stable_pages, NULL))
David Brazdil0f672f62019-12-10 10:32:29 +00001919 q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
1920 else
1921 q->backing_dev_info->capabilities &= ~BDI_CAP_STABLE_WRITES;
1922
1923 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001924 * Determine whether or not this queue's I/O timings contribute
1925 * to the entropy pool, Only request-based targets use this.
1926 * Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
1927 * have it set.
1928 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001929 if (blk_queue_add_random(q) &&
1930 dm_table_any_dev_attr(t, device_is_not_random, NULL))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001931 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
David Brazdil0f672f62019-12-10 10:32:29 +00001932
1933 /*
1934 * For a zoned target, the number of zones should be updated for the
1935 * correct value to be exposed in sysfs queue/nr_zones. For a BIO based
1936 * target, this is all that is needed. For a request based target, the
1937 * queue zone bitmaps must also be updated.
1938 * Use blk_revalidate_disk_zones() to handle this.
1939 */
1940 if (blk_queue_is_zoned(q))
1941 blk_revalidate_disk_zones(t->md->disk);
1942
1943 /* Allow reads to exceed readahead limits */
1944 q->backing_dev_info->io_pages = limits->max_sectors >> (PAGE_SHIFT - 9);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001945}
1946
1947unsigned int dm_table_get_num_targets(struct dm_table *t)
1948{
1949 return t->num_targets;
1950}
1951
1952struct list_head *dm_table_get_devices(struct dm_table *t)
1953{
1954 return &t->devices;
1955}
1956
1957fmode_t dm_table_get_mode(struct dm_table *t)
1958{
1959 return t->mode;
1960}
1961EXPORT_SYMBOL(dm_table_get_mode);
1962
1963enum suspend_mode {
1964 PRESUSPEND,
1965 PRESUSPEND_UNDO,
1966 POSTSUSPEND,
1967};
1968
1969static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
1970{
1971 int i = t->num_targets;
1972 struct dm_target *ti = t->targets;
1973
1974 lockdep_assert_held(&t->md->suspend_lock);
1975
1976 while (i--) {
1977 switch (mode) {
1978 case PRESUSPEND:
1979 if (ti->type->presuspend)
1980 ti->type->presuspend(ti);
1981 break;
1982 case PRESUSPEND_UNDO:
1983 if (ti->type->presuspend_undo)
1984 ti->type->presuspend_undo(ti);
1985 break;
1986 case POSTSUSPEND:
1987 if (ti->type->postsuspend)
1988 ti->type->postsuspend(ti);
1989 break;
1990 }
1991 ti++;
1992 }
1993}
1994
1995void dm_table_presuspend_targets(struct dm_table *t)
1996{
1997 if (!t)
1998 return;
1999
2000 suspend_targets(t, PRESUSPEND);
2001}
2002
2003void dm_table_presuspend_undo_targets(struct dm_table *t)
2004{
2005 if (!t)
2006 return;
2007
2008 suspend_targets(t, PRESUSPEND_UNDO);
2009}
2010
2011void dm_table_postsuspend_targets(struct dm_table *t)
2012{
2013 if (!t)
2014 return;
2015
2016 suspend_targets(t, POSTSUSPEND);
2017}
2018
2019int dm_table_resume_targets(struct dm_table *t)
2020{
2021 int i, r = 0;
2022
2023 lockdep_assert_held(&t->md->suspend_lock);
2024
2025 for (i = 0; i < t->num_targets; i++) {
2026 struct dm_target *ti = t->targets + i;
2027
2028 if (!ti->type->preresume)
2029 continue;
2030
2031 r = ti->type->preresume(ti);
2032 if (r) {
2033 DMERR("%s: %s: preresume failed, error = %d",
2034 dm_device_name(t->md), ti->type->name, r);
2035 return r;
2036 }
2037 }
2038
2039 for (i = 0; i < t->num_targets; i++) {
2040 struct dm_target *ti = t->targets + i;
2041
2042 if (ti->type->resume)
2043 ti->type->resume(ti);
2044 }
2045
2046 return 0;
2047}
2048
2049void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
2050{
2051 list_add(&cb->list, &t->target_callbacks);
2052}
2053EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
2054
2055int dm_table_any_congested(struct dm_table *t, int bdi_bits)
2056{
2057 struct dm_dev_internal *dd;
2058 struct list_head *devices = dm_table_get_devices(t);
2059 struct dm_target_callbacks *cb;
2060 int r = 0;
2061
2062 list_for_each_entry(dd, devices, list) {
2063 struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
2064 char b[BDEVNAME_SIZE];
2065
2066 if (likely(q))
2067 r |= bdi_congested(q->backing_dev_info, bdi_bits);
2068 else
2069 DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
2070 dm_device_name(t->md),
2071 bdevname(dd->dm_dev->bdev, b));
2072 }
2073
2074 list_for_each_entry(cb, &t->target_callbacks, list)
2075 if (cb->congested_fn)
2076 r |= cb->congested_fn(cb, bdi_bits);
2077
2078 return r;
2079}
2080
2081struct mapped_device *dm_table_get_md(struct dm_table *t)
2082{
2083 return t->md;
2084}
2085EXPORT_SYMBOL(dm_table_get_md);
2086
David Brazdil0f672f62019-12-10 10:32:29 +00002087const char *dm_table_device_name(struct dm_table *t)
2088{
2089 return dm_device_name(t->md);
2090}
2091EXPORT_SYMBOL_GPL(dm_table_device_name);
2092
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002093void dm_table_run_md_queue_async(struct dm_table *t)
2094{
2095 struct mapped_device *md;
2096 struct request_queue *queue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002097
2098 if (!dm_table_request_based(t))
2099 return;
2100
2101 md = dm_table_get_md(t);
2102 queue = dm_get_md_queue(md);
David Brazdil0f672f62019-12-10 10:32:29 +00002103 if (queue)
2104 blk_mq_run_hw_queues(queue, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002105}
2106EXPORT_SYMBOL(dm_table_run_md_queue_async);
2107