MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / md / dm-table.c
bloba6c24847d3f4faf5e103fed3943b4cd5caf2e261
1 /*
2 * Copyright (C) 2001 Sistina Software (UK) Limited.
4 * This file is released under the GPL.
5 */
7 #include "dm.h"
9 #include <linux/module.h>
10 #include <linux/vmalloc.h>
11 #include <linux/blkdev.h>
12 #include <linux/namei.h>
13 #include <linux/ctype.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <asm/atomic.h>
18 #define MAX_DEPTH 16
19 #define NODE_SIZE L1_CACHE_BYTES
20 #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
21 #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
23 struct dm_table {
24 atomic_t holders;
26 /* btree table */
27 unsigned int depth;
28 unsigned int counts[MAX_DEPTH]; /* in nodes */
29 sector_t *index[MAX_DEPTH];
31 unsigned int num_targets;
32 unsigned int num_allocated;
33 sector_t *highs;
34 struct dm_target *targets;
37 * Indicates the rw permissions for the new logical
38 * device. This should be a combination of FMODE_READ
39 * and FMODE_WRITE.
41 int mode;
43 /* a list of devices used by this table */
44 struct list_head devices;
47 * These are optimistic limits taken from all the
48 * targets, some targets will need smaller limits.
50 struct io_restrictions limits;
52 /* events get handed up using this callback */
53 void (*event_fn)(void *);
54 void *event_context;
58 * Similar to ceiling(log_size(n))
60 static unsigned int int_log(unsigned long n, unsigned long base)
62 int result = 0;
64 while (n > 1) {
65 n = dm_div_up(n, base);
66 result++;
69 return result;
73 * Returns the minimum that is _not_ zero, unless both are zero.
75 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
78 * Combine two io_restrictions, always taking the lower value.
80 static void combine_restrictions_low(struct io_restrictions *lhs,
81 struct io_restrictions *rhs)
83 lhs->max_sectors =
84 min_not_zero(lhs->max_sectors, rhs->max_sectors);
86 lhs->max_phys_segments =
87 min_not_zero(lhs->max_phys_segments, rhs->max_phys_segments);
89 lhs->max_hw_segments =
90 min_not_zero(lhs->max_hw_segments, rhs->max_hw_segments);
92 lhs->hardsect_size = max(lhs->hardsect_size, rhs->hardsect_size);
94 lhs->max_segment_size =
95 min_not_zero(lhs->max_segment_size, rhs->max_segment_size);
97 lhs->seg_boundary_mask =
98 min_not_zero(lhs->seg_boundary_mask, rhs->seg_boundary_mask);
102 * Calculate the index of the child node of the n'th node k'th key.
104 static inline unsigned int get_child(unsigned int n, unsigned int k)
106 return (n * CHILDREN_PER_NODE) + k;
110 * Return the n'th node of level l from table t.
112 static inline sector_t *get_node(struct dm_table *t,
113 unsigned int l, unsigned int n)
115 return t->index[l] + (n * KEYS_PER_NODE);
119 * Return the highest key that you could lookup from the n'th
120 * node on level l of the btree.
122 static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
124 for (; l < t->depth - 1; l++)
125 n = get_child(n, CHILDREN_PER_NODE - 1);
127 if (n >= t->counts[l])
128 return (sector_t) - 1;
130 return get_node(t, l, n)[KEYS_PER_NODE - 1];
134 * Fills in a level of the btree based on the highs of the level
135 * below it.
137 static int setup_btree_index(unsigned int l, struct dm_table *t)
139 unsigned int n, k;
140 sector_t *node;
142 for (n = 0U; n < t->counts[l]; n++) {
143 node = get_node(t, l, n);
145 for (k = 0U; k < KEYS_PER_NODE; k++)
146 node[k] = high(t, l + 1, get_child(n, k));
149 return 0;
152 void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
154 unsigned long size;
155 void *addr;
158 * Check that we're not going to overflow.
160 if (nmemb > (ULONG_MAX / elem_size))
161 return NULL;
163 size = nmemb * elem_size;
164 addr = vmalloc(size);
165 if (addr)
166 memset(addr, 0, size);
168 return addr;
172 * highs, and targets are managed as dynamic arrays during a
173 * table load.
175 static int alloc_targets(struct dm_table *t, unsigned int num)
177 sector_t *n_highs;
178 struct dm_target *n_targets;
179 int n = t->num_targets;
182 * Allocate both the target array and offset array at once.
184 n_highs = (sector_t *) dm_vcalloc(num, sizeof(struct dm_target) +
185 sizeof(sector_t));
186 if (!n_highs)
187 return -ENOMEM;
189 n_targets = (struct dm_target *) (n_highs + num);
191 if (n) {
192 memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
193 memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
196 memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
197 vfree(t->highs);
199 t->num_allocated = num;
200 t->highs = n_highs;
201 t->targets = n_targets;
203 return 0;
206 int dm_table_create(struct dm_table **result, int mode, unsigned num_targets)
208 struct dm_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
210 if (!t)
211 return -ENOMEM;
213 memset(t, 0, sizeof(*t));
214 INIT_LIST_HEAD(&t->devices);
215 atomic_set(&t->holders, 1);
217 if (!num_targets)
218 num_targets = KEYS_PER_NODE;
220 num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
222 if (alloc_targets(t, num_targets)) {
223 kfree(t);
224 t = NULL;
225 return -ENOMEM;
228 t->mode = mode;
229 *result = t;
230 return 0;
233 static void free_devices(struct list_head *devices)
235 struct list_head *tmp, *next;
237 for (tmp = devices->next; tmp != devices; tmp = next) {
238 struct dm_dev *dd = list_entry(tmp, struct dm_dev, list);
239 next = tmp->next;
240 kfree(dd);
244 void table_destroy(struct dm_table *t)
246 unsigned int i;
248 /* free the indexes (see dm_table_complete) */
249 if (t->depth >= 2)
250 vfree(t->index[t->depth - 2]);
252 /* free the targets */
253 for (i = 0; i < t->num_targets; i++) {
254 struct dm_target *tgt = t->targets + i;
256 if (tgt->type->dtr)
257 tgt->type->dtr(tgt);
259 dm_put_target_type(tgt->type);
262 vfree(t->highs);
264 /* free the device list */
265 if (t->devices.next != &t->devices) {
266 DMWARN("devices still present during destroy: "
267 "dm_table_remove_device calls missing");
269 free_devices(&t->devices);
272 kfree(t);
275 void dm_table_get(struct dm_table *t)
277 atomic_inc(&t->holders);
280 void dm_table_put(struct dm_table *t)
282 if (!t)
283 return;
285 if (atomic_dec_and_test(&t->holders))
286 table_destroy(t);
290 * Checks to see if we need to extend highs or targets.
292 static inline int check_space(struct dm_table *t)
294 if (t->num_targets >= t->num_allocated)
295 return alloc_targets(t, t->num_allocated * 2);
297 return 0;
301 * Convert a device path to a dev_t.
303 static int lookup_device(const char *path, dev_t *dev)
305 int r;
306 struct nameidata nd;
307 struct inode *inode;
309 if ((r = path_lookup(path, LOOKUP_FOLLOW, &nd)))
310 return r;
312 inode = nd.dentry->d_inode;
313 if (!inode) {
314 r = -ENOENT;
315 goto out;
318 if (!S_ISBLK(inode->i_mode)) {
319 r = -ENOTBLK;
320 goto out;
323 *dev = inode->i_rdev;
325 out:
326 path_release(&nd);
327 return r;
331 * See if we've already got a device in the list.
333 static struct dm_dev *find_device(struct list_head *l, dev_t dev)
335 struct dm_dev *dd;
337 list_for_each_entry (dd, l, list)
338 if (dd->bdev->bd_dev == dev)
339 return dd;
341 return NULL;
345 * Open a device so we can use it as a map destination.
347 static int open_dev(struct dm_dev *d, dev_t dev)
349 static char *_claim_ptr = "I belong to device-mapper";
350 struct block_device *bdev;
352 int r;
354 if (d->bdev)
355 BUG();
357 bdev = open_by_devnum(dev, d->mode);
358 if (IS_ERR(bdev))
359 return PTR_ERR(bdev);
360 r = bd_claim(bdev, _claim_ptr);
361 if (r)
362 blkdev_put(bdev);
363 else
364 d->bdev = bdev;
365 return r;
369 * Close a device that we've been using.
371 static void close_dev(struct dm_dev *d)
373 if (!d->bdev)
374 return;
376 bd_release(d->bdev);
377 blkdev_put(d->bdev);
378 d->bdev = NULL;
382 * If possible (ie. blk_size[major] is set), this checks an area
383 * of a destination device is valid.
385 static int check_device_area(struct dm_dev *dd, sector_t start, sector_t len)
387 sector_t dev_size;
388 dev_size = dd->bdev->bd_inode->i_size >> SECTOR_SHIFT;
389 return ((start < dev_size) && (len <= (dev_size - start)));
393 * This upgrades the mode on an already open dm_dev. Being
394 * careful to leave things as they were if we fail to reopen the
395 * device.
397 static int upgrade_mode(struct dm_dev *dd, int new_mode)
399 int r;
400 struct dm_dev dd_copy;
401 dev_t dev = dd->bdev->bd_dev;
403 dd_copy = *dd;
405 dd->mode |= new_mode;
406 dd->bdev = NULL;
407 r = open_dev(dd, dev);
408 if (!r)
409 close_dev(&dd_copy);
410 else
411 *dd = dd_copy;
413 return r;
417 * Add a device to the list, or just increment the usage count if
418 * it's already present.
420 static int __table_get_device(struct dm_table *t, struct dm_target *ti,
421 const char *path, sector_t start, sector_t len,
422 int mode, struct dm_dev **result)
424 int r;
425 dev_t dev;
426 struct dm_dev *dd;
427 unsigned int major, minor;
429 if (!t)
430 BUG();
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 if ((r = lookup_device(path, &dev)))
440 return r;
443 dd = find_device(&t->devices, dev);
444 if (!dd) {
445 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
446 if (!dd)
447 return -ENOMEM;
449 dd->mode = mode;
450 dd->bdev = NULL;
452 if ((r = open_dev(dd, dev))) {
453 kfree(dd);
454 return r;
457 atomic_set(&dd->count, 0);
458 list_add(&dd->list, &t->devices);
460 } else if (dd->mode != (mode | dd->mode)) {
461 r = upgrade_mode(dd, mode);
462 if (r)
463 return r;
465 atomic_inc(&dd->count);
467 if (!check_device_area(dd, start, len)) {
468 DMWARN("device %s too small for target", path);
469 dm_put_device(ti, dd);
470 return -EINVAL;
473 *result = dd;
475 return 0;
479 int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
480 sector_t len, int mode, struct dm_dev **result)
482 int r = __table_get_device(ti->table, ti, path,
483 start, len, mode, result);
484 if (!r) {
485 request_queue_t *q = bdev_get_queue((*result)->bdev);
486 struct io_restrictions *rs = &ti->limits;
489 * Combine the device limits low.
491 * FIXME: if we move an io_restriction struct
492 * into q this would just be a call to
493 * combine_restrictions_low()
495 rs->max_sectors =
496 min_not_zero(rs->max_sectors, q->max_sectors);
498 /* FIXME: Device-Mapper on top of RAID-0 breaks because DM
499 * currently doesn't honor MD's merge_bvec_fn routine.
500 * In this case, we'll force DM to use PAGE_SIZE or
501 * smaller I/O, just to be safe. A better fix is in the
502 * works, but add this for the time being so it will at
503 * least operate correctly.
505 if (q->merge_bvec_fn)
506 rs->max_sectors =
507 min_not_zero(rs->max_sectors,
508 (unsigned short)(PAGE_SIZE >> 9));
510 rs->max_phys_segments =
511 min_not_zero(rs->max_phys_segments,
512 q->max_phys_segments);
514 rs->max_hw_segments =
515 min_not_zero(rs->max_hw_segments, q->max_hw_segments);
517 rs->hardsect_size = max(rs->hardsect_size, q->hardsect_size);
519 rs->max_segment_size =
520 min_not_zero(rs->max_segment_size, q->max_segment_size);
522 rs->seg_boundary_mask =
523 min_not_zero(rs->seg_boundary_mask,
524 q->seg_boundary_mask);
527 return r;
531 * Decrement a devices use count and remove it if necessary.
533 void dm_put_device(struct dm_target *ti, struct dm_dev *dd)
535 if (atomic_dec_and_test(&dd->count)) {
536 close_dev(dd);
537 list_del(&dd->list);
538 kfree(dd);
543 * Checks to see if the target joins onto the end of the table.
545 static int adjoin(struct dm_table *table, struct dm_target *ti)
547 struct dm_target *prev;
549 if (!table->num_targets)
550 return !ti->begin;
552 prev = &table->targets[table->num_targets - 1];
553 return (ti->begin == (prev->begin + prev->len));
557 * Used to dynamically allocate the arg array.
559 static char **realloc_argv(unsigned *array_size, char **old_argv)
561 char **argv;
562 unsigned new_size;
564 new_size = *array_size ? *array_size * 2 : 64;
565 argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL);
566 if (argv) {
567 memcpy(argv, old_argv, *array_size * sizeof(*argv));
568 *array_size = new_size;
571 kfree(old_argv);
572 return argv;
576 * Destructively splits up the argument list to pass to ctr.
578 static int split_args(int *argc, char ***argvp, char *input)
580 char *start, *end = input, *out, **argv = NULL;
581 unsigned array_size = 0;
583 *argc = 0;
584 argv = realloc_argv(&array_size, argv);
585 if (!argv)
586 return -ENOMEM;
588 while (1) {
589 start = end;
591 /* Skip whitespace */
592 while (*start && isspace(*start))
593 start++;
595 if (!*start)
596 break; /* success, we hit the end */
598 /* 'out' is used to remove any back-quotes */
599 end = out = start;
600 while (*end) {
601 /* Everything apart from '\0' can be quoted */
602 if (*end == '\\' && *(end + 1)) {
603 *out++ = *(end + 1);
604 end += 2;
605 continue;
608 if (isspace(*end))
609 break; /* end of token */
611 *out++ = *end++;
614 /* have we already filled the array ? */
615 if ((*argc + 1) > array_size) {
616 argv = realloc_argv(&array_size, argv);
617 if (!argv)
618 return -ENOMEM;
621 /* we know this is whitespace */
622 if (*end)
623 end++;
625 /* terminate the string and put it in the array */
626 *out = '\0';
627 argv[*argc] = start;
628 (*argc)++;
631 *argvp = argv;
632 return 0;
635 static void check_for_valid_limits(struct io_restrictions *rs)
637 if (!rs->max_sectors)
638 rs->max_sectors = MAX_SECTORS;
639 if (!rs->max_phys_segments)
640 rs->max_phys_segments = MAX_PHYS_SEGMENTS;
641 if (!rs->max_hw_segments)
642 rs->max_hw_segments = MAX_HW_SEGMENTS;
643 if (!rs->hardsect_size)
644 rs->hardsect_size = 1 << SECTOR_SHIFT;
645 if (!rs->max_segment_size)
646 rs->max_segment_size = MAX_SEGMENT_SIZE;
647 if (!rs->seg_boundary_mask)
648 rs->seg_boundary_mask = -1;
651 int dm_table_add_target(struct dm_table *t, const char *type,
652 sector_t start, sector_t len, char *params)
654 int r = -EINVAL, argc;
655 char **argv;
656 struct dm_target *tgt;
658 if ((r = check_space(t)))
659 return r;
661 tgt = t->targets + t->num_targets;
662 memset(tgt, 0, sizeof(*tgt));
664 if (!len) {
665 tgt->error = "zero-length target";
666 DMERR(": %s\n", tgt->error);
667 return -EINVAL;
670 tgt->type = dm_get_target_type(type);
671 if (!tgt->type) {
672 tgt->error = "unknown target type";
673 DMERR(": %s\n", tgt->error);
674 return -EINVAL;
677 tgt->table = t;
678 tgt->begin = start;
679 tgt->len = len;
680 tgt->error = "Unknown error";
683 * Does this target adjoin the previous one ?
685 if (!adjoin(t, tgt)) {
686 tgt->error = "Gap in table";
687 r = -EINVAL;
688 goto bad;
691 r = split_args(&argc, &argv, params);
692 if (r) {
693 tgt->error = "couldn't split parameters (insufficient memory)";
694 goto bad;
697 r = tgt->type->ctr(tgt, argc, argv);
698 kfree(argv);
699 if (r)
700 goto bad;
702 t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
704 /* FIXME: the plan is to combine high here and then have
705 * the merge fn apply the target level restrictions. */
706 combine_restrictions_low(&t->limits, &tgt->limits);
707 return 0;
709 bad:
710 DMERR(": %s\n", tgt->error);
711 dm_put_target_type(tgt->type);
712 return r;
715 static int setup_indexes(struct dm_table *t)
717 int i;
718 unsigned int total = 0;
719 sector_t *indexes;
721 /* allocate the space for *all* the indexes */
722 for (i = t->depth - 2; i >= 0; i--) {
723 t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
724 total += t->counts[i];
727 indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
728 if (!indexes)
729 return -ENOMEM;
731 /* set up internal nodes, bottom-up */
732 for (i = t->depth - 2, total = 0; i >= 0; i--) {
733 t->index[i] = indexes;
734 indexes += (KEYS_PER_NODE * t->counts[i]);
735 setup_btree_index(i, t);
738 return 0;
742 * Builds the btree to index the map.
744 int dm_table_complete(struct dm_table *t)
746 int r = 0;
747 unsigned int leaf_nodes;
749 check_for_valid_limits(&t->limits);
751 /* how many indexes will the btree have ? */
752 leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
753 t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
755 /* leaf layer has already been set up */
756 t->counts[t->depth - 1] = leaf_nodes;
757 t->index[t->depth - 1] = t->highs;
759 if (t->depth >= 2)
760 r = setup_indexes(t);
762 return r;
765 static DECLARE_MUTEX(_event_lock);
766 void dm_table_event_callback(struct dm_table *t,
767 void (*fn)(void *), void *context)
769 down(&_event_lock);
770 t->event_fn = fn;
771 t->event_context = context;
772 up(&_event_lock);
775 void dm_table_event(struct dm_table *t)
778 * You can no longer call dm_table_event() from interrupt
779 * context, use a bottom half instead.
781 BUG_ON(in_interrupt());
783 down(&_event_lock);
784 if (t->event_fn)
785 t->event_fn(t->event_context);
786 up(&_event_lock);
789 sector_t dm_table_get_size(struct dm_table *t)
791 return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
794 struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
796 if (index > t->num_targets)
797 return NULL;
799 return t->targets + index;
803 * Search the btree for the correct target.
805 struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
807 unsigned int l, n = 0, k = 0;
808 sector_t *node;
810 for (l = 0; l < t->depth; l++) {
811 n = get_child(n, k);
812 node = get_node(t, l, n);
814 for (k = 0; k < KEYS_PER_NODE; k++)
815 if (node[k] >= sector)
816 break;
819 return &t->targets[(KEYS_PER_NODE * n) + k];
822 void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
825 * Make sure we obey the optimistic sub devices
826 * restrictions.
828 blk_queue_max_sectors(q, t->limits.max_sectors);
829 q->max_phys_segments = t->limits.max_phys_segments;
830 q->max_hw_segments = t->limits.max_hw_segments;
831 q->hardsect_size = t->limits.hardsect_size;
832 q->max_segment_size = t->limits.max_segment_size;
833 q->seg_boundary_mask = t->limits.seg_boundary_mask;
836 unsigned int dm_table_get_num_targets(struct dm_table *t)
838 return t->num_targets;
841 struct list_head *dm_table_get_devices(struct dm_table *t)
843 return &t->devices;
846 int dm_table_get_mode(struct dm_table *t)
848 return t->mode;
851 void dm_table_suspend_targets(struct dm_table *t)
853 int i;
855 for (i = 0; i < t->num_targets; i++) {
856 struct dm_target *ti = t->targets + i;
858 if (ti->type->suspend)
859 ti->type->suspend(ti);
863 void dm_table_resume_targets(struct dm_table *t)
865 int i;
867 for (i = 0; i < t->num_targets; i++) {
868 struct dm_target *ti = t->targets + i;
870 if (ti->type->resume)
871 ti->type->resume(ti);
875 int dm_table_any_congested(struct dm_table *t, int bdi_bits)
877 struct list_head *d, *devices;
878 int r = 0;
880 devices = dm_table_get_devices(t);
881 for (d = devices->next; d != devices; d = d->next) {
882 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
883 request_queue_t *q = bdev_get_queue(dd->bdev);
884 r |= bdi_congested(&q->backing_dev_info, bdi_bits);
887 return r;
890 void dm_table_unplug_all(struct dm_table *t)
892 struct list_head *d, *devices = dm_table_get_devices(t);
894 for (d = devices->next; d != devices; d = d->next) {
895 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
896 request_queue_t *q = bdev_get_queue(dd->bdev);
898 if (q->unplug_fn)
899 q->unplug_fn(q);
903 int dm_table_flush_all(struct dm_table *t)
905 struct list_head *d, *devices = dm_table_get_devices(t);
906 int ret = 0;
908 for (d = devices->next; d != devices; d = d->next) {
909 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
910 request_queue_t *q = bdev_get_queue(dd->bdev);
911 int err;
913 if (!q->issue_flush_fn)
914 err = -EOPNOTSUPP;
915 else
916 err = q->issue_flush_fn(q, dd->bdev->bd_disk, NULL);
918 if (!ret)
919 ret = err;
922 return ret;
925 EXPORT_SYMBOL(dm_vcalloc);
926 EXPORT_SYMBOL(dm_get_device);
927 EXPORT_SYMBOL(dm_put_device);
928 EXPORT_SYMBOL(dm_table_event);
929 EXPORT_SYMBOL(dm_table_get_mode);
930 EXPORT_SYMBOL(dm_table_put);
931 EXPORT_SYMBOL(dm_table_get);
932 EXPORT_SYMBOL(dm_table_unplug_all);
933 EXPORT_SYMBOL(dm_table_flush_all);