• 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.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/mount.h>
15 #include <linux/ctype.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
19 #include <linux/mutex.h>
20 #include <linux/delay.h>
21 #include <linux/atomic.h>
22 #include <linux/blk-mq.h>
23 #include <linux/mount.h>
24 
25 #define DM_MSG_PREFIX "table"
26 
27 #define MAX_DEPTH 16
28 #define NODE_SIZE L1_CACHE_BYTES
29 #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
30 #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
31 
32 struct dm_table {
33 	struct mapped_device *md;
34 	unsigned type;
35 
36 	/* btree table */
37 	unsigned int depth;
38 	unsigned int counts[MAX_DEPTH];	/* in nodes */
39 	sector_t *index[MAX_DEPTH];
40 
41 	unsigned int num_targets;
42 	unsigned int num_allocated;
43 	sector_t *highs;
44 	struct dm_target *targets;
45 
46 	struct target_type *immutable_target_type;
47 	unsigned integrity_supported:1;
48 	unsigned singleton:1;
49 
50 	/*
51 	 * Indicates the rw permissions for the new logical
52 	 * device.  This should be a combination of FMODE_READ
53 	 * and FMODE_WRITE.
54 	 */
55 	fmode_t mode;
56 
57 	/* a list of devices used by this table */
58 	struct list_head devices;
59 
60 	/* events get handed up using this callback */
61 	void (*event_fn)(void *);
62 	void *event_context;
63 
64 	struct dm_md_mempools *mempools;
65 
66 	struct list_head target_callbacks;
67 };
68 
69 /*
70  * Similar to ceiling(log_size(n))
71  */
int_log(unsigned int n,unsigned int base)72 static unsigned int int_log(unsigned int n, unsigned int base)
73 {
74 	int result = 0;
75 
76 	while (n > 1) {
77 		n = dm_div_up(n, base);
78 		result++;
79 	}
80 
81 	return result;
82 }
83 
84 /*
85  * Calculate the index of the child node of the n'th node k'th key.
86  */
get_child(unsigned int n,unsigned int k)87 static inline unsigned int get_child(unsigned int n, unsigned int k)
88 {
89 	return (n * CHILDREN_PER_NODE) + k;
90 }
91 
92 /*
93  * Return the n'th node of level l from table t.
94  */
get_node(struct dm_table * t,unsigned int l,unsigned int n)95 static inline sector_t *get_node(struct dm_table *t,
96 				 unsigned int l, unsigned int n)
97 {
98 	return t->index[l] + (n * KEYS_PER_NODE);
99 }
100 
101 /*
102  * Return the highest key that you could lookup from the n'th
103  * node on level l of the btree.
104  */
high(struct dm_table * t,unsigned int l,unsigned int n)105 static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
106 {
107 	for (; l < t->depth - 1; l++)
108 		n = get_child(n, CHILDREN_PER_NODE - 1);
109 
110 	if (n >= t->counts[l])
111 		return (sector_t) - 1;
112 
113 	return get_node(t, l, n)[KEYS_PER_NODE - 1];
114 }
115 
116 /*
117  * Fills in a level of the btree based on the highs of the level
118  * below it.
119  */
setup_btree_index(unsigned int l,struct dm_table * t)120 static int setup_btree_index(unsigned int l, struct dm_table *t)
121 {
122 	unsigned int n, k;
123 	sector_t *node;
124 
125 	for (n = 0U; n < t->counts[l]; n++) {
126 		node = get_node(t, l, n);
127 
128 		for (k = 0U; k < KEYS_PER_NODE; k++)
129 			node[k] = high(t, l + 1, get_child(n, k));
130 	}
131 
132 	return 0;
133 }
134 
dm_vcalloc(unsigned long nmemb,unsigned long elem_size)135 void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
136 {
137 	unsigned long size;
138 	void *addr;
139 
140 	/*
141 	 * Check that we're not going to overflow.
142 	 */
143 	if (nmemb > (ULONG_MAX / elem_size))
144 		return NULL;
145 
146 	size = nmemb * elem_size;
147 	addr = vzalloc(size);
148 
149 	return addr;
150 }
151 EXPORT_SYMBOL(dm_vcalloc);
152 
153 /*
154  * highs, and targets are managed as dynamic arrays during a
155  * table load.
156  */
alloc_targets(struct dm_table * t,unsigned int num)157 static int alloc_targets(struct dm_table *t, unsigned int num)
158 {
159 	sector_t *n_highs;
160 	struct dm_target *n_targets;
161 
162 	/*
163 	 * Allocate both the target array and offset array at once.
164 	 * Append an empty entry to catch sectors beyond the end of
165 	 * the device.
166 	 */
167 	n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
168 					  sizeof(sector_t));
169 	if (!n_highs)
170 		return -ENOMEM;
171 
172 	n_targets = (struct dm_target *) (n_highs + num);
173 
174 	memset(n_highs, -1, sizeof(*n_highs) * num);
175 	vfree(t->highs);
176 
177 	t->num_allocated = num;
178 	t->highs = n_highs;
179 	t->targets = n_targets;
180 
181 	return 0;
182 }
183 
dm_table_create(struct dm_table ** result,fmode_t mode,unsigned num_targets,struct mapped_device * md)184 int dm_table_create(struct dm_table **result, fmode_t mode,
185 		    unsigned num_targets, struct mapped_device *md)
186 {
187 	struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
188 
189 	if (!t)
190 		return -ENOMEM;
191 
192 	INIT_LIST_HEAD(&t->devices);
193 	INIT_LIST_HEAD(&t->target_callbacks);
194 
195 	if (!num_targets)
196 		num_targets = KEYS_PER_NODE;
197 
198 	num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
199 
200 	if (!num_targets) {
201 		kfree(t);
202 		return -ENOMEM;
203 	}
204 
205 	if (alloc_targets(t, num_targets)) {
206 		kfree(t);
207 		return -ENOMEM;
208 	}
209 
210 	t->mode = mode;
211 	t->md = md;
212 	*result = t;
213 	return 0;
214 }
215 
free_devices(struct list_head * devices,struct mapped_device * md)216 static void free_devices(struct list_head *devices, struct mapped_device *md)
217 {
218 	struct list_head *tmp, *next;
219 
220 	list_for_each_safe(tmp, next, devices) {
221 		struct dm_dev_internal *dd =
222 		    list_entry(tmp, struct dm_dev_internal, list);
223 		DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s",
224 		       dm_device_name(md), dd->dm_dev->name);
225 		dm_put_table_device(md, dd->dm_dev);
226 		kfree(dd);
227 	}
228 }
229 
dm_table_destroy(struct dm_table * t)230 void dm_table_destroy(struct dm_table *t)
231 {
232 	unsigned int i;
233 
234 	if (!t)
235 		return;
236 
237 	/* free the indexes */
238 	if (t->depth >= 2)
239 		vfree(t->index[t->depth - 2]);
240 
241 	/* free the targets */
242 	for (i = 0; i < t->num_targets; i++) {
243 		struct dm_target *tgt = t->targets + i;
244 
245 		if (tgt->type->dtr)
246 			tgt->type->dtr(tgt);
247 
248 		dm_put_target_type(tgt->type);
249 	}
250 
251 	vfree(t->highs);
252 
253 	/* free the device list */
254 	free_devices(&t->devices, t->md);
255 
256 	dm_free_md_mempools(t->mempools);
257 
258 	kfree(t);
259 }
260 
261 /*
262  * See if we've already got a device in the list.
263  */
find_device(struct list_head * l,dev_t dev)264 static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
265 {
266 	struct dm_dev_internal *dd;
267 
268 	list_for_each_entry (dd, l, list)
269 		if (dd->dm_dev->bdev->bd_dev == dev)
270 			return dd;
271 
272 	return NULL;
273 }
274 
275 /*
276  * If possible, this checks an area of a destination device is invalid.
277  */
device_area_is_invalid(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)278 static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
279 				  sector_t start, sector_t len, void *data)
280 {
281 	struct request_queue *q;
282 	struct queue_limits *limits = data;
283 	struct block_device *bdev = dev->bdev;
284 	sector_t dev_size =
285 		i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
286 	unsigned short logical_block_size_sectors =
287 		limits->logical_block_size >> SECTOR_SHIFT;
288 	char b[BDEVNAME_SIZE];
289 
290 	/*
291 	 * Some devices exist without request functions,
292 	 * such as loop devices not yet bound to backing files.
293 	 * Forbid the use of such devices.
294 	 */
295 	q = bdev_get_queue(bdev);
296 	if (!q || !q->make_request_fn) {
297 		DMWARN("%s: %s is not yet initialised: "
298 		       "start=%llu, len=%llu, dev_size=%llu",
299 		       dm_device_name(ti->table->md), bdevname(bdev, b),
300 		       (unsigned long long)start,
301 		       (unsigned long long)len,
302 		       (unsigned long long)dev_size);
303 		return 1;
304 	}
305 
306 	if (!dev_size)
307 		return 0;
308 
309 	if ((start >= dev_size) || (start + len > dev_size)) {
310 		DMWARN("%s: %s too small for target: "
311 		       "start=%llu, len=%llu, dev_size=%llu",
312 		       dm_device_name(ti->table->md), bdevname(bdev, b),
313 		       (unsigned long long)start,
314 		       (unsigned long long)len,
315 		       (unsigned long long)dev_size);
316 		return 1;
317 	}
318 
319 	if (logical_block_size_sectors <= 1)
320 		return 0;
321 
322 	if (start & (logical_block_size_sectors - 1)) {
323 		DMWARN("%s: start=%llu not aligned to h/w "
324 		       "logical block size %u of %s",
325 		       dm_device_name(ti->table->md),
326 		       (unsigned long long)start,
327 		       limits->logical_block_size, bdevname(bdev, b));
328 		return 1;
329 	}
330 
331 	if (len & (logical_block_size_sectors - 1)) {
332 		DMWARN("%s: len=%llu not aligned to h/w "
333 		       "logical block size %u of %s",
334 		       dm_device_name(ti->table->md),
335 		       (unsigned long long)len,
336 		       limits->logical_block_size, bdevname(bdev, b));
337 		return 1;
338 	}
339 
340 	return 0;
341 }
342 
343 /*
344  * This upgrades the mode on an already open dm_dev, being
345  * careful to leave things as they were if we fail to reopen the
346  * device and not to touch the existing bdev field in case
347  * it is accessed concurrently inside dm_table_any_congested().
348  */
upgrade_mode(struct dm_dev_internal * dd,fmode_t new_mode,struct mapped_device * md)349 static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
350 			struct mapped_device *md)
351 {
352 	int r;
353 	struct dm_dev *old_dev, *new_dev;
354 
355 	old_dev = dd->dm_dev;
356 
357 	r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
358 				dd->dm_dev->mode | new_mode, &new_dev);
359 	if (r)
360 		return r;
361 
362 	dd->dm_dev = new_dev;
363 	dm_put_table_device(md, old_dev);
364 
365 	return 0;
366 }
367 
368 /*
369  * Convert the path to a device
370  */
dm_get_dev_t(const char * path)371 dev_t dm_get_dev_t(const char *path)
372 {
373 	dev_t uninitialized_var(dev);
374 	struct block_device *bdev;
375 
376 	bdev = lookup_bdev(path);
377 	if (IS_ERR(bdev))
378 		dev = name_to_dev_t(path);
379 	else {
380 		dev = bdev->bd_dev;
381 		bdput(bdev);
382 	}
383 
384 	return dev;
385 }
386 EXPORT_SYMBOL_GPL(dm_get_dev_t);
387 
388 /*
389  * Add a device to the list, or just increment the usage count if
390  * it's already present.
391  */
dm_get_device(struct dm_target * ti,const char * path,fmode_t mode,struct dm_dev ** result)392 int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
393 		  struct dm_dev **result)
394 {
395 	int r;
396 	dev_t dev;
397 	unsigned int major, minor;
398 	char dummy;
399 	struct dm_dev_internal *dd;
400 	struct dm_table *t = ti->table;
401 
402 	BUG_ON(!t);
403 
404 	if (sscanf(path, "%u:%u%c", &major, &minor, &dummy) == 2) {
405 		/* Extract the major/minor numbers */
406 		dev = MKDEV(major, minor);
407 		if (MAJOR(dev) != major || MINOR(dev) != minor)
408 			return -EOVERFLOW;
409 	} else {
410 		dev = dm_get_dev_t(path);
411 		if (!dev)
412 			return -ENODEV;
413 	}
414 
415 	dd = find_device(&t->devices, dev);
416 	if (!dd) {
417 		dd = kmalloc(sizeof(*dd), GFP_KERNEL);
418 		if (!dd)
419 			return -ENOMEM;
420 
421 		if ((r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev))) {
422 			kfree(dd);
423 			return r;
424 		}
425 
426 		atomic_set(&dd->count, 0);
427 		list_add(&dd->list, &t->devices);
428 
429 	} else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
430 		r = upgrade_mode(dd, mode, t->md);
431 		if (r)
432 			return r;
433 	}
434 	atomic_inc(&dd->count);
435 
436 	*result = dd->dm_dev;
437 	return 0;
438 }
439 EXPORT_SYMBOL(dm_get_device);
440 
dm_set_device_limits(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)441 static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
442 				sector_t start, sector_t len, void *data)
443 {
444 	struct queue_limits *limits = data;
445 	struct block_device *bdev = dev->bdev;
446 	struct request_queue *q = bdev_get_queue(bdev);
447 	char b[BDEVNAME_SIZE];
448 
449 	if (unlikely(!q)) {
450 		DMWARN("%s: Cannot set limits for nonexistent device %s",
451 		       dm_device_name(ti->table->md), bdevname(bdev, b));
452 		return 0;
453 	}
454 
455 	if (bdev_stack_limits(limits, bdev, start) < 0)
456 		DMWARN("%s: adding target device %s caused an alignment inconsistency: "
457 		       "physical_block_size=%u, logical_block_size=%u, "
458 		       "alignment_offset=%u, start=%llu",
459 		       dm_device_name(ti->table->md), bdevname(bdev, b),
460 		       q->limits.physical_block_size,
461 		       q->limits.logical_block_size,
462 		       q->limits.alignment_offset,
463 		       (unsigned long long) start << SECTOR_SHIFT);
464 
465 	return 0;
466 }
467 
468 /*
469  * Decrement a device's use count and remove it if necessary.
470  */
dm_put_device(struct dm_target * ti,struct dm_dev * d)471 void dm_put_device(struct dm_target *ti, struct dm_dev *d)
472 {
473 	int found = 0;
474 	struct list_head *devices = &ti->table->devices;
475 	struct dm_dev_internal *dd;
476 
477 	list_for_each_entry(dd, devices, list) {
478 		if (dd->dm_dev == d) {
479 			found = 1;
480 			break;
481 		}
482 	}
483 	if (!found) {
484 		DMWARN("%s: device %s not in table devices list",
485 		       dm_device_name(ti->table->md), d->name);
486 		return;
487 	}
488 	if (atomic_dec_and_test(&dd->count)) {
489 		dm_put_table_device(ti->table->md, d);
490 		list_del(&dd->list);
491 		kfree(dd);
492 	}
493 }
494 EXPORT_SYMBOL(dm_put_device);
495 
496 /*
497  * Checks to see if the target joins onto the end of the table.
498  */
adjoin(struct dm_table * table,struct dm_target * ti)499 static int adjoin(struct dm_table *table, struct dm_target *ti)
500 {
501 	struct dm_target *prev;
502 
503 	if (!table->num_targets)
504 		return !ti->begin;
505 
506 	prev = &table->targets[table->num_targets - 1];
507 	return (ti->begin == (prev->begin + prev->len));
508 }
509 
510 /*
511  * Used to dynamically allocate the arg array.
512  *
513  * We do first allocation with GFP_NOIO because dm-mpath and dm-thin must
514  * process messages even if some device is suspended. These messages have a
515  * small fixed number of arguments.
516  *
517  * On the other hand, dm-switch needs to process bulk data using messages and
518  * excessive use of GFP_NOIO could cause trouble.
519  */
realloc_argv(unsigned * size,char ** old_argv)520 static char **realloc_argv(unsigned *size, char **old_argv)
521 {
522 	char **argv;
523 	unsigned new_size;
524 	gfp_t gfp;
525 
526 	if (*size) {
527 		new_size = *size * 2;
528 		gfp = GFP_KERNEL;
529 	} else {
530 		new_size = 8;
531 		gfp = GFP_NOIO;
532 	}
533 	argv = kmalloc(new_size * sizeof(*argv), gfp);
534 	if (argv) {
535 		memcpy(argv, old_argv, *size * sizeof(*argv));
536 		*size = new_size;
537 	}
538 
539 	kfree(old_argv);
540 	return argv;
541 }
542 
543 /*
544  * Destructively splits up the argument list to pass to ctr.
545  */
dm_split_args(int * argc,char *** argvp,char * input)546 int dm_split_args(int *argc, char ***argvp, char *input)
547 {
548 	char *start, *end = input, *out, **argv = NULL;
549 	unsigned array_size = 0;
550 
551 	*argc = 0;
552 
553 	if (!input) {
554 		*argvp = NULL;
555 		return 0;
556 	}
557 
558 	argv = realloc_argv(&array_size, argv);
559 	if (!argv)
560 		return -ENOMEM;
561 
562 	while (1) {
563 		/* Skip whitespace */
564 		start = skip_spaces(end);
565 
566 		if (!*start)
567 			break;	/* success, we hit the end */
568 
569 		/* 'out' is used to remove any back-quotes */
570 		end = out = start;
571 		while (*end) {
572 			/* Everything apart from '\0' can be quoted */
573 			if (*end == '\\' && *(end + 1)) {
574 				*out++ = *(end + 1);
575 				end += 2;
576 				continue;
577 			}
578 
579 			if (isspace(*end))
580 				break;	/* end of token */
581 
582 			*out++ = *end++;
583 		}
584 
585 		/* have we already filled the array ? */
586 		if ((*argc + 1) > array_size) {
587 			argv = realloc_argv(&array_size, argv);
588 			if (!argv)
589 				return -ENOMEM;
590 		}
591 
592 		/* we know this is whitespace */
593 		if (*end)
594 			end++;
595 
596 		/* terminate the string and put it in the array */
597 		*out = '\0';
598 		argv[*argc] = start;
599 		(*argc)++;
600 	}
601 
602 	*argvp = argv;
603 	return 0;
604 }
605 
606 /*
607  * Impose necessary and sufficient conditions on a devices's table such
608  * that any incoming bio which respects its logical_block_size can be
609  * processed successfully.  If it falls across the boundary between
610  * two or more targets, the size of each piece it gets split into must
611  * be compatible with the logical_block_size of the target processing it.
612  */
validate_hardware_logical_block_alignment(struct dm_table * table,struct queue_limits * limits)613 static int validate_hardware_logical_block_alignment(struct dm_table *table,
614 						 struct queue_limits *limits)
615 {
616 	/*
617 	 * This function uses arithmetic modulo the logical_block_size
618 	 * (in units of 512-byte sectors).
619 	 */
620 	unsigned short device_logical_block_size_sects =
621 		limits->logical_block_size >> SECTOR_SHIFT;
622 
623 	/*
624 	 * Offset of the start of the next table entry, mod logical_block_size.
625 	 */
626 	unsigned short next_target_start = 0;
627 
628 	/*
629 	 * Given an aligned bio that extends beyond the end of a
630 	 * target, how many sectors must the next target handle?
631 	 */
632 	unsigned short remaining = 0;
633 
634 	struct dm_target *uninitialized_var(ti);
635 	struct queue_limits ti_limits;
636 	unsigned i = 0;
637 
638 	/*
639 	 * Check each entry in the table in turn.
640 	 */
641 	while (i < dm_table_get_num_targets(table)) {
642 		ti = dm_table_get_target(table, i++);
643 
644 		blk_set_stacking_limits(&ti_limits);
645 
646 		/* combine all target devices' limits */
647 		if (ti->type->iterate_devices)
648 			ti->type->iterate_devices(ti, dm_set_device_limits,
649 						  &ti_limits);
650 
651 		/*
652 		 * If the remaining sectors fall entirely within this
653 		 * table entry are they compatible with its logical_block_size?
654 		 */
655 		if (remaining < ti->len &&
656 		    remaining & ((ti_limits.logical_block_size >>
657 				  SECTOR_SHIFT) - 1))
658 			break;	/* Error */
659 
660 		next_target_start =
661 		    (unsigned short) ((next_target_start + ti->len) &
662 				      (device_logical_block_size_sects - 1));
663 		remaining = next_target_start ?
664 		    device_logical_block_size_sects - next_target_start : 0;
665 	}
666 
667 	if (remaining) {
668 		DMWARN("%s: table line %u (start sect %llu len %llu) "
669 		       "not aligned to h/w logical block size %u",
670 		       dm_device_name(table->md), i,
671 		       (unsigned long long) ti->begin,
672 		       (unsigned long long) ti->len,
673 		       limits->logical_block_size);
674 		return -EINVAL;
675 	}
676 
677 	return 0;
678 }
679 
dm_table_add_target(struct dm_table * t,const char * type,sector_t start,sector_t len,char * params)680 int dm_table_add_target(struct dm_table *t, const char *type,
681 			sector_t start, sector_t len, char *params)
682 {
683 	int r = -EINVAL, argc;
684 	char **argv;
685 	struct dm_target *tgt;
686 
687 	if (t->singleton) {
688 		DMERR("%s: target type %s must appear alone in table",
689 		      dm_device_name(t->md), t->targets->type->name);
690 		return -EINVAL;
691 	}
692 
693 	BUG_ON(t->num_targets >= t->num_allocated);
694 
695 	tgt = t->targets + t->num_targets;
696 	memset(tgt, 0, sizeof(*tgt));
697 
698 	if (!len) {
699 		DMERR("%s: zero-length target", dm_device_name(t->md));
700 		return -EINVAL;
701 	}
702 
703 	tgt->type = dm_get_target_type(type);
704 	if (!tgt->type) {
705 		DMERR("%s: %s: unknown target type", dm_device_name(t->md),
706 		      type);
707 		return -EINVAL;
708 	}
709 
710 	if (dm_target_needs_singleton(tgt->type)) {
711 		if (t->num_targets) {
712 			DMERR("%s: target type %s must appear alone in table",
713 			      dm_device_name(t->md), type);
714 			return -EINVAL;
715 		}
716 		t->singleton = 1;
717 	}
718 
719 	if (dm_target_always_writeable(tgt->type) && !(t->mode & FMODE_WRITE)) {
720 		DMERR("%s: target type %s may not be included in read-only tables",
721 		      dm_device_name(t->md), type);
722 		return -EINVAL;
723 	}
724 
725 	if (t->immutable_target_type) {
726 		if (t->immutable_target_type != tgt->type) {
727 			DMERR("%s: immutable target type %s cannot be mixed with other target types",
728 			      dm_device_name(t->md), t->immutable_target_type->name);
729 			return -EINVAL;
730 		}
731 	} else if (dm_target_is_immutable(tgt->type)) {
732 		if (t->num_targets) {
733 			DMERR("%s: immutable target type %s cannot be mixed with other target types",
734 			      dm_device_name(t->md), tgt->type->name);
735 			return -EINVAL;
736 		}
737 		t->immutable_target_type = tgt->type;
738 	}
739 
740 	tgt->table = t;
741 	tgt->begin = start;
742 	tgt->len = len;
743 	tgt->error = "Unknown error";
744 
745 	/*
746 	 * Does this target adjoin the previous one ?
747 	 */
748 	if (!adjoin(t, tgt)) {
749 		tgt->error = "Gap in table";
750 		r = -EINVAL;
751 		goto bad;
752 	}
753 
754 	r = dm_split_args(&argc, &argv, params);
755 	if (r) {
756 		tgt->error = "couldn't split parameters (insufficient memory)";
757 		goto bad;
758 	}
759 
760 	r = tgt->type->ctr(tgt, argc, argv);
761 	kfree(argv);
762 	if (r)
763 		goto bad;
764 
765 	t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
766 
767 	if (!tgt->num_discard_bios && tgt->discards_supported)
768 		DMWARN("%s: %s: ignoring discards_supported because num_discard_bios is zero.",
769 		       dm_device_name(t->md), type);
770 
771 	return 0;
772 
773  bad:
774 	DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
775 	dm_put_target_type(tgt->type);
776 	return r;
777 }
778 
779 /*
780  * Target argument parsing helpers.
781  */
validate_next_arg(struct dm_arg * arg,struct dm_arg_set * arg_set,unsigned * value,char ** error,unsigned grouped)782 static int validate_next_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
783 			     unsigned *value, char **error, unsigned grouped)
784 {
785 	const char *arg_str = dm_shift_arg(arg_set);
786 	char dummy;
787 
788 	if (!arg_str ||
789 	    (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
790 	    (*value < arg->min) ||
791 	    (*value > arg->max) ||
792 	    (grouped && arg_set->argc < *value)) {
793 		*error = arg->error;
794 		return -EINVAL;
795 	}
796 
797 	return 0;
798 }
799 
dm_read_arg(struct dm_arg * arg,struct dm_arg_set * arg_set,unsigned * value,char ** error)800 int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
801 		unsigned *value, char **error)
802 {
803 	return validate_next_arg(arg, arg_set, value, error, 0);
804 }
805 EXPORT_SYMBOL(dm_read_arg);
806 
dm_read_arg_group(struct dm_arg * arg,struct dm_arg_set * arg_set,unsigned * value,char ** error)807 int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
808 		      unsigned *value, char **error)
809 {
810 	return validate_next_arg(arg, arg_set, value, error, 1);
811 }
812 EXPORT_SYMBOL(dm_read_arg_group);
813 
dm_shift_arg(struct dm_arg_set * as)814 const char *dm_shift_arg(struct dm_arg_set *as)
815 {
816 	char *r;
817 
818 	if (as->argc) {
819 		as->argc--;
820 		r = *as->argv;
821 		as->argv++;
822 		return r;
823 	}
824 
825 	return NULL;
826 }
827 EXPORT_SYMBOL(dm_shift_arg);
828 
dm_consume_args(struct dm_arg_set * as,unsigned num_args)829 void dm_consume_args(struct dm_arg_set *as, unsigned num_args)
830 {
831 	BUG_ON(as->argc < num_args);
832 	as->argc -= num_args;
833 	as->argv += num_args;
834 }
835 EXPORT_SYMBOL(dm_consume_args);
836 
__table_type_request_based(unsigned table_type)837 static bool __table_type_request_based(unsigned table_type)
838 {
839 	return (table_type == DM_TYPE_REQUEST_BASED ||
840 		table_type == DM_TYPE_MQ_REQUEST_BASED);
841 }
842 
dm_table_set_type(struct dm_table * t)843 static int dm_table_set_type(struct dm_table *t)
844 {
845 	unsigned i;
846 	unsigned bio_based = 0, request_based = 0, hybrid = 0;
847 	bool use_blk_mq = false;
848 	struct dm_target *tgt;
849 	struct dm_dev_internal *dd;
850 	struct list_head *devices;
851 	unsigned live_md_type = dm_get_md_type(t->md);
852 
853 	for (i = 0; i < t->num_targets; i++) {
854 		tgt = t->targets + i;
855 		if (dm_target_hybrid(tgt))
856 			hybrid = 1;
857 		else if (dm_target_request_based(tgt))
858 			request_based = 1;
859 		else
860 			bio_based = 1;
861 
862 		if (bio_based && request_based) {
863 			DMWARN("Inconsistent table: different target types"
864 			       " can't be mixed up");
865 			return -EINVAL;
866 		}
867 	}
868 
869 	if (hybrid && !bio_based && !request_based) {
870 		/*
871 		 * The targets can work either way.
872 		 * Determine the type from the live device.
873 		 * Default to bio-based if device is new.
874 		 */
875 		if (__table_type_request_based(live_md_type))
876 			request_based = 1;
877 		else
878 			bio_based = 1;
879 	}
880 
881 	if (bio_based) {
882 		/* We must use this table as bio-based */
883 		t->type = DM_TYPE_BIO_BASED;
884 		return 0;
885 	}
886 
887 	BUG_ON(!request_based); /* No targets in this table */
888 
889 	/*
890 	 * Request-based dm supports only tables that have a single target now.
891 	 * To support multiple targets, request splitting support is needed,
892 	 * and that needs lots of changes in the block-layer.
893 	 * (e.g. request completion process for partial completion.)
894 	 */
895 	if (t->num_targets > 1) {
896 		DMWARN("Request-based dm doesn't support multiple targets yet");
897 		return -EINVAL;
898 	}
899 
900 	/* Non-request-stackable devices can't be used for request-based dm */
901 	devices = dm_table_get_devices(t);
902 	list_for_each_entry(dd, devices, list) {
903 		struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
904 
905 		if (!blk_queue_stackable(q)) {
906 			DMERR("table load rejected: including"
907 			      " non-request-stackable devices");
908 			return -EINVAL;
909 		}
910 
911 		if (q->mq_ops)
912 			use_blk_mq = true;
913 	}
914 
915 	if (use_blk_mq) {
916 		/* verify _all_ devices in the table are blk-mq devices */
917 		list_for_each_entry(dd, devices, list)
918 			if (!bdev_get_queue(dd->dm_dev->bdev)->mq_ops) {
919 				DMERR("table load rejected: not all devices"
920 				      " are blk-mq request-stackable");
921 				return -EINVAL;
922 			}
923 		t->type = DM_TYPE_MQ_REQUEST_BASED;
924 
925 	} else if (list_empty(devices) && __table_type_request_based(live_md_type)) {
926 		/* inherit live MD type */
927 		t->type = live_md_type;
928 
929 	} else
930 		t->type = DM_TYPE_REQUEST_BASED;
931 
932 	return 0;
933 }
934 
dm_table_get_type(struct dm_table * t)935 unsigned dm_table_get_type(struct dm_table *t)
936 {
937 	return t->type;
938 }
939 
dm_table_get_immutable_target_type(struct dm_table * t)940 struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
941 {
942 	return t->immutable_target_type;
943 }
944 
dm_table_request_based(struct dm_table * t)945 bool dm_table_request_based(struct dm_table *t)
946 {
947 	return __table_type_request_based(dm_table_get_type(t));
948 }
949 
dm_table_mq_request_based(struct dm_table * t)950 bool dm_table_mq_request_based(struct dm_table *t)
951 {
952 	return dm_table_get_type(t) == DM_TYPE_MQ_REQUEST_BASED;
953 }
954 
dm_table_alloc_md_mempools(struct dm_table * t,struct mapped_device * md)955 static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
956 {
957 	unsigned type = dm_table_get_type(t);
958 	unsigned per_bio_data_size = 0;
959 	struct dm_target *tgt;
960 	unsigned i;
961 
962 	if (unlikely(type == DM_TYPE_NONE)) {
963 		DMWARN("no table type is set, can't allocate mempools");
964 		return -EINVAL;
965 	}
966 
967 	if (type == DM_TYPE_BIO_BASED)
968 		for (i = 0; i < t->num_targets; i++) {
969 			tgt = t->targets + i;
970 			per_bio_data_size = max(per_bio_data_size, tgt->per_bio_data_size);
971 		}
972 
973 	t->mempools = dm_alloc_md_mempools(md, type, t->integrity_supported, per_bio_data_size);
974 	if (!t->mempools)
975 		return -ENOMEM;
976 
977 	return 0;
978 }
979 
dm_table_free_md_mempools(struct dm_table * t)980 void dm_table_free_md_mempools(struct dm_table *t)
981 {
982 	dm_free_md_mempools(t->mempools);
983 	t->mempools = NULL;
984 }
985 
dm_table_get_md_mempools(struct dm_table * t)986 struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
987 {
988 	return t->mempools;
989 }
990 
setup_indexes(struct dm_table * t)991 static int setup_indexes(struct dm_table *t)
992 {
993 	int i;
994 	unsigned int total = 0;
995 	sector_t *indexes;
996 
997 	/* allocate the space for *all* the indexes */
998 	for (i = t->depth - 2; i >= 0; i--) {
999 		t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
1000 		total += t->counts[i];
1001 	}
1002 
1003 	indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
1004 	if (!indexes)
1005 		return -ENOMEM;
1006 
1007 	/* set up internal nodes, bottom-up */
1008 	for (i = t->depth - 2; i >= 0; i--) {
1009 		t->index[i] = indexes;
1010 		indexes += (KEYS_PER_NODE * t->counts[i]);
1011 		setup_btree_index(i, t);
1012 	}
1013 
1014 	return 0;
1015 }
1016 
1017 /*
1018  * Builds the btree to index the map.
1019  */
dm_table_build_index(struct dm_table * t)1020 static int dm_table_build_index(struct dm_table *t)
1021 {
1022 	int r = 0;
1023 	unsigned int leaf_nodes;
1024 
1025 	/* how many indexes will the btree have ? */
1026 	leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
1027 	t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
1028 
1029 	/* leaf layer has already been set up */
1030 	t->counts[t->depth - 1] = leaf_nodes;
1031 	t->index[t->depth - 1] = t->highs;
1032 
1033 	if (t->depth >= 2)
1034 		r = setup_indexes(t);
1035 
1036 	return r;
1037 }
1038 
integrity_profile_exists(struct gendisk * disk)1039 static bool integrity_profile_exists(struct gendisk *disk)
1040 {
1041 	return !!blk_get_integrity(disk);
1042 }
1043 
1044 /*
1045  * Get a disk whose integrity profile reflects the table's profile.
1046  * Returns NULL if integrity support was inconsistent or unavailable.
1047  */
dm_table_get_integrity_disk(struct dm_table * t)1048 static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t)
1049 {
1050 	struct list_head *devices = dm_table_get_devices(t);
1051 	struct dm_dev_internal *dd = NULL;
1052 	struct gendisk *prev_disk = NULL, *template_disk = NULL;
1053 
1054 	list_for_each_entry(dd, devices, list) {
1055 		template_disk = dd->dm_dev->bdev->bd_disk;
1056 		if (!integrity_profile_exists(template_disk))
1057 			goto no_integrity;
1058 		else if (prev_disk &&
1059 			 blk_integrity_compare(prev_disk, template_disk) < 0)
1060 			goto no_integrity;
1061 		prev_disk = template_disk;
1062 	}
1063 
1064 	return template_disk;
1065 
1066 no_integrity:
1067 	if (prev_disk)
1068 		DMWARN("%s: integrity not set: %s and %s profile mismatch",
1069 		       dm_device_name(t->md),
1070 		       prev_disk->disk_name,
1071 		       template_disk->disk_name);
1072 	return NULL;
1073 }
1074 
1075 /*
1076  * Register the mapped device for blk_integrity support if the
1077  * underlying devices have an integrity profile.  But all devices may
1078  * not have matching profiles (checking all devices isn't reliable
1079  * during table load because this table may use other DM device(s) which
1080  * must be resumed before they will have an initialized integity
1081  * profile).  Consequently, stacked DM devices force a 2 stage integrity
1082  * profile validation: First pass during table load, final pass during
1083  * resume.
1084  */
dm_table_register_integrity(struct dm_table * t)1085 static int dm_table_register_integrity(struct dm_table *t)
1086 {
1087 	struct mapped_device *md = t->md;
1088 	struct gendisk *template_disk = NULL;
1089 
1090 	template_disk = dm_table_get_integrity_disk(t);
1091 	if (!template_disk)
1092 		return 0;
1093 
1094 	if (!integrity_profile_exists(dm_disk(md))) {
1095 		t->integrity_supported = 1;
1096 		/*
1097 		 * Register integrity profile during table load; we can do
1098 		 * this because the final profile must match during resume.
1099 		 */
1100 		blk_integrity_register(dm_disk(md),
1101 				       blk_get_integrity(template_disk));
1102 		return 0;
1103 	}
1104 
1105 	/*
1106 	 * If DM device already has an initialized integrity
1107 	 * profile the new profile should not conflict.
1108 	 */
1109 	if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
1110 		DMWARN("%s: conflict with existing integrity profile: "
1111 		       "%s profile mismatch",
1112 		       dm_device_name(t->md),
1113 		       template_disk->disk_name);
1114 		return 1;
1115 	}
1116 
1117 	/* Preserve existing integrity profile */
1118 	t->integrity_supported = 1;
1119 	return 0;
1120 }
1121 
1122 /*
1123  * Prepares the table for use by building the indices,
1124  * setting the type, and allocating mempools.
1125  */
dm_table_complete(struct dm_table * t)1126 int dm_table_complete(struct dm_table *t)
1127 {
1128 	int r;
1129 
1130 	r = dm_table_set_type(t);
1131 	if (r) {
1132 		DMERR("unable to set table type");
1133 		return r;
1134 	}
1135 
1136 	r = dm_table_build_index(t);
1137 	if (r) {
1138 		DMERR("unable to build btrees");
1139 		return r;
1140 	}
1141 
1142 	r = dm_table_register_integrity(t);
1143 	if (r) {
1144 		DMERR("could not register integrity profile.");
1145 		return r;
1146 	}
1147 
1148 	r = dm_table_alloc_md_mempools(t, t->md);
1149 	if (r)
1150 		DMERR("unable to allocate mempools");
1151 
1152 	return r;
1153 }
1154 
1155 static DEFINE_MUTEX(_event_lock);
dm_table_event_callback(struct dm_table * t,void (* fn)(void *),void * context)1156 void dm_table_event_callback(struct dm_table *t,
1157 			     void (*fn)(void *), void *context)
1158 {
1159 	mutex_lock(&_event_lock);
1160 	t->event_fn = fn;
1161 	t->event_context = context;
1162 	mutex_unlock(&_event_lock);
1163 }
1164 
dm_table_event(struct dm_table * t)1165 void dm_table_event(struct dm_table *t)
1166 {
1167 	mutex_lock(&_event_lock);
1168 	if (t->event_fn)
1169 		t->event_fn(t->event_context);
1170 	mutex_unlock(&_event_lock);
1171 }
1172 EXPORT_SYMBOL(dm_table_event);
1173 
dm_table_get_size(struct dm_table * t)1174 inline sector_t dm_table_get_size(struct dm_table *t)
1175 {
1176 	return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
1177 }
1178 EXPORT_SYMBOL(dm_table_get_size);
1179 
dm_table_get_target(struct dm_table * t,unsigned int index)1180 struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
1181 {
1182 	if (index >= t->num_targets)
1183 		return NULL;
1184 
1185 	return t->targets + index;
1186 }
1187 
1188 /*
1189  * Search the btree for the correct target.
1190  *
1191  * Caller should check returned pointer with dm_target_is_valid()
1192  * to trap I/O beyond end of device.
1193  */
dm_table_find_target(struct dm_table * t,sector_t sector)1194 struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
1195 {
1196 	unsigned int l, n = 0, k = 0;
1197 	sector_t *node;
1198 
1199 	if (unlikely(sector >= dm_table_get_size(t)))
1200 		return &t->targets[t->num_targets];
1201 
1202 	for (l = 0; l < t->depth; l++) {
1203 		n = get_child(n, k);
1204 		node = get_node(t, l, n);
1205 
1206 		for (k = 0; k < KEYS_PER_NODE; k++)
1207 			if (node[k] >= sector)
1208 				break;
1209 	}
1210 
1211 	return &t->targets[(KEYS_PER_NODE * n) + k];
1212 }
1213 
1214 /*
1215  * type->iterate_devices() should be called when the sanity check needs to
1216  * iterate and check all underlying data devices. iterate_devices() will
1217  * iterate all underlying data devices until it encounters a non-zero return
1218  * code, returned by whether the input iterate_devices_callout_fn, or
1219  * iterate_devices() itself internally.
1220  *
1221  * For some target type (e.g. dm-stripe), one call of iterate_devices() may
1222  * iterate multiple underlying devices internally, in which case a non-zero
1223  * return code returned by iterate_devices_callout_fn will stop the iteration
1224  * in advance.
1225  *
1226  * Cases requiring _any_ underlying device supporting some kind of attribute,
1227  * should use the iteration structure like dm_table_any_dev_attr(), or call
1228  * it directly. @func should handle semantics of positive examples, e.g.
1229  * capable of something.
1230  *
1231  * Cases requiring _all_ underlying devices supporting some kind of attribute,
1232  * should use the iteration structure like dm_table_supports_nowait() or
1233  * dm_table_supports_discards(). Or introduce dm_table_all_devs_attr() that
1234  * uses an @anti_func that handle semantics of counter examples, e.g. not
1235  * capable of something. So: return !dm_table_any_dev_attr(t, anti_func);
1236  */
dm_table_any_dev_attr(struct dm_table * t,iterate_devices_callout_fn func)1237 static bool dm_table_any_dev_attr(struct dm_table *t,
1238 				  iterate_devices_callout_fn func)
1239 {
1240 	struct dm_target *ti;
1241 	unsigned int i;
1242 
1243 	for (i = 0; i < dm_table_get_num_targets(t); i++) {
1244 		ti = dm_table_get_target(t, i);
1245 
1246 		if (ti->type->iterate_devices &&
1247 		    ti->type->iterate_devices(ti, func, NULL))
1248 			return true;
1249         }
1250 
1251 	return false;
1252 }
1253 
count_device(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)1254 static int count_device(struct dm_target *ti, struct dm_dev *dev,
1255 			sector_t start, sector_t len, void *data)
1256 {
1257 	unsigned *num_devices = data;
1258 
1259 	(*num_devices)++;
1260 
1261 	return 0;
1262 }
1263 
1264 /*
1265  * Check whether a table has no data devices attached using each
1266  * target's iterate_devices method.
1267  * Returns false if the result is unknown because a target doesn't
1268  * support iterate_devices.
1269  */
dm_table_has_no_data_devices(struct dm_table * table)1270 bool dm_table_has_no_data_devices(struct dm_table *table)
1271 {
1272 	struct dm_target *uninitialized_var(ti);
1273 	unsigned i = 0, num_devices = 0;
1274 
1275 	while (i < dm_table_get_num_targets(table)) {
1276 		ti = dm_table_get_target(table, i++);
1277 
1278 		if (!ti->type->iterate_devices)
1279 			return false;
1280 
1281 		ti->type->iterate_devices(ti, count_device, &num_devices);
1282 		if (num_devices)
1283 			return false;
1284 	}
1285 
1286 	return true;
1287 }
1288 
1289 /*
1290  * Establish the new table's queue_limits and validate them.
1291  */
dm_calculate_queue_limits(struct dm_table * table,struct queue_limits * limits)1292 int dm_calculate_queue_limits(struct dm_table *table,
1293 			      struct queue_limits *limits)
1294 {
1295 	struct dm_target *uninitialized_var(ti);
1296 	struct queue_limits ti_limits;
1297 	unsigned i = 0;
1298 
1299 	blk_set_stacking_limits(limits);
1300 
1301 	while (i < dm_table_get_num_targets(table)) {
1302 		blk_set_stacking_limits(&ti_limits);
1303 
1304 		ti = dm_table_get_target(table, i++);
1305 
1306 		if (!ti->type->iterate_devices)
1307 			goto combine_limits;
1308 
1309 		/*
1310 		 * Combine queue limits of all the devices this target uses.
1311 		 */
1312 		ti->type->iterate_devices(ti, dm_set_device_limits,
1313 					  &ti_limits);
1314 
1315 		/* Set I/O hints portion of queue limits */
1316 		if (ti->type->io_hints)
1317 			ti->type->io_hints(ti, &ti_limits);
1318 
1319 		/*
1320 		 * Check each device area is consistent with the target's
1321 		 * overall queue limits.
1322 		 */
1323 		if (ti->type->iterate_devices(ti, device_area_is_invalid,
1324 					      &ti_limits))
1325 			return -EINVAL;
1326 
1327 combine_limits:
1328 		/*
1329 		 * Merge this target's queue limits into the overall limits
1330 		 * for the table.
1331 		 */
1332 		if (blk_stack_limits(limits, &ti_limits, 0) < 0)
1333 			DMWARN("%s: adding target device "
1334 			       "(start sect %llu len %llu) "
1335 			       "caused an alignment inconsistency",
1336 			       dm_device_name(table->md),
1337 			       (unsigned long long) ti->begin,
1338 			       (unsigned long long) ti->len);
1339 	}
1340 
1341 	return validate_hardware_logical_block_alignment(table, limits);
1342 }
1343 
1344 /*
1345  * Verify that all devices have an integrity profile that matches the
1346  * DM device's registered integrity profile.  If the profiles don't
1347  * match then unregister the DM device's integrity profile.
1348  */
dm_table_verify_integrity(struct dm_table * t)1349 static void dm_table_verify_integrity(struct dm_table *t)
1350 {
1351 	struct gendisk *template_disk = NULL;
1352 
1353 	if (t->integrity_supported) {
1354 		/*
1355 		 * Verify that the original integrity profile
1356 		 * matches all the devices in this table.
1357 		 */
1358 		template_disk = dm_table_get_integrity_disk(t);
1359 		if (template_disk &&
1360 		    blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
1361 			return;
1362 	}
1363 
1364 	if (integrity_profile_exists(dm_disk(t->md))) {
1365 		DMWARN("%s: unable to establish an integrity profile",
1366 		       dm_device_name(t->md));
1367 		blk_integrity_unregister(dm_disk(t->md));
1368 	}
1369 }
1370 
device_flush_capable(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)1371 static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
1372 				sector_t start, sector_t len, void *data)
1373 {
1374 	unsigned flush = (*(unsigned *)data);
1375 	struct request_queue *q = bdev_get_queue(dev->bdev);
1376 
1377 	return q && (q->flush_flags & flush);
1378 }
1379 
dm_table_supports_flush(struct dm_table * t,unsigned flush)1380 static bool dm_table_supports_flush(struct dm_table *t, unsigned flush)
1381 {
1382 	struct dm_target *ti;
1383 	unsigned i = 0;
1384 
1385 	/*
1386 	 * Require at least one underlying device to support flushes.
1387 	 * t->devices includes internal dm devices such as mirror logs
1388 	 * so we need to use iterate_devices here, which targets
1389 	 * supporting flushes must provide.
1390 	 */
1391 	while (i < dm_table_get_num_targets(t)) {
1392 		ti = dm_table_get_target(t, i++);
1393 
1394 		if (!ti->num_flush_bios)
1395 			continue;
1396 
1397 		if (ti->flush_supported)
1398 			return true;
1399 
1400 		if (ti->type->iterate_devices &&
1401 		    ti->type->iterate_devices(ti, device_flush_capable, &flush))
1402 			return true;
1403 	}
1404 
1405 	return false;
1406 }
1407 
dm_table_discard_zeroes_data(struct dm_table * t)1408 static bool dm_table_discard_zeroes_data(struct dm_table *t)
1409 {
1410 	struct dm_target *ti;
1411 	unsigned i = 0;
1412 
1413 	/* Ensure that all targets supports discard_zeroes_data. */
1414 	while (i < dm_table_get_num_targets(t)) {
1415 		ti = dm_table_get_target(t, i++);
1416 
1417 		if (ti->discard_zeroes_data_unsupported)
1418 			return false;
1419 	}
1420 
1421 	return true;
1422 }
1423 
device_is_rotational(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)1424 static int device_is_rotational(struct dm_target *ti, struct dm_dev *dev,
1425 				sector_t start, sector_t len, void *data)
1426 {
1427 	struct request_queue *q = bdev_get_queue(dev->bdev);
1428 
1429 	return q && !blk_queue_nonrot(q);
1430 }
1431 
device_is_not_random(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)1432 static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
1433 			     sector_t start, sector_t len, void *data)
1434 {
1435 	struct request_queue *q = bdev_get_queue(dev->bdev);
1436 
1437 	return q && !blk_queue_add_random(q);
1438 }
1439 
queue_no_sg_merge(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)1440 static int queue_no_sg_merge(struct dm_target *ti, struct dm_dev *dev,
1441 			     sector_t start, sector_t len, void *data)
1442 {
1443 	struct request_queue *q = bdev_get_queue(dev->bdev);
1444 
1445 	return q && test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
1446 }
1447 
device_not_write_same_capable(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)1448 static int device_not_write_same_capable(struct dm_target *ti, struct dm_dev *dev,
1449 					 sector_t start, sector_t len, void *data)
1450 {
1451 	struct request_queue *q = bdev_get_queue(dev->bdev);
1452 
1453 	return q && !q->limits.max_write_same_sectors;
1454 }
1455 
dm_table_supports_write_same(struct dm_table * t)1456 static bool dm_table_supports_write_same(struct dm_table *t)
1457 {
1458 	struct dm_target *ti;
1459 	unsigned i = 0;
1460 
1461 	while (i < dm_table_get_num_targets(t)) {
1462 		ti = dm_table_get_target(t, i++);
1463 
1464 		if (!ti->num_write_same_bios)
1465 			return false;
1466 
1467 		if (!ti->type->iterate_devices ||
1468 		    ti->type->iterate_devices(ti, device_not_write_same_capable, NULL))
1469 			return false;
1470 	}
1471 
1472 	return true;
1473 }
1474 
device_discard_capable(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)1475 static int device_discard_capable(struct dm_target *ti, struct dm_dev *dev,
1476 				  sector_t start, sector_t len, void *data)
1477 {
1478 	struct request_queue *q = bdev_get_queue(dev->bdev);
1479 
1480 	return q && blk_queue_discard(q);
1481 }
1482 
dm_table_supports_discards(struct dm_table * t)1483 static bool dm_table_supports_discards(struct dm_table *t)
1484 {
1485 	struct dm_target *ti;
1486 	unsigned i = 0;
1487 
1488 	/*
1489 	 * Unless any target used by the table set discards_supported,
1490 	 * require at least one underlying device to support discards.
1491 	 * t->devices includes internal dm devices such as mirror logs
1492 	 * so we need to use iterate_devices here, which targets
1493 	 * supporting discard selectively must provide.
1494 	 */
1495 	while (i < dm_table_get_num_targets(t)) {
1496 		ti = dm_table_get_target(t, i++);
1497 
1498 		if (!ti->num_discard_bios)
1499 			continue;
1500 
1501 		if (ti->discards_supported)
1502 			return true;
1503 
1504 		if (ti->type->iterate_devices &&
1505 		    ti->type->iterate_devices(ti, device_discard_capable, NULL))
1506 			return true;
1507 	}
1508 
1509 	return false;
1510 }
1511 
dm_table_set_restrictions(struct dm_table * t,struct request_queue * q,struct queue_limits * limits)1512 void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
1513 			       struct queue_limits *limits)
1514 {
1515 	unsigned flush = 0;
1516 
1517 	/*
1518 	 * Copy table's limits to the DM device's request_queue
1519 	 */
1520 	q->limits = *limits;
1521 
1522 	if (!dm_table_supports_discards(t))
1523 		queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1524 	else
1525 		queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1526 
1527 	if (dm_table_supports_flush(t, REQ_FLUSH)) {
1528 		flush |= REQ_FLUSH;
1529 		if (dm_table_supports_flush(t, REQ_FUA))
1530 			flush |= REQ_FUA;
1531 	}
1532 	blk_queue_flush(q, flush);
1533 
1534 	if (!dm_table_discard_zeroes_data(t))
1535 		q->limits.discard_zeroes_data = 0;
1536 
1537 	/* Ensure that all underlying devices are non-rotational. */
1538 	if (dm_table_any_dev_attr(t, device_is_rotational))
1539 		queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
1540 	else
1541 		queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
1542 
1543 	if (!dm_table_supports_write_same(t))
1544 		q->limits.max_write_same_sectors = 0;
1545 
1546 	if (dm_table_any_dev_attr(t, queue_no_sg_merge))
1547 		queue_flag_set_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
1548 	else
1549 		queue_flag_clear_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
1550 
1551 	dm_table_verify_integrity(t);
1552 
1553 	/*
1554 	 * Determine whether or not this queue's I/O timings contribute
1555 	 * to the entropy pool, Only request-based targets use this.
1556 	 * Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
1557 	 * have it set.
1558 	 */
1559 	if (blk_queue_add_random(q) && dm_table_any_dev_attr(t, device_is_not_random))
1560 		queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
1561 
1562 	/*
1563 	 * QUEUE_FLAG_STACKABLE must be set after all queue settings are
1564 	 * visible to other CPUs because, once the flag is set, incoming bios
1565 	 * are processed by request-based dm, which refers to the queue
1566 	 * settings.
1567 	 * Until the flag set, bios are passed to bio-based dm and queued to
1568 	 * md->deferred where queue settings are not needed yet.
1569 	 * Those bios are passed to request-based dm at the resume time.
1570 	 */
1571 	smp_mb();
1572 	if (dm_table_request_based(t))
1573 		queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
1574 }
1575 
dm_table_get_num_targets(struct dm_table * t)1576 unsigned int dm_table_get_num_targets(struct dm_table *t)
1577 {
1578 	return t->num_targets;
1579 }
1580 
dm_table_get_devices(struct dm_table * t)1581 struct list_head *dm_table_get_devices(struct dm_table *t)
1582 {
1583 	return &t->devices;
1584 }
1585 
dm_table_get_mode(struct dm_table * t)1586 fmode_t dm_table_get_mode(struct dm_table *t)
1587 {
1588 	return t->mode;
1589 }
1590 EXPORT_SYMBOL(dm_table_get_mode);
1591 
1592 enum suspend_mode {
1593 	PRESUSPEND,
1594 	PRESUSPEND_UNDO,
1595 	POSTSUSPEND,
1596 };
1597 
suspend_targets(struct dm_table * t,enum suspend_mode mode)1598 static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
1599 {
1600 	int i = t->num_targets;
1601 	struct dm_target *ti = t->targets;
1602 
1603 	while (i--) {
1604 		switch (mode) {
1605 		case PRESUSPEND:
1606 			if (ti->type->presuspend)
1607 				ti->type->presuspend(ti);
1608 			break;
1609 		case PRESUSPEND_UNDO:
1610 			if (ti->type->presuspend_undo)
1611 				ti->type->presuspend_undo(ti);
1612 			break;
1613 		case POSTSUSPEND:
1614 			if (ti->type->postsuspend)
1615 				ti->type->postsuspend(ti);
1616 			break;
1617 		}
1618 		ti++;
1619 	}
1620 }
1621 
dm_table_presuspend_targets(struct dm_table * t)1622 void dm_table_presuspend_targets(struct dm_table *t)
1623 {
1624 	if (!t)
1625 		return;
1626 
1627 	suspend_targets(t, PRESUSPEND);
1628 }
1629 
dm_table_presuspend_undo_targets(struct dm_table * t)1630 void dm_table_presuspend_undo_targets(struct dm_table *t)
1631 {
1632 	if (!t)
1633 		return;
1634 
1635 	suspend_targets(t, PRESUSPEND_UNDO);
1636 }
1637 
dm_table_postsuspend_targets(struct dm_table * t)1638 void dm_table_postsuspend_targets(struct dm_table *t)
1639 {
1640 	if (!t)
1641 		return;
1642 
1643 	suspend_targets(t, POSTSUSPEND);
1644 }
1645 
dm_table_resume_targets(struct dm_table * t)1646 int dm_table_resume_targets(struct dm_table *t)
1647 {
1648 	int i, r = 0;
1649 
1650 	for (i = 0; i < t->num_targets; i++) {
1651 		struct dm_target *ti = t->targets + i;
1652 
1653 		if (!ti->type->preresume)
1654 			continue;
1655 
1656 		r = ti->type->preresume(ti);
1657 		if (r) {
1658 			DMERR("%s: %s: preresume failed, error = %d",
1659 			      dm_device_name(t->md), ti->type->name, r);
1660 			return r;
1661 		}
1662 	}
1663 
1664 	for (i = 0; i < t->num_targets; i++) {
1665 		struct dm_target *ti = t->targets + i;
1666 
1667 		if (ti->type->resume)
1668 			ti->type->resume(ti);
1669 	}
1670 
1671 	return 0;
1672 }
1673 
dm_table_add_target_callbacks(struct dm_table * t,struct dm_target_callbacks * cb)1674 void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
1675 {
1676 	list_add(&cb->list, &t->target_callbacks);
1677 }
1678 EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
1679 
dm_table_any_congested(struct dm_table * t,int bdi_bits)1680 int dm_table_any_congested(struct dm_table *t, int bdi_bits)
1681 {
1682 	struct dm_dev_internal *dd;
1683 	struct list_head *devices = dm_table_get_devices(t);
1684 	struct dm_target_callbacks *cb;
1685 	int r = 0;
1686 
1687 	list_for_each_entry(dd, devices, list) {
1688 		struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
1689 		char b[BDEVNAME_SIZE];
1690 
1691 		if (likely(q))
1692 			r |= bdi_congested(&q->backing_dev_info, bdi_bits);
1693 		else
1694 			DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
1695 				     dm_device_name(t->md),
1696 				     bdevname(dd->dm_dev->bdev, b));
1697 	}
1698 
1699 	list_for_each_entry(cb, &t->target_callbacks, list)
1700 		if (cb->congested_fn)
1701 			r |= cb->congested_fn(cb, bdi_bits);
1702 
1703 	return r;
1704 }
1705 
dm_table_get_md(struct dm_table * t)1706 struct mapped_device *dm_table_get_md(struct dm_table *t)
1707 {
1708 	return t->md;
1709 }
1710 EXPORT_SYMBOL(dm_table_get_md);
1711 
dm_table_run_md_queue_async(struct dm_table * t)1712 void dm_table_run_md_queue_async(struct dm_table *t)
1713 {
1714 	struct mapped_device *md;
1715 	struct request_queue *queue;
1716 	unsigned long flags;
1717 
1718 	if (!dm_table_request_based(t))
1719 		return;
1720 
1721 	md = dm_table_get_md(t);
1722 	queue = dm_get_md_queue(md);
1723 	if (queue) {
1724 		if (queue->mq_ops)
1725 			blk_mq_run_hw_queues(queue, true);
1726 		else {
1727 			spin_lock_irqsave(queue->queue_lock, flags);
1728 			blk_run_queue_async(queue);
1729 			spin_unlock_irqrestore(queue->queue_lock, flags);
1730 		}
1731 	}
1732 }
1733 EXPORT_SYMBOL(dm_table_run_md_queue_async);
1734 
1735