btrfs-progs: mkfs, align help strings
[platform/upstream/btrfs-progs.git] / super-recover.c
1 /*
2  * Copyright (C) 2013 FUJITSU LIMITED.  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 <stdio.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <uuid/uuid.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <ctype.h>
27 #include <getopt.h>
28
29 #include "kerncompat.h"
30 #include "ctree.h"
31 #include "disk-io.h"
32 #include "list.h"
33 #include "utils.h"
34 #include "crc32c.h"
35 #include "volumes.h"
36 #include "commands.h"
37
38 struct btrfs_recover_superblock {
39         struct btrfs_fs_devices *fs_devices;
40
41         struct list_head good_supers;
42         struct list_head bad_supers;
43
44         u64 max_generation;
45 };
46
47 struct super_block_record {
48         struct list_head list;
49
50         char *device_name;
51         struct btrfs_super_block sb;
52
53         u64 bytenr;
54 };
55
56 static
57 void init_recover_superblock(struct btrfs_recover_superblock *recover)
58 {
59         INIT_LIST_HEAD(&recover->good_supers);
60         INIT_LIST_HEAD(&recover->bad_supers);
61
62         recover->fs_devices = NULL;
63         recover->max_generation = 0;
64 }
65
66 static
67 void free_recover_superblock(struct btrfs_recover_superblock *recover)
68 {
69         struct super_block_record *record;
70
71         if (!recover->fs_devices)
72                 return;
73
74         while (!list_empty(&recover->good_supers)) {
75                 record = list_entry(recover->good_supers.next,
76                                 struct super_block_record, list);
77                 list_del_init(&record->list);
78                 free(record->device_name);
79                 free(record);
80         }
81
82         while (!list_empty(&recover->bad_supers)) {
83                 record = list_entry(recover->bad_supers.next,
84                                 struct super_block_record, list);
85                 list_del_init(&record->list);
86                 free(record->device_name);
87                 free(record);
88         }
89 }
90
91 static int check_super(u64 bytenr, struct btrfs_super_block *sb)
92 {
93         int csum_size = btrfs_super_csum_size(sb);
94         char result[csum_size];
95         u32 crc = ~(u32)0;
96
97         if (btrfs_super_bytenr(sb) != bytenr)
98                 return 0;
99         if (sb->magic != cpu_to_le64(BTRFS_MAGIC))
100                 return 0;
101
102         crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE,
103                         crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
104         btrfs_csum_final(crc, result);
105
106         return !memcmp(sb, &result, csum_size);
107 }
108
109 static int add_superblock_record(struct btrfs_super_block *sb, char *fname,
110                         u64 bytenr, struct list_head *head)
111 {
112         struct super_block_record *record;
113
114         record = malloc(sizeof(struct super_block_record));
115         if (!record)
116                 return -ENOMEM;
117
118         record->device_name = strdup(fname);
119         if (!record->device_name) {
120                 free(record);
121                 return -ENOMEM;
122         }
123         memcpy(&record->sb, sb, sizeof(*sb));
124         record->bytenr = bytenr;
125         list_add_tail(&record->list, head);
126
127         return 0;
128 }
129
130 static int
131 read_dev_supers(char *filename, struct btrfs_recover_superblock *recover)
132 {
133         int i, ret, fd;
134         u8 buf[BTRFS_SUPER_INFO_SIZE];
135         u64 max_gen, bytenr;
136         /* just ignore errno that were set in btrfs_scan_fs_devices() */
137         errno = 0;
138
139         struct btrfs_super_block *sb = (struct btrfs_super_block *)buf;
140
141         fd = open(filename, O_RDONLY, 0666);
142         if (fd < 0)
143                 return -errno;
144
145         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
146                 bytenr = btrfs_sb_offset(i);
147                 ret = pread64(fd, buf, sizeof(buf), bytenr);
148                 if (ret < sizeof(buf)) {
149                         ret = -errno;
150                         goto out;
151                 }
152                 ret = check_super(bytenr, sb);
153                 if (ret) {
154                         ret = add_superblock_record(sb, filename, bytenr,
155                                                         &recover->good_supers);
156                         if (ret)
157                                 goto out;
158                         max_gen = btrfs_super_generation(sb);
159                         if (max_gen > recover->max_generation)
160                                 recover->max_generation = max_gen;
161                 } else {
162                         ret = add_superblock_record(sb, filename, bytenr,
163                                                 &recover->bad_supers);
164                         if (ret)
165                                 goto out;
166                 }
167         }
168 out:
169         close(fd);
170         return ret;
171 }
172
173 static int read_fs_supers(struct btrfs_recover_superblock *recover)
174 {
175         struct super_block_record *record;
176         struct super_block_record *next_record;
177         struct btrfs_device *dev;
178         int ret;
179         u64 gen;
180
181         list_for_each_entry(dev, &recover->fs_devices->devices,
182                                 dev_list) {
183                 ret = read_dev_supers(dev->name, recover);
184                 if (ret)
185                         return ret;
186         }
187         list_for_each_entry_safe(record, next_record,
188                         &recover->good_supers, list) {
189                 gen = btrfs_super_generation(&record->sb);
190                 if (gen < recover->max_generation)
191                         list_move_tail(&record->list, &recover->bad_supers);
192         }
193
194         return 0;
195 }
196
197 static struct super_block_record *recover_get_good_super(
198                                 struct btrfs_recover_superblock *recover)
199 {
200         struct super_block_record *record;
201         record = list_entry(recover->good_supers.next,
202                                 struct super_block_record, list);
203         return record;
204 }
205
206 static void print_all_devices(struct list_head *devices)
207 {
208         struct btrfs_device *dev;
209
210         printf("All Devices:\n");
211         list_for_each_entry(dev, devices, dev_list) {
212                 printf("\t");
213                 printf("Device: id = %llu, name = %s\n",
214                         dev->devid, dev->name);
215         }
216         printf("\n");
217 }
218
219 static void print_super_info(struct super_block_record *record)
220 {
221         printf("\t\tdevice name = %s\n", record->device_name);
222         printf("\t\tsuperblock bytenr = %llu\n", record->bytenr);
223 }
224
225 static void print_all_supers(struct btrfs_recover_superblock *recover)
226 {
227         struct super_block_record *record;
228
229         printf("\t[All good supers]:\n");
230         list_for_each_entry(record, &recover->good_supers, list) {
231                 print_super_info(record);
232                 printf("\n");
233         }
234
235         printf("\t[All bad supers]:\n");
236         list_for_each_entry(record, &recover->bad_supers, list) {
237                 print_super_info(record);
238                 printf("\n");
239         }
240         printf("\n");
241 }
242
243 static void recover_err_str(int ret)
244 {
245         switch (ret) {
246         case 0:
247                 printf("All supers are valid, no need to recover\n");
248                 break;
249         case 1:
250                 printf("Usage or syntax errors\n");
251                 break;
252         case 2:
253                 printf("Recovered bad superblocks successful\n");
254                 break;
255         case 3:
256                 printf("Failed to recover bad superblocks\n");
257                 break;
258         case 4:
259                 printf("Aborted to recover bad superblocks\n");
260                 break;
261         default:
262                 printf("Unknown recover result\n");
263                 break;
264         }
265 }
266
267 int btrfs_recover_superblocks(const char *dname,
268                         int verbose, int yes)
269 {
270         int fd, ret;
271         struct btrfs_recover_superblock recover;
272         struct super_block_record *record;
273         struct btrfs_root *root = NULL;
274
275         fd = open(dname, O_RDONLY);
276         if (fd < 0) {
277                 fprintf(stderr, "open %s error\n", dname);
278                 return 1;
279         }
280         init_recover_superblock(&recover);
281
282         ret = btrfs_scan_fs_devices(fd, dname, &recover.fs_devices, 0, 1, 0);
283         close(fd);
284         if (ret) {
285                 ret = 1;
286                 goto no_recover;
287         }
288
289         if (verbose)
290                 print_all_devices(&recover.fs_devices->devices);
291
292         ret = read_fs_supers(&recover);
293         if (ret) {
294                 ret = 1;
295                 goto no_recover;
296         }
297         if (verbose) {
298                 printf("Before Recovering:\n");
299                 print_all_supers(&recover);
300         }
301
302         if (list_empty(&recover.bad_supers))
303                 goto no_recover;
304
305         if (!yes) {
306                 ret = ask_user("Make sure this is a btrfs disk otherwise the tool will destroy other fs, Are you sure?");
307                 if (!ret) {
308                         ret = 4;
309                         goto no_recover;
310                 }
311         }
312         record = recover_get_good_super(&recover);
313         root = open_ctree(record->device_name, record->bytenr,
314                           OPEN_CTREE_RECOVER_SUPER | OPEN_CTREE_WRITES);
315         if (!root) {
316                 ret = 3;
317                 goto no_recover;
318         }
319         /* reset super_bytenr in order that we will rewite all supers */
320         root->fs_info->super_bytenr = BTRFS_SUPER_INFO_OFFSET;
321         ret = write_all_supers(root);
322         if (!ret)
323                 ret = 2;
324         else
325                 ret = 3;
326
327         close_ctree(root);
328 no_recover:
329         recover_err_str(ret);
330         free_recover_superblock(&recover);
331         /* check if we have freed fs_deivces in close_ctree() */
332         if (!root)
333                 btrfs_close_devices(recover.fs_devices);
334         return ret;
335 }
336