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"
37 * Add the chunk info to the chunk_info list
39 static int add_info_to_list(struct chunk_info
**info_ptr
,
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
);
49 for (j
= 0 ; j
< num_stripes
; j
++) {
51 struct chunk_info
*p
= NULL
;
52 struct btrfs_stripe
*stripe
;
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
) {
67 int tmp
= sizeof(struct btrfs_chunk
) * (*info_count
+ 1);
68 struct chunk_info
*res
= realloc(*info_ptr
, tmp
);
72 error("not enough memory");
77 p
= res
+ *info_count
;
83 p
->num_stripes
= num_stripes
;
95 * Helper to sort the chunk type
97 static int cmp_chunk_block_group(u64 f1
, u64 f2
)
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
)
107 else if (f1
& BTRFS_BLOCK_GROUP_SYSTEM
)
110 mask
= BTRFS_BLOCK_GROUP_TYPE_MASK
;
112 if ((f1
& mask
) > (f2
& mask
))
114 else if ((f1
& mask
) < (f2
& mask
))
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
)
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;
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
146 sk
->tree_id
= BTRFS_CHUNK_TREE_OBJECTID
;
148 sk
->min_objectid
= 0;
149 sk
->max_objectid
= (u64
)-1;
151 sk
->min_type
= (u8
)-1;
153 sk
->max_offset
= (u64
)-1;
155 sk
->max_transid
= (u64
)-1;
159 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
);
165 error("cannot look up chunk tree info: %s",
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
);
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 */
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
= NULL
, *sargs_orig
= NULL
;
234 sargs_orig
= sargs
= calloc(1, sizeof(struct btrfs_ioctl_space_args
));
236 error("not enough memory");
240 sargs
->space_slots
= 0;
241 sargs
->total_spaces
= 0;
243 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
245 error("cannot get space info on '%s': %s", path
,
250 if (!sargs
->total_spaces
) {
252 printf("No chunks found\n");
256 count
= sargs
->total_spaces
;
258 sargs
= realloc(sargs
, sizeof(struct btrfs_ioctl_space_args
) +
259 (count
* sizeof(struct btrfs_ioctl_space_info
)));
262 error("not enough memory");
266 sargs
->space_slots
= count
;
267 sargs
->total_spaces
= 0;
269 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
271 error("cannot get space info with %u slots: %s",
272 count
, strerror(errno
));
277 qsort(&(sargs
->spaces
), count
, sizeof(struct btrfs_ioctl_space_info
),
278 cmp_btrfs_ioctl_space_info
);
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
;
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);
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
;
313 int width
= 10; /* default 10 for human units */
315 * r_* prefix is for raw data
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 */
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;
332 double metadata_ratio
;
336 u64 l_global_reserve
= 0;
337 u64 l_global_reserve_used
= 0;
338 u64 free_estimated
= 0;
340 int max_data_ratio
= 1;
343 sargs
= load_space_info(fd
, path
);
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
));
363 get_raid56_used(chunkinfo
, chunkcount
, &raid5_used
, &raid6_used
);
365 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
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
)
375 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
377 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
379 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
381 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
383 else if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
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
)) {
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
;
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
;
429 metadata_ratio
= data_ratio
;
431 metadata_ratio
= (double)r_metadata_chunks
/ l_metadata_chunks
;
434 /* add the raid5/6 allocated space */
435 total_chunks
+= raid5_used
+ raid6_used
;
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.
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
)
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(",
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",
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
));
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
)
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
);
527 error("cannot get filesystem info: %s",
532 info
= calloc(fi_args
.num_devices
, sizeof(struct device_info
));
534 error("not enough memory");
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
);
544 memset(&dev_info
, 0, sizeof(dev_info
));
545 ret
= get_device_info(fd
, i
, &dev_info
);
550 error("cannot get info about device devid=%d", i
);
554 info
[ndevs
].devid
= dev_info
.devid
;
555 if (!dev_info
.path
[0]) {
556 strcpy(info
[ndevs
].path
, "missing");
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
;
566 if (ndevs
!= fi_args
.num_devices
) {
567 error("unexpected number of devices: %d != %llu", ndevs
,
568 (unsigned long long)fi_args
.num_devices
);
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
;
585 int load_chunk_and_device_info(int fd
, struct chunk_info
**chunkinfo
,
586 int *chunkcount
, struct device_info
**devinfo
, int *devcount
)
590 ret
= load_chunk_info(fd
, chunkinfo
, chunkcount
);
593 "cannot read detailed chunk info, RAID5/6 numbers will be incorrect, run as root");
598 ret
= load_device_info(fd
, devinfo
, devcount
);
601 "cannot get filesystem info from ioctl(FS_INFO), run as root");
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
)
617 else if (ci
->type
& BTRFS_BLOCK_GROUP_DUP
)
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
;
629 * This function print the results of the command "btrfs fi usage"
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
)
640 u64 total_unused
= 0;
641 struct string_table
*matrix
= NULL
;
646 const int vhdr_skip
= 3; /* amount of vertical header space */
648 /* id, path, unallocated */
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
)
658 /* 2 for header, empty line, devices, ===, total, used */
659 nrows
= vhdr_skip
+ device_info_count
+ 1 + 2;
661 matrix
= table_create(ncols
, nrows
);
663 error("not enough memory");
668 * We have to skip the global block reserve everywhere as it's an
669 * artificial blockgroup
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
)
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
));
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");
692 for (i
= 0; i
< device_info_count
; i
++) {
696 u64 total_allocated
= 0, unused
;
698 p
= strrchr(device_info_ptr
[i
].path
, '/');
700 p
= device_info_ptr
[i
].path
;
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
;
715 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
718 for (j
= 0 ; j
< chunks_info_count
; j
++) {
719 if (chunks_info_ptr
[j
].type
!= flags
)
721 if (chunks_info_ptr
[j
].devid
!= devid
)
724 size
+= calc_chunk_size(chunks_info_ptr
+j
);
728 table_printf(matrix
, col
, vhdr_skip
+ i
,
729 ">%s", pretty_size_mode(size
, unit_mode
));
731 table_printf(matrix
, col
, vhdr_skip
+ i
, ">-");
733 total_allocated
+= size
;
737 unused
= get_partition_size(device_info_ptr
[i
].path
)
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
)
755 table_printf(matrix
, col
, vhdr_skip
- 1, "*-");
756 table_printf(matrix
, col
, vhdr_skip
+ device_info_count
, "*-");
759 /* One for Unallocated */
760 table_printf(matrix
, col
, vhdr_skip
- 1, "*-");
761 table_printf(matrix
, col
, vhdr_skip
+ device_info_count
, "*-");
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
)
769 table_printf(matrix
, col
++, vhdr_skip
+ device_info_count
+ 1,
771 pretty_size_mode(sargs
->spaces
[i
].total_bytes
, unit_mode
));
774 table_printf(matrix
, unallocated_col
, vhdr_skip
+ device_info_count
+ 1,
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
)
783 table_printf(matrix
, col
++, vhdr_skip
+ device_info_count
+ 2,
785 pretty_size_mode(sargs
->spaces
[i
].used_bytes
, unit_mode
));
793 * This function prints the unused space per every disk
795 static void print_unused(struct chunk_info
*info_ptr
,
797 struct device_info
*device_info_ptr
,
798 int device_info_count
,
802 for (i
= 0; i
< device_info_count
; i
++) {
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
,
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
,
829 for (i
= 0; i
< device_info_count
; i
++) {
833 for (j
= 0; j
< chunks_info_count
; j
++) {
835 if (chunks_info_ptr
[j
].type
!= chunk_type
)
837 if (chunks_info_ptr
[j
].devid
!= device_info_ptr
[i
].devid
)
840 total
+= calc_chunk_size(&(chunks_info_ptr
[j
]));
841 //total += chunks_info_ptr[j].size;
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"
855 static void _cmd_filesystem_usage_linear(unsigned unit_mode
,
856 struct btrfs_ioctl_space_args
*sargs
,
857 struct chunk_info
*info_ptr
,
859 struct device_info
*device_info_ptr
,
860 int device_info_count
)
864 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
865 const char *description
;
867 u64 flags
= sargs
->spaces
[i
].flags
;
869 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
872 description
= btrfs_group_type_str(flags
);
873 r_mode
= btrfs_group_profile_str(flags
);
875 printf("%s,%s: Size:%s, ",
878 pretty_size_mode(sargs
->spaces
[i
].total_bytes
,
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
);
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
;
903 sargs
= load_space_info(fd
, path
);
910 _cmd_filesystem_usage_tabular(unit_mode
, sargs
, chunkinfo
,
911 chunkcount
, devinfo
, devcount
);
913 _cmd_filesystem_usage_linear(unit_mode
, sargs
, chunkinfo
,
914 chunkcount
, devinfo
, devcount
);
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",
929 int cmd_filesystem_usage(int argc
, char **argv
)
934 int more_than_one
= 0;
937 unit_mode
= get_unit_mode_from_arg(&argc
, argv
, 1);
942 c
= getopt(argc
, argv
, "T");
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
++) {
960 DIR *dirstream
= NULL
;
961 struct chunk_info
*chunkinfo
= NULL
;
962 struct device_info
*devinfo
= NULL
;
966 fd
= btrfs_open_dir(argv
[i
], &dirstream
, 1);
974 ret
= load_chunk_and_device_info(fd
, &chunkinfo
, &chunkcount
,
975 &devinfo
, &devcount
);
979 ret
= print_filesystem_usage_overall(fd
, chunkinfo
, chunkcount
,
980 devinfo
, devcount
, argv
[i
], unit_mode
);
984 ret
= print_filesystem_usage_by_chunk(fd
, chunkinfo
, chunkcount
,
985 devinfo
, devcount
, argv
[i
], unit_mode
, tabular
);
987 close_file_or_dir(fd
, dirstream
);
1000 void print_device_chunks(struct device_info
*devinfo
,
1001 struct chunk_info
*chunks_info_ptr
,
1002 int chunks_info_count
, unsigned unit_mode
)
1007 for (i
= 0 ; i
< chunks_info_count
; i
++) {
1008 const char *description
;
1013 if (chunks_info_ptr
[i
].devid
!= devinfo
->devid
)
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",
1024 (int)(20 - strlen(description
) - strlen(r_mode
)), "",
1025 pretty_size_mode(size
, unit_mode
));
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
,