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