--- /dev/null
+/*
+ * QEMU Block backends
+ *
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * Authors:
+ * Markus Armbruster <armbru@redhat.com>,
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1
+ * or later. See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "sysemu/block-backend.h"
+#include "block/block_int.h"
+
+struct BlockBackend {
+ char *name;
+ int refcnt;
+ QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */
+};
+
+/* All the BlockBackends */
+static QTAILQ_HEAD(, BlockBackend) blk_backends =
+ QTAILQ_HEAD_INITIALIZER(blk_backends);
+
+/*
+ * Create a new BlockBackend with @name, with a reference count of one.
+ * @name must not be null or empty.
+ * Fail if a BlockBackend with this name already exists.
+ * Store an error through @errp on failure, unless it's null.
+ * Return the new BlockBackend on success, null on failure.
+ */
+BlockBackend *blk_new(const char *name, Error **errp)
+{
+ BlockBackend *blk;
+
+ assert(name && name[0]);
+ if (blk_by_name(name)) {
+ error_setg(errp, "Device with id '%s' already exists", name);
+ return NULL;
+ }
+
+ blk = g_new0(BlockBackend, 1);
+ blk->name = g_strdup(name);
+ blk->refcnt = 1;
+ QTAILQ_INSERT_TAIL(&blk_backends, blk, link);
+ return blk;
+}
+
+static void blk_delete(BlockBackend *blk)
+{
+ assert(!blk->refcnt);
+ QTAILQ_REMOVE(&blk_backends, blk, link);
+ g_free(blk->name);
+ g_free(blk);
+}
+
+/*
+ * Increment @blk's reference count.
+ * @blk must not be null.
+ */
+void blk_ref(BlockBackend *blk)
+{
+ blk->refcnt++;
+}
+
+/*
+ * Decrement @blk's reference count.
+ * If this drops it to zero, destroy @blk.
+ * For convenience, do nothing if @blk is null.
+ */
+void blk_unref(BlockBackend *blk)
+{
+ if (blk) {
+ assert(blk->refcnt > 0);
+ if (!--blk->refcnt) {
+ blk_delete(blk);
+ }
+ }
+}
+
+/*
+ * Return the BlockBackend after @blk.
+ * If @blk is null, return the first one.
+ * Else, return @blk's next sibling, which may be null.
+ *
+ * To iterate over all BlockBackends, do
+ * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
+ * ...
+ * }
+ */
+BlockBackend *blk_next(BlockBackend *blk)
+{
+ return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
+}
+
+/*
+ * Return @blk's name, a non-null, non-empty string.
+ */
+const char *blk_name(BlockBackend *blk)
+{
+ return blk->name;
+}
+
+/*
+ * Return the BlockBackend with name @name if it exists, else null.
+ * @name must not be null.
+ */
+BlockBackend *blk_by_name(const char *name)
+{
+ BlockBackend *blk;
+
+ assert(name);
+ QTAILQ_FOREACH(blk, &blk_backends, link) {
+ if (!strcmp(name, blk->name)) {
+ return blk;
+ }
+ }
+ return NULL;
+}
#include "qemu/error-report.h"
#include "qemu/osdep.h"
#include "sysemu/sysemu.h"
+#include "sysemu/block-backend.h"
#include "block/block_int.h"
#include "block/qapi.h"
#include <getopt.h>
int c, ret;
OutputFormat output_format = OFORMAT_HUMAN;
const char *filename, *fmt, *output, *cache;
+ BlockBackend *blk;
BlockDriverState *bs;
int fix = 0;
int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
- ImageCheck *check;
+ ImageCheck *check = NULL;
bool quiet = false;
fmt = NULL;
return 1;
}
+ blk = blk_new("image", &error_abort);
bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
if (!bs) {
- return 1;
+ ret = 1;
+ goto fail;
}
check = g_new0(ImageCheck, 1);
fail:
qapi_free_ImageCheck(check);
bdrv_unref(bs);
+ blk_unref(blk);
return ret;
}
{
int c, ret, flags;
const char *filename, *fmt, *cache;
+ BlockBackend *blk;
BlockDriverState *bs;
bool quiet = false;
return 1;
}
+ blk = blk_new("image", &error_abort);
bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
if (!bs) {
- return 1;
+ ret = -1;
+ goto out;
}
ret = bdrv_commit(bs);
switch(ret) {
break;
}
+out:
bdrv_unref(bs);
+ blk_unref(blk);
if (ret) {
return 1;
}
static int img_compare(int argc, char **argv)
{
const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
+ BlockBackend *blk1, *blk2;
BlockDriverState *bs1, *bs2;
int64_t total_sectors1, total_sectors2;
uint8_t *buf1 = NULL, *buf2 = NULL;
goto out3;
}
+ blk1 = blk_new("image_1", &error_abort);
bs1 = bdrv_new_open("image_1", filename1, fmt1, flags, true, quiet);
if (!bs1) {
error_report("Can't open file %s", filename1);
ret = 2;
- goto out3;
+ goto out2;
}
+ blk2 = blk_new("image_2", &error_abort);
bs2 = bdrv_new_open("image_2", filename2, fmt2, flags, true, quiet);
if (!bs2) {
error_report("Can't open file %s", filename2);
ret = 2;
- goto out2;
+ goto out1;
}
buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
ret = 0;
out:
- bdrv_unref(bs2);
qemu_vfree(buf1);
qemu_vfree(buf2);
+out1:
+ bdrv_unref(bs2);
+ blk_unref(blk2);
out2:
bdrv_unref(bs1);
+ blk_unref(blk1);
out3:
qemu_progress_end();
return ret;
int progress = 0, flags, src_flags;
const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
BlockDriver *drv, *proto_drv;
+ BlockBackend **blk = NULL, *out_blk = NULL;
BlockDriverState **bs = NULL, *out_bs = NULL;
int64_t total_sectors, nb_sectors, sector_num, bs_offset;
int64_t *bs_sectors = NULL;
qemu_progress_print(0, 100);
+ blk = g_new0(BlockBackend *, bs_n);
bs = g_new0(BlockDriverState *, bs_n);
bs_sectors = g_new(int64_t, bs_n);
for (bs_i = 0; bs_i < bs_n; bs_i++) {
char *id = bs_n > 1 ? g_strdup_printf("source_%d", bs_i)
: g_strdup("source");
+ blk[bs_i] = blk_new(id, &error_abort);
bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, src_flags,
true, quiet);
g_free(id);
goto out;
}
+ out_blk = blk_new("target", &error_abort);
out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
if (!out_bs) {
ret = -1;
if (out_bs) {
bdrv_unref(out_bs);
}
+ blk_unref(out_blk);
if (bs) {
for (bs_i = 0; bs_i < bs_n; bs_i++) {
if (bs[bs_i]) {
}
g_free(bs);
}
+ if (blk) {
+ for (bs_i = 0; bs_i < bs_n; bs_i++) {
+ blk_unref(blk[bs_i]);
+ }
+ g_free(blk);
+ }
g_free(bs_sectors);
fail_getopt:
g_free(options);
filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
while (filename) {
+ BlockBackend *blk;
BlockDriverState *bs;
ImageInfo *info;
ImageInfoList *elem;
}
g_hash_table_insert(filenames, (gpointer)filename, NULL);
+ blk = blk_new("image", &error_abort);
bs = bdrv_new_open("image", filename, fmt,
BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
if (!bs) {
+ blk_unref(blk);
goto err;
}
error_report("%s", error_get_pretty(err));
error_free(err);
bdrv_unref(bs);
+ blk_unref(blk);
goto err;
}
last = &elem->next;
bdrv_unref(bs);
+ blk_unref(blk);
filename = fmt = NULL;
if (chain) {
{
int c;
OutputFormat output_format = OFORMAT_HUMAN;
+ BlockBackend *blk;
BlockDriverState *bs;
const char *filename, *fmt, *output;
int64_t length;
return 1;
}
+ blk = blk_new("image", &error_abort);
bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
if (!bs) {
- return 1;
+ ret = -1;
+ goto out;
}
if (output_format == OFORMAT_HUMAN) {
out:
bdrv_unref(bs);
+ blk_unref(blk);
return ret < 0;
}
static int img_snapshot(int argc, char **argv)
{
+ BlockBackend *blk;
BlockDriverState *bs;
QEMUSnapshotInfo sn;
char *filename, *snapshot_name = NULL;
filename = argv[optind++];
/* Open the image */
+ blk = blk_new("image", &error_abort);
bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
if (!bs) {
- return 1;
+ ret = -1;
+ goto out;
}
/* Perform the requested action */
}
/* Cleanup */
+out:
bdrv_unref(bs);
+ blk_unref(blk);
if (ret) {
return 1;
}
static int img_rebase(int argc, char **argv)
{
+ BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL;
BlockDriverState *bs = NULL, *bs_old_backing = NULL, *bs_new_backing = NULL;
BlockDriver *old_backing_drv, *new_backing_drv;
char *filename;
* Ignore the old backing file for unsafe rebase in case we want to correct
* the reference to a renamed or moved backing file.
*/
+ blk = blk_new("image", &error_abort);
bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
if (!bs) {
ret = -1;
if (!unsafe) {
char backing_name[1024];
+ blk_old_backing = blk_new("old_backing", &error_abort);
bs_old_backing = bdrv_new_root("old_backing", &error_abort);
bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, src_flags,
goto out;
}
if (out_baseimg[0]) {
+ blk_new_backing = blk_new("new_backing", &error_abort);
bs_new_backing = bdrv_new_root("new_backing", &error_abort);
ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL, src_flags,
new_backing_drv, &local_err);
if (bs_old_backing != NULL) {
bdrv_unref(bs_old_backing);
}
+ blk_unref(blk_old_backing);
if (bs_new_backing != NULL) {
bdrv_unref(bs_new_backing);
}
+ blk_unref(blk_new_backing);
}
bdrv_unref(bs);
+ blk_unref(blk);
if (ret) {
return 1;
}
const char *filename, *fmt, *size;
int64_t n, total_size;
bool quiet = false;
+ BlockBackend *blk = NULL;
BlockDriverState *bs = NULL;
QemuOpts *param;
static QemuOptsList resize_options = {
n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
qemu_opts_del(param);
+ blk = blk_new("image", &error_abort);
bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
true, quiet);
if (!bs) {
if (bs) {
bdrv_unref(bs);
}
+ blk_unref(blk);
if (ret) {
return 1;
}
const char *fmt = NULL, *filename, *cache;
int flags;
bool quiet = false;
+ BlockBackend *blk = NULL;
BlockDriverState *bs = NULL;
cache = BDRV_DEFAULT_CACHE;
goto out;
}
+ blk = blk_new("image", &error_abort);
bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
if (!bs) {
error_report("Could not open image '%s'", filename);
if (bs) {
bdrv_unref(bs);
}
+ blk_unref(blk);
qemu_opts_del(opts);
qemu_opts_free(create_opts);
g_free(options);