btrfs-progs: fi usage: do not print global block reserve
[platform/upstream/btrfs-progs.git] / cmds-fi-usage.c
index adf1c27..a5a4936 100644 (file)
@@ -504,7 +504,7 @@ static int cmp_device_info(const void *a, const void *b)
 static int load_device_info(int fd, struct device_info **device_info_ptr,
                           int *device_info_count)
 {
-       int ret, i, ndevs, e;
+       int ret, i, ndevs;
        struct btrfs_ioctl_fs_info_args fi_args;
        struct btrfs_ioctl_dev_info_args dev_info;
        struct device_info *info;
@@ -513,12 +513,11 @@ static int load_device_info(int fd, struct device_info **device_info_ptr,
        *device_info_ptr = 0;
 
        ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
-       e = errno;
-       if (e == EPERM)
-               return -e;
        if (ret < 0) {
+               if (errno == EPERM)
+                       return -errno;
                fprintf(stderr, "ERROR: cannot get filesystem info - %s\n",
-                               strerror(e));
+                               strerror(errno));
                return 1;
        }
 
@@ -623,8 +622,12 @@ static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
        u64 total_unused = 0;
        struct string_table *matrix = 0;
        int  ncols, nrows;
+       int col;
+       int unallocated_col;
 
-       ncols = sargs->total_spaces + 2;
+       /* data/metadata/system, unallocated */
+       ncols = sargs->total_spaces + 1;
+       /* 2 for header, empty line, devices, ===, total, used */
        nrows = 2 + 1 + device_info_count + 1 + 2;
 
        matrix = table_create(ncols, nrows);
@@ -633,8 +636,13 @@ static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
                return;
        }
 
+       /*
+        * We have to skip the global block reserve everywhere as it's an
+        * artificial blockgroup
+        */
+
        /* header */
