4 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
6 * This file is released under the GPL.
9 #include <linux/blkdev.h>
10 #include <linux/device-mapper.h>
11 #include <linux/delay.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>
20 #include <linux/log2.h>
21 #include <linux/dm-kcopyd.h>
22 #include <linux/semaphore.h>
26 #include "dm-exception-store.h"
28 #define DM_MSG_PREFIX "snapshots"
30 static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
32 #define dm_target_is_snapshot_merge(ti) \
33 ((ti)->type->name == dm_snapshot_merge_target_name)
36 * The size of the mempool used to track chunks in use.
40 #define DM_TRACKED_CHUNK_HASH_SIZE 16
41 #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
42 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
44 struct dm_exception_table {
47 struct list_head *table;
53 struct dm_dev *origin;
58 /* List of snapshots per Origin */
59 struct list_head list;
62 * You can't use a snapshot if this is 0 (e.g. if full).
63 * A snapshot-merge target never clears this.
68 * The snapshot overflowed because of a write to the snapshot device.
69 * We don't have to invalidate the snapshot in this case, but we need
70 * to prevent further writes.
72 int snapshot_overflowed;
74 /* Origin writes don't trigger exceptions until this is set */
77 atomic_t pending_exceptions_count;
79 /* Protected by "lock" */
80 sector_t exception_start_sequence;
82 /* Protected by kcopyd single-threaded callback */
83 sector_t exception_complete_sequence;
86 * A list of pending exceptions that completed out of order.
87 * Protected by kcopyd single-threaded callback.
89 struct rb_root out_of_order_tree;
91 mempool_t pending_pool;
93 struct dm_exception_table pending;
94 struct dm_exception_table complete;
97 * pe_lock protects all pending_exception operations and access
98 * as well as the snapshot_bios list.
102 /* Chunks with outstanding reads */
103 spinlock_t tracked_chunk_lock;
104 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
106 /* The on disk metadata handler */
107 struct dm_exception_store *store;
109 /* Maximum number of in-flight COW jobs. */
110 struct semaphore cow_count;
112 struct dm_kcopyd_client *kcopyd_client;
114 /* Wait for events based on state_bits */
115 unsigned long state_bits;
117 /* Range of chunks currently being merged. */
118 chunk_t first_merging_chunk;
119 int num_merging_chunks;
122 * The merge operation failed if this flag is set.
123 * Failure modes are handled as follows:
124 * - I/O error reading the header
125 * => don't load the target; abort.
126 * - Header does not have "valid" flag set
127 * => use the origin; forget about the snapshot.
128 * - I/O error when reading exceptions
129 * => don't load the target; abort.
130 * (We can't use the intermediate origin state.)
131 * - I/O error while merging
132 * => stop merging; set merge_failed; process I/O normally.
137 * Incoming bios that overlap with chunks being merged must wait
138 * for them to be committed.
140 struct bio_list bios_queued_during_merge;
145 * RUNNING_MERGE - Merge operation is in progress.
146 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
147 * cleared afterwards.
149 #define RUNNING_MERGE 0
150 #define SHUTDOWN_MERGE 1
153 * Maximum number of chunks being copied on write.
155 * The value was decided experimentally as a trade-off between memory
156 * consumption, stalling the kernel's workqueues and maintaining a high enough
159 #define DEFAULT_COW_THRESHOLD 2048
161 static int cow_threshold = DEFAULT_COW_THRESHOLD;
162 module_param_named(snapshot_cow_threshold, cow_threshold, int, 0644);
163 MODULE_PARM_DESC(snapshot_cow_threshold, "Maximum number of chunks being copied on write");
165 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
166 "A percentage of time allocated for copy on write");
168 struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
172 EXPORT_SYMBOL(dm_snap_origin);
174 struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
178 EXPORT_SYMBOL(dm_snap_cow);
180 static sector_t chunk_to_sector(struct dm_exception_store *store,
183 return chunk << store->chunk_shift;
186 static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
189 * There is only ever one instance of a particular block
190 * device so we can compare pointers safely.
195 struct dm_snap_pending_exception {
196 struct dm_exception e;
199 * Origin buffers waiting for this to complete are held
202 struct bio_list origin_bios;
203 struct bio_list snapshot_bios;
205 /* Pointer back to snapshot context */
206 struct dm_snapshot *snap;
209 * 1 indicates the exception has already been sent to
214 /* There was copying error. */
217 /* A sequence number, it is used for in-order completion. */
218 sector_t exception_sequence;
220 struct rb_node out_of_order_node;
223 * For writing a complete chunk, bypassing the copy.
225 struct bio *full_bio;
226 bio_end_io_t *full_bio_end_io;
230 * Hash table mapping origin volumes to lists of snapshots and
231 * a lock to protect it
233 static struct kmem_cache *exception_cache;
234 static struct kmem_cache *pending_cache;
236 struct dm_snap_tracked_chunk {
237 struct hlist_node node;
241 static void init_tracked_chunk(struct bio *bio)
243 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
244 INIT_HLIST_NODE(&c->node);
247 static bool is_bio_tracked(struct bio *bio)
249 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
250 return !hlist_unhashed(&c->node);
253 static void track_chunk(struct dm_snapshot *s, struct bio *bio, chunk_t chunk)
255 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
259 spin_lock_irq(&s->tracked_chunk_lock);
260 hlist_add_head(&c->node,
261 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
262 spin_unlock_irq(&s->tracked_chunk_lock);
265 static void stop_tracking_chunk(struct dm_snapshot *s, struct bio *bio)
267 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
270 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
272 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
275 static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
277 struct dm_snap_tracked_chunk *c;
280 spin_lock_irq(&s->tracked_chunk_lock);
282 hlist_for_each_entry(c,
283 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
284 if (c->chunk == chunk) {
290 spin_unlock_irq(&s->tracked_chunk_lock);
296 * This conflicting I/O is extremely improbable in the caller,
297 * so msleep(1) is sufficient and there is no need for a wait queue.
299 static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
301 while (__chunk_is_tracked(s, chunk))
306 * One of these per registered origin, held in the snapshot_origins hash
309 /* The origin device */
310 struct block_device *bdev;
312 struct list_head hash_list;
314 /* List of snapshots for this origin */
315 struct list_head snapshots;
319 * This structure is allocated for each origin target
323 struct dm_target *ti;
324 unsigned split_boundary;
325 struct list_head hash_list;
329 * Size of the hash table for origin volumes. If we make this
330 * the size of the minors list then it should be nearly perfect
332 #define ORIGIN_HASH_SIZE 256
333 #define ORIGIN_MASK 0xFF
334 static struct list_head *_origins;
335 static struct list_head *_dm_origins;
336 static struct rw_semaphore _origins_lock;
338 static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
339 static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
340 static uint64_t _pending_exceptions_done_count;
342 static int init_origin_hash(void)
346 _origins = kmalloc_array(ORIGIN_HASH_SIZE, sizeof(struct list_head),
349 DMERR("unable to allocate memory for _origins");
352 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
353 INIT_LIST_HEAD(_origins + i);
355 _dm_origins = kmalloc_array(ORIGIN_HASH_SIZE,
356 sizeof(struct list_head),
359 DMERR("unable to allocate memory for _dm_origins");
363 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
364 INIT_LIST_HEAD(_dm_origins + i);
366 init_rwsem(&_origins_lock);
371 static void exit_origin_hash(void)
377 static unsigned origin_hash(struct block_device *bdev)
379 return bdev->bd_dev & ORIGIN_MASK;
382 static struct origin *__lookup_origin(struct block_device *origin)
384 struct list_head *ol;
387 ol = &_origins[origin_hash(origin)];
388 list_for_each_entry (o, ol, hash_list)
389 if (bdev_equal(o->bdev, origin))
395 static void __insert_origin(struct origin *o)
397 struct list_head *sl = &_origins[origin_hash(o->bdev)];
398 list_add_tail(&o->hash_list, sl);
401 static struct dm_origin *__lookup_dm_origin(struct block_device *origin)
403 struct list_head *ol;
406 ol = &_dm_origins[origin_hash(origin)];
407 list_for_each_entry (o, ol, hash_list)
408 if (bdev_equal(o->dev->bdev, origin))
414 static void __insert_dm_origin(struct dm_origin *o)
416 struct list_head *sl = &_dm_origins[origin_hash(o->dev->bdev)];
417 list_add_tail(&o->hash_list, sl);
420 static void __remove_dm_origin(struct dm_origin *o)
422 list_del(&o->hash_list);
426 * _origins_lock must be held when calling this function.
427 * Returns number of snapshots registered using the supplied cow device, plus:
428 * snap_src - a snapshot suitable for use as a source of exception handover
429 * snap_dest - a snapshot capable of receiving exception handover.
430 * snap_merge - an existing snapshot-merge target linked to the same origin.
431 * There can be at most one snapshot-merge target. The parameter is optional.
433 * Possible return values and states of snap_src and snap_dest.
434 * 0: NULL, NULL - first new snapshot
435 * 1: snap_src, NULL - normal snapshot
436 * 2: snap_src, snap_dest - waiting for handover
437 * 2: snap_src, NULL - handed over, waiting for old to be deleted
438 * 1: NULL, snap_dest - source got destroyed without handover
440 static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
441 struct dm_snapshot **snap_src,
442 struct dm_snapshot **snap_dest,
443 struct dm_snapshot **snap_merge)
445 struct dm_snapshot *s;
450 o = __lookup_origin(snap->origin->bdev);
454 list_for_each_entry(s, &o->snapshots, list) {
455 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
457 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
460 mutex_lock(&s->lock);
462 mutex_unlock(&s->lock);
467 } else if (snap_dest)
478 * On success, returns 1 if this snapshot is a handover destination,
479 * otherwise returns 0.
481 static int __validate_exception_handover(struct dm_snapshot *snap)
483 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
484 struct dm_snapshot *snap_merge = NULL;
486 /* Does snapshot need exceptions handed over to it? */
487 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
488 &snap_merge) == 2) ||
490 snap->ti->error = "Snapshot cow pairing for exception "
491 "table handover failed";
496 * If no snap_src was found, snap cannot become a handover
503 * Non-snapshot-merge handover?
505 if (!dm_target_is_snapshot_merge(snap->ti))
509 * Do not allow more than one merging snapshot.
512 snap->ti->error = "A snapshot is already merging.";
516 if (!snap_src->store->type->prepare_merge ||
517 !snap_src->store->type->commit_merge) {
518 snap->ti->error = "Snapshot exception store does not "
519 "support snapshot-merge.";
526 static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
528 struct dm_snapshot *l;
530 /* Sort the list according to chunk size, largest-first smallest-last */
531 list_for_each_entry(l, &o->snapshots, list)
532 if (l->store->chunk_size < s->store->chunk_size)
534 list_add_tail(&s->list, &l->list);
538 * Make a note of the snapshot and its origin so we can look it
539 * up when the origin has a write on it.
541 * Also validate snapshot exception store handovers.
542 * On success, returns 1 if this registration is a handover destination,
543 * otherwise returns 0.
545 static int register_snapshot(struct dm_snapshot *snap)
547 struct origin *o, *new_o = NULL;
548 struct block_device *bdev = snap->origin->bdev;
551 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
555 down_write(&_origins_lock);
557 r = __validate_exception_handover(snap);
563 o = __lookup_origin(bdev);
570 /* Initialise the struct */
571 INIT_LIST_HEAD(&o->snapshots);
577 __insert_snapshot(o, snap);
580 up_write(&_origins_lock);
586 * Move snapshot to correct place in list according to chunk size.
588 static void reregister_snapshot(struct dm_snapshot *s)
590 struct block_device *bdev = s->origin->bdev;
592 down_write(&_origins_lock);
595 __insert_snapshot(__lookup_origin(bdev), s);
597 up_write(&_origins_lock);
600 static void unregister_snapshot(struct dm_snapshot *s)
604 down_write(&_origins_lock);
605 o = __lookup_origin(s->origin->bdev);
608 if (o && list_empty(&o->snapshots)) {
609 list_del(&o->hash_list);
613 up_write(&_origins_lock);
617 * Implementation of the exception hash tables.
618 * The lowest hash_shift bits of the chunk number are ignored, allowing
619 * some consecutive chunks to be grouped together.
621 static int dm_exception_table_init(struct dm_exception_table *et,
622 uint32_t size, unsigned hash_shift)
626 et->hash_shift = hash_shift;
627 et->hash_mask = size - 1;
628 et->table = dm_vcalloc(size, sizeof(struct list_head));
632 for (i = 0; i < size; i++)
633 INIT_LIST_HEAD(et->table + i);
638 static void dm_exception_table_exit(struct dm_exception_table *et,
639 struct kmem_cache *mem)
641 struct list_head *slot;
642 struct dm_exception *ex, *next;
645 size = et->hash_mask + 1;
646 for (i = 0; i < size; i++) {
647 slot = et->table + i;
649 list_for_each_entry_safe (ex, next, slot, hash_list)
650 kmem_cache_free(mem, ex);
656 static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
658 return (chunk >> et->hash_shift) & et->hash_mask;
661 static void dm_remove_exception(struct dm_exception *e)
663 list_del(&e->hash_list);
667 * Return the exception data for a sector, or NULL if not
670 static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
673 struct list_head *slot;
674 struct dm_exception *e;
676 slot = &et->table[exception_hash(et, chunk)];
677 list_for_each_entry (e, slot, hash_list)
678 if (chunk >= e->old_chunk &&
679 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
685 static struct dm_exception *alloc_completed_exception(gfp_t gfp)
687 struct dm_exception *e;
689 e = kmem_cache_alloc(exception_cache, gfp);
690 if (!e && gfp == GFP_NOIO)
691 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
696 static void free_completed_exception(struct dm_exception *e)
698 kmem_cache_free(exception_cache, e);
701 static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
703 struct dm_snap_pending_exception *pe = mempool_alloc(&s->pending_pool,
706 atomic_inc(&s->pending_exceptions_count);
712 static void free_pending_exception(struct dm_snap_pending_exception *pe)
714 struct dm_snapshot *s = pe->snap;
716 mempool_free(pe, &s->pending_pool);
717 smp_mb__before_atomic();
718 atomic_dec(&s->pending_exceptions_count);
721 static void dm_insert_exception(struct dm_exception_table *eh,
722 struct dm_exception *new_e)
725 struct dm_exception *e = NULL;
727 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
729 /* Add immediately if this table doesn't support consecutive chunks */
733 /* List is ordered by old_chunk */
734 list_for_each_entry_reverse(e, l, hash_list) {
735 /* Insert after an existing chunk? */
736 if (new_e->old_chunk == (e->old_chunk +
737 dm_consecutive_chunk_count(e) + 1) &&
738 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
739 dm_consecutive_chunk_count(e) + 1)) {
740 dm_consecutive_chunk_count_inc(e);
741 free_completed_exception(new_e);
745 /* Insert before an existing chunk? */
746 if (new_e->old_chunk == (e->old_chunk - 1) &&
747 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
748 dm_consecutive_chunk_count_inc(e);
751 free_completed_exception(new_e);
755 if (new_e->old_chunk > e->old_chunk)
760 list_add(&new_e->hash_list, e ? &e->hash_list : l);
764 * Callback used by the exception stores to load exceptions when
767 static int dm_add_exception(void *context, chunk_t old, chunk_t new)
769 struct dm_snapshot *s = context;
770 struct dm_exception *e;
772 e = alloc_completed_exception(GFP_KERNEL);
778 /* Consecutive_count is implicitly initialised to zero */
781 dm_insert_exception(&s->complete, e);
787 * Return a minimum chunk size of all snapshots that have the specified origin.
788 * Return zero if the origin has no snapshots.
790 static uint32_t __minimum_chunk_size(struct origin *o)
792 struct dm_snapshot *snap;
793 unsigned chunk_size = 0;
796 list_for_each_entry(snap, &o->snapshots, list)
797 chunk_size = min_not_zero(chunk_size,
798 snap->store->chunk_size);
800 return (uint32_t) chunk_size;
806 static int calc_max_buckets(void)
808 /* use a fixed size of 2MB */
809 unsigned long mem = 2 * 1024 * 1024;
810 mem /= sizeof(struct list_head);
816 * Allocate room for a suitable hash table.
818 static int init_hash_tables(struct dm_snapshot *s)
820 sector_t hash_size, cow_dev_size, max_buckets;
823 * Calculate based on the size of the original volume or
826 cow_dev_size = get_dev_size(s->cow->bdev);
827 max_buckets = calc_max_buckets();
829 hash_size = cow_dev_size >> s->store->chunk_shift;
830 hash_size = min(hash_size, max_buckets);
834 hash_size = rounddown_pow_of_two(hash_size);
835 if (dm_exception_table_init(&s->complete, hash_size,
836 DM_CHUNK_CONSECUTIVE_BITS))
840 * Allocate hash table for in-flight exceptions
841 * Make this smaller than the real hash table
847 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
848 dm_exception_table_exit(&s->complete, exception_cache);
855 static void merge_shutdown(struct dm_snapshot *s)
857 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
858 smp_mb__after_atomic();
859 wake_up_bit(&s->state_bits, RUNNING_MERGE);
862 static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
864 s->first_merging_chunk = 0;
865 s->num_merging_chunks = 0;
867 return bio_list_get(&s->bios_queued_during_merge);
871 * Remove one chunk from the index of completed exceptions.
873 static int __remove_single_exception_chunk(struct dm_snapshot *s,
876 struct dm_exception *e;
878 e = dm_lookup_exception(&s->complete, old_chunk);
880 DMERR("Corruption detected: exception for block %llu is "
881 "on disk but not in memory",
882 (unsigned long long)old_chunk);
887 * If this is the only chunk using this exception, remove exception.
889 if (!dm_consecutive_chunk_count(e)) {
890 dm_remove_exception(e);
891 free_completed_exception(e);
896 * The chunk may be either at the beginning or the end of a
897 * group of consecutive chunks - never in the middle. We are
898 * removing chunks in the opposite order to that in which they
899 * were added, so this should always be true.
900 * Decrement the consecutive chunk counter and adjust the
901 * starting point if necessary.
903 if (old_chunk == e->old_chunk) {
906 } else if (old_chunk != e->old_chunk +
907 dm_consecutive_chunk_count(e)) {
908 DMERR("Attempt to merge block %llu from the "
909 "middle of a chunk range [%llu - %llu]",
910 (unsigned long long)old_chunk,
911 (unsigned long long)e->old_chunk,
913 e->old_chunk + dm_consecutive_chunk_count(e));
917 dm_consecutive_chunk_count_dec(e);
922 static void flush_bios(struct bio *bio);
924 static int remove_single_exception_chunk(struct dm_snapshot *s)
926 struct bio *b = NULL;
928 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
930 mutex_lock(&s->lock);
933 * Process chunks (and associated exceptions) in reverse order
934 * so that dm_consecutive_chunk_count_dec() accounting works.
937 r = __remove_single_exception_chunk(s, old_chunk);
940 } while (old_chunk-- > s->first_merging_chunk);
942 b = __release_queued_bios_after_merge(s);
945 mutex_unlock(&s->lock);
952 static int origin_write_extent(struct dm_snapshot *merging_snap,
953 sector_t sector, unsigned chunk_size);
955 static void merge_callback(int read_err, unsigned long write_err,
958 static uint64_t read_pending_exceptions_done_count(void)
960 uint64_t pending_exceptions_done;
962 spin_lock(&_pending_exceptions_done_spinlock);
963 pending_exceptions_done = _pending_exceptions_done_count;
964 spin_unlock(&_pending_exceptions_done_spinlock);
966 return pending_exceptions_done;
969 static void increment_pending_exceptions_done_count(void)
971 spin_lock(&_pending_exceptions_done_spinlock);
972 _pending_exceptions_done_count++;
973 spin_unlock(&_pending_exceptions_done_spinlock);
975 wake_up_all(&_pending_exceptions_done);
978 static void snapshot_merge_next_chunks(struct dm_snapshot *s)
980 int i, linear_chunks;
981 chunk_t old_chunk, new_chunk;
982 struct dm_io_region src, dest;
984 uint64_t previous_count;
986 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
987 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
991 * valid flag never changes during merge, so no lock required.
994 DMERR("Snapshot is invalid: can't merge");
998 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
1000 if (linear_chunks <= 0) {
1001 if (linear_chunks < 0) {
1002 DMERR("Read error in exception store: "
1003 "shutting down merge");
1004 mutex_lock(&s->lock);
1005 s->merge_failed = 1;
1006 mutex_unlock(&s->lock);
1011 /* Adjust old_chunk and new_chunk to reflect start of linear region */
1012 old_chunk = old_chunk + 1 - linear_chunks;
1013 new_chunk = new_chunk + 1 - linear_chunks;
1016 * Use one (potentially large) I/O to copy all 'linear_chunks'
1017 * from the exception store to the origin
1019 io_size = linear_chunks * s->store->chunk_size;
1021 dest.bdev = s->origin->bdev;
1022 dest.sector = chunk_to_sector(s->store, old_chunk);
1023 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
1025 src.bdev = s->cow->bdev;
1026 src.sector = chunk_to_sector(s->store, new_chunk);
1027 src.count = dest.count;
1030 * Reallocate any exceptions needed in other snapshots then
1031 * wait for the pending exceptions to complete.
1032 * Each time any pending exception (globally on the system)
1033 * completes we are woken and repeat the process to find out
1034 * if we can proceed. While this may not seem a particularly
1035 * efficient algorithm, it is not expected to have any
1036 * significant impact on performance.
1038 previous_count = read_pending_exceptions_done_count();
1039 while (origin_write_extent(s, dest.sector, io_size)) {
1040 wait_event(_pending_exceptions_done,
1041 (read_pending_exceptions_done_count() !=
1043 /* Retry after the wait, until all exceptions are done. */
1044 previous_count = read_pending_exceptions_done_count();
1047 mutex_lock(&s->lock);
1048 s->first_merging_chunk = old_chunk;
1049 s->num_merging_chunks = linear_chunks;
1050 mutex_unlock(&s->lock);
1052 /* Wait until writes to all 'linear_chunks' drain */
1053 for (i = 0; i < linear_chunks; i++)
1054 __check_for_conflicting_io(s, old_chunk + i);
1056 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
1063 static void error_bios(struct bio *bio);
1065 static void merge_callback(int read_err, unsigned long write_err, void *context)
1067 struct dm_snapshot *s = context;
1068 struct bio *b = NULL;
1070 if (read_err || write_err) {
1072 DMERR("Read error: shutting down merge.");
1074 DMERR("Write error: shutting down merge.");
1078 if (s->store->type->commit_merge(s->store,
1079 s->num_merging_chunks) < 0) {
1080 DMERR("Write error in exception store: shutting down merge");
1084 if (remove_single_exception_chunk(s) < 0)
1087 snapshot_merge_next_chunks(s);
1092 mutex_lock(&s->lock);
1093 s->merge_failed = 1;
1094 b = __release_queued_bios_after_merge(s);
1095 mutex_unlock(&s->lock);
1101 static void start_merge(struct dm_snapshot *s)
1103 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1104 snapshot_merge_next_chunks(s);
1108 * Stop the merging process and wait until it finishes.
1110 static void stop_merge(struct dm_snapshot *s)
1112 set_bit(SHUTDOWN_MERGE, &s->state_bits);
1113 wait_on_bit(&s->state_bits, RUNNING_MERGE, TASK_UNINTERRUPTIBLE);
1114 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1118 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p|po|n> <chunk-size>
1120 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1122 struct dm_snapshot *s;
1125 char *origin_path, *cow_path;
1126 dev_t origin_dev, cow_dev;
1127 unsigned args_used, num_flush_bios = 1;
1128 fmode_t origin_mode = FMODE_READ;
1131 ti->error = "requires exactly 4 arguments";
1136 if (dm_target_is_snapshot_merge(ti)) {
1138 origin_mode = FMODE_WRITE;
1141 s = kzalloc(sizeof(*s), GFP_KERNEL);
1143 ti->error = "Cannot allocate private snapshot structure";
1148 origin_path = argv[0];
1152 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1154 ti->error = "Cannot get origin device";
1157 origin_dev = s->origin->bdev->bd_dev;
1163 cow_dev = dm_get_dev_t(cow_path);
1164 if (cow_dev && cow_dev == origin_dev) {
1165 ti->error = "COW device cannot be the same as origin device";
1170 r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
1172 ti->error = "Cannot get COW device";
1176 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1178 ti->error = "Couldn't create exception store";
1188 s->snapshot_overflowed = 0;
1190 atomic_set(&s->pending_exceptions_count, 0);
1191 s->exception_start_sequence = 0;
1192 s->exception_complete_sequence = 0;
1193 s->out_of_order_tree = RB_ROOT;
1194 mutex_init(&s->lock);
1195 INIT_LIST_HEAD(&s->list);
1196 spin_lock_init(&s->pe_lock);
1198 s->merge_failed = 0;
1199 s->first_merging_chunk = 0;
1200 s->num_merging_chunks = 0;
1201 bio_list_init(&s->bios_queued_during_merge);
1203 /* Allocate hash table for COW data */
1204 if (init_hash_tables(s)) {
1205 ti->error = "Unable to allocate hash table space";
1207 goto bad_hash_tables;
1210 sema_init(&s->cow_count, (cow_threshold > 0) ? cow_threshold : INT_MAX);
1212 s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
1213 if (IS_ERR(s->kcopyd_client)) {
1214 r = PTR_ERR(s->kcopyd_client);
1215 ti->error = "Could not create kcopyd client";
1219 r = mempool_init_slab_pool(&s->pending_pool, MIN_IOS, pending_cache);
1221 ti->error = "Could not allocate mempool for pending exceptions";
1222 goto bad_pending_pool;
1225 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1226 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1228 spin_lock_init(&s->tracked_chunk_lock);
1231 ti->num_flush_bios = num_flush_bios;
1232 ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk);
1234 /* Add snapshot to the list of snapshots for this origin */
1235 /* Exceptions aren't triggered till snapshot_resume() is called */
1236 r = register_snapshot(s);
1238 ti->error = "Snapshot origin struct allocation failed";
1239 goto bad_load_and_register;
1241 /* invalid handover, register_snapshot has set ti->error */
1242 goto bad_load_and_register;
1246 * Metadata must only be loaded into one table at once, so skip this
1247 * if metadata will be handed over during resume.
1248 * Chunk size will be set during the handover - set it to zero to
1249 * ensure it's ignored.
1252 s->store->chunk_size = 0;
1256 r = s->store->type->read_metadata(s->store, dm_add_exception,
1259 ti->error = "Failed to read snapshot metadata";
1260 goto bad_read_metadata;
1263 DMWARN("Snapshot is marked invalid.");
1266 if (!s->store->chunk_size) {
1267 ti->error = "Chunk size not set";
1268 goto bad_read_metadata;
1271 r = dm_set_target_max_io_len(ti, s->store->chunk_size);
1273 goto bad_read_metadata;
1278 unregister_snapshot(s);
1280 bad_load_and_register:
1281 mempool_exit(&s->pending_pool);
1284 dm_kcopyd_client_destroy(s->kcopyd_client);
1287 dm_exception_table_exit(&s->pending, pending_cache);
1288 dm_exception_table_exit(&s->complete, exception_cache);
1291 dm_exception_store_destroy(s->store);
1294 dm_put_device(ti, s->cow);
1297 dm_put_device(ti, s->origin);
1306 static void __free_exceptions(struct dm_snapshot *s)
1308 dm_kcopyd_client_destroy(s->kcopyd_client);
1309 s->kcopyd_client = NULL;
1311 dm_exception_table_exit(&s->pending, pending_cache);
1312 dm_exception_table_exit(&s->complete, exception_cache);
1315 static void __handover_exceptions(struct dm_snapshot *snap_src,
1316 struct dm_snapshot *snap_dest)
1319 struct dm_exception_table table_swap;
1320 struct dm_exception_store *store_swap;
1324 * Swap all snapshot context information between the two instances.
1326 u.table_swap = snap_dest->complete;
1327 snap_dest->complete = snap_src->complete;
1328 snap_src->complete = u.table_swap;
1330 u.store_swap = snap_dest->store;
1331 snap_dest->store = snap_src->store;
1332 snap_dest->store->userspace_supports_overflow = u.store_swap->userspace_supports_overflow;
1333 snap_src->store = u.store_swap;
1335 snap_dest->store->snap = snap_dest;
1336 snap_src->store->snap = snap_src;
1338 snap_dest->ti->max_io_len = snap_dest->store->chunk_size;
1339 snap_dest->valid = snap_src->valid;
1340 snap_dest->snapshot_overflowed = snap_src->snapshot_overflowed;
1343 * Set source invalid to ensure it receives no further I/O.
1345 snap_src->valid = 0;
1348 static void snapshot_dtr(struct dm_target *ti)
1350 #ifdef CONFIG_DM_DEBUG
1353 struct dm_snapshot *s = ti->private;
1354 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1356 down_read(&_origins_lock);
1357 /* Check whether exception handover must be cancelled */
1358 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1359 if (snap_src && snap_dest && (s == snap_src)) {
1360 mutex_lock(&snap_dest->lock);
1361 snap_dest->valid = 0;
1362 mutex_unlock(&snap_dest->lock);
1363 DMERR("Cancelling snapshot handover.");
1365 up_read(&_origins_lock);
1367 if (dm_target_is_snapshot_merge(ti))
1370 /* Prevent further origin writes from using this snapshot. */
1371 /* After this returns there can be no new kcopyd jobs. */
1372 unregister_snapshot(s);
1374 while (atomic_read(&s->pending_exceptions_count))
1377 * Ensure instructions in mempool_exit aren't reordered
1378 * before atomic_read.
1382 #ifdef CONFIG_DM_DEBUG
1383 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1384 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1387 __free_exceptions(s);
1389 mempool_exit(&s->pending_pool);
1391 dm_exception_store_destroy(s->store);
1393 mutex_destroy(&s->lock);
1395 dm_put_device(ti, s->cow);
1397 dm_put_device(ti, s->origin);
1403 * Flush a list of buffers.
1405 static void flush_bios(struct bio *bio)
1411 bio->bi_next = NULL;
1412 generic_make_request(bio);
1417 static int do_origin(struct dm_dev *origin, struct bio *bio);
1420 * Flush a list of buffers.
1422 static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1429 bio->bi_next = NULL;
1430 r = do_origin(s->origin, bio);
1431 if (r == DM_MAPIO_REMAPPED)
1432 generic_make_request(bio);
1438 * Error a list of buffers.
1440 static void error_bios(struct bio *bio)
1446 bio->bi_next = NULL;
1452 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
1458 DMERR("Invalidating snapshot: Error reading/writing.");
1459 else if (err == -ENOMEM)
1460 DMERR("Invalidating snapshot: Unable to allocate exception.");
1462 if (s->store->type->drop_snapshot)
1463 s->store->type->drop_snapshot(s->store);
1467 dm_table_event(s->ti->table);
1470 static void pending_complete(void *context, int success)
1472 struct dm_snap_pending_exception *pe = context;
1473 struct dm_exception *e;
1474 struct dm_snapshot *s = pe->snap;
1475 struct bio *origin_bios = NULL;
1476 struct bio *snapshot_bios = NULL;
1477 struct bio *full_bio = NULL;
1481 /* Read/write error - snapshot is unusable */
1482 mutex_lock(&s->lock);
1483 __invalidate_snapshot(s, -EIO);
1488 e = alloc_completed_exception(GFP_NOIO);
1490 mutex_lock(&s->lock);
1491 __invalidate_snapshot(s, -ENOMEM);
1497 mutex_lock(&s->lock);
1499 free_completed_exception(e);
1504 /* Check for conflicting reads */
1505 __check_for_conflicting_io(s, pe->e.old_chunk);
1508 * Add a proper exception, and remove the
1509 * in-flight exception from the list.
1511 dm_insert_exception(&s->complete, e);
1514 dm_remove_exception(&pe->e);
1515 snapshot_bios = bio_list_get(&pe->snapshot_bios);
1516 origin_bios = bio_list_get(&pe->origin_bios);
1517 full_bio = pe->full_bio;
1519 full_bio->bi_end_io = pe->full_bio_end_io;
1520 increment_pending_exceptions_done_count();
1522 mutex_unlock(&s->lock);
1524 /* Submit any pending write bios */
1527 bio_io_error(full_bio);
1528 error_bios(snapshot_bios);
1531 bio_endio(full_bio);
1532 flush_bios(snapshot_bios);
1535 retry_origin_bios(s, origin_bios);
1537 free_pending_exception(pe);
1540 static void complete_exception(struct dm_snap_pending_exception *pe)
1542 struct dm_snapshot *s = pe->snap;
1544 /* Update the metadata if we are persistent */
1545 s->store->type->commit_exception(s->store, &pe->e, !pe->copy_error,
1546 pending_complete, pe);
1550 * Called when the copy I/O has finished. kcopyd actually runs
1551 * this code so don't block.
1553 static void copy_callback(int read_err, unsigned long write_err, void *context)
1555 struct dm_snap_pending_exception *pe = context;
1556 struct dm_snapshot *s = pe->snap;
1558 pe->copy_error = read_err || write_err;
1560 if (pe->exception_sequence == s->exception_complete_sequence) {
1561 struct rb_node *next;
1563 s->exception_complete_sequence++;
1564 complete_exception(pe);
1566 next = rb_first(&s->out_of_order_tree);
1568 pe = rb_entry(next, struct dm_snap_pending_exception,
1570 if (pe->exception_sequence != s->exception_complete_sequence)
1572 next = rb_next(next);
1573 s->exception_complete_sequence++;
1574 rb_erase(&pe->out_of_order_node, &s->out_of_order_tree);
1575 complete_exception(pe);
1579 struct rb_node *parent = NULL;
1580 struct rb_node **p = &s->out_of_order_tree.rb_node;
1581 struct dm_snap_pending_exception *pe2;
1584 pe2 = rb_entry(*p, struct dm_snap_pending_exception, out_of_order_node);
1587 BUG_ON(pe->exception_sequence == pe2->exception_sequence);
1588 if (pe->exception_sequence < pe2->exception_sequence)
1589 p = &((*p)->rb_left);
1591 p = &((*p)->rb_right);
1594 rb_link_node(&pe->out_of_order_node, parent, p);
1595 rb_insert_color(&pe->out_of_order_node, &s->out_of_order_tree);
1601 * Dispatches the copy operation to kcopyd.
1603 static void start_copy(struct dm_snap_pending_exception *pe)
1605 struct dm_snapshot *s = pe->snap;
1606 struct dm_io_region src, dest;
1607 struct block_device *bdev = s->origin->bdev;
1610 dev_size = get_dev_size(bdev);
1613 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
1614 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
1616 dest.bdev = s->cow->bdev;
1617 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
1618 dest.count = src.count;
1620 /* Hand over to kcopyd */
1621 down(&s->cow_count);
1622 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);
1625 static void full_bio_end_io(struct bio *bio)
1627 void *callback_data = bio->bi_private;
1629 dm_kcopyd_do_callback(callback_data, 0, bio->bi_status ? 1 : 0);
1632 static void start_full_bio(struct dm_snap_pending_exception *pe,
1635 struct dm_snapshot *s = pe->snap;
1636 void *callback_data;
1639 pe->full_bio_end_io = bio->bi_end_io;
1641 down(&s->cow_count);
1642 callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client,
1645 bio->bi_end_io = full_bio_end_io;
1646 bio->bi_private = callback_data;
1648 generic_make_request(bio);
1651 static struct dm_snap_pending_exception *
1652 __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1654 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
1659 return container_of(e, struct dm_snap_pending_exception, e);
1663 * Looks to see if this snapshot already has a pending exception
1664 * for this chunk, otherwise it allocates a new one and inserts
1665 * it into the pending table.
1667 * NOTE: a write lock must be held on snap->lock before calling
1670 static struct dm_snap_pending_exception *
1671 __find_pending_exception(struct dm_snapshot *s,
1672 struct dm_snap_pending_exception *pe, chunk_t chunk)
1674 struct dm_snap_pending_exception *pe2;
1676 pe2 = __lookup_pending_exception(s, chunk);
1678 free_pending_exception(pe);
1682 pe->e.old_chunk = chunk;
1683 bio_list_init(&pe->origin_bios);
1684 bio_list_init(&pe->snapshot_bios);
1686 pe->full_bio = NULL;
1688 if (s->store->type->prepare_exception(s->store, &pe->e)) {
1689 free_pending_exception(pe);
1693 pe->exception_sequence = s->exception_start_sequence++;
1695 dm_insert_exception(&s->pending, &pe->e);
1700 static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
1701 struct bio *bio, chunk_t chunk)
1703 bio_set_dev(bio, s->cow->bdev);
1704 bio->bi_iter.bi_sector =
1705 chunk_to_sector(s->store, dm_chunk_number(e->new_chunk) +
1706 (chunk - e->old_chunk)) +
1707 (bio->bi_iter.bi_sector & s->store->chunk_mask);
1710 static int snapshot_map(struct dm_target *ti, struct bio *bio)
1712 struct dm_exception *e;
1713 struct dm_snapshot *s = ti->private;
1714 int r = DM_MAPIO_REMAPPED;
1716 struct dm_snap_pending_exception *pe = NULL;
1718 init_tracked_chunk(bio);
1720 if (bio->bi_opf & REQ_PREFLUSH) {
1721 bio_set_dev(bio, s->cow->bdev);
1722 return DM_MAPIO_REMAPPED;
1725 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
1727 /* Full snapshots are not usable */
1728 /* To get here the table must be live so s->active is always set. */
1730 return DM_MAPIO_KILL;
1732 mutex_lock(&s->lock);
1734 if (!s->valid || (unlikely(s->snapshot_overflowed) &&
1735 bio_data_dir(bio) == WRITE)) {
1740 /* If the block is already remapped - use that, else remap it */
1741 e = dm_lookup_exception(&s->complete, chunk);
1743 remap_exception(s, e, bio, chunk);
1748 * Write to snapshot - higher level takes care of RW/RO
1749 * flags so we should only get this if we are
1752 if (bio_data_dir(bio) == WRITE) {
1753 pe = __lookup_pending_exception(s, chunk);
1755 mutex_unlock(&s->lock);
1756 pe = alloc_pending_exception(s);
1757 mutex_lock(&s->lock);
1759 if (!s->valid || s->snapshot_overflowed) {
1760 free_pending_exception(pe);
1765 e = dm_lookup_exception(&s->complete, chunk);
1767 free_pending_exception(pe);
1768 remap_exception(s, e, bio, chunk);
1772 pe = __find_pending_exception(s, pe, chunk);
1774 if (s->store->userspace_supports_overflow) {
1775 s->snapshot_overflowed = 1;
1776 DMERR("Snapshot overflowed: Unable to allocate exception.");
1778 __invalidate_snapshot(s, -ENOMEM);
1784 remap_exception(s, &pe->e, bio, chunk);
1786 r = DM_MAPIO_SUBMITTED;
1789 bio->bi_iter.bi_size ==
1790 (s->store->chunk_size << SECTOR_SHIFT)) {
1792 mutex_unlock(&s->lock);
1793 start_full_bio(pe, bio);
1797 bio_list_add(&pe->snapshot_bios, bio);
1800 /* this is protected by snap->lock */
1802 mutex_unlock(&s->lock);
1807 bio_set_dev(bio, s->origin->bdev);
1808 track_chunk(s, bio, chunk);
1812 mutex_unlock(&s->lock);
1818 * A snapshot-merge target behaves like a combination of a snapshot
1819 * target and a snapshot-origin target. It only generates new
1820 * exceptions in other snapshots and not in the one that is being
1823 * For each chunk, if there is an existing exception, it is used to
1824 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1825 * which in turn might generate exceptions in other snapshots.
1826 * If merging is currently taking place on the chunk in question, the
1827 * I/O is deferred by adding it to s->bios_queued_during_merge.
1829 static int snapshot_merge_map(struct dm_target *ti, struct bio *bio)
1831 struct dm_exception *e;
1832 struct dm_snapshot *s = ti->private;
1833 int r = DM_MAPIO_REMAPPED;
1836 init_tracked_chunk(bio);
1838 if (bio->bi_opf & REQ_PREFLUSH) {
1839 if (!dm_bio_get_target_bio_nr(bio))
1840 bio_set_dev(bio, s->origin->bdev);
1842 bio_set_dev(bio, s->cow->bdev);
1843 return DM_MAPIO_REMAPPED;
1846 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
1848 mutex_lock(&s->lock);
1850 /* Full merging snapshots are redirected to the origin */
1852 goto redirect_to_origin;
1854 /* If the block is already remapped - use that */
1855 e = dm_lookup_exception(&s->complete, chunk);
1857 /* Queue writes overlapping with chunks being merged */
1858 if (bio_data_dir(bio) == WRITE &&
1859 chunk >= s->first_merging_chunk &&
1860 chunk < (s->first_merging_chunk +
1861 s->num_merging_chunks)) {
1862 bio_set_dev(bio, s->origin->bdev);
1863 bio_list_add(&s->bios_queued_during_merge, bio);
1864 r = DM_MAPIO_SUBMITTED;
1868 remap_exception(s, e, bio, chunk);
1870 if (bio_data_dir(bio) == WRITE)
1871 track_chunk(s, bio, chunk);
1876 bio_set_dev(bio, s->origin->bdev);
1878 if (bio_data_dir(bio) == WRITE) {
1879 mutex_unlock(&s->lock);
1880 return do_origin(s->origin, bio);
1884 mutex_unlock(&s->lock);
1889 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1890 blk_status_t *error)
1892 struct dm_snapshot *s = ti->private;
1894 if (is_bio_tracked(bio))
1895 stop_tracking_chunk(s, bio);
1897 return DM_ENDIO_DONE;
1900 static void snapshot_merge_presuspend(struct dm_target *ti)
1902 struct dm_snapshot *s = ti->private;
1907 static int snapshot_preresume(struct dm_target *ti)
1910 struct dm_snapshot *s = ti->private;
1911 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1913 down_read(&_origins_lock);
1914 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1915 if (snap_src && snap_dest) {
1916 mutex_lock(&snap_src->lock);
1917 if (s == snap_src) {
1918 DMERR("Unable to resume snapshot source until "
1919 "handover completes.");
1921 } else if (!dm_suspended(snap_src->ti)) {
1922 DMERR("Unable to perform snapshot handover until "
1923 "source is suspended.");
1926 mutex_unlock(&snap_src->lock);
1928 up_read(&_origins_lock);
1933 static void snapshot_resume(struct dm_target *ti)
1935 struct dm_snapshot *s = ti->private;
1936 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL, *snap_merging = NULL;
1937 struct dm_origin *o;
1938 struct mapped_device *origin_md = NULL;
1939 bool must_restart_merging = false;
1941 down_read(&_origins_lock);
1943 o = __lookup_dm_origin(s->origin->bdev);
1945 origin_md = dm_table_get_md(o->ti->table);
1947 (void) __find_snapshots_sharing_cow(s, NULL, NULL, &snap_merging);
1949 origin_md = dm_table_get_md(snap_merging->ti->table);
1951 if (origin_md == dm_table_get_md(ti->table))
1954 if (dm_hold(origin_md))
1958 up_read(&_origins_lock);
1961 dm_internal_suspend_fast(origin_md);
1962 if (snap_merging && test_bit(RUNNING_MERGE, &snap_merging->state_bits)) {
1963 must_restart_merging = true;
1964 stop_merge(snap_merging);
1968 down_read(&_origins_lock);
1970 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1971 if (snap_src && snap_dest) {
1972 mutex_lock(&snap_src->lock);
1973 mutex_lock_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1974 __handover_exceptions(snap_src, snap_dest);
1975 mutex_unlock(&snap_dest->lock);
1976 mutex_unlock(&snap_src->lock);
1979 up_read(&_origins_lock);
1982 if (must_restart_merging)
1983 start_merge(snap_merging);
1984 dm_internal_resume_fast(origin_md);
1988 /* Now we have correct chunk size, reregister */
1989 reregister_snapshot(s);
1991 mutex_lock(&s->lock);
1993 mutex_unlock(&s->lock);
1996 static uint32_t get_origin_minimum_chunksize(struct block_device *bdev)
1998 uint32_t min_chunksize;
2000 down_read(&_origins_lock);
2001 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
2002 up_read(&_origins_lock);
2004 return min_chunksize;
2007 static void snapshot_merge_resume(struct dm_target *ti)
2009 struct dm_snapshot *s = ti->private;
2012 * Handover exceptions from existing snapshot.
2014 snapshot_resume(ti);
2017 * snapshot-merge acts as an origin, so set ti->max_io_len
2019 ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev);
2024 static void snapshot_status(struct dm_target *ti, status_type_t type,
2025 unsigned status_flags, char *result, unsigned maxlen)
2028 struct dm_snapshot *snap = ti->private;
2031 case STATUSTYPE_INFO:
2033 mutex_lock(&snap->lock);
2037 else if (snap->merge_failed)
2038 DMEMIT("Merge failed");
2039 else if (snap->snapshot_overflowed)
2042 if (snap->store->type->usage) {
2043 sector_t total_sectors, sectors_allocated,
2045 snap->store->type->usage(snap->store,
2049 DMEMIT("%llu/%llu %llu",
2050 (unsigned long long)sectors_allocated,
2051 (unsigned long long)total_sectors,
2052 (unsigned long long)metadata_sectors);
2058 mutex_unlock(&snap->lock);
2062 case STATUSTYPE_TABLE:
2064 * kdevname returns a static pointer so we need
2065 * to make private copies if the output is to
2068 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
2069 snap->store->type->status(snap->store, type, result + sz,
2075 static int snapshot_iterate_devices(struct dm_target *ti,
2076 iterate_devices_callout_fn fn, void *data)
2078 struct dm_snapshot *snap = ti->private;
2081 r = fn(ti, snap->origin, 0, ti->len, data);
2084 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
2090 /*-----------------------------------------------------------------
2092 *---------------------------------------------------------------*/
2095 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
2096 * supplied bio was ignored. The caller may submit it immediately.
2097 * (No remapping actually occurs as the origin is always a direct linear
2100 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
2101 * and any supplied bio is added to a list to be submitted once all
2102 * the necessary exceptions exist.
2104 static int __origin_write(struct list_head *snapshots, sector_t sector,
2107 int r = DM_MAPIO_REMAPPED;
2108 struct dm_snapshot *snap;
2109 struct dm_exception *e;
2110 struct dm_snap_pending_exception *pe;
2111 struct dm_snap_pending_exception *pe_to_start_now = NULL;
2112 struct dm_snap_pending_exception *pe_to_start_last = NULL;
2115 /* Do all the snapshots on this origin */
2116 list_for_each_entry (snap, snapshots, list) {
2118 * Don't make new exceptions in a merging snapshot
2119 * because it has effectively been deleted
2121 if (dm_target_is_snapshot_merge(snap->ti))
2124 mutex_lock(&snap->lock);
2126 /* Only deal with valid and active snapshots */
2127 if (!snap->valid || !snap->active)
2130 /* Nothing to do if writing beyond end of snapshot */
2131 if (sector >= dm_table_get_size(snap->ti->table))
2135 * Remember, different snapshots can have
2136 * different chunk sizes.
2138 chunk = sector_to_chunk(snap->store, sector);
2141 * Check exception table to see if block
2142 * is already remapped in this snapshot
2143 * and trigger an exception if not.
2145 e = dm_lookup_exception(&snap->complete, chunk);
2149 pe = __lookup_pending_exception(snap, chunk);
2151 mutex_unlock(&snap->lock);
2152 pe = alloc_pending_exception(snap);
2153 mutex_lock(&snap->lock);
2156 free_pending_exception(pe);
2160 e = dm_lookup_exception(&snap->complete, chunk);
2162 free_pending_exception(pe);
2166 pe = __find_pending_exception(snap, pe, chunk);
2168 __invalidate_snapshot(snap, -ENOMEM);
2173 r = DM_MAPIO_SUBMITTED;
2176 * If an origin bio was supplied, queue it to wait for the
2177 * completion of this exception, and start this one last,
2178 * at the end of the function.
2181 bio_list_add(&pe->origin_bios, bio);
2186 pe_to_start_last = pe;
2192 pe_to_start_now = pe;
2196 mutex_unlock(&snap->lock);
2198 if (pe_to_start_now) {
2199 start_copy(pe_to_start_now);
2200 pe_to_start_now = NULL;
2205 * Submit the exception against which the bio is queued last,
2206 * to give the other exceptions a head start.
2208 if (pe_to_start_last)
2209 start_copy(pe_to_start_last);
2215 * Called on a write from the origin driver.
2217 static int do_origin(struct dm_dev *origin, struct bio *bio)
2220 int r = DM_MAPIO_REMAPPED;
2222 down_read(&_origins_lock);
2223 o = __lookup_origin(origin->bdev);
2225 r = __origin_write(&o->snapshots, bio->bi_iter.bi_sector, bio);
2226 up_read(&_origins_lock);
2232 * Trigger exceptions in all non-merging snapshots.
2234 * The chunk size of the merging snapshot may be larger than the chunk
2235 * size of some other snapshot so we may need to reallocate multiple
2236 * chunks in other snapshots.
2238 * We scan all the overlapping exceptions in the other snapshots.
2239 * Returns 1 if anything was reallocated and must be waited for,
2240 * otherwise returns 0.
2242 * size must be a multiple of merging_snap's chunk_size.
2244 static int origin_write_extent(struct dm_snapshot *merging_snap,
2245 sector_t sector, unsigned size)
2252 * The origin's __minimum_chunk_size() got stored in max_io_len
2253 * by snapshot_merge_resume().
2255 down_read(&_origins_lock);
2256 o = __lookup_origin(merging_snap->origin->bdev);
2257 for (n = 0; n < size; n += merging_snap->ti->max_io_len)
2258 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2261 up_read(&_origins_lock);
2267 * Origin: maps a linear range of a device, with hooks for snapshotting.
2271 * Construct an origin mapping: <dev_path>
2272 * The context for an origin is merely a 'struct dm_dev *'
2273 * pointing to the real device.
2275 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2278 struct dm_origin *o;
2281 ti->error = "origin: incorrect number of arguments";
2285 o = kmalloc(sizeof(struct dm_origin), GFP_KERNEL);
2287 ti->error = "Cannot allocate private origin structure";
2292 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev);
2294 ti->error = "Cannot get target device";
2300 ti->num_flush_bios = 1;
2310 static void origin_dtr(struct dm_target *ti)
2312 struct dm_origin *o = ti->private;
2314 dm_put_device(ti, o->dev);
2318 static int origin_map(struct dm_target *ti, struct bio *bio)
2320 struct dm_origin *o = ti->private;
2321 unsigned available_sectors;
2323 bio_set_dev(bio, o->dev->bdev);
2325 if (unlikely(bio->bi_opf & REQ_PREFLUSH))
2326 return DM_MAPIO_REMAPPED;
2328 if (bio_data_dir(bio) != WRITE)
2329 return DM_MAPIO_REMAPPED;
2331 available_sectors = o->split_boundary -
2332 ((unsigned)bio->bi_iter.bi_sector & (o->split_boundary - 1));
2334 if (bio_sectors(bio) > available_sectors)
2335 dm_accept_partial_bio(bio, available_sectors);
2337 /* Only tell snapshots if this is a write */
2338 return do_origin(o->dev, bio);
2341 static long origin_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
2342 long nr_pages, void **kaddr, pfn_t *pfn)
2344 DMWARN("device does not support dax.");
2349 * Set the target "max_io_len" field to the minimum of all the snapshots'
2352 static void origin_resume(struct dm_target *ti)
2354 struct dm_origin *o = ti->private;
2356 o->split_boundary = get_origin_minimum_chunksize(o->dev->bdev);
2358 down_write(&_origins_lock);
2359 __insert_dm_origin(o);
2360 up_write(&_origins_lock);
2363 static void origin_postsuspend(struct dm_target *ti)
2365 struct dm_origin *o = ti->private;
2367 down_write(&_origins_lock);
2368 __remove_dm_origin(o);
2369 up_write(&_origins_lock);
2372 static void origin_status(struct dm_target *ti, status_type_t type,
2373 unsigned status_flags, char *result, unsigned maxlen)
2375 struct dm_origin *o = ti->private;
2378 case STATUSTYPE_INFO:
2382 case STATUSTYPE_TABLE:
2383 snprintf(result, maxlen, "%s", o->dev->name);
2388 static int origin_iterate_devices(struct dm_target *ti,
2389 iterate_devices_callout_fn fn, void *data)
2391 struct dm_origin *o = ti->private;
2393 return fn(ti, o->dev, 0, ti->len, data);
2396 static struct target_type origin_target = {
2397 .name = "snapshot-origin",
2398 .version = {1, 9, 0},
2399 .module = THIS_MODULE,
2403 .resume = origin_resume,
2404 .postsuspend = origin_postsuspend,
2405 .status = origin_status,
2406 .iterate_devices = origin_iterate_devices,
2407 .direct_access = origin_dax_direct_access,
2410 static struct target_type snapshot_target = {
2412 .version = {1, 15, 0},
2413 .module = THIS_MODULE,
2414 .ctr = snapshot_ctr,
2415 .dtr = snapshot_dtr,
2416 .map = snapshot_map,
2417 .end_io = snapshot_end_io,
2418 .preresume = snapshot_preresume,
2419 .resume = snapshot_resume,
2420 .status = snapshot_status,
2421 .iterate_devices = snapshot_iterate_devices,
2424 static struct target_type merge_target = {
2425 .name = dm_snapshot_merge_target_name,
2426 .version = {1, 4, 0},
2427 .module = THIS_MODULE,
2428 .ctr = snapshot_ctr,
2429 .dtr = snapshot_dtr,
2430 .map = snapshot_merge_map,
2431 .end_io = snapshot_end_io,
2432 .presuspend = snapshot_merge_presuspend,
2433 .preresume = snapshot_preresume,
2434 .resume = snapshot_merge_resume,
2435 .status = snapshot_status,
2436 .iterate_devices = snapshot_iterate_devices,
2439 static int __init dm_snapshot_init(void)
2443 r = dm_exception_store_init();
2445 DMERR("Failed to initialize exception stores");
2449 r = init_origin_hash();
2451 DMERR("init_origin_hash failed.");
2452 goto bad_origin_hash;
2455 exception_cache = KMEM_CACHE(dm_exception, 0);
2456 if (!exception_cache) {
2457 DMERR("Couldn't create exception cache.");
2459 goto bad_exception_cache;
2462 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
2463 if (!pending_cache) {
2464 DMERR("Couldn't create pending cache.");
2466 goto bad_pending_cache;
2469 r = dm_register_target(&snapshot_target);
2471 DMERR("snapshot target register failed %d", r);
2472 goto bad_register_snapshot_target;
2475 r = dm_register_target(&origin_target);
2477 DMERR("Origin target register failed %d", r);
2478 goto bad_register_origin_target;
2481 r = dm_register_target(&merge_target);
2483 DMERR("Merge target register failed %d", r);
2484 goto bad_register_merge_target;
2489 bad_register_merge_target:
2490 dm_unregister_target(&origin_target);
2491 bad_register_origin_target:
2492 dm_unregister_target(&snapshot_target);
2493 bad_register_snapshot_target:
2494 kmem_cache_destroy(pending_cache);
2496 kmem_cache_destroy(exception_cache);
2497 bad_exception_cache:
2500 dm_exception_store_exit();
2505 static void __exit dm_snapshot_exit(void)
2507 dm_unregister_target(&snapshot_target);
2508 dm_unregister_target(&origin_target);
2509 dm_unregister_target(&merge_target);
2512 kmem_cache_destroy(pending_cache);
2513 kmem_cache_destroy(exception_cache);
2515 dm_exception_store_exit();
2519 module_init(dm_snapshot_init);
2520 module_exit(dm_snapshot_exit);
2522 MODULE_DESCRIPTION(DM_NAME " snapshot target");
2523 MODULE_AUTHOR("Joe Thornber");
2524 MODULE_LICENSE("GPL");
2525 MODULE_ALIAS("dm-snapshot-origin");
2526 MODULE_ALIAS("dm-snapshot-merge");