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.
19 #define _XOPEN_SOURCE 600
24 #include <sys/types.h>
28 #include "kerncompat.h"
29 #include "radix-tree.h"
33 #include "transaction.h"
36 #include "print-tree.h"
38 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
41 struct btrfs_fs_devices *fs_devices;
44 if (buf->start != btrfs_header_bytenr(buf)) {
45 printk("Check tree block failed, want=%Lu, have=%Lu\n",
46 buf->start, btrfs_header_bytenr(buf));
50 fs_devices = root->fs_info->fs_devices;
52 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
58 fs_devices = fs_devices->seed;
63 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
65 return crc32c(seed, data, len);
68 void btrfs_csum_final(u32 crc, char *result)
70 *(__le32 *)result = ~cpu_to_le32(crc);
73 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
74 int verify, int silent)
80 result = malloc(csum_size * sizeof(char));
84 len = buf->len - BTRFS_CSUM_SIZE;
85 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
86 btrfs_csum_final(crc, result);
89 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
91 printk("checksum verify failed on %llu found %08X wanted %08X\n",
92 (unsigned long long)buf->start,
94 *((u32*)(char *)buf->data));
99 write_extent_buffer(buf, result, 0, csum_size);
105 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
107 return __csum_tree_block_size(buf, csum_size, verify, 0);
110 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
112 return __csum_tree_block_size(buf, csum_size, 1, 1);
115 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
119 btrfs_super_csum_size(root->fs_info->super_copy);
120 return csum_tree_block_size(buf, csum_size, verify);
123 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
124 u64 bytenr, u32 blocksize)
126 return find_extent_buffer(&root->fs_info->extent_cache,
130 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
131 u64 bytenr, u32 blocksize)
133 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
137 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
140 struct extent_buffer *eb;
142 struct btrfs_multi_bio *multi = NULL;
143 struct btrfs_device *device;
145 eb = btrfs_find_tree_block(root, bytenr, blocksize);
146 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
147 !btrfs_map_block(&root->fs_info->mapping_tree, READ,
148 bytenr, &length, &multi, 0, NULL)) {
149 device = multi->stripes[0].dev;
151 blocksize = min(blocksize, (u32)(64 * 1024));
152 readahead(device->fd, multi->stripes[0].physical, blocksize);
155 free_extent_buffer(eb);
159 static int verify_parent_transid(struct extent_io_tree *io_tree,
160 struct extent_buffer *eb, u64 parent_transid,
165 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
168 if (extent_buffer_uptodate(eb) &&
169 btrfs_header_generation(eb) == parent_transid) {
173 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
174 (unsigned long long)eb->start,
175 (unsigned long long)parent_transid,
176 (unsigned long long)btrfs_header_generation(eb));
178 eb->flags |= EXTENT_BAD_TRANSID;
179 printk("Ignoring transid failure\n");
185 clear_extent_buffer_uptodate(io_tree, eb);
191 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
193 unsigned long offset = 0;
194 struct btrfs_multi_bio *multi = NULL;
195 struct btrfs_device *device;
198 unsigned long bytes_left = eb->len;
201 read_len = bytes_left;
204 if (!info->on_restoring) {
205 ret = btrfs_map_block(&info->mapping_tree, READ,
206 eb->start + offset, &read_len, &multi,
209 printk("Couldn't map the block %Lu\n", eb->start + offset);
213 device = multi->stripes[0].dev;
215 if (device->fd == 0) {
222 eb->dev_bytenr = multi->stripes[0].physical;
226 /* special case for restore metadump */
227 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
228 if (device->devid == 1)
233 eb->dev_bytenr = eb->start;
237 if (read_len > bytes_left)
238 read_len = bytes_left;
240 ret = read_extent_from_disk(eb, offset, read_len);
244 bytes_left -= read_len;
249 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
250 u32 blocksize, u64 parent_transid)
253 struct extent_buffer *eb;
254 u64 best_transid = 0;
260 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
264 if (btrfs_buffer_uptodate(eb, parent_transid))
268 ret = read_whole_eb(root->fs_info, eb, mirror_num);
269 if (ret == 0 && check_tree_block(root, eb) == 0 &&
270 csum_tree_block(root, eb, 1) == 0 &&
271 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
273 if (eb->flags & EXTENT_BAD_TRANSID &&
274 list_empty(&eb->recow)) {
275 list_add_tail(&eb->recow,
276 &root->fs_info->recow_ebs);
279 btrfs_set_buffer_uptodate(eb);
283 if (check_tree_block(root, eb))
284 printk("read block failed check_tree_block\n");
286 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 memset(&root->root_key, 0, sizeof(root->root_key));
379 memset(&root->root_item, 0, sizeof(root->root_item));
380 root->root_key.objectid = objectid;
384 static int update_cowonly_root(struct btrfs_trans_handle *trans,
385 struct btrfs_root *root)
389 struct btrfs_root *tree_root = root->fs_info->tree_root;
391 btrfs_write_dirty_block_groups(trans, root);
393 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
394 if (old_root_bytenr == root->node->start)
396 btrfs_set_root_bytenr(&root->root_item,
398 btrfs_set_root_generation(&root->root_item,
400 root->root_item.level = btrfs_header_level(root->node);
401 ret = btrfs_update_root(trans, tree_root,
405 btrfs_write_dirty_block_groups(trans, root);
410 static int commit_tree_roots(struct btrfs_trans_handle *trans,
411 struct btrfs_fs_info *fs_info)
413 struct btrfs_root *root;
414 struct list_head *next;
415 struct extent_buffer *eb;
418 if (fs_info->readonly)
421 eb = fs_info->tree_root->node;
422 extent_buffer_get(eb);
423 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
424 free_extent_buffer(eb);
428 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
429 next = fs_info->dirty_cowonly_roots.next;
431 root = list_entry(next, struct btrfs_root, dirty_list);
432 update_cowonly_root(trans, root);
433 free_extent_buffer(root->commit_root);
434 root->commit_root = NULL;
440 static int __commit_transaction(struct btrfs_trans_handle *trans,
441 struct btrfs_root *root)
445 struct extent_buffer *eb;
446 struct extent_io_tree *tree = &root->fs_info->extent_cache;
450 ret = find_first_extent_bit(tree, 0, &start, &end,
454 while(start <= end) {
455 eb = find_first_extent_buffer(tree, start);
456 BUG_ON(!eb || eb->start != start);
457 ret = write_tree_block(trans, root, eb);
460 clear_extent_buffer_dirty(eb);
461 free_extent_buffer(eb);
467 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
468 struct btrfs_root *root)
470 u64 transid = trans->transid;
472 struct btrfs_fs_info *fs_info = root->fs_info;
474 if (root->commit_root == root->node)
477 free_extent_buffer(root->commit_root);
478 root->commit_root = NULL;
480 btrfs_set_root_bytenr(&root->root_item, root->node->start);
481 btrfs_set_root_generation(&root->root_item, trans->transid);
482 root->root_item.level = btrfs_header_level(root->node);
483 ret = btrfs_update_root(trans, root->fs_info->tree_root,
484 &root->root_key, &root->root_item);
487 ret = commit_tree_roots(trans, fs_info);
489 ret = __commit_transaction(trans, root);
491 write_ctree_super(trans, root);
492 btrfs_finish_extent_commit(trans, fs_info->extent_root,
493 &fs_info->pinned_extents);
494 btrfs_free_transaction(root, trans);
495 free_extent_buffer(root->commit_root);
496 root->commit_root = NULL;
497 fs_info->running_transaction = NULL;
498 fs_info->last_trans_committed = transid;
502 static int find_and_setup_root(struct btrfs_root *tree_root,
503 struct btrfs_fs_info *fs_info,
504 u64 objectid, struct btrfs_root *root)
510 __setup_root(tree_root->nodesize, tree_root->leafsize,
511 tree_root->sectorsize, tree_root->stripesize,
512 root, fs_info, objectid);
513 ret = btrfs_find_last_root(tree_root, objectid,
514 &root->root_item, &root->root_key);
518 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
519 generation = btrfs_root_generation(&root->root_item);
520 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
521 blocksize, generation);
522 if (!extent_buffer_uptodate(root->node))
528 static int find_and_setup_log_root(struct btrfs_root *tree_root,
529 struct btrfs_fs_info *fs_info,
530 struct btrfs_super_block *disk_super)
533 u64 blocknr = btrfs_super_log_root(disk_super);
534 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
544 blocksize = btrfs_level_size(tree_root,
545 btrfs_super_log_root_level(disk_super));
547 __setup_root(tree_root->nodesize, tree_root->leafsize,
548 tree_root->sectorsize, tree_root->stripesize,
549 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
551 log_root->node = read_tree_block(tree_root, blocknr,
553 btrfs_super_generation(disk_super) + 1);
555 fs_info->log_root_tree = log_root;
557 if (!extent_buffer_uptodate(log_root->node)) {
558 free_extent_buffer(log_root->node);
560 fs_info->log_root_tree = NULL;
567 int btrfs_free_fs_root(struct btrfs_root *root)
570 free_extent_buffer(root->node);
571 if (root->commit_root)
572 free_extent_buffer(root->commit_root);
577 static void __free_fs_root(struct rb_node *node)
579 struct btrfs_root *root;
581 root = container_of(node, struct btrfs_root, rb_node);
582 btrfs_free_fs_root(root);
585 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
587 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
588 struct btrfs_key *location)
590 struct btrfs_root *root;
591 struct btrfs_root *tree_root = fs_info->tree_root;
592 struct btrfs_path *path;
593 struct extent_buffer *l;
598 root = malloc(sizeof(*root));
600 return ERR_PTR(-ENOMEM);
601 memset(root, 0, sizeof(*root));
602 if (location->offset == (u64)-1) {
603 ret = find_and_setup_root(tree_root, fs_info,
604 location->objectid, root);
612 __setup_root(tree_root->nodesize, tree_root->leafsize,
613 tree_root->sectorsize, tree_root->stripesize,
614 root, fs_info, location->objectid);
616 path = btrfs_alloc_path();
618 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
625 read_extent_buffer(l, &root->root_item,
626 btrfs_item_ptr_offset(l, path->slots[0]),
627 sizeof(root->root_item));
628 memcpy(&root->root_key, location, sizeof(*location));
631 btrfs_free_path(path);
636 generation = btrfs_root_generation(&root->root_item);
637 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
638 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
639 blocksize, generation);
642 return ERR_PTR(-EIO);
649 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
652 u64 objectid = *((u64 *)data);
653 struct btrfs_root *root;
655 root = rb_entry(node, struct btrfs_root, rb_node);
656 if (objectid > root->objectid)
658 else if (objectid < root->objectid)
664 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
665 struct rb_node *node2)
667 struct btrfs_root *root;
669 root = rb_entry(node2, struct btrfs_root, rb_node);
670 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
673 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
674 struct btrfs_key *location)
676 struct btrfs_root *root;
677 struct rb_node *node;
679 u64 objectid = location->objectid;
681 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
682 return fs_info->tree_root;
683 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
684 return fs_info->extent_root;
685 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
686 return fs_info->chunk_root;
687 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
688 return fs_info->dev_root;
689 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
690 return fs_info->csum_root;
691 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
692 return fs_info->csum_root;
694 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
695 location->offset != (u64)-1);
697 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
698 btrfs_fs_roots_compare_objectids, NULL);
700 return container_of(node, struct btrfs_root, rb_node);
702 root = btrfs_read_fs_root_no_cache(fs_info, location);
706 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
707 btrfs_fs_roots_compare_roots);
712 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
714 free(fs_info->tree_root);
715 free(fs_info->extent_root);
716 free(fs_info->chunk_root);
717 free(fs_info->dev_root);
718 free(fs_info->csum_root);
719 free(fs_info->quota_root);
720 free(fs_info->super_copy);
721 free(fs_info->log_root_tree);
725 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
727 struct btrfs_fs_info *fs_info;
729 fs_info = malloc(sizeof(struct btrfs_fs_info));
733 memset(fs_info, 0, sizeof(struct btrfs_fs_info));
735 fs_info->tree_root = malloc(sizeof(struct btrfs_root));
736 fs_info->extent_root = malloc(sizeof(struct btrfs_root));
737 fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
738 fs_info->dev_root = malloc(sizeof(struct btrfs_root));
739 fs_info->csum_root = malloc(sizeof(struct btrfs_root));
740 fs_info->quota_root = malloc(sizeof(struct btrfs_root));
741 fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
743 if (!fs_info->tree_root || !fs_info->extent_root ||
744 !fs_info->chunk_root || !fs_info->dev_root ||
745 !fs_info->csum_root || !fs_info->quota_root ||
746 !fs_info->super_copy)
749 memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
750 memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
751 memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
752 memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
753 memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
754 memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
755 memset(fs_info->quota_root, 0, sizeof(struct btrfs_root));
757 extent_io_tree_init(&fs_info->extent_cache);
758 extent_io_tree_init(&fs_info->free_space_cache);
759 extent_io_tree_init(&fs_info->block_group_cache);
760 extent_io_tree_init(&fs_info->pinned_extents);
761 extent_io_tree_init(&fs_info->pending_del);
762 extent_io_tree_init(&fs_info->extent_ins);
763 fs_info->fs_root_tree = RB_ROOT;
764 cache_tree_init(&fs_info->mapping_tree.cache_tree);
766 mutex_init(&fs_info->fs_mutex);
767 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
768 INIT_LIST_HEAD(&fs_info->space_info);
769 INIT_LIST_HEAD(&fs_info->recow_ebs);
772 fs_info->readonly = 1;
774 fs_info->super_bytenr = sb_bytenr;
775 fs_info->data_alloc_profile = (u64)-1;
776 fs_info->metadata_alloc_profile = (u64)-1;
777 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
780 btrfs_free_fs_info(fs_info);
784 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
788 features = btrfs_super_incompat_flags(sb) &
789 ~BTRFS_FEATURE_INCOMPAT_SUPP;
791 printk("couldn't open because of unsupported "
792 "option features (%Lx).\n",
793 (unsigned long long)features);
797 features = btrfs_super_incompat_flags(sb);
798 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
799 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
800 btrfs_set_super_incompat_flags(sb, features);
803 features = btrfs_super_compat_ro_flags(sb) &
804 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
805 if (writable && features) {
806 printk("couldn't open RDWR because of unsupported "
807 "option features (%Lx).\n",
808 (unsigned long long)features);
814 static int find_best_backup_root(struct btrfs_super_block *super)
816 struct btrfs_root_backup *backup;
817 u64 orig_gen = btrfs_super_generation(super);
822 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
823 backup = super->super_roots + i;
824 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
825 btrfs_backup_tree_root_gen(backup) > gen) {
827 gen = btrfs_backup_tree_root_gen(backup);
833 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
834 enum btrfs_open_ctree_flags flags)
836 struct btrfs_super_block *sb = fs_info->super_copy;
837 struct btrfs_root *root;
838 struct btrfs_key key;
847 nodesize = btrfs_super_nodesize(sb);
848 leafsize = btrfs_super_leafsize(sb);
849 sectorsize = btrfs_super_sectorsize(sb);
850 stripesize = btrfs_super_stripesize(sb);
852 root = fs_info->tree_root;
853 __setup_root(nodesize, leafsize, sectorsize, stripesize,
854 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
855 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
856 generation = btrfs_super_generation(sb);
858 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
859 root_tree_bytenr = btrfs_super_root(sb);
860 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
861 struct btrfs_root_backup *backup;
862 int index = find_best_backup_root(sb);
863 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
864 fprintf(stderr, "Invalid backup root number\n");
867 backup = fs_info->super_copy->super_roots + index;
868 root_tree_bytenr = btrfs_backup_tree_root(backup);
869 generation = btrfs_backup_tree_root_gen(backup);
872 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
874 if (!extent_buffer_uptodate(root->node)) {
875 fprintf(stderr, "Couldn't read tree root\n");
879 ret = find_and_setup_root(root, fs_info, BTRFS_EXTENT_TREE_OBJECTID,
880 fs_info->extent_root);
882 printk("Couldn't setup extent tree\n");
883 if (!(flags & OPEN_CTREE_PARTIAL))
885 /* Need a blank node here just so we don't screw up in the
886 * million of places that assume a root has a valid ->node
888 fs_info->extent_root->node =
889 btrfs_find_create_tree_block(fs_info->extent_root, 0,
891 if (!fs_info->extent_root->node)
893 clear_extent_buffer_uptodate(NULL, fs_info->extent_root->node);
895 fs_info->extent_root->track_dirty = 1;
897 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
900 printk("Couldn't setup device tree\n");
903 fs_info->dev_root->track_dirty = 1;
905 ret = find_and_setup_root(root, fs_info, BTRFS_CSUM_TREE_OBJECTID,
908 printk("Couldn't setup csum tree\n");
909 if (!(flags & OPEN_CTREE_PARTIAL))
912 fs_info->csum_root->track_dirty = 1;
914 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
915 fs_info->quota_root);
917 fs_info->quota_enabled = 1;
919 ret = find_and_setup_log_root(root, fs_info, sb);
921 printk("Couldn't setup log root tree\n");
925 fs_info->generation = generation;
926 fs_info->last_trans_committed = generation;
927 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
928 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
929 btrfs_read_block_groups(fs_info->tree_root);
931 key.objectid = BTRFS_FS_TREE_OBJECTID;
932 key.type = BTRFS_ROOT_ITEM_KEY;
933 key.offset = (u64)-1;
934 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
936 if (IS_ERR(fs_info->fs_root))
941 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
943 if (fs_info->quota_root)
944 free_extent_buffer(fs_info->quota_root->node);
945 if (fs_info->csum_root)
946 free_extent_buffer(fs_info->csum_root->node);
947 if (fs_info->dev_root)
948 free_extent_buffer(fs_info->dev_root->node);
949 if (fs_info->extent_root)
950 free_extent_buffer(fs_info->extent_root->node);
951 if (fs_info->tree_root)
952 free_extent_buffer(fs_info->tree_root->node);
953 if (fs_info->log_root_tree)
954 free_extent_buffer(fs_info->log_root_tree->node);
955 if (fs_info->chunk_root)
956 free_extent_buffer(fs_info->chunk_root->node);
959 static void free_map_lookup(struct cache_extent *ce)
961 struct map_lookup *map;
963 map = container_of(ce, struct map_lookup, ce);
967 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
969 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
971 while (!list_empty(&fs_info->recow_ebs)) {
972 struct extent_buffer *eb;
973 eb = list_first_entry(&fs_info->recow_ebs,
974 struct extent_buffer, recow);
975 list_del_init(&eb->recow);
976 free_extent_buffer(eb);
978 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
979 extent_io_tree_cleanup(&fs_info->extent_cache);
980 extent_io_tree_cleanup(&fs_info->free_space_cache);
981 extent_io_tree_cleanup(&fs_info->block_group_cache);
982 extent_io_tree_cleanup(&fs_info->pinned_extents);
983 extent_io_tree_cleanup(&fs_info->pending_del);
984 extent_io_tree_cleanup(&fs_info->extent_ins);
987 int btrfs_scan_fs_devices(int fd, const char *path,
988 struct btrfs_fs_devices **fs_devices,
989 u64 sb_bytenr, int run_ioctl, int super_recover)
994 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
996 ret = btrfs_scan_one_device(fd, path, fs_devices,
997 &total_devs, sb_bytenr, super_recover);
999 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1003 if (total_devs != 1) {
1004 ret = btrfs_scan_for_fsid(run_ioctl);
1011 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
1013 struct btrfs_super_block *sb = fs_info->super_copy;
1022 nodesize = btrfs_super_nodesize(sb);
1023 leafsize = btrfs_super_leafsize(sb);
1024 sectorsize = btrfs_super_sectorsize(sb);
1025 stripesize = btrfs_super_stripesize(sb);
1027 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1028 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1030 ret = btrfs_read_sys_array(fs_info->chunk_root);
1034 blocksize = btrfs_level_size(fs_info->chunk_root,
1035 btrfs_super_chunk_root_level(sb));
1036 generation = btrfs_super_chunk_root_generation(sb);
1038 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1039 btrfs_super_chunk_root(sb),
1040 blocksize, generation);
1041 if (!fs_info->chunk_root->node ||
1042 !extent_buffer_uptodate(fs_info->chunk_root->node)) {
1043 fprintf(stderr, "Couldn't read chunk root\n");
1047 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1048 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1050 fprintf(stderr, "Couldn't read chunk tree\n");
1057 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1059 u64 root_tree_bytenr,
1060 enum btrfs_open_ctree_flags flags)
1062 struct btrfs_fs_info *fs_info;
1063 struct btrfs_super_block *disk_super;
1064 struct btrfs_fs_devices *fs_devices = NULL;
1065 struct extent_buffer *eb;
1070 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1072 /* try to drop all the caches */
1073 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1074 fprintf(stderr, "Warning, could not drop caches\n");
1076 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1078 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1081 if (flags & OPEN_CTREE_RESTORE)
1082 fs_info->on_restoring = 1;
1084 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1085 !(flags & OPEN_CTREE_RECOVER_SUPER),
1086 (flags & OPEN_CTREE_RECOVER_SUPER));
1090 fs_info->fs_devices = fs_devices;
1091 if (flags & OPEN_CTREE_WRITES)
1096 if (flags & OPEN_CTREE_EXCLUSIVE)
1099 ret = btrfs_open_devices(fs_devices, oflags);
1103 disk_super = fs_info->super_copy;
1104 if (!(flags & OPEN_CTREE_RECOVER_SUPER))
1105 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1106 disk_super, sb_bytenr, 1);
1108 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
1110 printk("No valid btrfs found\n");
1114 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1116 ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1117 flags & OPEN_CTREE_WRITES);
1121 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1125 eb = fs_info->chunk_root->node;
1126 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1127 btrfs_header_chunk_tree_uuid(eb),
1130 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1137 if (flags & OPEN_CTREE_PARTIAL)
1140 btrfs_release_all_roots(fs_info);
1141 btrfs_cleanup_all_caches(fs_info);
1143 btrfs_close_devices(fs_devices);
1145 btrfs_free_fs_info(fs_info);
1149 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1150 u64 sb_bytenr, u64 root_tree_bytenr,
1151 enum btrfs_open_ctree_flags flags)
1154 struct btrfs_fs_info *info;
1155 int oflags = O_CREAT | O_RDWR;
1157 if (!(flags & OPEN_CTREE_WRITES))
1160 fp = open(filename, oflags, 0600);
1162 fprintf (stderr, "Could not open %s\n", filename);
1165 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1171 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1172 enum btrfs_open_ctree_flags flags)
1174 struct btrfs_fs_info *info;
1176 info = open_ctree_fs_info(filename, sb_bytenr, 0, flags);
1179 return info->fs_root;
1182 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1183 enum btrfs_open_ctree_flags flags)
1185 struct btrfs_fs_info *info;
1186 info = __open_ctree_fd(fp, path, sb_bytenr, 0, flags);
1189 return info->fs_root;
1192 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1195 u8 fsid[BTRFS_FSID_SIZE];
1196 int fsid_is_initialized = 0;
1197 struct btrfs_super_block buf;
1200 int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
1204 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1205 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1206 if (ret < sizeof(buf))
1209 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1210 btrfs_super_magic(&buf) != BTRFS_MAGIC)
1213 memcpy(sb, &buf, sizeof(*sb));
1218 * we would like to check all the supers, but that would make
1219 * a btrfs mount succeed after a mkfs from a different FS.
1220 * So, we need to add a special mount option to scan for
1221 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1224 for (i = 0; i < max_super; i++) {
1225 bytenr = btrfs_sb_offset(i);
1226 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1227 if (ret < sizeof(buf))
1230 if (btrfs_super_bytenr(&buf) != bytenr )
1232 /* if magic is NULL, the device was removed */
1233 if (btrfs_super_magic(&buf) == 0 && i == 0)
1235 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1238 if (!fsid_is_initialized) {
1239 memcpy(fsid, buf.fsid, sizeof(fsid));
1240 fsid_is_initialized = 1;
1241 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1243 * the superblocks (the original one and
1244 * its backups) contain data of different
1245 * filesystems -> the super cannot be trusted
1250 if (btrfs_super_generation(&buf) > transid) {
1251 memcpy(sb, &buf, sizeof(*sb));
1252 transid = btrfs_super_generation(&buf);
1256 return transid > 0 ? 0 : -1;
1259 static int write_dev_supers(struct btrfs_root *root,
1260 struct btrfs_super_block *sb,
1261 struct btrfs_device *device)
1267 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1268 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1270 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1271 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1272 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1275 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1276 * zero filled, we can use it directly
1278 ret = pwrite64(device->fd, root->fs_info->super_copy,
1279 BTRFS_SUPER_INFO_SIZE,
1280 root->fs_info->super_bytenr);
1281 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1285 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1286 bytenr = btrfs_sb_offset(i);
1287 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1290 btrfs_set_super_bytenr(sb, bytenr);
1293 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1294 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1295 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1298 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1299 * zero filled, we can use it directly
1301 ret = pwrite64(device->fd, root->fs_info->super_copy,
1302 BTRFS_SUPER_INFO_SIZE, bytenr);
1303 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1309 int write_all_supers(struct btrfs_root *root)
1311 struct list_head *cur;
1312 struct list_head *head = &root->fs_info->fs_devices->devices;
1313 struct btrfs_device *dev;
1314 struct btrfs_super_block *sb;
1315 struct btrfs_dev_item *dev_item;
1319 sb = root->fs_info->super_copy;
1320 dev_item = &sb->dev_item;
1321 list_for_each(cur, head) {
1322 dev = list_entry(cur, struct btrfs_device, dev_list);
1323 if (!dev->writeable)
1326 btrfs_set_stack_device_generation(dev_item, 0);
1327 btrfs_set_stack_device_type(dev_item, dev->type);
1328 btrfs_set_stack_device_id(dev_item, dev->devid);
1329 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1330 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1331 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1332 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1333 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1334 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1335 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1337 flags = btrfs_super_flags(sb);
1338 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1340 ret = write_dev_supers(root, sb, dev);
1346 int write_ctree_super(struct btrfs_trans_handle *trans,
1347 struct btrfs_root *root)
1350 struct btrfs_root *tree_root = root->fs_info->tree_root;
1351 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1353 if (root->fs_info->readonly)
1356 btrfs_set_super_generation(root->fs_info->super_copy,
1358 btrfs_set_super_root(root->fs_info->super_copy,
1359 tree_root->node->start);
1360 btrfs_set_super_root_level(root->fs_info->super_copy,
1361 btrfs_header_level(tree_root->node));
1362 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1363 chunk_root->node->start);
1364 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1365 btrfs_header_level(chunk_root->node));
1366 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1367 btrfs_header_generation(chunk_root->node));
1369 ret = write_all_supers(root);
1371 fprintf(stderr, "failed to write new super block err %d\n", ret);
1375 int close_ctree(struct btrfs_root *root)
1378 struct btrfs_trans_handle *trans;
1379 struct btrfs_fs_info *fs_info = root->fs_info;
1381 if (fs_info->last_trans_committed !=
1382 fs_info->generation) {
1383 trans = btrfs_start_transaction(root, 1);
1384 btrfs_commit_transaction(trans, root);
1385 trans = btrfs_start_transaction(root, 1);
1386 ret = commit_tree_roots(trans, fs_info);
1388 ret = __commit_transaction(trans, root);
1390 write_ctree_super(trans, root);
1391 btrfs_free_transaction(root, trans);
1393 btrfs_free_block_groups(fs_info);
1395 free_fs_roots_tree(&fs_info->fs_root_tree);
1397 btrfs_release_all_roots(fs_info);
1398 btrfs_close_devices(fs_info->fs_devices);
1399 btrfs_cleanup_all_caches(fs_info);
1400 btrfs_free_fs_info(fs_info);
1404 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1405 struct extent_buffer *eb)
1407 return clear_extent_buffer_dirty(eb);
1410 int wait_on_tree_block_writeback(struct btrfs_root *root,
1411 struct extent_buffer *eb)
1416 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1418 set_extent_buffer_dirty(eb);
1421 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1425 ret = extent_buffer_uptodate(buf);
1429 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1433 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1435 return set_extent_buffer_uptodate(eb);