blob: eed210152b75802930369d1953d6006aaedc993a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/device-mapper.h>
Mikulas Patocka90fa1522009-01-06 03:04:54 +000011#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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 babu6f3c3f02007-10-19 22:38:44 +010020#include <linux/log2.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010021#include <linux/dm-kcopyd.h>
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010022#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jonathan Brassowaea53d92009-01-06 03:05:15 +000024#include "dm-exception-store.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "snapshots"
27
Mikulas Patockad698aa42009-12-10 23:52:30 +000028static 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 Torvalds1da177e2005-04-16 15:20:36 -070033/*
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 Broz8ee27672008-04-24 21:42:36 +010044 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
Milan Broz8ee27672008-04-24 21:42:36 +010046#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Mikulas Patockacd45daf2008-07-21 12:00:32 +010048/*
49 * The size of the mempool used to track chunks in use.
50 */
51#define MIN_IOS 256
52
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010053#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 Brassow191437a2009-12-10 23:52:10 +000057struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010058 uint32_t hash_mask;
59 unsigned hash_shift;
60 struct list_head *table;
61};
62
63struct dm_snapshot {
64 struct rw_semaphore lock;
65
66 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000067 struct dm_dev *cow;
68
69 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010070
71 /* List of snapshots per Origin */
72 struct list_head list;
73
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +000074 /*
75 * You can't use a snapshot if this is 0 (e.g. if full).
76 * A snapshot-merge target never clears this.
77 */
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010078 int valid;
79
80 /* Origin writes don't trigger exceptions until this is set */
81 int active;
82
Mike Snitzerc26655c2009-12-10 23:52:12 +000083 /* Whether or not owning mapped_device is suspended */
84 int suspended;
85
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010086 atomic_t pending_exceptions_count;
87
Mike Snitzer924e6002010-03-06 02:32:33 +000088 mempool_t *pending_pool;
89
Jon Brassow191437a2009-12-10 23:52:10 +000090 struct dm_exception_table pending;
91 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010092
93 /*
94 * pe_lock protects all pending_exception operations and access
95 * as well as the snapshot_bios list.
96 */
97 spinlock_t pe_lock;
98
Mike Snitzer924e6002010-03-06 02:32:33 +000099 /* Chunks with outstanding reads */
100 spinlock_t tracked_chunk_lock;
101 mempool_t *tracked_chunk_pool;
102 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
103
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100104 /* The on disk metadata handler */
105 struct dm_exception_store *store;
106
107 struct dm_kcopyd_client *kcopyd_client;
108
109 /* Queue of snapshot writes for ksnapd to flush */
110 struct bio_list queued_bios;
111 struct work_struct queued_bios_work;
112
Mike Snitzer924e6002010-03-06 02:32:33 +0000113 /* Wait for events based on state_bits */
114 unsigned long state_bits;
115
116 /* Range of chunks currently being merged. */
117 chunk_t first_merging_chunk;
118 int num_merging_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000119
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000120 /*
121 * The merge operation failed if this flag is set.
122 * Failure modes are handled as follows:
123 * - I/O error reading the header
124 * => don't load the target; abort.
125 * - Header does not have "valid" flag set
126 * => use the origin; forget about the snapshot.
127 * - I/O error when reading exceptions
128 * => don't load the target; abort.
129 * (We can't use the intermediate origin state.)
130 * - I/O error while merging
131 * => stop merging; set merge_failed; process I/O normally.
132 */
133 int merge_failed;
134
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000135 /*
136 * Incoming bios that overlap with chunks being merged must wait
137 * for them to be committed.
138 */
139 struct bio_list bios_queued_during_merge;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100140};
141
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000142/*
143 * state_bits:
144 * RUNNING_MERGE - Merge operation is in progress.
145 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
146 * cleared afterwards.
147 */
148#define RUNNING_MERGE 0
149#define SHUTDOWN_MERGE 1
150
Mikulas Patockac2411042010-08-12 04:13:51 +0100151struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
152{
153 return s->origin;
154}
155EXPORT_SYMBOL(dm_snap_origin);
156
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000157struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
158{
159 return s->cow;
160}
161EXPORT_SYMBOL(dm_snap_cow);
162
Adrian Bunkc642f9e2006-12-08 02:41:13 -0800163static struct workqueue_struct *ksnapd;
David Howellsc4028952006-11-22 14:57:56 +0000164static void flush_queued_bios(struct work_struct *work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700165
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100166static sector_t chunk_to_sector(struct dm_exception_store *store,
167 chunk_t chunk)
168{
169 return chunk << store->chunk_shift;
170}
171
172static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
173{
174 /*
175 * There is only ever one instance of a particular block
176 * device so we can compare pointers safely.
177 */
178 return lhs == rhs;
179}
180
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100181struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000182 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /*
185 * Origin buffers waiting for this to complete are held
186 * in a bio list
187 */
188 struct bio_list origin_bios;
189 struct bio_list snapshot_bios;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Pointer back to snapshot context */
192 struct dm_snapshot *snap;
193
194 /*
195 * 1 indicates the exception has already been sent to
196 * kcopyd.
197 */
198 int started;
199};
200
201/*
202 * Hash table mapping origin volumes to lists of snapshots and
203 * a lock to protect it
204 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800205static struct kmem_cache *exception_cache;
206static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100208struct dm_snap_tracked_chunk {
209 struct hlist_node node;
210 chunk_t chunk;
211};
212
213static struct kmem_cache *tracked_chunk_cache;
214
215static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
216 chunk_t chunk)
217{
218 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
219 GFP_NOIO);
220 unsigned long flags;
221
222 c->chunk = chunk;
223
224 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
225 hlist_add_head(&c->node,
226 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
227 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
228
229 return c;
230}
231
232static void stop_tracking_chunk(struct dm_snapshot *s,
233 struct dm_snap_tracked_chunk *c)
234{
235 unsigned long flags;
236
237 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
238 hlist_del(&c->node);
239 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
240
241 mempool_free(c, s->tracked_chunk_pool);
242}
243
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100244static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
245{
246 struct dm_snap_tracked_chunk *c;
247 struct hlist_node *hn;
248 int found = 0;
249
250 spin_lock_irq(&s->tracked_chunk_lock);
251
252 hlist_for_each_entry(c, hn,
253 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
254 if (c->chunk == chunk) {
255 found = 1;
256 break;
257 }
258 }
259
260 spin_unlock_irq(&s->tracked_chunk_lock);
261
262 return found;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000266 * This conflicting I/O is extremely improbable in the caller,
267 * so msleep(1) is sufficient and there is no need for a wait queue.
268 */
269static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
270{
271 while (__chunk_is_tracked(s, chunk))
272 msleep(1);
273}
274
275/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * One of these per registered origin, held in the snapshot_origins hash
277 */
278struct origin {
279 /* The origin device */
280 struct block_device *bdev;
281
282 struct list_head hash_list;
283
284 /* List of snapshots for this origin */
285 struct list_head snapshots;
286};
287
288/*
289 * Size of the hash table for origin volumes. If we make this
290 * the size of the minors list then it should be nearly perfect
291 */
292#define ORIGIN_HASH_SIZE 256
293#define ORIGIN_MASK 0xFF
294static struct list_head *_origins;
295static struct rw_semaphore _origins_lock;
296
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000297static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
298static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
299static uint64_t _pending_exceptions_done_count;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static int init_origin_hash(void)
302{
303 int i;
304
305 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
306 GFP_KERNEL);
307 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700308 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -ENOMEM;
310 }
311
312 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
313 INIT_LIST_HEAD(_origins + i);
314 init_rwsem(&_origins_lock);
315
316 return 0;
317}
318
319static void exit_origin_hash(void)
320{
321 kfree(_origins);
322}
323
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100324static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 return bdev->bd_dev & ORIGIN_MASK;
327}
328
329static struct origin *__lookup_origin(struct block_device *origin)
330{
331 struct list_head *ol;
332 struct origin *o;
333
334 ol = &_origins[origin_hash(origin)];
335 list_for_each_entry (o, ol, hash_list)
336 if (bdev_equal(o->bdev, origin))
337 return o;
338
339 return NULL;
340}
341
342static void __insert_origin(struct origin *o)
343{
344 struct list_head *sl = &_origins[origin_hash(o->bdev)];
345 list_add_tail(&o->hash_list, sl);
346}
347
348/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000349 * _origins_lock must be held when calling this function.
350 * Returns number of snapshots registered using the supplied cow device, plus:
351 * snap_src - a snapshot suitable for use as a source of exception handover
352 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000353 * snap_merge - an existing snapshot-merge target linked to the same origin.
354 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000355 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000356 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000357 * 0: NULL, NULL - first new snapshot
358 * 1: snap_src, NULL - normal snapshot
359 * 2: snap_src, snap_dest - waiting for handover
360 * 2: snap_src, NULL - handed over, waiting for old to be deleted
361 * 1: NULL, snap_dest - source got destroyed without handover
362 */
363static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
364 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000365 struct dm_snapshot **snap_dest,
366 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000367{
368 struct dm_snapshot *s;
369 struct origin *o;
370 int count = 0;
371 int active;
372
373 o = __lookup_origin(snap->origin->bdev);
374 if (!o)
375 goto out;
376
377 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000378 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
379 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000380 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
381 continue;
382
383 down_read(&s->lock);
384 active = s->active;
385 up_read(&s->lock);
386
387 if (active) {
388 if (snap_src)
389 *snap_src = s;
390 } else if (snap_dest)
391 *snap_dest = s;
392
393 count++;
394 }
395
396out:
397 return count;
398}
399
400/*
401 * On success, returns 1 if this snapshot is a handover destination,
402 * otherwise returns 0.
403 */
404static int __validate_exception_handover(struct dm_snapshot *snap)
405{
406 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000407 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000408
409 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000410 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
411 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000412 snap_dest) {
413 snap->ti->error = "Snapshot cow pairing for exception "
414 "table handover failed";
415 return -EINVAL;
416 }
417
418 /*
419 * If no snap_src was found, snap cannot become a handover
420 * destination.
421 */
422 if (!snap_src)
423 return 0;
424
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000425 /*
426 * Non-snapshot-merge handover?
427 */
428 if (!dm_target_is_snapshot_merge(snap->ti))
429 return 1;
430
431 /*
432 * Do not allow more than one merging snapshot.
433 */
434 if (snap_merge) {
435 snap->ti->error = "A snapshot is already merging.";
436 return -EINVAL;
437 }
438
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000439 if (!snap_src->store->type->prepare_merge ||
440 !snap_src->store->type->commit_merge) {
441 snap->ti->error = "Snapshot exception store does not "
442 "support snapshot-merge.";
443 return -EINVAL;
444 }
445
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000446 return 1;
447}
448
449static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
450{
451 struct dm_snapshot *l;
452
453 /* Sort the list according to chunk size, largest-first smallest-last */
454 list_for_each_entry(l, &o->snapshots, list)
455 if (l->store->chunk_size < s->store->chunk_size)
456 break;
457 list_add_tail(&s->list, &l->list);
458}
459
460/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * Make a note of the snapshot and its origin so we can look it
462 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000463 *
464 * Also validate snapshot exception store handovers.
465 * On success, returns 1 if this registration is a handover destination,
466 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
468static int register_snapshot(struct dm_snapshot *snap)
469{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000470 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000472 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000474 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
475 if (!new_o)
476 return -ENOMEM;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000480 r = __validate_exception_handover(snap);
481 if (r < 0) {
482 kfree(new_o);
483 goto out;
484 }
485
486 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000487 if (o)
488 kfree(new_o);
489 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000491 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* Initialise the struct */
494 INIT_LIST_HEAD(&o->snapshots);
495 o->bdev = bdev;
496
497 __insert_origin(o);
498 }
499
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000500 __insert_snapshot(o, snap);
501
502out:
503 up_write(&_origins_lock);
504
505 return r;
506}
507
508/*
509 * Move snapshot to correct place in list according to chunk size.
510 */
511static void reregister_snapshot(struct dm_snapshot *s)
512{
513 struct block_device *bdev = s->origin->bdev;
514
515 down_write(&_origins_lock);
516
517 list_del(&s->list);
518 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523static void unregister_snapshot(struct dm_snapshot *s)
524{
525 struct origin *o;
526
527 down_write(&_origins_lock);
528 o = __lookup_origin(s->origin->bdev);
529
530 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000531 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 list_del(&o->hash_list);
533 kfree(o);
534 }
535
536 up_write(&_origins_lock);
537}
538
539/*
540 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000541 * The lowest hash_shift bits of the chunk number are ignored, allowing
542 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000544static int dm_exception_table_init(struct dm_exception_table *et,
545 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 unsigned int i;
548
Milan Brozd74f81f2008-02-08 02:11:27 +0000549 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 et->hash_mask = size - 1;
551 et->table = dm_vcalloc(size, sizeof(struct list_head));
552 if (!et->table)
553 return -ENOMEM;
554
555 for (i = 0; i < size; i++)
556 INIT_LIST_HEAD(et->table + i);
557
558 return 0;
559}
560
Jon Brassow3510cb92009-12-10 23:52:11 +0000561static void dm_exception_table_exit(struct dm_exception_table *et,
562 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000565 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int i, size;
567
568 size = et->hash_mask + 1;
569 for (i = 0; i < size; i++) {
570 slot = et->table + i;
571
572 list_for_each_entry_safe (ex, next, slot, hash_list)
573 kmem_cache_free(mem, ex);
574 }
575
576 vfree(et->table);
577}
578
Jon Brassow191437a2009-12-10 23:52:10 +0000579static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Milan Brozd74f81f2008-02-08 02:11:27 +0000581 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Jon Brassow3510cb92009-12-10 23:52:11 +0000584static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 list_del(&e->hash_list);
587}
588
589/*
590 * Return the exception data for a sector, or NULL if not
591 * remapped.
592 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000593static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
594 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000597 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 slot = &et->table[exception_hash(et, chunk)];
600 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000601 if (chunk >= e->old_chunk &&
602 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return e;
604
605 return NULL;
606}
607
Jon Brassow3510cb92009-12-10 23:52:11 +0000608static struct dm_exception *alloc_completed_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000610 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
613 if (!e)
614 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
615
616 return e;
617}
618
Jon Brassow3510cb92009-12-10 23:52:11 +0000619static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 kmem_cache_free(exception_cache, e);
622}
623
Mikulas Patocka92e86812008-07-21 12:00:35 +0100624static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100626 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
627 GFP_NOIO);
628
Mikulas Patocka879129d2008-10-30 13:33:16 +0000629 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100630 pe->snap = s;
631
632 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100635static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Mikulas Patocka879129d2008-10-30 13:33:16 +0000637 struct dm_snapshot *s = pe->snap;
638
639 mempool_free(pe, s->pending_pool);
640 smp_mb__before_atomic_dec();
641 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Jon Brassow3510cb92009-12-10 23:52:11 +0000644static void dm_insert_exception(struct dm_exception_table *eh,
645 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000646{
Milan Brozd74f81f2008-02-08 02:11:27 +0000647 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000648 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000649
650 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
651
652 /* Add immediately if this table doesn't support consecutive chunks */
653 if (!eh->hash_shift)
654 goto out;
655
656 /* List is ordered by old_chunk */
657 list_for_each_entry_reverse(e, l, hash_list) {
658 /* Insert after an existing chunk? */
659 if (new_e->old_chunk == (e->old_chunk +
660 dm_consecutive_chunk_count(e) + 1) &&
661 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
662 dm_consecutive_chunk_count(e) + 1)) {
663 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000664 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000665 return;
666 }
667
668 /* Insert before an existing chunk? */
669 if (new_e->old_chunk == (e->old_chunk - 1) &&
670 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
671 dm_consecutive_chunk_count_inc(e);
672 e->old_chunk--;
673 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000674 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000675 return;
676 }
677
678 if (new_e->old_chunk > e->old_chunk)
679 break;
680 }
681
682out:
683 list_add(&new_e->hash_list, e ? &e->hash_list : l);
684}
685
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000686/*
687 * Callback used by the exception stores to load exceptions when
688 * initialising.
689 */
690static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000692 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000693 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Jon Brassow3510cb92009-12-10 23:52:11 +0000695 e = alloc_completed_exception();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!e)
697 return -ENOMEM;
698
699 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000700
701 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000703
Jon Brassow3510cb92009-12-10 23:52:11 +0000704 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707}
708
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000709#define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
710
711/*
712 * Return a minimum chunk size of all snapshots that have the specified origin.
713 * Return zero if the origin has no snapshots.
714 */
715static sector_t __minimum_chunk_size(struct origin *o)
716{
717 struct dm_snapshot *snap;
718 unsigned chunk_size = 0;
719
720 if (o)
721 list_for_each_entry(snap, &o->snapshots, list)
722 chunk_size = min_not_zero(chunk_size,
723 snap->store->chunk_size);
724
725 return chunk_size;
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/*
729 * Hard coded magic.
730 */
731static int calc_max_buckets(void)
732{
733 /* use a fixed size of 2MB */
734 unsigned long mem = 2 * 1024 * 1024;
735 mem /= sizeof(struct list_head);
736
737 return mem;
738}
739
740/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * Allocate room for a suitable hash table.
742 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100743static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
746
747 /*
748 * Calculate based on the size of the original volume or
749 * the COW volume...
750 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000751 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 origin_dev_size = get_dev_size(s->origin->bdev);
753 max_buckets = calc_max_buckets();
754
Jonathan Brassowfee19982009-04-02 19:55:34 +0100755 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 hash_size = min(hash_size, max_buckets);
757
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000758 if (hash_size < 64)
759 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000760 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000761 if (dm_exception_table_init(&s->complete, hash_size,
762 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -ENOMEM;
764
765 /*
766 * Allocate hash table for in-flight exceptions
767 * Make this smaller than the real hash table
768 */
769 hash_size >>= 3;
770 if (hash_size < 64)
771 hash_size = 64;
772
Jon Brassow3510cb92009-12-10 23:52:11 +0000773 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
774 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -ENOMEM;
776 }
777
778 return 0;
779}
780
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000781static void merge_shutdown(struct dm_snapshot *s)
782{
783 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
784 smp_mb__after_clear_bit();
785 wake_up_bit(&s->state_bits, RUNNING_MERGE);
786}
787
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000788static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
789{
790 s->first_merging_chunk = 0;
791 s->num_merging_chunks = 0;
792
793 return bio_list_get(&s->bios_queued_during_merge);
794}
795
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000796/*
797 * Remove one chunk from the index of completed exceptions.
798 */
799static int __remove_single_exception_chunk(struct dm_snapshot *s,
800 chunk_t old_chunk)
801{
802 struct dm_exception *e;
803
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000804 e = dm_lookup_exception(&s->complete, old_chunk);
805 if (!e) {
806 DMERR("Corruption detected: exception for block %llu is "
807 "on disk but not in memory",
808 (unsigned long long)old_chunk);
809 return -EINVAL;
810 }
811
812 /*
813 * If this is the only chunk using this exception, remove exception.
814 */
815 if (!dm_consecutive_chunk_count(e)) {
816 dm_remove_exception(e);
817 free_completed_exception(e);
818 return 0;
819 }
820
821 /*
822 * The chunk may be either at the beginning or the end of a
823 * group of consecutive chunks - never in the middle. We are
824 * removing chunks in the opposite order to that in which they
825 * were added, so this should always be true.
826 * Decrement the consecutive chunk counter and adjust the
827 * starting point if necessary.
828 */
829 if (old_chunk == e->old_chunk) {
830 e->old_chunk++;
831 e->new_chunk++;
832 } else if (old_chunk != e->old_chunk +
833 dm_consecutive_chunk_count(e)) {
834 DMERR("Attempt to merge block %llu from the "
835 "middle of a chunk range [%llu - %llu]",
836 (unsigned long long)old_chunk,
837 (unsigned long long)e->old_chunk,
838 (unsigned long long)
839 e->old_chunk + dm_consecutive_chunk_count(e));
840 return -EINVAL;
841 }
842
843 dm_consecutive_chunk_count_dec(e);
844
845 return 0;
846}
847
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000848static void flush_bios(struct bio *bio);
849
850static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000851{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000852 struct bio *b = NULL;
853 int r;
854 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000855
856 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000857
858 /*
859 * Process chunks (and associated exceptions) in reverse order
860 * so that dm_consecutive_chunk_count_dec() accounting works.
861 */
862 do {
863 r = __remove_single_exception_chunk(s, old_chunk);
864 if (r)
865 goto out;
866 } while (old_chunk-- > s->first_merging_chunk);
867
868 b = __release_queued_bios_after_merge(s);
869
870out:
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000871 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000872 if (b)
873 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000874
875 return r;
876}
877
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000878static int origin_write_extent(struct dm_snapshot *merging_snap,
879 sector_t sector, unsigned chunk_size);
880
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000881static void merge_callback(int read_err, unsigned long write_err,
882 void *context);
883
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000884static uint64_t read_pending_exceptions_done_count(void)
885{
886 uint64_t pending_exceptions_done;
887
888 spin_lock(&_pending_exceptions_done_spinlock);
889 pending_exceptions_done = _pending_exceptions_done_count;
890 spin_unlock(&_pending_exceptions_done_spinlock);
891
892 return pending_exceptions_done;
893}
894
895static void increment_pending_exceptions_done_count(void)
896{
897 spin_lock(&_pending_exceptions_done_spinlock);
898 _pending_exceptions_done_count++;
899 spin_unlock(&_pending_exceptions_done_spinlock);
900
901 wake_up_all(&_pending_exceptions_done);
902}
903
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000904static void snapshot_merge_next_chunks(struct dm_snapshot *s)
905{
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000906 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000907 chunk_t old_chunk, new_chunk;
908 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000909 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000910 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000911
912 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
913 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
914 goto shut;
915
916 /*
917 * valid flag never changes during merge, so no lock required.
918 */
919 if (!s->valid) {
920 DMERR("Snapshot is invalid: can't merge");
921 goto shut;
922 }
923
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000924 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
925 &new_chunk);
926 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000927 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000928 DMERR("Read error in exception store: "
929 "shutting down merge");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000930 down_write(&s->lock);
931 s->merge_failed = 1;
932 up_write(&s->lock);
933 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000934 goto shut;
935 }
936
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000937 /* Adjust old_chunk and new_chunk to reflect start of linear region */
938 old_chunk = old_chunk + 1 - linear_chunks;
939 new_chunk = new_chunk + 1 - linear_chunks;
940
941 /*
942 * Use one (potentially large) I/O to copy all 'linear_chunks'
943 * from the exception store to the origin
944 */
945 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000946
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000947 dest.bdev = s->origin->bdev;
948 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000949 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000950
951 src.bdev = s->cow->bdev;
952 src.sector = chunk_to_sector(s->store, new_chunk);
953 src.count = dest.count;
954
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000955 /*
956 * Reallocate any exceptions needed in other snapshots then
957 * wait for the pending exceptions to complete.
958 * Each time any pending exception (globally on the system)
959 * completes we are woken and repeat the process to find out
960 * if we can proceed. While this may not seem a particularly
961 * efficient algorithm, it is not expected to have any
962 * significant impact on performance.
963 */
964 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000965 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000966 wait_event(_pending_exceptions_done,
967 (read_pending_exceptions_done_count() !=
968 previous_count));
969 /* Retry after the wait, until all exceptions are done. */
970 previous_count = read_pending_exceptions_done_count();
971 }
972
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000973 down_write(&s->lock);
974 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000975 s->num_merging_chunks = linear_chunks;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000976 up_write(&s->lock);
977
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000978 /* Wait until writes to all 'linear_chunks' drain */
979 for (i = 0; i < linear_chunks; i++)
980 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000981
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000982 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
983 return;
984
985shut:
986 merge_shutdown(s);
987}
988
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000989static void error_bios(struct bio *bio);
990
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000991static void merge_callback(int read_err, unsigned long write_err, void *context)
992{
993 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000994 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000995
996 if (read_err || write_err) {
997 if (read_err)
998 DMERR("Read error: shutting down merge.");
999 else
1000 DMERR("Write error: shutting down merge.");
1001 goto shut;
1002 }
1003
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001004 if (s->store->type->commit_merge(s->store,
1005 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001006 DMERR("Write error in exception store: shutting down merge");
1007 goto shut;
1008 }
1009
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001010 if (remove_single_exception_chunk(s) < 0)
1011 goto shut;
1012
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001013 snapshot_merge_next_chunks(s);
1014
1015 return;
1016
1017shut:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001018 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001019 s->merge_failed = 1;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001020 b = __release_queued_bios_after_merge(s);
1021 up_write(&s->lock);
1022 error_bios(b);
1023
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001024 merge_shutdown(s);
1025}
1026
1027static void start_merge(struct dm_snapshot *s)
1028{
1029 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1030 snapshot_merge_next_chunks(s);
1031}
1032
1033static int wait_schedule(void *ptr)
1034{
1035 schedule();
1036
1037 return 0;
1038}
1039
1040/*
1041 * Stop the merging process and wait until it finishes.
1042 */
1043static void stop_merge(struct dm_snapshot *s)
1044{
1045 set_bit(SHUTDOWN_MERGE, &s->state_bits);
1046 wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule,
1047 TASK_UNINTERRUPTIBLE);
1048 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
1053 */
1054static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1055{
1056 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001057 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001059 char *origin_path, *cow_path;
Mike Snitzer10b81062009-12-10 23:52:31 +00001060 unsigned args_used, num_flush_requests = 1;
1061 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -07001063 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001064 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001066 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Mike Snitzer10b81062009-12-10 23:52:31 +00001069 if (dm_target_is_snapshot_merge(ti)) {
1070 num_flush_requests = 2;
1071 origin_mode = FMODE_WRITE;
1072 }
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001075 if (!s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ti->error = "Cannot allocate snapshot context private "
1077 "structure";
1078 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001079 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
Mikulas Patockac2411042010-08-12 04:13:51 +01001082 origin_path = argv[0];
1083 argv++;
1084 argc--;
1085
1086 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1087 if (r) {
1088 ti->error = "Cannot get origin device";
1089 goto bad_origin;
1090 }
1091
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001092 cow_path = argv[0];
1093 argv++;
1094 argc--;
1095
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001096 r = dm_get_device(ti, cow_path, FMODE_READ | FMODE_WRITE, &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001097 if (r) {
1098 ti->error = "Cannot get COW device";
1099 goto bad_cow;
1100 }
1101
1102 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1103 if (r) {
1104 ti->error = "Couldn't create exception store";
1105 r = -EINVAL;
1106 goto bad_store;
1107 }
1108
1109 argv += args_used;
1110 argc -= args_used;
1111
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001112 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001114 s->active = 0;
Mike Snitzerc26655c2009-12-10 23:52:12 +00001115 s->suspended = 0;
Mikulas Patocka879129d2008-10-30 13:33:16 +00001116 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001118 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001119 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001120 s->state_bits = 0;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001121 s->merge_failed = 0;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001122 s->first_merging_chunk = 0;
1123 s->num_merging_chunks = 0;
1124 bio_list_init(&s->bios_queued_during_merge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001127 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ti->error = "Unable to allocate hash table space";
1129 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001130 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001133 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (r) {
1135 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001136 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
Mikulas Patocka92e86812008-07-21 12:00:35 +01001139 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
1140 if (!s->pending_pool) {
1141 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001142 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001143 }
1144
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001145 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
1146 tracked_chunk_cache);
1147 if (!s->tracked_chunk_pool) {
1148 ti->error = "Could not allocate tracked_chunk mempool for "
1149 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +01001150 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001151 }
1152
1153 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1154 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1155
1156 spin_lock_init(&s->tracked_chunk_lock);
1157
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001158 bio_list_init(&s->queued_bios);
1159 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
1160
1161 ti->private = s;
Mike Snitzer10b81062009-12-10 23:52:31 +00001162 ti->num_flush_requests = num_flush_requests;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001163
1164 /* Add snapshot to the list of snapshots for this origin */
1165 /* Exceptions aren't triggered till snapshot_resume() is called */
1166 r = register_snapshot(s);
1167 if (r == -ENOMEM) {
1168 ti->error = "Snapshot origin struct allocation failed";
1169 goto bad_load_and_register;
1170 } else if (r < 0) {
1171 /* invalid handover, register_snapshot has set ti->error */
1172 goto bad_load_and_register;
1173 }
1174
1175 /*
1176 * Metadata must only be loaded into one table at once, so skip this
1177 * if metadata will be handed over during resume.
1178 * Chunk size will be set during the handover - set it to zero to
1179 * ensure it's ignored.
1180 */
1181 if (r > 0) {
1182 s->store->chunk_size = 0;
1183 return 0;
1184 }
1185
Jonathan Brassow493df712009-04-02 19:55:31 +01001186 r = s->store->type->read_metadata(s->store, dm_add_exception,
1187 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001188 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001189 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001190 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001191 } else if (r > 0) {
1192 s->valid = 0;
1193 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001194 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001195
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001196 if (!s->store->chunk_size) {
1197 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001198 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001199 }
Jonathan Brassowd0216842009-04-02 19:55:32 +01001200 ti->split_io = s->store->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 return 0;
1203
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001204bad_read_metadata:
1205 unregister_snapshot(s);
1206
Jonathan Brassowfee19982009-04-02 19:55:34 +01001207bad_load_and_register:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001208 mempool_destroy(s->tracked_chunk_pool);
1209
Jonathan Brassowfee19982009-04-02 19:55:34 +01001210bad_tracked_chunk_pool:
Mikulas Patocka92e86812008-07-21 12:00:35 +01001211 mempool_destroy(s->pending_pool);
1212
Jonathan Brassowfee19982009-04-02 19:55:34 +01001213bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001214 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jonathan Brassowfee19982009-04-02 19:55:34 +01001216bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001217 dm_exception_table_exit(&s->pending, pending_cache);
1218 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Jonathan Brassowfee19982009-04-02 19:55:34 +01001220bad_hash_tables:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001221 dm_exception_store_destroy(s->store);
1222
1223bad_store:
1224 dm_put_device(ti, s->cow);
1225
1226bad_cow:
Mikulas Patockac2411042010-08-12 04:13:51 +01001227 dm_put_device(ti, s->origin);
1228
1229bad_origin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 kfree(s);
1231
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001232bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return r;
1234}
1235
Milan Broz31c93a02006-12-08 02:41:11 -08001236static void __free_exceptions(struct dm_snapshot *s)
1237{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001238 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -08001239 s->kcopyd_client = NULL;
1240
Jon Brassow3510cb92009-12-10 23:52:11 +00001241 dm_exception_table_exit(&s->pending, pending_cache);
1242 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -08001243}
1244
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001245static void __handover_exceptions(struct dm_snapshot *snap_src,
1246 struct dm_snapshot *snap_dest)
1247{
1248 union {
1249 struct dm_exception_table table_swap;
1250 struct dm_exception_store *store_swap;
1251 } u;
1252
1253 /*
1254 * Swap all snapshot context information between the two instances.
1255 */
1256 u.table_swap = snap_dest->complete;
1257 snap_dest->complete = snap_src->complete;
1258 snap_src->complete = u.table_swap;
1259
1260 u.store_swap = snap_dest->store;
1261 snap_dest->store = snap_src->store;
1262 snap_src->store = u.store_swap;
1263
1264 snap_dest->store->snap = snap_dest;
1265 snap_src->store->snap = snap_src;
1266
1267 snap_dest->ti->split_io = snap_dest->store->chunk_size;
1268 snap_dest->valid = snap_src->valid;
1269
1270 /*
1271 * Set source invalid to ensure it receives no further I/O.
1272 */
1273 snap_src->valid = 0;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static void snapshot_dtr(struct dm_target *ti)
1277{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001278#ifdef CONFIG_DM_DEBUG
1279 int i;
1280#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001281 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001282 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001284 flush_workqueue(ksnapd);
1285
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001286 down_read(&_origins_lock);
1287 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001288 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001289 if (snap_src && snap_dest && (s == snap_src)) {
1290 down_write(&snap_dest->lock);
1291 snap_dest->valid = 0;
1292 up_write(&snap_dest->lock);
1293 DMERR("Cancelling snapshot handover.");
1294 }
1295 up_read(&_origins_lock);
1296
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001297 if (dm_target_is_snapshot_merge(ti))
1298 stop_merge(s);
1299
Alasdair G Kergon138728d2006-03-27 01:17:50 -08001300 /* Prevent further origin writes from using this snapshot. */
1301 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 unregister_snapshot(s);
1303
Mikulas Patocka879129d2008-10-30 13:33:16 +00001304 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001305 msleep(1);
Mikulas Patocka879129d2008-10-30 13:33:16 +00001306 /*
1307 * Ensure instructions in mempool_destroy aren't reordered
1308 * before atomic_read.
1309 */
1310 smp_mb();
1311
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001312#ifdef CONFIG_DM_DEBUG
1313 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1314 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1315#endif
1316
1317 mempool_destroy(s->tracked_chunk_pool);
1318
Milan Broz31c93a02006-12-08 02:41:11 -08001319 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Mikulas Patocka92e86812008-07-21 12:00:35 +01001321 mempool_destroy(s->pending_pool);
1322
Jonathan Brassowfee19982009-04-02 19:55:34 +01001323 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728d2006-03-27 01:17:50 -08001324
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001325 dm_put_device(ti, s->cow);
1326
Mikulas Patockac2411042010-08-12 04:13:51 +01001327 dm_put_device(ti, s->origin);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 kfree(s);
1330}
1331
1332/*
1333 * Flush a list of buffers.
1334 */
1335static void flush_bios(struct bio *bio)
1336{
1337 struct bio *n;
1338
1339 while (bio) {
1340 n = bio->bi_next;
1341 bio->bi_next = NULL;
1342 generic_make_request(bio);
1343 bio = n;
1344 }
1345}
1346
David Howellsc4028952006-11-22 14:57:56 +00001347static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001348{
David Howellsc4028952006-11-22 14:57:56 +00001349 struct dm_snapshot *s =
1350 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001351 struct bio *queued_bios;
1352 unsigned long flags;
1353
1354 spin_lock_irqsave(&s->pe_lock, flags);
1355 queued_bios = bio_list_get(&s->queued_bios);
1356 spin_unlock_irqrestore(&s->pe_lock, flags);
1357
1358 flush_bios(queued_bios);
1359}
1360
Mikulas Patocka515ad662009-12-10 23:52:30 +00001361static int do_origin(struct dm_dev *origin, struct bio *bio);
1362
1363/*
1364 * Flush a list of buffers.
1365 */
1366static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1367{
1368 struct bio *n;
1369 int r;
1370
1371 while (bio) {
1372 n = bio->bi_next;
1373 bio->bi_next = NULL;
1374 r = do_origin(s->origin, bio);
1375 if (r == DM_MAPIO_REMAPPED)
1376 generic_make_request(bio);
1377 bio = n;
1378 }
1379}
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/*
1382 * Error a list of buffers.
1383 */
1384static void error_bios(struct bio *bio)
1385{
1386 struct bio *n;
1387
1388 while (bio) {
1389 n = bio->bi_next;
1390 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001391 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 bio = n;
1393 }
1394}
1395
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001396static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001397{
1398 if (!s->valid)
1399 return;
1400
1401 if (err == -EIO)
1402 DMERR("Invalidating snapshot: Error reading/writing.");
1403 else if (err == -ENOMEM)
1404 DMERR("Invalidating snapshot: Unable to allocate exception.");
1405
Jonathan Brassow493df712009-04-02 19:55:31 +01001406 if (s->store->type->drop_snapshot)
1407 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001408
1409 s->valid = 0;
1410
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001411 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001412}
1413
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001414static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001416 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001418 struct bio *origin_bios = NULL;
1419 struct bio *snapshot_bios = NULL;
1420 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001422 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /* Read/write error - snapshot is unusable */
1424 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001425 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001426 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001427 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429
Jon Brassow3510cb92009-12-10 23:52:11 +00001430 e = alloc_completed_exception();
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001431 if (!e) {
1432 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001433 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001434 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001435 goto out;
1436 }
1437 *e = pe->e;
1438
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001439 down_write(&s->lock);
1440 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +00001441 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001442 error = 1;
1443 goto out;
1444 }
1445
Mike Snitzer615d1eb2009-12-10 23:52:29 +00001446 /* Check for conflicting reads */
1447 __check_for_conflicting_io(s, pe->e.old_chunk);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001448
1449 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001450 * Add a proper exception, and remove the
1451 * in-flight exception from the list.
1452 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001453 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 out:
Jon Brassow3510cb92009-12-10 23:52:11 +00001456 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001457 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001458 origin_bios = bio_list_get(&pe->origin_bios);
1459 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001461 increment_pending_exceptions_done_count();
1462
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001463 up_write(&s->lock);
1464
1465 /* Submit any pending write bios */
1466 if (error)
1467 error_bios(snapshot_bios);
1468 else
1469 flush_bios(snapshot_bios);
1470
Mikulas Patocka515ad662009-12-10 23:52:30 +00001471 retry_origin_bios(s, origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
1474static void commit_callback(void *context, int success)
1475{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001476 struct dm_snap_pending_exception *pe = context;
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 pending_complete(pe, success);
1479}
1480
1481/*
1482 * Called when the copy I/O has finished. kcopyd actually runs
1483 * this code so don't block.
1484 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001485static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001487 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 struct dm_snapshot *s = pe->snap;
1489
1490 if (read_err || write_err)
1491 pending_complete(pe, 0);
1492
1493 else
1494 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +01001495 s->store->type->commit_exception(s->store, &pe->e,
1496 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499/*
1500 * Dispatches the copy operation to kcopyd.
1501 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001502static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001505 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 struct block_device *bdev = s->origin->bdev;
1507 sector_t dev_size;
1508
1509 dev_size = get_dev_size(bdev);
1510
1511 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001512 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001513 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001515 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001516 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 dest.count = src.count;
1518
1519 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001520 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 &src, 1, &dest, 0, copy_callback, pe);
1522}
1523
Mikulas Patocka29138082009-04-02 19:55:25 +01001524static struct dm_snap_pending_exception *
1525__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1526{
Jon Brassow3510cb92009-12-10 23:52:11 +00001527 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001528
1529 if (!e)
1530 return NULL;
1531
1532 return container_of(e, struct dm_snap_pending_exception, e);
1533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535/*
1536 * Looks to see if this snapshot already has a pending exception
1537 * for this chunk, otherwise it allocates a new one and inserts
1538 * it into the pending table.
1539 *
1540 * NOTE: a write lock must be held on snap->lock before calling
1541 * this.
1542 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001543static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001544__find_pending_exception(struct dm_snapshot *s,
1545 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Mikulas Patockac6621392009-04-02 19:55:25 +01001547 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001548
Mikulas Patocka29138082009-04-02 19:55:25 +01001549 pe2 = __lookup_pending_exception(s, chunk);
1550 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001551 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001552 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001553 }
1554
1555 pe->e.old_chunk = chunk;
1556 bio_list_init(&pe->origin_bios);
1557 bio_list_init(&pe->snapshot_bios);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001558 pe->started = 0;
1559
Jonathan Brassow493df712009-04-02 19:55:31 +01001560 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001561 free_pending_exception(pe);
1562 return NULL;
1563 }
1564
Jon Brassow3510cb92009-12-10 23:52:11 +00001565 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return pe;
1568}
1569
Jon Brassow1d4989c2009-12-10 23:52:10 +00001570static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001571 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001573 bio->bi_bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001574 bio->bi_sector = chunk_to_sector(s->store,
1575 dm_chunk_number(e->new_chunk) +
1576 (chunk - e->old_chunk)) +
1577 (bio->bi_sector &
1578 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581static int snapshot_map(struct dm_target *ti, struct bio *bio,
1582 union map_info *map_context)
1583{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001584 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001585 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001586 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001588 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Tejun Heod87f4c12010-09-03 11:56:19 +02001590 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001591 bio->bi_bdev = s->cow->bdev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001592 return DM_MAPIO_REMAPPED;
1593 }
1594
Jonathan Brassow71fab002009-04-02 19:55:33 +01001595 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001598 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001600 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001602 /* FIXME: should only take write lock if we need
1603 * to copy an exception */
1604 down_write(&s->lock);
1605
1606 if (!s->valid) {
1607 r = -EIO;
1608 goto out_unlock;
1609 }
1610
1611 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001612 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001613 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001614 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001615 goto out_unlock;
1616 }
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
1619 * Write to snapshot - higher level takes care of RW/RO
1620 * flags so we should only get this if we are
1621 * writeable.
1622 */
1623 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001624 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001625 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001626 up_write(&s->lock);
1627 pe = alloc_pending_exception(s);
1628 down_write(&s->lock);
1629
1630 if (!s->valid) {
1631 free_pending_exception(pe);
1632 r = -EIO;
1633 goto out_unlock;
1634 }
1635
Jon Brassow3510cb92009-12-10 23:52:11 +00001636 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001637 if (e) {
1638 free_pending_exception(pe);
1639 remap_exception(s, e, bio, chunk);
1640 goto out_unlock;
1641 }
1642
Mikulas Patockac6621392009-04-02 19:55:25 +01001643 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001644 if (!pe) {
1645 __invalidate_snapshot(s, -ENOMEM);
1646 r = -EIO;
1647 goto out_unlock;
1648 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001649 }
1650
Milan Brozd74f81f2008-02-08 02:11:27 +00001651 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001652 bio_list_add(&pe->snapshot_bios, bio);
1653
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001654 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001655
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001656 if (!pe->started) {
1657 /* this is protected by snap->lock */
1658 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001659 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001660 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001661 goto out;
1662 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001663 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001664 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001665 map_context->ptr = track_chunk(s, chunk);
1666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001668 out_unlock:
1669 up_write(&s->lock);
1670 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return r;
1672}
1673
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001674/*
1675 * A snapshot-merge target behaves like a combination of a snapshot
1676 * target and a snapshot-origin target. It only generates new
1677 * exceptions in other snapshots and not in the one that is being
1678 * merged.
1679 *
1680 * For each chunk, if there is an existing exception, it is used to
1681 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1682 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001683 * If merging is currently taking place on the chunk in question, the
1684 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001685 */
1686static int snapshot_merge_map(struct dm_target *ti, struct bio *bio,
1687 union map_info *map_context)
1688{
1689 struct dm_exception *e;
1690 struct dm_snapshot *s = ti->private;
1691 int r = DM_MAPIO_REMAPPED;
1692 chunk_t chunk;
1693
Tejun Heod87f4c12010-09-03 11:56:19 +02001694 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001695 if (!map_context->target_request_nr)
Mike Snitzer10b81062009-12-10 23:52:31 +00001696 bio->bi_bdev = s->origin->bdev;
1697 else
1698 bio->bi_bdev = s->cow->bdev;
1699 map_context->ptr = NULL;
1700 return DM_MAPIO_REMAPPED;
1701 }
1702
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001703 chunk = sector_to_chunk(s->store, bio->bi_sector);
1704
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001705 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001706
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001707 /* Full merging snapshots are redirected to the origin */
1708 if (!s->valid)
1709 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001710
1711 /* If the block is already remapped - use that */
1712 e = dm_lookup_exception(&s->complete, chunk);
1713 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001714 /* Queue writes overlapping with chunks being merged */
1715 if (bio_rw(bio) == WRITE &&
1716 chunk >= s->first_merging_chunk &&
1717 chunk < (s->first_merging_chunk +
1718 s->num_merging_chunks)) {
1719 bio->bi_bdev = s->origin->bdev;
1720 bio_list_add(&s->bios_queued_during_merge, bio);
1721 r = DM_MAPIO_SUBMITTED;
1722 goto out_unlock;
1723 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001724
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001725 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001726
1727 if (bio_rw(bio) == WRITE)
1728 map_context->ptr = track_chunk(s, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001729 goto out_unlock;
1730 }
1731
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001732redirect_to_origin:
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001733 bio->bi_bdev = s->origin->bdev;
1734
1735 if (bio_rw(bio) == WRITE) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001736 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001737 return do_origin(s->origin, bio);
1738 }
1739
1740out_unlock:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001741 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001742
1743 return r;
1744}
1745
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001746static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1747 int error, union map_info *map_context)
1748{
1749 struct dm_snapshot *s = ti->private;
1750 struct dm_snap_tracked_chunk *c = map_context->ptr;
1751
1752 if (c)
1753 stop_tracking_chunk(s, c);
1754
1755 return 0;
1756}
1757
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001758static void snapshot_merge_presuspend(struct dm_target *ti)
1759{
1760 struct dm_snapshot *s = ti->private;
1761
1762 stop_merge(s);
1763}
1764
Mike Snitzerc26655c2009-12-10 23:52:12 +00001765static void snapshot_postsuspend(struct dm_target *ti)
1766{
1767 struct dm_snapshot *s = ti->private;
1768
1769 down_write(&s->lock);
1770 s->suspended = 1;
1771 up_write(&s->lock);
1772}
1773
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001774static int snapshot_preresume(struct dm_target *ti)
1775{
1776 int r = 0;
1777 struct dm_snapshot *s = ti->private;
1778 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1779
1780 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001781 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001782 if (snap_src && snap_dest) {
1783 down_read(&snap_src->lock);
1784 if (s == snap_src) {
1785 DMERR("Unable to resume snapshot source until "
1786 "handover completes.");
1787 r = -EINVAL;
1788 } else if (!snap_src->suspended) {
1789 DMERR("Unable to perform snapshot handover until "
1790 "source is suspended.");
1791 r = -EINVAL;
1792 }
1793 up_read(&snap_src->lock);
1794 }
1795 up_read(&_origins_lock);
1796
1797 return r;
1798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800static void snapshot_resume(struct dm_target *ti)
1801{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001802 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001803 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1804
1805 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001806 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001807 if (snap_src && snap_dest) {
1808 down_write(&snap_src->lock);
1809 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1810 __handover_exceptions(snap_src, snap_dest);
1811 up_write(&snap_dest->lock);
1812 up_write(&snap_src->lock);
1813 }
1814 up_read(&_origins_lock);
1815
1816 /* Now we have correct chunk size, reregister */
1817 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001819 down_write(&s->lock);
1820 s->active = 1;
Mike Snitzerc26655c2009-12-10 23:52:12 +00001821 s->suspended = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001822 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001825static sector_t get_origin_minimum_chunksize(struct block_device *bdev)
1826{
1827 sector_t min_chunksize;
1828
1829 down_read(&_origins_lock);
1830 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1831 up_read(&_origins_lock);
1832
1833 return min_chunksize;
1834}
1835
1836static void snapshot_merge_resume(struct dm_target *ti)
1837{
1838 struct dm_snapshot *s = ti->private;
1839
1840 /*
1841 * Handover exceptions from existing snapshot.
1842 */
1843 snapshot_resume(ti);
1844
1845 /*
1846 * snapshot-merge acts as an origin, so set ti->split_io
1847 */
1848 ti->split_io = get_origin_minimum_chunksize(s->origin->bdev);
1849
1850 start_merge(s);
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static int snapshot_status(struct dm_target *ti, status_type_t type,
1854 char *result, unsigned int maxlen)
1855{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001856 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001857 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 switch (type) {
1860 case STATUSTYPE_INFO:
Mikulas Patocka94e76572009-12-10 23:51:53 +00001861
1862 down_write(&snap->lock);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001865 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001866 else if (snap->merge_failed)
1867 DMEMIT("Merge failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00001869 if (snap->store->type->usage) {
1870 sector_t total_sectors, sectors_allocated,
1871 metadata_sectors;
1872 snap->store->type->usage(snap->store,
1873 &total_sectors,
1874 &sectors_allocated,
1875 &metadata_sectors);
1876 DMEMIT("%llu/%llu %llu",
1877 (unsigned long long)sectors_allocated,
1878 (unsigned long long)total_sectors,
1879 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001882 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Mikulas Patocka94e76572009-12-10 23:51:53 +00001884
1885 up_write(&snap->lock);
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
1888
1889 case STATUSTYPE_TABLE:
1890 /*
1891 * kdevname returns a static pointer so we need
1892 * to make private copies if the output is to
1893 * make sense.
1894 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001895 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001896 snap->store->type->status(snap->store, type, result + sz,
1897 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 break;
1899 }
1900
1901 return 0;
1902}
1903
Mike Snitzer8811f462009-09-04 20:40:19 +01001904static int snapshot_iterate_devices(struct dm_target *ti,
1905 iterate_devices_callout_fn fn, void *data)
1906{
1907 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001908 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001909
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001910 r = fn(ti, snap->origin, 0, ti->len, data);
1911
1912 if (!r)
1913 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
1914
1915 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001916}
1917
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919/*-----------------------------------------------------------------
1920 * Origin methods
1921 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001922
1923/*
1924 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
1925 * supplied bio was ignored. The caller may submit it immediately.
1926 * (No remapping actually occurs as the origin is always a direct linear
1927 * map.)
1928 *
1929 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
1930 * and any supplied bio is added to a list to be submitted once all
1931 * the necessary exceptions exist.
1932 */
1933static int __origin_write(struct list_head *snapshots, sector_t sector,
1934 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Mikulas Patocka515ad662009-12-10 23:52:30 +00001936 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001938 struct dm_exception *e;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001939 struct dm_snap_pending_exception *pe;
1940 struct dm_snap_pending_exception *pe_to_start_now = NULL;
1941 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 chunk_t chunk;
1943
1944 /* Do all the snapshots on this origin */
1945 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001946 /*
1947 * Don't make new exceptions in a merging snapshot
1948 * because it has effectively been deleted
1949 */
1950 if (dm_target_is_snapshot_merge(snap->ti))
1951 continue;
1952
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001953 down_write(&snap->lock);
1954
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001955 /* Only deal with valid and active snapshots */
1956 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001957 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001959 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001960 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001961 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /*
1964 * Remember, different snapshots can have
1965 * different chunk sizes.
1966 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001967 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 /*
1970 * Check exception table to see if block
1971 * is already remapped in this snapshot
1972 * and trigger an exception if not.
1973 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001974 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001975 if (e)
1976 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Mikulas Patocka29138082009-04-02 19:55:25 +01001978 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001979 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001980 up_write(&snap->lock);
1981 pe = alloc_pending_exception(snap);
1982 down_write(&snap->lock);
1983
1984 if (!snap->valid) {
1985 free_pending_exception(pe);
1986 goto next_snapshot;
1987 }
1988
Jon Brassow3510cb92009-12-10 23:52:11 +00001989 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001990 if (e) {
1991 free_pending_exception(pe);
1992 goto next_snapshot;
1993 }
1994
Mikulas Patockac6621392009-04-02 19:55:25 +01001995 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001996 if (!pe) {
1997 __invalidate_snapshot(snap, -ENOMEM);
1998 goto next_snapshot;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001
Mikulas Patocka515ad662009-12-10 23:52:30 +00002002 r = DM_MAPIO_SUBMITTED;
2003
2004 /*
2005 * If an origin bio was supplied, queue it to wait for the
2006 * completion of this exception, and start this one last,
2007 * at the end of the function.
2008 */
2009 if (bio) {
2010 bio_list_add(&pe->origin_bios, bio);
2011 bio = NULL;
2012
2013 if (!pe->started) {
2014 pe->started = 1;
2015 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002016 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002017 }
2018
2019 if (!pe->started) {
2020 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002021 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002022 }
2023
2024 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Mikulas Patocka515ad662009-12-10 23:52:30 +00002027 if (pe_to_start_now) {
2028 start_copy(pe_to_start_now);
2029 pe_to_start_now = NULL;
2030 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08002031 }
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00002034 * Submit the exception against which the bio is queued last,
2035 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00002037 if (pe_to_start_last)
2038 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 return r;
2041}
2042
2043/*
2044 * Called on a write from the origin driver.
2045 */
2046static int do_origin(struct dm_dev *origin, struct bio *bio)
2047{
2048 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002049 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 down_read(&_origins_lock);
2052 o = __lookup_origin(origin->bdev);
2053 if (o)
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002054 r = __origin_write(&o->snapshots, bio->bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 up_read(&_origins_lock);
2056
2057 return r;
2058}
2059
2060/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002061 * Trigger exceptions in all non-merging snapshots.
2062 *
2063 * The chunk size of the merging snapshot may be larger than the chunk
2064 * size of some other snapshot so we may need to reallocate multiple
2065 * chunks in other snapshots.
2066 *
2067 * We scan all the overlapping exceptions in the other snapshots.
2068 * Returns 1 if anything was reallocated and must be waited for,
2069 * otherwise returns 0.
2070 *
2071 * size must be a multiple of merging_snap's chunk_size.
2072 */
2073static int origin_write_extent(struct dm_snapshot *merging_snap,
2074 sector_t sector, unsigned size)
2075{
2076 int must_wait = 0;
2077 sector_t n;
2078 struct origin *o;
2079
2080 /*
2081 * The origin's __minimum_chunk_size() got stored in split_io
2082 * by snapshot_merge_resume().
2083 */
2084 down_read(&_origins_lock);
2085 o = __lookup_origin(merging_snap->origin->bdev);
2086 for (n = 0; n < size; n += merging_snap->ti->split_io)
2087 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2088 DM_MAPIO_SUBMITTED)
2089 must_wait = 1;
2090 up_read(&_origins_lock);
2091
2092 return must_wait;
2093}
2094
2095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * Origin: maps a linear range of a device, with hooks for snapshotting.
2097 */
2098
2099/*
2100 * Construct an origin mapping: <dev_path>
2101 * The context for an origin is merely a 'struct dm_dev *'
2102 * pointing to the real device.
2103 */
2104static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2105{
2106 int r;
2107 struct dm_dev *dev;
2108
2109 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002110 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return -EINVAL;
2112 }
2113
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00002114 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (r) {
2116 ti->error = "Cannot get target device";
2117 return r;
2118 }
2119
2120 ti->private = dev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002121 ti->num_flush_requests = 1;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return 0;
2124}
2125
2126static void origin_dtr(struct dm_target *ti)
2127{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002128 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 dm_put_device(ti, dev);
2130}
2131
2132static int origin_map(struct dm_target *ti, struct bio *bio,
2133 union map_info *map_context)
2134{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002135 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 bio->bi_bdev = dev->bdev;
2137
Tejun Heod87f4c12010-09-03 11:56:19 +02002138 if (bio->bi_rw & REQ_FLUSH)
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002139 return DM_MAPIO_REMAPPED;
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002142 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145/*
2146 * Set the target "split_io" field to the minimum of all the snapshots'
2147 * chunk sizes.
2148 */
2149static void origin_resume(struct dm_target *ti)
2150{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002151 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002153 ti->split_io = get_origin_minimum_chunksize(dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
2156static int origin_status(struct dm_target *ti, status_type_t type, char *result,
2157 unsigned int maxlen)
2158{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002159 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 switch (type) {
2162 case STATUSTYPE_INFO:
2163 result[0] = '\0';
2164 break;
2165
2166 case STATUSTYPE_TABLE:
2167 snprintf(result, maxlen, "%s", dev->name);
2168 break;
2169 }
2170
2171 return 0;
2172}
2173
Mikulas Patockab1d55522010-08-12 04:14:02 +01002174static int origin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2175 struct bio_vec *biovec, int max_size)
2176{
2177 struct dm_dev *dev = ti->private;
2178 struct request_queue *q = bdev_get_queue(dev->bdev);
2179
2180 if (!q->merge_bvec_fn)
2181 return max_size;
2182
2183 bvm->bi_bdev = dev->bdev;
2184 bvm->bi_sector = bvm->bi_sector;
2185
2186 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2187}
2188
Mike Snitzer8811f462009-09-04 20:40:19 +01002189static int origin_iterate_devices(struct dm_target *ti,
2190 iterate_devices_callout_fn fn, void *data)
2191{
2192 struct dm_dev *dev = ti->private;
2193
2194 return fn(ti, dev, 0, ti->len, data);
2195}
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197static struct target_type origin_target = {
2198 .name = "snapshot-origin",
Mike Snitzer8811f462009-09-04 20:40:19 +01002199 .version = {1, 7, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 .module = THIS_MODULE,
2201 .ctr = origin_ctr,
2202 .dtr = origin_dtr,
2203 .map = origin_map,
2204 .resume = origin_resume,
2205 .status = origin_status,
Mikulas Patockab1d55522010-08-12 04:14:02 +01002206 .merge = origin_merge,
Mike Snitzer8811f462009-09-04 20:40:19 +01002207 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208};
2209
2210static struct target_type snapshot_target = {
2211 .name = "snapshot",
Mike Snitzerc26655c2009-12-10 23:52:12 +00002212 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 .module = THIS_MODULE,
2214 .ctr = snapshot_ctr,
2215 .dtr = snapshot_dtr,
2216 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002217 .end_io = snapshot_end_io,
Mike Snitzerc26655c2009-12-10 23:52:12 +00002218 .postsuspend = snapshot_postsuspend,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002219 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 .resume = snapshot_resume,
2221 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002222 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223};
2224
Mikulas Patockad698aa42009-12-10 23:52:30 +00002225static struct target_type merge_target = {
2226 .name = dm_snapshot_merge_target_name,
2227 .version = {1, 0, 0},
2228 .module = THIS_MODULE,
2229 .ctr = snapshot_ctr,
2230 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002231 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002232 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002233 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002234 .postsuspend = snapshot_postsuspend,
2235 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002236 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002237 .status = snapshot_status,
2238 .iterate_devices = snapshot_iterate_devices,
2239};
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241static int __init dm_snapshot_init(void)
2242{
2243 int r;
2244
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002245 r = dm_exception_store_init();
2246 if (r) {
2247 DMERR("Failed to initialize exception stores");
2248 return r;
2249 }
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 r = dm_register_target(&snapshot_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002252 if (r < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 DMERR("snapshot target register failed %d", r);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002254 goto bad_register_snapshot_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
2256
2257 r = dm_register_target(&origin_target);
2258 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002259 DMERR("Origin target register failed %d", r);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002260 goto bad_register_origin_target;
2261 }
2262
2263 r = dm_register_target(&merge_target);
2264 if (r < 0) {
2265 DMERR("Merge target register failed %d", r);
2266 goto bad_register_merge_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268
2269 r = init_origin_hash();
2270 if (r) {
2271 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002272 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274
Jon Brassow1d4989c2009-12-10 23:52:10 +00002275 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (!exception_cache) {
2277 DMERR("Couldn't create exception cache.");
2278 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002279 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002282 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (!pending_cache) {
2284 DMERR("Couldn't create pending cache.");
2285 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002286 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002289 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
2290 if (!tracked_chunk_cache) {
2291 DMERR("Couldn't create cache to track chunks in use.");
2292 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002293 goto bad_tracked_chunk_cache;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002294 }
2295
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07002296 ksnapd = create_singlethread_workqueue("ksnapd");
2297 if (!ksnapd) {
2298 DMERR("Failed to create ksnapd workqueue.");
2299 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01002300 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07002301 }
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return 0;
2304
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002305bad_pending_pool:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002306 kmem_cache_destroy(tracked_chunk_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002307bad_tracked_chunk_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 kmem_cache_destroy(pending_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002309bad_pending_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 kmem_cache_destroy(exception_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002311bad_exception_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 exit_origin_hash();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002313bad_origin_hash:
2314 dm_unregister_target(&merge_target);
2315bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002317bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002319bad_register_snapshot_target:
2320 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return r;
2323}
2324
2325static void __exit dm_snapshot_exit(void)
2326{
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07002327 destroy_workqueue(ksnapd);
2328
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002329 dm_unregister_target(&snapshot_target);
2330 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002331 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 kmem_cache_destroy(pending_cache);
2335 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002336 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002337
2338 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
2341/* Module hooks */
2342module_init(dm_snapshot_init);
2343module_exit(dm_snapshot_exit);
2344
2345MODULE_DESCRIPTION(DM_NAME " snapshot target");
2346MODULE_AUTHOR("Joe Thornber");
2347MODULE_LICENSE("GPL");