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;
162 int c = getopt(ac, av, "vcfs:l:t:");
178 start = parse_size(optarg);
182 len = parse_size(optarg);
186 thresh = parse_size(optarg);
190 fprintf(stderr, "Invalid arguments for defragment\n");
195 if (ac - optind == 0) {
196 fprintf(stderr, "Invalid arguments for defragment\n");
201 memset(&range, 0, sizeof(range));
204 range.extent_thresh = thresh;
206 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
208 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
210 for (i = optind; i < ac; i++) {
212 printf("%s\n", av[i]);
213 fd = open_file_or_dir(av[i]);
215 fprintf(stderr, "failed to open %s\n", av[i]);
221 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
223 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
224 if (ret && errno == ENOTTY) {
225 fprintf(stderr, "defrag range ioctl not "
226 "supported in this kernel, please try "
227 "without any options.\n");
233 fprintf(stderr, "ioctl failed on %s ret %d errno %d\n",
240 printf("%s\n", BTRFS_BUILD_VERSION);
242 fprintf(stderr, "total %d failures\n", errors);
250 int do_subvol_list(int argc, char **argv)
258 ret = test_issubvolume(subvol);
260 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
264 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
268 fd = open_file_or_dir(subvol);
270 fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
273 ret = list_subvols(fd);
279 int do_clone(int argc, char **argv)
282 int res, fd, fddst, len;
288 struct btrfs_ioctl_vol_args args;
290 res = test_issubvolume(subvol);
292 fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
296 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
300 res = test_isdir(dst);
302 fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
307 newname = strdup(subvol);
308 newname = basename(newname);
311 newname = strdup(dst);
312 newname = basename(newname);
313 dstdir = strdup(dst);
314 dstdir = dirname(dstdir);
317 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
318 strchr(newname, '/') ){
319 fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
324 len = strlen(newname);
325 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
326 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
331 fddst = open_file_or_dir(dstdir);
333 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
337 fd = open_file_or_dir(subvol);
340 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
344 printf("Create a snapshot of '%s' in '%s/%s'\n",
345 subvol, dstdir, newname);
347 strcpy(args.name, newname);
348 res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args);
354 fprintf( stderr, "ERROR: cannot snapshot '%s'\n",subvol);
362 int do_delete_subvolume(int argc, char **argv)
365 struct btrfs_ioctl_vol_args args;
366 char *dname, *vname, *cpath;
367 char *path = argv[1];
369 res = test_issubvolume(path);
371 fprintf(stderr, "ERROR: error accessing '%s'\n", path);
375 fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
379 cpath = realpath(path, 0);
380 dname = strdup(cpath);
381 dname = dirname(dname);
382 vname = strdup(cpath);
383 vname = basename(vname);
386 if( !strcmp(vname,".") || !strcmp(vname,"..") ||
387 strchr(vname, '/') ){
388 fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
394 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
395 fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
400 fd = open_file_or_dir(dname);
403 fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
407 printf("Delete subvolume '%s/%s'\n", dname, vname);
408 strcpy(args.name, vname);
409 res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
414 fprintf( stderr, "ERROR: cannot delete '%s/%s'\n",dname, vname);
422 int do_create_subvol(int argc, char **argv)
427 struct btrfs_ioctl_vol_args args;
430 res = test_isdir(dst);
432 fprintf(stderr, "ERROR: '%s' exists\n", dst);
436 newname = strdup(dst);
437 newname = basename(newname);
438 dstdir = strdup(dst);
439 dstdir = dirname(dstdir);
441 if( !strcmp(newname,".") || !strcmp(newname,"..") ||
442 strchr(newname, '/') ){
443 fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
448 len = strlen(newname);
449 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
450 fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
455 fddst = open_file_or_dir(dstdir);
457 fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
461 printf("Create subvolume '%s/%s'\n", dstdir, newname);
462 strcpy(args.name, newname);
463 res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
468 fprintf( stderr, "ERROR: cannot create subvolume\n");
476 int do_fssync(int argc, char **argv)
479 char *path = argv[1];
481 fd = open_file_or_dir(path);
483 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
487 printf("FSSync '%s'\n", path);
488 res = ioctl(fd, BTRFS_IOC_SYNC);
491 fprintf(stderr, "ERROR: unable to fs-syncing '%s'\n", path);
498 int do_scan(int argc, char **argv)
504 printf("Scanning for Btrfs filesystems\n");
505 ret = btrfs_scan_one_dir("/dev", 1);
507 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
513 fd = open("/dev/btrfs-control", O_RDWR);
515 perror("failed to open /dev/btrfs-control");
519 for( i = 1 ; i < argc ; i++ ){
520 struct btrfs_ioctl_vol_args args;
523 printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
525 strcpy(args.name, argv[i]);
527 * FIXME: which are the error code returned by this ioctl ?
528 * it seems that is impossible to understand if there no is
529 * a btrfs filesystem from an I/O error !!!
531 ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
535 fprintf(stderr, "ERROR: unable to scan the device '%s'\n", argv[i]);
545 int do_resize(int argc, char **argv)
548 struct btrfs_ioctl_vol_args args;
550 char *amount=argv[1], *path=argv[2];
552 fd = open_file_or_dir(path);
554 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
557 len = strlen(amount);
558 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
559 fprintf(stderr, "ERROR: size value too long ('%s)\n",
564 printf("Resize '%s' of '%s'\n", path, amount);
565 strcpy(args.name, amount);
566 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
569 fprintf(stderr, "ERROR: unable to resize '%s'\n", path);
575 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
577 struct list_head *cur;
578 struct btrfs_device *device;
580 list_for_each(cur, &fs_devices->devices) {
581 device = list_entry(cur, struct btrfs_device, dev_list);
582 if ((device->label && strcmp(device->label, search) == 0) ||
583 strcmp(device->name, search) == 0)
589 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
592 struct list_head *cur;
593 struct btrfs_device *device;
594 char *super_bytes_used;
598 uuid_unparse(fs_devices->fsid, uuidbuf);
599 device = list_entry(fs_devices->devices.next, struct btrfs_device,
601 if (device->label && device->label[0])
602 printf("Label: '%s' ", device->label);
604 printf("Label: none ");
606 super_bytes_used = pretty_sizes(device->super_bytes_used);
608 total = device->total_devs;
609 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
610 (unsigned long long)total, super_bytes_used);
612 free(super_bytes_used);
614 list_for_each(cur, &fs_devices->devices) {
617 device = list_entry(cur, struct btrfs_device, dev_list);
618 total_bytes = pretty_sizes(device->total_bytes);
619 bytes_used = pretty_sizes(device->bytes_used);
620 printf("\tdevid %4llu size %s used %s path %s\n",
621 (unsigned long long)device->devid,
622 total_bytes, bytes_used, device->name);
627 if (devs_found < total) {
628 printf("\t*** Some devices missing\n");
633 int do_show_filesystem(int argc, char **argv)
635 struct list_head *all_uuids;
636 struct btrfs_fs_devices *fs_devices;
637 struct list_head *cur_uuid;
638 char *search = argv[1];
641 ret = btrfs_scan_one_dir("/dev", 0);
643 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
647 all_uuids = btrfs_scanned_uuids();
648 list_for_each(cur_uuid, all_uuids) {
649 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
651 if (search && uuid_search(fs_devices, search) == 0)
653 print_one_uuid(fs_devices);
655 printf("%s\n", BTRFS_BUILD_VERSION);
659 int do_add_volume(int nargs, char **args)
662 char *mntpnt = args[nargs-1];
666 fdmnt = open_file_or_dir(mntpnt);
668 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
672 for(i=1 ; i < (nargs-1) ; i++ ){
673 struct btrfs_ioctl_vol_args ioctl_args;
675 u64 dev_block_count = 0;
678 devfd = open(args[i], O_RDWR);
680 fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
685 ret = fstat(devfd, &st);
687 fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
692 if (!S_ISBLK(st.st_mode)) {
693 fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
699 res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count);
701 fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
708 strcpy(ioctl_args.name, args[i]);
709 res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
711 fprintf(stderr, "ERROR: error adding the device '%s'\n", args[i]);
725 int do_balance(int argc, char **argv)
729 struct btrfs_ioctl_vol_args args;
730 char *path = argv[1];
732 fdmnt = open_file_or_dir(path);
734 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
738 memset(&args, 0, sizeof(args));
739 ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
742 fprintf(stderr, "ERROR: balancing '%s'\n", path);
748 int do_remove_volume(int nargs, char **args)
751 char *mntpnt = args[nargs-1];
754 fdmnt = open_file_or_dir(mntpnt);
756 fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
760 for(i=1 ; i < (nargs-1) ; i++ ){
761 struct btrfs_ioctl_vol_args arg;
764 strcpy(arg.name, args[i]);
765 res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
767 fprintf(stderr, "ERROR: error removing the device '%s'\n", args[i]);
779 int do_set_default_subvol(int nargs, char **argv)
783 char *path = argv[2];
784 char *subvolid = argv[1];
786 fd = open_file_or_dir(path);
788 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
792 objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
793 if (errno == ERANGE) {
794 fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
797 ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
800 fprintf(stderr, "ERROR: unable to set a new default subvolume\n");
806 int do_df_filesystem(int nargs, char **argv)
808 struct btrfs_ioctl_space_args *sargs;
812 char *path = argv[1];
814 fd = open_file_or_dir(path);
816 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
820 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
824 sargs->space_slots = 0;
825 sargs->total_spaces = 0;
827 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
832 if (!sargs->total_spaces)
835 count = sargs->total_spaces;
837 sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
838 (count * sizeof(struct btrfs_ioctl_space_info)));
842 sargs->space_slots = count;
843 sargs->total_spaces = 0;
845 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
851 for (i = 0; i < sargs->total_spaces; i++) {
852 char description[80];
856 u64 flags = sargs->spaces[i].flags;
858 memset(description, 0, 80);
860 if (flags & BTRFS_BLOCK_GROUP_DATA) {
861 snprintf(description, 5, "%s", "Data");
863 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
864 snprintf(description, 7, "%s", "System");
866 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
867 snprintf(description, 9, "%s", "Metadata");
871 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
872 snprintf(description+written, 8, "%s", ", RAID0");
874 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
875 snprintf(description+written, 8, "%s", ", RAID1");
877 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
878 snprintf(description+written, 6, "%s", ", DUP");
880 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
881 snprintf(description+written, 9, "%s", ", RAID10");
885 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
886 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
887 printf("%s: total=%s, used=%s\n", description, total_bytes,