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

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