* [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 11:43 [PATCH][BTRFS-PROGS][V1] btrfs filesystem df Goffredo Baroncelli
@ 2012-10-03 11:43 ` Goffredo Baroncelli
2012-10-03 15:02 ` Ilya Dryomov
0 siblings, 1 reply; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 11:43 UTC (permalink / raw)
To: Chris Mason; +Cc: linux-btrfs@vger.kernel.org, Goffredo Baroncelli
The command btrfs filesystem df is used to query the
status of the chunks, how many space on the disk(s) are used by
the chunks, how many space are available in the chunks, and an
estimation of the free space of the filesystem.
---
cmds-filesystem.c | 264 +++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 215 insertions(+), 49 deletions(-)
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index b1457de..6c3ebdc 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <uuid/uuid.h>
#include <ctype.h>
+#include <sys/vfs.h>
#include "kerncompat.h"
#include "ctree.h"
@@ -39,25 +40,55 @@ static const char * const filesystem_cmd_group_usage[] = {
NULL
};
-static const char * const cmd_df_usage[] = {
- "btrfs filesystem df <path>",
- "Show space usage information for a mount point",
- NULL
-};
+static u64 disk_size( char *path){
+ struct statfs sfs;
+
+ if( statfs(path, &sfs) < 0 )
+ return 0;
+ else
+ return sfs.f_bsize * sfs.f_blocks;
+
+}
+
+static void print_string(char *s, int w)
+{
+ int i;
+
+ printf("%s", s);
+ for( i = strlen(s) ; i < w ; i++ )
+ putchar(' ');
+}
+
+#define DF_SHOW_SUMMARY (1<<1)
+#define DF_SHOW_DETAIL (1<<2)
+#define DF_HUMAN_UNIT (1<<3)
+
+static void pretty_sizes_r(u64 size, int w, int mode)
+{
+ if( mode & DF_HUMAN_UNIT ){
+ char *s = pretty_sizes(size);
+ printf("%*s", w, s);
+ free(s);
+ } else {
+ printf("%*llu", w, size/1024);
+
+ }
+}
-static int cmd_df(int argc, char **argv)
+static int _cmd_disk_free(char *path, int mode)
{
struct btrfs_ioctl_space_args *sargs;
u64 count = 0, i;
int ret;
int fd;
- int e;
- char *path;
-
- if (check_argc_exact(argc, 2))
- usage(cmd_df_usage);
-
- path = argv[1];
+ int e, width;
+ u64 total_disk; /* fielsystem size == sum of
+ disk sizes */
+ u64 total_chunks; /* sum of chunks sizes on disk(s) */
+ u64 total_used; /* logical space used */
+ u64 total_free; /* logical space un-used */
+ double K;
+
fd = open_file_or_dir(path);
if (fd < 0) {
@@ -103,56 +134,191 @@ static int cmd_df(int argc, char **argv)
return ret;
}
- for (i = 0; i < sargs->total_spaces; i++) {
- char description[80];
- char *total_bytes;
- char *used_bytes;
- int written = 0;
- u64 flags = sargs->spaces[i].flags;
+ total_disk = disk_size(path);
+ e = errno;
+ if( total_disk == 0 ){
+ fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
+ path, strerror(e));
+ close(fd);
+ free(sargs);
+ return ret;
+ }
+
+ total_chunks = total_used = total_free = 0;
- memset(description, 0, 80);
+ for (i = 0; i < sargs->total_spaces; i++) {
+ int ratio=1;
+ u64 allocated;
- if (flags & BTRFS_BLOCK_GROUP_DATA) {
- if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 14, "%s",
- "Data+Metadata");
- written += 13;
- } else {
- snprintf(description, 5, "%s", "Data");
- written += 4;
- }
- } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
- snprintf(description, 7, "%s", "System");
- written += 6;
- } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 9, "%s", "Metadata");
- written += 8;
- }
+ u64 flags = sargs->spaces[i].flags;
if (flags & BTRFS_BLOCK_GROUP_RAID0) {
- snprintf(description+written, 8, "%s", ", RAID0");
- written += 7;
+ ratio=1;
} else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
- snprintf(description+written, 8, "%s", ", RAID1");
- written += 7;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_DUP) {
- snprintf(description+written, 6, "%s", ", DUP");
- written += 5;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
- snprintf(description+written, 9, "%s", ", RAID10");
- written += 8;
+ ratio=2;
+ } else {
+ ratio=1;
}
- total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
- used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
- printf("%s: total=%s, used=%s\n", description, total_bytes,
- used_bytes);
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ total_chunks += allocated;
+ total_used += sargs->spaces[i].used_bytes;
+ total_free += (sargs->spaces[i].total_bytes -
+ sargs->spaces[i].used_bytes);
+
+ }
+ K = ((double)total_used + (double)total_free) /
+ (double)total_chunks;
+
+ if( mode & DF_HUMAN_UNIT )
+ width = 12;
+ else
+ width = 18;
+
+ printf("Path: %s\n", path);
+ if( mode & DF_SHOW_SUMMARY ){
+ printf("Summary:\n");
+ printf(" Disk_size:\t\t");
+ pretty_sizes_r(total_disk, width, mode);
+ printf("\n Disk_allocated:\t");
+ pretty_sizes_r(total_chunks, width, mode);
+ printf("\n Disk_unallocated:\t");
+ pretty_sizes_r(total_disk-total_chunks, width, mode);
+ printf("\n Logical_size:\t\t");
+ pretty_sizes_r(total_used+total_free, width, mode);
+ printf("\n Used:\t\t\t");
+ pretty_sizes_r(total_used, width, mode);
+ printf("\n Free_(Estimated):\t");
+ pretty_sizes_r((u64)(K*total_disk-total_used), width, mode);
+ printf("\t(Max: ");
+ pretty_sizes_r(total_disk-total_chunks+total_free,
+ 0, mode );
+ printf(", Min: ");
+ pretty_sizes_r((total_disk-total_chunks)/2+total_free,
+ 0, mode );
+ printf(")\n Data_to_disk_ratio:\t%*.0f %%\n",
+ width-2, K*100);
+ }
+
+ if( ( mode & DF_SHOW_DETAIL ) && ( mode & DF_SHOW_SUMMARY ) )
+ printf("\n");
+
+ if( mode & DF_SHOW_DETAIL ){
+ /* Remember: the terminals have maximum 80 columns
+ do not believe to who says otherwise */
+ printf("Details:\n");
+ printf(" %-12s%-8s%*s%*s%*s\n",
+ "Chunk-type",
+ "Mode",
+ width, "Chunk-size",
+ 1+width, "Logical-size",
+ width, "Used"
+ );
+
+ for (i = 0; i < sargs->total_spaces; i++) {
+ char *description="";
+ int ratio=1;
+ char *r_mode;
+ u64 allocated;
+
+ u64 flags = sargs->spaces[i].flags;
+
+ if (flags & BTRFS_BLOCK_GROUP_DATA) {
+ if (flags & BTRFS_BLOCK_GROUP_METADATA){
+ description = "Data+M.data";
+ } else {
+ description = "Data";
+ }
+ } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+ description = "System";
+ } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+ description = "Metadata";
+ }
+
+ if (flags & BTRFS_BLOCK_GROUP_RAID0) {
+ r_mode = "RAID0";
+ ratio=1;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
+ r_mode = "RAID1";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
+ r_mode = "DUP";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
+ r_mode = "RAID10";
+ ratio=2;
+ } else {
+ r_mode = "Single";
+ ratio=1;
+ }
+
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ printf(" ");
+ print_string(description,12);
+ print_string(r_mode, 8);
+ pretty_sizes_r(allocated, width, mode);
+ pretty_sizes_r(sargs->spaces[i].total_bytes ,
+ width+1, mode);
+
+ pretty_sizes_r(sargs->spaces[i].used_bytes,
+ width, mode);
+ printf("\n");
+
+ }
}
free(sargs);
return 0;
}
+static const char * const cmd_disk_free_usage[] = {
+ "btrfs filesystem df [-d][-s][-k] <path> [<path>..]",
+ "Show space usage information for a mount point(s).",
+ "",
+ "-k\tSet KB (1024 bytes) as unit",
+ "-s\tDon't show the summary section",
+ "-d\tDon't show the detail section",
+ NULL
+};
+
+static int cmd_disk_free(int argc, char **argv)
+{
+
+ int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
+ int i, more_than_one=0;
+
+ if (check_argc_min(argc, 2))
+ usage(cmd_disk_free_usage);
+
+ for(i=1; i< argc ; i++){
+ if(!strcmp(argv[i],"-d"))
+ flags &= ~DF_SHOW_DETAIL;
+ else if(!strcmp(argv[i],"-s"))
+ flags &= ~DF_SHOW_SUMMARY;
+ else if(!strcmp(argv[i],"-k"))
+ flags &= ~DF_HUMAN_UNIT;
+ else{
+ int r;
+ if(more_than_one)
+ printf("\n");
+ r = _cmd_disk_free(argv[i], flags);
+ if( r ) return r;
+ more_than_one=1;
+ }
+
+ }
+
+ return 0;
+}
+
+
+
static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
{
char uuidbuf[37];
@@ -529,7 +695,7 @@ static int cmd_label(int argc, char **argv)
const struct cmd_group filesystem_cmd_group = {
filesystem_cmd_group_usage, NULL, {
- { "df", cmd_df, cmd_df_usage, NULL, 0 },
+ { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
{ "show", cmd_show, cmd_show_usage, NULL, 0 },
{ "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
{ "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
--
1.7.10.4
^ permalink raw reply related [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 11:43 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
@ 2012-10-03 15:02 ` Ilya Dryomov
2012-10-03 16:34 ` Goffredo Baroncelli
2012-10-03 17:09 ` Goffredo Baroncelli
0 siblings, 2 replies; 19+ messages in thread
From: Ilya Dryomov @ 2012-10-03 15:02 UTC (permalink / raw)
To: Goffredo Baroncelli
Cc: Chris Mason, linux-btrfs@vger.kernel.org, Goffredo Baroncelli
On Wed, Oct 03, 2012 at 01:43:15PM +0200, Goffredo Baroncelli wrote:
> The command btrfs filesystem df is used to query the
> status of the chunks, how many space on the disk(s) are used by
> the chunks, how many space are available in the chunks, and an
> estimation of the free space of the filesystem.
> ---
> cmds-filesystem.c | 264 +++++++++++++++++++++++++++++++++++++++++++----------
> 1 file changed, 215 insertions(+), 49 deletions(-)
>
> diff --git a/cmds-filesystem.c b/cmds-filesystem.c
> index b1457de..6c3ebdc 100644
> --- a/cmds-filesystem.c
> +++ b/cmds-filesystem.c
(snipped)
> +static int cmd_disk_free(int argc, char **argv)
> +{
> +
> + int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
> + int i, more_than_one=0;
> +
> + if (check_argc_min(argc, 2))
> + usage(cmd_disk_free_usage);
> +
> + for(i=1; i< argc ; i++){
> + if(!strcmp(argv[i],"-d"))
> + flags &= ~DF_SHOW_DETAIL;
> + else if(!strcmp(argv[i],"-s"))
> + flags &= ~DF_SHOW_SUMMARY;
> + else if(!strcmp(argv[i],"-k"))
> + flags &= ~DF_HUMAN_UNIT;
> + else{
> + int r;
> + if(more_than_one)
> + printf("\n");
> + r = _cmd_disk_free(argv[i], flags);
> + if( r ) return r;
> + more_than_one=1;
> + }
Is there any reason getopt(), or better yet, getopt_long() won't work?
> +
> + }
> +
> + return 0;
> +}
> +
> +
> +
> static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
> {
> char uuidbuf[37];
> @@ -529,7 +695,7 @@ static int cmd_label(int argc, char **argv)
>
> const struct cmd_group filesystem_cmd_group = {
> filesystem_cmd_group_usage, NULL, {
> - { "df", cmd_df, cmd_df_usage, NULL, 0 },
> + { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
If this command is going to replace df, you should change the function
name back to cmd_df.
Thanks,
Ilya
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 15:02 ` Ilya Dryomov
@ 2012-10-03 16:34 ` Goffredo Baroncelli
2012-10-03 17:20 ` Ilya Dryomov
2012-10-03 17:09 ` Goffredo Baroncelli
1 sibling, 1 reply; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 16:34 UTC (permalink / raw)
To: Ilya Dryomov
Cc: Chris Mason, linux-btrfs@vger.kernel.org, Goffredo Baroncelli
On 10/03/2012 05:02 PM, Ilya Dryomov wrote:
> On Wed, Oct 03, 2012 at 01:43:15PM +0200, Goffredo Baroncelli wrote:
>> The command btrfs filesystem df is used to query the
>> status of the chunks, how many space on the disk(s) are used by
>> the chunks, how many space are available in the chunks, and an
>> estimation of the free space of the filesystem.
>> ---
>> cmds-filesystem.c | 264 +++++++++++++++++++++++++++++++++++++++++++----------
>> 1 file changed, 215 insertions(+), 49 deletions(-)
>>
>> diff --git a/cmds-filesystem.c b/cmds-filesystem.c
>> index b1457de..6c3ebdc 100644
>> --- a/cmds-filesystem.c
>> +++ b/cmds-filesystem.c
>
> (snipped)
>
>> +static int cmd_disk_free(int argc, char **argv)
>> +{
>> +
>> + int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
>> + int i, more_than_one=0;
>> +
>> + if (check_argc_min(argc, 2))
>> + usage(cmd_disk_free_usage);
>> +
>> + for(i=1; i< argc ; i++){
>> + if(!strcmp(argv[i],"-d"))
>> + flags&= ~DF_SHOW_DETAIL;
>> + else if(!strcmp(argv[i],"-s"))
>> + flags&= ~DF_SHOW_SUMMARY;
>> + else if(!strcmp(argv[i],"-k"))
>> + flags&= ~DF_HUMAN_UNIT;
>> + else{
>> + int r;
>> + if(more_than_one)
>> + printf("\n");
>> + r = _cmd_disk_free(argv[i], flags);
>> + if( r ) return r;
>> + more_than_one=1;
>> + }
>
> Is there any reason getopt(), or better yet, getopt_long() won't work?
In the beginning there were also the switches +d, +s, +k, then I dropped
them. So I leaved this style. The code is simple enough to not justify a
change.
>
>> +
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +
>> +
>> static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
>> {
>> char uuidbuf[37];
>> @@ -529,7 +695,7 @@ static int cmd_label(int argc, char **argv)
>>
>> const struct cmd_group filesystem_cmd_group = {
>> filesystem_cmd_group_usage, NULL, {
>> - { "df", cmd_df, cmd_df_usage, NULL, 0 },
>> + { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
>
> If this command is going to replace df, you should change the function
> name back to cmd_df.
I was never convinced to use 'df'. At the beginning when I wrote the
first parser of btrfs, was suggested (not by me) to use "long" command
and allow the parser to match a contracted command until there was any
ambiguity. I suggested to use disk-free, but everybody were confortable
with df.. so I leaved it as "official name". But I prefer for the
internal code a more verbose name.
>
> Thanks,
Thanks you for your review.
>
> Ilya
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 15:02 ` Ilya Dryomov
2012-10-03 16:34 ` Goffredo Baroncelli
@ 2012-10-03 17:09 ` Goffredo Baroncelli
1 sibling, 0 replies; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 17:09 UTC (permalink / raw)
To: Ilya Dryomov; +Cc: linux-btrfs@vger.kernel.org
On 10/03/2012 05:02 PM, Ilya Dryomov wrote:
>> +static int cmd_disk_free(int argc, char **argv)
>> > +{
>> > +
>> > + int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
>> > + int i, more_than_one=0;
>> > +
>> > + if (check_argc_min(argc, 2))
>> > + usage(cmd_disk_free_usage);
>> > +
>> > + for(i=1; i< argc ; i++){
>> > + if(!strcmp(argv[i],"-d"))
>> > + flags&= ~DF_SHOW_DETAIL;
>> > + else if(!strcmp(argv[i],"-s"))
>> > + flags&= ~DF_SHOW_SUMMARY;
>> > + else if(!strcmp(argv[i],"-k"))
>> > + flags&= ~DF_HUMAN_UNIT;
>> > + else{
>> > + int r;
>> > + if(more_than_one)
>> > + printf("\n");
>> > + r = _cmd_disk_free(argv[i], flags);
>> > + if( r ) return r;
>> > + more_than_one=1;
>> > + }
> Is there any reason getopt(), or better yet, getopt_long() won't work?
I re-changed idea: I will use getopt()
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 16:34 ` Goffredo Baroncelli
@ 2012-10-03 17:20 ` Ilya Dryomov
2012-10-03 17:38 ` Goffredo Baroncelli
0 siblings, 1 reply; 19+ messages in thread
From: Ilya Dryomov @ 2012-10-03 17:20 UTC (permalink / raw)
To: Goffredo Baroncelli
Cc: Chris Mason, linux-btrfs@vger.kernel.org, Goffredo Baroncelli
On Wed, Oct 03, 2012 at 06:34:38PM +0200, Goffredo Baroncelli wrote:
> On 10/03/2012 05:02 PM, Ilya Dryomov wrote:
> >On Wed, Oct 03, 2012 at 01:43:15PM +0200, Goffredo Baroncelli wrote:
> >>The command btrfs filesystem df is used to query the
> >>status of the chunks, how many space on the disk(s) are used by
> >>the chunks, how many space are available in the chunks, and an
> >>estimation of the free space of the filesystem.
> >>---
> >> cmds-filesystem.c | 264 +++++++++++++++++++++++++++++++++++++++++++----------
> >> 1 file changed, 215 insertions(+), 49 deletions(-)
> >>
> >>diff --git a/cmds-filesystem.c b/cmds-filesystem.c
> >>index b1457de..6c3ebdc 100644
> >>--- a/cmds-filesystem.c
> >>+++ b/cmds-filesystem.c
> >
> >(snipped)
> >
> >>+static int cmd_disk_free(int argc, char **argv)
> >>+{
> >>+
> >>+ int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
> >>+ int i, more_than_one=0;
> >>+
> >>+ if (check_argc_min(argc, 2))
> >>+ usage(cmd_disk_free_usage);
> >>+
> >>+ for(i=1; i< argc ; i++){
> >>+ if(!strcmp(argv[i],"-d"))
> >>+ flags&= ~DF_SHOW_DETAIL;
> >>+ else if(!strcmp(argv[i],"-s"))
> >>+ flags&= ~DF_SHOW_SUMMARY;
> >>+ else if(!strcmp(argv[i],"-k"))
> >>+ flags&= ~DF_HUMAN_UNIT;
> >>+ else{
> >>+ int r;
> >>+ if(more_than_one)
> >>+ printf("\n");
> >>+ r = _cmd_disk_free(argv[i], flags);
> >>+ if( r ) return r;
> >>+ more_than_one=1;
> >>+ }
> >
> >Is there any reason getopt(), or better yet, getopt_long() won't work?
>
> In the beginning there were also the switches +d, +s, +k, then I
> dropped them. So I leaved this style. The code is simple enough to
> not justify a change.
It's not a matter of style. It is generally expected that several short
options can be specified after one dash. Further, if somebody else were
to add an option with an optional parameter, they'd have to spend time
rewriting your stuff.
In addition to that, other parts of progs already use getopt() in
similar situations. One example would be the scrub subgroup: scrub
commands are equally simple (a few short switches, no parameters), and
yet getopt() is used.
>
> >
> >>+
> >>+ }
> >>+
> >>+ return 0;
> >>+}
> >>+
> >>+
> >>+
> >> static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
> >> {
> >> char uuidbuf[37];
> >>@@ -529,7 +695,7 @@ static int cmd_label(int argc, char **argv)
> >>
> >> const struct cmd_group filesystem_cmd_group = {
> >> filesystem_cmd_group_usage, NULL, {
> >>- { "df", cmd_df, cmd_df_usage, NULL, 0 },
> >>+ { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
> >
> >If this command is going to replace df, you should change the function
> >name back to cmd_df.
>
> I was never convinced to use 'df'. At the beginning when I wrote the
> first parser of btrfs, was suggested (not by me) to use "long"
> command and allow the parser to match a contracted command until
> there was any ambiguity. I suggested to use disk-free, but everybody
> were confortable with df.. so I leaved it as "official name". But I
> prefer for the internal code a more verbose name.
Well, all your patch is doing is extending the functionality of an
existing command. The "official name" for that command is "df", and you
are not changing its name. Why change the name of an existing function?
Thanks,
Ilya
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH][BTRFS-PROGS][V2] btrfs filesystem df
@ 2012-10-03 17:22 Goffredo Baroncelli
2012-10-03 17:22 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
` (2 more replies)
0 siblings, 3 replies; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 17:22 UTC (permalink / raw)
To: linux-btrfs; +Cc: Hugo Mills, Roman Mamedov, Sébastien Maury, Ilya Dryomov
>From the man page (see 2nd patch):
[...]
The command btrfs filesystem df is used to query the
status of the chunks, how many space on the disk(s) are used by
the chunks, how many space are available in the chunks, and an
estimation of the free space of the filesystem.
[...]
$ ./btrfs filesystem df --help
usage: btrfs filesystem disk-usage [-d|-s][-k] <path> [<path>..]
Show space usage information for a mount point(s).
-k Set KB (1024 bytes) as unit
-s Show the summary section only
-d Show the detail section only
$ ./btrfs filesystem df /
Path: /
Summary:
Disk_size: 72.57GB
Disk_allocated: 25.10GB
Disk_unallocated: 47.48GB
Logical_size: 23.06GB
Used: 11.01GB
Free_(Estimated): 55.66GB (Max: 59.52GB, Min: 35.78GB)
Data_to_disk_ratio: 92 %
Details:
Chunk-type Mode Chunk_size Logical_size Used
Data Single 21.01GB 21.01GB 10.34GB
System DUP 80.00MB 40.00MB 4.00KB
System Single 4.00MB 4.00MB 0.00
Metadata DUP 4.00GB 2.00GB 686.93MB
Metadata Single 8.00MB 8.00MB 0.00
Where:
Disk_size -> sum of sizes of teh disks
Disk_allocated -> sum of chunk sizes
Disk_unallocated -> Disk_size - Disk_allocated
Logical_size -> sum of logical area sizes
Used -> logical area used
Free_(Estimated) -> on the basis of allocated
chunk, an estrapolation of
the free space
Data_to_disk_ratio -> ration between the space occuped
by a chunk and the real space
available ( due to duplication
and/or RAID level)
Chunk_type -> kind of chunk
Mode -> allocation policy of a chunk
Chunk_size -> area of disk(s) occuped by the
chunk (see it as raw space used)
Logical_size -> logical area size of the chunk
Used -> portion of the logical area
used by the filesystem
You can pull this change from
http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git
branch
disk_free
Please pull.
BR
Goffredo Baroncelli
Changelog:
V0->V1 Change the name of command from disk-usage to df
V1->V2 Uses getopt(); replace "-" with "_"; change the behaviour
of the switches '-s' and '-d'.
Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 17:22 [PATCH][BTRFS-PROGS][V2] btrfs filesystem df Goffredo Baroncelli
@ 2012-10-03 17:22 ` Goffredo Baroncelli
2012-10-03 20:34 ` Ilya Dryomov
2012-10-03 17:22 ` [PATCH 2/2] Update help page Goffredo Baroncelli
2012-10-03 19:32 ` [PATCH][BTRFS-PROGS][V2] btrfs filesystem df David Sterba
2 siblings, 1 reply; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 17:22 UTC (permalink / raw)
To: linux-btrfs
Cc: Hugo Mills, Roman Mamedov, Sébastien Maury, Ilya Dryomov,
Goffredo Baroncelli
From: Goffredo Baroncelli <kreijack@inwind.it>
The command btrfs filesystem df is used to query the
status of the chunks, how many space on the disk(s) are used by
the chunks, how many space are available in the chunks, and an
estimation of the free space of the filesystem.
---
cmds-filesystem.c | 284 ++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 235 insertions(+), 49 deletions(-)
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index b1457de..2701904 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <uuid/uuid.h>
#include <ctype.h>
+#include <sys/vfs.h>
#include "kerncompat.h"
#include "ctree.h"
@@ -39,25 +40,55 @@ static const char * const filesystem_cmd_group_usage[] = {
NULL
};
-static const char * const cmd_df_usage[] = {
- "btrfs filesystem df <path>",
- "Show space usage information for a mount point",
- NULL
-};
+static u64 disk_size( char *path){
+ struct statfs sfs;
+
+ if( statfs(path, &sfs) < 0 )
+ return 0;
+ else
+ return sfs.f_bsize * sfs.f_blocks;
+
+}
+
+static void print_string(char *s, int w)
+{
+ int i;
+
+ printf("%s", s);
+ for( i = strlen(s) ; i < w ; i++ )
+ putchar(' ');
+}
+
+#define DF_SHOW_SUMMARY (1<<1)
+#define DF_SHOW_DETAIL (1<<2)
+#define DF_HUMAN_UNIT (1<<3)
+
+static void pretty_sizes_r(u64 size, int w, int mode)
+{
+ if( mode & DF_HUMAN_UNIT ){
+ char *s = pretty_sizes(size);
+ printf("%*s", w, s);
+ free(s);
+ } else {
+ printf("%*llu", w, size/1024);
+
+ }
+}
-static int cmd_df(int argc, char **argv)
+static int _cmd_disk_free(char *path, int mode)
{
struct btrfs_ioctl_space_args *sargs;
u64 count = 0, i;
int ret;
int fd;
- int e;
- char *path;
-
- if (check_argc_exact(argc, 2))
- usage(cmd_df_usage);
-
- path = argv[1];
+ int e, width;
+ u64 total_disk; /* filesystem size == sum of
+ disks sizes */
+ u64 total_chunks; /* sum of chunks sizes on disk(s) */
+ u64 total_used; /* logical space used */
+ u64 total_free; /* logical space un-used */
+ double K;
+
fd = open_file_or_dir(path);
if (fd < 0) {
@@ -103,56 +134,211 @@ static int cmd_df(int argc, char **argv)
return ret;
}
- for (i = 0; i < sargs->total_spaces; i++) {
- char description[80];
- char *total_bytes;
- char *used_bytes;
- int written = 0;
- u64 flags = sargs->spaces[i].flags;
+ total_disk = disk_size(path);
+ e = errno;
+ if( total_disk == 0 ){
+ fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
+ path, strerror(e));
+ close(fd);
+ free(sargs);
+ return 19;
+ }
+
+ total_chunks = total_used = total_free = 0;
- memset(description, 0, 80);
+ for (i = 0; i < sargs->total_spaces; i++) {
+ int ratio=1;
+ u64 allocated;
- if (flags & BTRFS_BLOCK_GROUP_DATA) {
- if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 14, "%s",
- "Data+Metadata");
- written += 13;
- } else {
- snprintf(description, 5, "%s", "Data");
- written += 4;
- }
- } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
- snprintf(description, 7, "%s", "System");
- written += 6;
- } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 9, "%s", "Metadata");
- written += 8;
- }
+ u64 flags = sargs->spaces[i].flags;
if (flags & BTRFS_BLOCK_GROUP_RAID0) {
- snprintf(description+written, 8, "%s", ", RAID0");
- written += 7;
+ ratio=1;
} else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
- snprintf(description+written, 8, "%s", ", RAID1");
- written += 7;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_DUP) {
- snprintf(description+written, 6, "%s", ", DUP");
- written += 5;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
- snprintf(description+written, 9, "%s", ", RAID10");
- written += 8;
+ ratio=2;
+ } else {
+ ratio=1;
}
- total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
- used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
- printf("%s: total=%s, used=%s\n", description, total_bytes,
- used_bytes);
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ total_chunks += allocated;
+ total_used += sargs->spaces[i].used_bytes;
+ total_free += (sargs->spaces[i].total_bytes -
+ sargs->spaces[i].used_bytes);
+
+ }
+ K = ((double)total_used + (double)total_free) /
+ (double)total_chunks;
+
+ if( mode & DF_HUMAN_UNIT )
+ width = 12;
+ else
+ width = 18;
+
+ printf("Path: %s\n", path);
+ if( mode & DF_SHOW_SUMMARY ){
+ printf("Summary:\n");
+ printf(" Disk_size:\t\t");
+ pretty_sizes_r(total_disk, width, mode);
+ printf("\n Disk_allocated:\t");
+ pretty_sizes_r(total_chunks, width, mode);
+ printf("\n Disk_unallocated:\t");
+ pretty_sizes_r(total_disk-total_chunks, width, mode);
+ printf("\n Logical_size:\t\t");
+ pretty_sizes_r(total_used+total_free, width, mode);
+ printf("\n Used:\t\t\t");
+ pretty_sizes_r(total_used, width, mode);
+ printf("\n Free_(Estimated):\t");
+ pretty_sizes_r((u64)(K*total_disk-total_used), width, mode);
+ printf("\t(Max: ");
+ pretty_sizes_r(total_disk-total_chunks+total_free,
+ 0, mode );
+ printf(", Min: ");
+ pretty_sizes_r((total_disk-total_chunks)/2+total_free,
+ 0, mode );
+ printf(")\n Data_to_disk_ratio:\t%*.0f %%\n",
+ width-2, K*100);
+ }
+
+ if( ( mode & DF_SHOW_DETAIL ) && ( mode & DF_SHOW_SUMMARY ) )
+ printf("\n");
+
+ if( mode & DF_SHOW_DETAIL ){
+ /* Remember: the terminals have maximum 80 columns
+ do not believe to who says otherwise */
+ printf("Details:\n");
+ printf(" %-12s%-8s%*s%*s%*s\n",
+ "Chunk_type",
+ "Mode",
+ width, "Chunk_size",
+ width+1, "Logical_size",
+ width, "Used"
+ );
+
+ for (i = 0; i < sargs->total_spaces; i++) {
+ char *description="";
+ int ratio=1;
+ char *r_mode;
+ u64 allocated;
+
+ u64 flags = sargs->spaces[i].flags;
+
+ if (flags & BTRFS_BLOCK_GROUP_DATA) {
+ if (flags & BTRFS_BLOCK_GROUP_METADATA){
+ description = "Data+M.data";
+ } else {
+ description = "Data";
+ }
+ } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+ description = "System";
+ } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+ description = "Metadata";
+ }
+
+ if (flags & BTRFS_BLOCK_GROUP_RAID0) {
+ r_mode = "RAID0";
+ ratio=1;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
+ r_mode = "RAID1";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
+ r_mode = "DUP";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
+ r_mode = "RAID10";
+ ratio=2;
+ } else {
+ r_mode = "Single";
+ ratio=1;
+ }
+
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ printf(" ");
+ print_string(description,12);
+ print_string(r_mode, 8);
+ pretty_sizes_r(allocated, width, mode);
+ pretty_sizes_r(sargs->spaces[i].total_bytes ,
+ width+1, mode);
+
+ pretty_sizes_r(sargs->spaces[i].used_bytes,
+ width, mode);
+ printf("\n");
+
+ }
}
free(sargs);
return 0;
}
+static const char * const cmd_disk_free_usage[] = {
+ "btrfs filesystem df [-d|-s][-k] <path> [<path>..]",
+ "Show space usage information for a mount point(s).",
+ "",
+ "-k\tSet KB (1024 bytes) as unit",
+ "-s\tShow the summary section only",
+ "-d\tShow the detail section only",
+ NULL
+};
+
+static int cmd_disk_free(int argc, char **argv)
+{
+
+ int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
+ int i, more_than_one=0;
+
+ optind = 1;
+ while(1){
+ char c = getopt(argc, argv, "dsk");
+ if(c<0)
+ break;
+ switch(c){
+ case 'd':
+ flags &= ~DF_SHOW_SUMMARY;
+ break;
+ case 's':
+ flags &= ~DF_SHOW_DETAIL;
+ break;
+ case 'k':
+ flags &= ~DF_HUMAN_UNIT;
+ break;
+ default:
+ usage(cmd_disk_free_usage);
+ }
+ }
+
+ if( !(flags & (DF_SHOW_SUMMARY|DF_SHOW_DETAIL)) ){
+ fprintf(stderr, "btrfs filesystem df: it is not possible to specify -s AND -d\n");
+ usage(cmd_disk_free_usage);
+ return 20;
+ }
+
+ if (check_argc_min(argc - optind, 1)){
+ usage(cmd_disk_free_usage);
+ return 21;
+ }
+
+ for(i=optind; i< argc ; i++){
+ int r;
+ if(more_than_one)
+ printf("\n");
+ r = _cmd_disk_free(argv[i], flags);
+ if( r ) return r;
+ more_than_one=1;
+
+ }
+
+ return 0;
+}
+
+
+
static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
{
char uuidbuf[37];
@@ -529,7 +715,7 @@ static int cmd_label(int argc, char **argv)
const struct cmd_group filesystem_cmd_group = {
filesystem_cmd_group_usage, NULL, {
- { "df", cmd_df, cmd_df_usage, NULL, 0 },
+ { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
{ "show", cmd_show, cmd_show_usage, NULL, 0 },
{ "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
{ "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
--
1.7.10.4
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH 2/2] Update help page
2012-10-03 17:22 [PATCH][BTRFS-PROGS][V2] btrfs filesystem df Goffredo Baroncelli
2012-10-03 17:22 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
@ 2012-10-03 17:22 ` Goffredo Baroncelli
2012-10-03 19:32 ` [PATCH][BTRFS-PROGS][V2] btrfs filesystem df David Sterba
2 siblings, 0 replies; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 17:22 UTC (permalink / raw)
To: linux-btrfs
Cc: Hugo Mills, Roman Mamedov, Sébastien Maury, Ilya Dryomov,
Goffredo Baroncelli
From: Goffredo Baroncelli <kreijack@inwind.it>
---
man/btrfs.8.in | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 94 insertions(+)
diff --git a/man/btrfs.8.in b/man/btrfs.8.in
index 4b0a9f9..0db588f 100644
--- a/man/btrfs.8.in
+++ b/man/btrfs.8.in
@@ -27,6 +27,8 @@ btrfs \- control a btrfs filesystem
.PP
\fBbtrfs\fP \fBfilesystem label\fP\fI <dev> [newlabel]\fP
.PP
+\fBbtrfs\fP \fBfilesystem df\fP\fI [-s|-d][-k] \fIpath [path..]\fR\fP
+.PP
\fBbtrfs\fP \fBsubvolume find-new\fP\fI <subvolume> <last_gen>\fP
.PP
\fBbtrfs\fP \fBfilesystem balance\fP\fI <path> \fP
@@ -214,6 +216,98 @@ NOTE: Currently there are the following limitations:
- the filesystem should not have more than one device.
.TP
+\fBfilesystem df\fP [-s|-d][-k] \fIpath [path..]\fR
+
+Show space usage information for a mount point.
+
+\fIOptions\fP
+
+\fB-k\fP Set KB (1024 bytes) as unit
+
+\fB-s\fP Show the summary section only
+
+\fB-d\fP Show the detail section only
+
+\fIUsage information\fP
+
+.\"
+.\" this section is extract from
+.\" http://en.wikipedia.org/wiki/Btrfs#Chunk_and_device_trees
+The disk(s) of a btrfs filesystem are divided into chunks of 256 MB or more.
+Chunks may be mirrored or striped across multiple devices, depending by
+the allocation policy.
+The mirroring/striping arrangement is transparent to the rest of the
+file system, which simply sees the single, logical address space that
+chunks are mapped into.
+Chunks are allocated on demand. In the default allocation policy
+the data chunks are not duplicated and the metadata chunks
+are duplicated. This default can be changed during the filesystem
+creation, and in general the chunks allocation policy may change
+during the filesystem life.
+
+A chunk DUPlicated or with a RAID1/RAID10 level
+requires a space two time greater than the logical one. Different RAID levels
+have a different ratio disk-usage / logical space offered.
+
+Because some files (the small ones) are stored in the
+metadata chunks the computation of the \fIfree space\fP and \fIused space\fP
+is complex: depending by the file size different allocation policies are used.
+
+The command \fBbtrfs filesystem df\fP is used to query the status
+of the chunks, how many space on the disk(s) are used by the chunks,
+how many space are available in the chunks, and an estimation of the free
+space of the filesystem.
+The output of the command \fBbtrfs filesystem df\fP shows:
+
+.RS
+.IP Disk\ size
+the total size of the disks which compose the filesystem.
+
+.IP Disk\ allocated
+the size of the area of the disks used by the chunks.
+
+.IP Disk\ unallocated
+the size of the area of the disks which is free (i.e.
+the differences of the values above).
+
+.IP Logical\ size
+the available logical space of chunk.
+
+.IP Used
+the portion of the logical space used by the file and metadata.
+
+.IP Free\ (estimated)
+the estimated free space available. The evaluation
+cannot be rigorous because it depends by the allocation policy (DUP,Single,
+RAID1...) of the metadata and data chunks. If every chunks is stored as
+"Single" the sum of the \fBfree (estimated)\fP space and the \fBused\fP
+space is equal to the \fBdisk size\fP.
+Otherwise if all the chunk are mirrored (raid1 or raid10) or duplicated
+the sum of the \fBfree (estimated)\fP space and the \fBused\fP space is
+half of the \fBdisk size\fP. Normally the \fBfree (estimated)\fP is between
+these two limits.
+
+.IP Data\ to\ disk\ ratio
+the ratio betwen the \fBlogical size\fP and the \fBdisk allocated\fP.
+
+.IP Mode
+the kind of allocation policy used by the chunk (e.g. DUPlicated,
+RAID1, RAID10, Single....)
+
+.RE
+.RS
+\fINOTE\fP
+
+When a chunk is allocated, its disk-area is used and its allocation
+policy is fixed.
+A rebalance operation could rearrange the chunks, moving data in the chunks
+and resizing the allocated chunks. This causes the change of all the values
+discussed above, with the exception of the \fBused\fP and
+\fBdisk size\fP values.
+
+.RE
+.TP
+
\fBfilesystem show\fR [--all-devices|<uuid>|<label>]\fR
Show the btrfs filesystem with some additional info. If no \fIUUID\fP or
\fIlabel\fP is passed, \fBbtrfs\fR show info of all the btrfs filesystem.
--
1.7.10.4
^ permalink raw reply related [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 17:20 ` Ilya Dryomov
@ 2012-10-03 17:38 ` Goffredo Baroncelli
0 siblings, 0 replies; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 17:38 UTC (permalink / raw)
To: Ilya Dryomov
Cc: Chris Mason, linux-btrfs@vger.kernel.org, Goffredo Baroncelli
Hi,
On 10/03/2012 07:20 PM, Ilya Dryomov wrote:
>>>> filesystem_cmd_group_usage, NULL, {
>>>> > >>- { "df", cmd_df, cmd_df_usage, NULL, 0 },
>>>> > >>+ { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
>>> > >
>>> > >If this command is going to replace df, you should change the function
>>> > >name back to cmd_df.
>> >
>> > I was never convinced to use 'df'. At the beginning when I wrote the
>> > first parser of btrfs, was suggested (not by me) to use "long"
>> > command and allow the parser to match a contracted command until
>> > there was any ambiguity. I suggested to use disk-free, but everybody
>> > were confortable with df.. so I leaved it as "official name". But I
>> > prefer for the internal code a more verbose name.
> Well, all your patch is doing is extending the functionality of an
> existing command. The "official name" for that command is "df", and you
> are not changing its name.
I would like change the name of the command from df to disk-free.
Unfortunately (for me) Chris ask to leaved the old name...
> Why change the name of an existing function?
Strictly speaking, I never changed the name of the function. At the
beginning I added another command (who was named disk-usage), then Chris
asked to replace df with the new one. So I removed the old one.
Seeing the patch it seems that I changed the name, but the truth is that
I added a new one then removed the old one. Due to how the patch is
generated it seems another thing.
But this doesn't change too much.
The point is that the function name doesn't match the command name. It
is not the single case: look at the function cmd_send_start()
(associated to the send command), cmd_defrag() )associated to the
command defragment....)
BR
G.Baroncelli
>
> Thanks,
>
> Ilya
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH][BTRFS-PROGS][V2] btrfs filesystem df
2012-10-03 17:22 [PATCH][BTRFS-PROGS][V2] btrfs filesystem df Goffredo Baroncelli
2012-10-03 17:22 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
2012-10-03 17:22 ` [PATCH 2/2] Update help page Goffredo Baroncelli
@ 2012-10-03 19:32 ` David Sterba
2012-10-03 19:38 ` Goffredo Baroncelli
2 siblings, 1 reply; 19+ messages in thread
From: David Sterba @ 2012-10-03 19:32 UTC (permalink / raw)
To: Goffredo Baroncelli
Cc: linux-btrfs, Hugo Mills, Roman Mamedov, Sébastien Maury,
Ilya Dryomov
On Wed, Oct 03, 2012 at 07:22:30PM +0200, Goffredo Baroncelli wrote:
> $ ./btrfs filesystem df /
> Path: /
> Summary:
> Disk_size: 72.57GB
> Disk_allocated: 25.10GB
> Disk_unallocated: 47.48GB
> Logical_size: 23.06GB
> Used: 11.01GB
> Free_(Estimated): 55.66GB (Max: 59.52GB, Min: 35.78GB)
> Data_to_disk_ratio: 92 %
>
> Details:
> Chunk-type Mode Chunk_size Logical_size Used
> Data Single 21.01GB 21.01GB 10.34GB
> System DUP 80.00MB 40.00MB 4.00KB
> System Single 4.00MB 4.00MB 0.00
> Metadata DUP 4.00GB 2.00GB 686.93MB
> Metadata Single 8.00MB 8.00MB 0.00
Please snip the following help text from basic output, eg. when I'm
running the 'df' command before/after some actions from scripts, this
is unnecessary.
> Where:
> Disk_size -> sum of sizes of teh disks
> Disk_allocated -> sum of chunk sizes
> Disk_unallocated -> Disk_size - Disk_allocated
> Logical_size -> sum of logical area sizes
> Used -> logical area used
> Free_(Estimated) -> on the basis of allocated
> chunk, an estrapolation of
> the free space
> Data_to_disk_ratio -> ration between the space occuped
> by a chunk and the real space
> available ( due to duplication
> and/or RAID level)
> Chunk_type -> kind of chunk
> Mode -> allocation policy of a chunk
> Chunk_size -> area of disk(s) occuped by the
> chunk (see it as raw space used)
> Logical_size -> logical area size of the chunk
> Used -> portion of the logical area
> used by the filesystem
david
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH][BTRFS-PROGS][V2] btrfs filesystem df
2012-10-03 19:32 ` [PATCH][BTRFS-PROGS][V2] btrfs filesystem df David Sterba
@ 2012-10-03 19:38 ` Goffredo Baroncelli
2012-10-04 7:15 ` David Sterba
0 siblings, 1 reply; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 19:38 UTC (permalink / raw)
To: linux-btrfs, Hugo Mills, Roman Mamedov, Sébastien Maury,
Ilya Dryomov
On 10/03/2012 09:32 PM, David Sterba wrote:
> On Wed, Oct 03, 2012 at 07:22:30PM +0200, Goffredo Baroncelli wrote:
>> $ ./btrfs filesystem df /
>> Path: /
>> Summary:
>> Disk_size: 72.57GB
>> Disk_allocated: 25.10GB
>> Disk_unallocated: 47.48GB
>> Logical_size: 23.06GB
>> Used: 11.01GB
>> Free_(Estimated): 55.66GB (Max: 59.52GB, Min: 35.78GB)
>> Data_to_disk_ratio: 92 %
>>
>> Details:
>> Chunk-type Mode Chunk_size Logical_size Used
>> Data Single 21.01GB 21.01GB 10.34GB
>> System DUP 80.00MB 40.00MB 4.00KB
>> System Single 4.00MB 4.00MB 0.00
>> Metadata DUP 4.00GB 2.00GB 686.93MB
>> Metadata Single 8.00MB 8.00MB 0.00
>
> Please snip the following help text from basic output, eg. when I'm
> running the 'df' command before/after some actions from scripts, this
> is unnecessary.
Hi David,
the output of the program ends with the "Metadata Single....". What you
called "Help text" is a short description in the email.
BR
Goffredo
>
>> Where:
>> Disk_size -> sum of sizes of teh disks
>> Disk_allocated -> sum of chunk sizes
>> Disk_unallocated -> Disk_size - Disk_allocated
>> Logical_size -> sum of logical area sizes
>> Used -> logical area used
>> Free_(Estimated) -> on the basis of allocated
>> chunk, an estrapolation of
>> the free space
>> Data_to_disk_ratio -> ration between the space occuped
>> by a chunk and the real space
>> available ( due to duplication
>> and/or RAID level)
>> Chunk_type -> kind of chunk
>> Mode -> allocation policy of a chunk
>> Chunk_size -> area of disk(s) occuped by the
>> chunk (see it as raw space used)
>> Logical_size -> logical area size of the chunk
>> Used -> portion of the logical area
>> used by the filesystem
>
>
> david
>
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 17:22 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
@ 2012-10-03 20:34 ` Ilya Dryomov
2012-10-03 20:38 ` Hugo Mills
0 siblings, 1 reply; 19+ messages in thread
From: Ilya Dryomov @ 2012-10-03 20:34 UTC (permalink / raw)
To: Goffredo Baroncelli
Cc: linux-btrfs, Hugo Mills, Roman Mamedov, Sébastien Maury,
Goffredo Baroncelli
On Wed, Oct 03, 2012 at 07:22:31PM +0200, Goffredo Baroncelli wrote:
> From: Goffredo Baroncelli <kreijack@inwind.it>
>
> The command btrfs filesystem df is used to query the
> status of the chunks, how many space on the disk(s) are used by
> the chunks, how many space are available in the chunks, and an
> estimation of the free space of the filesystem.
> ---
> cmds-filesystem.c | 284 ++++++++++++++++++++++++++++++++++++++++++++---------
> 1 file changed, 235 insertions(+), 49 deletions(-)
>
> diff --git a/cmds-filesystem.c b/cmds-filesystem.c
> index b1457de..2701904 100644
> --- a/cmds-filesystem.c
> +++ b/cmds-filesystem.c
> @@ -22,6 +22,7 @@
> #include <errno.h>
> #include <uuid/uuid.h>
> #include <ctype.h>
> +#include <sys/vfs.h>
>
> #include "kerncompat.h"
> #include "ctree.h"
> @@ -39,25 +40,55 @@ static const char * const filesystem_cmd_group_usage[] = {
> NULL
> };
>
> -static const char * const cmd_df_usage[] = {
> - "btrfs filesystem df <path>",
> - "Show space usage information for a mount point",
> - NULL
> -};
> +static u64 disk_size( char *path){
> + struct statfs sfs;
> +
> + if( statfs(path, &sfs) < 0 )
> + return 0;
> + else
> + return sfs.f_bsize * sfs.f_blocks;
> +
> +}
> +
> +static void print_string(char *s, int w)
> +{
> + int i;
> +
> + printf("%s", s);
> + for( i = strlen(s) ; i < w ; i++ )
> + putchar(' ');
> +}
> +
> +#define DF_SHOW_SUMMARY (1<<1)
> +#define DF_SHOW_DETAIL (1<<2)
> +#define DF_HUMAN_UNIT (1<<3)
> +
> +static void pretty_sizes_r(u64 size, int w, int mode)
> +{
> + if( mode & DF_HUMAN_UNIT ){
> + char *s = pretty_sizes(size);
> + printf("%*s", w, s);
> + free(s);
> + } else {
> + printf("%*llu", w, size/1024);
> +
> + }
> +}
>
> -static int cmd_df(int argc, char **argv)
> +static int _cmd_disk_free(char *path, int mode)
> {
> struct btrfs_ioctl_space_args *sargs;
> u64 count = 0, i;
> int ret;
> int fd;
> - int e;
> - char *path;
> -
> - if (check_argc_exact(argc, 2))
> - usage(cmd_df_usage);
> -
> - path = argv[1];
> + int e, width;
> + u64 total_disk; /* filesystem size == sum of
> + disks sizes */
> + u64 total_chunks; /* sum of chunks sizes on disk(s) */
> + u64 total_used; /* logical space used */
> + u64 total_free; /* logical space un-used */
> + double K;
> +
>
> fd = open_file_or_dir(path);
> if (fd < 0) {
> @@ -103,56 +134,211 @@ static int cmd_df(int argc, char **argv)
> return ret;
> }
>
> - for (i = 0; i < sargs->total_spaces; i++) {
> - char description[80];
> - char *total_bytes;
> - char *used_bytes;
> - int written = 0;
> - u64 flags = sargs->spaces[i].flags;
> + total_disk = disk_size(path);
> + e = errno;
> + if( total_disk == 0 ){
> + fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
> + path, strerror(e));
> + close(fd);
> + free(sargs);
> + return 19;
> + }
> +
> + total_chunks = total_used = total_free = 0;
>
> - memset(description, 0, 80);
> + for (i = 0; i < sargs->total_spaces; i++) {
> + int ratio=1;
> + u64 allocated;
>
> - if (flags & BTRFS_BLOCK_GROUP_DATA) {
> - if (flags & BTRFS_BLOCK_GROUP_METADATA) {
> - snprintf(description, 14, "%s",
> - "Data+Metadata");
> - written += 13;
> - } else {
> - snprintf(description, 5, "%s", "Data");
> - written += 4;
> - }
> - } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
> - snprintf(description, 7, "%s", "System");
> - written += 6;
> - } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
> - snprintf(description, 9, "%s", "Metadata");
> - written += 8;
> - }
> + u64 flags = sargs->spaces[i].flags;
>
> if (flags & BTRFS_BLOCK_GROUP_RAID0) {
> - snprintf(description+written, 8, "%s", ", RAID0");
> - written += 7;
> + ratio=1;
> } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
> - snprintf(description+written, 8, "%s", ", RAID1");
> - written += 7;
> + ratio=2;
> } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
> - snprintf(description+written, 6, "%s", ", DUP");
> - written += 5;
> + ratio=2;
> } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
> - snprintf(description+written, 9, "%s", ", RAID10");
> - written += 8;
> + ratio=2;
> + } else {
> + ratio=1;
> }
>
> - total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
> - used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
> - printf("%s: total=%s, used=%s\n", description, total_bytes,
> - used_bytes);
> + allocated = sargs->spaces[i].total_bytes * ratio;
> +
> + total_chunks += allocated;
> + total_used += sargs->spaces[i].used_bytes;
> + total_free += (sargs->spaces[i].total_bytes -
> + sargs->spaces[i].used_bytes);
> +
> + }
> + K = ((double)total_used + (double)total_free) /
> + (double)total_chunks;
> +
> + if( mode & DF_HUMAN_UNIT )
> + width = 12;
> + else
> + width = 18;
> +
> + printf("Path: %s\n", path);
> + if( mode & DF_SHOW_SUMMARY ){
> + printf("Summary:\n");
> + printf(" Disk_size:\t\t");
> + pretty_sizes_r(total_disk, width, mode);
> + printf("\n Disk_allocated:\t");
> + pretty_sizes_r(total_chunks, width, mode);
> + printf("\n Disk_unallocated:\t");
> + pretty_sizes_r(total_disk-total_chunks, width, mode);
> + printf("\n Logical_size:\t\t");
> + pretty_sizes_r(total_used+total_free, width, mode);
> + printf("\n Used:\t\t\t");
> + pretty_sizes_r(total_used, width, mode);
> + printf("\n Free_(Estimated):\t");
> + pretty_sizes_r((u64)(K*total_disk-total_used), width, mode);
> + printf("\t(Max: ");
> + pretty_sizes_r(total_disk-total_chunks+total_free,
> + 0, mode );
> + printf(", Min: ");
> + pretty_sizes_r((total_disk-total_chunks)/2+total_free,
> + 0, mode );
> + printf(")\n Data_to_disk_ratio:\t%*.0f %%\n",
> + width-2, K*100);
> + }
> +
> + if( ( mode & DF_SHOW_DETAIL ) && ( mode & DF_SHOW_SUMMARY ) )
> + printf("\n");
> +
> + if( mode & DF_SHOW_DETAIL ){
> + /* Remember: the terminals have maximum 80 columns
> + do not believe to who says otherwise */
> + printf("Details:\n");
> + printf(" %-12s%-8s%*s%*s%*s\n",
> + "Chunk_type",
> + "Mode",
> + width, "Chunk_size",
> + width+1, "Logical_size",
> + width, "Used"
> + );
> +
> + for (i = 0; i < sargs->total_spaces; i++) {
> + char *description="";
> + int ratio=1;
> + char *r_mode;
> + u64 allocated;
> +
> + u64 flags = sargs->spaces[i].flags;
> +
> + if (flags & BTRFS_BLOCK_GROUP_DATA) {
> + if (flags & BTRFS_BLOCK_GROUP_METADATA){
> + description = "Data+M.data";
> + } else {
> + description = "Data";
> + }
> + } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
> + description = "System";
> + } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
> + description = "Metadata";
> + }
> +
> + if (flags & BTRFS_BLOCK_GROUP_RAID0) {
> + r_mode = "RAID0";
> + ratio=1;
> + } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
> + r_mode = "RAID1";
> + ratio=2;
> + } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
> + r_mode = "DUP";
> + ratio=2;
> + } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
> + r_mode = "RAID10";
> + ratio=2;
> + } else {
> + r_mode = "Single";
> + ratio=1;
> + }
> +
> + allocated = sargs->spaces[i].total_bytes * ratio;
> +
> + printf(" ");
> + print_string(description,12);
> + print_string(r_mode, 8);
> + pretty_sizes_r(allocated, width, mode);
> + pretty_sizes_r(sargs->spaces[i].total_bytes ,
> + width+1, mode);
> +
> + pretty_sizes_r(sargs->spaces[i].used_bytes,
> + width, mode);
> + printf("\n");
> +
> + }
> }
> free(sargs);
>
> return 0;
> }
>
> +static const char * const cmd_disk_free_usage[] = {
> + "btrfs filesystem df [-d|-s][-k] <path> [<path>..]",
> + "Show space usage information for a mount point(s).",
> + "",
> + "-k\tSet KB (1024 bytes) as unit",
> + "-s\tShow the summary section only",
> + "-d\tShow the detail section only",
> + NULL
> +};
> +
> +static int cmd_disk_free(int argc, char **argv)
> +{
> +
> + int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
> + int i, more_than_one=0;
> +
> + optind = 1;
> + while(1){
> + char c = getopt(argc, argv, "dsk");
> + if(c<0)
> + break;
> + switch(c){
> + case 'd':
> + flags &= ~DF_SHOW_SUMMARY;
> + break;
> + case 's':
> + flags &= ~DF_SHOW_DETAIL;
> + break;
> + case 'k':
> + flags &= ~DF_HUMAN_UNIT;
> + break;
> + default:
> + usage(cmd_disk_free_usage);
> + }
> + }
> +
> + if( !(flags & (DF_SHOW_SUMMARY|DF_SHOW_DETAIL)) ){
> + fprintf(stderr, "btrfs filesystem df: it is not possible to specify -s AND -d\n");
This doesn't look right at all. You are adding two switches and
specifying both of them is an error? A little too much for a command
whose job is to do some basic math and pretty-print the result.
How about displaying just the summary by default and then adding a
*single* switch (-v or whatever) for summary+details?
Thanks,
Ilya
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 20:34 ` Ilya Dryomov
@ 2012-10-03 20:38 ` Hugo Mills
2012-10-03 20:43 ` Ilya Dryomov
2012-10-03 20:53 ` Goffredo Baroncelli
0 siblings, 2 replies; 19+ messages in thread
From: Hugo Mills @ 2012-10-03 20:38 UTC (permalink / raw)
To: Ilya Dryomov
Cc: Goffredo Baroncelli, linux-btrfs, Roman Mamedov,
Sébastien Maury, Goffredo Baroncelli
[-- Attachment #1: Type: text/plain, Size: 2234 bytes --]
On Wed, Oct 03, 2012 at 11:34:00PM +0300, Ilya Dryomov wrote:
> On Wed, Oct 03, 2012 at 07:22:31PM +0200, Goffredo Baroncelli wrote:
[snip]
> > +static const char * const cmd_disk_free_usage[] = {
> > + "btrfs filesystem df [-d|-s][-k] <path> [<path>..]",
> > + "Show space usage information for a mount point(s).",
> > + "",
> > + "-k\tSet KB (1024 bytes) as unit",
> > + "-s\tShow the summary section only",
> > + "-d\tShow the detail section only",
> > + NULL
> > +};
> > +
> > +static int cmd_disk_free(int argc, char **argv)
> > +{
> > +
> > + int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
> > + int i, more_than_one=0;
> > +
> > + optind = 1;
> > + while(1){
> > + char c = getopt(argc, argv, "dsk");
> > + if(c<0)
> > + break;
> > + switch(c){
> > + case 'd':
> > + flags &= ~DF_SHOW_SUMMARY;
> > + break;
> > + case 's':
> > + flags &= ~DF_SHOW_DETAIL;
> > + break;
> > + case 'k':
> > + flags &= ~DF_HUMAN_UNIT;
> > + break;
> > + default:
> > + usage(cmd_disk_free_usage);
> > + }
> > + }
> > +
> > + if( !(flags & (DF_SHOW_SUMMARY|DF_SHOW_DETAIL)) ){
> > + fprintf(stderr, "btrfs filesystem df: it is not possible to specify -s AND -d\n");
>
> This doesn't look right at all. You are adding two switches and
> specifying both of them is an error? A little too much for a command
> whose job is to do some basic math and pretty-print the result.
>
> How about displaying just the summary by default and then adding a
> *single* switch (-v or whatever) for summary+details?
I'd prefer to see both sections by default. The reason for this is
that without both sections, people tend to get confused because they
don't know they're looking at half the story (e.g. some numbers change
twice as fast as they think they should).
I think supplying both options should probably show both sections
again, and make it not an error to do so, but I'm happy either way.
Hugo.
--
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
--- "He's a nutcase, you know. There's no getting away from it -- ---
he'll end up with a knighthood"
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 828 bytes --]
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 20:38 ` Hugo Mills
@ 2012-10-03 20:43 ` Ilya Dryomov
2012-10-03 20:53 ` Goffredo Baroncelli
1 sibling, 0 replies; 19+ messages in thread
From: Ilya Dryomov @ 2012-10-03 20:43 UTC (permalink / raw)
To: Hugo Mills, Goffredo Baroncelli, linux-btrfs, Roman Mamedov,
Sébastien Maury, Goffredo Baroncelli
On Wed, Oct 03, 2012 at 09:38:52PM +0100, Hugo Mills wrote:
> On Wed, Oct 03, 2012 at 11:34:00PM +0300, Ilya Dryomov wrote:
> > On Wed, Oct 03, 2012 at 07:22:31PM +0200, Goffredo Baroncelli wrote:
> [snip]
> > > +static const char * const cmd_disk_free_usage[] = {
> > > + "btrfs filesystem df [-d|-s][-k] <path> [<path>..]",
> > > + "Show space usage information for a mount point(s).",
> > > + "",
> > > + "-k\tSet KB (1024 bytes) as unit",
> > > + "-s\tShow the summary section only",
> > > + "-d\tShow the detail section only",
> > > + NULL
> > > +};
> > > +
> > > +static int cmd_disk_free(int argc, char **argv)
> > > +{
> > > +
> > > + int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
> > > + int i, more_than_one=0;
> > > +
> > > + optind = 1;
> > > + while(1){
> > > + char c = getopt(argc, argv, "dsk");
> > > + if(c<0)
> > > + break;
> > > + switch(c){
> > > + case 'd':
> > > + flags &= ~DF_SHOW_SUMMARY;
> > > + break;
> > > + case 's':
> > > + flags &= ~DF_SHOW_DETAIL;
> > > + break;
> > > + case 'k':
> > > + flags &= ~DF_HUMAN_UNIT;
> > > + break;
> > > + default:
> > > + usage(cmd_disk_free_usage);
> > > + }
> > > + }
> > > +
> > > + if( !(flags & (DF_SHOW_SUMMARY|DF_SHOW_DETAIL)) ){
> > > + fprintf(stderr, "btrfs filesystem df: it is not possible to specify -s AND -d\n");
> >
> > This doesn't look right at all. You are adding two switches and
> > specifying both of them is an error? A little too much for a command
> > whose job is to do some basic math and pretty-print the result.
> >
> > How about displaying just the summary by default and then adding a
> > *single* switch (-v or whatever) for summary+details?
>
> I'd prefer to see both sections by default. The reason for this is
> that without both sections, people tend to get confused because they
> don't know they're looking at half the story (e.g. some numbers change
> twice as fast as they think they should).
If we want both sections by default, there is no need for any switches
whatsoever, I think.
Thanks,
Ilya
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH 1/2] Update btrfs filesystem df command
2012-10-03 20:38 ` Hugo Mills
2012-10-03 20:43 ` Ilya Dryomov
@ 2012-10-03 20:53 ` Goffredo Baroncelli
1 sibling, 0 replies; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-03 20:53 UTC (permalink / raw)
To: Hugo Mills, Ilya Dryomov, linux-btrfs, Roman Mamedov,
Sébastien Maury
On 10/03/2012 10:38 PM, Hugo Mills wrote:
>>> + if( !(flags& (DF_SHOW_SUMMARY|DF_SHOW_DETAIL)) ){
>>> > > + fprintf(stderr, "btrfs filesystem df: it is not possible to specify -s AND -d\n");
>> >
>> > This doesn't look right at all. You are adding two switches and
>> > specifying both of them is an error? A little too much for a command
>> > whose job is to do some basic math and pretty-print the result.
>> >
>> > How about displaying just the summary by default and then adding a
>> > *single* switch (-v or whatever) for summary+details?
> I'd prefer to see both sections by default. The reason for this is
> that without both sections, people tend to get confused because they
> don't know they're looking at half the story (e.g. some numbers change
> twice as fast as they think they should).
I agree with Hugo. As default both the section should be showed.
> I think supplying both options should probably show both sections
> again, and make it not an error to do so, but I'm happy either way.
Because the default is to show both the sections, passing a switch means
"hide the other section". So passing both the switches means: "hide both
the sections"...
This is the reason of the check.
I am thinking to remove both the switches... But today is too late...
Now I am seeing a reply by Ilya which reached this same conclusion...
But today is still to late. Tomorrow I will rearrange the patch...
>
> Hugo.
Goffredp
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH][BTRFS-PROGS][V2] btrfs filesystem df
2012-10-03 19:38 ` Goffredo Baroncelli
@ 2012-10-04 7:15 ` David Sterba
0 siblings, 0 replies; 19+ messages in thread
From: David Sterba @ 2012-10-04 7:15 UTC (permalink / raw)
To: Goffredo Baroncelli
Cc: linux-btrfs, Hugo Mills, Roman Mamedov, Sébastien Maury,
Ilya Dryomov
On Wed, Oct 03, 2012 at 09:38:29PM +0200, Goffredo Baroncelli wrote:
> the output of the program ends with the "Metadata Single....". What you
> called "Help text" is a short description in the email.
Ah, I see. I still find it useful, would be good to include it in help
text or under a separate option.
david
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH 1/2] Update btrfs filesystem df command
2012-10-04 17:27 [PATCH][BTRFS-PROGS][V3] btrfs filesystem df Goffredo Baroncelli
@ 2012-10-04 17:27 ` Goffredo Baroncelli
0 siblings, 0 replies; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-04 17:27 UTC (permalink / raw)
To: Chris Mason
Cc: Hugo Mills, Roman Mamedov, Sébastien Maury, Ilya Dryomov,
linux-btrfs, Goffredo Baroncelli
From: Goffredo Baroncelli <kreijack@inwind.it>
The command btrfs filesystem df is used to query the
status of the chunks, how many space on the disk(s) are used by
the chunks, how many space are available in the chunks, and an
estimation of the free space of the filesystem.
---
cmds-filesystem.c | 270 +++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 221 insertions(+), 49 deletions(-)
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index b1457de..2e2b4b6 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <uuid/uuid.h>
#include <ctype.h>
+#include <sys/vfs.h>
#include "kerncompat.h"
#include "ctree.h"
@@ -39,25 +40,55 @@ static const char * const filesystem_cmd_group_usage[] = {
NULL
};
-static const char * const cmd_df_usage[] = {
- "btrfs filesystem df <path>",
- "Show space usage information for a mount point",
- NULL
-};
+static u64 disk_size( char *path){
+ struct statfs sfs;
+
+ if( statfs(path, &sfs) < 0 )
+ return 0;
+ else
+ return sfs.f_bsize * sfs.f_blocks;
-static int cmd_df(int argc, char **argv)
+}
+
+static void print_string(char *s, int w)
+{
+ int i;
+
+ printf("%s", s);
+ for( i = strlen(s) ; i < w ; i++ )
+ putchar(' ');
+}
+
+#define DF_SHOW_SUMMARY (1<<1)
+#define DF_SHOW_DETAIL (1<<2)
+#define DF_HUMAN_UNIT (1<<3)
+
+static void pretty_sizes_r(u64 size, int w, int mode)
+{
+ if( mode & DF_HUMAN_UNIT ){
+ char *s = pretty_sizes(size);
+ printf("%*s", w, s);
+ free(s);
+ } else {
+ printf("%*llu", w, size/1024);
+
+ }
+}
+
+static int _cmd_disk_free(char *path, int mode)
{
struct btrfs_ioctl_space_args *sargs;
u64 count = 0, i;
int ret;
int fd;
- int e;
- char *path;
-
- if (check_argc_exact(argc, 2))
- usage(cmd_df_usage);
-
- path = argv[1];
+ int e, width;
+ u64 total_disk; /* filesystem size == sum of
+ disks sizes */
+ u64 total_chunks; /* sum of chunks sizes on disk(s) */
+ u64 total_used; /* logical space used */
+ u64 total_free; /* logical space un-used */
+ double K;
+
fd = open_file_or_dir(path);
if (fd < 0) {
@@ -103,56 +134,197 @@ static int cmd_df(int argc, char **argv)
return ret;
}
- for (i = 0; i < sargs->total_spaces; i++) {
- char description[80];
- char *total_bytes;
- char *used_bytes;
- int written = 0;
- u64 flags = sargs->spaces[i].flags;
+ total_disk = disk_size(path);
+ e = errno;
+ if( total_disk == 0 ){
+ fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
+ path, strerror(e));
+ close(fd);
+ free(sargs);
+ return 19;
+ }
+
+ total_chunks = total_used = total_free = 0;
- memset(description, 0, 80);
+ for (i = 0; i < sargs->total_spaces; i++) {
+ int ratio=1;
+ u64 allocated;
- if (flags & BTRFS_BLOCK_GROUP_DATA) {
- if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 14, "%s",
- "Data+Metadata");
- written += 13;
- } else {
- snprintf(description, 5, "%s", "Data");
- written += 4;
- }
- } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
- snprintf(description, 7, "%s", "System");
- written += 6;
- } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 9, "%s", "Metadata");
- written += 8;
- }
+ u64 flags = sargs->spaces[i].flags;
if (flags & BTRFS_BLOCK_GROUP_RAID0) {
- snprintf(description+written, 8, "%s", ", RAID0");
- written += 7;
+ ratio=1;
} else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
- snprintf(description+written, 8, "%s", ", RAID1");
- written += 7;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_DUP) {
- snprintf(description+written, 6, "%s", ", DUP");
- written += 5;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
- snprintf(description+written, 9, "%s", ", RAID10");
- written += 8;
+ ratio=2;
+ } else {
+ ratio=1;
}
- total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
- used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
- printf("%s: total=%s, used=%s\n", description, total_bytes,
- used_bytes);
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ total_chunks += allocated;
+ total_used += sargs->spaces[i].used_bytes;
+ total_free += (sargs->spaces[i].total_bytes -
+ sargs->spaces[i].used_bytes);
+
+ }
+ K = ((double)total_used + (double)total_free) /
+ (double)total_chunks;
+
+ if( mode & DF_HUMAN_UNIT )
+ width = 12;
+ else
+ width = 18;
+
+ printf("Path: %s\n", path);
+ if( mode & DF_SHOW_SUMMARY ){
+ printf("Summary:\n");
+ printf(" Disk_size:\t\t");
+ pretty_sizes_r(total_disk, width, mode);
+ printf("\n Disk_allocated:\t");
+ pretty_sizes_r(total_chunks, width, mode);
+ printf("\n Disk_unallocated:\t");
+ pretty_sizes_r(total_disk-total_chunks, width, mode);
+ printf("\n Logical_size:\t\t");
+ pretty_sizes_r(total_used+total_free, width, mode);
+ printf("\n Used:\t\t\t");
+ pretty_sizes_r(total_used, width, mode);
+ printf("\n Free_(Estimated):\t");
+ pretty_sizes_r((u64)(K*total_disk-total_used), width, mode);
+ printf("\t(Max: ");
+ pretty_sizes_r(total_disk-total_chunks+total_free,
+ 0, mode );
+ printf(", Min: ");
+ pretty_sizes_r((total_disk-total_chunks)/2+total_free,
+ 0, mode );
+ printf(")\n Data_to_disk_ratio:\t%*.0f %%\n",
+ width-2, K*100);
+ }
+
+ if( ( mode & DF_SHOW_DETAIL ) && ( mode & DF_SHOW_SUMMARY ) )
+ printf("\n");
+
+ if( mode & DF_SHOW_DETAIL ){
+ /* Remember: the terminals have maximum 80 columns
+ do not believe to who says otherwise */
+ printf("Details:\n");
+ printf(" %-12s%-8s%*s%*s%*s\n",
+ "Chunk_type",
+ "Mode",
+ width, "Size_(disk)",
+ width+3, "Size_(logical)",
+ width-3, "Used"
+ );
+
+ for (i = 0; i < sargs->total_spaces; i++) {
+ char *description="";
+ int ratio=1;
+ char *r_mode;
+ u64 allocated;
+
+ u64 flags = sargs->spaces[i].flags;
+
+ if (flags & BTRFS_BLOCK_GROUP_DATA) {
+ if (flags & BTRFS_BLOCK_GROUP_METADATA){
+ description = "Data+M.data";
+ } else {
+ description = "Data";
+ }
+ } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+ description = "System";
+ } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+ description = "Metadata";
+ }
+
+ if (flags & BTRFS_BLOCK_GROUP_RAID0) {
+ r_mode = "RAID0";
+ ratio=1;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
+ r_mode = "RAID1";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
+ r_mode = "DUP";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
+ r_mode = "RAID10";
+ ratio=2;
+ } else {
+ r_mode = "Single";
+ ratio=1;
+ }
+
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ printf(" ");
+ print_string(description,12);
+ print_string(r_mode, 8);
+ pretty_sizes_r(allocated, width, mode);
+ pretty_sizes_r(sargs->spaces[i].total_bytes ,
+ width, mode);
+
+ pretty_sizes_r(sargs->spaces[i].used_bytes,
+ width, mode);
+ printf("\n");
+
+ }
}
free(sargs);
return 0;
}
+static const char * const cmd_disk_free_usage[] = {
+ "btrfs filesystem df [-k] <path> [<path>..]",
+ "Show space usage information for a mount point(s).",
+ "",
+ "-k\tSet KB (1024 bytes) as unit",
+ NULL
+};
+
+static int cmd_disk_free(int argc, char **argv)
+{
+
+ int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
+ int i, more_than_one=0;
+
+ optind = 1;
+ while(1){
+ char c = getopt(argc, argv, "dsk");
+ if(c<0)
+ break;
+ switch(c){
+ case 'k':
+ flags &= ~DF_HUMAN_UNIT;
+ break;
+ default:
+ usage(cmd_disk_free_usage);
+ }
+ }
+
+ if (check_argc_min(argc - optind, 1)){
+ usage(cmd_disk_free_usage);
+ return 21;
+ }
+
+ for(i=optind; i< argc ; i++){
+ int r;
+ if(more_than_one)
+ printf("\n");
+ r = _cmd_disk_free(argv[i], flags);
+ if( r ) return r;
+ more_than_one=1;
+
+ }
+
+ return 0;
+}
+
+
+
static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
{
char uuidbuf[37];
@@ -529,7 +701,7 @@ static int cmd_label(int argc, char **argv)
const struct cmd_group filesystem_cmd_group = {
filesystem_cmd_group_usage, NULL, {
- { "df", cmd_df, cmd_df_usage, NULL, 0 },
+ { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
{ "show", cmd_show, cmd_show_usage, NULL, 0 },
{ "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
{ "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
--
1.7.10.4
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH 1/2] Update btrfs filesystem df command
2012-10-13 17:47 [PATCH][BTRFS-PROGS][V4] btrfs filesystem df Goffredo Baroncelli
@ 2012-10-13 17:47 ` Goffredo Baroncelli
0 siblings, 0 replies; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-13 17:47 UTC (permalink / raw)
To: Chris Mason
Cc: Hugo Mills, Roman Mamedov, Sébastien Maury, Ilya Dryomov,
linux-btrfs, Martin Steigerwald, Goffredo Baroncelli
From: Goffredo Baroncelli <kreijack@inwind.it>
The command btrfs filesystem df is used to query the
status of the chunks, how many space on the disk(s) are used by
the chunks, how many space are available in the chunks, and an
estimation of the free space of the filesystem.
---
cmds-filesystem.c | 308 ++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 259 insertions(+), 49 deletions(-)
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index b1457de..509e393 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <uuid/uuid.h>
#include <ctype.h>
+#include <sys/vfs.h>
#include "kerncompat.h"
#include "ctree.h"
@@ -39,25 +40,86 @@ static const char * const filesystem_cmd_group_usage[] = {
NULL
};
-static const char * const cmd_df_usage[] = {
- "btrfs filesystem df <path>",
- "Show space usage information for a mount point",
- NULL
-};
+static u64 disk_size( char *path){
+ struct statfs sfs;
+
+ if( statfs(path, &sfs) < 0 )
+ return 0;
+ else
+ return sfs.f_bsize * sfs.f_blocks;
+
+}
+
+static void **strings_to_free=0;
+static int count_string_to_free=0;
+
+static void add_strings_to_free(char *s)
+{
+ int size;
+
+ size = sizeof(void*) * ++count_string_to_free;
+ strings_to_free = realloc( strings_to_free, size);
+
+ /* if we don't have enough memory, we have more serius
+ problem than that a wrong handling of not enough memory */
+ if(!strings_to_free){
+ fprintf(stderr, "add_string_to_free(): Not enough memory\n");
+ strings_to_free=0;
+ count_string_to_free=0;
+ }
+
+ strings_to_free[count_string_to_free-1] = s;
+}
+
+static void free_strings_to_free( )
+{
+ int i;
+ for( i = 0 ; i < count_string_to_free ; i++ )
+ free(strings_to_free[i]);
+
+ free(strings_to_free);
+
+ strings_to_free=0;
+ count_string_to_free=0;
+}
+
+#define DF_SHOW_SUMMARY (1<<1)
+#define DF_SHOW_DETAIL (1<<2)
+#define DF_HUMAN_UNIT (1<<3)
-static int cmd_df(int argc, char **argv)
+static char *df_pretty_sizes(u64 size, int mode)
+{
+ char *s;
+
+ if( mode & DF_HUMAN_UNIT ){
+ s = pretty_sizes(size);
+ if(!s) return NULL;
+ } else {
+ s = malloc(20);
+ if(!s) return NULL;
+ sprintf(s, "%llu", size/1024);
+ }
+
+ add_strings_to_free(s);
+ return s;
+}
+
+
+static int _cmd_disk_free(char *path, int mode)
{
struct btrfs_ioctl_space_args *sargs;
u64 count = 0, i;
int ret;
int fd;
- int e;
- char *path;
-
- if (check_argc_exact(argc, 2))
- usage(cmd_df_usage);
-
- path = argv[1];
+ int e, width, width_sl;
+ u64 total_disk; /* filesystem size == sum of
+ disks sizes */
+ u64 total_chunks; /* sum of chunks sizes on disk(s) */
+ u64 total_used; /* logical space used */
+ u64 total_free; /* logical space un-used */
+ double K;
+ char *rpath;
+
fd = open_file_or_dir(path);
if (fd < 0) {
@@ -103,56 +165,204 @@ static int cmd_df(int argc, char **argv)
return ret;
}
- for (i = 0; i < sargs->total_spaces; i++) {
- char description[80];
- char *total_bytes;
- char *used_bytes;
- int written = 0;
- u64 flags = sargs->spaces[i].flags;
+ total_disk = disk_size(path);
+ e = errno;
+ if( total_disk == 0 ){
+ fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
+ path, strerror(e));
+ close(fd);
+ free(sargs);
+ return 19;
+ }
+
+ total_chunks = total_used = total_free = 0;
- memset(description, 0, 80);
+ for (i = 0; i < sargs->total_spaces; i++) {
+ int ratio=1;
+ u64 allocated;
- if (flags & BTRFS_BLOCK_GROUP_DATA) {
- if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 14, "%s",
- "Data+Metadata");
- written += 13;
- } else {
- snprintf(description, 5, "%s", "Data");
- written += 4;
- }
- } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
- snprintf(description, 7, "%s", "System");
- written += 6;
- } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 9, "%s", "Metadata");
- written += 8;
- }
+ u64 flags = sargs->spaces[i].flags;
if (flags & BTRFS_BLOCK_GROUP_RAID0) {
- snprintf(description+written, 8, "%s", ", RAID0");
- written += 7;
+ ratio=1;
} else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
- snprintf(description+written, 8, "%s", ", RAID1");
- written += 7;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_DUP) {
- snprintf(description+written, 6, "%s", ", DUP");
- written += 5;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
- snprintf(description+written, 9, "%s", ", RAID10");
- written += 8;
+ ratio=2;
+ } else {
+ ratio=1;
}
- total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
- used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
- printf("%s: total=%s, used=%s\n", description, total_bytes,
- used_bytes);
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ total_chunks += allocated;
+ total_used += sargs->spaces[i].used_bytes;
+ total_free += (sargs->spaces[i].total_bytes -
+ sargs->spaces[i].used_bytes);
+
+ }
+ K = ((double)total_used + (double)total_free) /
+ (double)total_chunks;
+
+ if( mode & DF_HUMAN_UNIT ){
+ width = 14;
+ width_sl = 18;
+ } else {
+ width = 18;
+ width_sl = 18;
}
+
+ rpath = realpath(path, 0);
+ printf("Path: %s\n", rpath);
+ free(rpath);
+
+ if( mode & DF_SHOW_SUMMARY ){
+ printf("Summary:\n");
+ printf(" Disk_size:\t\t%*s\n", width,
+ df_pretty_sizes(total_disk, mode));
+ printf(" Disk_allocated:\t%*s\n", width,
+ df_pretty_sizes(total_chunks, mode));
+ printf(" Disk_unallocated:\t%*s\n", width,
+ df_pretty_sizes(total_disk-total_chunks, mode));
+ printf(" Logical_size:\t\t%*s\n", width,
+ df_pretty_sizes(total_used+total_free, mode));
+ printf(" Used:\t\t\t%*s\n", width,
+ df_pretty_sizes(total_used, mode));
+ printf(" Free_(Estimated):\t%*s\t(Max: %s, min: %s)\n",
+ width,
+ df_pretty_sizes((u64)(K*total_disk-total_used), mode),
+ df_pretty_sizes(total_disk-total_chunks+total_free,
+ mode ),
+ df_pretty_sizes((total_disk-total_chunks)/2+total_free,
+ mode ));
+ printf(" Data_to_disk_ratio:\t%*.0f %%\n",
+ width-2, K*100);
+ }
+
+ if( ( mode & DF_SHOW_DETAIL ) && ( mode & DF_SHOW_SUMMARY ) )
+ printf("\n");
+
+ if( mode & DF_SHOW_DETAIL ){
+ /* Remember: the terminals have maximum 80 columns
+ do not believe to who says otherwise */
+ printf("Allocated_area:\n");
+ printf(" %-12s%-8s%*s%*s%*s\n",
+ "Type",
+ "Mode",
+ width, "Size_(disk)",
+ width_sl, "Size_(logical)",
+ width, "Used"
+ );
+
+ for (i = 0; i < sargs->total_spaces; i++) {
+ char *description="";
+ int ratio=1;
+ char *r_mode;
+ u64 allocated;
+
+ u64 flags = sargs->spaces[i].flags;
+
+ if (flags & BTRFS_BLOCK_GROUP_DATA) {
+ if (flags & BTRFS_BLOCK_GROUP_METADATA){
+ description = "Data+M.data";
+ } else {
+ description = "Data";
+ }
+ } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+ description = "System";
+ } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+ description = "Metadata";
+ }
+
+ if (flags & BTRFS_BLOCK_GROUP_RAID0) {
+ r_mode = "RAID0";
+ ratio=1;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
+ r_mode = "RAID1";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
+ r_mode = "DUP";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
+ r_mode = "RAID10";
+ ratio=2;
+ } else {
+ r_mode = "Single";
+ ratio=1;
+ }
+
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ printf(" %-12s%-8s%*s%*s%*s\n",
+ description,
+ r_mode,
+ width,
+ df_pretty_sizes(allocated, mode),
+ width_sl,
+ df_pretty_sizes(sargs->spaces[i].total_bytes ,
+ mode),
+ width,
+ df_pretty_sizes(sargs->spaces[i].used_bytes,
+ mode));
+
+ }
+ }
+ free_strings_to_free();
free(sargs);
return 0;
}
+static const char * const cmd_disk_free_usage[] = {
+ "btrfs filesystem df [-k] <path> [<path>..]",
+ "Show space usage information for a mount point(s).",
+ "",
+ "-k\tSet KB (1024 bytes) as unit",
+ NULL
+};
+
+static int cmd_disk_free(int argc, char **argv)
+{
+
+ int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
+ int i, more_than_one=0;
+
+ optind = 1;
+ while(1){
+ char c = getopt(argc, argv, "k");
+ if(c<0)
+ break;
+ switch(c){
+ case 'k':
+ flags &= ~DF_HUMAN_UNIT;
+ break;
+ default:
+ usage(cmd_disk_free_usage);
+ }
+ }
+
+ if (check_argc_min(argc - optind, 1)){
+ usage(cmd_disk_free_usage);
+ return 21;
+ }
+
+ for(i=optind; i< argc ; i++){
+ int r;
+ if(more_than_one)
+ printf("\n");
+ r = _cmd_disk_free(argv[i], flags);
+ if( r ) return r;
+ more_than_one=1;
+
+ }
+
+ return 0;
+}
+
+
+
static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
{
char uuidbuf[37];
@@ -529,7 +739,7 @@ static int cmd_label(int argc, char **argv)
const struct cmd_group filesystem_cmd_group = {
filesystem_cmd_group_usage, NULL, {
- { "df", cmd_df, cmd_df_usage, NULL, 0 },
+ { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
{ "show", cmd_show, cmd_show_usage, NULL, 0 },
{ "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
{ "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
--
1.7.10.4
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH 1/2] Update btrfs filesystem df command
2012-10-13 19:14 [PATCH][BTRFS-PROGS][V6] " Goffredo Baroncelli
@ 2012-10-13 19:14 ` Goffredo Baroncelli
0 siblings, 0 replies; 19+ messages in thread
From: Goffredo Baroncelli @ 2012-10-13 19:14 UTC (permalink / raw)
To: Chris Mason
Cc: Goffredo Baroncelli, Hugo Mills, Roman Mamedov,
Sébastien Maury, Ilya Dryomov, linux-btrfs,
Martin Steigerwald, Goffredo Baroncelli
From: Goffredo Baroncelli <kreijack@inwind.it>
The command btrfs filesystem df is used to query the
status of the chunks, how many space on the disk(s) are used by
the chunks, how many space are available in the chunks, and an
estimation of the free space of the filesystem.
---
cmds-filesystem.c | 311 ++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 260 insertions(+), 51 deletions(-)
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index 9c43d35..fceba0e 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <uuid/uuid.h>
#include <ctype.h>
+#include <sys/vfs.h>
#include "kerncompat.h"
#include "ctree.h"
@@ -39,25 +40,86 @@ static const char * const filesystem_cmd_group_usage[] = {
NULL
};
-static const char * const cmd_df_usage[] = {
- "btrfs filesystem df <path>",
- "Show space usage information for a mount point",
- NULL
-};
+static u64 disk_size( char *path){
+ struct statfs sfs;
+
+ if( statfs(path, &sfs) < 0 )
+ return 0;
+ else
+ return sfs.f_bsize * sfs.f_blocks;
+
+}
+
+static void **strings_to_free=0;
+static int count_string_to_free=0;
+
+static void add_strings_to_free(char *s)
+{
+ int size;
+
+ size = sizeof(void*) * ++count_string_to_free;
+ strings_to_free = realloc( strings_to_free, size);
+
+ /* if we don't have enough memory, we have more serius
+ problem than that a wrong handling of not enough memory */
+ if(!strings_to_free){
+ fprintf(stderr, "add_string_to_free(): Not enough memory\n");
+ strings_to_free=0;
+ count_string_to_free=0;
+ }
+
+ strings_to_free[count_string_to_free-1] = s;
+}
+
+static void free_strings_to_free( )
+{
+ int i;
+ for( i = 0 ; i < count_string_to_free ; i++ )
+ free(strings_to_free[i]);
+
+ free(strings_to_free);
+
+ strings_to_free=0;
+ count_string_to_free=0;
+}
+
+#define DF_SHOW_SUMMARY (1<<1)
+#define DF_SHOW_DETAIL (1<<2)
+#define DF_HUMAN_UNIT (1<<3)
+
+static char *df_pretty_sizes(u64 size, int mode)
+{
+ char *s;
+
+ if( mode & DF_HUMAN_UNIT ){
+ s = pretty_sizes(size);
+ if(!s) return NULL;
+ } else {
+ s = malloc(20);
+ if(!s) return NULL;
+ sprintf(s, "%llu", size/1024);
+ }
+
+ add_strings_to_free(s);
+ return s;
+}
+
-static int cmd_df(int argc, char **argv)
+static int _cmd_disk_free(char *path, int mode)
{
struct btrfs_ioctl_space_args *sargs, *sargs_orig;
u64 count = 0, i;
int ret;
int fd;
- int e;
- char *path;
-
- if (check_argc_exact(argc, 2))
- usage(cmd_df_usage);
-
- path = argv[1];
+ int e, width, width_sl;
+ u64 total_disk; /* filesystem size == sum of
+ disks sizes */
+ u64 total_chunks; /* sum of chunks sizes on disk(s) */
+ u64 total_used; /* logical space used */
+ u64 total_free; /* logical space un-used */
+ double K;
+ char *rpath;
+
fd = open_file_or_dir(path);
if (fd < 0) {
@@ -102,65 +164,212 @@ static int cmd_df(int argc, char **argv)
ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
e = errno;
+ close(fd);
+
if (ret) {
fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
path, strerror(e));
- close(fd);
free(sargs);
return ret;
}
- for (i = 0; i < sargs->total_spaces; i++) {
- char description[80];
- char *total_bytes;
- char *used_bytes;
- int written = 0;
- u64 flags = sargs->spaces[i].flags;
+ total_disk = disk_size(path);
+ e = errno;
+ if( total_disk == 0 ){
+ fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
+ path, strerror(e));
+ free(sargs);
+ return 19;
+ }
+
+ total_chunks = total_used = total_free = 0;
- memset(description, 0, 80);
+ for (i = 0; i < sargs->total_spaces; i++) {
+ int ratio=1;
+ u64 allocated;
- if (flags & BTRFS_BLOCK_GROUP_DATA) {
- if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 14, "%s",
- "Data+Metadata");
- written += 13;
- } else {
- snprintf(description, 5, "%s", "Data");
- written += 4;
- }
- } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
- snprintf(description, 7, "%s", "System");
- written += 6;
- } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
- snprintf(description, 9, "%s", "Metadata");
- written += 8;
- }
+ u64 flags = sargs->spaces[i].flags;
if (flags & BTRFS_BLOCK_GROUP_RAID0) {
- snprintf(description+written, 8, "%s", ", RAID0");
- written += 7;
+ ratio=1;
} else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
- snprintf(description+written, 8, "%s", ", RAID1");
- written += 7;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_DUP) {
- snprintf(description+written, 6, "%s", ", DUP");
- written += 5;
+ ratio=2;
} else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
- snprintf(description+written, 9, "%s", ", RAID10");
- written += 8;
+ ratio=2;
+ } else {
+ ratio=1;
}
- total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
- used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
- printf("%s: total=%s, used=%s\n", description, total_bytes,
- used_bytes);
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ total_chunks += allocated;
+ total_used += sargs->spaces[i].used_bytes;
+ total_free += (sargs->spaces[i].total_bytes -
+ sargs->spaces[i].used_bytes);
+
}
- close(fd);
+ K = ((double)total_used + (double)total_free) /
+ (double)total_chunks;
+
+ if( mode & DF_HUMAN_UNIT ){
+ width = 14;
+ width_sl = 18;
+ } else {
+ width = 18;
+ width_sl = 18;
+ }
+
+ rpath = realpath(path, 0);
+ printf("Path: %s\n", rpath);
+ free(rpath);
+
+ if( mode & DF_SHOW_SUMMARY ){
+ printf("Summary:\n");
+ printf(" Disk_size:\t\t%*s\n", width,
+ df_pretty_sizes(total_disk, mode));
+ printf(" Disk_allocated:\t%*s\n", width,
+ df_pretty_sizes(total_chunks, mode));
+ printf(" Disk_unallocated:\t%*s\n", width,
+ df_pretty_sizes(total_disk-total_chunks, mode));
+ printf(" Logical_size:\t\t%*s\n", width,
+ df_pretty_sizes(total_used+total_free, mode));
+ printf(" Used:\t\t\t%*s\n", width,
+ df_pretty_sizes(total_used, mode));
+ printf(" Free_(Estimated):\t%*s\t(Max: %s, min: %s)\n",
+ width,
+ df_pretty_sizes((u64)(K*total_disk-total_used), mode),
+ df_pretty_sizes(total_disk-total_chunks+total_free,
+ mode ),
+ df_pretty_sizes((total_disk-total_chunks)/2+total_free,
+ mode ));
+ printf(" Data_to_disk_ratio:\t%*.0f %%\n",
+ width-2, K*100);
+ }
+
+ if( ( mode & DF_SHOW_DETAIL ) && ( mode & DF_SHOW_SUMMARY ) )
+ printf("\n");
+
+ if( mode & DF_SHOW_DETAIL ){
+ /* Remember: the terminals have maximum 80 columns
+ do not believe to who says otherwise */
+ printf("Allocated_area:\n");
+ printf(" %-12s%-8s%*s%*s%*s\n",
+ "Type",
+ "Mode",
+ width, "Size_(disk)",
+ width_sl, "Size_(logical)",
+ width, "Used"
+ );
+
+ for (i = 0; i < sargs->total_spaces; i++) {
+ char *description="";
+ int ratio=1;
+ char *r_mode;
+ u64 allocated;
+
+ u64 flags = sargs->spaces[i].flags;
+
+ if (flags & BTRFS_BLOCK_GROUP_DATA) {
+ if (flags & BTRFS_BLOCK_GROUP_METADATA){
+ description = "Data+M.data";
+ } else {
+ description = "Data";
+ }
+ } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
+ description = "System";
+ } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
+ description = "Metadata";
+ }
+
+ if (flags & BTRFS_BLOCK_GROUP_RAID0) {
+ r_mode = "RAID0";
+ ratio=1;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
+ r_mode = "RAID1";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
+ r_mode = "DUP";
+ ratio=2;
+ } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
+ r_mode = "RAID10";
+ ratio=2;
+ } else {
+ r_mode = "Single";
+ ratio=1;
+ }
+
+ allocated = sargs->spaces[i].total_bytes * ratio;
+
+ printf(" %-12s%-8s%*s%*s%*s\n",
+ description,
+ r_mode,
+ width,
+ df_pretty_sizes(allocated, mode),
+ width_sl,
+ df_pretty_sizes(sargs->spaces[i].total_bytes ,
+ mode),
+ width,
+ df_pretty_sizes(sargs->spaces[i].used_bytes,
+ mode));
+
+ }
+ }
+ free_strings_to_free();
free(sargs);
return 0;
}
+static const char * const cmd_disk_free_usage[] = {
+ "btrfs filesystem df [-k] <path> [<path>..]",
+ "Show space usage information for a mount point(s).",
+ "",
+ "-k\tSet KB (1024 bytes) as unit",
+ NULL
+};
+
+static int cmd_disk_free(int argc, char **argv)
+{
+
+ int flags=DF_SHOW_SUMMARY|DF_SHOW_DETAIL|DF_HUMAN_UNIT;
+ int i, more_than_one=0;
+
+ optind = 1;
+ while(1){
+ char c = getopt(argc, argv, "k");
+ if(c<0)
+ break;
+ switch(c){
+ case 'k':
+ flags &= ~DF_HUMAN_UNIT;
+ break;
+ default:
+ usage(cmd_disk_free_usage);
+ }
+ }
+
+ if (check_argc_min(argc - optind, 1)){
+ usage(cmd_disk_free_usage);
+ return 21;
+ }
+
+ for(i=optind; i< argc ; i++){
+ int r;
+ if(more_than_one)
+ printf("\n");
+ r = _cmd_disk_free(argv[i], flags);
+ if( r ) return r;
+ more_than_one=1;
+
+ }
+
+ return 0;
+}
+
+
+
static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
{
char uuidbuf[37];
@@ -537,7 +746,7 @@ static int cmd_label(int argc, char **argv)
const struct cmd_group filesystem_cmd_group = {
filesystem_cmd_group_usage, NULL, {
- { "df", cmd_df, cmd_df_usage, NULL, 0 },
+ { "df", cmd_disk_free, cmd_disk_free_usage, NULL, 0 },
{ "show", cmd_show, cmd_show_usage, NULL, 0 },
{ "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
{ "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
--
1.7.10.4
^ permalink raw reply related [flat|nested] 19+ messages in thread
end of thread, other threads:[~2012-10-13 19:14 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-03 17:22 [PATCH][BTRFS-PROGS][V2] btrfs filesystem df Goffredo Baroncelli
2012-10-03 17:22 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
2012-10-03 20:34 ` Ilya Dryomov
2012-10-03 20:38 ` Hugo Mills
2012-10-03 20:43 ` Ilya Dryomov
2012-10-03 20:53 ` Goffredo Baroncelli
2012-10-03 17:22 ` [PATCH 2/2] Update help page Goffredo Baroncelli
2012-10-03 19:32 ` [PATCH][BTRFS-PROGS][V2] btrfs filesystem df David Sterba
2012-10-03 19:38 ` Goffredo Baroncelli
2012-10-04 7:15 ` David Sterba
-- strict thread matches above, loose matches on Subject: below --
2012-10-13 19:14 [PATCH][BTRFS-PROGS][V6] " Goffredo Baroncelli
2012-10-13 19:14 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
2012-10-13 17:47 [PATCH][BTRFS-PROGS][V4] btrfs filesystem df Goffredo Baroncelli
2012-10-13 17:47 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
2012-10-04 17:27 [PATCH][BTRFS-PROGS][V3] btrfs filesystem df Goffredo Baroncelli
2012-10-04 17:27 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
2012-10-03 11:43 [PATCH][BTRFS-PROGS][V1] btrfs filesystem df Goffredo Baroncelli
2012-10-03 11:43 ` [PATCH 1/2] Update btrfs filesystem df command Goffredo Baroncelli
2012-10-03 15:02 ` Ilya Dryomov
2012-10-03 16:34 ` Goffredo Baroncelli
2012-10-03 17:20 ` Ilya Dryomov
2012-10-03 17:38 ` Goffredo Baroncelli
2012-10-03 17:09 ` Goffredo Baroncelli
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).