diff mbox series

btrfs-progs: btrfs dev us: don't print uncorrect unallocated data

Message ID f862a81c8ac4b63b2cca2096ffb75907ae899c95.1704398024.git.kreijack@inwind.it (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: btrfs dev us: don't print uncorrect unallocated data | expand

Commit Message

Goffredo Baroncelli Jan. 4, 2024, 7:53 p.m. UTC
From: Goffredo Baroncelli <kreijack@inwind.it>

If "btrfs dev us" is invoked by a not root user, it is imposible to
collect the chunk info data (not enough privileges). This causes
"btrfs dev us" to print as "Unallocated" value the size of the disk.

This patch handle the case where print_device_chunks() is invoked
without the chunk info data, printing "Unallocated N/A":

Before the patch:

$ btrfs dev us t/
WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
/dev/loop0, ID: 1
   Device size:             5.00GiB
   Device slack:              0.00B
   Unallocated:             5.00GiB  <-- Wrong

$ sudo btrfs dev us t/
[sudo] password for ghigo:
/dev/loop0, ID: 1
   Device size:             5.00GiB
   Device slack:              0.00B
   Data,single:             8.00MiB
   Metadata,DUP:          512.00MiB
   System,DUP:             16.00MiB
   Unallocated:             4.48GiB  <-- Correct

After the patch:
$ ./btrfs dev us /tmp/t/
WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
/dev/loop0, ID: 1
   Device size:             5.00GiB
   Device slack:              0.00B
   Unallocated:                 N/A

$ sudo ./btrfs dev us /tmp/t/
[sudo] password for ghigo:
/dev/loop0, ID: 1
   Device size:             5.00GiB
   Device slack:              0.00B
   Data,single:             8.00MiB
   Metadata,DUP:          512.00MiB
   System,DUP:             16.00MiB
   Unallocated:             4.48GiB

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Reviewed-by: Anand Jain <anand.jain@oracle.com>

---
 cmds/filesystem-usage.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

Comments

Goffredo Baroncelli Jan. 4, 2024, 7:56 p.m. UTC | #1
This patch is equal to the previous one. I added only the SOB and Reviewed-by lines.

On 04/01/2024 20.53, Goffredo Baroncelli wrote:
> From: Goffredo Baroncelli <kreijack@inwind.it>
> 
> If "btrfs dev us" is invoked by a not root user, it is imposible to
> collect the chunk info data (not enough privileges). This causes
> "btrfs dev us" to print as "Unallocated" value the size of the disk.
> 
> This patch handle the case where print_device_chunks() is invoked
> without the chunk info data, printing "Unallocated N/A":
> 
> Before the patch:
> 
> $ btrfs dev us t/
> WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
> /dev/loop0, ID: 1
>     Device size:             5.00GiB
>     Device slack:              0.00B
>     Unallocated:             5.00GiB  <-- Wrong
> 
> $ sudo btrfs dev us t/
> [sudo] password for ghigo:
> /dev/loop0, ID: 1
>     Device size:             5.00GiB
>     Device slack:              0.00B
>     Data,single:             8.00MiB
>     Metadata,DUP:          512.00MiB
>     System,DUP:             16.00MiB
>     Unallocated:             4.48GiB  <-- Correct
> 
> After the patch:
> $ ./btrfs dev us /tmp/t/
> WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
> /dev/loop0, ID: 1
>     Device size:             5.00GiB
>     Device slack:              0.00B
>     Unallocated:                 N/A
> 
> $ sudo ./btrfs dev us /tmp/t/
> [sudo] password for ghigo:
> /dev/loop0, ID: 1
>     Device size:             5.00GiB
>     Device slack:              0.00B
>     Data,single:             8.00MiB
>     Metadata,DUP:          512.00MiB
>     System,DUP:             16.00MiB
>     Unallocated:             4.48GiB
> 
> Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
> Reviewed-by: Anand Jain <anand.jain@oracle.com>
> 
> ---
>   cmds/filesystem-usage.c | 18 ++++++++++++++----
>   1 file changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
> index 015c401e..e88ee323 100644
> --- a/cmds/filesystem-usage.c
> +++ b/cmds/filesystem-usage.c
> @@ -1302,10 +1302,20 @@ void print_device_chunks(const struct device_info *devinfo,
>   		allocated += size;
>   
>   	}
> -	pr_verbose(LOG_DEFAULT, "   Unallocated: %*s%10s\n",
> -		(int)(20 - strlen("Unallocated")), "",
> -		pretty_size_mode(devinfo->size - allocated,
> -			unit_mode | UNITS_NEGATIVE));
> +
> +	/*
> +	 * If chunkinfos is empty, we cannot compute the unallocated
> +	 * size, so don't print uncorrect data.
> +	 */
> +	if (chunkinfos->length == 0)
> +		pr_verbose(LOG_DEFAULT, "   Unallocated: %*s%10s\n",
> +			(int)(20 - strlen("Unallocated")), "",
> +			"N/A");
> +	else
> +		pr_verbose(LOG_DEFAULT, "   Unallocated: %*s%10s\n",
> +			(int)(20 - strlen("Unallocated")), "",
> +			pretty_size_mode(devinfo->size - allocated,
> +				unit_mode | UNITS_NEGATIVE));
>   }
>   
>   void print_device_sizes(const struct device_info *devinfo, unsigned unit_mode)
David Sterba Jan. 17, 2024, 7:52 p.m. UTC | #2
On Thu, Jan 04, 2024 at 08:53:44PM +0100, Goffredo Baroncelli wrote:
> From: Goffredo Baroncelli <kreijack@inwind.it>
> 
> If "btrfs dev us" is invoked by a not root user, it is imposible to
> collect the chunk info data (not enough privileges). This causes
> "btrfs dev us" to print as "Unallocated" value the size of the disk.
> 
> This patch handle the case where print_device_chunks() is invoked
> without the chunk info data, printing "Unallocated N/A":
> 
> Before the patch:
> 
> $ btrfs dev us t/
> WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
> /dev/loop0, ID: 1
>    Device size:             5.00GiB
>    Device slack:              0.00B
>    Unallocated:             5.00GiB  <-- Wrong
> 
> $ sudo btrfs dev us t/
> [sudo] password for ghigo:
> /dev/loop0, ID: 1
>    Device size:             5.00GiB
>    Device slack:              0.00B
>    Data,single:             8.00MiB
>    Metadata,DUP:          512.00MiB
>    System,DUP:             16.00MiB
>    Unallocated:             4.48GiB  <-- Correct
> 
> After the patch:
> $ ./btrfs dev us /tmp/t/
> WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
> /dev/loop0, ID: 1
>    Device size:             5.00GiB
>    Device slack:              0.00B
>    Unallocated:                 N/A
> 
> $ sudo ./btrfs dev us /tmp/t/
> [sudo] password for ghigo:
> /dev/loop0, ID: 1
>    Device size:             5.00GiB
>    Device slack:              0.00B
>    Data,single:             8.00MiB
>    Metadata,DUP:          512.00MiB
>    System,DUP:             16.00MiB
>    Unallocated:             4.48GiB
> 
> Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
> Reviewed-by: Anand Jain <anand.jain@oracle.com>

