btrfs-progs: cleanup related to btrfs super block
cover btrfs-progs: cleanup related to btrfs super block
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(-)
Signature · 1 line
2.33.0
PATCH 1/3 btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE
Just like kernel change, pad struct btrfs_super_block to 4096 bytes.
--- kernel-shared/ctree.h | 7 +++++++ kernel-shared/disk-io.h | 3 --- 2 files changed, 7 insertions(+), 3 deletions(-)
kernel-shared/ctree.h +7 -0
@@ -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
kernel-shared/disk-io.h +0 -3
@@ -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
Signature · 1 line
2.33.0
Re: [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE
Quoted 23 lines · Qu Wenruo
Just like kernel change, pad struct
btrfs_super_blockto 4096 bytes.Signed-off-by Qu Wenruo <[email protected]>--- kernel-shared/ctree.h | 7 +++++++ kernel-shared/disk-io.h | 3 --- 2 files changed, 7 insertions(+), 3 deletions(-)kernel-shared/ctree.h +3
-0@@ -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
kernel-shared/ctree.h struct btrfs_super_block { · line 466
Quoted 8 lines
kernel-shared/ctree.h +3
-0@@ -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.
Re: [PATCH 1/3] btrfs-progs: unify sizeof(struct btrfs_super_block) and BTRFS_SUPER_INFO_SIZE
Quoted 31 lines · David Sterba
Quoted 30 lines · Qu Wenruo
Just like kernel change, pad struct
btrfs_super_blockto 4096 bytes.Signed-off-by Qu Wenruo <[email protected]>--- kernel-shared/ctree.h | 7 +++++++ kernel-shared/disk-io.h | 3 --- 2 files changed, 7 insertions(+), 3 deletions(-)kernel-shared/ctree.h +5
-0@@ -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__));
Quoted 8 lines
kernel-shared/ctree.h struct btrfs_super_block { · line 466
Quoted 1 line
kernel-shared/ctree.h +1
-0+static_assert (sizeof (struct btrfs_super_block ) ==BTRFS_SUPER_INFO_SIZE );Using
static_assertbreaks 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_ASSERTused inioctl.h, eventually we can copy thestatic_assertfrom kernel or use_Static_assertdirectly.
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
PATCH 2/3 btrfs-progs: remove temporary buffer for super block
In reply to Qu WenruoThere 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.
--- 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(-)
cmds/filesystem-usage.c +3 -5
@@ -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:
cmds/inspect-dump-super.c +4 -7
@@ -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 ; }
cmds/rescue-chunk-recover.c +9 -11
@@ -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;
cmds/rescue-super-recover.c +5 -6
@@ -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;
common/device-scan.c +7 -16
@@ -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; }
common/utils.c +3 -5
@@ -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 );
convert/common.c +37 -42
154 lines not on this page · raw message
convert/main.c +12 -17
@@ -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;
image/main.c +6 -8
@@ -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 )
kernel-shared/disk-io.c +17 -18
@@ -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); } }
kernel-shared/volumes.c +6 -8
@@ -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; }
mkfs/common.c +3 -5
@@ -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 ;
Signature · 1 line
2.33.0
PATCH 3/3 btrfs-progs: cache csum_size and csum_type in btrfs_fs_info
In reply to Qu WenruoJust 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.
--- 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(-)
btrfs-corrupt-block.c +3 -3
@@ -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); }
check/main.c +3 -3
@@ -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;
check/mode-common.c +1 -1
@@ -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);
cmds/rescue-chunk-recover.c +2 -2
@@ -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;
kernel-shared/ctree.h +2 -0
@@ -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
kernel-shared/disk-io.c +4 -2
@@ -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)
kernel-shared/file-item.c +5 -10
@@ -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;
kernel-shared/print-tree.c +2 -2
@@ -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++) {
Signature · 1 line
2.33.0
Quoted 14 lines · Qu Wenruo
This patchset mostly cleans up the mismatch between
BTRFS_SUPER_INFO_SIZEandsizeof(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;
Quoted 8 lines
Also since we're here, also cache
csum_typeandcsum_sizeinfs_info, just like what we did in the kernel.Qu Wenruo (3):
btrfs-progs: unifysizeof(struct btrfs_super_block)and
BTRFS_SUPER_INFO_SIZE
btrfs-progs: remove temporary buffer for super block
btrfs-progs: cachecsum_sizeandcsum_typeinbtrfs_fs_info
Added to devel, thanks, the removal of temporary buffer for superblock is nice.