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

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