RFCv2: add Spice block device
cover RFCv2: 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.
The backend only support read-only device atm (although it shouldn't be hard to add write support if necessary)
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-November/015452.html
http://lists.freedesktop.org/archives/spice-devel/2013-November/015431.html
Caveats: 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. We also save the associate device name in the block drivers.
During migration, the source needs to be able to flush pending operations, so the Spice channel context must be in a running loop. A modification to the Spice server API allows to associate a particular channel with the AIO loop, and may be used in the future to associate channels with different context or athreads. However, the AIO context doesn't have timers yet. Since they aren't really needed for the NBD channel, it's not a problem. I have been told timers in AIO are on their way, so this could be updated later.
Since the block driver state is not migrated, the destination needs to wait until the block driver is initialized before the VM can run. This is done with a simple hold count. It is also necessary to avoid extra media changed notifications, which is easily done by checking migration state.
Marc-André Lureau (21):
vscclient: do not add a socket watch if there is not data to send
spice-char: remove unused field
qmp_change_blockdev() remove unused has_format
include: add missing config-host.h include
char: add qemu_chr_fe_event()
Split nbd block client code
nbd: don't change socket block during negotiate
nbd: pass export name as init argument
nbd: make session_close() idempotent
nbd: finish any pending coroutine
nbd: avoid uninitialized warnings
block: save the associated child name in BlockDriverState
blockdev: add qmp_change_blockdev_int()
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: do not notify change during migration
sysemu: add vm_start_hold/release
spice-core: allow an interface to be in AIO context
block: add spice block device backend
block.c | 225 ++++++++++++------- block/Makefile.objs | 3 +- block/nbd-client.c | 384 +++++++++++++++++++++++++++++++++ block/nbd-client.h | 50 +++++ block/nbd.c | 380 +++----------------------------- block/spicebd.c | 536 ++++++++++++++++++++++++++++++++++++++++++++++ blockdev.c | 24 ++- hw/block/fdc.c | 8 +- hw/ide/core.c | 12 +- hw/scsi/scsi-disk.c | 11 +- hw/sd/sd.c | 6 +- include/block/block.h | 2 +- include/block/block_int.h | 1 + include/sysemu/blockdev.h | 5 +- include/sysemu/char.h | 10 + include/sysemu/sysemu.h | 2 + include/ui/qemu-spice.h | 4 +- libcacard/vscclient.c | 10 +- nbd.c | 1 - qemu-char.c | 9 +- qmp.c | 2 +- spice-qemu-char.c | 20 +- stubs/vm-stop.c | 5 + ui/spice-core.c | 62 +++++- vl.c | 17 ++ 25 files changed, 1320 insertions(+), 469 deletions(-) create mode 100644 block/nbd-client.c create mode 100644 block/nbd-client.h create mode 100644 block/spicebd.c
Signature · 1 line
1.8.3.1
PATCH 1/21 vscclient: do not add a socket watch if there is not data to send
From: Marc-André Lureau <[email protected]>
Fixes the following error:
** (process:780): CRITICAL **: do_socket_send: assertion `socket_to_send->len != 0' failed
--- libcacard/vscclient.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
libcacard/vscclient.c +6 -4
@@ -58,7 +58,7 @@ static QemuMutex socket_to_send_lock;static guint socket_tag;static void -update_socket_watch (gboolean out); +update_socket_watch (void ); static gboolean do_socket_send(GIOChannel *source,
@@ -80,7 +80,7 @@ do_socket_send(GIOChannel *source,g_byte_array_remove_range (socket_to_send,0 , bw);if (socket_to_send->len ==0 ) { -update_socket_watch (FALSE); +update_socket_watch ();return FALSE; }return TRUE;
@@ -89,7 +89,7 @@ do_socket_send(GIOChannel *source,static gboolean socket_prepare_sending (gpointer user_data) { -update_socket_watch (TRUE); +update_socket_watch ();return FALSE; }
@@ -440,8 +440,10 @@ do_socket(GIOChannel *source, }static void -update_socket_watch (gboolean out) +update_socket_watch (void ) { +gboolean out = socket_to_send->len >0 ; +if (socket_tag !=0 ) {g_source_remove (socket_tag); }
Signature · 1 line
1.8.3.1
PATCH 2/21 spice-char: remove unused field
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- spice-qemu-char.c | 1 - 1 file changed, 1 deletion(-)
spice-qemu-char.c +0 -1
@@ -11,7 +11,6 @@ typedef struct SpiceCharDriver { CharDriverState* chr; SpiceCharDeviceInstance sin; - char *subtype; bool active; bool blocked; const uint8_t *datapos;
Signature · 1 line
1.8.3.1
PATCH 3/21 qmp_change_blockdev() remove unused has_format
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- blockdev.c | 2 +- include/sysemu/blockdev.h | 2 +- qmp.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
blockdev.c +1 -1
@@ -1524,7 +1524,7 @@ static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, }void qmp_change_blockdev (const char *device,const char *filename, -bool has_format,const char *format,Error **errp) +const char *format,Error **errp) {BlockDriverState *bs;BlockDriver *drv =NULL ;
include/sysemu/blockdev.h +1 -1
@@ -64,7 +64,7 @@ DriveInfo *drive_init(QemuOpts *arg, BlockInterfaceType block_default_type);DriveInfo *add_init_drive (const char *opts);void qmp_change_blockdev (const char *device,const char *filename, -bool has_format,const char *format,Error **errp); +const char *format,Error **errp);void do_commit (Monitor *mon,const QDict *qdict);int do_drive_del (Monitor *mon,const QDict *qdict,QObject **ret_data);#endif
qmp.c +1 -1
@@ -400,7 +400,7 @@ void qmp_change(const char *device, const char *target,if (strcmp (device,"vnc" ) ==0 ) {qmp_change_vnc (target, has_arg, arg, err); }else { -qmp_change_blockdev (device, target, has_arg, arg, err); +qmp_change_blockdev (device, target, arg, err); } }
Signature · 1 line
1.8.3.1
PATCH 4/21 include: add missing config-host.h include
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- 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.1
PATCH 5/21 char: add qemu_chr_fe_event()
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- 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;
@@ -138,6 +139,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
@@ -3353,6 +3353,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
@@ -222,6 +222,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;
@@ -255,6 +264,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.1
Re: [Qemu-devel] [PATCH 05/21] char: add qemu_chr_fe_event()
Quoted 1 line · Marc-André Lureau
From: Marc-André Lureau <[email protected]>
The patch description is incomplete, or the patch should be split - this patch also implements qemu_chr_fe_event for spiceport.
Quoted 83 lines
Signed-off-by Marc-André Lureau <[email protected]>--- 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;@@ -138,6 +139,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@@ -3353,6 +3353,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@@ -222,6 +222,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;@@ -255,6 +264,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);
PATCH 6/21 Split nbd block client code
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- block/Makefile.objs | 2 +- block/nbd-client.c | 372 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/nbd-client.h | 51 +++++++ block/nbd.c | 373 ++++------------------------------------------------ 4 files changed, 452 insertions(+), 346 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 +372 -0
372 lines not on this page · raw message
block/nbd-client.h new file +51 -0
51 lines not on this page · raw message
block/nbd.c +28 -345
466 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 7/21 nbd: don't change socket block during negotiate
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
The caller might handle non-blocking using coroutine. Leave the choice to the caller to use a blocking or non-blocking negotiate.
--- nbd.c | 1 - 1 file changed, 1 deletion(-)
nbd.c +0 -1
@@ -443,7 +443,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.1
PATCH 8/21 nbd: pass export name as init argument
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
There 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 | 10 ++++------ block/nbd-client.h | 5 ++--- block/nbd.c | 13 ++++++++----- 3 files changed, 14 insertions(+), 14 deletions(-)
block/nbd-client.c +4 -6
@@ -338,17 +338,15 @@ static void nbd_teardown_connection(NbdClientSession *client)void nbd_client_session_close (NbdClientSession *client) {nbd_teardown_connection (client); -g_free (client->export_name); - client->export_name =NULL ; } -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; -/* NBD handshake */ - 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
@@ -30,14 +30,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
@@ -188,7 +188,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 ;
@@ -218,8 +218,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" ); }
@@ -253,10 +253,11 @@ static int nbd_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) {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; }
@@ -270,7 +271,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.1
PATCH 9/21 nbd: make session_close() idempotent
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- block/nbd-client.c | 5 +++++ 1 file changed, 5 insertions(+)
block/nbd-client.c +5 -0
@@ -337,7 +337,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.1
PATCH 10/21 nbd: finish any pending coroutine
In reply to Marc-André LureauMake sure all pending coroutines are finished when closing the session.
--- block/nbd-client.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
block/nbd-client.c +17 -5
@@ -32,6 +32,18 @@#define HANDLE_TO_INDEX (bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))#define INDEX_TO_HANDLE (bs, index) ((index) ^ ((uint64_t)(intptr_t)bs)) +static void nbd_recv_coroutines_enter_all (NbdClientSession *s) +{ +int i; + +for (i =0 ; i <MAX_NBD_REQUESTS ; i++) { +if (s->recv_coroutine[i]) { +fprintf (stderr,"*nbd reply enter: %p %d\n" , s, s->reply.error); +qemu_coroutine_enter (s->recv_coroutine[i],NULL ); + } + } +} +static void nbd_reply_ready (void *opaque) {NbdClientSession *s = opaque;
@@ -67,11 +79,7 @@ static void nbd_reply_ready(void *opaque) } fail: -for (i =0 ; i <MAX_NBD_REQUESTS ; i++) { -if (s->recv_coroutine[i]) { -qemu_coroutine_enter (s->recv_coroutine[i],NULL ); - } - } +nbd_recv_coroutines_enter_all (s); }static void nbd_restart_write (void *opaque)
@@ -332,6 +340,10 @@ static void nbd_teardown_connection(NbdClientSession *client)qemu_aio_set_fd_handler (client->sock,NULL ,NULL ,NULL );closesocket (client->sock); +/* finish any pending coroutines */ + client->reply.handle =0 ; + client->reply.error =EIO ; +nbd_recv_coroutines_enter_all (client); client->sock =-1 ; }
Signature · 1 line
1.8.3.1
PATCH 11/21 nbd: avoid uninitialized warnings
In reply to Marc-André Lureau==15815== Thread 1:
==15815== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==15815== at 0x65AD5CB: send (send.c:31) ==15815== by 0x37F84B: nbd_wr_sync (nbd.c:145) ==15815== by 0x37F94B: write_sync (nbd.c:186) ==15815== by 0x380FA9: nbd_send_request (nbd.c:681) ==15815== by 0x1C4A2D: nbd_teardown_connection (nbd-client.c:337) ==15815== by 0x1C4AD8: nbd_client_session_close (nbd-client.c:354) ==15815== by 0x1ED2D8: close_socketpair (spicebd.c:132) ==15815== by 0x1EE265: spice_close (spicebd.c:457) ==15815== by 0x1ACBF6: bdrv_close (block.c:1519) ==15815== by 0x1AD804: bdrv_delete (block.c:1772) ==15815== by 0x1B4136: bdrv_unref (block.c:4476) ==15815== by 0x1ACCE0: bdrv_close (block.c:1541)
==15815== Address 0x7feffef98 is on thread 1's stack
--- block/nbd-client.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
block/nbd-client.c +9 -12
@@ -186,11 +186,10 @@ static int nbd_co_readv_1(NbdClientSession *client, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int offset) { - struct nbd_request request; + struct nbd_request request = { .type = NBD_CMD_READ };struct nbd_reply reply;ssize_t ret; - request.type =NBD_CMD_READ ; request.from = sector_num *512 ; request.len = nb_sectors *512 ;
@@ -210,11 +209,10 @@ static int nbd_co_writev_1(NbdClientSession *client, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int offset) { - struct nbd_request request; + struct nbd_request request = { .type = NBD_CMD_WRITE };struct nbd_reply reply;ssize_t ret; - request.type =NBD_CMD_WRITE ;if (!bdrv_enable_write_cache (client->bs) && (client->nbdflags &NBD_FLAG_SEND_FUA )) { request.type |=NBD_CMD_FLAG_FUA ;
@@ -276,7 +274,7 @@ int nbd_client_session_co_writev(NbdClientSession *client, int64_t sector_num,int nbd_client_session_co_flush (NbdClientSession *client) { -struct nbd_request request; +struct nbd_request request = { .type =NBD_CMD_FLUSH };struct nbd_reply reply;ssize_t ret;
@@ -284,7 +282,6 @@ int nbd_client_session_co_flush(NbdClientSession *client)return 0 ; } - request.type =NBD_CMD_FLUSH ;if (client->nbdflags &NBD_FLAG_SEND_FUA ) { request.type |=NBD_CMD_FLAG_FUA ; }
@@ -306,14 +303,13 @@ int nbd_client_session_co_flush(NbdClientSession *client)int nbd_client_session_co_discard (NbdClientSession *client,int64_t sector_num,int nb_sectors) { -struct nbd_request request; +struct nbd_request request = { .type =NBD_CMD_TRIM };struct nbd_reply reply;ssize_t ret;if (!(client->nbdflags &NBD_FLAG_SEND_TRIM )) {return 0 ; } - request.type =NBD_CMD_TRIM ; request.from = sector_num *512 ; request.len = nb_sectors *512 ;
@@ -331,11 +327,12 @@ int nbd_client_session_co_discard(NbdClientSession *client, int64_t sector_num,static void nbd_teardown_connection (NbdClientSession *client) { -struct nbd_request request; +struct nbd_request request = { + .type =NBD_CMD_DISC , + .from =0 , + .len =0 + }; - request.type =NBD_CMD_DISC ; - request.from =0 ; - request.len =0 ;nbd_send_request (client->sock, &request);qemu_aio_set_fd_handler (client->sock,NULL ,NULL ,NULL );
Signature · 1 line
1.8.3.1
PATCH 12/21 block: save the associated child name in BlockDriverState
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
This allows the Spice block driver to eject the associated device.
--- block.c | 58 ++++++++++++++++++++++++++++++++--------------- include/block/block_int.h | 1 + 2 files changed, 41 insertions(+), 18 deletions(-)
block.c +40 -18
@@ -318,7 +318,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;
@@ -334,9 +335,24 @@ BlockDriverState *bdrv_new(const char *device_name)qemu_co_queue_init (&bs->throttled_reqs[1 ]); bs->refcnt =1 ; +if (child) { +if (strlen (child->child_device_name)) { +pstrcpy (bs->child_device_name,sizeof (bs->child_device_name), + child->child_device_name); + }else { +pstrcpy (bs->child_device_name,sizeof (bs->child_device_name), + child->device_name); + } + } +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);
@@ -847,16 +863,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,Error **errp) +static int bdrv_file_open_int (BlockDriverState **pbs,const char *filename, +QDict *options,int flags,BlockDriverState *child,Error **errp) {BlockDriverState *bs;BlockDriver *drv;
@@ -870,7 +878,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);
@@ -957,6 +965,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,Error **errp) +{ +return bdrv_file_open_int (pbs, filename, options, flags,NULL , errp); +} + +/* * 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
@@ -992,8 +1014,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) sizeof(backing_filename)); } - bs->backing_hd =bdrv_new ("" ); - + bs->backing_hd =bdrv_new_int ("" , bs);if (bs->backing_format[0 ] !='\0' ) { back_drv =bdrv_find_format (bs->backing_format); }
@@ -1063,7 +1084,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, &local_err);if (ret <0 ) {bdrv_unref (bs1);
@@ -1124,8 +1145,9 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,qdict_extract_subqdict (options, &file_options,"file." ); - ret =bdrv_file_open (&file, filename, file_options, -bdrv_open_flags (bs, flags |BDRV_O_UNMAP ), &local_err); + ret =bdrv_file_open_int (&file, filename, file_options, +bdrv_open_flags (bs, flags |BDRV_O_UNMAP ), + bs, &local_err);if (ret <0 ) { goto fail; }
@@ -1883,7 +1905,7 @@ int bdrv_commit(BlockDriverState *bs)if (!drv)return -ENOMEDIUM ; - +if (!bs->backing_hd) {return -ENOTSUP ; }
include/block/block_int.h +1 -0
@@ -301,6 +301,7 @@ struct BlockDriverState {bool iostatus_enabled;BlockDeviceIoStatus iostatus;char device_name[32 ]; +char child_device_name[32 ];HBitmap *dirty_bitmap;int refcnt;int in_use;/* users other than guest access, eg. block migration */
Signature · 1 line
1.8.3.1
PATCH 13/21 blockdev: add qmp_change_blockdev_int()
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
Allow to pass additional arguments, such as options and opaque
--- blockdev.c | 24 +++++++++++++++++++----- include/sysemu/blockdev.h | 3 +++ 2 files changed, 22 insertions(+), 5 deletions(-)
blockdev.c +19 -5
@@ -1498,12 +1498,13 @@ void qmp_block_passwd(const char *device, const char *password, Error **errp)static void qmp_bdrv_open_encrypted (BlockDriverState *bs,const char *filename,int bdrv_flags,BlockDriver *drv, -const char *password,Error **errp) +const char *password,QDict *options, +Error **errp) {Error *local_err =NULL ;int ret; - ret =bdrv_open (bs, filename,NULL , bdrv_flags, drv, &local_err); + ret =bdrv_open (bs, filename, options, bdrv_flags, drv, &local_err);if (ret <0 ) {error_propagate (errp, local_err);return ;
@@ -1523,8 +1524,9 @@ static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, } } -void qmp_change_blockdev (const char *device,const char *filename, -const char *format,Error **errp) +void qmp_change_blockdev_int (const char *device,const char *filename, +const char *format,QDict *options,void *opaque, +Error **errp) {BlockDriverState *bs;BlockDriver *drv =NULL ;
@@ -1554,7 +1556,19 @@ void qmp_change_blockdev(const char *device, const char *filename, bdrv_flags =bdrv_is_read_only (bs) ?0 :BDRV_O_RDWR ; bdrv_flags |=bdrv_is_snapshot (bs) ?BDRV_O_SNAPSHOT :0 ; -qmp_bdrv_open_encrypted (bs, filename, bdrv_flags, drv,NULL , errp); +if (bs->opaque) { +error_set (errp,QERR_INVALID_PARAMETER , device); +return ; + } + bs->opaque = opaque; + +qmp_bdrv_open_encrypted (bs, filename, bdrv_flags, drv,NULL , options, errp); +} + +void qmp_change_blockdev (const char *device,const char *filename, +const char *format,Error **errp) +{ +qmp_change_blockdev_int (device, filename, format,NULL ,NULL , errp); }/* throttling disk I/O limits */
include/sysemu/blockdev.h +3 -0
@@ -65,6 +65,9 @@ DriveInfo *add_init_drive(const char *opts);void qmp_change_blockdev (const char *device,const char *filename,const char *format,Error **errp); +void qmp_change_blockdev_int (const char *device,const char *filename, +const char *format,QDict *options,void *opaque, +Error **errp);void do_commit (Monitor *mon,const QDict *qdict);int do_drive_del (Monitor *mon,const QDict *qdict,QObject **ret_data);#endif
Signature · 1 line
1.8.3.1
PATCH 14/21 block: extract make_snapshot() from bdrv_open()
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- block.c | 121 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 51 deletions(-)
block.c +70 -51
@@ -1038,6 +1038,73 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)return 0 ; } +static int make_snapshot (BlockDriverState *bs,int64_t total_size, +const char **pfilename,BlockDriver **pdrv, +Error **errp) +{ +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 ]; +Error *local_err =NULL ; + +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; +error_setg_errno (errp, errno,"Could not resolve path '%s'" , filename); + 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, &local_err); +free_option_parameters (create_options); +if (ret <0 ) { +error_setg_errno (errp, -ret,"Could not create temporary overlay " +"'%s': %s" , tmp_filename, +error_get_pretty (local_err)); +error_free (local_err); + local_err =NULL ; + goto fail; + } + + *pfilename = tmp_filename; + *pdrv = bdrv_qcow2; + bs->is_temporary =1 ; +return 0 ; + +fail: +return ret; +} +/* * Opens a disk image (raw, qcow2, vmdk, ...) *
@@ -1050,8 +1117,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, int flags, BlockDriver *drv, Error **errp) { 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 ;const char *drvname;
@@ -1069,73 +1134,27 @@ 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_setg (errp,"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, &local_err);if (ret <0 ) {bdrv_unref (bs1); goto fail; } - total_size =bdrv_getlength (bs1) &BDRV_SECTOR_MASK ; + total_size =bdrv_getlength (bs1) &BDRV_SECTOR_MASK ;bdrv_unref (bs1); - ret =get_tmp_filename (tmp_filename,sizeof (tmp_filename)); -if (ret <0 ) { -error_setg_errno (errp, -ret,"Could not get temporary filename" ); - 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; -error_setg_errno (errp, errno,"Could not resolve path '%s'" , filename); - 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, &local_err); -free_option_parameters (create_options); + ret =make_snapshot (bs, total_size, &filename, &drv, errp);if (ret <0 ) { -error_setg_errno (errp, -ret,"Could not create temporary overlay " -"'%s': %s" , tmp_filename, -error_get_pretty (local_err)); -error_free (local_err); - local_err =NULL ; goto fail; } - - filename = tmp_filename; - drv = bdrv_qcow2; - bs->is_temporary =1 ; }/* Open image file without format layer */
Signature · 1 line
1.8.3.1
PATCH 15/21 block: add "snapshot.size" option to avoid extra bdrv_open()
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- block.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
block.c +12 -7
@@ -1135,27 +1135,32 @@ 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_setg (errp,"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, &local_err); -if (ret <0 ) { +if (total_size ==-1 ) { + bs1 =bdrv_new_int ("" ,NULL ); + ret =bdrv_open (bs1, filename,NULL ,0 , drv, &local_err); +if (ret <0 ) { +bdrv_unref (bs1); + goto fail; + } + total_size =bdrv_getlength (bs1);bdrv_unref (bs1); - goto fail; } - total_size =bdrv_getlength (bs1) &BDRV_SECTOR_MASK ; -bdrv_unref (bs1); - ret =make_snapshot (bs, total_size, &filename, &drv, errp);if (ret <0 ) { goto fail; } } +qdict_del (options,"snapshot.size" );/* Open image file without format layer */ if (flags &BDRV_O_RDWR ) {
Signature · 1 line
1.8.3.1
PATCH 16/21 block: learn to open a driver with a given opaque
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
If 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 | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-)
block.c +29 -19
@@ -319,7 +319,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;
@@ -344,13 +344,14 @@ static BlockDriverState *bdrv_new_int(const char *device_name, child->device_name); } } + 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)
@@ -810,7 +811,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 );
@@ -864,7 +867,8 @@ free_and_fail: }static int bdrv_file_open_int (BlockDriverState **pbs,const char *filename, -QDict *options,int flags,BlockDriverState *child,Error **errp) +QDict *options,int flags,BlockDriverState *child,void *opaque, +Error **errp) {BlockDriverState *bs;BlockDriver *drv;
@@ -878,7 +882,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);
@@ -975,18 +979,11 @@ fail:int bdrv_file_open (BlockDriverState **pbs,const char *filename,QDict *options,int flags,Error **errp) { -return bdrv_file_open_int (pbs, filename, options, flags,NULL , errp); +return bdrv_file_open_int (pbs, filename, options, flags,NULL ,NULL , errp); } -/* -* 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,Error **errp) +static int bdrv_open_backing_file_int (BlockDriverState *bs, +QDict *options,void *opaque,Error **errp) {char backing_filename[PATH_MAX ];int back_flags, ret;
@@ -1014,7 +1011,7 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) sizeof(backing_filename)); } - bs->backing_hd =bdrv_new_int ("" , bs); + bs->backing_hd =bdrv_new_int ("" , bs, opaque);if (bs->backing_format[0 ] !='\0' ) { back_drv =bdrv_find_format (bs->backing_format); }
@@ -1038,6 +1035,19 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)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,Error **errp) +{ +return bdrv_open_backing_file_int (bs, options,NULL , errp); +} +static int make_snapshot (BlockDriverState *bs,int64_t total_size,const char **pfilename,BlockDriver **pdrv,Error **errp)
@@ -1145,7 +1155,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, &local_err);if (ret <0 ) {bdrv_unref (bs1);
@@ -1171,7 +1181,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, ret =bdrv_file_open_int (&file, filename, file_options,bdrv_open_flags (bs, flags |BDRV_O_UNMAP ), - bs, &local_err); + bs,NULL , &local_err);if (ret <0 ) { goto fail; }
@@ -1207,7 +1217,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,QDict *backing_options;qdict_extract_subqdict (options, &backing_options,"backing." ); - ret =bdrv_open_backing_file (bs, backing_options, &local_err); + ret =bdrv_open_backing_file_int (bs, backing_options,NULL , &local_err);if (ret <0 ) { goto close_and_fail; }
Signature · 1 line
1.8.3.1
PATCH 17/21 block: allow to call bdrv_open() with an opaque
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
If the block driver already has a bs->opaque when calling bdrv_open(), pass it down to the file driver.
Conflicts:
block.c
--- block.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
block.c +7 -2
@@ -1131,6 +1131,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,QDict *file_options =NULL ;const char *drvname;Error *local_err =NULL ; +void *backing_opaque =NULL ;/* NULL means an empty set of options */ if (options ==NULL ) {
@@ -1154,6 +1155,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, &local_err);
@@ -1181,7 +1184,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, ret =bdrv_file_open_int (&file, filename, file_options,bdrv_open_flags (bs, flags |BDRV_O_UNMAP ), - bs,NULL , &local_err); + bs, bs->opaque, &local_err); + bs->opaque =NULL ;if (ret <0 ) { goto fail; }
@@ -1217,7 +1221,8 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,QDict *backing_options;qdict_extract_subqdict (options, &backing_options,"backing." ); - ret =bdrv_open_backing_file_int (bs, backing_options,NULL , &local_err); + ret =bdrv_open_backing_file_int (bs, backing_options, + backing_opaque, &local_err);if (ret <0 ) { goto close_and_fail; }
Signature · 1 line
1.8.3.1
PATCH 18/21 block: do not notify change during migration
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
When starting qemu, a block driver isn't associated with a device, so no notification is emitted when the media is loaded.
The Spice block driver loads the media during migration. But at that time, the device is already associated, however, we want to avoid notification to the guest. Checking the runstate seems the simplest way.
--- block.c | 4 +++- hw/block/fdc.c | 8 +++++--- hw/ide/core.c | 12 +++++++----- hw/scsi/scsi-disk.c | 11 +++++++---- hw/sd/sd.c | 6 +++++- include/block/block.h | 2 +- stubs/vm-stop.c | 5 +++++ 7 files changed, 33 insertions(+), 15 deletions(-)
block.c +3 -1
@@ -1864,9 +1864,11 @@ static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected)static void bdrv_dev_change_media_cb (BlockDriverState *bs,bool load) { +bool notify = !runstate_check (RUN_STATE_INMIGRATE ); +if (bs->dev_ops && bs->dev_ops->change_media_cb) {bool tray_was_closed = !bdrv_dev_is_tray_open (bs); - bs->dev_ops->change_media_cb (bs->dev_opaque, load); + bs->dev_ops->change_media_cb (bs->dev_opaque, load, notify);if (tray_was_closed) {/* tray open */ bdrv_emit_qmp_eject_event (bs, true);
hw/block/fdc.c +5 -3
@@ -1984,11 +1984,13 @@ static void fdctrl_result_timer(void *opaque) } } -static void fdctrl_change_cb (void *opaque,bool load) +static void fdctrl_change_cb (void *opaque,bool load,bool notify) {FDrive *drive = opaque; - drive->media_changed =1 ; +if (notify) { + drive->media_changed =1 ; + }fd_revalidate (drive); }
@@ -2018,7 +2020,7 @@ static void fdctrl_connect_drives(FDCtrl *fdctrl, Error **errp) }fd_init (drive); -fdctrl_change_cb (drive,0 ); +fdctrl_change_cb (drive,0 , false);if (drive->bs) {bdrv_set_dev_ops (drive->bs, &fdctrl_block_ops, drive); }
hw/ide/core.c +7 -5
@@ -882,7 +882,7 @@ static void ide_cfata_metadata_write(IDEState *s) }/* called when the inserted state of the media has changed */ -static void ide_cd_change_cb (void *opaque,bool load) +static void ide_cd_change_cb (void *opaque,bool load,bool notify) {IDEState *s = opaque;uint64_t nb_sectors;
@@ -898,10 +898,12 @@ static void ide_cd_change_cb(void *opaque, bool load)* Then we set UNIT_ATTENTION, by which the guest will * detect a new CD in the drive. See ide_atapi_cmd() for details. */ - s->cdrom_changed =1 ; - s->events.new_media = true; - s->events.eject_request = false; -ide_set_irq (s->bus); +if (notify) { + s->cdrom_changed =1 ; + s->events.new_media = true; + s->events.eject_request = false; +ide_set_irq (s->bus); + } }static void ide_cd_eject_request_cb (void *opaque,bool force)
hw/scsi/scsi-disk.c +7 -4
@@ -2010,7 +2010,7 @@ static void scsi_disk_resize_cb(void *opaque) } } -static void scsi_cd_change_media_cb (void *opaque,bool load) +static void scsi_cd_change_media_cb (void *opaque,bool load,bool notify) {SCSIDiskState *s = opaque;
@@ -2024,11 +2024,14 @@ static void scsi_cd_change_media_cb(void *opaque, bool load)* media_changed governs the state machine used for unit attention * report. media_event is used by GET EVENT STATUS NOTIFICATION. */ - s->media_changed = load; s->tray_open = !load; -scsi_device_set_ua (&s->qdev,SENSE_CODE (UNIT_ATTENTION_NO_MEDIUM )); - s->media_event = true; s->eject_request = false; + +if (notify) { + s->media_changed = load; +scsi_device_set_ua (&s->qdev,SENSE_CODE (UNIT_ATTENTION_NO_MEDIUM )); + s->media_event = true; + } }static void scsi_cd_eject_request_cb (void *opaque,bool force)
hw/sd/sd.c +5 -1
@@ -439,10 +439,14 @@ static void sd_reset(SDState *sd, BlockDriverState *bdrv) sd->expecting_acmd = false; } -static void sd_cardchange (void *opaque,bool load) +static void sd_cardchange (void *opaque,bool load,bool notify) {SDState *sd = opaque; +if (!notify) { +return ; + } +qemu_set_irq (sd->inserted_cb,bdrv_is_inserted (sd->bdrv));if (bdrv_is_inserted (sd->bdrv)) {sd_reset (sd, sd->bdrv);
include/block/block.h +1 -1
@@ -36,7 +36,7 @@ typedef struct BlockDevOps {* changes. Sure would be useful if it did. * Device models with removable media must implement this callback. */ -void (*change_media_cb)(void *opaque,bool load); +void (*change_media_cb)(void *opaque,bool load,bool notify);/* * Runs when an eject request is issued from the monitor, the tray * is closed, and the medium is locked.
stubs/vm-stop.c +5 -0
@@ -5,3 +5,8 @@ int vm_stop(RunState state) {abort (); } + +bool runstate_check (RunState state) +{ +return FALSE; +}
Signature · 1 line
1.8.3.1
PATCH 19/21 sysemu: add vm_start_hold/release
In reply to Marc-André LureauThis is a simple solution (or hack?) to allow the Spice block driver to hold the VM from starting before the migration state is completed.
During migration, the destination qemu needs to initialize the NBD session. This requires waiting for the Spice client and communication before the VM is started, but using a running main loop.
--- include/sysemu/sysemu.h | 2 ++ vl.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+)
include/sysemu/sysemu.h +2 -0
@@ -38,6 +38,8 @@ void vm_state_notify(int running, RunState state);#define VMRESET_REPORT truevoid vm_start (void ); +void vm_start_hold (void ); +void vm_start_release (void );int vm_stop (RunState state);int vm_stop_force_state (RunState state);
vl.c +17 -0
@@ -1690,8 +1690,25 @@ void vm_state_notify(int running, RunState state) } } +static int start_hold; + +void vm_start_hold (void ) +{ + start_hold++; +} + +void vm_start_release (void ) +{ + start_hold--; +vm_start (); +} +void vm_start (void ) { +if (start_hold !=0 ) { +return ; + } +if (!runstate_is_running ()) {cpu_enable_ticks ();runstate_set (RUN_STATE_RUNNING );
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 19/21] sysemu: add vm_start_hold/release
Il 18/11/2013 13:25, Marc-André Lureau ha scritto:
Quoted 9 lines
vl.c +9
-0+static int start_hold; + +void vm_start_hold (void ) +{ + start_hold++; +} + +void vm_start_release (void ) +{
vl.c void vm_state_notify(int running, RunState state) · line 1711
Quoted 10 lines
vl.c +8
-0+ start_hold--; +vm_start (); +} +void vm_start (void ) { +if (start_hold !=0 ) { +return ; + } +
This is interesting. I like it, but I think this sequence:
vm_start_hold()
vm_start_release()
should not call vm_start(), while this:
vm_start_hold()
vm_start()
vm_start_release()
should call it.
Also, vm_start_hold() should assert that the VM is not running.
Paolo
PATCH 20/21 spice-core: allow an interface to be in AIO context
In reply to Marc-André LureauThe Spice block driver must be able complete operations within a AIO context only.
Spice is currently only running within the main loop, and doesn't allow the block driver to complete operations, such as flush during migration.
This patch allows a Spice interface to be associated with a different context. Currently, the interface user_data is simply used to differentiate main loop from AIO, but could later be used to associate an interface with a particular thread.
--- include/ui/qemu-spice.h | 2 +- qemu-char.c | 2 +- spice-qemu-char.c | 9 +++---- ui/spice-core.c | 62 +++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 62 insertions(+), 13 deletions(-)
include/ui/qemu-spice.h +1 -1
@@ -48,7 +48,7 @@ int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,void do_info_spice_print (Monitor *mon,const QObject *data);void do_info_spice (Monitor *mon,QObject **ret_data); -CharDriverState *qemu_chr_open_spice_vmc (const char *type); +CharDriverState *qemu_chr_open_spice_vmc (const char *type,bool aio);#if SPICE_SERVER_VERSION >=0x000c02 CharDriverState *qemu_chr_open_spice_port (const char *name);void qemu_spice_register_ports (void );
qemu-char.c +1 -1
@@ -3747,7 +3747,7 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,#endif #ifdef CONFIG_SPICE case CHARDEV_BACKEND_KIND_SPICEVMC : - chr =qemu_chr_open_spice_vmc (backend->spicevmc->type); + chr =qemu_chr_open_spice_vmc (backend->spicevmc->type, false);break ;case CHARDEV_BACKEND_KIND_SPICEPORT : chr =qemu_chr_open_spice_port (backend->spiceport->fqdn);
spice-qemu-char.c +5 -4
@@ -248,7 +248,7 @@ static void print_allowed_subtypes(void)fprintf (stderr,"\n" ); } -static CharDriverState *chr_open (const char *subtype) +static CharDriverState *chr_open (const char *subtype,bool aio) {CharDriverState *chr;SpiceCharDriver *s;
@@ -257,6 +257,7 @@ static CharDriverState *chr_open(const char *subtype) s =g_malloc0 (sizeof (SpiceCharDriver)); s->chr = chr; s->active = false; + s->sin.base.user_data = (void *)aio; s->sin.subtype =g_strdup (subtype); chr->opaque = s; chr->chr_write = spice_chr_write;
@@ -271,7 +272,7 @@ static CharDriverState *chr_open(const char *subtype)return chr; } -CharDriverState *qemu_chr_open_spice_vmc (const char *type) +CharDriverState *qemu_chr_open_spice_vmc (const char *type,bool aio) {const char **psubtype =spice_server_char_device_recognized_subtypes ();
@@ -291,7 +292,7 @@ CharDriverState *qemu_chr_open_spice_vmc(const char *type)return NULL ; } -return chr_open (type); +return chr_open (type, aio); }#if SPICE_SERVER_VERSION >=0x000c02
@@ -305,7 +306,7 @@ CharDriverState *qemu_chr_open_spice_port(const char *name)return NULL ; } - chr =chr_open ("port" ); + chr =chr_open ("port" , false); s = chr->opaque; s->sin.portname =g_strdup (name);
ui/spice-core.c +55 -7
118 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [Spice-devel] [PATCH 20/21] spice-core: allow an interface to be in AIO context
Quoted 109 lines · Marc-André Lureau
The Spice block driver must be able complete operations within a AIO context only.
Spice is currently only running within the main loop, and doesn't allow the block driver to complete operations, such as flush during migration.
This patch allows a Spice interface to be associated with a different context. Currently, the interface
user_datais simply used to differentiate main loop from AIO, but could later be used to associate an interface with a particular thread.Signed-off-by Marc-André Lureau <[email protected]>--- include/ui/qemu-spice.h | 2 +- qemu-char.c | 2 +- spice-qemu-char.c | 9 +++---- ui/spice-core.c | 62 +++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 62 insertions(+), 13 deletions(-)include/ui/qemu-spice.h +1
-1@@ -48,7 +48,7 @@ int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,void do_info_spice_print (Monitor *mon,const QObject *data);void do_info_spice (Monitor *mon,QObject **ret_data); -CharDriverState *qemu_chr_open_spice_vmc (const char *type); +CharDriverState *qemu_chr_open_spice_vmc (const char *type,bool aio);#if SPICE_SERVER_VERSION >=0x000c02 CharDriverState *qemu_chr_open_spice_port (const char *name);void qemu_spice_register_ports (void );qemu-char.c +1
-1@@ -3747,7 +3747,7 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,#endif #ifdef CONFIG_SPICE case CHARDEV_BACKEND_KIND_SPICEVMC : - chr =qemu_chr_open_spice_vmc (backend->spicevmc->type); + chr =qemu_chr_open_spice_vmc (backend->spicevmc->type, false);break ;case CHARDEV_BACKEND_KIND_SPICEPORT : chr =qemu_chr_open_spice_port (backend->spiceport->fqdn);spice-qemu-char.c +5
-4@@ -248,7 +248,7 @@ static void print_allowed_subtypes(void)fprintf (stderr,"\n" ); } -static CharDriverState *chr_open (const char *subtype) +static CharDriverState *chr_open (const char *subtype,bool aio) {CharDriverState *chr;SpiceCharDriver *s;@@ -257,6 +257,7 @@ static CharDriverState *chr_open(const char *subtype) s =g_malloc0 (sizeof (SpiceCharDriver)); s->chr = chr; s->active = false; + s->sin.base.user_data = (void *)aio; s->sin.subtype =g_strdup (subtype); chr->opaque = s; chr->chr_write = spice_chr_write;@@ -271,7 +272,7 @@ static CharDriverState *chr_open(const char *subtype)return chr; } -CharDriverState *qemu_chr_open_spice_vmc (const char *type) +CharDriverState *qemu_chr_open_spice_vmc (const char *type,bool aio) {const char **psubtype =spice_server_char_device_recognized_subtypes ();@@ -291,7 +292,7 @@ CharDriverState *qemu_chr_open_spice_vmc(const char *type)return NULL ; } -return chr_open (type); +return chr_open (type, aio); }#if SPICE_SERVER_VERSION >=0x000c02 @@ -305,7 +306,7 @@ CharDriverState *qemu_chr_open_spice_port(const char *name)return NULL ; } - chr =chr_open ("port" ); + chr =chr_open ("port" , false); s = chr->opaque; s->sin.portname =g_strdup (name);ui/spice-core.c +4
-0@@ -53,34 +53,64 @@ static QemuThread me;struct SpiceTimer {QEMUTimer *timer; +QEMUBH *bh;QTAILQ_ENTRY (SpiceTimer) next; }; static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers); +#if SPICE_INTERFACE_CORE_MAJOR >=2 +static SpiceTimer *timer_add (SpiceTimerFunc func,void *opaque,SpiceBaseInstance *sin) +#else
ui/spice-core.c static QemuThread me; · line 72
Quoted 10 lines
ui/spice-core.c +4
-1static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque) +#endif {SpiceTimer *timer; timer =g_malloc0 (sizeof (*timer)); - timer->timer =timer_new_ms (QEMU_CLOCK_REALTIME , func, opaque); + +#if SPICE_INTERFACE_CORE_MAJOR >=2 +bool aio = sin ? !!sin->user_data : false;
Shouldn't there be a cast there:
(bool)sin->user_data
?
Quoted 102 lines
ui/spice-core.c +47
-6+if (aio) { +fprintf (stderr,"AIO doesn't have timers yet, using BH\n" ); + timer->bh =qemu_bh_new (func, opaque); + } else +#endif + { + timer->timer =timer_new_ms (QEMU_CLOCK_REALTIME , func, opaque); + } +QTAILQ_INSERT_TAIL (&timers, timer, next); +return timer; }static void timer_start (SpiceTimer *timer,uint32_t ms) { -timer_mod (timer->timer,qemu_clock_get_ms (QEMU_CLOCK_REALTIME ) + ms); +if (timer->bh) { +qemu_bh_schedule_idle (timer->bh);/* at least every 10ms, see async.c */ + }else { +timer_mod (timer->timer,qemu_clock_get_ms (QEMU_CLOCK_REALTIME ) + ms); + } }static void timer_cancel (SpiceTimer *timer) { -timer_del (timer->timer); +if (timer->bh) { +qemu_bh_cancel (timer->bh); + }else { +timer_del (timer->timer); + } }static void timer_remove (SpiceTimer *timer) { -timer_del (timer->timer); -timer_free (timer->timer); +if (timer->bh) { +qemu_bh_delete (timer->bh); + }else { +timer_del (timer->timer); +timer_free (timer->timer); + } +QTAILQ_REMOVE (&timers, timer, next);g_free (timer); }@@ -89,6 +119,7 @@ struct SpiceWatch {int fd;int event_mask;SpiceWatchFunc func; +bool aio;void *opaque;QTAILQ_ENTRY (SpiceWatch) next; };@@ -118,10 +149,19 @@ static void watch_update_mask(SpiceWatch *watch, int event_mask)if (watch->event_mask &SPICE_WATCH_EVENT_WRITE ) { on_write = watch_write; } -qemu_set_fd_handler (watch->fd, on_read, on_write, watch); + +if (watch->aio) { +qemu_aio_set_fd_handler (watch->fd, on_read, on_write, watch); + }else { +qemu_set_fd_handler (watch->fd, on_read, on_write, watch); + } } +#if SPICE_INTERFACE_CORE_MAJOR >=2 +static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque, SpiceBaseInstance *sin) +#else static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque) +#endif {SpiceWatch *watch;@@ -129,6 +169,10 @@ static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void * watch->fd = fd; watch->func = func; watch->opaque = opaque; +#if SPICE_INTERFACE_CORE_MAJOR >=2 + watch->aio = sin ? !!sin->user_data : false; +#endif +QTAILQ_INSERT_TAIL (&watches, watch, next);watch_update_mask (watch, event_mask);@@ -137,7 +181,11 @@ static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *static void watch_remove (SpiceWatch *watch) { -qemu_set_fd_handler (watch->fd,NULL ,NULL ,NULL ); +if (watch->aio) { +qemu_aio_set_fd_handler (watch->fd,NULL ,NULL ,NULL ); + }else { +qemu_set_fd_handler (watch->fd,NULL ,NULL ,NULL ); + }QTAILQ_REMOVE (&watches, watch, next);g_free (watch); }
PATCH 21/21 block: add spice block device backend
In reply to Marc-André LureauFrom: Marc-André Lureau <[email protected]>
--- block/Makefile.objs | 1 + block/spicebd.c | 536 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 537 insertions(+) create mode 100644 block/spicebd.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) += spicebd.o endif common-obj-y += stream.o
block/spicebd.c new file +536 -0
536 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 21/21] block: add spice block device backend
/note to self:
- add failing case where nbd channel isn't connected before and after migration (do not wait for nbd init or disconnection)
Quoted 566 lines · Marc-André Lureau
From: Marc-André Lureau <[email protected]>
Signed-off-by Marc-André Lureau <[email protected]>--- block/Makefile.objs | 1 + block/spicebd.c | 536 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 537 insertions(+) create mode 100644 block/spicebd.cblock/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) += spicebd.o endif common-obj-y += stream.oblock/spicebd.c new file +536
-1@@ -0,0 +1,536 @@ +/* +* Spice block backend for QEMU. +* +* Copyright (C) 2013 Red Hat, Inc. +* Author: Marc-André Lureau <[email protected]> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <spice/protocol.h> + +#include "nbd-client.h" +#include "ui/qemu-spice.h" +#include "block/block_int.h" +#include "qemu/sockets.h" +#include "qemu/uri.h" +#include "qapi/qmp/qint.h" +#include "sysemu/sysemu.h" +#include "sysemu/char.h" +#include "qmp-commands.h" +#include "sysemu/blockdev.h" +#include "migration/migration.h" + +#ifndef DEBUG_SPICE +#define DEBUG_SPICE 0 +#endif + +#define SOCKET_CHR 0 +#define SOCKET_NBD 1 + +#define DPRINTF (fmt, ...) \ + do { \ + if (DEBUG_SPICE) { \ + fprintf(stderr, "spicebd: %-15s " fmt "\n", \ + __func__, ##__VA_ARGS__); \ + } \ + } while (0) + +typedef struct Buffer { +uint8_t data[4096 ]; +uint8_t *p; +char left; +}Buffer ; + +typedef struct BDRVSpiceState { +BlockDriverState *bs; +QEMUBH *bh; +NbdClientSession client; + +/* our spicechr-fd pipe */ +int sv[2 ]; +Buffer readb; +Buffer writeb; + +int aio_count; +CharDriverState *chr; +guint chr_watch; + +Coroutine *coroutine; +bool need_read; +bool need_write; +bool opened; +bool inmigrate; +}BDRVSpiceState ; + +static void nbd_read_handler (void *opaque); +static void update_chr_handlers (BDRVSpiceState *s); + +static int parse_uri (const char *filename,QDict *options,Error **errp) +{ +URI *uri =NULL ; + + uri =uri_parse (filename); +if (!uri) { +return -EINVAL ; + } + +if (strcmp (uri->scheme,"spicebd" ) !=0 ) { +error_setg (errp,"URI scheme must be 'spicebd'" ); + goto err; + } + +uri_free (uri); +return 0 ; + + err: +if (uri) { +uri_free (uri); + } +return -EINVAL ; +} + +static void spice_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +parse_uri (filename, options, errp); +} + +static void co_restart (void *opaque) +{ +BDRVSpiceState *s = opaque; + +qemu_coroutine_enter (s->coroutine,NULL ); +} + +static void close_socketpair (BDRVSpiceState *s) +{ +if (!s->opened) { +return ; + } + +DPRINTF ("" ); +nbd_client_session_close (&s->client); + +if (s->sv[SOCKET_NBD ] >=0 ) { +qemu_aio_set_fd_handler (s->sv[SOCKET_NBD ],NULL ,NULL ,NULL ); +closesocket (s->sv[SOCKET_NBD ]); + s->sv[SOCKET_NBD ] =-1 ; + } + +if (s->sv[SOCKET_CHR ] >=0 ) { +qemu_aio_set_fd_handler (s->sv[SOCKET_CHR ],NULL ,NULL ,NULL ); +closesocket (s->sv[SOCKET_CHR ]); + s->sv[SOCKET_CHR ] =-1 ; + } + +if (s->inmigrate) { +vm_start_release (); + s->inmigrate = false; + } + + s->opened = FALSE; +if (s->coroutine && s->coroutine !=qemu_coroutine_self ()) { +co_restart (s); + } +} + +static int chardev_can_read (void *opaque) +{ +BDRVSpiceState *s = opaque; +int retval =0 ; + +GPollFD pfd = { + .fd = s->sv[SOCKET_CHR ], + .events =G_IO_OUT + }; +g_poll (&pfd,1 ,0 ); +if (pfd.revents &G_IO_OUT ) { + retval = s->writeb.left ==0 ?sizeof (s->writeb.data) :0 ; + } + +return retval; +} + +static void chardev_read (void *opaque,const uint8_t *buf,int size) +{ +BDRVSpiceState *s = opaque; +int written; + +DPRINTF ("reply from client %d" , size); + written =write (s->sv[SOCKET_CHR ], buf, size); +if (written ==-1 ) { +if (errno !=EAGAIN ) { +close_socketpair (s); +return ; + }else { + written =0 ; + } + } + +if (s->writeb.left ==0 ) { + size -= written; +assert (size <=sizeof (s->writeb.data)); +memcpy (s->writeb.data, buf, size); + s->writeb.p = s->writeb.data; + s->writeb.left = size; + }else { + s->writeb.left -= written; + s->writeb.p += written; + } + + s->need_write = s->writeb.left >0 ; +update_chr_handlers (s); +} + +static void change_blockdev_cb (void *opaque) +{ +BDRVSpiceState *s = opaque; +BlockDriverState *bs = s->bs; +Error *err =NULL ; +int bdrv_flags; +QDict *options =NULL ; +char device_name[32 ]; + +qemu_bh_delete (s->bh); + s->bh =NULL ; + +memcpy (device_name, bs->child_device_name,sizeof (device_name)); +DPRINTF ("set bs %p to NULL" , bs); + bs->opaque =NULL ; + + bdrv_flags =bdrv_is_read_only (bs) ?0 :BDRV_O_RDWR ; +if (bdrv_is_snapshot (bs)) { + bdrv_flags |=BDRV_O_SNAPSHOT ; + options =qdict_new (); +qdict_put (options,"snapshot.size" ,qint_from_int (s->client.size)); + } + +qmp_eject (device_name, true, true, &err); +if (error_is_set (&err)) { +fprintf (stderr,"spicebd: %s\n" ,error_get_pretty (err)); +error_free (err); +QDECREF (options); +free (s); + } + +qmp_change_blockdev_int (device_name,"spicebd:" ,NULL , + options, s, &err); +if (error_is_set (&err)) { +fprintf (stderr,"spicebd: %s\n" ,error_get_pretty (err)); +error_free (err); + } + +if (s->inmigrate) { +vm_start_release (); + s->inmigrate = false; + } +} + +static void coroutine_fn co_init(void *opaque) +{ +BDRVSpiceState *s = opaque; +BlockDriverState *bs = s->bs; + +DPRINTF ("temporary coroutine for session initialization %p, " +"device: %s" , s, bs->child_device_name); + +qemu_set_nonblock (s->sv[SOCKET_NBD ]); +/* After session_init, the fd_handler is managed by nbd-client */ +qemu_aio_set_fd_handler (s->sv[SOCKET_NBD ], co_restart,NULL , s); +if (nbd_client_session_init (&s->client, bs, + s->sv[SOCKET_NBD ],NULL ) <0 ) { +close_socketpair (s); + }else { +assert (s->bh ==NULL ); +/* NOTE: need to defer out of current AIO call, to avoid non-blocking +drain_all when ejecting? */ + s->bh =qemu_bh_new (change_blockdev_cb, s); +qemu_bh_schedule (s->bh); + } + + s->coroutine =NULL ; +DPRINTF ("end of temporary coroutine" ); +} + +static void spice_init (BDRVSpiceState *s) +{ +if (s->opened) { +return ; + } + + s->opened = TRUE; + +/* TODO: teach nbd to use a iostream api instead of a socket */ +if (socketpair (PF_UNIX ,SOCK_STREAM ,0 , s->sv) ==-1 ) { +fprintf (stderr,"failed to create socketpair\n" ); +return ; + } + + s->need_read = TRUE; +update_chr_handlers (s); + +if (runstate_check (RUN_STATE_INMIGRATE )) { +vm_start_hold (); + s->inmigrate = true; + } + +/* tell server we are ready */ +qemu_chr_fe_event (s->chr,SPICE_PORT_EVENT_OPENED ); + s->coroutine =qemu_coroutine_create (co_init); +qemu_coroutine_enter (s->coroutine, s); +} + +static void chardev_event (void *opaque,int event) +{ +BDRVSpiceState *s = opaque; + +switch (event) { +case CHR_EVENT_CLOSED : +DPRINTF ("chardev close" ); +close_socketpair (s); +break ; +case CHR_EVENT_BREAK : +DPRINTF ("chardev break" ); +if (s->coroutine) { +DPRINTF ("already waiting for incoming session" ); +qemu_chr_fe_event (s->chr,SPICE_PORT_EVENT_OPENED ); +return ; + } +close_socketpair (s); +/* fall-through */ +case CHR_EVENT_OPENED : +DPRINTF ("chardev opened" ); +spice_init (s); +break ; +default : +DPRINTF ("unhandled chardev event %d" , event); + } +} + +static gboolean write_to_chr (GIOChannel *chan,GIOCondition cond, +void *opaque) +{ +BDRVSpiceState *s = opaque; +int r; + + r =qemu_chr_fe_write (s->chr, s->readb.p, s->readb.left); +DPRINTF ("write_to_chr %d/%d" , r, s->readb.left); +if (r <=0 ) { +close_socketpair (s); +return FALSE; + } + + s->readb.p += r; + s->readb.left -= r; + +if (s->readb.left >0 ) { +if (!s->chr_watch) { + s->chr_watch =qemu_chr_fe_add_watch (s->chr,G_IO_OUT , + write_to_chr, s); + } +return TRUE; + }else { + s->need_read = TRUE; +update_chr_handlers (s); + } + +return FALSE; +} + +static void nbd_read_handler (void *opaque) +{ +BDRVSpiceState *s = opaque; + +DPRINTF ("read from nbd" ); + +if (s->readb.left >0 ) { +abort (); + } + +do { + s->readb.left =recv (s->sv[SOCKET_CHR ], s->readb.data, +sizeof (s->readb.data),MSG_DONTWAIT ); + }while (s->readb.left ==-1 && errno ==EAGAIN ); + +if (s->readb.left <=0 ) { +close_socketpair (s); +return ; + } + + s->need_read = FALSE; +update_chr_handlers (s); + + s->readb.p = s->readb.data; +write_to_chr (NULL ,0 , s); +} + +static void nbd_write_handler (void *opaque) +{ +BDRVSpiceState *s = opaque; + +DPRINTF ("resuming chardev_read left: %d" , s->writeb.left); + +chardev_read (s, s->writeb.data, s->writeb.left); +} + +static void update_chr_handlers (BDRVSpiceState *s) +{ +qemu_aio_set_fd_handler (s->sv[SOCKET_CHR ], + s->need_read ? nbd_read_handler :NULL , + s->need_write ? nbd_write_handler :NULL , + s); +} + +static int spice_file_open (BlockDriverState *bs,QDict *options,int bdrv_flags, +Error **errp) +{ +BDRVSpiceState *s = bs->opaque; +int ret =-1 ; + + s->bs = bs; + +if (s->opened) { +DPRINTF ("re-open spicebd" ); + s->client.bs = bs; +return 0 ; + } + +DPRINTF ("open %p device=%s flags=0x%x" , s, + bs->child_device_name, bdrv_flags); +if (strlen (bs->child_device_name) ==0 ) { +fprintf (stderr,"spicebd: missing associated child device\n" ); +return -1 ; + } + +if (bdrv_flags &BDRV_O_RDWR ) { +fprintf (stderr,"spicebd: only read-only supported\n" ); +return -1 ; + } + + s->chr =qemu_chr_open_spice_vmc ("nbd" , true); +if (!s->chr) { + goto err; + } + +qemu_chr_add_handlers (s->chr, chardev_can_read, + chardev_read, chardev_event, s); +spice_init (s); + +return 0 ; + + err: +return ret; +} + +static void spice_close (BlockDriverState *bs) +{ +BDRVSpiceState *s = bs->opaque; + +DPRINTF ("spice close %p\n" , s); + +if (s ==NULL ) { +/* changing bd */ +return ; + } + + s->bs =NULL ; + +close_socketpair (s); +assert (!s->coroutine);/* after close_socketpair */ + +if (s->chr) { + s->chr->chr_close (s->chr); +g_free (s->chr); + s->chr =NULL ; + } +} + +static coroutine_fn int spice_co_readv(BlockDriverState *bs, +int64_t sector_num, +int nb_sectors,QEMUIOVector *qiov) +{ +BDRVSpiceState *s = bs->opaque; + +return nbd_client_session_co_readv (&s->client, sector_num, + nb_sectors, qiov); +} + +static coroutine_fn int spice_co_writev(BlockDriverState *bs, +int64_t sector_num, +int nb_sectors,QEMUIOVector *qiov) +{ +BDRVSpiceState *s = bs->opaque; + +return nbd_client_session_co_writev (&s->client, sector_num, + nb_sectors, qiov); +} + +static coroutine_fn int spice_co_flush(BlockDriverState *bs) +{ +BDRVSpiceState *s = bs->opaque; + +if (s ==NULL ) { +/* changing bd */ +return -1 ; + } + +return nbd_client_session_co_flush (&s->client); +} + +static coroutine_fn int spice_co_discard(BlockDriverState *bs, +int64_t sector_num,int nb_sectors) +{ +BDRVSpiceState *s = bs->opaque; + +return nbd_client_session_co_discard (&s->client, sector_num, nb_sectors); +} + +static coroutine_fn int64_t spice_getlength(BlockDriverState *bs) +{ +BDRVSpiceState *s = bs->opaque; + +DPRINTF ("length=%" PRIi64, s->client.size); + +return s->client.size; +} + +static BlockDriver bdrv_spice = { + .format_name ="spicebd" , + .protocol_name ="spicebd" , + .instance_size =sizeof (BDRVSpiceState), + .bdrv_parse_filename = spice_parse_filename, + .bdrv_file_open = spice_file_open, + .bdrv_close = spice_close, + .bdrv_co_readv = spice_co_readv, + .bdrv_co_writev = spice_co_writev, + .bdrv_getlength = spice_getlength, + .bdrv_co_flush_to_os = spice_co_flush, + .bdrv_co_discard = spice_co_discard, +}; + +static void bdrv_spice_init (void ) +{ +bdrv_register (&bdrv_spice); +} + +block_init (bdrv_spice_init); --1.8.3.1
Signature · 1 line
Marc-André Lureau
Hi there,
Even though there is no rush to review this series, it would be nice to get the first ~10 patches. It would make the next rebase easier, and the review smaller.
cheers
Quoted 96 lines · Marc-André Lureau
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.
The backend only support read-only device atm (although it shouldn't be hard to add write support if necessary)
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-November/015452.html
http://lists.freedesktop.org/archives/spice-devel/2013-November/015431.htmlCaveats: 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. We also save the associate device name in the block drivers.
During migration, the source needs to be able to flush pending operations, so the Spice channel context must be in a running loop. A modification to the Spice server API allows to associate a particular channel with the AIO loop, and may be used in the future to associate channels with different context or athreads. However, the AIO context doesn't have timers yet. Since they aren't really needed for the NBD channel, it's not a problem. I have been told timers in AIO are on their way, so this could be updated later.
Since the block driver state is not migrated, the destination needs to wait until the block driver is initialized before the VM can run. This is done with a simple hold count. It is also necessary to avoid extra media changed notifications, which is easily done by checking migration state.
Marc-André Lureau (21):
vscclient: do not add a socket watch if there is not data to send
spice-char: remove unused field
qmp_change_blockdev()remove unusedhas_format
include: add missingconfig-host.hinclude
char: addqemu_chr_fe_event()
Split nbd block client code
nbd: don't change socket block during negotiate
nbd: pass export name as init argument
nbd: makesession_close()idempotent
nbd: finish any pending coroutine
nbd: avoid uninitialized warnings
block: save the associated child name in BlockDriverState
blockdev: addqmp_change_blockdev_int()
block: extractmake_snapshot()frombdrv_open()
block: add "snapshot.size" option to avoid extrabdrv_open()
block: learn to open a driver with a given opaque
block: allow to callbdrv_open()with an opaque
block: do not notify change during migration
sysemu: add vm_start_hold/release
spice-core: allow an interface to be in AIO context
block: add spice block device backendblock.c | 225 ++++++++++++------- block/Makefile.objs | 3 +- block/nbd-client.c | 384 +++++++++++++++++++++++++++++++++ block/nbd-client.h | 50 +++++ block/nbd.c | 380 +++----------------------------- block/spicebd.c | 536 ++++++++++++++++++++++++++++++++++++++++++++++ blockdev.c | 24 ++- hw/block/fdc.c | 8 +- hw/ide/core.c | 12 +- hw/scsi/scsi-disk.c | 11 +- hw/sd/sd.c | 6 +- include/block/block.h | 2 +- include/block/block_int.h | 1 + include/sysemu/blockdev.h | 5 +- include/sysemu/char.h | 10 + include/sysemu/sysemu.h | 2 + include/ui/qemu-spice.h | 4 +- libcacard/vscclient.c | 10 +- nbd.c | 1 - qemu-char.c | 9 +- qmp.c | 2 +- spice-qemu-char.c | 20 +- stubs/vm-stop.c | 5 + ui/spice-core.c | 62 +++++- vl.c | 17 ++ 25 files changed, 1320 insertions(+), 469 deletions(-) create mode 100644 block/nbd-client.c create mode 100644 block/nbd-client.h create mode 100644 block/spicebd.c--
1.8.3.1
Signature · 1 line
Marc-André Lureau
Quoted 5 lines · Marc-André Lureau
Hi there,
Even though there is no rush to review this series, it would be nice to get the first ~10 patches. It would make the next rebase easier, and the review smaller.
Can you send the ready-to-go spice bits as separate patch series (after addressing alon's comment on patch #5)? Then I'll add them to the spice queue.
thanks,
Gerd