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>
33 #include "kerncompat.h"
35 #include "transaction.h"
40 #define BLKGETSIZE64 0
41 #define BTRFS_IOC_SNAP_CREATE 0
42 #define BTRFS_VOL_NAME_MAX 255
43 struct btrfs_ioctl_vol_args
{ char name
[BTRFS_VOL_NAME_MAX
]; };
44 static inline int ioctl(int fd
, int define
, void *arg
) { return 0; }
47 static void print_usage(void)
49 printf("usage: btrfsctl [ -d file|dir] [ -s snap_name subvol|tree ]\n");
50 printf(" [-r size] [-A device] [-a] [-c] [-D dir .]\n");
51 printf("\t-d filename: defragments one file\n");
52 printf("\t-d directory: defragments the entire Btree\n");
53 printf("\t-s snap_name dir: creates a new snapshot of dir\n");
54 printf("\t-S subvol_name dir: creates a new subvolume\n");
55 printf("\t-r [+-]size[gkm]: resize the FS by size amount\n");
56 printf("\t-A device: scans the device file for a Btrfs filesystem\n");
57 printf("\t-a: scans all devices for Btrfs filesystems\n");
58 printf("\t-c: forces a single FS sync\n");
59 printf("\t-D: delete snapshot\n");
60 printf("\t-m [tree id] directory: set the default mounted subvolume"
61 " to the [tree id] or the directory\n");
62 printf("%s\n", BTRFS_BUILD_VERSION
);
66 static int open_file_or_dir(const char *fname
)
73 ret
= stat(fname
, &st
);
78 if (S_ISDIR(st
.st_mode
)) {
79 dirstream
= opendir(fname
);
84 fd
= dirfd(dirstream
);
86 fd
= open(fname
, O_RDWR
);
94 int main(int ac
, char **av
)
97 char *snap_location
= NULL
;
101 struct btrfs_ioctl_vol_args args
;
104 unsigned long command
= 0;
109 if (ac
== 2 && strcmp(av
[1], "-a") == 0) {
110 fprintf(stderr
, "Scanning for Btrfs filesystems\n");
111 btrfs_scan_one_dir("/dev", 1);
114 for (i
= 1; i
< ac
; i
++) {
115 if (strcmp(av
[i
], "-s") == 0) {
116 if (i
+ 1 >= ac
- 1) {
117 fprintf(stderr
, "-s requires an arg");
120 fullpath
= av
[i
+ 1];
122 snap_location
= strdup(fullpath
);
123 snap_location
= dirname(snap_location
);
125 snap_fd
= open_file_or_dir(snap_location
);
127 name
= strdup(fullpath
);
128 name
= basename(name
);
131 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
133 "snapshot name zero length or too long\n");
136 if (strchr(name
, '/')) {
138 "error: / not allowed in names\n");
141 command
= BTRFS_IOC_SNAP_CREATE
;
142 } else if (strcmp(av
[i
], "-S") == 0) {
143 if (i
+ 1 >= ac
- 1) {
144 fprintf(stderr
, "-S requires an arg");
149 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
151 "snapshot name zero length or too long\n");
154 if (strchr(name
, '/')) {
156 "error: / not allowed in names\n");
159 command
= BTRFS_IOC_SUBVOL_CREATE
;
160 } else if (strcmp(av
[i
], "-d") == 0) {
162 fprintf(stderr
, "-d requires an arg\n");
165 command
= BTRFS_IOC_DEFRAG
;
166 } else if (strcmp(av
[i
], "-D") == 0) {
168 fprintf(stderr
, "-D requires an arg\n");
171 command
= BTRFS_IOC_SNAP_DESTROY
;
174 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
175 fprintf(stderr
, "-D size too long\n");
178 } else if (strcmp(av
[i
], "-A") == 0) {
180 fprintf(stderr
, "-A requires an arg\n");
183 command
= BTRFS_IOC_SCAN_DEV
;
184 } else if (strcmp(av
[i
], "-r") == 0) {
186 fprintf(stderr
, "-r requires an arg\n");
191 if (len
== 0 || len
>= BTRFS_VOL_NAME_MAX
) {
192 fprintf(stderr
, "-r size too long\n");
195 command
= BTRFS_IOC_RESIZE
;
196 } else if (strcmp(av
[i
], "-c") == 0) {
197 command
= BTRFS_IOC_SYNC
;
198 } else if (strcmp(av
[i
], "-m") == 0) {
199 command
= BTRFS_IOC_DEFAULT_SUBVOL
;
201 objectid
= (unsigned long long)
202 strtoll(av
[i
+ 1], NULL
, 0);
203 if (errno
== ERANGE
) {
204 fprintf(stderr
, "invalid tree id\n");
211 fprintf(stderr
, "no valid commands given\n");
217 if (command
== BTRFS_IOC_SCAN_DEV
) {
218 fd
= open("/dev/btrfs-control", O_RDWR
);
220 perror("failed to open /dev/btrfs-control");
225 fd
= open_file_or_dir(fname
);
229 strcpy(args
.name
, name
);
233 if (command
== BTRFS_IOC_SNAP_CREATE
) {
235 ret
= ioctl(snap_fd
, command
, &args
);
236 } else if (command
== BTRFS_IOC_DEFAULT_SUBVOL
) {
237 printf("objectid is %llu\n", objectid
);
238 ret
= ioctl(fd
, command
, &objectid
);
240 ret
= ioctl(fd
, command
, &args
);
246 printf("operation complete\n");
248 printf("ioctl failed with error %d\n", ret
);
250 printf("%s\n", BTRFS_BUILD_VERSION
);