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 (root->fs_info->ignore_fsid_mismatch ||
53 !memcmp_extent_buffer(buf, fs_devices->fsid,
59 fs_devices = fs_devices->seed;
64 static void print_tree_block_error(struct btrfs_root *root,
65 struct extent_buffer *eb,
68 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
69 char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
70 u8 buf[BTRFS_UUID_SIZE];
74 read_extent_buffer(eb, buf, btrfs_header_fsid(),
76 uuid_unparse(buf, found_uuid);
77 uuid_unparse(root->fs_info->fsid, fs_uuid);
78 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
81 case BTRFS_BAD_BYTENR:
82 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
83 eb->start, btrfs_header_bytenr(eb));
88 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
90 return crc32c(seed, data, len);
93 void btrfs_csum_final(u32 crc, char *result)
95 *(__le32 *)result = ~cpu_to_le32(crc);
98 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
99 int verify, int silent)
105 result = malloc(csum_size * sizeof(char));
109 len = buf->len - BTRFS_CSUM_SIZE;
110 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
111 btrfs_csum_final(crc, result);
114 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
116 printk("checksum verify failed on %llu found %08X wanted %08X\n",
117 (unsigned long long)buf->start,
119 *((u32*)(char *)buf->data));
124 write_extent_buffer(buf, result, 0, csum_size);
130 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
132 return __csum_tree_block_size(buf, csum_size, verify, 0);
135 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
137 return __csum_tree_block_size(buf, csum_size, 1, 1);
140 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
144 btrfs_super_csum_size(root->fs_info->super_copy);
145 if (verify && root->fs_info->suppress_check_block_errors)
146 return verify_tree_block_csum_silent(buf, csum_size);
147 return csum_tree_block_size(buf, csum_size, verify);
150 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
151 u64 bytenr, u32 blocksize)
153 return find_extent_buffer(&root->fs_info->extent_cache,
157 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
158 u64 bytenr, u32 blocksize)
160 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
164 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
167 struct extent_buffer *eb;
169 struct btrfs_multi_bio *multi = NULL;
170 struct btrfs_device *device;
172 eb = btrfs_find_tree_block(root, bytenr, blocksize);
173 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
174 !btrfs_map_block(&root->fs_info->mapping_tree, READ,
175 bytenr, &length, &multi, 0, NULL)) {
176 device = multi->stripes[0].dev;
178 blocksize = min(blocksize, (u32)(64 * 1024));
179 readahead(device->fd, multi->stripes[0].physical, blocksize);
182 free_extent_buffer(eb);
186 static int verify_parent_transid(struct extent_io_tree *io_tree,
187 struct extent_buffer *eb, u64 parent_transid,
192 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
195 if (extent_buffer_uptodate(eb) &&
196 btrfs_header_generation(eb) == parent_transid) {
200 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
201 (unsigned long long)eb->start,
202 (unsigned long long)parent_transid,
203 (unsigned long long)btrfs_header_generation(eb));
205 eb->flags |= EXTENT_BAD_TRANSID;
206 printk("Ignoring transid failure\n");
212 clear_extent_buffer_uptodate(io_tree, eb);
218 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
220 unsigned long offset = 0;
221 struct btrfs_multi_bio *multi = NULL;
222 struct btrfs_device *device;
225 unsigned long bytes_left = eb->len;
228 read_len = bytes_left;
231 if (!info->on_restoring &&
232 eb->start != BTRFS_SUPER_INFO_OFFSET) {
233 ret = btrfs_map_block(&info->mapping_tree, READ,
234 eb->start + offset, &read_len, &multi,
237 printk("Couldn't map the block %Lu\n", eb->start + offset);
241 device = multi->stripes[0].dev;
243 if (device->fd == 0) {
250 eb->dev_bytenr = multi->stripes[0].physical;
254 /* special case for restore metadump */
255 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
256 if (device->devid == 1)
261 eb->dev_bytenr = eb->start;
265 if (read_len > bytes_left)
266 read_len = bytes_left;
268 ret = read_extent_from_disk(eb, offset, read_len);
272 bytes_left -= read_len;
277 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
278 u32 blocksize, u64 parent_transid)
281 struct extent_buffer *eb;
282 u64 best_transid = 0;
288 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
290 return ERR_PTR(-ENOMEM);
292 if (btrfs_buffer_uptodate(eb, parent_transid))
296 ret = read_whole_eb(root->fs_info, eb, mirror_num);
297 if (ret == 0 && csum_tree_block(root, eb, 1) == 0 &&
298 check_tree_block(root, eb) == 0 &&
299 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
301 if (eb->flags & EXTENT_BAD_TRANSID &&
302 list_empty(&eb->recow)) {
303 list_add_tail(&eb->recow,
304 &root->fs_info->recow_ebs);
307 btrfs_set_buffer_uptodate(eb);
311 if (check_tree_block(root, eb)) {
312 if (!root->fs_info->suppress_check_block_errors)
313 print_tree_block_error(root, eb,
314 check_tree_block(root, eb));
316 if (!root->fs_info->suppress_check_block_errors)
317 fprintf(stderr, "Csum didn't match\n");
322 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
324 if (num_copies == 1) {
328 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
329 best_transid = btrfs_header_generation(eb);
330 good_mirror = mirror_num;
333 if (mirror_num > num_copies) {
334 mirror_num = good_mirror;
339 free_extent_buffer(eb);
343 int write_and_map_eb(struct btrfs_trans_handle *trans,
344 struct btrfs_root *root,
345 struct extent_buffer *eb)
350 u64 *raid_map = NULL;
351 struct btrfs_multi_bio *multi = NULL;
355 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
356 eb->start, &length, &multi, 0, &raid_map);
359 ret = write_raid56_with_parity(root->fs_info, eb, multi,
362 } else while (dev_nr < multi->num_stripes) {
364 eb->fd = multi->stripes[dev_nr].dev->fd;
365 eb->dev_bytenr = multi->stripes[dev_nr].physical;
366 multi->stripes[dev_nr].dev->total_ios++;
368 ret = write_extent_to_disk(eb);
375 int write_tree_block(struct btrfs_trans_handle *trans,
376 struct btrfs_root *root,
377 struct extent_buffer *eb)
379 if (check_tree_block(root, eb)) {
380 print_tree_block_error(root, eb, check_tree_block(root, eb));
384 if (trans && !btrfs_buffer_uptodate(eb, trans->transid))
387 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
388 csum_tree_block(root, eb, 0);
390 return write_and_map_eb(trans, root, eb);
393 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
394 u32 stripesize, struct btrfs_root *root,
395 struct btrfs_fs_info *fs_info, u64 objectid)
398 root->commit_root = NULL;
399 root->sectorsize = sectorsize;
400 root->nodesize = nodesize;
401 root->leafsize = leafsize;
402 root->stripesize = stripesize;
404 root->track_dirty = 0;
406 root->fs_info = fs_info;
407 root->objectid = objectid;
408 root->last_trans = 0;
409 root->highest_inode = 0;
410 root->last_inode_alloc = 0;
412 INIT_LIST_HEAD(&root->dirty_list);
413 INIT_LIST_HEAD(&root->orphan_data_extents);
414 memset(&root->root_key, 0, sizeof(root->root_key));
415 memset(&root->root_item, 0, sizeof(root->root_item));
416 root->root_key.objectid = objectid;
420 static int update_cowonly_root(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root)
425 struct btrfs_root *tree_root = root->fs_info->tree_root;
427 btrfs_write_dirty_block_groups(trans, root);
429 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
430 if (old_root_bytenr == root->node->start)
432 btrfs_set_root_bytenr(&root->root_item,
434 btrfs_set_root_generation(&root->root_item,
436 root->root_item.level = btrfs_header_level(root->node);
437 ret = btrfs_update_root(trans, tree_root,
441 btrfs_write_dirty_block_groups(trans, root);
446 static int commit_tree_roots(struct btrfs_trans_handle *trans,
447 struct btrfs_fs_info *fs_info)
449 struct btrfs_root *root;
450 struct list_head *next;
451 struct extent_buffer *eb;
454 if (fs_info->readonly)
457 eb = fs_info->tree_root->node;
458 extent_buffer_get(eb);
459 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
460 free_extent_buffer(eb);
464 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
465 next = fs_info->dirty_cowonly_roots.next;
467 root = list_entry(next, struct btrfs_root, dirty_list);
468 update_cowonly_root(trans, root);
469 free_extent_buffer(root->commit_root);
470 root->commit_root = NULL;
476 static int __commit_transaction(struct btrfs_trans_handle *trans,
477 struct btrfs_root *root)
481 struct extent_buffer *eb;
482 struct extent_io_tree *tree = &root->fs_info->extent_cache;
486 ret = find_first_extent_bit(tree, 0, &start, &end,
490 while(start <= end) {
491 eb = find_first_extent_buffer(tree, start);
492 BUG_ON(!eb || eb->start != start);
493 ret = write_tree_block(trans, root, eb);
496 clear_extent_buffer_dirty(eb);
497 free_extent_buffer(eb);
503 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
504 struct btrfs_root *root)
506 u64 transid = trans->transid;
508 struct btrfs_fs_info *fs_info = root->fs_info;
510 if (root->commit_root == root->node)
512 if (root == root->fs_info->tree_root)
515 free_extent_buffer(root->commit_root);
516 root->commit_root = NULL;
518 btrfs_set_root_bytenr(&root->root_item, root->node->start);
519 btrfs_set_root_generation(&root->root_item, trans->transid);
520 root->root_item.level = btrfs_header_level(root->node);
521 ret = btrfs_update_root(trans, root->fs_info->tree_root,
522 &root->root_key, &root->root_item);
525 ret = commit_tree_roots(trans, fs_info);
527 ret = __commit_transaction(trans, root);
529 write_ctree_super(trans, root);
530 btrfs_finish_extent_commit(trans, fs_info->extent_root,
531 &fs_info->pinned_extents);
532 btrfs_free_transaction(root, trans);
533 free_extent_buffer(root->commit_root);
534 root->commit_root = NULL;
535 fs_info->running_transaction = NULL;
536 fs_info->last_trans_committed = transid;
540 static int find_and_setup_root(struct btrfs_root *tree_root,
541 struct btrfs_fs_info *fs_info,
542 u64 objectid, struct btrfs_root *root)
548 __setup_root(tree_root->nodesize, tree_root->leafsize,
549 tree_root->sectorsize, tree_root->stripesize,
550 root, fs_info, objectid);
551 ret = btrfs_find_last_root(tree_root, objectid,
552 &root->root_item, &root->root_key);
556 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
557 generation = btrfs_root_generation(&root->root_item);
558 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
559 blocksize, generation);
560 if (!extent_buffer_uptodate(root->node))
566 static int find_and_setup_log_root(struct btrfs_root *tree_root,
567 struct btrfs_fs_info *fs_info,
568 struct btrfs_super_block *disk_super)
571 u64 blocknr = btrfs_super_log_root(disk_super);
572 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
582 blocksize = btrfs_level_size(tree_root,
583 btrfs_super_log_root_level(disk_super));
585 __setup_root(tree_root->nodesize, tree_root->leafsize,
586 tree_root->sectorsize, tree_root->stripesize,
587 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
589 log_root->node = read_tree_block(tree_root, blocknr,
591 btrfs_super_generation(disk_super) + 1);
593 fs_info->log_root_tree = log_root;
595 if (!extent_buffer_uptodate(log_root->node)) {
596 free_extent_buffer(log_root->node);
598 fs_info->log_root_tree = NULL;
605 int btrfs_free_fs_root(struct btrfs_root *root)
608 free_extent_buffer(root->node);
609 if (root->commit_root)
610 free_extent_buffer(root->commit_root);
615 static void __free_fs_root(struct rb_node *node)
617 struct btrfs_root *root;
619 root = container_of(node, struct btrfs_root, rb_node);
620 btrfs_free_fs_root(root);
623 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
625 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
626 struct btrfs_key *location)
628 struct btrfs_root *root;
629 struct btrfs_root *tree_root = fs_info->tree_root;
630 struct btrfs_path *path;
631 struct extent_buffer *l;
636 root = malloc(sizeof(*root));
638 return ERR_PTR(-ENOMEM);
639 memset(root, 0, sizeof(*root));
640 if (location->offset == (u64)-1) {
641 ret = find_and_setup_root(tree_root, fs_info,
642 location->objectid, root);
650 __setup_root(tree_root->nodesize, tree_root->leafsize,
651 tree_root->sectorsize, tree_root->stripesize,
652 root, fs_info, location->objectid);
654 path = btrfs_alloc_path();
656 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
663 read_extent_buffer(l, &root->root_item,
664 btrfs_item_ptr_offset(l, path->slots[0]),
665 sizeof(root->root_item));
666 memcpy(&root->root_key, location, sizeof(*location));
669 btrfs_free_path(path);
674 generation = btrfs_root_generation(&root->root_item);
675 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
676 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
677 blocksize, generation);
678 if (!extent_buffer_uptodate(root->node)) {
680 return ERR_PTR(-EIO);
687 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
690 u64 objectid = *((u64 *)data);
691 struct btrfs_root *root;
693 root = rb_entry(node, struct btrfs_root, rb_node);
694 if (objectid > root->objectid)
696 else if (objectid < root->objectid)
702 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
703 struct rb_node *node2)
705 struct btrfs_root *root;
707 root = rb_entry(node2, struct btrfs_root, rb_node);
708 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
711 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
712 struct btrfs_key *location)
714 struct btrfs_root *root;
715 struct rb_node *node;
717 u64 objectid = location->objectid;
719 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
720 return fs_info->tree_root;
721 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
722 return fs_info->extent_root;
723 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
724 return fs_info->chunk_root;
725 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
726 return fs_info->dev_root;
727 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
728 return fs_info->csum_root;
729 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
730 return fs_info->quota_root;
732 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
733 location->offset != (u64)-1);
735 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
736 btrfs_fs_roots_compare_objectids, NULL);
738 return container_of(node, struct btrfs_root, rb_node);
740 root = btrfs_read_fs_root_no_cache(fs_info, location);
744 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
745 btrfs_fs_roots_compare_roots);
750 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
752 free(fs_info->tree_root);
753 free(fs_info->extent_root);
754 free(fs_info->chunk_root);
755 free(fs_info->dev_root);
756 free(fs_info->csum_root);
757 free(fs_info->quota_root);
758 free(fs_info->super_copy);
759 free(fs_info->log_root_tree);
763 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
765 struct btrfs_fs_info *fs_info;
767 fs_info = malloc(sizeof(struct btrfs_fs_info));
771 memset(fs_info, 0, sizeof(struct btrfs_fs_info));
773 fs_info->tree_root = malloc(sizeof(struct btrfs_root));
774 fs_info->extent_root = malloc(sizeof(struct btrfs_root));
775 fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
776 fs_info->dev_root = malloc(sizeof(struct btrfs_root));
777 fs_info->csum_root = malloc(sizeof(struct btrfs_root));
778 fs_info->quota_root = malloc(sizeof(struct btrfs_root));
779 fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
781 if (!fs_info->tree_root || !fs_info->extent_root ||
782 !fs_info->chunk_root || !fs_info->dev_root ||
783 !fs_info->csum_root || !fs_info->quota_root ||
784 !fs_info->super_copy)
787 memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
788 memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
789 memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
790 memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
791 memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
792 memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
793 memset(fs_info->quota_root, 0, sizeof(struct btrfs_root));
795 extent_io_tree_init(&fs_info->extent_cache);
796 extent_io_tree_init(&fs_info->free_space_cache);
797 extent_io_tree_init(&fs_info->block_group_cache);
798 extent_io_tree_init(&fs_info->pinned_extents);
799 extent_io_tree_init(&fs_info->pending_del);
800 extent_io_tree_init(&fs_info->extent_ins);
801 fs_info->excluded_extents = NULL;
803 fs_info->fs_root_tree = RB_ROOT;
804 cache_tree_init(&fs_info->mapping_tree.cache_tree);
806 mutex_init(&fs_info->fs_mutex);
807 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
808 INIT_LIST_HEAD(&fs_info->space_info);
809 INIT_LIST_HEAD(&fs_info->recow_ebs);
812 fs_info->readonly = 1;
814 fs_info->super_bytenr = sb_bytenr;
815 fs_info->data_alloc_profile = (u64)-1;
816 fs_info->metadata_alloc_profile = (u64)-1;
817 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
820 btrfs_free_fs_info(fs_info);
824 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
828 features = btrfs_super_incompat_flags(sb) &
829 ~BTRFS_FEATURE_INCOMPAT_SUPP;
831 printk("couldn't open because of unsupported "
832 "option features (%Lx).\n",
833 (unsigned long long)features);
837 features = btrfs_super_incompat_flags(sb);
838 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
839 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
840 btrfs_set_super_incompat_flags(sb, features);
843 features = btrfs_super_compat_ro_flags(sb) &
844 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
845 if (writable && features) {
846 printk("couldn't open RDWR because of unsupported "
847 "option features (%Lx).\n",
848 (unsigned long long)features);
854 static int find_best_backup_root(struct btrfs_super_block *super)
856 struct btrfs_root_backup *backup;
857 u64 orig_gen = btrfs_super_generation(super);
862 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
863 backup = super->super_roots + i;
864 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
865 btrfs_backup_tree_root_gen(backup) > gen) {
867 gen = btrfs_backup_tree_root_gen(backup);
873 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
874 enum btrfs_open_ctree_flags flags,
875 struct btrfs_root *info_root,
876 u64 objectid, char *str)
878 struct btrfs_super_block *sb = fs_info->super_copy;
879 struct btrfs_root *root = fs_info->tree_root;
880 u32 leafsize = btrfs_super_leafsize(sb);
883 ret = find_and_setup_root(root, fs_info, objectid, info_root);
885 printk("Couldn't setup %s tree\n", str);
886 if (!(flags & OPEN_CTREE_PARTIAL))
889 * Need a blank node here just so we don't screw up in the
890 * million of places that assume a root has a valid ->node
893 btrfs_find_create_tree_block(info_root, 0, leafsize);
894 if (!info_root->node)
896 clear_extent_buffer_uptodate(NULL, info_root->node);
902 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
903 enum btrfs_open_ctree_flags flags)
905 struct btrfs_super_block *sb = fs_info->super_copy;
906 struct btrfs_root *root;
907 struct btrfs_key key;
916 nodesize = btrfs_super_nodesize(sb);
917 leafsize = btrfs_super_leafsize(sb);
918 sectorsize = btrfs_super_sectorsize(sb);
919 stripesize = btrfs_super_stripesize(sb);
921 root = fs_info->tree_root;
922 __setup_root(nodesize, leafsize, sectorsize, stripesize,
923 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
924 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
925 generation = btrfs_super_generation(sb);
927 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
928 root_tree_bytenr = btrfs_super_root(sb);
929 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
930 struct btrfs_root_backup *backup;
931 int index = find_best_backup_root(sb);
932 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
933 fprintf(stderr, "Invalid backup root number\n");
936 backup = fs_info->super_copy->super_roots + index;
937 root_tree_bytenr = btrfs_backup_tree_root(backup);
938 generation = btrfs_backup_tree_root_gen(backup);
941 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
943 if (!extent_buffer_uptodate(root->node)) {
944 fprintf(stderr, "Couldn't read tree root\n");
948 ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
949 BTRFS_EXTENT_TREE_OBJECTID, "extent");
952 fs_info->extent_root->track_dirty = 1;
954 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
957 printk("Couldn't setup device tree\n");
960 fs_info->dev_root->track_dirty = 1;
962 ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
963 BTRFS_CSUM_TREE_OBJECTID, "csum");
966 fs_info->csum_root->track_dirty = 1;
968 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
969 fs_info->quota_root);
971 fs_info->quota_enabled = 1;
973 ret = find_and_setup_log_root(root, fs_info, sb);
975 printk("Couldn't setup log root tree\n");
976 if (!(flags & OPEN_CTREE_PARTIAL))
980 fs_info->generation = generation;
981 fs_info->last_trans_committed = generation;
982 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
983 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
984 btrfs_read_block_groups(fs_info->tree_root);
986 key.objectid = BTRFS_FS_TREE_OBJECTID;
987 key.type = BTRFS_ROOT_ITEM_KEY;
988 key.offset = (u64)-1;
989 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
991 if (IS_ERR(fs_info->fs_root))
996 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
998 if (fs_info->quota_root)
999 free_extent_buffer(fs_info->quota_root->node);
1000 if (fs_info->csum_root)
1001 free_extent_buffer(fs_info->csum_root->node);
1002 if (fs_info->dev_root)
1003 free_extent_buffer(fs_info->dev_root->node);
1004 if (fs_info->extent_root)
1005 free_extent_buffer(fs_info->extent_root->node);
1006 if (fs_info->tree_root)
1007 free_extent_buffer(fs_info->tree_root->node);
1008 if (fs_info->log_root_tree)
1009 free_extent_buffer(fs_info->log_root_tree->node);
1010 if (fs_info->chunk_root)
1011 free_extent_buffer(fs_info->chunk_root->node);
1014 static void free_map_lookup(struct cache_extent *ce)
1016 struct map_lookup *map;
1018 map = container_of(ce, struct map_lookup, ce);
1022 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
1024 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
1026 while (!list_empty(&fs_info->recow_ebs)) {
1027 struct extent_buffer *eb;
1028 eb = list_first_entry(&fs_info->recow_ebs,
1029 struct extent_buffer, recow);
1030 list_del_init(&eb->recow);
1031 free_extent_buffer(eb);
1033 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
1034 extent_io_tree_cleanup(&fs_info->extent_cache);
1035 extent_io_tree_cleanup(&fs_info->free_space_cache);
1036 extent_io_tree_cleanup(&fs_info->block_group_cache);
1037 extent_io_tree_cleanup(&fs_info->pinned_extents);
1038 extent_io_tree_cleanup(&fs_info->pending_del);
1039 extent_io_tree_cleanup(&fs_info->extent_ins);
1042 int btrfs_scan_fs_devices(int fd, const char *path,
1043 struct btrfs_fs_devices **fs_devices,
1044 u64 sb_bytenr, int super_recover,
1052 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1054 seek_ret = lseek(fd, 0, SEEK_END);
1058 dev_size = seek_ret;
1059 lseek(fd, 0, SEEK_SET);
1060 if (sb_bytenr > dev_size) {
1061 fprintf(stderr, "Superblock bytenr is larger than device size\n");
1065 ret = btrfs_scan_one_device(fd, path, fs_devices,
1066 &total_devs, sb_bytenr, super_recover);
1068 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1072 if (!skip_devices && total_devs != 1) {
1073 ret = btrfs_scan_lblkid();
1080 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1082 struct btrfs_super_block *sb = fs_info->super_copy;
1091 nodesize = btrfs_super_nodesize(sb);
1092 leafsize = btrfs_super_leafsize(sb);
1093 sectorsize = btrfs_super_sectorsize(sb);
1094 stripesize = btrfs_super_stripesize(sb);
1096 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1097 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1099 ret = btrfs_read_sys_array(fs_info->chunk_root);
1103 blocksize = btrfs_level_size(fs_info->chunk_root,
1104 btrfs_super_chunk_root_level(sb));
1105 generation = btrfs_super_chunk_root_generation(sb);
1107 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1108 btrfs_super_chunk_root(sb),
1109 blocksize, generation);
1110 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1111 fprintf(stderr, "Couldn't read chunk root\n");
1115 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1116 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1118 fprintf(stderr, "Couldn't read chunk tree\n");
1125 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1127 u64 root_tree_bytenr,
1128 enum btrfs_open_ctree_flags flags)
1130 struct btrfs_fs_info *fs_info;
1131 struct btrfs_super_block *disk_super;
1132 struct btrfs_fs_devices *fs_devices = NULL;
1133 struct extent_buffer *eb;
1138 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1140 /* try to drop all the caches */
1141 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1142 fprintf(stderr, "Warning, could not drop caches\n");
1144 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1146 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1149 if (flags & OPEN_CTREE_RESTORE)
1150 fs_info->on_restoring = 1;
1151 if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1152 fs_info->suppress_check_block_errors = 1;
1153 if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
1154 fs_info->ignore_fsid_mismatch = 1;
1156 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1157 (flags & OPEN_CTREE_RECOVER_SUPER),
1158 (flags & OPEN_CTREE_NO_DEVICES));
1162 fs_info->fs_devices = fs_devices;
1163 if (flags & OPEN_CTREE_WRITES)
1168 if (flags & OPEN_CTREE_EXCLUSIVE)
1171 ret = btrfs_open_devices(fs_devices, oflags);
1175 disk_super = fs_info->super_copy;
1176 if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1177 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1178 disk_super, sb_bytenr, 1);
1180 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1182 printk("No valid btrfs found\n");
1186 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID &&
1187 !fs_info->ignore_fsid_mismatch) {
1188 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1192 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1194 ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1195 flags & OPEN_CTREE_WRITES);
1199 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1203 eb = fs_info->chunk_root->node;
1204 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1205 btrfs_header_chunk_tree_uuid(eb),
1208 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1209 if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT))
1215 btrfs_release_all_roots(fs_info);
1216 btrfs_cleanup_all_caches(fs_info);
1218 btrfs_close_devices(fs_devices);
1220 btrfs_free_fs_info(fs_info);
1224 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1225 u64 sb_bytenr, u64 root_tree_bytenr,
1226 enum btrfs_open_ctree_flags flags)
1229 struct btrfs_fs_info *info;
1230 int oflags = O_CREAT | O_RDWR;
1232 if (!(flags & OPEN_CTREE_WRITES))
1235 fp = open(filename, oflags, 0600);
1237 fprintf (stderr, "Could not open %s\n", filename);
1240 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1246 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1247 enum btrfs_open_ctree_flags flags)
1249 struct btrfs_fs_info *info;
1251 info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1254 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1255 return info->chunk_root;
1256 return info->fs_root;
1259 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1260 enum btrfs_open_ctree_flags flags)
1262 struct btrfs_fs_info *info;
1263 info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1266 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1267 return info->chunk_root;
1268 return info->fs_root;
1271 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1274 u8 fsid[BTRFS_FSID_SIZE];
1275 int fsid_is_initialized = 0;
1276 struct btrfs_super_block buf;
1279 int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1283 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1284 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1285 if (ret < sizeof(buf))
1288 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1289 btrfs_super_magic(&buf) != BTRFS_MAGIC)
1292 memcpy(sb, &buf, sizeof(*sb));
1297 * we would like to check all the supers, but that would make
1298 * a btrfs mount succeed after a mkfs from a different FS.
1299 * So, we need to add a special mount option to scan for
1300 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1303 for (i = 0; i < max_super; i++) {
1304 bytenr = btrfs_sb_offset(i);
1305 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1306 if (ret < sizeof(buf))
1309 if (btrfs_super_bytenr(&buf) != bytenr )
1311 /* if magic is NULL, the device was removed */
1312 if (btrfs_super_magic(&buf) == 0 && i == 0)
1314 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1317 if (!fsid_is_initialized) {
1318 memcpy(fsid, buf.fsid, sizeof(fsid));
1319 fsid_is_initialized = 1;
1320 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1322 * the superblocks (the original one and
1323 * its backups) contain data of different
1324 * filesystems -> the super cannot be trusted
1329 if (btrfs_super_generation(&buf) > transid) {
1330 memcpy(sb, &buf, sizeof(*sb));
1331 transid = btrfs_super_generation(&buf);
1335 return transid > 0 ? 0 : -1;
1338 static int write_dev_supers(struct btrfs_root *root,
1339 struct btrfs_super_block *sb,
1340 struct btrfs_device *device)
1346 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1347 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1349 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1350 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1351 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1354 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1355 * zero filled, we can use it directly
1357 ret = pwrite64(device->fd, root->fs_info->super_copy,
1358 BTRFS_SUPER_INFO_SIZE,
1359 root->fs_info->super_bytenr);
1360 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1364 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1365 bytenr = btrfs_sb_offset(i);
1366 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1369 btrfs_set_super_bytenr(sb, bytenr);
1372 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1373 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1374 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1377 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1378 * zero filled, we can use it directly
1380 ret = pwrite64(device->fd, root->fs_info->super_copy,
1381 BTRFS_SUPER_INFO_SIZE, bytenr);
1382 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1388 int write_all_supers(struct btrfs_root *root)
1390 struct list_head *cur;
1391 struct list_head *head = &root->fs_info->fs_devices->devices;
1392 struct btrfs_device *dev;
1393 struct btrfs_super_block *sb;
1394 struct btrfs_dev_item *dev_item;
1398 sb = root->fs_info->super_copy;
1399 dev_item = &sb->dev_item;
1400 list_for_each(cur, head) {
1401 dev = list_entry(cur, struct btrfs_device, dev_list);
1402 if (!dev->writeable)
1405 btrfs_set_stack_device_generation(dev_item, 0);
1406 btrfs_set_stack_device_type(dev_item, dev->type);
1407 btrfs_set_stack_device_id(dev_item, dev->devid);
1408 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1409 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1410 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1411 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1412 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1413 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1414 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1416 flags = btrfs_super_flags(sb);
1417 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1419 ret = write_dev_supers(root, sb, dev);
1425 int write_ctree_super(struct btrfs_trans_handle *trans,
1426 struct btrfs_root *root)
1429 struct btrfs_root *tree_root = root->fs_info->tree_root;
1430 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1432 if (root->fs_info->readonly)
1435 btrfs_set_super_generation(root->fs_info->super_copy,
1437 btrfs_set_super_root(root->fs_info->super_copy,
1438 tree_root->node->start);
1439 btrfs_set_super_root_level(root->fs_info->super_copy,
1440 btrfs_header_level(tree_root->node));
1441 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1442 chunk_root->node->start);
1443 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1444 btrfs_header_level(chunk_root->node));
1445 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1446 btrfs_header_generation(chunk_root->node));
1448 ret = write_all_supers(root);
1450 fprintf(stderr, "failed to write new super block err %d\n", ret);
1454 int close_ctree(struct btrfs_root *root)
1457 struct btrfs_trans_handle *trans;
1458 struct btrfs_fs_info *fs_info = root->fs_info;
1460 if (fs_info->last_trans_committed !=
1461 fs_info->generation) {
1462 trans = btrfs_start_transaction(root, 1);
1463 btrfs_commit_transaction(trans, root);
1464 trans = btrfs_start_transaction(root, 1);
1465 ret = commit_tree_roots(trans, fs_info);
1467 ret = __commit_transaction(trans, root);
1469 write_ctree_super(trans, root);
1470 btrfs_free_transaction(root, trans);
1472 btrfs_free_block_groups(fs_info);
1474 free_fs_roots_tree(&fs_info->fs_root_tree);
1476 btrfs_release_all_roots(fs_info);
1477 btrfs_close_devices(fs_info->fs_devices);
1478 btrfs_cleanup_all_caches(fs_info);
1479 btrfs_free_fs_info(fs_info);
1483 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1484 struct extent_buffer *eb)
1486 return clear_extent_buffer_dirty(eb);
1489 int wait_on_tree_block_writeback(struct btrfs_root *root,
1490 struct extent_buffer *eb)
1495 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1497 set_extent_buffer_dirty(eb);
1500 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1504 ret = extent_buffer_uptodate(buf);
1508 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1512 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1514 return set_extent_buffer_uptodate(eb);