btrfs-progs: check: repair block group accounting in lowmem mode
[btrfs-progs-unstable/devel.git] / btrfs.c
blob2d39f2ced3e81b3d425b12971306df76495583d6
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <getopt.h>
22 #include "volumes.h"
23 #include "crc32c.h"
24 #include "commands.h"
25 #include "utils.h"
26 #include "help.h"
28 static const char * const btrfs_cmd_group_usage[] = {
29 "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
30 NULL
33 static const char btrfs_cmd_group_info[] =
34 "Use --help as an argument for information on a specific group or command.";
36 static inline const char *skip_prefix(const char *str, const char *prefix)
38 size_t len = strlen(prefix);
39 return strncmp(str, prefix, len) ? NULL : str + len;
42 static int parse_one_token(const char *arg, const struct cmd_group *grp,
43 const struct cmd_struct **cmd_ret)
45 const struct cmd_struct *cmd = grp->commands;
46 const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
48 for (; cmd->token; cmd++) {
49 const char *rest;
51 rest = skip_prefix(arg, cmd->token);
52 if (!rest) {
53 if (!prefixcmp(cmd->token, arg)) {
54 if (abbrev_cmd) {
56 * If this is abbreviated, it is
57 * ambiguous. So when there is no
58 * exact match later, we need to
59 * error out.
61 ambiguous_cmd = abbrev_cmd;
63 abbrev_cmd = cmd;
65 continue;
67 if (*rest)
68 continue;
70 *cmd_ret = cmd;
71 return 0;
74 if (ambiguous_cmd)
75 return -2;
77 if (abbrev_cmd) {
78 *cmd_ret = abbrev_cmd;
79 return 0;
82 return -1;
85 static const struct cmd_struct *
86 parse_command_token(const char *arg, const struct cmd_group *grp)
88 const struct cmd_struct *cmd = NULL;
90 switch(parse_one_token(arg, grp, &cmd)) {
91 case -1:
92 help_unknown_token(arg, grp);
93 case -2:
94 help_ambiguous_token(arg, grp);
97 return cmd;
100 static void handle_help_options_next_level(const struct cmd_struct *cmd,
101 int argc, char **argv)
103 if (argc < 2)
104 return;
106 if (!strcmp(argv[1], "--help")) {
107 if (cmd->next) {
108 argc--;
109 argv++;
110 help_command_group(cmd->next, argc, argv);
111 } else {
112 usage_command(cmd, 1, 0);
115 exit(0);
119 int handle_command_group(const struct cmd_group *grp, int argc,
120 char **argv)
123 const struct cmd_struct *cmd;
125 argc--;
126 argv++;
127 if (argc < 1) {
128 usage_command_group(grp, 0, 0);
129 exit(1);
132 cmd = parse_command_token(argv[0], grp);
134 handle_help_options_next_level(cmd, argc, argv);
136 fixup_argv0(argv, cmd->token);
137 return cmd->fn(argc, argv);
140 static const struct cmd_group btrfs_cmd_group;
142 static const char * const cmd_help_usage[] = {
143 "btrfs help [--full]",
144 "Display help information",
146 "--full display detailed help on every command",
147 NULL
150 static int cmd_help(int argc, char **argv)
152 help_command_group(&btrfs_cmd_group, argc, argv);
153 return 0;
156 static const char * const cmd_version_usage[] = {
157 "btrfs version",
158 "Display btrfs-progs version",
159 NULL
162 static int cmd_version(int argc, char **argv)
164 printf("%s\n", PACKAGE_STRING);
165 return 0;
169 * Parse global options, between binary name and first non-option argument
170 * after processing all valid options (including those with arguments).
172 * Returns index to argv where parsting stopped, optind is reset to 1
174 static int handle_global_options(int argc, char **argv)
176 enum { OPT_HELP = 256, OPT_VERSION, OPT_FULL };
177 static const struct option long_options[] = {
178 { "help", no_argument, NULL, OPT_HELP },
179 { "version", no_argument, NULL, OPT_VERSION },
180 { "full", no_argument, NULL, OPT_FULL },
181 { NULL, 0, NULL, 0}
183 int shift;
185 if (argc == 0)
186 return 0;
188 opterr = 0;
189 while (1) {
190 int c;
192 c = getopt_long(argc, argv, "+", long_options, NULL);
193 if (c < 0)
194 break;
196 switch (c) {
197 case OPT_HELP: break;
198 case OPT_VERSION: break;
199 case OPT_FULL: break;
200 default:
201 fprintf(stderr, "Unknown global option: %s\n",
202 argv[optind - 1]);
203 exit(129);
207 shift = optind;
208 optind = 1;
210 return shift;
213 void handle_special_globals(int shift, int argc, char **argv)
215 int has_help = 0;
216 int has_full = 0;
217 int i;
219 for (i = 0; i < shift; i++) {
220 if (strcmp(argv[i], "--help") == 0)
221 has_help = 1;
222 else if (strcmp(argv[i], "--full") == 0)
223 has_full = 1;
226 if (has_help) {
227 if (has_full)
228 usage_command_group(&btrfs_cmd_group, 1, 0);
229 else
230 cmd_help(argc, argv);
231 exit(0);
234 for (i = 0; i < shift; i++)
235 if (strcmp(argv[i], "--version") == 0) {
236 cmd_version(argc, argv);
237 exit(0);
241 static const struct cmd_group btrfs_cmd_group = {
242 btrfs_cmd_group_usage, btrfs_cmd_group_info, {
243 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
244 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
245 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
246 { "device", cmd_device, NULL, &device_cmd_group, 0 },
247 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
248 { "check", cmd_check, cmd_check_usage, NULL, 0 },
249 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
250 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
251 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
252 { "property", cmd_property, NULL, &property_cmd_group, 0 },
253 { "send", cmd_send, cmd_send_usage, NULL, 0 },
254 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
255 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
256 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
257 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
258 { "help", cmd_help, cmd_help_usage, NULL, 0 },
259 { "version", cmd_version, cmd_version_usage, NULL, 0 },
260 NULL_CMD_STRUCT
264 int main(int argc, char **argv)
266 const struct cmd_struct *cmd;
267 const char *bname;
268 int ret;
270 btrfs_config_init();
272 if ((bname = strrchr(argv[0], '/')) != NULL)
273 bname++;
274 else
275 bname = argv[0];
277 if (!strcmp(bname, "btrfsck")) {
278 argv[0] = "check";
279 } else {
280 int shift;
282 shift = handle_global_options(argc, argv);
283 handle_special_globals(shift, argc, argv);
284 while (shift-- > 0) {
285 argc--;
286 argv++;
288 if (argc == 0) {
289 usage_command_group_short(&btrfs_cmd_group);
290 exit(1);
294 cmd = parse_command_token(argv[0], &btrfs_cmd_group);
296 handle_help_options_next_level(cmd, argc, argv);
298 crc32c_optimization_init();
300 fixup_argv0(argv, cmd->token);
302 ret = cmd->fn(argc, argv);
304 btrfs_close_all_devices();
306 exit(ret);