c40088ad374e2e69447a93b05fd0379a083833b8
[platform/upstream/btrfs-progs.git] / cmds-rescue.c
1 /*
2  * Copyright (C) 2013 SUSE.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include "kerncompat.h"
20
21 #include <getopt.h>
22 #include "ctree.h"
23 #include "volumes.h"
24 #include "transaction.h"
25 #include "disk-io.h"
26 #include "commands.h"
27 #include "utils.h"
28 #include "help.h"
29
30 static const char * const rescue_cmd_group_usage[] = {
31         "btrfs rescue <command> [options] <path>",
32         NULL
33 };
34
35 int btrfs_recover_chunk_tree(char *path, int verbose, int yes);
36 int btrfs_recover_superblocks(char *path, int verbose, int yes);
37
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.",
41         "",
42         "-y     Assume an answer of `yes' to all questions",
43         "-v     Verbose mode",
44         "-h     Help",
45         NULL
46 };
47
48 static int cmd_rescue_chunk_recover(int argc, char *argv[])
49 {
50         int ret = 0;
51         char *file;
52         int yes = 0;
53         int verbose = 0;
54
55         while (1) {
56                 int c = getopt(argc, argv, "yvh");
57                 if (c < 0)
58                         break;
59                 switch (c) {
60                 case 'y':
61                         yes = 1;
62                         break;
63                 case 'v':
64                         verbose = 1;
65                         break;
66                 case 'h':
67                 default:
68                         usage(cmd_rescue_chunk_recover_usage);
69                 }
70         }
71
72         if (check_argc_exact(argc - optind, 1))
73                 usage(cmd_rescue_chunk_recover_usage);
74
75         file = argv[optind];
76
77         ret = check_mounted(file);
78         if (ret < 0) {
79                 error("could not check mount status: %s", strerror(-ret));
80                 return 1;
81         } else if (ret) {
82                 error("the device is busy");
83                 return 1;
84         }
85
86         ret = btrfs_recover_chunk_tree(file, verbose, yes);
87         if (!ret) {
88                 fprintf(stdout, "Chunk tree recovered successfully\n");
89         } else if (ret > 0) {
90                 ret = 0;
91                 fprintf(stdout, "Chunk tree recovery aborted\n");
92         } else {
93                 fprintf(stdout, "Chunk tree recovery failed\n");
94         }
95         return ret;
96 }
97
98 static const char * const cmd_rescue_super_recover_usage[] = {
99         "btrfs rescue super-recover [options] <device>",
100         "Recover bad superblocks from good copies",
101         "",
102         "-y     Assume an answer of `yes' to all questions",
103         "-v     Verbose mode",
104         NULL
105 };
106
107 /*
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
114  */
115 static int cmd_rescue_super_recover(int argc, char **argv)
116 {
117         int ret;
118         int verbose = 0;
119         int yes = 0;
120         char *dname;
121
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);
135                 }
136         }
137         if (check_argc_exact(argc - optind, 1))
138                 usage(cmd_rescue_super_recover_usage);
139
140         dname = argv[optind];
141         ret = check_mounted(dname);
142         if (ret < 0) {
143                 error("could not check mount status: %s", strerror(-ret));
144                 return 1;
145         } else if (ret) {
146                 error("the device is busy");
147                 return 1;
148         }
149         ret = btrfs_recover_superblocks(dname, verbose, yes);
150         return ret;
151 }
152
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.",
156         "",
157         NULL
158 };
159
160 static int cmd_rescue_zero_log(int argc, char **argv)
161 {
162         struct btrfs_root *root;
163         struct btrfs_trans_handle *trans;
164         struct btrfs_super_block *sb;
165         char *devname;
166         int ret;
167
168         clean_args_no_options(argc, argv, cmd_rescue_zero_log_usage);
169
170         if (check_argc_exact(argc, 2))
171                 usage(cmd_rescue_zero_log_usage);
172
173         devname = argv[optind];
174         ret = check_mounted(devname);
175         if (ret < 0) {
176                 error("could not check mount status: %s", strerror(-ret));
177                 goto out;
178         } else if (ret) {
179                 error("%s is currently mounted", devname);
180                 ret = -EBUSY;
181                 goto out;
182         }
183
184         root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL);
185         if (!root) {
186                 error("could not open ctree");
187                 return 1;
188         }
189
190         sb = root->fs_info->super_copy;
191         printf("Clearing log on %s, previous log_root %llu, level %u\n",
192                         devname,
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);
200         close_ctree(root);
201
202 out:
203         return !!ret;
204 }
205
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",
209         "",
210         NULL
211 };
212
213 static int cmd_rescue_fix_device_size(int argc, char **argv)
214 {
215         struct btrfs_fs_info *fs_info;
216         char *devname;
217         int ret;
218
219         clean_args_no_options(argc, argv, cmd_rescue_fix_device_size_usage);
220
221         if (check_argc_exact(argc, 2))
222                 usage(cmd_rescue_fix_device_size_usage);
223
224         devname = argv[optind];
225         ret = check_mounted(devname);
226         if (ret < 0) {
227                 error("could not check mount status: %s", strerror(-ret));
228                 goto out;
229         } else if (ret) {
230                 error("%s is currently mounted", devname);
231                 ret = -EBUSY;
232                 goto out;
233         }
234
235         fs_info = open_ctree_fs_info(devname, 0, 0, 0, OPEN_CTREE_WRITES |
236                                      OPEN_CTREE_PARTIAL);
237         if (!fs_info) {
238                 error("could not open btrfs");
239                 ret = -EIO;
240                 goto out;
241         }
242
243         ret = btrfs_fix_device_and_super_size(fs_info);
244         if (ret > 0)
245                 ret = 0;
246         close_ctree(fs_info->tree_root);
247 out:
248         return !!ret;
249 }
250
251 static const char rescue_cmd_group_info[] =
252 "toolbox for specific rescue operations";
253
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},
263                 NULL_CMD_STRUCT
264         }
265 };
266
267 int cmd_rescue(int argc, char **argv)
268 {
269         return handle_command_group(&rescue_cmd_group, argc, argv);
270 }