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>
22 #include <sys/types.h>
29 #include <uuid/uuid.h>
34 #include "kerncompat.h"
36 #include "transaction.h"
42 #include "btrfs_cmds.h"
43 #include "btrfslabel.h"
46 #define BLKGETSIZE64 0
47 #define BTRFS_IOC_SNAP_CREATE_V2 0
48 #define BTRFS_VOL_NAME_MAX 255
49 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
50 static inline int ioctl(int fd, int define, void *arg) { return 0; }
54 * test if path is a subvolume:
55 * this function return
56 * 0-> path exists but it is not a subvolume
57 * 1-> path exists and it is a subvolume
58 * -1 -> path is unaccessible
60 static int test_issubvolume(char *path)
66 res = stat(path, &st);
70 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
75 * test if path is a directory
76 * this function return
77 * 0-> path exists but it is not a directory
78 * 1-> path exists and it is a directory
79 * -1 -> path is unaccessible
81 static int test_isdir(char *path)
86 res = stat(path, &st);
90 return S_ISDIR(st.st_mode);
94 static int open_file_or_dir(const char *fname)
101 ret = stat(fname, &st);
105 if (S_ISDIR(st.st_mode)) {
106 dirstream = opendir(fname);
110 fd = dirfd(dirstream);
112 fd = open(fname, O_RDWR);
120 static u64 parse_size(char *s)
126 if (!isdigit(s[len - 1])) {
127 c = tolower(s[len - 1]);
138 fprintf(stderr, "Unknown size descriptor %c\n", c);
143 return atoll(s) * mult;
146 static int parse_compress_type(char *s)
148 if (strcmp(optarg, "zlib") == 0)
149 return BTRFS_COMPRESS_ZLIB;
150 else if (strcmp(optarg, "lzo") == 0)
151 return BTRFS_COMPRESS_LZO;
153 fprintf(stderr, "Unknown compress type %s\n", s);
158 int do_defrag(int ac, char **av)
170 struct btrfs_ioctl_defrag_range_args range;
172 int compress_type = BTRFS_COMPRESS_NONE;
176 int c = getopt(ac, av, "vc::fs:l:t:");
181 compress_type = BTRFS_COMPRESS_ZLIB;
183 compress_type = parse_compress_type(optarg);
194 start = parse_size(optarg);
198 len = parse_size(optarg);
202 thresh = parse_size(optarg);
206 fprintf(stderr, "Invalid arguments for defragment\n");
211 if (ac - optind == 0) {
212 fprintf(stderr, "Invalid arguments for defragment\n");
217 memset(&range, 0, sizeof(range));
220 range.extent_thresh = thresh;
222 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
223 range.compress_type = compress_type;
226 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
228 for (i = optind; i < ac; i++) {
230 printf("%s\n", av[i]);
231 fd = open_file_or_dir(av[i]);
233 fprintf(stderr, "failed to open %s\n", av[i]);
239 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
242 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
243 if (ret && errno == ENOTTY) {
244 fprintf(stderr, "ERROR: defrag range ioctl not "
245 "supported in this kernel, please try "
246 "without any options.\n");
253 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
260 printf("%s\n", BTRFS_BUILD_VERSION);
262 fprintf(stderr, "total %d failures\n", errors);
270 int do_find_newer(int argc, char **argv)
278 last_gen = atoll(argv[2]);
280 ret = test_issubvolume(subvol);
282 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
286 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
290 fd = open_file_or_dir(subvol);
292 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
295 ret = find_updated_files(fd, 0, last_gen);
301 int do_subvol_list(int argc, char **argv)
309 ret = test_issubvolume(subvol);
311 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
315 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
319 fd = open_file_or_dir(subvol);
321 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
324 ret = list_subvols(fd);
330 int do_clone(int argc, char **argv)
333 int res, fd, fddst, len, e, optind = 0, readonly = 0;
336 struct btrfs_ioctl_vol_args_v2 args;
338 memset(&args, 0, sizeof(args));
341 int c = getopt(argc, argv, "r");
352 "Invalid arguments for subvolume snapshot\n");
357 if (argc - optind < 2) {
358 fprintf(stderr, "Invalid arguments for subvolume snapshot\n");
363 subvol = argv[optind+1];
364 dst = argv[optind+2];
366 res = test_issubvolume(subvol);
368 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
372 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
376 res = test_isdir(dst);
378 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
383 newname = strdup(subvol);
384 newname = basename(newname);
387 newname = strdup(dst);
388 newname = basename(newname);
389 dstdir = strdup(dst);
390 dstdir = dirname(dstdir);
393 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
394 strchr(newname, '/') ){
395 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
400 len = strlen(newname);
401 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
402 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
407 fddst = open_file_or_dir(dstdir);
409 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
413 fd = open_file_or_dir(subvol);
416 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
421 args.flags |= BTRFS_SUBVOL_RDONLY;
422 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
423 subvol, dstdir, newname);
425 printf("Create a snapshot of '%s' in '%s/%s'\n",
426 subvol, dstdir, newname);
430 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
431 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
438 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
439 subvol, strerror(e));
447 int do_delete_subvolume(int argc, char **argv)
450 struct btrfs_ioctl_vol_args args;
451 char *dname, *vname, *cpath;
452 char *path = argv[1];
454 res = test_issubvolume(path);
456 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
460 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
464 cpath = realpath(path, 0);
465 dname = strdup(cpath);
466 dname = dirname(dname);
467 vname = strdup(cpath);
468 vname = basename(vname);
471 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
472 strchr(vname, '/') ){
473 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
479 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
480 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
485 fd = open_file_or_dir(dname);
488 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
492 printf("Delete subvolume '%s/%s'\n", dname, vname);
493 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
494 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
500 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
501 dname, vname, strerror(e));
509 int do_create_subvol(int argc, char **argv)
511 int res, fddst, len, e;
514 struct btrfs_ioctl_vol_args args;
517 res = test_isdir(dst);
519 fprintf(stderr, "ERROR: '%s' exists\n", dst);
523 newname = strdup(dst);
524 newname = basename(newname);
525 dstdir = strdup(dst);
526 dstdir = dirname(dstdir);
528 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
529 strchr(newname, '/') ){
530 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
535 len = strlen(newname);
536 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
537 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
542 fddst = open_file_or_dir(dstdir);
544 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
548 printf("Create subvolume '%s/%s'\n", dstdir, newname);
549 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
550 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
556 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
565 int do_fssync(int argc, char **argv)
568 char *path = argv[1];
570 fd = open_file_or_dir(path);
572 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
576 printf("FSSync '%s'\n", path);
577 res = ioctl(fd, BTRFS_IOC_SYNC);
581 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
589 int do_scan(int argc, char **argv)
595 printf("Scanning for Btrfs filesystems\n");
596 ret = btrfs_scan_one_dir("/dev", 1);
598 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
604 fd = open("/dev/btrfs-control", O_RDWR);
606 perror("failed to open /dev/btrfs-control");
610 for( i = 1 ; i < argc ; i++ ){
611 struct btrfs_ioctl_vol_args args;
614 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
616 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
618 * FIXME: which are the error code returned by this ioctl ?
619 * it seems that is impossible to understand if there no is
620 * a btrfs filesystem from an I/O error !!!
622 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
627 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
628 argv[i], strerror(e));
638 int do_resize(int argc, char **argv)
641 struct btrfs_ioctl_vol_args args;
643 char *amount=argv[1], *path=argv[2];
645 fd = open_file_or_dir(path);
647 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
650 len = strlen(amount);
651 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
652 fprintf(stderr, "ERROR: size value too long ('%s)\n",
657 printf("Resize '%s' of '%s'\n", path, amount);
658 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
659 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
663 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
670 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
672 struct list_head *cur;
673 struct btrfs_device *device;
675 list_for_each(cur, &fs_devices->devices) {
676 device = list_entry(cur, struct btrfs_device, dev_list);
677 if ((device->label && strcmp(device->label, search) == 0) ||
678 strcmp(device->name, search) == 0)
684 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
687 struct list_head *cur;
688 struct btrfs_device *device;
689 char *super_bytes_used;
693 uuid_unparse(fs_devices->fsid, uuidbuf);
694 device = list_entry(fs_devices->devices.next, struct btrfs_device,
696 if (device->label && device->label[0])
697 printf("Label: '%s' ", device->label);
699 printf("Label: none ");
701 super_bytes_used = pretty_sizes(device->super_bytes_used);
703 total = device->total_devs;
704 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
705 (unsigned long long)total, super_bytes_used);
707 free(super_bytes_used);
709 list_for_each(cur, &fs_devices->devices) {
712 device = list_entry(cur, struct btrfs_device, dev_list);
713 total_bytes = pretty_sizes(device->total_bytes);
714 bytes_used = pretty_sizes(device->bytes_used);
715 printf("\tdevid %4llu size %s used %s path %s\n",
716 (unsigned long long)device->devid,
717 total_bytes, bytes_used, device->name);
722 if (devs_found < total) {
723 printf("\t*** Some devices missing\n");
728 int do_show_filesystem(int argc, char **argv)
730 struct list_head *all_uuids;
731 struct btrfs_fs_devices *fs_devices;
732 struct list_head *cur_uuid;
733 char *search = argv[1];
736 ret = btrfs_scan_one_dir("/dev", 0);
738 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
742 all_uuids = btrfs_scanned_uuids();
743 list_for_each(cur_uuid, all_uuids) {
744 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
746 if (search && uuid_search(fs_devices, search) == 0)
748 print_one_uuid(fs_devices);
750 printf("%s\n", BTRFS_BUILD_VERSION);
754 int do_add_volume(int nargs, char **args)
757 char *mntpnt = args[nargs-1];
758 int i, fdmnt, ret=0, e;
761 fdmnt = open_file_or_dir(mntpnt);
763 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
767 for (i = 1; i < (nargs-1); i++ ){
768 struct btrfs_ioctl_vol_args ioctl_args;
770 u64 dev_block_count = 0;
774 res = check_mounted(args[i]);
776 fprintf(stderr, "error checking %s mount status\n",
782 fprintf(stderr, "%s is mounted\n", args[i]);
787 devfd = open(args[i], O_RDWR);
789 fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
794 res = fstat(devfd, &st);
796 fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
801 if (!S_ISBLK(st.st_mode)) {
802 fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
808 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
810 fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
817 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
818 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
821 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
822 args[i], strerror(e));
836 int do_balance(int argc, char **argv)
840 struct btrfs_ioctl_vol_args args;
841 char *path = argv[1];
843 fdmnt = open_file_or_dir(path);
845 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
849 memset(&args, 0, sizeof(args));
850 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
854 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n",
861 int do_remove_volume(int nargs, char **args)
864 char *mntpnt = args[nargs-1];
865 int i, fdmnt, ret=0, e;
867 fdmnt = open_file_or_dir(mntpnt);
869 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
873 for(i=1 ; i < (nargs-1) ; i++ ){
874 struct btrfs_ioctl_vol_args arg;
877 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
878 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
881 fprintf(stderr, "ERROR: error removing the device '%s' - %s\n",
882 args[i], strerror(e));
894 int do_set_default_subvol(int nargs, char **argv)
898 char *path = argv[2];
899 char *subvolid = argv[1];
901 fd = open_file_or_dir(path);
903 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
907 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
908 if (errno == ERANGE) {
909 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
912 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
916 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
923 int do_change_label(int nargs, char **argv)
925 /* check the number of argument */
927 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
930 }else if (nargs == 2){
931 return get_label(argv[1]);
932 } else { /* nargs == 0 */
933 return set_label(argv[1], argv[2]);
938 int do_df_filesystem(int nargs, char **argv)
940 struct btrfs_ioctl_space_args *sargs;
945 char *path = argv[1];
947 fd = open_file_or_dir(path);
949 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
953 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
957 sargs->space_slots = 0;
958 sargs->total_spaces = 0;
960 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
963 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
968 if (!sargs->total_spaces)
971 count = sargs->total_spaces;
973 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
974 (count * sizeof(struct btrfs_ioctl_space_info)));
978 sargs->space_slots = count;
979 sargs->total_spaces = 0;
981 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
984 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
991 for (i = 0; i < sargs->total_spaces; i++) {
992 char description[80];
996 u64 flags = sargs->spaces[i].flags;
998 memset(description, 0, 80);
1000 if (flags & BTRFS_BLOCK_GROUP_DATA) {
1001 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1002 snprintf(description, 15, "%s",
1006 snprintf(description, 5, "%s", "Data");
1009 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
1010 snprintf(description, 7, "%s", "System");
1012 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1013 snprintf(description, 9, "%s", "Metadata");
1017 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
1018 snprintf(description+written, 8, "%s", ", RAID0");
1020 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
1021 snprintf(description+written, 8, "%s", ", RAID1");
1023 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
1024 snprintf(description+written, 6, "%s", ", DUP");
1026 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
1027 snprintf(description+written, 9, "%s", ", RAID10");
1031 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
1032 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
1033 printf("%s: total=%s, used=%s\n", description, total_bytes,