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.
21 #include <sys/types.h>
25 #include <uuid/uuid.h>
26 #include "kerncompat.h"
27 #include "radix-tree.h"
31 #include "transaction.h"
34 #include "print-tree.h"
35 #include "rbtree-utils.h"
37 /* specified errno for check_tree_block */
38 #define BTRFS_BAD_BYTENR (-1)
39 #define BTRFS_BAD_FSID (-2)
40 #define BTRFS_BAD_LEVEL (-3)
41 #define BTRFS_BAD_NRITEMS (-4)
43 /* Calculate max possible nritems for a leaf/node */
44 static u32 max_nritems(u8 level, u32 nodesize)
48 return ((nodesize - sizeof(struct btrfs_header)) /
49 sizeof(struct btrfs_item));
50 return ((nodesize - sizeof(struct btrfs_header)) /
51 sizeof(struct btrfs_key_ptr));
54 static int check_tree_block(struct btrfs_fs_info *fs_info,
55 struct extent_buffer *buf)
58 struct btrfs_fs_devices *fs_devices;
59 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
60 int ret = BTRFS_BAD_FSID;
62 if (buf->start != btrfs_header_bytenr(buf))
63 return BTRFS_BAD_BYTENR;
64 if (btrfs_header_level(buf) >= BTRFS_MAX_LEVEL)
65 return BTRFS_BAD_LEVEL;
66 if (btrfs_header_nritems(buf) > max_nritems(btrfs_header_level(buf),
68 return BTRFS_BAD_NRITEMS;
70 /* Only leaf can be empty */
71 if (btrfs_header_nritems(buf) == 0 &&
72 btrfs_header_level(buf) != 0)
73 return BTRFS_BAD_NRITEMS;
75 fs_devices = fs_info->fs_devices;
77 if (fs_info->ignore_fsid_mismatch ||
78 !memcmp_extent_buffer(buf, fs_devices->fsid,
84 fs_devices = fs_devices->seed;
89 static void print_tree_block_error(struct btrfs_fs_info *fs_info,
90 struct extent_buffer *eb,
93 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
94 char found_uuid[BTRFS_UUID_UNPARSED_SIZE] = {'\0'};
95 u8 buf[BTRFS_UUID_SIZE];
99 read_extent_buffer(eb, buf, btrfs_header_fsid(),
101 uuid_unparse(buf, found_uuid);
102 uuid_unparse(fs_info->fsid, fs_uuid);
103 fprintf(stderr, "fsid mismatch, want=%s, have=%s\n",
104 fs_uuid, found_uuid);
106 case BTRFS_BAD_BYTENR:
107 fprintf(stderr, "bytenr mismatch, want=%llu, have=%llu\n",
108 eb->start, btrfs_header_bytenr(eb));
110 case BTRFS_BAD_LEVEL:
111 fprintf(stderr, "bad level, %u > %u\n",
112 btrfs_header_level(eb), BTRFS_MAX_LEVEL);
114 case BTRFS_BAD_NRITEMS:
115 fprintf(stderr, "invalid nr_items: %u\n",
116 btrfs_header_nritems(eb));
121 u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
123 return crc32c(seed, data, len);
126 void btrfs_csum_final(u32 crc, u8 *result)
128 put_unaligned_le32(~crc, result);
131 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
132 int verify, int silent)
134 u8 result[BTRFS_CSUM_SIZE];
138 len = buf->len - BTRFS_CSUM_SIZE;
139 crc = crc32c(crc, buf->data + BTRFS_CSUM_SIZE, len);
140 btrfs_csum_final(crc, result);
143 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
145 printk("checksum verify failed on %llu found %08X wanted %08X\n",
146 (unsigned long long)buf->start,
148 *((u32*)(char *)buf->data));
152 write_extent_buffer(buf, result, 0, csum_size);
157 int csum_tree_block_size(struct extent_buffer *buf, u16 csum_size, int verify)
159 return __csum_tree_block_size(buf, csum_size, verify, 0);
162 int verify_tree_block_csum_silent(struct extent_buffer *buf, u16 csum_size)
164 return __csum_tree_block_size(buf, csum_size, 1, 1);
167 static int csum_tree_block_fs_info(struct btrfs_fs_info *fs_info,
168 struct extent_buffer *buf, int verify)
171 btrfs_super_csum_size(fs_info->super_copy);
172 if (verify && fs_info->suppress_check_block_errors)
173 return verify_tree_block_csum_silent(buf, csum_size);
174 return csum_tree_block_size(buf, csum_size, verify);
177 int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
180 return csum_tree_block_fs_info(root->fs_info, buf, verify);
183 struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
184 u64 bytenr, u32 blocksize)
186 return find_extent_buffer(&root->fs_info->extent_cache,
190 struct extent_buffer* btrfs_find_create_tree_block(
191 struct btrfs_fs_info *fs_info, u64 bytenr, u32 blocksize)
193 return alloc_extent_buffer(&fs_info->extent_cache, bytenr, blocksize);
196 void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
199 struct extent_buffer *eb;
201 struct btrfs_multi_bio *multi = NULL;
202 struct btrfs_device *device;
204 eb = btrfs_find_tree_block(root, bytenr, blocksize);
205 if (!(eb && btrfs_buffer_uptodate(eb, parent_transid)) &&
206 !btrfs_map_block(&root->fs_info->mapping_tree, READ,
207 bytenr, &length, &multi, 0, NULL)) {
208 device = multi->stripes[0].dev;
210 blocksize = min(blocksize, (u32)(64 * 1024));
211 readahead(device->fd, multi->stripes[0].physical, blocksize);
214 free_extent_buffer(eb);
218 static int verify_parent_transid(struct extent_io_tree *io_tree,
219 struct extent_buffer *eb, u64 parent_transid,
224 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
227 if (extent_buffer_uptodate(eb) &&
228 btrfs_header_generation(eb) == parent_transid) {
232 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
233 (unsigned long long)eb->start,
234 (unsigned long long)parent_transid,
235 (unsigned long long)btrfs_header_generation(eb));
237 eb->flags |= EXTENT_BAD_TRANSID;
238 printk("Ignoring transid failure\n");
244 clear_extent_buffer_uptodate(eb);
250 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror)
252 unsigned long offset = 0;
253 struct btrfs_multi_bio *multi = NULL;
254 struct btrfs_device *device;
257 unsigned long bytes_left = eb->len;
260 read_len = bytes_left;
263 if (!info->on_restoring &&
264 eb->start != BTRFS_SUPER_INFO_OFFSET) {
265 ret = btrfs_map_block(&info->mapping_tree, READ,
266 eb->start + offset, &read_len, &multi,
269 printk("Couldn't map the block %Lu\n", eb->start + offset);
273 device = multi->stripes[0].dev;
275 if (device->fd <= 0) {
282 eb->dev_bytenr = multi->stripes[0].physical;
286 /* special case for restore metadump */
287 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
288 if (device->devid == 1)
293 eb->dev_bytenr = eb->start;
297 if (read_len > bytes_left)
298 read_len = bytes_left;
300 ret = read_extent_from_disk(eb, offset, read_len);
304 bytes_left -= read_len;
309 struct extent_buffer* read_tree_block_fs_info(
310 struct btrfs_fs_info *fs_info, u64 bytenr, u32 blocksize,
314 struct extent_buffer *eb;
315 u64 best_transid = 0;
316 u32 sectorsize = btrfs_super_sectorsize(fs_info->super_copy);
317 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
324 * Don't even try to create tree block for unaligned tree block
326 * Such unaligned tree block will free overlapping extent buffer,
327 * causing use-after-free bugs for fuzzed images.
329 if (bytenr < sectorsize || !IS_ALIGNED(bytenr, sectorsize)) {
330 error("tree block bytenr %llu is not aligned to sectorsize %u",
332 return ERR_PTR(-EIO);
334 if (blocksize < nodesize || !IS_ALIGNED(blocksize, nodesize)) {
335 error("tree block size %u is not aligned to nodesize %u",
336 blocksize, nodesize);
337 return ERR_PTR(-EIO);
340 eb = btrfs_find_create_tree_block(fs_info, bytenr, blocksize);
342 return ERR_PTR(-ENOMEM);
344 if (btrfs_buffer_uptodate(eb, parent_transid))
348 ret = read_whole_eb(fs_info, eb, mirror_num);
349 if (ret == 0 && csum_tree_block_fs_info(fs_info, eb, 1) == 0 &&
350 check_tree_block(fs_info, eb) == 0 &&
351 verify_parent_transid(eb->tree, eb, parent_transid, ignore)
353 if (eb->flags & EXTENT_BAD_TRANSID &&
354 list_empty(&eb->recow)) {
355 list_add_tail(&eb->recow,
356 &fs_info->recow_ebs);
359 btrfs_set_buffer_uptodate(eb);
363 if (check_tree_block(fs_info, eb)) {
364 if (!fs_info->suppress_check_block_errors)
365 print_tree_block_error(fs_info, eb,
366 check_tree_block(fs_info, eb));
368 if (!fs_info->suppress_check_block_errors)
369 fprintf(stderr, "Csum didn't match\n");
374 num_copies = btrfs_num_copies(&fs_info->mapping_tree,
376 if (num_copies == 1) {
380 if (btrfs_header_generation(eb) > best_transid && mirror_num) {
381 best_transid = btrfs_header_generation(eb);
382 good_mirror = mirror_num;
385 if (mirror_num > num_copies) {
386 mirror_num = good_mirror;
391 free_extent_buffer(eb);
395 int read_extent_data(struct btrfs_root *root, char *data,
396 u64 logical, u64 *len, int mirror)
399 struct btrfs_multi_bio *multi = NULL;
400 struct btrfs_fs_info *info = root->fs_info;
401 struct btrfs_device *device;
405 ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
406 &multi, mirror, NULL);
408 fprintf(stderr, "Couldn't map the block %llu\n",
412 device = multi->stripes[0].dev;
419 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
429 int write_and_map_eb(struct btrfs_trans_handle *trans,
430 struct btrfs_root *root,
431 struct extent_buffer *eb)
436 u64 *raid_map = NULL;
437 struct btrfs_multi_bio *multi = NULL;
441 ret = btrfs_map_block(&root->fs_info->mapping_tree, WRITE,
442 eb->start, &length, &multi, 0, &raid_map);
445 ret = write_raid56_with_parity(root->fs_info, eb, multi,
448 } else while (dev_nr < multi->num_stripes) {
450 eb->fd = multi->stripes[dev_nr].dev->fd;
451 eb->dev_bytenr = multi->stripes[dev_nr].physical;
452 multi->stripes[dev_nr].dev->total_ios++;
454 ret = write_extent_to_disk(eb);
462 int write_tree_block(struct btrfs_trans_handle *trans,
463 struct btrfs_root *root,
464 struct extent_buffer *eb)
466 if (check_tree_block(root->fs_info, eb)) {
467 print_tree_block_error(root->fs_info, eb,
468 check_tree_block(root->fs_info, eb));
472 if (trans && !btrfs_buffer_uptodate(eb, trans->transid))
475 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
476 csum_tree_block(root, eb, 0);
478 return write_and_map_eb(trans, root, eb);
481 void btrfs_setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
482 u32 stripesize, struct btrfs_root *root,
483 struct btrfs_fs_info *fs_info, u64 objectid)
486 root->commit_root = NULL;
487 root->sectorsize = sectorsize;
488 root->nodesize = nodesize;
489 root->leafsize = leafsize;
490 root->stripesize = stripesize;
492 root->track_dirty = 0;
494 root->fs_info = fs_info;
495 root->objectid = objectid;
496 root->last_trans = 0;
497 root->highest_inode = 0;
498 root->last_inode_alloc = 0;
500 INIT_LIST_HEAD(&root->dirty_list);
501 INIT_LIST_HEAD(&root->orphan_data_extents);
502 memset(&root->root_key, 0, sizeof(root->root_key));
503 memset(&root->root_item, 0, sizeof(root->root_item));
504 root->root_key.objectid = objectid;
507 static int update_cowonly_root(struct btrfs_trans_handle *trans,
508 struct btrfs_root *root)
512 struct btrfs_root *tree_root = root->fs_info->tree_root;
514 btrfs_write_dirty_block_groups(trans, root);
516 old_root_bytenr = btrfs_root_bytenr(&root->root_item);
517 if (old_root_bytenr == root->node->start)
519 btrfs_set_root_bytenr(&root->root_item,
521 btrfs_set_root_generation(&root->root_item,
523 root->root_item.level = btrfs_header_level(root->node);
524 ret = btrfs_update_root(trans, tree_root,
528 btrfs_write_dirty_block_groups(trans, root);
533 static int commit_tree_roots(struct btrfs_trans_handle *trans,
534 struct btrfs_fs_info *fs_info)
536 struct btrfs_root *root;
537 struct list_head *next;
538 struct extent_buffer *eb;
541 if (fs_info->readonly)
544 eb = fs_info->tree_root->node;
545 extent_buffer_get(eb);
546 ret = btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
547 free_extent_buffer(eb);
551 while(!list_empty(&fs_info->dirty_cowonly_roots)) {
552 next = fs_info->dirty_cowonly_roots.next;
554 root = list_entry(next, struct btrfs_root, dirty_list);
555 update_cowonly_root(trans, root);
556 free_extent_buffer(root->commit_root);
557 root->commit_root = NULL;
563 static int __commit_transaction(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root)
568 struct extent_buffer *eb;
569 struct extent_io_tree *tree = &root->fs_info->extent_cache;
573 ret = find_first_extent_bit(tree, 0, &start, &end,
577 while(start <= end) {
578 eb = find_first_extent_buffer(tree, start);
579 BUG_ON(!eb || eb->start != start);
580 ret = write_tree_block(trans, root, eb);
583 clear_extent_buffer_dirty(eb);
584 free_extent_buffer(eb);
590 int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
591 struct btrfs_root *root)
593 u64 transid = trans->transid;
595 struct btrfs_fs_info *fs_info = root->fs_info;
597 if (root->commit_root == root->node)
599 if (root == root->fs_info->tree_root)
601 if (root == root->fs_info->chunk_root)
604 free_extent_buffer(root->commit_root);
605 root->commit_root = NULL;
607 btrfs_set_root_bytenr(&root->root_item, root->node->start);
608 btrfs_set_root_generation(&root->root_item, trans->transid);
609 root->root_item.level = btrfs_header_level(root->node);
610 ret = btrfs_update_root(trans, root->fs_info->tree_root,
611 &root->root_key, &root->root_item);
614 ret = commit_tree_roots(trans, fs_info);
616 ret = __commit_transaction(trans, root);
618 write_ctree_super(trans, root);
619 btrfs_finish_extent_commit(trans, fs_info->extent_root,
620 &fs_info->pinned_extents);
621 btrfs_free_transaction(root, trans);
622 free_extent_buffer(root->commit_root);
623 root->commit_root = NULL;
624 fs_info->running_transaction = NULL;
625 fs_info->last_trans_committed = transid;
629 static int find_and_setup_root(struct btrfs_root *tree_root,
630 struct btrfs_fs_info *fs_info,
631 u64 objectid, struct btrfs_root *root)
637 btrfs_setup_root(tree_root->nodesize, tree_root->leafsize,
638 tree_root->sectorsize, tree_root->stripesize,
639 root, fs_info, objectid);
640 ret = btrfs_find_last_root(tree_root, objectid,
641 &root->root_item, &root->root_key);
645 blocksize = root->nodesize;
646 generation = btrfs_root_generation(&root->root_item);
647 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
648 blocksize, generation);
649 if (!extent_buffer_uptodate(root->node))
655 static int find_and_setup_log_root(struct btrfs_root *tree_root,
656 struct btrfs_fs_info *fs_info,
657 struct btrfs_super_block *disk_super)
660 u64 blocknr = btrfs_super_log_root(disk_super);
661 struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root));
671 blocksize = tree_root->nodesize;
673 btrfs_setup_root(tree_root->nodesize, tree_root->leafsize,
674 tree_root->sectorsize, tree_root->stripesize,
675 log_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
677 log_root->node = read_tree_block(tree_root, blocknr,
679 btrfs_super_generation(disk_super) + 1);
681 fs_info->log_root_tree = log_root;
683 if (!extent_buffer_uptodate(log_root->node)) {
684 free_extent_buffer(log_root->node);
686 fs_info->log_root_tree = NULL;
693 int btrfs_free_fs_root(struct btrfs_root *root)
696 free_extent_buffer(root->node);
697 if (root->commit_root)
698 free_extent_buffer(root->commit_root);
703 static void __free_fs_root(struct rb_node *node)
705 struct btrfs_root *root;
707 root = container_of(node, struct btrfs_root, rb_node);
708 btrfs_free_fs_root(root);
711 FREE_RB_BASED_TREE(fs_roots, __free_fs_root);
713 struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
714 struct btrfs_key *location)
716 struct btrfs_root *root;
717 struct btrfs_root *tree_root = fs_info->tree_root;
718 struct btrfs_path *path;
719 struct extent_buffer *l;
724 root = calloc(1, sizeof(*root));
726 return ERR_PTR(-ENOMEM);
727 if (location->offset == (u64)-1) {
728 ret = find_and_setup_root(tree_root, fs_info,
729 location->objectid, root);
737 btrfs_setup_root(tree_root->nodesize, tree_root->leafsize,
738 tree_root->sectorsize, tree_root->stripesize,
739 root, fs_info, location->objectid);
741 path = btrfs_alloc_path();
744 return ERR_PTR(-ENOMEM);
747 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
754 read_extent_buffer(l, &root->root_item,
755 btrfs_item_ptr_offset(l, path->slots[0]),
756 sizeof(root->root_item));
757 memcpy(&root->root_key, location, sizeof(*location));
760 btrfs_free_path(path);
765 generation = btrfs_root_generation(&root->root_item);
766 blocksize = root->nodesize;
767 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
768 blocksize, generation);
769 if (!extent_buffer_uptodate(root->node)) {
771 return ERR_PTR(-EIO);
778 static int btrfs_fs_roots_compare_objectids(struct rb_node *node,
781 u64 objectid = *((u64 *)data);
782 struct btrfs_root *root;
784 root = rb_entry(node, struct btrfs_root, rb_node);
785 if (objectid > root->objectid)
787 else if (objectid < root->objectid)
793 static int btrfs_fs_roots_compare_roots(struct rb_node *node1,
794 struct rb_node *node2)
796 struct btrfs_root *root;
798 root = rb_entry(node2, struct btrfs_root, rb_node);
799 return btrfs_fs_roots_compare_objectids(node1, (void *)&root->objectid);
802 struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
803 struct btrfs_key *location)
805 struct btrfs_root *root;
806 struct rb_node *node;
808 u64 objectid = location->objectid;
810 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
811 return fs_info->tree_root;
812 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
813 return fs_info->extent_root;
814 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
815 return fs_info->chunk_root;
816 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
817 return fs_info->dev_root;
818 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
819 return fs_info->csum_root;
820 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
821 return fs_info->quota_root;
823 BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
824 location->offset != (u64)-1);
826 node = rb_search(&fs_info->fs_root_tree, (void *)&objectid,
827 btrfs_fs_roots_compare_objectids, NULL);
829 return container_of(node, struct btrfs_root, rb_node);
831 root = btrfs_read_fs_root_no_cache(fs_info, location);
835 ret = rb_insert(&fs_info->fs_root_tree, &root->rb_node,
836 btrfs_fs_roots_compare_roots);
841 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
843 free(fs_info->tree_root);
844 free(fs_info->extent_root);
845 free(fs_info->chunk_root);
846 free(fs_info->dev_root);
847 free(fs_info->csum_root);
848 free(fs_info->quota_root);
849 free(fs_info->free_space_root);
850 free(fs_info->super_copy);
851 free(fs_info->log_root_tree);
855 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
857 struct btrfs_fs_info *fs_info;
859 fs_info = calloc(1, sizeof(struct btrfs_fs_info));
863 fs_info->tree_root = calloc(1, sizeof(struct btrfs_root));
864 fs_info->extent_root = calloc(1, sizeof(struct btrfs_root));
865 fs_info->chunk_root = calloc(1, sizeof(struct btrfs_root));
866 fs_info->dev_root = calloc(1, sizeof(struct btrfs_root));
867 fs_info->csum_root = calloc(1, sizeof(struct btrfs_root));
868 fs_info->quota_root = calloc(1, sizeof(struct btrfs_root));
869 fs_info->free_space_root = calloc(1, sizeof(struct btrfs_root));
870 fs_info->super_copy = calloc(1, BTRFS_SUPER_INFO_SIZE);
872 if (!fs_info->tree_root || !fs_info->extent_root ||
873 !fs_info->chunk_root || !fs_info->dev_root ||
874 !fs_info->csum_root || !fs_info->quota_root ||
875 !fs_info->free_space_root || !fs_info->super_copy)
878 extent_io_tree_init(&fs_info->extent_cache);
879 extent_io_tree_init(&fs_info->free_space_cache);
880 extent_io_tree_init(&fs_info->block_group_cache);
881 extent_io_tree_init(&fs_info->pinned_extents);
882 extent_io_tree_init(&fs_info->pending_del);
883 extent_io_tree_init(&fs_info->extent_ins);
884 fs_info->excluded_extents = NULL;
886 fs_info->fs_root_tree = RB_ROOT;
887 cache_tree_init(&fs_info->mapping_tree.cache_tree);
889 mutex_init(&fs_info->fs_mutex);
890 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
891 INIT_LIST_HEAD(&fs_info->space_info);
892 INIT_LIST_HEAD(&fs_info->recow_ebs);
895 fs_info->readonly = 1;
897 fs_info->super_bytenr = sb_bytenr;
898 fs_info->data_alloc_profile = (u64)-1;
899 fs_info->metadata_alloc_profile = (u64)-1;
900 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
903 btrfs_free_fs_info(fs_info);
907 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable)
911 features = btrfs_super_incompat_flags(sb) &
912 ~BTRFS_FEATURE_INCOMPAT_SUPP;
914 printk("couldn't open because of unsupported "
915 "option features (%Lx).\n",
916 (unsigned long long)features);
920 features = btrfs_super_incompat_flags(sb);
921 if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) {
922 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
923 btrfs_set_super_incompat_flags(sb, features);
926 features = btrfs_super_compat_ro_flags(sb) &
927 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
928 if (writable && features) {
929 printk("couldn't open RDWR because of unsupported "
930 "option features (%Lx).\n",
931 (unsigned long long)features);
937 static int find_best_backup_root(struct btrfs_super_block *super)
939 struct btrfs_root_backup *backup;
940 u64 orig_gen = btrfs_super_generation(super);
945 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
946 backup = super->super_roots + i;
947 if (btrfs_backup_tree_root_gen(backup) != orig_gen &&
948 btrfs_backup_tree_root_gen(backup) > gen) {
950 gen = btrfs_backup_tree_root_gen(backup);
956 static int setup_root_or_create_block(struct btrfs_fs_info *fs_info,
958 struct btrfs_root *info_root,
959 u64 objectid, char *str)
961 struct btrfs_super_block *sb = fs_info->super_copy;
962 struct btrfs_root *root = fs_info->tree_root;
963 u32 nodesize = btrfs_super_nodesize(sb);
966 ret = find_and_setup_root(root, fs_info, objectid, info_root);
968 printk("Couldn't setup %s tree\n", str);
969 if (!(flags & OPEN_CTREE_PARTIAL))
972 * Need a blank node here just so we don't screw up in the
973 * million of places that assume a root has a valid ->node
976 btrfs_find_create_tree_block(fs_info, 0, nodesize);
977 if (!info_root->node)
979 clear_extent_buffer_uptodate(info_root->node);
985 int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
988 struct btrfs_super_block *sb = fs_info->super_copy;
989 struct btrfs_root *root;
990 struct btrfs_key key;
999 nodesize = btrfs_super_nodesize(sb);
1000 leafsize = btrfs_super_leafsize(sb);
1001 sectorsize = btrfs_super_sectorsize(sb);
1002 stripesize = btrfs_super_stripesize(sb);
1004 root = fs_info->tree_root;
1005 btrfs_setup_root(nodesize, leafsize, sectorsize, stripesize,
1006 root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
1007 blocksize = root->nodesize;
1008 generation = btrfs_super_generation(sb);
1010 if (!root_tree_bytenr && !(flags & OPEN_CTREE_BACKUP_ROOT)) {
1011 root_tree_bytenr = btrfs_super_root(sb);
1012 } else if (flags & OPEN_CTREE_BACKUP_ROOT) {
1013 struct btrfs_root_backup *backup;
1014 int index = find_best_backup_root(sb);
1015 if (index >= BTRFS_NUM_BACKUP_ROOTS) {
1016 fprintf(stderr, "Invalid backup root number\n");
1019 backup = fs_info->super_copy->super_roots + index;
1020 root_tree_bytenr = btrfs_backup_tree_root(backup);
1021 generation = btrfs_backup_tree_root_gen(backup);
1024 root->node = read_tree_block(root, root_tree_bytenr, blocksize,
1026 if (!extent_buffer_uptodate(root->node)) {
1027 fprintf(stderr, "Couldn't read tree root\n");
1031 ret = setup_root_or_create_block(fs_info, flags, fs_info->extent_root,
1032 BTRFS_EXTENT_TREE_OBJECTID, "extent");
1035 fs_info->extent_root->track_dirty = 1;
1037 ret = find_and_setup_root(root, fs_info, BTRFS_DEV_TREE_OBJECTID,
1040 printk("Couldn't setup device tree\n");
1043 fs_info->dev_root->track_dirty = 1;
1045 ret = setup_root_or_create_block(fs_info, flags, fs_info->csum_root,
1046 BTRFS_CSUM_TREE_OBJECTID, "csum");
1049 fs_info->csum_root->track_dirty = 1;
1051 ret = find_and_setup_root(root, fs_info, BTRFS_QUOTA_TREE_OBJECTID,
1052 fs_info->quota_root);
1054 fs_info->quota_enabled = 1;
1056 if (btrfs_fs_compat_ro(fs_info, BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE)) {
1057 ret = find_and_setup_root(root, fs_info, BTRFS_FREE_SPACE_TREE_OBJECTID,
1058 fs_info->free_space_root);
1060 printk("Couldn't read free space tree\n");
1063 fs_info->free_space_root->track_dirty = 1;
1066 ret = find_and_setup_log_root(root, fs_info, sb);
1068 printk("Couldn't setup log root tree\n");
1069 if (!(flags & OPEN_CTREE_PARTIAL))
1073 fs_info->generation = generation;
1074 fs_info->last_trans_committed = generation;
1075 if (extent_buffer_uptodate(fs_info->extent_root->node) &&
1076 !(flags & OPEN_CTREE_NO_BLOCK_GROUPS))
1077 btrfs_read_block_groups(fs_info->tree_root);
1079 key.objectid = BTRFS_FS_TREE_OBJECTID;
1080 key.type = BTRFS_ROOT_ITEM_KEY;
1081 key.offset = (u64)-1;
1082 fs_info->fs_root = btrfs_read_fs_root(fs_info, &key);
1084 if (IS_ERR(fs_info->fs_root))
1089 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info)
1091 if (fs_info->free_space_root)
1092 free_extent_buffer(fs_info->free_space_root->node);
1093 if (fs_info->quota_root)
1094 free_extent_buffer(fs_info->quota_root->node);
1095 if (fs_info->csum_root)
1096 free_extent_buffer(fs_info->csum_root->node);
1097 if (fs_info->dev_root)
1098 free_extent_buffer(fs_info->dev_root->node);
1099 if (fs_info->extent_root)
1100 free_extent_buffer(fs_info->extent_root->node);
1101 if (fs_info->tree_root)
1102 free_extent_buffer(fs_info->tree_root->node);
1103 if (fs_info->log_root_tree)
1104 free_extent_buffer(fs_info->log_root_tree->node);
1105 if (fs_info->chunk_root)
1106 free_extent_buffer(fs_info->chunk_root->node);
1109 static void free_map_lookup(struct cache_extent *ce)
1111 struct map_lookup *map;
1113 map = container_of(ce, struct map_lookup, ce);
1117 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache, free_map_lookup);
1119 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
1121 while (!list_empty(&fs_info->recow_ebs)) {
1122 struct extent_buffer *eb;
1123 eb = list_first_entry(&fs_info->recow_ebs,
1124 struct extent_buffer, recow);
1125 list_del_init(&eb->recow);
1126 free_extent_buffer(eb);
1128 free_mapping_cache_tree(&fs_info->mapping_tree.cache_tree);
1129 extent_io_tree_cleanup(&fs_info->extent_cache);
1130 extent_io_tree_cleanup(&fs_info->free_space_cache);
1131 extent_io_tree_cleanup(&fs_info->block_group_cache);
1132 extent_io_tree_cleanup(&fs_info->pinned_extents);
1133 extent_io_tree_cleanup(&fs_info->pending_del);
1134 extent_io_tree_cleanup(&fs_info->extent_ins);
1137 int btrfs_scan_fs_devices(int fd, const char *path,
1138 struct btrfs_fs_devices **fs_devices,
1139 u64 sb_bytenr, unsigned sbflags,
1147 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1149 seek_ret = lseek(fd, 0, SEEK_END);
1153 dev_size = seek_ret;
1154 lseek(fd, 0, SEEK_SET);
1155 if (sb_bytenr > dev_size) {
1156 error("superblock bytenr %llu is larger than device size %llu",
1157 (unsigned long long)sb_bytenr,
1158 (unsigned long long)dev_size);
1162 ret = btrfs_scan_one_device(fd, path, fs_devices,
1163 &total_devs, sb_bytenr, sbflags);
1165 fprintf(stderr, "No valid Btrfs found on %s\n", path);
1169 if (!skip_devices && total_devs != 1) {
1170 ret = btrfs_scan_devices();
1177 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
1178 u64 chunk_root_bytenr)
1180 struct btrfs_super_block *sb = fs_info->super_copy;
1189 nodesize = btrfs_super_nodesize(sb);
1190 leafsize = btrfs_super_leafsize(sb);
1191 sectorsize = btrfs_super_sectorsize(sb);
1192 stripesize = btrfs_super_stripesize(sb);
1194 btrfs_setup_root(nodesize, leafsize, sectorsize, stripesize,
1195 fs_info->chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1197 ret = btrfs_read_sys_array(fs_info->chunk_root);
1201 blocksize = fs_info->chunk_root->nodesize;
1202 generation = btrfs_super_chunk_root_generation(sb);
1204 if (chunk_root_bytenr && !IS_ALIGNED(chunk_root_bytenr,
1205 btrfs_super_sectorsize(sb))) {
1206 warning("chunk_root_bytenr %llu is unaligned to %u, ignore it",
1207 chunk_root_bytenr, btrfs_super_sectorsize(sb));
1208 chunk_root_bytenr = 0;
1211 if (!chunk_root_bytenr)
1212 chunk_root_bytenr = btrfs_super_chunk_root(sb);
1216 fs_info->chunk_root->node = read_tree_block(fs_info->chunk_root,
1218 blocksize, generation);
1219 if (!extent_buffer_uptodate(fs_info->chunk_root->node)) {
1220 if (fs_info->ignore_chunk_tree_error) {
1221 warning("cannot read chunk root, continue anyway");
1222 fs_info->chunk_root = NULL;
1225 error("cannot read chunk root");
1230 if (!(btrfs_super_flags(sb) & BTRFS_SUPER_FLAG_METADUMP)) {
1231 ret = btrfs_read_chunk_tree(fs_info->chunk_root);
1233 fprintf(stderr, "Couldn't read chunk tree\n");
1240 static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
1242 u64 root_tree_bytenr,
1243 u64 chunk_root_bytenr,
1246 struct btrfs_fs_info *fs_info;
1247 struct btrfs_super_block *disk_super;
1248 struct btrfs_fs_devices *fs_devices = NULL;
1249 struct extent_buffer *eb;
1252 unsigned sbflags = SBREAD_DEFAULT;
1255 sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
1257 /* try to drop all the caches */
1258 if (posix_fadvise(fp, 0, 0, POSIX_FADV_DONTNEED))
1259 fprintf(stderr, "Warning, could not drop caches\n");
1261 fs_info = btrfs_new_fs_info(flags & OPEN_CTREE_WRITES, sb_bytenr);
1263 fprintf(stderr, "Failed to allocate memory for fs_info\n");
1266 if (flags & OPEN_CTREE_RESTORE)
1267 fs_info->on_restoring = 1;
1268 if (flags & OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS)
1269 fs_info->suppress_check_block_errors = 1;
1270 if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
1271 fs_info->ignore_fsid_mismatch = 1;
1272 if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR)
1273 fs_info->ignore_chunk_tree_error = 1;
1275 if ((flags & OPEN_CTREE_RECOVER_SUPER)
1276 && (flags & OPEN_CTREE_FS_PARTIAL)) {
1278 "cannot open a partially created filesystem for recovery");
1282 if (flags & OPEN_CTREE_FS_PARTIAL)
1283 sbflags = SBREAD_PARTIAL;
1285 ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr, sbflags,
1286 (flags & OPEN_CTREE_NO_DEVICES));
1290 fs_info->fs_devices = fs_devices;
1291 if (flags & OPEN_CTREE_WRITES)
1296 if (flags & OPEN_CTREE_EXCLUSIVE)
1299 ret = btrfs_open_devices(fs_devices, oflags);
1303 disk_super = fs_info->super_copy;
1304 if (flags & OPEN_CTREE_RECOVER_SUPER)
1305 ret = btrfs_read_dev_super(fs_devices->latest_bdev, disk_super,
1306 sb_bytenr, SBREAD_RECOVER);
1308 ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr,
1311 printk("No valid btrfs found\n");
1315 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_CHANGING_FSID &&
1316 !fs_info->ignore_fsid_mismatch) {
1317 fprintf(stderr, "ERROR: Filesystem UUID change in progress\n");
1321 memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE);
1323 ret = btrfs_check_fs_compatibility(fs_info->super_copy,
1324 flags & OPEN_CTREE_WRITES);
1328 ret = btrfs_setup_chunk_tree_and_device_map(fs_info, chunk_root_bytenr);
1332 /* Chunk tree root is unable to read, return directly */
1333 if (!fs_info->chunk_root)
1336 eb = fs_info->chunk_root->node;
1337 read_extent_buffer(eb, fs_info->chunk_tree_uuid,
1338 btrfs_header_chunk_tree_uuid(eb),
1341 ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
1342 if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT) &&
1343 !fs_info->ignore_chunk_tree_error)
1349 btrfs_release_all_roots(fs_info);
1350 btrfs_cleanup_all_caches(fs_info);
1352 btrfs_close_devices(fs_devices);
1354 btrfs_free_fs_info(fs_info);
1358 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
1359 u64 sb_bytenr, u64 root_tree_bytenr,
1360 u64 chunk_root_bytenr,
1365 struct btrfs_fs_info *info;
1366 int oflags = O_RDWR;
1369 ret = stat(filename, &st);
1371 error("cannot stat '%s': %s", filename, strerror(errno));
1374 if (!(((st.st_mode & S_IFMT) == S_IFREG) || ((st.st_mode & S_IFMT) == S_IFBLK))) {
1375 error("not a regular file or block device: %s", filename);
1379 if (!(flags & OPEN_CTREE_WRITES))
1382 fp = open(filename, oflags);
1384 error("cannot open '%s': %s", filename, strerror(errno));
1387 info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
1388 chunk_root_bytenr, flags);
1393 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
1396 struct btrfs_fs_info *info;
1398 /* This flags may not return fs_info with any valid root */
1399 BUG_ON(flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
1400 info = open_ctree_fs_info(filename, sb_bytenr, 0, 0, flags);
1403 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1404 return info->chunk_root;
1405 return info->fs_root;
1408 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
1411 struct btrfs_fs_info *info;
1413 /* This flags may not return fs_info with any valid root */
1414 if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR) {
1415 error("invalid open_ctree flags: 0x%llx",
1416 (unsigned long long)flags);
1419 info = __open_ctree_fd(fp, path, sb_bytenr, 0, 0, flags);
1422 if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
1423 return info->chunk_root;
1424 return info->fs_root;
1428 * Check if the super is valid:
1429 * - nodesize/sectorsize - minimum, maximum, alignment
1430 * - tree block starts - alignment
1431 * - number of devices - something sane
1432 * - sys array size - maximum
1434 static int check_super(struct btrfs_super_block *sb, unsigned sbflags)
1436 u8 result[BTRFS_CSUM_SIZE];
1441 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
1442 if (btrfs_super_magic(sb) == BTRFS_MAGIC_PARTIAL) {
1443 if (!(sbflags & SBREAD_PARTIAL)) {
1444 error("superblock magic doesn't match");
1450 csum_type = btrfs_super_csum_type(sb);
1451 if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
1452 error("unsupported checksum algorithm %u", csum_type);
1455 csum_size = btrfs_csum_sizes[csum_type];
1458 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1459 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1460 btrfs_csum_final(crc, result);
1462 if (memcmp(result, sb->csum, csum_size)) {
1463 error("superblock checksum mismatch");
1466 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
1467 error("tree_root level too big: %d >= %d",
1468 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
1471 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
1472 error("chunk_root level too big: %d >= %d",
1473 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
1476 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
1477 error("log_root level too big: %d >= %d",
1478 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
1482 if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) {
1483 error("tree_root block unaligned: %llu", btrfs_super_root(sb));
1486 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) {
1487 error("chunk_root block unaligned: %llu",
1488 btrfs_super_chunk_root(sb));
1491 if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096)) {
1492 error("log_root block unaligned: %llu",
1493 btrfs_super_log_root(sb));
1496 if (btrfs_super_nodesize(sb) < 4096) {
1497 error("nodesize too small: %u < 4096",
1498 btrfs_super_nodesize(sb));
1501 if (!IS_ALIGNED(btrfs_super_nodesize(sb), 4096)) {
1502 error("nodesize unaligned: %u", btrfs_super_nodesize(sb));
1505 if (btrfs_super_sectorsize(sb) < 4096) {
1506 error("sectorsize too small: %u < 4096",
1507 btrfs_super_sectorsize(sb));
1510 if (!IS_ALIGNED(btrfs_super_sectorsize(sb), 4096)) {
1511 error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb));
1514 if (btrfs_super_total_bytes(sb) == 0) {
1515 error("invalid total_bytes 0");
1518 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
1519 error("invalid bytes_used %llu", btrfs_super_bytes_used(sb));
1522 if ((btrfs_super_stripesize(sb) != 4096)
1523 && (btrfs_super_stripesize(sb) != btrfs_super_sectorsize(sb))) {
1524 error("invalid stripesize %u", btrfs_super_stripesize(sb));
1528 if (memcmp(sb->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
1529 char fsid[BTRFS_UUID_UNPARSED_SIZE];
1530 char dev_fsid[BTRFS_UUID_UNPARSED_SIZE];
1532 uuid_unparse(sb->fsid, fsid);
1533 uuid_unparse(sb->dev_item.fsid, dev_fsid);
1534 error("dev_item UUID does not match fsid: %s != %s",
1540 * Hint to catch really bogus numbers, bitflips or so
1542 if (btrfs_super_num_devices(sb) > (1UL << 31)) {
1543 warning("suspicious number of devices: %llu",
1544 btrfs_super_num_devices(sb));
1547 if (btrfs_super_num_devices(sb) == 0) {
1548 error("number of devices is 0");
1553 * Obvious sys_chunk_array corruptions, it must hold at least one key
1556 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
1557 error("system chunk array too big %u > %u",
1558 btrfs_super_sys_array_size(sb),
1559 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
1562 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
1563 + sizeof(struct btrfs_chunk)) {
1564 error("system chunk array too small %u < %zu",
1565 btrfs_super_sys_array_size(sb),
1566 sizeof(struct btrfs_disk_key) +
1567 sizeof(struct btrfs_chunk));
1574 error("superblock checksum matches but it has invalid members");
1578 int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
1581 u8 fsid[BTRFS_FSID_SIZE];
1582 int fsid_is_initialized = 0;
1583 char tmp[BTRFS_SUPER_INFO_SIZE];
1584 struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
1587 int max_super = sbflags & SBREAD_RECOVER ? BTRFS_SUPER_MIRROR_MAX : 1;
1591 if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1592 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
1597 /* Not large enough sb, return -ENOENT instead of normal -EIO */
1598 if (ret < BTRFS_SUPER_INFO_SIZE)
1601 if (btrfs_super_bytenr(buf) != sb_bytenr)
1604 ret = check_super(buf, sbflags);
1607 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1612 * we would like to check all the supers, but that would make
1613 * a btrfs mount succeed after a mkfs from a different FS.
1614 * So, we need to add a special mount option to scan for
1615 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1618 for (i = 0; i < max_super; i++) {
1619 bytenr = btrfs_sb_offset(i);
1620 ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1621 if (ret < BTRFS_SUPER_INFO_SIZE)
1624 if (btrfs_super_bytenr(buf) != bytenr )
1626 /* if magic is NULL, the device was removed */
1627 if (btrfs_super_magic(buf) == 0 && i == 0)
1629 if (check_super(buf, sbflags))
1632 if (!fsid_is_initialized) {
1633 memcpy(fsid, buf->fsid, sizeof(fsid));
1634 fsid_is_initialized = 1;
1635 } else if (memcmp(fsid, buf->fsid, sizeof(fsid))) {
1637 * the superblocks (the original one and
1638 * its backups) contain data of different
1639 * filesystems -> the super cannot be trusted
1644 if (btrfs_super_generation(buf) > transid) {
1645 memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
1646 transid = btrfs_super_generation(buf);
1650 return transid > 0 ? 0 : -1;
1653 static int write_dev_supers(struct btrfs_root *root,
1654 struct btrfs_super_block *sb,
1655 struct btrfs_device *device)
1661 if (root->fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
1662 btrfs_set_super_bytenr(sb, root->fs_info->super_bytenr);
1664 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1665 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1666 btrfs_csum_final(crc, &sb->csum[0]);
1669 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1670 * zero filled, we can use it directly
1672 ret = pwrite64(device->fd, root->fs_info->super_copy,
1673 BTRFS_SUPER_INFO_SIZE,
1674 root->fs_info->super_bytenr);
1675 if (ret != BTRFS_SUPER_INFO_SIZE)
1680 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1681 bytenr = btrfs_sb_offset(i);
1682 if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
1685 btrfs_set_super_bytenr(sb, bytenr);
1688 crc = btrfs_csum_data(NULL, (char *)sb + BTRFS_CSUM_SIZE, crc,
1689 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
1690 btrfs_csum_final(crc, &sb->csum[0]);
1693 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1694 * zero filled, we can use it directly
1696 ret = pwrite64(device->fd, root->fs_info->super_copy,
1697 BTRFS_SUPER_INFO_SIZE, bytenr);
1698 if (ret != BTRFS_SUPER_INFO_SIZE)
1706 fprintf(stderr, "WARNING: failed to write all sb data\n");
1708 fprintf(stderr, "WARNING: failed to write sb: %s\n",
1713 int write_all_supers(struct btrfs_root *root)
1715 struct list_head *cur;
1716 struct list_head *head = &root->fs_info->fs_devices->devices;
1717 struct btrfs_device *dev;
1718 struct btrfs_super_block *sb;
1719 struct btrfs_dev_item *dev_item;
1723 sb = root->fs_info->super_copy;
1724 dev_item = &sb->dev_item;
1725 list_for_each(cur, head) {
1726 dev = list_entry(cur, struct btrfs_device, dev_list);
1727 if (!dev->writeable)
1730 btrfs_set_stack_device_generation(dev_item, 0);
1731 btrfs_set_stack_device_type(dev_item, dev->type);
1732 btrfs_set_stack_device_id(dev_item, dev->devid);
1733 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
1734 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
1735 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
1736 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
1737 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
1738 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
1739 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
1741 flags = btrfs_super_flags(sb);
1742 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
1744 ret = write_dev_supers(root, sb, dev);
1750 int write_ctree_super(struct btrfs_trans_handle *trans,
1751 struct btrfs_root *root)
1754 struct btrfs_root *tree_root = root->fs_info->tree_root;
1755 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1757 if (root->fs_info->readonly)
1760 btrfs_set_super_generation(root->fs_info->super_copy,
1762 btrfs_set_super_root(root->fs_info->super_copy,
1763 tree_root->node->start);
1764 btrfs_set_super_root_level(root->fs_info->super_copy,
1765 btrfs_header_level(tree_root->node));
1766 btrfs_set_super_chunk_root(root->fs_info->super_copy,
1767 chunk_root->node->start);
1768 btrfs_set_super_chunk_root_level(root->fs_info->super_copy,
1769 btrfs_header_level(chunk_root->node));
1770 btrfs_set_super_chunk_root_generation(root->fs_info->super_copy,
1771 btrfs_header_generation(chunk_root->node));
1773 ret = write_all_supers(root);
1775 fprintf(stderr, "failed to write new super block err %d\n", ret);
1779 int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
1782 struct btrfs_trans_handle *trans;
1783 struct btrfs_root *root = fs_info->tree_root;
1785 if (fs_info->last_trans_committed !=
1786 fs_info->generation) {
1788 trans = btrfs_start_transaction(root, 1);
1789 btrfs_commit_transaction(trans, root);
1790 trans = btrfs_start_transaction(root, 1);
1791 ret = commit_tree_roots(trans, fs_info);
1793 ret = __commit_transaction(trans, root);
1795 write_ctree_super(trans, root);
1796 btrfs_free_transaction(root, trans);
1799 if (fs_info->finalize_on_close) {
1800 btrfs_set_super_magic(fs_info->super_copy, BTRFS_MAGIC);
1801 root->fs_info->finalize_on_close = 0;
1802 ret = write_all_supers(root);
1805 "failed to write new super block err %d\n", ret);
1807 btrfs_free_block_groups(fs_info);
1809 free_fs_roots_tree(&fs_info->fs_root_tree);
1811 btrfs_release_all_roots(fs_info);
1812 btrfs_close_devices(fs_info->fs_devices);
1813 btrfs_cleanup_all_caches(fs_info);
1814 btrfs_free_fs_info(fs_info);
1818 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1819 struct extent_buffer *eb)
1821 return clear_extent_buffer_dirty(eb);
1824 int wait_on_tree_block_writeback(struct btrfs_root *root,
1825 struct extent_buffer *eb)
1830 void btrfs_mark_buffer_dirty(struct extent_buffer *eb)
1832 set_extent_buffer_dirty(eb);
1835 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
1839 ret = extent_buffer_uptodate(buf);
1843 ret = verify_parent_transid(buf->tree, buf, parent_transid, 1);
1847 int btrfs_set_buffer_uptodate(struct extent_buffer *eb)
1849 return set_extent_buffer_uptodate(eb);