2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
21 #include <sys/types.h>
25 #include <uuid/uuid.h>
26 #include "kerncompat.h"
27 #include "radix-tree.h"
31 #include "transaction.h"
34 #include "print-tree.h"
35 #include "rbtree-utils.h"
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)
43 /* Calculate max possible nritems for a leaf/node */
44 static u32 max_nritems(u8 level, u32 nodesize)
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));
54 static int check_tree_block(struct btrfs_fs_info *fs_info,
55 struct extent_buffer *buf)
58 struct btrfs_fs_devices *fs_devices;
59 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
60 int ret = BTRFS_BAD_FSID;
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),
68 return BTRFS_BAD_NRITEMS;
70 /* Only leaf can be empty */
71 if (btrfs_header_nritems(buf) == 0 &&
72 btrfs_header_level(buf) != 0)
73 return BTRFS_BAD_NRITEMS;
75 fs_devices = fs_info->fs_devices;
77 if (fs_info->ignore_fsid_mismatch ||
78 !memcmp_extent_buffer(buf, fs_devices->fsid,
84 fs_devices = fs_devices->seed;
89 static void print_tree_block_error(struct btrfs_fs_info *fs_info,
90 struct extent_buffer *eb,
93 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
94 char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
95 u8 buf[BTRFS_UUID_SIZE];
99 read_extent_buffer(eb, buf, btrfs_header_fsid(),
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);
106 case BTRFS_BAD_BYTENR:
107 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
108 eb->start, btrfs_header_bytenr(eb));
110 case BTRFS_BAD_LEVEL:
111 fprintf(stderr, "bad level, %u > %u\n",
112 btrfs_header_level(eb), BTRFS_MAX_LEVEL);
114 case BTRFS_BAD_NRITEMS:
115 fprintf(stderr, "invalid nr_items: %u\n",
116 btrfs_header_nritems(eb));
121 u32 btrfs_csum_data(char *data, u32 seed, size_t len)
123 return crc32c(seed, data, len);
126 void btrfs_csum_final(u32 crc, u8 *result)
128 put_unaligned_le32(~crc, result);
131 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
132 int verify, int silent)
134 u8 result[BTRFS_CSUM_SIZE];
138 len = buf->len - BTRFS_CSUM_SIZE;
139 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
140 btrfs_csum_final(crc, result);
143 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
145 printk("checksum verify failed on %llu found %08X wanted %08X\n",
146 (unsigned long long)buf->start,
148 *((u32*)(char *)buf->data));
152 write_extent_buffer(buf, result, 0, csum_size);
157 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
159 return __csum_tree_block_size(buf, csum_size, verify, 0);
162 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
164 return __csum_tree_block_size(buf, csum_size, 1, 1);
167 static int csum_tree_block_fs_info(struct btrfs_fs_info *fs_info,
168 struct extent_buffer *buf, int verify)
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);
177 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
180 return csum_tree_block_fs_info(root->fs_info, buf, verify);
183 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
184 u64 bytenr, u32 blocksize)
186 return find_extent_buffer(&root->fs_info->extent_cache,
190 struct extent_buffer* btrfs_find_create_tree_block(
191 struct btrfs_fs_info *fs_info, u64 bytenr, u32 blocksize)
193 return alloc_extent_buffer(&fs_info->extent_cache, bytenr, blocksize);
196 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
199 struct extent_buffer *eb;
201 struct btrfs_multi_bio *multi = NULL;
202 struct btrfs_device *device;
204 eb = btrfs_find_tree_block(root, bytenr, blocksize);
205 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
206 !btrfs_map_block(&root->fs_info->mapping_tree, READ,
207 bytenr, &length, &multi, 0, NULL)) {
208 device = multi->stripes[0].dev;
210 blocksize = min(blocksize, (u32)SZ_64K);
211 readahead(device->fd, multi->stripes[0].physical, blocksize);
214 free_extent_buffer(eb);
218 static int verify_parent_transid(struct extent_io_tree *io_tree,
219 struct extent_buffer *eb, u64 parent_transid,
224 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
227 if (extent_buffer_uptodate(eb) &&
228 btrfs_header_generation(eb) == parent_transid) {
232 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
233 (unsigned long long)eb->start,
234 (unsigned long long)parent_transid,
235 (unsigned long long)btrfs_header_generation(eb));
237 eb->flags |= EXTENT_BAD_TRANSID;
238 printk("Ignoring transid failure\n");
244 clear_extent_buffer_uptodate(eb);
250 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
252 unsigned long offset = 0;
253 struct btrfs_multi_bio *multi = NULL;
254 struct btrfs_device *device;
257 unsigned long bytes_left = eb->len;
260 read_len = bytes_left;
263 if (!info->on_restoring &&
264 eb->start != BTRFS_SUPER_INFO_OFFSET) {
265 ret = btrfs_map_block(&info->mapping_tree, READ,
266 eb->start + offset, &read_len, &multi,
269 printk("Couldn't map the block %Lu\n", eb->start + offset);
273 device = multi->stripes[0].dev;
275 if (device->fd <= 0) {
282 eb->dev_bytenr = multi->stripes[0].physical;
286 /* special case for restore metadump */
287 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
288 if (device->devid == 1)
293 eb->dev_bytenr = eb->start;
297 if (read_len > bytes_left)
298 read_len = bytes_left;
300 ret = read_extent_from_disk(eb, offset, read_len);
304 bytes_left -= read_len;
309 struct extent_buffer* read_tree_block_fs_info(
310 struct btrfs_fs_info *fs_info, u64 bytenr, u32 blocksize,
314 struct extent_buffer *eb;
315 u64 best_transid = 0;
316 u32 sectorsize = btrfs_super_sectorsize(fs_info->super_copy);
317 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
324 * Don't even try to create tree block for unaligned tree block
326 * Such unaligned tree block will free overlapping extent buffer,
327 * causing use-after-free bugs for fuzzed images.
329 if (bytenr < sectorsize || !IS_ALIGNED(bytenr, sectorsize)) {
330 error("tree block bytenr %llu is not aligned to sectorsize %u",
332 return ERR_PTR(-EIO);
334 if (blocksize < nodesize || !IS_ALIGNED(blocksize, nodesize)) {
335 error("tree block size %u is not aligned to nodesize %u",
336 blocksize, nodesize);
337 return ERR_PTR(-EIO);
340 eb = btrfs_find_create_tree_block(fs_info, bytenr, blocksize);
342 return ERR_PTR(-ENOMEM);
344 if (btrfs_buffer_uptodate(eb, parent_transid))
348 ret = read_whole_eb(fs_info, eb, mirror_num);
349 if (ret == 0 && csum_tree_block_fs_info(fs_info, eb, 1) == 0 &&
350 check_tree_block(fs_info, eb) == 0 &&
351 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
353 if (eb->flags & EXTENT_BAD_TRANSID &&
354 list_empty(&eb->recow)) {
355 list_add_tail(&eb->recow,
356 &fs_info->recow_ebs);
359 btrfs_set_buffer_uptodate(eb);
363 if (check_tree_block(fs_info, eb)) {
364 if (!fs_info->suppress_check_block_errors)
365 print_tree_block_error(fs_info, eb,
366 check_tree_block(fs_info, eb));
368 if (!fs_info->suppress_check_block_errors)
369 fprintf(stderr, "Csum didn't match\n");
374 num_copies = btrfs_num_copies(&fs_info->mapping_tree,
376 if (num_copies == 1) {
380 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
381 best_transid = btrfs_header_generation(eb);
382 good_mirror = mirror_num;
385 if (mirror_num > num_copies) {
386 mirror_num = good_mirror;
391 free_extent_buffer(eb);
395 int read_extent_data(struct btrfs_root *root, char *data,
396 u64 logical, u64 *len, int mirror)
399 struct btrfs_multi_bio *multi = NULL;
400 struct btrfs_fs_info *info = root->fs_info;
401 struct btrfs_device *device;
405 ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
406 &multi, mirror, NULL);
408 fprintf(stderr, "Couldn't map the block %llu\n",
412 device = multi->stripes[0].dev;
419 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
429 int write_and_map_eb(struct btrfs_root *root, struct extent_buffer *eb)
434 u64 *raid_map = NULL;
435 struct btrfs_multi_bio *multi = NULL;
439 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
440 eb->start, &length, &multi, 0, &raid_map);
443 ret = write_raid56_with_parity(root->fs_info, eb, multi,
446 } else while (dev_nr < multi->num_stripes) {
448 eb->fd = multi->stripes[dev_nr].dev->fd;
449 eb->dev_bytenr = multi->stripes[dev_nr].physical;
450 multi->stripes[dev_nr].dev->total_ios++;
452 ret = write_extent_to_disk(eb);
460 int write_tree_block(struct btrfs_trans_handle *trans,
461 struct btrfs_root *root,
462 struct extent_buffer *eb)
464 if (check_tree_block(root->fs_info, eb)) {
465 print_tree_block_error(root->fs_info, eb,
466 check_tree_block(root->fs_info, eb));
470 if (trans && !btrfs_buffer_uptodate(eb, trans->transid))
473 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
474 csum_tree_block(root, eb, 0);
476 return write_and_map_eb(root, eb);
479 void btrfs_setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
480 u32 stripesize, struct btrfs_root *root,
481 struct btrfs_fs_info *fs_info, u64 objectid)
484 root->commit_root = NULL;
485 root->sectorsize = sectorsize;
486 root->nodesize = nodesize;
487 root->leafsize = leafsize;
488 root->stripesize = stripesize;
490 root->track_dirty = 0;
492 root->fs_info = fs_info;
493 root->objectid = objectid;
494 root->last_trans = 0;
495 root->last_inode_alloc = 0;
497 INIT_LIST_HEAD(&root->dirty_list);
498 INIT_LIST_HEAD(&root->orphan_data_extents);
499 memset(&root->root_key, 0, sizeof(root->root_key));
500 memset(&root->root_item, 0, sizeof(root->root_item));
501 root->root_key.objectid = objectid;
504 static int update_cowonly_root(struct btrfs_trans_handle *trans,
505 struct btrfs_root *root)
509 struct btrfs_root *tree_root = root->fs_info->tree_root;
511 btrfs_write_dirty_block_groups(trans, root);
513 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
514 if (old_root_bytenr == root->node->start)
516 btrfs_set_root_bytenr(&root->root_item,
518 btrfs_set_root_generation(&root->root_item,
520 root->root_item.level = btrfs_header_level(root->node);
521 ret = btrfs_update_root(trans, tree_root,
525 btrfs_write_dirty_block_groups(trans, root);
530 static int commit_tree_roots(struct btrfs_trans_handle *trans,
531 struct btrfs_fs_info *fs_info)
533 struct btrfs_root *root;
534 struct list_head *next;
535 struct extent_buffer *eb;
538 if (fs_info->readonly)
541 eb = fs_info->tree_root->node;
542 extent_buffer_get(eb);
543 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
544 free_extent_buffer(eb);
548 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
549 next = fs_info->dirty_cowonly_roots.next;
551 root = list_entry(next, struct btrfs_root, dirty_list);
552 update_cowonly_root(trans, root);
553 free_extent_buffer(root->commit_root);
554 root->commit_root = NULL;
560 static int __commit_transaction(struct btrfs_trans_handle *trans,
561 struct btrfs_root *root)
565 struct extent_buffer *eb;
566 struct extent_io_tree *tree = &root->fs_info->extent_cache;
570 ret = find_first_extent_bit(tree, 0, &start, &end,
574 while(start <= end) {
575 eb = find_first_extent_buffer(tree, start);
576 BUG_ON(!eb || eb->start != start);
577 ret = write_tree_block(trans, root, eb);
580 clear_extent_buffer_dirty(eb);
581 free_extent_buffer(eb);
587 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
588 struct btrfs_root *root)
590 u64 transid = trans->transid;
592 struct btrfs_fs_info *fs_info = root->fs_info;
594 if (root->commit_root == root->node)
596 if (root == root->fs_info->tree_root)
598 if (root == root->fs_info->chunk_root)
601 free_extent_buffer(root->commit_root);
602 root->commit_root = NULL;
604 btrfs_set_root_bytenr(&root->root_item, root->node->start);
605 btrfs_set_root_generation(&root->root_item, trans->transid);
606 root->root_item.level = btrfs_header_level(root->node);
607 ret = btrfs_update_root(trans, root->fs_info->tree_root,
608 &root->root_key, &root->root_item);
611 ret = commit_tree_roots(trans, fs_info);
613 ret = __commit_transaction(trans, root);
615 write_ctree_super(trans, root);
616 btrfs_finish_extent_commit(trans, fs_info->extent_root,
617 &fs_info->pinned_extents);
619 free_extent_buffer(root->commit_root);
620 root->commit_root = NULL;
621 fs_info->running_transaction = NULL;
622 fs_info->last_trans_committed = transid;
626 static int find_and_setup_root(struct btrfs_root *tree_root,
627 struct btrfs_fs_info *fs_info,
628 u64 objectid, struct btrfs_root *root)
634 btrfs_setup_root(tree_root->nodesize, tree_root->leafsize,
635 tree_root->sectorsize, tree_root->stripesize,
636 root, fs_info, objectid);
637 ret = btrfs_find_last_root(tree_root, objectid,
638 &root->root_item, &root->root_key);
642 blocksize = root->nodesize;
643 generation = btrfs_root_generation(&root->root_item);
644 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
645 blocksize, generation);
646 if (!extent_buffer_uptodate(root->node))
652 static int find_and_setup_log_root(struct btrfs_root *tree_root,
653 struct btrfs_fs_info *fs_info,
654 struct btrfs_super_block *disk_super)
657 u64 blocknr = btrfs_super_log_root(disk_super);
658 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
668 blocksize = tree_root->nodesize;
670 btrfs_setup_root(tree_root->nodesize, tree_root->leafsize,
671 tree_root->sectorsize, tree_root->stripesize,
672 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
674 log_root->node = read_tree_block(tree_root, blocknr,
676 btrfs_super_generation(disk_super) + 1);
678 fs_info->log_root_tree = log_root;
680 if (!extent_buffer_uptodate(log_root->node)) {
681 free_extent_buffer(log_root->node);
683 fs_info->log_root_tree = NULL;
690 int btrfs_free_fs_root(struct btrfs_root *root)
693 free_extent_buffer(root->node);
694 if (root->commit_root)
695 free_extent_buffer(root->commit_root);
700 static void __free_fs_root(struct rb_node *node)
702 struct btrfs_root *root;
704 root = container_of(node, struct btrfs_root, rb_node);
705 btrfs_free_fs_root(root);
708 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
710 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
711 struct btrfs_key *location)
713 struct btrfs_root *root;
714 struct btrfs_root *tree_root = fs_info->tree_root;
715 struct btrfs_path *path;
716 struct extent_buffer *l;
721 root = calloc(1, sizeof(*root));
723 return ERR_PTR(-ENOMEM);
724 if (location->offset == (u64)-1) {
725 ret = find_and_setup_root(tree_root, fs_info,
726 location->objectid, root);
734 btrfs_setup_root(tree_root->nodesize, tree_root->leafsize,
735 tree_root->sectorsize, tree_root->stripesize,
736 root, fs_info, location->objectid);
738 path = btrfs_alloc_path();
741 return ERR_PTR(-ENOMEM);
744 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
751 read_extent_buffer(l, &root->root_item,
752 btrfs_item_ptr_offset(l, path->slots[0]),
753 sizeof(root->root_item));
754 memcpy(&root->root_key, location, sizeof(*location));
757 btrfs_free_path(path);
762 generation = btrfs_root_generation(&root->root_item);
763 blocksize = root->nodesize;
764 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
765 blocksize, generation);
766 if (!extent_buffer_uptodate(root->node)) {
768 return ERR_PTR(-EIO);
775 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
778 u64 objectid = *((u64 *)data);
779 struct btrfs_root *root;
781 root = rb_entry(node, struct btrfs_root, rb_node);
782 if (objectid > root->objectid)
784 else if (objectid < root->objectid)
790 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
791 struct rb_node *node2)
793 struct btrfs_root *root;
795 root = rb_entry(node2, struct btrfs_root, rb_node);
796 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
799 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
800 struct btrfs_key *location)
802 struct btrfs_root *root;
803 struct rb_node *node;
805 u64 objectid = location->objectid;
807 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
808 return fs_info->tree_root;
809 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
810 return fs_info->extent_root;
811 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
812 return fs_info->chunk_root;
813 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
814 return fs_info->dev_root;
815 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
816 return fs_info->csum_root;
817 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
818 return fs_info->quota_root;
820 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
821 location->offset != (u64)-1);
823 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
824 btrfs_fs_roots_compare_objectids, NULL);
826 return container_of(node, struct btrfs_root, rb_node);
828 root = btrfs_read_fs_root_no_cache(fs_info, location);
832 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
833 btrfs_fs_roots_compare_roots);
838 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
840 free(fs_info->tree_root);
841 free(fs_info->extent_root);
842 free(fs_info->chunk_root);
843 free(fs_info->dev_root);
844 free(fs_info->csum_root);
845 free(fs_info->quota_root);
846 free(fs_info->free_space_root);
847 free(fs_info->super_copy);
848 free(fs_info->log_root_tree);
852 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
854 struct btrfs_fs_info *fs_info;
856 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
860 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
861 fs_info->extent_root = calloc(1, sizeof(struct btrfs_root));
862 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
863 fs_info->dev_root = calloc(1, sizeof(struct btrfs_root));
864 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
865 fs_info->quota_root = calloc(1, sizeof(struct btrfs_root));
866 fs_info->free_space_root = calloc(1, sizeof(struct btrfs_root));
867 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
869 if (!fs_info->tree_root || !fs_info->extent_root ||
870 !fs_info->chunk_root || !fs_info->dev_root ||
871 !fs_info->csum_root || !fs_info->quota_root ||
872 !fs_info->free_space_root || !fs_info->super_copy)
875 extent_io_tree_init(&fs_info->extent_cache);
876 extent_io_tree_init(&fs_info->free_space_cache);
877 extent_io_tree_init(&fs_info->block_group_cache);
878 extent_io_tree_init(&fs_info->pinned_extents);
879 extent_io_tree_init(&fs_info->pending_del);
880 extent_io_tree_init(&fs_info->extent_ins);
881 fs_info->excluded_extents = NULL;
883 fs_info->fs_root_tree = RB_ROOT;
884 cache_tree_init(&fs_info->mapping_tree.cache_tree);
886 mutex_init(&fs_info->fs_mutex);
887 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
888 INIT_LIST_HEAD(&fs_info->space_info);
889 INIT_LIST_HEAD(&fs_info->recow_ebs);
892 fs_info->readonly = 1;
894 fs_info->super_bytenr = sb_bytenr;
895 fs_info->data_alloc_profile = (u64)-1;
896 fs_info->metadata_alloc_profile = (u64)-1;
897 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
900 btrfs_free_fs_info(fs_info);
904 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb,
909 features = btrfs_super_incompat_flags(sb) &
910 ~BTRFS_FEATURE_INCOMPAT_SUPP;
912 printk("couldn't open because of unsupported "
913 "option features (%Lx).\n",
914 (unsigned long long)features);
918 features = btrfs_super_incompat_flags(sb);
919 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
920 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
921 btrfs_set_super_incompat_flags(sb, features);
924 features = btrfs_super_compat_ro_flags(sb);
925 if (flags & OPEN_CTREE_WRITES) {
926 if (flags & OPEN_CTREE_INVALIDATE_FST) {
927 /* Clear the FREE_SPACE_TREE_VALID bit on disk... */
928 features &= ~BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID;
929 btrfs_set_super_compat_ro_flags(sb, features);
930 /* ... and ignore the free space tree bit. */
931 features &= ~BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE;
933 if (features & ~BTRFS_FEATURE_COMPAT_RO_SUPP) {
934 printk("couldn't open RDWR because of unsupported "
935 "option features (%Lx).\n",
936 (unsigned long long)features);
944 static int find_best_backup_root(struct btrfs_super_block *super)
946 struct btrfs_root_backup *backup;
947 u64 orig_gen = btrfs_super_generation(super);
952 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
953 backup = super->super_roots + i;
954 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
955 btrfs_backup_tree_root_gen(backup) > gen) {
957 gen = btrfs_backup_tree_root_gen(backup);
963 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
965 struct btrfs_root *info_root,
966 u64 objectid, char *str)
968 struct btrfs_super_block *sb = fs_info->super_copy;
969 struct btrfs_root *root = fs_info->tree_root;
970 u32 nodesize = btrfs_super_nodesize(sb);
973 ret = find_and_setup_root(root, fs_info, objectid, info_root);
975 printk("Couldn't setup %s tree\n", str);
976 if (!(flags & OPEN_CTREE_PARTIAL))
979 * Need a blank node here just so we don't screw up in the
980 * million of places that assume a root has a valid ->node
983 btrfs_find_create_tree_block(fs_info, 0, nodesize);
984 if (!info_root->node)
986 clear_extent_buffer_uptodate(info_root->node);
992 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
995 struct btrfs_super_block *sb = fs_info->super_copy;
996 struct btrfs_root *root;
997 struct btrfs_key key;
1006 nodesize = btrfs_super_nodesize(sb);
1007 leafsize = btrfs_super_leafsize(sb);
1008 sectorsize = btrfs_super_sectorsize(sb);
1009 stripesize = btrfs_super_stripesize(sb);
1011 root = fs_info->tree_root;
1012 btrfs_setup_root(nodesize, leafsize, sectorsize, stripesize,
1013 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
1014 blocksize = root->nodesize;
1015 generation = btrfs_super_generation(sb);
1017 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
1018 root_tree_bytenr = btrfs_super_root(sb);
1019 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
1020 struct btrfs_root_backup *backup;
1021 int index = find_best_backup_root(sb);
1022 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
1023 fprintf(stderr, "Invalid backup root number\n");
1026 backup = fs_info->super_copy->super_roots + index;
1027 root_tree_bytenr = btrfs_backup_tree_root(backup);
1028 generation = btrfs_backup_tree_root_gen(backup);
1031 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
1033 if (!extent_buffer_uptodate(root->node)) {
1034 fprintf(stderr, "Couldn't read tree root\n");
1038 ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
1039 BTRFS_EXTENT_TREE_OBJECTID, "extent");
1042 fs_info->extent_root->track_dirty = 1;
1044 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
1047 printk("Couldn't setup device tree\n");
1050 fs_info->dev_root->track_dirty = 1;
1052 ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
1053 BTRFS_CSUM_TREE_OBJECTID, "csum");
1056 fs_info->csum_root->track_dirty = 1;
1058 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
1059 fs_info->quota_root);
1061 fs_info->quota_enabled = 1;
1063 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
1064 ret = find_and_setup_root(root, fs_info, BTRFS_FREE_SPACE_TREE_OBJECTID,
1065 fs_info->free_space_root);
1067 printk("Couldn't read free space tree\n");
1070 fs_info->free_space_root->track_dirty = 1;
1073 ret = find_and_setup_log_root(root, fs_info, sb);
1075 printk("Couldn't setup log root tree\n");
1076 if (!(flags & OPEN_CTREE_PARTIAL))
1080 fs_info->generation = generation;
1081 fs_info->last_trans_committed = generation;
1082 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
1083 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
1084 btrfs_read_block_groups(fs_info->tree_root);
1086 key.objectid = BTRFS_FS_TREE_OBJECTID;
1087 key.type = BTRFS_ROOT_ITEM_KEY;
1088 key.offset = (u64)-1;
1089 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
1091 if (IS_ERR(fs_info->fs_root))
1096 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
1098 if (fs_info->free_space_root)
1099 free_extent_buffer(fs_info->free_space_root->node);
1100 if (fs_info->quota_root)
1101 free_extent_buffer(fs_info->quota_root->node);
1102 if (fs_info->csum_root)
1103 free_extent_buffer(fs_info->csum_root->node);
1104 if (fs_info->dev_root)
1105 free_extent_buffer(fs_info->dev_root->node);
1106 if (fs_info->extent_root)
1107 free_extent_buffer(fs_info->extent_root->node);
1108 if (fs_info->tree_root)
1109 free_extent_buffer(fs_info->tree_root->node);
1110 if (fs_info->log_root_tree)
1111 free_extent_buffer(fs_info->log_root_tree->node);
1112 if (fs_info->chunk_root)
1113 free_extent_buffer(fs_info->chunk_root->node);
1116 static void free_map_lookup(struct cache_extent *ce)
1118 struct map_lookup *map;
1120 map = container_of(ce, struct map_lookup, ce);
1124 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
1126 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
1128 while (!list_empty(&fs_info->recow_ebs)) {
1129 struct extent_buffer *eb;
1130 eb = list_first_entry(&fs_info->recow_ebs,
1131 struct extent_buffer, recow);
1132 list_del_init(&eb->recow);
1133 free_extent_buffer(eb);
1135 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
1136 extent_io_tree_cleanup(&fs_info->extent_cache);
1137 extent_io_tree_cleanup(&fs_info->free_space_cache);
1138 extent_io_tree_cleanup(&fs_info->block_group_cache);
1139 extent_io_tree_cleanup(&fs_info->pinned_extents);
1140 extent_io_tree_cleanup(&fs_info->pending_del);
1141 extent_io_tree_cleanup(&fs_info->extent_ins);
1144 int btrfs_scan_fs_devices(int fd, const char *path,
1145 struct btrfs_fs_devices **fs_devices,
1146 u64 sb_bytenr, unsigned sbflags,
1154 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1156 seek_ret = lseek(fd, 0, SEEK_END);
1160 dev_size = seek_ret;
1161 lseek(fd, 0, SEEK_SET);
1162 if (sb_bytenr > dev_size) {
1163 error("superblock bytenr %llu is larger than device size %llu",
1164 (unsigned long long)sb_bytenr,
1165 (unsigned long long)dev_size);
1169 ret = btrfs_scan_one_device(fd, path, fs_devices,
1170 &total_devs, sb_bytenr, sbflags);
1172 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1176 if (!skip_devices && total_devs != 1) {
1177 ret = btrfs_scan_devices();
1184 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
1185 u64 chunk_root_bytenr)
1187 struct btrfs_super_block *sb = fs_info->super_copy;
1196 nodesize = btrfs_super_nodesize(sb);
1197 leafsize = btrfs_super_leafsize(sb);
1198 sectorsize = btrfs_super_sectorsize(sb);
1199 stripesize = btrfs_super_stripesize(sb);
1201 btrfs_setup_root(nodesize, leafsize, sectorsize, stripesize,
1202 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1204 ret = btrfs_read_sys_array(fs_info->chunk_root);
1208 blocksize = fs_info->chunk_root->nodesize;
1209 generation = btrfs_super_chunk_root_generation(sb);
1211 if (chunk_root_bytenr && !IS_ALIGNED(chunk_root_bytenr,
1212 btrfs_super_sectorsize(sb))) {
1213 warning("chunk_root_bytenr %llu is unaligned to %u, ignore it",
1214 chunk_root_bytenr, btrfs_super_sectorsize(sb));
1215 chunk_root_bytenr = 0;
1218 if (!chunk_root_bytenr)
1219 chunk_root_bytenr = btrfs_super_chunk_root(sb);
1223 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1225 blocksize, generation);
1226 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1227 if (fs_info->ignore_chunk_tree_error) {
1228 warning("cannot read chunk root, continue anyway");
1229 fs_info->chunk_root = NULL;
1232 error("cannot read chunk root");
1237 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1238 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1240 fprintf(stderr, "Couldn't read chunk tree\n");
1247 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1249 u64 root_tree_bytenr,
1250 u64 chunk_root_bytenr,
1253 struct btrfs_fs_info *fs_info;
1254 struct btrfs_super_block *disk_super;
1255 struct btrfs_fs_devices *fs_devices = NULL;
1256 struct extent_buffer *eb;
1259 unsigned sbflags = SBREAD_DEFAULT;
1262 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1264 /* try to drop all the caches */
1265 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1266 fprintf(stderr, "Warning, could not drop caches\n");
1268 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1270 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1273 if (flags & OPEN_CTREE_RESTORE)
1274 fs_info->on_restoring = 1;
1275 if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1276 fs_info->suppress_check_block_errors = 1;
1277 if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
1278 fs_info->ignore_fsid_mismatch = 1;
1279 if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR)
1280 fs_info->ignore_chunk_tree_error = 1;
1282 if ((flags & OPEN_CTREE_RECOVER_SUPER)
1283 && (flags & OPEN_CTREE_FS_PARTIAL)) {
1285 "cannot open a partially created filesystem for recovery");
1289 if (flags & OPEN_CTREE_FS_PARTIAL)
1290 sbflags = SBREAD_PARTIAL;
1292 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr, sbflags,
1293 (flags & OPEN_CTREE_NO_DEVICES));
1297 fs_info->fs_devices = fs_devices;
1298 if (flags & OPEN_CTREE_WRITES)
1303 if (flags & OPEN_CTREE_EXCLUSIVE)
1306 ret = btrfs_open_devices(fs_devices, oflags);
1310 disk_super = fs_info->super_copy;
1311 if (flags & OPEN_CTREE_RECOVER_SUPER)
1312 ret = btrfs_read_dev_super(fs_devices->latest_bdev, disk_super,
1313 sb_bytenr, SBREAD_RECOVER);
1315 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr,
1318 printk("No valid btrfs found\n");
1322 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID &&
1323 !fs_info->ignore_fsid_mismatch) {
1324 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1328 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1330 ret = btrfs_check_fs_compatibility(fs_info->super_copy, flags);
1334 ret = btrfs_setup_chunk_tree_and_device_map(fs_info, chunk_root_bytenr);
1338 /* Chunk tree root is unable to read, return directly */
1339 if (!fs_info->chunk_root)
1342 eb = fs_info->chunk_root->node;
1343 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1344 btrfs_header_chunk_tree_uuid(eb),
1347 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1348 if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT) &&
1349 !fs_info->ignore_chunk_tree_error)
1355 btrfs_release_all_roots(fs_info);
1356 btrfs_cleanup_all_caches(fs_info);
1358 btrfs_close_devices(fs_devices);
1360 btrfs_free_fs_info(fs_info);
1364 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1365 u64 sb_bytenr, u64 root_tree_bytenr,
1366 u64 chunk_root_bytenr,
1371 struct btrfs_fs_info *info;
1372 int oflags = O_RDWR;
1375 ret = stat(filename, &st);
1377 error("cannot stat '%s': %s", filename, strerror(errno));
1380 if (!(((st.st_mode & S_IFMT) == S_IFREG) || ((st.st_mode & S_IFMT) == S_IFBLK))) {
1381 error("not a regular file or block device: %s", filename);
1385 if (!(flags & OPEN_CTREE_WRITES))
1388 fp = open(filename, oflags);
1390 error("cannot open '%s': %s", filename, strerror(errno));
1393 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1394 chunk_root_bytenr, flags);
1399 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1402 struct btrfs_fs_info *info;
1404 /* This flags may not return fs_info with any valid root */
1405 BUG_ON(flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
1406 info = open_ctree_fs_info(filename, sb_bytenr, 0, 0, flags);
1409 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1410 return info->chunk_root;
1411 return info->fs_root;
1414 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1417 struct btrfs_fs_info *info;
1419 /* This flags may not return fs_info with any valid root */
1420 if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR) {
1421 error("invalid open_ctree flags: 0x%llx",
1422 (unsigned long long)flags);
1425 info = __open_ctree_fd(fp, path, sb_bytenr, 0, 0, flags);
1428 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1429 return info->chunk_root;
1430 return info->fs_root;
1434 * Check if the super is valid:
1435 * - nodesize/sectorsize - minimum, maximum, alignment
1436 * - tree block starts - alignment
1437 * - number of devices - something sane
1438 * - sys array size - maximum
1440 static int check_super(struct btrfs_super_block *sb, unsigned sbflags)
1442 u8 result[BTRFS_CSUM_SIZE];
1447 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
1448 if (btrfs_super_magic(sb) == BTRFS_MAGIC_PARTIAL) {
1449 if (!(sbflags & SBREAD_PARTIAL)) {
1450 error("superblock magic doesn't match");
1456 csum_type = btrfs_super_csum_type(sb);
1457 if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
1458 error("unsupported checksum algorithm %u", csum_type);
1461 csum_size = btrfs_csum_sizes[csum_type];
1464 crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
1465 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1466 btrfs_csum_final(crc, result);
1468 if (memcmp(result, sb->csum, csum_size)) {
1469 error("superblock checksum mismatch");
1472 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
1473 error("tree_root level too big: %d >= %d",
1474 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
1477 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
1478 error("chunk_root level too big: %d >= %d",
1479 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
1482 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
1483 error("log_root level too big: %d >= %d",
1484 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
1488 if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
1489 error("tree_root block unaligned: %llu", btrfs_super_root(sb));
1492 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
1493 error("chunk_root block unaligned: %llu",
1494 btrfs_super_chunk_root(sb));
1497 if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
1498 error("log_root block unaligned: %llu",
1499 btrfs_super_log_root(sb));
1502 if (btrfs_super_nodesize(sb) < 4096) {
1503 error("nodesize too small: %u < 4096",
1504 btrfs_super_nodesize(sb));
1507 if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
1508 error("nodesize unaligned: %u", btrfs_super_nodesize(sb));
1511 if (btrfs_super_sectorsize(sb) < 4096) {
1512 error("sectorsize too small: %u < 4096",
1513 btrfs_super_sectorsize(sb));
1516 if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
1517 error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb));
1520 if (btrfs_super_total_bytes(sb) == 0) {
1521 error("invalid total_bytes 0");
1524 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
1525 error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
1528 if ((btrfs_super_stripesize(sb) != 4096)
1529 && (btrfs_super_stripesize(sb) != btrfs_super_sectorsize(sb))) {
1530 error("invalid stripesize %u", btrfs_super_stripesize(sb));
1534 if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
1535 char fsid[BTRFS_UUID_UNPARSED_SIZE];
1536 char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
1538 uuid_unparse(sb->fsid, fsid);
1539 uuid_unparse(sb->dev_item.fsid, dev_fsid);
1540 error("dev_item UUID does not match fsid: %s != %s",
1546 * Hint to catch really bogus numbers, bitflips or so
1548 if (btrfs_super_num_devices(sb) > (1UL << 31)) {
1549 warning("suspicious number of devices: %llu",
1550 btrfs_super_num_devices(sb));
1553 if (btrfs_super_num_devices(sb) == 0) {
1554 error("number of devices is 0");
1559 * Obvious sys_chunk_array corruptions, it must hold at least one key
1562 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
1563 error("system chunk array too big %u > %u",
1564 btrfs_super_sys_array_size(sb),
1565 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
1568 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
1569 + sizeof(struct btrfs_chunk)) {
1570 error("system chunk array too small %u < %zu",
1571 btrfs_super_sys_array_size(sb),
1572 sizeof(struct btrfs_disk_key) +
1573 sizeof(struct btrfs_chunk));
1580 error("superblock checksum matches but it has invalid members");
1584 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1587 u8 fsid[BTRFS_FSID_SIZE];
1588 int fsid_is_initialized = 0;
1589 char tmp[BTRFS_SUPER_INFO_SIZE];
1590 struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
1593 int max_super = sbflags & SBREAD_RECOVER ? BTRFS_SUPER_MIRROR_MAX : 1;
1597 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1598 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
1603 /* Not large enough sb, return -ENOENT instead of normal -EIO */
1604 if (ret < BTRFS_SUPER_INFO_SIZE)
1607 if (btrfs_super_bytenr(buf) != sb_bytenr)
1610 ret = check_super(buf, sbflags);
1613 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1618 * we would like to check all the supers, but that would make
1619 * a btrfs mount succeed after a mkfs from a different FS.
1620 * So, we need to add a special mount option to scan for
1621 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1624 for (i = 0; i < max_super; i++) {
1625 bytenr = btrfs_sb_offset(i);
1626 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1627 if (ret < BTRFS_SUPER_INFO_SIZE)
1630 if (btrfs_super_bytenr(buf) != bytenr )
1632 /* if magic is NULL, the device was removed */
1633 if (btrfs_super_magic(buf) == 0 && i == 0)
1635 if (check_super(buf, sbflags))
1638 if (!fsid_is_initialized) {
1639 memcpy(fsid, buf->fsid, sizeof(fsid));
1640 fsid_is_initialized = 1;
1641 } else if (memcmp(fsid, buf->fsid, sizeof(fsid))) {
1643 * the superblocks (the original one and
1644 * its backups) contain data of different
1645 * filesystems -> the super cannot be trusted
1650 if (btrfs_super_generation(buf) > transid) {
1651 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1652 transid = btrfs_super_generation(buf);
1656 return transid > 0 ? 0 : -1;
1659 static int write_dev_supers(struct btrfs_root *root,
1660 struct btrfs_super_block *sb,
1661 struct btrfs_device *device)
1667 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1668 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1670 crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
1671 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1672 btrfs_csum_final(crc, &sb->csum[0]);
1675 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1676 * zero filled, we can use it directly
1678 ret = pwrite64(device->fd, root->fs_info->super_copy,
1679 BTRFS_SUPER_INFO_SIZE,
1680 root->fs_info->super_bytenr);
1681 if (ret != BTRFS_SUPER_INFO_SIZE)
1686 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1687 bytenr = btrfs_sb_offset(i);
1688 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1691 btrfs_set_super_bytenr(sb, bytenr);
1694 crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, crc,
1695 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1696 btrfs_csum_final(crc, &sb->csum[0]);
1699 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1700 * zero filled, we can use it directly
1702 ret = pwrite64(device->fd, root->fs_info->super_copy,
1703 BTRFS_SUPER_INFO_SIZE, bytenr);
1704 if (ret != BTRFS_SUPER_INFO_SIZE)
1712 fprintf(stderr, "WARNING: failed to write all sb data\n");
1714 fprintf(stderr, "WARNING: failed to write sb: %s\n",
1719 int write_all_supers(struct btrfs_root *root)
1721 struct list_head *cur;
1722 struct list_head *head = &root->fs_info->fs_devices->devices;
1723 struct btrfs_device *dev;
1724 struct btrfs_super_block *sb;
1725 struct btrfs_dev_item *dev_item;
1729 sb = root->fs_info->super_copy;
1730 dev_item = &sb->dev_item;
1731 list_for_each(cur, head) {
1732 dev = list_entry(cur, struct btrfs_device, dev_list);
1733 if (!dev->writeable)
1736 btrfs_set_stack_device_generation(dev_item, 0);
1737 btrfs_set_stack_device_type(dev_item, dev->type);
1738 btrfs_set_stack_device_id(dev_item, dev->devid);
1739 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1740 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1741 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1742 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1743 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1744 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1745 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1747 flags = btrfs_super_flags(sb);
1748 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1750 ret = write_dev_supers(root, sb, dev);
1756 int write_ctree_super(struct btrfs_trans_handle *trans,
1757 struct btrfs_root *root)
1760 struct btrfs_root *tree_root = root->fs_info->tree_root;
1761 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1763 if (root->fs_info->readonly)
1766 btrfs_set_super_generation(root->fs_info->super_copy,
1768 btrfs_set_super_root(root->fs_info->super_copy,
1769 tree_root->node->start);
1770 btrfs_set_super_root_level(root->fs_info->super_copy,
1771 btrfs_header_level(tree_root->node));
1772 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1773 chunk_root->node->start);
1774 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1775 btrfs_header_level(chunk_root->node));
1776 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1777 btrfs_header_generation(chunk_root->node));
1779 ret = write_all_supers(root);
1781 fprintf(stderr, "failed to write new super block err %d\n", ret);
1785 int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
1788 struct btrfs_trans_handle *trans;
1789 struct btrfs_root *root = fs_info->tree_root;
1791 if (fs_info->last_trans_committed !=
1792 fs_info->generation) {
1794 trans = btrfs_start_transaction(root, 1);
1795 btrfs_commit_transaction(trans, root);
1796 trans = btrfs_start_transaction(root, 1);
1797 ret = commit_tree_roots(trans, fs_info);
1799 ret = __commit_transaction(trans, root);
1801 write_ctree_super(trans, root);
1805 if (fs_info->finalize_on_close) {
1806 btrfs_set_super_magic(fs_info->super_copy, BTRFS_MAGIC);
1807 root->fs_info->finalize_on_close = 0;
1808 ret = write_all_supers(root);
1811 "failed to write new super block err %d\n", ret);
1813 btrfs_free_block_groups(fs_info);
1815 free_fs_roots_tree(&fs_info->fs_root_tree);
1817 btrfs_release_all_roots(fs_info);
1818 ret = btrfs_close_devices(fs_info->fs_devices);
1819 btrfs_cleanup_all_caches(fs_info);
1820 btrfs_free_fs_info(fs_info);
1824 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1825 struct extent_buffer *eb)
1827 return clear_extent_buffer_dirty(eb);
1830 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1832 set_extent_buffer_dirty(eb);
1835 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1839 ret = extent_buffer_uptodate(buf);
1843 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1847 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1849 return set_extent_buffer_uptodate(eb);