blob: 827b648fac50fafaeb3f0e54b449a9bdf027925a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 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>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080017#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/atomic.h>
19
20#define MAX_DEPTH 16
21#define NODE_SIZE L1_CACHE_BYTES
22#define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
23#define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
24
25struct dm_table {
Mike Anderson1134e5a2006-03-27 01:17:54 -080026 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 atomic_t holders;
28
29 /* btree table */
30 unsigned int depth;
31 unsigned int counts[MAX_DEPTH]; /* in nodes */
32 sector_t *index[MAX_DEPTH];
33
34 unsigned int num_targets;
35 unsigned int num_allocated;
36 sector_t *highs;
37 struct dm_target *targets;
38
39 /*
40 * Indicates the rw permissions for the new logical
41 * device. This should be a combination of FMODE_READ
42 * and FMODE_WRITE.
43 */
44 int mode;
45
46 /* a list of devices used by this table */
47 struct list_head devices;
48
49 /*
50 * These are optimistic limits taken from all the
51 * targets, some targets will need smaller limits.
52 */
53 struct io_restrictions limits;
54
55 /* events get handed up using this callback */
56 void (*event_fn)(void *);
57 void *event_context;
58};
59
60/*
61 * Similar to ceiling(log_size(n))
62 */
63static unsigned int int_log(unsigned int n, unsigned int base)
64{
65 int result = 0;
66
67 while (n > 1) {
68 n = dm_div_up(n, base);
69 result++;
70 }
71
72 return result;
73}
74
75/*
76 * Returns the minimum that is _not_ zero, unless both are zero.
77 */
78#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
79
80/*
81 * Combine two io_restrictions, always taking the lower value.
82 */
83static void combine_restrictions_low(struct io_restrictions *lhs,
84 struct io_restrictions *rhs)
85{
86 lhs->max_sectors =
87 min_not_zero(lhs->max_sectors, rhs->max_sectors);
88
89 lhs->max_phys_segments =
90 min_not_zero(lhs->max_phys_segments, rhs->max_phys_segments);
91
92 lhs->max_hw_segments =
93 min_not_zero(lhs->max_hw_segments, rhs->max_hw_segments);
94
95 lhs->hardsect_size = max(lhs->hardsect_size, rhs->hardsect_size);
96
97 lhs->max_segment_size =
98 min_not_zero(lhs->max_segment_size, rhs->max_segment_size);
99
100 lhs->seg_boundary_mask =
101 min_not_zero(lhs->seg_boundary_mask, rhs->seg_boundary_mask);
NeilBrown969429b2006-03-27 01:17:49 -0800102
103 lhs->no_cluster |= rhs->no_cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106/*
107 * Calculate the index of the child node of the n'th node k'th key.
108 */
109static inline unsigned int get_child(unsigned int n, unsigned int k)
110{
111 return (n * CHILDREN_PER_NODE) + k;
112}
113
114/*
115 * Return the n'th node of level l from table t.
116 */
117static inline sector_t *get_node(struct dm_table *t,
118 unsigned int l, unsigned int n)
119{
120 return t->index[l] + (n * KEYS_PER_NODE);
121}
122
123/*
124 * Return the highest key that you could lookup from the n'th
125 * node on level l of the btree.
126 */
127static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
128{
129 for (; l < t->depth - 1; l++)
130 n = get_child(n, CHILDREN_PER_NODE - 1);
131
132 if (n >= t->counts[l])
133 return (sector_t) - 1;
134
135 return get_node(t, l, n)[KEYS_PER_NODE - 1];
136}
137
138/*
139 * Fills in a level of the btree based on the highs of the level
140 * below it.
141 */
142static int setup_btree_index(unsigned int l, struct dm_table *t)
143{
144 unsigned int n, k;
145 sector_t *node;
146
147 for (n = 0U; n < t->counts[l]; n++) {
148 node = get_node(t, l, n);
149
150 for (k = 0U; k < KEYS_PER_NODE; k++)
151 node[k] = high(t, l + 1, get_child(n, k));
152 }
153
154 return 0;
155}
156
157void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
158{
159 unsigned long size;
160 void *addr;
161
162 /*
163 * Check that we're not going to overflow.
164 */
165 if (nmemb > (ULONG_MAX / elem_size))
166 return NULL;
167
168 size = nmemb * elem_size;
169 addr = vmalloc(size);
170 if (addr)
171 memset(addr, 0, size);
172
173 return addr;
174}
175
176/*
177 * highs, and targets are managed as dynamic arrays during a
178 * table load.
179 */
180static int alloc_targets(struct dm_table *t, unsigned int num)
181{
182 sector_t *n_highs;
183 struct dm_target *n_targets;
184 int n = t->num_targets;
185
186 /*
187 * Allocate both the target array and offset array at once.
188 */
189 n_highs = (sector_t *) dm_vcalloc(num, sizeof(struct dm_target) +
190 sizeof(sector_t));
191 if (!n_highs)
192 return -ENOMEM;
193
194 n_targets = (struct dm_target *) (n_highs + num);
195
196 if (n) {
197 memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
198 memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
199 }
200
201 memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
202 vfree(t->highs);
203
204 t->num_allocated = num;
205 t->highs = n_highs;
206 t->targets = n_targets;
207
208 return 0;
209}
210
Mike Anderson1134e5a2006-03-27 01:17:54 -0800211int dm_table_create(struct dm_table **result, int mode,
212 unsigned num_targets, struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct dm_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
215
216 if (!t)
217 return -ENOMEM;
218
219 memset(t, 0, sizeof(*t));
220 INIT_LIST_HEAD(&t->devices);
221 atomic_set(&t->holders, 1);
222
223 if (!num_targets)
224 num_targets = KEYS_PER_NODE;
225
226 num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
227
228 if (alloc_targets(t, num_targets)) {
229 kfree(t);
230 t = NULL;
231 return -ENOMEM;
232 }
233
234 t->mode = mode;
Mike Anderson1134e5a2006-03-27 01:17:54 -0800235 t->md = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 *result = t;
237 return 0;
238}
239
David Teiglandc2ade422006-06-26 00:27:33 -0700240int dm_create_error_table(struct dm_table **result, struct mapped_device *md)
241{
242 struct dm_table *t;
243 sector_t dev_size = 1;
244 int r;
245
246 /*
247 * Find current size of device.
248 * Default to 1 sector if inactive.
249 */
250 t = dm_get_table(md);
251 if (t) {
252 dev_size = dm_table_get_size(t);
253 dm_table_put(t);
254 }
255
256 r = dm_table_create(&t, FMODE_READ, 1, md);
257 if (r)
258 return r;
259
260 r = dm_table_add_target(t, "error", 0, dev_size, NULL);
261 if (r)
262 goto out;
263
264 r = dm_table_complete(t);
265 if (r)
266 goto out;
267
268 *result = t;
269
270out:
271 if (r)
272 dm_table_put(t);
273
274 return r;
275}
276EXPORT_SYMBOL_GPL(dm_create_error_table);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static void free_devices(struct list_head *devices)
279{
280 struct list_head *tmp, *next;
281
282 for (tmp = devices->next; tmp != devices; tmp = next) {
283 struct dm_dev *dd = list_entry(tmp, struct dm_dev, list);
284 next = tmp->next;
285 kfree(dd);
286 }
287}
288
Alasdair G Kergon5e198d92005-05-05 16:16:09 -0700289static void table_destroy(struct dm_table *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 unsigned int i;
292
293 /* free the indexes (see dm_table_complete) */
294 if (t->depth >= 2)
295 vfree(t->index[t->depth - 2]);
296
297 /* free the targets */
298 for (i = 0; i < t->num_targets; i++) {
299 struct dm_target *tgt = t->targets + i;
300
301 if (tgt->type->dtr)
302 tgt->type->dtr(tgt);
303
304 dm_put_target_type(tgt->type);
305 }
306
307 vfree(t->highs);
308
309 /* free the device list */
310 if (t->devices.next != &t->devices) {
311 DMWARN("devices still present during destroy: "
312 "dm_table_remove_device calls missing");
313
314 free_devices(&t->devices);
315 }
316
317 kfree(t);
318}
319
320void dm_table_get(struct dm_table *t)
321{
322 atomic_inc(&t->holders);
323}
324
325void dm_table_put(struct dm_table *t)
326{
327 if (!t)
328 return;
329
330 if (atomic_dec_and_test(&t->holders))
331 table_destroy(t);
332}
333
334/*
335 * Checks to see if we need to extend highs or targets.
336 */
337static inline int check_space(struct dm_table *t)
338{
339 if (t->num_targets >= t->num_allocated)
340 return alloc_targets(t, t->num_allocated * 2);
341
342 return 0;
343}
344
345/*
346 * Convert a device path to a dev_t.
347 */
348static int lookup_device(const char *path, dev_t *dev)
349{
350 int r;
351 struct nameidata nd;
352 struct inode *inode;
353
354 if ((r = path_lookup(path, LOOKUP_FOLLOW, &nd)))
355 return r;
356
357 inode = nd.dentry->d_inode;
358 if (!inode) {
359 r = -ENOENT;
360 goto out;
361 }
362
363 if (!S_ISBLK(inode->i_mode)) {
364 r = -ENOTBLK;
365 goto out;
366 }
367
368 *dev = inode->i_rdev;
369
370 out:
371 path_release(&nd);
372 return r;
373}
374
375/*
376 * See if we've already got a device in the list.
377 */
378static struct dm_dev *find_device(struct list_head *l, dev_t dev)
379{
380 struct dm_dev *dd;
381
382 list_for_each_entry (dd, l, list)
383 if (dd->bdev->bd_dev == dev)
384 return dd;
385
386 return NULL;
387}
388
389/*
390 * Open a device so we can use it as a map destination.
391 */
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800392static int open_dev(struct dm_dev *d, dev_t dev, struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 static char *_claim_ptr = "I belong to device-mapper";
395 struct block_device *bdev;
396
397 int r;
398
Eric Sesterhenn547bc922006-03-26 18:22:50 +0200399 BUG_ON(d->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 bdev = open_by_devnum(dev, d->mode);
402 if (IS_ERR(bdev))
403 return PTR_ERR(bdev);
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800404 r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (r)
406 blkdev_put(bdev);
407 else
408 d->bdev = bdev;
409 return r;
410}
411
412/*
413 * Close a device that we've been using.
414 */
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800415static void close_dev(struct dm_dev *d, struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 if (!d->bdev)
418 return;
419
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800420 bd_release_from_disk(d->bdev, dm_disk(md));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 blkdev_put(d->bdev);
422 d->bdev = NULL;
423}
424
425/*
426 * If possible (ie. blk_size[major] is set), this checks an area
427 * of a destination device is valid.
428 */
429static int check_device_area(struct dm_dev *dd, sector_t start, sector_t len)
430{
431 sector_t dev_size;
432 dev_size = dd->bdev->bd_inode->i_size >> SECTOR_SHIFT;
433 return ((start < dev_size) && (len <= (dev_size - start)));
434}
435
436/*
437 * This upgrades the mode on an already open dm_dev. Being
438 * careful to leave things as they were if we fail to reopen the
439 * device.
440 */
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800441static int upgrade_mode(struct dm_dev *dd, int new_mode, struct mapped_device *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 int r;
444 struct dm_dev dd_copy;
445 dev_t dev = dd->bdev->bd_dev;
446
447 dd_copy = *dd;
448
449 dd->mode |= new_mode;
450 dd->bdev = NULL;
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800451 r = open_dev(dd, dev, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!r)
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800453 close_dev(&dd_copy, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 else
455 *dd = dd_copy;
456
457 return r;
458}
459
460/*
461 * Add a device to the list, or just increment the usage count if
462 * it's already present.
463 */
464static int __table_get_device(struct dm_table *t, struct dm_target *ti,
465 const char *path, sector_t start, sector_t len,
466 int mode, struct dm_dev **result)
467{
468 int r;
469 dev_t dev;
470 struct dm_dev *dd;
471 unsigned int major, minor;
472
Eric Sesterhenn547bc922006-03-26 18:22:50 +0200473 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (sscanf(path, "%u:%u", &major, &minor) == 2) {
476 /* Extract the major/minor numbers */
477 dev = MKDEV(major, minor);
478 if (MAJOR(dev) != major || MINOR(dev) != minor)
479 return -EOVERFLOW;
480 } else {
481 /* convert the path to a device */
482 if ((r = lookup_device(path, &dev)))
483 return r;
484 }
485
486 dd = find_device(&t->devices, dev);
487 if (!dd) {
488 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
489 if (!dd)
490 return -ENOMEM;
491
492 dd->mode = mode;
493 dd->bdev = NULL;
494
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800495 if ((r = open_dev(dd, dev, t->md))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 kfree(dd);
497 return r;
498 }
499
500 format_dev_t(dd->name, dev);
501
502 atomic_set(&dd->count, 0);
503 list_add(&dd->list, &t->devices);
504
505 } else if (dd->mode != (mode | dd->mode)) {
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800506 r = upgrade_mode(dd, mode, t->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (r)
508 return r;
509 }
510 atomic_inc(&dd->count);
511
512 if (!check_device_area(dd, start, len)) {
513 DMWARN("device %s too small for target", path);
514 dm_put_device(ti, dd);
515 return -EINVAL;
516 }
517
518 *result = dd;
519
520 return 0;
521}
522
523
524int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
525 sector_t len, int mode, struct dm_dev **result)
526{
527 int r = __table_get_device(ti->table, ti, path,
528 start, len, mode, result);
529 if (!r) {
530 request_queue_t *q = bdev_get_queue((*result)->bdev);
531 struct io_restrictions *rs = &ti->limits;
532
533 /*
534 * Combine the device limits low.
535 *
536 * FIXME: if we move an io_restriction struct
537 * into q this would just be a call to
538 * combine_restrictions_low()
539 */
540 rs->max_sectors =
541 min_not_zero(rs->max_sectors, q->max_sectors);
542
543 /* FIXME: Device-Mapper on top of RAID-0 breaks because DM
544 * currently doesn't honor MD's merge_bvec_fn routine.
545 * In this case, we'll force DM to use PAGE_SIZE or
546 * smaller I/O, just to be safe. A better fix is in the
547 * works, but add this for the time being so it will at
548 * least operate correctly.
549 */
550 if (q->merge_bvec_fn)
551 rs->max_sectors =
552 min_not_zero(rs->max_sectors,
Alasdair G Kergon3ee247e2006-02-01 03:04:55 -0800553 (unsigned int) (PAGE_SIZE >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 rs->max_phys_segments =
556 min_not_zero(rs->max_phys_segments,
557 q->max_phys_segments);
558
559 rs->max_hw_segments =
560 min_not_zero(rs->max_hw_segments, q->max_hw_segments);
561
562 rs->hardsect_size = max(rs->hardsect_size, q->hardsect_size);
563
564 rs->max_segment_size =
565 min_not_zero(rs->max_segment_size, q->max_segment_size);
566
567 rs->seg_boundary_mask =
568 min_not_zero(rs->seg_boundary_mask,
569 q->seg_boundary_mask);
NeilBrown969429b2006-03-27 01:17:49 -0800570
571 rs->no_cluster |= !test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 return r;
575}
576
577/*
578 * Decrement a devices use count and remove it if necessary.
579 */
580void dm_put_device(struct dm_target *ti, struct dm_dev *dd)
581{
582 if (atomic_dec_and_test(&dd->count)) {
Jun'ichi Nomuraf1659212006-03-27 01:17:59 -0800583 close_dev(dd, ti->table->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 list_del(&dd->list);
585 kfree(dd);
586 }
587}
588
589/*
590 * Checks to see if the target joins onto the end of the table.
591 */
592static int adjoin(struct dm_table *table, struct dm_target *ti)
593{
594 struct dm_target *prev;
595
596 if (!table->num_targets)
597 return !ti->begin;
598
599 prev = &table->targets[table->num_targets - 1];
600 return (ti->begin == (prev->begin + prev->len));
601}
602
603/*
604 * Used to dynamically allocate the arg array.
605 */
606static char **realloc_argv(unsigned *array_size, char **old_argv)
607{
608 char **argv;
609 unsigned new_size;
610
611 new_size = *array_size ? *array_size * 2 : 64;
612 argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
613 if (argv) {
614 memcpy(argv, old_argv, *array_size * sizeof(*argv));
615 *array_size = new_size;
616 }
617
618 kfree(old_argv);
619 return argv;
620}
621
622/*
623 * Destructively splits up the argument list to pass to ctr.
624 */
625int dm_split_args(int *argc, char ***argvp, char *input)
626{
627 char *start, *end = input, *out, **argv = NULL;
628 unsigned array_size = 0;
629
630 *argc = 0;
David Teigland814d6862006-06-26 00:27:31 -0700631
632 if (!input) {
633 *argvp = NULL;
634 return 0;
635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 argv = realloc_argv(&array_size, argv);
638 if (!argv)
639 return -ENOMEM;
640
641 while (1) {
642 start = end;
643
644 /* Skip whitespace */
645 while (*start && isspace(*start))
646 start++;
647
648 if (!*start)
649 break; /* success, we hit the end */
650
651 /* 'out' is used to remove any back-quotes */
652 end = out = start;
653 while (*end) {
654 /* Everything apart from '\0' can be quoted */
655 if (*end == '\\' && *(end + 1)) {
656 *out++ = *(end + 1);
657 end += 2;
658 continue;
659 }
660
661 if (isspace(*end))
662 break; /* end of token */
663
664 *out++ = *end++;
665 }
666
667 /* have we already filled the array ? */
668 if ((*argc + 1) > array_size) {
669 argv = realloc_argv(&array_size, argv);
670 if (!argv)
671 return -ENOMEM;
672 }
673
674 /* we know this is whitespace */
675 if (*end)
676 end++;
677
678 /* terminate the string and put it in the array */
679 *out = '\0';
680 argv[*argc] = start;
681 (*argc)++;
682 }
683
684 *argvp = argv;
685 return 0;
686}
687
688static void check_for_valid_limits(struct io_restrictions *rs)
689{
690 if (!rs->max_sectors)
Mike Christiedefd94b2005-12-05 02:37:06 -0600691 rs->max_sectors = SAFE_MAX_SECTORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (!rs->max_phys_segments)
693 rs->max_phys_segments = MAX_PHYS_SEGMENTS;
694 if (!rs->max_hw_segments)
695 rs->max_hw_segments = MAX_HW_SEGMENTS;
696 if (!rs->hardsect_size)
697 rs->hardsect_size = 1 << SECTOR_SHIFT;
698 if (!rs->max_segment_size)
699 rs->max_segment_size = MAX_SEGMENT_SIZE;
700 if (!rs->seg_boundary_mask)
701 rs->seg_boundary_mask = -1;
702}
703
704int dm_table_add_target(struct dm_table *t, const char *type,
705 sector_t start, sector_t len, char *params)
706{
707 int r = -EINVAL, argc;
708 char **argv;
709 struct dm_target *tgt;
710
711 if ((r = check_space(t)))
712 return r;
713
714 tgt = t->targets + t->num_targets;
715 memset(tgt, 0, sizeof(*tgt));
716
717 if (!len) {
718 tgt->error = "zero-length target";
719 DMERR("%s", tgt->error);
720 return -EINVAL;
721 }
722
723 tgt->type = dm_get_target_type(type);
724 if (!tgt->type) {
725 tgt->error = "unknown target type";
726 DMERR("%s", tgt->error);
727 return -EINVAL;
728 }
729
730 tgt->table = t;
731 tgt->begin = start;
732 tgt->len = len;
733 tgt->error = "Unknown error";
734
735 /*
736 * Does this target adjoin the previous one ?
737 */
738 if (!adjoin(t, tgt)) {
739 tgt->error = "Gap in table";
740 r = -EINVAL;
741 goto bad;
742 }
743
744 r = dm_split_args(&argc, &argv, params);
745 if (r) {
746 tgt->error = "couldn't split parameters (insufficient memory)";
747 goto bad;
748 }
749
750 r = tgt->type->ctr(tgt, argc, argv);
751 kfree(argv);
752 if (r)
753 goto bad;
754
755 t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
756
757 /* FIXME: the plan is to combine high here and then have
758 * the merge fn apply the target level restrictions. */
759 combine_restrictions_low(&t->limits, &tgt->limits);
760 return 0;
761
762 bad:
763 DMERR("%s", tgt->error);
764 dm_put_target_type(tgt->type);
765 return r;
766}
767
768static int setup_indexes(struct dm_table *t)
769{
770 int i;
771 unsigned int total = 0;
772 sector_t *indexes;
773
774 /* allocate the space for *all* the indexes */
775 for (i = t->depth - 2; i >= 0; i--) {
776 t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
777 total += t->counts[i];
778 }
779
780 indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
781 if (!indexes)
782 return -ENOMEM;
783
784 /* set up internal nodes, bottom-up */
785 for (i = t->depth - 2, total = 0; i >= 0; i--) {
786 t->index[i] = indexes;
787 indexes += (KEYS_PER_NODE * t->counts[i]);
788 setup_btree_index(i, t);
789 }
790
791 return 0;
792}
793
794/*
795 * Builds the btree to index the map.
796 */
797int dm_table_complete(struct dm_table *t)
798{
799 int r = 0;
800 unsigned int leaf_nodes;
801
802 check_for_valid_limits(&t->limits);
803
804 /* how many indexes will the btree have ? */
805 leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
806 t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
807
808 /* leaf layer has already been set up */
809 t->counts[t->depth - 1] = leaf_nodes;
810 t->index[t->depth - 1] = t->highs;
811
812 if (t->depth >= 2)
813 r = setup_indexes(t);
814
815 return r;
816}
817
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800818static DEFINE_MUTEX(_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819void dm_table_event_callback(struct dm_table *t,
820 void (*fn)(void *), void *context)
821{
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800822 mutex_lock(&_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 t->event_fn = fn;
824 t->event_context = context;
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800825 mutex_unlock(&_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828void dm_table_event(struct dm_table *t)
829{
830 /*
831 * You can no longer call dm_table_event() from interrupt
832 * context, use a bottom half instead.
833 */
834 BUG_ON(in_interrupt());
835
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800836 mutex_lock(&_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (t->event_fn)
838 t->event_fn(t->event_context);
Arjan van de Ven48c9c272006-03-27 01:18:20 -0800839 mutex_unlock(&_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
842sector_t dm_table_get_size(struct dm_table *t)
843{
844 return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
845}
846
847struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
848{
Milan Broz14353532006-06-26 00:27:27 -0700849 if (index >= t->num_targets)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return NULL;
851
852 return t->targets + index;
853}
854
855/*
856 * Search the btree for the correct target.
857 */
858struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
859{
860 unsigned int l, n = 0, k = 0;
861 sector_t *node;
862
863 for (l = 0; l < t->depth; l++) {
864 n = get_child(n, k);
865 node = get_node(t, l, n);
866
867 for (k = 0; k < KEYS_PER_NODE; k++)
868 if (node[k] >= sector)
869 break;
870 }
871
872 return &t->targets[(KEYS_PER_NODE * n) + k];
873}
874
875void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
876{
877 /*
878 * Make sure we obey the optimistic sub devices
879 * restrictions.
880 */
881 blk_queue_max_sectors(q, t->limits.max_sectors);
882 q->max_phys_segments = t->limits.max_phys_segments;
883 q->max_hw_segments = t->limits.max_hw_segments;
884 q->hardsect_size = t->limits.hardsect_size;
885 q->max_segment_size = t->limits.max_segment_size;
886 q->seg_boundary_mask = t->limits.seg_boundary_mask;
NeilBrown969429b2006-03-27 01:17:49 -0800887 if (t->limits.no_cluster)
888 q->queue_flags &= ~(1 << QUEUE_FLAG_CLUSTER);
889 else
890 q->queue_flags |= (1 << QUEUE_FLAG_CLUSTER);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894unsigned int dm_table_get_num_targets(struct dm_table *t)
895{
896 return t->num_targets;
897}
898
899struct list_head *dm_table_get_devices(struct dm_table *t)
900{
901 return &t->devices;
902}
903
904int dm_table_get_mode(struct dm_table *t)
905{
906 return t->mode;
907}
908
909static void suspend_targets(struct dm_table *t, unsigned postsuspend)
910{
911 int i = t->num_targets;
912 struct dm_target *ti = t->targets;
913
914 while (i--) {
915 if (postsuspend) {
916 if (ti->type->postsuspend)
917 ti->type->postsuspend(ti);
918 } else if (ti->type->presuspend)
919 ti->type->presuspend(ti);
920
921 ti++;
922 }
923}
924
925void dm_table_presuspend_targets(struct dm_table *t)
926{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700927 if (!t)
928 return;
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return suspend_targets(t, 0);
931}
932
933void dm_table_postsuspend_targets(struct dm_table *t)
934{
Alasdair G Kergoncf222b32005-07-28 21:15:57 -0700935 if (!t)
936 return;
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return suspend_targets(t, 1);
939}
940
941void dm_table_resume_targets(struct dm_table *t)
942{
943 int i;
944
945 for (i = 0; i < t->num_targets; i++) {
946 struct dm_target *ti = t->targets + i;
947
948 if (ti->type->resume)
949 ti->type->resume(ti);
950 }
951}
952
953int dm_table_any_congested(struct dm_table *t, int bdi_bits)
954{
955 struct list_head *d, *devices;
956 int r = 0;
957
958 devices = dm_table_get_devices(t);
959 for (d = devices->next; d != devices; d = d->next) {
960 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
961 request_queue_t *q = bdev_get_queue(dd->bdev);
962 r |= bdi_congested(&q->backing_dev_info, bdi_bits);
963 }
964
965 return r;
966}
967
968void dm_table_unplug_all(struct dm_table *t)
969{
970 struct list_head *d, *devices = dm_table_get_devices(t);
971
972 for (d = devices->next; d != devices; d = d->next) {
973 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
974 request_queue_t *q = bdev_get_queue(dd->bdev);
975
976 if (q->unplug_fn)
977 q->unplug_fn(q);
978 }
979}
980
981int dm_table_flush_all(struct dm_table *t)
982{
983 struct list_head *d, *devices = dm_table_get_devices(t);
984 int ret = 0;
985
986 for (d = devices->next; d != devices; d = d->next) {
987 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
988 request_queue_t *q = bdev_get_queue(dd->bdev);
989 int err;
990
991 if (!q->issue_flush_fn)
992 err = -EOPNOTSUPP;
993 else
994 err = q->issue_flush_fn(q, dd->bdev->bd_disk, NULL);
995
996 if (!ret)
997 ret = err;
998 }
999
1000 return ret;
1001}
1002
Mike Anderson1134e5a2006-03-27 01:17:54 -08001003struct mapped_device *dm_table_get_md(struct dm_table *t)
1004{
1005 dm_get(t->md);
1006
1007 return t->md;
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010EXPORT_SYMBOL(dm_vcalloc);
1011EXPORT_SYMBOL(dm_get_device);
1012EXPORT_SYMBOL(dm_put_device);
1013EXPORT_SYMBOL(dm_table_event);
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001014EXPORT_SYMBOL(dm_table_get_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015EXPORT_SYMBOL(dm_table_get_mode);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001016EXPORT_SYMBOL(dm_table_get_md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017EXPORT_SYMBOL(dm_table_put);
1018EXPORT_SYMBOL(dm_table_get);
1019EXPORT_SYMBOL(dm_table_unplug_all);
1020EXPORT_SYMBOL(dm_table_flush_all);