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"
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);
146 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
151 /* This really should never happen */
152 if (!sargs->total_spaces) {
156 count = sargs->total_spaces;
159 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
160 (count * sizeof(struct btrfs_ioctl_space_info)));
164 sargs->space_slots = count;
165 sargs->total_spaces = 0;
166 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
169 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
178 static void print_df(struct btrfs_ioctl_space_args *sargs, unsigned unit_mode)
181 struct btrfs_ioctl_space_info *sp = sargs->spaces;
183 for (i = 0; i < sargs->total_spaces; i++, sp++) {
184 printf("%s, %s: total=%s, used=%s\n",
185 btrfs_group_type_str(sp->flags),
186 btrfs_group_profile_str(sp->flags),
187 pretty_size_mode(sp->total_bytes, unit_mode),
188 pretty_size_mode(sp->used_bytes, unit_mode));
192 static int cmd_filesystem_df(int argc, char **argv)
194 struct btrfs_ioctl_space_args *sargs = NULL;
198 DIR *dirstream = NULL;
201 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
203 if (argc != 2 || argv[1][0] == '-')
204 usage(cmd_filesystem_df_usage);
208 fd = btrfs_open_dir(path, &dirstream, 1);
212 ret = get_df(fd, &sargs);
215 print_df(sargs, unit_mode);
218 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(-ret));
221 close_file_or_dir(fd, dirstream);
225 static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
228 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
229 int search_len = strlen(search);
231 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
232 uuid_unparse(fsid, uuidbuf);
233 if (!strncmp(uuidbuf, search, search_len))
236 if (*label && strcmp(label, search) == 0)
239 if (strcmp(mnt, search) == 0)
245 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
247 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
248 struct list_head *cur;
249 struct btrfs_device *device;
250 int search_len = strlen(search);
252 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
253 uuid_unparse(fs_devices->fsid, uuidbuf);
254 if (!strncmp(uuidbuf, search, search_len))
257 list_for_each(cur, &fs_devices->devices) {
258 device = list_entry(cur, struct btrfs_device, dev_list);
259 if ((device->label && strcmp(device->label, search) == 0) ||
260 strcmp(device->name, search) == 0)
267 * Sort devices by devid, ascending
269 static int cmp_device_id(void *priv, struct list_head *a,
272 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
274 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
277 return da->devid < db->devid ? -1 :
278 da->devid > db->devid ? 1 : 0;
281 static void splice_device_list(struct list_head *seed_devices,
282 struct list_head *all_devices)
284 struct btrfs_device *in_all, *next_all;
285 struct btrfs_device *in_seed, *next_seed;
287 list_for_each_entry_safe(in_all, next_all, all_devices, dev_list) {
288 list_for_each_entry_safe(in_seed, next_seed, seed_devices,
290 if (in_all->devid == in_seed->devid) {
292 * When do dev replace in a sprout fs
293 * to a dev in its seed fs, the replacing
294 * dev will reside in the sprout fs and
295 * the replaced dev will still exist
297 * So pick the latest one when showing
300 if (in_all->generation
301 < in_seed->generation) {
302 list_del(&in_all->dev_list);
304 } else if (in_all->generation
305 > in_seed->generation) {
306 list_del(&in_seed->dev_list);
314 list_splice(seed_devices, all_devices);
317 static void print_devices(struct btrfs_fs_devices *fs_devices,
318 u64 *devs_found, unsigned unit_mode)
320 struct btrfs_device *device;
321 struct btrfs_fs_devices *cur_fs;
322 struct list_head *all_devices;
324 all_devices = &fs_devices->devices;
325 cur_fs = fs_devices->seed;
326 /* add all devices of seed fs to the fs to be printed */
328 splice_device_list(&cur_fs->devices, all_devices);
329 cur_fs = cur_fs->seed;
332 list_sort(NULL, all_devices, cmp_device_id);
333 list_for_each_entry(device, all_devices, dev_list) {
334 printf("\tdevid %4llu size %s used %s path %s\n",
335 (unsigned long long)device->devid,
336 pretty_size_mode(device->total_bytes, unit_mode),
337 pretty_size_mode(device->bytes_used, unit_mode),
344 static void print_one_uuid(struct btrfs_fs_devices *fs_devices,
347 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
348 struct btrfs_device *device;
352 if (add_seen_fsid(fs_devices->fsid))
355 uuid_unparse(fs_devices->fsid, uuidbuf);
356 device = list_entry(fs_devices->devices.next, struct btrfs_device,
358 if (device->label && device->label[0])
359 printf("Label: '%s' ", device->label);
361 printf("Label: none ");
363 total = device->total_devs;
364 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
365 (unsigned long long)total,
366 pretty_size_mode(device->super_bytes_used, unit_mode));
368 print_devices(fs_devices, &devs_found, unit_mode);
370 if (devs_found < total) {
371 printf("\t*** Some devices missing\n");
376 /* adds up all the used spaces as reported by the space info ioctl
378 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
382 for (i = 0; i < si->total_spaces; i++)
383 ret += si->spaces[i].used_bytes;
387 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
388 struct btrfs_ioctl_dev_info_args *dev_info,
389 struct btrfs_ioctl_space_args *space_info,
390 char *label, unsigned unit_mode)
395 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
396 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
399 ret = add_seen_fsid(fs_info->fsid);
405 uuid_unparse(fs_info->fsid, uuidbuf);
407 printf("Label: '%s' ", label);
409 printf("Label: none ");
411 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
412 fs_info->num_devices,
413 pretty_size_mode(calc_used_bytes(space_info),
416 for (i = 0; i < fs_info->num_devices; i++) {
417 char *canonical_path;
419 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
421 /* Add check for missing devices even mounted */
422 fd = open((char *)tmp_dev_info->path, O_RDONLY);
428 canonical_path = canonicalize_path((char *)tmp_dev_info->path);
429 printf("\tdevid %4llu size %s used %s path %s\n",
431 pretty_size_mode(tmp_dev_info->total_bytes, unit_mode),
432 pretty_size_mode(tmp_dev_info->bytes_used, unit_mode),
435 free(canonical_path);
439 printf("\t*** Some devices missing\n");
444 static int btrfs_scan_kernel(void *search, unsigned unit_mode)
450 struct btrfs_ioctl_fs_info_args fs_info_arg;
451 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
452 struct btrfs_ioctl_space_args *space_info_arg = NULL;
453 char label[BTRFS_LABEL_SIZE];
455 f = setmntent("/proc/self/mounts", "r");
459 memset(label, 0, sizeof(label));
460 while ((mnt = getmntent(f)) != NULL) {
461 if (strcmp(mnt->mnt_type, "btrfs"))
463 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
470 /* skip all fs already shown as mounted fs */
471 if (is_seen_fsid(fs_info_arg.fsid))
474 ret = get_label_mounted(mnt->mnt_dir, label);
475 /* provide backward kernel compatibility */
477 ret = get_label_unmounted(
478 (const char *)dev_info_arg->path, label);
484 if (search && !match_search_item_kernel(fs_info_arg.fsid,
485 mnt->mnt_dir, label, search)) {
491 fd = open(mnt->mnt_dir, O_RDONLY);
492 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
493 print_one_fs(&fs_info_arg, dev_info_arg,
494 space_info_arg, label, unit_mode);
495 kfree(space_info_arg);
496 memset(label, 0, sizeof(label));
510 static int dev_to_fsid(char *dev, __u8 *fsid)
512 struct btrfs_super_block *disk_super;
513 char buf[BTRFS_SUPER_INFO_SIZE];
517 fd = open(dev, O_RDONLY);
523 disk_super = (struct btrfs_super_block *)buf;
524 ret = btrfs_read_dev_super(fd, disk_super,
525 BTRFS_SUPER_INFO_OFFSET, 0);
529 memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
537 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
539 struct btrfs_fs_devices *cur_seed, *next_seed;
540 struct btrfs_device *device;
542 while (!list_empty(&fs_devices->devices)) {
543 device = list_entry(fs_devices->devices.next,
544 struct btrfs_device, dev_list);
545 list_del(&device->dev_list);
552 /* free seed fs chain */
553 cur_seed = fs_devices->seed;
554 fs_devices->seed = NULL;
556 next_seed = cur_seed->seed;
559 cur_seed = next_seed;
562 list_del(&fs_devices->list);
566 static int copy_device(struct btrfs_device *dst,
567 struct btrfs_device *src)
569 dst->devid = src->devid;
570 memcpy(dst->uuid, src->uuid, BTRFS_UUID_SIZE);
571 if (src->name == NULL)
574 dst->name = strdup(src->name);
578 if (src->label == NULL)
581 dst->label = strdup(src->label);
587 dst->total_devs = src->total_devs;
588 dst->super_bytes_used = src->super_bytes_used;
589 dst->total_bytes = src->total_bytes;
590 dst->bytes_used = src->bytes_used;
591 dst->generation = src->generation;
596 static int copy_fs_devices(struct btrfs_fs_devices *dst,
597 struct btrfs_fs_devices *src)
599 struct btrfs_device *cur_dev, *dev_copy;
602 memcpy(dst->fsid, src->fsid, BTRFS_FSID_SIZE);
603 INIT_LIST_HEAD(&dst->devices);
606 list_for_each_entry(cur_dev, &src->devices, dev_list) {
607 dev_copy = malloc(sizeof(*dev_copy));
613 ret = copy_device(dev_copy, cur_dev);
619 list_add(&dev_copy->dev_list, &dst->devices);
620 dev_copy->fs_devices = dst;
626 static int find_and_copy_seed(struct btrfs_fs_devices *seed,
627 struct btrfs_fs_devices *copy,
628 struct list_head *fs_uuids) {
629 struct btrfs_fs_devices *cur_fs;
631 list_for_each_entry(cur_fs, fs_uuids, list)
632 if (!memcmp(seed->fsid, cur_fs->fsid, BTRFS_FSID_SIZE))
633 return copy_fs_devices(copy, cur_fs);
638 static int has_seed_devices(struct btrfs_fs_devices *fs_devices)
640 struct btrfs_device *device;
641 int dev_cnt_total, dev_cnt = 0;
643 device = list_first_entry(&fs_devices->devices, struct btrfs_device,
646 dev_cnt_total = device->total_devs;
648 list_for_each_entry(device, &fs_devices->devices, dev_list)
651 return dev_cnt_total != dev_cnt;
654 static int search_umounted_fs_uuids(struct list_head *all_uuids,
655 char *search, int *found)
657 struct btrfs_fs_devices *cur_fs, *fs_copy;
658 struct list_head *fs_uuids;
661 fs_uuids = btrfs_scanned_uuids();
664 * The fs_uuids list is global, and open_ctree_* will
665 * modify it, make a private copy here
667 list_for_each_entry(cur_fs, fs_uuids, list) {
668 /* don't bother handle all fs, if search target specified */
670 if (uuid_search(cur_fs, search) == 0)
676 /* skip all fs already shown as mounted fs */
677 if (is_seen_fsid(cur_fs->fsid))
680 fs_copy = calloc(1, sizeof(*fs_copy));
686 ret = copy_fs_devices(fs_copy, cur_fs);
692 list_add(&fs_copy->list, all_uuids);
699 static int map_seed_devices(struct list_head *all_uuids)
701 struct btrfs_fs_devices *cur_fs, *cur_seed;
702 struct btrfs_fs_devices *seed_copy;
703 struct btrfs_fs_devices *opened_fs;
704 struct btrfs_device *device;
705 struct btrfs_fs_info *fs_info;
706 struct list_head *fs_uuids;
709 fs_uuids = btrfs_scanned_uuids();
711 list_for_each_entry(cur_fs, all_uuids, list) {
712 device = list_first_entry(&cur_fs->devices,
713 struct btrfs_device, dev_list);
717 /* skip fs without seeds */
718 if (!has_seed_devices(cur_fs))
722 * open_ctree_* detects seed/sprout mapping
724 fs_info = open_ctree_fs_info(device->name, 0, 0,
730 * copy the seed chain under the opened fs
732 opened_fs = fs_info->fs_devices;
734 while (opened_fs->seed) {
735 seed_copy = malloc(sizeof(*seed_copy));
740 ret = find_and_copy_seed(opened_fs->seed, seed_copy,
747 cur_seed->seed = seed_copy;
749 opened_fs = opened_fs->seed;
750 cur_seed = cur_seed->seed;
753 close_ctree(fs_info->chunk_root);
759 close_ctree(fs_info->chunk_root);
763 static const char * const cmd_filesystem_show_usage[] = {
764 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
765 "Show the structure of a filesystem",
766 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
767 "-m|--mounted show only mounted btrfs",
769 "If no argument is given, structure of all present filesystems is shown.",
773 static int cmd_filesystem_show(int argc, char **argv)
775 LIST_HEAD(all_uuids);
776 struct btrfs_fs_devices *fs_devices;
779 /* default, search both kernel and udev */
784 __u8 fsid[BTRFS_FSID_SIZE];
785 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
789 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
793 static const struct option long_options[] = {
794 { "all-devices", no_argument, NULL, 'd'},
795 { "mounted", no_argument, NULL, 'm'},
799 c = getopt_long(argc, argv, "dm", long_options, NULL);
804 where = BTRFS_SCAN_LBLKID;
807 where = BTRFS_SCAN_MOUNTED;
810 usage(cmd_filesystem_show_usage);
814 if (check_argc_max(argc, optind + 1))
815 usage(cmd_filesystem_show_usage);
818 search = argv[optind];
820 usage(cmd_filesystem_show_usage);
821 type = check_arg_type(search);
824 * For search is a device:
825 * realpath do /dev/mapper/XX => /dev/dm-X
826 * which is required by BTRFS_SCAN_DEV
827 * For search is a mountpoint:
828 * realpath do /mnt/btrfs/ => /mnt/btrfs
829 * which shall be recognized by btrfs_scan_kernel()
831 if (realpath(search, path))
835 * Needs special handling if input arg is block dev And if
836 * input arg is mount-point just print it right away
838 if (type == BTRFS_ARG_BLKDEV && where != BTRFS_SCAN_LBLKID) {
839 ret = get_btrfs_mount(search, mp, sizeof(mp));
841 /* given block dev is mounted */
843 type = BTRFS_ARG_MNTPOINT;
845 ret = dev_to_fsid(search, fsid);
848 "ERROR: No btrfs on %s\n",
852 uuid_unparse(fsid, uuid_buf);
854 type = BTRFS_ARG_UUID;
860 if (where == BTRFS_SCAN_LBLKID)
863 /* show mounted btrfs */
864 ret = btrfs_scan_kernel(search, unit_mode);
865 if (search && !ret) {
866 /* since search is found we are done */
870 /* shows mounted only */
871 if (where == BTRFS_SCAN_MOUNTED)
875 ret = btrfs_scan_lblkid();
878 fprintf(stderr, "ERROR: %d while scanning\n", ret);
882 ret = search_umounted_fs_uuids(&all_uuids, search, &found);
885 "ERROR: %d while searching target device\n", ret);
890 * The seed/sprout mapping are not detected yet,
891 * do mapping build for all umounted fs
893 ret = map_seed_devices(&all_uuids);
896 "ERROR: %d while mapping seed devices\n", ret);
900 list_for_each_entry(fs_devices, &all_uuids, list)
901 print_one_uuid(fs_devices, unit_mode);
903 if (search && !found)
906 while (!list_empty(&all_uuids)) {
907 fs_devices = list_entry(all_uuids.next,
908 struct btrfs_fs_devices, list);
909 free_fs_devices(fs_devices);
916 static const char * const cmd_filesystem_sync_usage[] = {
917 "btrfs filesystem sync <path>",
918 "Force a sync on a filesystem",
922 static int cmd_filesystem_sync(int argc, char **argv)
926 DIR *dirstream = NULL;
928 if (check_argc_exact(argc, 2))
929 usage(cmd_filesystem_sync_usage);
933 fd = btrfs_open_dir(path, &dirstream, 1);
937 printf("FSSync '%s'\n", path);
938 res = ioctl(fd, BTRFS_IOC_SYNC);
940 close_file_or_dir(fd, dirstream);
942 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
950 static int parse_compress_type(char *s)
952 if (strcmp(optarg, "zlib") == 0)
953 return BTRFS_COMPRESS_ZLIB;
954 else if (strcmp(optarg, "lzo") == 0)
955 return BTRFS_COMPRESS_LZO;
957 fprintf(stderr, "Unknown compress type %s\n", s);
962 static const char * const cmd_filesystem_defrag_usage[] = {
963 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
964 "Defragment a file or a directory",
967 "-r defragment files recursively",
968 "-c[zlib,lzo] compress the file while defragmenting",
969 "-f flush data to disk immediately after defragmenting",
970 "-s start defragment only from byte onward",
971 "-l len defragment only up to len bytes",
972 "-t size target extent size hint",
976 static int do_defrag(int fd, int fancy_ioctl,
977 struct btrfs_ioctl_defrag_range_args *range)
982 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
984 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
989 static int defrag_global_fancy_ioctl;
990 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
991 static int defrag_global_verbose;
992 static int defrag_global_errors;
993 static int defrag_callback(const char *fpath, const struct stat *sb,
994 int typeflag, struct FTW *ftwbuf)
1000 if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
1001 if (defrag_global_verbose)
1002 printf("%s\n", fpath);
1003 fd = open(fpath, O_RDWR);
1007 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
1010 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1011 fprintf(stderr, "ERROR: defrag range ioctl not "
1012 "supported in this kernel, please try "
1013 "without any options.\n");
1014 defrag_global_errors++;
1023 fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
1024 defrag_global_errors++;
1028 static int cmd_filesystem_defrag(int argc, char **argv)
1039 int compress_type = BTRFS_COMPRESS_NONE;
1042 defrag_global_errors = 0;
1043 defrag_global_verbose = 0;
1044 defrag_global_errors = 0;
1045 defrag_global_fancy_ioctl = 0;
1048 int c = getopt(argc, argv, "vrc::fs:l:t:");
1054 compress_type = BTRFS_COMPRESS_ZLIB;
1056 compress_type = parse_compress_type(optarg);
1057 defrag_global_fancy_ioctl = 1;
1061 defrag_global_fancy_ioctl = 1;
1064 defrag_global_verbose = 1;
1067 start = parse_size(optarg);
1068 defrag_global_fancy_ioctl = 1;
1071 len = parse_size(optarg);
1072 defrag_global_fancy_ioctl = 1;
1075 thresh = parse_size(optarg);
1076 if (thresh > (u32)-1) {
1078 "WARNING: target extent size %llu too big, trimmed to %u\n",
1082 defrag_global_fancy_ioctl = 1;
1088 usage(cmd_filesystem_defrag_usage);
1092 if (check_argc_min(argc - optind, 1))
1093 usage(cmd_filesystem_defrag_usage);
1095 memset(&defrag_global_range, 0, sizeof(defrag_global_range));
1096 defrag_global_range.start = start;
1097 defrag_global_range.len = len;
1098 defrag_global_range.extent_thresh = (u32)thresh;
1099 if (compress_type) {
1100 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
1101 defrag_global_range.compress_type = compress_type;
1104 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
1106 for (i = optind; i < argc; i++) {
1110 fd = open_file_or_dir(argv[i], &dirstream);
1112 fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
1114 defrag_global_errors++;
1115 close_file_or_dir(fd, dirstream);
1118 if (fstat(fd, &st)) {
1119 fprintf(stderr, "ERROR: failed to stat %s - %s\n",
1120 argv[i], strerror(errno));
1121 defrag_global_errors++;
1122 close_file_or_dir(fd, dirstream);
1125 if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
1127 "ERROR: %s is not a directory or a regular file\n",
1129 defrag_global_errors++;
1130 close_file_or_dir(fd, dirstream);
1134 if (S_ISDIR(st.st_mode)) {
1135 ret = nftw(argv[i], defrag_callback, 10,
1136 FTW_MOUNT | FTW_PHYS);
1139 /* errors are handled in the callback */
1142 if (defrag_global_verbose)
1143 printf("%s\n", argv[i]);
1144 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1145 &defrag_global_range);
1149 if (defrag_global_verbose)
1150 printf("%s\n", argv[i]);
1151 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1152 &defrag_global_range);
1155 close_file_or_dir(fd, dirstream);
1156 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1157 fprintf(stderr, "ERROR: defrag range ioctl not "
1158 "supported in this kernel, please try "
1159 "without any options.\n");
1160 defrag_global_errors++;
1164 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
1165 argv[i], strerror(e));
1166 defrag_global_errors++;
1169 if (defrag_global_errors)
1170 fprintf(stderr, "total %d failures\n", defrag_global_errors);
1172 return !!defrag_global_errors;
1175 static const char * const cmd_filesystem_resize_usage[] = {
1176 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1177 "Resize a filesystem",
1178 "If 'max' is passed, the filesystem will occupy all available space",
1179 "on the device 'devid'.",
1180 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1184 static int cmd_filesystem_resize(int argc, char **argv)
1186 struct btrfs_ioctl_vol_args args;
1187 int fd, res, len, e;
1188 char *amount, *path;
1189 DIR *dirstream = NULL;
1192 if (check_argc_exact(argc, 3))
1193 usage(cmd_filesystem_resize_usage);
1198 len = strlen(amount);
1199 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
1200 fprintf(stderr, "ERROR: size value too long ('%s)\n",
1205 res = stat(path, &st);
1207 fprintf(stderr, "ERROR: resize: cannot stat %s: %s\n",
1208 path, strerror(errno));
1211 if (!S_ISDIR(st.st_mode)) {
1213 "ERROR: resize works on mounted filesystems and accepts only\n"
1214 "directories as argument. Passing file containing a btrfs image\n"
1215 "would resize the underlying filesystem instead of the image.\n");
1219 fd = btrfs_open_dir(path, &dirstream, 1);
1223 printf("Resize '%s' of '%s'\n", path, amount);
1224 memset(&args, 0, sizeof(args));
1225 strncpy_null(args.name, amount);
1226 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
1228 close_file_or_dir(fd, dirstream);
1232 fprintf(stderr, "ERROR: unable to resize '%s' - no enouth free space\n",
1236 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
1241 } else if (res > 0) {
1242 const char *err_str = btrfs_err_str(res);
1245 fprintf(stderr, "ERROR: btrfs error resizing '%s' - %s\n",
1249 "ERROR: btrfs error resizing '%s' - unknown btrfs_err_code %d\n",
1257 static const char * const cmd_filesystem_label_usage[] = {
1258 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1259 "Get or change the label of a filesystem",
1260 "With one argument, get the label of filesystem on <device>.",
1261 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1265 static int cmd_filesystem_label(int argc, char **argv)
1267 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
1268 usage(cmd_filesystem_label_usage);
1271 return set_label(argv[1], argv[2]);
1273 char label[BTRFS_LABEL_SIZE];
1276 ret = get_label(argv[1], label);
1278 fprintf(stdout, "%s\n", label);
1284 static const char filesystem_cmd_group_info[] =
1285 "overall filesystem tasks and information";
1287 const struct cmd_group filesystem_cmd_group = {
1288 filesystem_cmd_group_usage, filesystem_cmd_group_info, {
1289 { "df", cmd_filesystem_df, cmd_filesystem_df_usage, NULL, 0 },
1290 { "show", cmd_filesystem_show, cmd_filesystem_show_usage, NULL,
1292 { "sync", cmd_filesystem_sync, cmd_filesystem_sync_usage, NULL,
1294 { "defragment", cmd_filesystem_defrag,
1295 cmd_filesystem_defrag_usage, NULL, 0 },
1296 { "balance", cmd_balance, NULL, &balance_cmd_group,
1298 { "resize", cmd_filesystem_resize, cmd_filesystem_resize_usage,
1300 { "label", cmd_filesystem_label, cmd_filesystem_label_usage,
1302 { "usage", cmd_filesystem_usage,
1303 cmd_filesystem_usage_usage, NULL, 0 },
1309 int cmd_filesystem(int argc, char **argv)
1311 return handle_command_group(&filesystem_cmd_group, argc, argv);