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