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)
233 struct block_device *bdev = snap->origin->bdev;
235 down_write(&_origins_lock);
236 o = __lookup_origin(bdev);
240 o = kmalloc(sizeof(*o), GFP_KERNEL);
242 up_write(&_origins_lock);
246 /* Initialise the struct */
247 INIT_LIST_HEAD(&o->snapshots);
253 list_add_tail(&snap->list, &o->snapshots);
255 up_write(&_origins_lock);
259 static void unregister_snapshot(struct dm_snapshot *s)
263 down_write(&_origins_lock);
264 o = __lookup_origin(s->origin->bdev);
267 if (list_empty(&o->snapshots)) {
268 list_del(&o->hash_list);
272 up_write(&_origins_lock);
276 * Implementation of the exception hash tables.
277 * The lowest hash_shift bits of the chunk number are ignored, allowing
278 * some consecutive chunks to be grouped together.
280 static int init_exception_table(struct exception_table *et, uint32_t size,
285 et->hash_shift = hash_shift;
286 et->hash_mask = size - 1;
287 et->table = dm_vcalloc(size, sizeof(struct list_head));
291 for (i = 0; i < size; i++)
292 INIT_LIST_HEAD(et->table + i);
297 static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
299 struct list_head *slot;
300 struct dm_snap_exception *ex, *next;
303 size = et->hash_mask + 1;
304 for (i = 0; i < size; i++) {
305 slot = et->table + i;
307 list_for_each_entry_safe (ex, next, slot, hash_list)
308 kmem_cache_free(mem, ex);
314 static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
316 return (chunk >> et->hash_shift) & et->hash_mask;
319 static void insert_exception(struct exception_table *eh,
320 struct dm_snap_exception *e)
322 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
323 list_add(&e->hash_list, l);
326 static void remove_exception(struct dm_snap_exception *e)
328 list_del(&e->hash_list);
332 * Return the exception data for a sector, or NULL if not
335 static struct dm_snap_exception *lookup_exception(struct exception_table *et,
338 struct list_head *slot;
339 struct dm_snap_exception *e;
341 slot = &et->table[exception_hash(et, chunk)];
342 list_for_each_entry (e, slot, hash_list)
343 if (chunk >= e->old_chunk &&
344 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
350 static struct dm_snap_exception *alloc_exception(void)
352 struct dm_snap_exception *e;
354 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
356 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
361 static void free_exception(struct dm_snap_exception *e)
363 kmem_cache_free(exception_cache, e);
366 static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
368 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
376 static void free_pending_exception(struct dm_snap_pending_exception *pe)
378 mempool_free(pe, pe->snap->pending_pool);
381 static void insert_completed_exception(struct dm_snapshot *s,
382 struct dm_snap_exception *new_e)
384 struct exception_table *eh = &s->complete;
386 struct dm_snap_exception *e = NULL;
388 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
390 /* Add immediately if this table doesn't support consecutive chunks */
394 /* List is ordered by old_chunk */
395 list_for_each_entry_reverse(e, l, hash_list) {
396 /* Insert after an existing chunk? */
397 if (new_e->old_chunk == (e->old_chunk +
398 dm_consecutive_chunk_count(e) + 1) &&
399 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
400 dm_consecutive_chunk_count(e) + 1)) {
401 dm_consecutive_chunk_count_inc(e);
402 free_exception(new_e);
406 /* Insert before an existing chunk? */
407 if (new_e->old_chunk == (e->old_chunk - 1) &&
408 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
409 dm_consecutive_chunk_count_inc(e);
412 free_exception(new_e);
416 if (new_e->old_chunk > e->old_chunk)
421 list_add(&new_e->hash_list, e ? &e->hash_list : l);
424 int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
426 struct dm_snap_exception *e;
428 e = alloc_exception();
434 /* Consecutive_count is implicitly initialised to zero */
437 insert_completed_exception(s, e);
445 static int calc_max_buckets(void)
447 /* use a fixed size of 2MB */
448 unsigned long mem = 2 * 1024 * 1024;
449 mem /= sizeof(struct list_head);
455 * Allocate room for a suitable hash table.
457 static int init_hash_tables(struct dm_snapshot *s)
459 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
462 * Calculate based on the size of the original volume or
465 cow_dev_size = get_dev_size(s->cow->bdev);
466 origin_dev_size = get_dev_size(s->origin->bdev);
467 max_buckets = calc_max_buckets();
469 hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
470 hash_size = min(hash_size, max_buckets);
472 hash_size = rounddown_pow_of_two(hash_size);
473 if (init_exception_table(&s->complete, hash_size,
474 DM_CHUNK_CONSECUTIVE_BITS))
478 * Allocate hash table for in-flight exceptions
479 * Make this smaller than the real hash table
485 if (init_exception_table(&s->pending, hash_size, 0)) {
486 exit_exception_table(&s->complete, exception_cache);
494 * Round a number up to the nearest 'size' boundary. size must
497 static ulong round_up(ulong n, ulong size)
500 return (n + size) & ~size;
503 static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
506 unsigned long chunk_size;
509 chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
510 if (*chunk_size_arg == '\0' || *value != '\0') {
511 *error = "Invalid chunk size";
516 s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
521 * Chunk size must be multiple of page size. Silently
522 * round up if it's not.
524 chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
526 /* Check chunk_size is a power of 2 */
527 if (!is_power_of_2(chunk_size)) {
528 *error = "Chunk size is not a power of 2";
532 /* Validate the chunk size against the device block size */
533 if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
534 *error = "Chunk size is not a multiple of device blocksize";
538 s->chunk_size = chunk_size;
539 s->chunk_mask = chunk_size - 1;
540 s->chunk_shift = ffs(chunk_size) - 1;
546 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
548 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
550 struct dm_snapshot *s;
558 ti->error = "requires exactly 4 arguments";
563 origin_path = argv[0];
565 persistent = toupper(*argv[2]);
567 if (persistent != 'P' && persistent != 'N') {
568 ti->error = "Persistent flag is not P or N";
573 s = kmalloc(sizeof(*s), GFP_KERNEL);
575 ti->error = "Cannot allocate snapshot context private "
581 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
583 ti->error = "Cannot get origin device";
587 r = dm_get_device(ti, cow_path, 0, 0,
588 FMODE_READ | FMODE_WRITE, &s->cow);
590 dm_put_device(ti, s->origin);
591 ti->error = "Cannot get COW device";
595 r = set_chunk_size(s, argv[3], &ti->error);
599 s->type = persistent;
603 init_rwsem(&s->lock);
604 spin_lock_init(&s->pe_lock);
607 /* Allocate hash table for COW data */
608 if (init_hash_tables(s)) {
609 ti->error = "Unable to allocate hash table space";
616 if (persistent == 'P')
617 r = dm_create_persistent(&s->store);
619 r = dm_create_transient(&s->store);
622 ti->error = "Couldn't create exception store";
627 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
629 ti->error = "Could not create kcopyd client";
633 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
634 if (!s->pending_pool) {
635 ti->error = "Could not allocate mempool for pending exceptions";
639 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
640 tracked_chunk_cache);
641 if (!s->tracked_chunk_pool) {
642 ti->error = "Could not allocate tracked_chunk mempool for "
644 goto bad_tracked_chunk_pool;
647 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
648 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
650 spin_lock_init(&s->tracked_chunk_lock);
652 /* Metadata must only be loaded into one table at once */
653 r = s->store.read_metadata(&s->store);
655 ti->error = "Failed to read snapshot metadata";
656 goto bad_load_and_register;
659 DMWARN("Snapshot is marked invalid.");
662 bio_list_init(&s->queued_bios);
663 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
665 /* Add snapshot to the list of snapshots for this origin */
666 /* Exceptions aren't triggered till snapshot_resume() is called */
667 if (register_snapshot(s)) {
669 ti->error = "Cannot register snapshot origin";
670 goto bad_load_and_register;
674 ti->split_io = s->chunk_size;
678 bad_load_and_register:
679 mempool_destroy(s->tracked_chunk_pool);
681 bad_tracked_chunk_pool:
682 mempool_destroy(s->pending_pool);
685 dm_kcopyd_client_destroy(s->kcopyd_client);
688 s->store.destroy(&s->store);
691 exit_exception_table(&s->pending, pending_cache);
692 exit_exception_table(&s->complete, exception_cache);
695 dm_put_device(ti, s->cow);
696 dm_put_device(ti, s->origin);
705 static void __free_exceptions(struct dm_snapshot *s)
707 dm_kcopyd_client_destroy(s->kcopyd_client);
708 s->kcopyd_client = NULL;
710 exit_exception_table(&s->pending, pending_cache);
711 exit_exception_table(&s->complete, exception_cache);
713 s->store.destroy(&s->store);
716 static void snapshot_dtr(struct dm_target *ti)
718 #ifdef CONFIG_DM_DEBUG
721 struct dm_snapshot *s = ti->private;
723 flush_workqueue(ksnapd);
725 /* Prevent further origin writes from using this snapshot. */
726 /* After this returns there can be no new kcopyd jobs. */
727 unregister_snapshot(s);
729 #ifdef CONFIG_DM_DEBUG
730 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
731 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
734 mempool_destroy(s->tracked_chunk_pool);
736 __free_exceptions(s);
738 mempool_destroy(s->pending_pool);
740 dm_put_device(ti, s->origin);
741 dm_put_device(ti, s->cow);
747 * Flush a list of buffers.
749 static void flush_bios(struct bio *bio)
756 generic_make_request(bio);
761 static void flush_queued_bios(struct work_struct *work)
763 struct dm_snapshot *s =
764 container_of(work, struct dm_snapshot, queued_bios_work);
765 struct bio *queued_bios;
768 spin_lock_irqsave(&s->pe_lock, flags);
769 queued_bios = bio_list_get(&s->queued_bios);
770 spin_unlock_irqrestore(&s->pe_lock, flags);
772 flush_bios(queued_bios);
776 * Error a list of buffers.
778 static void error_bios(struct bio *bio)
790 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
796 DMERR("Invalidating snapshot: Error reading/writing.");
797 else if (err == -ENOMEM)
798 DMERR("Invalidating snapshot: Unable to allocate exception.");
800 if (s->store.drop_snapshot)
801 s->store.drop_snapshot(&s->store);
805 dm_table_event(s->ti->table);
808 static void get_pending_exception(struct dm_snap_pending_exception *pe)
810 atomic_inc(&pe->ref_count);
813 static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
815 struct dm_snap_pending_exception *primary_pe;
816 struct bio *origin_bios = NULL;
818 primary_pe = pe->primary_pe;
821 * If this pe is involved in a write to the origin and
822 * it is the last sibling to complete then release
823 * the bios for the original write to the origin.
826 atomic_dec_and_test(&primary_pe->ref_count)) {
827 origin_bios = bio_list_get(&primary_pe->origin_bios);
828 free_pending_exception(primary_pe);
832 * Free the pe if it's not linked to an origin write or if
833 * it's not itself a primary pe.
835 if (!primary_pe || primary_pe != pe)
836 free_pending_exception(pe);
841 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
843 struct dm_snap_exception *e;
844 struct dm_snapshot *s = pe->snap;
845 struct bio *origin_bios = NULL;
846 struct bio *snapshot_bios = NULL;
850 /* Read/write error - snapshot is unusable */
851 down_write(&s->lock);
852 __invalidate_snapshot(s, -EIO);
857 e = alloc_exception();
859 down_write(&s->lock);
860 __invalidate_snapshot(s, -ENOMEM);
866 down_write(&s->lock);
874 * Check for conflicting reads. This is extremely improbable,
875 * so yield() is sufficient and there is no need for a wait queue.
877 while (__chunk_is_tracked(s, pe->e.old_chunk))
881 * Add a proper exception, and remove the
882 * in-flight exception from the list.
884 insert_completed_exception(s, e);
887 remove_exception(&pe->e);
888 snapshot_bios = bio_list_get(&pe->snapshot_bios);
889 origin_bios = put_pending_exception(pe);
893 /* Submit any pending write bios */
895 error_bios(snapshot_bios);
897 flush_bios(snapshot_bios);
899 flush_bios(origin_bios);
902 static void commit_callback(void *context, int success)
904 struct dm_snap_pending_exception *pe = context;
906 pending_complete(pe, success);
910 * Called when the copy I/O has finished. kcopyd actually runs
911 * this code so don't block.
913 static void copy_callback(int read_err, unsigned long write_err, void *context)
915 struct dm_snap_pending_exception *pe = context;
916 struct dm_snapshot *s = pe->snap;
918 if (read_err || write_err)
919 pending_complete(pe, 0);
922 /* Update the metadata if we are persistent */
923 s->store.commit_exception(&s->store, &pe->e, commit_callback,
928 * Dispatches the copy operation to kcopyd.
930 static void start_copy(struct dm_snap_pending_exception *pe)
932 struct dm_snapshot *s = pe->snap;
933 struct dm_io_region src, dest;
934 struct block_device *bdev = s->origin->bdev;
937 dev_size = get_dev_size(bdev);
940 src.sector = chunk_to_sector(s, pe->e.old_chunk);
941 src.count = min(s->chunk_size, dev_size - src.sector);
943 dest.bdev = s->cow->bdev;
944 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
945 dest.count = src.count;
947 /* Hand over to kcopyd */
948 dm_kcopyd_copy(s->kcopyd_client,
949 &src, 1, &dest, 0, copy_callback, pe);
953 * Looks to see if this snapshot already has a pending exception
954 * for this chunk, otherwise it allocates a new one and inserts
955 * it into the pending table.
957 * NOTE: a write lock must be held on snap->lock before calling
960 static struct dm_snap_pending_exception *
961 __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
963 struct dm_snap_exception *e;
964 struct dm_snap_pending_exception *pe;
965 chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
968 * Is there a pending exception for this already ?
970 e = lookup_exception(&s->pending, chunk);
972 /* cast the exception to a pending exception */
973 pe = container_of(e, struct dm_snap_pending_exception, e);
978 * Create a new pending exception, we don't want
979 * to hold the lock while we do this.
982 pe = alloc_pending_exception(s);
983 down_write(&s->lock);
986 free_pending_exception(pe);
990 e = lookup_exception(&s->pending, chunk);
992 free_pending_exception(pe);
993 pe = container_of(e, struct dm_snap_pending_exception, e);
997 pe->e.old_chunk = chunk;
998 bio_list_init(&pe->origin_bios);
999 bio_list_init(&pe->snapshot_bios);
1000 pe->primary_pe = NULL;
1001 atomic_set(&pe->ref_count, 0);
1004 if (s->store.prepare_exception(&s->store, &pe->e)) {
1005 free_pending_exception(pe);
1009 get_pending_exception(pe);
1010 insert_exception(&s->pending, &pe->e);
1016 static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
1017 struct bio *bio, chunk_t chunk)
1019 bio->bi_bdev = s->cow->bdev;
1020 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
1021 (chunk - e->old_chunk)) +
1022 (bio->bi_sector & s->chunk_mask);
1025 static int snapshot_map(struct dm_target *ti, struct bio *bio,
1026 union map_info *map_context)
1028 struct dm_snap_exception *e;
1029 struct dm_snapshot *s = ti->private;
1030 int r = DM_MAPIO_REMAPPED;
1032 struct dm_snap_pending_exception *pe = NULL;
1034 chunk = sector_to_chunk(s, bio->bi_sector);
1036 /* Full snapshots are not usable */
1037 /* To get here the table must be live so s->active is always set. */
1041 /* FIXME: should only take write lock if we need
1042 * to copy an exception */
1043 down_write(&s->lock);
1050 /* If the block is already remapped - use that, else remap it */
1051 e = lookup_exception(&s->complete, chunk);
1053 remap_exception(s, e, bio, chunk);
1058 * Write to snapshot - higher level takes care of RW/RO
1059 * flags so we should only get this if we are
1062 if (bio_rw(bio) == WRITE) {
1063 pe = __find_pending_exception(s, bio);
1065 __invalidate_snapshot(s, -ENOMEM);
1070 remap_exception(s, &pe->e, bio, chunk);
1071 bio_list_add(&pe->snapshot_bios, bio);
1073 r = DM_MAPIO_SUBMITTED;
1076 /* this is protected by snap->lock */
1083 bio->bi_bdev = s->origin->bdev;
1084 map_context->ptr = track_chunk(s, chunk);
1093 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1094 int error, union map_info *map_context)
1096 struct dm_snapshot *s = ti->private;
1097 struct dm_snap_tracked_chunk *c = map_context->ptr;
1100 stop_tracking_chunk(s, c);
1105 static void snapshot_resume(struct dm_target *ti)
1107 struct dm_snapshot *s = ti->private;
1109 down_write(&s->lock);
1114 static int snapshot_status(struct dm_target *ti, status_type_t type,
1115 char *result, unsigned int maxlen)
1117 struct dm_snapshot *snap = ti->private;
1120 case STATUSTYPE_INFO:
1122 snprintf(result, maxlen, "Invalid");
1124 if (snap->store.fraction_full) {
1125 sector_t numerator, denominator;
1126 snap->store.fraction_full(&snap->store,
1129 snprintf(result, maxlen, "%llu/%llu",
1130 (unsigned long long)numerator,
1131 (unsigned long long)denominator);
1134 snprintf(result, maxlen, "Unknown");
1138 case STATUSTYPE_TABLE:
1140 * kdevname returns a static pointer so we need
1141 * to make private copies if the output is to
1144 snprintf(result, maxlen, "%s %s %c %llu",
1145 snap->origin->name, snap->cow->name,
1147 (unsigned long long)snap->chunk_size);
1154 /*-----------------------------------------------------------------
1156 *---------------------------------------------------------------*/
1157 static int __origin_write(struct list_head *snapshots, struct bio *bio)
1159 int r = DM_MAPIO_REMAPPED, first = 0;
1160 struct dm_snapshot *snap;
1161 struct dm_snap_exception *e;
1162 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
1164 LIST_HEAD(pe_queue);
1166 /* Do all the snapshots on this origin */
1167 list_for_each_entry (snap, snapshots, list) {
1169 down_write(&snap->lock);
1171 /* Only deal with valid and active snapshots */
1172 if (!snap->valid || !snap->active)
1175 /* Nothing to do if writing beyond end of snapshot */
1176 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
1180 * Remember, different snapshots can have
1181 * different chunk sizes.
1183 chunk = sector_to_chunk(snap, bio->bi_sector);
1186 * Check exception table to see if block
1187 * is already remapped in this snapshot
1188 * and trigger an exception if not.
1190 * ref_count is initialised to 1 so pending_complete()
1191 * won't destroy the primary_pe while we're inside this loop.
1193 e = lookup_exception(&snap->complete, chunk);
1197 pe = __find_pending_exception(snap, bio);
1199 __invalidate_snapshot(snap, -ENOMEM);
1205 * Either every pe here has same
1206 * primary_pe or none has one yet.
1209 primary_pe = pe->primary_pe;
1215 bio_list_add(&primary_pe->origin_bios, bio);
1217 r = DM_MAPIO_SUBMITTED;
1220 if (!pe->primary_pe) {
1221 pe->primary_pe = primary_pe;
1222 get_pending_exception(primary_pe);
1227 list_add_tail(&pe->list, &pe_queue);
1231 up_write(&snap->lock);
1238 * If this is the first time we're processing this chunk and
1239 * ref_count is now 1 it means all the pending exceptions
1240 * got completed while we were in the loop above, so it falls to
1241 * us here to remove the primary_pe and submit any origin_bios.
1244 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
1245 flush_bios(bio_list_get(&primary_pe->origin_bios));
1246 free_pending_exception(primary_pe);
1247 /* If we got here, pe_queue is necessarily empty. */
1252 * Now that we have a complete pe list we can start the copying.
1254 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1261 * Called on a write from the origin driver.
1263 static int do_origin(struct dm_dev *origin, struct bio *bio)
1266 int r = DM_MAPIO_REMAPPED;
1268 down_read(&_origins_lock);
1269 o = __lookup_origin(origin->bdev);
1271 r = __origin_write(&o->snapshots, bio);
1272 up_read(&_origins_lock);
1278 * Origin: maps a linear range of a device, with hooks for snapshotting.
1282 * Construct an origin mapping: <dev_path>
1283 * The context for an origin is merely a 'struct dm_dev *'
1284 * pointing to the real device.
1286 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1292 ti->error = "origin: incorrect number of arguments";
1296 r = dm_get_device(ti, argv[0], 0, ti->len,
1297 dm_table_get_mode(ti->table), &dev);
1299 ti->error = "Cannot get target device";
1307 static void origin_dtr(struct dm_target *ti)
1309 struct dm_dev *dev = ti->private;
1310 dm_put_device(ti, dev);
1313 static int origin_map(struct dm_target *ti, struct bio *bio,
1314 union map_info *map_context)
1316 struct dm_dev *dev = ti->private;
1317 bio->bi_bdev = dev->bdev;
1319 /* Only tell snapshots if this is a write */
1320 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
1323 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1326 * Set the target "split_io" field to the minimum of all the snapshots'
1329 static void origin_resume(struct dm_target *ti)
1331 struct dm_dev *dev = ti->private;
1332 struct dm_snapshot *snap;
1334 chunk_t chunk_size = 0;
1336 down_read(&_origins_lock);
1337 o = __lookup_origin(dev->bdev);
1339 list_for_each_entry (snap, &o->snapshots, list)
1340 chunk_size = min_not_zero(chunk_size, snap->chunk_size);
1341 up_read(&_origins_lock);
1343 ti->split_io = chunk_size;
1346 static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1347 unsigned int maxlen)
1349 struct dm_dev *dev = ti->private;
1352 case STATUSTYPE_INFO:
1356 case STATUSTYPE_TABLE:
1357 snprintf(result, maxlen, "%s", dev->name);
1364 static struct target_type origin_target = {
1365 .name = "snapshot-origin",
1366 .version = {1, 6, 0},
1367 .module = THIS_MODULE,
1371 .resume = origin_resume,
1372 .status = origin_status,
1375 static struct target_type snapshot_target = {
1377 .version = {1, 6, 0},
1378 .module = THIS_MODULE,
1379 .ctr = snapshot_ctr,
1380 .dtr = snapshot_dtr,
1381 .map = snapshot_map,
1382 .end_io = snapshot_end_io,
1383 .resume = snapshot_resume,
1384 .status = snapshot_status,
1387 static int __init dm_snapshot_init(void)
1391 r = dm_register_target(&snapshot_target);
1393 DMERR("snapshot target register failed %d", r);
1397 r = dm_register_target(&origin_target);
1399 DMERR("Origin target register failed %d", r);
1403 r = init_origin_hash();
1405 DMERR("init_origin_hash failed.");
1409 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
1410 if (!exception_cache) {
1411 DMERR("Couldn't create exception cache.");
1416 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
1417 if (!pending_cache) {
1418 DMERR("Couldn't create pending cache.");
1423 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1424 if (!tracked_chunk_cache) {
1425 DMERR("Couldn't create cache to track chunks in use.");
1430 ksnapd = create_singlethread_workqueue("ksnapd");
1432 DMERR("Failed to create ksnapd workqueue.");
1434 goto bad_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 kmem_cache_destroy(pending_cache);
1470 kmem_cache_destroy(exception_cache);
1471 kmem_cache_destroy(tracked_chunk_cache);
1475 module_init(dm_snapshot_init);
1476 module_exit(dm_snapshot_exit);
1478 MODULE_DESCRIPTION(DM_NAME " snapshot target");
1479 MODULE_AUTHOR("Joe Thornber");
1480 MODULE_LICENSE("GPL");