Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block
@ 2021-10-21  1:40 Qu Wenruo
  2021-10-21  1:40 ` [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE Qu Wenruo
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Qu Wenruo @ 2021-10-21  1:40 UTC (permalink / raw)
  To: linux-btrfs

This patchset mostly cleans up the mismatch between
BTRFS_SUPER_INFO_SIZE and sizeof(struct btrfs_super_block).

This cleanup itself is going to replace all the following snippets:

	u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
	struct btrfs_super_block *sb;

	sb = (struct btrfs_super_block *)super_block_data;

With:

	struct btrfs_super_block sb;

Also since we're here, also cache csum_type and csum_size in fs_info,
just like what we did in the kernel.

Qu Wenruo (3):
  btrfs-progs: unify sizeof(struct btrfs_super_block) and
    BTRFS_SUPER_INFO_SIZE
  btrfs-progs: remove temporary buffer for super block
  btrfs-progs: cache csum_size and csum_type in btrfs_fs_info

 btrfs-corrupt-block.c       |  6 +--
 check/main.c                |  6 +--
 check/mode-common.c         |  2 +-
 cmds/filesystem-usage.c     |  8 ++--
 cmds/inspect-dump-super.c   | 11 ++----
 cmds/rescue-chunk-recover.c | 24 ++++++-----
 cmds/rescue-super-recover.c | 11 +++---
 common/device-scan.c        | 23 ++++-------
 common/utils.c              |  8 ++--
 convert/common.c            | 79 +++++++++++++++++--------------------
 convert/main.c              | 29 ++++++--------
 image/main.c                | 14 +++----
 kernel-shared/ctree.h       |  9 +++++
 kernel-shared/disk-io.c     | 41 +++++++++----------
 kernel-shared/disk-io.h     |  3 --
 kernel-shared/file-item.c   | 15 +++----
 kernel-shared/print-tree.c  |  4 +-
 kernel-shared/volumes.c     | 14 +++----
 mkfs/common.c               |  8 ++--
 19 files changed, 141 insertions(+), 174 deletions(-)

-- 
2.33.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE
  2021-10-21  1:40 [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block Qu Wenruo
@ 2021-10-21  1:40 ` Qu Wenruo
  2021-10-21 12:40   ` David Sterba
  2021-10-21  1:40 ` [PATCH 2/3] btrfs-progs: remove temporary buffer for super block Qu Wenruo
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Qu Wenruo @ 2021-10-21  1:40 UTC (permalink / raw)
  To: linux-btrfs

Just like kernel change, pad struct btrfs_super_block to 4096 bytes.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 kernel-shared/ctree.h   | 7 +++++++
 kernel-shared/disk-io.h | 3 ---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/kernel-shared/ctree.h b/kernel-shared/ctree.h
index 563ea50b3587..6451690ce4fa 100644
--- a/kernel-shared/ctree.h
+++ b/kernel-shared/ctree.h
@@ -406,6 +406,9 @@ struct btrfs_root_backup {
 	u8 unused_8[10];
 } __attribute__ ((__packed__));
 
+#define BTRFS_SUPER_INFO_OFFSET SZ_64K
+#define BTRFS_SUPER_INFO_SIZE 4096
+
 /*
  * the super block basically lists the main trees of the FS
  * it currently lacks any block count etc etc
@@ -456,8 +459,12 @@ struct btrfs_super_block {
 	__le64 reserved[28];
 	u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
 	struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
+	/* Padded to 4096 bytes */
+	u8 padding[565];
 } __attribute__ ((__packed__));
 
+static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);
+
 /*
  * Compat flags that we support.  If any incompat flags are set other than the
  * ones specified below then we will fail to mount
diff --git a/kernel-shared/disk-io.h b/kernel-shared/disk-io.h
index e113d842c906..823e5af37e75 100644
--- a/kernel-shared/disk-io.h
+++ b/kernel-shared/disk-io.h
@@ -23,9 +23,6 @@
 #include "kernel-shared/ctree.h"
 #include "kernel-lib/sizes.h"
 
-#define BTRFS_SUPER_INFO_OFFSET SZ_64K
-#define BTRFS_SUPER_INFO_SIZE 4096
-
 #define BTRFS_SUPER_MIRROR_MAX	 3
 #define BTRFS_SUPER_MIRROR_SHIFT 12
 
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] btrfs-progs: remove temporary buffer for super block
  2021-10-21  1:40 [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block Qu Wenruo
  2021-10-21  1:40 ` [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE Qu Wenruo
@ 2021-10-21  1:40 ` Qu Wenruo
  2021-10-21  1:40 ` [PATCH 3/3] btrfs-progs: cache csum_size and csum_type in btrfs_fs_info Qu Wenruo
  2021-11-04 19:10 ` [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block David Sterba
  3 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2021-10-21  1:40 UTC (permalink / raw)
  To: linux-btrfs

There are a lot of call sites where we use the following code snippet:

	u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
	struct btrfs_super_block *sb;
	u64 ret;

	sb = (struct btrfs_super_block *)super_block_data;

The reason for this is, structure btrfs_super_block was smaller than
BTRFS_SUPER_INFO_SIZE.

Thus for anything with csum involved, we have to use a proper 4K buffer.

Since the recent unification of sizeof(struct btrfs_super_block), we no
longer needs such workaround, and can use struct btrfs_super_block
directly to do any operation.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 cmds/filesystem-usage.c     |  8 ++--
 cmds/inspect-dump-super.c   | 11 ++----
 cmds/rescue-chunk-recover.c | 20 +++++-----
 cmds/rescue-super-recover.c | 11 +++---
 common/device-scan.c        | 23 ++++-------
 common/utils.c              |  8 ++--
 convert/common.c            | 79 +++++++++++++++++--------------------
 convert/main.c              | 29 ++++++--------
 image/main.c                | 14 +++----
 kernel-shared/disk-io.c     | 35 ++++++++--------
 kernel-shared/volumes.c     | 14 +++----
 mkfs/common.c               |  8 ++--
 12 files changed, 112 insertions(+), 148 deletions(-)

diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
index 7f810cd0c5ac..1ba48fafce0d 100644
--- a/cmds/filesystem-usage.c
+++ b/cmds/filesystem-usage.c
@@ -668,8 +668,7 @@ static int cmp_device_info(const void *a, const void *b)
 
 int dev_to_fsid(const char *dev, u8 *fsid)
 {
-	struct btrfs_super_block *disk_super;
-	char buf[BTRFS_SUPER_INFO_SIZE];
+	struct btrfs_super_block disk_super;
 	int ret;
 	int fd;
 
@@ -679,13 +678,12 @@ int dev_to_fsid(const char *dev, u8 *fsid)
 		return ret;
 	}
 
-	disk_super = (struct btrfs_super_block *)buf;
-	ret = btrfs_read_dev_super(fd, disk_super,
+	ret = btrfs_read_dev_super(fd, &disk_super,
 				   BTRFS_SUPER_INFO_OFFSET, SBREAD_DEFAULT);
 	if (ret)
 		goto out;
 
-	memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
+	memcpy(fsid, disk_super.fsid, BTRFS_FSID_SIZE);
 	ret = 0;
 
 out:
diff --git a/cmds/inspect-dump-super.c b/cmds/inspect-dump-super.c
index 04e81d8c3b60..d8435624df90 100644
--- a/cmds/inspect-dump-super.c
+++ b/cmds/inspect-dump-super.c
@@ -33,13 +33,10 @@
 static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
 		int force)
 {
-	u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
-	struct btrfs_super_block *sb;
+	struct btrfs_super_block sb;
 	u64 ret;
 
-	sb = (struct btrfs_super_block *)super_block_data;
-
-	ret = sbread(fd, super_block_data, sb_bytenr);
+	ret = sbread(fd, &sb, sb_bytenr);
 	if (ret != BTRFS_SUPER_INFO_SIZE) {
 		/* check if the disk if too short for further superblock */
 		if (ret == 0 && errno == 0)
