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 leafsize
= btrfs_super_leafsize(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 fs_devices
= fs_info
->fs_devices
;
72 if (fs_info
->ignore_fsid_mismatch
||
73 !memcmp_extent_buffer(buf
, fs_devices
->fsid
,
79 fs_devices
= fs_devices
->seed
;
84 static void print_tree_block_error(struct btrfs_fs_info
*fs_info
,
85 struct extent_buffer
*eb
,
88 char fs_uuid
[BTRFS_UUID_UNPARSED_SIZE
] = {'\0'};
89 char found_uuid
[BTRFS_UUID_UNPARSED_SIZE
] = {'\0'};
90 u8 buf
[BTRFS_UUID_SIZE
];
94 read_extent_buffer(eb
, buf
, btrfs_header_fsid(),
96 uuid_unparse(buf
, found_uuid
);
97 uuid_unparse(fs_info
->fsid
, fs_uuid
);
98 fprintf(stderr
, "fsid mismatch, want=%s, have=%s\n",
101 case BTRFS_BAD_BYTENR
:
102 fprintf(stderr
, "bytenr mismatch, want=%llu, have=%llu\n",
103 eb
->start
, btrfs_header_bytenr(eb
));
105 case BTRFS_BAD_LEVEL
:
106 fprintf(stderr
, "bad level, %u > %u\n",
107 btrfs_header_level(eb
), BTRFS_MAX_LEVEL
);
109 case BTRFS_BAD_NRITEMS
:
110 fprintf(stderr
, "invalid nr_items: %u\n",
111 btrfs_header_nritems(eb
));
116 u32
btrfs_csum_data(struct btrfs_root
*root
, char *data
, u32 seed
, size_t len
)
118 return crc32c(seed
, data
, len
);
121 void btrfs_csum_final(u32 crc
, char *result
)
123 *(__le32
*)result
= ~cpu_to_le32(crc
);
126 static int __csum_tree_block_size(struct extent_buffer
*buf
, u16 csum_size
,
127 int verify
, int silent
)
129 char result
[BTRFS_CSUM_SIZE
];
133 len
= buf
->len
- BTRFS_CSUM_SIZE
;
134 crc
= crc32c(crc
, buf
->data
+ BTRFS_CSUM_SIZE
, len
);
135 btrfs_csum_final(crc
, result
);
138 if (memcmp_extent_buffer(buf
, result
, 0, csum_size
)) {
140 printk("checksum verify failed on %llu found %08X wanted %08X\n",
141 (unsigned long long)buf
->start
,
143 *((u32
*)(char *)buf
->data
));
147 write_extent_buffer(buf
, result
, 0, csum_size
);
152 int csum_tree_block_size(struct extent_buffer
*buf
, u16 csum_size
, int verify
)
154 return __csum_tree_block_size(buf
, csum_size
, verify
, 0);
157 int verify_tree_block_csum_silent(struct extent_buffer
*buf
, u16 csum_size
)
159 return __csum_tree_block_size(buf
, csum_size
, 1, 1);
162 static int csum_tree_block_fs_info(struct btrfs_fs_info
*fs_info
,
163 struct extent_buffer
*buf
, int verify
)
166 btrfs_super_csum_size(fs_info
->super_copy
);
167 if (verify
&& fs_info
->suppress_check_block_errors
)
168 return verify_tree_block_csum_silent(buf
, csum_size
);
169 return csum_tree_block_size(buf
, csum_size
, verify
);
172 int csum_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
,
175 return csum_tree_block_fs_info(root
->fs_info
, buf
, verify
);
178 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
179 u64 bytenr
, u32 blocksize
)
181 return find_extent_buffer(&root
->fs_info
->extent_cache
,
185 struct extent_buffer
* btrfs_find_create_tree_block(
186 struct btrfs_fs_info
*fs_info
, u64 bytenr
, u32 blocksize
)
188 return alloc_extent_buffer(&fs_info
->extent_cache
, bytenr
, blocksize
);
191 void readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
,
194 struct extent_buffer
*eb
;
196 struct btrfs_multi_bio
*multi
= NULL
;
197 struct btrfs_device
*device
;
199 eb
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
200 if (!(eb
&& btrfs_buffer_uptodate(eb
, parent_transid
)) &&
201 !btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
202 bytenr
, &length
, &multi
, 0, NULL
)) {
203 device
= multi
->stripes
[0].dev
;
205 blocksize
= min(blocksize
, (u32
)(64 * 1024));
206 readahead(device
->fd
, multi
->stripes
[0].physical
, blocksize
);
209 free_extent_buffer(eb
);
213 static int verify_parent_transid(struct extent_io_tree
*io_tree
,
214 struct extent_buffer
*eb
, u64 parent_transid
,
219 if (!parent_transid
|| btrfs_header_generation(eb
) == parent_transid
)
222 if (extent_buffer_uptodate(eb
) &&
223 btrfs_header_generation(eb
) == parent_transid
) {
227 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
228 (unsigned long long)eb
->start
,
229 (unsigned long long)parent_transid
,
230 (unsigned long long)btrfs_header_generation(eb
));
232 eb
->flags
|= EXTENT_BAD_TRANSID
;
233 printk("Ignoring transid failure\n");
239 clear_extent_buffer_uptodate(io_tree
, eb
);
245 int read_whole_eb(struct btrfs_fs_info
*info
, struct extent_buffer
*eb
, int mirror
)
247 unsigned long offset
= 0;
248 struct btrfs_multi_bio
*multi
= NULL
;
249 struct btrfs_device
*device
;
252 unsigned long bytes_left
= eb
->len
;
255 read_len
= bytes_left
;
258 if (!info
->on_restoring
&&
259 eb
->start
!= BTRFS_SUPER_INFO_OFFSET
) {
260 ret
= btrfs_map_block(&info
->mapping_tree
, READ
,
261 eb
->start
+ offset
, &read_len
, &multi
,
264 printk("Couldn't map the block %Lu\n", eb
->start
+ offset
);
268 device
= multi
->stripes
[0].dev
;
270 if (device
->fd
<= 0) {
277 eb
->dev_bytenr
= multi
->stripes
[0].physical
;
281 /* special case for restore metadump */
282 list_for_each_entry(device
, &info
->fs_devices
->devices
, dev_list
) {
283 if (device
->devid
== 1)
288 eb
->dev_bytenr
= eb
->start
;
292 if (read_len
> bytes_left
)
293 read_len
= bytes_left
;
295 ret
= read_extent_from_disk(eb
, offset
, read_len
);
299 bytes_left
-= read_len
;
304 struct extent_buffer
* read_tree_block_fs_info(
305 struct btrfs_fs_info
*fs_info
, u64 bytenr
, u32 blocksize
,
309 struct extent_buffer
*eb
;
310 u64 best_transid
= 0;
316 eb
= btrfs_find_create_tree_block(fs_info
, bytenr
, blocksize
);
318 return ERR_PTR(-ENOMEM
);
320 if (btrfs_buffer_uptodate(eb
, parent_transid
))
324 ret
= read_whole_eb(fs_info
, eb
, mirror_num
);
325 if (ret
== 0 && csum_tree_block_fs_info(fs_info
, eb
, 1) == 0 &&
326 check_tree_block(fs_info
, eb
) == 0 &&
327 verify_parent_transid(eb
->tree
, eb
, parent_transid
, ignore
)
329 if (eb
->flags
& EXTENT_BAD_TRANSID
&&
330 list_empty(&eb
->recow
)) {
331 list_add_tail(&eb
->recow
,
332 &fs_info
->recow_ebs
);
335 btrfs_set_buffer_uptodate(eb
);
339 if (check_tree_block(fs_info
, eb
)) {
340 if (!fs_info
->suppress_check_block_errors
)
341 print_tree_block_error(fs_info
, eb
,
342 check_tree_block(fs_info
, eb
));
344 if (!fs_info
->suppress_check_block_errors
)
345 fprintf(stderr
, "Csum didn't match\n");
350 num_copies
= btrfs_num_copies(&fs_info
->mapping_tree
,
352 if (num_copies
== 1) {
356 if (btrfs_header_generation(eb
) > best_transid
&& mirror_num
) {
357 best_transid
= btrfs_header_generation(eb
);
358 good_mirror
= mirror_num
;
361 if (mirror_num
> num_copies
) {
362 mirror_num
= good_mirror
;
367 free_extent_buffer(eb
);
371 int read_extent_data(struct btrfs_root
*root
, char *data
,
372 u64 logical
, u64
*len
, int mirror
)
375 struct btrfs_multi_bio
*multi
= NULL
;
376 struct btrfs_fs_info
*info
= root
->fs_info
;
377 struct btrfs_device
*device
;
381 ret
= btrfs_map_block(&info
->mapping_tree
, READ
, logical
, len
,
382 &multi
, mirror
, NULL
);
384 fprintf(stderr
, "Couldn't map the block %llu\n",
388 device
= multi
->stripes
[0].dev
;
395 ret
= pread64(device
->fd
, data
, *len
, multi
->stripes
[0].physical
);
405 int write_and_map_eb(struct btrfs_trans_handle
*trans
,
406 struct btrfs_root
*root
,
407 struct extent_buffer
*eb
)
412 u64
*raid_map
= NULL
;
413 struct btrfs_multi_bio
*multi
= NULL
;
417 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, WRITE
,
418 eb
->start
, &length
, &multi
, 0, &raid_map
);
421 ret
= write_raid56_with_parity(root
->fs_info
, eb
, multi
,
424 } else while (dev_nr
< multi
->num_stripes
) {
426 eb
->fd
= multi
->stripes
[dev_nr
].dev
->fd
;
427 eb
->dev_bytenr
= multi
->stripes
[dev_nr
].physical
;
428 multi
->stripes
[dev_nr
].dev
->total_ios
++;
430 ret
= write_extent_to_disk(eb
);
438 int write_tree_block(struct btrfs_trans_handle
*trans
,
439 struct btrfs_root
*root
,
440 struct extent_buffer
*eb
)
442 if (check_tree_block(root
->fs_info
, eb
)) {
443 print_tree_block_error(root
->fs_info
, eb
,
444 check_tree_block(root
->fs_info
, eb
));
448 if (trans
&& !btrfs_buffer_uptodate(eb
, trans
->transid
))
451 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
452 csum_tree_block(root
, eb
, 0);
454 return write_and_map_eb(trans
, root
, eb
);
457 int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
458 u32 stripesize
, struct btrfs_root
*root
,
459 struct btrfs_fs_info
*fs_info
, u64 objectid
)
462 root
->commit_root
= NULL
;
463 root
->sectorsize
= sectorsize
;
464 root
->nodesize
= nodesize
;
465 root
->leafsize
= leafsize
;
466 root
->stripesize
= stripesize
;
468 root
->track_dirty
= 0;
470 root
->fs_info
= fs_info
;
471 root
->objectid
= objectid
;
472 root
->last_trans
= 0;
473 root
->highest_inode
= 0;
474 root
->last_inode_alloc
= 0;
476 INIT_LIST_HEAD(&root
->dirty_list
);
477 INIT_LIST_HEAD(&root
->orphan_data_extents
);
478 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
479 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
480 root
->root_key
.objectid
= objectid
;
484 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
485 struct btrfs_root
*root
)
489 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
491 btrfs_write_dirty_block_groups(trans
, root
);
493 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
494 if (old_root_bytenr
== root
->node
->start
)
496 btrfs_set_root_bytenr(&root
->root_item
,
498 btrfs_set_root_generation(&root
->root_item
,
500 root
->root_item
.level
= btrfs_header_level(root
->node
);
501 ret
= btrfs_update_root(trans
, tree_root
,
505 btrfs_write_dirty_block_groups(trans
, root
);
510 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
511 struct btrfs_fs_info
*fs_info
)
513 struct btrfs_root
*root
;
514 struct list_head
*next
;
515 struct extent_buffer
*eb
;
518 if (fs_info
->readonly
)
521 eb
= fs_info
->tree_root
->node
;
522 extent_buffer_get(eb
);
523 ret
= btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
, 0, &eb
);
524 free_extent_buffer(eb
);
528 while(!list_empty(&fs_info
->dirty_cowonly_roots
)) {
529 next
= fs_info
->dirty_cowonly_roots
.next
;
531 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
532 update_cowonly_root(trans
, root
);
533 free_extent_buffer(root
->commit_root
);
534 root
->commit_root
= NULL
;
540 static int __commit_transaction(struct btrfs_trans_handle
*trans
,
541 struct btrfs_root
*root
)
545 struct extent_buffer
*eb
;
546 struct extent_io_tree
*tree
= &root
->fs_info
->extent_cache
;
550 ret
= find_first_extent_bit(tree
, 0, &start
, &end
,
554 while(start
<= end
) {
555 eb
= find_first_extent_buffer(tree
, start
);
556 BUG_ON(!eb
|| eb
->start
!= start
);
557 ret
= write_tree_block(trans
, root
, eb
);
560 clear_extent_buffer_dirty(eb
);
561 free_extent_buffer(eb
);
567 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
568 struct btrfs_root
*root
)
570 u64 transid
= trans
->transid
;
572 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
574 if (root
->commit_root
== root
->node
)
576 if (root
== root
->fs_info
->tree_root
)
578 if (root
== root
->fs_info
->chunk_root
)
581 free_extent_buffer(root
->commit_root
);
582 root
->commit_root
= NULL
;
584 btrfs_set_root_bytenr(&root
->root_item
, root
->node
->start
);
585 btrfs_set_root_generation(&root
->root_item
, trans
->transid
);
586 root
->root_item
.level
= btrfs_header_level(root
->node
);
587 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
588 &root
->root_key
, &root
->root_item
);
591 ret
= commit_tree_roots(trans
, fs_info
);
593 ret
= __commit_transaction(trans
, root
);
595 write_ctree_super(trans
, root
);
596 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
597 &fs_info
->pinned_extents
);
598 btrfs_free_transaction(root
, trans
);
599 free_extent_buffer(root
->commit_root
);
600 root
->commit_root
= NULL
;
601 fs_info
->running_transaction
= NULL
;
602 fs_info
->last_trans_committed
= transid
;
606 static int find_and_setup_root(struct btrfs_root
*tree_root
,
607 struct btrfs_fs_info
*fs_info
,
608 u64 objectid
, struct btrfs_root
*root
)
614 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
615 tree_root
->sectorsize
, tree_root
->stripesize
,
616 root
, fs_info
, objectid
);
617 ret
= btrfs_find_last_root(tree_root
, objectid
,
618 &root
->root_item
, &root
->root_key
);
622 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
623 generation
= btrfs_root_generation(&root
->root_item
);
624 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
625 blocksize
, generation
);
626 if (!extent_buffer_uptodate(root
->node
))
632 static int find_and_setup_log_root(struct btrfs_root
*tree_root
,
633 struct btrfs_fs_info
*fs_info
,
634 struct btrfs_super_block
*disk_super
)
637 u64 blocknr
= btrfs_super_log_root(disk_super
);
638 struct btrfs_root
*log_root
= malloc(sizeof(struct btrfs_root
));
648 blocksize
= btrfs_level_size(tree_root
,
649 btrfs_super_log_root_level(disk_super
));
651 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
652 tree_root
->sectorsize
, tree_root
->stripesize
,
653 log_root
, fs_info
, BTRFS_TREE_LOG_OBJECTID
);
655 log_root
->node
= read_tree_block(tree_root
, blocknr
,
657 btrfs_super_generation(disk_super
) + 1);
659 fs_info
->log_root_tree
= log_root
;
661 if (!extent_buffer_uptodate(log_root
->node
)) {
662 free_extent_buffer(log_root
->node
);
664 fs_info
->log_root_tree
= NULL
;
671 int btrfs_free_fs_root(struct btrfs_root
*root
)
674 free_extent_buffer(root
->node
);
675 if (root
->commit_root
)
676 free_extent_buffer(root
->commit_root
);
681 static void __free_fs_root(struct rb_node
*node
)
683 struct btrfs_root
*root
;
685 root
= container_of(node
, struct btrfs_root
, rb_node
);
686 btrfs_free_fs_root(root
);
689 FREE_RB_BASED_TREE(fs_roots
, __free_fs_root
);
691 struct btrfs_root
*btrfs_read_fs_root_no_cache(struct btrfs_fs_info
*fs_info
,
692 struct btrfs_key
*location
)
694 struct btrfs_root
*root
;
695 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
696 struct btrfs_path
*path
;
697 struct extent_buffer
*l
;
702 root
= calloc(1, sizeof(*root
));
704 return ERR_PTR(-ENOMEM
);
705 if (location
->offset
== (u64
)-1) {
706 ret
= find_and_setup_root(tree_root
, fs_info
,
707 location
->objectid
, root
);
715 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
716 tree_root
->sectorsize
, tree_root
->stripesize
,
717 root
, fs_info
, location
->objectid
);
719 path
= btrfs_alloc_path();
721 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
728 read_extent_buffer(l
, &root
->root_item
,
729 btrfs_item_ptr_offset(l
, path
->slots
[0]),
730 sizeof(root
->root_item
));
731 memcpy(&root
->root_key
, location
, sizeof(*location
));
734 btrfs_free_path(path
);
739 generation
= btrfs_root_generation(&root
->root_item
);
740 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
741 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
742 blocksize
, generation
);
743 if (!extent_buffer_uptodate(root
->node
)) {
745 return ERR_PTR(-EIO
);
752 static int btrfs_fs_roots_compare_objectids(struct rb_node
*node
,
755 u64 objectid
= *((u64
*)data
);
756 struct btrfs_root
*root
;
758 root
= rb_entry(node
, struct btrfs_root
, rb_node
);
759 if (objectid
> root
->objectid
)
761 else if (objectid
< root
->objectid
)
767 static int btrfs_fs_roots_compare_roots(struct rb_node
*node1
,
768 struct rb_node
*node2
)
770 struct btrfs_root
*root
;
772 root
= rb_entry(node2
, struct btrfs_root
, rb_node
);
773 return btrfs_fs_roots_compare_objectids(node1
, (void *)&root
->objectid
);
776 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
777 struct btrfs_key
*location
)
779 struct btrfs_root
*root
;
780 struct rb_node
*node
;
782 u64 objectid
= location
->objectid
;
784 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
785 return fs_info
->tree_root
;
786 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
787 return fs_info
->extent_root
;
788 if (location
->objectid
== BTRFS_CHUNK_TREE_OBJECTID
)
789 return fs_info
->chunk_root
;
790 if (location
->objectid
== BTRFS_DEV_TREE_OBJECTID
)
791 return fs_info
->dev_root
;
792 if (location
->objectid
== BTRFS_CSUM_TREE_OBJECTID
)
793 return fs_info
->csum_root
;
794 if (location
->objectid
== BTRFS_QUOTA_TREE_OBJECTID
)
795 return fs_info
->quota_root
;
797 BUG_ON(location
->objectid
== BTRFS_TREE_RELOC_OBJECTID
||
798 location
->offset
!= (u64
)-1);
800 node
= rb_search(&fs_info
->fs_root_tree
, (void *)&objectid
,
801 btrfs_fs_roots_compare_objectids
, NULL
);
803 return container_of(node
, struct btrfs_root
, rb_node
);
805 root
= btrfs_read_fs_root_no_cache(fs_info
, location
);
809 ret
= rb_insert(&fs_info
->fs_root_tree
, &root
->rb_node
,
810 btrfs_fs_roots_compare_roots
);
815 void btrfs_free_fs_info(struct btrfs_fs_info
*fs_info
)
817 free(fs_info
->tree_root
);
818 free(fs_info
->extent_root
);
819 free(fs_info
->chunk_root
);
820 free(fs_info
->dev_root
);
821 free(fs_info
->csum_root
);
822 free(fs_info
->quota_root
);
823 free(fs_info
->free_space_root
);
824 free(fs_info
->super_copy
);
825 free(fs_info
->log_root_tree
);
829 struct btrfs_fs_info
*btrfs_new_fs_info(int writable
, u64 sb_bytenr
)
831 struct btrfs_fs_info
*fs_info
;
833 fs_info
= calloc(1, sizeof(struct btrfs_fs_info
));
837 fs_info
->tree_root
= calloc(1, sizeof(struct btrfs_root
));
838 fs_info
->extent_root
= calloc(1, sizeof(struct btrfs_root
));
839 fs_info
->chunk_root
= calloc(1, sizeof(struct btrfs_root
));
840 fs_info
->dev_root
= calloc(1, sizeof(struct btrfs_root
));
841 fs_info
->csum_root
= calloc(1, sizeof(struct btrfs_root
));
842 fs_info
->quota_root
= calloc(1, sizeof(struct btrfs_root
));
843 fs_info
->free_space_root
= calloc(1, sizeof(struct btrfs_root
));
844 fs_info
->super_copy
= calloc(1, BTRFS_SUPER_INFO_SIZE
);
846 if (!fs_info
->tree_root
|| !fs_info
->extent_root
||
847 !fs_info
->chunk_root
|| !fs_info
->dev_root
||
848 !fs_info
->csum_root
|| !fs_info
->quota_root
||
849 !fs_info
->free_space_root
|| !fs_info
->super_copy
)
852 extent_io_tree_init(&fs_info
->extent_cache
);
853 extent_io_tree_init(&fs_info
->free_space_cache
);
854 extent_io_tree_init(&fs_info
->block_group_cache
);
855 extent_io_tree_init(&fs_info
->pinned_extents
);
856 extent_io_tree_init(&fs_info
->pending_del
);
857 extent_io_tree_init(&fs_info
->extent_ins
);
858 fs_info
->excluded_extents
= NULL
;
860 fs_info
->fs_root_tree
= RB_ROOT
;
861 cache_tree_init(&fs_info
->mapping_tree
.cache_tree
);
863 mutex_init(&fs_info
->fs_mutex
);
864 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
865 INIT_LIST_HEAD(&fs_info
->space_info
);
866 INIT_LIST_HEAD(&fs_info
->recow_ebs
);
869 fs_info
->readonly
= 1;
871 fs_info
->super_bytenr
= sb_bytenr
;
872 fs_info
->data_alloc_profile
= (u64
)-1;
873 fs_info
->metadata_alloc_profile
= (u64
)-1;
874 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
877 btrfs_free_fs_info(fs_info
);
881 int btrfs_check_fs_compatibility(struct btrfs_super_block
*sb
, int writable
)
885 features
= btrfs_super_incompat_flags(sb
) &
886 ~BTRFS_FEATURE_INCOMPAT_SUPP
;
888 printk("couldn't open because of unsupported "
889 "option features (%Lx).\n",
890 (unsigned long long)features
);
894 features
= btrfs_super_incompat_flags(sb
);
895 if (!(features
& BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF
)) {
896 features
|= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF
;
897 btrfs_set_super_incompat_flags(sb
, features
);
900 features
= btrfs_super_compat_ro_flags(sb
) &
901 ~BTRFS_FEATURE_COMPAT_RO_SUPP
;
902 if (writable
&& features
) {
903 printk("couldn't open RDWR because of unsupported "
904 "option features (%Lx).\n",
905 (unsigned long long)features
);
911 static int find_best_backup_root(struct btrfs_super_block
*super
)
913 struct btrfs_root_backup
*backup
;
914 u64 orig_gen
= btrfs_super_generation(super
);
919 for (i
= 0; i
< BTRFS_NUM_BACKUP_ROOTS
; i
++) {
920 backup
= super
->super_roots
+ i
;
921 if (btrfs_backup_tree_root_gen(backup
) != orig_gen
&&
922 btrfs_backup_tree_root_gen(backup
) > gen
) {
924 gen
= btrfs_backup_tree_root_gen(backup
);
930 static int setup_root_or_create_block(struct btrfs_fs_info
*fs_info
,
931 enum btrfs_open_ctree_flags flags
,
932 struct btrfs_root
*info_root
,
933 u64 objectid
, char *str
)
935 struct btrfs_super_block
*sb
= fs_info
->super_copy
;
936 struct btrfs_root
*root
= fs_info
->tree_root
;
937 u32 leafsize
= btrfs_super_leafsize(sb
);
940 ret
= find_and_setup_root(root
, fs_info
, objectid
, info_root
);
942 printk("Couldn't setup %s tree\n", str
);
943 if (!(flags
& OPEN_CTREE_PARTIAL
))
946 * Need a blank node here just so we don't screw up in the
947 * million of places that assume a root has a valid ->node
950 btrfs_find_create_tree_block(fs_info
, 0, leafsize
);
951 if (!info_root
->node
)
953 clear_extent_buffer_uptodate(NULL
, info_root
->node
);
959 int btrfs_setup_all_roots(struct btrfs_fs_info
*fs_info
, u64 root_tree_bytenr
,
960 enum btrfs_open_ctree_flags flags
)
962 struct btrfs_super_block
*sb
= fs_info
->super_copy
;
963 struct btrfs_root
*root
;
964 struct btrfs_key key
;
973 nodesize
= btrfs_super_nodesize(sb
);
974 leafsize
= btrfs_super_leafsize(sb
);
975 sectorsize
= btrfs_super_sectorsize(sb
);
976 stripesize
= btrfs_super_stripesize(sb
);
978 root
= fs_info
->tree_root
;
979 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
980 root
, fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
981 blocksize
= btrfs_level_size(root
, btrfs_super_root_level(sb
));
982 generation
= btrfs_super_generation(sb
);
984 if (!root_tree_bytenr
&& !(flags
& OPEN_CTREE_BACKUP_ROOT
)) {
985 root_tree_bytenr
= btrfs_super_root(sb
);
986 } else if (flags
& OPEN_CTREE_BACKUP_ROOT
) {
987 struct btrfs_root_backup
*backup
;
988 int index
= find_best_backup_root(sb
);
989 if (index
>= BTRFS_NUM_BACKUP_ROOTS
) {
990 fprintf(stderr
, "Invalid backup root number\n");
993 backup
= fs_info
->super_copy
->super_roots
+ index
;
994 root_tree_bytenr
= btrfs_backup_tree_root(backup
);
995 generation
= btrfs_backup_tree_root_gen(backup
);
998 root
->node
= read_tree_block(root
, root_tree_bytenr
, blocksize
,
1000 if (!extent_buffer_uptodate(root
->node
)) {
1001 fprintf(stderr
, "Couldn't read tree root\n");
1005 ret
= setup_root_or_create_block(fs_info
, flags
, fs_info
->extent_root
,
1006 BTRFS_EXTENT_TREE_OBJECTID
, "extent");
1009 fs_info
->extent_root
->track_dirty
= 1;
1011 ret
= find_and_setup_root(root
, fs_info
, BTRFS_DEV_TREE_OBJECTID
,
1014 printk("Couldn't setup device tree\n");
1017 fs_info
->dev_root
->track_dirty
= 1;
1019 ret
= setup_root_or_create_block(fs_info
, flags
, fs_info
->csum_root
,
1020 BTRFS_CSUM_TREE_OBJECTID
, "csum");
1023 fs_info
->csum_root
->track_dirty
= 1;
1025 ret
= find_and_setup_root(root
, fs_info
, BTRFS_QUOTA_TREE_OBJECTID
,
1026 fs_info
->quota_root
);
1028 fs_info
->quota_enabled
= 1;
1030 if (btrfs_fs_compat_ro(fs_info
, BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE
)) {
1031 ret
= find_and_setup_root(root
, fs_info
, BTRFS_FREE_SPACE_TREE_OBJECTID
,
1032 fs_info
->free_space_root
);
1034 printk("Couldn't read free space tree\n");
1037 fs_info
->free_space_root
->track_dirty
= 1;
1040 ret
= find_and_setup_log_root(root
, fs_info
, sb
);
1042 printk("Couldn't setup log root tree\n");
1043 if (!(flags
& OPEN_CTREE_PARTIAL
))
1047 fs_info
->generation
= generation
;
1048 fs_info
->last_trans_committed
= generation
;
1049 if (extent_buffer_uptodate(fs_info
->extent_root
->node
) &&
1050 !(flags
& OPEN_CTREE_NO_BLOCK_GROUPS
))
1051 btrfs_read_block_groups(fs_info
->tree_root
);
1053 key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
1054 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1055 key
.offset
= (u64
)-1;
1056 fs_info
->fs_root
= btrfs_read_fs_root(fs_info
, &key
);
1058 if (IS_ERR(fs_info
->fs_root
))
1063 void btrfs_release_all_roots(struct btrfs_fs_info
*fs_info
)
1065 if (fs_info
->free_space_root
)
1066 free_extent_buffer(fs_info
->free_space_root
->node
);
1067 if (fs_info
->quota_root
)
1068 free_extent_buffer(fs_info
->quota_root
->node
);
1069 if (fs_info
->csum_root
)
1070 free_extent_buffer(fs_info
->csum_root
->node
);
1071 if (fs_info
->dev_root
)
1072 free_extent_buffer(fs_info
->dev_root
->node
);
1073 if (fs_info
->extent_root
)
1074 free_extent_buffer(fs_info
->extent_root
->node
);
1075 if (fs_info
->tree_root
)
1076 free_extent_buffer(fs_info
->tree_root
->node
);
1077 if (fs_info
->log_root_tree
)
1078 free_extent_buffer(fs_info
->log_root_tree
->node
);
1079 if (fs_info
->chunk_root
)
1080 free_extent_buffer(fs_info
->chunk_root
->node
);
1083 static void free_map_lookup(struct cache_extent
*ce
)
1085 struct map_lookup
*map
;
1087 map
= container_of(ce
, struct map_lookup
, ce
);
1091 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache
, free_map_lookup
);
1093 void btrfs_cleanup_all_caches(struct btrfs_fs_info
*fs_info
)
1095 while (!list_empty(&fs_info
->recow_ebs
)) {
1096 struct extent_buffer
*eb
;
1097 eb
= list_first_entry(&fs_info
->recow_ebs
,
1098 struct extent_buffer
, recow
);
1099 list_del_init(&eb
->recow
);
1100 free_extent_buffer(eb
);
1102 free_mapping_cache_tree(&fs_info
->mapping_tree
.cache_tree
);
1103 extent_io_tree_cleanup(&fs_info
->extent_cache
);
1104 extent_io_tree_cleanup(&fs_info
->free_space_cache
);
1105 extent_io_tree_cleanup(&fs_info
->block_group_cache
);
1106 extent_io_tree_cleanup(&fs_info
->pinned_extents
);
1107 extent_io_tree_cleanup(&fs_info
->pending_del
);
1108 extent_io_tree_cleanup(&fs_info
->extent_ins
);
1111 int btrfs_scan_fs_devices(int fd
, const char *path
,
1112 struct btrfs_fs_devices
**fs_devices
,
1113 u64 sb_bytenr
, int super_recover
,
1121 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
1123 seek_ret
= lseek(fd
, 0, SEEK_END
);
1127 dev_size
= seek_ret
;
1128 lseek(fd
, 0, SEEK_SET
);
1129 if (sb_bytenr
> dev_size
) {
1130 fprintf(stderr
, "Superblock bytenr is larger than device size\n");
1134 ret
= btrfs_scan_one_device(fd
, path
, fs_devices
,
1135 &total_devs
, sb_bytenr
, super_recover
);
1137 fprintf(stderr
, "No valid Btrfs found on %s\n", path
);
1141 if (!skip_devices
&& total_devs
!= 1) {
1142 ret
= btrfs_scan_lblkid();
1149 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info
*fs_info
,
1150 u64 chunk_root_bytenr
)
1152 struct btrfs_super_block
*sb
= fs_info
->super_copy
;
1161 nodesize
= btrfs_super_nodesize(sb
);
1162 leafsize
= btrfs_super_leafsize(sb
);
1163 sectorsize
= btrfs_super_sectorsize(sb
);
1164 stripesize
= btrfs_super_stripesize(sb
);
1166 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
1167 fs_info
->chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
1169 ret
= btrfs_read_sys_array(fs_info
->chunk_root
);
1173 blocksize
= btrfs_level_size(fs_info
->chunk_root
,
1174 btrfs_super_chunk_root_level(sb
));
1175 generation
= btrfs_super_chunk_root_generation(sb
);
1177 if (chunk_root_bytenr
&& !IS_ALIGNED(chunk_root_bytenr
,
1178 btrfs_super_sectorsize(sb
))) {
1179 warning("chunk_root_bytenr %llu is unaligned to %u, ignore it",
1180 chunk_root_bytenr
, btrfs_super_sectorsize(sb
));
1181 chunk_root_bytenr
= 0;
1184 if (!chunk_root_bytenr
)
1185 chunk_root_bytenr
= btrfs_super_chunk_root(sb
);
1189 fs_info
->chunk_root
->node
= read_tree_block(fs_info
->chunk_root
,
1191 blocksize
, generation
);
1192 if (!extent_buffer_uptodate(fs_info
->chunk_root
->node
)) {
1193 if (fs_info
->ignore_chunk_tree_error
) {
1194 warning("cannot read chunk root, continue anyway");
1195 fs_info
->chunk_root
= NULL
;
1198 error("cannot read chunk root");
1203 if (!(btrfs_super_flags(sb
) & BTRFS_SUPER_FLAG_METADUMP
)) {
1204 ret
= btrfs_read_chunk_tree(fs_info
->chunk_root
);
1206 fprintf(stderr
, "Couldn't read chunk tree\n");
1213 static struct btrfs_fs_info
*__open_ctree_fd(int fp
, const char *path
,
1215 u64 root_tree_bytenr
,
1216 u64 chunk_root_bytenr
,
1217 enum btrfs_open_ctree_flags flags
)
1219 struct btrfs_fs_info
*fs_info
;
1220 struct btrfs_super_block
*disk_super
;
1221 struct btrfs_fs_devices
*fs_devices
= NULL
;
1222 struct extent_buffer
*eb
;
1227 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
1229 /* try to drop all the caches */
1230 if (posix_fadvise(fp
, 0, 0, POSIX_FADV_DONTNEED
))
1231 fprintf(stderr
, "Warning, could not drop caches\n");
1233 fs_info
= btrfs_new_fs_info(flags
& OPEN_CTREE_WRITES
, sb_bytenr
);
1235 fprintf(stderr
, "Failed to allocate memory for fs_info\n");
1238 if (flags
& OPEN_CTREE_RESTORE
)
1239 fs_info
->on_restoring
= 1;
1240 if (flags
& OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS
)
1241 fs_info
->suppress_check_block_errors
= 1;
1242 if (flags
& OPEN_CTREE_IGNORE_FSID_MISMATCH
)
1243 fs_info
->ignore_fsid_mismatch
= 1;
1244 if (flags
& OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR
)
1245 fs_info
->ignore_chunk_tree_error
= 1;
1247 ret
= btrfs_scan_fs_devices(fp
, path
, &fs_devices
, sb_bytenr
,
1248 (flags
& OPEN_CTREE_RECOVER_SUPER
),
1249 (flags
& OPEN_CTREE_NO_DEVICES
));
1253 fs_info
->fs_devices
= fs_devices
;
1254 if (flags
& OPEN_CTREE_WRITES
)
1259 if (flags
& OPEN_CTREE_EXCLUSIVE
)
1262 ret
= btrfs_open_devices(fs_devices
, oflags
);
1266 disk_super
= fs_info
->super_copy
;
1267 if (!(flags
& OPEN_CTREE_RECOVER_SUPER
))
1268 ret
= btrfs_read_dev_super(fs_devices
->latest_bdev
,
1269 disk_super
, sb_bytenr
, 1);
1271 ret
= btrfs_read_dev_super(fp
, disk_super
, sb_bytenr
, 0);
1273 printk("No valid btrfs found\n");
1277 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_CHANGING_FSID
&&
1278 !fs_info
->ignore_fsid_mismatch
) {
1279 fprintf(stderr
, "ERROR: Filesystem UUID change in progress\n");
1283 memcpy(fs_info
->fsid
, &disk_super
->fsid
, BTRFS_FSID_SIZE
);
1285 ret
= btrfs_check_fs_compatibility(fs_info
->super_copy
,
1286 flags
& OPEN_CTREE_WRITES
);
1290 ret
= btrfs_setup_chunk_tree_and_device_map(fs_info
, chunk_root_bytenr
);
1294 /* Chunk tree root is unable to read, return directly */
1295 if (!fs_info
->chunk_root
)
1298 eb
= fs_info
->chunk_root
->node
;
1299 read_extent_buffer(eb
, fs_info
->chunk_tree_uuid
,
1300 btrfs_header_chunk_tree_uuid(eb
),
1303 ret
= btrfs_setup_all_roots(fs_info
, root_tree_bytenr
, flags
);
1304 if (ret
&& !(flags
& __OPEN_CTREE_RETURN_CHUNK_ROOT
) &&
1305 !fs_info
->ignore_chunk_tree_error
)
1311 btrfs_release_all_roots(fs_info
);
1312 btrfs_cleanup_all_caches(fs_info
);
1314 btrfs_close_devices(fs_devices
);
1316 btrfs_free_fs_info(fs_info
);
1320 struct btrfs_fs_info
*open_ctree_fs_info(const char *filename
,
1321 u64 sb_bytenr
, u64 root_tree_bytenr
,
1322 u64 chunk_root_bytenr
,
1323 enum btrfs_open_ctree_flags flags
)
1326 struct btrfs_fs_info
*info
;
1327 int oflags
= O_CREAT
| O_RDWR
;
1329 if (!(flags
& OPEN_CTREE_WRITES
))
1332 fp
= open(filename
, oflags
, 0600);
1334 fprintf (stderr
, "Could not open %s\n", filename
);
1337 info
= __open_ctree_fd(fp
, filename
, sb_bytenr
, root_tree_bytenr
,
1338 chunk_root_bytenr
, flags
);
1343 struct btrfs_root
*open_ctree(const char *filename
, u64 sb_bytenr
,
1344 enum btrfs_open_ctree_flags flags
)
1346 struct btrfs_fs_info
*info
;
1348 /* This flags may not return fs_info with any valid root */
1349 BUG_ON(flags
& OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR
);
1350 info
= open_ctree_fs_info(filename
, sb_bytenr
, 0, 0, flags
);
1353 if (flags
& __OPEN_CTREE_RETURN_CHUNK_ROOT
)
1354 return info
->chunk_root
;
1355 return info
->fs_root
;
1358 struct btrfs_root
*open_ctree_fd(int fp
, const char *path
, u64 sb_bytenr
,
1359 enum btrfs_open_ctree_flags flags
)
1361 struct btrfs_fs_info
*info
;
1363 /* This flags may not return fs_info with any valid root */
1364 BUG_ON(flags
& OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR
);
1365 info
= __open_ctree_fd(fp
, path
, sb_bytenr
, 0, 0, flags
);
1368 if (flags
& __OPEN_CTREE_RETURN_CHUNK_ROOT
)
1369 return info
->chunk_root
;
1370 return info
->fs_root
;
1374 * Check if the super is valid:
1375 * - nodesize/sectorsize - minimum, maximum, alignment
1376 * - tree block starts - alignment
1377 * - number of devices - something sane
1378 * - sys array size - maximum
1380 static int check_super(struct btrfs_super_block
*sb
)
1382 char result
[BTRFS_CSUM_SIZE
];
1387 if (btrfs_super_magic(sb
) != BTRFS_MAGIC
) {
1388 fprintf(stderr
, "ERROR: superblock magic doesn't match\n");
1392 csum_type
= btrfs_super_csum_type(sb
);
1393 if (csum_type
>= ARRAY_SIZE(btrfs_csum_sizes
)) {
1394 fprintf(stderr
, "ERROR: unsupported checksum algorithm %u\n",
1398 csum_size
= btrfs_csum_sizes
[csum_type
];
1401 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
1402 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
1403 btrfs_csum_final(crc
, result
);
1405 if (memcmp(result
, sb
->csum
, csum_size
)) {
1406 fprintf(stderr
, "ERROR: superblock checksum mismatch\n");
1409 if (btrfs_super_root_level(sb
) >= BTRFS_MAX_LEVEL
) {
1410 fprintf(stderr
, "ERROR: tree_root level too big: %d >= %d\n",
1411 btrfs_super_root_level(sb
), BTRFS_MAX_LEVEL
);
1414 if (btrfs_super_chunk_root_level(sb
) >= BTRFS_MAX_LEVEL
) {
1415 fprintf(stderr
, "ERROR: chunk_root level too big: %d >= %d\n",
1416 btrfs_super_chunk_root_level(sb
), BTRFS_MAX_LEVEL
);
1419 if (btrfs_super_log_root_level(sb
) >= BTRFS_MAX_LEVEL
) {
1420 fprintf(stderr
, "ERROR: log_root level too big: %d >= %d\n",
1421 btrfs_super_log_root_level(sb
), BTRFS_MAX_LEVEL
);
1425 if (!IS_ALIGNED(btrfs_super_root(sb
), 4096)) {
1426 fprintf(stderr
, "ERROR: tree_root block unaligned: %llu\n",
1427 btrfs_super_root(sb
));
1430 if (!IS_ALIGNED(btrfs_super_chunk_root(sb
), 4096)) {
1431 fprintf(stderr
, "ERROR: chunk_root block unaligned: %llu\n",
1432 btrfs_super_chunk_root(sb
));
1435 if (!IS_ALIGNED(btrfs_super_log_root(sb
), 4096)) {
1436 fprintf(stderr
, "ERROR: log_root block unaligned: %llu\n",
1437 btrfs_super_log_root(sb
));
1440 if (btrfs_super_nodesize(sb
) < 4096) {
1441 fprintf(stderr
, "ERROR: nodesize too small: %u < 4096\n",
1442 btrfs_super_nodesize(sb
));
1445 if (!IS_ALIGNED(btrfs_super_nodesize(sb
), 4096)) {
1446 fprintf(stderr
, "ERROR: nodesize unaligned: %u\n",
1447 btrfs_super_nodesize(sb
));
1450 if (btrfs_super_sectorsize(sb
) < 4096) {
1451 fprintf(stderr
, "ERROR: sectorsize too small: %u < 4096\n",
1452 btrfs_super_sectorsize(sb
));
1455 if (!IS_ALIGNED(btrfs_super_sectorsize(sb
), 4096)) {
1456 fprintf(stderr
, "ERROR: sectorsize unaligned: %u\n",
1457 btrfs_super_sectorsize(sb
));
1461 if (memcmp(sb
->fsid
, sb
->dev_item
.fsid
, BTRFS_UUID_SIZE
) != 0) {
1462 char fsid
[BTRFS_UUID_UNPARSED_SIZE
];
1463 char dev_fsid
[BTRFS_UUID_UNPARSED_SIZE
];
1465 uuid_unparse(sb
->fsid
, fsid
);
1466 uuid_unparse(sb
->dev_item
.fsid
, dev_fsid
);
1468 "ERROR: dev_item UUID does not match fsid: %s != %s\n",
1474 * Hint to catch really bogus numbers, bitflips or so
1476 if (btrfs_super_num_devices(sb
) > (1UL << 31)) {
1477 fprintf(stderr
, "WARNING: suspicious number of devices: %llu\n",
1478 btrfs_super_num_devices(sb
));
1481 if (btrfs_super_num_devices(sb
) == 0) {
1482 fprintf(stderr
, "ERROR: number of devices is 0\n");
1487 * Obvious sys_chunk_array corruptions, it must hold at least one key
1490 if (btrfs_super_sys_array_size(sb
) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) {
1491 fprintf(stderr
, "BTRFS: system chunk array too big %u > %u\n",
1492 btrfs_super_sys_array_size(sb
),
1493 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
);
1496 if (btrfs_super_sys_array_size(sb
) < sizeof(struct btrfs_disk_key
)
1497 + sizeof(struct btrfs_chunk
)) {
1498 fprintf(stderr
, "BTRFS: system chunk array too small %u < %lu\n",
1499 btrfs_super_sys_array_size(sb
),
1500 sizeof(struct btrfs_disk_key
) +
1501 sizeof(struct btrfs_chunk
));
1508 int btrfs_read_dev_super(int fd
, struct btrfs_super_block
*sb
, u64 sb_bytenr
,
1511 u8 fsid
[BTRFS_FSID_SIZE
];
1512 int fsid_is_initialized
= 0;
1513 char tmp
[BTRFS_SUPER_INFO_SIZE
];
1514 struct btrfs_super_block
*buf
= (struct btrfs_super_block
*)tmp
;
1517 int max_super
= super_recover
? BTRFS_SUPER_MIRROR_MAX
: 1;
1521 if (sb_bytenr
!= BTRFS_SUPER_INFO_OFFSET
) {
1522 ret
= pread64(fd
, buf
, BTRFS_SUPER_INFO_SIZE
, sb_bytenr
);
1523 if (ret
< BTRFS_SUPER_INFO_SIZE
)
1526 if (btrfs_super_bytenr(buf
) != sb_bytenr
)
1529 if (check_super(buf
))
1531 memcpy(sb
, buf
, BTRFS_SUPER_INFO_SIZE
);
1536 * we would like to check all the supers, but that would make
1537 * a btrfs mount succeed after a mkfs from a different FS.
1538 * So, we need to add a special mount option to scan for
1539 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1542 for (i
= 0; i
< max_super
; i
++) {
1543 bytenr
= btrfs_sb_offset(i
);
1544 ret
= pread64(fd
, buf
, BTRFS_SUPER_INFO_SIZE
, bytenr
);
1545 if (ret
< BTRFS_SUPER_INFO_SIZE
)
1548 if (btrfs_super_bytenr(buf
) != bytenr
)
1550 /* if magic is NULL, the device was removed */
1551 if (btrfs_super_magic(buf
) == 0 && i
== 0)
1553 if (check_super(buf
))
1556 if (!fsid_is_initialized
) {
1557 memcpy(fsid
, buf
->fsid
, sizeof(fsid
));
1558 fsid_is_initialized
= 1;
1559 } else if (memcmp(fsid
, buf
->fsid
, sizeof(fsid
))) {
1561 * the superblocks (the original one and
1562 * its backups) contain data of different
1563 * filesystems -> the super cannot be trusted
1568 if (btrfs_super_generation(buf
) > transid
) {
1569 memcpy(sb
, buf
, BTRFS_SUPER_INFO_SIZE
);
1570 transid
= btrfs_super_generation(buf
);
1574 return transid
> 0 ? 0 : -1;
1577 static int write_dev_supers(struct btrfs_root
*root
,
1578 struct btrfs_super_block
*sb
,
1579 struct btrfs_device
*device
)
1585 if (root
->fs_info
->super_bytenr
!= BTRFS_SUPER_INFO_OFFSET
) {
1586 btrfs_set_super_bytenr(sb
, root
->fs_info
->super_bytenr
);
1588 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
1589 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
1590 btrfs_csum_final(crc
, (char *)&sb
->csum
[0]);
1593 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1594 * zero filled, we can use it directly
1596 ret
= pwrite64(device
->fd
, root
->fs_info
->super_copy
,
1597 BTRFS_SUPER_INFO_SIZE
,
1598 root
->fs_info
->super_bytenr
);
1599 if (ret
!= BTRFS_SUPER_INFO_SIZE
)
1604 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1605 bytenr
= btrfs_sb_offset(i
);
1606 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
> device
->total_bytes
)
1609 btrfs_set_super_bytenr(sb
, bytenr
);
1612 crc
= btrfs_csum_data(NULL
, (char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
1613 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
1614 btrfs_csum_final(crc
, (char *)&sb
->csum
[0]);
1617 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1618 * zero filled, we can use it directly
1620 ret
= pwrite64(device
->fd
, root
->fs_info
->super_copy
,
1621 BTRFS_SUPER_INFO_SIZE
, bytenr
);
1622 if (ret
!= BTRFS_SUPER_INFO_SIZE
)
1630 fprintf(stderr
, "WARNING: failed to write all sb data\n");
1632 fprintf(stderr
, "WARNING: failed to write sb: %s\n",
1637 int write_all_supers(struct btrfs_root
*root
)
1639 struct list_head
*cur
;
1640 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
1641 struct btrfs_device
*dev
;
1642 struct btrfs_super_block
*sb
;
1643 struct btrfs_dev_item
*dev_item
;
1647 sb
= root
->fs_info
->super_copy
;
1648 dev_item
= &sb
->dev_item
;
1649 list_for_each(cur
, head
) {
1650 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
1651 if (!dev
->writeable
)
1654 btrfs_set_stack_device_generation(dev_item
, 0);
1655 btrfs_set_stack_device_type(dev_item
, dev
->type
);
1656 btrfs_set_stack_device_id(dev_item
, dev
->devid
);
1657 btrfs_set_stack_device_total_bytes(dev_item
, dev
->total_bytes
);
1658 btrfs_set_stack_device_bytes_used(dev_item
, dev
->bytes_used
);
1659 btrfs_set_stack_device_io_align(dev_item
, dev
->io_align
);
1660 btrfs_set_stack_device_io_width(dev_item
, dev
->io_width
);
1661 btrfs_set_stack_device_sector_size(dev_item
, dev
->sector_size
);
1662 memcpy(dev_item
->uuid
, dev
->uuid
, BTRFS_UUID_SIZE
);
1663 memcpy(dev_item
->fsid
, dev
->fs_devices
->fsid
, BTRFS_UUID_SIZE
);
1665 flags
= btrfs_super_flags(sb
);
1666 btrfs_set_super_flags(sb
, flags
| BTRFS_HEADER_FLAG_WRITTEN
);
1668 ret
= write_dev_supers(root
, sb
, dev
);
1674 int write_ctree_super(struct btrfs_trans_handle
*trans
,
1675 struct btrfs_root
*root
)
1678 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
1679 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
1681 if (root
->fs_info
->readonly
)
1684 btrfs_set_super_generation(root
->fs_info
->super_copy
,
1686 btrfs_set_super_root(root
->fs_info
->super_copy
,
1687 tree_root
->node
->start
);
1688 btrfs_set_super_root_level(root
->fs_info
->super_copy
,
1689 btrfs_header_level(tree_root
->node
));
1690 btrfs_set_super_chunk_root(root
->fs_info
->super_copy
,
1691 chunk_root
->node
->start
);
1692 btrfs_set_super_chunk_root_level(root
->fs_info
->super_copy
,
1693 btrfs_header_level(chunk_root
->node
));
1694 btrfs_set_super_chunk_root_generation(root
->fs_info
->super_copy
,
1695 btrfs_header_generation(chunk_root
->node
));
1697 ret
= write_all_supers(root
);
1699 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
1703 int close_ctree_fs_info(struct btrfs_fs_info
*fs_info
)
1706 struct btrfs_trans_handle
*trans
;
1707 struct btrfs_root
*root
= fs_info
->tree_root
;
1709 if (fs_info
->last_trans_committed
!=
1710 fs_info
->generation
) {
1712 trans
= btrfs_start_transaction(root
, 1);
1713 btrfs_commit_transaction(trans
, root
);
1714 trans
= btrfs_start_transaction(root
, 1);
1715 ret
= commit_tree_roots(trans
, fs_info
);
1717 ret
= __commit_transaction(trans
, root
);
1719 write_ctree_super(trans
, root
);
1720 btrfs_free_transaction(root
, trans
);
1722 btrfs_free_block_groups(fs_info
);
1724 free_fs_roots_tree(&fs_info
->fs_root_tree
);
1726 btrfs_release_all_roots(fs_info
);
1727 btrfs_close_devices(fs_info
->fs_devices
);
1728 btrfs_cleanup_all_caches(fs_info
);
1729 btrfs_free_fs_info(fs_info
);
1733 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1734 struct extent_buffer
*eb
)
1736 return clear_extent_buffer_dirty(eb
);
1739 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
1740 struct extent_buffer
*eb
)
1745 void btrfs_mark_buffer_dirty(struct extent_buffer
*eb
)
1747 set_extent_buffer_dirty(eb
);
1750 int btrfs_buffer_uptodate(struct extent_buffer
*buf
, u64 parent_transid
)
1754 ret
= extent_buffer_uptodate(buf
);
1758 ret
= verify_parent_transid(buf
->tree
, buf
, parent_transid
, 1);
1762 int btrfs_set_buffer_uptodate(struct extent_buffer
*eb
)
1764 return set_extent_buffer_uptodate(eb
);