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
= NULL
;
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 tmp
= sizeof(struct btrfs_chunk
) * (*info_count
+ 1);
67 struct chunk_info
*res
= realloc(*info_ptr
, tmp
);
71 error("not enough memory");
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
);
164 error("cannot look up chunk tree info: %s",
168 /* the ioctl returns the number of item it found in nr_items */
170 if (sk
->nr_items
== 0)
174 for (i
= 0; i
< sk
->nr_items
; i
++) {
175 struct btrfs_chunk
*item
;
176 sh
= (struct btrfs_ioctl_search_header
*)(args
.buf
+
180 item
= (struct btrfs_chunk
*)(args
.buf
+ off
);
182 ret
= add_info_to_list(info_ptr
, info_count
, item
);
188 off
+= btrfs_search_header_len(sh
);
190 sk
->min_objectid
= btrfs_search_header_objectid(sh
);
191 sk
->min_type
= btrfs_search_header_type(sh
);
192 sk
->min_offset
= btrfs_search_header_offset(sh
)+1;
195 if (!sk
->min_offset
) /* overflow */
205 if (!sk
->min_objectid
)
209 qsort(*info_ptr
, *info_count
, sizeof(struct chunk_info
),
216 * Helper to sort the struct btrfs_ioctl_space_info
218 static int cmp_btrfs_ioctl_space_info(const void *a
, const void *b
)
220 return cmp_chunk_block_group(
221 ((struct btrfs_ioctl_space_info
*)a
)->flags
,
222 ((struct btrfs_ioctl_space_info
*)b
)->flags
);
226 * This function load all the information about the space usage
228 static struct btrfs_ioctl_space_args
*load_space_info(int fd
, char *path
)
230 struct btrfs_ioctl_space_args
*sargs
= NULL
, *sargs_orig
= NULL
;
233 sargs_orig
= sargs
= calloc(1, sizeof(struct btrfs_ioctl_space_args
));
235 error("not enough memory");
239 sargs
->space_slots
= 0;
240 sargs
->total_spaces
= 0;
242 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
244 error("cannot get space info on '%s': %s", path
,
249 if (!sargs
->total_spaces
) {
251 printf("No chunks found\n");
255 count
= sargs
->total_spaces
;
257 sargs
= realloc(sargs
, sizeof(struct btrfs_ioctl_space_args
) +
258 (count
* sizeof(struct btrfs_ioctl_space_info
)));
261 error("not enough memory");
265 sargs
->space_slots
= count
;
266 sargs
->total_spaces
= 0;
268 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
270 error("cannot get space info with %u slots: %s",
271 count
, strerror(errno
));
276 qsort(&(sargs
->spaces
), count
, sizeof(struct btrfs_ioctl_space_info
),
277 cmp_btrfs_ioctl_space_info
);
283 * This function computes the space occupied by a *single* RAID5/RAID6 chunk.
284 * The computation is performed on the basis of the number of stripes
285 * which compose the chunk, which could be different from the number of devices
286 * if a disk is added later.
288 static void get_raid56_used(int fd
, struct chunk_info
*chunks
, int chunkcount
,
289 u64
*raid5_used
, u64
*raid6_used
)
291 struct chunk_info
*info_ptr
= chunks
;
295 while (chunkcount
-- > 0) {
296 if (info_ptr
->type
& BTRFS_BLOCK_GROUP_RAID5
)
297 (*raid5_used
) += info_ptr
->size
/ (info_ptr
->num_stripes
- 1);
298 if (info_ptr
->type
& BTRFS_BLOCK_GROUP_RAID6
)
299 (*raid6_used
) += info_ptr
->size
/ (info_ptr
->num_stripes
- 2);
304 #define MIN_UNALOCATED_THRESH (16 * 1024 * 1024)
305 static int print_filesystem_usage_overall(int fd
, struct chunk_info
*chunkinfo
,
306 int chunkcount
, struct device_info
*devinfo
, int devcount
,
307 char *path
, unsigned unit_mode
)
309 struct btrfs_ioctl_space_args
*sargs
= NULL
;
312 int width
= 10; /* default 10 for human units */
314 * r_* prefix is for raw data
317 u64 r_total_size
= 0; /* filesystem size, sum of device sizes */
318 u64 r_total_chunks
= 0; /* sum of chunks sizes on disk(s) */
319 u64 r_total_used
= 0;
320 u64 r_total_unused
= 0;
321 u64 r_total_missing
= 0; /* sum of missing devices size */
323 u64 r_data_chunks
= 0;
324 u64 l_data_chunks
= 0;
325 u64 r_metadata_used
= 0;
326 u64 r_metadata_chunks
= 0;
327 u64 l_metadata_chunks
= 0;
328 u64 r_system_used
= 0;
329 u64 r_system_chunks
= 0;
331 double metadata_ratio
;
335 u64 l_global_reserve
= 0;
336 u64 l_global_reserve_used
= 0;
337 u64 free_estimated
= 0;
339 int max_data_ratio
= 1;
342 sargs
= load_space_info(fd
, path
);
349 for (i
= 0; i
< devcount
; i
++) {
350 r_total_size
+= devinfo
[i
].size
;
351 if (!devinfo
[i
].device_size
)
352 r_total_missing
+= devinfo
[i
].size
;
355 if (r_total_size
== 0) {
356 error("cannot get space info on '%s': %s",
357 path
, strerror(errno
));
362 get_raid56_used(fd
, chunkinfo
, chunkcount
, &raid5_used
, &raid6_used
);
364 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
366 u64 flags
= sargs
->spaces
[i
].flags
;
369 * The raid5/raid6 ratio depends by the stripes number
370 * used by every chunk. It is computed separately
372 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
374 else if (flags
& BTRFS_BLOCK_GROUP_RAID1
)
376 else if (flags
& BTRFS_BLOCK_GROUP_RAID5
)
378 else if (flags
& BTRFS_BLOCK_GROUP_RAID6
)
380 else if (flags
& BTRFS_BLOCK_GROUP_DUP
)
382 else if (flags
& BTRFS_BLOCK_GROUP_RAID10
)
388 warning("RAID56 detected, not implemented");
390 if (ratio
> max_data_ratio
)
391 max_data_ratio
= ratio
;
393 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
) {
394 l_global_reserve
= sargs
->spaces
[i
].total_bytes
;
395 l_global_reserve_used
= sargs
->spaces
[i
].used_bytes
;
397 if ((flags
& (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
))
398 == (BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
)) {
401 if (flags
& BTRFS_BLOCK_GROUP_DATA
) {
402 r_data_used
+= sargs
->spaces
[i
].used_bytes
* ratio
;
403 r_data_chunks
+= sargs
->spaces
[i
].total_bytes
* ratio
;
404 l_data_chunks
+= sargs
->spaces
[i
].total_bytes
;
406 if (flags
& BTRFS_BLOCK_GROUP_METADATA
) {
407 r_metadata_used
+= sargs
->spaces
[i
].used_bytes
* ratio
;
408 r_metadata_chunks
+= sargs
->spaces
[i
].total_bytes
* ratio
;
409 l_metadata_chunks
+= sargs
->spaces
[i
].total_bytes
;
411 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
412 r_system_used
+= sargs
->spaces
[i
].used_bytes
* ratio
;
413 r_system_chunks
+= sargs
->spaces
[i
].total_bytes
* ratio
;
417 r_total_chunks
= r_data_chunks
+ r_system_chunks
;
418 r_total_used
= r_data_used
+ r_system_used
;
420 r_total_chunks
+= r_metadata_chunks
;
421 r_total_used
+= r_metadata_used
;
423 r_total_unused
= r_total_size
- r_total_chunks
;
425 /* Raw / Logical = raid factor, >= 1 */
426 data_ratio
= (double)r_data_chunks
/ l_data_chunks
;
428 metadata_ratio
= data_ratio
;
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
;
448 * For mixed-bg the metadata are left out in calculations thus global
449 * reserve would be lost. Part of it could be permanently allocated,
450 * we have to subtract the used bytes so we don't go under zero free.
453 free_estimated
-= l_global_reserve
- l_global_reserve_used
;
454 free_min
= free_estimated
;
456 /* Chop unallocatable space */
457 /* FIXME: must be applied per device */
458 if (r_total_unused
>= MIN_UNALOCATED_THRESH
) {
459 free_estimated
+= r_total_unused
/ data_ratio
;
460 /* Match the calculation of 'df', use the highest raid ratio */
461 free_min
+= r_total_unused
/ max_data_ratio
;
464 if (unit_mode
!= UNITS_HUMAN
)
467 printf("Overall:\n");
469 printf(" Device size:\t\t%*s\n", width
,
470 pretty_size_mode(r_total_size
, unit_mode
));
471 printf(" Device allocated:\t\t%*s\n", width
,
472 pretty_size_mode(r_total_chunks
, unit_mode
));
473 printf(" Device unallocated:\t\t%*s\n", width
,
474 pretty_size_mode(r_total_unused
, unit_mode
));
475 printf(" Device missing:\t\t%*s\n", width
,
476 pretty_size_mode(r_total_missing
, unit_mode
));
477 printf(" Used:\t\t\t%*s\n", width
,
478 pretty_size_mode(r_total_used
, unit_mode
));
479 printf(" Free (estimated):\t\t%*s\t(",
481 pretty_size_mode(free_estimated
, unit_mode
));
482 printf("min: %s)\n", pretty_size_mode(free_min
, unit_mode
));
483 printf(" Data ratio:\t\t\t%*.2f\n",
485 printf(" Metadata ratio:\t\t%*.2f\n",
486 width
, metadata_ratio
);
487 printf(" Global reserve:\t\t%*s\t(used: %s)\n", width
,
488 pretty_size_mode(l_global_reserve
, unit_mode
),
489 pretty_size_mode(l_global_reserve_used
, unit_mode
));
500 * Helper to sort the device_info structure
502 static int cmp_device_info(const void *a
, const void *b
)
504 return strcmp(((struct device_info
*)a
)->path
,
505 ((struct device_info
*)b
)->path
);
509 * This function loads the device_info structure and put them in an array
511 static int load_device_info(int fd
, struct device_info
**device_info_ptr
,
512 int *device_info_count
)
515 struct btrfs_ioctl_fs_info_args fi_args
;
516 struct btrfs_ioctl_dev_info_args dev_info
;
517 struct device_info
*info
;
519 *device_info_count
= 0;
520 *device_info_ptr
= NULL
;
522 ret
= ioctl(fd
, BTRFS_IOC_FS_INFO
, &fi_args
);
526 error("cannot get filesystem info: %s",
531 info
= calloc(fi_args
.num_devices
, sizeof(struct device_info
));
533 error("not enough memory");
537 for (i
= 0, ndevs
= 0 ; i
<= fi_args
.max_id
; i
++) {
538 BUG_ON(ndevs
>= fi_args
.num_devices
);
539 memset(&dev_info
, 0, sizeof(dev_info
));
540 ret
= get_device_info(fd
, i
, &dev_info
);
545 error("cannot get info about device devid=%d", i
);
550 info
[ndevs
].devid
= dev_info
.devid
;
551 if (!dev_info
.path
[0]) {
552 strcpy(info
[ndevs
].path
, "missing");
554 strcpy(info
[ndevs
].path
, (char *)dev_info
.path
);
555 info
[ndevs
].device_size
=
556 get_partition_size((char *)dev_info
.path
);
558 info
[ndevs
].size
= dev_info
.total_bytes
;
562 BUG_ON(ndevs
!= fi_args
.num_devices
);
563 qsort(info
, fi_args
.num_devices
,
564 sizeof(struct device_info
), cmp_device_info
);
566 *device_info_count
= fi_args
.num_devices
;
567 *device_info_ptr
= info
;
572 int load_chunk_and_device_info(int fd
, struct chunk_info
**chunkinfo
,
573 int *chunkcount
, struct device_info
**devinfo
, int *devcount
)
577 ret
= load_chunk_info(fd
, chunkinfo
, chunkcount
);
580 "cannot read detailed chunk info, RAID5/6 numbers will be incorrect, run as root");
585 ret
= load_device_info(fd
, devinfo
, devcount
);
588 "cannot get filesystem info from ioctl(FS_INFO), run as root");
596 * This function computes the size of a chunk in a disk
598 static u64
calc_chunk_size(struct chunk_info
*ci
)
600 if (ci
->type
& BTRFS_BLOCK_GROUP_RAID0
)
601 return ci
->size
/ ci
->num_stripes
;
602 else if (ci
->type
& BTRFS_BLOCK_GROUP_RAID1
)
604 else if (ci
->type
& BTRFS_BLOCK_GROUP_DUP
)
606 else if (ci
->type
& BTRFS_BLOCK_GROUP_RAID5
)
607 return ci
->size
/ (ci
->num_stripes
-1);
608 else if (ci
->type
& BTRFS_BLOCK_GROUP_RAID6
)
609 return ci
->size
/ (ci
->num_stripes
-2);
610 else if (ci
->type
& BTRFS_BLOCK_GROUP_RAID10
)
611 return ci
->size
/ ci
->num_stripes
;
616 * This function print the results of the command "btrfs fi usage"
619 static void _cmd_filesystem_usage_tabular(unsigned unit_mode
,
620 struct btrfs_ioctl_space_args
*sargs
,
621 struct chunk_info
*chunks_info_ptr
,
622 int chunks_info_count
,
623 struct device_info
*device_info_ptr
,
624 int device_info_count
)
627 u64 total_unused
= 0;
628 struct string_table
*matrix
= NULL
;
633 const int vhdr_skip
= 3; /* amount of vertical header space */
635 /* id, path, unallocated */
638 /* Properly count the real space infos */
639 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
640 if (sargs
->spaces
[i
].flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
645 /* 2 for header, empty line, devices, ===, total, used */
646 nrows
= vhdr_skip
+ device_info_count
+ 1 + 2;
648 matrix
= table_create(ncols
, nrows
);
650 error("not enough memory");
655 * We have to skip the global block reserve everywhere as it's an
656 * artificial blockgroup
660 for (i
= 0, col
= spaceinfos_col
; i
< sargs
->total_spaces
; i
++) {
661 u64 flags
= sargs
->spaces
[i
].flags
;
663 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
666 table_printf(matrix
, col
, 0, "<%s",
667 btrfs_group_type_str(flags
));
668 table_printf(matrix
, col
, 1, "<%s",
669 btrfs_group_profile_str(flags
));
672 unallocated_col
= col
;
674 table_printf(matrix
, 0, 1, "<Id");
675 table_printf(matrix
, 1, 1, "<Path");
676 table_printf(matrix
, unallocated_col
, 1, "<Unallocated");
679 for (i
= 0; i
< device_info_count
; i
++) {
683 u64 total_allocated
= 0, unused
;
685 p
= strrchr(device_info_ptr
[i
].path
, '/');
687 p
= device_info_ptr
[i
].path
;
691 table_printf(matrix
, 0, vhdr_skip
+ i
, ">%llu",
692 device_info_ptr
[i
].devid
);
693 table_printf(matrix
, 1, vhdr_skip
+ i
, "<%s",
694 device_info_ptr
[i
].path
);
696 for (col
= spaceinfos_col
, k
= 0; k
< sargs
->total_spaces
; k
++) {
697 u64 flags
= sargs
->spaces
[k
].flags
;
698 u64 devid
= device_info_ptr
[i
].devid
;
702 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
705 for (j
= 0 ; j
< chunks_info_count
; j
++) {
706 if (chunks_info_ptr
[j
].type
!= flags
)
708 if (chunks_info_ptr
[j
].devid
!= devid
)
711 size
+= calc_chunk_size(chunks_info_ptr
+j
);
715 table_printf(matrix
, col
, vhdr_skip
+ i
,
716 ">%s", pretty_size_mode(size
, unit_mode
));
718 table_printf(matrix
, col
, vhdr_skip
+ i
, ">-");
720 total_allocated
+= size
;
724 unused
= get_partition_size(device_info_ptr
[i
].path
)
727 table_printf(matrix
, unallocated_col
, vhdr_skip
+ i
,
728 ">%s", pretty_size_mode(unused
, unit_mode
));
729 total_unused
+= unused
;
733 for (i
= 0; i
< spaceinfos_col
; i
++) {
734 table_printf(matrix
, i
, vhdr_skip
- 1, "*-");
735 table_printf(matrix
, i
, vhdr_skip
+ device_info_count
, "*-");
738 for (i
= 0, col
= spaceinfos_col
; i
< sargs
->total_spaces
; i
++) {
739 if (sargs
->spaces
[i
].flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
742 table_printf(matrix
, col
, vhdr_skip
- 1, "*-");
743 table_printf(matrix
, col
, vhdr_skip
+ device_info_count
, "*-");
746 /* One for Unallocated */
747 table_printf(matrix
, col
, vhdr_skip
- 1, "*-");
748 table_printf(matrix
, col
, vhdr_skip
+ device_info_count
, "*-");
751 table_printf(matrix
, 1, vhdr_skip
+ device_info_count
+ 1, "<Total");
752 for (i
= 0, col
= spaceinfos_col
; i
< sargs
->total_spaces
; i
++) {
753 if (sargs
->spaces
[i
].flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
756 table_printf(matrix
, col
++, vhdr_skip
+ device_info_count
+ 1,
758 pretty_size_mode(sargs
->spaces
[i
].total_bytes
, unit_mode
));
761 table_printf(matrix
, unallocated_col
, vhdr_skip
+ device_info_count
+ 1,
762 ">%s", pretty_size_mode(total_unused
, unit_mode
));
764 table_printf(matrix
, 1, vhdr_skip
+ device_info_count
+ 2, "<Used");
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
+ 2,
771 pretty_size_mode(sargs
->spaces
[i
].used_bytes
, unit_mode
));
779 * This function prints the unused space per every disk
781 static void print_unused(struct chunk_info
*info_ptr
,
783 struct device_info
*device_info_ptr
,
784 int device_info_count
,
788 for (i
= 0; i
< device_info_count
; i
++) {
792 for (j
= 0; j
< info_count
; j
++)
793 if (info_ptr
[j
].devid
== device_info_ptr
[i
].devid
)
794 total
+= calc_chunk_size(info_ptr
+j
);
796 printf(" %s\t%10s\n",
797 device_info_ptr
[i
].path
,
798 pretty_size_mode(device_info_ptr
[i
].size
- total
,
804 * This function prints the allocated chunk per every disk
806 static void print_chunk_device(u64 chunk_type
,
807 struct chunk_info
*chunks_info_ptr
,
808 int chunks_info_count
,
809 struct device_info
*device_info_ptr
,
810 int device_info_count
,
815 for (i
= 0; i
< device_info_count
; i
++) {
819 for (j
= 0; j
< chunks_info_count
; j
++) {
821 if (chunks_info_ptr
[j
].type
!= chunk_type
)
823 if (chunks_info_ptr
[j
].devid
!= device_info_ptr
[i
].devid
)
826 total
+= calc_chunk_size(&(chunks_info_ptr
[j
]));
827 //total += chunks_info_ptr[j].size;
831 printf(" %s\t%10s\n",
832 device_info_ptr
[i
].path
,
833 pretty_size_mode(total
, unit_mode
));
838 * This function print the results of the command "btrfs fi usage"
841 static void _cmd_filesystem_usage_linear(unsigned unit_mode
,
842 struct btrfs_ioctl_space_args
*sargs
,
843 struct chunk_info
*info_ptr
,
845 struct device_info
*device_info_ptr
,
846 int device_info_count
)
850 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
851 const char *description
;
853 u64 flags
= sargs
->spaces
[i
].flags
;
855 if (flags
& BTRFS_SPACE_INFO_GLOBAL_RSV
)
858 description
= btrfs_group_type_str(flags
);
859 r_mode
= btrfs_group_profile_str(flags
);
861 printf("%s,%s: Size:%s, ",
864 pretty_size_mode(sargs
->spaces
[i
].total_bytes
,
867 pretty_size_mode(sargs
->spaces
[i
].used_bytes
, unit_mode
));
868 print_chunk_device(flags
, info_ptr
, info_count
,
869 device_info_ptr
, device_info_count
, unit_mode
);
873 printf("Unallocated:\n");
874 print_unused(info_ptr
, info_count
, device_info_ptr
, device_info_count
,
878 static int print_filesystem_usage_by_chunk(int fd
,
879 struct chunk_info
*chunkinfo
, int chunkcount
,
880 struct device_info
*devinfo
, int devcount
,
881 char *path
, unsigned unit_mode
, int tabular
)
883 struct btrfs_ioctl_space_args
*sargs
;
889 sargs
= load_space_info(fd
, path
);
896 _cmd_filesystem_usage_tabular(unit_mode
, sargs
, chunkinfo
,
897 chunkcount
, devinfo
, devcount
);
899 _cmd_filesystem_usage_linear(unit_mode
, sargs
, chunkinfo
,
900 chunkcount
, devinfo
, devcount
);
907 const char * const cmd_filesystem_usage_usage
[] = {
908 "btrfs filesystem usage [options] <path> [<path>..]",
909 "Show detailed information about internal filesystem usage .",
910 HELPINFO_UNITS_SHORT_LONG
,
911 "-T show data in tabular format",
915 int cmd_filesystem_usage(int argc
, char **argv
)
920 int more_than_one
= 0;
923 unit_mode
= get_unit_mode_from_arg(&argc
, argv
, 1);
928 c
= getopt(argc
, argv
, "T");
937 usage(cmd_filesystem_usage_usage
);
941 if (check_argc_min(argc
- optind
, 1))
942 usage(cmd_filesystem_usage_usage
);
944 for (i
= optind
; i
< argc
; i
++) {
946 DIR *dirstream
= NULL
;
947 struct chunk_info
*chunkinfo
= NULL
;
948 struct device_info
*devinfo
= NULL
;
952 fd
= btrfs_open_dir(argv
[i
], &dirstream
, 1);
960 ret
= load_chunk_and_device_info(fd
, &chunkinfo
, &chunkcount
,
961 &devinfo
, &devcount
);
965 ret
= print_filesystem_usage_overall(fd
, chunkinfo
, chunkcount
,
966 devinfo
, devcount
, argv
[i
], unit_mode
);
970 ret
= print_filesystem_usage_by_chunk(fd
, chunkinfo
, chunkcount
,
971 devinfo
, devcount
, argv
[i
], unit_mode
, tabular
);
973 close_file_or_dir(fd
, dirstream
);
986 void print_device_chunks(int fd
, struct device_info
*devinfo
,
987 struct chunk_info
*chunks_info_ptr
,
988 int chunks_info_count
, unsigned unit_mode
)
993 for (i
= 0 ; i
< chunks_info_count
; i
++) {
994 const char *description
;
999 if (chunks_info_ptr
[i
].devid
!= devinfo
->devid
)
1002 flags
= chunks_info_ptr
[i
].type
;
1004 description
= btrfs_group_type_str(flags
);
1005 r_mode
= btrfs_group_profile_str(flags
);
1006 size
= calc_chunk_size(chunks_info_ptr
+i
);
1007 printf(" %s,%s:%*s%10s\n",
1010 (int)(20 - strlen(description
) - strlen(r_mode
)), "",
1011 pretty_size_mode(size
, unit_mode
));
1016 printf(" Unallocated: %*s%10s\n",
1017 (int)(20 - strlen("Unallocated")), "",
1018 pretty_size_mode(devinfo
->size
- allocated
, unit_mode
));
1021 void print_device_sizes(int fd
, struct device_info
*devinfo
, unsigned unit_mode
)
1023 printf(" Device size: %*s%10s\n",
1024 (int)(20 - strlen("Device size")), "",
1025 pretty_size_mode(devinfo
->device_size
, unit_mode
));
1026 printf(" Device slack: %*s%10s\n",
1027 (int)(20 - strlen("Device slack")), "",
1028 pretty_size_mode(devinfo
->device_size
- devinfo
->size
,