qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/4] block: Add auto-read-only option
@ 2018-10-09 19:35 Kevin Wolf
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 1/4] block: Update flags in bdrv_set_read_only() Kevin Wolf
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Kevin Wolf @ 2018-10-09 19:35 UTC (permalink / raw)
  To: qemu-block; +Cc: kwolf, mreitz, eblake, pkrempa, qemu-devel

Peter, would this provide what libvirt urgently needs for backing files
vs. the commit block job?

Kevin Wolf (4):
  block: Update flags in bdrv_set_read_only()
  block: Add auto-read-only option
  nbd: Support auto-read-only option
  file-posix: Support auto-read-only option

 qapi/block-core.json  |  6 ++++++
 block/nbd-client.h    |  1 +
 include/block/block.h |  2 ++
 block.c               | 28 +++++++++++++++++++++++++++-
 block/file-posix.c    | 13 +++++++++++++
 block/nbd-client.c    | 14 +++++++++++---
 block/nbd.c           |  4 +++-
 block/vvfat.c         |  1 +
 8 files changed, 64 insertions(+), 5 deletions(-)

-- 
2.13.6

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Qemu-devel] [PATCH 1/4] block: Update flags in bdrv_set_read_only()
  2018-10-09 19:35 [Qemu-devel] [PATCH 0/4] block: Add auto-read-only option Kevin Wolf
@ 2018-10-09 19:35 ` Kevin Wolf
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 2/4] block: Add auto-read-only option Kevin Wolf
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Kevin Wolf @ 2018-10-09 19:35 UTC (permalink / raw)
  To: qemu-block; +Cc: kwolf, mreitz, eblake, pkrempa, qemu-devel

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 <kwolf@redhat.com>
---
 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)
     }
 
     bs->read_only = read_only;
+
+    if (read_only) {
+        bs->open_flags &= ~BDRV_O_RDWR;
+    } else {
+        bs->open_flags |= BDRV_O_RDWR;
+    }
+
     return 0;
 }
 
-- 
2.13.6

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [Qemu-devel] [PATCH 2/4] block: Add auto-read-only option
  2018-10-09 19:35 [Qemu-devel] [PATCH 0/4] block: Add auto-read-only option Kevin Wolf
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 1/4] block: Update flags in bdrv_set_read_only() Kevin Wolf
@ 2018-10-09 19:35 ` Kevin Wolf
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 3/4] nbd: Support " Kevin Wolf
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Kevin Wolf @ 2018-10-09 19:35 UTC (permalink / raw)
  To: qemu-block; +Cc: kwolf, mreitz, eblake, pkrempa, qemu-devel

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 <kwolf@redhat.com>
---
 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 case,
 #                 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-only or
+#                  read-write (and switch between the modes later), e.g.
+#                  depending on whether the image file is writable or whether 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 driver,
                                       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 */
 
 #define BDRV_O_CACHE_MASK  (BDRV_O_NOCACHE | BDRV_O_NO_FLUSH)
 
@@ -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"
 
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, QDict *child_options,
 
     /* 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_ONLY);
 
     /* Our block drivers take care to send flushes and respect unmap policy,
      * 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, QDict *child_options,
 
     /* 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 &= ~BDRV_O_COPY_ON_READ;
 
     /* snapshot=on is handled on the top layer */
@@ -1142,6 +1144,10 @@ static void update_flags_from_options(int *flags, QemuOpts *opts)
         *flags |= BDRV_O_RDWR;
     }
 
+    assert(qemu_opt_find(opts, BDRV_OPT_AUTO_READ_ONLY));
+    if (qemu_opt_get_bool_del(opts, BDRV_OPT_AUTO_READ_ONLY, false)) {
+        *flags |= BDRV_O_AUTO_RDONLY;
+    }
 }
 
 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);
+    }
 }
 
 static void bdrv_assign_node_name(BlockDriverState *bs,
@@ -1329,6 +1339,11 @@ QemuOptsList bdrv_runtime_opts = {
             .help = "Node is opened in read-only mode",
         },
         {
+            .name = BDRV_OPT_AUTO_READ_ONLY,
+            .type = QEMU_OPT_BOOL,
+            .help = "Node can become read-only if opening read-write fails",
+        },
+        {
             .name = "detect-zeroes",
             .type = QEMU_OPT_STRING,
             .help = "try to optimize zero writes (off, on, unmap)",
@@ -1430,7 +1445,9 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
     assert(atomic_read(&bs->copy_on_read) == 0);
 
     if (bs->open_flags & BDRV_O_COPY_ON_READ) {
-        if (!bs->read_only) {
+        if ((bs->open_flags & (BDRV_O_RDWR | BDRV_O_AUTO_RDONLY))
+            == 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");
+
     }
 
     bs = 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, QDict *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");
 }
 
-- 
2.13.6

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [Qemu-devel] [PATCH 3/4] nbd: Support auto-read-only option
  2018-10-09 19:35 [Qemu-devel] [PATCH 0/4] block: Add auto-read-only option Kevin Wolf
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 1/4] block: Update flags in bdrv_set_read_only() Kevin Wolf
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 2/4] block: Add auto-read-only option Kevin Wolf
@ 2018-10-09 19:35 ` Kevin Wolf
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 4/4] file-posix: " Kevin Wolf
  2018-10-11  8:29 ` [Qemu-devel] [PATCH 0/4] block: Add " Peter Krempa
  4 siblings, 0 replies; 6+ messages in thread
From: Kevin Wolf @ 2018-10-09 19:35 UTC (permalink / raw)
  To: qemu-block; +Cc: kwolf, mreitz, eblake, pkrempa, qemu-devel

If read-only=off, but auto-read-only=on 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 <kwolf@redhat.com>
---
 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);
 
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 = 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 export");
-        return -EACCES;
+        if (auto_readonly) {
+            ret = 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 = 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 *options, int flags,
 
     /* NBD handshake */
     ret = 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));
