The merging process. We don't need a separate thread, kcopyd does the job just fine (provided that we have private kcopyd). Merging is started when origin is resumed and it is stopped when origin is suspended or when the merging snapshot is destoyed. Merging is not interlocked with writes, so there is a race condition with concurrent access. It will be fixed in further patches. Signed-off-by: Mikulas Patocka --- drivers/md/dm-snap.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++- drivers/md/dm-snap.h | 7 ++ 2 files changed, 140 insertions(+), 1 deletion(-) Index: linux-2.6.26-fast/drivers/md/dm-snap.c =================================================================== --- linux-2.6.26-fast.orig/drivers/md/dm-snap.c 2008-07-23 23:01:21.000000000 +0200 +++ linux-2.6.26-fast/drivers/md/dm-snap.c 2008-07-23 23:03:53.000000000 +0200 @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "dm-snap.h" @@ -563,6 +564,115 @@ static int set_chunk_size(struct dm_snap return 0; } +static void merge_callback(int read_err, unsigned long write_err, void *context); + +static void snapshot_merge_process(struct dm_snapshot *s) +{ + int r; + chunk_t old_chunk, new_chunk; + struct dm_snap_exception *e; + struct dm_io_region src, dest; + + BUG_ON(!s->merge_running); + if (s->merge_shutdown) + goto shut; + + if (!s->valid) { + DMERR("snapshot is invalid, can't merge"); + goto shut; + } + + if (!s->store.prepare_exception || !s->store.commit_exception) { + DMERR("target store does not support merging"); + goto shut; + } + r = s->store.prepare_merge(&s->store, &old_chunk, &new_chunk); + if (r <= 0) { + if (r < 0) + DMERR("Read error in exception store, shutting down merge"); + goto shut; + } + + /* TODO: use larger I/O size once we verify that kcopyd handles it */ + + /* !!! FIXME: intelock writes to this chunk */ + down_write(&s->lock); + e = lookup_exception(&s->complete, old_chunk); + if (!e) { + DMERR("exception for block %Lu is on disk but not in memory", (unsigned long long)old_chunk); + up_write(&s->lock); + goto shut; + } + if (dm_consecutive_chunk_count(e)) { + dm_consecutive_chunk_count_dec(e); + } else { + remove_exception(e); + free_exception(e); + } + up_write(&s->lock); + + dest.bdev = s->origin->bdev; + dest.sector = chunk_to_sector(s, old_chunk); + dest.count = min((sector_t)s->chunk_size, get_dev_size(dest.bdev) - dest.sector); + + src.bdev = s->cow->bdev; + src.sector = chunk_to_sector(s, new_chunk); + src.count = dest.count; + + dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s); + return; + + shut: + s->merge_running = 0; +} + +static void merge_callback(int read_err, unsigned long write_err, void *context) +{ + int r; + struct dm_snapshot *s = context; + + if (read_err || write_err) { + if (read_err) + DMERR("Read error in data, shutting down merge"); + else + DMERR("Write error in data, shutting down merge"); + goto shut; + } + + r = s->store.commit_merge(&s->store, 1); + if (r < 0) { + DMERR("Write error in exception store, shutting down merge"); + goto shut; + } + + snapshot_merge_process(s); + return; + + shut: + s->merge_running = 0; +} + +static void start_merge(struct dm_snapshot *merging) +{ + if (!merging->merge_running && !merging->merge_shutdown) { + merging->merge_running = 1; + snapshot_merge_process(merging); + } +} + +/* + * Stop the merging process and wait until it finishes. + */ + +static void stop_merge(struct dm_snapshot *merging) +{ + while (merging->merge_running) { + merging->merge_shutdown = 1; + msleep(1); + } + merging->merge_shutdown = 0; +} + /* * Construct a snapshot mapping:

*/ @@ -628,6 +738,8 @@ static int snapshot_ctr(struct dm_target s->valid = 1; s->active = 0; s->handover = 0; + s->merge_running = 0; + s->merge_shutdown = 0; init_rwsem(&s->lock); s->ti = ti; @@ -788,6 +900,9 @@ static void snapshot_dtr(struct dm_targe up_write(&s->lock); up_write(&_origins_lock); + if (is_merge(ti)) + stop_merge(s); + /* Prevent further origin writes from using this snapshot. */ /* After this returns there can be no new kcopyd jobs. */ unregister_snapshot(s); @@ -1195,6 +1310,21 @@ static void snapshot_resume(struct dm_ta up_write(&_origins_lock); } +static void snapshot_merge_resume(struct dm_target *ti) +{ + struct dm_snapshot *s = ti->private; + + snapshot_resume(ti); + start_merge(s); +} + +static void snapshot_merge_presuspend(struct dm_target *ti) +{ + struct dm_snapshot *s = ti->private; + + stop_merge(s); +} + static int snapshot_status(struct dm_target *ti, status_type_t type, char *result, unsigned int maxlen) { @@ -1378,6 +1508,7 @@ static int origin_ctr(struct dm_target * static void origin_dtr(struct dm_target *ti) { struct dm_dev *dev = ti->private; + dm_put_device(ti, dev); } @@ -1455,7 +1586,8 @@ static struct target_type snapshot_merge .dtr = snapshot_dtr, .map = snapshot_merge_map, .end_io = snapshot_end_io, - .resume = snapshot_resume, + .presuspend = snapshot_merge_presuspend, + .resume = snapshot_merge_resume, .status = snapshot_status, }; Index: linux-2.6.26-fast/drivers/md/dm-snap.h =================================================================== --- linux-2.6.26-fast.orig/drivers/md/dm-snap.h 2008-07-23 23:01:10.000000000 +0200 +++ linux-2.6.26-fast/drivers/md/dm-snap.h 2008-07-23 23:03:19.000000000 +0200 @@ -210,6 +210,13 @@ struct dm_snapshot { mempool_t *tracked_chunk_pool; spinlock_t tracked_chunk_lock; struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE]; + + /* Merge operation is in progress */ + int merge_running; + + /* It is requested to shut down merging */ + /* Cleared back to 0 when the merging is stopped */ + int merge_shutdown; }; /*