qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 13/22] blockdev: make image streaming safe across hotplug
Date: Thu, 26 Jan 2012 17:38:07 +0100	[thread overview]
Message-ID: <1327595896-19623-14-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1327595896-19623-1-git-send-email-kwolf@redhat.com>

From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>

Unplugging a storage interface like virtio-blk causes the host block
device to be deleted too.  Long-running operations like block migration
must take a DriveInfo reference to prevent the BlockDriverState from
being freed.  For image streaming we can do the same thing.

Note that it is not possible to acquire/release the drive reference in
block.c where the block job functions live because
drive_get_ref()/drive_put_ref() are blockdev.c functions.  Calling them
from block.c would be a layering violation - tools like qemu-img don't
even link against blockdev.c.

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 blockdev.c |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/blockdev.c b/blockdev.c
index d026cd4..2fa0151 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -202,6 +202,37 @@ void drive_get_ref(DriveInfo *dinfo)
     dinfo->refcount++;
 }
 
+typedef struct {
+    QEMUBH *bh;
+    DriveInfo *dinfo;
+} DrivePutRefBH;
+
+static void drive_put_ref_bh(void *opaque)
+{
+    DrivePutRefBH *s = opaque;
+
+    drive_put_ref(s->dinfo);
+    qemu_bh_delete(s->bh);
+    g_free(s);
+}
+
+/*
+ * Release a drive reference in a BH
+ *
+ * It is not possible to use drive_put_ref() from a callback function when the
+ * callers still need the drive.  In such cases we schedule a BH to release the
+ * reference.
+ */
+static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
+{
+    DrivePutRefBH *s;
+
+    s = g_new(DrivePutRefBH, 1);
+    s->bh = qemu_bh_new(drive_put_ref_bh, s);
+    s->dinfo = dinfo;
+    qemu_bh_schedule(s->bh);
+}
+
 static int parse_block_error_action(const char *buf, int is_read)
 {
     if (!strcmp(buf, "ignore")) {
@@ -934,6 +965,8 @@ static void block_stream_cb(void *opaque, int ret)
         monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
     }
     qobject_decref(obj);
+
+    drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
 }
 
 void qmp_block_stream(const char *device, bool has_base,
@@ -966,6 +999,11 @@ void qmp_block_stream(const char *device, bool has_base,
         }
     }
 
+    /* Grab a reference so hotplug does not delete the BlockDriverState from
+     * underneath us.
+     */
+    drive_get_ref(drive_get_by_blockdev(bs));
+
     trace_qmp_block_stream(bs, bs->job);
 }
 
-- 
1.7.6.5

  parent reply	other threads:[~2012-01-26 16:35 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-26 16:37 [Qemu-devel] [PULL 00/22] Block patches Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 01/22] rbd: wire up snapshot removal and rollback functionality Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 02/22] block: replace unchecked strdup/malloc/calloc with glib Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 03/22] coroutine: add co_sleep_ns() coroutine sleep function Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 04/22] block: check bdrv_in_use() before blockdev operations Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 05/22] block: make copy-on-read a per-request flag Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 06/22] block: add BlockJob interface for long-running operations Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 07/22] block: add image streaming block job Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 08/22] block: rate-limit streaming operations Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 09/22] qmp: add block_stream command Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 10/22] qmp: add block_job_set_speed command Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 11/22] qmp: add block_job_cancel command Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 12/22] qmp: add query-block-jobs Kevin Wolf
2012-01-26 16:38 ` Kevin Wolf [this message]
2012-01-26 16:38 ` [Qemu-devel] [PATCH 14/22] block: add bdrv_find_backing_image Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 15/22] add QERR_BASE_NOT_FOUND Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 16/22] block: add support for partial streaming Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 17/22] docs: describe live block operations Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 18/22] virtio-blk: add virtio_blk_handle_read trace event Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 19/22] block/vdi: Zero unused parts when allocating a new block (fix #919242) Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 20/22] qcow: Return real error code in qcow_open Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 21/22] qcow: Use bdrv functions to replace file operation Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 22/22] scsi: Guard against buflen exceeding req->cmd.xfer in scsi_disk_emulate_command Kevin Wolf
2012-01-27 17:33 ` [Qemu-devel] [PULL 00/22] Block patches Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1327595896-19623-14-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=anthony@codemonkey.ws \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).