Commit c51c2752 authored by Alasdair G Kergon's avatar Alasdair G Kergon Committed by Linus Torvalds

[PATCH] dm snapshot: unify chunk_size

Persistent snapshots currently store a private copy of the chunk size.
Userspace also supplies the chunk size when loading a snapshot.  Ensure
consistency by only storing the chunk_size in one place instead of two.

Currently the two sizes will differ if the chunk size supplied by userspace
does not match the chunk size an existing snapshot actually uses.  Amongst
other problems, this causes an incorrect 'percentage full' to be reported.

The patch ensures consistency by only storing the chunk_size in one place,
removing it from struct pstore.  Some initialisation is delayed until the
correct chunk_size is known.  If read_header() discovers that the wrong chunk
size was supplied, the 'area' buffer (which the header already got read into)
is reinitialised to the correct size.

[akpm: too late for 2.6.17 - suitable for 2.6.17.x after it has settled]
Signed-off-by: default avatarAlasdair G Kergon <agk@redhat.com>
Cc: <stable@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent b877a964
...@@ -91,7 +91,6 @@ struct pstore { ...@@ -91,7 +91,6 @@ struct pstore {
struct dm_snapshot *snap; /* up pointer to my snapshot */ struct dm_snapshot *snap; /* up pointer to my snapshot */
int version; int version;
int valid; int valid;
uint32_t chunk_size;
uint32_t exceptions_per_area; uint32_t exceptions_per_area;
/* /*
...@@ -133,7 +132,7 @@ static int alloc_area(struct pstore *ps) ...@@ -133,7 +132,7 @@ static int alloc_area(struct pstore *ps)
int r = -ENOMEM; int r = -ENOMEM;
size_t len; size_t len;
len = ps->chunk_size << SECTOR_SHIFT; len = ps->snap->chunk_size << SECTOR_SHIFT;
/* /*
* Allocate the chunk_size block of memory that will hold * Allocate the chunk_size block of memory that will hold
...@@ -160,8 +159,8 @@ static int chunk_io(struct pstore *ps, uint32_t chunk, int rw) ...@@ -160,8 +159,8 @@ static int chunk_io(struct pstore *ps, uint32_t chunk, int rw)
unsigned long bits; unsigned long bits;
where.bdev = ps->snap->cow->bdev; where.bdev = ps->snap->cow->bdev;
where.sector = ps->chunk_size * chunk; where.sector = ps->snap->chunk_size * chunk;
where.count = ps->chunk_size; where.count = ps->snap->chunk_size;
return dm_io_sync_vm(1, &where, rw, ps->area, &bits); return dm_io_sync_vm(1, &where, rw, ps->area, &bits);
} }
...@@ -188,7 +187,7 @@ static int area_io(struct pstore *ps, uint32_t area, int rw) ...@@ -188,7 +187,7 @@ static int area_io(struct pstore *ps, uint32_t area, int rw)
static int zero_area(struct pstore *ps, uint32_t area) static int zero_area(struct pstore *ps, uint32_t area)
{ {
memset(ps->area, 0, ps->chunk_size << SECTOR_SHIFT); memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
return area_io(ps, area, WRITE); return area_io(ps, area, WRITE);
} }
...@@ -196,6 +195,7 @@ static int read_header(struct pstore *ps, int *new_snapshot) ...@@ -196,6 +195,7 @@ static int read_header(struct pstore *ps, int *new_snapshot)
{ {
int r; int r;
struct disk_header *dh; struct disk_header *dh;
chunk_t chunk_size;
r = chunk_io(ps, 0, READ); r = chunk_io(ps, 0, READ);
if (r) if (r)
...@@ -210,8 +210,29 @@ static int read_header(struct pstore *ps, int *new_snapshot) ...@@ -210,8 +210,29 @@ static int read_header(struct pstore *ps, int *new_snapshot)
*new_snapshot = 0; *new_snapshot = 0;
ps->valid = le32_to_cpu(dh->valid); ps->valid = le32_to_cpu(dh->valid);
ps->version = le32_to_cpu(dh->version); ps->version = le32_to_cpu(dh->version);
ps->chunk_size = le32_to_cpu(dh->chunk_size); chunk_size = le32_to_cpu(dh->chunk_size);
if (ps->snap->chunk_size != chunk_size) {
DMWARN("chunk size %llu in device metadata overrides "
"table chunk size of %llu.",
(unsigned long long)chunk_size,
(unsigned long long)ps->snap->chunk_size);
/* We had a bogus chunk_size. Fix stuff up. */
dm_io_put(sectors_to_pages(ps->snap->chunk_size));
free_area(ps);
ps->snap->chunk_size = chunk_size;
ps->snap->chunk_mask = chunk_size - 1;
ps->snap->chunk_shift = ffs(chunk_size) - 1;
r = alloc_area(ps);
if (r)
return r;
r = dm_io_get(sectors_to_pages(chunk_size));
if (r)
return r;
}
} else { } else {
DMWARN("Invalid/corrupt snapshot"); DMWARN("Invalid/corrupt snapshot");
r = -ENXIO; r = -ENXIO;
...@@ -224,13 +245,13 @@ static int write_header(struct pstore *ps) ...@@ -224,13 +245,13 @@ static int write_header(struct pstore *ps)
{ {
struct disk_header *dh; struct disk_header *dh;
memset(ps->area, 0, ps->chunk_size << SECTOR_SHIFT); memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
dh = (struct disk_header *) ps->area; dh = (struct disk_header *) ps->area;
dh->magic = cpu_to_le32(SNAP_MAGIC); dh->magic = cpu_to_le32(SNAP_MAGIC);
dh->valid = cpu_to_le32(ps->valid); dh->valid = cpu_to_le32(ps->valid);
dh->version = cpu_to_le32(ps->version); dh->version = cpu_to_le32(ps->version);
dh->chunk_size = cpu_to_le32(ps->chunk_size); dh->chunk_size = cpu_to_le32(ps->snap->chunk_size);
return chunk_io(ps, 0, WRITE); return chunk_io(ps, 0, WRITE);
} }
...@@ -365,7 +386,7 @@ static void persistent_destroy(struct exception_store *store) ...@@ -365,7 +386,7 @@ static void persistent_destroy(struct exception_store *store)
{ {
struct pstore *ps = get_info(store); struct pstore *ps = get_info(store);
dm_io_put(sectors_to_pages(ps->chunk_size)); dm_io_put(sectors_to_pages(ps->snap->chunk_size));
vfree(ps->callbacks); vfree(ps->callbacks);
free_area(ps); free_area(ps);
kfree(ps); kfree(ps);
...@@ -383,6 +404,16 @@ static int persistent_read_metadata(struct exception_store *store) ...@@ -383,6 +404,16 @@ static int persistent_read_metadata(struct exception_store *store)
if (r) if (r)
return r; return r;
/*
* Now we know correct chunk_size, complete the initialisation.
*/
ps->exceptions_per_area = (ps->snap->chunk_size << SECTOR_SHIFT) /
sizeof(struct disk_exception);
ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
sizeof(*ps->callbacks));
if (!ps->callbacks)
return -ENOMEM;
/* /*
* Do we need to setup a new snapshot ? * Do we need to setup a new snapshot ?
*/ */
...@@ -533,9 +564,6 @@ int dm_create_persistent(struct exception_store *store, uint32_t chunk_size) ...@@ -533,9 +564,6 @@ int dm_create_persistent(struct exception_store *store, uint32_t chunk_size)
ps->snap = store->snap; ps->snap = store->snap;
ps->valid = 1; ps->valid = 1;
ps->version = SNAPSHOT_DISK_VERSION; ps->version = SNAPSHOT_DISK_VERSION;
ps->chunk_size = chunk_size;
ps->exceptions_per_area = (chunk_size << SECTOR_SHIFT) /
sizeof(struct disk_exception);
ps->next_free = 2; /* skipping the header and first area */ ps->next_free = 2; /* skipping the header and first area */
ps->current_committed = 0; ps->current_committed = 0;
...@@ -543,18 +571,9 @@ int dm_create_persistent(struct exception_store *store, uint32_t chunk_size) ...@@ -543,18 +571,9 @@ int dm_create_persistent(struct exception_store *store, uint32_t chunk_size)
if (r) if (r)
goto bad; goto bad;
/*
* Allocate space for all the callbacks.
*/
ps->callback_count = 0; ps->callback_count = 0;
atomic_set(&ps->pending_count, 0); atomic_set(&ps->pending_count, 0);
ps->callbacks = dm_vcalloc(ps->exceptions_per_area, ps->callbacks = NULL;
sizeof(*ps->callbacks));
if (!ps->callbacks) {
r = -ENOMEM;
goto bad;
}
store->destroy = persistent_destroy; store->destroy = persistent_destroy;
store->read_metadata = persistent_read_metadata; store->read_metadata = persistent_read_metadata;
......
...@@ -530,7 +530,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) ...@@ -530,7 +530,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
} }
ti->private = s; ti->private = s;
ti->split_io = chunk_size; ti->split_io = s->chunk_size;
return 0; return 0;
...@@ -1204,7 +1204,7 @@ static int origin_status(struct dm_target *ti, status_type_t type, char *result, ...@@ -1204,7 +1204,7 @@ static int origin_status(struct dm_target *ti, status_type_t type, char *result,
static struct target_type origin_target = { static struct target_type origin_target = {
.name = "snapshot-origin", .name = "snapshot-origin",
.version = {1, 1, 0}, .version = {1, 4, 0},
.module = THIS_MODULE, .module = THIS_MODULE,
.ctr = origin_ctr, .ctr = origin_ctr,
.dtr = origin_dtr, .dtr = origin_dtr,
...@@ -1215,7 +1215,7 @@ static struct target_type origin_target = { ...@@ -1215,7 +1215,7 @@ static struct target_type origin_target = {
static struct target_type snapshot_target = { static struct target_type snapshot_target = {
.name = "snapshot", .name = "snapshot",
.version = {1, 1, 0}, .version = {1, 4, 0},
.module = THIS_MODULE, .module = THIS_MODULE,
.ctr = snapshot_ctr, .ctr = snapshot_ctr,
.dtr = snapshot_dtr, .dtr = snapshot_dtr,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment