Skip to contentSkip to conversation

ocfs2: validate i_refcount_loc when refcount flag is set

PATCH Superseded Deepanshu Kartikey 2 participants 2 messages Dec 8, 2025 – Dec 11, 2025 OCFS2

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: validate i_refcount_loc when refcount flag is set

Add validation in ocfs2_validate_inode_block() to check that if an inode has OCFS2_HAS_REFCOUNT_FL set, it must also have a valid i_refcount_loc. A corrupted filesystem image can have this inconsistent state, which later triggers a BUG_ON in ocfs2_remove_refcount_tree() when the inode is being wiped during unlink.

Catch this corruption early during inode validation to fail gracefully instead of crashing the kernel.

---
 fs/ocfs2/inode.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/ocfs2/inode.c +7 -0
index 78f81950c9ee..dd56407fc056 100644
@@ -1484,6 +1484,13 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
+	if ((di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) &&
+	    !di->i_refcount_loc) {
+		rc = ocfs2_error(sb, "Invalid dinode #%llu: refcount flag set but i_refcount_loc is zero\n",
+		      (unsigned long long)bh->b_blocknr);
+		goto bail;
+	}
+
 	rc = 0;
 
 bail:
Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 22 lines · Deepanshu Kartikey

Add validation in ocfs2_validate_inode_block() to check that if an inode has OCFS2_HAS_REFCOUNT_FL set, it must also have a valid i_refcount_loc. A corrupted filesystem image can have this inconsistent state, which later triggers a BUG_ON in ocfs2_remove_refcount_tree() when the inode is being wiped during unlink.

Catch this corruption early during inode validation to fail gracefully instead of crashing the kernel.

---
 fs/ocfs2/inode.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/ocfs2/inode.c +0 -0
index 78f81950c9ee..dd56407fc056 100644

fs/ocfs2/inode.c int ocfs2_validate_inode_block(struct super_block *sb, · line 1493

Quoted 10 lines
fs/ocfs2/inode.c +7 -0
@@ -1484,6 +1484,13 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
+	if ((di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) &&
+	    !di->i_refcount_loc) {
+		rc = ocfs2_error(sb, "Invalid dinode #%llu: refcount flag set but i_refcount_loc is zero\n",
+		      (unsigned long long)bh->b_blocknr);
+		goto bail;
+	}
+

The code logic looks good to me, but please run "kernel/scripts/checkpatch.pl" on the patch before sending it out. We can see that the line length is too long.

Thanks,
Heming

Quoted 7 lines
fs/ocfs2/inode.c +0 -0
 	rc = 0;
 
 bail:
Signature · 1 line
2.43.0