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 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
45 /* Calculate max possible nritems for a leaf/node */
46 static u32 max_nritems(u8 level, u32 nodesize)
50 return ((nodesize - sizeof(struct btrfs_header)) /
51 sizeof(struct btrfs_item));
52 return ((nodesize - sizeof(struct btrfs_header)) /
53 sizeof(struct btrfs_key_ptr));
56 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
59 struct btrfs_fs_devices *fs_devices;
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 fs_devices = root->fs_info->fs_devices;
72 if (root->fs_info->ignore_fsid_mismatch ||
73 !memcmp_extent_buffer(buf, fs_devices->fsid,
79 fs_devices = fs_devices->seed;
84 static void print_tree_block_error(struct btrfs_root *root,
85 struct extent_buffer *eb,
88 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
89 char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
90 u8 buf[BTRFS_UUID_SIZE];
94 read_extent_buffer(eb, buf, btrfs_header_fsid(),
96 uuid_unparse(buf, found_uuid);
97 uuid_unparse(root->fs_info->fsid, fs_uuid);
98 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
101 case BTRFS_BAD_BYTENR:
102 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
103 eb->start, btrfs_header_bytenr(eb));
105 case BTRFS_BAD_LEVEL:
106 fprintf(stderr, "bad level, %u > %u\n",
107 btrfs_header_level(eb), BTRFS_MAX_LEVEL);
109 case BTRFS_BAD_NRITEMS:
110 fprintf(stderr, "invalid nr_items: %u\n",
111 btrfs_header_nritems(eb));
116 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
118 return crc32c(seed, data, len);
121 void btrfs_csum_final(u32 crc, char *result)
123 *(__le32 *)result = ~cpu_to_le32(crc);
126 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
127 int verify, int silent)
133 result = malloc(csum_size * sizeof(char));
137 len = buf->len - BTRFS_CSUM_SIZE;
138 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
139 btrfs_csum_final(crc, result);
142 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
144 printk("checksum verify failed on %llu found %08X wanted %08X\n",
145 (unsigned long long)buf->start,
147 *((u32*)(char *)buf->data));
152 write_extent_buffer(buf, result, 0, csum_size);
158 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
160 return __csum_tree_block_size(buf, csum_size, verify, 0);
163 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
165 return __csum_tree_block_size(buf, csum_size, 1, 1);
168 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
172 btrfs_super_csum_size(root->fs_info->super_copy);
173 if (verify && root->fs_info->suppress_check_block_errors)
174 return verify_tree_block_csum_silent(buf, csum_size);
175 return csum_tree_block_size(buf, csum_size, verify);
178 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
179 u64 bytenr, u32 blocksize)
181 return find_extent_buffer(&root->fs_info->extent_cache,
185 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
186 u64 bytenr, u32 blocksize)
188 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
192 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
195 struct extent_buffer *eb;
197 struct btrfs_multi_bio *multi = NULL;
198 struct btrfs_device *device;
200 eb = btrfs_find_tree_block(root, bytenr, blocksize);
201 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
202 !btrfs_map_block(&root->fs_info->mapping_tree, READ,
203 bytenr, &length, &multi, 0, NULL)) {
204 device = multi->stripes[0].dev;
206 blocksize = min(blocksize, (u32)(64 * 1024));
207 readahead(device->fd, multi->stripes[0].physical, blocksize);
210 free_extent_buffer(eb);
214 static int verify_parent_transid(struct extent_io_tree *io_tree,
215 struct extent_buffer *eb, u64 parent_transid,
220 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
223 if (extent_buffer_uptodate(eb) &&
224 btrfs_header_generation(eb) == parent_transid) {
228 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
229 (unsigned long long)eb->start,
230 (unsigned long long)parent_transid,
231 (unsigned long long)btrfs_header_generation(eb));
233 eb->flags |= EXTENT_BAD_TRANSID;
234 printk("Ignoring transid failure\n");
240 clear_extent_buffer_uptodate(io_tree, eb);
246 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
248 unsigned long offset = 0;
249 struct btrfs_multi_bio *multi = NULL;
250 struct btrfs_device *device;
253 unsigned long bytes_left = eb->len;
256 read_len = bytes_left;
259 if (!info->on_restoring &&
260 eb->start != BTRFS_SUPER_INFO_OFFSET) {
261 ret = btrfs_map_block(&info->mapping_tree, READ,
262 eb->start + offset, &read_len, &multi,
265 printk("Couldn't map the block %Lu\n", eb->start + offset);
269 device = multi->stripes[0].dev;
271 if (device->fd <= 0) {
278 eb->dev_bytenr = multi->stripes[0].physical;
282 /* special case for restore metadump */
283 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
284 if (device->devid == 1)
289 eb->dev_bytenr = eb->start;
293 if (read_len > bytes_left)
294 read_len = bytes_left;
296 ret = read_extent_from_disk(eb, offset, read_len);
300 bytes_left -= read_len;
305 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
306 u32 blocksize, u64 parent_transid)
309 struct extent_buffer *eb;
310 u64 best_transid = 0;
316 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
318 return ERR_PTR(-ENOMEM);
320 if (btrfs_buffer_uptodate(eb, parent_transid))
324 ret = read_whole_eb(root->fs_info, eb, mirror_num);
325 if (ret == 0 && csum_tree_block(root, eb, 1) == 0 &&
326 check_tree_block(root, eb) == 0 &&
327 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
329 if (eb->flags & EXTENT_BAD_TRANSID &&
330 list_empty(&eb->recow)) {
331 list_add_tail(&eb->recow,
332 &root->fs_info->recow_ebs);
335 btrfs_set_buffer_uptodate(eb);
339 if (check_tree_block(root, eb)) {
340 if (!root->fs_info->suppress_check_block_errors)
341 print_tree_block_error(root, eb,
342 check_tree_block(root, eb));
344 if (!root->fs_info->suppress_check_block_errors)
345 fprintf(stderr, "Csum didn't match\n");
350 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
352 if (num_copies == 1) {
356 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
357 best_transid = btrfs_header_generation(eb);
358 good_mirror = mirror_num;
361 if (mirror_num > num_copies) {
362 mirror_num = good_mirror;
367 free_extent_buffer(eb);
371 int read_extent_data(struct btrfs_root *root, char *data,
372 u64 logical, u64 *len, int mirror)
375 struct btrfs_multi_bio *multi = NULL;
376 struct btrfs_fs_info *info = root->fs_info;
377 struct btrfs_device *device;
381 ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
382 &multi, mirror, NULL);
384 fprintf(stderr, "Couldn't map the block %llu\n",
388 device = multi->stripes[0].dev;
395 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
405 int write_and_map_eb(struct btrfs_trans_handle *trans,
406 struct btrfs_root *root,
407 struct extent_buffer *eb)
412 u64 *raid_map = NULL;
413 struct btrfs_multi_bio *multi = NULL;
417 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
418 eb->start, &length, &multi, 0, &raid_map);
421 ret = write_raid56_with_parity(root->fs_info, eb, multi,
424 } else while (dev_nr < multi->num_stripes) {
426 eb->fd = multi->stripes[dev_nr].dev->fd;
427 eb->dev_bytenr = multi->stripes[dev_nr].physical;
428 multi->stripes[dev_nr].dev->total_ios++;
430 ret = write_extent_to_disk(eb);
438 int write_tree_block(struct btrfs_trans_handle *trans,
439 struct btrfs_root *root,
440 struct extent_buffer *eb)
442 if (check_tree_block(root, eb)) {
443 print_tree_block_error(root, eb, check_tree_block(root, eb));
447 if (trans && !btrfs_buffer_uptodate(eb, trans->transid))
450 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
451 csum_tree_block(root, eb, 0);
453 return write_and_map_eb(trans, root, eb);
456 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
457 u32 stripesize, struct btrfs_root *root,
458 struct btrfs_fs_info *fs_info, u64 objectid)
461 root->commit_root = NULL;
462 root->sectorsize = sectorsize;
463 root->nodesize = nodesize;
464 root->leafsize = leafsize;
465 root->stripesize = stripesize;
467 root->track_dirty = 0;
469 root->fs_info = fs_info;
470 root->objectid = objectid;
471 root->last_trans = 0;
472 root->highest_inode = 0;
473 root->last_inode_alloc = 0;
475 INIT_LIST_HEAD(&root->dirty_list);
476 INIT_LIST_HEAD(&root->orphan_data_extents);
477 memset(&root->root_key, 0, sizeof(root->root_key));
478 memset(&root->root_item, 0, sizeof(root->root_item));
479 root->root_key.objectid = objectid;
483 static int update_cowonly_root(struct btrfs_trans_handle *trans,
484 struct btrfs_root *root)
488 struct btrfs_root *tree_root = root->fs_info->tree_root;
490 btrfs_write_dirty_block_groups(trans, root);
492 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
493 if (old_root_bytenr == root->node->start)
495 btrfs_set_root_bytenr(&root->root_item,
497 btrfs_set_root_generation(&root->root_item,
499 root->root_item.level = btrfs_header_level(root->node);
500 ret = btrfs_update_root(trans, tree_root,
504 btrfs_write_dirty_block_groups(trans, root);
509 static int commit_tree_roots(struct btrfs_trans_handle *trans,
510 struct btrfs_fs_info *fs_info)
512 struct btrfs_root *root;
513 struct list_head *next;
514 struct extent_buffer *eb;
517 if (fs_info->readonly)
520 eb = fs_info->tree_root->node;
521 extent_buffer_get(eb);
522 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
523 free_extent_buffer(eb);
527 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
528 next = fs_info->dirty_cowonly_roots.next;
530 root = list_entry(next, struct btrfs_root, dirty_list);
531 update_cowonly_root(trans, root);
532 free_extent_buffer(root->commit_root);
533 root->commit_root = NULL;
539 static int __commit_transaction(struct btrfs_trans_handle *trans,
540 struct btrfs_root *root)
544 struct extent_buffer *eb;
545 struct extent_io_tree *tree = &root->fs_info->extent_cache;
549 ret = find_first_extent_bit(tree, 0, &start, &end,
553 while(start <= end) {
554 eb = find_first_extent_buffer(tree, start);
555 BUG_ON(!eb || eb->start != start);
556 ret = write_tree_block(trans, root, eb);
559 clear_extent_buffer_dirty(eb);
560 free_extent_buffer(eb);
566 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
567 struct btrfs_root *root)
569 u64 transid = trans->transid;
571 struct btrfs_fs_info *fs_info = root->fs_info;
573 if (root->commit_root == root->node)
575 if (root == root->fs_info->tree_root)
577 if (root == root->fs_info->chunk_root)
580 free_extent_buffer(root->commit_root);
581 root->commit_root = NULL;
583 btrfs_set_root_bytenr(&root->root_item, root->node->start);
584 btrfs_set_root_generation(&root->root_item, trans->transid);
585 root->root_item.level = btrfs_header_level(root->node);
586 ret = btrfs_update_root(trans, root->fs_info->tree_root,
587 &root->root_key, &root->root_item);
590 ret = commit_tree_roots(trans, fs_info);
592 ret = __commit_transaction(trans, root);
594 write_ctree_super(trans, root);
595 btrfs_finish_extent_commit(trans, fs_info->extent_root,
596 &fs_info->pinned_extents);
597 btrfs_free_transaction(root, trans);
598 free_extent_buffer(root->commit_root);
599 root->commit_root = NULL;
600 fs_info->running_transaction = NULL;
601 fs_info->last_trans_committed = transid;
605 static int find_and_setup_root(struct btrfs_root *tree_root,
606 struct btrfs_fs_info *fs_info,
607 u64 objectid, struct btrfs_root *root)
613 __setup_root(tree_root->nodesize, tree_root->leafsize,
614 tree_root->sectorsize, tree_root->stripesize,
615 root, fs_info, objectid);
616 ret = btrfs_find_last_root(tree_root, objectid,
617 &root->root_item, &root->root_key);
621 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
622 generation = btrfs_root_generation(&root->root_item);
623 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
624 blocksize, generation);
625 if (!extent_buffer_uptodate(root->node))
631 static int find_and_setup_log_root(struct btrfs_root *tree_root,
632 struct btrfs_fs_info *fs_info,
633 struct btrfs_super_block *disk_super)
636 u64 blocknr = btrfs_super_log_root(disk_super);
637 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
647 blocksize = btrfs_level_size(tree_root,
648 btrfs_super_log_root_level(disk_super));
650 __setup_root(tree_root->nodesize, tree_root->leafsize,
651 tree_root->sectorsize, tree_root->stripesize,
652 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
654 log_root->node = read_tree_block(tree_root, blocknr,
656 btrfs_super_generation(disk_super) + 1);
658 fs_info->log_root_tree = log_root;
660 if (!extent_buffer_uptodate(log_root->node)) {
661 free_extent_buffer(log_root->node);
663 fs_info->log_root_tree = NULL;
670 int btrfs_free_fs_root(struct btrfs_root *root)
673 free_extent_buffer(root->node);
674 if (root->commit_root)
675 free_extent_buffer(root->commit_root);
680 static void __free_fs_root(struct rb_node *node)
682 struct btrfs_root *root;
684 root = container_of(node, struct btrfs_root, rb_node);
685 btrfs_free_fs_root(root);
688 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
690 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
691 struct btrfs_key *location)
693 struct btrfs_root *root;
694 struct btrfs_root *tree_root = fs_info->tree_root;
695 struct btrfs_path *path;
696 struct extent_buffer *l;
701 root = calloc(1, sizeof(*root));
703 return ERR_PTR(-ENOMEM);
704 if (location->offset == (u64)-1) {
705 ret = find_and_setup_root(tree_root, fs_info,
706 location->objectid, root);
714 __setup_root(tree_root->nodesize, tree_root->leafsize,
715 tree_root->sectorsize, tree_root->stripesize,
716 root, fs_info, location->objectid);
718 path = btrfs_alloc_path();
720 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
727 read_extent_buffer(l, &root->root_item,
728 btrfs_item_ptr_offset(l, path->slots[0]),
729 sizeof(root->root_item));
730 memcpy(&root->root_key, location, sizeof(*location));
733 btrfs_free_path(path);
738 generation = btrfs_root_generation(&root->root_item);
739 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
740 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
741 blocksize, generation);
742 if (!extent_buffer_uptodate(root->node)) {
744 return ERR_PTR(-EIO);
751 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
754 u64 objectid = *((u64 *)data);
755 struct btrfs_root *root;
757 root = rb_entry(node, struct btrfs_root, rb_node);
758 if (objectid > root->objectid)
760 else if (objectid < root->objectid)
766 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
767 struct rb_node *node2)
769 struct btrfs_root *root;
771 root = rb_entry(node2, struct btrfs_root, rb_node);
772 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
775 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
776 struct btrfs_key *location)
778 struct btrfs_root *root;
779 struct rb_node *node;
781 u64 objectid = location->objectid;
783 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
784 return fs_info->tree_root;
785 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
786 return fs_info->extent_root;
787 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
788 return fs_info->chunk_root;
789 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
790 return fs_info->dev_root;
791 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
792 return fs_info->csum_root;
793 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
794 return fs_info->quota_root;
796 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
797 location->offset != (u64)-1);
799 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
800 btrfs_fs_roots_compare_objectids, NULL);
802 return container_of(node, struct btrfs_root, rb_node);
804 root = btrfs_read_fs_root_no_cache(fs_info, location);
808 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
809 btrfs_fs_roots_compare_roots);
814 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
816 free(fs_info->tree_root);
817 free(fs_info->extent_root);
818 free(fs_info->chunk_root);
819 free(fs_info->dev_root);
820 free(fs_info->csum_root);
821 free(fs_info->quota_root);
822 free(fs_info->super_copy);
823 free(fs_info->log_root_tree);
827 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
829 struct btrfs_fs_info *fs_info;
831 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
835 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
836 fs_info->extent_root = calloc(1, sizeof(struct btrfs_root));
837 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
838 fs_info->dev_root = calloc(1, sizeof(struct btrfs_root));
839 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
840 fs_info->quota_root = calloc(1, sizeof(struct btrfs_root));
841 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
843 if (!fs_info->tree_root || !fs_info->extent_root ||
844 !fs_info->chunk_root || !fs_info->dev_root ||
845 !fs_info->csum_root || !fs_info->quota_root ||
846 !fs_info->super_copy)
849 extent_io_tree_init(&fs_info->extent_cache);
850 extent_io_tree_init(&fs_info->free_space_cache);
851 extent_io_tree_init(&fs_info->block_group_cache);
852 extent_io_tree_init(&fs_info->pinned_extents);
853 extent_io_tree_init(&fs_info->pending_del);
854 extent_io_tree_init(&fs_info->extent_ins);
855 fs_info->excluded_extents = NULL;
857 fs_info->fs_root_tree = RB_ROOT;
858 cache_tree_init(&fs_info->mapping_tree.cache_tree);
860 mutex_init(&fs_info->fs_mutex);
861 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
862 INIT_LIST_HEAD(&fs_info->space_info);
863 INIT_LIST_HEAD(&fs_info->recow_ebs);
866 fs_info->readonly = 1;
868 fs_info->super_bytenr = sb_bytenr;
869 fs_info->data_alloc_profile = (u64)-1;
870 fs_info->metadata_alloc_profile = (u64)-1;
871 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
874 btrfs_free_fs_info(fs_info);
878 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
882 features = btrfs_super_incompat_flags(sb) &
883 ~BTRFS_FEATURE_INCOMPAT_SUPP;
885 printk("couldn't open because of unsupported "
886 "option features (%Lx).\n",
887 (unsigned long long)features);
891 features = btrfs_super_incompat_flags(sb);
892 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
893 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
894 btrfs_set_super_incompat_flags(sb, features);
897 features = btrfs_super_compat_ro_flags(sb) &
898 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
899 if (writable && features) {
900 printk("couldn't open RDWR because of unsupported "
901 "option features (%Lx).\n",
902 (unsigned long long)features);
908 static int find_best_backup_root(struct btrfs_super_block *super)
910 struct btrfs_root_backup *backup;
911 u64 orig_gen = btrfs_super_generation(super);
916 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
917 backup = super->super_roots + i;
918 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
919 btrfs_backup_tree_root_gen(backup) > gen) {
921 gen = btrfs_backup_tree_root_gen(backup);
927 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
928 enum btrfs_open_ctree_flags flags,
929 struct btrfs_root *info_root,
930 u64 objectid, char *str)
932 struct btrfs_super_block *sb = fs_info->super_copy;
933 struct btrfs_root *root = fs_info->tree_root;
934 u32 leafsize = btrfs_super_leafsize(sb);
937 ret = find_and_setup_root(root, fs_info, objectid, info_root);
939 printk("Couldn't setup %s tree\n", str);
940 if (!(flags & OPEN_CTREE_PARTIAL))
943 * Need a blank node here just so we don't screw up in the
944 * million of places that assume a root has a valid ->node
947 btrfs_find_create_tree_block(info_root, 0, leafsize);
948 if (!info_root->node)
950 clear_extent_buffer_uptodate(NULL, info_root->node);
956 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
957 enum btrfs_open_ctree_flags flags)
959 struct btrfs_super_block *sb = fs_info->super_copy;
960 struct btrfs_root *root;
961 struct btrfs_key key;
970 nodesize = btrfs_super_nodesize(sb);
971 leafsize = btrfs_super_leafsize(sb);
972 sectorsize = btrfs_super_sectorsize(sb);
973 stripesize = btrfs_super_stripesize(sb);
975 root = fs_info->tree_root;
976 __setup_root(nodesize, leafsize, sectorsize, stripesize,
977 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
978 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
979 generation = btrfs_super_generation(sb);
981 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
982 root_tree_bytenr = btrfs_super_root(sb);
983 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
984 struct btrfs_root_backup *backup;
985 int index = find_best_backup_root(sb);
986 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
987 fprintf(stderr, "Invalid backup root number\n");
990 backup = fs_info->super_copy->super_roots + index;
991 root_tree_bytenr = btrfs_backup_tree_root(backup);
992 generation = btrfs_backup_tree_root_gen(backup);
995 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
997 if (!extent_buffer_uptodate(root->node)) {
998 fprintf(stderr, "Couldn't read tree root\n");
1002 ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
1003 BTRFS_EXTENT_TREE_OBJECTID, "extent");
1006 fs_info->extent_root->track_dirty = 1;
1008 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
1011 printk("Couldn't setup device tree\n");
1014 fs_info->dev_root->track_dirty = 1;
1016 ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
1017 BTRFS_CSUM_TREE_OBJECTID, "csum");
1020 fs_info->csum_root->track_dirty = 1;
1022 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
1023 fs_info->quota_root);
1025 fs_info->quota_enabled = 1;
1027 ret = find_and_setup_log_root(root, fs_info, sb);
1029 printk("Couldn't setup log root tree\n");
1030 if (!(flags & OPEN_CTREE_PARTIAL))
1034 fs_info->generation = generation;
1035 fs_info->last_trans_committed = generation;
1036 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
1037 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
1038 btrfs_read_block_groups(fs_info->tree_root);
1040 key.objectid = BTRFS_FS_TREE_OBJECTID;
1041 key.type = BTRFS_ROOT_ITEM_KEY;
1042 key.offset = (u64)-1;
1043 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
1045 if (IS_ERR(fs_info->fs_root))
1050 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
1052 if (fs_info->quota_root)
1053 free_extent_buffer(fs_info->quota_root->node);
1054 if (fs_info->csum_root)
1055 free_extent_buffer(fs_info->csum_root->node);
1056 if (fs_info->dev_root)
1057 free_extent_buffer(fs_info->dev_root->node);
1058 if (fs_info->extent_root)
1059 free_extent_buffer(fs_info->extent_root->node);
1060 if (fs_info->tree_root)
1061 free_extent_buffer(fs_info->tree_root->node);
1062 if (fs_info->log_root_tree)
1063 free_extent_buffer(fs_info->log_root_tree->node);
1064 if (fs_info->chunk_root)
1065 free_extent_buffer(fs_info->chunk_root->node);
1068 static void free_map_lookup(struct cache_extent *ce)
1070 struct map_lookup *map;
1072 map = container_of(ce, struct map_lookup, ce);
1076 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
1078 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
1080 while (!list_empty(&fs_info->recow_ebs)) {
1081 struct extent_buffer *eb;
1082 eb = list_first_entry(&fs_info->recow_ebs,
1083 struct extent_buffer, recow);
1084 list_del_init(&eb->recow);
1085 free_extent_buffer(eb);
1087 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
1088 extent_io_tree_cleanup(&fs_info->extent_cache);
1089 extent_io_tree_cleanup(&fs_info->free_space_cache);
1090 extent_io_tree_cleanup(&fs_info->block_group_cache);
1091 extent_io_tree_cleanup(&fs_info->pinned_extents);
1092 extent_io_tree_cleanup(&fs_info->pending_del);
1093 extent_io_tree_cleanup(&fs_info->extent_ins);
1096 int btrfs_scan_fs_devices(int fd, const char *path,
1097 struct btrfs_fs_devices **fs_devices,
1098 u64 sb_bytenr, int super_recover,
1106 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1108 seek_ret = lseek(fd, 0, SEEK_END);
1112 dev_size = seek_ret;
1113 lseek(fd, 0, SEEK_SET);
1114 if (sb_bytenr > dev_size) {
1115 fprintf(stderr, "Superblock bytenr is larger than device size\n");
1119 ret = btrfs_scan_one_device(fd, path, fs_devices,
1120 &total_devs, sb_bytenr, super_recover);
1122 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1126 if (!skip_devices && total_devs != 1) {
1127 ret = btrfs_scan_lblkid();
1134 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1136 struct btrfs_super_block *sb = fs_info->super_copy;
1145 nodesize = btrfs_super_nodesize(sb);
1146 leafsize = btrfs_super_leafsize(sb);
1147 sectorsize = btrfs_super_sectorsize(sb);
1148 stripesize = btrfs_super_stripesize(sb);
1150 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1151 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1153 ret = btrfs_read_sys_array(fs_info->chunk_root);
1157 blocksize = btrfs_level_size(fs_info->chunk_root,
1158 btrfs_super_chunk_root_level(sb));
1159 generation = btrfs_super_chunk_root_generation(sb);
1161 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1162 btrfs_super_chunk_root(sb),
1163 blocksize, generation);
1164 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1165 fprintf(stderr, "Couldn't read chunk root\n");
1169 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1170 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1172 fprintf(stderr, "Couldn't read chunk tree\n");
1179 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1181 u64 root_tree_bytenr,
1182 enum btrfs_open_ctree_flags flags)
1184 struct btrfs_fs_info *fs_info;
1185 struct btrfs_super_block *disk_super;
1186 struct btrfs_fs_devices *fs_devices = NULL;
1187 struct extent_buffer *eb;
1192 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1194 /* try to drop all the caches */
1195 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1196 fprintf(stderr, "Warning, could not drop caches\n");
1198 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1200 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1203 if (flags & OPEN_CTREE_RESTORE)
1204 fs_info->on_restoring = 1;
1205 if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1206 fs_info->suppress_check_block_errors = 1;
1207 if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
1208 fs_info->ignore_fsid_mismatch = 1;
1210 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1211 (flags & OPEN_CTREE_RECOVER_SUPER),
1212 (flags & OPEN_CTREE_NO_DEVICES));
1216 fs_info->fs_devices = fs_devices;
1217 if (flags & OPEN_CTREE_WRITES)
1222 if (flags & OPEN_CTREE_EXCLUSIVE)
1225 ret = btrfs_open_devices(fs_devices, oflags);
1229 disk_super = fs_info->super_copy;
1230 if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1231 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1232 disk_super, sb_bytenr, 1);
1234 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1236 printk("No valid btrfs found\n");
1240 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID &&
1241 !fs_info->ignore_fsid_mismatch) {
1242 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1246 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1248 ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1249 flags & OPEN_CTREE_WRITES);
1253 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1257 eb = fs_info->chunk_root->node;
1258 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1259 btrfs_header_chunk_tree_uuid(eb),
1262 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1263 if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT))
1269 btrfs_release_all_roots(fs_info);
1270 btrfs_cleanup_all_caches(fs_info);
1272 btrfs_close_devices(fs_devices);
1274 btrfs_free_fs_info(fs_info);
1278 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1279 u64 sb_bytenr, u64 root_tree_bytenr,
1280 enum btrfs_open_ctree_flags flags)
1283 struct btrfs_fs_info *info;
1284 int oflags = O_CREAT | O_RDWR;
1286 if (!(flags & OPEN_CTREE_WRITES))
1289 fp = open(filename, oflags, 0600);
1291 fprintf (stderr, "Could not open %s\n", filename);
1294 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1300 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1301 enum btrfs_open_ctree_flags flags)
1303 struct btrfs_fs_info *info;
1305 info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1308 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1309 return info->chunk_root;
1310 return info->fs_root;
1313 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1314 enum btrfs_open_ctree_flags flags)
1316 struct btrfs_fs_info *info;
1317 info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1320 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1321 return info->chunk_root;
1322 return info->fs_root;
1326 * Check if the super is valid:
1327 * - nodesize/sectorsize - minimum, maximum, alignment
1328 * - tree block starts - alignment
1329 * - number of devices - something sane
1330 * - sys array size - maximum
1332 static int check_super(struct btrfs_super_block *sb)
1334 char result[BTRFS_CSUM_SIZE];
1339 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
1340 fprintf(stderr, "ERROR: superblock magic doesn't match\n");
1344 csum_type = btrfs_super_csum_type(sb);
1345 if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
1346 fprintf(stderr, "ERROR: unsupported checksum algorithm %u\n",
1350 csum_size = btrfs_csum_sizes[csum_type];
1353 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1354 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1355 btrfs_csum_final(crc, result);
1357 if (memcmp(result, sb->csum, csum_size)) {
1358 fprintf(stderr, "ERROR: superblock checksum mismatch\n");
1361 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
1362 fprintf(stderr, "ERROR: tree_root level too big: %d >= %d\n",
1363 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
1366 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
1367 fprintf(stderr, "ERROR: chunk_root level too big: %d >= %d\n",
1368 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
1371 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
1372 fprintf(stderr, "ERROR: log_root level too big: %d >= %d\n",
1373 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
1377 if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
1378 fprintf(stderr, "ERROR: tree_root block unaligned: %llu\n",
1379 btrfs_super_root(sb));
1382 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
1383 fprintf(stderr, "ERROR: chunk_root block unaligned: %llu\n",
1384 btrfs_super_chunk_root(sb));
1387 if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
1388 fprintf(stderr, "ERROR: log_root block unaligned: %llu\n",
1389 btrfs_super_log_root(sb));
1392 if (btrfs_super_nodesize(sb) < 4096) {
1393 fprintf(stderr, "ERROR: nodesize too small: %u < 4096\n",
1394 btrfs_super_nodesize(sb));
1397 if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
1398 fprintf(stderr, "ERROR: nodesize unaligned: %u\n",
1399 btrfs_super_nodesize(sb));
1402 if (btrfs_super_sectorsize(sb) < 4096) {
1403 fprintf(stderr, "ERROR: sectorsize too small: %u < 4096\n",
1404 btrfs_super_sectorsize(sb));
1407 if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
1408 fprintf(stderr, "ERROR: sectorsize unaligned: %u\n",
1409 btrfs_super_sectorsize(sb));
1413 if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
1414 char fsid[BTRFS_UUID_UNPARSED_SIZE];
1415 char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
1417 uuid_unparse(sb->fsid, fsid);
1418 uuid_unparse(sb->dev_item.fsid, dev_fsid);
1420 "ERROR: dev_item UUID does not match fsid: %s != %s\n",
1426 * Hint to catch really bogus numbers, bitflips or so
1428 if (btrfs_super_num_devices(sb) > (1UL << 31)) {
1429 fprintf(stderr, "WARNING: suspicious number of devices: %llu\n",
1430 btrfs_super_num_devices(sb));
1433 if (btrfs_super_num_devices(sb) == 0) {
1434 fprintf(stderr, "ERROR: number of devices is 0\n");
1439 * Obvious sys_chunk_array corruptions, it must hold at least one key
1442 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
1443 fprintf(stderr, "BTRFS: system chunk array too big %u > %u\n",
1444 btrfs_super_sys_array_size(sb),
1445 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
1448 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
1449 + sizeof(struct btrfs_chunk)) {
1450 fprintf(stderr, "BTRFS: system chunk array too small %u < %lu\n",
1451 btrfs_super_sys_array_size(sb),
1452 sizeof(struct btrfs_disk_key) +
1453 sizeof(struct btrfs_chunk));
1460 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1463 u8 fsid[BTRFS_FSID_SIZE];
1464 int fsid_is_initialized = 0;
1465 char tmp[BTRFS_SUPER_INFO_SIZE];
1466 struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
1469 int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1473 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1474 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
1475 if (ret < BTRFS_SUPER_INFO_SIZE)
1478 if (btrfs_super_bytenr(buf) != sb_bytenr)
1481 if (check_super(buf))
1483 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1488 * we would like to check all the supers, but that would make
1489 * a btrfs mount succeed after a mkfs from a different FS.
1490 * So, we need to add a special mount option to scan for
1491 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1494 for (i = 0; i < max_super; i++) {
1495 bytenr = btrfs_sb_offset(i);
1496 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1497 if (ret < BTRFS_SUPER_INFO_SIZE)
1500 if (btrfs_super_bytenr(buf) != bytenr )
1502 /* if magic is NULL, the device was removed */
1503 if (btrfs_super_magic(buf) == 0 && i == 0)
1505 if (check_super(buf))
1508 if (!fsid_is_initialized) {
1509 memcpy(fsid, buf->fsid, sizeof(fsid));
1510 fsid_is_initialized = 1;
1511 } else if (memcmp(fsid, buf->fsid, sizeof(fsid))) {
1513 * the superblocks (the original one and
1514 * its backups) contain data of different
1515 * filesystems -> the super cannot be trusted
1520 if (btrfs_super_generation(buf) > transid) {
1521 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1522 transid = btrfs_super_generation(buf);
1526 return transid > 0 ? 0 : -1;
1529 static int write_dev_supers(struct btrfs_root *root,
1530 struct btrfs_super_block *sb,
1531 struct btrfs_device *device)
1537 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1538 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1540 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1541 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1542 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1545 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1546 * zero filled, we can use it directly
1548 ret = pwrite64(device->fd, root->fs_info->super_copy,
1549 BTRFS_SUPER_INFO_SIZE,
1550 root->fs_info->super_bytenr);
1551 if (ret != BTRFS_SUPER_INFO_SIZE)
1556 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1557 bytenr = btrfs_sb_offset(i);
1558 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1561 btrfs_set_super_bytenr(sb, bytenr);
1564 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1565 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1566 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1569 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1570 * zero filled, we can use it directly
1572 ret = pwrite64(device->fd, root->fs_info->super_copy,
1573 BTRFS_SUPER_INFO_SIZE, bytenr);
1574 if (ret != BTRFS_SUPER_INFO_SIZE)
1582 fprintf(stderr, "WARNING: failed to write all sb data\n");
1584 fprintf(stderr, "WARNING: failed to write sb: %s\n",
1589 int write_all_supers(struct btrfs_root *root)
1591 struct list_head *cur;
1592 struct list_head *head = &root->fs_info->fs_devices->devices;
1593 struct btrfs_device *dev;
1594 struct btrfs_super_block *sb;
1595 struct btrfs_dev_item *dev_item;
1599 sb = root->fs_info->super_copy;
1600 dev_item = &sb->dev_item;
1601 list_for_each(cur, head) {
1602 dev = list_entry(cur, struct btrfs_device, dev_list);
1603 if (!dev->writeable)
1606 btrfs_set_stack_device_generation(dev_item, 0);
1607 btrfs_set_stack_device_type(dev_item, dev->type);
1608 btrfs_set_stack_device_id(dev_item, dev->devid);
1609 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1610 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1611 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1612 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1613 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1614 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1615 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1617 flags = btrfs_super_flags(sb);
1618 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1620 ret = write_dev_supers(root, sb, dev);
1626 int write_ctree_super(struct btrfs_trans_handle *trans,
1627 struct btrfs_root *root)
1630 struct btrfs_root *tree_root = root->fs_info->tree_root;
1631 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1633 if (root->fs_info->readonly)
1636 btrfs_set_super_generation(root->fs_info->super_copy,
1638 btrfs_set_super_root(root->fs_info->super_copy,
1639 tree_root->node->start);
1640 btrfs_set_super_root_level(root->fs_info->super_copy,
1641 btrfs_header_level(tree_root->node));
1642 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1643 chunk_root->node->start);
1644 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1645 btrfs_header_level(chunk_root->node));
1646 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1647 btrfs_header_generation(chunk_root->node));
1649 ret = write_all_supers(root);
1651 fprintf(stderr, "failed to write new super block err %d\n", ret);
1655 int close_ctree(struct btrfs_root *root)
1658 struct btrfs_trans_handle *trans;
1659 struct btrfs_fs_info *fs_info = root->fs_info;
1661 if (fs_info->last_trans_committed !=
1662 fs_info->generation) {
1663 trans = btrfs_start_transaction(root, 1);
1664 btrfs_commit_transaction(trans, root);
1665 trans = btrfs_start_transaction(root, 1);
1666 ret = commit_tree_roots(trans, fs_info);
1668 ret = __commit_transaction(trans, root);
1670 write_ctree_super(trans, root);
1671 btrfs_free_transaction(root, trans);
1673 btrfs_free_block_groups(fs_info);
1675 free_fs_roots_tree(&fs_info->fs_root_tree);
1677 btrfs_release_all_roots(fs_info);
1678 btrfs_close_devices(fs_info->fs_devices);
1679 btrfs_cleanup_all_caches(fs_info);
1680 btrfs_free_fs_info(fs_info);
1684 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1685 struct extent_buffer *eb)
1687 return clear_extent_buffer_dirty(eb);
1690 int wait_on_tree_block_writeback(struct btrfs_root *root,
1691 struct extent_buffer *eb)
1696 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1698 set_extent_buffer_dirty(eb);
1701 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1705 ret = extent_buffer_uptodate(buf);
1709 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1713 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1715 return set_extent_buffer_uptodate(eb);