Skip to contentSkip to conversation

xfs_repair: fix link counts update following repair of a bad block

PATCH Superseded [email protected] 2 participants 3 messages Apr 15, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfs_repair: fix link counts update following repair of a bad block

From: Bill O'Donnell <[email protected]>

Updating nlinks, following repair of a bad block needs a bit of work. In unique cases, 2 runs of xfs_repair is needed to adjust the count to the proper value. This patch modifies location of longform_dir2_entry_check, moving longform_dir2_entry_check_data to run after the check_dir3_header error check. This results in the hashtab to be correctly filled and those entries don't end up in lost+found, and nlinks is properly adjusted on the first xfs_repair pass.

---
v2: add logic to cover shortform directory.

 repair/phase6.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
repair/phase6.c +17 -3
index dbc090a54139..8fc1c3896d2b 100644
@@ -2426,6 +2426,23 @@ longform_dir2_entry_check(
 
 		/* check v5 metadata */
 		if (xfs_has_crc(mp)) {
+			longform_dir2_entry_check_data(mp, ip, num_illegal,
+				need_dot,
+				irec, ino_offset, bp, hashtab,
+				&freetab, da_bno, fmt == XFS_DIR2_FMT_BLOCK);
+			error = check_dir3_header(mp, bp, ino);
+			if (error) {
+				fixit++;
+				if (fmt == XFS_DIR2_FMT_BLOCK)
+					goto out_fix;
+
+				libxfs_buf_relse(bp);
+				bp = NULL;
+				continue;
+			}
+		}
+		else {
+			/* No crc. Directory appears to be shortform. */
 			error = check_dir3_header(mp, bp, ino);
 			if (error) {
 				fixit++;
@@ -2438,9 +2455,6 @@ longform_dir2_entry_check(
 			}
 		}
 
-		longform_dir2_entry_check_data(mp, ip, num_illegal, need_dot,
-				irec, ino_offset, bp, hashtab,
-				&freetab, da_bno, fmt == XFS_DIR2_FMT_BLOCK);
 		if (fmt == XFS_DIR2_FMT_BLOCK)
 			break;
 
Signature · 1 line
2.49.0
#1 lore raw
Quoted 25 lines · On Tue, Apr 15, 2025 at 10:01:04AM -0500, [email protected] wrote:

From: Bill O'Donnell <[email protected]>

Updating nlinks, following repair of a bad block needs a bit of work. In unique cases, 2 runs of xfs_repair is needed to adjust the count to the proper value. This patch modifies location of longform_dir2_entry_check, moving longform_dir2_entry_check_data to run after the check_dir3_header error check. This results in the hashtab to be correctly filled and those entries don't end up in lost+found, and nlinks is properly adjusted on the first xfs_repair pass.

---
v2: add logic to cover shortform directory.

 repair/phase6.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
repair/phase6.c +0 -0
index dbc090a54139..8fc1c3896d2b 100644

repair/phase6.c longform_dir2_entry_check( · line 2435

Quoted 10 lines
repair/phase6.c +7 -0
@@ -2426,6 +2426,23 @@ longform_dir2_entry_check(
 
 		/* check v5 metadata */
 		if (xfs_has_crc(mp)) {
+			longform_dir2_entry_check_data(mp, ip, num_illegal,
+				need_dot,
+				irec, ino_offset, bp, hashtab,
+				&freetab, da_bno, fmt == XFS_DIR2_FMT_BLOCK);
+			error = check_dir3_header(mp, bp, ino);
+			if (error) {
+				fixit++;

I think what you're trying to do here is to get longform_dir2_entry_check_data to try to find directory entries in the directory block (no matter how damaged it is). Then if the dir3 header fields are wrong, we bump fixit so that the directory gets rebuilt from the salvaged directory entries. Right?

So I think you could structure this more like:

		/* salvage any dirents that look ok */
		longform_dir2_entry_check_data(...);

		/* check v5 metadata */
		if (xfs_has_crc(mp)) {
			error = check_dir3_header(mp, bp, ino);
			if (error) {
				fixit++;
				if (fmt == XFS_DIR2_FMT_BLOCK)
					goto out_fix;

				libxfs_buf_relse(bp);
				bp = NULL;
				continue;
			}
		}

		if (fmt == XFS_DIR2_FMT_BLOCK)
			break;

		libxfs_buf_relse(bp);
		bp = NULL;
	}

repair/phase6.c longform_dir2_entry_check( · line 2446

Quoted 11 lines
repair/phase6.c +10 -0
+				if (fmt == XFS_DIR2_FMT_BLOCK)
+					goto out_fix;
+
+				libxfs_buf_relse(bp);
+				bp = NULL;
+				continue;
+			}
+		}
+		else {
+			/* No crc. Directory appears to be shortform. */
 			error = check_dir3_header(mp, bp, ino);

dir3 headers (as opposed to dir2 headers) are a crc-only feature, so this isn't correct either.

repair/phase6.c longform_dir2_entry_check( · line 2443

Quoted 9 lines
repair/phase6.c +0 -3
 			if (error) {
 				fixit++;
@@ -2438,9 +2455,6 @@ longform_dir2_entry_check(
 			}
 		}
 
-		longform_dir2_entry_check_data(mp, ip, num_illegal, need_dot,
-				irec, ino_offset, bp, hashtab,
-				&freetab, da_bno, fmt == XFS_DIR2_FMT_BLOCK);

and removing this call means that we never scan a V4 directory at all.

--D

Quoted 7 lines
repair/phase6.c +0 -0
 		if (fmt == XFS_DIR2_FMT_BLOCK)
 			break;
 
Signature · 1 line
2.49.0
#2 lore raw
Quoted 27 lines · Darrick J. Wong
Quoted 26 lines · On Tue, Apr 15, 2025 at 10:01:04AM -0500, [email protected] wrote:

From: Bill O'Donnell <[email protected]>

Updating nlinks, following repair of a bad block needs a bit of work. In unique cases, 2 runs of xfs_repair is needed to adjust the count to the proper value. This patch modifies location of longform_dir2_entry_check, moving longform_dir2_entry_check_data to run after the check_dir3_header error check. This results in the hashtab to be correctly filled and those entries don't end up in lost+found, and nlinks is properly adjusted on the first xfs_repair pass.

---
v2: add logic to cover shortform directory.

 repair/phase6.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
repair/phase6.c +0 -0
index dbc090a54139..8fc1c3896d2b 100644
@@ -2426,6 +2426,23 @@ longform_dir2_entry_check(
 
Quoted 15 lines

repair/phase6.c longform_dir2_entry_check( · line 2435

Quoted 9 lines
repair/phase6.c +7 -0
 		/* check v5 metadata */
 		if (xfs_has_crc(mp)) {
+			longform_dir2_entry_check_data(mp, ip, num_illegal,
+				need_dot,
+				irec, ino_offset, bp, hashtab,
+				&freetab, da_bno, fmt == XFS_DIR2_FMT_BLOCK);
+			error = check_dir3_header(mp, bp, ino);
+			if (error) {
+				fixit++;

I think what you're trying to do here is to get longform_dir2_entry_check_data to try to find directory entries in the directory block (no matter how damaged it is). Then if the dir3 header fields are wrong, we bump fixit so that the directory gets rebuilt from the salvaged directory entries. Right?

Yes.

Quoted 18 lines

So I think you could structure this more like:

		/* salvage any dirents that look ok */
		longform_dir2_entry_check_data(...);

		/* check v5 metadata */
		if (xfs_has_crc(mp)) {
			error = check_dir3_header(mp, bp, ino);
			if (error) {
				fixit++;
				if (fmt == XFS_DIR2_FMT_BLOCK)
					goto out_fix;

				libxfs_buf_relse(bp);
				bp = NULL;
				continue;
			}
Quoted 8 lines
		}

		if (fmt == XFS_DIR2_FMT_BLOCK)
			break;

		libxfs_buf_relse(bp);
		bp = NULL;
	}

Ah, this makes better sense.

Quoted 15 lines

repair/phase6.c longform_dir2_entry_check( · line 2446

Quoted 12 lines
repair/phase6.c +10 -0
+				if (fmt == XFS_DIR2_FMT_BLOCK)
+					goto out_fix;
+
+				libxfs_buf_relse(bp);
+				bp = NULL;
+				continue;
+			}
+		}
+		else {
+			/* No crc. Directory appears to be shortform. */
 			error = check_dir3_header(mp, bp, ino);

dir3 headers (as opposed to dir2 headers) are a crc-only feature, so this isn't correct either.

Agree.

Quoted 7 lines
Quoted 7 lines
repair/phase6.c +0 -0
 			if (error) {
 				fixit++;
@@ -2438,9 +2455,6 @@ longform_dir2_entry_check(
 			}
 		}
 
Quoted 5 lines

repair/phase6.c longform_dir2_entry_check( · line 2443

Quoted 3 lines
repair/phase6.c +0 -3
-		longform_dir2_entry_check_data(mp, ip, num_illegal, need_dot,
-				irec, ino_offset, bp, hashtab,
-				&freetab, da_bno, fmt == XFS_DIR2_FMT_BLOCK);

and removing this call means that we never scan a V4 directory at all.

Doh! I'll fix it up and send a v3. Thanks for your review.

-Bill

Quoted 11 lines

--D

Quoted 7 lines
repair/phase6.c +0 -0
 		if (fmt == XFS_DIR2_FMT_BLOCK)
 			break;
 
Signature · 1 line
2.49.0