Commit 26f54e9a authored by Markus Armbruster's avatar Markus Armbruster Committed by Kevin Wolf

block: New BlockBackend

A block device consists of a frontend device model and a backend.

A block backend has a tree of block drivers doing the actual work.
The tree is managed by the block layer.

We currently use a single abstraction BlockDriverState both for tree
nodes and the backend as a whole.  Drawbacks:

* Its API includes both stuff that makes sense only at the block
  backend level (root of the tree) and stuff that's only for use
  within the block layer.  This makes the API bigger and more complex
  than necessary.  Moreover, it's not obvious which interfaces are
  meant for device models, and which really aren't.

* Since device models keep a reference to their backend, the backend
  object can't just be destroyed.  But for media change, we need to
  replace the tree.  Our solution is to make the BlockDriverState
  generic, with actual driver state in a separate object, pointed to
  by member opaque.  That lets us replace the tree by deinitializing
  and reinitializing its root.  This special need of the root makes
  the data structure awkward everywhere in the tree.

The general plan is to separate the APIs into "block backend", for use
by device models, monitor and whatever other code dealing with block
backends, and "block driver", for use by the block layer and whatever
other code (if any) dealing with trees and tree nodes.

Code dealing with block backends, device models in particular, should
become completely oblivious of BlockDriverState.  This should let us
clean up both APIs, and the tree data structures.

This commit is a first step.  It creates a minimal "block backend"
API: type BlockBackend and functions to create, destroy and find them.

BlockBackend objects are created and destroyed exactly when root
BlockDriverState objects are created and destroyed.  "Root" in the
sense of "in bdrv_states".  They're not yet used for anything; that'll
come shortly.

A root BlockDriverState is created with bdrv_new_root(), so where to
create a BlockBackend is obvious.  Where these roots get destroyed
isn't always as obvious.

It is obvious in qemu-img.c, qemu-io.c and qemu-nbd.c, and in error
paths of blockdev_init(), blk_connect().  That leaves destruction of
objects successfully created by blockdev_init() and blk_connect().

