* [V2 PATCH 00/59] Validate journal log items during log recovery.
@ 2026-11-16 23:14 Dave Chinner
2026-11-16 23:14 ` [PATCH 01/59] xfs: set the unmount record operation header length Dave Chinner
` (58 more replies)
0 siblings, 59 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Log recovery reads log items straight from the journal and acts on
them with very little checking. Validation of each item type is
scattered and much of it runs only as the item is being applied to
the filesystem. Corrupt or malicious log contents can therefore be
partially acted on before anything notices they are wrong, and the
checks that do exist are inconsistent from one item type to the
next.
This series makes log item validation a first-class, up-front step.
As each item is decoded from the journal it is fully validated
before any recovery code acts on it, so downstream recovery can
trust the item's structure and contents. Existing validation is
scattered through the commit handlers, and is largely ad hoc in
nature.
This version starts with fixes for existing bugs. Exact validation
exposed them, or depends on the state they leave behind. They carry
Fixes tags and do not depend on the validation patches. Of note:
- Several btree root level checks were off by one. The bmap btree
maximum height calculation was one level short, and the rt rmap
and rt refcount root checks allowed a root one level too deep.
Validation bounds logged roots by these heights, so they must be
exact, and they should be consistent across all btree types. The
minimum log size calculation keeps using the old bmap btree
height, so existing filesystems still mount.
- Growfs, and recovery of a growfs, leave mount state derived from
the superblock geometry stale. This includes the rt btree heights,
the transaction reservations, the realtime extent geometry and the
mount features. Recovery validates and replays items against that
state, so it is recomputed when growfs runs, when a superblock
change is replayed, and after the log has been recovered. The
mount's own superblock updates are applied after the post-recovery
superblock re-read, so recovery no longer discards them.
- Changing max_atomic_write on remount ran the minimum log size
calculation on a live filesystem, which temporarily changes
mount-wide btree heights. It is replaced with a check of the
atomic write reservation against what the current log can hold.
Mount teardown after a failed growfs, or a failed growfs recovery,
could also try to free groups that were never set up.
The new validation is driven by per-region and per-item-type
callbacks on the recovery item operations, so every operation
header, region and item type is checked the same way. First we
validate the ophdr based record framing, then we reassemble whole
regions, and finally we validate whole items.
The scope is the on-disk log format and the in-core structures
recovery builds from it. Every log item type is covered. The
validation is what can be checked from the log item alone together
with stable mount and superblock geometry.
Some checks cannot be made at decode time and are deliberately left
to the first runtime access after recovery. Realtime group bounds
are not available because the realtime inodes are not set up until
after recovery. Filesystem block number bounds cannot be trusted at
decode time because a growfs superblock update is not replayed into
the in-core superblock until pass two. Hence extent and pointer block
range validation will need to be reworked before it can be relied
on.
More detail on these issues can be found under known issues in
the version 1 changelog. Version 2 does not address them; the
changes in V2 are a result of LLM reviews uncovering pre-existing
bugs and issues in the initial validation code that needed to be
fixed first.
The known issues will take a little time to be addressed, so I'd
prefer to get feedback on the rest of the patchset before I spend
time reworking that aspect of it. Early(-ish) feedback on the
structure and implementation as it currently stands is more
important right now.
The patchset passes all of fstests on 1k and 4k block sizes (except
for the couple of growfs/recovery stress tests) without regressions,
so it largely works for known good journals. Anyone who has been
fuzzing journal structures with LLMs and/or modified syzbots are
welcome to try it and see what holes they can find in it.
The entire series can be pulled from here:
https://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.git log-verification-2
Known Issues:
1. Validation runs repeatedly on every recovery pass
Except for the CRC validation pass.
Pass 1 needs to validate all the transaction/checkpoint structures
as we walk the entire active journal to find cancellation items. We
also need to validate buffer and quotaoff items as they are recorded
in cancelation tables.
Pass 2 readahead needs items that are read ahead to be validated
sufficiently to ensure the location being read from disk is likely
to be OK.
Pass 2 needs to fully validate all the items being recovered, before
they are recovered.
Some of the item recovery also reformats the items being recovered;
things like 32bit vs 64bit incore structure differences are being
resolved at item validation time so they don't need to be handled
differently by validation or recovery code. This means that
validation needs to run on those items on every pass that they will
have something run against them. This needs more thought; inodes are
the main issue here, and largely they are only parsed in pass 2.
Hence we might be able to split up validation into pass-1 + pass-2
variants. pass-1 needs to validate the number and size of regions
matches the format header to ensure checkpoint decoding is always
valid. pass-2 can then do the full item validation.
2. Geometry validation versus growfs and shrink
This is an extension of issue 1, in that validation occurs before
replay. The geometry items validate against is the geometry in
effect at the start of recovery. A geometry change only takes effect
when the primary superblock buffer is replayed, which happens during
transaction recovery in the second pass, not during checkpoint
region decoding.
A growfs operation logs the superblock geometry change ahead of any
metadata that uses the new geometry. A shrink also logs the
superblock geometry change after relevant internal metadata has been
updated and/or invalidated. Hence if the superblock geometry changes
during recovery, item validation at region decode time will fail to
validate geometry-based fields correctly.
This affects any item that carries a type that encodes geometry
information, such as inode numbers, disk addresses, and extents,
including the realtime variants. Structural validation that does
not depend on geometry is unaffected.
As the geometry change only takes effect during transaction
recovery, any validation that depends on it must run after the
superblock buffer has been replayed.
This causes issues for readahead validation, which decodes
addresses and submits IO, potentially for addresses outside the
range of the initial superblock geometry. Readahead silently
discards reads beyond the end of the filesystem, but we shouldn't
issue them in the first place.
Even in pass 2, the checkpoint where the geometry change is
recorded cannot be validated until recovery even though items in the
checkpoint are correctly ordered either side of the change.
Hence it is likely that we need two layers of item validation for
pass 2 - the decode time validation of everything except disk
addresses, and recovery time validation of addresses. This will
order all disk address based validation correctly against
grow/shrink operations.
3. fold ->complete() into ->validate_item()
The xlog_recover_item_ops ->complete() callback was added early on,
when it was not entirely clear how item validation would end up
looking. In the end, it is used by only one special item type:
the transaction header.
->validate_item() already runs on every item at that same point, so
->complete() probably should be folded into it. ->validate_item()
would perform the header copy and signal that the item is consumed,
removing the need for ->complete() altogether.
Changelog:
Version 2:
- New patch: fix the off-by-one rt rmap btree maximum level checks in
the root loader, block verifier and inode repair, allowing an empty
level zero root before growfs adds the first realtime volume.
- New patch: allow an empty level zero rt refcount btree root before
growfs adds the first realtime volume, and make inode repair use the
same root level rules as the loader.
- New patch: recompute m_rtbtree_maxlevels wherever growfs recomputes
the rt btree maximum heights.
- New patch: re-derive the mount features and recompute the rt btree
maximum heights when the primary superblock is replayed, and abort
recovery if it adds a realtime volume without a realtime device.
- New patch: move the post-recovery superblock re-read out of log
recovery into the mount code, and reinitialise the mount geometry
state derived from it after a dirty log has been recovered.
- New patch: apply the mount's in-core superblock updates after the
post-recovery superblock re-read so they are not discarded on mounts
that recover the log.
- Metadata btree root validation now uses the same root level and record
count rules as the runtime loaders, accepting the empty level zero
root growfs creates before the first realtime volume, and gates each
root on the btree feature.
- New patch: reject logged btree fork roots whose compact on-disk form
does not fit within the inode fork, matching the runtime root
loaders.
- New patch: fix the bmap btree maximum height calculation to include
the inode root level, change the bmap btree root level checks to
match, and keep the legacy calculation only for the minimum log
size.
- New patch: check the remount max_atomic_write reservation against the
largest reservation the current log can hold, instead of running the
minimum log size calculation on a live filesystem
- New patch: recompute the transaction reservations after growfs changes
the rt btree heights, keeping a configured atomic ioend reservation
- check for a missing realtime device before updating the in-core
superblock during superblock recovery, so a failed mount does not free
rtgroups that were never created
- update the mount features before setting up new rtgroups during
superblock recovery, so the first rtgroup excludes the realtime
superblock
- recompute the realtime extent size geometry when recovering a
superblock change
- recompute the realtime extent size geometry when reinitialising the
filesystem geometry after log recovery
- compute the minimum log size attr reservation with the legacy bmap
btree height, so the bmap btree height fix does not change the minimum
log size, and set up the legacy btree heights for the whole minimum
log size calculation
- new patch: skip group indexes with no instantiated group when freeing
groups, so teardown after a failed growfs or growfs recovery does not
dereference a NULL group
- document that full item validation bounds the realtime btree inode
fork root level by the largest height any geometry allows until the
recovery geometry problem is solved
- bound logged realtime btree inode fork root levels by the largest
height any geometry allows, so recovery of a growfs that adds the
first realtime volume is not rejected against the pre-recovery
geometry
- noted the pre-existing growfs vs max_atomic_write remount race in the
trans resv recalc patch header and comment
- added a Fixes tag to the rt geometry superblock recovery patch
- Restored the XMI ->validate_region()/->validate_item() wiring and
commit handler cleanup that were dropped from the mapping exchange
intent patch when v1 was rebased.
- Fail recovery when a commit record arrives while a log item is still
being assembled, instead of silently dropping the incomplete item.
- The unmount record check during the head and tail scan no longer fails
the mount: a record that fails validation leaves the log dirty for
head verification and recovery, validation there is quiet, and the
unmount magic is accepted in either byte order.
- Zero the buffers allocated for EFI and EFD host format conversion so
structure padding is not left uninitialised.
- Check the logged dquot buffer offset against the location fixed by the
dquot ID, and bound the dqblk within the cluster buffer without
arithmetic that can wrap on 32-bit.
- Zero the buffer allocated for inode log format host conversion so the
padding field is not left uninitialised.
- Check the logged inode buffer block and offset against the location
fixed by the inode number for all inode chunk geometries, and bound
the inode within the cluster buffer.
- Report bad logged extent records against the validated format header
inode number rather than reading di_ino, which is beyond the end of a
V2 log dinode.
- Buffer item validation now accepts an unknown buffer type on non-CRC
filesystems, rejects a non-cancelled buffer with no dirty regions, and
the header notes that split page-boundary regions are no longer
accepted.
- The redundant buffer replay checks patch header now notes that the
removed handling for split page-boundary regions is rejected by
validation.
- Only read the dquot buffer data region after checking the item has
exactly two regions.
- Logged bmap btree root validation now bounds the root level at one
less than the corrected maximum height, matching the root loader.
- rebased onto v7.4-rc1; __xfs_inode_fork_size() uses the new
XFS_FORKOFF_TO_B() conversion macro
- rewrote the headers of the documentation and transaction header
refactoring patches to remove stale AI prompt tags and add Assisted-by
and Signed-off-by tags
- rewrote the log recovery validation design document to describe the
implemented four layer design and the open growfs geometry problem,
removed the implementation plan, and folded all later doc changes into
the doc patch
- reject a zero length op header that opens any region other than the
checkpoint transaction header, or that completes a region
- fail recovery with -EFSCORRUPTED on an unmount record inside an open
checkpoint instead of warning and discarding the checkpoint
- dropped the redundant host format size comparison from the EFI region
length check
- use SHUTDOWN_LOG_IO_ERROR for the unmount record op header length
check shutdown so it does not force the log and deadlock against the
CIL push that called it
- corrected the design doc commit message (four layers, no
implementation plan)
- design doc: quota off regions must be 160 or 88 bytes with valid quota
off flags, and inode/dquot buffers may be logged without their type
flag
- quota off recovery now accepts the 160 and 88 byte regions that v5.14
kernels logged, and checks the quota off flags
- buffer item recovery accepts inode and dquot buffers logged without
their type flag (V4 inode allocation, online repair), and the repair
code explains why it doesn't set the flag
- dquot buffer recovery comment and commit message account for online
repair logging dquot buffers without the type flag
Version 1:
- Initial version.
- https://lore.kernel.org/linux-xfs/20261013060204.3138690-1-dgc@kernel.org/
- https://git.kernel.org/pub/scm/linux/kernel/git/dgc/linux-xfs.git log-verification-1
Commits:
45b1d70066c7 xfs: set the unmount record operation header length
f1199ef01d51 xfs: fix the rt rmap btree maximum level checks
b42451a39759 xfs: allow an empty rt refcount btree root before the rt volume exists
0bc1f0d3a14d xfs: recompute the rt btree maxlevels when growfs changes them
1051b2bb4bbd xfs: recompute the transaction reservations after growfs
01eb31903a63 xfs: skip missing groups when freeing them
8a62f4b00eac xfs: update the rt geometry when recovering a superblock change
7216d4a64759 xfs: reinitialise the filesystem geometry after log recovery
afaf768b097a xfs: apply mount superblock updates after log recovery
ed3fc2f8a765 xfs: check the atomic write reservation against the current log size
298f54b2ce69 xfs: fix the bmap btree maximum height calculation
b03a3941bad5 xfs: add log recovery item validation design document
bc1fea813fb1 xfs: lift transaction header parsing out of the region assembly
a20a7f77bd02 xfs: track transaction header decode state explicitly
1347f4fe9f5a xfs: track the log item being rebuilt with an explicit pointer
fb996aa82b19 xfs: complete log items explicitly during recovery
1296a786754f xfs: add log item validation infrastructure to log recovery
250789ccb8c2 xfs: recover the transaction header as a validated item type
0673f7673082 xfs: factor out xlog_recover_validate_ophdr()
edeb9a121555 xfs: validate operation header length and alignment during recovery
4baf15d9e2b9 xfs: validate the operation header padding during recovery
7e57a8976e19 xfs: validate the operation header flags during recovery
55c3a57b62f6 xfs: enforce zero length control records during recovery
8cf887589d89 xfs: validate the unmount record during recovery
73ea77f87940 xfs: handle zero length continuation op headers during recovery
bedc12862c52 xfs: bound the assembled region size during recovery
13c7907b8278 xfs: dump the corrupt region when log item validation fails
1fb1b54316f9 xfs: validate inode create and quota off log items during recovery
cd26dbdbe15b xfs: validate extent free log items during recovery
9daa54c92774 xfs: validate and convert extent free intents during recovery
b8519bbce522 xfs: validate and convert extent free done items during recovery
c5b15f11306c xfs: validate rmap update intents during recovery
09207a252d5a xfs: validate rmap update done items during recovery
518a2de12579 xfs: validate refcount update intents during recovery
f281914d5bc3 xfs: validate refcount update done items during recovery
6bdd8d8b39ae xfs: validate bmap update intents during recovery
89f9f006ca26 xfs: validate bmap update done items during recovery
b9237a533bb1 xfs: validate mapping exchange intents during recovery
371b3d5b8955 xfs: validate mapping exchange done items during recovery
2def519ac4b5 xfs: validate dquot items during recovery
905a22386578 xfs: validate attr done items during recovery
d6446ad17c86 xfs: validate attr intents during recovery
e83627742991 xfs: convert inode items to host format during recovery
88ad40085e34 xfs: allow xfs_imap() to map without an inode btree lookup
c5e34f4114f2 xfs: validate the inode item format header during recovery
99fdd487b959 xfs: validate the logged inode core during recovery
a4e13d066360 xfs: validate the logged inode forks during recovery
c911386a0470 xfs: validate logged local fork contents during recovery
0a931c845ded xfs: validate logged extent fork contents during recovery
f5a8d1d9ddf3 xfs: allow a long format btree root to be checked without a cursor
0a1a37173833 xfs: validate logged btree fork roots during recovery
6a8381aee121 xfs: validate logged metadata btree fork roots during recovery
391dd3bf9056 xfs: check logged btree fork roots fit in the inode fork during recovery
0caea84f2127 xfs: validate buffer item region count during recovery
93759056d8d4 xfs: validate buffer item regions during recovery
dec0ab7ab4f5 xfs: validate the whole buffer item during recovery
70209b185ac5 xfs: remove redundant buffer checks from recovery replay
99549fb7954f xfs: fail recovery on a buffer type mismatch
0d3a450a0106 xfs: validate logged dquot buffers during recovery
Diffstat:
Documentation/filesystems/xfs/index.rst | 1 +-
Documentation/filesystems/xfs/xfs-log-recovery-validation-design.rst | 672 ++++-
fs/xfs/libxfs/xfs_bmap.c | 96 +-
fs/xfs/libxfs/xfs_bmap.h | 10 +-
fs/xfs/libxfs/xfs_bmap_btree.c | 2 +-
fs/xfs/libxfs/xfs_btree.c | 50 +-
fs/xfs/libxfs/xfs_btree.h | 4 +-
fs/xfs/libxfs/xfs_dquot_buf.c | 64 +-
fs/xfs/libxfs/xfs_format.h | 8 +-
fs/xfs/libxfs/xfs_group.c | 9 +-
fs/xfs/libxfs/xfs_ialloc.c | 7 +-
fs/xfs/libxfs/xfs_inode_buf.c | 19 +-
fs/xfs/libxfs/xfs_inode_buf.h | 2 +-
fs/xfs/libxfs/xfs_inode_fork.c | 27 +-
fs/xfs/libxfs/xfs_log_format.h | 8 +-
fs/xfs/libxfs/xfs_log_recover.h | 64 +-
fs/xfs/libxfs/xfs_log_rlimit.c | 111 +-
fs/xfs/libxfs/xfs_quota_defs.h | 2 +-
fs/xfs/libxfs/xfs_rtrefcount_btree.c | 27 +-
fs/xfs/libxfs/xfs_rtrmap_btree.c | 29 +-
fs/xfs/libxfs/xfs_shared.h | 1 +-
fs/xfs/libxfs/xfs_trans_resv.c | 72 +-
fs/xfs/libxfs/xfs_trans_resv.h | 1 +-
fs/xfs/scrub/inode_repair.c | 44 +-
fs/xfs/scrub/quota_repair.c | 7 +-
fs/xfs/xfs_attr_item.c | 283 +-
fs/xfs/xfs_bmap_item.c | 109 +-
fs/xfs/xfs_buf_item_recover.c | 422 ++-
fs/xfs/xfs_dquot_item_recover.c | 157 +-
fs/xfs/xfs_exchmaps_item.c | 96 +-
fs/xfs/xfs_extfree_item.c | 367 +-
fs/xfs/xfs_fsops.c | 7 +-
fs/xfs/xfs_icache.h | 2 +-
fs/xfs/xfs_icreate_item.c | 18 +-
fs/xfs/xfs_inode.h | 43 +-
fs/xfs/xfs_inode_item_recover.c | 1089 ++++++-
fs/xfs/xfs_log.c | 16 +-
fs/xfs/xfs_log_recover.c | 857 ++++--
fs/xfs/xfs_mount.c | 139 +-
fs/xfs/xfs_mount.h | 9 +-
fs/xfs/xfs_refcount_item.c | 163 +-
fs/xfs/xfs_rmap_item.c | 163 +-
fs/xfs/xfs_rtalloc.c | 15 +-
fs/xfs/xfs_trace.h | 34 +-
44 files changed, 4340 insertions(+), 986 deletions(-)
^ permalink raw reply [flat|nested] 60+ messages in thread
* [PATCH 01/59] xfs: set the unmount record operation header length
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 02/59] xfs: fix the rt rmap btree maximum level checks Dave Chinner
` (57 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The unmount record is written to the log with a hand built operation
header passed directly to xlog_write() through xlog_write_one_vec().
The operation header length was never set, so the unmount record is
written with a zero length header even though it carries an unmount
log format structure. Recovery does not check the unmount record, so
the incorrect length goes unnoticed.
Before commit db357078b0423e ("xfs: introduce xlog_write_full()")
the operation header length was rewritten correctly by xlog_write(),
hence the incorrectly built unmount header went unnoticed. After
that commit, xlog_write() never rewrote operation header lengths for
regions that fit fully into the iclog. This change exposed the
problem with the unmount record header construction.
Set the operation header length when building the unmount record.
xlog_write_one_vec() now validates that the operation header length
is set correctly for the region being written, and shuts down the
filesystem if a malformed record write is detected.
Fixes: db357078b0423e ("xfs: introduce xlog_write_full()")
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/fs/xfs/xfs_log.c b/fs/xfs/xfs_log.c
index 74b324f915a1..ab7ee848562c 100644
--- a/fs/xfs/xfs_log.c
+++ b/fs/xfs/xfs_log.c
@@ -811,6 +811,7 @@ xlog_write_one_vec(
struct xfs_log_iovec *reg,
struct xlog_ticket *ticket)
{
+ struct xlog_op_header *ophdr = reg->i_addr;
struct xfs_log_vec lv = {
.lv_niovecs = 1,
.lv_iovecp = reg,
@@ -818,6 +819,20 @@ xlog_write_one_vec(
};
LIST_HEAD (lv_chain);
+ /*
+ * The caller builds the operation header itself, and xlog_write() no
+ * longer rewrites the length when the region is written whole. Make sure
+ * the length the caller recorded in the header matches the region, so a
+ * bad header is caught here rather than being written to the log.
+ */
+ if (reg->i_len != sizeof(*ophdr) + be32_to_cpu(ophdr->oh_len)) {
+ xfs_alert(log->l_mp,
+ "%s: region length %u does not match op header length %u",
+ __func__, reg->i_len, be32_to_cpu(ophdr->oh_len));
+ xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
+ return -EFSCORRUPTED;
+ }
+
/* account for space used by record data */
ticket->t_curr_res -= lv.lv_bytes;
@@ -842,6 +857,7 @@ xlog_write_unmount_record(
.ophdr = {
.oh_clientid = XFS_LOG,
.oh_tid = cpu_to_be32(ticket->t_tid),
+ .oh_len = cpu_to_be32(sizeof(struct xfs_unmount_log_format)),
.oh_flags = XLOG_UNMOUNT_TRANS,
},
.ulf = {
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 02/59] xfs: fix the rt rmap btree maximum level checks
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 01/59] xfs: set the unmount record operation header length Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 03/59] xfs: allow an empty rt refcount btree root before the rt volume exists Dave Chinner
` (56 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
m_rtrmap_maxlevels is the maximum height of the rt rmap btree, including the
inode root level. The deepest valid root level is therefore one less than
m_rtrmap_maxlevels. The root loader, the block verifier and the inode repair
code all reject only levels greater than m_rtrmap_maxlevels, so they accept a
root one level too deep.
This level mismatch results in the same off-by-one that commit cc3144da377d
fixed for the rt refcount btree when a cursor is built over a maximum height
btree. To avoid this out of bounds access past the end of the cursor, reject
levels at or above m_rtrmap_maxlevels instead.
There is a transient state on disk where an rtrmap inode has no contents,
because growfs can create rtrmap inodes dynamically. m_rtrmap_maxlevels is zero
until growfs adds the first realtime volume. The root loader and repair code
therefore accept an empty level zero root when m_rtrmap_maxlevels is zero. A
non-empty level zero root is a normal leaf root that is still checked to fit
within the inode fork.
Fixes: fc6856c6ff08 ("xfs: introduce realtime rmap btree ondisk definitions")
Fixes: f33659e8a114 ("xfs: wire up a new metafile type for the realtime rmap")
Fixes: 3dd3aba6b92b ("xfs: repair rmap btree inodes")
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_rtrmap_btree.c | 29 ++++++++++++++++++++++++-----
fs/xfs/scrub/inode_repair.c | 7 ++++++-
2 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_rtrmap_btree.c b/fs/xfs/libxfs/xfs_rtrmap_btree.c
index 2f00d0698ae1..ba0d4058e717 100644
--- a/fs/xfs/libxfs/xfs_rtrmap_btree.c
+++ b/fs/xfs/libxfs/xfs_rtrmap_btree.c
@@ -140,7 +140,7 @@ xfs_rtrmapbt_verify(
if (fa)
return fa;
level = be16_to_cpu(block->bb_level);
- if (level > mp->m_rtrmap_maxlevels)
+ if (level >= mp->m_rtrmap_maxlevels)
return __this_address;
return xfs_btree_fsblock_verify(bp, mp->m_rtrmap_mxr[level != 0]);
@@ -692,17 +692,36 @@ xfs_iformat_rtrmap(
numrecs = be16_to_cpu(dfp->bb_numrecs);
level = be16_to_cpu(dfp->bb_level);
- if (level > mp->m_rtrmap_maxlevels ||
- xfs_rtrmap_droot_space_calc(level, numrecs) > dsize) {
- xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
- return -EFSCORRUPTED;
+ if (level == 0) {
+ /*
+ * rtrmap inodes can be dynamically created by growfs, resulting
+ * in a transient state on disk where they can have no contents.
+ * Hence a rtrmap inode with an empty level zero root is a valid
+ * state. Until growfs adds the first realtime volume there is
+ * nothing to map, so the root must be empty.
+ */
+ if (mp->m_rtrmap_maxlevels == 0 && numrecs != 0)
+ goto out_corrupt;
+ } else if (level >= mp->m_rtrmap_maxlevels) {
+ /*
+ * m_rtrmap_maxlevels includes the inode root level, so the
+ * deepest valid root level is one less than that.
+ */
+ goto out_corrupt;
}
+ if (xfs_rtrmap_droot_space_calc(level, numrecs) > dsize)
+ goto out_corrupt;
+
broot = xfs_broot_alloc(xfs_ifork_ptr(ip, XFS_DATA_FORK),
xfs_rtrmap_broot_space_calc(level, numrecs));
if (broot)
xfs_rtrmapbt_from_disk(ip, dfp, dsize, broot);
return 0;
+
+out_corrupt:
+ xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
+ return -EFSCORRUPTED;
}
/* Convert in-memory form of btree root to on-disk form. */
diff --git a/fs/xfs/scrub/inode_repair.c b/fs/xfs/scrub/inode_repair.c
index a9ccf763c059..4486881329b9 100644
--- a/fs/xfs/scrub/inode_repair.c
+++ b/fs/xfs/scrub/inode_repair.c
@@ -969,8 +969,13 @@ xrep_dinode_bad_rtrmapbt_fork(
nrecs = be16_to_cpu(dfp->bb_numrecs);
level = be16_to_cpu(dfp->bb_level);
- if (level > sc->mp->m_rtrmap_maxlevels)
+ /* Same root level rules as xfs_iformat_rtrmap(). */
+ if (level == 0) {
+ if (sc->mp->m_rtrmap_maxlevels == 0 && nrecs != 0)
+ return true;
+ } else if (level >= sc->mp->m_rtrmap_maxlevels) {
return true;
+ }
if (xfs_rtrmap_droot_space_calc(level, nrecs) > dfork_size)
return true;
if (level > 0 && nrecs == 0)
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 03/59] xfs: allow an empty rt refcount btree root before the rt volume exists
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 01/59] xfs: set the unmount record operation header length Dave Chinner
2026-11-16 23:14 ` [PATCH 02/59] xfs: fix the rt rmap btree maximum level checks Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 04/59] xfs: recompute the rt btree maxlevels when growfs changes them Dave Chinner
` (55 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
There is a transient state on disk where an rtrefcount inode has no contents,
because growfs can create rtrefcount inodes dynamically. m_rtrefc_maxlevels is
zero until growfs adds the first realtime volume. Growfs loads an existing
rtrefcount inode before it adds the space and recomputes m_rtrefc_maxlevels. An
inode left behind by an earlier growfs that failed after creating it is loaded
while m_rtrefc_maxlevels is still zero.
Commit cc3144da377d changed the root loader to reject levels at or above
m_rtrefc_maxlevels. That rejects every level when m_rtrefc_maxlevels is zero,
including the empty level zero root. A retried growfs then fails on a valid
inode.
Accept an empty level zero root when m_rtrefc_maxlevels is zero. A non-empty
level zero root is a normal leaf root that is still checked to fit within the
inode fork.
The inode repair code rejects only levels greater than m_rtrefc_maxlevels, so it
disagrees with the root loader. Give it the same root level rules.
Fixes: cc3144da377d ("xfs: fix off-by-one in rtrefcount btree root level validation")
Fixes: 92b2019493d1 ("xfs: repair inodes that have a refcount btree in the data fork")
Cc: stable@vger.kernel.org # v6.14
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_rtrefcount_btree.c | 27 +++++++++++++++++++++++----
fs/xfs/scrub/inode_repair.c | 7 ++++++-
2 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_rtrefcount_btree.c b/fs/xfs/libxfs/xfs_rtrefcount_btree.c
index 697f5e622685..b91b2f1895ce 100644
--- a/fs/xfs/libxfs/xfs_rtrefcount_btree.c
+++ b/fs/xfs/libxfs/xfs_rtrefcount_btree.c
@@ -559,17 +559,36 @@ xfs_iformat_rtrefcount(
numrecs = be16_to_cpu(dfp->bb_numrecs);
level = be16_to_cpu(dfp->bb_level);
- if (level >= mp->m_rtrefc_maxlevels ||
- xfs_rtrefcount_droot_space_calc(level, numrecs) > dsize) {
- xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
- return -EFSCORRUPTED;
+ if (level == 0) {
+ /*
+ * rtrefcount inodes can be dynamically created by growfs,
+ * resulting in a transient state on disk where they can have no
+ * contents. Hence a rtrefcount inode with an empty level zero
+ * root is a valid state. Until growfs adds the first realtime
+ * volume there is nothing to count, so the root must be empty.
+ */
+ if (mp->m_rtrefc_maxlevels == 0 && numrecs != 0)
+ goto out_corrupt;
+ } else if (level >= mp->m_rtrefc_maxlevels) {
+ /*
+ * m_rtrefc_maxlevels includes the inode root level, so the
+ * deepest valid root level is one less than that.
+ */
+ goto out_corrupt;
}
+ if (xfs_rtrefcount_droot_space_calc(level, numrecs) > dsize)
+ goto out_corrupt;
+
broot = xfs_broot_alloc(xfs_ifork_ptr(ip, XFS_DATA_FORK),
xfs_rtrefcount_broot_space_calc(level, numrecs));
if (broot)
xfs_rtrefcountbt_from_disk(ip, dfp, dsize, broot);
return 0;
+
+out_corrupt:
+ xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
+ return -EFSCORRUPTED;
}
/*
diff --git a/fs/xfs/scrub/inode_repair.c b/fs/xfs/scrub/inode_repair.c
index 4486881329b9..05bb4be29390 100644
--- a/fs/xfs/scrub/inode_repair.c
+++ b/fs/xfs/scrub/inode_repair.c
@@ -1002,8 +1002,13 @@ xrep_dinode_bad_rtrefcountbt_fork(
nrecs = be16_to_cpu(dfp->bb_numrecs);
level = be16_to_cpu(dfp->bb_level);
- if (level > sc->mp->m_rtrefc_maxlevels)
+ /* Same root level rules as xfs_iformat_rtrefcount(). */
+ if (level == 0) {
+ if (sc->mp->m_rtrefc_maxlevels == 0 && nrecs != 0)
+ return true;
+ } else if (level >= sc->mp->m_rtrefc_maxlevels) {
return true;
+ }
if (xfs_rtrefcount_droot_space_calc(level, nrecs) > dfork_size)
return true;
if (level > 0 && nrecs == 0)
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 04/59] xfs: recompute the rt btree maxlevels when growfs changes them
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (2 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 03/59] xfs: allow an empty rt refcount btree root before the rt volume exists Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 05/59] xfs: recompute the transaction reservations after growfs Dave Chinner
` (54 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
m_rtbtree_maxlevels is the larger of the rt rmap and rt refcount btree maximum
heights, and the metadata btree space reservation code uses it. It is only
computed at mount time.
Growfs recomputes m_rtrmap_maxlevels and m_rtrefc_maxlevels when realtime
growfs adds the first realtime volume, and when data growfs changes the data
device size that bounds their height. m_rtbtree_maxlevels is stale until the
next mount after either of these. It stays zero after growfs adds the first
realtime volume.
Move the helper that computes it into xfs_mount.h, and call it wherever growfs
recomputes the rt btree maximum heights.
Fixes: 05290bd5c623 ("xfs: allow inode-based btrees to reserve space in the data device")
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_fsops.c | 1 +
fs/xfs/xfs_mount.c | 9 ---------
fs/xfs/xfs_mount.h | 9 +++++++++
fs/xfs/xfs_rtalloc.c | 2 ++
4 files changed, 12 insertions(+), 9 deletions(-)
diff --git a/fs/xfs/xfs_fsops.c b/fs/xfs/xfs_fsops.c
index 21114bb6d4ff..cd399e2a9c7f 100644
--- a/fs/xfs/xfs_fsops.c
+++ b/fs/xfs/xfs_fsops.c
@@ -226,6 +226,7 @@ xfs_growfs_data_private(
/* Compute new maxlevels for rt btrees. */
xfs_rtrmapbt_compute_maxlevels(mp);
xfs_rtrefcountbt_compute_maxlevels(mp);
+ xfs_rtbtree_compute_maxlevels(mp);
}
return error;
diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c
index 60931cb00817..e4036eec579d 100644
--- a/fs/xfs/xfs_mount.c
+++ b/fs/xfs/xfs_mount.c
@@ -833,15 +833,6 @@ xfs_set_max_atomic_write_opt(
return 0;
}
-/* Compute maximum possible height for realtime btree types for this fs. */
-static inline void
-xfs_rtbtree_compute_maxlevels(
- struct xfs_mount *mp)
-{
- mp->m_rtbtree_maxlevels = max(mp->m_rtrmap_maxlevels,
- mp->m_rtrefc_maxlevels);
-}
-
/*
* This function does the following on an initial mount of a file system:
* - reads the superblock from disk and init the mount struct
diff --git a/fs/xfs/xfs_mount.h b/fs/xfs/xfs_mount.h
index cbcfb89c67a4..43afd55a613a 100644
--- a/fs/xfs/xfs_mount.h
+++ b/fs/xfs/xfs_mount.h
@@ -487,6 +487,15 @@ static inline bool xfs_has_rtreflink(const struct xfs_mount *mp)
xfs_has_reflink(mp);
}
+/* Compute maximum possible height for realtime btree types for this fs. */
+static inline void
+xfs_rtbtree_compute_maxlevels(
+ struct xfs_mount *mp)
+{
+ mp->m_rtbtree_maxlevels = max(mp->m_rtrmap_maxlevels,
+ mp->m_rtrefc_maxlevels);
+}
+
static inline bool xfs_has_nonzoned(const struct xfs_mount *mp)
{
return !xfs_has_zoned(mp);
diff --git a/fs/xfs/xfs_rtalloc.c b/fs/xfs/xfs_rtalloc.c
index 31d90828b4e9..760d73e4afa2 100644
--- a/fs/xfs/xfs_rtalloc.c
+++ b/fs/xfs/xfs_rtalloc.c
@@ -933,6 +933,7 @@ xfs_growfs_rt_zoned(
mp->m_features |= XFS_FEAT_REALTIME;
xfs_rtrmapbt_compute_maxlevels(mp);
xfs_rtrefcountbt_compute_maxlevels(mp);
+ xfs_rtbtree_compute_maxlevels(mp);
/*
* Finally add the newly added zone to the freelist and add the space
@@ -1078,6 +1079,7 @@ xfs_growfs_rt_bmblock(
mp->m_features |= XFS_FEAT_REALTIME;
xfs_rtrmapbt_compute_maxlevels(mp);
xfs_rtrefcountbt_compute_maxlevels(mp);
+ xfs_rtbtree_compute_maxlevels(mp);
kfree(nmp);
return 0;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 05/59] xfs: recompute the transaction reservations after growfs
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (3 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 04/59] xfs: recompute the rt btree maxlevels when growfs changes them Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 06/59] xfs: skip missing groups when freeing them Dave Chinner
` (53 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Growfs can leave the transaction reservations sized for the old geometry.
Growing the data device or the realtime volume can increase the rt rmap
btree height. The write and truncate reservations include that height, but
growfs recomputes the btree heights without recomputing the reservations.
Adding the first realtime volume also leaves the reservations without their
realtime terms, because they are recalculated before the realtime feature
is set. A transaction that later needs the larger reservation can overrun
it and shut down the log.
The realtime grow also loses a configured max_atomic_write reservation. Its
reservation update resets the atomic ioend reservation to the default.
The atomic write limits are still sized for the larger reservation, so an
atomic write can overrun its ioend transaction reservation.
Add xfs_trans_resv_recalc() to recompute the reservations on a live
filesystem. It calculates the reservations in a separate table and copies
them into place, so a transaction never sees a partial update. It keeps a
configured atomic ioend reservation, which growfs does not change. Call it
after every growfs recomputes the rt btree heights. This replaces the
realtime grow's update before the commit, which only refreshed a
reservation that the grow takes from its temporary mount.
Growfs and a remount that changes max_atomic_write are not serialised.
Both update the atomic ioend reservation, so a racing remount can lose its
update. The realtime grow already raced this way, because it reset the
reservation in place. This race is not addressed here.
Fixes: 59a57acbce28 ("xfs: check that the rtrmapbt maxlevels doesn't increase when growing fs")
Fixes: 4528b9052731 ("xfs: allow sysadmins to specify a maximum atomic write limit at mount time")
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_trans_resv.c | 27 +++++++++++++++++++++++++++
fs/xfs/libxfs/xfs_trans_resv.h | 1 +
fs/xfs/xfs_fsops.c | 6 +++++-
fs/xfs/xfs_rtalloc.c | 13 ++++++-------
4 files changed, 39 insertions(+), 8 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_trans_resv.c b/fs/xfs/libxfs/xfs_trans_resv.c
index b09c88aec185..fadbed82cf07 100644
--- a/fs/xfs/libxfs/xfs_trans_resv.c
+++ b/fs/xfs/libxfs/xfs_trans_resv.c
@@ -1402,6 +1402,33 @@ xfs_trans_resv_calc(
xfs_calc_default_atomic_ioend_reservation(resp);
}
+/*
+ * Recompute the transaction reservations on a live filesystem after growfs
+ * has changed the geometry they depend on.
+ *
+ * The new reservations are calculated in a separate table and copied into
+ * place, so that transactions starting during the update never see partially
+ * calculated values.
+ *
+ * A configured atomic ioend reservation depends only on intent item sizes,
+ * AG btree heights, the bmap btree height and the filesystem features, none of
+ * which growfs changes. Keep it, rather than resetting it to the default.
+ *
+ * This is not serialised against a remount that changes max_atomic_write,
+ * which also updates the atomic ioend reservation.
+ */
+void
+xfs_trans_resv_recalc(
+ struct xfs_mount *mp)
+{
+ struct xfs_trans_resv resv = {};
+
+ xfs_trans_resv_calc(mp, &resv);
+ if (mp->m_awu_max_bytes)
+ resv.tr_atomic_ioend = M_RES(mp)->tr_atomic_ioend;
+ *M_RES(mp) = resv;
+}
+
/*
* Return the per-extent and fixed transaction reservation sizes needed to
* complete an atomic write.
diff --git a/fs/xfs/libxfs/xfs_trans_resv.h b/fs/xfs/libxfs/xfs_trans_resv.h
index 336279e0fc61..33b5bd2e50df 100644
--- a/fs/xfs/libxfs/xfs_trans_resv.h
+++ b/fs/xfs/libxfs/xfs_trans_resv.h
@@ -97,6 +97,7 @@ struct xfs_trans_resv {
#define XFS_WRITE_LOG_COUNT_REFLINK 8
void xfs_trans_resv_calc(struct xfs_mount *mp, struct xfs_trans_resv *resp);
+void xfs_trans_resv_recalc(struct xfs_mount *mp);
uint xfs_allocfree_block_count(struct xfs_mount *mp, uint num_ops);
unsigned int xfs_calc_finish_bui_reservation(struct xfs_mount *mp,
diff --git a/fs/xfs/xfs_fsops.c b/fs/xfs/xfs_fsops.c
index cd399e2a9c7f..3c68ab991a44 100644
--- a/fs/xfs/xfs_fsops.c
+++ b/fs/xfs/xfs_fsops.c
@@ -223,10 +223,14 @@ xfs_growfs_data_private(
if (error == -ENOSPC)
error = 0;
- /* Compute new maxlevels for rt btrees. */
+ /*
+ * Compute new maxlevels for rt btrees, and the transaction
+ * reservations that depend on them.
+ */
xfs_rtrmapbt_compute_maxlevels(mp);
xfs_rtrefcountbt_compute_maxlevels(mp);
xfs_rtbtree_compute_maxlevels(mp);
+ xfs_trans_resv_recalc(mp);
}
return error;
diff --git a/fs/xfs/xfs_rtalloc.c b/fs/xfs/xfs_rtalloc.c
index 760d73e4afa2..b11a088e43ff 100644
--- a/fs/xfs/xfs_rtalloc.c
+++ b/fs/xfs/xfs_rtalloc.c
@@ -928,12 +928,14 @@ xfs_growfs_rt_zoned(
/*
* Ensure the mount RT feature flag is now set, and compute new
- * maxlevels for rt btrees.
+ * maxlevels for rt btrees and the transaction reservations that depend
+ * on them.
*/
mp->m_features |= XFS_FEAT_REALTIME;
xfs_rtrmapbt_compute_maxlevels(mp);
xfs_rtrefcountbt_compute_maxlevels(mp);
xfs_rtbtree_compute_maxlevels(mp);
+ xfs_trans_resv_recalc(mp);
/*
* Finally add the newly added zone to the freelist and add the space
@@ -1054,11 +1056,6 @@ xfs_growfs_rt_bmblock(
mp->m_rsumlevels = nmp->m_rsumlevels;
mp->m_rsumblocks = nmp->m_rsumblocks;
- /*
- * Recompute the growfsrt reservation from the new rsumsize.
- */
- xfs_trans_resv_calc(mp, &mp->m_resv);
-
/*
* Sync sb counters now to reflect the updated values. Lazy counters are
* not always updated and in order to avoid inconsistencies between
@@ -1074,12 +1071,14 @@ xfs_growfs_rt_bmblock(
/*
* Ensure the mount RT feature flag is now set, and compute new
- * maxlevels for rt btrees.
+ * maxlevels for rt btrees and the transaction reservations that depend
+ * on them.
*/
mp->m_features |= XFS_FEAT_REALTIME;
xfs_rtrmapbt_compute_maxlevels(mp);
xfs_rtrefcountbt_compute_maxlevels(mp);
xfs_rtbtree_compute_maxlevels(mp);
+ xfs_trans_resv_recalc(mp);
kfree(nmp);
return 0;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 06/59] xfs: skip missing groups when freeing them
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (4 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 05/59] xfs: recompute the transaction reservations after growfs Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 07/59] xfs: update the rt geometry when recovering a superblock change Dave Chinner
` (52 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Runtime growfs and recovery of growfs operations have specific ordering
constraints for group instantiation that can be difficult to maintain when
failures occur. There are corruption/shutdown situations where the in-memory
superblock can indicate more groups exist than we have instantiated, and we
trip over this in teardown trying to free groups that don't exist.
Skip group indexes that have no instantiated group when freeing them.
Fixes: e9c4d8bfb26c ("xfs: factor out a generic xfs_group structure")
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_group.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/fs/xfs/libxfs/xfs_group.c b/fs/xfs/libxfs/xfs_group.c
index 2ff9d1e56b47..8b60e2ea3b4f 100644
--- a/fs/xfs/libxfs/xfs_group.c
+++ b/fs/xfs/libxfs/xfs_group.c
@@ -159,6 +159,15 @@ xfs_group_free(
{
struct xfs_group *xg = xa_erase(&mp->m_groups[type].xa, index);
+ /*
+ * When shutdown failures occur concurrently with runtime geometry
+ * changes, the instantiated groups might be out of sync with the
+ * superblock group count. This can result in mount teardown iterating
+ * group indexes that have not yet been instantiated.
+ */
+ if (!xg)
+ return;
+
XFS_IS_CORRUPT(mp, atomic_read(&xg->xg_ref) != 0);
xfs_defer_drain_free(&xg->xg_intents_drain);
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 07/59] xfs: update the rt geometry when recovering a superblock change
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (5 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 06/59] xfs: skip missing groups when freeing them Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 08/59] xfs: reinitialise the filesystem geometry after log recovery Dave Chinner
` (51 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Recovering a growfs can change the realtime geometry. Adding the first
realtime volume can change the realtime extent size. It also brings the rt
rmap and rt refcount btrees into use. Changing the data device size changes
the height those btrees can reach. Superblock recovery does not update the
realtime geometry, the mount features or the realtime btree maximum heights.
The rest of recovery then runs with the old geometry. New rtgroups are set up
with the wrong layout. Valid realtime btree blocks can fail verification and
abort recovery. A realtime device needs to be defined to recover realtime
metadata. Abort superblock recovery if it adds a realtime volume but no device
exists.
Update the realtime geometry, the mount features and the realtime btree
maximum heights when the primary superblock is recovered. Growfs does the same
at runtime.
Fixes: 87fe4c34a383 ("xfs: create incore realtime group structures")
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_buf_item_recover.c | 38 +++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
index 57929f115055..383d0e87ef6a 100644
--- a/fs/xfs/xfs_buf_item_recover.c
+++ b/fs/xfs/xfs_buf_item_recover.c
@@ -27,6 +27,8 @@
#include "xfs_sb.h"
#include "xfs_rtgroup.h"
#include "xfs_rtbitmap.h"
+#include "xfs_rtrmap_btree.h"
+#include "xfs_rtrefcount_btree.h"
/*
* This is the number of entries in the l_buf_cancel_table used during
@@ -741,6 +743,7 @@ xlog_recover_do_primary_sb_buffer(
struct xfs_dsb *dsb = bp->b_addr;
xfs_agnumber_t orig_agcount = mp->m_sb.sb_agcount;
xfs_rgnumber_t orig_rgcount = mp->m_sb.sb_rgcount;
+ xfs_rfsblock_t orig_rblocks = mp->m_sb.sb_rblocks;
int error;
error = xlog_recover_do_reg_buffer(mp, item, bp, buf_f, current_lsn);
@@ -752,11 +755,36 @@ xlog_recover_do_primary_sb_buffer(
return -EFSCORRUPTED;
}
+ /*
+ * Growfs can add the first realtime volume to a filesystem that was
+ * mounted with a realtime device but had no realtime section. Without
+ * a realtime device the new realtime volume cannot be recovered.
+ *
+ * Check this before the in-core superblock is updated. A failed mount
+ * frees the rtgroups counted by the in-core superblock, and the new
+ * rtgroups have not been created yet.
+ */
+ if (orig_rblocks == 0 && be64_to_cpu(dsb->sb_rblocks) > 0 &&
+ !XFS_IS_REALTIME_MOUNT(mp)) {
+ xfs_warn(mp,
+ "Recovered a realtime volume, use rtdev=device option");
+ return -ENODEV;
+ }
+
/*
* Update the in-core super block from the freshly recovered on-disk one.
*/
xfs_sb_from_disk(&mp->m_sb, dsb);
+ /*
+ * Growfs can change the realtime extent size when it adds the first
+ * realtime volume, and adding it sets the realtime feature. Update the
+ * realtime geometry and the mount features before the rtgroups are
+ * set up, as both are used to calculate the rtgroup geometry.
+ */
+ xfs_sb_mount_rextsize(mp, &mp->m_sb);
+ mp->m_features |= xfs_sb_version_to_features(&mp->m_sb);
+
/*
* Grow can change the device size. Mirror that into the buftarg.
*/
@@ -816,6 +844,16 @@ xlog_recover_do_primary_sb_buffer(
xfs_warn(mp, "Failed recovery rtgroup init: %d", error);
return error;
}
+
+ /*
+ * The realtime btree geometry depends on the realtime volume and the
+ * data device size, both of which growfs can change. Later recovery
+ * verifies realtime btree blocks against that geometry, so recompute
+ * the realtime btree maximum heights now.
+ */
+ xfs_rtrmapbt_compute_maxlevels(mp);
+ xfs_rtrefcountbt_compute_maxlevels(mp);
+ xfs_rtbtree_compute_maxlevels(mp);
return 0;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 08/59] xfs: reinitialise the filesystem geometry after log recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (6 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 07/59] xfs: update the rt geometry when recovering a superblock change Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 09/59] xfs: apply mount superblock updates " Dave Chinner
` (50 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Log recovery can replay a growfs, which changes the filesystem geometry
recorded in the superblock. Mount computes state such as the realtime extent
and summary geometry, the low space thresholds and the transaction
reservations from the superblock before recovery runs. Nothing recomputes that
state afterwards, so the rest of the mount uses values that describe the
geometry from before recovery. The realtime device is also only checked
against the pre-recovery superblock. Runtime growfs recomputes this state
whenever it changes the superblock.
Log recovery re-reads the superblock once it has replayed the log, and
re-derives the mount features from it. That is mount reinitialisation rather
than journal work. Move it into the mount code, and run it after a dirty log has
been recovered. Recompute all the mount state derived from the superblock
geometry there as well, finishing with the transaction reservations that depend
on it.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 27 ------------------
fs/xfs/xfs_mount.c | 59 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+), 27 deletions(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 457617979668..fd356b09f9d1 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -3368,9 +3368,6 @@ xlog_do_recover(
xfs_daddr_t head_blk,
xfs_daddr_t tail_blk)
{
- struct xfs_mount *mp = log->l_mp;
- struct xfs_buf *bp = mp->m_sb_bp;
- struct xfs_sb *sbp = &mp->m_sb;
int error;
trace_xfs_log_recover(log, head_blk, tail_blk);
@@ -3395,30 +3392,6 @@ xlog_do_recover(
*/
xfs_ail_assign_tail_lsn(log->l_ailp);
- /*
- * Now that we've finished replaying all buffer and inode updates,
- * re-read the superblock and reverify it.
- */
- xfs_buf_lock(bp);
- xfs_buf_hold(bp);
- error = _xfs_buf_read(bp);
- if (error) {
- if (!xlog_is_shutdown(log)) {
- xfs_buf_ioerror_alert(bp, __this_address);
- ASSERT(0);
- }
- xfs_buf_relse(bp);
- return error;
- }
-
- /* Convert superblock from on-disk format */
- xfs_sb_from_disk(sbp, bp->b_addr);
- xfs_buf_relse(bp);
-
- /* re-initialise in-core superblock and geometry structures */
- mp->m_features |= xfs_sb_version_to_features(sbp);
- xfs_reinit_percpu_counters(mp);
-
/* Normal transactions can now occur */
clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
return 0;
diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c
index e4036eec579d..11040773fca0 100644
--- a/fs/xfs/xfs_mount.c
+++ b/fs/xfs/xfs_mount.c
@@ -833,6 +833,54 @@ xfs_set_max_atomic_write_opt(
return 0;
}
+/*
+ * Log recovery can change the on-disk superblock, including the filesystem
+ * geometry if it replays a growfs. Re-read the superblock now that recovery has
+ * finished, and recompute the mount state that is derived from it so the rest
+ * of the mount uses the recovered geometry.
+ */
+static int
+xfs_mount_reinit_fs_geometry(
+ struct xfs_mount *mp)
+{
+ struct xfs_buf *bp = mp->m_sb_bp;
+ int error;
+
+ xfs_buf_lock(bp);
+ xfs_buf_hold(bp);
+ error = _xfs_buf_read(bp);
+ if (error) {
+ if (!xfs_is_shutdown(mp)) {
+ xfs_buf_ioerror_alert(bp, __this_address);
+ ASSERT(0);
+ }
+ xfs_buf_relse(bp);
+ return error;
+ }
+ xfs_sb_from_disk(&mp->m_sb, bp->b_addr);
+ xfs_buf_relse(bp);
+
+ xfs_sb_mount_rextsize(mp, &mp->m_sb);
+ mp->m_features |= xfs_sb_version_to_features(&mp->m_sb);
+ xfs_reinit_percpu_counters(mp);
+
+ xfs_rtrmapbt_compute_maxlevels(mp);
+ xfs_rtrefcountbt_compute_maxlevels(mp);
+ xfs_rtbtree_compute_maxlevels(mp);
+
+ error = xfs_rtmount_init(mp);
+ if (error) {
+ xfs_warn(mp, "RT mount failed");
+ return error;
+ }
+
+ xfs_set_low_space_thresholds(mp);
+
+ /* The reservations depend on all of the above. */
+ xfs_trans_resv_calc(mp, M_RES(mp));
+ return 0;
+}
+
/*
* This function does the following on an initial mount of a file system:
* - reads the superblock from disk and init the mount struct
@@ -1047,6 +1095,17 @@ xfs_mountfs(
goto out_inodegc_shrinker;
}
+ /*
+ * If the log was dirty, recovery may have changed the filesystem
+ * geometry. Re-read the superblock and reinitialise the mount geometry
+ * structures derived from it.
+ */
+ if (xlog_recovery_needed(mp->m_log)) {
+ error = xfs_mount_reinit_fs_geometry(mp);
+ if (error)
+ goto out_free_metadir;
+ }
+
/*
* If we're resuming quota status and recovered the log, re-sample the
* qflags from the ondisk superblock now that we've recovered it, just
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 09/59] xfs: apply mount superblock updates after log recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (7 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 08/59] xfs: reinitialise the filesystem geometry after log recovery Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 10/59] xfs: check the atomic write reservation against the current log size Dave Chinner
` (49 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Mount makes several updates to the in-core superblock that are written back
later in the mount. These updates are applied before log recovery runs. The
superblock is re-read from disk when the log is dirty, and the re-read discards
them. The later superblock write then writes the old values back, so the
updates are lost on any mount that recovers the log. Stripe geometry given as
mount options is used by that mount, but it never reaches the on-disk
superblock.
Apply these updates after the superblock has been re-read, on every mount.
Nothing between their old and new positions in the mount depends on them. The
superblock verifier also sees the same stripe alignment state when the
superblock is re-read as when it was first read.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_mount.c | 79 +++++++++++++++++++++++++++-------------------
1 file changed, 46 insertions(+), 33 deletions(-)
diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c
index 11040773fca0..cfc16023231c 100644
--- a/fs/xfs/xfs_mount.c
+++ b/fs/xfs/xfs_mount.c
@@ -882,28 +882,15 @@ xfs_mount_reinit_fs_geometry(
}
/*
- * This function does the following on an initial mount of a file system:
- * - reads the superblock from disk and init the mount struct
- * - if we're a 32-bit kernel, do a size check on the superblock
- * so we don't mount terabyte filesystems
- * - init mount struct realtime fields
- * - allocate inode hash table for fs
- * - init directory manager
- * - perform recovery and init the log manager
+ * Apply the updates this mount makes to the in-core superblock. These are
+ * written back later in the mount, so they must be applied after any re-read of
+ * the superblock following log recovery.
*/
-int
-xfs_mountfs(
+static int
+xfs_mount_apply_sb_updates(
struct xfs_mount *mp)
{
- struct xfs_sb *sbp = &(mp->m_sb);
- struct xfs_inode *rip;
- struct xfs_ino_geometry *igeo = M_IGEO(mp);
- uint quotamount = 0;
- uint quotaflags = 0;
- int error = 0;
- int i;
-
- xfs_sb_mount_common(mp, sbp);
+ struct xfs_sb *sbp = &mp->m_sb;
/*
* Check for a mismatched features2 values. Older kernels read & wrote
@@ -927,14 +914,47 @@ xfs_mountfs(
mp->m_update_sb = true;
}
-
/* always use v2 inodes by default now */
- if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
- mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
+ if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
+ sbp->sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
mp->m_features |= XFS_FEAT_NLINK;
mp->m_update_sb = true;
}
+ /*
+ * Check if sb_agblocks is aligned at stripe boundary. If sb_agblocks
+ * is NOT aligned turn off m_dalign since allocator alignment is within
+ * an ag, therefore ag has to be aligned at stripe boundary. Note that
+ * we must compute the free space and rmap btree geometry before doing
+ * this.
+ */
+ return xfs_update_alignment(mp);
+}
+
+/*
+ * This function does the following on an initial mount of a file system:
+ * - reads the superblock from disk and init the mount struct
+ * - if we're a 32-bit kernel, do a size check on the superblock
+ * so we don't mount terabyte filesystems
+ * - init mount struct realtime fields
+ * - allocate inode hash table for fs
+ * - init directory manager
+ * - perform recovery and init the log manager
+ */
+int
+xfs_mountfs(
+ struct xfs_mount *mp)
+{
+ struct xfs_sb *sbp = &(mp->m_sb);
+ struct xfs_inode *rip;
+ struct xfs_ino_geometry *igeo = M_IGEO(mp);
+ uint quotamount = 0;
+ uint quotaflags = 0;
+ int error = 0;
+ int i;
+
+ xfs_sb_mount_common(mp, sbp);
+
/*
* If we were given new sunit/swidth options, do some basic validation
* checks and convert the incore dalign and swidth values to the
@@ -957,17 +977,6 @@ xfs_mountfs(
xfs_agbtree_compute_maxlevels(mp);
xfs_rtbtree_compute_maxlevels(mp);
- /*
- * Check if sb_agblocks is aligned at stripe boundary. If sb_agblocks
- * is NOT aligned turn off m_dalign since allocator alignment is within
- * an ag, therefore ag has to be aligned at stripe boundary. Note that
- * we must compute the free space and rmap btree geometry before doing
- * this.
- */
- error = xfs_update_alignment(mp);
- if (error)
- goto out;
-
/* enable fail_at_unmount as default */
mp->m_fail_unmount = true;
@@ -1106,6 +1115,10 @@ xfs_mountfs(
goto out_free_metadir;
}
+ error = xfs_mount_apply_sb_updates(mp);
+ if (error)
+ goto out_free_metadir;
+
/*
* If we're resuming quota status and recovered the log, re-sample the
* qflags from the ondisk superblock now that we've recovered it, just
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 10/59] xfs: check the atomic write reservation against the current log size
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (8 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 09/59] xfs: apply mount superblock updates " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 11/59] xfs: fix the bmap btree maximum height calculation Dave Chinner
` (48 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Changing max_atomic_write on remount can disturb transactions running on the
filesystem. The new reservation is checked by computing the minimum log size
for the whole reservation table. That calculation temporarily changes the
mount-wide rmap btree height and resets the live atomic ioend reservation to
its default. It is only safe at mkfs and mount time, but remount runs it on a
live filesystem. A transaction started in that window can see the wrong btree
height or get the wrong atomic ioend reservation.
Mount has already checked every other reservation against the log. Only the
new atomic ioend reservation needs checking. Invert the minimum log size
calculation to give the largest reservation that the current log can hold. It
reads only the superblock log geometry, so it is safe on a live filesystem.
The atomic ioend reservation is now only updated once the new value is known
to fit.
xfs_calc_atomic_write_log_geometry() is unchanged and is still used by mkfs to
size the log.
Fixes: 4528b9052731 ("xfs: allow sysadmins to specify a maximum atomic write limit at mount time")
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_log_rlimit.c | 45 ++++++++++++++++++++++++++++++++++
fs/xfs/libxfs/xfs_shared.h | 1 +
fs/xfs/libxfs/xfs_trans_resv.c | 45 ++++++++++++++++++++++++++++------
fs/xfs/xfs_trace.h | 34 +++++++++++++++++++++++++
4 files changed, 118 insertions(+), 7 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_log_rlimit.c b/fs/xfs/libxfs/xfs_log_rlimit.c
index 37712b2f8757..685d4797913e 100644
--- a/fs/xfs/libxfs/xfs_log_rlimit.c
+++ b/fs/xfs/libxfs/xfs_log_rlimit.c
@@ -249,3 +249,48 @@ xfs_log_calc_minimum_size(
return XFS_BB_TO_FSB(mp, min_logblks);
}
+
+/*
+ * Calculate the largest transaction reservation, in bytes, that fits in the
+ * current log. The reservation is a unit reservation from
+ * xfs_log_calc_unit_res() multiplied by the log count, as in
+ * xfs_log_calc_minimum_size().
+ *
+ * This is the inverse of xfs_log_calc_minimum_size(): a reservation fits in the
+ * log if, and only if, it is no larger than the value returned here. The two
+ * functions must be kept in step.
+ *
+ * xfs_log_calc_minimum_size() temporarily changes mount state to calculate the
+ * reservations, so it can only be used at mkfs and mount time. This function
+ * only reads the superblock log geometry, so it is safe to use on a live
+ * filesystem.
+ */
+int
+xfs_log_calc_max_res(
+ struct xfs_mount *mp)
+{
+ int logbbs;
+ int lsunit = 0;
+
+ /* The superblock verifier limits the log to XFS_MAX_LOG_BYTES. */
+ logbbs = (int)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
+ logbbs /= XFS_MIN_LOG_FACTOR;
+
+ if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1)
+ lsunit = BTOBB(mp->m_sb.sb_logsunit);
+
+ if (lsunit) {
+ logbbs -= 2 * lsunit;
+ if (logbbs > 0)
+ logbbs = rounddown(logbbs, lsunit);
+ } else {
+ logbbs -= 2 * BBSIZE;
+ }
+
+ /* Mount has already checked that the log fits its reservations. */
+ ASSERT(logbbs > 0);
+ if (logbbs <= 0)
+ return 0;
+ ASSERT(logbbs < BTOBB(XFS_MAX_LOG_BYTES) / 2);
+ return BBTOB(logbbs);
+}
diff --git a/fs/xfs/libxfs/xfs_shared.h b/fs/xfs/libxfs/xfs_shared.h
index b1e0d9bc1f7d..ced25294b57c 100644
--- a/fs/xfs/libxfs/xfs_shared.h
+++ b/fs/xfs/libxfs/xfs_shared.h
@@ -124,6 +124,7 @@ static inline bool xfs_btree_is_rtrefcount(const struct xfs_btree_ops *ops)
/* log size calculation functions */
int xfs_log_calc_unit_res(struct xfs_mount *mp, int unit_bytes);
int xfs_log_calc_minimum_size(struct xfs_mount *);
+int xfs_log_calc_max_res(struct xfs_mount *mp);
struct xfs_trans_res;
void xfs_log_get_max_trans_res(struct xfs_mount *mp,
diff --git a/fs/xfs/libxfs/xfs_trans_resv.c b/fs/xfs/libxfs/xfs_trans_resv.c
index fadbed82cf07..372b0640357c 100644
--- a/fs/xfs/libxfs/xfs_trans_resv.c
+++ b/fs/xfs/libxfs/xfs_trans_resv.c
@@ -1521,6 +1521,10 @@ xfs_calc_max_atomic_write_fsblocks(
* Compute the log blocks and transaction reservation needed to complete an
* atomic write of a given number of blocks. Worst case, each block requires
* separate handling. A return value of 0 means something went wrong.
+ *
+ * This is for mkfs to size the log. It temporarily modifies the mount's
+ * transaction reservations and btree geometry, so it must not be used on a
+ * live filesystem.
*/
xfs_extlen_t
xfs_calc_atomic_write_log_geometry(
@@ -1559,29 +1563,56 @@ xfs_calc_atomic_write_log_geometry(
/*
* Compute the transaction reservation needed to complete an out of place
* atomic write of a given number of blocks.
+ *
+ * This is called at mount and remount time, so it must not modify any mount
+ * state other than the atomic ioend reservation. The new reservation is
+ * checked against the largest reservation the current log can hold, and the
+ * atomic ioend reservation is only updated once it is known to fit.
*/
int
xfs_calc_atomic_write_reservation(
struct xfs_mount *mp,
xfs_extlen_t blockcount)
{
- unsigned int new_logres;
- xfs_extlen_t min_logblocks;
+ struct xfs_trans_resv *resp = M_RES(mp);
+ struct xfs_trans_res tres;
+ unsigned int per_intent, step_size;
+ unsigned int logres;
+ uint64_t need_res = 0;
+ int max_res;
/*
* If the caller doesn't ask for a specific atomic write size, then
* use the defaults.
*/
if (blockcount == 0) {
- xfs_calc_default_atomic_ioend_reservation(M_RES(mp));
+ xfs_calc_default_atomic_ioend_reservation(resp);
return 0;
}
- min_logblocks = xfs_calc_atomic_write_log_geometry(mp, blockcount,
- &new_logres);
- if (!min_logblocks || min_logblocks > mp->m_sb.sb_logblocks)
+ per_intent = xfs_calc_atomic_write_ioend_geometry(mp, &step_size);
+
+ /* Check for overflows */
+ if (check_mul_overflow(blockcount, per_intent, &logres) ||
+ check_add_overflow(logres, step_size, &logres) ||
+ logres > INT_MAX)
+ return -EINVAL;
+
+ /* The atomic ioend reservation is based on the default. */
+ tres = resp->tr_itruncate;
+ tres.tr_logres = logres;
+
+ need_res = xfs_log_calc_unit_res(mp, tres.tr_logres);
+ if (tres.tr_logcount > 1)
+ need_res *= tres.tr_logcount;
+ max_res = xfs_log_calc_max_res(mp);
+
+ trace_xfs_calc_atomic_write_reservation(mp, per_intent, step_size,
+ blockcount, logres, need_res, max_res);
+
+ if (need_res > max_res)
return -EINVAL;
- M_RES(mp)->tr_atomic_ioend.tr_logres = new_logres;
+ resp->tr_atomic_ioend = tres;
return 0;
}
diff --git a/fs/xfs/xfs_trace.h b/fs/xfs/xfs_trace.h
index 28d49f158b22..0e195a0835ec 100644
--- a/fs/xfs/xfs_trace.h
+++ b/fs/xfs/xfs_trace.h
@@ -264,6 +264,40 @@ TRACE_EVENT(xfs_calc_max_atomic_write_log_geometry,
__entry->logres)
);
+TRACE_EVENT(xfs_calc_atomic_write_reservation,
+ TP_PROTO(struct xfs_mount *mp, unsigned int per_intent,
+ unsigned int step_size, unsigned int blockcount,
+ unsigned int logres, uint64_t need_res, int max_res),
+ TP_ARGS(mp, per_intent, step_size, blockcount, logres, need_res,
+ max_res),
+ TP_STRUCT__entry(
+ __field(dev_t, dev)
+ __field(unsigned int, per_intent)
+ __field(unsigned int, step_size)
+ __field(unsigned int, blockcount)
+ __field(unsigned int, logres)
+ __field(uint64_t, need_res)
+ __field(int, max_res)
+ ),
+ TP_fast_assign(
+ __entry->dev = mp->m_super->s_dev;
+ __entry->per_intent = per_intent;
+ __entry->step_size = step_size;
+ __entry->blockcount = blockcount;
+ __entry->logres = logres;
+ __entry->need_res = need_res;
+ __entry->max_res = max_res;
+ ),
+ TP_printk("dev %d:%d per_intent %u step_size %u blockcount %u logres %u need_res %llu max_res %d",
+ MAJOR(__entry->dev), MINOR(__entry->dev),
+ __entry->per_intent,
+ __entry->step_size,
+ __entry->blockcount,
+ __entry->logres,
+ __entry->need_res,
+ __entry->max_res)
+);
+
TRACE_EVENT(xlog_intent_recovery_failed,
TP_PROTO(struct xfs_mount *mp, const struct xfs_defer_op_type *ops,
int error),
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 11/59] xfs: fix the bmap btree maximum height calculation
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (9 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 10/59] xfs: check the atomic write reservation against the current log size Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 12/59] xfs: add log recovery item validation design document Dave Chinner
` (47 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
m_bm_maxlevels is the maximum height of a bmap btree, including the inode root
level. The calculation models a worst case tree with every block at minimum
fill. It stops adding levels once a level reduces to a single block. That block
is a disk block, but the bmap btree root lives in the inode and holds far fewer
pointers than a disk block. The top disk level usually needs more pointers than
the inode root can hold, so a disk node and the inode root above it are both
still needed. The calculation leaves out that level.
m_bm_maxlevels is therefore one level less than the worst case height for
almost every filesystem geometry. Space and log reservations sized from it can
be one level short for a full height split, and a cursor over a worst case tree
has more levels than it was sized for.
Compute the height by adding disk levels until all the blocks of the top disk
level can be pointed to from the inode root, then adding the inode root level.
With the corrected height, the deepest valid root level is one less than
m_bm_maxlevels. The root loader, the block verifier and the inode repair code
reject only levels greater than m_bm_maxlevels, which was correct for the old
value. Reject levels at or above m_bm_maxlevels instead. This must change
together with the height calculation, or the checks would accept a root one
level too deep.
The minimum log size is derived from transaction reservations sized by
m_bm_maxlevels. Changing it would change the minimum log size, and existing
filesystems with logs at the old minimum would no longer mount. Keep the legacy
calculation, and use it only to compute the minimum log size.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_bmap.c | 48 ++++++++++++++++++++++---
fs/xfs/libxfs/xfs_bmap.h | 2 ++
fs/xfs/libxfs/xfs_bmap_btree.c | 2 +-
fs/xfs/libxfs/xfs_inode_fork.c | 2 +-
fs/xfs/libxfs/xfs_log_rlimit.c | 66 ++++++++++++++++++++++------------
fs/xfs/scrub/inode_repair.c | 2 +-
6 files changed, 92 insertions(+), 30 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_bmap.c b/fs/xfs/libxfs/xfs_bmap.c
index dcd5faf3428e..ed081a5e4166 100644
--- a/fs/xfs/libxfs/xfs_bmap.c
+++ b/fs/xfs/libxfs/xfs_bmap.c
@@ -49,13 +49,53 @@ struct kmem_cache *xfs_bmap_intent_cache;
*/
/*
- * Compute and fill in the value of the maximum depth of a bmap btree
- * in this filesystem. Done once, during mount.
+ * Compute and fill in the value of the maximum height of a bmap btree in this
+ * filesystem, including the inode root level. Done once, during mount.
+ *
+ * The worst case tree has every block at minimum fill. The root lives in the
+ * inode and holds far fewer pointers than a disk block, so disk levels are
+ * added until all the blocks of the top disk level can be pointed to from the
+ * inode root, then the inode root level is added on top.
*/
void
xfs_bmap_compute_maxlevels(
- xfs_mount_t *mp, /* file system mount structure */
- int whichfork) /* data or attr fork */
+ struct xfs_mount *mp,
+ int whichfork)
+{
+ uint64_t maxblocks;
+ xfs_extnum_t maxleafents;
+ int level;
+ int maxrootrecs;
+ int sz;
+
+ maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
+ whichfork);
+ if (whichfork == XFS_DATA_FORK)
+ sz = xfs_bmdr_space_calc(MINDBTPTRS);
+ else
+ sz = xfs_bmdr_space_calc(MINABTPTRS);
+ maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
+
+ maxblocks = howmany_64(maxleafents, mp->m_bmap_dmnr[0]);
+ for (level = 1; maxblocks > maxrootrecs; level++)
+ maxblocks = howmany_64(maxblocks, mp->m_bmap_dmnr[1]);
+
+ /* Add the inode root level. */
+ mp->m_bm_maxlevels[whichfork] = level + 1;
+ ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
+}
+
+/*
+ * Legacy calculation of the maximum height of a bmap btree. This stops once a
+ * level reduces to a single block without accounting for the inode root above
+ * it, so it is one level short for most geometries. The minimum log size is
+ * derived from transaction reservations sized by this value, so it is only used
+ * to compute the minimum log size.
+ */
+void
+xfs_bmap_compute_maxlevels_minlogsize(
+ struct xfs_mount *mp,
+ int whichfork)
{
uint64_t maxblocks; /* max blocks at this level */
xfs_extnum_t maxleafents; /* max leaf entries possible */
diff --git a/fs/xfs/libxfs/xfs_bmap.h b/fs/xfs/libxfs/xfs_bmap.h
index fb7481e8190c..ab870415c505 100644
--- a/fs/xfs/libxfs/xfs_bmap.h
+++ b/fs/xfs/libxfs/xfs_bmap.h
@@ -199,6 +199,8 @@ int xfs_bmap_local_to_extents(struct xfs_trans *tp, struct xfs_inode *ip,
void *priv),
void *priv);
void xfs_bmap_compute_maxlevels(struct xfs_mount *mp, int whichfork);
+void xfs_bmap_compute_maxlevels_minlogsize(struct xfs_mount *mp,
+ int whichfork);
int xfs_bmap_first_unused(struct xfs_trans *tp, struct xfs_inode *ip,
xfs_extlen_t len, xfs_fileoff_t *unused, int whichfork);
int xfs_bmap_last_before(struct xfs_trans *tp, struct xfs_inode *ip,
diff --git a/fs/xfs/libxfs/xfs_bmap_btree.c b/fs/xfs/libxfs/xfs_bmap_btree.c
index 758a4b1ccf5b..a55655300c04 100644
--- a/fs/xfs/libxfs/xfs_bmap_btree.c
+++ b/fs/xfs/libxfs/xfs_bmap_btree.c
@@ -421,7 +421,7 @@ xfs_bmbt_verify(
* precise.
*/
level = be16_to_cpu(block->bb_level);
- if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
+ if (level >= max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1]))
return __this_address;
return xfs_btree_fsblock_verify(bp, mp->m_bmap_dmxr[level != 0]);
diff --git a/fs/xfs/libxfs/xfs_inode_fork.c b/fs/xfs/libxfs/xfs_inode_fork.c
index 486fe7ab8b61..0cf91ea2bf37 100644
--- a/fs/xfs/libxfs/xfs_inode_fork.c
+++ b/fs/xfs/libxfs/xfs_inode_fork.c
@@ -203,7 +203,7 @@ xfs_iformat_btree(
xfs_bmdr_space_calc(nrecs) >
XFS_DFORK_SIZE(dip, mp, whichfork) ||
ifp->if_nextents > ip->i_nblocks) ||
- level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
+ level == 0 || level >= XFS_BM_MAXLEVELS(mp, whichfork)) {
xfs_warn(mp, "corrupt inode %llu (btree).",
(unsigned long long)I_INO(ip));
xfs_inode_verifier_error(ip, -EFSCORRUPTED,
diff --git a/fs/xfs/libxfs/xfs_log_rlimit.c b/fs/xfs/libxfs/xfs_log_rlimit.c
index 685d4797913e..f3be6ef9e433 100644
--- a/fs/xfs/libxfs/xfs_log_rlimit.c
+++ b/fs/xfs/libxfs/xfs_log_rlimit.c
@@ -14,6 +14,7 @@
#include "xfs_trans_space.h"
#include "xfs_da_btree.h"
#include "xfs_bmap_btree.h"
+#include "xfs_bmap.h"
#include "xfs_trace.h"
/*
@@ -50,7 +51,8 @@ xfs_want_minlogsize_fixes(
*/
STATIC int
xfs_log_calc_max_attrsetm_res(
- struct xfs_mount *mp)
+ struct xfs_mount *mp,
+ const struct xfs_trans_resv *resv)
{
int size;
int nblks;
@@ -70,21 +72,20 @@ xfs_log_calc_max_attrsetm_res(
nblks += XFS_NEXTENTADD_SPACE_RES(mp, size, XFS_ATTR_FORK);
- return M_RES(mp)->tr_attrsetm.tr_logres +
- M_RES(mp)->tr_attrsetrt.tr_logres * nblks;
+ return resv->tr_attrsetm.tr_logres +
+ resv->tr_attrsetrt.tr_logres * nblks;
}
/*
* Compute an alternate set of log reservation sizes for use exclusively with
- * minimum log size calculations.
+ * minimum log size calculations. The caller must have set up the legacy btree
+ * heights.
*/
static void
xfs_log_calc_trans_resv_for_minlogblocks(
struct xfs_mount *mp,
struct xfs_trans_resv *resv)
{
- unsigned int rmap_maxlevels = mp->m_rmap_maxlevels;
-
/*
* If the feature set is new enough, drop the oversized minimum log
* size computation introduced by the original reflink code.
@@ -95,17 +96,6 @@ xfs_log_calc_trans_resv_for_minlogblocks(
return;
}
- /*
- * In the early days of rmap+reflink, we always set the rmap maxlevels
- * to 9 even if the AG was small enough that it would never grow to
- * that height. Transaction reservation sizes influence the minimum
- * log size calculation, which influences the size of the log that mkfs
- * creates. Use the old value here to ensure that newly formatted
- * small filesystems will mount on older kernels.
- */
- if (xfs_has_rmapbt(mp) && xfs_has_reflink(mp))
- mp->m_rmap_maxlevels = XFS_OLD_REFLINK_RMAP_MAXLEVELS;
-
xfs_trans_resv_calc(mp, resv);
/* Copy the dynamic transaction reservation types from the running fs */
@@ -141,15 +131,15 @@ xfs_log_calc_trans_resv_for_minlogblocks(
xfs_calc_itruncate_reservation_minlogsize(mp);
resv->tr_qm_dqalloc.tr_logres =
xfs_calc_qm_dqalloc_reservation_minlogsize(mp);
-
- /* Put everything back the way it was. This goes at the end. */
- mp->m_rmap_maxlevels = rmap_maxlevels;
}
/*
* Iterate over the log space reservation table to figure out and return
- * the maximum one in terms of the pre-calculated values which were done
- * at mount time.
+ * the maximum one for the minimum log size calculation.
+ *
+ * The minimum log size must not change for existing filesystems, so the
+ * reservations are calculated with legacy btree heights. This temporarily
+ * changes mount-wide state, so it can only be used at mkfs and mount time.
*/
void
xfs_log_get_max_trans_res(
@@ -159,13 +149,43 @@ xfs_log_get_max_trans_res(
struct xfs_trans_resv resv = {};
struct xfs_trans_res *resp;
struct xfs_trans_res *end_resp;
+ unsigned int rmap_maxlevels = mp->m_rmap_maxlevels;
+ unsigned int bm_maxlevels[2] = {
+ mp->m_bm_maxlevels[XFS_DATA_FORK],
+ mp->m_bm_maxlevels[XFS_ATTR_FORK],
+ };
unsigned int i;
int log_space = 0;
int attr_space;
- attr_space = xfs_log_calc_max_attrsetm_res(mp);
+ /*
+ * The minimum log size was always computed with the legacy bmap btree
+ * height, which is one level short for most geometries. Use it here so
+ * the minimum log size does not change.
+ */
+ xfs_bmap_compute_maxlevels_minlogsize(mp, XFS_DATA_FORK);
+ xfs_bmap_compute_maxlevels_minlogsize(mp, XFS_ATTR_FORK);
+
+ /*
+ * In the early days of rmap+reflink, we always set the rmap maxlevels
+ * to 9 even if the AG was small enough that it would never grow to
+ * that height. Transaction reservation sizes influence the minimum
+ * log size calculation, which influences the size of the log that mkfs
+ * creates. Use the old value here to ensure that newly formatted
+ * small filesystems will mount on older kernels. Feature sets new
+ * enough for the minimum log size fixes do not need this.
+ */
+ if (!xfs_want_minlogsize_fixes(&mp->m_sb) &&
+ xfs_has_rmapbt(mp) && xfs_has_reflink(mp))
+ mp->m_rmap_maxlevels = XFS_OLD_REFLINK_RMAP_MAXLEVELS;
xfs_log_calc_trans_resv_for_minlogblocks(mp, &resv);
+ attr_space = xfs_log_calc_max_attrsetm_res(mp, &resv);
+
+ /* Put everything back the way it was. */
+ mp->m_rmap_maxlevels = rmap_maxlevels;
+ mp->m_bm_maxlevels[XFS_DATA_FORK] = bm_maxlevels[XFS_DATA_FORK];
+ mp->m_bm_maxlevels[XFS_ATTR_FORK] = bm_maxlevels[XFS_ATTR_FORK];
resp = (struct xfs_trans_res *)&resv;
end_resp = (struct xfs_trans_res *)(&resv + 1);
diff --git a/fs/xfs/scrub/inode_repair.c b/fs/xfs/scrub/inode_repair.c
index 05bb4be29390..dbd42976bdcd 100644
--- a/fs/xfs/scrub/inode_repair.c
+++ b/fs/xfs/scrub/inode_repair.c
@@ -927,7 +927,7 @@ xrep_dinode_bad_bmbt_fork(
if (nrecs == 0 || xfs_bmdr_space_calc(nrecs) > dfork_size)
return true;
- if (level == 0 || level > XFS_BM_MAXLEVELS(sc->mp, whichfork))
+ if (level == 0 || level >= XFS_BM_MAXLEVELS(sc->mp, whichfork))
return true;
dmxr = xfs_bmdr_maxrecs(dfork_size, 0);
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 12/59] xfs: add log recovery item validation design document
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (10 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 11/59] xfs: fix the bmap btree maximum height calculation Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 13/59] xfs: lift transaction header parsing out of the region assembly Dave Chinner
` (46 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Log item data read from the journal cannot be fully trusted. Torn writes,
hardware errors and software bugs can all produce corrupt log items. Recovery
uses most of the log item contents without checking them first.
Add a design document describing how recovery validates log items before
replaying them. It describes the four layers of validation, and the treatment
of the transaction header as a validated item. It also covers the fixes needed
in the generic region assembly code.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
Documentation/filesystems/xfs/index.rst | 1 +
.../xfs-log-recovery-validation-design.rst | 672 ++++++++++++++++++
2 files changed, 673 insertions(+)
create mode 100644 Documentation/filesystems/xfs/xfs-log-recovery-validation-design.rst
diff --git a/Documentation/filesystems/xfs/index.rst b/Documentation/filesystems/xfs/index.rst
index ab66c57a5d18..231c1382b27a 100644
--- a/Documentation/filesystems/xfs/index.rst
+++ b/Documentation/filesystems/xfs/index.rst
@@ -9,6 +9,7 @@ XFS Filesystem Documentation
:numbered:
xfs-delayed-logging-design
+ xfs-log-recovery-validation-design
xfs-maintainer-entry-profile
xfs-self-describing-metadata
xfs-online-fsck-design
diff --git a/Documentation/filesystems/xfs/xfs-log-recovery-validation-design.rst b/Documentation/filesystems/xfs/xfs-log-recovery-validation-design.rst
new file mode 100644
index 000000000000..fe3ae61b5ef5
--- /dev/null
+++ b/Documentation/filesystems/xfs/xfs-log-recovery-validation-design.rst
@@ -0,0 +1,672 @@
+.. SPDX-License-Identifier: GPL-2.0
+.. _xfs_log_recovery_validation:
+
+===================================
+XFS Log Recovery Item Validation
+===================================
+
+Problem
+=======
+
+Log recovery replays the changes recorded in the journal into the filesystem
+metadata after an unclean shutdown. Journal records are CRC protected. Storage
+failures such as torn writes and media errors are caught by the record
+checksums before any log item in the record is decoded.
+
+The checksums only protect the record contents from changing after they were
+written. They do not protect against the contents being wrong in the first
+place. A runtime bug in the code that formats log items writes a malformed
+structure into a record with a valid checksum. A fuzzer that mangles the log
+items on disk and then recalculates the record checksum produces the same
+result. Recovery trusts the structure and contents of the log items it decodes
+from such a record. Their fields are used to size memory allocations, index
+arrays, and select the metadata to read and rewrite. A malformed item can
+therefore crash the kernel, overrun memory, or write corrupt metadata into the
+filesystem.
+
+Fuzzers and LLM based code analysis are finding these malformed structure bugs
+in recovery one at a time. Each is fixed with a point check for the one field
+or path that was reported. That approach never closes the class of bug, as the
+next unchecked field is simply the next report.
+
+The aim of this design is to remove this class of bug from journal recovery
+altogether. Recovery never acts on log item contents that it has not first
+validated. Every log item is validated as it is decoded from the journal,
+before any of it is used to replay changes. A journal that fails validation is
+rejected as corrupt. The mount then fails cleanly rather than crashing or
+propagating the corruption into the filesystem.
+
+Design
+======
+
+Overview
+--------
+
+Recovery decodes the journal in stages. A log record is a sequence of
+operation headers, each followed by one fragment of a region. Fragments are
+assembled into regions, and regions are assembled into log items. Complete log
+items are queued on their transaction, and replayed when the transaction's
+commit record is seen. Each stage has a layer of validation attached to it.
+Each layer validates only what its stage can see.
+
+Layer 1 runs as each operation header is decoded from the record. An
+operation header describes the fragment that follows it. The walk over the
+record uses its length to find the next operation header. The length,
+alignment, flags, client ID and padding are checked against the values the log
+writer produces. This keeps the walk over the record in sync. It also bounds
+every fragment before anything is allocated for it. Nothing about the log
+items is known at this layer.
+
+Layer 2 runs when the first fragment of a new item arrives. That fragment may
+be only 4 bytes long, enough to hold the item type and region count. Nothing
+else in the item exists yet. The type selects the item's recovery operations,
+and those operations validate the region count. An unknown type or a bad
+region count is rejected before any memory is sized from it.
+
+Layer 3 runs each time a region of the item has been fully assembled. A region
+can be split across many fragments, so it cannot be checked until its final
+fragment has arrived. Each region is checked against the size and layout its
+item type requires. Regions arrive in order, so a region can be checked
+against the format header in region 0, which has already been validated. A
+region cannot be checked against regions that have not arrived yet.
+
+Layer 4 runs once all of the item's regions have been assembled. This is the
+first point at which the whole item is available. It checks that the regions
+are consistent with each other, and validates the contents of the item's
+fields, such as extent ranges, inode core fields and fork contents. Some item
+types also convert the item to host format here, so replay only ever sees one
+form.
+
+The layers build on each other. A layer only runs once the layers before it
+have passed, so it can rely on what they established. Region validation can
+trust the region count and the minimum size of the format header. Full item
+validation can trust that every region exists and has a valid size. Each layer
+therefore only needs to check what has newly become available to it. Region
+size is bounded at two points. Operation header validation bounds each
+fragment, and region assembly bounds the assembled region.
+
+Validation uses only the contents of the item and the in-core filesystem
+geometry. It never reads filesystem metadata. Checks that depend on the
+current on-disk state, such as whether a logged change is newer than the
+metadata it applies to, remain in replay.
+
+A failure at any layer aborts recovery with -EFSCORRUPTED, and the corrupt
+item data is dumped to the log. An item that has not passed all four layers is
+never queued for replay. A transaction whose commit record arrives while an
+item is still incomplete is rejected. Replay can therefore trust every item it
+is given, and the replay code no longer duplicates the validation checks.
+
+The transaction header at the start of each transaction is not a log item, but
+it is decoded through the same layers. It has its own item recovery
+operations, is validated like any other item, and is then consumed into the
+transaction rather than replayed.
+
+Geometry changes during recovery
+--------------------------------
+
+Filesystem geometry can change during recovery. A growfs logs the superblock
+geometry change ahead of any metadata that uses the new geometry. A shrink
+logs the superblock geometry change after the metadata in the removed space
+has been updated or invalidated. The in-core geometry changes when recovery
+replays the superblock buffer.
+
+Pass 2 recovers the journal one checkpoint at a time. The items in a
+checkpoint are decoded and validated as its records are read, and the
+checkpoint is replayed when its commit record is reached. Once a checkpoint
+has been replayed, any geometry change it contains is in effect. Later
+checkpoints are validated against the new geometry. A geometry change is
+therefore correctly ordered against the items in the checkpoints that follow
+it.
+
+The ordering problem is confined to the checkpoint that contains the geometry
+change. All of its items are decoded and validated before the checkpoint is
+replayed, so they are validated against the geometry from before the change.
+Items logged after a growfs in the same checkpoint can legitimately refer to
+inode numbers, disk addresses and extents beyond that geometry. Checkpoints
+can also overlap in the journal. The items of a later checkpoint can be
+decoded before the commit record of the checkpoint containing the geometry
+change is reached. Those items are also validated against the old geometry.
+This affects every item type that carries inode numbers, disk addresses or
+extents, including the realtime variants.
+
+Each recovery pass needs a different amount of validation, and each runs at a
+different point relative to the geometry change:
+
+- Pass 1 walks the whole journal to find buffer cancellation items before
+ anything is replayed. It must validate enough of the headers and items to
+ walk the journal safely and record the cancellations correctly. How much
+ validation that requires is not yet determined.
+
+- Pass 2 readahead decodes disk addresses and issues I/O to them before the
+ checkpoint is replayed. It needs some form of geometry validation, but it
+ runs before any geometry change in the checkpoint has taken effect.
+
+- Pass 2 replay needs the item to be fully validated. Geometry dependent
+ fields must be validated against the geometry in effect at the point the
+ item is replayed, which may differ from the geometry at decode time.
+
+How to meet all of these requirements is an open problem. Validation
+currently runs in full at decode time in both passes, against the geometry in
+effect at that point. Recovery of a geometry change can therefore fail
+validation on items that are valid.
+
+The realtime btree inode fork roots work around this for now. Their maximum
+height depends on the data and realtime device geometry, which growfs can
+change. Recovery cannot know that height until the superblock has been
+replayed, and pass 1 never replays it. Full item validation therefore bounds
+the root level by the largest height any geometry allows. The root is only
+checked against the actual geometry when the inode is loaded after recovery.
+This needs to be addressed as part of solving the geometry problem, so that
+the root level is validated against the geometry in effect when the item is
+replayed.
+
+Layer 1: Operation header decode (checkpoint based)
+---------------------------------------------------
+
+A log record carries the operation headers of one or more checkpoints. Each
+operation header carries the transaction ID of the checkpoint it belongs to,
+so the operation headers of different checkpoints can be interleaved within a
+record. Recovery tracks each open checkpoint by its transaction ID. An
+operation header with the start flag opens a new checkpoint. An operation
+header with the commit flag closes the checkpoint, and the checkpoint is
+replayed. Operation headers for a transaction ID with no open checkpoint are
+skipped, unless they carry the start flag.
+
+Between the start and commit records, each operation header carries one
+fragment of a region. A region that does not fit in the space left in a record
+is split across operation headers, and possibly across records. The flags on
+each operation header describe where its fragment sits within the region:
+
+========================================= ====================================
+Flags Fragment
+========================================= ====================================
+0 A whole region
+XLOG_CONTINUE_TRANS The first fragment of a split region
+XLOG_WAS_CONT_TRANS | XLOG_CONTINUE_TRANS A middle fragment
+XLOG_WAS_CONT_TRANS | XLOG_END_TRANS The final fragment
+========================================= ====================================
+
+Every operation header is validated by ``xlog_recover_validate_ophdr()`` as
+it is decoded, before any of its fields are acted on:
+
+- The operation header is 32 bit aligned, and lies wholly within the record.
+- The fragment length is 32 bit aligned, and the fragment lies wholly within
+ the record.
+- The fragment length is no larger than ``XLOG_MAX_REGION_SIZE``, the largest
+ region recovery assembles.
+- The client ID is ``XFS_TRANSACTION`` or ``XFS_LOG``.
+- The padding is zero.
+- The flags are one of the combinations the log writer produces. The start,
+ commit and unmount flags each stand alone, and the fragment flags must be one
+ of the combinations above.
+
+The alignment and length checks keep the walk over the record in sync with
+the on-disk layout. A misaligned or oversized length would make every
+following operation header in the record garbage. The length bound also
+limits the memory any single fragment can make recovery allocate.
+
+The start and commit records only bracket a checkpoint. They carry no data, so
+their length must be zero.
+
+The unmount record is written by the log itself rather than by a checkpoint,
+and checkpoint processing skips it. It is fully validated here. It must have
+the ``XFS_LOG`` client ID and be exactly the size of the unmount format
+structure. Its magic number is written in host byte order, and a clean log can
+be mounted on a host of either endianness, so both byte orders are accepted.
+Its padding must be zero. Some kernels wrote the unmount record with a zero
+length. Such a record has no payload to check, so it is accepted with a
+warning so those filesystems still mount.
+
+Finding the log tail reads the last record to see if it is an unmount record.
+This happens before the log has been checked for torn writes, so the record
+may legitimately be garbage. The same validation is run in a quiet mode for
+this probe. A record that fails validation is not an error here. The log is
+treated as dirty, and recovery then validates the record normally.
+
+After an operation header has been validated, its flags are checked against
+the state of its checkpoint. A start record for a checkpoint that is already
+open is corrupt. A continuation fragment must have a region under assembly to
+append to. A commit record must not arrive while an item is still
+incomplete, as every item in a checkpoint is written before its commit record.
+An unmount record carries the ``XFS_LOG`` client ID, so it never belongs to a
+checkpoint. An unmount record that matches the transaction ID of an open
+checkpoint means the checkpoint is corrupt, and it is rejected.
+
+Operation header decode also tells the later layers when a region is complete.
+A fragment without the continuation flag completes its region. That is the
+only point at which region validation can run on it.
+
+The log writer always ensures a record has room for an operation header and
+at least some of its region before starting a new region in it. The one
+exception is the transaction header at the start of a checkpoint. The
+operation header that opens it can be written with a zero length fragment,
+and the header data then follows in the continuation fragment. A zero length
+opening fragment is therefore valid only for the first region of a checkpoint.
+There is nothing to decode, so the checkpoint is left waiting for its
+transaction header. The continuation fragment that follows is decoded as the
+first fragment of the transaction header. A zero length fragment that opens
+any other region is corrupt.
+
+Layer 2: Region header decode (generic)
+---------------------------------------
+
+Region header decode starts a new log item. It runs when the first fragment of
+the item's first region arrives, in ``xlog_recover_add_to_trans()``, when there
+is no item under assembly in the checkpoint.
+
+Every log item format begins with the same two 16 bit fields: the item type,
+followed by the number of regions in the item. Region header decode reads
+these two fields and nothing else. The first fragment may be all of the first
+region or only the start of it, so nothing beyond these two fields can be
+relied on yet. Operation header validation guarantees that the fragment length
+is a non-zero multiple of 4 bytes, because a zero length opening fragment
+never reaches this point. Both fields are therefore always present.
+
+Starting an item
+~~~~~~~~~~~~~~~~
+
+``xlog_recover_add_item()`` starts the item from the two fields:
+
+- The item type is used to look up the item's recovery operations with
+ ``xlog_find_item_ops()``. An unknown type is rejected with
+ ``-EFSCORRUPTED``.
+- The region count is passed to the type's ``validate_nregions()`` method,
+ which returns the number of regions to assemble or a negative error. A bad
+ count is rejected with ``-EFSCORRUPTED``.
+- The item is allocated, with its region array sized from the validated count.
+ The recovery operations are attached to the item.
+
+The item becomes the checkpoint's current item, ``r_cur_item``. It is not
+added to the checkpoint's item queue until it has been fully assembled and has
+passed full item validation. An item that fails validation is therefore never
+visible to replay.
+
+Each subsequent fragment either continues the current region or starts the
+next region of the current item. A continuation fragment is appended to the
+region under assembly. The assembled region is bounded by
+``XLOG_MAX_REGION_SIZE`` before the region buffer is grown, so a corrupt
+stream of continuations cannot drive an unbounded allocation. A fragment that
+starts a region beyond the validated region count is rejected.
+
+Recovery operations
+~~~~~~~~~~~~~~~~~~~
+
+Region header decode uses two members of ``struct xlog_recover_item_ops``::
+
+ uint16_t min_hdr_len; /* minimum ri_buf[0].iov_len */
+ int (*validate_nregions)(struct xlog *log, uint16_t nregions);
+
+The region count is validated by a method rather than a static minimum and
+maximum. The method is given the raw count read from the journal, and returns
+the number of regions to assemble. Not every recovered type describes its
+regions with the common type and count format header, so each type must be
+able to extract and validate its own count. A type that supplies no method
+falls back to the generic ``xlog_recover_nregions()`` helper, which accepts any
+count from 1 to ``XLOG_MAX_REGIONS_IN_ITEM``.
+
+``min_hdr_len`` is the minimum valid length of the item's format header in
+region 0. It cannot be checked when the item is started, because the first
+fragment may only be the start of region 0. It is checked once region 0 is
+complete, before region validation runs on it.
+
+The region count and minimum format header length for each type are:
+
+=========== ================================= ===============================
+Item Type Regions min_hdr_len
+=========== ================================= ===============================
+TRANS_HDR 1 sizeof(xfs_trans_header)
+BUF 1 .. ``XLOG_MAX_REGIONS_IN_ITEM`` offsetof(xfs_buf_log_format,
+ blf_data_map)
+INODE 2 .. 4 sizeof(xfs_inode_log_format_32)
+DQUOT 2 sizeof(xfs_dq_logformat)
+EFI 1 sizeof(xfs_efi_log_format)
+EFD 1 sizeof(xfs_efd_log_format)
+RUI 1 sizeof(xfs_rui_log_format)
+RUD 1 sizeof(xfs_rud_log_format)
+CUI 1 sizeof(xfs_cui_log_format)
+CUD 1 sizeof(xfs_cud_log_format)
+BUI 1 sizeof(xfs_bui_log_format)
+BUD 1 sizeof(xfs_bud_log_format)
+ATTRI 2 .. 5 sizeof(xfs_attri_log_format)
+ATTRD 1 sizeof(xfs_attrd_log_format)
+XMI 1 sizeof(xfs_xmi_log_format)
+XMD 1 sizeof(xfs_xmd_log_format)
+ICREATE 1 sizeof(xfs_icreate_log)
+QUOTAOFF 1 sizeof(xfs_qoff_logformat)
+=========== ================================= ===============================
+
+The realtime variants of the intent and done items are the same as their
+non-realtime counterparts.
+
+Two types have a format header whose size varies. The BUF format header
+contains an inline dirty bitmap of variable size, so ``min_hdr_len`` covers
+only its fixed portion. Region validation checks the full header size
+including the bitmap. The INODE format header is logged in either the host
+format or a smaller 32 bit format, so ``min_hdr_len`` is the size of the
+smaller one. Region validation checks that it is exactly one of the two sizes.
+
+Transaction header as a validated type
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+The transaction header (``struct xfs_trans_header``) is the first region of
+every checkpoint. It is not a log item and is not replayed. It is still
+decoded through the same layers as the log items, so it gets the same
+validation. It has its own ``xlog_recover_item_ops`` entry:
+
+- ``min_hdr_len`` is ``sizeof(struct xfs_trans_header)``.
+- ``validate_nregions`` checks the magic number and returns a region count of
+ 1, as described below.
+- ``validate_region`` checks that the region is exactly
+ ``sizeof(struct xfs_trans_header)`` long, and checks the magic number.
+- ``complete`` copies the header into ``trans->r_theader`` and returns
+ ``XLOG_RECOVER_ITEM_CONSUMED``, so the item is freed rather than queued for
+ replay.
+
+The header does not start with the type and count fields of a log item
+format. Its first 32 bits are ``th_magic``. When region header decode reads
+the first two 16 bit words as the type and region count, they are the two
+halves of the magic number. The type half selects the ops entry.
+``validate_nregions`` checks that the count half holds the other half of the
+magic, and returns 1.
+
+The XFS log is written in host byte order, so which half of the magic is read
+as the type depends on the endianness of the host that wrote the log. There is
+an ops entry for each byte order, with ``item_type`` of ``0x414e`` and
+``0x5452``. Each checks for the complementary half. On any given host only one
+of them is ever matched. Neither type code collides with an ``XFS_LI_*`` value.
+
+A zero length opening fragment is valid only for the transaction header, as
+described for operation header validation. The continuation that follows it
+is decoded as the header's first fragment, and the header is then assembled
+and validated like any other single region item.
+
+Layer 3: Per-region validation (type-specific)
+----------------------------------------------
+
+Region validation checks each region of an item as soon as it has been fully
+assembled. It ensures that every region is the size and shape its item type
+requires before anything indexes into it. Without it, a region that is shorter
+than the structure it holds would be read past its end. A region that is
+longer than its structure allows would hide trailing garbage. Rejecting a bad
+region as soon as it is complete also stops recovery from assembling the rest
+of a corrupt item.
+
+Region validation has two constraints. A region can only be checked once it
+is complete, which may be many fragments after it started. And regions
+complete in order, so a region can only be checked against the regions before
+it. Any check that needs a later region has to wait for full item validation.
+
+The format header
+~~~~~~~~~~~~~~~~~
+
+Region 0 of every item holds the item's log format structure, its format
+header. The format header describes how the rest of the item is laid out. It
+can record which optional regions are present, the size of the variable length
+regions, and the number of entries in an array within a region. It is the
+first region to complete, and every later region is interpreted through it.
+
+The format header is therefore validated before any other region. Its length
+is checked against ``min_hdr_len``, and then against the exact sizes its type
+allows. The format header fields that describe the item's region structure are
+checked to the extent they can be from region 0 alone. Later regions can then
+be checked against a format header that is known to be well formed.
+
+Some format header fields describe regions whose layout also depends on
+another region. The inode fork regions are an example. Their size depends on
+both the format header and the fork formats and offsets in the inode core. Such
+fields can only be checked once every region they relate to is present, so
+they are left to full item validation.
+
+A format header can be logged in more than one layout for the same type. The
+log is written in host byte order, but some format structures differ in size
+and alignment between 32 and 64 bit hosts. Region validation accepts each
+layout the format header may legitimately have been logged in. Conversion to
+a single host layout happens during full item validation.
+
+Region completion and continuations
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+A region is validated as soon as it has been fully rebuilt, and never before.
+A region that arrives whole in a single fragment is complete as soon as it is
+added to the item. A region split across fragments is complete only once its
+final fragment has been appended. Each continuation fragment is appended to
+the region under assembly. The assembled size must stay within
+``XLOG_MAX_REGION_SIZE`` as each fragment is added, so a corrupt stream of
+continuations cannot drive an unbounded allocation.
+
+Region 0 must be at least ``min_hdr_len`` long before its contents are
+validated. Each completed region is then validated by its type. Once the last
+region of the item has been validated, the item passes to full item
+validation. A region that fails validation is dumped to the log, and recovery
+is aborted with ``-EFSCORRUPTED``.
+
+The validation method
+~~~~~~~~~~~~~~~~~~~~~
+
+Region validation adds one method to ``struct xlog_recover_item_ops``::
+
+ int (*validate_region)(struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index);
+
+It is called with the index of the region that has just completed, and
+returns 0 or a negative error. Every region before ``region_index`` has
+already passed validation, so the method can rely on them.
+
+Item region validation
+~~~~~~~~~~~~~~~~~~~~~~
+
+Transaction header
+ - Length is exactly the size of the header.
+ - The magic number is valid.
+
+EFI, RUI, CUI and their realtime variants
+ - The extent count is non-zero.
+ - The extent count is bounded so the size calculation cannot overflow.
+ - Length matches the format header plus that many extents.
+ - EFI extents may use either the packed 12 byte layout written by i386
+ hosts or the padded 16 byte layout written by all other hosts. RUI and
+ CUI extents have a single layout.
+
+BUI
+ - The extent count is exactly one.
+ - Length matches the format header plus one extent.
+
+EFD and its realtime variant
+ - The extent count is non-zero.
+ - The extent count is bounded so the size calculation cannot overflow.
+ - Length matches the format header plus that many extents, in either of
+ the two EFI extent layouts.
+
+RUD, CUD, BUD, XMD and ATTRD
+ - Length is exactly the size of the format structure.
+ - The padding is zero.
+
+XMI and ICREATE
+ - Length is exactly the size of the format structure.
+
+QUOTAOFF
+ - Length is 160 or 88 bytes, the 64 bit and 32 bit sizes of the in-core
+ quota off log item that v5.14 kernels logged. Only the leading format
+ structure was written.
+ - The flags are a non-zero set of quota accounting flags.
+
+DQUOT
+ - Region 0 is exactly the size of the dquot format header.
+ - Region 1 is exactly the size of an on-disk dquot.
+
+ATTRI
+ - Region 0 is exactly the size of the format header.
+ - Region 1 is a name, no longer than the largest attribute name.
+ - Regions 2 to 4 are names or values, no longer than the largest attribute
+ value.
+
+BUF
+ - The dirty bitmap size is no larger than the largest bitmap.
+ - The format header, including the bitmap, fits within region 0.
+ - Each data region is non-zero in length.
+ - Each data region is a multiple of the logging chunk size.
+
+INODE
+ - Region 0 is exactly the size of either the host or the 32 bit format
+ header layout.
+ - Region 1 is exactly the log dinode core size of the running filesystem.
+ - The data and attribute fork regions are validated with the whole item,
+ as their presence and size depend on the format header and the inode
+ core.
+
+Layer 4: Full item validation (type-specific, cross-region)
+-----------------------------------------------------------
+
+Full item validation runs once every region of an item has been assembled and
+has passed region validation. It is the last check before the item is queued
+on its checkpoint for replay. This is the first point at which the whole item
+is available. Relationships between regions, and between the format header
+and the regions it describes, can only be checked here.
+
+Full item validation can rely on everything the earlier layers established.
+The item has the region count its type allows. Every region is complete, and
+is the right size for what it holds. The format header is well formed. Full
+item validation therefore checks only what needs more than one region, or
+needs the region contents to be interpreted:
+
+- The format header agrees with the regions that are actually present.
+- The fields of the item hold valid values, such as known flags and operation
+ types, valid inode numbers, and extents that lie within the filesystem.
+- Where a region carries a recognisable structure, that structure is
+ consistent with the rest of the item.
+
+Some item types also convert their format header or contents to the host
+layout here. Replay then only ever sees one layout, and contains no
+conversion or layout specific code.
+
+Constraints
+~~~~~~~~~~~
+
+Full item validation cannot verify the complete metadata structures an item
+changes. A log item records changes to a structure, not necessarily the whole
+structure. A buffer item logs only the dirty ranges of a buffer, so its data
+regions hold fragments of a metadata block. An inode item can log the core
+without the forks. A structure may also be changed by several items in
+different checkpoints. Its full state only exists once all of them have been
+replayed into it.
+
+Complete structures are therefore verified when the recovered metadata is
+written back. Each recovered buffer is written with its metadata verifier
+attached, so the write verifier checks the whole block after replay. A
+recovered inode is verified after its logged state has been copied into the
+inode buffer. Checks that need the current on-disk metadata are also made
+during replay. For example, the buffer type recorded in the log item must
+match the metadata found in the buffer being replayed into.
+
+Full item validation never reads filesystem metadata. It uses only the
+contents of the item and the in-core filesystem geometry. Validation of
+geometry dependent fields is subject to the limitations described in the
+section on geometry changes during recovery.
+
+The validation methods
+~~~~~~~~~~~~~~~~~~~~~~
+
+Full item validation adds two methods to ``struct xlog_recover_item_ops``::
+
+ int (*validate_item)(struct xlog *log,
+ struct xlog_recover_item *item);
+ int (*complete)(struct xlog *log, struct xlog_recover *trans,
+ struct xlog_recover_item *item);
+
+``validate_item`` validates the whole item, and may convert it to the host
+layout. It returns 0 or a negative error. ``complete`` allows a type to
+consume the item rather than have it queued for replay. Only the transaction
+header uses it. A failure is dumped to the log, and recovery is aborted with
+``-EFSCORRUPTED``.
+
+Item validation
+~~~~~~~~~~~~~~~
+
+EFI, EFD and their realtime variants
+ - The extents are converted to the host layout.
+ - Each extent lies within the data device, or within the realtime device
+ for the realtime variants.
+
+RUI and its realtime variant
+ - Each mapping has a known operation type and valid flags.
+ - Each mapping's owner and file offset are valid.
+ - Each mapping's extent lies within its device.
+
+CUI and its realtime variant
+ - Each extent has a known operation type.
+ - Each extent lies within its device.
+
+BUI
+ - The mapping has a known operation type and valid flags.
+ - The inode number and file offset are valid.
+ - The extent lies within its device.
+
+XMI
+ - The padding is zero and the flags are known.
+ - Both inode numbers are valid.
+ - Both file ranges and the file sizes are valid.
+
+DQUOT
+ - The buffer length is exactly one dquot cluster.
+ - The dquot's offset within the buffer matches its dquot ID.
+ - The dquot fits within the buffer.
+ - The buffer address is a valid filesystem block.
+ - The logged dquot passes the dquot verifier.
+
+ATTRI
+ - The operation, flags and name and value lengths in the format header are
+ valid.
+ - The region count is exactly what the operation requires.
+ - Each name and value region matches the length recorded for it in the
+ format header.
+ - Each name is a valid attribute name.
+
+BUF
+ - The item size in the format header matches the region count.
+ - The buffer length is non-zero and no larger than ``XLOG_MAX_REGION_SIZE``.
+ - The buffer lies wholly within the data device.
+ - A cancelled buffer logs only its format header.
+ - Any other buffer logs at least one data region.
+ - A type flag, if set, matches the buffer type. The type flags select
+ special replay handling, so a buffer logged for full replay carries only
+ its type, even if it holds inodes or dquots.
+ - Only a V4 filesystem may log an untyped buffer, with at most one type
+ flag set.
+ - Each run of dirty bits maps to exactly one data region of the same
+ length, within the buffer.
+ - The total dirty data is no larger than the buffer.
+ - A buffer with a dquot type flag logs one data region covering the whole
+ buffer, and every dquot in it passes the dquot verifier.
+
+INODE
+ - The format header is converted to the host layout.
+ - The inode number is valid.
+ - The inode core is logged.
+ - The item size in the format header matches the region count.
+ - The region count matches the fork regions the format header flags say
+ are present.
+ - The cluster buffer length matches the inode cluster size.
+ - The cluster buffer address and offset match the inode number, where the
+ inode geometry allows them to be calculated without a btree lookup.
+ - The log dinode core passes a host order counterpart of the on-disk
+ inode verifier.
+ - Each fork format is valid for the inode mode.
+ - Each logged fork's flag matches its fork format.
+ - Each fork region's length matches the fork size in the format header.
+ - A local or extents format fork fits within the space the fork offset
+ gives it.
+ - A local data fork is a valid shortform directory or symlink target, and
+ a local attribute fork is a valid shortform attribute fork.
+ - Every record in an extents format fork is valid.
+ - A btree format fork root has a valid header and level, a record count
+ that matches its size, and valid keys and pointers.
+ - A metadata btree fork root has a valid header, a level no deeper than
+ any geometry allows, a record count that matches its size, and valid
+ node pointers. Leaf records are not checked, because the realtime
+ groups they refer to are not set up until after recovery. Tighter
+ validation of the root level depends on solving the problem described
+ in the section on geometry changes during recovery.
+
+ICREATE, QUOTAOFF, RUD, CUD, BUD, XMD and ATTRD are fully validated by region
+validation, and have no full item validation.
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 13/59] xfs: lift transaction header parsing out of the region assembly
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (11 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 12/59] xfs: add log recovery item validation design document Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 14/59] xfs: track transaction header decode state explicitly Dave Chinner
` (45 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The transaction header at the start of a recovered transaction is not a log
item. Its parsing is tangled into the generic log item region assembly code.
Both region assembly functions carry a special case that decodes the header
instead of a log item region while the item queue is empty.
Move the header decoding into a single helper called before log item region
processing. The two header decode paths are merged, so the header length is
checked in one place. A header fragment must now be at least 4 bytes, the
minimum needed to read the magic number. The bad magic warning reports the
observed and expected values. The ASSERT(0) calls on the corruption paths are
removed so debug builds exercise the error paths.
The header is still reassembled in place. The first log item is still added
once the header is complete. Removing the header special cases allows the
region assembly functions to be simplified in following patches.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 113 +++++++++++++++++++++++++--------------
1 file changed, 73 insertions(+), 40 deletions(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index fd356b09f9d1..494bf5ebfa7b 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2108,35 +2108,76 @@ xlog_recover_add_item(
list_add_tail(&item->ri_list, head);
}
+/*
+ * Decode the transaction header at the start of a new transaction.
+ *
+ * The transaction header is not a log item. It can be arbitrarily split across
+ * op records, so it may arrive whole in a single op record, or as an initial
+ * fragment (cont == false) followed by one or more continuation fragments
+ * (cont == true). Reassemble it directly into r_theader where the rest of
+ * recovery expects to find it.
+ */
STATIC int
-xlog_recover_add_to_cont_trans(
+xlog_recover_init_new_trans(
struct xlog *log,
struct xlog_recover *trans,
char *dp,
- int len)
+ int len,
+ bool cont)
{
- struct xlog_recover_item *item;
- char *ptr, *old_ptr;
- int old_len;
+ char *ptr;
/*
- * If the transaction is empty, the header was split across this and the
- * previous record. Copy the rest of the header.
+ * All regions are 32 bit aligned, so the smallest valid fragment is 4
+ * bytes; that is also the minimum needed to read the magic number. A
+ * fragment larger than the whole header is corruption in either case.
*/
- if (list_empty(&trans->r_itemq)) {
- ASSERT(len <= sizeof(struct xfs_trans_header));
- if (len > sizeof(struct xfs_trans_header)) {
- xfs_warn(log->l_mp, "%s: bad header length", __func__);
+ if (len < 4 || len > sizeof(struct xfs_trans_header)) {
+ xfs_warn(log->l_mp, "%s: bad header length %d", __func__, len);
+ return -EFSCORRUPTED;
+ }
+
+ if (!cont) {
+ /*
+ * Initial fragment. The magic number is at the start of the
+ * header, which lands at the start of r_theader.
+ */
+ if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
+ xfs_warn(log->l_mp,
+ "%s: bad header magic number 0x%x, expected 0x%x",
+ __func__, *(uint *)dp, XFS_TRANS_HEADER_MAGIC);
return -EFSCORRUPTED;
}
-
- xlog_recover_add_item(&trans->r_itemq);
+ ptr = (char *)&trans->r_theader;
+ } else {
+ /* continuation fragment completes the tail of the header */
ptr = (char *)&trans->r_theader +
sizeof(struct xfs_trans_header) - len;
- memcpy(ptr, dp, len);
- return 0;
}
+ /*
+ * Add an item to the transaction once the header is complete: for an
+ * unsplit header that is this initial fragment (len == full header),
+ * and for a split header it is the continuation fragment that finishes
+ * it.
+ */
+ if (cont || len == sizeof(struct xfs_trans_header))
+ xlog_recover_add_item(&trans->r_itemq);
+ memcpy(ptr, dp, len);
+ return 0;
+}
+
+STATIC int
+xlog_recover_add_to_cont_trans(
+ struct xlog *log,
+ struct xlog_recover *trans,
+ char *dp,
+ int len)
+{
+ struct xlog_recover_item *item;
+ char *ptr, *old_ptr;
+ int old_len;
+
/* take the tail entry */
item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
ri_list);
@@ -2180,31 +2221,6 @@ xlog_recover_add_to_trans(
if (!len)
return 0;
- if (list_empty(&trans->r_itemq)) {
- /* we need to catch log corruptions here */
- if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
- xfs_warn(log->l_mp, "%s: bad header magic number",
- __func__);
- ASSERT(0);
- return -EFSCORRUPTED;
- }
-
- if (len > sizeof(struct xfs_trans_header)) {
- xfs_warn(log->l_mp, "%s: bad header length", __func__);
- ASSERT(0);
- return -EFSCORRUPTED;
- }
-
- /*
- * The transaction header can be arbitrarily split across op
- * records. If we don't have the whole thing here, copy what we
- * do have and handle the rest in the next record.
- */
- if (len == sizeof(struct xfs_trans_header))
- xlog_recover_add_item(&trans->r_itemq);
- memcpy(&trans->r_theader, dp, len);
- return 0;
- }
ptr = xlog_kvmalloc(len);
memcpy(ptr, dp, len);
@@ -2302,6 +2318,21 @@ xlog_recovery_process_trans(
if (flags & XLOG_WAS_CONT_TRANS)
flags &= ~XLOG_CONTINUE_TRANS;
+ /*
+ * An empty item queue means we are still decoding the transaction
+ * header at the start of the transaction rather than a log item. The
+ * header can arrive whole (flags == 0), as an initial fragment
+ * (XLOG_CONTINUE_TRANS) or as a continuation fragment
+ * (XLOG_WAS_CONT_TRANS); XLOG_WAS_CONT_TRANS distinguishes the
+ * continuation.
+ */
+ if (list_empty(&trans->r_itemq) &&
+ (!flags || (flags & (XLOG_CONTINUE_TRANS | XLOG_WAS_CONT_TRANS)))) {
+ error = xlog_recover_init_new_trans(log, trans, dp, len,
+ flags & XLOG_WAS_CONT_TRANS);
+ goto out_error;
+ }
+
/*
* Callees must not free the trans structure. We'll decide if we need to
* free it or not based on the operation being done and it's result.
@@ -2335,6 +2366,8 @@ xlog_recovery_process_trans(
error = -EFSCORRUPTED;
break;
}
+
+out_error:
if (error || freeit)
xlog_recover_free_trans(trans);
return error;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 14/59] xfs: track transaction header decode state explicitly
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (12 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 13/59] xfs: lift transaction header parsing out of the region assembly Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 15/59] xfs: track the log item being rebuilt with an explicit pointer Dave Chinner
` (44 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Recovery decides whether an incoming op record is part of the transaction
header by checking whether the transaction item queue is empty. That is an
indirect signal. It only works because no item is added until the header is
complete.
Add an explicit flag to the recovery transaction. It is set once the header
has been fully assembled, at the same point the first item is added. Use it to
route op records instead of the item queue state. There is no functional
change.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_log_recover.h | 1 +
fs/xfs/xfs_log_recover.c | 24 ++++++++++++++----------
2 files changed, 15 insertions(+), 10 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_log_recover.h b/fs/xfs/libxfs/xfs_log_recover.h
index 9e712e62369c..ad73a1a6abf8 100644
--- a/fs/xfs/libxfs/xfs_log_recover.h
+++ b/fs/xfs/libxfs/xfs_log_recover.h
@@ -115,6 +115,7 @@ struct xlog_recover {
int r_state; /* not needed */
xfs_lsn_t r_lsn; /* xact lsn */
struct list_head r_itemq; /* q for items */
+ bool r_hdr_decoded; /* trans header fully decoded */
};
#define ITEM_TYPE(i) (*(unsigned short *)(i)->ri_buf[0].iov_base)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 494bf5ebfa7b..c50f85757216 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2155,15 +2155,19 @@ xlog_recover_init_new_trans(
sizeof(struct xfs_trans_header) - len;
}
+ memcpy(ptr, dp, len);
+
/*
- * Add an item to the transaction once the header is complete: for an
- * unsplit header that is this initial fragment (len == full header),
+ * The header is complete once the full struct has been assembled: for
+ * an unsplit header that is this initial fragment (len == full header),
* and for a split header it is the continuation fragment that finishes
- * it.
+ * it. Mark it decoded and add the item that subsequent log item regions
+ * will be decoded into.
*/
- if (cont || len == sizeof(struct xfs_trans_header))
+ if (cont || len == sizeof(struct xfs_trans_header)) {
+ trans->r_hdr_decoded = true;
xlog_recover_add_item(&trans->r_itemq);
- memcpy(ptr, dp, len);
+ }
return 0;
}
@@ -2319,14 +2323,14 @@ xlog_recovery_process_trans(
flags &= ~XLOG_CONTINUE_TRANS;
/*
- * An empty item queue means we are still decoding the transaction
- * header at the start of the transaction rather than a log item. The
- * header can arrive whole (flags == 0), as an initial fragment
- * (XLOG_CONTINUE_TRANS) or as a continuation fragment
+ * Until the transaction header has been fully decoded we are still
+ * assembling it at the start of the transaction rather than decoding a
+ * log item. The header can arrive whole (flags == 0), as an initial
+ * fragment (XLOG_CONTINUE_TRANS) or as a continuation fragment
* (XLOG_WAS_CONT_TRANS); XLOG_WAS_CONT_TRANS distinguishes the
* continuation.
*/
- if (list_empty(&trans->r_itemq) &&
+ if (!trans->r_hdr_decoded &&
(!flags || (flags & (XLOG_CONTINUE_TRANS | XLOG_WAS_CONT_TRANS)))) {
error = xlog_recover_init_new_trans(log, trans, dp, len,
flags & XLOG_WAS_CONT_TRANS);
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 15/59] xfs: track the log item being rebuilt with an explicit pointer
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (13 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 14/59] xfs: track transaction header decode state explicitly Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 16/59] xfs: complete log items explicitly during recovery Dave Chinner
` (43 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Recovery finds the log item being reassembled from op records by taking the
tail entry of the transaction item queue. This lookup is open coded in several
places. It relies on the item being rebuilt always being the last one on the
queue.
Track the current item with an explicit pointer in the recovery transaction
instead. Adding an item now records it as the current item and returns it to
the caller. The pointer is set when the transaction header completes, so it is
valid before any log item region is decoded. There is no functional change.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_log_recover.h | 1 +
fs/xfs/xfs_log_recover.c | 29 +++++++++++++----------------
2 files changed, 14 insertions(+), 16 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_log_recover.h b/fs/xfs/libxfs/xfs_log_recover.h
index ad73a1a6abf8..1fef69ec918c 100644
--- a/fs/xfs/libxfs/xfs_log_recover.h
+++ b/fs/xfs/libxfs/xfs_log_recover.h
@@ -116,6 +116,7 @@ struct xlog_recover {
xfs_lsn_t r_lsn; /* xact lsn */
struct list_head r_itemq; /* q for items */
bool r_hdr_decoded; /* trans header fully decoded */
+ struct xlog_recover_item *r_cur_item; /* item currently being rebuilt */
};
#define ITEM_TYPE(i) (*(unsigned short *)(i)->ri_buf[0].iov_base)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index c50f85757216..404e27fe2337 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2097,15 +2097,19 @@ xlog_recover_commit_trans(
return error;
}
-STATIC void
+STATIC struct xlog_recover_item *
xlog_recover_add_item(
- struct list_head *head)
+ struct xlog_recover *trans)
{
struct xlog_recover_item *item;
item = kzalloc_obj(struct xlog_recover_item, GFP_KERNEL | __GFP_NOFAIL);
INIT_LIST_HEAD(&item->ri_list);
- list_add_tail(&item->ri_list, head);
+ list_add_tail(&item->ri_list, &trans->r_itemq);
+
+ /* this is now the item being rebuilt */
+ trans->r_cur_item = item;
+ return item;
}
/*
@@ -2166,7 +2170,7 @@ xlog_recover_init_new_trans(
*/
if (cont || len == sizeof(struct xfs_trans_header)) {
trans->r_hdr_decoded = true;
- xlog_recover_add_item(&trans->r_itemq);
+ xlog_recover_add_item(trans);
}
return 0;
}
@@ -2178,14 +2182,10 @@ xlog_recover_add_to_cont_trans(
char *dp,
int len)
{
- struct xlog_recover_item *item;
+ struct xlog_recover_item *item = trans->r_cur_item;
char *ptr, *old_ptr;
int old_len;
- /* take the tail entry */
- item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
- ri_list);
-
old_ptr = item->ri_buf[item->ri_cnt-1].iov_base;
old_len = item->ri_buf[item->ri_cnt-1].iov_len;
@@ -2230,15 +2230,12 @@ xlog_recover_add_to_trans(
memcpy(ptr, dp, len);
in_f = (struct xfs_inode_log_format *)ptr;
- /* take the tail entry */
- item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
- ri_list);
+ /* the item currently being rebuilt */
+ item = trans->r_cur_item;
if (item->ri_total != 0 &&
item->ri_total == item->ri_cnt) {
- /* tail item is in use, get a new one */
- xlog_recover_add_item(&trans->r_itemq);
- item = list_entry(trans->r_itemq.prev,
- struct xlog_recover_item, ri_list);
+ /* current item is full, start a new one */
+ item = xlog_recover_add_item(trans);
}
if (item->ri_total == 0) { /* first region to be added */
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 16/59] xfs: complete log items explicitly during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (14 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 15/59] xfs: track the log item being rebuilt with an explicit pointer Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 17/59] xfs: add log item validation infrastructure to log recovery Dave Chinner
` (42 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Log recovery only noticed that an item was complete when the next region
arrived and found the current item's region count had reached its total. An
item was added to the transaction item queue as soon as its first region was
decoded, so a partially assembled item sat on the queue alongside complete
ones.
Rework item assembly so that items are started and completed explicitly. An
item being rebuilt is tracked only as the current item and is not on the item
queue. It is moved onto the queue once its final region has been fully
decoded, and the next region then starts a new item. Region completion is
taken from the continuation flags of each operation header, so an item
completes at the point its last region finishes rather than when the next
region turns up.
The transaction header is a single region, and it is now marked decoded when
that region completes. It no longer creates a placeholder item for the first
log item to reuse, so every log item starts from a fresh item.
The commit record is written after all the items in the transaction. An item
still being assembled when the commit record is seen means the transaction is
incomplete. Recovery now fails with a corruption error in that case, rather
than replaying the incomplete transaction.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 141 ++++++++++++++++++++++++++++++---------
1 file changed, 109 insertions(+), 32 deletions(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 404e27fe2337..e5eca851d497 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2105,13 +2105,31 @@ xlog_recover_add_item(
item = kzalloc_obj(struct xlog_recover_item, GFP_KERNEL | __GFP_NOFAIL);
INIT_LIST_HEAD(&item->ri_list);
- list_add_tail(&item->ri_list, &trans->r_itemq);
- /* this is now the item being rebuilt */
+ /*
+ * The item is not placed on the transaction's item queue until it has
+ * been fully decoded (see xlog_recover_complete_item()). Until then it
+ * is tracked as the item currently being rebuilt.
+ */
trans->r_cur_item = item;
return item;
}
+/*
+ * A log item has been fully decoded from the journal. Move it onto the
+ * transaction's item queue and clear the current item so the next region
+ * decoded starts a new item.
+ */
+static void
+xlog_recover_complete_item(
+ struct xlog_recover *trans)
+{
+ struct xlog_recover_item *item = trans->r_cur_item;
+
+ list_add_tail(&item->ri_list, &trans->r_itemq);
+ trans->r_cur_item = NULL;
+}
+
/*
* Decode the transaction header at the start of a new transaction.
*
@@ -2127,7 +2145,8 @@ xlog_recover_init_new_trans(
struct xlog_recover *trans,
char *dp,
int len,
- bool cont)
+ bool cont,
+ bool region_complete)
{
char *ptr;
@@ -2162,16 +2181,13 @@ xlog_recover_init_new_trans(
memcpy(ptr, dp, len);
/*
- * The header is complete once the full struct has been assembled: for
- * an unsplit header that is this initial fragment (len == full header),
- * and for a split header it is the continuation fragment that finishes
- * it. Mark it decoded and add the item that subsequent log item regions
- * will be decoded into.
+ * The transaction header is a single region, so it is fully decoded
+ * once the region is complete. The header is not a log item, so nothing
+ * is added to the item queue here; the first log item region decoded
+ * after this will start the first item.
*/
- if (cont || len == sizeof(struct xfs_trans_header)) {
+ if (region_complete)
trans->r_hdr_decoded = true;
- xlog_recover_add_item(trans);
- }
return 0;
}
@@ -2180,7 +2196,8 @@ xlog_recover_add_to_cont_trans(
struct xlog *log,
struct xlog_recover *trans,
char *dp,
- int len)
+ int len,
+ bool region_complete)
{
struct xlog_recover_item *item = trans->r_cur_item;
char *ptr, *old_ptr;
@@ -2196,6 +2213,14 @@ xlog_recover_add_to_cont_trans(
item->ri_buf[item->ri_cnt-1].iov_len += len;
item->ri_buf[item->ri_cnt-1].iov_base = ptr;
trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
+
+ /*
+ * The continued region is only complete once its final fragment has
+ * been appended. When that finishes the item's last region, the item is
+ * complete.
+ */
+ if (region_complete && item->ri_cnt == item->ri_total)
+ xlog_recover_complete_item(trans);
return 0;
}
@@ -2217,9 +2242,9 @@ xlog_recover_add_to_trans(
struct xlog *log,
struct xlog_recover *trans,
char *dp,
- int len)
+ int len,
+ bool region_complete)
{
- struct xfs_inode_log_format *in_f; /* any will do */
struct xlog_recover_item *item;
char *ptr;
@@ -2228,17 +2253,17 @@ xlog_recover_add_to_trans(
ptr = xlog_kvmalloc(len);
memcpy(ptr, dp, len);
- in_f = (struct xfs_inode_log_format *)ptr;
- /* the item currently being rebuilt */
+ /*
+ * If there is no item currently being rebuilt, this region is the first
+ * region of a new item. Start the item and use its first region to
+ * determine how many regions it has.
+ */
item = trans->r_cur_item;
- if (item->ri_total != 0 &&
- item->ri_total == item->ri_cnt) {
- /* current item is full, start a new one */
- item = xlog_recover_add_item(trans);
- }
+ if (!item) {
+ struct xfs_inode_log_format *in_f = /* any will do */
+ (struct xfs_inode_log_format *)ptr;
- if (item->ri_total == 0) { /* first region to be added */
if (in_f->ilf_size == 0 ||
in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
xfs_warn(log->l_mp,
@@ -2249,6 +2274,7 @@ xlog_recover_add_to_trans(
return -EFSCORRUPTED;
}
+ item = xlog_recover_add_item(trans);
item->ri_total = in_f->ilf_size;
item->ri_buf = kzalloc_objs(*item->ri_buf, item->ri_total,
GFP_KERNEL | __GFP_NOFAIL);
@@ -2268,6 +2294,14 @@ xlog_recover_add_to_trans(
item->ri_buf[item->ri_cnt].iov_len = len;
item->ri_cnt++;
trace_xfs_log_recover_item_add(log, trans, item, 0);
+
+ /*
+ * The item is complete once its final region has been fully decoded. If
+ * this region continues into the next op record it is not complete yet,
+ * so the item cannot be either.
+ */
+ if (region_complete && item->ri_cnt == item->ri_total)
+ xlog_recover_complete_item(trans);
return 0;
}
@@ -2276,24 +2310,40 @@ xlog_recover_add_to_trans(
*
* Remember that EFIs, EFDs, and IUNLINKs are handled later.
*/
+static void
+xlog_recover_free_item(
+ struct xlog_recover_item *item)
+{
+ int i;
+
+ /* Free the regions in the item. */
+ for (i = 0; i < item->ri_cnt; i++)
+ kvfree(item->ri_buf[i].iov_base);
+ /* Free the item itself */
+ kfree(item->ri_buf);
+ kfree(item);
+}
+
STATIC void
xlog_recover_free_trans(
struct xlog_recover *trans)
{
struct xlog_recover_item *item, *n;
- int i;
hlist_del_init(&trans->r_list);
list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
- /* Free the regions in the item. */
list_del(&item->ri_list);
- for (i = 0; i < item->ri_cnt; i++)
- kvfree(item->ri_buf[i].iov_base);
- /* Free the item itself */
- kfree(item->ri_buf);
- kfree(item);
+ xlog_recover_free_item(item);
}
+
+ /*
+ * An item still being rebuilt has not been added to the item queue yet,
+ * so free it separately.
+ */
+ if (trans->r_cur_item)
+ xlog_recover_free_item(trans->r_cur_item);
+
/* Free the transaction recover structure */
kfree(trans);
}
@@ -2313,6 +2363,17 @@ xlog_recovery_process_trans(
{
int error = 0;
bool freeit = false;
+ bool region_complete;
+
+ /*
+ * If XLOG_CONTINUE_TRANS is set the current region is only a partial
+ * fragment that continues into the next op record, so it cannot be
+ * completed yet. The region is complete once this flag is clear, which
+ * is the case for both an unsplit region and the final (XLOG_END_TRANS)
+ * fragment of a split region. Capture this before the flags are masked
+ * below.
+ */
+ region_complete = !(flags & XLOG_CONTINUE_TRANS);
/* mask off ophdr transaction container flags */
flags &= ~XLOG_END_TRANS;
@@ -2330,7 +2391,8 @@ xlog_recovery_process_trans(
if (!trans->r_hdr_decoded &&
(!flags || (flags & (XLOG_CONTINUE_TRANS | XLOG_WAS_CONT_TRANS)))) {
error = xlog_recover_init_new_trans(log, trans, dp, len,
- flags & XLOG_WAS_CONT_TRANS);
+ flags & XLOG_WAS_CONT_TRANS,
+ region_complete);
goto out_error;
}
@@ -2342,12 +2404,27 @@ xlog_recovery_process_trans(
/* expected flag values */
case 0:
case XLOG_CONTINUE_TRANS:
- error = xlog_recover_add_to_trans(log, trans, dp, len);
+ error = xlog_recover_add_to_trans(log, trans, dp, len,
+ region_complete);
break;
case XLOG_WAS_CONT_TRANS:
- error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
+ error = xlog_recover_add_to_cont_trans(log, trans, dp, len,
+ region_complete);
break;
case XLOG_COMMIT_TRANS:
+ /*
+ * The commit record is written after all items in the
+ * transaction have been written to the journal. Hence we
+ * should have completely processed all items in the
+ * transaction before we see the commit record.
+ */
+ if (trans->r_cur_item) {
+ xfs_warn(log->l_mp,
+ "%s: transaction 0x%x committed with an incomplete log item",
+ __func__, trans->r_log_tid);
+ error = -EFSCORRUPTED;
+ break;
+ }
error = xlog_recover_commit_trans(log, trans, pass,
buffer_list);
/* success or fail, we are now done with this transaction. */
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 17/59] xfs: add log item validation infrastructure to log recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (15 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 16/59] xfs: complete log items explicitly during recovery Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 18/59] xfs: recover the transaction header as a validated item type Dave Chinner
` (41 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Log recovery decodes log items straight out of the journal and trusts the
region layout it finds there. Corrupt or malicious journal contents can
therefore drive recovery with bogus type codes, region counts and header
lengths before any validation happens. Add the infrastructure that later
patches will use to validate each item as it is decoded, and wire the
generic validation call sites into the region assembly path so per-type
patches only need to supply the checks.
struct xlog_recover_item_ops gains a min_hdr_len field (the minimum valid
length of the format header in ri_buf[0]) and four optional callbacks:
validate_nregions() returns the number of regions to assemble for the item;
validate_region() checks a single region once it has been fully assembled;
validate_item() checks the whole item once all of its regions are present;
and complete() is a per-type completion hook run once the item is fully
assembled and validated.
The region count is a per-type method rather than a static bound because not
all recovered types describe their regions with the same "type/size" format
header that log items use. Each such type needs to extract and validate its
own region count. Types that do use the common layout supply no method and
fall back to the generic xlog_recover_nregions() helper, which accepts any
0 < count <= XLOG_MAX_REGIONS_IN_ITEM.
Item completion is driven through a shared path.
xlog_recover_complete_region() runs the min_hdr_len check on the format
header and the type's validate_region() callback once a region is complete,
then completes the item via xlog_recover_complete_item() - which runs
validate_item() - once the final region has been assembled. A region is
complete when it is not a partial continuation fragment, tracked with a
region_complete flag derived from XLOG_CONTINUE_TRANS in
xlog_recovery_process_trans() and passed into both
xlog_recover_add_to_trans() and xlog_recover_add_to_cont_trans(). Running
the header and region checks only at region-completion time means a region
split across op records is not rejected while it is still being assembled.
A completed item is queued on the transaction for replay by default. The
complete() hook lets a type do type-specific work at completion first, and it
may consume the item there and then rather than have it queued for later
recovery: returning XLOG_RECOVER_ITEM_CONSUMED frees the item instead of
queuing it, while returning 0 falls through to the default queuing. This
supports items that need finalising during decode, including pseudo-items
that are consumed rather than replayed. No type supplies the hook yet.
The ops lookup, unknown-type rejection and region-count validation move into
xlog_recover_add_item() as part of this, so the lookup is no longer done in
xlog_recover_reorder_trans() and xlog_find_item_ops() now takes the type
code directly. No item type supplies the new field or callbacks yet, so the
generic helper is always used and there is no functional change beyond the
unknown-type rejection moving earlier in decode.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_log_recover.h | 53 +++++++
fs/xfs/xfs_log_recover.c | 238 +++++++++++++++++++++++---------
2 files changed, 225 insertions(+), 66 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_log_recover.h b/fs/xfs/libxfs/xfs_log_recover.h
index 1fef69ec918c..dbb4bb5c57ad 100644
--- a/fs/xfs/libxfs/xfs_log_recover.h
+++ b/fs/xfs/libxfs/xfs_log_recover.h
@@ -11,6 +11,7 @@
* define how recovery should work for that type of log item.
*/
struct xlog_recover_item;
+struct xlog_recover;
struct xfs_defer_op_type;
/* Sorting hat for log items as they're read in. */
@@ -24,6 +25,24 @@ enum xlog_recover_reorder {
struct xlog_recover_item_ops {
uint16_t item_type; /* XFS_LI_* type code. */
+ /*
+ * Minimum valid length of the item's format header (ri_buf[0]). Checked
+ * as the item is decoded from the journal before the region data is
+ * trusted. A value of 0 means no minimum header length is enforced.
+ */
+ uint16_t min_hdr_len;
+
+ /*
+ * Validate the region count read from the front of the item's first
+ * region and return the number of regions to assemble for the item.
+ * The returned count is used to size ri_buf and to detect item
+ * completion, so it need not equal the raw on-disk value (the
+ * transaction header, for example, ignores it and always returns 1).
+ * Returns a negative error (e.g. -EFSCORRUPTED) if the count is
+ * invalid. Types that do not supply this use xlog_recover_nregions().
+ */
+ int (*validate_nregions)(struct xlog *log, uint16_t nregions);
+
/*
* Help sort recovered log items into the order required to replay them
* correctly. Log item types that always use XLOG_REORDER_ITEM_LIST do
@@ -58,8 +77,42 @@ struct xlog_recover_item_ops {
*/
int (*commit_pass2)(struct xlog *log, struct list_head *buffer_list,
struct xlog_recover_item *item, xfs_lsn_t lsn);
+
+ /*
+ * Validate a single region of the item once it has been fully
+ * assembled from the journal. Called with the index of the region to
+ * check. Returns 0 if the region is valid or a negative error (e.g.
+ * -EFSCORRUPTED) if it is not. Optional.
+ */
+ int (*validate_region)(struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index);
+
+ /*
+ * Validate the whole item once all of its regions have been assembled
+ * from the journal. Used for cross-region structural checks that can
+ * only be done with the complete item. Returns 0 if the item is valid
+ * or a negative error (e.g. -EFSCORRUPTED) if it is not. Optional.
+ */
+ int (*validate_item)(struct xlog *log,
+ struct xlog_recover_item *item);
+
+ /*
+ * Dispose of the item once it has been fully assembled and validated,
+ * instead of the default of queuing it on the transaction for replay.
+ * Used by pseudo-items that are consumed during decode rather than
+ * replayed (e.g. the transaction header, which is copied into the
+ * transaction). Returns XLOG_RECOVER_ITEM_CONSUMED if it took
+ * ownership of the item (the caller then frees it), 0 to fall back to
+ * the default queuing, or a negative error. Optional.
+ */
+ int (*complete)(struct xlog *log, struct xlog_recover *trans,
+ struct xlog_recover_item *item);
};
+/* ->complete() took ownership of the item; do not queue it for replay. */
+#define XLOG_RECOVER_ITEM_CONSUMED 1
+
extern const struct xlog_recover_item_ops xlog_icreate_item_ops;
extern const struct xlog_recover_item_ops xlog_buf_item_ops;
extern const struct xlog_recover_item_ops xlog_inode_item_ops;
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index e5eca851d497..dee839b831b1 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -1829,12 +1829,12 @@ static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
static const struct xlog_recover_item_ops *
xlog_find_item_ops(
- struct xlog_recover_item *item)
+ uint16_t item_type)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(xlog_recover_item_ops); i++)
- if (ITEM_TYPE(item) == xlog_recover_item_ops[i]->item_type)
+ if (xlog_recover_item_ops[i]->item_type == item_type)
return xlog_recover_item_ops[i];
return NULL;
@@ -1916,15 +1916,6 @@ xlog_recover_reorder_trans(
break;
}
- item->ri_ops = xlog_find_item_ops(item);
- if (!item->ri_ops) {
- xfs_warn(log->l_mp,
- "%s: unrecognized type of log operation (%d)",
- __func__, ITEM_TYPE(item));
- error = -EFSCORRUPTED;
- break;
- }
-
if (item->ri_ops->reorder)
fate = item->ri_ops->reorder(item);
@@ -2097,37 +2088,171 @@ xlog_recover_commit_trans(
return error;
}
+/*
+ * Generic region count validation for item types that do not supply their own
+ * ->validate_nregions() method. Any non-zero count up to the maximum an item
+ * can hold is accepted, and the count is used as-is.
+ */
+static int
+xlog_recover_nregions(
+ struct xlog *log,
+ uint16_t nregions)
+{
+ if (nregions == 0 || nregions > XLOG_MAX_REGIONS_IN_ITEM) {
+ xfs_warn(log->l_mp,
+ "bad number of regions (%d) in inode log format", nregions);
+ return -EFSCORRUPTED;
+ }
+ return nregions;
+}
+
+/*
+ * Start decoding a new log item. The item type and region count come from the
+ * type/size fields at the front of the item's first region. Look up the item
+ * ops from the type, validate the region count, then allocate and initialise
+ * the item.
+ *
+ * Returns the new item, which becomes the item currently being rebuilt, or an
+ * ERR_PTR() if the type is unknown or the region count is invalid. The item is
+ * not placed on the transaction's item queue until it has been fully decoded
+ * (see xlog_recover_complete_item()).
+ */
STATIC struct xlog_recover_item *
xlog_recover_add_item(
- struct xlog_recover *trans)
+ struct xlog *log,
+ struct xlog_recover *trans,
+ uint16_t item_type,
+ uint16_t nregions)
{
+ const struct xlog_recover_item_ops *ops;
struct xlog_recover_item *item;
+ int valid_nregions;
- item = kzalloc_obj(struct xlog_recover_item, GFP_KERNEL | __GFP_NOFAIL);
- INIT_LIST_HEAD(&item->ri_list);
+ ops = xlog_find_item_ops(item_type);
+ if (!ops) {
+ xfs_warn(log->l_mp,
+ "%s: unrecognized type of log operation (0x%x)",
+ __func__, item_type);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
/*
- * The item is not placed on the transaction's item queue until it has
- * been fully decoded (see xlog_recover_complete_item()). Until then it
- * is tracked as the item currently being rebuilt.
+ * Validate the region count and determine how many regions to assemble
+ * for the item. The type may override the raw count (e.g. the
+ * transaction header always has a single region).
*/
+ if (ops->validate_nregions)
+ valid_nregions = ops->validate_nregions(log, nregions);
+ else
+ valid_nregions = xlog_recover_nregions(log, nregions);
+ if (valid_nregions < 0)
+ return ERR_PTR(valid_nregions);
+
+ item = kzalloc_obj(struct xlog_recover_item, GFP_KERNEL | __GFP_NOFAIL);
+ INIT_LIST_HEAD(&item->ri_list);
+ item->ri_ops = ops;
+ item->ri_total = valid_nregions;
+ item->ri_buf = kzalloc_objs(*item->ri_buf, item->ri_total,
+ GFP_KERNEL | __GFP_NOFAIL);
+
trans->r_cur_item = item;
return item;
}
/*
- * A log item has been fully decoded from the journal. Move it onto the
- * transaction's item queue and clear the current item so the next region
- * decoded starts a new item.
+ * Free up any resources allocated by the transaction
+ *
+ * Remember that EFIs, EFDs, and IUNLINKs are handled later.
*/
static void
+xlog_recover_free_item(
+ struct xlog_recover_item *item)
+{
+ int i;
+
+ /* Free the regions in the item. */
+ for (i = 0; i < item->ri_cnt; i++)
+ kvfree(item->ri_buf[i].iov_base);
+ /* Free the item itself */
+ kfree(item->ri_buf);
+ kfree(item);
+}
+
+/*
+ * A log item has been fully decoded from the journal. Validate the assembled
+ * item, then move it onto the transaction's item queue and clear the current
+ * item so the next region decoded starts a new item.
+ *
+ * A type may instead consume the item during decode via its ->complete()
+ * method, in which case it is freed here rather than queued for replay.
+ */
+static int
xlog_recover_complete_item(
+ struct xlog *log,
struct xlog_recover *trans)
{
struct xlog_recover_item *item = trans->r_cur_item;
+ int error;
+
+ if (item->ri_ops->validate_item) {
+ error = item->ri_ops->validate_item(log, item);
+ if (error)
+ return error;
+ }
+
+ if (item->ri_ops->complete) {
+ error = item->ri_ops->complete(log, trans, item);
+ if (error < 0)
+ return error;
+ if (error == XLOG_RECOVER_ITEM_CONSUMED) {
+ xlog_recover_free_item(item);
+ trans->r_cur_item = NULL;
+ return 0;
+ }
+ }
list_add_tail(&item->ri_list, &trans->r_itemq);
trans->r_cur_item = NULL;
+ return 0;
+}
+
+/*
+ * A region of the current item has been fully assembled from the journal.
+ * Validate it, and if it is the last region of the item complete the item.
+ * Called only when the region is complete (not a partial continuation).
+ */
+static int
+xlog_recover_complete_region(
+ struct xlog *log,
+ struct xlog_recover *trans)
+{
+ struct xlog_recover_item *item = trans->r_cur_item;
+ int error;
+
+ /*
+ * The format header (region 0) must be at least the minimum size for
+ * the item type. This is checked here, once the region is complete, so
+ * a partial first fragment is not rejected while still being assembled.
+ */
+ if (item->ri_cnt == 1 && item->ri_ops->min_hdr_len &&
+ item->ri_buf[0].iov_len < item->ri_ops->min_hdr_len) {
+ xfs_warn(log->l_mp,
+ "log item type 0x%x header too short (%zu < %u)",
+ item->ri_ops->item_type, item->ri_buf[0].iov_len,
+ item->ri_ops->min_hdr_len);
+ return -EFSCORRUPTED;
+ }
+
+ if (item->ri_ops->validate_region) {
+ error = item->ri_ops->validate_region(log, item,
+ item->ri_cnt - 1);
+ if (error)
+ return error;
+ }
+
+ if (item->ri_cnt == item->ri_total)
+ return xlog_recover_complete_item(log, trans);
+ return 0;
}
/*
@@ -2216,11 +2341,10 @@ xlog_recover_add_to_cont_trans(
/*
* The continued region is only complete once its final fragment has
- * been appended. When that finishes the item's last region, the item is
- * complete.
+ * been appended; until then it cannot be validated.
*/
- if (region_complete && item->ri_cnt == item->ri_total)
- xlog_recover_complete_item(trans);
+ if (region_complete)
+ return xlog_recover_complete_region(log, trans);
return 0;
}
@@ -2247,6 +2371,7 @@ xlog_recover_add_to_trans(
{
struct xlog_recover_item *item;
char *ptr;
+ int error;
if (!len)
return 0;
@@ -2256,37 +2381,32 @@ xlog_recover_add_to_trans(
/*
* If there is no item currently being rebuilt, this region is the first
- * region of a new item. Start the item and use its first region to
- * determine how many regions it has.
+ * region of a new item. The type and region count are at the front of
+ * the region; start the item from them. ptr is still standalone here,
+ * so on error it just needs to be freed (out_free). Per-region size
+ * validation (including min_hdr_len) happens once the region is
+ * complete, below, because this first region may only be a partial
+ * fragment here.
*/
item = trans->r_cur_item;
if (!item) {
struct xfs_inode_log_format *in_f = /* any will do */
(struct xfs_inode_log_format *)ptr;
- if (in_f->ilf_size == 0 ||
- in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
- xfs_warn(log->l_mp,
- "bad number of regions (%d) in inode log format",
- in_f->ilf_size);
- ASSERT(0);
- kvfree(ptr);
- return -EFSCORRUPTED;
+ item = xlog_recover_add_item(log, trans, in_f->ilf_type,
+ in_f->ilf_size);
+ if (IS_ERR(item)) {
+ error = PTR_ERR(item);
+ goto out_free;
}
-
- item = xlog_recover_add_item(trans);
- item->ri_total = in_f->ilf_size;
- item->ri_buf = kzalloc_objs(*item->ri_buf, item->ri_total,
- GFP_KERNEL | __GFP_NOFAIL);
}
- if (item->ri_total <= item->ri_cnt) {
+ if (item->ri_cnt >= item->ri_total) {
xfs_warn(log->l_mp,
"log item region count (%d) overflowed size (%d)",
item->ri_cnt, item->ri_total);
- ASSERT(0);
- kvfree(ptr);
- return -EFSCORRUPTED;
+ error = -EFSCORRUPTED;
+ goto out_free;
}
/* Description region is ri_buf[0] */
@@ -2296,32 +2416,18 @@ xlog_recover_add_to_trans(
trace_xfs_log_recover_item_add(log, trans, item, 0);
/*
- * The item is complete once its final region has been fully decoded. If
- * this region continues into the next op record it is not complete yet,
- * so the item cannot be either.
+ * A region that continues into the next op record is not yet complete
+ * and cannot be validated until the continuation finishes assembling
+ * it. ptr is now owned by the item, so the transaction teardown frees
+ * it on any error from here.
*/
- if (region_complete && item->ri_cnt == item->ri_total)
- xlog_recover_complete_item(trans);
+ if (region_complete)
+ return xlog_recover_complete_region(log, trans);
return 0;
-}
-
-/*
- * Free up any resources allocated by the transaction
- *
- * Remember that EFIs, EFDs, and IUNLINKs are handled later.
- */
-static void
-xlog_recover_free_item(
- struct xlog_recover_item *item)
-{
- int i;
- /* Free the regions in the item. */
- for (i = 0; i < item->ri_cnt; i++)
- kvfree(item->ri_buf[i].iov_base);
- /* Free the item itself */
- kfree(item->ri_buf);
- kfree(item);
+out_free:
+ kvfree(ptr);
+ return error;
}
STATIC void
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 18/59] xfs: recover the transaction header as a validated item type
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (16 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 17/59] xfs: add log item validation infrastructure to log recovery Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 19/59] xfs: factor out xlog_recover_validate_ophdr() Dave Chinner
` (40 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The transaction header that starts each recovered transaction was decoded by
a bespoke path: xlog_recovery_process_trans() routed the first op records of
a transaction to xlog_recover_init_new_trans() based on an r_hdr_decoded
flag, which reassembled the header into r_theader with its own inline magic
and length checks. This is a second, parallel copy of the region assembly and
validation logic that the generic item path already provides, and it is not
subject to the item validation infrastructure.
Decode the header through the generic item machinery instead. The header is
laid out so that the first two 16 bit words at the front of its single region
- which xlog_recover_add_item() reads as the item type and region count -
overlap the 32 bit th_magic field. Register an xlog_recover_item_ops keyed on
the magic so the header is assembled, validated and completed exactly like any
other item, and consumed via its ->complete() method (copied into the
transaction and freed rather than queued for replay).
The XFS log is written in host byte order, so which half of th_magic is read
as the type depends on the endianness of the host that wrote the log. Two ops
entries are registered, one for each order; on any given host only one of the
two type codes is matched. The type-count method validates that the other
half of the magic is present and returns a region count of 1, the region
validation checks the full magic and exact header length once assembled, and
min_hdr_len rejects a short header.
With the header handled as an item, xlog_recover_init_new_trans(), the
r_hdr_decoded flag and the special-case dispatch in
xlog_recovery_process_trans() are all removed; a header op record now falls
through to xlog_recover_add_to_trans()/add_to_cont_trans() like any other
region.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_log_recover.h | 1 -
fs/xfs/xfs_log_recover.c | 199 +++++++++++++++++++-------------
2 files changed, 121 insertions(+), 79 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_log_recover.h b/fs/xfs/libxfs/xfs_log_recover.h
index dbb4bb5c57ad..f4dbed616ee6 100644
--- a/fs/xfs/libxfs/xfs_log_recover.h
+++ b/fs/xfs/libxfs/xfs_log_recover.h
@@ -168,7 +168,6 @@ struct xlog_recover {
int r_state; /* not needed */
xfs_lsn_t r_lsn; /* xact lsn */
struct list_head r_itemq; /* q for items */
- bool r_hdr_decoded; /* trans header fully decoded */
struct xlog_recover_item *r_cur_item; /* item currently being rebuilt */
};
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index dee839b831b1..4f05b5130bdd 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -1801,7 +1801,128 @@ xlog_recover_iget_handle(
*
******************************************************************************
*/
+
+/*
+ * The transaction header that starts each recovered transaction is not a log
+ * item, but it is decoded from the journal as a region in exactly the same way.
+ * Give it an xlog_recover_item_ops so it is assembled and validated through the
+ * same generic machinery as everything else, then consumed into the
+ * transaction rather than queued for replay.
+ *
+ * ITEM_TYPE() and xlog_recover_add_item() read the 16 bit type and region count
+ * from the first two 16 bit words at the front of the first region. For the
+ * transaction header these words overlap the 32 bit th_magic field, so the two
+ * halves of the magic act as the type and the region count. The XFS log is
+ * written in host byte order, so which half is read as the type depends on the
+ * endianness of the host that wrote the log: on a little endian host the type
+ * is 0x414e and the count is 0x5452, and on a big endian host they are the
+ * other way around. Register an ops entry for each order so a log is recovered
+ * correctly regardless of the byte order it was written in; on any given host
+ * only one of the two type codes is ever matched. Neither 0x414e nor 0x5452
+ * collides with an XFS_LI_* type code.
+ *
+ * The header always has a single region regardless of the count read from the
+ * journal, so validate_nregions() checks that the other half of the magic is
+ * present and returns 1.
+ */
+#define XLOG_TRANS_HDR_MAGIC_LO ((uint16_t)XFS_TRANS_HEADER_MAGIC)
+#define XLOG_TRANS_HDR_MAGIC_HI ((uint16_t)(XFS_TRANS_HEADER_MAGIC >> 16))
+
+static int
+xlog_recover_trans_hdr_check_half(
+ struct xlog *log,
+ uint16_t nregions,
+ uint16_t expected)
+{
+ if (nregions != expected) {
+ xfs_warn(log->l_mp,
+ "bad transaction header magic number 0x%x, expected 0x%x",
+ nregions, expected);
+ return -EFSCORRUPTED;
+ }
+ return 1;
+}
+
+/* Little endian host: type is the low half, count is the high half. */
+static int
+xlog_recover_trans_hdr_nregions_le(
+ struct xlog *log,
+ uint16_t nregions)
+{
+ return xlog_recover_trans_hdr_check_half(log, nregions,
+ XLOG_TRANS_HDR_MAGIC_HI);
+}
+
+/* Big endian host: type is the high half, count is the low half. */
+static int
+xlog_recover_trans_hdr_nregions_be(
+ struct xlog *log,
+ uint16_t nregions)
+{
+ return xlog_recover_trans_hdr_check_half(log, nregions,
+ XLOG_TRANS_HDR_MAGIC_LO);
+}
+
+static int
+xlog_recover_trans_hdr_validate(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_trans_header *thdr = item->ri_buf[region_index].iov_base;
+ size_t len = item->ri_buf[region_index].iov_len;
+
+ if (len != sizeof(struct xfs_trans_header)) {
+ xfs_warn(log->l_mp,
+ "bad transaction header length %zu, expected %zu",
+ len, sizeof(struct xfs_trans_header));
+ return -EFSCORRUPTED;
+ }
+
+ if (thdr->th_magic != XFS_TRANS_HEADER_MAGIC) {
+ xfs_warn(log->l_mp,
+ "bad transaction header magic number 0x%x, expected 0x%x",
+ thdr->th_magic, XFS_TRANS_HEADER_MAGIC);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
+static int
+xlog_recover_trans_hdr_complete(
+ struct xlog *log,
+ struct xlog_recover *trans,
+ struct xlog_recover_item *item)
+{
+ /*
+ * The header has been validated. Copy it into the transaction and
+ * consume the item - it is not a log item and is not replayed.
+ */
+ memcpy(&trans->r_theader, item->ri_buf[0].iov_base,
+ sizeof(struct xfs_trans_header));
+ return XLOG_RECOVER_ITEM_CONSUMED;
+}
+
+static const struct xlog_recover_item_ops xlog_trans_hdr_le_item_ops = {
+ .item_type = XLOG_TRANS_HDR_MAGIC_LO,
+ .min_hdr_len = sizeof(struct xfs_trans_header),
+ .validate_nregions = xlog_recover_trans_hdr_nregions_le,
+ .validate_region = xlog_recover_trans_hdr_validate,
+ .complete = xlog_recover_trans_hdr_complete,
+};
+
+static const struct xlog_recover_item_ops xlog_trans_hdr_be_item_ops = {
+ .item_type = XLOG_TRANS_HDR_MAGIC_HI,
+ .min_hdr_len = sizeof(struct xfs_trans_header),
+ .validate_nregions = xlog_recover_trans_hdr_nregions_be,
+ .validate_region = xlog_recover_trans_hdr_validate,
+ .complete = xlog_recover_trans_hdr_complete,
+};
+
static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
+ &xlog_trans_hdr_le_item_ops,
+ &xlog_trans_hdr_be_item_ops,
&xlog_buf_item_ops,
&xlog_inode_item_ops,
&xlog_dquot_item_ops,
@@ -2255,67 +2376,6 @@ xlog_recover_complete_region(
return 0;
}
-/*
- * Decode the transaction header at the start of a new transaction.
- *
- * The transaction header is not a log item. It can be arbitrarily split across
- * op records, so it may arrive whole in a single op record, or as an initial
- * fragment (cont == false) followed by one or more continuation fragments
- * (cont == true). Reassemble it directly into r_theader where the rest of
- * recovery expects to find it.
- */
-STATIC int
-xlog_recover_init_new_trans(
- struct xlog *log,
- struct xlog_recover *trans,
- char *dp,
- int len,
- bool cont,
- bool region_complete)
-{
- char *ptr;
-
- /*
- * All regions are 32 bit aligned, so the smallest valid fragment is 4
- * bytes; that is also the minimum needed to read the magic number. A
- * fragment larger than the whole header is corruption in either case.
- */
- if (len < 4 || len > sizeof(struct xfs_trans_header)) {
- xfs_warn(log->l_mp, "%s: bad header length %d", __func__, len);
- return -EFSCORRUPTED;
- }
-
- if (!cont) {
- /*
- * Initial fragment. The magic number is at the start of the
- * header, which lands at the start of r_theader.
- */
- if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
- xfs_warn(log->l_mp,
- "%s: bad header magic number 0x%x, expected 0x%x",
- __func__, *(uint *)dp, XFS_TRANS_HEADER_MAGIC);
- return -EFSCORRUPTED;
- }
- ptr = (char *)&trans->r_theader;
- } else {
- /* continuation fragment completes the tail of the header */
- ptr = (char *)&trans->r_theader +
- sizeof(struct xfs_trans_header) - len;
- }
-
- memcpy(ptr, dp, len);
-
- /*
- * The transaction header is a single region, so it is fully decoded
- * once the region is complete. The header is not a log item, so nothing
- * is added to the item queue here; the first log item region decoded
- * after this will start the first item.
- */
- if (region_complete)
- trans->r_hdr_decoded = true;
- return 0;
-}
-
STATIC int
xlog_recover_add_to_cont_trans(
struct xlog *log,
@@ -2486,22 +2546,6 @@ xlog_recovery_process_trans(
if (flags & XLOG_WAS_CONT_TRANS)
flags &= ~XLOG_CONTINUE_TRANS;
- /*
- * Until the transaction header has been fully decoded we are still
- * assembling it at the start of the transaction rather than decoding a
- * log item. The header can arrive whole (flags == 0), as an initial
- * fragment (XLOG_CONTINUE_TRANS) or as a continuation fragment
- * (XLOG_WAS_CONT_TRANS); XLOG_WAS_CONT_TRANS distinguishes the
- * continuation.
- */
- if (!trans->r_hdr_decoded &&
- (!flags || (flags & (XLOG_CONTINUE_TRANS | XLOG_WAS_CONT_TRANS)))) {
- error = xlog_recover_init_new_trans(log, trans, dp, len,
- flags & XLOG_WAS_CONT_TRANS,
- region_complete);
- goto out_error;
- }
-
/*
* Callees must not free the trans structure. We'll decide if we need to
* free it or not based on the operation being done and it's result.
@@ -2551,7 +2595,6 @@ xlog_recovery_process_trans(
break;
}
-out_error:
if (error || freeit)
xlog_recover_free_trans(trans);
return error;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 19/59] xfs: factor out xlog_recover_validate_ophdr()
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (17 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 18/59] xfs: recover the transaction header as a validated item type Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 20/59] xfs: validate operation header length and alignment during recovery Dave Chinner
` (39 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Factor the existing operation header checks out of
xlog_recover_process_ophdr() into a dedicated helper to centralise ophdr
validation. Later patches add validation of the remaining ophdr fields here.
The ASSERT(0) and WARN_ON(1) are dropped as they add nothing over the logged
message and returned error, and they fire on expected corrupt input.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 55 ++++++++++++++++++++++++++--------------
1 file changed, 36 insertions(+), 19 deletions(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 4f05b5130bdd..a5fa7d2676cd 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2651,39 +2651,56 @@ xlog_recover_ophdr_to_trans(
return NULL;
}
+/*
+ * Validate an operation header decoded from the journal before it is used to
+ * drive transaction recovery. The ophdr and its data region come straight from
+ * the on-disk log and cannot be trusted, so check the fields for known values
+ * and obvious corruption here, in one place, before anything acts on them.
+ */
STATIC int
-xlog_recover_process_ophdr(
+xlog_recover_validate_ophdr(
struct xlog *log,
- struct hlist_head rhash[],
- struct xlog_rec_header *rhead,
struct xlog_op_header *ohead,
char *dp,
- char *end,
- int pass,
- struct list_head *buffer_list)
+ char *end)
{
- struct xlog_recover *trans;
- unsigned int len;
- int error;
+ unsigned int len = be32_to_cpu(ohead->oh_len);
+
+ /* Check the ophdr contains all the data it is supposed to contain. */
+ if (dp + len > end) {
+ xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
+ return -EFSCORRUPTED;
+ }
/* Do we understand who wrote this op? */
if (ohead->oh_clientid != XFS_TRANSACTION &&
ohead->oh_clientid != XFS_LOG) {
xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
__func__, ohead->oh_clientid);
- ASSERT(0);
return -EFSCORRUPTED;
}
- /*
- * Check the ophdr contains all the data it is supposed to contain.
- */
- len = be32_to_cpu(ohead->oh_len);
- if (dp + len > end) {
- xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
- WARN_ON(1);
- return -EFSCORRUPTED;
- }
+ return 0;
+}
+
+STATIC int
+xlog_recover_process_ophdr(
+ struct xlog *log,
+ struct hlist_head rhash[],
+ struct xlog_rec_header *rhead,
+ struct xlog_op_header *ohead,
+ char *dp,
+ char *end,
+ int pass,
+ struct list_head *buffer_list)
+{
+ struct xlog_recover *trans;
+ unsigned int len = be32_to_cpu(ohead->oh_len);
+ int error;
+
+ error = xlog_recover_validate_ophdr(log, ohead, dp, end);
+ if (error)
+ return error;
trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
if (!trans) {
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 20/59] xfs: validate operation header length and alignment during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (18 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 19/59] xfs: factor out xlog_recover_validate_ophdr() Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 21/59] xfs: validate the operation header padding " Dave Chinner
` (38 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The operation header length read from the journal was only checked against
the end of the log record, so an implausibly large or misaligned length was
still trusted to drive region assembly and memory allocation.
Bound the length against the largest object that can be logged in a single
region and require it to be 32 bit aligned. Move the in-place ophdr decode
into the validation helper as well, so a misaligned or out-of-bounds header
is rejected before it is dereferenced rather than after.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 115 +++++++++++++++++++++++++++------------
1 file changed, 81 insertions(+), 34 deletions(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index a5fa7d2676cd..472e0093d589 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2652,24 +2652,69 @@ xlog_recover_ophdr_to_trans(
}
/*
- * Validate an operation header decoded from the journal before it is used to
- * drive transaction recovery. The ophdr and its data region come straight from
- * the on-disk log and cannot be trusted, so check the fields for known values
- * and obvious corruption here, in one place, before anything acts on them.
+ * Decode and validate an operation header from the journal. dp points at the
+ * ophdr within the log record and end is the end of the record data. The ophdr
+ * and its data region come straight from the on-disk log and cannot be trusted,
+ * so decode the header in place and check its fields for known values and
+ * obvious corruption here, in one place, before anything acts on them.
+ *
+ * Returns the validated ophdr, or an ERR_PTR() on corruption.
*/
-STATIC int
+STATIC struct xlog_op_header *
xlog_recover_validate_ophdr(
struct xlog *log,
- struct xlog_op_header *ohead,
char *dp,
char *end)
{
- unsigned int len = be32_to_cpu(ohead->oh_len);
+ struct xlog_op_header *ohead;
+ unsigned int len;
+
+ /*
+ * All log regions are 32 bit aligned, so the ophdr must be too. A
+ * misaligned ophdr means the walk over the record has lost sync with
+ * the on-disk layout.
+ */
+ if (dp != PTR_ALIGN(dp, sizeof(uint32_t))) {
+ xfs_warn(log->l_mp, "%s: unaligned op header", __func__);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+
+ /* The ophdr itself must fit within the record. */
+ if (dp + sizeof(*ohead) > end) {
+ xfs_warn(log->l_mp, "%s: op header overrun", __func__);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+
+ ohead = (struct xlog_op_header *)dp;
+ dp += sizeof(*ohead);
+ len = be32_to_cpu(ohead->oh_len);
/* Check the ophdr contains all the data it is supposed to contain. */
if (dp + len > end) {
xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
- return -EFSCORRUPTED;
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+
+ /*
+ * All log regions are 32 bit aligned, so the length must be too. A
+ * misaligned length desyncs the walk over the ophdrs in the record.
+ */
+ if (!IS_ALIGNED(len, sizeof(uint32_t))) {
+ xfs_warn(log->l_mp, "%s: unaligned length 0x%x", __func__, len);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+
+ /*
+ * A single region cannot be larger than the largest object we log in
+ * one region. That is a whole buffer in a buffer log item (up to
+ * XFS_MAX_BLOCKSIZE, e.g. a 64k directory block on a 4k block size
+ * filesystem) or a remote attribute value (up to XFS_XATTR_SIZE_MAX,
+ * logged in its own region); both are 64k.
+ */
+ BUILD_BUG_ON(XFS_XATTR_SIZE_MAX > XFS_MAX_BLOCKSIZE);
+ if (len > XFS_MAX_BLOCKSIZE) {
+ xfs_warn(log->l_mp, "%s: length 0x%x too large", __func__, len);
+ return ERR_PTR(-EFSCORRUPTED);
}
/* Do we understand who wrote this op? */
@@ -2677,10 +2722,10 @@ xlog_recover_validate_ophdr(
ohead->oh_clientid != XFS_LOG) {
xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
__func__, ohead->oh_clientid);
- return -EFSCORRUPTED;
+ return ERR_PTR(-EFSCORRUPTED);
}
- return 0;
+ return ohead;
}
STATIC int
@@ -2688,24 +2733,28 @@ xlog_recover_process_ophdr(
struct xlog *log,
struct hlist_head rhash[],
struct xlog_rec_header *rhead,
- struct xlog_op_header *ohead,
char *dp,
char *end,
int pass,
struct list_head *buffer_list)
{
+ struct xlog_op_header *ohead;
struct xlog_recover *trans;
- unsigned int len = be32_to_cpu(ohead->oh_len);
+ unsigned int len;
int error;
- error = xlog_recover_validate_ophdr(log, ohead, dp, end);
- if (error)
- return error;
+ ohead = xlog_recover_validate_ophdr(log, dp, end);
+ if (IS_ERR(ohead))
+ return PTR_ERR(ohead);
+
+ /* the data region follows the ophdr */
+ dp += sizeof(*ohead);
+ len = be32_to_cpu(ohead->oh_len);
trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
if (!trans) {
/* nothing to do, so skip over this ophdr */
- return 0;
+ return sizeof(*ohead) + len;
}
/*
@@ -2739,8 +2788,11 @@ xlog_recover_process_ophdr(
log->l_recovery_lsn = trans->r_lsn;
}
- return xlog_recovery_process_trans(log, trans, dp, len,
- ohead->oh_flags, pass, buffer_list);
+ error = xlog_recovery_process_trans(log, trans, dp, len,
+ ohead->oh_flags, pass, buffer_list);
+ if (error)
+ return error;
+ return sizeof(*ohead) + len;
}
/*
@@ -2761,10 +2813,9 @@ xlog_recover_process_data(
int pass,
struct list_head *buffer_list)
{
- struct xlog_op_header *ohead;
char *end;
int num_logops;
- int error;
+ int consumed;
end = dp + be32_to_cpu(rhead->h_len);
num_logops = be32_to_cpu(rhead->h_num_logops);
@@ -2775,21 +2826,17 @@ xlog_recover_process_data(
trace_xfs_log_recover_record(log, rhead, pass);
while ((dp < end) && num_logops) {
+ /*
+ * Decode, validate and process the next ophdr and its data
+ * region. On success the number of bytes consumed from the
+ * record is returned; errors abort recovery.
+ */
+ consumed = xlog_recover_process_ophdr(log, rhash, rhead, dp,
+ end, pass, buffer_list);
+ if (consumed < 0)
+ return consumed;
- ohead = (struct xlog_op_header *)dp;
- dp += sizeof(*ohead);
- if (dp > end) {
- xfs_warn(log->l_mp, "%s: op header overrun", __func__);
- return -EFSCORRUPTED;
- }
-
- /* errors will abort recovery */
- error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
- dp, end, pass, buffer_list);
- if (error)
- return error;
-
- dp += be32_to_cpu(ohead->oh_len);
+ dp += consumed;
num_logops--;
}
return 0;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 21/59] xfs: validate the operation header padding during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (19 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 20/59] xfs: validate operation header length and alignment during recovery Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 22/59] xfs: validate the operation header flags " Dave Chinner
` (37 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The operation header padding field is always written as zero, so a non-zero
value in a recovered ophdr is a reliable indication of corruption that
recovery currently ignores.
Reject a non-zero padding field during ophdr validation.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 472e0093d589..2ff06d80a05b 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2725,6 +2725,13 @@ xlog_recover_validate_ophdr(
return ERR_PTR(-EFSCORRUPTED);
}
+ /* The padding is always written as zero. */
+ if (ohead->oh_res2 != 0) {
+ xfs_warn(log->l_mp, "%s: bad padding 0x%x",
+ __func__, ohead->oh_res2);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+
return ohead;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 22/59] xfs: validate the operation header flags during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (20 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 21/59] xfs: validate the operation header padding " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 23/59] xfs: enforce zero length control records " Dave Chinner
` (36 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Recovery acted on the operation header flags without checking them for unknown
bits or impossible combinations, so corruption in the flags could misdirect
transaction assembly rather than being detected.
Reject any oh_flags value the log writer does not produce, accepting only the
known individual flags and the valid continuation combinations.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 2ff06d80a05b..3a052a0337c1 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2732,6 +2732,27 @@ xlog_recover_validate_ophdr(
return ERR_PTR(-EFSCORRUPTED);
}
+ /*
+ * Only the flag combinations the log writer actually produces are
+ * valid. Reject unknown flags and impossible combinations. END is only
+ * ever set together with WAS_CONT, and START, COMMIT and UNMOUNT each
+ * stand alone.
+ */
+ switch (ohead->oh_flags) {
+ case 0:
+ case XLOG_START_TRANS:
+ case XLOG_COMMIT_TRANS:
+ case XLOG_CONTINUE_TRANS:
+ case XLOG_WAS_CONT_TRANS | XLOG_CONTINUE_TRANS:
+ case XLOG_WAS_CONT_TRANS | XLOG_END_TRANS:
+ case XLOG_UNMOUNT_TRANS:
+ break;
+ default:
+ xfs_warn(log->l_mp, "%s: bad flags 0x%x",
+ __func__, ohead->oh_flags);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+
return ohead;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 23/59] xfs: enforce zero length control records during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (21 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 22/59] xfs: validate the operation header flags " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 24/59] xfs: validate the unmount record " Dave Chinner
` (35 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Start and commit records only bracket a transaction and carry no data of
their own, but recovery only asserted this for start records and did not check
it for commit records, so a corrupt non-zero length on either was accepted on
production builds.
Enforce the zero length for both in the operation header flag validation and
drop the now redundant assert.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 3a052a0337c1..bb3e23c20069 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2631,8 +2631,6 @@ xlog_recover_ophdr_to_trans(
if (!(ohead->oh_flags & XLOG_START_TRANS))
return NULL;
- ASSERT(be32_to_cpu(ohead->oh_len) == 0);
-
/*
* This is a new transaction so allocate a new recovery container to
* hold the recovery ops that will follow.
@@ -2739,9 +2737,19 @@ xlog_recover_validate_ophdr(
* stand alone.
*/
switch (ohead->oh_flags) {
- case 0:
case XLOG_START_TRANS:
case XLOG_COMMIT_TRANS:
+ /*
+ * Start and commit records only bracket a transaction and carry
+ * no data of their own.
+ */
+ if (len != 0) {
+ xfs_warn(log->l_mp, "%s: bad control record length 0x%x",
+ __func__, len);
+ return ERR_PTR(-EFSCORRUPTED);
+ }
+ break;
+ case 0:
case XLOG_CONTINUE_TRANS:
case XLOG_WAS_CONT_TRANS | XLOG_CONTINUE_TRANS:
case XLOG_WAS_CONT_TRANS | XLOG_END_TRANS:
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 24/59] xfs: validate the unmount record during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (22 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 23/59] xfs: enforce zero length control records " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 25/59] xfs: handle zero length continuation op headers " Dave Chinner
` (34 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The unmount record is the only record the log writes itself rather than
through a transaction. The log item processing loop skips it, so its contents
were never validated on recovery. A clean unmount is detected by inspecting it
during the head and tail scan, which trusted the operation header flags
without validating the header first.
Add xlog_recover_validate_unmount_record() to check the record is written by
the log, is the expected length, and carries the expected magic and zeroed
padding. Call it from the operation header validation.
The head and tail scan now validates the operation header before trusting its
flags. That scan runs before torn writes have been trimmed from the head of the
log, so an invalid record there is not treated as corruption. The log is only
considered clean if the record is a valid unmount record. Otherwise the log is
treated as dirty, and head verification and recovery deal with the record. The
scan validates quietly, as a record that is not an unmount record is expected
there.
A clean log can be mounted on a host of either endianness. The unmount record
magic is written in host byte order, so both byte orders are accepted.
The unmount record length was incorrectly written as zero for a period of time.
A zero length record has no payload to check, so accept it with a warning so
that filesystems written by those kernels still recover.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 144 +++++++++++++++++++++++++++++++++------
1 file changed, 123 insertions(+), 21 deletions(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index bb3e23c20069..0c7788345cd3 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -42,6 +42,8 @@ xlog_clear_stale_blocks(
STATIC int
xlog_do_recovery_pass(
struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
+STATIC struct xlog_op_header *
+xlog_recover_validate_ophdr(struct xlog *log, char *dp, char *end, bool quiet);
/*
* Sector aligned buffer routines for buffer create/read/write/access
@@ -1169,8 +1171,18 @@ xlog_check_unmount_rec(
if (error)
return error;
- op_head = (struct xlog_op_header *)offset;
- if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
+ /*
+ * The log has not been checked for torn writes yet, so this
+ * record cannot be trusted until its ophdr has been validated.
+ * Only a valid unmount record makes the log clean. Anything else
+ * is not an error here: the log is treated as dirty, and head
+ * verification and recovery deal with the record.
+ */
+ op_head = xlog_recover_validate_ophdr(log, offset,
+ offset + BBSIZE, true);
+ if (IS_ERR(op_head))
+ return 0;
+ if (op_head->oh_flags == XLOG_UNMOUNT_TRANS) {
/*
* Set tail and last sync so that newly written log
* records will point recovery to after the current
@@ -2583,9 +2595,11 @@ xlog_recovery_process_trans(
/* unexpected flag values */
case XLOG_UNMOUNT_TRANS:
- /* just skip trans */
- xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
- freeit = true;
+ /* An unmount record never belongs to a checkpoint. */
+ xfs_warn(log->l_mp,
+ "%s: unmount record in transaction 0x%x",
+ __func__, trans->r_log_tid);
+ error = -EFSCORRUPTED;
break;
case XLOG_START_TRANS:
default:
@@ -2649,6 +2663,72 @@ xlog_recover_ophdr_to_trans(
return NULL;
}
+/*
+ * Validate an unmount record. The unmount record is the only record written by
+ * the log itself rather than by a transaction, and the log item processing loop
+ * skips it, so it can only be validated here. It is a single fixed size region,
+ * written by the log with the XFS_LOG clientid, containing an unmount format
+ * structure with a known magic number and zeroed padding.
+ *
+ * The ophdr has already been validated, so oh_len is known to be within the
+ * record. The unmount format length was incorrectly written for a period of
+ * time as zero rather than the region length. Such a record has no payload to
+ * read, so accept it with a warning and stop, so that filesystems written by
+ * those kernels still recover. That warning is always emitted, as the record
+ * is accepted. @quiet suppresses only the warnings for a rejected record.
+ */
+STATIC int
+xlog_recover_validate_unmount_record(
+ struct xlog *log,
+ struct xlog_op_header *ohead,
+ bool quiet)
+{
+ struct xfs_unmount_log_format *ulf;
+ unsigned int len = be32_to_cpu(ohead->oh_len);
+
+ if (ohead->oh_clientid != XFS_LOG) {
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: bad unmount clientid 0x%x",
+ __func__, ohead->oh_clientid);
+ return -EFSCORRUPTED;
+ }
+
+ if (len == 0) {
+ xfs_warn(log->l_mp,
+ "%s: zero length unmount record recovered.", __func__);
+ return 0;
+ }
+
+ if (len != sizeof(*ulf)) {
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: bad unmount record length 0x%x",
+ __func__, len);
+ return -EFSCORRUPTED;
+ }
+
+ /*
+ * The magic number is written in host byte order, and a clean log can be
+ * mounted on a host of either endianness, so accept both byte orders.
+ */
+ ulf = (struct xfs_unmount_log_format *)(ohead + 1);
+ if (ulf->magic != XLOG_UNMOUNT_TYPE &&
+ ulf->magic != swab16(XLOG_UNMOUNT_TYPE)) {
+ if (!quiet)
+ xfs_warn(log->l_mp,
+ "%s: bad unmount magic 0x%x, expected 0x%x",
+ __func__, ulf->magic, XLOG_UNMOUNT_TYPE);
+ return -EFSCORRUPTED;
+ }
+
+ if (ulf->pad1 != 0 || ulf->pad2 != 0) {
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: bad unmount padding", __func__);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
/*
* Decode and validate an operation header from the journal. dp points at the
* ophdr within the log record and end is the end of the record data. The ophdr
@@ -2656,16 +2736,21 @@ xlog_recover_ophdr_to_trans(
* so decode the header in place and check its fields for known values and
* obvious corruption here, in one place, before anything acts on them.
*
+ * A caller probing for a record that may legitimately not be there sets @quiet
+ * to suppress the warnings for an ophdr that fails validation.
+ *
* Returns the validated ophdr, or an ERR_PTR() on corruption.
*/
STATIC struct xlog_op_header *
xlog_recover_validate_ophdr(
struct xlog *log,
char *dp,
- char *end)
+ char *end,
+ bool quiet)
{
struct xlog_op_header *ohead;
unsigned int len;
+ int error;
/*
* All log regions are 32 bit aligned, so the ophdr must be too. A
@@ -2673,13 +2758,15 @@ xlog_recover_validate_ophdr(
* the on-disk layout.
*/
if (dp != PTR_ALIGN(dp, sizeof(uint32_t))) {
- xfs_warn(log->l_mp, "%s: unaligned op header", __func__);
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: unaligned op header", __func__);
return ERR_PTR(-EFSCORRUPTED);
}
/* The ophdr itself must fit within the record. */
if (dp + sizeof(*ohead) > end) {
- xfs_warn(log->l_mp, "%s: op header overrun", __func__);
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: op header overrun", __func__);
return ERR_PTR(-EFSCORRUPTED);
}
@@ -2689,7 +2776,9 @@ xlog_recover_validate_ophdr(
/* Check the ophdr contains all the data it is supposed to contain. */
if (dp + len > end) {
- xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__,
+ len);
return ERR_PTR(-EFSCORRUPTED);
}
@@ -2698,7 +2787,9 @@ xlog_recover_validate_ophdr(
* misaligned length desyncs the walk over the ophdrs in the record.
*/
if (!IS_ALIGNED(len, sizeof(uint32_t))) {
- xfs_warn(log->l_mp, "%s: unaligned length 0x%x", __func__, len);
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: unaligned length 0x%x",
+ __func__, len);
return ERR_PTR(-EFSCORRUPTED);
}
@@ -2711,22 +2802,26 @@ xlog_recover_validate_ophdr(
*/
BUILD_BUG_ON(XFS_XATTR_SIZE_MAX > XFS_MAX_BLOCKSIZE);
if (len > XFS_MAX_BLOCKSIZE) {
- xfs_warn(log->l_mp, "%s: length 0x%x too large", __func__, len);
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: length 0x%x too large",
+ __func__, len);
return ERR_PTR(-EFSCORRUPTED);
}
/* Do we understand who wrote this op? */
if (ohead->oh_clientid != XFS_TRANSACTION &&
ohead->oh_clientid != XFS_LOG) {
- xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
- __func__, ohead->oh_clientid);
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
+ __func__, ohead->oh_clientid);
return ERR_PTR(-EFSCORRUPTED);
}
/* The padding is always written as zero. */
if (ohead->oh_res2 != 0) {
- xfs_warn(log->l_mp, "%s: bad padding 0x%x",
- __func__, ohead->oh_res2);
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: bad padding 0x%x",
+ __func__, ohead->oh_res2);
return ERR_PTR(-EFSCORRUPTED);
}
@@ -2744,20 +2839,27 @@ xlog_recover_validate_ophdr(
* no data of their own.
*/
if (len != 0) {
- xfs_warn(log->l_mp, "%s: bad control record length 0x%x",
- __func__, len);
+ if (!quiet)
+ xfs_warn(log->l_mp,
+ "%s: bad control record length 0x%x",
+ __func__, len);
return ERR_PTR(-EFSCORRUPTED);
}
break;
+ case XLOG_UNMOUNT_TRANS:
+ error = xlog_recover_validate_unmount_record(log, ohead, quiet);
+ if (error)
+ return ERR_PTR(error);
+ break;
case 0:
case XLOG_CONTINUE_TRANS:
case XLOG_WAS_CONT_TRANS | XLOG_CONTINUE_TRANS:
case XLOG_WAS_CONT_TRANS | XLOG_END_TRANS:
- case XLOG_UNMOUNT_TRANS:
break;
default:
- xfs_warn(log->l_mp, "%s: bad flags 0x%x",
- __func__, ohead->oh_flags);
+ if (!quiet)
+ xfs_warn(log->l_mp, "%s: bad flags 0x%x",
+ __func__, ohead->oh_flags);
return ERR_PTR(-EFSCORRUPTED);
}
@@ -2779,7 +2881,7 @@ xlog_recover_process_ophdr(
unsigned int len;
int error;
- ohead = xlog_recover_validate_ophdr(log, dp, end);
+ ohead = xlog_recover_validate_ophdr(log, dp, end, false);
if (IS_ERR(ohead))
return PTR_ERR(ohead);
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 25/59] xfs: handle zero length continuation op headers during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (23 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 24/59] xfs: validate the unmount record " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 26/59] xfs: bound the assembled region size " Dave Chinner
` (33 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The log writer can start the transaction header at the start of a checkpoint
with a zero length op header, with the header data following in the
continuation. This is the only region the log writer ever opens with a zero
length op header. Recovery used to handle this as a special case in the
bespoke transaction header decode. Routing the transaction header through the
generic item assembly dropped that handling.
A zero length op header that opens the transaction header carries nothing to
decode, so the item cannot be initialised until the continuation arrives.
Leave a sentinel in r_cur_item for this case so the continuation is decoded as
the first region of the transaction header.
A zero length op header anywhere else in a checkpoint is corrupt, as is a zero
length op header that completes a region. Reject them. A continuation that
reaches the append path with no item under assembly is also treated as a
corrupt log.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_log_recover.h | 9 +++++
fs/xfs/xfs_log_recover.c | 59 ++++++++++++++++++++++++++++-----
2 files changed, 60 insertions(+), 8 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_log_recover.h b/fs/xfs/libxfs/xfs_log_recover.h
index f4dbed616ee6..d2e128fca058 100644
--- a/fs/xfs/libxfs/xfs_log_recover.h
+++ b/fs/xfs/libxfs/xfs_log_recover.h
@@ -171,6 +171,15 @@ struct xlog_recover {
struct xlog_recover_item *r_cur_item; /* item currently being rebuilt */
};
+/*
+ * The current recovery item can sometimes start with a zero length continuation
+ * op header. In this case there is nothing to decode, so the recovery item
+ * cannot be initialised yet. Leave this sentinel in r_cur_item instead so the
+ * continuation initialises the recovery item from the start of the continued
+ * region data.
+ */
+#define XLOG_RECOVER_CONT_ITEM ((struct xlog_recover_item *)1UL)
+
#define ITEM_TYPE(i) (*(unsigned short *)(i)->ri_buf[0].iov_base)
#define XLOG_RECOVER_CRCPASS 0
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 0c7788345cd3..aea6f5404cec 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2400,6 +2400,18 @@ xlog_recover_add_to_cont_trans(
char *ptr, *old_ptr;
int old_len;
+ /*
+ * A continuation must append to a region under assembly. The only valid
+ * way to reach here without one is a region opened by a zero length op
+ * header, which the dispatch in xlog_recovery_process_trans() routes to
+ * xlog_recover_add_to_trans() instead. So no current item here means the
+ * log is corrupt.
+ */
+ if (!item) {
+ xfs_warn(log->l_mp, "%s: continuation with no item", __func__);
+ return -EFSCORRUPTED;
+ }
+
old_ptr = item->ri_buf[item->ri_cnt-1].iov_base;
old_len = item->ri_buf[item->ri_cnt-1].iov_len;
@@ -2445,8 +2457,28 @@ xlog_recover_add_to_trans(
char *ptr;
int error;
- if (!len)
+ /*
+ * Only the transaction header at the start of a checkpoint can be
+ * opened by a zero length fragment, with its data following in the
+ * continuation. A zero length fragment anywhere else, or one that
+ * completes a region, is corrupt.
+ *
+ * There is nothing to decode yet, so leave a sentinel in r_cur_item so
+ * that xlog_recover_process_trans() will call here again with the
+ * continuation that carries the data we need to initialise the
+ * recovery item.
+ */
+ if (!len) {
+ if (region_complete || trans->r_cur_item ||
+ trans->r_theader.th_magic == XFS_TRANS_HEADER_MAGIC) {
+ xfs_warn(log->l_mp,
+ "%s: zero length region in transaction 0x%x",
+ __func__, trans->r_log_tid);
+ return -EFSCORRUPTED;
+ }
+ trans->r_cur_item = XLOG_RECOVER_CONT_ITEM;
return 0;
+ }
ptr = xlog_kvmalloc(len);
memcpy(ptr, dp, len);
@@ -2461,7 +2493,7 @@ xlog_recover_add_to_trans(
* fragment here.
*/
item = trans->r_cur_item;
- if (!item) {
+ if (!item || item == XLOG_RECOVER_CONT_ITEM) {
struct xfs_inode_log_format *in_f = /* any will do */
(struct xfs_inode_log_format *)ptr;
@@ -2517,9 +2549,10 @@ xlog_recover_free_trans(
/*
* An item still being rebuilt has not been added to the item queue yet,
- * so free it separately.
+ * so free it separately. The sentinel is not a real item and has nothing
+ * to free.
*/
- if (trans->r_cur_item)
+ if (trans->r_cur_item && trans->r_cur_item != XLOG_RECOVER_CONT_ITEM)
xlog_recover_free_item(trans->r_cur_item);
/* Free the transaction recover structure */
@@ -2564,15 +2597,25 @@ xlog_recovery_process_trans(
*/
switch (flags) {
/* expected flag values */
+ case XLOG_WAS_CONT_TRANS:
+ /*
+ * A continuation appends to the region under assembly, unless
+ * the region was opened by a zero length op header. In that case
+ * r_cur_item holds the sentinel and this continuation carries
+ * the first data of the region, so fall through and decode it as
+ * the first region of a new item.
+ */
+ if (trans->r_cur_item != XLOG_RECOVER_CONT_ITEM) {
+ error = xlog_recover_add_to_cont_trans(log, trans, dp,
+ len, region_complete);
+ break;
+ }
+ fallthrough;
case 0:
case XLOG_CONTINUE_TRANS:
error = xlog_recover_add_to_trans(log, trans, dp, len,
region_complete);
break;
- case XLOG_WAS_CONT_TRANS:
- error = xlog_recover_add_to_cont_trans(log, trans, dp, len,
- region_complete);
- break;
case XLOG_COMMIT_TRANS:
/*
* The commit record is written after all items in the
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 26/59] xfs: bound the assembled region size during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (24 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 25/59] xfs: handle zero length continuation op headers " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 27/59] xfs: dump the corrupt region when log item validation fails Dave Chinner
` (32 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A region split across continuation op headers is grown one fragment at a time
with no limit on the total size, so a corrupt continuation stream can drive an
unbounded allocation during recovery.
Add XLOG_MAX_REGION_SIZE - the largest object logged in a single region - as a
format level bound usable by both the log write and recovery paths, and reject
a continuation once the assembled region would exceed it. Use the same constant
for the existing per-op-header length check.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_log_format.h | 8 ++++++++
fs/xfs/xfs_log_recover.c | 21 ++++++++++++---------
2 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_log_format.h b/fs/xfs/libxfs/xfs_log_format.h
index 6e620380d5dc..6d49cb831d73 100644
--- a/fs/xfs/libxfs/xfs_log_format.h
+++ b/fs/xfs/libxfs/xfs_log_format.h
@@ -38,6 +38,14 @@ typedef uint32_t xlog_tid_t;
#define XLOG_HEADER_SIZE 512
+/*
+ * The largest object that is logged in a single region: a whole buffer in a
+ * buffer log item, or a remote attribute value. This bounds the size of any
+ * single log region both when writing the log and when reassembling regions
+ * during recovery.
+ */
+#define XLOG_MAX_REGION_SIZE max(XFS_MAX_BLOCKSIZE, XFS_XATTR_SIZE_MAX)
+
/* Minimum number of transactions that must fit in the log (defined by mkfs) */
#define XFS_MIN_LOG_FACTOR 3
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index aea6f5404cec..374de5d21da6 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2415,6 +2415,16 @@ xlog_recover_add_to_cont_trans(
old_ptr = item->ri_buf[item->ri_cnt-1].iov_base;
old_len = item->ri_buf[item->ri_cnt-1].iov_len;
+ /*
+ * Bound the assembled region size before growing the buffer so a corrupt
+ * continuation stream cannot drive an unbounded allocation.
+ */
+ if (old_len + len > XLOG_MAX_REGION_SIZE) {
+ xfs_warn(log->l_mp, "%s: region too large (0x%x)",
+ __func__, old_len + len);
+ return -EFSCORRUPTED;
+ }
+
ptr = kvrealloc(old_ptr, len + old_len, GFP_KERNEL);
if (!ptr)
return -ENOMEM;
@@ -2836,15 +2846,8 @@ xlog_recover_validate_ophdr(
return ERR_PTR(-EFSCORRUPTED);
}
- /*
- * A single region cannot be larger than the largest object we log in
- * one region. That is a whole buffer in a buffer log item (up to
- * XFS_MAX_BLOCKSIZE, e.g. a 64k directory block on a 4k block size
- * filesystem) or a remote attribute value (up to XFS_XATTR_SIZE_MAX,
- * logged in its own region); both are 64k.
- */
- BUILD_BUG_ON(XFS_XATTR_SIZE_MAX > XFS_MAX_BLOCKSIZE);
- if (len > XFS_MAX_BLOCKSIZE) {
+ /* A single region cannot be larger than the largest object we log. */
+ if (len > XLOG_MAX_REGION_SIZE) {
if (!quiet)
xfs_warn(log->l_mp, "%s: length 0x%x too large",
__func__, len);
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 27/59] xfs: dump the corrupt region when log item validation fails
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (25 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 26/59] xfs: bound the assembled region size " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 28/59] xfs: validate inode create and quota off log items during recovery Dave Chinner
` (31 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Log item validation reports failures with a type specific warning, but that
warning does not include the region contents. The existing recovery code
dumped the offending buffer via XFS_CORRUPTION_ERROR for diagnostics, and that
is being removed as per-type validation replaces it.
Emit XFS_CORRUPTION_ERROR from the region completion path when validation
fails, so the type specific warning is accompanied by a hex dump of the
relevant buffer. A per-region failure dumps that region; a whole-item failure
dumps the format header. This is added before the per-type validation so those
patches can drop their own corruption dumps.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_log_recover.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 374de5d21da6..927062a53dd4 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2360,7 +2360,8 @@ xlog_recover_complete_region(
struct xlog_recover *trans)
{
struct xlog_recover_item *item = trans->r_cur_item;
- int error;
+ int region_to_dump = 0;
+ int error = 0;
/*
* The format header (region 0) must be at least the minimum size for
@@ -2373,19 +2374,29 @@ xlog_recover_complete_region(
"log item type 0x%x header too short (%zu < %u)",
item->ri_ops->item_type, item->ri_buf[0].iov_len,
item->ri_ops->min_hdr_len);
- return -EFSCORRUPTED;
+ error = -EFSCORRUPTED;
+ goto out_error;
}
if (item->ri_ops->validate_region) {
error = item->ri_ops->validate_region(log, item,
item->ri_cnt - 1);
- if (error)
- return error;
+ if (error) {
+ /* dump the bad region rather than the log format header */
+ region_to_dump = item->ri_cnt - 1;
+ goto out_error;
+ }
}
if (item->ri_cnt == item->ri_total)
- return xlog_recover_complete_item(log, trans);
- return 0;
+ error = xlog_recover_complete_item(log, trans);
+
+out_error:
+ if (error == -EFSCORRUPTED)
+ XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
+ item->ri_buf[region_to_dump].iov_base,
+ item->ri_buf[region_to_dump].iov_len);
+ return error;
}
STATIC int
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 28/59] xfs: validate inode create and quota off log items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (26 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 27/59] xfs: dump the corrupt region when log item validation fails Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 29/59] xfs: validate extent free " Dave Chinner
` (30 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The inode create and quota off log items are each a single region. Recovery
trusted the region layout decoded from the journal without checking it.
Validate both as they are decoded. A shared region count check rejects
anything other than a single region. The inode create region must be
exactly the format structure size.
Quota off items were last logged by v5.14 kernels. They wrote the format
structure into a region sized as the in-core log item, so the region is 160
bytes on 64 bit platforms and 88 bytes on 32 bit platforms. The quota off
region must be one of those sizes. The rest of the region beyond the format
structure was never written, so only the flags are checked. Quota off was
only logged when it turned off quota accounting, so the flags must name at
least one accounting type and nothing else.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_log_recover.h | 1 +
fs/xfs/xfs_dquot_item_recover.c | 38 +++++++++++++++++++++++++++++++++
fs/xfs/xfs_icreate_item.c | 18 ++++++++++++++++
fs/xfs/xfs_log_recover.c | 25 +++++++++++++++++-----
4 files changed, 77 insertions(+), 5 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_log_recover.h b/fs/xfs/libxfs/xfs_log_recover.h
index d2e128fca058..2aefaac8e5cd 100644
--- a/fs/xfs/libxfs/xfs_log_recover.h
+++ b/fs/xfs/libxfs/xfs_log_recover.h
@@ -196,6 +196,7 @@ int xlog_recover_iget_handle(struct xfs_mount *mp, xfs_ino_t ino, uint32_t gen,
struct xfs_inode **ipp);
void xlog_recover_release_intent(struct xlog *log, unsigned short intent_type,
uint64_t intent_id);
+int xlog_recover_single_region(struct xlog *log, uint16_t nregions);
int xlog_alloc_buf_cancel_table(struct xlog *log);
void xlog_free_buf_cancel_table(struct xlog *log);
diff --git a/fs/xfs/xfs_dquot_item_recover.c b/fs/xfs/xfs_dquot_item_recover.c
index b8854316ac75..1291e50d3031 100644
--- a/fs/xfs/xfs_dquot_item_recover.c
+++ b/fs/xfs/xfs_dquot_item_recover.c
@@ -208,8 +208,46 @@ xlog_recover_quotaoff_commit_pass1(
return 0;
}
+/*
+ * Quota off items were last logged by v5.14 kernels. The format structure was
+ * written at the start of the region, but the region was sized as the in-core
+ * struct xfs_qoff_logitem. The region is therefore 160 bytes on 64 bit platforms
+ * and 88 bytes on 32 bit platforms. Only the format structure was ever written.
+ * The rest of the region is stale memory, so it cannot be checked.
+ */
+#define XFS_QOFF_LOG_REGION_SIZE_64 160
+#define XFS_QOFF_LOG_REGION_SIZE_32 88
+
+static int
+xlog_recover_quotaoff_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_qoff_logformat *qoff_f = item->ri_buf[region_index].iov_base;
+ size_t len = item->ri_buf[region_index].iov_len;
+
+ if (len != XFS_QOFF_LOG_REGION_SIZE_64 &&
+ len != XFS_QOFF_LOG_REGION_SIZE_32) {
+ xfs_warn(log->l_mp, "QOFF: bad item region length %zu recovered.",
+ len);
+ return -EFSCORRUPTED;
+ }
+
+ /* Quota off was only logged when it turned off quota accounting. */
+ if (!qoff_f->qf_flags || (qoff_f->qf_flags & ~XFS_ALL_QUOTA_ACCT)) {
+ xfs_warn(log->l_mp, "QOFF: bad flags 0x%x recovered.",
+ qoff_f->qf_flags);
+ return -EFSCORRUPTED;
+ }
+ return 0;
+}
+
const struct xlog_recover_item_ops xlog_quotaoff_item_ops = {
.item_type = XFS_LI_QUOTAOFF,
+ .min_hdr_len = sizeof(struct xfs_qoff_logformat),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_quotaoff_validate_region,
.commit_pass1 = xlog_recover_quotaoff_commit_pass1,
/* nothing to commit in pass2 */
};
diff --git a/fs/xfs/xfs_icreate_item.c b/fs/xfs/xfs_icreate_item.c
index 95b0eba242e9..bae6a8ad3ca7 100644
--- a/fs/xfs/xfs_icreate_item.c
+++ b/fs/xfs/xfs_icreate_item.c
@@ -253,8 +253,26 @@ xlog_recover_icreate_commit_pass2(
length, be32_to_cpu(icl->icl_gen));
}
+static int
+xlog_recover_icreate_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ if (item->ri_buf[region_index].iov_len !=
+ sizeof(struct xfs_icreate_log)) {
+ xfs_warn(log->l_mp, "ICR: bad item region length %zu recovered.",
+ item->ri_buf[region_index].iov_len);
+ return -EFSCORRUPTED;
+ }
+ return 0;
+}
+
const struct xlog_recover_item_ops xlog_icreate_item_ops = {
.item_type = XFS_LI_ICREATE,
+ .min_hdr_len = sizeof(struct xfs_icreate_log),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_icreate_validate_region,
.reorder = xlog_recover_icreate_reorder,
.commit_pass2 = xlog_recover_icreate_commit_pass2,
};
diff --git a/fs/xfs/xfs_log_recover.c b/fs/xfs/xfs_log_recover.c
index 927062a53dd4..4ed9cecdec03 100644
--- a/fs/xfs/xfs_log_recover.c
+++ b/fs/xfs/xfs_log_recover.c
@@ -2231,14 +2231,25 @@ xlog_recover_nregions(
struct xlog *log,
uint16_t nregions)
{
- if (nregions == 0 || nregions > XLOG_MAX_REGIONS_IN_ITEM) {
- xfs_warn(log->l_mp,
- "bad number of regions (%d) in inode log format", nregions);
+ if (nregions == 0 || nregions > XLOG_MAX_REGIONS_IN_ITEM)
return -EFSCORRUPTED;
- }
return nregions;
}
+/*
+ * ->validate_nregions() helper for the many item types that are made up of a
+ * single region.
+ */
+int
+xlog_recover_single_region(
+ struct xlog *log,
+ uint16_t nregions)
+{
+ if (nregions != 1)
+ return -EFSCORRUPTED;
+ return 1;
+}
+
/*
* Start decoding a new log item. The item type and region count come from the
* type/size fields at the front of the item's first region. Look up the item
@@ -2278,8 +2289,12 @@ xlog_recover_add_item(
valid_nregions = ops->validate_nregions(log, nregions);
else
valid_nregions = xlog_recover_nregions(log, nregions);
- if (valid_nregions < 0)
+ if (valid_nregions < 0) {
+ xfs_warn(log->l_mp,
+ "%s: bad region count %u for log item type 0x%x",
+ __func__, nregions, item_type);
return ERR_PTR(valid_nregions);
+ }
item = kzalloc_obj(struct xlog_recover_item, GFP_KERNEL | __GFP_NOFAIL);
INIT_LIST_HEAD(&item->ri_list);
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 29/59] xfs: validate extent free log items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (27 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 28/59] xfs: validate inode create and quota off log items during recovery Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 30/59] xfs: validate and convert extent free intents " Dave Chinner
` (29 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The extent free intent and done items are single region items whose length
depends on the extent count declared in the region. Recovery only checked this
in the commit handlers, after the item had already been decoded and
initialised. Validate the structure up front as the region is decoded instead.
The data and realtime variants share these checks as their formats are
identical.
The commit handlers and xfs_efi_copy_format() can now trust the structure, so
the redundant checks are removed from them. xfs_efi_copy_format() no longer has
a failure case and returns void.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_extfree_item.c | 216 ++++++++++++++++++++++++--------------
1 file changed, 135 insertions(+), 81 deletions(-)
diff --git a/fs/xfs/xfs_extfree_item.c b/fs/xfs/xfs_extfree_item.c
index 2266d56e37dc..02079e97e443 100644
--- a/fs/xfs/xfs_extfree_item.c
+++ b/fs/xfs/xfs_extfree_item.c
@@ -180,58 +180,58 @@ xfs_efi_init(
* one of which will be the native format for this kernel.
* It will handle the conversion of formats if necessary.
*/
-STATIC int
+STATIC void
xfs_efi_copy_format(
struct kvec *buf,
struct xfs_efi_log_format *dst_efi_fmt)
{
struct xfs_efi_log_format *src_efi_fmt = buf->iov_base;
- uint len, len32, len64, i;
-
- len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents);
- len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents);
- len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents);
+ struct xfs_efi_log_format_32 *src_efi_fmt_32;
+ struct xfs_efi_log_format_64 *src_efi_fmt_64;
+ uint nextents = src_efi_fmt->efi_nextents;
+ uint i;
- if (buf->iov_len == len) {
+ /*
+ * The buffer has already been validated as one of the three formats, so
+ * pick which one it is by its length and copy it into the native form.
+ */
+ if (buf->iov_len == xfs_efi_log_format_sizeof(nextents)) {
memcpy(dst_efi_fmt, src_efi_fmt,
offsetof(struct xfs_efi_log_format, efi_extents));
- for (i = 0; i < src_efi_fmt->efi_nextents; i++)
+ for (i = 0; i < nextents; i++)
memcpy(&dst_efi_fmt->efi_extents[i],
&src_efi_fmt->efi_extents[i],
sizeof(struct xfs_extent));
- return 0;
- } else if (buf->iov_len == len32) {
- struct xfs_efi_log_format_32 *src_efi_fmt_32 = buf->iov_base;
+ return;
+ }
+ if (buf->iov_len == xfs_efi_log_format32_sizeof(nextents)) {
+ src_efi_fmt_32 = buf->iov_base;
dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
- for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
+ for (i = 0; i < nextents; i++) {
dst_efi_fmt->efi_extents[i].ext_start =
src_efi_fmt_32->efi_extents[i].ext_start;
dst_efi_fmt->efi_extents[i].ext_len =
src_efi_fmt_32->efi_extents[i].ext_len;
}
- return 0;
- } else if (buf->iov_len == len64) {
- struct xfs_efi_log_format_64 *src_efi_fmt_64 = buf->iov_base;
-
- dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
- dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
- dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
- dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
- for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
- dst_efi_fmt->efi_extents[i].ext_start =
- src_efi_fmt_64->efi_extents[i].ext_start;
- dst_efi_fmt->efi_extents[i].ext_len =
- src_efi_fmt_64->efi_extents[i].ext_len;
- }
- return 0;
+ return;
+ }
+
+ ASSERT(buf->iov_len == xfs_efi_log_format64_sizeof(nextents));
+ src_efi_fmt_64 = buf->iov_base;
+ dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
+ dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
+ dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
+ dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
+ for (i = 0; i < nextents; i++) {
+ dst_efi_fmt->efi_extents[i].ext_start =
+ src_efi_fmt_64->efi_extents[i].ext_start;
+ dst_efi_fmt->efi_extents[i].ext_len =
+ src_efi_fmt_64->efi_extents[i].ext_len;
}
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->iov_base,
- buf->iov_len);
- return -EFSCORRUPTED;
}
static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
@@ -864,22 +864,11 @@ xlog_recover_efi_commit_pass2(
struct xfs_mount *mp = log->l_mp;
struct xfs_efi_log_item *efip;
struct xfs_efi_log_format *efi_formatp;
- int error;
efi_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len < xfs_efi_log_format_sizeof(0)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
- }
-
efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents);
- error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
- if (error) {
- xfs_efi_item_free(efip);
- return error;
- }
+ xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
xlog_recover_intent_item(log, &efip->efi_item, lsn,
@@ -887,8 +876,59 @@ xlog_recover_efi_commit_pass2(
return 0;
}
+/*
+ * The efi is a single region whose length must match the extent count for one
+ * of the native, 32 bit or 64 bit on-disk formats. This is shared by the data
+ * and realtime efi items, which have identical formats.
+ */
+static int
+xlog_recover_efi_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_efi_log_format *efi_formatp =
+ item->ri_buf[region_index].iov_base;
+ size_t len = item->ri_buf[region_index].iov_len;
+ uint32_t nextents;
+
+ if (len < xfs_efi_log_format_sizeof(0)) {
+ xfs_warn(log->l_mp, "EFI: bad item region length %zu recovered.",
+ len);
+ return -EFSCORRUPTED;
+ }
+
+ /*
+ * Bound the extent count before the exact size checks below so a corrupt
+ * count cannot overflow the size calculations. This limit is a fuzzy
+ * upper bound using the smallest extent encoding to prevent overflow; it
+ * does not need to be exact as the size checks that follow enforce the
+ * precise length for each format.
+ */
+ nextents = efi_formatp->efi_nextents;
+ if (nextents == 0 ||
+ nextents > XLOG_MAX_REGION_SIZE / sizeof(struct xfs_extent_32)) {
+ xfs_warn(log->l_mp, "EFI: bad extent count %u recovered.",
+ nextents);
+ return -EFSCORRUPTED;
+ }
+
+ if (len != xfs_efi_log_format32_sizeof(nextents) &&
+ len != xfs_efi_log_format64_sizeof(nextents)) {
+ xfs_warn(log->l_mp,
+ "EFI: item region length %zu does not match %u extents recovered.",
+ len, nextents);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
const struct xlog_recover_item_ops xlog_efi_item_ops = {
.item_type = XFS_LI_EFI,
+ .min_hdr_len = sizeof(struct xfs_efi_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_efi_validate_region,
.commit_pass2 = xlog_recover_efi_commit_pass2,
};
@@ -903,22 +943,11 @@ xlog_recover_rtefi_commit_pass2(
struct xfs_mount *mp = log->l_mp;
struct xfs_efi_log_item *efip;
struct xfs_efi_log_format *efi_formatp;
- int error;
efi_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len < xfs_efi_log_format_sizeof(0)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
- }
-
efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents);
- error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
- if (error) {
- xfs_efi_item_free(efip);
- return error;
- }
+ xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
xlog_recover_intent_item(log, &efip->efi_item, lsn,
@@ -941,6 +970,9 @@ xlog_recover_rtefi_commit_pass2(
const struct xlog_recover_item_ops xlog_rtefi_item_ops = {
.item_type = XFS_LI_EFI_RT,
+ .min_hdr_len = sizeof(struct xfs_efi_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_efi_validate_region,
.commit_pass2 = xlog_recover_rtefi_commit_pass2,
};
@@ -959,31 +991,66 @@ xlog_recover_efd_commit_pass2(
xfs_lsn_t lsn)
{
struct xfs_efd_log_format *efd_formatp;
- int buflen = item->ri_buf[0].iov_len;
efd_formatp = item->ri_buf[0].iov_base;
- if (buflen < sizeof(struct xfs_efd_log_format)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- efd_formatp, buflen);
+ xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
+ return 0;
+}
+
+/*
+ * The efd is a single region whose length must match the extent count for the
+ * 32 bit or 64 bit on-disk format. This is shared by the data and realtime efd
+ * items, which have identical formats.
+ */
+static int
+xlog_recover_efd_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_efd_log_format *efd_formatp =
+ item->ri_buf[region_index].iov_base;
+ size_t len = item->ri_buf[region_index].iov_len;
+ uint32_t nextents;
+
+ if (len < sizeof(struct xfs_efd_log_format)) {
+ xfs_warn(log->l_mp, "EFD: bad item region length %zu recovered.",
+ len);
return -EFSCORRUPTED;
}
- if (item->ri_buf[0].iov_len != xfs_efd_log_format32_sizeof(
- efd_formatp->efd_nextents) &&
- item->ri_buf[0].iov_len != xfs_efd_log_format64_sizeof(
- efd_formatp->efd_nextents)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- efd_formatp, buflen);
+ /*
+ * Bound the extent count before the exact size checks below so a corrupt
+ * count cannot overflow the size calculations. This limit is a fuzzy
+ * upper bound using the smallest extent encoding to prevent overflow; it
+ * does not need to be exact as the size checks that follow enforce the
+ * precise length for each format.
+ */
+ nextents = efd_formatp->efd_nextents;
+ if (nextents == 0 ||
+ nextents > XLOG_MAX_REGION_SIZE / sizeof(struct xfs_extent_32)) {
+ xfs_warn(log->l_mp, "EFD: bad extent count %u recovered.",
+ nextents);
+ return -EFSCORRUPTED;
+ }
+
+ if (len != xfs_efd_log_format32_sizeof(nextents) &&
+ len != xfs_efd_log_format64_sizeof(nextents)) {
+ xfs_warn(log->l_mp,
+ "EFD: item region length %zu does not match %u extents recovered.",
+ len, nextents);
return -EFSCORRUPTED;
}
- xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
return 0;
}
const struct xlog_recover_item_ops xlog_efd_item_ops = {
.item_type = XFS_LI_EFD,
+ .min_hdr_len = sizeof(struct xfs_efd_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_efd_validate_region,
.commit_pass2 = xlog_recover_efd_commit_pass2,
};
@@ -996,25 +1063,9 @@ xlog_recover_rtefd_commit_pass2(
xfs_lsn_t lsn)
{
struct xfs_efd_log_format *efd_formatp;
- int buflen = item->ri_buf[0].iov_len;
efd_formatp = item->ri_buf[0].iov_base;
- if (buflen < sizeof(struct xfs_efd_log_format)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- efd_formatp, buflen);
- return -EFSCORRUPTED;
- }
-
- if (item->ri_buf[0].iov_len != xfs_efd_log_format32_sizeof(
- efd_formatp->efd_nextents) &&
- item->ri_buf[0].iov_len != xfs_efd_log_format64_sizeof(
- efd_formatp->efd_nextents)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- efd_formatp, buflen);
- return -EFSCORRUPTED;
- }
-
xlog_recover_release_intent(log, XFS_LI_EFI_RT,
efd_formatp->efd_efi_id);
return 0;
@@ -1025,5 +1076,8 @@ xlog_recover_rtefd_commit_pass2(
const struct xlog_recover_item_ops xlog_rtefd_item_ops = {
.item_type = XFS_LI_EFD_RT,
+ .min_hdr_len = sizeof(struct xfs_efd_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_efd_validate_region,
.commit_pass2 = xlog_recover_rtefd_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 30/59] xfs: validate and convert extent free intents during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (28 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 29/59] xfs: validate extent free " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 31/59] xfs: validate and convert extent free done items " Dave Chinner
` (28 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The extents in a recovered extent free intent were only checked for validity
during intent replay, after the item had been decoded, copied to host format
and queued. A corrupt extent was therefore carried a long way into recovery
before being rejected.
Validate the extents up front and convert the item to host format in
->validate_item(), so the rest of recovery iterates the item as a host
structure without regard for the on-disk format. The conversion allocates a
new buffer only when the on-disk format differs in size from the host format.
The commit handlers then copy the already host format item directly, and the
extent validity check is dropped from intent replay. The format conversion no
longer needs its host format input case and is renamed accordingly. This is
shared by the data and realtime extent free items.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_extfree_item.c | 88 +++++++++++++++++++++++----------------
1 file changed, 51 insertions(+), 37 deletions(-)
diff --git a/fs/xfs/xfs_extfree_item.c b/fs/xfs/xfs_extfree_item.c
index 02079e97e443..893d19812a0c 100644
--- a/fs/xfs/xfs_extfree_item.c
+++ b/fs/xfs/xfs_extfree_item.c
@@ -174,14 +174,13 @@ xfs_efi_init(
}
/*
- * Copy an EFI format buffer from the given buf, and into the destination
- * EFI format structure.
- * The given buffer can be in 32 bit or 64 bit form (which has different padding),
- * one of which will be the native format for this kernel.
- * It will handle the conversion of formats if necessary.
+ * Convert an on-disk EFI format buffer into the given host format structure.
+ * The buffer is in 32 bit or 64 bit on-disk form (which have different padding);
+ * a buffer already in host format does not need conversion and is not passed
+ * here. The buffer has already been validated as one of these two formats.
*/
STATIC void
-xfs_efi_copy_format(
+xfs_efi_to_host_format(
struct kvec *buf,
struct xfs_efi_log_format *dst_efi_fmt)
{
@@ -191,20 +190,6 @@ xfs_efi_copy_format(
uint nextents = src_efi_fmt->efi_nextents;
uint i;
- /*
- * The buffer has already been validated as one of the three formats, so
- * pick which one it is by its length and copy it into the native form.
- */
- if (buf->iov_len == xfs_efi_log_format_sizeof(nextents)) {
- memcpy(dst_efi_fmt, src_efi_fmt,
- offsetof(struct xfs_efi_log_format, efi_extents));
- for (i = 0; i < nextents; i++)
- memcpy(&dst_efi_fmt->efi_extents[i],
- &src_efi_fmt->efi_extents[i],
- sizeof(struct xfs_extent));
- return;
- }
-
if (buf->iov_len == xfs_efi_log_format32_sizeof(nextents)) {
src_efi_fmt_32 = buf->iov_base;
dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
@@ -660,23 +645,10 @@ xfs_extent_free_recover_work(
int error = 0;
bool isrt = xfs_efi_item_isrt(lip);
- /*
- * First check the validity of the extents described by the EFI. If
- * any are bad, then assume that all are bad and just toss the EFI.
- * Mixing RT and non-RT extents in the same EFI item is not allowed.
- */
- for (i = 0; i < efip->efi_format.efi_nextents; i++) {
- if (!xfs_efi_validate_ext(mp, isrt,
- &efip->efi_format.efi_extents[i])) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- &efip->efi_format,
- sizeof(efip->efi_format));
- return -EFSCORRUPTED;
- }
-
+ /* The extents were validated when the efi was recovered. */
+ for (i = 0; i < efip->efi_format.efi_nextents; i++)
xfs_efi_recover_work(mp, dfp, isrt,
&efip->efi_format.efi_extents[i]);
- }
resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
error = xfs_trans_alloc(mp, &resv, 0, 0, 0, &tp);
@@ -868,7 +840,7 @@ xlog_recover_efi_commit_pass2(
efi_formatp = item->ri_buf[0].iov_base;
efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents);
- xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
+ memcpy(&efip->efi_format, efi_formatp, item->ri_buf[0].iov_len);
atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
xlog_recover_intent_item(log, &efip->efi_item, lsn,
@@ -924,11 +896,52 @@ xlog_recover_efi_validate_region(
return 0;
}
+/*
+ * Convert the efi to host format and validate its contents. Later recovery then
+ * iterates ri_buf[0] as a host struct xfs_efi_log_format without caring about
+ * the on-disk format. The conversion only needs to allocate a new buffer when
+ * the on-disk format differs in size from the host format; a buffer already in
+ * host format is validated in place.
+ */
+static int
+xlog_recover_efi_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_efi_log_format *efi_formatp = item->ri_buf[0].iov_base;
+ uint32_t nextents = efi_formatp->efi_nextents;
+ size_t host_len = xfs_efi_log_format_sizeof(nextents);
+ bool isrt = ITEM_TYPE(item) == XFS_LI_EFI_RT;
+ uint32_t i;
+
+ if (item->ri_buf[0].iov_len != host_len) {
+ efi_formatp = kzalloc(host_len, GFP_KERNEL | __GFP_NOFAIL);
+ xfs_efi_to_host_format(&item->ri_buf[0], efi_formatp);
+ kvfree(item->ri_buf[0].iov_base);
+ item->ri_buf[0].iov_base = efi_formatp;
+ item->ri_buf[0].iov_len = host_len;
+ }
+
+ for (i = 0; i < nextents; i++) {
+ struct xfs_extent *extp = &efi_formatp->efi_extents[i];
+
+ if (!xfs_efi_validate_ext(log->l_mp, isrt, extp)) {
+ xfs_warn(log->l_mp,
+ "EFI: bad extent %u (start 0x%llx, len 0x%x) recovered.",
+ i, extp->ext_start, extp->ext_len);
+ return -EFSCORRUPTED;
+ }
+ }
+
+ return 0;
+}
+
const struct xlog_recover_item_ops xlog_efi_item_ops = {
.item_type = XFS_LI_EFI,
.min_hdr_len = sizeof(struct xfs_efi_log_format),
.validate_nregions = xlog_recover_single_region,
.validate_region = xlog_recover_efi_validate_region,
+ .validate_item = xlog_recover_efi_validate_item,
.commit_pass2 = xlog_recover_efi_commit_pass2,
};
@@ -947,7 +960,7 @@ xlog_recover_rtefi_commit_pass2(
efi_formatp = item->ri_buf[0].iov_base;
efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents);
- xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
+ memcpy(&efip->efi_format, efi_formatp, item->ri_buf[0].iov_len);
atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
xlog_recover_intent_item(log, &efip->efi_item, lsn,
@@ -973,6 +986,7 @@ const struct xlog_recover_item_ops xlog_rtefi_item_ops = {
.min_hdr_len = sizeof(struct xfs_efi_log_format),
.validate_nregions = xlog_recover_single_region,
.validate_region = xlog_recover_efi_validate_region,
+ .validate_item = xlog_recover_efi_validate_item,
.commit_pass2 = xlog_recover_rtefi_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 31/59] xfs: validate and convert extent free done items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (29 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 30/59] xfs: validate and convert extent free intents " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 32/59] xfs: validate rmap update intents " Dave Chinner
` (27 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
An extent free done item carries the same extents as the intent it completes,
but recovery never converted it to host format or checked those extents. The
on-disk format was left in place and its contents trusted.
Convert the item to host format and validate its extents in ->validate_item(),
mirroring the extent free intent item. The conversion allocates a new buffer
only when the on-disk format differs in size from the host format. This is
shared by the data and realtime extent free done items.
This conversion and validation is being done now so that, in future, the EFD
extents can be checked against the extents in the matched EFI. This will ensure
a corruption of the efd_efi_id does not result in completion of an incorrect
EFI.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_extfree_item.c | 85 +++++++++++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+)
diff --git a/fs/xfs/xfs_extfree_item.c b/fs/xfs/xfs_extfree_item.c
index 893d19812a0c..7ec18c8224cb 100644
--- a/fs/xfs/xfs_extfree_item.c
+++ b/fs/xfs/xfs_extfree_item.c
@@ -219,6 +219,52 @@ xfs_efi_to_host_format(
}
}
+/*
+ * Convert an on-disk EFD format buffer into the given host format structure.
+ * The buffer is in 32 bit or 64 bit on-disk form (which have different padding);
+ * a buffer already in host format does not need conversion and is not passed
+ * here. The buffer has already been validated as one of these two formats.
+ */
+STATIC void
+xfs_efd_to_host_format(
+ struct kvec *buf,
+ struct xfs_efd_log_format *dst_efd_fmt)
+{
+ struct xfs_efd_log_format *src_efd_fmt = buf->iov_base;
+ struct xfs_efd_log_format_32 *src_efd_fmt_32;
+ struct xfs_efd_log_format_64 *src_efd_fmt_64;
+ uint nextents = src_efd_fmt->efd_nextents;
+ uint i;
+
+ if (buf->iov_len == xfs_efd_log_format32_sizeof(nextents)) {
+ src_efd_fmt_32 = buf->iov_base;
+ dst_efd_fmt->efd_type = src_efd_fmt_32->efd_type;
+ dst_efd_fmt->efd_size = src_efd_fmt_32->efd_size;
+ dst_efd_fmt->efd_nextents = src_efd_fmt_32->efd_nextents;
+ dst_efd_fmt->efd_efi_id = src_efd_fmt_32->efd_efi_id;
+ for (i = 0; i < nextents; i++) {
+ dst_efd_fmt->efd_extents[i].ext_start =
+ src_efd_fmt_32->efd_extents[i].ext_start;
+ dst_efd_fmt->efd_extents[i].ext_len =
+ src_efd_fmt_32->efd_extents[i].ext_len;
+ }
+ return;
+ }
+
+ ASSERT(buf->iov_len == xfs_efd_log_format64_sizeof(nextents));
+ src_efd_fmt_64 = buf->iov_base;
+ dst_efd_fmt->efd_type = src_efd_fmt_64->efd_type;
+ dst_efd_fmt->efd_size = src_efd_fmt_64->efd_size;
+ dst_efd_fmt->efd_nextents = src_efd_fmt_64->efd_nextents;
+ dst_efd_fmt->efd_efi_id = src_efd_fmt_64->efd_efi_id;
+ for (i = 0; i < nextents; i++) {
+ dst_efd_fmt->efd_extents[i].ext_start =
+ src_efd_fmt_64->efd_extents[i].ext_start;
+ dst_efd_fmt->efd_extents[i].ext_len =
+ src_efd_fmt_64->efd_extents[i].ext_len;
+ }
+}
+
static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
{
return container_of(lip, struct xfs_efd_log_item, efd_item);
@@ -1060,11 +1106,49 @@ xlog_recover_efd_validate_region(
return 0;
}
+/*
+ * Convert the efd to host format and validate its contents, as for the efi.
+ * The efd extents mirror those of the efi it completes.
+ */
+static int
+xlog_recover_efd_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_efd_log_format *efd_formatp = item->ri_buf[0].iov_base;
+ uint32_t nextents = efd_formatp->efd_nextents;
+ size_t host_len = xfs_efd_log_format_sizeof(nextents);
+ bool isrt = ITEM_TYPE(item) == XFS_LI_EFD_RT;
+ uint32_t i;
+
+ if (item->ri_buf[0].iov_len != host_len) {
+ efd_formatp = kzalloc(host_len, GFP_KERNEL | __GFP_NOFAIL);
+ xfs_efd_to_host_format(&item->ri_buf[0], efd_formatp);
+ kvfree(item->ri_buf[0].iov_base);
+ item->ri_buf[0].iov_base = efd_formatp;
+ item->ri_buf[0].iov_len = host_len;
+ }
+
+ for (i = 0; i < nextents; i++) {
+ struct xfs_extent *extp = &efd_formatp->efd_extents[i];
+
+ if (!xfs_efi_validate_ext(log->l_mp, isrt, extp)) {
+ xfs_warn(log->l_mp,
+ "EFD: bad extent %u (start 0x%llx, len 0x%x) recovered.",
+ i, extp->ext_start, extp->ext_len);
+ return -EFSCORRUPTED;
+ }
+ }
+
+ return 0;
+}
+
const struct xlog_recover_item_ops xlog_efd_item_ops = {
.item_type = XFS_LI_EFD,
.min_hdr_len = sizeof(struct xfs_efd_log_format),
.validate_nregions = xlog_recover_single_region,
.validate_region = xlog_recover_efd_validate_region,
+ .validate_item = xlog_recover_efd_validate_item,
.commit_pass2 = xlog_recover_efd_commit_pass2,
};
@@ -1093,5 +1177,6 @@ const struct xlog_recover_item_ops xlog_rtefd_item_ops = {
.min_hdr_len = sizeof(struct xfs_efd_log_format),
.validate_nregions = xlog_recover_single_region,
.validate_region = xlog_recover_efd_validate_region,
+ .validate_item = xlog_recover_efd_validate_item,
.commit_pass2 = xlog_recover_rtefd_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 32/59] xfs: validate rmap update intents during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (30 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 31/59] xfs: validate and convert extent free done items " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 33/59] xfs: validate rmap update done items " Dave Chinner
` (26 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
An rmap update intent is a single region item whose length depends on its
extent count, and whose extents carry rmap owner, offset and state that
recovery must trust. These were only checked during intent replay, after the
item had been decoded and queued.
Validate the region length as it is decoded, and validate the rmap extents in
->validate_item(). The rmap map extent has fixed width fields, so there is a
single on-disk format and no conversion is required. The commit handlers and
intent replay can then trust the item, so the redundant checks are removed from
them. This is shared by the data and realtime rmap update items.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_rmap_item.c | 119 ++++++++++++++++++++++++++---------------
1 file changed, 77 insertions(+), 42 deletions(-)
diff --git a/fs/xfs/xfs_rmap_item.c b/fs/xfs/xfs_rmap_item.c
index be2d5d6fe863..def068cef09d 100644
--- a/fs/xfs/xfs_rmap_item.c
+++ b/fs/xfs/xfs_rmap_item.c
@@ -578,23 +578,10 @@ xfs_rmap_recover_work(
int i;
int error = 0;
- /*
- * First check the validity of the extents described by the
- * RUI. If any are bad, then assume that all are bad and
- * just toss the RUI.
- */
- for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
- if (!xfs_rui_validate_map(mp, isrt,
- &ruip->rui_format.rui_extents[i])) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- &ruip->rui_format,
- sizeof(ruip->rui_format));
- return -EFSCORRUPTED;
- }
-
+ /* The extents were validated when the rui was recovered. */
+ for (i = 0; i < ruip->rui_format.rui_nextents; i++)
xfs_rui_recover_work(mp, dfp, isrt,
&ruip->rui_format.rui_extents[i]);
- }
resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
if (isrt)
@@ -746,34 +733,92 @@ xlog_recover_rui_commit_pass2(
struct xfs_mount *mp = log->l_mp;
struct xfs_rui_log_item *ruip;
struct xfs_rui_log_format *rui_formatp;
- size_t len;
rui_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len < xfs_rui_log_format_sizeof(0)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ ruip = xfs_rui_init(mp, ITEM_TYPE(item), rui_formatp->rui_nextents);
+ xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
+ atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
+
+ xlog_recover_intent_item(log, &ruip->rui_item, lsn,
+ &xfs_rmap_update_defer_type);
+ return 0;
+}
+
+/*
+ * The rui is a single region whose length must match its extent count. The
+ * rmap map extent has fixed width fields, so there is a single on-disk format.
+ * This is shared by the data and realtime rui items, which have identical
+ * formats.
+ */
+static int
+xlog_recover_rui_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_rui_log_format *rui_formatp =
+ item->ri_buf[region_index].iov_base;
+ size_t len = item->ri_buf[region_index].iov_len;
+ uint32_t nextents;
+
+ if (len < xfs_rui_log_format_sizeof(0)) {
+ xfs_warn(log->l_mp, "RUI: bad item region length %zu recovered.",
+ len);
return -EFSCORRUPTED;
}
- len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
- if (item->ri_buf[0].iov_len != len) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ /*
+ * Bound the extent count before the exact size check below so a corrupt
+ * count cannot overflow the size calculation. This limit is a fuzzy
+ * upper bound to prevent overflow; the size check that follows enforces
+ * the precise length.
+ */
+ nextents = rui_formatp->rui_nextents;
+ if (nextents == 0 ||
+ nextents > XLOG_MAX_REGION_SIZE / sizeof(struct xfs_map_extent)) {
+ xfs_warn(log->l_mp, "RUI: bad extent count %u recovered.",
+ nextents);
return -EFSCORRUPTED;
}
- ruip = xfs_rui_init(mp, ITEM_TYPE(item), rui_formatp->rui_nextents);
- xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
- atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
+ if (len != xfs_rui_log_format_sizeof(nextents)) {
+ xfs_warn(log->l_mp,
+ "RUI: item region length %zu does not match %u extents recovered.",
+ len, nextents);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
+/* Validate the rmap update extents in the rui. */
+static int
+xlog_recover_rui_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_rui_log_format *rui_formatp = item->ri_buf[0].iov_base;
+ bool isrt = ITEM_TYPE(item) == XFS_LI_RUI_RT;
+ uint32_t i;
+
+ for (i = 0; i < rui_formatp->rui_nextents; i++) {
+ if (!xfs_rui_validate_map(log->l_mp, isrt,
+ &rui_formatp->rui_extents[i])) {
+ xfs_warn(log->l_mp, "RUI: bad extent %u recovered.", i);
+ return -EFSCORRUPTED;
+ }
+ }
- xlog_recover_intent_item(log, &ruip->rui_item, lsn,
- &xfs_rmap_update_defer_type);
return 0;
}
const struct xlog_recover_item_ops xlog_rui_item_ops = {
.item_type = XFS_LI_RUI,
+ .min_hdr_len = sizeof(struct xfs_rui_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_rui_validate_region,
+ .validate_item = xlog_recover_rui_validate_item,
.commit_pass2 = xlog_recover_rui_commit_pass2,
};
@@ -788,23 +833,9 @@ xlog_recover_rtrui_commit_pass2(
struct xfs_mount *mp = log->l_mp;
struct xfs_rui_log_item *ruip;
struct xfs_rui_log_format *rui_formatp;
- size_t len;
rui_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len < xfs_rui_log_format_sizeof(0)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
- }
-
- len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
- if (item->ri_buf[0].iov_len != len) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
- }
-
ruip = xfs_rui_init(mp, ITEM_TYPE(item), rui_formatp->rui_nextents);
xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
@@ -829,6 +860,10 @@ xlog_recover_rtrui_commit_pass2(
const struct xlog_recover_item_ops xlog_rtrui_item_ops = {
.item_type = XFS_LI_RUI_RT,
+ .min_hdr_len = sizeof(struct xfs_rui_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_rui_validate_region,
+ .validate_item = xlog_recover_rui_validate_item,
.commit_pass2 = xlog_recover_rtrui_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 33/59] xfs: validate rmap update done items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (31 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 32/59] xfs: validate rmap update intents " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 34/59] xfs: validate refcount update intents " Dave Chinner
` (25 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
An rmap update done item is a single fixed size region, but recovery only
checked its length in the commit handler and never checked its padding.
Validate the region length and require the padding to be zero as the item is
decoded. The commit handlers can then trust the item, so the length check is
removed from them. This is shared by the data and realtime rmap update done
items.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_rmap_item.c | 44 ++++++++++++++++++++++++++++++++----------
1 file changed, 34 insertions(+), 10 deletions(-)
diff --git a/fs/xfs/xfs_rmap_item.c b/fs/xfs/xfs_rmap_item.c
index def068cef09d..3a353256a930 100644
--- a/fs/xfs/xfs_rmap_item.c
+++ b/fs/xfs/xfs_rmap_item.c
@@ -884,18 +884,45 @@ xlog_recover_rud_commit_pass2(
struct xfs_rud_log_format *rud_formatp;
rud_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len != sizeof(struct xfs_rud_log_format)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- rud_formatp, item->ri_buf[0].iov_len);
+ xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
+ return 0;
+}
+
+/*
+ * The rud is a single fixed size region. This is shared by the data and
+ * realtime rud items, which have identical formats.
+ */
+static int
+xlog_recover_rud_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_rud_log_format *rud_formatp =
+ item->ri_buf[region_index].iov_base;
+
+ if (item->ri_buf[region_index].iov_len !=
+ sizeof(struct xfs_rud_log_format)) {
+ xfs_warn(log->l_mp, "RUD: bad item region length %zu recovered.",
+ item->ri_buf[region_index].iov_len);
+ return -EFSCORRUPTED;
+ }
+
+ /* The padding is always written as zero. */
+ if (rud_formatp->__pad != 0) {
+ xfs_warn(log->l_mp, "RUD: bad padding 0x%x recovered.",
+ rud_formatp->__pad);
return -EFSCORRUPTED;
}
- xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
return 0;
}
const struct xlog_recover_item_ops xlog_rud_item_ops = {
.item_type = XFS_LI_RUD,
+ .min_hdr_len = sizeof(struct xfs_rud_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_rud_validate_region,
.commit_pass2 = xlog_recover_rud_commit_pass2,
};
@@ -910,12 +937,6 @@ xlog_recover_rtrud_commit_pass2(
struct xfs_rud_log_format *rud_formatp;
rud_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len != sizeof(struct xfs_rud_log_format)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- rud_formatp, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
- }
-
xlog_recover_release_intent(log, XFS_LI_RUI_RT,
rud_formatp->rud_rui_id);
return 0;
@@ -926,5 +947,8 @@ xlog_recover_rtrud_commit_pass2(
const struct xlog_recover_item_ops xlog_rtrud_item_ops = {
.item_type = XFS_LI_RUD_RT,
+ .min_hdr_len = sizeof(struct xfs_rud_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_rud_validate_region,
.commit_pass2 = xlog_recover_rtrud_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 34/59] xfs: validate refcount update intents during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (32 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 33/59] xfs: validate rmap update done items " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 35/59] xfs: validate refcount update done items " Dave Chinner
` (24 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A refcount update intent is a single region item whose length depends on its
extent count, and whose extents carry refcount operation state that recovery
must trust. These were only checked during intent replay, after the item had
been decoded and queued.
Validate the region length as it is decoded, and validate the refcount extents
in ->validate_item(). The commit handlers and intent replay can then trust the
item, so the redundant checks are removed from them. This is shared by the data
and realtime refcount update items.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_refcount_item.c | 119 ++++++++++++++++++++++++-------------
1 file changed, 77 insertions(+), 42 deletions(-)
diff --git a/fs/xfs/xfs_refcount_item.c b/fs/xfs/xfs_refcount_item.c
index 682c6e1b45e3..1a5a4c3fb913 100644
--- a/fs/xfs/xfs_refcount_item.c
+++ b/fs/xfs/xfs_refcount_item.c
@@ -513,23 +513,10 @@ xfs_refcount_recover_work(
int i;
int error = 0;
- /*
- * First check the validity of the extents described by the
- * CUI. If any are bad, then assume that all are bad and
- * just toss the CUI.
- */
- for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
- if (!xfs_cui_validate_phys(mp, isrt,
- &cuip->cui_format.cui_extents[i])) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- &cuip->cui_format,
- sizeof(cuip->cui_format));
- return -EFSCORRUPTED;
- }
-
+ /* The extents were validated when the cui was recovered. */
+ for (i = 0; i < cuip->cui_format.cui_nextents; i++)
xfs_cui_recover_work(mp, dfp, isrt,
&cuip->cui_format.cui_extents[i]);
- }
/*
* Under normal operation, refcount updates are deferred, so we
@@ -717,34 +704,92 @@ xlog_recover_cui_commit_pass2(
struct xfs_mount *mp = log->l_mp;
struct xfs_cui_log_item *cuip;
struct xfs_cui_log_format *cui_formatp;
- size_t len;
cui_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len < xfs_cui_log_format_sizeof(0)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ cuip = xfs_cui_init(mp, ITEM_TYPE(item), cui_formatp->cui_nextents);
+ xfs_cui_copy_format(&cuip->cui_format, cui_formatp);
+ atomic_set(&cuip->cui_next_extent, cui_formatp->cui_nextents);
+
+ xlog_recover_intent_item(log, &cuip->cui_item, lsn,
+ &xfs_refcount_update_defer_type);
+ return 0;
+}
+
+/*
+ * The cui is a single region whose length must match its extent count. The
+ * refcount phys extent has fixed width fields, so there is a single on-disk
+ * format. This is shared by the data and realtime cui items, which have
+ * identical formats.
+ */
+static int
+xlog_recover_cui_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_cui_log_format *cui_formatp =
+ item->ri_buf[region_index].iov_base;
+ size_t len = item->ri_buf[region_index].iov_len;
+ uint32_t nextents;
+
+ if (len < xfs_cui_log_format_sizeof(0)) {
+ xfs_warn(log->l_mp, "CUI: bad item region length %zu recovered.",
+ len);
return -EFSCORRUPTED;
}
- len = xfs_cui_log_format_sizeof(cui_formatp->cui_nextents);
- if (item->ri_buf[0].iov_len != len) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ /*
+ * Bound the extent count before the exact size check below so a corrupt
+ * count cannot overflow the size calculation. This limit is a fuzzy
+ * upper bound to prevent overflow; the size check that follows enforces
+ * the precise length.
+ */
+ nextents = cui_formatp->cui_nextents;
+ if (nextents == 0 ||
+ nextents > XLOG_MAX_REGION_SIZE / sizeof(struct xfs_phys_extent)) {
+ xfs_warn(log->l_mp, "CUI: bad extent count %u recovered.",
+ nextents);
return -EFSCORRUPTED;
}
- cuip = xfs_cui_init(mp, ITEM_TYPE(item), cui_formatp->cui_nextents);
- xfs_cui_copy_format(&cuip->cui_format, cui_formatp);
- atomic_set(&cuip->cui_next_extent, cui_formatp->cui_nextents);
+ if (len != xfs_cui_log_format_sizeof(nextents)) {
+ xfs_warn(log->l_mp,
+ "CUI: item region length %zu does not match %u extents recovered.",
+ len, nextents);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
+/* Validate the refcount update extents in the cui. */
+static int
+xlog_recover_cui_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_cui_log_format *cui_formatp = item->ri_buf[0].iov_base;
+ bool isrt = ITEM_TYPE(item) == XFS_LI_CUI_RT;
+ uint32_t i;
+
+ for (i = 0; i < cui_formatp->cui_nextents; i++) {
+ if (!xfs_cui_validate_phys(log->l_mp, isrt,
+ &cui_formatp->cui_extents[i])) {
+ xfs_warn(log->l_mp, "CUI: bad extent %u recovered.", i);
+ return -EFSCORRUPTED;
+ }
+ }
- xlog_recover_intent_item(log, &cuip->cui_item, lsn,
- &xfs_refcount_update_defer_type);
return 0;
}
const struct xlog_recover_item_ops xlog_cui_item_ops = {
.item_type = XFS_LI_CUI,
+ .min_hdr_len = sizeof(struct xfs_cui_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_cui_validate_region,
+ .validate_item = xlog_recover_cui_validate_item,
.commit_pass2 = xlog_recover_cui_commit_pass2,
};
@@ -759,23 +804,9 @@ xlog_recover_rtcui_commit_pass2(
struct xfs_mount *mp = log->l_mp;
struct xfs_cui_log_item *cuip;
struct xfs_cui_log_format *cui_formatp;
- size_t len;
cui_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len < xfs_cui_log_format_sizeof(0)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
- }
-
- len = xfs_cui_log_format_sizeof(cui_formatp->cui_nextents);
- if (item->ri_buf[0].iov_len != len) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
- }
-
cuip = xfs_cui_init(mp, ITEM_TYPE(item), cui_formatp->cui_nextents);
xfs_cui_copy_format(&cuip->cui_format, cui_formatp);
atomic_set(&cuip->cui_next_extent, cui_formatp->cui_nextents);
@@ -800,6 +831,10 @@ xlog_recover_rtcui_commit_pass2(
const struct xlog_recover_item_ops xlog_rtcui_item_ops = {
.item_type = XFS_LI_CUI_RT,
+ .min_hdr_len = sizeof(struct xfs_cui_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_cui_validate_region,
+ .validate_item = xlog_recover_cui_validate_item,
.commit_pass2 = xlog_recover_rtcui_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 35/59] xfs: validate refcount update done items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (33 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 34/59] xfs: validate refcount update intents " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 36/59] xfs: validate bmap update intents " Dave Chinner
` (23 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A refcount update done item is a single fixed size region, but recovery only
checked its length in the commit handler and never checked its padding.
Validate the region length and require the padding to be zero as the item is
decoded. The commit handlers can then trust the item, so the length check is
removed from them. This is shared by the data and realtime refcount update done
items.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_refcount_item.c | 44 +++++++++++++++++++++++++++++---------
1 file changed, 34 insertions(+), 10 deletions(-)
diff --git a/fs/xfs/xfs_refcount_item.c b/fs/xfs/xfs_refcount_item.c
index 1a5a4c3fb913..f94ad3f557ed 100644
--- a/fs/xfs/xfs_refcount_item.c
+++ b/fs/xfs/xfs_refcount_item.c
@@ -855,18 +855,45 @@ xlog_recover_cud_commit_pass2(
struct xfs_cud_log_format *cud_formatp;
cud_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len != sizeof(struct xfs_cud_log_format)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ xlog_recover_release_intent(log, XFS_LI_CUI, cud_formatp->cud_cui_id);
+ return 0;
+}
+
+/*
+ * The cud is a single fixed size region. This is shared by the data and
+ * realtime cud items, which have identical formats.
+ */
+static int
+xlog_recover_cud_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_cud_log_format *cud_formatp =
+ item->ri_buf[region_index].iov_base;
+
+ if (item->ri_buf[region_index].iov_len !=
+ sizeof(struct xfs_cud_log_format)) {
+ xfs_warn(log->l_mp, "CUD: bad item region length %zu recovered.",
+ item->ri_buf[region_index].iov_len);
+ return -EFSCORRUPTED;
+ }
+
+ /* The padding is always written as zero. */
+ if (cud_formatp->__pad != 0) {
+ xfs_warn(log->l_mp, "CUD: bad padding 0x%x recovered.",
+ cud_formatp->__pad);
return -EFSCORRUPTED;
}
- xlog_recover_release_intent(log, XFS_LI_CUI, cud_formatp->cud_cui_id);
return 0;
}
const struct xlog_recover_item_ops xlog_cud_item_ops = {
.item_type = XFS_LI_CUD,
+ .min_hdr_len = sizeof(struct xfs_cud_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_cud_validate_region,
.commit_pass2 = xlog_recover_cud_commit_pass2,
};
@@ -881,12 +908,6 @@ xlog_recover_rtcud_commit_pass2(
struct xfs_cud_log_format *cud_formatp;
cud_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len != sizeof(struct xfs_cud_log_format)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
- }
-
xlog_recover_release_intent(log, XFS_LI_CUI_RT,
cud_formatp->cud_cui_id);
return 0;
@@ -897,5 +918,8 @@ xlog_recover_rtcud_commit_pass2(
const struct xlog_recover_item_ops xlog_rtcud_item_ops = {
.item_type = XFS_LI_CUD_RT,
+ .min_hdr_len = sizeof(struct xfs_cud_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_cud_validate_region,
.commit_pass2 = xlog_recover_rtcud_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 36/59] xfs: validate bmap update intents during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (34 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 35/59] xfs: validate refcount update done items " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 37/59] xfs: validate bmap update done items " Dave Chinner
` (22 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A bmap update intent is a single region item carrying one mapping operation
whose fork, owner and extent recovery must trust. These were only checked
during intent replay, after the item had been decoded and queued.
Validate the region length as it is decoded, and validate the mapping in
->validate_item(). xfs_bui_validate() now takes the format structure so it can
run against the decoded region. The commit handler and intent replay can then
trust the item, so the redundant checks are removed from them.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_bmap_item.c | 77 +++++++++++++++++++++++++++++-------------
1 file changed, 53 insertions(+), 24 deletions(-)
diff --git a/fs/xfs/xfs_bmap_item.c b/fs/xfs/xfs_bmap_item.c
index 62be62342fea..37ada25109c9 100644
--- a/fs/xfs/xfs_bmap_item.c
+++ b/fs/xfs/xfs_bmap_item.c
@@ -418,15 +418,15 @@ xfs_bmap_update_abort_intent(
static inline bool
xfs_bui_validate(
struct xfs_mount *mp,
- struct xfs_bui_log_item *buip)
+ struct xfs_bui_log_format *bui_formatp)
{
struct xfs_map_extent *map;
/* Only one mapping operation per BUI... */
- if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
+ if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
return false;
- map = &buip->bui_format.bui_extents[0];
+ map = &bui_formatp->bui_extents[0];
if (map->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
return false;
@@ -505,12 +505,7 @@ xfs_bmap_recover_work(
int iext_delta;
int error = 0;
- if (!xfs_bui_validate(mp, buip)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- &buip->bui_format, sizeof(buip->bui_format));
- return -EFSCORRUPTED;
- }
-
+ /* The bui was validated when it was recovered. */
map = &buip->bui_format.bui_extents[0];
work = xfs_bui_recover_work(mp, dfp, &ip, map);
if (IS_ERR(work))
@@ -650,40 +645,74 @@ xlog_recover_bui_commit_pass2(
struct xfs_mount *mp = log->l_mp;
struct xfs_bui_log_item *buip;
struct xfs_bui_log_format *bui_formatp;
- size_t len;
bui_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len < xfs_bui_log_format_sizeof(0)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ buip = xfs_bui_init(mp);
+ xfs_bui_copy_format(&buip->bui_format, bui_formatp);
+ atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
+
+ xlog_recover_intent_item(log, &buip->bui_item, lsn,
+ &xfs_bmap_update_defer_type);
+ return 0;
+}
+
+/*
+ * The bui is a single region with exactly one mapping operation, so its length
+ * must match a single extent.
+ */
+static int
+xlog_recover_bui_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_bui_log_format *bui_formatp =
+ item->ri_buf[region_index].iov_base;
+ size_t len = item->ri_buf[region_index].iov_len;
+
+ if (len < xfs_bui_log_format_sizeof(0)) {
+ xfs_warn(log->l_mp, "BUI: bad item region length %zu recovered.",
+ len);
return -EFSCORRUPTED;
}
if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ xfs_warn(log->l_mp, "BUI: bad extent count %u recovered.",
+ bui_formatp->bui_nextents);
return -EFSCORRUPTED;
}
- len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
- if (item->ri_buf[0].iov_len != len) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ if (len != xfs_bui_log_format_sizeof(bui_formatp->bui_nextents)) {
+ xfs_warn(log->l_mp,
+ "BUI: item region length %zu does not match %u extents recovered.",
+ len, bui_formatp->bui_nextents);
return -EFSCORRUPTED;
}
- buip = xfs_bui_init(mp);
- xfs_bui_copy_format(&buip->bui_format, bui_formatp);
- atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
+ return 0;
+}
+
+/* Validate the bmap update mapping in the bui. */
+static int
+xlog_recover_bui_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ if (!xfs_bui_validate(log->l_mp, item->ri_buf[0].iov_base)) {
+ xfs_warn(log->l_mp, "BUI: bad mapping recovered.");
+ return -EFSCORRUPTED;
+ }
- xlog_recover_intent_item(log, &buip->bui_item, lsn,
- &xfs_bmap_update_defer_type);
return 0;
}
const struct xlog_recover_item_ops xlog_bui_item_ops = {
.item_type = XFS_LI_BUI,
+ .min_hdr_len = sizeof(struct xfs_bui_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_bui_validate_region,
+ .validate_item = xlog_recover_bui_validate_item,
.commit_pass2 = xlog_recover_bui_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 37/59] xfs: validate bmap update done items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (35 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 36/59] xfs: validate bmap update intents " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 38/59] xfs: validate mapping exchange intents " Dave Chinner
` (21 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A bmap update done item is a single fixed size region, but recovery only
checked its length in the commit handler and never checked its padding.
Validate the region length and require the padding to be zero as the item is
decoded. The commit handler can then trust the item, so the length check is
removed from it.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_bmap_item.c | 32 ++++++++++++++++++++++++++++----
1 file changed, 28 insertions(+), 4 deletions(-)
diff --git a/fs/xfs/xfs_bmap_item.c b/fs/xfs/xfs_bmap_item.c
index 37ada25109c9..d2205335a08f 100644
--- a/fs/xfs/xfs_bmap_item.c
+++ b/fs/xfs/xfs_bmap_item.c
@@ -733,17 +733,41 @@ xlog_recover_bud_commit_pass2(
struct xfs_bud_log_format *bud_formatp;
bud_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len != sizeof(struct xfs_bud_log_format)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
+ return 0;
+}
+
+/* The bud is a single fixed size region. */
+static int
+xlog_recover_bud_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_bud_log_format *bud_formatp =
+ item->ri_buf[region_index].iov_base;
+
+ if (item->ri_buf[region_index].iov_len !=
+ sizeof(struct xfs_bud_log_format)) {
+ xfs_warn(log->l_mp, "BUD: bad item region length %zu recovered.",
+ item->ri_buf[region_index].iov_len);
+ return -EFSCORRUPTED;
+ }
+
+ /* The padding is always written as zero. */
+ if (bud_formatp->__pad != 0) {
+ xfs_warn(log->l_mp, "BUD: bad padding 0x%x recovered.",
+ bud_formatp->__pad);
return -EFSCORRUPTED;
}
- xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
return 0;
}
const struct xlog_recover_item_ops xlog_bud_item_ops = {
.item_type = XFS_LI_BUD,
+ .min_hdr_len = sizeof(struct xfs_bud_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_bud_validate_region,
.commit_pass2 = xlog_recover_bud_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 38/59] xfs: validate mapping exchange intents during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (36 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 37/59] xfs: validate bmap update done items " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 39/59] xfs: validate mapping exchange done items " Dave Chinner
` (20 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A mapping exchange intent is a single fixed size region describing an exchange
between two files, but its contents were only checked during intent replay,
after the item had been decoded and queued.
Validate the region length as it is decoded, and validate the exchange
parameters in ->validate_item(). xfs_xmi_validate() now takes the format
structure so it can run against the decoded region. The commit handler and
intent replay can then trust the item, so the redundant checks are removed from
them.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_exchmaps_item.c | 64 +++++++++++++++++++++++---------------
1 file changed, 39 insertions(+), 25 deletions(-)
diff --git a/fs/xfs/xfs_exchmaps_item.c b/fs/xfs/xfs_exchmaps_item.c
index 23381cb264c4..973df8ded795 100644
--- a/fs/xfs/xfs_exchmaps_item.c
+++ b/fs/xfs/xfs_exchmaps_item.c
@@ -324,14 +324,12 @@ xfs_exchmaps_abort_intent(
static inline bool
xfs_xmi_validate(
struct xfs_mount *mp,
- struct xfs_xmi_log_item *xmi_lip)
+ struct xfs_xmi_log_format *xlf)
{
- struct xfs_xmi_log_format *xlf = &xmi_lip->xmi_format;
-
if (!xfs_has_exchange_range(mp))
return false;
- if (xmi_lip->xmi_format.__pad != 0)
+ if (xlf->__pad != 0)
return false;
if (xlf->xmi_flags & ~XFS_EXCHMAPS_LOGGED_FLAGS)
@@ -447,13 +445,7 @@ xfs_exchmaps_recover_work(
struct xfs_inode *ip1, *ip2;
int error = 0;
- if (!xfs_xmi_validate(mp, xmi_lip)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- &xmi_lip->xmi_format,
- sizeof(xmi_lip->xmi_format));
- return -EFSCORRUPTED;
- }
-
+ /* The xmi was validated when it was recovered. */
xmi = xfs_xmi_item_recover_intent(mp, dfp, &xmi_lip->xmi_format, &req,
&ip1, &ip2);
if (IS_ERR(xmi))
@@ -569,32 +561,54 @@ xlog_recover_xmi_commit_pass2(
struct xfs_mount *mp = log->l_mp;
struct xfs_xmi_log_item *xmi_lip;
struct xfs_xmi_log_format *xmi_formatp;
- size_t len;
-
- len = sizeof(struct xfs_xmi_log_format);
- if (item->ri_buf[0].iov_len != len) {
- XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW,
- -EFSCORRUPTED, log->l_mp);
- return -EFSCORRUPTED;
- }
xmi_formatp = item->ri_buf[0].iov_base;
- if (xmi_formatp->__pad != 0) {
- XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW,
- -EFSCORRUPTED, log->l_mp);
- return -EFSCORRUPTED;
- }
xmi_lip = xfs_xmi_init(mp);
- memcpy(&xmi_lip->xmi_format, xmi_formatp, len);
+ memcpy(&xmi_lip->xmi_format, xmi_formatp,
+ sizeof(struct xfs_xmi_log_format));
xlog_recover_intent_item(log, &xmi_lip->xmi_item, lsn,
&xfs_exchmaps_defer_type);
return 0;
}
+/* The xmi is a single fixed size region. */
+static int
+xlog_recover_xmi_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ if (item->ri_buf[region_index].iov_len !=
+ sizeof(struct xfs_xmi_log_format)) {
+ xfs_warn(log->l_mp, "XMI: bad item region length %zu recovered.",
+ item->ri_buf[region_index].iov_len);
+ return -EFSCORRUPTED;
+ }
+ return 0;
+}
+
+/* Validate the mapping exchange parameters in the xmi. */
+static int
+xlog_recover_xmi_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ if (!xfs_xmi_validate(log->l_mp, item->ri_buf[0].iov_base)) {
+ xfs_warn(log->l_mp, "XMI: bad exchange parameters recovered.");
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
const struct xlog_recover_item_ops xlog_xmi_item_ops = {
.item_type = XFS_LI_XMI,
+ .min_hdr_len = sizeof(struct xfs_xmi_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_xmi_validate_region,
+ .validate_item = xlog_recover_xmi_validate_item,
.commit_pass2 = xlog_recover_xmi_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 39/59] xfs: validate mapping exchange done items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (37 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 38/59] xfs: validate mapping exchange intents " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 40/59] xfs: validate dquot " Dave Chinner
` (19 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A mapping exchange done item is a single fixed size region, but recovery only
checked its length in the commit handler and never checked its padding.
Validate the region length and require the padding to be zero as the item is
decoded. The commit handler can then trust the item, so the length check is
removed from it.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_exchmaps_item.c | 32 ++++++++++++++++++++++++++++----
1 file changed, 28 insertions(+), 4 deletions(-)
diff --git a/fs/xfs/xfs_exchmaps_item.c b/fs/xfs/xfs_exchmaps_item.c
index 973df8ded795..02e57aee1fa3 100644
--- a/fs/xfs/xfs_exchmaps_item.c
+++ b/fs/xfs/xfs_exchmaps_item.c
@@ -629,17 +629,41 @@ xlog_recover_xmd_commit_pass2(
struct xfs_xmd_log_format *xmd_formatp;
xmd_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len != sizeof(struct xfs_xmd_log_format)) {
- XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW,
- -EFSCORRUPTED, log->l_mp);
+ xlog_recover_release_intent(log, XFS_LI_XMI, xmd_formatp->xmd_xmi_id);
+ return 0;
+}
+
+/* The xmd is a single fixed size region. */
+static int
+xlog_recover_xmd_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_xmd_log_format *xmd_formatp =
+ item->ri_buf[region_index].iov_base;
+
+ if (item->ri_buf[region_index].iov_len !=
+ sizeof(struct xfs_xmd_log_format)) {
+ xfs_warn(log->l_mp, "XMD: bad item region length %zu recovered.",
+ item->ri_buf[region_index].iov_len);
+ return -EFSCORRUPTED;
+ }
+
+ /* The padding is always written as zero. */
+ if (xmd_formatp->__pad != 0) {
+ xfs_warn(log->l_mp, "XMD: bad padding 0x%x recovered.",
+ xmd_formatp->__pad);
return -EFSCORRUPTED;
}
- xlog_recover_release_intent(log, XFS_LI_XMI, xmd_formatp->xmd_xmi_id);
return 0;
}
const struct xlog_recover_item_ops xlog_xmd_item_ops = {
.item_type = XFS_LI_XMD,
+ .min_hdr_len = sizeof(struct xfs_xmd_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_xmd_validate_region,
.commit_pass2 = xlog_recover_xmd_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 40/59] xfs: validate dquot items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (38 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 39/59] xfs: validate mapping exchange done items " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 41/59] xfs: validate attr done " Dave Chinner
` (18 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A dquot item is a format header region followed by the dquot record region.
Recovery validated the dquot record with xfs_dquot_verify() but never checked
the log format header, and only loosely checked the record region size, all
during commit rather than as the item was decoded. The buffer address and
offset in the format header were used to read a buffer without any bounds
checking.
Validate the region layout and the whole item up front as it is decoded, so
commit and readahead can trust the item. The location of a dquot within its
cluster buffer is fixed by the dquot ID, so the logged offset must match the
location derived from the ID. The redundant checks are removed from commit and
readahead.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_dquot_item_recover.c | 119 ++++++++++++++++++++++++++------
1 file changed, 97 insertions(+), 22 deletions(-)
diff --git a/fs/xfs/xfs_dquot_item_recover.c b/fs/xfs/xfs_dquot_item_recover.c
index 1291e50d3031..b0be570c4842 100644
--- a/fs/xfs/xfs_dquot_item_recover.c
+++ b/fs/xfs/xfs_dquot_item_recover.c
@@ -35,11 +35,6 @@ xlog_recover_dquot_ra_pass2(
return;
recddq = item->ri_buf[1].iov_base;
- if (recddq == NULL)
- return;
- if (item->ri_buf[1].iov_len < sizeof(struct xfs_disk_dquot))
- return;
-
type = recddq->d_type & XFS_DQTYPE_REC_MASK;
ASSERT(type);
if (log->l_quotaoffs_flag & type)
@@ -80,15 +75,6 @@ xlog_recover_dquot_commit_pass2(
return 0;
recddq = item->ri_buf[1].iov_base;
- if (recddq == NULL) {
- xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
- return -EFSCORRUPTED;
- }
- if (item->ri_buf[1].iov_len < sizeof(struct xfs_disk_dquot)) {
- xfs_alert(log->l_mp, "dquot too small (%zd) in %s.",
- item->ri_buf[1].iov_len, __func__);
- return -EFSCORRUPTED;
- }
/*
* This type of quotas was turned off, so ignore this record.
@@ -109,14 +95,6 @@ xlog_recover_dquot_commit_pass2(
* removed since the last mount - ENOSYS.
*/
dq_f = item->ri_buf[0].iov_base;
- ASSERT(dq_f);
- fa = xfs_dquot_verify(mp, recddq, dq_f->qlf_id);
- if (fa) {
- xfs_alert(mp, "corrupt dquot ID 0x%x in log at %pS",
- dq_f->qlf_id, fa);
- return -EFSCORRUPTED;
- }
- ASSERT(dq_f->qlf_len == 1);
/*
* At this point we are assuming that the dquots have been allocated
@@ -175,8 +153,105 @@ xlog_recover_dquot_commit_pass2(
return error;
}
+/* A dquot item is a format header region followed by the dquot record. */
+static int
+xlog_recover_dquot_nregions(
+ struct xlog *log,
+ uint16_t nregions)
+{
+ if (nregions != 2)
+ return -EFSCORRUPTED;
+ return 2;
+}
+
+static int
+xlog_recover_dquot_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ size_t len = item->ri_buf[region_index].iov_len;
+ size_t expected;
+
+ if (region_index == 0)
+ expected = sizeof(struct xfs_dq_logformat);
+ else
+ expected = sizeof(struct xfs_disk_dquot);
+
+ if (len != expected) {
+ xfs_warn(log->l_mp,
+ "DQUOT: bad region %d length %zu recovered.",
+ region_index, len);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
+static int
+xlog_recover_dquot_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_mount *mp = log->l_mp;
+ struct xfs_dq_logformat *dq_f = item->ri_buf[0].iov_base;
+ struct xfs_disk_dquot *ddq = item->ri_buf[1].iov_base;
+ xfs_failaddr_t fa;
+ unsigned int ndquots;
+ unsigned int start;
+ unsigned int end;
+
+ if (dq_f->qlf_len != XFS_DQUOT_CLUSTER_SIZE_FSB) {
+ xfs_warn(mp, "DQUOT: bad buffer length %d recovered.",
+ dq_f->qlf_len);
+ return -EFSCORRUPTED;
+ }
+
+ /*
+ * There is a fixed relationship between the dquot ID and its location
+ * in the cluster buffer. These must match and the dqblk must still fit
+ * within the dquot cluster buffer size.
+ */
+ ndquots = xfs_calc_dquots_per_chunk(
+ XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB));
+ start = (dq_f->qlf_id % ndquots) * sizeof(struct xfs_dqblk);
+ end = start + sizeof(struct xfs_dqblk);
+ if (dq_f->qlf_boffset != start) {
+ xfs_warn(mp,
+ "DQUOT: dquot id 0x%x bad buffer offset %u, expected %u recovered.",
+ dq_f->qlf_id, dq_f->qlf_boffset, start);
+ return -EFSCORRUPTED;
+ }
+ if (end > XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB)) {
+ xfs_warn(mp,
+ "DQUOT: dquot id 0x%x at offset %u overruns buffer size %lld recovered.",
+ dq_f->qlf_id, start,
+ XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB));
+ return -EFSCORRUPTED;
+ }
+
+ if (!xfs_verify_fsbno(mp, XFS_DADDR_TO_FSB(mp, dq_f->qlf_blkno))) {
+ xfs_warn(mp, "DQUOT: bad buffer block 0x%llx recovered.",
+ dq_f->qlf_blkno);
+ return -EFSCORRUPTED;
+ }
+
+ fa = xfs_dquot_verify(mp, ddq, dq_f->qlf_id);
+ if (fa) {
+ xfs_warn(mp, "DQUOT: dquot id 0x%x failed validation at %pS",
+ dq_f->qlf_id, fa);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
const struct xlog_recover_item_ops xlog_dquot_item_ops = {
.item_type = XFS_LI_DQUOT,
+ .min_hdr_len = sizeof(struct xfs_dq_logformat),
+ .validate_nregions = xlog_recover_dquot_nregions,
+ .validate_region = xlog_recover_dquot_validate_region,
+ .validate_item = xlog_recover_dquot_validate_item,
.ra_pass2 = xlog_recover_dquot_ra_pass2,
.commit_pass2 = xlog_recover_dquot_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 41/59] xfs: validate attr done items during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (39 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 40/59] xfs: validate dquot " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:14 ` [PATCH 42/59] xfs: validate attr intents " Dave Chinner
` (17 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
An attr done item is a single fixed size region, but recovery only checked its
length in the commit handler and never checked its padding.
Validate the region length and require the padding to be zero as the item is
decoded. The commit handler can then trust the item, so the length check is
removed from it.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_attr_item.c | 34 +++++++++++++++++++++++++++++-----
1 file changed, 29 insertions(+), 5 deletions(-)
diff --git a/fs/xfs/xfs_attr_item.c b/fs/xfs/xfs_attr_item.c
index a53ee2b0c54a..a6e36b2aa7b3 100644
--- a/fs/xfs/xfs_attr_item.c
+++ b/fs/xfs/xfs_attr_item.c
@@ -1167,14 +1167,35 @@ xlog_recover_attrd_commit_pass2(
struct xfs_attrd_log_format *attrd_formatp;
attrd_formatp = item->ri_buf[0].iov_base;
- if (item->ri_buf[0].iov_len != sizeof(struct xfs_attrd_log_format)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
+ xlog_recover_release_intent(log, XFS_LI_ATTRI,
+ attrd_formatp->alfd_alf_id);
+ return 0;
+}
+
+/* The attrd is a single fixed size region. */
+static int
+xlog_recover_attrd_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ struct xfs_attrd_log_format *attrd_formatp =
+ item->ri_buf[region_index].iov_base;
+
+ if (item->ri_buf[region_index].iov_len !=
+ sizeof(struct xfs_attrd_log_format)) {
+ xfs_warn(log->l_mp, "ATTRD: bad item region length %zu recovered.",
+ item->ri_buf[region_index].iov_len);
+ return -EFSCORRUPTED;
+ }
+
+ /* The padding is always written as zero. */
+ if (attrd_formatp->__pad != 0) {
+ xfs_warn(log->l_mp, "ATTRD: bad padding 0x%x recovered.",
+ attrd_formatp->__pad);
return -EFSCORRUPTED;
}
- xlog_recover_release_intent(log, XFS_LI_ATTRI,
- attrd_formatp->alfd_alf_id);
return 0;
}
@@ -1203,5 +1224,8 @@ static const struct xfs_item_ops xfs_attrd_item_ops = {
const struct xlog_recover_item_ops xlog_attrd_item_ops = {
.item_type = XFS_LI_ATTRD,
+ .min_hdr_len = sizeof(struct xfs_attrd_log_format),
+ .validate_nregions = xlog_recover_single_region,
+ .validate_region = xlog_recover_attrd_validate_region,
.commit_pass2 = xlog_recover_attrd_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 42/59] xfs: validate attr intents during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (40 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 41/59] xfs: validate attr done " Dave Chinner
@ 2026-11-16 23:14 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 43/59] xfs: convert inode items to host format " Dave Chinner
` (16 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:14 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
An attr intent is a format header region followed by up to four name and value
regions, the number and contents of which depend on the operation it describes.
Recovery validated all of this inline in the commit handler, interleaved with
building the in-core item, so the item was validated only as it was being set
up for replay.
Validate the attr intent as it is decoded instead. Region lengths are bounded
as the regions are assembled. Exact region length validation is done during
item validation. The commit handler is then left to do only the replay setup:
pull the validated name and value regions out of the item and build the in-core
intent from them.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_attr_item.c | 249 +++++++++++++++++++++++++----------------
1 file changed, 151 insertions(+), 98 deletions(-)
diff --git a/fs/xfs/xfs_attr_item.c b/fs/xfs/xfs_attr_item.c
index a6e36b2aa7b3..cd0225957c6d 100644
--- a/fs/xfs/xfs_attr_item.c
+++ b/fs/xfs/xfs_attr_item.c
@@ -1002,151 +1002,200 @@ xlog_recover_attri_commit_pass2(
{
struct xfs_mount *mp = log->l_mp;
struct xfs_attri_log_item *attrip;
- struct xfs_attri_log_format *attri_formatp;
+ struct xfs_attri_log_format *attri_formatp = item->ri_buf[0].iov_base;
struct xfs_attri_log_nameval *nv;
const void *attr_name;
const void *attr_value = NULL;
const void *attr_new_name = NULL;
const void *attr_new_value = NULL;
- size_t len;
unsigned int name_len = 0;
unsigned int value_len = 0;
unsigned int new_name_len = 0;
unsigned int new_value_len = 0;
- unsigned int op, i = 0;
+ unsigned int i = 1;
- /* Validate xfs_attri_log_format before the large memory allocation */
- len = sizeof(struct xfs_attri_log_format);
- if (item->ri_buf[i].iov_len != len) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
- return -EFSCORRUPTED;
+ /*
+ * The item has already been validated, so pull the name and value
+ * lengths out of the format header. The regions present depend on the
+ * operation being performed.
+ */
+ switch (xfs_attr_log_item_op(attri_formatp)) {
+ case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
+ case XFS_ATTRI_OP_FLAGS_PPTR_SET:
+ case XFS_ATTRI_OP_FLAGS_SET:
+ case XFS_ATTRI_OP_FLAGS_REPLACE:
+ name_len = attri_formatp->alfi_name_len;
+ value_len = attri_formatp->alfi_value_len;
+ break;
+ case XFS_ATTRI_OP_FLAGS_REMOVE:
+ name_len = attri_formatp->alfi_name_len;
+ break;
+ case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
+ name_len = attri_formatp->alfi_old_name_len;
+ new_name_len = attri_formatp->alfi_new_name_len;
+ new_value_len = value_len = attri_formatp->alfi_value_len;
+ break;
}
- attri_formatp = item->ri_buf[i].iov_base;
- if (!xfs_attri_validate(mp, attri_formatp)) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- attri_formatp, len);
+ /* Pull the name and value regions out in the order they are logged. */
+ attr_name = item->ri_buf[i++].iov_base;
+ if (new_name_len > 0)
+ attr_new_name = item->ri_buf[i++].iov_base;
+ if (value_len > 0)
+ attr_value = item->ri_buf[i++].iov_base;
+ if (new_value_len > 0)
+ attr_new_value = item->ri_buf[i++].iov_base;
+
+ /*
+ * Memory alloc failure will cause replay to abort. We attach the
+ * name/value buffer to the recovered incore log item and drop our
+ * reference.
+ */
+ nv = xfs_attri_log_nameval_alloc(attr_name, name_len,
+ attr_new_name, new_name_len,
+ attr_value, value_len,
+ attr_new_value, new_value_len);
+
+ attrip = xfs_attri_init(mp, nv);
+ memcpy(&attrip->attri_format, attri_formatp,
+ sizeof(struct xfs_attri_log_format));
+
+ xlog_recover_intent_item(log, &attrip->attri_item, lsn,
+ &xfs_attr_defer_type);
+ xfs_attri_log_nameval_put(nv);
+ return 0;
+}
+
+/*
+ * An attri has a format header region followed by up to four name and value
+ * regions, the exact number depending on the operation it describes. The exact
+ * count for the operation is checked once the format header is decoded in
+ * ->validate_item(); here we only bound the count to the possible range.
+ */
+static int
+xlog_recover_attri_nregions(
+ struct xlog *log,
+ uint16_t nregions)
+{
+ if (nregions < 2 || nregions > 5)
return -EFSCORRUPTED;
+ return nregions;
+}
+
+/*
+ * Bound each region by its largest possible size. Region 0 is the fixed size
+ * format header. Region 1 is always a name, so no larger than a maximum length
+ * attr name. Regions 2 to 4 may be a name or a value, so no larger than a
+ * maximum length attr value. This detects an unbounded continuation of a region
+ * before the exact per-operation sizes are checked in ->validate_item().
+ */
+static int
+xlog_recover_attri_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ size_t len = item->ri_buf[region_index].iov_len;
+
+ if (region_index == 0) {
+ if (len != sizeof(struct xfs_attri_log_format))
+ goto out_error;
+ return 0;
}
- /* Check the number of log iovecs makes sense for the op code. */
- op = xfs_attr_log_item_op(attri_formatp);
- switch (op) {
+ if (region_index == 1) {
+ if (len > xlog_calc_iovec_len(XATTR_NAME_MAX))
+ goto out_error;
+ return 0;
+ }
+
+ if (len > xlog_calc_iovec_len(XATTR_SIZE_MAX))
+ goto out_error;
+ return 0;
+
+out_error:
+ xfs_warn(log->l_mp, "ATTRI: bad region %d length %zu recovered.",
+ region_index, len);
+ return -EFSCORRUPTED;
+}
+
+/*
+ * Validate the whole attri: the format header, the exact region count for the
+ * operation it describes, and the name and value regions that operation
+ * requires. Once this passes, the rest of recovery can walk the regions without
+ * checking them.
+ */
+static int
+xlog_recover_attri_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_mount *mp = log->l_mp;
+ struct xfs_attri_log_format *attri_formatp = item->ri_buf[0].iov_base;
+ unsigned int name_len = 0;
+ unsigned int value_len = 0;
+ unsigned int new_name_len = 0;
+ unsigned int new_value_len = 0;
+ unsigned int i = 1;
+
+ if (!xfs_attri_validate(mp, attri_formatp))
+ return -EFSCORRUPTED;
+
+ /* Check the number of regions makes sense for the op code. */
+ switch (xfs_attr_log_item_op(attri_formatp)) {
case XFS_ATTRI_OP_FLAGS_PPTR_REMOVE:
case XFS_ATTRI_OP_FLAGS_PPTR_SET:
- /* Log item, attr name, attr value */
- if (item->ri_total != 3) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- attri_formatp, len);
+ /* Format header, attr name, attr value */
+ if (item->ri_total != 3)
return -EFSCORRUPTED;
- }
name_len = attri_formatp->alfi_name_len;
value_len = attri_formatp->alfi_value_len;
break;
case XFS_ATTRI_OP_FLAGS_SET:
case XFS_ATTRI_OP_FLAGS_REPLACE:
- /* Log item, attr name, optional attr value */
- if (item->ri_total != 2 + !!attri_formatp->alfi_value_len) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- attri_formatp, len);
+ /* Format header, attr name, optional attr value */
+ if (item->ri_total != (2 + !!attri_formatp->alfi_value_len))
return -EFSCORRUPTED;
- }
name_len = attri_formatp->alfi_name_len;
value_len = attri_formatp->alfi_value_len;
break;
case XFS_ATTRI_OP_FLAGS_REMOVE:
- /* Log item, attr name */
- if (item->ri_total != 2) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- attri_formatp, len);
+ /* Format header, attr name */
+ if (item->ri_total != 2)
return -EFSCORRUPTED;
- }
name_len = attri_formatp->alfi_name_len;
break;
case XFS_ATTRI_OP_FLAGS_PPTR_REPLACE:
- /*
- * Log item, attr name, new attr name, attr value, new attr
- * value
- */
- if (item->ri_total != 5) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- attri_formatp, len);
+ /* Format header, attr name, new name, attr value, new value */
+ if (item->ri_total != 5)
return -EFSCORRUPTED;
- }
name_len = attri_formatp->alfi_old_name_len;
new_name_len = attri_formatp->alfi_new_name_len;
new_value_len = value_len = attri_formatp->alfi_value_len;
break;
default:
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- attri_formatp, len);
return -EFSCORRUPTED;
}
- i++;
- /* Validate the attr name */
- attr_name = xfs_attri_validate_name_iovec(mp, attri_formatp,
- &item->ri_buf[i], name_len);
- if (!attr_name)
+ if (!xfs_attri_validate_name_iovec(mp, attri_formatp, &item->ri_buf[i++],
+ name_len))
return -EFSCORRUPTED;
- i++;
-
- /* Validate the new attr name */
- if (new_name_len > 0) {
- attr_new_name = xfs_attri_validate_name_iovec(mp,
- attri_formatp, &item->ri_buf[i],
- new_name_len);
- if (!attr_new_name)
- return -EFSCORRUPTED;
- i++;
- }
-
- /* Validate the attr value, if present */
- if (value_len != 0) {
- attr_value = xfs_attri_validate_value_iovec(mp, attri_formatp,
- &item->ri_buf[i], value_len);
- if (!attr_value)
- return -EFSCORRUPTED;
- i++;
- }
-
- /* Validate the new attr value, if present */
- if (new_value_len != 0) {
- attr_new_value = xfs_attri_validate_value_iovec(mp,
- attri_formatp, &item->ri_buf[i],
- new_value_len);
- if (!attr_new_value)
- return -EFSCORRUPTED;
- i++;
- }
- /*
- * Make sure we got the correct number of buffers for the operation
- * that we just loaded.
- */
- if (i != item->ri_total) {
- XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
- attri_formatp, len);
+ if (new_name_len > 0 &&
+ !xfs_attri_validate_name_iovec(mp, attri_formatp, &item->ri_buf[i++],
+ new_name_len))
return -EFSCORRUPTED;
- }
- /*
- * Memory alloc failure will cause replay to abort. We attach the
- * name/value buffer to the recovered incore log item and drop our
- * reference.
- */
- nv = xfs_attri_log_nameval_alloc(attr_name, name_len,
- attr_new_name, new_name_len,
- attr_value, value_len,
- attr_new_value, new_value_len);
+ if (value_len > 0 &&
+ !xfs_attri_validate_value_iovec(mp, attri_formatp, &item->ri_buf[i++],
+ value_len))
+ return -EFSCORRUPTED;
- attrip = xfs_attri_init(mp, nv);
- memcpy(&attrip->attri_format, attri_formatp, len);
+ if (new_value_len > 0 &&
+ !xfs_attri_validate_value_iovec(mp, attri_formatp, &item->ri_buf[i++],
+ new_value_len))
+ return -EFSCORRUPTED;
- xlog_recover_intent_item(log, &attrip->attri_item, lsn,
- &xfs_attr_defer_type);
- xfs_attri_log_nameval_put(nv);
return 0;
}
@@ -1210,6 +1259,10 @@ static const struct xfs_item_ops xfs_attri_item_ops = {
const struct xlog_recover_item_ops xlog_attri_item_ops = {
.item_type = XFS_LI_ATTRI,
+ .min_hdr_len = sizeof(struct xfs_attri_log_format),
+ .validate_nregions = xlog_recover_attri_nregions,
+ .validate_region = xlog_recover_attri_validate_region,
+ .validate_item = xlog_recover_attri_validate_item,
.commit_pass2 = xlog_recover_attri_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 43/59] xfs: convert inode items to host format during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (41 preceding siblings ...)
2026-11-16 23:14 ` [PATCH 42/59] xfs: validate attr intents " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 44/59] xfs: allow xfs_imap() to map without an inode btree lookup Dave Chinner
` (15 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The inode item format header is logged in either the host format or the older
32 bit on-disk format. Recovery converted between them in the commit handler
and carried a separately allocated copy through the rest of the function.
Convert the format header to host format in ->validate_item() as the item is
decoded, so the rest of recovery treats region 0 as a host format header
without regard for the on-disk format. The commit handler and readahead now use
the header directly. The separate allocation and its cleanup are removed.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_inode_item_recover.c | 108 +++++++++++++++++++++++---------
1 file changed, 79 insertions(+), 29 deletions(-)
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index 8da95131ab55..2c7a5627158b 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -30,17 +30,10 @@ xlog_recover_inode_ra_pass2(
struct xlog *log,
struct xlog_recover_item *item)
{
- if (item->ri_buf[0].iov_len == sizeof(struct xfs_inode_log_format)) {
- struct xfs_inode_log_format *ilfp = item->ri_buf[0].iov_base;
+ struct xfs_inode_log_format *ilfp = item->ri_buf[0].iov_base;
- xlog_buf_readahead(log, ilfp->ilf_blkno, ilfp->ilf_len,
- &xfs_inode_buf_ra_ops);
- } else {
- struct xfs_inode_log_format_32 *ilfp = item->ri_buf[0].iov_base;
-
- xlog_buf_readahead(log, ilfp->ilf_blkno, ilfp->ilf_len,
- &xfs_inode_buf_ra_ops);
- }
+ xlog_buf_readahead(log, ilfp->ilf_blkno, ilfp->ilf_len,
+ &xfs_inode_buf_ra_ops);
}
/*
@@ -304,6 +297,75 @@ xlog_recover_inode_dbroot(
return 0;
}
+/*
+ * An inode item is a format header region, the log dinode core, and up to two
+ * fork regions, so it has two to four regions. The exact count depends on the
+ * fields logged and is checked once the format header is decoded.
+ */
+static int
+xlog_recover_inode_nregions(
+ struct xlog *log,
+ uint16_t nregions)
+{
+ if (nregions < 2 || nregions > 4)
+ return -EFSCORRUPTED;
+ return nregions;
+}
+
+/*
+ * The format header (region 0) is logged in either the host format or the 32
+ * bit on-disk format, so it must match one of those two sizes.
+ */
+static int
+xlog_recover_inode_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ size_t len = item->ri_buf[region_index].iov_len;
+
+ if (region_index > 0)
+ return 0;
+
+ if (len != sizeof(struct xfs_inode_log_format) &&
+ len != sizeof(struct xfs_inode_log_format_32)) {
+ xfs_warn(log->l_mp,
+ "INODE: bad format region length %zu recovered.", len);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
+/*
+ * Convert the format header to host format so the rest of recovery can treat
+ * region 0 as a struct xfs_inode_log_format without regard for the on-disk
+ * format. A header already in host format is left in place.
+ */
+static int
+xlog_recover_inode_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_inode_log_format *in_f;
+ int error;
+
+ if (item->ri_buf[0].iov_len == sizeof(struct xfs_inode_log_format))
+ return 0;
+
+ in_f = kzalloc_obj(struct xfs_inode_log_format, GFP_KERNEL | __GFP_NOFAIL);
+ error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
+ if (error) {
+ kfree(in_f);
+ return error;
+ }
+
+ kvfree(item->ri_buf[0].iov_base);
+ item->ri_buf[0].iov_base = in_f;
+ item->ri_buf[0].iov_len = sizeof(struct xfs_inode_log_format);
+ return 0;
+}
+
STATIC int
xlog_recover_inode_commit_pass2(
struct xlog *log,
@@ -311,7 +373,7 @@ xlog_recover_inode_commit_pass2(
struct xlog_recover_item *item,
xfs_lsn_t current_lsn)
{
- struct xfs_inode_log_format *in_f;
+ struct xfs_inode_log_format *in_f = item->ri_buf[0].iov_base;
struct xfs_mount *mp = log->l_mp;
struct xfs_buf *bp;
struct xfs_dinode *dip;
@@ -323,35 +385,22 @@ xlog_recover_inode_commit_pass2(
uint fields;
struct xfs_log_dinode *ldip;
uint isize;
- int need_free = 0;
xfs_failaddr_t fa;
- if (item->ri_buf[0].iov_len == sizeof(struct xfs_inode_log_format)) {
- in_f = item->ri_buf[0].iov_base;
- } else {
- in_f = kmalloc_obj(struct xfs_inode_log_format,
- GFP_KERNEL | __GFP_NOFAIL);
- need_free = 1;
- error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
- if (error)
- goto error;
- }
-
/*
* Inode buffers can be freed, look out for it,
* and do not replay the inode.
*/
if (xlog_is_buffer_cancelled(log, in_f->ilf_blkno, in_f->ilf_len)) {
- error = 0;
trace_xfs_log_recover_inode_cancel(log, in_f);
- goto error;
+ return 0;
}
trace_xfs_log_recover_inode_recover(log, in_f);
error = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len,
0, &bp, &xfs_inode_buf_ops);
if (error)
- goto error;
+ return error;
ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
dip = xfs_buf_offset(bp, in_f->ilf_boffset);
@@ -608,14 +657,15 @@ xlog_recover_inode_commit_pass2(
out_release:
xfs_buf_relse(bp);
-error:
- if (need_free)
- kfree(in_f);
return error;
}
const struct xlog_recover_item_ops xlog_inode_item_ops = {
.item_type = XFS_LI_INODE,
+ .min_hdr_len = sizeof(struct xfs_inode_log_format_32),
+ .validate_nregions = xlog_recover_inode_nregions,
+ .validate_region = xlog_recover_inode_validate_region,
+ .validate_item = xlog_recover_inode_validate_item,
.ra_pass2 = xlog_recover_inode_ra_pass2,
.commit_pass2 = xlog_recover_inode_commit_pass2,
};
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 44/59] xfs: allow xfs_imap() to map without an inode btree lookup
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (42 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 43/59] xfs: convert inode items to host format " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 45/59] xfs: validate the inode item format header during recovery Dave Chinner
` (14 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
xfs_imap() maps an inode number to its cluster buffer location. For the common
inode cluster geometries it does this by arithmetic, but for the general case it
falls back to an inode btree lookup that reads the AGI and walks the inode
btree.
Some callers cannot allow that lookup. Log recovery in particular wants to
cross-check a logged inode buffer location against the one derived from the
inode number, but it runs before the filesystem is fully available and must not
issue btree lookups.
Add an XFS_IMAP_NO_LOOKUP flag. When it is set, xfs_imap() maps the inode by
arithmetic alone and returns -EAGAIN instead of performing the btree lookup, so
the caller can tell that arithmetic mapping was not possible for this geometry.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_ialloc.c | 7 +++++++
fs/xfs/xfs_icache.h | 2 ++
2 files changed, 9 insertions(+)
diff --git a/fs/xfs/libxfs/xfs_ialloc.c b/fs/xfs/libxfs/xfs_ialloc.c
index ec0a37499f28..f57435f490c4 100644
--- a/fs/xfs/libxfs/xfs_ialloc.c
+++ b/fs/xfs/libxfs/xfs_ialloc.c
@@ -2435,6 +2435,10 @@ xfs_imap_lookup(
/*
* Return the location of the inode in imap, for mapping it into a buffer.
+ *
+ * If the caller cannot allow a btree lookup to be performed, then it should set
+ * XFS_IMAP_NO_LOOKUP. If arithmetic mapping cannot be performed, we return
+ * -EAGAIN to inform the caller mapping failed due to a lookup being required.
*/
int
xfs_imap(
@@ -2522,6 +2526,9 @@ xfs_imap(
*/
}
+ if (flags & XFS_IMAP_NO_LOOKUP)
+ return -EAGAIN;
+
error = xfs_imap_lookup(pag, tp, agino, agbno, &chunk_agbno,
&offset_agbno, flags);
if (error)
diff --git a/fs/xfs/xfs_icache.h b/fs/xfs/xfs_icache.h
index 905944dafbe5..a1386b16edcd 100644
--- a/fs/xfs/xfs_icache.h
+++ b/fs/xfs/xfs_icache.h
@@ -41,6 +41,8 @@ struct xfs_icwalk {
#define XFS_IGET_INCORE (1U << 3)
/* Return -EAGAIN immediately if the inode is unavailable. */
#define XFS_IGET_NORETRY (1U << 4)
+/* Map the inode by arithmetic only, never by an inode btree lookup. */
+#define XFS_IMAP_NO_LOOKUP (1U << 5)
int xfs_iget(struct xfs_mount *mp, struct xfs_trans *tp, xfs_ino_t ino,
uint flags, uint lock_flags, xfs_inode_t **ipp);
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 45/59] xfs: validate the inode item format header during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (43 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 44/59] xfs: allow xfs_imap() to map without an inode btree lookup Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 46/59] xfs: validate the logged inode core " Dave Chinner
` (13 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The inode item format header describes how many regions the item has and where
the inode lives on disk. Recovery trusted these values, guarding only a few of
them with asserts in the commit handler while walking the regions and reading
the inode buffer.
Validate the format header as the item is decoded, so the rest of recovery can
trust the region count and buffer location it describes. The header is
self-consistent with the fields it logs, and the buffer location is fixed by the
inode number, so both are checked here.
The inode number fixes the block holding the inode and the inode's offset
within that block, so the logged buffer block and offset are always checked
against those, and the inode must lie within the cluster buffer. Where the
cluster starts relative to that block depends on the inode chunk alignment.
When xfs_imap() can resolve that without an inode btree lookup, the exact
cluster location is checked too. Recovery never issues an inode btree lookup.
The host format conversion is split into its own helper so validation always
runs against a host format header.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_inode_item_recover.c | 171 ++++++++++++++++++++++++++++++--
1 file changed, 164 insertions(+), 7 deletions(-)
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index 2c7a5627158b..98596f27fb06 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -21,6 +21,8 @@
#include "xfs_log_priv.h"
#include "xfs_log_recover.h"
#include "xfs_icache.h"
+#include "xfs_ialloc.h"
+#include "xfs_ag.h"
#include "xfs_bmap_btree.h"
#include "xfs_rtrmap_btree.h"
#include "xfs_rtrefcount_btree.h"
@@ -343,29 +345,184 @@ xlog_recover_inode_validate_region(
* format. A header already in host format is left in place.
*/
static int
-xlog_recover_inode_validate_item(
- struct xlog *log,
+xlog_recover_inode_convert_format(
struct xlog_recover_item *item)
{
- struct xfs_inode_log_format *in_f;
+ struct xfs_inode_log_format *ilfp;
int error;
if (item->ri_buf[0].iov_len == sizeof(struct xfs_inode_log_format))
return 0;
- in_f = kzalloc_obj(struct xfs_inode_log_format, GFP_KERNEL | __GFP_NOFAIL);
- error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
+ ilfp = kzalloc_obj(struct xfs_inode_log_format, GFP_KERNEL | __GFP_NOFAIL);
+ error = xfs_inode_item_format_convert(&item->ri_buf[0], ilfp);
if (error) {
- kfree(in_f);
+ kfree(ilfp);
return error;
}
kvfree(item->ri_buf[0].iov_base);
- item->ri_buf[0].iov_base = in_f;
+ item->ri_buf[0].iov_base = ilfp;
item->ri_buf[0].iov_len = sizeof(struct xfs_inode_log_format);
return 0;
}
+/*
+ * The inode number fixes the filesystem block holding the inode and the inode's
+ * offset within that block. The logged buffer offset and block must agree with
+ * those, and the inode must lie entirely within the cluster buffer.
+ *
+ * Where the cluster starts relative to the inode's block depends on the inode
+ * chunk alignment. For most geometries that is resolvable by arithmetic, and the
+ * exact location is checked. Otherwise it needs an inode btree lookup, which
+ * cannot be run this early in recovery, so the cluster boundary is not checked.
+ */
+static int
+xlog_recover_inode_check_location(
+ struct xlog *log,
+ struct xfs_inode_log_format *ilfp)
+{
+ struct xfs_mount *mp = log->l_mp;
+ xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ilfp->ilf_ino);
+ xfs_agblock_t agbno = XFS_AGINO_TO_AGBNO(mp,
+ XFS_INO_TO_AGINO(mp, ilfp->ilf_ino));
+ unsigned int ioffset = XFS_INO_TO_OFFSET(mp,
+ ilfp->ilf_ino) << mp->m_sb.sb_inodelog;
+ struct xfs_perag *pag;
+ struct xfs_imap imap;
+ xfs_daddr_t blkno;
+ unsigned int start;
+ unsigned int end;
+ int len;
+ int error;
+
+ len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
+ if (ilfp->ilf_len != len) {
+ xfs_warn(mp,
+ "INODE: inode 0x%llx buffer length 0x%x does not match expected 0x%x recovered.",
+ ilfp->ilf_ino, ilfp->ilf_len, len);
+ return -EFSCORRUPTED;
+ }
+
+ /* The inode must lie entirely within the cluster buffer. */
+ if (ilfp->ilf_boffset < 0) {
+ xfs_warn(mp,
+ "INODE: inode 0x%llx bad buffer offset %d recovered.",
+ ilfp->ilf_ino, ilfp->ilf_boffset);
+ return -EFSCORRUPTED;
+ }
+ start = ilfp->ilf_boffset;
+ end = start + mp->m_sb.sb_inodesize;
+ if (end > BBTOB(ilfp->ilf_len)) {
+ xfs_warn(mp,
+ "INODE: inode 0x%llx at buffer offset 0x%x overruns buffer size 0x%x recovered.",
+ ilfp->ilf_ino, start, BBTOB(ilfp->ilf_len));
+ return -EFSCORRUPTED;
+ }
+
+ /* The offset within the inode's block is fixed by the inode number. */
+ if ((start & mp->m_blockmask) != ioffset) {
+ xfs_warn(mp,
+ "INODE: inode 0x%llx buffer offset 0x%x does not match offset 0x%x in its block recovered.",
+ ilfp->ilf_ino, start, ioffset);
+ return -EFSCORRUPTED;
+ }
+
+ /*
+ * The block part of the buffer offset, counted back from the inode's
+ * block, must land on the start of the cluster buffer.
+ */
+ blkno = XFS_AGB_TO_DADDR(mp, agno, agbno) -
+ XFS_FSB_TO_BB(mp, start >> mp->m_sb.sb_blocklog);
+ if (ilfp->ilf_blkno != blkno) {
+ xfs_warn(mp,
+ "INODE: inode 0x%llx buffer block 0x%llx does not match expected 0x%llx recovered.",
+ ilfp->ilf_ino, ilfp->ilf_blkno, blkno);
+ return -EFSCORRUPTED;
+ }
+
+ pag = xfs_perag_get(mp, agno);
+ error = xfs_imap(pag, NULL, ilfp->ilf_ino, &imap, XFS_IMAP_NO_LOOKUP);
+ xfs_perag_put(pag);
+ if (error == -EAGAIN)
+ return 0;
+ if (error)
+ return error;
+
+ /* The geometry is resolvable, so check the exact cluster location. */
+ blkno = XFS_AGB_TO_DADDR(mp, agno, imap.im_agbno);
+ if (ilfp->ilf_blkno != blkno) {
+ xfs_warn(mp,
+ "INODE: inode 0x%llx buffer block 0x%llx does not match expected 0x%llx recovered.",
+ ilfp->ilf_ino, ilfp->ilf_blkno, blkno);
+ return -EFSCORRUPTED;
+ }
+ if (ilfp->ilf_boffset != imap.im_boffset) {
+ xfs_warn(mp,
+ "INODE: inode 0x%llx buffer offset 0x%x does not match expected 0x%x recovered.",
+ ilfp->ilf_ino, ilfp->ilf_boffset, imap.im_boffset);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
+/*
+ * Validate the format header once it is in host format. The item size records
+ * how many regions the item should have, the set of logged fields fixes what
+ * that count must be, and the inode number fixes the buffer location, so all of
+ * these are checked against the header here.
+ */
+static int
+xlog_recover_inode_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_inode_log_format *ilfp;
+ unsigned int nregions;
+ int error;
+
+ error = xlog_recover_inode_convert_format(item);
+ if (error)
+ return error;
+ ilfp = item->ri_buf[0].iov_base;
+
+ if (!xfs_verify_ino(log->l_mp, ilfp->ilf_ino)) {
+ xfs_warn(log->l_mp, "INODE: bad inode number 0x%llx recovered.",
+ ilfp->ilf_ino);
+ return -EFSCORRUPTED;
+ }
+
+ if (!(ilfp->ilf_fields & XFS_ILOG_CORE)) {
+ xfs_warn(log->l_mp,
+ "INODE: inode 0x%llx has no core logged, fields 0x%x recovered.",
+ ilfp->ilf_ino, ilfp->ilf_fields);
+ return -EFSCORRUPTED;
+ }
+
+ if (ilfp->ilf_size != item->ri_total) {
+ xfs_warn(log->l_mp,
+ "INODE: inode 0x%llx item size %u does not match region count %u recovered.",
+ ilfp->ilf_ino, ilfp->ilf_size, item->ri_total);
+ return -EFSCORRUPTED;
+ }
+
+ nregions = 2;
+ if (ilfp->ilf_fields & XFS_ILOG_DFORK)
+ nregions++;
+ if (ilfp->ilf_fields & XFS_ILOG_AFORK)
+ nregions++;
+ if (ilfp->ilf_size != nregions) {
+ xfs_warn(log->l_mp,
+ "INODE: inode 0x%llx item size %u does not match region count %u the fields 0x%x define.",
+ ilfp->ilf_ino, ilfp->ilf_size, nregions,
+ ilfp->ilf_fields);
+ return -EFSCORRUPTED;
+ }
+
+ return xlog_recover_inode_check_location(log, ilfp);
+}
+
STATIC int
xlog_recover_inode_commit_pass2(
struct xlog *log,
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 46/59] xfs: validate the logged inode core during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (44 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 45/59] xfs: validate the inode item format header during recovery Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 47/59] xfs: validate the logged inode forks " Dave Chinner
` (12 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The log dinode core carries the inode fields being recovered into the on-disk
inode. Recovery validated only a handful of these fields, scattered through the
commit handler as it set up the replay, so most of the core was trusted and
written back without being checked.
Add xfs_log_dinode_verify() that verifies the fields carried in the log dinode
core. Call it during item validation once the core region length has been
checked, so the whole core is trusted before replay begins.
The fork offset check is common to the on-disk and log dinode, so
xfs_dinode_verify_forkoff() is recast as xfs_inode_verify_forkoff() taking the
offset and format as arguments and shared between them. The extent count helper
already present in recovery is converted to the failaddr idiom so it composes as
a sub-check of the new verifier.
The core checks the commit handler performed are removed, and the core region
length is now checked exactly against the size the running filesystem logs. The
on-disk inode magic check in the commit handler is also removed as the inode
buffer verifier run by the buffer read already performs it.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_inode_buf.c | 19 +-
fs/xfs/libxfs/xfs_inode_buf.h | 2 +
fs/xfs/xfs_inode_item_recover.c | 362 ++++++++++++++++++++++----------
3 files changed, 259 insertions(+), 124 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_inode_buf.c b/fs/xfs/libxfs/xfs_inode_buf.c
index 8c9d296ebfe9..8d5618684813 100644
--- a/fs/xfs/libxfs/xfs_inode_buf.c
+++ b/fs/xfs/libxfs/xfs_inode_buf.c
@@ -477,17 +477,18 @@ xfs_dinode_verify_fork(
return NULL;
}
-static xfs_failaddr_t
-xfs_dinode_verify_forkoff(
- struct xfs_dinode *dip,
- struct xfs_mount *mp)
+xfs_failaddr_t
+xfs_inode_verify_forkoff(
+ struct xfs_mount *mp,
+ uint8_t forkoff,
+ uint8_t format)
{
- if (!dip->di_forkoff)
+ if (!forkoff)
return NULL;
- switch (dip->di_format) {
+ switch (format) {
case XFS_DINODE_FMT_DEV:
- if (dip->di_forkoff != XFS_B_TO_FORKOFF(sizeof(xfs_dev_t)))
+ if (forkoff != XFS_B_TO_FORKOFF(sizeof(xfs_dev_t)))
return __this_address;
break;
case XFS_DINODE_FMT_META_BTREE:
@@ -497,7 +498,7 @@ xfs_dinode_verify_forkoff(
case XFS_DINODE_FMT_LOCAL: /* fall through ... */
case XFS_DINODE_FMT_EXTENTS: /* fall through ... */
case XFS_DINODE_FMT_BTREE:
- if (dip->di_forkoff >= XFS_B_TO_FORKOFFT(XFS_LITINO(mp)))
+ if (forkoff >= XFS_B_TO_FORKOFFT(XFS_LITINO(mp)))
return __this_address;
break;
default:
@@ -683,7 +684,7 @@ xfs_dinode_verify(
return __this_address;
/* check for illegal values of forkoff */
- fa = xfs_dinode_verify_forkoff(dip, mp);
+ fa = xfs_inode_verify_forkoff(mp, dip->di_forkoff, dip->di_format);
if (fa)
return fa;
diff --git a/fs/xfs/libxfs/xfs_inode_buf.h b/fs/xfs/libxfs/xfs_inode_buf.h
index f3624532b023..5e8e7f920d55 100644
--- a/fs/xfs/libxfs/xfs_inode_buf.h
+++ b/fs/xfs/libxfs/xfs_inode_buf.h
@@ -35,6 +35,8 @@ xfs_failaddr_t xfs_inode_validate_extsize(struct xfs_mount *mp,
xfs_failaddr_t xfs_inode_validate_cowextsize(struct xfs_mount *mp,
uint32_t cowextsize, uint16_t mode, uint16_t flags,
uint64_t flags2);
+xfs_failaddr_t xfs_inode_verify_forkoff(struct xfs_mount *mp, uint8_t forkoff,
+ uint8_t format);
static inline uint64_t xfs_inode_encode_bigtime(struct timespec64 tv)
{
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index 98596f27fb06..572d276074f6 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -23,6 +23,8 @@
#include "xfs_icache.h"
#include "xfs_ialloc.h"
#include "xfs_ag.h"
+#include "xfs_dir2.h"
+#include "xfs_metafile.h"
#include "xfs_bmap_btree.h"
#include "xfs_rtrmap_btree.h"
#include "xfs_rtrefcount_btree.h"
@@ -213,7 +215,7 @@ xfs_log_dinode_to_disk(
xfs_log_dinode_to_disk_iext_counters(from, to);
}
-STATIC int
+static xfs_failaddr_t
xlog_dinode_verify_extent_counts(
struct xfs_mount *mp,
struct xfs_log_dinode *ldip)
@@ -223,45 +225,220 @@ xlog_dinode_verify_extent_counts(
if (xfs_log_dinode_has_large_extent_counts(ldip)) {
if (!xfs_has_large_extent_counts(mp) ||
- (ldip->di_nrext64_pad != 0)) {
- XFS_CORRUPTION_ERROR(
- "Bad log dinode large extent count format",
- XFS_ERRLEVEL_LOW, mp, ldip, sizeof(*ldip));
- xfs_alert(mp,
- "Bad inode 0x%llx, large extent counts %d, padding 0x%x",
- ldip->di_ino, xfs_has_large_extent_counts(mp),
- ldip->di_nrext64_pad);
- return -EFSCORRUPTED;
- }
+ (ldip->di_nrext64_pad != 0))
+ return __this_address;
nextents = ldip->di_big_nextents;
anextents = ldip->di_big_anextents;
} else {
- if (ldip->di_version == 3 && ldip->di_v3_pad != 0) {
- XFS_CORRUPTION_ERROR(
- "Bad log dinode di_v3_pad",
- XFS_ERRLEVEL_LOW, mp, ldip, sizeof(*ldip));
- xfs_alert(mp,
- "Bad inode 0x%llx, di_v3_pad 0x%llx",
- ldip->di_ino, ldip->di_v3_pad);
- return -EFSCORRUPTED;
- }
+ if (ldip->di_version == 3 && ldip->di_v3_pad != 0)
+ return __this_address;
nextents = ldip->di_nextents;
anextents = ldip->di_anextents;
}
- if (unlikely(nextents + anextents > ldip->di_nblocks)) {
- XFS_CORRUPTION_ERROR("Bad log dinode extent counts",
- XFS_ERRLEVEL_LOW, mp, ldip, sizeof(*ldip));
- xfs_alert(mp,
- "Bad inode 0x%llx, large extent counts %d, nextents 0x%llx, anextents 0x%x, nblocks 0x%llx",
- ldip->di_ino, xfs_has_large_extent_counts(mp), nextents,
- anextents, ldip->di_nblocks);
- return -EFSCORRUPTED;
+ if (nextents + anextents > ldip->di_nblocks)
+ return __this_address;
+
+ return NULL;
+}
+
+/*
+ * Host order counterpart of xfs_dinode_verify_metadir(). The generic v3 field
+ * checks that verifier repeats are already covered before we get here, so this
+ * only validates the metadata inode specific rules.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_metadir(
+ struct xfs_mount *mp,
+ struct xfs_log_dinode *ldip)
+{
+ if (!xfs_has_metadir(mp))
+ return __this_address;
+
+ if (ldip->di_metatype >= XFS_METAFILE_MAX)
+ return __this_address;
+
+ /* Metadata files can only be directories or regular files */
+ if (!S_ISDIR(ldip->di_mode) && !S_ISREG(ldip->di_mode))
+ return __this_address;
+
+ /* They must have zero access permissions */
+ if (ldip->di_mode & 0777)
+ return __this_address;
+
+ /* DMAPI event and state masks are zero */
+ if (ldip->di_dmevmask || ldip->di_dmstate)
+ return __this_address;
+
+ /*
+ * User and group IDs must be zero. The project ID is used for
+ * grouping inodes. Metadata inodes are never accounted to quotas.
+ */
+ if (ldip->di_uid || ldip->di_gid)
+ return __this_address;
+
+ /* Mandatory inode flags must be set */
+ if (S_ISDIR(ldip->di_mode)) {
+ if ((ldip->di_flags & XFS_METADIR_DIFLAGS) != XFS_METADIR_DIFLAGS)
+ return __this_address;
+ } else {
+ if ((ldip->di_flags & XFS_METAFILE_DIFLAGS) != XFS_METAFILE_DIFLAGS)
+ return __this_address;
}
- return 0;
+ /* dax flags2 must not be set */
+ if (ldip->di_flags2 & XFS_DIFLAG2_DAX)
+ return __this_address;
+
+ return NULL;
+}
+
+/*
+ * Verify the logged inode core. This is the host order counterpart of
+ * xfs_dinode_verify(), restricted to the fields carried in the log dinode
+ * core. The checks that read the data or attribute fork contents are done
+ * separately against the logged fork regions, and the checks that only apply
+ * to a written on-disk inode (the CRC, di_next_unlinked, the LSN) do not apply
+ * to the log dinode at all.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify(
+ struct xfs_mount *mp,
+ xfs_ino_t ino,
+ struct xfs_log_dinode *ldip)
+{
+ xfs_failaddr_t fa;
+ uint16_t mode;
+ uint16_t flags;
+ uint64_t flags2;
+
+ if (ldip->di_magic != XFS_DINODE_MAGIC)
+ return __this_address;
+
+ /*
+ * Only inode versions 2 and 3 are valid. Verify the version specific
+ * integrity information, which for v3 must be checked before the rest
+ * of the core so it can be trusted.
+ */
+ switch (ldip->di_version) {
+ case 2:
+ if (ldip->di_metatype)
+ return __this_address;
+ break;
+ case 3:
+ if (!xfs_has_v3inodes(mp))
+ return __this_address;
+ if (ldip->di_ino != ino)
+ return __this_address;
+ if (!uuid_equal(&ldip->di_uuid, &mp->m_sb.sb_meta_uuid))
+ return __this_address;
+ if (!(ldip->di_flags2 & XFS_DIFLAG2_METADATA) &&
+ ldip->di_metatype)
+ return __this_address;
+ break;
+ default:
+ return __this_address;
+ }
+
+ /* don't allow invalid i_size */
+ if (ldip->di_size & (1ULL << 63))
+ return __this_address;
+
+ mode = ldip->di_mode;
+ if (mode && xfs_mode_to_ftype(mode) == XFS_DIR3_FT_UNKNOWN)
+ return __this_address;
+
+ /*
+ * No zero-length symlinks/dirs unless they're unlinked and hence being
+ * inactivated.
+ */
+ if ((S_ISLNK(mode) || S_ISDIR(mode)) && ldip->di_size == 0) {
+ if (ldip->di_nlink)
+ return __this_address;
+ }
+
+ fa = xlog_dinode_verify_extent_counts(mp, ldip);
+ if (fa)
+ return fa;
+
+ flags = ldip->di_flags;
+
+ if (mode && (flags & XFS_DIFLAG_REALTIME) && !mp->m_rtdev_targp)
+ return __this_address;
+
+ /* check for illegal values of forkoff */
+ fa = xfs_inode_verify_forkoff(mp, ldip->di_forkoff, ldip->di_format);
+ if (fa)
+ return fa;
+
+ /* Do we have appropriate data fork formats for the mode? */
+ switch (mode & S_IFMT) {
+ case S_IFIFO:
+ case S_IFCHR:
+ case S_IFBLK:
+ case S_IFSOCK:
+ if (ldip->di_format != XFS_DINODE_FMT_DEV)
+ return __this_address;
+ break;
+ case S_IFREG:
+ case S_IFLNK:
+ case S_IFDIR:
+ case 0:
+ /* Fork format is verified against the logged fork regions. */
+ break;
+ default:
+ return __this_address;
+ }
+
+ /* extent size hint validation */
+ fa = xfs_inode_validate_extsize(mp, ldip->di_extsize, mode, flags);
+ if (fa)
+ return fa;
+
+ /* only version 3 or greater inodes are extensively verified here */
+ if (ldip->di_version < 3)
+ return NULL;
+
+ flags2 = ldip->di_flags2;
+
+ /* don't allow reflink/cowextsize if we don't have reflink */
+ if ((flags2 & (XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE)) &&
+ !xfs_has_reflink(mp))
+ return __this_address;
+
+ /* only regular files get reflink */
+ if ((flags2 & XFS_DIFLAG2_REFLINK) && (mode & S_IFMT) != S_IFREG)
+ return __this_address;
+
+ /* don't let reflink and realtime mix */
+ if ((flags2 & XFS_DIFLAG2_REFLINK) && (flags & XFS_DIFLAG_REALTIME) &&
+ !xfs_has_rtreflink(mp))
+ return __this_address;
+
+ if (xfs_has_zoned(mp) && ldip->di_metatype == XFS_METAFILE_RTRMAP) {
+ if (ldip->di_used_blocks > mp->m_sb.sb_rgextents)
+ return __this_address;
+ } else {
+ /* COW extent size hint validation */
+ fa = xfs_inode_validate_cowextsize(mp, ldip->di_cowextsize,
+ mode, flags, flags2);
+ if (fa)
+ return fa;
+ }
+
+ /* bigtime iflag can only happen on bigtime filesystems */
+ if (xfs_log_dinode_has_bigtime(ldip) && !xfs_has_bigtime(mp))
+ return __this_address;
+
+ if (flags2 & XFS_DIFLAG2_METADATA) {
+ fa = xlog_dinode_verify_metadir(mp, ldip);
+ if (fa)
+ return fa;
+ }
+
+ return NULL;
}
static inline int
@@ -315,8 +492,9 @@ xlog_recover_inode_nregions(
}
/*
- * The format header (region 0) is logged in either the host format or the 32
- * bit on-disk format, so it must match one of those two sizes.
+ * Region 0 is the format header, logged in either the host format or the 32 bit
+ * on-disk format, so it must match one of those two sizes. Region 1 is the log
+ * dinode core, always logged at the size the running filesystem uses.
*/
static int
xlog_recover_inode_validate_region(
@@ -326,14 +504,25 @@ xlog_recover_inode_validate_region(
{
size_t len = item->ri_buf[region_index].iov_len;
- if (region_index > 0)
+ if (region_index == 0) {
+ if (len != sizeof(struct xfs_inode_log_format) &&
+ len != sizeof(struct xfs_inode_log_format_32)) {
+ xfs_warn(log->l_mp,
+ "INODE: bad format region length %zu recovered.",
+ len);
+ return -EFSCORRUPTED;
+ }
return 0;
+ }
- if (len != sizeof(struct xfs_inode_log_format) &&
- len != sizeof(struct xfs_inode_log_format_32)) {
- xfs_warn(log->l_mp,
- "INODE: bad format region length %zu recovered.", len);
- return -EFSCORRUPTED;
+ if (region_index == 1) {
+ if (len != xfs_log_dinode_size(log->l_mp)) {
+ xfs_warn(log->l_mp,
+ "INODE: bad core region length %zu recovered.",
+ len);
+ return -EFSCORRUPTED;
+ }
+ return 0;
}
return 0;
@@ -468,10 +657,11 @@ xlog_recover_inode_check_location(
}
/*
- * Validate the format header once it is in host format. The item size records
- * how many regions the item should have, the set of logged fields fixes what
- * that count must be, and the inode number fixes the buffer location, so all of
- * these are checked against the header here.
+ * Validate the format header once it is in host format, then the log dinode
+ * core it describes. The item size records how many regions the item should
+ * have, the set of logged fields fixes what that count must be, and the inode
+ * number fixes the buffer location, so all of these are checked against the
+ * header here.
*/
static int
xlog_recover_inode_validate_item(
@@ -479,6 +669,8 @@ xlog_recover_inode_validate_item(
struct xlog_recover_item *item)
{
struct xfs_inode_log_format *ilfp;
+ struct xfs_log_dinode *ldip;
+ xfs_failaddr_t fa;
unsigned int nregions;
int error;
@@ -520,7 +712,20 @@ xlog_recover_inode_validate_item(
return -EFSCORRUPTED;
}
- return xlog_recover_inode_check_location(log, ilfp);
+ error = xlog_recover_inode_check_location(log, ilfp);
+ if (error)
+ return error;
+
+ ldip = item->ri_buf[1].iov_base;
+ fa = xlog_dinode_verify(log->l_mp, ilfp->ilf_ino, ldip);
+ if (fa) {
+ xfs_warn(log->l_mp,
+ "INODE: inode 0x%llx log dinode failed validation at %pS",
+ ilfp->ilf_ino, fa);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
}
STATIC int
@@ -541,7 +746,6 @@ xlog_recover_inode_commit_pass2(
int attr_index;
uint fields;
struct xfs_log_dinode *ldip;
- uint isize;
xfs_failaddr_t fa;
/*
@@ -558,28 +762,8 @@ xlog_recover_inode_commit_pass2(
0, &bp, &xfs_inode_buf_ops);
if (error)
return error;
- ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
dip = xfs_buf_offset(bp, in_f->ilf_boffset);
-
- /*
- * Make sure the place we're flushing out to really looks
- * like an inode!
- */
- if (XFS_IS_CORRUPT(mp, !xfs_verify_magic16(bp, dip->di_magic))) {
- xfs_alert(mp,
- "%s: Bad inode magic number, dip = "PTR_FMT", dino bp = "PTR_FMT", ino = %lld",
- __func__, dip, bp, in_f->ilf_ino);
- error = -EFSCORRUPTED;
- goto out_release;
- }
ldip = item->ri_buf[1].iov_base;
- if (XFS_IS_CORRUPT(mp, ldip->di_magic != XFS_DINODE_MAGIC)) {
- xfs_alert(mp,
- "%s: Bad inode log record, rec ptr "PTR_FMT", ino %lld",
- __func__, item, in_f->ilf_ino);
- error = -EFSCORRUPTED;
- goto out_release;
- }
/*
* If the inode has an LSN in it, recover the inode only if the on-disk
@@ -636,58 +820,6 @@ xlog_recover_inode_commit_pass2(
}
- if (unlikely(S_ISREG(ldip->di_mode))) {
- if (ldip->di_format != XFS_DINODE_FMT_EXTENTS &&
- ldip->di_format != XFS_DINODE_FMT_BTREE &&
- ldip->di_format != XFS_DINODE_FMT_META_BTREE) {
- XFS_CORRUPTION_ERROR(
- "Bad log dinode data fork format for regular file",
- XFS_ERRLEVEL_LOW, mp, ldip, sizeof(*ldip));
- xfs_alert(mp,
- "Bad inode 0x%llx, data fork format 0x%x",
- in_f->ilf_ino, ldip->di_format);
- error = -EFSCORRUPTED;
- goto out_release;
- }
- } else if (unlikely(S_ISDIR(ldip->di_mode))) {
- if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
- (ldip->di_format != XFS_DINODE_FMT_BTREE) &&
- (ldip->di_format != XFS_DINODE_FMT_LOCAL)) {
- XFS_CORRUPTION_ERROR(
- "Bad log dinode data fork format for directory",
- XFS_ERRLEVEL_LOW, mp, ldip, sizeof(*ldip));
- xfs_alert(mp,
- "Bad inode 0x%llx, data fork format 0x%x",
- in_f->ilf_ino, ldip->di_format);
- error = -EFSCORRUPTED;
- goto out_release;
- }
- }
-
- error = xlog_dinode_verify_extent_counts(mp, ldip);
- if (error)
- goto out_release;
-
- if (unlikely(ldip->di_forkoff > mp->m_sb.sb_inodesize)) {
- XFS_CORRUPTION_ERROR("Bad log dinode fork offset",
- XFS_ERRLEVEL_LOW, mp, ldip, sizeof(*ldip));
- xfs_alert(mp,
- "Bad inode 0x%llx, di_forkoff 0x%x",
- in_f->ilf_ino, ldip->di_forkoff);
- error = -EFSCORRUPTED;
- goto out_release;
- }
- isize = xfs_log_dinode_size(mp);
- if (unlikely(item->ri_buf[1].iov_len > isize)) {
- XFS_CORRUPTION_ERROR("Bad log dinode size", XFS_ERRLEVEL_LOW,
- mp, ldip, sizeof(*ldip));
- xfs_alert(mp,
- "Bad inode 0x%llx log dinode size 0x%zx",
- in_f->ilf_ino, item->ri_buf[1].iov_len);
- error = -EFSCORRUPTED;
- goto out_release;
- }
-
/*
* Recover the log dinode inode into the on disk inode.
*
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 47/59] xfs: validate the logged inode forks during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (45 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 46/59] xfs: validate the logged inode core " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 48/59] xfs: validate logged local fork contents " Dave Chinner
` (11 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
An inode item logs a data and/or attribute fork region alongside the core. The
commit handler trusted these regions. It guarded them only with asserts as it
copied each fork into the on-disk inode. A fork whose format, size or placement
did not agree with the core was replayed regardless.
Verify the logged forks during item validation, once the core has been verified,
so the fork is trusted before replay begins. The contents of the fork regions
are not inspected here. A device inode is handled separately as it holds its
rdev in the format header and logs no data fork region. The asserts the commit
handler used are removed.
The fork size calculation was duplicated between the on-disk inode macros and
the in-memory inode helpers. Add __xfs_inode_fork_size(), keyed on the scalar
fork offset, and route the macros, the in-memory helpers and the new log dinode
check through it so the calculation exists in one place.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_format.h | 8 +-
fs/xfs/libxfs/xfs_inode_fork.c | 2 +-
fs/xfs/xfs_inode.h | 43 +++++---
fs/xfs/xfs_inode_item_recover.c | 168 +++++++++++++++++++++++++++++---
4 files changed, 185 insertions(+), 36 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_format.h b/fs/xfs/libxfs/xfs_format.h
index 469e879149ac..939514309012 100644
--- a/fs/xfs/libxfs/xfs_format.h
+++ b/fs/xfs/libxfs/xfs_format.h
@@ -1103,13 +1103,11 @@ enum xfs_dinode_fmt {
#define XFS_DFORK_BOFF(dip) XFS_FORKOFF_TO_B((dip)->di_forkoff)
#define XFS_DFORK_DSIZE(dip,mp) \
- ((dip)->di_forkoff ? XFS_DFORK_BOFF(dip) : XFS_LITINO(mp))
+ __xfs_inode_fork_size(mp, (dip)->di_forkoff, XFS_DATA_FORK)
#define XFS_DFORK_ASIZE(dip,mp) \
- ((dip)->di_forkoff ? XFS_LITINO(mp) - XFS_DFORK_BOFF(dip) : 0)
+ __xfs_inode_fork_size(mp, (dip)->di_forkoff, XFS_ATTR_FORK)
#define XFS_DFORK_SIZE(dip,mp,w) \
- ((w) == XFS_DATA_FORK ? \
- XFS_DFORK_DSIZE(dip, mp) : \
- XFS_DFORK_ASIZE(dip, mp))
+ __xfs_inode_fork_size(mp, (dip)->di_forkoff, w)
#define XFS_DFORK_MAXEXT(dip, mp, w) \
(XFS_DFORK_SIZE(dip, mp, w) / sizeof(struct xfs_bmbt_rec))
diff --git a/fs/xfs/libxfs/xfs_inode_fork.c b/fs/xfs/libxfs/xfs_inode_fork.c
index 0cf91ea2bf37..d856174ed8fa 100644
--- a/fs/xfs/libxfs/xfs_inode_fork.c
+++ b/fs/xfs/libxfs/xfs_inode_fork.c
@@ -86,7 +86,7 @@ xfs_iformat_local(
*/
if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
xfs_warn(ip->i_mount,
- "corrupt inode %llu (bad size %d for local fork, size = %zd).",
+ "corrupt inode %llu (bad size %d for local fork, size = %u).",
(unsigned long long)I_INO(ip), size,
XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
xfs_inode_verifier_error(ip, -EFSCORRUPTED,
diff --git a/fs/xfs/xfs_inode.h b/fs/xfs/xfs_inode.h
index b16eee99b567..71cea0bcd932 100644
--- a/fs/xfs/xfs_inode.h
+++ b/fs/xfs/xfs_inode.h
@@ -135,19 +135,39 @@ static inline unsigned int xfs_inode_fork_boff(struct xfs_inode *ip)
return XFS_FORKOFF_TO_B(ip->i_forkoff);
}
-static inline unsigned int xfs_inode_data_fork_size(struct xfs_inode *ip)
+/*
+ * Space available to a fork given the inode's fork offset. The on-disk, logged
+ * and in-memory inodes all express the fork offset the same way, so share this
+ * calculation through a scalar helper.
+ */
+static inline unsigned int
+__xfs_inode_fork_size(
+ struct xfs_mount *mp,
+ uint8_t forkoff,
+ int whichfork)
{
- if (xfs_inode_has_attr_fork(ip))
- return xfs_inode_fork_boff(ip);
+ unsigned int boff = XFS_FORKOFF_TO_B(forkoff);
- return XFS_LITINO(ip->i_mount);
+ switch (whichfork) {
+ case XFS_DATA_FORK:
+ return forkoff ? boff : XFS_LITINO(mp);
+ case XFS_ATTR_FORK:
+ return forkoff ? XFS_LITINO(mp) - boff : 0;
+ default:
+ return 0;
+ }
+}
+
+static inline unsigned int xfs_inode_data_fork_size(struct xfs_inode *ip)
+{
+ return __xfs_inode_fork_size(ip->i_mount, ip->i_forkoff,
+ XFS_DATA_FORK);
}
static inline unsigned int xfs_inode_attr_fork_size(struct xfs_inode *ip)
{
- if (xfs_inode_has_attr_fork(ip))
- return XFS_LITINO(ip->i_mount) - xfs_inode_fork_boff(ip);
- return 0;
+ return __xfs_inode_fork_size(ip->i_mount, ip->i_forkoff,
+ XFS_ATTR_FORK);
}
static inline unsigned int
@@ -155,14 +175,7 @@ xfs_inode_fork_size(
struct xfs_inode *ip,
int whichfork)
{
- switch (whichfork) {
- case XFS_DATA_FORK:
- return xfs_inode_data_fork_size(ip);
- case XFS_ATTR_FORK:
- return xfs_inode_attr_fork_size(ip);
- default:
- return 0;
- }
+ return __xfs_inode_fork_size(ip->i_mount, ip->i_forkoff, whichfork);
}
/* Convert from vfs inode to xfs inode */
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index 572d276074f6..566b03d1fe03 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -441,6 +441,128 @@ xlog_dinode_verify(
return NULL;
}
+/*
+ * Verify the logged data fork against the core and the fork region that carries
+ * it. The core has already been verified, so the mode and fork offset can be
+ * trusted here. The data fork format must be valid for the inode mode, whether
+ * or not the fork was logged. A regular file has no local format data fork. If
+ * the fork was logged, the field logged for it must match that format, and the
+ * region holding it must be the size the format header records. A local or
+ * extents fork is copied verbatim into the inode fork, so it must also fit
+ * within the space the fork offset leaves for it. A btree fork is logged in the
+ * larger expanded form and compressed into the fork on replay, so it does not
+ * get the fit check. The contents of the region are not inspected.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_dfork(
+ struct xfs_mount *mp,
+ struct xfs_log_dinode *ldip,
+ struct xfs_inode_log_format *ilfp,
+ struct xlog_recover_item *item)
+{
+ unsigned int field = ilfp->ilf_fields & XFS_ILOG_DFORK;
+ unsigned int fork_size;
+
+ switch (ldip->di_format) {
+ case XFS_DINODE_FMT_LOCAL:
+ if (S_ISREG(ldip->di_mode))
+ return __this_address;
+ break;
+ case XFS_DINODE_FMT_EXTENTS:
+ case XFS_DINODE_FMT_BTREE:
+ case XFS_DINODE_FMT_META_BTREE:
+ break;
+ default:
+ return __this_address;
+ }
+
+ if (!field)
+ return NULL;
+
+ if (item->ri_buf[2].iov_len != xlog_calc_iovec_len(ilfp->ilf_dsize))
+ return __this_address;
+
+ fork_size = __xfs_inode_fork_size(mp, ldip->di_forkoff, XFS_DATA_FORK);
+
+ switch (ldip->di_format) {
+ case XFS_DINODE_FMT_LOCAL:
+ if (field != XFS_ILOG_DDATA)
+ return __this_address;
+ if (ilfp->ilf_dsize > fork_size)
+ return __this_address;
+ break;
+ case XFS_DINODE_FMT_EXTENTS:
+ if (field != XFS_ILOG_DEXT)
+ return __this_address;
+ if (ilfp->ilf_dsize > fork_size)
+ return __this_address;
+ break;
+ case XFS_DINODE_FMT_BTREE:
+ case XFS_DINODE_FMT_META_BTREE:
+ if (field != XFS_ILOG_DBROOT)
+ return __this_address;
+ break;
+ }
+
+ return NULL;
+}
+
+/*
+ * Verify the logged attribute fork against the core and the fork region that
+ * carries it, as for the data fork. The attribute fork has no local device or
+ * metadata btree format.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_afork(
+ struct xfs_mount *mp,
+ struct xfs_log_dinode *ldip,
+ struct xfs_inode_log_format *ilfp,
+ struct xlog_recover_item *item)
+{
+ unsigned int field = ilfp->ilf_fields & XFS_ILOG_AFORK;
+ unsigned int fork_size;
+ unsigned int region;
+
+ switch (ldip->di_aformat) {
+ case XFS_DINODE_FMT_LOCAL:
+ case XFS_DINODE_FMT_EXTENTS:
+ case XFS_DINODE_FMT_BTREE:
+ break;
+ default:
+ return __this_address;
+ }
+
+ if (!field)
+ return NULL;
+
+ region = (ilfp->ilf_fields & XFS_ILOG_DFORK) ? 3 : 2;
+ if (item->ri_buf[region].iov_len != xlog_calc_iovec_len(ilfp->ilf_asize))
+ return __this_address;
+
+ fork_size = __xfs_inode_fork_size(mp, ldip->di_forkoff, XFS_ATTR_FORK);
+
+ switch (ldip->di_aformat) {
+ case XFS_DINODE_FMT_LOCAL:
+ if (field != XFS_ILOG_ADATA)
+ return __this_address;
+ if (ilfp->ilf_asize > fork_size)
+ return __this_address;
+ break;
+ case XFS_DINODE_FMT_EXTENTS:
+ if (field != XFS_ILOG_AEXT)
+ return __this_address;
+ if (ilfp->ilf_asize > fork_size)
+ return __this_address;
+ break;
+ case XFS_DINODE_FMT_BTREE:
+ if (field != XFS_ILOG_ABROOT)
+ return __this_address;
+ break;
+ }
+
+ return NULL;
+}
+
static inline int
xlog_recover_inode_dbroot(
struct xfs_mount *mp,
@@ -725,6 +847,36 @@ xlog_recover_inode_validate_item(
return -EFSCORRUPTED;
}
+ /*
+ * A device inode holds its rdev in the format header and logs no data
+ * fork region. Any other inode has its logged data fork verified against
+ * the core and the fork offset.
+ */
+ if (ldip->di_format == XFS_DINODE_FMT_DEV) {
+ if (ilfp->ilf_fields & XFS_ILOG_DFORK) {
+ xfs_warn(log->l_mp,
+ "INODE: device inode 0x%llx logged a data fork.",
+ ilfp->ilf_ino);
+ return -EFSCORRUPTED;
+ }
+ } else {
+ fa = xlog_dinode_verify_dfork(log->l_mp, ldip, ilfp, item);
+ if (fa) {
+ xfs_warn(log->l_mp,
+ "INODE: inode 0x%llx logged data fork failed validation at %pS",
+ ilfp->ilf_ino, fa);
+ return -EFSCORRUPTED;
+ }
+ }
+
+ fa = xlog_dinode_verify_afork(log->l_mp, ldip, ilfp, item);
+ if (fa) {
+ xfs_warn(log->l_mp,
+ "INODE: inode 0x%llx logged attr fork failed validation at %pS",
+ ilfp->ilf_ino, fa);
+ return -EFSCORRUPTED;
+ }
+
return 0;
}
@@ -840,10 +992,6 @@ xlog_recover_inode_commit_pass2(
goto out_owner_change;
len = item->ri_buf[2].iov_len;
src = item->ri_buf[2].iov_base;
- ASSERT(in_f->ilf_size <= 4);
- ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
- ASSERT(!(fields & XFS_ILOG_DFORK) ||
- (len == xlog_calc_iovec_len(in_f->ilf_dsize)));
/*
* The recovered inode is verified only after the fork data has been
@@ -877,10 +1025,7 @@ xlog_recover_inode_commit_pass2(
break;
default:
- /*
- * There are no data fork flags set.
- */
- ASSERT((fields & XFS_ILOG_DFORK) == 0);
+ /* No data fork was logged. */
break;
}
@@ -897,7 +1042,6 @@ xlog_recover_inode_commit_pass2(
}
len = item->ri_buf[attr_index].iov_len;
src = item->ri_buf[attr_index].iov_base;
- ASSERT(len == xlog_calc_iovec_len(in_f->ilf_asize));
switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
case XFS_ILOG_ADATA:
@@ -912,12 +1056,6 @@ xlog_recover_inode_commit_pass2(
len, (struct xfs_bmdr_block *)dest,
XFS_DFORK_ASIZE(dip, mp));
break;
-
- default:
- xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
- ASSERT(0);
- error = -EFSCORRUPTED;
- goto out_release;
}
}
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 48/59] xfs: validate logged local fork contents during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (46 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 47/59] xfs: validate the logged inode forks " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 49/59] xfs: validate logged extent " Dave Chinner
` (10 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A local format fork carries a shortform directory, an extended attribute
structure or a symlink target directly in the inode. Recovery copied these into
the inode without inspecting them, so a corrupt shortform structure was replayed
as it stood.
Validate the contents of a logged local fork once the fork has been verified
against the core. The shortform directory and attribute verifiers are reused
directly as they already operate on the on-disk form. The logged symlink target
is validated by a recovery local helper because the shared verifier expects the
null terminated in-core form, which the logged target does not have.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_inode_item_recover.c | 53 +++++++++++++++++++++++++++++----
1 file changed, 48 insertions(+), 5 deletions(-)
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index 566b03d1fe03..a2001716eeca 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -24,6 +24,8 @@
#include "xfs_ialloc.h"
#include "xfs_ag.h"
#include "xfs_dir2.h"
+#include "xfs_dir2_priv.h"
+#include "xfs_attr_leaf.h"
#include "xfs_metafile.h"
#include "xfs_bmap_btree.h"
#include "xfs_rtrmap_btree.h"
@@ -441,6 +443,45 @@ xlog_dinode_verify(
return NULL;
}
+/*
+ * Verify a logged shortform symlink target. The logged target is the on-disk
+ * form, which is not null terminated, so this cannot use
+ * xfs_symlink_shortform_verify().
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_symlink(
+ void *sfp,
+ unsigned int size)
+{
+ if (size == 0 || size > XFS_SYMLINK_MAXLEN)
+ return __this_address;
+
+ /* The target must not contain a null. */
+ if (memchr(sfp, 0, size))
+ return __this_address;
+
+ return NULL;
+}
+
+/*
+ * Verify the contents of a logged local format data fork. A directory holds a
+ * shortform directory, and a symlink holds its target. A regular file has no
+ * local format data fork.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_local_dfork(
+ struct xfs_mount *mp,
+ struct xfs_log_dinode *ldip,
+ void *src,
+ unsigned int size)
+{
+ if (S_ISDIR(ldip->di_mode))
+ return xfs_dir2_sf_verify(mp, src, size);
+ if (S_ISLNK(ldip->di_mode))
+ return xlog_dinode_verify_symlink(src, size);
+ return __this_address;
+}
+
/*
* Verify the logged data fork against the core and the fork region that carries
* it. The core has already been verified, so the mode and fork offset can be
@@ -449,9 +490,9 @@ xlog_dinode_verify(
* the fork was logged, the field logged for it must match that format, and the
* region holding it must be the size the format header records. A local or
* extents fork is copied verbatim into the inode fork, so it must also fit
- * within the space the fork offset leaves for it. A btree fork is logged in the
- * larger expanded form and compressed into the fork on replay, so it does not
- * get the fit check. The contents of the region are not inspected.
+ * within the space the fork offset leaves for it, and a local fork must hold
+ * valid contents. A btree fork is logged in the larger expanded form and
+ * compressed into the fork on replay, so it does not get the fit check.
*/
static xfs_failaddr_t
xlog_dinode_verify_dfork(
@@ -490,7 +531,8 @@ xlog_dinode_verify_dfork(
return __this_address;
if (ilfp->ilf_dsize > fork_size)
return __this_address;
- break;
+ return xlog_dinode_verify_local_dfork(mp, ldip,
+ item->ri_buf[2].iov_base, ilfp->ilf_dsize);
case XFS_DINODE_FMT_EXTENTS:
if (field != XFS_ILOG_DEXT)
return __this_address;
@@ -547,7 +589,8 @@ xlog_dinode_verify_afork(
return __this_address;
if (ilfp->ilf_asize > fork_size)
return __this_address;
- break;
+ return xfs_attr_shortform_verify(item->ri_buf[region].iov_base,
+ ilfp->ilf_asize);
case XFS_DINODE_FMT_EXTENTS:
if (field != XFS_ILOG_AEXT)
return __this_address;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 49/59] xfs: validate logged extent fork contents during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (47 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 48/59] xfs: validate logged local fork contents " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 50/59] xfs: allow a long format btree root to be checked without a cursor Dave Chinner
` (9 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
An extents format fork is logged as a packed array of on-disk extent records.
Recovery copied this array into the inode without inspecting it, so corrupt
extent records were replayed as they stood.
The existing verifiers open code the extent array walk for validation, so factor
that walk into xfs_bmap_validate_extent_array() and use it in all three places.
The on-disk reader now dumps the whole fork on a bad record rather than the
single record, so the failing record is still captured.
xfs_bmap_complain_bad_rec() is recast to take a mount and inode number rather
than an incore inode, so the array walk can report a bad record on behalf of any
caller. The walk reports against the inode number it is passed, and stays silent
when passed NULLFSINO.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_bmap.c | 48 ++++++++++++++++++++++++++++-----
fs/xfs/libxfs/xfs_bmap.h | 8 ++++--
fs/xfs/libxfs/xfs_inode_fork.c | 23 ++++++++--------
fs/xfs/scrub/inode_repair.c | 14 +++-------
fs/xfs/xfs_inode_item_recover.c | 35 ++++++++++++++++++++++--
5 files changed, 94 insertions(+), 34 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_bmap.c b/fs/xfs/libxfs/xfs_bmap.c
index ed081a5e4166..8dddde66de2a 100644
--- a/fs/xfs/libxfs/xfs_bmap.c
+++ b/fs/xfs/libxfs/xfs_bmap.c
@@ -1130,12 +1130,12 @@ struct xfs_iread_state {
int
xfs_bmap_complain_bad_rec(
- struct xfs_inode *ip,
+ struct xfs_mount *mp,
+ xfs_ino_t ino,
int whichfork,
xfs_failaddr_t fa,
const struct xfs_bmbt_irec *irec)
{
- struct xfs_mount *mp = ip->i_mount;
const char *forkname;
switch (whichfork) {
@@ -1147,7 +1147,7 @@ xfs_bmap_complain_bad_rec(
xfs_warn(mp,
"Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
- I_INO(ip), forkname, fa);
+ ino, forkname, fa);
xfs_warn(mp,
"Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
irec->br_startoff, irec->br_startblock, irec->br_blockcount,
@@ -1200,8 +1200,8 @@ xfs_iread_bmbt_block(
"xfs_iread_extents(2)", frp,
sizeof(*frp), fa);
xfs_bmap_mark_sick(ip, whichfork);
- return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
- &new);
+ return xfs_bmap_complain_bad_rec(ip->i_mount,
+ I_INO(ip), whichfork, fa, &new);
}
xfs_iext_insert(ip, &ir->icur, &new,
xfs_bmap_fork_to_state(whichfork));
@@ -6102,6 +6102,39 @@ xfs_bmap_validate_extent(
XFS_IS_REALTIME_INODE(ip), whichfork, irec);
}
+/*
+ * Check a packed on-disk array of extent records. Returns the address of the
+ * first bad record, or NULL if they are all good. If ino is not NULLFSINO, a
+ * bad record is also reported against that inode.
+ */
+xfs_failaddr_t
+xfs_bmap_validate_extent_array(
+ struct xfs_mount *mp,
+ xfs_ino_t ino,
+ bool rtfile,
+ int whichfork,
+ struct xfs_bmbt_rec *recs,
+ xfs_extnum_t nrecs)
+{
+ xfs_extnum_t i;
+
+ for (i = 0; i < nrecs; i++) {
+ struct xfs_bmbt_irec irec;
+ xfs_failaddr_t fa;
+
+ xfs_bmbt_disk_get_all(&recs[i], &irec);
+ fa = xfs_bmap_validate_extent_raw(mp, rtfile, whichfork, &irec);
+ if (fa) {
+ if (ino != NULLFSINO)
+ xfs_bmap_complain_bad_rec(mp, ino, whichfork,
+ fa, &irec);
+ return fa;
+ }
+ }
+
+ return NULL;
+}
+
/*
* Used in xfs_itruncate_extents(). This is the maximum number of extents
* freed from a file in a single transaction.
@@ -6163,8 +6196,9 @@ xfs_bmap_query_range_helper(
&irec);
if (fa) {
xfs_btree_mark_sick(cur);
- return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
- cur->bc_ino.whichfork, fa, &irec);
+ return xfs_bmap_complain_bad_rec(cur->bc_mp,
+ I_INO(cur->bc_ino.ip), cur->bc_ino.whichfork,
+ fa, &irec);
}
return query->fn(cur, &irec, query->priv);
diff --git a/fs/xfs/libxfs/xfs_bmap.h b/fs/xfs/libxfs/xfs_bmap.h
index ab870415c505..26c80926278f 100644
--- a/fs/xfs/libxfs/xfs_bmap.h
+++ b/fs/xfs/libxfs/xfs_bmap.h
@@ -291,8 +291,12 @@ xfs_failaddr_t xfs_bmap_validate_extent_raw(struct xfs_mount *mp, bool rtfile,
int whichfork, struct xfs_bmbt_irec *irec);
xfs_failaddr_t xfs_bmap_validate_extent(struct xfs_inode *ip, int whichfork,
struct xfs_bmbt_irec *irec);
-int xfs_bmap_complain_bad_rec(struct xfs_inode *ip, int whichfork,
- xfs_failaddr_t fa, const struct xfs_bmbt_irec *irec);
+xfs_failaddr_t xfs_bmap_validate_extent_array(struct xfs_mount *mp,
+ xfs_ino_t ino, bool rtfile, int whichfork,
+ struct xfs_bmbt_rec *recs, xfs_extnum_t nrecs);
+int xfs_bmap_complain_bad_rec(struct xfs_mount *mp, xfs_ino_t ino,
+ int whichfork, xfs_failaddr_t fa,
+ const struct xfs_bmbt_irec *irec);
int xfs_bmapi_remap(struct xfs_trans *tp, struct xfs_inode *ip,
xfs_fileoff_t bno, xfs_filblks_t len, xfs_fsblock_t startblock,
diff --git a/fs/xfs/libxfs/xfs_inode_fork.c b/fs/xfs/libxfs/xfs_inode_fork.c
index d856174ed8fa..79f4fce650ff 100644
--- a/fs/xfs/libxfs/xfs_inode_fork.c
+++ b/fs/xfs/libxfs/xfs_inode_fork.c
@@ -138,23 +138,22 @@ xfs_iformat_extents(
ifp->if_data = NULL;
ifp->if_height = 0;
if (size) {
+ xfs_failaddr_t fa;
+
dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
+ fa = xfs_bmap_validate_extent_array(mp, I_INO(ip),
+ XFS_IS_REALTIME_INODE(ip), whichfork, dp, nex);
+ if (fa) {
+ xfs_inode_verifier_error(ip, -EFSCORRUPTED,
+ "xfs_iformat_extents(2)", dp, size, fa);
+ xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
+ return -EFSCORRUPTED;
+ }
+
xfs_iext_first(ifp, &icur);
for (i = 0; i < nex; i++, dp++) {
- xfs_failaddr_t fa;
-
xfs_bmbt_disk_get_all(dp, &new);
- fa = xfs_bmap_validate_extent(ip, whichfork, &new);
- if (fa) {
- xfs_inode_verifier_error(ip, -EFSCORRUPTED,
- "xfs_iformat_extents(2)",
- dp, sizeof(*dp), fa);
- xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
- return xfs_bmap_complain_bad_rec(ip, whichfork,
- fa, &new);
- }
-
xfs_iext_insert(ip, &icur, &new, state);
trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
xfs_iext_next(ifp, &icur);
diff --git a/fs/xfs/scrub/inode_repair.c b/fs/xfs/scrub/inode_repair.c
index dbd42976bdcd..2dd3ea7fb361 100644
--- a/fs/xfs/scrub/inode_repair.c
+++ b/fs/xfs/scrub/inode_repair.c
@@ -873,11 +873,9 @@ xrep_dinode_bad_extents_fork(
unsigned int dfork_size,
int whichfork)
{
- struct xfs_bmbt_irec new;
struct xfs_bmbt_rec *dp;
xfs_extnum_t nex;
bool isrt;
- unsigned int i;
nex = xfs_dfork_nextents(dip, whichfork);
if (nex > dfork_size / sizeof(struct xfs_bmbt_rec))
@@ -886,15 +884,9 @@ xrep_dinode_bad_extents_fork(
dp = XFS_DFORK_PTR(dip, whichfork);
isrt = dip->di_flags & cpu_to_be16(XFS_DIFLAG_REALTIME);
- for (i = 0; i < nex; i++, dp++) {
- xfs_failaddr_t fa;
-
- xfs_bmbt_disk_get_all(dp, &new);
- fa = xfs_bmap_validate_extent_raw(sc->mp, isrt, whichfork,
- &new);
- if (fa)
- return true;
- }
+ if (xfs_bmap_validate_extent_array(sc->mp, NULLFSINO, isrt, whichfork,
+ dp, nex))
+ return true;
return false;
}
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index a2001716eeca..89a087d77aae 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -27,6 +27,7 @@
#include "xfs_dir2_priv.h"
#include "xfs_attr_leaf.h"
#include "xfs_metafile.h"
+#include "xfs_bmap.h"
#include "xfs_bmap_btree.h"
#include "xfs_rtrmap_btree.h"
#include "xfs_rtrefcount_btree.h"
@@ -443,6 +444,32 @@ xlog_dinode_verify(
return NULL;
}
+/*
+ * Verify a logged extents fork. The region is a packed array of on-disk extent
+ * records, so its size fixes the number of records it holds.
+ *
+ * The region size has already been bounds checked against the inode fork size,
+ * so it is known to fit within the fork and does not need rechecking here.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_extents(
+ struct xfs_mount *mp,
+ xfs_ino_t ino,
+ struct xfs_log_dinode *ldip,
+ int whichfork,
+ void *src,
+ unsigned int size)
+{
+ bool isrt;
+
+ if (size == 0 || (size % sizeof(struct xfs_bmbt_rec)))
+ return __this_address;
+
+ isrt = ldip->di_flags & XFS_DIFLAG_REALTIME;
+ return xfs_bmap_validate_extent_array(mp, ino, isrt, whichfork,
+ src, size / sizeof(struct xfs_bmbt_rec));
+}
+
/*
* Verify a logged shortform symlink target. The logged target is the on-disk
* form, which is not null terminated, so this cannot use
@@ -538,7 +565,9 @@ xlog_dinode_verify_dfork(
return __this_address;
if (ilfp->ilf_dsize > fork_size)
return __this_address;
- break;
+ return xlog_dinode_verify_extents(mp, ilfp->ilf_ino, ldip,
+ XFS_DATA_FORK, item->ri_buf[2].iov_base,
+ ilfp->ilf_dsize);
case XFS_DINODE_FMT_BTREE:
case XFS_DINODE_FMT_META_BTREE:
if (field != XFS_ILOG_DBROOT)
@@ -596,7 +625,9 @@ xlog_dinode_verify_afork(
return __this_address;
if (ilfp->ilf_asize > fork_size)
return __this_address;
- break;
+ return xlog_dinode_verify_extents(mp, ilfp->ilf_ino, ldip,
+ XFS_ATTR_FORK, item->ri_buf[region].iov_base,
+ ilfp->ilf_asize);
case XFS_DINODE_FMT_BTREE:
if (field != XFS_ILOG_ABROOT)
return __this_address;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 50/59] xfs: allow a long format btree root to be checked without a cursor
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (48 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 49/59] xfs: validate logged extent " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 51/59] xfs: validate logged btree fork roots during recovery Dave Chinner
` (8 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The long format btree block header check derives the block magic and the
maximum record count from a btree cursor. An inode rooted btree logged to the
journal has no cursor available during recovery, so this check cannot be used to
validate a logged btree root.
Rework the header check to take the magic and maximum records as arguments
rather than reading them from a cursor. The cursor based callers derive both
from the cursor and pass them in, so their behaviour is unchanged.
Add xfs_btree_check_fsblock_root_hdr() for callers that hold a long format btree
root but no cursor. It checks the root against the supplied magic, maximum
records and level range, and confirms the root has no siblings.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_btree.c | 50 ++++++++++++++++++++++++++++++---------
fs/xfs/libxfs/xfs_btree.h | 4 ++++
2 files changed, 43 insertions(+), 11 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_btree.c b/fs/xfs/libxfs/xfs_btree.c
index e03b83b1d9b2..1971d2a56a3d 100644
--- a/fs/xfs/libxfs/xfs_btree.c
+++ b/fs/xfs/libxfs/xfs_btree.c
@@ -123,13 +123,13 @@ xfs_btree_check_agblock_siblings(
static xfs_failaddr_t
__xfs_btree_check_lblock_hdr(
- struct xfs_btree_cur *cur,
+ struct xfs_mount *mp,
+ uint32_t magic,
+ unsigned int maxrecs,
struct xfs_btree_block *block,
int level,
struct xfs_buf *bp)
{
- struct xfs_mount *mp = cur->bc_mp;
-
if (xfs_has_crc(mp)) {
if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
return __this_address;
@@ -140,12 +140,11 @@ __xfs_btree_check_lblock_hdr(
return __this_address;
}
- if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(mp, cur->bc_ops))
+ if (be32_to_cpu(block->bb_magic) != magic)
return __this_address;
if (be16_to_cpu(block->bb_level) != level)
return __this_address;
- if (be16_to_cpu(block->bb_numrecs) >
- cur->bc_ops->get_maxrecs(cur, level))
+ if (be16_to_cpu(block->bb_numrecs) > maxrecs)
return __this_address;
return NULL;
@@ -157,16 +156,17 @@ __xfs_btree_check_lblock_hdr(
*/
static xfs_failaddr_t
__xfs_btree_check_fsblock(
- struct xfs_btree_cur *cur,
+ struct xfs_mount *mp,
+ uint32_t magic,
+ unsigned int maxrecs,
struct xfs_btree_block *block,
int level,
struct xfs_buf *bp)
{
- struct xfs_mount *mp = cur->bc_mp;
xfs_failaddr_t fa;
xfs_fsblock_t fsb;
- fa = __xfs_btree_check_lblock_hdr(cur, block, level, bp);
+ fa = __xfs_btree_check_lblock_hdr(mp, magic, maxrecs, block, level, bp);
if (fa)
return fa;
@@ -191,6 +191,29 @@ __xfs_btree_check_fsblock(
return fa;
}
+/*
+ * Check the header of a long format btree root that sits in an inode fork,
+ * without needing a btree cursor. The block is a root, so it has no siblings.
+ * The caller supplies the block magic, the maximum records for the block, and
+ * the valid level range for the btree.
+ */
+xfs_failaddr_t
+xfs_btree_check_fsblock_root_hdr(
+ struct xfs_mount *mp,
+ uint32_t magic,
+ unsigned int maxrecs,
+ struct xfs_btree_block *block,
+ unsigned int minlevel,
+ unsigned int maxlevel)
+{
+ unsigned int level = be16_to_cpu(block->bb_level);
+
+ if (level < minlevel || level > maxlevel)
+ return __this_address;
+
+ return __xfs_btree_check_fsblock(mp, magic, maxrecs, block, level, NULL);
+}
+
/*
* Check an in-memory btree block header. Return the address of the failing
* check, or NULL if everything is ok.
@@ -202,11 +225,13 @@ __xfs_btree_check_memblock(
int level,
struct xfs_buf *bp)
{
+ struct xfs_mount *mp = cur->bc_mp;
struct xfs_buftarg *btp = cur->bc_mem.xfbtree->target;
xfs_failaddr_t fa;
xfbno_t bno;
- fa = __xfs_btree_check_lblock_hdr(cur, block, level, bp);
+ fa = __xfs_btree_check_lblock_hdr(mp, xfs_btree_magic(mp, cur->bc_ops),
+ cur->bc_ops->get_maxrecs(cur, level), block, level, bp);
if (fa)
return fa;
@@ -277,7 +302,10 @@ __xfs_btree_check_block(
case XFS_BTREE_TYPE_AG:
return __xfs_btree_check_agblock(cur, block, level, bp);
case XFS_BTREE_TYPE_INODE:
- return __xfs_btree_check_fsblock(cur, block, level, bp);
+ return __xfs_btree_check_fsblock(cur->bc_mp,
+ xfs_btree_magic(cur->bc_mp, cur->bc_ops),
+ cur->bc_ops->get_maxrecs(cur, level),
+ block, level, bp);
default:
ASSERT(0);
return __this_address;
diff --git a/fs/xfs/libxfs/xfs_btree.h b/fs/xfs/libxfs/xfs_btree.h
index c4337ab244f2..ad095a8b837c 100644
--- a/fs/xfs/libxfs/xfs_btree.h
+++ b/fs/xfs/libxfs/xfs_btree.h
@@ -345,6 +345,10 @@ xfs_btree_cur_sizeof(unsigned int nlevels)
xfs_failaddr_t __xfs_btree_check_block(struct xfs_btree_cur *cur,
struct xfs_btree_block *block, int level, struct xfs_buf *bp);
+xfs_failaddr_t xfs_btree_check_fsblock_root_hdr(struct xfs_mount *mp,
+ uint32_t magic, unsigned int maxrecs,
+ struct xfs_btree_block *block, unsigned int minlevel,
+ unsigned int maxlevel);
int __xfs_btree_check_ptr(struct xfs_btree_cur *cur,
const union xfs_btree_ptr *ptr, int index, int level);
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 51/59] xfs: validate logged btree fork roots during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (49 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 50/59] xfs: allow a long format btree root to be checked without a cursor Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 52/59] xfs: validate logged metadata " Dave Chinner
` (7 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A btree format fork is logged as the expanded in-core bmap btree root, which is
a normal btree root block whose length is only as long as the fork region that
holds it. Recovery copied this root into the inode without inspecting it, so a
corrupt root was replayed as it stood.
Validate the logged bmap btree root once the fork has been verified against the
core. The root is checked as a short btree root block whose record count fills
the fork region exactly, and its keys and pointers are checked to be in range.
The metadata btree roots are a distinct form and remain validated by a following
patch.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_inode_item_recover.c | 66 ++++++++++++++++++++++++++++++++-
1 file changed, 65 insertions(+), 1 deletion(-)
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index 89a087d77aae..d9ab9900a201 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -29,6 +29,7 @@
#include "xfs_metafile.h"
#include "xfs_bmap.h"
#include "xfs_bmap_btree.h"
+#include "xfs_btree.h"
#include "xfs_rtrmap_btree.h"
#include "xfs_rtrefcount_btree.h"
@@ -470,6 +471,64 @@ xlog_dinode_verify_extents(
src, size / sizeof(struct xfs_bmbt_rec));
}
+/*
+ * Verify a logged bmap btree root. The logged root is the expanded in-core
+ * block form, not the compact on-disk root, so it is a normal btree root block
+ * whose length is only as long as the fork region that holds it. The block
+ * length is fixed by the record count, so the region size fixes how many
+ * records the root can hold.
+ *
+ * The region size has already been bounds checked against the inode fork size,
+ * so it is known to fit within the fork and does not need rechecking here.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_bmbt_root(
+ struct xfs_mount *mp,
+ struct xfs_log_dinode *ldip,
+ int whichfork,
+ void *src,
+ unsigned int size)
+{
+ struct xfs_btree_block *block = src;
+ xfs_failaddr_t fa;
+ uint32_t magic;
+ unsigned int numrecs;
+ unsigned int i;
+
+ if (size < xfs_bmbt_block_len(mp))
+ return __this_address;
+
+ /* The root block is never a leaf, so it always has keys and pointers. */
+ magic = xfs_has_crc(mp) ? XFS_BMAP_CRC_MAGIC : XFS_BMAP_MAGIC;
+ /*
+ * m_bm_maxlevels includes the inode root level, so the deepest valid
+ * root level is one less than that.
+ */
+ fa = xfs_btree_check_fsblock_root_hdr(mp, magic,
+ xfs_bmbt_maxrecs(mp, size, false), block, 1,
+ mp->m_bm_maxlevels[whichfork] - 1);
+ if (fa)
+ return fa;
+
+ /* The record count must be non-zero and exactly fill the region. */
+ numrecs = be16_to_cpu(block->bb_numrecs);
+ if (numrecs == 0 || size != xfs_bmap_broot_space_calc(mp, numrecs))
+ return __this_address;
+
+ for (i = 1; i <= numrecs; i++) {
+ struct xfs_bmbt_key *key = xfs_bmbt_key_addr(mp, block, i);
+ xfs_bmbt_ptr_t *ptr = xfs_bmap_broot_ptr_addr(mp, block,
+ i, size);
+
+ if (!xfs_verify_fileoff(be64_to_cpu(key->br_startoff)))
+ return __this_address;
+ if (!xfs_verify_fsbno(mp, be64_to_cpu(*ptr)))
+ return __this_address;
+ }
+
+ return NULL;
+}
+
/*
* Verify a logged shortform symlink target. The logged target is the on-disk
* form, which is not null terminated, so this cannot use
@@ -569,6 +628,10 @@ xlog_dinode_verify_dfork(
XFS_DATA_FORK, item->ri_buf[2].iov_base,
ilfp->ilf_dsize);
case XFS_DINODE_FMT_BTREE:
+ if (field != XFS_ILOG_DBROOT)
+ return __this_address;
+ return xlog_dinode_verify_bmbt_root(mp, ldip, XFS_DATA_FORK,
+ item->ri_buf[2].iov_base, ilfp->ilf_dsize);
case XFS_DINODE_FMT_META_BTREE:
if (field != XFS_ILOG_DBROOT)
return __this_address;
@@ -631,7 +694,8 @@ xlog_dinode_verify_afork(
case XFS_DINODE_FMT_BTREE:
if (field != XFS_ILOG_ABROOT)
return __this_address;
- break;
+ return xlog_dinode_verify_bmbt_root(mp, ldip, XFS_ATTR_FORK,
+ item->ri_buf[region].iov_base, ilfp->ilf_asize);
}
return NULL;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 52/59] xfs: validate logged metadata btree fork roots during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (50 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 51/59] xfs: validate logged btree fork roots during recovery Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 53/59] xfs: check logged btree fork roots fit in the inode fork " Dave Chinner
` (6 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A metadata btree format data fork holds an rt rmap or rt refcount btree root,
logged as the expanded in-core btree root like a bmap btree root. Recovery
copied this root into the inode without inspecting it, so a corrupt root was
replayed as it stood.
Validate the logged metadata btree root once the fork has been verified against
the core. The root is checked as a short btree root block whose record count
fills the fork region exactly, and its node pointers are checked to be in range.
The record and key contents are relative to a realtime group, which is not set up
until after log recovery, so their validation is left to the first runtime access
to the root block.
The maximum height of these btrees depends on the filesystem geometry, which
a growfs being recovered can change. The geometry the item will be replayed
against is not known when it is validated, so the root level is bounded by
the largest height any geometry allows. The runtime root loaders check it
against the real geometry once recovery has finished. An empty level zero
root is accepted, because growfs creates these inodes before it adds the
first realtime volume.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_inode_item_recover.c | 170 +++++++++++++++++++++++++++++++-
1 file changed, 169 insertions(+), 1 deletion(-)
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index d9ab9900a201..efb142efa0ef 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -529,6 +529,173 @@ xlog_dinode_verify_bmbt_root(
return NULL;
}
+/*
+ * Verify a logged rt rmap btree root, as for the bmap btree root. The node
+ * pointers are filesystem block numbers and are checked here.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_rtrmap_root(
+ struct xfs_mount *mp,
+ struct xfs_log_dinode *ldip,
+ void *src,
+ unsigned int size)
+{
+ struct xfs_btree_block *block = src;
+ xfs_failaddr_t fa;
+ unsigned int level;
+ unsigned int numrecs;
+ unsigned int i;
+
+ if (size < XFS_RTRMAP_BLOCK_LEN)
+ return __this_address;
+
+ /*
+ * The maximum rtrmap btree height depends on the filesystem geometry,
+ * which a growfs being recovered can change. The geometry this item
+ * will be replayed against is not known yet, so bound the root level by
+ * the largest height any geometry allows. The root is checked against
+ * the real geometry when the inode is loaded after recovery. Tighter
+ * validation depends on solving the recovery geometry problem described
+ * in the log recovery validation design document.
+ */
+ fa = xfs_btree_check_fsblock_root_hdr(mp, XFS_RTRMAP_CRC_MAGIC,
+ xfs_rtrmapbt_maxrecs(mp, size,
+ be16_to_cpu(block->bb_level) == 0),
+ block, 0, xfs_rtrmapbt_maxlevels_ondisk() - 1);
+ if (fa)
+ return fa;
+
+ /*
+ * rtrmap inodes can be dynamically created by growfs, resulting in a
+ * transient state on disk where they can have no contents. Only a level
+ * zero root may be empty.
+ */
+ level = be16_to_cpu(block->bb_level);
+ numrecs = be16_to_cpu(block->bb_numrecs);
+ if (level > 0 && numrecs == 0)
+ return __this_address;
+
+ if (size != xfs_rtrmap_broot_space_calc(level, numrecs))
+ return __this_address;
+
+ /*
+ * A leaf root holds records whose bounds are relative to a realtime
+ * group. The realtime groups are not set up until long after log
+ * recovery runs, so the leaf contents cannot be validated here. That is
+ * deferred to the first runtime access to the root block.
+ */
+ if (level == 0)
+ return NULL;
+
+ for (i = 1; i <= numrecs; i++) {
+ xfs_rtrmap_ptr_t *ptr = xfs_rtrmap_broot_ptr_addr(mp,
+ block, i, size);
+
+ if (!xfs_verify_fsbno(mp, be64_to_cpu(*ptr)))
+ return __this_address;
+ }
+
+ return NULL;
+}
+
+/*
+ * Verify a logged rt refcount btree root, as for the rt rmap btree root.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_rtrefcount_root(
+ struct xfs_mount *mp,
+ struct xfs_log_dinode *ldip,
+ void *src,
+ unsigned int size)
+{
+ struct xfs_btree_block *block = src;
+ xfs_failaddr_t fa;
+ unsigned int level;
+ unsigned int numrecs;
+ unsigned int i;
+
+ if (size < XFS_RTREFCOUNT_BLOCK_LEN)
+ return __this_address;
+
+ /*
+ * The maximum rtrefcount btree height depends on the filesystem
+ * geometry, which a growfs being recovered can change. The geometry
+ * this item will be replayed against is not known yet, so bound the
+ * root level by the largest height any geometry allows. The root is
+ * checked against the real geometry when the inode is loaded after
+ * recovery. Tighter validation depends on solving the recovery geometry
+ * problem described in the log recovery validation design document.
+ */
+ fa = xfs_btree_check_fsblock_root_hdr(mp, XFS_RTREFC_CRC_MAGIC,
+ xfs_rtrefcountbt_maxrecs(mp, size,
+ be16_to_cpu(block->bb_level) == 0),
+ block, 0, xfs_rtrefcountbt_maxlevels_ondisk() - 1);
+ if (fa)
+ return fa;
+
+ /*
+ * rtrefcount inodes can be dynamically created by growfs, resulting in
+ * a transient state on disk where they can have no contents. Only a
+ * level zero root may be empty.
+ */
+ level = be16_to_cpu(block->bb_level);
+ numrecs = be16_to_cpu(block->bb_numrecs);
+ if (level > 0 && numrecs == 0)
+ return __this_address;
+
+ if (size != xfs_rtrefcount_broot_space_calc(level, numrecs))
+ return __this_address;
+
+ /*
+ * A leaf root holds records whose bounds are relative to a realtime
+ * group. The realtime groups are not set up until long after log
+ * recovery runs, so the leaf contents cannot be validated here. That is
+ * deferred to the first runtime access to the root block.
+ */
+ if (level == 0)
+ return NULL;
+
+ for (i = 1; i <= numrecs; i++) {
+ xfs_rtrefcount_ptr_t *ptr = xfs_rtrefcount_broot_ptr_addr(mp,
+ block, i, size);
+
+ if (!xfs_verify_fsbno(mp, be64_to_cpu(*ptr)))
+ return __this_address;
+ }
+
+ return NULL;
+}
+
+/*
+ * Verify a logged metadata btree root. The metadata btree type is fixed by the
+ * inode, so it is chosen from the log dinode metadata type.
+ */
+static xfs_failaddr_t
+xlog_dinode_verify_metabtree_root(
+ struct xfs_mount *mp,
+ struct xfs_log_dinode *ldip,
+ void *src,
+ unsigned int size)
+{
+ /*
+ * growfs creates these inodes before it adds a realtime volume, so gate
+ * them on the btree feature rather than the realtime btree feature, as
+ * the root loaders do.
+ */
+ switch (ldip->di_metatype) {
+ case XFS_METAFILE_RTRMAP:
+ if (!xfs_has_rmapbt(mp))
+ return __this_address;
+ return xlog_dinode_verify_rtrmap_root(mp, ldip, src, size);
+ case XFS_METAFILE_RTREFCOUNT:
+ if (!xfs_has_reflink(mp))
+ return __this_address;
+ return xlog_dinode_verify_rtrefcount_root(mp, ldip, src, size);
+ default:
+ return __this_address;
+ }
+}
+
/*
* Verify a logged shortform symlink target. The logged target is the on-disk
* form, which is not null terminated, so this cannot use
@@ -635,7 +802,8 @@ xlog_dinode_verify_dfork(
case XFS_DINODE_FMT_META_BTREE:
if (field != XFS_ILOG_DBROOT)
return __this_address;
- break;
+ return xlog_dinode_verify_metabtree_root(mp, ldip,
+ item->ri_buf[2].iov_base, ilfp->ilf_dsize);
}
return NULL;
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 53/59] xfs: check logged btree fork roots fit in the inode fork during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (51 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 52/59] xfs: validate logged metadata " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 54/59] xfs: validate buffer item region count " Dave Chinner
` (5 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A btree format fork is logged as the expanded in-core btree root. Replay
converts it to the compact on-disk root as it copies it into the inode. The
conversion lays the root out using the capacity of the on-disk fork, then copies
as many keys and pointers as the logged root says it holds. Nothing bounds that
record count by what the fork can hold. A logged root that is otherwise
consistent but holds too many records is copied past the end of the fork, into
the rest of the inode cluster buffer.
Reject a logged btree root whose compact on-disk form does not fit within the
inode fork. This uses the same check as the runtime root loaders apply when they
read a root from disk.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_inode_item_recover.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/fs/xfs/xfs_inode_item_recover.c b/fs/xfs/xfs_inode_item_recover.c
index efb142efa0ef..6920aab9263a 100644
--- a/fs/xfs/xfs_inode_item_recover.c
+++ b/fs/xfs/xfs_inode_item_recover.c
@@ -478,8 +478,8 @@ xlog_dinode_verify_extents(
* length is fixed by the record count, so the region size fixes how many
* records the root can hold.
*
- * The region size has already been bounds checked against the inode fork size,
- * so it is known to fit within the fork and does not need rechecking here.
+ * The logged root is converted to the compact on-disk root when it is replayed
+ * into the inode, so it is the compact form that must fit within the fork.
*/
static xfs_failaddr_t
xlog_dinode_verify_bmbt_root(
@@ -515,6 +515,10 @@ xlog_dinode_verify_bmbt_root(
if (numrecs == 0 || size != xfs_bmap_broot_space_calc(mp, numrecs))
return __this_address;
+ if (xfs_bmdr_space_calc(numrecs) >
+ __xfs_inode_fork_size(mp, ldip->di_forkoff, whichfork))
+ return __this_address;
+
for (i = 1; i <= numrecs; i++) {
struct xfs_bmbt_key *key = xfs_bmbt_key_addr(mp, block, i);
xfs_bmbt_ptr_t *ptr = xfs_bmap_broot_ptr_addr(mp, block,
@@ -578,6 +582,11 @@ xlog_dinode_verify_rtrmap_root(
if (size != xfs_rtrmap_broot_space_calc(level, numrecs))
return __this_address;
+ /* The compact on-disk form of the root must fit within the fork. */
+ if (xfs_rtrmap_droot_space_calc(level, numrecs) >
+ __xfs_inode_fork_size(mp, ldip->di_forkoff, XFS_DATA_FORK))
+ return __this_address;
+
/*
* A leaf root holds records whose bounds are relative to a realtime
* group. The realtime groups are not set up until long after log
@@ -646,6 +655,11 @@ xlog_dinode_verify_rtrefcount_root(
if (size != xfs_rtrefcount_broot_space_calc(level, numrecs))
return __this_address;
+ /* The compact on-disk form of the root must fit within the fork. */
+ if (xfs_rtrefcount_droot_space_calc(level, numrecs) >
+ __xfs_inode_fork_size(mp, ldip->di_forkoff, XFS_DATA_FORK))
+ return __this_address;
+
/*
* A leaf root holds records whose bounds are relative to a realtime
* group. The realtime groups are not set up until long after log
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 54/59] xfs: validate buffer item region count during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (52 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 53/59] xfs: check logged btree fork roots fit in the inode fork " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 55/59] xfs: validate buffer item regions " Dave Chinner
` (4 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Validate the number of regions in a buffer log item as it is decoded from the
journal. A cancelled buffer logs only its format header, so the valid range
spans the whole range a log item can hold.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_buf_item_recover.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
index 383d0e87ef6a..3742099de2eb 100644
--- a/fs/xfs/xfs_buf_item_recover.c
+++ b/fs/xfs/xfs_buf_item_recover.c
@@ -1211,8 +1211,25 @@ xlog_recover_buf_commit_pass2(
return 0;
}
+/*
+ * A buffer item is a format header region followed by a region for each dirty
+ * range of the buffer. However, a cancelled buffer only logs a format header,
+ * hence the valid region count range spans the entire range a log item can
+ * legally have.
+ */
+static int
+xlog_recover_buf_nregions(
+ struct xlog *log,
+ uint16_t nregions)
+{
+ if (nregions < 1 || nregions > XLOG_MAX_REGIONS_IN_ITEM)
+ return -EFSCORRUPTED;
+ return nregions;
+}
+
const struct xlog_recover_item_ops xlog_buf_item_ops = {
.item_type = XFS_LI_BUF,
+ .validate_nregions = xlog_recover_buf_nregions,
.reorder = xlog_recover_buf_reorder,
.ra_pass2 = xlog_recover_buf_ra_pass2,
.commit_pass1 = xlog_recover_buf_commit_pass1,
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 55/59] xfs: validate buffer item regions during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (53 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 54/59] xfs: validate buffer item region count " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 56/59] xfs: validate the whole buffer item " Dave Chinner
` (3 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Validate each region of a buffer log item as it is decoded from the journal.
The format header must hold a dirty bitmap that fits within the region, and each
dirty range region must cover whole chunks of the buffer.
The fixed prefix of the format header is checked through min_hdr_len, so the
region check only has to bound the used part of the dirty bitmap against the
region.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_buf_item_recover.c | 46 +++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
index 3742099de2eb..8fb419aba094 100644
--- a/fs/xfs/xfs_buf_item_recover.c
+++ b/fs/xfs/xfs_buf_item_recover.c
@@ -1227,9 +1227,55 @@ xlog_recover_buf_nregions(
return nregions;
}
+/*
+ * Region 0 is the format header. The fixed prefix is already checked by
+ * min_hdr_len, so check that the used part of the dirty bitmap does not exceed
+ * the bitmap and fits within the region. Later regions are the dirty ranges of
+ * the buffer, each covering whole chunks of it. The region length has already
+ * been bounds checked against XLOG_MAX_REGION_SIZE during assembly, so only the
+ * lower bound and chunk alignment are checked here.
+ */
+static int
+xlog_recover_buf_validate_region(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ int region_index)
+{
+ size_t len = item->ri_buf[region_index].iov_len;
+
+ if (region_index == 0) {
+ struct xfs_buf_log_format *blf = item->ri_buf[0].iov_base;
+
+ if (blf->blf_map_size > XFS_BLF_DATAMAP_SIZE) {
+ xfs_warn(log->l_mp,
+ "BUF: bad dirty bitmap size %u recovered.",
+ blf->blf_map_size);
+ return -EFSCORRUPTED;
+ }
+ if (!xfs_buf_log_check_iovec(&item->ri_buf[0])) {
+ xfs_warn(log->l_mp,
+ "BUF: bad format region length %zu recovered.",
+ len);
+ return -EFSCORRUPTED;
+ }
+ return 0;
+ }
+
+ if (len == 0 || (len % XFS_BLF_CHUNK)) {
+ xfs_warn(log->l_mp,
+ "BUF: bad region %d length %zu recovered.",
+ region_index, len);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
const struct xlog_recover_item_ops xlog_buf_item_ops = {
.item_type = XFS_LI_BUF,
+ .min_hdr_len = offsetof(struct xfs_buf_log_format, blf_data_map),
.validate_nregions = xlog_recover_buf_nregions,
+ .validate_region = xlog_recover_buf_validate_region,
.reorder = xlog_recover_buf_reorder,
.ra_pass2 = xlog_recover_buf_ra_pass2,
.commit_pass1 = xlog_recover_buf_commit_pass1,
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 56/59] xfs: validate the whole buffer item during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (54 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 55/59] xfs: validate buffer item regions " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 57/59] xfs: remove redundant buffer checks from recovery replay Dave Chinner
` (2 subsequent siblings)
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
Validate a buffer log item once all its regions are decoded from the journal.
The rest of recovery can then trust it. The item size must match the number of
regions attached. The buffer must lie wholly within the data device.
A cancelled buffer is not replayed. It logs only its format header, so it must
be a single region. Nothing further is checked for a cancelled buffer.
Any other buffer must log at least one dirty region. It must record a valid
buffer type. A type flag, if set, must match the buffer type. The type flags
select special replay handling. A buffer logged for full replay carries only
its type, even if it holds inodes or dquots. V4 inode allocation and online
repair log buffers this way, so a missing type flag is valid. The dirty
regions must correspond to the dirty bitmap. They must not describe more than
a buffer's worth of data.
All CRC enabled filesystems record buffer types. Older V4 filesystems may log
buffers without a type. An unknown type is accepted only on those filesystems.
Such a buffer may have no more than one type flag set.
Historically, a dirty region that spanned a page boundary in a buffer could be
split into two logged regions. The runtime code has not triggered this split
logging in kernel git history, so we should never encounter it during recovery.
Hence each run of dirty bits must map to a single region.
The buffer block range is checked against the data device size held in the
buftarg rather than the superblock geometry. Deriving a shared daddr range
helper is left for later.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/scrub/inode_repair.c | 14 +++
fs/xfs/scrub/quota_repair.c | 7 ++
fs/xfs/xfs_buf_item_recover.c | 185 ++++++++++++++++++++++++++++++++++
3 files changed, 206 insertions(+)
diff --git a/fs/xfs/scrub/inode_repair.c b/fs/xfs/scrub/inode_repair.c
index 2dd3ea7fb361..3eff8cc66d1e 100644
--- a/fs/xfs/scrub/inode_repair.c
+++ b/fs/xfs/scrub/inode_repair.c
@@ -204,6 +204,13 @@ xrep_dinode_buf_core(
if (!unlinked_ok)
dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
xfs_dinode_calc_crc(mp, dip);
+
+ /*
+ * We don't mark the buffer with xfs_trans_inode_buf() as that would
+ * cause recovery to only replay unlinked list pointers. However, we
+ * still need to set the buffer type for recovery to correctly classify
+ * it as an inode buffer.
+ */
xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
xfs_trans_log_buf(tp, bp, ioffset,
ioffset + sizeof(struct xfs_dinode) - 1);
@@ -1632,6 +1639,13 @@ xrep_dinode_core(
/* Write out the inode. */
trace_xrep_dinode_fixed(sc, dip);
xfs_dinode_calc_crc(mp, dip);
+
+ /*
+ * We don't mark the buffer with xfs_trans_inode_buf() as that would
+ * cause recovery to only replay unlinked list pointers. However, we
+ * still need to set the buffer type for recovery to correctly classify
+ * it as an inode buffer.
+ */
xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_DINO_BUF);
xfs_trans_log_buf(sc->tp, bp, ri->imap.im_boffset,
ri->imap.im_boffset + mp->m_sb.sb_inodesize - 1);
diff --git a/fs/xfs/scrub/quota_repair.c b/fs/xfs/scrub/quota_repair.c
index 88e341d6d1af..8660cbf4683a 100644
--- a/fs/xfs/scrub/quota_repair.c
+++ b/fs/xfs/scrub/quota_repair.c
@@ -408,6 +408,13 @@ xrep_quota_block(
buftype = XFS_BLFT_PDQUOT_BUF;
break;
}
+
+ /*
+ * We don't mark the buffer with xfs_trans_dquot_buf() as that would
+ * cause recovery to skip replaying it when quotas are not in use.
+ * However, we still need to set the buffer type for recovery to
+ * correctly classify it as a dquot buffer.
+ */
xfs_trans_buf_set_type(sc->tp, bp, buftype);
xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
return xrep_roll_trans(sc);
diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
index 8fb419aba094..d8cec17bbb4e 100644
--- a/fs/xfs/xfs_buf_item_recover.c
+++ b/fs/xfs/xfs_buf_item_recover.c
@@ -1271,11 +1271,196 @@ xlog_recover_buf_validate_region(
return 0;
}
+/* The buffer type flags. A buffer has at most one of these set. */
+#define XFS_BLF_TYPE_MASK \
+ (XFS_BLF_INODE_BUF | XFS_BLF_UDQUOT_BUF | XFS_BLF_PDQUOT_BUF | \
+ XFS_BLF_GDQUOT_BUF)
+
+/*
+ * The buffer type is recorded both in the top bits of the format flags and in
+ * the type flags, and they describe different things. The type field
+ * describes the metadata held in the buffer. The type flags select special
+ * replay handling. An inode buffer flag replays only the unlinked list
+ * pointers, and a dquot buffer flag skips replay when that quota type is not
+ * in use. A buffer logged for full replay carries only its type, even if it
+ * holds inodes or dquots. V4 inode allocation and online repair log buffers
+ * this way.
+ *
+ * A buffer therefore carries at most one type flag, and if it has one it must
+ * be the one the type field describes.
+ */
+static xfs_failaddr_t
+xlog_recover_buf_verify_type(
+ struct xfs_mount *mp,
+ uint16_t blft,
+ unsigned int type)
+{
+ unsigned int expected;
+
+ /*
+ * All CRC enabled filesystems have buffer types defined. Older V4
+ * filesystems may not have types set, so allow an unknown type only in
+ * that case.
+ */
+ if (blft == XFS_BLFT_UNKNOWN_BUF && !xfs_has_crc(mp)) {
+ /*
+ * Only one inode or dquot buffer type flag can be set on a
+ * buffer. Without a buffer type there is nothing to validate
+ * the specific flag against, so only check that no more than
+ * one of them is set.
+ */
+ if (type & (type - 1))
+ return __this_address;
+ return NULL;
+ }
+
+ if (blft <= XFS_BLFT_UNKNOWN_BUF || blft >= XFS_BLFT_MAX_BUF)
+ return __this_address;
+
+ switch (blft) {
+ case XFS_BLFT_DINO_BUF:
+ expected = XFS_BLF_INODE_BUF;
+ break;
+ case XFS_BLFT_UDQUOT_BUF:
+ expected = XFS_BLF_UDQUOT_BUF;
+ break;
+ case XFS_BLFT_PDQUOT_BUF:
+ expected = XFS_BLF_PDQUOT_BUF;
+ break;
+ case XFS_BLFT_GDQUOT_BUF:
+ expected = XFS_BLF_GDQUOT_BUF;
+ break;
+ default:
+ expected = 0;
+ break;
+ }
+
+ if (type && type != expected)
+ return __this_address;
+ return NULL;
+}
+
+/*
+ * Validate the whole buffer item once all regions are assembled. The item size
+ * must match the number of regions attached. A cancelled buffer is not
+ * replayed, so it logs only the format header. Any other buffer must have a
+ * valid type, and its dirty regions must match the dirty bitmap and not
+ * describe more than a buffer's worth of data.
+ */
+static int
+xlog_recover_buf_validate_item(
+ struct xlog *log,
+ struct xlog_recover_item *item)
+{
+ struct xfs_buf_log_format *blf = item->ri_buf[0].iov_base;
+ xfs_daddr_t eofs = log->l_mp->m_ddev_targp->bt_nr_sectors;
+ xfs_failaddr_t fa;
+ size_t total = 0;
+ unsigned int ri = 1;
+ int first_bit;
+
+ if (blf->blf_size != item->ri_total) {
+ xfs_warn(log->l_mp,
+ "BUF: item size %u does not match region count %u recovered.",
+ blf->blf_size, item->ri_total);
+ return -EFSCORRUPTED;
+ }
+
+ /*
+ * The buffer length is an invariant: a buffer cannot be larger than the
+ * most data that can be logged in a single region.
+ */
+ if (blf->blf_len == 0 || BBTOB(blf->blf_len) > XLOG_MAX_REGION_SIZE) {
+ xfs_warn(log->l_mp,
+ "BUF: bad buffer length %u recovered.", blf->blf_len);
+ return -EFSCORRUPTED;
+ }
+
+ /*
+ * The buffer must lie wholly within the data device. Guard against the
+ * end wrapping so an out of range buffer cannot pass the bounds check.
+ */
+ if (blf->blf_blkno < 0 ||
+ blf->blf_blkno >= eofs ||
+ blf->blf_blkno + blf->blf_len < blf->blf_blkno ||
+ blf->blf_blkno + blf->blf_len > eofs) {
+ xfs_warn(log->l_mp,
+ "BUF: bad buffer block 0x%llx length %u recovered.",
+ blf->blf_blkno, blf->blf_len);
+ return -EFSCORRUPTED;
+ }
+
+ /*
+ * A cancelled buffer overrides everything else. It is not replayed, so
+ * it logs only the format header regardless of what the dirty bitmap
+ * holds, and nothing else needs to be checked.
+ */
+ if (blf->blf_flags & XFS_BLF_CANCEL) {
+ if (blf->blf_size != 1) {
+ xfs_warn(log->l_mp,
+ "BUF: cancelled buffer has %u regions recovered.",
+ blf->blf_size);
+ return -EFSCORRUPTED;
+ }
+ return 0;
+ }
+
+ /* A buffer that is not cancelled always logs some dirty data. */
+ if (item->ri_total < 2) {
+ xfs_warn(log->l_mp,
+ "BUF: buffer has no dirty regions recovered.");
+ return -EFSCORRUPTED;
+ }
+
+ fa = xlog_recover_buf_verify_type(log->l_mp, xfs_blft_from_flags(blf),
+ blf->blf_flags & XFS_BLF_TYPE_MASK);
+ if (fa) {
+ xfs_warn(log->l_mp,
+ "BUF: type %u and flags 0x%x disagree recovered at %pS",
+ xfs_blft_from_flags(blf), blf->blf_flags, fa);
+ return -EFSCORRUPTED;
+ }
+
+ /*
+ * Each dirty region covers one contiguous run of set bits in the dirty
+ * bitmap. Walk the bitmap and the regions together: each region size
+ * must match the length of the run it covers, each run must lie within
+ * the buffer, and the regions must exactly cover the bitmap. The dirty
+ * data across all regions cannot be larger than the buffer itself.
+ */
+ first_bit = xfs_next_bit(blf->blf_data_map, blf->blf_map_size, 0);
+ while (first_bit != -1) {
+ int nbits = xfs_contig_bits(blf->blf_data_map,
+ blf->blf_map_size, first_bit);
+
+ if (ri >= item->ri_total ||
+ item->ri_buf[ri].iov_len != (nbits << XFS_BLF_SHIFT) ||
+ ((first_bit + nbits) << XFS_BLF_SHIFT) > BBTOB(blf->blf_len))
+ goto bad_map;
+
+ total += item->ri_buf[ri].iov_len;
+ ri++;
+ first_bit = xfs_next_bit(blf->blf_data_map, blf->blf_map_size,
+ first_bit + nbits + 1);
+ }
+
+ if (ri != item->ri_total || total > BBTOB(blf->blf_len))
+ goto bad_map;
+
+ return 0;
+
+bad_map:
+ xfs_warn(log->l_mp,
+ "BUF: dirty bitmap does not match regions recovered.");
+ return -EFSCORRUPTED;
+}
+
const struct xlog_recover_item_ops xlog_buf_item_ops = {
.item_type = XFS_LI_BUF,
.min_hdr_len = offsetof(struct xfs_buf_log_format, blf_data_map),
.validate_nregions = xlog_recover_buf_nregions,
.validate_region = xlog_recover_buf_validate_region,
+ .validate_item = xlog_recover_buf_validate_item,
.reorder = xlog_recover_buf_reorder,
.ra_pass2 = xlog_recover_buf_ra_pass2,
.commit_pass1 = xlog_recover_buf_commit_pass1,
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 57/59] xfs: remove redundant buffer checks from recovery replay
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (55 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 56/59] xfs: validate the whole buffer item " Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 58/59] xfs: fail recovery on a buffer type mismatch Dave Chinner
2026-11-16 23:15 ` [PATCH 59/59] xfs: validate logged dquot buffers during recovery Dave Chinner
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
The buffer item is now fully validated as it is decoded from the journal. The
replay paths can therefore trust its regions and dirty bitmap. The bitmap and
region consistency checks scattered through the buffer and inode buffer replay
loops are now redundant.
Remove the region and bitmap checks from the replay loops. The checks on buffer
contents remain, because they cannot be validated from the log item alone.
Validation now requires each run of dirty bits to map to a single logged region.
Replay therefore no longer needs to handle a dirty region split into two logged
regions. The code that trimmed each run to the length of the logged region being
copied is removed as well.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_buf_item_recover.c | 40 -----------------------------------
1 file changed, 40 deletions(-)
diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
index d8cec17bbb4e..bedaa25fa12e 100644
--- a/fs/xfs/xfs_buf_item_recover.c
+++ b/fs/xfs/xfs_buf_item_recover.c
@@ -487,38 +487,6 @@ xlog_recover_do_reg_buffer(
break;
nbits = xfs_contig_bits(buf_f->blf_data_map,
buf_f->blf_map_size, bit);
- ASSERT(nbits > 0);
- ASSERT(item->ri_buf[i].iov_base != NULL);
- ASSERT(item->ri_buf[i].iov_len % XFS_BLF_CHUNK == 0);
- /*
- * The bitmap is only trustworthy to the extent that it
- * describes a region that actually fits inside the buffer we
- * read in based on the (attacker-controlled) blf_len. Do not
- * rely on an ASSERT() for this -- it compiles away entirely on
- * non-DEBUG kernels, which is exactly where this matters, so
- * validate it for real and abort recovery of this buffer rather
- * than copying past the end of it.
- */
- if (XFS_IS_CORRUPT(mp, BBTOB(bp->b_length) <
- ((uint)bit << XFS_BLF_SHIFT) +
- (nbits << XFS_BLF_SHIFT))) {
- xfs_alert(mp,
- "Bad buffer log item dirty bitmap (bit %d, nbits %d) for %d-byte buffer at daddr 0x%llx.",
- bit, nbits, BBTOB(bp->b_length),
- xfs_buf_daddr(bp));
- return -EFSCORRUPTED;
- }
-
- /*
- * The dirty regions logged in the buffer, even though
- * contiguous, may span multiple chunks. This is because the
- * dirty region may span a physical page boundary in a buffer
- * and hence be split into two separate vectors for writing into
- * the log. Hence we need to trim nbits back to the length of
- * the current region being copied out of the log.
- */
- if (item->ri_buf[i].iov_len < (nbits << XFS_BLF_SHIFT))
- nbits = item->ri_buf[i].iov_len >> XFS_BLF_SHIFT;
/*
* Do a sanity check if this is a dquot buffer. Just checking
@@ -557,9 +525,6 @@ xlog_recover_do_reg_buffer(
bit += nbits;
}
- /* Shouldn't be any more regions */
- ASSERT(i == item->ri_total);
-
xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
return 0;
}
@@ -676,7 +641,6 @@ xlog_recover_do_inode_buffer(
nbits = xfs_contig_bits(buf_f->blf_data_map,
buf_f->blf_map_size, bit);
- ASSERT(nbits > 0);
reg_buf_offset = bit << XFS_BLF_SHIFT;
reg_buf_bytes = nbits << XFS_BLF_SHIFT;
item_index++;
@@ -690,10 +654,6 @@ xlog_recover_do_inode_buffer(
if (next_unlinked_offset < reg_buf_offset)
continue;
- ASSERT(item->ri_buf[item_index].iov_base != NULL);
- ASSERT((item->ri_buf[item_index].iov_len % XFS_BLF_CHUNK) == 0);
- ASSERT((reg_buf_offset + reg_buf_bytes) <= BBTOB(bp->b_length));
-
/*
* The current logged region contains a copy of the
* current di_next_unlinked field. Extract its value
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 58/59] xfs: fail recovery on a buffer type mismatch
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (56 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 57/59] xfs: remove redundant buffer checks from recovery replay Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
2026-11-16 23:15 ` [PATCH 59/59] xfs: validate logged dquot buffers during recovery Dave Chinner
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
When recovering a buffer, xlog_recover_validate_buf_type() checks that the
buffer type recorded in the log item agrees with the magic number of the
buffer being replayed into. A mismatch left bp->b_ops unset and was only
reported with a warning and an ASSERT. On a production kernel recovery then
queued the buffer with no verifier attached, so xfs_buf_verify_write() warned
about the missing operations but still wrote the mismatched recovery data to
disk.
Return an error from xlog_recover_validate_buf_type() on a type mismatch
instead of asserting. The replay path propagates the error so recovery fails
rather than writing out an unverifiable buffer. The skip path, where the
buffer on disk is newer than the log item being recovered, ignores the result:
later changes to that buffer are still to be replayed and may change its type,
so a mismatch against the current log item is expected there and must not warn.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/xfs_buf_item_recover.c | 23 ++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)
diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
index bedaa25fa12e..32c7bf16378a 100644
--- a/fs/xfs/xfs_buf_item_recover.c
+++ b/fs/xfs/xfs_buf_item_recover.c
@@ -214,7 +214,7 @@ xlog_recover_buf_commit_pass1(
* the first 32 bits of the buffer (most blocks),
* inside a struct xfs_da_blkinfo at the start of the buffer.
*/
-static void
+static int
xlog_recover_validate_buf_type(
struct xfs_mount *mp,
struct xfs_buf *bp,
@@ -236,7 +236,7 @@ xlog_recover_validate_buf_type(
* just avoid the verification stage for non-crc filesystems
*/
if (!xfs_has_crc(mp))
- return;
+ return 0;
magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
@@ -426,14 +426,22 @@ xlog_recover_validate_buf_type(
/*
* Nothing else to do in the case of a NULL current LSN as this means
* the buffer is more recent than the change in the log and will be
- * skipped.
+ * skipped. The buffer contents are newer than this format, so later
+ * changes to this buffer still to be replayed may change its type. A
+ * mismatch against this format is therefore expected and not a corruption.
*/
if (current_lsn == NULLCOMMITLSN)
- return;
+ return 0;
+ /*
+ * The buffer type does not match the magic number in the buffer we are
+ * about to replay into. The write verifier keyed off the type would
+ * reject it, so fail recovery rather than submit a buffer that cannot be
+ * verified.
+ */
if (warnmsg) {
xfs_warn(mp, warnmsg);
- ASSERT(0);
+ return -EFSCORRUPTED;
}
/*
@@ -454,6 +462,8 @@ xlog_recover_validate_buf_type(
bip = bp->b_log_item;
bip->bli_item.li_lsn = current_lsn;
}
+
+ return 0;
}
/*
@@ -525,8 +535,7 @@ xlog_recover_do_reg_buffer(
bit += nbits;
}
- xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
- return 0;
+ return xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
}
/*
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
* [PATCH 59/59] xfs: validate logged dquot buffers during recovery
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
` (57 preceding siblings ...)
2026-11-16 23:15 ` [PATCH 58/59] xfs: fail recovery on a buffer type mismatch Dave Chinner
@ 2026-11-16 23:15 ` Dave Chinner
58 siblings, 0 replies; 60+ messages in thread
From: Dave Chinner @ 2026-11-16 23:15 UTC (permalink / raw)
To: linux-xfs; +Cc: cem
A buffer with a dquot type flag is only ever logged by
xfs_qm_init_dquot_blk(), which initialises and logs the whole buffer in a
single region. So such a buffer has exactly two regions: the format header,
and one data region covering the entire buffer that holds a full chunk of
valid dquots. Online repair also logs whole dquot buffers, but without the
type flag, so they are not checked here.
Validate this in the buffer item validate_item callback. A buffer carrying a
dquot type flag must have that shape, and every dquot in the logged chunk is
verified so a corrupt dquot is never replayed to disk. The dquot content check
in the replay path only looked at the first dquot and relied on the replay loop
for the region bounds, so it is removed and the replay loop reduces to copying
the logged regions into the buffer.
To share the per-dquot verification, xfs_dquot_verify_chunk() is factored out of
xfs_dquot_buf_verify(). It walks a chunk of dquots and, on failure, updates its
dquot pointer to the offending dquot so the caller can report it. The buffer
read verifier is otherwise unchanged.
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
fs/xfs/libxfs/xfs_dquot_buf.c | 64 ++++++++++++++++-------------
fs/xfs/libxfs/xfs_quota_defs.h | 2 +
fs/xfs/xfs_buf_item_recover.c | 73 +++++++++++++++++++---------------
3 files changed, 81 insertions(+), 58 deletions(-)
diff --git a/fs/xfs/libxfs/xfs_dquot_buf.c b/fs/xfs/libxfs/xfs_dquot_buf.c
index 6c8a9afdfb1c..d6837c840989 100644
--- a/fs/xfs/libxfs/xfs_dquot_buf.c
+++ b/fs/xfs/libxfs/xfs_dquot_buf.c
@@ -179,6 +179,38 @@ xfs_dquot_buf_verify_crc(
return true;
}
+/*
+ * Verify every dquot in a full chunk of dquots. We don't know what the id of
+ * the first dquot is supposed to be, just that they should be increasing
+ * monotonically within the chunk. If the first id is corrupt, then it will fail
+ * on the second dquot in the chunk so corruptions could point to the wrong
+ * dquot in this case. On failure @dqb is updated to point at the failing dquot
+ * so the caller can report it.
+ */
+xfs_failaddr_t
+xfs_dquot_verify_chunk(
+ struct xfs_mount *mp,
+ struct xfs_dqblk **dqb,
+ unsigned int ndquots)
+{
+ xfs_failaddr_t fa;
+ xfs_dqid_t id = 0;
+ unsigned int i;
+
+ for (i = 0; i < ndquots; i++) {
+ if (i == 0)
+ id = be32_to_cpu((*dqb)[i].dd_diskdq.d_id);
+
+ fa = xfs_dqblk_verify(mp, &(*dqb)[i], id + i);
+ if (fa) {
+ *dqb = &(*dqb)[i];
+ return fa;
+ }
+ }
+
+ return NULL;
+}
+
STATIC xfs_failaddr_t
xfs_dquot_buf_verify(
struct xfs_mount *mp,
@@ -187,9 +219,7 @@ xfs_dquot_buf_verify(
{
struct xfs_dqblk *dqb = bp->b_addr;
xfs_failaddr_t fa;
- xfs_dqid_t id = 0;
int ndquots;
- int i;
/*
* if we are in log recovery, the quota subsystem has not been
@@ -201,32 +231,12 @@ xfs_dquot_buf_verify(
else
ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
- /*
- * On the first read of the buffer, verify that each dquot is valid.
- * We don't know what the id of the dquot is supposed to be, just that
- * they should be increasing monotonically within the buffer. If the
- * first id is corrupt, then it will fail on the second dquot in the
- * buffer so corruptions could point to the wrong dquot in this case.
- */
- for (i = 0; i < ndquots; i++) {
- struct xfs_disk_dquot *ddq;
+ fa = xfs_dquot_verify_chunk(mp, &dqb, ndquots);
+ if (fa && !readahead)
+ xfs_buf_verifier_error(bp, -EFSCORRUPTED, __func__, dqb,
+ sizeof(struct xfs_dqblk), fa);
- ddq = &dqb[i].dd_diskdq;
-
- if (i == 0)
- id = be32_to_cpu(ddq->d_id);
-
- fa = xfs_dqblk_verify(mp, &dqb[i], id + i);
- if (fa) {
- if (!readahead)
- xfs_buf_verifier_error(bp, -EFSCORRUPTED,
- __func__, &dqb[i],
- sizeof(struct xfs_dqblk), fa);
- return fa;
- }
- }
-
- return NULL;
+ return fa;
}
static xfs_failaddr_t
diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
index 551d7ae46c5c..73d82edf0969 100644
--- a/fs/xfs/libxfs/xfs_quota_defs.h
+++ b/fs/xfs/libxfs/xfs_quota_defs.h
@@ -132,6 +132,8 @@ extern xfs_failaddr_t xfs_dquot_verify(struct xfs_mount *mp,
struct xfs_disk_dquot *ddq, xfs_dqid_t id);
extern xfs_failaddr_t xfs_dqblk_verify(struct xfs_mount *mp,
struct xfs_dqblk *dqb, xfs_dqid_t id);
+xfs_failaddr_t xfs_dquot_verify_chunk(struct xfs_mount *mp,
+ struct xfs_dqblk **dqb, unsigned int ndquots);
extern int xfs_calc_dquots_per_chunk(unsigned int nbblks);
extern void xfs_dqblk_repair(struct xfs_mount *mp, struct xfs_dqblk *dqb,
xfs_dqid_t id, xfs_dqtype_t type);
diff --git a/fs/xfs/xfs_buf_item_recover.c b/fs/xfs/xfs_buf_item_recover.c
index 32c7bf16378a..7be00f0e6b34 100644
--- a/fs/xfs/xfs_buf_item_recover.c
+++ b/fs/xfs/xfs_buf_item_recover.c
@@ -483,8 +483,6 @@ xlog_recover_do_reg_buffer(
int i;
int bit;
int nbits;
- xfs_failaddr_t fa;
- const size_t size_disk_dquot = sizeof(struct xfs_disk_dquot);
trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
@@ -498,39 +496,10 @@ xlog_recover_do_reg_buffer(
nbits = xfs_contig_bits(buf_f->blf_data_map,
buf_f->blf_map_size, bit);
- /*
- * Do a sanity check if this is a dquot buffer. Just checking
- * the first dquot in the buffer should do. XXXThis is
- * probably a good thing to do for other buf types also.
- */
- fa = NULL;
- if (buf_f->blf_flags &
- (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
- if (item->ri_buf[i].iov_base == NULL) {
- xfs_alert(mp,
- "XFS: NULL dquot in %s.", __func__);
- goto next;
- }
- if (item->ri_buf[i].iov_len < size_disk_dquot) {
- xfs_alert(mp,
- "XFS: dquot too small (%zd) in %s.",
- item->ri_buf[i].iov_len, __func__);
- goto next;
- }
- fa = xfs_dquot_verify(mp, item->ri_buf[i].iov_base, -1);
- if (fa) {
- xfs_alert(mp,
- "dquot corrupt at %pS trying to replay into block 0x%llx",
- fa, xfs_buf_daddr(bp));
- goto next;
- }
- }
-
memcpy(xfs_buf_offset(bp,
(uint)bit << XFS_BLF_SHIFT), /* dest */
item->ri_buf[i].iov_base, /* source */
nbits<<XFS_BLF_SHIFT); /* length */
- next:
i++;
bit += nbits;
}
@@ -1309,6 +1278,44 @@ xlog_recover_buf_verify_type(
return NULL;
}
+/*
+ * A buffer with a dquot type flag is only logged by xfs_qm_init_dquot_blk(),
+ * which initialises and logs the entire buffer in one go. Online repair also
+ * logs whole dquot buffers, but without the type flag, so they are replayed as
+ * regular buffers and are not checked here. So a logged dquot buffer has
+ * exactly one data region covering the whole buffer, and that region is a full
+ * chunk of valid dquots. Reject anything else, then verify every dquot in the
+ * logged chunk so a corrupt dquot is not replayed onto disk.
+ */
+static int
+xlog_recover_buf_validate_dquot(
+ struct xlog *log,
+ struct xlog_recover_item *item,
+ struct xfs_buf_log_format *blf)
+{
+ struct xfs_dqblk *dqb;
+ xfs_failaddr_t fa;
+ unsigned int ndquots;
+
+ if (item->ri_total != 2 ||
+ item->ri_buf[1].iov_len != BBTOB(blf->blf_len)) {
+ xfs_warn(log->l_mp,
+ "BUF: dquot buffer is not a single whole region recovered.");
+ return -EFSCORRUPTED;
+ }
+ dqb = item->ri_buf[1].iov_base;
+
+ ndquots = xfs_calc_dquots_per_chunk(blf->blf_len);
+ fa = xfs_dquot_verify_chunk(log->l_mp, &dqb, ndquots);
+ if (fa) {
+ xfs_warn(log->l_mp,
+ "BUF: dquot at %pS failed validation recovered.", fa);
+ return -EFSCORRUPTED;
+ }
+
+ return 0;
+}
+
/*
* Validate the whole buffer item once all regions are assembled. The item size
* must match the number of regions attached. A cancelled buffer is not
@@ -1416,6 +1423,10 @@ xlog_recover_buf_validate_item(
if (ri != item->ri_total || total > BBTOB(blf->blf_len))
goto bad_map;
+ if (blf->blf_flags &
+ (XFS_BLF_UDQUOT_BUF | XFS_BLF_PDQUOT_BUF | XFS_BLF_GDQUOT_BUF))
+ return xlog_recover_buf_validate_dquot(log, item, blf);
+
return 0;
bad_map:
--
2.55.0
^ permalink raw reply related [flat|nested] 60+ messages in thread
end of thread, other threads:[~2026-11-16 23:18 UTC | newest]
Thread overview: 60+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 01/59] xfs: set the unmount record operation header length Dave Chinner
2026-11-16 23:14 ` [PATCH 02/59] xfs: fix the rt rmap btree maximum level checks Dave Chinner
2026-11-16 23:14 ` [PATCH 03/59] xfs: allow an empty rt refcount btree root before the rt volume exists Dave Chinner
2026-11-16 23:14 ` [PATCH 04/59] xfs: recompute the rt btree maxlevels when growfs changes them Dave Chinner
2026-11-16 23:14 ` [PATCH 05/59] xfs: recompute the transaction reservations after growfs Dave Chinner
2026-11-16 23:14 ` [PATCH 06/59] xfs: skip missing groups when freeing them Dave Chinner
2026-11-16 23:14 ` [PATCH 07/59] xfs: update the rt geometry when recovering a superblock change Dave Chinner
2026-11-16 23:14 ` [PATCH 08/59] xfs: reinitialise the filesystem geometry after log recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 09/59] xfs: apply mount superblock updates " Dave Chinner
2026-11-16 23:14 ` [PATCH 10/59] xfs: check the atomic write reservation against the current log size Dave Chinner
2026-11-16 23:14 ` [PATCH 11/59] xfs: fix the bmap btree maximum height calculation Dave Chinner
2026-11-16 23:14 ` [PATCH 12/59] xfs: add log recovery item validation design document Dave Chinner
2026-11-16 23:14 ` [PATCH 13/59] xfs: lift transaction header parsing out of the region assembly Dave Chinner
2026-11-16 23:14 ` [PATCH 14/59] xfs: track transaction header decode state explicitly Dave Chinner
2026-11-16 23:14 ` [PATCH 15/59] xfs: track the log item being rebuilt with an explicit pointer Dave Chinner
2026-11-16 23:14 ` [PATCH 16/59] xfs: complete log items explicitly during recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 17/59] xfs: add log item validation infrastructure to log recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 18/59] xfs: recover the transaction header as a validated item type Dave Chinner
2026-11-16 23:14 ` [PATCH 19/59] xfs: factor out xlog_recover_validate_ophdr() Dave Chinner
2026-11-16 23:14 ` [PATCH 20/59] xfs: validate operation header length and alignment during recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 21/59] xfs: validate the operation header padding " Dave Chinner
2026-11-16 23:14 ` [PATCH 22/59] xfs: validate the operation header flags " Dave Chinner
2026-11-16 23:14 ` [PATCH 23/59] xfs: enforce zero length control records " Dave Chinner
2026-11-16 23:14 ` [PATCH 24/59] xfs: validate the unmount record " Dave Chinner
2026-11-16 23:14 ` [PATCH 25/59] xfs: handle zero length continuation op headers " Dave Chinner
2026-11-16 23:14 ` [PATCH 26/59] xfs: bound the assembled region size " Dave Chinner
2026-11-16 23:14 ` [PATCH 27/59] xfs: dump the corrupt region when log item validation fails Dave Chinner
2026-11-16 23:14 ` [PATCH 28/59] xfs: validate inode create and quota off log items during recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 29/59] xfs: validate extent free " Dave Chinner
2026-11-16 23:14 ` [PATCH 30/59] xfs: validate and convert extent free intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 31/59] xfs: validate and convert extent free done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 32/59] xfs: validate rmap update intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 33/59] xfs: validate rmap update done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 34/59] xfs: validate refcount update intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 35/59] xfs: validate refcount update done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 36/59] xfs: validate bmap update intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 37/59] xfs: validate bmap update done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 38/59] xfs: validate mapping exchange intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 39/59] xfs: validate mapping exchange done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 40/59] xfs: validate dquot " Dave Chinner
2026-11-16 23:14 ` [PATCH 41/59] xfs: validate attr done " Dave Chinner
2026-11-16 23:14 ` [PATCH 42/59] xfs: validate attr intents " Dave Chinner
2026-11-16 23:15 ` [PATCH 43/59] xfs: convert inode items to host format " Dave Chinner
2026-11-16 23:15 ` [PATCH 44/59] xfs: allow xfs_imap() to map without an inode btree lookup Dave Chinner
2026-11-16 23:15 ` [PATCH 45/59] xfs: validate the inode item format header during recovery Dave Chinner
2026-11-16 23:15 ` [PATCH 46/59] xfs: validate the logged inode core " Dave Chinner
2026-11-16 23:15 ` [PATCH 47/59] xfs: validate the logged inode forks " Dave Chinner
2026-11-16 23:15 ` [PATCH 48/59] xfs: validate logged local fork contents " Dave Chinner
2026-11-16 23:15 ` [PATCH 49/59] xfs: validate logged extent " Dave Chinner
2026-11-16 23:15 ` [PATCH 50/59] xfs: allow a long format btree root to be checked without a cursor Dave Chinner
2026-11-16 23:15 ` [PATCH 51/59] xfs: validate logged btree fork roots during recovery Dave Chinner
2026-11-16 23:15 ` [PATCH 52/59] xfs: validate logged metadata " Dave Chinner
2026-11-16 23:15 ` [PATCH 53/59] xfs: check logged btree fork roots fit in the inode fork " Dave Chinner
2026-11-16 23:15 ` [PATCH 54/59] xfs: validate buffer item region count " Dave Chinner
2026-11-16 23:15 ` [PATCH 55/59] xfs: validate buffer item regions " Dave Chinner
2026-11-16 23:15 ` [PATCH 56/59] xfs: validate the whole buffer item " Dave Chinner
2026-11-16 23:15 ` [PATCH 57/59] xfs: remove redundant buffer checks from recovery replay Dave Chinner
2026-11-16 23:15 ` [PATCH 58/59] xfs: fail recovery on a buffer type mismatch Dave Chinner
2026-11-16 23:15 ` [PATCH 59/59] xfs: validate logged dquot buffers during recovery Dave Chinner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).