Commit 7dfee17b authored by Dave Chinner's avatar Dave Chinner Committed by Dave Chinner

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: default avatarDave Chinner <dchinner@redhat.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarDarrick J. Wong <djwong@kernel.org>
Signed-off-by: default avatarDave Chinner <david@fromorbit.com>
parent 3148ebf2
...@@ -984,7 +984,10 @@ xfs_ag_shrink_space( ...@@ -984,7 +984,10 @@ xfs_ag_shrink_space(
if (err2 != -ENOSPC) if (err2 != -ENOSPC)
goto resv_err; goto resv_err;
__xfs_free_extent_later(*tpp, args.fsbno, delta, NULL, true); err2 = __xfs_free_extent_later(*tpp, args.fsbno, delta, NULL,
true);
if (err2)
goto resv_err;
/* /*
* Roll the transaction before trying to re-init the per-ag * Roll the transaction before trying to re-init the per-ag
......
...@@ -2431,7 +2431,7 @@ xfs_agfl_reset( ...@@ -2431,7 +2431,7 @@ xfs_agfl_reset(
* the real allocation can proceed. Deferring the free disconnects freeing up * the real allocation can proceed. Deferring the free disconnects freeing up
* the AGFL slot from freeing the block. * the AGFL slot from freeing the block.
*/ */
STATIC void static int
xfs_defer_agfl_block( xfs_defer_agfl_block(
struct xfs_trans *tp, struct xfs_trans *tp,
xfs_agnumber_t agno, xfs_agnumber_t agno,
...@@ -2450,17 +2450,21 @@ xfs_defer_agfl_block( ...@@ -2450,17 +2450,21 @@ xfs_defer_agfl_block(
xefi->xefi_blockcount = 1; xefi->xefi_blockcount = 1;
xefi->xefi_owner = oinfo->oi_owner; xefi->xefi_owner = oinfo->oi_owner;
if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, xefi->xefi_startblock)))
return -EFSCORRUPTED;
trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1); trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1);
xfs_extent_free_get_group(mp, xefi); xfs_extent_free_get_group(mp, xefi);
xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_AGFL_FREE, &xefi->xefi_list); xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_AGFL_FREE, &xefi->xefi_list);
return 0;
} }
/* /*
* Add the extent to the list of extents to be free at transaction end. * Add the extent to the list of extents to be free at transaction end.
* The list is maintained sorted (by block number). * The list is maintained sorted (by block number).
*/ */
void int
__xfs_free_extent_later( __xfs_free_extent_later(
struct xfs_trans *tp, struct xfs_trans *tp,
xfs_fsblock_t bno, xfs_fsblock_t bno,
...@@ -2487,6 +2491,9 @@ __xfs_free_extent_later( ...@@ -2487,6 +2491,9 @@ __xfs_free_extent_later(
#endif #endif
ASSERT(xfs_extfree_item_cache != NULL); ASSERT(xfs_extfree_item_cache != NULL);
if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbext(mp, bno, len)))
return -EFSCORRUPTED;
xefi = kmem_cache_zalloc(xfs_extfree_item_cache, xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
GFP_KERNEL | __GFP_NOFAIL); GFP_KERNEL | __GFP_NOFAIL);
xefi->xefi_startblock = bno; xefi->xefi_startblock = bno;
...@@ -2510,6 +2517,7 @@ __xfs_free_extent_later( ...@@ -2510,6 +2517,7 @@ __xfs_free_extent_later(
xfs_extent_free_get_group(mp, xefi); xfs_extent_free_get_group(mp, xefi);
xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &xefi->xefi_list); xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &xefi->xefi_list);
return 0;
} }
#ifdef DEBUG #ifdef DEBUG
...@@ -2670,7 +2678,9 @@ xfs_alloc_fix_freelist( ...@@ -2670,7 +2678,9 @@ xfs_alloc_fix_freelist(
goto out_agbp_relse; goto out_agbp_relse;
/* defer agfl frees */ /* defer agfl frees */
xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo); error = xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo);
if (error)
goto out_agbp_relse;
} }
targs.tp = tp; targs.tp = tp;
......
...@@ -230,7 +230,7 @@ xfs_buf_to_agfl_bno( ...@@ -230,7 +230,7 @@ xfs_buf_to_agfl_bno(
return bp->b_addr; return bp->b_addr;
} }
void __xfs_free_extent_later(struct xfs_trans *tp, xfs_fsblock_t bno, int __xfs_free_extent_later(struct xfs_trans *tp, xfs_fsblock_t bno,
xfs_filblks_t len, const struct xfs_owner_info *oinfo, xfs_filblks_t len, const struct xfs_owner_info *oinfo,
bool skip_discard); bool skip_discard);
...@@ -254,14 +254,14 @@ void xfs_extent_free_get_group(struct xfs_mount *mp, ...@@ -254,14 +254,14 @@ void xfs_extent_free_get_group(struct xfs_mount *mp,
#define XFS_EFI_ATTR_FORK (1U << 1) /* freeing attr fork block */ #define XFS_EFI_ATTR_FORK (1U << 1) /* freeing attr fork block */
#define XFS_EFI_BMBT_BLOCK (1U << 2) /* freeing bmap btree block */ #define XFS_EFI_BMBT_BLOCK (1U << 2) /* freeing bmap btree block */
static inline void static inline int
xfs_free_extent_later( xfs_free_extent_later(
struct xfs_trans *tp, struct xfs_trans *tp,
xfs_fsblock_t bno, xfs_fsblock_t bno,
xfs_filblks_t len, xfs_filblks_t len,
const struct xfs_owner_info *oinfo) const struct xfs_owner_info *oinfo)
{ {
__xfs_free_extent_later(tp, bno, len, oinfo, false); return __xfs_free_extent_later(tp, bno, len, oinfo, false);
} }
......
...@@ -572,8 +572,12 @@ xfs_bmap_btree_to_extents( ...@@ -572,8 +572,12 @@ xfs_bmap_btree_to_extents(
cblock = XFS_BUF_TO_BLOCK(cbp); cblock = XFS_BUF_TO_BLOCK(cbp);
if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
return error; return error;
xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo); error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo);
if (error)
return error;
ip->i_nblocks--; ip->i_nblocks--;
xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
xfs_trans_binval(tp, cbp); xfs_trans_binval(tp, cbp);
...@@ -5230,10 +5234,12 @@ xfs_bmap_del_extent_real( ...@@ -5230,10 +5234,12 @@ xfs_bmap_del_extent_real(
if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
xfs_refcount_decrease_extent(tp, del); xfs_refcount_decrease_extent(tp, del);
} else { } else {
__xfs_free_extent_later(tp, del->br_startblock, error = __xfs_free_extent_later(tp, del->br_startblock,
del->br_blockcount, NULL, del->br_blockcount, NULL,
(bflags & XFS_BMAPI_NODISCARD) || (bflags & XFS_BMAPI_NODISCARD) ||
del->br_state == XFS_EXT_UNWRITTEN); del->br_state == XFS_EXT_UNWRITTEN);
if (error)
goto done;
} }
} }
......
...@@ -268,11 +268,14 @@ xfs_bmbt_free_block( ...@@ -268,11 +268,14 @@ xfs_bmbt_free_block(
struct xfs_trans *tp = cur->bc_tp; struct xfs_trans *tp = cur->bc_tp;
xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp)); xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, xfs_buf_daddr(bp));
struct xfs_owner_info oinfo; struct xfs_owner_info oinfo;
int error;
xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork); xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, cur->bc_ino.whichfork);
xfs_free_extent_later(cur->bc_tp, fsbno, 1, &oinfo); error = xfs_free_extent_later(cur->bc_tp, fsbno, 1, &oinfo);
ip->i_nblocks--; if (error)
return error;
ip->i_nblocks--;
xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
return 0; return 0;
......
...@@ -1834,7 +1834,7 @@ xfs_dialloc( ...@@ -1834,7 +1834,7 @@ xfs_dialloc(
* might be sparse and only free the regions that are allocated as part of the * might be sparse and only free the regions that are allocated as part of the
* chunk. * chunk.
*/ */
STATIC void static int
xfs_difree_inode_chunk( xfs_difree_inode_chunk(
struct xfs_trans *tp, struct xfs_trans *tp,
xfs_agnumber_t agno, xfs_agnumber_t agno,
...@@ -1851,10 +1851,10 @@ xfs_difree_inode_chunk( ...@@ -1851,10 +1851,10 @@ xfs_difree_inode_chunk(
if (!xfs_inobt_issparse(rec->ir_holemask)) { if (!xfs_inobt_issparse(rec->ir_holemask)) {
/* not sparse, calculate extent info directly */ /* not sparse, calculate extent info directly */
xfs_free_extent_later(tp, XFS_AGB_TO_FSB(mp, agno, sagbno), return xfs_free_extent_later(tp,
XFS_AGB_TO_FSB(mp, agno, sagbno),
M_IGEO(mp)->ialloc_blks, M_IGEO(mp)->ialloc_blks,
&XFS_RMAP_OINFO_INODES); &XFS_RMAP_OINFO_INODES);
return;
} }
/* holemask is only 16-bits (fits in an unsigned long) */ /* holemask is only 16-bits (fits in an unsigned long) */
...@@ -1871,6 +1871,8 @@ xfs_difree_inode_chunk( ...@@ -1871,6 +1871,8 @@ xfs_difree_inode_chunk(
XFS_INOBT_HOLEMASK_BITS); XFS_INOBT_HOLEMASK_BITS);
nextbit = startidx + 1; nextbit = startidx + 1;
while (startidx < XFS_INOBT_HOLEMASK_BITS) { while (startidx < XFS_INOBT_HOLEMASK_BITS) {
int error;
nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS, nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
nextbit); nextbit);
/* /*
...@@ -1896,8 +1898,11 @@ xfs_difree_inode_chunk( ...@@ -1896,8 +1898,11 @@ xfs_difree_inode_chunk(
ASSERT(agbno % mp->m_sb.sb_spino_align == 0); ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
ASSERT(contigblk % mp->m_sb.sb_spino_align == 0); ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
xfs_free_extent_later(tp, XFS_AGB_TO_FSB(mp, agno, agbno), error = xfs_free_extent_later(tp,
XFS_AGB_TO_FSB(mp, agno, agbno),
contigblk, &XFS_RMAP_OINFO_INODES); contigblk, &XFS_RMAP_OINFO_INODES);
if (error)
return error;
/* reset range to current bit and carry on... */ /* reset range to current bit and carry on... */
startidx = endidx = nextbit; startidx = endidx = nextbit;
...@@ -1905,6 +1910,7 @@ xfs_difree_inode_chunk( ...@@ -1905,6 +1910,7 @@ xfs_difree_inode_chunk(
next: next:
nextbit++; nextbit++;
} }
return 0;
} }
STATIC int STATIC int
...@@ -2003,7 +2009,9 @@ xfs_difree_inobt( ...@@ -2003,7 +2009,9 @@ xfs_difree_inobt(
goto error0; goto error0;
} }
xfs_difree_inode_chunk(tp, pag->pag_agno, &rec); error = xfs_difree_inode_chunk(tp, pag->pag_agno, &rec);
if (error)
goto error0;
} else { } else {
xic->deleted = false; xic->deleted = false;
......
...@@ -1151,8 +1151,10 @@ xfs_refcount_adjust_extents( ...@@ -1151,8 +1151,10 @@ xfs_refcount_adjust_extents(
fsbno = XFS_AGB_TO_FSB(cur->bc_mp, fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
cur->bc_ag.pag->pag_agno, cur->bc_ag.pag->pag_agno,
tmp.rc_startblock); tmp.rc_startblock);
xfs_free_extent_later(cur->bc_tp, fsbno, error = xfs_free_extent_later(cur->bc_tp, fsbno,
tmp.rc_blockcount, NULL); tmp.rc_blockcount, NULL);
if (error)
goto out_error;
} }
(*agbno) += tmp.rc_blockcount; (*agbno) += tmp.rc_blockcount;
...@@ -1210,8 +1212,10 @@ xfs_refcount_adjust_extents( ...@@ -1210,8 +1212,10 @@ xfs_refcount_adjust_extents(
fsbno = XFS_AGB_TO_FSB(cur->bc_mp, fsbno = XFS_AGB_TO_FSB(cur->bc_mp,
cur->bc_ag.pag->pag_agno, cur->bc_ag.pag->pag_agno,
ext.rc_startblock); ext.rc_startblock);
xfs_free_extent_later(cur->bc_tp, fsbno, error = xfs_free_extent_later(cur->bc_tp, fsbno,
ext.rc_blockcount, NULL); ext.rc_blockcount, NULL);
if (error)
goto out_error;
} }
skip: skip:
...@@ -1976,7 +1980,10 @@ xfs_refcount_recover_cow_leftovers( ...@@ -1976,7 +1980,10 @@ xfs_refcount_recover_cow_leftovers(
rr->rr_rrec.rc_blockcount); rr->rr_rrec.rc_blockcount);
/* Free the block. */ /* Free the block. */
xfs_free_extent_later(tp, fsb, rr->rr_rrec.rc_blockcount, NULL); error = xfs_free_extent_later(tp, fsb,
rr->rr_rrec.rc_blockcount, NULL);
if (error)
goto out_trans;
error = xfs_trans_commit(tp); error = xfs_trans_commit(tp);
if (error) if (error)
......
...@@ -616,8 +616,10 @@ xfs_reflink_cancel_cow_blocks( ...@@ -616,8 +616,10 @@ xfs_reflink_cancel_cow_blocks(
xfs_refcount_free_cow_extent(*tpp, del.br_startblock, xfs_refcount_free_cow_extent(*tpp, del.br_startblock,
del.br_blockcount); del.br_blockcount);
xfs_free_extent_later(*tpp, del.br_startblock, error = xfs_free_extent_later(*tpp, del.br_startblock,
del.br_blockcount, NULL); del.br_blockcount, NULL);
if (error)
break;
/* Roll the transaction */ /* Roll the transaction */
error = xfs_defer_finish(tpp); error = xfs_defer_finish(tpp);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment