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>
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>
24 #include "dm-bio-list.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 static struct workqueue_struct *ksnapd;
49 static void flush_queued_bios(struct work_struct *work);
51 struct dm_snap_pending_exception {
52 struct dm_snap_exception e;
55 * Origin buffers waiting for this to complete are held
58 struct bio_list origin_bios;
59 struct bio_list snapshot_bios;
62 * Short-term queue of pending exceptions prior to submission.
64 struct list_head list;
67 * The primary pending_exception is the one that holds
68 * the ref_count and the list of origin_bios for a
69 * group of pending_exceptions. It is always last to get freed.
70 * These fields get set up when writing to the origin.
72 struct dm_snap_pending_exception *primary_pe;
75 * Number of pending_exceptions processing this chunk.
76 * When this drops to zero we must complete the origin bios.
77 * If incrementing or decrementing this, hold pe->snap->lock for
78 * the sibling concerned and not pe->primary_pe->snap->lock unless
83 /* Pointer back to snapshot context */
84 struct dm_snapshot *snap;
87 * 1 indicates the exception has already been sent to
94 * Hash table mapping origin volumes to lists of snapshots and
95 * a lock to protect it
97 static struct kmem_cache *exception_cache;
98 static struct kmem_cache *pending_cache;
100 struct dm_snap_tracked_chunk {
101 struct hlist_node node;
105 static struct kmem_cache *tracked_chunk_cache;
107 static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
110 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
116 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
117 hlist_add_head(&c->node,
118 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
119 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
124 static void stop_tracking_chunk(struct dm_snapshot *s,
125 struct dm_snap_tracked_chunk *c)
129 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
131 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
133 mempool_free(c, s->tracked_chunk_pool);
136 static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
138 struct dm_snap_tracked_chunk *c;
139 struct hlist_node *hn;
142 spin_lock_irq(&s->tracked_chunk_lock);
144 hlist_for_each_entry(c, hn,
145 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
146 if (c->chunk == chunk) {
152 spin_unlock_irq(&s->tracked_chunk_lock);
158 * One of these per registered origin, held in the snapshot_origins hash
161 /* The origin device */
162 struct block_device *bdev;
164 struct list_head hash_list;
166 /* List of snapshots for this origin */
167 struct list_head snapshots;
171 * Size of the hash table for origin volumes. If we make this
172 * the size of the minors list then it should be nearly perfect
174 #define ORIGIN_HASH_SIZE 256
175 #define ORIGIN_MASK 0xFF
176 static struct list_head *_origins;
177 static struct rw_semaphore _origins_lock;
179 static int init_origin_hash(void)
183 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
186 DMERR("unable to allocate memory");
190 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
191 INIT_LIST_HEAD(_origins + i);
192 init_rwsem(&_origins_lock);
197 static void exit_origin_hash(void)
202 static unsigned origin_hash(struct block_device *bdev)
204 return bdev->bd_dev & ORIGIN_MASK;
207 static struct origin *__lookup_origin(struct block_device *origin)
209 struct list_head *ol;
212 ol = &_origins[origin_hash(origin)];
213 list_for_each_entry (o, ol, hash_list)
214 if (bdev_equal(o->bdev, origin))
220 static void __insert_origin(struct origin *o)
222 struct list_head *sl = &_origins[origin_hash(o->bdev)];
223 list_add_tail(&o->hash_list, sl);
227 * Make a note of the snapshot and its origin so we can look it
228 * up when the origin has a write on it.
230 static int register_snapshot(struct dm_snapshot *snap)
232 struct origin *o, *new_o;
233 struct block_device *bdev = snap->origin->bdev;
235 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
239 down_write(&_origins_lock);
240 o = __lookup_origin(bdev);
248 /* Initialise the struct */
249 INIT_LIST_HEAD(&o->snapshots);
255 list_add_tail(&snap->list, &o->snapshots);
257 up_write(&_origins_lock);
261 static void unregister_snapshot(struct dm_snapshot *s)
265 down_write(&_origins_lock);
266 o = __lookup_origin(s->origin->bdev);
269 if (list_empty(&o->snapshots)) {
270 list_del(&o->hash_list);
274 up_write(&_origins_lock);
278 * Implementation of the exception hash tables.
279 * The lowest hash_shift bits of the chunk number are ignored, allowing
280 * some consecutive chunks to be grouped together.
282 static int init_exception_table(struct exception_table *et, uint32_t size,
287 et->hash_shift = hash_shift;
288 et->hash_mask = size - 1;
289 et->table = dm_vcalloc(size, sizeof(struct list_head));
293 for (i = 0; i < size; i++)
294 INIT_LIST_HEAD(et->table + i);
299 static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
301 struct list_head *slot;
302 struct dm_snap_exception *ex, *next;
305 size = et->hash_mask + 1;
306 for (i = 0; i < size; i++) {
307 slot = et->table + i;
309 list_for_each_entry_safe (ex, next, slot, hash_list)
310 kmem_cache_free(mem, ex);
316 static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
318 return (chunk >> et->hash_shift) & et->hash_mask;
321 static void insert_exception(struct exception_table *eh,
322 struct dm_snap_exception *e)
324 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
325 list_add(&e->hash_list, l);
328 static void remove_exception(struct dm_snap_exception *e)
330 list_del(&e->hash_list);
334 * Return the exception data for a sector, or NULL if not
337 static struct dm_snap_exception *lookup_exception(struct exception_table *et,
340 struct list_head *slot;
341 struct dm_snap_exception *e;
343 slot = &et->table[exception_hash(et, chunk)];
344 list_for_each_entry (e, slot, hash_list)
345 if (chunk >= e->old_chunk &&
346 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
352 static struct dm_snap_exception *alloc_exception(void)
354 struct dm_snap_exception *e;
356 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
358 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
363 static void free_exception(struct dm_snap_exception *e)
365 kmem_cache_free(exception_cache, e);
368 static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
370 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
378 static void free_pending_exception(struct dm_snap_pending_exception *pe)
380 mempool_free(pe, pe->snap->pending_pool);
383 static void insert_completed_exception(struct dm_snapshot *s,
384 struct dm_snap_exception *new_e)
386 struct exception_table *eh = &s->complete;
388 struct dm_snap_exception *e = NULL;
390 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
392 /* Add immediately if this table doesn't support consecutive chunks */
396 /* List is ordered by old_chunk */
397 list_for_each_entry_reverse(e, l, hash_list) {
398 /* Insert after an existing chunk? */
399 if (new_e->old_chunk == (e->old_chunk +
400 dm_consecutive_chunk_count(e) + 1) &&
401 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
402 dm_consecutive_chunk_count(e) + 1)) {
403 dm_consecutive_chunk_count_inc(e);
404 free_exception(new_e);
408 /* Insert before an existing chunk? */
409 if (new_e->old_chunk == (e->old_chunk - 1) &&
410 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
411 dm_consecutive_chunk_count_inc(e);
414 free_exception(new_e);
418 if (new_e->old_chunk > e->old_chunk)
423 list_add(&new_e->hash_list, e ? &e->hash_list : l);
426 int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
428 struct dm_snap_exception *e;
430 e = alloc_exception();
436 /* Consecutive_count is implicitly initialised to zero */
439 insert_completed_exception(s, e);
447 static int calc_max_buckets(void)
449 /* use a fixed size of 2MB */
450 unsigned long mem = 2 * 1024 * 1024;
451 mem /= sizeof(struct list_head);
457 * Allocate room for a suitable hash table.
459 static int init_hash_tables(struct dm_snapshot *s)
461 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
464 * Calculate based on the size of the original volume or
467 cow_dev_size = get_dev_size(s->cow->bdev);
468 origin_dev_size = get_dev_size(s->origin->bdev);
469 max_buckets = calc_max_buckets();
471 hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
472 hash_size = min(hash_size, max_buckets);
474 hash_size = rounddown_pow_of_two(hash_size);
475 if (init_exception_table(&s->complete, hash_size,
476 DM_CHUNK_CONSECUTIVE_BITS))
480 * Allocate hash table for in-flight exceptions
481 * Make this smaller than the real hash table
487 if (init_exception_table(&s->pending, hash_size, 0)) {
488 exit_exception_table(&s->complete, exception_cache);
496 * Round a number up to the nearest 'size' boundary. size must
499 static ulong round_up(ulong n, ulong size)
502 return (n + size) & ~size;
505 static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
508 unsigned long chunk_size;
511 chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
512 if (*chunk_size_arg == '\0' || *value != '\0') {
513 *error = "Invalid chunk size";
518 s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
523 * Chunk size must be multiple of page size. Silently
524 * round up if it's not.
526 chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
528 /* Check chunk_size is a power of 2 */
529 if (!is_power_of_2(chunk_size)) {
530 *error = "Chunk size is not a power of 2";
534 /* Validate the chunk size against the device block size */
535 if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
536 *error = "Chunk size is not a multiple of device blocksize";
540 s->chunk_size = chunk_size;
541 s->chunk_mask = chunk_size - 1;
542 s->chunk_shift = ffs(chunk_size) - 1;
548 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
550 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
552 struct dm_snapshot *s;
560 ti->error = "requires exactly 4 arguments";
565 origin_path = argv[0];
567 persistent = toupper(*argv[2]);
569 if (persistent != 'P' && persistent != 'N') {
570 ti->error = "Persistent flag is not P or N";
575 s = kmalloc(sizeof(*s), GFP_KERNEL);
577 ti->error = "Cannot allocate snapshot context private "
583 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
585 ti->error = "Cannot get origin device";
589 r = dm_get_device(ti, cow_path, 0, 0,
590 FMODE_READ | FMODE_WRITE, &s->cow);
592 dm_put_device(ti, s->origin);
593 ti->error = "Cannot get COW device";
597 r = set_chunk_size(s, argv[3], &ti->error);
601 s->type = persistent;
605 init_rwsem(&s->lock);
606 spin_lock_init(&s->pe_lock);
609 /* Allocate hash table for COW data */
610 if (init_hash_tables(s)) {
611 ti->error = "Unable to allocate hash table space";
618 if (persistent == 'P')
619 r = dm_create_persistent(&s->store);
621 r = dm_create_transient(&s->store);
624 ti->error = "Couldn't create exception store";
629 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
631 ti->error = "Could not create kcopyd client";
635 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
636 if (!s->pending_pool) {
637 ti->error = "Could not allocate mempool for pending exceptions";
641 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
642 tracked_chunk_cache);
643 if (!s->tracked_chunk_pool) {
644 ti->error = "Could not allocate tracked_chunk mempool for "
646 goto bad_tracked_chunk_pool;
649 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
650 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
652 spin_lock_init(&s->tracked_chunk_lock);
654 /* Metadata must only be loaded into one table at once */
655 r = s->store.read_metadata(&s->store);
657 ti->error = "Failed to read snapshot metadata";
658 goto bad_load_and_register;
661 DMWARN("Snapshot is marked invalid.");
664 bio_list_init(&s->queued_bios);
665 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
667 /* Add snapshot to the list of snapshots for this origin */
668 /* Exceptions aren't triggered till snapshot_resume() is called */
669 if (register_snapshot(s)) {
671 ti->error = "Cannot register snapshot origin";
672 goto bad_load_and_register;
676 ti->split_io = s->chunk_size;
680 bad_load_and_register:
681 mempool_destroy(s->tracked_chunk_pool);
683 bad_tracked_chunk_pool:
684 mempool_destroy(s->pending_pool);
687 dm_kcopyd_client_destroy(s->kcopyd_client);
690 s->store.destroy(&s->store);
693 exit_exception_table(&s->pending, pending_cache);
694 exit_exception_table(&s->complete, exception_cache);
697 dm_put_device(ti, s->cow);
698 dm_put_device(ti, s->origin);
707 static void __free_exceptions(struct dm_snapshot *s)
709 dm_kcopyd_client_destroy(s->kcopyd_client);
710 s->kcopyd_client = NULL;
712 exit_exception_table(&s->pending, pending_cache);
713 exit_exception_table(&s->complete, exception_cache);
715 s->store.destroy(&s->store);
718 static void snapshot_dtr(struct dm_target *ti)
720 #ifdef CONFIG_DM_DEBUG
723 struct dm_snapshot *s = ti->private;
725 flush_workqueue(ksnapd);
727 /* Prevent further origin writes from using this snapshot. */
728 /* After this returns there can be no new kcopyd jobs. */
729 unregister_snapshot(s);
731 #ifdef CONFIG_DM_DEBUG
732 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
733 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
736 mempool_destroy(s->tracked_chunk_pool);
738 __free_exceptions(s);
740 mempool_destroy(s->pending_pool);
742 dm_put_device(ti, s->origin);
743 dm_put_device(ti, s->cow);
749 * Flush a list of buffers.
751 static void flush_bios(struct bio *bio)
758 generic_make_request(bio);
763 static void flush_queued_bios(struct work_struct *work)
765 struct dm_snapshot *s =
766 container_of(work, struct dm_snapshot, queued_bios_work);
767 struct bio *queued_bios;
770 spin_lock_irqsave(&s->pe_lock, flags);
771 queued_bios = bio_list_get(&s->queued_bios);
772 spin_unlock_irqrestore(&s->pe_lock, flags);
774 flush_bios(queued_bios);
778 * Error a list of buffers.
780 static void error_bios(struct bio *bio)
792 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
798 DMERR("Invalidating snapshot: Error reading/writing.");
799 else if (err == -ENOMEM)
800 DMERR("Invalidating snapshot: Unable to allocate exception.");
802 if (s->store.drop_snapshot)
803 s->store.drop_snapshot(&s->store);
807 dm_table_event(s->ti->table);
810 static void get_pending_exception(struct dm_snap_pending_exception *pe)
812 atomic_inc(&pe->ref_count);
815 static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
817 struct dm_snap_pending_exception *primary_pe;
818 struct bio *origin_bios = NULL;
820 primary_pe = pe->primary_pe;
823 * If this pe is involved in a write to the origin and
824 * it is the last sibling to complete then release
825 * the bios for the original write to the origin.
828 atomic_dec_and_test(&primary_pe->ref_count)) {
829 origin_bios = bio_list_get(&primary_pe->origin_bios);
830 free_pending_exception(primary_pe);
834 * Free the pe if it's not linked to an origin write or if
835 * it's not itself a primary pe.
837 if (!primary_pe || primary_pe != pe)
838 free_pending_exception(pe);
843 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
845 struct dm_snap_exception *e;
846 struct dm_snapshot *s = pe->snap;
847 struct bio *origin_bios = NULL;
848 struct bio *snapshot_bios = NULL;
852 /* Read/write error - snapshot is unusable */
853 down_write(&s->lock);
854 __invalidate_snapshot(s, -EIO);
859 e = alloc_exception();
861 down_write(&s->lock);
862 __invalidate_snapshot(s, -ENOMEM);
868 down_write(&s->lock);
876 * Check for conflicting reads. This is extremely improbable,
877 * so yield() is sufficient and there is no need for a wait queue.
879 while (__chunk_is_tracked(s, pe->e.old_chunk))
883 * Add a proper exception, and remove the
884 * in-flight exception from the list.
886 insert_completed_exception(s, e);
889 remove_exception(&pe->e);
890 snapshot_bios = bio_list_get(&pe->snapshot_bios);
891 origin_bios = put_pending_exception(pe);
895 /* Submit any pending write bios */
897 error_bios(snapshot_bios);
899 flush_bios(snapshot_bios);
901 flush_bios(origin_bios);
904 static void commit_callback(void *context, int success)
906 struct dm_snap_pending_exception *pe = context;
908 pending_complete(pe, success);
912 * Called when the copy I/O has finished. kcopyd actually runs
913 * this code so don't block.
915 static void copy_callback(int read_err, unsigned long write_err, void *context)
917 struct dm_snap_pending_exception *pe = context;
918 struct dm_snapshot *s = pe->snap;
920 if (read_err || write_err)
921 pending_complete(pe, 0);
924 /* Update the metadata if we are persistent */
925 s->store.commit_exception(&s->store, &pe->e, commit_callback,
930 * Dispatches the copy operation to kcopyd.
932 static void start_copy(struct dm_snap_pending_exception *pe)
934 struct dm_snapshot *s = pe->snap;
935 struct dm_io_region src, dest;
936 struct block_device *bdev = s->origin->bdev;
939 dev_size = get_dev_size(bdev);
942 src.sector = chunk_to_sector(s, pe->e.old_chunk);
943 src.count = min(s->chunk_size, dev_size - src.sector);
945 dest.bdev = s->cow->bdev;
946 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
947 dest.count = src.count;
949 /* Hand over to kcopyd */
950 dm_kcopyd_copy(s->kcopyd_client,
951 &src, 1, &dest, 0, copy_callback, pe);
955 * Looks to see if this snapshot already has a pending exception
956 * for this chunk, otherwise it allocates a new one and inserts
957 * it into the pending table.
959 * NOTE: a write lock must be held on snap->lock before calling
962 static struct dm_snap_pending_exception *
963 __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
965 struct dm_snap_exception *e;
966 struct dm_snap_pending_exception *pe;
967 chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
970 * Is there a pending exception for this already ?
972 e = lookup_exception(&s->pending, chunk);
974 /* cast the exception to a pending exception */
975 pe = container_of(e, struct dm_snap_pending_exception, e);
980 * Create a new pending exception, we don't want
981 * to hold the lock while we do this.
984 pe = alloc_pending_exception(s);
985 down_write(&s->lock);
988 free_pending_exception(pe);
992 e = lookup_exception(&s->pending, chunk);
994 free_pending_exception(pe);
995 pe = container_of(e, struct dm_snap_pending_exception, e);
999 pe->e.old_chunk = chunk;
1000 bio_list_init(&pe->origin_bios);
1001 bio_list_init(&pe->snapshot_bios);
1002 pe->primary_pe = NULL;
1003 atomic_set(&pe->ref_count, 0);
1006 if (s->store.prepare_exception(&s->store, &pe->e)) {
1007 free_pending_exception(pe);
1011 get_pending_exception(pe);
1012 insert_exception(&s->pending, &pe->e);
1018 static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
1019 struct bio *bio, chunk_t chunk)
1021 bio->bi_bdev = s->cow->bdev;
1022 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
1023 (chunk - e->old_chunk)) +
1024 (bio->bi_sector & s->chunk_mask);
1027 static int snapshot_map(struct dm_target *ti, struct bio *bio,
1028 union map_info *map_context)
1030 struct dm_snap_exception *e;
1031 struct dm_snapshot *s = ti->private;
1032 int r = DM_MAPIO_REMAPPED;
1034 struct dm_snap_pending_exception *pe = NULL;
1036 chunk = sector_to_chunk(s, bio->bi_sector);
1038 /* Full snapshots are not usable */
1039 /* To get here the table must be live so s->active is always set. */
1043 /* FIXME: should only take write lock if we need
1044 * to copy an exception */
1045 down_write(&s->lock);
1052 /* If the block is already remapped - use that, else remap it */
1053 e = lookup_exception(&s->complete, chunk);
1055 remap_exception(s, e, bio, chunk);
1060 * Write to snapshot - higher level takes care of RW/RO
1061 * flags so we should only get this if we are
1064 if (bio_rw(bio) == WRITE) {
1065 pe = __find_pending_exception(s, bio);
1067 __invalidate_snapshot(s, -ENOMEM);
1072 remap_exception(s, &pe->e, bio, chunk);
1073 bio_list_add(&pe->snapshot_bios, bio);
1075 r = DM_MAPIO_SUBMITTED;
1078 /* this is protected by snap->lock */
1085 bio->bi_bdev = s->origin->bdev;
1086 map_context->ptr = track_chunk(s, chunk);
1095 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1096 int error, union map_info *map_context)
1098 struct dm_snapshot *s = ti->private;
1099 struct dm_snap_tracked_chunk *c = map_context->ptr;
1102 stop_tracking_chunk(s, c);
1107 static void snapshot_resume(struct dm_target *ti)
1109 struct dm_snapshot *s = ti->private;
1111 down_write(&s->lock);
1116 static int snapshot_status(struct dm_target *ti, status_type_t type,
1117 char *result, unsigned int maxlen)
1119 struct dm_snapshot *snap = ti->private;
1122 case STATUSTYPE_INFO:
1124 snprintf(result, maxlen, "Invalid");
1126 if (snap->store.fraction_full) {
1127 sector_t numerator, denominator;
1128 snap->store.fraction_full(&snap->store,
1131 snprintf(result, maxlen, "%llu/%llu",
1132 (unsigned long long)numerator,
1133 (unsigned long long)denominator);
1136 snprintf(result, maxlen, "Unknown");
1140 case STATUSTYPE_TABLE:
1142 * kdevname returns a static pointer so we need
1143 * to make private copies if the output is to
1146 snprintf(result, maxlen, "%s %s %c %llu",
1147 snap->origin->name, snap->cow->name,
1149 (unsigned long long)snap->chunk_size);
1156 /*-----------------------------------------------------------------
1158 *---------------------------------------------------------------*/
1159 static int __origin_write(struct list_head *snapshots, struct bio *bio)
1161 int r = DM_MAPIO_REMAPPED, first = 0;
1162 struct dm_snapshot *snap;
1163 struct dm_snap_exception *e;
1164 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
1166 LIST_HEAD(pe_queue);
1168 /* Do all the snapshots on this origin */
1169 list_for_each_entry (snap, snapshots, list) {
1171 down_write(&snap->lock);
1173 /* Only deal with valid and active snapshots */
1174 if (!snap->valid || !snap->active)
1177 /* Nothing to do if writing beyond end of snapshot */
1178 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
1182 * Remember, different snapshots can have
1183 * different chunk sizes.
1185 chunk = sector_to_chunk(snap, bio->bi_sector);
1188 * Check exception table to see if block
1189 * is already remapped in this snapshot
1190 * and trigger an exception if not.
1192 * ref_count is initialised to 1 so pending_complete()
1193 * won't destroy the primary_pe while we're inside this loop.
1195 e = lookup_exception(&snap->complete, chunk);
1199 pe = __find_pending_exception(snap, bio);
1201 __invalidate_snapshot(snap, -ENOMEM);
1207 * Either every pe here has same
1208 * primary_pe or none has one yet.
1211 primary_pe = pe->primary_pe;
1217 bio_list_add(&primary_pe->origin_bios, bio);
1219 r = DM_MAPIO_SUBMITTED;
1222 if (!pe->primary_pe) {
1223 pe->primary_pe = primary_pe;
1224 get_pending_exception(primary_pe);
1229 list_add_tail(&pe->list, &pe_queue);
1233 up_write(&snap->lock);
1240 * If this is the first time we're processing this chunk and
1241 * ref_count is now 1 it means all the pending exceptions
1242 * got completed while we were in the loop above, so it falls to
1243 * us here to remove the primary_pe and submit any origin_bios.
1246 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
1247 flush_bios(bio_list_get(&primary_pe->origin_bios));
1248 free_pending_exception(primary_pe);
1249 /* If we got here, pe_queue is necessarily empty. */
1254 * Now that we have a complete pe list we can start the copying.
1256 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1263 * Called on a write from the origin driver.
1265 static int do_origin(struct dm_dev *origin, struct bio *bio)
1268 int r = DM_MAPIO_REMAPPED;
1270 down_read(&_origins_lock);
1271 o = __lookup_origin(origin->bdev);
1273 r = __origin_write(&o->snapshots, bio);
1274 up_read(&_origins_lock);
1280 * Origin: maps a linear range of a device, with hooks for snapshotting.
1284 * Construct an origin mapping: <dev_path>
1285 * The context for an origin is merely a 'struct dm_dev *'
1286 * pointing to the real device.
1288 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1294 ti->error = "origin: incorrect number of arguments";
1298 r = dm_get_device(ti, argv[0], 0, ti->len,
1299 dm_table_get_mode(ti->table), &dev);
1301 ti->error = "Cannot get target device";
1309 static void origin_dtr(struct dm_target *ti)
1311 struct dm_dev *dev = ti->private;
1312 dm_put_device(ti, dev);
1315 static int origin_map(struct dm_target *ti, struct bio *bio,
1316 union map_info *map_context)
1318 struct dm_dev *dev = ti->private;
1319 bio->bi_bdev = dev->bdev;
1321 /* Only tell snapshots if this is a write */
1322 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
1325 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1328 * Set the target "split_io" field to the minimum of all the snapshots'
1331 static void origin_resume(struct dm_target *ti)
1333 struct dm_dev *dev = ti->private;
1334 struct dm_snapshot *snap;
1336 chunk_t chunk_size = 0;
1338 down_read(&_origins_lock);
1339 o = __lookup_origin(dev->bdev);
1341 list_for_each_entry (snap, &o->snapshots, list)
1342 chunk_size = min_not_zero(chunk_size, snap->chunk_size);
1343 up_read(&_origins_lock);
1345 ti->split_io = chunk_size;
1348 static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1349 unsigned int maxlen)
1351 struct dm_dev *dev = ti->private;
1354 case STATUSTYPE_INFO:
1358 case STATUSTYPE_TABLE:
1359 snprintf(result, maxlen, "%s", dev->name);
1366 static struct target_type origin_target = {
1367 .name = "snapshot-origin",
1368 .version = {1, 6, 0},
1369 .module = THIS_MODULE,
1373 .resume = origin_resume,
1374 .status = origin_status,
1377 static struct target_type snapshot_target = {
1379 .version = {1, 6, 0},
1380 .module = THIS_MODULE,
1381 .ctr = snapshot_ctr,
1382 .dtr = snapshot_dtr,
1383 .map = snapshot_map,
1384 .end_io = snapshot_end_io,
1385 .resume = snapshot_resume,
1386 .status = snapshot_status,
1389 static int __init dm_snapshot_init(void)
1393 r = dm_register_target(&snapshot_target);
1395 DMERR("snapshot target register failed %d", r);
1399 r = dm_register_target(&origin_target);
1401 DMERR("Origin target register failed %d", r);
1405 r = init_origin_hash();
1407 DMERR("init_origin_hash failed.");
1411 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
1412 if (!exception_cache) {
1413 DMERR("Couldn't create exception cache.");
1418 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
1419 if (!pending_cache) {
1420 DMERR("Couldn't create pending cache.");
1425 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1426 if (!tracked_chunk_cache) {
1427 DMERR("Couldn't create cache to track chunks in use.");
1432 ksnapd = create_singlethread_workqueue("ksnapd");
1434 DMERR("Failed to create ksnapd workqueue.");
1436 goto bad_pending_pool;
1442 kmem_cache_destroy(tracked_chunk_cache);
1444 kmem_cache_destroy(pending_cache);
1446 kmem_cache_destroy(exception_cache);
1450 dm_unregister_target(&origin_target);
1452 dm_unregister_target(&snapshot_target);
1456 static void __exit dm_snapshot_exit(void)
1460 destroy_workqueue(ksnapd);
1462 r = dm_unregister_target(&snapshot_target);
1464 DMERR("snapshot unregister failed %d", r);
1466 r = dm_unregister_target(&origin_target);
1468 DMERR("origin unregister failed %d", r);
1471 kmem_cache_destroy(pending_cache);
1472 kmem_cache_destroy(exception_cache);
1473 kmem_cache_destroy(tracked_chunk_cache);
1477 module_init(dm_snapshot_init);
1478 module_exit(dm_snapshot_exit);
1480 MODULE_DESCRIPTION(DM_NAME " snapshot target");
1481 MODULE_AUTHOR("Joe Thornber");
1482 MODULE_LICENSE("GPL");