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
= fs_info
->nodesize
;
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(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 int csum_tree_block(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 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_fs_info
*fs_info
,
178 u64 bytenr
, u32 blocksize
)
180 return find_extent_buffer(&fs_info
->extent_cache
,
184 struct extent_buffer
* btrfs_find_create_tree_block(
185 struct btrfs_fs_info
*fs_info
, u64 bytenr
, u32 blocksize
)
187 return alloc_extent_buffer(&fs_info
->extent_cache
, bytenr
, blocksize
);
190 void readahead_tree_block(struct btrfs_fs_info
*fs_info
, u64 bytenr
,
191 u32 blocksize
, u64 parent_transid
)
193 struct extent_buffer
*eb
;
195 struct btrfs_multi_bio
*multi
= NULL
;
196 struct btrfs_device
*device
;
198 eb
= btrfs_find_tree_block(fs_info
, bytenr
, blocksize
);
199 if (!(eb
&& btrfs_buffer_uptodate(eb
, parent_transid
)) &&
200 !btrfs_map_block(fs_info
, READ
, bytenr
, &length
, &multi
, 0,
202 device
= multi
->stripes
[0].dev
;
204 blocksize
= min(blocksize
, (u32
)SZ_64K
);
205 readahead(device
->fd
, multi
->stripes
[0].physical
, blocksize
);
208 free_extent_buffer(eb
);
212 static int verify_parent_transid(struct extent_io_tree
*io_tree
,
213 struct extent_buffer
*eb
, u64 parent_transid
,
218 if (!parent_transid
|| btrfs_header_generation(eb
) == parent_transid
)
221 if (extent_buffer_uptodate(eb
) &&
222 btrfs_header_generation(eb
) == parent_transid
) {
226 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
227 (unsigned long long)eb
->start
,
228 (unsigned long long)parent_transid
,
229 (unsigned long long)btrfs_header_generation(eb
));
231 eb
->flags
|= EXTENT_BAD_TRANSID
;
232 printk("Ignoring transid failure\n");
238 clear_extent_buffer_uptodate(eb
);
244 int read_whole_eb(struct btrfs_fs_info
*info
, struct extent_buffer
*eb
, int mirror
)
246 unsigned long offset
= 0;
247 struct btrfs_multi_bio
*multi
= NULL
;
248 struct btrfs_device
*device
;
251 unsigned long bytes_left
= eb
->len
;
254 read_len
= bytes_left
;
257 if (!info
->on_restoring
&&
258 eb
->start
!= BTRFS_SUPER_INFO_OFFSET
) {
259 ret
= btrfs_map_block(info
, READ
, eb
->start
+ offset
,
260 &read_len
, &multi
, mirror
, NULL
);
262 printk("Couldn't map the block %Lu\n", eb
->start
+ offset
);
266 device
= multi
->stripes
[0].dev
;
268 if (device
->fd
<= 0) {
275 eb
->dev_bytenr
= multi
->stripes
[0].physical
;
279 /* special case for restore metadump */
280 list_for_each_entry(device
, &info
->fs_devices
->devices
, dev_list
) {
281 if (device
->devid
== 1)
286 eb
->dev_bytenr
= eb
->start
;
290 if (read_len
> bytes_left
)
291 read_len
= bytes_left
;
293 ret
= read_extent_from_disk(eb
, offset
, read_len
);
297 bytes_left
-= read_len
;
302 struct extent_buffer
* read_tree_block(
303 struct btrfs_fs_info
*fs_info
, u64 bytenr
, u32 blocksize
,
307 struct extent_buffer
*eb
;
308 u64 best_transid
= 0;
309 u32 sectorsize
= fs_info
->sectorsize
;
310 u32 nodesize
= fs_info
->nodesize
;
317 * Don't even try to create tree block for unaligned tree block
319 * Such unaligned tree block will free overlapping extent buffer,
320 * causing use-after-free bugs for fuzzed images.
322 if (bytenr
< sectorsize
|| !IS_ALIGNED(bytenr
, sectorsize
)) {
323 error("tree block bytenr %llu is not aligned to sectorsize %u",
325 return ERR_PTR(-EIO
);
327 if (blocksize
< nodesize
|| !IS_ALIGNED(blocksize
, nodesize
)) {
328 error("tree block size %u is not aligned to nodesize %u",
329 blocksize
, nodesize
);
330 return ERR_PTR(-EIO
);
333 eb
= btrfs_find_create_tree_block(fs_info
, bytenr
, blocksize
);
335 return ERR_PTR(-ENOMEM
);
337 if (btrfs_buffer_uptodate(eb
, parent_transid
))
341 ret
= read_whole_eb(fs_info
, eb
, mirror_num
);
342 if (ret
== 0 && csum_tree_block(fs_info
, eb
, 1) == 0 &&
343 check_tree_block(fs_info
, eb
) == 0 &&
344 verify_parent_transid(eb
->tree
, eb
, parent_transid
, ignore
)
346 if (eb
->flags
& EXTENT_BAD_TRANSID
&&
347 list_empty(&eb
->recow
)) {
348 list_add_tail(&eb
->recow
,
349 &fs_info
->recow_ebs
);
352 btrfs_set_buffer_uptodate(eb
);
356 if (check_tree_block(fs_info
, eb
)) {
357 if (!fs_info
->suppress_check_block_errors
)
358 print_tree_block_error(fs_info
, eb
,
359 check_tree_block(fs_info
, eb
));
361 if (!fs_info
->suppress_check_block_errors
)
362 fprintf(stderr
, "Csum didn't match\n");
367 num_copies
= btrfs_num_copies(fs_info
, eb
->start
, eb
->len
);
368 if (num_copies
== 1) {
372 if (btrfs_header_generation(eb
) > best_transid
&& mirror_num
) {
373 best_transid
= btrfs_header_generation(eb
);
374 good_mirror
= mirror_num
;
377 if (mirror_num
> num_copies
) {
378 mirror_num
= good_mirror
;
383 free_extent_buffer(eb
);
387 int read_extent_data(struct btrfs_fs_info
*fs_info
, char *data
, u64 logical
,
388 u64
*len
, int mirror
)
391 struct btrfs_multi_bio
*multi
= NULL
;
392 struct btrfs_device
*device
;
396 ret
= btrfs_map_block(fs_info
, READ
, logical
, len
, &multi
, mirror
,
399 fprintf(stderr
, "Couldn't map the block %llu\n",
403 device
= multi
->stripes
[0].dev
;
410 ret
= pread64(device
->fd
, data
, *len
, multi
->stripes
[0].physical
);
420 int write_and_map_eb(struct btrfs_fs_info
*fs_info
, struct extent_buffer
*eb
)
425 u64
*raid_map
= NULL
;
426 struct btrfs_multi_bio
*multi
= NULL
;
430 ret
= btrfs_map_block(fs_info
, WRITE
, eb
->start
, &length
,
431 &multi
, 0, &raid_map
);
434 ret
= write_raid56_with_parity(fs_info
, eb
, multi
,
437 } else while (dev_nr
< multi
->num_stripes
) {
439 eb
->fd
= multi
->stripes
[dev_nr
].dev
->fd
;
440 eb
->dev_bytenr
= multi
->stripes
[dev_nr
].physical
;
441 multi
->stripes
[dev_nr
].dev
->total_ios
++;
443 ret
= write_extent_to_disk(eb
);
451 int write_tree_block(struct btrfs_trans_handle
*trans
,
452 struct btrfs_fs_info
*fs_info
,
453 struct extent_buffer
*eb
)
455 if (check_tree_block(fs_info
, eb
)) {
456 print_tree_block_error(fs_info
, eb
,
457 check_tree_block(fs_info
, eb
));
461 if (trans
&& !btrfs_buffer_uptodate(eb
, trans
->transid
))
464 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
465 csum_tree_block(fs_info
, eb
, 0);
467 return write_and_map_eb(fs_info
, eb
);
470 void btrfs_setup_root(struct btrfs_root
*root
, struct btrfs_fs_info
*fs_info
,
474 root
->commit_root
= NULL
;
476 root
->track_dirty
= 0;
478 root
->fs_info
= fs_info
;
479 root
->objectid
= objectid
;
480 root
->last_trans
= 0;
481 root
->last_inode_alloc
= 0;
483 INIT_LIST_HEAD(&root
->dirty_list
);
484 INIT_LIST_HEAD(&root
->orphan_data_extents
);
485 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
486 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
487 root
->root_key
.objectid
= objectid
;
490 static int update_cowonly_root(struct btrfs_trans_handle
*trans
,
491 struct btrfs_root
*root
)
495 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
497 btrfs_write_dirty_block_groups(trans
, root
);
499 old_root_bytenr
= btrfs_root_bytenr(&root
->root_item
);
500 if (old_root_bytenr
== root
->node
->start
)
502 btrfs_set_root_bytenr(&root
->root_item
,
504 btrfs_set_root_generation(&root
->root_item
,
506 root
->root_item
.level
= btrfs_header_level(root
->node
);
507 ret
= btrfs_update_root(trans
, tree_root
,
511 btrfs_write_dirty_block_groups(trans
, root
);
516 static int commit_tree_roots(struct btrfs_trans_handle
*trans
,
517 struct btrfs_fs_info
*fs_info
)
519 struct btrfs_root
*root
;
520 struct list_head
*next
;
521 struct extent_buffer
*eb
;
524 if (fs_info
->readonly
)
527 eb
= fs_info
->tree_root
->node
;
528 extent_buffer_get(eb
);
529 ret
= btrfs_cow_block(trans
, fs_info
->tree_root
, eb
, NULL
, 0, &eb
);
530 free_extent_buffer(eb
);
534 while(!list_empty(&fs_info
->dirty_cowonly_roots
)) {
535 next
= fs_info
->dirty_cowonly_roots
.next
;
537 root
= list_entry(next
, struct btrfs_root
, dirty_list
);
538 update_cowonly_root(trans
, root
);
539 free_extent_buffer(root
->commit_root
);
540 root
->commit_root
= NULL
;
546 static int __commit_transaction(struct btrfs_trans_handle
*trans
,
547 struct btrfs_root
*root
)
551 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
552 struct extent_buffer
*eb
;
553 struct extent_io_tree
*tree
= &fs_info
->extent_cache
;
557 ret
= find_first_extent_bit(tree
, 0, &start
, &end
,
561 while(start
<= end
) {
562 eb
= find_first_extent_buffer(tree
, start
);
563 BUG_ON(!eb
|| eb
->start
!= start
);
564 ret
= write_tree_block(trans
, fs_info
, eb
);
567 clear_extent_buffer_dirty(eb
);
568 free_extent_buffer(eb
);
574 int btrfs_commit_transaction(struct btrfs_trans_handle
*trans
,
575 struct btrfs_root
*root
)
577 u64 transid
= trans
->transid
;
579 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
581 if (root
->commit_root
== root
->node
)
583 if (root
== root
->fs_info
->tree_root
)
585 if (root
== root
->fs_info
->chunk_root
)
588 free_extent_buffer(root
->commit_root
);
589 root
->commit_root
= NULL
;
591 btrfs_set_root_bytenr(&root
->root_item
, root
->node
->start
);
592 btrfs_set_root_generation(&root
->root_item
, trans
->transid
);
593 root
->root_item
.level
= btrfs_header_level(root
->node
);
594 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
595 &root
->root_key
, &root
->root_item
);
598 ret
= commit_tree_roots(trans
, fs_info
);
600 ret
= __commit_transaction(trans
, root
);
602 write_ctree_super(trans
, fs_info
);
603 btrfs_finish_extent_commit(trans
, fs_info
->extent_root
,
604 &fs_info
->pinned_extents
);
606 free_extent_buffer(root
->commit_root
);
607 root
->commit_root
= NULL
;
608 fs_info
->running_transaction
= NULL
;
609 fs_info
->last_trans_committed
= transid
;
613 static int find_and_setup_root(struct btrfs_root
*tree_root
,
614 struct btrfs_fs_info
*fs_info
,
615 u64 objectid
, struct btrfs_root
*root
)
621 btrfs_setup_root(root
, fs_info
, objectid
);
622 ret
= btrfs_find_last_root(tree_root
, objectid
,
623 &root
->root_item
, &root
->root_key
);
627 blocksize
= fs_info
->nodesize
;
628 generation
= btrfs_root_generation(&root
->root_item
);
629 root
->node
= read_tree_block(fs_info
,
630 btrfs_root_bytenr(&root
->root_item
),
631 blocksize
, generation
);
632 if (!extent_buffer_uptodate(root
->node
))
638 static int find_and_setup_log_root(struct btrfs_root
*tree_root
,
639 struct btrfs_fs_info
*fs_info
,
640 struct btrfs_super_block
*disk_super
)
643 u64 blocknr
= btrfs_super_log_root(disk_super
);
644 struct btrfs_root
*log_root
= malloc(sizeof(struct btrfs_root
));
654 blocksize
= fs_info
->nodesize
;
656 btrfs_setup_root(log_root
, fs_info
,
657 BTRFS_TREE_LOG_OBJECTID
);
659 log_root
->node
= read_tree_block(fs_info
, 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
;
675 int btrfs_free_fs_root(struct btrfs_root
*root
)
678 free_extent_buffer(root
->node
);
679 if (root
->commit_root
)
680 free_extent_buffer(root
->commit_root
);
685 static void __free_fs_root(struct rb_node
*node
)
687 struct btrfs_root
*root
;
689 root
= container_of(node
, struct btrfs_root
, rb_node
);
690 btrfs_free_fs_root(root
);
693 FREE_RB_BASED_TREE(fs_roots
, __free_fs_root
);
695 struct btrfs_root
*btrfs_read_fs_root_no_cache(struct btrfs_fs_info
*fs_info
,
696 struct btrfs_key
*location
)
698 struct btrfs_root
*root
;
699 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
700 struct btrfs_path
*path
;
701 struct extent_buffer
*l
;
706 root
= calloc(1, sizeof(*root
));
708 return ERR_PTR(-ENOMEM
);
709 if (location
->offset
== (u64
)-1) {
710 ret
= find_and_setup_root(tree_root
, fs_info
,
711 location
->objectid
, root
);
719 btrfs_setup_root(root
, fs_info
,
722 path
= btrfs_alloc_path();
725 return ERR_PTR(-ENOMEM
);
728 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
735 read_extent_buffer(l
, &root
->root_item
,
736 btrfs_item_ptr_offset(l
, path
->slots
[0]),
737 sizeof(root
->root_item
));
738 memcpy(&root
->root_key
, location
, sizeof(*location
));
741 btrfs_free_path(path
);
746 generation
= btrfs_root_generation(&root
->root_item
);
747 blocksize
= fs_info
->nodesize
;
748 root
->node
= read_tree_block(fs_info
,
749 btrfs_root_bytenr(&root
->root_item
),
750 blocksize
, generation
);
751 if (!extent_buffer_uptodate(root
->node
)) {
753 return ERR_PTR(-EIO
);
760 static int btrfs_fs_roots_compare_objectids(struct rb_node
*node
,
763 u64 objectid
= *((u64
*)data
);
764 struct btrfs_root
*root
;
766 root
= rb_entry(node
, struct btrfs_root
, rb_node
);
767 if (objectid
> root
->objectid
)
769 else if (objectid
< root
->objectid
)
775 static int btrfs_fs_roots_compare_roots(struct rb_node
*node1
,
776 struct rb_node
*node2
)
778 struct btrfs_root
*root
;
780 root
= rb_entry(node2
, struct btrfs_root
, rb_node
);
781 return btrfs_fs_roots_compare_objectids(node1
, (void *)&root
->objectid
);
784 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
785 struct btrfs_key
*location
)
787 struct btrfs_root
*root
;
788 struct rb_node
*node
;
790 u64 objectid
= location
->objectid
;
792 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
793 return fs_info
->tree_root
;
794 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
795 return fs_info
->extent_root
;
796 if (location
->objectid
== BTRFS_CHUNK_TREE_OBJECTID
)
797 return fs_info
->chunk_root
;
798 if (location
->objectid
== BTRFS_DEV_TREE_OBJECTID
)
799 return fs_info
->dev_root
;
800 if (location
->objectid
== BTRFS_CSUM_TREE_OBJECTID
)
801 return fs_info
->csum_root
;
802 if (location
->objectid
== BTRFS_QUOTA_TREE_OBJECTID
)
803 return fs_info
->quota_enabled
? fs_info
->quota_root
:
806 BUG_ON(location
->objectid
== BTRFS_TREE_RELOC_OBJECTID
||
807 location
->offset
!= (u64
)-1);
809 node
= rb_search(&fs_info
->fs_root_tree
, (void *)&objectid
,
810 btrfs_fs_roots_compare_objectids
, NULL
);
812 return container_of(node
, struct btrfs_root
, rb_node
);
814 root
= btrfs_read_fs_root_no_cache(fs_info
, location
);
818 ret
= rb_insert(&fs_info
->fs_root_tree
, &root
->rb_node
,
819 btrfs_fs_roots_compare_roots
);
824 void btrfs_free_fs_info(struct btrfs_fs_info
*fs_info
)
826 if (fs_info
->quota_root
)
827 free(fs_info
->quota_root
);
829 free(fs_info
->tree_root
);
830 free(fs_info
->extent_root
);
831 free(fs_info
->chunk_root
);
832 free(fs_info
->dev_root
);
833 free(fs_info
->csum_root
);
834 free(fs_info
->free_space_root
);
835 free(fs_info
->super_copy
);
836 free(fs_info
->log_root_tree
);
840 struct btrfs_fs_info
*btrfs_new_fs_info(int writable
, u64 sb_bytenr
)
842 struct btrfs_fs_info
*fs_info
;
844 fs_info
= calloc(1, sizeof(struct btrfs_fs_info
));
848 fs_info
->tree_root
= calloc(1, sizeof(struct btrfs_root
));
849 fs_info
->extent_root
= calloc(1, sizeof(struct btrfs_root
));
850 fs_info
->chunk_root
= calloc(1, sizeof(struct btrfs_root
));
851 fs_info
->dev_root
= calloc(1, sizeof(struct btrfs_root
));
852 fs_info
->csum_root
= calloc(1, sizeof(struct btrfs_root
));
853 fs_info
->quota_root
= calloc(1, sizeof(struct btrfs_root
));
854 fs_info
->free_space_root
= calloc(1, sizeof(struct btrfs_root
));
855 fs_info
->super_copy
= calloc(1, BTRFS_SUPER_INFO_SIZE
);
857 if (!fs_info
->tree_root
|| !fs_info
->extent_root
||
858 !fs_info
->chunk_root
|| !fs_info
->dev_root
||
859 !fs_info
->csum_root
|| !fs_info
->quota_root
||
860 !fs_info
->free_space_root
|| !fs_info
->super_copy
)
863 extent_io_tree_init(&fs_info
->extent_cache
);
864 extent_io_tree_init(&fs_info
->free_space_cache
);
865 extent_io_tree_init(&fs_info
->block_group_cache
);
866 extent_io_tree_init(&fs_info
->pinned_extents
);
867 extent_io_tree_init(&fs_info
->pending_del
);
868 extent_io_tree_init(&fs_info
->extent_ins
);
869 fs_info
->excluded_extents
= NULL
;
871 fs_info
->fs_root_tree
= RB_ROOT
;
872 cache_tree_init(&fs_info
->mapping_tree
.cache_tree
);
874 mutex_init(&fs_info
->fs_mutex
);
875 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
876 INIT_LIST_HEAD(&fs_info
->space_info
);
877 INIT_LIST_HEAD(&fs_info
->recow_ebs
);
880 fs_info
->readonly
= 1;
882 fs_info
->super_bytenr
= sb_bytenr
;
883 fs_info
->data_alloc_profile
= (u64
)-1;
884 fs_info
->metadata_alloc_profile
= (u64
)-1;
885 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
888 btrfs_free_fs_info(fs_info
);
892 int btrfs_check_fs_compatibility(struct btrfs_super_block
*sb
,
897 features
= btrfs_super_incompat_flags(sb
) &
898 ~BTRFS_FEATURE_INCOMPAT_SUPP
;
900 printk("couldn't open because of unsupported "
901 "option features (%Lx).\n",
902 (unsigned long long)features
);
906 features
= btrfs_super_incompat_flags(sb
);
907 if (!(features
& BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF
)) {
908 features
|= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF
;
909 btrfs_set_super_incompat_flags(sb
, features
);
912 features
= btrfs_super_compat_ro_flags(sb
);
913 if (flags
& OPEN_CTREE_WRITES
) {
914 if (flags
& OPEN_CTREE_INVALIDATE_FST
) {
915 /* Clear the FREE_SPACE_TREE_VALID bit on disk... */
916 features
&= ~BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID
;
917 btrfs_set_super_compat_ro_flags(sb
, features
);
918 /* ... and ignore the free space tree bit. */
919 features
&= ~BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE
;
921 if (features
& ~BTRFS_FEATURE_COMPAT_RO_SUPP
) {
922 printk("couldn't open RDWR because of unsupported "
923 "option features (%Lx).\n",
924 (unsigned long long)features
);
932 static int find_best_backup_root(struct btrfs_super_block
*super
)
934 struct btrfs_root_backup
*backup
;
935 u64 orig_gen
= btrfs_super_generation(super
);
940 for (i
= 0; i
< BTRFS_NUM_BACKUP_ROOTS
; i
++) {
941 backup
= super
->super_roots
+ i
;
942 if (btrfs_backup_tree_root_gen(backup
) != orig_gen
&&
943 btrfs_backup_tree_root_gen(backup
) > gen
) {
945 gen
= btrfs_backup_tree_root_gen(backup
);
951 static int setup_root_or_create_block(struct btrfs_fs_info
*fs_info
,
953 struct btrfs_root
*info_root
,
954 u64 objectid
, char *str
)
956 struct btrfs_super_block
*sb
= fs_info
->super_copy
;
957 struct btrfs_root
*root
= fs_info
->tree_root
;
958 u32 nodesize
= btrfs_super_nodesize(sb
);
961 ret
= find_and_setup_root(root
, fs_info
, objectid
, info_root
);
963 printk("Couldn't setup %s tree\n", str
);
964 if (!(flags
& OPEN_CTREE_PARTIAL
))
967 * Need a blank node here just so we don't screw up in the
968 * million of places that assume a root has a valid ->node
971 btrfs_find_create_tree_block(fs_info
, 0, nodesize
);
972 if (!info_root
->node
)
974 clear_extent_buffer_uptodate(info_root
->node
);
980 int btrfs_setup_all_roots(struct btrfs_fs_info
*fs_info
, u64 root_tree_bytenr
,
983 struct btrfs_super_block
*sb
= fs_info
->super_copy
;
984 struct btrfs_root
*root
;
985 struct btrfs_key key
;
990 root
= fs_info
->tree_root
;
991 btrfs_setup_root(root
, fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
992 blocksize
= fs_info
->nodesize
;
993 generation
= btrfs_super_generation(sb
);
995 if (!root_tree_bytenr
&& !(flags
& OPEN_CTREE_BACKUP_ROOT
)) {
996 root_tree_bytenr
= btrfs_super_root(sb
);
997 } else if (flags
& OPEN_CTREE_BACKUP_ROOT
) {
998 struct btrfs_root_backup
*backup
;
999 int index
= find_best_backup_root(sb
);
1000 if (index
>= BTRFS_NUM_BACKUP_ROOTS
) {
1001 fprintf(stderr
, "Invalid backup root number\n");
1004 backup
= fs_info
->super_copy
->super_roots
+ index
;
1005 root_tree_bytenr
= btrfs_backup_tree_root(backup
);
1006 generation
= btrfs_backup_tree_root_gen(backup
);
1009 root
->node
= read_tree_block(fs_info
, root_tree_bytenr
, blocksize
,
1011 if (!extent_buffer_uptodate(root
->node
)) {
1012 fprintf(stderr
, "Couldn't read tree root\n");
1016 ret
= setup_root_or_create_block(fs_info
, flags
, fs_info
->extent_root
,
1017 BTRFS_EXTENT_TREE_OBJECTID
, "extent");
1020 fs_info
->extent_root
->track_dirty
= 1;
1022 ret
= find_and_setup_root(root
, fs_info
, BTRFS_DEV_TREE_OBJECTID
,
1025 printk("Couldn't setup device tree\n");
1028 fs_info
->dev_root
->track_dirty
= 1;
1030 ret
= setup_root_or_create_block(fs_info
, flags
, fs_info
->csum_root
,
1031 BTRFS_CSUM_TREE_OBJECTID
, "csum");
1034 fs_info
->csum_root
->track_dirty
= 1;
1036 ret
= find_and_setup_root(root
, fs_info
, BTRFS_QUOTA_TREE_OBJECTID
,
1037 fs_info
->quota_root
);
1039 free(fs_info
->quota_root
);
1040 fs_info
->quota_root
= NULL
;
1042 fs_info
->quota_enabled
= 1;
1045 if (btrfs_fs_compat_ro(fs_info
, FREE_SPACE_TREE
)) {
1046 ret
= find_and_setup_root(root
, fs_info
, BTRFS_FREE_SPACE_TREE_OBJECTID
,
1047 fs_info
->free_space_root
);
1049 printk("Couldn't read free space tree\n");
1052 fs_info
->free_space_root
->track_dirty
= 1;
1055 ret
= find_and_setup_log_root(root
, fs_info
, sb
);
1057 printk("Couldn't setup log root tree\n");
1058 if (!(flags
& OPEN_CTREE_PARTIAL
))
1062 fs_info
->generation
= generation
;
1063 fs_info
->last_trans_committed
= generation
;
1064 if (extent_buffer_uptodate(fs_info
->extent_root
->node
) &&
1065 !(flags
& OPEN_CTREE_NO_BLOCK_GROUPS
))
1066 btrfs_read_block_groups(fs_info
->tree_root
);
1068 key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
1069 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1070 key
.offset
= (u64
)-1;
1071 fs_info
->fs_root
= btrfs_read_fs_root(fs_info
, &key
);
1073 if (IS_ERR(fs_info
->fs_root
))
1078 void btrfs_release_all_roots(struct btrfs_fs_info
*fs_info
)
1080 if (fs_info
->free_space_root
)
1081 free_extent_buffer(fs_info
->free_space_root
->node
);
1082 if (fs_info
->quota_root
)
1083 free_extent_buffer(fs_info
->quota_root
->node
);
1084 if (fs_info
->csum_root
)
1085 free_extent_buffer(fs_info
->csum_root
->node
);
1086 if (fs_info
->dev_root
)
1087 free_extent_buffer(fs_info
->dev_root
->node
);
1088 if (fs_info
->extent_root
)
1089 free_extent_buffer(fs_info
->extent_root
->node
);
1090 if (fs_info
->tree_root
)
1091 free_extent_buffer(fs_info
->tree_root
->node
);
1092 if (fs_info
->log_root_tree
)
1093 free_extent_buffer(fs_info
->log_root_tree
->node
);
1094 if (fs_info
->chunk_root
)
1095 free_extent_buffer(fs_info
->chunk_root
->node
);
1098 static void free_map_lookup(struct cache_extent
*ce
)
1100 struct map_lookup
*map
;
1102 map
= container_of(ce
, struct map_lookup
, ce
);
1106 FREE_EXTENT_CACHE_BASED_TREE(mapping_cache
, free_map_lookup
);
1108 void btrfs_cleanup_all_caches(struct btrfs_fs_info
*fs_info
)
1110 while (!list_empty(&fs_info
->recow_ebs
)) {
1111 struct extent_buffer
*eb
;
1112 eb
= list_first_entry(&fs_info
->recow_ebs
,
1113 struct extent_buffer
, recow
);
1114 list_del_init(&eb
->recow
);
1115 free_extent_buffer(eb
);
1117 free_mapping_cache_tree(&fs_info
->mapping_tree
.cache_tree
);
1118 extent_io_tree_cleanup(&fs_info
->extent_cache
);
1119 extent_io_tree_cleanup(&fs_info
->free_space_cache
);
1120 extent_io_tree_cleanup(&fs_info
->block_group_cache
);
1121 extent_io_tree_cleanup(&fs_info
->pinned_extents
);
1122 extent_io_tree_cleanup(&fs_info
->pending_del
);
1123 extent_io_tree_cleanup(&fs_info
->extent_ins
);
1126 int btrfs_scan_fs_devices(int fd
, const char *path
,
1127 struct btrfs_fs_devices
**fs_devices
,
1128 u64 sb_bytenr
, unsigned sbflags
,
1136 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
1138 seek_ret
= lseek(fd
, 0, SEEK_END
);
1142 dev_size
= seek_ret
;
1143 lseek(fd
, 0, SEEK_SET
);
1144 if (sb_bytenr
> dev_size
) {
1145 error("superblock bytenr %llu is larger than device size %llu",
1146 (unsigned long long)sb_bytenr
,
1147 (unsigned long long)dev_size
);
1151 ret
= btrfs_scan_one_device(fd
, path
, fs_devices
,
1152 &total_devs
, sb_bytenr
, sbflags
);
1154 fprintf(stderr
, "No valid Btrfs found on %s\n", path
);
1158 if (!skip_devices
&& total_devs
!= 1) {
1159 ret
= btrfs_scan_devices();
1166 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info
*fs_info
,
1167 u64 chunk_root_bytenr
)
1169 struct btrfs_super_block
*sb
= fs_info
->super_copy
;
1173 btrfs_setup_root(fs_info
->chunk_root
, fs_info
,
1174 BTRFS_CHUNK_TREE_OBJECTID
);
1176 ret
= btrfs_read_sys_array(fs_info
);
1180 generation
= btrfs_super_chunk_root_generation(sb
);
1182 if (chunk_root_bytenr
&& !IS_ALIGNED(chunk_root_bytenr
,
1183 fs_info
->sectorsize
)) {
1184 warning("chunk_root_bytenr %llu is unaligned to %u, ignore it",
1185 chunk_root_bytenr
, fs_info
->sectorsize
);
1186 chunk_root_bytenr
= 0;
1189 if (!chunk_root_bytenr
)
1190 chunk_root_bytenr
= btrfs_super_chunk_root(sb
);
1194 fs_info
->chunk_root
->node
= read_tree_block(fs_info
,
1198 if (!extent_buffer_uptodate(fs_info
->chunk_root
->node
)) {
1199 if (fs_info
->ignore_chunk_tree_error
) {
1200 warning("cannot read chunk root, continue anyway");
1201 fs_info
->chunk_root
= NULL
;
1204 error("cannot read chunk root");
1209 if (!(btrfs_super_flags(sb
) & BTRFS_SUPER_FLAG_METADUMP
)) {
1210 ret
= btrfs_read_chunk_tree(fs_info
);
1212 fprintf(stderr
, "Couldn't read chunk tree\n");
1219 static struct btrfs_fs_info
*__open_ctree_fd(int fp
, const char *path
,
1221 u64 root_tree_bytenr
,
1222 u64 chunk_root_bytenr
,
1225 struct btrfs_fs_info
*fs_info
;
1226 struct btrfs_super_block
*disk_super
;
1227 struct btrfs_fs_devices
*fs_devices
= NULL
;
1228 struct extent_buffer
*eb
;
1231 unsigned sbflags
= SBREAD_DEFAULT
;
1234 sb_bytenr
= BTRFS_SUPER_INFO_OFFSET
;
1236 /* try to drop all the caches */
1237 if (posix_fadvise(fp
, 0, 0, POSIX_FADV_DONTNEED
))
1238 fprintf(stderr
, "Warning, could not drop caches\n");
1240 fs_info
= btrfs_new_fs_info(flags
& OPEN_CTREE_WRITES
, sb_bytenr
);
1242 fprintf(stderr
, "Failed to allocate memory for fs_info\n");
1245 if (flags
& OPEN_CTREE_RESTORE
)
1246 fs_info
->on_restoring
= 1;
1247 if (flags
& OPEN_CTREE_SUPPRESS_CHECK_BLOCK_ERRORS
)
1248 fs_info
->suppress_check_block_errors
= 1;
1249 if (flags
& OPEN_CTREE_IGNORE_FSID_MISMATCH
)
1250 fs_info
->ignore_fsid_mismatch
= 1;
1251 if (flags
& OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR
)
1252 fs_info
->ignore_chunk_tree_error
= 1;
1254 if ((flags
& OPEN_CTREE_RECOVER_SUPER
)
1255 && (flags
& OPEN_CTREE_FS_PARTIAL
)) {
1257 "cannot open a partially created filesystem for recovery");
1261 if (flags
& OPEN_CTREE_FS_PARTIAL
)
1262 sbflags
= SBREAD_PARTIAL
;
1264 ret
= btrfs_scan_fs_devices(fp
, path
, &fs_devices
, sb_bytenr
, sbflags
,
1265 (flags
& OPEN_CTREE_NO_DEVICES
));
1269 fs_info
->fs_devices
= fs_devices
;
1270 if (flags
& OPEN_CTREE_WRITES
)
1275 if (flags
& OPEN_CTREE_EXCLUSIVE
)
1278 ret
= btrfs_open_devices(fs_devices
, oflags
);
1282 disk_super
= fs_info
->super_copy
;
1283 if (flags
& OPEN_CTREE_RECOVER_SUPER
)
1284 ret
= btrfs_read_dev_super(fs_devices
->latest_bdev
, disk_super
,
1285 sb_bytenr
, SBREAD_RECOVER
);
1287 ret
= btrfs_read_dev_super(fp
, disk_super
, sb_bytenr
,
1290 printk("No valid btrfs found\n");
1294 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_CHANGING_FSID
&&
1295 !fs_info
->ignore_fsid_mismatch
) {
1296 fprintf(stderr
, "ERROR: Filesystem UUID change in progress\n");
1300 memcpy(fs_info
->fsid
, &disk_super
->fsid
, BTRFS_FSID_SIZE
);
1301 fs_info
->sectorsize
= btrfs_super_sectorsize(disk_super
);
1302 fs_info
->nodesize
= btrfs_super_nodesize(disk_super
);
1303 fs_info
->stripesize
= btrfs_super_stripesize(disk_super
);
1305 ret
= btrfs_check_fs_compatibility(fs_info
->super_copy
, flags
);
1309 ret
= btrfs_setup_chunk_tree_and_device_map(fs_info
, chunk_root_bytenr
);
1313 /* Chunk tree root is unable to read, return directly */
1314 if (!fs_info
->chunk_root
)
1317 eb
= fs_info
->chunk_root
->node
;
1318 read_extent_buffer(eb
, fs_info
->chunk_tree_uuid
,
1319 btrfs_header_chunk_tree_uuid(eb
),
1322 ret
= btrfs_setup_all_roots(fs_info
, root_tree_bytenr
, flags
);
1323 if (ret
&& !(flags
& __OPEN_CTREE_RETURN_CHUNK_ROOT
) &&
1324 !fs_info
->ignore_chunk_tree_error
)
1330 btrfs_release_all_roots(fs_info
);
1331 btrfs_cleanup_all_caches(fs_info
);
1333 btrfs_close_devices(fs_devices
);
1335 btrfs_free_fs_info(fs_info
);
1339 struct btrfs_fs_info
*open_ctree_fs_info(const char *filename
,
1340 u64 sb_bytenr
, u64 root_tree_bytenr
,
1341 u64 chunk_root_bytenr
,
1346 struct btrfs_fs_info
*info
;
1347 int oflags
= O_RDWR
;
1350 ret
= stat(filename
, &st
);
1352 error("cannot stat '%s': %s", filename
, strerror(errno
));
1355 if (!(((st
.st_mode
& S_IFMT
) == S_IFREG
) || ((st
.st_mode
& S_IFMT
) == S_IFBLK
))) {
1356 error("not a regular file or block device: %s", filename
);
1360 if (!(flags
& OPEN_CTREE_WRITES
))
1363 fp
= open(filename
, oflags
);
1365 error("cannot open '%s': %s", filename
, strerror(errno
));
1368 info
= __open_ctree_fd(fp
, filename
, sb_bytenr
, root_tree_bytenr
,
1369 chunk_root_bytenr
, flags
);
1374 struct btrfs_root
*open_ctree(const char *filename
, u64 sb_bytenr
,
1377 struct btrfs_fs_info
*info
;
1379 /* This flags may not return fs_info with any valid root */
1380 BUG_ON(flags
& OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR
);
1381 info
= open_ctree_fs_info(filename
, sb_bytenr
, 0, 0, flags
);
1384 if (flags
& __OPEN_CTREE_RETURN_CHUNK_ROOT
)
1385 return info
->chunk_root
;
1386 return info
->fs_root
;
1389 struct btrfs_root
*open_ctree_fd(int fp
, const char *path
, u64 sb_bytenr
,
1392 struct btrfs_fs_info
*info
;
1394 /* This flags may not return fs_info with any valid root */
1395 if (flags
& OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR
) {
1396 error("invalid open_ctree flags: 0x%llx",
1397 (unsigned long long)flags
);
1400 info
= __open_ctree_fd(fp
, path
, sb_bytenr
, 0, 0, flags
);
1403 if (flags
& __OPEN_CTREE_RETURN_CHUNK_ROOT
)
1404 return info
->chunk_root
;
1405 return info
->fs_root
;
1409 * Check if the super is valid:
1410 * - nodesize/sectorsize - minimum, maximum, alignment
1411 * - tree block starts - alignment
1412 * - number of devices - something sane
1413 * - sys array size - maximum
1415 static int check_super(struct btrfs_super_block
*sb
, unsigned sbflags
)
1417 u8 result
[BTRFS_CSUM_SIZE
];
1422 if (btrfs_super_magic(sb
) != BTRFS_MAGIC
) {
1423 if (btrfs_super_magic(sb
) == BTRFS_MAGIC_PARTIAL
) {
1424 if (!(sbflags
& SBREAD_PARTIAL
)) {
1425 error("superblock magic doesn't match");
1431 csum_type
= btrfs_super_csum_type(sb
);
1432 if (csum_type
>= ARRAY_SIZE(btrfs_csum_sizes
)) {
1433 error("unsupported checksum algorithm %u", csum_type
);
1436 csum_size
= btrfs_csum_sizes
[csum_type
];
1439 crc
= btrfs_csum_data((char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
1440 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
1441 btrfs_csum_final(crc
, result
);
1443 if (memcmp(result
, sb
->csum
, csum_size
)) {
1444 error("superblock checksum mismatch");
1447 if (btrfs_super_root_level(sb
) >= BTRFS_MAX_LEVEL
) {
1448 error("tree_root level too big: %d >= %d",
1449 btrfs_super_root_level(sb
), BTRFS_MAX_LEVEL
);
1452 if (btrfs_super_chunk_root_level(sb
) >= BTRFS_MAX_LEVEL
) {
1453 error("chunk_root level too big: %d >= %d",
1454 btrfs_super_chunk_root_level(sb
), BTRFS_MAX_LEVEL
);
1457 if (btrfs_super_log_root_level(sb
) >= BTRFS_MAX_LEVEL
) {
1458 error("log_root level too big: %d >= %d",
1459 btrfs_super_log_root_level(sb
), BTRFS_MAX_LEVEL
);
1463 if (!IS_ALIGNED(btrfs_super_root(sb
), 4096)) {
1464 error("tree_root block unaligned: %llu", btrfs_super_root(sb
));
1467 if (!IS_ALIGNED(btrfs_super_chunk_root(sb
), 4096)) {
1468 error("chunk_root block unaligned: %llu",
1469 btrfs_super_chunk_root(sb
));
1472 if (!IS_ALIGNED(btrfs_super_log_root(sb
), 4096)) {
1473 error("log_root block unaligned: %llu",
1474 btrfs_super_log_root(sb
));
1477 if (btrfs_super_nodesize(sb
) < 4096) {
1478 error("nodesize too small: %u < 4096",
1479 btrfs_super_nodesize(sb
));
1482 if (!IS_ALIGNED(btrfs_super_nodesize(sb
), 4096)) {
1483 error("nodesize unaligned: %u", btrfs_super_nodesize(sb
));
1486 if (btrfs_super_sectorsize(sb
) < 4096) {
1487 error("sectorsize too small: %u < 4096",
1488 btrfs_super_sectorsize(sb
));
1491 if (!IS_ALIGNED(btrfs_super_sectorsize(sb
), 4096)) {
1492 error("sectorsize unaligned: %u", btrfs_super_sectorsize(sb
));
1495 if (btrfs_super_total_bytes(sb
) == 0) {
1496 error("invalid total_bytes 0");
1499 if (btrfs_super_bytes_used(sb
) < 6 * btrfs_super_nodesize(sb
)) {
1500 error("invalid bytes_used %llu", btrfs_super_bytes_used(sb
));
1503 if ((btrfs_super_stripesize(sb
) != 4096)
1504 && (btrfs_super_stripesize(sb
) != btrfs_super_sectorsize(sb
))) {
1505 error("invalid stripesize %u", btrfs_super_stripesize(sb
));
1509 if (memcmp(sb
->fsid
, sb
->dev_item
.fsid
, BTRFS_UUID_SIZE
) != 0) {
1510 char fsid
[BTRFS_UUID_UNPARSED_SIZE
];
1511 char dev_fsid
[BTRFS_UUID_UNPARSED_SIZE
];
1513 uuid_unparse(sb
->fsid
, fsid
);
1514 uuid_unparse(sb
->dev_item
.fsid
, dev_fsid
);
1515 error("dev_item UUID does not match fsid: %s != %s",
1521 * Hint to catch really bogus numbers, bitflips or so
1523 if (btrfs_super_num_devices(sb
) > (1UL << 31)) {
1524 warning("suspicious number of devices: %llu",
1525 btrfs_super_num_devices(sb
));
1528 if (btrfs_super_num_devices(sb
) == 0) {
1529 error("number of devices is 0");
1534 * Obvious sys_chunk_array corruptions, it must hold at least one key
1537 if (btrfs_super_sys_array_size(sb
) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) {
1538 error("system chunk array too big %u > %u",
1539 btrfs_super_sys_array_size(sb
),
1540 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
);
1543 if (btrfs_super_sys_array_size(sb
) < sizeof(struct btrfs_disk_key
)
1544 + sizeof(struct btrfs_chunk
)) {
1545 error("system chunk array too small %u < %zu",
1546 btrfs_super_sys_array_size(sb
),
1547 sizeof(struct btrfs_disk_key
) +
1548 sizeof(struct btrfs_chunk
));
1555 error("superblock checksum matches but it has invalid members");
1559 int btrfs_read_dev_super(int fd
, struct btrfs_super_block
*sb
, u64 sb_bytenr
,
1562 u8 fsid
[BTRFS_FSID_SIZE
];
1563 int fsid_is_initialized
= 0;
1564 char tmp
[BTRFS_SUPER_INFO_SIZE
];
1565 struct btrfs_super_block
*buf
= (struct btrfs_super_block
*)tmp
;
1568 int max_super
= sbflags
& SBREAD_RECOVER
? BTRFS_SUPER_MIRROR_MAX
: 1;
1572 if (sb_bytenr
!= BTRFS_SUPER_INFO_OFFSET
) {
1573 ret
= pread64(fd
, buf
, BTRFS_SUPER_INFO_SIZE
, sb_bytenr
);
1578 /* Not large enough sb, return -ENOENT instead of normal -EIO */
1579 if (ret
< BTRFS_SUPER_INFO_SIZE
)
1582 if (btrfs_super_bytenr(buf
) != sb_bytenr
)
1585 ret
= check_super(buf
, sbflags
);
1588 memcpy(sb
, buf
, BTRFS_SUPER_INFO_SIZE
);
1593 * we would like to check all the supers, but that would make
1594 * a btrfs mount succeed after a mkfs from a different FS.
1595 * So, we need to add a special mount option to scan for
1596 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1599 for (i
= 0; i
< max_super
; i
++) {
1600 bytenr
= btrfs_sb_offset(i
);
1601 ret
= pread64(fd
, buf
, BTRFS_SUPER_INFO_SIZE
, bytenr
);
1602 if (ret
< BTRFS_SUPER_INFO_SIZE
)
1605 if (btrfs_super_bytenr(buf
) != bytenr
)
1607 /* if magic is NULL, the device was removed */
1608 if (btrfs_super_magic(buf
) == 0 && i
== 0)
1610 if (check_super(buf
, sbflags
))
1613 if (!fsid_is_initialized
) {
1614 memcpy(fsid
, buf
->fsid
, sizeof(fsid
));
1615 fsid_is_initialized
= 1;
1616 } else if (memcmp(fsid
, buf
->fsid
, sizeof(fsid
))) {
1618 * the superblocks (the original one and
1619 * its backups) contain data of different
1620 * filesystems -> the super cannot be trusted
1625 if (btrfs_super_generation(buf
) > transid
) {
1626 memcpy(sb
, buf
, BTRFS_SUPER_INFO_SIZE
);
1627 transid
= btrfs_super_generation(buf
);
1631 return transid
> 0 ? 0 : -1;
1634 static int write_dev_supers(struct btrfs_fs_info
*fs_info
,
1635 struct btrfs_super_block
*sb
,
1636 struct btrfs_device
*device
)
1642 if (fs_info
->super_bytenr
!= BTRFS_SUPER_INFO_OFFSET
) {
1643 btrfs_set_super_bytenr(sb
, fs_info
->super_bytenr
);
1645 crc
= btrfs_csum_data((char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
1646 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
1647 btrfs_csum_final(crc
, &sb
->csum
[0]);
1650 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1651 * zero filled, we can use it directly
1653 ret
= pwrite64(device
->fd
, fs_info
->super_copy
,
1654 BTRFS_SUPER_INFO_SIZE
,
1655 fs_info
->super_bytenr
);
1656 if (ret
!= BTRFS_SUPER_INFO_SIZE
)
1661 for (i
= 0; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1662 bytenr
= btrfs_sb_offset(i
);
1663 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
> device
->total_bytes
)
1666 btrfs_set_super_bytenr(sb
, bytenr
);
1669 crc
= btrfs_csum_data((char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
1670 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
1671 btrfs_csum_final(crc
, &sb
->csum
[0]);
1674 * super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
1675 * zero filled, we can use it directly
1677 ret
= pwrite64(device
->fd
, fs_info
->super_copy
,
1678 BTRFS_SUPER_INFO_SIZE
, bytenr
);
1679 if (ret
!= BTRFS_SUPER_INFO_SIZE
)
1687 fprintf(stderr
, "WARNING: failed to write all sb data\n");
1689 fprintf(stderr
, "WARNING: failed to write sb: %s\n",
1694 int write_all_supers(struct btrfs_fs_info
*fs_info
)
1696 struct list_head
*cur
;
1697 struct list_head
*head
= &fs_info
->fs_devices
->devices
;
1698 struct btrfs_device
*dev
;
1699 struct btrfs_super_block
*sb
;
1700 struct btrfs_dev_item
*dev_item
;
1704 sb
= fs_info
->super_copy
;
1705 dev_item
= &sb
->dev_item
;
1706 list_for_each(cur
, head
) {
1707 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
1708 if (!dev
->writeable
)
1711 btrfs_set_stack_device_generation(dev_item
, 0);
1712 btrfs_set_stack_device_type(dev_item
, dev
->type
);
1713 btrfs_set_stack_device_id(dev_item
, dev
->devid
);
1714 btrfs_set_stack_device_total_bytes(dev_item
, dev
->total_bytes
);
1715 btrfs_set_stack_device_bytes_used(dev_item
, dev
->bytes_used
);
1716 btrfs_set_stack_device_io_align(dev_item
, dev
->io_align
);
1717 btrfs_set_stack_device_io_width(dev_item
, dev
->io_width
);
1718 btrfs_set_stack_device_sector_size(dev_item
, dev
->sector_size
);
1719 memcpy(dev_item
->uuid
, dev
->uuid
, BTRFS_UUID_SIZE
);
1720 memcpy(dev_item
->fsid
, dev
->fs_devices
->fsid
, BTRFS_UUID_SIZE
);
1722 flags
= btrfs_super_flags(sb
);
1723 btrfs_set_super_flags(sb
, flags
| BTRFS_HEADER_FLAG_WRITTEN
);
1725 ret
= write_dev_supers(fs_info
, sb
, dev
);
1731 int write_ctree_super(struct btrfs_trans_handle
*trans
,
1732 struct btrfs_fs_info
*fs_info
)
1735 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
1736 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
1738 if (fs_info
->readonly
)
1741 btrfs_set_super_generation(fs_info
->super_copy
,
1743 btrfs_set_super_root(fs_info
->super_copy
,
1744 tree_root
->node
->start
);
1745 btrfs_set_super_root_level(fs_info
->super_copy
,
1746 btrfs_header_level(tree_root
->node
));
1747 btrfs_set_super_chunk_root(fs_info
->super_copy
,
1748 chunk_root
->node
->start
);
1749 btrfs_set_super_chunk_root_level(fs_info
->super_copy
,
1750 btrfs_header_level(chunk_root
->node
));
1751 btrfs_set_super_chunk_root_generation(fs_info
->super_copy
,
1752 btrfs_header_generation(chunk_root
->node
));
1754 ret
= write_all_supers(fs_info
);
1756 fprintf(stderr
, "failed to write new super block err %d\n", ret
);
1760 int close_ctree_fs_info(struct btrfs_fs_info
*fs_info
)
1763 struct btrfs_trans_handle
*trans
;
1764 struct btrfs_root
*root
= fs_info
->tree_root
;
1766 if (fs_info
->last_trans_committed
!=
1767 fs_info
->generation
) {
1769 trans
= btrfs_start_transaction(root
, 1);
1770 btrfs_commit_transaction(trans
, root
);
1771 trans
= btrfs_start_transaction(root
, 1);
1772 ret
= commit_tree_roots(trans
, fs_info
);
1774 ret
= __commit_transaction(trans
, root
);
1776 write_ctree_super(trans
, fs_info
);
1780 if (fs_info
->finalize_on_close
) {
1781 btrfs_set_super_magic(fs_info
->super_copy
, BTRFS_MAGIC
);
1782 root
->fs_info
->finalize_on_close
= 0;
1783 ret
= write_all_supers(fs_info
);
1786 "failed to write new super block err %d\n", ret
);
1788 btrfs_free_block_groups(fs_info
);
1790 free_fs_roots_tree(&fs_info
->fs_root_tree
);
1792 btrfs_release_all_roots(fs_info
);
1793 ret
= btrfs_close_devices(fs_info
->fs_devices
);
1794 btrfs_cleanup_all_caches(fs_info
);
1795 btrfs_free_fs_info(fs_info
);
1799 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
1800 struct extent_buffer
*eb
)
1802 return clear_extent_buffer_dirty(eb
);
1805 void btrfs_mark_buffer_dirty(struct extent_buffer
*eb
)
1807 set_extent_buffer_dirty(eb
);
1810 int btrfs_buffer_uptodate(struct extent_buffer
*buf
, u64 parent_transid
)
1814 ret
= extent_buffer_uptodate(buf
);
1818 ret
= verify_parent_transid(buf
->tree
, buf
, parent_transid
, 1);
1822 int btrfs_set_buffer_uptodate(struct extent_buffer
*eb
)
1824 return set_extent_buffer_uptodate(eb
);