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 #define _XOPEN_SOURCE 500
22 #include <sys/ioctl.h>
24 #include <uuid/uuid.h>
29 #include <linux/limits.h>
32 #include "kerncompat.h"
39 #include "list_sort.h"
44 * for btrfs fi show, we maintain a hash of fsids we've already printed.
45 * This way we don't print dups if a given FS is mounted more than once.
47 #define SEEN_FSID_HASH_SIZE 256
50 u8 fsid
[BTRFS_FSID_SIZE
];
51 struct seen_fsid
*next
;
54 static struct seen_fsid
*seen_fsid_hash
[SEEN_FSID_HASH_SIZE
] = {NULL
,};
56 static int is_seen_fsid(u8
*fsid
)
59 int slot
= hash
% SEEN_FSID_HASH_SIZE
;
60 struct seen_fsid
*seen
= seen_fsid_hash
[slot
];
65 static int add_seen_fsid(u8
*fsid
)
68 int slot
= hash
% SEEN_FSID_HASH_SIZE
;
69 struct seen_fsid
*seen
= seen_fsid_hash
[slot
];
70 struct seen_fsid
*alloc
;
76 if (memcmp(seen
->fsid
, fsid
, BTRFS_FSID_SIZE
) == 0)
87 alloc
= malloc(sizeof(*alloc
));
92 memcpy(alloc
->fsid
, fsid
, BTRFS_FSID_SIZE
);
97 seen_fsid_hash
[slot
] = alloc
;
102 static void free_seen_fsid(void)
105 struct seen_fsid
*seen
;
106 struct seen_fsid
*next
;
108 for (slot
= 0; slot
< SEEN_FSID_HASH_SIZE
; slot
++) {
109 seen
= seen_fsid_hash
[slot
];
115 seen_fsid_hash
[slot
] = NULL
;
119 static const char * const filesystem_cmd_group_usage
[] = {
120 "btrfs filesystem [<group>] <command> [<args>]",
124 static const char * const cmd_df_usage
[] = {
125 "btrfs filesystem df [options] <path>",
126 "Show space usage information for a mount point",
127 "-b|--raw raw numbers in bytes",
128 "-h human friendly numbers, base 1024 (default)",
129 "-H human friendly numbers, base 1000",
130 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
131 "--si use 1000 as a base (kB, MB, GB, TB)",
132 "-k|--kbytes show sizes in KiB, or kB with --si",
133 "-m|--mbytes show sizes in MiB, or MB with --si",
134 "-g|--gbytes show sizes in GiB, or GB with --si",
135 "-t|--tbytes show sizes in TiB, or TB with --si",
139 static char *group_type_str(u64 flag
)
141 u64 mask
= BTRFS_BLOCK_GROUP_TYPE_MASK
|
142 BTRFS_SPACE_INFO_GLOBAL_RSV
;
144 switch (flag
& mask
) {
145 case BTRFS_BLOCK_GROUP_DATA
:
147 case BTRFS_BLOCK_GROUP_SYSTEM
:
149 case BTRFS_BLOCK_GROUP_METADATA
:
151 case BTRFS_BLOCK_GROUP_DATA
|BTRFS_BLOCK_GROUP_METADATA
:
152 return "Data+Metadata";
153 case BTRFS_SPACE_INFO_GLOBAL_RSV
:
154 return "GlobalReserve";
160 static char *group_profile_str(u64 flag
)
162 switch (flag
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
165 case BTRFS_BLOCK_GROUP_RAID0
:
167 case BTRFS_BLOCK_GROUP_RAID1
:
169 case BTRFS_BLOCK_GROUP_RAID5
:
171 case BTRFS_BLOCK_GROUP_RAID6
:
173 case BTRFS_BLOCK_GROUP_DUP
:
175 case BTRFS_BLOCK_GROUP_RAID10
:
182 static int get_df(int fd
, struct btrfs_ioctl_space_args
**sargs_ret
)
186 struct btrfs_ioctl_space_args
*sargs
;
188 sargs
= malloc(sizeof(struct btrfs_ioctl_space_args
));
192 sargs
->space_slots
= 0;
193 sargs
->total_spaces
= 0;
195 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
198 fprintf(stderr
, "ERROR: couldn't get space info - %s\n",
203 /* This really should never happen */
204 if (!sargs
->total_spaces
) {
208 count
= sargs
->total_spaces
;
211 sargs
= malloc(sizeof(struct btrfs_ioctl_space_args
) +
212 (count
* sizeof(struct btrfs_ioctl_space_info
)));
216 sargs
->space_slots
= count
;
217 sargs
->total_spaces
= 0;
218 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
221 fprintf(stderr
, "ERROR: get space info count %llu - %s\n",
230 static void print_df(struct btrfs_ioctl_space_args
*sargs
, unsigned unit_mode
)
233 struct btrfs_ioctl_space_info
*sp
= sargs
->spaces
;
235 for (i
= 0; i
< sargs
->total_spaces
; i
++, sp
++) {
236 printf("%s, %s: total=%s, used=%s\n",
237 group_type_str(sp
->flags
),
238 group_profile_str(sp
->flags
),
239 pretty_size_mode(sp
->total_bytes
, unit_mode
),
240 pretty_size_mode(sp
->used_bytes
, unit_mode
));
244 static int cmd_df(int argc
, char **argv
)
246 struct btrfs_ioctl_space_args
*sargs
= NULL
;
250 DIR *dirstream
= NULL
;
251 unsigned unit_mode
= UNITS_DEFAULT
;
255 static const struct option long_options
[] = {
256 { "raw", no_argument
, NULL
, 'b'},
257 { "kbytes", no_argument
, NULL
, 'k'},
258 { "mbytes", no_argument
, NULL
, 'm'},
259 { "gbytes", no_argument
, NULL
, 'g'},
260 { "tbytes", no_argument
, NULL
, 't'},
261 { "si", no_argument
, NULL
, 256},
262 { "iec", no_argument
, NULL
, 257},
264 int c
= getopt_long(argc
, argv
, "bhHkmgt", long_options
,
270 unit_mode
= UNITS_RAW
;
273 units_set_base(&unit_mode
, UNITS_KBYTES
);
276 units_set_base(&unit_mode
, UNITS_MBYTES
);
279 units_set_base(&unit_mode
, UNITS_GBYTES
);
282 units_set_base(&unit_mode
, UNITS_TBYTES
);
285 unit_mode
= UNITS_HUMAN_BINARY
;
288 unit_mode
= UNITS_HUMAN_DECIMAL
;
291 units_set_mode(&unit_mode
, UNITS_DECIMAL
);
294 units_set_mode(&unit_mode
, UNITS_BINARY
);
301 if (check_argc_exact(argc
, optind
+ 1))
306 fd
= open_file_or_dir(path
, &dirstream
);
308 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
311 ret
= get_df(fd
, &sargs
);
314 print_df(sargs
, unit_mode
);
317 fprintf(stderr
, "ERROR: get_df failed %s\n", strerror(-ret
));
320 close_file_or_dir(fd
, dirstream
);
324 static int match_search_item_kernel(__u8
*fsid
, char *mnt
, char *label
,
327 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
328 int search_len
= strlen(search
);
330 search_len
= min(search_len
, BTRFS_UUID_UNPARSED_SIZE
);
331 uuid_unparse(fsid
, uuidbuf
);
332 if (!strncmp(uuidbuf
, search
, search_len
))
335 if (strlen(label
) && strcmp(label
, search
) == 0)
338 if (strcmp(mnt
, search
) == 0)
344 static int uuid_search(struct btrfs_fs_devices
*fs_devices
, char *search
)
346 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
347 struct list_head
*cur
;
348 struct btrfs_device
*device
;
349 int search_len
= strlen(search
);
351 search_len
= min(search_len
, BTRFS_UUID_UNPARSED_SIZE
);
352 uuid_unparse(fs_devices
->fsid
, uuidbuf
);
353 if (!strncmp(uuidbuf
, search
, search_len
))
356 list_for_each(cur
, &fs_devices
->devices
) {
357 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
358 if ((device
->label
&& strcmp(device
->label
, search
) == 0) ||
359 strcmp(device
->name
, search
) == 0)
366 * Sort devices by devid, ascending
368 static int cmp_device_id(void *priv
, struct list_head
*a
,
371 const struct btrfs_device
*da
= list_entry(a
, struct btrfs_device
,
373 const struct btrfs_device
*db
= list_entry(b
, struct btrfs_device
,
376 return da
->devid
< db
->devid
? -1 :
377 da
->devid
> db
->devid
? 1 : 0;
380 static void splice_device_list(struct list_head
*seed_devices
,
381 struct list_head
*all_devices
)
383 struct btrfs_device
*in_all
, *next_all
;
384 struct btrfs_device
*in_seed
, *next_seed
;
386 list_for_each_entry_safe(in_all
, next_all
, all_devices
, dev_list
) {
387 list_for_each_entry_safe(in_seed
, next_seed
, seed_devices
,
389 if (in_all
->devid
== in_seed
->devid
) {
391 * When do dev replace in a sprout fs
392 * to a dev in its seed fs, the replacing
393 * dev will reside in the sprout fs and
394 * the replaced dev will still exist
396 * So pick the latest one when showing
399 if (in_all
->generation
400 < in_seed
->generation
) {
401 list_del(&in_all
->dev_list
);
403 } else if (in_all
->generation
404 > in_seed
->generation
) {
405 list_del(&in_seed
->dev_list
);
413 list_splice(seed_devices
, all_devices
);
416 static void print_devices(struct btrfs_fs_devices
*fs_devices
,
419 struct btrfs_device
*device
;
420 struct btrfs_fs_devices
*cur_fs
;
421 struct list_head
*all_devices
;
423 all_devices
= &fs_devices
->devices
;
424 cur_fs
= fs_devices
->seed
;
425 /* add all devices of seed fs to the fs to be printed */
427 splice_device_list(&cur_fs
->devices
, all_devices
);
428 cur_fs
= cur_fs
->seed
;
431 list_sort(NULL
, all_devices
, cmp_device_id
);
432 list_for_each_entry(device
, all_devices
, dev_list
) {
433 printf("\tdevid %4llu size %s used %s path %s\n",
434 (unsigned long long)device
->devid
,
435 pretty_size(device
->total_bytes
),
436 pretty_size(device
->bytes_used
), device
->name
);
442 static void print_one_uuid(struct btrfs_fs_devices
*fs_devices
)
444 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
445 struct btrfs_device
*device
;
449 if (add_seen_fsid(fs_devices
->fsid
))
452 uuid_unparse(fs_devices
->fsid
, uuidbuf
);
453 device
= list_entry(fs_devices
->devices
.next
, struct btrfs_device
,
455 if (device
->label
&& device
->label
[0])
456 printf("Label: '%s' ", device
->label
);
458 printf("Label: none ");
460 total
= device
->total_devs
;
461 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf
,
462 (unsigned long long)total
,
463 pretty_size(device
->super_bytes_used
));
465 print_devices(fs_devices
, &devs_found
);
467 if (devs_found
< total
) {
468 printf("\t*** Some devices missing\n");
473 /* adds up all the used spaces as reported by the space info ioctl
475 static u64
calc_used_bytes(struct btrfs_ioctl_space_args
*si
)
479 for (i
= 0; i
< si
->total_spaces
; i
++)
480 ret
+= si
->spaces
[i
].used_bytes
;
484 static int print_one_fs(struct btrfs_ioctl_fs_info_args
*fs_info
,
485 struct btrfs_ioctl_dev_info_args
*dev_info
,
486 struct btrfs_ioctl_space_args
*space_info
,
487 char *label
, char *path
)
492 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
493 struct btrfs_ioctl_dev_info_args
*tmp_dev_info
;
496 ret
= add_seen_fsid(fs_info
->fsid
);
502 uuid_unparse(fs_info
->fsid
, uuidbuf
);
503 if (label
&& strlen(label
))
504 printf("Label: '%s' ", label
);
506 printf("Label: none ");
508 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf
,
509 fs_info
->num_devices
,
510 pretty_size(calc_used_bytes(space_info
)));
512 for (i
= 0; i
< fs_info
->num_devices
; i
++) {
513 tmp_dev_info
= (struct btrfs_ioctl_dev_info_args
*)&dev_info
[i
];
515 /* Add check for missing devices even mounted */
516 fd
= open((char *)tmp_dev_info
->path
, O_RDONLY
);
522 printf("\tdevid %4llu size %s used %s path %s\n",
524 pretty_size(tmp_dev_info
->total_bytes
),
525 pretty_size(tmp_dev_info
->bytes_used
),
530 printf("\t*** Some devices missing\n");
535 /* This function checks if the given input parameter is
537 * return -1: some error in the given input
538 * return 0: unknow input
539 * return 1: given input is uuid
540 * return 2: given input is path
542 static int check_arg_type(char *input
)
550 if (realpath(input
, path
)) {
551 if (is_block_device(path
) == 1)
552 return BTRFS_ARG_BLKDEV
;
554 if (is_mount_point(path
) == 1)
555 return BTRFS_ARG_MNTPOINT
;
557 return BTRFS_ARG_UNKNOWN
;
560 if (strlen(input
) == (BTRFS_UUID_UNPARSED_SIZE
- 1) &&
561 !uuid_parse(input
, out
))
562 return BTRFS_ARG_UUID
;
564 return BTRFS_ARG_UNKNOWN
;
567 static int btrfs_scan_kernel(void *search
)
573 struct btrfs_ioctl_fs_info_args fs_info_arg
;
574 struct btrfs_ioctl_dev_info_args
*dev_info_arg
= NULL
;
575 struct btrfs_ioctl_space_args
*space_info_arg
= NULL
;
576 char label
[BTRFS_LABEL_SIZE
];
578 f
= setmntent("/proc/self/mounts", "r");
582 memset(label
, 0, sizeof(label
));
583 while ((mnt
= getmntent(f
)) != NULL
) {
584 if (strcmp(mnt
->mnt_type
, "btrfs"))
586 ret
= get_fs_info(mnt
->mnt_dir
, &fs_info_arg
,
591 if (get_label_mounted(mnt
->mnt_dir
, label
)) {
595 if (search
&& !match_search_item_kernel(fs_info_arg
.fsid
,
596 mnt
->mnt_dir
, label
, search
)) {
601 fd
= open(mnt
->mnt_dir
, O_RDONLY
);
602 if ((fd
!= -1) && !get_df(fd
, &space_info_arg
)) {
603 print_one_fs(&fs_info_arg
, dev_info_arg
,
604 space_info_arg
, label
, mnt
->mnt_dir
);
605 kfree(space_info_arg
);
606 memset(label
, 0, sizeof(label
));
619 static int dev_to_fsid(char *dev
, __u8
*fsid
)
621 struct btrfs_super_block
*disk_super
;
630 fd
= open(dev
, O_RDONLY
);
637 disk_super
= (struct btrfs_super_block
*)buf
;
638 ret
= btrfs_read_dev_super(fd
, disk_super
,
639 BTRFS_SUPER_INFO_OFFSET
, 0);
643 memcpy(fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
652 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
654 struct btrfs_fs_devices
*cur_seed
, *next_seed
;
655 struct btrfs_device
*device
;
657 while (!list_empty(&fs_devices
->devices
)) {
658 device
= list_entry(fs_devices
->devices
.next
,
659 struct btrfs_device
, dev_list
);
660 list_del(&device
->dev_list
);
667 /* free seed fs chain */
668 cur_seed
= fs_devices
->seed
;
669 fs_devices
->seed
= NULL
;
671 next_seed
= cur_seed
->seed
;
674 cur_seed
= next_seed
;
677 list_del(&fs_devices
->list
);
681 static int copy_device(struct btrfs_device
*dst
,
682 struct btrfs_device
*src
)
684 dst
->devid
= src
->devid
;
685 memcpy(dst
->uuid
, src
->uuid
, BTRFS_UUID_SIZE
);
686 if (src
->name
== NULL
)
689 dst
->name
= strdup(src
->name
);
693 if (src
->label
== NULL
)
696 dst
->label
= strdup(src
->label
);
702 dst
->total_devs
= src
->total_devs
;
703 dst
->super_bytes_used
= src
->super_bytes_used
;
704 dst
->total_bytes
= src
->total_bytes
;
705 dst
->bytes_used
= src
->bytes_used
;
706 dst
->generation
= src
->generation
;
711 static int copy_fs_devices(struct btrfs_fs_devices
*dst
,
712 struct btrfs_fs_devices
*src
)
714 struct btrfs_device
*cur_dev
, *dev_copy
;
717 memcpy(dst
->fsid
, src
->fsid
, BTRFS_FSID_SIZE
);
718 INIT_LIST_HEAD(&dst
->devices
);
721 list_for_each_entry(cur_dev
, &src
->devices
, dev_list
) {
722 dev_copy
= malloc(sizeof(*dev_copy
));
728 ret
= copy_device(dev_copy
, cur_dev
);
734 list_add(&dev_copy
->dev_list
, &dst
->devices
);
735 dev_copy
->fs_devices
= dst
;
741 static int find_and_copy_seed(struct btrfs_fs_devices
*seed
,
742 struct btrfs_fs_devices
*copy
,
743 struct list_head
*fs_uuids
) {
744 struct btrfs_fs_devices
*cur_fs
;
746 list_for_each_entry(cur_fs
, fs_uuids
, list
)
747 if (!memcmp(seed
->fsid
, cur_fs
->fsid
, BTRFS_FSID_SIZE
))
748 return copy_fs_devices(copy
, cur_fs
);
753 static int map_seed_devices(struct list_head
*all_uuids
,
754 char *search
, int *found
)
756 struct btrfs_fs_devices
*cur_fs
, *cur_seed
;
757 struct btrfs_fs_devices
*fs_copy
, *seed_copy
;
758 struct btrfs_fs_devices
*opened_fs
;
759 struct btrfs_device
*device
;
760 struct btrfs_fs_info
*fs_info
;
761 struct list_head
*fs_uuids
;
764 fs_uuids
= btrfs_scanned_uuids();
767 * The fs_uuids list is global, and open_ctree_* will
768 * modify it, make a private copy here
770 list_for_each_entry(cur_fs
, fs_uuids
, list
) {
771 /* don't bother handle all fs, if search target specified */
773 if (uuid_search(cur_fs
, search
) == 0)
778 /* skip all fs already shown as mounted fs */
779 if (is_seen_fsid(cur_fs
->fsid
))
782 fs_copy
= malloc(sizeof(*fs_copy
));
788 ret
= copy_fs_devices(fs_copy
, cur_fs
);
794 list_add(&fs_copy
->list
, all_uuids
);
797 list_for_each_entry(cur_fs
, all_uuids
, list
) {
798 device
= list_first_entry(&cur_fs
->devices
,
799 struct btrfs_device
, dev_list
);
803 * open_ctree_* detects seed/sprout mapping
805 fs_info
= open_ctree_fs_info(device
->name
, 0, 0,
811 * copy the seed chain under the opened fs
813 opened_fs
= fs_info
->fs_devices
;
815 while (opened_fs
->seed
) {
816 seed_copy
= malloc(sizeof(*seed_copy
));
821 ret
= find_and_copy_seed(opened_fs
->seed
, seed_copy
,
828 cur_seed
->seed
= seed_copy
;
830 opened_fs
= opened_fs
->seed
;
831 cur_seed
= cur_seed
->seed
;
834 close_ctree(fs_info
->chunk_root
);
840 close_ctree(fs_info
->chunk_root
);
844 static const char * const cmd_show_usage
[] = {
845 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
846 "Show the structure of a filesystem",
847 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
848 "-m|--mounted show only mounted btrfs",
849 "If no argument is given, structure of all present filesystems is shown.",
853 static int cmd_show(int argc
, char **argv
)
855 LIST_HEAD(all_uuids
);
856 struct btrfs_fs_devices
*fs_devices
;
859 /* default, search both kernel and udev */
862 char mp
[BTRFS_PATH_NAME_MAX
+ 1];
864 __u8 fsid
[BTRFS_FSID_SIZE
];
865 char uuid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
870 static struct option long_options
[] = {
871 { "all-devices", no_argument
, NULL
, 'd'},
872 { "mounted", no_argument
, NULL
, 'm'},
873 { NULL
, no_argument
, NULL
, 0 },
875 int c
= getopt_long(argc
, argv
, "dm", long_options
,
881 where
= BTRFS_SCAN_LBLKID
;
884 where
= BTRFS_SCAN_MOUNTED
;
887 usage(cmd_show_usage
);
891 if (check_argc_max(argc
, optind
+ 1))
892 usage(cmd_show_usage
);
895 search
= argv
[optind
];
896 if (strlen(search
) == 0)
897 usage(cmd_show_usage
);
898 type
= check_arg_type(search
);
900 * needs spl handling if input arg is block dev
901 * And if input arg is mount-point just print it
904 if (type
== BTRFS_ARG_BLKDEV
) {
905 if (where
== BTRFS_SCAN_LBLKID
) {
906 /* we need to do this because
907 * legacy BTRFS_SCAN_DEV
908 * provides /dev/dm-x paths
910 if (realpath(search
, path
))
913 ret
= get_btrfs_mount(search
,
916 /* given block dev is mounted*/
918 type
= BTRFS_ARG_MNTPOINT
;
920 ret
= dev_to_fsid(search
, fsid
);
923 "ERROR: No btrfs on %s\n",
927 uuid_unparse(fsid
, uuid_buf
);
929 type
= BTRFS_ARG_UUID
;
936 if (where
== BTRFS_SCAN_LBLKID
)
939 /* show mounted btrfs */
940 ret
= btrfs_scan_kernel(search
);
941 if (search
&& !ret
) {
942 /* since search is found we are done */
946 /* shows mounted only */
947 if (where
== BTRFS_SCAN_MOUNTED
)
951 ret
= btrfs_scan_lblkid();
954 fprintf(stderr
, "ERROR: %d while scanning\n", ret
);
959 * scan_for_btrfs() don't build seed/sprout mapping,
960 * do mapping build for each scanned fs here
962 ret
= map_seed_devices(&all_uuids
, search
, &found
);
965 "ERROR: %d while mapping seed devices\n", ret
);
969 list_for_each_entry(fs_devices
, &all_uuids
, list
)
970 print_one_uuid(fs_devices
);
972 if (search
&& !found
)
975 while (!list_empty(&all_uuids
)) {
976 fs_devices
= list_entry(all_uuids
.next
,
977 struct btrfs_fs_devices
, list
);
978 free_fs_devices(fs_devices
);
981 printf("%s\n", BTRFS_BUILD_VERSION
);
986 static const char * const cmd_sync_usage
[] = {
987 "btrfs filesystem sync <path>",
988 "Force a sync on a filesystem",
992 static int cmd_sync(int argc
, char **argv
)
996 DIR *dirstream
= NULL
;
998 if (check_argc_exact(argc
, 2))
999 usage(cmd_sync_usage
);
1003 fd
= open_file_or_dir(path
, &dirstream
);
1005 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
1009 printf("FSSync '%s'\n", path
);
1010 res
= ioctl(fd
, BTRFS_IOC_SYNC
);
1012 close_file_or_dir(fd
, dirstream
);
1014 fprintf(stderr
, "ERROR: unable to fs-syncing '%s' - %s\n",
1022 static int parse_compress_type(char *s
)
1024 if (strcmp(optarg
, "zlib") == 0)
1025 return BTRFS_COMPRESS_ZLIB
;
1026 else if (strcmp(optarg
, "lzo") == 0)
1027 return BTRFS_COMPRESS_LZO
;
1029 fprintf(stderr
, "Unknown compress type %s\n", s
);
1034 static const char * const cmd_defrag_usage
[] = {
1035 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
1036 "Defragment a file or a directory",
1039 "-r defragment files recursively",
1040 "-c[zlib,lzo] compress the file while defragmenting",
1041 "-f flush data to disk immediately after defragmenting",
1042 "-s start defragment only from byte onward",
1043 "-l len defragment only up to len bytes",
1044 "-t size minimal size of file to be considered for defragmenting",
1048 static int do_defrag(int fd
, int fancy_ioctl
,
1049 struct btrfs_ioctl_defrag_range_args
*range
)
1054 ret
= ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
);
1056 ret
= ioctl(fd
, BTRFS_IOC_DEFRAG_RANGE
, range
);
1061 static int defrag_global_fancy_ioctl
;
1062 static struct btrfs_ioctl_defrag_range_args defrag_global_range
;
1063 static int defrag_global_verbose
;
1064 static int defrag_global_errors
;
1065 static int defrag_callback(const char *fpath
, const struct stat
*sb
,
1066 int typeflag
, struct FTW
*ftwbuf
)
1072 if ((typeflag
== FTW_F
) && S_ISREG(sb
->st_mode
)) {
1073 if (defrag_global_verbose
)
1074 printf("%s\n", fpath
);
1075 fd
= open(fpath
, O_RDWR
);
1079 ret
= do_defrag(fd
, defrag_global_fancy_ioctl
, &defrag_global_range
);
1082 if (ret
&& e
== ENOTTY
&& defrag_global_fancy_ioctl
) {
1083 fprintf(stderr
, "ERROR: defrag range ioctl not "
1084 "supported in this kernel, please try "
1085 "without any options.\n");
1086 defrag_global_errors
++;
1095 fprintf(stderr
, "ERROR: defrag failed on %s - %s\n", fpath
, strerror(e
));
1096 defrag_global_errors
++;
1100 static int cmd_defrag(int argc
, char **argv
)
1110 struct btrfs_ioctl_defrag_range_args range
;
1112 int compress_type
= BTRFS_COMPRESS_NONE
;
1115 defrag_global_errors
= 0;
1116 defrag_global_verbose
= 0;
1117 defrag_global_errors
= 0;
1118 defrag_global_fancy_ioctl
= 0;
1121 int c
= getopt(argc
, argv
, "vrc::fs:l:t:");
1127 compress_type
= BTRFS_COMPRESS_ZLIB
;
1129 compress_type
= parse_compress_type(optarg
);
1130 defrag_global_fancy_ioctl
= 1;
1134 defrag_global_fancy_ioctl
= 1;
1137 defrag_global_verbose
= 1;
1140 start
= parse_size(optarg
);
1141 defrag_global_fancy_ioctl
= 1;
1144 len
= parse_size(optarg
);
1145 defrag_global_fancy_ioctl
= 1;
1148 thresh
= parse_size(optarg
);
1149 defrag_global_fancy_ioctl
= 1;
1155 usage(cmd_defrag_usage
);
1159 if (check_argc_min(argc
- optind
, 1))
1160 usage(cmd_defrag_usage
);
1162 memset(&defrag_global_range
, 0, sizeof(range
));
1163 defrag_global_range
.start
= start
;
1164 defrag_global_range
.len
= len
;
1165 defrag_global_range
.extent_thresh
= thresh
;
1166 if (compress_type
) {
1167 defrag_global_range
.flags
|= BTRFS_DEFRAG_RANGE_COMPRESS
;
1168 defrag_global_range
.compress_type
= compress_type
;
1171 defrag_global_range
.flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1173 for (i
= optind
; i
< argc
; i
++) {
1177 fd
= open_file_or_dir(argv
[i
], &dirstream
);
1179 fprintf(stderr
, "ERROR: failed to open %s - %s\n", argv
[i
],
1181 defrag_global_errors
++;
1182 close_file_or_dir(fd
, dirstream
);
1185 if (fstat(fd
, &st
)) {
1186 fprintf(stderr
, "ERROR: failed to stat %s - %s\n",
1187 argv
[i
], strerror(errno
));
1188 defrag_global_errors
++;
1189 close_file_or_dir(fd
, dirstream
);
1192 if (!(S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))) {
1194 "ERROR: %s is not a directory or a regular file\n",
1196 defrag_global_errors
++;
1197 close_file_or_dir(fd
, dirstream
);
1201 if (S_ISDIR(st
.st_mode
)) {
1202 ret
= nftw(argv
[i
], defrag_callback
, 10,
1203 FTW_MOUNT
| FTW_PHYS
);
1206 /* errors are handled in the callback */
1209 if (defrag_global_verbose
)
1210 printf("%s\n", argv
[i
]);
1211 ret
= do_defrag(fd
, defrag_global_fancy_ioctl
,
1212 &defrag_global_range
);
1216 if (defrag_global_verbose
)
1217 printf("%s\n", argv
[i
]);
1218 ret
= do_defrag(fd
, defrag_global_fancy_ioctl
,
1219 &defrag_global_range
);
1222 close_file_or_dir(fd
, dirstream
);
1223 if (ret
&& e
== ENOTTY
&& defrag_global_fancy_ioctl
) {
1224 fprintf(stderr
, "ERROR: defrag range ioctl not "
1225 "supported in this kernel, please try "
1226 "without any options.\n");
1227 defrag_global_errors
++;
1231 fprintf(stderr
, "ERROR: defrag failed on %s - %s\n",
1232 argv
[i
], strerror(e
));
1233 defrag_global_errors
++;
1236 if (defrag_global_verbose
)
1237 printf("%s\n", BTRFS_BUILD_VERSION
);
1238 if (defrag_global_errors
)
1239 fprintf(stderr
, "total %d failures\n", defrag_global_errors
);
1241 return !!defrag_global_errors
;
1244 static const char * const cmd_resize_usage
[] = {
1245 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1246 "Resize a filesystem",
1247 "If 'max' is passed, the filesystem will occupy all available space",
1248 "on the device 'devid'.",
1249 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1253 static int cmd_resize(int argc
, char **argv
)
1255 struct btrfs_ioctl_vol_args args
;
1256 int fd
, res
, len
, e
;
1257 char *amount
, *path
;
1258 DIR *dirstream
= NULL
;
1260 if (check_argc_exact(argc
, 3))
1261 usage(cmd_resize_usage
);
1266 len
= strlen(amount
);
1267 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
1268 fprintf(stderr
, "ERROR: size value too long ('%s)\n",
1273 fd
= open_file_or_dir(path
, &dirstream
);
1275 fprintf(stderr
, "ERROR: can't access '%s'\n", path
);
1279 printf("Resize '%s' of '%s'\n", path
, amount
);
1280 strncpy_null(args
.name
, amount
);
1281 res
= ioctl(fd
, BTRFS_IOC_RESIZE
, &args
);
1283 close_file_or_dir(fd
, dirstream
);
1285 fprintf(stderr
, "ERROR: unable to resize '%s' - %s\n",
1292 static const char * const cmd_label_usage
[] = {
1293 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1294 "Get or change the label of a filesystem",
1295 "With one argument, get the label of filesystem on <device>.",
1296 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1300 static int cmd_label(int argc
, char **argv
)
1302 if (check_argc_min(argc
, 2) || check_argc_max(argc
, 3))
1303 usage(cmd_label_usage
);
1306 return set_label(argv
[1], argv
[2]);
1308 char label
[BTRFS_LABEL_SIZE
];
1311 ret
= get_label(argv
[1], label
);
1313 fprintf(stdout
, "%s\n", label
);
1319 const struct cmd_group filesystem_cmd_group
= {
1320 filesystem_cmd_group_usage
, NULL
, {
1321 { "df", cmd_df
, cmd_df_usage
, NULL
, 0 },
1322 { "show", cmd_show
, cmd_show_usage
, NULL
, 0 },
1323 { "sync", cmd_sync
, cmd_sync_usage
, NULL
, 0 },
1324 { "defragment", cmd_defrag
, cmd_defrag_usage
, NULL
, 0 },
1325 { "balance", cmd_balance
, NULL
, &balance_cmd_group
, 1 },
1326 { "resize", cmd_resize
, cmd_resize_usage
, NULL
, 0 },
1327 { "label", cmd_label
, cmd_label_usage
, NULL
, 0 },
1332 int cmd_filesystem(int argc
, char **argv
)
1334 return handle_command_group(&filesystem_cmd_group
, argc
, argv
);