[PATCH] dm snapshot: allow zero chunk_size

The chunk size of snapshots cannot be changed so it is redundant to require it
as a parameter when activating an existing snapshot.  Allow a value of zero in
this case and ignore it.  For a new snapshot, use a default value if zero is
specified.

Signed-off-by: Mark McLoughlin <markmc@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
This commit is contained in:
Mark McLoughlin 2006-10-03 01:15:25 -07:00 committed by Linus Torvalds
parent 92c060a692
commit 4c7e3bf44d
3 changed files with 122 additions and 101 deletions

View file

@ -17,6 +17,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#define DM_MSG_PREFIX "snapshots" #define DM_MSG_PREFIX "snapshots"
#define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
/*----------------------------------------------------------------- /*-----------------------------------------------------------------
* Persistent snapshots, by persistent we mean that the snapshot * Persistent snapshots, by persistent we mean that the snapshot
@ -150,6 +151,7 @@ static int alloc_area(struct pstore *ps)
static void free_area(struct pstore *ps) static void free_area(struct pstore *ps)
{ {
vfree(ps->area); vfree(ps->area);
ps->area = NULL;
} }
/* /*
@ -198,22 +200,52 @@ 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; chunk_t chunk_size;
int chunk_size_supplied = 1;
/*
* Use default chunk size (or hardsect_size, if larger) if none supplied
*/
if (!ps->snap->chunk_size) {
ps->snap->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
bdev_hardsect_size(ps->snap->cow->bdev) >> 9);
ps->snap->chunk_mask = ps->snap->chunk_size - 1;
ps->snap->chunk_shift = ffs(ps->snap->chunk_size) - 1;
chunk_size_supplied = 0;
}
r = dm_io_get(sectors_to_pages(ps->snap->chunk_size));
if (r)
return r;
r = alloc_area(ps);
if (r)
goto bad1;
r = chunk_io(ps, 0, READ); r = chunk_io(ps, 0, READ);
if (r) if (r)
return r; goto bad2;
dh = (struct disk_header *) ps->area; dh = (struct disk_header *) ps->area;
if (le32_to_cpu(dh->magic) == 0) { if (le32_to_cpu(dh->magic) == 0) {
*new_snapshot = 1; *new_snapshot = 1;
return 0;
}
if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
DMWARN("Invalid or corrupt snapshot");
r = -ENXIO;
goto bad2;
}
} else if (le32_to_cpu(dh->magic) == SNAP_MAGIC) {
*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);
chunk_size = le32_to_cpu(dh->chunk_size); chunk_size = le32_to_cpu(dh->chunk_size);
if (ps->snap->chunk_size != chunk_size) {
if (!chunk_size_supplied || ps->snap->chunk_size == chunk_size)
return 0;
DMWARN("chunk size %llu in device metadata overrides " DMWARN("chunk size %llu in device metadata overrides "
"table chunk size of %llu.", "table chunk size of %llu.",
(unsigned long long)chunk_size, (unsigned long long)chunk_size,
@ -227,19 +259,20 @@ static int read_header(struct pstore *ps, int *new_snapshot)
ps->snap->chunk_mask = chunk_size - 1; ps->snap->chunk_mask = chunk_size - 1;
ps->snap->chunk_shift = ffs(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)); r = dm_io_get(sectors_to_pages(chunk_size));
if (r) if (r)
return r; return r;
}
} else {
DMWARN("Invalid/corrupt snapshot");
r = -ENXIO;
}
r = alloc_area(ps);
if (r)
goto bad1;
return 0;
bad2:
free_area(ps);
bad1:
dm_io_put(sectors_to_pages(ps->snap->chunk_size));
return r; return r;
} }
@ -547,32 +580,22 @@ static void persistent_drop(struct exception_store *store)
DMWARN("write header failed"); DMWARN("write header failed");
} }
int dm_create_persistent(struct exception_store *store, uint32_t chunk_size) int dm_create_persistent(struct exception_store *store)
{ {
int r;
struct pstore *ps; struct pstore *ps;
r = dm_io_get(sectors_to_pages(chunk_size));
if (r)
return r;
/* allocate the pstore */ /* allocate the pstore */
ps = kmalloc(sizeof(*ps), GFP_KERNEL); ps = kmalloc(sizeof(*ps), GFP_KERNEL);
if (!ps) { if (!ps)
r = -ENOMEM; return -ENOMEM;
goto bad;
}
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->area = NULL;
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;
r = alloc_area(ps);
if (r)
goto bad;
ps->callback_count = 0; ps->callback_count = 0;
atomic_set(&ps->pending_count, 0); atomic_set(&ps->pending_count, 0);
ps->callbacks = NULL; ps->callbacks = NULL;
@ -586,13 +609,6 @@ int dm_create_persistent(struct exception_store *store, uint32_t chunk_size)
store->context = ps; store->context = ps;
return 0; return 0;
bad:
dm_io_put(sectors_to_pages(chunk_size));
if (ps && ps->area)
free_area(ps);
kfree(ps);
return r;
} }
/*----------------------------------------------------------------- /*-----------------------------------------------------------------
@ -642,18 +658,16 @@ static void transient_fraction_full(struct exception_store *store,
*denominator = get_dev_size(store->snap->cow->bdev); *denominator = get_dev_size(store->snap->cow->bdev);
} }
int dm_create_transient(struct exception_store *store, int dm_create_transient(struct exception_store *store)
struct dm_snapshot *s, int blocksize)
{ {
struct transient_c *tc; struct transient_c *tc;
memset(store, 0, sizeof(*store));
store->destroy = transient_destroy; store->destroy = transient_destroy;
store->read_metadata = transient_read_metadata; store->read_metadata = transient_read_metadata;
store->prepare_exception = transient_prepare; store->prepare_exception = transient_prepare;
store->commit_exception = transient_commit; store->commit_exception = transient_commit;
store->drop_snapshot = NULL;
store->fraction_full = transient_fraction_full; store->fraction_full = transient_fraction_full;
store->snap = s;
tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL); tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
if (!tc) if (!tc)

View file

@ -398,21 +398,60 @@ static void read_snapshot_metadata(struct dm_snapshot *s)
} }
} }
static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
char **error)
{
unsigned long chunk_size;
char *value;
chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
if (*chunk_size_arg == '\0' || *value != '\0') {
*error = "Invalid chunk size";
return -EINVAL;
}
if (!chunk_size) {
s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
return 0;
}
/*
* Chunk size must be multiple of page size. Silently
* round up if it's not.
*/
chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
/* Check chunk_size is a power of 2 */
if (chunk_size & (chunk_size - 1)) {
*error = "Chunk size is not a power of 2";
return -EINVAL;
}
/* Validate the chunk size against the device block size */
if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
*error = "Chunk size is not a multiple of device blocksize";
return -EINVAL;
}
s->chunk_size = chunk_size;
s->chunk_mask = chunk_size - 1;
s->chunk_shift = ffs(chunk_size) - 1;
return 0;
}
/* /*
* Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size> * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
*/ */
static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{ {
struct dm_snapshot *s; struct dm_snapshot *s;
unsigned long chunk_size;
int r = -EINVAL; int r = -EINVAL;
char persistent; char persistent;
char *origin_path; char *origin_path;
char *cow_path; char *cow_path;
char *value;
int blocksize;
if (argc < 4) { if (argc != 4) {
ti->error = "requires exactly 4 arguments"; ti->error = "requires exactly 4 arguments";
r = -EINVAL; r = -EINVAL;
goto bad1; goto bad1;
@ -428,13 +467,6 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
goto bad1; goto bad1;
} }
chunk_size = simple_strtoul(argv[3], &value, 10);
if (chunk_size == 0 || value == NULL) {
ti->error = "Invalid chunk size";
r = -EINVAL;
goto bad1;
}
s = kmalloc(sizeof(*s), GFP_KERNEL); s = kmalloc(sizeof(*s), GFP_KERNEL);
if (s == NULL) { if (s == NULL) {
ti->error = "Cannot allocate snapshot context private " ti->error = "Cannot allocate snapshot context private "
@ -457,31 +489,11 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
goto bad2; goto bad2;
} }
/* r = set_chunk_size(s, argv[3], &ti->error);
* Chunk size must be multiple of page size. Silently if (r)
* round up if it's not.
*/
chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
/* Validate the chunk size against the device block size */
blocksize = s->cow->bdev->bd_disk->queue->hardsect_size;
if (chunk_size % (blocksize >> 9)) {
ti->error = "Chunk size is not a multiple of device blocksize";
r = -EINVAL;
goto bad3; goto bad3;
}
/* Check chunk_size is a power of 2 */
if (chunk_size & (chunk_size - 1)) {
ti->error = "Chunk size is not a power of 2";
r = -EINVAL;
goto bad3;
}
s->chunk_size = chunk_size;
s->chunk_mask = chunk_size - 1;
s->type = persistent; s->type = persistent;
s->chunk_shift = ffs(chunk_size) - 1;
s->valid = 1; s->valid = 1;
s->active = 0; s->active = 0;
@ -496,16 +508,12 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
goto bad3; goto bad3;
} }
/*
* Check the persistent flag - done here because we need the iobuf
* to check the LV header
*/
s->store.snap = s; s->store.snap = s;
if (persistent == 'P') if (persistent == 'P')
r = dm_create_persistent(&s->store, chunk_size); r = dm_create_persistent(&s->store);
else else
r = dm_create_transient(&s->store, s, blocksize); r = dm_create_transient(&s->store);
if (r) { if (r) {
ti->error = "Couldn't create exception store"; ti->error = "Couldn't create exception store";
@ -1205,7 +1213,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, 4, 0}, .version = {1, 5, 0},
.module = THIS_MODULE, .module = THIS_MODULE,
.ctr = origin_ctr, .ctr = origin_ctr,
.dtr = origin_dtr, .dtr = origin_dtr,
@ -1216,7 +1224,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, 4, 0}, .version = {1, 5, 0},
.module = THIS_MODULE, .module = THIS_MODULE,
.ctr = snapshot_ctr, .ctr = snapshot_ctr,
.dtr = snapshot_dtr, .dtr = snapshot_dtr,

View file

@ -128,10 +128,9 @@ int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new);
* Constructor and destructor for the default persistent * Constructor and destructor for the default persistent
* store. * store.
*/ */
int dm_create_persistent(struct exception_store *store, uint32_t chunk_size); int dm_create_persistent(struct exception_store *store);
int dm_create_transient(struct exception_store *store, int dm_create_transient(struct exception_store *store);
struct dm_snapshot *s, int blocksize);
/* /*
* Return the number of sectors in the device. * Return the number of sectors in the device.