Skip to content

Commit c3b880a

Browse files
Long LiDarrick J. Wong
authored andcommitted
xfs: fix ag count overflow during growfs
I found a corruption during growfs: XFS (loop0): Internal error agbno >= mp->m_sb.sb_agblocks at line 3661 of file fs/xfs/libxfs/xfs_alloc.c. Caller __xfs_free_extent+0x28e/0x3c0 CPU: 0 PID: 573 Comm: xfs_growfs Not tainted 6.3.0-rc7-next-20230420-00001-gda8c95746257 Call Trace: <TASK> dump_stack_lvl+0x50/0x70 xfs_corruption_error+0x134/0x150 __xfs_free_extent+0x2c1/0x3c0 xfs_ag_extend_space+0x291/0x3e0 xfs_growfs_data+0xd72/0xe90 xfs_file_ioctl+0x5f9/0x14a0 __x64_sys_ioctl+0x13e/0x1c0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd XFS (loop0): Corruption detected. Unmount and run xfs_repair XFS (loop0): Internal error xfs_trans_cancel at line 1097 of file fs/xfs/xfs_trans.c. Caller xfs_growfs_data+0x691/0xe90 CPU: 0 PID: 573 Comm: xfs_growfs Not tainted 6.3.0-rc7-next-20230420-00001-gda8c95746257 Call Trace: <TASK> dump_stack_lvl+0x50/0x70 xfs_error_report+0x93/0xc0 xfs_trans_cancel+0x2c0/0x350 xfs_growfs_data+0x691/0xe90 xfs_file_ioctl+0x5f9/0x14a0 __x64_sys_ioctl+0x13e/0x1c0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd RIP: 0033:0x7f2d86706577 The bug can be reproduced with the following sequence: # truncate -s 1073741824 xfs_test.img # mkfs.xfs -f -b size=1024 -d agcount=4 xfs_test.img # truncate -s 2305843009213693952 xfs_test.img # mount -o loop xfs_test.img /mnt/test # xfs_growfs -D 1125899907891200 /mnt/test The root cause is that during growfs, user space passed in a large value of newblcoks to xfs_growfs_data_private(), due to current sb_agblocks is too small, new AG count will exceed UINT_MAX. Because of AG number type is unsigned int and it would overflow, that caused nagcount much smaller than the actual value. During AG extent space, delta blocks in xfs_resizefs_init_new_ags() will much larger than the actual value due to incorrect nagcount, even exceed UINT_MAX. This will cause corruption and be detected in __xfs_free_extent. Fix it by growing the filesystem to up to the maximally allowed AGs and not return EINVAL when new AG count overflow. Signed-off-by: Long Li <leo.lilong@huawei.com> Reviewed-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Darrick J. Wong <djwong@kernel.org>
1 parent b294349 commit c3b880a

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

fs/xfs/libxfs/xfs_fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ typedef struct xfs_fsop_resblks {
257257
#define XFS_MAX_AG_BLOCKS (XFS_MAX_AG_BYTES / XFS_MIN_BLOCKSIZE)
258258
#define XFS_MAX_CRC_AG_BLOCKS (XFS_MAX_AG_BYTES / XFS_MIN_CRC_BLOCKSIZE)
259259

260+
#define XFS_MAX_AGNUMBER ((xfs_agnumber_t)(NULLAGNUMBER - 1))
261+
260262
/* keep the maximum size under 2^31 by a small amount */
261263
#define XFS_MAX_LOG_BYTES \
262264
((2 * 1024 * 1024 * 1024ULL) - XFS_MIN_LOG_BYTES)

fs/xfs/xfs_fsops.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,16 @@ xfs_growfs_data_private(
115115

116116
nb_div = nb;
117117
nb_mod = do_div(nb_div, mp->m_sb.sb_agblocks);
118-
nagcount = nb_div + (nb_mod != 0);
119-
if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
120-
nagcount--;
121-
nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
118+
if (nb_mod && nb_mod >= XFS_MIN_AG_BLOCKS)
119+
nb_div++;
120+
else if (nb_mod)
121+
nb = nb_div * mp->m_sb.sb_agblocks;
122+
123+
if (nb_div > XFS_MAX_AGNUMBER + 1) {
124+
nb_div = XFS_MAX_AGNUMBER + 1;
125+
nb = nb_div * mp->m_sb.sb_agblocks;
122126
}
127+
nagcount = nb_div;
123128
delta = nb - mp->m_sb.sb_dblocks;
124129
/*
125130
* Reject filesystems with a single AG because they are not

0 commit comments

Comments
 (0)