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