Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include "dm.h" |
| 9 | #include "dm-bio-list.h" |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 10 | #include "dm-uevent.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/blkpg.h> |
| 17 | #include <linux/bio.h> |
| 18 | #include <linux/buffer_head.h> |
| 19 | #include <linux/mempool.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/idr.h> |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 22 | #include <linux/hdreg.h> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 23 | #include <linux/blktrace_api.h> |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 24 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 26 | #define DM_MSG_PREFIX "core" |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static const char *_name = DM_NAME; |
| 29 | |
| 30 | static unsigned int major = 0; |
| 31 | static unsigned int _major = 0; |
| 32 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 33 | static DEFINE_SPINLOCK(_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | /* |
| 35 | * One of these is allocated per bio. |
| 36 | */ |
| 37 | struct dm_io { |
| 38 | struct mapped_device *md; |
| 39 | int error; |
| 40 | struct bio *bio; |
| 41 | atomic_t io_count; |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 42 | unsigned long start_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * One of these is allocated per target within a bio. Hopefully |
| 47 | * this will be simplified out one day. |
| 48 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 49 | struct dm_target_io { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | struct dm_io *io; |
| 51 | struct dm_target *ti; |
| 52 | union map_info info; |
| 53 | }; |
| 54 | |
| 55 | union map_info *dm_get_mapinfo(struct bio *bio) |
| 56 | { |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 57 | if (bio && bio->bi_private) |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 58 | return &((struct dm_target_io *)bio->bi_private)->info; |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 59 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 62 | #define MINOR_ALLOCED ((void *)-1) |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /* |
| 65 | * Bits for the md->flags field. |
| 66 | */ |
| 67 | #define DMF_BLOCK_IO 0 |
| 68 | #define DMF_SUSPENDED 1 |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 69 | #define DMF_FROZEN 2 |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 70 | #define DMF_FREEING 3 |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 71 | #define DMF_DELETING 4 |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 72 | #define DMF_NOFLUSH_SUSPENDING 5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | struct mapped_device { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 75 | struct rw_semaphore io_lock; |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 76 | struct mutex suspend_lock; |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 77 | spinlock_t pushback_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | rwlock_t map_lock; |
| 79 | atomic_t holders; |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 80 | atomic_t open_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | unsigned long flags; |
| 83 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 84 | struct request_queue *queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | struct gendisk *disk; |
Mike Anderson | 7e51f25 | 2006-03-27 01:17:52 -0800 | [diff] [blame] | 86 | char name[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | void *interface_ptr; |
| 89 | |
| 90 | /* |
| 91 | * A list of ios that arrived while we were suspended. |
| 92 | */ |
| 93 | atomic_t pending; |
| 94 | wait_queue_head_t wait; |
Kiyoshi Ueda | 7485936 | 2006-12-08 02:41:02 -0800 | [diff] [blame] | 95 | struct bio_list deferred; |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 96 | struct bio_list pushback; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * The current mapping. |
| 100 | */ |
| 101 | struct dm_table *map; |
| 102 | |
| 103 | /* |
| 104 | * io objects are allocated from here. |
| 105 | */ |
| 106 | mempool_t *io_pool; |
| 107 | mempool_t *tio_pool; |
| 108 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 109 | struct bio_set *bs; |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* |
| 112 | * Event handling. |
| 113 | */ |
| 114 | atomic_t event_nr; |
| 115 | wait_queue_head_t eventq; |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 116 | atomic_t uevent_seq; |
| 117 | struct list_head uevent_list; |
| 118 | spinlock_t uevent_lock; /* Protect access to uevent_list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * freeze/thaw support require holding onto a super block |
| 122 | */ |
| 123 | struct super_block *frozen_sb; |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 124 | struct block_device *suspended_bdev; |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 125 | |
| 126 | /* forced geometry settings */ |
| 127 | struct hd_geometry geometry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | #define MIN_IOS 256 |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 131 | static struct kmem_cache *_io_cache; |
| 132 | static struct kmem_cache *_tio_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | static int __init local_init(void) |
| 135 | { |
| 136 | int r; |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* allocate a slab for the dm_ios */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 139 | _io_cache = KMEM_CACHE(dm_io, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (!_io_cache) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | /* allocate a slab for the target ios */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 144 | _tio_cache = KMEM_CACHE(dm_target_io, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (!_tio_cache) { |
| 146 | kmem_cache_destroy(_io_cache); |
| 147 | return -ENOMEM; |
| 148 | } |
| 149 | |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 150 | r = dm_uevent_init(); |
| 151 | if (r) { |
| 152 | kmem_cache_destroy(_tio_cache); |
| 153 | kmem_cache_destroy(_io_cache); |
| 154 | return r; |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | _major = major; |
| 158 | r = register_blkdev(_major, _name); |
| 159 | if (r < 0) { |
| 160 | kmem_cache_destroy(_tio_cache); |
| 161 | kmem_cache_destroy(_io_cache); |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 162 | dm_uevent_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return r; |
| 164 | } |
| 165 | |
| 166 | if (!_major) |
| 167 | _major = r; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void local_exit(void) |
| 173 | { |
| 174 | kmem_cache_destroy(_tio_cache); |
| 175 | kmem_cache_destroy(_io_cache); |
Akinobu Mita | 00d5940 | 2007-07-17 04:03:46 -0700 | [diff] [blame] | 176 | unregister_blkdev(_major, _name); |
Mike Anderson | 51e5b2b | 2007-10-19 22:48:00 +0100 | [diff] [blame] | 177 | dm_uevent_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | _major = 0; |
| 180 | |
| 181 | DMINFO("cleaned up"); |
| 182 | } |
| 183 | |
Alasdair G Kergon | b9249e5 | 2008-02-08 02:09:51 +0000 | [diff] [blame] | 184 | static int (*_inits[])(void) __initdata = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | local_init, |
| 186 | dm_target_init, |
| 187 | dm_linear_init, |
| 188 | dm_stripe_init, |
| 189 | dm_interface_init, |
| 190 | }; |
| 191 | |
Alasdair G Kergon | b9249e5 | 2008-02-08 02:09:51 +0000 | [diff] [blame] | 192 | static void (*_exits[])(void) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | local_exit, |
| 194 | dm_target_exit, |
| 195 | dm_linear_exit, |
| 196 | dm_stripe_exit, |
| 197 | dm_interface_exit, |
| 198 | }; |
| 199 | |
| 200 | static int __init dm_init(void) |
| 201 | { |
| 202 | const int count = ARRAY_SIZE(_inits); |
| 203 | |
| 204 | int r, i; |
| 205 | |
| 206 | for (i = 0; i < count; i++) { |
| 207 | r = _inits[i](); |
| 208 | if (r) |
| 209 | goto bad; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | |
| 214 | bad: |
| 215 | while (i--) |
| 216 | _exits[i](); |
| 217 | |
| 218 | return r; |
| 219 | } |
| 220 | |
| 221 | static void __exit dm_exit(void) |
| 222 | { |
| 223 | int i = ARRAY_SIZE(_exits); |
| 224 | |
| 225 | while (i--) |
| 226 | _exits[i](); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Block device functions |
| 231 | */ |
| 232 | static int dm_blk_open(struct inode *inode, struct file *file) |
| 233 | { |
| 234 | struct mapped_device *md; |
| 235 | |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 236 | spin_lock(&_minor_lock); |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | md = inode->i_bdev->bd_disk->private_data; |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 239 | if (!md) |
| 240 | goto out; |
| 241 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 242 | if (test_bit(DMF_FREEING, &md->flags) || |
| 243 | test_bit(DMF_DELETING, &md->flags)) { |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 244 | md = NULL; |
| 245 | goto out; |
| 246 | } |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | dm_get(md); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 249 | atomic_inc(&md->open_count); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 250 | |
| 251 | out: |
| 252 | spin_unlock(&_minor_lock); |
| 253 | |
| 254 | return md ? 0 : -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static int dm_blk_close(struct inode *inode, struct file *file) |
| 258 | { |
| 259 | struct mapped_device *md; |
| 260 | |
| 261 | md = inode->i_bdev->bd_disk->private_data; |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 262 | atomic_dec(&md->open_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | dm_put(md); |
| 264 | return 0; |
| 265 | } |
| 266 | |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 267 | int dm_open_count(struct mapped_device *md) |
| 268 | { |
| 269 | return atomic_read(&md->open_count); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Guarantees nothing is using the device before it's deleted. |
| 274 | */ |
| 275 | int dm_lock_for_deletion(struct mapped_device *md) |
| 276 | { |
| 277 | int r = 0; |
| 278 | |
| 279 | spin_lock(&_minor_lock); |
| 280 | |
| 281 | if (dm_open_count(md)) |
| 282 | r = -EBUSY; |
| 283 | else |
| 284 | set_bit(DMF_DELETING, &md->flags); |
| 285 | |
| 286 | spin_unlock(&_minor_lock); |
| 287 | |
| 288 | return r; |
| 289 | } |
| 290 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 291 | static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 292 | { |
| 293 | struct mapped_device *md = bdev->bd_disk->private_data; |
| 294 | |
| 295 | return dm_get_geometry(md, geo); |
| 296 | } |
| 297 | |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 298 | static int dm_blk_ioctl(struct inode *inode, struct file *file, |
| 299 | unsigned int cmd, unsigned long arg) |
| 300 | { |
| 301 | struct mapped_device *md; |
| 302 | struct dm_table *map; |
| 303 | struct dm_target *tgt; |
| 304 | int r = -ENOTTY; |
| 305 | |
| 306 | /* We don't really need this lock, but we do need 'inode'. */ |
| 307 | unlock_kernel(); |
| 308 | |
| 309 | md = inode->i_bdev->bd_disk->private_data; |
| 310 | |
| 311 | map = dm_get_table(md); |
| 312 | |
| 313 | if (!map || !dm_table_get_size(map)) |
| 314 | goto out; |
| 315 | |
| 316 | /* We only support devices that have a single target */ |
| 317 | if (dm_table_get_num_targets(map) != 1) |
| 318 | goto out; |
| 319 | |
| 320 | tgt = dm_table_get_target(map, 0); |
| 321 | |
| 322 | if (dm_suspended(md)) { |
| 323 | r = -EAGAIN; |
| 324 | goto out; |
| 325 | } |
| 326 | |
| 327 | if (tgt->type->ioctl) |
| 328 | r = tgt->type->ioctl(tgt, inode, file, cmd, arg); |
| 329 | |
| 330 | out: |
| 331 | dm_table_put(map); |
| 332 | |
| 333 | lock_kernel(); |
| 334 | return r; |
| 335 | } |
| 336 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 337 | static struct dm_io *alloc_io(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
| 339 | return mempool_alloc(md->io_pool, GFP_NOIO); |
| 340 | } |
| 341 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 342 | static void free_io(struct mapped_device *md, struct dm_io *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | mempool_free(io, md->io_pool); |
| 345 | } |
| 346 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 347 | static struct dm_target_io *alloc_tio(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | return mempool_alloc(md->tio_pool, GFP_NOIO); |
| 350 | } |
| 351 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 352 | static void free_tio(struct mapped_device *md, struct dm_target_io *tio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | mempool_free(tio, md->tio_pool); |
| 355 | } |
| 356 | |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 357 | static void start_io_acct(struct dm_io *io) |
| 358 | { |
| 359 | struct mapped_device *md = io->md; |
| 360 | |
| 361 | io->start_time = jiffies; |
| 362 | |
| 363 | preempt_disable(); |
| 364 | disk_round_stats(dm_disk(md)); |
| 365 | preempt_enable(); |
| 366 | dm_disk(md)->in_flight = atomic_inc_return(&md->pending); |
| 367 | } |
| 368 | |
| 369 | static int end_io_acct(struct dm_io *io) |
| 370 | { |
| 371 | struct mapped_device *md = io->md; |
| 372 | struct bio *bio = io->bio; |
| 373 | unsigned long duration = jiffies - io->start_time; |
| 374 | int pending; |
| 375 | int rw = bio_data_dir(bio); |
| 376 | |
| 377 | preempt_disable(); |
| 378 | disk_round_stats(dm_disk(md)); |
| 379 | preempt_enable(); |
| 380 | dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending); |
| 381 | |
| 382 | disk_stat_add(dm_disk(md), ticks[rw], duration); |
| 383 | |
| 384 | return !pending; |
| 385 | } |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | /* |
| 388 | * Add the bio to the list of deferred io. |
| 389 | */ |
| 390 | static int queue_io(struct mapped_device *md, struct bio *bio) |
| 391 | { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 392 | down_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | if (!test_bit(DMF_BLOCK_IO, &md->flags)) { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 395 | up_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return 1; |
| 397 | } |
| 398 | |
| 399 | bio_list_add(&md->deferred, bio); |
| 400 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 401 | up_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return 0; /* deferred successfully */ |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Everyone (including functions in this file), should use this |
| 407 | * function to access the md->map field, and make sure they call |
| 408 | * dm_table_put() when finished. |
| 409 | */ |
| 410 | struct dm_table *dm_get_table(struct mapped_device *md) |
| 411 | { |
| 412 | struct dm_table *t; |
| 413 | |
| 414 | read_lock(&md->map_lock); |
| 415 | t = md->map; |
| 416 | if (t) |
| 417 | dm_table_get(t); |
| 418 | read_unlock(&md->map_lock); |
| 419 | |
| 420 | return t; |
| 421 | } |
| 422 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 423 | /* |
| 424 | * Get the geometry associated with a dm device |
| 425 | */ |
| 426 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) |
| 427 | { |
| 428 | *geo = md->geometry; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Set the geometry of a device. |
| 435 | */ |
| 436 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) |
| 437 | { |
| 438 | sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; |
| 439 | |
| 440 | if (geo->start > sz) { |
| 441 | DMWARN("Start sector is beyond the geometry limits."); |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | |
| 445 | md->geometry = *geo; |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | /*----------------------------------------------------------------- |
| 451 | * CRUD START: |
| 452 | * A more elegant soln is in the works that uses the queue |
| 453 | * merge fn, unfortunately there are a couple of changes to |
| 454 | * the block layer that I want to make for this. So in the |
| 455 | * interests of getting something for people to use I give |
| 456 | * you this clearly demarcated crap. |
| 457 | *---------------------------------------------------------------*/ |
| 458 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 459 | static int __noflush_suspending(struct mapped_device *md) |
| 460 | { |
| 461 | return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
| 462 | } |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | /* |
| 465 | * Decrements the number of outstanding ios that a bio has been |
| 466 | * cloned into, completing the original io if necc. |
| 467 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 468 | static void dec_pending(struct dm_io *io, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 470 | unsigned long flags; |
| 471 | |
| 472 | /* Push-back supersedes any I/O errors */ |
| 473 | if (error && !(io->error > 0 && __noflush_suspending(io->md))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | io->error = error; |
| 475 | |
| 476 | if (atomic_dec_and_test(&io->io_count)) { |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 477 | if (io->error == DM_ENDIO_REQUEUE) { |
| 478 | /* |
| 479 | * Target requested pushing back the I/O. |
| 480 | * This must be handled before the sleeper on |
| 481 | * suspend queue merges the pushback list. |
| 482 | */ |
| 483 | spin_lock_irqsave(&io->md->pushback_lock, flags); |
| 484 | if (__noflush_suspending(io->md)) |
| 485 | bio_list_add(&io->md->pushback, io->bio); |
| 486 | else |
| 487 | /* noflush suspend was interrupted. */ |
| 488 | io->error = -EIO; |
| 489 | spin_unlock_irqrestore(&io->md->pushback_lock, flags); |
| 490 | } |
| 491 | |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 492 | if (end_io_acct(io)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | /* nudge anyone waiting on suspend queue */ |
| 494 | wake_up(&io->md->wait); |
| 495 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 496 | if (io->error != DM_ENDIO_REQUEUE) { |
| 497 | blk_add_trace_bio(io->md->queue, io->bio, |
| 498 | BLK_TA_COMPLETE); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 499 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 500 | bio_endio(io->bio, io->error); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | free_io(io->md, io); |
| 504 | } |
| 505 | } |
| 506 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 507 | static void clone_endio(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
| 509 | int r = 0; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 510 | struct dm_target_io *tio = bio->bi_private; |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 511 | struct mapped_device *md = tio->io->md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | dm_endio_fn endio = tio->ti->type->end_io; |
| 513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | if (!bio_flagged(bio, BIO_UPTODATE) && !error) |
| 515 | error = -EIO; |
| 516 | |
| 517 | if (endio) { |
| 518 | r = endio(tio->ti, bio, error, &tio->info); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 519 | if (r < 0 || r == DM_ENDIO_REQUEUE) |
| 520 | /* |
| 521 | * error and requeue request are handled |
| 522 | * in dec_pending(). |
| 523 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | error = r; |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 525 | else if (r == DM_ENDIO_INCOMPLETE) |
| 526 | /* The target will handle the io */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 527 | return; |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 528 | else if (r) { |
| 529 | DMWARN("unimplemented target endio return value: %d", r); |
| 530 | BUG(); |
| 531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 534 | dec_pending(tio->io, error); |
| 535 | |
| 536 | /* |
| 537 | * Store md for cleanup instead of tio which is about to get freed. |
| 538 | */ |
| 539 | bio->bi_private = md->bs; |
| 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | bio_put(bio); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 542 | free_tio(md, tio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static sector_t max_io_len(struct mapped_device *md, |
| 546 | sector_t sector, struct dm_target *ti) |
| 547 | { |
| 548 | sector_t offset = sector - ti->begin; |
| 549 | sector_t len = ti->len - offset; |
| 550 | |
| 551 | /* |
| 552 | * Does the target need to split even further ? |
| 553 | */ |
| 554 | if (ti->split_io) { |
| 555 | sector_t boundary; |
| 556 | boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) |
| 557 | - offset; |
| 558 | if (len > boundary) |
| 559 | len = boundary; |
| 560 | } |
| 561 | |
| 562 | return len; |
| 563 | } |
| 564 | |
| 565 | static void __map_bio(struct dm_target *ti, struct bio *clone, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 566 | struct dm_target_io *tio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | { |
| 568 | int r; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 569 | sector_t sector; |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 570 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
| 572 | /* |
| 573 | * Sanity checks. |
| 574 | */ |
| 575 | BUG_ON(!clone->bi_size); |
| 576 | |
| 577 | clone->bi_end_io = clone_endio; |
| 578 | clone->bi_private = tio; |
| 579 | |
| 580 | /* |
| 581 | * Map the clone. If r == 0 we don't need to do |
| 582 | * anything, the target has assumed ownership of |
| 583 | * this io. |
| 584 | */ |
| 585 | atomic_inc(&tio->io->io_count); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 586 | sector = clone->bi_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | r = ti->type->map(ti, clone, &tio->info); |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 588 | if (r == DM_MAPIO_REMAPPED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | /* the bio has been remapped so dispatch it */ |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 590 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 591 | blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone, |
Alan D. Brunelle | c7149d6 | 2007-08-07 15:30:23 +0200 | [diff] [blame] | 592 | tio->io->bio->bi_bdev->bd_dev, |
| 593 | clone->bi_sector, sector); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | generic_make_request(clone); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 596 | } else if (r < 0 || r == DM_MAPIO_REQUEUE) { |
| 597 | /* error the io and bail out, or requeue it if needed */ |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 598 | md = tio->io->md; |
| 599 | dec_pending(tio->io, r); |
| 600 | /* |
| 601 | * Store bio_set for cleanup. |
| 602 | */ |
| 603 | clone->bi_private = md->bs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | bio_put(clone); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 605 | free_tio(md, tio); |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 606 | } else if (r) { |
| 607 | DMWARN("unimplemented target map return value: %d", r); |
| 608 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | struct clone_info { |
| 613 | struct mapped_device *md; |
| 614 | struct dm_table *map; |
| 615 | struct bio *bio; |
| 616 | struct dm_io *io; |
| 617 | sector_t sector; |
| 618 | sector_t sector_count; |
| 619 | unsigned short idx; |
| 620 | }; |
| 621 | |
Peter Osterlund | 3676347 | 2005-09-06 15:16:42 -0700 | [diff] [blame] | 622 | static void dm_bio_destructor(struct bio *bio) |
| 623 | { |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 624 | struct bio_set *bs = bio->bi_private; |
| 625 | |
| 626 | bio_free(bio, bs); |
Peter Osterlund | 3676347 | 2005-09-06 15:16:42 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | /* |
| 630 | * Creates a little bio that is just does part of a bvec. |
| 631 | */ |
| 632 | static struct bio *split_bvec(struct bio *bio, sector_t sector, |
| 633 | unsigned short idx, unsigned int offset, |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 634 | unsigned int len, struct bio_set *bs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
| 636 | struct bio *clone; |
| 637 | struct bio_vec *bv = bio->bi_io_vec + idx; |
| 638 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 639 | clone = bio_alloc_bioset(GFP_NOIO, 1, bs); |
Peter Osterlund | 3676347 | 2005-09-06 15:16:42 -0700 | [diff] [blame] | 640 | clone->bi_destructor = dm_bio_destructor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | *clone->bi_io_vec = *bv; |
| 642 | |
| 643 | clone->bi_sector = sector; |
| 644 | clone->bi_bdev = bio->bi_bdev; |
| 645 | clone->bi_rw = bio->bi_rw; |
| 646 | clone->bi_vcnt = 1; |
| 647 | clone->bi_size = to_bytes(len); |
| 648 | clone->bi_io_vec->bv_offset = offset; |
| 649 | clone->bi_io_vec->bv_len = clone->bi_size; |
| 650 | |
| 651 | return clone; |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * Creates a bio that consists of range of complete bvecs. |
| 656 | */ |
| 657 | static struct bio *clone_bio(struct bio *bio, sector_t sector, |
| 658 | unsigned short idx, unsigned short bv_count, |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 659 | unsigned int len, struct bio_set *bs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
| 661 | struct bio *clone; |
| 662 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 663 | clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); |
| 664 | __bio_clone(clone, bio); |
| 665 | clone->bi_destructor = dm_bio_destructor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | clone->bi_sector = sector; |
| 667 | clone->bi_idx = idx; |
| 668 | clone->bi_vcnt = idx + bv_count; |
| 669 | clone->bi_size = to_bytes(len); |
| 670 | clone->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 671 | |
| 672 | return clone; |
| 673 | } |
| 674 | |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 675 | static int __clone_and_map(struct clone_info *ci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
| 677 | struct bio *clone, *bio = ci->bio; |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 678 | struct dm_target *ti; |
| 679 | sector_t len = 0, max; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 680 | struct dm_target_io *tio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 682 | ti = dm_table_find_target(ci->map, ci->sector); |
| 683 | if (!dm_target_is_valid(ti)) |
| 684 | return -EIO; |
| 685 | |
| 686 | max = max_io_len(ci->md, ci->sector, ti); |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | /* |
| 689 | * Allocate a target io object. |
| 690 | */ |
| 691 | tio = alloc_tio(ci->md); |
| 692 | tio->io = ci->io; |
| 693 | tio->ti = ti; |
| 694 | memset(&tio->info, 0, sizeof(tio->info)); |
| 695 | |
| 696 | if (ci->sector_count <= max) { |
| 697 | /* |
| 698 | * Optimise for the simple case where we can do all of |
| 699 | * the remaining io with a single clone. |
| 700 | */ |
| 701 | clone = clone_bio(bio, ci->sector, ci->idx, |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 702 | bio->bi_vcnt - ci->idx, ci->sector_count, |
| 703 | ci->md->bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | __map_bio(ti, clone, tio); |
| 705 | ci->sector_count = 0; |
| 706 | |
| 707 | } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { |
| 708 | /* |
| 709 | * There are some bvecs that don't span targets. |
| 710 | * Do as many of these as possible. |
| 711 | */ |
| 712 | int i; |
| 713 | sector_t remaining = max; |
| 714 | sector_t bv_len; |
| 715 | |
| 716 | for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { |
| 717 | bv_len = to_sector(bio->bi_io_vec[i].bv_len); |
| 718 | |
| 719 | if (bv_len > remaining) |
| 720 | break; |
| 721 | |
| 722 | remaining -= bv_len; |
| 723 | len += bv_len; |
| 724 | } |
| 725 | |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 726 | clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, |
| 727 | ci->md->bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | __map_bio(ti, clone, tio); |
| 729 | |
| 730 | ci->sector += len; |
| 731 | ci->sector_count -= len; |
| 732 | ci->idx = i; |
| 733 | |
| 734 | } else { |
| 735 | /* |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 736 | * Handle a bvec that must be split between two or more targets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | */ |
| 738 | struct bio_vec *bv = bio->bi_io_vec + ci->idx; |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 739 | sector_t remaining = to_sector(bv->bv_len); |
| 740 | unsigned int offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 742 | do { |
| 743 | if (offset) { |
| 744 | ti = dm_table_find_target(ci->map, ci->sector); |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 745 | if (!dm_target_is_valid(ti)) |
| 746 | return -EIO; |
| 747 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 748 | max = max_io_len(ci->md, ci->sector, ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 750 | tio = alloc_tio(ci->md); |
| 751 | tio->io = ci->io; |
| 752 | tio->ti = ti; |
| 753 | memset(&tio->info, 0, sizeof(tio->info)); |
| 754 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 756 | len = min(remaining, max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 758 | clone = split_bvec(bio, ci->sector, ci->idx, |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 759 | bv->bv_offset + offset, len, |
| 760 | ci->md->bs); |
Alasdair G Kergon | d2044a9 | 2006-03-22 00:07:42 -0800 | [diff] [blame] | 761 | |
| 762 | __map_bio(ti, clone, tio); |
| 763 | |
| 764 | ci->sector += len; |
| 765 | ci->sector_count -= len; |
| 766 | offset += to_bytes(len); |
| 767 | } while (remaining -= len); |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | ci->idx++; |
| 770 | } |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 771 | |
| 772 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Split the bio into several clones. |
| 777 | */ |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 778 | static int __split_bio(struct mapped_device *md, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
| 780 | struct clone_info ci; |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 781 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | ci.map = dm_get_table(md); |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 784 | if (unlikely(!ci.map)) |
| 785 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
| 787 | ci.md = md; |
| 788 | ci.bio = bio; |
| 789 | ci.io = alloc_io(md); |
| 790 | ci.io->error = 0; |
| 791 | atomic_set(&ci.io->io_count, 1); |
| 792 | ci.io->bio = bio; |
| 793 | ci.io->md = md; |
| 794 | ci.sector = bio->bi_sector; |
| 795 | ci.sector_count = bio_sectors(bio); |
| 796 | ci.idx = bio->bi_idx; |
| 797 | |
Jun'ichi "Nick" Nomura | 3eaf840 | 2006-02-01 03:04:53 -0800 | [diff] [blame] | 798 | start_io_acct(ci.io); |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 799 | while (ci.sector_count && !error) |
| 800 | error = __clone_and_map(&ci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 802 | /* drop the extra reference count */ |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 803 | dec_pending(ci.io, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | dm_table_put(ci.map); |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 805 | |
| 806 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
| 808 | /*----------------------------------------------------------------- |
| 809 | * CRUD END |
| 810 | *---------------------------------------------------------------*/ |
| 811 | |
| 812 | /* |
| 813 | * The request function that just remaps the bio built up by |
| 814 | * dm_merge_bvec. |
| 815 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 816 | static int dm_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 818 | int r = -EIO; |
Kevin Corry | 12f03a4 | 2006-02-01 03:04:52 -0800 | [diff] [blame] | 819 | int rw = bio_data_dir(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | struct mapped_device *md = q->queuedata; |
| 821 | |
Stefan Bader | 07a83c4 | 2007-07-12 17:28:33 +0100 | [diff] [blame] | 822 | /* |
| 823 | * There is no use in forwarding any barrier request since we can't |
| 824 | * guarantee it is (or can be) handled by the targets correctly. |
| 825 | */ |
| 826 | if (unlikely(bio_barrier(bio))) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 827 | bio_endio(bio, -EOPNOTSUPP); |
Stefan Bader | 07a83c4 | 2007-07-12 17:28:33 +0100 | [diff] [blame] | 828 | return 0; |
| 829 | } |
| 830 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 831 | down_read(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
Kevin Corry | 12f03a4 | 2006-02-01 03:04:52 -0800 | [diff] [blame] | 833 | disk_stat_inc(dm_disk(md), ios[rw]); |
| 834 | disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio)); |
| 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | /* |
| 837 | * If we're suspended we have to queue |
| 838 | * this io for later. |
| 839 | */ |
| 840 | while (test_bit(DMF_BLOCK_IO, &md->flags)) { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 841 | up_read(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 843 | if (bio_rw(bio) != READA) |
| 844 | r = queue_io(md, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 846 | if (r <= 0) |
| 847 | goto out_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
| 849 | /* |
| 850 | * We're in a while loop, because someone could suspend |
| 851 | * before we get to the following read lock. |
| 852 | */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 853 | down_read(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 856 | r = __split_bio(md, bio); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 857 | up_read(&md->io_lock); |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 858 | |
| 859 | out_req: |
| 860 | if (r < 0) |
| 861 | bio_io_error(bio); |
| 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 866 | static void dm_unplug_all(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
| 868 | struct mapped_device *md = q->queuedata; |
| 869 | struct dm_table *map = dm_get_table(md); |
| 870 | |
| 871 | if (map) { |
| 872 | dm_table_unplug_all(map); |
| 873 | dm_table_put(map); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | static int dm_any_congested(void *congested_data, int bdi_bits) |
| 878 | { |
| 879 | int r; |
| 880 | struct mapped_device *md = (struct mapped_device *) congested_data; |
| 881 | struct dm_table *map = dm_get_table(md); |
| 882 | |
| 883 | if (!map || test_bit(DMF_BLOCK_IO, &md->flags)) |
| 884 | r = bdi_bits; |
| 885 | else |
| 886 | r = dm_table_any_congested(map, bdi_bits); |
| 887 | |
| 888 | dm_table_put(map); |
| 889 | return r; |
| 890 | } |
| 891 | |
| 892 | /*----------------------------------------------------------------- |
| 893 | * An IDR is used to keep track of allocated minor numbers. |
| 894 | *---------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | static DEFINE_IDR(_minor_idr); |
| 896 | |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 897 | static void free_minor(int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | { |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 899 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | idr_remove(&_minor_idr, minor); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 901 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* |
| 905 | * See if the device with a specific minor # is free. |
| 906 | */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 907 | static int specific_minor(struct mapped_device *md, int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | { |
| 909 | int r, m; |
| 910 | |
| 911 | if (minor >= (1 << MINORBITS)) |
| 912 | return -EINVAL; |
| 913 | |
Jeff Mahoney | 62f75c2 | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 914 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); |
| 915 | if (!r) |
| 916 | return -ENOMEM; |
| 917 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 918 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
| 920 | if (idr_find(&_minor_idr, minor)) { |
| 921 | r = -EBUSY; |
| 922 | goto out; |
| 923 | } |
| 924 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 925 | r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); |
Jeff Mahoney | 62f75c2 | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 926 | if (r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
| 929 | if (m != minor) { |
| 930 | idr_remove(&_minor_idr, m); |
| 931 | r = -EBUSY; |
| 932 | goto out; |
| 933 | } |
| 934 | |
| 935 | out: |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 936 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | return r; |
| 938 | } |
| 939 | |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 940 | static int next_free_minor(struct mapped_device *md, int *minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | { |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 942 | int r, m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | r = idr_pre_get(&_minor_idr, GFP_KERNEL); |
Jeff Mahoney | 62f75c2 | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 945 | if (!r) |
| 946 | return -ENOMEM; |
| 947 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 948 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 950 | r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | if (r) { |
| 952 | goto out; |
| 953 | } |
| 954 | |
| 955 | if (m >= (1 << MINORBITS)) { |
| 956 | idr_remove(&_minor_idr, m); |
| 957 | r = -ENOSPC; |
| 958 | goto out; |
| 959 | } |
| 960 | |
| 961 | *minor = m; |
| 962 | |
| 963 | out: |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 964 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | return r; |
| 966 | } |
| 967 | |
| 968 | static struct block_device_operations dm_blk_dops; |
| 969 | |
| 970 | /* |
| 971 | * Allocate and initialise a blank device with a given minor. |
| 972 | */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 973 | static struct mapped_device *alloc_dev(int minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | { |
| 975 | int r; |
| 976 | struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 977 | void *old_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | |
| 979 | if (!md) { |
| 980 | DMWARN("unable to allocate device, out of memory."); |
| 981 | return NULL; |
| 982 | } |
| 983 | |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 984 | if (!try_module_get(THIS_MODULE)) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 985 | goto bad_module_get; |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | /* get a minor number for the dev */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 988 | if (minor == DM_ANY_MINOR) |
| 989 | r = next_free_minor(md, &minor); |
| 990 | else |
| 991 | r = specific_minor(md, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | if (r < 0) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 993 | goto bad_minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | memset(md, 0, sizeof(*md)); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 996 | init_rwsem(&md->io_lock); |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 997 | mutex_init(&md->suspend_lock); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 998 | spin_lock_init(&md->pushback_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | rwlock_init(&md->map_lock); |
| 1000 | atomic_set(&md->holders, 1); |
Alasdair G Kergon | 5c6bd75 | 2006-06-26 00:27:34 -0700 | [diff] [blame] | 1001 | atomic_set(&md->open_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | atomic_set(&md->event_nr, 0); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1003 | atomic_set(&md->uevent_seq, 0); |
| 1004 | INIT_LIST_HEAD(&md->uevent_list); |
| 1005 | spin_lock_init(&md->uevent_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
| 1007 | md->queue = blk_alloc_queue(GFP_KERNEL); |
| 1008 | if (!md->queue) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1009 | goto bad_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | md->queue->queuedata = md; |
| 1012 | md->queue->backing_dev_info.congested_fn = dm_any_congested; |
| 1013 | md->queue->backing_dev_info.congested_data = md; |
| 1014 | blk_queue_make_request(md->queue, dm_request); |
Jens Axboe | daef265 | 2006-01-10 10:48:02 +0100 | [diff] [blame] | 1015 | blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | md->queue->unplug_fn = dm_unplug_all; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1018 | md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache); |
Kiyoshi Ueda | 7485936 | 2006-12-08 02:41:02 -0800 | [diff] [blame] | 1019 | if (!md->io_pool) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1020 | goto bad_io_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1022 | md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | if (!md->tio_pool) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1024 | goto bad_tio_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | |
Jens Axboe | 5972511 | 2007-04-02 10:06:42 +0200 | [diff] [blame] | 1026 | md->bs = bioset_create(16, 16); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1027 | if (!md->bs) |
| 1028 | goto bad_no_bioset; |
| 1029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | md->disk = alloc_disk(1); |
| 1031 | if (!md->disk) |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1032 | goto bad_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Jeff Mahoney | f0b0411 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1034 | atomic_set(&md->pending, 0); |
| 1035 | init_waitqueue_head(&md->wait); |
| 1036 | init_waitqueue_head(&md->eventq); |
| 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | md->disk->major = _major; |
| 1039 | md->disk->first_minor = minor; |
| 1040 | md->disk->fops = &dm_blk_dops; |
| 1041 | md->disk->queue = md->queue; |
| 1042 | md->disk->private_data = md; |
| 1043 | sprintf(md->disk->disk_name, "dm-%d", minor); |
| 1044 | add_disk(md->disk); |
Mike Anderson | 7e51f25 | 2006-03-27 01:17:52 -0800 | [diff] [blame] | 1045 | format_dev_t(md->name, MKDEV(_major, minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1047 | /* Populate the mapping, nobody knows we exist yet */ |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1048 | spin_lock(&_minor_lock); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1049 | old_md = idr_replace(&_minor_idr, md, minor); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1050 | spin_unlock(&_minor_lock); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1051 | |
| 1052 | BUG_ON(old_md != MINOR_ALLOCED); |
| 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | return md; |
| 1055 | |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1056 | bad_disk: |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1057 | bioset_free(md->bs); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1058 | bad_no_bioset: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | mempool_destroy(md->tio_pool); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1060 | bad_tio_pool: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | mempool_destroy(md->io_pool); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1062 | bad_io_pool: |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 1063 | blk_cleanup_queue(md->queue); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1064 | bad_queue: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | free_minor(minor); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1066 | bad_minor: |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1067 | module_put(THIS_MODULE); |
Milan Broz | 6ed7ade | 2008-02-08 02:10:19 +0000 | [diff] [blame] | 1068 | bad_module_get: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | kfree(md); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | |
Jun'ichi Nomura | ae9da83 | 2007-10-19 22:38:43 +0100 | [diff] [blame] | 1073 | static void unlock_fs(struct mapped_device *md); |
| 1074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | static void free_dev(struct mapped_device *md) |
| 1076 | { |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1077 | int minor = md->disk->first_minor; |
Jun'ichi Nomura | 63d94e4 | 2006-02-24 13:04:25 -0800 | [diff] [blame] | 1078 | |
Jun'ichi Nomura | d9dde59 | 2006-02-24 13:04:24 -0800 | [diff] [blame] | 1079 | if (md->suspended_bdev) { |
Jun'ichi Nomura | ae9da83 | 2007-10-19 22:38:43 +0100 | [diff] [blame] | 1080 | unlock_fs(md); |
Jun'ichi Nomura | d9dde59 | 2006-02-24 13:04:24 -0800 | [diff] [blame] | 1081 | bdput(md->suspended_bdev); |
| 1082 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | mempool_destroy(md->tio_pool); |
| 1084 | mempool_destroy(md->io_pool); |
Stefan Bader | 9faf400 | 2006-10-03 01:15:41 -0700 | [diff] [blame] | 1085 | bioset_free(md->bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | del_gendisk(md->disk); |
Jun'ichi Nomura | 63d94e4 | 2006-02-24 13:04:25 -0800 | [diff] [blame] | 1087 | free_minor(minor); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 1088 | |
| 1089 | spin_lock(&_minor_lock); |
| 1090 | md->disk->private_data = NULL; |
| 1091 | spin_unlock(&_minor_lock); |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | put_disk(md->disk); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 1094 | blk_cleanup_queue(md->queue); |
Jeff Mahoney | 10da4f7 | 2006-06-26 00:27:25 -0700 | [diff] [blame] | 1095 | module_put(THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | kfree(md); |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * Bind a table to the device. |
| 1101 | */ |
| 1102 | static void event_callback(void *context) |
| 1103 | { |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1104 | unsigned long flags; |
| 1105 | LIST_HEAD(uevents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | struct mapped_device *md = (struct mapped_device *) context; |
| 1107 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1108 | spin_lock_irqsave(&md->uevent_lock, flags); |
| 1109 | list_splice_init(&md->uevent_list, &uevents); |
| 1110 | spin_unlock_irqrestore(&md->uevent_lock, flags); |
| 1111 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1112 | dm_send_uevents(&uevents, &md->disk->dev.kobj); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | atomic_inc(&md->event_nr); |
| 1115 | wake_up(&md->eventq); |
| 1116 | } |
| 1117 | |
Alasdair G Kergon | 4e90188be | 2005-07-28 21:15:59 -0700 | [diff] [blame] | 1118 | static void __set_size(struct mapped_device *md, sector_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | { |
Alasdair G Kergon | 4e90188be | 2005-07-28 21:15:59 -0700 | [diff] [blame] | 1120 | set_capacity(md->disk, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1122 | mutex_lock(&md->suspended_bdev->bd_inode->i_mutex); |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1123 | i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1124 | mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static int __bind(struct mapped_device *md, struct dm_table *t) |
| 1128 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1129 | struct request_queue *q = md->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | sector_t size; |
| 1131 | |
| 1132 | size = dm_table_get_size(t); |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1133 | |
| 1134 | /* |
| 1135 | * Wipe any geometry if the size of the table changed. |
| 1136 | */ |
| 1137 | if (size != get_capacity(md->disk)) |
| 1138 | memset(&md->geometry, 0, sizeof(md->geometry)); |
| 1139 | |
Jun'ichi Nomura | bfa152f | 2007-01-26 00:57:07 -0800 | [diff] [blame] | 1140 | if (md->suspended_bdev) |
| 1141 | __set_size(md, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | if (size == 0) |
| 1143 | return 0; |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | dm_table_get(t); |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1146 | dm_table_event_callback(t, event_callback, md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1147 | |
| 1148 | write_lock(&md->map_lock); |
| 1149 | md->map = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | dm_table_set_restrictions(t, q); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1151 | write_unlock(&md->map_lock); |
| 1152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | static void __unbind(struct mapped_device *md) |
| 1157 | { |
| 1158 | struct dm_table *map = md->map; |
| 1159 | |
| 1160 | if (!map) |
| 1161 | return; |
| 1162 | |
| 1163 | dm_table_event_callback(map, NULL, NULL); |
| 1164 | write_lock(&md->map_lock); |
| 1165 | md->map = NULL; |
| 1166 | write_unlock(&md->map_lock); |
| 1167 | dm_table_put(map); |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * Constructor for a new device. |
| 1172 | */ |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1173 | int dm_create(int minor, struct mapped_device **result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | { |
| 1175 | struct mapped_device *md; |
| 1176 | |
Alasdair G Kergon | 2b06cff | 2006-06-26 00:27:32 -0700 | [diff] [blame] | 1177 | md = alloc_dev(minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | if (!md) |
| 1179 | return -ENXIO; |
| 1180 | |
| 1181 | *result = md; |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 1185 | static struct mapped_device *dm_find_md(dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | { |
| 1187 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | unsigned minor = MINOR(dev); |
| 1189 | |
| 1190 | if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) |
| 1191 | return NULL; |
| 1192 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1193 | spin_lock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
| 1195 | md = idr_find(&_minor_idr, minor); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 1196 | if (md && (md == MINOR_ALLOCED || |
| 1197 | (dm_disk(md)->first_minor != minor) || |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 1198 | test_bit(DMF_FREEING, &md->flags))) { |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 1199 | md = NULL; |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 1200 | goto out; |
| 1201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 1203 | out: |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1204 | spin_unlock(&_minor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 1206 | return md; |
| 1207 | } |
| 1208 | |
David Teigland | d229a95 | 2006-01-06 00:20:01 -0800 | [diff] [blame] | 1209 | struct mapped_device *dm_get_md(dev_t dev) |
| 1210 | { |
| 1211 | struct mapped_device *md = dm_find_md(dev); |
| 1212 | |
| 1213 | if (md) |
| 1214 | dm_get(md); |
| 1215 | |
| 1216 | return md; |
| 1217 | } |
| 1218 | |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 1219 | void *dm_get_mdptr(struct mapped_device *md) |
David Teigland | 637842c | 2006-01-06 00:20:00 -0800 | [diff] [blame] | 1220 | { |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 1221 | return md->interface_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | void dm_set_mdptr(struct mapped_device *md, void *ptr) |
| 1225 | { |
| 1226 | md->interface_ptr = ptr; |
| 1227 | } |
| 1228 | |
| 1229 | void dm_get(struct mapped_device *md) |
| 1230 | { |
| 1231 | atomic_inc(&md->holders); |
| 1232 | } |
| 1233 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1234 | const char *dm_device_name(struct mapped_device *md) |
| 1235 | { |
| 1236 | return md->name; |
| 1237 | } |
| 1238 | EXPORT_SYMBOL_GPL(dm_device_name); |
| 1239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | void dm_put(struct mapped_device *md) |
| 1241 | { |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1242 | struct dm_table *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 1244 | BUG_ON(test_bit(DMF_FREEING, &md->flags)); |
| 1245 | |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1246 | if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1247 | map = dm_get_table(md); |
Jeff Mahoney | ba61fdd | 2006-06-26 00:27:21 -0700 | [diff] [blame] | 1248 | idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor); |
Jeff Mahoney | fba9f90 | 2006-06-26 00:27:23 -0700 | [diff] [blame] | 1249 | set_bit(DMF_FREEING, &md->flags); |
Jeff Mahoney | f32c10b | 2006-06-26 00:27:22 -0700 | [diff] [blame] | 1250 | spin_unlock(&_minor_lock); |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1251 | if (!dm_suspended(md)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | dm_table_presuspend_targets(map); |
| 1253 | dm_table_postsuspend_targets(map); |
| 1254 | } |
| 1255 | __unbind(md); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1256 | dm_table_put(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | free_dev(md); |
| 1258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | } |
Edward Goggin | 79eb885 | 2007-05-09 02:32:56 -0700 | [diff] [blame] | 1260 | EXPORT_SYMBOL_GPL(dm_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
| 1262 | /* |
| 1263 | * Process the deferred bios |
| 1264 | */ |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1265 | static void __flush_deferred_io(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | { |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1267 | struct bio *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1269 | while ((c = bio_list_pop(&md->deferred))) { |
Milan Broz | 9e4e5f8 | 2007-10-19 22:38:53 +0100 | [diff] [blame] | 1270 | if (__split_bio(md, c)) |
| 1271 | bio_io_error(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | } |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 1273 | |
| 1274 | clear_bit(DMF_BLOCK_IO, &md->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1277 | static void __merge_pushback_list(struct mapped_device *md) |
| 1278 | { |
| 1279 | unsigned long flags; |
| 1280 | |
| 1281 | spin_lock_irqsave(&md->pushback_lock, flags); |
| 1282 | clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
| 1283 | bio_list_merge_head(&md->deferred, &md->pushback); |
| 1284 | bio_list_init(&md->pushback); |
| 1285 | spin_unlock_irqrestore(&md->pushback_lock, flags); |
| 1286 | } |
| 1287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | /* |
| 1289 | * Swap in a new table (destroying old one). |
| 1290 | */ |
| 1291 | int dm_swap_table(struct mapped_device *md, struct dm_table *table) |
| 1292 | { |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 1293 | int r = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 1295 | mutex_lock(&md->suspend_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | /* device must be suspended */ |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1298 | if (!dm_suspended(md)) |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 1299 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Jun'ichi Nomura | bfa152f | 2007-01-26 00:57:07 -0800 | [diff] [blame] | 1301 | /* without bdev, the device size cannot be changed */ |
| 1302 | if (!md->suspended_bdev) |
| 1303 | if (get_capacity(md->disk) != dm_table_get_size(table)) |
| 1304 | goto out; |
| 1305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | __unbind(md); |
| 1307 | r = __bind(md, table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 1309 | out: |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 1310 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | 93c534a | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 1311 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * Functions to lock and unlock any filesystem running on the |
| 1316 | * device. |
| 1317 | */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1318 | static int lock_fs(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | { |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1320 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
| 1322 | WARN_ON(md->frozen_sb); |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 1323 | |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1324 | md->frozen_sb = freeze_bdev(md->suspended_bdev); |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 1325 | if (IS_ERR(md->frozen_sb)) { |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1326 | r = PTR_ERR(md->frozen_sb); |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1327 | md->frozen_sb = NULL; |
| 1328 | return r; |
Alasdair G Kergon | dfbe03f | 2005-05-05 16:16:04 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 1331 | set_bit(DMF_FROZEN, &md->flags); |
| 1332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | /* don't bdput right now, we don't want the bdev |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1334 | * to go away while it is locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | */ |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1339 | static void unlock_fs(struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | { |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 1341 | if (!test_bit(DMF_FROZEN, &md->flags)) |
| 1342 | return; |
| 1343 | |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1344 | thaw_bdev(md->suspended_bdev, md->frozen_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | md->frozen_sb = NULL; |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 1346 | clear_bit(DMF_FROZEN, &md->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | /* |
| 1350 | * We need to be able to change a mapping table under a mounted |
| 1351 | * filesystem. For example we might want to move some data in |
| 1352 | * the background. Before the table can be swapped with |
| 1353 | * dm_bind_table, dm_suspend must be called to flush any in |
| 1354 | * flight bios and ensure that any further io gets deferred. |
| 1355 | */ |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 1356 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | { |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1358 | struct dm_table *map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | DECLARE_WAITQUEUE(wait, current); |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 1360 | int pending, r = 0; |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 1361 | int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1362 | int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 1364 | mutex_lock(&md->suspend_lock); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1365 | |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 1366 | if (dm_suspended(md)) { |
| 1367 | r = -EINVAL; |
Alasdair G Kergon | d287483 | 2006-11-08 17:44:43 -0800 | [diff] [blame] | 1368 | goto out_unlock; |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 1369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
| 1371 | map = dm_get_table(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1373 | /* |
| 1374 | * DMF_NOFLUSH_SUSPENDING must be set before presuspend. |
| 1375 | * This flag is cleared before dm_suspend returns. |
| 1376 | */ |
| 1377 | if (noflush) |
| 1378 | set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); |
| 1379 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1380 | /* This does not get reverted if there's an error later. */ |
| 1381 | dm_table_presuspend_targets(map); |
| 1382 | |
Jun'ichi Nomura | bfa152f | 2007-01-26 00:57:07 -0800 | [diff] [blame] | 1383 | /* bdget() can stall if the pending I/Os are not flushed */ |
| 1384 | if (!noflush) { |
| 1385 | md->suspended_bdev = bdget_disk(md->disk, 0); |
| 1386 | if (!md->suspended_bdev) { |
| 1387 | DMWARN("bdget failed in dm_suspend"); |
| 1388 | r = -ENOMEM; |
| 1389 | goto flush_and_out; |
| 1390 | } |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1391 | |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1392 | /* |
| 1393 | * Flush I/O to the device. noflush supersedes do_lockfs, |
| 1394 | * because lock_fs() needs to flush I/Os. |
| 1395 | */ |
| 1396 | if (do_lockfs) { |
| 1397 | r = lock_fs(md); |
| 1398 | if (r) |
| 1399 | goto out; |
| 1400 | } |
Alasdair G Kergon | aa8d7c2 | 2006-01-06 00:20:06 -0800 | [diff] [blame] | 1401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
| 1403 | /* |
Alasdair G Kergon | 354e007 | 2005-05-05 16:16:05 -0700 | [diff] [blame] | 1404 | * First we set the BLOCK_IO flag so no more ios will be mapped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1406 | down_write(&md->io_lock); |
| 1407 | set_bit(DMF_BLOCK_IO, &md->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | add_wait_queue(&md->wait, &wait); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1410 | up_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | |
| 1412 | /* unplug */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1413 | if (map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | dm_table_unplug_all(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
| 1416 | /* |
| 1417 | * Then we wait for the already mapped ios to |
| 1418 | * complete. |
| 1419 | */ |
| 1420 | while (1) { |
| 1421 | set_current_state(TASK_INTERRUPTIBLE); |
| 1422 | |
Milan Broz | 7e5c1e8 | 2008-02-08 02:09:49 +0000 | [diff] [blame] | 1423 | smp_mb(); |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 1424 | pending = atomic_read(&md->pending); |
| 1425 | if (!pending || signal_pending(current)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | break; |
| 1427 | |
| 1428 | io_schedule(); |
| 1429 | } |
| 1430 | set_current_state(TASK_RUNNING); |
| 1431 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1432 | down_write(&md->io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | remove_wait_queue(&md->wait, &wait); |
| 1434 | |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1435 | if (noflush) |
| 1436 | __merge_pushback_list(md); |
Milan Broz | 94d6351 | 2008-02-08 02:10:27 +0000 | [diff] [blame^] | 1437 | up_write(&md->io_lock); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | /* were we interrupted ? */ |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 1440 | if (pending) { |
Milan Broz | 94d6351 | 2008-02-08 02:10:27 +0000 | [diff] [blame^] | 1441 | down_write(&md->io_lock); |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1442 | __flush_deferred_io(md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1443 | up_write(&md->io_lock); |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 1444 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1445 | unlock_fs(md); |
Milan Broz | 73d410c | 2008-02-08 02:10:25 +0000 | [diff] [blame] | 1446 | r = -EINTR; |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1447 | goto out; /* pushback list is already flushed, so skip flush */ |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1448 | } |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1449 | |
| 1450 | dm_table_postsuspend_targets(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
| 1452 | set_bit(DMF_SUSPENDED, &md->flags); |
| 1453 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1454 | flush_and_out: |
| 1455 | if (r && noflush) { |
| 1456 | /* |
| 1457 | * Because there may be already I/Os in the pushback list, |
| 1458 | * flush them before return. |
| 1459 | */ |
| 1460 | down_write(&md->io_lock); |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1461 | __merge_pushback_list(md); |
| 1462 | __flush_deferred_io(md); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1463 | up_write(&md->io_lock); |
| 1464 | } |
| 1465 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1466 | out: |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1467 | if (r && md->suspended_bdev) { |
| 1468 | bdput(md->suspended_bdev); |
| 1469 | md->suspended_bdev = NULL; |
| 1470 | } |
| 1471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | dm_table_put(map); |
Alasdair G Kergon | d287483 | 2006-11-08 17:44:43 -0800 | [diff] [blame] | 1473 | |
| 1474 | out_unlock: |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 1475 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1476 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | int dm_resume(struct mapped_device *md) |
| 1480 | { |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1481 | int r = -EINVAL; |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1482 | struct dm_table *map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 1484 | mutex_lock(&md->suspend_lock); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1485 | if (!dm_suspended(md)) |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1486 | goto out; |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1487 | |
| 1488 | map = dm_get_table(md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1489 | if (!map || !dm_table_get_size(map)) |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1490 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 1492 | r = dm_table_resume_targets(map); |
| 1493 | if (r) |
| 1494 | goto out; |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1495 | |
| 1496 | down_write(&md->io_lock); |
Milan Broz | 6d6f10d | 2008-02-08 02:10:22 +0000 | [diff] [blame] | 1497 | __flush_deferred_io(md); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1498 | up_write(&md->io_lock); |
| 1499 | |
| 1500 | unlock_fs(md); |
| 1501 | |
Jun'ichi Nomura | bfa152f | 2007-01-26 00:57:07 -0800 | [diff] [blame] | 1502 | if (md->suspended_bdev) { |
| 1503 | bdput(md->suspended_bdev); |
| 1504 | md->suspended_bdev = NULL; |
| 1505 | } |
Alasdair G Kergon | e39e2e9 | 2006-01-06 00:20:05 -0800 | [diff] [blame] | 1506 | |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1507 | clear_bit(DMF_SUSPENDED, &md->flags); |
| 1508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | dm_table_unplug_all(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
Alasdair G Kergon | 69267a3 | 2007-12-13 14:15:57 +0000 | [diff] [blame] | 1511 | dm_kobject_uevent(md); |
Hannes Reinecke | 8560ed6 | 2006-10-03 01:15:35 -0700 | [diff] [blame] | 1512 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1513 | r = 0; |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1514 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1515 | out: |
| 1516 | dm_table_put(map); |
Daniel Walker | e61290a | 2008-02-08 02:10:08 +0000 | [diff] [blame] | 1517 | mutex_unlock(&md->suspend_lock); |
Alasdair G Kergon | 2ca3310 | 2005-07-28 21:16:00 -0700 | [diff] [blame] | 1518 | |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1519 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /*----------------------------------------------------------------- |
| 1523 | * Event notification. |
| 1524 | *---------------------------------------------------------------*/ |
Alasdair G Kergon | 69267a3 | 2007-12-13 14:15:57 +0000 | [diff] [blame] | 1525 | void dm_kobject_uevent(struct mapped_device *md) |
| 1526 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1527 | kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE); |
Alasdair G Kergon | 69267a3 | 2007-12-13 14:15:57 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1530 | uint32_t dm_next_uevent_seq(struct mapped_device *md) |
| 1531 | { |
| 1532 | return atomic_add_return(1, &md->uevent_seq); |
| 1533 | } |
| 1534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | uint32_t dm_get_event_nr(struct mapped_device *md) |
| 1536 | { |
| 1537 | return atomic_read(&md->event_nr); |
| 1538 | } |
| 1539 | |
| 1540 | int dm_wait_event(struct mapped_device *md, int event_nr) |
| 1541 | { |
| 1542 | return wait_event_interruptible(md->eventq, |
| 1543 | (event_nr != atomic_read(&md->event_nr))); |
| 1544 | } |
| 1545 | |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 1546 | void dm_uevent_add(struct mapped_device *md, struct list_head *elist) |
| 1547 | { |
| 1548 | unsigned long flags; |
| 1549 | |
| 1550 | spin_lock_irqsave(&md->uevent_lock, flags); |
| 1551 | list_add(elist, &md->uevent_list); |
| 1552 | spin_unlock_irqrestore(&md->uevent_lock, flags); |
| 1553 | } |
| 1554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | /* |
| 1556 | * The gendisk is only valid as long as you have a reference |
| 1557 | * count on 'md'. |
| 1558 | */ |
| 1559 | struct gendisk *dm_disk(struct mapped_device *md) |
| 1560 | { |
| 1561 | return md->disk; |
| 1562 | } |
| 1563 | |
| 1564 | int dm_suspended(struct mapped_device *md) |
| 1565 | { |
| 1566 | return test_bit(DMF_SUSPENDED, &md->flags); |
| 1567 | } |
| 1568 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 1569 | int dm_noflush_suspending(struct dm_target *ti) |
| 1570 | { |
| 1571 | struct mapped_device *md = dm_table_get_md(ti->table); |
| 1572 | int r = __noflush_suspending(md); |
| 1573 | |
| 1574 | dm_put(md); |
| 1575 | |
| 1576 | return r; |
| 1577 | } |
| 1578 | EXPORT_SYMBOL_GPL(dm_noflush_suspending); |
| 1579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | static struct block_device_operations dm_blk_dops = { |
| 1581 | .open = dm_blk_open, |
| 1582 | .release = dm_blk_close, |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 1583 | .ioctl = dm_blk_ioctl, |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1584 | .getgeo = dm_blk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | .owner = THIS_MODULE |
| 1586 | }; |
| 1587 | |
| 1588 | EXPORT_SYMBOL(dm_get_mapinfo); |
| 1589 | |
| 1590 | /* |
| 1591 | * module hooks |
| 1592 | */ |
| 1593 | module_init(dm_init); |
| 1594 | module_exit(dm_exit); |
| 1595 | |
| 1596 | module_param(major, uint, 0); |
| 1597 | MODULE_PARM_DESC(major, "The major number of the device mapper"); |
| 1598 | MODULE_DESCRIPTION(DM_NAME " driver"); |
| 1599 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); |
| 1600 | MODULE_LICENSE("GPL"); |