Btrfs-progs: bugfix for subvolume parent determination in btrfs send
[platform/upstream/btrfs-progs.git] / btrfsctl.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
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"
38
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
46
47 static void print_usage(void)
48 {
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);
64 }
65
66 static int open_file_or_dir(const char *fname)
67 {
68         int ret;
69         struct stat st;
70         DIR *dirstream;
71         int fd;
72
73         ret = stat(fname, &st);
74         if (ret < 0) {
75                 perror("stat:");
76                 exit(1);
77         }
78         if (S_ISDIR(st.st_mode)) {
79                 dirstream = opendir(fname);
80                 if (!dirstream) {
81                         perror("opendir");
82                         exit(1);
83                 }
84                 fd = dirfd(dirstream);
85         } else {
86                 fd = open(fname, O_RDWR);
87         }
88         if (fd < 0) {
89                 perror("open");
90                 exit(1);
91         }
92         return fd;
93 }
94 int main(int ac, char **av)
95 {
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;
109
110         printf( "**\n"
111                 "** WARNING: this program is considered deprecated\n"
112                 "** Please consider to switch to the btrfs utility\n"
113                 "**\n");
114         
115         if (ac == 2 && strcmp(av[1], "-a") == 0) {
116                 fprintf(stderr, "Scanning for Btrfs filesystems\n");
117                 btrfs_scan_one_dir("/dev", 1);
118                 exit(0);
119         }
120         for (i = 1; i < ac; i++) {
121                 if (strcmp(av[i], "-s") == 0) {
122                         if (i + 1 >= ac - 1) {
123                                 fprintf(stderr, "-s requires an arg");
124                                 print_usage();
125                         }
126                         fullpath = av[i + 1];
127
128                         snap_location = strdup(fullpath);
129                         snap_location = dirname(snap_location);
130
131                         snap_fd = open_file_or_dir(snap_location);
132
133                         name = strdup(fullpath);
134                         name = basename(name);
135                         len = strlen(name);
136
137                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
138                                 fprintf(stderr,
139                                      "snapshot name zero length or too long\n");
140                                 exit(1);
141                         }
142                         if (strchr(name, '/')) {
143                                 fprintf(stderr,
144                                         "error: / not allowed in names\n");
145                                 exit(1);
146                         }
147                         command = BTRFS_IOC_SNAP_CREATE;
148                 } else if (strcmp(av[i], "-S") == 0) {
149                         if (i + 1 >= ac - 1) {
150                                 fprintf(stderr, "-S requires an arg");
151                                 print_usage();
152                         }
153                         name = av[i + 1];
154                         len = strlen(name);
155                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
156                                 fprintf(stderr,
157                                      "snapshot name zero length or too long\n");
158                                 exit(1);
159                         }
160                         if (strchr(name, '/')) {
161                                 fprintf(stderr,
162                                         "error: / not allowed in names\n");
163                                 exit(1);
164                         }
165                         command = BTRFS_IOC_SUBVOL_CREATE;
166                 } else if (strcmp(av[i], "-d") == 0) {
167                         if (i >= ac - 1) {
168                                 fprintf(stderr, "-d requires an arg\n");
169                                 print_usage();
170                         }
171                         command = BTRFS_IOC_DEFRAG;
172                 } else if (strcmp(av[i], "-D") == 0) {
173                         if (i >= ac - 1) {
174                                 fprintf(stderr, "-D requires an arg\n");
175                                 print_usage();
176                         }
177                         command = BTRFS_IOC_SNAP_DESTROY;
178                         name = av[i + 1];
179                         len = strlen(name);
180                         pos = strchr(name, '/');
181                         if (pos) {
182                                 if (*(pos + 1) == '\0')
183                                         *(pos) = '\0';
184                                 else {
185                                         fprintf(stderr,
186                                                 "error: / not allowed in names\n");
187                                         exit(1);
188                                 }
189                         }
190                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
191                                 fprintf(stderr, "-D size too long\n");
192                                 exit(1);
193                         }
194                 } else if (strcmp(av[i], "-A") == 0) {
195                         if (i >= ac - 1) {
196                                 fprintf(stderr, "-A requires an arg\n");
197                                 print_usage();
198                         }
199                         command = BTRFS_IOC_SCAN_DEV;
200                 } else if (strcmp(av[i], "-r") == 0) {
201                         if (i >= ac - 1) {
202                                 fprintf(stderr, "-r requires an arg\n");
203                                 print_usage();
204                         }
205                         name = av[i + 1];
206                         len = strlen(name);
207                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
208                                 fprintf(stderr, "-r size too long\n");
209                                 exit(1);
210                         }
211                         command = BTRFS_IOC_RESIZE;
212                 } else if (strcmp(av[i], "-c") == 0) {
213                         command = BTRFS_IOC_SYNC;
214                 } else if (strcmp(av[i], "-m") == 0) {
215                         command = BTRFS_IOC_DEFAULT_SUBVOL;
216                         if (i == ac - 3) {
217                                 objectid = (unsigned long long)
218                                             strtoll(av[i + 1], NULL, 0);
219                                 if (errno == ERANGE) {
220                                         fprintf(stderr, "invalid tree id\n");
221                                         exit(1);
222                                 }
223                         }
224                 }
225         }
226         if (command == 0) {
227                 fprintf(stderr, "no valid commands given\n");
228                 print_usage();
229                 exit(1);
230         }
231         fname = av[ac - 1];
232
233         if (command == BTRFS_IOC_SCAN_DEV) {
234                 fd = open("/dev/btrfs-control", O_RDWR);
235                 if (fd < 0) {
236                         perror("failed to open /dev/btrfs-control");
237                         exit(1);
238                 }
239                 name = fname;
240          } else {
241                 fd = open_file_or_dir(fname);
242          }
243
244         if (name) {
245                 strncpy(args.name, name, BTRFS_PATH_NAME_MAX + 1);
246                 args.name[BTRFS_PATH_NAME_MAX] = 0;
247         } else
248                 args.name[0] = '\0';
249
250         if (command == BTRFS_IOC_SNAP_CREATE) {
251                 args.fd = fd;
252                 ret = ioctl(snap_fd, command, &args);
253         } else if (command == BTRFS_IOC_DEFAULT_SUBVOL) {
254                 printf("objectid is %llu\n", (unsigned long long)objectid);
255                 ret = ioctl(fd, command, &objectid);
256         } else
257                 ret = ioctl(fd, command, &args);
258         if (ret < 0) {
259                 perror("ioctl:");
260                 exit(1);
261         }
262         if (ret == 0) {
263                 printf("operation complete\n");
264         } else {
265                 printf("ioctl failed with error %d\n", ret);
266         }
267         printf("%s\n", BTRFS_BUILD_VERSION);
268         if (ret)
269                 exit(1);
270
271         return 0;
272 }
273