diff options
Diffstat (limited to 'drivers/md/dm.c')
-rw-r--r-- | drivers/md/dm.c | 184 |
1 files changed, 127 insertions, 57 deletions
diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 4d710b7a133b..3ed2e53b9eb6 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. | 2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. |
3 | * Copyright (C) 2004 Red Hat, Inc. All rights reserved. | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
4 | * | 4 | * |
5 | * This file is released under the GPL. | 5 | * This file is released under the GPL. |
6 | */ | 6 | */ |
@@ -21,11 +21,14 @@ | |||
21 | #include <linux/hdreg.h> | 21 | #include <linux/hdreg.h> |
22 | #include <linux/blktrace_api.h> | 22 | #include <linux/blktrace_api.h> |
23 | 23 | ||
24 | #define DM_MSG_PREFIX "core" | ||
25 | |||
24 | static const char *_name = DM_NAME; | 26 | static const char *_name = DM_NAME; |
25 | 27 | ||
26 | static unsigned int major = 0; | 28 | static unsigned int major = 0; |
27 | static unsigned int _major = 0; | 29 | static unsigned int _major = 0; |
28 | 30 | ||
31 | static DEFINE_SPINLOCK(_minor_lock); | ||
29 | /* | 32 | /* |
30 | * One of these is allocated per bio. | 33 | * One of these is allocated per bio. |
31 | */ | 34 | */ |
@@ -49,23 +52,28 @@ struct target_io { | |||
49 | 52 | ||
50 | union map_info *dm_get_mapinfo(struct bio *bio) | 53 | union map_info *dm_get_mapinfo(struct bio *bio) |
51 | { | 54 | { |
52 | if (bio && bio->bi_private) | 55 | if (bio && bio->bi_private) |
53 | return &((struct target_io *)bio->bi_private)->info; | 56 | return &((struct target_io *)bio->bi_private)->info; |
54 | return NULL; | 57 | return NULL; |
55 | } | 58 | } |
56 | 59 | ||
60 | #define MINOR_ALLOCED ((void *)-1) | ||
61 | |||
57 | /* | 62 | /* |
58 | * Bits for the md->flags field. | 63 | * Bits for the md->flags field. |
59 | */ | 64 | */ |
60 | #define DMF_BLOCK_IO 0 | 65 | #define DMF_BLOCK_IO 0 |
61 | #define DMF_SUSPENDED 1 | 66 | #define DMF_SUSPENDED 1 |
62 | #define DMF_FROZEN 2 | 67 | #define DMF_FROZEN 2 |
68 | #define DMF_FREEING 3 | ||
69 | #define DMF_DELETING 4 | ||
63 | 70 | ||
64 | struct mapped_device { | 71 | struct mapped_device { |
65 | struct rw_semaphore io_lock; | 72 | struct rw_semaphore io_lock; |
66 | struct semaphore suspend_lock; | 73 | struct semaphore suspend_lock; |
67 | rwlock_t map_lock; | 74 | rwlock_t map_lock; |
68 | atomic_t holders; | 75 | atomic_t holders; |
76 | atomic_t open_count; | ||
69 | 77 | ||
70 | unsigned long flags; | 78 | unsigned long flags; |
71 | 79 | ||
@@ -218,9 +226,25 @@ static int dm_blk_open(struct inode *inode, struct file *file) | |||
218 | { | 226 | { |
219 | struct mapped_device *md; | 227 | struct mapped_device *md; |
220 | 228 | ||
229 | spin_lock(&_minor_lock); | ||
230 | |||
221 | md = inode->i_bdev->bd_disk->private_data; | 231 | md = inode->i_bdev->bd_disk->private_data; |
232 | if (!md) | ||
233 | goto out; | ||
234 | |||
235 | if (test_bit(DMF_FREEING, &md->flags) || | ||
236 | test_bit(DMF_DELETING, &md->flags)) { | ||
237 | md = NULL; | ||
238 | goto out; | ||
239 | } | ||
240 | |||
222 | dm_get(md); | 241 | dm_get(md); |
223 | return 0; | 242 | atomic_inc(&md->open_count); |
243 | |||
244 | out: | ||
245 | spin_unlock(&_minor_lock); | ||
246 | |||
247 | return md ? 0 : -ENXIO; | ||
224 | } | 248 | } |
225 | 249 | ||
226 | static int dm_blk_close(struct inode *inode, struct file *file) | 250 | static int dm_blk_close(struct inode *inode, struct file *file) |
@@ -228,10 +252,35 @@ static int dm_blk_close(struct inode *inode, struct file *file) | |||
228 | struct mapped_device *md; | 252 | struct mapped_device *md; |
229 | 253 | ||
230 | md = inode->i_bdev->bd_disk->private_data; | 254 | md = inode->i_bdev->bd_disk->private_data; |
255 | atomic_dec(&md->open_count); | ||
231 | dm_put(md); | 256 | dm_put(md); |
232 | return 0; | 257 | return 0; |
233 | } | 258 | } |
234 | 259 | ||
260 | int dm_open_count(struct mapped_device *md) | ||
261 | { | ||
262 | return atomic_read(&md->open_count); | ||
263 | } | ||
264 | |||
265 | /* | ||
266 | * Guarantees nothing is using the device before it's deleted. | ||
267 | */ | ||
268 | int dm_lock_for_deletion(struct mapped_device *md) | ||
269 | { | ||
270 | int r = 0; | ||
271 | |||
272 | spin_lock(&_minor_lock); | ||
273 | |||
274 | if (dm_open_count(md)) | ||
275 | r = -EBUSY; | ||
276 | else | ||
277 | set_bit(DMF_DELETING, &md->flags); | ||
278 | |||
279 | spin_unlock(&_minor_lock); | ||
280 | |||
281 | return r; | ||
282 | } | ||
283 | |||
235 | static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) | 284 | static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
236 | { | 285 | { |
237 | struct mapped_device *md = bdev->bd_disk->private_data; | 286 | struct mapped_device *md = bdev->bd_disk->private_data; |
@@ -456,8 +505,8 @@ static void __map_bio(struct dm_target *ti, struct bio *clone, | |||
456 | if (r > 0) { | 505 | if (r > 0) { |
457 | /* the bio has been remapped so dispatch it */ | 506 | /* the bio has been remapped so dispatch it */ |
458 | 507 | ||
459 | blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, | 508 | blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, |
460 | tio->io->bio->bi_bdev->bd_dev, sector, | 509 | tio->io->bio->bi_bdev->bd_dev, sector, |
461 | clone->bi_sector); | 510 | clone->bi_sector); |
462 | 511 | ||
463 | generic_make_request(clone); | 512 | generic_make_request(clone); |
@@ -744,43 +793,39 @@ static int dm_any_congested(void *congested_data, int bdi_bits) | |||
744 | /*----------------------------------------------------------------- | 793 | /*----------------------------------------------------------------- |
745 | * An IDR is used to keep track of allocated minor numbers. | 794 | * An IDR is used to keep track of allocated minor numbers. |
746 | *---------------------------------------------------------------*/ | 795 | *---------------------------------------------------------------*/ |
747 | static DEFINE_MUTEX(_minor_lock); | ||
748 | static DEFINE_IDR(_minor_idr); | 796 | static DEFINE_IDR(_minor_idr); |
749 | 797 | ||
750 | static void free_minor(unsigned int minor) | 798 | static void free_minor(int minor) |
751 | { | 799 | { |
752 | mutex_lock(&_minor_lock); | 800 | spin_lock(&_minor_lock); |
753 | idr_remove(&_minor_idr, minor); | 801 | idr_remove(&_minor_idr, minor); |
754 | mutex_unlock(&_minor_lock); | 802 | spin_unlock(&_minor_lock); |
755 | } | 803 | } |
756 | 804 | ||
757 | /* | 805 | /* |
758 | * See if the device with a specific minor # is free. | 806 | * See if the device with a specific minor # is free. |
759 | */ | 807 | */ |
760 | static int specific_minor(struct mapped_device *md, unsigned int minor) | 808 | static int specific_minor(struct mapped_device *md, int minor) |
761 | { | 809 | { |
762 | int r, m; | 810 | int r, m; |
763 | 811 | ||
764 | if (minor >= (1 << MINORBITS)) | 812 | if (minor >= (1 << MINORBITS)) |
765 | return -EINVAL; | 813 | return -EINVAL; |
766 | 814 | ||
767 | mutex_lock(&_minor_lock); | 815 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); |
816 | if (!r) | ||
817 | return -ENOMEM; | ||
818 | |||
819 | spin_lock(&_minor_lock); | ||
768 | 820 | ||
769 | if (idr_find(&_minor_idr, minor)) { | 821 | if (idr_find(&_minor_idr, minor)) { |
770 | r = -EBUSY; | 822 | r = -EBUSY; |
771 | goto out; | 823 | goto out; |
772 | } | 824 | } |
773 | 825 | ||
774 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); | 826 | r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); |
775 | if (!r) { | 827 | if (r) |
776 | r = -ENOMEM; | ||
777 | goto out; | ||
778 | } | ||
779 | |||
780 | r = idr_get_new_above(&_minor_idr, md, minor, &m); | ||
781 | if (r) { | ||
782 | goto out; | 828 | goto out; |
783 | } | ||
784 | 829 | ||
785 | if (m != minor) { | 830 | if (m != minor) { |
786 | idr_remove(&_minor_idr, m); | 831 | idr_remove(&_minor_idr, m); |
@@ -789,24 +834,21 @@ static int specific_minor(struct mapped_device *md, unsigned int minor) | |||
789 | } | 834 | } |
790 | 835 | ||
791 | out: | 836 | out: |
792 | mutex_unlock(&_minor_lock); | 837 | spin_unlock(&_minor_lock); |
793 | return r; | 838 | return r; |
794 | } | 839 | } |
795 | 840 | ||
796 | static int next_free_minor(struct mapped_device *md, unsigned int *minor) | 841 | static int next_free_minor(struct mapped_device *md, int *minor) |
797 | { | 842 | { |
798 | int r; | 843 | int r, m; |
799 | unsigned int m; | ||
800 | |||
801 | mutex_lock(&_minor_lock); | ||
802 | 844 | ||
803 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); | 845 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); |
804 | if (!r) { | 846 | if (!r) |
805 | r = -ENOMEM; | 847 | return -ENOMEM; |
806 | goto out; | 848 | |
807 | } | 849 | spin_lock(&_minor_lock); |
808 | 850 | ||
809 | r = idr_get_new(&_minor_idr, md, &m); | 851 | r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); |
810 | if (r) { | 852 | if (r) { |
811 | goto out; | 853 | goto out; |
812 | } | 854 | } |
@@ -820,7 +862,7 @@ static int next_free_minor(struct mapped_device *md, unsigned int *minor) | |||
820 | *minor = m; | 862 | *minor = m; |
821 | 863 | ||
822 | out: | 864 | out: |
823 | mutex_unlock(&_minor_lock); | 865 | spin_unlock(&_minor_lock); |
824 | return r; | 866 | return r; |
825 | } | 867 | } |
826 | 868 | ||
@@ -829,18 +871,25 @@ static struct block_device_operations dm_blk_dops; | |||
829 | /* | 871 | /* |
830 | * Allocate and initialise a blank device with a given minor. | 872 | * Allocate and initialise a blank device with a given minor. |
831 | */ | 873 | */ |
832 | static struct mapped_device *alloc_dev(unsigned int minor, int persistent) | 874 | static struct mapped_device *alloc_dev(int minor) |
833 | { | 875 | { |
834 | int r; | 876 | int r; |
835 | struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL); | 877 | struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL); |
878 | void *old_md; | ||
836 | 879 | ||
837 | if (!md) { | 880 | if (!md) { |
838 | DMWARN("unable to allocate device, out of memory."); | 881 | DMWARN("unable to allocate device, out of memory."); |
839 | return NULL; | 882 | return NULL; |
840 | } | 883 | } |
841 | 884 | ||
885 | if (!try_module_get(THIS_MODULE)) | ||
886 | goto bad0; | ||
887 | |||
842 | /* get a minor number for the dev */ | 888 | /* get a minor number for the dev */ |
843 | r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor); | 889 | if (minor == DM_ANY_MINOR) |
890 | r = next_free_minor(md, &minor); | ||
891 | else | ||
892 | r = specific_minor(md, minor); | ||
844 | if (r < 0) | 893 | if (r < 0) |
845 | goto bad1; | 894 | goto bad1; |
846 | 895 | ||
@@ -849,6 +898,7 @@ static struct mapped_device *alloc_dev(unsigned int minor, int persistent) | |||
849 | init_MUTEX(&md->suspend_lock); | 898 | init_MUTEX(&md->suspend_lock); |
850 | rwlock_init(&md->map_lock); | 899 | rwlock_init(&md->map_lock); |
851 | atomic_set(&md->holders, 1); | 900 | atomic_set(&md->holders, 1); |
901 | atomic_set(&md->open_count, 0); | ||
852 | atomic_set(&md->event_nr, 0); | 902 | atomic_set(&md->event_nr, 0); |
853 | 903 | ||
854 | md->queue = blk_alloc_queue(GFP_KERNEL); | 904 | md->queue = blk_alloc_queue(GFP_KERNEL); |
@@ -875,6 +925,10 @@ static struct mapped_device *alloc_dev(unsigned int minor, int persistent) | |||
875 | if (!md->disk) | 925 | if (!md->disk) |
876 | goto bad4; | 926 | goto bad4; |
877 | 927 | ||
928 | atomic_set(&md->pending, 0); | ||
929 | init_waitqueue_head(&md->wait); | ||
930 | init_waitqueue_head(&md->eventq); | ||
931 | |||
878 | md->disk->major = _major; | 932 | md->disk->major = _major; |
879 | md->disk->first_minor = minor; | 933 | md->disk->first_minor = minor; |
880 | md->disk->fops = &dm_blk_dops; | 934 | md->disk->fops = &dm_blk_dops; |
@@ -884,9 +938,12 @@ static struct mapped_device *alloc_dev(unsigned int minor, int persistent) | |||
884 | add_disk(md->disk); | 938 | add_disk(md->disk); |
885 | format_dev_t(md->name, MKDEV(_major, minor)); | 939 | format_dev_t(md->name, MKDEV(_major, minor)); |
886 | 940 | ||
887 | atomic_set(&md->pending, 0); | 941 | /* Populate the mapping, nobody knows we exist yet */ |
888 | init_waitqueue_head(&md->wait); | 942 | spin_lock(&_minor_lock); |
889 | init_waitqueue_head(&md->eventq); | 943 | old_md = idr_replace(&_minor_idr, md, minor); |
944 | spin_unlock(&_minor_lock); | ||
945 | |||
946 | BUG_ON(old_md != MINOR_ALLOCED); | ||
890 | 947 | ||
891 | return md; | 948 | return md; |
892 | 949 | ||
@@ -898,13 +955,15 @@ static struct mapped_device *alloc_dev(unsigned int minor, int persistent) | |||
898 | blk_cleanup_queue(md->queue); | 955 | blk_cleanup_queue(md->queue); |
899 | free_minor(minor); | 956 | free_minor(minor); |
900 | bad1: | 957 | bad1: |
958 | module_put(THIS_MODULE); | ||
959 | bad0: | ||
901 | kfree(md); | 960 | kfree(md); |
902 | return NULL; | 961 | return NULL; |
903 | } | 962 | } |
904 | 963 | ||
905 | static void free_dev(struct mapped_device *md) | 964 | static void free_dev(struct mapped_device *md) |
906 | { | 965 | { |
907 | unsigned int minor = md->disk->first_minor; | 966 | int minor = md->disk->first_minor; |
908 | 967 | ||
909 | if (md->suspended_bdev) { | 968 | if (md->suspended_bdev) { |
910 | thaw_bdev(md->suspended_bdev, NULL); | 969 | thaw_bdev(md->suspended_bdev, NULL); |
@@ -914,8 +973,14 @@ static void free_dev(struct mapped_device *md) | |||
914 | mempool_destroy(md->io_pool); | 973 | mempool_destroy(md->io_pool); |
915 | del_gendisk(md->disk); | 974 | del_gendisk(md->disk); |
916 | free_minor(minor); | 975 | free_minor(minor); |
976 | |||
977 | spin_lock(&_minor_lock); | ||
978 | md->disk->private_data = NULL; | ||
979 | spin_unlock(&_minor_lock); | ||
980 | |||
917 | put_disk(md->disk); | 981 | put_disk(md->disk); |
918 | blk_cleanup_queue(md->queue); | 982 | blk_cleanup_queue(md->queue); |
983 | module_put(THIS_MODULE); | ||
919 | kfree(md); | 984 | kfree(md); |
920 | } | 985 | } |
921 | 986 | ||
@@ -984,12 +1049,11 @@ static void __unbind(struct mapped_device *md) | |||
984 | /* | 1049 | /* |
985 | * Constructor for a new device. | 1050 | * Constructor for a new device. |
986 | */ | 1051 | */ |
987 | static int create_aux(unsigned int minor, int persistent, | 1052 | int dm_create(int minor, struct mapped_device **result) |
988 | struct mapped_device **result) | ||
989 | { | 1053 | { |
990 | struct mapped_device *md; | 1054 | struct mapped_device *md; |
991 | 1055 | ||
992 | md = alloc_dev(minor, persistent); | 1056 | md = alloc_dev(minor); |
993 | if (!md) | 1057 | if (!md) |
994 | return -ENXIO; | 1058 | return -ENXIO; |
995 | 1059 | ||
@@ -997,16 +1061,6 @@ static int create_aux(unsigned int minor, int persistent, | |||
997 | return 0; | 1061 | return 0; |
998 | } | 1062 | } |
999 | 1063 | ||
1000 | int dm_create(struct mapped_device **result) | ||
1001 | { | ||
1002 | return create_aux(0, 0, result); | ||
1003 | } | ||
1004 | |||
1005 | int dm_create_with_minor(unsigned int minor, struct mapped_device **result) | ||
1006 | { | ||
1007 | return create_aux(minor, 1, result); | ||
1008 | } | ||
1009 | |||
1010 | static struct mapped_device *dm_find_md(dev_t dev) | 1064 | static struct mapped_device *dm_find_md(dev_t dev) |
1011 | { | 1065 | { |
1012 | struct mapped_device *md; | 1066 | struct mapped_device *md; |
@@ -1015,13 +1069,18 @@ static struct mapped_device *dm_find_md(dev_t dev) | |||
1015 | if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) | 1069 | if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) |
1016 | return NULL; | 1070 | return NULL; |
1017 | 1071 | ||
1018 | mutex_lock(&_minor_lock); | 1072 | spin_lock(&_minor_lock); |
1019 | 1073 | ||
1020 | md = idr_find(&_minor_idr, minor); | 1074 | md = idr_find(&_minor_idr, minor); |
1021 | if (!md || (dm_disk(md)->first_minor != minor)) | 1075 | if (md && (md == MINOR_ALLOCED || |
1076 | (dm_disk(md)->first_minor != minor) || | ||
1077 | test_bit(DMF_FREEING, &md->flags))) { | ||
1022 | md = NULL; | 1078 | md = NULL; |
1079 | goto out; | ||
1080 | } | ||
1023 | 1081 | ||
1024 | mutex_unlock(&_minor_lock); | 1082 | out: |
1083 | spin_unlock(&_minor_lock); | ||
1025 | 1084 | ||
1026 | return md; | 1085 | return md; |
1027 | } | 1086 | } |
@@ -1051,12 +1110,23 @@ void dm_get(struct mapped_device *md) | |||
1051 | atomic_inc(&md->holders); | 1110 | atomic_inc(&md->holders); |
1052 | } | 1111 | } |
1053 | 1112 | ||
1113 | const char *dm_device_name(struct mapped_device *md) | ||
1114 | { | ||
1115 | return md->name; | ||
1116 | } | ||
1117 | EXPORT_SYMBOL_GPL(dm_device_name); | ||
1118 | |||
1054 | void dm_put(struct mapped_device *md) | 1119 | void dm_put(struct mapped_device *md) |
1055 | { | 1120 | { |
1056 | struct dm_table *map; | 1121 | struct dm_table *map; |
1057 | 1122 | ||
1058 | if (atomic_dec_and_test(&md->holders)) { | 1123 | BUG_ON(test_bit(DMF_FREEING, &md->flags)); |
1124 | |||
1125 | if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { | ||
1059 | map = dm_get_table(md); | 1126 | map = dm_get_table(md); |
1127 | idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor); | ||
1128 | set_bit(DMF_FREEING, &md->flags); | ||
1129 | spin_unlock(&_minor_lock); | ||
1060 | if (!dm_suspended(md)) { | 1130 | if (!dm_suspended(md)) { |
1061 | dm_table_presuspend_targets(map); | 1131 | dm_table_presuspend_targets(map); |
1062 | dm_table_postsuspend_targets(map); | 1132 | dm_table_postsuspend_targets(map); |