2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include <sys/ioctl.h>
21 #include <sys/mount.h>
26 #include <sys/types.h>
32 #include "kerncompat.h"
34 #include "transaction.h"
39 #define BLKGETSIZE64 0
40 #define BTRFS_IOC_SNAP_CREATE 0
41 #define BTRFS_VOL_NAME_MAX 255
42 struct btrfs_ioctl_vol_args
{ char name
[BTRFS_VOL_NAME_MAX
]; };
43 static inline int ioctl(int fd
, int define
, void *arg
) { return 0; }
46 void print_usage(void)
48 printf("usage: btrfsctl [ -d file|dir] [ -s snap_name subvol|tree ]\n");
49 printf(" [-r size] [-A device] [-a] [-c]\n");
50 printf("\t-d filename: defragments one file\n");
51 printf("\t-d directory: defragments the entire Btree\n");
52 printf("\t-s snap_name dir: creates a new snapshot of dir\n");
53 printf("\t-S subvol_name dir: creates a new subvolume\n");
54 printf("\t-r [+-]size[gkm]: resize the FS by size amount\n");
55 printf("\t-A device: scans the device file for a Btrfs filesystem\n");
56 printf("\t-a: scans all devices for Btrfs filesystems\n");
57 printf("\t-c: forces a single FS sync\n");
58 printf("%s\n", BTRFS_BUILD_VERSION
);
62 int open_file_or_dir(char *fname
)
69 ret
= stat(fname
, &st
);
74 if (S_ISDIR(st
.st_mode
)) {
75 dirstream
= opendir(fname
);
80 fd
= dirfd(dirstream
);
82 fd
= open(fname
, O_RDWR
);
90 int main(int ac
, char **av
)
93 char *snap_location
= NULL
;
97 struct btrfs_ioctl_vol_args args
;
100 unsigned long command
= 0;
104 if (ac
== 2 && strcmp(av
[1], "-a") == 0) {
105 fprintf(stderr
, "Scanning for Btrfs filesystems\n");
106 btrfs_scan_one_dir("/dev", 1);
109 for (i
= 1; i
< ac
; i
++) {
110 if (strcmp(av
[i
], "-s") == 0) {
111 if (i
+ 1 >= ac
- 1) {
112 fprintf(stderr
, "-s requires an arg");
115 fullpath
= av
[i
+ 1];
117 snap_location
= strdup(fullpath
);
118 snap_location
= dirname(snap_location
);
120 snap_fd
= open_file_or_dir(snap_location
);
122 name
= strdup(fullpath
);
123 name
= basename(name
);
126 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
128 "snapshot name zero length or too long\n");
131 if (strchr(name
, '/')) {
133 "error: / not allowed in names\n");
136 command
= BTRFS_IOC_SNAP_CREATE
;
137 } else if (strcmp(av
[i
], "-S") == 0) {
138 if (i
+ 1 >= ac
- 1) {
139 fprintf(stderr
, "-S requires an arg");
144 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
146 "snapshot name zero length or too long\n");
149 if (strchr(name
, '/')) {
151 "error: / not allowed in names\n");
154 command
= BTRFS_IOC_SUBVOL_CREATE
;
155 } else if (strcmp(av
[i
], "-d") == 0) {
157 fprintf(stderr
, "-d requires an arg\n");
160 command
= BTRFS_IOC_DEFRAG
;
161 } else if (strcmp(av
[i
], "-A") == 0) {
163 fprintf(stderr
, "-A requires an arg\n");
166 command
= BTRFS_IOC_SCAN_DEV
;
167 } else if (strcmp(av
[i
], "-r") == 0) {
169 fprintf(stderr
, "-r requires an arg\n");
174 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
175 fprintf(stderr
, "-r size too long\n");
178 command
= BTRFS_IOC_RESIZE
;
179 } else if (strcmp(av
[i
], "-c") == 0) {
180 command
= BTRFS_IOC_SYNC
;
184 fprintf(stderr
, "no valid commands given\n");
190 if (command
== BTRFS_IOC_SCAN_DEV
) {
191 fd
= open("/dev/btrfs-control", O_RDWR
);
194 fd
= open_file_or_dir(fname
);
198 strcpy(args
.name
, name
);
202 if (command
== BTRFS_IOC_SNAP_CREATE
) {
204 ret
= ioctl(snap_fd
, command
, &args
);
206 ret
= ioctl(fd
, command
, &args
);
212 printf("operation complete\n");
214 printf("ioctl failed with error %d\n", ret
);
216 printf("%s\n", BTRFS_BUILD_VERSION
);