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 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
310 int chunkcount, struct device_info *devinfo, int devcount,
311 char *path, int mode)
313 struct btrfs_ioctl_space_args *sargs = 0;
317 u64 total_disk; /* filesystem size == sum of
319 u64 total_chunks; /* sum of chunks sizes on disk(s) */
320 u64 total_used; /* logical space used */
321 u64 total_free; /* logical space un-used */
323 u64 raid5_used, raid6_used;
325 u64 global_reserve_used;
327 sargs = load_space_info(fd, path);
333 total_disk = disk_size(path);
335 if (total_disk == 0) {
337 "ERROR: couldn't get space info on '%s' - %s\n",
343 get_raid56_used(fd, chunkinfo, chunkcount, &raid5_used, &raid6_used);
349 global_reserve_used = 0;
351 for (i = 0; i < sargs->total_spaces; i++) {
354 u64 flags = sargs->spaces[i].flags;
357 * The raid5/raid6 ratio depends by the stripes number
358 * used by every chunk. It is computed separately
360 if (flags & BTRFS_BLOCK_GROUP_RAID0)
362 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
364 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
366 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
368 else if (flags & BTRFS_BLOCK_GROUP_DUP)
370 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
375 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV) {
376 global_reserve = sargs->spaces[i].total_bytes;
377 global_reserve_used = sargs->spaces[i].used_bytes;
380 allocated = sargs->spaces[i].total_bytes * ratio;
382 total_chunks += allocated;
383 total_used += sargs->spaces[i].used_bytes;
384 total_free += (sargs->spaces[i].total_bytes -
385 sargs->spaces[i].used_bytes);
389 /* add the raid5/6 allocated space */
390 total_chunks += raid5_used + raid6_used;
392 K = ((double)total_used + (double)total_free) / (double)total_chunks;
394 if (mode == UNITS_HUMAN)
399 printf("Overall:\n");
401 printf(" Device size:\t\t%*s\n", width,
402 pretty_size_mode(total_disk, mode));
403 printf(" Device allocated:\t\t%*s\n", width,
404 pretty_size_mode(total_chunks, mode));
405 printf(" Device unallocated:\t\t%*s\n", width,
406 pretty_size_mode(total_disk - total_chunks, mode));
407 printf(" Used:\t\t\t%*s\n", width,
408 pretty_size_mode(total_used, mode));
409 printf(" Free (Estimated):\t\t%*s\t(",
411 pretty_size_mode((u64)(K * total_disk - total_used), mode));
413 pretty_size_mode(total_disk - total_chunks + total_free, mode));
415 pretty_size_mode((total_disk-total_chunks) / 2 + total_free, mode));
416 printf(" Data to device ratio:\t%*.0f %%\n",
418 printf(" Global reserve:\t\t%*s\t(used: %s)\n", width,
419 pretty_size_mode(global_reserve, mode),
420 pretty_size_mode(global_reserve_used, mode));
431 * Helper to sort the device_info structure
433 static int cmp_device_info(const void *a, const void *b)
435 return strcmp(((struct device_info *)a)->path,
436 ((struct device_info *)b)->path);
440 * This function loads the device_info structure and put them in an array
442 static int load_device_info(int fd, struct device_info **device_info_ptr,
443 int *device_info_count)
446 struct btrfs_ioctl_fs_info_args fi_args;
447 struct btrfs_ioctl_dev_info_args dev_info;
448 struct device_info *info;
450 *device_info_count = 0;
451 *device_info_ptr = 0;
453 ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
457 fprintf(stderr, "ERROR: cannot get filesystem info\n");
461 info = calloc(fi_args.num_devices, sizeof(struct device_info));
463 fprintf(stderr, "ERROR: not enough memory\n");
467 for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
468 BUG_ON(ndevs >= fi_args.num_devices);
469 memset(&dev_info, 0, sizeof(dev_info));
470 ret = get_device_info(fd, i, &dev_info);
476 "ERROR: cannot get info about device devid=%d\n",
482 info[ndevs].devid = dev_info.devid;
483 strcpy(info[ndevs].path, (char *)dev_info.path);
484 info[ndevs].device_size = get_partition_size((char *)dev_info.path);
485 info[ndevs].size = dev_info.total_bytes;
489 BUG_ON(ndevs != fi_args.num_devices);
490 qsort(info, fi_args.num_devices,
491 sizeof(struct device_info), cmp_device_info);
493 *device_info_count = fi_args.num_devices;
494 *device_info_ptr = info;
499 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
500 int *chunkcount, struct device_info **devinfo, int *devcount)
504 ret = load_chunk_info(fd, chunkinfo, chunkcount);
507 "WARNING: can't read detailed chunk info, RAID5/6 numbers will be incorrect, run as root\n");
512 ret = load_device_info(fd, devinfo, devcount);
515 "WARNING: can't get filesystem info from ioctl(FS_INFO), run as root\n");
523 * This function computes the size of a chunk in a disk
525 static u64 calc_chunk_size(struct chunk_info *ci)
527 if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
528 return ci->size / ci->num_stripes;
529 else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
531 else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
533 else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
534 return ci->size / (ci->num_stripes -1);
535 else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
536 return ci->size / (ci->num_stripes -2);
537 else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
538 return ci->size / ci->num_stripes;
543 * This function print the results of the command "btrfs fi usage"
546 static void _cmd_filesystem_usage_tabular(int mode,
547 struct btrfs_ioctl_space_args *sargs,
548 struct chunk_info *chunks_info_ptr,
549 int chunks_info_count,
550 struct device_info *device_info_ptr,
551 int device_info_count)
554 u64 total_unused = 0;
555 struct string_table *matrix = 0;
558 ncols = sargs->total_spaces + 2;
559 nrows = 2 + 1 + device_info_count + 1 + 2;
561 matrix = table_create(ncols, nrows);
563 fprintf(stderr, "ERROR: not enough memory\n");
568 for (i = 0; i < sargs->total_spaces; i++) {
569 const char *description;
570 u64 flags = sargs->spaces[i].flags;
572 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
575 description = btrfs_group_type_str(flags);
577 table_printf(matrix, 1+i, 0, "<%s", description);
580 for (i = 0; i < sargs->total_spaces; i++) {
583 u64 flags = sargs->spaces[i].flags;
584 r_mode = btrfs_group_profile_str(flags);
586 table_printf(matrix, 1+i, 1, "<%s", r_mode);
589 table_printf(matrix, 1+sargs->total_spaces, 1, "<Unallocated");
592 for (i = 0; i < device_info_count; i++) {
596 u64 total_allocated = 0, unused;
598 p = strrchr(device_info_ptr[i].path, '/');
600 p = device_info_ptr[i].path;
604 table_printf(matrix, 0, i + 3, "<%s", device_info_ptr[i].path);
606 for (col = 1, k = 0 ; k < sargs->total_spaces ; k++) {
607 u64 flags = sargs->spaces[k].flags;
608 u64 devid = device_info_ptr[i].devid;
612 for (j = 0 ; j < chunks_info_count ; j++) {
613 if (chunks_info_ptr[j].type != flags )
615 if (chunks_info_ptr[j].devid != devid)
618 size += calc_chunk_size(chunks_info_ptr+j);
622 table_printf(matrix, col, i+3,
623 ">%s", pretty_size_mode(size, mode));
625 table_printf(matrix, col, i+3, ">-");
627 total_allocated += size;
631 unused = get_partition_size(device_info_ptr[i].path)
634 table_printf(matrix, sargs->total_spaces + 1, i + 3,
635 ">%s", pretty_size_mode(unused, mode));
636 total_unused += unused;
640 for (i = 0; i <= sargs->total_spaces; i++)
641 table_printf(matrix, i + 1, device_info_count + 3, "=");
644 table_printf(matrix, 0, device_info_count + 4, "<Total");
645 for (i = 0; i < sargs->total_spaces; i++)
646 table_printf(matrix, 1 + i, device_info_count + 4, ">%s",
647 pretty_size_mode(sargs->spaces[i].total_bytes, mode));
649 table_printf(matrix, sargs->total_spaces + 1, device_info_count + 4,
650 ">%s", pretty_size_mode(total_unused, mode));
652 table_printf(matrix, 0, device_info_count + 5, "<Used");
653 for (i = 0; i < sargs->total_spaces; i++)
654 table_printf(matrix, 1 + i, device_info_count+5, ">%s",
655 pretty_size_mode(sargs->spaces[i].used_bytes, mode));
662 * This function prints the unused space per every disk
664 static void print_unused(struct chunk_info *info_ptr,
666 struct device_info *device_info_ptr,
667 int device_info_count,
671 for (i = 0; i < device_info_count; i++) {
675 for (j = 0; j < info_count; j++)
676 if (info_ptr[j].devid == device_info_ptr[i].devid)
677 total += calc_chunk_size(info_ptr+j);
679 printf(" %s\t%10s\n",
680 device_info_ptr[i].path,
681 pretty_size_mode(device_info_ptr[i].size - total, mode));
686 * This function prints the allocated chunk per every disk
688 static void print_chunk_device(u64 chunk_type,
689 struct chunk_info *chunks_info_ptr,
690 int chunks_info_count,
691 struct device_info *device_info_ptr,
692 int device_info_count,
697 for (i = 0; i < device_info_count; i++) {
701 for (j = 0; j < chunks_info_count; j++) {
703 if (chunks_info_ptr[j].type != chunk_type)
705 if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
708 total += calc_chunk_size(&(chunks_info_ptr[j]));
709 //total += chunks_info_ptr[j].size;
713 printf(" %s\t%10s\n",
714 device_info_ptr[i].path,
715 pretty_size_mode(total, mode));
720 * This function print the results of the command "btrfs fi usage"
723 static void _cmd_filesystem_usage_linear(int mode,
724 struct btrfs_ioctl_space_args *sargs,
725 struct chunk_info *info_ptr,
727 struct device_info *device_info_ptr,
728 int device_info_count)
732 for (i = 0; i < sargs->total_spaces; i++) {
733 const char *description;
735 u64 flags = sargs->spaces[i].flags;
737 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
740 description = btrfs_group_type_str(flags);
741 r_mode = btrfs_group_profile_str(flags);
743 printf("%s,%s: Size:%s, ",
746 pretty_size_mode(sargs->spaces[i].total_bytes,
749 pretty_size_mode(sargs->spaces[i].used_bytes, mode));
750 print_chunk_device(flags, info_ptr, info_count,
751 device_info_ptr, device_info_count, mode);
755 printf("Unallocated:\n");
756 print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
760 static int print_filesystem_usage_by_chunk(int fd,
761 struct chunk_info *chunkinfo, int chunkcount,
762 struct device_info *devinfo, int devcount,
763 char *path, int mode, int tabular)
765 struct btrfs_ioctl_space_args *sargs;
771 sargs = load_space_info(fd, path);
778 _cmd_filesystem_usage_tabular(mode, sargs, chunkinfo,
779 chunkcount, devinfo, devcount);
781 _cmd_filesystem_usage_linear(mode, sargs, chunkinfo,
782 chunkcount, devinfo, devcount);
789 const char * const cmd_filesystem_usage_usage[] = {
790 "btrfs filesystem usage [-b][-t] <path> [<path>..]",
791 "Show in which disk the chunks are allocated.",
793 "-b\tSet byte as unit",
794 "-t\tShow data in tabular format",
798 int cmd_filesystem_usage(int argc, char **argv)
800 int mode = UNITS_HUMAN;
802 int i, more_than_one = 0;
807 int c = getopt(argc, argv, "bt");
820 usage(cmd_filesystem_usage_usage);
824 if (check_argc_min(argc - optind, 1))
825 usage(cmd_filesystem_usage_usage);
827 for (i = optind; i < argc; i++) {
829 DIR *dirstream = NULL;
830 struct chunk_info *chunkinfo = NULL;
831 struct device_info *devinfo = NULL;
835 fd = open_file_or_dir(argv[i], &dirstream);
837 fprintf(stderr, "ERROR: can't access '%s'\n",
845 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
846 &devinfo, &devcount);
850 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
851 devinfo, devcount, argv[i], mode);
855 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
856 devinfo, devcount, argv[i], mode, tabular);
858 close_file_or_dir(fd, dirstream);
871 void print_device_chunks(int fd, struct device_info *devinfo,
872 struct chunk_info *chunks_info_ptr,
873 int chunks_info_count, int mode)
878 for (i = 0 ; i < chunks_info_count ; i++) {
879 const char *description;
884 if (chunks_info_ptr[i].devid != devinfo->devid)
887 flags = chunks_info_ptr[i].type;
889 description = btrfs_group_type_str(flags);
890 r_mode = btrfs_group_profile_str(flags);
891 size = calc_chunk_size(chunks_info_ptr+i);
892 printf(" %s,%s:%*s%10s\n",
895 (int)(20 - strlen(description) - strlen(r_mode)), "",
896 pretty_size_mode(size, mode));
901 printf(" Unallocated: %*s%10s\n",
902 (int)(20 - strlen("Unallocated")), "",
903 pretty_size_mode(devinfo->size - allocated, mode));
906 void print_device_sizes(int fd, struct device_info *devinfo, int mode)
908 printf(" Device size: %*s%10s\n",
909 (int)(20 - strlen("Device size")), "",
910 pretty_size_mode(devinfo->device_size, mode));
913 * The term has not seen an agreement and we don't want to change it
914 * once it's in non-development branches or even released.
916 printf(" FS occupied: %*s%10s\n",
917 (int)(20 - strlen("FS occupied")), "",
918 pretty_size_mode(devinfo->size, mode));