Skip to contentSkip to conversation

xfs: reserve enough blocks to handle btree splits when remapping

PATCH Superseded Darrick J. Wong 3 participants 4 messages Apr 25, 2017 – Apr 27, 2017 XFS filesystem

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 xfs: reserve enough blocks to handle btree splits when remapping

In xfs_reflink_end_cow, we erroneously reserve only enough blocks to handle adding 1 extent. This is problematic if we fragment free space, have to do CoW, and then have to perform multiple bmap btree expansions. Furthermore, the BUI recovery routine doesn't reserve /any/ blocks to handle btree splits, so log recovery fails after our first error causes the filesystem to go down.

Therefore, refactor the transaction block reservation macros until we have a macro that works for our deferred (re)mapping activities, and fix both problems by using that macro.

With 1k blocks we can hit this fairly often in g/187 if the scratch fs is big enough.

---
v3: don't use the swap-extents-with-rmap block reservation for cow remapping; we should already have sufficient per-ag reservation
v2: avoid 64-bit division when calculating block reservation

---
 fs/xfs/libxfs/xfs_trans_space.h |   13 +++++++------
 fs/xfs/xfs_bmap_item.c          |    7 ++++++-
 fs/xfs/xfs_reflink.c            |   12 ++++++++++--
 3 files changed, 23 insertions(+), 9 deletions(-)
fs/xfs/libxfs/xfs_trans_space.h +7 -6
index 7917f6e..0044e14 100644
@@ -23,6 +23,11 @@
  */
 #define XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)    \
 		(((mp)->m_rmap_mxr[0]) - ((mp)->m_rmap_mnr[0]))
+#define XFS_RMAPADD_SPACE_RES(mp) ((mp)->m_rmap_maxlevels)
+#define XFS_NRMAPADD_SPACE_RES(mp,b,w)\
+	(((b + XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp) - 1) / \
+	  XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)) * \
+	  XFS_RMAPADD_SPACE_RES(mp))
 #define XFS_MAX_CONTIG_EXTENTS_PER_BLOCK(mp)    \
 		(((mp)->m_alloc_mxr[0]) - ((mp)->m_alloc_mnr[0]))
 #define	XFS_EXTENTADD_SPACE_RES(mp,w)	(XFS_BM_MAXLEVELS(mp,w) - 1)
@@ -31,12 +36,8 @@
 	  XFS_MAX_CONTIG_EXTENTS_PER_BLOCK(mp)) * \
 	  XFS_EXTENTADD_SPACE_RES(mp,w))
 #define XFS_SWAP_RMAP_SPACE_RES(mp,b,w)\
-	(((b + XFS_MAX_CONTIG_EXTENTS_PER_BLOCK(mp) - 1) / \
-	  XFS_MAX_CONTIG_EXTENTS_PER_BLOCK(mp)) * \
-	  XFS_EXTENTADD_SPACE_RES(mp,w) + \
-	 ((b + XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp) - 1) / \
-	  XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)) * \
-	  (mp)->m_rmap_maxlevels)
+	(XFS_NEXTENTADD_SPACE_RES((mp), (b), (w)) + \
+	 XFS_NRMAPADD_SPACE_RES((mp), (b), (w)))
 #define	XFS_DAENTER_1B(mp,w)	\
 	((w) == XFS_DATA_FORK ? (mp)->m_dir_geo->fsbcount : 1)
 #define	XFS_DAENTER_DBS(mp,w)	\
fs/xfs/xfs_bmap_item.c +6 -1
index 2d70173..378f144 100644
@@ -34,6 +34,8 @@
 #include "xfs_bmap.h"
 #include "xfs_icache.h"
 #include "xfs_trace.h"
+#include "xfs_bmap_btree.h"
+#include "xfs_trans_space.h"
 
 
 kmem_zone_t	*xfs_bui_zone;
@@ -403,6 +405,7 @@ xfs_bui_recover(
 	struct xfs_inode		*ip = NULL;
 	struct xfs_defer_ops		dfops;
 	xfs_fsblock_t			firstfsb;
+	unsigned int			resblks;
 
 	ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
 
@@ -447,7 +450,9 @@ xfs_bui_recover(
 		return -EIO;
 	}
 
-	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
+	resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
+	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks, 0,
+			0, &tp);
 	if (error)
 		return error;
 	budp = xfs_trans_get_bud(tp, buip);
