Skip to content

Commit

Permalink
qcow2: Store data file name in the image
Browse files Browse the repository at this point in the history
Rather than requiring that the external data file node is passed
explicitly when creating the qcow2 node, store the filename in the
designated header extension during .bdrv_create and read it from there
as a default during .bdrv_open.

Signed-off-by: Kevin Wolf <[email protected]>
  • Loading branch information
kevmw committed Mar 8, 2019
1 parent dcc9868 commit 9b890bd
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 2 deletions.
94 changes: 93 additions & 1 deletion block/qcow2.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,21 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
#endif
break;

case QCOW2_EXT_MAGIC_DATA_FILE:
{
s->image_data_file = g_malloc0(ext.len + 1);
ret = bdrv_pread(bs->file, offset, s->image_data_file, ext.len);
if (ret < 0) {
error_setg_errno(errp, -ret,
"ERROR: Could not read data file name");
return ret;
}
#ifdef DEBUG_EXT
printf("Qcow2: Got external data file %s\n", s->image_data_file);
#endif
break;
}

default:
/* unknown magic - save it in case we need to rewrite the header */
/* If you add a new feature, make sure to also update the fast
Expand Down Expand Up @@ -1463,6 +1478,15 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
}

if (s->incompatible_features & QCOW2_INCOMPAT_DATA_FILE) {
if (!s->data_file && s->image_data_file) {
s->data_file = bdrv_open_child(s->image_data_file, options,
"data-file", bs, &child_file,
false, errp);
if (!s->data_file) {
ret = -EINVAL;
goto fail;
}
}
if (!s->data_file) {
error_setg(errp, "'data-file' is required for this image");
ret = -EINVAL;
Expand Down Expand Up @@ -1650,6 +1674,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
return ret;

fail:
g_free(s->image_data_file);
if (has_data_file(bs)) {
bdrv_unref_child(bs, s->data_file);
}
Expand Down Expand Up @@ -2269,6 +2294,7 @@ static void qcow2_close(BlockDriverState *bs)
g_free(s->unknown_header_fields);
cleanup_unknown_header_ext(bs);

g_free(s->image_data_file);
g_free(s->image_backing_file);
g_free(s->image_backing_format);

Expand Down Expand Up @@ -2445,6 +2471,19 @@ int qcow2_update_header(BlockDriverState *bs)
buflen -= ret;
}

/* External data file header extension */
if (has_data_file(bs) && s->image_data_file) {
ret = header_ext_add(buf, QCOW2_EXT_MAGIC_DATA_FILE,
s->image_data_file, strlen(s->image_data_file),
buflen);
if (ret < 0) {
goto fail;
}

buf += ret;
buflen -= ret;
}

/* Full disk encryption header pointer extension */
if (s->crypto_header.offset != 0) {
s->crypto_header.offset = cpu_to_be64(s->crypto_header.offset);
Expand Down Expand Up @@ -3086,6 +3125,12 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
abort();
}

/* Set the external data file if necessary */
if (data_bs) {
BDRVQcow2State *s = blk_bs(blk)->opaque;
s->image_data_file = g_strdup(data_bs->filename);
}

