btrfs-progs: Factor out common print_device_info
[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 add_superblock_record(struct btrfs_super_block *sb, char *fname,
92                         u64 bytenr, struct list_head *head)
93 {
94         struct super_block_record *record;
95
96         record = malloc(sizeof(struct super_block_record));
97         if (!record)
98                 return -ENOMEM;
99
100         record->device_name = strdup(fname);
101         if (!record->device_name) {
102                 free(record);
103                 return -ENOMEM;
104         }
105         memcpy(&record->sb, sb, sizeof(*sb));
106         record->bytenr = bytenr;
107         list_add_tail(&record->list, head);
108
109         return 0;
110 }
111
112 static int
113 read_dev_supers(char *filename, struct btrfs_recover_superblock *recover)
114 {
115         int i, ret, fd;
116         u8 buf[BTRFS_SUPER_INFO_SIZE];
117         u64 max_gen, bytenr;
118         struct btrfs_super_block *sb = (struct btrfs_super_block *)buf;
119
120         /* just ignore errno that were set in btrfs_scan_fs_devices() */
121         errno = 0;
122
123         fd = open(filename, O_RDONLY);
124         if (fd < 0)
125                 return -errno;
126
127         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
128                 bytenr = btrfs_sb_offset(i);
129
130                 ret = btrfs_read_dev_super(fd, sb, bytenr, SBREAD_DEFAULT);
131                 if (!ret) {
132                         ret = add_superblock_record(sb, filename, bytenr,
133                                                         &recover->good_supers);
134                         if (ret)
135                                 goto out;
136                         max_gen = btrfs_super_generation(sb);
137                         if (max_gen > recover->max_generation)
138                                 recover->max_generation = max_gen;
139                 } else if (ret == -EIO){
140                         /*
141                          * Skip superblock which doesn't exist, only adds
142                          * really corrupted superblock
143                          */
144                         ret = add_superblock_record(sb, filename, bytenr,
145                                                 &recover->bad_supers);
146                         if (ret)
147                                 goto out;
148                 }
149         }
150         ret = 0;
151 out:
152         close(fd);
153         return ret;
154 }
155
156 static int read_fs_supers(struct btrfs_recover_superblock *recover)
157 {
158         struct super_block_record *record;
159         struct super_block_record *next_record;
160         struct btrfs_device *dev;
161         int ret;
162         u64 gen;
163
164         list_for_each_entry(dev, &recover->fs_devices->devices,
165                                 dev_list) {
166                 ret = read_dev_supers(dev->name, recover);
167                 if (ret)
168                         return ret;
169         }
170         list_for_each_entry_safe(record, next_record,
171                         &recover->good_supers, list) {
172                 gen = btrfs_super_generation(&record->sb);
173                 if (gen < recover->max_generation)
174                         list_move_tail(&record->list, &recover->bad_supers);
175         }
176
177         return 0;
178 }
179
180 static struct super_block_record *recover_get_good_super(
181                                 struct btrfs_recover_superblock *recover)
182 {
183         struct super_block_record *record;
184         record = list_entry(recover->good_supers.next,
185                                 struct super_block_record, list);
186         return record;
187 }
188
189 static void print_super_info(struct super_block_record *record)
190 {
191         printf("\t\tdevice name = %s\n", record->device_name);
192         printf("\t\tsuperblock bytenr = %llu\n", record->bytenr);
193 }
194
195 static void print_all_supers(struct btrfs_recover_superblock *recover)
196 {
197         struct super_block_record *record;
198
199         printf("\t[All good supers]:\n");
200         list_for_each_entry(record, &recover->good_supers, list) {
201                 print_super_info(record);
202                 printf("\n");
203         }
204
205         printf("\t[All bad supers]:\n");
206         list_for_each_entry(record, &recover->bad_supers, list) {
207                 print_super_info(record);
208                 printf("\n");
209         }
210         printf("\n");
211 }
212
213 static void recover_err_str(int ret)
214 {
215         switch (ret) {
216         case 0:
217                 printf("All supers are valid, no need to recover\n");
218                 break;
219         case 1:
220                 printf("Usage or syntax errors\n");
221                 break;
222         case 2:
223                 printf("Recovered bad superblocks successful\n");
224                 break;
225         case 3:
226                 printf("Failed to recover bad superblocks\n");
227                 break;
228         case 4:
229                 printf("Aborted to recover bad superblocks\n");
230                 break;
231         default:
232                 printf("Unknown recover result\n");
233                 break;
234         }
235 }
236
237 int btrfs_recover_superblocks(const char *dname,
238                         int verbose, int yes)
239 {
240         int fd, ret;
241         struct btrfs_recover_superblock recover;
242         struct super_block_record *record;
243         struct btrfs_root *root = NULL;
244
245         fd = open(dname, O_RDONLY);
246         if (fd < 0) {
247                 fprintf(stderr, "open %s error\n", dname);
248                 return 1;
249         }
250         init_recover_superblock(&recover);
251
252         ret = btrfs_scan_fs_devices(fd, dname, &recover.fs_devices, 0,
253                         SBREAD_RECOVER, 0);
254         close(fd);
255         if (ret) {
256                 ret = 1;
257                 goto no_recover;
258         }
259
260         if (verbose)
261                 print_all_devices(&recover.fs_devices->devices);
262
263         ret = read_fs_supers(&recover);
264         if (ret) {
265                 ret = 1;
266                 goto no_recover;
267         }
268         if (verbose) {
269                 printf("Before Recovering:\n");
270                 print_all_supers(&recover);
271         }
272
273         if (list_empty(&recover.bad_supers))
274                 goto no_recover;
275
276         if (!yes) {
277                 ret = ask_user("Make sure this is a btrfs disk otherwise the tool will destroy other fs, Are you sure?");
278                 if (!ret) {
279                         ret = 4;
280                         goto no_recover;
281                 }
282         }
283         record = recover_get_good_super(&recover);
284         root = open_ctree(record->device_name, record->bytenr,
285                           OPEN_CTREE_RECOVER_SUPER | OPEN_CTREE_WRITES);
286         if (!root) {
287                 ret = 3;
288                 goto no_recover;
289         }
290         /* reset super_bytenr in order that we will rewrite all supers */
291         root->fs_info->super_bytenr = BTRFS_SUPER_INFO_OFFSET;
292         ret = write_all_supers(root->fs_info);
293         if (!ret)
294                 ret = 2;
295         else
296                 ret = 3;
297
298         close_ctree(root);
299 no_recover:
300         recover_err_str(ret);
301         free_recover_superblock(&recover);
302         /* check if we have freed fs_devices in close_ctree() */
303         if (!root)
304                 btrfs_close_devices(recover.fs_devices);
305         return ret;
306 }
307