fs/xfs/xfs_reflink.c +10 -2
index c0f3754..aab156a 100644
@@ -705,8 +705,16 @@ xfs_reflink_end_cow(
 	offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
 	end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
 
-	/* Start a rolling transaction to switch the mappings */
-	resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
+	/*
+	 * Start a rolling transaction to switch the mappings.  We're
+	 * unlikely ever to have to remap 16T worth of single-block
+	 * extents, so just cap the worst case extent count to 2^32-1.
+	 * Stick a warning in just in case, and avoid 64-bit division.
+	 */
+	WARN_ON(end_fsb - offset_fsb + 1 > UINT_MAX);
+	resblks = min_t(xfs_fileoff_t, UINT_MAX, end_fsb - offset_fsb + 1);
+	resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount, resblks,
+			XFS_DATA_FORK);
 	error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
 			resblks, 0, 0, &tp);
 	if (error)
Reviewed-by on v3 1/1
#1 lore raw
Quoted 11 lines · Darrick J. Wong

In xfs_reflink_end_cow, we erroneously reserve only enough blocks to handle adding 1 extent. This is problematic if we fragment free space, have to do CoW, and then have to perform multiple bmap btree expansions. Furthermore, the BUI recovery routine doesn't reserve /any/ blocks to handle btree splits, so log recovery fails after our first error causes the filesystem to go down.

Therefore, refactor the transaction block reservation macros until we have a macro that works for our deferred (re)mapping activities, and fix both problems by using that macro.

Quoted 5 lines

With 1k blocks we can hit this fairly often in g/187 if the scratch fs is big enough.

---

Quoted 99 lines

v3: don't use the swap-extents-with-rmap block reservation for cow remapping; we should already have sufficient per-ag reservation
v2: avoid 64-bit division when calculating block reservation

---
 fs/xfs/libxfs/xfs_trans_space.h |   13 +++++++------
 fs/xfs/xfs_bmap_item.c          |    7 ++++++-
 fs/xfs/xfs_reflink.c            |   12 ++++++++++--
 3 files changed, 23 insertions(+), 9 deletions(-)
fs/xfs/libxfs/xfs_trans_space.h +7 -6
index 7917f6e..0044e14 100644
@@ -23,6 +23,11 @@
  */
 #define XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)    \
 		(((mp)->m_rmap_mxr[0]) - ((mp)->m_rmap_mnr[0]))
+#define XFS_RMAPADD_SPACE_RES(mp) ((mp)->m_rmap_maxlevels)
+#define XFS_NRMAPADD_SPACE_RES(mp,b,w)\
+	(((b + XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp) - 1) / \
+	  XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)) * \
+	  XFS_RMAPADD_SPACE_RES(mp))
 #define XFS_MAX_CONTIG_EXTENTS_PER_BLOCK(mp)    \
 		(((mp)->m_alloc_mxr[0]) - ((mp)->m_alloc_mnr[0]))
 #define	XFS_EXTENTADD_SPACE_RES(mp,w)	(XFS_BM_MAXLEVELS(mp,w) - 1)
@@ -31,12 +36,8 @@
 	  XFS_MAX_CONTIG_EXTENTS_PER_BLOCK(mp)) * \
 	  XFS_EXTENTADD_SPACE_RES(mp,w))
 #define XFS_SWAP_RMAP_SPACE_RES(mp,b,w)\
-	(((b + XFS_MAX_CONTIG_EXTENTS_PER_BLOCK(mp) - 1) / \
-	  XFS_MAX_CONTIG_EXTENTS_PER_BLOCK(mp)) * \
-	  XFS_EXTENTADD_SPACE_RES(mp,w) + \
-	 ((b + XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp) - 1) / \
-	  XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)) * \
-	  (mp)->m_rmap_maxlevels)
+	(XFS_NEXTENTADD_SPACE_RES((mp), (b), (w)) + \
+	 XFS_NRMAPADD_SPACE_RES((mp), (b), (w)))
 #define	XFS_DAENTER_1B(mp,w)	\
 	((w) == XFS_DATA_FORK ? (mp)->m_dir_geo->fsbcount : 1)
 #define	XFS_DAENTER_DBS(mp,w)	\
fs/xfs/xfs_bmap_item.c +6 -1
index 2d70173..378f144 100644
@@ -34,6 +34,8 @@
 #include "xfs_bmap.h"
 #include "xfs_icache.h"
 #include "xfs_trace.h"
+#include "xfs_bmap_btree.h"
+#include "xfs_trans_space.h"
 
 
 kmem_zone_t	*xfs_bui_zone;
@@ -403,6 +405,7 @@ xfs_bui_recover(
 	struct xfs_inode		*ip = NULL;
 	struct xfs_defer_ops		dfops;
 	xfs_fsblock_t			firstfsb;
+	unsigned int			resblks;
 
 	ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
 
@@ -447,7 +450,9 @@ xfs_bui_recover(
 		return -EIO;
 	}
 
-	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
+	resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
+	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks, 0,
+			0, &tp);
 	if (error)
 		return error;
 	budp = xfs_trans_get_bud(tp, buip);
