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;
99 static mempool_t *pending_pool;
101 struct dm_snap_tracked_chunk {
102 struct hlist_node node;
106 static struct kmem_cache *tracked_chunk_cache;
108 static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
111 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
117 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
118 hlist_add_head(&c->node,
119 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
120 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
125 static void stop_tracking_chunk(struct dm_snapshot *s,
126 struct dm_snap_tracked_chunk *c)
130 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
132 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
134 mempool_free(c, s->tracked_chunk_pool);
137 static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
139 struct dm_snap_tracked_chunk *c;
140 struct hlist_node *hn;
143 spin_lock_irq(&s->tracked_chunk_lock);
145 hlist_for_each_entry(c, hn,
146 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
147 if (c->chunk == chunk) {
153 spin_unlock_irq(&s->tracked_chunk_lock);
159 * One of these per registered origin, held in the snapshot_origins hash
162 /* The origin device */
163 struct block_device *bdev;
165 struct list_head hash_list;
167 /* List of snapshots for this origin */
168 struct list_head snapshots;
172 * Size of the hash table for origin volumes. If we make this
173 * the size of the minors list then it should be nearly perfect
175 #define ORIGIN_HASH_SIZE 256
176 #define ORIGIN_MASK 0xFF
177 static struct list_head *_origins;
178 static struct rw_semaphore _origins_lock;
180 static int init_origin_hash(void)
184 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
187 DMERR("unable to allocate memory");
191 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
192 INIT_LIST_HEAD(_origins + i);
193 init_rwsem(&_origins_lock);
198 static void exit_origin_hash(void)
203 static unsigned origin_hash(struct block_device *bdev)
205 return bdev->bd_dev & ORIGIN_MASK;
208 static struct origin *__lookup_origin(struct block_device *origin)
210 struct list_head *ol;
213 ol = &_origins[origin_hash(origin)];
214 list_for_each_entry (o, ol, hash_list)
215 if (bdev_equal(o->bdev, origin))
221 static void __insert_origin(struct origin *o)
223 struct list_head *sl = &_origins[origin_hash(o->bdev)];
224 list_add_tail(&o->hash_list, sl);
228 * Make a note of the snapshot and its origin so we can look it
229 * up when the origin has a write on it.
231 static int register_snapshot(struct dm_snapshot *snap)
234 struct block_device *bdev = snap->origin->bdev;
236 down_write(&_origins_lock);
237 o = __lookup_origin(bdev);
241 o = kmalloc(sizeof(*o), GFP_KERNEL);
243 up_write(&_origins_lock);
247 /* Initialise the struct */
248 INIT_LIST_HEAD(&o->snapshots);
254 list_add_tail(&snap->list, &o->snapshots);
256 up_write(&_origins_lock);
260 static void unregister_snapshot(struct dm_snapshot *s)
264 down_write(&_origins_lock);
265 o = __lookup_origin(s->origin->bdev);
268 if (list_empty(&o->snapshots)) {
269 list_del(&o->hash_list);
273 up_write(&_origins_lock);
277 * Implementation of the exception hash tables.
278 * The lowest hash_shift bits of the chunk number are ignored, allowing
279 * some consecutive chunks to be grouped together.
281 static int init_exception_table(struct exception_table *et, uint32_t size,
286 et->hash_shift = hash_shift;
287 et->hash_mask = size - 1;
288 et->table = dm_vcalloc(size, sizeof(struct list_head));
292 for (i = 0; i < size; i++)
293 INIT_LIST_HEAD(et->table + i);
298 static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
300 struct list_head *slot;
301 struct dm_snap_exception *ex, *next;
304 size = et->hash_mask + 1;
305 for (i = 0; i < size; i++) {
306 slot = et->table + i;
308 list_for_each_entry_safe (ex, next, slot, hash_list)
309 kmem_cache_free(mem, ex);
315 static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
317 return (chunk >> et->hash_shift) & et->hash_mask;
320 static void insert_exception(struct exception_table *eh,
321 struct dm_snap_exception *e)
323 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
324 list_add(&e->hash_list, l);
327 static void remove_exception(struct dm_snap_exception *e)
329 list_del(&e->hash_list);
333 * Return the exception data for a sector, or NULL if not
336 static struct dm_snap_exception *lookup_exception(struct exception_table *et,
339 struct list_head *slot;
340 struct dm_snap_exception *e;
342 slot = &et->table[exception_hash(et, chunk)];
343 list_for_each_entry (e, slot, hash_list)
344 if (chunk >= e->old_chunk &&
345 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
351 static struct dm_snap_exception *alloc_exception(void)
353 struct dm_snap_exception *e;
355 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
357 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
362 static void free_exception(struct dm_snap_exception *e)
364 kmem_cache_free(exception_cache, e);
367 static struct dm_snap_pending_exception *alloc_pending_exception(void)
369 return mempool_alloc(pending_pool, GFP_NOIO);
372 static void free_pending_exception(struct dm_snap_pending_exception *pe)
374 mempool_free(pe, pending_pool);
377 static void insert_completed_exception(struct dm_snapshot *s,
378 struct dm_snap_exception *new_e)
380 struct exception_table *eh = &s->complete;
382 struct dm_snap_exception *e = NULL;
384 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
386 /* Add immediately if this table doesn't support consecutive chunks */
390 /* List is ordered by old_chunk */
391 list_for_each_entry_reverse(e, l, hash_list) {
392 /* Insert after an existing chunk? */
393 if (new_e->old_chunk == (e->old_chunk +
394 dm_consecutive_chunk_count(e) + 1) &&
395 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
396 dm_consecutive_chunk_count(e) + 1)) {
397 dm_consecutive_chunk_count_inc(e);
398 free_exception(new_e);
402 /* Insert before an existing chunk? */
403 if (new_e->old_chunk == (e->old_chunk - 1) &&
404 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
405 dm_consecutive_chunk_count_inc(e);
408 free_exception(new_e);
412 if (new_e->old_chunk > e->old_chunk)
417 list_add(&new_e->hash_list, e ? &e->hash_list : l);
420 int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
422 struct dm_snap_exception *e;
424 e = alloc_exception();
430 /* Consecutive_count is implicitly initialised to zero */
433 insert_completed_exception(s, e);
441 static int calc_max_buckets(void)
443 /* use a fixed size of 2MB */
444 unsigned long mem = 2 * 1024 * 1024;
445 mem /= sizeof(struct list_head);
451 * Allocate room for a suitable hash table.
453 static int init_hash_tables(struct dm_snapshot *s)
455 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
458 * Calculate based on the size of the original volume or
461 cow_dev_size = get_dev_size(s->cow->bdev);
462 origin_dev_size = get_dev_size(s->origin->bdev);
463 max_buckets = calc_max_buckets();
465 hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
466 hash_size = min(hash_size, max_buckets);
468 hash_size = rounddown_pow_of_two(hash_size);
469 if (init_exception_table(&s->complete, hash_size,
470 DM_CHUNK_CONSECUTIVE_BITS))
474 * Allocate hash table for in-flight exceptions
475 * Make this smaller than the real hash table
481 if (init_exception_table(&s->pending, hash_size, 0)) {
482 exit_exception_table(&s->complete, exception_cache);
490 * Round a number up to the nearest 'size' boundary. size must
493 static ulong round_up(ulong n, ulong size)
496 return (n + size) & ~size;
499 static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
502 unsigned long chunk_size;
505 chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
506 if (*chunk_size_arg == '\0' || *value != '\0') {
507 *error = "Invalid chunk size";
512 s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
517 * Chunk size must be multiple of page size. Silently
518 * round up if it's not.
520 chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
522 /* Check chunk_size is a power of 2 */
523 if (!is_power_of_2(chunk_size)) {
524 *error = "Chunk size is not a power of 2";
528 /* Validate the chunk size against the device block size */
529 if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
530 *error = "Chunk size is not a multiple of device blocksize";
534 s->chunk_size = chunk_size;
535 s->chunk_mask = chunk_size - 1;
536 s->chunk_shift = ffs(chunk_size) - 1;
542 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
544 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
546 struct dm_snapshot *s;
554 ti->error = "requires exactly 4 arguments";
559 origin_path = argv[0];
561 persistent = toupper(*argv[2]);
563 if (persistent != 'P' && persistent != 'N') {
564 ti->error = "Persistent flag is not P or N";
569 s = kmalloc(sizeof(*s), GFP_KERNEL);
571 ti->error = "Cannot allocate snapshot context private "
577 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
579 ti->error = "Cannot get origin device";
583 r = dm_get_device(ti, cow_path, 0, 0,
584 FMODE_READ | FMODE_WRITE, &s->cow);
586 dm_put_device(ti, s->origin);
587 ti->error = "Cannot get COW device";
591 r = set_chunk_size(s, argv[3], &ti->error);
595 s->type = persistent;
600 init_rwsem(&s->lock);
601 spin_lock_init(&s->pe_lock);
604 /* Allocate hash table for COW data */
605 if (init_hash_tables(s)) {
606 ti->error = "Unable to allocate hash table space";
613 if (persistent == 'P')
614 r = dm_create_persistent(&s->store);
616 r = dm_create_transient(&s->store);
619 ti->error = "Couldn't create exception store";
624 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
626 ti->error = "Could not create kcopyd client";
630 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
631 tracked_chunk_cache);
632 if (!s->tracked_chunk_pool) {
633 ti->error = "Could not allocate tracked_chunk mempool for "
638 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
639 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
641 spin_lock_init(&s->tracked_chunk_lock);
643 /* Metadata must only be loaded into one table at once */
644 r = s->store.read_metadata(&s->store);
646 ti->error = "Failed to read snapshot metadata";
647 goto bad_load_and_register;
650 DMWARN("Snapshot is marked invalid.");
653 bio_list_init(&s->queued_bios);
654 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
656 /* Add snapshot to the list of snapshots for this origin */
657 /* Exceptions aren't triggered till snapshot_resume() is called */
658 if (register_snapshot(s)) {
660 ti->error = "Cannot register snapshot origin";
661 goto bad_load_and_register;
665 ti->split_io = s->chunk_size;
669 bad_load_and_register:
670 mempool_destroy(s->tracked_chunk_pool);
673 dm_kcopyd_client_destroy(s->kcopyd_client);
676 s->store.destroy(&s->store);
679 exit_exception_table(&s->pending, pending_cache);
680 exit_exception_table(&s->complete, exception_cache);
683 dm_put_device(ti, s->cow);
684 dm_put_device(ti, s->origin);
693 static void __free_exceptions(struct dm_snapshot *s)
695 dm_kcopyd_client_destroy(s->kcopyd_client);
696 s->kcopyd_client = NULL;
698 exit_exception_table(&s->pending, pending_cache);
699 exit_exception_table(&s->complete, exception_cache);
701 s->store.destroy(&s->store);
704 static void snapshot_dtr(struct dm_target *ti)
706 #ifdef CONFIG_DM_DEBUG
709 struct dm_snapshot *s = ti->private;
711 flush_workqueue(ksnapd);
713 /* Prevent further origin writes from using this snapshot. */
714 /* After this returns there can be no new kcopyd jobs. */
715 unregister_snapshot(s);
717 #ifdef CONFIG_DM_DEBUG
718 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
719 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
722 mempool_destroy(s->tracked_chunk_pool);
724 __free_exceptions(s);
726 dm_put_device(ti, s->origin);
727 dm_put_device(ti, s->cow);
733 * Flush a list of buffers.
735 static void flush_bios(struct bio *bio)
742 generic_make_request(bio);
747 static void flush_queued_bios(struct work_struct *work)
749 struct dm_snapshot *s =
750 container_of(work, struct dm_snapshot, queued_bios_work);
751 struct bio *queued_bios;
754 spin_lock_irqsave(&s->pe_lock, flags);
755 queued_bios = bio_list_get(&s->queued_bios);
756 spin_unlock_irqrestore(&s->pe_lock, flags);
758 flush_bios(queued_bios);
762 * Error a list of buffers.
764 static void error_bios(struct bio *bio)
776 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
782 DMERR("Invalidating snapshot: Error reading/writing.");
783 else if (err == -ENOMEM)
784 DMERR("Invalidating snapshot: Unable to allocate exception.");
786 if (s->store.drop_snapshot)
787 s->store.drop_snapshot(&s->store);
791 dm_table_event(s->ti->table);
794 static void get_pending_exception(struct dm_snap_pending_exception *pe)
796 atomic_inc(&pe->ref_count);
799 static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
801 struct dm_snap_pending_exception *primary_pe;
802 struct bio *origin_bios = NULL;
804 primary_pe = pe->primary_pe;
807 * If this pe is involved in a write to the origin and
808 * it is the last sibling to complete then release
809 * the bios for the original write to the origin.
812 atomic_dec_and_test(&primary_pe->ref_count))
813 origin_bios = bio_list_get(&primary_pe->origin_bios);
816 * Free the pe if it's not linked to an origin write or if
817 * it's not itself a primary pe.
819 if (!primary_pe || primary_pe != pe)
820 free_pending_exception(pe);
823 * Free the primary pe if nothing references it.
825 if (primary_pe && !atomic_read(&primary_pe->ref_count))
826 free_pending_exception(primary_pe);
831 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
833 struct dm_snap_exception *e;
834 struct dm_snapshot *s = pe->snap;
835 struct bio *origin_bios = NULL;
836 struct bio *snapshot_bios = NULL;
840 /* Read/write error - snapshot is unusable */
841 down_write(&s->lock);
842 __invalidate_snapshot(s, -EIO);
847 e = alloc_exception();
849 down_write(&s->lock);
850 __invalidate_snapshot(s, -ENOMEM);
856 down_write(&s->lock);
864 * Check for conflicting reads. This is extremely improbable,
865 * so yield() is sufficient and there is no need for a wait queue.
867 while (__chunk_is_tracked(s, pe->e.old_chunk))
871 * Add a proper exception, and remove the
872 * in-flight exception from the list.
874 insert_completed_exception(s, e);
877 remove_exception(&pe->e);
878 snapshot_bios = bio_list_get(&pe->snapshot_bios);
879 origin_bios = put_pending_exception(pe);
883 /* Submit any pending write bios */
885 error_bios(snapshot_bios);
887 flush_bios(snapshot_bios);
889 flush_bios(origin_bios);
892 static void commit_callback(void *context, int success)
894 struct dm_snap_pending_exception *pe = context;
896 pending_complete(pe, success);
900 * Called when the copy I/O has finished. kcopyd actually runs
901 * this code so don't block.
903 static void copy_callback(int read_err, unsigned long write_err, void *context)
905 struct dm_snap_pending_exception *pe = context;
906 struct dm_snapshot *s = pe->snap;
908 if (read_err || write_err)
909 pending_complete(pe, 0);
912 /* Update the metadata if we are persistent */
913 s->store.commit_exception(&s->store, &pe->e, commit_callback,
918 * Dispatches the copy operation to kcopyd.
920 static void start_copy(struct dm_snap_pending_exception *pe)
922 struct dm_snapshot *s = pe->snap;
923 struct dm_io_region src, dest;
924 struct block_device *bdev = s->origin->bdev;
927 dev_size = get_dev_size(bdev);
930 src.sector = chunk_to_sector(s, pe->e.old_chunk);
931 src.count = min(s->chunk_size, dev_size - src.sector);
933 dest.bdev = s->cow->bdev;
934 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
935 dest.count = src.count;
937 /* Hand over to kcopyd */
938 dm_kcopyd_copy(s->kcopyd_client,
939 &src, 1, &dest, 0, copy_callback, pe);
943 * Looks to see if this snapshot already has a pending exception
944 * for this chunk, otherwise it allocates a new one and inserts
945 * it into the pending table.
947 * NOTE: a write lock must be held on snap->lock before calling
950 static struct dm_snap_pending_exception *
951 __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
953 struct dm_snap_exception *e;
954 struct dm_snap_pending_exception *pe;
955 chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
958 * Is there a pending exception for this already ?
960 e = lookup_exception(&s->pending, chunk);
962 /* cast the exception to a pending exception */
963 pe = container_of(e, struct dm_snap_pending_exception, e);
968 * Create a new pending exception, we don't want
969 * to hold the lock while we do this.
972 pe = alloc_pending_exception();
973 down_write(&s->lock);
976 free_pending_exception(pe);
980 e = lookup_exception(&s->pending, chunk);
982 free_pending_exception(pe);
983 pe = container_of(e, struct dm_snap_pending_exception, e);
987 pe->e.old_chunk = chunk;
988 bio_list_init(&pe->origin_bios);
989 bio_list_init(&pe->snapshot_bios);
990 pe->primary_pe = NULL;
991 atomic_set(&pe->ref_count, 0);
995 if (s->store.prepare_exception(&s->store, &pe->e)) {
996 free_pending_exception(pe);
1000 get_pending_exception(pe);
1001 insert_exception(&s->pending, &pe->e);
1007 static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
1008 struct bio *bio, chunk_t chunk)
1010 bio->bi_bdev = s->cow->bdev;
1011 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
1012 (chunk - e->old_chunk)) +
1013 (bio->bi_sector & s->chunk_mask);
1016 static int snapshot_map(struct dm_target *ti, struct bio *bio,
1017 union map_info *map_context)
1019 struct dm_snap_exception *e;
1020 struct dm_snapshot *s = ti->private;
1021 int r = DM_MAPIO_REMAPPED;
1023 struct dm_snap_pending_exception *pe = NULL;
1025 chunk = sector_to_chunk(s, bio->bi_sector);
1027 /* Full snapshots are not usable */
1028 /* To get here the table must be live so s->active is always set. */
1032 /* FIXME: should only take write lock if we need
1033 * to copy an exception */
1034 down_write(&s->lock);
1041 /* If the block is already remapped - use that, else remap it */
1042 e = lookup_exception(&s->complete, chunk);
1044 remap_exception(s, e, bio, chunk);
1049 * Write to snapshot - higher level takes care of RW/RO
1050 * flags so we should only get this if we are
1053 if (bio_rw(bio) == WRITE) {
1054 pe = __find_pending_exception(s, bio);
1056 __invalidate_snapshot(s, -ENOMEM);
1061 remap_exception(s, &pe->e, bio, chunk);
1062 bio_list_add(&pe->snapshot_bios, bio);
1064 r = DM_MAPIO_SUBMITTED;
1067 /* this is protected by snap->lock */
1074 bio->bi_bdev = s->origin->bdev;
1075 map_context->ptr = track_chunk(s, chunk);
1084 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1085 int error, union map_info *map_context)
1087 struct dm_snapshot *s = ti->private;
1088 struct dm_snap_tracked_chunk *c = map_context->ptr;
1091 stop_tracking_chunk(s, c);
1096 static void snapshot_resume(struct dm_target *ti)
1098 struct dm_snapshot *s = ti->private;
1100 down_write(&s->lock);
1105 static int snapshot_status(struct dm_target *ti, status_type_t type,
1106 char *result, unsigned int maxlen)
1108 struct dm_snapshot *snap = ti->private;
1111 case STATUSTYPE_INFO:
1113 snprintf(result, maxlen, "Invalid");
1115 if (snap->store.fraction_full) {
1116 sector_t numerator, denominator;
1117 snap->store.fraction_full(&snap->store,
1120 snprintf(result, maxlen, "%llu/%llu",
1121 (unsigned long long)numerator,
1122 (unsigned long long)denominator);
1125 snprintf(result, maxlen, "Unknown");
1129 case STATUSTYPE_TABLE:
1131 * kdevname returns a static pointer so we need
1132 * to make private copies if the output is to
1135 snprintf(result, maxlen, "%s %s %c %llu",
1136 snap->origin->name, snap->cow->name,
1138 (unsigned long long)snap->chunk_size);
1145 /*-----------------------------------------------------------------
1147 *---------------------------------------------------------------*/
1148 static int __origin_write(struct list_head *snapshots, struct bio *bio)
1150 int r = DM_MAPIO_REMAPPED, first = 0;
1151 struct dm_snapshot *snap;
1152 struct dm_snap_exception *e;
1153 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
1155 LIST_HEAD(pe_queue);
1157 /* Do all the snapshots on this origin */
1158 list_for_each_entry (snap, snapshots, list) {
1160 down_write(&snap->lock);
1162 /* Only deal with valid and active snapshots */
1163 if (!snap->valid || !snap->active)
1166 /* Nothing to do if writing beyond end of snapshot */
1167 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
1171 * Remember, different snapshots can have
1172 * different chunk sizes.
1174 chunk = sector_to_chunk(snap, bio->bi_sector);
1177 * Check exception table to see if block
1178 * is already remapped in this snapshot
1179 * and trigger an exception if not.
1181 * ref_count is initialised to 1 so pending_complete()
1182 * won't destroy the primary_pe while we're inside this loop.
1184 e = lookup_exception(&snap->complete, chunk);
1188 pe = __find_pending_exception(snap, bio);
1190 __invalidate_snapshot(snap, -ENOMEM);
1196 * Either every pe here has same
1197 * primary_pe or none has one yet.
1200 primary_pe = pe->primary_pe;
1206 bio_list_add(&primary_pe->origin_bios, bio);
1208 r = DM_MAPIO_SUBMITTED;
1211 if (!pe->primary_pe) {
1212 pe->primary_pe = primary_pe;
1213 get_pending_exception(primary_pe);
1218 list_add_tail(&pe->list, &pe_queue);
1222 up_write(&snap->lock);
1229 * If this is the first time we're processing this chunk and
1230 * ref_count is now 1 it means all the pending exceptions
1231 * got completed while we were in the loop above, so it falls to
1232 * us here to remove the primary_pe and submit any origin_bios.
1235 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
1236 flush_bios(bio_list_get(&primary_pe->origin_bios));
1237 free_pending_exception(primary_pe);
1238 /* If we got here, pe_queue is necessarily empty. */
1243 * Now that we have a complete pe list we can start the copying.
1245 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1252 * Called on a write from the origin driver.
1254 static int do_origin(struct dm_dev *origin, struct bio *bio)
1257 int r = DM_MAPIO_REMAPPED;
1259 down_read(&_origins_lock);
1260 o = __lookup_origin(origin->bdev);
1262 r = __origin_write(&o->snapshots, bio);
1263 up_read(&_origins_lock);
1269 * Origin: maps a linear range of a device, with hooks for snapshotting.
1273 * Construct an origin mapping: <dev_path>
1274 * The context for an origin is merely a 'struct dm_dev *'
1275 * pointing to the real device.
1277 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1283 ti->error = "origin: incorrect number of arguments";
1287 r = dm_get_device(ti, argv[0], 0, ti->len,
1288 dm_table_get_mode(ti->table), &dev);
1290 ti->error = "Cannot get target device";
1298 static void origin_dtr(struct dm_target *ti)
1300 struct dm_dev *dev = ti->private;
1301 dm_put_device(ti, dev);
1304 static int origin_map(struct dm_target *ti, struct bio *bio,
1305 union map_info *map_context)
1307 struct dm_dev *dev = ti->private;
1308 bio->bi_bdev = dev->bdev;
1310 /* Only tell snapshots if this is a write */
1311 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
1314 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1317 * Set the target "split_io" field to the minimum of all the snapshots'
1320 static void origin_resume(struct dm_target *ti)
1322 struct dm_dev *dev = ti->private;
1323 struct dm_snapshot *snap;
1325 chunk_t chunk_size = 0;
1327 down_read(&_origins_lock);
1328 o = __lookup_origin(dev->bdev);
1330 list_for_each_entry (snap, &o->snapshots, list)
1331 chunk_size = min_not_zero(chunk_size, snap->chunk_size);
1332 up_read(&_origins_lock);
1334 ti->split_io = chunk_size;
1337 static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1338 unsigned int maxlen)
1340 struct dm_dev *dev = ti->private;
1343 case STATUSTYPE_INFO:
1347 case STATUSTYPE_TABLE:
1348 snprintf(result, maxlen, "%s", dev->name);
1355 static struct target_type origin_target = {
1356 .name = "snapshot-origin",
1357 .version = {1, 6, 0},
1358 .module = THIS_MODULE,
1362 .resume = origin_resume,
1363 .status = origin_status,
1366 static struct target_type snapshot_target = {
1368 .version = {1, 6, 0},
1369 .module = THIS_MODULE,
1370 .ctr = snapshot_ctr,
1371 .dtr = snapshot_dtr,
1372 .map = snapshot_map,
1373 .end_io = snapshot_end_io,
1374 .resume = snapshot_resume,
1375 .status = snapshot_status,
1378 static int __init dm_snapshot_init(void)
1382 r = dm_register_target(&snapshot_target);
1384 DMERR("snapshot target register failed %d", r);
1388 r = dm_register_target(&origin_target);
1390 DMERR("Origin target register failed %d", r);
1394 r = init_origin_hash();
1396 DMERR("init_origin_hash failed.");
1400 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
1401 if (!exception_cache) {
1402 DMERR("Couldn't create exception cache.");
1407 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
1408 if (!pending_cache) {
1409 DMERR("Couldn't create pending cache.");
1414 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1415 if (!tracked_chunk_cache) {
1416 DMERR("Couldn't create cache to track chunks in use.");
1421 pending_pool = mempool_create_slab_pool(128, pending_cache);
1422 if (!pending_pool) {
1423 DMERR("Couldn't create pending pool.");
1425 goto bad_pending_pool;
1428 ksnapd = create_singlethread_workqueue("ksnapd");
1430 DMERR("Failed to create ksnapd workqueue.");
1438 mempool_destroy(pending_pool);
1440 kmem_cache_destroy(tracked_chunk_cache);
1442 kmem_cache_destroy(pending_cache);
1444 kmem_cache_destroy(exception_cache);
1448 dm_unregister_target(&origin_target);
1450 dm_unregister_target(&snapshot_target);
1454 static void __exit dm_snapshot_exit(void)
1458 destroy_workqueue(ksnapd);
1460 r = dm_unregister_target(&snapshot_target);
1462 DMERR("snapshot unregister failed %d", r);
1464 r = dm_unregister_target(&origin_target);
1466 DMERR("origin unregister failed %d", r);
1469 mempool_destroy(pending_pool);
1470 kmem_cache_destroy(pending_cache);
1471 kmem_cache_destroy(exception_cache);
1472 kmem_cache_destroy(tracked_chunk_cache);
1476 module_init(dm_snapshot_init);
1477 module_exit(dm_snapshot_exit);
1479 MODULE_DESCRIPTION(DM_NAME " snapshot target");
1480 MODULE_AUTHOR("Joe Thornber");
1481 MODULE_LICENSE("GPL");