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);
138 * One of these per registered origin, held in the snapshot_origins hash
141 /* The origin device */
142 struct block_device *bdev;
144 struct list_head hash_list;
146 /* List of snapshots for this origin */
147 struct list_head snapshots;
151 * Size of the hash table for origin volumes. If we make this
152 * the size of the minors list then it should be nearly perfect
154 #define ORIGIN_HASH_SIZE 256
155 #define ORIGIN_MASK 0xFF
156 static struct list_head *_origins;
157 static struct rw_semaphore _origins_lock;
159 static int init_origin_hash(void)
163 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
166 DMERR("unable to allocate memory");
170 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
171 INIT_LIST_HEAD(_origins + i);
172 init_rwsem(&_origins_lock);
177 static void exit_origin_hash(void)
182 static unsigned origin_hash(struct block_device *bdev)
184 return bdev->bd_dev & ORIGIN_MASK;
187 static struct origin *__lookup_origin(struct block_device *origin)
189 struct list_head *ol;
192 ol = &_origins[origin_hash(origin)];
193 list_for_each_entry (o, ol, hash_list)
194 if (bdev_equal(o->bdev, origin))
200 static void __insert_origin(struct origin *o)
202 struct list_head *sl = &_origins[origin_hash(o->bdev)];
203 list_add_tail(&o->hash_list, sl);
207 * Make a note of the snapshot and its origin so we can look it
208 * up when the origin has a write on it.
210 static int register_snapshot(struct dm_snapshot *snap)
213 struct block_device *bdev = snap->origin->bdev;
215 down_write(&_origins_lock);
216 o = __lookup_origin(bdev);
220 o = kmalloc(sizeof(*o), GFP_KERNEL);
222 up_write(&_origins_lock);
226 /* Initialise the struct */
227 INIT_LIST_HEAD(&o->snapshots);
233 list_add_tail(&snap->list, &o->snapshots);
235 up_write(&_origins_lock);
239 static void unregister_snapshot(struct dm_snapshot *s)
243 down_write(&_origins_lock);
244 o = __lookup_origin(s->origin->bdev);
247 if (list_empty(&o->snapshots)) {
248 list_del(&o->hash_list);
252 up_write(&_origins_lock);
256 * Implementation of the exception hash tables.
257 * The lowest hash_shift bits of the chunk number are ignored, allowing
258 * some consecutive chunks to be grouped together.
260 static int init_exception_table(struct exception_table *et, uint32_t size,
265 et->hash_shift = hash_shift;
266 et->hash_mask = size - 1;
267 et->table = dm_vcalloc(size, sizeof(struct list_head));
271 for (i = 0; i < size; i++)
272 INIT_LIST_HEAD(et->table + i);
277 static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
279 struct list_head *slot;
280 struct dm_snap_exception *ex, *next;
283 size = et->hash_mask + 1;
284 for (i = 0; i < size; i++) {
285 slot = et->table + i;
287 list_for_each_entry_safe (ex, next, slot, hash_list)
288 kmem_cache_free(mem, ex);
294 static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
296 return (chunk >> et->hash_shift) & et->hash_mask;
299 static void insert_exception(struct exception_table *eh,
300 struct dm_snap_exception *e)
302 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
303 list_add(&e->hash_list, l);
306 static void remove_exception(struct dm_snap_exception *e)
308 list_del(&e->hash_list);
312 * Return the exception data for a sector, or NULL if not
315 static struct dm_snap_exception *lookup_exception(struct exception_table *et,
318 struct list_head *slot;
319 struct dm_snap_exception *e;
321 slot = &et->table[exception_hash(et, chunk)];
322 list_for_each_entry (e, slot, hash_list)
323 if (chunk >= e->old_chunk &&
324 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
330 static struct dm_snap_exception *alloc_exception(void)
332 struct dm_snap_exception *e;
334 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
336 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
341 static void free_exception(struct dm_snap_exception *e)
343 kmem_cache_free(exception_cache, e);
346 static struct dm_snap_pending_exception *alloc_pending_exception(void)
348 return mempool_alloc(pending_pool, GFP_NOIO);
351 static void free_pending_exception(struct dm_snap_pending_exception *pe)
353 mempool_free(pe, pending_pool);
356 static void insert_completed_exception(struct dm_snapshot *s,
357 struct dm_snap_exception *new_e)
359 struct exception_table *eh = &s->complete;
361 struct dm_snap_exception *e = NULL;
363 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
365 /* Add immediately if this table doesn't support consecutive chunks */
369 /* List is ordered by old_chunk */
370 list_for_each_entry_reverse(e, l, hash_list) {
371 /* Insert after an existing chunk? */
372 if (new_e->old_chunk == (e->old_chunk +
373 dm_consecutive_chunk_count(e) + 1) &&
374 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
375 dm_consecutive_chunk_count(e) + 1)) {
376 dm_consecutive_chunk_count_inc(e);
377 free_exception(new_e);
381 /* Insert before an existing chunk? */
382 if (new_e->old_chunk == (e->old_chunk - 1) &&
383 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
384 dm_consecutive_chunk_count_inc(e);
387 free_exception(new_e);
391 if (new_e->old_chunk > e->old_chunk)
396 list_add(&new_e->hash_list, e ? &e->hash_list : l);
399 int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
401 struct dm_snap_exception *e;
403 e = alloc_exception();
409 /* Consecutive_count is implicitly initialised to zero */
412 insert_completed_exception(s, e);
420 static int calc_max_buckets(void)
422 /* use a fixed size of 2MB */
423 unsigned long mem = 2 * 1024 * 1024;
424 mem /= sizeof(struct list_head);
430 * Allocate room for a suitable hash table.
432 static int init_hash_tables(struct dm_snapshot *s)
434 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
437 * Calculate based on the size of the original volume or
440 cow_dev_size = get_dev_size(s->cow->bdev);
441 origin_dev_size = get_dev_size(s->origin->bdev);
442 max_buckets = calc_max_buckets();
444 hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
445 hash_size = min(hash_size, max_buckets);
447 hash_size = rounddown_pow_of_two(hash_size);
448 if (init_exception_table(&s->complete, hash_size,
449 DM_CHUNK_CONSECUTIVE_BITS))
453 * Allocate hash table for in-flight exceptions
454 * Make this smaller than the real hash table
460 if (init_exception_table(&s->pending, hash_size, 0)) {
461 exit_exception_table(&s->complete, exception_cache);
469 * Round a number up to the nearest 'size' boundary. size must
472 static ulong round_up(ulong n, ulong size)
475 return (n + size) & ~size;
478 static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
481 unsigned long chunk_size;
484 chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
485 if (*chunk_size_arg == '\0' || *value != '\0') {
486 *error = "Invalid chunk size";
491 s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
496 * Chunk size must be multiple of page size. Silently
497 * round up if it's not.
499 chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
501 /* Check chunk_size is a power of 2 */
502 if (!is_power_of_2(chunk_size)) {
503 *error = "Chunk size is not a power of 2";
507 /* Validate the chunk size against the device block size */
508 if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
509 *error = "Chunk size is not a multiple of device blocksize";
513 s->chunk_size = chunk_size;
514 s->chunk_mask = chunk_size - 1;
515 s->chunk_shift = ffs(chunk_size) - 1;
521 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
523 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
525 struct dm_snapshot *s;
533 ti->error = "requires exactly 4 arguments";
538 origin_path = argv[0];
540 persistent = toupper(*argv[2]);
542 if (persistent != 'P' && persistent != 'N') {
543 ti->error = "Persistent flag is not P or N";
548 s = kmalloc(sizeof(*s), GFP_KERNEL);
550 ti->error = "Cannot allocate snapshot context private "
556 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
558 ti->error = "Cannot get origin device";
562 r = dm_get_device(ti, cow_path, 0, 0,
563 FMODE_READ | FMODE_WRITE, &s->cow);
565 dm_put_device(ti, s->origin);
566 ti->error = "Cannot get COW device";
570 r = set_chunk_size(s, argv[3], &ti->error);
574 s->type = persistent;
579 init_rwsem(&s->lock);
580 spin_lock_init(&s->pe_lock);
583 /* Allocate hash table for COW data */
584 if (init_hash_tables(s)) {
585 ti->error = "Unable to allocate hash table space";
592 if (persistent == 'P')
593 r = dm_create_persistent(&s->store);
595 r = dm_create_transient(&s->store);
598 ti->error = "Couldn't create exception store";
603 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
605 ti->error = "Could not create kcopyd client";
609 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
610 tracked_chunk_cache);
611 if (!s->tracked_chunk_pool) {
612 ti->error = "Could not allocate tracked_chunk mempool for "
617 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
618 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
620 spin_lock_init(&s->tracked_chunk_lock);
622 /* Metadata must only be loaded into one table at once */
623 r = s->store.read_metadata(&s->store);
625 ti->error = "Failed to read snapshot metadata";
626 goto bad_load_and_register;
629 DMWARN("Snapshot is marked invalid.");
632 bio_list_init(&s->queued_bios);
633 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
635 /* Add snapshot to the list of snapshots for this origin */
636 /* Exceptions aren't triggered till snapshot_resume() is called */
637 if (register_snapshot(s)) {
639 ti->error = "Cannot register snapshot origin";
640 goto bad_load_and_register;
644 ti->split_io = s->chunk_size;
648 bad_load_and_register:
649 mempool_destroy(s->tracked_chunk_pool);
652 dm_kcopyd_client_destroy(s->kcopyd_client);
655 s->store.destroy(&s->store);
658 exit_exception_table(&s->pending, pending_cache);
659 exit_exception_table(&s->complete, exception_cache);
662 dm_put_device(ti, s->cow);
663 dm_put_device(ti, s->origin);
672 static void __free_exceptions(struct dm_snapshot *s)
674 dm_kcopyd_client_destroy(s->kcopyd_client);
675 s->kcopyd_client = NULL;
677 exit_exception_table(&s->pending, pending_cache);
678 exit_exception_table(&s->complete, exception_cache);
680 s->store.destroy(&s->store);
683 static void snapshot_dtr(struct dm_target *ti)
685 #ifdef CONFIG_DM_DEBUG
688 struct dm_snapshot *s = ti->private;
690 flush_workqueue(ksnapd);
692 /* Prevent further origin writes from using this snapshot. */
693 /* After this returns there can be no new kcopyd jobs. */
694 unregister_snapshot(s);
696 #ifdef CONFIG_DM_DEBUG
697 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
698 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
701 mempool_destroy(s->tracked_chunk_pool);
703 __free_exceptions(s);
705 dm_put_device(ti, s->origin);
706 dm_put_device(ti, s->cow);
712 * Flush a list of buffers.
714 static void flush_bios(struct bio *bio)
721 generic_make_request(bio);
726 static void flush_queued_bios(struct work_struct *work)
728 struct dm_snapshot *s =
729 container_of(work, struct dm_snapshot, queued_bios_work);
730 struct bio *queued_bios;
733 spin_lock_irqsave(&s->pe_lock, flags);
734 queued_bios = bio_list_get(&s->queued_bios);
735 spin_unlock_irqrestore(&s->pe_lock, flags);
737 flush_bios(queued_bios);
741 * Error a list of buffers.
743 static void error_bios(struct bio *bio)
755 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
761 DMERR("Invalidating snapshot: Error reading/writing.");
762 else if (err == -ENOMEM)
763 DMERR("Invalidating snapshot: Unable to allocate exception.");
765 if (s->store.drop_snapshot)
766 s->store.drop_snapshot(&s->store);
770 dm_table_event(s->ti->table);
773 static void get_pending_exception(struct dm_snap_pending_exception *pe)
775 atomic_inc(&pe->ref_count);
778 static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
780 struct dm_snap_pending_exception *primary_pe;
781 struct bio *origin_bios = NULL;
783 primary_pe = pe->primary_pe;
786 * If this pe is involved in a write to the origin and
787 * it is the last sibling to complete then release
788 * the bios for the original write to the origin.
791 atomic_dec_and_test(&primary_pe->ref_count))
792 origin_bios = bio_list_get(&primary_pe->origin_bios);
795 * Free the pe if it's not linked to an origin write or if
796 * it's not itself a primary pe.
798 if (!primary_pe || primary_pe != pe)
799 free_pending_exception(pe);
802 * Free the primary pe if nothing references it.
804 if (primary_pe && !atomic_read(&primary_pe->ref_count))
805 free_pending_exception(primary_pe);
810 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
812 struct dm_snap_exception *e;
813 struct dm_snapshot *s = pe->snap;
814 struct bio *origin_bios = NULL;
815 struct bio *snapshot_bios = NULL;
819 /* Read/write error - snapshot is unusable */
820 down_write(&s->lock);
821 __invalidate_snapshot(s, -EIO);
826 e = alloc_exception();
828 down_write(&s->lock);
829 __invalidate_snapshot(s, -ENOMEM);
835 down_write(&s->lock);
843 * Add a proper exception, and remove the
844 * in-flight exception from the list.
846 insert_completed_exception(s, e);
849 remove_exception(&pe->e);
850 snapshot_bios = bio_list_get(&pe->snapshot_bios);
851 origin_bios = put_pending_exception(pe);
855 /* Submit any pending write bios */
857 error_bios(snapshot_bios);
859 flush_bios(snapshot_bios);
861 flush_bios(origin_bios);
864 static void commit_callback(void *context, int success)
866 struct dm_snap_pending_exception *pe = context;
868 pending_complete(pe, success);
872 * Called when the copy I/O has finished. kcopyd actually runs
873 * this code so don't block.
875 static void copy_callback(int read_err, unsigned long write_err, void *context)
877 struct dm_snap_pending_exception *pe = context;
878 struct dm_snapshot *s = pe->snap;
880 if (read_err || write_err)
881 pending_complete(pe, 0);
884 /* Update the metadata if we are persistent */
885 s->store.commit_exception(&s->store, &pe->e, commit_callback,
890 * Dispatches the copy operation to kcopyd.
892 static void start_copy(struct dm_snap_pending_exception *pe)
894 struct dm_snapshot *s = pe->snap;
895 struct dm_io_region src, dest;
896 struct block_device *bdev = s->origin->bdev;
899 dev_size = get_dev_size(bdev);
902 src.sector = chunk_to_sector(s, pe->e.old_chunk);
903 src.count = min(s->chunk_size, dev_size - src.sector);
905 dest.bdev = s->cow->bdev;
906 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
907 dest.count = src.count;
909 /* Hand over to kcopyd */
910 dm_kcopyd_copy(s->kcopyd_client,
911 &src, 1, &dest, 0, copy_callback, pe);
915 * Looks to see if this snapshot already has a pending exception
916 * for this chunk, otherwise it allocates a new one and inserts
917 * it into the pending table.
919 * NOTE: a write lock must be held on snap->lock before calling
922 static struct dm_snap_pending_exception *
923 __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
925 struct dm_snap_exception *e;
926 struct dm_snap_pending_exception *pe;
927 chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
930 * Is there a pending exception for this already ?
932 e = lookup_exception(&s->pending, chunk);
934 /* cast the exception to a pending exception */
935 pe = container_of(e, struct dm_snap_pending_exception, e);
940 * Create a new pending exception, we don't want
941 * to hold the lock while we do this.
944 pe = alloc_pending_exception();
945 down_write(&s->lock);
948 free_pending_exception(pe);
952 e = lookup_exception(&s->pending, chunk);
954 free_pending_exception(pe);
955 pe = container_of(e, struct dm_snap_pending_exception, e);
959 pe->e.old_chunk = chunk;
960 bio_list_init(&pe->origin_bios);
961 bio_list_init(&pe->snapshot_bios);
962 pe->primary_pe = NULL;
963 atomic_set(&pe->ref_count, 0);
967 if (s->store.prepare_exception(&s->store, &pe->e)) {
968 free_pending_exception(pe);
972 get_pending_exception(pe);
973 insert_exception(&s->pending, &pe->e);
979 static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
980 struct bio *bio, chunk_t chunk)
982 bio->bi_bdev = s->cow->bdev;
983 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
984 (chunk - e->old_chunk)) +
985 (bio->bi_sector & s->chunk_mask);
988 static int snapshot_map(struct dm_target *ti, struct bio *bio,
989 union map_info *map_context)
991 struct dm_snap_exception *e;
992 struct dm_snapshot *s = ti->private;
993 int r = DM_MAPIO_REMAPPED;
995 struct dm_snap_pending_exception *pe = NULL;
997 chunk = sector_to_chunk(s, bio->bi_sector);
999 /* Full snapshots are not usable */
1000 /* To get here the table must be live so s->active is always set. */
1004 /* FIXME: should only take write lock if we need
1005 * to copy an exception */
1006 down_write(&s->lock);
1013 /* If the block is already remapped - use that, else remap it */
1014 e = lookup_exception(&s->complete, chunk);
1016 remap_exception(s, e, bio, chunk);
1021 * Write to snapshot - higher level takes care of RW/RO
1022 * flags so we should only get this if we are
1025 if (bio_rw(bio) == WRITE) {
1026 pe = __find_pending_exception(s, bio);
1028 __invalidate_snapshot(s, -ENOMEM);
1033 remap_exception(s, &pe->e, bio, chunk);
1034 bio_list_add(&pe->snapshot_bios, bio);
1036 r = DM_MAPIO_SUBMITTED;
1039 /* this is protected by snap->lock */
1046 bio->bi_bdev = s->origin->bdev;
1047 map_context->ptr = track_chunk(s, chunk);
1056 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1057 int error, union map_info *map_context)
1059 struct dm_snapshot *s = ti->private;
1060 struct dm_snap_tracked_chunk *c = map_context->ptr;
1063 stop_tracking_chunk(s, c);
1068 static void snapshot_resume(struct dm_target *ti)
1070 struct dm_snapshot *s = ti->private;
1072 down_write(&s->lock);
1077 static int snapshot_status(struct dm_target *ti, status_type_t type,
1078 char *result, unsigned int maxlen)
1080 struct dm_snapshot *snap = ti->private;
1083 case STATUSTYPE_INFO:
1085 snprintf(result, maxlen, "Invalid");
1087 if (snap->store.fraction_full) {
1088 sector_t numerator, denominator;
1089 snap->store.fraction_full(&snap->store,
1092 snprintf(result, maxlen, "%llu/%llu",
1093 (unsigned long long)numerator,
1094 (unsigned long long)denominator);
1097 snprintf(result, maxlen, "Unknown");
1101 case STATUSTYPE_TABLE:
1103 * kdevname returns a static pointer so we need
1104 * to make private copies if the output is to
1107 snprintf(result, maxlen, "%s %s %c %llu",
1108 snap->origin->name, snap->cow->name,
1110 (unsigned long long)snap->chunk_size);
1117 /*-----------------------------------------------------------------
1119 *---------------------------------------------------------------*/
1120 static int __origin_write(struct list_head *snapshots, struct bio *bio)
1122 int r = DM_MAPIO_REMAPPED, first = 0;
1123 struct dm_snapshot *snap;
1124 struct dm_snap_exception *e;
1125 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
1127 LIST_HEAD(pe_queue);
1129 /* Do all the snapshots on this origin */
1130 list_for_each_entry (snap, snapshots, list) {
1132 down_write(&snap->lock);
1134 /* Only deal with valid and active snapshots */
1135 if (!snap->valid || !snap->active)
1138 /* Nothing to do if writing beyond end of snapshot */
1139 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
1143 * Remember, different snapshots can have
1144 * different chunk sizes.
1146 chunk = sector_to_chunk(snap, bio->bi_sector);
1149 * Check exception table to see if block
1150 * is already remapped in this snapshot
1151 * and trigger an exception if not.
1153 * ref_count is initialised to 1 so pending_complete()
1154 * won't destroy the primary_pe while we're inside this loop.
1156 e = lookup_exception(&snap->complete, chunk);
1160 pe = __find_pending_exception(snap, bio);
1162 __invalidate_snapshot(snap, -ENOMEM);
1168 * Either every pe here has same
1169 * primary_pe or none has one yet.
1172 primary_pe = pe->primary_pe;
1178 bio_list_add(&primary_pe->origin_bios, bio);
1180 r = DM_MAPIO_SUBMITTED;
1183 if (!pe->primary_pe) {
1184 pe->primary_pe = primary_pe;
1185 get_pending_exception(primary_pe);
1190 list_add_tail(&pe->list, &pe_queue);
1194 up_write(&snap->lock);
1201 * If this is the first time we're processing this chunk and
1202 * ref_count is now 1 it means all the pending exceptions
1203 * got completed while we were in the loop above, so it falls to
1204 * us here to remove the primary_pe and submit any origin_bios.
1207 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
1208 flush_bios(bio_list_get(&primary_pe->origin_bios));
1209 free_pending_exception(primary_pe);
1210 /* If we got here, pe_queue is necessarily empty. */
1215 * Now that we have a complete pe list we can start the copying.
1217 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1224 * Called on a write from the origin driver.
1226 static int do_origin(struct dm_dev *origin, struct bio *bio)
1229 int r = DM_MAPIO_REMAPPED;
1231 down_read(&_origins_lock);
1232 o = __lookup_origin(origin->bdev);
1234 r = __origin_write(&o->snapshots, bio);
1235 up_read(&_origins_lock);
1241 * Origin: maps a linear range of a device, with hooks for snapshotting.
1245 * Construct an origin mapping: <dev_path>
1246 * The context for an origin is merely a 'struct dm_dev *'
1247 * pointing to the real device.
1249 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1255 ti->error = "origin: incorrect number of arguments";
1259 r = dm_get_device(ti, argv[0], 0, ti->len,
1260 dm_table_get_mode(ti->table), &dev);
1262 ti->error = "Cannot get target device";
1270 static void origin_dtr(struct dm_target *ti)
1272 struct dm_dev *dev = ti->private;
1273 dm_put_device(ti, dev);
1276 static int origin_map(struct dm_target *ti, struct bio *bio,
1277 union map_info *map_context)
1279 struct dm_dev *dev = ti->private;
1280 bio->bi_bdev = dev->bdev;
1282 /* Only tell snapshots if this is a write */
1283 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
1286 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1289 * Set the target "split_io" field to the minimum of all the snapshots'
1292 static void origin_resume(struct dm_target *ti)
1294 struct dm_dev *dev = ti->private;
1295 struct dm_snapshot *snap;
1297 chunk_t chunk_size = 0;
1299 down_read(&_origins_lock);
1300 o = __lookup_origin(dev->bdev);
1302 list_for_each_entry (snap, &o->snapshots, list)
1303 chunk_size = min_not_zero(chunk_size, snap->chunk_size);
1304 up_read(&_origins_lock);
1306 ti->split_io = chunk_size;
1309 static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1310 unsigned int maxlen)
1312 struct dm_dev *dev = ti->private;
1315 case STATUSTYPE_INFO:
1319 case STATUSTYPE_TABLE:
1320 snprintf(result, maxlen, "%s", dev->name);
1327 static struct target_type origin_target = {
1328 .name = "snapshot-origin",
1329 .version = {1, 6, 0},
1330 .module = THIS_MODULE,
1334 .resume = origin_resume,
1335 .status = origin_status,
1338 static struct target_type snapshot_target = {
1340 .version = {1, 6, 0},
1341 .module = THIS_MODULE,
1342 .ctr = snapshot_ctr,
1343 .dtr = snapshot_dtr,
1344 .map = snapshot_map,
1345 .end_io = snapshot_end_io,
1346 .resume = snapshot_resume,
1347 .status = snapshot_status,
1350 static int __init dm_snapshot_init(void)
1354 r = dm_register_target(&snapshot_target);
1356 DMERR("snapshot target register failed %d", r);
1360 r = dm_register_target(&origin_target);
1362 DMERR("Origin target register failed %d", r);
1366 r = init_origin_hash();
1368 DMERR("init_origin_hash failed.");
1372 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
1373 if (!exception_cache) {
1374 DMERR("Couldn't create exception cache.");
1379 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
1380 if (!pending_cache) {
1381 DMERR("Couldn't create pending cache.");
1386 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1387 if (!tracked_chunk_cache) {
1388 DMERR("Couldn't create cache to track chunks in use.");
1393 pending_pool = mempool_create_slab_pool(128, pending_cache);
1394 if (!pending_pool) {
1395 DMERR("Couldn't create pending pool.");
1397 goto bad_pending_pool;
1400 ksnapd = create_singlethread_workqueue("ksnapd");
1402 DMERR("Failed to create ksnapd workqueue.");
1410 mempool_destroy(pending_pool);
1412 kmem_cache_destroy(tracked_chunk_cache);
1414 kmem_cache_destroy(pending_cache);
1416 kmem_cache_destroy(exception_cache);
1420 dm_unregister_target(&origin_target);
1422 dm_unregister_target(&snapshot_target);
1426 static void __exit dm_snapshot_exit(void)
1430 destroy_workqueue(ksnapd);
1432 r = dm_unregister_target(&snapshot_target);
1434 DMERR("snapshot unregister failed %d", r);
1436 r = dm_unregister_target(&origin_target);
1438 DMERR("origin unregister failed %d", r);
1441 mempool_destroy(pending_pool);
1442 kmem_cache_destroy(pending_cache);
1443 kmem_cache_destroy(exception_cache);
1444 kmem_cache_destroy(tracked_chunk_cache);
1448 module_init(dm_snapshot_init);
1449 module_exit(dm_snapshot_exit);
1451 MODULE_DESCRIPTION(DM_NAME " snapshot target");
1452 MODULE_AUTHOR("Joe Thornber");
1453 MODULE_LICENSE("GPL");