btrfs-progs: docs: mkfs, implications of DUP on devices
[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 "transaction.h"
24 #include "disk-io.h"
25 #include "commands.h"
26 #include "utils.h"
27
28 static const char * const rescue_cmd_group_usage[] = {
29         "btrfs rescue <command> [options] <path>",
30         NULL
31 };
32
33 int btrfs_recover_chunk_tree(char *path, int verbose, int yes);
34 int btrfs_recover_superblocks(char *path, int verbose, int yes);
35
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.",
39         "",
40         "-y     Assume an answer of `yes' to all questions",
41         "-v     Verbose mode",
42         "-h     Help",
43         NULL
44 };
45
46 static 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
53 };
54
55 static int cmd_rescue_chunk_recover(int argc, char *argv[])
56 {
57         int ret = 0;
58         char *file;
59         int yes = 0;
60         int verbose = 0;
61
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);
76                 }
77         }
78
79         argc = argc - optind;
80         if (check_argc_exact(argc, 1))
81                 usage(cmd_rescue_chunk_recover_usage);
82
83         file = argv[optind];
84
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;
93         }
94
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");
103         }
104         return ret;
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         argc = argc - optind;
138         if (check_argc_exact(argc, 1))
139                 usage(cmd_rescue_super_recover_usage);
140
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;
150         }
151         ret = btrfs_recover_superblocks(dname, verbose, yes);
152         return ret;
153 }
154
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.",
158         "",
159         NULL
160 };
161
162 static int cmd_rescue_zero_log(int argc, char **argv)
163 {
164         struct btrfs_root *root;
165         struct btrfs_trans_handle *trans;
166         struct btrfs_super_block *sb;
167         char *devname;
168         int ret;
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                 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;
181         }
182
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;
187         }
188
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);
199
200 out:
201         return !!ret;
202 }
203
204 static const char rescue_cmd_group_info[] =
205 "toolbox for specific rescue operations";
206
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
215         }
216 };
217
218 int cmd_rescue(int argc, char **argv)
219 {
220         return handle_command_group(&rescue_cmd_group, argc, argv);
221 }