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(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.",
42 "-y Assume an answer of `yes' to all questions",
48 static int cmd_rescue_chunk_recover(int argc
, char *argv
[])
57 int c
= getopt(argc
, argv
, "yvh");
69 usage(cmd_rescue_chunk_recover_usage
);
73 if (check_argc_exact(argc
- optind
, 1))
74 usage(cmd_rescue_chunk_recover_usage
);
78 ret
= check_mounted(file
);
80 error("could not check mount status: %s", strerror(-ret
));
83 error("the device is busy");
87 ret
= btrfs_recover_chunk_tree(file
, verbose
, yes
);
89 fprintf(stdout
, "Chunk tree recovered successfully\n");
92 fprintf(stdout
, "Chunk tree recovery aborted\n");
94 fprintf(stdout
, "Chunk tree recovery failed\n");
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",
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
)
125 int c
= getopt(argc
, argv
, "vy");
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
);
145 error("could not check mount status: %s", strerror(-ret
));
148 error("the device is busy");
151 ret
= btrfs_recover_superblocks(dname
, verbose
, yes
);
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.",
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
;
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
);
178 error("could not check mount status: %s", strerror(-ret
));
181 error("%s is currently mounted", devname
);
186 root
= open_ctree(devname
, 0, OPEN_CTREE_WRITES
| OPEN_CTREE_PARTIAL
);
188 error("could not open ctree");
192 sb
= root
->fs_info
->super_copy
;
193 printf("Clearing log on %s, previous log_root %llu, level %u\n",
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
);
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",
215 static int cmd_rescue_fix_device_size(int argc
, char **argv
)
217 struct btrfs_fs_info
*fs_info
;
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
);
229 error("could not check mount status: %s", strerror(-ret
));
232 error("%s is currently mounted", devname
);
237 fs_info
= open_ctree_fs_info(devname
, 0, 0, 0, OPEN_CTREE_WRITES
|
240 error("could not open btrfs");
245 ret
= btrfs_fix_device_and_super_size(fs_info
);
248 close_ctree(fs_info
->tree_root
);
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},
269 int cmd_rescue(int argc
, char **argv
)
271 return handle_command_group(&rescue_cmd_group
, argc
, argv
);