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