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,
53 (unsigned long)btrfs_header_fsid(buf),
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 printk("Ignoring transid failure\n");
189 clear_extent_buffer_uptodate(io_tree, eb);
195 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
197 unsigned long offset = 0;
198 struct btrfs_multi_bio *multi = NULL;
199 struct btrfs_device *device;
202 unsigned long bytes_left = eb->len;
205 read_len = bytes_left;
208 if (!info->on_restoring) {
209 ret = btrfs_map_block(&info->mapping_tree, READ,
210 eb->start + offset, &read_len, &multi,
213 printk("Couldn't map the block %Lu\n", eb->start + offset);
217 device = multi->stripes[0].dev;
219 if (device->fd == 0) {
226 eb->dev_bytenr = multi->stripes[0].physical;
230 /* special case for restore metadump */
231 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
232 if (device->devid == 1)
237 eb->dev_bytenr = eb->start;
241 if (read_len > bytes_left)
242 read_len = bytes_left;
244 ret = read_extent_from_disk(eb, offset, read_len);
248 bytes_left -= read_len;
253 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
254 u32 blocksize, u64 parent_transid)
257 struct extent_buffer *eb;
258 u64 best_transid = 0;
264 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
268 if (btrfs_buffer_uptodate(eb, parent_transid))
272 ret = read_whole_eb(root->fs_info, eb, mirror_num);
273 if (ret == 0 && check_tree_block(root, eb) == 0 &&
274 csum_tree_block(root, eb, 1) == 0 &&
275 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
277 btrfs_set_buffer_uptodate(eb);
281 if (check_tree_block(root, eb))
282 printk("read block failed check_tree_block\n");
284 printk("Csum didn't match\n");
287 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
289 if (num_copies == 1) {
293 if (btrfs_header_generation(eb) > best_transid) {
294 best_transid = btrfs_header_generation(eb);
295 good_mirror = mirror_num;
298 if (mirror_num > num_copies) {
299 mirror_num = good_mirror;
304 free_extent_buffer(eb);
308 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
309 struct extent_buffer *eb)
314 u64 *raid_map = NULL;
315 struct btrfs_multi_bio *multi = NULL;
317 if (check_tree_block(root, eb))
320 if (!btrfs_buffer_uptodate(eb, trans->transid))
323 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
324 csum_tree_block(root, eb, 0);
328 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
329 eb->start, &length, &multi, 0, &raid_map);
332 ret = write_raid56_with_parity(root->fs_info, eb, multi,
335 } else while (dev_nr < multi->num_stripes) {
337 eb->fd = multi->stripes[dev_nr].dev->fd;
338 eb->dev_bytenr = multi->stripes[dev_nr].physical;
339 multi->stripes[dev_nr].dev->total_ios++;
341 ret = write_extent_to_disk(eb);
348 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
349 u32 stripesize, struct btrfs_root *root,
350 struct btrfs_fs_info *fs_info, u64 objectid)
353 root->commit_root = NULL;
354 root->sectorsize = sectorsize;
355 root->nodesize = nodesize;
356 root->leafsize = leafsize;
357 root->stripesize = stripesize;
359 root->track_dirty = 0;
361 root->fs_info = fs_info;
362 root->objectid = objectid;
363 root->last_trans = 0;
364 root->highest_inode = 0;
365 root->last_inode_alloc = 0;
367 INIT_LIST_HEAD(&root->dirty_list);
368 memset(&root->root_key, 0, sizeof(root->root_key));
369 memset(&root->root_item, 0, sizeof(root->root_item));
370 root->root_key.objectid = objectid;
374 static int update_cowonly_root(struct btrfs_trans_handle *trans,
375 struct btrfs_root *root)
379 struct btrfs_root *tree_root = root->fs_info->tree_root;
381 btrfs_write_dirty_block_groups(trans, root);
383 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
384 if (old_root_bytenr == root->node->start)
386 btrfs_set_root_bytenr(&root->root_item,
388 btrfs_set_root_generation(&root->root_item,
390 root->root_item.level = btrfs_header_level(root->node);
391 ret = btrfs_update_root(trans, tree_root,
395 btrfs_write_dirty_block_groups(trans, root);
400 static int commit_tree_roots(struct btrfs_trans_handle *trans,
401 struct btrfs_fs_info *fs_info)
403 struct btrfs_root *root;
404 struct list_head *next;
405 struct extent_buffer *eb;
408 if (fs_info->readonly)
411 eb = fs_info->tree_root->node;
412 extent_buffer_get(eb);
413 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
414 free_extent_buffer(eb);
418 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
419 next = fs_info->dirty_cowonly_roots.next;
421 root = list_entry(next, struct btrfs_root, dirty_list);
422 update_cowonly_root(trans, root);
423 free_extent_buffer(root->commit_root);
424 root->commit_root = NULL;
430 static int __commit_transaction(struct btrfs_trans_handle *trans,
431 struct btrfs_root *root)
435 struct extent_buffer *eb;
436 struct extent_io_tree *tree = &root->fs_info->extent_cache;
440 ret = find_first_extent_bit(tree, 0, &start, &end,
444 while(start <= end) {
445 eb = find_first_extent_buffer(tree, start);
446 BUG_ON(!eb || eb->start != start);
447 ret = write_tree_block(trans, root, eb);
450 clear_extent_buffer_dirty(eb);
451 free_extent_buffer(eb);
457 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
458 struct btrfs_root *root)
460 u64 transid = trans->transid;
462 struct btrfs_fs_info *fs_info = root->fs_info;
464 if (root->commit_root == root->node)
467 free_extent_buffer(root->commit_root);
468 root->commit_root = NULL;
470 btrfs_set_root_bytenr(&root->root_item, root->node->start);
471 btrfs_set_root_generation(&root->root_item, trans->transid);
472 root->root_item.level = btrfs_header_level(root->node);
473 ret = btrfs_update_root(trans, root->fs_info->tree_root,
474 &root->root_key, &root->root_item);
477 ret = commit_tree_roots(trans, fs_info);
479 ret = __commit_transaction(trans, root);
481 write_ctree_super(trans, root);
482 btrfs_finish_extent_commit(trans, fs_info->extent_root,
483 &fs_info->pinned_extents);
484 btrfs_free_transaction(root, trans);
485 free_extent_buffer(root->commit_root);
486 root->commit_root = NULL;
487 fs_info->running_transaction = NULL;
488 fs_info->last_trans_committed = transid;
492 static int find_and_setup_root(struct btrfs_root *tree_root,
493 struct btrfs_fs_info *fs_info,
494 u64 objectid, struct btrfs_root *root)
500 __setup_root(tree_root->nodesize, tree_root->leafsize,
501 tree_root->sectorsize, tree_root->stripesize,
502 root, fs_info, objectid);
503 ret = btrfs_find_last_root(tree_root, objectid,
504 &root->root_item, &root->root_key);
508 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
509 generation = btrfs_root_generation(&root->root_item);
510 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
511 blocksize, generation);
512 if (!extent_buffer_uptodate(root->node))
518 static int find_and_setup_log_root(struct btrfs_root *tree_root,
519 struct btrfs_fs_info *fs_info,
520 struct btrfs_super_block *disk_super)
523 u64 blocknr = btrfs_super_log_root(disk_super);
524 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
534 blocksize = btrfs_level_size(tree_root,
535 btrfs_super_log_root_level(disk_super));
537 __setup_root(tree_root->nodesize, tree_root->leafsize,
538 tree_root->sectorsize, tree_root->stripesize,
539 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
541 log_root->node = read_tree_block(tree_root, blocknr,
543 btrfs_super_generation(disk_super) + 1);
545 fs_info->log_root_tree = log_root;
547 if (!extent_buffer_uptodate(log_root->node)) {
548 free_extent_buffer(log_root->node);
550 fs_info->log_root_tree = NULL;
558 int btrfs_free_fs_root(struct btrfs_root *root)
561 free_extent_buffer(root->node);
562 if (root->commit_root)
563 free_extent_buffer(root->commit_root);
568 static void __free_fs_root(struct rb_node *node)
570 struct btrfs_root *root;
572 root = container_of(node, struct btrfs_root, rb_node);
573 btrfs_free_fs_root(root);
576 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
578 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
579 struct btrfs_key *location)
581 struct btrfs_root *root;
582 struct btrfs_root *tree_root = fs_info->tree_root;
583 struct btrfs_path *path;
584 struct extent_buffer *l;
589 root = malloc(sizeof(*root));
591 return ERR_PTR(-ENOMEM);
592 memset(root, 0, sizeof(*root));
593 if (location->offset == (u64)-1) {
594 ret = find_and_setup_root(tree_root, fs_info,
595 location->objectid, root);
603 __setup_root(tree_root->nodesize, tree_root->leafsize,
604 tree_root->sectorsize, tree_root->stripesize,
605 root, fs_info, location->objectid);
607 path = btrfs_alloc_path();
609 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
616 read_extent_buffer(l, &root->root_item,
617 btrfs_item_ptr_offset(l, path->slots[0]),
618 sizeof(root->root_item));
619 memcpy(&root->root_key, location, sizeof(*location));
622 btrfs_release_path(root, path);
623 btrfs_free_path(path);
628 generation = btrfs_root_generation(&root->root_item);
629 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
630 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
631 blocksize, generation);
638 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
641 u64 objectid = *((u64 *)data);
642 struct btrfs_root *root;
644 root = rb_entry(node, struct btrfs_root, rb_node);
645 if (objectid > root->objectid)
647 else if (objectid < root->objectid)
653 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
654 struct rb_node *node2)
656 struct btrfs_root *root;
658 root = rb_entry(node2, struct btrfs_root, rb_node);
659 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
662 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
663 struct btrfs_key *location)
665 struct btrfs_root *root;
666 struct rb_node *node;
669 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
670 return fs_info->tree_root;
671 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
672 return fs_info->extent_root;
673 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
674 return fs_info->chunk_root;
675 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
676 return fs_info->dev_root;
677 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
678 return fs_info->csum_root;
680 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
681 location->offset != (u64)-1);
683 node = rb_search(&fs_info->fs_root_tree, (void *)&location->objectid,
684 btrfs_fs_roots_compare_objectids, NULL);
686 return container_of(node, struct btrfs_root, rb_node);
688 root = btrfs_read_fs_root_no_cache(fs_info, location);
692 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
693 btrfs_fs_roots_compare_roots);
698 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
700 free(fs_info->tree_root);
701 free(fs_info->extent_root);
702 free(fs_info->chunk_root);
703 free(fs_info->dev_root);
704 free(fs_info->csum_root);
705 free(fs_info->super_copy);
706 free(fs_info->log_root_tree);
710 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
712 struct btrfs_fs_info *fs_info;
714 fs_info = malloc(sizeof(struct btrfs_fs_info));
718 memset(fs_info, 0, sizeof(struct btrfs_fs_info));
720 fs_info->tree_root = malloc(sizeof(struct btrfs_root));
721 fs_info->extent_root = malloc(sizeof(struct btrfs_root));
722 fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
723 fs_info->dev_root = malloc(sizeof(struct btrfs_root));
724 fs_info->csum_root = malloc(sizeof(struct btrfs_root));
725 fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
727 if (!fs_info->tree_root || !fs_info->extent_root ||
728 !fs_info->chunk_root || !fs_info->dev_root ||
729 !fs_info->csum_root || !fs_info->super_copy)
732 memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
733 memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
734 memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
735 memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
736 memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
737 memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
739 extent_io_tree_init(&fs_info->extent_cache);
740 extent_io_tree_init(&fs_info->free_space_cache);
741 extent_io_tree_init(&fs_info->block_group_cache);
742 extent_io_tree_init(&fs_info->pinned_extents);
743 extent_io_tree_init(&fs_info->pending_del);
744 extent_io_tree_init(&fs_info->extent_ins);
745 fs_info->fs_root_tree = RB_ROOT;
746 cache_tree_init(&fs_info->mapping_tree.cache_tree);
748 mutex_init(&fs_info->fs_mutex);
749 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
750 INIT_LIST_HEAD(&fs_info->space_info);
753 fs_info->readonly = 1;
755 fs_info->super_bytenr = sb_bytenr;
756 fs_info->data_alloc_profile = (u64)-1;
757 fs_info->metadata_alloc_profile = (u64)-1;
758 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
761 btrfs_free_fs_info(fs_info);
765 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
769 features = btrfs_super_incompat_flags(sb) &
770 ~BTRFS_FEATURE_INCOMPAT_SUPP;
772 printk("couldn't open because of unsupported "
773 "option features (%Lx).\n",
774 (unsigned long long)features);
778 features = btrfs_super_incompat_flags(sb);
779 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
780 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
781 btrfs_set_super_incompat_flags(sb, features);
784 features = btrfs_super_compat_ro_flags(sb) &
785 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
786 if (writable && features) {
787 printk("couldn't open RDWR because of unsupported "
788 "option features (%Lx).\n",
789 (unsigned long long)features);
795 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info,
796 u64 root_tree_bytenr, int partial)
798 struct btrfs_super_block *sb = fs_info->super_copy;
799 struct btrfs_root *root;
800 struct btrfs_key key;
809 nodesize = btrfs_super_nodesize(sb);
810 leafsize = btrfs_super_leafsize(sb);
811 sectorsize = btrfs_super_sectorsize(sb);
812 stripesize = btrfs_super_stripesize(sb);
814 root = fs_info->tree_root;
815 __setup_root(nodesize, leafsize, sectorsize, stripesize,
816 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
817 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
818 generation = btrfs_super_generation(sb);
820 if (!root_tree_bytenr)
821 root_tree_bytenr = btrfs_super_root(sb);
822 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
824 if (!extent_buffer_uptodate(root->node)) {
825 fprintf(stderr, "Couldn't read tree root\n");
829 ret = find_and_setup_root(root, fs_info, BTRFS_EXTENT_TREE_OBJECTID,
830 fs_info->extent_root);
832 printk("Couldn't setup extent tree\n");
835 fs_info->extent_root->track_dirty = 1;
837 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
840 printk("Couldn't setup device tree\n");
843 fs_info->dev_root->track_dirty = 1;
845 ret = find_and_setup_root(root, fs_info, BTRFS_CSUM_TREE_OBJECTID,
848 printk("Couldn't setup csum tree\n");
852 fs_info->csum_root->track_dirty = 1;
854 ret = find_and_setup_log_root(root, fs_info, sb);
856 printk("Couldn't setup log root tree\n");
860 fs_info->generation = generation;
861 fs_info->last_trans_committed = generation;
862 btrfs_read_block_groups(fs_info->tree_root);
864 key.objectid = BTRFS_FS_TREE_OBJECTID;
865 key.type = BTRFS_ROOT_ITEM_KEY;
866 key.offset = (u64)-1;
867 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
869 if (!fs_info->fs_root)
874 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
876 if (fs_info->csum_root)
877 free_extent_buffer(fs_info->csum_root->node);
878 if (fs_info->dev_root)
879 free_extent_buffer(fs_info->dev_root->node);
880 if (fs_info->extent_root)
881 free_extent_buffer(fs_info->extent_root->node);
882 if (fs_info->tree_root)
883 free_extent_buffer(fs_info->tree_root->node);
884 if (fs_info->log_root_tree)
885 free_extent_buffer(fs_info->log_root_tree->node);
886 if (fs_info->chunk_root)
887 free_extent_buffer(fs_info->chunk_root->node);
890 static void free_map_lookup(struct cache_extent *ce)
892 struct map_lookup *map;
894 map = container_of(ce, struct map_lookup, ce);
898 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
900 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
902 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
903 extent_io_tree_cleanup(&fs_info->extent_cache);
904 extent_io_tree_cleanup(&fs_info->free_space_cache);
905 extent_io_tree_cleanup(&fs_info->block_group_cache);
906 extent_io_tree_cleanup(&fs_info->pinned_extents);
907 extent_io_tree_cleanup(&fs_info->pending_del);
908 extent_io_tree_cleanup(&fs_info->extent_ins);
911 int btrfs_scan_fs_devices(int fd, const char *path,
912 struct btrfs_fs_devices **fs_devices)
917 ret = btrfs_scan_one_device(fd, path, fs_devices,
918 &total_devs, BTRFS_SUPER_INFO_OFFSET);
920 fprintf(stderr, "No valid Btrfs found on %s\n", path);
924 if (total_devs != 1) {
925 ret = btrfs_scan_for_fsid(*fs_devices, total_devs, 1);
932 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
934 struct btrfs_super_block *sb = fs_info->super_copy;
943 nodesize = btrfs_super_nodesize(sb);
944 leafsize = btrfs_super_leafsize(sb);
945 sectorsize = btrfs_super_sectorsize(sb);
946 stripesize = btrfs_super_stripesize(sb);
948 __setup_root(nodesize, leafsize, sectorsize, stripesize,
949 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
951 ret = btrfs_read_sys_array(fs_info->chunk_root);
955 blocksize = btrfs_level_size(fs_info->chunk_root,
956 btrfs_super_chunk_root_level(sb));
957 generation = btrfs_super_chunk_root_generation(sb);
959 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
960 btrfs_super_chunk_root(sb),
961 blocksize, generation);
962 if (!fs_info->chunk_root->node ||
963 !extent_buffer_uptodate(fs_info->chunk_root->node)) {
964 fprintf(stderr, "Couldn't read chunk root\n");
968 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
969 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
971 fprintf(stderr, "Couldn't read chunk tree\n");
978 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
980 u64 root_tree_bytenr, int writes,
981 int partial, int restore)
983 struct btrfs_fs_info *fs_info;
984 struct btrfs_super_block *disk_super;
985 struct btrfs_fs_devices *fs_devices = NULL;
986 struct extent_buffer *eb;
990 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
992 /* try to drop all the caches */
993 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
994 fprintf(stderr, "Warning, could not drop caches\n");
996 fs_info = btrfs_new_fs_info(writes, sb_bytenr);
998 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1002 fs_info->on_restoring = 1;
1004 ret = btrfs_scan_fs_devices(fp, path, &fs_devices);
1008 fs_info->fs_devices = fs_devices;
1010 ret = btrfs_open_devices(fs_devices, O_RDWR);
1012 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1017 disk_super = fs_info->super_copy;
1018 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1019 disk_super, sb_bytenr);
1021 printk("No valid btrfs found\n");
1025 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1027 ret = btrfs_check_fs_compatibility(fs_info->super_copy, writes);
1031 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1035 eb = fs_info->chunk_root->node;
1036 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1037 (unsigned long)btrfs_header_chunk_tree_uuid(eb),
1040 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, partial);
1050 btrfs_release_all_roots(fs_info);
1051 btrfs_cleanup_all_caches(fs_info);
1053 btrfs_close_devices(fs_devices);
1055 btrfs_free_fs_info(fs_info);
1059 struct btrfs_fs_info *open_ctree_fs_info_restore(const char *filename,
1060 u64 sb_bytenr, u64 root_tree_bytenr,
1061 int writes, int partial)
1064 struct btrfs_fs_info *info;
1065 int flags = O_CREAT | O_RDWR;
1071 fp = open(filename, flags, 0600);
1073 fprintf (stderr, "Could not open %s\n", filename);
1076 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1077 writes, partial, restore);
1082 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1083 u64 sb_bytenr, u64 root_tree_bytenr,
1084 int writes, int partial)
1087 struct btrfs_fs_info *info;
1088 int flags = O_CREAT | O_RDWR;
1093 fp = open(filename, flags, 0600);
1095 fprintf (stderr, "Could not open %s\n", filename);
1098 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1099 writes, partial, 0);
1104 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
1106 struct btrfs_fs_info *info;
1108 info = open_ctree_fs_info(filename, sb_bytenr, 0, writes, 0);
1111 return info->fs_root;
1114 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1117 struct btrfs_fs_info *info;
1118 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0, 0);
1121 return info->fs_root;
1124 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
1126 u8 fsid[BTRFS_FSID_SIZE];
1127 int fsid_is_initialized = 0;
1128 struct btrfs_super_block buf;
1134 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1135 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1136 if (ret < sizeof(buf))
1139 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1140 buf.magic != cpu_to_le64(BTRFS_MAGIC))
1143 memcpy(sb, &buf, sizeof(*sb));
1147 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1148 bytenr = btrfs_sb_offset(i);
1149 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1150 if (ret < sizeof(buf))
1153 if (btrfs_super_bytenr(&buf) != bytenr )
1155 /* if magic is NULL, the device was removed */
1156 if (buf.magic == 0 && i == 0)
1158 if (buf.magic != cpu_to_le64(BTRFS_MAGIC))
1161 if (!fsid_is_initialized) {
1162 memcpy(fsid, buf.fsid, sizeof(fsid));
1163 fsid_is_initialized = 1;
1164 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1166 * the superblocks (the original one and
1167 * its backups) contain data of different
1168 * filesystems -> the super cannot be trusted
1173 if (btrfs_super_generation(&buf) > transid) {
1174 memcpy(sb, &buf, sizeof(*sb));
1175 transid = btrfs_super_generation(&buf);
1179 return transid > 0 ? 0 : -1;
1182 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
1183 struct btrfs_device *device)
1189 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1190 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1192 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1193 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1194 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1197 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1198 * zero filled, we can use it directly
1200 ret = pwrite64(device->fd, root->fs_info->super_copy,
1201 BTRFS_SUPER_INFO_SIZE,
1202 root->fs_info->super_bytenr);
1203 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1207 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1208 bytenr = btrfs_sb_offset(i);
1209 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1212 btrfs_set_super_bytenr(sb, bytenr);
1215 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1216 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1217 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1220 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1221 * zero filled, we can use it directly
1223 ret = pwrite64(device->fd, root->fs_info->super_copy,
1224 BTRFS_SUPER_INFO_SIZE, bytenr);
1225 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1231 int write_all_supers(struct btrfs_root *root)
1233 struct list_head *cur;
1234 struct list_head *head = &root->fs_info->fs_devices->devices;
1235 struct btrfs_device *dev;
1236 struct btrfs_super_block *sb;
1237 struct btrfs_dev_item *dev_item;
1241 sb = root->fs_info->super_copy;
1242 dev_item = &sb->dev_item;
1243 list_for_each(cur, head) {
1244 dev = list_entry(cur, struct btrfs_device, dev_list);
1245 if (!dev->writeable)
1248 btrfs_set_stack_device_generation(dev_item, 0);
1249 btrfs_set_stack_device_type(dev_item, dev->type);
1250 btrfs_set_stack_device_id(dev_item, dev->devid);
1251 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1252 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1253 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1254 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1255 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1256 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1257 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1259 flags = btrfs_super_flags(sb);
1260 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1262 ret = write_dev_supers(root, sb, dev);
1268 int write_ctree_super(struct btrfs_trans_handle *trans,
1269 struct btrfs_root *root)
1272 struct btrfs_root *tree_root = root->fs_info->tree_root;
1273 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1275 if (root->fs_info->readonly)
1278 btrfs_set_super_generation(root->fs_info->super_copy,
1280 btrfs_set_super_root(root->fs_info->super_copy,
1281 tree_root->node->start);
1282 btrfs_set_super_root_level(root->fs_info->super_copy,
1283 btrfs_header_level(tree_root->node));
1284 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1285 chunk_root->node->start);
1286 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1287 btrfs_header_level(chunk_root->node));
1288 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1289 btrfs_header_generation(chunk_root->node));
1291 ret = write_all_supers(root);
1293 fprintf(stderr, "failed to write new super block err %d\n", ret);
1297 int close_ctree(struct btrfs_root *root)
1300 struct btrfs_trans_handle *trans;
1301 struct btrfs_fs_info *fs_info = root->fs_info;
1303 if (fs_info->last_trans_committed !=
1304 fs_info->generation) {
1305 trans = btrfs_start_transaction(root, 1);
1306 btrfs_commit_transaction(trans, root);
1307 trans = btrfs_start_transaction(root, 1);
1308 ret = commit_tree_roots(trans, fs_info);
1310 ret = __commit_transaction(trans, root);
1312 write_ctree_super(trans, root);
1313 btrfs_free_transaction(root, trans);
1315 btrfs_free_block_groups(fs_info);
1317 free_fs_roots_tree(&fs_info->fs_root_tree);
1319 btrfs_release_all_roots(fs_info);
1320 btrfs_close_devices(fs_info->fs_devices);
1321 btrfs_cleanup_all_caches(fs_info);
1322 btrfs_free_fs_info(fs_info);
1326 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1327 struct extent_buffer *eb)
1329 return clear_extent_buffer_dirty(eb);
1332 int wait_on_tree_block_writeback(struct btrfs_root *root,
1333 struct extent_buffer *eb)
1338 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1340 set_extent_buffer_dirty(eb);
1343 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1347 ret = extent_buffer_uptodate(buf);
1351 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1355 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1357 return set_extent_buffer_uptodate(eb);