提交 23588797 编写于 作者: K Kevin Wolf

qcow2: Use BB functions in .bdrv_create()

All users of the block layers are supposed to go through a BlockBackend.
The .bdrv_create() implementation is one such user, so this patch
converts it.
Signed-off-by: NKevin Wolf <kwolf@redhat.com>
上级 6af40160
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "qemu/osdep.h" #include "qemu/osdep.h"
#include "qemu-common.h" #include "qemu-common.h"
#include "block/block_int.h" #include "block/block_int.h"
#include "sysemu/block-backend.h"
#include "qemu/module.h" #include "qemu/module.h"
#include <zlib.h> #include <zlib.h>
#include "block/qcow2.h" #include "block/qcow2.h"
...@@ -2097,7 +2098,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2097,7 +2098,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
* 2 GB for 64k clusters, and we don't want to have a 2 GB initial file * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
* size for any qcow2 image. * size for any qcow2 image.
*/ */
BlockDriverState* bs; BlockBackend *blk;
QCowHeader *header; QCowHeader *header;
uint64_t* refcount_table; uint64_t* refcount_table;
Error *local_err = NULL; Error *local_err = NULL;
...@@ -2172,15 +2173,16 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2172,15 +2173,16 @@ static int qcow2_create2(const char *filename, int64_t total_size,
return ret; return ret;
} }
bs = NULL; blk = blk_new_open("image", filename, NULL, NULL,
ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL, &local_err);
&local_err); if (blk == NULL) {
if (ret < 0) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
return ret; return -EIO;
} }
blk_set_allow_write_beyond_eof(blk, true);
/* Write the header */ /* Write the header */
QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header)); QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
header = g_malloc0(cluster_size); header = g_malloc0(cluster_size);
...@@ -2208,7 +2210,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2208,7 +2210,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
} }
ret = bdrv_pwrite(bs, 0, header, cluster_size); ret = blk_pwrite(blk, 0, header, cluster_size);
g_free(header); g_free(header);
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Could not write qcow2 header"); error_setg_errno(errp, -ret, "Could not write qcow2 header");
...@@ -2218,7 +2220,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2218,7 +2220,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
/* Write a refcount table with one refcount block */ /* Write a refcount table with one refcount block */
refcount_table = g_malloc0(2 * cluster_size); refcount_table = g_malloc0(2 * cluster_size);
refcount_table[0] = cpu_to_be64(2 * cluster_size); refcount_table[0] = cpu_to_be64(2 * cluster_size);
ret = bdrv_pwrite(bs, cluster_size, refcount_table, 2 * cluster_size); ret = blk_pwrite(blk, cluster_size, refcount_table, 2 * cluster_size);
g_free(refcount_table); g_free(refcount_table);
if (ret < 0) { if (ret < 0) {
...@@ -2226,8 +2228,8 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2226,8 +2228,8 @@ static int qcow2_create2(const char *filename, int64_t total_size,
goto out; goto out;
} }
bdrv_unref(bs); blk_unref(blk);
bs = NULL; blk = NULL;
/* /*
* And now open the image and make it consistent first (i.e. increase the * And now open the image and make it consistent first (i.e. increase the
...@@ -2236,15 +2238,16 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2236,15 +2238,16 @@ static int qcow2_create2(const char *filename, int64_t total_size,
*/ */
options = qdict_new(); options = qdict_new();
qdict_put(options, "driver", qstring_from_str("qcow2")); qdict_put(options, "driver", qstring_from_str("qcow2"));
ret = bdrv_open(&bs, filename, NULL, options, blk = blk_new_open("image-qcow2", filename, NULL, options,
BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH, BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH,
&local_err); &local_err);
if (ret < 0) { if (blk == NULL) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
ret = -EIO;
goto out; goto out;
} }
ret = qcow2_alloc_clusters(bs, 3 * cluster_size); ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size);
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 " error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
"header and refcount table"); "header and refcount table");
...@@ -2256,14 +2259,14 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2256,14 +2259,14 @@ static int qcow2_create2(const char *filename, int64_t total_size,
} }
/* Create a full header (including things like feature table) */ /* Create a full header (including things like feature table) */
ret = qcow2_update_header(bs); ret = qcow2_update_header(blk_bs(blk));
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Could not update qcow2 header"); error_setg_errno(errp, -ret, "Could not update qcow2 header");
goto out; goto out;
} }
/* Okay, now that we have a valid image, let's give it the right size */ /* Okay, now that we have a valid image, let's give it the right size */
ret = bdrv_truncate(bs, total_size); ret = blk_truncate(blk, total_size);
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Could not resize image"); error_setg_errno(errp, -ret, "Could not resize image");
goto out; goto out;
...@@ -2271,7 +2274,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2271,7 +2274,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
/* Want a backing file? There you go.*/ /* Want a backing file? There you go.*/
if (backing_file) { if (backing_file) {
ret = bdrv_change_backing_file(bs, backing_file, backing_format); ret = bdrv_change_backing_file(blk_bs(blk), backing_file, backing_format);
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Could not assign backing file '%s' " error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
"with format '%s'", backing_file, backing_format); "with format '%s'", backing_file, backing_format);
...@@ -2281,9 +2284,9 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2281,9 +2284,9 @@ static int qcow2_create2(const char *filename, int64_t total_size,
/* And if we're supposed to preallocate metadata, do that now */ /* And if we're supposed to preallocate metadata, do that now */
if (prealloc != PREALLOC_MODE_OFF) { if (prealloc != PREALLOC_MODE_OFF) {
BDRVQcow2State *s = bs->opaque; BDRVQcow2State *s = blk_bs(blk)->opaque;
qemu_co_mutex_lock(&s->lock); qemu_co_mutex_lock(&s->lock);
ret = preallocate(bs); ret = preallocate(blk_bs(blk));
qemu_co_mutex_unlock(&s->lock); qemu_co_mutex_unlock(&s->lock);
if (ret < 0) { if (ret < 0) {
error_setg_errno(errp, -ret, "Could not preallocate metadata"); error_setg_errno(errp, -ret, "Could not preallocate metadata");
...@@ -2291,24 +2294,25 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -2291,24 +2294,25 @@ static int qcow2_create2(const char *filename, int64_t total_size,
} }
} }
bdrv_unref(bs); blk_unref(blk);
bs = NULL; blk = NULL;
/* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */ /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
options = qdict_new(); options = qdict_new();
qdict_put(options, "driver", qstring_from_str("qcow2")); qdict_put(options, "driver", qstring_from_str("qcow2"));
ret = bdrv_open(&bs, filename, NULL, options, blk = blk_new_open("image-flush", filename, NULL, options,
BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING, BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
&local_err); &local_err);
if (local_err) { if (blk == NULL) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
ret = -EIO;
goto out; goto out;
} }
ret = 0; ret = 0;
out: out:
if (bs) { if (blk) {
bdrv_unref(bs); blk_unref(blk);
} }
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册