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_OUTPUT_UNIT_DF,
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 = open_file_or_dir(path, &dirstream);
210 fprintf(stderr, "ERROR: can't access '%s'\n", path);
213 ret = get_df(fd, &sargs);
216 print_df(sargs, unit_mode);
219 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(-ret));
222 close_file_or_dir(fd, dirstream);
226 static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
229 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
230 int search_len = strlen(search);
232 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
233 uuid_unparse(fsid, uuidbuf);
234 if (!strncmp(uuidbuf, search, search_len))
237 if (strlen(label) && strcmp(label, search) == 0)
240 if (strcmp(mnt, search) == 0)
246 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
248 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
249 struct list_head *cur;
250 struct btrfs_device *device;
251 int search_len = strlen(search);
253 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
254 uuid_unparse(fs_devices->fsid, uuidbuf);
255 if (!strncmp(uuidbuf, search, search_len))
258 list_for_each(cur, &fs_devices->devices) {
259 device = list_entry(cur, struct btrfs_device, dev_list);
260 if ((device->label && strcmp(device->label, search) == 0) ||
261 strcmp(device->name, search) == 0)
268 * Sort devices by devid, ascending
270 static int cmp_device_id(void *priv, struct list_head *a,
273 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
275 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
278 return da->devid < db->devid ? -1 :
279 da->devid > db->devid ? 1 : 0;
282 static void splice_device_list(struct list_head *seed_devices,
283 struct list_head *all_devices)
285 struct btrfs_device *in_all, *next_all;
286 struct btrfs_device *in_seed, *next_seed;
288 list_for_each_entry_safe(in_all, next_all, all_devices, dev_list) {
289 list_for_each_entry_safe(in_seed, next_seed, seed_devices,
291 if (in_all->devid == in_seed->devid) {
293 * When do dev replace in a sprout fs
294 * to a dev in its seed fs, the replacing
295 * dev will reside in the sprout fs and
296 * the replaced dev will still exist
298 * So pick the latest one when showing
301 if (in_all->generation
302 < in_seed->generation) {
303 list_del(&in_all->dev_list);
305 } else if (in_all->generation
306 > in_seed->generation) {
307 list_del(&in_seed->dev_list);
315 list_splice(seed_devices, all_devices);
318 static void print_devices(struct btrfs_fs_devices *fs_devices,
319 u64 *devs_found, unsigned unit_mode)
321 struct btrfs_device *device;
322 struct btrfs_fs_devices *cur_fs;
323 struct list_head *all_devices;
325 all_devices = &fs_devices->devices;
326 cur_fs = fs_devices->seed;
327 /* add all devices of seed fs to the fs to be printed */
329 splice_device_list(&cur_fs->devices, all_devices);
330 cur_fs = cur_fs->seed;
333 list_sort(NULL, all_devices, cmp_device_id);
334 list_for_each_entry(device, all_devices, dev_list) {
335 printf("\tdevid %4llu size %s used %s path %s\n",
336 (unsigned long long)device->devid,
337 pretty_size_mode(device->total_bytes, unit_mode),
338 pretty_size_mode(device->bytes_used, unit_mode),
345 static void print_one_uuid(struct btrfs_fs_devices *fs_devices,
348 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
349 struct btrfs_device *device;
353 if (add_seen_fsid(fs_devices->fsid))
356 uuid_unparse(fs_devices->fsid, uuidbuf);
357 device = list_entry(fs_devices->devices.next, struct btrfs_device,
359 if (device->label && device->label[0])
360 printf("Label: '%s' ", device->label);
362 printf("Label: none ");
364 total = device->total_devs;
365 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
366 (unsigned long long)total,
367 pretty_size_mode(device->super_bytes_used, unit_mode));
369 print_devices(fs_devices, &devs_found, unit_mode);
371 if (devs_found < total) {
372 printf("\t*** Some devices missing\n");
377 /* adds up all the used spaces as reported by the space info ioctl
379 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
383 for (i = 0; i < si->total_spaces; i++)
384 ret += si->spaces[i].used_bytes;
388 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
389 struct btrfs_ioctl_dev_info_args *dev_info,
390 struct btrfs_ioctl_space_args *space_info,
391 char *label, char *path, unsigned unit_mode)
396 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
397 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
400 ret = add_seen_fsid(fs_info->fsid);
406 uuid_unparse(fs_info->fsid, uuidbuf);
407 if (label && strlen(label))
408 printf("Label: '%s' ", label);
410 printf("Label: none ");
412 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
413 fs_info->num_devices,
414 pretty_size_mode(calc_used_bytes(space_info),
417 for (i = 0; i < fs_info->num_devices; i++) {
418 char *canonical_path;
420 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
422 /* Add check for missing devices even mounted */
423 fd = open((char *)tmp_dev_info->path, O_RDONLY);
429 canonical_path = canonicalize_path((char *)tmp_dev_info->path);
430 printf("\tdevid %4llu size %s used %s path %s\n",
432 pretty_size_mode(tmp_dev_info->total_bytes, unit_mode),
433 pretty_size_mode(tmp_dev_info->bytes_used, unit_mode),
436 free(canonical_path);
440 printf("\t*** Some devices missing\n");
445 static int btrfs_scan_kernel(void *search, unsigned unit_mode)
451 struct btrfs_ioctl_fs_info_args fs_info_arg;
452 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
453 struct btrfs_ioctl_space_args *space_info_arg = NULL;
454 char label[BTRFS_LABEL_SIZE];
456 f = setmntent("/proc/self/mounts", "r");
460 memset(label, 0, sizeof(label));
461 while ((mnt = getmntent(f)) != NULL) {
462 if (strcmp(mnt->mnt_type, "btrfs"))
464 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
471 if (get_label_mounted(mnt->mnt_dir, label)) {
475 if (search && !match_search_item_kernel(fs_info_arg.fsid,
476 mnt->mnt_dir, label, search)) {
481 fd = open(mnt->mnt_dir, O_RDONLY);
482 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
483 print_one_fs(&fs_info_arg, dev_info_arg,
484 space_info_arg, label, mnt->mnt_dir,
486 kfree(space_info_arg);
487 memset(label, 0, sizeof(label));
500 static int dev_to_fsid(char *dev, __u8 *fsid)
502 struct btrfs_super_block *disk_super;
511 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);
533 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
535 struct btrfs_fs_devices *cur_seed, *next_seed;
536 struct btrfs_device *device;
538 while (!list_empty(&fs_devices->devices)) {
539 device = list_entry(fs_devices->devices.next,
540 struct btrfs_device, dev_list);
541 list_del(&device->dev_list);
548 /* free seed fs chain */
549 cur_seed = fs_devices->seed;
550 fs_devices->seed = NULL;
552 next_seed = cur_seed->seed;
555 cur_seed = next_seed;
558 list_del(&fs_devices->list);
562 static int copy_device(struct btrfs_device *dst,
563 struct btrfs_device *src)
565 dst->devid = src->devid;
566 memcpy(dst->uuid, src->uuid, BTRFS_UUID_SIZE);
567 if (src->name == NULL)
570 dst->name = strdup(src->name);
574 if (src->label == NULL)
577 dst->label = strdup(src->label);
583 dst->total_devs = src->total_devs;
584 dst->super_bytes_used = src->super_bytes_used;
585 dst->total_bytes = src->total_bytes;
586 dst->bytes_used = src->bytes_used;
587 dst->generation = src->generation;
592 static int copy_fs_devices(struct btrfs_fs_devices *dst,
593 struct btrfs_fs_devices *src)
595 struct btrfs_device *cur_dev, *dev_copy;
598 memcpy(dst->fsid, src->fsid, BTRFS_FSID_SIZE);
599 INIT_LIST_HEAD(&dst->devices);
602 list_for_each_entry(cur_dev, &src->devices, dev_list) {
603 dev_copy = malloc(sizeof(*dev_copy));
609 ret = copy_device(dev_copy, cur_dev);
615 list_add(&dev_copy->dev_list, &dst->devices);
616 dev_copy->fs_devices = dst;
622 static int find_and_copy_seed(struct btrfs_fs_devices *seed,
623 struct btrfs_fs_devices *copy,
624 struct list_head *fs_uuids) {
625 struct btrfs_fs_devices *cur_fs;
627 list_for_each_entry(cur_fs, fs_uuids, list)
628 if (!memcmp(seed->fsid, cur_fs->fsid, BTRFS_FSID_SIZE))
629 return copy_fs_devices(copy, cur_fs);
634 static int has_seed_devices(struct btrfs_fs_devices *fs_devices)
636 struct btrfs_device *device;
637 int dev_cnt_total, dev_cnt = 0;
639 device = list_first_entry(&fs_devices->devices, struct btrfs_device,
642 dev_cnt_total = device->total_devs;
644 list_for_each_entry(device, &fs_devices->devices, dev_list)
647 return dev_cnt_total != dev_cnt;
650 static int search_umounted_fs_uuids(struct list_head *all_uuids,
651 char *search, int *found)
653 struct btrfs_fs_devices *cur_fs, *fs_copy;
654 struct list_head *fs_uuids;
657 fs_uuids = btrfs_scanned_uuids();
660 * The fs_uuids list is global, and open_ctree_* will
661 * modify it, make a private copy here
663 list_for_each_entry(cur_fs, fs_uuids, list) {
664 /* don't bother handle all fs, if search target specified */
666 if (uuid_search(cur_fs, search) == 0)
672 /* skip all fs already shown as mounted fs */
673 if (is_seen_fsid(cur_fs->fsid))
676 fs_copy = malloc(sizeof(*fs_copy));
682 ret = copy_fs_devices(fs_copy, cur_fs);
688 list_add(&fs_copy->list, all_uuids);
695 static int map_seed_devices(struct list_head *all_uuids)
697 struct btrfs_fs_devices *cur_fs, *cur_seed;
698 struct btrfs_fs_devices *seed_copy;
699 struct btrfs_fs_devices *opened_fs;
700 struct btrfs_device *device;
701 struct btrfs_fs_info *fs_info;
702 struct list_head *fs_uuids;
705 fs_uuids = btrfs_scanned_uuids();
707 list_for_each_entry(cur_fs, all_uuids, list) {
708 device = list_first_entry(&cur_fs->devices,
709 struct btrfs_device, dev_list);
713 /* skip fs without seeds */
714 if (!has_seed_devices(cur_fs))
718 * open_ctree_* detects seed/sprout mapping
720 fs_info = open_ctree_fs_info(device->name, 0, 0,
726 * copy the seed chain under the opened fs
728 opened_fs = fs_info->fs_devices;
730 while (opened_fs->seed) {
731 seed_copy = malloc(sizeof(*seed_copy));
736 ret = find_and_copy_seed(opened_fs->seed, seed_copy,
743 cur_seed->seed = seed_copy;
745 opened_fs = opened_fs->seed;
746 cur_seed = cur_seed->seed;
749 close_ctree(fs_info->chunk_root);
755 close_ctree(fs_info->chunk_root);
759 static const char * const cmd_filesystem_show_usage[] = {
760 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
761 "Show the structure of a filesystem",
762 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
763 "-m|--mounted show only mounted btrfs",
764 HELPINFO_OUTPUT_UNIT,
765 "If no argument is given, structure of all present filesystems is shown.",
769 static int cmd_filesystem_show(int argc, char **argv)
771 LIST_HEAD(all_uuids);
772 struct btrfs_fs_devices *fs_devices;
775 /* default, search both kernel and udev */
780 __u8 fsid[BTRFS_FSID_SIZE];
781 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
785 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
789 static const struct option long_options[] = {
790 { "all-devices", no_argument, NULL, 'd'},
791 { "mounted", no_argument, NULL, 'm'},
795 c = getopt_long(argc, argv, "dm", long_options, NULL);
800 where = BTRFS_SCAN_LBLKID;
803 where = BTRFS_SCAN_MOUNTED;
806 usage(cmd_filesystem_show_usage);
810 if (check_argc_max(argc, optind + 1))
811 usage(cmd_filesystem_show_usage);
814 search = argv[optind];
815 if (strlen(search) == 0)
816 usage(cmd_filesystem_show_usage);
817 type = check_arg_type(search);
820 * For search is a device:
821 * realpath do /dev/mapper/XX => /dev/dm-X
822 * which is required by BTRFS_SCAN_DEV
823 * For search is a mountpoint:
824 * realpath do /mnt/btrfs/ => /mnt/btrfs
825 * which shall be recognized by btrfs_scan_kernel()
827 if (realpath(search, path))
831 * Needs special handling if input arg is block dev And if
832 * input arg is mount-point just print it right away
834 if (type == BTRFS_ARG_BLKDEV && where != BTRFS_SCAN_LBLKID) {
835 ret = get_btrfs_mount(search, mp, sizeof(mp));
837 /* given block dev is mounted */
839 type = BTRFS_ARG_MNTPOINT;
841 ret = dev_to_fsid(search, fsid);
844 "ERROR: No btrfs on %s\n",
848 uuid_unparse(fsid, uuid_buf);
850 type = BTRFS_ARG_UUID;
856 if (where == BTRFS_SCAN_LBLKID)
859 /* show mounted btrfs */
860 ret = btrfs_scan_kernel(search, unit_mode);
861 if (search && !ret) {
862 /* since search is found we are done */
866 /* shows mounted only */
867 if (where == BTRFS_SCAN_MOUNTED)
871 ret = btrfs_scan_lblkid();
874 fprintf(stderr, "ERROR: %d while scanning\n", ret);
878 ret = search_umounted_fs_uuids(&all_uuids, search, &found);
881 "ERROR: %d while searching target device\n", ret);
886 * The seed/sprout mapping are not detected yet,
887 * do mapping build for all umounted fs
889 ret = map_seed_devices(&all_uuids);
892 "ERROR: %d while mapping seed devices\n", ret);
896 list_for_each_entry(fs_devices, &all_uuids, list)
897 print_one_uuid(fs_devices, unit_mode);
899 if (search && !found)
902 while (!list_empty(&all_uuids)) {
903 fs_devices = list_entry(all_uuids.next,
904 struct btrfs_fs_devices, list);
905 free_fs_devices(fs_devices);
908 printf("%s\n", PACKAGE_STRING);
913 static const char * const cmd_filesystem_sync_usage[] = {
914 "btrfs filesystem sync <path>",
915 "Force a sync on a filesystem",
919 static int cmd_filesystem_sync(int argc, char **argv)
923 DIR *dirstream = NULL;
925 if (check_argc_exact(argc, 2))
926 usage(cmd_filesystem_sync_usage);
930 fd = open_file_or_dir(path, &dirstream);
932 fprintf(stderr, "ERROR: can't access '%s'\n", path);
936 printf("FSSync '%s'\n", path);
937 res = ioctl(fd, BTRFS_IOC_SYNC);
939 close_file_or_dir(fd, dirstream);
941 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
949 static int parse_compress_type(char *s)
951 if (strcmp(optarg, "zlib") == 0)
952 return BTRFS_COMPRESS_ZLIB;
953 else if (strcmp(optarg, "lzo") == 0)
954 return BTRFS_COMPRESS_LZO;
956 fprintf(stderr, "Unknown compress type %s\n", s);
961 static const char * const cmd_filesystem_defrag_usage[] = {
962 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
963 "Defragment a file or a directory",
966 "-r defragment files recursively",
967 "-c[zlib,lzo] compress the file while defragmenting",
968 "-f flush data to disk immediately after defragmenting",
969 "-s start defragment only from byte onward",
970 "-l len defragment only up to len bytes",
971 "-t size target extent size hint",
975 static int do_defrag(int fd, int fancy_ioctl,
976 struct btrfs_ioctl_defrag_range_args *range)
981 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
983 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
988 static int defrag_global_fancy_ioctl;
989 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
990 static int defrag_global_verbose;
991 static int defrag_global_errors;
992 static int defrag_callback(const char *fpath, const struct stat *sb,
993 int typeflag, struct FTW *ftwbuf)
999 if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
1000 if (defrag_global_verbose)
1001 printf("%s\n", fpath);
1002 fd = open(fpath, O_RDWR);
1006 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
1009 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1010 fprintf(stderr, "ERROR: defrag range ioctl not "
1011 "supported in this kernel, please try "
1012 "without any options.\n");
1013 defrag_global_errors++;
1022 fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
1023 defrag_global_errors++;
1027 static int cmd_filesystem_defrag(int argc, char **argv)
1038 int compress_type = BTRFS_COMPRESS_NONE;
1041 defrag_global_errors = 0;
1042 defrag_global_verbose = 0;
1043 defrag_global_errors = 0;
1044 defrag_global_fancy_ioctl = 0;
1047 int c = getopt(argc, argv, "vrc::fs:l:t:");
1053 compress_type = BTRFS_COMPRESS_ZLIB;
1055 compress_type = parse_compress_type(optarg);
1056 defrag_global_fancy_ioctl = 1;
1060 defrag_global_fancy_ioctl = 1;
1063 defrag_global_verbose = 1;
1066 start = parse_size(optarg);
1067 defrag_global_fancy_ioctl = 1;
1070 len = parse_size(optarg);
1071 defrag_global_fancy_ioctl = 1;
1074 thresh = parse_size(optarg);
1075 if (thresh > (u32)-1) {
1077 "WARNING: target extent size %llu too big, trimmed to %u\n",
1081 defrag_global_fancy_ioctl = 1;
1087 usage(cmd_filesystem_defrag_usage);
1091 if (check_argc_min(argc - optind, 1))
1092 usage(cmd_filesystem_defrag_usage);
1094 memset(&defrag_global_range, 0, sizeof(defrag_global_range));
1095 defrag_global_range.start = start;
1096 defrag_global_range.len = len;
1097 defrag_global_range.extent_thresh = (u32)thresh;
1098 if (compress_type) {
1099 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
1100 defrag_global_range.compress_type = compress_type;
1103 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
1105 for (i = optind; i < argc; i++) {
1109 fd = open_file_or_dir(argv[i], &dirstream);
1111 fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
1113 defrag_global_errors++;
1114 close_file_or_dir(fd, dirstream);
1117 if (fstat(fd, &st)) {
1118 fprintf(stderr, "ERROR: failed to stat %s - %s\n",
1119 argv[i], strerror(errno));
1120 defrag_global_errors++;
1121 close_file_or_dir(fd, dirstream);
1124 if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
1126 "ERROR: %s is not a directory or a regular file\n",
1128 defrag_global_errors++;
1129 close_file_or_dir(fd, dirstream);
1133 if (S_ISDIR(st.st_mode)) {
1134 ret = nftw(argv[i], defrag_callback, 10,
1135 FTW_MOUNT | FTW_PHYS);
1138 /* errors are handled in the callback */
1141 if (defrag_global_verbose)
1142 printf("%s\n", argv[i]);
1143 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1144 &defrag_global_range);
1148 if (defrag_global_verbose)
1149 printf("%s\n", argv[i]);
1150 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1151 &defrag_global_range);
1154 close_file_or_dir(fd, dirstream);
1155 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1156 fprintf(stderr, "ERROR: defrag range ioctl not "
1157 "supported in this kernel, please try "
1158 "without any options.\n");
1159 defrag_global_errors++;
1163 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
1164 argv[i], strerror(e));
1165 defrag_global_errors++;
1168 if (defrag_global_verbose)
1169 printf("%s\n", PACKAGE_STRING);
1170 if (defrag_global_errors)
1171 fprintf(stderr, "total %d failures\n", defrag_global_errors);
1173 return !!defrag_global_errors;
1176 static const char * const cmd_filesystem_resize_usage[] = {
1177 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1178 "Resize a filesystem",
1179 "If 'max' is passed, the filesystem will occupy all available space",
1180 "on the device 'devid'.",
1181 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1185 static int cmd_filesystem_resize(int argc, char **argv)
1187 struct btrfs_ioctl_vol_args args;
1188 int fd, res, len, e;
1189 char *amount, *path;
1190 DIR *dirstream = NULL;
1193 if (check_argc_exact(argc, 3))
1194 usage(cmd_filesystem_resize_usage);
1199 len = strlen(amount);
1200 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
1201 fprintf(stderr, "ERROR: size value too long ('%s)\n",
1206 res = stat(path, &st);
1208 fprintf(stderr, "ERROR: resize: cannot stat %s: %s\n",
1209 path, strerror(errno));
1212 if (!S_ISDIR(st.st_mode)) {
1214 "ERROR: resize works on mounted filesystems and accepts only\n"
1215 "directories as argument. Passing file containing a btrfs image\n"
1216 "would resize the underlying filesystem instead of the image.\n");
1220 fd = open_file_or_dir(path, &dirstream);
1222 fprintf(stderr, "ERROR: can't access '%s'\n", path);
1226 printf("Resize '%s' of '%s'\n", path, amount);
1227 memset(&args, 0, sizeof(args));
1228 strncpy_null(args.name, amount);
1229 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
1231 close_file_or_dir(fd, dirstream);
1235 fprintf(stderr, "ERROR: unable to resize '%s' - no enouth free space\n",
1239 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
1244 } else if (res > 0) {
1245 const char *err_str = btrfs_err_str(res);
1248 fprintf(stderr, "ERROR: btrfs error resizing '%s' - %s\n",
1252 "ERROR: btrfs error resizing '%s' - unknown btrfs_err_code %d\n",
1260 static const char * const cmd_filesystem_label_usage[] = {
1261 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1262 "Get or change the label of a filesystem",
1263 "With one argument, get the label of filesystem on <device>.",
1264 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1268 static int cmd_filesystem_label(int argc, char **argv)
1270 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
1271 usage(cmd_filesystem_label_usage);
1274 return set_label(argv[1], argv[2]);
1276 char label[BTRFS_LABEL_SIZE];
1279 ret = get_label(argv[1], label);
1281 fprintf(stdout, "%s\n", label);
1287 static const char filesystem_cmd_group_info[] =
1288 "overall filesystem tasks and information";
1290 const struct cmd_group filesystem_cmd_group = {
1291 filesystem_cmd_group_usage, filesystem_cmd_group_info, {
1292 { "df", cmd_filesystem_df, cmd_filesystem_df_usage, NULL, 0 },
1293 { "show", cmd_filesystem_show, cmd_filesystem_show_usage, NULL,
1295 { "sync", cmd_filesystem_sync, cmd_filesystem_sync_usage, NULL,
1297 { "defragment", cmd_filesystem_defrag,
1298 cmd_filesystem_defrag_usage, NULL, 0 },
1299 { "balance", cmd_balance, NULL, &balance_cmd_group,
1301 { "resize", cmd_filesystem_resize, cmd_filesystem_resize_usage,
1303 { "label", cmd_filesystem_label, cmd_filesystem_label_usage,
1305 { "usage", cmd_filesystem_usage,
1306 cmd_filesystem_usage_usage, NULL, 0 },
1312 int cmd_filesystem(int argc, char **argv)
1314 return handle_command_group(&filesystem_cmd_group, argc, argv);