Skip to contentSkip to conversation

xfs_repair: fix totally broken unit conversion in directory invalidation

PATCH In review Darrick J. Wong 2 participants 4 messages Dec 18, 2019 – Dec 24, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_repair: fix totally broken unit conversion in directory invalidation

From: Darrick J. Wong <[email protected]>

Your humble author forgot that xfs_dablk_t has the same units as xfs_fileoff_t, and totally screwed up the directory buffer invalidation loop in dir_binval. Not only is there an off-by-one error in the loop conditional, but the unit conversions are wrong.

Fix all this stupidity by adding a for loop macro to take care of these details for us so that everyone can iterate all logical directory blocks (xfs_dir2_db_t) that start within a given bmbt record.

The pre-5.5 xfs_da_get_buf implementation mostly hides the off-by-one error because dir_binval turns on "don't complain if no mapping" mode, but on dirblocksize > fsblocksize filesystems the incorrect units can cause us to miss invalidating some blocks, which can lead to other buffer cache errors later.

---
 libxfs/xfs_dir2.h |   10 ++++++++++
 repair/phase6.c   |    8 ++------
 2 files changed, 12 insertions(+), 6 deletions(-)
libxfs/xfs_dir2.h +10 -0
index f5424477..13221243 100644
@@ -308,6 +308,16 @@ xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
 		  sizeof(struct xfs_dir2_leaf_tail));
 }
 
+/*
+ * For a given dir/attr geometry and extent mapping record, walk every file
+ * offset block (xfs_dablk_t) in the mapping that corresponds to the start
+ * of a logical directory block (xfs_dir2_db_t).
+ */
+#define for_each_xfs_bmap_dabno(geo, irec, dabno) \
+	for ((dabno) = round_up((irec)->br_startoff, (geo)->fsbcount); \
+	     (dabno) < (irec)->br_startoff + (irec)->br_blockcount; \
+	     (dabno) += (geo)->fsbcount)
+
 /*
  * The Linux API doesn't pass down the total size of the buffer
  * we read into down to the filesystem.  With the filldir concept
repair/phase6.c +2 -6
index 91d208a6..a4dd3188 100644
@@ -1276,7 +1276,7 @@ dir_binval(
 	struct xfs_ifork	*ifp;
 	struct xfs_da_geometry	*geo;
 	struct xfs_buf		*bp;
-	xfs_dablk_t		dabno, end_dabno;
+	xfs_dablk_t		dabno;
 	int			error = 0;
 
 	if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
@@ -1286,11 +1286,7 @@ dir_binval(
 	geo = tp->t_mountp->m_dir_geo;
 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
 	for_each_xfs_iext(ifp, &icur, &rec) {
-		dabno = xfs_dir2_db_to_da(geo, rec.br_startoff +
-				geo->fsbcount - 1);
-		end_dabno = xfs_dir2_db_to_da(geo, rec.br_startoff +
-				rec.br_blockcount);
-		for (; dabno <= end_dabno; dabno += geo->fsbcount) {
+		for_each_xfs_bmap_dabno(geo, &rec, dabno) {
 			bp = NULL;
 			error = -libxfs_da_get_buf(tp, ip, dabno, -2, &bp,
 					whichfork);
#1 lore raw
Quoted 11 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Your humble author forgot that xfs_dablk_t has the same units as xfs_fileoff_t, and totally screwed up the directory buffer invalidation loop in dir_binval. Not only is there an off-by-one error in the loop conditional, but the unit conversions are wrong.

Fix all this stupidity by adding a for loop macro to take care of these details for us so that everyone can iterate all logical directory blocks (xfs_dir2_db_t) that start within a given bmbt record.

Quoted 7 lines

The pre-5.5 xfs_da_get_buf implementation mostly hides the off-by-one error because dir_binval turns on "don't complain if no mapping" mode, but on dirblocksize > fsblocksize filesystems the incorrect units can cause us to miss invalidating some blocks, which can lead to other buffer cache errors later.

Fixes f9c559f4e4fb4 ("xfs_repair: invalidate dirty dir buffers when we zap a directory")

--D

Quoted 52 lines
---
 libxfs/xfs_dir2.h |   10 ++++++++++
 repair/phase6.c   |    8 ++------
 2 files changed, 12 insertions(+), 6 deletions(-)
libxfs/xfs_dir2.h +10 -0
index f5424477..13221243 100644
@@ -308,6 +308,16 @@ xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
 		  sizeof(struct xfs_dir2_leaf_tail));
 }
 
+/*
+ * For a given dir/attr geometry and extent mapping record, walk every file
+ * offset block (xfs_dablk_t) in the mapping that corresponds to the start
+ * of a logical directory block (xfs_dir2_db_t).
+ */
+#define for_each_xfs_bmap_dabno(geo, irec, dabno) \
+	for ((dabno) = round_up((irec)->br_startoff, (geo)->fsbcount); \
+	     (dabno) < (irec)->br_startoff + (irec)->br_blockcount; \
+	     (dabno) += (geo)->fsbcount)
+
 /*
  * The Linux API doesn't pass down the total size of the buffer
  * we read into down to the filesystem.  With the filldir concept
repair/phase6.c +2 -6
index 91d208a6..a4dd3188 100644
@@ -1276,7 +1276,7 @@ dir_binval(
 	struct xfs_ifork	*ifp;
 	struct xfs_da_geometry	*geo;
 	struct xfs_buf		*bp;
-	xfs_dablk_t		dabno, end_dabno;
+	xfs_dablk_t		dabno;
 	int			error = 0;
 
 	if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
@@ -1286,11 +1286,7 @@ dir_binval(
 	geo = tp->t_mountp->m_dir_geo;
 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
 	for_each_xfs_iext(ifp, &icur, &rec) {
-		dabno = xfs_dir2_db_to_da(geo, rec.br_startoff +
-				geo->fsbcount - 1);
-		end_dabno = xfs_dir2_db_to_da(geo, rec.br_startoff +
-				rec.br_blockcount);
-		for (; dabno <= end_dabno; dabno += geo->fsbcount) {
+		for_each_xfs_bmap_dabno(geo, &rec, dabno) {
 			bp = NULL;
 			error = -libxfs_da_get_buf(tp, ip, dabno, -2, &bp,
 					whichfork);
#2 lore raw
In reply to Darrick J. Wong
Quoted 6 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Your humble author forgot that xfs_dablk_t has the same units as xfs_fileoff_t, and totally screwed up the directory buffer invalidation loop in dir_binval. Not only is there an off-by-one error in the loop conditional, but the unit conversions are wrong.

Can we kill off xfs_dablk_t? I found the concept very, very confusing when touching the dir code.

Quoted 5 lines
libxfs/xfs_dir2.h +0 -0
@@ -308,6 +308,16 @@ xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
 		  sizeof(struct xfs_dir2_leaf_tail));
 }

libxfs/xfs_dir2.h xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp) · line 319

Quoted 10 lines
libxfs/xfs_dir2.h +9 -0
 
+/*
+ * For a given dir/attr geometry and extent mapping record, walk every file
+ * offset block (xfs_dablk_t) in the mapping that corresponds to the start
+ * of a logical directory block (xfs_dir2_db_t).
+ */
+#define for_each_xfs_bmap_dabno(geo, irec, dabno) \
+	for ((dabno) = round_up((irec)->br_startoff, (geo)->fsbcount); \
+	     (dabno) < (irec)->br_startoff + (irec)->br_blockcount; \
+	     (dabno) += (geo)->fsbcount)

