Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / cmds-rescue.c
blob38c4ab9b2ef61a696c89f49207765d5d55929619
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(const char *path, int verbose, int yes);
36 int btrfs_recover_superblocks(const 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 optind = 0;
56 while (1) {
57 int c = getopt(argc, argv, "yvh");
58 if (c < 0)
59 break;
60 switch (c) {
61 case 'y':
62 yes = 1;
63 break;
64 case 'v':
65 verbose = 1;
66 break;
67 case 'h':
68 default:
69 usage(cmd_rescue_chunk_recover_usage);
73 if (check_argc_exact(argc - optind, 1))
74 usage(cmd_rescue_chunk_recover_usage);
76 file = argv[optind];
78 ret = check_mounted(file);
79 if (ret < 0) {
80 error("could not check mount status: %s", strerror(-ret));
81 return 1;
82 } else if (ret) {
83 error("the device is busy");
84 return 1;
87 ret = btrfs_recover_chunk_tree(file, verbose, yes);
88 if (!ret) {
89 fprintf(stdout, "Chunk tree recovered successfully\n");
90 } else if (ret > 0) {
91 ret = 0;
92 fprintf(stdout, "Chunk tree recovery aborted\n");
93 } else {
94 fprintf(stdout, "Chunk tree recovery failed\n");
96 return ret;
99 static const char * const cmd_rescue_super_recover_usage[] = {
100 "btrfs rescue super-recover [options] <device>",
101 "Recover bad superblocks from good copies",
103 "-y Assume an answer of `yes' to all questions",
104 "-v Verbose mode",
105 NULL
109 * return codes:
110 * 0 : All superblocks are valid, no need to recover
111 * 1 : Usage or syntax error
112 * 2 : Recover all bad superblocks successfully
113 * 3 : Fail to Recover bad supeblocks
114 * 4 : Abort to recover bad superblocks
116 static int cmd_rescue_super_recover(int argc, char **argv)
118 int ret;
119 int verbose = 0;
120 int yes = 0;
121 char *dname;
123 optind = 0;
124 while (1) {
125 int c = getopt(argc, argv, "vy");
126 if (c < 0)
127 break;
128 switch (c) {
129 case 'v':
130 verbose = 1;
131 break;
132 case 'y':
133 yes = 1;
134 break;
135 default:
136 usage(cmd_rescue_super_recover_usage);
139 if (check_argc_exact(argc - optind, 1))
140 usage(cmd_rescue_super_recover_usage);
142 dname = argv[optind];
143 ret = check_mounted(dname);
144 if (ret < 0) {
145 error("could not check mount status: %s", strerror(-ret));
146 return 1;
147 } else if (ret) {
148 error("the device is busy");
149 return 1;
151 ret = btrfs_recover_superblocks(dname, verbose, yes);
152 return ret;
155 static const char * const cmd_rescue_zero_log_usage[] = {
156 "btrfs rescue zero-log <device>",
157 "Clear the tree log. Usable if it's corrupted and prevents mount.",
159 NULL
162 static int cmd_rescue_zero_log(int argc, char **argv)
164 struct btrfs_root *root;
165 struct btrfs_trans_handle *trans;
166 struct btrfs_super_block *sb;
167 char *devname;
168 int ret;
170 clean_args_no_options(argc, argv, cmd_rescue_zero_log_usage);
172 if (check_argc_exact(argc, 2))
173 usage(cmd_rescue_zero_log_usage);
175 devname = argv[optind];
176 ret = check_mounted(devname);
177 if (ret < 0) {
178 error("could not check mount status: %s", strerror(-ret));
179 goto out;
180 } else if (ret) {
181 error("%s is currently mounted", devname);
182 ret = -EBUSY;
183 goto out;
186 root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL);
187 if (!root) {
188 error("could not open ctree");
189 return 1;
192 sb = root->fs_info->super_copy;
193 printf("Clearing log on %s, previous log_root %llu, level %u\n",
194 devname,
195 (unsigned long long)btrfs_super_log_root(sb),
196 (unsigned)btrfs_super_log_root_level(sb));
197 trans = btrfs_start_transaction(root, 1);
198 BUG_ON(IS_ERR(trans));
199 btrfs_set_super_log_root(sb, 0);
200 btrfs_set_super_log_root_level(sb, 0);
201 btrfs_commit_transaction(trans, root);
202 close_ctree(root);
204 out:
205 return !!ret;
208 static const char * const cmd_rescue_fix_device_size_usage[] = {
209 "btrfs rescue fix-device-size <device>",
210 "Re-align device and super block sizes. Usable if newer kernel refuse to mount it due to mismatch super size",
212 NULL
215 static int cmd_rescue_fix_device_size(int argc, char **argv)
217 struct btrfs_fs_info *fs_info;
218 char *devname;
219 int ret;
221 clean_args_no_options(argc, argv, cmd_rescue_fix_device_size_usage);
223 if (check_argc_exact(argc, 2))
224 usage(cmd_rescue_fix_device_size_usage);
226 devname = argv[optind];
227 ret = check_mounted(devname);
228 if (ret < 0) {
229 error("could not check mount status: %s", strerror(-ret));
230 goto out;
231 } else if (ret) {
232 error("%s is currently mounted", devname);
233 ret = -EBUSY;
234 goto out;
237 fs_info = open_ctree_fs_info(devname, 0, 0, 0, OPEN_CTREE_WRITES |
238 OPEN_CTREE_PARTIAL);
239 if (!fs_info) {
240 error("could not open btrfs");
241 ret = -EIO;
242 goto out;
245 ret = btrfs_fix_device_and_super_size(fs_info);
246 if (ret > 0)
247 ret = 0;
248 close_ctree(fs_info->tree_root);
249 out:
250 return !!ret;
253 static const char rescue_cmd_group_info[] =
254 "toolbox for specific rescue operations";
256 const struct cmd_group rescue_cmd_group = {
257 rescue_cmd_group_usage, rescue_cmd_group_info, {
258 { "chunk-recover", cmd_rescue_chunk_recover,
259 cmd_rescue_chunk_recover_usage, NULL, 0},
260 { "super-recover", cmd_rescue_super_recover,
261 cmd_rescue_super_recover_usage, NULL, 0},
262 { "zero-log", cmd_rescue_zero_log, cmd_rescue_zero_log_usage, NULL, 0},
263 { "fix-device-size", cmd_rescue_fix_device_size,
264 cmd_rescue_fix_device_size_usage, NULL, 0},
265 NULL_CMD_STRUCT
269 int cmd_rescue(int argc, char **argv)
271 return handle_command_group(&rescue_cmd_group, argc, argv);