dm table: replace struct io_restrictions with struct queue_limits
[safe/jmp/linux-2.6] / 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.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/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/mutex.h>
18 #include <linux/delay.h>
19 #include <asm/atomic.h>
20
21 #define DM_MSG_PREFIX "table"
22
23 #define MAX_DEPTH 16
24 #define NODE_SIZE L1_CACHE_BYTES
25 #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
26 #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
27
28 /*
29  * The table has always exactly one reference from either mapped_device->map
30  * or hash_cell->new_map. This reference is not counted in table->holders.
31  * A pair of dm_create_table/dm_destroy_table functions is used for table
32  * creation/destruction.
33  *
34  * Temporary references from the other code increase table->holders. A pair
35  * of dm_table_get/dm_table_put functions is used to manipulate it.
36  *
37  * When the table is about to be destroyed, we wait for table->holders to
38  * drop to zero.
39  */
40
41 struct dm_table {
42         struct mapped_device *md;
43         atomic_t holders;
44
45         /* btree table */
46         unsigned int depth;
47         unsigned int counts[MAX_DEPTH]; /* in nodes */
48         sector_t *index[MAX_DEPTH];
49
50         unsigned int num_targets;
51         unsigned int num_allocated;
52         sector_t *highs;
53         struct dm_target *targets;
54
55         /*
56          * Indicates the rw permissions for the new logical
57          * device.  This should be a combination of FMODE_READ
58          * and FMODE_WRITE.
59          */
60         fmode_t mode;
61
62         /* a list of devices used by this table */
63         struct list_head devices;
64
65         /*
66          * These are optimistic limits taken from all the
67          * targets, some targets will need smaller limits.
68          */
69         struct queue_limits limits;
70
71         /* events get handed up using this callback */
72         void (*event_fn)(void *);
73         void *event_context;
74 };
75
76 /*
77  * Similar to ceiling(log_size(n))
78  */
79 static unsigned int int_log(unsigned int n, unsigned int base)
80 {
81         int result = 0;
82
83         while (n > 1) {
84                 n = dm_div_up(n, base);
85                 result++;
86         }
87
88         return result;
89 }
90
91 /*
92  * Calculate the index of the child node of the n'th node k'th key.
93  */
94 static inline unsigned int get_child(unsigned int n, unsigned int k)
95 {
96         return (n * CHILDREN_PER_NODE) + k;
97 }
98
99 /*
100  * Return the n'th node of level l from table t.
101  */
102 static inline sector_t *get_node(struct dm_table *t,
103                                  unsigned int l, unsigned int n)
104 {
105         return t->index[l] + (n * KEYS_PER_NODE);
106 }
107
108 /*
109  * Return the highest key that you could lookup from the n'th
110  * node on level l of the btree.
111  */
112 static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
113 {
114         for (; l < t->depth - 1; l++)
115                 n = get_child(n, CHILDREN_PER_NODE - 1);
116
117         if (n >= t->counts[l])
118                 return (sector_t) - 1;
119
120         return get_node(t, l, n)[KEYS_PER_NODE - 1];
121 }
122
123 /*
124  * Fills in a level of the btree based on the highs of the level
125  * below it.
126  */
127 static int setup_btree_index(unsigned int l, struct dm_table *t)
128 {
129         unsigned int n, k;
130         sector_t *node;
131
132         for (n = 0U; n < t->counts[l]; n++) {
133                 node = get_node(t, l, n);
134
135                 for (k = 0U; k < KEYS_PER_NODE; k++)
136                         node[k] = high(t, l + 1, get_child(n, k));
137         }
138
139         return 0;
140 }
141
142 void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
143 {
144         unsigned long size;
145         void *addr;
146
147         /*
148          * Check that we're not going to overflow.
149          */
150         if (nmemb > (ULONG_MAX / elem_size))
151                 return NULL;
152
153         size = nmemb * elem_size;
154         addr = vmalloc(size);
155         if (addr)
156                 memset(addr, 0, size);
157
158         return addr;
159 }
160
161 /*
162  * highs, and targets are managed as dynamic arrays during a
163  * table load.
164  */
165 static int alloc_targets(struct dm_table *t, unsigned int num)
166 {
167         sector_t *n_highs;
168         struct dm_target *n_targets;
169         int n = t->num_targets;
170
171         /*
172          * Allocate both the target array and offset array at once.
173          * Append an empty entry to catch sectors beyond the end of
174          * the device.
175          */
176         n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
177                                           sizeof(sector_t));
178         if (!n_highs)
179                 return -ENOMEM;
180
181         n_targets = (struct dm_target *) (n_highs + num);
182
183         if (n) {
184                 memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
185                 memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
186         }
187
188         memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
189         vfree(t->highs);
190
191         t->num_allocated = num;
192         t->highs = n_highs;
193         t->targets = n_targets;
194
195         return 0;
196 }
197
198 int dm_table_create(struct dm_table **result, fmode_t mode,
199                     unsigned num_targets, struct mapped_device *md)
200 {
201         struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
202
203         if (!t)
204                 return -ENOMEM;
205
206         INIT_LIST_HEAD(&t->devices);
207         atomic_set(&t->holders, 0);
208
209         if (!num_targets)
210                 num_targets = KEYS_PER_NODE;
211
212         num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
213
214         if (alloc_targets(t, num_targets)) {
215                 kfree(t);
216                 t = NULL;
217                 return -ENOMEM;
218         }
219
220         t->mode = mode;
221         t->md = md;
222         *result = t;
223         return 0;
224 }
225
226 static void free_devices(struct list_head *devices)
227 {
228         struct list_head *tmp, *next;
229
230         list_for_each_safe(tmp, next, devices) {
231                 struct dm_dev_internal *dd =
232                     list_entry(tmp, struct dm_dev_internal, list);
233                 DMWARN("dm_table_destroy: dm_put_device call missing for %s",
234                        dd->dm_dev.name);
235                 kfree(dd);
236         }
237 }
238
239 void dm_table_destroy(struct dm_table *t)
240 {
241         unsigned int i;
242
243         while (atomic_read(&t->holders))
244                 msleep(1);
245         smp_mb();
246
247         /* free the indexes (see dm_table_complete) */
248         if (t->depth >= 2)
249                 vfree(t->index[t->depth - 2]);
250
251         /* free the targets */
252         for (i = 0; i < t->num_targets; i++) {
253                 struct dm_target *tgt = t->targets + i;
254
255                 if (tgt->type->dtr)
256                         tgt->type->dtr(tgt);
257
258                 dm_put_target_type(tgt->type);
259         }
260
261         vfree(t->highs);
262
263         /* free the device list */
264         if (t->devices.next != &t->devices)
265                 free_devices(&t->devices);
266
267         kfree(t);
268 }
269
270 void dm_table_get(struct dm_table *t)
271 {
272         atomic_inc(&t->holders);
273 }
274
275 void dm_table_put(struct dm_table *t)
276 {
277         if (!t)
278                 return;
279
280         smp_mb__before_atomic_dec();
281         atomic_dec(&t->holders);
282 }
283
284 /*
285  * Checks to see if we need to extend highs or targets.
286  */
287 static inline int check_space(struct dm_table *t)
288 {
289         if (t->num_targets >= t->num_allocated)
290                 return alloc_targets(t, t->num_allocated * 2);
291
292         return 0;
293 }
294
295 /*
296  * See if we've already got a device in the list.
297  */
298 static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
299 {
300         struct dm_dev_internal *dd;
301
302         list_for_each_entry (dd, l, list)
303                 if (dd->dm_dev.bdev->bd_dev == dev)
304                         return dd;
305
306         return NULL;
307 }
308
309 /*
310  * Open a device so we can use it as a map destination.
311  */
312 static int open_dev(struct dm_dev_internal *d, dev_t dev,
313                     struct mapped_device *md)
314 {
315         static char *_claim_ptr = "I belong to device-mapper";
316         struct block_device *bdev;
317
318         int r;
319
320         BUG_ON(d->dm_dev.bdev);
321
322         bdev = open_by_devnum(dev, d->dm_dev.mode);
323         if (IS_ERR(bdev))
324                 return PTR_ERR(bdev);
325         r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
326         if (r)
327                 blkdev_put(bdev, d->dm_dev.mode);
328         else
329                 d->dm_dev.bdev = bdev;
330         return r;
331 }
332
333 /*
334  * Close a device that we've been using.
335  */
336 static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
337 {
338         if (!d->dm_dev.bdev)
339                 return;
340
341         bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
342         blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
343         d->dm_dev.bdev = NULL;
344 }
345
346 /*
347  * If possible, this checks an area of a destination device is valid.
348  */
349 static int device_area_is_valid(struct dm_target *ti, struct block_device *bdev,
350                              sector_t start, sector_t len)
351 {
352         sector_t dev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
353         unsigned short logical_block_size_sectors =
354                 ti->limits.logical_block_size >> SECTOR_SHIFT;
355         char b[BDEVNAME_SIZE];
356
357         if (!dev_size)
358                 return 1;
359
360         if ((start >= dev_size) || (start + len > dev_size)) {
361                 DMWARN("%s: %s too small for target",
362                        dm_device_name(ti->table->md), bdevname(bdev, b));
363                 return 0;
364         }
365
366         if (logical_block_size_sectors <= 1)
367                 return 1;
368
369         if (start & (logical_block_size_sectors - 1)) {
370                 DMWARN("%s: start=%llu not aligned to h/w "
371                        "logical block size %hu of %s",
372                        dm_device_name(ti->table->md),
373                        (unsigned long long)start,
374                        ti->limits.logical_block_size, bdevname(bdev, b));
375                 return 0;
376         }
377
378         if (len & (logical_block_size_sectors - 1)) {
379                 DMWARN("%s: len=%llu not aligned to h/w "
380                        "logical block size %hu of %s",
381                        dm_device_name(ti->table->md),
382                        (unsigned long long)len,
383                        ti->limits.logical_block_size, bdevname(bdev, b));
384                 return 0;
385         }
386
387         return 1;
388 }
389
390 /*
391  * This upgrades the mode on an already open dm_dev, being
392  * careful to leave things as they were if we fail to reopen the
393  * device and not to touch the existing bdev field in case
394  * it is accessed concurrently inside dm_table_any_congested().
395  */
396 static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
397                         struct mapped_device *md)
398 {
399         int r;
400         struct dm_dev_internal dd_new, dd_old;
401
402         dd_new = dd_old = *dd;
403
404         dd_new.dm_dev.mode |= new_mode;
405         dd_new.dm_dev.bdev = NULL;
406
407         r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md);
408         if (r)
409                 return r;
410
411         dd->dm_dev.mode |= new_mode;
412         close_dev(&dd_old, md);
413
414         return 0;
415 }
416
417 /*
418  * Add a device to the list, or just increment the usage count if
419  * it's already present.
420  */
421 static int __table_get_device(struct dm_table *t, struct dm_target *ti,
422                               const char *path, sector_t start, sector_t len,
423                               fmode_t mode, struct dm_dev **result)
424 {
425         int r;
426         dev_t uninitialized_var(dev);
427         struct dm_dev_internal *dd;
428         unsigned int major, minor;
429
430         BUG_ON(!t);
431
432         if (sscanf(path, "%u:%u", &major, &minor) == 2) {
433                 /* Extract the major/minor numbers */
434                 dev = MKDEV(major, minor);
435                 if (MAJOR(dev) != major || MINOR(dev) != minor)
436                         return -EOVERFLOW;
437         } else {
438                 /* convert the path to a device */
439                 struct block_device *bdev = lookup_bdev(path);
440
441                 if (IS_ERR(bdev))
442                         return PTR_ERR(bdev);
443                 dev = bdev->bd_dev;
444                 bdput(bdev);
445         }
446
447         dd = find_device(&t->devices, dev);
448         if (!dd) {
449                 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
450                 if (!dd)
451                         return -ENOMEM;
452
453                 dd->dm_dev.mode = mode;
454                 dd->dm_dev.bdev = NULL;
455
456                 if ((r = open_dev(dd, dev, t->md))) {
457                         kfree(dd);
458                         return r;
459                 }
460
461                 format_dev_t(dd->dm_dev.name, dev);
462
463                 atomic_set(&dd->count, 0);
464                 list_add(&dd->list, &t->devices);
465
466         } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
467                 r = upgrade_mode(dd, mode, t->md);
468                 if (r)
469                         return r;
470         }
471         atomic_inc(&dd->count);
472
473         *result = &dd->dm_dev;
474         return 0;
475 }
476
477 /*
478  * Returns the minimum that is _not_ zero, unless both are zero.
479  */
480 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
481
482 void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
483 {
484         struct request_queue *q = bdev_get_queue(bdev);
485         char b[BDEVNAME_SIZE];
486
487         if (unlikely(!q)) {
488                 DMWARN("%s: Cannot set limits for nonexistent device %s",
489                        dm_device_name(ti->table->md), bdevname(bdev, b));
490                 return;
491         }
492
493         if (blk_stack_limits(&ti->limits, &q->limits, 0) < 0)
494                 DMWARN("%s: target device %s is misaligned",
495                        dm_device_name(ti->table->md), bdevname(bdev, b));
496
497         /*
498          * Check if merge fn is supported.
499          * If not we'll force DM to use PAGE_SIZE or
500          * smaller I/O, just to be safe.
501          */
502
503         if (q->merge_bvec_fn && !ti->type->merge)
504                 ti->limits.max_sectors =
505                         min_not_zero(ti->limits.max_sectors,
506                                      (unsigned int) (PAGE_SIZE >> 9));
507 }
508 EXPORT_SYMBOL_GPL(dm_set_device_limits);
509
510 int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
511                   sector_t len, fmode_t mode, struct dm_dev **result)
512 {
513         int r = __table_get_device(ti->table, ti, path,
514                                    start, len, mode, result);
515
516         if (r)
517                 return r;
518
519         dm_set_device_limits(ti, (*result)->bdev);
520
521         if (!device_area_is_valid(ti, (*result)->bdev, start, len)) {
522                 dm_put_device(ti, *result);
523                 *result = NULL;
524                 return -EINVAL;
525         }
526
527         return r;
528 }
529
530 /*
531  * Decrement a devices use count and remove it if necessary.
532  */
533 void dm_put_device(struct dm_target *ti, struct dm_dev *d)
534 {
535         struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
536                                                   dm_dev);
537
538         if (atomic_dec_and_test(&dd->count)) {
539                 close_dev(dd, ti->table->md);
540                 list_del(&dd->list);
541                 kfree(dd);
542         }
543 }
544
545 /*
546  * Checks to see if the target joins onto the end of the table.
547  */
548 static int adjoin(struct dm_table *table, struct dm_target *ti)
549 {
550         struct dm_target *prev;
551
552         if (!table->num_targets)
553                 return !ti->begin;
554
555         prev = &table->targets[table->num_targets - 1];
556         return (ti->begin == (prev->begin + prev->len));
557 }
558
559 /*
560  * Used to dynamically allocate the arg array.
561  */
562 static char **realloc_argv(unsigned *array_size, char **old_argv)
563 {
564         char **argv;
565         unsigned new_size;
566
567         new_size = *array_size ? *array_size * 2 : 64;
568         argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
569         if (argv) {
570                 memcpy(argv, old_argv, *array_size * sizeof(*argv));
571                 *array_size = new_size;
572         }
573
574         kfree(old_argv);
575         return argv;
576 }
577
578 /*
579  * Destructively splits up the argument list to pass to ctr.
580  */
581 int dm_split_args(int *argc, char ***argvp, char *input)
582 {
583         char *start, *end = input, *out, **argv = NULL;
584         unsigned array_size = 0;
585
586         *argc = 0;
587
588         if (!input) {
589                 *argvp = NULL;
590                 return 0;
591         }
592
593         argv = realloc_argv(&array_size, argv);
594         if (!argv)
595                 return -ENOMEM;
596
597         while (1) {
598                 start = end;
599
600                 /* Skip whitespace */
601                 while (*start && isspace(*start))
602                         start++;
603
604                 if (!*start)
605                         break;  /* success, we hit the end */
606
607                 /* 'out' is used to remove any back-quotes */
608                 end = out = start;
609                 while (*end) {
610                         /* Everything apart from '\0' can be quoted */
611                         if (*end == '\\' && *(end + 1)) {
612                                 *out++ = *(end + 1);
613                                 end += 2;
614                                 continue;
615                         }
616
617                         if (isspace(*end))
618                                 break;  /* end of token */
619
620                         *out++ = *end++;
621                 }
622
623                 /* have we already filled the array ? */
624                 if ((*argc + 1) > array_size) {
625                         argv = realloc_argv(&array_size, argv);
626                         if (!argv)
627                                 return -ENOMEM;
628                 }
629
630                 /* we know this is whitespace */
631                 if (*end)
632                         end++;
633
634                 /* terminate the string and put it in the array */
635                 *out = '\0';
636                 argv[*argc] = start;
637                 (*argc)++;
638         }
639
640         *argvp = argv;
641         return 0;
642 }
643
644 static void init_valid_queue_limits(struct queue_limits *limits)
645 {
646         if (!limits->max_sectors)
647                 limits->max_sectors = SAFE_MAX_SECTORS;
648         if (!limits->max_hw_sectors)
649                 limits->max_hw_sectors = SAFE_MAX_SECTORS;
650         if (!limits->max_phys_segments)
651                 limits->max_phys_segments = MAX_PHYS_SEGMENTS;
652         if (!limits->max_hw_segments)
653                 limits->max_hw_segments = MAX_HW_SEGMENTS;
654         if (!limits->logical_block_size)
655                 limits->logical_block_size = 1 << SECTOR_SHIFT;
656         if (!limits->physical_block_size)
657                 limits->physical_block_size = 1 << SECTOR_SHIFT;
658         if (!limits->io_min)
659                 limits->io_min = 1 << SECTOR_SHIFT;
660         if (!limits->max_segment_size)
661                 limits->max_segment_size = MAX_SEGMENT_SIZE;
662         if (!limits->seg_boundary_mask)
663                 limits->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
664         if (!limits->bounce_pfn)
665                 limits->bounce_pfn = -1;
666         /*
667          * The other fields (alignment_offset, io_opt, misaligned)
668          * hold 0 from the kzalloc().
669          */
670 }
671
672 /*
673  * Impose necessary and sufficient conditions on a devices's table such
674  * that any incoming bio which respects its logical_block_size can be
675  * processed successfully.  If it falls across the boundary between
676  * two or more targets, the size of each piece it gets split into must
677  * be compatible with the logical_block_size of the target processing it.
678  */
679 static int validate_hardware_logical_block_alignment(struct dm_table *table)
680 {
681         /*
682          * This function uses arithmetic modulo the logical_block_size
683          * (in units of 512-byte sectors).
684          */
685         unsigned short device_logical_block_size_sects =
686                 table->limits.logical_block_size >> SECTOR_SHIFT;
687
688         /*
689          * Offset of the start of the next table entry, mod logical_block_size.
690          */
691         unsigned short next_target_start = 0;
692
693         /*
694          * Given an aligned bio that extends beyond the end of a
695          * target, how many sectors must the next target handle?
696          */
697         unsigned short remaining = 0;
698
699         struct dm_target *uninitialized_var(ti);
700         unsigned i = 0;
701
702         /*
703          * Check each entry in the table in turn.
704          */
705         while (i < dm_table_get_num_targets(table)) {
706                 ti = dm_table_get_target(table, i++);
707
708                 /*
709                  * If the remaining sectors fall entirely within this
710                  * table entry are they compatible with its logical_block_size?
711                  */
712                 if (remaining < ti->len &&
713                     remaining & ((ti->limits.logical_block_size >>
714                                   SECTOR_SHIFT) - 1))
715                         break;  /* Error */
716
717                 next_target_start =
718                     (unsigned short) ((next_target_start + ti->len) &
719                                       (device_logical_block_size_sects - 1));
720                 remaining = next_target_start ?
721                     device_logical_block_size_sects - next_target_start : 0;
722         }
723
724         if (remaining) {
725                 DMWARN("%s: table line %u (start sect %llu len %llu) "
726                        "not aligned to hardware logical block size %hu",
727                        dm_device_name(table->md), i,
728                        (unsigned long long) ti->begin,
729                        (unsigned long long) ti->len,
730                        table->limits.logical_block_size);
731                 return -EINVAL;
732         }
733
734         return 0;
735 }
736
737 int dm_table_add_target(struct dm_table *t, const char *type,
738                         sector_t start, sector_t len, char *params)
739 {
740         int r = -EINVAL, argc;
741         char **argv;
742         struct dm_target *tgt;
743
744         if ((r = check_space(t)))
745                 return r;
746
747         tgt = t->targets + t->num_targets;
748         memset(tgt, 0, sizeof(*tgt));
749
750         if (!len) {
751                 DMERR("%s: zero-length target", dm_device_name(t->md));
752                 return -EINVAL;
753         }
754
755         tgt->type = dm_get_target_type(type);
756         if (!tgt->type) {
757                 DMERR("%s: %s: unknown target type", dm_device_name(t->md),
758                       type);
759                 return -EINVAL;
760         }
761
762         tgt->table = t;
763         tgt->begin = start;
764         tgt->len = len;
765         tgt->error = "Unknown error";
766
767         /*
768          * Does this target adjoin the previous one ?
769          */
770         if (!adjoin(t, tgt)) {
771                 tgt->error = "Gap in table";
772                 r = -EINVAL;
773                 goto bad;
774         }
775
776         r = dm_split_args(&argc, &argv, params);
777         if (r) {
778                 tgt->error = "couldn't split parameters (insufficient memory)";
779                 goto bad;
780         }
781
782         r = tgt->type->ctr(tgt, argc, argv);
783         kfree(argv);
784         if (r)
785                 goto bad;
786
787         t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
788
789         if (blk_stack_limits(&t->limits, &tgt->limits, 0) < 0)
790                 DMWARN("%s: target device (start sect %llu len %llu) "
791                        "is misaligned",
792                        dm_device_name(t->md),
793                        (unsigned long long) tgt->begin,
794                        (unsigned long long) tgt->len);
795         return 0;
796
797  bad:
798         DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
799         dm_put_target_type(tgt->type);
800         return r;
801 }
802
803 static int setup_indexes(struct dm_table *t)
804 {
805         int i;
806         unsigned int total = 0;
807         sector_t *indexes;
808
809         /* allocate the space for *all* the indexes */
810         for (i = t->depth - 2; i >= 0; i--) {
811                 t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
812                 total += t->counts[i];
813         }
814
815         indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
816         if (!indexes)
817                 return -ENOMEM;
818
819         /* set up internal nodes, bottom-up */
820         for (i = t->depth - 2; i >= 0; i--) {
821                 t->index[i] = indexes;
822                 indexes += (KEYS_PER_NODE * t->counts[i]);
823                 setup_btree_index(i, t);
824         }
825
826         return 0;
827 }
828
829 /*
830  * Builds the btree to index the map.
831  */
832 int dm_table_complete(struct dm_table *t)
833 {
834         int r = 0;
835         unsigned int leaf_nodes;
836
837         init_valid_queue_limits(&t->limits);
838
839         r = validate_hardware_logical_block_alignment(t);
840         if (r)
841                 return r;
842
843         /* how many indexes will the btree have ? */
844         leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
845         t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
846
847         /* leaf layer has already been set up */
848         t->counts[t->depth - 1] = leaf_nodes;
849         t->index[t->depth - 1] = t->highs;
850
851         if (t->depth >= 2)
852                 r = setup_indexes(t);
853
854         return r;
855 }
856
857 static DEFINE_MUTEX(_event_lock);
858 void dm_table_event_callback(struct dm_table *t,
859                              void (*fn)(void *), void *context)
860 {
861         mutex_lock(&_event_lock);
862         t->event_fn = fn;
863         t->event_context = context;
864         mutex_unlock(&_event_lock);
865 }
866
867 void dm_table_event(struct dm_table *t)
868 {
869         /*
870          * You can no longer call dm_table_event() from interrupt
871          * context, use a bottom half instead.
872          */
873         BUG_ON(in_interrupt());
874
875         mutex_lock(&_event_lock);
876         if (t->event_fn)
877                 t->event_fn(t->event_context);
878         mutex_unlock(&_event_lock);
879 }
880
881 sector_t dm_table_get_size(struct dm_table *t)
882 {
883         return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
884 }
885
886 struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
887 {
888         if (index >= t->num_targets)
889                 return NULL;
890
891         return t->targets + index;
892 }
893
894 /*
895  * Search the btree for the correct target.
896  *
897  * Caller should check returned pointer with dm_target_is_valid()
898  * to trap I/O beyond end of device.
899  */
900 struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
901 {
902         unsigned int l, n = 0, k = 0;
903         sector_t *node;
904
905         for (l = 0; l < t->depth; l++) {
906                 n = get_child(n, k);
907                 node = get_node(t, l, n);
908
909                 for (k = 0; k < KEYS_PER_NODE; k++)
910                         if (node[k] >= sector)
911                                 break;
912         }
913
914         return &t->targets[(KEYS_PER_NODE * n) + k];
915 }
916
917 /*
918  * Set the integrity profile for this device if all devices used have
919  * matching profiles.
920  */
921 static void dm_table_set_integrity(struct dm_table *t)
922 {
923         struct list_head *devices = dm_table_get_devices(t);
924         struct dm_dev_internal *prev = NULL, *dd = NULL;
925
926         if (!blk_get_integrity(dm_disk(t->md)))
927                 return;
928
929         list_for_each_entry(dd, devices, list) {
930                 if (prev &&
931                     blk_integrity_compare(prev->dm_dev.bdev->bd_disk,
932                                           dd->dm_dev.bdev->bd_disk) < 0) {
933                         DMWARN("%s: integrity not set: %s and %s mismatch",
934                                dm_device_name(t->md),
935                                prev->dm_dev.bdev->bd_disk->disk_name,
936                                dd->dm_dev.bdev->bd_disk->disk_name);
937                         goto no_integrity;
938                 }
939                 prev = dd;
940         }
941
942         if (!prev || !bdev_get_integrity(prev->dm_dev.bdev))
943                 goto no_integrity;
944
945         blk_integrity_register(dm_disk(t->md),
946                                bdev_get_integrity(prev->dm_dev.bdev));
947
948         return;
949
950 no_integrity:
951         blk_integrity_register(dm_disk(t->md), NULL);
952
953         return;
954 }
955
956 void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
957 {
958         /*
959          * Make sure we obey the optimistic sub devices
960          * restrictions.
961          */
962         blk_queue_max_sectors(q, t->limits.max_sectors);
963         blk_queue_max_phys_segments(q, t->limits.max_phys_segments);
964         blk_queue_max_hw_segments(q, t->limits.max_hw_segments);
965         blk_queue_logical_block_size(q, t->limits.logical_block_size);
966         blk_queue_max_segment_size(q, t->limits.max_segment_size);
967         blk_queue_max_hw_sectors(q, t->limits.max_hw_sectors);
968         blk_queue_segment_boundary(q, t->limits.seg_boundary_mask);
969         blk_queue_bounce_limit(q, t->limits.bounce_pfn);
970
971         if (t->limits.no_cluster)
972                 queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
973         else
974                 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
975
976         dm_table_set_integrity(t);
977 }
978
979 unsigned int dm_table_get_num_targets(struct dm_table *t)
980 {
981         return t->num_targets;
982 }
983
984 struct list_head *dm_table_get_devices(struct dm_table *t)
985 {
986         return &t->devices;
987 }
988
989 fmode_t dm_table_get_mode(struct dm_table *t)
990 {
991         return t->mode;
992 }
993
994 static void suspend_targets(struct dm_table *t, unsigned postsuspend)
995 {
996         int i = t->num_targets;
997         struct dm_target *ti = t->targets;
998
999         while (i--) {
1000                 if (postsuspend) {
1001                         if (ti->type->postsuspend)
1002                                 ti->type->postsuspend(ti);
1003                 } else if (ti->type->presuspend)
1004                         ti->type->presuspend(ti);
1005
1006                 ti++;
1007         }
1008 }
1009
1010 void dm_table_presuspend_targets(struct dm_table *t)
1011 {
1012         if (!t)
1013                 return;
1014
1015         suspend_targets(t, 0);
1016 }
1017
1018 void dm_table_postsuspend_targets(struct dm_table *t)
1019 {
1020         if (!t)
1021                 return;
1022
1023         suspend_targets(t, 1);
1024 }
1025
1026 int dm_table_resume_targets(struct dm_table *t)
1027 {
1028         int i, r = 0;
1029
1030         for (i = 0; i < t->num_targets; i++) {
1031                 struct dm_target *ti = t->targets + i;
1032
1033                 if (!ti->type->preresume)
1034                         continue;
1035
1036                 r = ti->type->preresume(ti);
1037                 if (r)
1038                         return r;
1039         }
1040
1041         for (i = 0; i < t->num_targets; i++) {
1042                 struct dm_target *ti = t->targets + i;
1043
1044                 if (ti->type->resume)
1045                         ti->type->resume(ti);
1046         }
1047
1048         return 0;
1049 }
1050
1051 int dm_table_any_congested(struct dm_table *t, int bdi_bits)
1052 {
1053         struct dm_dev_internal *dd;
1054         struct list_head *devices = dm_table_get_devices(t);
1055         int r = 0;
1056
1057         list_for_each_entry(dd, devices, list) {
1058                 struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
1059                 char b[BDEVNAME_SIZE];
1060
1061                 if (likely(q))
1062                         r |= bdi_congested(&q->backing_dev_info, bdi_bits);
1063                 else
1064                         DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
1065                                      dm_device_name(t->md),
1066                                      bdevname(dd->dm_dev.bdev, b));
1067         }
1068
1069         return r;
1070 }
1071
1072 void dm_table_unplug_all(struct dm_table *t)
1073 {
1074         struct dm_dev_internal *dd;
1075         struct list_head *devices = dm_table_get_devices(t);
1076
1077         list_for_each_entry(dd, devices, list) {
1078                 struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
1079                 char b[BDEVNAME_SIZE];
1080
1081                 if (likely(q))
1082                         blk_unplug(q);
1083                 else
1084                         DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
1085                                      dm_device_name(t->md),
1086                                      bdevname(dd->dm_dev.bdev, b));
1087         }
1088 }
1089
1090 struct mapped_device *dm_table_get_md(struct dm_table *t)
1091 {
1092         dm_get(t->md);
1093
1094         return t->md;
1095 }
1096
1097 EXPORT_SYMBOL(dm_vcalloc);
1098 EXPORT_SYMBOL(dm_get_device);
1099 EXPORT_SYMBOL(dm_put_device);
1100 EXPORT_SYMBOL(dm_table_event);
1101 EXPORT_SYMBOL(dm_table_get_size);
1102 EXPORT_SYMBOL(dm_table_get_mode);
1103 EXPORT_SYMBOL(dm_table_get_md);
1104 EXPORT_SYMBOL(dm_table_put);
1105 EXPORT_SYMBOL(dm_table_get);
1106 EXPORT_SYMBOL(dm_table_unplug_all);