Skip to contentSkip to conversation

Preparatory to add the csum check in the scan context

PATCH Superseded Anand Jain 2 participants 16 messages Mar 20, 2018 – Mar 26, 2018 Btrfs file system

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

cover Preparatory to add the csum check in the scan context

Anand Jain (3):
btrfs: cleanup btrfs_check_super_csum() to check the csum_type to the
type
btrfs: return required error from btrfs_check_super_csum
btrfs: refactor btrfs_check_super_csum() for scan

 fs/btrfs/disk-io.c | 50 ++++++++++++++++++++++++--------------------------
 fs/btrfs/disk-io.h |  1 +
 2 files changed, 25 insertions(+), 26 deletions(-)
Signature · 1 line
2.15.0
author
#1 lore raw

PATCH 1/3 btrfs: cleanup btrfs_check_super_csum() to check the csum_type to the type

%csum_type is being checked to check the number of csum types we support, which is 1 as of now. Instead just check if the type matches with the only type we support, that is BTRFS_CSUM_TYPE_CRC32. And further adds cleanup.

---
 fs/btrfs/disk-io.c | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)
fs/btrfs/disk-io.c +19 -22
index 1657d6aa4fa6..582ed6af3c50 100644
@@ -399,32 +399,29 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	struct btrfs_super_block *disk_sb =
 		(struct btrfs_super_block *)raw_disk_sb;
 	u16 csum_type = btrfs_super_csum_type(disk_sb);
-	int ret = 0;
-
-	if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
-		u32 crc = ~(u32)0;
-		char result[sizeof(crc)];
-
-		/*
-		 * The super_block structure does not span the whole
-		 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
-		 * is filled with zeros and is included in the checksum.
-		 */
-		crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
-				crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
-		btrfs_csum_final(crc, result);
-
-		if (memcmp(raw_disk_sb, result, sizeof(result)))
-			ret = 1;
-	}
+	u32 crc = ~(u32)0;
+	char result[sizeof(crc)];
 
-	if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
+	/* We support csum type crc32 only as of now */
+	if (csum_type != BTRFS_CSUM_TYPE_CRC32) {
 		btrfs_err(fs_info, "unsupported checksum algorithm %u",
-				csum_type);
-		ret = 1;
+			  csum_type);
+		return 1;
 	}
 
-	return ret;
+	/*
+	 * The super_block structure does not span the whole
+	 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
+	 * is filled with zeros and is included in the checksum.
+	 */
+	crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
+			      crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+	btrfs_csum_final(crc, result);
+
+	if (memcmp(raw_disk_sb, result, sizeof(result)))
+		return 1;
+
+	return 0;
 }
 
 /*
Signature · 1 line
2.15.0
#2 lore raw

Re: [PATCH 1/3] btrfs: cleanup btrfs_check_super_csum() to check the csum_type to the type

Quoted 37 lines · Anand Jain

%csum_type is being checked to check the number of csum types we support, which is 1 as of now. Instead just check if the type matches with the only type we support, that is BTRFS_CSUM_TYPE_CRC32. And further adds cleanup.

---
 fs/btrfs/disk-io.c | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)
fs/btrfs/disk-io.c +1 -18
index 1657d6aa4fa6..582ed6af3c50 100644
@@ -399,32 +399,29 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	struct btrfs_super_block *disk_sb =
 		(struct btrfs_super_block *)raw_disk_sb;
 	u16 csum_type = btrfs_super_csum_type(disk_sb);
-	int ret = 0;
-
-	if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
-		u32 crc = ~(u32)0;
-		char result[sizeof(crc)];
-
-		/*
-		 * The super_block structure does not span the whole
-		 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
-		 * is filled with zeros and is included in the checksum.
-		 */
-		crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
-				crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
-		btrfs_csum_final(crc, result);
-
-		if (memcmp(raw_disk_sb, result, sizeof(result)))
-			ret = 1;
-	}
+	u32 crc = ~(u32)0;

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · line 409

Quoted 10 lines
fs/btrfs/disk-io.c +5 -3
+	char result[sizeof(crc)];
 
