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 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
;
339 struct btrfs_ioctl_vol_args args
;
341 res
= test_issubvolume(subvol
);
343 fprintf(stderr
, "ERROR: error accessing '%s'\n", subvol
);
347 fprintf(stderr
, "ERROR: '%s' is not a subvolume\n", subvol
);
351 res
= test_isdir(dst
);
353 fprintf(stderr
, "ERROR: '%s' exists and it is not a directory\n", dst
);
358 newname
= strdup(subvol
);
359 newname
= basename(newname
);
362 newname
= strdup(dst
);
363 newname
= basename(newname
);
364 dstdir
= strdup(dst
);
365 dstdir
= dirname(dstdir
);
368 if( !strcmp(newname
,".") || !strcmp(newname
,"..") ||
369 strchr(newname
, '/') ){
370 fprintf(stderr
, "ERROR: incorrect snapshot name ('%s')\n",
375 len
= strlen(newname
);
376 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
377 fprintf(stderr
, "ERROR: snapshot name too long ('%s)\n",
382 fddst
= open_file_or_dir(dstdir
);
384 fprintf(stderr
, "ERROR: can't access to '%s'\n", dstdir
);
388 fd
= open_file_or_dir(subvol
);
391 fprintf(stderr
, "ERROR: can't access to '%s'\n", dstdir
);
395 printf("Create a snapshot of '%s' in '%s/%s'\n",
396 subvol
, dstdir
, newname
);
398 strncpy(args
.name
, newname
, BTRFS_PATH_NAME_MAX
);
399 res
= ioctl(fddst
, BTRFS_IOC_SNAP_CREATE
, &args
);
406 fprintf( stderr
, "ERROR: cannot snapshot '%s' - %s\n",
407 subvol
, strerror(e
));
415 int do_delete_subvolume(int argc
, char **argv
)
418 struct btrfs_ioctl_vol_args args
;
419 char *dname
, *vname
, *cpath
;
420 char *path
= argv
[1];
422 res
= test_issubvolume(path
);
424 fprintf(stderr
, "ERROR: error accessing '%s'\n", path
);
428 fprintf(stderr
, "ERROR: '%s' is not a subvolume\n", path
);
432 cpath
= realpath(path
, 0);
433 dname
= strdup(cpath
);
434 dname
= dirname(dname
);
435 vname
= strdup(cpath
);
436 vname
= basename(vname
);
439 if( !strcmp(vname
,".") || !strcmp(vname
,"..") ||
440 strchr(vname
, '/') ){
441 fprintf(stderr
, "ERROR: incorrect subvolume name ('%s')\n",
447 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
448 fprintf(stderr
, "ERROR: snapshot name too long ('%s)\n",
453 fd
= open_file_or_dir(dname
);
456 fprintf(stderr
, "ERROR: can't access to '%s'\n", dname
);
460 printf("Delete subvolume '%s/%s'\n", dname
, vname
);
461 strncpy(args
.name
, vname
, BTRFS_PATH_NAME_MAX
);
462 res
= ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &args
);
468 fprintf( stderr
, "ERROR: cannot delete '%s/%s' - %s\n",
469 dname
, vname
, strerror(e
));
477 int do_create_subvol(int argc
, char **argv
)
479 int res
, fddst
, len
, e
;
482 struct btrfs_ioctl_vol_args args
;
485 res
= test_isdir(dst
);
487 fprintf(stderr
, "ERROR: '%s' exists\n", dst
);
491 newname
= strdup(dst
);
492 newname
= basename(newname
);
493 dstdir
= strdup(dst
);
494 dstdir
= dirname(dstdir
);
496 if( !strcmp(newname
,".") || !strcmp(newname
,"..") ||
497 strchr(newname
, '/') ){
498 fprintf(stderr
, "ERROR: uncorrect subvolume name ('%s')\n",
503 len
= strlen(newname
);
504 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
505 fprintf(stderr
, "ERROR: subvolume name too long ('%s)\n",
510 fddst
= open_file_or_dir(dstdir
);
512 fprintf(stderr
, "ERROR: can't access to '%s'\n", dstdir
);
516 printf("Create subvolume '%s/%s'\n", dstdir
, newname
);
517 strncpy(args
.name
, newname
, BTRFS_PATH_NAME_MAX
);
518 res
= ioctl(fddst
, BTRFS_IOC_SUBVOL_CREATE
, &args
);
524 fprintf( stderr
, "ERROR: cannot create subvolume - %s\n",
533 int do_fssync(int argc
, char **argv
)
536 char *path
= argv
[1];
538 fd
= open_file_or_dir(path
);
540 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
544 printf("FSSync '%s'\n", path
);
545 res
= ioctl(fd
, BTRFS_IOC_SYNC
);
549 fprintf(stderr
, "ERROR: unable to fs-syncing '%s' - %s\n",
557 int do_scan(int argc
, char **argv
)
563 printf("Scanning for Btrfs filesystems\n");
564 ret
= btrfs_scan_one_dir("/dev", 1);
566 fprintf(stderr
, "ERROR: error %d while scanning\n", ret
);
572 fd
= open("/dev/btrfs-control", O_RDWR
);
574 perror("failed to open /dev/btrfs-control");
578 for( i
= 1 ; i
< argc
; i
++ ){
579 struct btrfs_ioctl_vol_args args
;
582 printf("Scanning for Btrfs filesystems in '%s'\n", argv
[i
]);
584 strncpy(args
.name
, argv
[i
], BTRFS_PATH_NAME_MAX
);
586 * FIXME: which are the error code returned by this ioctl ?
587 * it seems that is impossible to understand if there no is
588 * a btrfs filesystem from an I/O error !!!
590 ret
= ioctl(fd
, BTRFS_IOC_SCAN_DEV
, &args
);
595 fprintf(stderr
, "ERROR: unable to scan the device '%s' - %s\n",
596 argv
[i
], strerror(e
));
606 int do_resize(int argc
, char **argv
)
609 struct btrfs_ioctl_vol_args args
;
611 char *amount
=argv
[1], *path
=argv
[2];
613 fd
= open_file_or_dir(path
);
615 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
618 len
= strlen(amount
);
619 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
620 fprintf(stderr
, "ERROR: size value too long ('%s)\n",
625 printf("Resize '%s' of '%s'\n", path
, amount
);
626 strncpy(args
.name
, amount
, BTRFS_PATH_NAME_MAX
);
627 res
= ioctl(fd
, BTRFS_IOC_RESIZE
, &args
);
631 fprintf(stderr
, "ERROR: unable to resize '%s' - %s\n",
638 static int uuid_search(struct btrfs_fs_devices
*fs_devices
, char *search
)
640 struct list_head
*cur
;
641 struct btrfs_device
*device
;
643 list_for_each(cur
, &fs_devices
->devices
) {
644 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
645 if ((device
->label
&& strcmp(device
->label
, search
) == 0) ||
646 strcmp(device
->name
, search
) == 0)
652 static void print_one_uuid(struct btrfs_fs_devices
*fs_devices
)
655 struct list_head
*cur
;
656 struct btrfs_device
*device
;
657 char *super_bytes_used
;
661 uuid_unparse(fs_devices
->fsid
, uuidbuf
);
662 device
= list_entry(fs_devices
->devices
.next
, struct btrfs_device
,
664 if (device
->label
&& device
->label
[0])
665 printf("Label: '%s' ", device
->label
);
667 printf("Label: none ");
669 super_bytes_used
= pretty_sizes(device
->super_bytes_used
);
671 total
= device
->total_devs
;
672 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf
,
673 (unsigned long long)total
, super_bytes_used
);
675 free(super_bytes_used
);
677 list_for_each(cur
, &fs_devices
->devices
) {
680 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
681 total_bytes
= pretty_sizes(device
->total_bytes
);
682 bytes_used
= pretty_sizes(device
->bytes_used
);
683 printf("\tdevid %4llu size %s used %s path %s\n",
684 (unsigned long long)device
->devid
,
685 total_bytes
, bytes_used
, device
->name
);
690 if (devs_found
< total
) {
691 printf("\t*** Some devices missing\n");
696 int do_show_filesystem(int argc
, char **argv
)
698 struct list_head
*all_uuids
;
699 struct btrfs_fs_devices
*fs_devices
;
700 struct list_head
*cur_uuid
;
701 char *search
= argv
[1];
704 ret
= btrfs_scan_one_dir("/dev", 0);
706 fprintf(stderr
, "ERROR: error %d while scanning\n", ret
);
710 all_uuids
= btrfs_scanned_uuids();
711 list_for_each(cur_uuid
, all_uuids
) {
712 fs_devices
= list_entry(cur_uuid
, struct btrfs_fs_devices
,
714 if (search
&& uuid_search(fs_devices
, search
) == 0)
716 print_one_uuid(fs_devices
);
718 printf("%s\n", BTRFS_BUILD_VERSION
);
722 int do_add_volume(int nargs
, char **args
)
725 char *mntpnt
= args
[nargs
-1];
726 int i
, fdmnt
, ret
=0, e
;
729 fdmnt
= open_file_or_dir(mntpnt
);
731 fprintf(stderr
, "ERROR: can't access to '%s'\n", mntpnt
);
735 for (i
= 1; i
< (nargs
-1); i
++ ){
736 struct btrfs_ioctl_vol_args ioctl_args
;
738 u64 dev_block_count
= 0;
742 res
= check_mounted(args
[i
]);
744 fprintf(stderr
, "error checking %s mount status\n",
750 fprintf(stderr
, "%s is mounted\n", args
[i
]);
755 devfd
= open(args
[i
], O_RDWR
);
757 fprintf(stderr
, "ERROR: Unable to open device '%s'\n", args
[i
]);
762 res
= fstat(devfd
, &st
);
764 fprintf(stderr
, "ERROR: Unable to stat '%s'\n", args
[i
]);
769 if (!S_ISBLK(st
.st_mode
)) {
770 fprintf(stderr
, "ERROR: '%s' is not a block device\n", args
[i
]);
776 res
= btrfs_prepare_device(devfd
, args
[i
], 1, &dev_block_count
, &mixed
);
778 fprintf(stderr
, "ERROR: Unable to init '%s'\n", args
[i
]);
785 strncpy(ioctl_args
.name
, args
[i
], BTRFS_PATH_NAME_MAX
);
786 res
= ioctl(fdmnt
, BTRFS_IOC_ADD_DEV
, &ioctl_args
);
789 fprintf(stderr
, "ERROR: error adding the device '%s' - %s\n",
790 args
[i
], strerror(e
));
804 int do_balance(int argc
, char **argv
)
808 struct btrfs_ioctl_vol_args args
;
809 char *path
= argv
[1];
811 fdmnt
= open_file_or_dir(path
);
813 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
817 memset(&args
, 0, sizeof(args
));
818 ret
= ioctl(fdmnt
, BTRFS_IOC_BALANCE
, &args
);
822 fprintf(stderr
, "ERROR: error during balancing '%s' - %s\n",
829 int do_remove_volume(int nargs
, char **args
)
832 char *mntpnt
= args
[nargs
-1];
833 int i
, fdmnt
, ret
=0, e
;
835 fdmnt
= open_file_or_dir(mntpnt
);
837 fprintf(stderr
, "ERROR: can't access to '%s'\n", mntpnt
);
841 for(i
=1 ; i
< (nargs
-1) ; i
++ ){
842 struct btrfs_ioctl_vol_args arg
;
845 strncpy(arg
.name
, args
[i
], BTRFS_PATH_NAME_MAX
);
846 res
= ioctl(fdmnt
, BTRFS_IOC_RM_DEV
, &arg
);
849 fprintf(stderr
, "ERROR: error removing the device '%s' - %s\n",
850 args
[i
], strerror(e
));
862 int do_set_default_subvol(int nargs
, char **argv
)
866 char *path
= argv
[2];
867 char *subvolid
= argv
[1];
869 fd
= open_file_or_dir(path
);
871 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
875 objectid
= (unsigned long long)strtoll(subvolid
, NULL
, 0);
876 if (errno
== ERANGE
) {
877 fprintf(stderr
, "ERROR: invalid tree id (%s)\n",subvolid
);
880 ret
= ioctl(fd
, BTRFS_IOC_DEFAULT_SUBVOL
, &objectid
);
884 fprintf(stderr
, "ERROR: unable to set a new default subvolume - %s\n",
891 int do_change_label(int nargs
, char **argv
)
893 /* check the number of argument */
895 fprintf(stderr
, "ERROR: '%s' requires maximum 2 args\n",
898 }else if (nargs
== 2){
899 return get_label(argv
[1]);
900 } else { /* nargs == 0 */
901 return set_label(argv
[1], argv
[2]);
906 int do_df_filesystem(int nargs
, char **argv
)
908 struct btrfs_ioctl_space_args
*sargs
;
913 char *path
= argv
[1];
915 fd
= open_file_or_dir(path
);
917 fprintf(stderr
, "ERROR: can't access to '%s'\n", path
);
921 sargs
= malloc(sizeof(struct btrfs_ioctl_space_args
));
925 sargs
->space_slots
= 0;
926 sargs
->total_spaces
= 0;
928 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
931 fprintf(stderr
, "ERROR: couldn't get space info on '%s' - %s\n",
936 if (!sargs
->total_spaces
)
939 count
= sargs
->total_spaces
;
941 sargs
= realloc(sargs
, sizeof(struct btrfs_ioctl_space_args
) +
942 (count
* sizeof(struct btrfs_ioctl_space_info
)));
946 sargs
->space_slots
= count
;
947 sargs
->total_spaces
= 0;
949 ret
= ioctl(fd
, BTRFS_IOC_SPACE_INFO
, sargs
);
952 fprintf(stderr
, "ERROR: couldn't get space info on '%s' - %s\n",
959 for (i
= 0; i
< sargs
->total_spaces
; i
++) {
960 char description
[80];
964 u64 flags
= sargs
->spaces
[i
].flags
;
966 memset(description
, 0, 80);
968 if (flags
& BTRFS_BLOCK_GROUP_DATA
) {
969 if (flags
& BTRFS_BLOCK_GROUP_METADATA
) {
970 snprintf(description
, 15, "%s",
974 snprintf(description
, 5, "%s", "Data");
977 } else if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
978 snprintf(description
, 7, "%s", "System");
980 } else if (flags
& BTRFS_BLOCK_GROUP_METADATA
) {
981 snprintf(description
, 9, "%s", "Metadata");
985 if (flags
& BTRFS_BLOCK_GROUP_RAID0
) {
986 snprintf(description
+written
, 8, "%s", ", RAID0");
988 } else if (flags
& BTRFS_BLOCK_GROUP_RAID1
) {
989 snprintf(description
+written
, 8, "%s", ", RAID1");
991 } else if (flags
& BTRFS_BLOCK_GROUP_DUP
) {
992 snprintf(description
+written
, 6, "%s", ", DUP");
994 } else if (flags
& BTRFS_BLOCK_GROUP_RAID10
) {
995 snprintf(description
+written
, 9, "%s", ", RAID10");
999 total_bytes
= pretty_sizes(sargs
->spaces
[i
].total_bytes
);
1000 used_bytes
= pretty_sizes(sargs
->spaces
[i
].used_bytes
);
1001 printf("%s: total=%s, used=%s\n", description
, total_bytes
,