blockdev_init() is used only by drive_new() and qmp_blockdev_add().
Objects created by the latter are currently indestructible (see commit
48f364dd "blockdev: Refuse to drive_del something added with
blockdev-add" and commit 2d246f01 "blockdev: Introduce
DriveInfo.enable_auto_del").  Objects created by the former get
destroyed by drive_del().

Objects created by blk_connect() get destroyed by blk_disconnect().

BlockBackend is reference-counted.  Its reference count never exceeds
one so far, but that's going to change.

In drive_del(), the BB's reference count is surely one now.  The BDS's
reference count is greater than one when something else is holding a
reference, such as a block job.  In this case, the BB is destroyed
right away, but the BDS lives on until all extra references get
dropped.
Signed-off-by: default avatarMarkus Armbruster <armbru@redhat.com>
Reviewed-by: default avatarMax Reitz <mreitz@redhat.com>
Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
parent e4e9986b
......@@ -5,7 +5,7 @@ block-obj-y += qed-check.o
block-obj-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o vhdx-log.o
block-obj-$(CONFIG_QUORUM) += quorum.o
block-obj-y += parallels.o blkdebug.o blkverify.o
block-obj-y += snapshot.o qapi.o
block-obj-y += block-backend.o snapshot.o qapi.o
block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
block-obj-$(CONFIG_POSIX) += raw-posix.o
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
......
/*
* 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;
}
......@@ -30,6 +30,7 @@
* THE SOFTWARE.
*/
#include "sysemu/block-backend.h"
#include "sysemu/blockdev.h"
#include "hw/block/block.h"
#include "block/blockjob.h"
......@@ -278,7 +279,10 @@ static void bdrv_format_print(void *opaque, const char *name)
void drive_del(DriveInfo *dinfo)
{
BlockBackend *blk = blk_by_name(dinfo->id);
bdrv_unref(dinfo->bdrv);
blk_unref(blk);
}
void drive_info_del(DriveInfo *dinfo)
......@@ -367,6 +371,7 @@ static DriveInfo *blockdev_init(const char *file, QDict *bs_opts,
int ro = 0;
int bdrv_flags = 0;
int on_read_error, on_write_error;
BlockBackend *blk;
BlockDriverState *bs;
DriveInfo *dinfo;
ThrottleConfig cfg;
......@@ -523,9 +528,13 @@ static DriveInfo *blockdev_init(const char *file, QDict *bs_opts,
}
/* init */
blk = blk_new(qemu_opts_id(opts), errp);
if (!blk) {
goto early_err;
}
bs = bdrv_new_root(qemu_opts_id(opts), errp);
if (!bs) {
goto early_err;
goto bdrv_new_err;
}
bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
bs->read_only = ro;
......@@ -591,6 +600,8 @@ static DriveInfo *blockdev_init(const char *file, QDict *bs_opts,
err:
bdrv_unref(bs);
bdrv_new_err:
blk_unref(blk);
early_err:
qemu_opts_del(opts);
err_no_opts:
......
......@@ -39,6 +39,7 @@
#include "hw/xen/xen_backend.h"
#include "xen_blkif.h"
#include "sysemu/blockdev.h"
#include "sysemu/block-backend.h"
/* ------------------------------------------------------------- */
......@@ -854,12 +855,18 @@ static int blk_connect(struct XenDevice *xendev)
blkdev->dinfo = drive_get(IF_XEN, 0, index);
if (!blkdev->dinfo) {
Error *local_err = NULL;
BlockBackend *blk;
BlockDriver *drv;
/* setup via xenbus -> create new block driver instance */
xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
blk = blk_new(blkdev->dev, NULL);
if (!blk) {
return -1;
}
blkdev->bs = bdrv_new_root(blkdev->dev, NULL);
if (!blkdev->bs) {
blk_unref(blk);
return -1;
}
......@@ -870,6 +877,7 @@ static int blk_connect(struct XenDevice *xendev)
error_get_pretty(local_err));
error_free(local_err);
bdrv_unref(blkdev->bs);
blk_unref(blk);
blkdev->bs = NULL;
return -1;
}
......@@ -985,6 +993,9 @@ static void blk_disconnect(struct XenDevice *xendev)
if (blkdev->bs) {
bdrv_detach_dev(blkdev->bs, blkdev);
bdrv_unref(blkdev->bs);
if (!blkdev->dinfo) {
blk_unref(blk_by_name(blkdev->dev));
}
blkdev->bs = NULL;
}
xen_be_unbind_evtchn(&blkdev->xendev);
......
......@@ -36,6 +36,7 @@ typedef struct MachineState MachineState;
typedef struct NICInfo NICInfo;
typedef struct HCIInfo HCIInfo;
typedef struct AudioState AudioState;
typedef struct BlockBackend BlockBackend;
typedef struct BlockDriverState BlockDriverState;
typedef struct DriveInfo DriveInfo;
typedef struct DisplayState DisplayState;
......
/*
* 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.
*/
#ifndef BLOCK_BACKEND_H
#define BLOCK_BACKEND_H
#include "qemu/typedefs.h"
#include "qapi/error.h"
BlockBackend *blk_new(const char *name, Error **errp);
void blk_ref(BlockBackend *blk);
void blk_unref(BlockBackend *blk);
const char *blk_name(BlockBackend *blk);
BlockBackend *blk_by_name(const char *name);
BlockBackend *blk_next(BlockBackend *blk);
#endif
This diff is collapsed.
......@@ -19,6 +19,7 @@
#include "qemu/option.h"
#include "qemu/config-file.h"
#include "qemu/readline.h"
#include "sysemu/block-backend.h"
#include "block/block_int.h"
#include "trace/control.h"
......@@ -26,6 +27,7 @@
static char *progname;
static BlockBackend *qemuio_blk;
static BlockDriverState *qemuio_bs;
/* qemu-io commands passed using -c */
......@@ -37,7 +39,9 @@ static ReadLineState *readline_state;
static int close_f(BlockDriverState *bs, int argc, char **argv)
{
bdrv_unref(bs);
blk_unref(qemuio_blk);
qemuio_bs = NULL;
qemuio_blk = NULL;
return 0;
}
......@@ -58,6 +62,7 @@ static int openfile(char *name, int flags, int growable, QDict *opts)
return 1;
}
qemuio_blk = blk_new("hda", &error_abort);
qemuio_bs = bdrv_new_root("hda", &error_abort);
if (growable) {
......@@ -70,7 +75,9 @@ static int openfile(char *name, int flags, int growable, QDict *opts)
error_get_pretty(local_err));
error_free(local_err);
bdrv_unref(qemuio_bs);
blk_unref(qemuio_blk);
qemuio_bs = NULL;
qemuio_blk = NULL;
return 1;
}
......@@ -484,6 +491,7 @@ int main(int argc, char **argv)
if (qemuio_bs) {
bdrv_unref(qemuio_bs);
}
blk_unref(qemuio_blk);
g_free(readline_state);
return 0;
}
......@@ -17,7 +17,7 @@
*/
#include "qemu-common.h"
#include "block/block.h"
#include "sysemu/block-backend.h"
#include "block/block_int.h"
#include "block/nbd.h"
#include "qemu/main-loop.h"
......@@ -384,6 +384,7 @@ static void nbd_accept(void *opaque)
int main(int argc, char **argv)
{
BlockBackend *blk;
BlockDriverState *bs;
BlockDriver *drv;
off_t dev_offset = 0;
......@@ -691,6 +692,7 @@ int main(int argc, char **argv)
drv = NULL;
}
blk = blk_new("hda", &error_abort);
bs = bdrv_new_root("hda", &error_abort);
srcpath = argv[optind];
......@@ -774,6 +776,7 @@ int main(int argc, char **argv)
} while (state != TERMINATED);
bdrv_unref(bs);
blk_unref(blk);
if (sockpath) {
unlink(sockpath);
}
......
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