btrfs-progs: convert: add missing newlines for printfs
[btrfs-progs-unstable/devel.git] / cmds-fi-usage.c
blob101a0c4ef8fb9e9f9d6bf1362705a7bd317c0016
1 /*
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.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/ioctl.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <getopt.h>
26 #include "utils.h"
27 #include "kerncompat.h"
28 #include "ctree.h"
29 #include "string-table.h"
30 #include "cmds-fi-usage.h"
31 #include "commands.h"
33 #include "version.h"
34 #include "help.h"
37 * Add the chunk info to the chunk_info list
39 static int add_info_to_list(struct chunk_info **info_ptr,
40 int *info_count,
41 struct btrfs_chunk *chunk)
44 u64 type = btrfs_stack_chunk_type(chunk);
45 u64 size = btrfs_stack_chunk_length(chunk);
46 int num_stripes = btrfs_stack_chunk_num_stripes(chunk);
47 int j;
49 for (j = 0 ; j < num_stripes ; j++) {
50 int i;
51 struct chunk_info *p = NULL;
52 struct btrfs_stripe *stripe;
53 u64 devid;
55 stripe = btrfs_stripe_nr(chunk, j);
56 devid = btrfs_stack_stripe_devid(stripe);
58 for (i = 0 ; i < *info_count ; i++)
59 if ((*info_ptr)[i].type == type &&
60 (*info_ptr)[i].devid == devid &&
61 (*info_ptr)[i].num_stripes == num_stripes ) {
62 p = (*info_ptr) + i;
63 break;
66 if (!p) {
67 int tmp = sizeof(struct btrfs_chunk) * (*info_count + 1);
68 struct chunk_info *res = realloc(*info_ptr, tmp);
70 if (!res) {
71 free(*info_ptr);
72 error("not enough memory");
73 return -ENOMEM;
76 *info_ptr = res;
77 p = res + *info_count;
78 (*info_count)++;
80 p->devid = devid;
81 p->type = type;
82 p->size = 0;
83 p->num_stripes = num_stripes;
86 p->size += size;
90 return 0;
95 * Helper to sort the chunk type
97 static int cmp_chunk_block_group(u64 f1, u64 f2)
100 u64 mask;
102 if ((f1 & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
103 (f2 & BTRFS_BLOCK_GROUP_TYPE_MASK))
104 mask = BTRFS_BLOCK_GROUP_PROFILE_MASK;
105 else if (f2 & BTRFS_BLOCK_GROUP_SYSTEM)
106 return -1;
107 else if (f1 & BTRFS_BLOCK_GROUP_SYSTEM)
108 return +1;
109 else
110 mask = BTRFS_BLOCK_GROUP_TYPE_MASK;
112 if ((f1 & mask) > (f2 & mask))
113 return +1;
114 else if ((f1 & mask) < (f2 & mask))
115 return -1;
116 else
117 return 0;
121 * Helper to sort the chunk
123 static int cmp_chunk_info(const void *a, const void *b)
125 return cmp_chunk_block_group(
126 ((struct chunk_info *)a)->type,
127 ((struct chunk_info *)b)->type);
130 static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
132 int ret;
133 struct btrfs_ioctl_search_args args;
134 struct btrfs_ioctl_search_key *sk = &args.key;
135 struct btrfs_ioctl_search_header *sh;
136 unsigned long off = 0;
137 int i, e;
139 memset(&args, 0, sizeof(args));
142 * there may be more than one ROOT_ITEM key if there are
143 * snapshots pending deletion, we have to loop through
144 * them.
146 sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
148 sk->min_objectid = 0;
149 sk->max_objectid = (u64)-1;
150 sk->max_type = 0;
151 sk->min_type = (u8)-1;
152 sk->min_offset = 0;
153 sk->max_offset = (u64)-1;
154 sk->min_transid = 0;
155 sk->max_transid = (u64)-1;
156 sk->nr_items = 4096;
158 while (1) {
159 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
160 e = errno;
161 if (e == EPERM)
162 return -e;
164 if (ret < 0) {
165 error("cannot look up chunk tree info: %s",
166 strerror(e));
167 return 1;
169 /* the ioctl returns the number of item it found in nr_items */
171 if (sk->nr_items == 0)
172 break;
174 off = 0;
175 for (i = 0; i < sk->nr_items; i++) {
176 struct btrfs_chunk *item;
177 sh = (struct btrfs_ioctl_search_header *)(args.buf +
178 off);
180 off += sizeof(*sh);
181 item = (struct btrfs_chunk *)(args.buf + off);
183 ret = add_info_to_list(info_ptr, info_count, item);
184 if (ret) {
185 *info_ptr = NULL;
186 return 1;
189 off += btrfs_search_header_len(sh);
191 sk->min_objectid = btrfs_search_header_objectid(sh);
192 sk->min_type = btrfs_search_header_type(sh);
193 sk->min_offset = btrfs_search_header_offset(sh)+1;
196 if (!sk->min_offset) /* overflow */
197 sk->min_type++;
198 else
199 continue;
201 if (!sk->min_type)
202 sk->min_objectid++;
203 else
204 continue;
206 if (!sk->min_objectid)
207 break;
210 qsort(*info_ptr, *info_count, sizeof(struct chunk_info),
211 cmp_chunk_info);
213 return 0;
217 * Helper to sort the struct btrfs_ioctl_space_info
219 static int cmp_btrfs_ioctl_space_info(const void *a, const void *b)
221 return cmp_chunk_block_group(
222 ((struct btrfs_ioctl_space_info *)a)->flags,
223 ((struct btrfs_ioctl_space_info *)b)->flags);
227 * This function load all the information about the space usage
229 static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path)
231 struct btrfs_ioctl_space_args *sargs = NULL, *sargs_orig = NULL;
232 int ret, count;
234 sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
235 if (!sargs) {
236 error("not enough memory");
237 return NULL;
240 sargs->space_slots = 0;
241 sargs->total_spaces = 0;
243 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
244 if (ret < 0) {
245 error("cannot get space info on '%s': %s", path,
246 strerror(errno));
247 free(sargs);
248 return NULL;
250 if (!sargs->total_spaces) {
251 free(sargs);
252 printf("No chunks found\n");
253 return NULL;
256 count = sargs->total_spaces;
258 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
259 (count * sizeof(struct btrfs_ioctl_space_info)));
260 if (!sargs) {
261 free(sargs_orig);
262 error("not enough memory");
263 return NULL;
266 sargs->space_slots = count;
267 sargs->total_spaces = 0;
269 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
270 if (ret < 0) {
271 error("cannot get space info with %u slots: %s",
272 count, strerror(errno));
273 free(sargs);
274 return NULL;
277 qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
278 cmp_btrfs_ioctl_space_info);
280 return sargs;
284 * This function computes the space occupied by a *single* RAID5/RAID6 chunk.
285 * The computation is performed on the basis of the number of stripes
286 * which compose the chunk, which could be different from the number of devices
287 * if a disk is added later.
289 static void get_raid56_used(struct chunk_info *chunks, int chunkcount,
290 u64 *raid5_used, u64 *raid6_used)
292 struct chunk_info *info_ptr = chunks;
293 *raid5_used = 0;
294 *raid6_used = 0;
296 while (chunkcount-- > 0) {
297 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID5)
298 (*raid5_used) += info_ptr->size / (info_ptr->num_stripes - 1);
299 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID6)
300 (*raid6_used) += info_ptr->size / (info_ptr->num_stripes - 2);
301 info_ptr++;
305 #define MIN_UNALOCATED_THRESH SZ_16M
306 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
307 int chunkcount, struct device_info *devinfo, int devcount,
308 char *path, unsigned unit_mode)
310 struct btrfs_ioctl_space_args *sargs = NULL;
311 int i;
312 int ret = 0;
313 int width = 10; /* default 10 for human units */
315 * r_* prefix is for raw data
316 * l_* is for logical
318 u64 r_total_size = 0; /* filesystem size, sum of device sizes */
319 u64 r_total_chunks = 0; /* sum of chunks sizes on disk(s) */
320 u64 r_total_used = 0;
321 u64 r_total_unused = 0;
322 u64 r_total_missing = 0; /* sum of missing devices size */
323 u64 r_data_used = 0;
324 u64 r_data_chunks = 0;
325 u64 l_data_chunks = 0;
326 u64 r_metadata_used = 0;
327 u64 r_metadata_chunks = 0;
328 u64 l_metadata_chunks = 0;
329 u64 r_system_used = 0;
330 u64 r_system_chunks = 0;
331 double data_ratio;
332 double metadata_ratio;
333 /* logical */
334 u64 raid5_used = 0;
335 u64 raid6_used = 0;
336 u64 l_global_reserve = 0;
337 u64 l_global_reserve_used = 0;
338 u64 free_estimated = 0;
339 u64 free_min = 0;
340 int max_data_ratio = 1;
341 int mixed = 0;
343 sargs = load_space_info(fd, path);
344 if (!sargs) {
345 ret = 1;
346 goto exit;
349 r_total_size = 0;
350 for (i = 0; i < devcount; i++) {
351 r_total_size += devinfo[i].size;
352 if (!devinfo[i].device_size)
353 r_total_missing += devinfo[i].size;
356 if (r_total_size == 0) {
357 error("cannot get space info on '%s': %s",
358 path, strerror(errno));
360 ret = 1;
361 goto exit;
363 get_raid56_used(chunkinfo, chunkcount, &raid5_used, &raid6_used);
365 for (i = 0; i < sargs->total_spaces; i++) {
366 int ratio;
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)
374 ratio = 1;
375 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
376 ratio = 2;
377 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
378 ratio = 0;
379 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
380 ratio = 0;
381 else if (flags & BTRFS_BLOCK_GROUP_DUP)
382 ratio = 2;
383 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
384 ratio = 2;
385 else
386 ratio = 1;
388 if (!ratio)
389 warning("RAID56 detected, not implemented");
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 mixed = 1;
402 if (flags & BTRFS_BLOCK_GROUP_DATA) {
403 r_data_used += sargs->spaces[i].used_bytes * ratio;
404 r_data_chunks += sargs->spaces[i].total_bytes * ratio;
405 l_data_chunks += sargs->spaces[i].total_bytes;
407 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
408 r_metadata_used += sargs->spaces[i].used_bytes * ratio;
409 r_metadata_chunks += sargs->spaces[i].total_bytes * ratio;
410 l_metadata_chunks += sargs->spaces[i].total_bytes;
412 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
413 r_system_used += sargs->spaces[i].used_bytes * ratio;
414 r_system_chunks += sargs->spaces[i].total_bytes * ratio;
418 r_total_chunks = r_data_chunks + r_system_chunks;
419 r_total_used = r_data_used + r_system_used;
420 if (!mixed) {
421 r_total_chunks += r_metadata_chunks;
422 r_total_used += r_metadata_used;
424 r_total_unused = r_total_size - r_total_chunks;
426 /* Raw / Logical = raid factor, >= 1 */
427 data_ratio = (double)r_data_chunks / l_data_chunks;
428 if (mixed)
429 metadata_ratio = data_ratio;
430 else
431 metadata_ratio = (double)r_metadata_chunks / l_metadata_chunks;
433 #if 0
434 /* add the raid5/6 allocated space */
435 total_chunks += raid5_used + raid6_used;
436 #endif
439 * We're able to fill at least DATA for the unused space
441 * With mixed raid levels, this gives a rough estimate but more
442 * accurate than just counting the logical free space
443 * (l_data_chunks - l_data_used)
445 * In non-mixed case there's no difference.
447 free_estimated = (r_data_chunks - r_data_used) / data_ratio;
449 * For mixed-bg the metadata are left out in calculations thus global
450 * reserve would be lost. Part of it could be permanently allocated,
451 * we have to subtract the used bytes so we don't go under zero free.
453 if (mixed)
454 free_estimated -= l_global_reserve - l_global_reserve_used;
455 free_min = free_estimated;
457 /* Chop unallocatable space */
458 /* FIXME: must be applied per device */
459 if (r_total_unused >= MIN_UNALOCATED_THRESH) {
460 free_estimated += r_total_unused / data_ratio;
461 /* Match the calculation of 'df', use the highest raid ratio */
462 free_min += r_total_unused / max_data_ratio;
465 if (unit_mode != UNITS_HUMAN)
466 width = 18;
468 printf("Overall:\n");
470 printf(" Device size:\t\t%*s\n", width,
471 pretty_size_mode(r_total_size, unit_mode));
472 printf(" Device allocated:\t\t%*s\n", width,
473 pretty_size_mode(r_total_chunks, unit_mode));
474 printf(" Device unallocated:\t\t%*s\n", width,
475 pretty_size_mode(r_total_unused, unit_mode | UNITS_NEGATIVE));
476 printf(" Device missing:\t\t%*s\n", width,
477 pretty_size_mode(r_total_missing, unit_mode));
478 printf(" Used:\t\t\t%*s\n", width,
479 pretty_size_mode(r_total_used, unit_mode));
480 printf(" Free (estimated):\t\t%*s\t(",
481 width,
482 pretty_size_mode(free_estimated, unit_mode));
483 printf("min: %s)\n", pretty_size_mode(free_min, unit_mode));
484 printf(" Data ratio:\t\t\t%*.2f\n",
485 width, data_ratio);
486 printf(" Metadata ratio:\t\t%*.2f\n",
487 width, metadata_ratio);
488 printf(" Global reserve:\t\t%*s\t(used: %s)\n", width,
489 pretty_size_mode(l_global_reserve, unit_mode),
490 pretty_size_mode(l_global_reserve_used, unit_mode));
492 exit:
494 if (sargs)
495 free(sargs);
497 return ret;
501 * Helper to sort the device_info structure
503 static int cmp_device_info(const void *a, const void *b)
505 return strcmp(((struct device_info *)a)->path,
506 ((struct device_info *)b)->path);
510 * This function loads the device_info structure and put them in an array
512 static int load_device_info(int fd, struct device_info **device_info_ptr,
513 int *device_info_count)
515 int ret, i, ndevs;
516 struct btrfs_ioctl_fs_info_args fi_args;
517 struct btrfs_ioctl_dev_info_args dev_info;
518 struct device_info *info;
520 *device_info_count = 0;
521 *device_info_ptr = NULL;
523 ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
524 if (ret < 0) {
525 if (errno == EPERM)
526 return -errno;
527 error("cannot get filesystem info: %s",
528 strerror(errno));
529 return 1;
532 info = calloc(fi_args.num_devices, sizeof(struct device_info));
533 if (!info) {
534 error("not enough memory");
535 return 1;
538 for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
539 if (ndevs >= fi_args.num_devices) {
540 error("unexpected number of devices: %d >= %llu", ndevs,
541 (unsigned long long)fi_args.num_devices);
542 goto out;
544 memset(&dev_info, 0, sizeof(dev_info));
545 ret = get_device_info(fd, i, &dev_info);
547 if (ret == -ENODEV)
548 continue;
549 if (ret) {
550 error("cannot get info about device devid=%d", i);
551 goto out;
554 info[ndevs].devid = dev_info.devid;
555 if (!dev_info.path[0]) {
556 strcpy(info[ndevs].path, "missing");
557 } else {
558 strcpy(info[ndevs].path, (char *)dev_info.path);
559 info[ndevs].device_size =
560 get_partition_size((char *)dev_info.path);
562 info[ndevs].size = dev_info.total_bytes;
563 ++ndevs;
566 if (ndevs != fi_args.num_devices) {
567 error("unexpected number of devices: %d != %llu", ndevs,
568 (unsigned long long)fi_args.num_devices);
569 goto out;
572 qsort(info, fi_args.num_devices,
573 sizeof(struct device_info), cmp_device_info);
575 *device_info_count = fi_args.num_devices;
576 *device_info_ptr = info;
578 return 0;
580 out:
581 free(info);
582 return ret;
585 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
586 int *chunkcount, struct device_info **devinfo, int *devcount)
588 int ret;
590 ret = load_chunk_info(fd, chunkinfo, chunkcount);
591 if (ret == -EPERM) {
592 warning(
593 "cannot read detailed chunk info, RAID5/6 numbers will be incorrect, run as root");
594 } else if (ret) {
595 return ret;
598 ret = load_device_info(fd, devinfo, devcount);
599 if (ret == -EPERM) {
600 warning(
601 "cannot get filesystem info from ioctl(FS_INFO), run as root");
602 ret = 0;
605 return ret;
609 * This function computes the size of a chunk in a disk
611 static u64 calc_chunk_size(struct chunk_info *ci)
613 if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
614 return ci->size / ci->num_stripes;
615 else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
616 return ci->size ;
617 else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
618 return ci->size ;
619 else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
620 return ci->size / (ci->num_stripes -1);
621 else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
622 return ci->size / (ci->num_stripes -2);
623 else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
624 return ci->size / ci->num_stripes;
625 return ci->size;
629 * This function print the results of the command "btrfs fi usage"
630 * in tabular format
632 static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
633 struct btrfs_ioctl_space_args *sargs,
634 struct chunk_info *chunks_info_ptr,
635 int chunks_info_count,
636 struct device_info *device_info_ptr,
637 int device_info_count)
639 int i;
640 u64 total_unused = 0;
641 struct string_table *matrix = NULL;
642 int ncols, nrows;
643 int col;
644 int unallocated_col;
645 int spaceinfos_col;
646 const int vhdr_skip = 3; /* amount of vertical header space */
648 /* id, path, unallocated */
649 ncols = 3;
650 spaceinfos_col = 2;
651 /* Properly count the real space infos */
652 for (i = 0; i < sargs->total_spaces; i++) {
653 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
654 continue;
655 ncols++;
658 /* 2 for header, empty line, devices, ===, total, used */
659 nrows = vhdr_skip + device_info_count + 1 + 2;
661 matrix = table_create(ncols, nrows);
662 if (!matrix) {
663 error("not enough memory");
664 return;
668 * We have to skip the global block reserve everywhere as it's an
669 * artificial blockgroup
672 /* header */
673 for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
674 u64 flags = sargs->spaces[i].flags;
676 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
677 continue;
679 table_printf(matrix, col, 0, "<%s",
680 btrfs_group_type_str(flags));
681 table_printf(matrix, col, 1, "<%s",
682 btrfs_group_profile_str(flags));
683 col++;
685 unallocated_col = col;
687 table_printf(matrix, 0, 1, "<Id");
688 table_printf(matrix, 1, 1, "<Path");
689 table_printf(matrix, unallocated_col, 1, "<Unallocated");
691 /* body */
692 for (i = 0; i < device_info_count; i++) {
693 int k;
694 char *p;
696 u64 total_allocated = 0, unused;
698 p = strrchr(device_info_ptr[i].path, '/');
699 if (!p)
700 p = device_info_ptr[i].path;
701 else
702 p++;
704 table_printf(matrix, 0, vhdr_skip + i, ">%llu",
705 device_info_ptr[i].devid);
706 table_printf(matrix, 1, vhdr_skip + i, "<%s",
707 device_info_ptr[i].path);
709 for (col = spaceinfos_col, k = 0; k < sargs->total_spaces; k++) {
710 u64 flags = sargs->spaces[k].flags;
711 u64 devid = device_info_ptr[i].devid;
712 int j;
713 u64 size = 0;
715 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
716 continue;
718 for (j = 0 ; j < chunks_info_count ; j++) {
719 if (chunks_info_ptr[j].type != flags )
720 continue;
721 if (chunks_info_ptr[j].devid != devid)
722 continue;
724 size += calc_chunk_size(chunks_info_ptr+j);
727 if (size)
728 table_printf(matrix, col, vhdr_skip+ i,
729 ">%s", pretty_size_mode(size, unit_mode));
730 else
731 table_printf(matrix, col, vhdr_skip + i, ">-");
733 total_allocated += size;
734 col++;
737 unused = get_partition_size(device_info_ptr[i].path)
738 - total_allocated;
740 table_printf(matrix, unallocated_col, vhdr_skip + i, ">%s",
741 pretty_size_mode(unused, unit_mode | UNITS_NEGATIVE));
742 total_unused += unused;
746 for (i = 0; i < spaceinfos_col; i++) {
747 table_printf(matrix, i, vhdr_skip - 1, "*-");
748 table_printf(matrix, i, vhdr_skip + device_info_count, "*-");
751 for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
752 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
753 continue;
755 table_printf(matrix, col, vhdr_skip - 1, "*-");
756 table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
757 col++;
759 /* One for Unallocated */
760 table_printf(matrix, col, vhdr_skip - 1, "*-");
761 table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
763 /* footer */
764 table_printf(matrix, 1, vhdr_skip + device_info_count + 1, "<Total");
765 for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
766 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
767 continue;
769 table_printf(matrix, col++, vhdr_skip + device_info_count + 1,
770 ">%s",
771 pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));
774 table_printf(matrix, unallocated_col, vhdr_skip + device_info_count + 1,
775 ">%s",
776 pretty_size_mode(total_unused, unit_mode | UNITS_NEGATIVE));
778 table_printf(matrix, 1, vhdr_skip + device_info_count + 2, "<Used");
779 for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
780 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
781 continue;
783 table_printf(matrix, col++, vhdr_skip + device_info_count + 2,
784 ">%s",
785 pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
788 table_dump(matrix);
789 table_free(matrix);
793 * This function prints the unused space per every disk
795 static void print_unused(struct chunk_info *info_ptr,
796 int info_count,
797 struct device_info *device_info_ptr,
798 int device_info_count,
799 unsigned unit_mode)
801 int i;
802 for (i = 0; i < device_info_count; i++) {
803 int j;
804 u64 total = 0;
806 for (j = 0; j < info_count; j++)
807 if (info_ptr[j].devid == device_info_ptr[i].devid)
808 total += calc_chunk_size(info_ptr+j);
810 printf(" %s\t%10s\n",
811 device_info_ptr[i].path,
812 pretty_size_mode(device_info_ptr[i].size - total,
813 unit_mode));
818 * This function prints the allocated chunk per every disk
820 static void print_chunk_device(u64 chunk_type,
821 struct chunk_info *chunks_info_ptr,
822 int chunks_info_count,
823 struct device_info *device_info_ptr,
824 int device_info_count,
825 unsigned unit_mode)
827 int i;
829 for (i = 0; i < device_info_count; i++) {
830 int j;
831 u64 total = 0;
833 for (j = 0; j < chunks_info_count; j++) {
835 if (chunks_info_ptr[j].type != chunk_type)
836 continue;
837 if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
838 continue;
840 total += calc_chunk_size(&(chunks_info_ptr[j]));
841 //total += chunks_info_ptr[j].size;
844 if (total > 0)
845 printf(" %s\t%10s\n",
846 device_info_ptr[i].path,
847 pretty_size_mode(total, unit_mode));
852 * This function print the results of the command "btrfs fi usage"
853 * in linear format
855 static void _cmd_filesystem_usage_linear(unsigned unit_mode,
856 struct btrfs_ioctl_space_args *sargs,
857 struct chunk_info *info_ptr,
858 int info_count,
859 struct device_info *device_info_ptr,
860 int device_info_count)
862 int i;
864 for (i = 0; i < sargs->total_spaces; i++) {
865 const char *description;
866 const char *r_mode;
867 u64 flags = sargs->spaces[i].flags;
869 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
870 continue;
872 description = btrfs_group_type_str(flags);
873 r_mode = btrfs_group_profile_str(flags);
875 printf("%s,%s: Size:%s, ",
876 description,
877 r_mode,
878 pretty_size_mode(sargs->spaces[i].total_bytes,
879 unit_mode));
880 printf("Used:%s\n",
881 pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
882 print_chunk_device(flags, info_ptr, info_count,
883 device_info_ptr, device_info_count, unit_mode);
884 printf("\n");
887 printf("Unallocated:\n");
888 print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
889 unit_mode | UNITS_NEGATIVE);
892 static int print_filesystem_usage_by_chunk(int fd,
893 struct chunk_info *chunkinfo, int chunkcount,
894 struct device_info *devinfo, int devcount,
895 char *path, unsigned unit_mode, int tabular)
897 struct btrfs_ioctl_space_args *sargs;
898 int ret = 0;
900 if (!chunkinfo)
901 return 0;
903 sargs = load_space_info(fd, path);
904 if (!sargs) {
905 ret = 1;
906 goto out;
909 if (tabular)
910 _cmd_filesystem_usage_tabular(unit_mode, sargs, chunkinfo,
911 chunkcount, devinfo, devcount);
912 else
913 _cmd_filesystem_usage_linear(unit_mode, sargs, chunkinfo,
914 chunkcount, devinfo, devcount);
916 free(sargs);
917 out:
918 return ret;
921 const char * const cmd_filesystem_usage_usage[] = {
922 "btrfs filesystem usage [options] <path> [<path>..]",
923 "Show detailed information about internal filesystem usage .",
924 HELPINFO_UNITS_SHORT_LONG,
925 "-T show data in tabular format",
926 NULL
929 int cmd_filesystem_usage(int argc, char **argv)
931 int ret = 0;
932 unsigned unit_mode;
933 int i;
934 int more_than_one = 0;
935 int tabular = 0;
937 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
939 while (1) {
940 int c;
942 c = getopt(argc, argv, "T");
943 if (c < 0)
944 break;
946 switch (c) {
947 case 'T':
948 tabular = 1;
949 break;
950 default:
951 usage(cmd_filesystem_usage_usage);
955 if (check_argc_min(argc - optind, 1))
956 usage(cmd_filesystem_usage_usage);
958 for (i = optind; i < argc; i++) {
959 int fd;
960 DIR *dirstream = NULL;
961 struct chunk_info *chunkinfo = NULL;
962 struct device_info *devinfo = NULL;
963 int chunkcount = 0;
964 int devcount = 0;
966 fd = btrfs_open_dir(argv[i], &dirstream, 1);
967 if (fd < 0) {
968 ret = 1;
969 goto out;
971 if (more_than_one)
972 printf("\n");
974 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
975 &devinfo, &devcount);
976 if (ret)
977 goto cleanup;
979 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
980 devinfo, devcount, argv[i], unit_mode);
981 if (ret)
982 goto cleanup;
983 printf("\n");
984 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
985 devinfo, devcount, argv[i], unit_mode, tabular);
986 cleanup:
987 close_file_or_dir(fd, dirstream);
988 free(chunkinfo);
989 free(devinfo);
991 if (ret)
992 goto out;
993 more_than_one = 1;
996 out:
997 return !!ret;
1000 void print_device_chunks(struct device_info *devinfo,
1001 struct chunk_info *chunks_info_ptr,
1002 int chunks_info_count, unsigned unit_mode)
1004 int i;
1005 u64 allocated = 0;
1007 for (i = 0 ; i < chunks_info_count ; i++) {
1008 const char *description;
1009 const char *r_mode;
1010 u64 flags;
1011 u64 size;
1013 if (chunks_info_ptr[i].devid != devinfo->devid)
1014 continue;
1016 flags = chunks_info_ptr[i].type;
1018 description = btrfs_group_type_str(flags);
1019 r_mode = btrfs_group_profile_str(flags);
1020 size = calc_chunk_size(chunks_info_ptr+i);
1021 printf(" %s,%s:%*s%10s\n",
1022 description,
1023 r_mode,
1024 (int)(20 - strlen(description) - strlen(r_mode)), "",
1025 pretty_size_mode(size, unit_mode));
1027 allocated += size;
1030 printf(" Unallocated: %*s%10s\n",
1031 (int)(20 - strlen("Unallocated")), "",
1032 pretty_size_mode(devinfo->size - allocated,
1033 unit_mode | UNITS_NEGATIVE));
1036 void print_device_sizes(struct device_info *devinfo, unsigned unit_mode)
1038 printf(" Device size: %*s%10s\n",
1039 (int)(20 - strlen("Device size")), "",
1040 pretty_size_mode(devinfo->device_size, unit_mode));
1041 printf(" Device slack: %*s%10s\n",
1042 (int)(20 - strlen("Device slack")), "",
1043 pretty_size_mode(devinfo->device_size - devinfo->size,
1044 unit_mode));