]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/md/dm-table.c
dm table: establish queue limits by copying table limits
[net-next-2.6.git] / drivers / md / dm-table.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2001 Sistina Software (UK) Limited.
d5816876 3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
1da177e4
LT
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>
48c9c27b 17#include <linux/mutex.h>
d5816876 18#include <linux/delay.h>
1da177e4
LT
19#include <asm/atomic.h>
20
72d94861
AK
21#define DM_MSG_PREFIX "table"
22
1da177e4
LT
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
d5816876
MP
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
1da177e4 41struct dm_table {
1134e5ae 42 struct mapped_device *md;
1da177e4
LT
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 */
aeb5d727 60 fmode_t mode;
1da177e4
LT
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 */
5ab97588 69 struct queue_limits limits;
1da177e4
LT
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 */
79static 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
1da177e4
LT
91/*
92 * Calculate the index of the child node of the n'th node k'th key.
93 */
94static 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 */
102static 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 */
112static 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 */
127static 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
142void *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 */
165static 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.
512875bd
JN
173 * Append an empty entry to catch sectors beyond the end of
174 * the device.
1da177e4 175 */
512875bd 176 n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
1da177e4
LT
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
aeb5d727 198int dm_table_create(struct dm_table **result, fmode_t mode,
1134e5ae 199 unsigned num_targets, struct mapped_device *md)
1da177e4 200{
094262db 201 struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
1da177e4
LT
202
203 if (!t)
204 return -ENOMEM;
205
1da177e4 206 INIT_LIST_HEAD(&t->devices);
d5816876 207 atomic_set(&t->holders, 0);
1da177e4
LT
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;
1134e5ae 221 t->md = md;
1da177e4
LT
222 *result = t;
223 return 0;
224}
225
226static void free_devices(struct list_head *devices)
227{
228 struct list_head *tmp, *next;
229
afb24528 230 list_for_each_safe(tmp, next, devices) {
82b1519b
MP
231 struct dm_dev_internal *dd =
232 list_entry(tmp, struct dm_dev_internal, list);
1b6da754
JB
233 DMWARN("dm_table_destroy: dm_put_device call missing for %s",
234 dd->dm_dev.name);
1da177e4
LT
235 kfree(dd);
236 }
237}
238
d5816876 239void dm_table_destroy(struct dm_table *t)
1da177e4
LT
240{
241 unsigned int i;
242
d5816876
MP
243 while (atomic_read(&t->holders))
244 msleep(1);
245 smp_mb();
246
1da177e4
LT
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 */
1b6da754 264 if (t->devices.next != &t->devices)
1da177e4 265 free_devices(&t->devices);
1da177e4
LT
266
267 kfree(t);
268}
269
270void dm_table_get(struct dm_table *t)
271{
272 atomic_inc(&t->holders);
273}
274
275void dm_table_put(struct dm_table *t)
276{
277 if (!t)
278 return;
279
d5816876
MP
280 smp_mb__before_atomic_dec();
281 atomic_dec(&t->holders);
1da177e4
LT
282}
283
284/*
285 * Checks to see if we need to extend highs or targets.
286 */
287static 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
1da177e4
LT
295/*
296 * See if we've already got a device in the list.
297 */
82b1519b 298static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
1da177e4 299{
82b1519b 300 struct dm_dev_internal *dd;
1da177e4
LT
301
302 list_for_each_entry (dd, l, list)
82b1519b 303 if (dd->dm_dev.bdev->bd_dev == dev)
1da177e4
LT
304 return dd;
305
306 return NULL;
307}
308
309/*
310 * Open a device so we can use it as a map destination.
311 */
82b1519b
MP
312static int open_dev(struct dm_dev_internal *d, dev_t dev,
313 struct mapped_device *md)
1da177e4
LT
314{
315 static char *_claim_ptr = "I belong to device-mapper";
316 struct block_device *bdev;
317
318 int r;
319
82b1519b 320 BUG_ON(d->dm_dev.bdev);
1da177e4 321
82b1519b 322 bdev = open_by_devnum(dev, d->dm_dev.mode);
1da177e4
LT
323 if (IS_ERR(bdev))
324 return PTR_ERR(bdev);
f165921d 325 r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
1da177e4 326 if (r)
9a1c3542 327 blkdev_put(bdev, d->dm_dev.mode);
1da177e4 328 else
82b1519b 329 d->dm_dev.bdev = bdev;
1da177e4
LT
330 return r;
331}
332
333/*
334 * Close a device that we've been using.
335 */
82b1519b 336static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
1da177e4 337{
82b1519b 338 if (!d->dm_dev.bdev)
1da177e4
LT
339 return;
340
82b1519b 341 bd_release_from_disk(d->dm_dev.bdev, dm_disk(md));
9a1c3542 342 blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
82b1519b 343 d->dm_dev.bdev = NULL;
1da177e4
LT
344}
345
346/*
2cd54d9b 347 * If possible, this checks an area of a destination device is valid.
1da177e4 348 */
02acc3a4
MS
349static int device_area_is_valid(struct dm_target *ti, struct block_device *bdev,
350 sector_t start, sector_t len)
1da177e4 351{
02acc3a4
MS
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];
2cd54d9b
MA
356
357 if (!dev_size)
358 return 1;
359
02acc3a4
MS
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;
1da177e4
LT
388}
389
390/*
570b9d96 391 * This upgrades the mode on an already open dm_dev, being
1da177e4 392 * careful to leave things as they were if we fail to reopen the
570b9d96
AK
393 * device and not to touch the existing bdev field in case
394 * it is accessed concurrently inside dm_table_any_congested().
1da177e4 395 */
aeb5d727 396static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
82b1519b 397 struct mapped_device *md)
1da177e4
LT
398{
399 int r;
570b9d96 400 struct dm_dev_internal dd_new, dd_old;
1da177e4 401
570b9d96
AK
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;
1da177e4 410
82b1519b 411 dd->dm_dev.mode |= new_mode;
570b9d96 412 close_dev(&dd_old, md);
1da177e4 413
570b9d96 414 return 0;
1da177e4
LT
415}
416
417/*
418 * Add a device to the list, or just increment the usage count if
419 * it's already present.
420 */
421static int __table_get_device(struct dm_table *t, struct dm_target *ti,
422 const char *path, sector_t start, sector_t len,
aeb5d727 423 fmode_t mode, struct dm_dev **result)
1da177e4
LT
424{
425 int r;
69a2ce72 426 dev_t uninitialized_var(dev);
82b1519b 427 struct dm_dev_internal *dd;
1da177e4
LT
428 unsigned int major, minor;
429
547bc926 430 BUG_ON(!t);
1da177e4
LT
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 */
72e8264e
CH
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);
1da177e4
LT
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
82b1519b
MP
453 dd->dm_dev.mode = mode;
454 dd->dm_dev.bdev = NULL;
1da177e4 455
f165921d 456 if ((r = open_dev(dd, dev, t->md))) {
1da177e4
LT
457 kfree(dd);
458 return r;
459 }
460
82b1519b 461 format_dev_t(dd->dm_dev.name, dev);
1da177e4
LT
462
463 atomic_set(&dd->count, 0);
464 list_add(&dd->list, &t->devices);
465
82b1519b 466 } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) {
f165921d 467 r = upgrade_mode(dd, mode, t->md);
1da177e4
LT
468 if (r)
469 return r;
470 }
471 atomic_inc(&dd->count);
472
82b1519b 473 *result = &dd->dm_dev;
1da177e4
LT
474 return 0;
475}
476
5ab97588
MS
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
3cb40214 482void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
1da177e4 483{
165125e1 484 struct request_queue *q = bdev_get_queue(bdev);
0c2322e4
AK
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 }
3cb40214 492
5ab97588
MS
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));
3cb40214 496
9980c638
MB
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.
3cb40214 501 */
9980c638
MB
502
503 if (q->merge_bvec_fn && !ti->type->merge)
5ab97588
MS
504 ti->limits.max_sectors =
505 min_not_zero(ti->limits.max_sectors,
3cb40214 506 (unsigned int) (PAGE_SIZE >> 9));
3cb40214
BR
507}
508EXPORT_SYMBOL_GPL(dm_set_device_limits);
969429b5 509
3cb40214 510int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
aeb5d727 511 sector_t len, fmode_t mode, struct dm_dev **result)
3cb40214
BR
512{
513 int r = __table_get_device(ti->table, ti, path,
514 start, len, mode, result);
515
02acc3a4
MS
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 }
1da177e4
LT
526
527 return r;
528}
529
530/*
531 * Decrement a devices use count and remove it if necessary.
532 */
82b1519b 533void dm_put_device(struct dm_target *ti, struct dm_dev *d)
1da177e4 534{
82b1519b
MP
535 struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal,
536 dm_dev);
537
1da177e4 538 if (atomic_dec_and_test(&dd->count)) {
f165921d 539 close_dev(dd, ti->table->md);
1da177e4
LT
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 */
548static 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 */
562static 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 */
581int 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;
814d6862
DT
587
588 if (!input) {
589 *argvp = NULL;
590 return 0;
591 }
592
1da177e4
LT
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
5ab97588
MS
644static 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 */
1da177e4
LT
670}
671
be6d4305
MS
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 */
679static 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
1da177e4
LT
737int 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) {
72d94861 751 DMERR("%s: zero-length target", dm_device_name(t->md));
1da177e4
LT
752 return -EINVAL;
753 }
754
755 tgt->type = dm_get_target_type(type);
756 if (!tgt->type) {
72d94861
AK
757 DMERR("%s: %s: unknown target type", dm_device_name(t->md),
758 type);
1da177e4
LT
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
5ab97588
MS
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);
1da177e4
LT
795 return 0;
796
797 bad:
72d94861 798 DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
1da177e4
LT
799 dm_put_target_type(tgt->type);
800 return r;
801}
802
803static 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 */
82d601dc 820 for (i = t->depth - 2; i >= 0; i--) {
1da177e4
LT
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 */
832int dm_table_complete(struct dm_table *t)
833{
834 int r = 0;
835 unsigned int leaf_nodes;
836
5ab97588 837 init_valid_queue_limits(&t->limits);
1da177e4 838
be6d4305
MS
839 r = validate_hardware_logical_block_alignment(t);
840 if (r)
841 return r;
842
1da177e4
LT
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
48c9c27b 857static DEFINE_MUTEX(_event_lock);
1da177e4
LT
858void dm_table_event_callback(struct dm_table *t,
859 void (*fn)(void *), void *context)
860{
48c9c27b 861 mutex_lock(&_event_lock);
1da177e4
LT
862 t->event_fn = fn;
863 t->event_context = context;
48c9c27b 864 mutex_unlock(&_event_lock);
1da177e4
LT
865}
866
867void 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
48c9c27b 875 mutex_lock(&_event_lock);
1da177e4
LT
876 if (t->event_fn)
877 t->event_fn(t->event_context);
48c9c27b 878 mutex_unlock(&_event_lock);
1da177e4
LT
879}
880
881sector_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
886struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
887{
14353539 888 if (index >= t->num_targets)
1da177e4
LT
889 return NULL;
890
891 return t->targets + index;
892}
893
894/*
895 * Search the btree for the correct target.
512875bd
JN
896 *
897 * Caller should check returned pointer with dm_target_is_valid()
898 * to trap I/O beyond end of device.
1da177e4
LT
899 */
900struct 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
9c47008d
MP
917/*
918 * Set the integrity profile for this device if all devices used have
919 * matching profiles.
920 */
921static 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
950no_integrity:
951 blk_integrity_register(dm_disk(t->md), NULL);
952
953 return;
954}
955
1da177e4
LT
956void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
957{
958 /*
1197764e 959 * Copy table's limits to the DM device's request_queue
1da177e4 960 */
1197764e 961 q->limits = t->limits;
c9a3f6d6 962
969429b5 963 if (t->limits.no_cluster)
c9a3f6d6 964 queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
969429b5 965 else
c9a3f6d6 966 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
969429b5 967
9c47008d 968 dm_table_set_integrity(t);
1da177e4
LT
969}
970
971unsigned int dm_table_get_num_targets(struct dm_table *t)
972{
973 return t->num_targets;
974}
975
976struct list_head *dm_table_get_devices(struct dm_table *t)
977{
978 return &t->devices;
979}
980
aeb5d727 981fmode_t dm_table_get_mode(struct dm_table *t)
1da177e4
LT
982{
983 return t->mode;
984}
985
986static void suspend_targets(struct dm_table *t, unsigned postsuspend)
987{
988 int i = t->num_targets;
989 struct dm_target *ti = t->targets;
990
991 while (i--) {
992 if (postsuspend) {
993 if (ti->type->postsuspend)
994 ti->type->postsuspend(ti);
995 } else if (ti->type->presuspend)
996 ti->type->presuspend(ti);
997
998 ti++;
999 }
1000}
1001
1002void dm_table_presuspend_targets(struct dm_table *t)
1003{
cf222b37
AK
1004 if (!t)
1005 return;
1006
e8488d08 1007 suspend_targets(t, 0);
1da177e4
LT
1008}
1009
1010void dm_table_postsuspend_targets(struct dm_table *t)
1011{
cf222b37
AK
1012 if (!t)
1013 return;
1014
e8488d08 1015 suspend_targets(t, 1);
1da177e4
LT
1016}
1017
8757b776 1018int dm_table_resume_targets(struct dm_table *t)
1da177e4 1019{
8757b776
MB
1020 int i, r = 0;
1021
1022 for (i = 0; i < t->num_targets; i++) {
1023 struct dm_target *ti = t->targets + i;
1024
1025 if (!ti->type->preresume)
1026 continue;
1027
1028 r = ti->type->preresume(ti);
1029 if (r)
1030 return r;
1031 }
1da177e4
LT
1032
1033 for (i = 0; i < t->num_targets; i++) {
1034 struct dm_target *ti = t->targets + i;
1035
1036 if (ti->type->resume)
1037 ti->type->resume(ti);
1038 }
8757b776
MB
1039
1040 return 0;
1da177e4
LT
1041}
1042
1043int dm_table_any_congested(struct dm_table *t, int bdi_bits)
1044{
82b1519b 1045 struct dm_dev_internal *dd;
afb24528 1046 struct list_head *devices = dm_table_get_devices(t);
1da177e4
LT
1047 int r = 0;
1048
afb24528 1049 list_for_each_entry(dd, devices, list) {
82b1519b 1050 struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
0c2322e4
AK
1051 char b[BDEVNAME_SIZE];
1052
1053 if (likely(q))
1054 r |= bdi_congested(&q->backing_dev_info, bdi_bits);
1055 else
1056 DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
1057 dm_device_name(t->md),
1058 bdevname(dd->dm_dev.bdev, b));
1da177e4
LT
1059 }
1060
1061 return r;
1062}
1063
1064void dm_table_unplug_all(struct dm_table *t)
1065{
82b1519b 1066 struct dm_dev_internal *dd;
afb24528 1067 struct list_head *devices = dm_table_get_devices(t);
1da177e4 1068
afb24528 1069 list_for_each_entry(dd, devices, list) {
82b1519b 1070 struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev);
0c2322e4
AK
1071 char b[BDEVNAME_SIZE];
1072
1073 if (likely(q))
1074 blk_unplug(q);
1075 else
1076 DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s",
1077 dm_device_name(t->md),
1078 bdevname(dd->dm_dev.bdev, b));
1da177e4
LT
1079 }
1080}
1081
1134e5ae
MA
1082struct mapped_device *dm_table_get_md(struct dm_table *t)
1083{
1084 dm_get(t->md);
1085
1086 return t->md;
1087}
1088
1da177e4
LT
1089EXPORT_SYMBOL(dm_vcalloc);
1090EXPORT_SYMBOL(dm_get_device);
1091EXPORT_SYMBOL(dm_put_device);
1092EXPORT_SYMBOL(dm_table_event);
d5e404c1 1093EXPORT_SYMBOL(dm_table_get_size);
1da177e4 1094EXPORT_SYMBOL(dm_table_get_mode);
1134e5ae 1095EXPORT_SYMBOL(dm_table_get_md);
1da177e4
LT
1096EXPORT_SYMBOL(dm_table_put);
1097EXPORT_SYMBOL(dm_table_get);
1098EXPORT_SYMBOL(dm_table_unplug_all);