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 %08X "
93 "wanted %08X\n", (unsigned long long)buf->start,
94 *((u32 *)result), *((u32*)(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);
204 device = multi->stripes[0].dev;
206 if (device->fd == 0) {
213 eb->dev_bytenr = multi->stripes[0].physical;
217 if (read_len > bytes_left)
218 read_len = bytes_left;
220 ret = read_extent_from_disk(eb, offset, read_len);
224 bytes_left -= read_len;
229 struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
230 u32 blocksize, u64 parent_transid)
233 struct extent_buffer *eb;
234 u64 best_transid = 0;
240 eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
244 if (btrfs_buffer_uptodate(eb, parent_transid))
248 ret = read_whole_eb(root->fs_info, eb, mirror_num);
249 if (ret == 0 && check_tree_block(root, eb) == 0 &&
250 csum_tree_block(root, eb, 1) == 0 &&
251 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
253 btrfs_set_buffer_uptodate(eb);
257 if (check_tree_block(root, eb))
258 printk("read block failed check_tree_block\n");
260 printk("Csum didn't match\n");
263 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
265 if (num_copies == 1) {
269 if (btrfs_header_generation(eb) > best_transid) {
270 best_transid = btrfs_header_generation(eb);
271 good_mirror = mirror_num;
274 if (mirror_num > num_copies) {
275 mirror_num = good_mirror;
280 free_extent_buffer(eb);
284 static int rmw_eb(struct btrfs_fs_info *info,
285 struct extent_buffer *eb, struct extent_buffer *orig_eb)
288 unsigned long orig_off = 0;
289 unsigned long dest_off = 0;
290 unsigned long copy_len = eb->len;
292 ret = read_whole_eb(info, eb, 0);
296 if (eb->start + eb->len <= orig_eb->start ||
297 eb->start >= orig_eb->start + orig_eb->len)
300 * | ----- orig_eb ------- |
301 * | ----- stripe ------- |
302 * | ----- orig_eb ------- |
303 * | ----- orig_eb ------- |
305 if (eb->start > orig_eb->start)
306 orig_off = eb->start - orig_eb->start;
307 if (orig_eb->start > eb->start)
308 dest_off = orig_eb->start - eb->start;
310 if (copy_len > orig_eb->len - orig_off)
311 copy_len = orig_eb->len - orig_off;
312 if (copy_len > eb->len - dest_off)
313 copy_len = eb->len - dest_off;
315 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
319 static void split_eb_for_raid56(struct btrfs_fs_info *info,
320 struct extent_buffer *orig_eb,
321 struct extent_buffer **ebs,
322 u64 stripe_len, u64 *raid_map,
325 struct extent_buffer *eb;
326 u64 start = orig_eb->start;
331 for (i = 0; i < num_stripes; i++) {
332 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
335 eb = malloc(sizeof(struct extent_buffer) + stripe_len);
338 memset(eb, 0, sizeof(struct extent_buffer) + stripe_len);
340 eb->start = raid_map[i];
341 eb->len = stripe_len;
345 eb->dev_bytenr = (u64)-1;
347 this_eb_start = raid_map[i];
349 if (start > this_eb_start ||
350 start + orig_eb->len < this_eb_start + stripe_len) {
351 ret = rmw_eb(info, eb, orig_eb);
354 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
360 static int write_raid56_with_parity(struct btrfs_fs_info *info,
361 struct extent_buffer *eb,
362 struct btrfs_multi_bio *multi,
363 u64 stripe_len, u64 *raid_map)
365 struct extent_buffer *ebs[multi->num_stripes], *p_eb = NULL, *q_eb = NULL;
369 int alloc_size = eb->len;
371 if (stripe_len > alloc_size)
372 alloc_size = stripe_len;
374 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
377 for (i = 0; i < multi->num_stripes; i++) {
378 struct extent_buffer *new_eb;
379 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
380 ebs[i]->dev_bytenr = multi->stripes[i].physical;
381 ebs[i]->fd = multi->stripes[i].dev->fd;
382 multi->stripes[i].dev->total_ios++;
383 BUG_ON(ebs[i]->start != raid_map[i]);
386 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
388 new_eb->dev_bytenr = multi->stripes[i].physical;
389 new_eb->fd = multi->stripes[i].dev->fd;
390 multi->stripes[i].dev->total_ios++;
391 new_eb->len = stripe_len;
393 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
395 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
399 void *pointers[multi->num_stripes];
400 ebs[multi->num_stripes - 2] = p_eb;
401 ebs[multi->num_stripes - 1] = q_eb;
403 for (i = 0; i < multi->num_stripes; i++)
404 pointers[i] = ebs[i]->data;
406 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
408 ebs[multi->num_stripes - 1] = p_eb;
409 memcpy(p_eb->data, ebs[0]->data, stripe_len);
410 for (j = 1; j < multi->num_stripes - 1; j++) {
411 for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
412 *(unsigned long *)(p_eb->data + i) ^=
413 *(unsigned long *)(ebs[j]->data + i);
418 for (i = 0; i < multi->num_stripes; i++) {
419 ret = write_extent_to_disk(ebs[i]);
427 int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
428 struct extent_buffer *eb)
433 u64 *raid_map = NULL;
434 struct btrfs_multi_bio *multi = NULL;
436 if (check_tree_block(root, eb))
438 if (!btrfs_buffer_uptodate(eb, trans->transid))
441 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
442 csum_tree_block(root, eb, 0);
446 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
447 eb->start, &length, &multi, 0, &raid_map);
450 ret = write_raid56_with_parity(root->fs_info, eb, multi,
453 } else while (dev_nr < multi->num_stripes) {
455 eb->fd = multi->stripes[dev_nr].dev->fd;
456 eb->dev_bytenr = multi->stripes[dev_nr].physical;
457 multi->stripes[dev_nr].dev->total_ios++;
459 ret = write_extent_to_disk(eb);
466 int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
467 u32 stripesize, struct btrfs_root *root,
468 struct btrfs_fs_info *fs_info, u64 objectid)
471 root->commit_root = NULL;
472 root->sectorsize = sectorsize;
473 root->nodesize = nodesize;
474 root->leafsize = leafsize;
475 root->stripesize = stripesize;
477 root->track_dirty = 0;
479 root->fs_info = fs_info;
480 root->objectid = objectid;
481 root->last_trans = 0;
482 root->highest_inode = 0;
483 root->last_inode_alloc = 0;
485 INIT_LIST_HEAD(&root->dirty_list);
486 memset(&root->root_key, 0, sizeof(root->root_key));
487 memset(&root->root_item, 0, sizeof(root->root_item));
488 root->root_key.objectid = objectid;
492 static int update_cowonly_root(struct btrfs_trans_handle *trans,
493 struct btrfs_root *root)
497 struct btrfs_root *tree_root = root->fs_info->tree_root;
499 btrfs_write_dirty_block_groups(trans, root);
501 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
502 if (old_root_bytenr == root->node->start)
504 btrfs_set_root_bytenr(&root->root_item,
506 btrfs_set_root_generation(&root->root_item,
508 root->root_item.level = btrfs_header_level(root->node);
509 ret = btrfs_update_root(trans, tree_root,
513 btrfs_write_dirty_block_groups(trans, root);
518 static int commit_tree_roots(struct btrfs_trans_handle *trans,
519 struct btrfs_fs_info *fs_info)
521 struct btrfs_root *root;
522 struct list_head *next;
523 struct extent_buffer *eb;
526 if (fs_info->readonly)
529 eb = fs_info->tree_root->node;
530 extent_buffer_get(eb);
531 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
532 free_extent_buffer(eb);
536 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
537 next = fs_info->dirty_cowonly_roots.next;
539 root = list_entry(next, struct btrfs_root, dirty_list);
540 update_cowonly_root(trans, root);
541 free_extent_buffer(root->commit_root);
542 root->commit_root = NULL;
548 static int __commit_transaction(struct btrfs_trans_handle *trans,
549 struct btrfs_root *root)
553 struct extent_buffer *eb;
554 struct extent_io_tree *tree = &root->fs_info->extent_cache;
558 ret = find_first_extent_bit(tree, 0, &start, &end,
562 while(start <= end) {
563 eb = find_first_extent_buffer(tree, start);
564 BUG_ON(!eb || eb->start != start);
565 ret = write_tree_block(trans, root, eb);
568 clear_extent_buffer_dirty(eb);
569 free_extent_buffer(eb);
575 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
576 struct btrfs_root *root)
578 u64 transid = trans->transid;
580 struct btrfs_fs_info *fs_info = root->fs_info;
582 if (root->commit_root == root->node)
585 free_extent_buffer(root->commit_root);
586 root->commit_root = NULL;
588 btrfs_set_root_bytenr(&root->root_item, root->node->start);
589 btrfs_set_root_generation(&root->root_item, trans->transid);
590 root->root_item.level = btrfs_header_level(root->node);
591 ret = btrfs_update_root(trans, root->fs_info->tree_root,
592 &root->root_key, &root->root_item);
595 ret = commit_tree_roots(trans, fs_info);
597 ret = __commit_transaction(trans, root);
599 write_ctree_super(trans, root);
600 btrfs_finish_extent_commit(trans, fs_info->extent_root,
601 &fs_info->pinned_extents);
602 btrfs_free_transaction(root, trans);
603 free_extent_buffer(root->commit_root);
604 root->commit_root = NULL;
605 fs_info->running_transaction = NULL;
606 fs_info->last_trans_committed = transid;
610 static int find_and_setup_root(struct btrfs_root *tree_root,
611 struct btrfs_fs_info *fs_info,
612 u64 objectid, struct btrfs_root *root)
618 __setup_root(tree_root->nodesize, tree_root->leafsize,
619 tree_root->sectorsize, tree_root->stripesize,
620 root, fs_info, objectid);
621 ret = btrfs_find_last_root(tree_root, objectid,
622 &root->root_item, &root->root_key);
626 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
627 generation = btrfs_root_generation(&root->root_item);
628 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
629 blocksize, generation);
630 if (!extent_buffer_uptodate(root->node))
636 static int find_and_setup_log_root(struct btrfs_root *tree_root,
637 struct btrfs_fs_info *fs_info,
638 struct btrfs_super_block *disk_super)
641 u64 blocknr = btrfs_super_log_root(disk_super);
642 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
652 blocksize = btrfs_level_size(tree_root,
653 btrfs_super_log_root_level(disk_super));
655 __setup_root(tree_root->nodesize, tree_root->leafsize,
656 tree_root->sectorsize, tree_root->stripesize,
657 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
659 log_root->node = read_tree_block(tree_root, blocknr,
661 btrfs_super_generation(disk_super) + 1);
663 fs_info->log_root_tree = log_root;
665 if (!extent_buffer_uptodate(log_root->node)) {
666 free_extent_buffer(log_root->node);
668 fs_info->log_root_tree = NULL;
676 int btrfs_free_fs_root(struct btrfs_fs_info *fs_info,
677 struct btrfs_root *root)
680 free_extent_buffer(root->node);
681 if (root->commit_root)
682 free_extent_buffer(root->commit_root);
687 static int free_fs_roots(struct btrfs_fs_info *fs_info)
689 struct cache_extent *cache;
690 struct btrfs_root *root;
693 cache = find_first_cache_extent(&fs_info->fs_root_cache, 0);
696 root = container_of(cache, struct btrfs_root, cache);
697 remove_cache_extent(&fs_info->fs_root_cache, cache);
698 btrfs_free_fs_root(fs_info, root);
703 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
704 struct btrfs_key *location)
706 struct btrfs_root *root;
707 struct btrfs_root *tree_root = fs_info->tree_root;
708 struct btrfs_path *path;
709 struct extent_buffer *l;
714 root = malloc(sizeof(*root));
716 return ERR_PTR(-ENOMEM);
717 memset(root, 0, sizeof(*root));
718 if (location->offset == (u64)-1) {
719 ret = find_and_setup_root(tree_root, fs_info,
720 location->objectid, root);
728 __setup_root(tree_root->nodesize, tree_root->leafsize,
729 tree_root->sectorsize, tree_root->stripesize,
730 root, fs_info, location->objectid);
732 path = btrfs_alloc_path();
734 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
741 read_extent_buffer(l, &root->root_item,
742 btrfs_item_ptr_offset(l, path->slots[0]),
743 sizeof(root->root_item));
744 memcpy(&root->root_key, location, sizeof(*location));
747 btrfs_release_path(root, path);
748 btrfs_free_path(path);
753 generation = btrfs_root_generation(&root->root_item);
754 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
755 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
756 blocksize, generation);
763 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
764 struct btrfs_key *location)
766 struct btrfs_root *root;
767 struct cache_extent *cache;
770 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
771 return fs_info->tree_root;
772 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
773 return fs_info->extent_root;
774 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
775 return fs_info->chunk_root;
776 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
777 return fs_info->dev_root;
778 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
779 return fs_info->csum_root;
781 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
782 location->offset != (u64)-1);
784 cache = find_cache_extent(&fs_info->fs_root_cache,
785 location->objectid, 1);
787 return container_of(cache, struct btrfs_root, cache);
789 root = btrfs_read_fs_root_no_cache(fs_info, location);
793 root->cache.start = location->objectid;
794 root->cache.size = 1;
795 ret = insert_existing_cache_extent(&fs_info->fs_root_cache,
801 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
803 u64 root_tree_bytenr, int writes,
812 struct btrfs_key key;
813 struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
814 struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
815 struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root));
816 struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root));
817 struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root));
818 struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
820 struct btrfs_super_block *disk_super;
821 struct btrfs_fs_devices *fs_devices = NULL;
826 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
828 /* try to drop all the caches */
829 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
830 fprintf(stderr, "Warning, could not drop caches\n");
832 ret = btrfs_scan_one_device(fp, path, &fs_devices,
833 &total_devs, sb_bytenr);
836 fprintf(stderr, "No valid Btrfs found on %s\n", path);
840 if (total_devs != 1) {
841 ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1);
846 memset(fs_info, 0, sizeof(*fs_info));
847 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
848 fs_info->tree_root = tree_root;
849 fs_info->extent_root = extent_root;
850 fs_info->chunk_root = chunk_root;
851 fs_info->dev_root = dev_root;
852 fs_info->csum_root = csum_root;
855 fs_info->readonly = 1;
857 extent_io_tree_init(&fs_info->extent_cache);
858 extent_io_tree_init(&fs_info->free_space_cache);
859 extent_io_tree_init(&fs_info->block_group_cache);
860 extent_io_tree_init(&fs_info->pinned_extents);
861 extent_io_tree_init(&fs_info->pending_del);
862 extent_io_tree_init(&fs_info->extent_ins);
863 cache_tree_init(&fs_info->fs_root_cache);
865 cache_tree_init(&fs_info->mapping_tree.cache_tree);
867 mutex_init(&fs_info->fs_mutex);
868 fs_info->fs_devices = fs_devices;
869 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
870 INIT_LIST_HEAD(&fs_info->space_info);
872 __setup_root(4096, 4096, 4096, 4096, tree_root,
873 fs_info, BTRFS_ROOT_TREE_OBJECTID);
876 ret = btrfs_open_devices(fs_devices, O_RDWR);
878 ret = btrfs_open_devices(fs_devices, O_RDONLY);
882 fs_info->super_bytenr = sb_bytenr;
883 disk_super = fs_info->super_copy;
884 ret = btrfs_read_dev_super(fs_devices->latest_bdev,
885 disk_super, sb_bytenr);
887 printk("No valid btrfs found\n");
891 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
894 features = btrfs_super_incompat_flags(disk_super) &
895 ~BTRFS_FEATURE_INCOMPAT_SUPP;
897 printk("couldn't open because of unsupported "
898 "option features (%Lx).\n",
899 (unsigned long long)features);
903 features = btrfs_super_incompat_flags(disk_super);
904 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
905 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
906 btrfs_set_super_incompat_flags(disk_super, features);
909 features = btrfs_super_compat_ro_flags(disk_super) &
910 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
911 if (writes && features) {
912 printk("couldn't open RDWR because of unsupported "
913 "option features (%Lx).\n",
914 (unsigned long long)features);
918 nodesize = btrfs_super_nodesize(disk_super);
919 leafsize = btrfs_super_leafsize(disk_super);
920 sectorsize = btrfs_super_sectorsize(disk_super);
921 stripesize = btrfs_super_stripesize(disk_super);
922 tree_root->nodesize = nodesize;
923 tree_root->leafsize = leafsize;
924 tree_root->sectorsize = sectorsize;
925 tree_root->stripesize = stripesize;
927 ret = btrfs_read_sys_array(tree_root);
930 blocksize = btrfs_level_size(tree_root,
931 btrfs_super_chunk_root_level(disk_super));
932 generation = btrfs_super_chunk_root_generation(disk_super);
934 __setup_root(nodesize, leafsize, sectorsize, stripesize,
935 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
937 chunk_root->node = read_tree_block(chunk_root,
938 btrfs_super_chunk_root(disk_super),
939 blocksize, generation);
940 if (!extent_buffer_uptodate(chunk_root->node)) {
941 printk("Couldn't read chunk root\n");
945 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
946 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
949 if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) {
950 ret = btrfs_read_chunk_tree(chunk_root);
952 printk("Couldn't read chunk tree\n");
957 blocksize = btrfs_level_size(tree_root,
958 btrfs_super_root_level(disk_super));
959 generation = btrfs_super_generation(disk_super);
961 if (!root_tree_bytenr)
962 root_tree_bytenr = btrfs_super_root(disk_super);
963 tree_root->node = read_tree_block(tree_root,
965 blocksize, generation);
966 if (!extent_buffer_uptodate(tree_root->node)) {
967 printk("Couldn't read tree root\n");
970 ret = find_and_setup_root(tree_root, fs_info,
971 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
973 printk("Couldn't setup extent tree\n");
976 extent_root->track_dirty = 1;
978 ret = find_and_setup_root(tree_root, fs_info,
979 BTRFS_DEV_TREE_OBJECTID, dev_root);
981 printk("Couldn't setup device tree\n");
984 dev_root->track_dirty = 1;
986 ret = find_and_setup_root(tree_root, fs_info,
987 BTRFS_CSUM_TREE_OBJECTID, csum_root);
989 printk("Couldn't setup csum tree\n");
993 csum_root->track_dirty = 1;
995 find_and_setup_log_root(tree_root, fs_info, disk_super);
997 fs_info->generation = generation;
998 fs_info->last_trans_committed = generation;
999 btrfs_read_block_groups(fs_info->tree_root);
1001 key.objectid = BTRFS_FS_TREE_OBJECTID;
1002 key.type = BTRFS_ROOT_ITEM_KEY;
1003 key.offset = (u64)-1;
1004 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
1006 if (!fs_info->fs_root)
1009 fs_info->data_alloc_profile = (u64)-1;
1010 fs_info->metadata_alloc_profile = (u64)-1;
1011 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1019 if (fs_info->csum_root)
1020 free_extent_buffer(fs_info->csum_root->node);
1021 if (fs_info->dev_root)
1022 free_extent_buffer(fs_info->dev_root->node);
1023 if (fs_info->extent_root)
1024 free_extent_buffer(fs_info->extent_root->node);
1025 if (fs_info->tree_root)
1026 free_extent_buffer(fs_info->tree_root->node);
1028 if (fs_info->chunk_root)
1029 free_extent_buffer(fs_info->chunk_root->node);
1031 close_all_devices(fs_info);
1033 extent_io_tree_cleanup(&fs_info->extent_cache);
1034 extent_io_tree_cleanup(&fs_info->free_space_cache);
1035 extent_io_tree_cleanup(&fs_info->block_group_cache);
1036 extent_io_tree_cleanup(&fs_info->pinned_extents);
1037 extent_io_tree_cleanup(&fs_info->pending_del);
1038 extent_io_tree_cleanup(&fs_info->extent_ins);
1049 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1050 u64 sb_bytenr, u64 root_tree_bytenr,
1051 int writes, int partial)
1054 struct btrfs_fs_info *info;
1055 int flags = O_CREAT | O_RDWR;
1060 fp = open(filename, flags, 0600);
1062 fprintf (stderr, "Could not open %s\n", filename);
1065 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1071 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes)
1073 struct btrfs_fs_info *info;
1075 info = open_ctree_fs_info(filename, sb_bytenr, 0, writes, 0);
1078 return info->fs_root;
1081 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1084 struct btrfs_fs_info *info;
1085 info = __open_ctree_fd(fp, path, sb_bytenr, 0, writes, 0);
1088 return info->fs_root;
1091 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr)
1093 u8 fsid[BTRFS_FSID_SIZE];
1094 int fsid_is_initialized = 0;
1095 struct btrfs_super_block buf;
1101 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1102 ret = pread64(fd, &buf, sizeof(buf), sb_bytenr);
1103 if (ret < sizeof(buf))
1106 if (btrfs_super_bytenr(&buf) != sb_bytenr ||
1107 buf.magic != cpu_to_le64(BTRFS_MAGIC))
1110 memcpy(sb, &buf, sizeof(*sb));
1114 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1115 bytenr = btrfs_sb_offset(i);
1116 ret = pread64(fd, &buf, sizeof(buf), bytenr);
1117 if (ret < sizeof(buf))
1120 if (btrfs_super_bytenr(&buf) != bytenr )
1122 /* if magic is NULL, the device was removed */
1123 if (buf.magic == 0 && i == 0)
1125 if (buf.magic != cpu_to_le64(BTRFS_MAGIC))
1128 if (!fsid_is_initialized) {
1129 memcpy(fsid, buf.fsid, sizeof(fsid));
1130 fsid_is_initialized = 1;
1131 } else if (memcmp(fsid, buf.fsid, sizeof(fsid))) {
1133 * the superblocks (the original one and
1134 * its backups) contain data of different
1135 * filesystems -> the super cannot be trusted
1140 if (btrfs_super_generation(&buf) > transid) {
1141 memcpy(sb, &buf, sizeof(*sb));
1142 transid = btrfs_super_generation(&buf);
1146 return transid > 0 ? 0 : -1;
1149 int write_dev_supers(struct btrfs_root *root, struct btrfs_super_block *sb,
1150 struct btrfs_device *device)
1156 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1157 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1159 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1160 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1161 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1164 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1165 * zero filled, we can use it directly
1167 ret = pwrite64(device->fd, root->fs_info->super_copy,
1168 BTRFS_SUPER_INFO_SIZE,
1169 root->fs_info->super_bytenr);
1170 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1174 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1175 bytenr = btrfs_sb_offset(i);
1176 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1179 btrfs_set_super_bytenr(sb, bytenr);
1182 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1183 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1184 btrfs_csum_final(crc, (char *)&sb->csum[0]);
1187 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1188 * zero filled, we can use it directly
1190 ret = pwrite64(device->fd, root->fs_info->super_copy,
1191 BTRFS_SUPER_INFO_SIZE, bytenr);
1192 BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
1198 int write_all_supers(struct btrfs_root *root)
1200 struct list_head *cur;
1201 struct list_head *head = &root->fs_info->fs_devices->devices;
1202 struct btrfs_device *dev;
1203 struct btrfs_super_block *sb;
1204 struct btrfs_dev_item *dev_item;
1208 sb = root->fs_info->super_copy;
1209 dev_item = &sb->dev_item;
1210 list_for_each(cur, head) {
1211 dev = list_entry(cur, struct btrfs_device, dev_list);
1212 if (!dev->writeable)
1215 btrfs_set_stack_device_generation(dev_item, 0);
1216 btrfs_set_stack_device_type(dev_item, dev->type);
1217 btrfs_set_stack_device_id(dev_item, dev->devid);
1218 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1219 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1220 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1221 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1222 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1223 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1224 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1226 flags = btrfs_super_flags(sb);
1227 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1229 ret = write_dev_supers(root, sb, dev);
1235 int write_ctree_super(struct btrfs_trans_handle *trans,
1236 struct btrfs_root *root)
1239 struct btrfs_root *tree_root = root->fs_info->tree_root;
1240 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1242 if (root->fs_info->readonly)
1245 btrfs_set_super_generation(root->fs_info->super_copy,
1247 btrfs_set_super_root(root->fs_info->super_copy,
1248 tree_root->node->start);
1249 btrfs_set_super_root_level(root->fs_info->super_copy,
1250 btrfs_header_level(tree_root->node));
1251 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1252 chunk_root->node->start);
1253 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1254 btrfs_header_level(chunk_root->node));
1255 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1256 btrfs_header_generation(chunk_root->node));
1258 ret = write_all_supers(root);
1260 fprintf(stderr, "failed to write new super block err %d\n", ret);
1264 static int close_all_devices(struct btrfs_fs_info *fs_info)
1266 struct list_head *list;
1267 struct btrfs_device *device;
1269 list = &fs_info->fs_devices->devices;
1270 while (!list_empty(list)) {
1271 device = list_entry(list->next, struct btrfs_device, dev_list);
1272 list_del_init(&device->dev_list);
1273 if (device->fd != -1) {
1275 if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED))
1276 fprintf(stderr, "Warning, could not drop caches\n");
1280 kfree(device->name);
1281 kfree(device->label);
1284 kfree(fs_info->fs_devices);
1288 static void free_mapping_cache(struct btrfs_fs_info *fs_info)
1290 struct cache_tree *cache_tree = &fs_info->mapping_tree.cache_tree;
1291 struct cache_extent *ce;
1292 struct map_lookup *map;
1294 while ((ce = find_first_cache_extent(cache_tree, 0))) {
1295 map = container_of(ce, struct map_lookup, ce);
1296 remove_cache_extent(cache_tree, ce);
1301 int close_ctree(struct btrfs_root *root)
1304 struct btrfs_trans_handle *trans;
1305 struct btrfs_fs_info *fs_info = root->fs_info;
1307 if (fs_info->last_trans_committed !=
1308 fs_info->generation) {
1309 trans = btrfs_start_transaction(root, 1);
1310 btrfs_commit_transaction(trans, root);
1311 trans = btrfs_start_transaction(root, 1);
1312 ret = commit_tree_roots(trans, fs_info);
1314 ret = __commit_transaction(trans, root);
1316 write_ctree_super(trans, root);
1317 btrfs_free_transaction(root, trans);
1319 btrfs_free_block_groups(fs_info);
1321 free_fs_roots(fs_info);
1323 if (fs_info->extent_root->node)
1324 free_extent_buffer(fs_info->extent_root->node);
1325 if (fs_info->tree_root->node)
1326 free_extent_buffer(fs_info->tree_root->node);
1327 if (fs_info->chunk_root->node)
1328 free_extent_buffer(fs_info->chunk_root->node);
1329 if (fs_info->dev_root->node)
1330 free_extent_buffer(fs_info->dev_root->node);
1331 if (fs_info->csum_root->node)
1332 free_extent_buffer(fs_info->csum_root->node);
1334 if (fs_info->log_root_tree) {
1335 if (fs_info->log_root_tree->node)
1336 free_extent_buffer(fs_info->log_root_tree->node);
1337 free(fs_info->log_root_tree);
1340 close_all_devices(fs_info);
1341 free_mapping_cache(fs_info);
1342 extent_io_tree_cleanup(&fs_info->extent_cache);
1343 extent_io_tree_cleanup(&fs_info->free_space_cache);
1344 extent_io_tree_cleanup(&fs_info->block_group_cache);
1345 extent_io_tree_cleanup(&fs_info->pinned_extents);
1346 extent_io_tree_cleanup(&fs_info->pending_del);
1347 extent_io_tree_cleanup(&fs_info->extent_ins);
1349 free(fs_info->tree_root);
1350 free(fs_info->extent_root);
1351 free(fs_info->chunk_root);
1352 free(fs_info->dev_root);
1353 free(fs_info->csum_root);
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);