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 "kerncompat.h"
26 #include "radix-tree.h"
30 #include "transaction.h"
33 #include "print-tree.h"
34 #include "rbtree-utils.h"
36 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
39 struct btrfs_fs_devices *fs_devices;
42 if (buf->start != btrfs_header_bytenr(buf)) {
43 printk("Check tree block failed, want=%Lu, have=%Lu\n",
44 buf->start, btrfs_header_bytenr(buf));
48 fs_devices = root->fs_info->fs_devices;
50 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
56 fs_devices = fs_devices->seed;
61 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
63 return crc32c(seed, data, len);
66 void btrfs_csum_final(u32 crc, char *result)
68 *(__le32 *)result = ~cpu_to_le32(crc);
71 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
72 int verify, int silent)
78 result = malloc(csum_size * sizeof(char));
82 len = buf->len - BTRFS_CSUM_SIZE;
83 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
84 btrfs_csum_final(crc, result);
87 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
89 printk("checksum verify failed on %llu found %08X wanted %08X\n",
90 (unsigned long long)buf->start,
92 *((u32*)(char *)buf->data));
97 write_extent_buffer(buf, result, 0, csum_size);
103 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
105 return __csum_tree_block_size(buf, csum_size, verify, 0);
108 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
110 return __csum_tree_block_size(buf, csum_size, 1, 1);
113 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
117 btrfs_super_csum_size(root->fs_info->super_copy);
118 return csum_tree_block_size(buf, csum_size, verify);
121 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
122 u64 bytenr, u32 blocksize)
124 return find_extent_buffer(&root->fs_info->extent_cache,
128 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
129 u64 bytenr, u32 blocksize)
131 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
135 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
138 struct extent_buffer *eb;
140 struct btrfs_multi_bio *multi = NULL;
141 struct btrfs_device *device;
143 eb = btrfs_find_tree_block(root, bytenr, blocksize);
144 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
145 !btrfs_map_block(&root->fs_info->mapping_tree, READ,
146 bytenr, &length, &multi, 0, NULL)) {
147 device = multi->stripes[0].dev;
149 blocksize = min(blocksize, (u32)(64 * 1024));
150 readahead(device->fd, multi->stripes[0].physical, blocksize);
153 free_extent_buffer(eb);
157 static int verify_parent_transid(struct extent_io_tree *io_tree,
158 struct extent_buffer *eb, u64 parent_transid,
163 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
166 if (extent_buffer_uptodate(eb) &&
167 btrfs_header_generation(eb) == parent_transid) {
171 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
172 (unsigned long long)eb->start,
173 (unsigned long long)parent_transid,
174 (unsigned long long)btrfs_header_generation(eb));
176 eb->flags |= EXTENT_BAD_TRANSID;
177 printk("Ignoring transid failure\n");
183 clear_extent_buffer_uptodate(io_tree, eb);
189 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
191 unsigned long offset = 0;
192 struct btrfs_multi_bio *multi = NULL;
193 struct btrfs_device *device;
196 unsigned long bytes_left = eb->len;
199 read_len = bytes_left;
202 if (!info->on_restoring &&
203 eb->start != BTRFS_SUPER_INFO_OFFSET) {
204 ret = btrfs_map_block(&info->mapping_tree, READ,
205 eb->start + offset, &read_len, &multi,
208 printk("Couldn't map the block %Lu\n", eb->start + offset);
212 device = multi->stripes[0].dev;
214 if (device->fd == 0) {
221 eb->dev_bytenr = multi->stripes[0].physical;
225 /* special case for restore metadump */
226 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
227 if (device->devid == 1)
232 eb->dev_bytenr = eb->start;
236 if (read_len > bytes_left)
237 read_len = bytes_left;
239 ret = read_extent_from_disk(eb, offset, read_len);
243 bytes_left -= read_len;
248 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
249 u32 blocksize, u64 parent_transid)
252 struct extent_buffer *eb;
253 u64 best_transid = 0;
259 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
261 return ERR_PTR(-ENOMEM);
263 if (btrfs_buffer_uptodate(eb, parent_transid))
267 ret = read_whole_eb(root->fs_info, eb, mirror_num);
268 if (ret == 0 && check_tree_block(root, eb) == 0 &&
269 csum_tree_block(root, eb, 1) == 0 &&
270 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
272 if (eb->flags & EXTENT_BAD_TRANSID &&
273 list_empty(&eb->recow)) {
274 list_add_tail(&eb->recow,
275 &root->fs_info->recow_ebs);
278 btrfs_set_buffer_uptodate(eb);
282 if (check_tree_block(root, eb))
283 printk("read block failed check_tree_block\n");
285 printk("Csum didn't match\n");
289 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
291 if (num_copies == 1) {
295 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
296 best_transid = btrfs_header_generation(eb);
297 good_mirror = mirror_num;
300 if (mirror_num > num_copies) {
301 mirror_num = good_mirror;
306 free_extent_buffer(eb);
310 int write_and_map_eb(struct btrfs_trans_handle *trans,
311 struct btrfs_root *root,
312 struct extent_buffer *eb)
317 u64 *raid_map = NULL;
318 struct btrfs_multi_bio *multi = NULL;
322 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
323 eb->start, &length, &multi, 0, &raid_map);
326 ret = write_raid56_with_parity(root->fs_info, eb, multi,
329 } else while (dev_nr < multi->num_stripes) {
331 eb->fd = multi->stripes[dev_nr].dev->fd;
332 eb->dev_bytenr = multi->stripes[dev_nr].physical;
333 multi->stripes[dev_nr].dev->total_ios++;
335 ret = write_extent_to_disk(eb);
342 static int write_tree_block(struct btrfs_trans_handle *trans,
343 struct btrfs_root *root,
344 struct extent_buffer *eb)
346 if (check_tree_block(root, eb))
349 if (!btrfs_buffer_uptodate(eb, trans->transid))
352 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
353 csum_tree_block(root, eb, 0);
355 return write_and_map_eb(trans, root, eb);
358 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
359 u32 stripesize, struct btrfs_root *root,
360 struct btrfs_fs_info *fs_info, u64 objectid)
363 root->commit_root = NULL;
364 root->sectorsize = sectorsize;
365 root->nodesize = nodesize;
366 root->leafsize = leafsize;
367 root->stripesize = stripesize;
369 root->track_dirty = 0;
371 root->fs_info = fs_info;
372 root->objectid = objectid;
373 root->last_trans = 0;
374 root->highest_inode = 0;
375 root->last_inode_alloc = 0;
377 INIT_LIST_HEAD(&root->dirty_list);
378 INIT_LIST_HEAD(&root->orphan_data_extents);
379 memset(&root->root_key, 0, sizeof(root->root_key));
380 memset(&root->root_item, 0, sizeof(root->root_item));
381 root->root_key.objectid = objectid;
385 static int update_cowonly_root(struct btrfs_trans_handle *trans,
386 struct btrfs_root *root)
390 struct btrfs_root *tree_root = root->fs_info->tree_root;
392 btrfs_write_dirty_block_groups(trans, root);
394 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
395 if (old_root_bytenr == root->node->start)
397 btrfs_set_root_bytenr(&root->root_item,
399 btrfs_set_root_generation(&root->root_item,
401 root->root_item.level = btrfs_header_level(root->node);
402 ret = btrfs_update_root(trans, tree_root,
406 btrfs_write_dirty_block_groups(trans, root);
411 static int commit_tree_roots(struct btrfs_trans_handle *trans,
412 struct btrfs_fs_info *fs_info)
414 struct btrfs_root *root;
415 struct list_head *next;
416 struct extent_buffer *eb;
419 if (fs_info->readonly)
422 eb = fs_info->tree_root->node;
423 extent_buffer_get(eb);
424 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
425 free_extent_buffer(eb);
429 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
430 next = fs_info->dirty_cowonly_roots.next;
432 root = list_entry(next, struct btrfs_root, dirty_list);
433 update_cowonly_root(trans, root);
434 free_extent_buffer(root->commit_root);
435 root->commit_root = NULL;
441 static int __commit_transaction(struct btrfs_trans_handle *trans,
442 struct btrfs_root *root)
446 struct extent_buffer *eb;
447 struct extent_io_tree *tree = &root->fs_info->extent_cache;
451 ret = find_first_extent_bit(tree, 0, &start, &end,
455 while(start <= end) {
456 eb = find_first_extent_buffer(tree, start);
457 BUG_ON(!eb || eb->start != start);
458 ret = write_tree_block(trans, root, eb);
461 clear_extent_buffer_dirty(eb);
462 free_extent_buffer(eb);
468 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
469 struct btrfs_root *root)
471 u64 transid = trans->transid;
473 struct btrfs_fs_info *fs_info = root->fs_info;
475 if (root->commit_root == root->node)
477 if (root == root->fs_info->tree_root)
480 free_extent_buffer(root->commit_root);
481 root->commit_root = NULL;
483 btrfs_set_root_bytenr(&root->root_item, root->node->start);
484 btrfs_set_root_generation(&root->root_item, trans->transid);
485 root->root_item.level = btrfs_header_level(root->node);
486 ret = btrfs_update_root(trans, root->fs_info->tree_root,
487 &root->root_key, &root->root_item);
490 ret = commit_tree_roots(trans, fs_info);
492 ret = __commit_transaction(trans, root);
494 write_ctree_super(trans, root);
495 btrfs_finish_extent_commit(trans, fs_info->extent_root,
496 &fs_info->pinned_extents);
497 btrfs_free_transaction(root, trans);
498 free_extent_buffer(root->commit_root);
499 root->commit_root = NULL;
500 fs_info->running_transaction = NULL;
501 fs_info->last_trans_committed = transid;
505 static int find_and_setup_root(struct btrfs_root *tree_root,
506 struct btrfs_fs_info *fs_info,
507 u64 objectid, struct btrfs_root *root)
513 __setup_root(tree_root->nodesize, tree_root->leafsize,
514 tree_root->sectorsize, tree_root->stripesize,
515 root, fs_info, objectid);
516 ret = btrfs_find_last_root(tree_root, objectid,
517 &root->root_item, &root->root_key);
521 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
522 generation = btrfs_root_generation(&root->root_item);
523 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
524 blocksize, generation);
525 if (!extent_buffer_uptodate(root->node))
531 static int find_and_setup_log_root(struct btrfs_root *tree_root,
532 struct btrfs_fs_info *fs_info,
533 struct btrfs_super_block *disk_super)
536 u64 blocknr = btrfs_super_log_root(disk_super);
537 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
547 blocksize = btrfs_level_size(tree_root,
548 btrfs_super_log_root_level(disk_super));
550 __setup_root(tree_root->nodesize, tree_root->leafsize,
551 tree_root->sectorsize, tree_root->stripesize,
552 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
554 log_root->node = read_tree_block(tree_root, blocknr,
556 btrfs_super_generation(disk_super) + 1);
558 fs_info->log_root_tree = log_root;
560 if (!extent_buffer_uptodate(log_root->node)) {
561 free_extent_buffer(log_root->node);
563 fs_info->log_root_tree = NULL;
570 int btrfs_free_fs_root(struct btrfs_root *root)
573 free_extent_buffer(root->node);
574 if (root->commit_root)
575 free_extent_buffer(root->commit_root);
580 static void __free_fs_root(struct rb_node *node)
582 struct btrfs_root *root;
584 root = container_of(node, struct btrfs_root, rb_node);
585 btrfs_free_fs_root(root);
588 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
590 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
591 struct btrfs_key *location)
593 struct btrfs_root *root;
594 struct btrfs_root *tree_root = fs_info->tree_root;
595 struct btrfs_path *path;
596 struct extent_buffer *l;
601 root = malloc(sizeof(*root));
603 return ERR_PTR(-ENOMEM);
604 memset(root, 0, sizeof(*root));
605 if (location->offset == (u64)-1) {
606 ret = find_and_setup_root(tree_root, fs_info,
607 location->objectid, root);
615 __setup_root(tree_root->nodesize, tree_root->leafsize,
616 tree_root->sectorsize, tree_root->stripesize,
617 root, fs_info, location->objectid);
619 path = btrfs_alloc_path();
621 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
628 read_extent_buffer(l, &root->root_item,
629 btrfs_item_ptr_offset(l, path->slots[0]),
630 sizeof(root->root_item));
631 memcpy(&root->root_key, location, sizeof(*location));
634 btrfs_free_path(path);
639 generation = btrfs_root_generation(&root->root_item);
640 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
641 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
642 blocksize, generation);
643 if (!extent_buffer_uptodate(root->node)) {
645 return ERR_PTR(-EIO);
652 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
655 u64 objectid = *((u64 *)data);
656 struct btrfs_root *root;
658 root = rb_entry(node, struct btrfs_root, rb_node);
659 if (objectid > root->objectid)
661 else if (objectid < root->objectid)
667 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
668 struct rb_node *node2)
670 struct btrfs_root *root;
672 root = rb_entry(node2, struct btrfs_root, rb_node);
673 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
676 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
677 struct btrfs_key *location)
679 struct btrfs_root *root;
680 struct rb_node *node;
682 u64 objectid = location->objectid;
684 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
685 return fs_info->tree_root;
686 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
687 return fs_info->extent_root;
688 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
689 return fs_info->chunk_root;
690 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
691 return fs_info->dev_root;
692 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
693 return fs_info->csum_root;
694 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
695 return fs_info->quota_root;
697 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
698 location->offset != (u64)-1);
700 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
701 btrfs_fs_roots_compare_objectids, NULL);
703 return container_of(node, struct btrfs_root, rb_node);
705 root = btrfs_read_fs_root_no_cache(fs_info, location);
709 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
710 btrfs_fs_roots_compare_roots);
715 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
717 free(fs_info->tree_root);
718 free(fs_info->extent_root);
719 free(fs_info->chunk_root);
720 free(fs_info->dev_root);
721 free(fs_info->csum_root);
722 free(fs_info->quota_root);
723 free(fs_info->super_copy);
724 free(fs_info->log_root_tree);
728 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
730 struct btrfs_fs_info *fs_info;
732 fs_info = malloc(sizeof(struct btrfs_fs_info));
736 memset(fs_info, 0, sizeof(struct btrfs_fs_info));
738 fs_info->tree_root = malloc(sizeof(struct btrfs_root));
739 fs_info->extent_root = malloc(sizeof(struct btrfs_root));
740 fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
741 fs_info->dev_root = malloc(sizeof(struct btrfs_root));
742 fs_info->csum_root = malloc(sizeof(struct btrfs_root));
743 fs_info->quota_root = malloc(sizeof(struct btrfs_root));
744 fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
746 if (!fs_info->tree_root || !fs_info->extent_root ||
747 !fs_info->chunk_root || !fs_info->dev_root ||
748 !fs_info->csum_root || !fs_info->quota_root ||
749 !fs_info->super_copy)
752 memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
753 memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
754 memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
755 memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
756 memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
757 memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
758 memset(fs_info->quota_root, 0, sizeof(struct btrfs_root));
760 extent_io_tree_init(&fs_info->extent_cache);
761 extent_io_tree_init(&fs_info->free_space_cache);
762 extent_io_tree_init(&fs_info->block_group_cache);
763 extent_io_tree_init(&fs_info->pinned_extents);
764 extent_io_tree_init(&fs_info->pending_del);
765 extent_io_tree_init(&fs_info->extent_ins);
766 fs_info->excluded_extents = NULL;
768 fs_info->fs_root_tree = RB_ROOT;
769 cache_tree_init(&fs_info->mapping_tree.cache_tree);
771 mutex_init(&fs_info->fs_mutex);
772 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
773 INIT_LIST_HEAD(&fs_info->space_info);
774 INIT_LIST_HEAD(&fs_info->recow_ebs);
777 fs_info->readonly = 1;
779 fs_info->super_bytenr = sb_bytenr;
780 fs_info->data_alloc_profile = (u64)-1;
781 fs_info->metadata_alloc_profile = (u64)-1;
782 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
785 btrfs_free_fs_info(fs_info);
789 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
793 features = btrfs_super_incompat_flags(sb) &
794 ~BTRFS_FEATURE_INCOMPAT_SUPP;
796 printk("couldn't open because of unsupported "
797 "option features (%Lx).\n",
798 (unsigned long long)features);
802 features = btrfs_super_incompat_flags(sb);
803 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
804 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
805 btrfs_set_super_incompat_flags(sb, features);
808 features = btrfs_super_compat_ro_flags(sb) &
809 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
810 if (writable && features) {
811 printk("couldn't open RDWR because of unsupported "
812 "option features (%Lx).\n",
813 (unsigned long long)features);
819 static int find_best_backup_root(struct btrfs_super_block *super)
821 struct btrfs_root_backup *backup;
822 u64 orig_gen = btrfs_super_generation(super);
827 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
828 backup = super->super_roots + i;
829 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
830 btrfs_backup_tree_root_gen(backup) > gen) {
832 gen = btrfs_backup_tree_root_gen(backup);
838 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
839 enum btrfs_open_ctree_flags flags,
840 struct btrfs_root *info_root,
841 u64 objectid, char *str)
843 struct btrfs_super_block *sb = fs_info->super_copy;
844 struct btrfs_root *root = fs_info->tree_root;
845 u32 leafsize = btrfs_super_leafsize(sb);
848 ret = find_and_setup_root(root, fs_info, objectid, info_root);
850 printk("Couldn't setup %s tree\n", str);
851 if (!(flags & OPEN_CTREE_PARTIAL))
854 * Need a blank node here just so we don't screw up in the
855 * million of places that assume a root has a valid ->node
858 btrfs_find_create_tree_block(info_root, 0, leafsize);
859 if (!info_root->node)
861 clear_extent_buffer_uptodate(NULL, info_root->node);
867 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
868 enum btrfs_open_ctree_flags flags)
870 struct btrfs_super_block *sb = fs_info->super_copy;
871 struct btrfs_root *root;
872 struct btrfs_key key;
881 nodesize = btrfs_super_nodesize(sb);
882 leafsize = btrfs_super_leafsize(sb);
883 sectorsize = btrfs_super_sectorsize(sb);
884 stripesize = btrfs_super_stripesize(sb);
886 root = fs_info->tree_root;
887 __setup_root(nodesize, leafsize, sectorsize, stripesize,
888 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
889 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
890 generation = btrfs_super_generation(sb);
892 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
893 root_tree_bytenr = btrfs_super_root(sb);
894 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
895 struct btrfs_root_backup *backup;
896 int index = find_best_backup_root(sb);
897 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
898 fprintf(stderr, "Invalid backup root number\n");
901 backup = fs_info->super_copy->super_roots + index;
902 root_tree_bytenr = btrfs_backup_tree_root(backup);
903 generation = btrfs_backup_tree_root_gen(backup);
906 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
908 if (!extent_buffer_uptodate(root->node)) {
909 fprintf(stderr, "Couldn't read tree root\n");
913 ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
914 BTRFS_EXTENT_TREE_OBJECTID, "extent");
917 fs_info->extent_root->track_dirty = 1;
919 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
922 printk("Couldn't setup device tree\n");
925 fs_info->dev_root->track_dirty = 1;
927 ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
928 BTRFS_CSUM_TREE_OBJECTID, "csum");
931 fs_info->csum_root->track_dirty = 1;
933 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
934 fs_info->quota_root);
936 fs_info->quota_enabled = 1;
938 ret = find_and_setup_log_root(root, fs_info, sb);
940 printk("Couldn't setup log root tree\n");
941 if (!(flags & OPEN_CTREE_PARTIAL))
945 fs_info->generation = generation;
946 fs_info->last_trans_committed = generation;
947 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
948 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
949 btrfs_read_block_groups(fs_info->tree_root);
951 key.objectid = BTRFS_FS_TREE_OBJECTID;
952 key.type = BTRFS_ROOT_ITEM_KEY;
953 key.offset = (u64)-1;
954 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
956 if (IS_ERR(fs_info->fs_root))
961 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
963 if (fs_info->quota_root)
964 free_extent_buffer(fs_info->quota_root->node);
965 if (fs_info->csum_root)
966 free_extent_buffer(fs_info->csum_root->node);
967 if (fs_info->dev_root)
968 free_extent_buffer(fs_info->dev_root->node);
969 if (fs_info->extent_root)
970 free_extent_buffer(fs_info->extent_root->node);
971 if (fs_info->tree_root)
972 free_extent_buffer(fs_info->tree_root->node);
973 if (fs_info->log_root_tree)
974 free_extent_buffer(fs_info->log_root_tree->node);
975 if (fs_info->chunk_root)
976 free_extent_buffer(fs_info->chunk_root->node);
979 static void free_map_lookup(struct cache_extent *ce)
981 struct map_lookup *map;
983 map = container_of(ce, struct map_lookup, ce);
987 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
989 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
991 while (!list_empty(&fs_info->recow_ebs)) {
992 struct extent_buffer *eb;
993 eb = list_first_entry(&fs_info->recow_ebs,
994 struct extent_buffer, recow);
995 list_del_init(&eb->recow);
996 free_extent_buffer(eb);
998 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
999 extent_io_tree_cleanup(&fs_info->extent_cache);
1000 extent_io_tree_cleanup(&fs_info->free_space_cache);
1001 extent_io_tree_cleanup(&fs_info->block_group_cache);
1002 extent_io_tree_cleanup(&fs_info->pinned_extents);
1003 extent_io_tree_cleanup(&fs_info->pending_del);
1004 extent_io_tree_cleanup(&fs_info->extent_ins);
1007 int btrfs_scan_fs_devices(int fd, const char *path,
1008 struct btrfs_fs_devices **fs_devices,
1009 u64 sb_bytenr, int super_recover)
1016 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1018 seek_ret = lseek(fd, 0, SEEK_END);
1022 dev_size = seek_ret;
1023 lseek(fd, 0, SEEK_SET);
1024 if (sb_bytenr > dev_size) {
1025 fprintf(stderr, "Superblock bytenr is larger than device size\n");
1029 ret = btrfs_scan_one_device(fd, path, fs_devices,
1030 &total_devs, sb_bytenr, super_recover);
1032 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1036 if (total_devs != 1) {
1037 ret = btrfs_scan_lblkid();
1044 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1046 struct btrfs_super_block *sb = fs_info->super_copy;
1055 nodesize = btrfs_super_nodesize(sb);
1056 leafsize = btrfs_super_leafsize(sb);
1057 sectorsize = btrfs_super_sectorsize(sb);
1058 stripesize = btrfs_super_stripesize(sb);
1060 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1061 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1063 ret = btrfs_read_sys_array(fs_info->chunk_root);
1067 blocksize = btrfs_level_size(fs_info->chunk_root,
1068 btrfs_super_chunk_root_level(sb));
1069 generation = btrfs_super_chunk_root_generation(sb);
1071 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1072 btrfs_super_chunk_root(sb),
1073 blocksize, generation);
1074 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1075 fprintf(stderr, "Couldn't read chunk root\n");
1079 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1080 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1082 fprintf(stderr, "Couldn't read chunk tree\n");
1089 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1091 u64 root_tree_bytenr,
1092 enum btrfs_open_ctree_flags flags)
1094 struct btrfs_fs_info *fs_info;
1095 struct btrfs_super_block *disk_super;
1096 struct btrfs_fs_devices *fs_devices = NULL;
1097 struct extent_buffer *eb;
1102 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1104 /* try to drop all the caches */
1105 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1106 fprintf(stderr, "Warning, could not drop caches\n");
1108 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1110 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1113 if (flags & OPEN_CTREE_RESTORE)
1114 fs_info->on_restoring = 1;
1116 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1117 (flags & OPEN_CTREE_RECOVER_SUPER));
1121 fs_info->fs_devices = fs_devices;
1122 if (flags & OPEN_CTREE_WRITES)
1127 if (flags & OPEN_CTREE_EXCLUSIVE)
1130 ret = btrfs_open_devices(fs_devices, oflags);
1134 disk_super = fs_info->super_copy;
1135 if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1136 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1137 disk_super, sb_bytenr, 1);
1139 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1141 printk("No valid btrfs found\n");
1145 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1147 ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1148 flags & OPEN_CTREE_WRITES);
1152 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1156 eb = fs_info->chunk_root->node;
1157 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1158 btrfs_header_chunk_tree_uuid(eb),
1161 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1168 btrfs_release_all_roots(fs_info);
1169 btrfs_cleanup_all_caches(fs_info);
1171 btrfs_close_devices(fs_devices);
1173 btrfs_free_fs_info(fs_info);
1177 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1178 u64 sb_bytenr, u64 root_tree_bytenr,
1179 enum btrfs_open_ctree_flags flags)
1182 struct btrfs_fs_info *info;
1183 int oflags = O_CREAT | O_RDWR;
1185 if (!(flags & OPEN_CTREE_WRITES))
1188 fp = open(filename, oflags, 0600);
1190 fprintf (stderr, "Could not open %s\n", filename);
1193 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1199 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1200 enum btrfs_open_ctree_flags flags)
1202 struct btrfs_fs_info *info;
1204 info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1207 return info->fs_root;
1210 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1211 enum btrfs_open_ctree_flags flags)
1213 struct btrfs_fs_info *info;
1214 info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1217 return info->fs_root;
1220 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1223 u8 fsid[BTRFS_FSID_SIZE];
1224 int fsid_is_initialized = 0;
1225 struct btrfs_super_block buf;
1228 int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1232 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1233 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1234 if (ret < sizeof(buf))
1237 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1238 btrfs_super_magic(&buf) != BTRFS_MAGIC)
1241 memcpy(sb, &buf, sizeof(*sb));
1246 * we would like to check all the supers, but that would make
1247 * a btrfs mount succeed after a mkfs from a different FS.
1248 * So, we need to add a special mount option to scan for
1249 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1252 for (i = 0; i < max_super; i++) {
1253 bytenr = btrfs_sb_offset(i);
1254 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1255 if (ret < sizeof(buf))
1258 if (btrfs_super_bytenr(&buf) != bytenr )
1260 /* if magic is NULL, the device was removed */
1261 if (btrfs_super_magic(&buf) == 0 && i == 0)
1263 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1266 if (!fsid_is_initialized) {
1267 memcpy(fsid, buf.fsid, sizeof(fsid));
1268 fsid_is_initialized = 1;
1269 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1271 * the superblocks (the original one and
1272 * its backups) contain data of different
1273 * filesystems -> the super cannot be trusted
1278 if (btrfs_super_generation(&buf) > transid) {
1279 memcpy(sb, &buf, sizeof(*sb));
1280 transid = btrfs_super_generation(&buf);
1284 return transid > 0 ? 0 : -1;
1287 static int write_dev_supers(struct btrfs_root *root,
1288 struct btrfs_super_block *sb,
1289 struct btrfs_device *device)
1295 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1296 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1298 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1299 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1300 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1303 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1304 * zero filled, we can use it directly
1306 ret = pwrite64(device->fd, root->fs_info->super_copy,
1307 BTRFS_SUPER_INFO_SIZE,
1308 root->fs_info->super_bytenr);
1309 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1313 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1314 bytenr = btrfs_sb_offset(i);
1315 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1318 btrfs_set_super_bytenr(sb, bytenr);
1321 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1322 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1323 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1326 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1327 * zero filled, we can use it directly
1329 ret = pwrite64(device->fd, root->fs_info->super_copy,
1330 BTRFS_SUPER_INFO_SIZE, bytenr);
1331 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1337 int write_all_supers(struct btrfs_root *root)
1339 struct list_head *cur;
1340 struct list_head *head = &root->fs_info->fs_devices->devices;
1341 struct btrfs_device *dev;
1342 struct btrfs_super_block *sb;
1343 struct btrfs_dev_item *dev_item;
1347 sb = root->fs_info->super_copy;
1348 dev_item = &sb->dev_item;
1349 list_for_each(cur, head) {
1350 dev = list_entry(cur, struct btrfs_device, dev_list);
1351 if (!dev->writeable)
1354 btrfs_set_stack_device_generation(dev_item, 0);
1355 btrfs_set_stack_device_type(dev_item, dev->type);
1356 btrfs_set_stack_device_id(dev_item, dev->devid);
1357 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1358 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1359 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1360 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1361 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1362 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1363 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1365 flags = btrfs_super_flags(sb);
1366 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1368 ret = write_dev_supers(root, sb, dev);
1374 int write_ctree_super(struct btrfs_trans_handle *trans,
1375 struct btrfs_root *root)
1378 struct btrfs_root *tree_root = root->fs_info->tree_root;
1379 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1381 if (root->fs_info->readonly)
1384 btrfs_set_super_generation(root->fs_info->super_copy,
1386 btrfs_set_super_root(root->fs_info->super_copy,
1387 tree_root->node->start);
1388 btrfs_set_super_root_level(root->fs_info->super_copy,
1389 btrfs_header_level(tree_root->node));
1390 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1391 chunk_root->node->start);
1392 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1393 btrfs_header_level(chunk_root->node));
1394 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1395 btrfs_header_generation(chunk_root->node));
1397 ret = write_all_supers(root);
1399 fprintf(stderr, "failed to write new super block err %d\n", ret);
1403 int close_ctree(struct btrfs_root *root)
1406 struct btrfs_trans_handle *trans;
1407 struct btrfs_fs_info *fs_info = root->fs_info;
1409 if (fs_info->last_trans_committed !=
1410 fs_info->generation) {
1411 trans = btrfs_start_transaction(root, 1);
1412 btrfs_commit_transaction(trans, root);
1413 trans = btrfs_start_transaction(root, 1);
1414 ret = commit_tree_roots(trans, fs_info);
1416 ret = __commit_transaction(trans, root);
1418 write_ctree_super(trans, root);
1419 btrfs_free_transaction(root, trans);
1421 btrfs_free_block_groups(fs_info);
1423 free_fs_roots_tree(&fs_info->fs_root_tree);
1425 btrfs_release_all_roots(fs_info);
1426 btrfs_close_devices(fs_info->fs_devices);
1427 btrfs_cleanup_all_caches(fs_info);
1428 btrfs_free_fs_info(fs_info);
1432 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1433 struct extent_buffer *eb)
1435 return clear_extent_buffer_dirty(eb);
1438 int wait_on_tree_block_writeback(struct btrfs_root *root,
1439 struct extent_buffer *eb)
1444 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1446 set_extent_buffer_dirty(eb);
1449 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1453 ret = extent_buffer_uptodate(buf);
1457 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1461 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1463 return set_extent_buffer_uptodate(eb);