Viewing: ext4-add-IGET_NO_CHECKS-flag.patch

commit 1045c6433c50055c58c1bbdab10a1d447cdb00c2
Author:     Li Dongyang <dongyangli@ddn.com>
AuthorDate: Fri Sep 6 21:20:24 2024 +1000
LU-18195 osd-ldiskfs: use IGET_NO_CHECKS for objects under lost+found

With e2fsck older than v1.47.1-rc2-10-g849a9e6e1,
e2fsck could link ea inode under lost+found, and this
creates problems on kernels enforce ea_inode flag checks.

Check for lost+found fid and use NO_CHECKS flag
in the lfsck code path.

Add s_lpf_ino in sbi, and set it by find the lost+found
entry during ext4_fill_super().
In ext4_lookup, check if the parent is the lost+found dir.
Note lost+found won't be changed when target mounted as
lustre, so we only lookup once during mount and don't
monitor lost+found entry. Even if it's changed duing e2fsck
or mounted as ldiskfs, we could still detect at next mount.

Test-Parameters: trivial
Test-Parameters: fstype=ldiskfs serverdistro=el9.4 testlist=conf-sanity env=ONLY=61b
Signed-off-by: Li Dongyang <dongyangli@ddn.com>
Reviewed-by: Alex Zhuravlev <bzzz@whamcloud.com>
Reviewed-by: Li Xi <lixi@ddn.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
Change-Id: I95492fcc2f0ec4188dfb92b1dc1aa6055cab6ca8
Reviewed-on: https://review.whamcloud.com/56276

Index: linux-5.14.0-427.24.1.el9_4/fs/ext4/ext4.h
===================================================================
--- linux-5.14.0-427.24.1.el9_4.orig/fs/ext4/ext4.h
+++ linux-5.14.0-427.24.1.el9_4/fs/ext4/ext4.h
@@ -1801,6 +1801,7 @@ struct ext4_sb_info {
 	int s_fc_debug_max_replay;
 #endif
 	struct ext4_fc_replay_state s_fc_replay_state;
+	unsigned long s_lpf_ino;
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -3176,7 +3177,8 @@ typedef enum {
 	EXT4_IGET_SPECIAL =	0x0001, /* OK to iget a system inode */
 	EXT4_IGET_HANDLE = 	0x0002,	/* Inode # is from a handle */
 	EXT4_IGET_BAD =		0x0004, /* Allow to iget a bad inode */
-	EXT4_IGET_EA_INODE =	0x0008	/* Inode should contain an EA value */
+	EXT4_IGET_EA_INODE =	0x0008,	/* Inode should contain an EA value */
+	EXT4_IGET_NO_CHECKS =	0x0010	/* Allow iget without checking */
 } ext4_iget_flags;
 
 extern struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
Index: linux-5.14.0-427.24.1.el9_4/fs/ext4/inode.c
===================================================================
--- linux-5.14.0-427.24.1.el9_4.orig/fs/ext4/inode.c
+++ linux-5.14.0-427.24.1.el9_4/fs/ext4/inode.c
@@ -4834,6 +4834,8 @@ static inline void ext4_inode_set_iversi
 static const char *check_igot_inode(struct inode *inode, ext4_iget_flags flags)
 
 {
+	if (flags & EXT4_IGET_NO_CHECKS)
+		return NULL;
 	if (flags & EXT4_IGET_EA_INODE) {
 		if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
 			return "missing EA_INODE flag";
Index: linux-5.14.0-427.24.1.el9_4/fs/ext4/namei.c
===================================================================
--- linux-5.14.0-427.24.1.el9_4.orig/fs/ext4/namei.c
+++ linux-5.14.0-427.24.1.el9_4/fs/ext4/namei.c
@@ -2214,6 +2214,7 @@ static struct dentry *ext4_lookup(struct
 	struct inode *inode;
 	struct ext4_dir_entry_2 *de;
 	struct buffer_head *bh;
+	struct ext4_sb_info *sbi = EXT4_SB(dir->i_sb);
 
 	if (dentry->d_name.len > EXT4_NAME_LEN)
 		return ERR_PTR(-ENAMETOOLONG);
@@ -2234,7 +2235,9 @@ static struct dentry *ext4_lookup(struct
 					 dentry);
 			return ERR_PTR(-EFSCORRUPTED);
 		}
-		inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL);
+		inode = ext4_iget(dir->i_sb, ino,
+				  dir->i_ino == sbi->s_lpf_ino ?
+				  EXT4_IGET_NO_CHECKS : EXT4_IGET_NORMAL);
 		if (inode == ERR_PTR(-ESTALE)) {
 			EXT4_ERROR_INODE(dir,
 					 "deleted inode referenced: %u",
Index: linux-5.14.0-427.24.1.el9_4/fs/ext4/super.c
===================================================================
--- linux-5.14.0-427.24.1.el9_4.orig/fs/ext4/super.c
+++ linux-5.14.0-427.24.1.el9_4/fs/ext4/super.c
@@ -5740,6 +5740,9 @@ static int ext4_fill_super(struct super_
 {
 	struct ext4_fs_context *ctx = fc->fs_private;
 	struct ext4_sb_info *sbi;
+	struct buffer_head *bh;
+	struct ext4_dir_entry_2 *de;
+	const struct qstr lpf = QSTR_INIT("lost+found", 10);
 	const char *descr;
 	int ret;
 
@@ -5760,6 +5763,13 @@ static int ext4_fill_super(struct super_
 	if (ret < 0)
 		goto free_sbi;
 
+	bh = ext4_find_entry_locked(d_inode(sb->s_root), &lpf, &de, NULL, NULL);
+	if (!IS_ERR(bh) && bh) {
+		if (ext4_valid_inum(sb, le32_to_cpu(de->inode)))
+			sbi->s_lpf_ino = le32_to_cpu(de->inode);
+		brelse(bh);
+	}
+
 	if (sbi->s_journal) {
 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
 			descr = " journalled data mode";