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