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"
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 add_seen_fsid(u8 *fsid)
58 int slot = hash % SEEN_FSID_HASH_SIZE;
59 struct seen_fsid *seen = seen_fsid_hash[slot];
60 struct seen_fsid *alloc;
66 if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
77 alloc = malloc(sizeof(*alloc));
82 memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE);
87 seen_fsid_hash[slot] = alloc;
92 static void free_seen_fsid(void)
95 struct seen_fsid *seen;
96 struct seen_fsid *next;
98 for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) {
99 seen = seen_fsid_hash[slot];
105 seen_fsid_hash[slot] = NULL;
109 static const char * const filesystem_cmd_group_usage[] = {
110 "btrfs filesystem [<group>] <command> [<args>]",
114 static const char * const cmd_df_usage[] = {
115 "btrfs filesystem df <path>",
116 "Show space usage information for a mount point",
120 static char *group_type_str(u64 flag)
122 switch (flag & BTRFS_BLOCK_GROUP_TYPE_MASK) {
123 case BTRFS_BLOCK_GROUP_DATA:
125 case BTRFS_BLOCK_GROUP_SYSTEM:
127 case BTRFS_BLOCK_GROUP_METADATA:
129 case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
130 return "Data+Metadata";
136 static char *group_profile_str(u64 flag)
138 switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
141 case BTRFS_BLOCK_GROUP_RAID0:
143 case BTRFS_BLOCK_GROUP_RAID1:
145 case BTRFS_BLOCK_GROUP_RAID5:
147 case BTRFS_BLOCK_GROUP_RAID6:
149 case BTRFS_BLOCK_GROUP_DUP:
151 case BTRFS_BLOCK_GROUP_RAID10:
158 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
162 struct btrfs_ioctl_space_args *sargs;
164 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
168 sargs->space_slots = 0;
169 sargs->total_spaces = 0;
171 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
174 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
179 /* This really should never happen */
180 if (!sargs->total_spaces) {
184 count = sargs->total_spaces;
187 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
188 (count * sizeof(struct btrfs_ioctl_space_info)));
192 sargs->space_slots = count;
193 sargs->total_spaces = 0;
194 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
197 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
206 static void print_df(struct btrfs_ioctl_space_args *sargs)
209 struct btrfs_ioctl_space_info *sp = sargs->spaces;
211 for (i = 0; i < sargs->total_spaces; i++, sp++) {
212 printf("%s, %s: total=%s, used=%s\n",
213 group_type_str(sp->flags),
214 group_profile_str(sp->flags),
215 pretty_size(sp->total_bytes),
216 pretty_size(sp->used_bytes));
220 static int cmd_df(int argc, char **argv)
222 struct btrfs_ioctl_space_args *sargs = NULL;
226 DIR *dirstream = NULL;
228 if (check_argc_exact(argc, 2))
233 fd = open_file_or_dir(path, &dirstream);
235 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
238 ret = get_df(fd, &sargs);
244 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(-ret));
247 close_file_or_dir(fd, dirstream);
251 static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
255 int search_len = strlen(search);
257 search_len = min(search_len, 37);
258 uuid_unparse(fsid, uuidbuf);
259 if (!strncmp(uuidbuf, search, search_len))
262 if (strlen(label) && strcmp(label, search) == 0)
265 if (strcmp(mnt, search) == 0)
271 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
274 struct list_head *cur;
275 struct btrfs_device *device;
276 int search_len = strlen(search);
278 search_len = min(search_len, 37);
279 uuid_unparse(fs_devices->fsid, uuidbuf);
280 if (!strncmp(uuidbuf, search, search_len))
283 list_for_each(cur, &fs_devices->devices) {
284 device = list_entry(cur, struct btrfs_device, dev_list);
285 if ((device->label && strcmp(device->label, search) == 0) ||
286 strcmp(device->name, search) == 0)
293 * Sort devices by devid, ascending
295 static int cmp_device_id(void *priv, struct list_head *a,
298 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
300 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
303 return da->devid < db->devid ? -1 :
304 da->devid > db->devid ? 1 : 0;
307 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
310 struct list_head *cur;
311 struct btrfs_device *device;
315 if (add_seen_fsid(fs_devices->fsid))
318 uuid_unparse(fs_devices->fsid, uuidbuf);
319 device = list_entry(fs_devices->devices.next, struct btrfs_device,
321 if (device->label && device->label[0])
322 printf("Label: '%s' ", device->label);
324 printf("Label: none ");
327 total = device->total_devs;
328 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
329 (unsigned long long)total,
330 pretty_size(device->super_bytes_used));
332 list_sort(NULL, &fs_devices->devices, cmp_device_id);
333 list_for_each(cur, &fs_devices->devices) {
334 device = list_entry(cur, struct btrfs_device, dev_list);
336 printf("\tdevid %4llu size %s used %s path %s\n",
337 (unsigned long long)device->devid,
338 pretty_size(device->total_bytes),
339 pretty_size(device->bytes_used), device->name);
343 if (devs_found < total) {
344 printf("\t*** Some devices missing\n");
349 /* adds up all the used spaces as reported by the space info ioctl
351 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
355 for (i = 0; i < si->total_spaces; i++)
356 ret += si->spaces[i].used_bytes;
360 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
361 struct btrfs_ioctl_dev_info_args *dev_info,
362 struct btrfs_ioctl_space_args *space_info,
363 char *label, char *path)
367 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
370 ret = add_seen_fsid(fs_info->fsid);
376 uuid_unparse(fs_info->fsid, uuidbuf);
377 if (label && strlen(label))
378 printf("Label: '%s' ", label);
380 printf("Label: none ");
382 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
383 fs_info->num_devices,
384 pretty_size(calc_used_bytes(space_info)));
386 for (i = 0; i < fs_info->num_devices; i++) {
387 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
388 printf("\tdevid %4llu size %s used %s path %s\n",
390 pretty_size(tmp_dev_info->total_bytes),
391 pretty_size(tmp_dev_info->bytes_used),
399 /* This function checks if the given input parameter is
401 * return -1: some error in the given input
402 * return 0: unknow input
403 * return 1: given input is uuid
404 * return 2: given input is path
406 static int check_arg_type(char *input)
414 if (realpath(input, path)) {
415 if (is_block_device(input) == 1)
416 return BTRFS_ARG_BLKDEV;
418 if (is_mount_point(input) == 1)
419 return BTRFS_ARG_MNTPOINT;
421 return BTRFS_ARG_UNKNOWN;
424 if (strlen(input) == 36 && !uuid_parse(input, out))
425 return BTRFS_ARG_UUID;
427 return BTRFS_ARG_UNKNOWN;
430 static int btrfs_scan_kernel(void *search)
435 struct btrfs_ioctl_fs_info_args fs_info_arg;
436 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
437 struct btrfs_ioctl_space_args *space_info_arg;
438 char label[BTRFS_LABEL_SIZE];
440 f = setmntent("/proc/self/mounts", "r");
444 memset(label, 0, sizeof(label));
445 while ((mnt = getmntent(f)) != NULL) {
446 if (strcmp(mnt->mnt_type, "btrfs"))
448 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
453 if (get_label_mounted(mnt->mnt_dir, label)) {
457 if (search && !match_search_item_kernel(fs_info_arg.fsid,
458 mnt->mnt_dir, label, search)) {
463 fd = open(mnt->mnt_dir, O_RDONLY);
464 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
465 print_one_fs(&fs_info_arg, dev_info_arg,
466 space_info_arg, label, mnt->mnt_dir);
467 kfree(space_info_arg);
468 memset(label, 0, sizeof(label));
481 static const char * const cmd_show_usage[] = {
482 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
483 "Show the structure of a filesystem",
484 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
485 "-m|--mounted show only mounted btrfs",
486 "If no argument is given, structure of all present filesystems is shown.",
490 static int cmd_show(int argc, char **argv)
492 struct list_head *all_uuids;
493 struct btrfs_fs_devices *fs_devices;
494 struct list_head *cur_uuid;
497 int where = BTRFS_SCAN_LBLKID;
499 char mp[BTRFS_PATH_NAME_MAX + 1];
504 static struct option long_options[] = {
505 { "all-devices", no_argument, NULL, 'd'},
506 { "mounted", no_argument, NULL, 'm'},
507 { NULL, no_argument, NULL, 0 },
509 int c = getopt_long(argc, argv, "dm", long_options,
515 where = BTRFS_SCAN_DEV;
518 where = BTRFS_SCAN_MOUNTED;
521 usage(cmd_show_usage);
525 if (check_argc_max(argc, optind + 1))
526 usage(cmd_show_usage);
529 search = argv[optind];
530 if (strlen(search) == 0)
531 usage(cmd_show_usage);
532 type = check_arg_type(search);
533 if (type == BTRFS_ARG_BLKDEV) {
534 if (where == BTRFS_SCAN_DEV) {
535 /* we need to do this because
536 * legacy BTRFS_SCAN_DEV
537 * provides /dev/dm-x paths
539 if (realpath(search, path))
542 ret = get_btrfs_mount(search,
545 /* given block dev is mounted*/
553 if (where == BTRFS_SCAN_DEV)
556 /* show mounted btrfs */
557 ret = btrfs_scan_kernel(search);
561 /* shows mounted only */
562 if (where == BTRFS_SCAN_MOUNTED)
566 ret = scan_for_btrfs(where, !BTRFS_UPDATE_KERNEL);
569 fprintf(stderr, "ERROR: %d while scanning\n", ret);
573 all_uuids = btrfs_scanned_uuids();
574 list_for_each(cur_uuid, all_uuids) {
575 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
577 if (search && uuid_search(fs_devices, search) == 0)
580 print_one_uuid(fs_devices);
584 printf("%s\n", BTRFS_BUILD_VERSION);
589 static const char * const cmd_sync_usage[] = {
590 "btrfs filesystem sync <path>",
591 "Force a sync on a filesystem",
595 static int cmd_sync(int argc, char **argv)
599 DIR *dirstream = NULL;
601 if (check_argc_exact(argc, 2))
602 usage(cmd_sync_usage);
606 fd = open_file_or_dir(path, &dirstream);
608 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
612 printf("FSSync '%s'\n", path);
613 res = ioctl(fd, BTRFS_IOC_SYNC);
615 close_file_or_dir(fd, dirstream);
617 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
625 static int parse_compress_type(char *s)
627 if (strcmp(optarg, "zlib") == 0)
628 return BTRFS_COMPRESS_ZLIB;
629 else if (strcmp(optarg, "lzo") == 0)
630 return BTRFS_COMPRESS_LZO;
632 fprintf(stderr, "Unknown compress type %s\n", s);
637 static const char * const cmd_defrag_usage[] = {
638 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
639 "Defragment a file or a directory",
642 "-r defragment files recursively",
643 "-c[zlib,lzo] compress the file while defragmenting",
644 "-f flush data to disk immediately after defragmenting",
645 "-s start defragment only from byte onward",
646 "-l len defragment only up to len bytes",
647 "-t size minimal size of file to be considered for defragmenting",
651 static int do_defrag(int fd, int fancy_ioctl,
652 struct btrfs_ioctl_defrag_range_args *range)
657 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
659 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
664 static int defrag_global_fancy_ioctl;
665 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
666 static int defrag_global_verbose;
667 static int defrag_global_errors;
668 static int defrag_callback(const char *fpath, const struct stat *sb,
669 int typeflag, struct FTW *ftwbuf)
675 if (typeflag == FTW_F) {
676 if (defrag_global_verbose)
677 printf("%s\n", fpath);
678 fd = open(fpath, O_RDWR);
682 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
685 if (ret && e == ENOTTY) {
686 fprintf(stderr, "ERROR: defrag range ioctl not "
687 "supported in this kernel, please try "
688 "without any options.\n");
689 defrag_global_errors++;
698 fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
699 defrag_global_errors++;
703 static int cmd_defrag(int argc, char **argv)
713 struct btrfs_ioctl_defrag_range_args range;
715 int compress_type = BTRFS_COMPRESS_NONE;
718 defrag_global_errors = 0;
719 defrag_global_verbose = 0;
720 defrag_global_errors = 0;
721 defrag_global_fancy_ioctl = 0;
724 int c = getopt(argc, argv, "vrc::fs:l:t:");
730 compress_type = BTRFS_COMPRESS_ZLIB;
732 compress_type = parse_compress_type(optarg);
733 defrag_global_fancy_ioctl = 1;
737 defrag_global_fancy_ioctl = 1;
740 defrag_global_verbose = 1;
743 start = parse_size(optarg);
744 defrag_global_fancy_ioctl = 1;
747 len = parse_size(optarg);
748 defrag_global_fancy_ioctl = 1;
751 thresh = parse_size(optarg);
752 defrag_global_fancy_ioctl = 1;
758 usage(cmd_defrag_usage);
762 if (check_argc_min(argc - optind, 1))
763 usage(cmd_defrag_usage);
765 memset(&defrag_global_range, 0, sizeof(range));
766 defrag_global_range.start = start;
767 defrag_global_range.len = len;
768 defrag_global_range.extent_thresh = thresh;
770 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
771 defrag_global_range.compress_type = compress_type;
774 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
776 for (i = optind; i < argc; i++) {
778 fd = open_file_or_dir(argv[i], &dirstream);
780 fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
782 defrag_global_errors++;
783 close_file_or_dir(fd, dirstream);
789 if (fstat(fd, &st)) {
790 fprintf(stderr, "ERROR: failed to stat %s - %s\n",
791 argv[i], strerror(errno));
792 defrag_global_errors++;
793 close_file_or_dir(fd, dirstream);
796 if (S_ISDIR(st.st_mode)) {
797 ret = nftw(argv[i], defrag_callback, 10,
798 FTW_MOUNT | FTW_PHYS);
801 /* errors are handled in the callback */
804 if (defrag_global_verbose)
805 printf("%s\n", argv[i]);
806 ret = do_defrag(fd, defrag_global_fancy_ioctl,
807 &defrag_global_range);
811 if (defrag_global_verbose)
812 printf("%s\n", argv[i]);
813 ret = do_defrag(fd, defrag_global_fancy_ioctl,
814 &defrag_global_range);
817 close_file_or_dir(fd, dirstream);
818 if (ret && e == ENOTTY) {
819 fprintf(stderr, "ERROR: defrag range ioctl not "
820 "supported in this kernel, please try "
821 "without any options.\n");
822 defrag_global_errors++;
826 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
827 argv[i], strerror(e));
828 defrag_global_errors++;
831 if (defrag_global_verbose)
832 printf("%s\n", BTRFS_BUILD_VERSION);
833 if (defrag_global_errors)
834 fprintf(stderr, "total %d failures\n", defrag_global_errors);
836 return !!defrag_global_errors;
839 static const char * const cmd_resize_usage[] = {
840 "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
841 "Resize a filesystem",
842 "If 'max' is passed, the filesystem will occupy all available space",
843 "on the device 'devid'.",
847 static int cmd_resize(int argc, char **argv)
849 struct btrfs_ioctl_vol_args args;
852 DIR *dirstream = NULL;
854 if (check_argc_exact(argc, 3))
855 usage(cmd_resize_usage);
860 len = strlen(amount);
861 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
862 fprintf(stderr, "ERROR: size value too long ('%s)\n",
867 fd = open_file_or_dir(path, &dirstream);
869 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
873 printf("Resize '%s' of '%s'\n", path, amount);
874 strncpy_null(args.name, amount);
875 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
877 close_file_or_dir(fd, dirstream);
879 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
886 static const char * const cmd_label_usage[] = {
887 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
888 "Get or change the label of a filesystem",
889 "With one argument, get the label of filesystem on <device>.",
890 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
894 static int cmd_label(int argc, char **argv)
896 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
897 usage(cmd_label_usage);
900 return set_label(argv[1], argv[2]);
902 char label[BTRFS_LABEL_SIZE];
905 ret = get_label(argv[1], label);
907 fprintf(stdout, "%s\n", label);
913 const struct cmd_group filesystem_cmd_group = {
914 filesystem_cmd_group_usage, NULL, {
915 { "df", cmd_df, cmd_df_usage, NULL, 0 },
916 { "show", cmd_show, cmd_show_usage, NULL, 0 },
917 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
918 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
919 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
920 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
921 { "label", cmd_label, cmd_label_usage, NULL, 0 },
926 int cmd_filesystem(int argc, char **argv)
928 return handle_command_group(&filesystem_cmd_group, argc, argv);