btrfs-progs: better error report in btrfs_scan_fs_devices
[platform/upstream/btrfs-progs.git] / disk-io.c
1 /*
2  * Copyright (C) 2007 Oracle.  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 <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <uuid/uuid.h>
26 #include "kerncompat.h"
27 #include "radix-tree.h"
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "volumes.h"
31 #include "transaction.h"
32 #include "crc32c.h"
33 #include "utils.h"
34 #include "print-tree.h"
35 #include "rbtree-utils.h"
36
37 /* specified errno for check_tree_block */
38 #define BTRFS_BAD_BYTENR                (-1)
39 #define BTRFS_BAD_FSID                  (-2)
40 #define BTRFS_BAD_LEVEL                 (-3)
41 #define BTRFS_BAD_NRITEMS               (-4)
42
43 /* Calculate max possible nritems for a leaf/node */
44 static u32 max_nritems(u8 level, u32 nodesize)
45 {
46
47         if (level == 0)
48                 return ((nodesize - sizeof(struct btrfs_header)) /
49                         sizeof(struct btrfs_item));
50         return ((nodesize - sizeof(struct btrfs_header)) /
51                 sizeof(struct btrfs_key_ptr));
52 }
53
54 static int check_tree_block(struct btrfs_fs_info *fs_info,
55                             struct extent_buffer *buf)
56 {
57
58         struct btrfs_fs_devices *fs_devices;
59         u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
60         int ret = BTRFS_BAD_FSID;
61
62         if (buf->start != btrfs_header_bytenr(buf))
63                 return BTRFS_BAD_BYTENR;
64         if (btrfs_header_level(buf) >= BTRFS_MAX_LEVEL)
65                 return BTRFS_BAD_LEVEL;
66         if (btrfs_header_nritems(buf) > max_nritems(btrfs_header_level(buf),
67                                                     nodesize))
68                 return BTRFS_BAD_NRITEMS;
69
70         /* Only leaf can be empty */
71         if (btrfs_header_nritems(buf) == 0 &&
72             btrfs_header_level(buf) != 0)
73                 return BTRFS_BAD_NRITEMS;
74
75         fs_devices = fs_info->fs_devices;
76         while (fs_devices) {
77                 if (fs_info->ignore_fsid_mismatch ||
78                     !memcmp_extent_buffer(buf, fs_devices->fsid,
79                                           btrfs_header_fsid(),
80                                           BTRFS_FSID_SIZE)) {
81                         ret = 0;
82                         break;
83                 }
84                 fs_devices = fs_devices->seed;
85         }
86         return ret;
87 }
88
89 static void print_tree_block_error(struct btrfs_fs_info *fs_info,
90                                 struct extent_buffer *eb,
91                                 int err)
92 {
93         char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
94         char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
95         u8 buf[BTRFS_UUID_SIZE];
96
97         switch (err) {
98         case BTRFS_BAD_FSID:
99                 read_extent_buffer(eb, buf, btrfs_header_fsid(),
100                                    BTRFS_UUID_SIZE);
101                 uuid_unparse(buf, found_uuid);
102                 uuid_unparse(fs_info->fsid, fs_uuid);
103                 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
104                         fs_uuid, found_uuid);
105                 break;
106         case BTRFS_BAD_BYTENR:
107                 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
108                         eb->start, btrfs_header_bytenr(eb));
109                 break;
110         case BTRFS_BAD_LEVEL:
111                 fprintf(stderr, "bad level, %u > %u\n",
112                         btrfs_header_level(eb), BTRFS_MAX_LEVEL);
113                 break;
114         case BTRFS_BAD_NRITEMS:
115                 fprintf(stderr, "invalid nr_items: %u\n",
116                         btrfs_header_nritems(eb));
117                 break;
118         }
119 }
120
121 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
122 {
123         return crc32c(seed, data, len);
124 }
125
126 void btrfs_csum_final(u32 crc, char *result)
127 {
128         put_unaligned_le32(~crc, result);
129 }
130
131 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
132                                   int verify, int silent)
133 {
134         char result[BTRFS_CSUM_SIZE];
135         u32 len;
136         u32 crc = ~(u32)0;
137
138         len = buf->len - BTRFS_CSUM_SIZE;
139         crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
140         btrfs_csum_final(crc, result);
141
142         if (verify) {
143                 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
144                         if (!silent)
145                                 printk("checksum verify failed on %llu found %08X wanted %08X\n",
146                                        (unsigned long long)buf->start,
147                                        *((u32 *)result),
148                                        *((u32*)(char *)buf->data));
149                         return 1;
150                 }
151         } else {
152                 write_extent_buffer(buf, result, 0, csum_size);
153         }
154         return 0;
155 }
156
157 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
158 {
159         return __csum_tree_block_size(buf, csum_size, verify, 0);
160 }
161
162 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
163 {
164         return __csum_tree_block_size(buf, csum_size, 1, 1);
165 }
166
167 static int csum_tree_block_fs_info(struct btrfs_fs_info *fs_info,
168                                    struct extent_buffer *buf, int verify)
169 {
170         u16 csum_size =
171                 btrfs_super_csum_size(fs_info->super_copy);
172         if (verify && fs_info->suppress_check_block_errors)
173                 return verify_tree_block_csum_silent(buf, csum_size);
174         return csum_tree_block_size(buf, csum_size, verify);
175 }
176
177 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
178                            int verify)
179 {
180         return csum_tree_block_fs_info(root->fs_info, buf, verify);
181 }
182
183 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
184                                             u64 bytenr, u32 blocksize)
185 {
186         return find_extent_buffer(&root->fs_info->extent_cache,
187                                   bytenr, blocksize);
188 }
189
190 struct extent_buffer* btrfs_find_create_tree_block(
191                 struct btrfs_fs_info *fs_info, u64 bytenr, u32 blocksize)
192 {
193         return alloc_extent_buffer(&fs_info->extent_cache, bytenr, blocksize);
194 }
195
196 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
197                           u64 parent_transid)
198 {
199         struct extent_buffer *eb;
200         u64 length;
201         struct btrfs_multi_bio *multi = NULL;
202         struct btrfs_device *device;
203
204         eb = btrfs_find_tree_block(root, bytenr, blocksize);
205         if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
206             !btrfs_map_block(&root->fs_info->mapping_tree, READ,
207                              bytenr, &length, &multi, 0, NULL)) {
208                 device = multi->stripes[0].dev;
209                 device->total_ios++;
210                 blocksize = min(blocksize, (u32)(64 * 1024));
211                 readahead(device->fd, multi->stripes[0].physical, blocksize);
212         }
213
214         free_extent_buffer(eb);
215         kfree(multi);
216 }
217
218 static int verify_parent_transid(struct extent_io_tree *io_tree,
219                                  struct extent_buffer *eb, u64 parent_transid,
220                                  int ignore)
221 {
222         int ret;
223
224         if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
225                 return 0;
226
227         if (extent_buffer_uptodate(eb) &&
228             btrfs_header_generation(eb) == parent_transid) {
229                 ret = 0;
230                 goto out;
231         }
232         printk("parent transid verify failed on %llu wanted %llu found %llu\n",
233                (unsigned long long)eb->start,
234                (unsigned long long)parent_transid,
235                (unsigned long long)btrfs_header_generation(eb));
236         if (ignore) {
237                 eb->flags |= EXTENT_BAD_TRANSID;
238                 printk("Ignoring transid failure\n");
239                 return 0;
240         }
241
242         ret = 1;
243 out:
244         clear_extent_buffer_uptodate(io_tree, eb);
245         return ret;
246
247 }
248
249
250 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
251 {
252         unsigned long offset = 0;
253         struct btrfs_multi_bio *multi = NULL;
254         struct btrfs_device *device;
255         int ret = 0;
256         u64 read_len;
257         unsigned long bytes_left = eb->len;
258
259         while (bytes_left) {
260                 read_len = bytes_left;
261                 device = NULL;
262
263                 if (!info->on_restoring &&
264                     eb->start != BTRFS_SUPER_INFO_OFFSET) {
265                         ret = btrfs_map_block(&info->mapping_tree, READ,
266                                               eb->start + offset, &read_len, &multi,
267                                               mirror, NULL);
268                         if (ret) {
269                                 printk("Couldn't map the block %Lu\n", eb->start + offset);
270                                 kfree(multi);
271                                 return -EIO;
272                         }
273                         device = multi->stripes[0].dev;
274
275                         if (device->fd <= 0) {
276                                 kfree(multi);
277                                 return -EIO;
278                         }
279
280                         eb->fd = device->fd;
281                         device->total_ios++;
282                         eb->dev_bytenr = multi->stripes[0].physical;
283                         kfree(multi);
284                         multi = NULL;
285                 } else {
286                         /* special case for restore metadump */
287                         list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
288                                 if (device->devid == 1)
289                                         break;
290                         }
291
292                         eb->fd = device->fd;
293                         eb->dev_bytenr = eb->start;
294                         device->total_ios++;
295                 }
296
297                 if (read_len > bytes_left)
298                         read_len = bytes_left;
299
300                 ret = read_extent_from_disk(eb, offset, read_len);
301                 if (ret)
302                         return -EIO;
303                 offset += read_len;
304                 bytes_left -= read_len;
305         }
306         return 0;
307 }
308
309 struct extent_buffer* read_tree_block_fs_info(
310                 struct btrfs_fs_info *fs_info, u64 bytenr, u32 blocksize,
311                 u64 parent_transid)
312 {
313         int ret;
314         struct extent_buffer *eb;
315         u64 best_transid = 0;
316         u32 sectorsize = btrfs_super_sectorsize(fs_info->super_copy);
317         u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
318         int mirror_num = 0;
319         int good_mirror = 0;
320         int num_copies;
321         int ignore = 0;
322
323         /*
324          * Don't even try to create tree block for unaligned tree block
325          * bytenr.
326          * Such unaligned tree block will free overlapping extent buffer,
327          * causing use-after-free bugs for fuzzed images.
328          */
329         if (!IS_ALIGNED(bytenr, sectorsize)) {
330                 error("tree block bytenr %llu is not aligned to sectorsize %u",
331                       bytenr, sectorsize);
332                 return ERR_PTR(-EIO);
333         }
334         if (!IS_ALIGNED(blocksize, nodesize)) {
335                 error("tree block size %u is not aligned to nodesize %u",
336                       blocksize, nodesize);
337                 return ERR_PTR(-EIO);
338         }
339         eb = btrfs_find_create_tree_block(fs_info, bytenr, blocksize);
340         if (!eb)
341                 return ERR_PTR(-ENOMEM);
342
343         if (btrfs_buffer_uptodate(eb, parent_transid))
344                 return eb;
345
346         while (1) {
347                 ret = read_whole_eb(fs_info, eb, mirror_num);
348                 if (ret == 0 && csum_tree_block_fs_info(fs_info, eb, 1) == 0 &&
349                     check_tree_block(fs_info, eb) == 0 &&
350                     verify_parent_transid(eb->tree, eb, parent_transid, ignore)
351                     == 0) {
352                         if (eb->flags & EXTENT_BAD_TRANSID &&
353                             list_empty(&eb->recow)) {
354                                 list_add_tail(&eb->recow,
355                                               &fs_info->recow_ebs);
356                                 eb->refs++;
357                         }
358                         btrfs_set_buffer_uptodate(eb);
359                         return eb;
360                 }
361                 if (ignore) {
362                         if (check_tree_block(fs_info, eb)) {
363                                 if (!fs_info->suppress_check_block_errors)
364                                         print_tree_block_error(fs_info, eb,
365                                                 check_tree_block(fs_info, eb));
366                         } else {
367                                 if (!fs_info->suppress_check_block_errors)
368                                         fprintf(stderr, "Csum didn't match\n");
369                         }
370                         ret = -EIO;
371                         break;
372                 }
373                 num_copies = btrfs_num_copies(&fs_info->mapping_tree,
374                                               eb->start, eb->len);
375                 if (num_copies == 1) {
376                         ignore = 1;
377                         continue;
378                 }
379                 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
380                         best_transid = btrfs_header_generation(eb);
381                         good_mirror = mirror_num;
382                 }
383                 mirror_num++;
384                 if (mirror_num > num_copies) {
385                         mirror_num = good_mirror;
386                         ignore = 1;
387                         continue;
388                 }
389         }
390         free_extent_buffer(eb);
391         return ERR_PTR(ret);
392 }
393
394 int read_extent_data(struct btrfs_root *root, char *data,
395                            u64 logical, u64 *len, int mirror)
396 {
397         u64 offset = 0;
398         struct btrfs_multi_bio *multi = NULL;
399         struct btrfs_fs_info *info = root->fs_info;
400         struct btrfs_device *device;
401         int ret = 0;
402         u64 max_len = *len;
403
404         ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
405                               &multi, mirror, NULL);
406         if (ret) {
407                 fprintf(stderr, "Couldn't map the block %llu\n",
408                                 logical + offset);
409                 goto err;
410         }
411         device = multi->stripes[0].dev;
412
413         if (device->fd <= 0)
414                 goto err;
415         if (*len > max_len)
416                 *len = max_len;
417
418         ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
419         if (ret != *len)
420                 ret = -EIO;
421         else
422                 ret = 0;
423 err:
424         kfree(multi);
425         return ret;
426 }
427
428 int write_and_map_eb(struct btrfs_trans_handle *trans,
429                      struct btrfs_root *root,
430                      struct extent_buffer *eb)
431 {
432         int ret;
433         int dev_nr;
434         u64 length;
435         u64 *raid_map = NULL;
436         struct btrfs_multi_bio *multi = NULL;
437
438         dev_nr = 0;
439         length = eb->len;
440         ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
441                               eb->start, &length, &multi, 0, &raid_map);
442
443         if (raid_map) {
444                 ret = write_raid56_with_parity(root->fs_info, eb, multi,
445                                                length, raid_map);
446                 BUG_ON(ret);
447         } else while (dev_nr < multi->num_stripes) {
448                 BUG_ON(ret);
449                 eb->fd = multi->stripes[dev_nr].dev->fd;
450                 eb->dev_bytenr = multi->stripes[dev_nr].physical;
451                 multi->stripes[dev_nr].dev->total_ios++;
452                 dev_nr++;
453                 ret = write_extent_to_disk(eb);
454                 BUG_ON(ret);
455         }
456         kfree(raid_map);
457         kfree(multi);
458         return 0;
459 }
460
461 int write_tree_block(struct btrfs_trans_handle *trans,
462                      struct btrfs_root *root,
463                      struct extent_buffer *eb)
464 {
465         if (check_tree_block(root->fs_info, eb)) {
466                 print_tree_block_error(root->fs_info, eb,
467                                 check_tree_block(root->fs_info, eb));
468                 BUG();
469         }
470
471         if (trans && !btrfs_buffer_uptodate(eb, trans->transid))
472                 BUG();
473
474         btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
475         csum_tree_block(root, eb, 0);
476
477         return write_and_map_eb(trans, root, eb);
478 }
479
480 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
481                         u32 stripesize, struct btrfs_root *root,
482                         struct btrfs_fs_info *fs_info, u64 objectid)
483 {
484         root->node = NULL;
485         root->commit_root = NULL;
486         root->sectorsize = sectorsize;
487         root->nodesize = nodesize;
488         root->leafsize = leafsize;
489         root->stripesize = stripesize;
490         root->ref_cows = 0;
491         root->track_dirty = 0;
492
493         root->fs_info = fs_info;
494         root->objectid = objectid;
495         root->last_trans = 0;
496         root->highest_inode = 0;
497         root->last_inode_alloc = 0;
498
499         INIT_LIST_HEAD(&root->dirty_list);
500         INIT_LIST_HEAD(&root->orphan_data_extents);
501         memset(&root->root_key, 0, sizeof(root->root_key));
502         memset(&root->root_item, 0, sizeof(root->root_item));
503         root->root_key.objectid = objectid;
504         return 0;
505 }
506
507 static int update_cowonly_root(struct btrfs_trans_handle *trans,
508                                struct btrfs_root *root)
509 {
510         int ret;
511         u64 old_root_bytenr;
512         struct btrfs_root *tree_root = root->fs_info->tree_root;
513
514         btrfs_write_dirty_block_groups(trans, root);
515         while(1) {
516                 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
517                 if (old_root_bytenr == root->node->start)
518                         break;
519                 btrfs_set_root_bytenr(&root->root_item,
520                                        root->node->start);
521                 btrfs_set_root_generation(&root->root_item,
522                                           trans->transid);
523                 root->root_item.level = btrfs_header_level(root->node);
524                 ret = btrfs_update_root(trans, tree_root,
525                                         &root->root_key,
526                                         &root->root_item);
527                 BUG_ON(ret);
528                 btrfs_write_dirty_block_groups(trans, root);
529         }
530         return 0;
531 }
532
533 static int commit_tree_roots(struct btrfs_trans_handle *trans,
534                              struct btrfs_fs_info *fs_info)
535 {
536         struct btrfs_root *root;
537         struct list_head *next;
538         struct extent_buffer *eb;
539         int ret;
540
541         if (fs_info->readonly)
542                 return 0;
543
544         eb = fs_info->tree_root->node;
545         extent_buffer_get(eb);
546         ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
547         free_extent_buffer(eb);
548         if (ret)
549                 return ret;
550
551         while(!list_empty(&fs_info->dirty_cowonly_roots)) {
552                 next = fs_info->dirty_cowonly_roots.next;
553                 list_del_init(next);
554                 root = list_entry(next, struct btrfs_root, dirty_list);
555                 update_cowonly_root(trans, root);
556                 free_extent_buffer(root->commit_root);
557                 root->commit_root = NULL;
558         }
559
560         return 0;
561 }
562
563 static int __commit_transaction(struct btrfs_trans_handle *trans,
564                                 struct btrfs_root *root)
565 {
566         u64 start;
567         u64 end;
568         struct extent_buffer *eb;
569         struct extent_io_tree *tree = &root->fs_info->extent_cache;
570         int ret;
571
572         while(1) {
573                 ret = find_first_extent_bit(tree, 0, &start, &end,
574                                             EXTENT_DIRTY);
575                 if (ret)
576                         break;
577                 while(start <= end) {
578                         eb = find_first_extent_buffer(tree, start);
579                         BUG_ON(!eb || eb->start != start);
580                         ret = write_tree_block(trans, root, eb);
581                         BUG_ON(ret);
582                         start += eb->len;
583                         clear_extent_buffer_dirty(eb);
584                         free_extent_buffer(eb);
585                 }
586         }
587         return 0;
588 }
589
590 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
591                              struct btrfs_root *root)
592 {
593         u64 transid = trans->transid;
594         int ret = 0;
595         struct btrfs_fs_info *fs_info = root->fs_info;
596
597         if (root->commit_root == root->node)
598                 goto commit_tree;
599         if (root == root->fs_info->tree_root)
600                 goto commit_tree;
601         if (root == root->fs_info->chunk_root)
602                 goto commit_tree;
603
604         free_extent_buffer(root->commit_root);
605         root->commit_root = NULL;
606
607         btrfs_set_root_bytenr(&root->root_item, root->node->start);
608         btrfs_set_root_generation(&root->root_item, trans->transid);
609         root->root_item.level = btrfs_header_level(root->node);
610         ret = btrfs_update_root(trans, root->fs_info->tree_root,
611                                 &root->root_key, &root->root_item);
612         BUG_ON(ret);
613 commit_tree:
614         ret = commit_tree_roots(trans, fs_info);
615         BUG_ON(ret);
616         ret = __commit_transaction(trans, root);
617         BUG_ON(ret);
618         write_ctree_super(trans, root);
619         btrfs_finish_extent_commit(trans, fs_info->extent_root,
620                                    &fs_info->pinned_extents);
621         btrfs_free_transaction(root, trans);
622         free_extent_buffer(root->commit_root);
623         root->commit_root = NULL;
624         fs_info->running_transaction = NULL;
625         fs_info->last_trans_committed = transid;
626         return 0;
627 }
628
629 static int find_and_setup_root(struct btrfs_root *tree_root,
630                                struct btrfs_fs_info *fs_info,
631                                u64 objectid, struct btrfs_root *root)
632 {
633         int ret;
634         u32 blocksize;
635         u64 generation;
636
637         __setup_root(tree_root->nodesize, tree_root->leafsize,
638                      tree_root->sectorsize, tree_root->stripesize,
639                      root, fs_info, objectid);
640         ret = btrfs_find_last_root(tree_root, objectid,
641                                    &root->root_item, &root->root_key);
642         if (ret)
643                 return ret;
644
645         blocksize = root->nodesize;
646         generation = btrfs_root_generation(&root->root_item);
647         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
648                                      blocksize, generation);
649         if (!extent_buffer_uptodate(root->node))
650                 return -EIO;
651
652         return 0;
653 }
654
655 static int find_and_setup_log_root(struct btrfs_root *tree_root,
656                                struct btrfs_fs_info *fs_info,
657                                struct btrfs_super_block *disk_super)
658 {
659         u32 blocksize;
660         u64 blocknr = btrfs_super_log_root(disk_super);
661         struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
662
663         if (!log_root)
664                 return -ENOMEM;
665
666         if (blocknr == 0) {
667                 free(log_root);
668                 return 0;
669         }
670
671         blocksize = tree_root->nodesize;
672
673         __setup_root(tree_root->nodesize, tree_root->leafsize,
674                      tree_root->sectorsize, tree_root->stripesize,
675                      log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
676
677         log_root->node = read_tree_block(tree_root, blocknr,
678                                      blocksize,
679                                      btrfs_super_generation(disk_super) + 1);
680
681         fs_info->log_root_tree = log_root;
682
683         if (!extent_buffer_uptodate(log_root->node)) {
684                 free_extent_buffer(log_root->node);
685                 free(log_root);
686                 fs_info->log_root_tree = NULL;
687                 return -EIO;
688         }
689
690         return 0;
691 }
692
693 int btrfs_free_fs_root(struct btrfs_root *root)
694 {
695         if (root->node)
696                 free_extent_buffer(root->node);
697         if (root->commit_root)
698                 free_extent_buffer(root->commit_root);
699         kfree(root);
700         return 0;
701 }
702
703 static void __free_fs_root(struct rb_node *node)
704 {
705         struct btrfs_root *root;
706
707         root = container_of(node, struct btrfs_root, rb_node);
708         btrfs_free_fs_root(root);
709 }
710
711 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
712
713 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
714                                                struct btrfs_key *location)
715 {
716         struct btrfs_root *root;
717         struct btrfs_root *tree_root = fs_info->tree_root;
718         struct btrfs_path *path;
719         struct extent_buffer *l;
720         u64 generation;
721         u32 blocksize;
722         int ret = 0;
723
724         root = calloc(1, sizeof(*root));
725         if (!root)
726                 return ERR_PTR(-ENOMEM);
727         if (location->offset == (u64)-1) {
728                 ret = find_and_setup_root(tree_root, fs_info,
729                                           location->objectid, root);
730                 if (ret) {
731                         free(root);
732                         return ERR_PTR(ret);
733                 }
734                 goto insert;
735         }
736
737         __setup_root(tree_root->nodesize, tree_root->leafsize,
738                      tree_root->sectorsize, tree_root->stripesize,
739                      root, fs_info, location->objectid);
740
741         path = btrfs_alloc_path();
742         BUG_ON(!path);
743         ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
744         if (ret != 0) {
745                 if (ret > 0)
746                         ret = -ENOENT;
747                 goto out;
748         }
749         l = path->nodes[0];
750         read_extent_buffer(l, &root->root_item,
751                btrfs_item_ptr_offset(l, path->slots[0]),
752                sizeof(root->root_item));
753         memcpy(&root->root_key, location, sizeof(*location));
754         ret = 0;
755 out:
756         btrfs_free_path(path);
757         if (ret) {
758                 free(root);
759                 return ERR_PTR(ret);
760         }
761         generation = btrfs_root_generation(&root->root_item);
762         blocksize = root->nodesize;
763         root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
764                                      blocksize, generation);
765         if (!extent_buffer_uptodate(root->node)) {
766                 free(root);
767                 return ERR_PTR(-EIO);
768         }
769 insert:
770         root->ref_cows = 1;
771         return root;
772 }
773
774 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
775                                             void *data)
776 {
777         u64 objectid = *((u64 *)data);
778         struct btrfs_root *root;
779
780         root = rb_entry(node, struct btrfs_root, rb_node);
781         if (objectid > root->objectid)
782                 return 1;
783         else if (objectid < root->objectid)
784                 return -1;
785         else
786                 return 0;
787 }
788
789 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
790                                         struct rb_node *node2)
791 {
792         struct btrfs_root *root;
793
794         root = rb_entry(node2, struct btrfs_root, rb_node);
795         return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
796 }
797
798 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
799                                       struct btrfs_key *location)
800 {
801         struct btrfs_root *root;
802         struct rb_node *node;
803         int ret;
804         u64 objectid = location->objectid;
805
806         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
807                 return fs_info->tree_root;
808         if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
809                 return fs_info->extent_root;
810         if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
811                 return fs_info->chunk_root;
812         if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
813                 return fs_info->dev_root;
814         if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
815                 return fs_info->csum_root;
816         if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
817                 return fs_info->quota_root;
818
819         BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
820                location->offset != (u64)-1);
821
822         node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
823                          btrfs_fs_roots_compare_objectids, NULL);
824         if (node)
825                 return container_of(node, struct btrfs_root, rb_node);
826
827         root = btrfs_read_fs_root_no_cache(fs_info, location);
828         if (IS_ERR(root))
829                 return root;
830
831         ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
832                         btrfs_fs_roots_compare_roots);
833         BUG_ON(ret);
834         return root;
835 }
836
837 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
838 {
839         free(fs_info->tree_root);
840         free(fs_info->extent_root);
841         free(fs_info->chunk_root);
842         free(fs_info->dev_root);
843         free(fs_info->csum_root);
844         free(fs_info->quota_root);
845         free(fs_info->free_space_root);
846         free(fs_info->super_copy);
847         free(fs_info->log_root_tree);
848         free(fs_info);
849 }
850
851 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
852 {
853         struct btrfs_fs_info *fs_info;
854
855         fs_info = calloc(1, sizeof(struct btrfs_fs_info));
856         if (!fs_info)
857                 return NULL;
858
859         fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
860         fs_info->extent_root = calloc(1, sizeof(struct btrfs_root));
861         fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
862         fs_info->dev_root = calloc(1, sizeof(struct btrfs_root));
863         fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
864         fs_info->quota_root = calloc(1, sizeof(struct btrfs_root));
865         fs_info->free_space_root = calloc(1, sizeof(struct btrfs_root));
866         fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
867
868         if (!fs_info->tree_root || !fs_info->extent_root ||
869             !fs_info->chunk_root || !fs_info->dev_root ||
870             !fs_info->csum_root || !fs_info->quota_root ||
871             !fs_info->free_space_root || !fs_info->super_copy)
872                 goto free_all;
873
874         extent_io_tree_init(&fs_info->extent_cache);
875         extent_io_tree_init(&fs_info->free_space_cache);
876         extent_io_tree_init(&fs_info->block_group_cache);
877         extent_io_tree_init(&fs_info->pinned_extents);
878         extent_io_tree_init(&fs_info->pending_del);
879         extent_io_tree_init(&fs_info->extent_ins);
880         fs_info->excluded_extents = NULL;
881
882         fs_info->fs_root_tree = RB_ROOT;
883         cache_tree_init(&fs_info->mapping_tree.cache_tree);
884
885         mutex_init(&fs_info->fs_mutex);
886         INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
887         INIT_LIST_HEAD(&fs_info->space_info);
888         INIT_LIST_HEAD(&fs_info->recow_ebs);
889
890         if (!writable)
891                 fs_info->readonly = 1;
892
893         fs_info->super_bytenr = sb_bytenr;
894         fs_info->data_alloc_profile = (u64)-1;
895         fs_info->metadata_alloc_profile = (u64)-1;
896         fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
897         return fs_info;
898 free_all:
899         btrfs_free_fs_info(fs_info);
900         return NULL;
901 }
902
903 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
904 {
905         u64 features;
906
907         features = btrfs_super_incompat_flags(sb) &
908                    ~BTRFS_FEATURE_INCOMPAT_SUPP;
909         if (features) {
910                 printk("couldn't open because of unsupported "
911                        "option features (%Lx).\n",
912                        (unsigned long long)features);
913                 return -ENOTSUP;
914         }
915
916         features = btrfs_super_incompat_flags(sb);
917         if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
918                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
919                 btrfs_set_super_incompat_flags(sb, features);
920         }
921
922         features = btrfs_super_compat_ro_flags(sb) &
923                 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
924         if (writable && features) {
925                 printk("couldn't open RDWR because of unsupported "
926                        "option features (%Lx).\n",
927                        (unsigned long long)features);
928                 return -ENOTSUP;
929         }
930         return 0;
931 }
932
933 static int find_best_backup_root(struct btrfs_super_block *super)
934 {
935         struct btrfs_root_backup *backup;
936         u64 orig_gen = btrfs_super_generation(super);
937         u64 gen = 0;
938         int best_index = 0;
939         int i;
940
941         for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
942                 backup = super->super_roots + i;
943                 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
944                     btrfs_backup_tree_root_gen(backup) > gen) {
945                         best_index = i;
946                         gen = btrfs_backup_tree_root_gen(backup);
947                 }
948         }
949         return best_index;
950 }
951
952 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
953                                       unsigned flags,
954                                       struct btrfs_root *info_root,
955                                       u64 objectid, char *str)
956 {
957         struct btrfs_super_block *sb = fs_info->super_copy;
958         struct btrfs_root *root = fs_info->tree_root;
959         u32 nodesize = btrfs_super_nodesize(sb);
960         int ret;
961
962         ret = find_and_setup_root(root, fs_info, objectid, info_root);
963         if (ret) {
964                 printk("Couldn't setup %s tree\n", str);
965                 if (!(flags & OPEN_CTREE_PARTIAL))
966                         return -EIO;
967                 /*
968                  * Need a blank node here just so we don't screw up in the
969                  * million of places that assume a root has a valid ->node
970                  */
971                 info_root->node =
972                         btrfs_find_create_tree_block(fs_info, 0, nodesize);
973                 if (!info_root->node)
974                         return -ENOMEM;
975                 clear_extent_buffer_uptodate(NULL, info_root->node);
976         }
977
978         return 0;
979 }
980
981 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
982                           unsigned flags)
983 {
984         struct btrfs_super_block *sb = fs_info->super_copy;
985         struct btrfs_root *root;
986         struct btrfs_key key;
987         u32 sectorsize;
988         u32 nodesize;
989         u32 leafsize;
990         u32 stripesize;
991         u64 generation;
992         u32 blocksize;
993         int ret;
994
995         nodesize = btrfs_super_nodesize(sb);
996         leafsize = btrfs_super_leafsize(sb);
997         sectorsize = btrfs_super_sectorsize(sb);
998         stripesize = btrfs_super_stripesize(sb);
999
1000         root = fs_info->tree_root;
1001         __setup_root(nodesize, leafsize, sectorsize, stripesize,
1002                      root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
1003         blocksize = root->nodesize;
1004         generation = btrfs_super_generation(sb);
1005
1006         if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
1007                 root_tree_bytenr = btrfs_super_root(sb);
1008         } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
1009                 struct btrfs_root_backup *backup;
1010                 int index = find_best_backup_root(sb);
1011                 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
1012                         fprintf(stderr, "Invalid backup root number\n");
1013                         return -EIO;
1014                 }
1015                 backup = fs_info->super_copy->super_roots + index;
1016                 root_tree_bytenr = btrfs_backup_tree_root(backup);
1017                 generation = btrfs_backup_tree_root_gen(backup);
1018         }
1019
1020         root->node = read_tree_block(root, root_tree_bytenr, blocksize,
1021                                      generation);
1022         if (!extent_buffer_uptodate(root->node)) {
1023                 fprintf(stderr, "Couldn't read tree root\n");
1024                 return -EIO;
1025         }
1026
1027         ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
1028                                          BTRFS_EXTENT_TREE_OBJECTID, "extent");
1029         if (ret)
1030                 return ret;
1031         fs_info->extent_root->track_dirty = 1;
1032
1033         ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
1034                                   fs_info->dev_root);
1035         if (ret) {
1036                 printk("Couldn't setup device tree\n");
1037                 return -EIO;
1038         }
1039         fs_info->dev_root->track_dirty = 1;
1040
1041         ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
1042                                          BTRFS_CSUM_TREE_OBJECTID, "csum");
1043         if (ret)
1044                 return ret;
1045         fs_info->csum_root->track_dirty = 1;
1046
1047         ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
1048                                   fs_info->quota_root);
1049         if (ret == 0)
1050                 fs_info->quota_enabled = 1;
1051
1052         if (btrfs_fs_compat_ro(fs_info, BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE)) {
1053                 ret = find_and_setup_root(root, fs_info, BTRFS_FREE_SPACE_TREE_OBJECTID,
1054                                           fs_info->free_space_root);
1055                 if (ret) {
1056                         printk("Couldn't read free space tree\n");
1057                         return -EIO;
1058                 }
1059                 fs_info->free_space_root->track_dirty = 1;
1060         }
1061
1062         ret = find_and_setup_log_root(root, fs_info, sb);
1063         if (ret) {
1064                 printk("Couldn't setup log root tree\n");
1065                 if (!(flags & OPEN_CTREE_PARTIAL))
1066                         return -EIO;
1067         }
1068
1069         fs_info->generation = generation;
1070         fs_info->last_trans_committed = generation;
1071         if (extent_buffer_uptodate(fs_info->extent_root->node) &&
1072             !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
1073                 btrfs_read_block_groups(fs_info->tree_root);
1074
1075         key.objectid = BTRFS_FS_TREE_OBJECTID;
1076         key.type = BTRFS_ROOT_ITEM_KEY;
1077         key.offset = (u64)-1;
1078         fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
1079
1080         if (IS_ERR(fs_info->fs_root))
1081                 return -EIO;
1082         return 0;
1083 }
1084
1085 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
1086 {
1087         if (fs_info->free_space_root)
1088                 free_extent_buffer(fs_info->free_space_root->node);
1089         if (fs_info->quota_root)
1090                 free_extent_buffer(fs_info->quota_root->node);
1091         if (fs_info->csum_root)
1092                 free_extent_buffer(fs_info->csum_root->node);
1093         if (fs_info->dev_root)
1094                 free_extent_buffer(fs_info->dev_root->node);
1095         if (fs_info->extent_root)
1096                 free_extent_buffer(fs_info->extent_root->node);
1097         if (fs_info->tree_root)
1098                 free_extent_buffer(fs_info->tree_root->node);
1099         if (fs_info->log_root_tree)
1100                 free_extent_buffer(fs_info->log_root_tree->node);
1101         if (fs_info->chunk_root)
1102                 free_extent_buffer(fs_info->chunk_root->node);
1103 }
1104
1105 static void free_map_lookup(struct cache_extent *ce)
1106 {
1107         struct map_lookup *map;
1108
1109         map = container_of(ce, struct map_lookup, ce);
1110         kfree(map);
1111 }
1112
1113 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
1114
1115 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
1116 {
1117         while (!list_empty(&fs_info->recow_ebs)) {
1118                 struct extent_buffer *eb;
1119                 eb = list_first_entry(&fs_info->recow_ebs,
1120                                       struct extent_buffer, recow);
1121                 list_del_init(&eb->recow);
1122                 free_extent_buffer(eb);
1123         }
1124         free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
1125         extent_io_tree_cleanup(&fs_info->extent_cache);
1126         extent_io_tree_cleanup(&fs_info->free_space_cache);
1127         extent_io_tree_cleanup(&fs_info->block_group_cache);
1128         extent_io_tree_cleanup(&fs_info->pinned_extents);
1129         extent_io_tree_cleanup(&fs_info->pending_del);
1130         extent_io_tree_cleanup(&fs_info->extent_ins);
1131 }
1132
1133 int btrfs_scan_fs_devices(int fd, const char *path,
1134                           struct btrfs_fs_devices **fs_devices,
1135                           u64 sb_bytenr, unsigned sbflags,
1136                           int skip_devices)
1137 {
1138         u64 total_devs;
1139         u64 dev_size;
1140         off_t seek_ret;
1141         int ret;
1142         if (!sb_bytenr)
1143                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1144
1145         seek_ret = lseek(fd, 0, SEEK_END);
1146         if (seek_ret < 0)
1147                 return -errno;
1148
1149         dev_size = seek_ret;
1150         lseek(fd, 0, SEEK_SET);
1151         if (sb_bytenr > dev_size) {
1152                 error("superblock bytenr %llu is larger than device size %llu",
1153                                 (unsigned long long)sb_bytenr,
1154                                 (unsigned long long)dev_size);
1155                 return -EINVAL;
1156         }
1157
1158         ret = btrfs_scan_one_device(fd, path, fs_devices,
1159                                     &total_devs, sb_bytenr, sbflags);
1160         if (ret) {
1161                 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1162                 return ret;
1163         }
1164
1165         if (!skip_devices && total_devs != 1) {
1166                 ret = btrfs_scan_lblkid();
1167                 if (ret)
1168                         return ret;
1169         }
1170         return 0;
1171 }
1172
1173 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
1174                                           u64 chunk_root_bytenr)
1175 {
1176         struct btrfs_super_block *sb = fs_info->super_copy;
1177         u32 sectorsize;
1178         u32 nodesize;
1179         u32 leafsize;
1180         u32 blocksize;
1181         u32 stripesize;
1182         u64 generation;
1183         int ret;
1184
1185         nodesize = btrfs_super_nodesize(sb);
1186         leafsize = btrfs_super_leafsize(sb);
1187         sectorsize = btrfs_super_sectorsize(sb);
1188         stripesize = btrfs_super_stripesize(sb);
1189
1190         __setup_root(nodesize, leafsize, sectorsize, stripesize,
1191                      fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1192
1193         ret = btrfs_read_sys_array(fs_info->chunk_root);
1194         if (ret)
1195                 return ret;
1196
1197         blocksize = fs_info->chunk_root->nodesize;
1198         generation = btrfs_super_chunk_root_generation(sb);
1199
1200         if (chunk_root_bytenr && !IS_ALIGNED(chunk_root_bytenr,
1201                                             btrfs_super_sectorsize(sb))) {
1202                 warning("chunk_root_bytenr %llu is unaligned to %u, ignore it",
1203                         chunk_root_bytenr, btrfs_super_sectorsize(sb));
1204                 chunk_root_bytenr = 0;
1205         }
1206
1207         if (!chunk_root_bytenr)
1208                 chunk_root_bytenr = btrfs_super_chunk_root(sb);
1209         else
1210                 generation = 0;
1211
1212         fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1213                                                     chunk_root_bytenr,
1214                                                     blocksize, generation);
1215         if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1216                 if (fs_info->ignore_chunk_tree_error) {
1217                         warning("cannot read chunk root, continue anyway");
1218                         fs_info->chunk_root = NULL;
1219                         return 0;
1220                 } else {
1221                         error("cannot read chunk root");
1222                         return -EIO;
1223                 }
1224         }
1225
1226         if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1227                 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1228                 if (ret) {
1229                         fprintf(stderr, "Couldn't read chunk tree\n");
1230                         return ret;
1231                 }
1232         }
1233         return 0;
1234 }
1235
1236 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1237                                              u64 sb_bytenr,
1238                                              u64 root_tree_bytenr,
1239                                              u64 chunk_root_bytenr,
1240                                              unsigned flags)
1241 {
1242         struct btrfs_fs_info *fs_info;
1243         struct btrfs_super_block *disk_super;
1244         struct btrfs_fs_devices *fs_devices = NULL;
1245         struct extent_buffer *eb;
1246         int ret;
1247         int oflags;
1248         unsigned sbflags = SBREAD_DEFAULT;
1249
1250         if (sb_bytenr == 0)
1251                 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1252
1253         /* try to drop all the caches */
1254         if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1255                 fprintf(stderr, "Warning, could not drop caches\n");
1256
1257         fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1258         if (!fs_info) {
1259                 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1260                 return NULL;
1261         }
1262         if (flags & OPEN_CTREE_RESTORE)
1263                 fs_info->on_restoring = 1;
1264         if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1265                 fs_info->suppress_check_block_errors = 1;
1266         if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
1267                 fs_info->ignore_fsid_mismatch = 1;
1268         if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR)
1269                 fs_info->ignore_chunk_tree_error = 1;
1270
1271         if ((flags & OPEN_CTREE_RECOVER_SUPER)
1272              && (flags & OPEN_CTREE_FS_PARTIAL)) {
1273                 fprintf(stderr,
1274                     "cannot open a partially created filesystem for recovery");
1275                 goto out;
1276         }
1277
1278         if (flags & OPEN_CTREE_FS_PARTIAL)
1279                 sbflags = SBREAD_PARTIAL;
1280
1281         ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr, sbflags,
1282                         (flags & OPEN_CTREE_NO_DEVICES));
1283         if (ret)
1284                 goto out;
1285
1286         fs_info->fs_devices = fs_devices;
1287         if (flags & OPEN_CTREE_WRITES)
1288                 oflags = O_RDWR;
1289         else
1290                 oflags = O_RDONLY;
1291
1292         if (flags & OPEN_CTREE_EXCLUSIVE)
1293                 oflags |= O_EXCL;
1294
1295         ret = btrfs_open_devices(fs_devices, oflags);
1296         if (ret)
1297                 goto out;
1298
1299         disk_super = fs_info->super_copy;
1300         if (flags & OPEN_CTREE_RECOVER_SUPER)
1301                 ret = btrfs_read_dev_super(fs_devices->latest_bdev, disk_super,
1302                                 sb_bytenr, SBREAD_RECOVER);
1303         else
1304                 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr,
1305                                 sbflags);
1306         if (ret) {
1307                 printk("No valid btrfs found\n");
1308                 goto out_devices;
1309         }
1310
1311         if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID &&
1312             !fs_info->ignore_fsid_mismatch) {
1313                 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1314                 goto out_devices;
1315         }
1316
1317         memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1318
1319         ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1320                                            flags & OPEN_CTREE_WRITES);
1321         if (ret)
1322                 goto out_devices;
1323
1324         ret = btrfs_setup_chunk_tree_and_device_map(fs_info, chunk_root_bytenr);
1325         if (ret)
1326                 goto out_chunk;
1327
1328         /* Chunk tree root is unable to read, return directly */
1329         if (!fs_info->chunk_root)
1330                 return fs_info;
1331
1332         eb = fs_info->chunk_root->node;
1333         read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1334                            btrfs_header_chunk_tree_uuid(eb),
1335                            BTRFS_UUID_SIZE);
1336
1337         ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1338         if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT) &&
1339             !fs_info->ignore_chunk_tree_error)
1340                 goto out_chunk;
1341
1342         return fs_info;
1343
1344 out_chunk:
1345         btrfs_release_all_roots(fs_info);
1346         btrfs_cleanup_all_caches(fs_info);
1347 out_devices:
1348         btrfs_close_devices(fs_devices);
1349 out:
1350         btrfs_free_fs_info(fs_info);
1351         return NULL;
1352 }
1353
1354 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1355                                          u64 sb_bytenr, u64 root_tree_bytenr,
1356                                          u64 chunk_root_bytenr,
1357                                          unsigned flags)
1358 {
1359         int fp;
1360         int ret;
1361         struct btrfs_fs_info *info;
1362         int oflags = O_RDWR;
1363         struct stat st;
1364
1365         ret = stat(filename, &st);
1366         if (ret < 0) {
1367                 error("cannot stat '%s': %s", filename, strerror(errno));
1368                 return NULL;
1369         }
1370         if (!(((st.st_mode & S_IFMT) == S_IFREG) || ((st.st_mode & S_IFMT) == S_IFBLK))) {
1371                 error("not a regular file or block device: %s", filename);
1372                 return NULL;
1373         }
1374
1375         if (!(flags & OPEN_CTREE_WRITES))
1376                 oflags = O_RDONLY;
1377
1378         fp = open(filename, oflags);
1379         if (fp < 0) {
1380                 error("cannot open '%s': %s", filename, strerror(errno));
1381                 return NULL;
1382         }
1383         info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1384                                chunk_root_bytenr, flags);
1385         close(fp);
1386         return info;
1387 }
1388
1389 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1390                               unsigned flags)
1391 {
1392         struct btrfs_fs_info *info;
1393
1394         /* This flags may not return fs_info with any valid root */
1395         BUG_ON(flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
1396         info = open_ctree_fs_info(filename, sb_bytenr, 0, 0, flags);
1397         if (!info)
1398                 return NULL;
1399         if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1400                 return info->chunk_root;
1401         return info->fs_root;
1402 }
1403
1404 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1405                                  unsigned flags)
1406 {
1407         struct btrfs_fs_info *info;
1408
1409         /* This flags may not return fs_info with any valid root */
1410         BUG_ON(flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
1411         info = __open_ctree_fd(fp, path, sb_bytenr, 0, 0, flags);
1412         if (!info)
1413                 return NULL;
1414         if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1415                 return info->chunk_root;
1416         return info->fs_root;
1417 }
1418
1419 /*
1420  * Check if the super is valid:
1421  * - nodesize/sectorsize - minimum, maximum, alignment
1422  * - tree block starts   - alignment
1423  * - number of devices   - something sane
1424  * - sys array size      - maximum
1425  */
1426 static int check_super(struct btrfs_super_block *sb, unsigned sbflags)
1427 {
1428         char result[BTRFS_CSUM_SIZE];
1429         u32 crc;
1430         u16 csum_type;
1431         int csum_size;
1432
1433         if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
1434                 if (btrfs_super_magic(sb) == BTRFS_MAGIC_PARTIAL) {
1435                         if (!(sbflags & SBREAD_PARTIAL)) {
1436                                 error("superblock magic doesn't match");
1437                                 return -EIO;
1438                         }
1439                 }
1440         }
1441
1442         csum_type = btrfs_super_csum_type(sb);
1443         if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
1444                 error("unsupported checksum algorithm %u", csum_type);
1445                 return -EIO;
1446         }
1447         csum_size = btrfs_csum_sizes[csum_type];
1448
1449         crc = ~(u32)0;
1450         crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1451                               BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1452         btrfs_csum_final(crc, result);
1453
1454         if (memcmp(result, sb->csum, csum_size)) {
1455                 error("superblock checksum mismatch");
1456                 return -EIO;
1457         }
1458         if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
1459                 error("tree_root level too big: %d >= %d",
1460                         btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
1461                 goto error_out;
1462         }
1463         if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
1464                 error("chunk_root level too big: %d >= %d",
1465                         btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
1466                 goto error_out;
1467         }
1468         if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
1469                 error("log_root level too big: %d >= %d",
1470                         btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
1471                 goto error_out;
1472         }
1473
1474         if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
1475                 error("tree_root block unaligned: %llu", btrfs_super_root(sb));
1476                 goto error_out;
1477         }
1478         if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
1479                 error("chunk_root block unaligned: %llu",
1480                         btrfs_super_chunk_root(sb));
1481                 goto error_out;
1482         }
1483         if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
1484                 error("log_root block unaligned: %llu",
1485                         btrfs_super_log_root(sb));
1486                 goto error_out;
1487         }
1488         if (btrfs_super_nodesize(sb) < 4096) {
1489                 error("nodesize too small: %u < 4096",
1490                         btrfs_super_nodesize(sb));
1491                 goto error_out;
1492         }
1493         if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
1494                 error("nodesize unaligned: %u", btrfs_super_nodesize(sb));
1495                 goto error_out;
1496         }
1497         if (btrfs_super_sectorsize(sb) < 4096) {
1498                 error("sectorsize too small: %u < 4096",
1499                         btrfs_super_sectorsize(sb));
1500                 goto error_out;
1501         }
1502         if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
1503                 error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb));
1504                 goto error_out;
1505         }
1506         if (btrfs_super_total_bytes(sb) == 0) {
1507                 error("invalid total_bytes 0");
1508                 goto error_out;
1509         }
1510         if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
1511                 error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
1512                 goto error_out;
1513         }
1514         if ((btrfs_super_stripesize(sb) != 4096)
1515                 && (btrfs_super_stripesize(sb) != btrfs_super_sectorsize(sb))) {
1516                 error("invalid stripesize %u", btrfs_super_stripesize(sb));
1517                 goto error_out;
1518         }
1519
1520         if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
1521                 char fsid[BTRFS_UUID_UNPARSED_SIZE];
1522                 char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
1523
1524                 uuid_unparse(sb->fsid, fsid);
1525                 uuid_unparse(sb->dev_item.fsid, dev_fsid);
1526                 error("dev_item UUID does not match fsid: %s != %s",
1527                         dev_fsid, fsid);
1528                 goto error_out;
1529         }
1530
1531         /*
1532          * Hint to catch really bogus numbers, bitflips or so
1533          */
1534         if (btrfs_super_num_devices(sb) > (1UL << 31)) {
1535                 warning("suspicious number of devices: %llu",
1536                         btrfs_super_num_devices(sb));
1537         }
1538
1539         if (btrfs_super_num_devices(sb) == 0) {
1540                 error("number of devices is 0");
1541                 goto error_out;
1542         }
1543
1544         /*
1545          * Obvious sys_chunk_array corruptions, it must hold at least one key
1546          * and one chunk
1547          */
1548         if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
1549                 error("system chunk array too big %u > %u",
1550                       btrfs_super_sys_array_size(sb),
1551                       BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
1552                 goto error_out;
1553         }
1554         if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
1555                         + sizeof(struct btrfs_chunk)) {
1556                 error("system chunk array too small %u < %lu",
1557                       btrfs_super_sys_array_size(sb),
1558                       sizeof(struct btrfs_disk_key) +
1559                       sizeof(struct btrfs_chunk));
1560                 goto error_out;
1561         }
1562
1563         return 0;
1564
1565 error_out:
1566         error("superblock checksum matches but it has invalid members");
1567         return -EIO;
1568 }
1569
1570 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1571                          unsigned sbflags)
1572 {
1573         u8 fsid[BTRFS_FSID_SIZE];
1574         int fsid_is_initialized = 0;
1575         char tmp[BTRFS_SUPER_INFO_SIZE];
1576         struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
1577         int i;
1578         int ret;
1579         int max_super = sbflags & SBREAD_RECOVER ? BTRFS_SUPER_MIRROR_MAX : 1;
1580         u64 transid = 0;
1581         u64 bytenr;
1582
1583         if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1584                 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
1585                 if (ret < BTRFS_SUPER_INFO_SIZE)
1586                         return -1;
1587
1588                 if (btrfs_super_bytenr(buf) != sb_bytenr)
1589                         return -1;
1590
1591                 if (check_super(buf, sbflags))
1592                         return -1;
1593                 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1594                 return 0;
1595         }
1596
1597         /*
1598         * we would like to check all the supers, but that would make
1599         * a btrfs mount succeed after a mkfs from a different FS.
1600         * So, we need to add a special mount option to scan for
1601         * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1602         */
1603
1604         for (i = 0; i < max_super; i++) {
1605                 bytenr = btrfs_sb_offset(i);
1606                 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1607                 if (ret < BTRFS_SUPER_INFO_SIZE)
1608                         break;
1609
1610                 if (btrfs_super_bytenr(buf) != bytenr )
1611                         continue;
1612                 /* if magic is NULL, the device was removed */
1613                 if (btrfs_super_magic(buf) == 0 && i == 0)
1614                         break;
1615                 if (check_super(buf, sbflags))
1616                         continue;
1617
1618                 if (!fsid_is_initialized) {
1619                         memcpy(fsid, buf->fsid, sizeof(fsid));
1620                         fsid_is_initialized = 1;
1621                 } else if (memcmp(fsid, buf->fsid, sizeof(fsid))) {
1622                         /*
1623                          * the superblocks (the original one and
1624                          * its backups) contain data of different
1625                          * filesystems -> the super cannot be trusted
1626                          */
1627                         continue;
1628                 }
1629
1630                 if (btrfs_super_generation(buf) > transid) {
1631                         memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1632                         transid = btrfs_super_generation(buf);
1633                 }
1634         }
1635
1636         return transid > 0 ? 0 : -1;
1637 }
1638
1639 static int write_dev_supers(struct btrfs_root *root,
1640                             struct btrfs_super_block *sb,
1641                             struct btrfs_device *device)
1642 {
1643         u64 bytenr;
1644         u32 crc;
1645         int i, ret;
1646
1647         if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1648                 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1649                 crc = ~(u32)0;
1650                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1651                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1652                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1653
1654                 /*
1655                  * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1656                  * zero filled, we can use it directly
1657                  */
1658                 ret = pwrite64(device->fd, root->fs_info->super_copy,
1659                                 BTRFS_SUPER_INFO_SIZE,
1660                                 root->fs_info->super_bytenr);
1661                 if (ret != BTRFS_SUPER_INFO_SIZE)
1662                         goto write_err;
1663                 return 0;
1664         }
1665
1666         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1667                 bytenr = btrfs_sb_offset(i);
1668                 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1669                         break;
1670
1671                 btrfs_set_super_bytenr(sb, bytenr);
1672
1673                 crc = ~(u32)0;
1674                 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1675                                       BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1676                 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1677
1678                 /*
1679                  * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1680                  * zero filled, we can use it directly
1681                  */
1682                 ret = pwrite64(device->fd, root->fs_info->super_copy,
1683                                 BTRFS_SUPER_INFO_SIZE, bytenr);
1684                 if (ret != BTRFS_SUPER_INFO_SIZE)
1685                         goto write_err;
1686         }
1687
1688         return 0;
1689
1690 write_err:
1691         if (ret > 0)
1692                 fprintf(stderr, "WARNING: failed to write all sb data\n");
1693         else
1694                 fprintf(stderr, "WARNING: failed to write sb: %s\n",
1695                         strerror(errno));
1696         return ret;
1697 }
1698
1699 int write_all_supers(struct btrfs_root *root)
1700 {
1701         struct list_head *cur;
1702         struct list_head *head = &root->fs_info->fs_devices->devices;
1703         struct btrfs_device *dev;
1704         struct btrfs_super_block *sb;
1705         struct btrfs_dev_item *dev_item;
1706         int ret;
1707         u64 flags;
1708
1709         sb = root->fs_info->super_copy;
1710         dev_item = &sb->dev_item;
1711         list_for_each(cur, head) {
1712                 dev = list_entry(cur, struct btrfs_device, dev_list);
1713                 if (!dev->writeable)
1714                         continue;
1715
1716                 btrfs_set_stack_device_generation(dev_item, 0);
1717                 btrfs_set_stack_device_type(dev_item, dev->type);
1718                 btrfs_set_stack_device_id(dev_item, dev->devid);
1719                 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1720                 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1721                 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1722                 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1723                 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1724                 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1725                 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1726
1727                 flags = btrfs_super_flags(sb);
1728                 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1729
1730                 ret = write_dev_supers(root, sb, dev);
1731                 BUG_ON(ret);
1732         }
1733         return 0;
1734 }
1735
1736 int write_ctree_super(struct btrfs_trans_handle *trans,
1737                       struct btrfs_root *root)
1738 {
1739         int ret;
1740         struct btrfs_root *tree_root = root->fs_info->tree_root;
1741         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1742
1743         if (root->fs_info->readonly)
1744                 return 0;
1745
1746         btrfs_set_super_generation(root->fs_info->super_copy,
1747                                    trans->transid);
1748         btrfs_set_super_root(root->fs_info->super_copy,
1749                              tree_root->node->start);
1750         btrfs_set_super_root_level(root->fs_info->super_copy,
1751                                    btrfs_header_level(tree_root->node));
1752         btrfs_set_super_chunk_root(root->fs_info->super_copy,
1753                                    chunk_root->node->start);
1754         btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1755                                          btrfs_header_level(chunk_root->node));
1756         btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1757                                 btrfs_header_generation(chunk_root->node));
1758
1759         ret = write_all_supers(root);
1760         if (ret)
1761                 fprintf(stderr, "failed to write new super block err %d\n", ret);
1762         return ret;
1763 }
1764
1765 int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
1766 {
1767         int ret;
1768         struct btrfs_trans_handle *trans;
1769         struct btrfs_root *root = fs_info->tree_root;
1770
1771         if (fs_info->last_trans_committed !=
1772             fs_info->generation) {
1773                 BUG_ON(!root);
1774                 trans = btrfs_start_transaction(root, 1);
1775                 btrfs_commit_transaction(trans, root);
1776                 trans = btrfs_start_transaction(root, 1);
1777                 ret = commit_tree_roots(trans, fs_info);
1778                 BUG_ON(ret);
1779                 ret = __commit_transaction(trans, root);
1780                 BUG_ON(ret);
1781                 write_ctree_super(trans, root);
1782                 btrfs_free_transaction(root, trans);
1783         }
1784
1785         if (fs_info->finalize_on_close) {
1786                 btrfs_set_super_magic(fs_info->super_copy, BTRFS_MAGIC);
1787                 root->fs_info->finalize_on_close = 0;
1788                 ret = write_all_supers(root);
1789                 if (ret)
1790                         fprintf(stderr,
1791                                 "failed to write new super block err %d\n", ret);
1792         }
1793         btrfs_free_block_groups(fs_info);
1794
1795         free_fs_roots_tree(&fs_info->fs_root_tree);
1796
1797         btrfs_release_all_roots(fs_info);
1798         btrfs_close_devices(fs_info->fs_devices);
1799         btrfs_cleanup_all_caches(fs_info);
1800         btrfs_free_fs_info(fs_info);
1801         return 0;
1802 }
1803
1804 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1805                      struct extent_buffer *eb)
1806 {
1807         return clear_extent_buffer_dirty(eb);
1808 }
1809
1810 int wait_on_tree_block_writeback(struct btrfs_root *root,
1811                                  struct extent_buffer *eb)
1812 {
1813         return 0;
1814 }
1815
1816 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1817 {
1818         set_extent_buffer_dirty(eb);
1819 }
1820
1821 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1822 {
1823         int ret;
1824
1825         ret = extent_buffer_uptodate(buf);
1826         if (!ret)
1827                 return ret;
1828
1829         ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1830         return !ret;
1831 }
1832
1833 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1834 {
1835         return set_extent_buffer_uptodate(eb);
1836 }