fs/xfs/xfs_reflink.c +10 -3
index c0f3754..aab156a 100644
@@ -705,8 +705,16 @@ xfs_reflink_end_cow(
 	offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
 	end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
 
-	/* Start a rolling transaction to switch the mappings */
-	resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
+	/*
+	 * Start a rolling transaction to switch the mappings.  We're
+	 * unlikely ever to have to remap 16T worth of single-block
+	 * extents, so just cap the worst case extent count to 2^32-1.
+	 * Stick a warning in just in case, and avoid 64-bit division.
+	 */
+	WARN_ON(end_fsb - offset_fsb + 1 > UINT_MAX);
+	resblks = min_t(xfs_fileoff_t, UINT_MAX, end_fsb - offset_fsb + 1);
+	resblks = XFS_NEXTENTADD_SPACE_RES(ip->i_mount, resblks,
+			XFS_DATA_FORK);
 	error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_write,
 			resblks, 0, 0, &tp);
 	if (error)
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
In reply to Darrick J. Wong
Quoted 30 lines · Darrick J. Wong

In xfs_reflink_end_cow, we erroneously reserve only enough blocks to handle adding 1 extent. This is problematic if we fragment free space, have to do CoW, and then have to perform multiple bmap btree expansions. Furthermore, the BUI recovery routine doesn't reserve /any/ blocks to handle btree splits, so log recovery fails after our first error causes the filesystem to go down.

Therefore, refactor the transaction block reservation macros until we have a macro that works for our deferred (re)mapping activities, and fix both problems by using that macro.

With 1k blocks we can hit this fairly often in g/187 if the scratch fs is big enough.

---
v3: don't use the swap-extents-with-rmap block reservation for cow remapping; we should already have sufficient per-ag reservation
v2: avoid 64-bit division when calculating block reservation

---
 fs/xfs/libxfs/xfs_trans_space.h |   13 +++++++------
 fs/xfs/xfs_bmap_item.c          |    7 ++++++-
 fs/xfs/xfs_reflink.c            |   12 ++++++++++--
 3 files changed, 23 insertions(+), 9 deletions(-)
fs/xfs/libxfs/xfs_trans_space.h +0 -0
index 7917f6e..0044e14 100644

fs/xfs/libxfs/xfs_trans_space.h line 30

Quoted 8 lines
fs/xfs/libxfs/xfs_trans_space.h +5 -0
@@ -23,6 +23,11 @@
  */
 #define XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)    \
 		(((mp)->m_rmap_mxr[0]) - ((mp)->m_rmap_mnr[0]))
+#define XFS_RMAPADD_SPACE_RES(mp) ((mp)->m_rmap_maxlevels)
+#define XFS_NRMAPADD_SPACE_RES(mp,b,w)\
+	(((b + XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp) - 1) / \
+	  XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)) * \
+	  XFS_RMAPADD_SPACE_RES(mp))

Comments, please! (I know the existing defintions don't have any, but that's bad enough to start with..)

fs/xfs/libxfs/xfs_trans_space.h xfs_bui_recover( · line 455

Quoted 13 lines
fs/xfs/libxfs/xfs_trans_space.h +4 -1
 	xfs_fsblock_t			firstfsb;
+	unsigned int			resblks;
 
 	ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
@@ -447,7 +450,9 @@ xfs_bui_recover(
 		return -EIO;
 	}
 
-	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
+	resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
+	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks, 0,
+			0, &tp);

Do we really need that resblks variable?

Quoted 11 lines
fs/xfs/libxfs/xfs_trans_space.h +0 -0
 	if (error)
 		return error;
 	budp = xfs_trans_get_bud(tp, buip);
