66c4e89f5303ba3595c48679cf1a7c6fe380bf7e
[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 "kerncompat.h"
33 #include "ctree.h"
34 #include "transaction.h"
35 #include "utils.h"
36 #include "version.h"
37
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
45
46 static void print_usage(void)
47 {
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);
60         exit(1);
61 }
62
63 static int open_file_or_dir(const char *fname)
64 {
65         int ret;
66         struct stat st;
67         DIR *dirstream;
68         int fd;
69
70         ret = stat(fname, &st);
71         if (ret < 0) {
72                 perror("stat:");
73                 exit(1);
74         }
75         if (S_ISDIR(st.st_mode)) {
76                 dirstream = opendir(fname);
77                 if (!dirstream) {
78                         perror("opendir");
79                         exit(1);
80                 }
81                 fd = dirfd(dirstream);
82         } else {
83                 fd = open(fname, O_RDWR);
84         }
85         if (fd < 0) {
86                 perror("open");
87                 exit(1);
88         }
89         return fd;
90 }
91 int main(int ac, char **av)
92 {
93         char *fname = NULL;
94         char *snap_location = NULL;
95         int snap_fd = 0;
96         int fd;
97         int ret;
98         struct btrfs_ioctl_vol_args args;
99         char *name = NULL;
100         int i;
101         unsigned long command = 0;
102         int len;
103         char *fullpath;
104
105         if (ac == 2 && strcmp(av[1], "-a") == 0) {
106                 fprintf(stderr, "Scanning for Btrfs filesystems\n");
107                 btrfs_scan_one_dir("/dev", 1);
108                 exit(0);
109         }
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");
114                                 print_usage();
115                         }
116                         fullpath = av[i + 1];
117
118                         snap_location = strdup(fullpath);
119                         snap_location = dirname(snap_location);
120
121                         snap_fd = open_file_or_dir(snap_location);
122
123                         name = strdup(fullpath);
124                         name = basename(name);
125                         len = strlen(name);
126
127                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
128                                 fprintf(stderr,
129                                      "snapshot name zero length or too long\n");
130                                 exit(1);
131                         }
132                         if (strchr(name, '/')) {
133                                 fprintf(stderr,
134                                         "error: / not allowed in names\n");
135                                 exit(1);
136                         }
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");
141                                 print_usage();
142                         }
143                         name = av[i + 1];
144                         len = strlen(name);
145                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
146                                 fprintf(stderr,
147                                      "snapshot name zero length or too long\n");
148                                 exit(1);
149                         }
150                         if (strchr(name, '/')) {
151                                 fprintf(stderr,
152                                         "error: / not allowed in names\n");
153                                 exit(1);
154                         }
155                         command = BTRFS_IOC_SUBVOL_CREATE;
156                 } else if (strcmp(av[i], "-d") == 0) {
157                         if (i >= ac - 1) {
158                                 fprintf(stderr, "-d requires an arg\n");
159                                 print_usage();
160                         }
161                         command = BTRFS_IOC_DEFRAG;
162                 } else if (strcmp(av[i], "-D") == 0) {
163                         if (i >= ac - 1) {
164                                 fprintf(stderr, "-D requires an arg\n");
165                                 print_usage();
166                         }
167                         command = BTRFS_IOC_SNAP_DESTROY;
168                         name = av[i + 1];
169                         len = strlen(name);
170                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
171                                 fprintf(stderr, "-D size too long\n");
172                                 exit(1);
173                         }
174                 } else if (strcmp(av[i], "-A") == 0) {
175                         if (i >= ac - 1) {
176                                 fprintf(stderr, "-A requires an arg\n");
177                                 print_usage();
178                         }
179                         command = BTRFS_IOC_SCAN_DEV;
180                 } else if (strcmp(av[i], "-r") == 0) {
181                         if (i >= ac - 1) {
182                                 fprintf(stderr, "-r requires an arg\n");
183                                 print_usage();
184                         }
185                         name = av[i + 1];
186                         len = strlen(name);
187                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
188                                 fprintf(stderr, "-r size too long\n");
189                                 exit(1);
190                         }
191                         command = BTRFS_IOC_RESIZE;
192                 } else if (strcmp(av[i], "-c") == 0) {
193                         command = BTRFS_IOC_SYNC;
194                 }
195         }
196         if (command == 0) {
197                 fprintf(stderr, "no valid commands given\n");
198                 print_usage();
199                 exit(1);
200         }
201         fname = av[ac - 1];
202
203         if (command == BTRFS_IOC_SCAN_DEV) {
204                 fd = open("/dev/btrfs-control", O_RDWR);
205                 if (fd < 0) {
206                         perror("failed to open /dev/btrfs-control");
207                         exit(1);
208                 }
209                 name = fname;
210          } else {
211                 fd = open_file_or_dir(fname);
212          }
213
214         if (name)
215                 strcpy(args.name, name);
216         else
217                 args.name[0] = '\0';
218
219         if (command == BTRFS_IOC_SNAP_CREATE) {
220                 args.fd = fd;
221                 ret = ioctl(snap_fd, command, &args);
222         } else
223                 ret = ioctl(fd, command, &args);
224         if (ret < 0) {
225                 perror("ioctl:");
226                 exit(1);
227         }
228         if (ret == 0) {
229                 printf("operation complete\n");
230         } else {
231                 printf("ioctl failed with error %d\n", ret);
232         }
233         printf("%s\n", BTRFS_BUILD_VERSION);
234         if (ret)
235                 exit(0);
236         else
237                 exit(1);
238 }
239