Validate journal log items during log recovery.
Subsystems (2)
v2 cover Validate journal log items during log recovery.
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/[email protected]/
- 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(-)
PATCH v2 1/59 xfs: set the unmount record operation header length
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.
--- fs/xfs/xfs_log.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
fs/xfs/xfs_log.c +16 -0
@@ -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 = {
Signature · 1 line
2.55.0
Re: [PATCH 01/59] xfs: set the unmount record operation header length
Quoted 14 lines · Dave Chinner
The unmount record is written to the log with a hand built operation header passed directly to
xlog_write()throughxlog_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: introducexlog_write_full()") the operation header length was rewritten correctly byxlog_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.
Quoted 4 lines
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.
The proper fix here is of course to automatically keep the length in sync. I have a series that does this, but if we can get the quick fix in before I won't complain.
fs/xfs/xfs_log.c xlog_write_one_vec(
Quoted 7 lines
fs/xfs/xfs_log.c +5
-0+/* +* The caller builds the operation header itself, and xlog_write() no +* longer rewrites the length when the region is written whole. Make sure +"%s: region length %u does not match op header length %u", +.oh_len = cpu_to_be32(sizeof(struct xfs_unmount_log_format)),
Please fix the various overly long lines.
PATCH v2 2/59 xfs: fix the rt rmap btree maximum level checks
In reply to Dave Chinnerm_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.
--- fs/xfs/libxfs/xfs_rtrmap_btree.c | 29 ++++++++++++++++++++++++----- fs/xfs/scrub/inode_repair.c | 7 ++++++- 2 files changed, 30 insertions(+), 6 deletions(-)
fs/xfs/libxfs/xfs_rtrmap_btree.c +24 -5
@@ -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. */
fs/xfs/scrub/inode_repair.c +6 -1
@@ -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 )
Signature · 1 line
2.55.0
Re: [PATCH 02/59] xfs: fix the rt rmap btree maximum level checks
Quoted 5 lines · Dave Chinner
m_rtrmap_maxlevelsis the maximum height of the rt rmap btree, including the inode root level. The deepest valid root level is therefore one less thanm_rtrmap_maxlevels. The root loader, the block verifier and the inode repair code all reject only levels greater thanm_rtrmap_maxlevels, so they accept a root one level too deep.
Please fix your commit message to stick to the usual 73 (or 75 by some definition) limit.
Otherwise looks good.
PATCH v2 3/59 xfs: allow an empty rt refcount btree root before the rt volume exists
In reply to Dave ChinnerThere 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.
--- fs/xfs/libxfs/xfs_rtrefcount_btree.c | 27 +++++++++++++++++++++++---- fs/xfs/scrub/inode_repair.c | 7 ++++++- 2 files changed, 29 insertions(+), 5 deletions(-)
fs/xfs/libxfs/xfs_rtrefcount_btree.c +23 -4
@@ -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 ; }/*
fs/xfs/scrub/inode_repair.c +6 -1
@@ -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 )
Signature · 1 line
2.55.0
Re: [PATCH 03/59] xfs: allow an empty rt refcount btree root before the rt volume exists
Looks good modulo the commit message issue reported before (not going to mention it for any additional patch).
PATCH v2 4/59 xfs: recompute the rt btree maxlevels when growfs changes them
In reply to Dave Chinnerm_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.
--- 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(-)
fs/xfs/xfs_fsops.c +1 -0
@@ -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;
fs/xfs/xfs_mount.c +0 -9
@@ -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
fs/xfs/xfs_mount.h +9 -0
@@ -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);
fs/xfs/xfs_rtalloc.c +2 -0
@@ -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 ;
Signature · 1 line
2.55.0
PATCH v2 5/59 xfs: recompute the transaction reservations after growfs
In reply to Dave ChinnerGrowfs 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.
--- 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(-)
fs/xfs/libxfs/xfs_trans_resv.c +27 -0
@@ -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.
fs/xfs/libxfs/xfs_trans_resv.h +1 -0
@@ -97,6 +97,7 @@ struct xfs_trans_resv {#define XFS_WRITE_LOG_COUNT_REFLINK 8void 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,
fs/xfs/xfs_fsops.c +5 -1
@@ -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;
fs/xfs/xfs_rtalloc.c +6 -7
@@ -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 ;
Signature · 1 line
2.55.0
PATCH v2 6/59 xfs: skip missing groups when freeing them
In reply to Dave ChinnerRuntime 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.
--- fs/xfs/libxfs/xfs_group.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/xfs/libxfs/xfs_group.c +9 -0
@@ -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);
Signature · 1 line
2.55.0
PATCH v2 7/59 xfs: update the rt geometry when recovering a superblock change
In reply to Dave ChinnerRecovering 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.
--- fs/xfs/xfs_buf_item_recover.c | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
fs/xfs/xfs_buf_item_recover.c +38 -0
@@ -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 ; }
Signature · 1 line
2.55.0
Re: [PATCH 07/59] xfs: update the rt geometry when recovering a superblock change
Quoted 11 lines · Dave Chinner
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.
Do you have a testcase for this? I'd hate to add all this code without actually being exercise as that introduces the chance of even more issues due to untested code.
Otherwise this does look sane.
PATCH v2 8/59 xfs: reinitialise the filesystem geometry after log recovery
In reply to Dave ChinnerLog 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.
--- fs/xfs/xfs_log_recover.c | 27 ------------------ fs/xfs/xfs_mount.c | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 27 deletions(-)
fs/xfs/xfs_log_recover.c +0 -27
@@ -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 ;
fs/xfs/xfs_mount.c +59 -0
71 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 9/59 xfs: apply mount superblock updates after log recovery
In reply to Dave ChinnerMount 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.
--- fs/xfs/xfs_mount.c | 79 +++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 33 deletions(-)
fs/xfs/xfs_mount.c +46 -33
@@ -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
Signature · 1 line
2.55.0
Re: [PATCH 09/59] xfs: apply mount superblock updates after log recovery
Similar to the previous one. I really worry about issues with this if we don't exercise it. And sashiko seems to worry as well.
PATCH v2 10/59 xfs: check the atomic write reservation against the current log size
In reply to Dave ChinnerChanging 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.
--- 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(-)
fs/xfs/libxfs/xfs_log_rlimit.c +45 -0
@@ -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); +}
fs/xfs/libxfs/xfs_shared.h +1 -0
@@ -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,
fs/xfs/libxfs/xfs_trans_resv.c +38 -7
73 lines not on this page · raw message
fs/xfs/xfs_trace.h +34 -0
@@ -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),
Signature · 1 line
2.55.0
Re: [PATCH 10/59] xfs: check the atomic write reservation against the current log size
Please split this out to a standalone patch and include John and the folks have the other atomic reservation patches out right now.
PATCH v2 11/59 xfs: fix the bmap btree maximum height calculation
In reply to Dave Chinnerm_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.
--- 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(-)
fs/xfs/libxfs/xfs_bmap.c +44 -4
@@ -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 */
fs/xfs/libxfs/xfs_bmap.h +2 -0
@@ -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,
fs/xfs/libxfs/xfs_bmap_btree.c +1 -1
@@ -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 ]);
fs/xfs/libxfs/xfs_inode_fork.c +1 -1
@@ -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,
fs/xfs/libxfs/xfs_log_rlimit.c +43 -23
122 lines not on this page · raw message
fs/xfs/scrub/inode_repair.c +1 -1
@@ -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 );
Signature · 1 line
2.55.0
PATCH v2 12/59 xfs: add log recovery item validation design document
In reply to Dave ChinnerLog 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.
--- 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
Documentation/filesystems/xfs/index.rst +1 -0
@@ -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
Documentation/filesystems/xfs/xfs-log-recovery-validation-design.rst new file +672 -0
672 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 13/59 xfs: lift transaction header parsing out of the region assembly
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_log_recover.c | 113 +++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 40 deletions(-)
fs/xfs/xfs_log_recover.c +73 -40
@@ -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;
Signature · 1 line
2.55.0
PATCH v2 14/59 xfs: track transaction header decode state explicitly
In reply to Dave ChinnerRecovery 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.
--- fs/xfs/libxfs/xfs_log_recover.h | 1 + fs/xfs/xfs_log_recover.c | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-)
fs/xfs/libxfs/xfs_log_recover.h +1 -0
@@ -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)
fs/xfs/xfs_log_recover.c +14 -10
@@ -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 );
Signature · 1 line
2.55.0
PATCH v2 15/59 xfs: track the log item being rebuilt with an explicit pointer
In reply to Dave ChinnerRecovery 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.
--- fs/xfs/libxfs/xfs_log_recover.h | 1 + fs/xfs/xfs_log_recover.c | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 16 deletions(-)
fs/xfs/libxfs/xfs_log_recover.h +1 -0
@@ -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)
fs/xfs/xfs_log_recover.c +13 -16
@@ -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 */
Signature · 1 line
2.55.0
PATCH v2 16/59 xfs: complete log items explicitly during recovery
In reply to Dave ChinnerLog 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.
--- fs/xfs/xfs_log_recover.c | 141 ++++++++++++++++++++++++++++++--------- 1 file changed, 109 insertions(+), 32 deletions(-)
fs/xfs/xfs_log_recover.c +109 -32
@@ -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 voidxlog_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. */
Signature · 1 line
2.55.0
PATCH v2 17/59 xfs: add log item validation infrastructure to log recovery
In reply to Dave ChinnerLog 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.
--- fs/xfs/libxfs/xfs_log_recover.h | 53 +++++++ fs/xfs/xfs_log_recover.c | 238 +++++++++++++++++++++++--------- 2 files changed, 225 insertions(+), 66 deletions(-)
fs/xfs/libxfs/xfs_log_recover.h +53 -0
@@ -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;
fs/xfs/xfs_log_recover.c +172 -66
322 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 18/59 xfs: recover the transaction header as a validated item type
In reply to Dave ChinnerThe 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.
--- fs/xfs/libxfs/xfs_log_recover.h | 1 - fs/xfs/xfs_log_recover.c | 199 +++++++++++++++++++------------- 2 files changed, 121 insertions(+), 79 deletions(-)
fs/xfs/libxfs/xfs_log_recover.h +0 -1
@@ -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 */ };
fs/xfs/xfs_log_recover.c +121 -78
224 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 19/59 xfs: factor out xlog_recover_validate_ophdr()
In reply to Dave ChinnerFactor 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.
--- fs/xfs/xfs_log_recover.c | 55 ++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 19 deletions(-)
fs/xfs/xfs_log_recover.c +36 -19
@@ -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) {
Signature · 1 line
2.55.0
PATCH v2 20/59 xfs: validate operation header length and alignment during recovery
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_log_recover.c | 115 +++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 34 deletions(-)
fs/xfs/xfs_log_recover.c +81 -34
@@ -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 ;
Signature · 1 line
2.55.0
PATCH v2 21/59 xfs: validate the operation header padding during recovery
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_log_recover.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/xfs/xfs_log_recover.c +7 -0
@@ -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; }
Signature · 1 line
2.55.0
PATCH v2 22/59 xfs: validate the operation header flags during recovery
In reply to Dave ChinnerRecovery 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.
--- fs/xfs/xfs_log_recover.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
fs/xfs/xfs_log_recover.c +21 -0
@@ -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; }
Signature · 1 line
2.55.0
PATCH v2 23/59 xfs: enforce zero length control records during recovery
In reply to Dave ChinnerStart 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.
--- fs/xfs/xfs_log_recover.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
fs/xfs/xfs_log_recover.c +11 -3
@@ -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 :
Signature · 1 line
2.55.0
PATCH v2 24/59 xfs: validate the unmount record during recovery
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_log_recover.c | 144 +++++++++++++++++++++++++++++++++------ 1 file changed, 123 insertions(+), 21 deletions(-)
fs/xfs/xfs_log_recover.c +123 -21
@@ -42,6 +42,8 @@ xlog_clear_stale_blocks( STATIC intxlog_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);
Signature · 1 line
2.55.0
PATCH v2 25/59 xfs: handle zero length continuation op headers during recovery
In reply to Dave ChinnerThe 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.
--- fs/xfs/libxfs/xfs_log_recover.h | 9 +++++ fs/xfs/xfs_log_recover.c | 59 ++++++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 8 deletions(-)
fs/xfs/libxfs/xfs_log_recover.h +9 -0
@@ -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
fs/xfs/xfs_log_recover.c +51 -8
96 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 26/59 xfs: bound the assembled region size during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/libxfs/xfs_log_format.h | 8 ++++++++ fs/xfs/xfs_log_recover.c | 21 ++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-)
fs/xfs/libxfs/xfs_log_format.h +8 -0
@@ -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
fs/xfs/xfs_log_recover.c +12 -9
@@ -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);
Signature · 1 line
2.55.0
PATCH v2 27/59 xfs: dump the corrupt region when log item validation fails
In reply to Dave ChinnerLog 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.
--- fs/xfs/xfs_log_recover.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
fs/xfs/xfs_log_recover.c +17 -6
@@ -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
Signature · 1 line
2.55.0
PATCH v2 28/59 xfs: validate inode create and quota off log items during recovery
In reply to Dave ChinnerThe 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.
--- 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(-)
fs/xfs/libxfs/xfs_log_recover.h +1 -0
@@ -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);
fs/xfs/xfs_dquot_item_recover.c +38 -0
@@ -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 */ };
fs/xfs/xfs_icreate_item.c +18 -0
@@ -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, };
fs/xfs/xfs_log_recover.c +20 -5
@@ -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);
Signature · 1 line
2.55.0
PATCH v2 29/59 xfs: validate extent free log items during recovery
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_extfree_item.c | 216 ++++++++++++++++++++++++-------------- 1 file changed, 135 insertions(+), 81 deletions(-)
fs/xfs/xfs_extfree_item.c +135 -81
@@ -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 voidxfs_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, };
Signature · 1 line
2.55.0
PATCH v2 30/59 xfs: validate and convert extent free intents during recovery
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_extfree_item.c | 88 +++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 37 deletions(-)
fs/xfs/xfs_extfree_item.c +51 -37
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 31/59 xfs: validate and convert extent free done items during recovery
In reply to Dave ChinnerAn 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.
--- fs/xfs/xfs_extfree_item.c | 85 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
fs/xfs/xfs_extfree_item.c +85 -0
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 32/59 xfs: validate rmap update intents during recovery
In reply to Dave ChinnerAn 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.
--- fs/xfs/xfs_rmap_item.c | 119 ++++++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 42 deletions(-)
fs/xfs/xfs_rmap_item.c +77 -42
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 33/59 xfs: validate rmap update done items during recovery
In reply to Dave ChinnerAn 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.
--- fs/xfs/xfs_rmap_item.c | 44 ++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-)
fs/xfs/xfs_rmap_item.c +34 -10
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 34/59 xfs: validate refcount update intents during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_refcount_item.c | 119 ++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 42 deletions(-)
fs/xfs/xfs_refcount_item.c +77 -42
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 35/59 xfs: validate refcount update done items during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_refcount_item.c | 44 +++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 10 deletions(-)
fs/xfs/xfs_refcount_item.c +34 -10
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 36/59 xfs: validate bmap update intents during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_bmap_item.c | 77 +++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 24 deletions(-)
fs/xfs/xfs_bmap_item.c +53 -24
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 37/59 xfs: validate bmap update done items during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_bmap_item.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-)
fs/xfs/xfs_bmap_item.c +28 -4
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 38/59 xfs: validate mapping exchange intents during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_exchmaps_item.c | 64 +++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 25 deletions(-)
fs/xfs/xfs_exchmaps_item.c +39 -25
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 39/59 xfs: validate mapping exchange done items during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_exchmaps_item.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-)
fs/xfs/xfs_exchmaps_item.c +28 -4
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 40/59 xfs: validate dquot items during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_dquot_item_recover.c | 119 ++++++++++++++++++++++++++------ 1 file changed, 97 insertions(+), 22 deletions(-)
fs/xfs/xfs_dquot_item_recover.c +97 -22
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 41/59 xfs: validate attr done items during recovery
In reply to Dave ChinnerAn 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.
--- fs/xfs/xfs_attr_item.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)
fs/xfs/xfs_attr_item.c +29 -5
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 42/59 xfs: validate attr intents during recovery
In reply to Dave ChinnerAn 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.
--- fs/xfs/xfs_attr_item.c | 249 +++++++++++++++++++++++++---------------- 1 file changed, 151 insertions(+), 98 deletions(-)
fs/xfs/xfs_attr_item.c +151 -98
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 43/59 xfs: convert inode items to host format during recovery
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_inode_item_recover.c | 108 +++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 29 deletions(-)
fs/xfs/xfs_inode_item_recover.c +79 -29
@@ -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, };
Signature · 1 line
2.55.0
PATCH v2 44/59 xfs: allow xfs_imap() to map without an inode btree lookup
In reply to Dave Chinnerxfs_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.
--- fs/xfs/libxfs/xfs_ialloc.c | 7 +++++++ fs/xfs/xfs_icache.h | 2 ++ 2 files changed, 9 insertions(+)
fs/xfs/libxfs/xfs_ialloc.c +7 -0
@@ -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)
fs/xfs/xfs_icache.h +2 -0
@@ -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);
Signature · 1 line
2.55.0
PATCH v2 45/59 xfs: validate the inode item format header during recovery
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_inode_item_recover.c | 171 ++++++++++++++++++++++++++++++-- 1 file changed, 164 insertions(+), 7 deletions(-)
fs/xfs/xfs_inode_item_recover.c +164 -7
@@ -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,
Signature · 1 line
2.55.0
PATCH v2 46/59 xfs: validate the logged inode core during recovery
In reply to Dave ChinnerThe 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.
--- 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(-)
fs/xfs/libxfs/xfs_inode_buf.c +10 -9
@@ -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;
fs/xfs/libxfs/xfs_inode_buf.h +2 -0
@@ -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) {
fs/xfs/xfs_inode_item_recover.c +247 -115
443 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 47/59 xfs: validate the logged inode forks during recovery
In reply to Dave ChinnerAn 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.
--- 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(-)
fs/xfs/libxfs/xfs_format.h +3 -5
@@ -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))
fs/xfs/libxfs/xfs_inode_fork.c +1 -1
@@ -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,
fs/xfs/xfs_inode.h +28 -15
61 lines not on this page · raw message
fs/xfs/xfs_inode_item_recover.c +153 -15
204 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 48/59 xfs: validate logged local fork contents during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_inode_item_recover.c | 53 +++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-)
fs/xfs/xfs_inode_item_recover.c +48 -5
@@ -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;
Signature · 1 line
2.55.0
PATCH v2 49/59 xfs: validate logged extent fork contents during recovery
In reply to Dave ChinnerAn 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.
--- 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(-)
fs/xfs/libxfs/xfs_bmap.c +41 -7
@@ -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);
fs/xfs/libxfs/xfs_bmap.h +6 -2
@@ -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,
fs/xfs/libxfs/xfs_inode_fork.c +11 -12
@@ -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);
fs/xfs/scrub/inode_repair.c +3 -11
@@ -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; }
fs/xfs/xfs_inode_item_recover.c +33 -2
@@ -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;
Signature · 1 line
2.55.0
PATCH v2 50/59 xfs: allow a long format btree root to be checked without a cursor
In reply to Dave ChinnerThe 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.
--- fs/xfs/libxfs/xfs_btree.c | 50 ++++++++++++++++++++++++++++++--------- fs/xfs/libxfs/xfs_btree.h | 4 ++++ 2 files changed, 43 insertions(+), 11 deletions(-)
fs/xfs/libxfs/xfs_btree.c +39 -11
@@ -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;
fs/xfs/libxfs/xfs_btree.h +4 -0
@@ -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);
Signature · 1 line
2.55.0
PATCH v2 51/59 xfs: validate logged btree fork roots during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_inode_item_recover.c | 66 ++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-)
fs/xfs/xfs_inode_item_recover.c +65 -1
@@ -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 ;
Signature · 1 line
2.55.0
PATCH v2 52/59 xfs: validate logged metadata btree fork roots during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_inode_item_recover.c | 170 +++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 1 deletion(-)
fs/xfs/xfs_inode_item_recover.c +169 -1
@@ -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 ;
Signature · 1 line
2.55.0
PATCH v2 53/59 xfs: check logged btree fork roots fit in the inode fork during recovery
In reply to Dave ChinnerA 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.
--- fs/xfs/xfs_inode_item_recover.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
fs/xfs/xfs_inode_item_recover.c +16 -2
@@ -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
Signature · 1 line
2.55.0
PATCH v2 54/59 xfs: validate buffer item region count during recovery
In reply to Dave ChinnerValidate 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.
--- fs/xfs/xfs_buf_item_recover.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
fs/xfs/xfs_buf_item_recover.c +17 -0
@@ -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,
Signature · 1 line
2.55.0
PATCH v2 55/59 xfs: validate buffer item regions during recovery
In reply to Dave ChinnerValidate 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.
--- fs/xfs/xfs_buf_item_recover.c | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
fs/xfs/xfs_buf_item_recover.c +46 -0
@@ -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,
Signature · 1 line
2.55.0
PATCH v2 56/59 xfs: validate the whole buffer item during recovery
In reply to Dave ChinnerValidate 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.
--- 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(+)
fs/xfs/scrub/inode_repair.c +14 -0
@@ -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 );
fs/xfs/scrub/quota_repair.c +7 -0
@@ -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);
fs/xfs/xfs_buf_item_recover.c +185 -0
196 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v2 57/59 xfs: remove redundant buffer checks from recovery replay
In reply to Dave ChinnerThe 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.
--- fs/xfs/xfs_buf_item_recover.c | 40 ----------------------------------- 1 file changed, 40 deletions(-)
fs/xfs/xfs_buf_item_recover.c +0 -40
@@ -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
Signature · 1 line
2.55.0
PATCH v2 58/59 xfs: fail recovery on a buffer type mismatch
In reply to Dave ChinnerWhen 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.
--- fs/xfs/xfs_buf_item_recover.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
fs/xfs/xfs_buf_item_recover.c +16 -7
@@ -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); }/*
Signature · 1 line
2.55.0
PATCH v2 59/59 xfs: validate logged dquot buffers during recovery
In reply to Dave ChinnerA 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.
--- 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(-)
fs/xfs/libxfs/xfs_dquot_buf.c +37 -27
@@ -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
fs/xfs/libxfs/xfs_quota_defs.h +2 -0
@@ -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);
fs/xfs/xfs_buf_item_recover.c +42 -31
101 lines not on this page · raw message
Signature · 1 line
2.55.0
Please try to get this going by splitting it up. The first 11 patches
have nothing to do with your design and framework, so let's get them out first, Ccing people who touched the area etc. And the apply geometry at log recovery bits are scary. Yes, we probably need them, but without testing I fear they might create even more problems than they solve. So let's get coverage for that.
Once we get the initial bits in I'll try to move on to the rest, but even there it would really help to chunk it up, i.e. do a series just for the generic log recovery changes without item-specific bits to keep everyone focussed and speed up the iteration.