Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / md / dm-table.c
blob18e9b9953fcd32489a208b273927616da7dd4627
1 /*
2 * Copyright (C) 2001 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
6 */
8 #include "dm.h"
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 <asm/atomic.h>
19 #define MAX_DEPTH 16
20 #define NODE_SIZE L1_CACHE_BYTES
21 #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
22 #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
24 struct dm_table {
25 atomic_t holders;
27 /* btree table */
28 unsigned int depth;
29 unsigned int counts[MAX_DEPTH]; /* in nodes */
30 sector_t *index[MAX_DEPTH];
32 unsigned int num_targets;
33 unsigned int num_allocated;
34 sector_t *highs;
35 struct dm_target *targets;
38 * Indicates the rw permissions for the new logical
39 * device. This should be a combination of FMODE_READ
40 * and FMODE_WRITE.
42 int mode;
44 /* a list of devices used by this table */
45 struct list_head devices;
48 * These are optimistic limits taken from all the
49 * targets, some targets will need smaller limits.
51 struct io_restrictions limits;
53 /* events get handed up using this callback */
54 void (*event_fn)(void *);
55 void *event_context;
59 * Similar to ceiling(log_size(n))
61 static unsigned int int_log(unsigned int n, unsigned int base)
63 int result = 0;
65 while (n > 1) {
66 n = dm_div_up(n, base);
67 result++;
70 return result;
74 * Returns the minimum that is _not_ zero, unless both are zero.
76 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
79 * Combine two io_restrictions, always taking the lower value.
81 static void combine_restrictions_low(struct io_restrictions *lhs,
82 struct io_restrictions *rhs)
84 lhs->max_sectors =
85 min_not_zero(lhs->max_sectors, rhs->max_sectors);
87 lhs->max_phys_segments =
88 min_not_zero(lhs->max_phys_segments, rhs->max_phys_segments);
90 lhs->max_hw_segments =
91 min_not_zero(lhs->max_hw_segments, rhs->max_hw_segments);
93 lhs->hardsect_size = max(lhs->hardsect_size, rhs->hardsect_size);
95 lhs->max_segment_size =
96 min_not_zero(lhs->max_segment_size, rhs->max_segment_size);
98 lhs->seg_boundary_mask =
99 min_not_zero(lhs->seg_boundary_mask, rhs->seg_boundary_mask);
103 * Calculate the index of the child node of the n'th node k'th key.
105 static inline unsigned int get_child(unsigned int n, unsigned int k)
107 return (n * CHILDREN_PER_NODE) + k;
111 * Return the n'th node of level l from table t.
113 static inline sector_t *get_node(struct dm_table *t,
114 unsigned int l, unsigned int n)
116 return t->index[l] + (n * KEYS_PER_NODE);
120 * Return the highest key that you could lookup from the n'th
121 * node on level l of the btree.
123 static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
125 for (; l < t->depth - 1; l++)
126 n = get_child(n, CHILDREN_PER_NODE - 1);
128 if (n >= t->counts[l])
129 return (sector_t) - 1;
131 return get_node(t, l, n)[KEYS_PER_NODE - 1];
135 * Fills in a level of the btree based on the highs of the level
136 * below it.
138 static int setup_btree_index(unsigned int l, struct dm_table *t)
140 unsigned int n, k;
141 sector_t *node;
143 for (n = 0U; n < t->counts[l]; n++) {
144 node = get_node(t, l, n);
146 for (k = 0U; k < KEYS_PER_NODE; k++)
147 node[k] = high(t, l + 1, get_child(n, k));
150 return 0;
153 void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
155 unsigned long size;
156 void *addr;
159 * Check that we're not going to overflow.
161 if (nmemb > (ULONG_MAX / elem_size))
162 return NULL;
164 size = nmemb * elem_size;
165 addr = vmalloc(size);
166 if (addr)
167 memset(addr, 0, size);
169 return addr;
173 * highs, and targets are managed as dynamic arrays during a
174 * table load.
176 static int alloc_targets(struct dm_table *t, unsigned int num)
178 sector_t *n_highs;
179 struct dm_target *n_targets;
180 int n = t->num_targets;
183 * Allocate both the target array and offset array at once.
185 n_highs = (sector_t *) dm_vcalloc(num, sizeof(struct dm_target) +
186 sizeof(sector_t));
187 if (!n_highs)
188 return -ENOMEM;
190 n_targets = (struct dm_target *) (n_highs + num);
192 if (n) {
193 memcpy(n_highs, t->highs, sizeof(*n_highs) * n);
194 memcpy(n_targets, t->targets, sizeof(*n_targets) * n);
197 memset(n_highs + n, -1, sizeof(*n_highs) * (num - n));
198 vfree(t->highs);
200 t->num_allocated = num;
201 t->highs = n_highs;
202 t->targets = n_targets;
204 return 0;
207 int dm_table_create(struct dm_table **result, int mode, unsigned num_targets)
209 struct dm_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
211 if (!t)
212 return -ENOMEM;
214 memset(t, 0, sizeof(*t));
215 INIT_LIST_HEAD(&t->devices);
216 atomic_set(&t->holders, 1);
218 if (!num_targets)
219 num_targets = KEYS_PER_NODE;
221 num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
223 if (alloc_targets(t, num_targets)) {
224 kfree(t);
225 t = NULL;
226 return -ENOMEM;
229 t->mode = mode;
230 *result = t;
231 return 0;
234 static void free_devices(struct list_head *devices)
236 struct list_head *tmp, *next;
238 for (tmp = devices->next; tmp != devices; tmp = next) {
239 struct dm_dev *dd = list_entry(tmp, struct dm_dev, list);
240 next = tmp->next;
241 kfree(dd);
245 static void table_destroy(struct dm_table *t)
247 unsigned int i;
249 /* free the indexes (see dm_table_complete) */
250 if (t->depth >= 2)
251 vfree(t->index[t->depth - 2]);
253 /* free the targets */
254 for (i = 0; i < t->num_targets; i++) {
255 struct dm_target *tgt = t->targets + i;
257 if (tgt->type->dtr)
258 tgt->type->dtr(tgt);
260 dm_put_target_type(tgt->type);
263 vfree(t->highs);
265 /* free the device list */
266 if (t->devices.next != &t->devices) {
267 DMWARN("devices still present during destroy: "
268 "dm_table_remove_device calls missing");
270 free_devices(&t->devices);
273 kfree(t);
276 void dm_table_get(struct dm_table *t)
278 atomic_inc(&t->holders);
281 void dm_table_put(struct dm_table *t)
283 if (!t)
284 return;
286 if (atomic_dec_and_test(&t->holders))
287 table_destroy(t);
291 * Checks to see if we need to extend highs or targets.
293 static inline int check_space(struct dm_table *t)
295 if (t->num_targets >= t->num_allocated)
296 return alloc_targets(t, t->num_allocated * 2);
298 return 0;
302 * Convert a device path to a dev_t.
304 static int lookup_device(const char *path, dev_t *dev)
306 int r;
307 struct nameidata nd;
308 struct inode *inode;
310 if ((r = path_lookup(path, LOOKUP_FOLLOW, &nd)))
311 return r;
313 inode = nd.dentry->d_inode;
314 if (!inode) {
315 r = -ENOENT;
316 goto out;
319 if (!S_ISBLK(inode->i_mode)) {
320 r = -ENOTBLK;
321 goto out;
324 *dev = inode->i_rdev;
326 out:
327 path_release(&nd);
328 return r;
332 * See if we've already got a device in the list.
334 static struct dm_dev *find_device(struct list_head *l, dev_t dev)
336 struct dm_dev *dd;
338 list_for_each_entry (dd, l, list)
339 if (dd->bdev->bd_dev == dev)
340 return dd;
342 return NULL;
346 * Open a device so we can use it as a map destination.
348 static int open_dev(struct dm_dev *d, dev_t dev)
350 static char *_claim_ptr = "I belong to device-mapper";
351 struct block_device *bdev;
353 int r;
355 if (d->bdev)
356 BUG();
358 bdev = open_by_devnum(dev, d->mode);
359 if (IS_ERR(bdev))
360 return PTR_ERR(bdev);
361 r = bd_claim(bdev, _claim_ptr);
362 if (r)
363 blkdev_put(bdev);
364 else
365 d->bdev = bdev;
366 return r;
370 * Close a device that we've been using.
372 static void close_dev(struct dm_dev *d)
374 if (!d->bdev)
375 return;
377 bd_release(d->bdev);
378 blkdev_put(d->bdev);
379 d->bdev = NULL;
383 * If possible (ie. blk_size[major] is set), this checks an area
384 * of a destination device is valid.
386 static int check_device_area(struct dm_dev *dd, sector_t start, sector_t len)
388 sector_t dev_size;
389 dev_size = dd->bdev->bd_inode->i_size >> SECTOR_SHIFT;
390 return ((start < dev_size) && (len <= (dev_size - start)));
394 * This upgrades the mode on an already open dm_dev. Being
395 * careful to leave things as they were if we fail to reopen the
396 * device.
398 static int upgrade_mode(struct dm_dev *dd, int new_mode)
400 int r;
401 struct dm_dev dd_copy;
402 dev_t dev = dd->bdev->bd_dev;
404 dd_copy = *dd;
406 dd->mode |= new_mode;
407 dd->bdev = NULL;
408 r = open_dev(dd, dev);
409 if (!r)
410 close_dev(&dd_copy);
411 else
412 *dd = dd_copy;
414 return r;
418 * Add a device to the list, or just increment the usage count if
419 * it's already present.
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 int mode, struct dm_dev **result)
425 int r;
426 dev_t dev;
427 struct dm_dev *dd;
428 unsigned int major, minor;
430 if (!t)
431 BUG();
433 if (sscanf(path, "%u:%u", &major, &minor) == 2) {
434 /* Extract the major/minor numbers */
435 dev = MKDEV(major, minor);
436 if (MAJOR(dev) != major || MINOR(dev) != minor)
437 return -EOVERFLOW;
438 } else {
439 /* convert the path to a device */
440 if ((r = lookup_device(path, &dev)))
441 return r;
444 dd = find_device(&t->devices, dev);
445 if (!dd) {
446 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
447 if (!dd)
448 return -ENOMEM;
450 dd->mode = mode;
451 dd->bdev = NULL;
453 if ((r = open_dev(dd, dev))) {
454 kfree(dd);
455 return r;
458 format_dev_t(dd->name, dev);
460 atomic_set(&dd->count, 0);
461 list_add(&dd->list, &t->devices);
463 } else if (dd->mode != (mode | dd->mode)) {
464 r = upgrade_mode(dd, mode);
465 if (r)
466 return r;
468 atomic_inc(&dd->count);
470 if (!check_device_area(dd, start, len)) {
471 DMWARN("device %s too small for target", path);
472 dm_put_device(ti, dd);
473 return -EINVAL;
476 *result = dd;
478 return 0;
482 int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
483 sector_t len, int mode, struct dm_dev **result)
485 int r = __table_get_device(ti->table, ti, path,
486 start, len, mode, result);
487 if (!r) {
488 request_queue_t *q = bdev_get_queue((*result)->bdev);
489 struct io_restrictions *rs = &ti->limits;
492 * Combine the device limits low.
494 * FIXME: if we move an io_restriction struct
495 * into q this would just be a call to
496 * combine_restrictions_low()
498 rs->max_sectors =
499 min_not_zero(rs->max_sectors, q->max_sectors);
501 /* FIXME: Device-Mapper on top of RAID-0 breaks because DM
502 * currently doesn't honor MD's merge_bvec_fn routine.
503 * In this case, we'll force DM to use PAGE_SIZE or
504 * smaller I/O, just to be safe. A better fix is in the
505 * works, but add this for the time being so it will at
506 * least operate correctly.
508 if (q->merge_bvec_fn)
509 rs->max_sectors =
510 min_not_zero(rs->max_sectors,
511 (unsigned short)(PAGE_SIZE >> 9));
513 rs->max_phys_segments =
514 min_not_zero(rs->max_phys_segments,
515 q->max_phys_segments);
517 rs->max_hw_segments =
518 min_not_zero(rs->max_hw_segments, q->max_hw_segments);
520 rs->hardsect_size = max(rs->hardsect_size, q->hardsect_size);
522 rs->max_segment_size =
523 min_not_zero(rs->max_segment_size, q->max_segment_size);
525 rs->seg_boundary_mask =
526 min_not_zero(rs->seg_boundary_mask,
527 q->seg_boundary_mask);
530 return r;
534 * Decrement a devices use count and remove it if necessary.
536 void dm_put_device(struct dm_target *ti, struct dm_dev *dd)
538 if (atomic_dec_and_test(&dd->count)) {
539 close_dev(dd);
540 list_del(&dd->list);
541 kfree(dd);
546 * Checks to see if the target joins onto the end of the table.
548 static int adjoin(struct dm_table *table, struct dm_target *ti)
550 struct dm_target *prev;
552 if (!table->num_targets)
553 return !ti->begin;
555 prev = &table->targets[table->num_targets - 1];
556 return (ti->begin == (prev->begin + prev->len));
560 * Used to dynamically allocate the arg array.
562 static char **realloc_argv(unsigned *array_size, char **old_argv)
564 char **argv;
565 unsigned new_size;
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;
574 kfree(old_argv);
575 return argv;
579 * Destructively splits up the argument list to pass to ctr.
581 int dm_split_args(int *argc, char ***argvp, char *input)
583 char *start, *end = input, *out, **argv = NULL;
584 unsigned array_size = 0;
586 *argc = 0;
587 argv = realloc_argv(&array_size, argv);
588 if (!argv)
589 return -ENOMEM;
591 while (1) {
592 start = end;
594 /* Skip whitespace */
595 while (*start && isspace(*start))
596 start++;
598 if (!*start)
599 break; /* success, we hit the end */
601 /* 'out' is used to remove any back-quotes */
602 end = out = start;
603 while (*end) {
604 /* Everything apart from '\0' can be quoted */
605 if (*end == '\\' && *(end + 1)) {
606 *out++ = *(end + 1);
607 end += 2;
608 continue;
611 if (isspace(*end))
612 break; /* end of token */
614 *out++ = *end++;
617 /* have we already filled the array ? */
618 if ((*argc + 1) > array_size) {
619 argv = realloc_argv(&array_size, argv);
620 if (!argv)
621 return -ENOMEM;
624 /* we know this is whitespace */
625 if (*end)
626 end++;
628 /* terminate the string and put it in the array */
629 *out = '\0';
630 argv[*argc] = start;
631 (*argc)++;
634 *argvp = argv;
635 return 0;
638 static void check_for_valid_limits(struct io_restrictions *rs)
640 if (!rs->max_sectors)
641 rs->max_sectors = MAX_SECTORS;
642 if (!rs->max_phys_segments)
643 rs->max_phys_segments = MAX_PHYS_SEGMENTS;
644 if (!rs->max_hw_segments)
645 rs->max_hw_segments = MAX_HW_SEGMENTS;
646 if (!rs->hardsect_size)
647 rs->hardsect_size = 1 << SECTOR_SHIFT;
648 if (!rs->max_segment_size)
649 rs->max_segment_size = MAX_SEGMENT_SIZE;
650 if (!rs->seg_boundary_mask)
651 rs->seg_boundary_mask = -1;
654 int dm_table_add_target(struct dm_table *t, const char *type,
655 sector_t start, sector_t len, char *params)
657 int r = -EINVAL, argc;
658 char **argv;
659 struct dm_target *tgt;
661 if ((r = check_space(t)))
662 return r;
664 tgt = t->targets + t->num_targets;
665 memset(tgt, 0, sizeof(*tgt));
667 if (!len) {
668 tgt->error = "zero-length target";
669 DMERR("%s", tgt->error);
670 return -EINVAL;
673 tgt->type = dm_get_target_type(type);
674 if (!tgt->type) {
675 tgt->error = "unknown target type";
676 DMERR("%s", tgt->error);
677 return -EINVAL;
680 tgt->table = t;
681 tgt->begin = start;
682 tgt->len = len;
683 tgt->error = "Unknown error";
686 * Does this target adjoin the previous one ?
688 if (!adjoin(t, tgt)) {
689 tgt->error = "Gap in table";
690 r = -EINVAL;
691 goto bad;
694 r = dm_split_args(&argc, &argv, params);
695 if (r) {
696 tgt->error = "couldn't split parameters (insufficient memory)";
697 goto bad;
700 r = tgt->type->ctr(tgt, argc, argv);
701 kfree(argv);
702 if (r)
703 goto bad;
705 t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
707 /* FIXME: the plan is to combine high here and then have
708 * the merge fn apply the target level restrictions. */
709 combine_restrictions_low(&t->limits, &tgt->limits);
710 return 0;
712 bad:
713 DMERR("%s", tgt->error);
714 dm_put_target_type(tgt->type);
715 return r;
718 static int setup_indexes(struct dm_table *t)
720 int i;
721 unsigned int total = 0;
722 sector_t *indexes;
724 /* allocate the space for *all* the indexes */
725 for (i = t->depth - 2; i >= 0; i--) {
726 t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
727 total += t->counts[i];
730 indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
731 if (!indexes)
732 return -ENOMEM;
734 /* set up internal nodes, bottom-up */
735 for (i = t->depth - 2, total = 0; i >= 0; i--) {
736 t->index[i] = indexes;
737 indexes += (KEYS_PER_NODE * t->counts[i]);
738 setup_btree_index(i, t);
741 return 0;
745 * Builds the btree to index the map.
747 int dm_table_complete(struct dm_table *t)
749 int r = 0;
750 unsigned int leaf_nodes;
752 check_for_valid_limits(&t->limits);
754 /* how many indexes will the btree have ? */
755 leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
756 t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
758 /* leaf layer has already been set up */
759 t->counts[t->depth - 1] = leaf_nodes;
760 t->index[t->depth - 1] = t->highs;
762 if (t->depth >= 2)
763 r = setup_indexes(t);
765 return r;
768 static DECLARE_MUTEX(_event_lock);
769 void dm_table_event_callback(struct dm_table *t,
770 void (*fn)(void *), void *context)
772 down(&_event_lock);
773 t->event_fn = fn;
774 t->event_context = context;
775 up(&_event_lock);
778 void dm_table_event(struct dm_table *t)
781 * You can no longer call dm_table_event() from interrupt
782 * context, use a bottom half instead.
784 BUG_ON(in_interrupt());
786 down(&_event_lock);
787 if (t->event_fn)
788 t->event_fn(t->event_context);
789 up(&_event_lock);
792 sector_t dm_table_get_size(struct dm_table *t)
794 return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
797 struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
799 if (index > t->num_targets)
800 return NULL;
802 return t->targets + index;
806 * Search the btree for the correct target.
808 struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
810 unsigned int l, n = 0, k = 0;
811 sector_t *node;
813 for (l = 0; l < t->depth; l++) {
814 n = get_child(n, k);
815 node = get_node(t, l, n);
817 for (k = 0; k < KEYS_PER_NODE; k++)
818 if (node[k] >= sector)
819 break;
822 return &t->targets[(KEYS_PER_NODE * n) + k];
825 void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
828 * Make sure we obey the optimistic sub devices
829 * restrictions.
831 blk_queue_max_sectors(q, t->limits.max_sectors);
832 q->max_phys_segments = t->limits.max_phys_segments;
833 q->max_hw_segments = t->limits.max_hw_segments;
834 q->hardsect_size = t->limits.hardsect_size;
835 q->max_segment_size = t->limits.max_segment_size;
836 q->seg_boundary_mask = t->limits.seg_boundary_mask;
839 unsigned int dm_table_get_num_targets(struct dm_table *t)
841 return t->num_targets;
844 struct list_head *dm_table_get_devices(struct dm_table *t)
846 return &t->devices;
849 int dm_table_get_mode(struct dm_table *t)
851 return t->mode;
854 static void suspend_targets(struct dm_table *t, unsigned postsuspend)
856 int i = t->num_targets;
857 struct dm_target *ti = t->targets;
859 while (i--) {
860 if (postsuspend) {
861 if (ti->type->postsuspend)
862 ti->type->postsuspend(ti);
863 } else if (ti->type->presuspend)
864 ti->type->presuspend(ti);
866 ti++;
870 void dm_table_presuspend_targets(struct dm_table *t)
872 return suspend_targets(t, 0);
875 void dm_table_postsuspend_targets(struct dm_table *t)
877 return suspend_targets(t, 1);
880 void dm_table_resume_targets(struct dm_table *t)
882 int i;
884 for (i = 0; i < t->num_targets; i++) {
885 struct dm_target *ti = t->targets + i;
887 if (ti->type->resume)
888 ti->type->resume(ti);
892 int dm_table_any_congested(struct dm_table *t, int bdi_bits)
894 struct list_head *d, *devices;
895 int r = 0;
897 devices = dm_table_get_devices(t);
898 for (d = devices->next; d != devices; d = d->next) {
899 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
900 request_queue_t *q = bdev_get_queue(dd->bdev);
901 r |= bdi_congested(&q->backing_dev_info, bdi_bits);
904 return r;
907 void dm_table_unplug_all(struct dm_table *t)
909 struct list_head *d, *devices = dm_table_get_devices(t);
911 for (d = devices->next; d != devices; d = d->next) {
912 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
913 request_queue_t *q = bdev_get_queue(dd->bdev);
915 if (q->unplug_fn)
916 q->unplug_fn(q);
920 int dm_table_flush_all(struct dm_table *t)
922 struct list_head *d, *devices = dm_table_get_devices(t);
923 int ret = 0;
925 for (d = devices->next; d != devices; d = d->next) {
926 struct dm_dev *dd = list_entry(d, struct dm_dev, list);
927 request_queue_t *q = bdev_get_queue(dd->bdev);
928 int err;
930 if (!q->issue_flush_fn)
931 err = -EOPNOTSUPP;
932 else
933 err = q->issue_flush_fn(q, dd->bdev->bd_disk, NULL);
935 if (!ret)
936 ret = err;
939 return ret;
942 EXPORT_SYMBOL(dm_vcalloc);
943 EXPORT_SYMBOL(dm_get_device);
944 EXPORT_SYMBOL(dm_put_device);
945 EXPORT_SYMBOL(dm_table_event);
946 EXPORT_SYMBOL(dm_table_get_mode);
947 EXPORT_SYMBOL(dm_table_put);
948 EXPORT_SYMBOL(dm_table_get);
949 EXPORT_SYMBOL(dm_table_unplug_all);
950 EXPORT_SYMBOL(dm_table_flush_all);