Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dm-snapshot.c |
| 3 | * |
| 4 | * Copyright (C) 2001-2002 Sistina Software (UK) Limited. |
| 5 | * |
| 6 | * This file is released under the GPL. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/ctype.h> |
| 11 | #include <linux/device-mapper.h> |
Mikulas Patocka | 90fa152 | 2009-01-06 03:04:54 +0000 | [diff] [blame] | 12 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/fs.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/kdev_t.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/mempool.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/vmalloc.h> |
vignesh babu | 6f3c3f0 | 2007-10-19 22:38:44 +0100 | [diff] [blame] | 21 | #include <linux/log2.h> |
Alasdair G Kergon | a765e20 | 2008-04-24 22:02:01 +0100 | [diff] [blame] | 22 | #include <linux/dm-kcopyd.h> |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 23 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Jonathan Brassow | aea53d9 | 2009-01-06 03:05:15 +0000 | [diff] [blame] | 25 | #include "dm-exception-store.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "dm-bio-list.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 28 | #define DM_MSG_PREFIX "snapshots" |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | /* |
| 31 | * The percentage increment we will wake up users at |
| 32 | */ |
| 33 | #define WAKE_UP_PERCENT 5 |
| 34 | |
| 35 | /* |
| 36 | * kcopyd priority of snapshot operations |
| 37 | */ |
| 38 | #define SNAPSHOT_COPY_PRIORITY 2 |
| 39 | |
| 40 | /* |
Milan Broz | 8ee2767 | 2008-04-24 21:42:36 +0100 | [diff] [blame] | 41 | * Reserve 1MB for each snapshot initially (with minimum of 1 page). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | */ |
Milan Broz | 8ee2767 | 2008-04-24 21:42:36 +0100 | [diff] [blame] | 43 | #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 45 | /* |
| 46 | * The size of the mempool used to track chunks in use. |
| 47 | */ |
| 48 | #define MIN_IOS 256 |
| 49 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 50 | #define DM_TRACKED_CHUNK_HASH_SIZE 16 |
| 51 | #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \ |
| 52 | (DM_TRACKED_CHUNK_HASH_SIZE - 1)) |
| 53 | |
| 54 | struct exception_table { |
| 55 | uint32_t hash_mask; |
| 56 | unsigned hash_shift; |
| 57 | struct list_head *table; |
| 58 | }; |
| 59 | |
| 60 | struct dm_snapshot { |
| 61 | struct rw_semaphore lock; |
| 62 | |
| 63 | struct dm_dev *origin; |
| 64 | |
| 65 | /* List of snapshots per Origin */ |
| 66 | struct list_head list; |
| 67 | |
| 68 | /* You can't use a snapshot if this is 0 (e.g. if full) */ |
| 69 | int valid; |
| 70 | |
| 71 | /* Origin writes don't trigger exceptions until this is set */ |
| 72 | int active; |
| 73 | |
| 74 | /* Used for display of table */ |
| 75 | char type; |
| 76 | |
| 77 | mempool_t *pending_pool; |
| 78 | |
| 79 | atomic_t pending_exceptions_count; |
| 80 | |
| 81 | struct exception_table pending; |
| 82 | struct exception_table complete; |
| 83 | |
| 84 | /* |
| 85 | * pe_lock protects all pending_exception operations and access |
| 86 | * as well as the snapshot_bios list. |
| 87 | */ |
| 88 | spinlock_t pe_lock; |
| 89 | |
| 90 | /* The on disk metadata handler */ |
| 91 | struct dm_exception_store *store; |
| 92 | |
| 93 | struct dm_kcopyd_client *kcopyd_client; |
| 94 | |
| 95 | /* Queue of snapshot writes for ksnapd to flush */ |
| 96 | struct bio_list queued_bios; |
| 97 | struct work_struct queued_bios_work; |
| 98 | |
| 99 | /* Chunks with outstanding reads */ |
| 100 | mempool_t *tracked_chunk_pool; |
| 101 | spinlock_t tracked_chunk_lock; |
| 102 | struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE]; |
| 103 | }; |
| 104 | |
Adrian Bunk | c642f9e | 2006-12-08 02:41:13 -0800 | [diff] [blame] | 105 | static struct workqueue_struct *ksnapd; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 106 | static void flush_queued_bios(struct work_struct *work); |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 107 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 108 | static sector_t chunk_to_sector(struct dm_exception_store *store, |
| 109 | chunk_t chunk) |
| 110 | { |
| 111 | return chunk << store->chunk_shift; |
| 112 | } |
| 113 | |
| 114 | static int bdev_equal(struct block_device *lhs, struct block_device *rhs) |
| 115 | { |
| 116 | /* |
| 117 | * There is only ever one instance of a particular block |
| 118 | * device so we can compare pointers safely. |
| 119 | */ |
| 120 | return lhs == rhs; |
| 121 | } |
| 122 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 123 | struct dm_snap_pending_exception { |
| 124 | struct dm_snap_exception e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * Origin buffers waiting for this to complete are held |
| 128 | * in a bio list |
| 129 | */ |
| 130 | struct bio_list origin_bios; |
| 131 | struct bio_list snapshot_bios; |
| 132 | |
| 133 | /* |
Alasdair G Kergon | eccf081 | 2006-03-27 01:17:42 -0800 | [diff] [blame] | 134 | * Short-term queue of pending exceptions prior to submission. |
| 135 | */ |
| 136 | struct list_head list; |
| 137 | |
| 138 | /* |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 139 | * The primary pending_exception is the one that holds |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 140 | * the ref_count and the list of origin_bios for a |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 141 | * group of pending_exceptions. It is always last to get freed. |
| 142 | * These fields get set up when writing to the origin. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 144 | struct dm_snap_pending_exception *primary_pe; |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * Number of pending_exceptions processing this chunk. |
| 148 | * When this drops to zero we must complete the origin bios. |
| 149 | * If incrementing or decrementing this, hold pe->snap->lock for |
| 150 | * the sibling concerned and not pe->primary_pe->snap->lock unless |
| 151 | * they are the same. |
| 152 | */ |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 153 | atomic_t ref_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | /* Pointer back to snapshot context */ |
| 156 | struct dm_snapshot *snap; |
| 157 | |
| 158 | /* |
| 159 | * 1 indicates the exception has already been sent to |
| 160 | * kcopyd. |
| 161 | */ |
| 162 | int started; |
| 163 | }; |
| 164 | |
| 165 | /* |
| 166 | * Hash table mapping origin volumes to lists of snapshots and |
| 167 | * a lock to protect it |
| 168 | */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 169 | static struct kmem_cache *exception_cache; |
| 170 | static struct kmem_cache *pending_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 172 | struct dm_snap_tracked_chunk { |
| 173 | struct hlist_node node; |
| 174 | chunk_t chunk; |
| 175 | }; |
| 176 | |
| 177 | static struct kmem_cache *tracked_chunk_cache; |
| 178 | |
| 179 | static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s, |
| 180 | chunk_t chunk) |
| 181 | { |
| 182 | struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool, |
| 183 | GFP_NOIO); |
| 184 | unsigned long flags; |
| 185 | |
| 186 | c->chunk = chunk; |
| 187 | |
| 188 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); |
| 189 | hlist_add_head(&c->node, |
| 190 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]); |
| 191 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); |
| 192 | |
| 193 | return c; |
| 194 | } |
| 195 | |
| 196 | static void stop_tracking_chunk(struct dm_snapshot *s, |
| 197 | struct dm_snap_tracked_chunk *c) |
| 198 | { |
| 199 | unsigned long flags; |
| 200 | |
| 201 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); |
| 202 | hlist_del(&c->node); |
| 203 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); |
| 204 | |
| 205 | mempool_free(c, s->tracked_chunk_pool); |
| 206 | } |
| 207 | |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 208 | static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk) |
| 209 | { |
| 210 | struct dm_snap_tracked_chunk *c; |
| 211 | struct hlist_node *hn; |
| 212 | int found = 0; |
| 213 | |
| 214 | spin_lock_irq(&s->tracked_chunk_lock); |
| 215 | |
| 216 | hlist_for_each_entry(c, hn, |
| 217 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) { |
| 218 | if (c->chunk == chunk) { |
| 219 | found = 1; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | spin_unlock_irq(&s->tracked_chunk_lock); |
| 225 | |
| 226 | return found; |
| 227 | } |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* |
| 230 | * One of these per registered origin, held in the snapshot_origins hash |
| 231 | */ |
| 232 | struct origin { |
| 233 | /* The origin device */ |
| 234 | struct block_device *bdev; |
| 235 | |
| 236 | struct list_head hash_list; |
| 237 | |
| 238 | /* List of snapshots for this origin */ |
| 239 | struct list_head snapshots; |
| 240 | }; |
| 241 | |
| 242 | /* |
| 243 | * Size of the hash table for origin volumes. If we make this |
| 244 | * the size of the minors list then it should be nearly perfect |
| 245 | */ |
| 246 | #define ORIGIN_HASH_SIZE 256 |
| 247 | #define ORIGIN_MASK 0xFF |
| 248 | static struct list_head *_origins; |
| 249 | static struct rw_semaphore _origins_lock; |
| 250 | |
| 251 | static int init_origin_hash(void) |
| 252 | { |
| 253 | int i; |
| 254 | |
| 255 | _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head), |
| 256 | GFP_KERNEL); |
| 257 | if (!_origins) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 258 | DMERR("unable to allocate memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return -ENOMEM; |
| 260 | } |
| 261 | |
| 262 | for (i = 0; i < ORIGIN_HASH_SIZE; i++) |
| 263 | INIT_LIST_HEAD(_origins + i); |
| 264 | init_rwsem(&_origins_lock); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static void exit_origin_hash(void) |
| 270 | { |
| 271 | kfree(_origins); |
| 272 | } |
| 273 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 274 | static unsigned origin_hash(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | return bdev->bd_dev & ORIGIN_MASK; |
| 277 | } |
| 278 | |
| 279 | static struct origin *__lookup_origin(struct block_device *origin) |
| 280 | { |
| 281 | struct list_head *ol; |
| 282 | struct origin *o; |
| 283 | |
| 284 | ol = &_origins[origin_hash(origin)]; |
| 285 | list_for_each_entry (o, ol, hash_list) |
| 286 | if (bdev_equal(o->bdev, origin)) |
| 287 | return o; |
| 288 | |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | static void __insert_origin(struct origin *o) |
| 293 | { |
| 294 | struct list_head *sl = &_origins[origin_hash(o->bdev)]; |
| 295 | list_add_tail(&o->hash_list, sl); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Make a note of the snapshot and its origin so we can look it |
| 300 | * up when the origin has a write on it. |
| 301 | */ |
| 302 | static int register_snapshot(struct dm_snapshot *snap) |
| 303 | { |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 304 | struct origin *o, *new_o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | struct block_device *bdev = snap->origin->bdev; |
| 306 | |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 307 | new_o = kmalloc(sizeof(*new_o), GFP_KERNEL); |
| 308 | if (!new_o) |
| 309 | return -ENOMEM; |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | down_write(&_origins_lock); |
| 312 | o = __lookup_origin(bdev); |
| 313 | |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 314 | if (o) |
| 315 | kfree(new_o); |
| 316 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* New origin */ |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 318 | o = new_o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | /* Initialise the struct */ |
| 321 | INIT_LIST_HEAD(&o->snapshots); |
| 322 | o->bdev = bdev; |
| 323 | |
| 324 | __insert_origin(o); |
| 325 | } |
| 326 | |
| 327 | list_add_tail(&snap->list, &o->snapshots); |
| 328 | |
| 329 | up_write(&_origins_lock); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static void unregister_snapshot(struct dm_snapshot *s) |
| 334 | { |
| 335 | struct origin *o; |
| 336 | |
| 337 | down_write(&_origins_lock); |
| 338 | o = __lookup_origin(s->origin->bdev); |
| 339 | |
| 340 | list_del(&s->list); |
| 341 | if (list_empty(&o->snapshots)) { |
| 342 | list_del(&o->hash_list); |
| 343 | kfree(o); |
| 344 | } |
| 345 | |
| 346 | up_write(&_origins_lock); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Implementation of the exception hash tables. |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 351 | * The lowest hash_shift bits of the chunk number are ignored, allowing |
| 352 | * some consecutive chunks to be grouped together. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | */ |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 354 | static int init_exception_table(struct exception_table *et, uint32_t size, |
| 355 | unsigned hash_shift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
| 357 | unsigned int i; |
| 358 | |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 359 | et->hash_shift = hash_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | et->hash_mask = size - 1; |
| 361 | et->table = dm_vcalloc(size, sizeof(struct list_head)); |
| 362 | if (!et->table) |
| 363 | return -ENOMEM; |
| 364 | |
| 365 | for (i = 0; i < size; i++) |
| 366 | INIT_LIST_HEAD(et->table + i); |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 371 | static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 373 | struct list_head *slot; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 374 | struct dm_snap_exception *ex, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | int i, size; |
| 376 | |
| 377 | size = et->hash_mask + 1; |
| 378 | for (i = 0; i < size; i++) { |
| 379 | slot = et->table + i; |
| 380 | |
| 381 | list_for_each_entry_safe (ex, next, slot, hash_list) |
| 382 | kmem_cache_free(mem, ex); |
| 383 | } |
| 384 | |
| 385 | vfree(et->table); |
| 386 | } |
| 387 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 388 | static uint32_t exception_hash(struct exception_table *et, chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 390 | return (chunk >> et->hash_shift) & et->hash_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 393 | static void insert_exception(struct exception_table *eh, |
| 394 | struct dm_snap_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
| 396 | struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)]; |
| 397 | list_add(&e->hash_list, l); |
| 398 | } |
| 399 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 400 | static void remove_exception(struct dm_snap_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
| 402 | list_del(&e->hash_list); |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Return the exception data for a sector, or NULL if not |
| 407 | * remapped. |
| 408 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 409 | static struct dm_snap_exception *lookup_exception(struct exception_table *et, |
| 410 | chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
| 412 | struct list_head *slot; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 413 | struct dm_snap_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | slot = &et->table[exception_hash(et, chunk)]; |
| 416 | list_for_each_entry (e, slot, hash_list) |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 417 | if (chunk >= e->old_chunk && |
| 418 | chunk <= e->old_chunk + dm_consecutive_chunk_count(e)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | return e; |
| 420 | |
| 421 | return NULL; |
| 422 | } |
| 423 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 424 | static struct dm_snap_exception *alloc_exception(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 426 | struct dm_snap_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | e = kmem_cache_alloc(exception_cache, GFP_NOIO); |
| 429 | if (!e) |
| 430 | e = kmem_cache_alloc(exception_cache, GFP_ATOMIC); |
| 431 | |
| 432 | return e; |
| 433 | } |
| 434 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 435 | static void free_exception(struct dm_snap_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
| 437 | kmem_cache_free(exception_cache, e); |
| 438 | } |
| 439 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 440 | static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 442 | struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool, |
| 443 | GFP_NOIO); |
| 444 | |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 445 | atomic_inc(&s->pending_exceptions_count); |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 446 | pe->snap = s; |
| 447 | |
| 448 | return pe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 451 | static void free_pending_exception(struct dm_snap_pending_exception *pe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 453 | struct dm_snapshot *s = pe->snap; |
| 454 | |
| 455 | mempool_free(pe, s->pending_pool); |
| 456 | smp_mb__before_atomic_dec(); |
| 457 | atomic_dec(&s->pending_exceptions_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 460 | static void insert_completed_exception(struct dm_snapshot *s, |
| 461 | struct dm_snap_exception *new_e) |
| 462 | { |
| 463 | struct exception_table *eh = &s->complete; |
| 464 | struct list_head *l; |
| 465 | struct dm_snap_exception *e = NULL; |
| 466 | |
| 467 | l = &eh->table[exception_hash(eh, new_e->old_chunk)]; |
| 468 | |
| 469 | /* Add immediately if this table doesn't support consecutive chunks */ |
| 470 | if (!eh->hash_shift) |
| 471 | goto out; |
| 472 | |
| 473 | /* List is ordered by old_chunk */ |
| 474 | list_for_each_entry_reverse(e, l, hash_list) { |
| 475 | /* Insert after an existing chunk? */ |
| 476 | if (new_e->old_chunk == (e->old_chunk + |
| 477 | dm_consecutive_chunk_count(e) + 1) && |
| 478 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) + |
| 479 | dm_consecutive_chunk_count(e) + 1)) { |
| 480 | dm_consecutive_chunk_count_inc(e); |
| 481 | free_exception(new_e); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | /* Insert before an existing chunk? */ |
| 486 | if (new_e->old_chunk == (e->old_chunk - 1) && |
| 487 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) { |
| 488 | dm_consecutive_chunk_count_inc(e); |
| 489 | e->old_chunk--; |
| 490 | e->new_chunk--; |
| 491 | free_exception(new_e); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | if (new_e->old_chunk > e->old_chunk) |
| 496 | break; |
| 497 | } |
| 498 | |
| 499 | out: |
| 500 | list_add(&new_e->hash_list, e ? &e->hash_list : l); |
| 501 | } |
| 502 | |
Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 503 | /* |
| 504 | * Callback used by the exception stores to load exceptions when |
| 505 | * initialising. |
| 506 | */ |
| 507 | static int dm_add_exception(void *context, chunk_t old, chunk_t new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 509 | struct dm_snapshot *s = context; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 510 | struct dm_snap_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | e = alloc_exception(); |
| 513 | if (!e) |
| 514 | return -ENOMEM; |
| 515 | |
| 516 | e->old_chunk = old; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 517 | |
| 518 | /* Consecutive_count is implicitly initialised to zero */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | e->new_chunk = new; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 520 | |
| 521 | insert_completed_exception(s, e); |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Hard coded magic. |
| 528 | */ |
| 529 | static int calc_max_buckets(void) |
| 530 | { |
| 531 | /* use a fixed size of 2MB */ |
| 532 | unsigned long mem = 2 * 1024 * 1024; |
| 533 | mem /= sizeof(struct list_head); |
| 534 | |
| 535 | return mem; |
| 536 | } |
| 537 | |
| 538 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | * Allocate room for a suitable hash table. |
| 540 | */ |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 541 | static int init_hash_tables(struct dm_snapshot *s, chunk_t chunk_shift, |
| 542 | struct dm_dev *cow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
| 544 | sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets; |
| 545 | |
| 546 | /* |
| 547 | * Calculate based on the size of the original volume or |
| 548 | * the COW volume... |
| 549 | */ |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 550 | cow_dev_size = get_dev_size(cow->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | origin_dev_size = get_dev_size(s->origin->bdev); |
| 552 | max_buckets = calc_max_buckets(); |
| 553 | |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 554 | hash_size = min(origin_dev_size, cow_dev_size) >> chunk_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | hash_size = min(hash_size, max_buckets); |
| 556 | |
Robert P. J. Day | 8defd83 | 2008-02-08 02:10:06 +0000 | [diff] [blame] | 557 | hash_size = rounddown_pow_of_two(hash_size); |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 558 | if (init_exception_table(&s->complete, hash_size, |
| 559 | DM_CHUNK_CONSECUTIVE_BITS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return -ENOMEM; |
| 561 | |
| 562 | /* |
| 563 | * Allocate hash table for in-flight exceptions |
| 564 | * Make this smaller than the real hash table |
| 565 | */ |
| 566 | hash_size >>= 3; |
| 567 | if (hash_size < 64) |
| 568 | hash_size = 64; |
| 569 | |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 570 | if (init_exception_table(&s->pending, hash_size, 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | exit_exception_table(&s->complete, exception_cache); |
| 572 | return -ENOMEM; |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Round a number up to the nearest 'size' boundary. size must |
| 580 | * be a power of 2. |
| 581 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 582 | static ulong round_up(ulong n, ulong size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
| 584 | size--; |
| 585 | return (n + size) & ~size; |
| 586 | } |
| 587 | |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 588 | static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg, |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 589 | chunk_t *chunk_size, chunk_t *chunk_mask, |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 590 | chunk_t *chunk_shift, struct dm_dev *cow, |
| 591 | char **error) |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 592 | { |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 593 | unsigned long chunk_size_ulong; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 594 | char *value; |
| 595 | |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 596 | chunk_size_ulong = simple_strtoul(chunk_size_arg, &value, 10); |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 597 | if (*chunk_size_arg == '\0' || *value != '\0') { |
| 598 | *error = "Invalid chunk size"; |
| 599 | return -EINVAL; |
| 600 | } |
| 601 | |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 602 | if (!chunk_size_ulong) { |
| 603 | *chunk_size = *chunk_mask = *chunk_shift = 0; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Chunk size must be multiple of page size. Silently |
| 609 | * round up if it's not. |
| 610 | */ |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 611 | chunk_size_ulong = round_up(chunk_size_ulong, PAGE_SIZE >> 9); |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 612 | |
| 613 | /* Check chunk_size is a power of 2 */ |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 614 | if (!is_power_of_2(chunk_size_ulong)) { |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 615 | *error = "Chunk size is not a power of 2"; |
| 616 | return -EINVAL; |
| 617 | } |
| 618 | |
| 619 | /* Validate the chunk size against the device block size */ |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 620 | if (chunk_size_ulong % (bdev_hardsect_size(cow->bdev) >> 9)) { |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 621 | *error = "Chunk size is not a multiple of device blocksize"; |
| 622 | return -EINVAL; |
| 623 | } |
| 624 | |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 625 | *chunk_size = chunk_size_ulong; |
| 626 | *chunk_mask = chunk_size_ulong - 1; |
| 627 | *chunk_shift = ffs(chunk_size_ulong) - 1; |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | /* |
| 633 | * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size> |
| 634 | */ |
| 635 | static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 636 | { |
| 637 | struct dm_snapshot *s; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 638 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | int r = -EINVAL; |
| 640 | char persistent; |
| 641 | char *origin_path; |
| 642 | char *cow_path; |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 643 | chunk_t chunk_size, chunk_mask, chunk_shift; |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 644 | struct dm_dev *cow; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 646 | if (argc != 4) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 647 | ti->error = "requires exactly 4 arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | r = -EINVAL; |
| 649 | goto bad1; |
| 650 | } |
| 651 | |
| 652 | origin_path = argv[0]; |
| 653 | cow_path = argv[1]; |
| 654 | persistent = toupper(*argv[2]); |
| 655 | |
| 656 | if (persistent != 'P' && persistent != 'N') { |
| 657 | ti->error = "Persistent flag is not P or N"; |
| 658 | r = -EINVAL; |
| 659 | goto bad1; |
| 660 | } |
| 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 663 | if (s == NULL) { |
| 664 | ti->error = "Cannot allocate snapshot context private " |
| 665 | "structure"; |
| 666 | r = -ENOMEM; |
| 667 | goto bad1; |
| 668 | } |
| 669 | |
| 670 | r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin); |
| 671 | if (r) { |
| 672 | ti->error = "Cannot get origin device"; |
| 673 | goto bad2; |
| 674 | } |
| 675 | |
| 676 | r = dm_get_device(ti, cow_path, 0, 0, |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 677 | FMODE_READ | FMODE_WRITE, &cow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (r) { |
| 679 | dm_put_device(ti, s->origin); |
| 680 | ti->error = "Cannot get COW device"; |
| 681 | goto bad2; |
| 682 | } |
| 683 | |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 684 | r = set_chunk_size(s, argv[3], &chunk_size, &chunk_mask, &chunk_shift, |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 685 | cow, &ti->error); |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 686 | if (r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | goto bad3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | s->valid = 1; |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 690 | s->active = 0; |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 691 | atomic_set(&s->pending_exceptions_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | init_rwsem(&s->lock); |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 693 | spin_lock_init(&s->pe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | /* Allocate hash table for COW data */ |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 696 | if (init_hash_tables(s, chunk_shift, cow)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | ti->error = "Unable to allocate hash table space"; |
| 698 | r = -ENOMEM; |
| 699 | goto bad3; |
| 700 | } |
| 701 | |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 702 | r = dm_exception_store_create(argv[2], ti, chunk_size, chunk_mask, |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 703 | chunk_shift, cow, &s->store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | if (r) { |
| 705 | ti->error = "Couldn't create exception store"; |
| 706 | r = -EINVAL; |
| 707 | goto bad4; |
| 708 | } |
| 709 | |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 710 | r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | if (r) { |
| 712 | ti->error = "Could not create kcopyd client"; |
| 713 | goto bad5; |
| 714 | } |
| 715 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 716 | s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache); |
| 717 | if (!s->pending_pool) { |
| 718 | ti->error = "Could not allocate mempool for pending exceptions"; |
| 719 | goto bad6; |
| 720 | } |
| 721 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 722 | s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS, |
| 723 | tracked_chunk_cache); |
| 724 | if (!s->tracked_chunk_pool) { |
| 725 | ti->error = "Could not allocate tracked_chunk mempool for " |
| 726 | "tracking reads"; |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 727 | goto bad_tracked_chunk_pool; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) |
| 731 | INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]); |
| 732 | |
| 733 | spin_lock_init(&s->tracked_chunk_lock); |
| 734 | |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 735 | /* Metadata must only be loaded into one table at once */ |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 736 | r = s->store->type->read_metadata(s->store, dm_add_exception, |
| 737 | (void *)s); |
Milan Broz | 0764147 | 2007-07-12 17:28:13 +0100 | [diff] [blame] | 738 | if (r < 0) { |
Mark McLoughlin | f9cea4f | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 739 | ti->error = "Failed to read snapshot metadata"; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 740 | goto bad_load_and_register; |
Milan Broz | 0764147 | 2007-07-12 17:28:13 +0100 | [diff] [blame] | 741 | } else if (r > 0) { |
| 742 | s->valid = 0; |
| 743 | DMWARN("Snapshot is marked invalid."); |
Mark McLoughlin | f9cea4f | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 744 | } |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 745 | |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 746 | bio_list_init(&s->queued_bios); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 747 | INIT_WORK(&s->queued_bios_work, flush_queued_bios); |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | /* Add snapshot to the list of snapshots for this origin */ |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 750 | /* Exceptions aren't triggered till snapshot_resume() is called */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | if (register_snapshot(s)) { |
| 752 | r = -EINVAL; |
| 753 | ti->error = "Cannot register snapshot origin"; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 754 | goto bad_load_and_register; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | ti->private = s; |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 758 | ti->split_io = s->store->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | return 0; |
| 761 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 762 | bad_load_and_register: |
| 763 | mempool_destroy(s->tracked_chunk_pool); |
| 764 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 765 | bad_tracked_chunk_pool: |
| 766 | mempool_destroy(s->pending_pool); |
| 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | bad6: |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 769 | dm_kcopyd_client_destroy(s->kcopyd_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | bad5: |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 772 | s->store->type->dtr(s->store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | bad4: |
| 775 | exit_exception_table(&s->pending, pending_cache); |
| 776 | exit_exception_table(&s->complete, exception_cache); |
| 777 | |
| 778 | bad3: |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 779 | dm_put_device(ti, cow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | dm_put_device(ti, s->origin); |
| 781 | |
| 782 | bad2: |
| 783 | kfree(s); |
| 784 | |
| 785 | bad1: |
| 786 | return r; |
| 787 | } |
| 788 | |
Milan Broz | 31c93a0 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 789 | static void __free_exceptions(struct dm_snapshot *s) |
| 790 | { |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 791 | dm_kcopyd_client_destroy(s->kcopyd_client); |
Milan Broz | 31c93a0 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 792 | s->kcopyd_client = NULL; |
| 793 | |
| 794 | exit_exception_table(&s->pending, pending_cache); |
| 795 | exit_exception_table(&s->complete, exception_cache); |
| 796 | |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 797 | s->store->type->dtr(s->store); |
Milan Broz | 31c93a0 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | static void snapshot_dtr(struct dm_target *ti) |
| 801 | { |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 802 | #ifdef CONFIG_DM_DEBUG |
| 803 | int i; |
| 804 | #endif |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 805 | struct dm_snapshot *s = ti->private; |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 806 | struct dm_dev *cow = s->store->cow; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 808 | flush_workqueue(ksnapd); |
| 809 | |
Alasdair G Kergon | 138728d | 2006-03-27 01:17:50 -0800 | [diff] [blame] | 810 | /* Prevent further origin writes from using this snapshot. */ |
| 811 | /* After this returns there can be no new kcopyd jobs. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | unregister_snapshot(s); |
| 813 | |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 814 | while (atomic_read(&s->pending_exceptions_count)) |
Mikulas Patocka | 90fa152 | 2009-01-06 03:04:54 +0000 | [diff] [blame] | 815 | msleep(1); |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 816 | /* |
| 817 | * Ensure instructions in mempool_destroy aren't reordered |
| 818 | * before atomic_read. |
| 819 | */ |
| 820 | smp_mb(); |
| 821 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 822 | #ifdef CONFIG_DM_DEBUG |
| 823 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) |
| 824 | BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i])); |
| 825 | #endif |
| 826 | |
| 827 | mempool_destroy(s->tracked_chunk_pool); |
| 828 | |
Milan Broz | 31c93a0 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 829 | __free_exceptions(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 831 | mempool_destroy(s->pending_pool); |
| 832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | dm_put_device(ti, s->origin); |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 834 | dm_put_device(ti, cow); |
Alasdair G Kergon | 138728d | 2006-03-27 01:17:50 -0800 | [diff] [blame] | 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | kfree(s); |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Flush a list of buffers. |
| 841 | */ |
| 842 | static void flush_bios(struct bio *bio) |
| 843 | { |
| 844 | struct bio *n; |
| 845 | |
| 846 | while (bio) { |
| 847 | n = bio->bi_next; |
| 848 | bio->bi_next = NULL; |
| 849 | generic_make_request(bio); |
| 850 | bio = n; |
| 851 | } |
| 852 | } |
| 853 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 854 | static void flush_queued_bios(struct work_struct *work) |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 855 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 856 | struct dm_snapshot *s = |
| 857 | container_of(work, struct dm_snapshot, queued_bios_work); |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 858 | struct bio *queued_bios; |
| 859 | unsigned long flags; |
| 860 | |
| 861 | spin_lock_irqsave(&s->pe_lock, flags); |
| 862 | queued_bios = bio_list_get(&s->queued_bios); |
| 863 | spin_unlock_irqrestore(&s->pe_lock, flags); |
| 864 | |
| 865 | flush_bios(queued_bios); |
| 866 | } |
| 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | /* |
| 869 | * Error a list of buffers. |
| 870 | */ |
| 871 | static void error_bios(struct bio *bio) |
| 872 | { |
| 873 | struct bio *n; |
| 874 | |
| 875 | while (bio) { |
| 876 | n = bio->bi_next; |
| 877 | bio->bi_next = NULL; |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 878 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | bio = n; |
| 880 | } |
| 881 | } |
| 882 | |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 883 | static void __invalidate_snapshot(struct dm_snapshot *s, int err) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 884 | { |
| 885 | if (!s->valid) |
| 886 | return; |
| 887 | |
| 888 | if (err == -EIO) |
| 889 | DMERR("Invalidating snapshot: Error reading/writing."); |
| 890 | else if (err == -ENOMEM) |
| 891 | DMERR("Invalidating snapshot: Unable to allocate exception."); |
| 892 | |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 893 | if (s->store->type->drop_snapshot) |
| 894 | s->store->type->drop_snapshot(s->store); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 895 | |
| 896 | s->valid = 0; |
| 897 | |
Jonathan Brassow | 0cea9c7 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 898 | dm_table_event(s->store->ti->table); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 901 | static void get_pending_exception(struct dm_snap_pending_exception *pe) |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 902 | { |
| 903 | atomic_inc(&pe->ref_count); |
| 904 | } |
| 905 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 906 | static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe) |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 907 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 908 | struct dm_snap_pending_exception *primary_pe; |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 909 | struct bio *origin_bios = NULL; |
| 910 | |
| 911 | primary_pe = pe->primary_pe; |
| 912 | |
| 913 | /* |
| 914 | * If this pe is involved in a write to the origin and |
| 915 | * it is the last sibling to complete then release |
| 916 | * the bios for the original write to the origin. |
| 917 | */ |
| 918 | if (primary_pe && |
Mikulas Patocka | 7c5f78b | 2008-10-21 17:44:51 +0100 | [diff] [blame] | 919 | atomic_dec_and_test(&primary_pe->ref_count)) { |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 920 | origin_bios = bio_list_get(&primary_pe->origin_bios); |
Mikulas Patocka | 7c5f78b | 2008-10-21 17:44:51 +0100 | [diff] [blame] | 921 | free_pending_exception(primary_pe); |
| 922 | } |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 923 | |
| 924 | /* |
| 925 | * Free the pe if it's not linked to an origin write or if |
| 926 | * it's not itself a primary pe. |
| 927 | */ |
| 928 | if (!primary_pe || primary_pe != pe) |
| 929 | free_pending_exception(pe); |
| 930 | |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 931 | return origin_bios; |
| 932 | } |
| 933 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 934 | static void pending_complete(struct dm_snap_pending_exception *pe, int success) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 936 | struct dm_snap_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | struct dm_snapshot *s = pe->snap; |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 938 | struct bio *origin_bios = NULL; |
| 939 | struct bio *snapshot_bios = NULL; |
| 940 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 942 | if (!success) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | /* Read/write error - snapshot is unusable */ |
| 944 | down_write(&s->lock); |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 945 | __invalidate_snapshot(s, -EIO); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 946 | error = 1; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 947 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 950 | e = alloc_exception(); |
| 951 | if (!e) { |
| 952 | down_write(&s->lock); |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 953 | __invalidate_snapshot(s, -ENOMEM); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 954 | error = 1; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 955 | goto out; |
| 956 | } |
| 957 | *e = pe->e; |
| 958 | |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 959 | down_write(&s->lock); |
| 960 | if (!s->valid) { |
| 961 | free_exception(e); |
| 962 | error = 1; |
| 963 | goto out; |
| 964 | } |
| 965 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 966 | /* |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 967 | * Check for conflicting reads. This is extremely improbable, |
Mikulas Patocka | 90fa152 | 2009-01-06 03:04:54 +0000 | [diff] [blame] | 968 | * so msleep(1) is sufficient and there is no need for a wait queue. |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 969 | */ |
| 970 | while (__chunk_is_tracked(s, pe->e.old_chunk)) |
Mikulas Patocka | 90fa152 | 2009-01-06 03:04:54 +0000 | [diff] [blame] | 971 | msleep(1); |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 972 | |
| 973 | /* |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 974 | * Add a proper exception, and remove the |
| 975 | * in-flight exception from the list. |
| 976 | */ |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 977 | insert_completed_exception(s, e); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | out: |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 980 | remove_exception(&pe->e); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 981 | snapshot_bios = bio_list_get(&pe->snapshot_bios); |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 982 | origin_bios = put_pending_exception(pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 984 | up_write(&s->lock); |
| 985 | |
| 986 | /* Submit any pending write bios */ |
| 987 | if (error) |
| 988 | error_bios(snapshot_bios); |
| 989 | else |
| 990 | flush_bios(snapshot_bios); |
| 991 | |
| 992 | flush_bios(origin_bios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | static void commit_callback(void *context, int success) |
| 996 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 997 | struct dm_snap_pending_exception *pe = context; |
| 998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | pending_complete(pe, success); |
| 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * Called when the copy I/O has finished. kcopyd actually runs |
| 1004 | * this code so don't block. |
| 1005 | */ |
Alasdair G Kergon | 4cdc1d1 | 2008-03-28 14:16:10 -0700 | [diff] [blame] | 1006 | static void copy_callback(int read_err, unsigned long write_err, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1008 | struct dm_snap_pending_exception *pe = context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | struct dm_snapshot *s = pe->snap; |
| 1010 | |
| 1011 | if (read_err || write_err) |
| 1012 | pending_complete(pe, 0); |
| 1013 | |
| 1014 | else |
| 1015 | /* Update the metadata if we are persistent */ |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1016 | s->store->type->commit_exception(s->store, &pe->e, |
| 1017 | commit_callback, pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * Dispatches the copy operation to kcopyd. |
| 1022 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1023 | static void start_copy(struct dm_snap_pending_exception *pe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | { |
| 1025 | struct dm_snapshot *s = pe->snap; |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 1026 | struct dm_io_region src, dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | struct block_device *bdev = s->origin->bdev; |
| 1028 | sector_t dev_size; |
| 1029 | |
| 1030 | dev_size = get_dev_size(bdev); |
| 1031 | |
| 1032 | src.bdev = bdev; |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1033 | src.sector = chunk_to_sector(s->store, pe->e.old_chunk); |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 1034 | src.count = min(s->store->chunk_size, dev_size - src.sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1036 | dest.bdev = s->store->cow->bdev; |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1037 | dest.sector = chunk_to_sector(s->store, pe->e.new_chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | dest.count = src.count; |
| 1039 | |
| 1040 | /* Hand over to kcopyd */ |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1041 | dm_kcopyd_copy(s->kcopyd_client, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | &src, 1, &dest, 0, copy_callback, pe); |
| 1043 | } |
| 1044 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1045 | static struct dm_snap_pending_exception * |
| 1046 | __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk) |
| 1047 | { |
| 1048 | struct dm_snap_exception *e = lookup_exception(&s->pending, chunk); |
| 1049 | |
| 1050 | if (!e) |
| 1051 | return NULL; |
| 1052 | |
| 1053 | return container_of(e, struct dm_snap_pending_exception, e); |
| 1054 | } |
| 1055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | /* |
| 1057 | * Looks to see if this snapshot already has a pending exception |
| 1058 | * for this chunk, otherwise it allocates a new one and inserts |
| 1059 | * it into the pending table. |
| 1060 | * |
| 1061 | * NOTE: a write lock must be held on snap->lock before calling |
| 1062 | * this. |
| 1063 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1064 | static struct dm_snap_pending_exception * |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1065 | __find_pending_exception(struct dm_snapshot *s, |
| 1066 | struct dm_snap_pending_exception *pe, chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | { |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1068 | struct dm_snap_pending_exception *pe2; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1069 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1070 | pe2 = __lookup_pending_exception(s, chunk); |
| 1071 | if (pe2) { |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1072 | free_pending_exception(pe); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1073 | return pe2; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | pe->e.old_chunk = chunk; |
| 1077 | bio_list_init(&pe->origin_bios); |
| 1078 | bio_list_init(&pe->snapshot_bios); |
| 1079 | pe->primary_pe = NULL; |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1080 | atomic_set(&pe->ref_count, 0); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1081 | pe->started = 0; |
| 1082 | |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1083 | if (s->store->type->prepare_exception(s->store, &pe->e)) { |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1084 | free_pending_exception(pe); |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1088 | get_pending_exception(pe); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1089 | insert_exception(&s->pending, &pe->e); |
| 1090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | return pe; |
| 1092 | } |
| 1093 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1094 | static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e, |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1095 | struct bio *bio, chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | { |
Jonathan Brassow | 49beb2b | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1097 | bio->bi_bdev = s->store->cow->bdev; |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1098 | bio->bi_sector = chunk_to_sector(s->store, |
| 1099 | dm_chunk_number(e->new_chunk) + |
| 1100 | (chunk - e->old_chunk)) + |
| 1101 | (bio->bi_sector & |
| 1102 | s->store->chunk_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static int snapshot_map(struct dm_target *ti, struct bio *bio, |
| 1106 | union map_info *map_context) |
| 1107 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1108 | struct dm_snap_exception *e; |
| 1109 | struct dm_snapshot *s = ti->private; |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1110 | int r = DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | chunk_t chunk; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1112 | struct dm_snap_pending_exception *pe = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1114 | chunk = sector_to_chunk(s->store, bio->bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | /* Full snapshots are not usable */ |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1117 | /* To get here the table must be live so s->active is always set. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | if (!s->valid) |
Alasdair G Kergon | f6a80ea | 2005-07-12 15:53:01 -0700 | [diff] [blame] | 1119 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1121 | /* FIXME: should only take write lock if we need |
| 1122 | * to copy an exception */ |
| 1123 | down_write(&s->lock); |
| 1124 | |
| 1125 | if (!s->valid) { |
| 1126 | r = -EIO; |
| 1127 | goto out_unlock; |
| 1128 | } |
| 1129 | |
| 1130 | /* If the block is already remapped - use that, else remap it */ |
| 1131 | e = lookup_exception(&s->complete, chunk); |
| 1132 | if (e) { |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1133 | remap_exception(s, e, bio, chunk); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1134 | goto out_unlock; |
| 1135 | } |
| 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | /* |
| 1138 | * Write to snapshot - higher level takes care of RW/RO |
| 1139 | * flags so we should only get this if we are |
| 1140 | * writeable. |
| 1141 | */ |
| 1142 | if (bio_rw(bio) == WRITE) { |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1143 | pe = __lookup_pending_exception(s, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1144 | if (!pe) { |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1145 | up_write(&s->lock); |
| 1146 | pe = alloc_pending_exception(s); |
| 1147 | down_write(&s->lock); |
| 1148 | |
| 1149 | if (!s->valid) { |
| 1150 | free_pending_exception(pe); |
| 1151 | r = -EIO; |
| 1152 | goto out_unlock; |
| 1153 | } |
| 1154 | |
Mikulas Patocka | 35bf659 | 2009-04-02 19:55:26 +0100 | [diff] [blame] | 1155 | e = lookup_exception(&s->complete, chunk); |
| 1156 | if (e) { |
| 1157 | free_pending_exception(pe); |
| 1158 | remap_exception(s, e, bio, chunk); |
| 1159 | goto out_unlock; |
| 1160 | } |
| 1161 | |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1162 | pe = __find_pending_exception(s, pe, chunk); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1163 | if (!pe) { |
| 1164 | __invalidate_snapshot(s, -ENOMEM); |
| 1165 | r = -EIO; |
| 1166 | goto out_unlock; |
| 1167 | } |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1170 | remap_exception(s, &pe->e, bio, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1171 | bio_list_add(&pe->snapshot_bios, bio); |
| 1172 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1173 | r = DM_MAPIO_SUBMITTED; |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1174 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1175 | if (!pe->started) { |
| 1176 | /* this is protected by snap->lock */ |
| 1177 | pe->started = 1; |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1178 | up_write(&s->lock); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1179 | start_copy(pe); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1180 | goto out; |
| 1181 | } |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1182 | } else { |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1183 | bio->bi_bdev = s->origin->bdev; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1184 | map_context->ptr = track_chunk(s, chunk); |
| 1185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1187 | out_unlock: |
| 1188 | up_write(&s->lock); |
| 1189 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | return r; |
| 1191 | } |
| 1192 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1193 | static int snapshot_end_io(struct dm_target *ti, struct bio *bio, |
| 1194 | int error, union map_info *map_context) |
| 1195 | { |
| 1196 | struct dm_snapshot *s = ti->private; |
| 1197 | struct dm_snap_tracked_chunk *c = map_context->ptr; |
| 1198 | |
| 1199 | if (c) |
| 1200 | stop_tracking_chunk(s, c); |
| 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | static void snapshot_resume(struct dm_target *ti) |
| 1206 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1207 | struct dm_snapshot *s = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 1209 | down_write(&s->lock); |
| 1210 | s->active = 1; |
| 1211 | up_write(&s->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | static int snapshot_status(struct dm_target *ti, status_type_t type, |
| 1215 | char *result, unsigned int maxlen) |
| 1216 | { |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame^] | 1217 | unsigned sz = 0; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1218 | struct dm_snapshot *snap = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
| 1220 | switch (type) { |
| 1221 | case STATUSTYPE_INFO: |
| 1222 | if (!snap->valid) |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame^] | 1223 | DMEMIT("Invalid"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | else { |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1225 | if (snap->store->type->fraction_full) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | sector_t numerator, denominator; |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1227 | snap->store->type->fraction_full(snap->store, |
| 1228 | &numerator, |
| 1229 | &denominator); |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame^] | 1230 | DMEMIT("%llu/%llu", |
| 1231 | (unsigned long long)numerator, |
| 1232 | (unsigned long long)denominator); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | } |
| 1234 | else |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame^] | 1235 | DMEMIT("Unknown"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
| 1237 | break; |
| 1238 | |
| 1239 | case STATUSTYPE_TABLE: |
| 1240 | /* |
| 1241 | * kdevname returns a static pointer so we need |
| 1242 | * to make private copies if the output is to |
| 1243 | * make sense. |
| 1244 | */ |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame^] | 1245 | DMEMIT("%s", snap->origin->name); |
| 1246 | DMEMIT(" %s %s %llu", snap->store->cow->name, |
| 1247 | snap->store->type->name, |
| 1248 | (unsigned long long)snap->store->chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | break; |
| 1250 | } |
| 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | /*----------------------------------------------------------------- |
| 1256 | * Origin methods |
| 1257 | *---------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | static int __origin_write(struct list_head *snapshots, struct bio *bio) |
| 1259 | { |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1260 | int r = DM_MAPIO_REMAPPED, first = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | struct dm_snapshot *snap; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1262 | struct dm_snap_exception *e; |
| 1263 | struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | chunk_t chunk; |
Alasdair G Kergon | eccf081 | 2006-03-27 01:17:42 -0800 | [diff] [blame] | 1265 | LIST_HEAD(pe_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
| 1267 | /* Do all the snapshots on this origin */ |
| 1268 | list_for_each_entry (snap, snapshots, list) { |
| 1269 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1270 | down_write(&snap->lock); |
| 1271 | |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 1272 | /* Only deal with valid and active snapshots */ |
| 1273 | if (!snap->valid || !snap->active) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1274 | goto next_snapshot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | |
Alasdair G Kergon | d5e404c | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 1276 | /* Nothing to do if writing beyond end of snapshot */ |
Jonathan Brassow | 0cea9c7 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 1277 | if (bio->bi_sector >= dm_table_get_size(snap->store->ti->table)) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1278 | goto next_snapshot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
| 1280 | /* |
| 1281 | * Remember, different snapshots can have |
| 1282 | * different chunk sizes. |
| 1283 | */ |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1284 | chunk = sector_to_chunk(snap->store, bio->bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
| 1286 | /* |
| 1287 | * Check exception table to see if block |
| 1288 | * is already remapped in this snapshot |
| 1289 | * and trigger an exception if not. |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 1290 | * |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1291 | * ref_count is initialised to 1 so pending_complete() |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 1292 | * won't destroy the primary_pe while we're inside this loop. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | */ |
| 1294 | e = lookup_exception(&snap->complete, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1295 | if (e) |
| 1296 | goto next_snapshot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1298 | pe = __lookup_pending_exception(snap, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1299 | if (!pe) { |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1300 | up_write(&snap->lock); |
| 1301 | pe = alloc_pending_exception(snap); |
| 1302 | down_write(&snap->lock); |
| 1303 | |
| 1304 | if (!snap->valid) { |
| 1305 | free_pending_exception(pe); |
| 1306 | goto next_snapshot; |
| 1307 | } |
| 1308 | |
Mikulas Patocka | 35bf659 | 2009-04-02 19:55:26 +0100 | [diff] [blame] | 1309 | e = lookup_exception(&snap->complete, chunk); |
| 1310 | if (e) { |
| 1311 | free_pending_exception(pe); |
| 1312 | goto next_snapshot; |
| 1313 | } |
| 1314 | |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1315 | pe = __find_pending_exception(snap, pe, chunk); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1316 | if (!pe) { |
| 1317 | __invalidate_snapshot(snap, -ENOMEM); |
| 1318 | goto next_snapshot; |
| 1319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1322 | if (!primary_pe) { |
| 1323 | /* |
| 1324 | * Either every pe here has same |
| 1325 | * primary_pe or none has one yet. |
| 1326 | */ |
| 1327 | if (pe->primary_pe) |
| 1328 | primary_pe = pe->primary_pe; |
| 1329 | else { |
| 1330 | primary_pe = pe; |
| 1331 | first = 1; |
| 1332 | } |
| 1333 | |
| 1334 | bio_list_add(&primary_pe->origin_bios, bio); |
| 1335 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1336 | r = DM_MAPIO_SUBMITTED; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | if (!pe->primary_pe) { |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1340 | pe->primary_pe = primary_pe; |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1341 | get_pending_exception(primary_pe); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | if (!pe->started) { |
| 1345 | pe->started = 1; |
| 1346 | list_add_tail(&pe->list, &pe_queue); |
| 1347 | } |
| 1348 | |
| 1349 | next_snapshot: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | up_write(&snap->lock); |
| 1351 | } |
| 1352 | |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 1353 | if (!primary_pe) |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1354 | return r; |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 1355 | |
| 1356 | /* |
| 1357 | * If this is the first time we're processing this chunk and |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1358 | * ref_count is now 1 it means all the pending exceptions |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 1359 | * got completed while we were in the loop above, so it falls to |
| 1360 | * us here to remove the primary_pe and submit any origin_bios. |
| 1361 | */ |
| 1362 | |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1363 | if (first && atomic_dec_and_test(&primary_pe->ref_count)) { |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 1364 | flush_bios(bio_list_get(&primary_pe->origin_bios)); |
| 1365 | free_pending_exception(primary_pe); |
| 1366 | /* If we got here, pe_queue is necessarily empty. */ |
Alasdair G Kergon | 4b832e8 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1367 | return r; |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 1368 | } |
| 1369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | /* |
| 1371 | * Now that we have a complete pe list we can start the copying. |
| 1372 | */ |
Alasdair G Kergon | eccf081 | 2006-03-27 01:17:42 -0800 | [diff] [blame] | 1373 | list_for_each_entry_safe(pe, next_pe, &pe_queue, list) |
| 1374 | start_copy(pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
| 1376 | return r; |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * Called on a write from the origin driver. |
| 1381 | */ |
| 1382 | static int do_origin(struct dm_dev *origin, struct bio *bio) |
| 1383 | { |
| 1384 | struct origin *o; |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1385 | int r = DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
| 1387 | down_read(&_origins_lock); |
| 1388 | o = __lookup_origin(origin->bdev); |
| 1389 | if (o) |
| 1390 | r = __origin_write(&o->snapshots, bio); |
| 1391 | up_read(&_origins_lock); |
| 1392 | |
| 1393 | return r; |
| 1394 | } |
| 1395 | |
| 1396 | /* |
| 1397 | * Origin: maps a linear range of a device, with hooks for snapshotting. |
| 1398 | */ |
| 1399 | |
| 1400 | /* |
| 1401 | * Construct an origin mapping: <dev_path> |
| 1402 | * The context for an origin is merely a 'struct dm_dev *' |
| 1403 | * pointing to the real device. |
| 1404 | */ |
| 1405 | static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1406 | { |
| 1407 | int r; |
| 1408 | struct dm_dev *dev; |
| 1409 | |
| 1410 | if (argc != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1411 | ti->error = "origin: incorrect number of arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | return -EINVAL; |
| 1413 | } |
| 1414 | |
| 1415 | r = dm_get_device(ti, argv[0], 0, ti->len, |
| 1416 | dm_table_get_mode(ti->table), &dev); |
| 1417 | if (r) { |
| 1418 | ti->error = "Cannot get target device"; |
| 1419 | return r; |
| 1420 | } |
| 1421 | |
| 1422 | ti->private = dev; |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
| 1426 | static void origin_dtr(struct dm_target *ti) |
| 1427 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1428 | struct dm_dev *dev = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | dm_put_device(ti, dev); |
| 1430 | } |
| 1431 | |
| 1432 | static int origin_map(struct dm_target *ti, struct bio *bio, |
| 1433 | union map_info *map_context) |
| 1434 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1435 | struct dm_dev *dev = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | bio->bi_bdev = dev->bdev; |
| 1437 | |
| 1438 | /* Only tell snapshots if this is a write */ |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1439 | return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r)) |
| 1443 | |
| 1444 | /* |
| 1445 | * Set the target "split_io" field to the minimum of all the snapshots' |
| 1446 | * chunk sizes. |
| 1447 | */ |
| 1448 | static void origin_resume(struct dm_target *ti) |
| 1449 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1450 | struct dm_dev *dev = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | struct dm_snapshot *snap; |
| 1452 | struct origin *o; |
| 1453 | chunk_t chunk_size = 0; |
| 1454 | |
| 1455 | down_read(&_origins_lock); |
| 1456 | o = __lookup_origin(dev->bdev); |
| 1457 | if (o) |
| 1458 | list_for_each_entry (snap, &o->snapshots, list) |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 1459 | chunk_size = min_not_zero(chunk_size, |
| 1460 | snap->store->chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | up_read(&_origins_lock); |
| 1462 | |
| 1463 | ti->split_io = chunk_size; |
| 1464 | } |
| 1465 | |
| 1466 | static int origin_status(struct dm_target *ti, status_type_t type, char *result, |
| 1467 | unsigned int maxlen) |
| 1468 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1469 | struct dm_dev *dev = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | |
| 1471 | switch (type) { |
| 1472 | case STATUSTYPE_INFO: |
| 1473 | result[0] = '\0'; |
| 1474 | break; |
| 1475 | |
| 1476 | case STATUSTYPE_TABLE: |
| 1477 | snprintf(result, maxlen, "%s", dev->name); |
| 1478 | break; |
| 1479 | } |
| 1480 | |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | static struct target_type origin_target = { |
| 1485 | .name = "snapshot-origin", |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1486 | .version = {1, 6, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | .module = THIS_MODULE, |
| 1488 | .ctr = origin_ctr, |
| 1489 | .dtr = origin_dtr, |
| 1490 | .map = origin_map, |
| 1491 | .resume = origin_resume, |
| 1492 | .status = origin_status, |
| 1493 | }; |
| 1494 | |
| 1495 | static struct target_type snapshot_target = { |
| 1496 | .name = "snapshot", |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1497 | .version = {1, 6, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | .module = THIS_MODULE, |
| 1499 | .ctr = snapshot_ctr, |
| 1500 | .dtr = snapshot_dtr, |
| 1501 | .map = snapshot_map, |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1502 | .end_io = snapshot_end_io, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | .resume = snapshot_resume, |
| 1504 | .status = snapshot_status, |
| 1505 | }; |
| 1506 | |
| 1507 | static int __init dm_snapshot_init(void) |
| 1508 | { |
| 1509 | int r; |
| 1510 | |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1511 | r = dm_exception_store_init(); |
| 1512 | if (r) { |
| 1513 | DMERR("Failed to initialize exception stores"); |
| 1514 | return r; |
| 1515 | } |
| 1516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | r = dm_register_target(&snapshot_target); |
| 1518 | if (r) { |
| 1519 | DMERR("snapshot target register failed %d", r); |
| 1520 | return r; |
| 1521 | } |
| 1522 | |
| 1523 | r = dm_register_target(&origin_target); |
| 1524 | if (r < 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1525 | DMERR("Origin target register failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | goto bad1; |
| 1527 | } |
| 1528 | |
| 1529 | r = init_origin_hash(); |
| 1530 | if (r) { |
| 1531 | DMERR("init_origin_hash failed."); |
| 1532 | goto bad2; |
| 1533 | } |
| 1534 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1535 | exception_cache = KMEM_CACHE(dm_snap_exception, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | if (!exception_cache) { |
| 1537 | DMERR("Couldn't create exception cache."); |
| 1538 | r = -ENOMEM; |
| 1539 | goto bad3; |
| 1540 | } |
| 1541 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1542 | pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | if (!pending_cache) { |
| 1544 | DMERR("Couldn't create pending cache."); |
| 1545 | r = -ENOMEM; |
| 1546 | goto bad4; |
| 1547 | } |
| 1548 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1549 | tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0); |
| 1550 | if (!tracked_chunk_cache) { |
| 1551 | DMERR("Couldn't create cache to track chunks in use."); |
| 1552 | r = -ENOMEM; |
| 1553 | goto bad5; |
| 1554 | } |
| 1555 | |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1556 | ksnapd = create_singlethread_workqueue("ksnapd"); |
| 1557 | if (!ksnapd) { |
| 1558 | DMERR("Failed to create ksnapd workqueue."); |
| 1559 | r = -ENOMEM; |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1560 | goto bad_pending_pool; |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | return 0; |
| 1564 | |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1565 | bad_pending_pool: |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1566 | kmem_cache_destroy(tracked_chunk_cache); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1567 | bad5: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | kmem_cache_destroy(pending_cache); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1569 | bad4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | kmem_cache_destroy(exception_cache); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1571 | bad3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | exit_origin_hash(); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1573 | bad2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | dm_unregister_target(&origin_target); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1575 | bad1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | dm_unregister_target(&snapshot_target); |
| 1577 | return r; |
| 1578 | } |
| 1579 | |
| 1580 | static void __exit dm_snapshot_exit(void) |
| 1581 | { |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1582 | destroy_workqueue(ksnapd); |
| 1583 | |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 1584 | dm_unregister_target(&snapshot_target); |
| 1585 | dm_unregister_target(&origin_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | |
| 1587 | exit_origin_hash(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | kmem_cache_destroy(pending_cache); |
| 1589 | kmem_cache_destroy(exception_cache); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1590 | kmem_cache_destroy(tracked_chunk_cache); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 1591 | |
| 1592 | dm_exception_store_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* Module hooks */ |
| 1596 | module_init(dm_snapshot_init); |
| 1597 | module_exit(dm_snapshot_exit); |
| 1598 | |
| 1599 | MODULE_DESCRIPTION(DM_NAME " snapshot target"); |
| 1600 | MODULE_AUTHOR("Joe Thornber"); |
| 1601 | MODULE_LICENSE("GPL"); |