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