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