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