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