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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Jonathan Brassow | aea53d9 | 2009-01-06 03:05:15 +0000 | [diff] [blame] | 23 | #include "dm-exception-store.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 25 | #define DM_MSG_PREFIX "snapshots" |
| 26 | |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 27 | static const char dm_snapshot_merge_target_name[] = "snapshot-merge"; |
| 28 | |
| 29 | #define dm_target_is_snapshot_merge(ti) \ |
| 30 | ((ti)->type->name == dm_snapshot_merge_target_name) |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 33 | * The size of the mempool used to track chunks in use. |
| 34 | */ |
| 35 | #define MIN_IOS 256 |
| 36 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 37 | #define DM_TRACKED_CHUNK_HASH_SIZE 16 |
| 38 | #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \ |
| 39 | (DM_TRACKED_CHUNK_HASH_SIZE - 1)) |
| 40 | |
Jon Brassow | 191437a | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 41 | struct dm_exception_table { |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 42 | uint32_t hash_mask; |
| 43 | unsigned hash_shift; |
| 44 | struct list_head *table; |
| 45 | }; |
| 46 | |
| 47 | struct dm_snapshot { |
| 48 | struct rw_semaphore lock; |
| 49 | |
| 50 | struct dm_dev *origin; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 51 | struct dm_dev *cow; |
| 52 | |
| 53 | struct dm_target *ti; |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 54 | |
| 55 | /* List of snapshots per Origin */ |
| 56 | struct list_head list; |
| 57 | |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 58 | /* |
| 59 | * You can't use a snapshot if this is 0 (e.g. if full). |
| 60 | * A snapshot-merge target never clears this. |
| 61 | */ |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 62 | int valid; |
| 63 | |
| 64 | /* Origin writes don't trigger exceptions until this is set */ |
| 65 | int active; |
| 66 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 67 | atomic_t pending_exceptions_count; |
| 68 | |
Mikulas Patocka | 4c0c59e | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 69 | /* Protected by "lock" */ |
| 70 | sector_t exception_start_sequence; |
| 71 | |
| 72 | /* Protected by kcopyd single-threaded callback */ |
| 73 | sector_t exception_complete_sequence; |
| 74 | |
| 75 | /* |
| 76 | * A list of pending exceptions that completed out of order. |
| 77 | * Protected by kcopyd single-threaded callback. |
| 78 | */ |
| 79 | struct list_head out_of_order_list; |
| 80 | |
Mike Snitzer | 924e600 | 2010-03-06 02:32:33 +0000 | [diff] [blame] | 81 | mempool_t *pending_pool; |
| 82 | |
Jon Brassow | 191437a | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 83 | struct dm_exception_table pending; |
| 84 | struct dm_exception_table complete; |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * pe_lock protects all pending_exception operations and access |
| 88 | * as well as the snapshot_bios list. |
| 89 | */ |
| 90 | spinlock_t pe_lock; |
| 91 | |
Mike Snitzer | 924e600 | 2010-03-06 02:32:33 +0000 | [diff] [blame] | 92 | /* Chunks with outstanding reads */ |
| 93 | spinlock_t tracked_chunk_lock; |
| 94 | mempool_t *tracked_chunk_pool; |
| 95 | struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE]; |
| 96 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 97 | /* The on disk metadata handler */ |
| 98 | struct dm_exception_store *store; |
| 99 | |
| 100 | struct dm_kcopyd_client *kcopyd_client; |
| 101 | |
Mike Snitzer | 924e600 | 2010-03-06 02:32:33 +0000 | [diff] [blame] | 102 | /* Wait for events based on state_bits */ |
| 103 | unsigned long state_bits; |
| 104 | |
| 105 | /* Range of chunks currently being merged. */ |
| 106 | chunk_t first_merging_chunk; |
| 107 | int num_merging_chunks; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 108 | |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 109 | /* |
| 110 | * The merge operation failed if this flag is set. |
| 111 | * Failure modes are handled as follows: |
| 112 | * - I/O error reading the header |
| 113 | * => don't load the target; abort. |
| 114 | * - Header does not have "valid" flag set |
| 115 | * => use the origin; forget about the snapshot. |
| 116 | * - I/O error when reading exceptions |
| 117 | * => don't load the target; abort. |
| 118 | * (We can't use the intermediate origin state.) |
| 119 | * - I/O error while merging |
| 120 | * => stop merging; set merge_failed; process I/O normally. |
| 121 | */ |
| 122 | int merge_failed; |
| 123 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 124 | /* |
| 125 | * Incoming bios that overlap with chunks being merged must wait |
| 126 | * for them to be committed. |
| 127 | */ |
| 128 | struct bio_list bios_queued_during_merge; |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 131 | /* |
| 132 | * state_bits: |
| 133 | * RUNNING_MERGE - Merge operation is in progress. |
| 134 | * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped; |
| 135 | * cleared afterwards. |
| 136 | */ |
| 137 | #define RUNNING_MERGE 0 |
| 138 | #define SHUTDOWN_MERGE 1 |
| 139 | |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 140 | struct dm_dev *dm_snap_origin(struct dm_snapshot *s) |
| 141 | { |
| 142 | return s->origin; |
| 143 | } |
| 144 | EXPORT_SYMBOL(dm_snap_origin); |
| 145 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 146 | struct dm_dev *dm_snap_cow(struct dm_snapshot *s) |
| 147 | { |
| 148 | return s->cow; |
| 149 | } |
| 150 | EXPORT_SYMBOL(dm_snap_cow); |
| 151 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 152 | static sector_t chunk_to_sector(struct dm_exception_store *store, |
| 153 | chunk_t chunk) |
| 154 | { |
| 155 | return chunk << store->chunk_shift; |
| 156 | } |
| 157 | |
| 158 | static int bdev_equal(struct block_device *lhs, struct block_device *rhs) |
| 159 | { |
| 160 | /* |
| 161 | * There is only ever one instance of a particular block |
| 162 | * device so we can compare pointers safely. |
| 163 | */ |
| 164 | return lhs == rhs; |
| 165 | } |
| 166 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 167 | struct dm_snap_pending_exception { |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 168 | struct dm_exception e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * Origin buffers waiting for this to complete are held |
| 172 | * in a bio list |
| 173 | */ |
| 174 | struct bio_list origin_bios; |
| 175 | struct bio_list snapshot_bios; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | /* Pointer back to snapshot context */ |
| 178 | struct dm_snapshot *snap; |
| 179 | |
| 180 | /* |
| 181 | * 1 indicates the exception has already been sent to |
| 182 | * kcopyd. |
| 183 | */ |
| 184 | int started; |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 185 | |
Mikulas Patocka | 4c0c59e | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 186 | /* There was copying error. */ |
| 187 | int copy_error; |
| 188 | |
| 189 | /* A sequence number, it is used for in-order completion. */ |
| 190 | sector_t exception_sequence; |
| 191 | |
| 192 | struct list_head out_of_order_entry; |
| 193 | |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 194 | /* |
| 195 | * For writing a complete chunk, bypassing the copy. |
| 196 | */ |
| 197 | struct bio *full_bio; |
| 198 | bio_end_io_t *full_bio_end_io; |
| 199 | void *full_bio_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | /* |
| 203 | * Hash table mapping origin volumes to lists of snapshots and |
| 204 | * a lock to protect it |
| 205 | */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 206 | static struct kmem_cache *exception_cache; |
| 207 | static struct kmem_cache *pending_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 209 | struct dm_snap_tracked_chunk { |
| 210 | struct hlist_node node; |
| 211 | chunk_t chunk; |
| 212 | }; |
| 213 | |
| 214 | static struct kmem_cache *tracked_chunk_cache; |
| 215 | |
| 216 | static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s, |
| 217 | chunk_t chunk) |
| 218 | { |
| 219 | struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool, |
| 220 | GFP_NOIO); |
| 221 | unsigned long flags; |
| 222 | |
| 223 | c->chunk = chunk; |
| 224 | |
| 225 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); |
| 226 | hlist_add_head(&c->node, |
| 227 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]); |
| 228 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); |
| 229 | |
| 230 | return c; |
| 231 | } |
| 232 | |
| 233 | static void stop_tracking_chunk(struct dm_snapshot *s, |
| 234 | struct dm_snap_tracked_chunk *c) |
| 235 | { |
| 236 | unsigned long flags; |
| 237 | |
| 238 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); |
| 239 | hlist_del(&c->node); |
| 240 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); |
| 241 | |
| 242 | mempool_free(c, s->tracked_chunk_pool); |
| 243 | } |
| 244 | |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 245 | static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk) |
| 246 | { |
| 247 | struct dm_snap_tracked_chunk *c; |
| 248 | struct hlist_node *hn; |
| 249 | int found = 0; |
| 250 | |
| 251 | spin_lock_irq(&s->tracked_chunk_lock); |
| 252 | |
| 253 | hlist_for_each_entry(c, hn, |
| 254 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) { |
| 255 | if (c->chunk == chunk) { |
| 256 | found = 1; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | spin_unlock_irq(&s->tracked_chunk_lock); |
| 262 | |
| 263 | return found; |
| 264 | } |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | /* |
Mike Snitzer | 615d1eb | 2009-12-10 23:52:29 +0000 | [diff] [blame] | 267 | * This conflicting I/O is extremely improbable in the caller, |
| 268 | * so msleep(1) is sufficient and there is no need for a wait queue. |
| 269 | */ |
| 270 | static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk) |
| 271 | { |
| 272 | while (__chunk_is_tracked(s, chunk)) |
| 273 | msleep(1); |
| 274 | } |
| 275 | |
| 276 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | * One of these per registered origin, held in the snapshot_origins hash |
| 278 | */ |
| 279 | struct origin { |
| 280 | /* The origin device */ |
| 281 | struct block_device *bdev; |
| 282 | |
| 283 | struct list_head hash_list; |
| 284 | |
| 285 | /* List of snapshots for this origin */ |
| 286 | struct list_head snapshots; |
| 287 | }; |
| 288 | |
| 289 | /* |
| 290 | * Size of the hash table for origin volumes. If we make this |
| 291 | * the size of the minors list then it should be nearly perfect |
| 292 | */ |
| 293 | #define ORIGIN_HASH_SIZE 256 |
| 294 | #define ORIGIN_MASK 0xFF |
| 295 | static struct list_head *_origins; |
| 296 | static struct rw_semaphore _origins_lock; |
| 297 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 298 | static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done); |
| 299 | static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock); |
| 300 | static uint64_t _pending_exceptions_done_count; |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | static int init_origin_hash(void) |
| 303 | { |
| 304 | int i; |
| 305 | |
| 306 | _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head), |
| 307 | GFP_KERNEL); |
| 308 | if (!_origins) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 309 | DMERR("unable to allocate memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | return -ENOMEM; |
| 311 | } |
| 312 | |
| 313 | for (i = 0; i < ORIGIN_HASH_SIZE; i++) |
| 314 | INIT_LIST_HEAD(_origins + i); |
| 315 | init_rwsem(&_origins_lock); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void exit_origin_hash(void) |
| 321 | { |
| 322 | kfree(_origins); |
| 323 | } |
| 324 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 325 | static unsigned origin_hash(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | return bdev->bd_dev & ORIGIN_MASK; |
| 328 | } |
| 329 | |
| 330 | static struct origin *__lookup_origin(struct block_device *origin) |
| 331 | { |
| 332 | struct list_head *ol; |
| 333 | struct origin *o; |
| 334 | |
| 335 | ol = &_origins[origin_hash(origin)]; |
| 336 | list_for_each_entry (o, ol, hash_list) |
| 337 | if (bdev_equal(o->bdev, origin)) |
| 338 | return o; |
| 339 | |
| 340 | return NULL; |
| 341 | } |
| 342 | |
| 343 | static void __insert_origin(struct origin *o) |
| 344 | { |
| 345 | struct list_head *sl = &_origins[origin_hash(o->bdev)]; |
| 346 | list_add_tail(&o->hash_list, sl); |
| 347 | } |
| 348 | |
| 349 | /* |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 350 | * _origins_lock must be held when calling this function. |
| 351 | * Returns number of snapshots registered using the supplied cow device, plus: |
| 352 | * snap_src - a snapshot suitable for use as a source of exception handover |
| 353 | * snap_dest - a snapshot capable of receiving exception handover. |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 354 | * snap_merge - an existing snapshot-merge target linked to the same origin. |
| 355 | * There can be at most one snapshot-merge target. The parameter is optional. |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 356 | * |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 357 | * Possible return values and states of snap_src and snap_dest. |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 358 | * 0: NULL, NULL - first new snapshot |
| 359 | * 1: snap_src, NULL - normal snapshot |
| 360 | * 2: snap_src, snap_dest - waiting for handover |
| 361 | * 2: snap_src, NULL - handed over, waiting for old to be deleted |
| 362 | * 1: NULL, snap_dest - source got destroyed without handover |
| 363 | */ |
| 364 | static int __find_snapshots_sharing_cow(struct dm_snapshot *snap, |
| 365 | struct dm_snapshot **snap_src, |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 366 | struct dm_snapshot **snap_dest, |
| 367 | struct dm_snapshot **snap_merge) |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 368 | { |
| 369 | struct dm_snapshot *s; |
| 370 | struct origin *o; |
| 371 | int count = 0; |
| 372 | int active; |
| 373 | |
| 374 | o = __lookup_origin(snap->origin->bdev); |
| 375 | if (!o) |
| 376 | goto out; |
| 377 | |
| 378 | list_for_each_entry(s, &o->snapshots, list) { |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 379 | if (dm_target_is_snapshot_merge(s->ti) && snap_merge) |
| 380 | *snap_merge = s; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 381 | if (!bdev_equal(s->cow->bdev, snap->cow->bdev)) |
| 382 | continue; |
| 383 | |
| 384 | down_read(&s->lock); |
| 385 | active = s->active; |
| 386 | up_read(&s->lock); |
| 387 | |
| 388 | if (active) { |
| 389 | if (snap_src) |
| 390 | *snap_src = s; |
| 391 | } else if (snap_dest) |
| 392 | *snap_dest = s; |
| 393 | |
| 394 | count++; |
| 395 | } |
| 396 | |
| 397 | out: |
| 398 | return count; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * On success, returns 1 if this snapshot is a handover destination, |
| 403 | * otherwise returns 0. |
| 404 | */ |
| 405 | static int __validate_exception_handover(struct dm_snapshot *snap) |
| 406 | { |
| 407 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 408 | struct dm_snapshot *snap_merge = NULL; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 409 | |
| 410 | /* Does snapshot need exceptions handed over to it? */ |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 411 | if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest, |
| 412 | &snap_merge) == 2) || |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 413 | snap_dest) { |
| 414 | snap->ti->error = "Snapshot cow pairing for exception " |
| 415 | "table handover failed"; |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * If no snap_src was found, snap cannot become a handover |
| 421 | * destination. |
| 422 | */ |
| 423 | if (!snap_src) |
| 424 | return 0; |
| 425 | |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 426 | /* |
| 427 | * Non-snapshot-merge handover? |
| 428 | */ |
| 429 | if (!dm_target_is_snapshot_merge(snap->ti)) |
| 430 | return 1; |
| 431 | |
| 432 | /* |
| 433 | * Do not allow more than one merging snapshot. |
| 434 | */ |
| 435 | if (snap_merge) { |
| 436 | snap->ti->error = "A snapshot is already merging."; |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 440 | if (!snap_src->store->type->prepare_merge || |
| 441 | !snap_src->store->type->commit_merge) { |
| 442 | snap->ti->error = "Snapshot exception store does not " |
| 443 | "support snapshot-merge."; |
| 444 | return -EINVAL; |
| 445 | } |
| 446 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | static void __insert_snapshot(struct origin *o, struct dm_snapshot *s) |
| 451 | { |
| 452 | struct dm_snapshot *l; |
| 453 | |
| 454 | /* Sort the list according to chunk size, largest-first smallest-last */ |
| 455 | list_for_each_entry(l, &o->snapshots, list) |
| 456 | if (l->store->chunk_size < s->store->chunk_size) |
| 457 | break; |
| 458 | list_add_tail(&s->list, &l->list); |
| 459 | } |
| 460 | |
| 461 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | * Make a note of the snapshot and its origin so we can look it |
| 463 | * up when the origin has a write on it. |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 464 | * |
| 465 | * Also validate snapshot exception store handovers. |
| 466 | * On success, returns 1 if this registration is a handover destination, |
| 467 | * otherwise returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | */ |
| 469 | static int register_snapshot(struct dm_snapshot *snap) |
| 470 | { |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 471 | struct origin *o, *new_o = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | struct block_device *bdev = snap->origin->bdev; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 473 | int r = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 475 | new_o = kmalloc(sizeof(*new_o), GFP_KERNEL); |
| 476 | if (!new_o) |
| 477 | return -ENOMEM; |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | down_write(&_origins_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 481 | r = __validate_exception_handover(snap); |
| 482 | if (r < 0) { |
| 483 | kfree(new_o); |
| 484 | goto out; |
| 485 | } |
| 486 | |
| 487 | o = __lookup_origin(bdev); |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 488 | if (o) |
| 489 | kfree(new_o); |
| 490 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /* New origin */ |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 492 | o = new_o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | /* Initialise the struct */ |
| 495 | INIT_LIST_HEAD(&o->snapshots); |
| 496 | o->bdev = bdev; |
| 497 | |
| 498 | __insert_origin(o); |
| 499 | } |
| 500 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 501 | __insert_snapshot(o, snap); |
| 502 | |
| 503 | out: |
| 504 | up_write(&_origins_lock); |
| 505 | |
| 506 | return r; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Move snapshot to correct place in list according to chunk size. |
| 511 | */ |
| 512 | static void reregister_snapshot(struct dm_snapshot *s) |
| 513 | { |
| 514 | struct block_device *bdev = s->origin->bdev; |
| 515 | |
| 516 | down_write(&_origins_lock); |
| 517 | |
| 518 | list_del(&s->list); |
| 519 | __insert_snapshot(__lookup_origin(bdev), s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | up_write(&_origins_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | static void unregister_snapshot(struct dm_snapshot *s) |
| 525 | { |
| 526 | struct origin *o; |
| 527 | |
| 528 | down_write(&_origins_lock); |
| 529 | o = __lookup_origin(s->origin->bdev); |
| 530 | |
| 531 | list_del(&s->list); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 532 | if (o && list_empty(&o->snapshots)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | list_del(&o->hash_list); |
| 534 | kfree(o); |
| 535 | } |
| 536 | |
| 537 | up_write(&_origins_lock); |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * Implementation of the exception hash tables. |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 542 | * The lowest hash_shift bits of the chunk number are ignored, allowing |
| 543 | * some consecutive chunks to be grouped together. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 545 | static int dm_exception_table_init(struct dm_exception_table *et, |
| 546 | uint32_t size, unsigned hash_shift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
| 548 | unsigned int i; |
| 549 | |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 550 | et->hash_shift = hash_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | et->hash_mask = size - 1; |
| 552 | et->table = dm_vcalloc(size, sizeof(struct list_head)); |
| 553 | if (!et->table) |
| 554 | return -ENOMEM; |
| 555 | |
| 556 | for (i = 0; i < size; i++) |
| 557 | INIT_LIST_HEAD(et->table + i); |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 562 | static void dm_exception_table_exit(struct dm_exception_table *et, |
| 563 | struct kmem_cache *mem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | struct list_head *slot; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 566 | struct dm_exception *ex, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | int i, size; |
| 568 | |
| 569 | size = et->hash_mask + 1; |
| 570 | for (i = 0; i < size; i++) { |
| 571 | slot = et->table + i; |
| 572 | |
| 573 | list_for_each_entry_safe (ex, next, slot, hash_list) |
| 574 | kmem_cache_free(mem, ex); |
| 575 | } |
| 576 | |
| 577 | vfree(et->table); |
| 578 | } |
| 579 | |
Jon Brassow | 191437a | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 580 | 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] | 581 | { |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 582 | return (chunk >> et->hash_shift) & et->hash_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 585 | static void dm_remove_exception(struct dm_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { |
| 587 | list_del(&e->hash_list); |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Return the exception data for a sector, or NULL if not |
| 592 | * remapped. |
| 593 | */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 594 | static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et, |
| 595 | chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
| 597 | struct list_head *slot; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 598 | struct dm_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | slot = &et->table[exception_hash(et, chunk)]; |
| 601 | list_for_each_entry (e, slot, hash_list) |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 602 | if (chunk >= e->old_chunk && |
| 603 | chunk <= e->old_chunk + dm_consecutive_chunk_count(e)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | return e; |
| 605 | |
| 606 | return NULL; |
| 607 | } |
| 608 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 609 | static struct dm_exception *alloc_completed_exception(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 611 | struct dm_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | e = kmem_cache_alloc(exception_cache, GFP_NOIO); |
| 614 | if (!e) |
| 615 | e = kmem_cache_alloc(exception_cache, GFP_ATOMIC); |
| 616 | |
| 617 | return e; |
| 618 | } |
| 619 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 620 | static void free_completed_exception(struct dm_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
| 622 | kmem_cache_free(exception_cache, e); |
| 623 | } |
| 624 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 625 | 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] | 626 | { |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 627 | struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool, |
| 628 | GFP_NOIO); |
| 629 | |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 630 | atomic_inc(&s->pending_exceptions_count); |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 631 | pe->snap = s; |
| 632 | |
| 633 | return pe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 636 | static void free_pending_exception(struct dm_snap_pending_exception *pe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 638 | struct dm_snapshot *s = pe->snap; |
| 639 | |
| 640 | mempool_free(pe, s->pending_pool); |
| 641 | smp_mb__before_atomic_dec(); |
| 642 | atomic_dec(&s->pending_exceptions_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 645 | static void dm_insert_exception(struct dm_exception_table *eh, |
| 646 | struct dm_exception *new_e) |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 647 | { |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 648 | struct list_head *l; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 649 | struct dm_exception *e = NULL; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 650 | |
| 651 | l = &eh->table[exception_hash(eh, new_e->old_chunk)]; |
| 652 | |
| 653 | /* Add immediately if this table doesn't support consecutive chunks */ |
| 654 | if (!eh->hash_shift) |
| 655 | goto out; |
| 656 | |
| 657 | /* List is ordered by old_chunk */ |
| 658 | list_for_each_entry_reverse(e, l, hash_list) { |
| 659 | /* Insert after an existing chunk? */ |
| 660 | if (new_e->old_chunk == (e->old_chunk + |
| 661 | dm_consecutive_chunk_count(e) + 1) && |
| 662 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) + |
| 663 | dm_consecutive_chunk_count(e) + 1)) { |
| 664 | dm_consecutive_chunk_count_inc(e); |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 665 | free_completed_exception(new_e); |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | |
| 669 | /* Insert before an existing chunk? */ |
| 670 | if (new_e->old_chunk == (e->old_chunk - 1) && |
| 671 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) { |
| 672 | dm_consecutive_chunk_count_inc(e); |
| 673 | e->old_chunk--; |
| 674 | e->new_chunk--; |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 675 | free_completed_exception(new_e); |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 676 | return; |
| 677 | } |
| 678 | |
| 679 | if (new_e->old_chunk > e->old_chunk) |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | out: |
| 684 | list_add(&new_e->hash_list, e ? &e->hash_list : l); |
| 685 | } |
| 686 | |
Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 687 | /* |
| 688 | * Callback used by the exception stores to load exceptions when |
| 689 | * initialising. |
| 690 | */ |
| 691 | 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] | 692 | { |
Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 693 | struct dm_snapshot *s = context; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 694 | struct dm_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 696 | e = alloc_completed_exception(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | if (!e) |
| 698 | return -ENOMEM; |
| 699 | |
| 700 | e->old_chunk = old; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 701 | |
| 702 | /* Consecutive_count is implicitly initialised to zero */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | e->new_chunk = new; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 704 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 705 | dm_insert_exception(&s->complete, e); |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
Mikulas Patocka | 7e201b3 | 2009-12-10 23:52:08 +0000 | [diff] [blame] | 710 | /* |
| 711 | * Return a minimum chunk size of all snapshots that have the specified origin. |
| 712 | * Return zero if the origin has no snapshots. |
| 713 | */ |
| 714 | static sector_t __minimum_chunk_size(struct origin *o) |
| 715 | { |
| 716 | struct dm_snapshot *snap; |
| 717 | unsigned chunk_size = 0; |
| 718 | |
| 719 | if (o) |
| 720 | list_for_each_entry(snap, &o->snapshots, list) |
| 721 | chunk_size = min_not_zero(chunk_size, |
| 722 | snap->store->chunk_size); |
| 723 | |
| 724 | return chunk_size; |
| 725 | } |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | /* |
| 728 | * Hard coded magic. |
| 729 | */ |
| 730 | static int calc_max_buckets(void) |
| 731 | { |
| 732 | /* use a fixed size of 2MB */ |
| 733 | unsigned long mem = 2 * 1024 * 1024; |
| 734 | mem /= sizeof(struct list_head); |
| 735 | |
| 736 | return mem; |
| 737 | } |
| 738 | |
| 739 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | * Allocate room for a suitable hash table. |
| 741 | */ |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 742 | static int init_hash_tables(struct dm_snapshot *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
Mikulas Patocka | 92906e6 | 2013-09-18 19:40:42 -0400 | [diff] [blame] | 744 | sector_t hash_size, cow_dev_size, max_buckets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * Calculate based on the size of the original volume or |
| 748 | * the COW volume... |
| 749 | */ |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 750 | cow_dev_size = get_dev_size(s->cow->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | max_buckets = calc_max_buckets(); |
| 752 | |
Mikulas Patocka | 92906e6 | 2013-09-18 19:40:42 -0400 | [diff] [blame] | 753 | hash_size = cow_dev_size >> s->store->chunk_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | hash_size = min(hash_size, max_buckets); |
| 755 | |
Mikulas Patocka | 8e87b9b | 2009-12-10 23:51:54 +0000 | [diff] [blame] | 756 | if (hash_size < 64) |
| 757 | hash_size = 64; |
Robert P. J. Day | 8defd83 | 2008-02-08 02:10:06 +0000 | [diff] [blame] | 758 | hash_size = rounddown_pow_of_two(hash_size); |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 759 | if (dm_exception_table_init(&s->complete, hash_size, |
| 760 | DM_CHUNK_CONSECUTIVE_BITS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return -ENOMEM; |
| 762 | |
| 763 | /* |
| 764 | * Allocate hash table for in-flight exceptions |
| 765 | * Make this smaller than the real hash table |
| 766 | */ |
| 767 | hash_size >>= 3; |
| 768 | if (hash_size < 64) |
| 769 | hash_size = 64; |
| 770 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 771 | if (dm_exception_table_init(&s->pending, hash_size, 0)) { |
| 772 | dm_exception_table_exit(&s->complete, exception_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | return -ENOMEM; |
| 774 | } |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 779 | static void merge_shutdown(struct dm_snapshot *s) |
| 780 | { |
| 781 | clear_bit_unlock(RUNNING_MERGE, &s->state_bits); |
| 782 | smp_mb__after_clear_bit(); |
| 783 | wake_up_bit(&s->state_bits, RUNNING_MERGE); |
| 784 | } |
| 785 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 786 | static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s) |
| 787 | { |
| 788 | s->first_merging_chunk = 0; |
| 789 | s->num_merging_chunks = 0; |
| 790 | |
| 791 | return bio_list_get(&s->bios_queued_during_merge); |
| 792 | } |
| 793 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 794 | /* |
| 795 | * Remove one chunk from the index of completed exceptions. |
| 796 | */ |
| 797 | static int __remove_single_exception_chunk(struct dm_snapshot *s, |
| 798 | chunk_t old_chunk) |
| 799 | { |
| 800 | struct dm_exception *e; |
| 801 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 802 | e = dm_lookup_exception(&s->complete, old_chunk); |
| 803 | if (!e) { |
| 804 | DMERR("Corruption detected: exception for block %llu is " |
| 805 | "on disk but not in memory", |
| 806 | (unsigned long long)old_chunk); |
| 807 | return -EINVAL; |
| 808 | } |
| 809 | |
| 810 | /* |
| 811 | * If this is the only chunk using this exception, remove exception. |
| 812 | */ |
| 813 | if (!dm_consecutive_chunk_count(e)) { |
| 814 | dm_remove_exception(e); |
| 815 | free_completed_exception(e); |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * The chunk may be either at the beginning or the end of a |
| 821 | * group of consecutive chunks - never in the middle. We are |
| 822 | * removing chunks in the opposite order to that in which they |
| 823 | * were added, so this should always be true. |
| 824 | * Decrement the consecutive chunk counter and adjust the |
| 825 | * starting point if necessary. |
| 826 | */ |
| 827 | if (old_chunk == e->old_chunk) { |
| 828 | e->old_chunk++; |
| 829 | e->new_chunk++; |
| 830 | } else if (old_chunk != e->old_chunk + |
| 831 | dm_consecutive_chunk_count(e)) { |
| 832 | DMERR("Attempt to merge block %llu from the " |
| 833 | "middle of a chunk range [%llu - %llu]", |
| 834 | (unsigned long long)old_chunk, |
| 835 | (unsigned long long)e->old_chunk, |
| 836 | (unsigned long long) |
| 837 | e->old_chunk + dm_consecutive_chunk_count(e)); |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | |
| 841 | dm_consecutive_chunk_count_dec(e); |
| 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 846 | static void flush_bios(struct bio *bio); |
| 847 | |
| 848 | static int remove_single_exception_chunk(struct dm_snapshot *s) |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 849 | { |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 850 | struct bio *b = NULL; |
| 851 | int r; |
| 852 | chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 853 | |
| 854 | down_write(&s->lock); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 855 | |
| 856 | /* |
| 857 | * Process chunks (and associated exceptions) in reverse order |
| 858 | * so that dm_consecutive_chunk_count_dec() accounting works. |
| 859 | */ |
| 860 | do { |
| 861 | r = __remove_single_exception_chunk(s, old_chunk); |
| 862 | if (r) |
| 863 | goto out; |
| 864 | } while (old_chunk-- > s->first_merging_chunk); |
| 865 | |
| 866 | b = __release_queued_bios_after_merge(s); |
| 867 | |
| 868 | out: |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 869 | up_write(&s->lock); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 870 | if (b) |
| 871 | flush_bios(b); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 872 | |
| 873 | return r; |
| 874 | } |
| 875 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 876 | static int origin_write_extent(struct dm_snapshot *merging_snap, |
| 877 | sector_t sector, unsigned chunk_size); |
| 878 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 879 | static void merge_callback(int read_err, unsigned long write_err, |
| 880 | void *context); |
| 881 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 882 | static uint64_t read_pending_exceptions_done_count(void) |
| 883 | { |
| 884 | uint64_t pending_exceptions_done; |
| 885 | |
| 886 | spin_lock(&_pending_exceptions_done_spinlock); |
| 887 | pending_exceptions_done = _pending_exceptions_done_count; |
| 888 | spin_unlock(&_pending_exceptions_done_spinlock); |
| 889 | |
| 890 | return pending_exceptions_done; |
| 891 | } |
| 892 | |
| 893 | static void increment_pending_exceptions_done_count(void) |
| 894 | { |
| 895 | spin_lock(&_pending_exceptions_done_spinlock); |
| 896 | _pending_exceptions_done_count++; |
| 897 | spin_unlock(&_pending_exceptions_done_spinlock); |
| 898 | |
| 899 | wake_up_all(&_pending_exceptions_done); |
| 900 | } |
| 901 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 902 | static void snapshot_merge_next_chunks(struct dm_snapshot *s) |
| 903 | { |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 904 | int i, linear_chunks; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 905 | chunk_t old_chunk, new_chunk; |
| 906 | struct dm_io_region src, dest; |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 907 | sector_t io_size; |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 908 | uint64_t previous_count; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 909 | |
| 910 | BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits)); |
| 911 | if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits))) |
| 912 | goto shut; |
| 913 | |
| 914 | /* |
| 915 | * valid flag never changes during merge, so no lock required. |
| 916 | */ |
| 917 | if (!s->valid) { |
| 918 | DMERR("Snapshot is invalid: can't merge"); |
| 919 | goto shut; |
| 920 | } |
| 921 | |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 922 | linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk, |
| 923 | &new_chunk); |
| 924 | if (linear_chunks <= 0) { |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 925 | if (linear_chunks < 0) { |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 926 | DMERR("Read error in exception store: " |
| 927 | "shutting down merge"); |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 928 | down_write(&s->lock); |
| 929 | s->merge_failed = 1; |
| 930 | up_write(&s->lock); |
| 931 | } |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 932 | goto shut; |
| 933 | } |
| 934 | |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 935 | /* Adjust old_chunk and new_chunk to reflect start of linear region */ |
| 936 | old_chunk = old_chunk + 1 - linear_chunks; |
| 937 | new_chunk = new_chunk + 1 - linear_chunks; |
| 938 | |
| 939 | /* |
| 940 | * Use one (potentially large) I/O to copy all 'linear_chunks' |
| 941 | * from the exception store to the origin |
| 942 | */ |
| 943 | io_size = linear_chunks * s->store->chunk_size; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 944 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 945 | dest.bdev = s->origin->bdev; |
| 946 | dest.sector = chunk_to_sector(s->store, old_chunk); |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 947 | dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 948 | |
| 949 | src.bdev = s->cow->bdev; |
| 950 | src.sector = chunk_to_sector(s->store, new_chunk); |
| 951 | src.count = dest.count; |
| 952 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 953 | /* |
| 954 | * Reallocate any exceptions needed in other snapshots then |
| 955 | * wait for the pending exceptions to complete. |
| 956 | * Each time any pending exception (globally on the system) |
| 957 | * completes we are woken and repeat the process to find out |
| 958 | * if we can proceed. While this may not seem a particularly |
| 959 | * efficient algorithm, it is not expected to have any |
| 960 | * significant impact on performance. |
| 961 | */ |
| 962 | previous_count = read_pending_exceptions_done_count(); |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 963 | while (origin_write_extent(s, dest.sector, io_size)) { |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 964 | wait_event(_pending_exceptions_done, |
| 965 | (read_pending_exceptions_done_count() != |
| 966 | previous_count)); |
| 967 | /* Retry after the wait, until all exceptions are done. */ |
| 968 | previous_count = read_pending_exceptions_done_count(); |
| 969 | } |
| 970 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 971 | down_write(&s->lock); |
| 972 | s->first_merging_chunk = old_chunk; |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 973 | s->num_merging_chunks = linear_chunks; |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 974 | up_write(&s->lock); |
| 975 | |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 976 | /* Wait until writes to all 'linear_chunks' drain */ |
| 977 | for (i = 0; i < linear_chunks; i++) |
| 978 | __check_for_conflicting_io(s, old_chunk + i); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 979 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 980 | dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s); |
| 981 | return; |
| 982 | |
| 983 | shut: |
| 984 | merge_shutdown(s); |
| 985 | } |
| 986 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 987 | static void error_bios(struct bio *bio); |
| 988 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 989 | static void merge_callback(int read_err, unsigned long write_err, void *context) |
| 990 | { |
| 991 | struct dm_snapshot *s = context; |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 992 | struct bio *b = NULL; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 993 | |
| 994 | if (read_err || write_err) { |
| 995 | if (read_err) |
| 996 | DMERR("Read error: shutting down merge."); |
| 997 | else |
| 998 | DMERR("Write error: shutting down merge."); |
| 999 | goto shut; |
| 1000 | } |
| 1001 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1002 | if (s->store->type->commit_merge(s->store, |
| 1003 | s->num_merging_chunks) < 0) { |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1004 | DMERR("Write error in exception store: shutting down merge"); |
| 1005 | goto shut; |
| 1006 | } |
| 1007 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1008 | if (remove_single_exception_chunk(s) < 0) |
| 1009 | goto shut; |
| 1010 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1011 | snapshot_merge_next_chunks(s); |
| 1012 | |
| 1013 | return; |
| 1014 | |
| 1015 | shut: |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1016 | down_write(&s->lock); |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 1017 | s->merge_failed = 1; |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1018 | b = __release_queued_bios_after_merge(s); |
| 1019 | up_write(&s->lock); |
| 1020 | error_bios(b); |
| 1021 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1022 | merge_shutdown(s); |
| 1023 | } |
| 1024 | |
| 1025 | static void start_merge(struct dm_snapshot *s) |
| 1026 | { |
| 1027 | if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits)) |
| 1028 | snapshot_merge_next_chunks(s); |
| 1029 | } |
| 1030 | |
| 1031 | static int wait_schedule(void *ptr) |
| 1032 | { |
| 1033 | schedule(); |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * Stop the merging process and wait until it finishes. |
| 1040 | */ |
| 1041 | static void stop_merge(struct dm_snapshot *s) |
| 1042 | { |
| 1043 | set_bit(SHUTDOWN_MERGE, &s->state_bits); |
| 1044 | wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule, |
| 1045 | TASK_UNINTERRUPTIBLE); |
| 1046 | clear_bit(SHUTDOWN_MERGE, &s->state_bits); |
| 1047 | } |
| 1048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size> |
| 1051 | */ |
| 1052 | static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1053 | { |
| 1054 | struct dm_snapshot *s; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1055 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | int r = -EINVAL; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1057 | char *origin_path, *cow_path; |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1058 | unsigned args_used, num_flush_requests = 1; |
| 1059 | fmode_t origin_mode = FMODE_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 1061 | if (argc != 4) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1062 | ti->error = "requires exactly 4 arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | r = -EINVAL; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1064 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1067 | if (dm_target_is_snapshot_merge(ti)) { |
| 1068 | num_flush_requests = 2; |
| 1069 | origin_mode = FMODE_WRITE; |
| 1070 | } |
| 1071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1073 | if (!s) { |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1074 | ti->error = "Cannot allocate private snapshot structure"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | r = -ENOMEM; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1076 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 1079 | origin_path = argv[0]; |
| 1080 | argv++; |
| 1081 | argc--; |
| 1082 | |
| 1083 | r = dm_get_device(ti, origin_path, origin_mode, &s->origin); |
| 1084 | if (r) { |
| 1085 | ti->error = "Cannot get origin device"; |
| 1086 | goto bad_origin; |
| 1087 | } |
| 1088 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1089 | cow_path = argv[0]; |
| 1090 | argv++; |
| 1091 | argc--; |
| 1092 | |
Milan Broz | 024d37e | 2011-03-24 13:52:14 +0000 | [diff] [blame] | 1093 | r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow); |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1094 | if (r) { |
| 1095 | ti->error = "Cannot get COW device"; |
| 1096 | goto bad_cow; |
| 1097 | } |
| 1098 | |
| 1099 | r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store); |
| 1100 | if (r) { |
| 1101 | ti->error = "Couldn't create exception store"; |
| 1102 | r = -EINVAL; |
| 1103 | goto bad_store; |
| 1104 | } |
| 1105 | |
| 1106 | argv += args_used; |
| 1107 | argc -= args_used; |
| 1108 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1109 | s->ti = ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | s->valid = 1; |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 1111 | s->active = 0; |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 1112 | atomic_set(&s->pending_exceptions_count, 0); |
Mikulas Patocka | 4c0c59e | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1113 | s->exception_start_sequence = 0; |
| 1114 | s->exception_complete_sequence = 0; |
| 1115 | INIT_LIST_HEAD(&s->out_of_order_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | init_rwsem(&s->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1117 | INIT_LIST_HEAD(&s->list); |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1118 | spin_lock_init(&s->pe_lock); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1119 | s->state_bits = 0; |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 1120 | s->merge_failed = 0; |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1121 | s->first_merging_chunk = 0; |
| 1122 | s->num_merging_chunks = 0; |
| 1123 | bio_list_init(&s->bios_queued_during_merge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
| 1125 | /* Allocate hash table for COW data */ |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1126 | if (init_hash_tables(s)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | ti->error = "Unable to allocate hash table space"; |
| 1128 | r = -ENOMEM; |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1129 | goto bad_hash_tables; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Mikulas Patocka | fa34ce7 | 2011-05-29 13:03:13 +0100 | [diff] [blame] | 1132 | s->kcopyd_client = dm_kcopyd_client_create(); |
| 1133 | if (IS_ERR(s->kcopyd_client)) { |
| 1134 | r = PTR_ERR(s->kcopyd_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | ti->error = "Could not create kcopyd client"; |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1136 | goto bad_kcopyd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1139 | s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache); |
| 1140 | if (!s->pending_pool) { |
| 1141 | ti->error = "Could not allocate mempool for pending exceptions"; |
Wei Yongjun | 18b4308 | 2013-05-10 14:37:15 +0100 | [diff] [blame] | 1142 | r = -ENOMEM; |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1143 | goto bad_pending_pool; |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1144 | } |
| 1145 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1146 | s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS, |
| 1147 | tracked_chunk_cache); |
| 1148 | if (!s->tracked_chunk_pool) { |
| 1149 | ti->error = "Could not allocate tracked_chunk mempool for " |
| 1150 | "tracking reads"; |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1151 | goto bad_tracked_chunk_pool; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) |
| 1155 | INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]); |
| 1156 | |
| 1157 | spin_lock_init(&s->tracked_chunk_lock); |
| 1158 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1159 | ti->private = s; |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1160 | ti->num_flush_requests = num_flush_requests; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1161 | |
| 1162 | /* Add snapshot to the list of snapshots for this origin */ |
| 1163 | /* Exceptions aren't triggered till snapshot_resume() is called */ |
| 1164 | r = register_snapshot(s); |
| 1165 | if (r == -ENOMEM) { |
| 1166 | ti->error = "Snapshot origin struct allocation failed"; |
| 1167 | goto bad_load_and_register; |
| 1168 | } else if (r < 0) { |
| 1169 | /* invalid handover, register_snapshot has set ti->error */ |
| 1170 | goto bad_load_and_register; |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * Metadata must only be loaded into one table at once, so skip this |
| 1175 | * if metadata will be handed over during resume. |
| 1176 | * Chunk size will be set during the handover - set it to zero to |
| 1177 | * ensure it's ignored. |
| 1178 | */ |
| 1179 | if (r > 0) { |
| 1180 | s->store->chunk_size = 0; |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1184 | r = s->store->type->read_metadata(s->store, dm_add_exception, |
| 1185 | (void *)s); |
Milan Broz | 0764147 | 2007-07-12 17:28:13 +0100 | [diff] [blame] | 1186 | if (r < 0) { |
Mark McLoughlin | f9cea4f | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 1187 | ti->error = "Failed to read snapshot metadata"; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1188 | goto bad_read_metadata; |
Milan Broz | 0764147 | 2007-07-12 17:28:13 +0100 | [diff] [blame] | 1189 | } else if (r > 0) { |
| 1190 | s->valid = 0; |
| 1191 | DMWARN("Snapshot is marked invalid."); |
Mark McLoughlin | f9cea4f | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 1192 | } |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 1193 | |
Mikulas Patocka | 3f2412d | 2009-10-16 23:18:16 +0100 | [diff] [blame] | 1194 | if (!s->store->chunk_size) { |
| 1195 | ti->error = "Chunk size not set"; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1196 | goto bad_read_metadata; |
Mikulas Patocka | 3f2412d | 2009-10-16 23:18:16 +0100 | [diff] [blame] | 1197 | } |
Jonathan Brassow | d021684 | 2009-04-02 19:55:32 +0100 | [diff] [blame] | 1198 | ti->split_io = s->store->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
| 1200 | return 0; |
| 1201 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1202 | bad_read_metadata: |
| 1203 | unregister_snapshot(s); |
| 1204 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1205 | bad_load_and_register: |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1206 | mempool_destroy(s->tracked_chunk_pool); |
| 1207 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1208 | bad_tracked_chunk_pool: |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1209 | mempool_destroy(s->pending_pool); |
| 1210 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1211 | bad_pending_pool: |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1212 | dm_kcopyd_client_destroy(s->kcopyd_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1214 | bad_kcopyd: |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1215 | dm_exception_table_exit(&s->pending, pending_cache); |
| 1216 | dm_exception_table_exit(&s->complete, exception_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1218 | bad_hash_tables: |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1219 | dm_exception_store_destroy(s->store); |
| 1220 | |
| 1221 | bad_store: |
| 1222 | dm_put_device(ti, s->cow); |
| 1223 | |
| 1224 | bad_cow: |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 1225 | dm_put_device(ti, s->origin); |
| 1226 | |
| 1227 | bad_origin: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | kfree(s); |
| 1229 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1230 | bad: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | return r; |
| 1232 | } |
| 1233 | |
Milan Broz | 31c93a0 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 1234 | static void __free_exceptions(struct dm_snapshot *s) |
| 1235 | { |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1236 | dm_kcopyd_client_destroy(s->kcopyd_client); |
Milan Broz | 31c93a0 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 1237 | s->kcopyd_client = NULL; |
| 1238 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1239 | dm_exception_table_exit(&s->pending, pending_cache); |
| 1240 | dm_exception_table_exit(&s->complete, exception_cache); |
Milan Broz | 31c93a0 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1243 | static void __handover_exceptions(struct dm_snapshot *snap_src, |
| 1244 | struct dm_snapshot *snap_dest) |
| 1245 | { |
| 1246 | union { |
| 1247 | struct dm_exception_table table_swap; |
| 1248 | struct dm_exception_store *store_swap; |
| 1249 | } u; |
| 1250 | |
| 1251 | /* |
| 1252 | * Swap all snapshot context information between the two instances. |
| 1253 | */ |
| 1254 | u.table_swap = snap_dest->complete; |
| 1255 | snap_dest->complete = snap_src->complete; |
| 1256 | snap_src->complete = u.table_swap; |
| 1257 | |
| 1258 | u.store_swap = snap_dest->store; |
| 1259 | snap_dest->store = snap_src->store; |
| 1260 | snap_src->store = u.store_swap; |
| 1261 | |
| 1262 | snap_dest->store->snap = snap_dest; |
| 1263 | snap_src->store->snap = snap_src; |
| 1264 | |
| 1265 | snap_dest->ti->split_io = snap_dest->store->chunk_size; |
| 1266 | snap_dest->valid = snap_src->valid; |
| 1267 | |
| 1268 | /* |
| 1269 | * Set source invalid to ensure it receives no further I/O. |
| 1270 | */ |
| 1271 | snap_src->valid = 0; |
| 1272 | } |
| 1273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | static void snapshot_dtr(struct dm_target *ti) |
| 1275 | { |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1276 | #ifdef CONFIG_DM_DEBUG |
| 1277 | int i; |
| 1278 | #endif |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1279 | struct dm_snapshot *s = ti->private; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1280 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1282 | down_read(&_origins_lock); |
| 1283 | /* Check whether exception handover must be cancelled */ |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1284 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1285 | if (snap_src && snap_dest && (s == snap_src)) { |
| 1286 | down_write(&snap_dest->lock); |
| 1287 | snap_dest->valid = 0; |
| 1288 | up_write(&snap_dest->lock); |
| 1289 | DMERR("Cancelling snapshot handover."); |
| 1290 | } |
| 1291 | up_read(&_origins_lock); |
| 1292 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1293 | if (dm_target_is_snapshot_merge(ti)) |
| 1294 | stop_merge(s); |
| 1295 | |
Alasdair G Kergon | 138728d | 2006-03-27 01:17:50 -0800 | [diff] [blame] | 1296 | /* Prevent further origin writes from using this snapshot. */ |
| 1297 | /* After this returns there can be no new kcopyd jobs. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | unregister_snapshot(s); |
| 1299 | |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 1300 | while (atomic_read(&s->pending_exceptions_count)) |
Mikulas Patocka | 90fa152 | 2009-01-06 03:04:54 +0000 | [diff] [blame] | 1301 | msleep(1); |
Mikulas Patocka | 879129d | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 1302 | /* |
| 1303 | * Ensure instructions in mempool_destroy aren't reordered |
| 1304 | * before atomic_read. |
| 1305 | */ |
| 1306 | smp_mb(); |
| 1307 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1308 | #ifdef CONFIG_DM_DEBUG |
| 1309 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) |
| 1310 | BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i])); |
| 1311 | #endif |
| 1312 | |
| 1313 | mempool_destroy(s->tracked_chunk_pool); |
| 1314 | |
Milan Broz | 31c93a0 | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 1315 | __free_exceptions(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1317 | mempool_destroy(s->pending_pool); |
| 1318 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1319 | dm_exception_store_destroy(s->store); |
Alasdair G Kergon | 138728d | 2006-03-27 01:17:50 -0800 | [diff] [blame] | 1320 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1321 | dm_put_device(ti, s->cow); |
| 1322 | |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 1323 | dm_put_device(ti, s->origin); |
| 1324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | kfree(s); |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Flush a list of buffers. |
| 1330 | */ |
| 1331 | static void flush_bios(struct bio *bio) |
| 1332 | { |
| 1333 | struct bio *n; |
| 1334 | |
| 1335 | while (bio) { |
| 1336 | n = bio->bi_next; |
| 1337 | bio->bi_next = NULL; |
| 1338 | generic_make_request(bio); |
| 1339 | bio = n; |
| 1340 | } |
| 1341 | } |
| 1342 | |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1343 | static int do_origin(struct dm_dev *origin, struct bio *bio); |
| 1344 | |
| 1345 | /* |
| 1346 | * Flush a list of buffers. |
| 1347 | */ |
| 1348 | static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio) |
| 1349 | { |
| 1350 | struct bio *n; |
| 1351 | int r; |
| 1352 | |
| 1353 | while (bio) { |
| 1354 | n = bio->bi_next; |
| 1355 | bio->bi_next = NULL; |
| 1356 | r = do_origin(s->origin, bio); |
| 1357 | if (r == DM_MAPIO_REMAPPED) |
| 1358 | generic_make_request(bio); |
| 1359 | bio = n; |
| 1360 | } |
| 1361 | } |
| 1362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | /* |
| 1364 | * Error a list of buffers. |
| 1365 | */ |
| 1366 | static void error_bios(struct bio *bio) |
| 1367 | { |
| 1368 | struct bio *n; |
| 1369 | |
| 1370 | while (bio) { |
| 1371 | n = bio->bi_next; |
| 1372 | bio->bi_next = NULL; |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1373 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | bio = n; |
| 1375 | } |
| 1376 | } |
| 1377 | |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 1378 | static void __invalidate_snapshot(struct dm_snapshot *s, int err) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1379 | { |
| 1380 | if (!s->valid) |
| 1381 | return; |
| 1382 | |
| 1383 | if (err == -EIO) |
| 1384 | DMERR("Invalidating snapshot: Error reading/writing."); |
| 1385 | else if (err == -ENOMEM) |
| 1386 | DMERR("Invalidating snapshot: Unable to allocate exception."); |
| 1387 | |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1388 | if (s->store->type->drop_snapshot) |
| 1389 | s->store->type->drop_snapshot(s->store); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1390 | |
| 1391 | s->valid = 0; |
| 1392 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1393 | dm_table_event(s->ti->table); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1396 | static void pending_complete(struct dm_snap_pending_exception *pe, int success) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | { |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 1398 | struct dm_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | struct dm_snapshot *s = pe->snap; |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1400 | struct bio *origin_bios = NULL; |
| 1401 | struct bio *snapshot_bios = NULL; |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1402 | struct bio *full_bio = NULL; |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1403 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1405 | if (!success) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | /* Read/write error - snapshot is unusable */ |
| 1407 | down_write(&s->lock); |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 1408 | __invalidate_snapshot(s, -EIO); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1409 | error = 1; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1410 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1413 | e = alloc_completed_exception(); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1414 | if (!e) { |
| 1415 | down_write(&s->lock); |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 1416 | __invalidate_snapshot(s, -ENOMEM); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1417 | error = 1; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1418 | goto out; |
| 1419 | } |
| 1420 | *e = pe->e; |
| 1421 | |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1422 | down_write(&s->lock); |
| 1423 | if (!s->valid) { |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1424 | free_completed_exception(e); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1425 | error = 1; |
| 1426 | goto out; |
| 1427 | } |
| 1428 | |
Mike Snitzer | 615d1eb | 2009-12-10 23:52:29 +0000 | [diff] [blame] | 1429 | /* Check for conflicting reads */ |
| 1430 | __check_for_conflicting_io(s, pe->e.old_chunk); |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 1431 | |
| 1432 | /* |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1433 | * Add a proper exception, and remove the |
| 1434 | * in-flight exception from the list. |
| 1435 | */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1436 | dm_insert_exception(&s->complete, e); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1437 | |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1438 | out: |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1439 | dm_remove_exception(&pe->e); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1440 | snapshot_bios = bio_list_get(&pe->snapshot_bios); |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1441 | origin_bios = bio_list_get(&pe->origin_bios); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1442 | full_bio = pe->full_bio; |
| 1443 | if (full_bio) { |
| 1444 | full_bio->bi_end_io = pe->full_bio_end_io; |
| 1445 | full_bio->bi_private = pe->full_bio_private; |
| 1446 | } |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1447 | free_pending_exception(pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1449 | increment_pending_exceptions_done_count(); |
| 1450 | |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1451 | up_write(&s->lock); |
| 1452 | |
| 1453 | /* Submit any pending write bios */ |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1454 | if (error) { |
| 1455 | if (full_bio) |
| 1456 | bio_io_error(full_bio); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1457 | error_bios(snapshot_bios); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1458 | } else { |
| 1459 | if (full_bio) |
| 1460 | bio_endio(full_bio, 0); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1461 | flush_bios(snapshot_bios); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1462 | } |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1463 | |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1464 | retry_origin_bios(s, origin_bios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | static void commit_callback(void *context, int success) |
| 1468 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1469 | struct dm_snap_pending_exception *pe = context; |
| 1470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | pending_complete(pe, success); |
| 1472 | } |
| 1473 | |
Mikulas Patocka | 4c0c59e | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1474 | static void complete_exception(struct dm_snap_pending_exception *pe) |
| 1475 | { |
| 1476 | struct dm_snapshot *s = pe->snap; |
| 1477 | |
| 1478 | if (unlikely(pe->copy_error)) |
| 1479 | pending_complete(pe, 0); |
| 1480 | |
| 1481 | else |
| 1482 | /* Update the metadata if we are persistent */ |
| 1483 | s->store->type->commit_exception(s->store, &pe->e, |
| 1484 | commit_callback, pe); |
| 1485 | } |
| 1486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | /* |
| 1488 | * Called when the copy I/O has finished. kcopyd actually runs |
| 1489 | * this code so don't block. |
| 1490 | */ |
Alasdair G Kergon | 4cdc1d1 | 2008-03-28 14:16:10 -0700 | [diff] [blame] | 1491 | 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] | 1492 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1493 | struct dm_snap_pending_exception *pe = context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | struct dm_snapshot *s = pe->snap; |
| 1495 | |
Mikulas Patocka | 4c0c59e | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1496 | pe->copy_error = read_err || write_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
Mikulas Patocka | 4c0c59e | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1498 | if (pe->exception_sequence == s->exception_complete_sequence) { |
| 1499 | s->exception_complete_sequence++; |
| 1500 | complete_exception(pe); |
| 1501 | |
| 1502 | while (!list_empty(&s->out_of_order_list)) { |
| 1503 | pe = list_entry(s->out_of_order_list.next, |
| 1504 | struct dm_snap_pending_exception, out_of_order_entry); |
| 1505 | if (pe->exception_sequence != s->exception_complete_sequence) |
| 1506 | break; |
| 1507 | s->exception_complete_sequence++; |
| 1508 | list_del(&pe->out_of_order_entry); |
| 1509 | complete_exception(pe); |
| 1510 | } |
| 1511 | } else { |
| 1512 | struct list_head *lh; |
| 1513 | struct dm_snap_pending_exception *pe2; |
| 1514 | |
| 1515 | list_for_each_prev(lh, &s->out_of_order_list) { |
| 1516 | pe2 = list_entry(lh, struct dm_snap_pending_exception, out_of_order_entry); |
| 1517 | if (pe2->exception_sequence < pe->exception_sequence) |
| 1518 | break; |
| 1519 | } |
| 1520 | list_add(&pe->out_of_order_entry, lh); |
| 1521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * Dispatches the copy operation to kcopyd. |
| 1526 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1527 | static void start_copy(struct dm_snap_pending_exception *pe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | { |
| 1529 | struct dm_snapshot *s = pe->snap; |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 1530 | struct dm_io_region src, dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | struct block_device *bdev = s->origin->bdev; |
| 1532 | sector_t dev_size; |
| 1533 | |
| 1534 | dev_size = get_dev_size(bdev); |
| 1535 | |
| 1536 | src.bdev = bdev; |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1537 | src.sector = chunk_to_sector(s->store, pe->e.old_chunk); |
Mikulas Patocka | df96eee | 2009-10-16 23:18:17 +0100 | [diff] [blame] | 1538 | 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] | 1539 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1540 | dest.bdev = s->cow->bdev; |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1541 | dest.sector = chunk_to_sector(s->store, pe->e.new_chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | dest.count = src.count; |
| 1543 | |
| 1544 | /* Hand over to kcopyd */ |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1545 | dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1548 | static void full_bio_end_io(struct bio *bio, int error) |
| 1549 | { |
| 1550 | void *callback_data = bio->bi_private; |
| 1551 | |
| 1552 | dm_kcopyd_do_callback(callback_data, 0, error ? 1 : 0); |
| 1553 | } |
| 1554 | |
| 1555 | static void start_full_bio(struct dm_snap_pending_exception *pe, |
| 1556 | struct bio *bio) |
| 1557 | { |
| 1558 | struct dm_snapshot *s = pe->snap; |
| 1559 | void *callback_data; |
| 1560 | |
| 1561 | pe->full_bio = bio; |
| 1562 | pe->full_bio_end_io = bio->bi_end_io; |
| 1563 | pe->full_bio_private = bio->bi_private; |
| 1564 | |
| 1565 | callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client, |
| 1566 | copy_callback, pe); |
| 1567 | |
| 1568 | bio->bi_end_io = full_bio_end_io; |
| 1569 | bio->bi_private = callback_data; |
| 1570 | |
| 1571 | generic_make_request(bio); |
| 1572 | } |
| 1573 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1574 | static struct dm_snap_pending_exception * |
| 1575 | __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk) |
| 1576 | { |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1577 | struct dm_exception *e = dm_lookup_exception(&s->pending, chunk); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1578 | |
| 1579 | if (!e) |
| 1580 | return NULL; |
| 1581 | |
| 1582 | return container_of(e, struct dm_snap_pending_exception, e); |
| 1583 | } |
| 1584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | /* |
| 1586 | * Looks to see if this snapshot already has a pending exception |
| 1587 | * for this chunk, otherwise it allocates a new one and inserts |
| 1588 | * it into the pending table. |
| 1589 | * |
| 1590 | * NOTE: a write lock must be held on snap->lock before calling |
| 1591 | * this. |
| 1592 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1593 | static struct dm_snap_pending_exception * |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1594 | __find_pending_exception(struct dm_snapshot *s, |
| 1595 | struct dm_snap_pending_exception *pe, chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | { |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1597 | struct dm_snap_pending_exception *pe2; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1598 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1599 | pe2 = __lookup_pending_exception(s, chunk); |
| 1600 | if (pe2) { |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1601 | free_pending_exception(pe); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1602 | return pe2; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | pe->e.old_chunk = chunk; |
| 1606 | bio_list_init(&pe->origin_bios); |
| 1607 | bio_list_init(&pe->snapshot_bios); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1608 | pe->started = 0; |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1609 | pe->full_bio = NULL; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1610 | |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1611 | if (s->store->type->prepare_exception(s->store, &pe->e)) { |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1612 | free_pending_exception(pe); |
| 1613 | return NULL; |
| 1614 | } |
| 1615 | |
Mikulas Patocka | 4c0c59e | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1616 | pe->exception_sequence = s->exception_start_sequence++; |
| 1617 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1618 | dm_insert_exception(&s->pending, &pe->e); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | return pe; |
| 1621 | } |
| 1622 | |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 1623 | static void remap_exception(struct dm_snapshot *s, struct dm_exception *e, |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1624 | struct bio *bio, chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | { |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1626 | bio->bi_bdev = s->cow->bdev; |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1627 | bio->bi_sector = chunk_to_sector(s->store, |
| 1628 | dm_chunk_number(e->new_chunk) + |
| 1629 | (chunk - e->old_chunk)) + |
| 1630 | (bio->bi_sector & |
| 1631 | s->store->chunk_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | static int snapshot_map(struct dm_target *ti, struct bio *bio, |
| 1635 | union map_info *map_context) |
| 1636 | { |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 1637 | struct dm_exception *e; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1638 | struct dm_snapshot *s = ti->private; |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1639 | int r = DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | chunk_t chunk; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1641 | struct dm_snap_pending_exception *pe = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 1643 | if (bio->bi_rw & REQ_FLUSH) { |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1644 | bio->bi_bdev = s->cow->bdev; |
Mikulas Patocka | 494b3ee | 2009-06-22 10:12:25 +0100 | [diff] [blame] | 1645 | return DM_MAPIO_REMAPPED; |
| 1646 | } |
| 1647 | |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1648 | chunk = sector_to_chunk(s->store, bio->bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | |
| 1650 | /* Full snapshots are not usable */ |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1651 | /* 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] | 1652 | if (!s->valid) |
Alasdair G Kergon | f6a80ea | 2005-07-12 15:53:01 -0700 | [diff] [blame] | 1653 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1655 | /* FIXME: should only take write lock if we need |
| 1656 | * to copy an exception */ |
| 1657 | down_write(&s->lock); |
| 1658 | |
| 1659 | if (!s->valid) { |
| 1660 | r = -EIO; |
| 1661 | goto out_unlock; |
| 1662 | } |
| 1663 | |
| 1664 | /* If the block is already remapped - use that, else remap it */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1665 | e = dm_lookup_exception(&s->complete, chunk); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1666 | if (e) { |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1667 | remap_exception(s, e, bio, chunk); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1668 | goto out_unlock; |
| 1669 | } |
| 1670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | /* |
| 1672 | * Write to snapshot - higher level takes care of RW/RO |
| 1673 | * flags so we should only get this if we are |
| 1674 | * writeable. |
| 1675 | */ |
| 1676 | if (bio_rw(bio) == WRITE) { |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1677 | pe = __lookup_pending_exception(s, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1678 | if (!pe) { |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1679 | up_write(&s->lock); |
| 1680 | pe = alloc_pending_exception(s); |
| 1681 | down_write(&s->lock); |
| 1682 | |
| 1683 | if (!s->valid) { |
| 1684 | free_pending_exception(pe); |
| 1685 | r = -EIO; |
| 1686 | goto out_unlock; |
| 1687 | } |
| 1688 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1689 | e = dm_lookup_exception(&s->complete, chunk); |
Mikulas Patocka | 35bf659 | 2009-04-02 19:55:26 +0100 | [diff] [blame] | 1690 | if (e) { |
| 1691 | free_pending_exception(pe); |
| 1692 | remap_exception(s, e, bio, chunk); |
| 1693 | goto out_unlock; |
| 1694 | } |
| 1695 | |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1696 | pe = __find_pending_exception(s, pe, chunk); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1697 | if (!pe) { |
| 1698 | __invalidate_snapshot(s, -ENOMEM); |
| 1699 | r = -EIO; |
| 1700 | goto out_unlock; |
| 1701 | } |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1702 | } |
| 1703 | |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1704 | remap_exception(s, &pe->e, bio, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1705 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1706 | r = DM_MAPIO_SUBMITTED; |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1707 | |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1708 | if (!pe->started && |
| 1709 | bio->bi_size == (s->store->chunk_size << SECTOR_SHIFT)) { |
| 1710 | pe->started = 1; |
| 1711 | up_write(&s->lock); |
| 1712 | start_full_bio(pe, bio); |
| 1713 | goto out; |
| 1714 | } |
| 1715 | |
| 1716 | bio_list_add(&pe->snapshot_bios, bio); |
| 1717 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1718 | if (!pe->started) { |
| 1719 | /* this is protected by snap->lock */ |
| 1720 | pe->started = 1; |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1721 | up_write(&s->lock); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1722 | start_copy(pe); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1723 | goto out; |
| 1724 | } |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1725 | } else { |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1726 | bio->bi_bdev = s->origin->bdev; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1727 | map_context->ptr = track_chunk(s, chunk); |
| 1728 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1730 | out_unlock: |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1731 | up_write(&s->lock); |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1732 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | return r; |
| 1734 | } |
| 1735 | |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1736 | /* |
| 1737 | * A snapshot-merge target behaves like a combination of a snapshot |
| 1738 | * target and a snapshot-origin target. It only generates new |
| 1739 | * exceptions in other snapshots and not in the one that is being |
| 1740 | * merged. |
| 1741 | * |
| 1742 | * For each chunk, if there is an existing exception, it is used to |
| 1743 | * redirect I/O to the cow device. Otherwise I/O is sent to the origin, |
| 1744 | * which in turn might generate exceptions in other snapshots. |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1745 | * If merging is currently taking place on the chunk in question, the |
| 1746 | * I/O is deferred by adding it to s->bios_queued_during_merge. |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1747 | */ |
| 1748 | static int snapshot_merge_map(struct dm_target *ti, struct bio *bio, |
| 1749 | union map_info *map_context) |
| 1750 | { |
| 1751 | struct dm_exception *e; |
| 1752 | struct dm_snapshot *s = ti->private; |
| 1753 | int r = DM_MAPIO_REMAPPED; |
| 1754 | chunk_t chunk; |
| 1755 | |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 1756 | if (bio->bi_rw & REQ_FLUSH) { |
Mike Snitzer | 57cba5d | 2010-08-12 04:14:04 +0100 | [diff] [blame] | 1757 | if (!map_context->target_request_nr) |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1758 | bio->bi_bdev = s->origin->bdev; |
| 1759 | else |
| 1760 | bio->bi_bdev = s->cow->bdev; |
| 1761 | map_context->ptr = NULL; |
| 1762 | return DM_MAPIO_REMAPPED; |
| 1763 | } |
| 1764 | |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1765 | chunk = sector_to_chunk(s->store, bio->bi_sector); |
| 1766 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1767 | down_write(&s->lock); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1768 | |
Mikulas Patocka | d2fdb77 | 2009-12-10 23:52:36 +0000 | [diff] [blame] | 1769 | /* Full merging snapshots are redirected to the origin */ |
| 1770 | if (!s->valid) |
| 1771 | goto redirect_to_origin; |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1772 | |
| 1773 | /* If the block is already remapped - use that */ |
| 1774 | e = dm_lookup_exception(&s->complete, chunk); |
| 1775 | if (e) { |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1776 | /* Queue writes overlapping with chunks being merged */ |
| 1777 | if (bio_rw(bio) == WRITE && |
| 1778 | chunk >= s->first_merging_chunk && |
| 1779 | chunk < (s->first_merging_chunk + |
| 1780 | s->num_merging_chunks)) { |
| 1781 | bio->bi_bdev = s->origin->bdev; |
| 1782 | bio_list_add(&s->bios_queued_during_merge, bio); |
| 1783 | r = DM_MAPIO_SUBMITTED; |
| 1784 | goto out_unlock; |
| 1785 | } |
Mikulas Patocka | 17aa033 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1786 | |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1787 | remap_exception(s, e, bio, chunk); |
Mikulas Patocka | 17aa033 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1788 | |
| 1789 | if (bio_rw(bio) == WRITE) |
| 1790 | map_context->ptr = track_chunk(s, chunk); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1791 | goto out_unlock; |
| 1792 | } |
| 1793 | |
Mikulas Patocka | d2fdb77 | 2009-12-10 23:52:36 +0000 | [diff] [blame] | 1794 | redirect_to_origin: |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1795 | bio->bi_bdev = s->origin->bdev; |
| 1796 | |
| 1797 | if (bio_rw(bio) == WRITE) { |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1798 | up_write(&s->lock); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1799 | return do_origin(s->origin, bio); |
| 1800 | } |
| 1801 | |
| 1802 | out_unlock: |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1803 | up_write(&s->lock); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1804 | |
| 1805 | return r; |
| 1806 | } |
| 1807 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1808 | static int snapshot_end_io(struct dm_target *ti, struct bio *bio, |
| 1809 | int error, union map_info *map_context) |
| 1810 | { |
| 1811 | struct dm_snapshot *s = ti->private; |
| 1812 | struct dm_snap_tracked_chunk *c = map_context->ptr; |
| 1813 | |
| 1814 | if (c) |
| 1815 | stop_tracking_chunk(s, c); |
| 1816 | |
| 1817 | return 0; |
| 1818 | } |
| 1819 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1820 | static void snapshot_merge_presuspend(struct dm_target *ti) |
| 1821 | { |
| 1822 | struct dm_snapshot *s = ti->private; |
| 1823 | |
| 1824 | stop_merge(s); |
| 1825 | } |
| 1826 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1827 | static int snapshot_preresume(struct dm_target *ti) |
| 1828 | { |
| 1829 | int r = 0; |
| 1830 | struct dm_snapshot *s = ti->private; |
| 1831 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
| 1832 | |
| 1833 | down_read(&_origins_lock); |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1834 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1835 | if (snap_src && snap_dest) { |
| 1836 | down_read(&snap_src->lock); |
| 1837 | if (s == snap_src) { |
| 1838 | DMERR("Unable to resume snapshot source until " |
| 1839 | "handover completes."); |
| 1840 | r = -EINVAL; |
Mike Snitzer | b83b2f2 | 2011-01-13 19:59:59 +0000 | [diff] [blame] | 1841 | } else if (!dm_suspended(snap_src->ti)) { |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1842 | DMERR("Unable to perform snapshot handover until " |
| 1843 | "source is suspended."); |
| 1844 | r = -EINVAL; |
| 1845 | } |
| 1846 | up_read(&snap_src->lock); |
| 1847 | } |
| 1848 | up_read(&_origins_lock); |
| 1849 | |
| 1850 | return r; |
| 1851 | } |
| 1852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | static void snapshot_resume(struct dm_target *ti) |
| 1854 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1855 | struct dm_snapshot *s = ti->private; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1856 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
| 1857 | |
| 1858 | down_read(&_origins_lock); |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1859 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1860 | if (snap_src && snap_dest) { |
| 1861 | down_write(&snap_src->lock); |
| 1862 | down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING); |
| 1863 | __handover_exceptions(snap_src, snap_dest); |
| 1864 | up_write(&snap_dest->lock); |
| 1865 | up_write(&snap_src->lock); |
| 1866 | } |
| 1867 | up_read(&_origins_lock); |
| 1868 | |
| 1869 | /* Now we have correct chunk size, reregister */ |
| 1870 | reregister_snapshot(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 1872 | down_write(&s->lock); |
| 1873 | s->active = 1; |
| 1874 | up_write(&s->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1877 | static sector_t get_origin_minimum_chunksize(struct block_device *bdev) |
| 1878 | { |
| 1879 | sector_t min_chunksize; |
| 1880 | |
| 1881 | down_read(&_origins_lock); |
| 1882 | min_chunksize = __minimum_chunk_size(__lookup_origin(bdev)); |
| 1883 | up_read(&_origins_lock); |
| 1884 | |
| 1885 | return min_chunksize; |
| 1886 | } |
| 1887 | |
| 1888 | static void snapshot_merge_resume(struct dm_target *ti) |
| 1889 | { |
| 1890 | struct dm_snapshot *s = ti->private; |
| 1891 | |
| 1892 | /* |
| 1893 | * Handover exceptions from existing snapshot. |
| 1894 | */ |
| 1895 | snapshot_resume(ti); |
| 1896 | |
| 1897 | /* |
| 1898 | * snapshot-merge acts as an origin, so set ti->split_io |
| 1899 | */ |
| 1900 | ti->split_io = get_origin_minimum_chunksize(s->origin->bdev); |
| 1901 | |
| 1902 | start_merge(s); |
| 1903 | } |
| 1904 | |
Mikulas Patocka | 5b7bb4a | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 1905 | static void snapshot_status(struct dm_target *ti, status_type_t type, |
| 1906 | char *result, unsigned int maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | { |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1908 | unsigned sz = 0; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1909 | struct dm_snapshot *snap = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | |
| 1911 | switch (type) { |
| 1912 | case STATUSTYPE_INFO: |
Mikulas Patocka | 94e7657 | 2009-12-10 23:51:53 +0000 | [diff] [blame] | 1913 | |
| 1914 | down_write(&snap->lock); |
| 1915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | if (!snap->valid) |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1917 | DMEMIT("Invalid"); |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 1918 | else if (snap->merge_failed) |
| 1919 | DMEMIT("Merge failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | else { |
Mike Snitzer | 985903b | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1921 | if (snap->store->type->usage) { |
| 1922 | sector_t total_sectors, sectors_allocated, |
| 1923 | metadata_sectors; |
| 1924 | snap->store->type->usage(snap->store, |
| 1925 | &total_sectors, |
| 1926 | §ors_allocated, |
| 1927 | &metadata_sectors); |
| 1928 | DMEMIT("%llu/%llu %llu", |
| 1929 | (unsigned long long)sectors_allocated, |
| 1930 | (unsigned long long)total_sectors, |
| 1931 | (unsigned long long)metadata_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | } |
| 1933 | else |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1934 | DMEMIT("Unknown"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | } |
Mikulas Patocka | 94e7657 | 2009-12-10 23:51:53 +0000 | [diff] [blame] | 1936 | |
| 1937 | up_write(&snap->lock); |
| 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | break; |
| 1940 | |
| 1941 | case STATUSTYPE_TABLE: |
| 1942 | /* |
| 1943 | * kdevname returns a static pointer so we need |
| 1944 | * to make private copies if the output is to |
| 1945 | * make sense. |
| 1946 | */ |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1947 | DMEMIT("%s %s", snap->origin->name, snap->cow->name); |
Jonathan Brassow | 1e302a9 | 2009-04-02 19:55:35 +0100 | [diff] [blame] | 1948 | snap->store->type->status(snap->store, type, result + sz, |
| 1949 | maxlen - sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | break; |
| 1951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 1954 | static int snapshot_iterate_devices(struct dm_target *ti, |
| 1955 | iterate_devices_callout_fn fn, void *data) |
| 1956 | { |
| 1957 | struct dm_snapshot *snap = ti->private; |
Mikulas Patocka | 1e5554c | 2010-08-12 04:13:50 +0100 | [diff] [blame] | 1958 | int r; |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 1959 | |
Mikulas Patocka | 1e5554c | 2010-08-12 04:13:50 +0100 | [diff] [blame] | 1960 | r = fn(ti, snap->origin, 0, ti->len, data); |
| 1961 | |
| 1962 | if (!r) |
| 1963 | r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data); |
| 1964 | |
| 1965 | return r; |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | /*----------------------------------------------------------------- |
| 1970 | * Origin methods |
| 1971 | *---------------------------------------------------------------*/ |
Mikulas Patocka | 9eaae8f | 2009-12-10 23:52:28 +0000 | [diff] [blame] | 1972 | |
| 1973 | /* |
| 1974 | * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any |
| 1975 | * supplied bio was ignored. The caller may submit it immediately. |
| 1976 | * (No remapping actually occurs as the origin is always a direct linear |
| 1977 | * map.) |
| 1978 | * |
| 1979 | * If further exceptions are required, DM_MAPIO_SUBMITTED is returned |
| 1980 | * and any supplied bio is added to a list to be submitted once all |
| 1981 | * the necessary exceptions exist. |
| 1982 | */ |
| 1983 | static int __origin_write(struct list_head *snapshots, sector_t sector, |
| 1984 | struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | { |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1986 | int r = DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | struct dm_snapshot *snap; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 1988 | struct dm_exception *e; |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1989 | struct dm_snap_pending_exception *pe; |
| 1990 | struct dm_snap_pending_exception *pe_to_start_now = NULL; |
| 1991 | struct dm_snap_pending_exception *pe_to_start_last = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | chunk_t chunk; |
| 1993 | |
| 1994 | /* Do all the snapshots on this origin */ |
| 1995 | list_for_each_entry (snap, snapshots, list) { |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1996 | /* |
| 1997 | * Don't make new exceptions in a merging snapshot |
| 1998 | * because it has effectively been deleted |
| 1999 | */ |
| 2000 | if (dm_target_is_snapshot_merge(snap->ti)) |
| 2001 | continue; |
| 2002 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2003 | down_write(&snap->lock); |
| 2004 | |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 2005 | /* Only deal with valid and active snapshots */ |
| 2006 | if (!snap->valid || !snap->active) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2007 | goto next_snapshot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
Alasdair G Kergon | d5e404c | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 2009 | /* Nothing to do if writing beyond end of snapshot */ |
Mikulas Patocka | 9eaae8f | 2009-12-10 23:52:28 +0000 | [diff] [blame] | 2010 | if (sector >= dm_table_get_size(snap->ti->table)) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2011 | goto next_snapshot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
| 2013 | /* |
| 2014 | * Remember, different snapshots can have |
| 2015 | * different chunk sizes. |
| 2016 | */ |
Mikulas Patocka | 9eaae8f | 2009-12-10 23:52:28 +0000 | [diff] [blame] | 2017 | chunk = sector_to_chunk(snap->store, sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
| 2019 | /* |
| 2020 | * Check exception table to see if block |
| 2021 | * is already remapped in this snapshot |
| 2022 | * and trigger an exception if not. |
| 2023 | */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 2024 | e = dm_lookup_exception(&snap->complete, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2025 | if (e) |
| 2026 | goto next_snapshot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 2028 | pe = __lookup_pending_exception(snap, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2029 | if (!pe) { |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 2030 | up_write(&snap->lock); |
| 2031 | pe = alloc_pending_exception(snap); |
| 2032 | down_write(&snap->lock); |
| 2033 | |
| 2034 | if (!snap->valid) { |
| 2035 | free_pending_exception(pe); |
| 2036 | goto next_snapshot; |
| 2037 | } |
| 2038 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 2039 | e = dm_lookup_exception(&snap->complete, chunk); |
Mikulas Patocka | 35bf659 | 2009-04-02 19:55:26 +0100 | [diff] [blame] | 2040 | if (e) { |
| 2041 | free_pending_exception(pe); |
| 2042 | goto next_snapshot; |
| 2043 | } |
| 2044 | |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 2045 | pe = __find_pending_exception(snap, pe, chunk); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 2046 | if (!pe) { |
| 2047 | __invalidate_snapshot(snap, -ENOMEM); |
| 2048 | goto next_snapshot; |
| 2049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2052 | r = DM_MAPIO_SUBMITTED; |
| 2053 | |
| 2054 | /* |
| 2055 | * If an origin bio was supplied, queue it to wait for the |
| 2056 | * completion of this exception, and start this one last, |
| 2057 | * at the end of the function. |
| 2058 | */ |
| 2059 | if (bio) { |
| 2060 | bio_list_add(&pe->origin_bios, bio); |
| 2061 | bio = NULL; |
| 2062 | |
| 2063 | if (!pe->started) { |
| 2064 | pe->started = 1; |
| 2065 | pe_to_start_last = pe; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2066 | } |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | if (!pe->started) { |
| 2070 | pe->started = 1; |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2071 | pe_to_start_now = pe; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2072 | } |
| 2073 | |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 2074 | next_snapshot: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | up_write(&snap->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2077 | if (pe_to_start_now) { |
| 2078 | start_copy(pe_to_start_now); |
| 2079 | pe_to_start_now = NULL; |
| 2080 | } |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 2081 | } |
| 2082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | /* |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2084 | * Submit the exception against which the bio is queued last, |
| 2085 | * to give the other exceptions a head start. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | */ |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2087 | if (pe_to_start_last) |
| 2088 | start_copy(pe_to_start_last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | |
| 2090 | return r; |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * Called on a write from the origin driver. |
| 2095 | */ |
| 2096 | static int do_origin(struct dm_dev *origin, struct bio *bio) |
| 2097 | { |
| 2098 | struct origin *o; |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 2099 | int r = DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | |
| 2101 | down_read(&_origins_lock); |
| 2102 | o = __lookup_origin(origin->bdev); |
| 2103 | if (o) |
Mikulas Patocka | 9eaae8f | 2009-12-10 23:52:28 +0000 | [diff] [blame] | 2104 | r = __origin_write(&o->snapshots, bio->bi_sector, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | up_read(&_origins_lock); |
| 2106 | |
| 2107 | return r; |
| 2108 | } |
| 2109 | |
| 2110 | /* |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 2111 | * Trigger exceptions in all non-merging snapshots. |
| 2112 | * |
| 2113 | * The chunk size of the merging snapshot may be larger than the chunk |
| 2114 | * size of some other snapshot so we may need to reallocate multiple |
| 2115 | * chunks in other snapshots. |
| 2116 | * |
| 2117 | * We scan all the overlapping exceptions in the other snapshots. |
| 2118 | * Returns 1 if anything was reallocated and must be waited for, |
| 2119 | * otherwise returns 0. |
| 2120 | * |
| 2121 | * size must be a multiple of merging_snap's chunk_size. |
| 2122 | */ |
| 2123 | static int origin_write_extent(struct dm_snapshot *merging_snap, |
| 2124 | sector_t sector, unsigned size) |
| 2125 | { |
| 2126 | int must_wait = 0; |
| 2127 | sector_t n; |
| 2128 | struct origin *o; |
| 2129 | |
| 2130 | /* |
| 2131 | * The origin's __minimum_chunk_size() got stored in split_io |
| 2132 | * by snapshot_merge_resume(). |
| 2133 | */ |
| 2134 | down_read(&_origins_lock); |
| 2135 | o = __lookup_origin(merging_snap->origin->bdev); |
| 2136 | for (n = 0; n < size; n += merging_snap->ti->split_io) |
| 2137 | if (__origin_write(&o->snapshots, sector + n, NULL) == |
| 2138 | DM_MAPIO_SUBMITTED) |
| 2139 | must_wait = 1; |
| 2140 | up_read(&_origins_lock); |
| 2141 | |
| 2142 | return must_wait; |
| 2143 | } |
| 2144 | |
| 2145 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | * Origin: maps a linear range of a device, with hooks for snapshotting. |
| 2147 | */ |
| 2148 | |
| 2149 | /* |
| 2150 | * Construct an origin mapping: <dev_path> |
| 2151 | * The context for an origin is merely a 'struct dm_dev *' |
| 2152 | * pointing to the real device. |
| 2153 | */ |
| 2154 | static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 2155 | { |
| 2156 | int r; |
| 2157 | struct dm_dev *dev; |
| 2158 | |
| 2159 | if (argc != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2160 | ti->error = "origin: incorrect number of arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | return -EINVAL; |
| 2162 | } |
| 2163 | |
Nikanth Karthikesan | 8215d6e | 2010-03-06 02:32:27 +0000 | [diff] [blame] | 2164 | r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | if (r) { |
| 2166 | ti->error = "Cannot get target device"; |
| 2167 | return r; |
| 2168 | } |
| 2169 | |
| 2170 | ti->private = dev; |
Mikulas Patocka | 494b3ee | 2009-06-22 10:12:25 +0100 | [diff] [blame] | 2171 | ti->num_flush_requests = 1; |
| 2172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | return 0; |
| 2174 | } |
| 2175 | |
| 2176 | static void origin_dtr(struct dm_target *ti) |
| 2177 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2178 | struct dm_dev *dev = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | dm_put_device(ti, dev); |
| 2180 | } |
| 2181 | |
| 2182 | static int origin_map(struct dm_target *ti, struct bio *bio, |
| 2183 | union map_info *map_context) |
| 2184 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2185 | struct dm_dev *dev = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | bio->bi_bdev = dev->bdev; |
| 2187 | |
Tejun Heo | d87f4c1 | 2010-09-03 11:56:19 +0200 | [diff] [blame] | 2188 | if (bio->bi_rw & REQ_FLUSH) |
Mikulas Patocka | 494b3ee | 2009-06-22 10:12:25 +0100 | [diff] [blame] | 2189 | return DM_MAPIO_REMAPPED; |
| 2190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | /* Only tell snapshots if this is a write */ |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 2192 | return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | /* |
| 2196 | * Set the target "split_io" field to the minimum of all the snapshots' |
| 2197 | * chunk sizes. |
| 2198 | */ |
| 2199 | static void origin_resume(struct dm_target *ti) |
| 2200 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2201 | struct dm_dev *dev = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2203 | ti->split_io = get_origin_minimum_chunksize(dev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
Mikulas Patocka | 5b7bb4a | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2206 | static void origin_status(struct dm_target *ti, status_type_t type, char *result, |
| 2207 | unsigned int maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2209 | struct dm_dev *dev = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | |
| 2211 | switch (type) { |
| 2212 | case STATUSTYPE_INFO: |
| 2213 | result[0] = '\0'; |
| 2214 | break; |
| 2215 | |
| 2216 | case STATUSTYPE_TABLE: |
| 2217 | snprintf(result, maxlen, "%s", dev->name); |
| 2218 | break; |
| 2219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Mikulas Patocka | b1d5552 | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2222 | static int origin_merge(struct dm_target *ti, struct bvec_merge_data *bvm, |
| 2223 | struct bio_vec *biovec, int max_size) |
| 2224 | { |
| 2225 | struct dm_dev *dev = ti->private; |
| 2226 | struct request_queue *q = bdev_get_queue(dev->bdev); |
| 2227 | |
| 2228 | if (!q->merge_bvec_fn) |
| 2229 | return max_size; |
| 2230 | |
| 2231 | bvm->bi_bdev = dev->bdev; |
| 2232 | bvm->bi_sector = bvm->bi_sector; |
| 2233 | |
| 2234 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); |
| 2235 | } |
| 2236 | |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2237 | static int origin_iterate_devices(struct dm_target *ti, |
| 2238 | iterate_devices_callout_fn fn, void *data) |
| 2239 | { |
| 2240 | struct dm_dev *dev = ti->private; |
| 2241 | |
| 2242 | return fn(ti, dev, 0, ti->len, data); |
| 2243 | } |
| 2244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | static struct target_type origin_target = { |
| 2246 | .name = "snapshot-origin", |
Mike Snitzer | b83b2f2 | 2011-01-13 19:59:59 +0000 | [diff] [blame] | 2247 | .version = {1, 7, 1}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | .module = THIS_MODULE, |
| 2249 | .ctr = origin_ctr, |
| 2250 | .dtr = origin_dtr, |
| 2251 | .map = origin_map, |
| 2252 | .resume = origin_resume, |
| 2253 | .status = origin_status, |
Mikulas Patocka | b1d5552 | 2010-08-12 04:14:02 +0100 | [diff] [blame] | 2254 | .merge = origin_merge, |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2255 | .iterate_devices = origin_iterate_devices, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | }; |
| 2257 | |
| 2258 | static struct target_type snapshot_target = { |
| 2259 | .name = "snapshot", |
Mikulas Patocka | 4c0c59e | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 2260 | .version = {1, 10, 2}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | .module = THIS_MODULE, |
| 2262 | .ctr = snapshot_ctr, |
| 2263 | .dtr = snapshot_dtr, |
| 2264 | .map = snapshot_map, |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 2265 | .end_io = snapshot_end_io, |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 2266 | .preresume = snapshot_preresume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | .resume = snapshot_resume, |
| 2268 | .status = snapshot_status, |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2269 | .iterate_devices = snapshot_iterate_devices, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | }; |
| 2271 | |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2272 | static struct target_type merge_target = { |
| 2273 | .name = dm_snapshot_merge_target_name, |
Mike Snitzer | b83b2f2 | 2011-01-13 19:59:59 +0000 | [diff] [blame] | 2274 | .version = {1, 1, 0}, |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2275 | .module = THIS_MODULE, |
| 2276 | .ctr = snapshot_ctr, |
| 2277 | .dtr = snapshot_dtr, |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 2278 | .map = snapshot_merge_map, |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2279 | .end_io = snapshot_end_io, |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2280 | .presuspend = snapshot_merge_presuspend, |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2281 | .preresume = snapshot_preresume, |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2282 | .resume = snapshot_merge_resume, |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2283 | .status = snapshot_status, |
| 2284 | .iterate_devices = snapshot_iterate_devices, |
| 2285 | }; |
| 2286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | static int __init dm_snapshot_init(void) |
| 2288 | { |
| 2289 | int r; |
| 2290 | |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 2291 | r = dm_exception_store_init(); |
| 2292 | if (r) { |
| 2293 | DMERR("Failed to initialize exception stores"); |
| 2294 | return r; |
| 2295 | } |
| 2296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | r = dm_register_target(&snapshot_target); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2298 | if (r < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | DMERR("snapshot target register failed %d", r); |
Jonathan Brassow | 034a186 | 2009-10-16 23:18:14 +0100 | [diff] [blame] | 2300 | goto bad_register_snapshot_target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | r = dm_register_target(&origin_target); |
| 2304 | if (r < 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2305 | DMERR("Origin target register failed %d", r); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2306 | goto bad_register_origin_target; |
| 2307 | } |
| 2308 | |
| 2309 | r = dm_register_target(&merge_target); |
| 2310 | if (r < 0) { |
| 2311 | DMERR("Merge target register failed %d", r); |
| 2312 | goto bad_register_merge_target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | r = init_origin_hash(); |
| 2316 | if (r) { |
| 2317 | DMERR("init_origin_hash failed."); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2318 | goto bad_origin_hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 2321 | exception_cache = KMEM_CACHE(dm_exception, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | if (!exception_cache) { |
| 2323 | DMERR("Couldn't create exception cache."); |
| 2324 | r = -ENOMEM; |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2325 | goto bad_exception_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2328 | pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | if (!pending_cache) { |
| 2330 | DMERR("Couldn't create pending cache."); |
| 2331 | r = -ENOMEM; |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2332 | goto bad_pending_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 2335 | tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0); |
| 2336 | if (!tracked_chunk_cache) { |
| 2337 | DMERR("Couldn't create cache to track chunks in use."); |
| 2338 | r = -ENOMEM; |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2339 | goto bad_tracked_chunk_cache; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 2340 | } |
| 2341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | return 0; |
| 2343 | |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2344 | bad_tracked_chunk_cache: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | kmem_cache_destroy(pending_cache); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2346 | bad_pending_cache: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | kmem_cache_destroy(exception_cache); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2348 | bad_exception_cache: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | exit_origin_hash(); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2350 | bad_origin_hash: |
| 2351 | dm_unregister_target(&merge_target); |
| 2352 | bad_register_merge_target: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | dm_unregister_target(&origin_target); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2354 | bad_register_origin_target: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | dm_unregister_target(&snapshot_target); |
Jonathan Brassow | 034a186 | 2009-10-16 23:18:14 +0100 | [diff] [blame] | 2356 | bad_register_snapshot_target: |
| 2357 | dm_exception_store_exit(); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | return r; |
| 2360 | } |
| 2361 | |
| 2362 | static void __exit dm_snapshot_exit(void) |
| 2363 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 2364 | dm_unregister_target(&snapshot_target); |
| 2365 | dm_unregister_target(&origin_target); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2366 | dm_unregister_target(&merge_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | |
| 2368 | exit_origin_hash(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | kmem_cache_destroy(pending_cache); |
| 2370 | kmem_cache_destroy(exception_cache); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 2371 | kmem_cache_destroy(tracked_chunk_cache); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 2372 | |
| 2373 | dm_exception_store_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | /* Module hooks */ |
| 2377 | module_init(dm_snapshot_init); |
| 2378 | module_exit(dm_snapshot_exit); |
| 2379 | |
| 2380 | MODULE_DESCRIPTION(DM_NAME " snapshot target"); |
| 2381 | MODULE_AUTHOR("Joe Thornber"); |
| 2382 | MODULE_LICENSE("GPL"); |
Mikulas Patocka | 71b1732 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 2383 | MODULE_ALIAS("dm-snapshot-origin"); |
| 2384 | MODULE_ALIAS("dm-snapshot-merge"); |