From nobody Wed Nov 12 20:45:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1573412726; cv=none; d=zoho.com; s=zohoarc; b=Sk/ymNGg7dlXFs2to5vh+CXeJ+PWtFMycekqwXpL8ZHF74fitVB10g1vuiVrpM6y/pQCjblmUfU87gilO9R0XrWchBMJ12QPsBS4bD6dqWQb4ji3D6vxubQQVMe8LoQjCSh9FgEBKEVGIIa96ir5vSx0kzPJQ4biB/34zwaz98w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573412726; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=bjcX6P4QI80lAIQ0xv/UKJQNZy7VUi4F6zut0VhkWoc=; b=DTU98CNvdKa2yONiT4SkLDL2aWz2SzUwYpnSvBnwqDyMp0pQQjBNLrkMPzvlVgcOOmAtgoiFhGoFakE++CBpMFHuHBLhDG1YSh3BQMM+TTdgWQMlfl3oUrsqycvmaICKjlPphs+UMu8P+DftUjN2H9YrJCsAet4Al2xtA3wc+wg= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573412726642510.0514326541877; Sun, 10 Nov 2019 11:05:26 -0800 (PST) Received: from localhost ([::1]:44694 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iTsWb-0001o8-Ht for importer@patchew.org; Sun, 10 Nov 2019 14:05:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34939) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iTsV3-0000Fl-KD for qemu-devel@nongnu.org; Sun, 10 Nov 2019 14:03:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iTsV2-00037J-D5 for qemu-devel@nongnu.org; Sun, 10 Nov 2019 14:03:49 -0500 Received: from relay.sw.ru ([185.231.240.75]:42170) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iTsUz-00033g-Gv; Sun, 10 Nov 2019 14:03:45 -0500 Received: from [10.94.4.71] (helo=dptest2.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1iTsUw-00076X-3V; Sun, 10 Nov 2019 22:03:42 +0300 From: Denis Plotnikov To: qemu-block@nongnu.org Subject: [PATCH v0 2/2] block: allow to set 'drive' property on a realized block device Date: Sun, 10 Nov 2019 22:03:10 +0300 Message-Id: <20191110190310.19799-3-dplotnikov@virtuozzo.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20191110190310.19799-1-dplotnikov@virtuozzo.com> References: <20191110190310.19799-1-dplotnikov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, berrange@redhat.com, ehabkost@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, pbonzini@redhat.com, den@virtuozzo.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This allows to change (replace) the file on a block device and is useful to workaround exclusive file access restrictions, e.g. to implement VM migration with a shared disk stored on some storage with the exclusive file opening model: a destination VM is started waiting for incomming migration with a fake image drive, and later, on the last migration phase, the fake image file is replaced with the real one. Signed-off-by: Denis Plotnikov --- hw/core/qdev-properties-system.c | 89 +++++++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-sys= tem.c index c534590dcd..aaab1370a4 100644 --- a/hw/core/qdev-properties-system.c +++ b/hw/core/qdev-properties-system.c @@ -79,8 +79,55 @@ static void set_pointer(Object *obj, Visitor *v, Propert= y *prop, =20 /* --- drive --- */ =20 -static void do_parse_drive(DeviceState *dev, const char *str, void **ptr, - const char *propname, bool iothread, Error **er= rp) +static void do_parse_drive_realized(DeviceState *dev, const char *str, + void **ptr, const char *propname, + bool iothread, Error **errp) +{ + BlockBackend *blk =3D *ptr; + BlockDriverState *bs =3D bdrv_lookup_bs(NULL, str, NULL); + int ret; + bool blk_created =3D false; + + if (!bs) { + error_setg(errp, "Can't find blockdev '%s'", str); + return; + } + + if (!blk) { + AioContext *ctx =3D iothread ? bdrv_get_aio_context(bs) : + qemu_get_aio_context(); + blk =3D blk_new(ctx, BLK_PERM_ALL, BLK_PERM_ALL); + blk_created =3D true; + } else { + if (blk_bs(blk)) { + blk_remove_bs(blk); + } + } + + ret =3D blk_insert_bs(blk, bs, errp); + + if (!ret && blk_created) { + if (blk_attach_dev(blk, dev) < 0) { + /* + * Shouldn't be any errors here since we just created + * the new blk because the device doesn't have any. + * Leave the message here in case blk_attach_dev is changed + */ + error_setg(errp, "Can't attach drive '%s' to device '%s'", + str, object_get_typename(OBJECT(dev))); + } else { + *ptr =3D blk; + } + } + + if (blk_created) { + blk_unref(blk); + } +} + +static void do_parse_drive_unrealized(DeviceState *dev, const char *str, + void **ptr, const char *propname, + bool iothread, Error **errp) { BlockBackend *blk; bool blk_created =3D false; @@ -137,18 +184,34 @@ fail: } } =20 -static void parse_drive(DeviceState *dev, const char *str, void **ptr, - const char *propname, Error **errp) -{ - do_parse_drive(dev, str, ptr, propname, false, errp); -} - -static void parse_drive_iothread(DeviceState *dev, const char *str, void *= *ptr, +static void parse_drive_realized(DeviceState *dev, const char *str, void *= *ptr, const char *propname, Error **errp) { - do_parse_drive(dev, str, ptr, propname, true, errp); + do_parse_drive_realized(dev, str, ptr, propname, false, errp); } =20 +static void parse_drive_realized_iothread(DeviceState *dev, const char *st= r, + void **ptr, const char *propname, + Error **errp) +{ + do_parse_drive_realized(dev, str, ptr, propname, true, errp); +} + +static void parse_drive_unrealized(DeviceState *dev, const char *str, + void **ptr, const char *propname, + Error **errp) +{ + do_parse_drive_unrealized(dev, str, ptr, propname, false, errp); +} + +static void parse_drive_unrealized_iothread(DeviceState *dev, const char *= str, + void **ptr, const char *propna= me, + Error **errp) +{ + do_parse_drive_unrealized(dev, str, ptr, propname, true, errp); +} + + static void release_drive(Object *obj, const char *name, void *opaque) { DeviceState *dev =3D DEVICE(obj); @@ -188,13 +251,15 @@ static void get_drive(Object *obj, Visitor *v, const = char *name, void *opaque, static void set_drive(Object *obj, Visitor *v, const char *name, void *opa= que, Error **errp) { - set_pointer(obj, v, opaque, NULL, parse_drive, name, errp); + set_pointer(obj, v, opaque, parse_drive_realized, parse_drive_unrealiz= ed, + name, errp); } =20 static void set_drive_iothread(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) { - set_pointer(obj, v, opaque, NULL, parse_drive_iothread, name, errp); + set_pointer(obj, v, opaque, parse_drive_realized_iothread, + parse_drive_unrealized_iothread, name, errp); } =20 const PropertyInfo qdev_prop_drive =3D { --=20 2.17.0