2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
17 #define _XOPEN_SOURCE 500
22 #include <sys/ioctl.h>
24 #include <uuid/uuid.h>
29 #include <linux/limits.h>
32 #include "kerncompat.h"
39 #include "list_sort.h"
44 * for btrfs fi show, we maintain a hash of fsids we've already printed.
45 * This way we don't print dups if a given FS is mounted more than once.
47 #define SEEN_FSID_HASH_SIZE 256
50 u8 fsid[BTRFS_FSID_SIZE];
51 struct seen_fsid *next;
54 static struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = {NULL,};
56 static int add_seen_fsid(u8 *fsid)
59 int slot = hash % SEEN_FSID_HASH_SIZE;
60 struct seen_fsid *seen = seen_fsid_hash[slot];
61 struct seen_fsid *alloc;
67 if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
78 alloc = malloc(sizeof(*alloc));
83 memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE);
88 seen_fsid_hash[slot] = alloc;
93 static void free_seen_fsid(void)
96 struct seen_fsid *seen;
97 struct seen_fsid *next;
99 for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) {
100 seen = seen_fsid_hash[slot];
106 seen_fsid_hash[slot] = NULL;
110 static const char * const filesystem_cmd_group_usage[] = {
111 "btrfs filesystem [<group>] <command> [<args>]",
115 static const char * const cmd_df_usage[] = {
116 "btrfs filesystem df <path>",
117 "Show space usage information for a mount point",
121 static char *group_type_str(u64 flag)
123 u64 mask = BTRFS_BLOCK_GROUP_TYPE_MASK |
124 BTRFS_SPACE_INFO_GLOBAL_RSV;
126 switch (flag & mask) {
127 case BTRFS_BLOCK_GROUP_DATA:
129 case BTRFS_BLOCK_GROUP_SYSTEM:
131 case BTRFS_BLOCK_GROUP_METADATA:
133 case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
134 return "Data+Metadata";
135 case BTRFS_SPACE_INFO_GLOBAL_RSV:
136 return "GlobalReserve";
142 static char *group_profile_str(u64 flag)
144 switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
147 case BTRFS_BLOCK_GROUP_RAID0:
149 case BTRFS_BLOCK_GROUP_RAID1:
151 case BTRFS_BLOCK_GROUP_RAID5:
153 case BTRFS_BLOCK_GROUP_RAID6:
155 case BTRFS_BLOCK_GROUP_DUP:
157 case BTRFS_BLOCK_GROUP_RAID10:
164 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
168 struct btrfs_ioctl_space_args *sargs;
170 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
174 sargs->space_slots = 0;
175 sargs->total_spaces = 0;
177 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
180 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
185 /* This really should never happen */
186 if (!sargs->total_spaces) {
190 count = sargs->total_spaces;
193 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
194 (count * sizeof(struct btrfs_ioctl_space_info)));
198 sargs->space_slots = count;
199 sargs->total_spaces = 0;
200 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
203 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
212 static void print_df(struct btrfs_ioctl_space_args *sargs)
215 struct btrfs_ioctl_space_info *sp = sargs->spaces;
217 for (i = 0; i < sargs->total_spaces; i++, sp++) {
218 printf("%s, %s: total=%s, used=%s\n",
219 group_type_str(sp->flags),
220 group_profile_str(sp->flags),
221 pretty_size(sp->total_bytes),
222 pretty_size(sp->used_bytes));
226 static int cmd_df(int argc, char **argv)
228 struct btrfs_ioctl_space_args *sargs = NULL;
232 DIR *dirstream = NULL;
234 if (check_argc_exact(argc, 2))
239 fd = open_file_or_dir(path, &dirstream);
241 fprintf(stderr, "ERROR: can't access '%s'\n", path);
244 ret = get_df(fd, &sargs);
250 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(-ret));
253 close_file_or_dir(fd, dirstream);
257 static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
260 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
261 int search_len = strlen(search);
263 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
264 uuid_unparse(fsid, uuidbuf);
265 if (!strncmp(uuidbuf, search, search_len))
268 if (strlen(label) && strcmp(label, search) == 0)
271 if (strcmp(mnt, search) == 0)
277 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
279 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
280 struct list_head *cur;
281 struct btrfs_device *device;
282 int search_len = strlen(search);
284 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
285 uuid_unparse(fs_devices->fsid, uuidbuf);
286 if (!strncmp(uuidbuf, search, search_len))
289 list_for_each(cur, &fs_devices->devices) {
290 device = list_entry(cur, struct btrfs_device, dev_list);
291 if ((device->label && strcmp(device->label, search) == 0) ||
292 strcmp(device->name, search) == 0)
299 * Sort devices by devid, ascending
301 static int cmp_device_id(void *priv, struct list_head *a,
304 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
306 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
309 return da->devid < db->devid ? -1 :
310 da->devid > db->devid ? 1 : 0;
313 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
315 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
316 struct list_head *cur;
317 struct btrfs_device *device;
321 if (add_seen_fsid(fs_devices->fsid))
324 uuid_unparse(fs_devices->fsid, uuidbuf);
325 device = list_entry(fs_devices->devices.next, struct btrfs_device,
327 if (device->label && device->label[0])
328 printf("Label: '%s' ", device->label);
330 printf("Label: none ");
333 total = device->total_devs;
334 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
335 (unsigned long long)total,
336 pretty_size(device->super_bytes_used));
338 list_sort(NULL, &fs_devices->devices, cmp_device_id);
339 list_for_each(cur, &fs_devices->devices) {
340 device = list_entry(cur, struct btrfs_device, dev_list);
342 printf("\tdevid %4llu size %s used %s path %s\n",
343 (unsigned long long)device->devid,
344 pretty_size(device->total_bytes),
345 pretty_size(device->bytes_used), device->name);
349 if (devs_found < total) {
350 printf("\t*** Some devices missing\n");
355 /* adds up all the used spaces as reported by the space info ioctl
357 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
361 for (i = 0; i < si->total_spaces; i++)
362 ret += si->spaces[i].used_bytes;
366 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
367 struct btrfs_ioctl_dev_info_args *dev_info,
368 struct btrfs_ioctl_space_args *space_info,
369 char *label, char *path)
374 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
375 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
378 ret = add_seen_fsid(fs_info->fsid);
384 uuid_unparse(fs_info->fsid, uuidbuf);
385 if (label && strlen(label))
386 printf("Label: '%s' ", label);
388 printf("Label: none ");
390 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
391 fs_info->num_devices,
392 pretty_size(calc_used_bytes(space_info)));
394 for (i = 0; i < fs_info->num_devices; i++) {
395 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
397 /* Add check for missing devices even mounted */
398 fd = open((char *)tmp_dev_info->path, O_RDONLY);
404 printf("\tdevid %4llu size %s used %s path %s\n",
406 pretty_size(tmp_dev_info->total_bytes),
407 pretty_size(tmp_dev_info->bytes_used),
412 printf("\t*** Some devices missing\n");
417 /* This function checks if the given input parameter is
419 * return -1: some error in the given input
420 * return 0: unknow input
421 * return 1: given input is uuid
422 * return 2: given input is path
424 static int check_arg_type(char *input)
432 if (realpath(input, path)) {
433 if (is_block_device(path) == 1)
434 return BTRFS_ARG_BLKDEV;
436 if (is_mount_point(path) == 1)
437 return BTRFS_ARG_MNTPOINT;
439 return BTRFS_ARG_UNKNOWN;
442 if (strlen(input) == (BTRFS_UUID_UNPARSED_SIZE - 1) &&
443 !uuid_parse(input, out))
444 return BTRFS_ARG_UUID;
446 return BTRFS_ARG_UNKNOWN;
449 static int btrfs_scan_kernel(void *search)
455 struct btrfs_ioctl_fs_info_args fs_info_arg;
456 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
457 struct btrfs_ioctl_space_args *space_info_arg = NULL;
458 char label[BTRFS_LABEL_SIZE];
460 f = setmntent("/proc/self/mounts", "r");
464 memset(label, 0, sizeof(label));
465 while ((mnt = getmntent(f)) != NULL) {
466 if (strcmp(mnt->mnt_type, "btrfs"))
468 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
473 if (get_label_mounted(mnt->mnt_dir, label)) {
477 if (search && !match_search_item_kernel(fs_info_arg.fsid,
478 mnt->mnt_dir, label, search)) {
483 fd = open(mnt->mnt_dir, O_RDONLY);
484 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
485 print_one_fs(&fs_info_arg, dev_info_arg,
486 space_info_arg, label, mnt->mnt_dir);
487 kfree(space_info_arg);
488 memset(label, 0, sizeof(label));
501 static int dev_to_fsid(char *dev, __u8 *fsid)
503 struct btrfs_super_block *disk_super;
512 fd = open(dev, O_RDONLY);
519 disk_super = (struct btrfs_super_block *)buf;
520 ret = btrfs_read_dev_super(fd, disk_super,
521 BTRFS_SUPER_INFO_OFFSET, 0);
525 memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
534 static const char * const cmd_show_usage[] = {
535 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
536 "Show the structure of a filesystem",
537 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
538 "-m|--mounted show only mounted btrfs",
539 "If no argument is given, structure of all present filesystems is shown.",
543 static int cmd_show(int argc, char **argv)
545 struct list_head *all_uuids;
546 struct btrfs_fs_devices *fs_devices;
547 struct list_head *cur_uuid;
550 /* default, search both kernel and udev */
553 char mp[BTRFS_PATH_NAME_MAX + 1];
555 __u8 fsid[BTRFS_FSID_SIZE];
556 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
561 static struct option long_options[] = {
562 { "all-devices", no_argument, NULL, 'd'},
563 { "mounted", no_argument, NULL, 'm'},
564 { NULL, no_argument, NULL, 0 },
566 int c = getopt_long(argc, argv, "dm", long_options,
572 where = BTRFS_SCAN_LBLKID;
575 where = BTRFS_SCAN_MOUNTED;
578 usage(cmd_show_usage);
582 if (check_argc_max(argc, optind + 1))
583 usage(cmd_show_usage);
586 search = argv[optind];
587 if (strlen(search) == 0)
588 usage(cmd_show_usage);
589 type = check_arg_type(search);
591 * needs spl handling if input arg is block dev
592 * And if input arg is mount-point just print it
595 if (type == BTRFS_ARG_BLKDEV) {
596 if (where == BTRFS_SCAN_LBLKID) {
597 /* we need to do this because
598 * legacy BTRFS_SCAN_DEV
599 * provides /dev/dm-x paths
601 if (realpath(search, path))
604 ret = get_btrfs_mount(search,
607 /* given block dev is mounted*/
609 type = BTRFS_ARG_MNTPOINT;
611 ret = dev_to_fsid(search, fsid);
614 "ERROR: No btrfs on %s\n",
618 uuid_unparse(fsid, uuid_buf);
620 type = BTRFS_ARG_UUID;
627 if (where == BTRFS_SCAN_LBLKID)
630 /* show mounted btrfs */
631 ret = btrfs_scan_kernel(search);
632 if (search && !ret) {
633 /* since search is found we are done */
637 /* shows mounted only */
638 if (where == BTRFS_SCAN_MOUNTED)
642 ret = btrfs_scan_lblkid(!BTRFS_UPDATE_KERNEL);
645 fprintf(stderr, "ERROR: %d while scanning\n", ret);
649 all_uuids = btrfs_scanned_uuids();
650 list_for_each(cur_uuid, all_uuids) {
651 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
653 if (search && uuid_search(fs_devices, search) == 0)
656 print_one_uuid(fs_devices);
659 if (search && !found)
662 while (!list_empty(all_uuids)) {
663 fs_devices = list_entry(all_uuids->next,
664 struct btrfs_fs_devices, list);
665 btrfs_close_devices(fs_devices);
668 printf("%s\n", BTRFS_BUILD_VERSION);
673 static const char * const cmd_sync_usage[] = {
674 "btrfs filesystem sync <path>",
675 "Force a sync on a filesystem",
679 static int cmd_sync(int argc, char **argv)
683 DIR *dirstream = NULL;
685 if (check_argc_exact(argc, 2))
686 usage(cmd_sync_usage);
690 fd = open_file_or_dir(path, &dirstream);
692 fprintf(stderr, "ERROR: can't access '%s'\n", path);
696 printf("FSSync '%s'\n", path);
697 res = ioctl(fd, BTRFS_IOC_SYNC);
699 close_file_or_dir(fd, dirstream);
701 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
709 static int parse_compress_type(char *s)
711 if (strcmp(optarg, "zlib") == 0)
712 return BTRFS_COMPRESS_ZLIB;
713 else if (strcmp(optarg, "lzo") == 0)
714 return BTRFS_COMPRESS_LZO;
716 fprintf(stderr, "Unknown compress type %s\n", s);
721 static const char * const cmd_defrag_usage[] = {
722 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
723 "Defragment a file or a directory",
726 "-r defragment files recursively",
727 "-c[zlib,lzo] compress the file while defragmenting",
728 "-f flush data to disk immediately after defragmenting",
729 "-s start defragment only from byte onward",
730 "-l len defragment only up to len bytes",
731 "-t size minimal size of file to be considered for defragmenting",
735 static int do_defrag(int fd, int fancy_ioctl,
736 struct btrfs_ioctl_defrag_range_args *range)
741 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
743 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
748 static int defrag_global_fancy_ioctl;
749 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
750 static int defrag_global_verbose;
751 static int defrag_global_errors;
752 static int defrag_callback(const char *fpath, const struct stat *sb,
753 int typeflag, struct FTW *ftwbuf)
759 if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
760 if (defrag_global_verbose)
761 printf("%s\n", fpath);
762 fd = open(fpath, O_RDWR);
766 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
769 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
770 fprintf(stderr, "ERROR: defrag range ioctl not "
771 "supported in this kernel, please try "
772 "without any options.\n");
773 defrag_global_errors++;
782 fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
783 defrag_global_errors++;
787 static int cmd_defrag(int argc, char **argv)
797 struct btrfs_ioctl_defrag_range_args range;
799 int compress_type = BTRFS_COMPRESS_NONE;
802 defrag_global_errors = 0;
803 defrag_global_verbose = 0;
804 defrag_global_errors = 0;
805 defrag_global_fancy_ioctl = 0;
808 int c = getopt(argc, argv, "vrc::fs:l:t:");
814 compress_type = BTRFS_COMPRESS_ZLIB;
816 compress_type = parse_compress_type(optarg);
817 defrag_global_fancy_ioctl = 1;
821 defrag_global_fancy_ioctl = 1;
824 defrag_global_verbose = 1;
827 start = parse_size(optarg);
828 defrag_global_fancy_ioctl = 1;
831 len = parse_size(optarg);
832 defrag_global_fancy_ioctl = 1;
835 thresh = parse_size(optarg);
836 defrag_global_fancy_ioctl = 1;
842 usage(cmd_defrag_usage);
846 if (check_argc_min(argc - optind, 1))
847 usage(cmd_defrag_usage);
849 memset(&defrag_global_range, 0, sizeof(range));
850 defrag_global_range.start = start;
851 defrag_global_range.len = len;
852 defrag_global_range.extent_thresh = thresh;
854 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
855 defrag_global_range.compress_type = compress_type;
858 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
860 for (i = optind; i < argc; i++) {
864 fd = open_file_or_dir(argv[i], &dirstream);
866 fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
868 defrag_global_errors++;
869 close_file_or_dir(fd, dirstream);
872 if (fstat(fd, &st)) {
873 fprintf(stderr, "ERROR: failed to stat %s - %s\n",
874 argv[i], strerror(errno));
875 defrag_global_errors++;
876 close_file_or_dir(fd, dirstream);
879 if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
881 "ERROR: %s is not a directory or a regular file\n",
883 defrag_global_errors++;
884 close_file_or_dir(fd, dirstream);
888 if (S_ISDIR(st.st_mode)) {
889 ret = nftw(argv[i], defrag_callback, 10,
890 FTW_MOUNT | FTW_PHYS);
893 /* errors are handled in the callback */
896 if (defrag_global_verbose)
897 printf("%s\n", argv[i]);
898 ret = do_defrag(fd, defrag_global_fancy_ioctl,
899 &defrag_global_range);
903 if (defrag_global_verbose)
904 printf("%s\n", argv[i]);
905 ret = do_defrag(fd, defrag_global_fancy_ioctl,
906 &defrag_global_range);
909 close_file_or_dir(fd, dirstream);
910 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
911 fprintf(stderr, "ERROR: defrag range ioctl not "
912 "supported in this kernel, please try "
913 "without any options.\n");
914 defrag_global_errors++;
918 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
919 argv[i], strerror(e));
920 defrag_global_errors++;
923 if (defrag_global_verbose)
924 printf("%s\n", BTRFS_BUILD_VERSION);
925 if (defrag_global_errors)
926 fprintf(stderr, "total %d failures\n", defrag_global_errors);
928 return !!defrag_global_errors;
931 static const char * const cmd_resize_usage[] = {
932 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
933 "Resize a filesystem",
934 "If 'max' is passed, the filesystem will occupy all available space",
935 "on the device 'devid'.",
936 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
940 static int cmd_resize(int argc, char **argv)
942 struct btrfs_ioctl_vol_args args;
945 DIR *dirstream = NULL;
947 if (check_argc_exact(argc, 3))
948 usage(cmd_resize_usage);
953 len = strlen(amount);
954 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
955 fprintf(stderr, "ERROR: size value too long ('%s)\n",
960 fd = open_file_or_dir(path, &dirstream);
962 fprintf(stderr, "ERROR: can't access '%s'\n", path);
966 printf("Resize '%s' of '%s'\n", path, amount);
967 strncpy_null(args.name, amount);
968 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
970 close_file_or_dir(fd, dirstream);
972 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
979 static const char * const cmd_label_usage[] = {
980 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
981 "Get or change the label of a filesystem",
982 "With one argument, get the label of filesystem on <device>.",
983 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
987 static int cmd_label(int argc, char **argv)
989 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
990 usage(cmd_label_usage);
993 return set_label(argv[1], argv[2]);
995 char label[BTRFS_LABEL_SIZE];
998 ret = get_label(argv[1], label);
1000 fprintf(stdout, "%s\n", label);
1006 const struct cmd_group filesystem_cmd_group = {
1007 filesystem_cmd_group_usage, NULL, {
1008 { "df", cmd_df, cmd_df_usage, NULL, 0 },
1009 { "show", cmd_show, cmd_show_usage, NULL, 0 },
1010 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
1011 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
1012 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
1013 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
1014 { "label", cmd_label, cmd_label_usage, NULL, 0 },
1019 int cmd_filesystem(int argc, char **argv)
1021 return handle_command_group(&filesystem_cmd_group, argc, argv);