Fix use after free in close_ctree
[btrfs-progs-unstable.git] / btrfsctl.c
blob92bdf39f962640e1fd36439d7e1b8918b79f1e7c
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 <stdlib.h>
33 #include "kerncompat.h"
34 #include "ctree.h"
35 #include "transaction.h"
36 #include "utils.h"
37 #include "version.h"
39 #ifdef __CHECKER__
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; }
45 #endif
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);
63 exit(1);
66 static int open_file_or_dir(const char *fname)
68 int ret;
69 struct stat st;
70 DIR *dirstream;
71 int fd;
73 ret = stat(fname, &st);
74 if (ret < 0) {
75 perror("stat:");
76 exit(1);
78 if (S_ISDIR(st.st_mode)) {
79 dirstream = opendir(fname);
80 if (!dirstream) {
81 perror("opendir");
82 exit(1);
84 fd = dirfd(dirstream);
85 } else {
86 fd = open(fname, O_RDWR);
88 if (fd < 0) {
89 perror("open");
90 exit(1);
92 return fd;
94 int main(int ac, char **av)
96 char *fname = NULL;
97 char *snap_location = NULL;
98 int snap_fd = 0;
99 int fd;
100 int ret;
101 struct btrfs_ioctl_vol_args args;
102 char *name = NULL;
103 int i;
104 unsigned long command = 0;
105 int len;
106 char *pos;
107 char *fullpath;
108 u64 objectid = 0;
110 if (ac == 2 && strcmp(av[1], "-a") == 0) {
111 fprintf(stderr, "Scanning for Btrfs filesystems\n");
112 btrfs_scan_one_dir("/dev", 1);
113 exit(0);
115 for (i = 1; i < ac; i++) {
116 if (strcmp(av[i], "-s") == 0) {
117 if (i + 1 >= ac - 1) {
118 fprintf(stderr, "-s requires an arg");
119 print_usage();
121 fullpath = av[i + 1];
123 snap_location = strdup(fullpath);
124 snap_location = dirname(snap_location);
126 snap_fd = open_file_or_dir(snap_location);
128 name = strdup(fullpath);
129 name = basename(name);
130 len = strlen(name);
132 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
133 fprintf(stderr,
134 "snapshot name zero length or too long\n");
135 exit(1);
137 if (strchr(name, '/')) {
138 fprintf(stderr,
139 "error: / not allowed in names\n");
140 exit(1);
142 command = BTRFS_IOC_SNAP_CREATE;
143 } else if (strcmp(av[i], "-S") == 0) {
144 if (i + 1 >= ac - 1) {
145 fprintf(stderr, "-S requires an arg");
146 print_usage();
148 name = av[i + 1];
149 len = strlen(name);
150 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
151 fprintf(stderr,
152 "snapshot name zero length or too long\n");
153 exit(1);
155 if (strchr(name, '/')) {
156 fprintf(stderr,
157 "error: / not allowed in names\n");
158 exit(1);
160 command = BTRFS_IOC_SUBVOL_CREATE;
161 } else if (strcmp(av[i], "-d") == 0) {
162 if (i >= ac - 1) {
163 fprintf(stderr, "-d requires an arg\n");
164 print_usage();
166 command = BTRFS_IOC_DEFRAG;
167 } else if (strcmp(av[i], "-D") == 0) {
168 if (i >= ac - 1) {
169 fprintf(stderr, "-D requires an arg\n");
170 print_usage();
172 command = BTRFS_IOC_SNAP_DESTROY;
173 name = av[i + 1];
174 len = strlen(name);
175 pos = strchr(name, '/');
176 if (pos) {
177 if (*(pos + 1) == '\0')
178 *(pos) = '\0';
179 else {
180 fprintf(stderr,
181 "error: / not allowed in names\n");
182 exit(1);
185 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
186 fprintf(stderr, "-D size too long\n");
187 exit(1);
189 } else if (strcmp(av[i], "-A") == 0) {
190 if (i >= ac - 1) {
191 fprintf(stderr, "-A requires an arg\n");
192 print_usage();
194 command = BTRFS_IOC_SCAN_DEV;
195 } else if (strcmp(av[i], "-r") == 0) {
196 if (i >= ac - 1) {
197 fprintf(stderr, "-r requires an arg\n");
198 print_usage();
200 name = av[i + 1];
201 len = strlen(name);
202 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
203 fprintf(stderr, "-r size too long\n");
204 exit(1);
206 command = BTRFS_IOC_RESIZE;
207 } else if (strcmp(av[i], "-c") == 0) {
208 command = BTRFS_IOC_SYNC;
209 } else if (strcmp(av[i], "-m") == 0) {
210 command = BTRFS_IOC_DEFAULT_SUBVOL;
211 if (i == ac - 3) {
212 objectid = (unsigned long long)
213 strtoll(av[i + 1], NULL, 0);
214 if (errno == ERANGE) {
215 fprintf(stderr, "invalid tree id\n");
216 exit(1);
221 if (command == 0) {
222 fprintf(stderr, "no valid commands given\n");
223 print_usage();
224 exit(1);
226 fname = av[ac - 1];
228 if (command == BTRFS_IOC_SCAN_DEV) {
229 fd = open("/dev/btrfs-control", O_RDWR);
230 if (fd < 0) {
231 perror("failed to open /dev/btrfs-control");
232 exit(1);
234 name = fname;
235 } else {
236 fd = open_file_or_dir(fname);
239 if (name)
240 strcpy(args.name, name);
241 else
242 args.name[0] = '\0';
244 if (command == BTRFS_IOC_SNAP_CREATE) {
245 args.fd = fd;
246 ret = ioctl(snap_fd, command, &args);
247 } else if (command == BTRFS_IOC_DEFAULT_SUBVOL) {
248 printf("objectid is %llu\n", objectid);
249 ret = ioctl(fd, command, &objectid);
250 } else
251 ret = ioctl(fd, command, &args);
252 if (ret < 0) {
253 perror("ioctl:");
254 exit(1);
256 if (ret == 0) {
257 printf("operation complete\n");
258 } else {
259 printf("ioctl failed with error %d\n", ret);
261 printf("%s\n", BTRFS_BUILD_VERSION);
262 if (ret)
263 exit(1);
265 return 0;