dm snapshot: queue writes to chunks being merged
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm-snap.c
blob91a47c522b09ec88214759ca753557ed544ce65f
1 /*
2 * dm-snapshot.c
4 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
6 * This file is released under the GPL.
7 */
9 #include <linux/blkdev.h>
10 #include <linux/device-mapper.h>
11 #include <linux/delay.h>
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>
20 #include <linux/log2.h>
21 #include <linux/dm-kcopyd.h>
22 #include <linux/workqueue.h>
24 #include "dm-exception-store.h"
26 #define DM_MSG_PREFIX "snapshots"
28 static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
30 #define dm_target_is_snapshot_merge(ti) \
31 ((ti)->type->name == dm_snapshot_merge_target_name)
34 * The percentage increment we will wake up users at
36 #define WAKE_UP_PERCENT 5
39 * kcopyd priority of snapshot operations
41 #define SNAPSHOT_COPY_PRIORITY 2
44 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
46 #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
49 * The size of the mempool used to track chunks in use.
51 #define MIN_IOS 256
53 #define DM_TRACKED_CHUNK_HASH_SIZE 16
54 #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
55 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
57 struct dm_exception_table {
58 uint32_t hash_mask;
59 unsigned hash_shift;
60 struct list_head *table;
63 struct dm_snapshot {
64 struct rw_semaphore lock;
66 struct dm_dev *origin;
67 struct dm_dev *cow;
69 struct dm_target *ti;
71 /* List of snapshots per Origin */
72 struct list_head list;
74 /* You can't use a snapshot if this is 0 (e.g. if full) */
75 int valid;
77 /* Origin writes don't trigger exceptions until this is set */
78 int active;
80 /* Whether or not owning mapped_device is suspended */
81 int suspended;
83 mempool_t *pending_pool;
85 atomic_t pending_exceptions_count;
87 struct dm_exception_table pending;
88 struct dm_exception_table complete;
91 * pe_lock protects all pending_exception operations and access
92 * as well as the snapshot_bios list.
94 spinlock_t pe_lock;
96 /* The on disk metadata handler */
97 struct dm_exception_store *store;
99 struct dm_kcopyd_client *kcopyd_client;
101 /* Queue of snapshot writes for ksnapd to flush */
102 struct bio_list queued_bios;
103 struct work_struct queued_bios_work;
105 /* Chunks with outstanding reads */
106 mempool_t *tracked_chunk_pool;
107 spinlock_t tracked_chunk_lock;
108 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
110 /* Wait for events based on state_bits */
111 unsigned long state_bits;
113 /* Range of chunks currently being merged. */
114 chunk_t first_merging_chunk;
115 int num_merging_chunks;
118 * Incoming bios that overlap with chunks being merged must wait
119 * for them to be committed.
121 struct bio_list bios_queued_during_merge;
125 * state_bits:
126 * RUNNING_MERGE - Merge operation is in progress.
127 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
128 * cleared afterwards.
130 #define RUNNING_MERGE 0
131 #define SHUTDOWN_MERGE 1
133 struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
135 return s->cow;
137 EXPORT_SYMBOL(dm_snap_cow);
139 static struct workqueue_struct *ksnapd;
140 static void flush_queued_bios(struct work_struct *work);
142 static sector_t chunk_to_sector(struct dm_exception_store *store,
143 chunk_t chunk)
145 return chunk << store->chunk_shift;
148 static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
151 * There is only ever one instance of a particular block
152 * device so we can compare pointers safely.
154 return lhs == rhs;
157 struct dm_snap_pending_exception {
158 struct dm_exception e;
161 * Origin buffers waiting for this to complete are held
162 * in a bio list
164 struct bio_list origin_bios;
165 struct bio_list snapshot_bios;
167 /* Pointer back to snapshot context */
168 struct dm_snapshot *snap;
171 * 1 indicates the exception has already been sent to
172 * kcopyd.
174 int started;
178 * Hash table mapping origin volumes to lists of snapshots and
179 * a lock to protect it
181 static struct kmem_cache *exception_cache;
182 static struct kmem_cache *pending_cache;
184 struct dm_snap_tracked_chunk {
185 struct hlist_node node;
186 chunk_t chunk;
189 static struct kmem_cache *tracked_chunk_cache;
191 static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
192 chunk_t chunk)
194 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
195 GFP_NOIO);
196 unsigned long flags;
198 c->chunk = chunk;
200 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
201 hlist_add_head(&c->node,
202 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
203 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
205 return c;
208 static void stop_tracking_chunk(struct dm_snapshot *s,
209 struct dm_snap_tracked_chunk *c)
211 unsigned long flags;
213 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
214 hlist_del(&c->node);
215 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
217 mempool_free(c, s->tracked_chunk_pool);
220 static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
222 struct dm_snap_tracked_chunk *c;
223 struct hlist_node *hn;
224 int found = 0;
226 spin_lock_irq(&s->tracked_chunk_lock);
228 hlist_for_each_entry(c, hn,
229 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
230 if (c->chunk == chunk) {
231 found = 1;
232 break;
236 spin_unlock_irq(&s->tracked_chunk_lock);
238 return found;
242 * This conflicting I/O is extremely improbable in the caller,
243 * so msleep(1) is sufficient and there is no need for a wait queue.
245 static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
247 while (__chunk_is_tracked(s, chunk))
248 msleep(1);
252 * One of these per registered origin, held in the snapshot_origins hash
254 struct origin {
255 /* The origin device */
256 struct block_device *bdev;
258 struct list_head hash_list;
260 /* List of snapshots for this origin */
261 struct list_head snapshots;
265 * Size of the hash table for origin volumes. If we make this
266 * the size of the minors list then it should be nearly perfect
268 #define ORIGIN_HASH_SIZE 256
269 #define ORIGIN_MASK 0xFF
270 static struct list_head *_origins;
271 static struct rw_semaphore _origins_lock;
273 static int init_origin_hash(void)
275 int i;
277 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
278 GFP_KERNEL);
279 if (!_origins) {
280 DMERR("unable to allocate memory");
281 return -ENOMEM;
284 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
285 INIT_LIST_HEAD(_origins + i);
286 init_rwsem(&_origins_lock);
288 return 0;
291 static void exit_origin_hash(void)
293 kfree(_origins);
296 static unsigned origin_hash(struct block_device *bdev)
298 return bdev->bd_dev & ORIGIN_MASK;
301 static struct origin *__lookup_origin(struct block_device *origin)
303 struct list_head *ol;
304 struct origin *o;
306 ol = &_origins[origin_hash(origin)];
307 list_for_each_entry (o, ol, hash_list)
308 if (bdev_equal(o->bdev, origin))
309 return o;
311 return NULL;
314 static void __insert_origin(struct origin *o)
316 struct list_head *sl = &_origins[origin_hash(o->bdev)];
317 list_add_tail(&o->hash_list, sl);
321 * _origins_lock must be held when calling this function.
322 * Returns number of snapshots registered using the supplied cow device, plus:
323 * snap_src - a snapshot suitable for use as a source of exception handover
324 * snap_dest - a snapshot capable of receiving exception handover.
325 * snap_merge - an existing snapshot-merge target linked to the same origin.
326 * There can be at most one snapshot-merge target. The parameter is optional.
328 * Possible return values and states of snap_src and snap_dest.
329 * 0: NULL, NULL - first new snapshot
330 * 1: snap_src, NULL - normal snapshot
331 * 2: snap_src, snap_dest - waiting for handover
332 * 2: snap_src, NULL - handed over, waiting for old to be deleted
333 * 1: NULL, snap_dest - source got destroyed without handover
335 static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
336 struct dm_snapshot **snap_src,
337 struct dm_snapshot **snap_dest,
338 struct dm_snapshot **snap_merge)
340 struct dm_snapshot *s;
341 struct origin *o;
342 int count = 0;
343 int active;
345 o = __lookup_origin(snap->origin->bdev);
346 if (!o)
347 goto out;
349 list_for_each_entry(s, &o->snapshots, list) {
350 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
351 *snap_merge = s;
352 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
353 continue;
355 down_read(&s->lock);
356 active = s->active;
357 up_read(&s->lock);
359 if (active) {
360 if (snap_src)
361 *snap_src = s;
362 } else if (snap_dest)
363 *snap_dest = s;
365 count++;
368 out:
369 return count;
373 * On success, returns 1 if this snapshot is a handover destination,
374 * otherwise returns 0.
376 static int __validate_exception_handover(struct dm_snapshot *snap)
378 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
379 struct dm_snapshot *snap_merge = NULL;
381 /* Does snapshot need exceptions handed over to it? */
382 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
383 &snap_merge) == 2) ||
384 snap_dest) {
385 snap->ti->error = "Snapshot cow pairing for exception "
386 "table handover failed";
387 return -EINVAL;
391 * If no snap_src was found, snap cannot become a handover
392 * destination.
394 if (!snap_src)
395 return 0;
398 * Non-snapshot-merge handover?
400 if (!dm_target_is_snapshot_merge(snap->ti))
401 return 1;
404 * Do not allow more than one merging snapshot.
406 if (snap_merge) {
407 snap->ti->error = "A snapshot is already merging.";
408 return -EINVAL;
411 if (!snap_src->store->type->prepare_merge ||
412 !snap_src->store->type->commit_merge) {
413 snap->ti->error = "Snapshot exception store does not "
414 "support snapshot-merge.";
415 return -EINVAL;
418 return 1;
421 static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
423 struct dm_snapshot *l;
425 /* Sort the list according to chunk size, largest-first smallest-last */
426 list_for_each_entry(l, &o->snapshots, list)
427 if (l->store->chunk_size < s->store->chunk_size)
428 break;
429 list_add_tail(&s->list, &l->list);
433 * Make a note of the snapshot and its origin so we can look it
434 * up when the origin has a write on it.
436 * Also validate snapshot exception store handovers.
437 * On success, returns 1 if this registration is a handover destination,
438 * otherwise returns 0.
440 static int register_snapshot(struct dm_snapshot *snap)
442 struct origin *o, *new_o = NULL;
443 struct block_device *bdev = snap->origin->bdev;
444 int r = 0;
446 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
447 if (!new_o)
448 return -ENOMEM;
450 down_write(&_origins_lock);
452 r = __validate_exception_handover(snap);
453 if (r < 0) {
454 kfree(new_o);
455 goto out;
458 o = __lookup_origin(bdev);
459 if (o)
460 kfree(new_o);
461 else {
462 /* New origin */
463 o = new_o;
465 /* Initialise the struct */
466 INIT_LIST_HEAD(&o->snapshots);
467 o->bdev = bdev;
469 __insert_origin(o);
472 __insert_snapshot(o, snap);
474 out:
475 up_write(&_origins_lock);
477 return r;
481 * Move snapshot to correct place in list according to chunk size.
483 static void reregister_snapshot(struct dm_snapshot *s)
485 struct block_device *bdev = s->origin->bdev;
487 down_write(&_origins_lock);
489 list_del(&s->list);
490 __insert_snapshot(__lookup_origin(bdev), s);
492 up_write(&_origins_lock);
495 static void unregister_snapshot(struct dm_snapshot *s)
497 struct origin *o;
499 down_write(&_origins_lock);
500 o = __lookup_origin(s->origin->bdev);
502 list_del(&s->list);
503 if (o && list_empty(&o->snapshots)) {
504 list_del(&o->hash_list);
505 kfree(o);
508 up_write(&_origins_lock);
512 * Implementation of the exception hash tables.
513 * The lowest hash_shift bits of the chunk number are ignored, allowing
514 * some consecutive chunks to be grouped together.
516 static int dm_exception_table_init(struct dm_exception_table *et,
517 uint32_t size, unsigned hash_shift)
519 unsigned int i;
521 et->hash_shift = hash_shift;
522 et->hash_mask = size - 1;
523 et->table = dm_vcalloc(size, sizeof(struct list_head));
524 if (!et->table)
525 return -ENOMEM;
527 for (i = 0; i < size; i++)
528 INIT_LIST_HEAD(et->table + i);
530 return 0;
533 static void dm_exception_table_exit(struct dm_exception_table *et,
534 struct kmem_cache *mem)
536 struct list_head *slot;
537 struct dm_exception *ex, *next;
538 int i, size;
540 size = et->hash_mask + 1;
541 for (i = 0; i < size; i++) {
542 slot = et->table + i;
544 list_for_each_entry_safe (ex, next, slot, hash_list)
545 kmem_cache_free(mem, ex);
548 vfree(et->table);
551 static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
553 return (chunk >> et->hash_shift) & et->hash_mask;
556 static void dm_remove_exception(struct dm_exception *e)
558 list_del(&e->hash_list);
562 * Return the exception data for a sector, or NULL if not
563 * remapped.
565 static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
566 chunk_t chunk)
568 struct list_head *slot;
569 struct dm_exception *e;
571 slot = &et->table[exception_hash(et, chunk)];
572 list_for_each_entry (e, slot, hash_list)
573 if (chunk >= e->old_chunk &&
574 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
575 return e;
577 return NULL;
580 static struct dm_exception *alloc_completed_exception(void)
582 struct dm_exception *e;
584 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
585 if (!e)
586 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
588 return e;
591 static void free_completed_exception(struct dm_exception *e)
593 kmem_cache_free(exception_cache, e);
596 static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
598 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
599 GFP_NOIO);
601 atomic_inc(&s->pending_exceptions_count);
602 pe->snap = s;
604 return pe;
607 static void free_pending_exception(struct dm_snap_pending_exception *pe)
609 struct dm_snapshot *s = pe->snap;
611 mempool_free(pe, s->pending_pool);
612 smp_mb__before_atomic_dec();
613 atomic_dec(&s->pending_exceptions_count);
616 static void dm_insert_exception(struct dm_exception_table *eh,
617 struct dm_exception *new_e)
619 struct list_head *l;
620 struct dm_exception *e = NULL;
622 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
624 /* Add immediately if this table doesn't support consecutive chunks */
625 if (!eh->hash_shift)
626 goto out;
628 /* List is ordered by old_chunk */
629 list_for_each_entry_reverse(e, l, hash_list) {
630 /* Insert after an existing chunk? */
631 if (new_e->old_chunk == (e->old_chunk +
632 dm_consecutive_chunk_count(e) + 1) &&
633 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
634 dm_consecutive_chunk_count(e) + 1)) {
635 dm_consecutive_chunk_count_inc(e);
636 free_completed_exception(new_e);
637 return;
640 /* Insert before an existing chunk? */
641 if (new_e->old_chunk == (e->old_chunk - 1) &&
642 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
643 dm_consecutive_chunk_count_inc(e);
644 e->old_chunk--;
645 e->new_chunk--;
646 free_completed_exception(new_e);
647 return;
650 if (new_e->old_chunk > e->old_chunk)
651 break;
654 out:
655 list_add(&new_e->hash_list, e ? &e->hash_list : l);
659 * Callback used by the exception stores to load exceptions when
660 * initialising.
662 static int dm_add_exception(void *context, chunk_t old, chunk_t new)
664 struct dm_snapshot *s = context;
665 struct dm_exception *e;
667 e = alloc_completed_exception();
668 if (!e)
669 return -ENOMEM;
671 e->old_chunk = old;
673 /* Consecutive_count is implicitly initialised to zero */
674 e->new_chunk = new;
676 dm_insert_exception(&s->complete, e);
678 return 0;
681 #define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
684 * Return a minimum chunk size of all snapshots that have the specified origin.
685 * Return zero if the origin has no snapshots.
687 static sector_t __minimum_chunk_size(struct origin *o)
689 struct dm_snapshot *snap;
690 unsigned chunk_size = 0;
692 if (o)
693 list_for_each_entry(snap, &o->snapshots, list)
694 chunk_size = min_not_zero(chunk_size,
695 snap->store->chunk_size);
697 return chunk_size;
701 * Hard coded magic.
703 static int calc_max_buckets(void)
705 /* use a fixed size of 2MB */
706 unsigned long mem = 2 * 1024 * 1024;
707 mem /= sizeof(struct list_head);
709 return mem;
713 * Allocate room for a suitable hash table.
715 static int init_hash_tables(struct dm_snapshot *s)
717 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
720 * Calculate based on the size of the original volume or
721 * the COW volume...
723 cow_dev_size = get_dev_size(s->cow->bdev);
724 origin_dev_size = get_dev_size(s->origin->bdev);
725 max_buckets = calc_max_buckets();
727 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
728 hash_size = min(hash_size, max_buckets);
730 if (hash_size < 64)
731 hash_size = 64;
732 hash_size = rounddown_pow_of_two(hash_size);
733 if (dm_exception_table_init(&s->complete, hash_size,
734 DM_CHUNK_CONSECUTIVE_BITS))
735 return -ENOMEM;
738 * Allocate hash table for in-flight exceptions
739 * Make this smaller than the real hash table
741 hash_size >>= 3;
742 if (hash_size < 64)
743 hash_size = 64;
745 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
746 dm_exception_table_exit(&s->complete, exception_cache);
747 return -ENOMEM;
750 return 0;
753 static void merge_shutdown(struct dm_snapshot *s)
755 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
756 smp_mb__after_clear_bit();
757 wake_up_bit(&s->state_bits, RUNNING_MERGE);
760 static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
762 s->first_merging_chunk = 0;
763 s->num_merging_chunks = 0;
765 return bio_list_get(&s->bios_queued_during_merge);
769 * Remove one chunk from the index of completed exceptions.
771 static int __remove_single_exception_chunk(struct dm_snapshot *s,
772 chunk_t old_chunk)
774 struct dm_exception *e;
776 e = dm_lookup_exception(&s->complete, old_chunk);
777 if (!e) {
778 DMERR("Corruption detected: exception for block %llu is "
779 "on disk but not in memory",
780 (unsigned long long)old_chunk);
781 return -EINVAL;
785 * If this is the only chunk using this exception, remove exception.
787 if (!dm_consecutive_chunk_count(e)) {
788 dm_remove_exception(e);
789 free_completed_exception(e);
790 return 0;
794 * The chunk may be either at the beginning or the end of a
795 * group of consecutive chunks - never in the middle. We are
796 * removing chunks in the opposite order to that in which they
797 * were added, so this should always be true.
798 * Decrement the consecutive chunk counter and adjust the
799 * starting point if necessary.
801 if (old_chunk == e->old_chunk) {
802 e->old_chunk++;
803 e->new_chunk++;
804 } else if (old_chunk != e->old_chunk +
805 dm_consecutive_chunk_count(e)) {
806 DMERR("Attempt to merge block %llu from the "
807 "middle of a chunk range [%llu - %llu]",
808 (unsigned long long)old_chunk,
809 (unsigned long long)e->old_chunk,
810 (unsigned long long)
811 e->old_chunk + dm_consecutive_chunk_count(e));
812 return -EINVAL;
815 dm_consecutive_chunk_count_dec(e);
817 return 0;
820 static void flush_bios(struct bio *bio);
822 static int remove_single_exception_chunk(struct dm_snapshot *s)
824 struct bio *b = NULL;
825 int r;
826 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
828 down_write(&s->lock);
831 * Process chunks (and associated exceptions) in reverse order
832 * so that dm_consecutive_chunk_count_dec() accounting works.
834 do {
835 r = __remove_single_exception_chunk(s, old_chunk);
836 if (r)
837 goto out;
838 } while (old_chunk-- > s->first_merging_chunk);
840 b = __release_queued_bios_after_merge(s);
842 out:
843 up_write(&s->lock);
844 if (b)
845 flush_bios(b);
847 return r;
850 static void merge_callback(int read_err, unsigned long write_err,
851 void *context);
853 static void snapshot_merge_next_chunks(struct dm_snapshot *s)
855 int r;
856 chunk_t old_chunk, new_chunk;
857 struct dm_io_region src, dest;
859 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
860 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
861 goto shut;
864 * valid flag never changes during merge, so no lock required.
866 if (!s->valid) {
867 DMERR("Snapshot is invalid: can't merge");
868 goto shut;
871 r = s->store->type->prepare_merge(s->store, &old_chunk, &new_chunk);
872 if (r <= 0) {
873 if (r < 0)
874 DMERR("Read error in exception store: "
875 "shutting down merge");
876 goto shut;
879 /* TODO: use larger I/O size once we verify that kcopyd handles it */
881 dest.bdev = s->origin->bdev;
882 dest.sector = chunk_to_sector(s->store, old_chunk);
883 dest.count = min((sector_t)s->store->chunk_size,
884 get_dev_size(dest.bdev) - dest.sector);
886 src.bdev = s->cow->bdev;
887 src.sector = chunk_to_sector(s->store, new_chunk);
888 src.count = dest.count;
890 down_write(&s->lock);
891 s->first_merging_chunk = old_chunk;
892 s->num_merging_chunks = 1;
893 up_write(&s->lock);
895 /* !!! FIXME: wait until writes to this chunk drain */
897 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
898 return;
900 shut:
901 merge_shutdown(s);
904 static void error_bios(struct bio *bio);
906 static void merge_callback(int read_err, unsigned long write_err, void *context)
908 struct dm_snapshot *s = context;
909 struct bio *b = NULL;
911 if (read_err || write_err) {
912 if (read_err)
913 DMERR("Read error: shutting down merge.");
914 else
915 DMERR("Write error: shutting down merge.");
916 goto shut;
919 if (s->store->type->commit_merge(s->store,
920 s->num_merging_chunks) < 0) {
921 DMERR("Write error in exception store: shutting down merge");
922 goto shut;
925 if (remove_single_exception_chunk(s) < 0)
926 goto shut;
928 snapshot_merge_next_chunks(s);
930 return;
932 shut:
933 down_write(&s->lock);
934 b = __release_queued_bios_after_merge(s);
935 up_write(&s->lock);
936 error_bios(b);
938 merge_shutdown(s);
941 static void start_merge(struct dm_snapshot *s)
943 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
944 snapshot_merge_next_chunks(s);
947 static int wait_schedule(void *ptr)
949 schedule();
951 return 0;
955 * Stop the merging process and wait until it finishes.
957 static void stop_merge(struct dm_snapshot *s)
959 set_bit(SHUTDOWN_MERGE, &s->state_bits);
960 wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule,
961 TASK_UNINTERRUPTIBLE);
962 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
966 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
968 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
970 struct dm_snapshot *s;
971 int i;
972 int r = -EINVAL;
973 char *origin_path, *cow_path;
974 unsigned args_used, num_flush_requests = 1;
975 fmode_t origin_mode = FMODE_READ;
977 if (argc != 4) {
978 ti->error = "requires exactly 4 arguments";
979 r = -EINVAL;
980 goto bad;
983 if (dm_target_is_snapshot_merge(ti)) {
984 num_flush_requests = 2;
985 origin_mode = FMODE_WRITE;
988 origin_path = argv[0];
989 argv++;
990 argc--;
992 s = kmalloc(sizeof(*s), GFP_KERNEL);
993 if (!s) {
994 ti->error = "Cannot allocate snapshot context private "
995 "structure";
996 r = -ENOMEM;
997 goto bad;
1000 cow_path = argv[0];
1001 argv++;
1002 argc--;
1004 r = dm_get_device(ti, cow_path, 0, 0,
1005 FMODE_READ | FMODE_WRITE, &s->cow);
1006 if (r) {
1007 ti->error = "Cannot get COW device";
1008 goto bad_cow;
1011 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1012 if (r) {
1013 ti->error = "Couldn't create exception store";
1014 r = -EINVAL;
1015 goto bad_store;
1018 argv += args_used;
1019 argc -= args_used;
1021 r = dm_get_device(ti, origin_path, 0, ti->len, origin_mode, &s->origin);
1022 if (r) {
1023 ti->error = "Cannot get origin device";
1024 goto bad_origin;
1027 s->ti = ti;
1028 s->valid = 1;
1029 s->active = 0;
1030 s->suspended = 0;
1031 atomic_set(&s->pending_exceptions_count, 0);
1032 init_rwsem(&s->lock);
1033 INIT_LIST_HEAD(&s->list);
1034 spin_lock_init(&s->pe_lock);
1035 s->state_bits = 0;
1036 s->first_merging_chunk = 0;
1037 s->num_merging_chunks = 0;
1038 bio_list_init(&s->bios_queued_during_merge);
1040 /* Allocate hash table for COW data */
1041 if (init_hash_tables(s)) {
1042 ti->error = "Unable to allocate hash table space";
1043 r = -ENOMEM;
1044 goto bad_hash_tables;
1047 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
1048 if (r) {
1049 ti->error = "Could not create kcopyd client";
1050 goto bad_kcopyd;
1053 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
1054 if (!s->pending_pool) {
1055 ti->error = "Could not allocate mempool for pending exceptions";
1056 goto bad_pending_pool;
1059 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
1060 tracked_chunk_cache);
1061 if (!s->tracked_chunk_pool) {
1062 ti->error = "Could not allocate tracked_chunk mempool for "
1063 "tracking reads";
1064 goto bad_tracked_chunk_pool;
1067 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1068 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1070 spin_lock_init(&s->tracked_chunk_lock);
1072 bio_list_init(&s->queued_bios);
1073 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
1075 ti->private = s;
1076 ti->num_flush_requests = num_flush_requests;
1078 /* Add snapshot to the list of snapshots for this origin */
1079 /* Exceptions aren't triggered till snapshot_resume() is called */
1080 r = register_snapshot(s);
1081 if (r == -ENOMEM) {
1082 ti->error = "Snapshot origin struct allocation failed";
1083 goto bad_load_and_register;
1084 } else if (r < 0) {
1085 /* invalid handover, register_snapshot has set ti->error */
1086 goto bad_load_and_register;
1090 * Metadata must only be loaded into one table at once, so skip this
1091 * if metadata will be handed over during resume.
1092 * Chunk size will be set during the handover - set it to zero to
1093 * ensure it's ignored.
1095 if (r > 0) {
1096 s->store->chunk_size = 0;
1097 return 0;
1100 r = s->store->type->read_metadata(s->store, dm_add_exception,
1101 (void *)s);
1102 if (r < 0) {
1103 ti->error = "Failed to read snapshot metadata";
1104 goto bad_read_metadata;
1105 } else if (r > 0) {
1106 s->valid = 0;
1107 DMWARN("Snapshot is marked invalid.");
1110 if (!s->store->chunk_size) {
1111 ti->error = "Chunk size not set";
1112 goto bad_read_metadata;
1114 ti->split_io = s->store->chunk_size;
1116 return 0;
1118 bad_read_metadata:
1119 unregister_snapshot(s);
1121 bad_load_and_register:
1122 mempool_destroy(s->tracked_chunk_pool);
1124 bad_tracked_chunk_pool:
1125 mempool_destroy(s->pending_pool);
1127 bad_pending_pool:
1128 dm_kcopyd_client_destroy(s->kcopyd_client);
1130 bad_kcopyd:
1131 dm_exception_table_exit(&s->pending, pending_cache);
1132 dm_exception_table_exit(&s->complete, exception_cache);
1134 bad_hash_tables:
1135 dm_put_device(ti, s->origin);
1137 bad_origin:
1138 dm_exception_store_destroy(s->store);
1140 bad_store:
1141 dm_put_device(ti, s->cow);
1143 bad_cow:
1144 kfree(s);
1146 bad:
1147 return r;
1150 static void __free_exceptions(struct dm_snapshot *s)
1152 dm_kcopyd_client_destroy(s->kcopyd_client);
1153 s->kcopyd_client = NULL;
1155 dm_exception_table_exit(&s->pending, pending_cache);
1156 dm_exception_table_exit(&s->complete, exception_cache);
1159 static void __handover_exceptions(struct dm_snapshot *snap_src,
1160 struct dm_snapshot *snap_dest)
1162 union {
1163 struct dm_exception_table table_swap;
1164 struct dm_exception_store *store_swap;
1165 } u;
1168 * Swap all snapshot context information between the two instances.
1170 u.table_swap = snap_dest->complete;
1171 snap_dest->complete = snap_src->complete;
1172 snap_src->complete = u.table_swap;
1174 u.store_swap = snap_dest->store;
1175 snap_dest->store = snap_src->store;
1176 snap_src->store = u.store_swap;
1178 snap_dest->store->snap = snap_dest;
1179 snap_src->store->snap = snap_src;
1181 snap_dest->ti->split_io = snap_dest->store->chunk_size;
1182 snap_dest->valid = snap_src->valid;
1185 * Set source invalid to ensure it receives no further I/O.
1187 snap_src->valid = 0;
1190 static void snapshot_dtr(struct dm_target *ti)
1192 #ifdef CONFIG_DM_DEBUG
1193 int i;
1194 #endif
1195 struct dm_snapshot *s = ti->private;
1196 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1198 flush_workqueue(ksnapd);
1200 down_read(&_origins_lock);
1201 /* Check whether exception handover must be cancelled */
1202 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1203 if (snap_src && snap_dest && (s == snap_src)) {
1204 down_write(&snap_dest->lock);
1205 snap_dest->valid = 0;
1206 up_write(&snap_dest->lock);
1207 DMERR("Cancelling snapshot handover.");
1209 up_read(&_origins_lock);
1211 if (dm_target_is_snapshot_merge(ti))
1212 stop_merge(s);
1214 /* Prevent further origin writes from using this snapshot. */
1215 /* After this returns there can be no new kcopyd jobs. */
1216 unregister_snapshot(s);
1218 while (atomic_read(&s->pending_exceptions_count))
1219 msleep(1);
1221 * Ensure instructions in mempool_destroy aren't reordered
1222 * before atomic_read.
1224 smp_mb();
1226 #ifdef CONFIG_DM_DEBUG
1227 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1228 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1229 #endif
1231 mempool_destroy(s->tracked_chunk_pool);
1233 __free_exceptions(s);
1235 mempool_destroy(s->pending_pool);
1237 dm_put_device(ti, s->origin);
1239 dm_exception_store_destroy(s->store);
1241 dm_put_device(ti, s->cow);
1243 kfree(s);
1247 * Flush a list of buffers.
1249 static void flush_bios(struct bio *bio)
1251 struct bio *n;
1253 while (bio) {
1254 n = bio->bi_next;
1255 bio->bi_next = NULL;
1256 generic_make_request(bio);
1257 bio = n;
1261 static void flush_queued_bios(struct work_struct *work)
1263 struct dm_snapshot *s =
1264 container_of(work, struct dm_snapshot, queued_bios_work);
1265 struct bio *queued_bios;
1266 unsigned long flags;
1268 spin_lock_irqsave(&s->pe_lock, flags);
1269 queued_bios = bio_list_get(&s->queued_bios);
1270 spin_unlock_irqrestore(&s->pe_lock, flags);
1272 flush_bios(queued_bios);
1275 static int do_origin(struct dm_dev *origin, struct bio *bio);
1278 * Flush a list of buffers.
1280 static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1282 struct bio *n;
1283 int r;
1285 while (bio) {
1286 n = bio->bi_next;
1287 bio->bi_next = NULL;
1288 r = do_origin(s->origin, bio);
1289 if (r == DM_MAPIO_REMAPPED)
1290 generic_make_request(bio);
1291 bio = n;
1296 * Error a list of buffers.
1298 static void error_bios(struct bio *bio)
1300 struct bio *n;
1302 while (bio) {
1303 n = bio->bi_next;
1304 bio->bi_next = NULL;
1305 bio_io_error(bio);
1306 bio = n;
1310 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
1312 if (!s->valid)
1313 return;
1315 if (err == -EIO)
1316 DMERR("Invalidating snapshot: Error reading/writing.");
1317 else if (err == -ENOMEM)
1318 DMERR("Invalidating snapshot: Unable to allocate exception.");
1320 if (s->store->type->drop_snapshot)
1321 s->store->type->drop_snapshot(s->store);
1323 s->valid = 0;
1325 dm_table_event(s->ti->table);
1328 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
1330 struct dm_exception *e;
1331 struct dm_snapshot *s = pe->snap;
1332 struct bio *origin_bios = NULL;
1333 struct bio *snapshot_bios = NULL;
1334 int error = 0;
1336 if (!success) {
1337 /* Read/write error - snapshot is unusable */
1338 down_write(&s->lock);
1339 __invalidate_snapshot(s, -EIO);
1340 error = 1;
1341 goto out;
1344 e = alloc_completed_exception();
1345 if (!e) {
1346 down_write(&s->lock);
1347 __invalidate_snapshot(s, -ENOMEM);
1348 error = 1;
1349 goto out;
1351 *e = pe->e;
1353 down_write(&s->lock);
1354 if (!s->valid) {
1355 free_completed_exception(e);
1356 error = 1;
1357 goto out;
1360 /* Check for conflicting reads */
1361 __check_for_conflicting_io(s, pe->e.old_chunk);
1364 * Add a proper exception, and remove the
1365 * in-flight exception from the list.
1367 dm_insert_exception(&s->complete, e);
1369 out:
1370 dm_remove_exception(&pe->e);
1371 snapshot_bios = bio_list_get(&pe->snapshot_bios);
1372 origin_bios = bio_list_get(&pe->origin_bios);
1373 free_pending_exception(pe);
1375 up_write(&s->lock);
1377 /* Submit any pending write bios */
1378 if (error)
1379 error_bios(snapshot_bios);
1380 else
1381 flush_bios(snapshot_bios);
1383 retry_origin_bios(s, origin_bios);
1386 static void commit_callback(void *context, int success)
1388 struct dm_snap_pending_exception *pe = context;
1390 pending_complete(pe, success);
1394 * Called when the copy I/O has finished. kcopyd actually runs
1395 * this code so don't block.
1397 static void copy_callback(int read_err, unsigned long write_err, void *context)
1399 struct dm_snap_pending_exception *pe = context;
1400 struct dm_snapshot *s = pe->snap;
1402 if (read_err || write_err)
1403 pending_complete(pe, 0);
1405 else
1406 /* Update the metadata if we are persistent */
1407 s->store->type->commit_exception(s->store, &pe->e,
1408 commit_callback, pe);
1412 * Dispatches the copy operation to kcopyd.
1414 static void start_copy(struct dm_snap_pending_exception *pe)
1416 struct dm_snapshot *s = pe->snap;
1417 struct dm_io_region src, dest;
1418 struct block_device *bdev = s->origin->bdev;
1419 sector_t dev_size;
1421 dev_size = get_dev_size(bdev);
1423 src.bdev = bdev;
1424 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
1425 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
1427 dest.bdev = s->cow->bdev;
1428 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
1429 dest.count = src.count;
1431 /* Hand over to kcopyd */
1432 dm_kcopyd_copy(s->kcopyd_client,
1433 &src, 1, &dest, 0, copy_callback, pe);
1436 static struct dm_snap_pending_exception *
1437 __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1439 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
1441 if (!e)
1442 return NULL;
1444 return container_of(e, struct dm_snap_pending_exception, e);
1448 * Looks to see if this snapshot already has a pending exception
1449 * for this chunk, otherwise it allocates a new one and inserts
1450 * it into the pending table.
1452 * NOTE: a write lock must be held on snap->lock before calling
1453 * this.
1455 static struct dm_snap_pending_exception *
1456 __find_pending_exception(struct dm_snapshot *s,
1457 struct dm_snap_pending_exception *pe, chunk_t chunk)
1459 struct dm_snap_pending_exception *pe2;
1461 pe2 = __lookup_pending_exception(s, chunk);
1462 if (pe2) {
1463 free_pending_exception(pe);
1464 return pe2;
1467 pe->e.old_chunk = chunk;
1468 bio_list_init(&pe->origin_bios);
1469 bio_list_init(&pe->snapshot_bios);
1470 pe->started = 0;
1472 if (s->store->type->prepare_exception(s->store, &pe->e)) {
1473 free_pending_exception(pe);
1474 return NULL;
1477 dm_insert_exception(&s->pending, &pe->e);
1479 return pe;
1482 static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
1483 struct bio *bio, chunk_t chunk)
1485 bio->bi_bdev = s->cow->bdev;
1486 bio->bi_sector = chunk_to_sector(s->store,
1487 dm_chunk_number(e->new_chunk) +
1488 (chunk - e->old_chunk)) +
1489 (bio->bi_sector &
1490 s->store->chunk_mask);
1493 static int snapshot_map(struct dm_target *ti, struct bio *bio,
1494 union map_info *map_context)
1496 struct dm_exception *e;
1497 struct dm_snapshot *s = ti->private;
1498 int r = DM_MAPIO_REMAPPED;
1499 chunk_t chunk;
1500 struct dm_snap_pending_exception *pe = NULL;
1502 if (unlikely(bio_empty_barrier(bio))) {
1503 bio->bi_bdev = s->cow->bdev;
1504 return DM_MAPIO_REMAPPED;
1507 chunk = sector_to_chunk(s->store, bio->bi_sector);
1509 /* Full snapshots are not usable */
1510 /* To get here the table must be live so s->active is always set. */
1511 if (!s->valid)
1512 return -EIO;
1514 /* FIXME: should only take write lock if we need
1515 * to copy an exception */
1516 down_write(&s->lock);
1518 if (!s->valid) {
1519 r = -EIO;
1520 goto out_unlock;
1523 /* If the block is already remapped - use that, else remap it */
1524 e = dm_lookup_exception(&s->complete, chunk);
1525 if (e) {
1526 remap_exception(s, e, bio, chunk);
1527 goto out_unlock;
1531 * Write to snapshot - higher level takes care of RW/RO
1532 * flags so we should only get this if we are
1533 * writeable.
1535 if (bio_rw(bio) == WRITE) {
1536 pe = __lookup_pending_exception(s, chunk);
1537 if (!pe) {
1538 up_write(&s->lock);
1539 pe = alloc_pending_exception(s);
1540 down_write(&s->lock);
1542 if (!s->valid) {
1543 free_pending_exception(pe);
1544 r = -EIO;
1545 goto out_unlock;
1548 e = dm_lookup_exception(&s->complete, chunk);
1549 if (e) {
1550 free_pending_exception(pe);
1551 remap_exception(s, e, bio, chunk);
1552 goto out_unlock;
1555 pe = __find_pending_exception(s, pe, chunk);
1556 if (!pe) {
1557 __invalidate_snapshot(s, -ENOMEM);
1558 r = -EIO;
1559 goto out_unlock;
1563 remap_exception(s, &pe->e, bio, chunk);
1564 bio_list_add(&pe->snapshot_bios, bio);
1566 r = DM_MAPIO_SUBMITTED;
1568 if (!pe->started) {
1569 /* this is protected by snap->lock */
1570 pe->started = 1;
1571 up_write(&s->lock);
1572 start_copy(pe);
1573 goto out;
1575 } else {
1576 bio->bi_bdev = s->origin->bdev;
1577 map_context->ptr = track_chunk(s, chunk);
1580 out_unlock:
1581 up_write(&s->lock);
1582 out:
1583 return r;
1587 * A snapshot-merge target behaves like a combination of a snapshot
1588 * target and a snapshot-origin target. It only generates new
1589 * exceptions in other snapshots and not in the one that is being
1590 * merged.
1592 * For each chunk, if there is an existing exception, it is used to
1593 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1594 * which in turn might generate exceptions in other snapshots.
1595 * If merging is currently taking place on the chunk in question, the
1596 * I/O is deferred by adding it to s->bios_queued_during_merge.
1598 static int snapshot_merge_map(struct dm_target *ti, struct bio *bio,
1599 union map_info *map_context)
1601 struct dm_exception *e;
1602 struct dm_snapshot *s = ti->private;
1603 int r = DM_MAPIO_REMAPPED;
1604 chunk_t chunk;
1606 if (unlikely(bio_empty_barrier(bio))) {
1607 if (!map_context->flush_request)
1608 bio->bi_bdev = s->origin->bdev;
1609 else
1610 bio->bi_bdev = s->cow->bdev;
1611 map_context->ptr = NULL;
1612 return DM_MAPIO_REMAPPED;
1615 chunk = sector_to_chunk(s->store, bio->bi_sector);
1617 down_write(&s->lock);
1619 /* Full snapshots are not usable */
1620 if (!s->valid) {
1621 r = -EIO;
1622 goto out_unlock;
1625 /* If the block is already remapped - use that */
1626 e = dm_lookup_exception(&s->complete, chunk);
1627 if (e) {
1628 /* Queue writes overlapping with chunks being merged */
1629 if (bio_rw(bio) == WRITE &&
1630 chunk >= s->first_merging_chunk &&
1631 chunk < (s->first_merging_chunk +
1632 s->num_merging_chunks)) {
1633 bio->bi_bdev = s->origin->bdev;
1634 bio_list_add(&s->bios_queued_during_merge, bio);
1635 r = DM_MAPIO_SUBMITTED;
1636 goto out_unlock;
1638 remap_exception(s, e, bio, chunk);
1639 goto out_unlock;
1642 bio->bi_bdev = s->origin->bdev;
1644 if (bio_rw(bio) == WRITE) {
1645 up_write(&s->lock);
1646 return do_origin(s->origin, bio);
1649 out_unlock:
1650 up_write(&s->lock);
1652 return r;
1655 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1656 int error, union map_info *map_context)
1658 struct dm_snapshot *s = ti->private;
1659 struct dm_snap_tracked_chunk *c = map_context->ptr;
1661 if (c)
1662 stop_tracking_chunk(s, c);
1664 return 0;
1667 static void snapshot_merge_presuspend(struct dm_target *ti)
1669 struct dm_snapshot *s = ti->private;
1671 stop_merge(s);
1674 static void snapshot_postsuspend(struct dm_target *ti)
1676 struct dm_snapshot *s = ti->private;
1678 down_write(&s->lock);
1679 s->suspended = 1;
1680 up_write(&s->lock);
1683 static int snapshot_preresume(struct dm_target *ti)
1685 int r = 0;
1686 struct dm_snapshot *s = ti->private;
1687 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1689 down_read(&_origins_lock);
1690 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1691 if (snap_src && snap_dest) {
1692 down_read(&snap_src->lock);
1693 if (s == snap_src) {
1694 DMERR("Unable to resume snapshot source until "
1695 "handover completes.");
1696 r = -EINVAL;
1697 } else if (!snap_src->suspended) {
1698 DMERR("Unable to perform snapshot handover until "
1699 "source is suspended.");
1700 r = -EINVAL;
1702 up_read(&snap_src->lock);
1704 up_read(&_origins_lock);
1706 return r;
1709 static void snapshot_resume(struct dm_target *ti)
1711 struct dm_snapshot *s = ti->private;
1712 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1714 down_read(&_origins_lock);
1715 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1716 if (snap_src && snap_dest) {
1717 down_write(&snap_src->lock);
1718 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1719 __handover_exceptions(snap_src, snap_dest);
1720 up_write(&snap_dest->lock);
1721 up_write(&snap_src->lock);
1723 up_read(&_origins_lock);
1725 /* Now we have correct chunk size, reregister */
1726 reregister_snapshot(s);
1728 down_write(&s->lock);
1729 s->active = 1;
1730 s->suspended = 0;
1731 up_write(&s->lock);
1734 static sector_t get_origin_minimum_chunksize(struct block_device *bdev)
1736 sector_t min_chunksize;
1738 down_read(&_origins_lock);
1739 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1740 up_read(&_origins_lock);
1742 return min_chunksize;
1745 static void snapshot_merge_resume(struct dm_target *ti)
1747 struct dm_snapshot *s = ti->private;
1750 * Handover exceptions from existing snapshot.
1752 snapshot_resume(ti);
1755 * snapshot-merge acts as an origin, so set ti->split_io
1757 ti->split_io = get_origin_minimum_chunksize(s->origin->bdev);
1759 start_merge(s);
1762 static int snapshot_status(struct dm_target *ti, status_type_t type,
1763 char *result, unsigned int maxlen)
1765 unsigned sz = 0;
1766 struct dm_snapshot *snap = ti->private;
1768 switch (type) {
1769 case STATUSTYPE_INFO:
1771 down_write(&snap->lock);
1773 if (!snap->valid)
1774 DMEMIT("Invalid");
1775 else {
1776 if (snap->store->type->usage) {
1777 sector_t total_sectors, sectors_allocated,
1778 metadata_sectors;
1779 snap->store->type->usage(snap->store,
1780 &total_sectors,
1781 &sectors_allocated,
1782 &metadata_sectors);
1783 DMEMIT("%llu/%llu %llu",
1784 (unsigned long long)sectors_allocated,
1785 (unsigned long long)total_sectors,
1786 (unsigned long long)metadata_sectors);
1788 else
1789 DMEMIT("Unknown");
1792 up_write(&snap->lock);
1794 break;
1796 case STATUSTYPE_TABLE:
1798 * kdevname returns a static pointer so we need
1799 * to make private copies if the output is to
1800 * make sense.
1802 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
1803 snap->store->type->status(snap->store, type, result + sz,
1804 maxlen - sz);
1805 break;
1808 return 0;
1811 static int snapshot_iterate_devices(struct dm_target *ti,
1812 iterate_devices_callout_fn fn, void *data)
1814 struct dm_snapshot *snap = ti->private;
1816 return fn(ti, snap->origin, 0, ti->len, data);
1820 /*-----------------------------------------------------------------
1821 * Origin methods
1822 *---------------------------------------------------------------*/
1825 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
1826 * supplied bio was ignored. The caller may submit it immediately.
1827 * (No remapping actually occurs as the origin is always a direct linear
1828 * map.)
1830 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
1831 * and any supplied bio is added to a list to be submitted once all
1832 * the necessary exceptions exist.
1834 static int __origin_write(struct list_head *snapshots, sector_t sector,
1835 struct bio *bio)
1837 int r = DM_MAPIO_REMAPPED;
1838 struct dm_snapshot *snap;
1839 struct dm_exception *e;
1840 struct dm_snap_pending_exception *pe;
1841 struct dm_snap_pending_exception *pe_to_start_now = NULL;
1842 struct dm_snap_pending_exception *pe_to_start_last = NULL;
1843 chunk_t chunk;
1845 /* Do all the snapshots on this origin */
1846 list_for_each_entry (snap, snapshots, list) {
1848 * Don't make new exceptions in a merging snapshot
1849 * because it has effectively been deleted
1851 if (dm_target_is_snapshot_merge(snap->ti))
1852 continue;
1854 down_write(&snap->lock);
1856 /* Only deal with valid and active snapshots */
1857 if (!snap->valid || !snap->active)
1858 goto next_snapshot;
1860 /* Nothing to do if writing beyond end of snapshot */
1861 if (sector >= dm_table_get_size(snap->ti->table))
1862 goto next_snapshot;
1865 * Remember, different snapshots can have
1866 * different chunk sizes.
1868 chunk = sector_to_chunk(snap->store, sector);
1871 * Check exception table to see if block
1872 * is already remapped in this snapshot
1873 * and trigger an exception if not.
1875 e = dm_lookup_exception(&snap->complete, chunk);
1876 if (e)
1877 goto next_snapshot;
1879 pe = __lookup_pending_exception(snap, chunk);
1880 if (!pe) {
1881 up_write(&snap->lock);
1882 pe = alloc_pending_exception(snap);
1883 down_write(&snap->lock);
1885 if (!snap->valid) {
1886 free_pending_exception(pe);
1887 goto next_snapshot;
1890 e = dm_lookup_exception(&snap->complete, chunk);
1891 if (e) {
1892 free_pending_exception(pe);
1893 goto next_snapshot;
1896 pe = __find_pending_exception(snap, pe, chunk);
1897 if (!pe) {
1898 __invalidate_snapshot(snap, -ENOMEM);
1899 goto next_snapshot;
1903 r = DM_MAPIO_SUBMITTED;
1906 * If an origin bio was supplied, queue it to wait for the
1907 * completion of this exception, and start this one last,
1908 * at the end of the function.
1910 if (bio) {
1911 bio_list_add(&pe->origin_bios, bio);
1912 bio = NULL;
1914 if (!pe->started) {
1915 pe->started = 1;
1916 pe_to_start_last = pe;
1920 if (!pe->started) {
1921 pe->started = 1;
1922 pe_to_start_now = pe;
1925 next_snapshot:
1926 up_write(&snap->lock);
1928 if (pe_to_start_now) {
1929 start_copy(pe_to_start_now);
1930 pe_to_start_now = NULL;
1935 * Submit the exception against which the bio is queued last,
1936 * to give the other exceptions a head start.
1938 if (pe_to_start_last)
1939 start_copy(pe_to_start_last);
1941 return r;
1945 * Called on a write from the origin driver.
1947 static int do_origin(struct dm_dev *origin, struct bio *bio)
1949 struct origin *o;
1950 int r = DM_MAPIO_REMAPPED;
1952 down_read(&_origins_lock);
1953 o = __lookup_origin(origin->bdev);
1954 if (o)
1955 r = __origin_write(&o->snapshots, bio->bi_sector, bio);
1956 up_read(&_origins_lock);
1958 return r;
1962 * Origin: maps a linear range of a device, with hooks for snapshotting.
1966 * Construct an origin mapping: <dev_path>
1967 * The context for an origin is merely a 'struct dm_dev *'
1968 * pointing to the real device.
1970 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1972 int r;
1973 struct dm_dev *dev;
1975 if (argc != 1) {
1976 ti->error = "origin: incorrect number of arguments";
1977 return -EINVAL;
1980 r = dm_get_device(ti, argv[0], 0, ti->len,
1981 dm_table_get_mode(ti->table), &dev);
1982 if (r) {
1983 ti->error = "Cannot get target device";
1984 return r;
1987 ti->private = dev;
1988 ti->num_flush_requests = 1;
1990 return 0;
1993 static void origin_dtr(struct dm_target *ti)
1995 struct dm_dev *dev = ti->private;
1996 dm_put_device(ti, dev);
1999 static int origin_map(struct dm_target *ti, struct bio *bio,
2000 union map_info *map_context)
2002 struct dm_dev *dev = ti->private;
2003 bio->bi_bdev = dev->bdev;
2005 if (unlikely(bio_empty_barrier(bio)))
2006 return DM_MAPIO_REMAPPED;
2008 /* Only tell snapshots if this is a write */
2009 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
2013 * Set the target "split_io" field to the minimum of all the snapshots'
2014 * chunk sizes.
2016 static void origin_resume(struct dm_target *ti)
2018 struct dm_dev *dev = ti->private;
2020 ti->split_io = get_origin_minimum_chunksize(dev->bdev);
2023 static int origin_status(struct dm_target *ti, status_type_t type, char *result,
2024 unsigned int maxlen)
2026 struct dm_dev *dev = ti->private;
2028 switch (type) {
2029 case STATUSTYPE_INFO:
2030 result[0] = '\0';
2031 break;
2033 case STATUSTYPE_TABLE:
2034 snprintf(result, maxlen, "%s", dev->name);
2035 break;
2038 return 0;
2041 static int origin_iterate_devices(struct dm_target *ti,
2042 iterate_devices_callout_fn fn, void *data)
2044 struct dm_dev *dev = ti->private;
2046 return fn(ti, dev, 0, ti->len, data);
2049 static struct target_type origin_target = {
2050 .name = "snapshot-origin",
2051 .version = {1, 7, 0},
2052 .module = THIS_MODULE,
2053 .ctr = origin_ctr,
2054 .dtr = origin_dtr,
2055 .map = origin_map,
2056 .resume = origin_resume,
2057 .status = origin_status,
2058 .iterate_devices = origin_iterate_devices,
2061 static struct target_type snapshot_target = {
2062 .name = "snapshot",
2063 .version = {1, 9, 0},
2064 .module = THIS_MODULE,
2065 .ctr = snapshot_ctr,
2066 .dtr = snapshot_dtr,
2067 .map = snapshot_map,
2068 .end_io = snapshot_end_io,
2069 .postsuspend = snapshot_postsuspend,
2070 .preresume = snapshot_preresume,
2071 .resume = snapshot_resume,
2072 .status = snapshot_status,
2073 .iterate_devices = snapshot_iterate_devices,
2076 static struct target_type merge_target = {
2077 .name = dm_snapshot_merge_target_name,
2078 .version = {1, 0, 0},
2079 .module = THIS_MODULE,
2080 .ctr = snapshot_ctr,
2081 .dtr = snapshot_dtr,
2082 .map = snapshot_merge_map,
2083 .end_io = snapshot_end_io,
2084 .presuspend = snapshot_merge_presuspend,
2085 .postsuspend = snapshot_postsuspend,
2086 .preresume = snapshot_preresume,
2087 .resume = snapshot_merge_resume,
2088 .status = snapshot_status,
2089 .iterate_devices = snapshot_iterate_devices,
2092 static int __init dm_snapshot_init(void)
2094 int r;
2096 r = dm_exception_store_init();
2097 if (r) {
2098 DMERR("Failed to initialize exception stores");
2099 return r;
2102 r = dm_register_target(&snapshot_target);
2103 if (r < 0) {
2104 DMERR("snapshot target register failed %d", r);
2105 goto bad_register_snapshot_target;
2108 r = dm_register_target(&origin_target);
2109 if (r < 0) {
2110 DMERR("Origin target register failed %d", r);
2111 goto bad_register_origin_target;
2114 r = dm_register_target(&merge_target);
2115 if (r < 0) {
2116 DMERR("Merge target register failed %d", r);
2117 goto bad_register_merge_target;
2120 r = init_origin_hash();
2121 if (r) {
2122 DMERR("init_origin_hash failed.");
2123 goto bad_origin_hash;
2126 exception_cache = KMEM_CACHE(dm_exception, 0);
2127 if (!exception_cache) {
2128 DMERR("Couldn't create exception cache.");
2129 r = -ENOMEM;
2130 goto bad_exception_cache;
2133 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
2134 if (!pending_cache) {
2135 DMERR("Couldn't create pending cache.");
2136 r = -ENOMEM;
2137 goto bad_pending_cache;
2140 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
2141 if (!tracked_chunk_cache) {
2142 DMERR("Couldn't create cache to track chunks in use.");
2143 r = -ENOMEM;
2144 goto bad_tracked_chunk_cache;
2147 ksnapd = create_singlethread_workqueue("ksnapd");
2148 if (!ksnapd) {
2149 DMERR("Failed to create ksnapd workqueue.");
2150 r = -ENOMEM;
2151 goto bad_pending_pool;
2154 return 0;
2156 bad_pending_pool:
2157 kmem_cache_destroy(tracked_chunk_cache);
2158 bad_tracked_chunk_cache:
2159 kmem_cache_destroy(pending_cache);
2160 bad_pending_cache:
2161 kmem_cache_destroy(exception_cache);
2162 bad_exception_cache:
2163 exit_origin_hash();
2164 bad_origin_hash:
2165 dm_unregister_target(&merge_target);
2166 bad_register_merge_target:
2167 dm_unregister_target(&origin_target);
2168 bad_register_origin_target:
2169 dm_unregister_target(&snapshot_target);
2170 bad_register_snapshot_target:
2171 dm_exception_store_exit();
2173 return r;
2176 static void __exit dm_snapshot_exit(void)
2178 destroy_workqueue(ksnapd);
2180 dm_unregister_target(&snapshot_target);
2181 dm_unregister_target(&origin_target);
2182 dm_unregister_target(&merge_target);
2184 exit_origin_hash();
2185 kmem_cache_destroy(pending_cache);
2186 kmem_cache_destroy(exception_cache);
2187 kmem_cache_destroy(tracked_chunk_cache);
2189 dm_exception_store_exit();
2192 /* Module hooks */
2193 module_init(dm_snapshot_init);
2194 module_exit(dm_snapshot_exit);
2196 MODULE_DESCRIPTION(DM_NAME " snapshot target");
2197 MODULE_AUTHOR("Joe Thornber");
2198 MODULE_LICENSE("GPL");