2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
21 #include <sys/ioctl.h>
28 #include "kerncompat.h"
30 #include "string-table.h"
31 #include "cmds-fi-usage.h"
39 * Add the chunk info to the chunk_info list
41 static int add_info_to_list(struct chunk_info **info_ptr,
43 struct btrfs_chunk *chunk)
46 u64 type = btrfs_stack_chunk_type(chunk);
47 u64 size = btrfs_stack_chunk_length(chunk);
48 int num_stripes = btrfs_stack_chunk_num_stripes(chunk);
51 for (j = 0 ; j < num_stripes ; j++) {
53 struct chunk_info *p = NULL;
54 struct btrfs_stripe *stripe;
57 stripe = btrfs_stripe_nr(chunk, j);
58 devid = btrfs_stack_stripe_devid(stripe);
60 for (i = 0 ; i < *info_count ; i++)
61 if ((*info_ptr)[i].type == type &&
62 (*info_ptr)[i].devid == devid &&
63 (*info_ptr)[i].num_stripes == num_stripes ) {
69 int tmp = sizeof(struct btrfs_chunk) * (*info_count + 1);
70 struct chunk_info *res = realloc(*info_ptr, tmp);
74 error("not enough memory");
79 p = res + *info_count;
85 p->num_stripes = num_stripes;
97 * Helper to sort the chunk type
99 static int cmp_chunk_block_group(u64 f1, u64 f2)
104 if ((f1 & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
105 (f2 & BTRFS_BLOCK_GROUP_TYPE_MASK))
106 mask = BTRFS_BLOCK_GROUP_PROFILE_MASK;
107 else if (f2 & BTRFS_BLOCK_GROUP_SYSTEM)
109 else if (f1 & BTRFS_BLOCK_GROUP_SYSTEM)
112 mask = BTRFS_BLOCK_GROUP_TYPE_MASK;
114 if ((f1 & mask) > (f2 & mask))
116 else if ((f1 & mask) < (f2 & mask))
123 * Helper to sort the chunk
125 static int cmp_chunk_info(const void *a, const void *b)
127 return cmp_chunk_block_group(
128 ((struct chunk_info *)a)->type,
129 ((struct chunk_info *)b)->type);
132 static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
135 struct btrfs_ioctl_search_args args;
136 struct btrfs_ioctl_search_key *sk = &args.key;
137 struct btrfs_ioctl_search_header *sh;
138 unsigned long off = 0;
141 memset(&args, 0, sizeof(args));
144 * there may be more than one ROOT_ITEM key if there are
145 * snapshots pending deletion, we have to loop through
148 sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
150 sk->min_objectid = 0;
151 sk->max_objectid = (u64)-1;
153 sk->min_type = (u8)-1;
155 sk->max_offset = (u64)-1;
157 sk->max_transid = (u64)-1;
161 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
167 error("cannot look up chunk tree info: %s",
171 /* the ioctl returns the number of item it found in nr_items */
173 if (sk->nr_items == 0)
177 for (i = 0; i < sk->nr_items; i++) {
178 struct btrfs_chunk *item;
179 sh = (struct btrfs_ioctl_search_header *)(args.buf +
183 item = (struct btrfs_chunk *)(args.buf + off);
185 ret = add_info_to_list(info_ptr, info_count, item);
191 off += btrfs_search_header_len(sh);
193 sk->min_objectid = btrfs_search_header_objectid(sh);
194 sk->min_type = btrfs_search_header_type(sh);
195 sk->min_offset = btrfs_search_header_offset(sh)+1;
198 if (!sk->min_offset) /* overflow */
208 if (!sk->min_objectid)
212 qsort(*info_ptr, *info_count, sizeof(struct chunk_info),
219 * Helper to sort the struct btrfs_ioctl_space_info
221 static int cmp_btrfs_ioctl_space_info(const void *a, const void *b)
223 return cmp_chunk_block_group(
224 ((struct btrfs_ioctl_space_info *)a)->flags,
225 ((struct btrfs_ioctl_space_info *)b)->flags);
229 * This function load all the information about the space usage
231 static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path)
233 struct btrfs_ioctl_space_args *sargs = NULL, *sargs_orig = NULL;
236 sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
238 error("not enough memory");
242 sargs->space_slots = 0;
243 sargs->total_spaces = 0;
245 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
247 error("cannot get space info on '%s': %s", path,
252 if (!sargs->total_spaces) {
254 printf("No chunks found\n");
258 count = sargs->total_spaces;
260 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
261 (count * sizeof(struct btrfs_ioctl_space_info)));
264 error("not enough memory");
268 sargs->space_slots = count;
269 sargs->total_spaces = 0;
271 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
273 error("cannot get space info with %u slots: %s",
274 count, strerror(errno));
279 qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
280 cmp_btrfs_ioctl_space_info);
286 * This function computes the space occupied by a *single* RAID5/RAID6 chunk.
287 * The computation is performed on the basis of the number of stripes
288 * which compose the chunk, which could be different from the number of devices
289 * if a disk is added later.
291 static void get_raid56_used(struct chunk_info *chunks, int chunkcount,
292 u64 *raid5_used, u64 *raid6_used)
294 struct chunk_info *info_ptr = chunks;
298 while (chunkcount-- > 0) {
299 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID5)
300 (*raid5_used) += info_ptr->size / (info_ptr->num_stripes - 1);
301 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID6)
302 (*raid6_used) += info_ptr->size / (info_ptr->num_stripes - 2);
307 #define MIN_UNALOCATED_THRESH SZ_16M
308 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
309 int chunkcount, struct device_info *devinfo, int devcount,
310 char *path, unsigned unit_mode)
312 struct btrfs_ioctl_space_args *sargs = NULL;
315 int width = 10; /* default 10 for human units */
317 * r_* prefix is for raw data
320 u64 r_total_size = 0; /* filesystem size, sum of device sizes */
321 u64 r_total_chunks = 0; /* sum of chunks sizes on disk(s) */
322 u64 r_total_used = 0;
323 u64 r_total_unused = 0;
324 u64 r_total_missing = 0; /* sum of missing devices size */
326 u64 r_data_chunks = 0;
327 u64 l_data_chunks = 0;
328 u64 r_metadata_used = 0;
329 u64 r_metadata_chunks = 0;
330 u64 l_metadata_chunks = 0;
331 u64 r_system_used = 0;
332 u64 r_system_chunks = 0;
334 double metadata_ratio;
338 u64 l_global_reserve = 0;
339 u64 l_global_reserve_used = 0;
340 u64 free_estimated = 0;
342 int max_data_ratio = 1;
345 sargs = load_space_info(fd, path);
352 for (i = 0; i < devcount; i++) {
353 r_total_size += devinfo[i].size;
354 if (!devinfo[i].device_size)
355 r_total_missing += devinfo[i].size;
358 if (r_total_size == 0) {
359 error("cannot get space info on '%s': %s",
360 path, strerror(errno));
365 get_raid56_used(chunkinfo, chunkcount, &raid5_used, &raid6_used);
367 for (i = 0; i < sargs->total_spaces; i++) {
369 u64 flags = sargs->spaces[i].flags;
372 * The raid5/raid6 ratio depends by the stripes number
373 * used by every chunk. It is computed separately
375 if (flags & BTRFS_BLOCK_GROUP_RAID0)
377 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
379 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
381 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
383 else if (flags & BTRFS_BLOCK_GROUP_DUP)
385 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
391 warning("RAID56 detected, not implemented");
393 if (ratio > max_data_ratio)
394 max_data_ratio = ratio;
396 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV) {
397 l_global_reserve = sargs->spaces[i].total_bytes;
398 l_global_reserve_used = sargs->spaces[i].used_bytes;
400 if ((flags & (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA))
401 == (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA)) {
404 if (flags & BTRFS_BLOCK_GROUP_DATA) {
405 r_data_used += sargs->spaces[i].used_bytes * ratio;
406 r_data_chunks += sargs->spaces[i].total_bytes * ratio;
407 l_data_chunks += sargs->spaces[i].total_bytes;
409 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
410 r_metadata_used += sargs->spaces[i].used_bytes * ratio;
411 r_metadata_chunks += sargs->spaces[i].total_bytes * ratio;
412 l_metadata_chunks += sargs->spaces[i].total_bytes;
414 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
415 r_system_used += sargs->spaces[i].used_bytes * ratio;
416 r_system_chunks += sargs->spaces[i].total_bytes * ratio;
420 r_total_chunks = r_data_chunks + r_system_chunks;
421 r_total_used = r_data_used + r_system_used;
423 r_total_chunks += r_metadata_chunks;
424 r_total_used += r_metadata_used;
426 r_total_unused = r_total_size - r_total_chunks;
428 /* Raw / Logical = raid factor, >= 1 */
429 data_ratio = (double)r_data_chunks / l_data_chunks;
431 metadata_ratio = data_ratio;
433 metadata_ratio = (double)r_metadata_chunks / l_metadata_chunks;
436 /* add the raid5/6 allocated space */
437 total_chunks += raid5_used + raid6_used;
441 * We're able to fill at least DATA for the unused space
443 * With mixed raid levels, this gives a rough estimate but more
444 * accurate than just counting the logical free space
445 * (l_data_chunks - l_data_used)
447 * In non-mixed case there's no difference.
449 free_estimated = (r_data_chunks - r_data_used) / data_ratio;
451 * For mixed-bg the metadata are left out in calculations thus global
452 * reserve would be lost. Part of it could be permanently allocated,
453 * we have to subtract the used bytes so we don't go under zero free.
456 free_estimated -= l_global_reserve - l_global_reserve_used;
457 free_min = free_estimated;
459 /* Chop unallocatable space */
460 /* FIXME: must be applied per device */
461 if (r_total_unused >= MIN_UNALOCATED_THRESH) {
462 free_estimated += r_total_unused / data_ratio;
463 /* Match the calculation of 'df', use the highest raid ratio */
464 free_min += r_total_unused / max_data_ratio;
467 if (unit_mode != UNITS_HUMAN)
470 printf("Overall:\n");
472 printf(" Device size:\t\t%*s\n", width,
473 pretty_size_mode(r_total_size, unit_mode));
474 printf(" Device allocated:\t\t%*s\n", width,
475 pretty_size_mode(r_total_chunks, unit_mode));
476 printf(" Device unallocated:\t\t%*s\n", width,
477 pretty_size_mode(r_total_unused, unit_mode | UNITS_NEGATIVE));
478 printf(" Device missing:\t\t%*s\n", width,
479 pretty_size_mode(r_total_missing, unit_mode));
480 printf(" Used:\t\t\t%*s\n", width,
481 pretty_size_mode(r_total_used, unit_mode));
482 printf(" Free (estimated):\t\t%*s\t(",
484 pretty_size_mode(free_estimated, unit_mode));
485 printf("min: %s)\n", pretty_size_mode(free_min, unit_mode));
486 printf(" Data ratio:\t\t\t%*.2f\n",
488 printf(" Metadata ratio:\t\t%*.2f\n",
489 width, metadata_ratio);
490 printf(" Global reserve:\t\t%*s\t(used: %s)\n", width,
491 pretty_size_mode(l_global_reserve, unit_mode),
492 pretty_size_mode(l_global_reserve_used, unit_mode));
503 * Helper to sort the device_info structure
505 static int cmp_device_info(const void *a, const void *b)
507 return strcmp(((struct device_info *)a)->path,
508 ((struct device_info *)b)->path);
511 int dev_to_fsid(const char *dev, __u8 *fsid)
513 struct btrfs_super_block *disk_super;
514 char buf[BTRFS_SUPER_INFO_SIZE];
518 fd = open(dev, O_RDONLY);
524 disk_super = (struct btrfs_super_block *)buf;
525 ret = btrfs_read_dev_super(fd, disk_super,
526 BTRFS_SUPER_INFO_OFFSET, SBREAD_DEFAULT);
530 memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
539 * This function loads the device_info structure and put them in an array
541 static int load_device_info(int fd, struct device_info **device_info_ptr,
542 int *device_info_count)
545 struct btrfs_ioctl_fs_info_args fi_args;
546 struct btrfs_ioctl_dev_info_args dev_info;
547 struct device_info *info;
549 *device_info_count = 0;
550 *device_info_ptr = NULL;
552 ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
556 error("cannot get filesystem info: %s",
561 info = calloc(fi_args.num_devices, sizeof(struct device_info));
563 error("not enough memory");
567 for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
568 if (ndevs >= fi_args.num_devices) {
569 error("unexpected number of devices: %d >= %llu", ndevs,
570 (unsigned long long)fi_args.num_devices);
573 memset(&dev_info, 0, sizeof(dev_info));
574 ret = get_device_info(fd, i, &dev_info);
579 error("cannot get info about device devid=%d", i);
583 info[ndevs].devid = dev_info.devid;
584 if (!dev_info.path[0]) {
585 strcpy(info[ndevs].path, "missing");
587 strcpy(info[ndevs].path, (char *)dev_info.path);
588 info[ndevs].device_size =
589 get_partition_size((char *)dev_info.path);
591 info[ndevs].size = dev_info.total_bytes;
595 if (ndevs != fi_args.num_devices) {
596 error("unexpected number of devices: %d != %llu", ndevs,
597 (unsigned long long)fi_args.num_devices);
601 qsort(info, fi_args.num_devices,
602 sizeof(struct device_info), cmp_device_info);
604 *device_info_count = fi_args.num_devices;
605 *device_info_ptr = info;
614 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
615 int *chunkcount, struct device_info **devinfo, int *devcount)
619 ret = load_chunk_info(fd, chunkinfo, chunkcount);
622 "cannot read detailed chunk info, RAID5/6 numbers will be incorrect, run as root");
627 ret = load_device_info(fd, devinfo, devcount);
630 "cannot get filesystem info from ioctl(FS_INFO), run as root");
638 * This function computes the size of a chunk in a disk
640 static u64 calc_chunk_size(struct chunk_info *ci)
642 if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
643 return ci->size / ci->num_stripes;
644 else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
646 else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
648 else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
649 return ci->size / (ci->num_stripes -1);
650 else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
651 return ci->size / (ci->num_stripes -2);
652 else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
653 return ci->size / ci->num_stripes;
658 * This function print the results of the command "btrfs fi usage"
661 static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
662 struct btrfs_ioctl_space_args *sargs,
663 struct chunk_info *chunks_info_ptr,
664 int chunks_info_count,
665 struct device_info *device_info_ptr,
666 int device_info_count)
669 u64 total_unused = 0;
670 struct string_table *matrix = NULL;
675 const int vhdr_skip = 3; /* amount of vertical header space */
677 /* id, path, unallocated */
680 /* Properly count the real space infos */
681 for (i = 0; i < sargs->total_spaces; i++) {
682 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
687 /* 2 for header, empty line, devices, ===, total, used */
688 nrows = vhdr_skip + device_info_count + 1 + 2;
690 matrix = table_create(ncols, nrows);
692 error("not enough memory");
697 * We have to skip the global block reserve everywhere as it's an
698 * artificial blockgroup
702 for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
703 u64 flags = sargs->spaces[i].flags;
705 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
708 table_printf(matrix, col, 0, "<%s",
709 btrfs_group_type_str(flags));
710 table_printf(matrix, col, 1, "<%s",
711 btrfs_group_profile_str(flags));
714 unallocated_col = col;
716 table_printf(matrix, 0, 1, "<Id");
717 table_printf(matrix, 1, 1, "<Path");
718 table_printf(matrix, unallocated_col, 1, "<Unallocated");
721 for (i = 0; i < device_info_count; i++) {
725 u64 total_allocated = 0, unused;
727 p = strrchr(device_info_ptr[i].path, '/');
729 p = device_info_ptr[i].path;
733 table_printf(matrix, 0, vhdr_skip + i, ">%llu",
734 device_info_ptr[i].devid);
735 table_printf(matrix, 1, vhdr_skip + i, "<%s",
736 device_info_ptr[i].path);
738 for (col = spaceinfos_col, k = 0; k < sargs->total_spaces; k++) {
739 u64 flags = sargs->spaces[k].flags;
740 u64 devid = device_info_ptr[i].devid;
744 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
747 for (j = 0 ; j < chunks_info_count ; j++) {
748 if (chunks_info_ptr[j].type != flags )
750 if (chunks_info_ptr[j].devid != devid)
753 size += calc_chunk_size(chunks_info_ptr+j);
757 table_printf(matrix, col, vhdr_skip+ i,
758 ">%s", pretty_size_mode(size, unit_mode));
760 table_printf(matrix, col, vhdr_skip + i, ">-");
762 total_allocated += size;
766 unused = get_partition_size(device_info_ptr[i].path)
769 table_printf(matrix, unallocated_col, vhdr_skip + i, ">%s",
770 pretty_size_mode(unused, unit_mode | UNITS_NEGATIVE));
771 total_unused += unused;
775 for (i = 0; i < spaceinfos_col; i++) {
776 table_printf(matrix, i, vhdr_skip - 1, "*-");
777 table_printf(matrix, i, vhdr_skip + device_info_count, "*-");
780 for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
781 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
784 table_printf(matrix, col, vhdr_skip - 1, "*-");
785 table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
788 /* One for Unallocated */
789 table_printf(matrix, col, vhdr_skip - 1, "*-");
790 table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
793 table_printf(matrix, 1, vhdr_skip + device_info_count + 1, "<Total");
794 for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
795 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
798 table_printf(matrix, col++, vhdr_skip + device_info_count + 1,
800 pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));
803 table_printf(matrix, unallocated_col, vhdr_skip + device_info_count + 1,
805 pretty_size_mode(total_unused, unit_mode | UNITS_NEGATIVE));
807 table_printf(matrix, 1, vhdr_skip + device_info_count + 2, "<Used");
808 for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
809 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
812 table_printf(matrix, col++, vhdr_skip + device_info_count + 2,
814 pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
822 * This function prints the unused space per every disk
824 static void print_unused(struct chunk_info *info_ptr,
826 struct device_info *device_info_ptr,
827 int device_info_count,
831 for (i = 0; i < device_info_count; i++) {
835 for (j = 0; j < info_count; j++)
836 if (info_ptr[j].devid == device_info_ptr[i].devid)
837 total += calc_chunk_size(info_ptr+j);
839 printf(" %s\t%10s\n",
840 device_info_ptr[i].path,
841 pretty_size_mode(device_info_ptr[i].size - total,
847 * This function prints the allocated chunk per every disk
849 static void print_chunk_device(u64 chunk_type,
850 struct chunk_info *chunks_info_ptr,
851 int chunks_info_count,
852 struct device_info *device_info_ptr,
853 int device_info_count,
858 for (i = 0; i < device_info_count; i++) {
862 for (j = 0; j < chunks_info_count; j++) {
864 if (chunks_info_ptr[j].type != chunk_type)
866 if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
869 total += calc_chunk_size(&(chunks_info_ptr[j]));
870 //total += chunks_info_ptr[j].size;
874 printf(" %s\t%10s\n",
875 device_info_ptr[i].path,
876 pretty_size_mode(total, unit_mode));
881 * This function print the results of the command "btrfs fi usage"
884 static void _cmd_filesystem_usage_linear(unsigned unit_mode,
885 struct btrfs_ioctl_space_args *sargs,
886 struct chunk_info *info_ptr,
888 struct device_info *device_info_ptr,
889 int device_info_count)
893 for (i = 0; i < sargs->total_spaces; i++) {
894 const char *description;
896 u64 flags = sargs->spaces[i].flags;
898 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
901 description = btrfs_group_type_str(flags);
902 r_mode = btrfs_group_profile_str(flags);
904 printf("%s,%s: Size:%s, ",
907 pretty_size_mode(sargs->spaces[i].total_bytes,
910 pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
911 print_chunk_device(flags, info_ptr, info_count,
912 device_info_ptr, device_info_count, unit_mode);
916 printf("Unallocated:\n");
917 print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
918 unit_mode | UNITS_NEGATIVE);
921 static int print_filesystem_usage_by_chunk(int fd,
922 struct chunk_info *chunkinfo, int chunkcount,
923 struct device_info *devinfo, int devcount,
924 char *path, unsigned unit_mode, int tabular)
926 struct btrfs_ioctl_space_args *sargs;
932 sargs = load_space_info(fd, path);
939 _cmd_filesystem_usage_tabular(unit_mode, sargs, chunkinfo,
940 chunkcount, devinfo, devcount);
942 _cmd_filesystem_usage_linear(unit_mode, sargs, chunkinfo,
943 chunkcount, devinfo, devcount);
950 const char * const cmd_filesystem_usage_usage[] = {
951 "btrfs filesystem usage [options] <path> [<path>..]",
952 "Show detailed information about internal filesystem usage .",
953 HELPINFO_UNITS_SHORT_LONG,
954 "-T show data in tabular format",
958 int cmd_filesystem_usage(int argc, char **argv)
963 int more_than_one = 0;
966 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
971 c = getopt(argc, argv, "T");
980 usage(cmd_filesystem_usage_usage);
984 if (check_argc_min(argc - optind, 1))
985 usage(cmd_filesystem_usage_usage);
987 for (i = optind; i < argc; i++) {
989 DIR *dirstream = NULL;
990 struct chunk_info *chunkinfo = NULL;
991 struct device_info *devinfo = NULL;
995 fd = btrfs_open_dir(argv[i], &dirstream, 1);
1003 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
1004 &devinfo, &devcount);
1008 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
1009 devinfo, devcount, argv[i], unit_mode);
1013 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
1014 devinfo, devcount, argv[i], unit_mode, tabular);
1016 close_file_or_dir(fd, dirstream);
1029 void print_device_chunks(struct device_info *devinfo,
1030 struct chunk_info *chunks_info_ptr,
1031 int chunks_info_count, unsigned unit_mode)
1036 for (i = 0 ; i < chunks_info_count ; i++) {
1037 const char *description;
1042 if (chunks_info_ptr[i].devid != devinfo->devid)
1045 flags = chunks_info_ptr[i].type;
1047 description = btrfs_group_type_str(flags);
1048 r_mode = btrfs_group_profile_str(flags);
1049 size = calc_chunk_size(chunks_info_ptr+i);
1050 printf(" %s,%s:%*s%10s\n",
1053 (int)(20 - strlen(description) - strlen(r_mode)), "",
1054 pretty_size_mode(size, unit_mode));
1059 printf(" Unallocated: %*s%10s\n",
1060 (int)(20 - strlen("Unallocated")), "",
1061 pretty_size_mode(devinfo->size - allocated,
1062 unit_mode | UNITS_NEGATIVE));
1065 void print_device_sizes(struct device_info *devinfo, unsigned unit_mode)
1067 printf(" Device size: %*s%10s\n",
1068 (int)(20 - strlen("Device size")), "",
1069 pretty_size_mode(devinfo->device_size, unit_mode));
1070 printf(" Device slack: %*s%10s\n",
1071 (int)(20 - strlen("Device slack")), "",
1072 pretty_size_mode(devinfo->device_size > 0 ?
1073 devinfo->device_size - devinfo->size : 0,