fs/xfs/xfs_reflink.c +0 -0
index c0f3754..aab156a 100644
@@ -705,8 +705,16 @@ xfs_reflink_end_cow(
 	offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
 	end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
 

fs/xfs/xfs_reflink.c xfs_reflink_end_cow( · line 715

Quoted 10 lines
fs/xfs/xfs_reflink.c +8 -2
-	/* Start a rolling transaction to switch the mappings */
-	resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
+	/*
+	 * Start a rolling transaction to switch the mappings.  We're
+	 * unlikely ever to have to remap 16T worth of single-block
+	 * extents, so just cap the worst case extent count to 2^32-1.
+	 * Stick a warning in just in case, and avoid 64-bit division.
+	 */
+	WARN_ON(end_fsb - offset_fsb + 1 > UINT_MAX);
+	resblks = min_t(xfs_fileoff_t, UINT_MAX, end_fsb - offset_fsb + 1);

I don't like unlikely statements. What prevents us from doing so? I think the way Linux limits writes to 32-bits using MAX_RW_COUNT does, but then the language should be more assertive here. I think we should also shut down the fs here if the assert fails - otherwise we'll leave a partially converted region around.

A BUILD_BUG_ON on MAX_RW_COUNT might also be useful to make the whole scheme even safer.

#3 lore raw
Quoted 26 lines · Christoph Hellwig
Quoted 25 lines · Darrick J. Wong

In xfs_reflink_end_cow, we erroneously reserve only enough blocks to handle adding 1 extent. This is problematic if we fragment free space, have to do CoW, and then have to perform multiple bmap btree expansions. Furthermore, the BUI recovery routine doesn't reserve /any/ blocks to handle btree splits, so log recovery fails after our first error causes the filesystem to go down.

Therefore, refactor the transaction block reservation macros until we have a macro that works for our deferred (re)mapping activities, and fix both problems by using that macro.

With 1k blocks we can hit this fairly often in g/187 if the scratch fs is big enough.

---
v3: don't use the swap-extents-with-rmap block reservation for cow remapping; we should already have sufficient per-ag reservation
v2: avoid 64-bit division when calculating block reservation

---
 fs/xfs/libxfs/xfs_trans_space.h |   13 +++++++------
 fs/xfs/xfs_bmap_item.c          |    7 ++++++-
 fs/xfs/xfs_reflink.c            |   12 ++++++++++--
 3 files changed, 23 insertions(+), 9 deletions(-)
Quoted 16 lines

fs/xfs/libxfs/xfs_trans_space.h line 30

Quoted 13 lines
fs/xfs/libxfs/xfs_trans_space.h +5 -0
index 7917f6e..0044e14 100644
@@ -23,6 +23,11 @@
  */
 #define XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)    \
 		(((mp)->m_rmap_mxr[0]) - ((mp)->m_rmap_mnr[0]))
+#define XFS_RMAPADD_SPACE_RES(mp) ((mp)->m_rmap_maxlevels)
+#define XFS_NRMAPADD_SPACE_RES(mp,b,w)\
+	(((b + XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp) - 1) / \
+	  XFS_MAX_CONTIG_RMAPS_PER_BLOCK(mp)) * \
+	  XFS_RMAPADD_SPACE_RES(mp))

Comments, please! (I know the existing defintions don't have any, but that's bad enough to start with..)

Ok.

Quoted 9 lines
Quoted 9 lines
fs/xfs/libxfs/xfs_trans_space.h +1 -0
 	xfs_fsblock_t			firstfsb;
+	unsigned int			resblks;
 
 	ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
@@ -447,7 +450,9 @@ xfs_bui_recover(
 		return -EIO;
 	}
 
Quoted 6 lines

fs/xfs/libxfs/xfs_trans_space.h xfs_bui_recover( · line 455

Quoted 4 lines
fs/xfs/libxfs/xfs_trans_space.h +3 -1
-	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
+	resblks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
+	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks, 0,
+			0, &tp);

Do we really need that resblks variable?

No.

Quoted 11 lines
Quoted 11 lines
fs/xfs/libxfs/xfs_trans_space.h +0 -0
 	if (error)
 		return error;
 	budp = xfs_trans_get_bud(tp, buip);
fs/xfs/xfs_reflink.c +0 -0
index c0f3754..aab156a 100644
@@ -705,8 +705,16 @@ xfs_reflink_end_cow(
 	offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
 	end_fsb = XFS_B_TO_FSB(ip->i_mount, offset + count);
 
Quoted 16 lines

fs/xfs/xfs_reflink.c xfs_reflink_end_cow( · line 715

Quoted 10 lines
fs/xfs/xfs_reflink.c +8 -2
-	/* Start a rolling transaction to switch the mappings */
-	resblks = XFS_EXTENTADD_SPACE_RES(ip->i_mount, XFS_DATA_FORK);
+	/*
+	 * Start a rolling transaction to switch the mappings.  We're
+	 * unlikely ever to have to remap 16T worth of single-block
+	 * extents, so just cap the worst case extent count to 2^32-1.
+	 * Stick a warning in just in case, and avoid 64-bit division.
+	 */
+	WARN_ON(end_fsb - offset_fsb + 1 > UINT_MAX);
+	resblks = min_t(xfs_fileoff_t, UINT_MAX, end_fsb - offset_fsb + 1);

I don't like unlikely statements. What prevents us from doing so? I think the way Linux limits writes to 32-bits using MAX_RW_COUNT does, but then the language should be more assertive here. I think we should also shut down the fs here if the assert fails - otherwise we'll leave a partially converted region around.

In the long run, I should just rewrite this to loop around if we are asked to do too much at once.

Quoted 2 lines

A BUILD_BUG_ON on MAX_RW_COUNT might also be useful to make the whole scheme even safer.

Yeah, we could do that too.

--D

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html