Skip to content

Commit

Permalink
xfs: try other AGs to allocate a BMBT block
Browse files Browse the repository at this point in the history
Prior to the introduction of reflink, allocating a block and mapping
it into a file was performed in a single transaction with a single
block reservation, and the allocator was supposed to find enough
blocks to allocate the extent and any BMBT blocks that might be
necessary (unless we're low on space).

However, due to the way copy on write works, allocation and mapping
have been split into two transactions, which means that we must be
able to handle the case where we allocate an extent for CoW but that
AG runs out of free space before the blocks can be mapped into a file,
and the mapping requires a new BMBT block.  When this happens, look in
one of the other AGs for a BMBT block instead of taking the FS down.

The same applies to the functions that convert a data fork to extents
and later btree format.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
  • Loading branch information
Darrick J. Wong committed Oct 5, 2016
1 parent 6fa164b commit 90e2056
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
30 changes: 30 additions & 0 deletions fs/xfs/libxfs/xfs_bmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ xfs_bmap_extents_to_btree(
args.type = XFS_ALLOCTYPE_START_BNO;
args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
} else if (dfops->dop_low) {
try_another_ag:
args.type = XFS_ALLOCTYPE_START_BNO;
args.fsbno = *firstblock;
} else {
Expand All @@ -767,6 +768,21 @@ xfs_bmap_extents_to_btree(
xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
return error;
}

/*
* During a CoW operation, the allocation and bmbt updates occur in
* different transactions. The mapping code tries to put new bmbt
* blocks near extents being mapped, but the only way to guarantee this
* is if the alloc and the mapping happen in a single transaction that
* has a block reservation. That isn't the case here, so if we run out
* of space we'll try again with another AG.
*/
if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
args.fsbno == NULLFSBLOCK &&
args.type == XFS_ALLOCTYPE_NEAR_BNO) {
dfops->dop_low = true;
goto try_another_ag;
}
/*
* Allocation can't fail, the space was reserved.
*/
Expand Down Expand Up @@ -902,6 +918,7 @@ xfs_bmap_local_to_extents(
* file currently fits in an inode.
*/
if (*firstblock == NULLFSBLOCK) {
try_another_ag:
args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
args.type = XFS_ALLOCTYPE_START_BNO;
} else {
Expand All @@ -914,6 +931,19 @@ xfs_bmap_local_to_extents(
if (error)
goto done;

/*
* During a CoW operation, the allocation and bmbt updates occur in
* different transactions. The mapping code tries to put new bmbt
* blocks near extents being mapped, but the only way to guarantee this
* is if the alloc and the mapping happen in a single transaction that
* has a block reservation. That isn't the case here, so if we run out
* of space we'll try again with another AG.
*/
if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
args.fsbno == NULLFSBLOCK &&
args.type == XFS_ALLOCTYPE_NEAR_BNO) {
goto try_another_ag;
}
/* Can't fail, the space was reserved. */
ASSERT(args.fsbno != NULLFSBLOCK);
ASSERT(args.len == 1);
Expand Down
17 changes: 17 additions & 0 deletions fs/xfs/libxfs/xfs_bmap_btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ xfs_bmbt_alloc_block(

if (args.fsbno == NULLFSBLOCK) {
args.fsbno = be64_to_cpu(start->l);
try_another_ag:
args.type = XFS_ALLOCTYPE_START_BNO;
/*
* Make sure there is sufficient room left in the AG to
Expand Down Expand Up @@ -482,6 +483,22 @@ xfs_bmbt_alloc_block(
if (error)
goto error0;

/*
* During a CoW operation, the allocation and bmbt updates occur in
* different transactions. The mapping code tries to put new bmbt
* blocks near extents being mapped, but the only way to guarantee this
* is if the alloc and the mapping happen in a single transaction that
* has a block reservation. That isn't the case here, so if we run out
* of space we'll try again with another AG.
*/
if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
args.fsbno == NULLFSBLOCK &&
args.type == XFS_ALLOCTYPE_NEAR_BNO) {
cur->bc_private.b.dfops->dop_low = true;
args.fsbno = cur->bc_private.b.firstblock;
goto try_another_ag;
}

if (args.fsbno == NULLFSBLOCK && args.minleft) {
/*
* Could not find an AG with enough free space to satisfy
Expand Down

0 comments on commit 90e2056

Please sign in to comment.