Skip to contentSkip to conversation

ocfs2: split dinode validation into shared predicates

PATCH In review Joseph Qi 2 participants 33 messages Nov 9

Subsystems (2)
lore
Share & tools
lore

Joseph Qi

author maintainer
#0 lore raw

v3 cover ocfs2: split dinode validation into shared predicates

ocfs2_validate_inode_block() is a flat sequence of about twenty open-coded dinode checks. The filecheck variant duplicates eight of them but reports
differently: mlog() plus an OCFS2_FILECHECK_ERR_* code instead of ocfs2_error(), which can force the filesystem read-only. The duplicated conditions had drifted apart, and filecheck silently skipped the rest.

Patch series summary

01 Bug fix. Exempt orphaned dinodes from the zero nlink check.
An open-unlinked inode legitimately has nlink 0 on disk with OCFS2_VALID_FL still set, so the check rejects valid inodes. Orphan recovery then skips the resulting bad inode, so the orphan is never wiped and its space leaks. Fixed at the root first, so the refactor stays behaviour preserving.

02-21 Pure refactor, one check per commit. Each condition becomes a
static bool ocfs2_dinode_has_*() predicate, following the four helpers already in inode.c. Return codes and log messages stay at the call sites, so no ocfs2_error() call site moves and the two reporting paths remain distinct.

22 Bug fix. filecheck fix reported SUCCESS on corruption it cannot
repair. Revalidate and return the residual code, snapshotting the block before validation so a failed repair rolls back to what was read from disk instead of leaving a dirty buffer or a stray ecc fixup behind.

23-31 Wire the remaining predicates into filecheck. All reuse
OCFS2_FILECHECK_ERR_INVALIDINO, so no new error code and no change to the userspace-visible names in ocfs2_filecheck_errs[].

The inline xattr check is still not shared: ocfs2_validate_inode_xattr() calls ocfs2_error() through its helpers in xattr.c, so reusing it needs a reporting flag threaded into that file. Left for a follow-up.

Testing

linux-next 20261105, in a QEMU guest running the series. Block device buffers are flushed and caches dropped before each remount and before each on-disk comparison, otherwise a corruption test can read a stale buffer and appear to pass.

- Refactor is behaviour preserving: all 27 messages, all return codes and all ocfs2_error()/mlog() call sites in the normal validator are unchanged, each predicate body was compared against the condition it replaced, and filecheck's disassembly is instruction identical apart from __LINE__. The normal validator's disassembly also shows block rescheduling, so equivalence there rests on the source comparison rather than an instruction by instruction proof.

- No false positives on 4K blocks with inline-data, refcount, sparse, usrquota, grpquota, xattr and indexed-dirs, over seventeen inode types plus all system inodes during mount and I/O; and on 512-byte blocks over five types. Forcing i_xattr_inline_size up on 512-byte blocks drove the inline maximum negative and gave "id_count 312 exceeds max -88", confirming the comparison stayed signed.

- Seven corrupted dinodes on a nometaecc volume were each detected with the expected message, with no escalation and the filesystem still read-write.

- With metaecc on: a repairable field alone gives SUCCESS and is really written to disk; an unrepairable field alone, and a repairable plus unrepairable pair, both give INVALIDINO with the on-disk block byte identical afterwards and repeated checks reporting a real filecheck code rather than FAILED. An orphaned dinode with nlink 0 reports SUCCESS, while the same dinode without OCFS2_ORPHANED_FL still reports INVALIDINO, so the exemption did not disable the check.

- The stray bit ocfs2_block_check_validate() leaves behind when its hamming fixup fails was tested with a python model of that fixup, validated by checking it predicts the exact bit and the exact restored block for 99 single bit corruptions. With the flip aimed at an unchecked timestamp byte, fix gives SUCCESS and the on-disk block matches the predicted repair byte for byte, differing from the input image only in i_blkno and i_check. With the flip aimed at i_suballoc_slot, fix gives SUCCESS and repairs the ecc. A recoverable single bit gives SUCCESS, increments ecc_recoveries and writes nothing back, so the legitimate in-place fixup is kept rather than rolled back.

The ocfs2 testsuite and xfstests were not run.

Changes since v2

- Patch 22 took the block snapshot after the initial validation, which has already mutated bh->b_data. ocfs2_validate_meta_ecc() -> ocfs2_block_check_validate() applies a hamming fixup in place and restores only the ocfs2_block_check field on the way out, so when the post-fix crc32 still fails the flipped bit stays behind and the snapshot recorded it. The snapshot now precedes validation, every error exit restores it including the readonly, jbd, signature and VALID_FL gates, and the bogus flip is undone before any field repair when the initial validation reported BLOCKECC. Without that last step the ecc recompute stamps the stray flip with a valid checksum, and the flip can also fabricate an unrepairable field that is not on disk. An ecc check that passed keeps its data, a successful single bit fixup included, so recoverable single bit errors are still repaired. Both effects were reproduced on v2, as described above.

Changes since v1

- The zero nlink check rejected valid orphaned dinodes. Instead of exempting them only in filecheck, patch 01 fixes the predicate at the root. The check was added by commit 7efb45f9685f ("ocfs2: invalidate inode if i_mode is zero after block read"), whose changelog describes a check for "(i_mode == 0, i_nlink == 0, non-orphan)" while the code has no orphan exemption and ORs the conditions instead of ANDing them. v1 also understated the reachability: it claimed orphan recovery skips validation because ocfs2_read_blocks_sync() takes no validate callback, but ocfs2_read_locked_inode() calls ocfs2_validate_inode_block() explicitly right after it in the !can_lock branch. Recovery therefore rejects every orphan on every pass, and ocfs2_orphan_filldir() ignores the resulting iget failure ("Skip bad inodes so that recovery can continue"), so the orphan is never wiped and its space is leaked, with a "Corrupt state" message logged on each mount.

- Returning an error from the repair callback after mark_buffer_dirty() left a dirty non-uptodate buffer in the block device cache. ocfs2_read_blocks() clears BH_Uptodate on validate failure and ocfs2_read_locked_inode() bails before the exclusive lock upgrade and ocfs2_write_block(), so the next OCFS2_BH_IGNORE_CACHE read skips the disk read and fails -EIO, and later filecheck requests report FAILED instead of the real reason. Patch 22 now recomputes the ecc, revalidates, and only then marks the buffer dirty, restoring a snapshot otherwise. Two details: the snapshot covers the whole block because ocfs2_validate_meta_ecc() mutates it in place during a hamming fix, and the ecc is recomputed before revalidating so a bad ecc does not roll back its own repair. The mechanism was confirmed by reading those two functions; it was not reproduced by running v1.

- The l_next_free_rec repair copied an unvalidated l_count, spreading the corruption to a second field and stamping it with a good ecc. Now guarded by a valid l_count.

- The suballoc slot commit cited ocfs2_validate_group_descriptor(), which does not inspect i_suballoc_slot, and called the range predicate stricter than suballoc.c when it is identical. Corrected to ocfs2_get_suballoc_slot_bit() and reworded: the pair of predicates is stricter, the range test alone is not.

Link to v1: https://lore.kernel.org/ocfs2-devel/[email protected]/
Link to v2: https://lore.kernel.org/ocfs2-devel/[email protected]/

Joseph Qi (31):
ocfs2: exempt orphaned dinodes from the zero nlink check
ocfs2: abstract the zero nlink or mode dinode check
ocfs2: abstract the dinode i_blkno mismatch check
ocfs2: abstract the dinode OCFS2_VALID_FL check
ocfs2: abstract the dinode fs_generation mismatch check
ocfs2: abstract the dinode suballoc slot checks
ocfs2: abstract the dinode suballoc bit check
ocfs2: abstract the dinode orphaned slot check
ocfs2: abstract the dinode DIO orphaned slot check
ocfs2: abstract the inline data dinode guard and cluster check
ocfs2: abstract the inline data id_count bound check
ocfs2: abstract the inline data i_size bound check
ocfs2: abstract the fast symlink dinode guard and size check
ocfs2: abstract the fast symlink NUL-termination check
ocfs2: abstract the chain list dinode guard and count check
ocfs2: abstract the chain list next free record check
ocfs2: abstract the chain list bits per cluster check
ocfs2: abstract the dinode refcount location check
ocfs2: abstract the zero extent list count check
ocfs2: abstract the oversized extent list count check
ocfs2: abstract the extent list next free record check
ocfs2: report unrepairable corruption from online filecheck fix
ocfs2: check dinode nlink and mode in online filecheck
ocfs2: check dinode suballoc slot in online filecheck
ocfs2: check dinode suballoc bit in online filecheck
ocfs2: check dinode orphaned slots in online filecheck
ocfs2: check inline data dinodes in online filecheck
ocfs2: check fast symlink dinodes in online filecheck
ocfs2: check chain list dinodes in online filecheck
ocfs2: check dinode refcount location in online filecheck
ocfs2: check extent list dinodes in online filecheck

 fs/ocfs2/inode.c | 545 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 480 insertions(+), 65 deletions(-)
Signature · 1 line
2.39.3
author maintainer
#1 lore raw

PATCH v3 1/31 ocfs2: exempt orphaned dinodes from the zero nlink check

An open-unlinked inode legitimately has a zero link count on disk. ocfs2_unlink() drops the last link and writes it out with ocfs2_set_links_count(), ocfs2_orphan_add() then sets OCFS2_ORPHANED_FL, and OCFS2_VALID_FL is only cleared much later by ocfs2_remove_inode(). The zero nlink check added by the commit below rejects such an inode as -EFSCORRUPTED even though it is perfectly valid.

That commit describes its own change as a check for "(i_mode == 0, i_nlink == 0, non-orphan)", but the code it added has no orphan exemption and ORs the two conditions rather than ANDing them, so a zero link count alone is enough to fail even when i_mode is valid. Add the exemption the changelog describes.

Orphan recovery hits this on every pass. OCFS2_FI_FLAG_ORPHAN_RECOVERY clears can_lock in ocfs2_read_locked_inode(), so the block is read with ocfs2_read_blocks_sync() and then validated by the explicit ocfs2_validate_inode_block() call in that branch. The read helper having no validate callback of its own does not mean the path skips validation. ocfs2_orphan_filldir() then ignores the resulting ocfs2_iget() failure ("Skip bad inodes so that recovery can continue"), so the orphan is never
wiped: its space is leaked permanently and the "Corrupt state" message is logged again on every mount.

The same applies to an orphaned dinode read through ocfs2_read_inode_block(), for example from ocfs2_delete_inode() once the buffer has been evicted.

Note that a zero i_mode is already rejected by the later ocfs2_valid_inode_mode() check, since fs_umode_to_ftype(0) is FT_UNKNOWN, so the nlink arm was not what protected against the zombie inode the original commit was fixing.

DIO orphans need no exemption: ocfs2_add_inode_to_orphan() is the only caller that passes dio=true to ocfs2_orphan_add(), and it never drops a link, so those inodes keep a positive link count.

Fixes 7efb45f9685f ("ocfs2: invalidate inode if i_mode is zero after block read")
---
 fs/ocfs2/inode.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/ocfs2/inode.c +9 -1
