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/device-mapper.h>
11 #include <linux/delay.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>
22 #include <linux/workqueue.h>
24 #include "dm-exception-store.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 #define DM_TRACKED_CHUNK_HASH_SIZE 16
49 #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
50 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
52 struct dm_exception_table
{
55 struct list_head
*table
;
59 struct rw_semaphore lock
;
61 struct dm_dev
*origin
;
66 /* List of snapshots per Origin */
67 struct list_head list
;
69 /* You can't use a snapshot if this is 0 (e.g. if full) */
72 /* Origin writes don't trigger exceptions until this is set */
75 /* Whether or not owning mapped_device is suspended */
78 mempool_t
*pending_pool
;
80 atomic_t pending_exceptions_count
;
82 struct dm_exception_table pending
;
83 struct dm_exception_table complete
;
86 * pe_lock protects all pending_exception operations and access
87 * as well as the snapshot_bios list.
91 /* The on disk metadata handler */
92 struct dm_exception_store
*store
;
94 struct dm_kcopyd_client
*kcopyd_client
;
96 /* Queue of snapshot writes for ksnapd to flush */
97 struct bio_list queued_bios
;
98 struct work_struct queued_bios_work
;
100 /* Chunks with outstanding reads */
101 mempool_t
*tracked_chunk_pool
;
102 spinlock_t tracked_chunk_lock
;
103 struct hlist_head tracked_chunk_hash
[DM_TRACKED_CHUNK_HASH_SIZE
];
106 struct dm_dev
*dm_snap_cow(struct dm_snapshot
*s
)
110 EXPORT_SYMBOL(dm_snap_cow
);
112 static struct workqueue_struct
*ksnapd
;
113 static void flush_queued_bios(struct work_struct
*work
);
115 static sector_t
chunk_to_sector(struct dm_exception_store
*store
,
118 return chunk
<< store
->chunk_shift
;
121 static int bdev_equal(struct block_device
*lhs
, struct block_device
*rhs
)
124 * There is only ever one instance of a particular block
125 * device so we can compare pointers safely.
130 struct dm_snap_pending_exception
{
131 struct dm_exception e
;
134 * Origin buffers waiting for this to complete are held
137 struct bio_list origin_bios
;
138 struct bio_list snapshot_bios
;
141 * Short-term queue of pending exceptions prior to submission.
143 struct list_head list
;
146 * The primary pending_exception is the one that holds
147 * the ref_count and the list of origin_bios for a
148 * group of pending_exceptions. It is always last to get freed.
149 * These fields get set up when writing to the origin.
151 struct dm_snap_pending_exception
*primary_pe
;
154 * Number of pending_exceptions processing this chunk.
155 * When this drops to zero we must complete the origin bios.
156 * If incrementing or decrementing this, hold pe->snap->lock for
157 * the sibling concerned and not pe->primary_pe->snap->lock unless
162 /* Pointer back to snapshot context */
163 struct dm_snapshot
*snap
;
166 * 1 indicates the exception has already been sent to
173 * Hash table mapping origin volumes to lists of snapshots and
174 * a lock to protect it
176 static struct kmem_cache
*exception_cache
;
177 static struct kmem_cache
*pending_cache
;
179 struct dm_snap_tracked_chunk
{
180 struct hlist_node node
;
184 static struct kmem_cache
*tracked_chunk_cache
;
186 static struct dm_snap_tracked_chunk
*track_chunk(struct dm_snapshot
*s
,
189 struct dm_snap_tracked_chunk
*c
= mempool_alloc(s
->tracked_chunk_pool
,
195 spin_lock_irqsave(&s
->tracked_chunk_lock
, flags
);
196 hlist_add_head(&c
->node
,
197 &s
->tracked_chunk_hash
[DM_TRACKED_CHUNK_HASH(chunk
)]);
198 spin_unlock_irqrestore(&s
->tracked_chunk_lock
, flags
);
203 static void stop_tracking_chunk(struct dm_snapshot
*s
,
204 struct dm_snap_tracked_chunk
*c
)
208 spin_lock_irqsave(&s
->tracked_chunk_lock
, flags
);
210 spin_unlock_irqrestore(&s
->tracked_chunk_lock
, flags
);
212 mempool_free(c
, s
->tracked_chunk_pool
);
215 static int __chunk_is_tracked(struct dm_snapshot
*s
, chunk_t chunk
)
217 struct dm_snap_tracked_chunk
*c
;
218 struct hlist_node
*hn
;
221 spin_lock_irq(&s
->tracked_chunk_lock
);
223 hlist_for_each_entry(c
, hn
,
224 &s
->tracked_chunk_hash
[DM_TRACKED_CHUNK_HASH(chunk
)], node
) {
225 if (c
->chunk
== chunk
) {
231 spin_unlock_irq(&s
->tracked_chunk_lock
);
237 * One of these per registered origin, held in the snapshot_origins hash
240 /* The origin device */
241 struct block_device
*bdev
;
243 struct list_head hash_list
;
245 /* List of snapshots for this origin */
246 struct list_head snapshots
;
250 * Size of the hash table for origin volumes. If we make this
251 * the size of the minors list then it should be nearly perfect
253 #define ORIGIN_HASH_SIZE 256
254 #define ORIGIN_MASK 0xFF
255 static struct list_head
*_origins
;
256 static struct rw_semaphore _origins_lock
;
258 static int init_origin_hash(void)
262 _origins
= kmalloc(ORIGIN_HASH_SIZE
* sizeof(struct list_head
),
265 DMERR("unable to allocate memory");
269 for (i
= 0; i
< ORIGIN_HASH_SIZE
; i
++)
270 INIT_LIST_HEAD(_origins
+ i
);
271 init_rwsem(&_origins_lock
);
276 static void exit_origin_hash(void)
281 static unsigned origin_hash(struct block_device
*bdev
)
283 return bdev
->bd_dev
& ORIGIN_MASK
;
286 static struct origin
*__lookup_origin(struct block_device
*origin
)
288 struct list_head
*ol
;
291 ol
= &_origins
[origin_hash(origin
)];
292 list_for_each_entry (o
, ol
, hash_list
)
293 if (bdev_equal(o
->bdev
, origin
))
299 static void __insert_origin(struct origin
*o
)
301 struct list_head
*sl
= &_origins
[origin_hash(o
->bdev
)];
302 list_add_tail(&o
->hash_list
, sl
);
306 * Make a note of the snapshot and its origin so we can look it
307 * up when the origin has a write on it.
309 static int register_snapshot(struct dm_snapshot
*snap
)
311 struct dm_snapshot
*l
;
312 struct origin
*o
, *new_o
;
313 struct block_device
*bdev
= snap
->origin
->bdev
;
315 new_o
= kmalloc(sizeof(*new_o
), GFP_KERNEL
);
319 down_write(&_origins_lock
);
320 o
= __lookup_origin(bdev
);
328 /* Initialise the struct */
329 INIT_LIST_HEAD(&o
->snapshots
);
335 /* Sort the list according to chunk size, largest-first smallest-last */
336 list_for_each_entry(l
, &o
->snapshots
, list
)
337 if (l
->store
->chunk_size
< snap
->store
->chunk_size
)
339 list_add_tail(&snap
->list
, &l
->list
);
341 up_write(&_origins_lock
);
345 static void unregister_snapshot(struct dm_snapshot
*s
)
349 down_write(&_origins_lock
);
350 o
= __lookup_origin(s
->origin
->bdev
);
353 if (list_empty(&o
->snapshots
)) {
354 list_del(&o
->hash_list
);
358 up_write(&_origins_lock
);
362 * Implementation of the exception hash tables.
363 * The lowest hash_shift bits of the chunk number are ignored, allowing
364 * some consecutive chunks to be grouped together.
366 static int dm_exception_table_init(struct dm_exception_table
*et
,
367 uint32_t size
, unsigned hash_shift
)
371 et
->hash_shift
= hash_shift
;
372 et
->hash_mask
= size
- 1;
373 et
->table
= dm_vcalloc(size
, sizeof(struct list_head
));
377 for (i
= 0; i
< size
; i
++)
378 INIT_LIST_HEAD(et
->table
+ i
);
383 static void dm_exception_table_exit(struct dm_exception_table
*et
,
384 struct kmem_cache
*mem
)
386 struct list_head
*slot
;
387 struct dm_exception
*ex
, *next
;
390 size
= et
->hash_mask
+ 1;
391 for (i
= 0; i
< size
; i
++) {
392 slot
= et
->table
+ i
;
394 list_for_each_entry_safe (ex
, next
, slot
, hash_list
)
395 kmem_cache_free(mem
, ex
);
401 static uint32_t exception_hash(struct dm_exception_table
*et
, chunk_t chunk
)
403 return (chunk
>> et
->hash_shift
) & et
->hash_mask
;
406 static void dm_remove_exception(struct dm_exception
*e
)
408 list_del(&e
->hash_list
);
412 * Return the exception data for a sector, or NULL if not
415 static struct dm_exception
*dm_lookup_exception(struct dm_exception_table
*et
,
418 struct list_head
*slot
;
419 struct dm_exception
*e
;
421 slot
= &et
->table
[exception_hash(et
, chunk
)];
422 list_for_each_entry (e
, slot
, hash_list
)
423 if (chunk
>= e
->old_chunk
&&
424 chunk
<= e
->old_chunk
+ dm_consecutive_chunk_count(e
))
430 static struct dm_exception
*alloc_completed_exception(void)
432 struct dm_exception
*e
;
434 e
= kmem_cache_alloc(exception_cache
, GFP_NOIO
);
436 e
= kmem_cache_alloc(exception_cache
, GFP_ATOMIC
);
441 static void free_completed_exception(struct dm_exception
*e
)
443 kmem_cache_free(exception_cache
, e
);
446 static struct dm_snap_pending_exception
*alloc_pending_exception(struct dm_snapshot
*s
)
448 struct dm_snap_pending_exception
*pe
= mempool_alloc(s
->pending_pool
,
451 atomic_inc(&s
->pending_exceptions_count
);
457 static void free_pending_exception(struct dm_snap_pending_exception
*pe
)
459 struct dm_snapshot
*s
= pe
->snap
;
461 mempool_free(pe
, s
->pending_pool
);
462 smp_mb__before_atomic_dec();
463 atomic_dec(&s
->pending_exceptions_count
);
466 static void dm_insert_exception(struct dm_exception_table
*eh
,
467 struct dm_exception
*new_e
)
470 struct dm_exception
*e
= NULL
;
472 l
= &eh
->table
[exception_hash(eh
, new_e
->old_chunk
)];
474 /* Add immediately if this table doesn't support consecutive chunks */
478 /* List is ordered by old_chunk */
479 list_for_each_entry_reverse(e
, l
, hash_list
) {
480 /* Insert after an existing chunk? */
481 if (new_e
->old_chunk
== (e
->old_chunk
+
482 dm_consecutive_chunk_count(e
) + 1) &&
483 new_e
->new_chunk
== (dm_chunk_number(e
->new_chunk
) +
484 dm_consecutive_chunk_count(e
) + 1)) {
485 dm_consecutive_chunk_count_inc(e
);
486 free_completed_exception(new_e
);
490 /* Insert before an existing chunk? */
491 if (new_e
->old_chunk
== (e
->old_chunk
- 1) &&
492 new_e
->new_chunk
== (dm_chunk_number(e
->new_chunk
) - 1)) {
493 dm_consecutive_chunk_count_inc(e
);
496 free_completed_exception(new_e
);
500 if (new_e
->old_chunk
> e
->old_chunk
)
505 list_add(&new_e
->hash_list
, e
? &e
->hash_list
: l
);
509 * Callback used by the exception stores to load exceptions when
512 static int dm_add_exception(void *context
, chunk_t old
, chunk_t
new)
514 struct dm_snapshot
*s
= context
;
515 struct dm_exception
*e
;
517 e
= alloc_completed_exception();
523 /* Consecutive_count is implicitly initialised to zero */
526 dm_insert_exception(&s
->complete
, e
);
531 #define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
534 * Return a minimum chunk size of all snapshots that have the specified origin.
535 * Return zero if the origin has no snapshots.
537 static sector_t
__minimum_chunk_size(struct origin
*o
)
539 struct dm_snapshot
*snap
;
540 unsigned chunk_size
= 0;
543 list_for_each_entry(snap
, &o
->snapshots
, list
)
544 chunk_size
= min_not_zero(chunk_size
,
545 snap
->store
->chunk_size
);
553 static int calc_max_buckets(void)
555 /* use a fixed size of 2MB */
556 unsigned long mem
= 2 * 1024 * 1024;
557 mem
/= sizeof(struct list_head
);
563 * Allocate room for a suitable hash table.
565 static int init_hash_tables(struct dm_snapshot
*s
)
567 sector_t hash_size
, cow_dev_size
, origin_dev_size
, max_buckets
;
570 * Calculate based on the size of the original volume or
573 cow_dev_size
= get_dev_size(s
->cow
->bdev
);
574 origin_dev_size
= get_dev_size(s
->origin
->bdev
);
575 max_buckets
= calc_max_buckets();
577 hash_size
= min(origin_dev_size
, cow_dev_size
) >> s
->store
->chunk_shift
;
578 hash_size
= min(hash_size
, max_buckets
);
582 hash_size
= rounddown_pow_of_two(hash_size
);
583 if (dm_exception_table_init(&s
->complete
, hash_size
,
584 DM_CHUNK_CONSECUTIVE_BITS
))
588 * Allocate hash table for in-flight exceptions
589 * Make this smaller than the real hash table
595 if (dm_exception_table_init(&s
->pending
, hash_size
, 0)) {
596 dm_exception_table_exit(&s
->complete
, exception_cache
);
604 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
606 static int snapshot_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
608 struct dm_snapshot
*s
;
611 char *origin_path
, *cow_path
;
615 ti
->error
= "requires exactly 4 arguments";
620 origin_path
= argv
[0];
624 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
626 ti
->error
= "Cannot allocate snapshot context private "
636 r
= dm_get_device(ti
, cow_path
, 0, 0,
637 FMODE_READ
| FMODE_WRITE
, &s
->cow
);
639 ti
->error
= "Cannot get COW device";
643 r
= dm_exception_store_create(ti
, argc
, argv
, s
, &args_used
, &s
->store
);
645 ti
->error
= "Couldn't create exception store";
653 r
= dm_get_device(ti
, origin_path
, 0, ti
->len
, FMODE_READ
, &s
->origin
);
655 ti
->error
= "Cannot get origin device";
663 atomic_set(&s
->pending_exceptions_count
, 0);
664 init_rwsem(&s
->lock
);
665 spin_lock_init(&s
->pe_lock
);
667 /* Allocate hash table for COW data */
668 if (init_hash_tables(s
)) {
669 ti
->error
= "Unable to allocate hash table space";
671 goto bad_hash_tables
;
674 r
= dm_kcopyd_client_create(SNAPSHOT_PAGES
, &s
->kcopyd_client
);
676 ti
->error
= "Could not create kcopyd client";
680 s
->pending_pool
= mempool_create_slab_pool(MIN_IOS
, pending_cache
);
681 if (!s
->pending_pool
) {
682 ti
->error
= "Could not allocate mempool for pending exceptions";
683 goto bad_pending_pool
;
686 s
->tracked_chunk_pool
= mempool_create_slab_pool(MIN_IOS
,
687 tracked_chunk_cache
);
688 if (!s
->tracked_chunk_pool
) {
689 ti
->error
= "Could not allocate tracked_chunk mempool for "
691 goto bad_tracked_chunk_pool
;
694 for (i
= 0; i
< DM_TRACKED_CHUNK_HASH_SIZE
; i
++)
695 INIT_HLIST_HEAD(&s
->tracked_chunk_hash
[i
]);
697 spin_lock_init(&s
->tracked_chunk_lock
);
699 /* Metadata must only be loaded into one table at once */
700 r
= s
->store
->type
->read_metadata(s
->store
, dm_add_exception
,
703 ti
->error
= "Failed to read snapshot metadata";
704 goto bad_load_and_register
;
707 DMWARN("Snapshot is marked invalid.");
710 bio_list_init(&s
->queued_bios
);
711 INIT_WORK(&s
->queued_bios_work
, flush_queued_bios
);
713 if (!s
->store
->chunk_size
) {
714 ti
->error
= "Chunk size not set";
715 goto bad_load_and_register
;
718 /* Add snapshot to the list of snapshots for this origin */
719 /* Exceptions aren't triggered till snapshot_resume() is called */
720 if (register_snapshot(s
)) {
722 ti
->error
= "Cannot register snapshot origin";
723 goto bad_load_and_register
;
727 ti
->split_io
= s
->store
->chunk_size
;
728 ti
->num_flush_requests
= 1;
732 bad_load_and_register
:
733 mempool_destroy(s
->tracked_chunk_pool
);
735 bad_tracked_chunk_pool
:
736 mempool_destroy(s
->pending_pool
);
739 dm_kcopyd_client_destroy(s
->kcopyd_client
);
742 dm_exception_table_exit(&s
->pending
, pending_cache
);
743 dm_exception_table_exit(&s
->complete
, exception_cache
);
746 dm_put_device(ti
, s
->origin
);
749 dm_exception_store_destroy(s
->store
);
752 dm_put_device(ti
, s
->cow
);
761 static void __free_exceptions(struct dm_snapshot
*s
)
763 dm_kcopyd_client_destroy(s
->kcopyd_client
);
764 s
->kcopyd_client
= NULL
;
766 dm_exception_table_exit(&s
->pending
, pending_cache
);
767 dm_exception_table_exit(&s
->complete
, exception_cache
);
770 static void snapshot_dtr(struct dm_target
*ti
)
772 #ifdef CONFIG_DM_DEBUG
775 struct dm_snapshot
*s
= ti
->private;
777 flush_workqueue(ksnapd
);
779 /* Prevent further origin writes from using this snapshot. */
780 /* After this returns there can be no new kcopyd jobs. */
781 unregister_snapshot(s
);
783 while (atomic_read(&s
->pending_exceptions_count
))
786 * Ensure instructions in mempool_destroy aren't reordered
787 * before atomic_read.
791 #ifdef CONFIG_DM_DEBUG
792 for (i
= 0; i
< DM_TRACKED_CHUNK_HASH_SIZE
; i
++)
793 BUG_ON(!hlist_empty(&s
->tracked_chunk_hash
[i
]));
796 mempool_destroy(s
->tracked_chunk_pool
);
798 __free_exceptions(s
);
800 mempool_destroy(s
->pending_pool
);
802 dm_put_device(ti
, s
->origin
);
804 dm_exception_store_destroy(s
->store
);
806 dm_put_device(ti
, s
->cow
);
812 * Flush a list of buffers.
814 static void flush_bios(struct bio
*bio
)
821 generic_make_request(bio
);
826 static void flush_queued_bios(struct work_struct
*work
)
828 struct dm_snapshot
*s
=
829 container_of(work
, struct dm_snapshot
, queued_bios_work
);
830 struct bio
*queued_bios
;
833 spin_lock_irqsave(&s
->pe_lock
, flags
);
834 queued_bios
= bio_list_get(&s
->queued_bios
);
835 spin_unlock_irqrestore(&s
->pe_lock
, flags
);
837 flush_bios(queued_bios
);
841 * Error a list of buffers.
843 static void error_bios(struct bio
*bio
)
855 static void __invalidate_snapshot(struct dm_snapshot
*s
, int err
)
861 DMERR("Invalidating snapshot: Error reading/writing.");
862 else if (err
== -ENOMEM
)
863 DMERR("Invalidating snapshot: Unable to allocate exception.");
865 if (s
->store
->type
->drop_snapshot
)
866 s
->store
->type
->drop_snapshot(s
->store
);
870 dm_table_event(s
->ti
->table
);
873 static void get_pending_exception(struct dm_snap_pending_exception
*pe
)
875 atomic_inc(&pe
->ref_count
);
878 static struct bio
*put_pending_exception(struct dm_snap_pending_exception
*pe
)
880 struct dm_snap_pending_exception
*primary_pe
;
881 struct bio
*origin_bios
= NULL
;
883 primary_pe
= pe
->primary_pe
;
886 * If this pe is involved in a write to the origin and
887 * it is the last sibling to complete then release
888 * the bios for the original write to the origin.
891 atomic_dec_and_test(&primary_pe
->ref_count
)) {
892 origin_bios
= bio_list_get(&primary_pe
->origin_bios
);
893 free_pending_exception(primary_pe
);
897 * Free the pe if it's not linked to an origin write or if
898 * it's not itself a primary pe.
900 if (!primary_pe
|| primary_pe
!= pe
)
901 free_pending_exception(pe
);
906 static void pending_complete(struct dm_snap_pending_exception
*pe
, int success
)
908 struct dm_exception
*e
;
909 struct dm_snapshot
*s
= pe
->snap
;
910 struct bio
*origin_bios
= NULL
;
911 struct bio
*snapshot_bios
= NULL
;
915 /* Read/write error - snapshot is unusable */
916 down_write(&s
->lock
);
917 __invalidate_snapshot(s
, -EIO
);
922 e
= alloc_completed_exception();
924 down_write(&s
->lock
);
925 __invalidate_snapshot(s
, -ENOMEM
);
931 down_write(&s
->lock
);
933 free_completed_exception(e
);
939 * Check for conflicting reads. This is extremely improbable,
940 * so msleep(1) is sufficient and there is no need for a wait queue.
942 while (__chunk_is_tracked(s
, pe
->e
.old_chunk
))
946 * Add a proper exception, and remove the
947 * in-flight exception from the list.
949 dm_insert_exception(&s
->complete
, e
);
952 dm_remove_exception(&pe
->e
);
953 snapshot_bios
= bio_list_get(&pe
->snapshot_bios
);
954 origin_bios
= put_pending_exception(pe
);
958 /* Submit any pending write bios */
960 error_bios(snapshot_bios
);
962 flush_bios(snapshot_bios
);
964 flush_bios(origin_bios
);
967 static void commit_callback(void *context
, int success
)
969 struct dm_snap_pending_exception
*pe
= context
;
971 pending_complete(pe
, success
);
975 * Called when the copy I/O has finished. kcopyd actually runs
976 * this code so don't block.
978 static void copy_callback(int read_err
, unsigned long write_err
, void *context
)
980 struct dm_snap_pending_exception
*pe
= context
;
981 struct dm_snapshot
*s
= pe
->snap
;
983 if (read_err
|| write_err
)
984 pending_complete(pe
, 0);
987 /* Update the metadata if we are persistent */
988 s
->store
->type
->commit_exception(s
->store
, &pe
->e
,
989 commit_callback
, pe
);
993 * Dispatches the copy operation to kcopyd.
995 static void start_copy(struct dm_snap_pending_exception
*pe
)
997 struct dm_snapshot
*s
= pe
->snap
;
998 struct dm_io_region src
, dest
;
999 struct block_device
*bdev
= s
->origin
->bdev
;
1002 dev_size
= get_dev_size(bdev
);
1005 src
.sector
= chunk_to_sector(s
->store
, pe
->e
.old_chunk
);
1006 src
.count
= min((sector_t
)s
->store
->chunk_size
, dev_size
- src
.sector
);
1008 dest
.bdev
= s
->cow
->bdev
;
1009 dest
.sector
= chunk_to_sector(s
->store
, pe
->e
.new_chunk
);
1010 dest
.count
= src
.count
;
1012 /* Hand over to kcopyd */
1013 dm_kcopyd_copy(s
->kcopyd_client
,
1014 &src
, 1, &dest
, 0, copy_callback
, pe
);
1017 static struct dm_snap_pending_exception
*
1018 __lookup_pending_exception(struct dm_snapshot
*s
, chunk_t chunk
)
1020 struct dm_exception
*e
= dm_lookup_exception(&s
->pending
, chunk
);
1025 return container_of(e
, struct dm_snap_pending_exception
, e
);
1029 * Looks to see if this snapshot already has a pending exception
1030 * for this chunk, otherwise it allocates a new one and inserts
1031 * it into the pending table.
1033 * NOTE: a write lock must be held on snap->lock before calling
1036 static struct dm_snap_pending_exception
*
1037 __find_pending_exception(struct dm_snapshot
*s
,
1038 struct dm_snap_pending_exception
*pe
, chunk_t chunk
)
1040 struct dm_snap_pending_exception
*pe2
;
1042 pe2
= __lookup_pending_exception(s
, chunk
);
1044 free_pending_exception(pe
);
1048 pe
->e
.old_chunk
= chunk
;
1049 bio_list_init(&pe
->origin_bios
);
1050 bio_list_init(&pe
->snapshot_bios
);
1051 pe
->primary_pe
= NULL
;
1052 atomic_set(&pe
->ref_count
, 0);
1055 if (s
->store
->type
->prepare_exception(s
->store
, &pe
->e
)) {
1056 free_pending_exception(pe
);
1060 get_pending_exception(pe
);
1061 dm_insert_exception(&s
->pending
, &pe
->e
);
1066 static void remap_exception(struct dm_snapshot
*s
, struct dm_exception
*e
,
1067 struct bio
*bio
, chunk_t chunk
)
1069 bio
->bi_bdev
= s
->cow
->bdev
;
1070 bio
->bi_sector
= chunk_to_sector(s
->store
,
1071 dm_chunk_number(e
->new_chunk
) +
1072 (chunk
- e
->old_chunk
)) +
1074 s
->store
->chunk_mask
);
1077 static int snapshot_map(struct dm_target
*ti
, struct bio
*bio
,
1078 union map_info
*map_context
)
1080 struct dm_exception
*e
;
1081 struct dm_snapshot
*s
= ti
->private;
1082 int r
= DM_MAPIO_REMAPPED
;
1084 struct dm_snap_pending_exception
*pe
= NULL
;
1086 if (unlikely(bio_empty_barrier(bio
))) {
1087 bio
->bi_bdev
= s
->cow
->bdev
;
1088 return DM_MAPIO_REMAPPED
;
1091 chunk
= sector_to_chunk(s
->store
, bio
->bi_sector
);
1093 /* Full snapshots are not usable */
1094 /* To get here the table must be live so s->active is always set. */
1098 /* FIXME: should only take write lock if we need
1099 * to copy an exception */
1100 down_write(&s
->lock
);
1107 /* If the block is already remapped - use that, else remap it */
1108 e
= dm_lookup_exception(&s
->complete
, chunk
);
1110 remap_exception(s
, e
, bio
, chunk
);
1115 * Write to snapshot - higher level takes care of RW/RO
1116 * flags so we should only get this if we are
1119 if (bio_rw(bio
) == WRITE
) {
1120 pe
= __lookup_pending_exception(s
, chunk
);
1123 pe
= alloc_pending_exception(s
);
1124 down_write(&s
->lock
);
1127 free_pending_exception(pe
);
1132 e
= dm_lookup_exception(&s
->complete
, chunk
);
1134 free_pending_exception(pe
);
1135 remap_exception(s
, e
, bio
, chunk
);
1139 pe
= __find_pending_exception(s
, pe
, chunk
);
1141 __invalidate_snapshot(s
, -ENOMEM
);
1147 remap_exception(s
, &pe
->e
, bio
, chunk
);
1148 bio_list_add(&pe
->snapshot_bios
, bio
);
1150 r
= DM_MAPIO_SUBMITTED
;
1153 /* this is protected by snap->lock */
1160 bio
->bi_bdev
= s
->origin
->bdev
;
1161 map_context
->ptr
= track_chunk(s
, chunk
);
1170 static int snapshot_end_io(struct dm_target
*ti
, struct bio
*bio
,
1171 int error
, union map_info
*map_context
)
1173 struct dm_snapshot
*s
= ti
->private;
1174 struct dm_snap_tracked_chunk
*c
= map_context
->ptr
;
1177 stop_tracking_chunk(s
, c
);
1182 static void snapshot_postsuspend(struct dm_target
*ti
)
1184 struct dm_snapshot
*s
= ti
->private;
1186 down_write(&s
->lock
);
1191 static void snapshot_resume(struct dm_target
*ti
)
1193 struct dm_snapshot
*s
= ti
->private;
1195 down_write(&s
->lock
);
1201 static int snapshot_status(struct dm_target
*ti
, status_type_t type
,
1202 char *result
, unsigned int maxlen
)
1205 struct dm_snapshot
*snap
= ti
->private;
1208 case STATUSTYPE_INFO
:
1210 down_write(&snap
->lock
);
1215 if (snap
->store
->type
->usage
) {
1216 sector_t total_sectors
, sectors_allocated
,
1218 snap
->store
->type
->usage(snap
->store
,
1222 DMEMIT("%llu/%llu %llu",
1223 (unsigned long long)sectors_allocated
,
1224 (unsigned long long)total_sectors
,
1225 (unsigned long long)metadata_sectors
);
1231 up_write(&snap
->lock
);
1235 case STATUSTYPE_TABLE
:
1237 * kdevname returns a static pointer so we need
1238 * to make private copies if the output is to
1241 DMEMIT("%s %s", snap
->origin
->name
, snap
->cow
->name
);
1242 snap
->store
->type
->status(snap
->store
, type
, result
+ sz
,
1250 static int snapshot_iterate_devices(struct dm_target
*ti
,
1251 iterate_devices_callout_fn fn
, void *data
)
1253 struct dm_snapshot
*snap
= ti
->private;
1255 return fn(ti
, snap
->origin
, 0, ti
->len
, data
);
1259 /*-----------------------------------------------------------------
1261 *---------------------------------------------------------------*/
1262 static int __origin_write(struct list_head
*snapshots
, struct bio
*bio
)
1264 int r
= DM_MAPIO_REMAPPED
, first
= 0;
1265 struct dm_snapshot
*snap
;
1266 struct dm_exception
*e
;
1267 struct dm_snap_pending_exception
*pe
, *next_pe
, *primary_pe
= NULL
;
1269 LIST_HEAD(pe_queue
);
1271 /* Do all the snapshots on this origin */
1272 list_for_each_entry (snap
, snapshots
, list
) {
1274 down_write(&snap
->lock
);
1276 /* Only deal with valid and active snapshots */
1277 if (!snap
->valid
|| !snap
->active
)
1280 /* Nothing to do if writing beyond end of snapshot */
1281 if (bio
->bi_sector
>= dm_table_get_size(snap
->ti
->table
))
1285 * Remember, different snapshots can have
1286 * different chunk sizes.
1288 chunk
= sector_to_chunk(snap
->store
, bio
->bi_sector
);
1291 * Check exception table to see if block
1292 * is already remapped in this snapshot
1293 * and trigger an exception if not.
1295 * ref_count is initialised to 1 so pending_complete()
1296 * won't destroy the primary_pe while we're inside this loop.
1298 e
= dm_lookup_exception(&snap
->complete
, chunk
);
1302 pe
= __lookup_pending_exception(snap
, chunk
);
1304 up_write(&snap
->lock
);
1305 pe
= alloc_pending_exception(snap
);
1306 down_write(&snap
->lock
);
1309 free_pending_exception(pe
);
1313 e
= dm_lookup_exception(&snap
->complete
, chunk
);
1315 free_pending_exception(pe
);
1319 pe
= __find_pending_exception(snap
, pe
, chunk
);
1321 __invalidate_snapshot(snap
, -ENOMEM
);
1328 * Either every pe here has same
1329 * primary_pe or none has one yet.
1332 primary_pe
= pe
->primary_pe
;
1338 bio_list_add(&primary_pe
->origin_bios
, bio
);
1340 r
= DM_MAPIO_SUBMITTED
;
1343 if (!pe
->primary_pe
) {
1344 pe
->primary_pe
= primary_pe
;
1345 get_pending_exception(primary_pe
);
1350 list_add_tail(&pe
->list
, &pe_queue
);
1354 up_write(&snap
->lock
);
1361 * If this is the first time we're processing this chunk and
1362 * ref_count is now 1 it means all the pending exceptions
1363 * got completed while we were in the loop above, so it falls to
1364 * us here to remove the primary_pe and submit any origin_bios.
1367 if (first
&& atomic_dec_and_test(&primary_pe
->ref_count
)) {
1368 flush_bios(bio_list_get(&primary_pe
->origin_bios
));
1369 free_pending_exception(primary_pe
);
1370 /* If we got here, pe_queue is necessarily empty. */
1375 * Now that we have a complete pe list we can start the copying.
1377 list_for_each_entry_safe(pe
, next_pe
, &pe_queue
, list
)
1384 * Called on a write from the origin driver.
1386 static int do_origin(struct dm_dev
*origin
, struct bio
*bio
)
1389 int r
= DM_MAPIO_REMAPPED
;
1391 down_read(&_origins_lock
);
1392 o
= __lookup_origin(origin
->bdev
);
1394 r
= __origin_write(&o
->snapshots
, bio
);
1395 up_read(&_origins_lock
);
1401 * Origin: maps a linear range of a device, with hooks for snapshotting.
1405 * Construct an origin mapping: <dev_path>
1406 * The context for an origin is merely a 'struct dm_dev *'
1407 * pointing to the real device.
1409 static int origin_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
1415 ti
->error
= "origin: incorrect number of arguments";
1419 r
= dm_get_device(ti
, argv
[0], 0, ti
->len
,
1420 dm_table_get_mode(ti
->table
), &dev
);
1422 ti
->error
= "Cannot get target device";
1427 ti
->num_flush_requests
= 1;
1432 static void origin_dtr(struct dm_target
*ti
)
1434 struct dm_dev
*dev
= ti
->private;
1435 dm_put_device(ti
, dev
);
1438 static int origin_map(struct dm_target
*ti
, struct bio
*bio
,
1439 union map_info
*map_context
)
1441 struct dm_dev
*dev
= ti
->private;
1442 bio
->bi_bdev
= dev
->bdev
;
1444 if (unlikely(bio_empty_barrier(bio
)))
1445 return DM_MAPIO_REMAPPED
;
1447 /* Only tell snapshots if this is a write */
1448 return (bio_rw(bio
) == WRITE
) ? do_origin(dev
, bio
) : DM_MAPIO_REMAPPED
;
1452 * Set the target "split_io" field to the minimum of all the snapshots'
1455 static void origin_resume(struct dm_target
*ti
)
1457 struct dm_dev
*dev
= ti
->private;
1459 down_read(&_origins_lock
);
1461 ti
->split_io
= __minimum_chunk_size(__lookup_origin(dev
->bdev
));
1463 up_read(&_origins_lock
);
1466 static int origin_status(struct dm_target
*ti
, status_type_t type
, char *result
,
1467 unsigned int maxlen
)
1469 struct dm_dev
*dev
= ti
->private;
1472 case STATUSTYPE_INFO
:
1476 case STATUSTYPE_TABLE
:
1477 snprintf(result
, maxlen
, "%s", dev
->name
);
1484 static int origin_iterate_devices(struct dm_target
*ti
,
1485 iterate_devices_callout_fn fn
, void *data
)
1487 struct dm_dev
*dev
= ti
->private;
1489 return fn(ti
, dev
, 0, ti
->len
, data
);
1492 static struct target_type origin_target
= {
1493 .name
= "snapshot-origin",
1494 .version
= {1, 7, 0},
1495 .module
= THIS_MODULE
,
1499 .resume
= origin_resume
,
1500 .status
= origin_status
,
1501 .iterate_devices
= origin_iterate_devices
,
1504 static struct target_type snapshot_target
= {
1506 .version
= {1, 9, 0},
1507 .module
= THIS_MODULE
,
1508 .ctr
= snapshot_ctr
,
1509 .dtr
= snapshot_dtr
,
1510 .map
= snapshot_map
,
1511 .end_io
= snapshot_end_io
,
1512 .postsuspend
= snapshot_postsuspend
,
1513 .resume
= snapshot_resume
,
1514 .status
= snapshot_status
,
1515 .iterate_devices
= snapshot_iterate_devices
,
1518 static int __init
dm_snapshot_init(void)
1522 r
= dm_exception_store_init();
1524 DMERR("Failed to initialize exception stores");
1528 r
= dm_register_target(&snapshot_target
);
1530 DMERR("snapshot target register failed %d", r
);
1531 goto bad_register_snapshot_target
;
1534 r
= dm_register_target(&origin_target
);
1536 DMERR("Origin target register failed %d", r
);
1540 r
= init_origin_hash();
1542 DMERR("init_origin_hash failed.");
1546 exception_cache
= KMEM_CACHE(dm_exception
, 0);
1547 if (!exception_cache
) {
1548 DMERR("Couldn't create exception cache.");
1553 pending_cache
= KMEM_CACHE(dm_snap_pending_exception
, 0);
1554 if (!pending_cache
) {
1555 DMERR("Couldn't create pending cache.");
1560 tracked_chunk_cache
= KMEM_CACHE(dm_snap_tracked_chunk
, 0);
1561 if (!tracked_chunk_cache
) {
1562 DMERR("Couldn't create cache to track chunks in use.");
1567 ksnapd
= create_singlethread_workqueue("ksnapd");
1569 DMERR("Failed to create ksnapd workqueue.");
1571 goto bad_pending_pool
;
1577 kmem_cache_destroy(tracked_chunk_cache
);
1579 kmem_cache_destroy(pending_cache
);
1581 kmem_cache_destroy(exception_cache
);
1585 dm_unregister_target(&origin_target
);
1587 dm_unregister_target(&snapshot_target
);
1589 bad_register_snapshot_target
:
1590 dm_exception_store_exit();
1594 static void __exit
dm_snapshot_exit(void)
1596 destroy_workqueue(ksnapd
);
1598 dm_unregister_target(&snapshot_target
);
1599 dm_unregister_target(&origin_target
);
1602 kmem_cache_destroy(pending_cache
);
1603 kmem_cache_destroy(exception_cache
);
1604 kmem_cache_destroy(tracked_chunk_cache
);
1606 dm_exception_store_exit();
1610 module_init(dm_snapshot_init
);
1611 module_exit(dm_snapshot_exit
);
1613 MODULE_DESCRIPTION(DM_NAME
" snapshot target");
1614 MODULE_AUTHOR("Joe Thornber");
1615 MODULE_LICENSE("GPL");