btrfs-progs: add option g to show generation
[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 btrfsctl_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
95 int main(int ac, char **av)
96 {
97         char *fname = NULL;
98         char *snap_location = NULL;
99         int snap_fd = 0;
100         int fd;
101         int ret;
102         struct btrfs_ioctl_vol_args args;
103         char *name = NULL;
104         int i;
105         unsigned long command = 0;
106         int len;
107         char *pos;
108         char *fullpath;
109         u64 objectid = 0;
110
111         printf( "**\n"
112                 "** WARNING: this program is considered deprecated\n"
113                 "** Please consider to switch to the btrfs utility\n"
114                 "**\n");
115         
116         if (ac == 2 && strcmp(av[1], "-a") == 0) {
117                 fprintf(stderr, "Scanning for Btrfs filesystems\n");
118                 btrfs_scan_one_dir("/dev", 1);
119                 exit(0);
120         }
121         for (i = 1; i < ac; i++) {
122                 if (strcmp(av[i], "-s") == 0) {
123                         if (i + 1 >= ac - 1) {
124                                 fprintf(stderr, "-s requires an arg");
125                                 print_usage();
126                         }
127                         fullpath = av[i + 1];
128
129                         snap_location = strdup(fullpath);
130                         snap_location = dirname(snap_location);
131
132                         snap_fd = btrfsctl_open_file_or_dir(snap_location);
133
134                         name = strdup(fullpath);
135                         name = basename(name);
136                         len = strlen(name);
137
138                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
139                                 fprintf(stderr,
140                                      "snapshot name zero length or too long\n");
141                                 exit(1);
142                         }
143                         if (strchr(name, '/')) {
144                                 fprintf(stderr,
145                                         "error: / not allowed in names\n");
146                                 exit(1);
147                         }
148                         command = BTRFS_IOC_SNAP_CREATE;
149                 } else if (strcmp(av[i], "-S") == 0) {
150                         if (i + 1 >= ac - 1) {
151                                 fprintf(stderr, "-S requires an arg");
152                                 print_usage();
153                         }
154                         name = av[i + 1];
155                         len = strlen(name);
156                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
157                                 fprintf(stderr,
158                                      "snapshot name zero length or too long\n");
159                                 exit(1);
160                         }
161                         if (strchr(name, '/')) {
162                                 fprintf(stderr,
163                                         "error: / not allowed in names\n");
164                                 exit(1);
165                         }
166                         command = BTRFS_IOC_SUBVOL_CREATE;
167                 } else if (strcmp(av[i], "-d") == 0) {
168                         if (i >= ac - 1) {
169                                 fprintf(stderr, "-d requires an arg\n");
170                                 print_usage();
171                         }
172                         command = BTRFS_IOC_DEFRAG;
173                 } else if (strcmp(av[i], "-D") == 0) {
174                         if (i >= ac - 1) {
175                                 fprintf(stderr, "-D requires an arg\n");
176                                 print_usage();
177                         }
178                         command = BTRFS_IOC_SNAP_DESTROY;
179                         name = av[i + 1];
180                         len = strlen(name);
181                         pos = strchr(name, '/');
182                         if (pos) {
183                                 if (*(pos + 1) == '\0')
184                                         *(pos) = '\0';
185                                 else {
186                                         fprintf(stderr,
187                                                 "error: / not allowed in names\n");
188                                         exit(1);
189                                 }
190                         }
191                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
192                                 fprintf(stderr, "-D size too long\n");
193                                 exit(1);
194                         }
195                 } else if (strcmp(av[i], "-A") == 0) {
196                         if (i >= ac - 1) {
197                                 fprintf(stderr, "-A requires an arg\n");
198                                 print_usage();
199                         }
200                         command = BTRFS_IOC_SCAN_DEV;
201                 } else if (strcmp(av[i], "-r") == 0) {
202                         if (i >= ac - 1) {
203                                 fprintf(stderr, "-r requires an arg\n");
204                                 print_usage();
205                         }
206                         name = av[i + 1];
207                         len = strlen(name);
208                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
209                                 fprintf(stderr, "-r size too long\n");
210                                 exit(1);
211                         }
212                         command = BTRFS_IOC_RESIZE;
213                 } else if (strcmp(av[i], "-c") == 0) {
214                         command = BTRFS_IOC_SYNC;
215                 } else if (strcmp(av[i], "-m") == 0) {
216                         command = BTRFS_IOC_DEFAULT_SUBVOL;
217                         if (i == ac - 3) {
218                                 objectid = (unsigned long long)
219                                             strtoll(av[i + 1], NULL, 0);
220                                 if (errno == ERANGE) {
221                                         fprintf(stderr, "invalid tree id\n");
222                                         exit(1);
223                                 }
224                         }
225                 }
226         }
227         if (command == 0) {
228                 fprintf(stderr, "no valid commands given\n");
229                 print_usage();
230                 exit(1);
231         }
232         fname = av[ac - 1];
233
234         if (command == BTRFS_IOC_SCAN_DEV) {
235                 fd = open("/dev/btrfs-control", O_RDWR);
236                 if (fd < 0) {
237                         perror("failed to open /dev/btrfs-control");
238                         exit(1);
239                 }
240                 name = fname;
241          } else {
242                 fd = btrfsctl_open_file_or_dir(fname);
243          }
244
245         if (name) {
246                 strncpy(args.name, name, BTRFS_PATH_NAME_MAX + 1);
247                 args.name[BTRFS_PATH_NAME_MAX] = 0;
248         } else
249                 args.name[0] = '\0';
250
251         if (command == BTRFS_IOC_SNAP_CREATE) {
252                 args.fd = fd;
253                 ret = ioctl(snap_fd, command, &args);
254         } else if (command == BTRFS_IOC_DEFAULT_SUBVOL) {
255                 printf("objectid is %llu\n", (unsigned long long)objectid);
256                 ret = ioctl(fd, command, &objectid);
257         } else
258                 ret = ioctl(fd, command, &args);
259         if (ret < 0) {
260                 perror("ioctl:");
261                 exit(1);
262         }
263         if (ret == 0) {
264                 printf("operation complete\n");
265         } else {
266                 printf("ioctl failed with error %d\n", ret);
267         }
268         printf("%s\n", BTRFS_BUILD_VERSION);
269         if (ret)
270                 exit(1);
271
272         return 0;
273 }
274