index 7005e59519b2..371857673712 100644
@@ -1482,7 +1482,15 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	if ((!di->i_links_count && !di->i_links_count_hi) || !di->i_mode) {
+	/*
+	 * An open-unlinked inode legitimately has a zero link count on disk:
+	 * ocfs2_unlink() drops the last link and ocfs2_orphan_add() sets
+	 * OCFS2_ORPHANED_FL, while OCFS2_VALID_FL is only cleared later by
+	 * ocfs2_remove_inode().  DIO orphans keep their link count, so they
+	 * need no exemption here.
+	 */
+	if (!(le32_to_cpu(di->i_flags) & OCFS2_ORPHANED_FL) &&
+	    ((!di->i_links_count && !di->i_links_count_hi) || !di->i_mode)) {
 		mlog(ML_ERROR, "Invalid dinode #%llu: "
 			"Corrupt state (nlink = %u or mode = %u) detected!\n",
 		        (unsigned long long)bh->b_blocknr,
Signature · 1 line
2.39.3
author maintainer
#2 lore raw

PATCH v3 2/31 ocfs2: abstract the zero nlink or mode dinode check

In reply to Joseph Qi

ocfs2_validate_inode_block() open-codes the test for a dinode that has no links or no mode. Fold the condition into a predicate so it reads like the neighbouring ocfs2_dinode_has_*() helpers, and so the filecheck variant can share it.

Reuse ocfs2_read_links_count() rather than testing i_links_count and i_links_count_hi separately. OCFS2_LINKS_HI_SHIFT is 16 and both fields are 16 bits wide, so the combined count is zero exactly when both raw fields are zero.

The OCFS2_ORPHANED_FL carve-out moves into the predicate along with the rest of the condition, in the same early-return style as ocfs2_dinode_has_size_without_clusters().

The return code and the log message stay at the call site: filecheck reports the same conditions with a different code and must not call ocfs2_error().

No functional change intended.

---
 fs/ocfs2/inode.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
fs/ocfs2/inode.c +16 -9
index 371857673712..209066321932 100644
@@ -67,6 +67,21 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 						      struct buffer_head *bh);
 
+static bool ocfs2_dinode_has_zero_nlink_or_mode(struct ocfs2_dinode *di)
+{
+	/*
+	 * An open-unlinked inode legitimately has a zero link count on disk:
+	 * ocfs2_unlink() drops the last link and ocfs2_orphan_add() sets
+	 * OCFS2_ORPHANED_FL, while OCFS2_VALID_FL is only cleared later by
+	 * ocfs2_remove_inode().  DIO orphans keep their link count, so they
+	 * need no exemption here.
+	 */
+	if (le32_to_cpu(di->i_flags) & OCFS2_ORPHANED_FL)
+		return false;
+
+	return !ocfs2_read_links_count(di) || !di->i_mode;
+}
+
 static bool ocfs2_valid_inode_mode(umode_t mode)
 {
 	return fs_umode_to_ftype(mode) != FT_UNKNOWN;
@@ -1482,15 +1497,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	/*
-	 * An open-unlinked inode legitimately has a zero link count on disk:
-	 * ocfs2_unlink() drops the last link and ocfs2_orphan_add() sets
-	 * OCFS2_ORPHANED_FL, while OCFS2_VALID_FL is only cleared later by
-	 * ocfs2_remove_inode().  DIO orphans keep their link count, so they
-	 * need no exemption here.
-	 */
-	if (!(le32_to_cpu(di->i_flags) & OCFS2_ORPHANED_FL) &&
-	    ((!di->i_links_count && !di->i_links_count_hi) || !di->i_mode)) {
+	if (ocfs2_dinode_has_zero_nlink_or_mode(di)) {
 		mlog(ML_ERROR, "Invalid dinode #%llu: "
 			"Corrupt state (nlink = %u or mode = %u) detected!\n",
 		        (unsigned long long)bh->b_blocknr,
Signature · 1 line
2.39.3
author maintainer
#3 lore raw

PATCH v3 3/31 ocfs2: abstract the dinode i_blkno mismatch check

In reply to Joseph Qi

The same i_blkno against b_blocknr comparison is open-coded in ocfs2_validate_inode_block(), ocfs2_filecheck_validate_inode_block() and ocfs2_filecheck_repair_inode_block(). Fold it into a shared predicate.

Each call site keeps its own log message and return code: the normal validator escalates through ocfs2_error(), filecheck reports OCFS2_FILECHECK_ERR_BLOCKNO, and repair rewrites the field.

No functional change intended.

---
 fs/ocfs2/inode.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/ocfs2/inode.c +8 -3
index 209066321932..587fcea59f5a 100644
@@ -82,6 +82,11 @@ static bool ocfs2_dinode_has_zero_nlink_or_mode(struct ocfs2_dinode *di)
 	return !ocfs2_read_links_count(di) || !di->i_mode;
 }
 
+static bool ocfs2_dinode_has_wrong_blkno(struct ocfs2_dinode *di, u64 blkno)
+{
+	return le64_to_cpu(di->i_blkno) != blkno;
+}
+
 static bool ocfs2_valid_inode_mode(umode_t mode)
 {
 	return fs_umode_to_ftype(mode) != FT_UNKNOWN;
@@ -1518,7 +1523,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
+	if (ocfs2_dinode_has_wrong_blkno(di, bh->b_blocknr)) {
 		rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
 				 (unsigned long long)bh->b_blocknr,
 				 (unsigned long long)le64_to_cpu(di->i_blkno));
@@ -1840,7 +1845,7 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 	} else if (rc)
 		goto bail;
 
-	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
+	if (ocfs2_dinode_has_wrong_blkno(di, bh->b_blocknr)) {
 		mlog(ML_ERROR,
 		     "Filecheck: invalid dinode #%llu: i_blkno is %llu\n",
 		     (unsigned long long)bh->b_blocknr,
@@ -1945,7 +1950,7 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 		return -OCFS2_FILECHECK_ERR_VALIDFLAG;
 	}
 
-	if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
+	if (ocfs2_dinode_has_wrong_blkno(di, bh->b_blocknr)) {
 		di->i_blkno = cpu_to_le64(bh->b_blocknr);
 		changed = 1;
 		mlog(ML_ERROR,
Signature · 1 line
2.39.3
author maintainer
#4 lore raw

PATCH v3 4/31 ocfs2: abstract the dinode OCFS2_VALID_FL check

In reply to Joseph Qi

The test for a dinode without OCFS2_VALID_FL is open-coded three times, and the two spellings have already drifted apart: the normal validator uses le32_to_cpu(di->i_flags) & OCFS2_VALID_FL while filecheck and repair use di->i_flags & cpu_to_le32(OCFS2_VALID_FL). Fold them into one predicate using the le32_to_cpu() form.

Byteswapping is a bit permutation, so both spellings test the same on-disk bit on either endianness. The BUG_ON() in ocfs2_populate_inode() is left alone; it asserts an invariant the validator has already established rather than validating on-disk state.

No functional change intended.

---
 fs/ocfs2/inode.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/ocfs2/inode.c +8 -3
index 587fcea59f5a..8b4652002ae2 100644
@@ -87,6 +87,11 @@ static bool ocfs2_dinode_has_wrong_blkno(struct ocfs2_dinode *di, u64 blkno)
 	return le64_to_cpu(di->i_blkno) != blkno;
 }
 
+static bool ocfs2_dinode_has_no_valid_flag(struct ocfs2_dinode *di)
+{
+	return !(le32_to_cpu(di->i_flags) & OCFS2_VALID_FL);
+}
+
 static bool ocfs2_valid_inode_mode(umode_t mode)
 {
 	return fs_umode_to_ftype(mode) != FT_UNKNOWN;
@@ -1530,7 +1535,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	if (!(le32_to_cpu(di->i_flags) & OCFS2_VALID_FL)) {
+	if (ocfs2_dinode_has_no_valid_flag(di)) {
 		rc = ocfs2_error(sb,
 				 "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
 				 (unsigned long long)bh->b_blocknr);
@@ -1854,7 +1859,7 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
+	if (ocfs2_dinode_has_no_valid_flag(di)) {
 		mlog(ML_ERROR,
 		     "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL "
 		     "not set\n",
@@ -1943,7 +1948,7 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 		return -OCFS2_FILECHECK_ERR_INVALIDINO;
 	}
 
-	if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
+	if (ocfs2_dinode_has_no_valid_flag(di)) {
 		/* Cannot just add VALID_FL flag back as a fix,
 		 * need more things to check here.
 		 */
Signature · 1 line
2.39.3
author maintainer
#5 lore raw

PATCH v3 5/31 ocfs2: abstract the dinode fs_generation mismatch check

In reply to Joseph Qi

The comparison against OCFS2_SB(sb)->fs_generation is open-coded in the normal validator, the filecheck validator and the repair path. Fold it into a shared predicate.

Each call site keeps its own log message and return code: the normal validator escalates through ocfs2_error(), filecheck reports OCFS2_FILECHECK_ERR_GENERATION, and repair rewrites the field.

No functional change intended.

---
 fs/ocfs2/inode.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
fs/ocfs2/inode.c +9 -6
index 8b4652002ae2..6b78b3f3c5fa 100644
@@ -92,6 +92,12 @@ static bool ocfs2_dinode_has_no_valid_flag(struct ocfs2_dinode *di)
 	return !(le32_to_cpu(di->i_flags) & OCFS2_VALID_FL);
 }
 
+static bool ocfs2_dinode_has_wrong_fs_generation(struct super_block *sb,
+						 struct ocfs2_dinode *di)
+{
+	return le32_to_cpu(di->i_fs_generation) != OCFS2_SB(sb)->fs_generation;
+}
+
 static bool ocfs2_valid_inode_mode(umode_t mode)
 {
 	return fs_umode_to_ftype(mode) != FT_UNKNOWN;
@@ -1542,8 +1548,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	if (le32_to_cpu(di->i_fs_generation) !=
-	    OCFS2_SB(sb)->fs_generation) {
+	if (ocfs2_dinode_has_wrong_fs_generation(sb, di)) {
 		rc = ocfs2_error(sb,
 				 "Invalid dinode #%llu: fs_generation is %u\n",
 				 (unsigned long long)bh->b_blocknr,
@@ -1868,8 +1873,7 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	if (le32_to_cpu(di->i_fs_generation) !=
-	    OCFS2_SB(sb)->fs_generation) {
+	if (ocfs2_dinode_has_wrong_fs_generation(sb, di)) {
 		mlog(ML_ERROR,
 		     "Filecheck: invalid dinode #%llu: fs_generation is %u\n",
 		     (unsigned long long)bh->b_blocknr,
@@ -1964,8 +1968,7 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 		     (unsigned long long)le64_to_cpu(di->i_blkno));
 	}
 
-	if (le32_to_cpu(di->i_fs_generation) !=
-	    OCFS2_SB(sb)->fs_generation) {
+	if (ocfs2_dinode_has_wrong_fs_generation(sb, di)) {
 		di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
 		changed = 1;
 		mlog(ML_ERROR,
Signature · 1 line
2.39.3
author maintainer
#6 lore raw

PATCH v3 6/31 ocfs2: abstract the dinode suballoc slot checks

In reply to Joseph Qi

The suballoc slot validation is a nested if/else-if whose two arms carry different error messages. Split it into two predicates so each message stays at its own call site without having to re-test the slot value to work out which arm fired.

The arms were already mutually exclusive, so two sequential tests are equivalent to the if/else-if.

Keep the OCFS2_INVALID_SLOT exclusion in the range predicate. OCFS2_INVALID_SLOT is 65535, which is greater than max_slots - 1, so without it every system inode mkfs.ocfs2 creates from the global allocator would be rejected.

ocfs2_get_suballoc_slot_bit() in suballoc.c open-codes an identical range test, and is deliberately not converted. The dinode validator pairs the range predicate with ocfs2_dinode_has_invalid_suballoc_slot(), which also rejects OCFS2_INVALID_SLOT on a non-system inode, so that pair is stricter than what ocfs2_get_suballoc_slot_bit() accepts. The range predicate on its own is not stricter, it is the same test.

No functional change intended.

---
 fs/ocfs2/inode.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
fs/ocfs2/inode.c +22 -9
index 6b78b3f3c5fa..0929deb3e4b4 100644
@@ -98,6 +98,19 @@ static bool ocfs2_dinode_has_wrong_fs_generation(struct super_block *sb,
 	return le32_to_cpu(di->i_fs_generation) != OCFS2_SB(sb)->fs_generation;
 }
 
+static bool ocfs2_dinode_has_invalid_suballoc_slot(struct ocfs2_dinode *di)
+{
+	return le16_to_cpu(di->i_suballoc_slot) == (u16)OCFS2_INVALID_SLOT &&
+		!(le32_to_cpu(di->i_flags) & OCFS2_SYSTEM_FL);
+}
+
+static bool ocfs2_dinode_has_out_of_range_suballoc_slot(struct super_block *sb,
+							struct ocfs2_dinode *di)
+{
+	return le16_to_cpu(di->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
+		(u32)le16_to_cpu(di->i_suballoc_slot) > OCFS2_SB(sb)->max_slots - 1;
+}
+
 static bool ocfs2_valid_inode_mode(umode_t mode)
 {
 	return fs_umode_to_ftype(mode) != FT_UNKNOWN;
@@ -1564,15 +1577,15 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 	 * deleting it would pass the slot to get_local_system_inode() via
 	 * ocfs2_remove_inode() and trigger BUG_ON(slot == OCFS2_INVALID_SLOT).
 	 */
-	if (le16_to_cpu(di->i_suballoc_slot) == (u16)OCFS2_INVALID_SLOT) {
-		if (!(le32_to_cpu(di->i_flags) & OCFS2_SYSTEM_FL)) {
-			rc = ocfs2_error(sb,
-					 "Invalid dinode %llu: suballoc slot %u for non-system inode\n",
-					 (unsigned long long)bh->b_blocknr,
-					 le16_to_cpu(di->i_suballoc_slot));
-			goto bail;
-		}
-	} else if ((u32)le16_to_cpu(di->i_suballoc_slot) > OCFS2_SB(sb)->max_slots - 1) {
+	if (ocfs2_dinode_has_invalid_suballoc_slot(di)) {
+		rc = ocfs2_error(sb,
+				 "Invalid dinode %llu: suballoc slot %u for non-system inode\n",
+				 (unsigned long long)bh->b_blocknr,
+				 le16_to_cpu(di->i_suballoc_slot));
+		goto bail;
+	}
+
+	if (ocfs2_dinode_has_out_of_range_suballoc_slot(sb, di)) {
 		rc = ocfs2_error(sb, "Invalid dinode %llu: suballoc slot %u\n",
 				 (unsigned long long)bh->b_blocknr,
 				 le16_to_cpu(di->i_suballoc_slot));
Signature · 1 line
2.39.3
author maintainer
#7 lore raw

PATCH v3 7/31 ocfs2: abstract the dinode suballoc bit check

In reply to Joseph Qi

Fold the i_suballoc_bit against ocfs2_suballoc_bits_per_block() test into a predicate. The rationale comment stays at the call site, next to the error it explains.

No functional change intended.

---
 fs/ocfs2/inode.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/ocfs2/inode.c +7 -1
index 0929deb3e4b4..3be98328925d 100644
@@ -111,6 +111,12 @@ static bool ocfs2_dinode_has_out_of_range_suballoc_slot(struct super_block *sb,
 		(u32)le16_to_cpu(di->i_suballoc_slot) > OCFS2_SB(sb)->max_slots - 1;
 }
 
+static bool ocfs2_dinode_has_bad_suballoc_bit(struct super_block *sb,
+					      struct ocfs2_dinode *di)
+{
+	return le16_to_cpu(di->i_suballoc_bit) >= ocfs2_suballoc_bits_per_block(sb);
+}
+
 static bool ocfs2_valid_inode_mode(umode_t mode)
 {
 	return fs_umode_to_ftype(mode) != FT_UNKNOWN;
@@ -1601,7 +1607,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 	 * BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl)), since any
 	 * group holds at most ocfs2_suballoc_bits_per_block() bits.
 	 */
-	if (le16_to_cpu(di->i_suballoc_bit) >= ocfs2_suballoc_bits_per_block(sb)) {
+	if (ocfs2_dinode_has_bad_suballoc_bit(sb, di)) {
 		rc = ocfs2_error(sb, "Invalid dinode %llu: suballoc bit %u\n",
 				 (unsigned long long)bh->b_blocknr,
 				 le16_to_cpu(di->i_suballoc_bit));
Signature · 1 line
2.39.3
author maintainer
#8 lore raw

PATCH v3 8/31 ocfs2: abstract the dinode orphaned slot check

In reply to Joseph Qi

Fold the OCFS2_ORPHANED_FL guarded i_orphaned_slot range test into a predicate. The flag test stays inside it: i_orphaned_slot is only meaningful when the inode is actually orphaned, so a bare range test would reject valid inodes whose orphaned slot field is stale.

No functional change intended.

---
 fs/ocfs2/inode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/ocfs2/inode.c +8 -2
index 3be98328925d..fd9613a95802 100644
@@ -117,6 +117,13 @@ static bool ocfs2_dinode_has_bad_suballoc_bit(struct super_block *sb,
 	return le16_to_cpu(di->i_suballoc_bit) >= ocfs2_suballoc_bits_per_block(sb);
 }
 
+static bool ocfs2_dinode_has_bad_orphaned_slot(struct super_block *sb,
+					       struct ocfs2_dinode *di)
+{
+	return (le32_to_cpu(di->i_flags) & OCFS2_ORPHANED_FL) &&
+		le16_to_cpu(di->i_orphaned_slot) >= OCFS2_SB(sb)->max_slots;
+}
+
 static bool ocfs2_valid_inode_mode(umode_t mode)
 {
 	return fs_umode_to_ftype(mode) != FT_UNKNOWN;
@@ -1614,8 +1621,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	if ((le32_to_cpu(di->i_flags) & OCFS2_ORPHANED_FL) &&
-	    le16_to_cpu(di->i_orphaned_slot) >= OCFS2_SB(sb)->max_slots) {
+	if (ocfs2_dinode_has_bad_orphaned_slot(sb, di)) {
 		rc = ocfs2_error(sb, "Invalid dinode %llu: orphaned slot %u\n",
 				 (unsigned long long)bh->b_blocknr,
 				 le16_to_cpu(di->i_orphaned_slot));
Signature · 1 line
2.39.3
author maintainer
#9 lore raw

PATCH v3 9/31 ocfs2: abstract the dinode DIO orphaned slot check

In reply to Joseph Qi

Fold the OCFS2_DIO_ORPHANED_FL guarded i_dio_orphaned_slot range test into a predicate, mirroring the orphaned slot one. The flag test stays inside it for the same reason: the slot field is only meaningful while the inode is DIO orphaned.

No functional change intended.

---
 fs/ocfs2/inode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/ocfs2/inode.c +8 -2
index fd9613a95802..17d546a9245d 100644
@@ -124,6 +124,13 @@ static bool ocfs2_dinode_has_bad_orphaned_slot(struct super_block *sb,
 		le16_to_cpu(di->i_orphaned_slot) >= OCFS2_SB(sb)->max_slots;
 }
 
+static bool ocfs2_dinode_has_bad_dio_orphaned_slot(struct super_block *sb,
+						   struct ocfs2_dinode *di)
+{
+	return (le32_to_cpu(di->i_flags) & OCFS2_DIO_ORPHANED_FL) &&
+		le16_to_cpu(di->i_dio_orphaned_slot) >= OCFS2_SB(sb)->max_slots;
+}
+
 static bool ocfs2_valid_inode_mode(umode_t mode)
 {
 	return fs_umode_to_ftype(mode) != FT_UNKNOWN;
@@ -1628,8 +1635,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
-	if ((le32_to_cpu(di->i_flags) & OCFS2_DIO_ORPHANED_FL) &&
-	    le16_to_cpu(di->i_dio_orphaned_slot) >= OCFS2_SB(sb)->max_slots) {
+	if (ocfs2_dinode_has_bad_dio_orphaned_slot(sb, di)) {
 		rc = ocfs2_error(sb, "Invalid dinode %llu: DIO orphaned slot %u\n",
 				 (unsigned long long)bh->b_blocknr,
 				 le16_to_cpu(di->i_dio_orphaned_slot));
Signature · 1 line
2.39.3
author maintainer
#10 lore raw

PATCH v3 10/31 ocfs2: abstract the inline data dinode guard and cluster check

In reply to Joseph Qi

Introduce ocfs2_dinode_has_inline_data() for the OCFS2_INLINE_DATA_FL test that selects the id2.i_data union arm, and ocfs2_dinode_has_inline_clusters() for the rule that an inline-data inode must not also have allocated clusters.

The guard stays above the sub-checks: reading id_count or i_clusters against the wrong id2 arm would produce false positives on valid inodes.

No functional change intended.

---
 fs/ocfs2/inode.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
fs/ocfs2/inode.c +12 -2
index 17d546a9245d..2aac1918562a 100644
@@ -164,6 +164,16 @@ static bool ocfs2_dinode_has_size_without_clusters(struct super_block *sb,
 	return !ocfs2_sparse_alloc(OCFS2_SB(sb)) && S_ISREG(mode);
 }
 
+static bool ocfs2_dinode_has_inline_data(struct ocfs2_dinode *di)
+{
+	return le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL;
+}
+
+static bool ocfs2_dinode_has_inline_clusters(struct ocfs2_dinode *di)
+{
+	return le32_to_cpu(di->i_clusters) != 0;
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1726,10 +1736,10 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 	if (rc)
 		goto bail;
 
-	if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) {
+	if (ocfs2_dinode_has_inline_data(di)) {
 		struct ocfs2_inline_data *data = &di->id2.i_data;
 
-		if (le32_to_cpu(di->i_clusters)) {
+		if (ocfs2_dinode_has_inline_clusters(di)) {
 			rc = ocfs2_error(sb,
 					 "Invalid dinode %llu: %u clusters\n",
 					 (unsigned long long)bh->b_blocknr,
Signature · 1 line
2.39.3
author maintainer
#11 lore raw

PATCH v3 11/31 ocfs2: abstract the inline data id_count bound check

In reply to Joseph Qi

Fold the id_count against ocfs2_max_inline_data_with_xattr() test into a predicate.

The comparison stays signed. ocfs2_max_inline_data_with_xattr() returns int and subtracts the unvalidated on-disk i_xattr_inline_size from the space available in the inode block, so it can go negative on small block sizes. Caching it in an unsigned local would turn a negative maximum into a huge bound and silently accept the oversized id_count. The message keeps its %d for the same reason.

No functional change intended.

---
 fs/ocfs2/inode.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
fs/ocfs2/inode.c +14 -2
index 2aac1918562a..259f44661f37 100644
@@ -174,6 +174,19 @@ static bool ocfs2_dinode_has_inline_clusters(struct ocfs2_dinode *di)
 	return le32_to_cpu(di->i_clusters) != 0;
 }
 
+static bool ocfs2_dinode_has_oversized_inline_data(struct super_block *sb,
+						   struct ocfs2_dinode *di)
+{
+	/*
+	 * The maximum is a signed int and can go negative, because
+	 * i_xattr_inline_size is itself unvalidated.  Keep the comparison
+	 * signed: an unsigned bound would turn a negative maximum into a
+	 * huge one and let the oversized id_count through.
+	 */
+	return le16_to_cpu(di->id2.i_data.id_count) >
+		ocfs2_max_inline_data_with_xattr(sb, di);
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1747,8 +1760,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 			goto bail;
 		}
 
-		if (le16_to_cpu(data->id_count) >
-		    ocfs2_max_inline_data_with_xattr(sb, di)) {
+		if (ocfs2_dinode_has_oversized_inline_data(sb, di)) {
 			rc = ocfs2_error(sb,
 					 "Invalid dinode #%llu: inline data id_count %u exceeds max %d\n",
 					 (unsigned long long)bh->b_blocknr,
Signature · 1 line
2.39.3
author maintainer
#12 lore raw

PATCH v3 12/31 ocfs2: abstract the inline data i_size bound check

In reply to Joseph Qi

Fold the i_size against id_count test into a predicate, completing the inline-data group.

No functional change intended.

---
 fs/ocfs2/inode.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/inode.c +6 -1
index 259f44661f37..8e867f6f394a 100644
@@ -187,6 +187,11 @@ static bool ocfs2_dinode_has_oversized_inline_data(struct super_block *sb,
 		ocfs2_max_inline_data_with_xattr(sb, di);
 }
 
+static bool ocfs2_dinode_has_inline_size_over_count(struct ocfs2_dinode *di)
+{
+	return le64_to_cpu(di->i_size) > le16_to_cpu(di->id2.i_data.id_count);
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1769,7 +1774,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 			goto bail;
 		}
 
-		if (le64_to_cpu(di->i_size) > le16_to_cpu(data->id_count)) {
+		if (ocfs2_dinode_has_inline_size_over_count(di)) {
 			rc = ocfs2_error(sb,
 					 "Invalid dinode #%llu: inline data i_size %llu exceeds id_count %u\n",
 					 (unsigned long long)bh->b_blocknr,
Signature · 1 line
2.39.3
author maintainer
#13 lore raw

PATCH v3 13/31 ocfs2: abstract the fast symlink dinode guard and size check

In reply to Joseph Qi

Introduce ocfs2_dinode_is_fast_symlink() for the S_ISLNK with no allocated clusters test that selects the id2.i_symlink union arm, and ocfs2_dinode_has_oversized_fast_symlink() for the i_size bound.

The guard is deliberately not shared with the similar test inside ocfs2_dinode_has_extents(), which additionally requires a non-zero i_size. A zero-length, zero-cluster symlink currently falls through to the extent list checks, and reusing one predicate for both would change that.

No functional change intended.

---
 fs/ocfs2/inode.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
fs/ocfs2/inode.c +17 -3
index 8e867f6f394a..bf27925dd5c8 100644
@@ -192,6 +192,21 @@ static bool ocfs2_dinode_has_inline_size_over_count(struct ocfs2_dinode *di)
 	return le64_to_cpu(di->i_size) > le16_to_cpu(di->id2.i_data.id_count);
 }
 
+/*
+ * Unlike the similar test in ocfs2_dinode_has_extents(), this does not require
+ * a non-zero i_size: a zero-length fast symlink is still validated here.
+ */
+static bool ocfs2_dinode_is_fast_symlink(struct ocfs2_dinode *di)
+{
+	return S_ISLNK(le16_to_cpu(di->i_mode)) && !le32_to_cpu(di->i_clusters);
+}
+
+static bool ocfs2_dinode_has_oversized_fast_symlink(struct super_block *sb,
+						    struct ocfs2_dinode *di)
+{
+	return le64_to_cpu(di->i_size) >= ocfs2_fast_symlink_chars(sb);
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1784,12 +1799,11 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		}
 	}
 
-	if (S_ISLNK(le16_to_cpu(di->i_mode)) &&
-	    !le32_to_cpu(di->i_clusters)) {
+	if (ocfs2_dinode_is_fast_symlink(di)) {
 		int max_inline = ocfs2_fast_symlink_chars(sb);
 		u64 i_size = le64_to_cpu(di->i_size);
 
-		if (i_size >= max_inline) {
+		if (ocfs2_dinode_has_oversized_fast_symlink(sb, di)) {
 			rc = ocfs2_error(sb,
 					 "Invalid dinode #%llu: fast symlink i_size %llu exceeds max %d\n",
 					 (unsigned long long)bh->b_blocknr,
Signature · 1 line
2.39.3
author maintainer
#14 lore raw

PATCH v3 14/31 ocfs2: abstract the fast symlink NUL-termination check

In reply to Joseph Qi

Fold the strnlen() test into a predicate that bounds the read itself against ocfs2_fast_symlink_chars(), which is exactly the space available for id2.i_symlink in the inode block.

The open-coded version was only safe because the preceding i_size check had already bailed: i_size comes from disk, so an unbounded strnlen() on i_size + 1 bytes can run past the end of the buffer. Keeping that ordering dependency implicit in a helper would be fragile, especially once the filecheck variant starts calling it, so the predicate returns false for an oversized i_size and leaves that case to ocfs2_dinode_has_oversized_fast_symlink().

At the existing call site the size check still runs first, so behaviour is unchanged.

No functional change intended.

---
 fs/ocfs2/inode.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
fs/ocfs2/inode.c +20 -1
index bf27925dd5c8..dbb4563dbc70 100644
@@ -207,6 +207,25 @@ static bool ocfs2_dinode_has_oversized_fast_symlink(struct super_block *sb,
 	return le64_to_cpu(di->i_size) >= ocfs2_fast_symlink_chars(sb);
 }
 
+static bool ocfs2_dinode_has_unterminated_fast_symlink(struct super_block *sb,
+						       struct ocfs2_dinode *di)
+{
+	u64 i_size = le64_to_cpu(di->i_size);
+
+	/*
+	 * Bound the strnlen() here rather than relying on the caller having
+	 * rejected an oversized i_size first.  i_size comes from disk, and
+	 * ocfs2_fast_symlink_chars() is exactly the space available in the
+	 * inode block, so without this bound the read can run past it.
+	 * An oversized i_size is reported by
+	 * ocfs2_dinode_has_oversized_fast_symlink() instead.
+	 */
+	if (i_size >= ocfs2_fast_symlink_chars(sb))
+		return false;
+
+	return strnlen((char *)di->id2.i_symlink, i_size + 1) != i_size;
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1812,7 +1831,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 			goto bail;
 		}
 
-		if (strnlen((char *)di->id2.i_symlink, i_size + 1) != i_size) {
+		if (ocfs2_dinode_has_unterminated_fast_symlink(sb, di)) {
 			rc = ocfs2_error(sb,
 					 "Invalid dinode #%llu: fast symlink is not NUL-terminated at i_size %llu\n",
 					 (unsigned long long)bh->b_blocknr,
Signature · 1 line
2.39.3
author maintainer
#15 lore raw

PATCH v3 15/31 ocfs2: abstract the chain list dinode guard and count check

In reply to Joseph Qi

Introduce ocfs2_dinode_has_chain_list() for the OCFS2_CHAIN_FL test that selects the id2.i_chain union arm, and ocfs2_dinode_has_bad_chain_count() for the cl_count against ocfs2_chain_recs_per_inode() test.

This mirrors the guard predicates already used for the inline-data and fast symlink groups, and ocfs2_dinode_has_extents() for the extent list group.

No functional change intended.

---
 fs/ocfs2/inode.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
fs/ocfs2/inode.c +14 -2
index dbb4563dbc70..71aded2c88f7 100644
@@ -226,6 +226,18 @@ static bool ocfs2_dinode_has_unterminated_fast_symlink(struct super_block *sb,
 	return strnlen((char *)di->id2.i_symlink, i_size + 1) != i_size;
 }
 
+static bool ocfs2_dinode_has_chain_list(struct ocfs2_dinode *di)
+{
+	return le32_to_cpu(di->i_flags) & OCFS2_CHAIN_FL;
+}
+
+static bool ocfs2_dinode_has_bad_chain_count(struct super_block *sb,
+					     struct ocfs2_dinode *di)
+{
+	return le16_to_cpu(di->id2.i_chain.cl_count) !=
+		ocfs2_chain_recs_per_inode(sb);
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1840,12 +1852,12 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		}
 	}
 
-	if (le32_to_cpu(di->i_flags) & OCFS2_CHAIN_FL) {
+	if (ocfs2_dinode_has_chain_list(di)) {
 		struct ocfs2_chain_list *cl = &di->id2.i_chain;
 		u16 bpc = 1 << (OCFS2_SB(sb)->s_clustersize_bits -
 				sb->s_blocksize_bits);
 
-		if (le16_to_cpu(cl->cl_count) != ocfs2_chain_recs_per_inode(sb)) {
+		if (ocfs2_dinode_has_bad_chain_count(sb, di)) {
 			rc = ocfs2_error(sb, "Invalid dinode %llu: chain list count %u\n",
 					 (unsigned long long)bh->b_blocknr,
 					 le16_to_cpu(cl->cl_count));
Signature · 1 line
2.39.3
author maintainer
#16 lore raw

PATCH v3 16/31 ocfs2: abstract the chain list next free record check

In reply to Joseph Qi

Fold the cl_next_free_rec against cl_count test into a predicate.

ocfs2_claim_suballoc_bits() in suballoc.c open-codes a similar test that also rejects cl_next_free_rec == 0. It is left alone because this predicate is not equivalent to it.

No functional change intended.

---
 fs/ocfs2/inode.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/ocfs2/inode.c +7 -1
index 71aded2c88f7..0ad86aaa9ab3 100644
@@ -238,6 +238,12 @@ static bool ocfs2_dinode_has_bad_chain_count(struct super_block *sb,
 		ocfs2_chain_recs_per_inode(sb);
 }
 
+static bool ocfs2_dinode_has_bad_chain_next_free(struct ocfs2_dinode *di)
+{
+	return le16_to_cpu(di->id2.i_chain.cl_next_free_rec) >
+		le16_to_cpu(di->id2.i_chain.cl_count);
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1863,7 +1869,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 					 le16_to_cpu(cl->cl_count));
 			goto bail;
 		}
-		if (le16_to_cpu(cl->cl_next_free_rec) > le16_to_cpu(cl->cl_count)) {
+		if (ocfs2_dinode_has_bad_chain_next_free(di)) {
 			rc = ocfs2_error(sb, "Invalid dinode %llu: chain list index %u\n",
 					 (unsigned long long)bh->b_blocknr,
 					 le16_to_cpu(cl->cl_next_free_rec));
Signature · 1 line
2.39.3
author maintainer
#17 lore raw

PATCH v3 17/31 ocfs2: abstract the chain list bits per cluster check

In reply to Joseph Qi

Fold the cl_bpc test into a predicate that computes the expected bits per cluster itself, and drop the u16 bpc local from ocfs2_validate_inode_block(). No log message prints it, so keeping it at the call site would leave an unused variable.

The two bitmap_blkno conditions are preserved: the global bitmap inode itself is exempt from the bits-per-cluster check.

No functional change intended.

---
 fs/ocfs2/inode.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
fs/ocfs2/inode.c +11 -5
index 0ad86aaa9ab3..34129e25a6ac 100644
@@ -244,6 +244,16 @@ static bool ocfs2_dinode_has_bad_chain_next_free(struct ocfs2_dinode *di)
 		le16_to_cpu(di->id2.i_chain.cl_count);
 }
 
+static bool ocfs2_dinode_has_bad_chain_bpc(struct super_block *sb,
+					   struct ocfs2_dinode *di)
+{
+	u16 bpc = 1 << (OCFS2_SB(sb)->s_clustersize_bits - sb->s_blocksize_bits);
+
+	return OCFS2_SB(sb)->bitmap_blkno &&
+		OCFS2_SB(sb)->bitmap_blkno != le64_to_cpu(di->i_blkno) &&
+		le16_to_cpu(di->id2.i_chain.cl_bpc) != bpc;
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1860,8 +1870,6 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 
 	if (ocfs2_dinode_has_chain_list(di)) {
 		struct ocfs2_chain_list *cl = &di->id2.i_chain;
-		u16 bpc = 1 << (OCFS2_SB(sb)->s_clustersize_bits -
-				sb->s_blocksize_bits);
 
 		if (ocfs2_dinode_has_bad_chain_count(sb, di)) {
 			rc = ocfs2_error(sb, "Invalid dinode %llu: chain list count %u\n",
@@ -1875,9 +1883,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 					 le16_to_cpu(cl->cl_next_free_rec));
 			goto bail;
 		}
-		if (OCFS2_SB(sb)->bitmap_blkno &&
-		    OCFS2_SB(sb)->bitmap_blkno != le64_to_cpu(di->i_blkno) &&
-		    le16_to_cpu(cl->cl_bpc) != bpc) {
+		if (ocfs2_dinode_has_bad_chain_bpc(sb, di)) {
 			rc = ocfs2_error(sb, "Invalid dinode %llu: bits per cluster %u\n",
 					 (unsigned long long)bh->b_blocknr,
 					 le16_to_cpu(cl->cl_bpc));
Signature · 1 line
2.39.3
author maintainer
#18 lore raw

PATCH v3 18/31 ocfs2: abstract the dinode refcount location check

In reply to Joseph Qi

Fold the OCFS2_HAS_REFCOUNT_FL without i_refcount_loc test into a predicate. The flag test stays inside it, since i_refcount_loc is only meaningful for a reflink-capable inode.

No functional change intended.

---
 fs/ocfs2/inode.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
fs/ocfs2/inode.c +7 -2
index 34129e25a6ac..501a11d2b071 100644
@@ -254,6 +254,12 @@ static bool ocfs2_dinode_has_bad_chain_bpc(struct super_block *sb,
 		le16_to_cpu(di->id2.i_chain.cl_bpc) != bpc;
 }
 
+static bool ocfs2_dinode_has_refcount_without_loc(struct ocfs2_dinode *di)
+{
+	return (le16_to_cpu(di->i_dyn_features) & OCFS2_HAS_REFCOUNT_FL) &&
+		!di->i_refcount_loc;
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1891,8 +1897,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		}
 	}
 
-	if ((le16_to_cpu(di->i_dyn_features) & OCFS2_HAS_REFCOUNT_FL) &&
-	    !di->i_refcount_loc) {
+	if (ocfs2_dinode_has_refcount_without_loc(di)) {
 		rc = ocfs2_error(sb, "Inode #%llu has refcount flag but no i_refcount_loc\n",
 				(unsigned long long)bh->b_blocknr);
 		goto bail;
Signature · 1 line
2.39.3
author maintainer
#19 lore raw

PATCH v3 19/31 ocfs2: abstract the zero extent list count check

In reply to Joseph Qi

Fold the l_count == 0 test into a predicate, the first of the extent list group guarded by ocfs2_dinode_has_extents().

No functional change intended.

---
 fs/ocfs2/inode.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/inode.c +6 -1
index 501a11d2b071..551263f290bf 100644
@@ -260,6 +260,11 @@ static bool ocfs2_dinode_has_refcount_without_loc(struct ocfs2_dinode *di)
 		!di->i_refcount_loc;
 }
 
+static bool ocfs2_dinode_has_zero_extent_count(struct ocfs2_dinode *di)
+{
+	return le16_to_cpu(di->id2.i_list.l_count) == 0;
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1908,7 +1913,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 		u16 count = le16_to_cpu(el->l_count);
 		u16 next_free = le16_to_cpu(el->l_next_free_rec);
 
-		if (count == 0) {
+		if (ocfs2_dinode_has_zero_extent_count(di)) {
 			rc = ocfs2_error(sb,
 					 "Invalid dinode %llu: extent list l_count is zero\n",
 					 (unsigned long long)bh->b_blocknr);
Signature · 1 line
2.39.3
author maintainer
#20 lore raw

PATCH v3 20/31 ocfs2: abstract the oversized extent list count check

In reply to Joseph Qi

Fold the l_count against ocfs2_extent_recs_per_inode() test into a predicate.

Move the rationale for using the no-xattr maximum into the predicate, next to the bound it explains, rather than leaving it at the call site where the bound is no longer computed.

No functional change intended.

---
 fs/ocfs2/inode.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
fs/ocfs2/inode.c +14 -7
index 551263f290bf..8c4ea3d048d1 100644
@@ -265,6 +265,19 @@ static bool ocfs2_dinode_has_zero_extent_count(struct ocfs2_dinode *di)
 	return le16_to_cpu(di->id2.i_list.l_count) == 0;
 }
 
+static bool ocfs2_dinode_has_oversized_extent_count(struct super_block *sb,
+						    struct ocfs2_dinode *di)
+{
+	/*
+	 * The exact capacity depends on i_xattr_inline_size, another
+	 * unvalidated on-disk field. Inline xattrs only shrink the list, so
+	 * the no-xattr maximum is a safe upper bound that a valid l_count
+	 * never exceeds.
+	 */
+	return le16_to_cpu(di->id2.i_list.l_count) >
+		ocfs2_extent_recs_per_inode(sb);
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1919,13 +1932,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 					 (unsigned long long)bh->b_blocknr);
 			goto bail;
 		}
-		/*
-		 * The exact capacity depends on i_xattr_inline_size, another
-		 * unvalidated on-disk field. Inline xattrs only shrink the
-		 * list, so the no-xattr maximum is a safe upper bound that a
-		 * valid l_count never exceeds.
-		 */
-		if (count > ocfs2_extent_recs_per_inode(sb)) {
+		if (ocfs2_dinode_has_oversized_extent_count(sb, di)) {
 			rc = ocfs2_error(sb,
 					 "Invalid dinode %llu: extent list l_count %u exceeds max %u\n",
 					 (unsigned long long)bh->b_blocknr, count,
Signature · 1 line
2.39.3
author maintainer
#21 lore raw

PATCH v3 21/31 ocfs2: abstract the extent list next free record check

In reply to Joseph Qi

Fold the l_next_free_rec against l_count test into a predicate and use it in both ocfs2_validate_inode_block() and ocfs2_filecheck_repair_inode_block(), which open-coded the same comparison. Repair keeps its ocfs2_dinode_has_extents() guard, which selects the id2.i_list union arm.

This completes the split of ocfs2_validate_inode_block()'s open-coded conditions into ocfs2_dinode_has_*() predicates. Every return code and log message stayed at its call site, so the normal validator still escalates through ocfs2_error() while filecheck reports its own OCFS2_FILECHECK_ERR_* codes without touching the error handling state.

No functional change intended.

---
 fs/ocfs2/inode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/ocfs2/inode.c +8 -2
index 8c4ea3d048d1..9db147de1982 100644
@@ -278,6 +278,12 @@ static bool ocfs2_dinode_has_oversized_extent_count(struct super_block *sb,
 		ocfs2_extent_recs_per_inode(sb);
 }
 
+static bool ocfs2_dinode_has_bad_extent_next_free(struct ocfs2_dinode *di)
+{
+	return le16_to_cpu(di->id2.i_list.l_next_free_rec) >
+		le16_to_cpu(di->id2.i_list.l_count);
+}
+
 void ocfs2_set_inode_flags(struct inode *inode)
 {
 	unsigned int flags = OCFS2_I(inode)->ip_attr;
@@ -1939,7 +1945,7 @@ int ocfs2_validate_inode_block(struct super_block *sb,
 					 ocfs2_extent_recs_per_inode(sb));
 			goto bail;
 		}
-		if (next_free > count) {
+		if (ocfs2_dinode_has_bad_extent_next_free(di)) {
 			rc = ocfs2_error(sb,
 					 "Invalid dinode %llu: extent list l_next_free_rec %u exceeds l_count %u\n",
 					 (unsigned long long)bh->b_blocknr, next_free, count);
@@ -2110,7 +2116,7 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 	}
 
 	if (ocfs2_dinode_has_extents(di) &&
-	    le16_to_cpu(di->id2.i_list.l_next_free_rec) > le16_to_cpu(di->id2.i_list.l_count)) {
+	    ocfs2_dinode_has_bad_extent_next_free(di)) {
 		di->id2.i_list.l_next_free_rec = di->id2.i_list.l_count;
 		changed = 1;
 		mlog(ML_ERROR,
Signature · 1 line
2.39.3
author maintainer
#22 lore raw

PATCH v3 22/31 ocfs2: report unrepairable corruption from online filecheck fix

In reply to Joseph Qi

ocfs2_filecheck_repair_inode_block() proceeds when validation fails but then returns 0 unconditionally, after repairing only i_blkno, fs_generation and l_next_free_rec. An inode failing any other check is reported to userspace as SUCCESS while still corrupt. Revalidate after the repairs and return the residual code. Documentation/filesystems/ocfs2-online-filecheck.rst already describes
this: "the inode must first be checked for errors before performing a final fix".

Returning an error from the validate callback is not free, though. ocfs2_read_blocks() clears BH_Uptodate on failure, and ocfs2_read_locked_inode() bails out before upgrading the cluster lock to exclusive and calling ocfs2_write_block(). Simply revalidating at the end would therefore leave a dirty non-uptodate buffer in the block device
cache: the next OCFS2_BH_IGNORE_CACHE read of that block sees buffer_dirty(), skips submitting the read, and then fails -EIO on !buffer_uptodate(), so later filecheck requests report FAILED instead of the real reason. The half repaired dinode, stamped with a freshly computed ecc, would also be flushed by generic writeback without the exclusive cluster lock.

Snapshot the block instead and only mark it dirty once the repaired dinode validates. On residual failure restore the snapshot and return the error with the buffer clean, so the next read goes to disk as usual. Every early return restores it too, including the readonly and jbd gates.

Take the snapshot before the initial validation rather than after it. ocfs2_validate_meta_ecc() -> ocfs2_block_check_validate() applies a hamming fixup in place and restores only the ocfs2_block_check field on the way out, so when the post-fix crc32 still fails the flipped bit stays in bh->b_data and a later snapshot would record it. Undo that flip before repairing fields when the initial validation reported BLOCKECC:
the fixup did not produce a crc32 valid block, so the bit is bogus and recomputing the ecc over it would stamp the corruption with a valid checksum. An ecc check that passed leaves good data behind, a successful single bit fixup included, and is kept.

The ecc is recomputed before revalidating, otherwise a bad ecc would always fail the revalidation and roll back the ecc repair itself. Whether it needs recomputing is taken from the initial validation's error code rather than a second ocfs2_validate_meta_ecc() call, since any field repair invalidates the ecc anyway.

Also guard the l_next_free_rec repair with a valid l_count. Copying a zero or oversized l_count into l_next_free_rec spread the corruption to a second field and stamped it with a good ecc.

Inodes failing an unrepairable check now report INVALIDINO instead of SUCCESS. No new error code is introduced, so ocfs2_filecheck_errs[] and its userspace-visible names are unchanged. A failed repair no longer leaves any modification behind, and revalidation runs the ecc check a second time, which increments osb_ecc_stats again and emits a second ocfs2_filecheck_validate_inode_block trace event per fix request.

Fixes d56a8f32e4c6 ("ocfs2: check/fix inode block for online file check")
---
 fs/ocfs2/inode.c | 85 +++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 76 insertions(+), 9 deletions(-)
fs/ocfs2/inode.c +76 -9
index 9db147de1982..ad40cd6baaa0 100644
@@ -2062,9 +2062,25 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 {
 	int changed = 0;
 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
+	void *orig;
+	int rc;
 
-	if (!ocfs2_filecheck_validate_inode_block(sb, bh))
-		return 0;
+	/*
+	 * Snapshot the whole block before validating it, and keep the snapshot
+	 * until the repair is known to be good.  ocfs2_validate_meta_ecc()
+	 * mutates the block in place while attempting a hamming fix and leaves
+	 * the flipped bit behind when the crc32 still fails, so a snapshot
+	 * taken afterwards would record that stray flip rather than what was
+	 * read from disk.  A field by field undo would not restore it either.
+	 */
+	orig = kmalloc(sb->s_blocksize, GFP_NOFS);
+	if (!orig)
+		return -OCFS2_FILECHECK_ERR_FAILED;
+	memcpy(orig, bh->b_data, sb->s_blocksize);
+
+	rc = ocfs2_filecheck_validate_inode_block(sb, bh);
+	if (!rc)
+		goto out_free;
 
 	trace_ocfs2_filecheck_repair_inode_block(
 		(unsigned long long)bh->b_blocknr);
@@ -2074,7 +2090,8 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 		     "Filecheck: cannot repair dinode #%llu "
 		     "on readonly filesystem\n",
 		     (unsigned long long)bh->b_blocknr);
-		return -OCFS2_FILECHECK_ERR_READONLY;
+		rc = -OCFS2_FILECHECK_ERR_READONLY;
+		goto out_restore;
 	}
 
 	if (buffer_jbd(bh)) {
@@ -2082,21 +2099,35 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 		     "Filecheck: cannot repair dinode #%llu, "
 		     "its buffer is in jbd\n",
 		     (unsigned long long)bh->b_blocknr);
-		return -OCFS2_FILECHECK_ERR_INJBD;
+		rc = -OCFS2_FILECHECK_ERR_INJBD;
+		goto out_restore;
 	}
 
 	if (!OCFS2_IS_VALID_DINODE(di)) {
 		/* Cannot fix invalid inode block */
-		return -OCFS2_FILECHECK_ERR_INVALIDINO;
+		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto out_restore;
 	}
 
 	if (ocfs2_dinode_has_no_valid_flag(di)) {
 		/* Cannot just add VALID_FL flag back as a fix,
 		 * need more things to check here.
 		 */
-		return -OCFS2_FILECHECK_ERR_VALIDFLAG;
+		rc = -OCFS2_FILECHECK_ERR_VALIDFLAG;
+		goto out_restore;
 	}
 
+	/*
+	 * A failed ecc check means the hamming fixup did not produce a crc32
+	 * valid block, so the bit it flipped is bogus.  Undo that before
+	 * repairing fields, otherwise the ecc recompute below would stamp the
+	 * stray flip with a valid checksum and write it out.  An ecc check that
+	 * passed leaves good data behind, a successful single bit fixup
+	 * included, and must be kept.
+	 */
+	if (rc == -OCFS2_FILECHECK_ERR_BLOCKECC)
+		memcpy(bh->b_data, orig, sb->s_blocksize);
+
 	if (ocfs2_dinode_has_wrong_blkno(di, bh->b_blocknr)) {
 		di->i_blkno = cpu_to_le64(bh->b_blocknr);
 		changed = 1;
@@ -2115,7 +2146,14 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 		     le32_to_cpu(di->i_fs_generation));
 	}
 
+	/*
+	 * Only copy l_count into l_next_free_rec when l_count itself is
+	 * sane, otherwise the repair would spread the corruption to a
+	 * second field and stamp it with a freshly computed ecc.
+	 */
 	if (ocfs2_dinode_has_extents(di) &&
+	    !ocfs2_dinode_has_zero_extent_count(di) &&
+	    !ocfs2_dinode_has_oversized_extent_count(sb, di) &&
 	    ocfs2_dinode_has_bad_extent_next_free(di)) {
 		di->id2.i_list.l_next_free_rec = di->id2.i_list.l_count;
 		changed = 1;
@@ -2125,15 +2163,44 @@ static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
 		     le16_to_cpu(di->id2.i_list.l_next_free_rec));
 	}
 
-	if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) {
+	/*
+	 * Recompute the ecc before revalidating, but do not mark the buffer
+	 * dirty yet.  The initial validation already told us whether the ecc
+	 * was bad, and any field repair above invalidates it anyway.
+	 */
+	if (changed || rc == -OCFS2_FILECHECK_ERR_BLOCKECC) {
 		ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check);
-		mark_buffer_dirty(bh);
 		mlog(ML_ERROR,
 		     "Filecheck: reset dinode #%llu: compute meta ecc\n",
 		     (unsigned long long)bh->b_blocknr);
 	}
 
-	return 0;
+	/*
+	 * Only i_blkno, fs_generation and l_next_free_rec are repairable, so
+	 * report whatever is still wrong instead of claiming success.
+	 *
+	 * Validate before dirtying.  ocfs2_read_blocks() clears BH_Uptodate
+	 * when the validate callback fails, and a dirty non-uptodate buffer
+	 * left in the block device cache makes every later
+	 * OCFS2_BH_IGNORE_CACHE read of this block skip the disk read and
+	 * fail with -EIO.  ocfs2_read_locked_inode() also bails out before
+	 * upgrading to an exclusive cluster lock and writing the block, so
+	 * the repair would be flushed later without that lock.  Roll back
+	 * instead, leaving the buffer clean.
+	 */
+	rc = ocfs2_filecheck_validate_inode_block(sb, bh);
+	if (rc)
+		goto out_restore;
+
+	mark_buffer_dirty(bh);
+	goto out_free;
+
+out_restore:
+	/* Leave the buffer exactly as it was read from disk. */
+	memcpy(bh->b_data, orig, sb->s_blocksize);
+out_free:
+	kfree(orig);
+	return rc;
 }
 
 static int
Signature · 1 line
2.39.3
author maintainer
#23 lore raw

PATCH v3 23/31 ocfs2: check dinode nlink and mode in online filecheck

In reply to Joseph Qi

The filecheck validator shares most of its conditions with ocfs2_validate_inode_block(), but skips the zero nlink or mode check. Wire it in using the predicate the two now share.

Filecheck reports OCFS2_FILECHECK_ERR_INVALIDINO and logs with the Filecheck prefix rather than calling ocfs2_error(), which would escalate to ocfs2_handle_error() and could force the filesystem read-only while an administrator is only asking for a diagnosis.

The check is placed after the ecc and signature gate so that the existing coupling is preserved: a bad signature still overrides a checksum failure with INVALIDINO, and every later check still runs only on an ecc-clean block.

---
 fs/ocfs2/inode.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/ocfs2/inode.c +9 -0
index ad40cd6baaa0..6d2fb46e6c38 100644
@@ -2020,6 +2020,15 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
+	if (ocfs2_dinode_has_zero_nlink_or_mode(di)) {
+		mlog(ML_ERROR,
+		     "Filecheck: invalid dinode #%llu: nlink = %u or mode = %u\n",
+		     (unsigned long long)bh->b_blocknr,
+		     ocfs2_read_links_count(di), le16_to_cpu(di->i_mode));
+		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto bail;
+	}
+
 	if (!ocfs2_valid_inode_mode(le16_to_cpu(di->i_mode))) {
 		mlog(ML_ERROR,
 		     "Filecheck: invalid dinode #%llu: mode 0%o has unknown file type\n",
Signature · 1 line
2.39.3
author maintainer
#24 lore raw

PATCH v3 24/31 ocfs2: check dinode suballoc slot in online filecheck

In reply to Joseph Qi

Wire both suballoc slot predicates into the filecheck validator. An OCFS2_INVALID_SLOT on a non-system inode and an out-of-range slot keep their separate messages, so the log still distinguishes the two.

The range predicate excludes OCFS2_INVALID_SLOT, so system inodes created by mkfs.ocfs2 from the global allocator are still accepted.

---
 fs/ocfs2/inode.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
fs/ocfs2/inode.c +18 -0
index 6d2fb46e6c38..10eaa2c4445e 100644
@@ -2029,6 +2029,24 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
+	if (ocfs2_dinode_has_invalid_suballoc_slot(di)) {
+		mlog(ML_ERROR,
+		     "Filecheck: invalid dinode #%llu: suballoc slot %u for non-system inode\n",
+		     (unsigned long long)bh->b_blocknr,
+		     le16_to_cpu(di->i_suballoc_slot));
+		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto bail;
+	}
+
+	if (ocfs2_dinode_has_out_of_range_suballoc_slot(sb, di)) {
+		mlog(ML_ERROR,
+		     "Filecheck: invalid dinode #%llu: suballoc slot %u\n",
+		     (unsigned long long)bh->b_blocknr,
+		     le16_to_cpu(di->i_suballoc_slot));
+		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto bail;
+	}
+
 	if (!ocfs2_valid_inode_mode(le16_to_cpu(di->i_mode))) {
 		mlog(ML_ERROR,
 		     "Filecheck: invalid dinode #%llu: mode 0%o has unknown file type\n",
Signature · 1 line
2.39.3
author maintainer
#25 lore raw

PATCH v3 25/31 ocfs2: check dinode suballoc bit in online filecheck

In reply to Joseph Qi

Wire the suballoc bit range predicate into the filecheck validator. A suballocator group bitmap lives in a single block, so a bit at or beyond ocfs2_suballoc_bits_per_block() can never be valid; accepting it would later trip the BUG_ON() in _ocfs2_free_suballoc_bits() when the inode is deleted.

---
 fs/ocfs2/inode.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/ocfs2/inode.c +9 -0
index 10eaa2c4445e..bb87878200a3 100644
@@ -2047,6 +2047,15 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
+	if (ocfs2_dinode_has_bad_suballoc_bit(sb, di)) {
+		mlog(ML_ERROR,
+		     "Filecheck: invalid dinode #%llu: suballoc bit %u\n",
+		     (unsigned long long)bh->b_blocknr,
+		     le16_to_cpu(di->i_suballoc_bit));
+		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto bail;
+	}
+
 	if (!ocfs2_valid_inode_mode(le16_to_cpu(di->i_mode))) {
 		mlog(ML_ERROR,
 		     "Filecheck: invalid dinode #%llu: mode 0%o has unknown file type\n",
Signature · 1 line
2.39.3
author maintainer
#26 lore raw

PATCH v3 26/31 ocfs2: check dinode orphaned slots in online filecheck

In reply to Joseph Qi

Wire both orphaned slot predicates into the filecheck validator. Each predicate keeps its flag guard, so a stale slot field on an inode that is not orphaned is still accepted.

---
 fs/ocfs2/inode.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
fs/ocfs2/inode.c +18 -0
index bb87878200a3..1ae42ac245cb 100644
@@ -2056,6 +2056,24 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
+	if (ocfs2_dinode_has_bad_orphaned_slot(sb, di)) {
+		mlog(ML_ERROR,
+		     "Filecheck: invalid dinode #%llu: orphaned slot %u\n",
+		     (unsigned long long)bh->b_blocknr,
+		     le16_to_cpu(di->i_orphaned_slot));
+		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto bail;
+	}
+
+	if (ocfs2_dinode_has_bad_dio_orphaned_slot(sb, di)) {
+		mlog(ML_ERROR,
+		     "Filecheck: invalid dinode #%llu: DIO orphaned slot %u\n",
+		     (unsigned long long)bh->b_blocknr,
+		     le16_to_cpu(di->i_dio_orphaned_slot));
+		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto bail;
+	}
+
 	if (!ocfs2_valid_inode_mode(le16_to_cpu(di->i_mode))) {
 		mlog(ML_ERROR,
 		     "Filecheck: invalid dinode #%llu: mode 0%o has unknown file type\n",
Signature · 1 line
2.39.3
author maintainer
#27 lore raw

PATCH v3 27/31 ocfs2: check inline data dinodes in online filecheck

In reply to Joseph Qi

Wire the inline-data guard and its three sub-checks into the filecheck
validator: allocated clusters on an inline inode, an id_count beyond the space available in the inode block, and an i_size beyond id_count.

The size-without-clusters check now needs an explicit goto bail. It previously fell through to the bail label because it was the last check; with checks following it, a later failure would overwrite its INVALIDINO code and report the wrong reason.

The id_count maximum stays a signed int in the message. ocfs2_max_inline_data_with_xattr() subtracts the unvalidated i_xattr_inline_size and can go negative on small block sizes.

---
 fs/ocfs2/inode.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
fs/ocfs2/inode.c +34 -0
index 1ae42ac245cb..e01c3078d974 100644
@@ -2105,6 +2105,40 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 			     (unsigned long long)bh->b_blocknr,
 			     (unsigned long long)le64_to_cpu(di->i_size));
 		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto bail;
+	}
+
+	if (ocfs2_dinode_has_inline_data(di)) {
+		struct ocfs2_inline_data *data = &di->id2.i_data;
+
+		if (ocfs2_dinode_has_inline_clusters(di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: %u clusters\n",
+			     (unsigned long long)bh->b_blocknr,
+			     le32_to_cpu(di->i_clusters));
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+
+		if (ocfs2_dinode_has_oversized_inline_data(sb, di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: inline data id_count %u exceeds max %d\n",
+			     (unsigned long long)bh->b_blocknr,
+			     le16_to_cpu(data->id_count),
+			     ocfs2_max_inline_data_with_xattr(sb, di));
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+
+		if (ocfs2_dinode_has_inline_size_over_count(di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: inline data i_size %llu exceeds id_count %u\n",
+			     (unsigned long long)bh->b_blocknr,
+			     (unsigned long long)le64_to_cpu(di->i_size),
+			     le16_to_cpu(data->id_count));
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
 	}
 
 bail:
Signature · 1 line
2.39.3
author maintainer
#28 lore raw

PATCH v3 28/31 ocfs2: check fast symlink dinodes in online filecheck

In reply to Joseph Qi

Wire the fast symlink guard and its two sub-checks into the filecheck
validator: an i_size beyond the space available for id2.i_symlink, and a target that is not NUL-terminated at i_size.

ocfs2_dinode_has_unterminated_fast_symlink() bounds its own strnlen() against ocfs2_fast_symlink_chars(), so this caller does not have to rely on the size check having run first to stay inside the inode block.

---
 fs/ocfs2/inode.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
fs/ocfs2/inode.c +24 -0
index e01c3078d974..132864f860fa 100644
@@ -2141,6 +2141,30 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		}
 	}
 
+	if (ocfs2_dinode_is_fast_symlink(di)) {
+		int max_inline = ocfs2_fast_symlink_chars(sb);
+		u64 i_size = le64_to_cpu(di->i_size);
+
+		if (ocfs2_dinode_has_oversized_fast_symlink(sb, di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: fast symlink i_size %llu exceeds max %d\n",
+			     (unsigned long long)bh->b_blocknr,
+			     (unsigned long long)i_size,
+			     max_inline - 1);
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+
+		if (ocfs2_dinode_has_unterminated_fast_symlink(sb, di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: fast symlink is not NUL-terminated at i_size %llu\n",
+			     (unsigned long long)bh->b_blocknr,
+			     (unsigned long long)i_size);
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+	}
+
 bail:
 	return rc;
 }
Signature · 1 line
2.39.3
author maintainer
#29 lore raw

PATCH v3 29/31 ocfs2: check chain list dinodes in online filecheck

In reply to Joseph Qi

Wire the chain-list guard and its three sub-checks into the filecheck
validator: cl_count against ocfs2_chain_recs_per_inode(), cl_next_free_rec against cl_count, and cl_bpc against the cluster to block size ratio.

The bits-per-cluster predicate keeps the two bitmap_blkno conditions, so the global bitmap inode itself remains exempt.

---
 fs/ocfs2/inode.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
fs/ocfs2/inode.c +31 -0
index 132864f860fa..7e299b2dc8c0 100644
@@ -2165,6 +2165,37 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		}
 	}
 
+	if (ocfs2_dinode_has_chain_list(di)) {
+		struct ocfs2_chain_list *cl = &di->id2.i_chain;
+
+		if (ocfs2_dinode_has_bad_chain_count(sb, di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: chain list count %u\n",
+			     (unsigned long long)bh->b_blocknr,
+			     le16_to_cpu(cl->cl_count));
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+
+		if (ocfs2_dinode_has_bad_chain_next_free(di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: chain list index %u\n",
+			     (unsigned long long)bh->b_blocknr,
+			     le16_to_cpu(cl->cl_next_free_rec));
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+
+		if (ocfs2_dinode_has_bad_chain_bpc(sb, di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: bits per cluster %u\n",
+			     (unsigned long long)bh->b_blocknr,
+			     le16_to_cpu(cl->cl_bpc));
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+	}
+
 bail:
 	return rc;
 }
Signature · 1 line
2.39.3
author maintainer
#30 lore raw

PATCH v3 30/31 ocfs2: check dinode refcount location in online filecheck

In reply to Joseph Qi

Wire the OCFS2_HAS_REFCOUNT_FL without i_refcount_loc predicate into the filecheck validator. A reflink-capable inode with no refcount tree location cannot be reflinked or have its refcount tree removed cleanly.

---
 fs/ocfs2/inode.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ocfs2/inode.c +8 -0
index 7e299b2dc8c0..6a7dff8a6a05 100644
@@ -2196,6 +2196,14 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		}
 	}
 
+	if (ocfs2_dinode_has_refcount_without_loc(di)) {
+		mlog(ML_ERROR,
+		     "Filecheck: inode #%llu has refcount flag but no i_refcount_loc\n",
+		     (unsigned long long)bh->b_blocknr);
+		rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+		goto bail;
+	}
+
 bail:
 	return rc;
 }
Signature · 1 line
2.39.3
author maintainer
#31 lore raw

PATCH v3 31/31 ocfs2: check extent list dinodes in online filecheck

In reply to Joseph Qi

Wire the extent list guard and its three sub-checks into the filecheck
validator: a zero l_count, an l_count beyond ocfs2_extent_recs_per_inode(), and an l_next_free_rec beyond l_count.

This brings the filecheck validator to parity with ocfs2_validate_inode_block() for every dinode check except the inline xattr one. ocfs2_validate_inode_xattr() calls ocfs2_error() through its helpers in xattr.c, so sharing it would mean threading a reporting flag down into that file; it is deliberately left out.

The l_next_free_rec predicate is already used by the repair path, which fixes that field, so a repair of it now revalidates cleanly.

---
 fs/ocfs2/inode.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
fs/ocfs2/inode.c +31 -0
index 6a7dff8a6a05..8e5f7a240381 100644
@@ -2204,6 +2204,37 @@ static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
 		goto bail;
 	}
 
+	if (ocfs2_dinode_has_extents(di)) {
+		struct ocfs2_extent_list *el = &di->id2.i_list;
+		u16 count = le16_to_cpu(el->l_count);
+		u16 next_free = le16_to_cpu(el->l_next_free_rec);
+
+		if (ocfs2_dinode_has_zero_extent_count(di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: extent list l_count is zero\n",
+			     (unsigned long long)bh->b_blocknr);
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+
+		if (ocfs2_dinode_has_oversized_extent_count(sb, di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: extent list l_count %u exceeds max %u\n",
+			     (unsigned long long)bh->b_blocknr, count,
+			     ocfs2_extent_recs_per_inode(sb));
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+
+		if (ocfs2_dinode_has_bad_extent_next_free(di)) {
+			mlog(ML_ERROR,
+			     "Filecheck: invalid dinode #%llu: extent list l_next_free_rec %u exceeds l_count %u\n",
+			     (unsigned long long)bh->b_blocknr, next_free, count);
+			rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
+			goto bail;
+		}
+	}
+
 bail:
 	return rc;
 }
Signature · 1 line
2.39.3
#32 lore raw
In reply to Joseph Qi
Quoted 5 lines · On Mon, 9 Nov 2026 20:20:50 +0800 Joseph Qi <[email protected]>…

ocfs2_validate_inode_block() is a flat sequence of about twenty open-coded dinode checks. The filecheck variant duplicates eight of them but reports
differently: mlog() plus an OCFS2_FILECHECK_ERR_* code instead of ocfs2_error(), which can force the filesystem read-only. The duplicated conditions had drifted apart, and filecheck silently skipped the rest.

Thanks. I'll add all this and feed it into linux-next.

Sashiko is almost happy, one thing which looks hard-to-hit.

	https://sashiko.dev/#/patchset/[email protected]

I'll hopefully feed all this into my LLM-driven live kernel testing setup, which is pretty comprehensive. First I need to address some reliability issues then make it not take 48 hours for an "overnight" testing job!