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)
41 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
44 struct btrfs_fs_devices *fs_devices;
45 int ret = BTRFS_BAD_FSID;
47 if (buf->start != btrfs_header_bytenr(buf))
48 return BTRFS_BAD_BYTENR;
50 fs_devices = root->fs_info->fs_devices;
52 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
58 fs_devices = fs_devices->seed;
63 static void print_tree_block_error(struct btrfs_root *root,
64 struct extent_buffer *eb,
67 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
68 char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
69 u8 buf[BTRFS_UUID_SIZE];
73 read_extent_buffer(eb, buf, btrfs_header_fsid(),
75 uuid_unparse(buf, found_uuid);
76 uuid_unparse(root->fs_info->fsid, fs_uuid);
77 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
80 case BTRFS_BAD_BYTENR:
81 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
82 eb->start, btrfs_header_bytenr(eb));
87 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
89 return crc32c(seed, data, len);
92 void btrfs_csum_final(u32 crc, char *result)
94 *(__le32 *)result = ~cpu_to_le32(crc);
97 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
98 int verify, int silent)
104 result = malloc(csum_size * sizeof(char));
108 len = buf->len - BTRFS_CSUM_SIZE;
109 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
110 btrfs_csum_final(crc, result);
113 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
115 printk("checksum verify failed on %llu found %08X wanted %08X\n",
116 (unsigned long long)buf->start,
118 *((u32*)(char *)buf->data));
123 write_extent_buffer(buf, result, 0, csum_size);
129 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
131 return __csum_tree_block_size(buf, csum_size, verify, 0);
134 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
136 return __csum_tree_block_size(buf, csum_size, 1, 1);
139 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
143 btrfs_super_csum_size(root->fs_info->super_copy);
144 if (verify && root->fs_info->suppress_check_block_errors)
145 return verify_tree_block_csum_silent(buf, csum_size);
146 return csum_tree_block_size(buf, csum_size, verify);
149 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
150 u64 bytenr, u32 blocksize)
152 return find_extent_buffer(&root->fs_info->extent_cache,
156 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
157 u64 bytenr, u32 blocksize)
159 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
163 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
166 struct extent_buffer *eb;
168 struct btrfs_multi_bio *multi = NULL;
169 struct btrfs_device *device;
171 eb = btrfs_find_tree_block(root, bytenr, blocksize);
172 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
173 !btrfs_map_block(&root->fs_info->mapping_tree, READ,
174 bytenr, &length, &multi, 0, NULL)) {
175 device = multi->stripes[0].dev;
177 blocksize = min(blocksize, (u32)(64 * 1024));
178 readahead(device->fd, multi->stripes[0].physical, blocksize);
181 free_extent_buffer(eb);
185 static int verify_parent_transid(struct extent_io_tree *io_tree,
186 struct extent_buffer *eb, u64 parent_transid,
191 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
194 if (extent_buffer_uptodate(eb) &&
195 btrfs_header_generation(eb) == parent_transid) {
199 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
200 (unsigned long long)eb->start,
201 (unsigned long long)parent_transid,
202 (unsigned long long)btrfs_header_generation(eb));
204 eb->flags |= EXTENT_BAD_TRANSID;
205 printk("Ignoring transid failure\n");
211 clear_extent_buffer_uptodate(io_tree, eb);
217 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
219 unsigned long offset = 0;
220 struct btrfs_multi_bio *multi = NULL;
221 struct btrfs_device *device;
224 unsigned long bytes_left = eb->len;
227 read_len = bytes_left;
230 if (!info->on_restoring &&
231 eb->start != BTRFS_SUPER_INFO_OFFSET) {
232 ret = btrfs_map_block(&info->mapping_tree, READ,
233 eb->start + offset, &read_len, &multi,
236 printk("Couldn't map the block %Lu\n", eb->start + offset);
240 device = multi->stripes[0].dev;
242 if (device->fd == 0) {
249 eb->dev_bytenr = multi->stripes[0].physical;
253 /* special case for restore metadump */
254 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
255 if (device->devid == 1)
260 eb->dev_bytenr = eb->start;
264 if (read_len > bytes_left)
265 read_len = bytes_left;
267 ret = read_extent_from_disk(eb, offset, read_len);
271 bytes_left -= read_len;
276 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
277 u32 blocksize, u64 parent_transid)
280 struct extent_buffer *eb;
281 u64 best_transid = 0;
287 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
289 return ERR_PTR(-ENOMEM);
291 if (btrfs_buffer_uptodate(eb, parent_transid))
295 ret = read_whole_eb(root->fs_info, eb, mirror_num);
296 if (ret == 0 && csum_tree_block(root, eb, 1) == 0 &&
297 check_tree_block(root, eb) == 0 &&
298 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
300 if (eb->flags & EXTENT_BAD_TRANSID &&
301 list_empty(&eb->recow)) {
302 list_add_tail(&eb->recow,
303 &root->fs_info->recow_ebs);
306 btrfs_set_buffer_uptodate(eb);
310 if (check_tree_block(root, eb)) {
311 if (!root->fs_info->suppress_check_block_errors)
312 print_tree_block_error(root, eb,
313 check_tree_block(root, eb));
315 if (!root->fs_info->suppress_check_block_errors)
316 fprintf(stderr, "Csum didn't match\n");
321 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
323 if (num_copies == 1) {
327 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
328 best_transid = btrfs_header_generation(eb);
329 good_mirror = mirror_num;
332 if (mirror_num > num_copies) {
333 mirror_num = good_mirror;
338 free_extent_buffer(eb);
342 int write_and_map_eb(struct btrfs_trans_handle *trans,
343 struct btrfs_root *root,
344 struct extent_buffer *eb)
349 u64 *raid_map = NULL;
350 struct btrfs_multi_bio *multi = NULL;
354 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
355 eb->start, &length, &multi, 0, &raid_map);
358 ret = write_raid56_with_parity(root->fs_info, eb, multi,
361 } else while (dev_nr < multi->num_stripes) {
363 eb->fd = multi->stripes[dev_nr].dev->fd;
364 eb->dev_bytenr = multi->stripes[dev_nr].physical;
365 multi->stripes[dev_nr].dev->total_ios++;
367 ret = write_extent_to_disk(eb);
374 static int write_tree_block(struct btrfs_trans_handle *trans,
375 struct btrfs_root *root,
376 struct extent_buffer *eb)
378 if (check_tree_block(root, eb)) {
379 print_tree_block_error(root, eb, check_tree_block(root, eb));
383 if (!btrfs_buffer_uptodate(eb, trans->transid))
386 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
387 csum_tree_block(root, eb, 0);
389 return write_and_map_eb(trans, root, eb);
392 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
393 u32 stripesize, struct btrfs_root *root,
394 struct btrfs_fs_info *fs_info, u64 objectid)
397 root->commit_root = NULL;
398 root->sectorsize = sectorsize;
399 root->nodesize = nodesize;
400 root->leafsize = leafsize;
401 root->stripesize = stripesize;
403 root->track_dirty = 0;
405 root->fs_info = fs_info;
406 root->objectid = objectid;
407 root->last_trans = 0;
408 root->highest_inode = 0;
409 root->last_inode_alloc = 0;
411 INIT_LIST_HEAD(&root->dirty_list);
412 INIT_LIST_HEAD(&root->orphan_data_extents);
413 memset(&root->root_key, 0, sizeof(root->root_key));
414 memset(&root->root_item, 0, sizeof(root->root_item));
415 root->root_key.objectid = objectid;
419 static int update_cowonly_root(struct btrfs_trans_handle *trans,
420 struct btrfs_root *root)
424 struct btrfs_root *tree_root = root->fs_info->tree_root;
426 btrfs_write_dirty_block_groups(trans, root);
428 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
429 if (old_root_bytenr == root->node->start)
431 btrfs_set_root_bytenr(&root->root_item,
433 btrfs_set_root_generation(&root->root_item,
435 root->root_item.level = btrfs_header_level(root->node);
436 ret = btrfs_update_root(trans, tree_root,
440 btrfs_write_dirty_block_groups(trans, root);
445 static int commit_tree_roots(struct btrfs_trans_handle *trans,
446 struct btrfs_fs_info *fs_info)
448 struct btrfs_root *root;
449 struct list_head *next;
450 struct extent_buffer *eb;
453 if (fs_info->readonly)
456 eb = fs_info->tree_root->node;
457 extent_buffer_get(eb);
458 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
459 free_extent_buffer(eb);
463 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
464 next = fs_info->dirty_cowonly_roots.next;
466 root = list_entry(next, struct btrfs_root, dirty_list);
467 update_cowonly_root(trans, root);
468 free_extent_buffer(root->commit_root);
469 root->commit_root = NULL;
475 static int __commit_transaction(struct btrfs_trans_handle *trans,
476 struct btrfs_root *root)
480 struct extent_buffer *eb;
481 struct extent_io_tree *tree = &root->fs_info->extent_cache;
485 ret = find_first_extent_bit(tree, 0, &start, &end,
489 while(start <= end) {
490 eb = find_first_extent_buffer(tree, start);
491 BUG_ON(!eb || eb->start != start);
492 ret = write_tree_block(trans, root, eb);
495 clear_extent_buffer_dirty(eb);
496 free_extent_buffer(eb);
502 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
503 struct btrfs_root *root)
505 u64 transid = trans->transid;
507 struct btrfs_fs_info *fs_info = root->fs_info;
509 if (root->commit_root == root->node)
511 if (root == root->fs_info->tree_root)
514 free_extent_buffer(root->commit_root);
515 root->commit_root = NULL;
517 btrfs_set_root_bytenr(&root->root_item, root->node->start);
518 btrfs_set_root_generation(&root->root_item, trans->transid);
519 root->root_item.level = btrfs_header_level(root->node);
520 ret = btrfs_update_root(trans, root->fs_info->tree_root,
521 &root->root_key, &root->root_item);
524 ret = commit_tree_roots(trans, fs_info);
526 ret = __commit_transaction(trans, root);
528 write_ctree_super(trans, root);
529 btrfs_finish_extent_commit(trans, fs_info->extent_root,
530 &fs_info->pinned_extents);
531 btrfs_free_transaction(root, trans);
532 free_extent_buffer(root->commit_root);
533 root->commit_root = NULL;
534 fs_info->running_transaction = NULL;
535 fs_info->last_trans_committed = transid;
539 static int find_and_setup_root(struct btrfs_root *tree_root,
540 struct btrfs_fs_info *fs_info,
541 u64 objectid, struct btrfs_root *root)
547 __setup_root(tree_root->nodesize, tree_root->leafsize,
548 tree_root->sectorsize, tree_root->stripesize,
549 root, fs_info, objectid);
550 ret = btrfs_find_last_root(tree_root, objectid,
551 &root->root_item, &root->root_key);
555 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
556 generation = btrfs_root_generation(&root->root_item);
557 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
558 blocksize, generation);
559 if (!extent_buffer_uptodate(root->node))
565 static int find_and_setup_log_root(struct btrfs_root *tree_root,
566 struct btrfs_fs_info *fs_info,
567 struct btrfs_super_block *disk_super)
570 u64 blocknr = btrfs_super_log_root(disk_super);
571 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
581 blocksize = btrfs_level_size(tree_root,
582 btrfs_super_log_root_level(disk_super));
584 __setup_root(tree_root->nodesize, tree_root->leafsize,
585 tree_root->sectorsize, tree_root->stripesize,
586 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
588 log_root->node = read_tree_block(tree_root, blocknr,
590 btrfs_super_generation(disk_super) + 1);
592 fs_info->log_root_tree = log_root;
594 if (!extent_buffer_uptodate(log_root->node)) {
595 free_extent_buffer(log_root->node);
597 fs_info->log_root_tree = NULL;
604 int btrfs_free_fs_root(struct btrfs_root *root)
607 free_extent_buffer(root->node);
608 if (root->commit_root)
609 free_extent_buffer(root->commit_root);
614 static void __free_fs_root(struct rb_node *node)
616 struct btrfs_root *root;
618 root = container_of(node, struct btrfs_root, rb_node);
619 btrfs_free_fs_root(root);
622 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
624 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
625 struct btrfs_key *location)
627 struct btrfs_root *root;
628 struct btrfs_root *tree_root = fs_info->tree_root;
629 struct btrfs_path *path;
630 struct extent_buffer *l;
635 root = malloc(sizeof(*root));
637 return ERR_PTR(-ENOMEM);
638 memset(root, 0, sizeof(*root));
639 if (location->offset == (u64)-1) {
640 ret = find_and_setup_root(tree_root, fs_info,
641 location->objectid, root);
649 __setup_root(tree_root->nodesize, tree_root->leafsize,
650 tree_root->sectorsize, tree_root->stripesize,
651 root, fs_info, location->objectid);
653 path = btrfs_alloc_path();
655 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
662 read_extent_buffer(l, &root->root_item,
663 btrfs_item_ptr_offset(l, path->slots[0]),
664 sizeof(root->root_item));
665 memcpy(&root->root_key, location, sizeof(*location));
668 btrfs_free_path(path);
673 generation = btrfs_root_generation(&root->root_item);
674 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
675 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
676 blocksize, generation);
677 if (!extent_buffer_uptodate(root->node)) {
679 return ERR_PTR(-EIO);
686 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
689 u64 objectid = *((u64 *)data);
690 struct btrfs_root *root;
692 root = rb_entry(node, struct btrfs_root, rb_node);
693 if (objectid > root->objectid)
695 else if (objectid < root->objectid)
701 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
702 struct rb_node *node2)
704 struct btrfs_root *root;
706 root = rb_entry(node2, struct btrfs_root, rb_node);
707 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
710 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
711 struct btrfs_key *location)
713 struct btrfs_root *root;
714 struct rb_node *node;
716 u64 objectid = location->objectid;
718 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
719 return fs_info->tree_root;
720 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
721 return fs_info->extent_root;
722 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
723 return fs_info->chunk_root;
724 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
725 return fs_info->dev_root;
726 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
727 return fs_info->csum_root;
728 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
729 return fs_info->quota_root;
731 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
732 location->offset != (u64)-1);
734 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
735 btrfs_fs_roots_compare_objectids, NULL);
737 return container_of(node, struct btrfs_root, rb_node);
739 root = btrfs_read_fs_root_no_cache(fs_info, location);
743 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
744 btrfs_fs_roots_compare_roots);
749 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
751 free(fs_info->tree_root);
752 free(fs_info->extent_root);
753 free(fs_info->chunk_root);
754 free(fs_info->dev_root);
755 free(fs_info->csum_root);
756 free(fs_info->quota_root);
757 free(fs_info->super_copy);
758 free(fs_info->log_root_tree);
762 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
764 struct btrfs_fs_info *fs_info;
766 fs_info = malloc(sizeof(struct btrfs_fs_info));
770 memset(fs_info, 0, sizeof(struct btrfs_fs_info));
772 fs_info->tree_root = malloc(sizeof(struct btrfs_root));
773 fs_info->extent_root = malloc(sizeof(struct btrfs_root));
774 fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
775 fs_info->dev_root = malloc(sizeof(struct btrfs_root));
776 fs_info->csum_root = malloc(sizeof(struct btrfs_root));
777 fs_info->quota_root = malloc(sizeof(struct btrfs_root));
778 fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
780 if (!fs_info->tree_root || !fs_info->extent_root ||
781 !fs_info->chunk_root || !fs_info->dev_root ||
782 !fs_info->csum_root || !fs_info->quota_root ||
783 !fs_info->super_copy)
786 memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
787 memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
788 memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
789 memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
790 memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
791 memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
792 memset(fs_info->quota_root, 0, sizeof(struct btrfs_root));
794 extent_io_tree_init(&fs_info->extent_cache);
795 extent_io_tree_init(&fs_info->free_space_cache);
796 extent_io_tree_init(&fs_info->block_group_cache);
797 extent_io_tree_init(&fs_info->pinned_extents);
798 extent_io_tree_init(&fs_info->pending_del);
799 extent_io_tree_init(&fs_info->extent_ins);
800 fs_info->excluded_extents = NULL;
802 fs_info->fs_root_tree = RB_ROOT;
803 cache_tree_init(&fs_info->mapping_tree.cache_tree);
805 mutex_init(&fs_info->fs_mutex);
806 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
807 INIT_LIST_HEAD(&fs_info->space_info);
808 INIT_LIST_HEAD(&fs_info->recow_ebs);
811 fs_info->readonly = 1;
813 fs_info->super_bytenr = sb_bytenr;
814 fs_info->data_alloc_profile = (u64)-1;
815 fs_info->metadata_alloc_profile = (u64)-1;
816 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
819 btrfs_free_fs_info(fs_info);
823 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
827 features = btrfs_super_incompat_flags(sb) &
828 ~BTRFS_FEATURE_INCOMPAT_SUPP;
830 printk("couldn't open because of unsupported "
831 "option features (%Lx).\n",
832 (unsigned long long)features);
836 features = btrfs_super_incompat_flags(sb);
837 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
838 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
839 btrfs_set_super_incompat_flags(sb, features);
842 features = btrfs_super_compat_ro_flags(sb) &
843 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
844 if (writable && features) {
845 printk("couldn't open RDWR because of unsupported "
846 "option features (%Lx).\n",
847 (unsigned long long)features);
853 static int find_best_backup_root(struct btrfs_super_block *super)
855 struct btrfs_root_backup *backup;
856 u64 orig_gen = btrfs_super_generation(super);
861 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
862 backup = super->super_roots + i;
863 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
864 btrfs_backup_tree_root_gen(backup) > gen) {
866 gen = btrfs_backup_tree_root_gen(backup);
872 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
873 enum btrfs_open_ctree_flags flags,
874 struct btrfs_root *info_root,
875 u64 objectid, char *str)
877 struct btrfs_super_block *sb = fs_info->super_copy;
878 struct btrfs_root *root = fs_info->tree_root;
879 u32 leafsize = btrfs_super_leafsize(sb);
882 ret = find_and_setup_root(root, fs_info, objectid, info_root);
884 printk("Couldn't setup %s tree\n", str);
885 if (!(flags & OPEN_CTREE_PARTIAL))
888 * Need a blank node here just so we don't screw up in the
889 * million of places that assume a root has a valid ->node
892 btrfs_find_create_tree_block(info_root, 0, leafsize);
893 if (!info_root->node)
895 clear_extent_buffer_uptodate(NULL, info_root->node);
901 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
902 enum btrfs_open_ctree_flags flags)
904 struct btrfs_super_block *sb = fs_info->super_copy;
905 struct btrfs_root *root;
906 struct btrfs_key key;
915 nodesize = btrfs_super_nodesize(sb);
916 leafsize = btrfs_super_leafsize(sb);
917 sectorsize = btrfs_super_sectorsize(sb);
918 stripesize = btrfs_super_stripesize(sb);
920 root = fs_info->tree_root;
921 __setup_root(nodesize, leafsize, sectorsize, stripesize,
922 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
923 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
924 generation = btrfs_super_generation(sb);
926 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
927 root_tree_bytenr = btrfs_super_root(sb);
928 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
929 struct btrfs_root_backup *backup;
930 int index = find_best_backup_root(sb);
931 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
932 fprintf(stderr, "Invalid backup root number\n");
935 backup = fs_info->super_copy->super_roots + index;
936 root_tree_bytenr = btrfs_backup_tree_root(backup);
937 generation = btrfs_backup_tree_root_gen(backup);
940 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
942 if (!extent_buffer_uptodate(root->node)) {
943 fprintf(stderr, "Couldn't read tree root\n");
947 ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
948 BTRFS_EXTENT_TREE_OBJECTID, "extent");
951 fs_info->extent_root->track_dirty = 1;
953 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
956 printk("Couldn't setup device tree\n");
959 fs_info->dev_root->track_dirty = 1;
961 ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
962 BTRFS_CSUM_TREE_OBJECTID, "csum");
965 fs_info->csum_root->track_dirty = 1;
967 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
968 fs_info->quota_root);
970 fs_info->quota_enabled = 1;
972 ret = find_and_setup_log_root(root, fs_info, sb);
974 printk("Couldn't setup log root tree\n");
975 if (!(flags & OPEN_CTREE_PARTIAL))
979 fs_info->generation = generation;
980 fs_info->last_trans_committed = generation;
981 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
982 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
983 btrfs_read_block_groups(fs_info->tree_root);
985 key.objectid = BTRFS_FS_TREE_OBJECTID;
986 key.type = BTRFS_ROOT_ITEM_KEY;
987 key.offset = (u64)-1;
988 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
990 if (IS_ERR(fs_info->fs_root))
995 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
997 if (fs_info->quota_root)
998 free_extent_buffer(fs_info->quota_root->node);
999 if (fs_info->csum_root)
1000 free_extent_buffer(fs_info->csum_root->node);
1001 if (fs_info->dev_root)
1002 free_extent_buffer(fs_info->dev_root->node);
1003 if (fs_info->extent_root)
1004 free_extent_buffer(fs_info->extent_root->node);
1005 if (fs_info->tree_root)
1006 free_extent_buffer(fs_info->tree_root->node);
1007 if (fs_info->log_root_tree)
1008 free_extent_buffer(fs_info->log_root_tree->node);
1009 if (fs_info->chunk_root)
1010 free_extent_buffer(fs_info->chunk_root->node);
1013 static void free_map_lookup(struct cache_extent *ce)
1015 struct map_lookup *map;
1017 map = container_of(ce, struct map_lookup, ce);
1021 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
1023 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
1025 while (!list_empty(&fs_info->recow_ebs)) {
1026 struct extent_buffer *eb;
1027 eb = list_first_entry(&fs_info->recow_ebs,
1028 struct extent_buffer, recow);
1029 list_del_init(&eb->recow);
1030 free_extent_buffer(eb);
1032 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
1033 extent_io_tree_cleanup(&fs_info->extent_cache);
1034 extent_io_tree_cleanup(&fs_info->free_space_cache);
1035 extent_io_tree_cleanup(&fs_info->block_group_cache);
1036 extent_io_tree_cleanup(&fs_info->pinned_extents);
1037 extent_io_tree_cleanup(&fs_info->pending_del);
1038 extent_io_tree_cleanup(&fs_info->extent_ins);
1041 int btrfs_scan_fs_devices(int fd, const char *path,
1042 struct btrfs_fs_devices **fs_devices,
1043 u64 sb_bytenr, int super_recover,
1051 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1053 seek_ret = lseek(fd, 0, SEEK_END);
1057 dev_size = seek_ret;
1058 lseek(fd, 0, SEEK_SET);
1059 if (sb_bytenr > dev_size) {
1060 fprintf(stderr, "Superblock bytenr is larger than device size\n");
1064 ret = btrfs_scan_one_device(fd, path, fs_devices,
1065 &total_devs, sb_bytenr, super_recover);
1067 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1071 if (!skip_devices && total_devs != 1) {
1072 ret = btrfs_scan_lblkid();
1079 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1081 struct btrfs_super_block *sb = fs_info->super_copy;
1090 nodesize = btrfs_super_nodesize(sb);
1091 leafsize = btrfs_super_leafsize(sb);
1092 sectorsize = btrfs_super_sectorsize(sb);
1093 stripesize = btrfs_super_stripesize(sb);
1095 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1096 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1098 ret = btrfs_read_sys_array(fs_info->chunk_root);
1102 blocksize = btrfs_level_size(fs_info->chunk_root,
1103 btrfs_super_chunk_root_level(sb));
1104 generation = btrfs_super_chunk_root_generation(sb);
1106 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1107 btrfs_super_chunk_root(sb),
1108 blocksize, generation);
1109 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1110 fprintf(stderr, "Couldn't read chunk root\n");
1114 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1115 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1117 fprintf(stderr, "Couldn't read chunk tree\n");
1124 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1126 u64 root_tree_bytenr,
1127 enum btrfs_open_ctree_flags flags)
1129 struct btrfs_fs_info *fs_info;
1130 struct btrfs_super_block *disk_super;
1131 struct btrfs_fs_devices *fs_devices = NULL;
1132 struct extent_buffer *eb;
1137 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1139 /* try to drop all the caches */
1140 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1141 fprintf(stderr, "Warning, could not drop caches\n");
1143 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1145 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1148 if (flags & OPEN_CTREE_RESTORE)
1149 fs_info->on_restoring = 1;
1150 if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1151 fs_info->suppress_check_block_errors = 1;
1153 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1154 (flags & OPEN_CTREE_RECOVER_SUPER),
1155 (flags & OPEN_CTREE_NO_DEVICES));
1159 fs_info->fs_devices = fs_devices;
1160 if (flags & OPEN_CTREE_WRITES)
1165 if (flags & OPEN_CTREE_EXCLUSIVE)
1168 ret = btrfs_open_devices(fs_devices, oflags);
1172 disk_super = fs_info->super_copy;
1173 if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1174 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1175 disk_super, sb_bytenr, 1);
1177 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1179 printk("No valid btrfs found\n");
1183 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1185 ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1186 flags & OPEN_CTREE_WRITES);
1190 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1194 eb = fs_info->chunk_root->node;
1195 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1196 btrfs_header_chunk_tree_uuid(eb),
1199 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1206 btrfs_release_all_roots(fs_info);
1207 btrfs_cleanup_all_caches(fs_info);
1209 btrfs_close_devices(fs_devices);
1211 btrfs_free_fs_info(fs_info);
1215 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1216 u64 sb_bytenr, u64 root_tree_bytenr,
1217 enum btrfs_open_ctree_flags flags)
1220 struct btrfs_fs_info *info;
1221 int oflags = O_CREAT | O_RDWR;
1223 if (!(flags & OPEN_CTREE_WRITES))
1226 fp = open(filename, oflags, 0600);
1228 fprintf (stderr, "Could not open %s\n", filename);
1231 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1237 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1238 enum btrfs_open_ctree_flags flags)
1240 struct btrfs_fs_info *info;
1242 info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1245 return info->fs_root;
1248 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1249 enum btrfs_open_ctree_flags flags)
1251 struct btrfs_fs_info *info;
1252 info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1255 return info->fs_root;
1258 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1261 u8 fsid[BTRFS_FSID_SIZE];
1262 int fsid_is_initialized = 0;
1263 struct btrfs_super_block buf;
1266 int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1270 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1271 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1272 if (ret < sizeof(buf))
1275 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1276 btrfs_super_magic(&buf) != BTRFS_MAGIC)
1279 memcpy(sb, &buf, sizeof(*sb));
1284 * we would like to check all the supers, but that would make
1285 * a btrfs mount succeed after a mkfs from a different FS.
1286 * So, we need to add a special mount option to scan for
1287 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1290 for (i = 0; i < max_super; i++) {
1291 bytenr = btrfs_sb_offset(i);
1292 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1293 if (ret < sizeof(buf))
1296 if (btrfs_super_bytenr(&buf) != bytenr )
1298 /* if magic is NULL, the device was removed */
1299 if (btrfs_super_magic(&buf) == 0 && i == 0)
1301 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1304 if (!fsid_is_initialized) {
1305 memcpy(fsid, buf.fsid, sizeof(fsid));
1306 fsid_is_initialized = 1;
1307 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1309 * the superblocks (the original one and
1310 * its backups) contain data of different
1311 * filesystems -> the super cannot be trusted
1316 if (btrfs_super_generation(&buf) > transid) {
1317 memcpy(sb, &buf, sizeof(*sb));
1318 transid = btrfs_super_generation(&buf);
1322 return transid > 0 ? 0 : -1;
1325 static int write_dev_supers(struct btrfs_root *root,
1326 struct btrfs_super_block *sb,
1327 struct btrfs_device *device)
1333 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1334 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1336 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1337 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1338 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1341 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1342 * zero filled, we can use it directly
1344 ret = pwrite64(device->fd, root->fs_info->super_copy,
1345 BTRFS_SUPER_INFO_SIZE,
1346 root->fs_info->super_bytenr);
1347 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1351 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1352 bytenr = btrfs_sb_offset(i);
1353 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1356 btrfs_set_super_bytenr(sb, bytenr);
1359 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1360 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1361 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1364 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1365 * zero filled, we can use it directly
1367 ret = pwrite64(device->fd, root->fs_info->super_copy,
1368 BTRFS_SUPER_INFO_SIZE, bytenr);
1369 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1375 int write_all_supers(struct btrfs_root *root)
1377 struct list_head *cur;
1378 struct list_head *head = &root->fs_info->fs_devices->devices;
1379 struct btrfs_device *dev;
1380 struct btrfs_super_block *sb;
1381 struct btrfs_dev_item *dev_item;
1385 sb = root->fs_info->super_copy;
1386 dev_item = &sb->dev_item;
1387 list_for_each(cur, head) {
1388 dev = list_entry(cur, struct btrfs_device, dev_list);
1389 if (!dev->writeable)
1392 btrfs_set_stack_device_generation(dev_item, 0);
1393 btrfs_set_stack_device_type(dev_item, dev->type);
1394 btrfs_set_stack_device_id(dev_item, dev->devid);
1395 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1396 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1397 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1398 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1399 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1400 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1401 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1403 flags = btrfs_super_flags(sb);
1404 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1406 ret = write_dev_supers(root, sb, dev);
1412 int write_ctree_super(struct btrfs_trans_handle *trans,
1413 struct btrfs_root *root)
1416 struct btrfs_root *tree_root = root->fs_info->tree_root;
1417 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1419 if (root->fs_info->readonly)
1422 btrfs_set_super_generation(root->fs_info->super_copy,
1424 btrfs_set_super_root(root->fs_info->super_copy,
1425 tree_root->node->start);
1426 btrfs_set_super_root_level(root->fs_info->super_copy,
1427 btrfs_header_level(tree_root->node));
1428 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1429 chunk_root->node->start);
1430 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1431 btrfs_header_level(chunk_root->node));
1432 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1433 btrfs_header_generation(chunk_root->node));
1435 ret = write_all_supers(root);
1437 fprintf(stderr, "failed to write new super block err %d\n", ret);
1441 int close_ctree(struct btrfs_root *root)
1444 struct btrfs_trans_handle *trans;
1445 struct btrfs_fs_info *fs_info = root->fs_info;
1447 if (fs_info->last_trans_committed !=
1448 fs_info->generation) {
1449 trans = btrfs_start_transaction(root, 1);
1450 btrfs_commit_transaction(trans, root);
1451 trans = btrfs_start_transaction(root, 1);
1452 ret = commit_tree_roots(trans, fs_info);
1454 ret = __commit_transaction(trans, root);
1456 write_ctree_super(trans, root);
1457 btrfs_free_transaction(root, trans);
1459 btrfs_free_block_groups(fs_info);
1461 free_fs_roots_tree(&fs_info->fs_root_tree);
1463 btrfs_release_all_roots(fs_info);
1464 btrfs_close_devices(fs_info->fs_devices);
1465 btrfs_cleanup_all_caches(fs_info);
1466 btrfs_free_fs_info(fs_info);
1470 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1471 struct extent_buffer *eb)
1473 return clear_extent_buffer_dirty(eb);
1476 int wait_on_tree_block_writeback(struct btrfs_root *root,
1477 struct extent_buffer *eb)
1482 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1484 set_extent_buffer_dirty(eb);
1487 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1491 ret = extent_buffer_uptodate(buf);
1495 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1499 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1501 return set_extent_buffer_uptodate(eb);