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 int write_and_map_eb(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;
327 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
328 eb->start, &length, &multi, 0, &raid_map);
331 ret = write_raid56_with_parity(root->fs_info, eb, multi,
334 } else while (dev_nr < multi->num_stripes) {
336 eb->fd = multi->stripes[dev_nr].dev->fd;
337 eb->dev_bytenr = multi->stripes[dev_nr].physical;
338 multi->stripes[dev_nr].dev->total_ios++;
340 ret = write_extent_to_disk(eb);
347 int write_tree_block(struct btrfs_trans_handle *trans,
348 struct btrfs_root *root,
349 struct extent_buffer *eb)
351 if (check_tree_block(root, eb))
354 if (!btrfs_buffer_uptodate(eb, trans->transid))
357 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
358 csum_tree_block(root, eb, 0);
360 return write_and_map_eb(trans, root, eb);
363 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
364 u32 stripesize, struct btrfs_root *root,
365 struct btrfs_fs_info *fs_info, u64 objectid)
368 root->commit_root = NULL;
369 root->sectorsize = sectorsize;
370 root->nodesize = nodesize;
371 root->leafsize = leafsize;
372 root->stripesize = stripesize;
374 root->track_dirty = 0;
376 root->fs_info = fs_info;
377 root->objectid = objectid;
378 root->last_trans = 0;
379 root->highest_inode = 0;
380 root->last_inode_alloc = 0;
382 INIT_LIST_HEAD(&root->dirty_list);
383 memset(&root->root_key, 0, sizeof(root->root_key));
384 memset(&root->root_item, 0, sizeof(root->root_item));
385 root->root_key.objectid = objectid;
389 static int update_cowonly_root(struct btrfs_trans_handle *trans,
390 struct btrfs_root *root)
394 struct btrfs_root *tree_root = root->fs_info->tree_root;
396 btrfs_write_dirty_block_groups(trans, root);
398 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
399 if (old_root_bytenr == root->node->start)
401 btrfs_set_root_bytenr(&root->root_item,
403 btrfs_set_root_generation(&root->root_item,
405 root->root_item.level = btrfs_header_level(root->node);
406 ret = btrfs_update_root(trans, tree_root,
410 btrfs_write_dirty_block_groups(trans, root);
415 static int commit_tree_roots(struct btrfs_trans_handle *trans,
416 struct btrfs_fs_info *fs_info)
418 struct btrfs_root *root;
419 struct list_head *next;
420 struct extent_buffer *eb;
423 if (fs_info->readonly)
426 eb = fs_info->tree_root->node;
427 extent_buffer_get(eb);
428 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
429 free_extent_buffer(eb);
433 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
434 next = fs_info->dirty_cowonly_roots.next;
436 root = list_entry(next, struct btrfs_root, dirty_list);
437 update_cowonly_root(trans, root);
438 free_extent_buffer(root->commit_root);
439 root->commit_root = NULL;
445 static int __commit_transaction(struct btrfs_trans_handle *trans,
446 struct btrfs_root *root)
450 struct extent_buffer *eb;
451 struct extent_io_tree *tree = &root->fs_info->extent_cache;
455 ret = find_first_extent_bit(tree, 0, &start, &end,
459 while(start <= end) {
460 eb = find_first_extent_buffer(tree, start);
461 BUG_ON(!eb || eb->start != start);
462 ret = write_tree_block(trans, root, eb);
465 clear_extent_buffer_dirty(eb);
466 free_extent_buffer(eb);
472 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
473 struct btrfs_root *root)
475 u64 transid = trans->transid;
477 struct btrfs_fs_info *fs_info = root->fs_info;
479 if (root->commit_root == root->node)
482 free_extent_buffer(root->commit_root);
483 root->commit_root = NULL;
485 btrfs_set_root_bytenr(&root->root_item, root->node->start);
486 btrfs_set_root_generation(&root->root_item, trans->transid);
487 root->root_item.level = btrfs_header_level(root->node);
488 ret = btrfs_update_root(trans, root->fs_info->tree_root,
489 &root->root_key, &root->root_item);
492 ret = commit_tree_roots(trans, fs_info);
494 ret = __commit_transaction(trans, root);
496 write_ctree_super(trans, root);
497 btrfs_finish_extent_commit(trans, fs_info->extent_root,
498 &fs_info->pinned_extents);
499 btrfs_free_transaction(root, trans);
500 free_extent_buffer(root->commit_root);
501 root->commit_root = NULL;
502 fs_info->running_transaction = NULL;
503 fs_info->last_trans_committed = transid;
507 static int find_and_setup_root(struct btrfs_root *tree_root,
508 struct btrfs_fs_info *fs_info,
509 u64 objectid, struct btrfs_root *root)
515 __setup_root(tree_root->nodesize, tree_root->leafsize,
516 tree_root->sectorsize, tree_root->stripesize,
517 root, fs_info, objectid);
518 ret = btrfs_find_last_root(tree_root, objectid,
519 &root->root_item, &root->root_key);
523 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
524 generation = btrfs_root_generation(&root->root_item);
525 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
526 blocksize, generation);
527 if (!extent_buffer_uptodate(root->node))
533 static int find_and_setup_log_root(struct btrfs_root *tree_root,
534 struct btrfs_fs_info *fs_info,
535 struct btrfs_super_block *disk_super)
538 u64 blocknr = btrfs_super_log_root(disk_super);
539 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
549 blocksize = btrfs_level_size(tree_root,
550 btrfs_super_log_root_level(disk_super));
552 __setup_root(tree_root->nodesize, tree_root->leafsize,
553 tree_root->sectorsize, tree_root->stripesize,
554 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
556 log_root->node = read_tree_block(tree_root, blocknr,
558 btrfs_super_generation(disk_super) + 1);
560 fs_info->log_root_tree = log_root;
562 if (!extent_buffer_uptodate(log_root->node)) {
563 free_extent_buffer(log_root->node);
565 fs_info->log_root_tree = NULL;
573 int btrfs_free_fs_root(struct btrfs_root *root)
576 free_extent_buffer(root->node);
577 if (root->commit_root)
578 free_extent_buffer(root->commit_root);
583 static void __free_fs_root(struct rb_node *node)
585 struct btrfs_root *root;
587 root = container_of(node, struct btrfs_root, rb_node);
588 btrfs_free_fs_root(root);
591 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
593 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
594 struct btrfs_key *location)
596 struct btrfs_root *root;
597 struct btrfs_root *tree_root = fs_info->tree_root;
598 struct btrfs_path *path;
599 struct extent_buffer *l;
604 root = malloc(sizeof(*root));
606 return ERR_PTR(-ENOMEM);
607 memset(root, 0, sizeof(*root));
608 if (location->offset == (u64)-1) {
609 ret = find_and_setup_root(tree_root, fs_info,
610 location->objectid, root);
618 __setup_root(tree_root->nodesize, tree_root->leafsize,
619 tree_root->sectorsize, tree_root->stripesize,
620 root, fs_info, location->objectid);
622 path = btrfs_alloc_path();
624 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
631 read_extent_buffer(l, &root->root_item,
632 btrfs_item_ptr_offset(l, path->slots[0]),
633 sizeof(root->root_item));
634 memcpy(&root->root_key, location, sizeof(*location));
637 btrfs_release_path(path);
638 btrfs_free_path(path);
643 generation = btrfs_root_generation(&root->root_item);
644 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
645 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
646 blocksize, generation);
653 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
656 u64 objectid = *((u64 *)data);
657 struct btrfs_root *root;
659 root = rb_entry(node, struct btrfs_root, rb_node);
660 if (objectid > root->objectid)
662 else if (objectid < root->objectid)
668 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
669 struct rb_node *node2)
671 struct btrfs_root *root;
673 root = rb_entry(node2, struct btrfs_root, rb_node);
674 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
677 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
678 struct btrfs_key *location)
680 struct btrfs_root *root;
681 struct rb_node *node;
684 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
685 return fs_info->tree_root;
686 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
687 return fs_info->extent_root;
688 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
689 return fs_info->chunk_root;
690 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
691 return fs_info->dev_root;
692 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
693 return fs_info->csum_root;
695 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
696 location->offset != (u64)-1);
698 node = rb_search(&fs_info->fs_root_tree, (void *)&location->objectid,
699 btrfs_fs_roots_compare_objectids, NULL);
701 return container_of(node, struct btrfs_root, rb_node);
703 root = btrfs_read_fs_root_no_cache(fs_info, location);
707 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
708 btrfs_fs_roots_compare_roots);
713 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
715 free(fs_info->tree_root);
716 free(fs_info->extent_root);
717 free(fs_info->chunk_root);
718 free(fs_info->dev_root);
719 free(fs_info->csum_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->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
742 if (!fs_info->tree_root || !fs_info->extent_root ||
743 !fs_info->chunk_root || !fs_info->dev_root ||
744 !fs_info->csum_root || !fs_info->super_copy)
747 memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
748 memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
749 memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
750 memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
751 memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
752 memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
754 extent_io_tree_init(&fs_info->extent_cache);
755 extent_io_tree_init(&fs_info->free_space_cache);
756 extent_io_tree_init(&fs_info->block_group_cache);
757 extent_io_tree_init(&fs_info->pinned_extents);
758 extent_io_tree_init(&fs_info->pending_del);
759 extent_io_tree_init(&fs_info->extent_ins);
760 fs_info->fs_root_tree = RB_ROOT;
761 cache_tree_init(&fs_info->mapping_tree.cache_tree);
763 mutex_init(&fs_info->fs_mutex);
764 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
765 INIT_LIST_HEAD(&fs_info->space_info);
766 INIT_LIST_HEAD(&fs_info->recow_ebs);
769 fs_info->readonly = 1;
771 fs_info->super_bytenr = sb_bytenr;
772 fs_info->data_alloc_profile = (u64)-1;
773 fs_info->metadata_alloc_profile = (u64)-1;
774 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
777 btrfs_free_fs_info(fs_info);
781 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
785 features = btrfs_super_incompat_flags(sb) &
786 ~BTRFS_FEATURE_INCOMPAT_SUPP;
788 printk("couldn't open because of unsupported "
789 "option features (%Lx).\n",
790 (unsigned long long)features);
794 features = btrfs_super_incompat_flags(sb);
795 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
796 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
797 btrfs_set_super_incompat_flags(sb, features);
800 features = btrfs_super_compat_ro_flags(sb) &
801 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
802 if (writable && features) {
803 printk("couldn't open RDWR because of unsupported "
804 "option features (%Lx).\n",
805 (unsigned long long)features);
811 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info,
812 u64 root_tree_bytenr, int partial)
814 struct btrfs_super_block *sb = fs_info->super_copy;
815 struct btrfs_root *root;
816 struct btrfs_key key;
825 nodesize = btrfs_super_nodesize(sb);
826 leafsize = btrfs_super_leafsize(sb);
827 sectorsize = btrfs_super_sectorsize(sb);
828 stripesize = btrfs_super_stripesize(sb);
830 root = fs_info->tree_root;
831 __setup_root(nodesize, leafsize, sectorsize, stripesize,
832 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
833 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
834 generation = btrfs_super_generation(sb);
836 if (!root_tree_bytenr)
837 root_tree_bytenr = btrfs_super_root(sb);
838 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
840 if (!extent_buffer_uptodate(root->node)) {
841 fprintf(stderr, "Couldn't read tree root\n");
845 ret = find_and_setup_root(root, fs_info, BTRFS_EXTENT_TREE_OBJECTID,
846 fs_info->extent_root);
848 printk("Couldn't setup extent tree\n");
851 fs_info->extent_root->track_dirty = 1;
853 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
856 printk("Couldn't setup device tree\n");
859 fs_info->dev_root->track_dirty = 1;
861 ret = find_and_setup_root(root, fs_info, BTRFS_CSUM_TREE_OBJECTID,
864 printk("Couldn't setup csum tree\n");
868 fs_info->csum_root->track_dirty = 1;
870 ret = find_and_setup_log_root(root, fs_info, sb);
872 printk("Couldn't setup log root tree\n");
876 fs_info->generation = generation;
877 fs_info->last_trans_committed = generation;
878 btrfs_read_block_groups(fs_info->tree_root);
880 key.objectid = BTRFS_FS_TREE_OBJECTID;
881 key.type = BTRFS_ROOT_ITEM_KEY;
882 key.offset = (u64)-1;
883 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
885 if (!fs_info->fs_root)
890 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
892 if (fs_info->csum_root)
893 free_extent_buffer(fs_info->csum_root->node);
894 if (fs_info->dev_root)
895 free_extent_buffer(fs_info->dev_root->node);
896 if (fs_info->extent_root)
897 free_extent_buffer(fs_info->extent_root->node);
898 if (fs_info->tree_root)
899 free_extent_buffer(fs_info->tree_root->node);
900 if (fs_info->log_root_tree)
901 free_extent_buffer(fs_info->log_root_tree->node);
902 if (fs_info->chunk_root)
903 free_extent_buffer(fs_info->chunk_root->node);
906 static void free_map_lookup(struct cache_extent *ce)
908 struct map_lookup *map;
910 map = container_of(ce, struct map_lookup, ce);
914 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
916 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
918 while (!list_empty(&fs_info->recow_ebs)) {
919 struct extent_buffer *eb;
920 eb = list_first_entry(&fs_info->recow_ebs,
921 struct extent_buffer, recow);
922 list_del_init(&eb->recow);
923 free_extent_buffer(eb);
925 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
926 extent_io_tree_cleanup(&fs_info->extent_cache);
927 extent_io_tree_cleanup(&fs_info->free_space_cache);
928 extent_io_tree_cleanup(&fs_info->block_group_cache);
929 extent_io_tree_cleanup(&fs_info->pinned_extents);
930 extent_io_tree_cleanup(&fs_info->pending_del);
931 extent_io_tree_cleanup(&fs_info->extent_ins);
934 int btrfs_scan_fs_devices(int fd, const char *path,
935 struct btrfs_fs_devices **fs_devices,
936 u64 sb_bytenr, int run_ioctl)
941 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
943 ret = btrfs_scan_one_device(fd, path, fs_devices,
944 &total_devs, sb_bytenr);
946 fprintf(stderr, "No valid Btrfs found on %s\n", path);
950 if (total_devs != 1) {
951 ret = btrfs_scan_for_fsid(run_ioctl);
958 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
960 struct btrfs_super_block *sb = fs_info->super_copy;
969 nodesize = btrfs_super_nodesize(sb);
970 leafsize = btrfs_super_leafsize(sb);
971 sectorsize = btrfs_super_sectorsize(sb);
972 stripesize = btrfs_super_stripesize(sb);
974 __setup_root(nodesize, leafsize, sectorsize, stripesize,
975 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
977 ret = btrfs_read_sys_array(fs_info->chunk_root);
981 blocksize = btrfs_level_size(fs_info->chunk_root,
982 btrfs_super_chunk_root_level(sb));
983 generation = btrfs_super_chunk_root_generation(sb);
985 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
986 btrfs_super_chunk_root(sb),
987 blocksize, generation);
988 if (!fs_info->chunk_root->node ||
989 !extent_buffer_uptodate(fs_info->chunk_root->node)) {
990 fprintf(stderr, "Couldn't read chunk root\n");
994 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
995 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
997 fprintf(stderr, "Couldn't read chunk tree\n");
1004 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1006 u64 root_tree_bytenr, int writes,
1007 int partial, int restore,
1010 struct btrfs_fs_info *fs_info;
1011 struct btrfs_super_block *disk_super;
1012 struct btrfs_fs_devices *fs_devices = NULL;
1013 struct extent_buffer *eb;
1017 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1019 /* try to drop all the caches */
1020 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1021 fprintf(stderr, "Warning, could not drop caches\n");
1023 fs_info = btrfs_new_fs_info(writes, sb_bytenr);
1025 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1029 fs_info->on_restoring = 1;
1031 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1036 fs_info->fs_devices = fs_devices;
1038 ret = btrfs_open_devices(fs_devices, O_RDWR);
1040 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1045 disk_super = fs_info->super_copy;
1047 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1048 disk_super, sb_bytenr);
1050 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr);
1052 printk("No valid btrfs found\n");
1056 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1058 ret = btrfs_check_fs_compatibility(fs_info->super_copy, writes);
1062 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1066 eb = fs_info->chunk_root->node;
1067 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1068 (unsigned long)btrfs_header_chunk_tree_uuid(eb),
1071 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, partial);
1081 btrfs_release_all_roots(fs_info);
1082 btrfs_cleanup_all_caches(fs_info);
1084 btrfs_close_devices(fs_devices);
1086 btrfs_free_fs_info(fs_info);
1090 struct btrfs_fs_info *open_ctree_fs_info_restore(const char *filename,
1091 u64 sb_bytenr, u64 root_tree_bytenr,
1092 int writes, int partial)
1095 struct btrfs_fs_info *info;
1096 int flags = O_CREAT | O_RDWR;
1102 fp = open(filename, flags, 0600);
1104 fprintf (stderr, "Could not open %s\n", filename);
1107 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1108 writes, partial, restore, 0);
1113 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1114 u64 sb_bytenr, u64 root_tree_bytenr,
1115 int writes, int partial)
1118 struct btrfs_fs_info *info;
1119 int flags = O_CREAT | O_RDWR;
1124 fp = open(filename, flags, 0600);
1126 fprintf (stderr, "Could not open %s\n", filename);
1129 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1130 writes, partial, 0, 0);
1135 struct btrfs_root *open_ctree_with_broken_super(const char *filename,
1136 u64 sb_bytenr, int writes)
1139 struct btrfs_fs_info *info;
1140 int flags = O_CREAT | O_RDWR;
1145 fp = open(filename, flags, 0600);
1147 fprintf(stderr, "Could not open %s\n", filename);
1150 info = __open_ctree_fd(fp, filename, sb_bytenr, 0,
1154 return info->fs_root;
1158 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
1160 struct btrfs_fs_info *info;
1162 info = open_ctree_fs_info(filename, sb_bytenr, 0, writes, 0);
1165 return info->fs_root;
1168 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1171 struct btrfs_fs_info *info;
1172 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0, 0, 0);
1175 return info->fs_root;
1178 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
1180 u8 fsid[BTRFS_FSID_SIZE];
1181 int fsid_is_initialized = 0;
1182 struct btrfs_super_block buf;
1188 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1189 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1190 if (ret < sizeof(buf))
1193 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1194 btrfs_super_magic(&buf) != BTRFS_MAGIC)
1197 memcpy(sb, &buf, sizeof(*sb));
1201 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1202 bytenr = btrfs_sb_offset(i);
1203 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1204 if (ret < sizeof(buf))
1207 if (btrfs_super_bytenr(&buf) != bytenr )
1209 /* if magic is NULL, the device was removed */
1210 if (btrfs_super_magic(&buf) == 0 && i == 0)
1212 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1215 if (!fsid_is_initialized) {
1216 memcpy(fsid, buf.fsid, sizeof(fsid));
1217 fsid_is_initialized = 1;
1218 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1220 * the superblocks (the original one and
1221 * its backups) contain data of different
1222 * filesystems -> the super cannot be trusted
1227 if (btrfs_super_generation(&buf) > transid) {
1228 memcpy(sb, &buf, sizeof(*sb));
1229 transid = btrfs_super_generation(&buf);
1233 return transid > 0 ? 0 : -1;
1236 static int write_dev_supers(struct btrfs_root *root,
1237 struct btrfs_super_block *sb,
1238 struct btrfs_device *device)
1244 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1245 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1247 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1248 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1249 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1252 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1253 * zero filled, we can use it directly
1255 ret = pwrite64(device->fd, root->fs_info->super_copy,
1256 BTRFS_SUPER_INFO_SIZE,
1257 root->fs_info->super_bytenr);
1258 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1262 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1263 bytenr = btrfs_sb_offset(i);
1264 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1267 btrfs_set_super_bytenr(sb, 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, bytenr);
1280 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1286 int write_all_supers(struct btrfs_root *root)
1288 struct list_head *cur;
1289 struct list_head *head = &root->fs_info->fs_devices->devices;
1290 struct btrfs_device *dev;
1291 struct btrfs_super_block *sb;
1292 struct btrfs_dev_item *dev_item;
1296 sb = root->fs_info->super_copy;
1297 dev_item = &sb->dev_item;
1298 list_for_each(cur, head) {
1299 dev = list_entry(cur, struct btrfs_device, dev_list);
1300 if (!dev->writeable)
1303 btrfs_set_stack_device_generation(dev_item, 0);
1304 btrfs_set_stack_device_type(dev_item, dev->type);
1305 btrfs_set_stack_device_id(dev_item, dev->devid);
1306 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1307 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1308 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1309 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1310 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1311 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1312 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1314 flags = btrfs_super_flags(sb);
1315 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1317 ret = write_dev_supers(root, sb, dev);
1323 int write_ctree_super(struct btrfs_trans_handle *trans,
1324 struct btrfs_root *root)
1327 struct btrfs_root *tree_root = root->fs_info->tree_root;
1328 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1330 if (root->fs_info->readonly)
1333 btrfs_set_super_generation(root->fs_info->super_copy,
1335 btrfs_set_super_root(root->fs_info->super_copy,
1336 tree_root->node->start);
1337 btrfs_set_super_root_level(root->fs_info->super_copy,
1338 btrfs_header_level(tree_root->node));
1339 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1340 chunk_root->node->start);
1341 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1342 btrfs_header_level(chunk_root->node));
1343 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1344 btrfs_header_generation(chunk_root->node));
1346 ret = write_all_supers(root);
1348 fprintf(stderr, "failed to write new super block err %d\n", ret);
1352 int close_ctree(struct btrfs_root *root)
1355 struct btrfs_trans_handle *trans;
1356 struct btrfs_fs_info *fs_info = root->fs_info;
1358 if (fs_info->last_trans_committed !=
1359 fs_info->generation) {
1360 trans = btrfs_start_transaction(root, 1);
1361 btrfs_commit_transaction(trans, root);
1362 trans = btrfs_start_transaction(root, 1);
1363 ret = commit_tree_roots(trans, fs_info);
1365 ret = __commit_transaction(trans, root);
1367 write_ctree_super(trans, root);
1368 btrfs_free_transaction(root, trans);
1370 btrfs_free_block_groups(fs_info);
1372 free_fs_roots_tree(&fs_info->fs_root_tree);
1374 btrfs_release_all_roots(fs_info);
1375 btrfs_close_devices(fs_info->fs_devices);
1376 btrfs_cleanup_all_caches(fs_info);
1377 btrfs_free_fs_info(fs_info);
1381 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1382 struct extent_buffer *eb)
1384 return clear_extent_buffer_dirty(eb);
1387 int wait_on_tree_block_writeback(struct btrfs_root *root,
1388 struct extent_buffer *eb)
1393 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1395 set_extent_buffer_dirty(eb);
1398 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1402 ret = extent_buffer_uptodate(buf);
1406 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1410 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1412 return set_extent_buffer_uptodate(eb);