[v2,1/3] btrfs-progs: factor out super_block reading from load_and_dump_sb
diff mbox series

Message ID 20190424144754.4612-2-jthumshirn@suse.de
State New
Headers show
Series
  • btrfs-progs: provide command to dump checksums
Related show

Commit Message

Johannes Thumshirn April 24, 2019, 2:47 p.m. UTC
inspect-internal dump-superblock's load_and_dump_sb() already reads a
super block from a file descriptor and places it into a 'struct
btrfs_super_block'.

For inspect-internal dump-csum we need this super block as well but don't
care about printing it.

Separate the read from the dump phase so we can re-use it elsewhere.

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
---
 cmds-inspect-dump-super.c |  6 +++---
 utils.c                   | 17 +++++++++++++++++
 utils.h                   |  2 ++
 3 files changed, 22 insertions(+), 3 deletions(-)

Comments

Nikolay Borisov May 13, 2019, 12:41 p.m. UTC | #1
On 24.04.19 г. 17:47 ч., Johannes Thumshirn wrote:
> inspect-internal dump-superblock's load_and_dump_sb() already reads a
> super block from a file descriptor and places it into a 'struct
> btrfs_super_block'.
> 
> For inspect-internal dump-csum we need this super block as well but don't
> care about printing it.
> 
> Separate the read from the dump phase so we can re-use it elsewhere.
> 
> Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
> ---
>  cmds-inspect-dump-super.c |  6 +++---
>  utils.c                   | 17 +++++++++++++++++
>  utils.h                   |  2 ++
>  3 files changed, 22 insertions(+), 3 deletions(-)
> 
> diff --git a/cmds-inspect-dump-super.c b/cmds-inspect-dump-super.c
> index 7815c863f2ed..516760fad3da 100644
> --- a/cmds-inspect-dump-super.c
> +++ b/cmds-inspect-dump-super.c
> @@ -483,10 +483,10 @@ static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
>  
>  	sb = (struct btrfs_super_block *)super_block_data;
>  
> -	ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
> -	if (ret != BTRFS_SUPER_INFO_SIZE) {
> +	ret = load_sb(fd, sb_bytenr, sb, BTRFS_SUPER_INFO_SIZE);

There is no point in passing BTRFS_SUPER_INFO_SIZE since you already
have a function, just hide this detail inside load_sb.

> +	if (ret) {
>  		/* check if the disk if too short for further superblock */
> -		if (ret == 0 && errno == 0)
> +		if (ret == -ENOSPC)
>  			return 0;
>  
>  		error("failed to read the superblock on %s at %llu",
> diff --git a/utils.c b/utils.c
> index 9e26c884cc6c..b15cfcf5a434 100644
> --- a/utils.c
> +++ b/utils.c
> @@ -2593,3 +2593,20 @@ void print_all_devices(struct list_head *devices)
>  		print_device_info(dev, "\t");
>  	printf("\n");
>  }
> +
> +int load_sb(int fd, u64 bytenr, struct btrfs_super_block *sb, size_t size)
> +{
> +	int ret;
> +
> +	if (size != BTRFS_SUPER_INFO_SIZE)
> +		return -EINVAL;
> +
> +	ret = pread64(fd, sb, size, bytenr);
> +	if (ret != size) {
> +		if (ret == 0 && errno == 0)
> +			return -ENOSPC;

Makes no sense to return -ENOSPC on read error. Perhahps EINVAL or some
such?

> +
> +		return -errno;
> +	}
> +	return 0;
> +}
> diff --git a/utils.h b/utils.h
> index 7c5eb798557d..65549374b19c 100644
> --- a/utils.h
> +++ b/utils.h
> @@ -171,6 +171,8 @@ unsigned long total_memory(void);
>  void print_device_info(struct btrfs_device *device, char *prefix);
>  void print_all_devices(struct list_head *devices);
>  
> +int load_sb(int fd, u64 bytenr, struct btrfs_super_block *sb, size_t size);
> +
>  /*
>   * Global program state, configurable by command line and available to
>   * functions without extra context passing.
>

Patch
diff mbox series

diff --git a/cmds-inspect-dump-super.c b/cmds-inspect-dump-super.c
index 7815c863f2ed..516760fad3da 100644
--- a/cmds-inspect-dump-super.c
+++ b/cmds-inspect-dump-super.c
@@ -483,10 +483,10 @@  static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
 
 	sb = (struct btrfs_super_block *)super_block_data;
 
-	ret = pread64(fd, super_block_data, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
-	if (ret != BTRFS_SUPER_INFO_SIZE) {
+	ret = load_sb(fd, sb_bytenr, sb, BTRFS_SUPER_INFO_SIZE);
+	if (ret) {
 		/* check if the disk if too short for further superblock */
-		if (ret == 0 && errno == 0)
+		if (ret == -ENOSPC)
 			return 0;
 
 		error("failed to read the superblock on %s at %llu",
diff --git a/utils.c b/utils.c
index 9e26c884cc6c..b15cfcf5a434 100644
--- a/utils.c
+++ b/utils.c
@@ -2593,3 +2593,20 @@  void print_all_devices(struct list_head *devices)
 		print_device_info(dev, "\t");
 	printf("\n");
 }
+
+int load_sb(int fd, u64 bytenr, struct btrfs_super_block *sb, size_t size)
+{
+	int ret;
+
+	if (size != BTRFS_SUPER_INFO_SIZE)
+		return -EINVAL;
+
+	ret = pread64(fd, sb, size, bytenr);
+	if (ret != size) {
+		if (ret == 0 && errno == 0)
+			return -ENOSPC;
+
+		return -errno;
+	}
+	return 0;
+}
diff --git a/utils.h b/utils.h
index 7c5eb798557d..65549374b19c 100644
--- a/utils.h
+++ b/utils.h
@@ -171,6 +171,8 @@  unsigned long total_memory(void);
 void print_device_info(struct btrfs_device *device, char *prefix);
 void print_all_devices(struct list_head *devices);
 
+int load_sb(int fd, u64 bytenr, struct btrfs_super_block *sb, size_t size);
+
 /*
  * Global program state, configurable by command line and available to
  * functions without extra context passing.