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 static int write_tree_block(struct btrfs_trans_handle *trans,
309 struct btrfs_root *root,
310 struct extent_buffer *eb)
315 u64 *raid_map = NULL;
316 struct btrfs_multi_bio *multi = NULL;
318 if (check_tree_block(root, eb))
321 if (!btrfs_buffer_uptodate(eb, trans->transid))
324 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
325 csum_tree_block(root, eb, 0);
329 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
330 eb->start, &length, &multi, 0, &raid_map);
333 ret = write_raid56_with_parity(root->fs_info, eb, multi,
336 } else while (dev_nr < multi->num_stripes) {
338 eb->fd = multi->stripes[dev_nr].dev->fd;
339 eb->dev_bytenr = multi->stripes[dev_nr].physical;
340 multi->stripes[dev_nr].dev->total_ios++;
342 ret = write_extent_to_disk(eb);
349 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
350 u32 stripesize, struct btrfs_root *root,
351 struct btrfs_fs_info *fs_info, u64 objectid)
354 root->commit_root = NULL;
355 root->sectorsize = sectorsize;
356 root->nodesize = nodesize;
357 root->leafsize = leafsize;
358 root->stripesize = stripesize;
360 root->track_dirty = 0;
362 root->fs_info = fs_info;
363 root->objectid = objectid;
364 root->last_trans = 0;
365 root->highest_inode = 0;
366 root->last_inode_alloc = 0;
368 INIT_LIST_HEAD(&root->dirty_list);
369 memset(&root->root_key, 0, sizeof(root->root_key));
370 memset(&root->root_item, 0, sizeof(root->root_item));
371 root->root_key.objectid = objectid;
375 static int update_cowonly_root(struct btrfs_trans_handle *trans,
376 struct btrfs_root *root)
380 struct btrfs_root *tree_root = root->fs_info->tree_root;
382 btrfs_write_dirty_block_groups(trans, root);
384 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
385 if (old_root_bytenr == root->node->start)
387 btrfs_set_root_bytenr(&root->root_item,
389 btrfs_set_root_generation(&root->root_item,
391 root->root_item.level = btrfs_header_level(root->node);
392 ret = btrfs_update_root(trans, tree_root,
396 btrfs_write_dirty_block_groups(trans, root);
401 static int commit_tree_roots(struct btrfs_trans_handle *trans,
402 struct btrfs_fs_info *fs_info)
404 struct btrfs_root *root;
405 struct list_head *next;
406 struct extent_buffer *eb;
409 if (fs_info->readonly)
412 eb = fs_info->tree_root->node;
413 extent_buffer_get(eb);
414 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
415 free_extent_buffer(eb);
419 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
420 next = fs_info->dirty_cowonly_roots.next;
422 root = list_entry(next, struct btrfs_root, dirty_list);
423 update_cowonly_root(trans, root);
424 free_extent_buffer(root->commit_root);
425 root->commit_root = NULL;
431 static int __commit_transaction(struct btrfs_trans_handle *trans,
432 struct btrfs_root *root)
436 struct extent_buffer *eb;
437 struct extent_io_tree *tree = &root->fs_info->extent_cache;
441 ret = find_first_extent_bit(tree, 0, &start, &end,
445 while(start <= end) {
446 eb = find_first_extent_buffer(tree, start);
447 BUG_ON(!eb || eb->start != start);
448 ret = write_tree_block(trans, root, eb);
451 clear_extent_buffer_dirty(eb);
452 free_extent_buffer(eb);
458 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
459 struct btrfs_root *root)
461 u64 transid = trans->transid;
463 struct btrfs_fs_info *fs_info = root->fs_info;
465 if (root->commit_root == root->node)
468 free_extent_buffer(root->commit_root);
469 root->commit_root = NULL;
471 btrfs_set_root_bytenr(&root->root_item, root->node->start);
472 btrfs_set_root_generation(&root->root_item, trans->transid);
473 root->root_item.level = btrfs_header_level(root->node);
474 ret = btrfs_update_root(trans, root->fs_info->tree_root,
475 &root->root_key, &root->root_item);
478 ret = commit_tree_roots(trans, fs_info);
480 ret = __commit_transaction(trans, root);
482 write_ctree_super(trans, root);
483 btrfs_finish_extent_commit(trans, fs_info->extent_root,
484 &fs_info->pinned_extents);
485 btrfs_free_transaction(root, trans);
486 free_extent_buffer(root->commit_root);
487 root->commit_root = NULL;
488 fs_info->running_transaction = NULL;
489 fs_info->last_trans_committed = transid;
493 static int find_and_setup_root(struct btrfs_root *tree_root,
494 struct btrfs_fs_info *fs_info,
495 u64 objectid, struct btrfs_root *root)
501 __setup_root(tree_root->nodesize, tree_root->leafsize,
502 tree_root->sectorsize, tree_root->stripesize,
503 root, fs_info, objectid);
504 ret = btrfs_find_last_root(tree_root, objectid,
505 &root->root_item, &root->root_key);
509 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
510 generation = btrfs_root_generation(&root->root_item);
511 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
512 blocksize, generation);
513 if (!extent_buffer_uptodate(root->node))
519 static int find_and_setup_log_root(struct btrfs_root *tree_root,
520 struct btrfs_fs_info *fs_info,
521 struct btrfs_super_block *disk_super)
524 u64 blocknr = btrfs_super_log_root(disk_super);
525 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
535 blocksize = btrfs_level_size(tree_root,
536 btrfs_super_log_root_level(disk_super));
538 __setup_root(tree_root->nodesize, tree_root->leafsize,
539 tree_root->sectorsize, tree_root->stripesize,
540 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
542 log_root->node = read_tree_block(tree_root, blocknr,
544 btrfs_super_generation(disk_super) + 1);
546 fs_info->log_root_tree = log_root;
548 if (!extent_buffer_uptodate(log_root->node)) {
549 free_extent_buffer(log_root->node);
551 fs_info->log_root_tree = NULL;
559 int btrfs_free_fs_root(struct btrfs_root *root)
562 free_extent_buffer(root->node);
563 if (root->commit_root)
564 free_extent_buffer(root->commit_root);
569 static void __free_fs_root(struct rb_node *node)
571 struct btrfs_root *root;
573 root = container_of(node, struct btrfs_root, rb_node);
574 btrfs_free_fs_root(root);
577 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
579 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
580 struct btrfs_key *location)
582 struct btrfs_root *root;
583 struct btrfs_root *tree_root = fs_info->tree_root;
584 struct btrfs_path *path;
585 struct extent_buffer *l;
590 root = malloc(sizeof(*root));
592 return ERR_PTR(-ENOMEM);
593 memset(root, 0, sizeof(*root));
594 if (location->offset == (u64)-1) {
595 ret = find_and_setup_root(tree_root, fs_info,
596 location->objectid, root);
604 __setup_root(tree_root->nodesize, tree_root->leafsize,
605 tree_root->sectorsize, tree_root->stripesize,
606 root, fs_info, location->objectid);
608 path = btrfs_alloc_path();
610 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
617 read_extent_buffer(l, &root->root_item,
618 btrfs_item_ptr_offset(l, path->slots[0]),
619 sizeof(root->root_item));
620 memcpy(&root->root_key, location, sizeof(*location));
623 btrfs_release_path(path);
624 btrfs_free_path(path);
629 generation = btrfs_root_generation(&root->root_item);
630 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
631 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
632 blocksize, generation);
639 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
642 u64 objectid = *((u64 *)data);
643 struct btrfs_root *root;
645 root = rb_entry(node, struct btrfs_root, rb_node);
646 if (objectid > root->objectid)
648 else if (objectid < root->objectid)
654 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
655 struct rb_node *node2)
657 struct btrfs_root *root;
659 root = rb_entry(node2, struct btrfs_root, rb_node);
660 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
663 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
664 struct btrfs_key *location)
666 struct btrfs_root *root;
667 struct rb_node *node;
670 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
671 return fs_info->tree_root;
672 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
673 return fs_info->extent_root;
674 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
675 return fs_info->chunk_root;
676 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
677 return fs_info->dev_root;
678 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
679 return fs_info->csum_root;
681 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
682 location->offset != (u64)-1);
684 node = rb_search(&fs_info->fs_root_tree, (void *)&location->objectid,
685 btrfs_fs_roots_compare_objectids, NULL);
687 return container_of(node, struct btrfs_root, rb_node);
689 root = btrfs_read_fs_root_no_cache(fs_info, location);
693 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
694 btrfs_fs_roots_compare_roots);
699 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
701 free(fs_info->tree_root);
702 free(fs_info->extent_root);
703 free(fs_info->chunk_root);
704 free(fs_info->dev_root);
705 free(fs_info->csum_root);
706 free(fs_info->super_copy);
707 free(fs_info->log_root_tree);
711 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
713 struct btrfs_fs_info *fs_info;
715 fs_info = malloc(sizeof(struct btrfs_fs_info));
719 memset(fs_info, 0, sizeof(struct btrfs_fs_info));
721 fs_info->tree_root = malloc(sizeof(struct btrfs_root));
722 fs_info->extent_root = malloc(sizeof(struct btrfs_root));
723 fs_info->chunk_root = malloc(sizeof(struct btrfs_root));
724 fs_info->dev_root = malloc(sizeof(struct btrfs_root));
725 fs_info->csum_root = malloc(sizeof(struct btrfs_root));
726 fs_info->super_copy = malloc(BTRFS_SUPER_INFO_SIZE);
728 if (!fs_info->tree_root || !fs_info->extent_root ||
729 !fs_info->chunk_root || !fs_info->dev_root ||
730 !fs_info->csum_root || !fs_info->super_copy)
733 memset(fs_info->super_copy, 0, BTRFS_SUPER_INFO_SIZE);
734 memset(fs_info->tree_root, 0, sizeof(struct btrfs_root));
735 memset(fs_info->extent_root, 0, sizeof(struct btrfs_root));
736 memset(fs_info->chunk_root, 0, sizeof(struct btrfs_root));
737 memset(fs_info->dev_root, 0, sizeof(struct btrfs_root));
738 memset(fs_info->csum_root, 0, sizeof(struct btrfs_root));
740 extent_io_tree_init(&fs_info->extent_cache);
741 extent_io_tree_init(&fs_info->free_space_cache);
742 extent_io_tree_init(&fs_info->block_group_cache);
743 extent_io_tree_init(&fs_info->pinned_extents);
744 extent_io_tree_init(&fs_info->pending_del);
745 extent_io_tree_init(&fs_info->extent_ins);
746 fs_info->fs_root_tree = RB_ROOT;
747 cache_tree_init(&fs_info->mapping_tree.cache_tree);
749 mutex_init(&fs_info->fs_mutex);
750 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
751 INIT_LIST_HEAD(&fs_info->space_info);
754 fs_info->readonly = 1;
756 fs_info->super_bytenr = sb_bytenr;
757 fs_info->data_alloc_profile = (u64)-1;
758 fs_info->metadata_alloc_profile = (u64)-1;
759 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
762 btrfs_free_fs_info(fs_info);
766 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
770 features = btrfs_super_incompat_flags(sb) &
771 ~BTRFS_FEATURE_INCOMPAT_SUPP;
773 printk("couldn't open because of unsupported "
774 "option features (%Lx).\n",
775 (unsigned long long)features);
779 features = btrfs_super_incompat_flags(sb);
780 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
781 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
782 btrfs_set_super_incompat_flags(sb, features);
785 features = btrfs_super_compat_ro_flags(sb) &
786 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
787 if (writable && features) {
788 printk("couldn't open RDWR because of unsupported "
789 "option features (%Lx).\n",
790 (unsigned long long)features);
796 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info,
797 u64 root_tree_bytenr, int partial)
799 struct btrfs_super_block *sb = fs_info->super_copy;
800 struct btrfs_root *root;
801 struct btrfs_key key;
810 nodesize = btrfs_super_nodesize(sb);
811 leafsize = btrfs_super_leafsize(sb);
812 sectorsize = btrfs_super_sectorsize(sb);
813 stripesize = btrfs_super_stripesize(sb);
815 root = fs_info->tree_root;
816 __setup_root(nodesize, leafsize, sectorsize, stripesize,
817 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
818 blocksize = btrfs_level_size(root, btrfs_super_root_level(sb));
819 generation = btrfs_super_generation(sb);
821 if (!root_tree_bytenr)
822 root_tree_bytenr = btrfs_super_root(sb);
823 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
825 if (!extent_buffer_uptodate(root->node)) {
826 fprintf(stderr, "Couldn't read tree root\n");
830 ret = find_and_setup_root(root, fs_info, BTRFS_EXTENT_TREE_OBJECTID,
831 fs_info->extent_root);
833 printk("Couldn't setup extent tree\n");
836 fs_info->extent_root->track_dirty = 1;
838 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
841 printk("Couldn't setup device tree\n");
844 fs_info->dev_root->track_dirty = 1;
846 ret = find_and_setup_root(root, fs_info, BTRFS_CSUM_TREE_OBJECTID,
849 printk("Couldn't setup csum tree\n");
853 fs_info->csum_root->track_dirty = 1;
855 ret = find_and_setup_log_root(root, fs_info, sb);
857 printk("Couldn't setup log root tree\n");
861 fs_info->generation = generation;
862 fs_info->last_trans_committed = generation;
863 btrfs_read_block_groups(fs_info->tree_root);
865 key.objectid = BTRFS_FS_TREE_OBJECTID;
866 key.type = BTRFS_ROOT_ITEM_KEY;
867 key.offset = (u64)-1;
868 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
870 if (!fs_info->fs_root)
875 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
877 if (fs_info->csum_root)
878 free_extent_buffer(fs_info->csum_root->node);
879 if (fs_info->dev_root)
880 free_extent_buffer(fs_info->dev_root->node);
881 if (fs_info->extent_root)
882 free_extent_buffer(fs_info->extent_root->node);
883 if (fs_info->tree_root)
884 free_extent_buffer(fs_info->tree_root->node);
885 if (fs_info->log_root_tree)
886 free_extent_buffer(fs_info->log_root_tree->node);
887 if (fs_info->chunk_root)
888 free_extent_buffer(fs_info->chunk_root->node);
891 static void free_map_lookup(struct cache_extent *ce)
893 struct map_lookup *map;
895 map = container_of(ce, struct map_lookup, ce);
899 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
901 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
903 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
904 extent_io_tree_cleanup(&fs_info->extent_cache);
905 extent_io_tree_cleanup(&fs_info->free_space_cache);
906 extent_io_tree_cleanup(&fs_info->block_group_cache);
907 extent_io_tree_cleanup(&fs_info->pinned_extents);
908 extent_io_tree_cleanup(&fs_info->pending_del);
909 extent_io_tree_cleanup(&fs_info->extent_ins);
912 int btrfs_scan_fs_devices(int fd, const char *path,
913 struct btrfs_fs_devices **fs_devices,
914 u64 sb_bytenr, int run_ioctl)
919 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
921 ret = btrfs_scan_one_device(fd, path, fs_devices,
922 &total_devs, sb_bytenr);
924 fprintf(stderr, "No valid Btrfs found on %s\n", path);
928 if (total_devs != 1) {
929 ret = btrfs_scan_for_fsid(run_ioctl);
936 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info)
938 struct btrfs_super_block *sb = fs_info->super_copy;
947 nodesize = btrfs_super_nodesize(sb);
948 leafsize = btrfs_super_leafsize(sb);
949 sectorsize = btrfs_super_sectorsize(sb);
950 stripesize = btrfs_super_stripesize(sb);
952 __setup_root(nodesize, leafsize, sectorsize, stripesize,
953 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
955 ret = btrfs_read_sys_array(fs_info->chunk_root);
959 blocksize = btrfs_level_size(fs_info->chunk_root,
960 btrfs_super_chunk_root_level(sb));
961 generation = btrfs_super_chunk_root_generation(sb);
963 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
964 btrfs_super_chunk_root(sb),
965 blocksize, generation);
966 if (!fs_info->chunk_root->node ||
967 !extent_buffer_uptodate(fs_info->chunk_root->node)) {
968 fprintf(stderr, "Couldn't read chunk root\n");
972 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
973 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
975 fprintf(stderr, "Couldn't read chunk tree\n");
982 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
984 u64 root_tree_bytenr, int writes,
985 int partial, int restore,
988 struct btrfs_fs_info *fs_info;
989 struct btrfs_super_block *disk_super;
990 struct btrfs_fs_devices *fs_devices = NULL;
991 struct extent_buffer *eb;
995 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
997 /* try to drop all the caches */
998 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
999 fprintf(stderr, "Warning, could not drop caches\n");
1001 fs_info = btrfs_new_fs_info(writes, sb_bytenr);
1003 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1007 fs_info->on_restoring = 1;
1009 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
1014 fs_info->fs_devices = fs_devices;
1016 ret = btrfs_open_devices(fs_devices, O_RDWR);
1018 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1023 disk_super = fs_info->super_copy;
1025 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
1026 disk_super, sb_bytenr);
1028 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr);
1030 printk("No valid btrfs found\n");
1034 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1036 ret = btrfs_check_fs_compatibility(fs_info->super_copy, writes);
1040 ret = btrfs_setup_chunk_tree_and_device_map(fs_info);
1044 eb = fs_info->chunk_root->node;
1045 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1046 (unsigned long)btrfs_header_chunk_tree_uuid(eb),
1049 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, partial);
1059 btrfs_release_all_roots(fs_info);
1060 btrfs_cleanup_all_caches(fs_info);
1062 btrfs_close_devices(fs_devices);
1064 btrfs_free_fs_info(fs_info);
1068 struct btrfs_fs_info *open_ctree_fs_info_restore(const char *filename,
1069 u64 sb_bytenr, u64 root_tree_bytenr,
1070 int writes, int partial)
1073 struct btrfs_fs_info *info;
1074 int flags = O_CREAT | O_RDWR;
1080 fp = open(filename, flags, 0600);
1082 fprintf (stderr, "Could not open %s\n", filename);
1085 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1086 writes, partial, restore, 0);
1091 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1092 u64 sb_bytenr, u64 root_tree_bytenr,
1093 int writes, int partial)
1096 struct btrfs_fs_info *info;
1097 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, 0, 0);
1113 struct btrfs_root *open_ctree_with_broken_super(const char *filename,
1114 u64 sb_bytenr, int writes)
1117 struct btrfs_fs_info *info;
1118 int flags = O_CREAT | O_RDWR;
1123 fp = open(filename, flags, 0600);
1125 fprintf(stderr, "Could not open %s\n", filename);
1128 info = __open_ctree_fd(fp, filename, sb_bytenr, 0,
1132 return info->fs_root;
1136 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
1138 struct btrfs_fs_info *info;
1140 info = open_ctree_fs_info(filename, sb_bytenr, 0, writes, 0);
1143 return info->fs_root;
1146 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1149 struct btrfs_fs_info *info;
1150 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0, 0, 0);
1153 return info->fs_root;
1156 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
1158 u8 fsid[BTRFS_FSID_SIZE];
1159 int fsid_is_initialized = 0;
1160 struct btrfs_super_block buf;
1166 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1167 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1168 if (ret < sizeof(buf))
1171 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1172 btrfs_super_magic(&buf) != BTRFS_MAGIC)
1175 memcpy(sb, &buf, sizeof(*sb));
1179 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1180 bytenr = btrfs_sb_offset(i);
1181 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1182 if (ret < sizeof(buf))
1185 if (btrfs_super_bytenr(&buf) != bytenr )
1187 /* if magic is NULL, the device was removed */
1188 if (btrfs_super_magic(&buf) == 0 && i == 0)
1190 if (btrfs_super_magic(&buf) != BTRFS_MAGIC)
1193 if (!fsid_is_initialized) {
1194 memcpy(fsid, buf.fsid, sizeof(fsid));
1195 fsid_is_initialized = 1;
1196 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1198 * the superblocks (the original one and
1199 * its backups) contain data of different
1200 * filesystems -> the super cannot be trusted
1205 if (btrfs_super_generation(&buf) > transid) {
1206 memcpy(sb, &buf, sizeof(*sb));
1207 transid = btrfs_super_generation(&buf);
1211 return transid > 0 ? 0 : -1;
1214 static int write_dev_supers(struct btrfs_root *root,
1215 struct btrfs_super_block *sb,
1216 struct btrfs_device *device)
1222 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1223 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1225 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1226 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1227 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1230 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1231 * zero filled, we can use it directly
1233 ret = pwrite64(device->fd, root->fs_info->super_copy,
1234 BTRFS_SUPER_INFO_SIZE,
1235 root->fs_info->super_bytenr);
1236 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1240 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1241 bytenr = btrfs_sb_offset(i);
1242 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1245 btrfs_set_super_bytenr(sb, bytenr);
1248 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1249 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1250 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1253 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1254 * zero filled, we can use it directly
1256 ret = pwrite64(device->fd, root->fs_info->super_copy,
1257 BTRFS_SUPER_INFO_SIZE, bytenr);
1258 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1264 int write_all_supers(struct btrfs_root *root)
1266 struct list_head *cur;
1267 struct list_head *head = &root->fs_info->fs_devices->devices;
1268 struct btrfs_device *dev;
1269 struct btrfs_super_block *sb;
1270 struct btrfs_dev_item *dev_item;
1274 sb = root->fs_info->super_copy;
1275 dev_item = &sb->dev_item;
1276 list_for_each(cur, head) {
1277 dev = list_entry(cur, struct btrfs_device, dev_list);
1278 if (!dev->writeable)
1281 btrfs_set_stack_device_generation(dev_item, 0);
1282 btrfs_set_stack_device_type(dev_item, dev->type);
1283 btrfs_set_stack_device_id(dev_item, dev->devid);
1284 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1285 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1286 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1287 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1288 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1289 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1290 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1292 flags = btrfs_super_flags(sb);
1293 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1295 ret = write_dev_supers(root, sb, dev);
1301 int write_ctree_super(struct btrfs_trans_handle *trans,
1302 struct btrfs_root *root)
1305 struct btrfs_root *tree_root = root->fs_info->tree_root;
1306 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1308 if (root->fs_info->readonly)
1311 btrfs_set_super_generation(root->fs_info->super_copy,
1313 btrfs_set_super_root(root->fs_info->super_copy,
1314 tree_root->node->start);
1315 btrfs_set_super_root_level(root->fs_info->super_copy,
1316 btrfs_header_level(tree_root->node));
1317 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1318 chunk_root->node->start);
1319 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1320 btrfs_header_level(chunk_root->node));
1321 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1322 btrfs_header_generation(chunk_root->node));
1324 ret = write_all_supers(root);
1326 fprintf(stderr, "failed to write new super block err %d\n", ret);
1330 int close_ctree(struct btrfs_root *root)
1333 struct btrfs_trans_handle *trans;
1334 struct btrfs_fs_info *fs_info = root->fs_info;
1336 if (fs_info->last_trans_committed !=
1337 fs_info->generation) {
1338 trans = btrfs_start_transaction(root, 1);
1339 btrfs_commit_transaction(trans, root);
1340 trans = btrfs_start_transaction(root, 1);
1341 ret = commit_tree_roots(trans, fs_info);
1343 ret = __commit_transaction(trans, root);
1345 write_ctree_super(trans, root);
1346 btrfs_free_transaction(root, trans);
1348 btrfs_free_block_groups(fs_info);
1350 free_fs_roots_tree(&fs_info->fs_root_tree);
1352 btrfs_release_all_roots(fs_info);
1353 btrfs_close_devices(fs_info->fs_devices);
1354 btrfs_cleanup_all_caches(fs_info);
1355 btrfs_free_fs_info(fs_info);
1359 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1360 struct extent_buffer *eb)
1362 return clear_extent_buffer_dirty(eb);
1365 int wait_on_tree_block_writeback(struct btrfs_root *root,
1366 struct extent_buffer *eb)
1371 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1373 set_extent_buffer_dirty(eb);
1376 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1380 ret = extent_buffer_uptodate(buf);
1384 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1388 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1390 return set_extent_buffer_uptodate(eb);