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/workqueue.h>
24 #include "dm-exception-store.h"
26 #define DM_MSG_PREFIX "snapshots"
29 * The percentage increment we will wake up users at
31 #define WAKE_UP_PERCENT 5
34 * kcopyd priority of snapshot operations
36 #define SNAPSHOT_COPY_PRIORITY 2
39 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
41 #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
44 * The size of the mempool used to track chunks in use.
48 #define DM_TRACKED_CHUNK_HASH_SIZE 16
49 #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
50 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
52 struct dm_exception_table {
55 struct list_head *table;
59 struct rw_semaphore lock;
61 struct dm_dev *origin;
66 /* List of snapshots per Origin */
67 struct list_head list;
69 /* You can't use a snapshot if this is 0 (e.g. if full) */
72 /* Origin writes don't trigger exceptions until this is set */
75 mempool_t *pending_pool;
77 atomic_t pending_exceptions_count;
79 struct dm_exception_table pending;
80 struct dm_exception_table complete;
83 * pe_lock protects all pending_exception operations and access
84 * as well as the snapshot_bios list.
88 /* The on disk metadata handler */
89 struct dm_exception_store *store;
91 struct dm_kcopyd_client *kcopyd_client;
93 /* Queue of snapshot writes for ksnapd to flush */
94 struct bio_list queued_bios;
95 struct work_struct queued_bios_work;
97 /* Chunks with outstanding reads */
98 mempool_t *tracked_chunk_pool;
99 spinlock_t tracked_chunk_lock;
100 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
103 struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
107 EXPORT_SYMBOL(dm_snap_cow);
109 static struct workqueue_struct *ksnapd;
110 static void flush_queued_bios(struct work_struct *work);
112 static sector_t chunk_to_sector(struct dm_exception_store *store,
115 return chunk << store->chunk_shift;
118 static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
121 * There is only ever one instance of a particular block
122 * device so we can compare pointers safely.
127 struct dm_snap_pending_exception {
128 struct dm_exception e;
131 * Origin buffers waiting for this to complete are held
134 struct bio_list origin_bios;
135 struct bio_list snapshot_bios;
138 * Short-term queue of pending exceptions prior to submission.
140 struct list_head list;
143 * The primary pending_exception is the one that holds
144 * the ref_count and the list of origin_bios for a
145 * group of pending_exceptions. It is always last to get freed.
146 * These fields get set up when writing to the origin.
148 struct dm_snap_pending_exception *primary_pe;
151 * Number of pending_exceptions processing this chunk.
152 * When this drops to zero we must complete the origin bios.
153 * If incrementing or decrementing this, hold pe->snap->lock for
154 * the sibling concerned and not pe->primary_pe->snap->lock unless
159 /* Pointer back to snapshot context */
160 struct dm_snapshot *snap;
163 * 1 indicates the exception has already been sent to
170 * Hash table mapping origin volumes to lists of snapshots and
171 * a lock to protect it
173 static struct kmem_cache *exception_cache;
174 static struct kmem_cache *pending_cache;
176 struct dm_snap_tracked_chunk {
177 struct hlist_node node;
181 static struct kmem_cache *tracked_chunk_cache;
183 static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
186 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
192 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
193 hlist_add_head(&c->node,
194 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
195 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
200 static void stop_tracking_chunk(struct dm_snapshot *s,
201 struct dm_snap_tracked_chunk *c)
205 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
207 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
209 mempool_free(c, s->tracked_chunk_pool);
212 static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
214 struct dm_snap_tracked_chunk *c;
215 struct hlist_node *hn;
218 spin_lock_irq(&s->tracked_chunk_lock);
220 hlist_for_each_entry(c, hn,
221 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
222 if (c->chunk == chunk) {
228 spin_unlock_irq(&s->tracked_chunk_lock);
234 * One of these per registered origin, held in the snapshot_origins hash
237 /* The origin device */
238 struct block_device *bdev;
240 struct list_head hash_list;
242 /* List of snapshots for this origin */
243 struct list_head snapshots;
247 * Size of the hash table for origin volumes. If we make this
248 * the size of the minors list then it should be nearly perfect
250 #define ORIGIN_HASH_SIZE 256
251 #define ORIGIN_MASK 0xFF
252 static struct list_head *_origins;
253 static struct rw_semaphore _origins_lock;
255 static int init_origin_hash(void)
259 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
262 DMERR("unable to allocate memory");
266 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
267 INIT_LIST_HEAD(_origins + i);
268 init_rwsem(&_origins_lock);
273 static void exit_origin_hash(void)
278 static unsigned origin_hash(struct block_device *bdev)
280 return bdev->bd_dev & ORIGIN_MASK;
283 static struct origin *__lookup_origin(struct block_device *origin)
285 struct list_head *ol;
288 ol = &_origins[origin_hash(origin)];
289 list_for_each_entry (o, ol, hash_list)
290 if (bdev_equal(o->bdev, origin))
296 static void __insert_origin(struct origin *o)
298 struct list_head *sl = &_origins[origin_hash(o->bdev)];
299 list_add_tail(&o->hash_list, sl);
303 * Make a note of the snapshot and its origin so we can look it
304 * up when the origin has a write on it.
306 static int register_snapshot(struct dm_snapshot *snap)
308 struct dm_snapshot *l;
309 struct origin *o, *new_o;
310 struct block_device *bdev = snap->origin->bdev;
312 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
316 down_write(&_origins_lock);
317 o = __lookup_origin(bdev);
325 /* Initialise the struct */
326 INIT_LIST_HEAD(&o->snapshots);
332 /* Sort the list according to chunk size, largest-first smallest-last */
333 list_for_each_entry(l, &o->snapshots, list)
334 if (l->store->chunk_size < snap->store->chunk_size)
336 list_add_tail(&snap->list, &l->list);
338 up_write(&_origins_lock);
342 static void unregister_snapshot(struct dm_snapshot *s)
346 down_write(&_origins_lock);
347 o = __lookup_origin(s->origin->bdev);
350 if (list_empty(&o->snapshots)) {
351 list_del(&o->hash_list);
355 up_write(&_origins_lock);
359 * Implementation of the exception hash tables.
360 * The lowest hash_shift bits of the chunk number are ignored, allowing
361 * some consecutive chunks to be grouped together.
363 static int dm_exception_table_init(struct dm_exception_table *et,
364 uint32_t size, unsigned hash_shift)
368 et->hash_shift = hash_shift;
369 et->hash_mask = size - 1;
370 et->table = dm_vcalloc(size, sizeof(struct list_head));
374 for (i = 0; i < size; i++)
375 INIT_LIST_HEAD(et->table + i);
380 static void dm_exception_table_exit(struct dm_exception_table *et,
381 struct kmem_cache *mem)
383 struct list_head *slot;
384 struct dm_exception *ex, *next;
387 size = et->hash_mask + 1;
388 for (i = 0; i < size; i++) {
389 slot = et->table + i;
391 list_for_each_entry_safe (ex, next, slot, hash_list)
392 kmem_cache_free(mem, ex);
398 static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
400 return (chunk >> et->hash_shift) & et->hash_mask;
403 static void dm_remove_exception(struct dm_exception *e)
405 list_del(&e->hash_list);
409 * Return the exception data for a sector, or NULL if not
412 static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
415 struct list_head *slot;
416 struct dm_exception *e;
418 slot = &et->table[exception_hash(et, chunk)];
419 list_for_each_entry (e, slot, hash_list)
420 if (chunk >= e->old_chunk &&
421 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
427 static struct dm_exception *alloc_completed_exception(void)
429 struct dm_exception *e;
431 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
433 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
438 static void free_completed_exception(struct dm_exception *e)
440 kmem_cache_free(exception_cache, e);
443 static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
445 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
448 atomic_inc(&s->pending_exceptions_count);
454 static void free_pending_exception(struct dm_snap_pending_exception *pe)
456 struct dm_snapshot *s = pe->snap;
458 mempool_free(pe, s->pending_pool);
459 smp_mb__before_atomic_dec();
460 atomic_dec(&s->pending_exceptions_count);
463 static void dm_insert_exception(struct dm_exception_table *eh,
464 struct dm_exception *new_e)
467 struct dm_exception *e = NULL;
469 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
471 /* Add immediately if this table doesn't support consecutive chunks */
475 /* List is ordered by old_chunk */
476 list_for_each_entry_reverse(e, l, hash_list) {
477 /* Insert after an existing chunk? */
478 if (new_e->old_chunk == (e->old_chunk +
479 dm_consecutive_chunk_count(e) + 1) &&
480 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
481 dm_consecutive_chunk_count(e) + 1)) {
482 dm_consecutive_chunk_count_inc(e);
483 free_completed_exception(new_e);
487 /* Insert before an existing chunk? */
488 if (new_e->old_chunk == (e->old_chunk - 1) &&
489 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
490 dm_consecutive_chunk_count_inc(e);
493 free_completed_exception(new_e);
497 if (new_e->old_chunk > e->old_chunk)
502 list_add(&new_e->hash_list, e ? &e->hash_list : l);
506 * Callback used by the exception stores to load exceptions when
509 static int dm_add_exception(void *context, chunk_t old, chunk_t new)
511 struct dm_snapshot *s = context;
512 struct dm_exception *e;
514 e = alloc_completed_exception();
520 /* Consecutive_count is implicitly initialised to zero */
523 dm_insert_exception(&s->complete, e);
528 #define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
531 * Return a minimum chunk size of all snapshots that have the specified origin.
532 * Return zero if the origin has no snapshots.
534 static sector_t __minimum_chunk_size(struct origin *o)
536 struct dm_snapshot *snap;
537 unsigned chunk_size = 0;
540 list_for_each_entry(snap, &o->snapshots, list)
541 chunk_size = min_not_zero(chunk_size,
542 snap->store->chunk_size);
550 static int calc_max_buckets(void)
552 /* use a fixed size of 2MB */
553 unsigned long mem = 2 * 1024 * 1024;
554 mem /= sizeof(struct list_head);
560 * Allocate room for a suitable hash table.
562 static int init_hash_tables(struct dm_snapshot *s)
564 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
567 * Calculate based on the size of the original volume or
570 cow_dev_size = get_dev_size(s->cow->bdev);
571 origin_dev_size = get_dev_size(s->origin->bdev);
572 max_buckets = calc_max_buckets();
574 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
575 hash_size = min(hash_size, max_buckets);
579 hash_size = rounddown_pow_of_two(hash_size);
580 if (dm_exception_table_init(&s->complete, hash_size,
581 DM_CHUNK_CONSECUTIVE_BITS))
585 * Allocate hash table for in-flight exceptions
586 * Make this smaller than the real hash table
592 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
593 dm_exception_table_exit(&s->complete, exception_cache);
601 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
603 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
605 struct dm_snapshot *s;
608 char *origin_path, *cow_path;
612 ti->error = "requires exactly 4 arguments";
617 origin_path = argv[0];
621 s = kmalloc(sizeof(*s), GFP_KERNEL);
623 ti->error = "Cannot allocate snapshot context private "
633 r = dm_get_device(ti, cow_path, 0, 0,
634 FMODE_READ | FMODE_WRITE, &s->cow);
636 ti->error = "Cannot get COW device";
640 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
642 ti->error = "Couldn't create exception store";
650 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
652 ti->error = "Cannot get origin device";
659 atomic_set(&s->pending_exceptions_count, 0);
660 init_rwsem(&s->lock);
661 spin_lock_init(&s->pe_lock);
663 /* Allocate hash table for COW data */
664 if (init_hash_tables(s)) {
665 ti->error = "Unable to allocate hash table space";
667 goto bad_hash_tables;
670 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
672 ti->error = "Could not create kcopyd client";
676 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
677 if (!s->pending_pool) {
678 ti->error = "Could not allocate mempool for pending exceptions";
679 goto bad_pending_pool;
682 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
683 tracked_chunk_cache);
684 if (!s->tracked_chunk_pool) {
685 ti->error = "Could not allocate tracked_chunk mempool for "
687 goto bad_tracked_chunk_pool;
690 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
691 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
693 spin_lock_init(&s->tracked_chunk_lock);
695 /* Metadata must only be loaded into one table at once */
696 r = s->store->type->read_metadata(s->store, dm_add_exception,
699 ti->error = "Failed to read snapshot metadata";
700 goto bad_load_and_register;
703 DMWARN("Snapshot is marked invalid.");
706 bio_list_init(&s->queued_bios);
707 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
709 if (!s->store->chunk_size) {
710 ti->error = "Chunk size not set";
711 goto bad_load_and_register;
714 /* Add snapshot to the list of snapshots for this origin */
715 /* Exceptions aren't triggered till snapshot_resume() is called */
716 if (register_snapshot(s)) {
718 ti->error = "Cannot register snapshot origin";
719 goto bad_load_and_register;
723 ti->split_io = s->store->chunk_size;
724 ti->num_flush_requests = 1;
728 bad_load_and_register:
729 mempool_destroy(s->tracked_chunk_pool);
731 bad_tracked_chunk_pool:
732 mempool_destroy(s->pending_pool);
735 dm_kcopyd_client_destroy(s->kcopyd_client);
738 dm_exception_table_exit(&s->pending, pending_cache);
739 dm_exception_table_exit(&s->complete, exception_cache);
742 dm_put_device(ti, s->origin);
745 dm_exception_store_destroy(s->store);
748 dm_put_device(ti, s->cow);
757 static void __free_exceptions(struct dm_snapshot *s)
759 dm_kcopyd_client_destroy(s->kcopyd_client);
760 s->kcopyd_client = NULL;
762 dm_exception_table_exit(&s->pending, pending_cache);
763 dm_exception_table_exit(&s->complete, exception_cache);
766 static void snapshot_dtr(struct dm_target *ti)
768 #ifdef CONFIG_DM_DEBUG
771 struct dm_snapshot *s = ti->private;
773 flush_workqueue(ksnapd);
775 /* Prevent further origin writes from using this snapshot. */
776 /* After this returns there can be no new kcopyd jobs. */
777 unregister_snapshot(s);
779 while (atomic_read(&s->pending_exceptions_count))
782 * Ensure instructions in mempool_destroy aren't reordered
783 * before atomic_read.
787 #ifdef CONFIG_DM_DEBUG
788 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
789 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
792 mempool_destroy(s->tracked_chunk_pool);
794 __free_exceptions(s);
796 mempool_destroy(s->pending_pool);
798 dm_put_device(ti, s->origin);
800 dm_exception_store_destroy(s->store);
802 dm_put_device(ti, s->cow);
808 * Flush a list of buffers.
810 static void flush_bios(struct bio *bio)
817 generic_make_request(bio);
822 static void flush_queued_bios(struct work_struct *work)
824 struct dm_snapshot *s =
825 container_of(work, struct dm_snapshot, queued_bios_work);
826 struct bio *queued_bios;
829 spin_lock_irqsave(&s->pe_lock, flags);
830 queued_bios = bio_list_get(&s->queued_bios);
831 spin_unlock_irqrestore(&s->pe_lock, flags);
833 flush_bios(queued_bios);
837 * Error a list of buffers.
839 static void error_bios(struct bio *bio)
851 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
857 DMERR("Invalidating snapshot: Error reading/writing.");
858 else if (err == -ENOMEM)
859 DMERR("Invalidating snapshot: Unable to allocate exception.");
861 if (s->store->type->drop_snapshot)
862 s->store->type->drop_snapshot(s->store);
866 dm_table_event(s->ti->table);
869 static void get_pending_exception(struct dm_snap_pending_exception *pe)
871 atomic_inc(&pe->ref_count);
874 static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
876 struct dm_snap_pending_exception *primary_pe;
877 struct bio *origin_bios = NULL;
879 primary_pe = pe->primary_pe;
882 * If this pe is involved in a write to the origin and
883 * it is the last sibling to complete then release
884 * the bios for the original write to the origin.
887 atomic_dec_and_test(&primary_pe->ref_count)) {
888 origin_bios = bio_list_get(&primary_pe->origin_bios);
889 free_pending_exception(primary_pe);
893 * Free the pe if it's not linked to an origin write or if
894 * it's not itself a primary pe.
896 if (!primary_pe || primary_pe != pe)
897 free_pending_exception(pe);
902 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
904 struct dm_exception *e;
905 struct dm_snapshot *s = pe->snap;
906 struct bio *origin_bios = NULL;
907 struct bio *snapshot_bios = NULL;
911 /* Read/write error - snapshot is unusable */
912 down_write(&s->lock);
913 __invalidate_snapshot(s, -EIO);
918 e = alloc_completed_exception();
920 down_write(&s->lock);
921 __invalidate_snapshot(s, -ENOMEM);
927 down_write(&s->lock);
929 free_completed_exception(e);
935 * Check for conflicting reads. This is extremely improbable,
936 * so msleep(1) is sufficient and there is no need for a wait queue.
938 while (__chunk_is_tracked(s, pe->e.old_chunk))
942 * Add a proper exception, and remove the
943 * in-flight exception from the list.
945 dm_insert_exception(&s->complete, e);
948 dm_remove_exception(&pe->e);
949 snapshot_bios = bio_list_get(&pe->snapshot_bios);
950 origin_bios = put_pending_exception(pe);
954 /* Submit any pending write bios */
956 error_bios(snapshot_bios);
958 flush_bios(snapshot_bios);
960 flush_bios(origin_bios);
963 static void commit_callback(void *context, int success)
965 struct dm_snap_pending_exception *pe = context;
967 pending_complete(pe, success);
971 * Called when the copy I/O has finished. kcopyd actually runs
972 * this code so don't block.
974 static void copy_callback(int read_err, unsigned long write_err, void *context)
976 struct dm_snap_pending_exception *pe = context;
977 struct dm_snapshot *s = pe->snap;
979 if (read_err || write_err)
980 pending_complete(pe, 0);
983 /* Update the metadata if we are persistent */
984 s->store->type->commit_exception(s->store, &pe->e,
985 commit_callback, pe);
989 * Dispatches the copy operation to kcopyd.
991 static void start_copy(struct dm_snap_pending_exception *pe)
993 struct dm_snapshot *s = pe->snap;
994 struct dm_io_region src, dest;
995 struct block_device *bdev = s->origin->bdev;
998 dev_size = get_dev_size(bdev);
1001 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
1002 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
1004 dest.bdev = s->cow->bdev;
1005 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
1006 dest.count = src.count;
1008 /* Hand over to kcopyd */
1009 dm_kcopyd_copy(s->kcopyd_client,
1010 &src, 1, &dest, 0, copy_callback, pe);
1013 static struct dm_snap_pending_exception *
1014 __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1016 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
1021 return container_of(e, struct dm_snap_pending_exception, e);
1025 * Looks to see if this snapshot already has a pending exception
1026 * for this chunk, otherwise it allocates a new one and inserts
1027 * it into the pending table.
1029 * NOTE: a write lock must be held on snap->lock before calling
1032 static struct dm_snap_pending_exception *
1033 __find_pending_exception(struct dm_snapshot *s,
1034 struct dm_snap_pending_exception *pe, chunk_t chunk)
1036 struct dm_snap_pending_exception *pe2;
1038 pe2 = __lookup_pending_exception(s, chunk);
1040 free_pending_exception(pe);
1044 pe->e.old_chunk = chunk;
1045 bio_list_init(&pe->origin_bios);
1046 bio_list_init(&pe->snapshot_bios);
1047 pe->primary_pe = NULL;
1048 atomic_set(&pe->ref_count, 0);
1051 if (s->store->type->prepare_exception(s->store, &pe->e)) {
1052 free_pending_exception(pe);
1056 get_pending_exception(pe);
1057 dm_insert_exception(&s->pending, &pe->e);
1062 static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
1063 struct bio *bio, chunk_t chunk)
1065 bio->bi_bdev = s->cow->bdev;
1066 bio->bi_sector = chunk_to_sector(s->store,
1067 dm_chunk_number(e->new_chunk) +
1068 (chunk - e->old_chunk)) +
1070 s->store->chunk_mask);
1073 static int snapshot_map(struct dm_target *ti, struct bio *bio,
1074 union map_info *map_context)
1076 struct dm_exception *e;
1077 struct dm_snapshot *s = ti->private;
1078 int r = DM_MAPIO_REMAPPED;
1080 struct dm_snap_pending_exception *pe = NULL;
1082 if (unlikely(bio_empty_barrier(bio))) {
1083 bio->bi_bdev = s->cow->bdev;
1084 return DM_MAPIO_REMAPPED;
1087 chunk = sector_to_chunk(s->store, bio->bi_sector);
1089 /* Full snapshots are not usable */
1090 /* To get here the table must be live so s->active is always set. */
1094 /* FIXME: should only take write lock if we need
1095 * to copy an exception */
1096 down_write(&s->lock);
1103 /* If the block is already remapped - use that, else remap it */
1104 e = dm_lookup_exception(&s->complete, chunk);
1106 remap_exception(s, e, bio, chunk);
1111 * Write to snapshot - higher level takes care of RW/RO
1112 * flags so we should only get this if we are
1115 if (bio_rw(bio) == WRITE) {
1116 pe = __lookup_pending_exception(s, chunk);
1119 pe = alloc_pending_exception(s);
1120 down_write(&s->lock);
1123 free_pending_exception(pe);
1128 e = dm_lookup_exception(&s->complete, chunk);
1130 free_pending_exception(pe);
1131 remap_exception(s, e, bio, chunk);
1135 pe = __find_pending_exception(s, pe, chunk);
1137 __invalidate_snapshot(s, -ENOMEM);
1143 remap_exception(s, &pe->e, bio, chunk);
1144 bio_list_add(&pe->snapshot_bios, bio);
1146 r = DM_MAPIO_SUBMITTED;
1149 /* this is protected by snap->lock */
1156 bio->bi_bdev = s->origin->bdev;
1157 map_context->ptr = track_chunk(s, chunk);
1166 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1167 int error, union map_info *map_context)
1169 struct dm_snapshot *s = ti->private;
1170 struct dm_snap_tracked_chunk *c = map_context->ptr;
1173 stop_tracking_chunk(s, c);
1178 static void snapshot_resume(struct dm_target *ti)
1180 struct dm_snapshot *s = ti->private;
1182 down_write(&s->lock);
1187 static int snapshot_status(struct dm_target *ti, status_type_t type,
1188 char *result, unsigned int maxlen)
1191 struct dm_snapshot *snap = ti->private;
1194 case STATUSTYPE_INFO:
1196 down_write(&snap->lock);
1201 if (snap->store->type->usage) {
1202 sector_t total_sectors, sectors_allocated,
1204 snap->store->type->usage(snap->store,
1208 DMEMIT("%llu/%llu %llu",
1209 (unsigned long long)sectors_allocated,
1210 (unsigned long long)total_sectors,
1211 (unsigned long long)metadata_sectors);
1217 up_write(&snap->lock);
1221 case STATUSTYPE_TABLE:
1223 * kdevname returns a static pointer so we need
1224 * to make private copies if the output is to
1227 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
1228 snap->store->type->status(snap->store, type, result + sz,
1236 static int snapshot_iterate_devices(struct dm_target *ti,
1237 iterate_devices_callout_fn fn, void *data)
1239 struct dm_snapshot *snap = ti->private;
1241 return fn(ti, snap->origin, 0, ti->len, data);
1245 /*-----------------------------------------------------------------
1247 *---------------------------------------------------------------*/
1248 static int __origin_write(struct list_head *snapshots, struct bio *bio)
1250 int r = DM_MAPIO_REMAPPED, first = 0;
1251 struct dm_snapshot *snap;
1252 struct dm_exception *e;
1253 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
1255 LIST_HEAD(pe_queue);
1257 /* Do all the snapshots on this origin */
1258 list_for_each_entry (snap, snapshots, list) {
1260 down_write(&snap->lock);
1262 /* Only deal with valid and active snapshots */
1263 if (!snap->valid || !snap->active)
1266 /* Nothing to do if writing beyond end of snapshot */
1267 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
1271 * Remember, different snapshots can have
1272 * different chunk sizes.
1274 chunk = sector_to_chunk(snap->store, bio->bi_sector);
1277 * Check exception table to see if block
1278 * is already remapped in this snapshot
1279 * and trigger an exception if not.
1281 * ref_count is initialised to 1 so pending_complete()
1282 * won't destroy the primary_pe while we're inside this loop.
1284 e = dm_lookup_exception(&snap->complete, chunk);
1288 pe = __lookup_pending_exception(snap, chunk);
1290 up_write(&snap->lock);
1291 pe = alloc_pending_exception(snap);
1292 down_write(&snap->lock);
1295 free_pending_exception(pe);
1299 e = dm_lookup_exception(&snap->complete, chunk);
1301 free_pending_exception(pe);
1305 pe = __find_pending_exception(snap, pe, chunk);
1307 __invalidate_snapshot(snap, -ENOMEM);
1314 * Either every pe here has same
1315 * primary_pe or none has one yet.
1318 primary_pe = pe->primary_pe;
1324 bio_list_add(&primary_pe->origin_bios, bio);
1326 r = DM_MAPIO_SUBMITTED;
1329 if (!pe->primary_pe) {
1330 pe->primary_pe = primary_pe;
1331 get_pending_exception(primary_pe);
1336 list_add_tail(&pe->list, &pe_queue);
1340 up_write(&snap->lock);
1347 * If this is the first time we're processing this chunk and
1348 * ref_count is now 1 it means all the pending exceptions
1349 * got completed while we were in the loop above, so it falls to
1350 * us here to remove the primary_pe and submit any origin_bios.
1353 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
1354 flush_bios(bio_list_get(&primary_pe->origin_bios));
1355 free_pending_exception(primary_pe);
1356 /* If we got here, pe_queue is necessarily empty. */
1361 * Now that we have a complete pe list we can start the copying.
1363 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1370 * Called on a write from the origin driver.
1372 static int do_origin(struct dm_dev *origin, struct bio *bio)
1375 int r = DM_MAPIO_REMAPPED;
1377 down_read(&_origins_lock);
1378 o = __lookup_origin(origin->bdev);
1380 r = __origin_write(&o->snapshots, bio);
1381 up_read(&_origins_lock);
1387 * Origin: maps a linear range of a device, with hooks for snapshotting.
1391 * Construct an origin mapping: <dev_path>
1392 * The context for an origin is merely a 'struct dm_dev *'
1393 * pointing to the real device.
1395 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1401 ti->error = "origin: incorrect number of arguments";
1405 r = dm_get_device(ti, argv[0], 0, ti->len,
1406 dm_table_get_mode(ti->table), &dev);
1408 ti->error = "Cannot get target device";
1413 ti->num_flush_requests = 1;
1418 static void origin_dtr(struct dm_target *ti)
1420 struct dm_dev *dev = ti->private;
1421 dm_put_device(ti, dev);
1424 static int origin_map(struct dm_target *ti, struct bio *bio,
1425 union map_info *map_context)
1427 struct dm_dev *dev = ti->private;
1428 bio->bi_bdev = dev->bdev;
1430 if (unlikely(bio_empty_barrier(bio)))
1431 return DM_MAPIO_REMAPPED;
1433 /* Only tell snapshots if this is a write */
1434 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
1438 * Set the target "split_io" field to the minimum of all the snapshots'
1441 static void origin_resume(struct dm_target *ti)
1443 struct dm_dev *dev = ti->private;
1445 down_read(&_origins_lock);
1447 ti->split_io = __minimum_chunk_size(__lookup_origin(dev->bdev));
1449 up_read(&_origins_lock);
1452 static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1453 unsigned int maxlen)
1455 struct dm_dev *dev = ti->private;
1458 case STATUSTYPE_INFO:
1462 case STATUSTYPE_TABLE:
1463 snprintf(result, maxlen, "%s", dev->name);
1470 static int origin_iterate_devices(struct dm_target *ti,
1471 iterate_devices_callout_fn fn, void *data)
1473 struct dm_dev *dev = ti->private;
1475 return fn(ti, dev, 0, ti->len, data);
1478 static struct target_type origin_target = {
1479 .name = "snapshot-origin",
1480 .version = {1, 7, 0},
1481 .module = THIS_MODULE,
1485 .resume = origin_resume,
1486 .status = origin_status,
1487 .iterate_devices = origin_iterate_devices,
1490 static struct target_type snapshot_target = {
1492 .version = {1, 8, 0},
1493 .module = THIS_MODULE,
1494 .ctr = snapshot_ctr,
1495 .dtr = snapshot_dtr,
1496 .map = snapshot_map,
1497 .end_io = snapshot_end_io,
1498 .resume = snapshot_resume,
1499 .status = snapshot_status,
1500 .iterate_devices = snapshot_iterate_devices,
1503 static int __init dm_snapshot_init(void)
1507 r = dm_exception_store_init();
1509 DMERR("Failed to initialize exception stores");
1513 r = dm_register_target(&snapshot_target);
1515 DMERR("snapshot target register failed %d", r);
1516 goto bad_register_snapshot_target;
1519 r = dm_register_target(&origin_target);
1521 DMERR("Origin target register failed %d", r);
1525 r = init_origin_hash();
1527 DMERR("init_origin_hash failed.");
1531 exception_cache = KMEM_CACHE(dm_exception, 0);
1532 if (!exception_cache) {
1533 DMERR("Couldn't create exception cache.");
1538 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
1539 if (!pending_cache) {
1540 DMERR("Couldn't create pending cache.");
1545 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1546 if (!tracked_chunk_cache) {
1547 DMERR("Couldn't create cache to track chunks in use.");
1552 ksnapd = create_singlethread_workqueue("ksnapd");
1554 DMERR("Failed to create ksnapd workqueue.");
1556 goto bad_pending_pool;
1562 kmem_cache_destroy(tracked_chunk_cache);
1564 kmem_cache_destroy(pending_cache);
1566 kmem_cache_destroy(exception_cache);
1570 dm_unregister_target(&origin_target);
1572 dm_unregister_target(&snapshot_target);
1574 bad_register_snapshot_target:
1575 dm_exception_store_exit();
1579 static void __exit dm_snapshot_exit(void)
1581 destroy_workqueue(ksnapd);
1583 dm_unregister_target(&snapshot_target);
1584 dm_unregister_target(&origin_target);
1587 kmem_cache_destroy(pending_cache);
1588 kmem_cache_destroy(exception_cache);
1589 kmem_cache_destroy(tracked_chunk_cache);
1591 dm_exception_store_exit();
1595 module_init(dm_snapshot_init);
1596 module_exit(dm_snapshot_exit);
1598 MODULE_DESCRIPTION(DM_NAME " snapshot target");
1599 MODULE_AUTHOR("Joe Thornber");
1600 MODULE_LICENSE("GPL");