I think not having the magic for macro would be cleaner..

Quoted 7 lines
libxfs/xfs_dir2.h +1 -0
+	xfs_dablk_t		dabno;
 	int			error = 0;
 
 	if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
@@ -1286,11 +1286,7 @@ dir_binval(
 	geo = tp->t_mountp->m_dir_geo;
 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);

libxfs/xfs_dir2.h dir_binval( · line 1292

Quoted 10 lines
libxfs/xfs_dir2.h +1 -5
 	for_each_xfs_iext(ifp, &icur, &rec) {
-		dabno = xfs_dir2_db_to_da(geo, rec.br_startoff +
-				geo->fsbcount - 1);
-		end_dabno = xfs_dir2_db_to_da(geo, rec.br_startoff +
-				rec.br_blockcount);
-		for (; dabno <= end_dabno; dabno += geo->fsbcount) {
+		for_each_xfs_bmap_dabno(geo, &rec, dabno) {
 			bp = NULL;
 			error = -libxfs_da_get_buf(tp, ip, dabno, -2, &bp,
 					whichfork);

But either way, the fix looks good.

#3 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Your humble author forgot that xfs_dablk_t has the same units as xfs_fileoff_t, and totally screwed up the directory buffer invalidation loop in dir_binval. Not only is there an off-by-one error in the loop conditional, but the unit conversions are wrong.

Can we kill off xfs_dablk_t? I found the concept very, very confusing when touching the dir code.

I personally wouldn't mind if that happened (as a separate patch).

Quoted 6 lines
Quoted 6 lines
libxfs/xfs_dir2.h +0 -0
@@ -308,6 +308,16 @@ xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp)
 		  sizeof(struct xfs_dir2_leaf_tail));
 }
 
Quoted 11 lines

libxfs/xfs_dir2.h xfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp) · line 319

Quoted 9 lines
libxfs/xfs_dir2.h +9 -0
+/*
+ * For a given dir/attr geometry and extent mapping record, walk every file
+ * offset block (xfs_dablk_t) in the mapping that corresponds to the start
+ * of a logical directory block (xfs_dir2_db_t).
+ */
+#define for_each_xfs_bmap_dabno(geo, irec, dabno) \
+	for ((dabno) = round_up((irec)->br_startoff, (geo)->fsbcount); \
+	     (dabno) < (irec)->br_startoff + (irec)->br_blockcount; \
+	     (dabno) += (geo)->fsbcount)

I think not having the magic for macro would be cleaner..

Eh, yeah, we don't really need it...

--D

Quoted 20 lines
Quoted 8 lines
libxfs/xfs_dir2.h +1 -0
+	xfs_dablk_t		dabno;
 	int			error = 0;
 
 	if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
@@ -1286,11 +1286,7 @@ dir_binval(
 	geo = tp->t_mountp->m_dir_geo;
 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);

libxfs/xfs_dir2.h dir_binval( · line 1292

Quoted 10 lines
libxfs/xfs_dir2.h +1 -5
 	for_each_xfs_iext(ifp, &icur, &rec) {
-		dabno = xfs_dir2_db_to_da(geo, rec.br_startoff +
-				geo->fsbcount - 1);
-		end_dabno = xfs_dir2_db_to_da(geo, rec.br_startoff +
-				rec.br_blockcount);
-		for (; dabno <= end_dabno; dabno += geo->fsbcount) {
+		for_each_xfs_bmap_dabno(geo, &rec, dabno) {
 			bp = NULL;
 			error = -libxfs_da_get_buf(tp, ip, dabno, -2, &bp,
 					whichfork);

But either way, the fix looks good.