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 found %X "
93 "wanted %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, NULL);
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 static int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
187 unsigned long offset = 0;
188 struct btrfs_multi_bio *multi = NULL;
189 struct btrfs_device *device;
192 unsigned long bytes_left = eb->len;
195 read_len = bytes_left;
196 ret = btrfs_map_block(&info->mapping_tree, READ,
197 eb->start + offset, &read_len, &multi,
200 printk("Couldn't map the block %Lu\n", eb->start + offset);
203 device = multi->stripes[0].dev;
210 eb->dev_bytenr = multi->stripes[0].physical;
213 if (read_len > bytes_left)
214 read_len = bytes_left;
216 ret = read_extent_from_disk(eb, offset, read_len);
220 bytes_left -= read_len;
225 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
226 u32 blocksize, u64 parent_transid)
229 struct extent_buffer *eb;
230 u64 best_transid = 0;
236 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
240 if (btrfs_buffer_uptodate(eb, parent_transid))
244 ret = read_whole_eb(root->fs_info, eb, mirror_num);
245 if (ret == 0 && check_tree_block(root, eb) == 0 &&
246 csum_tree_block(root, eb, 1) == 0 &&
247 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
249 btrfs_set_buffer_uptodate(eb);
253 if (check_tree_block(root, eb))
254 printk("read block failed check_tree_block\n");
256 printk("Csum didn't match\n");
259 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
261 if (num_copies == 1) {
265 if (btrfs_header_generation(eb) > best_transid) {
266 best_transid = btrfs_header_generation(eb);
267 good_mirror = mirror_num;
270 if (mirror_num > num_copies) {
271 mirror_num = good_mirror;
276 free_extent_buffer(eb);
280 static int rmw_eb(struct btrfs_fs_info *info,
281 struct extent_buffer *eb, struct extent_buffer *orig_eb)
284 unsigned long orig_off = 0;
285 unsigned long dest_off = 0;
286 unsigned long copy_len = eb->len;
288 ret = read_whole_eb(info, eb, 0);
292 if (eb->start + eb->len <= orig_eb->start ||
293 eb->start >= orig_eb->start + orig_eb->len)
296 * | ----- orig_eb ------- |
297 * | ----- stripe ------- |
298 * | ----- orig_eb ------- |
299 * | ----- orig_eb ------- |
301 if (eb->start > orig_eb->start)
302 orig_off = eb->start - orig_eb->start;
303 if (orig_eb->start > eb->start)
304 dest_off = orig_eb->start - eb->start;
306 if (copy_len > orig_eb->len - orig_off)
307 copy_len = orig_eb->len - orig_off;
308 if (copy_len > eb->len - dest_off)
309 copy_len = eb->len - dest_off;
311 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
315 static void split_eb_for_raid56(struct btrfs_fs_info *info,
316 struct extent_buffer *orig_eb,
317 struct extent_buffer **ebs,
318 u64 stripe_len, u64 *raid_map,
321 struct extent_buffer *eb;
322 u64 start = orig_eb->start;
327 for (i = 0; i < num_stripes; i++) {
328 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
331 eb = malloc(sizeof(struct extent_buffer) + stripe_len);
334 memset(eb, 0, sizeof(struct extent_buffer) + stripe_len);
336 eb->start = raid_map[i];
337 eb->len = stripe_len;
341 eb->dev_bytenr = (u64)-1;
343 this_eb_start = raid_map[i];
345 if (start > this_eb_start ||
346 start + orig_eb->len < this_eb_start + stripe_len) {
347 ret = rmw_eb(info, eb, orig_eb);
350 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
356 static int write_raid56_with_parity(struct btrfs_fs_info *info,
357 struct extent_buffer *eb,
358 struct btrfs_multi_bio *multi,
359 u64 stripe_len, u64 *raid_map)
361 struct extent_buffer *ebs[multi->num_stripes], *p_eb = NULL, *q_eb = NULL;
365 int alloc_size = eb->len;
367 if (stripe_len > alloc_size)
368 alloc_size = stripe_len;
370 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
373 for (i = 0; i < multi->num_stripes; i++) {
374 struct extent_buffer *new_eb;
375 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
376 ebs[i]->dev_bytenr = multi->stripes[i].physical;
377 ebs[i]->fd = multi->stripes[i].dev->fd;
378 multi->stripes[i].dev->total_ios++;
379 BUG_ON(ebs[i]->start != raid_map[i]);
382 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
384 new_eb->dev_bytenr = multi->stripes[i].physical;
385 new_eb->fd = multi->stripes[i].dev->fd;
386 multi->stripes[i].dev->total_ios++;
387 new_eb->len = stripe_len;
389 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
391 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
395 void *pointers[multi->num_stripes];
396 ebs[multi->num_stripes - 2] = p_eb;
397 ebs[multi->num_stripes - 1] = q_eb;
399 for (i = 0; i < multi->num_stripes; i++)
400 pointers[i] = ebs[i]->data;
402 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
404 ebs[multi->num_stripes - 1] = p_eb;
405 memcpy(p_eb->data, ebs[0]->data, stripe_len);
406 for (j = 1; j < multi->num_stripes - 1; j++) {
407 for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
408 *(unsigned long *)(p_eb->data + i) ^=
409 *(unsigned long *)(ebs[j]->data + i);
414 for (i = 0; i < multi->num_stripes; i++) {
415 ret = write_extent_to_disk(ebs[i]);
423 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
424 struct extent_buffer *eb)
429 u64 *raid_map = NULL;
430 struct btrfs_multi_bio *multi = NULL;
432 if (check_tree_block(root, eb))
434 if (!btrfs_buffer_uptodate(eb, trans->transid))
437 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
438 csum_tree_block(root, eb, 0);
442 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
443 eb->start, &length, &multi, 0, &raid_map);
446 ret = write_raid56_with_parity(root->fs_info, eb, multi,
449 } else while (dev_nr < multi->num_stripes) {
451 eb->fd = multi->stripes[dev_nr].dev->fd;
452 eb->dev_bytenr = multi->stripes[dev_nr].physical;
453 multi->stripes[dev_nr].dev->total_ios++;
455 ret = write_extent_to_disk(eb);
462 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
463 u32 stripesize, struct btrfs_root *root,
464 struct btrfs_fs_info *fs_info, u64 objectid)
467 root->commit_root = NULL;
468 root->sectorsize = sectorsize;
469 root->nodesize = nodesize;
470 root->leafsize = leafsize;
471 root->stripesize = stripesize;
473 root->track_dirty = 0;
475 root->fs_info = fs_info;
476 root->objectid = objectid;
477 root->last_trans = 0;
478 root->highest_inode = 0;
479 root->last_inode_alloc = 0;
481 INIT_LIST_HEAD(&root->dirty_list);
482 memset(&root->root_key, 0, sizeof(root->root_key));
483 memset(&root->root_item, 0, sizeof(root->root_item));
484 root->root_key.objectid = objectid;
488 static int update_cowonly_root(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root)
493 struct btrfs_root *tree_root = root->fs_info->tree_root;
495 btrfs_write_dirty_block_groups(trans, root);
497 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
498 if (old_root_bytenr == root->node->start)
500 btrfs_set_root_bytenr(&root->root_item,
502 btrfs_set_root_generation(&root->root_item,
504 root->root_item.level = btrfs_header_level(root->node);
505 ret = btrfs_update_root(trans, tree_root,
509 btrfs_write_dirty_block_groups(trans, root);
514 static int commit_tree_roots(struct btrfs_trans_handle *trans,
515 struct btrfs_fs_info *fs_info)
517 struct btrfs_root *root;
518 struct list_head *next;
519 struct extent_buffer *eb;
522 if (fs_info->readonly)
525 eb = fs_info->tree_root->node;
526 extent_buffer_get(eb);
527 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
528 free_extent_buffer(eb);
532 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
533 next = fs_info->dirty_cowonly_roots.next;
535 root = list_entry(next, struct btrfs_root, dirty_list);
536 update_cowonly_root(trans, root);
541 static int __commit_transaction(struct btrfs_trans_handle *trans,
542 struct btrfs_root *root)
546 struct extent_buffer *eb;
547 struct extent_io_tree *tree = &root->fs_info->extent_cache;
551 ret = find_first_extent_bit(tree, 0, &start, &end,
555 while(start <= end) {
556 eb = find_first_extent_buffer(tree, start);
557 BUG_ON(!eb || eb->start != start);
558 ret = write_tree_block(trans, root, eb);
561 clear_extent_buffer_dirty(eb);
562 free_extent_buffer(eb);
568 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
569 struct btrfs_root *root)
571 u64 transid = trans->transid;
573 struct btrfs_fs_info *fs_info = root->fs_info;
575 if (root->commit_root == root->node)
578 free_extent_buffer(root->commit_root);
579 root->commit_root = NULL;
581 btrfs_set_root_bytenr(&root->root_item, root->node->start);
582 btrfs_set_root_generation(&root->root_item, trans->transid);
583 root->root_item.level = btrfs_header_level(root->node);
584 ret = btrfs_update_root(trans, root->fs_info->tree_root,
585 &root->root_key, &root->root_item);
588 ret = commit_tree_roots(trans, fs_info);
590 ret = __commit_transaction(trans, root);
592 write_ctree_super(trans, root);
593 btrfs_finish_extent_commit(trans, fs_info->extent_root,
594 &fs_info->pinned_extents);
595 btrfs_free_transaction(root, trans);
596 free_extent_buffer(root->commit_root);
597 root->commit_root = NULL;
598 fs_info->running_transaction = NULL;
599 fs_info->last_trans_committed = transid;
603 static int find_and_setup_root(struct btrfs_root *tree_root,
604 struct btrfs_fs_info *fs_info,
605 u64 objectid, struct btrfs_root *root)
611 __setup_root(tree_root->nodesize, tree_root->leafsize,
612 tree_root->sectorsize, tree_root->stripesize,
613 root, fs_info, objectid);
614 ret = btrfs_find_last_root(tree_root, objectid,
615 &root->root_item, &root->root_key);
619 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
620 generation = btrfs_root_generation(&root->root_item);
621 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
622 blocksize, generation);
623 if (!extent_buffer_uptodate(root->node))
629 static int find_and_setup_log_root(struct btrfs_root *tree_root,
630 struct btrfs_fs_info *fs_info,
631 struct btrfs_super_block *disk_super)
634 u64 blocknr = btrfs_super_log_root(disk_super);
635 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
645 blocksize = btrfs_level_size(tree_root,
646 btrfs_super_log_root_level(disk_super));
648 __setup_root(tree_root->nodesize, tree_root->leafsize,
649 tree_root->sectorsize, tree_root->stripesize,
650 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
652 log_root->node = read_tree_block(tree_root, blocknr,
654 btrfs_super_generation(disk_super) + 1);
656 fs_info->log_root_tree = log_root;
658 if (!extent_buffer_uptodate(log_root->node)) {
668 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
669 struct btrfs_root *root)
672 free_extent_buffer(root->node);
673 if (root->commit_root)
674 free_extent_buffer(root->commit_root);
679 static int free_fs_roots(struct btrfs_fs_info *fs_info)
681 struct cache_extent *cache;
682 struct btrfs_root *root;
685 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
688 root = container_of(cache, struct btrfs_root, cache);
689 remove_cache_extent(&fs_info->fs_root_cache, cache);
690 btrfs_free_fs_root(fs_info, root);
695 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
696 struct btrfs_key *location)
698 struct btrfs_root *root;
699 struct btrfs_root *tree_root = fs_info->tree_root;
700 struct btrfs_path *path;
701 struct extent_buffer *l;
706 root = malloc(sizeof(*root));
708 return ERR_PTR(-ENOMEM);
709 memset(root, 0, sizeof(*root));
710 if (location->offset == (u64)-1) {
711 ret = find_and_setup_root(tree_root, fs_info,
712 location->objectid, root);
720 __setup_root(tree_root->nodesize, tree_root->leafsize,
721 tree_root->sectorsize, tree_root->stripesize,
722 root, fs_info, location->objectid);
724 path = btrfs_alloc_path();
726 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
733 read_extent_buffer(l, &root->root_item,
734 btrfs_item_ptr_offset(l, path->slots[0]),
735 sizeof(root->root_item));
736 memcpy(&root->root_key, location, sizeof(*location));
739 btrfs_release_path(root, path);
740 btrfs_free_path(path);
745 generation = btrfs_root_generation(&root->root_item);
746 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
747 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
748 blocksize, generation);
755 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
756 struct btrfs_key *location)
758 struct btrfs_root *root;
759 struct cache_extent *cache;
762 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
763 return fs_info->tree_root;
764 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
765 return fs_info->extent_root;
766 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
767 return fs_info->chunk_root;
768 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
769 return fs_info->dev_root;
770 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
771 return fs_info->csum_root;
773 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
774 location->offset != (u64)-1);
776 cache = find_cache_extent(&fs_info->fs_root_cache,
777 location->objectid, 1);
779 return container_of(cache, struct btrfs_root, cache);
781 root = btrfs_read_fs_root_no_cache(fs_info, location);
785 root->cache.start = location->objectid;
786 root->cache.size = 1;
787 ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
793 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
795 u64 root_tree_bytenr, int writes,
804 struct btrfs_key key;
805 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
806 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
807 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
808 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
809 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
810 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
812 struct btrfs_super_block *disk_super;
813 struct btrfs_fs_devices *fs_devices = NULL;
818 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
820 /* try to drop all the caches */
821 posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED);
823 ret = btrfs_scan_one_device(fp, path, &fs_devices,
824 &total_devs, sb_bytenr);
827 fprintf(stderr, "No valid Btrfs found on %s\n", path);
831 if (total_devs != 1) {
832 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
837 memset(fs_info, 0, sizeof(*fs_info));
838 fs_info->tree_root = tree_root;
839 fs_info->extent_root = extent_root;
840 fs_info->chunk_root = chunk_root;
841 fs_info->dev_root = dev_root;
842 fs_info->csum_root = csum_root;
845 fs_info->readonly = 1;
847 extent_io_tree_init(&fs_info->extent_cache);
848 extent_io_tree_init(&fs_info->free_space_cache);
849 extent_io_tree_init(&fs_info->block_group_cache);
850 extent_io_tree_init(&fs_info->pinned_extents);
851 extent_io_tree_init(&fs_info->pending_del);
852 extent_io_tree_init(&fs_info->extent_ins);
853 cache_tree_init(&fs_info->fs_root_cache);
855 cache_tree_init(&fs_info->mapping_tree.cache_tree);
857 mutex_init(&fs_info->fs_mutex);
858 fs_info->fs_devices = fs_devices;
859 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
860 INIT_LIST_HEAD(&fs_info->space_info);
862 __setup_root(4096, 4096, 4096, 4096, tree_root,
863 fs_info, BTRFS_ROOT_TREE_OBJECTID);
866 ret = btrfs_open_devices(fs_devices, O_RDWR);
868 ret = btrfs_open_devices(fs_devices, O_RDONLY);
872 fs_info->super_bytenr = sb_bytenr;
873 disk_super = &fs_info->super_copy;
874 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
875 disk_super, sb_bytenr);
877 printk("No valid btrfs found\n");
881 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
884 features = btrfs_super_incompat_flags(disk_super) &
885 ~BTRFS_FEATURE_INCOMPAT_SUPP;
887 printk("couldn't open because of unsupported "
888 "option features (%Lx).\n",
889 (unsigned long long)features);
893 features = btrfs_super_incompat_flags(disk_super);
894 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
895 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
896 btrfs_set_super_incompat_flags(disk_super, features);
899 features = btrfs_super_compat_ro_flags(disk_super) &
900 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
901 if (writes && features) {
902 printk("couldn't open RDWR because of unsupported "
903 "option features (%Lx).\n",
904 (unsigned long long)features);
908 nodesize = btrfs_super_nodesize(disk_super);
909 leafsize = btrfs_super_leafsize(disk_super);
910 sectorsize = btrfs_super_sectorsize(disk_super);
911 stripesize = btrfs_super_stripesize(disk_super);
912 tree_root->nodesize = nodesize;
913 tree_root->leafsize = leafsize;
914 tree_root->sectorsize = sectorsize;
915 tree_root->stripesize = stripesize;
917 ret = btrfs_read_sys_array(tree_root);
920 blocksize = btrfs_level_size(tree_root,
921 btrfs_super_chunk_root_level(disk_super));
922 generation = btrfs_super_chunk_root_generation(disk_super);
924 __setup_root(nodesize, leafsize, sectorsize, stripesize,
925 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
927 chunk_root->node = read_tree_block(chunk_root,
928 btrfs_super_chunk_root(disk_super),
929 blocksize, generation);
930 if (!extent_buffer_uptodate(chunk_root->node)) {
931 printk("Couldn't read chunk root\n");
935 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
936 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
939 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
940 ret = btrfs_read_chunk_tree(chunk_root);
945 blocksize = btrfs_level_size(tree_root,
946 btrfs_super_root_level(disk_super));
947 generation = btrfs_super_generation(disk_super);
949 if (!root_tree_bytenr)
950 root_tree_bytenr = btrfs_super_root(disk_super);
951 tree_root->node = read_tree_block(tree_root,
953 blocksize, generation);
954 if (!extent_buffer_uptodate(tree_root->node)) {
955 printk("Couldn't read tree root\n");
958 ret = find_and_setup_root(tree_root, fs_info,
959 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
961 printk("Couldn't setup extent tree\n");
964 extent_root->track_dirty = 1;
966 ret = find_and_setup_root(tree_root, fs_info,
967 BTRFS_DEV_TREE_OBJECTID, dev_root);
969 printk("Couldn't setup device tree\n");
972 dev_root->track_dirty = 1;
974 ret = find_and_setup_root(tree_root, fs_info,
975 BTRFS_CSUM_TREE_OBJECTID, csum_root);
977 printk("Couldn't setup csum tree\n");
981 csum_root->track_dirty = 1;
983 find_and_setup_log_root(tree_root, fs_info, disk_super);
985 fs_info->generation = generation;
986 fs_info->last_trans_committed = generation;
987 btrfs_read_block_groups(fs_info->tree_root);
989 key.objectid = BTRFS_FS_TREE_OBJECTID;
990 key.type = BTRFS_ROOT_ITEM_KEY;
991 key.offset = (u64)-1;
992 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
994 if (!fs_info->fs_root)
997 fs_info->data_alloc_profile = (u64)-1;
998 fs_info->metadata_alloc_profile = (u64)-1;
999 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1007 if (fs_info->csum_root)
1008 free_extent_buffer(fs_info->csum_root->node);
1009 if (fs_info->dev_root)
1010 free_extent_buffer(fs_info->dev_root->node);
1011 if (fs_info->extent_root)
1012 free_extent_buffer(fs_info->extent_root->node);
1013 if (fs_info->tree_root)
1014 free_extent_buffer(fs_info->tree_root->node);
1015 if (fs_info->chunk_root)
1016 free_extent_buffer(fs_info->chunk_root->node);
1018 close_all_devices(fs_info);
1020 extent_io_tree_cleanup(&fs_info->extent_cache);
1021 extent_io_tree_cleanup(&fs_info->free_space_cache);
1022 extent_io_tree_cleanup(&fs_info->block_group_cache);
1023 extent_io_tree_cleanup(&fs_info->pinned_extents);
1024 extent_io_tree_cleanup(&fs_info->pending_del);
1025 extent_io_tree_cleanup(&fs_info->extent_ins);
1036 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1037 u64 sb_bytenr, int writes,
1041 struct btrfs_fs_info *info;
1042 int flags = O_CREAT | O_RDWR;
1047 fp = open(filename, flags, 0600);
1049 fprintf (stderr, "Could not open %s\n", filename);
1052 info = __open_ctree_fd(fp, filename, sb_bytenr, 0, writes, partial);
1057 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
1059 struct btrfs_fs_info *info;
1061 info = open_ctree_fs_info(filename, sb_bytenr, writes, 0);
1064 return info->fs_root;
1067 struct btrfs_root *open_ctree_recovery(const char *filename, u64 sb_bytenr,
1068 u64 root_tree_bytenr)
1071 struct btrfs_fs_info *info;
1074 fp = open(filename, O_RDONLY);
1076 fprintf (stderr, "Could not open %s\n", filename);
1079 info = __open_ctree_fd(fp, filename, sb_bytenr,
1080 root_tree_bytenr, 0, 0);
1085 return info->fs_root;
1088 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1091 struct btrfs_fs_info *info;
1092 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0);
1095 return info->fs_root;
1098 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
1100 u8 fsid[BTRFS_FSID_SIZE];
1101 int fsid_is_initialized = 0;
1102 struct btrfs_super_block buf;
1108 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1109 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1110 if (ret < sizeof(buf))
1113 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1114 buf.magic != cpu_to_le64(BTRFS_MAGIC))
1117 memcpy(sb, &buf, sizeof(*sb));
1121 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1122 bytenr = btrfs_sb_offset(i);
1123 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1124 if (ret < sizeof(buf))
1127 if (btrfs_super_bytenr(&buf) != bytenr )
1129 /* if magic is NULL, the device was removed */
1130 if (buf.magic == 0 && i == 0)
1132 if (buf.magic != cpu_to_le64(BTRFS_MAGIC))
1135 if (!fsid_is_initialized) {
1136 memcpy(fsid, buf.fsid, sizeof(fsid));
1137 fsid_is_initialized = 1;
1138 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1140 * the superblocks (the original one and
1141 * its backups) contain data of different
1142 * filesystems -> the super cannot be trusted
1147 if (btrfs_super_generation(&buf) > transid) {
1148 memcpy(sb, &buf, sizeof(*sb));
1149 transid = btrfs_super_generation(&buf);
1153 return transid > 0 ? 0 : -1;
1156 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
1157 struct btrfs_device *device)
1164 buf = calloc(1, BTRFS_SUPER_INFO_SIZE);
1167 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1168 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1170 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1171 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1172 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1174 memcpy(buf, sb, sizeof(*sb));
1175 ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE,
1176 root->fs_info->super_bytenr);
1177 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1181 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1182 bytenr = btrfs_sb_offset(i);
1183 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1186 btrfs_set_super_bytenr(sb, bytenr);
1189 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1190 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1191 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1193 memcpy(buf, sb, sizeof(*sb));
1194 ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1195 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1202 int write_all_supers(struct btrfs_root *root)
1204 struct list_head *cur;
1205 struct list_head *head = &root->fs_info->fs_devices->devices;
1206 struct btrfs_device *dev;
1207 struct btrfs_super_block *sb;
1208 struct btrfs_dev_item *dev_item;
1212 sb = &root->fs_info->super_copy;
1213 dev_item = &sb->dev_item;
1214 list_for_each(cur, head) {
1215 dev = list_entry(cur, struct btrfs_device, dev_list);
1216 if (!dev->writeable)
1219 btrfs_set_stack_device_generation(dev_item, 0);
1220 btrfs_set_stack_device_type(dev_item, dev->type);
1221 btrfs_set_stack_device_id(dev_item, dev->devid);
1222 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1223 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1224 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1225 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1226 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1227 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1228 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1230 flags = btrfs_super_flags(sb);
1231 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1233 ret = write_dev_supers(root, sb, dev);
1239 int write_ctree_super(struct btrfs_trans_handle *trans,
1240 struct btrfs_root *root)
1243 struct btrfs_root *tree_root = root->fs_info->tree_root;
1244 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1246 if (root->fs_info->readonly)
1249 btrfs_set_super_generation(&root->fs_info->super_copy,
1251 btrfs_set_super_root(&root->fs_info->super_copy,
1252 tree_root->node->start);
1253 btrfs_set_super_root_level(&root->fs_info->super_copy,
1254 btrfs_header_level(tree_root->node));
1255 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1256 chunk_root->node->start);
1257 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1258 btrfs_header_level(chunk_root->node));
1259 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1260 btrfs_header_generation(chunk_root->node));
1262 ret = write_all_supers(root);
1264 fprintf(stderr, "failed to write new super block err %d\n", ret);
1268 static int close_all_devices(struct btrfs_fs_info *fs_info)
1270 struct list_head *list;
1271 struct list_head *next;
1272 struct btrfs_device *device;
1276 list = &fs_info->fs_devices->devices;
1277 list_for_each(next, list) {
1278 device = list_entry(next, struct btrfs_device, dev_list);
1281 posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED);
1288 int close_ctree(struct btrfs_root *root)
1291 struct btrfs_trans_handle *trans;
1292 struct btrfs_fs_info *fs_info = root->fs_info;
1294 if (fs_info->last_trans_committed !=
1295 fs_info->generation) {
1296 trans = btrfs_start_transaction(root, 1);
1297 btrfs_commit_transaction(trans, root);
1298 trans = btrfs_start_transaction(root, 1);
1299 ret = commit_tree_roots(trans, fs_info);
1301 ret = __commit_transaction(trans, root);
1303 write_ctree_super(trans, root);
1304 btrfs_free_transaction(root, trans);
1306 btrfs_free_block_groups(fs_info);
1308 free_fs_roots(fs_info);
1310 if (fs_info->extent_root->node)
1311 free_extent_buffer(fs_info->extent_root->node);
1312 if (fs_info->tree_root->node)
1313 free_extent_buffer(fs_info->tree_root->node);
1314 if (fs_info->chunk_root->node)
1315 free_extent_buffer(fs_info->chunk_root->node);
1316 if (fs_info->dev_root->node)
1317 free_extent_buffer(fs_info->dev_root->node);
1318 if (fs_info->csum_root->node)
1319 free_extent_buffer(fs_info->csum_root->node);
1321 if (fs_info->log_root_tree) {
1322 if (fs_info->log_root_tree->node)
1323 free_extent_buffer(fs_info->log_root_tree->node);
1324 free(fs_info->log_root_tree);
1327 close_all_devices(fs_info);
1328 extent_io_tree_cleanup(&fs_info->extent_cache);
1329 extent_io_tree_cleanup(&fs_info->free_space_cache);
1330 extent_io_tree_cleanup(&fs_info->block_group_cache);
1331 extent_io_tree_cleanup(&fs_info->pinned_extents);
1332 extent_io_tree_cleanup(&fs_info->pending_del);
1333 extent_io_tree_cleanup(&fs_info->extent_ins);
1335 free(fs_info->tree_root);
1336 free(fs_info->extent_root);
1337 free(fs_info->chunk_root);
1338 free(fs_info->dev_root);
1339 free(fs_info->csum_root);
1345 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1346 struct extent_buffer *eb)
1348 return clear_extent_buffer_dirty(eb);
1351 int wait_on_tree_block_writeback(struct btrfs_root *root,
1352 struct extent_buffer *eb)
1357 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1359 set_extent_buffer_dirty(eb);
1362 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1366 ret = extent_buffer_uptodate(buf);
1370 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1374 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1376 return set_extent_buffer_uptodate(eb);