From nobody Thu Nov 6 00:17:18 2025 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=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539113932631159.97582351950257; Tue, 9 Oct 2018 12:38:52 -0700 (PDT) Received: from localhost ([::1]:53547 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9xqF-0003nT-I8 for importer@patchew.org; Tue, 09 Oct 2018 15:38:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59596) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9xn5-0001WM-Q2 for qemu-devel@nongnu.org; Tue, 09 Oct 2018 15:35:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9xn3-0007Ds-9B for qemu-devel@nongnu.org; Tue, 09 Oct 2018 15:35:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57696) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9xn1-0007Cm-Co; Tue, 09 Oct 2018 15:35:31 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BC932307D84D; Tue, 9 Oct 2018 19:35:30 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-66.ams2.redhat.com [10.36.116.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E431600C2; Tue, 9 Oct 2018 19:35:29 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 9 Oct 2018 21:35:21 +0200 Message-Id: <20181009193524.19052-2-kwolf@redhat.com> In-Reply-To: <20181009193524.19052-1-kwolf@redhat.com> References: <20181009193524.19052-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Tue, 09 Oct 2018 19:35:30 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/4] block: Update flags in bdrv_set_read_only() 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, pkrempa@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" To fully change the read-only state of a node, we must not only change bs->read_only, but also update bs->open_flags. Signed-off-by: Kevin Wolf --- block.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/block.c b/block.c index 0d6e5f1a76..d7bd6d29b4 100644 --- a/block.c +++ b/block.c @@ -281,6 +281,13 @@ int bdrv_set_read_only(BlockDriverState *bs, bool read= _only, Error **errp) } =20 bs->read_only =3D read_only; + + if (read_only) { + bs->open_flags &=3D ~BDRV_O_RDWR; + } else { + bs->open_flags |=3D BDRV_O_RDWR; + } + return 0; } =20 --=20 2.13.6 From nobody Thu Nov 6 00:17:18 2025 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=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539114046279902.1131899217739; Tue, 9 Oct 2018 12:40:46 -0700 (PDT) Received: from localhost ([::1]:53572 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9xru-0005Ry-Bw for importer@patchew.org; Tue, 09 Oct 2018 15:40:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59655) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9xnB-0001b8-MO for qemu-devel@nongnu.org; Tue, 09 Oct 2018 15:35:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9xn7-0007HW-Kp for qemu-devel@nongnu.org; Tue, 09 Oct 2018 15:35:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49648) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9xn3-0007Dg-D1; Tue, 09 Oct 2018 15:35:33 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B2CF9308A95E; Tue, 9 Oct 2018 19:35:32 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-66.ams2.redhat.com [10.36.116.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 15B57600C1; Tue, 9 Oct 2018 19:35:30 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 9 Oct 2018 21:35:22 +0200 Message-Id: <20181009193524.19052-3-kwolf@redhat.com> In-Reply-To: <20181009193524.19052-1-kwolf@redhat.com> References: <20181009193524.19052-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Tue, 09 Oct 2018 19:35:32 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/4] block: Add auto-read-only option 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, pkrempa@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If a management application builds the block graph node by node, the protocol layer doesn't inherit its read-only option from the format layer any more, so it must be set explicitly. Backing files should work on read-only storage, but at the same time, a block job like commit should be able to reopen them read-write if they are on read-write storage. However, without option inheritance, reopen only changes the read-only option for the root node (typically the format layer), but not the protocol layer, so reopening fails (the format layer wants to get write permissions, but the protocol layer is still read-only). A simple workaround for the problem in the management tool would be to open the protocol layer always read-write and to make only the format layer read-only for backing files. However, sometimes the file is actually stored on read-only storage and we don't know whether the image can be opened read-write (for example, for NBD it depends on the server we're trying to connect to). This adds an option that makes QEMU try to open the image read-write, but allows it to degrade to a read-only mode without returning an error. The documentation for this option is consciously phrased in a way that allows QEMU to switch to a better model eventually: Instead of trying when the image is first opened, making the read-only flag dynamic and changing it automatically whenever the first BLK_PERM_WRITE user is attached or the last one is detached would be much more useful behaviour. Unfortunately, this more useful behaviour is also a lot harder to implement, and libvirt needs a solution now before it can switch to -blockdev, so let's start with this easier approach for now. Signed-off-by: Kevin Wolf --- qapi/block-core.json | 6 ++++++ include/block/block.h | 2 ++ block.c | 21 ++++++++++++++++++++- block/vvfat.c | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index cfb37f8c1d..3a899298de 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3651,6 +3651,11 @@ # either generally or in certain configurations. In this c= ase, # the default value does not work and the option must be # specified explicitly. +# @auto-read-only: if true, QEMU may ignore the @read-only option and +# automatically decide whether to open the image read-onl= y or +# read-write (and switch between the modes later), e.g. +# depending on whether the image file is writable or whet= her a +# writing user is attached to the node (default: false). # @detect-zeroes: detect and optimize zero writes (Since 2.1) # (default: off) # @force-share: force share all permission on added nodes. @@ -3666,6 +3671,7 @@ '*discard': 'BlockdevDiscardOptions', '*cache': 'BlockdevCacheOptions', '*read-only': 'bool', + '*auto-read-only': 'bool', '*force-share': 'bool', '*detect-zeroes': 'BlockdevDetectZeroesOptions' }, 'discriminator': 'driver', diff --git a/include/block/block.h b/include/block/block.h index b189cf422e..580b3716c3 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -115,6 +115,7 @@ typedef struct HDGeometry { select an appropriate protocol drive= r, ignoring the format layer */ #define BDRV_O_NO_IO 0x10000 /* don't initialize for I/O */ +#define BDRV_O_AUTO_RDONLY 0x20000 /* degrade to read-only if opening read= -write fails */ =20 #define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_NO_FLUSH) =20 @@ -125,6 +126,7 @@ typedef struct HDGeometry { #define BDRV_OPT_CACHE_DIRECT "cache.direct" #define BDRV_OPT_CACHE_NO_FLUSH "cache.no-flush" #define BDRV_OPT_READ_ONLY "read-only" +#define BDRV_OPT_AUTO_READ_ONLY "auto-read-only" #define BDRV_OPT_DISCARD "discard" #define BDRV_OPT_FORCE_SHARE "force-share" =20 diff --git a/block.c b/block.c index d7bd6d29b4..f999393e28 100644 --- a/block.c +++ b/block.c @@ -930,6 +930,7 @@ static void bdrv_inherited_options(int *child_flags, QD= ict *child_options, =20 /* Inherit the read-only option from the parent if it's not set */ qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY); + qdict_copy_default(child_options, parent_options, BDRV_OPT_AUTO_READ_O= NLY); =20 /* Our block drivers take care to send flushes and respect unmap polic= y, * so we can default to enable both on lower layers regardless of the @@ -1053,6 +1054,7 @@ static void bdrv_backing_options(int *child_flags, QD= ict *child_options, =20 /* backing files always opened read-only */ qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "on"); + qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off"); flags &=3D ~BDRV_O_COPY_ON_READ; =20 /* snapshot=3Don is handled on the top layer */ @@ -1142,6 +1144,10 @@ static void update_flags_from_options(int *flags, Qe= muOpts *opts) *flags |=3D BDRV_O_RDWR; } =20 + assert(qemu_opt_find(opts, BDRV_OPT_AUTO_READ_ONLY)); + if (qemu_opt_get_bool_del(opts, BDRV_OPT_AUTO_READ_ONLY, false)) { + *flags |=3D BDRV_O_AUTO_RDONLY; + } } =20 static void update_options_from_flags(QDict *options, int flags) @@ -1156,6 +1162,10 @@ static void update_options_from_flags(QDict *options= , int flags) if (!qdict_haskey(options, BDRV_OPT_READ_ONLY)) { qdict_put_bool(options, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR)= ); } + if (!qdict_haskey(options, BDRV_OPT_AUTO_READ_ONLY)) { + qdict_put_bool(options, BDRV_OPT_AUTO_READ_ONLY, + flags & BDRV_O_AUTO_RDONLY); + } } =20 static void bdrv_assign_node_name(BlockDriverState *bs, @@ -1329,6 +1339,11 @@ QemuOptsList bdrv_runtime_opts =3D { .help =3D "Node is opened in read-only mode", }, { + .name =3D BDRV_OPT_AUTO_READ_ONLY, + .type =3D QEMU_OPT_BOOL, + .help =3D "Node can become read-only if opening read-write fai= ls", + }, + { .name =3D "detect-zeroes", .type =3D QEMU_OPT_STRING, .help =3D "try to optimize zero writes (off, on, unmap)", @@ -1430,7 +1445,9 @@ 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) { + if ((bs->open_flags & (BDRV_O_RDWR | BDRV_O_AUTO_RDONLY)) + =3D=3D BDRV_O_RDWR) + { bdrv_enable_copy_on_read(bs); } else { error_setg(errp, "Can't use copy-on-read on read-only device"); @@ -2486,6 +2503,8 @@ BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef = *ref, Error **errp) qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off"); qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off"); qdict_set_default_str(qdict, BDRV_OPT_READ_ONLY, "off"); + qdict_set_default_str(qdict, BDRV_OPT_AUTO_READ_ONLY, "off"); + } =20 bs =3D bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, errp); diff --git a/block/vvfat.c b/block/vvfat.c index f2e7d501cf..98ba5e2bac 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -3130,6 +3130,7 @@ static void vvfat_qcow_options(int *child_flags, QDic= t *child_options, int parent_flags, QDict *parent_options) { qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "off"); + qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off"); qdict_set_default_str(child_options, BDRV_OPT_CACHE_NO_FLUSH, "on"); } =20 --=20 2.13.6 From nobody Thu Nov 6 00:17:18 2025 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=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539113908663890.7902406564248; Tue, 9 Oct 2018 12:38:28 -0700 (PDT) Received: from localhost ([::1]:53545 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9xpr-0003SC-Je for importer@patchew.org; Tue, 09 Oct 2018 15:38:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59651) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9xnB-0001b5-Kb for qemu-devel@nongnu.org; Tue, 09 Oct 2018 15:35:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9xn7-0007Hb-MF for qemu-devel@nongnu.org; Tue, 09 Oct 2018 15:35:39 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44454) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9xn5-0007FT-5o; Tue, 09 Oct 2018 15:35:35 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7CB675F732; Tue, 9 Oct 2018 19:35:34 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-66.ams2.redhat.com [10.36.116.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 09B10600C1; Tue, 9 Oct 2018 19:35:32 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 9 Oct 2018 21:35:23 +0200 Message-Id: <20181009193524.19052-4-kwolf@redhat.com> In-Reply-To: <20181009193524.19052-1-kwolf@redhat.com> References: <20181009193524.19052-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 09 Oct 2018 19:35:34 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/4] nbd: Support auto-read-only option 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, pkrempa@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If read-only=3Doff, but auto-read-only=3Don are given, open a read-write NBD connection if the server provides a read-write export, but instead of erroring out for read-only exports, just degrade to read-only. Signed-off-by: Kevin Wolf --- block/nbd-client.h | 1 + block/nbd-client.c | 14 +++++++++++--- block/nbd.c | 4 +++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/block/nbd-client.h b/block/nbd-client.h index cfc90550b9..1d14e78e92 100644 --- a/block/nbd-client.h +++ b/block/nbd-client.h @@ -46,6 +46,7 @@ int nbd_client_init(BlockDriverState *bs, QCryptoTLSCreds *tlscreds, const char *hostname, const char *x_dirty_bitmap, + bool auto_readonly, Error **errp); void nbd_client_close(BlockDriverState *bs); =20 diff --git a/block/nbd-client.c b/block/nbd-client.c index 9686ecbd5e..6a1e634bec 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -971,6 +971,7 @@ int nbd_client_init(BlockDriverState *bs, QCryptoTLSCreds *tlscreds, const char *hostname, const char *x_dirty_bitmap, + bool auto_readonly, Error **errp) { NBDClientSession *client =3D nbd_get_client_session(bs); @@ -994,9 +995,16 @@ int nbd_client_init(BlockDriverState *bs, } if (client->info.flags & NBD_FLAG_READ_ONLY && !bdrv_is_read_only(bs)) { - error_setg(errp, - "request for write access conflicts with read-only expo= rt"); - return -EACCES; + if (auto_readonly) { + ret =3D bdrv_set_read_only(bs, true, errp); + if (ret < 0) { + return ret; + } + } else { + error_setg(errp, "request for write access conflicts with " + "read-only export"); + return -EACCES; + } } if (client->info.flags & NBD_FLAG_SEND_FUA) { bs->supported_write_flags =3D BDRV_REQ_FUA; diff --git a/block/nbd.c b/block/nbd.c index e87699fb73..8c352a45cd 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -445,7 +445,9 @@ static int nbd_open(BlockDriverState *bs, QDict *option= s, int flags, =20 /* NBD handshake */ ret =3D nbd_client_init(bs, sioc, s->export, tlscreds, hostname, - qemu_opt_get(opts, "x-dirty-bitmap"), errp); + qemu_opt_get(opts, "x-dirty-bitmap"), + (flags & BDRV_O_AUTO_RDONLY), + errp); error: if (sioc) { object_unref(OBJECT(sioc)); --=20 2.13.6 From nobody Thu Nov 6 00:17:18 2025 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=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539113862807698.4632635983907; Tue, 9 Oct 2018 12:37:42 -0700 (PDT) Received: from localhost ([::1]:53543 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9xp7-0002md-KM for importer@patchew.org; Tue, 09 Oct 2018 15:37:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59692) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9xnG-0001fI-0G for qemu-devel@nongnu.org; Tue, 09 Oct 2018 15:35:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9xnD-0007K0-Ep for qemu-devel@nongnu.org; Tue, 09 Oct 2018 15:35:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51076) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9xn6-0007Gl-Vl; Tue, 09 Oct 2018 15:35:37 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 411A63083390; Tue, 9 Oct 2018 19:35:36 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-66.ams2.redhat.com [10.36.116.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id C6D15600C1; Tue, 9 Oct 2018 19:35:34 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 9 Oct 2018 21:35:24 +0200 Message-Id: <20181009193524.19052-5-kwolf@redhat.com> In-Reply-To: <20181009193524.19052-1-kwolf@redhat.com> References: <20181009193524.19052-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Tue, 09 Oct 2018 19:35:36 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 4/4] file-posix: Support auto-read-only option 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, pkrempa@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If read-only=3Doff, but auto-read-only=3Don are given, open the file read-write if we have the permissions, but instead of erroring out for read-only files, just degrade to read-only. Signed-off-by: Kevin Wolf --- block/file-posix.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index 2da3a76355..69b312a3a3 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -450,6 +450,7 @@ static int raw_open_common(BlockDriverState *bs, QDict = *options, int fd, ret; struct stat st; OnOffAuto locking; + bool auto_readonly =3D bdrv_flags & BDRV_O_AUTO_RDONLY; =20 opts =3D qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); qemu_opts_absorb_qdict(opts, options, &local_err); @@ -527,6 +528,18 @@ static int raw_open_common(BlockDriverState *bs, QDict= *options, =20 s->fd =3D -1; fd =3D qemu_open(filename, s->open_flags, 0644); + + if (auto_readonly && fd < 0 && (errno =3D=3D EACCES || errno =3D=3D ER= OFS)) { + ret =3D bdrv_set_read_only(bs, true, errp); + if (ret < 0) { + goto fail; + } + bdrv_flags &=3D ~BDRV_O_RDWR; + raw_parse_flags(bdrv_flags, &s->open_flags); + assert(!(s->open_flags & O_CREAT)); + fd =3D qemu_open(filename, s->open_flags); + } + if (fd < 0) { ret =3D -errno; error_setg_errno(errp, errno, "Could not open '%s'", filename); --=20 2.13.6