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 "kerncompat.h"
37 #include "cmds-fi-usage.h"
38 #include "list_sort.h"
40 #include "cmds-fi-du.h"
43 * for btrfs fi show, we maintain a hash of fsids we've already printed.
44 * This way we don't print dups if a given FS is mounted more than once.
46 #define SEEN_FSID_HASH_SIZE 256
49 u8 fsid[BTRFS_FSID_SIZE];
50 struct seen_fsid *next;
53 static struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = {NULL,};
55 static int is_seen_fsid(u8 *fsid)
58 int slot = hash % SEEN_FSID_HASH_SIZE;
59 struct seen_fsid *seen = seen_fsid_hash[slot];
64 static int add_seen_fsid(u8 *fsid)
67 int slot = hash % SEEN_FSID_HASH_SIZE;
68 struct seen_fsid *seen = seen_fsid_hash[slot];
69 struct seen_fsid *alloc;
75 if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
86 alloc = malloc(sizeof(*alloc));
91 memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE);
96 seen_fsid_hash[slot] = alloc;
101 static void free_seen_fsid(void)
104 struct seen_fsid *seen;
105 struct seen_fsid *next;
107 for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) {
108 seen = seen_fsid_hash[slot];
114 seen_fsid_hash[slot] = NULL;
118 static const char * const filesystem_cmd_group_usage[] = {
119 "btrfs filesystem [<group>] <command> [<args>]",
123 static const char * const cmd_filesystem_df_usage[] = {
124 "btrfs filesystem df [options] <path>",
125 "Show space usage information for a mount point",
126 HELPINFO_UNITS_SHORT_LONG,
130 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
134 struct btrfs_ioctl_space_args *sargs;
136 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
140 sargs->space_slots = 0;
141 sargs->total_spaces = 0;
143 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
145 error("cannot get space info: %s\n", strerror(errno));
149 /* This really should never happen */
150 if (!sargs->total_spaces) {
154 count = sargs->total_spaces;
157 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
158 (count * sizeof(struct btrfs_ioctl_space_info)));
162 sargs->space_slots = count;
163 sargs->total_spaces = 0;
164 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
166 error("cannot get space info with %llu slots: %s",
167 count, strerror(errno));
175 static void print_df(struct btrfs_ioctl_space_args *sargs, unsigned unit_mode)
178 struct btrfs_ioctl_space_info *sp = sargs->spaces;
180 for (i = 0; i < sargs->total_spaces; i++, sp++) {
181 printf("%s, %s: total=%s, used=%s\n",
182 btrfs_group_type_str(sp->flags),
183 btrfs_group_profile_str(sp->flags),
184 pretty_size_mode(sp->total_bytes, unit_mode),
185 pretty_size_mode(sp->used_bytes, unit_mode));
189 static int cmd_filesystem_df(int argc, char **argv)
191 struct btrfs_ioctl_space_args *sargs = NULL;
195 DIR *dirstream = NULL;
198 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
200 clean_args_no_options(argc, argv, cmd_filesystem_df_usage);
202 if (check_argc_exact(argc - optind, 1))
203 usage(cmd_filesystem_df_usage);
207 fd = btrfs_open_dir(path, &dirstream, 1);
211 ret = get_df(fd, &sargs);
214 print_df(sargs, unit_mode);
217 error("get_df failed %s", strerror(-ret));
220 close_file_or_dir(fd, dirstream);
224 static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
227 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
228 int search_len = strlen(search);
230 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
231 uuid_unparse(fsid, uuidbuf);
232 if (!strncmp(uuidbuf, search, search_len))
235 if (*label && strcmp(label, search) == 0)
238 if (strcmp(mnt, search) == 0)
244 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
246 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
247 struct list_head *cur;
248 struct btrfs_device *device;
249 int search_len = strlen(search);
251 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
252 uuid_unparse(fs_devices->fsid, uuidbuf);
253 if (!strncmp(uuidbuf, search, search_len))
256 list_for_each(cur, &fs_devices->devices) {
257 device = list_entry(cur, struct btrfs_device, dev_list);
258 if ((device->label && strcmp(device->label, search) == 0) ||
259 strcmp(device->name, search) == 0)
266 * Sort devices by devid, ascending
268 static int cmp_device_id(void *priv, struct list_head *a,
271 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
273 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
276 return da->devid < db->devid ? -1 :
277 da->devid > db->devid ? 1 : 0;
280 static void splice_device_list(struct list_head *seed_devices,
281 struct list_head *all_devices)
283 struct btrfs_device *in_all, *next_all;
284 struct btrfs_device *in_seed, *next_seed;
286 list_for_each_entry_safe(in_all, next_all, all_devices, dev_list) {
287 list_for_each_entry_safe(in_seed, next_seed, seed_devices,
289 if (in_all->devid == in_seed->devid) {
291 * When do dev replace in a sprout fs
292 * to a dev in its seed fs, the replacing
293 * dev will reside in the sprout fs and
294 * the replaced dev will still exist
296 * So pick the latest one when showing
299 if (in_all->generation
300 < in_seed->generation) {
301 list_del(&in_all->dev_list);
303 } else if (in_all->generation
304 > in_seed->generation) {
305 list_del(&in_seed->dev_list);
313 list_splice(seed_devices, all_devices);
316 static void print_devices(struct btrfs_fs_devices *fs_devices,
317 u64 *devs_found, unsigned unit_mode)
319 struct btrfs_device *device;
320 struct btrfs_fs_devices *cur_fs;
321 struct list_head *all_devices;
323 all_devices = &fs_devices->devices;
324 cur_fs = fs_devices->seed;
325 /* add all devices of seed fs to the fs to be printed */
327 splice_device_list(&cur_fs->devices, all_devices);
328 cur_fs = cur_fs->seed;
331 list_sort(NULL, all_devices, cmp_device_id);
332 list_for_each_entry(device, all_devices, dev_list) {
333 printf("\tdevid %4llu size %s used %s path %s\n",
334 (unsigned long long)device->devid,
335 pretty_size_mode(device->total_bytes, unit_mode),
336 pretty_size_mode(device->bytes_used, unit_mode),
343 static void print_one_uuid(struct btrfs_fs_devices *fs_devices,
346 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
347 struct btrfs_device *device;
351 if (add_seen_fsid(fs_devices->fsid))
354 uuid_unparse(fs_devices->fsid, uuidbuf);
355 device = list_entry(fs_devices->devices.next, struct btrfs_device,
357 if (device->label && device->label[0])
358 printf("Label: '%s' ", device->label);
360 printf("Label: none ");
362 total = device->total_devs;
363 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
364 (unsigned long long)total,
365 pretty_size_mode(device->super_bytes_used, unit_mode));
367 print_devices(fs_devices, &devs_found, unit_mode);
369 if (devs_found < total) {
370 printf("\t*** Some devices missing\n");
375 /* adds up all the used spaces as reported by the space info ioctl
377 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
381 for (i = 0; i < si->total_spaces; i++)
382 ret += si->spaces[i].used_bytes;
386 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
387 struct btrfs_ioctl_dev_info_args *dev_info,
388 struct btrfs_ioctl_space_args *space_info,
389 char *label, unsigned unit_mode)
394 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
395 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
398 ret = add_seen_fsid(fs_info->fsid);
404 uuid_unparse(fs_info->fsid, uuidbuf);
406 printf("Label: '%s' ", label);
408 printf("Label: none ");
410 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
411 fs_info->num_devices,
412 pretty_size_mode(calc_used_bytes(space_info),
415 for (i = 0; i < fs_info->num_devices; i++) {
416 char *canonical_path;
418 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
420 /* Add check for missing devices even mounted */
421 fd = open((char *)tmp_dev_info->path, O_RDONLY);
427 canonical_path = canonicalize_path((char *)tmp_dev_info->path);
428 printf("\tdevid %4llu size %s used %s path %s\n",
430 pretty_size_mode(tmp_dev_info->total_bytes, unit_mode),
431 pretty_size_mode(tmp_dev_info->bytes_used, unit_mode),
434 free(canonical_path);
438 printf("\t*** Some devices missing\n");
443 static int btrfs_scan_kernel(void *search, unsigned unit_mode)
449 struct btrfs_ioctl_fs_info_args fs_info_arg;
450 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
451 struct btrfs_ioctl_space_args *space_info_arg = NULL;
452 char label[BTRFS_LABEL_SIZE];
454 f = setmntent("/proc/self/mounts", "r");
458 memset(label, 0, sizeof(label));
459 while ((mnt = getmntent(f)) != NULL) {
462 if (strcmp(mnt->mnt_type, "btrfs"))
464 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
469 /* skip all fs already shown as mounted fs */
470 if (is_seen_fsid(fs_info_arg.fsid))
473 ret = get_label_mounted(mnt->mnt_dir, label);
474 /* provide backward kernel compatibility */
476 ret = get_label_unmounted(
477 (const char *)dev_info_arg->path, label);
482 if (search && !match_search_item_kernel(fs_info_arg.fsid,
483 mnt->mnt_dir, label, search)) {
487 fd = open(mnt->mnt_dir, O_RDONLY);
488 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
489 print_one_fs(&fs_info_arg, dev_info_arg,
490 space_info_arg, label, unit_mode);
491 kfree(space_info_arg);
492 memset(label, 0, sizeof(label));
505 static int dev_to_fsid(char *dev, __u8 *fsid)
507 struct btrfs_super_block *disk_super;
508 char buf[BTRFS_SUPER_INFO_SIZE];
512 fd = open(dev, O_RDONLY);
518 disk_super = (struct btrfs_super_block *)buf;
519 ret = btrfs_read_dev_super(fd, disk_super,
520 BTRFS_SUPER_INFO_OFFSET, 0);
524 memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
532 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
534 struct btrfs_fs_devices *cur_seed, *next_seed;
535 struct btrfs_device *device;
537 while (!list_empty(&fs_devices->devices)) {
538 device = list_entry(fs_devices->devices.next,
539 struct btrfs_device, dev_list);
540 list_del(&device->dev_list);
547 /* free seed fs chain */
548 cur_seed = fs_devices->seed;
549 fs_devices->seed = NULL;
551 next_seed = cur_seed->seed;
554 cur_seed = next_seed;
557 list_del(&fs_devices->list);
561 static int copy_device(struct btrfs_device *dst,
562 struct btrfs_device *src)
564 dst->devid = src->devid;
565 memcpy(dst->uuid, src->uuid, BTRFS_UUID_SIZE);
566 if (src->name == NULL)
569 dst->name = strdup(src->name);
573 if (src->label == NULL)
576 dst->label = strdup(src->label);
582 dst->total_devs = src->total_devs;
583 dst->super_bytes_used = src->super_bytes_used;
584 dst->total_bytes = src->total_bytes;
585 dst->bytes_used = src->bytes_used;
586 dst->generation = src->generation;
591 static int copy_fs_devices(struct btrfs_fs_devices *dst,
592 struct btrfs_fs_devices *src)
594 struct btrfs_device *cur_dev, *dev_copy;
597 memcpy(dst->fsid, src->fsid, BTRFS_FSID_SIZE);
598 INIT_LIST_HEAD(&dst->devices);
601 list_for_each_entry(cur_dev, &src->devices, dev_list) {
602 dev_copy = malloc(sizeof(*dev_copy));
608 ret = copy_device(dev_copy, cur_dev);
614 list_add(&dev_copy->dev_list, &dst->devices);
615 dev_copy->fs_devices = dst;
621 static int find_and_copy_seed(struct btrfs_fs_devices *seed,
622 struct btrfs_fs_devices *copy,
623 struct list_head *fs_uuids) {
624 struct btrfs_fs_devices *cur_fs;
626 list_for_each_entry(cur_fs, fs_uuids, list)
627 if (!memcmp(seed->fsid, cur_fs->fsid, BTRFS_FSID_SIZE))
628 return copy_fs_devices(copy, cur_fs);
633 static int has_seed_devices(struct btrfs_fs_devices *fs_devices)
635 struct btrfs_device *device;
636 int dev_cnt_total, dev_cnt = 0;
638 device = list_first_entry(&fs_devices->devices, struct btrfs_device,
641 dev_cnt_total = device->total_devs;
643 list_for_each_entry(device, &fs_devices->devices, dev_list)
646 return dev_cnt_total != dev_cnt;
649 static int search_umounted_fs_uuids(struct list_head *all_uuids,
650 char *search, int *found)
652 struct btrfs_fs_devices *cur_fs, *fs_copy;
653 struct list_head *fs_uuids;
656 fs_uuids = btrfs_scanned_uuids();
659 * The fs_uuids list is global, and open_ctree_* will
660 * modify it, make a private copy here
662 list_for_each_entry(cur_fs, fs_uuids, list) {
663 /* don't bother handle all fs, if search target specified */
665 if (uuid_search(cur_fs, search) == 0)
671 /* skip all fs already shown as mounted fs */
672 if (is_seen_fsid(cur_fs->fsid))
675 fs_copy = calloc(1, sizeof(*fs_copy));
681 ret = copy_fs_devices(fs_copy, cur_fs);
687 list_add(&fs_copy->list, all_uuids);
694 static int map_seed_devices(struct list_head *all_uuids)
696 struct btrfs_fs_devices *cur_fs, *cur_seed;
697 struct btrfs_fs_devices *seed_copy;
698 struct btrfs_fs_devices *opened_fs;
699 struct btrfs_device *device;
700 struct btrfs_fs_info *fs_info;
701 struct list_head *fs_uuids;
704 fs_uuids = btrfs_scanned_uuids();
706 list_for_each_entry(cur_fs, all_uuids, list) {
707 device = list_first_entry(&cur_fs->devices,
708 struct btrfs_device, dev_list);
712 /* skip fs without seeds */
713 if (!has_seed_devices(cur_fs))
717 * open_ctree_* detects seed/sprout mapping
719 fs_info = open_ctree_fs_info(device->name, 0, 0, 0,
725 * copy the seed chain under the opened fs
727 opened_fs = fs_info->fs_devices;
729 while (opened_fs->seed) {
730 seed_copy = malloc(sizeof(*seed_copy));
735 ret = find_and_copy_seed(opened_fs->seed, seed_copy,
742 cur_seed->seed = seed_copy;
744 opened_fs = opened_fs->seed;
745 cur_seed = cur_seed->seed;
748 close_ctree(fs_info->chunk_root);
754 close_ctree(fs_info->chunk_root);
758 static const char * const cmd_filesystem_show_usage[] = {
759 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
760 "Show the structure of a filesystem",
761 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
762 "-m|--mounted show only mounted btrfs",
764 "If no argument is given, structure of all present filesystems is shown.",
768 static int cmd_filesystem_show(int argc, char **argv)
770 LIST_HEAD(all_uuids);
771 struct btrfs_fs_devices *fs_devices;
774 /* default, search both kernel and udev */
779 __u8 fsid[BTRFS_FSID_SIZE];
780 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
784 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
788 static const struct option long_options[] = {
789 { "all-devices", no_argument, NULL, 'd'},
790 { "mounted", no_argument, NULL, 'm'},
794 c = getopt_long(argc, argv, "dm", long_options, NULL);
799 where = BTRFS_SCAN_LBLKID;
802 where = BTRFS_SCAN_MOUNTED;
805 usage(cmd_filesystem_show_usage);
809 if (check_argc_max(argc, optind + 1))
810 usage(cmd_filesystem_show_usage);
813 search = argv[optind];
815 usage(cmd_filesystem_show_usage);
816 type = check_arg_type(search);
819 * For search is a device:
820 * realpath do /dev/mapper/XX => /dev/dm-X
821 * which is required by BTRFS_SCAN_DEV
822 * For search is a mountpoint:
823 * realpath do /mnt/btrfs/ => /mnt/btrfs
824 * which shall be recognized by btrfs_scan_kernel()
826 if (realpath(search, path))
830 * Needs special handling if input arg is block dev And if
831 * input arg is mount-point just print it right away
833 if (type == BTRFS_ARG_BLKDEV && where != BTRFS_SCAN_LBLKID) {
834 ret = get_btrfs_mount(search, mp, sizeof(mp));
836 /* given block dev is mounted */
838 type = BTRFS_ARG_MNTPOINT;
840 ret = dev_to_fsid(search, fsid);
842 error("no btrfs on %s", search);
845 uuid_unparse(fsid, uuid_buf);
847 type = BTRFS_ARG_UUID;
853 if (where == BTRFS_SCAN_LBLKID)
856 /* show mounted btrfs */
857 ret = btrfs_scan_kernel(search, unit_mode);
858 if (search && !ret) {
859 /* since search is found we are done */
863 /* shows mounted only */
864 if (where == BTRFS_SCAN_MOUNTED)
868 ret = btrfs_scan_lblkid();
871 error("blkid device scan returned %d\n", ret);
875 ret = search_umounted_fs_uuids(&all_uuids, search, &found);
877 error("searching target device returned error %d", ret);
882 * The seed/sprout mapping are not detected yet,
883 * do mapping build for all umounted fs
885 ret = map_seed_devices(&all_uuids);
887 error("mapping seed devices returned error %d", ret);
891 list_for_each_entry(fs_devices, &all_uuids, list)
892 print_one_uuid(fs_devices, unit_mode);
894 if (search && !found)
897 while (!list_empty(&all_uuids)) {
898 fs_devices = list_entry(all_uuids.next,
899 struct btrfs_fs_devices, list);
900 free_fs_devices(fs_devices);
907 static const char * const cmd_filesystem_sync_usage[] = {
908 "btrfs filesystem sync <path>",
909 "Force a sync on a filesystem",
913 static int cmd_filesystem_sync(int argc, char **argv)
917 DIR *dirstream = NULL;
919 clean_args_no_options(argc, argv, cmd_filesystem_sync_usage);
921 if (check_argc_exact(argc - optind, 1))
922 usage(cmd_filesystem_sync_usage);
926 fd = btrfs_open_dir(path, &dirstream, 1);
930 printf("FSSync '%s'\n", path);
931 res = ioctl(fd, BTRFS_IOC_SYNC);
933 close_file_or_dir(fd, dirstream);
935 error("sync ioctl failed on '%s': %s", path, strerror(e));
942 static int parse_compress_type(char *s)
944 if (strcmp(optarg, "zlib") == 0)
945 return BTRFS_COMPRESS_ZLIB;
946 else if (strcmp(optarg, "lzo") == 0)
947 return BTRFS_COMPRESS_LZO;
949 error("unknown compression type %s", s);
954 static const char * const cmd_filesystem_defrag_usage[] = {
955 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
956 "Defragment a file or a directory",
959 "-r defragment files recursively",
960 "-c[zlib,lzo] compress the file while defragmenting",
961 "-f flush data to disk immediately after defragmenting",
962 "-s start defragment only from byte onward",
963 "-l len defragment only up to len bytes",
964 "-t size target extent size hint",
968 static int do_defrag(int fd, int fancy_ioctl,
969 struct btrfs_ioctl_defrag_range_args *range)
974 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
976 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
981 static int defrag_global_fancy_ioctl;
982 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
983 static int defrag_global_verbose;
984 static int defrag_global_errors;
985 static int defrag_callback(const char *fpath, const struct stat *sb,
986 int typeflag, struct FTW *ftwbuf)
992 if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
993 if (defrag_global_verbose)
994 printf("%s\n", fpath);
995 fd = open(fpath, O_RDWR);
998 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
1001 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1002 error("defrag range ioctl not "
1003 "supported in this kernel, please try "
1004 "without any options.");
1005 defrag_global_errors++;
1014 error("defrag failed on %s: %s", fpath, strerror(e));
1015 defrag_global_errors++;
1019 static int cmd_filesystem_defrag(int argc, char **argv)
1030 int compress_type = BTRFS_COMPRESS_NONE;
1033 defrag_global_errors = 0;
1034 defrag_global_verbose = 0;
1035 defrag_global_errors = 0;
1036 defrag_global_fancy_ioctl = 0;
1039 int c = getopt(argc, argv, "vrc::fs:l:t:");
1045 compress_type = BTRFS_COMPRESS_ZLIB;
1047 compress_type = parse_compress_type(optarg);
1048 defrag_global_fancy_ioctl = 1;
1052 defrag_global_fancy_ioctl = 1;
1055 defrag_global_verbose = 1;
1058 start = parse_size(optarg);
1059 defrag_global_fancy_ioctl = 1;
1062 len = parse_size(optarg);
1063 defrag_global_fancy_ioctl = 1;
1066 thresh = parse_size(optarg);
1067 if (thresh > (u32)-1) {
1069 "target extent size %llu too big, trimmed to %u",
1073 defrag_global_fancy_ioctl = 1;
1079 usage(cmd_filesystem_defrag_usage);
1083 if (check_argc_min(argc - optind, 1))
1084 usage(cmd_filesystem_defrag_usage);
1086 memset(&defrag_global_range, 0, sizeof(defrag_global_range));
1087 defrag_global_range.start = start;
1088 defrag_global_range.len = len;
1089 defrag_global_range.extent_thresh = (u32)thresh;
1090 if (compress_type) {
1091 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
1092 defrag_global_range.compress_type = compress_type;
1095 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
1097 for (i = optind; i < argc; i++) {
1101 fd = open_file_or_dir(argv[i], &dirstream);
1103 error("cannot open %s: %s\n", argv[i],
1105 defrag_global_errors++;
1106 close_file_or_dir(fd, dirstream);
1109 if (fstat(fd, &st)) {
1110 error("failed to stat %s: %s",
1111 argv[i], strerror(errno));
1112 defrag_global_errors++;
1113 close_file_or_dir(fd, dirstream);
1116 if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
1117 error("%s is not a directory or a regular file\n",
1119 defrag_global_errors++;
1120 close_file_or_dir(fd, dirstream);
1124 if (S_ISDIR(st.st_mode)) {
1125 ret = nftw(argv[i], defrag_callback, 10,
1126 FTW_MOUNT | FTW_PHYS);
1129 /* errors are handled in the callback */
1132 if (defrag_global_verbose)
1133 printf("%s\n", argv[i]);
1134 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1135 &defrag_global_range);
1139 if (defrag_global_verbose)
1140 printf("%s\n", argv[i]);
1141 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1142 &defrag_global_range);
1145 close_file_or_dir(fd, dirstream);
1146 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1147 error("defrag range ioctl not "
1148 "supported in this kernel, please try "
1149 "without any options.");
1150 defrag_global_errors++;
1154 error("defrag failed on %s: %s", argv[i], strerror(e));
1155 defrag_global_errors++;
1158 if (defrag_global_errors)
1159 fprintf(stderr, "total %d failures\n", defrag_global_errors);
1161 return !!defrag_global_errors;
1164 static const char * const cmd_filesystem_resize_usage[] = {
1165 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1166 "Resize a filesystem",
1167 "If 'max' is passed, the filesystem will occupy all available space",
1168 "on the device 'devid'.",
1169 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1173 static int cmd_filesystem_resize(int argc, char **argv)
1175 struct btrfs_ioctl_vol_args args;
1176 int fd, res, len, e;
1177 char *amount, *path;
1178 DIR *dirstream = NULL;
1181 clean_args_no_options(argc, argv, cmd_filesystem_resize_usage);
1183 if (check_argc_exact(argc - optind, 2))
1184 usage(cmd_filesystem_resize_usage);
1186 amount = argv[optind];
1187 path = argv[optind + 1];
1189 len = strlen(amount);
1190 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
1191 error("resize value too long (%s)", amount);
1195 res = stat(path, &st);
1197 error("resize: cannot stat %s: %s", path, strerror(errno));
1200 if (!S_ISDIR(st.st_mode)) {
1201 error("resize works on mounted filesystems and accepts only\n"
1202 "directories as argument. Passing file containing a btrfs image\n"
1203 "would resize the underlying filesystem instead of the image.\n");
1207 fd = btrfs_open_dir(path, &dirstream, 1);
1211 printf("Resize '%s' of '%s'\n", path, amount);
1212 memset(&args, 0, sizeof(args));
1213 strncpy_null(args.name, amount);
1214 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
1216 close_file_or_dir(fd, dirstream);
1220 error("unable to resize '%s': no enough free space",
1224 error("unable to resize '%s': %s", path, strerror(e));
1228 } else if (res > 0) {
1229 const char *err_str = btrfs_err_str(res);
1232 error("resizing of '%s' failed: %s", path, err_str);
1234 error("resizing of '%s' failed: unknown error %d",
1242 static const char * const cmd_filesystem_label_usage[] = {
1243 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1244 "Get or change the label of a filesystem",
1245 "With one argument, get the label of filesystem on <device>.",
1246 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1250 static int cmd_filesystem_label(int argc, char **argv)
1252 clean_args_no_options(argc, argv, cmd_filesystem_label_usage);
1254 if (check_argc_min(argc - optind, 1) ||
1255 check_argc_max(argc - optind, 2))
1256 usage(cmd_filesystem_label_usage);
1258 if (argc - optind > 1) {
1259 return set_label(argv[optind], argv[optind + 1]);
1261 char label[BTRFS_LABEL_SIZE];
1264 ret = get_label(argv[optind], label);
1266 fprintf(stdout, "%s\n", label);
1272 static const char filesystem_cmd_group_info[] =
1273 "overall filesystem tasks and information";
1275 const struct cmd_group filesystem_cmd_group = {
1276 filesystem_cmd_group_usage, filesystem_cmd_group_info, {
1277 { "df", cmd_filesystem_df, cmd_filesystem_df_usage, NULL, 0 },
1278 { "du", cmd_filesystem_du, cmd_filesystem_du_usage, NULL, 0 },
1279 { "show", cmd_filesystem_show, cmd_filesystem_show_usage, NULL,
1281 { "sync", cmd_filesystem_sync, cmd_filesystem_sync_usage, NULL,
1283 { "defragment", cmd_filesystem_defrag,
1284 cmd_filesystem_defrag_usage, NULL, 0 },
1285 { "balance", cmd_balance, NULL, &balance_cmd_group,
1287 { "resize", cmd_filesystem_resize, cmd_filesystem_resize_usage,
1289 { "label", cmd_filesystem_label, cmd_filesystem_label_usage,
1291 { "usage", cmd_filesystem_usage,
1292 cmd_filesystem_usage_usage, NULL, 0 },
1298 int cmd_filesystem(int argc, char **argv)
1300 return handle_command_group(&filesystem_cmd_group, argc, argv);