26ba25
From 8aff951dc5acb2965131e43e10b1cd9fce17992e Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Thu, 10 Jan 2019 12:44:36 +0000
26ba25
Subject: [PATCH 06/14] file-posix: Support auto-read-only option
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20190110124442.30132-7-kwolf@redhat.com>
26ba25
Patchwork-id: 83955
26ba25
O-Subject: [RHEL-8.0 qemu-kvm PATCH 06/12] file-posix: Support auto-read-only option
26ba25
Bugzilla: 1644996
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Eric Blake <eblake@redhat.com>
26ba25
26ba25
If read-only=off, but auto-read-only=on is given, open the file
26ba25
read-write if we have the permissions, but instead of erroring out for
26ba25
read-only files, just degrade to read-only.
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Reviewed-by: Eric Blake <eblake@redhat.com>
26ba25
(cherry picked from commit 64107dc044a54ebe46348ac0fe87584be2eb3e81)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 block/file-posix.c | 19 ++++++++++++++++---
26ba25
 1 file changed, 16 insertions(+), 3 deletions(-)
26ba25
26ba25
diff --git a/block/file-posix.c b/block/file-posix.c
26ba25
index c12cdb7..7e6869d 100644
26ba25
--- a/block/file-posix.c
26ba25
+++ b/block/file-posix.c
26ba25
@@ -517,9 +517,22 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
26ba25
 
26ba25
     s->fd = -1;
26ba25
     fd = qemu_open(filename, s->open_flags, 0644);
26ba25
-    if (fd < 0) {
26ba25
-        ret = -errno;
26ba25
-        error_setg_errno(errp, errno, "Could not open '%s'", filename);
26ba25
+    ret = fd < 0 ? -errno : 0;
26ba25
+
26ba25
+    if (ret == -EACCES || ret == -EROFS) {
26ba25
+        /* Try to degrade to read-only, but if it doesn't work, still use the
26ba25
+         * normal error message. */
26ba25
+        if (bdrv_apply_auto_read_only(bs, NULL, NULL) == 0) {
26ba25
+            bdrv_flags &= ~BDRV_O_RDWR;
26ba25
+            raw_parse_flags(bdrv_flags, &s->open_flags);
26ba25
+            assert(!(s->open_flags & O_CREAT));
26ba25
+            fd = qemu_open(filename, s->open_flags);
26ba25
+            ret = fd < 0 ? -errno : 0;
26ba25
+        }
26ba25
+    }
26ba25
+
26ba25
+    if (ret < 0) {
26ba25
+        error_setg_errno(errp, -ret, "Could not open '%s'", filename);
26ba25
         if (ret == -EROFS) {
26ba25
             ret = -EACCES;
26ba25
         }
26ba25
-- 
26ba25
1.8.3.1
26ba25