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