btrfs-progs: corrupt-block: Convert -K flag argument handling to common function
[btrfs-progs-unstable/devel.git] / cmds-rescue.c
blobc40088ad374e2e69447a93b05fd0379a083833b8
1 /*
2 * Copyright (C) 2013 SUSE. All rights reserved.
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.
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.
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.
19 #include "kerncompat.h"
21 #include <getopt.h>
22 #include "ctree.h"
23 #include "volumes.h"
24 #include "transaction.h"
25 #include "disk-io.h"
26 #include "commands.h"
27 #include "utils.h"
28 #include "help.h"
30 static const char * const rescue_cmd_group_usage[] = {
31 "btrfs rescue <command> [options] <path>",
32 NULL
35 int btrfs_recover_chunk_tree(char *path, int verbose, int yes);
36 int btrfs_recover_superblocks(char *path, int verbose, int yes);
38 static const char * const cmd_rescue_chunk_recover_usage[] = {
39 "btrfs rescue chunk-recover [options] <device>",
40 "Recover the chunk tree by scanning the devices one by one.",
41 "",
42 "-y Assume an answer of `yes' to all questions",
43 "-v Verbose mode",
44 "-h Help",
45 NULL
48 static int cmd_rescue_chunk_recover(int argc, char *argv[])
50 int ret = 0;
51 char *file;
52 int yes = 0;
53 int verbose = 0;
55 while (1) {
56 int c = getopt(argc, argv, "yvh");
57 if (c < 0)
58 break;
59 switch (c) {
60 case 'y':
61 yes = 1;
62 break;
63 case 'v':
64 verbose = 1;
65 break;
66 case 'h':
67 default:
68 usage(cmd_rescue_chunk_recover_usage);
72 if (check_argc_exact(argc - optind, 1))
73 usage(cmd_rescue_chunk_recover_usage);
75 file = argv[optind];
77 ret = check_mounted(file);
78 if (ret < 0) {
79 error("could not check mount status: %s", strerror(-ret));
80 return 1;
81 } else if (ret) {
82 error("the device is busy");
83 return 1;
86 ret = btrfs_recover_chunk_tree(file, verbose, yes);
87 if (!ret) {
88 fprintf(stdout, "Chunk tree recovered successfully\n");
89 } else if (ret > 0) {
90 ret = 0;
91 fprintf(stdout, "Chunk tree recovery aborted\n");
92 } else {
93 fprintf(stdout, "Chunk tree recovery failed\n");
95 return ret;
98 static const char * const cmd_rescue_super_recover_usage[] = {
99 "btrfs rescue super-recover [options] <device>",
100 "Recover bad superblocks from good copies",
102 "-y Assume an answer of `yes' to all questions",
103 "-v Verbose mode",
104 NULL
108 * return codes:
109 * 0 : All superblocks are valid, no need to recover
110 * 1 : Usage or syntax error
111 * 2 : Recover all bad superblocks successfully
112 * 3 : Fail to Recover bad supeblocks
113 * 4 : Abort to recover bad superblocks
115 static int cmd_rescue_super_recover(int argc, char **argv)
117 int ret;
118 int verbose = 0;
119 int yes = 0;
120 char *dname;
122 while (1) {
123 int c = getopt(argc, argv, "vy");
124 if (c < 0)
125 break;
126 switch (c) {
127 case 'v':
128 verbose = 1;
129 break;
130 case 'y':
131 yes = 1;
132 break;
133 default:
134 usage(cmd_rescue_super_recover_usage);
137 if (check_argc_exact(argc - optind, 1))
138 usage(cmd_rescue_super_recover_usage);
140 dname = argv[optind];
141 ret = check_mounted(dname);
142 if (ret < 0) {
143 error("could not check mount status: %s", strerror(-ret));
144 return 1;
145 } else if (ret) {
146 error("the device is busy");
147 return 1;
149 ret = btrfs_recover_superblocks(dname, verbose, yes);
150 return ret;
153 static const char * const cmd_rescue_zero_log_usage[] = {
154 "btrfs rescue zero-log <device>",
155 "Clear the tree log. Usable if it's corrupted and prevents mount.",
157 NULL
160 static int cmd_rescue_zero_log(int argc, char **argv)
162 struct btrfs_root *root;
163 struct btrfs_trans_handle *trans;
164 struct btrfs_super_block *sb;
165 char *devname;
166 int ret;
168 clean_args_no_options(argc, argv, cmd_rescue_zero_log_usage);
170 if (check_argc_exact(argc, 2))
171 usage(cmd_rescue_zero_log_usage);
173 devname = argv[optind];
174 ret = check_mounted(devname);
175 if (ret < 0) {
176 error("could not check mount status: %s", strerror(-ret));
177 goto out;
178 } else if (ret) {
179 error("%s is currently mounted", devname);
180 ret = -EBUSY;
181 goto out;
184 root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL);
185 if (!root) {
186 error("could not open ctree");
187 return 1;
190 sb = root->fs_info->super_copy;
191 printf("Clearing log on %s, previous log_root %llu, level %u\n",
192 devname,
193 (unsigned long long)btrfs_super_log_root(sb),
194 (unsigned)btrfs_super_log_root_level(sb));
195 trans = btrfs_start_transaction(root, 1);
196 BUG_ON(IS_ERR(trans));
197 btrfs_set_super_log_root(sb, 0);
198 btrfs_set_super_log_root_level(sb, 0);
199 btrfs_commit_transaction(trans, root);
200 close_ctree(root);
202 out:
203 return !!ret;
206 static const char * const cmd_rescue_fix_device_size_usage[] = {
207 "btrfs rescue fix-device-size <device>",
208 "Re-align device and super block sizes. Usable if newer kernel refuse to mount it due to mismatch super size",
210 NULL
213 static int cmd_rescue_fix_device_size(int argc, char **argv)
215 struct btrfs_fs_info *fs_info;
216 char *devname;
217 int ret;
219 clean_args_no_options(argc, argv, cmd_rescue_fix_device_size_usage);
221 if (check_argc_exact(argc, 2))
222 usage(cmd_rescue_fix_device_size_usage);
224 devname = argv[optind];
225 ret = check_mounted(devname);
226 if (ret < 0) {
227 error("could not check mount status: %s", strerror(-ret));
228 goto out;
229 } else if (ret) {
230 error("%s is currently mounted", devname);
231 ret = -EBUSY;
232 goto out;
235 fs_info = open_ctree_fs_info(devname, 0, 0, 0, OPEN_CTREE_WRITES |
236 OPEN_CTREE_PARTIAL);
237 if (!fs_info) {
238 error("could not open btrfs");
239 ret = -EIO;
240 goto out;
243 ret = btrfs_fix_device_and_super_size(fs_info);
244 if (ret > 0)
245 ret = 0;
246 close_ctree(fs_info->tree_root);
247 out:
248 return !!ret;
251 static const char rescue_cmd_group_info[] =
252 "toolbox for specific rescue operations";
254 const struct cmd_group rescue_cmd_group = {
255 rescue_cmd_group_usage, rescue_cmd_group_info, {
256 { "chunk-recover", cmd_rescue_chunk_recover,
257 cmd_rescue_chunk_recover_usage, NULL, 0},
258 { "super-recover", cmd_rescue_super_recover,
259 cmd_rescue_super_recover_usage, NULL, 0},
260 { "zero-log", cmd_rescue_zero_log, cmd_rescue_zero_log_usage, NULL, 0},
261 { "fix-device-size", cmd_rescue_fix_device_size,
262 cmd_rescue_fix_device_size_usage, NULL, 0},
263 NULL_CMD_STRUCT
267 int cmd_rescue(int argc, char **argv)
269 return handle_command_group(&rescue_cmd_group, argc, argv);