-	if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
+	/* We support csum type crc32 only as of now */
+	if (csum_type != BTRFS_CSUM_TYPE_CRC32) {
 		btrfs_err(fs_info, "unsupported checksum algorithm %u",
-				csum_type);
-		ret = 1;
+			  csum_type);
+		return 1;

I think it would make sense to return EINVAL here. Then the sole caller of this function (open_ctree) can use the code a bit more idiomatically:

ret = btrfs_check_super_csum()

if (ret < 0) {
handle failure
}

Right now we return 1 on both when the function fails due to invalid CRC algorithm and if the checksum doesn't match. And we are going to print 2
things: unsupported checksum algorithm and the
btrfs_err(fs_info, "superblock checksum mismatch"); from open_ctree.

Let's implemented proper error returning strategy for this function.

Quoted 4 lines
fs/btrfs/disk-io.c +1 -1
 	}
 
-	return ret;
+	/*

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · line 422

Quoted 10 lines
fs/btrfs/disk-io.c +10 -0
+	 * The super_block structure does not span the whole
+	 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
+	 * is filled with zeros and is included in the checksum.
+	 */
+	crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
+			      crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+	btrfs_csum_final(crc, result);
+
+	if (memcmp(raw_disk_sb, result, sizeof(result)))
+		return 1;

Perhaps return EUCLEAN i.e something is corrupted hence the checksum didn't pan out as we expected.

Quoted 6 lines
fs/btrfs/disk-io.c +2 -0
+
+	return 0;
 }
 
 /*
#3 lore raw

Re: [PATCH 1/3] btrfs: cleanup btrfs_check_super_csum() to check the csum_type to the type

Quoted 60 lines · Nikolay Borisov
Quoted 37 lines · Anand Jain

%csum_type is being checked to check the number of csum types we support, which is 1 as of now. Instead just check if the type matches with the only type we support, that is BTRFS_CSUM_TYPE_CRC32. And further adds cleanup.

---
 fs/btrfs/disk-io.c | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)
fs/btrfs/disk-io.c +1 -18
index 1657d6aa4fa6..582ed6af3c50 100644
@@ -399,32 +399,29 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	struct btrfs_super_block *disk_sb =
 		(struct btrfs_super_block *)raw_disk_sb;
 	u16 csum_type = btrfs_super_csum_type(disk_sb);
-	int ret = 0;
-
-	if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
-		u32 crc = ~(u32)0;
-		char result[sizeof(crc)];
-
-		/*
-		 * The super_block structure does not span the whole
-		 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
-		 * is filled with zeros and is included in the checksum.
-		 */
-		crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
-				crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
-		btrfs_csum_final(crc, result);
-
-		if (memcmp(raw_disk_sb, result, sizeof(result)))
-			ret = 1;
-	}
+	u32 crc = ~(u32)0;

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · line 409

Quoted 10 lines
fs/btrfs/disk-io.c +5 -3
+	char result[sizeof(crc)];
 
-	if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
+	/* We support csum type crc32 only as of now */
+	if (csum_type != BTRFS_CSUM_TYPE_CRC32) {
 		btrfs_err(fs_info, "unsupported checksum algorithm %u",
-				csum_type);
-		ret = 1;
+			  csum_type);
+		return 1;

I think it would make sense to return EINVAL here. Then the sole caller of this function (open_ctree) can use the code a bit more idiomatically:

ret = btrfs_check_super_csum()

if (ret < 0) {
handle failure
}

Quoted 7 lines

Right now we return 1 on both when the function fails due to invalid CRC algorithm and if the checksum doesn't match. And we are going to print 2
things: unsupported checksum algorithm and the
btrfs_err(fs_info, "superblock checksum mismatch"); from open_ctree.

Let's implemented proper error returning strategy for this function.

Disregard, you have already implemented something like that in 2/3. I will comment there how it can be improved.

Quoted 31 lines
Quoted 6 lines
fs/btrfs/disk-io.c +1 -1
 	}
 
-	return ret;
+	/*

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · line 422

Quoted 10 lines
fs/btrfs/disk-io.c +10 -0
+	 * The super_block structure does not span the whole
+	 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
+	 * is filled with zeros and is included in the checksum.
+	 */
+	crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
+			      crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+	btrfs_csum_final(crc, result);
+
+	if (memcmp(raw_disk_sb, result, sizeof(result)))
+		return 1;

