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 "cmds-fi-disk_usage.h"
40 #include "list_sort.h"
45 * for btrfs fi show, we maintain a hash of fsids we've already printed.
46 * This way we don't print dups if a given FS is mounted more than once.
48 #define SEEN_FSID_HASH_SIZE 256
51 u8 fsid[BTRFS_FSID_SIZE];
52 struct seen_fsid *next;
55 static struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = {NULL,};
57 static int is_seen_fsid(u8 *fsid)
60 int slot = hash % SEEN_FSID_HASH_SIZE;
61 struct seen_fsid *seen = seen_fsid_hash[slot];
66 static int add_seen_fsid(u8 *fsid)
69 int slot = hash % SEEN_FSID_HASH_SIZE;
70 struct seen_fsid *seen = seen_fsid_hash[slot];
71 struct seen_fsid *alloc;
77 if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
88 alloc = malloc(sizeof(*alloc));
93 memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE);
98 seen_fsid_hash[slot] = alloc;
103 static void free_seen_fsid(void)
106 struct seen_fsid *seen;
107 struct seen_fsid *next;
109 for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) {
110 seen = seen_fsid_hash[slot];
116 seen_fsid_hash[slot] = NULL;
120 static const char * const filesystem_cmd_group_usage[] = {
121 "btrfs filesystem [<group>] <command> [<args>]",
126 static const char * const cmd_df_usage[] = {
127 "btrfs filesystem df [options] <path>",
128 "Show space usage information for a mount point",
129 "-b|--raw raw numbers in bytes",
130 "-h human friendly numbers, base 1024 (default)",
131 "-H human friendly numbers, base 1000",
132 "--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
133 "--si use 1000 as a base (kB, MB, GB, TB)",
134 "-k|--kbytes show sizes in KiB, or kB with --si",
135 "-m|--mbytes show sizes in MiB, or MB with --si",
136 "-g|--gbytes show sizes in GiB, or GB with --si",
137 "-t|--tbytes show sizes in TiB, or TB with --si",
142 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
146 struct btrfs_ioctl_space_args *sargs;
148 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
152 sargs->space_slots = 0;
153 sargs->total_spaces = 0;
155 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
158 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
163 /* This really should never happen */
164 if (!sargs->total_spaces) {
168 count = sargs->total_spaces;
171 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
172 (count * sizeof(struct btrfs_ioctl_space_info)));
176 sargs->space_slots = count;
177 sargs->total_spaces = 0;
178 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
181 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
191 static void print_df(struct btrfs_ioctl_space_args *sargs, unsigned unit_mode)
194 struct btrfs_ioctl_space_info *sp = sargs->spaces;
196 for (i = 0; i < sargs->total_spaces; i++, sp++) {
197 printf("%s, %s: total=%s, used=%s\n",
198 btrfs_group_type_str(sp->flags),
199 btrfs_group_profile_str(sp->flags),
200 pretty_size_mode(sp->total_bytes, unit_mode),
201 pretty_size_mode(sp->used_bytes, unit_mode));
205 static int cmd_df(int argc, char **argv)
207 struct btrfs_ioctl_space_args *sargs = NULL;
211 DIR *dirstream = NULL;
212 unsigned unit_mode = UNITS_DEFAULT;
216 static const struct option long_options[] = {
217 { "raw", no_argument, NULL, 'b'},
218 { "kbytes", no_argument, NULL, 'k'},
219 { "mbytes", no_argument, NULL, 'm'},
220 { "gbytes", no_argument, NULL, 'g'},
221 { "tbytes", no_argument, NULL, 't'},
222 { "si", no_argument, NULL, 256},
223 { "iec", no_argument, NULL, 257},
225 int c = getopt_long(argc, argv, "bhHkmgt", long_options,
231 unit_mode = UNITS_RAW;
234 units_set_base(&unit_mode, UNITS_KBYTES);
237 units_set_base(&unit_mode, UNITS_MBYTES);
240 units_set_base(&unit_mode, UNITS_GBYTES);
243 units_set_base(&unit_mode, UNITS_TBYTES);
246 unit_mode = UNITS_HUMAN_BINARY;
249 unit_mode = UNITS_HUMAN_DECIMAL;
252 units_set_mode(&unit_mode, UNITS_DECIMAL);
255 units_set_mode(&unit_mode, UNITS_BINARY);
262 if (check_argc_exact(argc, optind + 1))
267 fd = open_file_or_dir(path, &dirstream);
269 fprintf(stderr, "ERROR: can't access '%s'\n", path);
272 ret = get_df(fd, &sargs);
275 print_df(sargs, unit_mode);
278 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(-ret));
281 close_file_or_dir(fd, dirstream);
286 static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
289 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
290 int search_len = strlen(search);
292 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
293 uuid_unparse(fsid, uuidbuf);
294 if (!strncmp(uuidbuf, search, search_len))
297 if (strlen(label) && strcmp(label, search) == 0)
300 if (strcmp(mnt, search) == 0)
306 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
308 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
309 struct list_head *cur;
310 struct btrfs_device *device;
311 int search_len = strlen(search);
313 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
314 uuid_unparse(fs_devices->fsid, uuidbuf);
315 if (!strncmp(uuidbuf, search, search_len))
318 list_for_each(cur, &fs_devices->devices) {
319 device = list_entry(cur, struct btrfs_device, dev_list);
320 if ((device->label && strcmp(device->label, search) == 0) ||
321 strcmp(device->name, search) == 0)
328 * Sort devices by devid, ascending
330 static int cmp_device_id(void *priv, struct list_head *a,
333 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
335 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
338 return da->devid < db->devid ? -1 :
339 da->devid > db->devid ? 1 : 0;
342 static void splice_device_list(struct list_head *seed_devices,
343 struct list_head *all_devices)
345 struct btrfs_device *in_all, *next_all;
346 struct btrfs_device *in_seed, *next_seed;
348 list_for_each_entry_safe(in_all, next_all, all_devices, dev_list) {
349 list_for_each_entry_safe(in_seed, next_seed, seed_devices,
351 if (in_all->devid == in_seed->devid) {
353 * When do dev replace in a sprout fs
354 * to a dev in its seed fs, the replacing
355 * dev will reside in the sprout fs and
356 * the replaced dev will still exist
358 * So pick the latest one when showing
361 if (in_all->generation
362 < in_seed->generation) {
363 list_del(&in_all->dev_list);
365 } else if (in_all->generation
366 > in_seed->generation) {
367 list_del(&in_seed->dev_list);
375 list_splice(seed_devices, all_devices);
378 static void print_devices(struct btrfs_fs_devices *fs_devices,
381 struct btrfs_device *device;
382 struct btrfs_fs_devices *cur_fs;
383 struct list_head *all_devices;
385 all_devices = &fs_devices->devices;
386 cur_fs = fs_devices->seed;
387 /* add all devices of seed fs to the fs to be printed */
389 splice_device_list(&cur_fs->devices, all_devices);
390 cur_fs = cur_fs->seed;
393 list_sort(NULL, all_devices, cmp_device_id);
394 list_for_each_entry(device, all_devices, dev_list) {
395 printf("\tdevid %4llu size %s used %s path %s\n",
396 (unsigned long long)device->devid,
397 pretty_size(device->total_bytes),
398 pretty_size(device->bytes_used), device->name);
404 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
406 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
407 struct btrfs_device *device;
411 if (add_seen_fsid(fs_devices->fsid))
414 uuid_unparse(fs_devices->fsid, uuidbuf);
415 device = list_entry(fs_devices->devices.next, struct btrfs_device,
417 if (device->label && device->label[0])
418 printf("Label: '%s' ", device->label);
420 printf("Label: none ");
422 total = device->total_devs;
423 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
424 (unsigned long long)total,
425 pretty_size(device->super_bytes_used));
427 print_devices(fs_devices, &devs_found);
429 if (devs_found < total) {
430 printf("\t*** Some devices missing\n");
435 /* adds up all the used spaces as reported by the space info ioctl
437 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
441 for (i = 0; i < si->total_spaces; i++)
442 ret += si->spaces[i].used_bytes;
446 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
447 struct btrfs_ioctl_dev_info_args *dev_info,
448 struct btrfs_ioctl_space_args *space_info,
449 char *label, char *path)
454 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
455 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
458 ret = add_seen_fsid(fs_info->fsid);
464 uuid_unparse(fs_info->fsid, uuidbuf);
465 if (label && strlen(label))
466 printf("Label: '%s' ", label);
468 printf("Label: none ");
470 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
471 fs_info->num_devices,
472 pretty_size(calc_used_bytes(space_info)));
474 for (i = 0; i < fs_info->num_devices; i++) {
475 char *canonical_path;
477 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
478 canonical_path = canonicalize_path((char *)tmp_dev_info->path);
480 /* Add check for missing devices even mounted */
481 fd = open((char *)tmp_dev_info->path, O_RDONLY);
487 printf("\tdevid %4llu size %s used %s path %s\n",
489 pretty_size(tmp_dev_info->total_bytes),
490 pretty_size(tmp_dev_info->bytes_used),
493 free(canonical_path);
497 printf("\t*** Some devices missing\n");
502 /* This function checks if the given input parameter is
504 * return -1: some error in the given input
505 * return 0: unknow input
506 * return 1: given input is uuid
507 * return 2: given input is path
509 static int check_arg_type(char *input)
517 if (realpath(input, path)) {
518 if (is_block_device(path) == 1)
519 return BTRFS_ARG_BLKDEV;
521 if (is_mount_point(path) == 1)
522 return BTRFS_ARG_MNTPOINT;
524 return BTRFS_ARG_UNKNOWN;
527 if (strlen(input) == (BTRFS_UUID_UNPARSED_SIZE - 1) &&
528 !uuid_parse(input, out))
529 return BTRFS_ARG_UUID;
531 return BTRFS_ARG_UNKNOWN;
534 static int btrfs_scan_kernel(void *search)
540 struct btrfs_ioctl_fs_info_args fs_info_arg;
541 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
542 struct btrfs_ioctl_space_args *space_info_arg = NULL;
543 char label[BTRFS_LABEL_SIZE];
545 f = setmntent("/proc/self/mounts", "r");
549 memset(label, 0, sizeof(label));
550 while ((mnt = getmntent(f)) != NULL) {
551 if (strcmp(mnt->mnt_type, "btrfs"))
553 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
558 if (get_label_mounted(mnt->mnt_dir, label)) {
562 if (search && !match_search_item_kernel(fs_info_arg.fsid,
563 mnt->mnt_dir, label, search)) {
568 fd = open(mnt->mnt_dir, O_RDONLY);
569 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
570 print_one_fs(&fs_info_arg, dev_info_arg,
571 space_info_arg, label, mnt->mnt_dir);
572 kfree(space_info_arg);
573 memset(label, 0, sizeof(label));
586 static int dev_to_fsid(char *dev, __u8 *fsid)
588 struct btrfs_super_block *disk_super;
597 fd = open(dev, O_RDONLY);
604 disk_super = (struct btrfs_super_block *)buf;
605 ret = btrfs_read_dev_super(fd, disk_super,
606 BTRFS_SUPER_INFO_OFFSET, 0);
610 memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
619 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
621 struct btrfs_fs_devices *cur_seed, *next_seed;
622 struct btrfs_device *device;
624 while (!list_empty(&fs_devices->devices)) {
625 device = list_entry(fs_devices->devices.next,
626 struct btrfs_device, dev_list);
627 list_del(&device->dev_list);
634 /* free seed fs chain */
635 cur_seed = fs_devices->seed;
636 fs_devices->seed = NULL;
638 next_seed = cur_seed->seed;
641 cur_seed = next_seed;
644 list_del(&fs_devices->list);
648 static int copy_device(struct btrfs_device *dst,
649 struct btrfs_device *src)
651 dst->devid = src->devid;
652 memcpy(dst->uuid, src->uuid, BTRFS_UUID_SIZE);
653 if (src->name == NULL)
656 dst->name = strdup(src->name);
660 if (src->label == NULL)
663 dst->label = strdup(src->label);
669 dst->total_devs = src->total_devs;
670 dst->super_bytes_used = src->super_bytes_used;
671 dst->total_bytes = src->total_bytes;
672 dst->bytes_used = src->bytes_used;
673 dst->generation = src->generation;
678 static int copy_fs_devices(struct btrfs_fs_devices *dst,
679 struct btrfs_fs_devices *src)
681 struct btrfs_device *cur_dev, *dev_copy;
684 memcpy(dst->fsid, src->fsid, BTRFS_FSID_SIZE);
685 INIT_LIST_HEAD(&dst->devices);
688 list_for_each_entry(cur_dev, &src->devices, dev_list) {
689 dev_copy = malloc(sizeof(*dev_copy));
695 ret = copy_device(dev_copy, cur_dev);
701 list_add(&dev_copy->dev_list, &dst->devices);
702 dev_copy->fs_devices = dst;
708 static int find_and_copy_seed(struct btrfs_fs_devices *seed,
709 struct btrfs_fs_devices *copy,
710 struct list_head *fs_uuids) {
711 struct btrfs_fs_devices *cur_fs;
713 list_for_each_entry(cur_fs, fs_uuids, list)
714 if (!memcmp(seed->fsid, cur_fs->fsid, BTRFS_FSID_SIZE))
715 return copy_fs_devices(copy, cur_fs);
720 static int map_seed_devices(struct list_head *all_uuids,
721 char *search, int *found)
723 struct btrfs_fs_devices *cur_fs, *cur_seed;
724 struct btrfs_fs_devices *fs_copy, *seed_copy;
725 struct btrfs_fs_devices *opened_fs;
726 struct btrfs_device *device;
727 struct btrfs_fs_info *fs_info;
728 struct list_head *fs_uuids;
731 fs_uuids = btrfs_scanned_uuids();
734 * The fs_uuids list is global, and open_ctree_* will
735 * modify it, make a private copy here
737 list_for_each_entry(cur_fs, fs_uuids, list) {
738 /* don't bother handle all fs, if search target specified */
740 if (uuid_search(cur_fs, search) == 0)
745 /* skip all fs already shown as mounted fs */
746 if (is_seen_fsid(cur_fs->fsid))
749 fs_copy = malloc(sizeof(*fs_copy));
755 ret = copy_fs_devices(fs_copy, cur_fs);
761 list_add(&fs_copy->list, all_uuids);
764 list_for_each_entry(cur_fs, all_uuids, list) {
765 device = list_first_entry(&cur_fs->devices,
766 struct btrfs_device, dev_list);
770 * open_ctree_* detects seed/sprout mapping
772 fs_info = open_ctree_fs_info(device->name, 0, 0,
778 * copy the seed chain under the opened fs
780 opened_fs = fs_info->fs_devices;
782 while (opened_fs->seed) {
783 seed_copy = malloc(sizeof(*seed_copy));
788 ret = find_and_copy_seed(opened_fs->seed, seed_copy,
795 cur_seed->seed = seed_copy;
797 opened_fs = opened_fs->seed;
798 cur_seed = cur_seed->seed;
801 close_ctree(fs_info->chunk_root);
807 close_ctree(fs_info->chunk_root);
811 static const char * const cmd_show_usage[] = {
812 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
813 "Show the structure of a filesystem",
814 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
815 "-m|--mounted show only mounted btrfs",
816 "If no argument is given, structure of all present filesystems is shown.",
820 static int cmd_show(int argc, char **argv)
822 LIST_HEAD(all_uuids);
823 struct btrfs_fs_devices *fs_devices;
826 /* default, search both kernel and udev */
829 char mp[BTRFS_PATH_NAME_MAX + 1];
831 __u8 fsid[BTRFS_FSID_SIZE];
832 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
837 static struct option long_options[] = {
838 { "all-devices", no_argument, NULL, 'd'},
839 { "mounted", no_argument, NULL, 'm'},
840 { NULL, no_argument, NULL, 0 },
842 int c = getopt_long(argc, argv, "dm", long_options,
848 where = BTRFS_SCAN_LBLKID;
851 where = BTRFS_SCAN_MOUNTED;
854 usage(cmd_show_usage);
858 if (check_argc_max(argc, optind + 1))
859 usage(cmd_show_usage);
862 search = argv[optind];
863 if (strlen(search) == 0)
864 usage(cmd_show_usage);
865 type = check_arg_type(search);
868 * For search is a device:
869 * realpath do /dev/mapper/XX => /dev/dm-X
870 * which is required by BTRFS_SCAN_DEV
871 * For search is a mountpoint:
872 * realpath do /mnt/btrfs/ => /mnt/btrfs
873 * which shall be recognized by btrfs_scan_kernel()
875 if (!realpath(search, path)) {
876 fprintf(stderr, "ERROR: Could not show %s: %s\n",
877 search, strerror(errno));
884 * Needs special handling if input arg is block dev And if
885 * input arg is mount-point just print it right away
887 if (type == BTRFS_ARG_BLKDEV && where != BTRFS_SCAN_LBLKID) {
888 ret = get_btrfs_mount(search, mp, sizeof(mp));
890 /* given block dev is mounted */
892 type = BTRFS_ARG_MNTPOINT;
894 ret = dev_to_fsid(search, fsid);
897 "ERROR: No btrfs on %s\n",
901 uuid_unparse(fsid, uuid_buf);
903 type = BTRFS_ARG_UUID;
909 if (where == BTRFS_SCAN_LBLKID)
912 /* show mounted btrfs */
913 ret = btrfs_scan_kernel(search);
914 if (search && !ret) {
915 /* since search is found we are done */
919 /* shows mounted only */
920 if (where == BTRFS_SCAN_MOUNTED)
924 ret = btrfs_scan_lblkid();
927 fprintf(stderr, "ERROR: %d while scanning\n", ret);
932 * scan_for_btrfs() don't build seed/sprout mapping,
933 * do mapping build for each scanned fs here
935 ret = map_seed_devices(&all_uuids, search, &found);
938 "ERROR: %d while mapping seed devices\n", ret);
942 list_for_each_entry(fs_devices, &all_uuids, list)
943 print_one_uuid(fs_devices);
945 if (search && !found)
948 while (!list_empty(&all_uuids)) {
949 fs_devices = list_entry(all_uuids.next,
950 struct btrfs_fs_devices, list);
951 free_fs_devices(fs_devices);
954 printf("%s\n", BTRFS_BUILD_VERSION);
959 static const char * const cmd_sync_usage[] = {
960 "btrfs filesystem sync <path>",
961 "Force a sync on a filesystem",
965 static int cmd_sync(int argc, char **argv)
969 DIR *dirstream = NULL;
971 if (check_argc_exact(argc, 2))
972 usage(cmd_sync_usage);
976 fd = open_file_or_dir(path, &dirstream);
978 fprintf(stderr, "ERROR: can't access '%s'\n", path);
982 printf("FSSync '%s'\n", path);
983 res = ioctl(fd, BTRFS_IOC_SYNC);
985 close_file_or_dir(fd, dirstream);
987 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
995 static int parse_compress_type(char *s)
997 if (strcmp(optarg, "zlib") == 0)
998 return BTRFS_COMPRESS_ZLIB;
999 else if (strcmp(optarg, "lzo") == 0)
1000 return BTRFS_COMPRESS_LZO;
1002 fprintf(stderr, "Unknown compress type %s\n", s);
1007 static const char * const cmd_defrag_usage[] = {
1008 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
1009 "Defragment a file or a directory",
1012 "-r defragment files recursively",
1013 "-c[zlib,lzo] compress the file while defragmenting",
1014 "-f flush data to disk immediately after defragmenting",
1015 "-s start defragment only from byte onward",
1016 "-l len defragment only up to len bytes",
1017 "-t size minimal size of file to be considered for defragmenting",
1021 static int do_defrag(int fd, int fancy_ioctl,
1022 struct btrfs_ioctl_defrag_range_args *range)
1027 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
1029 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
1034 static int defrag_global_fancy_ioctl;
1035 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
1036 static int defrag_global_verbose;
1037 static int defrag_global_errors;
1038 static int defrag_callback(const char *fpath, const struct stat *sb,
1039 int typeflag, struct FTW *ftwbuf)
1045 if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
1046 if (defrag_global_verbose)
1047 printf("%s\n", fpath);
1048 fd = open(fpath, O_RDWR);
1052 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
1055 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1056 fprintf(stderr, "ERROR: defrag range ioctl not "
1057 "supported in this kernel, please try "
1058 "without any options.\n");
1059 defrag_global_errors++;
1068 fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
1069 defrag_global_errors++;
1073 static int cmd_defrag(int argc, char **argv)
1083 struct btrfs_ioctl_defrag_range_args range;
1085 int compress_type = BTRFS_COMPRESS_NONE;
1088 defrag_global_errors = 0;
1089 defrag_global_verbose = 0;
1090 defrag_global_errors = 0;
1091 defrag_global_fancy_ioctl = 0;
1094 int c = getopt(argc, argv, "vrc::fs:l:t:");
1100 compress_type = BTRFS_COMPRESS_ZLIB;
1102 compress_type = parse_compress_type(optarg);
1103 defrag_global_fancy_ioctl = 1;
1107 defrag_global_fancy_ioctl = 1;
1110 defrag_global_verbose = 1;
1113 start = parse_size(optarg);
1114 defrag_global_fancy_ioctl = 1;
1117 len = parse_size(optarg);
1118 defrag_global_fancy_ioctl = 1;
1121 thresh = parse_size(optarg);
1122 defrag_global_fancy_ioctl = 1;
1128 usage(cmd_defrag_usage);
1132 if (check_argc_min(argc - optind, 1))
1133 usage(cmd_defrag_usage);
1135 memset(&defrag_global_range, 0, sizeof(range));
1136 defrag_global_range.start = start;
1137 defrag_global_range.len = len;
1138 defrag_global_range.extent_thresh = thresh;
1139 if (compress_type) {
1140 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
1141 defrag_global_range.compress_type = compress_type;
1144 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
1146 for (i = optind; i < argc; i++) {
1150 fd = open_file_or_dir(argv[i], &dirstream);
1152 fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
1154 defrag_global_errors++;
1155 close_file_or_dir(fd, dirstream);
1158 if (fstat(fd, &st)) {
1159 fprintf(stderr, "ERROR: failed to stat %s - %s\n",
1160 argv[i], strerror(errno));
1161 defrag_global_errors++;
1162 close_file_or_dir(fd, dirstream);
1165 if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
1167 "ERROR: %s is not a directory or a regular file\n",
1169 defrag_global_errors++;
1170 close_file_or_dir(fd, dirstream);
1174 if (S_ISDIR(st.st_mode)) {
1175 ret = nftw(argv[i], defrag_callback, 10,
1176 FTW_MOUNT | FTW_PHYS);
1179 /* errors are handled in the callback */
1182 if (defrag_global_verbose)
1183 printf("%s\n", argv[i]);
1184 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1185 &defrag_global_range);
1189 if (defrag_global_verbose)
1190 printf("%s\n", argv[i]);
1191 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1192 &defrag_global_range);
1195 close_file_or_dir(fd, dirstream);
1196 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1197 fprintf(stderr, "ERROR: defrag range ioctl not "
1198 "supported in this kernel, please try "
1199 "without any options.\n");
1200 defrag_global_errors++;
1204 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
1205 argv[i], strerror(e));
1206 defrag_global_errors++;
1209 if (defrag_global_verbose)
1210 printf("%s\n", BTRFS_BUILD_VERSION);
1211 if (defrag_global_errors)
1212 fprintf(stderr, "total %d failures\n", defrag_global_errors);
1214 return !!defrag_global_errors;
1217 static const char * const cmd_resize_usage[] = {
1218 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1219 "Resize a filesystem",
1220 "If 'max' is passed, the filesystem will occupy all available space",
1221 "on the device 'devid'.",
1222 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1226 static int cmd_resize(int argc, char **argv)
1228 struct btrfs_ioctl_vol_args args;
1229 int fd, res, len, e;
1230 char *amount, *path;
1231 DIR *dirstream = NULL;
1233 if (check_argc_exact(argc, 3))
1234 usage(cmd_resize_usage);
1239 len = strlen(amount);
1240 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
1241 fprintf(stderr, "ERROR: size value too long ('%s)\n",
1246 fd = open_file_or_dir(path, &dirstream);
1248 fprintf(stderr, "ERROR: can't access '%s'\n", path);
1252 printf("Resize '%s' of '%s'\n", path, amount);
1253 strncpy_null(args.name, amount);
1254 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
1256 close_file_or_dir(fd, dirstream);
1258 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
1265 static const char * const cmd_label_usage[] = {
1266 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1267 "Get or change the label of a filesystem",
1268 "With one argument, get the label of filesystem on <device>.",
1269 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1273 static int cmd_label(int argc, char **argv)
1275 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
1276 usage(cmd_label_usage);
1279 return set_label(argv[1], argv[2]);
1281 char label[BTRFS_LABEL_SIZE];
1284 ret = get_label(argv[1], label);
1286 fprintf(stdout, "%s\n", label);
1292 const struct cmd_group filesystem_cmd_group = {
1293 filesystem_cmd_group_usage, NULL, {
1294 { "df", cmd_filesystem_df, cmd_filesystem_df_usage, NULL, 0 },
1295 { "show", cmd_show, cmd_show_usage, NULL, 0 },
1296 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
1297 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
1298 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
1299 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
1300 { "label", cmd_label, cmd_label_usage, NULL, 0 },
1301 { "disk-usage", cmd_filesystem_disk_usage,
1302 cmd_filesystem_disk_usage_usage, NULL, 0 },
1308 int cmd_filesystem(int argc, char **argv)
1310 return handle_command_group(&filesystem_cmd_group, argc, argv);