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);
618 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
619 generation = btrfs_root_generation(&root->root_item);
620 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
621 blocksize, generation);
622 if (!extent_buffer_uptodate(root->node))
628 static int find_and_setup_log_root(struct btrfs_root *tree_root,
629 struct btrfs_fs_info *fs_info,
630 struct btrfs_super_block *disk_super)
633 u64 blocknr = btrfs_super_log_root(disk_super);
634 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
644 blocksize = btrfs_level_size(tree_root,
645 btrfs_super_log_root_level(disk_super));
647 __setup_root(tree_root->nodesize, tree_root->leafsize,
648 tree_root->sectorsize, tree_root->stripesize,
649 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
651 log_root->node = read_tree_block(tree_root, blocknr,
653 btrfs_super_generation(disk_super) + 1);
655 fs_info->log_root_tree = log_root;
657 if (!extent_buffer_uptodate(log_root->node)) {
667 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
668 struct btrfs_root *root)
671 free_extent_buffer(root->node);
672 if (root->commit_root)
673 free_extent_buffer(root->commit_root);
678 static int free_fs_roots(struct btrfs_fs_info *fs_info)
680 struct cache_extent *cache;
681 struct btrfs_root *root;
684 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
687 root = container_of(cache, struct btrfs_root, cache);
688 remove_cache_extent(&fs_info->fs_root_cache, cache);
689 btrfs_free_fs_root(fs_info, root);
694 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
695 struct btrfs_key *location)
697 struct btrfs_root *root;
698 struct btrfs_root *tree_root = fs_info->tree_root;
699 struct btrfs_path *path;
700 struct extent_buffer *l;
705 root = malloc(sizeof(*root));
707 return ERR_PTR(-ENOMEM);
708 memset(root, 0, sizeof(*root));
709 if (location->offset == (u64)-1) {
710 ret = find_and_setup_root(tree_root, fs_info,
711 location->objectid, root);
719 __setup_root(tree_root->nodesize, tree_root->leafsize,
720 tree_root->sectorsize, tree_root->stripesize,
721 root, fs_info, location->objectid);
723 path = btrfs_alloc_path();
725 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
732 read_extent_buffer(l, &root->root_item,
733 btrfs_item_ptr_offset(l, path->slots[0]),
734 sizeof(root->root_item));
735 memcpy(&root->root_key, location, sizeof(*location));
738 btrfs_release_path(root, path);
739 btrfs_free_path(path);
744 generation = btrfs_root_generation(&root->root_item);
745 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
746 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
747 blocksize, generation);
754 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
755 struct btrfs_key *location)
757 struct btrfs_root *root;
758 struct cache_extent *cache;
761 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
762 return fs_info->tree_root;
763 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
764 return fs_info->extent_root;
765 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
766 return fs_info->chunk_root;
767 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
768 return fs_info->dev_root;
769 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
770 return fs_info->csum_root;
772 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
773 location->offset != (u64)-1);
775 cache = find_cache_extent(&fs_info->fs_root_cache,
776 location->objectid, 1);
778 return container_of(cache, struct btrfs_root, cache);
780 root = btrfs_read_fs_root_no_cache(fs_info, location);
784 root->cache.start = location->objectid;
785 root->cache.size = 1;
786 ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
792 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
794 u64 root_tree_bytenr, int writes,
803 struct btrfs_key key;
804 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
805 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
806 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
807 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
808 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
809 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
811 struct btrfs_super_block *disk_super;
812 struct btrfs_fs_devices *fs_devices = NULL;
817 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
819 /* try to drop all the caches */
820 posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED);
822 ret = btrfs_scan_one_device(fp, path, &fs_devices,
823 &total_devs, sb_bytenr);
826 fprintf(stderr, "No valid Btrfs found on %s\n", path);
830 if (total_devs != 1) {
831 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
836 memset(fs_info, 0, sizeof(*fs_info));
837 fs_info->tree_root = tree_root;
838 fs_info->extent_root = extent_root;
839 fs_info->chunk_root = chunk_root;
840 fs_info->dev_root = dev_root;
841 fs_info->csum_root = csum_root;
844 fs_info->readonly = 1;
846 extent_io_tree_init(&fs_info->extent_cache);
847 extent_io_tree_init(&fs_info->free_space_cache);
848 extent_io_tree_init(&fs_info->block_group_cache);
849 extent_io_tree_init(&fs_info->pinned_extents);
850 extent_io_tree_init(&fs_info->pending_del);
851 extent_io_tree_init(&fs_info->extent_ins);
852 cache_tree_init(&fs_info->fs_root_cache);
854 cache_tree_init(&fs_info->mapping_tree.cache_tree);
856 mutex_init(&fs_info->fs_mutex);
857 fs_info->fs_devices = fs_devices;
858 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
859 INIT_LIST_HEAD(&fs_info->space_info);
861 __setup_root(4096, 4096, 4096, 4096, tree_root,
862 fs_info, BTRFS_ROOT_TREE_OBJECTID);
865 ret = btrfs_open_devices(fs_devices, O_RDWR);
867 ret = btrfs_open_devices(fs_devices, O_RDONLY);
871 fs_info->super_bytenr = sb_bytenr;
872 disk_super = &fs_info->super_copy;
873 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
874 disk_super, sb_bytenr);
876 printk("No valid btrfs found\n");
880 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
883 features = btrfs_super_incompat_flags(disk_super) &
884 ~BTRFS_FEATURE_INCOMPAT_SUPP;
886 printk("couldn't open because of unsupported "
887 "option features (%Lx).\n",
888 (unsigned long long)features);
892 features = btrfs_super_incompat_flags(disk_super);
893 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
894 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
895 btrfs_set_super_incompat_flags(disk_super, features);
898 features = btrfs_super_compat_ro_flags(disk_super) &
899 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
900 if (writes && features) {
901 printk("couldn't open RDWR because of unsupported "
902 "option features (%Lx).\n",
903 (unsigned long long)features);
907 nodesize = btrfs_super_nodesize(disk_super);
908 leafsize = btrfs_super_leafsize(disk_super);
909 sectorsize = btrfs_super_sectorsize(disk_super);
910 stripesize = btrfs_super_stripesize(disk_super);
911 tree_root->nodesize = nodesize;
912 tree_root->leafsize = leafsize;
913 tree_root->sectorsize = sectorsize;
914 tree_root->stripesize = stripesize;
916 ret = btrfs_read_sys_array(tree_root);
919 blocksize = btrfs_level_size(tree_root,
920 btrfs_super_chunk_root_level(disk_super));
921 generation = btrfs_super_chunk_root_generation(disk_super);
923 __setup_root(nodesize, leafsize, sectorsize, stripesize,
924 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
926 chunk_root->node = read_tree_block(chunk_root,
927 btrfs_super_chunk_root(disk_super),
928 blocksize, generation);
929 if (!extent_buffer_uptodate(chunk_root->node)) {
930 printk("Couldn't read chunk root\n");
934 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
935 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
938 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
939 ret = btrfs_read_chunk_tree(chunk_root);
944 blocksize = btrfs_level_size(tree_root,
945 btrfs_super_root_level(disk_super));
946 generation = btrfs_super_generation(disk_super);
948 if (!root_tree_bytenr)
949 root_tree_bytenr = btrfs_super_root(disk_super);
950 tree_root->node = read_tree_block(tree_root,
952 blocksize, generation);
953 if (!extent_buffer_uptodate(tree_root->node)) {
954 printk("Couldn't read tree root\n");
957 ret = find_and_setup_root(tree_root, fs_info,
958 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
960 printk("Couldn't setup extent tree\n");
963 extent_root->track_dirty = 1;
965 ret = find_and_setup_root(tree_root, fs_info,
966 BTRFS_DEV_TREE_OBJECTID, dev_root);
968 printk("Couldn't setup device tree\n");
971 dev_root->track_dirty = 1;
973 ret = find_and_setup_root(tree_root, fs_info,
974 BTRFS_CSUM_TREE_OBJECTID, csum_root);
976 printk("Couldn't setup csum tree\n");
980 csum_root->track_dirty = 1;
982 find_and_setup_log_root(tree_root, fs_info, disk_super);
984 fs_info->generation = generation;
985 fs_info->last_trans_committed = generation;
986 btrfs_read_block_groups(fs_info->tree_root);
988 key.objectid = BTRFS_FS_TREE_OBJECTID;
989 key.type = BTRFS_ROOT_ITEM_KEY;
990 key.offset = (u64)-1;
991 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
993 if (!fs_info->fs_root)
996 fs_info->data_alloc_profile = (u64)-1;
997 fs_info->metadata_alloc_profile = (u64)-1;
998 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1006 if (fs_info->csum_root)
1007 free_extent_buffer(fs_info->csum_root->node);
1008 if (fs_info->dev_root)
1009 free_extent_buffer(fs_info->dev_root->node);
1010 if (fs_info->extent_root)
1011 free_extent_buffer(fs_info->extent_root->node);
1012 if (fs_info->tree_root)
1013 free_extent_buffer(fs_info->tree_root->node);
1014 if (fs_info->chunk_root)
1015 free_extent_buffer(fs_info->chunk_root->node);
1017 close_all_devices(fs_info);
1019 extent_io_tree_cleanup(&fs_info->extent_cache);
1020 extent_io_tree_cleanup(&fs_info->free_space_cache);
1021 extent_io_tree_cleanup(&fs_info->block_group_cache);
1022 extent_io_tree_cleanup(&fs_info->pinned_extents);
1023 extent_io_tree_cleanup(&fs_info->pending_del);
1024 extent_io_tree_cleanup(&fs_info->extent_ins);
1035 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1036 u64 sb_bytenr, int writes,
1040 struct btrfs_fs_info *info;
1041 int flags = O_CREAT | O_RDWR;
1046 fp = open(filename, flags, 0600);
1048 fprintf (stderr, "Could not open %s\n", filename);
1051 info = __open_ctree_fd(fp, filename, sb_bytenr, 0, writes, partial);
1056 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
1058 struct btrfs_fs_info *info;
1060 info = open_ctree_fs_info(filename, sb_bytenr, writes, 0);
1063 return info->fs_root;
1066 struct btrfs_root *open_ctree_recovery(const char *filename, u64 sb_bytenr,
1067 u64 root_tree_bytenr)
1070 struct btrfs_fs_info *info;
1073 fp = open(filename, O_RDONLY);
1075 fprintf (stderr, "Could not open %s\n", filename);
1078 info = __open_ctree_fd(fp, filename, sb_bytenr,
1079 root_tree_bytenr, 0, 0);
1084 return info->fs_root;
1087 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1090 struct btrfs_fs_info *info;
1091 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0);
1094 return info->fs_root;
1097 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
1099 u8 fsid[BTRFS_FSID_SIZE];
1100 int fsid_is_initialized = 0;
1101 struct btrfs_super_block buf;
1107 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1108 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1109 if (ret < sizeof(buf))
1112 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1113 buf.magic != cpu_to_le64(BTRFS_MAGIC))
1116 memcpy(sb, &buf, sizeof(*sb));
1120 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1121 bytenr = btrfs_sb_offset(i);
1122 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1123 if (ret < sizeof(buf))
1126 if (btrfs_super_bytenr(&buf) != bytenr )
1128 /* if magic is NULL, the device was removed */
1129 if (buf.magic == 0 && i == 0)
1131 if (buf.magic != cpu_to_le64(BTRFS_MAGIC))
1134 if (!fsid_is_initialized) {
1135 memcpy(fsid, buf.fsid, sizeof(fsid));
1136 fsid_is_initialized = 1;
1137 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1139 * the superblocks (the original one and
1140 * its backups) contain data of different
1141 * filesystems -> the super cannot be trusted
1146 if (btrfs_super_generation(&buf) > transid) {
1147 memcpy(sb, &buf, sizeof(*sb));
1148 transid = btrfs_super_generation(&buf);
1152 return transid > 0 ? 0 : -1;
1155 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
1156 struct btrfs_device *device)
1163 buf = calloc(1, BTRFS_SUPER_INFO_SIZE);
1166 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1167 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1169 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1170 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1171 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1173 memcpy(buf, sb, sizeof(*sb));
1174 ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE,
1175 root->fs_info->super_bytenr);
1176 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1180 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1181 bytenr = btrfs_sb_offset(i);
1182 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1185 btrfs_set_super_bytenr(sb, bytenr);
1188 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1189 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1190 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1192 memcpy(buf, sb, sizeof(*sb));
1193 ret = pwrite64(device->fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1194 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1201 int write_all_supers(struct btrfs_root *root)
1203 struct list_head *cur;
1204 struct list_head *head = &root->fs_info->fs_devices->devices;
1205 struct btrfs_device *dev;
1206 struct btrfs_super_block *sb;
1207 struct btrfs_dev_item *dev_item;
1211 sb = &root->fs_info->super_copy;
1212 dev_item = &sb->dev_item;
1213 list_for_each(cur, head) {
1214 dev = list_entry(cur, struct btrfs_device, dev_list);
1215 if (!dev->writeable)
1218 btrfs_set_stack_device_generation(dev_item, 0);
1219 btrfs_set_stack_device_type(dev_item, dev->type);
1220 btrfs_set_stack_device_id(dev_item, dev->devid);
1221 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1222 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1223 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1224 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1225 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1226 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1227 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1229 flags = btrfs_super_flags(sb);
1230 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1232 ret = write_dev_supers(root, sb, dev);
1238 int write_ctree_super(struct btrfs_trans_handle *trans,
1239 struct btrfs_root *root)
1242 struct btrfs_root *tree_root = root->fs_info->tree_root;
1243 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1245 if (root->fs_info->readonly)
1248 btrfs_set_super_generation(&root->fs_info->super_copy,
1250 btrfs_set_super_root(&root->fs_info->super_copy,
1251 tree_root->node->start);
1252 btrfs_set_super_root_level(&root->fs_info->super_copy,
1253 btrfs_header_level(tree_root->node));
1254 btrfs_set_super_chunk_root(&root->fs_info->super_copy,
1255 chunk_root->node->start);
1256 btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
1257 btrfs_header_level(chunk_root->node));
1258 btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
1259 btrfs_header_generation(chunk_root->node));
1261 ret = write_all_supers(root);
1263 fprintf(stderr, "failed to write new super block err %d\n", ret);
1267 static int close_all_devices(struct btrfs_fs_info *fs_info)
1269 struct list_head *list;
1270 struct list_head *next;
1271 struct btrfs_device *device;
1275 list = &fs_info->fs_devices->devices;
1276 list_for_each(next, list) {
1277 device = list_entry(next, struct btrfs_device, dev_list);
1280 posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED);
1287 int close_ctree(struct btrfs_root *root)
1290 struct btrfs_trans_handle *trans;
1291 struct btrfs_fs_info *fs_info = root->fs_info;
1293 if (fs_info->last_trans_committed !=
1294 fs_info->generation) {
1295 trans = btrfs_start_transaction(root, 1);
1296 btrfs_commit_transaction(trans, root);
1297 trans = btrfs_start_transaction(root, 1);
1298 ret = commit_tree_roots(trans, fs_info);
1300 ret = __commit_transaction(trans, root);
1302 write_ctree_super(trans, root);
1303 btrfs_free_transaction(root, trans);
1305 btrfs_free_block_groups(fs_info);
1307 free_fs_roots(fs_info);
1309 if (fs_info->extent_root->node)
1310 free_extent_buffer(fs_info->extent_root->node);
1311 if (fs_info->tree_root->node)
1312 free_extent_buffer(fs_info->tree_root->node);
1313 if (fs_info->chunk_root->node)
1314 free_extent_buffer(fs_info->chunk_root->node);
1315 if (fs_info->dev_root->node)
1316 free_extent_buffer(fs_info->dev_root->node);
1317 if (fs_info->csum_root->node)
1318 free_extent_buffer(fs_info->csum_root->node);
1320 if (fs_info->log_root_tree) {
1321 if (fs_info->log_root_tree->node)
1322 free_extent_buffer(fs_info->log_root_tree->node);
1323 free(fs_info->log_root_tree);
1326 close_all_devices(fs_info);
1327 extent_io_tree_cleanup(&fs_info->extent_cache);
1328 extent_io_tree_cleanup(&fs_info->free_space_cache);
1329 extent_io_tree_cleanup(&fs_info->block_group_cache);
1330 extent_io_tree_cleanup(&fs_info->pinned_extents);
1331 extent_io_tree_cleanup(&fs_info->pending_del);
1332 extent_io_tree_cleanup(&fs_info->extent_ins);
1334 free(fs_info->tree_root);
1335 free(fs_info->extent_root);
1336 free(fs_info->chunk_root);
1337 free(fs_info->dev_root);
1338 free(fs_info->csum_root);
1344 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1345 struct extent_buffer *eb)
1347 return clear_extent_buffer_dirty(eb);
1350 int wait_on_tree_block_writeback(struct btrfs_root *root,
1351 struct extent_buffer *eb)
1356 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1358 set_extent_buffer_dirty(eb);
1361 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1365 ret = extent_buffer_uptodate(buf);
1369 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1373 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1375 return set_extent_buffer_uptodate(eb);