qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 4/4] file-posix: Support auto-read-only option


From: Kevin Wolf
Subject: [Qemu-devel] [PATCH 4/4] file-posix: Support auto-read-only option
Date: Tue, 9 Oct 2018 21:35:24 +0200

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 <address@hidden>
---
 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]