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