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 * Pretty print the size
36 * PAY ATTENTION: it return a statically buffer
38 char *df_pretty_sizes(u64 size, int mode)
42 if (mode & DF_HUMAN_UNIT)
43 (void)pretty_size_snprintf(size, buf, sizeof(buf), UNITS_DEFAULT);
45 sprintf(buf, "%llu", size);
51 * Add the chunk info to the chunk_info list
53 static int add_info_to_list(struct chunk_info **info_ptr,
55 struct btrfs_chunk *chunk)
58 u64 type = btrfs_stack_chunk_type(chunk);
59 u64 size = btrfs_stack_chunk_length(chunk);
60 int num_stripes = btrfs_stack_chunk_num_stripes(chunk);
63 for (j = 0 ; j < num_stripes ; j++) {
65 struct chunk_info *p = 0;
66 struct btrfs_stripe *stripe;
69 stripe = btrfs_stripe_nr(chunk, j);
70 devid = btrfs_stack_stripe_devid(stripe);
72 for (i = 0 ; i < *info_count ; i++)
73 if ((*info_ptr)[i].type == type &&
74 (*info_ptr)[i].devid == devid &&
75 (*info_ptr)[i].num_stripes == num_stripes ) {
81 int size = sizeof(struct btrfs_chunk) * (*info_count+1);
82 struct chunk_info *res = realloc(*info_ptr, size);
86 fprintf(stderr, "ERROR: not enough memory\n");
91 p = res + *info_count;
97 p->num_stripes = num_stripes;
109 * Helper to sort the chunk type
111 static int cmp_chunk_block_group(u64 f1, u64 f2)
116 if ((f1 & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
117 (f2 & BTRFS_BLOCK_GROUP_TYPE_MASK))
118 mask = BTRFS_BLOCK_GROUP_PROFILE_MASK;
119 else if (f2 & BTRFS_BLOCK_GROUP_SYSTEM)
121 else if (f1 & BTRFS_BLOCK_GROUP_SYSTEM)
124 mask = BTRFS_BLOCK_GROUP_TYPE_MASK;
126 if ((f1 & mask) > (f2 & mask))
128 else if ((f1 & mask) < (f2 & mask))
135 * Helper to sort the chunk
137 static int cmp_chunk_info(const void *a, const void *b)
139 return cmp_chunk_block_group(
140 ((struct chunk_info *)a)->type,
141 ((struct chunk_info *)b)->type);
144 int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
147 struct btrfs_ioctl_search_args args;
148 struct btrfs_ioctl_search_key *sk = &args.key;
149 struct btrfs_ioctl_search_header *sh;
150 unsigned long off = 0;
153 memset(&args, 0, sizeof(args));
156 * there may be more than one ROOT_ITEM key if there are
157 * snapshots pending deletion, we have to loop through
160 sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
162 sk->min_objectid = 0;
163 sk->max_objectid = (u64)-1;
165 sk->min_type = (u8)-1;
167 sk->max_offset = (u64)-1;
169 sk->max_transid = (u64)-1;
173 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
177 "ERROR: can't read detailed chunk info from ioctl(TREE_SEARCH), run as root\n");
183 "ERROR: can't perform the search - %s\n",
187 /* the ioctl returns the number of item it found in nr_items */
189 if (sk->nr_items == 0)
193 for (i = 0; i < sk->nr_items; i++) {
194 struct btrfs_chunk *item;
195 sh = (struct btrfs_ioctl_search_header *)(args.buf +
199 item = (struct btrfs_chunk *)(args.buf + off);
201 if (add_info_to_list(info_ptr, info_count, item)) {
208 sk->min_objectid = sh->objectid;
209 sk->min_type = sh->type;
210 sk->min_offset = sh->offset+1;
213 if (!sk->min_offset) /* overflow */
223 if (!sk->min_objectid)
227 qsort(*info_ptr, *info_count, sizeof(struct chunk_info),
234 * Helper to sort the struct btrfs_ioctl_space_info
236 static int cmp_btrfs_ioctl_space_info(const void *a, const void *b)
238 return cmp_chunk_block_group(
239 ((struct btrfs_ioctl_space_info *)a)->flags,
240 ((struct btrfs_ioctl_space_info *)b)->flags);
244 * This function load all the information about the space usage
246 static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path)
248 struct btrfs_ioctl_space_args *sargs = 0, *sargs_orig = 0;
251 sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
253 fprintf(stderr, "ERROR: not enough memory\n");
257 sargs->space_slots = 0;
258 sargs->total_spaces = 0;
260 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
264 "ERROR: couldn't get space info on '%s' - %s\n",
269 if (!sargs->total_spaces) {
271 printf("No chunks found\n");
275 count = sargs->total_spaces;
277 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
278 (count * sizeof(struct btrfs_ioctl_space_info)));
281 fprintf(stderr, "ERROR: not enough memory\n");
285 sargs->space_slots = count;
286 sargs->total_spaces = 0;
288 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
293 "ERROR: couldn't get space info on '%s' - %s\n",
299 qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
300 cmp_btrfs_ioctl_space_info);
305 /* Not used, keep for later */
308 * This function computes the space occuped by a *single* RAID5/RAID6 chunk.
309 * The computation is performed on the basis of the number of stripes
310 * which compose the chunk, which could be different from the number of devices
311 * if a disk is added later.
313 static int get_raid56_used(int fd, u64 *raid5_used, u64 *raid6_used)
315 struct chunk_info *info_ptr=0, *p;
319 *raid5_used = *raid6_used =0;
321 ret = load_chunk_info(fd, &info_ptr, &info_count);
325 for ( p = info_ptr; info_count ; info_count--, p++ ) {
326 if (p->type & BTRFS_BLOCK_GROUP_RAID5)
327 (*raid5_used) += p->size / (p->num_stripes -1);
328 if (p->type & BTRFS_BLOCK_GROUP_RAID6)
329 (*raid6_used) += p->size / (p->num_stripes -2);
337 static int _cmd_disk_free(int fd, char *path, int mode)
339 struct btrfs_ioctl_space_args *sargs = 0;
343 u64 total_disk; /* filesystem size == sum of
345 u64 total_chunks; /* sum of chunks sizes on disk(s) */
346 u64 total_used; /* logical space used */
347 u64 total_free; /* logical space un-used */
349 u64 raid5_used, raid6_used;
351 if ((sargs = load_space_info(fd, path)) == NULL) {
356 total_disk = disk_size(path);
358 if (total_disk == 0) {
360 "ERROR: couldn't get space info on '%s' - %s\n",
366 if (get_raid56_used(fd, &raid5_used, &raid6_used) < 0) {
368 "ERROR: couldn't get space info on '%s'\n",
374 total_chunks = total_used = total_free = 0;
376 for (i = 0; i < sargs->total_spaces; i++) {
379 u64 flags = sargs->spaces[i].flags;
382 * The raid5/raid6 ratio depends by the stripes number
383 * used by every chunk. It is computed separately
385 if (flags & BTRFS_BLOCK_GROUP_RAID0)
387 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
389 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
391 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
393 else if (flags & BTRFS_BLOCK_GROUP_DUP)
395 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
400 allocated = sargs->spaces[i].total_bytes * ratio;
402 total_chunks += allocated;
403 total_used += sargs->spaces[i].used_bytes;
404 total_free += (sargs->spaces[i].total_bytes -
405 sargs->spaces[i].used_bytes);
409 /* add the raid5/6 allocated space */
410 total_chunks += raid5_used + raid6_used;
412 K = ((double)total_used + (double)total_free) / (double)total_chunks;
414 if (mode & DF_HUMAN_UNIT)
419 printf("Disk size:\t\t%*s\n", width,
420 df_pretty_sizes(total_disk, mode));
421 printf("Disk allocated:\t\t%*s\n", width,
422 df_pretty_sizes(total_chunks, mode));
423 printf("Disk unallocated:\t%*s\n", width,
424 df_pretty_sizes(total_disk-total_chunks, mode));
425 printf("Used:\t\t\t%*s\n", width,
426 df_pretty_sizes(total_used, mode));
427 printf("Free (Estimated):\t%*s\t(",
429 df_pretty_sizes((u64)(K*total_disk-total_used), mode));
431 df_pretty_sizes(total_disk-total_chunks+total_free, mode));
433 df_pretty_sizes((total_disk-total_chunks)/2+total_free, mode));
434 printf("Data to disk ratio:\t%*.0f %%\n",
447 * Helper to sort the device_info structure
449 static int cmp_device_info(const void *a, const void *b)
451 return strcmp(((struct device_info *)a)->path,
452 ((struct device_info *)b)->path);
456 * This function loads the device_info structure and put them in an array
458 int load_device_info(int fd, struct device_info **device_info_ptr,
459 int *device_info_count)
462 struct btrfs_ioctl_fs_info_args fi_args;
463 struct btrfs_ioctl_dev_info_args dev_info;
464 struct device_info *info;
466 *device_info_count = 0;
467 *device_info_ptr = 0;
469 ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
471 fprintf(stderr, "ERROR: can't get filesystem info from ioctl(FS_INFO), run as root\n");
475 fprintf(stderr, "ERROR: cannot get filesystem info\n");
479 info = calloc(fi_args.num_devices, sizeof(struct device_info));
481 fprintf(stderr, "ERROR: not enough memory\n");
485 for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
486 BUG_ON(ndevs >= fi_args.num_devices);
487 memset(&dev_info, 0, sizeof(dev_info));
488 ret = get_device_info(fd, i, &dev_info);
494 "ERROR: cannot get info about device devid=%d\n",
500 info[ndevs].devid = dev_info.devid;
501 strcpy(info[ndevs].path, (char *)dev_info.path);
502 info[ndevs].device_size = get_partition_size((char *)dev_info.path);
503 info[ndevs].size = dev_info.total_bytes;
507 BUG_ON(ndevs != fi_args.num_devices);
508 qsort(info, fi_args.num_devices,
509 sizeof(struct device_info), cmp_device_info);
511 *device_info_count = fi_args.num_devices;
512 *device_info_ptr = info;
518 * This function computes the size of a chunk in a disk
520 static u64 calc_chunk_size(struct chunk_info *ci)
522 if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
523 return ci->size / ci->num_stripes;
524 else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
526 else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
528 else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
529 return ci->size / (ci->num_stripes -1);
530 else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
531 return ci->size / (ci->num_stripes -2);
532 else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
533 return ci->size / ci->num_stripes;
538 * This function print the results of the command "btrfs fi usage"
541 static void _cmd_filesystem_usage_tabular(int mode,
542 struct btrfs_ioctl_space_args *sargs,
543 struct chunk_info *chunks_info_ptr,
544 int chunks_info_count,
545 struct device_info *device_info_ptr,
546 int device_info_count)
549 u64 total_unused = 0;
550 struct string_table *matrix = 0;
553 ncols = sargs->total_spaces + 2;
554 nrows = 2 + 1 + device_info_count + 1 + 2;
556 matrix = table_create(ncols, nrows);
558 fprintf(stderr, "ERROR: not enough memory\n");
563 for (i = 0; i < sargs->total_spaces; i++) {
564 const char *description;
566 u64 flags = sargs->spaces[i].flags;
567 description = btrfs_group_type_str(flags);
569 table_printf(matrix, 1+i, 0, "<%s", description);
572 for (i = 0; i < sargs->total_spaces; i++) {
575 u64 flags = sargs->spaces[i].flags;
576 r_mode = btrfs_group_profile_str(flags);
578 table_printf(matrix, 1+i, 1, "<%s", r_mode);
581 table_printf(matrix, 1+sargs->total_spaces, 1, "<Unallocated");
584 for (i = 0; i < device_info_count; i++) {
588 u64 total_allocated = 0, unused;
590 p = strrchr(device_info_ptr[i].path, '/');
592 p = device_info_ptr[i].path;
596 table_printf(matrix, 0, i + 3, "<%s", device_info_ptr[i].path);
598 for (col = 1, k = 0 ; k < sargs->total_spaces ; k++) {
599 u64 flags = sargs->spaces[k].flags;
600 u64 devid = device_info_ptr[i].devid;
604 for (j = 0 ; j < chunks_info_count ; j++) {
605 if (chunks_info_ptr[j].type != flags )
607 if (chunks_info_ptr[j].devid != devid)
610 size += calc_chunk_size(chunks_info_ptr+j);
614 table_printf(matrix, col, i+3,
615 ">%s", df_pretty_sizes(size, mode));
617 table_printf(matrix, col, i+3, ">-");
619 total_allocated += size;
623 unused = get_partition_size(device_info_ptr[i].path)
626 table_printf(matrix, sargs->total_spaces + 1, i + 3,
627 ">%s", df_pretty_sizes(unused, mode));
628 total_unused += unused;
632 for (i = 0; i <= sargs->total_spaces; i++)
633 table_printf(matrix, i + 1, device_info_count + 3, "=");
636 table_printf(matrix, 0, device_info_count + 4, "<Total");
637 for (i = 0; i < sargs->total_spaces; i++)
638 table_printf(matrix, 1 + i, device_info_count + 4, ">%s",
639 df_pretty_sizes(sargs->spaces[i].total_bytes, mode));
641 table_printf(matrix, sargs->total_spaces + 1, device_info_count + 4,
642 ">%s", df_pretty_sizes(total_unused, mode));
644 table_printf(matrix, 0, device_info_count + 5, "<Used");
645 for (i = 0; i < sargs->total_spaces; i++)
646 table_printf(matrix, 1 + i, device_info_count+5, ">%s",
647 df_pretty_sizes(sargs->spaces[i].used_bytes, mode));
654 * This function prints the unused space per every disk
656 static void print_unused(struct chunk_info *info_ptr,
658 struct device_info *device_info_ptr,
659 int device_info_count,
663 for (i = 0; i < device_info_count; i++) {
667 for (j = 0; j < info_count; j++)
668 if (info_ptr[j].devid == device_info_ptr[i].devid)
669 total += calc_chunk_size(info_ptr+j);
671 printf(" %s\t%10s\n",
672 device_info_ptr[i].path,
673 df_pretty_sizes(device_info_ptr[i].size - total, mode));
678 * This function prints the allocated chunk per every disk
680 static void print_chunk_device(u64 chunk_type,
681 struct chunk_info *chunks_info_ptr,
682 int chunks_info_count,
683 struct device_info *device_info_ptr,
684 int device_info_count,
689 for (i = 0; i < device_info_count; i++) {
693 for (j = 0; j < chunks_info_count; j++) {
695 if (chunks_info_ptr[j].type != chunk_type)
697 if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
700 total += calc_chunk_size(&(chunks_info_ptr[j]));
701 //total += chunks_info_ptr[j].size;
705 printf(" %s\t%10s\n",
706 device_info_ptr[i].path,
707 df_pretty_sizes(total, mode));
712 * This function print the results of the command "btrfs fi usage"
715 static void _cmd_filesystem_usage_linear(int mode,
716 struct btrfs_ioctl_space_args *sargs,
717 struct chunk_info *info_ptr,
719 struct device_info *device_info_ptr,
720 int device_info_count)
724 for (i = 0; i < sargs->total_spaces; i++) {
725 const char *description;
728 u64 flags = sargs->spaces[i].flags;
729 description = btrfs_group_type_str(flags);
730 r_mode = btrfs_group_profile_str(flags);
732 printf("%s,%s: Size:%s, ",
735 df_pretty_sizes(sargs->spaces[i].total_bytes ,
738 df_pretty_sizes(sargs->spaces[i].used_bytes, mode));
739 print_chunk_device(flags, info_ptr, info_count,
740 device_info_ptr, device_info_count, mode);
744 printf("Unallocated:\n");
745 print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
749 static int _cmd_filesystem_usage(int fd, char *path, int mode, int tabular)
751 struct btrfs_ioctl_space_args *sargs = 0;
753 struct chunk_info *info_ptr = 0;
754 struct device_info *device_info_ptr = 0;
755 int device_info_count = 0;
758 if (load_chunk_info(fd, &info_ptr, &info_count) ||
759 load_device_info(fd, &device_info_ptr, &device_info_count)) {
764 if ((sargs = load_space_info(fd, path)) == NULL) {
770 _cmd_filesystem_usage_tabular(mode, sargs,
771 info_ptr, info_count,
772 device_info_ptr, device_info_count);
774 _cmd_filesystem_usage_linear(mode, sargs,
775 info_ptr, info_count,
776 device_info_ptr, device_info_count);
783 free(device_info_ptr);
790 const char * const cmd_filesystem_usage_usage[] = {
791 "btrfs filesystem usage [-b][-t] <path> [<path>..]",
792 "Show in which disk the chunks are allocated.",
794 "-b\tSet byte as unit",
795 "-t\tShow data in tabular format",
799 int cmd_filesystem_usage(int argc, char **argv)
802 int flags = DF_HUMAN_UNIT;
803 int i, more_than_one = 0;
808 char c = getopt(argc, argv, "bt");
813 flags &= ~DF_HUMAN_UNIT;
819 usage(cmd_filesystem_usage_usage);
823 if (check_argc_min(argc - optind, 1))
824 usage(cmd_filesystem_usage_usage);
826 for (i = optind; i < argc ; i++) {
828 DIR *dirstream = NULL;
832 fd = open_file_or_dir(argv[i], &dirstream);
834 fprintf(stderr, "ERROR: can't access to '%s'\n",
838 r = _cmd_filesystem_usage(fd, argv[i], flags, tabular);
839 close_file_or_dir(fd, dirstream);
850 void print_device_chunks(int fd, u64 devid, u64 total_size,
851 struct chunk_info *chunks_info_ptr,
852 int chunks_info_count, int mode)
857 for (i = 0 ; i < chunks_info_count ; i++) {
858 const char *description;
863 if (chunks_info_ptr[i].devid != devid)
866 flags = chunks_info_ptr[i].type;
868 description = btrfs_group_type_str(flags);
869 r_mode = btrfs_group_profile_str(flags);
870 size = calc_chunk_size(chunks_info_ptr+i);
871 printf(" %s,%s:%*s%10s\n",
874 (int)(20 - strlen(description) - strlen(r_mode)), "",
875 df_pretty_sizes(size, mode));
880 printf(" Unallocated: %*s%10s\n",
881 (int)(20 - strlen("Unallocated")), "",
882 df_pretty_sizes(total_size - allocated, mode));
885 void print_device_sizes(int fd, struct device_info *devinfo, int mode)
887 printf(" Device size: %*s%10s\n",
888 (int)(20 - strlen("Device size")), "",
889 df_pretty_sizes(devinfo->device_size, mode));
890 printf(" FS occupied: %*s%10s\n",
891 (int)(20 - strlen("FS occupied")), "",
892 df_pretty_sizes(devinfo->size, mode));