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>
27 #include "kerncompat.h"
29 #include "string-table.h"
30 #include "cmds-fi-usage.h"
36 * Add the chunk info to the chunk_info list
38 static int add_info_to_list(struct chunk_info
**info_ptr
,
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
);
48 for (j
= 0 ; j
< num_stripes
; j
++) {
50 struct chunk_info
*p
= 0;
51 struct btrfs_stripe
*stripe
;
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
) {
66 int size
= sizeof(struct btrfs_chunk
) * (*info_count
+1);
67 struct chunk_info
*res
= realloc(*info_ptr
, size
);
71 fprintf(stderr
, "ERROR: not enough memory\n");
76 p
= res
+ *info_count
;
82 p
->num_stripes
= num_stripes
;
94 * Helper to sort the chunk type
96 static int cmp_chunk_block_group(u64 f1
, u64 f2
)
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
)
106 else if (f1
& BTRFS_BLOCK_GROUP_SYSTEM
)
109 mask
= BTRFS_BLOCK_GROUP_TYPE_MASK
;
111 if ((f1
& mask
) > (f2
& mask
))
113 else if ((f1
& mask
) < (f2
& mask
))
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
)
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;
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
145 sk
->tree_id
= BTRFS_CHUNK_TREE_OBJECTID
;
147 sk
->min_objectid
= 0;
148 sk
->max_objectid
= (u64
)-1;
150 sk
->min_type
= (u8
)-1;
152 sk
->max_offset
= (u64
)-1;
154 sk
->max_transid
= (u64
)-1;
158 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
165 "ERROR: can't perform the search - %s\n",
169 /* the ioctl returns the number of item it found in nr_items */
171 if (sk
->nr_items
== 0)
175 for (i
= 0; i
< sk
->nr_items
; i
++) {
176 struct btrfs_chunk
*item
;
177 sh
= (struct btrfs_ioctl_search_header
*)(args
.buf
+
181 item
= (struct btrfs_chunk
*)(args
.buf
+ off
);
183 ret
= add_info_to_list(info_ptr
, info_count
, item
);
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 */
206 if (!sk
->min_objectid
)
210 qsort(*info_ptr
, *info_count
, sizeof(struct chunk_info
),
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;
234 sargs_orig
= sargs
= calloc(1, sizeof(struct btrfs_ioctl_space_args
));
236 fprintf(stderr
, "ERROR: not enough memory\n");
240 sargs
->space_slots
= 0;
241 sargs
->total_spaces
= 0;
243 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
247 "ERROR: couldn't get space info on '%s' - %s\n",
252 if (!sargs
->total_spaces
) {
254 printf("No chunks found\n");
258 count
= sargs
->total_spaces
;
260 sargs
= realloc(sargs
, sizeof(struct btrfs_ioctl_space_args
) +
261 (count
* sizeof(struct btrfs_ioctl_space_info
)));
264 fprintf(stderr
, "ERROR: not enough memory\n");
268 sargs
->space_slots
= count
;
269 sargs
->total_spaces
= 0;
271 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
276 "ERROR: couldn't get space info on '%s' - %s\n",
282 qsort(&(sargs
->spaces
), count
, sizeof(struct btrfs_ioctl_space_info
),
283 cmp_btrfs_ioctl_space_info
);
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
;
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);
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;
318 int width
= 10; /* default 10 for human units */
320 * r_* prefix is for raw data
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_total_missing
= 0; /* sum of missing devices size */
329 u64 r_data_chunks
= 0;
330 u64 l_data_chunks
= 0;
331 u64 r_metadata_used
= 0;
332 u64 r_metadata_chunks
= 0;
333 u64 l_metadata_chunks
= 0;
334 u64 r_system_used
= 0;
335 u64 r_system_chunks
= 0;
337 double metadata_ratio
;
341 u64 l_global_reserve
= 0;
342 u64 l_global_reserve_used
= 0;
343 u64 free_estimated
= 0;
345 int max_data_ratio
= 1;
347 sargs
= load_space_info(fd
, path
);
354 for (i
= 0; i
< devcount
; i
++) {
355 r_total_size
+= devinfo
[i
].size
;
356 if (!devinfo
[i
].device_size
)
357 r_total_missing
+= devinfo
[i
].size
;
360 if (r_total_size
== 0) {
362 "ERROR: couldn't get space info on '%s' - %s\n",
363 path
, strerror(errno
));
368 get_raid56_used(fd
, chunkinfo
, chunkcount
, &raid5_used
, &raid6_used
);
370 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
372 u64 flags
= sargs
->spaces
[i
].flags
;
375 * The raid5/raid6 ratio depends by the stripes number
376 * used by every chunk. It is computed separately
378 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
380 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
382 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
384 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
386 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
388 else if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
394 fprintf(stderr
, "WARNING: RAID56 detected, not implemented\n");
396 if (ratio
> max_data_ratio
)
397 max_data_ratio
= ratio
;
399 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
) {
400 l_global_reserve
= sargs
->spaces
[i
].total_bytes
;
401 l_global_reserve_used
= sargs
->spaces
[i
].used_bytes
;
403 if ((flags
& (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
))
404 == (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
)) {
405 fprintf(stderr
, "WARNING: MIXED blockgroups not handled\n");
408 if (flags
& BTRFS_BLOCK_GROUP_DATA
) {
409 r_data_used
+= sargs
->spaces
[i
].used_bytes
* ratio
;
410 r_data_chunks
+= sargs
->spaces
[i
].total_bytes
* ratio
;
411 l_data_chunks
+= sargs
->spaces
[i
].total_bytes
;
413 if (flags
& BTRFS_BLOCK_GROUP_METADATA
) {
414 r_metadata_used
+= sargs
->spaces
[i
].used_bytes
* ratio
;
415 r_metadata_chunks
+= sargs
->spaces
[i
].total_bytes
* ratio
;
416 l_metadata_chunks
+= sargs
->spaces
[i
].total_bytes
;
418 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
419 r_system_used
+= sargs
->spaces
[i
].used_bytes
* ratio
;
420 r_system_chunks
+= sargs
->spaces
[i
].total_bytes
* ratio
;
424 r_total_chunks
= r_data_chunks
+ r_metadata_chunks
+ r_system_chunks
;
425 r_total_used
= r_data_used
+ r_metadata_used
+ r_system_used
;
426 r_total_unused
= r_total_size
- r_total_chunks
;
428 /* Raw / Logical = raid factor, >= 1 */
429 data_ratio
= (double)r_data_chunks
/ l_data_chunks
;
430 metadata_ratio
= (double)r_metadata_chunks
/ l_metadata_chunks
;
433 /* add the raid5/6 allocated space */
434 total_chunks
+= raid5_used
+ raid6_used
;
438 * We're able to fill at least DATA for the unused space
440 * With mixed raid levels, this gives a rough estimate but more
441 * accurate than just counting the logical free space
442 * (l_data_chunks - l_data_used)
444 * In non-mixed case there's no difference.
446 free_estimated
= (r_data_chunks
- r_data_used
) / data_ratio
;
447 free_min
= free_estimated
;
449 /* Chop unallocatable space */
450 /* FIXME: must be applied per device */
451 if (r_total_unused
>= MIN_UNALOCATED_THRESH
) {
452 free_estimated
+= r_total_unused
/ data_ratio
;
453 /* Match the calculation of 'df', use the highest raid ratio */
454 free_min
+= r_total_unused
/ max_data_ratio
;
457 if (unit_mode
!= UNITS_HUMAN
)
460 printf("Overall:\n");
462 printf(" Device size:\t\t%*s\n", width
,
463 pretty_size_mode(r_total_size
, unit_mode
));
464 printf(" Device allocated:\t\t%*s\n", width
,
465 pretty_size_mode(r_total_chunks
, unit_mode
));
466 printf(" Device unallocated:\t\t%*s\n", width
,
467 pretty_size_mode(r_total_unused
, unit_mode
));
468 printf(" Device missing:\t\t%*s\n", width
,
469 pretty_size_mode(r_total_missing
, unit_mode
));
470 printf(" Used:\t\t\t%*s\n", width
,
471 pretty_size_mode(r_total_used
, unit_mode
));
472 printf(" Free (estimated):\t\t%*s\t(",
474 pretty_size_mode(free_estimated
, unit_mode
));
475 printf("min: %s)\n", pretty_size_mode(free_min
, unit_mode
));
476 printf(" Data ratio:\t\t\t%*.2f\n",
478 printf(" Metadata ratio:\t\t%*.2f\n",
479 width
, metadata_ratio
);
480 printf(" Global reserve:\t\t%*s\t(used: %s)\n", width
,
481 pretty_size_mode(l_global_reserve
, unit_mode
),
482 pretty_size_mode(l_global_reserve_used
, unit_mode
));
493 * Helper to sort the device_info structure
495 static int cmp_device_info(const void *a
, const void *b
)
497 return strcmp(((struct device_info
*)a
)->path
,
498 ((struct device_info
*)b
)->path
);
502 * This function loads the device_info structure and put them in an array
504 static int load_device_info(int fd
, struct device_info
**device_info_ptr
,
505 int *device_info_count
)
508 struct btrfs_ioctl_fs_info_args fi_args
;
509 struct btrfs_ioctl_dev_info_args dev_info
;
510 struct device_info
*info
;
512 *device_info_count
= 0;
513 *device_info_ptr
= 0;
515 ret
= ioctl(fd
, BTRFS_IOC_FS_INFO
, &fi_args
);
519 fprintf(stderr
, "ERROR: cannot get filesystem info - %s\n",
524 info
= calloc(fi_args
.num_devices
, sizeof(struct device_info
));
526 fprintf(stderr
, "ERROR: not enough memory\n");
530 for (i
= 0, ndevs
= 0 ; i
<= fi_args
.max_id
; i
++) {
531 BUG_ON(ndevs
>= fi_args
.num_devices
);
532 memset(&dev_info
, 0, sizeof(dev_info
));
533 ret
= get_device_info(fd
, i
, &dev_info
);
539 "ERROR: cannot get info about device devid=%d\n",
545 info
[ndevs
].devid
= dev_info
.devid
;
546 if (!dev_info
.path
[0]) {
547 strcpy(info
[ndevs
].path
, "missing");
549 strcpy(info
[ndevs
].path
, (char *)dev_info
.path
);
550 info
[ndevs
].device_size
=
551 get_partition_size((char *)dev_info
.path
);
553 info
[ndevs
].size
= dev_info
.total_bytes
;
557 BUG_ON(ndevs
!= fi_args
.num_devices
);
558 qsort(info
, fi_args
.num_devices
,
559 sizeof(struct device_info
), cmp_device_info
);
561 *device_info_count
= fi_args
.num_devices
;
562 *device_info_ptr
= info
;
567 int load_chunk_and_device_info(int fd
, struct chunk_info
**chunkinfo
,
568 int *chunkcount
, struct device_info
**devinfo
, int *devcount
)
572 ret
= load_chunk_info(fd
, chunkinfo
, chunkcount
);
575 "WARNING: can't read detailed chunk info, RAID5/6 numbers will be incorrect, run as root\n");
580 ret
= load_device_info(fd
, devinfo
, devcount
);
583 "WARNING: can't get filesystem info from ioctl(FS_INFO), run as root\n");
591 * This function computes the size of a chunk in a disk
593 static u64
calc_chunk_size(struct chunk_info
*ci
)
595 if (ci
->type
& BTRFS_BLOCK_GROUP_RAID0
)
596 return ci
->size
/ ci
->num_stripes
;
597 else if (ci
->type
& BTRFS_BLOCK_GROUP_RAID1
)
599 else if (ci
->type
& BTRFS_BLOCK_GROUP_DUP
)
601 else if (ci
->type
& BTRFS_BLOCK_GROUP_RAID5
)
602 return ci
->size
/ (ci
->num_stripes
-1);
603 else if (ci
->type
& BTRFS_BLOCK_GROUP_RAID6
)
604 return ci
->size
/ (ci
->num_stripes
-2);
605 else if (ci
->type
& BTRFS_BLOCK_GROUP_RAID10
)
606 return ci
->size
/ ci
->num_stripes
;
611 * This function print the results of the command "btrfs fi usage"
614 static void _cmd_filesystem_usage_tabular(unsigned unit_mode
,
615 struct btrfs_ioctl_space_args
*sargs
,
616 struct chunk_info
*chunks_info_ptr
,
617 int chunks_info_count
,
618 struct device_info
*device_info_ptr
,
619 int device_info_count
)
622 u64 total_unused
= 0;
623 struct string_table
*matrix
= 0;
626 ncols
= sargs
->total_spaces
+ 2;
627 nrows
= 2 + 1 + device_info_count
+ 1 + 2;
629 matrix
= table_create(ncols
, nrows
);
631 fprintf(stderr
, "ERROR: not enough memory\n");
636 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
637 const char *description
;
638 u64 flags
= sargs
->spaces
[i
].flags
;
640 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
643 description
= btrfs_group_type_str(flags
);
645 table_printf(matrix
, 1+i
, 0, "<%s", description
);
648 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
651 u64 flags
= sargs
->spaces
[i
].flags
;
652 r_mode
= btrfs_group_profile_str(flags
);
654 table_printf(matrix
, 1+i
, 1, "<%s", r_mode
);
657 table_printf(matrix
, 1+sargs
->total_spaces
, 1, "<Unallocated");
660 for (i
= 0; i
< device_info_count
; i
++) {
664 u64 total_allocated
= 0, unused
;
666 p
= strrchr(device_info_ptr
[i
].path
, '/');
668 p
= device_info_ptr
[i
].path
;
672 table_printf(matrix
, 0, i
+ 3, "<%s", device_info_ptr
[i
].path
);
674 for (col
= 1, k
= 0 ; k
< sargs
->total_spaces
; k
++) {
675 u64 flags
= sargs
->spaces
[k
].flags
;
676 u64 devid
= device_info_ptr
[i
].devid
;
680 for (j
= 0 ; j
< chunks_info_count
; j
++) {
681 if (chunks_info_ptr
[j
].type
!= flags
)
683 if (chunks_info_ptr
[j
].devid
!= devid
)
686 size
+= calc_chunk_size(chunks_info_ptr
+j
);
690 table_printf(matrix
, col
, i
+3,
691 ">%s", pretty_size_mode(size
, unit_mode
));
693 table_printf(matrix
, col
, i
+3, ">-");
695 total_allocated
+= size
;
699 unused
= get_partition_size(device_info_ptr
[i
].path
)
702 table_printf(matrix
, sargs
->total_spaces
+ 1, i
+ 3,
703 ">%s", pretty_size_mode(unused
, unit_mode
));
704 total_unused
+= unused
;
708 for (i
= 0; i
<= sargs
->total_spaces
; i
++)
709 table_printf(matrix
, i
+ 1, device_info_count
+ 3, "=");
712 table_printf(matrix
, 0, device_info_count
+ 4, "<Total");
713 for (i
= 0; i
< sargs
->total_spaces
; i
++)
714 table_printf(matrix
, 1 + i
, device_info_count
+ 4, ">%s",
715 pretty_size_mode(sargs
->spaces
[i
].total_bytes
, unit_mode
));
717 table_printf(matrix
, sargs
->total_spaces
+ 1, device_info_count
+ 4,
718 ">%s", pretty_size_mode(total_unused
, unit_mode
));
720 table_printf(matrix
, 0, device_info_count
+ 5, "<Used");
721 for (i
= 0; i
< sargs
->total_spaces
; i
++)
722 table_printf(matrix
, 1 + i
, device_info_count
+5, ">%s",
723 pretty_size_mode(sargs
->spaces
[i
].used_bytes
, unit_mode
));
730 * This function prints the unused space per every disk
732 static void print_unused(struct chunk_info
*info_ptr
,
734 struct device_info
*device_info_ptr
,
735 int device_info_count
,
739 for (i
= 0; i
< device_info_count
; i
++) {
743 for (j
= 0; j
< info_count
; j
++)
744 if (info_ptr
[j
].devid
== device_info_ptr
[i
].devid
)
745 total
+= calc_chunk_size(info_ptr
+j
);
747 printf(" %s\t%10s\n",
748 device_info_ptr
[i
].path
,
749 pretty_size_mode(device_info_ptr
[i
].size
- total
,
755 * This function prints the allocated chunk per every disk
757 static void print_chunk_device(u64 chunk_type
,
758 struct chunk_info
*chunks_info_ptr
,
759 int chunks_info_count
,
760 struct device_info
*device_info_ptr
,
761 int device_info_count
,
766 for (i
= 0; i
< device_info_count
; i
++) {
770 for (j
= 0; j
< chunks_info_count
; j
++) {
772 if (chunks_info_ptr
[j
].type
!= chunk_type
)
774 if (chunks_info_ptr
[j
].devid
!= device_info_ptr
[i
].devid
)
777 total
+= calc_chunk_size(&(chunks_info_ptr
[j
]));
778 //total += chunks_info_ptr[j].size;
782 printf(" %s\t%10s\n",
783 device_info_ptr
[i
].path
,
784 pretty_size_mode(total
, unit_mode
));
789 * This function print the results of the command "btrfs fi usage"
792 static void _cmd_filesystem_usage_linear(unsigned unit_mode
,
793 struct btrfs_ioctl_space_args
*sargs
,
794 struct chunk_info
*info_ptr
,
796 struct device_info
*device_info_ptr
,
797 int device_info_count
)
801 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
802 const char *description
;
804 u64 flags
= sargs
->spaces
[i
].flags
;
806 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
809 description
= btrfs_group_type_str(flags
);
810 r_mode
= btrfs_group_profile_str(flags
);
812 printf("%s,%s: Size:%s, ",
815 pretty_size_mode(sargs
->spaces
[i
].total_bytes
,
818 pretty_size_mode(sargs
->spaces
[i
].used_bytes
, unit_mode
));
819 print_chunk_device(flags
, info_ptr
, info_count
,
820 device_info_ptr
, device_info_count
, unit_mode
);
824 printf("Unallocated:\n");
825 print_unused(info_ptr
, info_count
, device_info_ptr
, device_info_count
,
829 static int print_filesystem_usage_by_chunk(int fd
,
830 struct chunk_info
*chunkinfo
, int chunkcount
,
831 struct device_info
*devinfo
, int devcount
,
832 char *path
, unsigned unit_mode
, int tabular
)
834 struct btrfs_ioctl_space_args
*sargs
;
840 sargs
= load_space_info(fd
, path
);
847 _cmd_filesystem_usage_tabular(unit_mode
, sargs
, chunkinfo
,
848 chunkcount
, devinfo
, devcount
);
850 _cmd_filesystem_usage_linear(unit_mode
, sargs
, chunkinfo
,
851 chunkcount
, devinfo
, devcount
);
858 const char * const cmd_filesystem_usage_usage
[] = {
859 "btrfs filesystem usage [options] <path> [<path>..]",
860 "Show detailed information about internal filesystem usage .",
861 HELPINFO_OUTPUT_UNIT_DF
,
862 "-T show data in tabular format",
866 int cmd_filesystem_usage(int argc
, char **argv
)
871 int more_than_one
= 0;
874 unit_mode
= get_unit_mode_from_arg(&argc
, argv
, 1);
880 c
= getopt(argc
, argv
, "T");
889 usage(cmd_filesystem_usage_usage
);
893 if (check_argc_min(argc
- optind
, 1))
894 usage(cmd_filesystem_usage_usage
);
896 for (i
= optind
; i
< argc
; i
++) {
898 DIR *dirstream
= NULL
;
899 struct chunk_info
*chunkinfo
= NULL
;
900 struct device_info
*devinfo
= NULL
;
904 fd
= btrfs_open_dir(argv
[i
], &dirstream
, 1);
912 ret
= load_chunk_and_device_info(fd
, &chunkinfo
, &chunkcount
,
913 &devinfo
, &devcount
);
917 ret
= print_filesystem_usage_overall(fd
, chunkinfo
, chunkcount
,
918 devinfo
, devcount
, argv
[i
], unit_mode
);
922 ret
= print_filesystem_usage_by_chunk(fd
, chunkinfo
, chunkcount
,
923 devinfo
, devcount
, argv
[i
], unit_mode
, tabular
);
925 close_file_or_dir(fd
, dirstream
);
938 void print_device_chunks(int fd
, struct device_info
*devinfo
,
939 struct chunk_info
*chunks_info_ptr
,
940 int chunks_info_count
, unsigned unit_mode
)
945 for (i
= 0 ; i
< chunks_info_count
; i
++) {
946 const char *description
;
951 if (chunks_info_ptr
[i
].devid
!= devinfo
->devid
)
954 flags
= chunks_info_ptr
[i
].type
;
956 description
= btrfs_group_type_str(flags
);
957 r_mode
= btrfs_group_profile_str(flags
);
958 size
= calc_chunk_size(chunks_info_ptr
+i
);
959 printf(" %s,%s:%*s%10s\n",
962 (int)(20 - strlen(description
) - strlen(r_mode
)), "",
963 pretty_size_mode(size
, unit_mode
));
968 printf(" Unallocated: %*s%10s\n",
969 (int)(20 - strlen("Unallocated")), "",
970 pretty_size_mode(devinfo
->size
- allocated
, unit_mode
));
973 void print_device_sizes(int fd
, struct device_info
*devinfo
, unsigned unit_mode
)
975 printf(" Device size: %*s%10s\n",
976 (int)(20 - strlen("Device size")), "",
977 pretty_size_mode(devinfo
->device_size
, unit_mode
));
980 * The term has not seen an agreement and we don't want to change it
981 * once it's in non-development branches or even released.
983 printf(" FS occupied: %*s%10s\n",
984 (int)(20 - strlen("FS occupied")), "",
985 pretty_size_mode(devinfo
->size
, unit_mode
));