* [PATCH][BTRFS-PROGS][V5] btrfs filesystem df
@ 2012-10-13 18:52 Goffredo Baroncelli
2012-10-13 18:52 ` [PATCH] Update btrfs filesystem df command Goffredo Baroncelli
2012-10-13 19:02 ` [PATCH][BTRFS-PROGS][V5] btrfs filesystem df Goffredo Baroncelli
0 siblings, 2 replies; 3+ messages in thread
From: Goffredo Baroncelli @ 2012-10-13 18:52 UTC (permalink / raw)
To: Chris Mason
Cc: Goffredo Baroncelli, Hugo Mills, Roman Mamedov,
Sébastien Maury, Ilya Dryomov, linux-btrfs,
Martin Steigerwald
Hi Chris,
this serie of patches updated the command "btrfs filesystem
df". I update this command because it is not so easy to get
the information about the disk usage from the command "fi df" and "fi show".
This patch was the result of some discussions on the btrfs
mailing list. Many thanks to all the contributors.
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 [-k] <path> [<path>..]
Show space usage information for a mount point(s).
-k Set KB (1024 bytes) as unit
$ ./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 %
Allocated_area:
Type Mode Size_(disk) Size_(logical) Used
Data Single 21.01GB 21.01GB 12.77GB
System DUP 80.00MB 40.00MB 4.00KB
System Single 4.00MB 4.00MB 0.00
Metadata DUP 4.00GB 2.00GB 709.63MB
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)
Type -> kind of chunk
Mode -> allocation policy of a chunk
Size_(disk) -> area of disk(s) occuped by the
chunk (see it as raw space used)
Size_(logical) -> 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.
Signed-off-by: Goffredo Baroncelli <kreijack@gmail.com>
BR
Goffredo Baroncelli
Changelog:
V4->V5 Add a close(fd) to avoid file descriptor leaking
V3->V4 Removed the switch -d -s from getopt(), aligned the
column Size_(logical), renamed the fields:
- Details -> Allocated_area
- Chunk_type -> Type
V2->V3 Removed the options '-s' and '-d'; replaced Chunk-size
and Logical-size with Size_(disk) and Size_(logical).
Update the man page.
V1->V2 Uses getopt(); replace "-" with "_"; change the behaviour
of the switches '-s' and '-d'.
V0->V1 Change the name of command from disk-usage to df
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH] Update btrfs filesystem df command
2012-10-13 18:52 [PATCH][BTRFS-PROGS][V5] btrfs filesystem df Goffredo Baroncelli
@ 2012-10-13 18:52 ` Goffredo Baroncelli
2012-10-13 19:02 ` [PATCH][BTRFS-PROGS][V5] btrfs filesystem df Goffredo Baroncelli
1 sibling, 0 replies; 3+ messages in thread
From: Goffredo Baroncelli @ 2012-10-13 18:52 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 | 310 ++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 260 insertions(+), 50 deletions(-)
diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index b1457de..77623f2 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);
-static int cmd_df(int argc, char **argv)
+ /* 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_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) {
@@ -95,64 +157,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);
+
+ }
+ 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] 3+ messages in thread
* Re: [PATCH][BTRFS-PROGS][V5] btrfs filesystem df
2012-10-13 18:52 [PATCH][BTRFS-PROGS][V5] btrfs filesystem df Goffredo Baroncelli
2012-10-13 18:52 ` [PATCH] Update btrfs filesystem df command Goffredo Baroncelli
@ 2012-10-13 19:02 ` Goffredo Baroncelli
1 sibling, 0 replies; 3+ messages in thread
From: Goffredo Baroncelli @ 2012-10-13 19:02 UTC (permalink / raw)
To: Goffredo Baroncelli
Cc: Chris Mason, Hugo Mills, Roman Mamedov, Sébastien Maury,
Ilya Dryomov, linux-btrfs, Martin Steigerwald
Please, discard this email...
On 2012-10-13 20:52, Goffredo Baroncelli wrote:
> Hi Chris,
>
> this serie of patches updated the command "btrfs filesystem
> df". I update this command because it is not so easy to get
> the information about the disk usage from the command "fi df" and "fi show".
> This patch was the result of some discussions on the btrfs
> mailing list. Many thanks to all the contributors.
>
> 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 [-k]<path> [<path>..]
>
> Show space usage information for a mount point(s).
>
> -k Set KB (1024 bytes) as unit
>
> $ ./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 %
>
> Allocated_area:
> Type Mode Size_(disk) Size_(logical) Used
> Data Single 21.01GB 21.01GB 12.77GB
> System DUP 80.00MB 40.00MB 4.00KB
> System Single 4.00MB 4.00MB 0.00
> Metadata DUP 4.00GB 2.00GB 709.63MB
> 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)
> Type -> kind of chunk
> Mode -> allocation policy of a chunk
> Size_(disk) -> area of disk(s) occuped by the
> chunk (see it as raw space used)
> Size_(logical) -> 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.
>
> Signed-off-by: Goffredo Baroncelli<kreijack@gmail.com>
>
> BR
> Goffredo Baroncelli
>
> Changelog:
> V4->V5 Add a close(fd) to avoid file descriptor leaking
> V3->V4 Removed the switch -d -s from getopt(), aligned the
> column Size_(logical), renamed the fields:
> - Details -> Allocated_area
> - Chunk_type -> Type
> V2->V3 Removed the options '-s' and '-d'; replaced Chunk-size
> and Logical-size with Size_(disk) and Size_(logical).
> Update the man page.
> V1->V2 Uses getopt(); replace "-" with "_"; change the behaviour
> of the switches '-s' and '-d'.
> V0->V1 Change the name of command from disk-usage to df
>
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-10-13 19:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-13 18:52 [PATCH][BTRFS-PROGS][V5] btrfs filesystem df Goffredo Baroncelli
2012-10-13 18:52 ` [PATCH] Update btrfs filesystem df command Goffredo Baroncelli
2012-10-13 19:02 ` [PATCH][BTRFS-PROGS][V5] btrfs filesystem df 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).