Commit 6a52325f authored by Alex Elder's avatar Alex Elder

rbd: rearrange rbd_header_from_disk()

This just moves code around for the most part.  It was pulled out as
a separate patch to avoid cluttering up some upcoming patches which
are more substantive.  The point is basically to group everything
related to initializing the snapshot context together.

The only functional change is that rbd_header_from_disk() now
ensures the (in-core) header it is passed is zero-filled.  This
allows a simpler error handling path in rbd_header_from_disk().
Signed-off-by: default avatarAlex Elder <elder@inktank.com>
Reviewed-by: default avatarJosh Durgin <josh.durgin@inktank.com>
parent d2bb24e5
...@@ -506,11 +506,14 @@ static int rbd_header_from_disk(struct rbd_image_header *header, ...@@ -506,11 +506,14 @@ static int rbd_header_from_disk(struct rbd_image_header *header,
if (snap_count > size / sizeof (header->snapc->snaps[0])) if (snap_count > size / sizeof (header->snapc->snaps[0]))
return -EINVAL; return -EINVAL;
size = sizeof (struct ceph_snap_context); memset(header, 0, sizeof (*header));
size += snap_count * sizeof (header->snapc->snaps[0]);
header->snapc = kmalloc(size, GFP_KERNEL); size = sizeof (ondisk->block_name) + 1;
if (!header->snapc) header->object_prefix = kmalloc(size, GFP_KERNEL);
if (!header->object_prefix)
return -ENOMEM; return -ENOMEM;
memcpy(header->object_prefix, ondisk->block_name, size - 1);
header->object_prefix[size - 1] = '\0';
if (snap_count) { if (snap_count) {
header->snap_names_len = le64_to_cpu(ondisk->snap_names_len); header->snap_names_len = le64_to_cpu(ondisk->snap_names_len);
...@@ -518,11 +521,12 @@ static int rbd_header_from_disk(struct rbd_image_header *header, ...@@ -518,11 +521,12 @@ static int rbd_header_from_disk(struct rbd_image_header *header,
header->snap_names = kmalloc(header->snap_names_len, header->snap_names = kmalloc(header->snap_names_len,
GFP_KERNEL); GFP_KERNEL);
if (!header->snap_names) if (!header->snap_names)
goto err_snapc; goto out_err;
size = snap_count * sizeof (*header->snap_sizes); size = snap_count * sizeof (*header->snap_sizes);
header->snap_sizes = kmalloc(size, GFP_KERNEL); header->snap_sizes = kmalloc(size, GFP_KERNEL);
if (!header->snap_sizes) if (!header->snap_sizes)
goto err_names; goto out_err;
} else { } else {
WARN_ON(ondisk->snap_names_len); WARN_ON(ondisk->snap_names_len);
header->snap_names_len = 0; header->snap_names_len = 0;
...@@ -530,22 +534,23 @@ static int rbd_header_from_disk(struct rbd_image_header *header, ...@@ -530,22 +534,23 @@ static int rbd_header_from_disk(struct rbd_image_header *header,
header->snap_sizes = NULL; header->snap_sizes = NULL;
} }
size = sizeof (ondisk->block_name) + 1;
header->object_prefix = kmalloc(size, GFP_KERNEL);
if (!header->object_prefix)
goto err_sizes;
memcpy(header->object_prefix, ondisk->block_name, size - 1);
header->object_prefix[size - 1] = '\0';
header->image_size = le64_to_cpu(ondisk->image_size); header->image_size = le64_to_cpu(ondisk->image_size);
header->obj_order = ondisk->options.order; header->obj_order = ondisk->options.order;
header->crypt_type = ondisk->options.crypt_type; header->crypt_type = ondisk->options.crypt_type;
header->comp_type = ondisk->options.comp_type; header->comp_type = ondisk->options.comp_type;
header->total_snaps = snap_count;
/* Set up the snapshot context */
size = sizeof (struct ceph_snap_context);
size += snap_count * sizeof (header->snapc->snaps[0]);
header->snapc = kzalloc(size, GFP_KERNEL);
if (!header->snapc)
goto out_err;
atomic_set(&header->snapc->nref, 1); atomic_set(&header->snapc->nref, 1);
header->snapc->seq = le64_to_cpu(ondisk->snap_seq); header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
header->snapc->num_snaps = snap_count; header->snapc->num_snaps = snap_count;
header->total_snaps = snap_count;
if (snap_count && allocated_snaps == snap_count) { if (snap_count && allocated_snaps == snap_count) {
int i; int i;
...@@ -564,16 +569,14 @@ static int rbd_header_from_disk(struct rbd_image_header *header, ...@@ -564,16 +569,14 @@ static int rbd_header_from_disk(struct rbd_image_header *header,
return 0; return 0;
err_sizes: out_err:
kfree(header->snap_sizes); kfree(header->snap_sizes);
header->snap_sizes = NULL; header->snap_sizes = NULL;
err_names:
kfree(header->snap_names); kfree(header->snap_names);
header->snap_names = NULL; header->snap_names = NULL;
header->snap_names_len = 0; header->snap_names_len = 0;
err_snapc: kfree(header->object_prefix);
kfree(header->snapc); header->object_prefix = NULL;
header->snapc = NULL;
return -ENOMEM; return -ENOMEM;
} }
......
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