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 close_all_devices(struct btrfs_fs_info *fs_info);
40 static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
43 struct btrfs_fs_devices *fs_devices;
46 if (buf->start != btrfs_header_bytenr(buf)) {
47 printk("Check tree block failed, want=%Lu, have=%Lu\n",
48 buf->start, btrfs_header_bytenr(buf));
52 fs_devices = root->fs_info->fs_devices;
54 if (!memcmp_extent_buffer(buf, fs_devices->fsid,
55 (unsigned long)btrfs_header_fsid(buf),
60 fs_devices = fs_devices->seed;
65 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
67 return crc32c(seed, data, len);
70 void btrfs_csum_final(u32 crc, char *result)
72 *(__le32 *)result = ~cpu_to_le32(crc);
75 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
82 result = malloc(csum_size * sizeof(char));
86 len = buf->len - BTRFS_CSUM_SIZE;
87 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
88 btrfs_csum_final(crc, result);
91 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
92 printk("checksum verify failed on %llu wanted %X "
93 "found %X\n", (unsigned long long)buf->start,
94 *((int *)result), *((char *)buf->data));
99 write_extent_buffer(buf, result, 0, csum_size);
105 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
109 btrfs_super_csum_size(&root->fs_info->super_copy);
110 return csum_tree_block_size(buf, csum_size, verify);
113 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
114 u64 bytenr, u32 blocksize)
116 return find_extent_buffer(&root->fs_info->extent_cache,
120 struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
121 u64 bytenr, u32 blocksize)
123 return alloc_extent_buffer(&root->fs_info->extent_cache, bytenr,
127 int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
131 struct extent_buffer *eb;
133 struct btrfs_multi_bio *multi = NULL;
134 struct btrfs_device *device;
136 eb = btrfs_find_tree_block(root, bytenr, blocksize);
137 if (eb && btrfs_buffer_uptodate(eb, parent_transid)) {
138 free_extent_buffer(eb);
143 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
144 bytenr, &length, &multi, 0);
146 device = multi->stripes[0].dev;
148 blocksize = min(blocksize, (u32)(64 * 1024));
149 readahead(device->fd, multi->stripes[0].physical, blocksize);
154 static int verify_parent_transid(struct extent_io_tree *io_tree,
155 struct extent_buffer *eb, u64 parent_transid,
160 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
163 if (extent_buffer_uptodate(eb) &&
164 btrfs_header_generation(eb) == parent_transid) {
168 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
169 (unsigned long long)eb->start,
170 (unsigned long long)parent_transid,
171 (unsigned long long)btrfs_header_generation(eb));
173 printk("Ignoring transid failure\n");
179 clear_extent_buffer_uptodate(io_tree, eb);
185 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
186 u32 blocksize, u64 parent_transid)
189 struct extent_buffer *eb;
191 u64 best_transid = 0;
192 struct btrfs_multi_bio *multi = NULL;
193 struct btrfs_device *device;
199 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
203 if (btrfs_buffer_uptodate(eb, parent_transid))
208 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
209 eb->start, &length, &multi, mirror_num);
211 printk("Couldn't map the block %Lu\n", bytenr);
214 device = multi->stripes[0].dev;
217 eb->dev_bytenr = multi->stripes[0].physical;
219 ret = read_extent_from_disk(eb);
221 if (ret == 0 && check_tree_block(root, eb) == 0 &&
222 csum_tree_block(root, eb, 1) == 0 &&
223 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
225 btrfs_set_buffer_uptodate(eb);
229 if (check_tree_block(root, eb))
230 printk("read block failed check_tree_block\n");
232 printk("Csum didn't match\n");
235 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
237 if (num_copies == 1) {
241 if (btrfs_header_generation(eb) > best_transid) {
242 best_transid = btrfs_header_generation(eb);
243 good_mirror = mirror_num;
246 if (mirror_num > num_copies) {
247 mirror_num = good_mirror;
252 free_extent_buffer(eb);
256 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
257 struct extent_buffer *eb)
262 struct btrfs_multi_bio *multi = NULL;
264 if (check_tree_block(root, eb))
266 if (!btrfs_buffer_uptodate(eb, trans->transid))
269 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
270 csum_tree_block(root, eb, 0);
274 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
275 eb->start, &length, &multi, 0);
277 while(dev_nr < multi->num_stripes) {
279 eb->fd = multi->stripes[dev_nr].dev->fd;
280 eb->dev_bytenr = multi->stripes[dev_nr].physical;
281 multi->stripes[dev_nr].dev->total_ios++;
283 ret = write_extent_to_disk(eb);
290 static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
291 u32 stripesize, struct btrfs_root *root,
292 struct btrfs_fs_info *fs_info, u64 objectid)
295 root->commit_root = NULL;
296 root->sectorsize = sectorsize;
297 root->nodesize = nodesize;
298 root->leafsize = leafsize;
299 root->stripesize = stripesize;
301 root->track_dirty = 0;
303 root->fs_info = fs_info;
304 root->objectid = objectid;
305 root->last_trans = 0;
306 root->highest_inode = 0;
307 root->last_inode_alloc = 0;
309 INIT_LIST_HEAD(&root->dirty_list);
310 memset(&root->root_key, 0, sizeof(root->root_key));
311 memset(&root->root_item, 0, sizeof(root->root_item));
312 root->root_key.objectid = objectid;
316 static int update_cowonly_root(struct btrfs_trans_handle *trans,
317 struct btrfs_root *root)
321 struct btrfs_root *tree_root = root->fs_info->tree_root;
323 btrfs_write_dirty_block_groups(trans, root);
325 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
326 if (old_root_bytenr == root->node->start)
328 btrfs_set_root_bytenr(&root->root_item,
330 btrfs_set_root_generation(&root->root_item,
332 root->root_item.level = btrfs_header_level(root->node);
333 ret = btrfs_update_root(trans, tree_root,
337 btrfs_write_dirty_block_groups(trans, root);
342 static int commit_tree_roots(struct btrfs_trans_handle *trans,
343 struct btrfs_fs_info *fs_info)
345 struct btrfs_root *root;
346 struct list_head *next;
347 struct extent_buffer *eb;
349 if (fs_info->readonly)
352 eb = fs_info->tree_root->node;
353 extent_buffer_get(eb);
354 btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
355 free_extent_buffer(eb);
357 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
358 next = fs_info->dirty_cowonly_roots.next;
360 root = list_entry(next, struct btrfs_root, dirty_list);
361 update_cowonly_root(trans, root);
366 static int __commit_transaction(struct btrfs_trans_handle *trans,
367 struct btrfs_root *root)
371 struct extent_buffer *eb;
372 struct extent_io_tree *tree = &root->fs_info->extent_cache;
376 ret = find_first_extent_bit(tree, 0, &start, &end,
380 while(start <= end) {
381 eb = find_first_extent_buffer(tree, start);
382 BUG_ON(!eb || eb->start != start);
383 ret = write_tree_block(trans, root, eb);
386 clear_extent_buffer_dirty(eb);
387 free_extent_buffer(eb);
393 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
394 struct btrfs_root *root)
396 u64 transid = trans->transid;
398 struct btrfs_fs_info *fs_info = root->fs_info;
400 if (root->commit_root == root->node)
403 free_extent_buffer(root->commit_root);
404 root->commit_root = NULL;
406 btrfs_set_root_bytenr(&root->root_item, root->node->start);
407 btrfs_set_root_generation(&root->root_item, trans->transid);
408 root->root_item.level = btrfs_header_level(root->node);
409 ret = btrfs_update_root(trans, root->fs_info->tree_root,
410 &root->root_key, &root->root_item);
413 ret = commit_tree_roots(trans, fs_info);
415 ret = __commit_transaction(trans, root);
417 write_ctree_super(trans, root);
418 btrfs_finish_extent_commit(trans, fs_info->extent_root,
419 &fs_info->pinned_extents);
420 btrfs_free_transaction(root, trans);
421 free_extent_buffer(root->commit_root);
422 root->commit_root = NULL;
423 fs_info->running_transaction = NULL;
424 fs_info->last_trans_committed = transid;
428 static int find_and_setup_root(struct btrfs_root *tree_root,
429 struct btrfs_fs_info *fs_info,
430 u64 objectid, struct btrfs_root *root)
436 __setup_root(tree_root->nodesize, tree_root->leafsize,
437 tree_root->sectorsize, tree_root->stripesize,
438 root, fs_info, objectid);
439 ret = btrfs_find_last_root(tree_root, objectid,
440 &root->root_item, &root->root_key);
443 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
444 generation = btrfs_root_generation(&root->root_item);
445 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
446 blocksize, generation);
447 if (!extent_buffer_uptodate(root->node))
453 static int find_and_setup_log_root(struct btrfs_root *tree_root,
454 struct btrfs_fs_info *fs_info,
455 struct btrfs_super_block *disk_super)
458 u64 blocknr = btrfs_super_log_root(disk_super);
459 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
469 blocksize = btrfs_level_size(tree_root,
470 btrfs_super_log_root_level(disk_super));
472 __setup_root(tree_root->nodesize, tree_root->leafsize,
473 tree_root->sectorsize, tree_root->stripesize,
474 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
476 log_root->node = read_tree_block(tree_root, blocknr,
478 btrfs_super_generation(disk_super) + 1);
480 fs_info->log_root_tree = log_root;
482 if (!extent_buffer_uptodate(log_root->node)) {
492 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
493 struct btrfs_root *root)
496 free_extent_buffer(root->node);
497 if (root->commit_root)
498 free_extent_buffer(root->commit_root);
503 static int free_fs_roots(struct btrfs_fs_info *fs_info)
505 struct cache_extent *cache;
506 struct btrfs_root *root;
509 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
512 root = container_of(cache, struct btrfs_root, cache);
513 remove_cache_extent(&fs_info->fs_root_cache, cache);
514 btrfs_free_fs_root(fs_info, root);
519 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
520 struct btrfs_key *location)
522 struct btrfs_root *root;
523 struct btrfs_root *tree_root = fs_info->tree_root;
524 struct btrfs_path *path;
525 struct extent_buffer *l;
530 root = malloc(sizeof(*root));
532 return ERR_PTR(-ENOMEM);
533 memset(root, 0, sizeof(*root));
534 if (location->offset == (u64)-1) {
535 ret = find_and_setup_root(tree_root, fs_info,
536 location->objectid, root);
544 __setup_root(tree_root->nodesize, tree_root->leafsize,
545 tree_root->sectorsize, tree_root->stripesize,
546 root, fs_info, location->objectid);
548 path = btrfs_alloc_path();
550 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
557 read_extent_buffer(l, &root->root_item,
558 btrfs_item_ptr_offset(l, path->slots[0]),
559 sizeof(root->root_item));
560 memcpy(&root->root_key, location, sizeof(*location));
563 btrfs_release_path(root, path);
564 btrfs_free_path(path);
569 generation = btrfs_root_generation(&root->root_item);
570 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
571 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
572 blocksize, generation);
579 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
580 struct btrfs_key *location)
582 struct btrfs_root *root;
583 struct cache_extent *cache;
586 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
587 return fs_info->tree_root;
588 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
589 return fs_info->extent_root;
590 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
591 return fs_info->chunk_root;
592 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
593 return fs_info->dev_root;
594 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
595 return fs_info->csum_root;
597 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
598 location->offset != (u64)-1);
600 cache = find_cache_extent(&fs_info->fs_root_cache,
601 location->objectid, 1);
603 return container_of(cache, struct btrfs_root, cache);
605 root = btrfs_read_fs_root_no_cache(fs_info, location);
609 root->cache.start = location->objectid;
610 root->cache.size = 1;
611 ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
617 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
619 u64 root_tree_bytenr, int writes,
628 struct btrfs_key key;
629 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
630 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
631 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
632 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
633 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
634 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
636 struct btrfs_super_block *disk_super;
637 struct btrfs_fs_devices *fs_devices = NULL;
642 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
644 ret = btrfs_scan_one_device(fp, path, &fs_devices,
645 &total_devs, sb_bytenr);
648 fprintf(stderr, "No valid Btrfs found on %s\n", path);
652 if (total_devs != 1) {
653 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
658 memset(fs_info, 0, sizeof(*fs_info));
659 fs_info->tree_root = tree_root;
660 fs_info->extent_root = extent_root;
661 fs_info->chunk_root = chunk_root;
662 fs_info->dev_root = dev_root;
663 fs_info->csum_root = csum_root;
666 fs_info->readonly = 1;
668 extent_io_tree_init(&fs_info->extent_cache);
669 extent_io_tree_init(&fs_info->free_space_cache);
670 extent_io_tree_init(&fs_info->block_group_cache);
671 extent_io_tree_init(&fs_info->pinned_extents);
672 extent_io_tree_init(&fs_info->pending_del);
673 extent_io_tree_init(&fs_info->extent_ins);
674 cache_tree_init(&fs_info->fs_root_cache);
676 cache_tree_init(&fs_info->mapping_tree.cache_tree);
678 mutex_init(&fs_info->fs_mutex);
679 fs_info->fs_devices = fs_devices;
680 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
681 INIT_LIST_HEAD(&fs_info->space_info);
683 __setup_root(4096, 4096, 4096, 4096, tree_root,
684 fs_info, BTRFS_ROOT_TREE_OBJECTID);
687 ret = btrfs_open_devices(fs_devices, O_RDWR);
689 ret = btrfs_open_devices(fs_devices, O_RDONLY);
693 fs_info->super_bytenr = sb_bytenr;
694 disk_super = &fs_info->super_copy;
695 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
696 disk_super, sb_bytenr);
698 printk("No valid btrfs found\n");
702 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
705 features = btrfs_super_incompat_flags(disk_super) &
706 ~BTRFS_FEATURE_INCOMPAT_SUPP;
708 printk("couldn't open because of unsupported "
709 "option features (%Lx).\n",
710 (unsigned long long)features);
714 features = btrfs_super_incompat_flags(disk_super);
715 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
716 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
717 btrfs_set_super_incompat_flags(disk_super, features);
720 features = btrfs_super_compat_ro_flags(disk_super) &
721 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
722 if (writes && features) {
723 printk("couldn't open RDWR because of unsupported "
724 "option features (%Lx).\n",
725 (unsigned long long)features);
729 nodesize = btrfs_super_nodesize(disk_super);
730 leafsize = btrfs_super_leafsize(disk_super);
731 sectorsize = btrfs_super_sectorsize(disk_super);
732 stripesize = btrfs_super_stripesize(disk_super);
733 tree_root->nodesize = nodesize;
734 tree_root->leafsize = leafsize;
735 tree_root->sectorsize = sectorsize;
736 tree_root->stripesize = stripesize;
738 ret = btrfs_read_sys_array(tree_root);
741 blocksize = btrfs_level_size(tree_root,
742 btrfs_super_chunk_root_level(disk_super));
743 generation = btrfs_super_chunk_root_generation(disk_super);
745 __setup_root(nodesize, leafsize, sectorsize, stripesize,
746 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
748 chunk_root->node = read_tree_block(chunk_root,
749 btrfs_super_chunk_root(disk_super),
750 blocksize, generation);
751 if (!extent_buffer_uptodate(chunk_root->node)) {
752 printk("Couldn't read chunk root\n");
756 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
757 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
760 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
761 ret = btrfs_read_chunk_tree(chunk_root);
766 blocksize = btrfs_level_size(tree_root,
767 btrfs_super_root_level(disk_super));
768 generation = btrfs_super_generation(disk_super);
770 if (!root_tree_bytenr)
771 root_tree_bytenr = btrfs_super_root(disk_super);
772 tree_root->node = read_tree_block(tree_root,
774 blocksize, generation);
775 if (!extent_buffer_uptodate(tree_root->node)) {
776 printk("Couldn't read tree root\n");
779 ret = find_and_setup_root(tree_root, fs_info,
780 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
782 printk("Couldn't setup extent tree\n");
785 extent_root->track_dirty = 1;
787 ret = find_and_setup_root(tree_root, fs_info,
788 BTRFS_DEV_TREE_OBJECTID, dev_root);
790 printk("Couldn't setup device tree\n");
793 dev_root->track_dirty = 1;
795 ret = find_and_setup_root(tree_root, fs_info,
796 BTRFS_CSUM_TREE_OBJECTID, csum_root);
798 printk("Couldn't setup csum tree\n");
802 csum_root->track_dirty = 1;
804 find_and_setup_log_root(tree_root, fs_info, disk_super);
806 fs_info->generation = generation;
807 fs_info->last_trans_committed = generation;
808 btrfs_read_block_groups(fs_info->tree_root);
810 key.objectid = BTRFS_FS_TREE_OBJECTID;
811 key.type = BTRFS_ROOT_ITEM_KEY;
812 key.offset = (u64)-1;
813 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
815 if (!fs_info->fs_root)
818 fs_info->data_alloc_profile = (u64)-1;
819 fs_info->metadata_alloc_profile = (u64)-1;
820 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
828 if (fs_info->csum_root)
829 free_extent_buffer(fs_info->csum_root->node);
830 if (fs_info->dev_root)
831 free_extent_buffer(fs_info->dev_root->node);
832 if (fs_info->extent_root)
833 free_extent_buffer(fs_info->extent_root->node);
834 if (fs_info->tree_root)
835 free_extent_buffer(fs_info->tree_root->node);
836 if (fs_info->chunk_root)
837 free_extent_buffer(fs_info->chunk_root->node);
839 close_all_devices(fs_info);
841 extent_io_tree_cleanup(&fs_info->extent_cache);
842 extent_io_tree_cleanup(&fs_info->free_space_cache);
843 extent_io_tree_cleanup(&fs_info->block_group_cache);
844 extent_io_tree_cleanup(&fs_info->pinned_extents);
845 extent_io_tree_cleanup(&fs_info->pending_del);
846 extent_io_tree_cleanup(&fs_info->extent_ins);
857 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
858 u64 sb_bytenr, int writes,
862 struct btrfs_fs_info *info;
863 int flags = O_CREAT | O_RDWR;
868 fp = open(filename, flags, 0600);
870 fprintf (stderr, "Could not open %s\n", filename);
873 info = __open_ctree_fd(fp, filename, sb_bytenr, 0, writes, partial);
878 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
880 struct btrfs_fs_info *info;
882 info = open_ctree_fs_info(filename, sb_bytenr, writes, 0);
885 return info->fs_root;
888 struct btrfs_root *open_ctree_recovery(const char *filename, u64 sb_bytenr,
889 u64 root_tree_bytenr)
892 struct btrfs_fs_info *info;
895 fp = open(filename, O_RDONLY);
897 fprintf (stderr, "Could not open %s\n", filename);
900 info = __open_ctree_fd(fp, filename, sb_bytenr,
901 root_tree_bytenr, 0, 0);
906 return info->fs_root;
909 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
912 struct btrfs_fs_info *info;
913 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0);
916 return info->fs_root;
919 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
921 u8 fsid[BTRFS_FSID_SIZE];
922 int fsid_is_initialized = 0;
923 struct btrfs_super_block buf;
929 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
930 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
931 if (ret < sizeof(buf))
934 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
935 strncmp((char *)(&buf.magic), BTRFS_MAGIC,
939 memcpy(sb, &buf, sizeof(*sb));
943 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
944 bytenr = btrfs_sb_offset(i);
945 ret = pread64(fd, &buf, sizeof(buf), bytenr);
946 if (ret < sizeof(buf))
949 if (btrfs_super_bytenr(&buf) != bytenr )
951 /* if magic is NULL, the device was removed */
952 if (buf.magic == 0 && i == 0)
954 if (strncmp((char *)(&buf.magic), BTRFS_MAGIC,
958 if (!fsid_is_initialized) {
959 memcpy(fsid, buf.fsid, sizeof(fsid));
960 fsid_is_initialized = 1;
961 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
963 * the superblocks (the original one and
964 * its backups) contain data of different
965 * filesystems -> the super cannot be trusted
970 if (btrfs_super_generation(&buf) > transid) {
971 memcpy(sb, &buf, sizeof(*sb));
972 transid = btrfs_super_generation(&buf);
976 return transid > 0 ? 0 : -1;
979 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
980 struct btrfs_device *device)
986 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
987 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
989 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
990 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
991 btrfs_csum_final(crc, (char *)&sb->csum[0]);
993 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE,
994 root->fs_info->super_bytenr);
995 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
999 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1000 bytenr = btrfs_sb_offset(i);
1001 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1004 btrfs_set_super_bytenr(sb, bytenr);
1007 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1008 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1009 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1011 ret = pwrite64(device->fd, sb, BTRFS_SUPER_INFO_SIZE, bytenr);
1012 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1017 int write_all_supers(struct btrfs_root *root)
1019 struct list_head *cur;
1020 struct list_head *head = &root->fs_info->fs_devices->devices;
1021 struct btrfs_device *dev;
1022 struct btrfs_super_block *sb;
1023 struct btrfs_dev_item *dev_item;
1027 sb = &root->fs_info->super_copy;
1028 dev_item = &sb->dev_item;
1029 list_for_each(cur, head) {
1030 dev = list_entry(cur, struct btrfs_device, dev_list);
1031 if (!dev->writeable)
1034 btrfs_set_stack_device_generation(dev_item, 0);
1035 btrfs_set_stack_device_type(dev_item, dev->type);
1036 btrfs_set_stack_device_id(dev_item, dev->devid);
1037 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1038 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1039 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1040 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1041 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1042 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1043 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1045 flags = btrfs_super_flags(sb);
1046 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1048 ret = write_dev_supers(root, sb, dev);
1054 int write_ctree_super(struct btrfs_trans_handle *trans,
1055 struct btrfs_root *root)
1058 struct btrfs_root *tree_root = root->fs_info->tree_root;
1059 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1061 if (root->fs_info->readonly)
1064 btrfs_set_super_generation(&root->fs_info->super_copy,
1066 btrfs_set_super_root(&root->fs_info->super_copy,
1067 tree_root->node->start);
1068 btrfs_set_super_root_level(&root->fs_info->super_copy,
1069 btrfs_header_level(tree_root->node));
1070 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1071 chunk_root->node->start);
1072 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1073 btrfs_header_level(chunk_root->node));
1074 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1075 btrfs_header_generation(chunk_root->node));
1077 ret = write_all_supers(root);
1079 fprintf(stderr, "failed to write new super block err %d\n", ret);
1083 static int close_all_devices(struct btrfs_fs_info *fs_info)
1085 struct list_head *list;
1086 struct list_head *next;
1087 struct btrfs_device *device;
1091 list = &fs_info->fs_devices->devices;
1092 list_for_each(next, list) {
1093 device = list_entry(next, struct btrfs_device, dev_list);
1099 int close_ctree(struct btrfs_root *root)
1102 struct btrfs_trans_handle *trans;
1103 struct btrfs_fs_info *fs_info = root->fs_info;
1105 if (fs_info->last_trans_committed !=
1106 fs_info->generation) {
1107 trans = btrfs_start_transaction(root, 1);
1108 btrfs_commit_transaction(trans, root);
1109 trans = btrfs_start_transaction(root, 1);
1110 ret = commit_tree_roots(trans, fs_info);
1112 ret = __commit_transaction(trans, root);
1114 write_ctree_super(trans, root);
1115 btrfs_free_transaction(root, trans);
1117 btrfs_free_block_groups(fs_info);
1119 free_fs_roots(fs_info);
1121 if (fs_info->extent_root->node)
1122 free_extent_buffer(fs_info->extent_root->node);
1123 if (fs_info->tree_root->node)
1124 free_extent_buffer(fs_info->tree_root->node);
1125 if (fs_info->chunk_root->node)
1126 free_extent_buffer(fs_info->chunk_root->node);
1127 if (fs_info->dev_root->node)
1128 free_extent_buffer(fs_info->dev_root->node);
1129 if (fs_info->csum_root->node)
1130 free_extent_buffer(fs_info->csum_root->node);
1132 if (fs_info->log_root_tree) {
1133 if (fs_info->log_root_tree->node)
1134 free_extent_buffer(fs_info->log_root_tree->node);
1135 free(fs_info->log_root_tree);
1138 close_all_devices(fs_info);
1139 extent_io_tree_cleanup(&fs_info->extent_cache);
1140 extent_io_tree_cleanup(&fs_info->free_space_cache);
1141 extent_io_tree_cleanup(&fs_info->block_group_cache);
1142 extent_io_tree_cleanup(&fs_info->pinned_extents);
1143 extent_io_tree_cleanup(&fs_info->pending_del);
1144 extent_io_tree_cleanup(&fs_info->extent_ins);
1146 free(fs_info->tree_root);
1147 free(fs_info->extent_root);
1148 free(fs_info->chunk_root);
1149 free(fs_info->dev_root);
1150 free(fs_info->csum_root);
1156 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1157 struct extent_buffer *eb)
1159 return clear_extent_buffer_dirty(eb);
1162 int wait_on_tree_block_writeback(struct btrfs_root *root,
1163 struct extent_buffer *eb)
1168 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1170 set_extent_buffer_dirty(eb);
1173 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1177 ret = extent_buffer_uptodate(buf);
1181 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1185 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1187 return set_extent_buffer_uptodate(eb);