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