@@ -52,11 +49,11 @@ static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
 	}
 	printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
 	printf("---------------------------------------------------------\n");
-	if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
+	if (btrfs_super_magic(&sb) != BTRFS_MAGIC && !force) {
 		error("bad magic on superblock on %s at %llu",
 				filename, (unsigned long long)sb_bytenr);
 	} else {
-		btrfs_print_superblock(sb, full);
+		btrfs_print_superblock(&sb, full);
 	}
 	return 0;
 }
diff --git a/cmds/rescue-chunk-recover.c b/cmds/rescue-chunk-recover.c
index cdf0fe1864cd..35c6f66548fd 100644
--- a/cmds/rescue-chunk-recover.c
+++ b/cmds/rescue-chunk-recover.c
@@ -1506,8 +1506,7 @@ static int recover_prepare(struct recover_control *rc, const char *path)
 {
 	int ret;
 	int fd;
-	struct btrfs_super_block *sb;
-	char buf[BTRFS_SUPER_INFO_SIZE];
+	struct btrfs_super_block sb;
 	struct btrfs_fs_devices *fs_devices;
 
 	ret = 0;
@@ -1517,23 +1516,22 @@ static int recover_prepare(struct recover_control *rc, const char *path)
 		return -1;
 	}
 
-	sb = (struct btrfs_super_block*)buf;
-	ret = btrfs_read_dev_super(fd, sb, BTRFS_SUPER_INFO_OFFSET,
+	ret = btrfs_read_dev_super(fd, &sb, BTRFS_SUPER_INFO_OFFSET,
 			SBREAD_RECOVER);
 	if (ret) {
 		fprintf(stderr, "read super block error\n");
 		goto out_close_fd;
 	}
 
-	rc->sectorsize = btrfs_super_sectorsize(sb);
-	rc->nodesize = btrfs_super_nodesize(sb);
-	rc->generation = btrfs_super_generation(sb);
-	rc->chunk_root_generation = btrfs_super_chunk_root_generation(sb);
-	rc->csum_size = btrfs_super_csum_size(sb);
-	rc->csum_type = btrfs_super_csum_type(sb);
+	rc->sectorsize = btrfs_super_sectorsize(&sb);
+	rc->nodesize = btrfs_super_nodesize(&sb);
+	rc->generation = btrfs_super_generation(&sb);
+	rc->chunk_root_generation = btrfs_super_chunk_root_generation(&sb);
+	rc->csum_size = btrfs_super_csum_size(&sb);
+	rc->csum_type = btrfs_super_csum_type(&sb);
 
 	/* if seed, the result of scanning below will be partial */
-	if (btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_SEEDING) {
+	if (btrfs_super_flags(&sb) & BTRFS_SUPER_FLAG_SEEDING) {
 		fprintf(stderr, "this device is seed device\n");
 		ret = -1;
 		goto out_close_fd;
diff --git a/cmds/rescue-super-recover.c b/cmds/rescue-super-recover.c
index 1eaa87298b2e..d78f5052f076 100644
--- a/cmds/rescue-super-recover.c
+++ b/cmds/rescue-super-recover.c
@@ -114,9 +114,8 @@ static int
 read_dev_supers(char *filename, struct btrfs_recover_superblock *recover)
 {
 	int i, ret, fd;
-	u8 buf[BTRFS_SUPER_INFO_SIZE];
 	u64 max_gen, bytenr;
-	struct btrfs_super_block *sb = (struct btrfs_super_block *)buf;
+	struct btrfs_super_block sb;
 
 	/* just ignore errno that were set in btrfs_scan_fs_devices() */
 	errno = 0;
@@ -128,13 +127,13 @@ read_dev_supers(char *filename, struct btrfs_recover_superblock *recover)
 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
 		bytenr = btrfs_sb_offset(i);
 
-		ret = btrfs_read_dev_super(fd, sb, bytenr, SBREAD_DEFAULT);
+		ret = btrfs_read_dev_super(fd, &sb, bytenr, SBREAD_DEFAULT);
 		if (!ret) {
-			ret = add_superblock_record(sb, filename, bytenr,
+			ret = add_superblock_record(&sb, filename, bytenr,
 							&recover->good_supers);
 			if (ret)
 				goto out;
-			max_gen = btrfs_super_generation(sb);
+			max_gen = btrfs_super_generation(&sb);
 			if (max_gen > recover->max_generation)
 				recover->max_generation = max_gen;
 		} else if (ret != -ENOENT){
@@ -142,7 +141,7 @@ read_dev_supers(char *filename, struct btrfs_recover_superblock *recover)
 			 * Skip superblock which doesn't exist, only adds
 			 * really corrupted superblock
 			 */
-			ret = add_superblock_record(sb, filename, bytenr,
+			ret = add_superblock_record(&sb, filename, bytenr,
 						&recover->bad_supers);
 			if (ret)
 				goto out;
diff --git a/common/device-scan.c b/common/device-scan.c
index 4c54ee3b7254..39b12c0e02c4 100644
--- a/common/device-scan.c
+++ b/common/device-scan.c
@@ -269,34 +269,25 @@ int btrfs_register_all_devices(void)
 int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
 				 int super_offset)
 {
-	struct btrfs_super_block *disk_super;
-	char *buf;
+	struct btrfs_super_block disk_super;
 	int ret = 0;
 
-	buf = malloc(BTRFS_SUPER_INFO_SIZE);
-	if (!buf) {
-		ret = -ENOMEM;
-		goto out;
-	}
-	ret = sbread(fd, buf, super_offset);
+	ret = sbread(fd, &disk_super, super_offset);
 	if (ret != BTRFS_SUPER_INFO_SIZE)
-		goto brelse;
+		goto out;
 
 	ret = 0;
-	disk_super = (struct btrfs_super_block *)buf;
 	/*
 	 * Accept devices from the same filesystem, allow partially created
 	 * structures.
 	 */
-	if (btrfs_super_magic(disk_super) != BTRFS_MAGIC &&
-			btrfs_super_magic(disk_super) != BTRFS_MAGIC_TEMPORARY)
-		goto brelse;
+	if (btrfs_super_magic(&disk_super) != BTRFS_MAGIC &&
+			btrfs_super_magic(&disk_super) != BTRFS_MAGIC_TEMPORARY)
+		goto out;
 
-	if (!memcmp(disk_super->fsid, root->fs_info->super_copy->fsid,
+	if (!memcmp(disk_super.fsid, root->fs_info->super_copy->fsid,
 		    BTRFS_FSID_SIZE))
 		ret = 1;
-brelse:
-	free(buf);
 out:
 	return ret;
 }
diff --git a/common/utils.c b/common/utils.c
index aee0eedc15fc..d81e79144790 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -438,8 +438,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 	memset(fi_args, 0, sizeof(*fi_args));
 
 	if (path_is_block_device(path) == 1) {
-		struct btrfs_super_block *disk_super;
-		char buf[BTRFS_SUPER_INFO_SIZE];
+		struct btrfs_super_block disk_super;
 
 		/* Ensure it's mounted, then set path to the mountpoint */
 		fd = open(path, O_RDONLY);
@@ -460,14 +459,13 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
 		/* Only fill in this one device */
 		fi_args->num_devices = 1;
 
-		disk_super = (struct btrfs_super_block *)buf;
-		ret = btrfs_read_dev_super(fd, disk_super,
+		ret = btrfs_read_dev_super(fd, &disk_super,
 					   BTRFS_SUPER_INFO_OFFSET, 0);
 		if (ret < 0) {
 			ret = -EIO;
 			goto out;
 		}
-		last_devid = btrfs_stack_device_id(&disk_super->dev_item);
+		last_devid = btrfs_stack_device_id(&disk_super.dev_item);
 		fi_args->max_id = last_devid;
 
 		memcpy(fi_args->fsid, fs_devices_mnt->fsid, BTRFS_FSID_SIZE);
diff --git a/convert/common.c b/convert/common.c
index 5e7a1d6bea5a..00a7e55361b8 100644
--- a/convert/common.c
+++ b/convert/common.c
@@ -91,15 +91,13 @@ static int setup_temp_super(int fd, struct btrfs_mkfs_config *cfg,
 			    u64 root_bytenr, u64 chunk_bytenr)
 {
 	unsigned char chunk_uuid[BTRFS_UUID_SIZE];
-	char super_buf[BTRFS_SUPER_INFO_SIZE];
-	struct btrfs_super_block *super = (struct btrfs_super_block *)super_buf;
+	struct btrfs_super_block super = {};
 	int ret;
 
-	memset(super_buf, 0, BTRFS_SUPER_INFO_SIZE);
 	cfg->num_bytes = round_down(cfg->num_bytes, cfg->sectorsize);
 
 	if (*cfg->fs_uuid) {
-		if (uuid_parse(cfg->fs_uuid, super->fsid) != 0) {
+		if (uuid_parse(cfg->fs_uuid, super.fsid) != 0) {
 			error("could not parse UUID: %s", cfg->fs_uuid);
 			ret = -EINVAL;
 			goto out;
@@ -108,43 +106,43 @@ static int setup_temp_super(int fd, struct btrfs_mkfs_config *cfg,
 		 * Caller should make sure the uuid is either unique or OK to
 		 * be duplicate in case it's copied from the source filesystem.
 		 */
-		uuid_copy(super->metadata_uuid, super->fsid);
+		uuid_copy(super.metadata_uuid, super.fsid);
 	} else {
-		uuid_generate(super->fsid);
-		uuid_unparse(super->fsid, cfg->fs_uuid);
-		uuid_copy(super->metadata_uuid, super->fsid);
+		uuid_generate(super.fsid);
+		uuid_unparse(super.fsid, cfg->fs_uuid);
+		uuid_copy(super.metadata_uuid, super.fsid);
 	}
 	uuid_generate(chunk_uuid);
 	uuid_unparse(chunk_uuid, cfg->chunk_uuid);
 
-	btrfs_set_super_bytenr(super, cfg->super_bytenr);
-	btrfs_set_super_num_devices(super, 1);
-	btrfs_set_super_magic(super, BTRFS_MAGIC_TEMPORARY);
-	btrfs_set_super_generation(super, 1);
-	btrfs_set_super_root(super, root_bytenr);
-	btrfs_set_super_chunk_root(super, chunk_bytenr);
-	btrfs_set_super_total_bytes(super, cfg->num_bytes);
+	btrfs_set_super_bytenr(&super, cfg->super_bytenr);
+	btrfs_set_super_num_devices(&super, 1);
+	btrfs_set_super_magic(&super, BTRFS_MAGIC_TEMPORARY);
+	btrfs_set_super_generation(&super, 1);
+	btrfs_set_super_root(&super, root_bytenr);
+	btrfs_set_super_chunk_root(&super, chunk_bytenr);
+	btrfs_set_super_total_bytes(&super, cfg->num_bytes);
 	/*
 	 * Temporary filesystem will only have 6 tree roots:
 	 * chunk tree, root tree, extent_tree, device tree, fs tree
 	 * and csum tree.
 	 */
-	btrfs_set_super_bytes_used(super, 6 * cfg->nodesize);
-	btrfs_set_super_sectorsize(super, cfg->sectorsize);
-	super->__unused_leafsize = cpu_to_le32(cfg->nodesize);
-	btrfs_set_super_nodesize(super, cfg->nodesize);
-	btrfs_set_super_stripesize(super, cfg->stripesize);
-	btrfs_set_super_csum_type(super, cfg->csum_type);
-	btrfs_set_super_chunk_root(super, chunk_bytenr);
-	btrfs_set_super_cache_generation(super, -1);
-	btrfs_set_super_incompat_flags(super, cfg->features);
+	btrfs_set_super_bytes_used(&super, 6 * cfg->nodesize);
+	btrfs_set_super_sectorsize(&super, cfg->sectorsize);
+	super.__unused_leafsize = cpu_to_le32(cfg->nodesize);
+	btrfs_set_super_nodesize(&super, cfg->nodesize);
+	btrfs_set_super_stripesize(&super, cfg->stripesize);
+	btrfs_set_super_csum_type(&super, cfg->csum_type);
+	btrfs_set_super_chunk_root(&super, chunk_bytenr);
+	btrfs_set_super_cache_generation(&super, -1);
+	btrfs_set_super_incompat_flags(&super, cfg->features);
 	if (cfg->label)
-		__strncpy_null(super->label, cfg->label, BTRFS_LABEL_SIZE - 1);
+		__strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE - 1);
 
 	/* Sys chunk array will be re-initialized at chunk tree init time */
-	super->sys_chunk_array_size = 0;
+	super.sys_chunk_array_size = 0;
 
-	ret = write_temp_super(fd, super, cfg->super_bytenr);
+	ret = write_temp_super(fd, &super, cfg->super_bytenr);
 out:
 	return ret;
 }
@@ -295,13 +293,12 @@ static int insert_temp_dev_item(int fd, struct extent_buffer *buf,
 {
 	struct btrfs_disk_key disk_key;
 	struct btrfs_dev_item *dev_item;
-	char super_buf[BTRFS_SUPER_INFO_SIZE];
 	unsigned char dev_uuid[BTRFS_UUID_SIZE];
 	unsigned char fsid[BTRFS_FSID_SIZE];
-	struct btrfs_super_block *super = (struct btrfs_super_block *)super_buf;
+	struct btrfs_super_block super;
 	int ret;
 
-	ret = pread(fd, super_buf, BTRFS_SUPER_INFO_SIZE, cfg->super_bytenr);
+	ret = pread(fd, &super, BTRFS_SUPER_INFO_SIZE, cfg->super_bytenr);
 	if (ret < BTRFS_SUPER_INFO_SIZE) {
 		ret = (ret < 0 ? -errno : -EIO);
 		goto out;
@@ -342,9 +339,9 @@ static int insert_temp_dev_item(int fd, struct extent_buffer *buf,
 	btrfs_set_device_type(buf, dev_item, 0);
 
 	/* Super dev_item is not complete, copy the complete one to sb */
-	read_extent_buffer(buf, &super->dev_item, (unsigned long)dev_item,
+	read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
 			   sizeof(*dev_item));
-	ret = write_temp_super(fd, super, cfg->super_bytenr);
+	ret = write_temp_super(fd, &super, cfg->super_bytenr);
 	(*slot)++;
 out:
 	return ret;
@@ -357,12 +354,10 @@ static int insert_temp_chunk_item(int fd, struct extent_buffer *buf,
 {
 	struct btrfs_chunk *chunk;
 	struct btrfs_disk_key disk_key;
-	char super_buf[BTRFS_SUPER_INFO_SIZE];
-	struct btrfs_super_block *sb = (struct btrfs_super_block *)super_buf;
+	struct btrfs_super_block sb;
 	int ret = 0;
 
-	ret = pread(fd, super_buf, BTRFS_SUPER_INFO_SIZE,
-		    cfg->super_bytenr);
+	ret = pread(fd, &sb, BTRFS_SUPER_INFO_SIZE, cfg->super_bytenr);
 	if (ret < BTRFS_SUPER_INFO_SIZE) {
 		ret = (ret < 0 ? ret : -EIO);
 		return ret;
@@ -391,7 +386,7 @@ static int insert_temp_chunk_item(int fd, struct extent_buffer *buf,
 	btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
 	/* We are doing 1:1 mapping, so start is its dev offset */
 	btrfs_set_stripe_offset_nr(buf, chunk, 0, start);
-	write_extent_buffer(buf, &sb->dev_item.uuid,
+	write_extent_buffer(buf, sb.dev_item.uuid,
 			    (unsigned long)btrfs_stripe_dev_uuid_nr(chunk, 0),
 			    BTRFS_UUID_SIZE);
 	(*slot)++;
@@ -403,18 +398,18 @@ static int insert_temp_chunk_item(int fd, struct extent_buffer *buf,
 		char *cur;
 		u32 array_size;
 
-		cur = (char *)sb->sys_chunk_array
-			+ btrfs_super_sys_array_size(sb);
+		cur = (char *)sb.sys_chunk_array
+			+ btrfs_super_sys_array_size(&sb);
 		memcpy(cur, &disk_key, sizeof(disk_key));
 		cur += sizeof(disk_key);
 		read_extent_buffer(buf, cur, (unsigned long int)chunk,
 				   btrfs_chunk_item_size(1));
-		array_size = btrfs_super_sys_array_size(sb);
+		array_size = btrfs_super_sys_array_size(&sb);
 		array_size += btrfs_chunk_item_size(1) +
 					    sizeof(disk_key);
-		btrfs_set_super_sys_array_size(sb, array_size);
+		btrfs_set_super_sys_array_size(&sb, array_size);
 
-		ret = write_temp_super(fd, sb, cfg->super_bytenr);
+		ret = write_temp_super(fd, &sb, cfg->super_bytenr);
 	}
 	return ret;
 }
diff --git a/convert/main.c b/convert/main.c
index 223eebad2e72..ccfa826ed0d5 100644
--- a/convert/main.c
+++ b/convert/main.c
@@ -1066,27 +1066,23 @@ err:
 static int migrate_super_block(int fd, u64 old_bytenr)
 {
 	int ret;
-	struct extent_buffer *buf;
-	struct btrfs_super_block *super;
+	struct btrfs_super_block super;
+	u8 result[BTRFS_CSUM_SIZE] = {};
 	u32 len;
 	u32 bytenr;
 
-	buf = malloc(sizeof(*buf) + BTRFS_SUPER_INFO_SIZE);
-	if (!buf)
-		return -ENOMEM;
-
-	buf->len = BTRFS_SUPER_INFO_SIZE;
-	ret = pread(fd, buf->data, BTRFS_SUPER_INFO_SIZE, old_bytenr);
+	ret = pread(fd, &super, BTRFS_SUPER_INFO_SIZE, old_bytenr);
 	if (ret != BTRFS_SUPER_INFO_SIZE)
 		goto fail;
 
-	super = (struct btrfs_super_block *)buf->data;
-	BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
-	btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
+	BUG_ON(btrfs_super_bytenr(&super) != old_bytenr);
+	btrfs_set_super_bytenr(&super, BTRFS_SUPER_INFO_OFFSET);
 
-	csum_tree_block_size(buf, btrfs_super_csum_size(super),
-			     0, btrfs_super_csum_type(super));
-	ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
+	btrfs_csum_data(NULL, btrfs_super_csum_type(&super),
+			(u8 *)&super + BTRFS_CSUM_SIZE, result,
+			BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+	memcpy(&super.csum[0], result, BTRFS_CSUM_SIZE);
+	ret = pwrite(fd, &super , BTRFS_SUPER_INFO_SIZE,
 		BTRFS_SUPER_INFO_OFFSET);
 	if (ret != BTRFS_SUPER_INFO_SIZE)
 		goto fail;
@@ -1095,12 +1091,12 @@ static int migrate_super_block(int fd, u64 old_bytenr)
 	if (ret)
 		goto fail;
 
-	memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
+	memset(&super, 0, BTRFS_SUPER_INFO_SIZE);
 	for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
 		len = BTRFS_SUPER_INFO_OFFSET - bytenr;
 		if (len > BTRFS_SUPER_INFO_SIZE)
 			len = BTRFS_SUPER_INFO_SIZE;
-		ret = pwrite(fd, buf->data, len, bytenr);
+		ret = pwrite(fd, &super, len, bytenr);
 		if (ret != len) {
 			fprintf(stderr, "unable to zero fill device\n");
 			break;
@@ -1110,7 +1106,6 @@ static int migrate_super_block(int fd, u64 old_bytenr)
 	ret = 0;
 	fsync(fd);
 fail:
-	free(buf);
 	if (ret > 0)
 		ret = -1;
 	return ret;
diff --git a/image/main.c b/image/main.c
index b40e0e5550f8..8929e94cb6e6 100644
--- a/image/main.c
+++ b/image/main.c
@@ -2927,12 +2927,11 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
 	struct extent_buffer *leaf;
 	struct btrfs_path path;
 	struct btrfs_dev_item *dev_item;
-	struct btrfs_super_block *disk_super;
+	struct btrfs_super_block disk_super;
 	char dev_uuid[BTRFS_UUID_SIZE];
 	char fs_uuid[BTRFS_UUID_SIZE];
 	u64 devid, type, io_align, io_width;
 	u64 sector_size, total_bytes, bytes_used;
-	char buf[BTRFS_SUPER_INFO_SIZE];
 	int fp = -1;
 	int ret;
 
@@ -2982,10 +2981,9 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
 		goto out;
 	}
 
-	memcpy(buf, info->super_copy, BTRFS_SUPER_INFO_SIZE);
+	memcpy(&disk_super, info->super_copy, BTRFS_SUPER_INFO_SIZE);
 
-	disk_super = (struct btrfs_super_block *)buf;
-	dev_item = &disk_super->dev_item;
+	dev_item = &disk_super.dev_item;
 
 	btrfs_set_stack_device_type(dev_item, type);
 	btrfs_set_stack_device_id(dev_item, devid);
@@ -2996,9 +2994,9 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
 	btrfs_set_stack_device_sector_size(dev_item, sector_size);
 	memcpy(dev_item->uuid, dev_uuid, BTRFS_UUID_SIZE);
 	memcpy(dev_item->fsid, fs_uuid, BTRFS_UUID_SIZE);
-	csum_block((u8 *)buf, BTRFS_SUPER_INFO_SIZE);
+	csum_block((u8 *)&disk_super, BTRFS_SUPER_INFO_SIZE);
 
-	ret = pwrite64(fp, buf, BTRFS_SUPER_INFO_SIZE, BTRFS_SUPER_INFO_OFFSET);
+	ret = pwrite64(fp, &disk_super, BTRFS_SUPER_INFO_SIZE, BTRFS_SUPER_INFO_OFFSET);
 	if (ret != BTRFS_SUPER_INFO_SIZE) {
 		if (ret < 0) {
 			errno = ret;
@@ -3010,7 +3008,7 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info,
 		goto out;
 	}
 
-	write_backup_supers(fp, (u8 *)buf);
+	write_backup_supers(fp, (u8 *)&disk_super);
 
 out:
 	if (fp != -1)
diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c
index 3b3c4523e65e..30f16f4da126 100644
--- a/kernel-shared/disk-io.c
+++ b/kernel-shared/disk-io.c
@@ -1619,8 +1619,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 	u8 fsid[BTRFS_FSID_SIZE];
 	u8 metadata_uuid[BTRFS_FSID_SIZE];
 	int fsid_is_initialized = 0;
-	char tmp[BTRFS_SUPER_INFO_SIZE];
-	struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
+	struct btrfs_super_block buf;
 	int i;
 	int ret;
 	int max_super = sbflags & SBREAD_RECOVER ? BTRFS_SUPER_MIRROR_MAX : 1;
@@ -1629,7 +1628,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 	u64 bytenr;
 
 	if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
-		ret = sbread(fd, buf, sb_bytenr);
+		ret = sbread(fd, &buf, sb_bytenr);
 		/* real error */
 		if (ret < 0)
 			return -errno;
@@ -1638,13 +1637,13 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 		if (ret < BTRFS_SUPER_INFO_SIZE)
 			return -ENOENT;
 
-		if (btrfs_super_bytenr(buf) != sb_bytenr)
+		if (btrfs_super_bytenr(&buf) != sb_bytenr)
 			return -EIO;
 
-		ret = btrfs_check_super(buf, sbflags);
+		ret = btrfs_check_super(&buf, sbflags);
 		if (ret < 0)
 			return ret;
-		memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
+		memcpy(sb, &buf, BTRFS_SUPER_INFO_SIZE);
 		return 0;
 	}
 
@@ -1657,31 +1656,31 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 
 	for (i = 0; i < max_super; i++) {
 		bytenr = btrfs_sb_offset(i);
-		ret = sbread(fd, buf, bytenr);
+		ret = sbread(fd, &buf, bytenr);
 
 		if (ret < BTRFS_SUPER_INFO_SIZE)
 			break;
 
-		if (btrfs_super_bytenr(buf) != bytenr )
+		if (btrfs_super_bytenr(&buf) != bytenr )
 			continue;
 		/* if magic is NULL, the device was removed */
-		if (btrfs_super_magic(buf) == 0 && i == 0)
+		if (btrfs_super_magic(&buf) == 0 && i == 0)
 			break;
-		if (btrfs_check_super(buf, sbflags))
+		if (btrfs_check_super(&buf, sbflags))
 			continue;
 
 		if (!fsid_is_initialized) {
-			if (btrfs_super_incompat_flags(buf) &
+			if (btrfs_super_incompat_flags(&buf) &
 			    BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
 				metadata_uuid_set = true;
-				memcpy(metadata_uuid, buf->metadata_uuid,
+				memcpy(metadata_uuid, buf.metadata_uuid,
 				       sizeof(metadata_uuid));
 			}
-			memcpy(fsid, buf->fsid, sizeof(fsid));
+			memcpy(fsid, buf.fsid, sizeof(fsid));
 			fsid_is_initialized = 1;
-		} else if (memcmp(fsid, buf->fsid, sizeof(fsid)) ||
+		} else if (memcmp(fsid, buf.fsid, sizeof(fsid)) ||
 			   (metadata_uuid_set && memcmp(metadata_uuid,
-							buf->metadata_uuid,
+							buf.metadata_uuid,
 							sizeof(metadata_uuid)))) {
 			/*
 			 * the superblocks (the original one and
@@ -1691,9 +1690,9 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 			continue;
 		}
 
-		if (btrfs_super_generation(buf) > transid) {
-			memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
-			transid = btrfs_super_generation(buf);
+		if (btrfs_super_generation(&buf) > transid) {
+			memcpy(sb, &buf, BTRFS_SUPER_INFO_SIZE);
+			transid = btrfs_super_generation(&buf);
 		}
 	}
 
diff --git a/kernel-shared/volumes.c b/kernel-shared/volumes.c
index 6c1e6f1018a3..de3a95d0c2e5 100644
--- a/kernel-shared/volumes.c
+++ b/kernel-shared/volumes.c
@@ -528,22 +528,20 @@ int btrfs_scan_one_device(int fd, const char *path,
 			  struct btrfs_fs_devices **fs_devices_ret,
 			  u64 *total_devs, u64 super_offset, unsigned sbflags)
 {
-	struct btrfs_super_block *disk_super;
-	char buf[BTRFS_SUPER_INFO_SIZE];
+	struct btrfs_super_block disk_super;
 	int ret;
 	u64 devid;
 
-	disk_super = (struct btrfs_super_block *)buf;
-	ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
+	ret = btrfs_read_dev_super(fd, &disk_super, super_offset, sbflags);
 	if (ret < 0)
 		return -EIO;
-	devid = btrfs_stack_device_id(&disk_super->dev_item);
-	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
+	devid = btrfs_stack_device_id(&disk_super.dev_item);
+	if (btrfs_super_flags(&disk_super) & BTRFS_SUPER_FLAG_METADUMP)
 		*total_devs = 1;
 	else
-		*total_devs = btrfs_super_num_devices(disk_super);
+		*total_devs = btrfs_super_num_devices(&disk_super);
 
-	ret = device_list_add(path, disk_super, devid, fs_devices_ret);
+	ret = device_list_add(path, &disk_super, devid, fs_devices_ret);
 
 	return ret;
 }
diff --git a/mkfs/common.c b/mkfs/common.c
index 5c8d6ac13a3b..0a1fda2ac72e 100644
--- a/mkfs/common.c
+++ b/mkfs/common.c
@@ -804,19 +804,17 @@ static int check_btrfs_signature_zoned(const char *device)
 {
 	int fd;
 	int ret;
-	char buf[BTRFS_SUPER_INFO_SIZE];
-	struct btrfs_super_block *sb;
+	struct btrfs_super_block sb;
 
 	fd = open(device, O_RDONLY);
 	if (fd < 0)
 		return -1;
-	ret = pread(fd, buf, BTRFS_SUPER_INFO_SIZE, 0);
+	ret = pread(fd, &sb, BTRFS_SUPER_INFO_SIZE, 0);
 	if (ret < 0) {
 		ret = -1;
 		goto out;
 	}
-	sb = (struct btrfs_super_block *)buf;
-	if (btrfs_super_magic(sb) == BTRFS_MAGIC)
+	if (btrfs_super_magic(&sb) == BTRFS_MAGIC)
 		ret = 1;
 	else
 		ret = 0;
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] btrfs-progs: cache csum_size and csum_type in btrfs_fs_info
  2021-10-21  1:40 [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block Qu Wenruo
  2021-10-21  1:40 ` [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE Qu Wenruo
  2021-10-21  1:40 ` [PATCH 2/3] btrfs-progs: remove temporary buffer for super block Qu Wenruo
@ 2021-10-21  1:40 ` Qu Wenruo
  2021-11-04 19:10 ` [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block David Sterba
  3 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2021-10-21  1:40 UTC (permalink / raw)
  To: linux-btrfs

Just like kernel commit 22b6331d9617 ("btrfs: store precalculated
csum_size in fs_info"), we can cache csum_size and csum_type in
btrfs_fs_info.

Furthermore, there is already a 32 bits hole in btrfs_fs_info, and we
can fit csum_type and csum_size into the hole without increase the size
of btrfs_fs_info.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 btrfs-corrupt-block.c       |  6 +++---
 check/main.c                |  6 +++---
 check/mode-common.c         |  2 +-
 cmds/rescue-chunk-recover.c |  4 ++--
 kernel-shared/ctree.h       |  2 ++
 kernel-shared/disk-io.c     |  6 ++++--
 kernel-shared/file-item.c   | 15 +++++----------
 kernel-shared/print-tree.c  |  4 ++--
 8 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c
index c1624ee1e6bf..d00ed98e8ffe 100644
--- a/btrfs-corrupt-block.c
+++ b/btrfs-corrupt-block.c
@@ -158,9 +158,9 @@ static void corrupt_keys(struct btrfs_trans_handle *trans,
 	}
 	btrfs_mark_buffer_dirty(eb);
 	if (!trans) {
-		u16 csum_size =
-			btrfs_super_csum_size(fs_info->super_copy);
-		u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
+		u16 csum_size = fs_info->csum_size;
+		u16 csum_type = fs_info->csum_type;
+
 		csum_tree_block_size(eb, csum_size, 0, csum_type);
 		write_extent_to_disk(eb);
 	}
diff --git a/check/main.c b/check/main.c
index 38b2cfdf5b0b..935e604ac1c6 100644
--- a/check/main.c
+++ b/check/main.c
@@ -5762,8 +5762,8 @@ static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
 			struct extent_buffer *eb)
 {
 	u64 offset = 0;
-	u16 csum_size = btrfs_super_csum_size(gfs_info->super_copy);
-	u16 csum_type = btrfs_super_csum_type(gfs_info->super_copy);
+	u16 csum_size = gfs_info->csum_size;
+	u16 csum_type = gfs_info->csum_type;
 	u8 *data;
 	unsigned long csum_offset;
 	u8 result[BTRFS_CSUM_SIZE];
@@ -5981,7 +5981,7 @@ static int check_csums(struct btrfs_root *root)
 	struct btrfs_key key;
 	u64 last_data_end = 0;
 	u64 offset = 0, num_bytes = 0;
-	u16 csum_size = btrfs_super_csum_size(gfs_info->super_copy);
+	u16 csum_size = gfs_info->csum_size;
 	int errors = 0;
 	int ret;
 	u64 data_len;
diff --git a/check/mode-common.c b/check/mode-common.c
index 0059672c6402..d28e79af64ef 100644
--- a/check/mode-common.c
+++ b/check/mode-common.c
@@ -294,7 +294,7 @@ int count_csum_range(u64 start, u64 len, u64 *found)
 	size_t size;
 	*found = 0;
 	u64 csum_end;
-	u16 csum_size = btrfs_super_csum_size(gfs_info->super_copy);
+	u16 csum_size = gfs_info->csum_size;
 
 	btrfs_init_path(&path);
 
diff --git a/cmds/rescue-chunk-recover.c b/cmds/rescue-chunk-recover.c
index 35c6f66548fd..15971873aca7 100644
--- a/cmds/rescue-chunk-recover.c
+++ b/cmds/rescue-chunk-recover.c
@@ -1820,7 +1820,7 @@ static int next_csum(struct btrfs_root *root,
 	struct btrfs_root *csum_root = root->fs_info->csum_root;
 	struct btrfs_csum_item *csum_item;
 	u32 blocksize = root->fs_info->sectorsize;
-	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
+	u16 csum_size = root->fs_info->csum_size;
 	int csums_in_item = btrfs_item_size_nr(*leaf, *slot) / csum_size;
 
 	if (*csum_offset >= csums_in_item) {
@@ -1904,7 +1904,7 @@ out:
 static u64 item_end_offset(struct btrfs_root *root, struct btrfs_key *key,
 			   struct extent_buffer *leaf, int slot) {
 	u32 blocksize = root->fs_info->sectorsize;
-	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
+	u16 csum_size = root->fs_info->csum_size;
 
 	u64 offset = btrfs_item_size_nr(leaf, slot);
 	offset /= csum_size;
diff --git a/kernel-shared/ctree.h b/kernel-shared/ctree.h
index 6451690ce4fa..2ebaae2e9321 100644
--- a/kernel-shared/ctree.h
+++ b/kernel-shared/ctree.h
@@ -1231,6 +1231,8 @@ struct btrfs_fs_info {
 	u32 nodesize;
 	u32 sectorsize;
 	u32 stripesize;
+	u16 csum_type;
+	u16 csum_size;
 
 	/*
 	 * Zone size > 0 when in ZONED mode, otherwise it's used for a check
diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c
index 30f16f4da126..e79f8b5a7c48 100644
--- a/kernel-shared/disk-io.c
+++ b/kernel-shared/disk-io.c
@@ -209,8 +209,8 @@ int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size,
 int csum_tree_block(struct btrfs_fs_info *fs_info,
 		    struct extent_buffer *buf, int verify)
 {
-	u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
-	u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
+	u16 csum_size = fs_info->csum_size;
+	u16 csum_type = fs_info->csum_type;
 
 	if (verify && fs_info->suppress_check_block_errors)
 		return verify_tree_block_csum_silent(buf, csum_size, csum_type);
@@ -1297,6 +1297,8 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, struct open_ctree_flags *oc
 	fs_info->sectorsize = btrfs_super_sectorsize(disk_super);
 	fs_info->nodesize = btrfs_super_nodesize(disk_super);
 	fs_info->stripesize = btrfs_super_stripesize(disk_super);
+	fs_info->csum_type = btrfs_super_csum_type(disk_super);
+	fs_info->csum_size = btrfs_super_csum_size(disk_super);
 
 	ret = btrfs_check_fs_compatibility(fs_info->super_copy, flags);
 	if (ret)
diff --git a/kernel-shared/file-item.c b/kernel-shared/file-item.c
index c910e27e5a5d..ecb8e5cd29e1 100644
--- a/kernel-shared/file-item.c
+++ b/kernel-shared/file-item.c
@@ -142,8 +142,7 @@ btrfs_lookup_csum(struct btrfs_trans_handle *trans,
 	struct btrfs_csum_item *item;
 	struct extent_buffer *leaf;
 	u64 csum_offset = 0;
-	u16 csum_size =
-		btrfs_super_csum_size(root->fs_info->super_copy);
+	u16 csum_size = root->fs_info->csum_size;
 	int csums_in_item;
 
 	file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
@@ -199,11 +198,8 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
 	u32 sectorsize = root->fs_info->sectorsize;
 	u32 nritems;
 	u32 ins_size;
-	u16 csum_size =
-		btrfs_super_csum_size(root->fs_info->super_copy);
-
-	u16 csum_type =
-		btrfs_super_csum_type(root->fs_info->super_copy);
+	u16 csum_size = root->fs_info->csum_size;
+	u16 csum_type = root->fs_info->csum_type;
 
 	path = btrfs_alloc_path();
 	if (!path)
@@ -341,8 +337,7 @@ static noinline int truncate_one_csum(struct btrfs_root *root,
 				      u64 bytenr, u64 len)
 {
 	struct extent_buffer *leaf;
-	u16 csum_size =
-		btrfs_super_csum_size(root->fs_info->super_copy);
+	u16 csum_size = root->fs_info->csum_size;
 	u64 csum_end;
 	u64 end_byte = bytenr + len;
 	u32 blocksize = root->fs_info->sectorsize;
@@ -399,7 +394,7 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, u64 bytenr, u64 len)
 	u64 csum_end;
 	struct extent_buffer *leaf;
 	int ret;
-	u16 csum_size = btrfs_super_csum_size(trans->fs_info->super_copy);
+	u16 csum_size = trans->fs_info->csum_size;
 	int blocksize = trans->fs_info->sectorsize;
 	struct btrfs_root *csum_root = trans->fs_info->csum_root;
 
diff --git a/kernel-shared/print-tree.c b/kernel-shared/print-tree.c
index 39655590272e..4de51175d62f 100644
--- a/kernel-shared/print-tree.c
+++ b/kernel-shared/print-tree.c
@@ -1156,7 +1156,7 @@ static void print_extent_csum(struct extent_buffer *eb,
 		printf("\t\trange start %llu\n", (unsigned long long)offset);
 		return;
 	}
-	csum_size = btrfs_super_csum_size(fs_info->super_copy);
+	csum_size = fs_info->csum_size;
 	size = (item_size / csum_size) * fs_info->sectorsize;
 	printf("\t\trange start %llu end %llu length %u\n",
 			(unsigned long long)offset,
@@ -1246,7 +1246,7 @@ static void print_header_info(struct extent_buffer *eb, unsigned int mode)
 		char *tmp = csum_str;
 		u8 *csum = (u8 *)(eb->data + offsetof(struct btrfs_header, csum));
 
-		csum_size = btrfs_super_csum_size(fs_info->super_copy);
+		csum_size = fs_info->csum_size;
 		strcpy(csum_str, " csum 0x");
 		tmp = csum_str + strlen(csum_str);
 		for (i = 0; i < csum_size; i++) {
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE
  2021-10-21  1:40 ` [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE Qu Wenruo
@ 2021-10-21 12:40   ` David Sterba
  2021-10-21 12:49     ` Qu Wenruo
  0 siblings, 1 reply; 7+ messages in thread
From: David Sterba @ 2021-10-21 12:40 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

On Thu, Oct 21, 2021 at 09:40:18AM +0800, Qu Wenruo wrote:
> Just like kernel change, pad struct btrfs_super_block to 4096 bytes.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  kernel-shared/ctree.h   | 7 +++++++
>  kernel-shared/disk-io.h | 3 ---
>  2 files changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel-shared/ctree.h b/kernel-shared/ctree.h
> index 563ea50b3587..6451690ce4fa 100644
> --- a/kernel-shared/ctree.h
> +++ b/kernel-shared/ctree.h
> @@ -406,6 +406,9 @@ struct btrfs_root_backup {
>  	u8 unused_8[10];
>  } __attribute__ ((__packed__));
>  
> +#define BTRFS_SUPER_INFO_OFFSET SZ_64K
> +#define BTRFS_SUPER_INFO_SIZE 4096
> +
>  /*
>   * the super block basically lists the main trees of the FS
>   * it currently lacks any block count etc etc
> @@ -456,8 +459,12 @@ struct btrfs_super_block {
>  	__le64 reserved[28];
>  	u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
>  	struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
> +	/* Padded to 4096 bytes */
> +	u8 padding[565];
>  } __attribute__ ((__packed__));
>  
> +static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);

Using static_assert breaks build on musl (you can verify that by running
ci/ci-build-musl if you have docker installed and set up).

There already is macro BUILD_ASSERT used in ioctl.h, eventually we can
copy the static_assert from kernel or use _Static_assert directly.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE
  2021-10-21 12:40   ` David Sterba
@ 2021-10-21 12:49     ` Qu Wenruo
  0 siblings, 0 replies; 7+ messages in thread
From: Qu Wenruo @ 2021-10-21 12:49 UTC (permalink / raw)
  To: dsterba, Qu Wenruo, linux-btrfs



On 2021/10/21 20:40, David Sterba wrote:
> On Thu, Oct 21, 2021 at 09:40:18AM +0800, Qu Wenruo wrote:
>> Just like kernel change, pad struct btrfs_super_block to 4096 bytes.
>>
>> Signed-off-by: Qu Wenruo <wqu@suse.com>
>> ---
>>   kernel-shared/ctree.h   | 7 +++++++
>>   kernel-shared/disk-io.h | 3 ---
>>   2 files changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel-shared/ctree.h b/kernel-shared/ctree.h
>> index 563ea50b3587..6451690ce4fa 100644
>> --- a/kernel-shared/ctree.h
>> +++ b/kernel-shared/ctree.h
>> @@ -406,6 +406,9 @@ struct btrfs_root_backup {
>>   	u8 unused_8[10];
>>   } __attribute__ ((__packed__));
>>
>> +#define BTRFS_SUPER_INFO_OFFSET SZ_64K
>> +#define BTRFS_SUPER_INFO_SIZE 4096
>> +
>>   /*
>>    * the super block basically lists the main trees of the FS
>>    * it currently lacks any block count etc etc
>> @@ -456,8 +459,12 @@ struct btrfs_super_block {
>>   	__le64 reserved[28];
>>   	u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
>>   	struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
>> +	/* Padded to 4096 bytes */
>> +	u8 padding[565];
>>   } __attribute__ ((__packed__));
>>
>> +static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);
>
> Using static_assert breaks build on musl (you can verify that by running
> ci/ci-build-musl if you have docker installed and set up).
>
> There already is macro BUILD_ASSERT used in ioctl.h, eventually we can
> copy the static_assert from kernel or use _Static_assert directly.
>
Mind to fix that on your side?

As I don't yet have any building environment for musl to verify the
failure or fix.

Thanks,
Qu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block
  2021-10-21  1:40 [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block Qu Wenruo
                   ` (2 preceding siblings ...)
  2021-10-21  1:40 ` [PATCH 3/3] btrfs-progs: cache csum_size and csum_type in btrfs_fs_info Qu Wenruo
@ 2021-11-04 19:10 ` David Sterba
  3 siblings, 0 replies; 7+ messages in thread
From: David Sterba @ 2021-11-04 19:10 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

On Thu, Oct 21, 2021 at 09:40:17AM +0800, Qu Wenruo wrote:
> This patchset mostly cleans up the mismatch between
> BTRFS_SUPER_INFO_SIZE and sizeof(struct btrfs_super_block).
> 
> This cleanup itself is going to replace all the following snippets:
> 
> 	u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
> 	struct btrfs_super_block *sb;
> 
> 	sb = (struct btrfs_super_block *)super_block_data;
> 
> With:
> 
> 	struct btrfs_super_block sb;
> 
> Also since we're here, also cache csum_type and csum_size in fs_info,
> just like what we did in the kernel.
> 
> Qu Wenruo (3):
>   btrfs-progs: unify sizeof(struct btrfs_super_block) and
>     BTRFS_SUPER_INFO_SIZE
>   btrfs-progs: remove temporary buffer for super block
>   btrfs-progs: cache csum_size and csum_type in btrfs_fs_info

Added to devel, thanks, the removal of temporary buffer for superblock
is nice.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-11-04 19:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-10-21  1:40 [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block Qu Wenruo
2021-10-21  1:40 ` [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE Qu Wenruo
2021-10-21 12:40   ` David Sterba
2021-10-21 12:49     ` Qu Wenruo
2021-10-21  1:40 ` [PATCH 2/3] btrfs-progs: remove temporary buffer for super block Qu Wenruo
2021-10-21  1:40 ` [PATCH 3/3] btrfs-progs: cache csum_size and csum_type in btrfs_fs_info Qu Wenruo
2021-11-04 19:10 ` [PATCH 0/3] btrfs-progs: cleanup related to btrfs super block David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox