Btrfs image tool
[btrfs-progs-unstable.git] / btrfsctl.c
blobe04979925963bad90c96238daf01c45c6f8bf593
1 /*
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.
19 #ifndef __CHECKER__
20 #include <sys/ioctl.h>
21 #include <sys/mount.h>
22 #include "ioctl.h"
23 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <dirent.h>
31 #include <libgen.h>
32 #include "kerncompat.h"
33 #include "ctree.h"
34 #include "transaction.h"
35 #include "utils.h"
36 #include "version.h"
38 #ifdef __CHECKER__
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; }
44 #endif
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);
59 exit(1);
62 int open_file_or_dir(char *fname)
64 int ret;
65 struct stat st;
66 DIR *dirstream;
67 int fd;
69 ret = stat(fname, &st);
70 if (ret < 0) {
71 perror("stat:");
72 exit(1);
74 if (S_ISDIR(st.st_mode)) {
75 dirstream = opendir(fname);
76 if (!dirstream) {
77 perror("opendir");
78 exit(1);
80 fd = dirfd(dirstream);
81 } else {
82 fd = open(fname, O_RDWR);
84 if (fd < 0) {
85 perror("open");
86 exit(1);
88 return fd;
90 int main(int ac, char **av)
92 char *fname = NULL;
93 char *snap_location = NULL;
94 int snap_fd = 0;
95 int fd;
96 int ret;
97 struct btrfs_ioctl_vol_args args;
98 char *name = NULL;
99 int i;
100 unsigned long command = 0;
101 int len;
102 char *fullpath;
104 if (ac == 2 && strcmp(av[1], "-a") == 0) {
105 fprintf(stderr, "Scanning for Btrfs filesystems\n");
106 btrfs_scan_one_dir("/dev", 1);
107 exit(0);
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");
113 print_usage();
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);
124 len = strlen(name);
126 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
127 fprintf(stderr,
128 "snapshot name zero length or too long\n");
129 exit(1);
131 if (strchr(name, '/')) {
132 fprintf(stderr,
133 "error: / not allowed in names\n");
134 exit(1);
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");
140 print_usage();
142 name = av[i + 1];
143 len = strlen(name);
144 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
145 fprintf(stderr,
146 "snapshot name zero length or too long\n");
147 exit(1);
149 if (strchr(name, '/')) {
150 fprintf(stderr,
151 "error: / not allowed in names\n");
152 exit(1);
154 command = BTRFS_IOC_SUBVOL_CREATE;
155 } else if (strcmp(av[i], "-d") == 0) {
156 if (i >= ac - 1) {
157 fprintf(stderr, "-d requires an arg\n");
158 print_usage();
160 command = BTRFS_IOC_DEFRAG;
161 } else if (strcmp(av[i], "-A") == 0) {
162 if (i >= ac - 1) {
163 fprintf(stderr, "-A requires an arg\n");
164 print_usage();
166 command = BTRFS_IOC_SCAN_DEV;
167 } else if (strcmp(av[i], "-r") == 0) {
168 if (i >= ac - 1) {
169 fprintf(stderr, "-r requires an arg\n");
170 print_usage();
172 name = av[i + 1];
173 len = strlen(name);
174 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
175 fprintf(stderr, "-r size too long\n");
176 exit(1);
178 command = BTRFS_IOC_RESIZE;
179 } else if (strcmp(av[i], "-c") == 0) {
180 command = BTRFS_IOC_SYNC;
183 if (command == 0) {
184 fprintf(stderr, "no valid commands given\n");
185 print_usage();
186 exit(1);
188 fname = av[ac - 1];
190 if (command == BTRFS_IOC_SCAN_DEV) {
191 fd = open("/dev/btrfs-control", O_RDWR);
192 name = fname;
193 } else {
194 fd = open_file_or_dir(fname);
197 if (name)
198 strcpy(args.name, name);
199 else
200 args.name[0] = '\0';
202 if (command == BTRFS_IOC_SNAP_CREATE) {
203 args.fd = fd;
204 ret = ioctl(snap_fd, command, &args);
205 } else
206 ret = ioctl(fd, command, &args);
207 if (ret < 0) {
208 perror("ioctl:");
209 exit(1);
211 if (ret == 0) {
212 printf("operation complete\n");
213 } else {
214 printf("ioctl failed with error %d\n", ret);
216 printf("%s\n", BTRFS_BUILD_VERSION);
217 if (ret)
218 exit(0);
219 else
220 exit(1);