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