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"
45 #define BLKGETSIZE64 0
46 #define BTRFS_IOC_SNAP_CREATE 0
47 #define BTRFS_VOL_NAME_MAX 255
48 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
49 static inline int ioctl(int fd, int define, void *arg) { return 0; }
53 * test if path is a subvolume:
54 * this function return
55 * 0-> path exists but it is not a subvolume
56 * 1-> path exists and it is a subvolume
57 * -1 -> path is unaccessible
59 static int test_issubvolume(char *path)
65 res = stat(path, &st);
69 return (st.st_ino == 256) && S_ISDIR(st.st_mode);
74 * test if path is a directory
75 * this function return
76 * 0-> path exists but it is not a directory
77 * 1-> path exists and it is a directory
78 * -1 -> path is unaccessible
80 static int test_isdir(char *path)
85 res = stat(path, &st);
89 return S_ISDIR(st.st_mode);
93 static int open_file_or_dir(const char *fname)
100 ret = stat(fname, &st);
104 if (S_ISDIR(st.st_mode)) {
105 dirstream = opendir(fname);
109 fd = dirfd(dirstream);
111 fd = open(fname, O_RDWR);
119 static u64 parse_size(char *s)
125 if (!isdigit(s[len - 1])) {
126 c = tolower(s[len - 1]);
137 fprintf(stderr, "Unknown size descriptor %c\n", c);
142 return atoll(s) * mult;
145 int do_defrag(int ac, char **av)
158 struct btrfs_ioctl_defrag_range_args range;
163 int c = getopt(ac, av, "vcfs:l:t:");
179 start = parse_size(optarg);
183 len = parse_size(optarg);
187 thresh = parse_size(optarg);
191 fprintf(stderr, "Invalid arguments for defragment\n");
196 if (ac - optind == 0) {
197 fprintf(stderr, "Invalid arguments for defragment\n");
202 memset(&range, 0, sizeof(range));
205 range.extent_thresh = thresh;
207 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
209 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
211 for (i = optind; i < ac; i++) {
213 printf("%s\n", av[i]);
214 fd = open_file_or_dir(av[i]);
216 fprintf(stderr, "failed to open %s\n", av[i]);
222 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
225 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
226 if (ret && errno == ENOTTY) {
227 fprintf(stderr, "ERROR: defrag range ioctl not "
228 "supported in this kernel, please try "
229 "without any options.\n");
236 fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
243 printf("%s\n", BTRFS_BUILD_VERSION);
245 fprintf(stderr, "total %d failures\n", errors);
253 int do_find_newer(int argc, char **argv)
261 last_gen = atoll(argv[2]);
263 ret = test_issubvolume(subvol);
265 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
269 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
273 fd = open_file_or_dir(subvol);
275 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
278 ret = find_updated_files(fd, 0, last_gen);
284 int do_subvol_list(int argc, char **argv)
292 ret = test_issubvolume(subvol);
294 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
298 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
302 fd = open_file_or_dir(subvol);
304 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
307 ret = list_subvols(fd);
313 int do_clone(int argc, char **argv)
316 int res, fd, fddst, len, e;
322 struct btrfs_ioctl_vol_args args;
324 res = test_issubvolume(subvol);
326 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
330 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
334 res = test_isdir(dst);
336 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
341 newname = strdup(subvol);
342 newname = basename(newname);
345 newname = strdup(dst);
346 newname = basename(newname);
347 dstdir = strdup(dst);
348 dstdir = dirname(dstdir);
351 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
352 strchr(newname, '/') ){
353 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
358 len = strlen(newname);
359 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
360 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
365 fddst = open_file_or_dir(dstdir);
367 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
371 fd = open_file_or_dir(subvol);
374 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
378 printf("Create a snapshot of '%s' in '%s/%s'\n",
379 subvol, dstdir, newname);
381 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
382 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args);
389 fprintf( stderr, "ERROR: cannot snapshot '%s' - %s\n",
390 subvol, strerror(e));
398 int do_delete_subvolume(int argc, char **argv)
401 struct btrfs_ioctl_vol_args args;
402 char *dname, *vname, *cpath;
403 char *path = argv[1];
405 res = test_issubvolume(path);
407 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
411 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
415 cpath = realpath(path, 0);
416 dname = strdup(cpath);
417 dname = dirname(dname);
418 vname = strdup(cpath);
419 vname = basename(vname);
422 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
423 strchr(vname, '/') ){
424 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
430 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
431 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
436 fd = open_file_or_dir(dname);
439 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
443 printf("Delete subvolume '%s/%s'\n", dname, vname);
444 strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
445 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
451 fprintf( stderr, "ERROR: cannot delete '%s/%s' - %s\n",
452 dname, vname, strerror(e));
460 int do_create_subvol(int argc, char **argv)
462 int res, fddst, len, e;
465 struct btrfs_ioctl_vol_args args;
468 res = test_isdir(dst);
470 fprintf(stderr, "ERROR: '%s' exists\n", dst);
474 newname = strdup(dst);
475 newname = basename(newname);
476 dstdir = strdup(dst);
477 dstdir = dirname(dstdir);
479 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
480 strchr(newname, '/') ){
481 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
486 len = strlen(newname);
487 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
488 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
493 fddst = open_file_or_dir(dstdir);
495 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
499 printf("Create subvolume '%s/%s'\n", dstdir, newname);
500 strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
501 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
507 fprintf( stderr, "ERROR: cannot create subvolume - %s\n",
516 int do_fssync(int argc, char **argv)
519 char *path = argv[1];
521 fd = open_file_or_dir(path);
523 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
527 printf("FSSync '%s'\n", path);
528 res = ioctl(fd, BTRFS_IOC_SYNC);
532 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n",
540 int do_scan(int argc, char **argv)
546 printf("Scanning for Btrfs filesystems\n");
547 ret = btrfs_scan_one_dir("/dev", 1);
549 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
555 fd = open("/dev/btrfs-control", O_RDWR);
557 perror("failed to open /dev/btrfs-control");
561 for( i = 1 ; i < argc ; i++ ){
562 struct btrfs_ioctl_vol_args args;
565 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
567 strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
569 * FIXME: which are the error code returned by this ioctl ?
570 * it seems that is impossible to understand if there no is
571 * a btrfs filesystem from an I/O error !!!
573 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
578 fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n",
579 argv[i], strerror(e));
589 int do_resize(int argc, char **argv)
592 struct btrfs_ioctl_vol_args args;
594 char *amount=argv[1], *path=argv[2];
596 fd = open_file_or_dir(path);
598 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
601 len = strlen(amount);
602 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
603 fprintf(stderr, "ERROR: size value too long ('%s)\n",
608 printf("Resize '%s' of '%s'\n", path, amount);
609 strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
610 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
614 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n",
621 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
623 struct list_head *cur;
624 struct btrfs_device *device;
626 list_for_each(cur, &fs_devices->devices) {
627 device = list_entry(cur, struct btrfs_device, dev_list);
628 if ((device->label && strcmp(device->label, search) == 0) ||
629 strcmp(device->name, search) == 0)
635 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
638 struct list_head *cur;
639 struct btrfs_device *device;
640 char *super_bytes_used;
644 uuid_unparse(fs_devices->fsid, uuidbuf);
645 device = list_entry(fs_devices->devices.next, struct btrfs_device,
647 if (device->label && device->label[0])
648 printf("Label: '%s' ", device->label);
650 printf("Label: none ");
652 super_bytes_used = pretty_sizes(device->super_bytes_used);
654 total = device->total_devs;
655 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
656 (unsigned long long)total, super_bytes_used);
658 free(super_bytes_used);
660 list_for_each(cur, &fs_devices->devices) {
663 device = list_entry(cur, struct btrfs_device, dev_list);
664 total_bytes = pretty_sizes(device->total_bytes);
665 bytes_used = pretty_sizes(device->bytes_used);
666 printf("\tdevid %4llu size %s used %s path %s\n",
667 (unsigned long long)device->devid,
668 total_bytes, bytes_used, device->name);
673 if (devs_found < total) {
674 printf("\t*** Some devices missing\n");
679 int do_show_filesystem(int argc, char **argv)
681 struct list_head *all_uuids;
682 struct btrfs_fs_devices *fs_devices;
683 struct list_head *cur_uuid;
684 char *search = argv[1];
687 ret = btrfs_scan_one_dir("/dev", 0);
689 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
693 all_uuids = btrfs_scanned_uuids();
694 list_for_each(cur_uuid, all_uuids) {
695 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
697 if (search && uuid_search(fs_devices, search) == 0)
699 print_one_uuid(fs_devices);
701 printf("%s\n", BTRFS_BUILD_VERSION);
705 int do_add_volume(int nargs, char **args)
708 char *mntpnt = args[nargs-1];
709 int i, fdmnt, ret=0, e;
712 fdmnt = open_file_or_dir(mntpnt);
714 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
718 for(i=1 ; i < (nargs-1) ; i++ ){
719 struct btrfs_ioctl_vol_args ioctl_args;
721 u64 dev_block_count = 0;
725 devfd = open(args[i], O_RDWR);
727 fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
732 ret = fstat(devfd, &st);
734 fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
739 if (!S_ISBLK(st.st_mode)) {
740 fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
746 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count, &mixed);
748 fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
755 strncpy(ioctl_args.name, args[i], BTRFS_PATH_NAME_MAX);
756 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
759 fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
760 args[i], strerror(e));
774 int do_balance(int argc, char **argv)
778 struct btrfs_ioctl_vol_args args;
779 char *path = argv[1];
781 fdmnt = open_file_or_dir(path);
783 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
787 memset(&args, 0, sizeof(args));
788 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
792 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n",
799 int do_remove_volume(int nargs, char **args)
802 char *mntpnt = args[nargs-1];
803 int i, fdmnt, ret=0, e;
805 fdmnt = open_file_or_dir(mntpnt);
807 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
811 for(i=1 ; i < (nargs-1) ; i++ ){
812 struct btrfs_ioctl_vol_args arg;
815 strncpy(arg.name, args[i], BTRFS_PATH_NAME_MAX);
816 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
819 fprintf(stderr, "ERROR: error removing the device '%s' - %s\n",
820 args[i], strerror(e));
832 int do_set_default_subvol(int nargs, char **argv)
836 char *path = argv[2];
837 char *subvolid = argv[1];
839 fd = open_file_or_dir(path);
841 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
845 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
846 if (errno == ERANGE) {
847 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
850 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
854 fprintf(stderr, "ERROR: unable to set a new default subvolume - %s\n",
861 int do_df_filesystem(int nargs, char **argv)
863 struct btrfs_ioctl_space_args *sargs;
868 char *path = argv[1];
870 fd = open_file_or_dir(path);
872 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
876 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
880 sargs->space_slots = 0;
881 sargs->total_spaces = 0;
883 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
886 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
891 if (!sargs->total_spaces)
894 count = sargs->total_spaces;
896 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
897 (count * sizeof(struct btrfs_ioctl_space_info)));
901 sargs->space_slots = count;
902 sargs->total_spaces = 0;
904 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
907 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
914 for (i = 0; i < sargs->total_spaces; i++) {
915 char description[80];
919 u64 flags = sargs->spaces[i].flags;
921 memset(description, 0, 80);
923 if (flags & BTRFS_BLOCK_GROUP_DATA) {
924 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
925 snprintf(description, 15, "%s",
929 snprintf(description, 5, "%s", "Data");
932 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
933 snprintf(description, 7, "%s", "System");
935 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
936 snprintf(description, 9, "%s", "Metadata");
940 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
941 snprintf(description+written, 8, "%s", ", RAID0");
943 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
944 snprintf(description+written, 8, "%s", ", RAID1");
946 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
947 snprintf(description+written, 6, "%s", ", DUP");
949 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
950 snprintf(description+written, 9, "%s", ", RAID10");
954 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
955 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
956 printf("%s: total=%s, used=%s\n", description, total_bytes,