Perhaps return EUCLEAN i.e something is corrupted hence the checksum didn't pan out as we expected.

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · line 427

Quoted 6 lines
fs/btrfs/disk-io.c +2 -0
+
+	return 0;
 }
 
 /*

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

author
#4 lore raw

PATCH 2/3 btrfs: return required error from btrfs_check_super_csum

In reply to Anand Jain

Return the required -EINVAL from the function btrfs_check_super_csum() and move the error logging into the btrfs_check_super_csum() itself.

---
 fs/btrfs/disk-io.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +7 -6
index 582ed6af3c50..aafd836af61d 100644
@@ -406,7 +406,7 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	if (csum_type != BTRFS_CSUM_TYPE_CRC32) {
 		btrfs_err(fs_info, "unsupported checksum algorithm %u",
 			  csum_type);
-		return 1;
+		return -EINVAL;
 	}
 
 	/*
@@ -418,8 +418,10 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 			      crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
 	btrfs_csum_final(crc, result);
 
-	if (memcmp(raw_disk_sb, result, sizeof(result)))
-		return 1;
+	if (memcmp(raw_disk_sb, result, sizeof(result))) {
+		btrfs_err(fs_info, "superblock checksum mismatch");
+		return -EINVAL;
+	}
 
 	return 0;
 }
@@ -2571,9 +2573,8 @@ int open_ctree(struct super_block *sb,
 	 * We want to check superblock checksum, the type is stored inside.
 	 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
 	 */