/* Create a full header (including things like feature table) */
ret = qcow2_update_header(blk_bs(blk));
if (ret < 0) {
Expand Down Expand Up @@ -3165,6 +3210,7 @@ static int coroutine_fn qcow2_co_create_opts(const char *filename, QemuOpts *opt
QDict *qdict;
Visitor *v;
BlockDriverState *bs = NULL;
BlockDriverState *data_bs = NULL;
Error *local_err = NULL;
const char *val;
int ret;
Expand Down Expand Up @@ -3228,6 +3274,26 @@ static int coroutine_fn qcow2_co_create_opts(const char *filename, QemuOpts *opt
goto finish;
}

/* Create and open an external data file (protocol layer) */
val = qdict_get_try_str(qdict, BLOCK_OPT_DATA_FILE);
if (val) {
ret = bdrv_create_file(val, opts, errp);
if (ret < 0) {
goto finish;
}

data_bs = bdrv_open(val, NULL, NULL,
BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
errp);
if (data_bs == NULL) {
ret = -EIO;
goto finish;
}

qdict_del(qdict, BLOCK_OPT_DATA_FILE);
qdict_put_str(qdict, "data-file", data_bs->node_name);
}

/* Set 'driver' and 'node' options */
qdict_put_str(qdict, "driver", "qcow2");
qdict_put_str(qdict, "file", bs->node_name);
Expand Down Expand Up @@ -3262,6 +3328,7 @@ static int coroutine_fn qcow2_co_create_opts(const char *filename, QemuOpts *opt
finish:
qobject_unref(qdict);
bdrv_unref(bs);
bdrv_unref(data_bs);
qapi_free_BlockdevCreateOptions(create_options);
return ret;
}
Expand Down Expand Up @@ -4552,6 +4619,8 @@ static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs,
.refcount_bits = s->refcount_bits,
.has_bitmaps = !!bitmaps,
.bitmaps = bitmaps,
.has_data_file = !!s->image_data_file,
.data_file = g_strdup(s->image_data_file),
};
} else {
/* if this assertion fails, this probably means a new version was
Expand Down Expand Up @@ -4754,7 +4823,7 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
BDRVQcow2State *s = bs->opaque;
int old_version = s->qcow_version, new_version = old_version;
uint64_t new_size = 0;
const char *backing_file = NULL, *backing_format = NULL;
const char *backing_file = NULL, *backing_format = NULL, *data_file = NULL;
bool lazy_refcounts = s->use_lazy_refcounts;
const char *compat = NULL;
uint64_t cluster_size = s->cluster_size;
Expand Down Expand Up @@ -4836,6 +4905,13 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
"may not exceed 64 bits");
return -EINVAL;
}
} else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE)) {
data_file = qemu_opt_get(opts, BLOCK_OPT_DATA_FILE);
if (data_file && !has_data_file(bs)) {
error_setg(errp, "data-file can only be set for images that "
"use an external data file");
return -EINVAL;
}
} else {
/* if this point is reached, this probably means a new option was
* added without having it covered here */
Expand Down Expand Up @@ -4882,6 +4958,17 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
}
}

if (data_file) {
g_free(s->image_data_file);
s->image_data_file = *data_file ? g_strdup(data_file) : NULL;
}

ret = qcow2_update_header(bs);
if (ret < 0) {
error_setg_errno(errp, -ret, "Failed to update the image header");
return ret;
}

if (backing_file || backing_format) {
ret = qcow2_change_backing_file(bs,
backing_file ?: s->image_backing_file,
Expand Down Expand Up @@ -5029,6 +5116,11 @@ static QemuOptsList qcow2_create_opts = {
.type = QEMU_OPT_STRING,
.help = "Image format of the base image"
},
{
.name = BLOCK_OPT_DATA_FILE,
.type = QEMU_OPT_STRING,
.help = "File name of an external data file"
},
{
.name = BLOCK_OPT_ENCRYPT,
.type = QEMU_OPT_BOOL,
Expand Down
1 change: 1 addition & 0 deletions block/qcow2.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ typedef struct BDRVQcow2State {
* override) */
char *image_backing_file;
char *image_backing_format;
char *image_data_file;

CoQueue compress_wait_queue;
int nb_compress_threads;
Expand Down
8 changes: 7 additions & 1 deletion qapi/block-core.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
#
# @compat: compatibility level
#
# @data-file: the filename of the external data file that is stored in the
# image and used as a default for opening the image (since: 4.0)
#
# @lazy-refcounts: on or off; only valid for compat >= 1.1
#
# @corrupt: true if the image has been marked corrupt; only valid for
Expand All @@ -76,6 +79,7 @@
{ 'struct': 'ImageInfoSpecificQCow2',
'data': {
'compat': 'str',
'*data-file': 'str',
'*lazy-refcounts': 'bool',
'*corrupt': 'bool',
'refcount-bits': 'int',
Expand Down Expand Up @@ -3082,7 +3086,9 @@
#
# @data-file: reference to or definition of the external data file.
# This may only be specified for images that require an
# external data file. (since 4.0)
# external data file. If it is not specified for such
# an image, the data file name is loaded from the image
# file. (since 4.0)
#
# Since: 2.9
##
Expand Down
Loading

0 comments on commit 9b890bd

Please sign in to comment.