14 #include <linux/kdev_t.h>
15 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
25 #define DM_MSG_PREFIX "snapshots"
27 static const char dm_snapshot_merge_target_name[] =
"snapshot-merge";
29 #define dm_target_is_snapshot_merge(ti) \
30 ((ti)->type->name == dm_snapshot_merge_target_name)
37 #define DM_TRACKED_CHUNK_HASH_SIZE 16
38 #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
39 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
125 #define RUNNING_MERGE 0
126 #define SHUTDOWN_MERGE 1
194 static struct kmem_cache *tracked_chunk_cache;
206 hlist_add_head(&c->
node,
235 if (c->
chunk == chunk) {
252 while (__chunk_is_tracked(s, chunk))
273 #define ORIGIN_HASH_SIZE 256
274 #define ORIGIN_MASK 0xFF
280 static uint64_t _pending_exceptions_done_count;
282 static int init_origin_hash(
void)
289 DMERR(
"unable to allocate memory");
294 INIT_LIST_HEAD(_origins + i);
300 static void exit_origin_hash(
void)
315 ol = &_origins[origin_hash(origin)];
317 if (bdev_equal(o->
bdev, origin))
323 static
void __insert_origin(
struct origin *o)
325 struct list_head *
sl = &_origins[origin_hash(o->bdev)];
354 o = __lookup_origin(snap->
origin->bdev);
361 if (!bdev_equal(s->
cow->bdev, snap->
cow->bdev))
371 }
else if (snap_dest)
385 static int __validate_exception_handover(
struct dm_snapshot *snap)
391 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
392 &snap_merge) == 2) ||
394 snap->
ti->error =
"Snapshot cow pairing for exception "
395 "table handover failed";
416 snap->
ti->error =
"A snapshot is already merging.";
420 if (!snap_src->
store->type->prepare_merge ||
421 !snap_src->
store->type->commit_merge) {
422 snap->
ti->error =
"Snapshot exception store does not "
423 "support snapshot-merge.";
430 static void __insert_snapshot(
struct origin *o,
struct dm_snapshot *s)
451 struct origin *o, *new_o =
NULL;
461 r = __validate_exception_handover(snap);
467 o = __lookup_origin(bdev);
481 __insert_snapshot(o, snap);
492 static void reregister_snapshot(
struct dm_snapshot *s)
499 __insert_snapshot(__lookup_origin(bdev), s);
504 static void unregister_snapshot(
struct dm_snapshot *s)
509 o = __lookup_origin(s->
origin->bdev);
536 for (i = 0; i <
size; i++)
537 INIT_LIST_HEAD(et->
table + i);
550 for (i = 0; i <
size; i++) {
562 return (chunk >> et->hash_shift) & et->hash_mask;
580 slot = &et->
table[exception_hash(et, chunk)];
583 chunk <= e->
old_chunk + dm_consecutive_chunk_count(e))
600 static void free_completed_exception(
struct dm_exception *e)
641 dm_consecutive_chunk_count(e) + 1) &&
643 dm_consecutive_chunk_count(e) + 1)) {
644 dm_consecutive_chunk_count_inc(e);
645 free_completed_exception(new_e);
652 dm_consecutive_chunk_count_inc(e);
655 free_completed_exception(new_e);
676 e = alloc_completed_exception();
685 dm_insert_exception(&s->
complete, e);
694 static uint32_t __minimum_chunk_size(
struct origin *o)
697 unsigned chunk_size = 0;
702 snap->
store->chunk_size);
710 static
int calc_max_buckets(
void)
713 unsigned long mem = 2 * 1024 * 1024;
724 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
730 cow_dev_size = get_dev_size(s->
cow->bdev);
731 origin_dev_size = get_dev_size(s->
origin->bdev);
732 max_buckets = calc_max_buckets();
734 hash_size =
min(origin_dev_size, cow_dev_size) >> s->
store->chunk_shift;
735 hash_size =
min(hash_size, max_buckets);
740 if (dm_exception_table_init(&s->
complete, hash_size,
752 if (dm_exception_table_init(&s->
pending, hash_size, 0)) {
753 dm_exception_table_exit(&s->
complete, exception_cache);
767 static struct bio *__release_queued_bios_after_merge(
struct dm_snapshot *s)
778 static int __remove_single_exception_chunk(
struct dm_snapshot *s,
783 e = dm_lookup_exception(&s->
complete, old_chunk);
785 DMERR(
"Corruption detected: exception for block %llu is "
786 "on disk but not in memory",
787 (
unsigned long long)old_chunk);
794 if (!dm_consecutive_chunk_count(e)) {
795 dm_remove_exception(e);
796 free_completed_exception(e);
812 dm_consecutive_chunk_count(e)) {
813 DMERR(
"Attempt to merge block %llu from the "
814 "middle of a chunk range [%llu - %llu]",
815 (
unsigned long long)old_chunk,
818 e->
old_chunk + dm_consecutive_chunk_count(e));
822 dm_consecutive_chunk_count_dec(e);
827 static void flush_bios(
struct bio *bio);
829 static int remove_single_exception_chunk(
struct dm_snapshot *s)
831 struct bio *
b =
NULL;
842 r = __remove_single_exception_chunk(s, old_chunk);
847 b = __release_queued_bios_after_merge(s);
857 static int origin_write_extent(
struct dm_snapshot *merging_snap,
860 static void merge_callback(
int read_err,
unsigned long write_err,
863 static uint64_t read_pending_exceptions_done_count(
void)
867 spin_lock(&_pending_exceptions_done_spinlock);
868 pending_exceptions_done = _pending_exceptions_done_count;
869 spin_unlock(&_pending_exceptions_done_spinlock);
871 return pending_exceptions_done;
874 static void increment_pending_exceptions_done_count(
void)
876 spin_lock(&_pending_exceptions_done_spinlock);
877 _pending_exceptions_done_count++;
878 spin_unlock(&_pending_exceptions_done_spinlock);
883 static void snapshot_merge_next_chunks(
struct dm_snapshot *s)
885 int i, linear_chunks;
899 DMERR(
"Snapshot is invalid: can't merge");
903 linear_chunks = s->
store->type->prepare_merge(s->
store, &old_chunk,
905 if (linear_chunks <= 0) {
906 if (linear_chunks < 0) {
907 DMERR(
"Read error in exception store: "
908 "shutting down merge");
917 old_chunk = old_chunk + 1 - linear_chunks;
918 new_chunk = new_chunk + 1 - linear_chunks;
924 io_size = linear_chunks * s->
store->chunk_size;
927 dest.sector = chunk_to_sector(s->
store, old_chunk);
931 src.sector = chunk_to_sector(s->
store, new_chunk);
943 previous_count = read_pending_exceptions_done_count();
944 while (origin_write_extent(s,
dest.sector, io_size)) {
946 (read_pending_exceptions_done_count() !=
949 previous_count = read_pending_exceptions_done_count();
958 for (i = 0; i < linear_chunks; i++)
959 __check_for_conflicting_io(s, old_chunk + i);
970 static void merge_callback(
int read_err,
unsigned long write_err,
void *context)
973 struct bio *b =
NULL;
975 if (read_err || write_err) {
977 DMERR(
"Read error: shutting down merge.");
979 DMERR(
"Write error: shutting down merge.");
985 DMERR(
"Write error in exception store: shutting down merge");
989 if (remove_single_exception_chunk(s) < 0)
992 snapshot_merge_next_chunks(s);
999 b = __release_queued_bios_after_merge(s);
1009 snapshot_merge_next_chunks(s);
1012 static int wait_schedule(
void *
ptr)
1033 static int snapshot_ctr(
struct dm_target *ti,
unsigned int argc,
char **argv)
1038 char *origin_path, *cow_path;
1039 unsigned args_used, num_flush_requests = 1;
1043 ti->
error =
"requires exactly 4 arguments";
1049 num_flush_requests = 2;
1055 ti->
error =
"Cannot allocate private snapshot structure";
1060 origin_path = argv[0];
1066 ti->
error =
"Cannot get origin device";
1076 ti->
error =
"Cannot get COW device";
1082 ti->
error =
"Couldn't create exception store";
1095 INIT_LIST_HEAD(&s->
list);
1104 if (init_hash_tables(s)) {
1105 ti->
error =
"Unable to allocate hash table space";
1107 goto bad_hash_tables;
1113 ti->
error =
"Could not create kcopyd client";
1119 ti->
error =
"Could not allocate mempool for pending exceptions";
1120 goto bad_pending_pool;
1124 tracked_chunk_cache);
1126 ti->
error =
"Could not allocate tracked_chunk mempool for "
1128 goto bad_tracked_chunk_pool;
1141 r = register_snapshot(s);
1143 ti->
error =
"Snapshot origin struct allocation failed";
1144 goto bad_load_and_register;
1147 goto bad_load_and_register;
1157 s->
store->chunk_size = 0;
1161 r = s->
store->type->read_metadata(s->
store, dm_add_exception,
1164 ti->
error =
"Failed to read snapshot metadata";
1165 goto bad_read_metadata;
1168 DMWARN(
"Snapshot is marked invalid.");
1171 if (!s->
store->chunk_size) {
1172 ti->
error =
"Chunk size not set";
1173 goto bad_read_metadata;
1178 goto bad_read_metadata;
1183 unregister_snapshot(s);
1185 bad_load_and_register:
1188 bad_tracked_chunk_pool:
1195 dm_exception_table_exit(&s->
pending, pending_cache);
1196 dm_exception_table_exit(&s->
complete, exception_cache);
1214 static void __free_exceptions(
struct dm_snapshot *s)
1219 dm_exception_table_exit(&s->
pending, pending_cache);
1220 dm_exception_table_exit(&s->
complete, exception_cache);
1223 static void __handover_exceptions(
struct dm_snapshot *snap_src,
1238 u.store_swap = snap_dest->
store;
1240 snap_src->
store =
u.store_swap;
1242 snap_dest->
store->snap = snap_dest;
1243 snap_src->
store->snap = snap_src;
1245 snap_dest->
ti->max_io_len = snap_dest->
store->chunk_size;
1251 snap_src->
valid = 0;
1254 static void snapshot_dtr(
struct dm_target *ti)
1256 #ifdef CONFIG_DM_DEBUG
1264 (
void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1265 if (snap_src && snap_dest && (s == snap_src)) {
1267 snap_dest->valid = 0;
1269 DMERR(
"Cancelling snapshot handover.");
1278 unregister_snapshot(s);
1288 #ifdef CONFIG_DM_DEBUG
1295 __free_exceptions(s);
1311 static void flush_bios(
struct bio *bio)
1317 bio->bi_next =
NULL;
1323 static int do_origin(
struct dm_dev *origin,
struct bio *bio);
1328 static void retry_origin_bios(
struct dm_snapshot *s,
struct bio *bio)
1335 bio->bi_next =
NULL;
1336 r = do_origin(s->
origin, bio);
1352 bio->bi_next =
NULL;
1358 static void __invalidate_snapshot(
struct dm_snapshot *s,
int err)
1364 DMERR(
"Invalidating snapshot: Error reading/writing.");
1366 DMERR(
"Invalidating snapshot: Unable to allocate exception.");
1368 if (s->
store->type->drop_snapshot)
1380 struct bio *origin_bios =
NULL;
1381 struct bio *snapshot_bios =
NULL;
1382 struct bio *full_bio =
NULL;
1388 __invalidate_snapshot(s, -
EIO);
1393 e = alloc_completed_exception();
1396 __invalidate_snapshot(s, -
ENOMEM);
1404 free_completed_exception(e);
1410 __check_for_conflicting_io(s, pe->
e.old_chunk);
1416 dm_insert_exception(&s->
complete, e);
1419 dm_remove_exception(&pe->
e);
1427 free_pending_exception(pe);
1429 increment_pending_exceptions_done_count();
1436 bio_io_error(full_bio);
1441 flush_bios(snapshot_bios);
1444 retry_origin_bios(s, origin_bios);
1451 pending_complete(pe, success);
1458 static void copy_callback(
int read_err,
unsigned long write_err,
void *context)
1463 if (read_err || write_err)
1464 pending_complete(pe, 0);
1468 s->
store->type->commit_exception(s->
store, &pe->
e,
1478 struct dm_io_region
src,
dest;
1482 dev_size = get_dev_size(bdev);
1485 src.sector = chunk_to_sector(s->
store, pe->
e.old_chunk);
1489 dest.sector = chunk_to_sector(s->
store, pe->
e.new_chunk);
1496 static void full_bio_end_io(
struct bio *bio,
int error)
1498 void *callback_data = bio->bi_private;
1507 void *callback_data;
1516 bio->bi_end_io = full_bio_end_io;
1517 bio->bi_private = callback_data;
1547 pe2 = __lookup_pending_exception(s, chunk);
1549 free_pending_exception(pe);
1559 if (s->
store->type->prepare_exception(s->
store, &pe->
e)) {
1560 free_pending_exception(pe);
1564 dm_insert_exception(&s->
pending, &pe->
e);
1570 struct bio *bio,
chunk_t chunk)
1572 bio->bi_bdev = s->
cow->bdev;
1573 bio->bi_sector = chunk_to_sector(s->
store,
1577 s->
store->chunk_mask);
1580 static int snapshot_map(
struct dm_target *ti,
struct bio *bio,
1590 bio->bi_bdev = s->
cow->bdev;
1594 chunk = sector_to_chunk(s->
store, bio->bi_sector);
1611 e = dm_lookup_exception(&s->
complete, chunk);
1613 remap_exception(s, e, bio, chunk);
1622 if (bio_rw(bio) ==
WRITE) {
1623 pe = __lookup_pending_exception(s, chunk);
1626 pe = alloc_pending_exception(s);
1630 free_pending_exception(pe);
1635 e = dm_lookup_exception(&s->
complete, chunk);
1637 free_pending_exception(pe);
1638 remap_exception(s, e, bio, chunk);
1642 pe = __find_pending_exception(s, pe, chunk);
1644 __invalidate_snapshot(s, -
ENOMEM);
1650 remap_exception(s, &pe->
e, bio, chunk);
1658 start_full_bio(pe, bio);
1672 bio->bi_bdev = s->
origin->bdev;
1673 map_context->
ptr = track_chunk(s, chunk);
1694 static int snapshot_merge_map(
struct dm_target *ti,
struct bio *bio,
1704 bio->bi_bdev = s->
origin->bdev;
1706 bio->bi_bdev = s->
cow->bdev;
1711 chunk = sector_to_chunk(s->
store, bio->bi_sector);
1717 goto redirect_to_origin;
1720 e = dm_lookup_exception(&s->
complete, chunk);
1723 if (bio_rw(bio) ==
WRITE &&
1727 bio->bi_bdev = s->
origin->bdev;
1733 remap_exception(s, e, bio, chunk);
1735 if (bio_rw(bio) ==
WRITE)
1736 map_context->
ptr = track_chunk(s, chunk);
1741 bio->bi_bdev = s->
origin->bdev;
1743 if (bio_rw(bio) ==
WRITE) {
1745 return do_origin(s->
origin, bio);
1754 static int snapshot_end_io(
struct dm_target *ti,
struct bio *bio,
1755 int error,
union map_info *map_context)
1761 stop_tracking_chunk(s, c);
1766 static void snapshot_merge_presuspend(
struct dm_target *ti)
1773 static int snapshot_preresume(
struct dm_target *ti)
1780 (
void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1781 if (snap_src && snap_dest) {
1783 if (s == snap_src) {
1784 DMERR(
"Unable to resume snapshot source until "
1785 "handover completes.");
1788 DMERR(
"Unable to perform snapshot handover until "
1789 "source is suspended.");
1799 static void snapshot_resume(
struct dm_target *ti)
1805 (
void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
1806 if (snap_src && snap_dest) {
1809 __handover_exceptions(snap_src, snap_dest);
1816 reregister_snapshot(s);
1828 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1831 return min_chunksize;
1834 static void snapshot_merge_resume(
struct dm_target *ti)
1841 snapshot_resume(ti);
1852 unsigned status_flags,
char *
result,
unsigned maxlen)
1867 if (snap->
store->type->usage) {
1868 sector_t total_sectors, sectors_allocated,
1875 (
unsigned long long)sectors_allocated,
1876 (
unsigned long long)total_sectors,
1877 (
unsigned long long)metadata_sectors);
1894 snap->
store->type->status(snap->
store, type, result + sz,
1902 static int snapshot_iterate_devices(
struct dm_target *ti,
1911 r =
fn(ti, snap->
cow, 0, get_dev_size(snap->
cow->bdev), data);
1965 chunk = sector_to_chunk(snap->
store, sector);
1972 e = dm_lookup_exception(&snap->
complete, chunk);
1976 pe = __lookup_pending_exception(snap, chunk);
1979 pe = alloc_pending_exception(snap);
1983 free_pending_exception(pe);
1987 e = dm_lookup_exception(&snap->
complete, chunk);
1989 free_pending_exception(pe);
1993 pe = __find_pending_exception(snap, pe, chunk);
1995 __invalidate_snapshot(snap, -
ENOMEM);
2013 pe_to_start_last = pe;
2019 pe_to_start_now = pe;
2025 if (pe_to_start_now) {
2026 start_copy(pe_to_start_now);
2027 pe_to_start_now =
NULL;
2035 if (pe_to_start_last)
2036 start_copy(pe_to_start_last);
2044 static int do_origin(
struct dm_dev *origin,
struct bio *bio)
2050 o = __lookup_origin(origin->
bdev);
2052 r = __origin_write(&o->
snapshots, bio->bi_sector, bio);
2071 static int origin_write_extent(
struct dm_snapshot *merging_snap,
2083 o = __lookup_origin(merging_snap->
origin->bdev);
2084 for (n = 0; n <
size; n += merging_snap->
ti->max_io_len)
2085 if (__origin_write(&o->
snapshots, sector + n, NULL) ==
2102 static int origin_ctr(
struct dm_target *ti,
unsigned int argc,
char **argv)
2108 ti->
error =
"origin: incorrect number of arguments";
2114 ti->
error =
"Cannot get target device";
2124 static void origin_dtr(
struct dm_target *ti)
2130 static int origin_map(
struct dm_target *ti,
struct bio *bio,
2134 bio->bi_bdev = dev->
bdev;
2147 static void origin_resume(
struct dm_target *ti)
2155 unsigned status_flags,
char *result,
unsigned maxlen)
2172 static int origin_merge(
struct dm_target *ti,
struct bvec_merge_data *bvm,
2173 struct bio_vec *biovec,
int max_size)
2178 if (!q->merge_bvec_fn)
2181 bvm->bi_bdev = dev->
bdev;
2183 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2186 static int origin_iterate_devices(
struct dm_target *ti,
2191 return fn(ti, dev, 0, ti->
len, data);
2195 .name =
"snapshot-origin",
2196 .version = {1, 7, 1},
2201 .resume = origin_resume,
2202 .status = origin_status,
2203 .merge = origin_merge,
2204 .iterate_devices = origin_iterate_devices,
2209 .version = {1, 10, 0},
2211 .ctr = snapshot_ctr,
2212 .dtr = snapshot_dtr,
2213 .map = snapshot_map,
2214 .end_io = snapshot_end_io,
2215 .preresume = snapshot_preresume,
2216 .resume = snapshot_resume,
2217 .status = snapshot_status,
2218 .iterate_devices = snapshot_iterate_devices,
2222 .name = dm_snapshot_merge_target_name,
2223 .version = {1, 1, 0},
2225 .ctr = snapshot_ctr,
2226 .dtr = snapshot_dtr,
2227 .map = snapshot_merge_map,
2228 .end_io = snapshot_end_io,
2229 .presuspend = snapshot_merge_presuspend,
2230 .preresume = snapshot_preresume,
2231 .resume = snapshot_merge_resume,
2232 .status = snapshot_status,
2233 .iterate_devices = snapshot_iterate_devices,
2236 static int __init dm_snapshot_init(
void)
2242 DMERR(
"Failed to initialize exception stores");
2248 DMERR(
"snapshot target register failed %d", r);
2249 goto bad_register_snapshot_target;
2254 DMERR(
"Origin target register failed %d", r);
2255 goto bad_register_origin_target;
2260 DMERR(
"Merge target register failed %d", r);
2261 goto bad_register_merge_target;
2264 r = init_origin_hash();
2266 DMERR(
"init_origin_hash failed.");
2267 goto bad_origin_hash;
2271 if (!exception_cache) {
2272 DMERR(
"Couldn't create exception cache.");
2274 goto bad_exception_cache;
2278 if (!pending_cache) {
2279 DMERR(
"Couldn't create pending cache.");
2281 goto bad_pending_cache;
2285 if (!tracked_chunk_cache) {
2286 DMERR(
"Couldn't create cache to track chunks in use.");
2288 goto bad_tracked_chunk_cache;
2293 bad_tracked_chunk_cache:
2297 bad_exception_cache:
2301 bad_register_merge_target:
2303 bad_register_origin_target:
2305 bad_register_snapshot_target:
2311 static void __exit dm_snapshot_exit(
void)