Blame SOURCES/e2fsprogs-1.45.6-mmp-do-not-use-O_DIRECT-when-working-with-regular-fi.patch

a77133
From d80424f5ceaa3a7a8ce7dbf6bacca32f2f05fdeb Mon Sep 17 00:00:00 2001
a77133
From: Lukas Czerner <lczerner@redhat.com>
a77133
Date: Thu, 18 Feb 2021 10:51:46 +0100
a77133
Subject: [PATCH 23/46] mmp: do not use O_DIRECT when working with regular file
a77133
Content-Type: text/plain
a77133
a77133
Currently the mmp block is read using O_DIRECT to avoid any caching that
a77133
may be done by the VM. However when working with regular files this
a77133
creates alignment issues when the device of the host file system has
a77133
sector size larger than the blocksize of the file system in the file
a77133
we're working with.
a77133
a77133
This can be reproduced with t_mmp_fail test when run on the device with
a77133
4k sector size because the mke2fs fails when trying to read the mmp
a77133
block.
a77133
a77133
Fix it by disabling O_DIRECT when working with regular files. I don't
a77133
think there is any risk of doing so since the file system layer, unlike
a77133
shared block device, should guarantee cache consistency.
a77133
a77133
Signed-off-by: Lukas Czerner <lczerner@redhat.com>
a77133
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
a77133
Reviewed-by: Andreas Dilger <adilger@dilger.ca>
a77133
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
a77133
---
a77133
 lib/ext2fs/mmp.c | 22 +++++++++++-----------
a77133
 1 file changed, 11 insertions(+), 11 deletions(-)
a77133
a77133
diff --git a/lib/ext2fs/mmp.c b/lib/ext2fs/mmp.c
a77133
index eddc66a7..626fe395 100644
a77133
--- a/lib/ext2fs/mmp.c
a77133
+++ b/lib/ext2fs/mmp.c
a77133
@@ -57,21 +57,21 @@ errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf)
a77133
 	 * regardless of how the io_manager is doing reads, to avoid caching of
a77133
 	 * the MMP block by the io_manager or the VM.  It needs to be fresh. */
a77133
 	if (fs->mmp_fd <= 0) {
a77133
+		struct stat st;
a77133
 		int flags = O_RDWR | O_DIRECT;
a77133
 
a77133
-retry:
a77133
+		/*
a77133
+		 * There is no reason for using O_DIRECT if we're working with
a77133
+		 * regular file. Disabling it also avoids problems with
a77133
+		 * alignment when the device of the host file system has sector
a77133
+		 * size larger than blocksize of the fs we're working with.
a77133
+		 */
a77133
+		if (stat(fs->device_name, &st) == 0 &&
a77133
+		    S_ISREG(st.st_mode))
a77133
+			flags &= ~O_DIRECT;
a77133
+
a77133
 		fs->mmp_fd = open(fs->device_name, flags);
a77133
 		if (fs->mmp_fd < 0) {
a77133
-			struct stat st;
a77133
-
a77133
-			/* Avoid O_DIRECT for filesystem image files if open
a77133
-			 * fails, since it breaks when running on tmpfs. */
a77133
-			if (errno == EINVAL && (flags & O_DIRECT) &&
a77133
-			    stat(fs->device_name, &st) == 0 &&
a77133
-			    S_ISREG(st.st_mode)) {
a77133
-				flags &= ~O_DIRECT;
a77133
-				goto retry;
a77133
-			}
a77133
 			retval = EXT2_ET_MMP_OPEN_DIRECT;
a77133
 			goto out;
a77133
 		}
a77133
-- 
a77133
2.35.1
a77133