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