1 // SPDX-License-Identifier: GPL-2.0+
3 #include <fs_internal.h>
6 #include "kernel-shared/btrfs_tree.h"
7 #include "common/rbtree-utils.h"
12 #include "extent-io.h"
13 #include "crypto/hash.h"
15 /* specified errno for check_tree_block */
16 #define BTRFS_BAD_BYTENR (-1)
17 #define BTRFS_BAD_FSID (-2)
18 #define BTRFS_BAD_LEVEL (-3)
19 #define BTRFS_BAD_NRITEMS (-4)
21 /* Calculate max possible nritems for a leaf/node */
22 static u32 max_nritems(u8 level, u32 nodesize)
26 return ((nodesize - sizeof(struct btrfs_header)) /
27 sizeof(struct btrfs_item));
28 return ((nodesize - sizeof(struct btrfs_header)) /
29 sizeof(struct btrfs_key_ptr));
32 static int check_tree_block(struct btrfs_fs_info *fs_info,
33 struct extent_buffer *buf)
36 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
37 u32 nodesize = fs_info->nodesize;
38 bool fsid_match = false;
39 int ret = BTRFS_BAD_FSID;
41 if (buf->start != btrfs_header_bytenr(buf))
42 return BTRFS_BAD_BYTENR;
43 if (btrfs_header_level(buf) >= BTRFS_MAX_LEVEL)
44 return BTRFS_BAD_LEVEL;
45 if (btrfs_header_nritems(buf) > max_nritems(btrfs_header_level(buf),
47 return BTRFS_BAD_NRITEMS;
49 /* Only leaf can be empty */
50 if (btrfs_header_nritems(buf) == 0 &&
51 btrfs_header_level(buf) != 0)
52 return BTRFS_BAD_NRITEMS;
56 * Checking the incompat flag is only valid for the current
57 * fs. For seed devices it's forbidden to have their uuid
58 * changed so reading ->fsid in this case is fine
60 if (fs_devices == fs_info->fs_devices &&
61 btrfs_fs_incompat(fs_info, METADATA_UUID))
62 fsid_match = !memcmp_extent_buffer(buf,
63 fs_devices->metadata_uuid,
67 fsid_match = !memcmp_extent_buffer(buf,
77 fs_devices = fs_devices->seed;
82 static void print_tree_block_error(struct btrfs_fs_info *fs_info,
83 struct extent_buffer *eb,
86 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
87 char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
88 u8 buf[BTRFS_UUID_SIZE];
93 fprintf(stderr, "bad tree block %llu, ", eb->start);
96 read_extent_buffer(eb, buf, btrfs_header_fsid(),
98 uuid_unparse(buf, found_uuid);
99 uuid_unparse(fs_info->fs_devices->metadata_uuid, fs_uuid);
100 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
101 fs_uuid, found_uuid);
103 case BTRFS_BAD_BYTENR:
104 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
105 eb->start, btrfs_header_bytenr(eb));
107 case BTRFS_BAD_LEVEL:
108 fprintf(stderr, "bad level, %u > %d\n",
109 btrfs_header_level(eb), BTRFS_MAX_LEVEL);
111 case BTRFS_BAD_NRITEMS:
112 fprintf(stderr, "invalid nr_items: %u\n",
113 btrfs_header_nritems(eb));
118 int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len)
120 memset(out, 0, BTRFS_CSUM_SIZE);
123 case BTRFS_CSUM_TYPE_CRC32:
124 return hash_crc32c(data, len, out);
125 case BTRFS_CSUM_TYPE_XXHASH:
126 return hash_xxhash(data, len, out);
127 case BTRFS_CSUM_TYPE_SHA256:
128 return hash_sha256(data, len, out);
130 printf("Unknown csum type %d\n", csum_type);
136 * Check if the super is valid:
137 * - nodesize/sectorsize - minimum, maximum, alignment
138 * - tree block starts - alignment
139 * - number of devices - something sane
140 * - sys array size - maximum
142 static int btrfs_check_super(struct btrfs_super_block *sb)
144 u8 result[BTRFS_CSUM_SIZE];
149 if (btrfs_super_magic(sb) != BTRFS_MAGIC)
152 csum_type = btrfs_super_csum_type(sb);
153 if (csum_type >= btrfs_super_num_csums()) {
154 error("unsupported checksum algorithm %u", csum_type);
157 csum_size = btrfs_super_csum_size(sb);
159 btrfs_csum_data(csum_type, (u8 *)sb + BTRFS_CSUM_SIZE,
160 result, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
162 if (memcmp(result, sb->csum, csum_size)) {
163 error("superblock checksum mismatch");
166 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
167 error("tree_root level too big: %d >= %d",
168 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
171 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
172 error("chunk_root level too big: %d >= %d",
173 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
176 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
177 error("log_root level too big: %d >= %d",
178 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
182 if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
183 error("tree_root block unaligned: %llu", btrfs_super_root(sb));
186 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
187 error("chunk_root block unaligned: %llu",
188 btrfs_super_chunk_root(sb));
191 if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
192 error("log_root block unaligned: %llu",
193 btrfs_super_log_root(sb));
196 if (btrfs_super_nodesize(sb) < 4096) {
197 error("nodesize too small: %u < 4096",
198 btrfs_super_nodesize(sb));
201 if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
202 error("nodesize unaligned: %u", btrfs_super_nodesize(sb));
205 if (btrfs_super_sectorsize(sb) < 4096) {
206 error("sectorsize too small: %u < 4096",
207 btrfs_super_sectorsize(sb));
210 if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
211 error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb));
214 if (btrfs_super_total_bytes(sb) == 0) {
215 error("invalid total_bytes 0");
218 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
219 error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
222 if ((btrfs_super_stripesize(sb) != 4096)
223 && (btrfs_super_stripesize(sb) != btrfs_super_sectorsize(sb))) {
224 error("invalid stripesize %u", btrfs_super_stripesize(sb));
228 if (btrfs_super_incompat_flags(sb) & BTRFS_FEATURE_INCOMPAT_METADATA_UUID)
229 metadata_uuid = sb->metadata_uuid;
231 metadata_uuid = sb->fsid;
233 if (memcmp(metadata_uuid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) {
234 char fsid[BTRFS_UUID_UNPARSED_SIZE];
235 char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
237 uuid_unparse(sb->metadata_uuid, fsid);
238 uuid_unparse(sb->dev_item.fsid, dev_fsid);
239 error("dev_item UUID does not match fsid: %s != %s",
245 * Hint to catch really bogus numbers, bitflips or so
247 if (btrfs_super_num_devices(sb) > (1UL << 31)) {
248 error("suspicious number of devices: %llu",
249 btrfs_super_num_devices(sb));
252 if (btrfs_super_num_devices(sb) == 0) {
253 error("number of devices is 0");
258 * Obvious sys_chunk_array corruptions, it must hold at least one key
261 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
262 error("system chunk array too big %u > %u",
263 btrfs_super_sys_array_size(sb),
264 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
267 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
268 + sizeof(struct btrfs_chunk)) {
269 error("system chunk array too small %u < %zu",
270 btrfs_super_sys_array_size(sb),
271 sizeof(struct btrfs_disk_key) +
272 sizeof(struct btrfs_chunk));
279 error("superblock checksum matches but it has invalid members");
284 * btrfs_read_dev_super - read a valid primary superblock from a block device
285 * @desc,@part: file descriptor of the device
286 * @sb: buffer where the superblock is going to be read in
288 * Unlike the btrfs-progs/kernel version, here we ony care about the first
289 * super block, thus it's much simpler.
291 int btrfs_read_dev_super(struct blk_desc *desc, struct disk_partition *part,
292 struct btrfs_super_block *sb)
294 char tmp[BTRFS_SUPER_INFO_SIZE];
295 struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
298 ret = __btrfs_devread(desc, part, tmp, BTRFS_SUPER_INFO_SIZE,
299 BTRFS_SUPER_INFO_OFFSET);
300 if (ret < BTRFS_SUPER_INFO_SIZE)
303 if (btrfs_super_bytenr(buf) != BTRFS_SUPER_INFO_OFFSET)
306 if (btrfs_check_super(buf))
309 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
313 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
314 int verify, int silent, u16 csum_type)
316 u8 result[BTRFS_CSUM_SIZE];
319 len = buf->len - BTRFS_CSUM_SIZE;
320 btrfs_csum_data(csum_type, (u8 *)buf->data + BTRFS_CSUM_SIZE,
324 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
327 printk("checksum verify failed on %llu found %08X wanted %08X\n",
328 (unsigned long long)buf->start,
334 write_extent_buffer(buf, result, 0, csum_size);
339 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify,
342 return __csum_tree_block_size(buf, csum_size, verify, 0, csum_type);
345 static int csum_tree_block(struct btrfs_fs_info *fs_info,
346 struct extent_buffer *buf, int verify)
348 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
349 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
351 return csum_tree_block_size(buf, csum_size, verify, csum_type);
354 struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
355 u64 bytenr, u32 blocksize)
357 return find_extent_buffer(&fs_info->extent_cache,
361 struct extent_buffer* btrfs_find_create_tree_block(
362 struct btrfs_fs_info *fs_info, u64 bytenr)
364 return alloc_extent_buffer(fs_info, bytenr, fs_info->nodesize);
367 static int verify_parent_transid(struct extent_io_tree *io_tree,
368 struct extent_buffer *eb, u64 parent_transid,
373 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
376 if (extent_buffer_uptodate(eb) &&
377 btrfs_header_generation(eb) == parent_transid) {
381 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
382 (unsigned long long)eb->start,
383 (unsigned long long)parent_transid,
384 (unsigned long long)btrfs_header_generation(eb));
386 eb->flags |= EXTENT_BAD_TRANSID;
387 printk("Ignoring transid failure\n");
393 clear_extent_buffer_uptodate(eb);
398 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
400 unsigned long offset = 0;
401 struct btrfs_multi_bio *multi = NULL;
402 struct btrfs_device *device;
405 unsigned long bytes_left = eb->len;
408 read_len = bytes_left;
411 ret = btrfs_map_block(info, READ, eb->start + offset,
412 &read_len, &multi, mirror, NULL);
414 printk("Couldn't map the block %Lu\n", eb->start + offset);
418 device = multi->stripes[0].dev;
420 if (!device->desc || !device->part) {
425 if (read_len > bytes_left)
426 read_len = bytes_left;
428 ret = read_extent_from_disk(device->desc, device->part,
429 multi->stripes[0].physical, eb,
437 bytes_left -= read_len;
442 struct extent_buffer* read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
446 struct extent_buffer *eb;
447 u64 best_transid = 0;
448 u32 sectorsize = fs_info->sectorsize;
451 int candidate_mirror = 0;
456 * Don't even try to create tree block for unaligned tree block
458 * Such unaligned tree block will free overlapping extent buffer,
459 * causing use-after-free bugs for fuzzed images.
461 if (bytenr < sectorsize || !IS_ALIGNED(bytenr, sectorsize)) {
462 error("tree block bytenr %llu is not aligned to sectorsize %u",
464 return ERR_PTR(-EIO);
467 eb = btrfs_find_create_tree_block(fs_info, bytenr);
469 return ERR_PTR(-ENOMEM);
471 if (btrfs_buffer_uptodate(eb, parent_transid))
474 num_copies = btrfs_num_copies(fs_info, eb->start, eb->len);
476 ret = read_whole_eb(fs_info, eb, mirror_num);
477 if (ret == 0 && csum_tree_block(fs_info, eb, 1) == 0 &&
478 check_tree_block(fs_info, eb) == 0 &&
479 verify_parent_transid(&fs_info->extent_cache, eb,
480 parent_transid, ignore) == 0) {
482 * check_tree_block() is less strict to allow btrfs
483 * check to get raw eb with bad key order and fix it.
484 * But we still need to try to get a good copy if
485 * possible, or bad key order can go into tools like
486 * btrfs ins dump-tree.
488 if (btrfs_header_level(eb))
489 ret = btrfs_check_node(fs_info, NULL, eb);
491 ret = btrfs_check_leaf(fs_info, NULL, eb);
492 if (!ret || candidate_mirror == mirror_num) {
493 btrfs_set_buffer_uptodate(eb);
496 if (candidate_mirror <= 0)
497 candidate_mirror = mirror_num;
500 if (candidate_mirror > 0) {
501 mirror_num = candidate_mirror;
504 if (check_tree_block(fs_info, eb))
505 print_tree_block_error(fs_info, eb,
506 check_tree_block(fs_info, eb));
508 fprintf(stderr, "Csum didn't match\n");
512 if (num_copies == 1) {
516 if (btrfs_header_generation(eb) > best_transid) {
517 best_transid = btrfs_header_generation(eb);
518 good_mirror = mirror_num;
521 if (mirror_num > num_copies) {
522 if (candidate_mirror > 0)
523 mirror_num = candidate_mirror;
525 mirror_num = good_mirror;
531 * We failed to read this tree block, it be should deleted right now
532 * to avoid stale cache populate the cache.
534 free_extent_buffer(eb);
538 int read_extent_data(struct btrfs_fs_info *fs_info, char *data, u64 logical,
539 u64 *len, int mirror)
542 struct btrfs_multi_bio *multi = NULL;
543 struct btrfs_device *device;
547 ret = btrfs_map_block(fs_info, READ, logical, len, &multi, mirror,
550 fprintf(stderr, "Couldn't map the block %llu\n",
554 device = multi->stripes[0].dev;
558 if (!device->desc || !device->part) {
563 ret = __btrfs_devread(device->desc, device->part, data, *len,
564 multi->stripes[0].physical);
574 void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
578 root->track_dirty = 0;
580 root->fs_info = fs_info;
581 root->objectid = objectid;
582 root->last_trans = 0;
583 root->last_inode_alloc = 0;
585 memset(&root->root_key, 0, sizeof(root->root_key));
586 memset(&root->root_item, 0, sizeof(root->root_item));
587 root->root_key.objectid = objectid;
590 static int find_and_setup_root(struct btrfs_root *tree_root,
591 struct btrfs_fs_info *fs_info,
592 u64 objectid, struct btrfs_root *root)
597 btrfs_setup_root(root, fs_info, objectid);
598 ret = btrfs_find_last_root(tree_root, objectid,
599 &root->root_item, &root->root_key);
603 generation = btrfs_root_generation(&root->root_item);
604 root->node = read_tree_block(fs_info,
605 btrfs_root_bytenr(&root->root_item), generation);
606 if (!extent_buffer_uptodate(root->node))
612 int btrfs_free_fs_root(struct btrfs_root *root)
615 free_extent_buffer(root->node);
620 static void __free_fs_root(struct rb_node *node)
622 struct btrfs_root *root;
624 root = container_of(node, struct btrfs_root, rb_node);
625 btrfs_free_fs_root(root);
628 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
630 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
631 struct btrfs_key *location)
633 struct btrfs_root *root;
634 struct btrfs_root *tree_root = fs_info->tree_root;
635 struct btrfs_path *path;
636 struct extent_buffer *l;
640 root = calloc(1, sizeof(*root));
642 return ERR_PTR(-ENOMEM);
643 if (location->offset == (u64)-1) {
644 ret = find_and_setup_root(tree_root, fs_info,
645 location->objectid, root);
653 btrfs_setup_root(root, fs_info,
656 path = btrfs_alloc_path();
659 return ERR_PTR(-ENOMEM);
662 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
669 read_extent_buffer(l, &root->root_item,
670 btrfs_item_ptr_offset(l, path->slots[0]),
671 sizeof(root->root_item));
672 memcpy(&root->root_key, location, sizeof(*location));
674 /* If this root is already an orphan, no need to read */
675 if (btrfs_root_refs(&root->root_item) == 0) {
681 btrfs_free_path(path);
686 generation = btrfs_root_generation(&root->root_item);
687 root->node = read_tree_block(fs_info,
688 btrfs_root_bytenr(&root->root_item), generation);
689 if (!extent_buffer_uptodate(root->node)) {
691 return ERR_PTR(-EIO);
698 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
701 u64 objectid = *((u64 *)data);
702 struct btrfs_root *root;
704 root = rb_entry(node, struct btrfs_root, rb_node);
705 if (objectid > root->objectid)
707 else if (objectid < root->objectid)
713 int btrfs_fs_roots_compare_roots(struct rb_node *node1, struct rb_node *node2)
715 struct btrfs_root *root;
717 root = rb_entry(node2, struct btrfs_root, rb_node);
718 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
721 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
722 struct btrfs_key *location)
724 struct btrfs_root *root;
725 struct rb_node *node;
727 u64 objectid = location->objectid;
729 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
730 return fs_info->tree_root;
731 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
732 return fs_info->chunk_root;
733 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
734 return fs_info->csum_root;
735 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
736 location->offset != (u64)-1);
738 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
739 btrfs_fs_roots_compare_objectids, NULL);
741 return container_of(node, struct btrfs_root, rb_node);
743 root = btrfs_read_fs_root_no_cache(fs_info, location);
747 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
748 btrfs_fs_roots_compare_roots);
753 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
755 free(fs_info->tree_root);
756 free(fs_info->chunk_root);
757 free(fs_info->csum_root);
758 free(fs_info->super_copy);
762 struct btrfs_fs_info *btrfs_new_fs_info(void)
764 struct btrfs_fs_info *fs_info;
766 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
770 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
771 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
772 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
773 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
775 if (!fs_info->tree_root || !fs_info->chunk_root ||
776 !fs_info->csum_root || !fs_info->super_copy)
779 extent_io_tree_init(&fs_info->extent_cache);
781 fs_info->fs_root_tree = RB_ROOT;
782 cache_tree_init(&fs_info->mapping_tree.cache_tree);
784 mutex_init(&fs_info->fs_mutex);
788 btrfs_free_fs_info(fs_info);
792 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
793 struct btrfs_root *info_root,
794 u64 objectid, char *str)
796 struct btrfs_root *root = fs_info->tree_root;
799 ret = find_and_setup_root(root, fs_info, objectid, info_root);
801 error("could not setup %s tree", str);
808 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info)
810 struct btrfs_super_block *sb = fs_info->super_copy;
811 struct btrfs_root *root;
812 struct btrfs_key key;
813 u64 root_tree_bytenr;
817 root = fs_info->tree_root;
818 btrfs_setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
819 generation = btrfs_super_generation(sb);
821 root_tree_bytenr = btrfs_super_root(sb);
823 root->node = read_tree_block(fs_info, root_tree_bytenr, generation);
824 if (!extent_buffer_uptodate(root->node)) {
825 fprintf(stderr, "Couldn't read tree root\n");
829 ret = setup_root_or_create_block(fs_info, fs_info->csum_root,
830 BTRFS_CSUM_TREE_OBJECTID, "csum");
833 fs_info->csum_root->track_dirty = 1;
835 fs_info->last_trans_committed = generation;
837 key.objectid = BTRFS_FS_TREE_OBJECTID;
838 key.type = BTRFS_ROOT_ITEM_KEY;
839 key.offset = (u64)-1;
840 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
842 if (IS_ERR(fs_info->fs_root))
847 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
849 if (fs_info->csum_root)
850 free_extent_buffer(fs_info->csum_root->node);
851 if (fs_info->tree_root)
852 free_extent_buffer(fs_info->tree_root->node);
853 if (fs_info->chunk_root)
854 free_extent_buffer(fs_info->chunk_root->node);
857 static void free_map_lookup(struct cache_extent *ce)
859 struct map_lookup *map;
861 map = container_of(ce, struct map_lookup, ce);
865 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
867 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
869 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
870 extent_io_tree_cleanup(&fs_info->extent_cache);
873 static int btrfs_scan_fs_devices(struct blk_desc *desc,
874 struct disk_partition *part,
875 struct btrfs_fs_devices **fs_devices)
880 if (round_up(BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
881 desc->blksz) > (part->size << desc->log2blksz)) {
882 error("superblock end %u is larger than device size " LBAFU,
883 BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
884 part->size << desc->log2blksz);
888 ret = btrfs_scan_one_device(desc, part, fs_devices, &total_devs);
890 fprintf(stderr, "No valid Btrfs found\n");
896 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb)
900 features = btrfs_super_incompat_flags(sb) &
901 ~BTRFS_FEATURE_INCOMPAT_SUPP;
903 printk("couldn't open because of unsupported "
904 "option features (%llx).\n",
905 (unsigned long long)features);
909 features = btrfs_super_incompat_flags(sb);
910 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
911 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
912 btrfs_set_super_incompat_flags(sb, features);
918 static int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
920 struct btrfs_super_block *sb = fs_info->super_copy;
921 u64 chunk_root_bytenr;
925 btrfs_setup_root(fs_info->chunk_root, fs_info,
926 BTRFS_CHUNK_TREE_OBJECTID);
928 ret = btrfs_read_sys_array(fs_info);
932 generation = btrfs_super_chunk_root_generation(sb);
933 chunk_root_bytenr = btrfs_super_chunk_root(sb);
935 fs_info->chunk_root->node = read_tree_block(fs_info,
938 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
939 error("cannot read chunk root");
943 ret = btrfs_read_chunk_tree(fs_info);
945 fprintf(stderr, "Couldn't read chunk tree\n");
951 struct btrfs_fs_info *open_ctree_fs_info(struct blk_desc *desc,
952 struct disk_partition *part)
954 struct btrfs_fs_info *fs_info;
955 struct btrfs_super_block *disk_super;
956 struct btrfs_fs_devices *fs_devices = NULL;
957 struct extent_buffer *eb;
960 fs_info = btrfs_new_fs_info();
962 fprintf(stderr, "Failed to allocate memory for fs_info\n");
966 ret = btrfs_scan_fs_devices(desc, part, &fs_devices);
970 fs_info->fs_devices = fs_devices;
972 ret = btrfs_open_devices(fs_devices);
976 disk_super = fs_info->super_copy;
977 ret = btrfs_read_dev_super(desc, part, disk_super);
979 printk("No valid btrfs found\n");
983 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID) {
984 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
988 ASSERT(!memcmp(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE));
989 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
990 ASSERT(!memcmp(disk_super->metadata_uuid,
991 fs_devices->metadata_uuid, BTRFS_FSID_SIZE));
993 fs_info->sectorsize = btrfs_super_sectorsize(disk_super);
994 fs_info->nodesize = btrfs_super_nodesize(disk_super);
995 fs_info->stripesize = btrfs_super_stripesize(disk_super);
997 ret = btrfs_check_fs_compatibility(fs_info->super_copy);
1001 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1005 /* Chunk tree root is unable to read, return directly */
1006 if (!fs_info->chunk_root)
1009 eb = fs_info->chunk_root->node;
1010 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1011 btrfs_header_chunk_tree_uuid(eb),
1014 ret = btrfs_setup_all_roots(fs_info);
1021 btrfs_release_all_roots(fs_info);
1022 btrfs_cleanup_all_caches(fs_info);
1024 btrfs_close_devices(fs_devices);
1026 btrfs_free_fs_info(fs_info);
1030 int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
1035 free_fs_roots_tree(&fs_info->fs_root_tree);
1037 btrfs_release_all_roots(fs_info);
1038 ret = btrfs_close_devices(fs_info->fs_devices);
1039 btrfs_cleanup_all_caches(fs_info);
1040 btrfs_free_fs_info(fs_info);
1046 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1050 ret = extent_buffer_uptodate(buf);
1054 ret = verify_parent_transid(&buf->fs_info->extent_cache, buf,
1059 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1061 return set_extent_buffer_uptodate(eb);