Added to devel, thanks.
diff mbox series

Patch

diff --git a/cmds/filesystem-usage.c b/cmds/filesystem-usage.c
index 015c401e..e88ee323 100644
--- a/cmds/filesystem-usage.c
+++ b/cmds/filesystem-usage.c
@@ -1302,10 +1302,20 @@  void print_device_chunks(const struct device_info *devinfo,
 		allocated += size;
 
 	}
-	pr_verbose(LOG_DEFAULT, "   Unallocated: %*s%10s\n",
-		(int)(20 - strlen("Unallocated")), "",
-		pretty_size_mode(devinfo->size - allocated,
-			unit_mode | UNITS_NEGATIVE));
+
+	/*
+	 * If chunkinfos is empty, we cannot compute the unallocated
+	 * size, so don't print uncorrect data.
+	 */
+	if (chunkinfos->length == 0)
+		pr_verbose(LOG_DEFAULT, "   Unallocated: %*s%10s\n",
+			(int)(20 - strlen("Unallocated")), "",
+			"N/A");
+	else
+		pr_verbose(LOG_DEFAULT, "   Unallocated: %*s%10s\n",
+			(int)(20 - strlen("Unallocated")), "",
+			pretty_size_mode(devinfo->size - allocated,
+				unit_mode | UNITS_NEGATIVE));
 }
 
 void print_device_sizes(const struct device_info *devinfo, unsigned unit_mode)