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>
23 #include <uuid/uuid.h>
28 #include <linux/limits.h>
31 #include <btrfsutil.h>
33 #include "kerncompat.h"
38 #include "cmds-fi-usage.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 static struct seen_fsid
*seen_fsid_hash
[SEEN_FSID_HASH_SIZE
] = {NULL
,};
49 static const char * const filesystem_cmd_group_usage
[] = {
50 "btrfs filesystem [<group>] <command> [<args>]",
54 static const char * const cmd_filesystem_df_usage
[] = {
55 "btrfs filesystem df [options] <path>",
56 "Show space usage information for a mount point",
57 HELPINFO_UNITS_SHORT_LONG
,
61 static int get_df(int fd
, struct btrfs_ioctl_space_args
**sargs_ret
)
65 struct btrfs_ioctl_space_args
*sargs
;
67 sargs
= malloc(sizeof(struct btrfs_ioctl_space_args
));
71 sargs
->space_slots
= 0;
72 sargs
->total_spaces
= 0;
74 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
76 error("cannot get space info: %m");
80 /* This really should never happen */
81 if (!sargs
->total_spaces
) {
85 count
= sargs
->total_spaces
;
88 sargs
= malloc(sizeof(struct btrfs_ioctl_space_args
) +
89 (count
* sizeof(struct btrfs_ioctl_space_info
)));
93 sargs
->space_slots
= count
;
94 sargs
->total_spaces
= 0;
95 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
97 error("cannot get space info with %llu slots: %m",
106 static void print_df(struct btrfs_ioctl_space_args
*sargs
, unsigned unit_mode
)
109 struct btrfs_ioctl_space_info
*sp
= sargs
->spaces
;
111 for (i
= 0; i
< sargs
->total_spaces
; i
++, sp
++) {
112 printf("%s, %s: total=%s, used=%s\n",
113 btrfs_group_type_str(sp
->flags
),
114 btrfs_group_profile_str(sp
->flags
),
115 pretty_size_mode(sp
->total_bytes
, unit_mode
),
116 pretty_size_mode(sp
->used_bytes
, unit_mode
));
120 static int cmd_filesystem_df(int argc
, char **argv
)
122 struct btrfs_ioctl_space_args
*sargs
= NULL
;
126 DIR *dirstream
= NULL
;
129 unit_mode
= get_unit_mode_from_arg(&argc
, argv
, 1);
131 clean_args_no_options(argc
, argv
, cmd_filesystem_df_usage
);
133 if (check_argc_exact(argc
- optind
, 1))
134 usage(cmd_filesystem_df_usage
);
138 fd
= btrfs_open_dir(path
, &dirstream
, 1);
142 ret
= get_df(fd
, &sargs
);
145 print_df(sargs
, unit_mode
);
148 error("get_df failed %s", strerror(-ret
));
151 close_file_or_dir(fd
, dirstream
);
155 static int match_search_item_kernel(u8
*fsid
, char *mnt
, char *label
,
158 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
159 int search_len
= strlen(search
);
161 search_len
= min(search_len
, BTRFS_UUID_UNPARSED_SIZE
);
162 uuid_unparse(fsid
, uuidbuf
);
163 if (!strncmp(uuidbuf
, search
, search_len
))
166 if (*label
&& strcmp(label
, search
) == 0)
169 if (strcmp(mnt
, search
) == 0)
175 static int uuid_search(struct btrfs_fs_devices
*fs_devices
, const char *search
)
177 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
178 struct btrfs_device
*device
;
179 int search_len
= strlen(search
);
181 search_len
= min(search_len
, BTRFS_UUID_UNPARSED_SIZE
);
182 uuid_unparse(fs_devices
->fsid
, uuidbuf
);
183 if (!strncmp(uuidbuf
, search
, search_len
))
186 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
187 if ((device
->label
&& strcmp(device
->label
, search
) == 0) ||
188 strcmp(device
->name
, search
) == 0)
195 * Sort devices by devid, ascending
197 static int cmp_device_id(void *priv
, struct list_head
*a
,
200 const struct btrfs_device
*da
= list_entry(a
, struct btrfs_device
,
202 const struct btrfs_device
*db
= list_entry(b
, struct btrfs_device
,
205 return da
->devid
< db
->devid
? -1 :
206 da
->devid
> db
->devid
? 1 : 0;
209 static void splice_device_list(struct list_head
*seed_devices
,
210 struct list_head
*all_devices
)
212 struct btrfs_device
*in_all
, *next_all
;
213 struct btrfs_device
*in_seed
, *next_seed
;
215 list_for_each_entry_safe(in_all
, next_all
, all_devices
, dev_list
) {
216 list_for_each_entry_safe(in_seed
, next_seed
, seed_devices
,
218 if (in_all
->devid
== in_seed
->devid
) {
220 * When do dev replace in a sprout fs
221 * to a dev in its seed fs, the replacing
222 * dev will reside in the sprout fs and
223 * the replaced dev will still exist
225 * So pick the latest one when showing
228 if (in_all
->generation
229 < in_seed
->generation
) {
230 list_del(&in_all
->dev_list
);
232 } else if (in_all
->generation
233 > in_seed
->generation
) {
234 list_del(&in_seed
->dev_list
);
242 list_splice(seed_devices
, all_devices
);
245 static void print_devices(struct btrfs_fs_devices
*fs_devices
,
246 u64
*devs_found
, unsigned unit_mode
)
248 struct btrfs_device
*device
;
249 struct btrfs_fs_devices
*cur_fs
;
250 struct list_head
*all_devices
;
252 all_devices
= &fs_devices
->devices
;
253 cur_fs
= fs_devices
->seed
;
254 /* add all devices of seed fs to the fs to be printed */
256 splice_device_list(&cur_fs
->devices
, all_devices
);
257 cur_fs
= cur_fs
->seed
;
260 list_sort(NULL
, all_devices
, cmp_device_id
);
261 list_for_each_entry(device
, all_devices
, dev_list
) {
262 printf("\tdevid %4llu size %s used %s path %s\n",
263 (unsigned long long)device
->devid
,
264 pretty_size_mode(device
->total_bytes
, unit_mode
),
265 pretty_size_mode(device
->bytes_used
, unit_mode
),
272 static void print_one_uuid(struct btrfs_fs_devices
*fs_devices
,
275 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
276 struct btrfs_device
*device
;
280 if (add_seen_fsid(fs_devices
->fsid
, seen_fsid_hash
, -1, NULL
))
283 uuid_unparse(fs_devices
->fsid
, uuidbuf
);
284 device
= list_entry(fs_devices
->devices
.next
, struct btrfs_device
,
286 if (device
->label
&& device
->label
[0])
287 printf("Label: '%s' ", device
->label
);
289 printf("Label: none ");
291 total
= device
->total_devs
;
292 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf
,
293 (unsigned long long)total
,
294 pretty_size_mode(device
->super_bytes_used
, unit_mode
));
296 print_devices(fs_devices
, &devs_found
, unit_mode
);
298 if (devs_found
< total
) {
299 printf("\t*** Some devices missing\n");
304 /* adds up all the used spaces as reported by the space info ioctl
306 static u64
calc_used_bytes(struct btrfs_ioctl_space_args
*si
)
310 for (i
= 0; i
< si
->total_spaces
; i
++)
311 ret
+= si
->spaces
[i
].used_bytes
;
315 static int print_one_fs(struct btrfs_ioctl_fs_info_args
*fs_info
,
316 struct btrfs_ioctl_dev_info_args
*dev_info
,
317 struct btrfs_ioctl_space_args
*space_info
,
318 char *label
, unsigned unit_mode
)
323 char uuidbuf
[BTRFS_UUID_UNPARSED_SIZE
];
324 struct btrfs_ioctl_dev_info_args
*tmp_dev_info
;
327 ret
= add_seen_fsid(fs_info
->fsid
, seen_fsid_hash
, -1, NULL
);
333 uuid_unparse(fs_info
->fsid
, uuidbuf
);
335 printf("Label: '%s' ", label
);
337 printf("Label: none ");
339 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf
,
340 fs_info
->num_devices
,
341 pretty_size_mode(calc_used_bytes(space_info
),
344 for (i
= 0; i
< fs_info
->num_devices
; i
++) {
345 char *canonical_path
;
347 tmp_dev_info
= (struct btrfs_ioctl_dev_info_args
*)&dev_info
[i
];
349 /* Add check for missing devices even mounted */
350 fd
= open((char *)tmp_dev_info
->path
, O_RDONLY
);
356 canonical_path
= canonicalize_path((char *)tmp_dev_info
->path
);
357 printf("\tdevid %4llu size %s used %s path %s\n",
359 pretty_size_mode(tmp_dev_info
->total_bytes
, unit_mode
),
360 pretty_size_mode(tmp_dev_info
->bytes_used
, unit_mode
),
363 free(canonical_path
);
367 printf("\t*** Some devices missing\n");
372 static int btrfs_scan_kernel(void *search
, unsigned unit_mode
)
378 struct btrfs_ioctl_fs_info_args fs_info_arg
;
379 struct btrfs_ioctl_dev_info_args
*dev_info_arg
= NULL
;
380 struct btrfs_ioctl_space_args
*space_info_arg
= NULL
;
381 char label
[BTRFS_LABEL_SIZE
];
383 f
= setmntent("/proc/self/mounts", "r");
387 memset(label
, 0, sizeof(label
));
388 while ((mnt
= getmntent(f
)) != NULL
) {
391 if (strcmp(mnt
->mnt_type
, "btrfs"))
393 ret
= get_fs_info(mnt
->mnt_dir
, &fs_info_arg
,
398 /* skip all fs already shown as mounted fs */
399 if (is_seen_fsid(fs_info_arg
.fsid
, seen_fsid_hash
))
402 ret
= get_label_mounted(mnt
->mnt_dir
, label
);
403 /* provide backward kernel compatibility */
405 ret
= get_label_unmounted(
406 (const char *)dev_info_arg
->path
, label
);
411 if (search
&& !match_search_item_kernel(fs_info_arg
.fsid
,
412 mnt
->mnt_dir
, label
, search
)) {
416 fd
= open(mnt
->mnt_dir
, O_RDONLY
);
417 if ((fd
!= -1) && !get_df(fd
, &space_info_arg
)) {
418 print_one_fs(&fs_info_arg
, dev_info_arg
,
419 space_info_arg
, label
, unit_mode
);
420 free(space_info_arg
);
421 memset(label
, 0, sizeof(label
));
434 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
436 struct btrfs_fs_devices
*cur_seed
, *next_seed
;
437 struct btrfs_device
*device
;
439 while (!list_empty(&fs_devices
->devices
)) {
440 device
= list_entry(fs_devices
->devices
.next
,
441 struct btrfs_device
, dev_list
);
442 list_del(&device
->dev_list
);
449 /* free seed fs chain */
450 cur_seed
= fs_devices
->seed
;
451 fs_devices
->seed
= NULL
;
453 next_seed
= cur_seed
->seed
;
456 cur_seed
= next_seed
;
459 list_del(&fs_devices
->list
);
463 static int copy_device(struct btrfs_device
*dst
,
464 struct btrfs_device
*src
)
466 dst
->devid
= src
->devid
;
467 memcpy(dst
->uuid
, src
->uuid
, BTRFS_UUID_SIZE
);
468 if (src
->name
== NULL
)
471 dst
->name
= strdup(src
->name
);
475 if (src
->label
== NULL
)
478 dst
->label
= strdup(src
->label
);
484 dst
->total_devs
= src
->total_devs
;
485 dst
->super_bytes_used
= src
->super_bytes_used
;
486 dst
->total_bytes
= src
->total_bytes
;
487 dst
->bytes_used
= src
->bytes_used
;
488 dst
->generation
= src
->generation
;
493 static int copy_fs_devices(struct btrfs_fs_devices
*dst
,
494 struct btrfs_fs_devices
*src
)
496 struct btrfs_device
*cur_dev
, *dev_copy
;
499 memcpy(dst
->fsid
, src
->fsid
, BTRFS_FSID_SIZE
);
500 INIT_LIST_HEAD(&dst
->devices
);
503 list_for_each_entry(cur_dev
, &src
->devices
, dev_list
) {
504 dev_copy
= malloc(sizeof(*dev_copy
));
510 ret
= copy_device(dev_copy
, cur_dev
);
516 list_add(&dev_copy
->dev_list
, &dst
->devices
);
517 dev_copy
->fs_devices
= dst
;
523 static int find_and_copy_seed(struct btrfs_fs_devices
*seed
,
524 struct btrfs_fs_devices
*copy
,
525 struct list_head
*fs_uuids
) {
526 struct btrfs_fs_devices
*cur_fs
;
528 list_for_each_entry(cur_fs
, fs_uuids
, list
)
529 if (!memcmp(seed
->fsid
, cur_fs
->fsid
, BTRFS_FSID_SIZE
))
530 return copy_fs_devices(copy
, cur_fs
);
535 static int has_seed_devices(struct btrfs_fs_devices
*fs_devices
)
537 struct btrfs_device
*device
;
538 int dev_cnt_total
, dev_cnt
= 0;
540 device
= list_first_entry(&fs_devices
->devices
, struct btrfs_device
,
543 dev_cnt_total
= device
->total_devs
;
545 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
548 return dev_cnt_total
!= dev_cnt
;
551 static int search_umounted_fs_uuids(struct list_head
*all_uuids
,
552 char *search
, int *found
)
554 struct btrfs_fs_devices
*cur_fs
, *fs_copy
;
555 struct list_head
*fs_uuids
;
558 fs_uuids
= btrfs_scanned_uuids();
561 * The fs_uuids list is global, and open_ctree_* will
562 * modify it, make a private copy here
564 list_for_each_entry(cur_fs
, fs_uuids
, list
) {
565 /* don't bother handle all fs, if search target specified */
567 if (uuid_search(cur_fs
, search
) == 0)
573 /* skip all fs already shown as mounted fs */
574 if (is_seen_fsid(cur_fs
->fsid
, seen_fsid_hash
))
577 fs_copy
= calloc(1, sizeof(*fs_copy
));
583 ret
= copy_fs_devices(fs_copy
, cur_fs
);
589 list_add(&fs_copy
->list
, all_uuids
);
596 static int map_seed_devices(struct list_head
*all_uuids
)
598 struct btrfs_fs_devices
*cur_fs
, *cur_seed
;
599 struct btrfs_fs_devices
*seed_copy
;
600 struct btrfs_fs_devices
*opened_fs
;
601 struct btrfs_device
*device
;
602 struct btrfs_fs_info
*fs_info
;
603 struct list_head
*fs_uuids
;
606 fs_uuids
= btrfs_scanned_uuids();
608 list_for_each_entry(cur_fs
, all_uuids
, list
) {
609 device
= list_first_entry(&cur_fs
->devices
,
610 struct btrfs_device
, dev_list
);
614 /* skip fs without seeds */
615 if (!has_seed_devices(cur_fs
))
619 * open_ctree_* detects seed/sprout mapping
621 fs_info
= open_ctree_fs_info(device
->name
, 0, 0, 0,
627 * copy the seed chain under the opened fs
629 opened_fs
= fs_info
->fs_devices
;
631 while (opened_fs
->seed
) {
632 seed_copy
= malloc(sizeof(*seed_copy
));
637 ret
= find_and_copy_seed(opened_fs
->seed
, seed_copy
,
644 cur_seed
->seed
= seed_copy
;
646 opened_fs
= opened_fs
->seed
;
647 cur_seed
= cur_seed
->seed
;
650 close_ctree(fs_info
->chunk_root
);
656 close_ctree(fs_info
->chunk_root
);
660 static const char * const cmd_filesystem_show_usage
[] = {
661 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
662 "Show the structure of a filesystem",
663 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
664 "-m|--mounted show only mounted btrfs",
666 "If no argument is given, structure of all present filesystems is shown.",
670 static int cmd_filesystem_show(int argc
, char **argv
)
672 LIST_HEAD(all_uuids
);
673 struct btrfs_fs_devices
*fs_devices
;
676 /* default, search both kernel and udev */
681 u8 fsid
[BTRFS_FSID_SIZE
];
682 char uuid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
686 unit_mode
= get_unit_mode_from_arg(&argc
, argv
, 0);
691 static const struct option long_options
[] = {
692 { "all-devices", no_argument
, NULL
, 'd'},
693 { "mounted", no_argument
, NULL
, 'm'},
697 c
= getopt_long(argc
, argv
, "dm", long_options
, NULL
);
702 where
= BTRFS_SCAN_LBLKID
;
705 where
= BTRFS_SCAN_MOUNTED
;
708 usage(cmd_filesystem_show_usage
);
712 if (check_argc_max(argc
, optind
+ 1))
713 usage(cmd_filesystem_show_usage
);
716 search
= argv
[optind
];
718 usage(cmd_filesystem_show_usage
);
719 type
= check_arg_type(search
);
722 * For search is a device:
723 * realpath do /dev/mapper/XX => /dev/dm-X
724 * which is required by BTRFS_SCAN_DEV
725 * For search is a mountpoint:
726 * realpath do /mnt/btrfs/ => /mnt/btrfs
727 * which shall be recognized by btrfs_scan_kernel()
729 if (realpath(search
, path
))
733 * Needs special handling if input arg is block dev And if
734 * input arg is mount-point just print it right away
736 if (type
== BTRFS_ARG_BLKDEV
&& where
!= BTRFS_SCAN_LBLKID
) {
737 ret
= get_btrfs_mount(search
, mp
, sizeof(mp
));
739 /* given block dev is mounted */
741 type
= BTRFS_ARG_MNTPOINT
;
743 ret
= dev_to_fsid(search
, fsid
);
745 error("no btrfs on %s", search
);
748 uuid_unparse(fsid
, uuid_buf
);
750 type
= BTRFS_ARG_UUID
;
756 if (where
== BTRFS_SCAN_LBLKID
)
759 /* show mounted btrfs */
760 ret
= btrfs_scan_kernel(search
, unit_mode
);
761 if (search
&& !ret
) {
762 /* since search is found we are done */
766 /* shows mounted only */
767 if (where
== BTRFS_SCAN_MOUNTED
)
771 ret
= btrfs_scan_devices();
774 error("blkid device scan returned %d", ret
);
778 ret
= search_umounted_fs_uuids(&all_uuids
, search
, &found
);
780 error("searching target device returned error %d", ret
);
785 * The seed/sprout mapping are not detected yet,
786 * do mapping build for all umounted fs
788 ret
= map_seed_devices(&all_uuids
);
790 error("mapping seed devices returned error %d", ret
);
794 list_for_each_entry(fs_devices
, &all_uuids
, list
)
795 print_one_uuid(fs_devices
, unit_mode
);
797 if (search
&& !found
) {
798 error("not a valid btrfs filesystem: %s", search
);
801 while (!list_empty(&all_uuids
)) {
802 fs_devices
= list_entry(all_uuids
.next
,
803 struct btrfs_fs_devices
, list
);
804 free_fs_devices(fs_devices
);
807 free_seen_fsid(seen_fsid_hash
);
811 static const char * const cmd_filesystem_sync_usage
[] = {
812 "btrfs filesystem sync <path>",
813 "Force a sync on a filesystem",
817 static int cmd_filesystem_sync(int argc
, char **argv
)
819 enum btrfs_util_error err
;
821 clean_args_no_options(argc
, argv
, cmd_filesystem_sync_usage
);
823 if (check_argc_exact(argc
- optind
, 1))
824 usage(cmd_filesystem_sync_usage
);
826 err
= btrfs_util_sync(argv
[optind
]);
828 error_btrfs_util(err
);
835 static int parse_compress_type(char *s
)
837 if (strcmp(optarg
, "zlib") == 0)
838 return BTRFS_COMPRESS_ZLIB
;
839 else if (strcmp(optarg
, "lzo") == 0)
840 return BTRFS_COMPRESS_LZO
;
841 else if (strcmp(optarg
, "zstd") == 0)
842 return BTRFS_COMPRESS_ZSTD
;
844 error("unknown compression type %s", s
);
849 static const char * const cmd_filesystem_defrag_usage
[] = {
850 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
851 "Defragment a file or a directory",
854 "-r defragment files recursively",
855 "-c[zlib,lzo,zstd] compress the file while defragmenting",
856 "-f flush data to disk immediately after defragmenting",
857 "-s start defragment only from byte onward",
858 "-l len defragment only up to len bytes",
859 "-t size target extent size hint (default: 32M)",
861 "Warning: most Linux kernels will break up the ref-links of COW data",
862 "(e.g., files copied with 'cp --reflink', snapshots) which may cause",
863 "considerable increase of space usage. See btrfs-filesystem(8) for",
868 static struct btrfs_ioctl_defrag_range_args defrag_global_range
;
869 static int defrag_global_verbose
;
870 static int defrag_global_errors
;
871 static int defrag_callback(const char *fpath
, const struct stat
*sb
,
872 int typeflag
, struct FTW
*ftwbuf
)
877 if ((typeflag
== FTW_F
) && S_ISREG(sb
->st_mode
)) {
878 if (defrag_global_verbose
)
879 printf("%s\n", fpath
);
880 fd
= open(fpath
, O_RDWR
);
884 ret
= ioctl(fd
, BTRFS_IOC_DEFRAG_RANGE
, &defrag_global_range
);
886 if (ret
&& errno
== ENOTTY
) {
888 "defrag range ioctl not supported in this kernel version, 2.6.33 and newer is required");
889 defrag_global_errors
++;
899 error("defrag failed on %s: %m", fpath
);
900 defrag_global_errors
++;
904 static int cmd_filesystem_defrag(int argc
, char **argv
)
914 int compress_type
= BTRFS_COMPRESS_NONE
;
918 * Kernel has a different default (256K) that is supposed to be safe,
919 * but it does not defragment very well. The 32M will likely lead to
920 * better results and is independent of the kernel default. We have to
921 * use the v2 defrag ioctl.
925 defrag_global_errors
= 0;
926 defrag_global_verbose
= 0;
927 defrag_global_errors
= 0;
930 int c
= getopt(argc
, argv
, "vrc::fs:l:t:");
936 compress_type
= BTRFS_COMPRESS_ZLIB
;
938 compress_type
= parse_compress_type(optarg
);
944 defrag_global_verbose
= 1;
947 start
= parse_size(optarg
);
950 len
= parse_size(optarg
);
953 thresh
= parse_size(optarg
);
954 if (thresh
> (u32
)-1) {
956 "target extent size %llu too big, trimmed to %u",
965 usage(cmd_filesystem_defrag_usage
);
969 if (check_argc_min(argc
- optind
, 1))
970 usage(cmd_filesystem_defrag_usage
);
972 memset(&defrag_global_range
, 0, sizeof(defrag_global_range
));
973 defrag_global_range
.start
= start
;
974 defrag_global_range
.len
= len
;
975 defrag_global_range
.extent_thresh
= (u32
)thresh
;
977 defrag_global_range
.flags
|= BTRFS_DEFRAG_RANGE_COMPRESS
;
978 defrag_global_range
.compress_type
= compress_type
;
981 defrag_global_range
.flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
984 * Look for directory arguments and warn if the recursive mode is not
985 * requested, as this is not implemented as recursive defragmentation
986 * in kernel. The stat errors are silent here as we check them below.
991 for (i
= optind
; i
< argc
; i
++) {
994 if (stat(argv
[i
], &st
))
997 if (S_ISDIR(st
.st_mode
)) {
999 "directory specified but recursive mode not requested: %s",
1006 "a directory passed to the defrag ioctl will not process the files\n"
1007 "recursively but will defragment the subvolume tree and the extent tree.\n"
1008 "If this is not intended, please use option -r .");
1012 for (i
= optind
; i
< argc
; i
++) {
1017 fd
= open_file_or_dir(argv
[i
], &dirstream
);
1019 error("cannot open %s: %m", argv
[i
]);
1024 ret
= fstat(fd
, &st
);
1026 error("failed to stat %s: %m", argv
[i
]);
1030 if (!(S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))) {
1031 error("%s is not a directory or a regular file",
1036 if (recursive
&& S_ISDIR(st
.st_mode
)) {
1037 ret
= nftw(argv
[i
], defrag_callback
, 10,
1038 FTW_MOUNT
| FTW_PHYS
);
1041 /* errors are handled in the callback */
1044 if (defrag_global_verbose
)
1045 printf("%s\n", argv
[i
]);
1046 ret
= ioctl(fd
, BTRFS_IOC_DEFRAG_RANGE
,
1047 &defrag_global_range
);
1049 if (ret
&& defrag_err
== ENOTTY
) {
1051 "defrag range ioctl not supported in this kernel version, 2.6.33 and newer is required");
1052 defrag_global_errors
++;
1053 close_file_or_dir(fd
, dirstream
);
1057 error("defrag failed on %s: %s", argv
[i
],
1058 strerror(defrag_err
));
1064 defrag_global_errors
++;
1065 close_file_or_dir(fd
, dirstream
);
1068 if (defrag_global_errors
)
1069 fprintf(stderr
, "total %d failures\n", defrag_global_errors
);
1071 return !!defrag_global_errors
;
1074 static const char * const cmd_filesystem_resize_usage
[] = {
1075 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1076 "Resize a filesystem",
1077 "If 'max' is passed, the filesystem will occupy all available space",
1078 "on the device 'devid'.",
1079 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1083 static int cmd_filesystem_resize(int argc
, char **argv
)
1085 struct btrfs_ioctl_vol_args args
;
1086 int fd
, res
, len
, e
;
1087 char *amount
, *path
;
1088 DIR *dirstream
= NULL
;
1091 clean_args_no_options_relaxed(argc
, argv
);
1093 if (check_argc_exact(argc
- optind
, 2))
1094 usage(cmd_filesystem_resize_usage
);
1096 amount
= argv
[optind
];
1097 path
= argv
[optind
+ 1];
1099 len
= strlen(amount
);
1100 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
1101 error("resize value too long (%s)", amount
);
1105 res
= stat(path
, &st
);
1107 error("resize: cannot stat %s: %m", path
);
1110 if (!S_ISDIR(st
.st_mode
)) {
1111 error("resize works on mounted filesystems and accepts only\n"
1112 "directories as argument. Passing file containing a btrfs image\n"
1113 "would resize the underlying filesystem instead of the image.\n");
1117 fd
= btrfs_open_dir(path
, &dirstream
, 1);
1121 printf("Resize '%s' of '%s'\n", path
, amount
);
1122 memset(&args
, 0, sizeof(args
));
1123 strncpy_null(args
.name
, amount
);
1124 res
= ioctl(fd
, BTRFS_IOC_RESIZE
, &args
);
1126 close_file_or_dir(fd
, dirstream
);
1130 error("unable to resize '%s': no enough free space",
1134 error("unable to resize '%s': %m", path
);
1138 } else if (res
> 0) {
1139 const char *err_str
= btrfs_err_str(res
);
1142 error("resizing of '%s' failed: %s", path
, err_str
);
1144 error("resizing of '%s' failed: unknown error %d",
1152 static const char * const cmd_filesystem_label_usage
[] = {
1153 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1154 "Get or change the label of a filesystem",
1155 "With one argument, get the label of filesystem on <device>.",
1156 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1160 static int cmd_filesystem_label(int argc
, char **argv
)
1162 clean_args_no_options(argc
, argv
, cmd_filesystem_label_usage
);
1164 if (check_argc_min(argc
- optind
, 1) ||
1165 check_argc_max(argc
- optind
, 2))
1166 usage(cmd_filesystem_label_usage
);
1168 if (argc
- optind
> 1) {
1169 return set_label(argv
[optind
], argv
[optind
+ 1]);
1171 char label
[BTRFS_LABEL_SIZE
];
1174 ret
= get_label(argv
[optind
], label
);
1176 fprintf(stdout
, "%s\n", label
);
1182 static const char filesystem_cmd_group_info
[] =
1183 "overall filesystem tasks and information";
1185 const struct cmd_group filesystem_cmd_group
= {
1186 filesystem_cmd_group_usage
, filesystem_cmd_group_info
, {
1187 { "df", cmd_filesystem_df
, cmd_filesystem_df_usage
, NULL
, 0 },
1188 { "du", cmd_filesystem_du
, cmd_filesystem_du_usage
, NULL
, 0 },
1189 { "show", cmd_filesystem_show
, cmd_filesystem_show_usage
, NULL
,
1191 { "sync", cmd_filesystem_sync
, cmd_filesystem_sync_usage
, NULL
,
1193 { "defragment", cmd_filesystem_defrag
,
1194 cmd_filesystem_defrag_usage
, NULL
, 0 },
1195 { "balance", cmd_balance
, NULL
, &balance_cmd_group
,
1197 { "resize", cmd_filesystem_resize
, cmd_filesystem_resize_usage
,
1199 { "label", cmd_filesystem_label
, cmd_filesystem_label_usage
,
1201 { "usage", cmd_filesystem_usage
,
1202 cmd_filesystem_usage_usage
, NULL
, 0 },
1208 int cmd_filesystem(int argc
, char **argv
)
1210 return handle_command_group(&filesystem_cmd_group
, argc
, argv
);