Skip to content

Commit 7dfee17

Browse files
Dave Chinnerdchinner
authored andcommitted
xfs: validate block number being freed before adding to xefi
Bad things happen in defered extent freeing operations if it is passed a bad block number in the xefi. This can come from a bogus agno/agbno pair from deferred agfl freeing, or just a bad fsbno being passed to __xfs_free_extent_later(). Either way, it's very difficult to diagnose where a null perag oops in EFI creation is coming from when the operation that queued the xefi has already been completed and there's no longer any trace of it around.... Signed-off-by: Dave Chinner <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Signed-off-by: Dave Chinner <[email protected]>
1 parent 3148ebf commit 7dfee17

File tree

8 files changed

+62
-23
lines changed

8 files changed

+62
-23
lines changed

fs/xfs/libxfs/xfs_ag.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,10 @@ xfs_ag_shrink_space(
984984
if (err2 != -ENOSPC)
985985
goto resv_err;
986986

987-
__xfs_free_extent_later(*tpp, args.fsbno, delta, NULL, true);
987+
err2 = __xfs_free_extent_later(*tpp, args.fsbno, delta, NULL,
988+
true);
989+
if (err2)
990+
goto resv_err;
988991

989992
/*
990993
* Roll the transaction before trying to re-init the per-ag

fs/xfs/libxfs/xfs_alloc.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,7 +2431,7 @@ xfs_agfl_reset(
24312431
* the real allocation can proceed. Deferring the free disconnects freeing up
24322432
* the AGFL slot from freeing the block.
24332433
*/
2434-
STATIC void
2434+
static int
24352435
xfs_defer_agfl_block(
24362436
struct xfs_trans *tp,
24372437
xfs_agnumber_t agno,
@@ -2450,17 +2450,21 @@ xfs_defer_agfl_block(
24502450
xefi->xefi_blockcount = 1;
24512451
xefi->xefi_owner = oinfo->oi_owner;
24522452

2453+
if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, xefi->xefi_startblock)))
2454+
return -EFSCORRUPTED;
2455+
24532456
trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1);
24542457

24552458
xfs_extent_free_get_group(mp, xefi);
24562459
xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_AGFL_FREE, &xefi->xefi_list);
2460+
return 0;
24572461
}
24582462

24592463
/*
24602464
* Add the extent to the list of extents to be free at transaction end.
24612465
* The list is maintained sorted (by block number).
24622466
*/
2463-
void
2467+
int
24642468
__xfs_free_extent_later(
24652469
struct xfs_trans *tp,
24662470
xfs_fsblock_t bno,
@@ -2487,6 +2491,9 @@ __xfs_free_extent_later(
24872491
#endif
24882492
ASSERT(xfs_extfree_item_cache != NULL);
24892493

2494+
if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbext(mp, bno, len)))
2495+
return -EFSCORRUPTED;
2496+
24902497
xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
24912498
GFP_KERNEL | __GFP_NOFAIL);
24922499
xefi->xefi_startblock = bno;
@@ -2510,6 +2517,7 @@ __xfs_free_extent_later(
25102517

25112518
xfs_extent_free_get_group(mp, xefi);
25122519
xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &xefi->xefi_list);
2520+
return 0;
25132521
}
25142522

25152523
#ifdef DEBUG
@@ -2670,7 +2678,9 @@ xfs_alloc_fix_freelist(
26702678
goto out_agbp_relse;
26712679

26722680
/* defer agfl frees */
2673-
xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo);
2681+
error = xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo);
2682+
if (error)
2683+
goto out_agbp_relse;
26742684
}
26752685

26762686
targs.tp = tp;

fs/xfs/libxfs/xfs_alloc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ xfs_buf_to_agfl_bno(
230230
return bp->b_addr;
231231
}
232232

233-
void __xfs_free_extent_later(struct xfs_trans *tp, xfs_fsblock_t bno,
233+
int __xfs_free_extent_later(struct xfs_trans *tp, xfs_fsblock_t bno,
234234
xfs_filblks_t len, const struct xfs_owner_info *oinfo,
235235
bool skip_discard);
236236

@@ -254,14 +254,14 @@ void xfs_extent_free_get_group(struct xfs_mount *mp,
254254
#define XFS_EFI_ATTR_FORK (1U << 1) /* freeing attr fork block */
255255
#define XFS_EFI_BMBT_BLOCK (1U << 2) /* freeing bmap btree block */
256256

257-
static inline void
257+
static inline int
258258
xfs_free_extent_later(
259259
struct xfs_trans *tp,
260260
xfs_fsblock_t bno,
261261
xfs_filblks_t len,
262262
const struct xfs_owner_info *oinfo)
263263
{
264-
__xfs_free_extent_later(tp, bno, len, oinfo, false);
264+
return __xfs_free_extent_later(tp, bno, len, oinfo, false);
265265
}
266266

267267

fs/xfs/libxfs/xfs_bmap.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,12 @@ xfs_bmap_btree_to_extents(
572572
cblock = XFS_BUF_TO_BLOCK(cbp);
573573
if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
574574
return error;
575+
575576
xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
576-
xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo);
577+
error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo);
578+
if (error)
579+
return error;
580+
577581
ip->i_nblocks--;
578582
xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
579583
xfs_trans_binval(tp, cbp);
@@ -5230,10 +5234,12 @@ xfs_bmap_del_extent_real(
52305234
if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
52315235
xfs_refcount_decrease_extent(tp, del);
52325236
} else {
5233-
__xfs_free_extent_later(tp, del->br_startblock,
5237+
error = __xfs_free_extent_later(tp, del->br_startblock,
52345238
del->br_blockcount, NULL,
52355239
(bflags & XFS_BMAPI_NODISCARD) ||
52365240
del->br_state == XFS_EXT_UNWRITTEN);
5241+
if (error)
5242+
goto done;
52375243
}
52385244
}
52395245

fs/xfs/libxfs/xfs_bmap_btree.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,14 @@ xfs_bmbt_free_block(
268268
struct xfs_trans *tp = cur->bc_tp;
269269
xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
270270
struct xfs_owner_info oinfo;
271+
int error;
271272

272273
xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork);
273-
xfs_free_extent_later(cur->bc_tp, fsbno, 1, &oinfo);
274-
ip->i_nblocks--;
274+
error = xfs_free_extent_later(cur->bc_tp, fsbno, 1, &oinfo);
275+
if (error)
276+
return error;
275277

278+
ip->i_nblocks--;
276279
xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
277280
xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
278281
return 0;

fs/xfs/libxfs/xfs_ialloc.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ xfs_dialloc(
18341834
* might be sparse and only free the regions that are allocated as part of the
18351835
* chunk.
18361836
*/
1837-
STATIC void
1837+
static int
18381838
xfs_difree_inode_chunk(
18391839
struct xfs_trans *tp,
18401840
xfs_agnumber_t agno,
@@ -1851,10 +1851,10 @@ xfs_difree_inode_chunk(
18511851

18521852
if (!xfs_inobt_issparse(rec->ir_holemask)) {
18531853
/* not sparse, calculate extent info directly */
1854-
xfs_free_extent_later(tp, XFS_AGB_TO_FSB(mp, agno, sagbno),
1855-
M_IGEO(mp)->ialloc_blks,
1856-
&XFS_RMAP_OINFO_INODES);
1857-
return;
1854+
return xfs_free_extent_later(tp,
1855+
XFS_AGB_TO_FSB(mp, agno, sagbno),
1856+
M_IGEO(mp)->ialloc_blks,
1857+
&XFS_RMAP_OINFO_INODES);
18581858
}
18591859

18601860
/* holemask is only 16-bits (fits in an unsigned long) */
@@ -1871,6 +1871,8 @@ xfs_difree_inode_chunk(
18711871
XFS_INOBT_HOLEMASK_BITS);
18721872
nextbit = startidx + 1;
18731873
while (startidx < XFS_INOBT_HOLEMASK_BITS) {
1874+
int error;
1875+
18741876
nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
18751877
nextbit);
18761878
/*
@@ -1896,15 +1898,19 @@ xfs_difree_inode_chunk(
18961898

18971899
ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
18981900
ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
1899-
xfs_free_extent_later(tp, XFS_AGB_TO_FSB(mp, agno, agbno),
1900-
contigblk, &XFS_RMAP_OINFO_INODES);
1901+
error = xfs_free_extent_later(tp,
1902+
XFS_AGB_TO_FSB(mp, agno, agbno),
1903+
contigblk, &XFS_RMAP_OINFO_INODES);
1904+
if (error)
1905+
return error;
19011906

19021907
/* reset range to current bit and carry on... */
19031908
startidx = endidx = nextbit;
19041909

19051910
next:
19061911
nextbit++;
19071912
}
1913+
return 0;
19081914
}
19091915

19101916
STATIC int
@@ -2003,7 +2009,9 @@ xfs_difree_inobt(
20032009
goto error0;
20042010
}
20052011

2006-
xfs_difree_inode_chunk(tp, pag->pag_agno, &rec);
2012+
error = xfs_difree_inode_chunk(tp, pag->pag_agno, &rec);
2013+
if (error)
2014+
goto error0;
20072015
} else {
20082016
xic->deleted = false;
20092017

fs/xfs/libxfs/xfs_refcount.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,10 @@ xfs_refcount_adjust_extents(
11511151
fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
11521152
cur->bc_ag.pag->pag_agno,
11531153
tmp.rc_startblock);
1154-
xfs_free_extent_later(cur->bc_tp, fsbno,
1154+
error = xfs_free_extent_later(cur->bc_tp, fsbno,
11551155
tmp.rc_blockcount, NULL);
1156+
if (error)
1157+
goto out_error;
11561158
}
11571159

11581160
(*agbno) += tmp.rc_blockcount;
@@ -1210,8 +1212,10 @@ xfs_refcount_adjust_extents(
12101212
fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
12111213
cur->bc_ag.pag->pag_agno,
12121214
ext.rc_startblock);
1213-
xfs_free_extent_later(cur->bc_tp, fsbno,
1215+
error = xfs_free_extent_later(cur->bc_tp, fsbno,
12141216
ext.rc_blockcount, NULL);
1217+
if (error)
1218+
goto out_error;
12151219
}
12161220

12171221
skip:
@@ -1976,7 +1980,10 @@ xfs_refcount_recover_cow_leftovers(
19761980
rr->rr_rrec.rc_blockcount);
19771981

19781982
/* Free the block. */
1979-
xfs_free_extent_later(tp, fsb, rr->rr_rrec.rc_blockcount, NULL);
1983+
error = xfs_free_extent_later(tp, fsb,
1984+
rr->rr_rrec.rc_blockcount, NULL);
1985+
if (error)
1986+
goto out_trans;
19801987

19811988
error = xfs_trans_commit(tp);
19821989
if (error)

fs/xfs/xfs_reflink.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,10 @@ xfs_reflink_cancel_cow_blocks(
616616
xfs_refcount_free_cow_extent(*tpp, del.br_startblock,
617617
del.br_blockcount);
618618

619-
xfs_free_extent_later(*tpp, del.br_startblock,
619+
error = xfs_free_extent_later(*tpp, del.br_startblock,
620620
del.br_blockcount, NULL);
621+
if (error)
622+
break;
621623

622624
/* Roll the transaction */
623625
error = xfs_defer_finish(tpp);

0 commit comments

Comments
 (0)