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>
26 #include "kerncompat.h"
28 #include "string-table.h"
29 #include "cmds-fi-disk_usage.h"
35 * Add the chunk info to the chunk_info list
37 static int add_info_to_list(struct chunk_info **info_ptr,
39 struct btrfs_chunk *chunk)
42 u64 type = btrfs_stack_chunk_type(chunk);
43 u64 size = btrfs_stack_chunk_length(chunk);
44 int num_stripes = btrfs_stack_chunk_num_stripes(chunk);
47 for (j = 0 ; j < num_stripes ; j++) {
49 struct chunk_info *p = 0;
50 struct btrfs_stripe *stripe;
53 stripe = btrfs_stripe_nr(chunk, j);
54 devid = btrfs_stack_stripe_devid(stripe);
56 for (i = 0 ; i < *info_count ; i++)
57 if ((*info_ptr)[i].type == type &&
58 (*info_ptr)[i].devid == devid &&
59 (*info_ptr)[i].num_stripes == num_stripes ) {
65 int size = sizeof(struct btrfs_chunk) * (*info_count+1);
66 struct chunk_info *res = realloc(*info_ptr, size);
70 fprintf(stderr, "ERROR: not enough memory\n");
75 p = res + *info_count;
81 p->num_stripes = num_stripes;
93 * Helper to sort the chunk type
95 static int cmp_chunk_block_group(u64 f1, u64 f2)
100 if ((f1 & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
101 (f2 & BTRFS_BLOCK_GROUP_TYPE_MASK))
102 mask = BTRFS_BLOCK_GROUP_PROFILE_MASK;
103 else if (f2 & BTRFS_BLOCK_GROUP_SYSTEM)
105 else if (f1 & BTRFS_BLOCK_GROUP_SYSTEM)
108 mask = BTRFS_BLOCK_GROUP_TYPE_MASK;
110 if ((f1 & mask) > (f2 & mask))
112 else if ((f1 & mask) < (f2 & mask))
119 * Helper to sort the chunk
121 static int cmp_chunk_info(const void *a, const void *b)
123 return cmp_chunk_block_group(
124 ((struct chunk_info *)a)->type,
125 ((struct chunk_info *)b)->type);
128 static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
131 struct btrfs_ioctl_search_args args;
132 struct btrfs_ioctl_search_key *sk = &args.key;
133 struct btrfs_ioctl_search_header *sh;
134 unsigned long off = 0;
137 memset(&args, 0, sizeof(args));
140 * there may be more than one ROOT_ITEM key if there are
141 * snapshots pending deletion, we have to loop through
144 sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
146 sk->min_objectid = 0;
147 sk->max_objectid = (u64)-1;
149 sk->min_type = (u8)-1;
151 sk->max_offset = (u64)-1;
153 sk->max_transid = (u64)-1;
157 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
164 "ERROR: can't perform the search - %s\n",
168 /* the ioctl returns the number of item it found in nr_items */
170 if (sk->nr_items == 0)
174 for (i = 0; i < sk->nr_items; i++) {
175 struct btrfs_chunk *item;
176 sh = (struct btrfs_ioctl_search_header *)(args.buf +
180 item = (struct btrfs_chunk *)(args.buf + off);
182 ret = add_info_to_list(info_ptr, info_count, item);
190 sk->min_objectid = sh->objectid;
191 sk->min_type = sh->type;
192 sk->min_offset = sh->offset+1;
195 if (!sk->min_offset) /* overflow */
205 if (!sk->min_objectid)
209 qsort(*info_ptr, *info_count, sizeof(struct chunk_info),
216 * Helper to sort the struct btrfs_ioctl_space_info
218 static int cmp_btrfs_ioctl_space_info(const void *a, const void *b)
220 return cmp_chunk_block_group(
221 ((struct btrfs_ioctl_space_info *)a)->flags,
222 ((struct btrfs_ioctl_space_info *)b)->flags);
226 * This function load all the information about the space usage
228 static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path)
230 struct btrfs_ioctl_space_args *sargs = 0, *sargs_orig = 0;
233 sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
235 fprintf(stderr, "ERROR: not enough memory\n");
239 sargs->space_slots = 0;
240 sargs->total_spaces = 0;
242 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
246 "ERROR: couldn't get space info on '%s' - %s\n",
251 if (!sargs->total_spaces) {
253 printf("No chunks found\n");
257 count = sargs->total_spaces;
259 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
260 (count * sizeof(struct btrfs_ioctl_space_info)));
263 fprintf(stderr, "ERROR: not enough memory\n");
267 sargs->space_slots = count;
268 sargs->total_spaces = 0;
270 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
275 "ERROR: couldn't get space info on '%s' - %s\n",
281 qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
282 cmp_btrfs_ioctl_space_info);
288 * This function computes the space occuped by a *single* RAID5/RAID6 chunk.
289 * The computation is performed on the basis of the number of stripes
290 * which compose the chunk, which could be different from the number of devices
291 * if a disk is added later.
293 static void get_raid56_used(int fd, struct chunk_info *chunks, int chunkcount,
294 u64 *raid5_used, u64 *raid6_used)
296 struct chunk_info *info_ptr = chunks;
300 while (chunkcount-- > 0) {
301 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID5)
302 (*raid5_used) += info_ptr->size / (info_ptr->num_stripes - 1);
303 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID6)
304 (*raid6_used) += info_ptr->size / (info_ptr->num_stripes - 2);
309 #define MIN_UNALOCATED_THRESH (16 * 1024 * 1024)
310 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
311 int chunkcount, struct device_info *devinfo, int devcount,
312 char *path, int mode)
314 struct btrfs_ioctl_space_args *sargs = 0;
317 int width = 10; /* default 10 for human units */
319 * r_* prefix is for raw data
322 u64 r_total_size = 0; /* filesystem size, sum of device sizes */
323 u64 r_total_chunks = 0; /* sum of chunks sizes on disk(s) */
324 u64 r_total_used = 0;
325 u64 r_total_unused = 0;
327 u64 r_data_chunks = 0;
328 u64 l_data_chunks = 0;
329 u64 r_metadata_used = 0;
330 u64 r_metadata_chunks = 0;
331 u64 l_metadata_chunks = 0;
332 u64 r_system_used = 0;
333 u64 r_system_chunks = 0;
335 double metadata_ratio;
339 u64 l_global_reserve = 0;
340 u64 l_global_reserve_used = 0;
341 u64 free_estimated = 0;
343 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].device_size;
355 if (r_total_size == 0) {
357 "ERROR: couldn't get space info on '%s' - %s\n",
358 path, strerror(errno));
363 get_raid56_used(fd, chunkinfo, chunkcount, &raid5_used, &raid6_used);
365 for (i = 0; i < sargs->total_spaces; i++) {
367 u64 flags = sargs->spaces[i].flags;
370 * The raid5/raid6 ratio depends by the stripes number
371 * used by every chunk. It is computed separately
373 if (flags & BTRFS_BLOCK_GROUP_RAID0)
375 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
377 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
379 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
381 else if (flags & BTRFS_BLOCK_GROUP_DUP)
383 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
389 fprintf(stderr, "WARNING: RAID56 detected, not implemented\n");
391 if (ratio > max_data_ratio)
392 max_data_ratio = ratio;
394 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV) {
395 l_global_reserve = sargs->spaces[i].total_bytes;
396 l_global_reserve_used = sargs->spaces[i].used_bytes;
398 if ((flags & (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA))
399 == (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA)) {
400 fprintf(stderr, "WARNING: MIXED blockgroups not handled\n");
403 if (flags & BTRFS_BLOCK_GROUP_DATA) {
404 r_data_used += sargs->spaces[i].used_bytes * ratio;
405 r_data_chunks += sargs->spaces[i].total_bytes * ratio;
406 l_data_chunks += sargs->spaces[i].total_bytes;
408 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
409 r_metadata_used += sargs->spaces[i].used_bytes * ratio;
410 r_metadata_chunks += sargs->spaces[i].total_bytes * ratio;
411 l_metadata_chunks += sargs->spaces[i].total_bytes;
413 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
414 r_system_used += sargs->spaces[i].used_bytes * ratio;
415 r_system_chunks += sargs->spaces[i].total_bytes * ratio;
419 r_total_chunks = r_data_chunks + r_metadata_chunks + r_system_chunks;
420 r_total_used = r_data_used + r_metadata_used + r_system_used;
421 r_total_unused = r_total_size - r_total_chunks;
423 /* Raw / Logical = raid factor, >= 1 */
424 data_ratio = (double)r_data_chunks / l_data_chunks;
425 metadata_ratio = (double)r_metadata_chunks / l_metadata_chunks;
428 /* add the raid5/6 allocated space */
429 total_chunks += raid5_used + raid6_used;
433 * We're able to fill at least DATA for the unused space
435 * With mixed raid levels, this gives a rough estimate but more
436 * accurate than just counting the logical free space
437 * (l_data_chunks - l_data_used)
439 * In non-mixed case there's no difference.
441 free_estimated = (r_data_chunks - r_data_used) / data_ratio;
442 free_min = free_estimated;
444 /* Chop unallocatable space */
445 /* FIXME: must be applied per device */
446 if (r_total_unused >= MIN_UNALOCATED_THRESH) {
447 free_estimated += r_total_unused / data_ratio;
448 /* Match the calculation of 'df', use the highest raid ratio */
449 free_min += r_total_unused / max_data_ratio;
452 if (mode != UNITS_HUMAN)
455 printf("Overall:\n");
457 printf(" Device size:\t\t%*s\n", width,
458 pretty_size_mode(r_total_size, mode));
459 printf(" Device allocated:\t\t%*s\n", width,
460 pretty_size_mode(r_total_chunks, mode));
461 printf(" Device unallocated:\t\t%*s\n", width,
462 pretty_size_mode(r_total_unused, mode));
463 printf(" Used:\t\t\t%*s\n", width,
464 pretty_size_mode(r_total_used, mode));
465 printf(" Free (estimated):\t\t%*s\t(",
467 pretty_size_mode(free_estimated, mode));
468 printf("min: %s)\n", pretty_size_mode(free_min, mode));
469 printf(" Data ratio:\t\t\t%*.2f\n",
471 printf(" Metadata ratio:\t\t%*.2f\n",
472 width, metadata_ratio);
473 printf(" Global reserve:\t\t%*s\t(used: %s)\n", width,
474 pretty_size_mode(l_global_reserve, mode),
475 pretty_size_mode(l_global_reserve_used, mode));
486 * Helper to sort the device_info structure
488 static int cmp_device_info(const void *a, const void *b)
490 return strcmp(((struct device_info *)a)->path,
491 ((struct device_info *)b)->path);
495 * This function loads the device_info structure and put them in an array
497 static int load_device_info(int fd, struct device_info **device_info_ptr,
498 int *device_info_count)
500 int ret, i, ndevs, e;
501 struct btrfs_ioctl_fs_info_args fi_args;
502 struct btrfs_ioctl_dev_info_args dev_info;
503 struct device_info *info;
505 *device_info_count = 0;
506 *device_info_ptr = 0;
508 ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
513 fprintf(stderr, "ERROR: cannot get filesystem info - %s\n",
518 info = calloc(fi_args.num_devices, sizeof(struct device_info));
520 fprintf(stderr, "ERROR: not enough memory\n");
524 for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
525 BUG_ON(ndevs >= fi_args.num_devices);
526 memset(&dev_info, 0, sizeof(dev_info));
527 ret = get_device_info(fd, i, &dev_info);
533 "ERROR: cannot get info about device devid=%d\n",
539 info[ndevs].devid = dev_info.devid;
540 strcpy(info[ndevs].path, (char *)dev_info.path);
541 info[ndevs].device_size = get_partition_size((char *)dev_info.path);
542 info[ndevs].size = dev_info.total_bytes;
546 BUG_ON(ndevs != fi_args.num_devices);
547 qsort(info, fi_args.num_devices,
548 sizeof(struct device_info), cmp_device_info);
550 *device_info_count = fi_args.num_devices;
551 *device_info_ptr = info;
556 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
557 int *chunkcount, struct device_info **devinfo, int *devcount)
561 ret = load_chunk_info(fd, chunkinfo, chunkcount);
564 "WARNING: can't read detailed chunk info, RAID5/6 numbers will be incorrect, run as root\n");
569 ret = load_device_info(fd, devinfo, devcount);
572 "WARNING: can't get filesystem info from ioctl(FS_INFO), run as root\n");
580 * This function computes the size of a chunk in a disk
582 static u64 calc_chunk_size(struct chunk_info *ci)
584 if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
585 return ci->size / ci->num_stripes;
586 else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
588 else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
590 else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
591 return ci->size / (ci->num_stripes -1);
592 else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
593 return ci->size / (ci->num_stripes -2);
594 else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
595 return ci->size / ci->num_stripes;
600 * This function print the results of the command "btrfs fi usage"
603 static void _cmd_filesystem_usage_tabular(int mode,
604 struct btrfs_ioctl_space_args *sargs,
605 struct chunk_info *chunks_info_ptr,
606 int chunks_info_count,
607 struct device_info *device_info_ptr,
608 int device_info_count)
611 u64 total_unused = 0;
612 struct string_table *matrix = 0;
615 ncols = sargs->total_spaces + 2;
616 nrows = 2 + 1 + device_info_count + 1 + 2;
618 matrix = table_create(ncols, nrows);
620 fprintf(stderr, "ERROR: not enough memory\n");
625 for (i = 0; i < sargs->total_spaces; i++) {
626 const char *description;
627 u64 flags = sargs->spaces[i].flags;
629 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
632 description = btrfs_group_type_str(flags);
634 table_printf(matrix, 1+i, 0, "<%s", description);
637 for (i = 0; i < sargs->total_spaces; i++) {
640 u64 flags = sargs->spaces[i].flags;
641 r_mode = btrfs_group_profile_str(flags);
643 table_printf(matrix, 1+i, 1, "<%s", r_mode);
646 table_printf(matrix, 1+sargs->total_spaces, 1, "<Unallocated");
649 for (i = 0; i < device_info_count; i++) {
653 u64 total_allocated = 0, unused;
655 p = strrchr(device_info_ptr[i].path, '/');
657 p = device_info_ptr[i].path;
661 table_printf(matrix, 0, i + 3, "<%s", device_info_ptr[i].path);
663 for (col = 1, k = 0 ; k < sargs->total_spaces ; k++) {
664 u64 flags = sargs->spaces[k].flags;
665 u64 devid = device_info_ptr[i].devid;
669 for (j = 0 ; j < chunks_info_count ; j++) {
670 if (chunks_info_ptr[j].type != flags )
672 if (chunks_info_ptr[j].devid != devid)
675 size += calc_chunk_size(chunks_info_ptr+j);
679 table_printf(matrix, col, i+3,
680 ">%s", pretty_size_mode(size, mode));
682 table_printf(matrix, col, i+3, ">-");
684 total_allocated += size;
688 unused = get_partition_size(device_info_ptr[i].path)
691 table_printf(matrix, sargs->total_spaces + 1, i + 3,
692 ">%s", pretty_size_mode(unused, mode));
693 total_unused += unused;
697 for (i = 0; i <= sargs->total_spaces; i++)
698 table_printf(matrix, i + 1, device_info_count + 3, "=");
701 table_printf(matrix, 0, device_info_count + 4, "<Total");
702 for (i = 0; i < sargs->total_spaces; i++)
703 table_printf(matrix, 1 + i, device_info_count + 4, ">%s",
704 pretty_size_mode(sargs->spaces[i].total_bytes, mode));
706 table_printf(matrix, sargs->total_spaces + 1, device_info_count + 4,
707 ">%s", pretty_size_mode(total_unused, mode));
709 table_printf(matrix, 0, device_info_count + 5, "<Used");
710 for (i = 0; i < sargs->total_spaces; i++)
711 table_printf(matrix, 1 + i, device_info_count+5, ">%s",
712 pretty_size_mode(sargs->spaces[i].used_bytes, mode));
719 * This function prints the unused space per every disk
721 static void print_unused(struct chunk_info *info_ptr,
723 struct device_info *device_info_ptr,
724 int device_info_count,
728 for (i = 0; i < device_info_count; i++) {
732 for (j = 0; j < info_count; j++)
733 if (info_ptr[j].devid == device_info_ptr[i].devid)
734 total += calc_chunk_size(info_ptr+j);
736 printf(" %s\t%10s\n",
737 device_info_ptr[i].path,
738 pretty_size_mode(device_info_ptr[i].size - total, mode));
743 * This function prints the allocated chunk per every disk
745 static void print_chunk_device(u64 chunk_type,
746 struct chunk_info *chunks_info_ptr,
747 int chunks_info_count,
748 struct device_info *device_info_ptr,
749 int device_info_count,
754 for (i = 0; i < device_info_count; i++) {
758 for (j = 0; j < chunks_info_count; j++) {
760 if (chunks_info_ptr[j].type != chunk_type)
762 if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
765 total += calc_chunk_size(&(chunks_info_ptr[j]));
766 //total += chunks_info_ptr[j].size;
770 printf(" %s\t%10s\n",
771 device_info_ptr[i].path,
772 pretty_size_mode(total, mode));
777 * This function print the results of the command "btrfs fi usage"
780 static void _cmd_filesystem_usage_linear(int mode,
781 struct btrfs_ioctl_space_args *sargs,
782 struct chunk_info *info_ptr,
784 struct device_info *device_info_ptr,
785 int device_info_count)
789 for (i = 0; i < sargs->total_spaces; i++) {
790 const char *description;
792 u64 flags = sargs->spaces[i].flags;
794 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
797 description = btrfs_group_type_str(flags);
798 r_mode = btrfs_group_profile_str(flags);
800 printf("%s,%s: Size:%s, ",
803 pretty_size_mode(sargs->spaces[i].total_bytes,
806 pretty_size_mode(sargs->spaces[i].used_bytes, mode));
807 print_chunk_device(flags, info_ptr, info_count,
808 device_info_ptr, device_info_count, mode);
812 printf("Unallocated:\n");
813 print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
817 static int print_filesystem_usage_by_chunk(int fd,
818 struct chunk_info *chunkinfo, int chunkcount,
819 struct device_info *devinfo, int devcount,
820 char *path, int mode, int tabular)
822 struct btrfs_ioctl_space_args *sargs;
828 sargs = load_space_info(fd, path);
835 _cmd_filesystem_usage_tabular(mode, sargs, chunkinfo,
836 chunkcount, devinfo, devcount);
838 _cmd_filesystem_usage_linear(mode, sargs, chunkinfo,
839 chunkcount, devinfo, devcount);
846 const char * const cmd_filesystem_usage_usage[] = {
847 "btrfs filesystem usage [-b][-t] <path> [<path>..]",
848 "Show in which disk the chunks are allocated.",
850 "-b\tSet byte as unit",
851 "-t\tShow data in tabular format",
855 int cmd_filesystem_usage(int argc, char **argv)
857 int mode = UNITS_HUMAN;
859 int i, more_than_one = 0;
864 int c = getopt(argc, argv, "bt");
877 usage(cmd_filesystem_usage_usage);
881 if (check_argc_min(argc - optind, 1))
882 usage(cmd_filesystem_usage_usage);
884 for (i = optind; i < argc; i++) {
886 DIR *dirstream = NULL;
887 struct chunk_info *chunkinfo = NULL;
888 struct device_info *devinfo = NULL;
892 fd = open_file_or_dir(argv[i], &dirstream);
894 fprintf(stderr, "ERROR: can't access '%s'\n",
902 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
903 &devinfo, &devcount);
907 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
908 devinfo, devcount, argv[i], mode);
912 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
913 devinfo, devcount, argv[i], mode, tabular);
915 close_file_or_dir(fd, dirstream);
928 void print_device_chunks(int fd, struct device_info *devinfo,
929 struct chunk_info *chunks_info_ptr,
930 int chunks_info_count, int mode)
935 for (i = 0 ; i < chunks_info_count ; i++) {
936 const char *description;
941 if (chunks_info_ptr[i].devid != devinfo->devid)
944 flags = chunks_info_ptr[i].type;
946 description = btrfs_group_type_str(flags);
947 r_mode = btrfs_group_profile_str(flags);
948 size = calc_chunk_size(chunks_info_ptr+i);
949 printf(" %s,%s:%*s%10s\n",
952 (int)(20 - strlen(description) - strlen(r_mode)), "",
953 pretty_size_mode(size, mode));
958 printf(" Unallocated: %*s%10s\n",
959 (int)(20 - strlen("Unallocated")), "",
960 pretty_size_mode(devinfo->size - allocated, mode));
963 void print_device_sizes(int fd, struct device_info *devinfo, int mode)
965 printf(" Device size: %*s%10s\n",
966 (int)(20 - strlen("Device size")), "",
967 pretty_size_mode(devinfo->device_size, mode));
970 * The term has not seen an agreement and we don't want to change it
971 * once it's in non-development branches or even released.
973 printf(" FS occupied: %*s%10s\n",
974 (int)(20 - strlen("FS occupied")), "",
975 pretty_size_mode(devinfo->size, mode));