RFC: add Spice block device
cover RFC: add Spice block device
Hi,
The following patch series implement a Spice block device, which allows the client to redirect a block device using the NBD protocol, which greatly simplifies the Spice code by reusing an existing protocol, and allows sharing existing qemu NBD implementation.
This block device driver is a bit special, since it is successfully initialized with size 0, and once the client is connected (or want to change block device) it re-opens itself. For this to work, we allow a block driver to be open with an existing opaque data.
The backend only support read-only device atm (although it shouldn't be hard to add write support if necessary). Migration hasn't been tested yet.
Usage with a CDROM drive:
-device ide-cd,drive=cd -drive if=none,id=cd,readonly,file=spicebd:
The associated server and client bits are:
http://lists.freedesktop.org/archives/spice-devel/2013-June/013608.html
http://lists.freedesktop.org/archives/spice-devel/2013-June/013609.html
http://lists.freedesktop.org/archives/spice-devel/2013-June/013610.html
Marc-André Lureau (12):
include: add missing config-host.h include
char: add qemu_chr_fe_event()
nbd: don't change socket block during negotiate Split nbd block client code
nbd: pass export name as init argument
nbd: make session_close() idempotent
block: save the associated child in BlockDriverState
block: extract make_snapshot() from bdrv_open()
block: add "snapshot.size" option to avoid extra bdrv_open()
block: learn to open a driver with a given opaque
block: allow to call bdrv_open() with an opaque
block: add spice block device backend
block.c | 191 ++++++++++------- block/Makefile.objs | 3 +- block/nbd-client.c | 391 ++++++++++++++++++++++++++++++++++ block/nbd-client.h | 51 +++++ block/nbd.c | 394 ++++------------------------------ block/spice.c | 523 ++++++++++++++++++++++++++++++++++++++++++++++ include/block/block_int.h | 1 + include/sysemu/char.h | 10 + include/ui/qemu-spice.h | 2 + nbd.c | 1 - qemu-char.c | 7 + spice-qemu-char.c | 10 + 12 files changed, 1151 insertions(+), 433 deletions(-) create mode 100644 block/nbd-client.c create mode 100644 block/nbd-client.h create mode 100644 block/spice.c
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 1/12 include: add missing config-host.h include
--- include/ui/qemu-spice.h | 2 ++ 1 file changed, 2 insertions(+)
include/ui/qemu-spice.h +2 -0
@@ -18,6 +18,8 @@#ifndef QEMU_SPICE_H #define QEMU_SPICE_H +#include "config-host.h" +#ifdef CONFIG_SPICE #include <spice.h>
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 2/12 char: add qemu_chr_fe_event()
In reply to Marc-André Lureau--- include/sysemu/char.h | 10 ++++++++++ qemu-char.c | 7 +++++++ spice-qemu-char.c | 10 ++++++++++ 3 files changed, 27 insertions(+)
include/sysemu/char.h +10 -0
@@ -69,6 +69,7 @@ struct CharDriverState {void (*chr_accept_input)(struct CharDriverState *chr);void (*chr_set_echo)(struct CharDriverState *chr,bool echo);void (*chr_set_fe_open)(struct CharDriverState *chr,int fe_open); +void (*chr_fe_event)(struct CharDriverState *chr,int event);void *opaque;char *label;char *filename;
@@ -136,6 +137,15 @@ void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo);void qemu_chr_fe_set_open (struct CharDriverState *chr,int fe_open);/** +* @qemu_chr_fe_event: +* +* Send an event from the back end to the front end. +* +* @event the event to send +*/ +void qemu_chr_fe_event (CharDriverState *s,int event); + +/** * @qemu_chr_fe_printf: * * Write to a character backend using a printf style interface.
qemu-char.c +7 -0
@@ -3317,6 +3317,13 @@ void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open) } } +void qemu_chr_fe_event (struct CharDriverState *chr,int event) +{ +if (chr->chr_fe_event) { + chr->chr_fe_event (chr, event); + } +} +int qemu_chr_fe_add_watch (CharDriverState *s,GIOCondition cond,GIOFunc func,void *user_data) {
spice-qemu-char.c +10 -0
@@ -223,6 +223,15 @@ static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open) } } +static void spice_chr_fe_event (struct CharDriverState *chr,int event) +{ +#if SPICE_SERVER_VERSION >=0x000c02 +SpiceCharDriver *s = chr->opaque; + +spice_server_port_event (&s->sin, event); +#endif +} +static void print_allowed_subtypes (void ) {const char ** psubtype;
@@ -256,6 +265,7 @@ static CharDriverState *chr_open(const char *subtype) chr->chr_close = spice_chr_close; chr->chr_set_fe_open = spice_chr_set_fe_open; chr->explicit_be_open = true; + chr->chr_fe_event = spice_chr_fe_event;QLIST_INSERT_HEAD (&spice_chars, s, next);
Signature · 1 line
1.8.3.rc1.49.g8d97506
Re: [Qemu-devel] [PATCH 02/12] char: add qemu_chr_fe_event()
Hi,
spice-qemu-char.c static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open) · line 233
Quoted 8 lines
spice-qemu-char.c +8
-0+static void spice_chr_fe_event (struct CharDriverState *chr,int event) +{ +#if SPICE_SERVER_VERSION >=0x000c02 +SpiceCharDriver *s = chr->opaque; + +spice_server_port_event (&s->sin, event); +#endif +}
No way. You are passing an qemu-internal value (event) to an external library here. That is going to cause major grief in case the internal change some day, so please don't.
I'd suggest to have something like this instead:
switch (event) {
case OPEN:
spice_server_port_open()
break;
[ ... ]
}cheers,
Gerd
PS: Small historical lesson: spice-server 0.4.x (IIRC) was full of
these, which was a major blocker of the upstream merge of spice support. spice-server 0.6.x got a radically different library interface to fix that. A few places escaped review, so we still have this in a few minor places, mouse button numbering for example. Luckily this is a place where changes are unlikely. But please don't let us add new ones.
Re: [Qemu-devel] [PATCH 02/12] char: add qemu_chr_fe_event()
Quoted 15 lines · Gerd Hoffmann
Hi,
spice-qemu-char.c static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open) · line 233
Quoted 8 lines
spice-qemu-char.c +8
-0+static void spice_chr_fe_event (struct CharDriverState *chr,int event) +{ +#if SPICE_SERVER_VERSION >=0x000c02 +SpiceCharDriver *s = chr->opaque; + +spice_server_port_event (&s->sin, event); +#endif +}No way. You are passing an qemu-internal value (event) to an external library here. That is going to cause major grief in case the internal change some day, so please don't.
Quoted 9 lines
I'd suggest to have something like this instead:
switch (event) { case OPEN: spice_server_port_open() break; [ ... ] }
Oh yeah, agreed.
Since the Spice server API already has spice_server_port_event() and events, I will change it to:
switch (event) {
case OPEN:
spice_server_port_open(SPICE_PORT_EVENT_OPENED)
Quoted 12 lines
cheers,
GerdPS: Small historical lesson: spice-server 0.4.x (IIRC) was full of
these, which was a major blocker of the upstream merge of spice support. spice-server 0.6.x got a radically different library interface to fix that. A few places escaped review, so we still have this in a few minor places, mouse button numbering for example. Luckily this is a place where changes are unlikely. But please don't let us add new ones.
Signature · 1 line
Marc-André Lureau
PATCH 3/12 nbd: don't change socket block during negotiate
In reply to Marc-André LureauThe caller might handle non-blocking using coroutine. Leave the choice to the caller to use a blocking or non-blocking noegotiate.
--- nbd.c | 1 - 1 file changed, 1 deletion(-)
nbd.c +0 -1
@@ -442,7 +442,6 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,TRACE ("Receiving negotiation." ); -qemu_set_block (csock); rc = -EINVAL ;if (read_sync (csock, buf,8 ) !=8 ) {
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 4/12 Split nbd block client code
In reply to Marc-André Lureau--- block/Makefile.objs | 2 +- block/nbd-client.c | 386 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/nbd-client.h | 52 +++++++ block/nbd.c | 387 ++++------------------------------------------------ 4 files changed, 469 insertions(+), 358 deletions(-) create mode 100644 block/nbd-client.c create mode 100644 block/nbd-client.h
block/Makefile.objs +1 -1
@@ -10,7 +10,7 @@ block-obj-$(CONFIG_POSIX) += raw-posix.o block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o ifeq ($(CONFIG_POSIX),y) -block-obj-y += nbd.o sheepdog.o +block-obj-y += nbd.o nbd-client.o sheepdog.o block-obj-$(CONFIG_LIBISCSI) += iscsi.o block-obj-$(CONFIG_CURL) += curl.o block-obj-$(CONFIG_RBD) += rbd.o
block/nbd-client.c new file +386 -0
386 lines not on this page · raw message
block/nbd-client.h new file +52 -0
52 lines not on this page · raw message
block/nbd.c +30 -357
483 lines not on this page · raw message
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 5/12 nbd: pass export name as init argument
In reply to Marc-André LureauThere is no need to keep the export name around, and it seems a better fit as an argument in the init() call.
--- block/nbd-client.c | 8 ++++---- block/nbd-client.h | 5 ++--- block/nbd.c | 13 ++++++++----- 3 files changed, 14 insertions(+), 12 deletions(-)
block/nbd-client.c +4 -4
@@ -353,15 +353,15 @@ static void nbd_teardown_connection(NbdClientSession *client)void nbd_client_session_close (NbdClientSession *client) {nbd_teardown_connection (client); -g_free (client->export_name); } -int nbd_client_session_init (NbdClientSession *client, -BlockDriverState *bs,int sock) +int nbd_client_session_init (NbdClientSession *client,BlockDriverState *bs, +int sock,const char *export) {int ret; - ret =nbd_receive_negotiate (sock, client->export_name, +logout ("session init %s\n" , export); + ret =nbd_receive_negotiate (sock, export, &client->nbdflags, &client->size, &client->blocksize);if (ret <0 ) {
block/nbd-client.h +2 -3
@@ -31,14 +31,13 @@ typedef struct NbdClientSession {Coroutine *recv_coroutine[MAX_NBD_REQUESTS ];struct nbd_reply reply; -char *export_name;/* An NBD server may export several devices */ bool is_unix;BlockDriverState *bs; } NbdClientSession; -int nbd_client_session_init (NbdClientSession *client, -BlockDriverState *bs,int sock); +int nbd_client_session_init (NbdClientSession *client,BlockDriverState *bs, +int sock,const char *export_name);void nbd_client_session_close (NbdClientSession *client); int nbd_client_session_co_discard(NbdClientSession *client, int64_t sector_num,
block/nbd.c +8 -5
@@ -179,7 +179,7 @@ out:g_free (file); } -static int nbd_config (BDRVNBDState *s,QDict *options) +static int nbd_config (BDRVNBDState *s,QDict *options,char **export) {Error *local_err =NULL ;
@@ -209,8 +209,8 @@ static int nbd_config(BDRVNBDState *s, QDict *options)qemu_opt_set_number (s->socket_opts,"port" ,NBD_DEFAULT_PORT ); } - s->client.export_name =g_strdup (qdict_get_try_str (options,"export" )); -if (s->client.export_name) { + *export =g_strdup (qdict_get_try_str (options,"export" )); +if (*export) {qdict_del (options,"export" ); }
@@ -243,10 +243,11 @@ static int nbd_establish_connection(BlockDriverState *bs)static int nbd_open (BlockDriverState *bs,QDict *options,int flags) {BDRVNBDState *s = bs->opaque; +char *export =NULL ;int result, sock;/* Pop the config into our state object. Exit if invalid. */ - result =nbd_config (s, options); + result =nbd_config (s, options, &export);if (result !=0 ) {return result; }
@@ -260,7 +261,9 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags) }/* NBD handshake */ -return nbd_client_session_init (&s->client, bs, sock); + result =nbd_client_session_init (&s->client, bs, sock, export); +g_free (export); +return result; } static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 6/12 nbd: make session_close() idempotent
In reply to Marc-André Lureau--- block/nbd-client.c | 5 +++++ 1 file changed, 5 insertions(+)
block/nbd-client.c +5 -0
@@ -352,7 +352,12 @@ static void nbd_teardown_connection(NbdClientSession *client)void nbd_client_session_close (NbdClientSession *client) { +if (!client->bs) { +return ; + } +nbd_teardown_connection (client); + client->bs =NULL ; } int nbd_client_session_init(NbdClientSession *client, BlockDriverState *bs,
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 7/12 block: save the associated child in BlockDriverState
In reply to Marc-André LureauThis allows the Spice block driver to eject the associated device.
--- block.c | 46 +++++++++++++++++++++++++++++----------------- include/block/block_int.h | 1 + 2 files changed, 30 insertions(+), 17 deletions(-)
block.c +29 -17
@@ -294,7 +294,8 @@ void bdrv_register(BlockDriver *bdrv) }/* create a new block device (by default it is empty) */ -BlockDriverState *bdrv_new (const char *device_name) +static BlockDriverState *bdrv_new_int (const char *device_name, +BlockDriverState *child) {BlockDriverState *bs;
@@ -305,10 +306,16 @@ BlockDriverState *bdrv_new(const char *device_name) }bdrv_iostatus_disable (bs);notifier_list_init (&bs->close_notifiers); + bs->child = child;return bs; } +BlockDriverState *bdrv_new (const char *device_name) +{ +return bdrv_new_int (device_name,NULL ); +} +void bdrv_add_close_notifier (BlockDriverState *bs,Notifier *notify) {notifier_list_add (&bs->close_notifiers, notify);
@@ -769,16 +776,8 @@ free_and_fail:return ret; } -/* -* Opens a file using a protocol (file, host_device, nbd, ...) -* -* options is a QDict of options to pass to the block drivers, or NULL for an -* empty set of options. The reference to the QDict belongs to the block layer -* after the call (even on failure), so if the caller intends to reuse the -* dictionary, it needs to use QINCREF() before calling bdrv_file_open. -*/ -int bdrv_file_open (BlockDriverState **pbs,const char *filename, -QDict *options,int flags) +static int bdrv_file_open_int (BlockDriverState **pbs,const char *filename, +QDict *options,int flags,BlockDriverState *child) {BlockDriverState *bs;BlockDriver *drv;
@@ -790,7 +789,7 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename, options =qdict_new (); } - bs =bdrv_new ("" ); + bs =bdrv_new_int ("" , child); bs->options = options; options =qdict_clone_shallow (options);
@@ -873,6 +872,20 @@ fail: }/* +* Opens a file using a protocol (file, host_device, nbd, ...) +* +* options is a QDict of options to pass to the block drivers, or NULL for an +* empty set of options. The reference to the QDict belongs to the block layer +* after the call (even on failure), so if the caller intends to reuse the +* dictionary, it needs to use QINCREF() before calling bdrv_file_open. +*/ +int bdrv_file_open (BlockDriverState **pbs,const char *filename, +QDict *options,int flags) +{ +return bdrv_file_open_int (pbs, filename, options, flags,NULL ); +} + +/* * Opens the backing file for a BlockDriverState if not yet open * * options is a QDict of options to pass to the block drivers, or NULL for an
@@ -904,7 +917,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)return 0 ; } - bs->backing_hd =bdrv_new ("" ); + bs->backing_hd =bdrv_new_int ("" , bs);bdrv_get_full_backing_filename (bs, backing_filename,sizeof (backing_filename));
@@ -990,7 +1003,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,instead of opening 'filename' directly */ /* if there is a backing file, use it */ - bs1 =bdrv_new ("" ); + bs1 =bdrv_new_int ("" , bs); ret =bdrv_open (bs1, filename,NULL ,0 , drv);if (ret <0 ) {bdrv_delete (bs1);
@@ -1043,9 +1056,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, }extract_subqdict (options, &file_options,"file." ); - - ret =bdrv_file_open (&file, filename, file_options, -bdrv_open_flags (bs, flags)); + ret =bdrv_file_open_int (&file, filename, file_options, +bdrv_open_flags (bs, flags), bs);if (ret <0 ) { goto fail; }
include/block/block_int.h +1 -0
@@ -245,6 +245,7 @@ struct BlockDriverState {BlockDriverState *backing_hd;BlockDriverState *file; +BlockDriverState *child;NotifierList close_notifiers;
Signature · 1 line
1.8.3.rc1.49.g8d97506
Re: [Qemu-devel] [PATCH 07/12] block: save the associated child in BlockDriverState
Il 20/06/2013 19:46, Marc-André Lureau ha scritto:
Quoted 1 line
This allows the Spice block driver to eject the associated device.
The child can change when you have for example a streaming operation. What exactly are you trying to do here (I guess I'll understand more when I get to the later patches)?
Can you draw the relationships between all the BlockDriverStates in a
spicebd: drive?
Paolo
Quoted 129 lines
Signed-off-by Marc-André Lureau <[email protected]>--- block.c | 46 +++++++++++++++++++++++++++++----------------- include/block/block_int.h | 1 + 2 files changed, 30 insertions(+), 17 deletions(-)block.c +29
-17@@ -294,7 +294,8 @@ void bdrv_register(BlockDriver *bdrv) }/* create a new block device (by default it is empty) */ -BlockDriverState *bdrv_new (const char *device_name) +static BlockDriverState *bdrv_new_int (const char *device_name, +BlockDriverState *child) {BlockDriverState *bs;@@ -305,10 +306,16 @@ BlockDriverState *bdrv_new(const char *device_name) }bdrv_iostatus_disable (bs);notifier_list_init (&bs->close_notifiers); + bs->child = child;return bs; } +BlockDriverState *bdrv_new (const char *device_name) +{ +return bdrv_new_int (device_name,NULL ); +} +void bdrv_add_close_notifier (BlockDriverState *bs,Notifier *notify) {notifier_list_add (&bs->close_notifiers, notify);@@ -769,16 +776,8 @@ free_and_fail:return ret; } -/* -* Opens a file using a protocol (file, host_device, nbd, ...) -* -* options is a QDict of options to pass to the block drivers, or NULL for an -* empty set of options. The reference to the QDict belongs to the block layer -* after the call (even on failure), so if the caller intends to reuse the -* dictionary, it needs to use QINCREF() before calling bdrv_file_open. -*/ -int bdrv_file_open (BlockDriverState **pbs,const char *filename, -QDict *options,int flags) +static int bdrv_file_open_int (BlockDriverState **pbs,const char *filename, +QDict *options,int flags,BlockDriverState *child) {BlockDriverState *bs;BlockDriver *drv;@@ -790,7 +789,7 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename, options =qdict_new (); } - bs =bdrv_new ("" ); + bs =bdrv_new_int ("" , child); bs->options = options; options =qdict_clone_shallow (options);@@ -873,6 +872,20 @@ fail: }/* +* Opens a file using a protocol (file, host_device, nbd, ...) +* +* options is a QDict of options to pass to the block drivers, or NULL for an +* empty set of options. The reference to the QDict belongs to the block layer +* after the call (even on failure), so if the caller intends to reuse the +* dictionary, it needs to use QINCREF() before calling bdrv_file_open. +*/ +int bdrv_file_open (BlockDriverState **pbs,const char *filename, +QDict *options,int flags) +{ +return bdrv_file_open_int (pbs, filename, options, flags,NULL ); +} + +/* * Opens the backing file for a BlockDriverState if not yet open * * options is a QDict of options to pass to the block drivers, or NULL for an @@ -904,7 +917,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)return 0 ; } - bs->backing_hd =bdrv_new ("" ); + bs->backing_hd =bdrv_new_int ("" , bs);bdrv_get_full_backing_filename (bs, backing_filename,sizeof (backing_filename));@@ -990,7 +1003,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,instead of opening 'filename' directly */ /* if there is a backing file, use it */ - bs1 =bdrv_new ("" ); + bs1 =bdrv_new_int ("" , bs); ret =bdrv_open (bs1, filename,NULL ,0 , drv);if (ret <0 ) {bdrv_delete (bs1);@@ -1043,9 +1056,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, }extract_subqdict (options, &file_options,"file." ); - - ret =bdrv_file_open (&file, filename, file_options, -bdrv_open_flags (bs, flags)); + ret =bdrv_file_open_int (&file, filename, file_options, +bdrv_open_flags (bs, flags), bs);if (ret <0 ) { goto fail; }include/block/block_int.h +1
-0@@ -245,6 +245,7 @@ struct BlockDriverState {BlockDriverState *backing_hd;BlockDriverState *file; +BlockDriverState *child;NotifierList close_notifiers;
Re: [Qemu-devel] [PATCH 07/12] block: save the associated child in BlockDriverState
Hi
Quoted 5 lines · Paolo Bonzini
Il 20/06/2013 19:46, Marc-André Lureau ha scritto:
Quoted 1 line
This allows the Spice block driver to eject the associated device.
The child can change when you have for example a streaming operation.
Ah, can you point me to some function or some way I can reproduce? I don't know what you mean by a streaming operation here.
What exactly are you trying to do here (I guess I'll understand more
Quoted 2 lines
when I get to the later patches)?
Getting to the bottom BlockDriverState to be able to eject & change it. (it could also be named the "parent", but other parts of the code suggest the "child" name)
Quoted 4 lines
Can you draw the relationships between all the BlockDriverStates in a
spicebd: drive?
Hopefully, but I have only tested with raw images (w/wo snapshot).
Quoted 146 lines
Paolo
Quoted 3 lines
Signed-off-by Marc-André Lureau <[email protected]>--- block.c | 46+++++++++++++++++++++++++++++-----------------
Quoted 35 lines
include/block/block_int.h | 1 + 2 files changed, 30 insertions(+), 17 deletions(-)block.c +8
-1@@ -294,7 +294,8 @@ void bdrv_register(BlockDriver *bdrv) }/* create a new block device (by default it is empty) */ -BlockDriverState *bdrv_new (const char *device_name) +static BlockDriverState *bdrv_new_int (const char *device_name, +BlockDriverState *child) {BlockDriverState *bs;@@ -305,10 +306,16 @@ BlockDriverState *bdrv_new(const char *device_name) }bdrv_iostatus_disable (bs);notifier_list_init (&bs->close_notifiers); + bs->child = child;return bs; } +BlockDriverState *bdrv_new (const char *device_name) +{ +return bdrv_new_int (device_name,NULL ); +} +void bdrv_add_close_notifier (BlockDriverState *bs,Notifier *notify) {notifier_list_add (&bs->close_notifiers, notify);block.c free_and_fail: · line 775
Quoted 7 lines
block.c +0
-4@@ -769,16 +776,8 @@ free_and_fail:return ret; } -/* -* Opens a file using a protocol (file, host_device, nbd, ...) -* -* options is a QDict of options to pass to the block drivers, or NULL for an
block.c free_and_fail: · line 776
Quoted 1 line
block.c +0
-1-* empty set of options. The reference to the QDict belongs to the block layer
block.c free_and_fail: · line 777
Quoted 1 line
block.c +0
-1-* after the call (even on failure), so if the caller intends to reuse the
block.c free_and_fail:
Quoted 5 lines
block.c +1
-4-* dictionary, it needs to use QINCREF() before calling bdrv_file_open. -*/ -int bdrv_file_open (BlockDriverState **pbs,const char *filename, -QDict *options,int flags) +static int bdrv_file_open_int(BlockDriverState **pbs, const char*filename,
block.c int bdrv_file_open(BlockDriverState **pbs, const
Quoted 5 lines
block.c +1
-0+ QDict *options, int flags, BlockDriverState *child) { BlockDriverState *bs;BlockDriver *drv;@@ -790,7 +789,7 @@ int bdrv_file_open(BlockDriverState **pbs, constchar *filename,
Quoted 9 lines
block.c +1
-1options =qdict_new (); } - bs =bdrv_new ("" ); + bs =bdrv_new_int ("" , child); bs->options = options; options =qdict_clone_shallow (options);block.c fail: · line 877
Quoted 6 lines
block.c +3
-0@@ -873,6 +872,20 @@ fail: }/* +* Opens a file using a protocol (file, host_device, nbd, ...) +* +* options is a QDict of options to pass to the block drivers, or NULL for an
block.c fail: · line 878
Quoted 1 line
block.c +1
-0+* empty set of options. The reference to the QDict belongs to the block layer
block.c fail: · line 879
Quoted 1 line
block.c +1
-0+* after the call (even on failure), so if the caller intends to reuse the
block.c fail:
Quoted 12 lines
block.c +9
-0+* dictionary, it needs to use QINCREF() before calling bdrv_file_open. +*/ +int bdrv_file_open (BlockDriverState **pbs,const char *filename, +QDict *options,int flags) +{ +return bdrv_file_open_int (pbs, filename, options, flags,NULL ); +} + +/* * Opens the backing file for a BlockDriverState if not yet open * * options is a QDict of options to pass to the block drivers, or NULL for an
block.c int bdrv_open_backing_file(BlockDriverState *bs,
Quoted 1 line
block.c +0
-0@@ -904,7 +917,7 @@ int bdrv_open_backing_file(BlockDriverState *bs,QDict *options)
block.c int bdrv_open(BlockDriverState *bs, const char
Quoted 9 lines
block.c +1
-1return 0 ; } - bs->backing_hd =bdrv_new ("" ); + bs->backing_hd =bdrv_new_int ("" , bs);bdrv_get_full_backing_filename (bs, backing_filename,sizeof (backing_filename));@@ -990,7 +1003,7 @@ int bdrv_open(BlockDriverState *bs, const char*filename, QDict *options,
block.c int bdrv_open(BlockDriverState *bs, const char
Quoted 9 lines
block.c +1
-1instead of opening 'filename' directly */ /* if there is a backing file, use it */ - bs1 =bdrv_new ("" ); + bs1 =bdrv_new_int ("" , bs); ret =bdrv_open (bs1, filename,NULL ,0 , drv);if (ret <0 ) {bdrv_delete (bs1);@@ -1043,9 +1056,8 @@ int bdrv_open(BlockDriverState *bs, const char*filename, QDict *options,
Quoted 24 lines
block.c +2
-3}extract_subqdict (options, &file_options,"file." ); - - ret =bdrv_file_open (&file, filename, file_options, -bdrv_open_flags (bs, flags)); + ret =bdrv_file_open_int (&file, filename, file_options, +bdrv_open_flags (bs, flags), bs);if (ret <0 ) { goto fail; }include/block/block_int.h +1
-0@@ -245,6 +245,7 @@ struct BlockDriverState {BlockDriverState *backing_hd;BlockDriverState *file; +BlockDriverState *child;NotifierList close_notifiers;
Signature · 1 line
Marc-André Lureau
Re: [Qemu-devel] [PATCH 07/12] block: save the associated child in BlockDriverState
Il 21/06/2013 10:36, Marc-André Lureau ha scritto:
Quoted 6 lines
Hi
On Fri, Jun 21, 2013 at 12:25 AM, Paolo Bonzini <[email protected] <mailto:[email protected]>> wrote:
Quoted 7 lines
Il 20/06/2013 19:46, Marc-André Lureau ha scritto:
> This allows the Spice block driver to eject the associated device.The child can change when you have for example a streaming operation.
Ah, can you point me to some function or some way I can reproduce? I don't know what you mean by a streaming operation here.
Let's just discuss the design so that it just works and you don't have to worry about it. :)
Quoted 6 lines
What exactly are you trying to do here (I guess I'll understand more when I get to the later patches)?
Getting to the bottom BlockDriverState to be able to eject & change it. (it could also be named the "parent", but other parts of the code suggest the "child" name)
There is already an interface for eject/change, which is the monitor. To signal an eject or medium change, the SPICE client should simply ask libvirt to do so. It is fine to change "from" spicebd: "to" spicebd:, the guest would still perceive it as a change.
I'm not sure how libvirt communicates the change back to the SPICE client, and whether it is asynchronous or synchronous.
BTW, note that IDE or virtio-blk do not support removable media, and are not able to pass eject or media change notifications. SCSI devices (such as usb-bot, usb-uas and virtio-scsi) can.
Quoted 5 lines
Can you draw the relationships between all the BlockDriverStates in a
spicebd: drive?Hopefully, but I have only tested with raw images (w/wo snapshot).
Then draw it. :) But from the above description it looks like it is not necessary, it should simply be "raw" on top of "spicebd". Parsing formats should be done on the client side, perhaps by invoking qemu-nbd and tunnelling the NBD data on the SPICE channel.
Paolo
Re: [Qemu-devel] [PATCH 07/12] block: save the associated child in BlockDriverState
Hi
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines
What exactly are you trying to do here (I guess I'll understand more when I get to the later patches)?
Getting to the bottom BlockDriverState to be able to eject & change it. (it could also be named the "parent", but other parts of the code suggest the "child" name)
Quoted 9 lines
There is already an interface for eject/change, which is the monitor. To signal an eject or medium change, the SPICE client should simply ask libvirt to do so. It is fine to change "from" spicebd: "to" spicebd:, the guest would still perceive it as a change.
I'm not sure how libvirt communicates the change back to the SPICE client, and whether it is asynchronous or synchronous.
Spice is not using libvirt, and doesn't have access to monitor. I looked at the monitor code. Basically, the spice block driver I proposed uses a similar code to forcefully eject and change.
Perhaps it's possible for the Spice qemu-side code to have access to the monitor, but the end result would be similar anyway.
Quoted 8 lines
BTW, note that IDE or virtio-blk do not support removable media, and are not able to pass eject or media change notifications. SCSI devices (such as usb-bot, usb-uas and virtio-scsi) can.
Quoted 4 lines
Can you draw the relationships between all the BlockDriverStates in a
spicebd: drive?
Quoted 8 lines
Quoted 1 line
Hopefully, but I have only tested with raw images (w/wo snapshot).
Then draw it. :) But from the above description it looks like it is not necessary, it should simply be "raw" on top of "spicebd". Parsing formats should be done on the client side, perhaps by invoking qemu-nbd and tunnelling the NBD data on the SPICE channel.
Right, "raw" on top of "spicebd" for iso/raw images (and "qcow2" on top for snapshot support - even in readonly).
I wonder what would happen if spicebd image would be something else than raw/iso, I suppose there would be more BlockDriverStates to handle the various format.
Signature · 1 line
Marc-André Lureau
Re: [Qemu-devel] [PATCH 07/12] block: save the associated child in BlockDriverState
Il 21/06/2013 15:30, Marc-André Lureau ha scritto:
Quoted 9 lines
Quoted 3 lines
Getting to the bottom BlockDriverState to be able to eject & change it. (it could also be named the "parent", but other parts of the code suggest the "child" name)
There is already an interface for eject/change, which is the monitor. To signal an eject or medium change, the SPICE client should simply ask libvirt to do so. It is fine to change "from" spicebd: "to" spicebd:, the guest would still perceive it as a change.
Quoted 6 lines
I'm not sure how libvirt communicates the change back to the SPICE client, and whether it is asynchronous or synchronous.
Spice is not using libvirt, and doesn't have access to monitor. I looked at the monitor code. Basically, the spice block driver I proposed uses a similar code to forcefully eject and change.
Similar, but it also violates encapsulation by adding bs->child. :)
Quoted 2 lines
Perhaps it's possible for the Spice qemu-side code to have access to the monitor, but the end result would be similar anyway.
That would work better. Spice qemu-side would keep an association between Spice channels and block device names, and use that to convert Spice commands to monitor commands (the API is qmp_change_blockdev/qmp_eject).
Paolo
Quoted 27 lines
BTW, note that IDE or virtio-blk do not support removable media, and are not able to pass eject or media change notifications. SCSI devices (such as usb-bot, usb-uas and virtio-scsi) can.
> Can you draw the relationships between all the
BlockDriverStates in a
> spicebd: drive?
>
>
> Hopefully, but I have only tested with raw images (w/wo snapshot).Then draw it. :) But from the above description it looks like it is not necessary, it should simply be "raw" on top of "spicebd". Parsing formats should be done on the client side, perhaps by invoking qemu-nbd and tunnelling the NBD data on the SPICE channel.
Right, "raw" on top of "spicebd" for iso/raw images (and "qcow2" on top for snapshot support - even in readonly).
I wonder what would happen if spicebd image would be something else than raw/iso, I suppose there would be more BlockDriverStates to handle the various format.
Signature · 1 line
Marc-André Lureau
PATCH 8/12 block: extract make_snapshot() from bdrv_open()
In reply to Marc-André Lureau--- block.c | 107 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 45 deletions(-)
block.c +62 -45
@@ -959,6 +959,65 @@ static void extract_subqdict(QDict *src, QDict **dst, const char *start) } } +static int make_snapshot (BlockDriverState *bs,int64_t total_size, +const char **pfilename,BlockDriver **pdrv) +{ +const char *filename = *pfilename; +BlockDriver *drv = *pdrv; +int ret; +BlockDriver *bdrv_qcow2; +QEMUOptionParameter *create_options; +char backing_filename[PATH_MAX ]; +/* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ +char tmp_filename[PATH_MAX +1 ]; + +assert (filename !=NULL ); + total_size &=BDRV_SECTOR_MASK ; + +/* if snapshot, we create a temporary backing file and open it +instead of opening 'filename' directly */ + + ret =get_tmp_filename (tmp_filename,sizeof (tmp_filename)); +if (ret <0 ) { + goto fail; + } + +/* Real path is meaningless for protocols */ +if (path_has_protocol (filename)) { +snprintf (backing_filename,sizeof (backing_filename), +"%s" , filename); + }else if (!realpath (filename, backing_filename)) { + ret = -errno; + goto fail; + } + + bdrv_qcow2 =bdrv_find_format ("qcow2" ); + create_options =parse_option_parameters ("" , bdrv_qcow2->create_options, +NULL ); + +set_option_parameter_int (create_options,BLOCK_OPT_SIZE , total_size); +set_option_parameter (create_options,BLOCK_OPT_BACKING_FILE , + backing_filename); +if (drv) { +set_option_parameter (create_options,BLOCK_OPT_BACKING_FMT , + drv->format_name); + } + + ret =bdrv_create (bdrv_qcow2, tmp_filename, create_options); +free_option_parameters (create_options); +if (ret <0 ) { + goto fail; + } + + *pfilename = tmp_filename; + *pdrv = bdrv_qcow2; + bs->is_temporary =1 ; +return 0 ; + +fail: +return ret; +} +/* * Opens a disk image (raw, qcow2, vmdk, ...) *
@@ -971,8 +1030,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, int flags, BlockDriver *drv) { int ret; -/* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ -char tmp_filename[PATH_MAX +1 ];BlockDriverState *file =NULL ;QDict *file_options =NULL ;
@@ -988,66 +1045,26 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,if (flags &BDRV_O_SNAPSHOT ) {BlockDriverState *bs1;int64_t total_size; -BlockDriver *bdrv_qcow2; -QEMUOptionParameter *create_options; -char backing_filename[PATH_MAX ];if (qdict_size (options) !=0 ) {error_report ("Can't use snapshot=on with driver-specific options" ); ret = -EINVAL ; goto fail; } -assert (filename !=NULL ); -/* if snapshot, we create a temporary backing file and open it -instead of opening 'filename' directly */ - -/* if there is a backing file, use it */ - bs1 =bdrv_new_int ("" , bs); + bs1 =bdrv_new_int ("" ,NULL ); ret =bdrv_open (bs1, filename,NULL ,0 , drv);if (ret <0 ) {bdrv_delete (bs1); goto fail; } - total_size =bdrv_getlength (bs1) &BDRV_SECTOR_MASK ; - + total_size =bdrv_getlength (bs1);bdrv_delete (bs1); - ret =get_tmp_filename (tmp_filename,sizeof (tmp_filename)); + ret =make_snapshot (bs, total_size, &filename, &drv);if (ret <0 ) { goto fail; } - -/* Real path is meaningless for protocols */ -if (path_has_protocol (filename)) { -snprintf (backing_filename,sizeof (backing_filename), -"%s" , filename); - }else if (!realpath (filename, backing_filename)) { - ret = -errno; - goto fail; - } - - bdrv_qcow2 =bdrv_find_format ("qcow2" ); - create_options =parse_option_parameters ("" , bdrv_qcow2->create_options, -NULL ); - -set_option_parameter_int (create_options,BLOCK_OPT_SIZE , total_size); -set_option_parameter (create_options,BLOCK_OPT_BACKING_FILE , - backing_filename); -if (drv) { -set_option_parameter (create_options,BLOCK_OPT_BACKING_FMT , - drv->format_name); - } - - ret =bdrv_create (bdrv_qcow2, tmp_filename, create_options); -free_option_parameters (create_options); -if (ret <0 ) { - goto fail; - } - - filename = tmp_filename; - drv = bdrv_qcow2; - bs->is_temporary =1 ; }/* Open image file without format layer */
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 9/12 block: add "snapshot.size" option to avoid extra bdrv_open()
In reply to Marc-André Lureau--- block.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
block.c +11 -6
@@ -1046,20 +1046,25 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,BlockDriverState *bs1;int64_t total_size; + total_size =qdict_get_try_int (options,"snapshot.size" ,-1 ); +qdict_del (options,"snapshot.size" ); +if (qdict_size (options) !=0 ) {error_report ("Can't use snapshot=on with driver-specific options" ); ret = -EINVAL ; goto fail; } - bs1 =bdrv_new_int ("" ,NULL ); - ret =bdrv_open (bs1, filename,NULL ,0 , drv); -if (ret <0 ) { +if (total_size ==-1 ) { + bs1 =bdrv_new_int ("" ,NULL ); + ret =bdrv_open (bs1, filename,NULL ,0 , drv); +if (ret <0 ) { +bdrv_delete (bs1); + goto fail; + } + total_size =bdrv_getlength (bs1);bdrv_delete (bs1); - goto fail; } - total_size =bdrv_getlength (bs1); -bdrv_delete (bs1); ret =make_snapshot (bs, total_size, &filename, &drv);if (ret <0 ) {
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 10/12 block: learn to open a driver with a given opaque
In reply to Marc-André LureauIf the block driver is given an opaque data, there is no need to allocate a new one. This allows to pass an existing driver state to the new driver.
--- block.c | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-)
block.c +28 -19
@@ -295,7 +295,7 @@ void bdrv_register(BlockDriver *bdrv)/* create a new block device (by default it is empty) */ static BlockDriverState *bdrv_new_int (const char *device_name, -BlockDriverState *child) +BlockDriverState *child,void *opaque) {BlockDriverState *bs;
@@ -307,13 +307,14 @@ static BlockDriverState *bdrv_new_int(const char *device_name,bdrv_iostatus_disable (bs);notifier_list_init (&bs->close_notifiers); bs->child = child; + bs->opaque = opaque;return bs; }BlockDriverState *bdrv_new (const char *device_name) { -return bdrv_new_int (device_name,NULL ); +return bdrv_new_int (device_name,NULL ,NULL ); }void bdrv_add_close_notifier (BlockDriverState *bs,Notifier *notify)
@@ -729,7 +730,9 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, } bs->drv = drv; - bs->opaque =g_malloc0 (drv->instance_size); +if (bs->opaque ==NULL ) { + bs->opaque =g_malloc0 (drv->instance_size); + } bs->enable_write_cache = !!(flags &BDRV_O_CACHE_WB );
@@ -777,7 +780,7 @@ free_and_fail: }static int bdrv_file_open_int (BlockDriverState **pbs,const char *filename, -QDict *options,int flags,BlockDriverState *child) +QDict *options,int flags,BlockDriverState *child,void *opaque) {BlockDriverState *bs;BlockDriver *drv;
@@ -789,7 +792,7 @@ static int bdrv_file_open_int(BlockDriverState **pbs, const char *filename, options =qdict_new (); } - bs =bdrv_new_int ("" , child); + bs =bdrv_new_int ("" , child, opaque); bs->options = options; options =qdict_clone_shallow (options);
@@ -882,18 +885,11 @@ fail:int bdrv_file_open (BlockDriverState **pbs,const char *filename,QDict *options,int flags) { -return bdrv_file_open_int (pbs, filename, options, flags,NULL ); +return bdrv_file_open_int (pbs, filename, options, flags,NULL ,NULL ); } -/* -* Opens the backing file for a BlockDriverState if not yet open -* -* options is a QDict of options to pass to the block drivers, or NULL for an -* empty set of options. The reference to the QDict is transferred to this -* function (even on failure), so if the caller intends to reuse the dictionary, -* it needs to use QINCREF() before calling bdrv_file_open. -*/ -int bdrv_open_backing_file (BlockDriverState *bs,QDict *options) +static int bdrv_open_backing_file_int (BlockDriverState *bs, +QDict *options,void *opaque) {char backing_filename[PATH_MAX ];int back_flags, ret;
@@ -917,7 +913,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)return 0 ; } - bs->backing_hd =bdrv_new_int ("" , bs); + bs->backing_hd =bdrv_new_int ("" , bs, opaque);bdrv_get_full_backing_filename (bs, backing_filename,sizeof (backing_filename));
@@ -940,6 +936,19 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)return 0 ; } +/* +* Opens the backing file for a BlockDriverState if not yet open +* +* options is a QDict of options to pass to the block drivers, or NULL for an +* empty set of options. The reference to the QDict is transferred to this +* function (even on failure), so if the caller intends to reuse the dictionary, +* it needs to use QINCREF() before calling bdrv_file_open. +*/ +int bdrv_open_backing_file (BlockDriverState *bs,QDict *options) +{ +return bdrv_open_backing_file_int (bs, options,NULL ); +} +static void extract_subqdict (QDict *src,QDict **dst,const char *start) {const QDictEntry *entry, *next;
@@ -1056,7 +1065,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, }if (total_size ==-1 ) { - bs1 =bdrv_new_int ("" ,NULL ); + bs1 =bdrv_new_int ("" ,NULL ,NULL ); ret =bdrv_open (bs1, filename,NULL ,0 , drv);if (ret <0 ) {bdrv_delete (bs1);
@@ -1079,7 +1088,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,extract_subqdict (options, &file_options,"file." ); ret =bdrv_file_open_int (&file, filename, file_options, -bdrv_open_flags (bs, flags), bs); +bdrv_open_flags (bs, flags), bs,NULL );if (ret <0 ) { goto fail; }
@@ -1109,7 +1118,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,QDict *backing_options;extract_subqdict (options, &backing_options,"backing." ); - ret =bdrv_open_backing_file (bs, backing_options); + ret =bdrv_open_backing_file_int (bs, backing_options,NULL );if (ret <0 ) { goto close_and_fail; }
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 11/12 block: allow to call bdrv_open() with an opaque
In reply to Marc-André LureauIf the block driver already has a bs->opaque when calling bdrv_open(), pass it down to the file driver.
--- block.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
block.c +6 -2
@@ -1041,6 +1041,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,int ret;BlockDriverState *file =NULL ;QDict *file_options =NULL ; +void *backing_opaque =NULL ;/* NULL means an empty set of options */ if (options ==NULL ) {
@@ -1064,6 +1065,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, goto fail; } + backing_opaque = bs->opaque; + bs->opaque =NULL ;if (total_size ==-1 ) { bs1 =bdrv_new_int ("" ,NULL ,NULL ); ret =bdrv_open (bs1, filename,NULL ,0 , drv);
@@ -1088,7 +1091,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,extract_subqdict (options, &file_options,"file." ); ret =bdrv_file_open_int (&file, filename, file_options, -bdrv_open_flags (bs, flags), bs,NULL ); +bdrv_open_flags (bs, flags), bs, bs->opaque); + bs->opaque =NULL ;if (ret <0 ) { goto fail; }
@@ -1118,7 +1122,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,QDict *backing_options;extract_subqdict (options, &backing_options,"backing." ); - ret =bdrv_open_backing_file_int (bs, backing_options,NULL ); + ret =bdrv_open_backing_file_int (bs, backing_options, backing_opaque);if (ret <0 ) { goto close_and_fail; }
Signature · 1 line
1.8.3.rc1.49.g8d97506
PATCH 12/12 block: add spice block device backend
In reply to Marc-André Lureau--- block/Makefile.objs | 1 + block/spice.c | 523 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 524 insertions(+) create mode 100644 block/spice.c
block/Makefile.objs +1 -0
@@ -16,6 +16,7 @@ block-obj-$(CONFIG_CURL) += curl.o block-obj-$(CONFIG_RBD) += rbd.o block-obj-$(CONFIG_GLUSTERFS) += gluster.o block-obj-$(CONFIG_LIBSSH2) += ssh.o +common-obj-$(CONFIG_SPICE) += spice.o endif common-obj-y += stream.o
block/spice.c new file +523 -0
523 lines not on this page · raw message
Signature · 1 line
1.8.3.rc1.49.g8d97506