btrfs-progs: Cleanup check_tree_block() function
[btrfs-progs-unstable/devel.git] / cmds-fi-disk_usage.c
blob27f9a4c8f213dde84aaef6a428a8347bfa9ee138
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-disk_usage.h"
31 #include "commands.h"
33 #include "version.h"
36 * Add the chunk info to the chunk_info list
38 static int add_info_to_list(struct chunk_info **info_ptr,
39 int *info_count,
40 struct btrfs_chunk *chunk)
43 u64 type = btrfs_stack_chunk_type(chunk);
44 u64 size = btrfs_stack_chunk_length(chunk);
45 int num_stripes = btrfs_stack_chunk_num_stripes(chunk);
46 int j;
48 for (j = 0 ; j < num_stripes ; j++) {
49 int i;
50 struct chunk_info *p = 0;
51 struct btrfs_stripe *stripe;
52 u64 devid;
54 stripe = btrfs_stripe_nr(chunk, j);
55 devid = btrfs_stack_stripe_devid(stripe);
57 for (i = 0 ; i < *info_count ; i++)
58 if ((*info_ptr)[i].type == type &&
59 (*info_ptr)[i].devid == devid &&
60 (*info_ptr)[i].num_stripes == num_stripes ) {
61 p = (*info_ptr) + i;
62 break;
65 if (!p) {
66 int size = sizeof(struct btrfs_chunk) * (*info_count+1);
67 struct chunk_info *res = realloc(*info_ptr, size);
69 if (!res) {
70 free(*info_ptr);
71 fprintf(stderr, "ERROR: not enough memory\n");
72 return -ENOMEM;
75 *info_ptr = res;
76 p = res + *info_count;
77 (*info_count)++;
79 p->devid = devid;
80 p->type = type;
81 p->size = 0;
82 p->num_stripes = num_stripes;
85 p->size += size;
89 return 0;
94 * Helper to sort the chunk type
96 static int cmp_chunk_block_group(u64 f1, u64 f2)
99 u64 mask;
101 if ((f1 & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
102 (f2 & BTRFS_BLOCK_GROUP_TYPE_MASK))
103 mask = BTRFS_BLOCK_GROUP_PROFILE_MASK;
104 else if (f2 & BTRFS_BLOCK_GROUP_SYSTEM)
105 return -1;
106 else if (f1 & BTRFS_BLOCK_GROUP_SYSTEM)
107 return +1;
108 else
109 mask = BTRFS_BLOCK_GROUP_TYPE_MASK;
111 if ((f1 & mask) > (f2 & mask))
112 return +1;
113 else if ((f1 & mask) < (f2 & mask))
114 return -1;
115 else
116 return 0;
120 * Helper to sort the chunk
122 static int cmp_chunk_info(const void *a, const void *b)
124 return cmp_chunk_block_group(
125 ((struct chunk_info *)a)->type,
126 ((struct chunk_info *)b)->type);
129 static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
131 int ret;
132 struct btrfs_ioctl_search_args args;
133 struct btrfs_ioctl_search_key *sk = &args.key;
134 struct btrfs_ioctl_search_header *sh;
135 unsigned long off = 0;
136 int i, e;
138 memset(&args, 0, sizeof(args));
141 * there may be more than one ROOT_ITEM key if there are
142 * snapshots pending deletion, we have to loop through
143 * them.
145 sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
147 sk->min_objectid = 0;
148 sk->max_objectid = (u64)-1;
149 sk->max_type = 0;
150 sk->min_type = (u8)-1;
151 sk->min_offset = 0;
152 sk->max_offset = (u64)-1;
153 sk->min_transid = 0;
154 sk->max_transid = (u64)-1;
155 sk->nr_items = 4096;
157 while (1) {
158 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
159 e = errno;
160 if (e == EPERM)
161 return -e;
163 if (ret < 0) {
164 fprintf(stderr,
165 "ERROR: can't perform the search - %s\n",
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 = 0;
186 return 1;
189 off += sh->len;
191 sk->min_objectid = sh->objectid;
192 sk->min_type = sh->type;
193 sk->min_offset = sh->offset+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 = 0, *sargs_orig = 0;
232 int e, ret, count;
234 sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
235 if (!sargs) {
236 fprintf(stderr, "ERROR: not enough memory\n");
237 return NULL;
240 sargs->space_slots = 0;
241 sargs->total_spaces = 0;
243 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
244 e = errno;
245 if (ret) {
246 fprintf(stderr,
247 "ERROR: couldn't get space info on '%s' - %s\n",
248 path, strerror(e));
249 free(sargs);
250 return NULL;
252 if (!sargs->total_spaces) {
253 free(sargs);
254 printf("No chunks found\n");
255 return NULL;
258 count = sargs->total_spaces;
260 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
261 (count * sizeof(struct btrfs_ioctl_space_info)));
262 if (!sargs) {
263 free(sargs_orig);
264 fprintf(stderr, "ERROR: not enough memory\n");
265 return NULL;
268 sargs->space_slots = count;
269 sargs->total_spaces = 0;
271 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
272 e = errno;
274 if (ret) {
275 fprintf(stderr,
276 "ERROR: couldn't get space info on '%s' - %s\n",
277 path, strerror(e));
278 free(sargs);
279 return NULL;
282 qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
283 cmp_btrfs_ioctl_space_info);
285 return sargs;
289 * This function computes the space occuped by a *single* RAID5/RAID6 chunk.
290 * The computation is performed on the basis of the number of stripes
291 * which compose the chunk, which could be different from the number of devices
292 * if a disk is added later.
294 static void get_raid56_used(int fd, struct chunk_info *chunks, int chunkcount,
295 u64 *raid5_used, u64 *raid6_used)
297 struct chunk_info *info_ptr = chunks;
298 *raid5_used = 0;
299 *raid6_used = 0;
301 while (chunkcount-- > 0) {
302 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID5)
303 (*raid5_used) += info_ptr->size / (info_ptr->num_stripes - 1);
304 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID6)
305 (*raid6_used) += info_ptr->size / (info_ptr->num_stripes - 2);
306 info_ptr++;
310 #define MIN_UNALOCATED_THRESH (16 * 1024 * 1024)
311 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
312 int chunkcount, struct device_info *devinfo, int devcount,
313 char *path, unsigned unit_mode)
315 struct btrfs_ioctl_space_args *sargs = 0;
316 int i;
317 int ret = 0;
318 int width = 10; /* default 10 for human units */
320 * r_* prefix is for raw data
321 * l_* is for logical
323 u64 r_total_size = 0; /* filesystem size, sum of device sizes */
324 u64 r_total_chunks = 0; /* sum of chunks sizes on disk(s) */
325 u64 r_total_used = 0;
326 u64 r_total_unused = 0;
327 u64 r_data_used = 0;
328 u64 r_data_chunks = 0;
329 u64 l_data_chunks = 0;
330 u64 r_metadata_used = 0;
331 u64 r_metadata_chunks = 0;
332 u64 l_metadata_chunks = 0;
333 u64 r_system_used = 0;
334 u64 r_system_chunks = 0;
335 double data_ratio;
336 double metadata_ratio;
337 /* logical */
338 u64 raid5_used = 0;
339 u64 raid6_used = 0;
340 u64 l_global_reserve = 0;
341 u64 l_global_reserve_used = 0;
342 u64 free_estimated = 0;
343 u64 free_min = 0;
344 int max_data_ratio = 1;
346 sargs = load_space_info(fd, path);
347 if (!sargs) {
348 ret = 1;
349 goto exit;
352 r_total_size = 0;
353 for (i = 0; i < devcount; i++)
354 r_total_size += devinfo[i].device_size;
356 if (r_total_size == 0) {
357 fprintf(stderr,
358 "ERROR: couldn't get space info on '%s' - %s\n",
359 path, strerror(errno));
361 ret = 1;
362 goto exit;
364 get_raid56_used(fd, chunkinfo, chunkcount, &raid5_used, &raid6_used);
366 for (i = 0; i < sargs->total_spaces; i++) {
367 int ratio;
368 u64 flags = sargs->spaces[i].flags;
371 * The raid5/raid6 ratio depends by the stripes number
372 * used by every chunk. It is computed separately
374 if (flags & BTRFS_BLOCK_GROUP_RAID0)
375 ratio = 1;
376 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
377 ratio = 2;
378 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
379 ratio = 0;
380 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
381 ratio = 0;
382 else if (flags & BTRFS_BLOCK_GROUP_DUP)
383 ratio = 2;
384 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
385 ratio = 2;
386 else
387 ratio = 1;
389 if (!ratio)
390 fprintf(stderr, "WARNING: RAID56 detected, not implemented\n");
392 if (ratio > max_data_ratio)
393 max_data_ratio = ratio;
395 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV) {
396 l_global_reserve = sargs->spaces[i].total_bytes;
397 l_global_reserve_used = sargs->spaces[i].used_bytes;
399 if ((flags & (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA))
400 == (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA)) {
401 fprintf(stderr, "WARNING: MIXED blockgroups not handled\n");
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_metadata_chunks + r_system_chunks;
421 r_total_used = r_data_used + r_metadata_used + r_system_used;
422 r_total_unused = r_total_size - r_total_chunks;
424 /* Raw / Logical = raid factor, >= 1 */
425 data_ratio = (double)r_data_chunks / l_data_chunks;
426 metadata_ratio = (double)r_metadata_chunks / l_metadata_chunks;
428 #if 0
429 /* add the raid5/6 allocated space */
430 total_chunks += raid5_used + raid6_used;
431 #endif
434 * We're able to fill at least DATA for the unused space
436 * With mixed raid levels, this gives a rough estimate but more
437 * accurate than just counting the logical free space
438 * (l_data_chunks - l_data_used)
440 * In non-mixed case there's no difference.
442 free_estimated = (r_data_chunks - r_data_used) / data_ratio;
443 free_min = free_estimated;
445 /* Chop unallocatable space */
446 /* FIXME: must be applied per device */
447 if (r_total_unused >= MIN_UNALOCATED_THRESH) {
448 free_estimated += r_total_unused / data_ratio;
449 /* Match the calculation of 'df', use the highest raid ratio */
450 free_min += r_total_unused / max_data_ratio;
453 if (unit_mode != UNITS_HUMAN)
454 width = 18;
456 printf("Overall:\n");
458 printf(" Device size:\t\t%*s\n", width,
459 pretty_size_mode(r_total_size, unit_mode));
460 printf(" Device allocated:\t\t%*s\n", width,
461 pretty_size_mode(r_total_chunks, unit_mode));
462 printf(" Device unallocated:\t\t%*s\n", width,
463 pretty_size_mode(r_total_unused, unit_mode));
464 printf(" Used:\t\t\t%*s\n", width,
465 pretty_size_mode(r_total_used, unit_mode));
466 printf(" Free (estimated):\t\t%*s\t(",
467 width,
468 pretty_size_mode(free_estimated, unit_mode));
469 printf("min: %s)\n", pretty_size_mode(free_min, unit_mode));
470 printf(" Data ratio:\t\t\t%*.2f\n",
471 width, data_ratio);
472 printf(" Metadata ratio:\t\t%*.2f\n",
473 width, metadata_ratio);
474 printf(" Global reserve:\t\t%*s\t(used: %s)\n", width,
475 pretty_size_mode(l_global_reserve, unit_mode),
476 pretty_size_mode(l_global_reserve_used, unit_mode));
478 exit:
480 if (sargs)
481 free(sargs);
483 return ret;
487 * Helper to sort the device_info structure
489 static int cmp_device_info(const void *a, const void *b)
491 return strcmp(((struct device_info *)a)->path,
492 ((struct device_info *)b)->path);
496 * This function loads the device_info structure and put them in an array
498 static int load_device_info(int fd, struct device_info **device_info_ptr,
499 int *device_info_count)
501 int ret, i, ndevs, e;
502 struct btrfs_ioctl_fs_info_args fi_args;
503 struct btrfs_ioctl_dev_info_args dev_info;
504 struct device_info *info;
506 *device_info_count = 0;
507 *device_info_ptr = 0;
509 ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
510 e = errno;
511 if (e == EPERM)
512 return -e;
513 if (ret < 0) {
514 fprintf(stderr, "ERROR: cannot get filesystem info - %s\n",
515 strerror(e));
516 return 1;
519 info = calloc(fi_args.num_devices, sizeof(struct device_info));
520 if (!info) {
521 fprintf(stderr, "ERROR: not enough memory\n");
522 return 1;
525 for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
526 BUG_ON(ndevs >= fi_args.num_devices);
527 memset(&dev_info, 0, sizeof(dev_info));
528 ret = get_device_info(fd, i, &dev_info);
530 if (ret == -ENODEV)
531 continue;
532 if (ret) {
533 fprintf(stderr,
534 "ERROR: cannot get info about device devid=%d\n",
536 free(info);
537 return ret;
540 info[ndevs].devid = dev_info.devid;
541 strcpy(info[ndevs].path, (char *)dev_info.path);
542 info[ndevs].device_size = get_partition_size((char *)dev_info.path);
543 info[ndevs].size = dev_info.total_bytes;
544 ++ndevs;
547 BUG_ON(ndevs != fi_args.num_devices);
548 qsort(info, fi_args.num_devices,
549 sizeof(struct device_info), cmp_device_info);
551 *device_info_count = fi_args.num_devices;
552 *device_info_ptr = info;
554 return 0;
557 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
558 int *chunkcount, struct device_info **devinfo, int *devcount)
560 int ret;
562 ret = load_chunk_info(fd, chunkinfo, chunkcount);
563 if (ret == -EPERM) {
564 fprintf(stderr,
565 "WARNING: can't read detailed chunk info, RAID5/6 numbers will be incorrect, run as root\n");
566 } else if (ret) {
567 return ret;
570 ret = load_device_info(fd, devinfo, devcount);
571 if (ret == -EPERM) {
572 fprintf(stderr,
573 "WARNING: can't get filesystem info from ioctl(FS_INFO), run as root\n");
574 ret = 0;
577 return ret;
581 * This function computes the size of a chunk in a disk
583 static u64 calc_chunk_size(struct chunk_info *ci)
585 if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
586 return ci->size / ci->num_stripes;
587 else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
588 return ci->size ;
589 else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
590 return ci->size ;
591 else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
592 return ci->size / (ci->num_stripes -1);
593 else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
594 return ci->size / (ci->num_stripes -2);
595 else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
596 return ci->size / ci->num_stripes;
597 return ci->size;
601 * This function print the results of the command "btrfs fi usage"
602 * in tabular format
604 static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
605 struct btrfs_ioctl_space_args *sargs,
606 struct chunk_info *chunks_info_ptr,
607 int chunks_info_count,
608 struct device_info *device_info_ptr,
609 int device_info_count)
611 int i;
612 u64 total_unused = 0;
613 struct string_table *matrix = 0;
614 int ncols, nrows;
616 ncols = sargs->total_spaces + 2;
617 nrows = 2 + 1 + device_info_count + 1 + 2;
619 matrix = table_create(ncols, nrows);
620 if (!matrix) {
621 fprintf(stderr, "ERROR: not enough memory\n");
622 return;
625 /* header */
626 for (i = 0; i < sargs->total_spaces; i++) {
627 const char *description;
628 u64 flags = sargs->spaces[i].flags;
630 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
631 continue;
633 description = btrfs_group_type_str(flags);
635 table_printf(matrix, 1+i, 0, "<%s", description);
638 for (i = 0; i < sargs->total_spaces; i++) {
639 const char *r_mode;
641 u64 flags = sargs->spaces[i].flags;
642 r_mode = btrfs_group_profile_str(flags);
644 table_printf(matrix, 1+i, 1, "<%s", r_mode);
647 table_printf(matrix, 1+sargs->total_spaces, 1, "<Unallocated");
649 /* body */
650 for (i = 0; i < device_info_count; i++) {
651 int k, col;
652 char *p;
654 u64 total_allocated = 0, unused;
656 p = strrchr(device_info_ptr[i].path, '/');
657 if (!p)
658 p = device_info_ptr[i].path;
659 else
660 p++;
662 table_printf(matrix, 0, i + 3, "<%s", device_info_ptr[i].path);
664 for (col = 1, k = 0 ; k < sargs->total_spaces ; k++) {
665 u64 flags = sargs->spaces[k].flags;
666 u64 devid = device_info_ptr[i].devid;
667 int j;
668 u64 size = 0;
670 for (j = 0 ; j < chunks_info_count ; j++) {
671 if (chunks_info_ptr[j].type != flags )
672 continue;
673 if (chunks_info_ptr[j].devid != devid)
674 continue;
676 size += calc_chunk_size(chunks_info_ptr+j);
679 if (size)
680 table_printf(matrix, col, i+3,
681 ">%s", pretty_size_mode(size, unit_mode));
682 else
683 table_printf(matrix, col, i+3, ">-");
685 total_allocated += size;
686 col++;
689 unused = get_partition_size(device_info_ptr[i].path)
690 - total_allocated;
692 table_printf(matrix, sargs->total_spaces + 1, i + 3,
693 ">%s", pretty_size_mode(unused, unit_mode));
694 total_unused += unused;
698 for (i = 0; i <= sargs->total_spaces; i++)
699 table_printf(matrix, i + 1, device_info_count + 3, "=");
701 /* footer */
702 table_printf(matrix, 0, device_info_count + 4, "<Total");
703 for (i = 0; i < sargs->total_spaces; i++)
704 table_printf(matrix, 1 + i, device_info_count + 4, ">%s",
705 pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));
707 table_printf(matrix, sargs->total_spaces + 1, device_info_count + 4,
708 ">%s", pretty_size_mode(total_unused, unit_mode));
710 table_printf(matrix, 0, device_info_count + 5, "<Used");
711 for (i = 0; i < sargs->total_spaces; i++)
712 table_printf(matrix, 1 + i, device_info_count+5, ">%s",
713 pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
715 table_dump(matrix);
716 table_free(matrix);
720 * This function prints the unused space per every disk
722 static void print_unused(struct chunk_info *info_ptr,
723 int info_count,
724 struct device_info *device_info_ptr,
725 int device_info_count,
726 unsigned unit_mode)
728 int i;
729 for (i = 0; i < device_info_count; i++) {
730 int j;
731 u64 total = 0;
733 for (j = 0; j < info_count; j++)
734 if (info_ptr[j].devid == device_info_ptr[i].devid)
735 total += calc_chunk_size(info_ptr+j);
737 printf(" %s\t%10s\n",
738 device_info_ptr[i].path,
739 pretty_size_mode(device_info_ptr[i].size - total,
740 unit_mode));
745 * This function prints the allocated chunk per every disk
747 static void print_chunk_device(u64 chunk_type,
748 struct chunk_info *chunks_info_ptr,
749 int chunks_info_count,
750 struct device_info *device_info_ptr,
751 int device_info_count,
752 unsigned unit_mode)
754 int i;
756 for (i = 0; i < device_info_count; i++) {
757 int j;
758 u64 total = 0;
760 for (j = 0; j < chunks_info_count; j++) {
762 if (chunks_info_ptr[j].type != chunk_type)
763 continue;
764 if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
765 continue;
767 total += calc_chunk_size(&(chunks_info_ptr[j]));
768 //total += chunks_info_ptr[j].size;
771 if (total > 0)
772 printf(" %s\t%10s\n",
773 device_info_ptr[i].path,
774 pretty_size_mode(total, unit_mode));
779 * This function print the results of the command "btrfs fi usage"
780 * in linear format
782 static void _cmd_filesystem_usage_linear(unsigned unit_mode,
783 struct btrfs_ioctl_space_args *sargs,
784 struct chunk_info *info_ptr,
785 int info_count,
786 struct device_info *device_info_ptr,
787 int device_info_count)
789 int i;
791 for (i = 0; i < sargs->total_spaces; i++) {
792 const char *description;
793 const char *r_mode;
794 u64 flags = sargs->spaces[i].flags;
796 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
797 continue;
799 description = btrfs_group_type_str(flags);
800 r_mode = btrfs_group_profile_str(flags);
802 printf("%s,%s: Size:%s, ",
803 description,
804 r_mode,
805 pretty_size_mode(sargs->spaces[i].total_bytes,
806 unit_mode));
807 printf("Used:%s\n",
808 pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
809 print_chunk_device(flags, info_ptr, info_count,
810 device_info_ptr, device_info_count, unit_mode);
811 printf("\n");
814 printf("Unallocated:\n");
815 print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
816 unit_mode);
819 static int print_filesystem_usage_by_chunk(int fd,
820 struct chunk_info *chunkinfo, int chunkcount,
821 struct device_info *devinfo, int devcount,
822 char *path, unsigned unit_mode, int tabular)
824 struct btrfs_ioctl_space_args *sargs;
825 int ret = 0;
827 if (!chunkinfo)
828 return 0;
830 sargs = load_space_info(fd, path);
831 if (!sargs) {
832 ret = 1;
833 goto out;
836 if (tabular)
837 _cmd_filesystem_usage_tabular(unit_mode, sargs, chunkinfo,
838 chunkcount, devinfo, devcount);
839 else
840 _cmd_filesystem_usage_linear(unit_mode, sargs, chunkinfo,
841 chunkcount, devinfo, devcount);
843 free(sargs);
844 out:
845 return ret;
848 const char * const cmd_filesystem_usage_usage[] = {
849 "btrfs filesystem usage [options] <path> [<path>..]",
850 "Show detailed information about internal filesystem usage .",
851 "-b|--raw raw numbers in bytes",
852 "-h|--human-readable",
853 " human friendly numbers, base 1024 (default)",
854 "-H human friendly numbers, base 1000",
855 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
856 "--si use 1000 as a base (kB, MB, GB, TB)",
857 "-k|--kbytes show sizes in KiB, or kB with --si",
858 "-m|--mbytes show sizes in MiB, or MB with --si",
859 "-g|--gbytes show sizes in GiB, or GB with --si",
860 "-t|--tbytes show sizes in TiB, or TB with --si",
861 "-T show data in tabular format",
862 NULL
865 int cmd_filesystem_usage(int argc, char **argv)
867 unsigned unit_mode = UNITS_DEFAULT;
868 int ret = 0;
869 int i, more_than_one = 0;
870 int tabular = 0;
872 optind = 1;
873 while (1) {
874 int long_index;
875 static const struct option long_options[] = {
876 { "raw", no_argument, NULL, 'b'},
877 { "kbytes", no_argument, NULL, 'k'},
878 { "mbytes", no_argument, NULL, 'm'},
879 { "gbytes", no_argument, NULL, 'g'},
880 { "tbytes", no_argument, NULL, 't'},
881 { "si", no_argument, NULL, GETOPT_VAL_SI},
882 { "iec", no_argument, NULL, GETOPT_VAL_IEC},
883 { "human-readable", no_argument, NULL,
884 GETOPT_VAL_HUMAN_READABLE},
885 { NULL, 0, NULL, 0 }
887 int c = getopt_long(argc, argv, "bhHkmgtT", long_options,
888 &long_index);
890 if (c < 0)
891 break;
892 switch (c) {
893 case 'b':
894 unit_mode = UNITS_RAW;
895 break;
896 case 'k':
897 units_set_base(&unit_mode, UNITS_KBYTES);
898 break;
899 case 'm':
900 units_set_base(&unit_mode, UNITS_MBYTES);
901 break;
902 case 'g':
903 units_set_base(&unit_mode, UNITS_GBYTES);
904 break;
905 case 't':
906 units_set_base(&unit_mode, UNITS_TBYTES);
907 break;
908 case GETOPT_VAL_HUMAN_READABLE:
909 case 'h':
910 unit_mode = UNITS_HUMAN_BINARY;
911 break;
912 case 'H':
913 unit_mode = UNITS_HUMAN_DECIMAL;
914 break;
915 case GETOPT_VAL_SI:
916 units_set_mode(&unit_mode, UNITS_DECIMAL);
917 break;
918 case GETOPT_VAL_IEC:
919 units_set_mode(&unit_mode, UNITS_BINARY);
920 break;
921 case 'T':
922 tabular = 1;
923 break;
924 default:
925 usage(cmd_filesystem_usage_usage);
929 if (check_argc_min(argc - optind, 1))
930 usage(cmd_filesystem_usage_usage);
932 for (i = optind; i < argc; i++) {
933 int fd;
934 DIR *dirstream = NULL;
935 struct chunk_info *chunkinfo = NULL;
936 struct device_info *devinfo = NULL;
937 int chunkcount = 0;
938 int devcount = 0;
940 fd = open_file_or_dir(argv[i], &dirstream);
941 if (fd < 0) {
942 fprintf(stderr, "ERROR: can't access '%s'\n",
943 argv[i]);
944 ret = 1;
945 goto out;
947 if (more_than_one)
948 printf("\n");
950 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
951 &devinfo, &devcount);
952 if (ret)
953 goto cleanup;
955 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
956 devinfo, devcount, argv[i], unit_mode);
957 if (ret)
958 goto cleanup;
959 printf("\n");
960 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
961 devinfo, devcount, argv[i], unit_mode, tabular);
962 cleanup:
963 close_file_or_dir(fd, dirstream);
964 free(chunkinfo);
965 free(devinfo);
967 if (ret)
968 goto out;
969 more_than_one = 1;
972 out:
973 return !!ret;
976 void print_device_chunks(int fd, struct device_info *devinfo,
977 struct chunk_info *chunks_info_ptr,
978 int chunks_info_count, unsigned unit_mode)
980 int i;
981 u64 allocated = 0;
983 for (i = 0 ; i < chunks_info_count ; i++) {
984 const char *description;
985 const char *r_mode;
986 u64 flags;
987 u64 size;
989 if (chunks_info_ptr[i].devid != devinfo->devid)
990 continue;
992 flags = chunks_info_ptr[i].type;
994 description = btrfs_group_type_str(flags);
995 r_mode = btrfs_group_profile_str(flags);
996 size = calc_chunk_size(chunks_info_ptr+i);
997 printf(" %s,%s:%*s%10s\n",
998 description,
999 r_mode,
1000 (int)(20 - strlen(description) - strlen(r_mode)), "",
1001 pretty_size_mode(size, unit_mode));
1003 allocated += size;
1006 printf(" Unallocated: %*s%10s\n",
1007 (int)(20 - strlen("Unallocated")), "",
1008 pretty_size_mode(devinfo->size - allocated, unit_mode));
1011 void print_device_sizes(int fd, struct device_info *devinfo, unsigned unit_mode)
1013 printf(" Device size: %*s%10s\n",
1014 (int)(20 - strlen("Device size")), "",
1015 pretty_size_mode(devinfo->device_size, unit_mode));
1016 #if 0
1018 * The term has not seen an agreement and we don't want to change it
1019 * once it's in non-development branches or even released.
1021 printf(" FS occupied: %*s%10s\n",
1022 (int)(20 - strlen("FS occupied")), "",
1023 pretty_size_mode(devinfo->size, unit_mode));
1024 #endif