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