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