-	if (btrfs_check_super_csum(fs_info, bh->b_data)) {
-		btrfs_err(fs_info, "superblock checksum mismatch");
-		err = -EINVAL;
+	err = btrfs_check_super_csum(fs_info, bh->b_data);
+	if (err) {
 		brelse(bh);
 		goto fail_alloc;
 	}
Signature · 1 line
2.15.0
#5 lore raw

Re: [PATCH 2/3] btrfs: return required error from btrfs_check_super_csum

Quoted 22 lines · Anand Jain

Return the required -EINVAL from the function btrfs_check_super_csum() and move the error logging into the btrfs_check_super_csum() itself.

---
 fs/btrfs/disk-io.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +1 -1
index 582ed6af3c50..aafd836af61d 100644
@@ -406,7 +406,7 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	if (csum_type != BTRFS_CSUM_TYPE_CRC32) {
 		btrfs_err(fs_info, "unsupported checksum algorithm %u",
 			  csum_type);
-		return 1;
+		return -EINVAL;
 	}
 
 	/*

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · line 423

Quoted 8 lines
fs/btrfs/disk-io.c +3 -2
@@ -418,8 +418,10 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 			      crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
 	btrfs_csum_final(crc, result);
 
-	if (memcmp(raw_disk_sb, result, sizeof(result)))
-		return 1;
+	if (memcmp(raw_disk_sb, result, sizeof(result))) {
+		btrfs_err(fs_info, "superblock checksum mismatch");
+		return -EINVAL;

Don't we want EUCLEAN here, since an error in the checksum indicates corruption?

Quoted 17 lines
fs/btrfs/disk-io.c +3 -3
+	}
 
 	return 0;
 }
@@ -2571,9 +2573,8 @@ int open_ctree(struct super_block *sb,
 	 * We want to check superblock checksum, the type is stored inside.
 	 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
 	 */
-	if (btrfs_check_super_csum(fs_info, bh->b_data)) {
-		btrfs_err(fs_info, "superblock checksum mismatch");
-		err = -EINVAL;
+	err = btrfs_check_super_csum(fs_info, bh->b_data);
+	if (err) {
 		brelse(bh);
 		goto fail_alloc;
 	}
author
#6 lore raw

Re: [PATCH 2/3] btrfs: return required error from btrfs_check_super_csum

Quoted 7 lines
Quoted 7 lines
fs/btrfs/disk-io.c +0 -0
@@ -418,8 +418,10 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
  			      crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  	btrfs_csum_final(crc, result);
 
Quoted 8 lines

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · line 423

Quoted 5 lines
fs/btrfs/disk-io.c +3 -2
-	if (memcmp(raw_disk_sb, result, sizeof(result)))
-		return 1;
+	if (memcmp(raw_disk_sb, result, sizeof(result))) {
+		btrfs_err(fs_info, "superblock checksum mismatch");
+		return -EINVAL;

Don't we want EUCLEAN here, since an error in the checksum indicates corruption?

yeah, I struggled with it before.
Strangely there isn't an error code for the csum error when csum being widely used in the computer systems.

Our closest are:

#define EFAULT 14 /* Bad address */
#define EUCLEAN 135 /* Structure needs cleaning */

Will use the suggested EUCLEAN.

Thanks, Anand

author
#7 lore raw

Re: [PATCH 2/3] btrfs: return required error from btrfs_check_super_csum

In reply to Nikolay Borisov
Quoted 6 lines
Quoted 6 lines
fs/btrfs/disk-io.c +0 -0
@@ -418,8 +418,10 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
  			      crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  	btrfs_csum_final(crc, result);
 
Quoted 8 lines

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, · line 423

Quoted 5 lines
fs/btrfs/disk-io.c +3 -2
-	if (memcmp(raw_disk_sb, result, sizeof(result)))
-		return 1;
+	if (memcmp(raw_disk_sb, result, sizeof(result))) {
+		btrfs_err(fs_info, "superblock checksum mismatch");
+		return -EINVAL;

Don't we want EUCLEAN here, since an error in the checksum indicates corruption?

With the v2 patch sentout here is the error str that is seen on the cli.

mount: mount /dev/sdb on /btrfs failed: Structure needs cleaning

I am ok. However would have much better if it says checksum error.

Do you/anyone suggest I should update the errno.h?

Thanks, Anand

author
#8 lore raw

Re: [PATCH 2/3] btrfs: return required error from btrfs_check_super_csum

Quoted 15 lines · Anand Jain
Quoted 14 lines

fs/btrfs/disk-io.c static int btrfs_check_super_csum(struct · line 423

Quoted 11 lines
fs/btrfs/disk-io.c +3 -2
@@ -418,8 +418,10 @@ static int btrfs_check_super_csum(struct 
 btrfs_fs_info *fs_info,
                     crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
       btrfs_csum_final(crc, result);
-    if (memcmp(raw_disk_sb, result, sizeof(result)))
-        return 1;
+    if (memcmp(raw_disk_sb, result, sizeof(result))) {
+        btrfs_err(fs_info, "superblock checksum mismatch");
+        return -EINVAL;

Don't we want EUCLEAN here, since an error in the checksum indicates corruption?

Quoted 3 lines

 With the v2 patch sentout here is the error str that is seen on the cli.

  mount: mount /dev/sdb on /btrfs failed: Structure needs cleaning

And since we return -EINVAL for the error csum_type not found we get..

mount: wrong fs type, bad option, bad superblock on /dev/sdb,
missing codepage or helper program, or other error

In some cases useful info is found in syslog - try dmesg | tail or so.

Looks like mount syscall interprets -EINVAL error.

Quoted 1 line

I am ok. However would have much better if it says checksum error.

Also we may need an error code to say incompatible disk format/version.

Quoted 1 line

Do you/anyone suggest I should update the errno.h?

Thanks, Anand

Quoted 6 lines

Thanks, Anand

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#9 lore raw

PATCH 3/3 btrfs: refactor btrfs_check_super_csum() for scan

In reply to Anand Jain

In preparation to use the function btrfs_check_super_csum() for the device scan context, make it nonstatic and pass the struct block_device instead of the struct fs_info.

---
 fs/btrfs/disk-io.c | 12 ++++++------
 fs/btrfs/disk-io.h |  1 +
 2 files changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +6 -6
index aafd836af61d..d2ace2dca9de 100644
@@ -393,8 +393,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
  * Return 0 if the superblock checksum type matches the checksum value of that
  * algorithm. Pass the raw disk superblock data.
  */
-static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
-				  char *raw_disk_sb)
+int btrfs_check_super_csum(struct block_device *bdev, char *raw_disk_sb)
 {
 	struct btrfs_super_block *disk_sb =
 		(struct btrfs_super_block *)raw_disk_sb;
@@ -404,8 +403,8 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 
 	/* We support csum type crc32 only as of now */
 	if (csum_type != BTRFS_CSUM_TYPE_CRC32) {
-		btrfs_err(fs_info, "unsupported checksum algorithm %u",
-			  csum_type);
+		pr_err("BTRFS error (device %pg): unsupported checksum algorithm %u",
+			bdev, csum_type);
 		return -EINVAL;
 	}
 
@@ -419,7 +418,8 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	btrfs_csum_final(crc, result);
 
 	if (memcmp(raw_disk_sb, result, sizeof(result))) {
-		btrfs_err(fs_info, "superblock checksum mismatch");
+		pr_err("BTRFS error (device %pg): superblock checksum mismatch",
+			bdev);
 		return -EINVAL;
 	}
 
@@ -2573,7 +2573,7 @@ int open_ctree(struct super_block *sb,
 	 * We want to check superblock checksum, the type is stored inside.
 	 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
 	 */
-	err = btrfs_check_super_csum(fs_info, bh->b_data);
+	err = btrfs_check_super_csum(fs_devices->latest_bdev, bh->b_data);
 	if (err) {
 		brelse(bh);
 		goto fail_alloc;
fs/btrfs/disk-io.h +1 -0
index 70a88d61b547..1427fa1181d4 100644
@@ -69,6 +69,7 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors);
 struct buffer_head *btrfs_read_dev_super(struct block_device *bdev);
 int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
 			struct buffer_head **bh_ret);
+int btrfs_check_super_csum(struct block_device *bdev, char *raw_disk_sb);
 int btrfs_commit_super(struct btrfs_fs_info *fs_info);
 struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
 				      struct btrfs_key *location);
Signature · 1 line
2.15.0
#10 lore raw

Re: [PATCH 3/3] btrfs: refactor btrfs_check_super_csum() for scan

Quoted 15 lines · Anand Jain

In preparation to use the function btrfs_check_super_csum() for the device scan context, make it nonstatic and pass the struct block_device instead of the struct fs_info.

---
 fs/btrfs/disk-io.c | 12 ++++++------
 fs/btrfs/disk-io.h |  1 +
 2 files changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +0 -0
index aafd836af61d..d2ace2dca9de 100644

fs/btrfs/disk-io.c static int verify_parent_transid(struct extent_io_tree *io_tree, · line 397

Quoted 7 lines
fs/btrfs/disk-io.c +1 -2
@@ -393,8 +393,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
  * Return 0 if the superblock checksum type matches the checksum value of that
  * algorithm. Pass the raw disk superblock data.
  */
-static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
-				  char *raw_disk_sb)
+int btrfs_check_super_csum(struct block_device *bdev, char *raw_disk_sb)
 {

Since this has become a public function and you've changed the fs_info parameter to taking a bdev, which is not used for anything else than printing the error I think it's appropriate to document which block device should be passed to this function.

Also passing the block device only for printing purposes seems a bit odd.

Quoted 45 lines
fs/btrfs/disk-io.c +5 -4
 	struct btrfs_super_block *disk_sb =
 		(struct btrfs_super_block *)raw_disk_sb;
@@ -404,8 +403,8 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 
 	/* We support csum type crc32 only as of now */
 	if (csum_type != BTRFS_CSUM_TYPE_CRC32) {
-		btrfs_err(fs_info, "unsupported checksum algorithm %u",
-			  csum_type);
+		pr_err("BTRFS error (device %pg): unsupported checksum algorithm %u",
+			bdev, csum_type);
 		return -EINVAL;
 	}
 
@@ -419,7 +418,8 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	btrfs_csum_final(crc, result);
 
 	if (memcmp(raw_disk_sb, result, sizeof(result))) {
-		btrfs_err(fs_info, "superblock checksum mismatch");
+		pr_err("BTRFS error (device %pg): superblock checksum mismatch",
+			bdev);
 		return -EINVAL;
 	}
 
@@ -2573,7 +2573,7 @@ int open_ctree(struct super_block *sb,
 	 * We want to check superblock checksum, the type is stored inside.
 	 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
 	 */
-	err = btrfs_check_super_csum(fs_info, bh->b_data);
+	err = btrfs_check_super_csum(fs_devices->latest_bdev, bh->b_data);
 	if (err) {
 		brelse(bh);
 		goto fail_alloc;
fs/btrfs/disk-io.h +1 -0
index 70a88d61b547..1427fa1181d4 100644
@@ -69,6 +69,7 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors);
 struct buffer_head *btrfs_read_dev_super(struct block_device *bdev);
 int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
 			struct buffer_head **bh_ret);
+int btrfs_check_super_csum(struct block_device *bdev, char *raw_disk_sb);
 int btrfs_commit_super(struct btrfs_fs_info *fs_info);
 struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
 				      struct btrfs_key *location);
author
#11 lore raw

Re: [PATCH 3/3] btrfs: refactor btrfs_check_super_csum() for scan

Quoted 31 lines · Nikolay Borisov
Quoted 15 lines · Anand Jain

In preparation to use the function btrfs_check_super_csum() for the device scan context, make it nonstatic and pass the struct block_device instead of the struct fs_info.

---
  fs/btrfs/disk-io.c | 12 ++++++------
  fs/btrfs/disk-io.h |  1 +
  2 files changed, 7 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +0 -0
index aafd836af61d..d2ace2dca9de 100644

fs/btrfs/disk-io.c static int verify_parent_transid(struct extent_io_tree *io_tree, · line 397

Quoted 7 lines
fs/btrfs/disk-io.c +1 -2
@@ -393,8 +393,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
   * Return 0 if the superblock checksum type matches the checksum value of that
   * algorithm. Pass the raw disk superblock data.
   */
-static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
-				  char *raw_disk_sb)
+int btrfs_check_super_csum(struct block_device *bdev, char *raw_disk_sb)
  {

Since this has become a public function and you've changed the fs_info parameter to taking a bdev, which is not used for anything else than printing the error I think it's appropriate to document which block device should be passed to this function.

Quoted 1 line

Also passing the block device only for printing purposes seems a bit odd.

Its the device on which we have read the superblock. I find it odd too. Will do that pr_err on the parent function. Will add comments to the public function.

Thanks, Anand

author
#12 lore raw

v3 cover Preparatory to add the csum check in the scan context

v2->v3:
1/2: Keep the check %(csum_type >= ARRAY_SIZE(btrfs_csum_sizes))
because it is inline to support future expansion, further btrfs-progs is already copied it.
2/2: Drop pr_err for default error which won't be possible.

v1->v2:
Merge 2/3 and 3/3
Use -EUCLEAN for csum mismatch
Move the error logging to the parent function
Drop the struct btrfs_fsinfo as the argument for
btrfs_check_super_csum()
Do not make btrfs_check_super_csum() nonstatic here, it will be part
of the patchset which will use the csum in the scan context

Anand Jain (2):
btrfs: cleanup btrfs_check_super_csum() to check the csum_type to the
type
btrfs: return required error from btrfs_check_super_csum

 fs/btrfs/disk-io.c | 53 ++++++++++++++++++++++++++---------------------------
 1 file changed, 26 insertions(+), 27 deletions(-)
Signature · 1 line
2.15.0
author
#13 lore raw

PATCH v3 1/2 btrfs: cleanup btrfs_check_super_csum() to check the csum_type to the type

%csum_type is being checked to check the number of csum types we support, which is 1 as of now. Instead just check if the type matches with the only type we support, that is BTRFS_CSUM_TYPE_CRC32. And further adds cleanup.

---
 fs/btrfs/disk-io.c | 38 +++++++++++++++++---------------------
 1 file changed, 17 insertions(+), 21 deletions(-)
fs/btrfs/disk-io.c +17 -21
index 1657d6aa4fa6..b9b435579527 100644
@@ -399,32 +399,28 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	struct btrfs_super_block *disk_sb =
 		(struct btrfs_super_block *)raw_disk_sb;
 	u16 csum_type = btrfs_super_csum_type(disk_sb);
-	int ret = 0;
-
-	if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
-		u32 crc = ~(u32)0;
-		char result[sizeof(crc)];
-
-		/*
-		 * The super_block structure does not span the whole
-		 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
-		 * is filled with zeros and is included in the checksum.
-		 */
-		crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
-				crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
-		btrfs_csum_final(crc, result);
-
-		if (memcmp(raw_disk_sb, result, sizeof(result)))
-			ret = 1;
-	}
+	u32 crc = ~(u32)0;
+	char result[sizeof(crc)];
 
 	if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
 		btrfs_err(fs_info, "unsupported checksum algorithm %u",
-				csum_type);
-		ret = 1;
+			  csum_type);
+		return 1;
 	}
 
-	return ret;
+	/*
+	 * The super_block structure does not span the whole
+	 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
+	 * is filled with zeros and is included in the checksum.
+	 */
+	crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
+			      crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+	btrfs_csum_final(crc, result);
+
+	if (memcmp(raw_disk_sb, result, sizeof(result)))
+		return 1;
+
+	return 0;
 }
 
 /*
Signature · 1 line
2.15.0
author
#14 lore raw

PATCH v3 2/2 btrfs: return required error from btrfs_check_super_csum

In reply to Anand Jain

Return the required -EINVAL and -EUCLEAN from the function btrfs_check_super_csum(). And more the error log into the parent function.

---
 fs/btrfs/disk-io.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
fs/btrfs/disk-io.c +14 -11
index b9b435579527..062b3e0c7fd1 100644
@@ -392,9 +392,10 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
 /*
  * Return 0 if the superblock checksum type matches the checksum value of that
  * algorithm. Pass the raw disk superblock data.
+ * Otherwise: -EINVAL  if csum type is not found
+ * 	      -EUCLEAN if csum does not match
  */
-static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
-				  char *raw_disk_sb)
+static int btrfs_check_super_csum(char *raw_disk_sb)
 {
 	struct btrfs_super_block *disk_sb =
 		(struct btrfs_super_block *)raw_disk_sb;
@@ -402,11 +403,8 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	u32 crc = ~(u32)0;
 	char result[sizeof(crc)];
 
-	if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
-		btrfs_err(fs_info, "unsupported checksum algorithm %u",
-			  csum_type);
-		return 1;
-	}
+	if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes))
+		return -EINVAL;
 
 	/*
 	 * The super_block structure does not span the whole
@@ -418,7 +416,7 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 	btrfs_csum_final(crc, result);
 
 	if (memcmp(raw_disk_sb, result, sizeof(result)))
-		return 1;
+		return -EUCLEAN;
 
 	return 0;
 }
@@ -2570,9 +2568,14 @@ int open_ctree(struct super_block *sb,
 	 * We want to check superblock checksum, the type is stored inside.
 	 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
 	 */
