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