dcavalca / rpms / qemu

Forked from rpms/qemu a year ago
Clone

Blame 0106-virtiofsd-Fix-data-corruption-with-O_APPEND-write-in.patch

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