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"
23 #include "transaction.h"
28 static const char * const rescue_cmd_group_usage[] = {
29 "btrfs rescue <command> [options] <path>",
33 int btrfs_recover_chunk_tree(char *path, int verbose, int yes);
34 int btrfs_recover_superblocks(char *path, int verbose, int yes);
36 static const char * const cmd_rescue_chunk_recover_usage[] = {
37 "btrfs rescue chunk-recover [options] <device>",
38 "Recover the chunk tree by scanning the devices one by one.",
40 "-y Assume an answer of `yes' to all questions",
46 static int cmd_rescue_chunk_recover(int argc, char *argv[])
54 int c = getopt(argc, argv, "yvh");
66 usage(cmd_rescue_chunk_recover_usage);
71 if (check_argc_exact(argc, 1))
72 usage(cmd_rescue_chunk_recover_usage);
76 ret = check_mounted(file);
78 error("could not check mount status: %s", strerror(-ret));
81 error("the device is busy");
85 ret = btrfs_recover_chunk_tree(file, verbose, yes);
87 fprintf(stdout, "Chunk tree recovered successfully\n");
90 fprintf(stdout, "Chunk tree recovery aborted\n");
92 fprintf(stdout, "Chunk tree recovery failed\n");
97 static const char * const cmd_rescue_super_recover_usage[] = {
98 "btrfs rescue super-recover [options] <device>",
99 "Recover bad superblocks from good copies",
101 "-y Assume an answer of `yes' to all questions",
108 * 0 : All superblocks are valid, no need to recover
109 * 1 : Usage or syntax error
110 * 2 : Recover all bad superblocks successfully
111 * 3 : Fail to Recover bad supeblocks
112 * 4 : Abort to recover bad superblocks
114 static int cmd_rescue_super_recover(int argc, char **argv)
122 int c = getopt(argc, argv, "vy");
133 usage(cmd_rescue_super_recover_usage);
136 argc = argc - optind;
137 if (check_argc_exact(argc, 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 if (check_argc_exact(argc, 2))
169 usage(cmd_rescue_zero_log_usage);
171 devname = argv[optind];
172 ret = check_mounted(devname);
174 error("could not check mount status: %s", strerror(-ret));
177 error("%s is currently mounted", devname);
181 root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL);
183 error("could not open ctree");
187 sb = root->fs_info->super_copy;
188 printf("Clearing log on %s, previous log_root %llu, level %u\n",
190 (unsigned long long)btrfs_super_log_root(sb),
191 (unsigned)btrfs_super_log_root_level(sb));
192 trans = btrfs_start_transaction(root, 1);
193 btrfs_set_super_log_root(sb, 0);
194 btrfs_set_super_log_root_level(sb, 0);
195 btrfs_commit_transaction(trans, root);
202 static const char rescue_cmd_group_info[] =
203 "toolbox for specific rescue operations";
205 const struct cmd_group rescue_cmd_group = {
206 rescue_cmd_group_usage, rescue_cmd_group_info, {
207 { "chunk-recover", cmd_rescue_chunk_recover,
208 cmd_rescue_chunk_recover_usage, NULL, 0},
209 { "super-recover", cmd_rescue_super_recover,
210 cmd_rescue_super_recover_usage, NULL, 0},
211 { "zero-log", cmd_rescue_zero_log, cmd_rescue_zero_log_usage, NULL, 0},
216 int cmd_rescue(int argc, char **argv)
218 return handle_command_group(&rescue_cmd_group, argc, argv);