btrfs-progs: use calloc instead of malloc+memset for tree roots
[btrfs-progs-unstable/devel.git] / cmds-rescue.c
blobdd7c01eaa956a94428b63da6e280e30878beeb3b
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 "transaction.h"
24 #include "disk-io.h"
25 #include "commands.h"
26 #include "utils.h"
28 static const char * const rescue_cmd_group_usage[] = {
29 "btrfs rescue <command> [options] <path>",
30 NULL
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_rescue_chunk_recover_usage[] = {
37 "btrfs rescue chunk-recover [options] <device>",
38 "Recover the chunk tree by scanning the devices one by one.",
39 "",
40 "-y Assume an answer of `yes' to all questions",
41 "-v Verbose mode",
42 "-h Help",
43 NULL
46 const char * const cmd_rescue_super_recover_usage[] = {
47 "btrfs rescue super-recover [options] <device>",
48 "Recover bad superblocks from good copies",
49 "",
50 "-y Assume an answer of `yes' to all questions",
51 "-v Verbose mode",
52 NULL
55 int cmd_rescue_chunk_recover(int argc, char *argv[])
57 int ret = 0;
58 char *file;
59 int yes = 0;
60 int verbose = 0;
62 while (1) {
63 int c = getopt(argc, argv, "yvh");
64 if (c < 0)
65 break;
66 switch (c) {
67 case 'y':
68 yes = 1;
69 break;
70 case 'v':
71 verbose = 1;
72 break;
73 case 'h':
74 default:
75 usage(cmd_rescue_chunk_recover_usage);
79 argc = argc - optind;
80 if (check_argc_exact(argc, 1))
81 usage(cmd_rescue_chunk_recover_usage);
83 file = argv[optind];
85 ret = check_mounted(file);
86 if (ret < 0) {
87 fprintf(stderr, "Could not check mount status: %s\n",
88 strerror(-ret));
89 return 1;
90 } else if (ret) {
91 fprintf(stderr, "the device is busy\n");
92 return 1;
95 ret = btrfs_recover_chunk_tree(file, verbose, yes);
96 if (!ret) {
97 fprintf(stdout, "Recover the chunk tree successfully.\n");
98 } else if (ret > 0) {
99 ret = 0;
100 fprintf(stdout, "Abort to rebuild the on-disk chunk tree.\n");
101 } else {
102 fprintf(stdout, "Fail to recover the chunk tree.\n");
104 return ret;
108 * return codes:
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_rescue_super_recover(int argc, char **argv)
117 int ret;
118 int verbose = 0;
119 int yes = 0;
120 char *dname;
122 while (1) {
123 int c = getopt(argc, argv, "vy");
124 if (c < 0)
125 break;
126 switch (c) {
127 case 'v':
128 verbose = 1;
129 break;
130 case 'y':
131 yes = 1;
132 break;
133 default:
134 usage(cmd_rescue_super_recover_usage);
137 argc = argc - optind;
138 if (check_argc_exact(argc, 1))
139 usage(cmd_rescue_super_recover_usage);
141 dname = argv[optind];
142 ret = check_mounted(dname);
143 if (ret < 0) {
144 fprintf(stderr, "Could not check mount status: %s\n",
145 strerror(-ret));
146 return 1;
147 } else if (ret) {
148 fprintf(stderr, "the device is busy\n");
149 return 1;
151 ret = btrfs_recover_superblocks(dname, verbose, yes);
152 return ret;
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.",
159 NULL
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;
167 char *devname;
168 int ret;
170 if (check_argc_exact(argc, 2))
171 usage(cmd_rescue_zero_log_usage);
173 devname = argv[optind];
174 ret = check_mounted(devname);
175 if (ret < 0) {
176 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
177 goto out;
178 } else if (ret) {
179 fprintf(stderr, "%s is currently mounted. Aborting.\n", devname);
180 ret = -EBUSY;
183 root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL);
184 if (!root) {
185 fprintf(stderr, "Could not open ctree\n");
186 return 1;
189 sb = root->fs_info->super_copy;
190 printf("Clearing log on %s, previous log_root %llu, level %u\n",
191 devname,
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);
198 close_ctree(root);
200 out:
201 return !!ret;
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_rescue_chunk_recover,
210 cmd_rescue_chunk_recover_usage, NULL, 0},
211 { "super-recover", cmd_rescue_super_recover,
212 cmd_rescue_super_recover_usage, NULL, 0},
213 { "zero-log", cmd_rescue_zero_log, cmd_rescue_zero_log_usage, NULL, 0},
214 NULL_CMD_STRUCT
218 int cmd_rescue(int argc, char **argv)
220 return handle_command_group(&rescue_cmd_group, argc, argv);