82c5f30df5624a6d09b7eaae1bec41849bd227a8
[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 "kerncompat.h"
32 #include "ctree.h"
33 #include "transaction.h"
34 #include "utils.h"
35 #include "version.h"
36
37 #ifdef __CHECKER__
38 #define BLKGETSIZE64 0
39 #define BTRFS_IOC_SNAP_CREATE 0
40 #define BTRFS_VOL_NAME_MAX 255
41 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
42 static inline int ioctl(int fd, int define, void *arg) { return 0; }
43 #endif
44
45 void print_usage(void)
46 {
47         printf("usage: btrfsctl [ -d file|dir] [ -s snap_name subvol|tree ]\n");
48         printf("                [-r size] [-A device] [-a] [-c]\n");
49         printf("\t-d filename: defragments one file\n");
50         printf("\t-d directory: defragments the entire Btree\n");
51         printf("\t-s snap_name: existing_subvol creates a new snapshot\n");
52         printf("\t-s snap_name: tree_root creates a new subvolume\n");
53         printf("\t-r [+-]size[gkm]: resize the FS by size amount\n");
54         printf("\t-A device: scans the device file for a Btrfs filesystem\n");
55         printf("\t-a: scans all devices for Btrfs filesystems\n");
56         printf("\t-c: forces a single FS sync\n");
57         printf("%s\n", BTRFS_BUILD_VERSION);
58         exit(1);
59 }
60
61 int main(int ac, char **av)
62 {
63         char *fname;
64         int fd;
65         int ret;
66         struct btrfs_ioctl_vol_args args;
67         char *name = NULL;
68         int i;
69         struct stat st;
70         DIR *dirstream;
71         unsigned long command = 0;
72         int len;
73
74         if (ac == 2 && strcmp(av[1], "-a") == 0) {
75                 fprintf(stderr, "Scanning for Btrfs filesystems\n");
76                 btrfs_scan_one_dir("/dev", 1);
77                 exit(0);
78         }
79         for (i = 1; i < ac; i++) {
80                 if (strcmp(av[i], "-s") == 0) {
81                         if (i + 1 >= ac - 1) {
82                                 fprintf(stderr, "-s requires an arg");
83                                 print_usage();
84                         }
85                         name = av[i + 1];
86                         len = strlen(name);
87                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
88                                 fprintf(stderr,
89                                      "snapshot name zero length or too long\n");
90                                 exit(1);
91                         }
92                         if (strchr(name, '/')) {
93                                 fprintf(stderr,
94                                         "error: / not allowed in names\n");
95                                 exit(1);
96                         }
97                         command = BTRFS_IOC_SNAP_CREATE;
98                 } else if (strcmp(av[i], "-d") == 0) {
99                         if (i >= ac - 1) {
100                                 fprintf(stderr, "-d requires an arg\n");
101                                 print_usage();
102                         }
103                         command = BTRFS_IOC_DEFRAG;
104                 } else if (strcmp(av[i], "-A") == 0) {
105                         if (i >= ac - 1) {
106                                 fprintf(stderr, "-A requires an arg\n");
107                                 print_usage();
108                         }
109                         command = BTRFS_IOC_SCAN_DEV;
110                 } else if (strcmp(av[i], "-r") == 0) {
111                         if (i >= ac - 1) {
112                                 fprintf(stderr, "-r requires an arg\n");
113                                 print_usage();
114                         }
115                         name = av[i + 1];
116                         len = strlen(name);
117                         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
118                                 fprintf(stderr, "-r size too long\n");
119                                 exit(1);
120                         }
121                         command = BTRFS_IOC_RESIZE;
122                 } else if (strcmp(av[i], "-c") == 0) {
123                         command = BTRFS_IOC_SYNC;
124                 }
125         }
126         if (command == 0) {
127                 fprintf(stderr, "no valid commands given\n");
128                 print_usage();
129                 exit(1);
130         }
131         fname = av[ac - 1];
132         ret = stat(fname, &st);
133         if (ret < 0) {
134                 perror("stat:");
135                 exit(1);
136         }
137         if (S_ISDIR(st.st_mode)) {
138                 dirstream = opendir(fname);
139                 if (!dirstream) {
140                         perror("opendir");
141                         exit(1);
142                 }
143                 fd = dirfd(dirstream);
144         } else if (command == BTRFS_IOC_SCAN_DEV) {
145                 fd = open("/dev/btrfs-control", O_RDWR);
146                 name = fname;
147         } else {
148                 fd = open(fname, O_RDWR);
149         }
150         if (fd < 0) {
151                 perror("open");
152                 exit(1);
153         }
154         if (name)
155                 strcpy(args.name, name);
156         else
157                 args.name[0] = '\0';
158         ret = ioctl(fd, command, &args);
159         if (ret < 0) {
160                 perror("ioctl:");
161                 exit(1);
162         }
163         if (ret == 0) {
164                 printf("operation complete\n");
165         } else {
166                 printf("ioctl failed with error %d\n", ret);
167         }
168         printf("%s\n", BTRFS_BUILD_VERSION);
169         if (ret)
170                 exit(0);
171         else
172                 exit(1);
173 }
174