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"
24 #include "transaction.h"
30 static const char * const rescue_cmd_group_usage[] = {
31 "btrfs rescue <command> [options] <path>",
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.",
42 "-y Assume an answer of `yes' to all questions",
48 static int cmd_rescue_chunk_recover(int argc, char *argv[])
56 int c = getopt(argc, argv, "yvh");
68 usage(cmd_rescue_chunk_recover_usage);
72 if (check_argc_exact(argc - optind, 1))
73 usage(cmd_rescue_chunk_recover_usage);
77 ret = check_mounted(file);
79 error("could not check mount status: %s", strerror(-ret));
82 error("the device is busy");
86 ret = btrfs_recover_chunk_tree(file, verbose, yes);
88 fprintf(stdout, "Chunk tree recovered successfully\n");
91 fprintf(stdout, "Chunk tree recovery aborted\n");
93 fprintf(stdout, "Chunk tree recovery failed\n");
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",
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)
123 int c = getopt(argc, argv, "vy");
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);
143 error("could not check mount status: %s", strerror(-ret));
146 error("the device is busy");
149 ret = btrfs_recover_superblocks(dname, verbose, yes);
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.",
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;
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);
176 error("could not check mount status: %s", strerror(-ret));
179 error("%s is currently mounted", devname);
184 root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL);
186 error("could not open ctree");
190 sb = root->fs_info->super_copy;
191 printf("Clearing log on %s, previous log_root %llu, level %u\n",
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);
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",
213 static int cmd_rescue_fix_device_size(int argc, char **argv)
215 struct btrfs_fs_info *fs_info;
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);
227 error("could not check mount status: %s", strerror(-ret));
230 error("%s is currently mounted", devname);
235 fs_info = open_ctree_fs_info(devname, 0, 0, 0, OPEN_CTREE_WRITES |
238 error("could not open btrfs");
243 ret = btrfs_fix_device_and_super_size(fs_info);
246 close_ctree(fs_info->tree_root);
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},
267 int cmd_rescue(int argc, char **argv)
269 return handle_command_group(&rescue_cmd_group, argc, argv);