-       for (i = 0; i < sargs->total_spaces; i++) {
+       for (i = 0, col = 1; i < sargs->total_spaces; i++) {
                const char *description;
                u64 flags = sargs->spaces[i].flags;
 
@@ -643,23 +651,27 @@ static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
 
                description = btrfs_group_type_str(flags);
 
-               table_printf(matrix, 1+i, 0, "<%s", description);
+               table_printf(matrix, col++, 0, "<%s", description);
        }
+       unallocated_col = col;
 
-       for (i = 0; i < sargs->total_spaces; i++) {
+       for (i = 0, col = 1; i < sargs->total_spaces; i++) {
                const char *r_mode;
-
                u64 flags = sargs->spaces[i].flags;
+
+               if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
+                       continue;
+
                r_mode = btrfs_group_profile_str(flags);
 
-               table_printf(matrix, 1+i, 1, "<%s", r_mode);
+               table_printf(matrix, col++, 1, "<%s", r_mode);
        }
 
-       table_printf(matrix, 1+sargs->total_spaces, 1, "<Unallocated");
+       table_printf(matrix, unallocated_col, 1, "<Unallocated");
 
        /* body */
        for (i = 0; i < device_info_count; i++) {
-               int k, col;
+               int k;
                char *p;
 
                u64  total_allocated = 0, unused;
@@ -678,6 +690,9 @@ static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
                        int     j;
                        u64 size = 0;
 
+                       if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
+                               continue;
+
                        for (j = 0 ; j < chunks_info_count ; j++) {
                                if (chunks_info_ptr[j].type != flags )
                                                continue;
@@ -700,28 +715,42 @@ static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
                unused = get_partition_size(device_info_ptr[i].path)
                                - total_allocated;
 
-               table_printf(matrix, sargs->total_spaces + 1, i + 3,
+               table_printf(matrix, unallocated_col, i + 3,
                               ">%s", pretty_size_mode(unused, unit_mode));
                total_unused += unused;
 
        }
 
-       for (i = 0; i <= sargs->total_spaces; i++)
-               table_printf(matrix, i + 1, device_info_count + 3, "=");
+       for (i = 0, col = 1; i < sargs->total_spaces; i++) {
+               if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
+                       continue;
+
+               table_printf(matrix, col++, device_info_count + 3, "=");
+       }
+       /* One for Unallocated */
+       table_printf(matrix, col, device_info_count + 3, "=");
 
        /* footer */
        table_printf(matrix, 0, device_info_count + 4, "<Total");
-       for (i = 0; i < sargs->total_spaces; i++)
-               table_printf(matrix, 1 + i, device_info_count + 4, ">%s",
+       for (i = 0, col = 1; i < sargs->total_spaces; i++) {
+               if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
+                       continue;
+
+               table_printf(matrix, col++, device_info_count + 4, ">%s",
                        pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));
+       }
 
-       table_printf(matrix, sargs->total_spaces + 1, device_info_count + 4,
+       table_printf(matrix, unallocated_col, device_info_count + 4,
                        ">%s", pretty_size_mode(total_unused, unit_mode));
 
        table_printf(matrix, 0, device_info_count + 5, "<Used");
-       for (i = 0; i < sargs->total_spaces; i++)
-               table_printf(matrix, 1 + i, device_info_count+5, ">%s",
+       for (i = 0, col = 1; i < sargs->total_spaces; i++) {
+               if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
+                       continue;
+
+               table_printf(matrix, col++, device_info_count+5, ">%s",
                        pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
+       }
 
        table_dump(matrix);
        table_free(matrix);
@@ -859,76 +888,30 @@ out:
 const char * const cmd_filesystem_usage_usage[] = {
        "btrfs filesystem usage [options] <path> [<path>..]",
        "Show detailed information about internal filesystem usage .",
-       "-b|--raw           raw numbers in bytes",
-       "-h|--human-readable",
-       "                   human friendly numbers, base 1024 (default)",
-       "-H                 human friendly numbers, base 1000",
-       "--iec              use 1024 as a base (KiB, MiB, GiB, TiB)",
-       "--si               use 1000 as a base (kB, MB, GB, TB)",
-       "-k|--kbytes        show sizes in KiB, or kB with --si",
-       "-m|--mbytes        show sizes in MiB, or MB with --si",
-       "-g|--gbytes        show sizes in GiB, or GB with --si",
-       "-t|--tbytes        show sizes in TiB, or TB with --si",
+       HELPINFO_OUTPUT_UNIT_DF,
        "-T                 show data in tabular format",
        NULL
 };
 
 int cmd_filesystem_usage(int argc, char **argv)
 {
-       unsigned unit_mode = UNITS_DEFAULT;
        int ret = 0;
-       int     i, more_than_one = 0;
-       int     tabular = 0;
+       unsigned unit_mode;
+       int i;
+       int more_than_one = 0;
+       int tabular = 0;
+
+       unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
 
        optind = 1;
        while (1) {
                int c;
-               static const struct option long_options[] = {
-                       { "raw", no_argument, NULL, 'b'},
-                       { "kbytes", no_argument, NULL, 'k'},
-                       { "mbytes", no_argument, NULL, 'm'},
-                       { "gbytes", no_argument, NULL, 'g'},
-                       { "tbytes", no_argument, NULL, 't'},
-                       { "si", no_argument, NULL, GETOPT_VAL_SI},
-                       { "iec", no_argument, NULL, GETOPT_VAL_IEC},
-                       { "human-readable", no_argument, NULL,
-                               GETOPT_VAL_HUMAN_READABLE},
-                       { NULL, 0, NULL, 0 }
-               };
-
-               c = getopt_long(argc, argv, "bhHkmgtT", long_options, NULL);
 
+               c = getopt(argc, argv, "T");
                if (c < 0)
                        break;
+
                switch (c) {
-               case 'b':
-                       unit_mode = UNITS_RAW;
-                       break;
-               case 'k':
-                       units_set_base(&unit_mode, UNITS_KBYTES);
-                       break;
-               case 'm':
-                       units_set_base(&unit_mode, UNITS_MBYTES);
-                       break;
-               case 'g':
-                       units_set_base(&unit_mode, UNITS_GBYTES);
-                       break;
-               case 't':
-                       units_set_base(&unit_mode, UNITS_TBYTES);
-                       break;
-               case GETOPT_VAL_HUMAN_READABLE:
-               case 'h':
-                       unit_mode = UNITS_HUMAN_BINARY;
-                       break;
-               case 'H':
-                       unit_mode = UNITS_HUMAN_DECIMAL;
-                       break;
-               case GETOPT_VAL_SI:
-                       units_set_mode(&unit_mode, UNITS_DECIMAL);
-                       break;
-               case GETOPT_VAL_IEC:
-                       units_set_mode(&unit_mode, UNITS_BINARY);
-                       break;
                case 'T':
                        tabular = 1;
                        break;
@@ -948,10 +931,8 @@ int cmd_filesystem_usage(int argc, char **argv)
                int chunkcount = 0;
                int devcount = 0;
 
-               fd = open_file_or_dir(argv[i], &dirstream);
+               fd = btrfs_open_dir(argv[i], &dirstream, 1);
                if (fd < 0) {
-                       fprintf(stderr, "ERROR: can't access '%s'\n",
-                               argv[i]);
                        ret = 1;
                        goto out;
                }