4 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
6 * This file is released under the GPL.
9 #include <linux/blkdev.h>
10 #include <linux/ctype.h>
11 #include <linux/device-mapper.h>
12 #include <linux/delay.h>
14 #include <linux/init.h>
15 #include <linux/kdev_t.h>
16 #include <linux/list.h>
17 #include <linux/mempool.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/log2.h>
22 #include <linux/dm-kcopyd.h>
24 #include "dm-exception-store.h"
26 #include "dm-bio-list.h"
28 #define DM_MSG_PREFIX "snapshots"
31 * The percentage increment we will wake up users at
33 #define WAKE_UP_PERCENT 5
36 * kcopyd priority of snapshot operations
38 #define SNAPSHOT_COPY_PRIORITY 2
41 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
43 #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
46 * The size of the mempool used to track chunks in use.
50 static struct workqueue_struct
*ksnapd
;
51 static void flush_queued_bios(struct work_struct
*work
);
53 struct dm_snap_pending_exception
{
54 struct dm_snap_exception e
;
57 * Origin buffers waiting for this to complete are held
60 struct bio_list origin_bios
;
61 struct bio_list snapshot_bios
;
64 * Short-term queue of pending exceptions prior to submission.
66 struct list_head list
;
69 * The primary pending_exception is the one that holds
70 * the ref_count and the list of origin_bios for a
71 * group of pending_exceptions. It is always last to get freed.
72 * These fields get set up when writing to the origin.
74 struct dm_snap_pending_exception
*primary_pe
;
77 * Number of pending_exceptions processing this chunk.
78 * When this drops to zero we must complete the origin bios.
79 * If incrementing or decrementing this, hold pe->snap->lock for
80 * the sibling concerned and not pe->primary_pe->snap->lock unless
85 /* Pointer back to snapshot context */
86 struct dm_snapshot
*snap
;
89 * 1 indicates the exception has already been sent to
96 * Hash table mapping origin volumes to lists of snapshots and
97 * a lock to protect it
99 static struct kmem_cache
*exception_cache
;
100 static struct kmem_cache
*pending_cache
;
102 struct dm_snap_tracked_chunk
{
103 struct hlist_node node
;
107 static struct kmem_cache
*tracked_chunk_cache
;
109 static struct dm_snap_tracked_chunk
*track_chunk(struct dm_snapshot
*s
,
112 struct dm_snap_tracked_chunk
*c
= mempool_alloc(s
->tracked_chunk_pool
,
118 spin_lock_irqsave(&s
->tracked_chunk_lock
, flags
);
119 hlist_add_head(&c
->node
,
120 &s
->tracked_chunk_hash
[DM_TRACKED_CHUNK_HASH(chunk
)]);
121 spin_unlock_irqrestore(&s
->tracked_chunk_lock
, flags
);
126 static void stop_tracking_chunk(struct dm_snapshot
*s
,
127 struct dm_snap_tracked_chunk
*c
)
131 spin_lock_irqsave(&s
->tracked_chunk_lock
, flags
);
133 spin_unlock_irqrestore(&s
->tracked_chunk_lock
, flags
);
135 mempool_free(c
, s
->tracked_chunk_pool
);
138 static int __chunk_is_tracked(struct dm_snapshot
*s
, chunk_t chunk
)
140 struct dm_snap_tracked_chunk
*c
;
141 struct hlist_node
*hn
;
144 spin_lock_irq(&s
->tracked_chunk_lock
);
146 hlist_for_each_entry(c
, hn
,
147 &s
->tracked_chunk_hash
[DM_TRACKED_CHUNK_HASH(chunk
)], node
) {
148 if (c
->chunk
== chunk
) {
154 spin_unlock_irq(&s
->tracked_chunk_lock
);
160 * One of these per registered origin, held in the snapshot_origins hash
163 /* The origin device */
164 struct block_device
*bdev
;
166 struct list_head hash_list
;
168 /* List of snapshots for this origin */
169 struct list_head snapshots
;
173 * Size of the hash table for origin volumes. If we make this
174 * the size of the minors list then it should be nearly perfect
176 #define ORIGIN_HASH_SIZE 256
177 #define ORIGIN_MASK 0xFF
178 static struct list_head
*_origins
;
179 static struct rw_semaphore _origins_lock
;
181 static int init_origin_hash(void)
185 _origins
= kmalloc(ORIGIN_HASH_SIZE
* sizeof(struct list_head
),
188 DMERR("unable to allocate memory");
192 for (i
= 0; i
< ORIGIN_HASH_SIZE
; i
++)
193 INIT_LIST_HEAD(_origins
+ i
);
194 init_rwsem(&_origins_lock
);
199 static void exit_origin_hash(void)
204 static unsigned origin_hash(struct block_device
*bdev
)
206 return bdev
->bd_dev
& ORIGIN_MASK
;
209 static struct origin
*__lookup_origin(struct block_device
*origin
)
211 struct list_head
*ol
;
214 ol
= &_origins
[origin_hash(origin
)];
215 list_for_each_entry (o
, ol
, hash_list
)
216 if (bdev_equal(o
->bdev
, origin
))
222 static void __insert_origin(struct origin
*o
)
224 struct list_head
*sl
= &_origins
[origin_hash(o
->bdev
)];
225 list_add_tail(&o
->hash_list
, sl
);
229 * Make a note of the snapshot and its origin so we can look it
230 * up when the origin has a write on it.
232 static int register_snapshot(struct dm_snapshot
*snap
)
234 struct origin
*o
, *new_o
;
235 struct block_device
*bdev
= snap
->origin
->bdev
;
237 new_o
= kmalloc(sizeof(*new_o
), GFP_KERNEL
);
241 down_write(&_origins_lock
);
242 o
= __lookup_origin(bdev
);
250 /* Initialise the struct */
251 INIT_LIST_HEAD(&o
->snapshots
);
257 list_add_tail(&snap
->list
, &o
->snapshots
);
259 up_write(&_origins_lock
);
263 static void unregister_snapshot(struct dm_snapshot
*s
)
267 down_write(&_origins_lock
);
268 o
= __lookup_origin(s
->origin
->bdev
);
271 if (list_empty(&o
->snapshots
)) {
272 list_del(&o
->hash_list
);
276 up_write(&_origins_lock
);
280 * Implementation of the exception hash tables.
281 * The lowest hash_shift bits of the chunk number are ignored, allowing
282 * some consecutive chunks to be grouped together.
284 static int init_exception_table(struct exception_table
*et
, uint32_t size
,
289 et
->hash_shift
= hash_shift
;
290 et
->hash_mask
= size
- 1;
291 et
->table
= dm_vcalloc(size
, sizeof(struct list_head
));
295 for (i
= 0; i
< size
; i
++)
296 INIT_LIST_HEAD(et
->table
+ i
);
301 static void exit_exception_table(struct exception_table
*et
, struct kmem_cache
*mem
)
303 struct list_head
*slot
;
304 struct dm_snap_exception
*ex
, *next
;
307 size
= et
->hash_mask
+ 1;
308 for (i
= 0; i
< size
; i
++) {
309 slot
= et
->table
+ i
;
311 list_for_each_entry_safe (ex
, next
, slot
, hash_list
)
312 kmem_cache_free(mem
, ex
);
318 static uint32_t exception_hash(struct exception_table
*et
, chunk_t chunk
)
320 return (chunk
>> et
->hash_shift
) & et
->hash_mask
;
323 static void insert_exception(struct exception_table
*eh
,
324 struct dm_snap_exception
*e
)
326 struct list_head
*l
= &eh
->table
[exception_hash(eh
, e
->old_chunk
)];
327 list_add(&e
->hash_list
, l
);
330 static void remove_exception(struct dm_snap_exception
*e
)
332 list_del(&e
->hash_list
);
336 * Return the exception data for a sector, or NULL if not
339 static struct dm_snap_exception
*lookup_exception(struct exception_table
*et
,
342 struct list_head
*slot
;
343 struct dm_snap_exception
*e
;
345 slot
= &et
->table
[exception_hash(et
, chunk
)];
346 list_for_each_entry (e
, slot
, hash_list
)
347 if (chunk
>= e
->old_chunk
&&
348 chunk
<= e
->old_chunk
+ dm_consecutive_chunk_count(e
))
354 static struct dm_snap_exception
*alloc_exception(void)
356 struct dm_snap_exception
*e
;
358 e
= kmem_cache_alloc(exception_cache
, GFP_NOIO
);
360 e
= kmem_cache_alloc(exception_cache
, GFP_ATOMIC
);
365 static void free_exception(struct dm_snap_exception
*e
)
367 kmem_cache_free(exception_cache
, e
);
370 static struct dm_snap_pending_exception
*alloc_pending_exception(struct dm_snapshot
*s
)
372 struct dm_snap_pending_exception
*pe
= mempool_alloc(s
->pending_pool
,
375 atomic_inc(&s
->pending_exceptions_count
);
381 static void free_pending_exception(struct dm_snap_pending_exception
*pe
)
383 struct dm_snapshot
*s
= pe
->snap
;
385 mempool_free(pe
, s
->pending_pool
);
386 smp_mb__before_atomic_dec();
387 atomic_dec(&s
->pending_exceptions_count
);
390 static void insert_completed_exception(struct dm_snapshot
*s
,
391 struct dm_snap_exception
*new_e
)
393 struct exception_table
*eh
= &s
->complete
;
395 struct dm_snap_exception
*e
= NULL
;
397 l
= &eh
->table
[exception_hash(eh
, new_e
->old_chunk
)];
399 /* Add immediately if this table doesn't support consecutive chunks */
403 /* List is ordered by old_chunk */
404 list_for_each_entry_reverse(e
, l
, hash_list
) {
405 /* Insert after an existing chunk? */
406 if (new_e
->old_chunk
== (e
->old_chunk
+
407 dm_consecutive_chunk_count(e
) + 1) &&
408 new_e
->new_chunk
== (dm_chunk_number(e
->new_chunk
) +
409 dm_consecutive_chunk_count(e
) + 1)) {
410 dm_consecutive_chunk_count_inc(e
);
411 free_exception(new_e
);
415 /* Insert before an existing chunk? */
416 if (new_e
->old_chunk
== (e
->old_chunk
- 1) &&
417 new_e
->new_chunk
== (dm_chunk_number(e
->new_chunk
) - 1)) {
418 dm_consecutive_chunk_count_inc(e
);
421 free_exception(new_e
);
425 if (new_e
->old_chunk
> e
->old_chunk
)
430 list_add(&new_e
->hash_list
, e
? &e
->hash_list
: l
);
434 * Callback used by the exception stores to load exceptions when
437 static int dm_add_exception(void *context
, chunk_t old
, chunk_t
new)
439 struct dm_snapshot
*s
= context
;
440 struct dm_snap_exception
*e
;
442 e
= alloc_exception();
448 /* Consecutive_count is implicitly initialised to zero */
451 insert_completed_exception(s
, e
);
459 static int calc_max_buckets(void)
461 /* use a fixed size of 2MB */
462 unsigned long mem
= 2 * 1024 * 1024;
463 mem
/= sizeof(struct list_head
);
469 * Allocate room for a suitable hash table.
471 static int init_hash_tables(struct dm_snapshot
*s
)
473 sector_t hash_size
, cow_dev_size
, origin_dev_size
, max_buckets
;
476 * Calculate based on the size of the original volume or
479 cow_dev_size
= get_dev_size(s
->cow
->bdev
);
480 origin_dev_size
= get_dev_size(s
->origin
->bdev
);
481 max_buckets
= calc_max_buckets();
483 hash_size
= min(origin_dev_size
, cow_dev_size
) >> s
->chunk_shift
;
484 hash_size
= min(hash_size
, max_buckets
);
486 hash_size
= rounddown_pow_of_two(hash_size
);
487 if (init_exception_table(&s
->complete
, hash_size
,
488 DM_CHUNK_CONSECUTIVE_BITS
))
492 * Allocate hash table for in-flight exceptions
493 * Make this smaller than the real hash table
499 if (init_exception_table(&s
->pending
, hash_size
, 0)) {
500 exit_exception_table(&s
->complete
, exception_cache
);
508 * Round a number up to the nearest 'size' boundary. size must
511 static ulong
round_up(ulong n
, ulong size
)
514 return (n
+ size
) & ~size
;
517 static int set_chunk_size(struct dm_snapshot
*s
, const char *chunk_size_arg
,
520 unsigned long chunk_size
;
523 chunk_size
= simple_strtoul(chunk_size_arg
, &value
, 10);
524 if (*chunk_size_arg
== '\0' || *value
!= '\0') {
525 *error
= "Invalid chunk size";
530 s
->chunk_size
= s
->chunk_mask
= s
->chunk_shift
= 0;
535 * Chunk size must be multiple of page size. Silently
536 * round up if it's not.
538 chunk_size
= round_up(chunk_size
, PAGE_SIZE
>> 9);
540 /* Check chunk_size is a power of 2 */
541 if (!is_power_of_2(chunk_size
)) {
542 *error
= "Chunk size is not a power of 2";
546 /* Validate the chunk size against the device block size */
547 if (chunk_size
% (bdev_hardsect_size(s
->cow
->bdev
) >> 9)) {
548 *error
= "Chunk size is not a multiple of device blocksize";
552 s
->chunk_size
= chunk_size
;
553 s
->chunk_mask
= chunk_size
- 1;
554 s
->chunk_shift
= ffs(chunk_size
) - 1;
560 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
562 static int snapshot_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
564 struct dm_snapshot
*s
;
572 ti
->error
= "requires exactly 4 arguments";
577 origin_path
= argv
[0];
579 persistent
= toupper(*argv
[2]);
581 if (persistent
!= 'P' && persistent
!= 'N') {
582 ti
->error
= "Persistent flag is not P or N";
587 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
589 ti
->error
= "Cannot allocate snapshot context private "
595 r
= dm_get_device(ti
, origin_path
, 0, ti
->len
, FMODE_READ
, &s
->origin
);
597 ti
->error
= "Cannot get origin device";
601 r
= dm_get_device(ti
, cow_path
, 0, 0,
602 FMODE_READ
| FMODE_WRITE
, &s
->cow
);
604 dm_put_device(ti
, s
->origin
);
605 ti
->error
= "Cannot get COW device";
609 r
= set_chunk_size(s
, argv
[3], &ti
->error
);
615 atomic_set(&s
->pending_exceptions_count
, 0);
616 init_rwsem(&s
->lock
);
617 spin_lock_init(&s
->pe_lock
);
620 /* Allocate hash table for COW data */
621 if (init_hash_tables(s
)) {
622 ti
->error
= "Unable to allocate hash table space";
627 r
= dm_exception_store_create(argv
[2], &s
->store
);
629 ti
->error
= "Couldn't create exception store";
636 r
= dm_kcopyd_client_create(SNAPSHOT_PAGES
, &s
->kcopyd_client
);
638 ti
->error
= "Could not create kcopyd client";
642 s
->pending_pool
= mempool_create_slab_pool(MIN_IOS
, pending_cache
);
643 if (!s
->pending_pool
) {
644 ti
->error
= "Could not allocate mempool for pending exceptions";
648 s
->tracked_chunk_pool
= mempool_create_slab_pool(MIN_IOS
,
649 tracked_chunk_cache
);
650 if (!s
->tracked_chunk_pool
) {
651 ti
->error
= "Could not allocate tracked_chunk mempool for "
653 goto bad_tracked_chunk_pool
;
656 for (i
= 0; i
< DM_TRACKED_CHUNK_HASH_SIZE
; i
++)
657 INIT_HLIST_HEAD(&s
->tracked_chunk_hash
[i
]);
659 spin_lock_init(&s
->tracked_chunk_lock
);
661 /* Metadata must only be loaded into one table at once */
662 r
= s
->store
->type
->read_metadata(s
->store
, dm_add_exception
,
665 ti
->error
= "Failed to read snapshot metadata";
666 goto bad_load_and_register
;
669 DMWARN("Snapshot is marked invalid.");
672 bio_list_init(&s
->queued_bios
);
673 INIT_WORK(&s
->queued_bios_work
, flush_queued_bios
);
675 /* Add snapshot to the list of snapshots for this origin */
676 /* Exceptions aren't triggered till snapshot_resume() is called */
677 if (register_snapshot(s
)) {
679 ti
->error
= "Cannot register snapshot origin";
680 goto bad_load_and_register
;
684 ti
->split_io
= s
->chunk_size
;
688 bad_load_and_register
:
689 mempool_destroy(s
->tracked_chunk_pool
);
691 bad_tracked_chunk_pool
:
692 mempool_destroy(s
->pending_pool
);
695 dm_kcopyd_client_destroy(s
->kcopyd_client
);
698 s
->store
->type
->dtr(s
->store
);
701 exit_exception_table(&s
->pending
, pending_cache
);
702 exit_exception_table(&s
->complete
, exception_cache
);
705 dm_put_device(ti
, s
->cow
);
706 dm_put_device(ti
, s
->origin
);
715 static void __free_exceptions(struct dm_snapshot
*s
)
717 dm_kcopyd_client_destroy(s
->kcopyd_client
);
718 s
->kcopyd_client
= NULL
;
720 exit_exception_table(&s
->pending
, pending_cache
);
721 exit_exception_table(&s
->complete
, exception_cache
);
723 s
->store
->type
->dtr(s
->store
);
726 static void snapshot_dtr(struct dm_target
*ti
)
728 #ifdef CONFIG_DM_DEBUG
731 struct dm_snapshot
*s
= ti
->private;
733 flush_workqueue(ksnapd
);
735 /* Prevent further origin writes from using this snapshot. */
736 /* After this returns there can be no new kcopyd jobs. */
737 unregister_snapshot(s
);
739 while (atomic_read(&s
->pending_exceptions_count
))
742 * Ensure instructions in mempool_destroy aren't reordered
743 * before atomic_read.
747 #ifdef CONFIG_DM_DEBUG
748 for (i
= 0; i
< DM_TRACKED_CHUNK_HASH_SIZE
; i
++)
749 BUG_ON(!hlist_empty(&s
->tracked_chunk_hash
[i
]));
752 mempool_destroy(s
->tracked_chunk_pool
);
754 __free_exceptions(s
);
756 mempool_destroy(s
->pending_pool
);
758 dm_put_device(ti
, s
->origin
);
759 dm_put_device(ti
, s
->cow
);
765 * Flush a list of buffers.
767 static void flush_bios(struct bio
*bio
)
774 generic_make_request(bio
);
779 static void flush_queued_bios(struct work_struct
*work
)
781 struct dm_snapshot
*s
=
782 container_of(work
, struct dm_snapshot
, queued_bios_work
);
783 struct bio
*queued_bios
;
786 spin_lock_irqsave(&s
->pe_lock
, flags
);
787 queued_bios
= bio_list_get(&s
->queued_bios
);
788 spin_unlock_irqrestore(&s
->pe_lock
, flags
);
790 flush_bios(queued_bios
);
794 * Error a list of buffers.
796 static void error_bios(struct bio
*bio
)
808 static void __invalidate_snapshot(struct dm_snapshot
*s
, int err
)
814 DMERR("Invalidating snapshot: Error reading/writing.");
815 else if (err
== -ENOMEM
)
816 DMERR("Invalidating snapshot: Unable to allocate exception.");
818 if (s
->store
->type
->drop_snapshot
)
819 s
->store
->type
->drop_snapshot(s
->store
);
823 dm_table_event(s
->ti
->table
);
826 static void get_pending_exception(struct dm_snap_pending_exception
*pe
)
828 atomic_inc(&pe
->ref_count
);
831 static struct bio
*put_pending_exception(struct dm_snap_pending_exception
*pe
)
833 struct dm_snap_pending_exception
*primary_pe
;
834 struct bio
*origin_bios
= NULL
;
836 primary_pe
= pe
->primary_pe
;
839 * If this pe is involved in a write to the origin and
840 * it is the last sibling to complete then release
841 * the bios for the original write to the origin.
844 atomic_dec_and_test(&primary_pe
->ref_count
)) {
845 origin_bios
= bio_list_get(&primary_pe
->origin_bios
);
846 free_pending_exception(primary_pe
);
850 * Free the pe if it's not linked to an origin write or if
851 * it's not itself a primary pe.
853 if (!primary_pe
|| primary_pe
!= pe
)
854 free_pending_exception(pe
);
859 static void pending_complete(struct dm_snap_pending_exception
*pe
, int success
)
861 struct dm_snap_exception
*e
;
862 struct dm_snapshot
*s
= pe
->snap
;
863 struct bio
*origin_bios
= NULL
;
864 struct bio
*snapshot_bios
= NULL
;
868 /* Read/write error - snapshot is unusable */
869 down_write(&s
->lock
);
870 __invalidate_snapshot(s
, -EIO
);
875 e
= alloc_exception();
877 down_write(&s
->lock
);
878 __invalidate_snapshot(s
, -ENOMEM
);
884 down_write(&s
->lock
);
892 * Check for conflicting reads. This is extremely improbable,
893 * so msleep(1) is sufficient and there is no need for a wait queue.
895 while (__chunk_is_tracked(s
, pe
->e
.old_chunk
))
899 * Add a proper exception, and remove the
900 * in-flight exception from the list.
902 insert_completed_exception(s
, e
);
905 remove_exception(&pe
->e
);
906 snapshot_bios
= bio_list_get(&pe
->snapshot_bios
);
907 origin_bios
= put_pending_exception(pe
);
911 /* Submit any pending write bios */
913 error_bios(snapshot_bios
);
915 flush_bios(snapshot_bios
);
917 flush_bios(origin_bios
);
920 static void commit_callback(void *context
, int success
)
922 struct dm_snap_pending_exception
*pe
= context
;
924 pending_complete(pe
, success
);
928 * Called when the copy I/O has finished. kcopyd actually runs
929 * this code so don't block.
931 static void copy_callback(int read_err
, unsigned long write_err
, void *context
)
933 struct dm_snap_pending_exception
*pe
= context
;
934 struct dm_snapshot
*s
= pe
->snap
;
936 if (read_err
|| write_err
)
937 pending_complete(pe
, 0);
940 /* Update the metadata if we are persistent */
941 s
->store
->type
->commit_exception(s
->store
, &pe
->e
,
942 commit_callback
, pe
);
946 * Dispatches the copy operation to kcopyd.
948 static void start_copy(struct dm_snap_pending_exception
*pe
)
950 struct dm_snapshot
*s
= pe
->snap
;
951 struct dm_io_region src
, dest
;
952 struct block_device
*bdev
= s
->origin
->bdev
;
955 dev_size
= get_dev_size(bdev
);
958 src
.sector
= chunk_to_sector(s
, pe
->e
.old_chunk
);
959 src
.count
= min(s
->chunk_size
, dev_size
- src
.sector
);
961 dest
.bdev
= s
->cow
->bdev
;
962 dest
.sector
= chunk_to_sector(s
, pe
->e
.new_chunk
);
963 dest
.count
= src
.count
;
965 /* Hand over to kcopyd */
966 dm_kcopyd_copy(s
->kcopyd_client
,
967 &src
, 1, &dest
, 0, copy_callback
, pe
);
970 static struct dm_snap_pending_exception
*
971 __lookup_pending_exception(struct dm_snapshot
*s
, chunk_t chunk
)
973 struct dm_snap_exception
*e
= lookup_exception(&s
->pending
, chunk
);
978 return container_of(e
, struct dm_snap_pending_exception
, e
);
982 * Looks to see if this snapshot already has a pending exception
983 * for this chunk, otherwise it allocates a new one and inserts
984 * it into the pending table.
986 * NOTE: a write lock must be held on snap->lock before calling
989 static struct dm_snap_pending_exception
*
990 __find_pending_exception(struct dm_snapshot
*s
,
991 struct dm_snap_pending_exception
*pe
, chunk_t chunk
)
993 struct dm_snap_pending_exception
*pe2
;
995 pe2
= __lookup_pending_exception(s
, chunk
);
997 free_pending_exception(pe
);
1001 pe
->e
.old_chunk
= chunk
;
1002 bio_list_init(&pe
->origin_bios
);
1003 bio_list_init(&pe
->snapshot_bios
);
1004 pe
->primary_pe
= NULL
;
1005 atomic_set(&pe
->ref_count
, 0);
1008 if (s
->store
->type
->prepare_exception(s
->store
, &pe
->e
)) {
1009 free_pending_exception(pe
);
1013 get_pending_exception(pe
);
1014 insert_exception(&s
->pending
, &pe
->e
);
1019 static void remap_exception(struct dm_snapshot
*s
, struct dm_snap_exception
*e
,
1020 struct bio
*bio
, chunk_t chunk
)
1022 bio
->bi_bdev
= s
->cow
->bdev
;
1023 bio
->bi_sector
= chunk_to_sector(s
, dm_chunk_number(e
->new_chunk
) +
1024 (chunk
- e
->old_chunk
)) +
1025 (bio
->bi_sector
& s
->chunk_mask
);
1028 static int snapshot_map(struct dm_target
*ti
, struct bio
*bio
,
1029 union map_info
*map_context
)
1031 struct dm_snap_exception
*e
;
1032 struct dm_snapshot
*s
= ti
->private;
1033 int r
= DM_MAPIO_REMAPPED
;
1035 struct dm_snap_pending_exception
*pe
= NULL
;
1037 chunk
= sector_to_chunk(s
, bio
->bi_sector
);
1039 /* Full snapshots are not usable */
1040 /* To get here the table must be live so s->active is always set. */
1044 /* FIXME: should only take write lock if we need
1045 * to copy an exception */
1046 down_write(&s
->lock
);
1053 /* If the block is already remapped - use that, else remap it */
1054 e
= lookup_exception(&s
->complete
, chunk
);
1056 remap_exception(s
, e
, bio
, chunk
);
1061 * Write to snapshot - higher level takes care of RW/RO
1062 * flags so we should only get this if we are
1065 if (bio_rw(bio
) == WRITE
) {
1066 pe
= __lookup_pending_exception(s
, chunk
);
1069 pe
= alloc_pending_exception(s
);
1070 down_write(&s
->lock
);
1073 free_pending_exception(pe
);
1078 e
= lookup_exception(&s
->complete
, chunk
);
1080 free_pending_exception(pe
);
1081 remap_exception(s
, e
, bio
, chunk
);
1085 pe
= __find_pending_exception(s
, pe
, chunk
);
1087 __invalidate_snapshot(s
, -ENOMEM
);
1093 remap_exception(s
, &pe
->e
, bio
, chunk
);
1094 bio_list_add(&pe
->snapshot_bios
, bio
);
1096 r
= DM_MAPIO_SUBMITTED
;
1099 /* this is protected by snap->lock */
1106 bio
->bi_bdev
= s
->origin
->bdev
;
1107 map_context
->ptr
= track_chunk(s
, chunk
);
1116 static int snapshot_end_io(struct dm_target
*ti
, struct bio
*bio
,
1117 int error
, union map_info
*map_context
)
1119 struct dm_snapshot
*s
= ti
->private;
1120 struct dm_snap_tracked_chunk
*c
= map_context
->ptr
;
1123 stop_tracking_chunk(s
, c
);
1128 static void snapshot_resume(struct dm_target
*ti
)
1130 struct dm_snapshot
*s
= ti
->private;
1132 down_write(&s
->lock
);
1137 static int snapshot_status(struct dm_target
*ti
, status_type_t type
,
1138 char *result
, unsigned int maxlen
)
1140 struct dm_snapshot
*snap
= ti
->private;
1143 case STATUSTYPE_INFO
:
1145 snprintf(result
, maxlen
, "Invalid");
1147 if (snap
->store
->type
->fraction_full
) {
1148 sector_t numerator
, denominator
;
1149 snap
->store
->type
->fraction_full(snap
->store
,
1152 snprintf(result
, maxlen
, "%llu/%llu",
1153 (unsigned long long)numerator
,
1154 (unsigned long long)denominator
);
1157 snprintf(result
, maxlen
, "Unknown");
1161 case STATUSTYPE_TABLE
:
1163 * kdevname returns a static pointer so we need
1164 * to make private copies if the output is to
1167 snprintf(result
, maxlen
, "%s %s %s %llu",
1168 snap
->origin
->name
, snap
->cow
->name
,
1169 snap
->store
->type
->name
,
1170 (unsigned long long)snap
->chunk_size
);
1177 /*-----------------------------------------------------------------
1179 *---------------------------------------------------------------*/
1180 static int __origin_write(struct list_head
*snapshots
, struct bio
*bio
)
1182 int r
= DM_MAPIO_REMAPPED
, first
= 0;
1183 struct dm_snapshot
*snap
;
1184 struct dm_snap_exception
*e
;
1185 struct dm_snap_pending_exception
*pe
, *next_pe
, *primary_pe
= NULL
;
1187 LIST_HEAD(pe_queue
);
1189 /* Do all the snapshots on this origin */
1190 list_for_each_entry (snap
, snapshots
, list
) {
1192 down_write(&snap
->lock
);
1194 /* Only deal with valid and active snapshots */
1195 if (!snap
->valid
|| !snap
->active
)
1198 /* Nothing to do if writing beyond end of snapshot */
1199 if (bio
->bi_sector
>= dm_table_get_size(snap
->ti
->table
))
1203 * Remember, different snapshots can have
1204 * different chunk sizes.
1206 chunk
= sector_to_chunk(snap
, bio
->bi_sector
);
1209 * Check exception table to see if block
1210 * is already remapped in this snapshot
1211 * and trigger an exception if not.
1213 * ref_count is initialised to 1 so pending_complete()
1214 * won't destroy the primary_pe while we're inside this loop.
1216 e
= lookup_exception(&snap
->complete
, chunk
);
1220 pe
= __lookup_pending_exception(snap
, chunk
);
1222 up_write(&snap
->lock
);
1223 pe
= alloc_pending_exception(snap
);
1224 down_write(&snap
->lock
);
1227 free_pending_exception(pe
);
1231 e
= lookup_exception(&snap
->complete
, chunk
);
1233 free_pending_exception(pe
);
1237 pe
= __find_pending_exception(snap
, pe
, chunk
);
1239 __invalidate_snapshot(snap
, -ENOMEM
);
1246 * Either every pe here has same
1247 * primary_pe or none has one yet.
1250 primary_pe
= pe
->primary_pe
;
1256 bio_list_add(&primary_pe
->origin_bios
, bio
);
1258 r
= DM_MAPIO_SUBMITTED
;
1261 if (!pe
->primary_pe
) {
1262 pe
->primary_pe
= primary_pe
;
1263 get_pending_exception(primary_pe
);
1268 list_add_tail(&pe
->list
, &pe_queue
);
1272 up_write(&snap
->lock
);
1279 * If this is the first time we're processing this chunk and
1280 * ref_count is now 1 it means all the pending exceptions
1281 * got completed while we were in the loop above, so it falls to
1282 * us here to remove the primary_pe and submit any origin_bios.
1285 if (first
&& atomic_dec_and_test(&primary_pe
->ref_count
)) {
1286 flush_bios(bio_list_get(&primary_pe
->origin_bios
));
1287 free_pending_exception(primary_pe
);
1288 /* If we got here, pe_queue is necessarily empty. */
1293 * Now that we have a complete pe list we can start the copying.
1295 list_for_each_entry_safe(pe
, next_pe
, &pe_queue
, list
)
1302 * Called on a write from the origin driver.
1304 static int do_origin(struct dm_dev
*origin
, struct bio
*bio
)
1307 int r
= DM_MAPIO_REMAPPED
;
1309 down_read(&_origins_lock
);
1310 o
= __lookup_origin(origin
->bdev
);
1312 r
= __origin_write(&o
->snapshots
, bio
);
1313 up_read(&_origins_lock
);
1319 * Origin: maps a linear range of a device, with hooks for snapshotting.
1323 * Construct an origin mapping: <dev_path>
1324 * The context for an origin is merely a 'struct dm_dev *'
1325 * pointing to the real device.
1327 static int origin_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
1333 ti
->error
= "origin: incorrect number of arguments";
1337 r
= dm_get_device(ti
, argv
[0], 0, ti
->len
,
1338 dm_table_get_mode(ti
->table
), &dev
);
1340 ti
->error
= "Cannot get target device";
1348 static void origin_dtr(struct dm_target
*ti
)
1350 struct dm_dev
*dev
= ti
->private;
1351 dm_put_device(ti
, dev
);
1354 static int origin_map(struct dm_target
*ti
, struct bio
*bio
,
1355 union map_info
*map_context
)
1357 struct dm_dev
*dev
= ti
->private;
1358 bio
->bi_bdev
= dev
->bdev
;
1360 /* Only tell snapshots if this is a write */
1361 return (bio_rw(bio
) == WRITE
) ? do_origin(dev
, bio
) : DM_MAPIO_REMAPPED
;
1364 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1367 * Set the target "split_io" field to the minimum of all the snapshots'
1370 static void origin_resume(struct dm_target
*ti
)
1372 struct dm_dev
*dev
= ti
->private;
1373 struct dm_snapshot
*snap
;
1375 chunk_t chunk_size
= 0;
1377 down_read(&_origins_lock
);
1378 o
= __lookup_origin(dev
->bdev
);
1380 list_for_each_entry (snap
, &o
->snapshots
, list
)
1381 chunk_size
= min_not_zero(chunk_size
, snap
->chunk_size
);
1382 up_read(&_origins_lock
);
1384 ti
->split_io
= chunk_size
;
1387 static int origin_status(struct dm_target
*ti
, status_type_t type
, char *result
,
1388 unsigned int maxlen
)
1390 struct dm_dev
*dev
= ti
->private;
1393 case STATUSTYPE_INFO
:
1397 case STATUSTYPE_TABLE
:
1398 snprintf(result
, maxlen
, "%s", dev
->name
);
1405 static struct target_type origin_target
= {
1406 .name
= "snapshot-origin",
1407 .version
= {1, 6, 0},
1408 .module
= THIS_MODULE
,
1412 .resume
= origin_resume
,
1413 .status
= origin_status
,
1416 static struct target_type snapshot_target
= {
1418 .version
= {1, 6, 0},
1419 .module
= THIS_MODULE
,
1420 .ctr
= snapshot_ctr
,
1421 .dtr
= snapshot_dtr
,
1422 .map
= snapshot_map
,
1423 .end_io
= snapshot_end_io
,
1424 .resume
= snapshot_resume
,
1425 .status
= snapshot_status
,
1428 static int __init
dm_snapshot_init(void)
1432 r
= dm_exception_store_init();
1434 DMERR("Failed to initialize exception stores");
1438 r
= dm_register_target(&snapshot_target
);
1440 DMERR("snapshot target register failed %d", r
);
1444 r
= dm_register_target(&origin_target
);
1446 DMERR("Origin target register failed %d", r
);
1450 r
= init_origin_hash();
1452 DMERR("init_origin_hash failed.");
1456 exception_cache
= KMEM_CACHE(dm_snap_exception
, 0);
1457 if (!exception_cache
) {
1458 DMERR("Couldn't create exception cache.");
1463 pending_cache
= KMEM_CACHE(dm_snap_pending_exception
, 0);
1464 if (!pending_cache
) {
1465 DMERR("Couldn't create pending cache.");
1470 tracked_chunk_cache
= KMEM_CACHE(dm_snap_tracked_chunk
, 0);
1471 if (!tracked_chunk_cache
) {
1472 DMERR("Couldn't create cache to track chunks in use.");
1477 ksnapd
= create_singlethread_workqueue("ksnapd");
1479 DMERR("Failed to create ksnapd workqueue.");
1481 goto bad_pending_pool
;
1487 kmem_cache_destroy(tracked_chunk_cache
);
1489 kmem_cache_destroy(pending_cache
);
1491 kmem_cache_destroy(exception_cache
);
1495 dm_unregister_target(&origin_target
);
1497 dm_unregister_target(&snapshot_target
);
1501 static void __exit
dm_snapshot_exit(void)
1503 destroy_workqueue(ksnapd
);
1505 dm_unregister_target(&snapshot_target
);
1506 dm_unregister_target(&origin_target
);
1509 kmem_cache_destroy(pending_cache
);
1510 kmem_cache_destroy(exception_cache
);
1511 kmem_cache_destroy(tracked_chunk_cache
);
1513 dm_exception_store_exit();
1517 module_init(dm_snapshot_init
);
1518 module_exit(dm_snapshot_exit
);
1520 MODULE_DESCRIPTION(DM_NAME
" snapshot target");
1521 MODULE_AUTHOR("Joe Thornber");
1522 MODULE_LICENSE("GPL");