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 add_seen_fsid(u8 *fsid)
59 int slot = hash % SEEN_FSID_HASH_SIZE;
60 struct seen_fsid *seen = seen_fsid_hash[slot];
61 struct seen_fsid *alloc;
67 if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
78 alloc = malloc(sizeof(*alloc));
83 memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE);
88 seen_fsid_hash[slot] = alloc;
93 static void free_seen_fsid(void)
96 struct seen_fsid *seen;
97 struct seen_fsid *next;
99 for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) {
100 seen = seen_fsid_hash[slot];
106 seen_fsid_hash[slot] = NULL;
110 static const char * const filesystem_cmd_group_usage[] = {
111 "btrfs filesystem [<group>] <command> [<args>]",
115 static const char * const cmd_df_usage[] = {
116 "btrfs filesystem df <path>",
117 "Show space usage information for a mount point",
121 static char *group_type_str(u64 flag)
123 u64 mask = BTRFS_BLOCK_GROUP_TYPE_MASK |
124 BTRFS_SPACE_INFO_GLOBAL_RSV;
126 switch (flag & mask) {
127 case BTRFS_BLOCK_GROUP_DATA:
129 case BTRFS_BLOCK_GROUP_SYSTEM:
131 case BTRFS_BLOCK_GROUP_METADATA:
133 case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
134 return "Data+Metadata";
135 case BTRFS_SPACE_INFO_GLOBAL_RSV:
136 return "GlobalReserve";
142 static char *group_profile_str(u64 flag)
144 switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
147 case BTRFS_BLOCK_GROUP_RAID0:
149 case BTRFS_BLOCK_GROUP_RAID1:
151 case BTRFS_BLOCK_GROUP_RAID5:
153 case BTRFS_BLOCK_GROUP_RAID6:
155 case BTRFS_BLOCK_GROUP_DUP:
157 case BTRFS_BLOCK_GROUP_RAID10:
164 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
168 struct btrfs_ioctl_space_args *sargs;
170 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
174 sargs->space_slots = 0;
175 sargs->total_spaces = 0;
177 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
180 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
185 /* This really should never happen */
186 if (!sargs->total_spaces) {
190 count = sargs->total_spaces;
193 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
194 (count * sizeof(struct btrfs_ioctl_space_info)));
198 sargs->space_slots = count;
199 sargs->total_spaces = 0;
200 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
203 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
212 static void print_df(struct btrfs_ioctl_space_args *sargs)
215 struct btrfs_ioctl_space_info *sp = sargs->spaces;
217 for (i = 0; i < sargs->total_spaces; i++, sp++) {
218 printf("%s, %s: total=%s, used=%s\n",
219 group_type_str(sp->flags),
220 group_profile_str(sp->flags),
221 pretty_size(sp->total_bytes),
222 pretty_size(sp->used_bytes));
226 static int cmd_df(int argc, char **argv)
228 struct btrfs_ioctl_space_args *sargs = NULL;
232 DIR *dirstream = NULL;
234 if (check_argc_exact(argc, 2))
239 fd = open_file_or_dir(path, &dirstream);
241 fprintf(stderr, "ERROR: can't access '%s'\n", path);
244 ret = get_df(fd, &sargs);
250 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(-ret));
253 close_file_or_dir(fd, dirstream);
257 static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
260 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
261 int search_len = strlen(search);
263 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
264 uuid_unparse(fsid, uuidbuf);
265 if (!strncmp(uuidbuf, search, search_len))
268 if (strlen(label) && strcmp(label, search) == 0)
271 if (strcmp(mnt, search) == 0)
277 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
279 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
280 struct list_head *cur;
281 struct btrfs_device *device;
282 int search_len = strlen(search);
284 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
285 uuid_unparse(fs_devices->fsid, uuidbuf);
286 if (!strncmp(uuidbuf, search, search_len))
289 list_for_each(cur, &fs_devices->devices) {
290 device = list_entry(cur, struct btrfs_device, dev_list);
291 if ((device->label && strcmp(device->label, search) == 0) ||
292 strcmp(device->name, search) == 0)
299 * Sort devices by devid, ascending
301 static int cmp_device_id(void *priv, struct list_head *a,
304 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
306 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
309 return da->devid < db->devid ? -1 :
310 da->devid > db->devid ? 1 : 0;
313 static void splice_device_list(struct list_head *seed_devices,
314 struct list_head *all_devices)
316 struct btrfs_device *in_all, *next_all;
317 struct btrfs_device *in_seed, *next_seed;
319 list_for_each_entry_safe(in_all, next_all, all_devices, dev_list) {
320 list_for_each_entry_safe(in_seed, next_seed, seed_devices,
322 if (in_all->devid == in_seed->devid) {
324 * When do dev replace in a sprout fs
325 * to a dev in its seed fs, the replacing
326 * dev will reside in the sprout fs and
327 * the replaced dev will still exist
329 * So pick the latest one when showing
332 if (in_all->generation
333 < in_seed->generation) {
334 list_del(&in_all->dev_list);
336 } else if (in_all->generation
337 > in_seed->generation) {
338 list_del(&in_seed->dev_list);
346 list_splice(seed_devices, all_devices);
349 static void print_devices(struct btrfs_fs_devices *fs_devices,
352 struct btrfs_device *device;
353 struct btrfs_fs_devices *cur_fs;
354 struct list_head *all_devices;
356 all_devices = &fs_devices->devices;
357 cur_fs = fs_devices->seed;
358 /* add all devices of seed fs to the fs to be printed */
360 splice_device_list(&cur_fs->devices, all_devices);
361 cur_fs = cur_fs->seed;
364 list_sort(NULL, all_devices, cmp_device_id);
365 list_for_each_entry(device, all_devices, dev_list) {
366 printf("\tdevid %4llu size %s used %s path %s\n",
367 (unsigned long long)device->devid,
368 pretty_size(device->total_bytes),
369 pretty_size(device->bytes_used), device->name);
375 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
377 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
378 struct btrfs_device *device;
382 if (add_seen_fsid(fs_devices->fsid))
385 uuid_unparse(fs_devices->fsid, uuidbuf);
386 device = list_entry(fs_devices->devices.next, struct btrfs_device,
388 if (device->label && device->label[0])
389 printf("Label: '%s' ", device->label);
391 printf("Label: none ");
393 total = device->total_devs;
394 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
395 (unsigned long long)total,
396 pretty_size(device->super_bytes_used));
398 print_devices(fs_devices, &devs_found);
400 if (devs_found < total) {
401 printf("\t*** Some devices missing\n");
406 /* adds up all the used spaces as reported by the space info ioctl
408 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
412 for (i = 0; i < si->total_spaces; i++)
413 ret += si->spaces[i].used_bytes;
417 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
418 struct btrfs_ioctl_dev_info_args *dev_info,
419 struct btrfs_ioctl_space_args *space_info,
420 char *label, char *path)
425 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
426 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
429 ret = add_seen_fsid(fs_info->fsid);
435 uuid_unparse(fs_info->fsid, uuidbuf);
436 if (label && strlen(label))
437 printf("Label: '%s' ", label);
439 printf("Label: none ");
441 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
442 fs_info->num_devices,
443 pretty_size(calc_used_bytes(space_info)));
445 for (i = 0; i < fs_info->num_devices; i++) {
446 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
448 /* Add check for missing devices even mounted */
449 fd = open((char *)tmp_dev_info->path, O_RDONLY);
455 printf("\tdevid %4llu size %s used %s path %s\n",
457 pretty_size(tmp_dev_info->total_bytes),
458 pretty_size(tmp_dev_info->bytes_used),
463 printf("\t*** Some devices missing\n");
468 /* This function checks if the given input parameter is
470 * return -1: some error in the given input
471 * return 0: unknow input
472 * return 1: given input is uuid
473 * return 2: given input is path
475 static int check_arg_type(char *input)
483 if (realpath(input, path)) {
484 if (is_block_device(path) == 1)
485 return BTRFS_ARG_BLKDEV;
487 if (is_mount_point(path) == 1)
488 return BTRFS_ARG_MNTPOINT;
490 return BTRFS_ARG_UNKNOWN;
493 if (strlen(input) == (BTRFS_UUID_UNPARSED_SIZE - 1) &&
494 !uuid_parse(input, out))
495 return BTRFS_ARG_UUID;
497 return BTRFS_ARG_UNKNOWN;
500 static int btrfs_scan_kernel(void *search)
506 struct btrfs_ioctl_fs_info_args fs_info_arg;
507 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
508 struct btrfs_ioctl_space_args *space_info_arg = NULL;
509 char label[BTRFS_LABEL_SIZE];
511 f = setmntent("/proc/self/mounts", "r");
515 memset(label, 0, sizeof(label));
516 while ((mnt = getmntent(f)) != NULL) {
517 if (strcmp(mnt->mnt_type, "btrfs"))
519 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
524 if (get_label_mounted(mnt->mnt_dir, label)) {
528 if (search && !match_search_item_kernel(fs_info_arg.fsid,
529 mnt->mnt_dir, label, search)) {
534 fd = open(mnt->mnt_dir, O_RDONLY);
535 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
536 print_one_fs(&fs_info_arg, dev_info_arg,
537 space_info_arg, label, mnt->mnt_dir);
538 kfree(space_info_arg);
539 memset(label, 0, sizeof(label));
552 static int dev_to_fsid(char *dev, __u8 *fsid)
554 struct btrfs_super_block *disk_super;
563 fd = open(dev, O_RDONLY);
570 disk_super = (struct btrfs_super_block *)buf;
571 ret = btrfs_read_dev_super(fd, disk_super,
572 BTRFS_SUPER_INFO_OFFSET, 0);
576 memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
585 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
587 struct btrfs_fs_devices *cur_seed, *next_seed;
588 struct btrfs_device *device;
590 while (!list_empty(&fs_devices->devices)) {
591 device = list_entry(fs_devices->devices.next,
592 struct btrfs_device, dev_list);
593 list_del(&device->dev_list);
600 /* free seed fs chain */
601 cur_seed = fs_devices->seed;
602 fs_devices->seed = NULL;
604 next_seed = cur_seed->seed;
607 cur_seed = next_seed;
610 list_del(&fs_devices->list);
614 static int copy_device(struct btrfs_device *dst,
615 struct btrfs_device *src)
617 dst->devid = src->devid;
618 memcpy(dst->uuid, src->uuid, BTRFS_UUID_SIZE);
619 if (src->name == NULL)
622 dst->name = strdup(src->name);
626 if (src->label == NULL)
629 dst->label = strdup(src->label);
635 dst->total_devs = src->total_devs;
636 dst->super_bytes_used = src->super_bytes_used;
637 dst->total_bytes = src->total_bytes;
638 dst->bytes_used = src->bytes_used;
639 dst->generation = src->generation;
644 static int copy_fs_devices(struct btrfs_fs_devices *dst,
645 struct btrfs_fs_devices *src)
647 struct btrfs_device *cur_dev, *dev_copy;
650 memcpy(dst->fsid, src->fsid, BTRFS_FSID_SIZE);
651 INIT_LIST_HEAD(&dst->devices);
654 list_for_each_entry(cur_dev, &src->devices, dev_list) {
655 dev_copy = malloc(sizeof(*dev_copy));
661 ret = copy_device(dev_copy, cur_dev);
667 list_add(&dev_copy->dev_list, &dst->devices);
668 dev_copy->fs_devices = dst;
674 static int find_and_copy_seed(struct btrfs_fs_devices *seed,
675 struct btrfs_fs_devices *copy,
676 struct list_head *fs_uuids) {
677 struct btrfs_fs_devices *cur_fs;
679 list_for_each_entry(cur_fs, fs_uuids, list)
680 if (!memcmp(seed->fsid, cur_fs->fsid, BTRFS_FSID_SIZE))
681 return copy_fs_devices(copy, cur_fs);
686 static int map_seed_devices(struct list_head *all_uuids,
687 char *search, int *found)
689 struct btrfs_fs_devices *cur_fs, *cur_seed;
690 struct btrfs_fs_devices *fs_copy, *seed_copy;
691 struct btrfs_fs_devices *opened_fs;
692 struct btrfs_device *device;
693 struct btrfs_fs_info *fs_info;
694 struct list_head *fs_uuids;
697 fs_uuids = btrfs_scanned_uuids();
700 * The fs_uuids list is global, and open_ctree_* will
701 * modify it, make a private copy here
703 list_for_each_entry(cur_fs, fs_uuids, list) {
704 /* don't bother handle all fs, if search target specified */
706 if (uuid_search(cur_fs, search) == 0)
711 fs_copy = malloc(sizeof(*fs_copy));
717 ret = copy_fs_devices(fs_copy, cur_fs);
723 list_add(&fs_copy->list, all_uuids);
726 list_for_each_entry(cur_fs, all_uuids, list) {
727 device = list_first_entry(&cur_fs->devices,
728 struct btrfs_device, dev_list);
732 * open_ctree_* detects seed/sprout mapping
734 fs_info = open_ctree_fs_info(device->name, 0, 0,
740 * copy the seed chain under the opened fs
742 opened_fs = fs_info->fs_devices;
744 while (opened_fs->seed) {
745 seed_copy = malloc(sizeof(*seed_copy));
750 ret = find_and_copy_seed(opened_fs->seed, seed_copy,
757 cur_seed->seed = seed_copy;
759 opened_fs = opened_fs->seed;
760 cur_seed = cur_seed->seed;
763 close_ctree(fs_info->chunk_root);
769 close_ctree(fs_info->chunk_root);
773 static const char * const cmd_show_usage[] = {
774 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
775 "Show the structure of a filesystem",
776 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
777 "-m|--mounted show only mounted btrfs",
778 "If no argument is given, structure of all present filesystems is shown.",
782 static int cmd_show(int argc, char **argv)
784 LIST_HEAD(all_uuids);
785 struct btrfs_fs_devices *fs_devices;
788 /* default, search both kernel and udev */
791 char mp[BTRFS_PATH_NAME_MAX + 1];
793 __u8 fsid[BTRFS_FSID_SIZE];
794 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
799 static struct option long_options[] = {
800 { "all-devices", no_argument, NULL, 'd'},
801 { "mounted", no_argument, NULL, 'm'},
802 { NULL, no_argument, NULL, 0 },
804 int c = getopt_long(argc, argv, "dm", long_options,
810 where = BTRFS_SCAN_LBLKID;
813 where = BTRFS_SCAN_MOUNTED;
816 usage(cmd_show_usage);
820 if (check_argc_max(argc, optind + 1))
821 usage(cmd_show_usage);
824 search = argv[optind];
825 if (strlen(search) == 0)
826 usage(cmd_show_usage);
827 type = check_arg_type(search);
829 * needs spl handling if input arg is block dev
830 * And if input arg is mount-point just print it
833 if (type == BTRFS_ARG_BLKDEV) {
834 if (where == BTRFS_SCAN_LBLKID) {
835 /* we need to do this because
836 * legacy BTRFS_SCAN_DEV
837 * provides /dev/dm-x paths
839 if (realpath(search, path))
842 ret = get_btrfs_mount(search,
845 /* given block dev is mounted*/
847 type = BTRFS_ARG_MNTPOINT;
849 ret = dev_to_fsid(search, fsid);
852 "ERROR: No btrfs on %s\n",
856 uuid_unparse(fsid, uuid_buf);
858 type = BTRFS_ARG_UUID;
865 if (where == BTRFS_SCAN_LBLKID)
868 /* show mounted btrfs */
869 ret = btrfs_scan_kernel(search);
870 if (search && !ret) {
871 /* since search is found we are done */
875 /* shows mounted only */
876 if (where == BTRFS_SCAN_MOUNTED)
880 ret = btrfs_scan_lblkid(!BTRFS_UPDATE_KERNEL);
883 fprintf(stderr, "ERROR: %d while scanning\n", ret);
888 * scan_for_btrfs() don't build seed/sprout mapping,
889 * do mapping build for each scanned fs here
891 ret = map_seed_devices(&all_uuids, search, &found);
894 "ERROR: %d while mapping seed devices\n", ret);
898 list_for_each_entry(fs_devices, &all_uuids, list)
899 print_one_uuid(fs_devices);
901 if (search && !found)
904 while (!list_empty(&all_uuids)) {
905 fs_devices = list_entry(all_uuids.next,
906 struct btrfs_fs_devices, list);
907 free_fs_devices(fs_devices);
910 printf("%s\n", BTRFS_BUILD_VERSION);
915 static const char * const cmd_sync_usage[] = {
916 "btrfs filesystem sync <path>",
917 "Force a sync on a filesystem",
921 static int cmd_sync(int argc, char **argv)
925 DIR *dirstream = NULL;
927 if (check_argc_exact(argc, 2))
928 usage(cmd_sync_usage);
932 fd = open_file_or_dir(path, &dirstream);
934 fprintf(stderr, "ERROR: can't access '%s'\n", path);
938 printf("FSSync '%s'\n", path);
939 res = ioctl(fd, BTRFS_IOC_SYNC);
941 close_file_or_dir(fd, dirstream);
943 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
951 static int parse_compress_type(char *s)
953 if (strcmp(optarg, "zlib") == 0)
954 return BTRFS_COMPRESS_ZLIB;
955 else if (strcmp(optarg, "lzo") == 0)
956 return BTRFS_COMPRESS_LZO;
958 fprintf(stderr, "Unknown compress type %s\n", s);
963 static const char * const cmd_defrag_usage[] = {
964 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
965 "Defragment a file or a directory",
968 "-r defragment files recursively",
969 "-c[zlib,lzo] compress the file while defragmenting",
970 "-f flush data to disk immediately after defragmenting",
971 "-s start defragment only from byte onward",
972 "-l len defragment only up to len bytes",
973 "-t size minimal size of file to be considered for defragmenting",
977 static int do_defrag(int fd, int fancy_ioctl,
978 struct btrfs_ioctl_defrag_range_args *range)
983 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
985 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
990 static int defrag_global_fancy_ioctl;
991 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
992 static int defrag_global_verbose;
993 static int defrag_global_errors;
994 static int defrag_callback(const char *fpath, const struct stat *sb,
995 int typeflag, struct FTW *ftwbuf)
1001 if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
1002 if (defrag_global_verbose)
1003 printf("%s\n", fpath);
1004 fd = open(fpath, O_RDWR);
1008 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
1011 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1012 fprintf(stderr, "ERROR: defrag range ioctl not "
1013 "supported in this kernel, please try "
1014 "without any options.\n");
1015 defrag_global_errors++;
1024 fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
1025 defrag_global_errors++;
1029 static int cmd_defrag(int argc, char **argv)
1039 struct btrfs_ioctl_defrag_range_args range;
1041 int compress_type = BTRFS_COMPRESS_NONE;
1044 defrag_global_errors = 0;
1045 defrag_global_verbose = 0;
1046 defrag_global_errors = 0;
1047 defrag_global_fancy_ioctl = 0;
1050 int c = getopt(argc, argv, "vrc::fs:l:t:");
1056 compress_type = BTRFS_COMPRESS_ZLIB;
1058 compress_type = parse_compress_type(optarg);
1059 defrag_global_fancy_ioctl = 1;
1063 defrag_global_fancy_ioctl = 1;
1066 defrag_global_verbose = 1;
1069 start = parse_size(optarg);
1070 defrag_global_fancy_ioctl = 1;
1073 len = parse_size(optarg);
1074 defrag_global_fancy_ioctl = 1;
1077 thresh = parse_size(optarg);
1078 defrag_global_fancy_ioctl = 1;
1084 usage(cmd_defrag_usage);
1088 if (check_argc_min(argc - optind, 1))
1089 usage(cmd_defrag_usage);
1091 memset(&defrag_global_range, 0, sizeof(range));
1092 defrag_global_range.start = start;
1093 defrag_global_range.len = len;
1094 defrag_global_range.extent_thresh = thresh;
1095 if (compress_type) {
1096 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
1097 defrag_global_range.compress_type = compress_type;
1100 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
1102 for (i = optind; i < argc; i++) {
1106 fd = open_file_or_dir(argv[i], &dirstream);
1108 fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
1110 defrag_global_errors++;
1111 close_file_or_dir(fd, dirstream);
1114 if (fstat(fd, &st)) {
1115 fprintf(stderr, "ERROR: failed to stat %s - %s\n",
1116 argv[i], strerror(errno));
1117 defrag_global_errors++;
1118 close_file_or_dir(fd, dirstream);
1121 if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
1123 "ERROR: %s is not a directory or a regular file\n",
1125 defrag_global_errors++;
1126 close_file_or_dir(fd, dirstream);
1130 if (S_ISDIR(st.st_mode)) {
1131 ret = nftw(argv[i], defrag_callback, 10,
1132 FTW_MOUNT | FTW_PHYS);
1135 /* errors are handled in the callback */
1138 if (defrag_global_verbose)
1139 printf("%s\n", argv[i]);
1140 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1141 &defrag_global_range);
1145 if (defrag_global_verbose)
1146 printf("%s\n", argv[i]);
1147 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1148 &defrag_global_range);
1151 close_file_or_dir(fd, dirstream);
1152 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1153 fprintf(stderr, "ERROR: defrag range ioctl not "
1154 "supported in this kernel, please try "
1155 "without any options.\n");
1156 defrag_global_errors++;
1160 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
1161 argv[i], strerror(e));
1162 defrag_global_errors++;
1165 if (defrag_global_verbose)
1166 printf("%s\n", BTRFS_BUILD_VERSION);
1167 if (defrag_global_errors)
1168 fprintf(stderr, "total %d failures\n", defrag_global_errors);
1170 return !!defrag_global_errors;
1173 static const char * const cmd_resize_usage[] = {
1174 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1175 "Resize a filesystem",
1176 "If 'max' is passed, the filesystem will occupy all available space",
1177 "on the device 'devid'.",
1178 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1182 static int cmd_resize(int argc, char **argv)
1184 struct btrfs_ioctl_vol_args args;
1185 int fd, res, len, e;
1186 char *amount, *path;
1187 DIR *dirstream = NULL;
1189 if (check_argc_exact(argc, 3))
1190 usage(cmd_resize_usage);
1195 len = strlen(amount);
1196 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
1197 fprintf(stderr, "ERROR: size value too long ('%s)\n",
1202 fd = open_file_or_dir(path, &dirstream);
1204 fprintf(stderr, "ERROR: can't access '%s'\n", path);
1208 printf("Resize '%s' of '%s'\n", path, amount);
1209 strncpy_null(args.name, amount);
1210 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
1212 close_file_or_dir(fd, dirstream);
1214 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
1221 static const char * const cmd_label_usage[] = {
1222 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1223 "Get or change the label of a filesystem",
1224 "With one argument, get the label of filesystem on <device>.",
1225 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1229 static int cmd_label(int argc, char **argv)
1231 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
1232 usage(cmd_label_usage);
1235 return set_label(argv[1], argv[2]);
1237 char label[BTRFS_LABEL_SIZE];
1240 ret = get_label(argv[1], label);
1242 fprintf(stdout, "%s\n", label);
1248 const struct cmd_group filesystem_cmd_group = {
1249 filesystem_cmd_group_usage, NULL, {
1250 { "df", cmd_df, cmd_df_usage, NULL, 0 },
1251 { "show", cmd_show, cmd_show_usage, NULL, 0 },
1252 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
1253 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
1254 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
1255 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
1256 { "label", cmd_label, cmd_label_usage, NULL, 0 },
1261 int cmd_filesystem(int argc, char **argv)
1263 return handle_command_group(&filesystem_cmd_group, argc, argv);