-- 
2.13.6

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [Qemu-devel] [PATCH 4/4] file-posix: Support auto-read-only option
  2018-10-09 19:35 [Qemu-devel] [PATCH 0/4] block: Add auto-read-only option Kevin Wolf
                   ` (2 preceding siblings ...)
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 3/4] nbd: Support " Kevin Wolf
@ 2018-10-09 19:35 ` Kevin Wolf
  2018-10-11  8:29 ` [Qemu-devel] [PATCH 0/4] block: Add " Peter Krempa
  4 siblings, 0 replies; 6+ messages in thread
From: Kevin Wolf @ 2018-10-09 19:35 UTC (permalink / raw)
  To: qemu-block; +Cc: kwolf, mreitz, eblake, pkrempa, qemu-devel

If read-only=off, but auto-read-only=on 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 <kwolf@redhat.com>
---
 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 = bdrv_flags & BDRV_O_AUTO_RDONLY;
 
     opts = 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,
 
     s->fd = -1;
     fd = qemu_open(filename, s->open_flags, 0644);
+
+    if (auto_readonly && fd < 0 && (errno == EACCES || errno == EROFS)) {
+        ret = bdrv_set_read_only(bs, true, errp);
+        if (ret < 0) {
+            goto fail;
+        }
+        bdrv_flags &= ~BDRV_O_RDWR;
+        raw_parse_flags(bdrv_flags, &s->open_flags);
+        assert(!(s->open_flags & O_CREAT));
+        fd = qemu_open(filename, s->open_flags);
+    }
+
     if (fd < 0) {
         ret = -errno;
         error_setg_errno(errp, errno, "Could not open '%s'", filename);
-- 
2.13.6

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH 0/4] block: Add auto-read-only option
  2018-10-09 19:35 [Qemu-devel] [PATCH 0/4] block: Add auto-read-only option Kevin Wolf
                   ` (3 preceding siblings ...)
  2018-10-09 19:35 ` [Qemu-devel] [PATCH 4/4] file-posix: " Kevin Wolf
@ 2018-10-11  8:29 ` Peter Krempa
  4 siblings, 0 replies; 6+ messages in thread
From: Peter Krempa @ 2018-10-11  8:29 UTC (permalink / raw)
  To: Kevin Wolf; +Cc: qemu-block, mreitz, eblake, qemu-devel

[-- Attachment #1: Type: text/plain, Size: 745 bytes --]

On Tue, Oct 09, 2018 at 21:35:20 +0200, Kevin Wolf wrote:
> Peter, would this provide what libvirt urgently needs for backing files
> vs. the commit block job?

This looks fine for us with one exception. I'd prefer if the curl driver
implemented this too so that we don't have to hardcode the readonly flag
for that as well.

With CURL based backend backing an image I get:

-blockdev {"driver":"http","url":"example.com","node-name":"libvirt-2-storage","discard":"unmap","auto-read-only":true}: curl block device does not support writes

I've tested it with libvirt's usage of -blockdev together with
block-commit and also with read-only-NBD backed images and everything
seems to work well for me. I'll report back once I give it more testing.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-10-11  8:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-09 19:35 [Qemu-devel] [PATCH 0/4] block: Add auto-read-only option Kevin Wolf
2018-10-09 19:35 ` [Qemu-devel] [PATCH 1/4] block: Update flags in bdrv_set_read_only() Kevin Wolf
2018-10-09 19:35 ` [Qemu-devel] [PATCH 2/4] block: Add auto-read-only option Kevin Wolf
2018-10-09 19:35 ` [Qemu-devel] [PATCH 3/4] nbd: Support " Kevin Wolf
2018-10-09 19:35 ` [Qemu-devel] [PATCH 4/4] file-posix: " Kevin Wolf
2018-10-11  8:29 ` [Qemu-devel] [PATCH 0/4] block: Add " Peter Krempa

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).