linux/fs/xfs/scrub
Darrick J. Wong 61e0d0cc51 xfs: fix perag structure refcounting error when scrub fails
The kernel test robot found the following bug when running xfs/355 to
scrub a bmap btree:

XFS: Assertion failed: !sa->pag, file: fs/xfs/scrub/common.c, line: 412
------------[ cut here ]------------
kernel BUG at fs/xfs/xfs_message.c:110!
invalid opcode: 0000 [#1] SMP PTI
CPU: 2 PID: 1415 Comm: xfs_scrub Not tainted 5.14.0-rc4-00021-g48c6615cc557 #1
Hardware name: Hewlett-Packard p6-1451cx/2ADA, BIOS 8.15 02/05/2013
RIP: 0010:assfail+0x23/0x28 [xfs]
RSP: 0018:ffffc9000aacb890 EFLAGS: 00010202
RAX: 0000000000000000 RBX: ffffc9000aacbcc8 RCX: 0000000000000000
RDX: 00000000ffffffc0 RSI: 000000000000000a RDI: ffffffffc09e7dcd
RBP: ffffc9000aacbc80 R08: ffff8881fdf17d50 R09: 0000000000000000
R10: 000000000000000a R11: f000000000000000 R12: 0000000000000000
R13: ffff88820c7ed000 R14: 0000000000000001 R15: ffffc9000aacb980
FS:  00007f185b955700(0000) GS:ffff8881fdf00000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f7f6ef43000 CR3: 000000020de38002 CR4: 00000000001706e0
Call Trace:
 xchk_ag_read_headers+0xda/0x100 [xfs]
 xchk_ag_init+0x15/0x40 [xfs]
 xchk_btree_check_block_owner+0x76/0x180 [xfs]
 xchk_btree_get_block+0xd0/0x140 [xfs]
 xchk_btree+0x32e/0x440 [xfs]
 xchk_bmap_btree+0xd4/0x140 [xfs]
 xchk_bmap+0x1eb/0x3c0 [xfs]
 xfs_scrub_metadata+0x227/0x4c0 [xfs]
 xfs_ioc_scrub_metadata+0x50/0xc0 [xfs]
 xfs_file_ioctl+0x90c/0xc40 [xfs]
 __x64_sys_ioctl+0x83/0xc0
 do_syscall_64+0x3b/0xc0

The unusual handling of errors while initializing struct xchk_ag is the
root cause here.  Since the beginning of xfs_scrub, the goal of
xchk_ag_read_headers has been to read all three AG header buffers and
attach them both to the xchk_ag structure and the scrub transaction.
Corruption errors on any of the three headers doesn't necessarily
trigger an immediate return to userspace, because xfs_scrub can also
tell us to /fix/ the problem.

In other words, it's possible for the xchk_ag init functions to return
an error code and a partially filled out structure so that scrub can use
however much information it managed to pull.  Before 5.15, it was
sufficient to cancel (or commit) the scrub transaction on the way out of
the scrub code to release the buffers.

Ccommit 48c6615cc5 added a reference to the perag structure to struct
xchk_ag.  Since perag structures are not attached to transactions like
buffers are, this adds the requirement that the perag ref be released
explicitly.  The scrub teardown function xchk_teardown was amended to do
this for the xchk_ag embedded in struct xfs_scrub.

Unfortunately, I forgot that certain parts of the scrub code probe
multiple AGs and therefore handle the initialization and cleanup on
their own.  Specifically, the bmbt scrubber will initialize it long
enough to cross-reference AG metadata for btree blocks and for the
extent mappings in the bmbt.

If one of the AG headers is corrupt, the init function returns with a
live perag structure reference and some of the AG header buffers.  If an
error occurs, the cross referencing will be noted as XCORRUPTion and
skipped, but the main scrub process will move on to the next record.
It is now necessary to release the perag reference before we try to
analyze something from a different AG, or else we'll trip over the
assertion noted above.

Fixes: 48c6615cc5 ("xfs: grab active perag ref when reading AG headers")
Reported-by: kernel test robot <oliver.sang@intel.com>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Chandan Babu R <chandanrlinux@gmail.com>
2021-08-20 13:20:33 -07:00
..
agheader_repair.c xfs: convert xfs_sb_version_has checks to use mount features 2021-08-19 10:07:14 -07:00
agheader.c xfs: introduce xfs_sb_is_v5 helper 2021-08-19 10:07:14 -07:00
alloc.c xfs: mark the record passed into xchk_btree functions as const 2021-08-18 18:46:01 -07:00
attr.c xfs: replace xfs_sb_version checks with feature flag checks 2021-08-19 10:07:12 -07:00
attr.h xfs: replace kmem_alloc_large() with kvmalloc() 2021-08-09 15:57:43 -07:00
bitmap.c xfs: convert bp->b_bn references to xfs_buf_daddr() 2021-08-19 10:07:15 -07:00
bitmap.h xfs: replace open-coded bitmap weight logic 2020-03-18 08:12:23 -07:00
bmap.c xfs: fix perag structure refcounting error when scrub fails 2021-08-20 13:20:33 -07:00
btree.c xfs: fix perag structure refcounting error when scrub fails 2021-08-20 13:20:33 -07:00
btree.h xfs: mark the record passed into xchk_btree functions as const 2021-08-18 18:46:01 -07:00
common.c xfs: convert bp->b_bn references to xfs_buf_daddr() 2021-08-19 10:07:15 -07:00
common.h xfs: grab active perag ref when reading AG headers 2021-08-09 11:13:17 -07:00
dabtree.c xfs: replace xfs_sb_version checks with feature flag checks 2021-08-19 10:07:12 -07:00
dabtree.h xfs: refactor btree node scrubbing 2019-11-10 16:54:19 -08:00
dir.c xfs: replace xfs_sb_version checks with feature flag checks 2021-08-19 10:07:12 -07:00
fscounters.c xfs: fix perag structure refcounting error when scrub fails 2021-08-20 13:20:33 -07:00
health.c xfs: move xfs_perag_get/put to xfs_ag.[ch] 2021-06-02 10:48:24 +10:00
health.h xfs: scrub should only cross-reference with healthy btrees 2019-04-16 10:01:57 -07:00
ialloc.c xfs: replace xfs_sb_version checks with feature flag checks 2021-08-19 10:07:12 -07:00
inode.c xfs: fix perag structure refcounting error when scrub fails 2021-08-20 13:20:33 -07:00
parent.c xfs: get rid of the ip parameter to xchk_setup_* 2021-04-09 10:27:18 -07:00
quota.c xfs: convert xfs_sb_version_has checks to use mount features 2021-08-19 10:07:14 -07:00
refcount.c xfs: mark the record passed into xchk_btree functions as const 2021-08-18 18:46:01 -07:00
repair.c xfs: replace xfs_sb_version checks with feature flag checks 2021-08-19 10:07:12 -07:00
repair.h xfs: get rid of the ip parameter to xchk_setup_* 2021-04-09 10:27:18 -07:00
rmap.c xfs: mark the record passed into xchk_btree functions as const 2021-08-18 18:46:01 -07:00
rtbitmap.c xfs: make the record pointer passed to query_range functions const 2021-08-18 18:46:01 -07:00
scrub.c xfs: convert scrub to use mount-based feature checks 2021-08-19 10:07:13 -07:00
scrub.h xfs: convert scrub to use mount-based feature checks 2021-08-19 10:07:13 -07:00
symlink.c xfs: remove XFS_IFINLINE 2021-04-15 09:35:51 -07:00
trace.c xfs: convert bp->b_bn references to xfs_buf_daddr() 2021-08-19 10:07:15 -07:00
trace.h xfs: start documenting common units and tags used in tracepoints 2021-08-19 10:07:11 -07:00
xfs_scrub.h xfs: fix scrub and remount-ro protection when running scrub 2021-04-09 10:27:13 -07:00