From nobody Fri Apr 19 08:06:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528905166299442.9539471211748; Wed, 13 Jun 2018 08:52:46 -0700 (PDT) Received: from localhost ([::1]:35283 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fT84j-00060u-EP for importer@patchew.org; Wed, 13 Jun 2018 11:52:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41498) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fT7zf-0002ba-16 for qemu-devel@nongnu.org; Wed, 13 Jun 2018 11:47:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fT7zd-0000A3-Vd for qemu-devel@nongnu.org; Wed, 13 Jun 2018 11:47:31 -0400 Received: from relay.sw.ru ([195.214.232.25]:41056) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fT7zb-0008S8-Ho; Wed, 13 Jun 2018 11:47:27 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=dptest2.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fT7zS-0006RM-C7; Wed, 13 Jun 2018 18:47:18 +0300 From: Denis Plotnikov To: qemu-block@nongnu.org Date: Wed, 13 Jun 2018 18:47:10 +0300 Message-Id: <20180613154711.12977-2-dplotnikov@virtuozzo.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180613154711.12977-1-dplotnikov@virtuozzo.com> References: <20180613154711.12977-1-dplotnikov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH v0 1/2] block: check for read-only on copy-on-read setting X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, famz@redhat.com, den@virtuozzo.com, qemu-devel@nongnu.org, jcody@redhat.com, armbru@redhat.com, mreitz@redhat.com, rkagan@virtuozzo.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We should always check whether block device is in the read-only state before enabling copy-on-read. The patch embeds the check in the copy-on-read setter. Signed-off-by: Denis Plotnikov --- block.c | 4 +--- block/io.c | 10 ++++++++-- block/stream.c | 5 +++-- include/block/block.h | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/block.c b/block.c index 50887087f3..e73fccd397 100644 --- a/block.c +++ b/block.c @@ -1370,9 +1370,7 @@ static int bdrv_open_common(BlockDriverState *bs, Blo= ckBackend *file, assert(atomic_read(&bs->copy_on_read) =3D=3D 0); =20 if (bs->open_flags & BDRV_O_COPY_ON_READ) { - if (!bs->read_only) { - bdrv_enable_copy_on_read(bs); - } else { + if (!bdrv_enable_copy_on_read(bs)) { error_setg(errp, "Can't use copy-on-read on read-only device"); ret =3D -EINVAL; goto fail_opts; diff --git a/block/io.c b/block/io.c index b7beaeeb9f..6ec008dbdc 100644 --- a/block/io.c +++ b/block/io.c @@ -131,9 +131,15 @@ void bdrv_refresh_limits(BlockDriverState *bs, Error *= *errp) * use the feature without worrying about clobbering its previous state. * Copy-on-read stays enabled until all users have called to disable it. */ -void bdrv_enable_copy_on_read(BlockDriverState *bs) +bool bdrv_enable_copy_on_read(BlockDriverState *bs) { - atomic_inc(&bs->copy_on_read); + if (bs->read_only) { + return false; + } else { + atomic_inc(&bs->copy_on_read); + } + + return true; } =20 void bdrv_disable_copy_on_read(BlockDriverState *bs) diff --git a/block/stream.c b/block/stream.c index 9264b68a1e..033e24f22c 100644 --- a/block/stream.c +++ b/block/stream.c @@ -130,8 +130,9 @@ static void coroutine_fn stream_run(void *opaque) * backing chain since the copy-on-read operation does not take base i= nto * account. */ - if (!base) { - bdrv_enable_copy_on_read(bs); + if (!base && !bdrv_enable_copy_on_read(bs)) { + ret =3D -EPERM; + goto out; } =20 for ( ; offset < len; offset +=3D n) { diff --git a/include/block/block.h b/include/block/block.h index e677080c4e..dddfd032f9 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -490,7 +490,7 @@ void *qemu_try_blockalign(BlockDriverState *bs, size_t = size); void *qemu_try_blockalign0(BlockDriverState *bs, size_t size); bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov); =20 -void bdrv_enable_copy_on_read(BlockDriverState *bs); +bool bdrv_enable_copy_on_read(BlockDriverState *bs); void bdrv_disable_copy_on_read(BlockDriverState *bs); =20 void bdrv_ref(BlockDriverState *bs); --=20 2.17.0 From nobody Fri Apr 19 08:06:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528904993611400.08740451908204; Wed, 13 Jun 2018 08:49:53 -0700 (PDT) Received: from localhost ([::1]:35262 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fT81n-0003jQ-2w for importer@patchew.org; Wed, 13 Jun 2018 11:49:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41500) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fT7zf-0002br-5z for qemu-devel@nongnu.org; Wed, 13 Jun 2018 11:47:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fT7ze-0000Ah-80 for qemu-devel@nongnu.org; Wed, 13 Jun 2018 11:47:31 -0400 Received: from relay.sw.ru ([195.214.232.25]:41052) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fT7zb-0008S9-HM; Wed, 13 Jun 2018 11:47:27 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=dptest2.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fT7zS-0006RM-Gh; Wed, 13 Jun 2018 18:47:18 +0300 From: Denis Plotnikov To: qemu-block@nongnu.org Date: Wed, 13 Jun 2018 18:47:11 +0300 Message-Id: <20180613154711.12977-3-dplotnikov@virtuozzo.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180613154711.12977-1-dplotnikov@virtuozzo.com> References: <20180613154711.12977-1-dplotnikov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 195.214.232.25 Subject: [Qemu-devel] [PATCH v0 2/2] qmp: add block-set-copy-on-read command X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, famz@redhat.com, den@virtuozzo.com, qemu-devel@nongnu.org, jcody@redhat.com, armbru@redhat.com, mreitz@redhat.com, rkagan@virtuozzo.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The command enables/disables copy-on-read mode for VM's disk while VM is running. This is needed when using external disk readers to shape access pattern to the disk backend. Signed-off-by: Denis Plotnikov --- blockdev.c | 38 ++++++++++++++++++++++++++++++++++++++ qapi/block-core.json | 20 ++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/blockdev.c b/blockdev.c index 4862323012..4a297dabef 100644 --- a/blockdev.c +++ b/blockdev.c @@ -4276,6 +4276,44 @@ out: aio_context_release(aio_context); } =20 +void qmp_block_set_copy_on_read(const char *device, bool enable, Error **e= rrp) +{ + Error *local_err =3D NULL; + AioContext *aio_context; + BlockDriverState *bs =3D bdrv_lookup_bs(device, device, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + aio_context =3D bdrv_get_aio_context(bs); + aio_context_acquire(aio_context); + + if (enable) { + if (bs->open_flags & BDRV_O_COPY_ON_READ) { + error_setg(errp, "Can't enable copy-on-read for the device. " + "Copy-on-read is already enabled."); + } else { + if (bdrv_enable_copy_on_read(bs)) { + bs->open_flags |=3D BDRV_O_COPY_ON_READ; + } else { + error_setg(errp, "Can't enable copy-on-read. " + "The device is read-only."); + } + } + } else { + if (bs->open_flags & BDRV_O_COPY_ON_READ) { + bs->open_flags &=3D ~BDRV_O_COPY_ON_READ; + bdrv_disable_copy_on_read(bs); + } else { + error_setg(errp, "Can't disable copy-on-read for the device. " + "Copy-on-read is already disabled."); + } + } + + aio_context_release(aio_context); +} + static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, const char *child_name) { diff --git a/qapi/block-core.json b/qapi/block-core.json index fff23fc82b..7369a13009 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -4701,6 +4701,26 @@ { 'command': 'block-set-write-threshold', 'data': { 'node-name': 'str', 'write-threshold': 'uint64' } } =20 +## +# @block-set-copy-on-read: +# +# Enables and disables the copy-on-read property of a block device. +# +# @device: device or graph node name on which copy-on-read must be set. +# +# Since: 2.12 +# +# Example: +# +# -> { "execute": "block-set-copy-on-read", +# "arguments": { "device": "scsi0-0-0-0", +# "enable": true } } +# <- { "return": {} } +# +## +{ 'command': 'block-set-copy-on-read', + 'data': { 'device': 'str', 'enable': 'bool' } } + ## # @x-blockdev-change: # --=20 2.17.0