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