Skip to contentSkip to conversation

xfs_repair: handling a block with bad crc, bad uuid, and bad magic number needs fixing

PATCH Superseded [email protected] 3 participants 7 messages Mar 21, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfs_repair: handling a block with bad crc, bad uuid, and bad magic number needs fixing

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

In certain cases, if a block is so messed up that crc, uuid and magic number are all bad, we need to not only detect in phase3 but fix it properly in phase6. In the current code, the mechanism doesn't work in that it only pays attention to one of the parameters.

Note: in this case, the nlink inode link count drops to 1, but re-running xfs_repair fixes it back to 2. This is a side effect that should probably be handled in update_inode_nlinks() with separate patch. Regardless, running xfs_repair twice fixes the issue. Also, this patch fixes the issue with v5, but not v4 xfs.

v2: remove superfluous wantmagic logic

---
 repair/phase6.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
repair/phase6.c +1 -4
index 4064a84b2450..9cffbb1f4510 100644
@@ -2364,7 +2364,6 @@ longform_dir2_entry_check(
 	     da_bno = (xfs_dablk_t)next_da_bno) {
 		const struct xfs_buf_ops *ops;
 		int			 error;
-		struct xfs_dir2_data_hdr *d;
 
 		next_da_bno = da_bno + mp->m_dir_geo->fsbcount - 1;
 		if (bmap_next_offset(ip, &next_da_bno)) {
@@ -2404,9 +2403,7 @@ longform_dir2_entry_check(
 		}
 
 		/* check v5 metadata */
-		d = bp->b_addr;
-		if (be32_to_cpu(d->magic) == XFS_DIR3_BLOCK_MAGIC ||
-		    be32_to_cpu(d->magic) == XFS_DIR3_DATA_MAGIC) {
+		if (xfs_has_crc(mp)) {
 			error = check_dir3_header(mp, bp, ino);
 			if (error) {
 				fixit++;
Signature · 1 line
2.48.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · On Fri, Mar 21, 2025 at 09:28:49AM -0500, [email protected] wrote:

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

In certain cases, if a block is so messed up that crc, uuid and magic number are all bad, we need to not only detect in phase3 but fix it properly in phase6. In the current code, the mechanism doesn't work in that it only pays attention to one of the parameters.

Quoted 7 lines

Note: in this case, the nlink inode link count drops to 1, but re-running xfs_repair fixes it back to 2. This is a side effect that should probably be handled in update_inode_nlinks() with separate patch. Regardless, running xfs_repair twice fixes the issue. Also, this patch fixes the issue with v5, but not v4 xfs.

That makes sense.

Bonus question: does longform_dir2_check_leaf need a similar correction for:

	if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
		error = check_da3_header(mp, bp, ip->i_ino);
		if (error) {
			libxfs_buf_relse(bp);
			return error;
		}
	}

--D

Quoted 34 lines

v2: remove superfluous wantmagic logic

---
 repair/phase6.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
repair/phase6.c +1 -4
index 4064a84b2450..9cffbb1f4510 100644
@@ -2364,7 +2364,6 @@ longform_dir2_entry_check(
 	     da_bno = (xfs_dablk_t)next_da_bno) {
 		const struct xfs_buf_ops *ops;
 		int			 error;
-		struct xfs_dir2_data_hdr *d;
 
 		next_da_bno = da_bno + mp->m_dir_geo->fsbcount - 1;
 		if (bmap_next_offset(ip, &next_da_bno)) {
@@ -2404,9 +2403,7 @@ longform_dir2_entry_check(
 		}
 
 		/* check v5 metadata */
-		d = bp->b_addr;
-		if (be32_to_cpu(d->magic) == XFS_DIR3_BLOCK_MAGIC ||
-		    be32_to_cpu(d->magic) == XFS_DIR3_DATA_MAGIC) {
+		if (xfs_has_crc(mp)) {
 			error = check_dir3_header(mp, bp, ino);
 			if (error) {
 				fixit++;
Signature · 1 line
2.48.1
#2 lore raw
Quoted 22 lines · Darrick J. Wong
Quoted 7 lines · On Fri, Mar 21, 2025 at 09:28:49AM -0500, [email protected] wrote:

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

In certain cases, if a block is so messed up that crc, uuid and magic number are all bad, we need to not only detect in phase3 but fix it properly in phase6. In the current code, the mechanism doesn't work in that it only pays attention to one of the parameters.

Quoted 7 lines

Note: in this case, the nlink inode link count drops to 1, but re-running xfs_repair fixes it back to 2. This is a side effect that should probably be handled in update_inode_nlinks() with separate patch. Regardless, running xfs_repair twice fixes the issue. Also, this patch fixes the issue with v5, but not v4 xfs.

That makes sense.

Bonus question: does longform_dir2_check_leaf need a similar correction for:

Quoted 9 lines
	if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
		error = check_da3_header(mp, bp, ip->i_ino);
		if (error) {
			libxfs_buf_relse(bp);
			return error;
		}
	}

--D

I believe so, yes. Basing the v4/v5 decisions on an assumed correct magic number is not so good. I'll fix it in a new version or separate patch if preferred.

Thanks-
Bill

Quoted 35 lines
Quoted 34 lines

v2: remove superfluous wantmagic logic

---
 repair/phase6.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
repair/phase6.c +1 -4
index 4064a84b2450..9cffbb1f4510 100644
@@ -2364,7 +2364,6 @@ longform_dir2_entry_check(
 	     da_bno = (xfs_dablk_t)next_da_bno) {
 		const struct xfs_buf_ops *ops;
 		int			 error;
-		struct xfs_dir2_data_hdr *d;
 
 		next_da_bno = da_bno + mp->m_dir_geo->fsbcount - 1;
 		if (bmap_next_offset(ip, &next_da_bno)) {
@@ -2404,9 +2403,7 @@ longform_dir2_entry_check(
 		}
 
 		/* check v5 metadata */
-		d = bp->b_addr;
-		if (be32_to_cpu(d->magic) == XFS_DIR3_BLOCK_MAGIC ||
-		    be32_to_cpu(d->magic) == XFS_DIR3_DATA_MAGIC) {
+		if (xfs_has_crc(mp)) {
 			error = check_dir3_header(mp, bp, ino);
 			if (error) {
 				fixit++;
Signature · 1 line
2.48.1
#3 lore raw
Quoted 33 lines · Bill O'Donnell
Quoted 22 lines · Darrick J. Wong
Quoted 7 lines · On Fri, Mar 21, 2025 at 09:28:49AM -0500, [email protected] wrote:

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

In certain cases, if a block is so messed up that crc, uuid and magic number are all bad, we need to not only detect in phase3 but fix it properly in phase6. In the current code, the mechanism doesn't work in that it only pays attention to one of the parameters.

Quoted 7 lines

Note: in this case, the nlink inode link count drops to 1, but re-running xfs_repair fixes it back to 2. This is a side effect that should probably be handled in update_inode_nlinks() with separate patch. Regardless, running xfs_repair twice fixes the issue. Also, this patch fixes the issue with v5, but not v4 xfs.

That makes sense.

Bonus question: does longform_dir2_check_leaf need a similar correction for:

Quoted 9 lines
	if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
		error = check_da3_header(mp, bp, ip->i_ino);
		if (error) {
			libxfs_buf_relse(bp);
			return error;
		}
	}

--D

Quoted 3 lines

I believe so, yes. Basing the v4/v5 decisions on an assumed correct magic number is not so good. I'll fix it in a new version or separate patch if preferred.

It's up to you, but since this fix has already earned its review, how about a separate patch? :)

--D

Quoted 40 lines

Thanks-
Bill

Quoted 35 lines
Quoted 34 lines

v2: remove superfluous wantmagic logic

---
 repair/phase6.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
repair/phase6.c +1 -4
index 4064a84b2450..9cffbb1f4510 100644
@@ -2364,7 +2364,6 @@ longform_dir2_entry_check(
 	     da_bno = (xfs_dablk_t)next_da_bno) {
 		const struct xfs_buf_ops *ops;
 		int			 error;
-		struct xfs_dir2_data_hdr *d;
 
 		next_da_bno = da_bno + mp->m_dir_geo->fsbcount - 1;
 		if (bmap_next_offset(ip, &next_da_bno)) {
@@ -2404,9 +2403,7 @@ longform_dir2_entry_check(
 		}
 
 		/* check v5 metadata */
-		d = bp->b_addr;
-		if (be32_to_cpu(d->magic) == XFS_DIR3_BLOCK_MAGIC ||
-		    be32_to_cpu(d->magic) == XFS_DIR3_DATA_MAGIC) {
+		if (xfs_has_crc(mp)) {
 			error = check_dir3_header(mp, bp, ino);
 			if (error) {
 				fixit++;
Signature · 1 line
2.48.1
#4 lore raw
Quoted 34 lines · Darrick J. Wong
Quoted 33 lines · Bill O'Donnell
Quoted 31 lines · Darrick J. Wong
Quoted 7 lines · On Fri, Mar 21, 2025 at 09:28:49AM -0500, [email protected] wrote:

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

In certain cases, if a block is so messed up that crc, uuid and magic number are all bad, we need to not only detect in phase3 but fix it properly in phase6. In the current code, the mechanism doesn't work in that it only pays attention to one of the parameters.

Quoted 7 lines

Note: in this case, the nlink inode link count drops to 1, but re-running xfs_repair fixes it back to 2. This is a side effect that should probably be handled in update_inode_nlinks() with separate patch. Regardless, running xfs_repair twice fixes the issue. Also, this patch fixes the issue with v5, but not v4 xfs.

That makes sense.

Bonus question: does longform_dir2_check_leaf need a similar correction for:

	if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
		error = check_da3_header(mp, bp, ip->i_ino);
		if (error) {
			libxfs_buf_relse(bp);
			return error;
		}
	}

--D

Quoted 6 lines
Quoted 3 lines

I believe so, yes. Basing the v4/v5 decisions on an assumed correct magic number is not so good. I'll fix it in a new version or separate patch if preferred.

It's up to you, but since this fix has already earned its review, how about a separate patch? :)

That's what I'll do. Thanks again for the review :) -Bill

Quoted 44 lines

--D

Quoted 40 lines

Thanks-
Bill

Quoted 35 lines
Quoted 34 lines

v2: remove superfluous wantmagic logic

---
 repair/phase6.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
repair/phase6.c +1 -4
index 4064a84b2450..9cffbb1f4510 100644
@@ -2364,7 +2364,6 @@ longform_dir2_entry_check(
 	     da_bno = (xfs_dablk_t)next_da_bno) {
 		const struct xfs_buf_ops *ops;
 		int			 error;
-		struct xfs_dir2_data_hdr *d;
 
 		next_da_bno = da_bno + mp->m_dir_geo->fsbcount - 1;
 		if (bmap_next_offset(ip, &next_da_bno)) {
@@ -2404,9 +2403,7 @@ longform_dir2_entry_check(
 		}
 
 		/* check v5 metadata */
-		d = bp->b_addr;
-		if (be32_to_cpu(d->magic) == XFS_DIR3_BLOCK_MAGIC ||
-		    be32_to_cpu(d->magic) == XFS_DIR3_DATA_MAGIC) {
+		if (xfs_has_crc(mp)) {
 			error = check_dir3_header(mp, bp, ino);
 			if (error) {
 				fixit++;
Signature · 1 line
2.48.1
#5 lore raw
In reply to [email protected]
Quoted 7 lines · On 3/21/25 9:28 AM, [email protected] wrote:

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

In certain cases, if a block is so messed up that crc, uuid and magic number are all bad, we need to not only detect in phase3 but fix it properly in phase6. In the current code, the mechanism doesn't work in that it only pays attention to one of the parameters.

Quoted 5 lines

Note: in this case, the nlink inode link count drops to 1, but re-running xfs_repair fixes it back to 2. This is a side effect that should probably be handled in update_inode_nlinks() with separate patch. Regardless, running xfs_repair twice fixes the issue. Also, this patch fixes the issue with v5, but not v4 xfs.

Nitpick: IIRC V4 filesystems do not have UUIDs in metadata blocks, so I think this problem is unique to corrupted V5 filesystems.

-Eric

#6 lore raw
Quoted 8 lines · Eric Sandeen
Quoted 7 lines · On 3/21/25 9:28 AM, [email protected] wrote:

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

In certain cases, if a block is so messed up that crc, uuid and magic number are all bad, we need to not only detect in phase3 but fix it properly in phase6. In the current code, the mechanism doesn't work in that it only pays attention to one of the parameters.

Quoted 8 lines
Quoted 5 lines

Note: in this case, the nlink inode link count drops to 1, but re-running xfs_repair fixes it back to 2. This is a side effect that should probably be handled in update_inode_nlinks() with separate patch. Regardless, running xfs_repair twice fixes the issue. Also, this patch fixes the issue with v5, but not v4 xfs.

Nitpick: IIRC V4 filesystems do not have UUIDs in metadata blocks, so I think this problem is unique to corrupted V5 filesystems.

Right. I'll send a patch version 3, just to clarify the message.

Thanks!
-Bill

Quoted 3 lines

-Eric