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