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_root *root, struct extent_buffer *buf)
57 struct btrfs_fs_devices *fs_devices;
58 int ret = BTRFS_BAD_FSID;
60 if (buf->start != btrfs_header_bytenr(buf))
61 return BTRFS_BAD_BYTENR;
62 if (btrfs_header_level(buf) >= BTRFS_MAX_LEVEL)
63 return BTRFS_BAD_LEVEL;
64 if (btrfs_header_nritems(buf) > max_nritems(btrfs_header_level(buf),
66 return BTRFS_BAD_NRITEMS;
68 fs_devices = root->fs_info->fs_devices;
70 if (root->fs_info->ignore_fsid_mismatch ||
71 !memcmp_extent_buffer(buf, fs_devices->fsid,
77 fs_devices = fs_devices->seed;
82 static void print_tree_block_error(struct btrfs_root *root,
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];
92 read_extent_buffer(eb, buf, btrfs_header_fsid(),
94 uuid_unparse(buf, found_uuid);
95 uuid_unparse(root->fs_info->fsid, fs_uuid);
96 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
99 case BTRFS_BAD_BYTENR:
100 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
101 eb->start, btrfs_header_bytenr(eb));
103 case BTRFS_BAD_LEVEL:
104 fprintf(stderr, "bad level, %u > %u\n",
105 btrfs_header_level(eb), BTRFS_MAX_LEVEL);
107 case BTRFS_BAD_NRITEMS:
108 fprintf(stderr, "invalid nr_items: %u\n",
109 btrfs_header_nritems(eb));
114 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
116 return crc32c(seed, data, len);
119 void btrfs_csum_final(u32 crc, char *result)
121 *(__le32 *)result = ~cpu_to_le32(crc);
124 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
125 int verify, int silent)
127 char result[BTRFS_CSUM_SIZE];
131 len = buf->len - BTRFS_CSUM_SIZE;
132 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
133 btrfs_csum_final(crc, result);
136 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
138 printk("checksum verify failed on %llu found %08X wanted %08X\n",
139 (unsigned long long)buf->start,
141 *((u32*)(char *)buf->data));
145 write_extent_buffer(buf, result, 0, csum_size);
150 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
152 return __csum_tree_block_size(buf, csum_size, verify, 0);
155 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
157 return __csum_tree_block_size(buf, csum_size, 1, 1);
160 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
164 btrfs_super_csum_size(root->fs_info->super_copy);
165 if (verify && root->fs_info->suppress_check_block_errors)
166 return verify_tree_block_csum_silent(buf, csum_size);
167 return csum_tree_block_size(buf, csum_size, verify);
170 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
171 u64 bytenr, u32 blocksize)
173 return find_extent_buffer(&root->fs_info->extent_cache,
177 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
178 u64 bytenr, u32 blocksize)
180 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
184 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
187 struct extent_buffer *eb;
189 struct btrfs_multi_bio *multi = NULL;
190 struct btrfs_device *device;
192 eb = btrfs_find_tree_block(root, bytenr, blocksize);
193 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
194 !btrfs_map_block(&root->fs_info->mapping_tree, READ,
195 bytenr, &length, &multi, 0, NULL)) {
196 device = multi->stripes[0].dev;
198 blocksize = min(blocksize, (u32)(64 * 1024));
199 readahead(device->fd, multi->stripes[0].physical, blocksize);
202 free_extent_buffer(eb);
206 static int verify_parent_transid(struct extent_io_tree *io_tree,
207 struct extent_buffer *eb, u64 parent_transid,
212 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
215 if (extent_buffer_uptodate(eb) &&
216 btrfs_header_generation(eb) == parent_transid) {
220 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
221 (unsigned long long)eb->start,
222 (unsigned long long)parent_transid,
223 (unsigned long long)btrfs_header_generation(eb));
225 eb->flags |= EXTENT_BAD_TRANSID;
226 printk("Ignoring transid failure\n");
232 clear_extent_buffer_uptodate(io_tree, eb);
238 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
240 unsigned long offset = 0;
241 struct btrfs_multi_bio *multi = NULL;
242 struct btrfs_device *device;
245 unsigned long bytes_left = eb->len;
248 read_len = bytes_left;
251 if (!info->on_restoring &&
252 eb->start != BTRFS_SUPER_INFO_OFFSET) {
253 ret = btrfs_map_block(&info->mapping_tree, READ,
254 eb->start + offset, &read_len, &multi,
257 printk("Couldn't map the block %Lu\n", eb->start + offset);
261 device = multi->stripes[0].dev;
263 if (device->fd <= 0) {
270 eb->dev_bytenr = multi->stripes[0].physical;
274 /* special case for restore metadump */
275 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
276 if (device->devid == 1)
281 eb->dev_bytenr = eb->start;
285 if (read_len > bytes_left)
286 read_len = bytes_left;
288 ret = read_extent_from_disk(eb, offset, read_len);
292 bytes_left -= read_len;
297 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
298 u32 blocksize, u64 parent_transid)
301 struct extent_buffer *eb;
302 u64 best_transid = 0;
308 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
310 return ERR_PTR(-ENOMEM);
312 if (btrfs_buffer_uptodate(eb, parent_transid))
316 ret = read_whole_eb(root->fs_info, eb, mirror_num);
317 if (ret == 0 && csum_tree_block(root, eb, 1) == 0 &&
318 check_tree_block(root, eb) == 0 &&
319 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
321 if (eb->flags & EXTENT_BAD_TRANSID &&
322 list_empty(&eb->recow)) {
323 list_add_tail(&eb->recow,
324 &root->fs_info->recow_ebs);
327 btrfs_set_buffer_uptodate(eb);
331 if (check_tree_block(root, eb)) {
332 if (!root->fs_info->suppress_check_block_errors)
333 print_tree_block_error(root, eb,
334 check_tree_block(root, eb));
336 if (!root->fs_info->suppress_check_block_errors)
337 fprintf(stderr, "Csum didn't match\n");
342 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
344 if (num_copies == 1) {
348 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
349 best_transid = btrfs_header_generation(eb);
350 good_mirror = mirror_num;
353 if (mirror_num > num_copies) {
354 mirror_num = good_mirror;
359 free_extent_buffer(eb);
363 int read_extent_data(struct btrfs_root *root, char *data,
364 u64 logical, u64 *len, int mirror)
367 struct btrfs_multi_bio *multi = NULL;
368 struct btrfs_fs_info *info = root->fs_info;
369 struct btrfs_device *device;
373 ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
374 &multi, mirror, NULL);
376 fprintf(stderr, "Couldn't map the block %llu\n",
380 device = multi->stripes[0].dev;
387 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
397 int write_and_map_eb(struct btrfs_trans_handle *trans,
398 struct btrfs_root *root,
399 struct extent_buffer *eb)
404 u64 *raid_map = NULL;
405 struct btrfs_multi_bio *multi = NULL;
409 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
410 eb->start, &length, &multi, 0, &raid_map);
413 ret = write_raid56_with_parity(root->fs_info, eb, multi,
416 } else while (dev_nr < multi->num_stripes) {
418 eb->fd = multi->stripes[dev_nr].dev->fd;
419 eb->dev_bytenr = multi->stripes[dev_nr].physical;
420 multi->stripes[dev_nr].dev->total_ios++;
422 ret = write_extent_to_disk(eb);
430 int write_tree_block(struct btrfs_trans_handle *trans,
431 struct btrfs_root *root,
432 struct extent_buffer *eb)
434 if (check_tree_block(root, eb)) {
435 print_tree_block_error(root, eb, check_tree_block(root, eb));
439 if (trans && !btrfs_buffer_uptodate(eb, trans->transid))
442 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
443 csum_tree_block(root, eb, 0);
445 return write_and_map_eb(trans, root, eb);
448 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
449 u32 stripesize, struct btrfs_root *root,
450 struct btrfs_fs_info *fs_info, u64 objectid)
453 root->commit_root = NULL;
454 root->sectorsize = sectorsize;
455 root->nodesize = nodesize;
456 root->leafsize = leafsize;
457 root->stripesize = stripesize;
459 root->track_dirty = 0;
461 root->fs_info = fs_info;
462 root->objectid = objectid;
463 root->last_trans = 0;
464 root->highest_inode = 0;
465 root->last_inode_alloc = 0;
467 INIT_LIST_HEAD(&root->dirty_list);
468 INIT_LIST_HEAD(&root->orphan_data_extents);
469 memset(&root->root_key, 0, sizeof(root->root_key));
470 memset(&root->root_item, 0, sizeof(root->root_item));
471 root->root_key.objectid = objectid;
475 static int update_cowonly_root(struct btrfs_trans_handle *trans,
476 struct btrfs_root *root)
480 struct btrfs_root *tree_root = root->fs_info->tree_root;
482 btrfs_write_dirty_block_groups(trans, root);
484 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
485 if (old_root_bytenr == root->node->start)
487 btrfs_set_root_bytenr(&root->root_item,
489 btrfs_set_root_generation(&root->root_item,
491 root->root_item.level = btrfs_header_level(root->node);
492 ret = btrfs_update_root(trans, tree_root,
496 btrfs_write_dirty_block_groups(trans, root);
501 static int commit_tree_roots(struct btrfs_trans_handle *trans,
502 struct btrfs_fs_info *fs_info)
504 struct btrfs_root *root;
505 struct list_head *next;
506 struct extent_buffer *eb;
509 if (fs_info->readonly)
512 eb = fs_info->tree_root->node;
513 extent_buffer_get(eb);
514 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
515 free_extent_buffer(eb);
519 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
520 next = fs_info->dirty_cowonly_roots.next;
522 root = list_entry(next, struct btrfs_root, dirty_list);
523 update_cowonly_root(trans, root);
524 free_extent_buffer(root->commit_root);
525 root->commit_root = NULL;
531 static int __commit_transaction(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root)
536 struct extent_buffer *eb;
537 struct extent_io_tree *tree = &root->fs_info->extent_cache;
541 ret = find_first_extent_bit(tree, 0, &start, &end,
545 while(start <= end) {
546 eb = find_first_extent_buffer(tree, start);
547 BUG_ON(!eb || eb->start != start);
548 ret = write_tree_block(trans, root, eb);
551 clear_extent_buffer_dirty(eb);
552 free_extent_buffer(eb);
558 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
559 struct btrfs_root *root)
561 u64 transid = trans->transid;
563 struct btrfs_fs_info *fs_info = root->fs_info;
565 if (root->commit_root == root->node)
567 if (root == root->fs_info->tree_root)
569 if (root == root->fs_info->chunk_root)
572 free_extent_buffer(root->commit_root);
573 root->commit_root = NULL;
575 btrfs_set_root_bytenr(&root->root_item, root->node->start);
576 btrfs_set_root_generation(&root->root_item, trans->transid);
577 root->root_item.level = btrfs_header_level(root->node);
578 ret = btrfs_update_root(trans, root->fs_info->tree_root,
579 &root->root_key, &root->root_item);
582 ret = commit_tree_roots(trans, fs_info);
584 ret = __commit_transaction(trans, root);
586 write_ctree_super(trans, root);
587 btrfs_finish_extent_commit(trans, fs_info->extent_root,
588 &fs_info->pinned_extents);
589 btrfs_free_transaction(root, trans);
590 free_extent_buffer(root->commit_root);
591 root->commit_root = NULL;
592 fs_info->running_transaction = NULL;
593 fs_info->last_trans_committed = transid;
597 static int find_and_setup_root(struct btrfs_root *tree_root,
598 struct btrfs_fs_info *fs_info,
599 u64 objectid, struct btrfs_root *root)
605 __setup_root(tree_root->nodesize, tree_root->leafsize,
606 tree_root->sectorsize, tree_root->stripesize,
607 root, fs_info, objectid);
608 ret = btrfs_find_last_root(tree_root, objectid,
609 &root->root_item, &root->root_key);
613 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
614 generation = btrfs_root_generation(&root->root_item);
615 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
616 blocksize, generation);
617 if (!extent_buffer_uptodate(root->node))
623 static int find_and_setup_log_root(struct btrfs_root *tree_root,
624 struct btrfs_fs_info *fs_info,
625 struct btrfs_super_block *disk_super)
628 u64 blocknr = btrfs_super_log_root(disk_super);
629 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
639 blocksize = btrfs_level_size(tree_root,
640 btrfs_super_log_root_level(disk_super));
642 __setup_root(tree_root->nodesize, tree_root->leafsize,
643 tree_root->sectorsize, tree_root->stripesize,
644 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
646 log_root->node = read_tree_block(tree_root, blocknr,
648 btrfs_super_generation(disk_super) + 1);
650 fs_info->log_root_tree = log_root;
652 if (!extent_buffer_uptodate(log_root->node)) {
653 free_extent_buffer(log_root->node);
655 fs_info->log_root_tree = NULL;
662 int btrfs_free_fs_root(struct btrfs_root *root)
665 free_extent_buffer(root->node);
666 if (root->commit_root)
667 free_extent_buffer(root->commit_root);
672 static void __free_fs_root(struct rb_node *node)
674 struct btrfs_root *root;
676 root = container_of(node, struct btrfs_root, rb_node);
677 btrfs_free_fs_root(root);
680 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
682 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
683 struct btrfs_key *location)
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;
693 root = calloc(1, sizeof(*root));
695 return ERR_PTR(-ENOMEM);
696 if (location->offset == (u64)-1) {
697 ret = find_and_setup_root(tree_root, fs_info,
698 location->objectid, root);
706 __setup_root(tree_root->nodesize, tree_root->leafsize,
707 tree_root->sectorsize, tree_root->stripesize,
708 root, fs_info, location->objectid);
710 path = btrfs_alloc_path();
712 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
719 read_extent_buffer(l, &root->root_item,
720 btrfs_item_ptr_offset(l, path->slots[0]),
721 sizeof(root->root_item));
722 memcpy(&root->root_key, location, sizeof(*location));
725 btrfs_free_path(path);
730 generation = btrfs_root_generation(&root->root_item);
731 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
732 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
733 blocksize, generation);
734 if (!extent_buffer_uptodate(root->node)) {
736 return ERR_PTR(-EIO);
743 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
746 u64 objectid = *((u64 *)data);
747 struct btrfs_root *root;
749 root = rb_entry(node, struct btrfs_root, rb_node);
750 if (objectid > root->objectid)
752 else if (objectid < root->objectid)
758 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
759 struct rb_node *node2)
761 struct btrfs_root *root;
763 root = rb_entry(node2, struct btrfs_root, rb_node);
764 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
767 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
768 struct btrfs_key *location)
770 struct btrfs_root *root;
771 struct rb_node *node;
773 u64 objectid = location->objectid;
775 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
776 return fs_info->tree_root;
777 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
778 return fs_info->extent_root;
779 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
780 return fs_info->chunk_root;
781 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
782 return fs_info->dev_root;
783 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
784 return fs_info->csum_root;
785 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
786 return fs_info->quota_root;
788 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
789 location->offset != (u64)-1);
791 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
792 btrfs_fs_roots_compare_objectids, NULL);
794 return container_of(node, struct btrfs_root, rb_node);
796 root = btrfs_read_fs_root_no_cache(fs_info, location);
800 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
801 btrfs_fs_roots_compare_roots);
806 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
808 free(fs_info->tree_root);
809 free(fs_info->extent_root);
810 free(fs_info->chunk_root);
811 free(fs_info->dev_root);
812 free(fs_info->csum_root);
813 free(fs_info->quota_root);
814 free(fs_info->super_copy);
815 free(fs_info->log_root_tree);
819 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
821 struct btrfs_fs_info *fs_info;
823 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
827 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
828 fs_info->extent_root = calloc(1, sizeof(struct btrfs_root));
829 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
830 fs_info->dev_root = calloc(1, sizeof(struct btrfs_root));
831 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
832 fs_info->quota_root = calloc(1, sizeof(struct btrfs_root));
833 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
835 if (!fs_info->tree_root || !fs_info->extent_root ||
836 !fs_info->chunk_root || !fs_info->dev_root ||
837 !fs_info->csum_root || !fs_info->quota_root ||
838 !fs_info->super_copy)
841 extent_io_tree_init(&fs_info->extent_cache);
842 extent_io_tree_init(&fs_info->free_space_cache);
843 extent_io_tree_init(&fs_info->block_group_cache);
844 extent_io_tree_init(&fs_info->pinned_extents);
845 extent_io_tree_init(&fs_info->pending_del);
846 extent_io_tree_init(&fs_info->extent_ins);
847 fs_info->excluded_extents = NULL;
849 fs_info->fs_root_tree = RB_ROOT;
850 cache_tree_init(&fs_info->mapping_tree.cache_tree);
852 mutex_init(&fs_info->fs_mutex);
853 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
854 INIT_LIST_HEAD(&fs_info->space_info);
855 INIT_LIST_HEAD(&fs_info->recow_ebs);
858 fs_info->readonly = 1;
860 fs_info->super_bytenr = sb_bytenr;
861 fs_info->data_alloc_profile = (u64)-1;
862 fs_info->metadata_alloc_profile = (u64)-1;
863 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
866 btrfs_free_fs_info(fs_info);
870 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
874 features = btrfs_super_incompat_flags(sb) &
875 ~BTRFS_FEATURE_INCOMPAT_SUPP;
877 printk("couldn't open because of unsupported "
878 "option features (%Lx).\n",
879 (unsigned long long)features);
883 features = btrfs_super_incompat_flags(sb);
884 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
885 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
886 btrfs_set_super_incompat_flags(sb, features);
889 features = btrfs_super_compat_ro_flags(sb) &
890 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
891 if (writable && features) {
892 printk("couldn't open RDWR because of unsupported "
893 "option features (%Lx).\n",
894 (unsigned long long)features);
900 static int find_best_backup_root(struct btrfs_super_block *super)
902 struct btrfs_root_backup *backup;
903 u64 orig_gen = btrfs_super_generation(super);
908 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
909 backup = super->super_roots + i;
910 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
911 btrfs_backup_tree_root_gen(backup) > gen) {
913 gen = btrfs_backup_tree_root_gen(backup);
919 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
920 enum btrfs_open_ctree_flags flags,
921 struct btrfs_root *info_root,
922 u64 objectid, char *str)
924 struct btrfs_super_block *sb = fs_info->super_copy;
925 struct btrfs_root *root = fs_info->tree_root;
926 u32 leafsize = btrfs_super_leafsize(sb);
929 ret = find_and_setup_root(root, fs_info, objectid, info_root);
931 printk("Couldn't setup %s tree\n", str);
932 if (!(flags & OPEN_CTREE_PARTIAL))
935 * Need a blank node here just so we don't screw up in the
936 * million of places that assume a root has a valid ->node
939 btrfs_find_create_tree_block(info_root, 0, leafsize);
940 if (!info_root->node)
942 clear_extent_buffer_uptodate(NULL, info_root->node);
948 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
949 enum btrfs_open_ctree_flags flags)
951 struct btrfs_super_block *sb = fs_info->super_copy;
952 struct btrfs_root *root;
953 struct btrfs_key key;
962 nodesize = btrfs_super_nodesize(sb);
963 leafsize = btrfs_super_leafsize(sb);
964 sectorsize = btrfs_super_sectorsize(sb);
965 stripesize = btrfs_super_stripesize(sb);
967 root = fs_info->tree_root;
968 __setup_root(nodesize, leafsize, sectorsize, stripesize,
969 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
970 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
971 generation = btrfs_super_generation(sb);
973 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
974 root_tree_bytenr = btrfs_super_root(sb);
975 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
976 struct btrfs_root_backup *backup;
977 int index = find_best_backup_root(sb);
978 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
979 fprintf(stderr, "Invalid backup root number\n");
982 backup = fs_info->super_copy->super_roots + index;
983 root_tree_bytenr = btrfs_backup_tree_root(backup);
984 generation = btrfs_backup_tree_root_gen(backup);
987 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
989 if (!extent_buffer_uptodate(root->node)) {
990 fprintf(stderr, "Couldn't read tree root\n");
994 ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
995 BTRFS_EXTENT_TREE_OBJECTID, "extent");
998 fs_info->extent_root->track_dirty = 1;
1000 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
1003 printk("Couldn't setup device tree\n");
1006 fs_info->dev_root->track_dirty = 1;
1008 ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
1009 BTRFS_CSUM_TREE_OBJECTID, "csum");
1012 fs_info->csum_root->track_dirty = 1;
1014 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
1015 fs_info->quota_root);
1017 fs_info->quota_enabled = 1;
1019 ret = find_and_setup_log_root(root, fs_info, sb);
1021 printk("Couldn't setup log root tree\n");
1022 if (!(flags & OPEN_CTREE_PARTIAL))
1026 fs_info->generation = generation;
1027 fs_info->last_trans_committed = generation;
1028 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
1029 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
1030 btrfs_read_block_groups(fs_info->tree_root);
1032 key.objectid = BTRFS_FS_TREE_OBJECTID;
1033 key.type = BTRFS_ROOT_ITEM_KEY;
1034 key.offset = (u64)-1;
1035 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
1037 if (IS_ERR(fs_info->fs_root))
1042 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
1044 if (fs_info->quota_root)
1045 free_extent_buffer(fs_info->quota_root->node);
1046 if (fs_info->csum_root)
1047 free_extent_buffer(fs_info->csum_root->node);
1048 if (fs_info->dev_root)
1049 free_extent_buffer(fs_info->dev_root->node);
1050 if (fs_info->extent_root)
1051 free_extent_buffer(fs_info->extent_root->node);
1052 if (fs_info->tree_root)
1053 free_extent_buffer(fs_info->tree_root->node);
1054 if (fs_info->log_root_tree)
1055 free_extent_buffer(fs_info->log_root_tree->node);
1056 if (fs_info->chunk_root)
1057 free_extent_buffer(fs_info->chunk_root->node);
1060 static void free_map_lookup(struct cache_extent *ce)
1062 struct map_lookup *map;
1064 map = container_of(ce, struct map_lookup, ce);
1068 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
1070 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
1072 while (!list_empty(&fs_info->recow_ebs)) {
1073 struct extent_buffer *eb;
1074 eb = list_first_entry(&fs_info->recow_ebs,
1075 struct extent_buffer, recow);
1076 list_del_init(&eb->recow);
1077 free_extent_buffer(eb);
1079 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
1080 extent_io_tree_cleanup(&fs_info->extent_cache);
1081 extent_io_tree_cleanup(&fs_info->free_space_cache);
1082 extent_io_tree_cleanup(&fs_info->block_group_cache);
1083 extent_io_tree_cleanup(&fs_info->pinned_extents);
1084 extent_io_tree_cleanup(&fs_info->pending_del);
1085 extent_io_tree_cleanup(&fs_info->extent_ins);
1088 int btrfs_scan_fs_devices(int fd, const char *path,
1089 struct btrfs_fs_devices **fs_devices,
1090 u64 sb_bytenr, int super_recover,
1098 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1100 seek_ret = lseek(fd, 0, SEEK_END);
1104 dev_size = seek_ret;
1105 lseek(fd, 0, SEEK_SET);
1106 if (sb_bytenr > dev_size) {
1107 fprintf(stderr, "Superblock bytenr is larger than device size\n");
1111 ret = btrfs_scan_one_device(fd, path, fs_devices,
1112 &total_devs, sb_bytenr, super_recover);
1114 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1118 if (!skip_devices && total_devs != 1) {
1119 ret = btrfs_scan_lblkid();
1126 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1128 struct btrfs_super_block *sb = fs_info->super_copy;
1137 nodesize = btrfs_super_nodesize(sb);
1138 leafsize = btrfs_super_leafsize(sb);
1139 sectorsize = btrfs_super_sectorsize(sb);
1140 stripesize = btrfs_super_stripesize(sb);
1142 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1143 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1145 ret = btrfs_read_sys_array(fs_info->chunk_root);
1149 blocksize = btrfs_level_size(fs_info->chunk_root,
1150 btrfs_super_chunk_root_level(sb));
1151 generation = btrfs_super_chunk_root_generation(sb);
1153 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1154 btrfs_super_chunk_root(sb),
1155 blocksize, generation);
1156 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1157 fprintf(stderr, "Couldn't read chunk root\n");
1161 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1162 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1164 fprintf(stderr, "Couldn't read chunk tree\n");
1171 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1173 u64 root_tree_bytenr,
1174 enum btrfs_open_ctree_flags flags)
1176 struct btrfs_fs_info *fs_info;
1177 struct btrfs_super_block *disk_super;
1178 struct btrfs_fs_devices *fs_devices = NULL;
1179 struct extent_buffer *eb;
1184 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1186 /* try to drop all the caches */
1187 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1188 fprintf(stderr, "Warning, could not drop caches\n");
1190 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1192 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1195 if (flags & OPEN_CTREE_RESTORE)
1196 fs_info->on_restoring = 1;
1197 if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1198 fs_info->suppress_check_block_errors = 1;
1199 if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
1200 fs_info->ignore_fsid_mismatch = 1;
1202 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1203 (flags & OPEN_CTREE_RECOVER_SUPER),
1204 (flags & OPEN_CTREE_NO_DEVICES));
1208 fs_info->fs_devices = fs_devices;
1209 if (flags & OPEN_CTREE_WRITES)
1214 if (flags & OPEN_CTREE_EXCLUSIVE)
1217 ret = btrfs_open_devices(fs_devices, oflags);
1221 disk_super = fs_info->super_copy;
1222 if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1223 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1224 disk_super, sb_bytenr, 1);
1226 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1228 printk("No valid btrfs found\n");
1232 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID &&
1233 !fs_info->ignore_fsid_mismatch) {
1234 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1238 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1240 ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1241 flags & OPEN_CTREE_WRITES);
1245 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1249 eb = fs_info->chunk_root->node;
1250 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1251 btrfs_header_chunk_tree_uuid(eb),
1254 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1255 if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT))
1261 btrfs_release_all_roots(fs_info);
1262 btrfs_cleanup_all_caches(fs_info);
1264 btrfs_close_devices(fs_devices);
1266 btrfs_free_fs_info(fs_info);
1270 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1271 u64 sb_bytenr, u64 root_tree_bytenr,
1272 enum btrfs_open_ctree_flags flags)
1275 struct btrfs_fs_info *info;
1276 int oflags = O_CREAT | O_RDWR;
1278 if (!(flags & OPEN_CTREE_WRITES))
1281 fp = open(filename, oflags, 0600);
1283 fprintf (stderr, "Could not open %s\n", filename);
1286 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1292 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1293 enum btrfs_open_ctree_flags flags)
1295 struct btrfs_fs_info *info;
1297 info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1300 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1301 return info->chunk_root;
1302 return info->fs_root;
1305 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1306 enum btrfs_open_ctree_flags flags)
1308 struct btrfs_fs_info *info;
1309 info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1312 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1313 return info->chunk_root;
1314 return info->fs_root;
1318 * Check if the super is valid:
1319 * - nodesize/sectorsize - minimum, maximum, alignment
1320 * - tree block starts - alignment
1321 * - number of devices - something sane
1322 * - sys array size - maximum
1324 static int check_super(struct btrfs_super_block *sb)
1326 char result[BTRFS_CSUM_SIZE];
1331 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
1332 fprintf(stderr, "ERROR: superblock magic doesn't match\n");
1336 csum_type = btrfs_super_csum_type(sb);
1337 if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
1338 fprintf(stderr, "ERROR: unsupported checksum algorithm %u\n",
1342 csum_size = btrfs_csum_sizes[csum_type];
1345 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1346 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1347 btrfs_csum_final(crc, result);
1349 if (memcmp(result, sb->csum, csum_size)) {
1350 fprintf(stderr, "ERROR: superblock checksum mismatch\n");
1353 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
1354 fprintf(stderr, "ERROR: tree_root level too big: %d >= %d\n",
1355 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
1358 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
1359 fprintf(stderr, "ERROR: chunk_root level too big: %d >= %d\n",
1360 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
1363 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
1364 fprintf(stderr, "ERROR: log_root level too big: %d >= %d\n",
1365 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
1369 if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
1370 fprintf(stderr, "ERROR: tree_root block unaligned: %llu\n",
1371 btrfs_super_root(sb));
1374 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
1375 fprintf(stderr, "ERROR: chunk_root block unaligned: %llu\n",
1376 btrfs_super_chunk_root(sb));
1379 if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
1380 fprintf(stderr, "ERROR: log_root block unaligned: %llu\n",
1381 btrfs_super_log_root(sb));
1384 if (btrfs_super_nodesize(sb) < 4096) {
1385 fprintf(stderr, "ERROR: nodesize too small: %u < 4096\n",
1386 btrfs_super_nodesize(sb));
1389 if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
1390 fprintf(stderr, "ERROR: nodesize unaligned: %u\n",
1391 btrfs_super_nodesize(sb));
1394 if (btrfs_super_sectorsize(sb) < 4096) {
1395 fprintf(stderr, "ERROR: sectorsize too small: %u < 4096\n",
1396 btrfs_super_sectorsize(sb));
1399 if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
1400 fprintf(stderr, "ERROR: sectorsize unaligned: %u\n",
1401 btrfs_super_sectorsize(sb));
1405 if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
1406 char fsid[BTRFS_UUID_UNPARSED_SIZE];
1407 char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
1409 uuid_unparse(sb->fsid, fsid);
1410 uuid_unparse(sb->dev_item.fsid, dev_fsid);
1412 "ERROR: dev_item UUID does not match fsid: %s != %s\n",
1418 * Hint to catch really bogus numbers, bitflips or so
1420 if (btrfs_super_num_devices(sb) > (1UL << 31)) {
1421 fprintf(stderr, "WARNING: suspicious number of devices: %llu\n",
1422 btrfs_super_num_devices(sb));
1425 if (btrfs_super_num_devices(sb) == 0) {
1426 fprintf(stderr, "ERROR: number of devices is 0\n");
1431 * Obvious sys_chunk_array corruptions, it must hold at least one key
1434 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
1435 fprintf(stderr, "BTRFS: system chunk array too big %u > %u\n",
1436 btrfs_super_sys_array_size(sb),
1437 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
1440 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
1441 + sizeof(struct btrfs_chunk)) {
1442 fprintf(stderr, "BTRFS: system chunk array too small %u < %lu\n",
1443 btrfs_super_sys_array_size(sb),
1444 sizeof(struct btrfs_disk_key) +
1445 sizeof(struct btrfs_chunk));
1452 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1455 u8 fsid[BTRFS_FSID_SIZE];
1456 int fsid_is_initialized = 0;
1457 char tmp[BTRFS_SUPER_INFO_SIZE];
1458 struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
1461 int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1465 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1466 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
1467 if (ret < BTRFS_SUPER_INFO_SIZE)
1470 if (btrfs_super_bytenr(buf) != sb_bytenr)
1473 if (check_super(buf))
1475 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1480 * we would like to check all the supers, but that would make
1481 * a btrfs mount succeed after a mkfs from a different FS.
1482 * So, we need to add a special mount option to scan for
1483 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1486 for (i = 0; i < max_super; i++) {
1487 bytenr = btrfs_sb_offset(i);
1488 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1489 if (ret < BTRFS_SUPER_INFO_SIZE)
1492 if (btrfs_super_bytenr(buf) != bytenr )
1494 /* if magic is NULL, the device was removed */
1495 if (btrfs_super_magic(buf) == 0 && i == 0)
1497 if (check_super(buf))
1500 if (!fsid_is_initialized) {
1501 memcpy(fsid, buf->fsid, sizeof(fsid));
1502 fsid_is_initialized = 1;
1503 } else if (memcmp(fsid, buf->fsid, sizeof(fsid))) {
1505 * the superblocks (the original one and
1506 * its backups) contain data of different
1507 * filesystems -> the super cannot be trusted
1512 if (btrfs_super_generation(buf) > transid) {
1513 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1514 transid = btrfs_super_generation(buf);
1518 return transid > 0 ? 0 : -1;
1521 static int write_dev_supers(struct btrfs_root *root,
1522 struct btrfs_super_block *sb,
1523 struct btrfs_device *device)
1529 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1530 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1532 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1533 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1534 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1537 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1538 * zero filled, we can use it directly
1540 ret = pwrite64(device->fd, root->fs_info->super_copy,
1541 BTRFS_SUPER_INFO_SIZE,
1542 root->fs_info->super_bytenr);
1543 if (ret != BTRFS_SUPER_INFO_SIZE)
1548 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1549 bytenr = btrfs_sb_offset(i);
1550 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1553 btrfs_set_super_bytenr(sb, bytenr);
1556 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1557 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1558 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1561 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1562 * zero filled, we can use it directly
1564 ret = pwrite64(device->fd, root->fs_info->super_copy,
1565 BTRFS_SUPER_INFO_SIZE, bytenr);
1566 if (ret != BTRFS_SUPER_INFO_SIZE)
1574 fprintf(stderr, "WARNING: failed to write all sb data\n");
1576 fprintf(stderr, "WARNING: failed to write sb: %s\n",
1581 int write_all_supers(struct btrfs_root *root)
1583 struct list_head *cur;
1584 struct list_head *head = &root->fs_info->fs_devices->devices;
1585 struct btrfs_device *dev;
1586 struct btrfs_super_block *sb;
1587 struct btrfs_dev_item *dev_item;
1591 sb = root->fs_info->super_copy;
1592 dev_item = &sb->dev_item;
1593 list_for_each(cur, head) {
1594 dev = list_entry(cur, struct btrfs_device, dev_list);
1595 if (!dev->writeable)
1598 btrfs_set_stack_device_generation(dev_item, 0);
1599 btrfs_set_stack_device_type(dev_item, dev->type);
1600 btrfs_set_stack_device_id(dev_item, dev->devid);
1601 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1602 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1603 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1604 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1605 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1606 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1607 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1609 flags = btrfs_super_flags(sb);
1610 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1612 ret = write_dev_supers(root, sb, dev);
1618 int write_ctree_super(struct btrfs_trans_handle *trans,
1619 struct btrfs_root *root)
1622 struct btrfs_root *tree_root = root->fs_info->tree_root;
1623 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1625 if (root->fs_info->readonly)
1628 btrfs_set_super_generation(root->fs_info->super_copy,
1630 btrfs_set_super_root(root->fs_info->super_copy,
1631 tree_root->node->start);
1632 btrfs_set_super_root_level(root->fs_info->super_copy,
1633 btrfs_header_level(tree_root->node));
1634 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1635 chunk_root->node->start);
1636 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1637 btrfs_header_level(chunk_root->node));
1638 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1639 btrfs_header_generation(chunk_root->node));
1641 ret = write_all_supers(root);
1643 fprintf(stderr, "failed to write new super block err %d\n", ret);
1647 int close_ctree(struct btrfs_root *root)
1650 struct btrfs_trans_handle *trans;
1651 struct btrfs_fs_info *fs_info = root->fs_info;
1653 if (fs_info->last_trans_committed !=
1654 fs_info->generation) {
1655 trans = btrfs_start_transaction(root, 1);
1656 btrfs_commit_transaction(trans, root);
1657 trans = btrfs_start_transaction(root, 1);
1658 ret = commit_tree_roots(trans, fs_info);
1660 ret = __commit_transaction(trans, root);
1662 write_ctree_super(trans, root);
1663 btrfs_free_transaction(root, trans);
1665 btrfs_free_block_groups(fs_info);
1667 free_fs_roots_tree(&fs_info->fs_root_tree);
1669 btrfs_release_all_roots(fs_info);
1670 btrfs_close_devices(fs_info->fs_devices);
1671 btrfs_cleanup_all_caches(fs_info);
1672 btrfs_free_fs_info(fs_info);
1676 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1677 struct extent_buffer *eb)
1679 return clear_extent_buffer_dirty(eb);
1682 int wait_on_tree_block_writeback(struct btrfs_root *root,
1683 struct extent_buffer *eb)
1688 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1690 set_extent_buffer_dirty(eb);
1693 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1697 ret = extent_buffer_uptodate(buf);
1701 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1705 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1707 return set_extent_buffer_uptodate(eb);