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 static 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] [-D dir .]\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("\t-D: delete snapshot\n");
59 printf("%s\n", BTRFS_BUILD_VERSION);
63 static int open_file_or_dir(const char *fname)
70 ret = stat(fname, &st);
75 if (S_ISDIR(st.st_mode)) {
76 dirstream = opendir(fname);
81 fd = dirfd(dirstream);
83 fd = open(fname, O_RDWR);
91 int main(int ac, char **av)
94 char *snap_location = NULL;
98 struct btrfs_ioctl_vol_args args;
101 unsigned long command = 0;
105 if (ac == 2 && strcmp(av[1], "-a") == 0) {
106 fprintf(stderr, "Scanning for Btrfs filesystems\n");
107 btrfs_scan_one_dir("/dev", 1);
110 for (i = 1; i < ac; i++) {
111 if (strcmp(av[i], "-s") == 0) {
112 if (i + 1 >= ac - 1) {
113 fprintf(stderr, "-s requires an arg");
116 fullpath = av[i + 1];
118 snap_location = strdup(fullpath);
119 snap_location = dirname(snap_location);
121 snap_fd = open_file_or_dir(snap_location);
123 name = strdup(fullpath);
124 name = basename(name);
127 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
129 "snapshot name zero length or too long\n");
132 if (strchr(name, '/')) {
134 "error: / not allowed in names\n");
137 command = BTRFS_IOC_SNAP_CREATE;
138 } else if (strcmp(av[i], "-S") == 0) {
139 if (i + 1 >= ac - 1) {
140 fprintf(stderr, "-S requires an arg");
145 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
147 "snapshot name zero length or too long\n");
150 if (strchr(name, '/')) {
152 "error: / not allowed in names\n");
155 command = BTRFS_IOC_SUBVOL_CREATE;
156 } else if (strcmp(av[i], "-d") == 0) {
158 fprintf(stderr, "-d requires an arg\n");
161 command = BTRFS_IOC_DEFRAG;
162 } else if (strcmp(av[i], "-D") == 0) {
164 fprintf(stderr, "-D requires an arg\n");
167 command = BTRFS_IOC_SNAP_DESTROY;
170 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
171 fprintf(stderr, "-D size too long\n");
174 } else if (strcmp(av[i], "-A") == 0) {
176 fprintf(stderr, "-A requires an arg\n");
179 command = BTRFS_IOC_SCAN_DEV;
180 } else if (strcmp(av[i], "-r") == 0) {
182 fprintf(stderr, "-r requires an arg\n");
187 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
188 fprintf(stderr, "-r size too long\n");
191 command = BTRFS_IOC_RESIZE;
192 } else if (strcmp(av[i], "-c") == 0) {
193 command = BTRFS_IOC_SYNC;
197 fprintf(stderr, "no valid commands given\n");
203 if (command == BTRFS_IOC_SCAN_DEV) {
204 fd = open("/dev/btrfs-control", O_RDWR);
206 perror("failed to open /dev/btrfs-control");
211 fd = open_file_or_dir(fname);
215 strcpy(args.name, name);
219 if (command == BTRFS_IOC_SNAP_CREATE) {
221 ret = ioctl(snap_fd, command, &args);
223 ret = ioctl(fd, command, &args);
229 printf("operation complete\n");
231 printf("ioctl failed with error %d\n", ret);
233 printf("%s\n", BTRFS_BUILD_VERSION);