-	if (btrfs_check_super_csum(fs_info, bh->b_data)) {
-		btrfs_err(fs_info, "superblock checksum mismatch");
-		err = -EINVAL;
+	err = btrfs_check_super_csum(bh->b_data);
+	if (err) {
+		if (err == -EINVAL)
+			pr_err("BTRFS error (device %pg): unsupported checksum algorithm",
+				fs_devices->latest_bdev);
+		else if (err == -EUCLEAN)
+			pr_err("BTRFS error (device %pg): superblock checksum mismatch",
+				fs_devices->latest_bdev);
 		brelse(bh);
 		goto fail_alloc;
 	}
Signature · 1 line
2.15.0
author
#15 lore raw
In reply to Anand Jain

Pls, ignore this bunch as I have included this with other cleanups.

Thanks, Anand

Quoted 23 lines · Anand Jain

v2->v3:
1/2: Keep the check %(csum_type >= ARRAY_SIZE(btrfs_csum_sizes))
because it is inline to support future expansion, further btrfs-progs is already copied it.
2/2: Drop pr_err for default error which won't be possible.

v1->v2:
Merge 2/3 and 3/3
Use -EUCLEAN for csum mismatch
Move the error logging to the parent function
Drop the struct btrfs_fsinfo as the argument for
btrfs_check_super_csum()
Do not make btrfs_check_super_csum() nonstatic here, it will be part
of the patchset which will use the csum in the scan context

Anand Jain (2):
btrfs: cleanup btrfs_check_super_csum() to check the csum_type to the
type
btrfs: return required error from btrfs_check_super_csum

  fs/btrfs/disk-io.c | 53 ++++++++++++++++++++++++++---------------------------
  1 file changed, 26 insertions(+), 27 deletions(-)