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 const char * const cmd_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 const char * const cmd_super_recover_usage[] = {
47 "btrfs rescue super-recover [options] <device>",
48 "Recover bad superblocks from good copies",
50 "-y Assume an answer of `yes' to all questions",
55 int cmd_chunk_recover(int argc, char *argv[])
63 int c = getopt(argc, argv, "yvh");
75 usage(cmd_chunk_recover_usage);
80 if (check_argc_exact(argc, 1))
81 usage(cmd_chunk_recover_usage);
85 ret = check_mounted(file);
87 fprintf(stderr, "Could not check mount status: %s\n",
91 fprintf(stderr, "the device is busy\n");
95 ret = btrfs_recover_chunk_tree(file, verbose, yes);
97 fprintf(stdout, "Recover the chunk tree successfully.\n");
100 fprintf(stdout, "Abort to rebuild the on-disk chunk tree.\n");
102 fprintf(stdout, "Fail to recover the chunk tree.\n");
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 int cmd_super_recover(int argc, char **argv)
123 int c = getopt(argc, argv, "vy");
134 usage(cmd_super_recover_usage);
137 argc = argc - optind;
138 if (check_argc_exact(argc, 1))
139 usage(cmd_super_recover_usage);
141 dname = argv[optind];
142 ret = check_mounted(dname);
144 fprintf(stderr, "Could not check mount status: %s\n",
148 fprintf(stderr, "the device is busy\n");
151 ret = btrfs_recover_superblocks(dname, verbose, yes);
155 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.",
162 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;
170 if (check_argc_exact(argc, 2))
171 usage(cmd_rescue_zero_log_usage);
173 devname = argv[optind];
174 ret = check_mounted(devname);
176 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
179 fprintf(stderr, "%s is currently mounted. Aborting.\n", devname);
183 root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL);
185 fprintf(stderr, "Could not open ctree\n");
189 sb = root->fs_info->super_copy;
190 printf("Clearing log on %s, previous log_root %llu, level %u\n",
192 (unsigned long long)btrfs_super_log_root(sb),
193 (unsigned)btrfs_super_log_root_level(sb));
194 trans = btrfs_start_transaction(root, 1);
195 btrfs_set_super_log_root(sb, 0);
196 btrfs_set_super_log_root_level(sb, 0);
197 btrfs_commit_transaction(trans, root);
204 static const char rescue_cmd_group_info[] =
205 "toolbox for specific rescue operations";
207 const struct cmd_group rescue_cmd_group = {
208 rescue_cmd_group_usage, rescue_cmd_group_info, {
209 { "chunk-recover", cmd_chunk_recover, cmd_chunk_recover_usage, NULL, 0},
210 { "super-recover", cmd_super_recover, cmd_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);