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 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)
305 int print_parent = 0;
310 int c = getopt(argc, argv, "p");
320 if (argc - optind != 1) {
321 fprintf(stderr, "ERROR: invalid arguments for subvolume list\n");
325 subvol = argv[optind];
327 ret = test_issubvolume(subvol);
329 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
333 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
337 fd = open_file_or_dir(subvol);
339 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
342 ret = list_subvols(fd, print_parent, 0);
348 int do_clone(int argc, char **argv)
351 int res, fd, fddst, len, e, optind = 0, readonly = 0;
354 struct btrfs_ioctl_vol_args_v2 args;
356 memset(&args, 0, sizeof(args));
359 int c = getopt(argc, argv, "r");
370 "Invalid arguments for subvolume snapshot\n");
375 if (argc - optind != 3) {
376 fprintf(stderr, "Invalid arguments for subvolume snapshot\n");
381 subvol = argv[optind+1];
382 dst = argv[optind+2];
384 res = test_issubvolume(subvol);
386 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
390 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
394 res = test_isdir(dst);
396 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
401 newname = strdup(subvol);
402 newname = basename(newname);
405 newname = strdup(dst);
406 newname = basename(newname);
407 dstdir = strdup(dst);
408 dstdir = dirname(dstdir);
411 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
412 strchr(newname, '/') ){
413 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
418 len = strlen(newname);
419 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
420 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
425 fddst = open_file_or_dir(dstdir);
427 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
431 fd = open_file_or_dir(subvol);
434 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
439 args.flags |= BTRFS_SUBVOL_RDONLY;
440 printf("Create a readonly snapshot of '%s' in '%s/%s'\n",
441 subvol, dstdir, newname);
443 printf("Create a snapshot of '%s' in '%s/%s'\n",
444 subvol, dstdir, newname);
448 strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
449 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
456 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
457 subvol, strerror(e));
465 int do_delete_subvolume(int argc, char **argv)
468 struct btrfs_ioctl_vol_args args;
469 char *dname, *vname, *cpath;
470 char *path = argv[1];
472 res = test_issubvolume(path);
474 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
478 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
482 cpath = realpath(path, 0);
483 dname = strdup(cpath);
484 dname = dirname(dname);
485 vname = strdup(cpath);
486 vname = basename(vname);
489 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
490 strchr(vname, '/') ){
491 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
497 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
498 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
503 fd = open_file_or_dir(dname);
506 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
510 printf("Delete subvolume '%s/%s'\n", dname, vname);
511 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
512 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
518 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
519 dname, vname, strerror(e));
527 int do_create_subvol(int argc, char **argv)
529 int res, fddst, len, e;
532 struct btrfs_ioctl_vol_args args;
535 res = test_isdir(dst);
537 fprintf(stderr, "ERROR: '%s' exists\n", dst);
541 newname = strdup(dst);
542 newname = basename(newname);
543 dstdir = strdup(dst);
544 dstdir = dirname(dstdir);
546 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
547 strchr(newname, '/') ){
548 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
553 len = strlen(newname);
554 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
555 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
560 fddst = open_file_or_dir(dstdir);
562 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
566 printf("Create subvolume '%s/%s'\n", dstdir, newname);
567 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
568 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
574 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
583 int do_fssync(int argc, char **argv)
586 char *path = argv[1];
588 fd = open_file_or_dir(path);
590 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
594 printf("FSSync '%s'\n", path);
595 res = ioctl(fd, BTRFS_IOC_SYNC);
599 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
607 int do_scan(int argc, char **argv)
613 printf("Scanning for Btrfs filesystems\n");
614 ret = btrfs_scan_one_dir("/dev", 1);
616 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
622 fd = open("/dev/btrfs-control", O_RDWR);
624 perror("failed to open /dev/btrfs-control");
628 for( i = 1 ; i < argc ; i++ ){
629 struct btrfs_ioctl_vol_args args;
632 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
634 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
636 * FIXME: which are the error code returned by this ioctl ?
637 * it seems that is impossible to understand if there no is
638 * a btrfs filesystem from an I/O error !!!
640 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
645 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
646 argv[i], strerror(e));
656 int do_resize(int argc, char **argv)
659 struct btrfs_ioctl_vol_args args;
661 char *amount=argv[1], *path=argv[2];
663 fd = open_file_or_dir(path);
665 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
668 len = strlen(amount);
669 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
670 fprintf(stderr, "ERROR: size value too long ('%s)\n",
675 printf("Resize '%s' of '%s'\n", path, amount);
676 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
677 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
681 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
688 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
690 struct list_head *cur;
691 struct btrfs_device *device;
693 list_for_each(cur, &fs_devices->devices) {
694 device = list_entry(cur, struct btrfs_device, dev_list);
695 if ((device->label && strcmp(device->label, search) == 0) ||
696 strcmp(device->name, search) == 0)
702 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
705 struct list_head *cur;
706 struct btrfs_device *device;
707 char *super_bytes_used;
711 uuid_unparse(fs_devices->fsid, uuidbuf);
712 device = list_entry(fs_devices->devices.next, struct btrfs_device,
714 if (device->label && device->label[0])
715 printf("Label: '%s' ", device->label);
717 printf("Label: none ");
719 super_bytes_used = pretty_sizes(device->super_bytes_used);
721 total = device->total_devs;
722 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
723 (unsigned long long)total, super_bytes_used);
725 free(super_bytes_used);
727 list_for_each(cur, &fs_devices->devices) {
730 device = list_entry(cur, struct btrfs_device, dev_list);
731 total_bytes = pretty_sizes(device->total_bytes);
732 bytes_used = pretty_sizes(device->bytes_used);
733 printf("\tdevid %4llu size %s used %s path %s\n",
734 (unsigned long long)device->devid,
735 total_bytes, bytes_used, device->name);
740 if (devs_found < total) {
741 printf("\t*** Some devices missing\n");
746 int do_show_filesystem(int argc, char **argv)
748 struct list_head *all_uuids;
749 struct btrfs_fs_devices *fs_devices;
750 struct list_head *cur_uuid;
751 char *search = argv[1];
754 ret = btrfs_scan_one_dir("/dev", 0);
756 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
760 all_uuids = btrfs_scanned_uuids();
761 list_for_each(cur_uuid, all_uuids) {
762 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
764 if (search && uuid_search(fs_devices, search) == 0)
766 print_one_uuid(fs_devices);
768 printf("%s\n", BTRFS_BUILD_VERSION);
772 int do_add_volume(int nargs, char **args)
775 char *mntpnt = args[nargs-1];
776 int i, fdmnt, ret=0, e;
779 fdmnt = open_file_or_dir(mntpnt);
781 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
785 for (i = 1; i < (nargs-1); i++ ){
786 struct btrfs_ioctl_vol_args ioctl_args;
788 u64 dev_block_count = 0;
792 res = check_mounted(args[i]);
794 fprintf(stderr, "error checking %s mount status\n",
800 fprintf(stderr, "%s is mounted\n", args[i]);
805 devfd = open(args[i], O_RDWR);
807 fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
812 res = fstat(devfd, &st);
814 fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
819 if (!S_ISBLK(st.st_mode)) {
820 fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
826 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
828 fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
835 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
836 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
839 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
840 args[i], strerror(e));
854 int do_balance(int argc, char **argv)
858 struct btrfs_ioctl_vol_args args;
859 char *path = argv[1];
861 fdmnt = open_file_or_dir(path);
863 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
867 memset(&args, 0, sizeof(args));
868 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
872 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n",
879 int do_remove_volume(int nargs, char **args)
882 char *mntpnt = args[nargs-1];
883 int i, fdmnt, ret=0, e;
885 fdmnt = open_file_or_dir(mntpnt);
887 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
891 for(i=1 ; i < (nargs-1) ; i++ ){
892 struct btrfs_ioctl_vol_args arg;
895 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
896 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
899 fprintf(stderr, "ERROR: error removing the device '%s' - %s\n",
900 args[i], strerror(e));
912 int do_set_default_subvol(int nargs, char **argv)
916 char *path = argv[2];
917 char *subvolid = argv[1];
919 fd = open_file_or_dir(path);
921 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
925 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
926 if (errno == ERANGE) {
927 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
930 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
934 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
941 int do_change_label(int nargs, char **argv)
943 /* check the number of argument */
945 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
948 }else if (nargs == 2){
949 return get_label(argv[1]);
950 } else { /* nargs == 0 */
951 return set_label(argv[1], argv[2]);
956 int do_get_default_subvol(int nargs, char **argv)
964 ret = test_issubvolume(subvol);
966 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
970 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
974 fd = open_file_or_dir(subvol);
976 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
979 ret = list_subvols(fd, 0, 1);
985 int do_df_filesystem(int nargs, char **argv)
987 struct btrfs_ioctl_space_args *sargs;
992 char *path = argv[1];
994 fd = open_file_or_dir(path);
996 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
1000 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
1004 sargs->space_slots = 0;
1005 sargs->total_spaces = 0;
1007 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
1010 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
1015 if (!sargs->total_spaces)
1018 count = sargs->total_spaces;
1020 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
1021 (count * sizeof(struct btrfs_ioctl_space_info)));
1025 sargs->space_slots = count;
1026 sargs->total_spaces = 0;
1028 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
1031 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
1038 for (i = 0; i < sargs->total_spaces; i++) {
1039 char description[80];
1043 u64 flags = sargs->spaces[i].flags;
1045 memset(description, 0, 80);
1047 if (flags & BTRFS_BLOCK_GROUP_DATA) {
1048 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1049 snprintf(description, 15, "%s",
1053 snprintf(description, 5, "%s", "Data");
1056 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
1057 snprintf(description, 7, "%s", "System");
1059 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
1060 snprintf(description, 9, "%s", "Metadata");
1064 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
1065 snprintf(description+written, 8, "%s", ", RAID0");
1067 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
1068 snprintf(description+written, 8, "%s", ", RAID1");
1070 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
1071 snprintf(description+written, 6, "%s", ", DUP");
1073 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
1074 snprintf(description+written, 9, "%s", ", RAID10");
1078 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
1079 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
1080 printf("%s: total=%s, used=%s\n", description, total_bytes,