Blame SOURCES/kvm-virtiofsd-Fix-data-corruption-with-O_APPEND-write-in.patch

ddf19c
From 9b5fbc95a287b2ce9448142194b161d8360d5e4e Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:02:15 +0100
ddf19c
Subject: [PATCH 104/116] virtiofsd: Fix data corruption with O_APPEND write in
ddf19c
 writeback mode
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-id: <20200127190227.40942-101-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93556
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 100/112] virtiofsd: Fix data corruption with O_APPEND write in writeback mode
ddf19c
Bugzilla: 1694164
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
ddf19c
From: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
ddf19c
ddf19c
When writeback mode is enabled (-o writeback), O_APPEND handling is
ddf19c
done in kernel. Therefore virtiofsd clears O_APPEND flag when open.
ddf19c
Otherwise O_APPEND flag takes precedence over pwrite() and write
ddf19c
data may corrupt.
ddf19c
ddf19c
Currently clearing O_APPEND flag is done in lo_open(), but we also
ddf19c
need the same operation in lo_create(). So, factor out the flag
ddf19c
update operation in lo_open() to update_open_flags() and call it
ddf19c
in both lo_open() and lo_create().
ddf19c
ddf19c
This fixes the failure of xfstest generic/069 in writeback mode
ddf19c
(which tests O_APPEND write data integrity).
ddf19c
ddf19c
Signed-off-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
ddf19c
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 8e4e41e39eac5ee5f378d66f069a2f70a1734317)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/passthrough_ll.c | 66 ++++++++++++++++++++--------------------
ddf19c
 1 file changed, 33 insertions(+), 33 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
ddf19c
index 948cb19..4c61ac5 100644
ddf19c
--- a/tools/virtiofsd/passthrough_ll.c
ddf19c
+++ b/tools/virtiofsd/passthrough_ll.c
ddf19c
@@ -1692,6 +1692,37 @@ static void lo_releasedir(fuse_req_t req, fuse_ino_t ino,
ddf19c
     fuse_reply_err(req, 0);
ddf19c
 }
ddf19c
 
ddf19c
+static void update_open_flags(int writeback, struct fuse_file_info *fi)
ddf19c
+{
ddf19c
+    /*
ddf19c
+     * With writeback cache, kernel may send read requests even
ddf19c
+     * when userspace opened write-only
ddf19c
+     */
ddf19c
+    if (writeback && (fi->flags & O_ACCMODE) == O_WRONLY) {
ddf19c
+        fi->flags &= ~O_ACCMODE;
ddf19c
+        fi->flags |= O_RDWR;
ddf19c
+    }
ddf19c
+
ddf19c
+    /*
ddf19c
+     * With writeback cache, O_APPEND is handled by the kernel.
ddf19c
+     * This breaks atomicity (since the file may change in the
ddf19c
+     * underlying filesystem, so that the kernel's idea of the
ddf19c
+     * end of the file isn't accurate anymore). In this example,
ddf19c
+     * we just accept that. A more rigorous filesystem may want
ddf19c
+     * to return an error here
ddf19c
+     */
ddf19c
+    if (writeback && (fi->flags & O_APPEND)) {
ddf19c
+        fi->flags &= ~O_APPEND;
ddf19c
+    }
ddf19c
+
ddf19c
+    /*
ddf19c
+     * O_DIRECT in guest should not necessarily mean bypassing page
ddf19c
+     * cache on host as well. If somebody needs that behavior, it
ddf19c
+     * probably should be a configuration knob in daemon.
ddf19c
+     */
ddf19c
+    fi->flags &= ~O_DIRECT;
ddf19c
+}
ddf19c
+
ddf19c
 static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
                       mode_t mode, struct fuse_file_info *fi)
ddf19c
 {
ddf19c
@@ -1721,12 +1752,7 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
         goto out;
ddf19c
     }
ddf19c
 
ddf19c
-    /*
ddf19c
-     * O_DIRECT in guest should not necessarily mean bypassing page
ddf19c
-     * cache on host as well. If somebody needs that behavior, it
ddf19c
-     * probably should be a configuration knob in daemon.
ddf19c
-     */
ddf19c
-    fi->flags &= ~O_DIRECT;
ddf19c
+    update_open_flags(lo->writeback, fi);
ddf19c
 
ddf19c
     fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
ddf19c
                 mode);
ddf19c
@@ -1936,33 +1962,7 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
ddf19c
     fuse_log(FUSE_LOG_DEBUG, "lo_open(ino=%" PRIu64 ", flags=%d)\n", ino,
ddf19c
              fi->flags);
ddf19c
 
ddf19c
-    /*
ddf19c
-     * With writeback cache, kernel may send read requests even
ddf19c
-     * when userspace opened write-only
ddf19c
-     */
ddf19c
-    if (lo->writeback && (fi->flags & O_ACCMODE) == O_WRONLY) {
ddf19c
-        fi->flags &= ~O_ACCMODE;
ddf19c
-        fi->flags |= O_RDWR;
ddf19c
-    }
ddf19c
-
ddf19c
-    /*
ddf19c
-     * With writeback cache, O_APPEND is handled by the kernel.
ddf19c
-     * This breaks atomicity (since the file may change in the
ddf19c
-     * underlying filesystem, so that the kernel's idea of the
ddf19c
-     * end of the file isn't accurate anymore). In this example,
ddf19c
-     * we just accept that. A more rigorous filesystem may want
ddf19c
-     * to return an error here
ddf19c
-     */
ddf19c
-    if (lo->writeback && (fi->flags & O_APPEND)) {
ddf19c
-        fi->flags &= ~O_APPEND;
ddf19c
-    }
ddf19c
-
ddf19c
-    /*
ddf19c
-     * O_DIRECT in guest should not necessarily mean bypassing page
ddf19c
-     * cache on host as well. If somebody needs that behavior, it
ddf19c
-     * probably should be a configuration knob in daemon.
ddf19c
-     */
ddf19c
-    fi->flags &= ~O_DIRECT;
ddf19c
+    update_open_flags(lo->writeback, fi);
ddf19c
 
ddf19c
     sprintf(buf, "%i", lo_fd(req, ino));
ddf19c
     fd = openat(lo->proc_self_fd, buf, fi->flags & ~O_NOFOLLOW);
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c