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