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"
41 static const char * const filesystem_cmd_group_usage[] = {
42 "btrfs filesystem [<group>] <command> [<args>]",
46 static const char * const cmd_df_usage[] = {
47 "btrfs filesystem df <path>",
48 "Show space usage information for a mount point",
52 static char *group_type_str(u64 flag)
54 switch (flag & BTRFS_BLOCK_GROUP_TYPE_MASK) {
55 case BTRFS_BLOCK_GROUP_DATA:
57 case BTRFS_BLOCK_GROUP_SYSTEM:
59 case BTRFS_BLOCK_GROUP_METADATA:
61 case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
62 return "Data+Metadata";
68 static char *group_profile_str(u64 flag)
70 switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
73 case BTRFS_BLOCK_GROUP_RAID0:
75 case BTRFS_BLOCK_GROUP_RAID1:
77 case BTRFS_BLOCK_GROUP_RAID5:
79 case BTRFS_BLOCK_GROUP_RAID6:
81 case BTRFS_BLOCK_GROUP_DUP:
83 case BTRFS_BLOCK_GROUP_RAID10:
90 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
94 struct btrfs_ioctl_space_args *sargs;
96 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
100 sargs->space_slots = 0;
101 sargs->total_spaces = 0;
103 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
106 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
111 if (!sargs->total_spaces) {
115 count = sargs->total_spaces;
118 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
119 (count * sizeof(struct btrfs_ioctl_space_info)));
123 sargs->space_slots = count;
124 sargs->total_spaces = 0;
125 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
128 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
137 static void print_df(struct btrfs_ioctl_space_args *sargs)
140 struct btrfs_ioctl_space_info *sp = sargs->spaces;
142 for (i = 0; i < sargs->total_spaces; i++, sp++) {
143 printf("%s, %s: total=%s, used=%s\n",
144 group_type_str(sp->flags),
145 group_profile_str(sp->flags),
146 pretty_size(sp->total_bytes),
147 pretty_size(sp->used_bytes));
151 static int cmd_df(int argc, char **argv)
153 struct btrfs_ioctl_space_args *sargs = NULL;
157 DIR *dirstream = NULL;
159 if (check_argc_exact(argc, 2))
164 fd = open_file_or_dir(path, &dirstream);
166 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
169 ret = get_df(fd, &sargs);
175 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
178 close_file_or_dir(fd, dirstream);
182 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
185 struct list_head *cur;
186 struct btrfs_device *device;
187 int search_len = strlen(search);
189 search_len = min(search_len, 37);
190 uuid_unparse(fs_devices->fsid, uuidbuf);
191 if (!strncmp(uuidbuf, search, search_len))
194 list_for_each(cur, &fs_devices->devices) {
195 device = list_entry(cur, struct btrfs_device, dev_list);
196 if ((device->label && strcmp(device->label, search) == 0) ||
197 strcmp(device->name, search) == 0)
204 * Sort devices by devid, ascending
206 static int cmp_device_id(void *priv, struct list_head *a,
209 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
211 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
214 return da->devid < db->devid ? -1 :
215 da->devid > db->devid ? 1 : 0;
218 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
221 struct list_head *cur;
222 struct btrfs_device *device;
226 uuid_unparse(fs_devices->fsid, uuidbuf);
227 device = list_entry(fs_devices->devices.next, struct btrfs_device,
229 if (device->label && device->label[0])
230 printf("Label: '%s' ", device->label);
232 printf("Label: none ");
235 total = device->total_devs;
236 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
237 (unsigned long long)total,
238 pretty_size(device->super_bytes_used));
240 list_sort(NULL, &fs_devices->devices, cmp_device_id);
241 list_for_each(cur, &fs_devices->devices) {
242 device = list_entry(cur, struct btrfs_device, dev_list);
244 printf("\tdevid %4llu size %s used %s path %s\n",
245 (unsigned long long)device->devid,
246 pretty_size(device->total_bytes),
247 pretty_size(device->bytes_used), device->name);
251 if (devs_found < total) {
252 printf("\t*** Some devices missing\n");
257 /* adds up all the used spaces as reported by the space info ioctl
259 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
263 for (i = 0; i < si->total_spaces; i++)
264 ret += si->spaces[i].used_bytes;
268 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
269 struct btrfs_ioctl_dev_info_args *dev_info,
270 struct btrfs_ioctl_space_args *space_info,
271 char *label, char *path)
275 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
277 uuid_unparse(fs_info->fsid, uuidbuf);
278 printf("Label: %s uuid: %s\n",
279 strlen(label) ? label : "none", uuidbuf);
281 printf("\tTotal devices %llu FS bytes used %s\n",
282 fs_info->num_devices,
283 pretty_size(calc_used_bytes(space_info)));
285 for (i = 0; i < fs_info->num_devices; i++) {
286 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
287 printf("\tdevid %llu size %s used %s path %s\n",
289 pretty_size(tmp_dev_info->total_bytes),
290 pretty_size(tmp_dev_info->bytes_used),
298 /* This function checks if the given input parameter is
300 * return -1: some error in the given input
301 * return 0: unknow input
302 * return 1: given input is uuid
303 * return 2: given input is path
305 static int check_arg_type(char *input)
311 return BTRFS_ARG_UNKNOWN;
313 if (realpath(input, path))
314 return BTRFS_ARG_PATH;
316 if (!uuid_parse(input, out))
317 return BTRFS_ARG_UUID;
319 return BTRFS_ARG_UNKNOWN;
322 static int btrfs_scan_kernel(void *search)
324 int ret = 0, fd, type;
327 struct btrfs_ioctl_fs_info_args fs_info_arg;
328 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
329 struct btrfs_ioctl_space_args *space_info_arg;
330 char label[BTRFS_LABEL_SIZE];
333 f = setmntent("/proc/self/mounts", "r");
337 type = check_arg_type(search);
339 while ((mnt = getmntent(f)) != NULL) {
340 if (strcmp(mnt->mnt_type, "btrfs"))
342 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
349 ret = uuid_parse(search, uuid);
352 if (uuid_compare(fs_info_arg.fsid, uuid))
356 if (strcmp(search, mnt->mnt_dir))
363 fd = open(mnt->mnt_dir, O_RDONLY);
364 if (fd > 0 && !get_df(fd, &space_info_arg)) {
365 get_label_mounted(mnt->mnt_dir, label);
366 print_one_fs(&fs_info_arg, dev_info_arg,
367 space_info_arg, label, mnt->mnt_dir);
368 free(space_info_arg);
377 static const char * const cmd_show_usage[] = {
378 "btrfs filesystem show [options] [<path>|<uuid>]",
379 "Show the structure of a filesystem",
380 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
381 "-m|--mounted show only mounted btrfs",
382 "If no argument is given, structure of all present filesystems is shown.",
386 static int cmd_show(int argc, char **argv)
388 struct list_head *all_uuids;
389 struct btrfs_fs_devices *fs_devices;
390 struct list_head *cur_uuid;
393 int where = BTRFS_SCAN_LBLKID;
398 static struct option long_options[] = {
399 { "all-devices", no_argument, NULL, 'd'},
400 { "mounted", no_argument, NULL, 'm'},
401 { NULL, no_argument, NULL, 0 },
403 int c = getopt_long(argc, argv, "dm", long_options,
409 where = BTRFS_SCAN_DEV;
412 where = BTRFS_SCAN_MOUNTED;
415 usage(cmd_show_usage);
419 if (check_argc_max(argc, optind + 1))
420 usage(cmd_show_usage);
422 search = argv[optind];
423 type = check_arg_type(search);
424 if (type == BTRFS_ARG_UNKNOWN) {
425 fprintf(stderr, "ERROR: arg type unknown\n");
426 usage(cmd_show_usage);
430 if (where == BTRFS_SCAN_DEV)
433 /* show mounted btrfs */
434 btrfs_scan_kernel(search);
436 /* shows mounted only */
437 if (where == BTRFS_SCAN_MOUNTED)
441 ret = scan_for_btrfs(where, !BTRFS_UPDATE_KERNEL);
444 fprintf(stderr, "ERROR: %d while scanning\n", ret);
448 all_uuids = btrfs_scanned_uuids();
449 list_for_each(cur_uuid, all_uuids) {
450 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
452 if (search && uuid_search(fs_devices, search) == 0)
455 print_one_uuid(fs_devices);
459 printf("%s\n", BTRFS_BUILD_VERSION);
463 static const char * const cmd_sync_usage[] = {
464 "btrfs filesystem sync <path>",
465 "Force a sync on a filesystem",
469 static int cmd_sync(int argc, char **argv)
473 DIR *dirstream = NULL;
475 if (check_argc_exact(argc, 2))
476 usage(cmd_sync_usage);
480 fd = open_file_or_dir(path, &dirstream);
482 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
486 printf("FSSync '%s'\n", path);
487 res = ioctl(fd, BTRFS_IOC_SYNC);
489 close_file_or_dir(fd, dirstream);
491 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
499 static int parse_compress_type(char *s)
501 if (strcmp(optarg, "zlib") == 0)
502 return BTRFS_COMPRESS_ZLIB;
503 else if (strcmp(optarg, "lzo") == 0)
504 return BTRFS_COMPRESS_LZO;
506 fprintf(stderr, "Unknown compress type %s\n", s);
511 static const char * const cmd_defrag_usage[] = {
512 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
513 "Defragment a file or a directory",
516 "-r defragment files recursively",
517 "-c[zlib,lzo] compress the file while defragmenting",
518 "-f flush data to disk immediately after defragmenting",
519 "-s start defragment only from byte onward",
520 "-l len defragment only up to len bytes",
521 "-t size minimal size of file to be considered for defragmenting",
525 static int do_defrag(int fd, int fancy_ioctl,
526 struct btrfs_ioctl_defrag_range_args *range)
531 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
533 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
538 static int defrag_global_fancy_ioctl;
539 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
540 static int defrag_global_verbose;
541 static int defrag_global_errors;
542 static int defrag_callback(const char *fpath, const struct stat *sb,
543 int typeflag, struct FTW *ftwbuf)
549 if (typeflag == FTW_F) {
550 if (defrag_global_verbose)
551 printf("%s\n", fpath);
552 fd = open(fpath, O_RDWR);
556 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
559 if (ret && e == ENOTTY) {
560 fprintf(stderr, "ERROR: defrag range ioctl not "
561 "supported in this kernel, please try "
562 "without any options.\n");
563 defrag_global_errors++;
572 fprintf(stderr, "ERROR: defrag failed on %s - %s\n", fpath, strerror(e));
573 defrag_global_errors++;
577 static int cmd_defrag(int argc, char **argv)
587 struct btrfs_ioctl_defrag_range_args range;
589 int compress_type = BTRFS_COMPRESS_NONE;
592 defrag_global_errors = 0;
593 defrag_global_verbose = 0;
594 defrag_global_errors = 0;
595 defrag_global_fancy_ioctl = 0;
598 int c = getopt(argc, argv, "vrc::fs:l:t:");
604 compress_type = BTRFS_COMPRESS_ZLIB;
606 compress_type = parse_compress_type(optarg);
607 defrag_global_fancy_ioctl = 1;
611 defrag_global_fancy_ioctl = 1;
614 defrag_global_verbose = 1;
617 start = parse_size(optarg);
618 defrag_global_fancy_ioctl = 1;
621 len = parse_size(optarg);
622 defrag_global_fancy_ioctl = 1;
625 thresh = parse_size(optarg);
626 defrag_global_fancy_ioctl = 1;
632 usage(cmd_defrag_usage);
636 if (check_argc_min(argc - optind, 1))
637 usage(cmd_defrag_usage);
639 memset(&defrag_global_range, 0, sizeof(range));
640 defrag_global_range.start = start;
641 defrag_global_range.len = len;
642 defrag_global_range.extent_thresh = thresh;
644 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
645 defrag_global_range.compress_type = compress_type;
648 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
650 for (i = optind; i < argc; i++) {
652 fd = open_file_or_dir(argv[i], &dirstream);
654 fprintf(stderr, "ERROR: failed to open %s - %s\n", argv[i],
656 defrag_global_errors++;
657 close_file_or_dir(fd, dirstream);
664 if (S_ISDIR(st.st_mode)) {
665 ret = nftw(argv[i], defrag_callback, 10,
666 FTW_MOUNT | FTW_PHYS);
669 /* errors are handled in the callback */
672 if (defrag_global_verbose)
673 printf("%s\n", argv[i]);
674 ret = do_defrag(fd, defrag_global_fancy_ioctl,
675 &defrag_global_range);
679 if (defrag_global_verbose)
680 printf("%s\n", argv[i]);
681 ret = do_defrag(fd, defrag_global_fancy_ioctl,
682 &defrag_global_range);
685 close_file_or_dir(fd, dirstream);
686 if (ret && e == ENOTTY) {
687 fprintf(stderr, "ERROR: defrag range ioctl not "
688 "supported in this kernel, please try "
689 "without any options.\n");
690 defrag_global_errors++;
694 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
695 argv[i], strerror(e));
696 defrag_global_errors++;
699 if (defrag_global_verbose)
700 printf("%s\n", BTRFS_BUILD_VERSION);
701 if (defrag_global_errors)
702 fprintf(stderr, "total %d failures\n", defrag_global_errors);
704 return !!defrag_global_errors;
707 static const char * const cmd_resize_usage[] = {
708 "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
709 "Resize a filesystem",
710 "If 'max' is passed, the filesystem will occupy all available space",
711 "on the device 'devid'.",
715 static int cmd_resize(int argc, char **argv)
717 struct btrfs_ioctl_vol_args args;
720 DIR *dirstream = NULL;
722 if (check_argc_exact(argc, 3))
723 usage(cmd_resize_usage);
728 len = strlen(amount);
729 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
730 fprintf(stderr, "ERROR: size value too long ('%s)\n",
735 fd = open_file_or_dir(path, &dirstream);
737 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
741 printf("Resize '%s' of '%s'\n", path, amount);
742 strncpy_null(args.name, amount);
743 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
745 close_file_or_dir(fd, dirstream);
747 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
754 static const char * const cmd_label_usage[] = {
755 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
756 "Get or change the label of a filesystem",
757 "With one argument, get the label of filesystem on <device>.",
758 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
762 static int cmd_label(int argc, char **argv)
764 if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
765 usage(cmd_label_usage);
768 return set_label(argv[1], argv[2]);
770 return get_label(argv[1]);
773 const struct cmd_group filesystem_cmd_group = {
774 filesystem_cmd_group_usage, NULL, {
775 { "df", cmd_df, cmd_df_usage, NULL, 0 },
776 { "show", cmd_show, cmd_show_usage, NULL, 0 },
777 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
778 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
779 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
780 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
781 { "label", cmd_label, cmd_label_usage, NULL, 0 },
786 int cmd_filesystem(int argc, char **argv)
788 return handle_command_group(&filesystem_cmd_group, argc, argv);