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/ctype.h>
11 #include <linux/device-mapper.h>
12 #include <linux/delay.h>
14 #include <linux/init.h>
15 #include <linux/kdev_t.h>
16 #include <linux/list.h>
17 #include <linux/mempool.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/log2.h>
22 #include <linux/dm-kcopyd.h>
23 #include <linux/workqueue.h>
25 #include "dm-exception-store.h"
26 #include "dm-bio-list.h"
28 #define DM_MSG_PREFIX "snapshots"
31 * The percentage increment we will wake up users at
33 #define WAKE_UP_PERCENT 5
36 * kcopyd priority of snapshot operations
38 #define SNAPSHOT_COPY_PRIORITY 2
41 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
43 #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
46 * The size of the mempool used to track chunks in use.
50 #define DM_TRACKED_CHUNK_HASH_SIZE 16
51 #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
52 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
54 struct exception_table {
57 struct list_head *table;
61 struct rw_semaphore lock;
63 struct dm_dev *origin;
65 /* List of snapshots per Origin */
66 struct list_head list;
68 /* You can't use a snapshot if this is 0 (e.g. if full) */
71 /* Origin writes don't trigger exceptions until this is set */
74 /* Used for display of table */
77 mempool_t *pending_pool;
79 atomic_t pending_exceptions_count;
81 struct exception_table pending;
82 struct exception_table complete;
85 * pe_lock protects all pending_exception operations and access
86 * as well as the snapshot_bios list.
90 /* The on disk metadata handler */
91 struct dm_exception_store *store;
93 struct dm_kcopyd_client *kcopyd_client;
95 /* Queue of snapshot writes for ksnapd to flush */
96 struct bio_list queued_bios;
97 struct work_struct queued_bios_work;
99 /* Chunks with outstanding reads */
100 mempool_t *tracked_chunk_pool;
101 spinlock_t tracked_chunk_lock;
102 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
105 static struct workqueue_struct *ksnapd;
106 static void flush_queued_bios(struct work_struct *work);
108 static sector_t chunk_to_sector(struct dm_exception_store *store,
111 return chunk << store->chunk_shift;
114 static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
117 * There is only ever one instance of a particular block
118 * device so we can compare pointers safely.
123 struct dm_snap_pending_exception {
124 struct dm_snap_exception e;
127 * Origin buffers waiting for this to complete are held
130 struct bio_list origin_bios;
131 struct bio_list snapshot_bios;
134 * Short-term queue of pending exceptions prior to submission.
136 struct list_head list;
139 * The primary pending_exception is the one that holds
140 * the ref_count and the list of origin_bios for a
141 * group of pending_exceptions. It is always last to get freed.
142 * These fields get set up when writing to the origin.
144 struct dm_snap_pending_exception *primary_pe;
147 * Number of pending_exceptions processing this chunk.
148 * When this drops to zero we must complete the origin bios.
149 * If incrementing or decrementing this, hold pe->snap->lock for
150 * the sibling concerned and not pe->primary_pe->snap->lock unless
155 /* Pointer back to snapshot context */
156 struct dm_snapshot *snap;
159 * 1 indicates the exception has already been sent to
166 * Hash table mapping origin volumes to lists of snapshots and
167 * a lock to protect it
169 static struct kmem_cache *exception_cache;
170 static struct kmem_cache *pending_cache;
172 struct dm_snap_tracked_chunk {
173 struct hlist_node node;
177 static struct kmem_cache *tracked_chunk_cache;
179 static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
182 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
188 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
189 hlist_add_head(&c->node,
190 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
191 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
196 static void stop_tracking_chunk(struct dm_snapshot *s,
197 struct dm_snap_tracked_chunk *c)
201 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
203 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
205 mempool_free(c, s->tracked_chunk_pool);
208 static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
210 struct dm_snap_tracked_chunk *c;
211 struct hlist_node *hn;
214 spin_lock_irq(&s->tracked_chunk_lock);
216 hlist_for_each_entry(c, hn,
217 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
218 if (c->chunk == chunk) {
224 spin_unlock_irq(&s->tracked_chunk_lock);
230 * One of these per registered origin, held in the snapshot_origins hash
233 /* The origin device */
234 struct block_device *bdev;
236 struct list_head hash_list;
238 /* List of snapshots for this origin */
239 struct list_head snapshots;
243 * Size of the hash table for origin volumes. If we make this
244 * the size of the minors list then it should be nearly perfect
246 #define ORIGIN_HASH_SIZE 256
247 #define ORIGIN_MASK 0xFF
248 static struct list_head *_origins;
249 static struct rw_semaphore _origins_lock;
251 static int init_origin_hash(void)
255 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
258 DMERR("unable to allocate memory");
262 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
263 INIT_LIST_HEAD(_origins + i);
264 init_rwsem(&_origins_lock);
269 static void exit_origin_hash(void)
274 static unsigned origin_hash(struct block_device *bdev)
276 return bdev->bd_dev & ORIGIN_MASK;
279 static struct origin *__lookup_origin(struct block_device *origin)
281 struct list_head *ol;
284 ol = &_origins[origin_hash(origin)];
285 list_for_each_entry (o, ol, hash_list)
286 if (bdev_equal(o->bdev, origin))
292 static void __insert_origin(struct origin *o)
294 struct list_head *sl = &_origins[origin_hash(o->bdev)];
295 list_add_tail(&o->hash_list, sl);
299 * Make a note of the snapshot and its origin so we can look it
300 * up when the origin has a write on it.
302 static int register_snapshot(struct dm_snapshot *snap)
304 struct origin *o, *new_o;
305 struct block_device *bdev = snap->origin->bdev;
307 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
311 down_write(&_origins_lock);
312 o = __lookup_origin(bdev);
320 /* Initialise the struct */
321 INIT_LIST_HEAD(&o->snapshots);
327 list_add_tail(&snap->list, &o->snapshots);
329 up_write(&_origins_lock);
333 static void unregister_snapshot(struct dm_snapshot *s)
337 down_write(&_origins_lock);
338 o = __lookup_origin(s->origin->bdev);
341 if (list_empty(&o->snapshots)) {
342 list_del(&o->hash_list);
346 up_write(&_origins_lock);
350 * Implementation of the exception hash tables.
351 * The lowest hash_shift bits of the chunk number are ignored, allowing
352 * some consecutive chunks to be grouped together.
354 static int init_exception_table(struct exception_table *et, uint32_t size,
359 et->hash_shift = hash_shift;
360 et->hash_mask = size - 1;
361 et->table = dm_vcalloc(size, sizeof(struct list_head));
365 for (i = 0; i < size; i++)
366 INIT_LIST_HEAD(et->table + i);
371 static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
373 struct list_head *slot;
374 struct dm_snap_exception *ex, *next;
377 size = et->hash_mask + 1;
378 for (i = 0; i < size; i++) {
379 slot = et->table + i;
381 list_for_each_entry_safe (ex, next, slot, hash_list)
382 kmem_cache_free(mem, ex);
388 static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
390 return (chunk >> et->hash_shift) & et->hash_mask;
393 static void insert_exception(struct exception_table *eh,
394 struct dm_snap_exception *e)
396 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
397 list_add(&e->hash_list, l);
400 static void remove_exception(struct dm_snap_exception *e)
402 list_del(&e->hash_list);
406 * Return the exception data for a sector, or NULL if not
409 static struct dm_snap_exception *lookup_exception(struct exception_table *et,
412 struct list_head *slot;
413 struct dm_snap_exception *e;
415 slot = &et->table[exception_hash(et, chunk)];
416 list_for_each_entry (e, slot, hash_list)
417 if (chunk >= e->old_chunk &&
418 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
424 static struct dm_snap_exception *alloc_exception(void)
426 struct dm_snap_exception *e;
428 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
430 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
435 static void free_exception(struct dm_snap_exception *e)
437 kmem_cache_free(exception_cache, e);
440 static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
442 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
445 atomic_inc(&s->pending_exceptions_count);
451 static void free_pending_exception(struct dm_snap_pending_exception *pe)
453 struct dm_snapshot *s = pe->snap;
455 mempool_free(pe, s->pending_pool);
456 smp_mb__before_atomic_dec();
457 atomic_dec(&s->pending_exceptions_count);
460 static void insert_completed_exception(struct dm_snapshot *s,
461 struct dm_snap_exception *new_e)
463 struct exception_table *eh = &s->complete;
465 struct dm_snap_exception *e = NULL;
467 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
469 /* Add immediately if this table doesn't support consecutive chunks */
473 /* List is ordered by old_chunk */
474 list_for_each_entry_reverse(e, l, hash_list) {
475 /* Insert after an existing chunk? */
476 if (new_e->old_chunk == (e->old_chunk +
477 dm_consecutive_chunk_count(e) + 1) &&
478 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
479 dm_consecutive_chunk_count(e) + 1)) {
480 dm_consecutive_chunk_count_inc(e);
481 free_exception(new_e);
485 /* Insert before an existing chunk? */
486 if (new_e->old_chunk == (e->old_chunk - 1) &&
487 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
488 dm_consecutive_chunk_count_inc(e);
491 free_exception(new_e);
495 if (new_e->old_chunk > e->old_chunk)
500 list_add(&new_e->hash_list, e ? &e->hash_list : l);
504 * Callback used by the exception stores to load exceptions when
507 static int dm_add_exception(void *context, chunk_t old, chunk_t new)
509 struct dm_snapshot *s = context;
510 struct dm_snap_exception *e;
512 e = alloc_exception();
518 /* Consecutive_count is implicitly initialised to zero */
521 insert_completed_exception(s, e);
529 static int calc_max_buckets(void)
531 /* use a fixed size of 2MB */
532 unsigned long mem = 2 * 1024 * 1024;
533 mem /= sizeof(struct list_head);
539 * Allocate room for a suitable hash table.
541 static int init_hash_tables(struct dm_snapshot *s, chunk_t chunk_shift,
544 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
547 * Calculate based on the size of the original volume or
550 cow_dev_size = get_dev_size(cow->bdev);
551 origin_dev_size = get_dev_size(s->origin->bdev);
552 max_buckets = calc_max_buckets();
554 hash_size = min(origin_dev_size, cow_dev_size) >> chunk_shift;
555 hash_size = min(hash_size, max_buckets);
557 hash_size = rounddown_pow_of_two(hash_size);
558 if (init_exception_table(&s->complete, hash_size,
559 DM_CHUNK_CONSECUTIVE_BITS))
563 * Allocate hash table for in-flight exceptions
564 * Make this smaller than the real hash table
570 if (init_exception_table(&s->pending, hash_size, 0)) {
571 exit_exception_table(&s->complete, exception_cache);
579 * Round a number up to the nearest 'size' boundary. size must
582 static ulong round_up(ulong n, ulong size)
585 return (n + size) & ~size;
588 static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
589 chunk_t *chunk_size, chunk_t *chunk_mask,
590 chunk_t *chunk_shift, struct dm_dev *cow,
593 unsigned long chunk_size_ulong;
596 chunk_size_ulong = simple_strtoul(chunk_size_arg, &value, 10);
597 if (*chunk_size_arg == '\0' || *value != '\0') {
598 *error = "Invalid chunk size";
602 if (!chunk_size_ulong) {
603 *chunk_size = *chunk_mask = *chunk_shift = 0;
608 * Chunk size must be multiple of page size. Silently
609 * round up if it's not.
611 chunk_size_ulong = round_up(chunk_size_ulong, PAGE_SIZE >> 9);
613 /* Check chunk_size is a power of 2 */
614 if (!is_power_of_2(chunk_size_ulong)) {
615 *error = "Chunk size is not a power of 2";
619 /* Validate the chunk size against the device block size */
620 if (chunk_size_ulong % (bdev_hardsect_size(cow->bdev) >> 9)) {
621 *error = "Chunk size is not a multiple of device blocksize";
625 *chunk_size = chunk_size_ulong;
626 *chunk_mask = chunk_size_ulong - 1;
627 *chunk_shift = ffs(chunk_size_ulong) - 1;
633 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
635 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
637 struct dm_snapshot *s;
643 chunk_t chunk_size, chunk_mask, chunk_shift;
647 ti->error = "requires exactly 4 arguments";
652 origin_path = argv[0];
654 persistent = toupper(*argv[2]);
656 if (persistent != 'P' && persistent != 'N') {
657 ti->error = "Persistent flag is not P or N";
662 s = kmalloc(sizeof(*s), GFP_KERNEL);
664 ti->error = "Cannot allocate snapshot context private "
670 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
672 ti->error = "Cannot get origin device";
676 r = dm_get_device(ti, cow_path, 0, 0,
677 FMODE_READ | FMODE_WRITE, &cow);
679 dm_put_device(ti, s->origin);
680 ti->error = "Cannot get COW device";
684 r = set_chunk_size(s, argv[3], &chunk_size, &chunk_mask, &chunk_shift,
691 atomic_set(&s->pending_exceptions_count, 0);
692 init_rwsem(&s->lock);
693 spin_lock_init(&s->pe_lock);
695 /* Allocate hash table for COW data */
696 if (init_hash_tables(s, chunk_shift, cow)) {
697 ti->error = "Unable to allocate hash table space";
702 r = dm_exception_store_create(argv[2], ti, chunk_size, chunk_mask,
703 chunk_shift, cow, &s->store);
705 ti->error = "Couldn't create exception store";
710 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
712 ti->error = "Could not create kcopyd client";
716 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
717 if (!s->pending_pool) {
718 ti->error = "Could not allocate mempool for pending exceptions";
722 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
723 tracked_chunk_cache);
724 if (!s->tracked_chunk_pool) {
725 ti->error = "Could not allocate tracked_chunk mempool for "
727 goto bad_tracked_chunk_pool;
730 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
731 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
733 spin_lock_init(&s->tracked_chunk_lock);
735 /* Metadata must only be loaded into one table at once */
736 r = s->store->type->read_metadata(s->store, dm_add_exception,
739 ti->error = "Failed to read snapshot metadata";
740 goto bad_load_and_register;
743 DMWARN("Snapshot is marked invalid.");
746 bio_list_init(&s->queued_bios);
747 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
749 /* Add snapshot to the list of snapshots for this origin */
750 /* Exceptions aren't triggered till snapshot_resume() is called */
751 if (register_snapshot(s)) {
753 ti->error = "Cannot register snapshot origin";
754 goto bad_load_and_register;
758 ti->split_io = s->store->chunk_size;
762 bad_load_and_register:
763 mempool_destroy(s->tracked_chunk_pool);
765 bad_tracked_chunk_pool:
766 mempool_destroy(s->pending_pool);
769 dm_kcopyd_client_destroy(s->kcopyd_client);
772 s->store->type->dtr(s->store);
775 exit_exception_table(&s->pending, pending_cache);
776 exit_exception_table(&s->complete, exception_cache);
779 dm_put_device(ti, cow);
780 dm_put_device(ti, s->origin);
789 static void __free_exceptions(struct dm_snapshot *s)
791 dm_kcopyd_client_destroy(s->kcopyd_client);
792 s->kcopyd_client = NULL;
794 exit_exception_table(&s->pending, pending_cache);
795 exit_exception_table(&s->complete, exception_cache);
797 s->store->type->dtr(s->store);
800 static void snapshot_dtr(struct dm_target *ti)
802 #ifdef CONFIG_DM_DEBUG
805 struct dm_snapshot *s = ti->private;
806 struct dm_dev *cow = s->store->cow;
808 flush_workqueue(ksnapd);
810 /* Prevent further origin writes from using this snapshot. */
811 /* After this returns there can be no new kcopyd jobs. */
812 unregister_snapshot(s);
814 while (atomic_read(&s->pending_exceptions_count))
817 * Ensure instructions in mempool_destroy aren't reordered
818 * before atomic_read.
822 #ifdef CONFIG_DM_DEBUG
823 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
824 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
827 mempool_destroy(s->tracked_chunk_pool);
829 __free_exceptions(s);
831 mempool_destroy(s->pending_pool);
833 dm_put_device(ti, s->origin);
834 dm_put_device(ti, cow);
840 * Flush a list of buffers.
842 static void flush_bios(struct bio *bio)
849 generic_make_request(bio);
854 static void flush_queued_bios(struct work_struct *work)
856 struct dm_snapshot *s =
857 container_of(work, struct dm_snapshot, queued_bios_work);
858 struct bio *queued_bios;
861 spin_lock_irqsave(&s->pe_lock, flags);
862 queued_bios = bio_list_get(&s->queued_bios);
863 spin_unlock_irqrestore(&s->pe_lock, flags);
865 flush_bios(queued_bios);
869 * Error a list of buffers.
871 static void error_bios(struct bio *bio)
883 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
889 DMERR("Invalidating snapshot: Error reading/writing.");
890 else if (err == -ENOMEM)
891 DMERR("Invalidating snapshot: Unable to allocate exception.");
893 if (s->store->type->drop_snapshot)
894 s->store->type->drop_snapshot(s->store);
898 dm_table_event(s->store->ti->table);
901 static void get_pending_exception(struct dm_snap_pending_exception *pe)
903 atomic_inc(&pe->ref_count);
906 static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
908 struct dm_snap_pending_exception *primary_pe;
909 struct bio *origin_bios = NULL;
911 primary_pe = pe->primary_pe;
914 * If this pe is involved in a write to the origin and
915 * it is the last sibling to complete then release
916 * the bios for the original write to the origin.
919 atomic_dec_and_test(&primary_pe->ref_count)) {
920 origin_bios = bio_list_get(&primary_pe->origin_bios);
921 free_pending_exception(primary_pe);
925 * Free the pe if it's not linked to an origin write or if
926 * it's not itself a primary pe.
928 if (!primary_pe || primary_pe != pe)
929 free_pending_exception(pe);
934 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
936 struct dm_snap_exception *e;
937 struct dm_snapshot *s = pe->snap;
938 struct bio *origin_bios = NULL;
939 struct bio *snapshot_bios = NULL;
943 /* Read/write error - snapshot is unusable */
944 down_write(&s->lock);
945 __invalidate_snapshot(s, -EIO);
950 e = alloc_exception();
952 down_write(&s->lock);
953 __invalidate_snapshot(s, -ENOMEM);
959 down_write(&s->lock);
967 * Check for conflicting reads. This is extremely improbable,
968 * so msleep(1) is sufficient and there is no need for a wait queue.
970 while (__chunk_is_tracked(s, pe->e.old_chunk))
974 * Add a proper exception, and remove the
975 * in-flight exception from the list.
977 insert_completed_exception(s, e);
980 remove_exception(&pe->e);
981 snapshot_bios = bio_list_get(&pe->snapshot_bios);
982 origin_bios = put_pending_exception(pe);
986 /* Submit any pending write bios */
988 error_bios(snapshot_bios);
990 flush_bios(snapshot_bios);
992 flush_bios(origin_bios);
995 static void commit_callback(void *context, int success)
997 struct dm_snap_pending_exception *pe = context;
999 pending_complete(pe, success);
1003 * Called when the copy I/O has finished. kcopyd actually runs
1004 * this code so don't block.
1006 static void copy_callback(int read_err, unsigned long write_err, void *context)
1008 struct dm_snap_pending_exception *pe = context;
1009 struct dm_snapshot *s = pe->snap;
1011 if (read_err || write_err)
1012 pending_complete(pe, 0);
1015 /* Update the metadata if we are persistent */
1016 s->store->type->commit_exception(s->store, &pe->e,
1017 commit_callback, pe);
1021 * Dispatches the copy operation to kcopyd.
1023 static void start_copy(struct dm_snap_pending_exception *pe)
1025 struct dm_snapshot *s = pe->snap;
1026 struct dm_io_region src, dest;
1027 struct block_device *bdev = s->origin->bdev;
1030 dev_size = get_dev_size(bdev);
1033 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
1034 src.count = min(s->store->chunk_size, dev_size - src.sector);
1036 dest.bdev = s->store->cow->bdev;
1037 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
1038 dest.count = src.count;
1040 /* Hand over to kcopyd */
1041 dm_kcopyd_copy(s->kcopyd_client,
1042 &src, 1, &dest, 0, copy_callback, pe);
1045 static struct dm_snap_pending_exception *
1046 __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1048 struct dm_snap_exception *e = lookup_exception(&s->pending, chunk);
1053 return container_of(e, struct dm_snap_pending_exception, e);
1057 * Looks to see if this snapshot already has a pending exception
1058 * for this chunk, otherwise it allocates a new one and inserts
1059 * it into the pending table.
1061 * NOTE: a write lock must be held on snap->lock before calling
1064 static struct dm_snap_pending_exception *
1065 __find_pending_exception(struct dm_snapshot *s,
1066 struct dm_snap_pending_exception *pe, chunk_t chunk)
1068 struct dm_snap_pending_exception *pe2;
1070 pe2 = __lookup_pending_exception(s, chunk);
1072 free_pending_exception(pe);
1076 pe->e.old_chunk = chunk;
1077 bio_list_init(&pe->origin_bios);
1078 bio_list_init(&pe->snapshot_bios);
1079 pe->primary_pe = NULL;
1080 atomic_set(&pe->ref_count, 0);
1083 if (s->store->type->prepare_exception(s->store, &pe->e)) {
1084 free_pending_exception(pe);
1088 get_pending_exception(pe);
1089 insert_exception(&s->pending, &pe->e);
1094 static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
1095 struct bio *bio, chunk_t chunk)
1097 bio->bi_bdev = s->store->cow->bdev;
1098 bio->bi_sector = chunk_to_sector(s->store,
1099 dm_chunk_number(e->new_chunk) +
1100 (chunk - e->old_chunk)) +
1102 s->store->chunk_mask);
1105 static int snapshot_map(struct dm_target *ti, struct bio *bio,
1106 union map_info *map_context)
1108 struct dm_snap_exception *e;
1109 struct dm_snapshot *s = ti->private;
1110 int r = DM_MAPIO_REMAPPED;
1112 struct dm_snap_pending_exception *pe = NULL;
1114 chunk = sector_to_chunk(s->store, bio->bi_sector);
1116 /* Full snapshots are not usable */
1117 /* To get here the table must be live so s->active is always set. */
1121 /* FIXME: should only take write lock if we need
1122 * to copy an exception */
1123 down_write(&s->lock);
1130 /* If the block is already remapped - use that, else remap it */
1131 e = lookup_exception(&s->complete, chunk);
1133 remap_exception(s, e, bio, chunk);
1138 * Write to snapshot - higher level takes care of RW/RO
1139 * flags so we should only get this if we are
1142 if (bio_rw(bio) == WRITE) {
1143 pe = __lookup_pending_exception(s, chunk);
1146 pe = alloc_pending_exception(s);
1147 down_write(&s->lock);
1150 free_pending_exception(pe);
1155 e = lookup_exception(&s->complete, chunk);
1157 free_pending_exception(pe);
1158 remap_exception(s, e, bio, chunk);
1162 pe = __find_pending_exception(s, pe, chunk);
1164 __invalidate_snapshot(s, -ENOMEM);
1170 remap_exception(s, &pe->e, bio, chunk);
1171 bio_list_add(&pe->snapshot_bios, bio);
1173 r = DM_MAPIO_SUBMITTED;
1176 /* this is protected by snap->lock */
1183 bio->bi_bdev = s->origin->bdev;
1184 map_context->ptr = track_chunk(s, chunk);
1193 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1194 int error, union map_info *map_context)
1196 struct dm_snapshot *s = ti->private;
1197 struct dm_snap_tracked_chunk *c = map_context->ptr;
1200 stop_tracking_chunk(s, c);
1205 static void snapshot_resume(struct dm_target *ti)
1207 struct dm_snapshot *s = ti->private;
1209 down_write(&s->lock);
1214 static int snapshot_status(struct dm_target *ti, status_type_t type,
1215 char *result, unsigned int maxlen)
1217 struct dm_snapshot *snap = ti->private;
1220 case STATUSTYPE_INFO:
1222 snprintf(result, maxlen, "Invalid");
1224 if (snap->store->type->fraction_full) {
1225 sector_t numerator, denominator;
1226 snap->store->type->fraction_full(snap->store,
1229 snprintf(result, maxlen, "%llu/%llu",
1230 (unsigned long long)numerator,
1231 (unsigned long long)denominator);
1234 snprintf(result, maxlen, "Unknown");
1238 case STATUSTYPE_TABLE:
1240 * kdevname returns a static pointer so we need
1241 * to make private copies if the output is to
1244 snprintf(result, maxlen, "%s %s %s %llu",
1245 snap->origin->name, snap->store->cow->name,
1246 snap->store->type->name,
1247 (unsigned long long)snap->store->chunk_size);
1254 /*-----------------------------------------------------------------
1256 *---------------------------------------------------------------*/
1257 static int __origin_write(struct list_head *snapshots, struct bio *bio)
1259 int r = DM_MAPIO_REMAPPED, first = 0;
1260 struct dm_snapshot *snap;
1261 struct dm_snap_exception *e;
1262 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
1264 LIST_HEAD(pe_queue);
1266 /* Do all the snapshots on this origin */
1267 list_for_each_entry (snap, snapshots, list) {
1269 down_write(&snap->lock);
1271 /* Only deal with valid and active snapshots */
1272 if (!snap->valid || !snap->active)
1275 /* Nothing to do if writing beyond end of snapshot */
1276 if (bio->bi_sector >= dm_table_get_size(snap->store->ti->table))
1280 * Remember, different snapshots can have
1281 * different chunk sizes.
1283 chunk = sector_to_chunk(snap->store, bio->bi_sector);
1286 * Check exception table to see if block
1287 * is already remapped in this snapshot
1288 * and trigger an exception if not.
1290 * ref_count is initialised to 1 so pending_complete()
1291 * won't destroy the primary_pe while we're inside this loop.
1293 e = lookup_exception(&snap->complete, chunk);
1297 pe = __lookup_pending_exception(snap, chunk);
1299 up_write(&snap->lock);
1300 pe = alloc_pending_exception(snap);
1301 down_write(&snap->lock);
1304 free_pending_exception(pe);
1308 e = lookup_exception(&snap->complete, chunk);
1310 free_pending_exception(pe);
1314 pe = __find_pending_exception(snap, pe, chunk);
1316 __invalidate_snapshot(snap, -ENOMEM);
1323 * Either every pe here has same
1324 * primary_pe or none has one yet.
1327 primary_pe = pe->primary_pe;
1333 bio_list_add(&primary_pe->origin_bios, bio);
1335 r = DM_MAPIO_SUBMITTED;
1338 if (!pe->primary_pe) {
1339 pe->primary_pe = primary_pe;
1340 get_pending_exception(primary_pe);
1345 list_add_tail(&pe->list, &pe_queue);
1349 up_write(&snap->lock);
1356 * If this is the first time we're processing this chunk and
1357 * ref_count is now 1 it means all the pending exceptions
1358 * got completed while we were in the loop above, so it falls to
1359 * us here to remove the primary_pe and submit any origin_bios.
1362 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
1363 flush_bios(bio_list_get(&primary_pe->origin_bios));
1364 free_pending_exception(primary_pe);
1365 /* If we got here, pe_queue is necessarily empty. */
1370 * Now that we have a complete pe list we can start the copying.
1372 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1379 * Called on a write from the origin driver.
1381 static int do_origin(struct dm_dev *origin, struct bio *bio)
1384 int r = DM_MAPIO_REMAPPED;
1386 down_read(&_origins_lock);
1387 o = __lookup_origin(origin->bdev);
1389 r = __origin_write(&o->snapshots, bio);
1390 up_read(&_origins_lock);
1396 * Origin: maps a linear range of a device, with hooks for snapshotting.
1400 * Construct an origin mapping: <dev_path>
1401 * The context for an origin is merely a 'struct dm_dev *'
1402 * pointing to the real device.
1404 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1410 ti->error = "origin: incorrect number of arguments";
1414 r = dm_get_device(ti, argv[0], 0, ti->len,
1415 dm_table_get_mode(ti->table), &dev);
1417 ti->error = "Cannot get target device";
1425 static void origin_dtr(struct dm_target *ti)
1427 struct dm_dev *dev = ti->private;
1428 dm_put_device(ti, dev);
1431 static int origin_map(struct dm_target *ti, struct bio *bio,
1432 union map_info *map_context)
1434 struct dm_dev *dev = ti->private;
1435 bio->bi_bdev = dev->bdev;
1437 /* Only tell snapshots if this is a write */
1438 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
1441 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1444 * Set the target "split_io" field to the minimum of all the snapshots'
1447 static void origin_resume(struct dm_target *ti)
1449 struct dm_dev *dev = ti->private;
1450 struct dm_snapshot *snap;
1452 chunk_t chunk_size = 0;
1454 down_read(&_origins_lock);
1455 o = __lookup_origin(dev->bdev);
1457 list_for_each_entry (snap, &o->snapshots, list)
1458 chunk_size = min_not_zero(chunk_size,
1459 snap->store->chunk_size);
1460 up_read(&_origins_lock);
1462 ti->split_io = chunk_size;
1465 static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1466 unsigned int maxlen)
1468 struct dm_dev *dev = ti->private;
1471 case STATUSTYPE_INFO:
1475 case STATUSTYPE_TABLE:
1476 snprintf(result, maxlen, "%s", dev->name);
1483 static struct target_type origin_target = {
1484 .name = "snapshot-origin",
1485 .version = {1, 6, 0},
1486 .module = THIS_MODULE,
1490 .resume = origin_resume,
1491 .status = origin_status,
1494 static struct target_type snapshot_target = {
1496 .version = {1, 6, 0},
1497 .module = THIS_MODULE,
1498 .ctr = snapshot_ctr,
1499 .dtr = snapshot_dtr,
1500 .map = snapshot_map,
1501 .end_io = snapshot_end_io,
1502 .resume = snapshot_resume,
1503 .status = snapshot_status,
1506 static int __init dm_snapshot_init(void)
1510 r = dm_exception_store_init();
1512 DMERR("Failed to initialize exception stores");
1516 r = dm_register_target(&snapshot_target);
1518 DMERR("snapshot target register failed %d", r);
1522 r = dm_register_target(&origin_target);
1524 DMERR("Origin target register failed %d", r);
1528 r = init_origin_hash();
1530 DMERR("init_origin_hash failed.");
1534 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
1535 if (!exception_cache) {
1536 DMERR("Couldn't create exception cache.");
1541 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
1542 if (!pending_cache) {
1543 DMERR("Couldn't create pending cache.");
1548 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1549 if (!tracked_chunk_cache) {
1550 DMERR("Couldn't create cache to track chunks in use.");
1555 ksnapd = create_singlethread_workqueue("ksnapd");
1557 DMERR("Failed to create ksnapd workqueue.");
1559 goto bad_pending_pool;
1565 kmem_cache_destroy(tracked_chunk_cache);
1567 kmem_cache_destroy(pending_cache);
1569 kmem_cache_destroy(exception_cache);
1573 dm_unregister_target(&origin_target);
1575 dm_unregister_target(&snapshot_target);
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");