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