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/config.h>
11 #include <linux/ctype.h>
12 #include <linux/device-mapper.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>
23 #include "dm-bio-list.h"
27 * The percentage increment we will wake up users at
29 #define WAKE_UP_PERCENT 5
32 * kcopyd priority of snapshot operations
34 #define SNAPSHOT_COPY_PRIORITY 2
37 * Each snapshot reserves this many pages for io
39 #define SNAPSHOT_PAGES 256
41 struct pending_exception
{
45 * Origin buffers waiting for this to complete are held
48 struct bio_list origin_bios
;
49 struct bio_list snapshot_bios
;
52 * Short-term queue of pending exceptions prior to submission.
54 struct list_head list
;
57 * The primary pending_exception is the one that holds
58 * the sibling_count and the list of origin_bios for a
59 * group of pending_exceptions. It is always last to get freed.
60 * These fields get set up when writing to the origin.
62 struct pending_exception
*primary_pe
;
65 * Number of pending_exceptions processing this chunk.
66 * When this drops to zero we must complete the origin bios.
67 * If incrementing or decrementing this, hold pe->snap->lock for
68 * the sibling concerned and not pe->primary_pe->snap->lock unless
71 atomic_t sibling_count
;
73 /* Pointer back to snapshot context */
74 struct dm_snapshot
*snap
;
77 * 1 indicates the exception has already been sent to
84 * Hash table mapping origin volumes to lists of snapshots and
85 * a lock to protect it
87 static kmem_cache_t
*exception_cache
;
88 static kmem_cache_t
*pending_cache
;
89 static mempool_t
*pending_pool
;
92 * One of these per registered origin, held in the snapshot_origins hash
95 /* The origin device */
96 struct block_device
*bdev
;
98 struct list_head hash_list
;
100 /* List of snapshots for this origin */
101 struct list_head snapshots
;
105 * Size of the hash table for origin volumes. If we make this
106 * the size of the minors list then it should be nearly perfect
108 #define ORIGIN_HASH_SIZE 256
109 #define ORIGIN_MASK 0xFF
110 static struct list_head
*_origins
;
111 static struct rw_semaphore _origins_lock
;
113 static int init_origin_hash(void)
117 _origins
= kmalloc(ORIGIN_HASH_SIZE
* sizeof(struct list_head
),
120 DMERR("Device mapper: Snapshot: unable to allocate memory");
124 for (i
= 0; i
< ORIGIN_HASH_SIZE
; i
++)
125 INIT_LIST_HEAD(_origins
+ i
);
126 init_rwsem(&_origins_lock
);
131 static void exit_origin_hash(void)
136 static inline unsigned int origin_hash(struct block_device
*bdev
)
138 return bdev
->bd_dev
& ORIGIN_MASK
;
141 static struct origin
*__lookup_origin(struct block_device
*origin
)
143 struct list_head
*ol
;
146 ol
= &_origins
[origin_hash(origin
)];
147 list_for_each_entry (o
, ol
, hash_list
)
148 if (bdev_equal(o
->bdev
, origin
))
154 static void __insert_origin(struct origin
*o
)
156 struct list_head
*sl
= &_origins
[origin_hash(o
->bdev
)];
157 list_add_tail(&o
->hash_list
, sl
);
161 * Make a note of the snapshot and its origin so we can look it
162 * up when the origin has a write on it.
164 static int register_snapshot(struct dm_snapshot
*snap
)
167 struct block_device
*bdev
= snap
->origin
->bdev
;
169 down_write(&_origins_lock
);
170 o
= __lookup_origin(bdev
);
174 o
= kmalloc(sizeof(*o
), GFP_KERNEL
);
176 up_write(&_origins_lock
);
180 /* Initialise the struct */
181 INIT_LIST_HEAD(&o
->snapshots
);
187 list_add_tail(&snap
->list
, &o
->snapshots
);
189 up_write(&_origins_lock
);
193 static void unregister_snapshot(struct dm_snapshot
*s
)
197 down_write(&_origins_lock
);
198 o
= __lookup_origin(s
->origin
->bdev
);
201 if (list_empty(&o
->snapshots
)) {
202 list_del(&o
->hash_list
);
206 up_write(&_origins_lock
);
210 * Implementation of the exception hash tables.
212 static int init_exception_table(struct exception_table
*et
, uint32_t size
)
216 et
->hash_mask
= size
- 1;
217 et
->table
= dm_vcalloc(size
, sizeof(struct list_head
));
221 for (i
= 0; i
< size
; i
++)
222 INIT_LIST_HEAD(et
->table
+ i
);
227 static void exit_exception_table(struct exception_table
*et
, kmem_cache_t
*mem
)
229 struct list_head
*slot
;
230 struct exception
*ex
, *next
;
233 size
= et
->hash_mask
+ 1;
234 for (i
= 0; i
< size
; i
++) {
235 slot
= et
->table
+ i
;
237 list_for_each_entry_safe (ex
, next
, slot
, hash_list
)
238 kmem_cache_free(mem
, ex
);
244 static inline uint32_t exception_hash(struct exception_table
*et
, chunk_t chunk
)
246 return chunk
& et
->hash_mask
;
249 static void insert_exception(struct exception_table
*eh
, struct exception
*e
)
251 struct list_head
*l
= &eh
->table
[exception_hash(eh
, e
->old_chunk
)];
252 list_add(&e
->hash_list
, l
);
255 static inline void remove_exception(struct exception
*e
)
257 list_del(&e
->hash_list
);
261 * Return the exception data for a sector, or NULL if not
264 static struct exception
*lookup_exception(struct exception_table
*et
,
267 struct list_head
*slot
;
270 slot
= &et
->table
[exception_hash(et
, chunk
)];
271 list_for_each_entry (e
, slot
, hash_list
)
272 if (e
->old_chunk
== chunk
)
278 static inline struct exception
*alloc_exception(void)
282 e
= kmem_cache_alloc(exception_cache
, GFP_NOIO
);
284 e
= kmem_cache_alloc(exception_cache
, GFP_ATOMIC
);
289 static inline void free_exception(struct exception
*e
)
291 kmem_cache_free(exception_cache
, e
);
294 static inline struct pending_exception
*alloc_pending_exception(void)
296 return mempool_alloc(pending_pool
, GFP_NOIO
);
299 static inline void free_pending_exception(struct pending_exception
*pe
)
301 mempool_free(pe
, pending_pool
);
304 int dm_add_exception(struct dm_snapshot
*s
, chunk_t old
, chunk_t
new)
308 e
= alloc_exception();
314 insert_exception(&s
->complete
, e
);
321 static int calc_max_buckets(void)
323 /* use a fixed size of 2MB */
324 unsigned long mem
= 2 * 1024 * 1024;
325 mem
/= sizeof(struct list_head
);
331 * Rounds a number down to a power of 2.
333 static inline uint32_t round_down(uint32_t n
)
341 * Allocate room for a suitable hash table.
343 static int init_hash_tables(struct dm_snapshot
*s
)
345 sector_t hash_size
, cow_dev_size
, origin_dev_size
, max_buckets
;
348 * Calculate based on the size of the original volume or
351 cow_dev_size
= get_dev_size(s
->cow
->bdev
);
352 origin_dev_size
= get_dev_size(s
->origin
->bdev
);
353 max_buckets
= calc_max_buckets();
355 hash_size
= min(origin_dev_size
, cow_dev_size
) >> s
->chunk_shift
;
356 hash_size
= min(hash_size
, max_buckets
);
358 /* Round it down to a power of 2 */
359 hash_size
= round_down(hash_size
);
360 if (init_exception_table(&s
->complete
, hash_size
))
364 * Allocate hash table for in-flight exceptions
365 * Make this smaller than the real hash table
371 if (init_exception_table(&s
->pending
, hash_size
)) {
372 exit_exception_table(&s
->complete
, exception_cache
);
380 * Round a number up to the nearest 'size' boundary. size must
383 static inline ulong
round_up(ulong n
, ulong size
)
386 return (n
+ size
) & ~size
;
389 static void read_snapshot_metadata(struct dm_snapshot
*s
)
391 if (s
->store
.read_metadata(&s
->store
)) {
392 down_write(&s
->lock
);
396 dm_table_event(s
->table
);
401 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
403 static int snapshot_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
405 struct dm_snapshot
*s
;
406 unsigned long chunk_size
;
415 ti
->error
= "dm-snapshot: requires exactly 4 arguments";
420 origin_path
= argv
[0];
422 persistent
= toupper(*argv
[2]);
424 if (persistent
!= 'P' && persistent
!= 'N') {
425 ti
->error
= "Persistent flag is not P or N";
430 chunk_size
= simple_strtoul(argv
[3], &value
, 10);
431 if (chunk_size
== 0 || value
== NULL
) {
432 ti
->error
= "Invalid chunk size";
437 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
439 ti
->error
= "Cannot allocate snapshot context private "
445 r
= dm_get_device(ti
, origin_path
, 0, ti
->len
, FMODE_READ
, &s
->origin
);
447 ti
->error
= "Cannot get origin device";
451 r
= dm_get_device(ti
, cow_path
, 0, 0,
452 FMODE_READ
| FMODE_WRITE
, &s
->cow
);
454 dm_put_device(ti
, s
->origin
);
455 ti
->error
= "Cannot get COW device";
460 * Chunk size must be multiple of page size. Silently
461 * round up if it's not.
463 chunk_size
= round_up(chunk_size
, PAGE_SIZE
>> 9);
465 /* Validate the chunk size against the device block size */
466 blocksize
= s
->cow
->bdev
->bd_disk
->queue
->hardsect_size
;
467 if (chunk_size
% (blocksize
>> 9)) {
468 ti
->error
= "Chunk size is not a multiple of device blocksize";
473 /* Check chunk_size is a power of 2 */
474 if (chunk_size
& (chunk_size
- 1)) {
475 ti
->error
= "Chunk size is not a power of 2";
480 s
->chunk_size
= chunk_size
;
481 s
->chunk_mask
= chunk_size
- 1;
482 s
->type
= persistent
;
483 s
->chunk_shift
= ffs(chunk_size
) - 1;
488 init_rwsem(&s
->lock
);
489 s
->table
= ti
->table
;
491 /* Allocate hash table for COW data */
492 if (init_hash_tables(s
)) {
493 ti
->error
= "Unable to allocate hash table space";
499 * Check the persistent flag - done here because we need the iobuf
500 * to check the LV header
504 if (persistent
== 'P')
505 r
= dm_create_persistent(&s
->store
, chunk_size
);
507 r
= dm_create_transient(&s
->store
, s
, blocksize
);
510 ti
->error
= "Couldn't create exception store";
515 r
= kcopyd_client_create(SNAPSHOT_PAGES
, &s
->kcopyd_client
);
517 ti
->error
= "Could not create kcopyd client";
521 /* Metadata must only be loaded into one table at once */
522 read_snapshot_metadata(s
);
524 /* Add snapshot to the list of snapshots for this origin */
525 /* Exceptions aren't triggered till snapshot_resume() is called */
526 if (register_snapshot(s
)) {
528 ti
->error
= "Cannot register snapshot origin";
533 ti
->split_io
= chunk_size
;
538 kcopyd_client_destroy(s
->kcopyd_client
);
541 s
->store
.destroy(&s
->store
);
544 exit_exception_table(&s
->pending
, pending_cache
);
545 exit_exception_table(&s
->complete
, exception_cache
);
548 dm_put_device(ti
, s
->cow
);
549 dm_put_device(ti
, s
->origin
);
558 static void snapshot_dtr(struct dm_target
*ti
)
560 struct dm_snapshot
*s
= (struct dm_snapshot
*) ti
->private;
562 /* Prevent further origin writes from using this snapshot. */
563 /* After this returns there can be no new kcopyd jobs. */
564 unregister_snapshot(s
);
566 kcopyd_client_destroy(s
->kcopyd_client
);
568 exit_exception_table(&s
->pending
, pending_cache
);
569 exit_exception_table(&s
->complete
, exception_cache
);
571 /* Deallocate memory used */
572 s
->store
.destroy(&s
->store
);
574 dm_put_device(ti
, s
->origin
);
575 dm_put_device(ti
, s
->cow
);
581 * Flush a list of buffers.
583 static void flush_bios(struct bio
*bio
)
590 generic_make_request(bio
);
596 * Error a list of buffers.
598 static void error_bios(struct bio
*bio
)
605 bio_io_error(bio
, bio
->bi_size
);
610 static inline void error_snapshot_bios(struct pending_exception
*pe
)
612 error_bios(bio_list_get(&pe
->snapshot_bios
));
615 static struct bio
*__flush_bios(struct pending_exception
*pe
)
618 * If this pe is involved in a write to the origin and
619 * it is the last sibling to complete then release
620 * the bios for the original write to the origin.
623 if (pe
->primary_pe
&&
624 atomic_dec_and_test(&pe
->primary_pe
->sibling_count
))
625 return bio_list_get(&pe
->primary_pe
->origin_bios
);
630 static void __invalidate_snapshot(struct dm_snapshot
*s
,
631 struct pending_exception
*pe
, int err
)
637 DMERR("Invalidating snapshot: Error reading/writing.");
638 else if (err
== -ENOMEM
)
639 DMERR("Invalidating snapshot: Unable to allocate exception.");
642 remove_exception(&pe
->e
);
644 if (s
->store
.drop_snapshot
)
645 s
->store
.drop_snapshot(&s
->store
);
649 dm_table_event(s
->table
);
652 static void pending_complete(struct pending_exception
*pe
, int success
)
655 struct pending_exception
*primary_pe
;
656 struct dm_snapshot
*s
= pe
->snap
;
657 struct bio
*flush
= NULL
;
660 /* Read/write error - snapshot is unusable */
661 down_write(&s
->lock
);
662 __invalidate_snapshot(s
, pe
, -EIO
);
663 flush
= __flush_bios(pe
);
666 error_snapshot_bios(pe
);
670 e
= alloc_exception();
672 down_write(&s
->lock
);
673 __invalidate_snapshot(s
, pe
, -ENOMEM
);
674 flush
= __flush_bios(pe
);
677 error_snapshot_bios(pe
);
683 * Add a proper exception, and remove the
684 * in-flight exception from the list.
686 down_write(&s
->lock
);
688 flush
= __flush_bios(pe
);
693 error_snapshot_bios(pe
);
697 insert_exception(&s
->complete
, e
);
698 remove_exception(&pe
->e
);
699 flush
= __flush_bios(pe
);
703 /* Submit any pending write bios */
704 flush_bios(bio_list_get(&pe
->snapshot_bios
));
707 primary_pe
= pe
->primary_pe
;
710 * Free the pe if it's not linked to an origin write or if
711 * it's not itself a primary pe.
713 if (!primary_pe
|| primary_pe
!= pe
)
714 free_pending_exception(pe
);
717 * Free the primary pe if nothing references it.
719 if (primary_pe
&& !atomic_read(&primary_pe
->sibling_count
))
720 free_pending_exception(primary_pe
);
726 static void commit_callback(void *context
, int success
)
728 struct pending_exception
*pe
= (struct pending_exception
*) context
;
729 pending_complete(pe
, success
);
733 * Called when the copy I/O has finished. kcopyd actually runs
734 * this code so don't block.
736 static void copy_callback(int read_err
, unsigned int write_err
, void *context
)
738 struct pending_exception
*pe
= (struct pending_exception
*) context
;
739 struct dm_snapshot
*s
= pe
->snap
;
741 if (read_err
|| write_err
)
742 pending_complete(pe
, 0);
745 /* Update the metadata if we are persistent */
746 s
->store
.commit_exception(&s
->store
, &pe
->e
, commit_callback
,
751 * Dispatches the copy operation to kcopyd.
753 static void start_copy(struct pending_exception
*pe
)
755 struct dm_snapshot
*s
= pe
->snap
;
756 struct io_region src
, dest
;
757 struct block_device
*bdev
= s
->origin
->bdev
;
760 dev_size
= get_dev_size(bdev
);
763 src
.sector
= chunk_to_sector(s
, pe
->e
.old_chunk
);
764 src
.count
= min(s
->chunk_size
, dev_size
- src
.sector
);
766 dest
.bdev
= s
->cow
->bdev
;
767 dest
.sector
= chunk_to_sector(s
, pe
->e
.new_chunk
);
768 dest
.count
= src
.count
;
770 /* Hand over to kcopyd */
771 kcopyd_copy(s
->kcopyd_client
,
772 &src
, 1, &dest
, 0, copy_callback
, pe
);
776 * Looks to see if this snapshot already has a pending exception
777 * for this chunk, otherwise it allocates a new one and inserts
778 * it into the pending table.
780 * NOTE: a write lock must be held on snap->lock before calling
783 static struct pending_exception
*
784 __find_pending_exception(struct dm_snapshot
*s
, struct bio
*bio
)
787 struct pending_exception
*pe
;
788 chunk_t chunk
= sector_to_chunk(s
, bio
->bi_sector
);
791 * Is there a pending exception for this already ?
793 e
= lookup_exception(&s
->pending
, chunk
);
795 /* cast the exception to a pending exception */
796 pe
= container_of(e
, struct pending_exception
, e
);
801 * Create a new pending exception, we don't want
802 * to hold the lock while we do this.
805 pe
= alloc_pending_exception();
806 down_write(&s
->lock
);
809 free_pending_exception(pe
);
813 e
= lookup_exception(&s
->pending
, chunk
);
815 free_pending_exception(pe
);
816 pe
= container_of(e
, struct pending_exception
, e
);
820 pe
->e
.old_chunk
= chunk
;
821 bio_list_init(&pe
->origin_bios
);
822 bio_list_init(&pe
->snapshot_bios
);
823 pe
->primary_pe
= NULL
;
824 atomic_set(&pe
->sibling_count
, 1);
828 if (s
->store
.prepare_exception(&s
->store
, &pe
->e
)) {
829 free_pending_exception(pe
);
833 insert_exception(&s
->pending
, &pe
->e
);
839 static inline void remap_exception(struct dm_snapshot
*s
, struct exception
*e
,
842 bio
->bi_bdev
= s
->cow
->bdev
;
843 bio
->bi_sector
= chunk_to_sector(s
, e
->new_chunk
) +
844 (bio
->bi_sector
& s
->chunk_mask
);
847 static int snapshot_map(struct dm_target
*ti
, struct bio
*bio
,
848 union map_info
*map_context
)
851 struct dm_snapshot
*s
= (struct dm_snapshot
*) ti
->private;
855 struct pending_exception
*pe
= NULL
;
857 chunk
= sector_to_chunk(s
, bio
->bi_sector
);
859 /* Full snapshots are not usable */
860 /* To get here the table must be live so s->active is always set. */
864 if (unlikely(bio_barrier(bio
)))
868 * Write to snapshot - higher level takes care of RW/RO
869 * flags so we should only get this if we are
872 if (bio_rw(bio
) == WRITE
) {
874 /* FIXME: should only take write lock if we need
875 * to copy an exception */
876 down_write(&s
->lock
);
883 /* If the block is already remapped - use that, else remap it */
884 e
= lookup_exception(&s
->complete
, chunk
);
886 remap_exception(s
, e
, bio
);
890 pe
= __find_pending_exception(s
, bio
);
892 __invalidate_snapshot(s
, pe
, -ENOMEM
);
897 remap_exception(s
, &pe
->e
, bio
);
898 bio_list_add(&pe
->snapshot_bios
, bio
);
901 /* this is protected by snap->lock */
915 * FIXME: this read path scares me because we
916 * always use the origin when we have a pending
917 * exception. However I can't think of a
918 * situation where this is wrong - ejt.
929 /* See if it it has been remapped */
930 e
= lookup_exception(&s
->complete
, chunk
);
932 remap_exception(s
, e
, bio
);
934 bio
->bi_bdev
= s
->origin
->bdev
;
942 static void snapshot_resume(struct dm_target
*ti
)
944 struct dm_snapshot
*s
= (struct dm_snapshot
*) ti
->private;
946 down_write(&s
->lock
);
951 static int snapshot_status(struct dm_target
*ti
, status_type_t type
,
952 char *result
, unsigned int maxlen
)
954 struct dm_snapshot
*snap
= (struct dm_snapshot
*) ti
->private;
957 case STATUSTYPE_INFO
:
959 snprintf(result
, maxlen
, "Invalid");
961 if (snap
->store
.fraction_full
) {
962 sector_t numerator
, denominator
;
963 snap
->store
.fraction_full(&snap
->store
,
966 snprintf(result
, maxlen
, "%llu/%llu",
967 (unsigned long long)numerator
,
968 (unsigned long long)denominator
);
971 snprintf(result
, maxlen
, "Unknown");
975 case STATUSTYPE_TABLE
:
977 * kdevname returns a static pointer so we need
978 * to make private copies if the output is to
981 snprintf(result
, maxlen
, "%s %s %c %llu",
982 snap
->origin
->name
, snap
->cow
->name
,
984 (unsigned long long)snap
->chunk_size
);
991 /*-----------------------------------------------------------------
993 *---------------------------------------------------------------*/
994 static int __origin_write(struct list_head
*snapshots
, struct bio
*bio
)
996 int r
= 1, first
= 0;
997 struct dm_snapshot
*snap
;
999 struct pending_exception
*pe
, *next_pe
, *primary_pe
= NULL
;
1001 LIST_HEAD(pe_queue
);
1003 /* Do all the snapshots on this origin */
1004 list_for_each_entry (snap
, snapshots
, list
) {
1006 down_write(&snap
->lock
);
1008 /* Only deal with valid and active snapshots */
1009 if (!snap
->valid
|| !snap
->active
)
1012 /* Nothing to do if writing beyond end of snapshot */
1013 if (bio
->bi_sector
>= dm_table_get_size(snap
->table
))
1017 * Remember, different snapshots can have
1018 * different chunk sizes.
1020 chunk
= sector_to_chunk(snap
, bio
->bi_sector
);
1023 * Check exception table to see if block
1024 * is already remapped in this snapshot
1025 * and trigger an exception if not.
1027 * sibling_count is initialised to 1 so pending_complete()
1028 * won't destroy the primary_pe while we're inside this loop.
1030 e
= lookup_exception(&snap
->complete
, chunk
);
1034 pe
= __find_pending_exception(snap
, bio
);
1036 __invalidate_snapshot(snap
, pe
, ENOMEM
);
1042 * Either every pe here has same
1043 * primary_pe or none has one yet.
1046 primary_pe
= pe
->primary_pe
;
1052 bio_list_add(&primary_pe
->origin_bios
, bio
);
1057 if (!pe
->primary_pe
) {
1058 atomic_inc(&primary_pe
->sibling_count
);
1059 pe
->primary_pe
= primary_pe
;
1064 list_add_tail(&pe
->list
, &pe_queue
);
1068 up_write(&snap
->lock
);
1075 * If this is the first time we're processing this chunk and
1076 * sibling_count is now 1 it means all the pending exceptions
1077 * got completed while we were in the loop above, so it falls to
1078 * us here to remove the primary_pe and submit any origin_bios.
1081 if (first
&& atomic_dec_and_test(&primary_pe
->sibling_count
)) {
1082 flush_bios(bio_list_get(&primary_pe
->origin_bios
));
1083 free_pending_exception(primary_pe
);
1084 /* If we got here, pe_queue is necessarily empty. */
1089 * Now that we have a complete pe list we can start the copying.
1091 list_for_each_entry_safe(pe
, next_pe
, &pe_queue
, list
)
1099 * Called on a write from the origin driver.
1101 static int do_origin(struct dm_dev
*origin
, struct bio
*bio
)
1106 down_read(&_origins_lock
);
1107 o
= __lookup_origin(origin
->bdev
);
1109 r
= __origin_write(&o
->snapshots
, bio
);
1110 up_read(&_origins_lock
);
1116 * Origin: maps a linear range of a device, with hooks for snapshotting.
1120 * Construct an origin mapping: <dev_path>
1121 * The context for an origin is merely a 'struct dm_dev *'
1122 * pointing to the real device.
1124 static int origin_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
1130 ti
->error
= "dm-origin: incorrect number of arguments";
1134 r
= dm_get_device(ti
, argv
[0], 0, ti
->len
,
1135 dm_table_get_mode(ti
->table
), &dev
);
1137 ti
->error
= "Cannot get target device";
1145 static void origin_dtr(struct dm_target
*ti
)
1147 struct dm_dev
*dev
= (struct dm_dev
*) ti
->private;
1148 dm_put_device(ti
, dev
);
1151 static int origin_map(struct dm_target
*ti
, struct bio
*bio
,
1152 union map_info
*map_context
)
1154 struct dm_dev
*dev
= (struct dm_dev
*) ti
->private;
1155 bio
->bi_bdev
= dev
->bdev
;
1157 if (unlikely(bio_barrier(bio
)))
1160 /* Only tell snapshots if this is a write */
1161 return (bio_rw(bio
) == WRITE
) ? do_origin(dev
, bio
) : 1;
1164 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1167 * Set the target "split_io" field to the minimum of all the snapshots'
1170 static void origin_resume(struct dm_target
*ti
)
1172 struct dm_dev
*dev
= (struct dm_dev
*) ti
->private;
1173 struct dm_snapshot
*snap
;
1175 chunk_t chunk_size
= 0;
1177 down_read(&_origins_lock
);
1178 o
= __lookup_origin(dev
->bdev
);
1180 list_for_each_entry (snap
, &o
->snapshots
, list
)
1181 chunk_size
= min_not_zero(chunk_size
, snap
->chunk_size
);
1182 up_read(&_origins_lock
);
1184 ti
->split_io
= chunk_size
;
1187 static int origin_status(struct dm_target
*ti
, status_type_t type
, char *result
,
1188 unsigned int maxlen
)
1190 struct dm_dev
*dev
= (struct dm_dev
*) ti
->private;
1193 case STATUSTYPE_INFO
:
1197 case STATUSTYPE_TABLE
:
1198 snprintf(result
, maxlen
, "%s", dev
->name
);
1205 static struct target_type origin_target
= {
1206 .name
= "snapshot-origin",
1207 .version
= {1, 1, 0},
1208 .module
= THIS_MODULE
,
1212 .resume
= origin_resume
,
1213 .status
= origin_status
,
1216 static struct target_type snapshot_target
= {
1218 .version
= {1, 1, 0},
1219 .module
= THIS_MODULE
,
1220 .ctr
= snapshot_ctr
,
1221 .dtr
= snapshot_dtr
,
1222 .map
= snapshot_map
,
1223 .resume
= snapshot_resume
,
1224 .status
= snapshot_status
,
1227 static int __init
dm_snapshot_init(void)
1231 r
= dm_register_target(&snapshot_target
);
1233 DMERR("snapshot target register failed %d", r
);
1237 r
= dm_register_target(&origin_target
);
1239 DMERR("Device mapper: Origin: register failed %d\n", r
);
1243 r
= init_origin_hash();
1245 DMERR("init_origin_hash failed.");
1249 exception_cache
= kmem_cache_create("dm-snapshot-ex",
1250 sizeof(struct exception
),
1251 __alignof__(struct exception
),
1253 if (!exception_cache
) {
1254 DMERR("Couldn't create exception cache.");
1260 kmem_cache_create("dm-snapshot-in",
1261 sizeof(struct pending_exception
),
1262 __alignof__(struct pending_exception
),
1264 if (!pending_cache
) {
1265 DMERR("Couldn't create pending cache.");
1270 pending_pool
= mempool_create_slab_pool(128, pending_cache
);
1271 if (!pending_pool
) {
1272 DMERR("Couldn't create pending pool.");
1280 kmem_cache_destroy(pending_cache
);
1282 kmem_cache_destroy(exception_cache
);
1286 dm_unregister_target(&origin_target
);
1288 dm_unregister_target(&snapshot_target
);
1292 static void __exit
dm_snapshot_exit(void)
1296 r
= dm_unregister_target(&snapshot_target
);
1298 DMERR("snapshot unregister failed %d", r
);
1300 r
= dm_unregister_target(&origin_target
);
1302 DMERR("origin unregister failed %d", r
);
1305 mempool_destroy(pending_pool
);
1306 kmem_cache_destroy(pending_cache
);
1307 kmem_cache_destroy(exception_cache
);
1311 module_init(dm_snapshot_init
);
1312 module_exit(dm_snapshot_exit
);
1314 MODULE_DESCRIPTION(DM_NAME
" snapshot target");
1315 MODULE_AUTHOR("Joe Thornber");
1316 MODULE_LICENSE("GPL");