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 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
141 struct extent_buffer *eb;
143 struct btrfs_multi_bio *multi = NULL;
144 struct btrfs_device *device;
146 eb = btrfs_find_tree_block(root, bytenr, blocksize);
147 if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
148 free_extent_buffer(eb);
153 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
154 bytenr, &length, &multi, 0, NULL);
156 device = multi->stripes[0].dev;
158 blocksize = min(blocksize, (u32)(64 * 1024));
159 readahead(device->fd, multi->stripes[0].physical, blocksize);
164 static int verify_parent_transid(struct extent_io_tree *io_tree,
165 struct extent_buffer *eb, u64 parent_transid,
170 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
173 if (extent_buffer_uptodate(eb) &&
174 btrfs_header_generation(eb) == parent_transid) {
178 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
179 (unsigned long long)eb->start,
180 (unsigned long long)parent_transid,
181 (unsigned long long)btrfs_header_generation(eb));
183 eb->flags |= EXTENT_BAD_TRANSID;
184 printk("Ignoring transid failure\n");
190 clear_extent_buffer_uptodate(io_tree, eb);
196 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
198 unsigned long offset = 0;
199 struct btrfs_multi_bio *multi = NULL;
200 struct btrfs_device *device;
203 unsigned long bytes_left = eb->len;
206 read_len = bytes_left;
209 if (!info->on_restoring) {
210 ret = btrfs_map_block(&info->mapping_tree, READ,
211 eb->start + offset, &read_len, &multi,
214 printk("Couldn't map the block %Lu\n", eb->start + offset);
218 device = multi->stripes[0].dev;
220 if (device->fd == 0) {
227 eb->dev_bytenr = multi->stripes[0].physical;
231 /* special case for restore metadump */
232 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
233 if (device->devid == 1)
238 eb->dev_bytenr = eb->start;
242 if (read_len > bytes_left)
243 read_len = bytes_left;
245 ret = read_extent_from_disk(eb, offset, read_len);
249 bytes_left -= read_len;
254 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
255 u32 blocksize, u64 parent_transid)
258 struct extent_buffer *eb;
259 u64 best_transid = 0;
265 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
269 if (btrfs_buffer_uptodate(eb, parent_transid))
273 ret = read_whole_eb(root->fs_info, eb, mirror_num);
274 if (ret == 0 && check_tree_block(root, eb) == 0 &&
275 csum_tree_block(root, eb, 1) == 0 &&
276 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
278 if (eb->flags & EXTENT_BAD_TRANSID &&
279 list_empty(&eb->recow)) {
280 list_add_tail(&eb->recow,
281 &root->fs_info->recow_ebs);
284 btrfs_set_buffer_uptodate(eb);
288 if (check_tree_block(root, eb))
289 printk("read block failed check_tree_block\n");
291 printk("Csum didn't match\n");
294 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
296 if (num_copies == 1) {
300 if (btrfs_header_generation(eb) > best_transid) {
301 best_transid = btrfs_header_generation(eb);
302 good_mirror = mirror_num;
305 if (mirror_num > num_copies) {
306 mirror_num = good_mirror;
311 free_extent_buffer(eb);
315 static int write_tree_block(struct btrfs_trans_handle *trans,
316 struct btrfs_root *root,
317 struct extent_buffer *eb)
322 u64 *raid_map = NULL;
323 struct btrfs_multi_bio *multi = NULL;
325 if (check_tree_block(root, eb))
328 if (!btrfs_buffer_uptodate(eb, trans->transid))
331 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
332 csum_tree_block(root, eb, 0);
336 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
337 eb->start, &length, &multi, 0, &raid_map);
340 ret = write_raid56_with_parity(root->fs_info, eb, multi,
343 } else while (dev_nr < multi->num_stripes) {
345 eb->fd = multi->stripes[dev_nr].dev->fd;
346 eb->dev_bytenr = multi->stripes[dev_nr].physical;
347 multi->stripes[dev_nr].dev->total_ios++;
349 ret = write_extent_to_disk(eb);
356 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
357 u32 stripesize, struct btrfs_root *root,
358 struct btrfs_fs_info *fs_info, u64 objectid)
361 root->commit_root = NULL;
362 root->sectorsize = sectorsize;
363 root->nodesize = nodesize;
364 root->leafsize = leafsize;
365 root->stripesize = stripesize;
367 root->track_dirty = 0;
369 root->fs_info = fs_info;
370 root->objectid = objectid;
371 root->last_trans = 0;
372 root->highest_inode = 0;
373 root->last_inode_alloc = 0;
375 INIT_LIST_HEAD(&root->dirty_list);
376 memset(&root->root_key, 0, sizeof(root->root_key));
377 memset(&root->root_item, 0, sizeof(root->root_item));
378 root->root_key.objectid = objectid;
382 static int update_cowonly_root(struct btrfs_trans_handle *trans,
383 struct btrfs_root *root)
387 struct btrfs_root *tree_root = root->fs_info->tree_root;
389 btrfs_write_dirty_block_groups(trans, root);
391 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
392 if (old_root_bytenr == root->node->start)
394 btrfs_set_root_bytenr(&root->root_item,
396 btrfs_set_root_generation(&root->root_item,
398 root->root_item.level = btrfs_header_level(root->node);
399 ret = btrfs_update_root(trans, tree_root,
403 btrfs_write_dirty_block_groups(trans, root);
408 static int commit_tree_roots(struct btrfs_trans_handle *trans,
409 struct btrfs_fs_info *fs_info)
411 struct btrfs_root *root;
412 struct list_head *next;
413 struct extent_buffer *eb;
416 if (fs_info->readonly)
419 eb = fs_info->tree_root->node;
420 extent_buffer_get(eb);
421 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
422 free_extent_buffer(eb);
426 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
427 next = fs_info->dirty_cowonly_roots.next;
429 root = list_entry(next, struct btrfs_root, dirty_list);
430 update_cowonly_root(trans, root);
431 free_extent_buffer(root->commit_root);
432 root->commit_root = NULL;
438 static int __commit_transaction(struct btrfs_trans_handle *trans,
439 struct btrfs_root *root)
443 struct extent_buffer *eb;
444 struct extent_io_tree *tree = &root->fs_info->extent_cache;
448 ret = find_first_extent_bit(tree, 0, &start, &end,
452 while(start <= end) {
453 eb = find_first_extent_buffer(tree, start);
454 BUG_ON(!eb || eb->start != start);
455 ret = write_tree_block(trans, root, eb);
458 clear_extent_buffer_dirty(eb);
459 free_extent_buffer(eb);
465 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
466 struct btrfs_root *root)
468 u64 transid = trans->transid;
470 struct btrfs_fs_info *fs_info = root->fs_info;
472 if (root->commit_root == root->node)
475 free_extent_buffer(root->commit_root);
476 root->commit_root = NULL;
478 btrfs_set_root_bytenr(&root->root_item, root->node->start);
479 btrfs_set_root_generation(&root->root_item, trans->transid);
480 root->root_item.level = btrfs_header_level(root->node);
481 ret = btrfs_update_root(trans, root->fs_info->tree_root,
482 &root->root_key, &root->root_item);
485 ret = commit_tree_roots(trans, fs_info);
487 ret = __commit_transaction(trans, root);
489 write_ctree_super(trans, root);
490 btrfs_finish_extent_commit(trans, fs_info->extent_root,
491 &fs_info->pinned_extents);
492 btrfs_free_transaction(root, trans);
493 free_extent_buffer(root->commit_root);
494 root->commit_root = NULL;
495 fs_info->running_transaction = NULL;
496 fs_info->last_trans_committed = transid;
500 static int find_and_setup_root(struct btrfs_root *tree_root,
501 struct btrfs_fs_info *fs_info,
502 u64 objectid, struct btrfs_root *root)
508 __setup_root(tree_root->nodesize, tree_root->leafsize,
509 tree_root->sectorsize, tree_root->stripesize,
510 root, fs_info, objectid);
511 ret = btrfs_find_last_root(tree_root, objectid,
512 &root->root_item, &root->root_key);
516 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
517 generation = btrfs_root_generation(&root->root_item);
518 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
519 blocksize, generation);
520 if (!extent_buffer_uptodate(root->node))
526 static int find_and_setup_log_root(struct btrfs_root *tree_root,
527 struct btrfs_fs_info *fs_info,
528 struct btrfs_super_block *disk_super)
531 u64 blocknr = btrfs_super_log_root(disk_super);
532 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
542 blocksize = btrfs_level_size(tree_root,
543 btrfs_super_log_root_level(disk_super));
545 __setup_root(tree_root->nodesize, tree_root->leafsize,
546 tree_root->sectorsize, tree_root->stripesize,
547 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
549 log_root->node = read_tree_block(tree_root, blocknr,
551 btrfs_super_generation(disk_super) + 1);
553 fs_info->log_root_tree = log_root;
555 if (!extent_buffer_uptodate(log_root->node)) {
556 free_extent_buffer(log_root->node);
558 fs_info->log_root_tree = NULL;
566 int btrfs_free_fs_root(struct btrfs_root *root)
569 free_extent_buffer(root->node);
570 if (root->commit_root)
571 free_extent_buffer(root->commit_root);
576 static void __free_fs_root(struct rb_node *node)
578 struct btrfs_root *root;
580 root = container_of(node, struct btrfs_root, rb_node);
581 btrfs_free_fs_root(root);
584 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
586 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
587 struct btrfs_key *location)
589 struct btrfs_root *root;
590 struct btrfs_root *tree_root = fs_info->tree_root;
591 struct btrfs_path *path;
592 struct extent_buffer *l;
597 root = malloc(sizeof(*root));
599 return ERR_PTR(-ENOMEM);
600 memset(root, 0, sizeof(*root));
601 if (location->offset == (u64)-1) {
602 ret = find_and_setup_root(tree_root, fs_info,
603 location->objectid, root);
611 __setup_root(tree_root->nodesize, tree_root->leafsize,
612 tree_root->sectorsize, tree_root->stripesize,
613 root, fs_info, location->objectid);
615 path = btrfs_alloc_path();
617 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
624 read_extent_buffer(l, &root->root_item,
625 btrfs_item_ptr_offset(l, path->slots[0]),
626 sizeof(root->root_item));
627 memcpy(&root->root_key, location, sizeof(*location));
630 btrfs_release_path(path);
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);
646 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
649 u64 objectid = *((u64 *)data);
650 struct btrfs_root *root;
652 root = rb_entry(node, struct btrfs_root, rb_node);
653 if (objectid > root->objectid)
655 else if (objectid < root->objectid)
661 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
662 struct rb_node *node2)
664 struct btrfs_root *root;
666 root = rb_entry(node2, struct btrfs_root, rb_node);
667 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
670 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
671 struct btrfs_key *location)
673 struct btrfs_root *root;
674 struct rb_node *node;
677 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
678 return fs_info->tree_root;
679 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
680 return fs_info->extent_root;
681 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
682 return fs_info->chunk_root;
683 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
684 return fs_info->dev_root;
685 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
686 return fs_info->csum_root;
688 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
689 location->offset != (u64)-1);
691 node = rb_search(&fs_info->fs_root_tree, (void *)&location->objectid,
692 btrfs_fs_roots_compare_objectids, NULL);
694 return container_of(node, struct btrfs_root, rb_node);
696 root = btrfs_read_fs_root_no_cache(fs_info, location);
700 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
701 btrfs_fs_roots_compare_roots);
706 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
708 free(fs_info->tree_root);
709 free(fs_info->extent_root);
710 free(fs_info->chunk_root);
711 free(fs_info->dev_root);
712 free(fs_info->csum_root);
713 free(fs_info->super_copy);
714 free(fs_info->log_root_tree);
718 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
720 struct btrfs_fs_info *fs_info;
722 fs_info = malloc(sizeof(struct btrfs_fs_info));
726 memset(fs_info, 0, sizeof(struct btrfs_fs_info));
728 fs_info->tree_root = malloc(sizeof(struct btrfs_root));
729 fs_info->extent_root = malloc(sizeof(struct btrfs_root));
730 fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
731 fs_info->dev_root = malloc(sizeof(struct btrfs_root));
732 fs_info->csum_root = malloc(sizeof(struct btrfs_root));
733 fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
735 if (!fs_info->tree_root || !fs_info->extent_root ||
736 !fs_info->chunk_root || !fs_info->dev_root ||
737 !fs_info->csum_root || !fs_info->super_copy)
740 memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
741 memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
742 memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
743 memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
744 memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
745 memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
747 extent_io_tree_init(&fs_info->extent_cache);
748 extent_io_tree_init(&fs_info->free_space_cache);
749 extent_io_tree_init(&fs_info->block_group_cache);
750 extent_io_tree_init(&fs_info->pinned_extents);
751 extent_io_tree_init(&fs_info->pending_del);
752 extent_io_tree_init(&fs_info->extent_ins);
753 fs_info->fs_root_tree = RB_ROOT;
754 cache_tree_init(&fs_info->mapping_tree.cache_tree);
756 mutex_init(&fs_info->fs_mutex);
757 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
758 INIT_LIST_HEAD(&fs_info->space_info);
759 INIT_LIST_HEAD(&fs_info->recow_ebs);
762 fs_info->readonly = 1;
764 fs_info->super_bytenr = sb_bytenr;
765 fs_info->data_alloc_profile = (u64)-1;
766 fs_info->metadata_alloc_profile = (u64)-1;
767 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
770 btrfs_free_fs_info(fs_info);
774 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
778 features = btrfs_super_incompat_flags(sb) &
779 ~BTRFS_FEATURE_INCOMPAT_SUPP;
781 printk("couldn't open because of unsupported "
782 "option features (%Lx).\n",
783 (unsigned long long)features);
787 features = btrfs_super_incompat_flags(sb);
788 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
789 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
790 btrfs_set_super_incompat_flags(sb, features);
793 features = btrfs_super_compat_ro_flags(sb) &
794 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
795 if (writable && features) {
796 printk("couldn't open RDWR because of unsupported "
797 "option features (%Lx).\n",
798 (unsigned long long)features);
804 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info,
805 u64 root_tree_bytenr, int partial)
807 struct btrfs_super_block *sb = fs_info->super_copy;
808 struct btrfs_root *root;
809 struct btrfs_key key;
818 nodesize = btrfs_super_nodesize(sb);
819 leafsize = btrfs_super_leafsize(sb);
820 sectorsize = btrfs_super_sectorsize(sb);
821 stripesize = btrfs_super_stripesize(sb);
823 root = fs_info->tree_root;
824 __setup_root(nodesize, leafsize, sectorsize, stripesize,
825 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
826 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
827 generation = btrfs_super_generation(sb);
829 if (!root_tree_bytenr)
830 root_tree_bytenr = btrfs_super_root(sb);
831 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
833 if (!extent_buffer_uptodate(root->node)) {
834 fprintf(stderr, "Couldn't read tree root\n");
838 ret = find_and_setup_root(root, fs_info, BTRFS_EXTENT_TREE_OBJECTID,
839 fs_info->extent_root);
841 printk("Couldn't setup extent tree\n");
844 fs_info->extent_root->track_dirty = 1;
846 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
849 printk("Couldn't setup device tree\n");
852 fs_info->dev_root->track_dirty = 1;
854 ret = find_and_setup_root(root, fs_info, BTRFS_CSUM_TREE_OBJECTID,
857 printk("Couldn't setup csum tree\n");
861 fs_info->csum_root->track_dirty = 1;
863 ret = find_and_setup_log_root(root, fs_info, sb);
865 printk("Couldn't setup log root tree\n");
869 fs_info->generation = generation;
870 fs_info->last_trans_committed = generation;
871 btrfs_read_block_groups(fs_info->tree_root);
873 key.objectid = BTRFS_FS_TREE_OBJECTID;
874 key.type = BTRFS_ROOT_ITEM_KEY;
875 key.offset = (u64)-1;
876 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
878 if (!fs_info->fs_root)
883 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
885 if (fs_info->csum_root)
886 free_extent_buffer(fs_info->csum_root->node);
887 if (fs_info->dev_root)
888 free_extent_buffer(fs_info->dev_root->node);
889 if (fs_info->extent_root)
890 free_extent_buffer(fs_info->extent_root->node);
891 if (fs_info->tree_root)
892 free_extent_buffer(fs_info->tree_root->node);
893 if (fs_info->log_root_tree)
894 free_extent_buffer(fs_info->log_root_tree->node);
895 if (fs_info->chunk_root)
896 free_extent_buffer(fs_info->chunk_root->node);
899 static void free_map_lookup(struct cache_extent *ce)
901 struct map_lookup *map;
903 map = container_of(ce, struct map_lookup, ce);
907 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
909 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
911 while (!list_empty(&fs_info->recow_ebs)) {
912 struct extent_buffer *eb;
913 eb = list_first_entry(&fs_info->recow_ebs,
914 struct extent_buffer, recow);
915 list_del_init(&eb->recow);
916 free_extent_buffer(eb);
918 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
919 extent_io_tree_cleanup(&fs_info->extent_cache);
920 extent_io_tree_cleanup(&fs_info->free_space_cache);
921 extent_io_tree_cleanup(&fs_info->block_group_cache);
922 extent_io_tree_cleanup(&fs_info->pinned_extents);
923 extent_io_tree_cleanup(&fs_info->pending_del);
924 extent_io_tree_cleanup(&fs_info->extent_ins);
927 int btrfs_scan_fs_devices(int fd, const char *path,
928 struct btrfs_fs_devices **fs_devices,
929 u64 sb_bytenr, int run_ioctl)
934 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
936 ret = btrfs_scan_one_device(fd, path, fs_devices,
937 &total_devs, sb_bytenr);
939 fprintf(stderr, "No valid Btrfs found on %s\n", path);
943 if (total_devs != 1) {
944 ret = btrfs_scan_for_fsid(run_ioctl);
951 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
953 struct btrfs_super_block *sb = fs_info->super_copy;
962 nodesize = btrfs_super_nodesize(sb);
963 leafsize = btrfs_super_leafsize(sb);
964 sectorsize = btrfs_super_sectorsize(sb);
965 stripesize = btrfs_super_stripesize(sb);
967 __setup_root(nodesize, leafsize, sectorsize, stripesize,
968 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
970 ret = btrfs_read_sys_array(fs_info->chunk_root);
974 blocksize = btrfs_level_size(fs_info->chunk_root,
975 btrfs_super_chunk_root_level(sb));
976 generation = btrfs_super_chunk_root_generation(sb);
978 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
979 btrfs_super_chunk_root(sb),
980 blocksize, generation);
981 if (!fs_info->chunk_root->node ||
982 !extent_buffer_uptodate(fs_info->chunk_root->node)) {
983 fprintf(stderr, "Couldn't read chunk root\n");
987 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
988 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
990 fprintf(stderr, "Couldn't read chunk tree\n");
997 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
999 u64 root_tree_bytenr, int writes,
1000 int partial, int restore,
1003 struct btrfs_fs_info *fs_info;
1004 struct btrfs_super_block *disk_super;
1005 struct btrfs_fs_devices *fs_devices = NULL;
1006 struct extent_buffer *eb;
1010 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1012 /* try to drop all the caches */
1013 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1014 fprintf(stderr, "Warning, could not drop caches\n");
1016 fs_info = btrfs_new_fs_info(writes, sb_bytenr);
1018 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1022 fs_info->on_restoring = 1;
1024 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1029 fs_info->fs_devices = fs_devices;
1031 ret = btrfs_open_devices(fs_devices, O_RDWR);
1033 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1038 disk_super = fs_info->super_copy;
1040 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1041 disk_super, sb_bytenr);
1043 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr);
1045 printk("No valid btrfs found\n");
1049 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1051 ret = btrfs_check_fs_compatibility(fs_info->super_copy, writes);
1055 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1059 eb = fs_info->chunk_root->node;
1060 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1061 (unsigned long)btrfs_header_chunk_tree_uuid(eb),
1064 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, partial);
1074 btrfs_release_all_roots(fs_info);
1075 btrfs_cleanup_all_caches(fs_info);
1077 btrfs_close_devices(fs_devices);
1079 btrfs_free_fs_info(fs_info);
1083 struct btrfs_fs_info *open_ctree_fs_info_restore(const char *filename,
1084 u64 sb_bytenr, u64 root_tree_bytenr,
1085 int writes, int partial)
1088 struct btrfs_fs_info *info;
1089 int flags = O_CREAT | O_RDWR;
1095 fp = open(filename, flags, 0600);
1097 fprintf (stderr, "Could not open %s\n", filename);
1100 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1101 writes, partial, restore, 0);
1106 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1107 u64 sb_bytenr, u64 root_tree_bytenr,
1108 int writes, int partial)
1111 struct btrfs_fs_info *info;
1112 int flags = O_CREAT | O_RDWR;
1117 fp = open(filename, flags, 0600);
1119 fprintf (stderr, "Could not open %s\n", filename);
1122 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1123 writes, partial, 0, 0);
1128 struct btrfs_root *open_ctree_with_broken_super(const char *filename,
1129 u64 sb_bytenr, int writes)
1132 struct btrfs_fs_info *info;
1133 int flags = O_CREAT | O_RDWR;
1138 fp = open(filename, flags, 0600);
1140 fprintf(stderr, "Could not open %s\n", filename);
1143 info = __open_ctree_fd(fp, filename, sb_bytenr, 0,
1147 return info->fs_root;
1151 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
1153 struct btrfs_fs_info *info;
1155 info = open_ctree_fs_info(filename, sb_bytenr, 0, writes, 0);
1158 return info->fs_root;
1161 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1164 struct btrfs_fs_info *info;
1165 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0, 0, 0);
1168 return info->fs_root;
1171 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
1173 u8 fsid[BTRFS_FSID_SIZE];
1174 int fsid_is_initialized = 0;
1175 struct btrfs_super_block buf;
1181 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1182 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1183 if (ret < sizeof(buf))
1186 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1187 btrfs_super_magic(&buf) != BTRFS_MAGIC)
1190 memcpy(sb, &buf, sizeof(*sb));
1194 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1195 bytenr = btrfs_sb_offset(i);
1196 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1197 if (ret < sizeof(buf))
1200 if (btrfs_super_bytenr(&buf) != bytenr )
1202 /* if magic is NULL, the device was removed */
1203 if (btrfs_super_magic(&buf) == 0 && i == 0)
1205 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1208 if (!fsid_is_initialized) {
1209 memcpy(fsid, buf.fsid, sizeof(fsid));
1210 fsid_is_initialized = 1;
1211 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1213 * the superblocks (the original one and
1214 * its backups) contain data of different
1215 * filesystems -> the super cannot be trusted
1220 if (btrfs_super_generation(&buf) > transid) {
1221 memcpy(sb, &buf, sizeof(*sb));
1222 transid = btrfs_super_generation(&buf);
1226 return transid > 0 ? 0 : -1;
1229 static int write_dev_supers(struct btrfs_root *root,
1230 struct btrfs_super_block *sb,
1231 struct btrfs_device *device)
1237 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1238 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1240 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1241 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1242 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1245 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1246 * zero filled, we can use it directly
1248 ret = pwrite64(device->fd, root->fs_info->super_copy,
1249 BTRFS_SUPER_INFO_SIZE,
1250 root->fs_info->super_bytenr);
1251 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1255 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1256 bytenr = btrfs_sb_offset(i);
1257 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1260 btrfs_set_super_bytenr(sb, bytenr);
1263 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1264 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1265 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1268 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1269 * zero filled, we can use it directly
1271 ret = pwrite64(device->fd, root->fs_info->super_copy,
1272 BTRFS_SUPER_INFO_SIZE, bytenr);
1273 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1279 int write_all_supers(struct btrfs_root *root)
1281 struct list_head *cur;
1282 struct list_head *head = &root->fs_info->fs_devices->devices;
1283 struct btrfs_device *dev;
1284 struct btrfs_super_block *sb;
1285 struct btrfs_dev_item *dev_item;
1289 sb = root->fs_info->super_copy;
1290 dev_item = &sb->dev_item;
1291 list_for_each(cur, head) {
1292 dev = list_entry(cur, struct btrfs_device, dev_list);
1293 if (!dev->writeable)
1296 btrfs_set_stack_device_generation(dev_item, 0);
1297 btrfs_set_stack_device_type(dev_item, dev->type);
1298 btrfs_set_stack_device_id(dev_item, dev->devid);
1299 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1300 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1301 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1302 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1303 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1304 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1305 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1307 flags = btrfs_super_flags(sb);
1308 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1310 ret = write_dev_supers(root, sb, dev);
1316 int write_ctree_super(struct btrfs_trans_handle *trans,
1317 struct btrfs_root *root)
1320 struct btrfs_root *tree_root = root->fs_info->tree_root;
1321 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1323 if (root->fs_info->readonly)
1326 btrfs_set_super_generation(root->fs_info->super_copy,
1328 btrfs_set_super_root(root->fs_info->super_copy,
1329 tree_root->node->start);
1330 btrfs_set_super_root_level(root->fs_info->super_copy,
1331 btrfs_header_level(tree_root->node));
1332 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1333 chunk_root->node->start);
1334 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1335 btrfs_header_level(chunk_root->node));
1336 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1337 btrfs_header_generation(chunk_root->node));
1339 ret = write_all_supers(root);
1341 fprintf(stderr, "failed to write new super block err %d\n", ret);
1345 int close_ctree(struct btrfs_root *root)
1348 struct btrfs_trans_handle *trans;
1349 struct btrfs_fs_info *fs_info = root->fs_info;
1351 if (fs_info->last_trans_committed !=
1352 fs_info->generation) {
1353 trans = btrfs_start_transaction(root, 1);
1354 btrfs_commit_transaction(trans, root);
1355 trans = btrfs_start_transaction(root, 1);
1356 ret = commit_tree_roots(trans, fs_info);
1358 ret = __commit_transaction(trans, root);
1360 write_ctree_super(trans, root);
1361 btrfs_free_transaction(root, trans);
1363 btrfs_free_block_groups(fs_info);
1365 free_fs_roots_tree(&fs_info->fs_root_tree);
1367 btrfs_release_all_roots(fs_info);
1368 btrfs_close_devices(fs_info->fs_devices);
1369 btrfs_cleanup_all_caches(fs_info);
1370 btrfs_free_fs_info(fs_info);
1374 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1375 struct extent_buffer *eb)
1377 return clear_extent_buffer_dirty(eb);
1380 int wait_on_tree_block_writeback(struct btrfs_root *root,
1381 struct extent_buffer *eb)
1386 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1388 set_extent_buffer_dirty(eb);
1391 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1395 ret = extent_buffer_uptodate(buf);
1399 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1403 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1405 return set_extent_buffer_uptodate(eb);