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