btrfs-progs: update btrfs-completion
[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 #include "help.h"
28
29 static const char * const rescue_cmd_group_usage[] = {
30         "btrfs rescue <command> [options] <path>",
31         NULL
32 };
33
34 int btrfs_recover_chunk_tree(char *path, int verbose, int yes);
35 int btrfs_recover_superblocks(char *path, int verbose, int yes);
36
37 static const char * const cmd_rescue_chunk_recover_usage[] = {
38         "btrfs rescue chunk-recover [options] <device>",
39         "Recover the chunk tree by scanning the devices one by one.",
40         "",
41         "-y     Assume an answer of `yes' to all questions",
42         "-v     Verbose mode",
43         "-h     Help",
44         NULL
45 };
46
47 static int cmd_rescue_chunk_recover(int argc, char *argv[])
48 {
49         int ret = 0;
50         char *file;
51         int yes = 0;
52         int verbose = 0;
53
54         while (1) {
55                 int c = getopt(argc, argv, "yvh");
56                 if (c < 0)
57                         break;
58                 switch (c) {
59                 case 'y':
60                         yes = 1;
61                         break;
62                 case 'v':
63                         verbose = 1;
64                         break;
65                 case 'h':
66                 default:
67                         usage(cmd_rescue_chunk_recover_usage);
68                 }
69         }
70
71         if (check_argc_exact(argc - optind, 1))
72                 usage(cmd_rescue_chunk_recover_usage);
73
74         file = argv[optind];
75
76         ret = check_mounted(file);
77         if (ret < 0) {
78                 error("could not check mount status: %s", strerror(-ret));
79                 return 1;
80         } else if (ret) {
81                 error("the device is busy");
82                 return 1;
83         }
84
85         ret = btrfs_recover_chunk_tree(file, verbose, yes);
86         if (!ret) {
87                 fprintf(stdout, "Chunk tree recovered successfully\n");
88         } else if (ret > 0) {
89                 ret = 0;
90                 fprintf(stdout, "Chunk tree recovery aborted\n");
91         } else {
92                 fprintf(stdout, "Chunk tree recovery failed\n");
93         }
94         return ret;
95 }
96
97 static const char * const cmd_rescue_super_recover_usage[] = {
98         "btrfs rescue super-recover [options] <device>",
99         "Recover bad superblocks from good copies",
100         "",
101         "-y     Assume an answer of `yes' to all questions",
102         "-v     Verbose mode",
103         NULL
104 };
105
106 /*
107  * return codes:
108  *   0 : All superblocks are valid, no need to recover
109  *   1 : Usage or syntax error
110  *   2 : Recover all bad superblocks successfully
111  *   3 : Fail to Recover bad supeblocks
112  *   4 : Abort to recover bad superblocks
113  */
114 static int cmd_rescue_super_recover(int argc, char **argv)
115 {
116         int ret;
117         int verbose = 0;
118         int yes = 0;
119         char *dname;
120
121         while (1) {
122                 int c = getopt(argc, argv, "vy");
123                 if (c < 0)
124                         break;
125                 switch (c) {
126                 case 'v':
127                         verbose = 1;
128                         break;
129                 case 'y':
130                         yes = 1;
131                         break;
132                 default:
133                         usage(cmd_rescue_super_recover_usage);
134                 }
135         }
136         if (check_argc_exact(argc - optind, 1))
137                 usage(cmd_rescue_super_recover_usage);
138
139         dname = argv[optind];
140         ret = check_mounted(dname);
141         if (ret < 0) {
142                 error("could not check mount status: %s", strerror(-ret));
143                 return 1;
144         } else if (ret) {
145                 error("the device is busy");
146                 return 1;
147         }
148         ret = btrfs_recover_superblocks(dname, verbose, yes);
149         return ret;
150 }
151
152 static const char * const cmd_rescue_zero_log_usage[] = {
153         "btrfs rescue zero-log <device>",
154         "Clear the tree log. Usable if it's corrupted and prevents mount.",
155         "",
156         NULL
157 };
158
159 static int cmd_rescue_zero_log(int argc, char **argv)
160 {
161         struct btrfs_root *root;
162         struct btrfs_trans_handle *trans;
163         struct btrfs_super_block *sb;
164         char *devname;
165         int ret;
166
167         clean_args_no_options(argc, argv, cmd_rescue_zero_log_usage);
168
169         if (check_argc_exact(argc, 2))
170                 usage(cmd_rescue_zero_log_usage);
171
172         devname = argv[optind];
173         ret = check_mounted(devname);
174         if (ret < 0) {
175                 error("could not check mount status: %s", strerror(-ret));
176                 goto out;
177         } else if (ret) {
178                 error("%s is currently mounted", devname);
179                 ret = -EBUSY;
180         }
181
182         root = open_ctree(devname, 0, OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL);
183         if (!root) {
184                 error("could not open ctree");
185                 return 1;
186         }
187
188         sb = root->fs_info->super_copy;
189         printf("Clearing log on %s, previous log_root %llu, level %u\n",
190                         devname,
191                         (unsigned long long)btrfs_super_log_root(sb),
192                         (unsigned)btrfs_super_log_root_level(sb));
193         trans = btrfs_start_transaction(root, 1);
194         BUG_ON(IS_ERR(trans));
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 }