While playing around with resizing volumes recently, I realised that I didn''t know whether btrfs fi show and btrfs fi df reported sizes in ISO (e.g. powers of 10^3) units, as they appear to from the labels they use, or in binary (powers of 2^10) units. Also, a mere three significant figures is somewhat less than I''m comfortable with if I''m about to resize the containing block device downwards. This patch series adds the ability to pick which scale is used for show and df, and labels the amounts properly (e.g. MB for ISO, MiB for binary units). 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 --- emacs: Eighty Megabytes And Constantly Swapping. --- -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
hugo-lkml@carfax.org.uk
2010-Oct-17 18:26 UTC
[patch 1/4] Update pretty-printer for different multiple systems.
Make the pretty-printer for data sizes capable of printing in ISO (powers of 10^3), binary (powers of 2^10) or raw (a simple byte count). We default to binary sizes, maintaining the original behaviour, but print (e.g.) MiB to indicate that it''s a power of 2^10. Signed-off-by: Hugo Mills <hugo@carfax.org.uk> --- btrfs-show.c | 7 ++++--- btrfs_cmds.c | 13 ++++++++----- mkfs.c | 3 ++- utils.c | 48 +++++++++++++++++++++++++++++++++--------------- utils.h | 7 ++++++- 5 files changed, 53 insertions(+), 25 deletions(-) Index: btrfs-progs-unstable/btrfs-show.c ==================================================================--- btrfs-progs-unstable.orig/btrfs-show.c 2010-10-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/btrfs-show.c 2010-10-17 19:00:34.000000000 +0100 @@ -69,7 +69,8 @@ else printf("Label: none "); - super_bytes_used = pretty_sizes(device->super_bytes_used); + super_bytes_used = pretty_sizes(device->super_bytes_used, + PRETTY_SIZE_BINARY); total = device->total_devs; printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, @@ -81,8 +82,8 @@ char *total_bytes; char *bytes_used; device = list_entry(cur, struct btrfs_device, dev_list); - total_bytes = pretty_sizes(device->total_bytes); - bytes_used = pretty_sizes(device->bytes_used); + total_bytes = pretty_sizes(device->total_bytes, PRETTY_SIZE_BINARY); + bytes_used = pretty_sizes(device->bytes_used, PRETTY_SIZE_BINARY); printf("\tdevid %4llu size %s used %s path %s\n", (unsigned long long)device->devid, total_bytes, bytes_used, device->name); Index: btrfs-progs-unstable/btrfs_cmds.c ==================================================================--- btrfs-progs-unstable.orig/btrfs_cmds.c 2010-10-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/btrfs_cmds.c 2010-10-17 19:00:39.000000000 +0100 @@ -634,7 +634,8 @@ else printf("Label: none "); - super_bytes_used = pretty_sizes(device->super_bytes_used); + super_bytes_used = pretty_sizes(device->super_bytes_used, + PRETTY_SIZE_BINARY); total = device->total_devs; printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, @@ -646,8 +647,8 @@ char *total_bytes; char *bytes_used; device = list_entry(cur, struct btrfs_device, dev_list); - total_bytes = pretty_sizes(device->total_bytes); - bytes_used = pretty_sizes(device->bytes_used); + total_bytes = pretty_sizes(device->total_bytes, PRETTY_SIZE_BINARY); + bytes_used = pretty_sizes(device->bytes_used, PRETTY_SIZE_BINARY); printf("\tdevid %4llu size %s used %s path %s\n", (unsigned long long)device->devid, total_bytes, bytes_used, device->name); @@ -913,8 +914,10 @@ written += 8; } - total_bytes = pretty_sizes(sargs->spaces[i].total_bytes); - used_bytes = pretty_sizes(sargs->spaces[i].used_bytes); + total_bytes = pretty_sizes(sargs->spaces[i].total_bytes, + PRETTY_SIZE_BINARY); + used_bytes = pretty_sizes(sargs->spaces[i].used_bytes, + PRETTY_SIZE_BINARY); printf("%s: total=%s, used=%s\n", description, total_bytes, used_bytes); } Index: btrfs-progs-unstable/mkfs.c ==================================================================--- btrfs-progs-unstable.orig/mkfs.c 2010-10-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/mkfs.c 2010-10-17 18:15:08.000000000 +0100 @@ -524,7 +524,8 @@ printf("fs created label %s on %s\n\tnodesize %u leafsize %u " "sectorsize %u size %s\n", label, first_file, nodesize, leafsize, sectorsize, - pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy))); + pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy), + PRETTY_SIZE_BINARY)); printf("%s\n", BTRFS_BUILD_VERSION); btrfs_commit_transaction(trans, root); Index: btrfs-progs-unstable/utils.c ==================================================================--- btrfs-progs-unstable.orig/utils.c 2010-10-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/utils.c 2010-10-17 18:44:13.000000000 +0100 @@ -966,30 +966,48 @@ return ret; } -static char *size_strs[] = { "", "KB", "MB", "GB", "TB", +static char *bin_size_strs[] = { "", "KiB", "MiB", "GiB", "TiB", + "PiB", "EiB", "ZiB", "YiB"}; +static char *iso_size_strs[] = { "", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; -char *pretty_sizes(u64 size) +char *pretty_sizes(u64 size, int format) { int num_divs = 0; u64 last_size = size; u64 fract_size = size; float fraction; char *pretty; + int divisor = 1024; + char** size_strs = bin_size_strs; - while(size > 0) { - fract_size = last_size; - last_size = size; - size /= 1024; - num_divs++; + if(format == PRETTY_SIZE_RAW) { + pretty = malloc(21); + sprintf(pretty, "%llu", size); + } else { + if(format == PRETTY_SIZE_ISO) { + divisor = 1000; + size_strs = iso_size_strs; + } else if(format == PRETTY_SIZE_BINARY) { + divisor = 1024; + size_strs = bin_size_strs; + } + + while(size > 0) { + fract_size = last_size; + last_size = size; + size /= divisor; + num_divs++; + } + if (num_divs == 0) + num_divs = 1; + if (num_divs > ARRAY_SIZE(bin_size_strs)) + return NULL; + + fraction = (float)fract_size / divisor; + pretty = malloc(16); + sprintf(pretty, "%.2f%s", fraction, size_strs[num_divs-1]); } - if (num_divs == 0) - num_divs = 1; - if (num_divs > ARRAY_SIZE(size_strs)) - return NULL; - - fraction = (float)fract_size / 1024; - pretty = malloc(16); - sprintf(pretty, "%.2f%s", fraction, size_strs[num_divs-1]); + return pretty; } Index: btrfs-progs-unstable/utils.h ==================================================================--- btrfs-progs-unstable.orig/utils.h 2010-10-17 18:13:29.000000000 +0100 +++ btrfs-progs-unstable/utils.h 2010-10-17 18:15:08.000000000 +0100 @@ -21,6 +21,11 @@ #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) +/* Constants for pretty_size() format parameter */ +#define PRETTY_SIZE_RAW 0 +#define PRETTY_SIZE_ISO 1 +#define PRETTY_SIZE_BINARY 2 + int make_btrfs(int fd, const char *device, const char *label, u64 blocks[6], u64 num_bytes, u32 nodesize, u32 leafsize, u32 sectorsize, u32 stripesize); @@ -39,5 +44,5 @@ int check_mounted(const char *devicename); int btrfs_device_already_in_root(struct btrfs_root *root, int fd, int super_offset); -char *pretty_sizes(u64 size); +char *pretty_sizes(u64 size, int format); #endif -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
hugo-lkml@carfax.org.uk
2010-Oct-17 18:26 UTC
[patch 2/4] Add an option to show ISO, binary or raw bytes counts using df.
Change btrfs filesystem df to allow the user to control the scales used for sizes in the output. Signed-off-by: Hugo Mills <hugo@carfax.org.uk> --- btrfs.c | 5 +++-- btrfs_cmds.c | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 7 deletions(-) Index: btrfs-progs-unstable/btrfs.c ==================================================================--- btrfs-progs-unstable.orig/btrfs.c 2010-10-17 18:43:57.000000000 +0100 +++ btrfs-progs-unstable/btrfs.c 2010-10-17 18:47:36.000000000 +0100 @@ -87,9 +87,10 @@ "Show the info of a btrfs filesystem. If no <uuid> or <label>\n" "is passed, info of all the btrfs filesystem are shown." }, - { do_df_filesystem, 1, - "filesystem df", "<path>\n" + { do_df_filesystem, -1, + "filesystem df", "[-r|-b|-i] <path>\n" "Show space usage information for a mount point\n." + "-r, -b, -i for raw (bytes), binary or ISO sizes." }, { do_balance, 1, "filesystem balance", "<path>\n" Index: btrfs-progs-unstable/btrfs_cmds.c ==================================================================--- btrfs-progs-unstable.orig/btrfs_cmds.c 2010-10-17 18:43:57.000000000 +0100 +++ btrfs-progs-unstable/btrfs_cmds.c 2010-10-17 18:47:36.000000000 +0100 @@ -841,7 +841,36 @@ u64 count = 0, i; int ret; int fd; - char *path = argv[1]; + char *path; + int format = PRETTY_SIZE_BINARY; + + optind = 1; + while(1) { + int c = getopt(nargs, argv, "rbi"); + if (c < 0) + break; + switch(c) { + case ''r'': + format = PRETTY_SIZE_RAW; + break; + case ''b'': + format = PRETTY_SIZE_BINARY; + break; + case ''i'': + format = PRETTY_SIZE_ISO; + break; + default: + fprintf(stderr, "Invalid arguments for df\n"); + free(argv); + return 1; + } + } + if (nargs - optind != 1) { + fprintf(stderr, "No path given for df\n"); + free(argv); + return 1; + } + path = argv[optind]; fd = open_file_or_dir(path); if (fd < 0) { @@ -914,10 +943,8 @@ written += 8; } - total_bytes = pretty_sizes(sargs->spaces[i].total_bytes, - PRETTY_SIZE_BINARY); - used_bytes = pretty_sizes(sargs->spaces[i].used_bytes, - PRETTY_SIZE_BINARY); + total_bytes = pretty_sizes(sargs->spaces[i].total_bytes, format); + used_bytes = pretty_sizes(sargs->spaces[i].used_bytes, format); printf("%s: total=%s, used=%s\n", description, total_bytes, used_bytes); } -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
hugo-lkml@carfax.org.uk
2010-Oct-17 18:26 UTC
[patch 3/4] Add an option to show ISO, binary or raw bytes counts using show.
Change btrfs filesystem show to allow the user to control the scales used for sizes in the output. Signed-off-by: Hugo Mills <hugo@carfax.org.uk> --- btrfs.c | 5 +++-- btrfs_cmds.c | 42 +++++++++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 9 deletions(-) Index: btrfs-progs-unstable/btrfs.c ==================================================================--- btrfs-progs-unstable.orig/btrfs.c 2010-10-17 18:47:36.000000000 +0100 +++ btrfs-progs-unstable/btrfs.c 2010-10-17 18:48:38.000000000 +0100 @@ -83,9 +83,10 @@ "will occupe all available space on the device." }, { do_show_filesystem, 999, - "filesystem show", "[<uuid>|<label>]\n" + "filesystem show", "[-r|-b|-i] [<uuid>|<label>]\n" "Show the info of a btrfs filesystem. If no <uuid> or <label>\n" - "is passed, info of all the btrfs filesystem are shown." + "is passed, info of all the btrfs filesystem are shown.\n" + "-r, -b, -i for raw (bytes), binary or ISO sizes." }, { do_df_filesystem, -1, "filesystem df", "[-r|-b|-i] <path>\n" Index: btrfs-progs-unstable/btrfs_cmds.c ==================================================================--- btrfs-progs-unstable.orig/btrfs_cmds.c 2010-10-17 18:47:36.000000000 +0100 +++ btrfs-progs-unstable/btrfs_cmds.c 2010-10-17 18:48:38.000000000 +0100 @@ -617,7 +617,7 @@ return 0; } -static void print_one_uuid(struct btrfs_fs_devices *fs_devices) +static void print_one_uuid(struct btrfs_fs_devices *fs_devices, int format) { char uuidbuf[37]; struct list_head *cur; @@ -634,8 +634,7 @@ else printf("Label: none "); - super_bytes_used = pretty_sizes(device->super_bytes_used, - PRETTY_SIZE_BINARY); + super_bytes_used = pretty_sizes(device->super_bytes_used, format); total = device->total_devs; printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, @@ -647,8 +646,8 @@ char *total_bytes; char *bytes_used; device = list_entry(cur, struct btrfs_device, dev_list); - total_bytes = pretty_sizes(device->total_bytes, PRETTY_SIZE_BINARY); - bytes_used = pretty_sizes(device->bytes_used, PRETTY_SIZE_BINARY); + total_bytes = pretty_sizes(device->total_bytes, format); + bytes_used = pretty_sizes(device->bytes_used, format); printf("\tdevid %4llu size %s used %s path %s\n", (unsigned long long)device->devid, total_bytes, bytes_used, device->name); @@ -667,8 +666,37 @@ struct list_head *all_uuids; struct btrfs_fs_devices *fs_devices; struct list_head *cur_uuid; - char *search = argv[1]; + char *search; int ret; + int format = PRETTY_SIZE_BINARY; + + optind = 1; + while(1) { + int c = getopt(argc, argv, "rbi"); + if (c < 0) + break; + switch(c) { + case ''r'': + format = PRETTY_SIZE_RAW; + break; + case ''b'': + format = PRETTY_SIZE_BINARY; + break; + case ''i'': + format = PRETTY_SIZE_ISO; + break; + default: + fprintf(stderr, "Invalid arguments for show\n"); + free(argv); + return 1; + } + } + if (argc - optind > 1) { + fprintf(stderr, "Too many arguments for show\n"); + free(argv); + return 1; + } + search = argv[optind]; ret = btrfs_scan_one_dir("/dev", 0); if (ret){ @@ -682,7 +710,7 @@ list); if (search && uuid_search(fs_devices, search) == 0) continue; - print_one_uuid(fs_devices); + print_one_uuid(fs_devices, format); } printf("%s\n", BTRFS_BUILD_VERSION); return 0; -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
hugo-lkml@carfax.org.uk
2010-Oct-17 18:26 UTC
[patch 4/4] Add an option to show ISO, binary or raw bytes counts using btrfs-show.
Change btrfs-show to allow the user to control the scales used for sizes in the output. Signed-off-by: Hugo Mills <hugo@carfax.org.uk> --- btrfs-show.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) Index: btrfs-progs-unstable/btrfs-show.c ==================================================================--- btrfs-progs-unstable.orig/btrfs-show.c 2010-10-17 19:00:34.000000000 +0100 +++ btrfs-progs-unstable/btrfs-show.c 2010-10-17 19:12:50.000000000 +0100 @@ -52,7 +52,7 @@ return 0; } -static void print_one_uuid(struct btrfs_fs_devices *fs_devices) +static void print_one_uuid(struct btrfs_fs_devices *fs_devices, int format) { char uuidbuf[37]; struct list_head *cur; @@ -69,8 +69,7 @@ else printf("Label: none "); - super_bytes_used = pretty_sizes(device->super_bytes_used, - PRETTY_SIZE_BINARY); + super_bytes_used = pretty_sizes(device->super_bytes_used, format); total = device->total_devs; printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, @@ -82,8 +81,8 @@ char *total_bytes; char *bytes_used; device = list_entry(cur, struct btrfs_device, dev_list); - total_bytes = pretty_sizes(device->total_bytes, PRETTY_SIZE_BINARY); - bytes_used = pretty_sizes(device->bytes_used, PRETTY_SIZE_BINARY); + total_bytes = pretty_sizes(device->total_bytes, format); + bytes_used = pretty_sizes(device->bytes_used, format); printf("\tdevid %4llu size %s used %s path %s\n", (unsigned long long)device->devid, total_bytes, bytes_used, device->name); @@ -99,7 +98,8 @@ static void print_usage(void) { - fprintf(stderr, "usage: btrfs-show [search label or device]\n"); + fprintf(stderr, "usage: btrfs-show [-i|-b|-r] [search label or device]\n"); + fprintf(stderr, "Options:\n -i, -b, -r: Show sizes in ISO, binary or raw form respectively.\n"); fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION); exit(1); } @@ -117,6 +117,7 @@ char *search = NULL; int ret; int option_index = 0; + int format = PRETTY_SIZE_BINARY; while(1) { int c; @@ -125,6 +126,15 @@ if (c < 0) break; switch(c) { + case ''i'': + format = PRETTY_SIZE_ISO; + break; + case ''b'': + format = PRETTY_SIZE_BINARY; + break; + case ''r'': + format = PRETTY_SIZE_RAW; + break; default: print_usage(); } @@ -144,7 +154,7 @@ list); if (search && uuid_search(fs_devices, search) == 0) continue; - print_one_uuid(fs_devices); + print_one_uuid(fs_devices, format); } printf("%s\n", BTRFS_BUILD_VERSION); return 0; -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Frank Kingswood
2010-Oct-18 08:21 UTC
Re: [patch 2/4] Add an option to show ISO, binary or raw bytes counts using df.
On 17/10/10 19:26, hugo-lkml@carfax.org.uk wrote:> Change btrfs filesystem df to allow the user to control the scales > used for sizes in the output. > > Index: btrfs-progs-unstable/btrfs.c > ==================================================================> --- btrfs-progs-unstable.orig/btrfs.c 2010-10-17 18:43:57.000000000 +0100 > +++ btrfs-progs-unstable/btrfs.c 2010-10-17 18:47:36.000000000 +0100 > @@ -87,9 +87,10 @@ > "Show the info of a btrfs filesystem. If no<uuid> or<label>\n" > "is passed, info of all the btrfs filesystem are shown." > }, > - { do_df_filesystem, 1, > - "filesystem df", "<path>\n" > + { do_df_filesystem, -1, > + "filesystem df", "[-r|-b|-i]<path>\n" > "Show space usage information for a mount point\n." > + "-r, -b, -i for raw (bytes), binary or ISO sizes." > },This seems to eat up the short option namespace a bit quickly. Fileutils uses different names as well, it may be convenient for users to match its names: -h --human-readable powers of 2**10 -H --si powers of 1000> { do_balance, 1, > "filesystem balance", "<path>\n" > Index: btrfs-progs-unstable/btrfs_cmds.c > ==================================================================> --- btrfs-progs-unstable.orig/btrfs_cmds.c 2010-10-17 18:43:57.000000000 +0100 > +++ btrfs-progs-unstable/btrfs_cmds.c 2010-10-17 18:47:36.000000000 +0100 > @@ -841,7 +841,36 @@ > u64 count = 0, i; > int ret; > int fd; > - char *path = argv[1]; > + char *path; > + int format = PRETTY_SIZE_BINARY;Should the default not be to show sizes in bytes ("RAW")? Frank -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Hugo Mills
2010-Oct-18 20:46 UTC
Re: [patch 2/4] Add an option to show ISO, binary or raw bytes counts using df.
On Mon, Oct 18, 2010 at 09:21:56AM +0100, Frank Kingswood wrote:> On 17/10/10 19:26, hugo-lkml@carfax.org.uk wrote: >> Change btrfs filesystem df to allow the user to control the scales >> used for sizes in the output. >> >> Index: btrfs-progs-unstable/btrfs.c >> ==================================================================>> --- btrfs-progs-unstable.orig/btrfs.c 2010-10-17 18:43:57.000000000 +0100 >> +++ btrfs-progs-unstable/btrfs.c 2010-10-17 18:47:36.000000000 +0100 >> @@ -87,9 +87,10 @@ >> "Show the info of a btrfs filesystem. If no<uuid> or<label>\n" >> "is passed, info of all the btrfs filesystem are shown." >> }, >> - { do_df_filesystem, 1, >> - "filesystem df", "<path>\n" >> + { do_df_filesystem, -1, >> + "filesystem df", "[-r|-b|-i]<path>\n" >> "Show space usage information for a mount point\n." >> + "-r, -b, -i for raw (bytes), binary or ISO sizes." >> }, > > This seems to eat up the short option namespace a bit quickly. > Fileutils uses different names as well, it may be convenient for users to > match its names: > -h --human-readable powers of 2**10 > -H --si powers of 1000Matching fileutils is probably a good idea. I''m happy to use -h and -H.>> { do_balance, 1, >> "filesystem balance", "<path>\n" >> Index: btrfs-progs-unstable/btrfs_cmds.c >> ==================================================================>> --- btrfs-progs-unstable.orig/btrfs_cmds.c 2010-10-17 18:43:57.000000000 +0100 >> +++ btrfs-progs-unstable/btrfs_cmds.c 2010-10-17 18:47:36.000000000 +0100 >> @@ -841,7 +841,36 @@ >> u64 count = 0, i; >> int ret; >> int fd; >> - char *path = argv[1]; >> + char *path; >> + int format = PRETTY_SIZE_BINARY; > > Should the default not be to show sizes in bytes ("RAW")?I was trying not to change the default behaviour at all, but with -h/-H (and no switch for --raw), that would make sense. I''ll re-roll the patches. (And update the man pages, as Goffredo asked). 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 --- "I don''t like the look of it, I tell you." "Well, stop --- looking at it, then."