2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
20 #include "transaction.h"
28 #include "check/mode-common.h"
29 #include "check/mode-lowmem.h"
31 static u64 last_allocated_chunk
;
33 static int calc_extent_flag(struct btrfs_root
*root
, struct extent_buffer
*eb
,
36 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
37 struct btrfs_root_item
*ri
= &root
->root_item
;
38 struct btrfs_extent_inline_ref
*iref
;
39 struct btrfs_extent_item
*ei
;
41 struct btrfs_path
*path
= NULL
;
52 * Except file/reloc tree, we can not have FULL BACKREF MODE
54 if (root
->objectid
< BTRFS_FIRST_FREE_OBJECTID
)
58 if (eb
->start
== btrfs_root_bytenr(ri
))
61 if (btrfs_header_flag(eb
, BTRFS_HEADER_FLAG_RELOC
))
64 owner
= btrfs_header_owner(eb
);
65 if (owner
== root
->objectid
)
68 path
= btrfs_alloc_path();
72 key
.objectid
= btrfs_header_bytenr(eb
);
76 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
83 ret
= btrfs_previous_extent_item(extent_root
, path
,
89 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
92 slot
= path
->slots
[0];
93 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_extent_item
);
95 flags
= btrfs_extent_flags(eb
, ei
);
96 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
99 ptr
= (unsigned long)(ei
+ 1);
100 end
= (unsigned long)ei
+ btrfs_item_size_nr(eb
, slot
);
102 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
)
103 ptr
+= sizeof(struct btrfs_tree_block_info
);
106 /* Reached extent item ends normally */
110 /* Beyond extent item end, wrong item size */
112 error("extent item at bytenr %llu slot %d has wrong size",
117 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
118 offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
119 type
= btrfs_extent_inline_ref_type(eb
, iref
);
121 if (type
== BTRFS_TREE_BLOCK_REF_KEY
&& offset
== owner
)
123 ptr
+= btrfs_extent_inline_ref_size(type
);
127 *flags_ret
&= ~BTRFS_BLOCK_FLAG_FULL_BACKREF
;
131 *flags_ret
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
133 btrfs_free_path(path
);
138 * for a tree node or leaf, if it's shared, indeed we don't need to iterate it
139 * in every fs or file tree check. Here we find its all root ids, and only check
140 * it in the fs or file tree which has the smallest root id.
142 static int need_check(struct btrfs_root
*root
, struct ulist
*roots
)
144 struct rb_node
*node
;
145 struct ulist_node
*u
;
148 * @roots can be empty if it belongs to tree reloc tree
149 * In that case, we should always check the leaf, as we can't use
150 * the tree owner to ensure some other root will check it.
152 if (roots
->nnodes
== 1 || roots
->nnodes
== 0)
155 node
= rb_first(&roots
->root
);
156 u
= rb_entry(node
, struct ulist_node
, rb_node
);
158 * current root id is not smallest, we skip it and let it be checked
159 * in the fs or file tree who hash the smallest root id.
161 if (root
->objectid
!= u
->val
)
168 * for a tree node or leaf, we record its reference count, so later if we still
169 * process this node or leaf, don't need to compute its reference count again.
171 * @bytenr if @bytenr == (u64)-1, only update nrefs->full_backref[level]
173 static int update_nodes_refs(struct btrfs_root
*root
, u64 bytenr
,
174 struct extent_buffer
*eb
, struct node_refs
*nrefs
,
175 u64 level
, int check_all
)
180 int root_level
= btrfs_header_level(root
->node
);
184 if (nrefs
->bytenr
[level
] == bytenr
)
187 if (bytenr
!= (u64
)-1) {
188 /* the return value of this function seems a mistake */
189 ret
= btrfs_lookup_extent_info(NULL
, root
, bytenr
,
190 level
, 1, &refs
, &flags
);
192 if (ret
< 0 && !check_all
)
195 nrefs
->bytenr
[level
] = bytenr
;
196 nrefs
->refs
[level
] = refs
;
197 nrefs
->full_backref
[level
] = 0;
198 nrefs
->checked
[level
] = 0;
201 ret
= btrfs_find_all_roots(NULL
, root
->fs_info
, bytenr
,
206 check
= need_check(root
, roots
);
208 nrefs
->need_check
[level
] = check
;
211 nrefs
->need_check
[level
] = 1;
213 if (level
== root_level
) {
214 nrefs
->need_check
[level
] = 1;
217 * The node refs may have not been
218 * updated if upper needs checking (the
219 * lowest root_objectid) the node can
222 nrefs
->need_check
[level
] =
223 nrefs
->need_check
[level
+ 1];
229 if (check_all
&& eb
) {
230 calc_extent_flag(root
, eb
, &flags
);
231 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)
232 nrefs
->full_backref
[level
] = 1;
239 * Mark all extents unfree in the block group. And set @block_group->cached
240 * according to @cache.
242 static int modify_block_group_cache(struct btrfs_fs_info
*fs_info
,
243 struct btrfs_block_group_cache
*block_group
, int cache
)
245 struct extent_io_tree
*free_space_cache
= &fs_info
->free_space_cache
;
246 u64 start
= block_group
->key
.objectid
;
247 u64 end
= start
+ block_group
->key
.offset
;
249 if (cache
&& !block_group
->cached
) {
250 block_group
->cached
= 1;
251 clear_extent_dirty(free_space_cache
, start
, end
- 1);
254 if (!cache
&& block_group
->cached
) {
255 block_group
->cached
= 0;
256 clear_extent_dirty(free_space_cache
, start
, end
- 1);
262 * Modify block groups which have @flags unfree in free space cache.
264 * @cache: if 0, clear block groups cache state;
265 * not 0, mark blocks groups cached.
267 static int modify_block_groups_cache(struct btrfs_fs_info
*fs_info
, u64 flags
,
270 struct btrfs_root
*root
= fs_info
->extent_root
;
271 struct btrfs_key key
;
272 struct btrfs_path path
;
273 struct btrfs_block_group_cache
*bg_cache
;
274 struct btrfs_block_group_item
*bi
;
275 struct btrfs_block_group_item bg_item
;
276 struct extent_buffer
*eb
;
281 key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
284 btrfs_init_path(&path
);
285 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
287 error("fail to search block groups due to %s", strerror(-ret
));
293 slot
= path
.slots
[0];
294 btrfs_item_key_to_cpu(eb
, &key
, slot
);
295 bg_cache
= btrfs_lookup_block_group(fs_info
, key
.objectid
);
301 bi
= btrfs_item_ptr(eb
, slot
, struct btrfs_block_group_item
);
302 read_extent_buffer(eb
, &bg_item
, (unsigned long)bi
,
304 if (btrfs_block_group_flags(&bg_item
) & flags
)
305 modify_block_group_cache(fs_info
, bg_cache
, cache
);
307 ret
= btrfs_next_item(root
, &path
);
317 btrfs_release_path(&path
);
321 static int mark_block_groups_full(struct btrfs_fs_info
*fs_info
, u64 flags
)
323 return modify_block_groups_cache(fs_info
, flags
, 1);
326 static int clear_block_groups_full(struct btrfs_fs_info
*fs_info
, u64 flags
)
328 return modify_block_groups_cache(fs_info
, flags
, 0);
331 static int create_chunk_and_block_group(struct btrfs_fs_info
*fs_info
,
332 u64 flags
, u64
*start
, u64
*nbytes
)
334 struct btrfs_trans_handle
*trans
;
335 struct btrfs_root
*root
= fs_info
->extent_root
;
338 if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) == 0)
341 trans
= btrfs_start_transaction(root
, 1);
343 ret
= PTR_ERR(trans
);
344 error("error starting transaction %s", strerror(-ret
));
347 ret
= btrfs_alloc_chunk(trans
, fs_info
, start
, nbytes
, flags
);
349 error("fail to allocate new chunk %s", strerror(-ret
));
352 ret
= btrfs_make_block_group(trans
, fs_info
, 0, flags
, *start
,
355 error("fail to make block group for chunk %llu %llu %s",
356 *start
, *nbytes
, strerror(-ret
));
360 btrfs_commit_transaction(trans
, root
);
364 static int force_cow_in_new_chunk(struct btrfs_fs_info
*fs_info
,
367 struct btrfs_block_group_cache
*bg
;
374 alloc_profile
= (fs_info
->avail_metadata_alloc_bits
&
375 fs_info
->metadata_alloc_profile
);
376 flags
= BTRFS_BLOCK_GROUP_METADATA
| alloc_profile
;
377 if (btrfs_fs_incompat(fs_info
, MIXED_GROUPS
))
378 flags
|= BTRFS_BLOCK_GROUP_DATA
;
380 ret
= create_chunk_and_block_group(fs_info
, flags
, &start
, &nbytes
);
383 printf("Created new chunk [%llu %llu]\n", start
, nbytes
);
385 flags
= BTRFS_BLOCK_GROUP_METADATA
;
386 /* Mark all metadata block groups cached and full in free space*/
387 ret
= mark_block_groups_full(fs_info
, flags
);
391 bg
= btrfs_lookup_block_group(fs_info
, start
);
394 error("fail to look up block group %llu %llu", start
, nbytes
);
398 /* Clear block group cache just allocated */
399 ret
= modify_block_group_cache(fs_info
, bg
, 0);
407 clear_block_groups_full(fs_info
, flags
);
413 * Returns 0 means not almost full.
414 * Returns >0 means almost full.
415 * Returns <0 means fatal error.
417 static int is_chunk_almost_full(struct btrfs_fs_info
*fs_info
, u64 start
)
419 struct btrfs_path path
;
420 struct btrfs_key key
;
421 struct btrfs_root
*root
= fs_info
->extent_root
;
422 struct btrfs_block_group_item
*bi
;
423 struct btrfs_block_group_item bg_item
;
424 struct extent_buffer
*eb
;
431 key
.objectid
= start
;
432 key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
433 key
.offset
= (u64
)-1;
435 btrfs_init_path(&path
);
436 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
441 ret
= btrfs_previous_item(root
, &path
, start
,
442 BTRFS_BLOCK_GROUP_ITEM_KEY
);
444 error("failed to find block group %llu", start
);
450 slot
= path
.slots
[0];
451 btrfs_item_key_to_cpu(eb
, &key
, slot
);
452 if (key
.objectid
!= start
) {
458 bi
= btrfs_item_ptr(eb
, slot
, struct btrfs_block_group_item
);
459 read_extent_buffer(eb
, &bg_item
, (unsigned long)bi
, sizeof(bg_item
));
460 used
= btrfs_block_group_used(&bg_item
);
463 * if the free space in the chunk is less than %10 of total,
464 * or not not enough for CoW once, we think the chunk is almost full.
466 min_free
= max_t(u64
, (BTRFS_MAX_LEVEL
+ 1) * fs_info
->nodesize
,
467 div_factor(total
, 1));
469 if ((total
- used
) > min_free
)
474 btrfs_release_path(&path
);
479 * Returns <0 for error.
480 * Returns 0 for success.
482 static int try_to_force_cow_in_new_chunk(struct btrfs_fs_info
*fs_info
,
483 u64 old_start
, u64
*new_start
)
488 ret
= is_chunk_almost_full(fs_info
, old_start
);
492 ret
= force_cow_in_new_chunk(fs_info
, new_start
);
496 static int avoid_extents_overwrite(struct btrfs_fs_info
*fs_info
)
499 int mixed
= btrfs_fs_incompat(fs_info
, MIXED_GROUPS
);
501 if (fs_info
->excluded_extents
)
504 if (last_allocated_chunk
!= (u64
)-1) {
505 ret
= try_to_force_cow_in_new_chunk(fs_info
,
506 last_allocated_chunk
, &last_allocated_chunk
);
510 * If failed, do not try to allocate chunk again in
512 * If there is no space left to allocate, try to exclude all
513 * metadata blocks. Mixed filesystem is unsupported.
515 last_allocated_chunk
= (u64
)-1;
516 if (ret
!= -ENOSPC
|| mixed
)
521 "Try to exclude all metadata blcoks and extents, it may be slow\n");
522 ret
= exclude_metadata_blocks(fs_info
);
525 error("failed to avoid extents overwrite %s", strerror(-ret
));
529 static int end_avoid_extents_overwrite(struct btrfs_fs_info
*fs_info
)
533 cleanup_excluded_extents(fs_info
);
534 if (last_allocated_chunk
)
535 ret
= clear_block_groups_full(fs_info
,
536 BTRFS_BLOCK_GROUP_METADATA
);
541 * Wrapper function for btrfs_fix_block_accounting().
543 * Returns 0 on success.
544 * Returns != 0 on error.
546 static int repair_block_accounting(struct btrfs_fs_info
*fs_info
)
548 struct btrfs_trans_handle
*trans
= NULL
;
549 struct btrfs_root
*root
= fs_info
->extent_root
;
552 trans
= btrfs_start_transaction(root
, 1);
554 ret
= PTR_ERR(trans
);
555 error("fail to start transaction %s", strerror(-ret
));
559 ret
= btrfs_fix_block_accounting(trans
, root
);
560 btrfs_commit_transaction(trans
, root
);
565 * This function only handles BACKREF_MISSING,
566 * If corresponding extent item exists, increase the ref, else insert an extent
569 * Returns error bits after repair.
571 static int repair_tree_block_ref(struct btrfs_root
*root
,
572 struct extent_buffer
*node
,
573 struct node_refs
*nrefs
, int level
, int err
)
575 struct btrfs_trans_handle
*trans
= NULL
;
576 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
577 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
578 struct btrfs_path path
;
579 struct btrfs_extent_item
*ei
;
580 struct btrfs_tree_block_info
*bi
;
581 struct btrfs_key key
;
582 struct extent_buffer
*eb
;
583 u32 size
= sizeof(*ei
);
584 u32 node_size
= root
->fs_info
->nodesize
;
585 int insert_extent
= 0;
586 int skinny_metadata
= btrfs_fs_incompat(fs_info
, SKINNY_METADATA
);
587 int root_level
= btrfs_header_level(root
->node
);
592 u64 flags
= BTRFS_EXTENT_FLAG_TREE_BLOCK
;
595 if ((err
& BACKREF_MISSING
) == 0)
598 WARN_ON(level
> BTRFS_MAX_LEVEL
);
601 btrfs_init_path(&path
);
602 bytenr
= btrfs_header_bytenr(node
);
603 owner
= btrfs_header_owner(node
);
604 generation
= btrfs_header_generation(node
);
606 key
.objectid
= bytenr
;
608 key
.offset
= (u64
)-1;
610 /* Search for the extent item */
611 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, &path
, 0, 0);
617 ret
= btrfs_previous_extent_item(extent_root
, &path
, bytenr
);
621 /* calculate if the extent item flag is full backref or not */
622 if (nrefs
->full_backref
[level
] != 0)
623 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
625 ret
= avoid_extents_overwrite(root
->fs_info
);
628 trans
= btrfs_start_transaction(extent_root
, 1);
630 ret
= PTR_ERR(trans
);
632 error("fail to start transaction %s", strerror(-ret
));
635 /* insert an extent item */
637 struct btrfs_disk_key copy_key
;
639 generation
= btrfs_header_generation(node
);
641 if (level
< root_level
&& nrefs
->full_backref
[level
+ 1] &&
642 owner
!= root
->objectid
) {
643 flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
646 key
.objectid
= bytenr
;
647 if (!skinny_metadata
) {
648 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
649 key
.offset
= node_size
;
652 key
.type
= BTRFS_METADATA_ITEM_KEY
;
656 btrfs_release_path(&path
);
657 ret
= btrfs_insert_empty_item(trans
, extent_root
, &path
, &key
,
663 ei
= btrfs_item_ptr(eb
, path
.slots
[0], struct btrfs_extent_item
);
665 btrfs_set_extent_refs(eb
, ei
, 0);
666 btrfs_set_extent_generation(eb
, ei
, generation
);
667 btrfs_set_extent_flags(eb
, ei
, flags
);
669 if (!skinny_metadata
) {
670 bi
= (struct btrfs_tree_block_info
*)(ei
+ 1);
671 memset_extent_buffer(eb
, 0, (unsigned long)bi
,
673 btrfs_set_disk_key_objectid(©_key
, root
->objectid
);
674 btrfs_set_disk_key_type(©_key
, 0);
675 btrfs_set_disk_key_offset(©_key
, 0);
677 btrfs_set_tree_block_level(eb
, bi
, level
);
678 btrfs_set_tree_block_key(eb
, bi
, ©_key
);
680 btrfs_mark_buffer_dirty(eb
);
681 printf("Added an extent item [%llu %u]\n", bytenr
, node_size
);
682 btrfs_update_block_group(extent_root
, bytenr
, node_size
, 1, 0);
684 nrefs
->refs
[level
] = 0;
685 nrefs
->full_backref
[level
] =
686 flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
;
687 btrfs_release_path(&path
);
690 if (level
< root_level
&& nrefs
->full_backref
[level
+ 1] &&
691 owner
!= root
->objectid
)
692 parent
= nrefs
->bytenr
[level
+ 1];
694 /* increase the ref */
695 ret
= btrfs_inc_extent_ref(trans
, extent_root
, bytenr
, node_size
,
696 parent
, root
->objectid
, level
, 0);
698 nrefs
->refs
[level
]++;
701 btrfs_commit_transaction(trans
, extent_root
);
702 btrfs_release_path(&path
);
705 "failed to repair tree block ref start %llu root %llu due to %s",
706 bytenr
, root
->objectid
, strerror(-ret
));
708 printf("Added one tree block ref start %llu %s %llu\n",
709 bytenr
, parent
? "parent" : "root",
710 parent
? parent
: root
->objectid
);
711 err
&= ~BACKREF_MISSING
;
718 * Update global fs information.
720 static void account_bytes(struct btrfs_root
*root
, struct btrfs_path
*path
,
724 struct extent_buffer
*eb
= path
->nodes
[level
];
726 total_btree_bytes
+= eb
->len
;
727 if (fs_root_objectid(root
->objectid
))
728 total_fs_tree_bytes
+= eb
->len
;
729 if (btrfs_header_owner(eb
) == BTRFS_EXTENT_TREE_OBJECTID
)
730 total_extent_tree_bytes
+= eb
->len
;
733 btree_space_waste
+= btrfs_leaf_free_space(eb
);
735 free_nrs
= (BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
) -
736 btrfs_header_nritems(eb
));
737 btree_space_waste
+= free_nrs
* sizeof(struct btrfs_key_ptr
);
742 * Find the @index according by @ino and name.
743 * Notice:time efficiency is O(N)
745 * @root: the root of the fs/file tree
746 * @index_ret: the index as return value
747 * @namebuf: the name to match
748 * @name_len: the length of name to match
749 * @file_type: the file_type of INODE_ITEM to match
751 * Returns 0 if found and *@index_ret will be modified with right value
752 * Returns< 0 not found and *@index_ret will be (u64)-1
754 static int find_dir_index(struct btrfs_root
*root
, u64 dirid
, u64 location_id
,
755 u64
*index_ret
, char *namebuf
, u32 name_len
,
758 struct btrfs_path path
;
759 struct extent_buffer
*node
;
760 struct btrfs_dir_item
*di
;
761 struct btrfs_key key
;
762 struct btrfs_key location
;
763 char name
[BTRFS_NAME_LEN
] = {0};
775 /* search from the last index */
776 key
.objectid
= dirid
;
777 key
.offset
= (u64
)-1;
778 key
.type
= BTRFS_DIR_INDEX_KEY
;
780 btrfs_init_path(&path
);
781 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
786 ret
= btrfs_previous_item(root
, &path
, dirid
, BTRFS_DIR_INDEX_KEY
);
792 /* Check whether inode_id/filetype/name match */
793 node
= path
.nodes
[0];
794 slot
= path
.slots
[0];
795 di
= btrfs_item_ptr(node
, slot
, struct btrfs_dir_item
);
796 total
= btrfs_item_size_nr(node
, slot
);
797 while (cur
< total
) {
799 len
= btrfs_dir_name_len(node
, di
);
800 data_len
= btrfs_dir_data_len(node
, di
);
802 btrfs_dir_item_key_to_cpu(node
, di
, &location
);
803 if (location
.objectid
!= location_id
||
804 location
.type
!= BTRFS_INODE_ITEM_KEY
||
805 location
.offset
!= 0)
808 filetype
= btrfs_dir_type(node
, di
);
809 if (file_type
!= filetype
)
812 if (len
> BTRFS_NAME_LEN
)
813 len
= BTRFS_NAME_LEN
;
815 read_extent_buffer(node
, name
, (unsigned long)(di
+ 1), len
);
816 if (len
!= name_len
|| strncmp(namebuf
, name
, len
))
819 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
820 *index_ret
= key
.offset
;
824 len
+= sizeof(*di
) + data_len
;
825 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
831 btrfs_release_path(&path
);
836 * Find DIR_ITEM/DIR_INDEX for the given key and check it with the specified
837 * INODE_REF/INODE_EXTREF match.
839 * @root: the root of the fs/file tree
840 * @key: the key of the DIR_ITEM/DIR_INDEX, key->offset will be right
841 * value while find index
842 * @location_key: location key of the struct btrfs_dir_item to match
843 * @name: the name to match
844 * @namelen: the length of name
845 * @file_type: the type of file to math
847 * Return 0 if no error occurred.
848 * Return DIR_ITEM_MISSING/DIR_INDEX_MISSING if couldn't find
850 * Return DIR_ITEM_MISMATCH/DIR_INDEX_MISMATCH if INODE_REF/INODE_EXTREF
851 * and DIR_ITEM/DIR_INDEX mismatch
853 static int find_dir_item(struct btrfs_root
*root
, struct btrfs_key
*key
,
854 struct btrfs_key
*location_key
, char *name
,
855 u32 namelen
, u8 file_type
)
857 struct btrfs_path path
;
858 struct extent_buffer
*node
;
859 struct btrfs_dir_item
*di
;
860 struct btrfs_key location
;
861 char namebuf
[BTRFS_NAME_LEN
] = {0};
870 /* get the index by traversing all index */
871 if (key
->type
== BTRFS_DIR_INDEX_KEY
&& key
->offset
== (u64
)-1) {
872 ret
= find_dir_index(root
, key
->objectid
,
873 location_key
->objectid
, &key
->offset
,
874 name
, namelen
, file_type
);
876 ret
= DIR_INDEX_MISSING
;
880 btrfs_init_path(&path
);
881 ret
= btrfs_search_slot(NULL
, root
, key
, &path
, 0, 0);
883 ret
= key
->type
== BTRFS_DIR_ITEM_KEY
? DIR_ITEM_MISSING
:
888 /* Check whether inode_id/filetype/name match */
889 node
= path
.nodes
[0];
890 slot
= path
.slots
[0];
891 di
= btrfs_item_ptr(node
, slot
, struct btrfs_dir_item
);
892 total
= btrfs_item_size_nr(node
, slot
);
893 while (cur
< total
) {
894 ret
= key
->type
== BTRFS_DIR_ITEM_KEY
?
895 DIR_ITEM_MISMATCH
: DIR_INDEX_MISMATCH
;
897 len
= btrfs_dir_name_len(node
, di
);
898 data_len
= btrfs_dir_data_len(node
, di
);
900 btrfs_dir_item_key_to_cpu(node
, di
, &location
);
901 if (location
.objectid
!= location_key
->objectid
||
902 location
.type
!= location_key
->type
||
903 location
.offset
!= location_key
->offset
)
906 filetype
= btrfs_dir_type(node
, di
);
907 if (file_type
!= filetype
)
910 if (len
> BTRFS_NAME_LEN
) {
911 len
= BTRFS_NAME_LEN
;
912 warning("root %llu %s[%llu %llu] name too long %u, trimmed",
914 key
->type
== BTRFS_DIR_ITEM_KEY
?
915 "DIR_ITEM" : "DIR_INDEX",
916 key
->objectid
, key
->offset
, len
);
918 read_extent_buffer(node
, namebuf
, (unsigned long)(di
+ 1),
920 if (len
!= namelen
|| strncmp(namebuf
, name
, len
))
926 len
+= sizeof(*di
) + data_len
;
927 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
932 btrfs_release_path(&path
);
937 * The ternary means dir item, dir index and relative inode ref.
938 * The function handles errs: INODE_MISSING, DIR_INDEX_MISSING
939 * DIR_INDEX_MISMATCH, DIR_ITEM_MISSING, DIR_ITEM_MISMATCH by the follow
941 * If two of three is missing or mismatched, delete the existing one.
942 * If one of three is missing or mismatched, add the missing one.
944 * returns 0 means success.
945 * returns not 0 means on error;
947 int repair_ternary_lowmem(struct btrfs_root
*root
, u64 dir_ino
, u64 ino
,
948 u64 index
, char *name
, int name_len
, u8 filetype
,
951 struct btrfs_trans_handle
*trans
;
956 * stage shall be one of following valild values:
957 * 0: Fine, nothing to do.
958 * 1: One of three is wrong, so add missing one.
959 * 2: Two of three is wrong, so delete existed one.
961 if (err
& (DIR_INDEX_MISMATCH
| DIR_INDEX_MISSING
))
963 if (err
& (DIR_ITEM_MISMATCH
| DIR_ITEM_MISSING
))
965 if (err
& (INODE_REF_MISSING
))
968 /* stage must be smllarer than 3 */
971 trans
= btrfs_start_transaction(root
, 1);
973 ret
= btrfs_unlink(trans
, root
, ino
, dir_ino
, index
, name
,
978 ret
= btrfs_unlink(trans
, root
, ino
, dir_ino
, index
, name
,
982 ret
= btrfs_add_link(trans
, root
, ino
, dir_ino
, name
, name_len
,
983 filetype
, &index
, 1, 1);
987 btrfs_commit_transaction(trans
, root
);
990 error("fail to repair inode %llu name %s filetype %u",
991 ino
, name
, filetype
);
993 printf("%s ref/dir_item of inode %llu name %s filetype %u\n",
994 stage
== 2 ? "Delete" : "Add",
995 ino
, name
, filetype
);
1001 * Prints inode ref error message
1003 static void print_inode_ref_err(struct btrfs_root
*root
, struct btrfs_key
*key
,
1004 u64 index
, const char *namebuf
, int name_len
,
1005 u8 filetype
, int err
)
1010 /* root dir error */
1011 if (key
->objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
1013 "root %llu root dir shouldn't have INODE REF[%llu %llu] name %s",
1014 root
->objectid
, key
->objectid
, key
->offset
, namebuf
);
1019 if (err
& (DIR_ITEM_MISMATCH
| DIR_ITEM_MISSING
))
1020 error("root %llu DIR ITEM[%llu %llu] %s name %s filetype %u",
1021 root
->objectid
, key
->offset
,
1022 btrfs_name_hash(namebuf
, name_len
),
1023 err
& DIR_ITEM_MISMATCH
? "mismatch" : "missing",
1025 if (err
& (DIR_INDEX_MISMATCH
| DIR_INDEX_MISSING
))
1026 error("root %llu DIR INDEX[%llu %llu] %s name %s filetype %u",
1027 root
->objectid
, key
->offset
, index
,
1028 err
& DIR_ITEM_MISMATCH
? "mismatch" : "missing",
1033 * Traverse the given INODE_REF and call find_dir_item() to find related
1034 * DIR_ITEM/DIR_INDEX.
1036 * @root: the root of the fs/file tree
1037 * @ref_key: the key of the INODE_REF
1038 * @path the path provides node and slot
1039 * @refs: the count of INODE_REF
1040 * @mode: the st_mode of INODE_ITEM
1041 * @name_ret: returns with the first ref's name
1042 * @name_len_ret: len of the name_ret
1044 * Return 0 if no error occurred.
1046 static int check_inode_ref(struct btrfs_root
*root
, struct btrfs_key
*ref_key
,
1047 struct btrfs_path
*path
, char *name_ret
,
1048 u32
*namelen_ret
, u64
*refs_ret
, int mode
)
1050 struct btrfs_key key
;
1051 struct btrfs_key location
;
1052 struct btrfs_inode_ref
*ref
;
1053 struct extent_buffer
*node
;
1054 char namebuf
[BTRFS_NAME_LEN
] = {0};
1064 int need_research
= 0;
1072 /* since after repair, path and the dir item may be changed */
1073 if (need_research
) {
1075 btrfs_release_path(path
);
1076 ret
= btrfs_search_slot(NULL
, root
, ref_key
, path
, 0, 0);
1078 * The item was deleted, let the path point to the last checked
1082 if (path
->slots
[0] == 0)
1083 btrfs_prev_leaf(root
, path
);
1091 location
.objectid
= ref_key
->objectid
;
1092 location
.type
= BTRFS_INODE_ITEM_KEY
;
1093 location
.offset
= 0;
1094 node
= path
->nodes
[0];
1095 slot
= path
->slots
[0];
1097 memset(namebuf
, 0, sizeof(namebuf
) / sizeof(*namebuf
));
1098 ref
= btrfs_item_ptr(node
, slot
, struct btrfs_inode_ref
);
1099 total
= btrfs_item_size_nr(node
, slot
);
1102 /* Update inode ref count */
1105 index
= btrfs_inode_ref_index(node
, ref
);
1106 name_len
= btrfs_inode_ref_name_len(node
, ref
);
1108 if (name_len
<= BTRFS_NAME_LEN
) {
1111 len
= BTRFS_NAME_LEN
;
1112 warning("root %llu INODE_REF[%llu %llu] name too long",
1113 root
->objectid
, ref_key
->objectid
, ref_key
->offset
);
1116 read_extent_buffer(node
, namebuf
, (unsigned long)(ref
+ 1), len
);
1118 /* copy the first name found to name_ret */
1119 if (refs
== 1 && name_ret
) {
1120 memcpy(name_ret
, namebuf
, len
);
1124 /* Check root dir ref */
1125 if (ref_key
->objectid
== BTRFS_FIRST_FREE_OBJECTID
) {
1126 if (index
!= 0 || len
!= strlen("..") ||
1127 strncmp("..", namebuf
, len
) ||
1128 ref_key
->offset
!= BTRFS_FIRST_FREE_OBJECTID
) {
1129 /* set err bits then repair will delete the ref */
1130 err
|= DIR_INDEX_MISSING
;
1131 err
|= DIR_ITEM_MISSING
;
1136 /* Find related DIR_INDEX */
1137 key
.objectid
= ref_key
->offset
;
1138 key
.type
= BTRFS_DIR_INDEX_KEY
;
1140 tmp_err
|= find_dir_item(root
, &key
, &location
, namebuf
, len
,
1141 imode_to_type(mode
));
1143 /* Find related dir_item */
1144 key
.objectid
= ref_key
->offset
;
1145 key
.type
= BTRFS_DIR_ITEM_KEY
;
1146 key
.offset
= btrfs_name_hash(namebuf
, len
);
1147 tmp_err
|= find_dir_item(root
, &key
, &location
, namebuf
, len
,
1148 imode_to_type(mode
));
1150 if (tmp_err
&& repair
) {
1151 ret
= repair_ternary_lowmem(root
, ref_key
->offset
,
1152 ref_key
->objectid
, index
, namebuf
,
1153 name_len
, imode_to_type(mode
),
1160 print_inode_ref_err(root
, ref_key
, index
, namebuf
, name_len
,
1161 imode_to_type(mode
), tmp_err
);
1163 len
= sizeof(*ref
) + name_len
;
1164 ref
= (struct btrfs_inode_ref
*)((char *)ref
+ len
);
1175 * Traverse the given INODE_EXTREF and call find_dir_item() to find related
1176 * DIR_ITEM/DIR_INDEX.
1178 * @root: the root of the fs/file tree
1179 * @ref_key: the key of the INODE_EXTREF
1180 * @refs: the count of INODE_EXTREF
1181 * @mode: the st_mode of INODE_ITEM
1183 * Return 0 if no error occurred.
1185 static int check_inode_extref(struct btrfs_root
*root
,
1186 struct btrfs_key
*ref_key
,
1187 struct extent_buffer
*node
, int slot
, u64
*refs
,
1190 struct btrfs_key key
;
1191 struct btrfs_key location
;
1192 struct btrfs_inode_extref
*extref
;
1193 char namebuf
[BTRFS_NAME_LEN
] = {0};
1203 location
.objectid
= ref_key
->objectid
;
1204 location
.type
= BTRFS_INODE_ITEM_KEY
;
1205 location
.offset
= 0;
1207 extref
= btrfs_item_ptr(node
, slot
, struct btrfs_inode_extref
);
1208 total
= btrfs_item_size_nr(node
, slot
);
1211 /* update inode ref count */
1213 name_len
= btrfs_inode_extref_name_len(node
, extref
);
1214 index
= btrfs_inode_extref_index(node
, extref
);
1215 parent
= btrfs_inode_extref_parent(node
, extref
);
1216 if (name_len
<= BTRFS_NAME_LEN
) {
1219 len
= BTRFS_NAME_LEN
;
1220 warning("root %llu INODE_EXTREF[%llu %llu] name too long",
1221 root
->objectid
, ref_key
->objectid
, ref_key
->offset
);
1223 read_extent_buffer(node
, namebuf
, (unsigned long)(extref
+ 1), len
);
1225 /* Check root dir ref name */
1226 if (index
== 0 && strncmp(namebuf
, "..", name_len
)) {
1227 error("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s",
1228 root
->objectid
, ref_key
->objectid
, ref_key
->offset
,
1230 err
|= ROOT_DIR_ERROR
;
1233 /* find related dir_index */
1234 key
.objectid
= parent
;
1235 key
.type
= BTRFS_DIR_INDEX_KEY
;
1237 ret
= find_dir_item(root
, &key
, &location
, namebuf
, len
, mode
);
1240 /* find related dir_item */
1241 key
.objectid
= parent
;
1242 key
.type
= BTRFS_DIR_ITEM_KEY
;
1243 key
.offset
= btrfs_name_hash(namebuf
, len
);
1244 ret
= find_dir_item(root
, &key
, &location
, namebuf
, len
, mode
);
1247 len
= sizeof(*extref
) + name_len
;
1248 extref
= (struct btrfs_inode_extref
*)((char *)extref
+ len
);
1258 * Find INODE_REF/INODE_EXTREF for the given key and check it with the specified
1259 * DIR_ITEM/DIR_INDEX match.
1260 * Return with @index_ret.
1262 * @root: the root of the fs/file tree
1263 * @key: the key of the INODE_REF/INODE_EXTREF
1264 * @name: the name in the INODE_REF/INODE_EXTREF
1265 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
1266 * @index_ret: the index in the INODE_REF/INODE_EXTREF,
1267 * value (64)-1 means do not check index
1269 * Return 0 if no error occurred.
1270 * Return >0 for error bitmap
1272 static int find_inode_ref(struct btrfs_root
*root
, struct btrfs_key
*key
,
1273 char *name
, int namelen
, u64
*index_ret
)
1276 struct btrfs_path path
;
1277 struct btrfs_inode_ref
*ref
;
1278 struct btrfs_inode_extref
*extref
;
1279 struct extent_buffer
*node
;
1280 char ref_namebuf
[BTRFS_NAME_LEN
] = {0};
1293 btrfs_init_path(&path
);
1294 ret
= btrfs_search_slot(NULL
, root
, key
, &path
, 0, 0);
1296 ret
= INODE_REF_MISSING
;
1300 node
= path
.nodes
[0];
1301 slot
= path
.slots
[0];
1303 ref
= btrfs_item_ptr(node
, slot
, struct btrfs_inode_ref
);
1304 total
= btrfs_item_size_nr(node
, slot
);
1306 /* Iterate all entry of INODE_REF */
1307 while (cur
< total
) {
1308 ret
= INODE_REF_MISSING
;
1310 ref_namelen
= btrfs_inode_ref_name_len(node
, ref
);
1311 ref_index
= btrfs_inode_ref_index(node
, ref
);
1312 if (*index_ret
!= (u64
)-1 && *index_ret
!= ref_index
)
1315 if (cur
+ sizeof(*ref
) + ref_namelen
> total
||
1316 ref_namelen
> BTRFS_NAME_LEN
) {
1317 warning("root %llu INODE %s[%llu %llu] name too long",
1319 key
->type
== BTRFS_INODE_REF_KEY
?
1321 key
->objectid
, key
->offset
);
1323 if (cur
+ sizeof(*ref
) > total
)
1325 len
= min_t(u32
, total
- cur
- sizeof(*ref
),
1331 read_extent_buffer(node
, ref_namebuf
, (unsigned long)(ref
+ 1),
1334 if (len
!= namelen
|| strncmp(ref_namebuf
, name
, len
))
1337 *index_ret
= ref_index
;
1341 len
= sizeof(*ref
) + ref_namelen
;
1342 ref
= (struct btrfs_inode_ref
*)((char *)ref
+ len
);
1348 /* Skip if not support EXTENDED_IREF feature */
1349 if (!btrfs_fs_incompat(root
->fs_info
, EXTENDED_IREF
))
1352 btrfs_release_path(&path
);
1353 btrfs_init_path(&path
);
1355 dir_id
= key
->offset
;
1356 key
->type
= BTRFS_INODE_EXTREF_KEY
;
1357 key
->offset
= btrfs_extref_hash(dir_id
, name
, namelen
);
1359 ret
= btrfs_search_slot(NULL
, root
, key
, &path
, 0, 0);
1361 ret
= INODE_REF_MISSING
;
1365 node
= path
.nodes
[0];
1366 slot
= path
.slots
[0];
1368 extref
= btrfs_item_ptr(node
, slot
, struct btrfs_inode_extref
);
1370 total
= btrfs_item_size_nr(node
, slot
);
1372 /* Iterate all entry of INODE_EXTREF */
1373 while (cur
< total
) {
1374 ret
= INODE_REF_MISSING
;
1376 ref_namelen
= btrfs_inode_extref_name_len(node
, extref
);
1377 ref_index
= btrfs_inode_extref_index(node
, extref
);
1378 parent
= btrfs_inode_extref_parent(node
, extref
);
1379 if (*index_ret
!= (u64
)-1 && *index_ret
!= ref_index
)
1382 if (parent
!= dir_id
)
1385 if (ref_namelen
<= BTRFS_NAME_LEN
) {
1388 len
= BTRFS_NAME_LEN
;
1389 warning("root %llu INODE %s[%llu %llu] name too long",
1391 key
->type
== BTRFS_INODE_REF_KEY
?
1393 key
->objectid
, key
->offset
);
1395 read_extent_buffer(node
, ref_namebuf
,
1396 (unsigned long)(extref
+ 1), len
);
1398 if (len
!= namelen
|| strncmp(ref_namebuf
, name
, len
))
1401 *index_ret
= ref_index
;
1406 len
= sizeof(*extref
) + ref_namelen
;
1407 extref
= (struct btrfs_inode_extref
*)((char *)extref
+ len
);
1412 btrfs_release_path(&path
);
1416 static int create_inode_item_lowmem(struct btrfs_trans_handle
*trans
,
1417 struct btrfs_root
*root
, u64 ino
,
1420 u32 mode
= (filetype
== BTRFS_FT_DIR
? S_IFDIR
: S_IFREG
) | 0755;
1422 return insert_inode_item(trans
, root
, ino
, 0, 0, 0, mode
);
1426 * Insert the missing inode item.
1428 * Returns 0 means success.
1429 * Returns <0 means error.
1431 static int repair_inode_item_missing(struct btrfs_root
*root
, u64 ino
,
1434 struct btrfs_key key
;
1435 struct btrfs_trans_handle
*trans
;
1436 struct btrfs_path path
;
1440 key
.type
= BTRFS_INODE_ITEM_KEY
;
1443 btrfs_init_path(&path
);
1444 trans
= btrfs_start_transaction(root
, 1);
1445 if (IS_ERR(trans
)) {
1450 ret
= btrfs_search_slot(trans
, root
, &key
, &path
, 1, 1);
1451 if (ret
< 0 || !ret
)
1454 /* insert inode item */
1455 create_inode_item_lowmem(trans
, root
, ino
, filetype
);
1458 btrfs_commit_transaction(trans
, root
);
1461 error("failed to repair root %llu INODE ITEM[%llu] missing",
1462 root
->objectid
, ino
);
1463 btrfs_release_path(&path
);
1468 * Call repair_inode_item_missing and repair_ternary_lowmem to repair
1470 * Returns error after repair
1472 static int repair_dir_item(struct btrfs_root
*root
, u64 dirid
, u64 ino
,
1473 u64 index
, u8 filetype
, char *namebuf
, u32 name_len
,
1478 if (err
& INODE_ITEM_MISSING
) {
1479 ret
= repair_inode_item_missing(root
, ino
, filetype
);
1481 err
&= ~(INODE_ITEM_MISMATCH
| INODE_ITEM_MISSING
);
1484 if (err
& ~(INODE_ITEM_MISMATCH
| INODE_ITEM_MISSING
)) {
1485 ret
= repair_ternary_lowmem(root
, dirid
, ino
, index
, namebuf
,
1486 name_len
, filetype
, err
);
1488 err
&= ~(DIR_INDEX_MISMATCH
| DIR_INDEX_MISSING
);
1489 err
&= ~(DIR_ITEM_MISMATCH
| DIR_ITEM_MISSING
);
1490 err
&= ~(INODE_REF_MISSING
);
1496 static void print_dir_item_err(struct btrfs_root
*root
, struct btrfs_key
*key
,
1497 u64 ino
, u64 index
, const char *namebuf
,
1498 int name_len
, u8 filetype
, int err
)
1500 if (err
& (DIR_ITEM_MISMATCH
| DIR_ITEM_MISSING
)) {
1501 error("root %llu DIR ITEM[%llu %llu] name %s filetype %d %s",
1502 root
->objectid
, key
->objectid
, key
->offset
, namebuf
,
1504 err
& DIR_ITEM_MISMATCH
? "mismath" : "missing");
1507 if (err
& (DIR_INDEX_MISMATCH
| DIR_INDEX_MISSING
)) {
1508 error("root %llu DIR INDEX[%llu %llu] name %s filetype %d %s",
1509 root
->objectid
, key
->objectid
, index
, namebuf
, filetype
,
1510 err
& DIR_ITEM_MISMATCH
? "mismath" : "missing");
1513 if (err
& (INODE_ITEM_MISSING
| INODE_ITEM_MISMATCH
)) {
1515 "root %llu INODE_ITEM[%llu] index %llu name %s filetype %d %s",
1516 root
->objectid
, ino
, index
, namebuf
, filetype
,
1517 err
& INODE_ITEM_MISMATCH
? "mismath" : "missing");
1520 if (err
& INODE_REF_MISSING
)
1522 "root %llu INODE REF[%llu, %llu] name %s filetype %u missing",
1523 root
->objectid
, ino
, key
->objectid
, namebuf
, filetype
);
1528 * Traverse the given DIR_ITEM/DIR_INDEX and check related INODE_ITEM and
1529 * call find_inode_ref() to check related INODE_REF/INODE_EXTREF.
1531 * @root: the root of the fs/file tree
1532 * @key: the key of the INODE_REF/INODE_EXTREF
1534 * @size: the st_size of the INODE_ITEM
1536 * Return 0 if no error occurred.
1537 * Return DIR_COUNT_AGAIN if the isize of the inode should be recalculated.
1539 static int check_dir_item(struct btrfs_root
*root
, struct btrfs_key
*di_key
,
1540 struct btrfs_path
*path
, u64
*size
)
1542 struct btrfs_dir_item
*di
;
1543 struct btrfs_inode_item
*ii
;
1544 struct btrfs_key key
;
1545 struct btrfs_key location
;
1546 struct extent_buffer
*node
;
1548 char namebuf
[BTRFS_NAME_LEN
] = {0};
1560 int need_research
= 0;
1566 /* since after repair, path and the dir item may be changed */
1567 if (need_research
) {
1569 err
|= DIR_COUNT_AGAIN
;
1570 btrfs_release_path(path
);
1571 ret
= btrfs_search_slot(NULL
, root
, di_key
, path
, 0, 0);
1572 /* the item was deleted, let path point the last checked item */
1574 if (path
->slots
[0] == 0)
1575 btrfs_prev_leaf(root
, path
);
1583 node
= path
->nodes
[0];
1584 slot
= path
->slots
[0];
1586 di
= btrfs_item_ptr(node
, slot
, struct btrfs_dir_item
);
1587 total
= btrfs_item_size_nr(node
, slot
);
1588 memset(namebuf
, 0, sizeof(namebuf
) / sizeof(*namebuf
));
1590 while (cur
< total
) {
1592 * For DIR_ITEM set index to (u64)-1, so that find_inode_ref
1593 * ignore index check.
1595 if (di_key
->type
== BTRFS_DIR_INDEX_KEY
)
1596 index
= di_key
->offset
;
1600 data_len
= btrfs_dir_data_len(node
, di
);
1603 error("root %llu %s[%llu %llu] data_len shouldn't be %u",
1605 di_key
->type
== BTRFS_DIR_ITEM_KEY
? "DIR_ITEM" : "DIR_INDEX",
1606 di_key
->objectid
, di_key
->offset
, data_len
);
1608 name_len
= btrfs_dir_name_len(node
, di
);
1609 if (name_len
<= BTRFS_NAME_LEN
) {
1612 len
= BTRFS_NAME_LEN
;
1613 warning("root %llu %s[%llu %llu] name too long",
1615 di_key
->type
== BTRFS_DIR_ITEM_KEY
? "DIR_ITEM" : "DIR_INDEX",
1616 di_key
->objectid
, di_key
->offset
);
1618 (*size
) += name_len
;
1619 read_extent_buffer(node
, namebuf
, (unsigned long)(di
+ 1),
1621 filetype
= btrfs_dir_type(node
, di
);
1623 if (di_key
->type
== BTRFS_DIR_ITEM_KEY
&&
1624 di_key
->offset
!= btrfs_name_hash(namebuf
, len
)) {
1626 error("root %llu DIR_ITEM[%llu %llu] name %s namelen %u filetype %u mismatch with its hash, wanted %llu have %llu",
1627 root
->objectid
, di_key
->objectid
, di_key
->offset
,
1628 namebuf
, len
, filetype
, di_key
->offset
,
1629 btrfs_name_hash(namebuf
, len
));
1632 btrfs_dir_item_key_to_cpu(node
, di
, &location
);
1633 /* Ignore related ROOT_ITEM check */
1634 if (location
.type
== BTRFS_ROOT_ITEM_KEY
)
1637 btrfs_release_path(path
);
1638 /* Check relative INODE_ITEM(existence/filetype) */
1639 ret
= btrfs_search_slot(NULL
, root
, &location
, path
, 0, 0);
1641 tmp_err
|= INODE_ITEM_MISSING
;
1645 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1646 struct btrfs_inode_item
);
1647 mode
= btrfs_inode_mode(path
->nodes
[0], ii
);
1648 if (imode_to_type(mode
) != filetype
) {
1649 tmp_err
|= INODE_ITEM_MISMATCH
;
1653 /* Check relative INODE_REF/INODE_EXTREF */
1654 key
.objectid
= location
.objectid
;
1655 key
.type
= BTRFS_INODE_REF_KEY
;
1656 key
.offset
= di_key
->objectid
;
1657 tmp_err
|= find_inode_ref(root
, &key
, namebuf
, len
, &index
);
1659 /* check relative INDEX/ITEM */
1660 key
.objectid
= di_key
->objectid
;
1661 if (key
.type
== BTRFS_DIR_ITEM_KEY
) {
1662 key
.type
= BTRFS_DIR_INDEX_KEY
;
1665 key
.type
= BTRFS_DIR_ITEM_KEY
;
1666 key
.offset
= btrfs_name_hash(namebuf
, name_len
);
1669 tmp_err
|= find_dir_item(root
, &key
, &location
, namebuf
,
1670 name_len
, filetype
);
1671 /* find_dir_item may find index */
1672 if (key
.type
== BTRFS_DIR_INDEX_KEY
)
1676 if (tmp_err
&& repair
) {
1677 ret
= repair_dir_item(root
, di_key
->objectid
,
1678 location
.objectid
, index
,
1679 imode_to_type(mode
), namebuf
,
1681 if (ret
!= tmp_err
) {
1686 btrfs_release_path(path
);
1687 print_dir_item_err(root
, di_key
, location
.objectid
, index
,
1688 namebuf
, name_len
, filetype
, tmp_err
);
1690 len
= sizeof(*di
) + name_len
+ data_len
;
1691 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
1694 if (di_key
->type
== BTRFS_DIR_INDEX_KEY
&& cur
< total
) {
1695 error("root %llu DIR_INDEX[%llu %llu] should contain only one entry",
1696 root
->objectid
, di_key
->objectid
,
1703 btrfs_release_path(path
);
1704 ret
= btrfs_search_slot(NULL
, root
, di_key
, path
, 0, 0);
1706 err
|= ret
> 0 ? -ENOENT
: ret
;
1711 * Wrapper function of btrfs_punch_hole.
1713 * Returns 0 means success.
1714 * Returns not 0 means error.
1716 static int punch_extent_hole(struct btrfs_root
*root
, u64 ino
, u64 start
,
1719 struct btrfs_trans_handle
*trans
;
1722 trans
= btrfs_start_transaction(root
, 1);
1724 return PTR_ERR(trans
);
1726 ret
= btrfs_punch_hole(trans
, root
, ino
, start
, len
);
1728 error("failed to add hole [%llu, %llu] in inode [%llu]",
1731 printf("Add a hole [%llu, %llu] in inode [%llu]\n", start
, len
,
1734 btrfs_commit_transaction(trans
, root
);
1739 * Check file extent datasum/hole, update the size of the file extents,
1740 * check and update the last offset of the file extent.
1742 * @root: the root of fs/file tree.
1743 * @fkey: the key of the file extent.
1744 * @nodatasum: INODE_NODATASUM feature.
1745 * @size: the sum of all EXTENT_DATA items size for this inode.
1746 * @end: the offset of the last extent.
1748 * Return 0 if no error occurred.
1750 static int check_file_extent(struct btrfs_root
*root
, struct btrfs_key
*fkey
,
1751 struct extent_buffer
*node
, int slot
,
1752 unsigned int nodatasum
, u64
*size
, u64
*end
)
1754 struct btrfs_file_extent_item
*fi
;
1757 u64 extent_num_bytes
;
1759 u64 csum_found
; /* In byte size, sectorsize aligned */
1760 u64 search_start
; /* Logical range start we search for csum */
1761 u64 search_len
; /* Logical range len we search for csum */
1762 u32 max_inline_extent_size
= min_t(u32
, root
->fs_info
->sectorsize
- 1,
1763 BTRFS_MAX_INLINE_DATA_SIZE(root
->fs_info
));
1764 unsigned int extent_type
;
1765 unsigned int is_hole
;
1770 fi
= btrfs_item_ptr(node
, slot
, struct btrfs_file_extent_item
);
1772 /* Check inline extent */
1773 extent_type
= btrfs_file_extent_type(node
, fi
);
1774 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1775 struct btrfs_item
*e
= btrfs_item_nr(slot
);
1776 u32 item_inline_len
;
1778 item_inline_len
= btrfs_file_extent_inline_item_len(node
, e
);
1779 extent_num_bytes
= btrfs_file_extent_inline_len(node
, slot
, fi
);
1780 compressed
= btrfs_file_extent_compression(node
, fi
);
1781 if (extent_num_bytes
== 0) {
1783 "root %llu EXTENT_DATA[%llu %llu] has empty inline extent",
1784 root
->objectid
, fkey
->objectid
, fkey
->offset
);
1785 err
|= FILE_EXTENT_ERROR
;
1788 if (extent_num_bytes
> root
->fs_info
->sectorsize
) {
1790 "root %llu EXTENT_DATA[%llu %llu] too large inline extent ram size, have %llu, max: %u",
1791 root
->objectid
, fkey
->objectid
,
1792 fkey
->offset
, extent_num_bytes
,
1793 root
->fs_info
->sectorsize
- 1);
1794 err
|= FILE_EXTENT_ERROR
;
1796 if (item_inline_len
> max_inline_extent_size
) {
1798 "root %llu EXTENT_DATA[%llu %llu] too large inline extent on-disk size, have %u, max: %u",
1799 root
->objectid
, fkey
->objectid
,
1800 fkey
->offset
, item_inline_len
,
1801 max_inline_extent_size
);
1802 err
|= FILE_EXTENT_ERROR
;
1805 if (extent_num_bytes
> max_inline_extent_size
) {
1807 "root %llu EXTENT_DATA[%llu %llu] too large inline extent size, have %llu, max: %u",
1808 root
->objectid
, fkey
->objectid
, fkey
->offset
,
1809 extent_num_bytes
, max_inline_extent_size
);
1810 err
|= FILE_EXTENT_ERROR
;
1813 if (!compressed
&& extent_num_bytes
!= item_inline_len
) {
1815 "root %llu EXTENT_DATA[%llu %llu] wrong inline size, have: %llu, expected: %u",
1816 root
->objectid
, fkey
->objectid
, fkey
->offset
,
1817 extent_num_bytes
, item_inline_len
);
1818 err
|= FILE_EXTENT_ERROR
;
1820 *end
+= extent_num_bytes
;
1821 *size
+= extent_num_bytes
;
1825 /* Check extent type */
1826 if (extent_type
!= BTRFS_FILE_EXTENT_REG
&&
1827 extent_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
1828 err
|= FILE_EXTENT_ERROR
;
1829 error("root %llu EXTENT_DATA[%llu %llu] type bad",
1830 root
->objectid
, fkey
->objectid
, fkey
->offset
);
1834 /* Check REG_EXTENT/PREALLOC_EXTENT */
1835 disk_bytenr
= btrfs_file_extent_disk_bytenr(node
, fi
);
1836 disk_num_bytes
= btrfs_file_extent_disk_num_bytes(node
, fi
);
1837 extent_num_bytes
= btrfs_file_extent_num_bytes(node
, fi
);
1838 extent_offset
= btrfs_file_extent_offset(node
, fi
);
1839 compressed
= btrfs_file_extent_compression(node
, fi
);
1840 is_hole
= (disk_bytenr
== 0) && (disk_num_bytes
== 0);
1843 * Check EXTENT_DATA csum
1845 * For plain (uncompressed) extent, we should only check the range
1846 * we're referring to, as it's possible that part of prealloc extent
1847 * has been written, and has csum:
1849 * |<--- Original large preallocated extent A ---->|
1850 * |<- Prealloc File Extent ->|<- Regular Extent ->|
1853 * For compressed extent, we should check the whole range.
1856 search_start
= disk_bytenr
+ extent_offset
;
1857 search_len
= extent_num_bytes
;
1859 search_start
= disk_bytenr
;
1860 search_len
= disk_num_bytes
;
1862 ret
= count_csum_range(root
->fs_info
, search_start
, search_len
,
1864 if (csum_found
> 0 && nodatasum
) {
1865 err
|= ODD_CSUM_ITEM
;
1866 error("root %llu EXTENT_DATA[%llu %llu] nodatasum shouldn't have datasum",
1867 root
->objectid
, fkey
->objectid
, fkey
->offset
);
1868 } else if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !nodatasum
&&
1869 !is_hole
&& (ret
< 0 || csum_found
< search_len
)) {
1870 err
|= CSUM_ITEM_MISSING
;
1871 error("root %llu EXTENT_DATA[%llu %llu] csum missing, have: %llu, expected: %llu",
1872 root
->objectid
, fkey
->objectid
, fkey
->offset
,
1873 csum_found
, search_len
);
1874 } else if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
&&
1876 ret
= check_prealloc_extent_written(root
->fs_info
,
1877 disk_bytenr
, disk_num_bytes
);
1881 err
|= ODD_CSUM_ITEM
;
1883 "root %llu EXTENT_DATA[%llu %llu] prealloc shouldn't have csum, but has: %llu",
1884 root
->objectid
, fkey
->objectid
, fkey
->offset
,
1889 /* Check EXTENT_DATA hole */
1890 if (!no_holes
&& *end
!= fkey
->offset
) {
1892 ret
= punch_extent_hole(root
, fkey
->objectid
,
1893 *end
, fkey
->offset
- *end
);
1894 if (!repair
|| ret
) {
1895 err
|= FILE_EXTENT_ERROR
;
1897 "root %llu EXTENT_DATA[%llu %llu] gap exists, expected: EXTENT_DATA[%llu %llu]",
1898 root
->objectid
, fkey
->objectid
, fkey
->offset
,
1899 fkey
->objectid
, *end
);
1903 *end
+= extent_num_bytes
;
1905 *size
+= extent_num_bytes
;
1910 static int __count_dir_isize(struct btrfs_root
*root
, u64 ino
, int type
,
1913 struct btrfs_key key
;
1914 struct btrfs_path path
;
1916 struct btrfs_dir_item
*di
;
1926 key
.offset
= (u64
)-1;
1928 btrfs_init_path(&path
);
1929 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
1934 /* if found, go to spacial case */
1939 ret
= btrfs_previous_item(root
, &path
, ino
, type
);
1947 di
= btrfs_item_ptr(path
.nodes
[0], path
.slots
[0], struct btrfs_dir_item
);
1949 total
= btrfs_item_size_nr(path
.nodes
[0], path
.slots
[0]);
1951 while (cur
< total
) {
1952 len
= btrfs_dir_name_len(path
.nodes
[0], di
);
1953 if (len
> BTRFS_NAME_LEN
)
1954 len
= BTRFS_NAME_LEN
;
1957 len
+= btrfs_dir_data_len(path
.nodes
[0], di
);
1959 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
1965 btrfs_release_path(&path
);
1969 static int count_dir_isize(struct btrfs_root
*root
, u64 ino
, u64
*size
)
1976 ret
= __count_dir_isize(root
, ino
, BTRFS_DIR_ITEM_KEY
, &item_size
);
1980 ret
= __count_dir_isize(root
, ino
, BTRFS_DIR_INDEX_KEY
, &index_size
);
1984 *size
= item_size
+ index_size
;
1988 error("failed to count root %llu INODE[%llu] root size",
1989 root
->objectid
, ino
);
1994 * Set inode item nbytes to @nbytes
1996 * Returns 0 on success
1997 * Returns != 0 on error
1999 static int repair_inode_nbytes_lowmem(struct btrfs_root
*root
,
2000 struct btrfs_path
*path
,
2001 u64 ino
, u64 nbytes
)
2003 struct btrfs_trans_handle
*trans
;
2004 struct btrfs_inode_item
*ii
;
2005 struct btrfs_key key
;
2006 struct btrfs_key research_key
;
2010 btrfs_item_key_to_cpu(path
->nodes
[0], &research_key
, path
->slots
[0]);
2013 key
.type
= BTRFS_INODE_ITEM_KEY
;
2016 trans
= btrfs_start_transaction(root
, 1);
2017 if (IS_ERR(trans
)) {
2018 ret
= PTR_ERR(trans
);
2023 btrfs_release_path(path
);
2024 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2032 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2033 struct btrfs_inode_item
);
2034 btrfs_set_inode_nbytes(path
->nodes
[0], ii
, nbytes
);
2035 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2037 btrfs_commit_transaction(trans
, root
);
2040 error("failed to set nbytes in inode %llu root %llu",
2041 ino
, root
->root_key
.objectid
);
2043 printf("Set nbytes in inode item %llu root %llu\n to %llu", ino
,
2044 root
->root_key
.objectid
, nbytes
);
2047 btrfs_release_path(path
);
2048 ret
= btrfs_search_slot(NULL
, root
, &research_key
, path
, 0, 0);
2055 * Set directory inode isize to @isize.
2057 * Returns 0 on success.
2058 * Returns != 0 on error.
2060 static int repair_dir_isize_lowmem(struct btrfs_root
*root
,
2061 struct btrfs_path
*path
,
2064 struct btrfs_trans_handle
*trans
;
2065 struct btrfs_inode_item
*ii
;
2066 struct btrfs_key key
;
2067 struct btrfs_key research_key
;
2071 btrfs_item_key_to_cpu(path
->nodes
[0], &research_key
, path
->slots
[0]);
2074 key
.type
= BTRFS_INODE_ITEM_KEY
;
2077 trans
= btrfs_start_transaction(root
, 1);
2078 if (IS_ERR(trans
)) {
2079 ret
= PTR_ERR(trans
);
2084 btrfs_release_path(path
);
2085 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2093 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2094 struct btrfs_inode_item
);
2095 btrfs_set_inode_size(path
->nodes
[0], ii
, isize
);
2096 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2098 btrfs_commit_transaction(trans
, root
);
2101 error("failed to set isize in inode %llu root %llu",
2102 ino
, root
->root_key
.objectid
);
2104 printf("Set isize in inode %llu root %llu to %llu\n",
2105 ino
, root
->root_key
.objectid
, isize
);
2107 btrfs_release_path(path
);
2108 ret
= btrfs_search_slot(NULL
, root
, &research_key
, path
, 0, 0);
2115 * Wrapper function for btrfs_add_orphan_item().
2117 * Returns 0 on success.
2118 * Returns != 0 on error.
2120 static int repair_inode_orphan_item_lowmem(struct btrfs_root
*root
,
2121 struct btrfs_path
*path
, u64 ino
)
2123 struct btrfs_trans_handle
*trans
;
2124 struct btrfs_key research_key
;
2128 btrfs_item_key_to_cpu(path
->nodes
[0], &research_key
, path
->slots
[0]);
2130 trans
= btrfs_start_transaction(root
, 1);
2131 if (IS_ERR(trans
)) {
2132 ret
= PTR_ERR(trans
);
2137 btrfs_release_path(path
);
2138 ret
= btrfs_add_orphan_item(trans
, root
, path
, ino
);
2140 btrfs_commit_transaction(trans
, root
);
2143 error("failed to add inode %llu as orphan item root %llu",
2144 ino
, root
->root_key
.objectid
);
2146 printf("Added inode %llu as orphan item root %llu\n",
2147 ino
, root
->root_key
.objectid
);
2149 btrfs_release_path(path
);
2150 ret
= btrfs_search_slot(NULL
, root
, &research_key
, path
, 0, 0);
2156 /* Set inode_item nlink to @ref_count.
2157 * If @ref_count == 0, move it to "lost+found" and increase @ref_count.
2159 * Returns 0 on success
2161 static int repair_inode_nlinks_lowmem(struct btrfs_root
*root
,
2162 struct btrfs_path
*path
, u64 ino
,
2163 const char *name
, u32 namelen
,
2164 u64 ref_count
, u8 filetype
, u64
*nlink
)
2166 struct btrfs_trans_handle
*trans
;
2167 struct btrfs_inode_item
*ii
;
2168 struct btrfs_key key
;
2169 struct btrfs_key old_key
;
2170 char namebuf
[BTRFS_NAME_LEN
] = {0};
2176 btrfs_item_key_to_cpu(path
->nodes
[0], &old_key
, path
->slots
[0]);
2178 if (name
&& namelen
) {
2179 ASSERT(namelen
<= BTRFS_NAME_LEN
);
2180 memcpy(namebuf
, name
, namelen
);
2183 sprintf(namebuf
, "%llu", ino
);
2184 name_len
= count_digits(ino
);
2185 printf("Can't find file name for inode %llu, use %s instead\n",
2189 trans
= btrfs_start_transaction(root
, 1);
2190 if (IS_ERR(trans
)) {
2191 ret
= PTR_ERR(trans
);
2195 btrfs_release_path(path
);
2196 /* if refs is 0, put it into lostfound */
2197 if (ref_count
== 0) {
2198 ret
= link_inode_to_lostfound(trans
, root
, path
, ino
, namebuf
,
2199 name_len
, filetype
, &ref_count
);
2204 /* reset inode_item's nlink to ref_count */
2206 key
.type
= BTRFS_INODE_ITEM_KEY
;
2209 btrfs_release_path(path
);
2210 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2216 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2217 struct btrfs_inode_item
);
2218 btrfs_set_inode_nlink(path
->nodes
[0], ii
, ref_count
);
2219 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2224 btrfs_commit_transaction(trans
, root
);
2228 "fail to repair nlink of inode %llu root %llu name %s filetype %u",
2229 root
->objectid
, ino
, namebuf
, filetype
);
2231 printf("Fixed nlink of inode %llu root %llu name %s filetype %u\n",
2232 root
->objectid
, ino
, namebuf
, filetype
);
2235 btrfs_release_path(path
);
2236 ret2
= btrfs_search_slot(NULL
, root
, &old_key
, path
, 0, 0);
2242 static bool has_orphan_item(struct btrfs_root
*root
, u64 ino
)
2244 struct btrfs_path path
;
2245 struct btrfs_key key
;
2248 btrfs_init_path(&path
);
2249 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
2250 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
2253 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
2254 btrfs_release_path(&path
);
2261 * Check INODE_ITEM and related ITEMs (the same inode number)
2262 * 1. check link count
2263 * 2. check inode ref/extref
2264 * 3. check dir item/index
2266 * Return 0 if no error occurred.
2267 * Return >0 for error or hit the traversal is done(by error bitmap)
2269 static int check_inode_item(struct btrfs_root
*root
, struct btrfs_path
*path
)
2271 struct extent_buffer
*node
;
2272 struct btrfs_inode_item
*ii
;
2273 struct btrfs_key key
;
2274 struct btrfs_key last_key
;
2283 u64 extent_size
= 0;
2285 unsigned int nodatasum
;
2286 bool is_orphan
= false;
2290 char namebuf
[BTRFS_NAME_LEN
] = {0};
2293 node
= path
->nodes
[0];
2294 slot
= path
->slots
[0];
2296 btrfs_item_key_to_cpu(node
, &key
, slot
);
2297 inode_id
= key
.objectid
;
2299 if (inode_id
== BTRFS_ORPHAN_OBJECTID
) {
2300 ret
= btrfs_next_item(root
, path
);
2306 ii
= btrfs_item_ptr(node
, slot
, struct btrfs_inode_item
);
2307 isize
= btrfs_inode_size(node
, ii
);
2308 nbytes
= btrfs_inode_nbytes(node
, ii
);
2309 mode
= btrfs_inode_mode(node
, ii
);
2310 dir
= imode_to_type(mode
) == BTRFS_FT_DIR
;
2311 nlink
= btrfs_inode_nlink(node
, ii
);
2312 nodatasum
= btrfs_inode_flags(node
, ii
) & BTRFS_INODE_NODATASUM
;
2315 btrfs_item_key_to_cpu(path
->nodes
[0], &last_key
, path
->slots
[0]);
2316 ret
= btrfs_next_item(root
, path
);
2318 /* out will fill 'err' rusing current statistics */
2320 } else if (ret
> 0) {
2325 node
= path
->nodes
[0];
2326 slot
= path
->slots
[0];
2327 btrfs_item_key_to_cpu(node
, &key
, slot
);
2328 if (key
.objectid
!= inode_id
)
2332 case BTRFS_INODE_REF_KEY
:
2333 ret
= check_inode_ref(root
, &key
, path
, namebuf
,
2334 &name_len
, &refs
, mode
);
2337 case BTRFS_INODE_EXTREF_KEY
:
2339 bool ext_ref
= btrfs_fs_incompat(root
->fs_info
,
2341 if (key
.type
== BTRFS_INODE_EXTREF_KEY
&& !ext_ref
)
2342 warning("root %llu EXTREF[%llu %llu] isn't supported",
2343 root
->objectid
, key
.objectid
,
2345 ret
= check_inode_extref(root
, &key
, node
, slot
, &refs
,
2350 case BTRFS_DIR_ITEM_KEY
:
2351 case BTRFS_DIR_INDEX_KEY
:
2353 warning("root %llu INODE[%llu] mode %u shouldn't have DIR_INDEX[%llu %llu]",
2354 root
->objectid
, inode_id
,
2355 imode_to_type(mode
), key
.objectid
,
2358 ret
= check_dir_item(root
, &key
, path
, &size
);
2361 case BTRFS_EXTENT_DATA_KEY
:
2363 warning("root %llu DIR INODE[%llu] shouldn't EXTENT_DATA[%llu %llu]",
2364 root
->objectid
, inode_id
, key
.objectid
,
2367 ret
= check_file_extent(root
, &key
, node
, slot
,
2368 nodatasum
, &extent_size
,
2372 case BTRFS_XATTR_ITEM_KEY
:
2375 error("ITEM[%llu %u %llu] UNKNOWN TYPE",
2376 key
.objectid
, key
.type
, key
.offset
);
2381 if (err
& LAST_ITEM
) {
2382 btrfs_release_path(path
);
2383 ret
= btrfs_search_slot(NULL
, root
, &last_key
, path
, 0, 0);
2388 /* verify INODE_ITEM nlink/isize/nbytes */
2390 if (repair
&& (err
& DIR_COUNT_AGAIN
)) {
2391 err
&= ~DIR_COUNT_AGAIN
;
2392 count_dir_isize(root
, inode_id
, &size
);
2395 if ((nlink
!= 1 || refs
!= 1) && repair
) {
2396 ret
= repair_inode_nlinks_lowmem(root
, path
, inode_id
,
2397 namebuf
, name_len
, refs
, imode_to_type(mode
),
2402 err
|= LINK_COUNT_ERROR
;
2403 error("root %llu DIR INODE[%llu] shouldn't have more than one link(%llu)",
2404 root
->objectid
, inode_id
, nlink
);
2408 * Just a warning, as dir inode nbytes is just an
2409 * instructive value.
2411 if (!IS_ALIGNED(nbytes
, root
->fs_info
->nodesize
)) {
2412 warning("root %llu DIR INODE[%llu] nbytes should be aligned to %u",
2413 root
->objectid
, inode_id
,
2414 root
->fs_info
->nodesize
);
2417 if (isize
!= size
) {
2419 ret
= repair_dir_isize_lowmem(root
, path
,
2421 if (!repair
|| ret
) {
2424 "root %llu DIR INODE [%llu] size %llu not equal to %llu",
2425 root
->objectid
, inode_id
, isize
, size
);
2429 if (nlink
!= refs
) {
2431 ret
= repair_inode_nlinks_lowmem(root
, path
,
2432 inode_id
, namebuf
, name_len
, refs
,
2433 imode_to_type(mode
), &nlink
);
2434 if (!repair
|| ret
) {
2435 err
|= LINK_COUNT_ERROR
;
2437 "root %llu INODE[%llu] nlink(%llu) not equal to inode_refs(%llu)",
2438 root
->objectid
, inode_id
, nlink
, refs
);
2440 } else if (!nlink
) {
2441 is_orphan
= has_orphan_item(root
, inode_id
);
2442 if (!is_orphan
&& repair
)
2443 ret
= repair_inode_orphan_item_lowmem(root
,
2445 if (!is_orphan
&& (!repair
|| ret
)) {
2447 error("root %llu INODE[%llu] is orphan item",
2448 root
->objectid
, inode_id
);
2452 if (!nbytes
&& !no_holes
&& extent_end
< isize
) {
2454 ret
= punch_extent_hole(root
, inode_id
,
2455 extent_end
, isize
- extent_end
);
2456 if (!repair
|| ret
) {
2457 err
|= NBYTES_ERROR
;
2459 "root %llu INODE[%llu] size %llu should have a file extent hole",
2460 root
->objectid
, inode_id
, isize
);
2464 if (nbytes
!= extent_size
) {
2466 ret
= repair_inode_nbytes_lowmem(root
, path
,
2467 inode_id
, extent_size
);
2468 if (!repair
|| ret
) {
2469 err
|= NBYTES_ERROR
;
2471 "root %llu INODE[%llu] nbytes %llu not equal to extent_size %llu",
2472 root
->objectid
, inode_id
, nbytes
,
2478 if (err
& LAST_ITEM
)
2479 btrfs_next_item(root
, path
);
2484 * Returns >0 Found error, not fatal, should continue
2485 * Returns <0 Fatal error, must exit the whole check
2486 * Returns 0 No errors found
2488 static int process_one_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
2489 struct node_refs
*nrefs
, int *level
)
2491 struct extent_buffer
*cur
= path
->nodes
[0];
2492 struct btrfs_key key
;
2496 int root_level
= btrfs_header_level(root
->node
);
2498 int ret
= 0; /* Final return value */
2499 int err
= 0; /* Positive error bitmap */
2501 cur_bytenr
= cur
->start
;
2503 /* skip to first inode item or the first inode number change */
2504 nritems
= btrfs_header_nritems(cur
);
2505 for (i
= 0; i
< nritems
; i
++) {
2506 btrfs_item_key_to_cpu(cur
, &key
, i
);
2508 first_ino
= key
.objectid
;
2509 if (key
.type
== BTRFS_INODE_ITEM_KEY
||
2510 (first_ino
&& first_ino
!= key
.objectid
))
2514 path
->slots
[0] = nritems
;
2520 err
|= check_inode_item(root
, path
);
2522 /* modify cur since check_inode_item may change path */
2523 cur
= path
->nodes
[0];
2525 if (err
& LAST_ITEM
)
2528 /* still have inode items in thie leaf */
2529 if (cur
->start
== cur_bytenr
)
2533 * we have switched to another leaf, above nodes may
2534 * have changed, here walk down the path, if a node
2535 * or leaf is shared, check whether we can skip this
2538 for (i
= root_level
; i
>= 0; i
--) {
2539 if (path
->nodes
[i
]->start
== nrefs
->bytenr
[i
])
2542 ret
= update_nodes_refs(root
, path
->nodes
[i
]->start
,
2543 path
->nodes
[i
], nrefs
, i
, 0);
2547 if (!nrefs
->need_check
[i
]) {
2553 for (i
= 0; i
< *level
; i
++) {
2554 free_extent_buffer(path
->nodes
[i
]);
2555 path
->nodes
[i
] = NULL
;
2565 * @level if @level == -1 means extent data item
2566 * else normal treeblocl.
2568 static int should_check_extent_strictly(struct btrfs_root
*root
,
2569 struct node_refs
*nrefs
, int level
)
2571 int root_level
= btrfs_header_level(root
->node
);
2573 if (level
> root_level
|| level
< -1)
2575 if (level
== root_level
)
2578 * if the upper node is marked full backref, it should contain shared
2579 * backref of the parent (except owner == root->objectid).
2581 while (++level
<= root_level
)
2582 if (nrefs
->refs
[level
] > 1)
2588 static int check_extent_inline_ref(struct extent_buffer
*eb
,
2589 struct btrfs_key
*key
, struct btrfs_extent_inline_ref
*iref
)
2592 u8 type
= btrfs_extent_inline_ref_type(eb
, iref
);
2595 case BTRFS_TREE_BLOCK_REF_KEY
:
2596 case BTRFS_EXTENT_DATA_REF_KEY
:
2597 case BTRFS_SHARED_BLOCK_REF_KEY
:
2598 case BTRFS_SHARED_DATA_REF_KEY
:
2602 error("extent[%llu %u %llu] has unknown ref type: %d",
2603 key
->objectid
, key
->type
, key
->offset
, type
);
2612 * Check backrefs of a tree block given by @bytenr or @eb.
2614 * @root: the root containing the @bytenr or @eb
2615 * @eb: tree block extent buffer, can be NULL
2616 * @bytenr: bytenr of the tree block to search
2617 * @level: tree level of the tree block
2618 * @owner: owner of the tree block
2620 * Return >0 for any error found and output error message
2621 * Return 0 for no error found
2623 static int check_tree_block_ref(struct btrfs_root
*root
,
2624 struct extent_buffer
*eb
, u64 bytenr
,
2625 int level
, u64 owner
, struct node_refs
*nrefs
)
2627 struct btrfs_key key
;
2628 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2629 struct btrfs_path path
;
2630 struct btrfs_extent_item
*ei
;
2631 struct btrfs_extent_inline_ref
*iref
;
2632 struct extent_buffer
*leaf
;
2637 int root_level
= btrfs_header_level(root
->node
);
2639 u32 nodesize
= root
->fs_info
->nodesize
;
2648 btrfs_init_path(&path
);
2649 key
.objectid
= bytenr
;
2650 if (btrfs_fs_incompat(root
->fs_info
, SKINNY_METADATA
))
2651 key
.type
= BTRFS_METADATA_ITEM_KEY
;
2653 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2654 key
.offset
= (u64
)-1;
2656 /* Search for the backref in extent tree */
2657 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, &path
, 0, 0);
2659 err
|= BACKREF_MISSING
;
2662 ret
= btrfs_previous_extent_item(extent_root
, &path
, bytenr
);
2664 err
|= BACKREF_MISSING
;
2668 leaf
= path
.nodes
[0];
2669 slot
= path
.slots
[0];
2670 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2672 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
2674 if (key
.type
== BTRFS_METADATA_ITEM_KEY
) {
2675 skinny_level
= (int)key
.offset
;
2676 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
2678 struct btrfs_tree_block_info
*info
;
2680 info
= (struct btrfs_tree_block_info
*)(ei
+ 1);
2681 skinny_level
= btrfs_tree_block_level(leaf
, info
);
2682 iref
= (struct btrfs_extent_inline_ref
*)(info
+ 1);
2691 * Due to the feature of shared tree blocks, if the upper node
2692 * is a fs root or shared node, the extent of checked node may
2693 * not be updated until the next CoW.
2696 strict
= should_check_extent_strictly(root
, nrefs
,
2698 if (!(btrfs_extent_flags(leaf
, ei
) &
2699 BTRFS_EXTENT_FLAG_TREE_BLOCK
)) {
2701 "extent[%llu %u] backref type mismatch, missing bit: %llx",
2702 key
.objectid
, nodesize
,
2703 BTRFS_EXTENT_FLAG_TREE_BLOCK
);
2704 err
= BACKREF_MISMATCH
;
2706 header_gen
= btrfs_header_generation(eb
);
2707 extent_gen
= btrfs_extent_generation(leaf
, ei
);
2708 if (header_gen
!= extent_gen
) {
2710 "extent[%llu %u] backref generation mismatch, wanted: %llu, have: %llu",
2711 key
.objectid
, nodesize
, header_gen
,
2713 err
= BACKREF_MISMATCH
;
2715 if (level
!= skinny_level
) {
2717 "extent[%llu %u] level mismatch, wanted: %u, have: %u",
2718 key
.objectid
, nodesize
, level
, skinny_level
);
2719 err
= BACKREF_MISMATCH
;
2721 if (!is_fstree(owner
) && btrfs_extent_refs(leaf
, ei
) != 1) {
2723 "extent[%llu %u] is referred by other roots than %llu",
2724 key
.objectid
, nodesize
, root
->objectid
);
2725 err
= BACKREF_MISMATCH
;
2730 * Iterate the extent/metadata item to find the exact backref
2732 item_size
= btrfs_item_size_nr(leaf
, slot
);
2733 ptr
= (unsigned long)iref
;
2734 end
= (unsigned long)ei
+ item_size
;
2737 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
2738 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
2739 offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
2741 ret
= check_extent_inline_ref(leaf
, &key
, iref
);
2746 if (type
== BTRFS_TREE_BLOCK_REF_KEY
) {
2747 if (offset
== root
->objectid
)
2749 if (!strict
&& owner
== offset
)
2751 } else if (type
== BTRFS_SHARED_BLOCK_REF_KEY
) {
2753 * Backref of tree reloc root points to itself, no need
2754 * to check backref any more.
2756 * This may be an error of loop backref, but extent tree
2757 * checker should have already handled it.
2758 * Here we only need to avoid infinite iteration.
2760 if (offset
== bytenr
) {
2764 * Check if the backref points to valid
2767 found_ref
= !check_tree_block_ref(root
, NULL
,
2768 offset
, level
+ 1, owner
, NULL
);
2774 ptr
+= btrfs_extent_inline_ref_size(type
);
2778 * Inlined extent item doesn't have what we need, check
2779 * TREE_BLOCK_REF_KEY
2782 btrfs_release_path(&path
);
2783 key
.objectid
= bytenr
;
2784 key
.type
= BTRFS_TREE_BLOCK_REF_KEY
;
2785 key
.offset
= root
->objectid
;
2787 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, &path
, 0, 0);
2792 * Finally check SHARED BLOCK REF, any found will be good
2793 * Here we're not doing comprehensive extent backref checking,
2794 * only need to ensure there is some extent referring to this
2798 btrfs_release_path(&path
);
2799 key
.objectid
= bytenr
;
2800 key
.type
= BTRFS_SHARED_BLOCK_REF_KEY
;
2801 key
.offset
= (u64
)-1;
2803 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, &path
, 0, 0);
2805 err
|= BACKREF_MISSING
;
2808 ret
= btrfs_previous_extent_item(extent_root
, &path
, bytenr
);
2810 err
|= BACKREF_MISSING
;
2816 err
|= BACKREF_MISSING
;
2818 btrfs_release_path(&path
);
2819 if (nrefs
&& strict
&&
2820 level
< root_level
&& nrefs
->full_backref
[level
+ 1])
2821 parent
= nrefs
->bytenr
[level
+ 1];
2822 if (eb
&& (err
& BACKREF_MISSING
))
2824 "extent[%llu %u] backref lost (owner: %llu, level: %u) %s %llu",
2825 bytenr
, nodesize
, owner
, level
,
2826 parent
? "parent" : "root",
2827 parent
? parent
: root
->objectid
);
2832 * If @err contains BACKREF_MISSING then add extent of the
2833 * file_extent_data_item.
2835 * Returns error bits after reapir.
2837 static int repair_extent_data_item(struct btrfs_root
*root
,
2838 struct btrfs_path
*pathp
,
2839 struct node_refs
*nrefs
,
2842 struct btrfs_trans_handle
*trans
= NULL
;
2843 struct btrfs_file_extent_item
*fi
;
2844 struct btrfs_key fi_key
;
2845 struct btrfs_key key
;
2846 struct btrfs_extent_item
*ei
;
2847 struct btrfs_path path
;
2848 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2849 struct extent_buffer
*eb
;
2859 int need_insert
= 0;
2862 eb
= pathp
->nodes
[0];
2863 slot
= pathp
->slots
[0];
2864 btrfs_item_key_to_cpu(eb
, &fi_key
, slot
);
2865 fi
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
2867 if (btrfs_file_extent_type(eb
, fi
) == BTRFS_FILE_EXTENT_INLINE
||
2868 btrfs_file_extent_disk_bytenr(eb
, fi
) == 0)
2871 file_offset
= fi_key
.offset
;
2872 generation
= btrfs_file_extent_generation(eb
, fi
);
2873 disk_bytenr
= btrfs_file_extent_disk_bytenr(eb
, fi
);
2874 num_bytes
= btrfs_file_extent_disk_num_bytes(eb
, fi
);
2875 extent_offset
= btrfs_file_extent_offset(eb
, fi
);
2876 offset
= file_offset
- extent_offset
;
2878 if (nrefs
->full_backref
[0])
2879 parent
= btrfs_header_bytenr(eb
);
2883 /* now repair only adds backref */
2884 if ((err
& BACKREF_MISSING
) == 0)
2887 /* search extent item */
2888 key
.objectid
= disk_bytenr
;
2889 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2890 key
.offset
= num_bytes
;
2892 btrfs_init_path(&path
);
2893 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, &path
, 0, 0);
2900 ret
= avoid_extents_overwrite(root
->fs_info
);
2903 trans
= btrfs_start_transaction(root
, 1);
2904 if (IS_ERR(trans
)) {
2905 ret
= PTR_ERR(trans
);
2907 error("fail to start transaction %s", strerror(-ret
));
2910 /* insert an extent item */
2912 key
.objectid
= disk_bytenr
;
2913 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
2914 key
.offset
= num_bytes
;
2917 btrfs_release_path(&path
);
2918 ret
= btrfs_insert_empty_item(trans
, extent_root
, &path
, &key
,
2923 ei
= btrfs_item_ptr(eb
, path
.slots
[0], struct btrfs_extent_item
);
2925 btrfs_set_extent_refs(eb
, ei
, 0);
2926 btrfs_set_extent_generation(eb
, ei
, generation
);
2927 btrfs_set_extent_flags(eb
, ei
, BTRFS_EXTENT_FLAG_DATA
);
2929 btrfs_mark_buffer_dirty(eb
);
2930 ret
= btrfs_update_block_group(extent_root
, disk_bytenr
,
2932 btrfs_release_path(&path
);
2935 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
, num_bytes
, parent
,
2937 parent
? BTRFS_FIRST_FREE_OBJECTID
: fi_key
.objectid
,
2941 "failed to increase extent data backref[%llu %llu] root %llu",
2942 disk_bytenr
, num_bytes
, root
->objectid
);
2945 printf("Add one extent data backref [%llu %llu]\n",
2946 disk_bytenr
, num_bytes
);
2949 err
&= ~BACKREF_MISSING
;
2952 btrfs_commit_transaction(trans
, root
);
2953 btrfs_release_path(&path
);
2955 error("can't repair root %llu extent data item[%llu %llu]",
2956 root
->objectid
, disk_bytenr
, num_bytes
);
2961 * Check EXTENT_DATA item, mainly for its dbackref in extent tree
2963 * Return >0 any error found and output error message
2964 * Return 0 for no error found
2966 static int check_extent_data_item(struct btrfs_root
*root
,
2967 struct btrfs_path
*pathp
,
2968 struct node_refs
*nrefs
, int account_bytes
)
2970 struct btrfs_file_extent_item
*fi
;
2971 struct extent_buffer
*eb
= pathp
->nodes
[0];
2972 struct btrfs_path path
;
2973 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2974 struct btrfs_key fi_key
;
2975 struct btrfs_key dbref_key
;
2976 struct extent_buffer
*leaf
;
2977 struct btrfs_extent_item
*ei
;
2978 struct btrfs_extent_inline_ref
*iref
;
2979 struct btrfs_extent_data_ref
*dref
;
2983 u64 extent_num_bytes
;
2990 int found_dbackref
= 0;
2991 int slot
= pathp
->slots
[0];
2996 btrfs_item_key_to_cpu(eb
, &fi_key
, slot
);
2997 fi
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
2999 /* Nothing to check for hole and inline data extents */
3000 if (btrfs_file_extent_type(eb
, fi
) == BTRFS_FILE_EXTENT_INLINE
||
3001 btrfs_file_extent_disk_bytenr(eb
, fi
) == 0)
3004 disk_bytenr
= btrfs_file_extent_disk_bytenr(eb
, fi
);
3005 disk_num_bytes
= btrfs_file_extent_disk_num_bytes(eb
, fi
);
3006 extent_num_bytes
= btrfs_file_extent_num_bytes(eb
, fi
);
3007 offset
= btrfs_file_extent_offset(eb
, fi
);
3009 /* Check unaligned disk_num_bytes and num_bytes */
3010 if (!IS_ALIGNED(disk_num_bytes
, root
->fs_info
->sectorsize
)) {
3012 "file extent [%llu, %llu] has unaligned disk num bytes: %llu, should be aligned to %u",
3013 fi_key
.objectid
, fi_key
.offset
, disk_num_bytes
,
3014 root
->fs_info
->sectorsize
);
3015 err
|= BYTES_UNALIGNED
;
3016 } else if (account_bytes
) {
3017 data_bytes_allocated
+= disk_num_bytes
;
3019 if (!IS_ALIGNED(extent_num_bytes
, root
->fs_info
->sectorsize
)) {
3021 "file extent [%llu, %llu] has unaligned num bytes: %llu, should be aligned to %u",
3022 fi_key
.objectid
, fi_key
.offset
, extent_num_bytes
,
3023 root
->fs_info
->sectorsize
);
3024 err
|= BYTES_UNALIGNED
;
3025 } else if (account_bytes
) {
3026 data_bytes_referenced
+= extent_num_bytes
;
3028 owner
= btrfs_header_owner(eb
);
3030 /* Check the extent item of the file extent in extent tree */
3031 btrfs_init_path(&path
);
3032 dbref_key
.objectid
= btrfs_file_extent_disk_bytenr(eb
, fi
);
3033 dbref_key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3034 dbref_key
.offset
= btrfs_file_extent_disk_num_bytes(eb
, fi
);
3036 ret
= btrfs_search_slot(NULL
, extent_root
, &dbref_key
, &path
, 0, 0);
3040 leaf
= path
.nodes
[0];
3041 slot
= path
.slots
[0];
3042 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
3044 extent_flags
= btrfs_extent_flags(leaf
, ei
);
3046 if (!(extent_flags
& BTRFS_EXTENT_FLAG_DATA
)) {
3048 "file extent[%llu %llu] root %llu owner %llu backref type mismatch, wanted bit: %llx",
3049 fi_key
.objectid
, fi_key
.offset
, root
->objectid
, owner
,
3050 BTRFS_EXTENT_FLAG_DATA
);
3051 err
|= BACKREF_MISMATCH
;
3054 /* Check data backref inside that extent item */
3055 item_size
= btrfs_item_size_nr(leaf
, path
.slots
[0]);
3056 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
3057 ptr
= (unsigned long)iref
;
3058 end
= (unsigned long)ei
+ item_size
;
3059 strict
= should_check_extent_strictly(root
, nrefs
, -1);
3067 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3068 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
3069 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3071 ret
= check_extent_inline_ref(leaf
, &dbref_key
, iref
);
3076 if (type
== BTRFS_EXTENT_DATA_REF_KEY
) {
3077 ref_root
= btrfs_extent_data_ref_root(leaf
, dref
);
3078 ref_objectid
= btrfs_extent_data_ref_objectid(leaf
,
3080 ref_offset
= btrfs_extent_data_ref_offset(leaf
, dref
);
3082 if (ref_objectid
== fi_key
.objectid
&&
3083 ref_offset
== fi_key
.offset
- offset
)
3085 if (ref_root
== root
->objectid
&& match
)
3087 else if (!strict
&& owner
== ref_root
&& match
)
3089 } else if (type
== BTRFS_SHARED_DATA_REF_KEY
) {
3090 found_dbackref
= !check_tree_block_ref(root
, NULL
,
3091 btrfs_extent_inline_ref_offset(leaf
, iref
),
3097 ptr
+= btrfs_extent_inline_ref_size(type
);
3100 if (!found_dbackref
) {
3101 btrfs_release_path(&path
);
3103 /* Didn't find inlined data backref, try EXTENT_DATA_REF_KEY */
3104 dbref_key
.objectid
= btrfs_file_extent_disk_bytenr(eb
, fi
);
3105 dbref_key
.type
= BTRFS_EXTENT_DATA_REF_KEY
;
3106 dbref_key
.offset
= hash_extent_data_ref(owner
, fi_key
.objectid
,
3107 fi_key
.offset
- offset
);
3109 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
,
3110 &dbref_key
, &path
, 0, 0);
3116 btrfs_release_path(&path
);
3119 * Neither inlined nor EXTENT_DATA_REF found, try
3120 * SHARED_DATA_REF as last chance.
3122 dbref_key
.objectid
= disk_bytenr
;
3123 dbref_key
.type
= BTRFS_SHARED_DATA_REF_KEY
;
3124 dbref_key
.offset
= eb
->start
;
3126 ret
= btrfs_search_slot(NULL
, root
->fs_info
->extent_root
,
3127 &dbref_key
, &path
, 0, 0);
3135 if (!found_dbackref
)
3136 err
|= BACKREF_MISSING
;
3137 btrfs_release_path(&path
);
3138 if (err
& BACKREF_MISSING
) {
3140 "file extent[%llu %llu] root %llu owner %llu backref lost",
3141 fi_key
.objectid
, fi_key
.offset
, root
->objectid
, owner
);
3147 * Check a block group item with its referener (chunk) and its used space
3148 * with extent/metadata item
3150 static int check_block_group_item(struct btrfs_fs_info
*fs_info
,
3151 struct extent_buffer
*eb
, int slot
)
3153 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3154 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3155 struct btrfs_block_group_item
*bi
;
3156 struct btrfs_block_group_item bg_item
;
3157 struct btrfs_path path
;
3158 struct btrfs_key bg_key
;
3159 struct btrfs_key chunk_key
;
3160 struct btrfs_key extent_key
;
3161 struct btrfs_chunk
*chunk
;
3162 struct extent_buffer
*leaf
;
3163 struct btrfs_extent_item
*ei
;
3164 u32 nodesize
= btrfs_super_nodesize(fs_info
->super_copy
);
3172 btrfs_item_key_to_cpu(eb
, &bg_key
, slot
);
3173 bi
= btrfs_item_ptr(eb
, slot
, struct btrfs_block_group_item
);
3174 read_extent_buffer(eb
, &bg_item
, (unsigned long)bi
, sizeof(bg_item
));
3175 used
= btrfs_block_group_used(&bg_item
);
3176 bg_flags
= btrfs_block_group_flags(&bg_item
);
3178 chunk_key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3179 chunk_key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3180 chunk_key
.offset
= bg_key
.objectid
;
3182 btrfs_init_path(&path
);
3183 /* Search for the referencer chunk */
3184 ret
= btrfs_search_slot(NULL
, chunk_root
, &chunk_key
, &path
, 0, 0);
3187 "block group[%llu %llu] did not find the related chunk item",
3188 bg_key
.objectid
, bg_key
.offset
);
3189 err
|= REFERENCER_MISSING
;
3191 chunk
= btrfs_item_ptr(path
.nodes
[0], path
.slots
[0],
3192 struct btrfs_chunk
);
3193 if (btrfs_chunk_length(path
.nodes
[0], chunk
) !=
3196 "block group[%llu %llu] related chunk item length does not match",
3197 bg_key
.objectid
, bg_key
.offset
);
3198 err
|= REFERENCER_MISMATCH
;
3201 btrfs_release_path(&path
);
3203 /* Search from the block group bytenr */
3204 extent_key
.objectid
= bg_key
.objectid
;
3205 extent_key
.type
= 0;
3206 extent_key
.offset
= 0;
3208 btrfs_init_path(&path
);
3209 ret
= btrfs_search_slot(NULL
, extent_root
, &extent_key
, &path
, 0, 0);
3213 /* Iterate extent tree to account used space */
3215 leaf
= path
.nodes
[0];
3217 /* Search slot can point to the last item beyond leaf nritems */
3218 if (path
.slots
[0] >= btrfs_header_nritems(leaf
))
3221 btrfs_item_key_to_cpu(leaf
, &extent_key
, path
.slots
[0]);
3222 if (extent_key
.objectid
>= bg_key
.objectid
+ bg_key
.offset
)
3225 if (extent_key
.type
!= BTRFS_METADATA_ITEM_KEY
&&
3226 extent_key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
3228 if (extent_key
.objectid
< bg_key
.objectid
)
3231 if (extent_key
.type
== BTRFS_METADATA_ITEM_KEY
)
3234 total
+= extent_key
.offset
;
3236 ei
= btrfs_item_ptr(leaf
, path
.slots
[0],
3237 struct btrfs_extent_item
);
3238 flags
= btrfs_extent_flags(leaf
, ei
);
3239 if (flags
& BTRFS_EXTENT_FLAG_DATA
) {
3240 if (!(bg_flags
& BTRFS_BLOCK_GROUP_DATA
)) {
3242 "bad extent[%llu, %llu) type mismatch with chunk",
3243 extent_key
.objectid
,
3244 extent_key
.objectid
+ extent_key
.offset
);
3245 err
|= CHUNK_TYPE_MISMATCH
;
3247 } else if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
3248 if (!(bg_flags
& (BTRFS_BLOCK_GROUP_SYSTEM
|
3249 BTRFS_BLOCK_GROUP_METADATA
))) {
3251 "bad extent[%llu, %llu) type mismatch with chunk",
3252 extent_key
.objectid
,
3253 extent_key
.objectid
+ nodesize
);
3254 err
|= CHUNK_TYPE_MISMATCH
;
3258 ret
= btrfs_next_item(extent_root
, &path
);
3264 btrfs_release_path(&path
);
3266 if (total
!= used
) {
3268 "block group[%llu %llu] used %llu but extent items used %llu",
3269 bg_key
.objectid
, bg_key
.offset
, used
, total
);
3270 err
|= BG_ACCOUNTING_ERROR
;
3276 * Get real tree block level for the case like shared block
3277 * Return >= 0 as tree level
3278 * Return <0 for error
3280 static int query_tree_block_level(struct btrfs_fs_info
*fs_info
, u64 bytenr
)
3282 struct extent_buffer
*eb
;
3283 struct btrfs_path path
;
3284 struct btrfs_key key
;
3285 struct btrfs_extent_item
*ei
;
3292 /* Search extent tree for extent generation and level */
3293 key
.objectid
= bytenr
;
3294 key
.type
= BTRFS_METADATA_ITEM_KEY
;
3295 key
.offset
= (u64
)-1;
3297 btrfs_init_path(&path
);
3298 ret
= btrfs_search_slot(NULL
, fs_info
->extent_root
, &key
, &path
, 0, 0);
3301 ret
= btrfs_previous_extent_item(fs_info
->extent_root
, &path
, bytenr
);
3309 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
3310 ei
= btrfs_item_ptr(path
.nodes
[0], path
.slots
[0],
3311 struct btrfs_extent_item
);
3312 flags
= btrfs_extent_flags(path
.nodes
[0], ei
);
3313 if (!(flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)) {
3318 /* Get transid for later read_tree_block() check */
3319 transid
= btrfs_extent_generation(path
.nodes
[0], ei
);
3321 /* Get backref level as one source */
3322 if (key
.type
== BTRFS_METADATA_ITEM_KEY
) {
3323 backref_level
= key
.offset
;
3325 struct btrfs_tree_block_info
*info
;
3327 info
= (struct btrfs_tree_block_info
*)(ei
+ 1);
3328 backref_level
= btrfs_tree_block_level(path
.nodes
[0], info
);
3330 btrfs_release_path(&path
);
3332 /* Get level from tree block as an alternative source */
3333 eb
= read_tree_block(fs_info
, bytenr
, transid
);
3334 if (!extent_buffer_uptodate(eb
)) {
3335 free_extent_buffer(eb
);
3338 header_level
= btrfs_header_level(eb
);
3339 free_extent_buffer(eb
);
3341 if (header_level
!= backref_level
)
3343 return header_level
;
3346 btrfs_release_path(&path
);
3351 * Check if a tree block backref is valid (points to a valid tree block)
3352 * if level == -1, level will be resolved
3353 * Return >0 for any error found and print error message
3355 static int check_tree_block_backref(struct btrfs_fs_info
*fs_info
, u64 root_id
,
3356 u64 bytenr
, int level
)
3358 struct btrfs_root
*root
;
3359 struct btrfs_key key
;
3360 struct btrfs_path path
;
3361 struct extent_buffer
*eb
;
3362 struct extent_buffer
*node
;
3363 u32 nodesize
= btrfs_super_nodesize(fs_info
->super_copy
);
3367 /* Query level for level == -1 special case */
3369 level
= query_tree_block_level(fs_info
, bytenr
);
3371 err
|= REFERENCER_MISSING
;
3375 key
.objectid
= root_id
;
3376 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3377 key
.offset
= (u64
)-1;
3379 root
= btrfs_read_fs_root(fs_info
, &key
);
3381 err
|= REFERENCER_MISSING
;
3385 /* Read out the tree block to get item/node key */
3386 eb
= read_tree_block(fs_info
, bytenr
, 0);
3387 if (!extent_buffer_uptodate(eb
)) {
3388 err
|= REFERENCER_MISSING
;
3389 free_extent_buffer(eb
);
3393 /* Empty tree, no need to check key */
3394 if (!btrfs_header_nritems(eb
) && !level
) {
3395 free_extent_buffer(eb
);
3400 btrfs_node_key_to_cpu(eb
, &key
, 0);
3402 btrfs_item_key_to_cpu(eb
, &key
, 0);
3404 free_extent_buffer(eb
);
3406 btrfs_init_path(&path
);
3407 path
.lowest_level
= level
;
3408 /* Search with the first key, to ensure we can reach it */
3409 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
3411 err
|= REFERENCER_MISSING
;
3415 node
= path
.nodes
[level
];
3416 if (btrfs_header_bytenr(node
) != bytenr
) {
3418 "extent [%llu %d] referencer bytenr mismatch, wanted: %llu, have: %llu",
3419 bytenr
, nodesize
, bytenr
,
3420 btrfs_header_bytenr(node
));
3421 err
|= REFERENCER_MISMATCH
;
3423 if (btrfs_header_level(node
) != level
) {
3425 "extent [%llu %d] referencer level mismatch, wanted: %d, have: %d",
3426 bytenr
, nodesize
, level
,
3427 btrfs_header_level(node
));
3428 err
|= REFERENCER_MISMATCH
;
3432 btrfs_release_path(&path
);
3434 if (err
& REFERENCER_MISSING
) {
3436 error("extent [%llu %d] lost referencer (owner: %llu)",
3437 bytenr
, nodesize
, root_id
);
3440 "extent [%llu %d] lost referencer (owner: %llu, level: %u)",
3441 bytenr
, nodesize
, root_id
, level
);
3448 * Check if tree block @eb is tree reloc root.
3449 * Return 0 if it's not or any problem happens
3450 * Return 1 if it's a tree reloc root
3452 static int is_tree_reloc_root(struct btrfs_fs_info
*fs_info
,
3453 struct extent_buffer
*eb
)
3455 struct btrfs_root
*tree_reloc_root
;
3456 struct btrfs_key key
;
3457 u64 bytenr
= btrfs_header_bytenr(eb
);
3458 u64 owner
= btrfs_header_owner(eb
);
3461 key
.objectid
= BTRFS_TREE_RELOC_OBJECTID
;
3463 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3465 tree_reloc_root
= btrfs_read_fs_root_no_cache(fs_info
, &key
);
3466 if (IS_ERR(tree_reloc_root
))
3469 if (bytenr
== btrfs_header_bytenr(tree_reloc_root
->node
))
3471 btrfs_free_fs_root(tree_reloc_root
);
3476 * Check referencer for shared block backref
3477 * If level == -1, this function will resolve the level.
3479 static int check_shared_block_backref(struct btrfs_fs_info
*fs_info
,
3480 u64 parent
, u64 bytenr
, int level
)
3482 struct extent_buffer
*eb
;
3484 int found_parent
= 0;
3487 eb
= read_tree_block(fs_info
, parent
, 0);
3488 if (!extent_buffer_uptodate(eb
))
3492 level
= query_tree_block_level(fs_info
, bytenr
);
3496 /* It's possible it's a tree reloc root */
3497 if (parent
== bytenr
) {
3498 if (is_tree_reloc_root(fs_info
, eb
))
3503 if (level
+ 1 != btrfs_header_level(eb
))
3506 nr
= btrfs_header_nritems(eb
);
3507 for (i
= 0; i
< nr
; i
++) {
3508 if (bytenr
== btrfs_node_blockptr(eb
, i
)) {
3514 free_extent_buffer(eb
);
3515 if (!found_parent
) {
3517 "shared extent[%llu %u] lost its parent (parent: %llu, level: %u)",
3518 bytenr
, fs_info
->nodesize
, parent
, level
);
3519 return REFERENCER_MISSING
;
3525 * Check referencer for normal (inlined) data ref
3526 * If len == 0, it will be resolved by searching in extent tree
3528 static int check_extent_data_backref(struct btrfs_fs_info
*fs_info
,
3529 u64 root_id
, u64 objectid
, u64 offset
,
3530 u64 bytenr
, u64 len
, u32 count
)
3532 struct btrfs_root
*root
;
3533 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3534 struct btrfs_key key
;
3535 struct btrfs_path path
;
3536 struct extent_buffer
*leaf
;
3537 struct btrfs_file_extent_item
*fi
;
3538 u32 found_count
= 0;
3543 key
.objectid
= bytenr
;
3544 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
3545 key
.offset
= (u64
)-1;
3547 btrfs_init_path(&path
);
3548 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, &path
, 0, 0);
3551 ret
= btrfs_previous_extent_item(extent_root
, &path
, bytenr
);
3554 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
3555 if (key
.objectid
!= bytenr
||
3556 key
.type
!= BTRFS_EXTENT_ITEM_KEY
)
3559 btrfs_release_path(&path
);
3561 key
.objectid
= root_id
;
3562 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3563 key
.offset
= (u64
)-1;
3564 btrfs_init_path(&path
);
3566 root
= btrfs_read_fs_root(fs_info
, &key
);
3570 key
.objectid
= objectid
;
3571 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3573 * It can be nasty as data backref offset is
3574 * file offset - file extent offset, which is smaller or
3575 * equal to original backref offset. The only special case is
3576 * overflow. So we need to special check and do further search.
3578 key
.offset
= offset
& (1ULL << 63) ? 0 : offset
;
3580 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
3585 * Search afterwards to get correct one
3586 * NOTE: As we must do a comprehensive check on the data backref to
3587 * make sure the dref count also matches, we must iterate all file
3588 * extents for that inode.
3591 leaf
= path
.nodes
[0];
3592 slot
= path
.slots
[0];
3594 if (slot
>= btrfs_header_nritems(leaf
) ||
3595 btrfs_header_owner(leaf
) != root_id
)
3597 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3598 if (key
.objectid
!= objectid
||
3599 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
3601 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
3603 * Except normal disk bytenr and disk num bytes, we still
3604 * need to do extra check on dbackref offset as
3605 * dbackref offset = file_offset - file_extent_offset
3607 * Also, we must check the leaf owner.
3608 * In case of shared tree blocks (snapshots) we can inherit
3609 * leaves from source snapshot.
3610 * In that case, reference from source snapshot should not
3613 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == bytenr
&&
3614 btrfs_file_extent_disk_num_bytes(leaf
, fi
) == len
&&
3615 (u64
)(key
.offset
- btrfs_file_extent_offset(leaf
, fi
)) ==
3616 offset
&& btrfs_header_owner(leaf
) == root_id
)
3620 ret
= btrfs_next_item(root
, &path
);
3625 btrfs_release_path(&path
);
3626 if (found_count
!= count
) {
3628 "extent[%llu, %llu] referencer count mismatch (root: %llu, owner: %llu, offset: %llu) wanted: %u, have: %u",
3629 bytenr
, len
, root_id
, objectid
, offset
, count
,
3631 return REFERENCER_MISSING
;
3637 * Check if the referencer of a shared data backref exists
3639 static int check_shared_data_backref(struct btrfs_fs_info
*fs_info
,
3640 u64 parent
, u64 bytenr
)
3642 struct extent_buffer
*eb
;
3643 struct btrfs_key key
;
3644 struct btrfs_file_extent_item
*fi
;
3646 int found_parent
= 0;
3649 eb
= read_tree_block(fs_info
, parent
, 0);
3650 if (!extent_buffer_uptodate(eb
))
3653 nr
= btrfs_header_nritems(eb
);
3654 for (i
= 0; i
< nr
; i
++) {
3655 btrfs_item_key_to_cpu(eb
, &key
, i
);
3656 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
3659 fi
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
3660 if (btrfs_file_extent_type(eb
, fi
) == BTRFS_FILE_EXTENT_INLINE
)
3663 if (btrfs_file_extent_disk_bytenr(eb
, fi
) == bytenr
) {
3670 free_extent_buffer(eb
);
3671 if (!found_parent
) {
3672 error("shared extent %llu referencer lost (parent: %llu)",
3674 return REFERENCER_MISSING
;
3680 * Only delete backref if REFERENCER_MISSING now
3682 * Returns <0 the extent was deleted
3683 * Returns >0 the backref was deleted but extent still exists, returned value
3684 * means error after repair
3685 * Returns 0 nothing happened
3687 static int repair_extent_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
3688 u64 bytenr
, u64 num_bytes
, u64 parent
, u64 root_objectid
,
3689 u64 owner
, u64 offset
, int err
)
3691 struct btrfs_trans_handle
*trans
;
3692 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
3693 struct btrfs_key old_key
;
3697 btrfs_item_key_to_cpu(path
->nodes
[0], &old_key
, path
->slots
[0]);
3699 if ((err
& (REFERENCER_MISSING
| REFERENCER_MISMATCH
)) == 0)
3702 ret
= avoid_extents_overwrite(root
->fs_info
);
3706 trans
= btrfs_start_transaction(extent_root
, 1);
3707 if (IS_ERR(trans
)) {
3708 ret
= PTR_ERR(trans
);
3709 error("fail to start transaction %s", strerror(-ret
));
3710 /* nothing happened */
3714 /* delete the backref */
3715 ret
= btrfs_free_extent(trans
, root
->fs_info
->fs_root
, bytenr
,
3716 num_bytes
, parent
, root_objectid
, owner
, offset
);
3719 err
&= ~REFERENCER_MISSING
;
3720 printf("Delete backref in extent [%llu %llu]\n",
3723 error("fail to delete backref in extent [%llu %llu]",
3726 btrfs_commit_transaction(trans
, extent_root
);
3728 /* btrfs_free_extent may delete the extent */
3729 btrfs_release_path(path
);
3730 ret
= btrfs_search_slot(NULL
, root
, &old_key
, path
, 0, 0);
3740 * This function will check a given extent item, including its backref and
3741 * itself (like crossing stripe boundary and type)
3743 * Since we don't use extent_record anymore, introduce new error bit
3745 static int check_extent_item(struct btrfs_fs_info
*fs_info
,
3746 struct btrfs_path
*path
)
3748 struct btrfs_extent_item
*ei
;
3749 struct btrfs_extent_inline_ref
*iref
;
3750 struct btrfs_extent_data_ref
*dref
;
3751 struct extent_buffer
*eb
= path
->nodes
[0];
3754 int slot
= path
->slots
[0];
3756 u32 nodesize
= btrfs_super_nodesize(fs_info
->super_copy
);
3757 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
3767 struct btrfs_key key
;
3771 btrfs_item_key_to_cpu(eb
, &key
, slot
);
3772 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
3773 bytes_used
+= key
.offset
;
3774 num_bytes
= key
.offset
;
3776 bytes_used
+= nodesize
;
3777 num_bytes
= nodesize
;
3780 if (item_size
< sizeof(*ei
)) {
3782 * COMPAT_EXTENT_TREE_V0 case, but it's already a super
3783 * old thing when on disk format is still un-determined.
3784 * No need to care about it anymore
3786 error("unsupported COMPAT_EXTENT_TREE_V0 detected");
3790 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_extent_item
);
3791 flags
= btrfs_extent_flags(eb
, ei
);
3793 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
3795 if (metadata
&& check_crossing_stripes(global_info
, key
.objectid
,
3797 error("bad metadata [%llu, %llu) crossing stripe boundary",
3798 key
.objectid
, key
.objectid
+ nodesize
);
3799 err
|= CROSSING_STRIPE_BOUNDARY
;
3802 ptr
= (unsigned long)(ei
+ 1);
3804 if (metadata
&& key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
3805 /* Old EXTENT_ITEM metadata */
3806 struct btrfs_tree_block_info
*info
;
3808 info
= (struct btrfs_tree_block_info
*)ptr
;
3809 level
= btrfs_tree_block_level(eb
, info
);
3810 ptr
+= sizeof(struct btrfs_tree_block_info
);
3812 /* New METADATA_ITEM */
3815 end
= (unsigned long)ei
+ item_size
;
3818 /* Reached extent item end normally */
3822 /* Beyond extent item end, wrong item size */
3824 err
|= ITEM_SIZE_MISMATCH
;
3825 error("extent item at bytenr %llu slot %d has wrong size",
3834 /* Now check every backref in this extent item */
3835 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
3836 type
= btrfs_extent_inline_ref_type(eb
, iref
);
3837 offset
= btrfs_extent_inline_ref_offset(eb
, iref
);
3839 case BTRFS_TREE_BLOCK_REF_KEY
:
3840 root_objectid
= offset
;
3842 ret
= check_tree_block_backref(fs_info
, offset
, key
.objectid
,
3846 case BTRFS_SHARED_BLOCK_REF_KEY
:
3848 ret
= check_shared_block_backref(fs_info
, offset
, key
.objectid
,
3852 case BTRFS_EXTENT_DATA_REF_KEY
:
3853 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
3854 root_objectid
= btrfs_extent_data_ref_root(eb
, dref
);
3855 owner
= btrfs_extent_data_ref_objectid(eb
, dref
);
3856 owner_offset
= btrfs_extent_data_ref_offset(eb
, dref
);
3857 ret
= check_extent_data_backref(fs_info
, root_objectid
, owner
,
3858 owner_offset
, key
.objectid
, key
.offset
,
3859 btrfs_extent_data_ref_count(eb
, dref
));
3862 case BTRFS_SHARED_DATA_REF_KEY
:
3864 ret
= check_shared_data_backref(fs_info
, offset
, key
.objectid
);
3868 error("extent[%llu %d %llu] has unknown ref type: %d",
3869 key
.objectid
, key
.type
, key
.offset
, type
);
3875 if (err
&& repair
) {
3876 ret
= repair_extent_item(fs_info
->extent_root
, path
,
3877 key
.objectid
, num_bytes
, parent
, root_objectid
,
3878 owner
, owner_offset
, ret
);
3887 ptr
+= btrfs_extent_inline_ref_size(type
);
3895 * Check if a dev extent item is referred correctly by its chunk
3897 static int check_dev_extent_item(struct btrfs_fs_info
*fs_info
,
3898 struct extent_buffer
*eb
, int slot
)
3900 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3901 struct btrfs_dev_extent
*ptr
;
3902 struct btrfs_path path
;
3903 struct btrfs_key chunk_key
;
3904 struct btrfs_key devext_key
;
3905 struct btrfs_chunk
*chunk
;
3906 struct extent_buffer
*l
;
3910 int found_chunk
= 0;
3913 btrfs_item_key_to_cpu(eb
, &devext_key
, slot
);
3914 ptr
= btrfs_item_ptr(eb
, slot
, struct btrfs_dev_extent
);
3915 length
= btrfs_dev_extent_length(eb
, ptr
);
3917 chunk_key
.objectid
= btrfs_dev_extent_chunk_objectid(eb
, ptr
);
3918 chunk_key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3919 chunk_key
.offset
= btrfs_dev_extent_chunk_offset(eb
, ptr
);
3921 btrfs_init_path(&path
);
3922 ret
= btrfs_search_slot(NULL
, chunk_root
, &chunk_key
, &path
, 0, 0);
3927 chunk
= btrfs_item_ptr(l
, path
.slots
[0], struct btrfs_chunk
);
3928 ret
= btrfs_check_chunk_valid(fs_info
, l
, chunk
, path
.slots
[0],
3933 if (btrfs_stripe_length(fs_info
, l
, chunk
) != length
)
3936 num_stripes
= btrfs_chunk_num_stripes(l
, chunk
);
3937 for (i
= 0; i
< num_stripes
; i
++) {
3938 u64 devid
= btrfs_stripe_devid_nr(l
, chunk
, i
);
3939 u64 offset
= btrfs_stripe_offset_nr(l
, chunk
, i
);
3941 if (devid
== devext_key
.objectid
&&
3942 offset
== devext_key
.offset
) {
3948 btrfs_release_path(&path
);
3951 "device extent[%llu, %llu, %llu] did not find the related chunk",
3952 devext_key
.objectid
, devext_key
.offset
, length
);
3953 return REFERENCER_MISSING
;
3959 * Check if the used space is correct with the dev item
3961 static int check_dev_item(struct btrfs_fs_info
*fs_info
,
3962 struct extent_buffer
*eb
, int slot
)
3964 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
3965 struct btrfs_dev_item
*dev_item
;
3966 struct btrfs_path path
;
3967 struct btrfs_key key
;
3968 struct btrfs_dev_extent
*ptr
;
3975 dev_item
= btrfs_item_ptr(eb
, slot
, struct btrfs_dev_item
);
3976 dev_id
= btrfs_device_id(eb
, dev_item
);
3977 used
= btrfs_device_bytes_used(eb
, dev_item
);
3978 total_bytes
= btrfs_device_total_bytes(eb
, dev_item
);
3980 key
.objectid
= dev_id
;
3981 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3984 btrfs_init_path(&path
);
3985 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, &path
, 0, 0);
3987 btrfs_item_key_to_cpu(eb
, &key
, slot
);
3988 error("cannot find any related dev extent for dev[%llu, %u, %llu]",
3989 key
.objectid
, key
.type
, key
.offset
);
3990 btrfs_release_path(&path
);
3991 return REFERENCER_MISSING
;
3994 /* Iterate dev_extents to calculate the used space of a device */
3996 if (path
.slots
[0] >= btrfs_header_nritems(path
.nodes
[0]))
3999 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
4000 if (key
.objectid
> dev_id
)
4002 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
|| key
.objectid
!= dev_id
)
4005 ptr
= btrfs_item_ptr(path
.nodes
[0], path
.slots
[0],
4006 struct btrfs_dev_extent
);
4007 total
+= btrfs_dev_extent_length(path
.nodes
[0], ptr
);
4009 ret
= btrfs_next_item(dev_root
, &path
);
4013 btrfs_release_path(&path
);
4015 if (used
!= total
) {
4016 btrfs_item_key_to_cpu(eb
, &key
, slot
);
4018 "Dev extent's total-byte %llu is not equal to bytes-used %llu in dev[%llu, %u, %llu]",
4019 total
, used
, BTRFS_ROOT_TREE_OBJECTID
,
4020 BTRFS_DEV_EXTENT_KEY
, dev_id
);
4021 return ACCOUNTING_MISMATCH
;
4023 check_dev_size_alignment(dev_id
, total_bytes
, fs_info
->sectorsize
);
4029 * Check a chunk item.
4030 * Including checking all referred dev_extents and block group
4032 static int check_chunk_item(struct btrfs_fs_info
*fs_info
,
4033 struct extent_buffer
*eb
, int slot
)
4035 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4036 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
4037 struct btrfs_path path
;
4038 struct btrfs_key chunk_key
;
4039 struct btrfs_key bg_key
;
4040 struct btrfs_key devext_key
;
4041 struct btrfs_chunk
*chunk
;
4042 struct extent_buffer
*leaf
;
4043 struct btrfs_block_group_item
*bi
;
4044 struct btrfs_block_group_item bg_item
;
4045 struct btrfs_dev_extent
*ptr
;
4057 btrfs_item_key_to_cpu(eb
, &chunk_key
, slot
);
4058 chunk
= btrfs_item_ptr(eb
, slot
, struct btrfs_chunk
);
4059 length
= btrfs_chunk_length(eb
, chunk
);
4060 chunk_end
= chunk_key
.offset
+ length
;
4061 ret
= btrfs_check_chunk_valid(fs_info
, eb
, chunk
, slot
,
4064 error("chunk[%llu %llu) is invalid", chunk_key
.offset
,
4066 err
|= BYTES_UNALIGNED
| UNKNOWN_TYPE
;
4069 type
= btrfs_chunk_type(eb
, chunk
);
4071 bg_key
.objectid
= chunk_key
.offset
;
4072 bg_key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
4073 bg_key
.offset
= length
;
4075 btrfs_init_path(&path
);
4076 ret
= btrfs_search_slot(NULL
, extent_root
, &bg_key
, &path
, 0, 0);
4079 "chunk[%llu %llu) did not find the related block group item",
4080 chunk_key
.offset
, chunk_end
);
4081 err
|= REFERENCER_MISSING
;
4083 leaf
= path
.nodes
[0];
4084 bi
= btrfs_item_ptr(leaf
, path
.slots
[0],
4085 struct btrfs_block_group_item
);
4086 read_extent_buffer(leaf
, &bg_item
, (unsigned long)bi
,
4088 if (btrfs_block_group_flags(&bg_item
) != type
) {
4090 "chunk[%llu %llu) related block group item flags mismatch, wanted: %llu, have: %llu",
4091 chunk_key
.offset
, chunk_end
, type
,
4092 btrfs_block_group_flags(&bg_item
));
4093 err
|= REFERENCER_MISSING
;
4097 num_stripes
= btrfs_chunk_num_stripes(eb
, chunk
);
4098 stripe_len
= btrfs_stripe_length(fs_info
, eb
, chunk
);
4099 for (i
= 0; i
< num_stripes
; i
++) {
4100 btrfs_release_path(&path
);
4101 btrfs_init_path(&path
);
4102 devext_key
.objectid
= btrfs_stripe_devid_nr(eb
, chunk
, i
);
4103 devext_key
.type
= BTRFS_DEV_EXTENT_KEY
;
4104 devext_key
.offset
= btrfs_stripe_offset_nr(eb
, chunk
, i
);
4106 ret
= btrfs_search_slot(NULL
, dev_root
, &devext_key
, &path
,
4111 leaf
= path
.nodes
[0];
4112 ptr
= btrfs_item_ptr(leaf
, path
.slots
[0],
4113 struct btrfs_dev_extent
);
4114 objectid
= btrfs_dev_extent_chunk_objectid(leaf
, ptr
);
4115 offset
= btrfs_dev_extent_chunk_offset(leaf
, ptr
);
4116 if (objectid
!= chunk_key
.objectid
||
4117 offset
!= chunk_key
.offset
||
4118 btrfs_dev_extent_length(leaf
, ptr
) != stripe_len
)
4122 err
|= BACKREF_MISSING
;
4124 "chunk[%llu %llu) stripe %d did not find the related dev extent",
4125 chunk_key
.objectid
, chunk_end
, i
);
4128 btrfs_release_path(&path
);
4134 * Add block group item to the extent tree if @err contains REFERENCER_MISSING.
4135 * FIXME: We still need to repair error of dev_item.
4137 * Returns error after repair.
4139 static int repair_chunk_item(struct btrfs_root
*chunk_root
,
4140 struct btrfs_path
*path
, int err
)
4142 struct btrfs_chunk
*chunk
;
4143 struct btrfs_key chunk_key
;
4144 struct extent_buffer
*eb
= path
->nodes
[0];
4145 struct btrfs_root
*extent_root
= chunk_root
->fs_info
->extent_root
;
4146 struct btrfs_trans_handle
*trans
;
4148 int slot
= path
->slots
[0];
4152 btrfs_item_key_to_cpu(eb
, &chunk_key
, slot
);
4153 if (chunk_key
.type
!= BTRFS_CHUNK_ITEM_KEY
)
4155 chunk
= btrfs_item_ptr(eb
, slot
, struct btrfs_chunk
);
4156 type
= btrfs_chunk_type(path
->nodes
[0], chunk
);
4157 length
= btrfs_chunk_length(eb
, chunk
);
4159 /* now repair only adds block group */
4160 if ((err
& REFERENCER_MISSING
) == 0)
4163 ret
= avoid_extents_overwrite(chunk_root
->fs_info
);
4167 trans
= btrfs_start_transaction(extent_root
, 1);
4168 if (IS_ERR(trans
)) {
4169 ret
= PTR_ERR(trans
);
4170 error("fail to start transaction %s", strerror(-ret
));
4174 ret
= btrfs_make_block_group(trans
, chunk_root
->fs_info
, 0, type
,
4175 chunk_key
.offset
, length
);
4177 error("fail to add block group item [%llu %llu]",
4178 chunk_key
.offset
, length
);
4180 err
&= ~REFERENCER_MISSING
;
4181 printf("Added block group item[%llu %llu]\n", chunk_key
.offset
,
4185 btrfs_commit_transaction(trans
, extent_root
);
4187 error("fail to repair item(s) related to chunk item [%llu %llu]",
4188 chunk_key
.objectid
, chunk_key
.offset
);
4192 static int delete_extent_tree_item(struct btrfs_root
*root
,
4193 struct btrfs_path
*path
)
4195 struct btrfs_key key
;
4196 struct btrfs_trans_handle
*trans
;
4199 ret
= avoid_extents_overwrite(root
->fs_info
);
4202 trans
= btrfs_start_transaction(root
, 1);
4203 if (IS_ERR(trans
)) {
4204 ret
= PTR_ERR(trans
);
4205 error("fail to start transaction %s", strerror(-ret
));
4208 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
4209 btrfs_release_path(path
);
4210 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4216 ret
= btrfs_del_item(trans
, root
, path
);
4220 if (path
->slots
[0] == 0)
4221 btrfs_prev_leaf(root
, path
);
4225 btrfs_commit_transaction(trans
, root
);
4227 error("failed to delete root %llu item[%llu, %u, %llu]",
4228 root
->objectid
, key
.objectid
, key
.type
, key
.offset
);
4230 printf("Deleted root %llu item[%llu, %u, %llu]\n",
4231 root
->objectid
, key
.objectid
, key
.type
, key
.offset
);
4236 * Main entry function to check known items and update related accounting info
4238 static int check_leaf_items(struct btrfs_root
*root
, struct btrfs_path
*path
,
4239 struct node_refs
*nrefs
, int account_bytes
)
4241 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4242 struct btrfs_key key
;
4243 struct extent_buffer
*eb
;
4246 struct btrfs_extent_data_ref
*dref
;
4251 eb
= path
->nodes
[0];
4252 slot
= path
->slots
[0];
4253 if (slot
>= btrfs_header_nritems(eb
)) {
4255 error("empty leaf [%llu %u] root %llu", eb
->start
,
4256 root
->fs_info
->nodesize
, root
->objectid
);
4262 btrfs_item_key_to_cpu(eb
, &key
, slot
);
4266 case BTRFS_EXTENT_DATA_KEY
:
4267 ret
= check_extent_data_item(root
, path
, nrefs
, account_bytes
);
4269 ret
= repair_extent_data_item(root
, path
, nrefs
, ret
);
4272 case BTRFS_BLOCK_GROUP_ITEM_KEY
:
4273 ret
= check_block_group_item(fs_info
, eb
, slot
);
4275 ret
& REFERENCER_MISSING
)
4276 ret
= delete_extent_tree_item(root
, path
);
4279 case BTRFS_DEV_ITEM_KEY
:
4280 ret
= check_dev_item(fs_info
, eb
, slot
);
4283 case BTRFS_CHUNK_ITEM_KEY
:
4284 ret
= check_chunk_item(fs_info
, eb
, slot
);
4286 ret
= repair_chunk_item(root
, path
, ret
);
4289 case BTRFS_DEV_EXTENT_KEY
:
4290 ret
= check_dev_extent_item(fs_info
, eb
, slot
);
4293 case BTRFS_EXTENT_ITEM_KEY
:
4294 case BTRFS_METADATA_ITEM_KEY
:
4295 ret
= check_extent_item(fs_info
, path
);
4298 case BTRFS_EXTENT_CSUM_KEY
:
4299 total_csum_bytes
+= btrfs_item_size_nr(eb
, slot
);
4302 case BTRFS_TREE_BLOCK_REF_KEY
:
4303 ret
= check_tree_block_backref(fs_info
, key
.offset
,
4306 ret
& (REFERENCER_MISMATCH
| REFERENCER_MISSING
))
4307 ret
= delete_extent_tree_item(root
, path
);
4310 case BTRFS_EXTENT_DATA_REF_KEY
:
4311 dref
= btrfs_item_ptr(eb
, slot
, struct btrfs_extent_data_ref
);
4312 ret
= check_extent_data_backref(fs_info
,
4313 btrfs_extent_data_ref_root(eb
, dref
),
4314 btrfs_extent_data_ref_objectid(eb
, dref
),
4315 btrfs_extent_data_ref_offset(eb
, dref
),
4317 btrfs_extent_data_ref_count(eb
, dref
));
4319 ret
& (REFERENCER_MISMATCH
| REFERENCER_MISSING
))
4320 ret
= delete_extent_tree_item(root
, path
);
4323 case BTRFS_SHARED_BLOCK_REF_KEY
:
4324 ret
= check_shared_block_backref(fs_info
, key
.offset
,
4327 ret
& (REFERENCER_MISMATCH
| REFERENCER_MISSING
))
4328 ret
= delete_extent_tree_item(root
, path
);
4331 case BTRFS_SHARED_DATA_REF_KEY
:
4332 ret
= check_shared_data_backref(fs_info
, key
.offset
,
4335 ret
& (REFERENCER_MISMATCH
| REFERENCER_MISSING
))
4336 ret
= delete_extent_tree_item(root
, path
);
4350 * @trans just for lowmem repair mode
4351 * @check all if not 0 then check all tree block backrefs and items
4352 * 0 then just check relationship of items in fs tree(s)
4354 * Returns >0 Found error, should continue
4355 * Returns <0 Fatal error, must exit the whole check
4356 * Returns 0 No errors found
4358 static int walk_down_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
4359 int *level
, struct node_refs
*nrefs
, int check_all
)
4361 enum btrfs_tree_block_status status
;
4364 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4365 struct extent_buffer
*next
;
4366 struct extent_buffer
*cur
;
4370 int account_file_data
= 0;
4372 WARN_ON(*level
< 0);
4373 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
4375 ret
= update_nodes_refs(root
, btrfs_header_bytenr(path
->nodes
[*level
]),
4376 path
->nodes
[*level
], nrefs
, *level
, check_all
);
4380 while (*level
>= 0) {
4381 WARN_ON(*level
< 0);
4382 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
4383 cur
= path
->nodes
[*level
];
4384 bytenr
= btrfs_header_bytenr(cur
);
4385 check
= nrefs
->need_check
[*level
];
4387 if (btrfs_header_level(cur
) != *level
)
4390 * Update bytes accounting and check tree block ref
4391 * NOTE: Doing accounting and check before checking nritems
4392 * is necessary because of empty node/leaf.
4394 if ((check_all
&& !nrefs
->checked
[*level
]) ||
4395 (!check_all
&& nrefs
->need_check
[*level
])) {
4396 ret
= check_tree_block_ref(root
, cur
,
4397 btrfs_header_bytenr(cur
), btrfs_header_level(cur
),
4398 btrfs_header_owner(cur
), nrefs
);
4401 ret
= repair_tree_block_ref(root
,
4402 path
->nodes
[*level
], nrefs
, *level
, ret
);
4405 if (check_all
&& nrefs
->need_check
[*level
] &&
4406 nrefs
->refs
[*level
]) {
4407 account_bytes(root
, path
, *level
);
4408 account_file_data
= 1;
4410 nrefs
->checked
[*level
] = 1;
4413 if (path
->slots
[*level
] >= btrfs_header_nritems(cur
))
4416 /* Don't forgot to check leaf/node validation */
4418 /* skip duplicate check */
4419 if (check
|| !check_all
) {
4420 ret
= btrfs_check_leaf(root
, NULL
, cur
);
4421 if (ret
!= BTRFS_TREE_BLOCK_CLEAN
) {
4429 ret
= process_one_leaf(root
, path
, nrefs
, level
);
4431 ret
= check_leaf_items(root
, path
,
4432 nrefs
, account_file_data
);
4436 if (check
|| !check_all
) {
4437 ret
= btrfs_check_node(root
, NULL
, cur
);
4438 if (ret
!= BTRFS_TREE_BLOCK_CLEAN
) {
4444 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
4445 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
4447 ret
= update_nodes_refs(root
, bytenr
, NULL
, nrefs
, *level
- 1,
4452 * check all trees in check_chunks_and_extent
4453 * check shared node once in check_fs_roots
4455 if (!check_all
&& !nrefs
->need_check
[*level
- 1]) {
4456 path
->slots
[*level
]++;
4460 next
= btrfs_find_tree_block(fs_info
, bytenr
, fs_info
->nodesize
);
4461 if (!next
|| !btrfs_buffer_uptodate(next
, ptr_gen
)) {
4462 free_extent_buffer(next
);
4463 reada_walk_down(root
, cur
, path
->slots
[*level
]);
4464 next
= read_tree_block(fs_info
, bytenr
, ptr_gen
);
4465 if (!extent_buffer_uptodate(next
)) {
4466 struct btrfs_key node_key
;
4468 btrfs_node_key_to_cpu(path
->nodes
[*level
],
4470 path
->slots
[*level
]);
4471 btrfs_add_corrupt_extent_record(fs_info
,
4472 &node_key
, path
->nodes
[*level
]->start
,
4473 fs_info
->nodesize
, *level
);
4479 ret
= check_child_node(cur
, path
->slots
[*level
], next
);
4484 if (btrfs_is_leaf(next
))
4485 status
= btrfs_check_leaf(root
, NULL
, next
);
4487 status
= btrfs_check_node(root
, NULL
, next
);
4488 if (status
!= BTRFS_TREE_BLOCK_CLEAN
) {
4489 free_extent_buffer(next
);
4494 *level
= *level
- 1;
4495 free_extent_buffer(path
->nodes
[*level
]);
4496 path
->nodes
[*level
] = next
;
4497 path
->slots
[*level
] = 0;
4498 account_file_data
= 0;
4500 update_nodes_refs(root
, (u64
)-1, next
, nrefs
, *level
, check_all
);
4505 static int walk_up_tree(struct btrfs_root
*root
, struct btrfs_path
*path
,
4509 struct extent_buffer
*leaf
;
4511 for (i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
4512 leaf
= path
->nodes
[i
];
4513 if (path
->slots
[i
] + 1 < btrfs_header_nritems(leaf
)) {
4518 free_extent_buffer(path
->nodes
[*level
]);
4519 path
->nodes
[*level
] = NULL
;
4526 * Insert the missing inode item and inode ref.
4528 * Normal INODE_ITEM_MISSING and INODE_REF_MISSING are handled in backref * dir.
4529 * Root dir should be handled specially because root dir is the root of fs.
4531 * returns err (>0 or 0) after repair
4533 static int repair_fs_first_inode(struct btrfs_root
*root
, int err
)
4535 struct btrfs_trans_handle
*trans
;
4536 struct btrfs_key key
;
4537 struct btrfs_path path
;
4538 int filetype
= BTRFS_FT_DIR
;
4541 btrfs_init_path(&path
);
4543 if (err
& INODE_REF_MISSING
) {
4544 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
4545 key
.type
= BTRFS_INODE_REF_KEY
;
4546 key
.offset
= BTRFS_FIRST_FREE_OBJECTID
;
4548 trans
= btrfs_start_transaction(root
, 1);
4549 if (IS_ERR(trans
)) {
4550 ret
= PTR_ERR(trans
);
4554 btrfs_release_path(&path
);
4555 ret
= btrfs_search_slot(trans
, root
, &key
, &path
, 1, 1);
4559 ret
= btrfs_insert_inode_ref(trans
, root
, "..", 2,
4560 BTRFS_FIRST_FREE_OBJECTID
,
4561 BTRFS_FIRST_FREE_OBJECTID
, 0);
4565 printf("Add INODE_REF[%llu %llu] name %s\n",
4566 BTRFS_FIRST_FREE_OBJECTID
, BTRFS_FIRST_FREE_OBJECTID
,
4568 err
&= ~INODE_REF_MISSING
;
4571 error("fail to insert first inode's ref");
4572 btrfs_commit_transaction(trans
, root
);
4575 if (err
& INODE_ITEM_MISSING
) {
4576 ret
= repair_inode_item_missing(root
,
4577 BTRFS_FIRST_FREE_OBJECTID
, filetype
);
4580 err
&= ~INODE_ITEM_MISSING
;
4584 error("fail to repair first inode");
4585 btrfs_release_path(&path
);
4590 * check first root dir's inode_item and inode_ref
4592 * returns 0 means no error
4593 * returns >0 means error
4594 * returns <0 means fatal error
4596 static int check_fs_first_inode(struct btrfs_root
*root
)
4598 struct btrfs_path path
;
4599 struct btrfs_key key
;
4600 struct btrfs_inode_item
*ii
;
4606 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
4607 key
.type
= BTRFS_INODE_ITEM_KEY
;
4610 /* For root being dropped, we don't need to check first inode */
4611 if (btrfs_root_refs(&root
->root_item
) == 0 &&
4612 btrfs_disk_key_objectid(&root
->root_item
.drop_progress
) >=
4613 BTRFS_FIRST_FREE_OBJECTID
)
4616 btrfs_init_path(&path
);
4617 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
4622 err
|= INODE_ITEM_MISSING
;
4624 ii
= btrfs_item_ptr(path
.nodes
[0], path
.slots
[0],
4625 struct btrfs_inode_item
);
4626 mode
= btrfs_inode_mode(path
.nodes
[0], ii
);
4627 if (imode_to_type(mode
) != BTRFS_FT_DIR
)
4628 err
|= INODE_ITEM_MISMATCH
;
4631 /* lookup first inode ref */
4632 key
.offset
= BTRFS_FIRST_FREE_OBJECTID
;
4633 key
.type
= BTRFS_INODE_REF_KEY
;
4634 /* special index value */
4637 ret
= find_inode_ref(root
, &key
, "..", strlen(".."), &index
);
4643 btrfs_release_path(&path
);
4646 err
= repair_fs_first_inode(root
, err
);
4648 if (err
& (INODE_ITEM_MISSING
| INODE_ITEM_MISMATCH
))
4649 error("root dir INODE_ITEM is %s",
4650 err
& INODE_ITEM_MISMATCH
? "mismatch" : "missing");
4651 if (err
& INODE_REF_MISSING
)
4652 error("root dir INODE_REF is missing");
4654 return ret
< 0 ? ret
: err
;
4658 * This function calls walk_down_tree and walk_up_tree to check tree
4659 * blocks and integrity of fs tree items.
4661 * @root: the root of the tree to be checked.
4662 * @account if NOT 0 means check the tree (including tree)'s treeblocks.
4663 * otherwise means check fs tree(s) items relationship and
4664 * @root MUST be a fs tree root.
4665 * Returns 0 represents OK.
4666 * Returns >0 represents error bits.
4668 static int check_btrfs_root(struct btrfs_root
*root
, int check_all
)
4670 struct btrfs_path path
;
4671 struct node_refs nrefs
;
4672 struct btrfs_root_item
*root_item
= &root
->root_item
;
4677 memset(&nrefs
, 0, sizeof(nrefs
));
4680 * We need to manually check the first inode item (256)
4681 * As the following traversal function will only start from
4682 * the first inode item in the leaf, if inode item (256) is
4683 * missing we will skip it forever.
4685 ret
= check_fs_first_inode(root
);
4691 level
= btrfs_header_level(root
->node
);
4692 btrfs_init_path(&path
);
4694 if (btrfs_root_refs(root_item
) > 0 ||
4695 btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
4696 path
.nodes
[level
] = root
->node
;
4697 path
.slots
[level
] = 0;
4698 extent_buffer_get(root
->node
);
4700 struct btrfs_key key
;
4702 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
4703 level
= root_item
->drop_level
;
4704 path
.lowest_level
= level
;
4705 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
4712 ret
= walk_down_tree(root
, &path
, &level
, &nrefs
, check_all
);
4716 /* if ret is negative, walk shall stop */
4718 ret
= err
| FATAL_ERROR
;
4722 ret
= walk_up_tree(root
, &path
, &level
);
4724 /* Normal exit, reset ret to err */
4731 btrfs_release_path(&path
);
4736 * Iterate all items in the tree and call check_inode_item() to check.
4738 * @root: the root of the tree to be checked.
4740 * Return 0 if no error found.
4741 * Return <0 for error.
4743 static int check_fs_root(struct btrfs_root
*root
)
4745 reset_cached_block_groups(root
->fs_info
);
4746 return check_btrfs_root(root
, 0);
4750 * Find the relative ref for root_ref and root_backref.
4752 * @root: the root of the root tree.
4753 * @ref_key: the key of the root ref.
4755 * Return 0 if no error occurred.
4757 static int check_root_ref(struct btrfs_root
*root
, struct btrfs_key
*ref_key
,
4758 struct extent_buffer
*node
, int slot
)
4760 struct btrfs_path path
;
4761 struct btrfs_key key
;
4762 struct btrfs_root_ref
*ref
;
4763 struct btrfs_root_ref
*backref
;
4764 char ref_name
[BTRFS_NAME_LEN
] = {0};
4765 char backref_name
[BTRFS_NAME_LEN
] = {0};
4771 u32 backref_namelen
;
4776 ref
= btrfs_item_ptr(node
, slot
, struct btrfs_root_ref
);
4777 ref_dirid
= btrfs_root_ref_dirid(node
, ref
);
4778 ref_seq
= btrfs_root_ref_sequence(node
, ref
);
4779 ref_namelen
= btrfs_root_ref_name_len(node
, ref
);
4781 if (ref_namelen
<= BTRFS_NAME_LEN
) {
4784 len
= BTRFS_NAME_LEN
;
4785 warning("%s[%llu %llu] ref_name too long",
4786 ref_key
->type
== BTRFS_ROOT_REF_KEY
?
4787 "ROOT_REF" : "ROOT_BACKREF", ref_key
->objectid
,
4790 read_extent_buffer(node
, ref_name
, (unsigned long)(ref
+ 1), len
);
4792 /* Find relative root_ref */
4793 key
.objectid
= ref_key
->offset
;
4794 key
.type
= BTRFS_ROOT_BACKREF_KEY
+ BTRFS_ROOT_REF_KEY
- ref_key
->type
;
4795 key
.offset
= ref_key
->objectid
;
4797 btrfs_init_path(&path
);
4798 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
4800 err
|= ROOT_REF_MISSING
;
4801 error("%s[%llu %llu] couldn't find relative ref",
4802 ref_key
->type
== BTRFS_ROOT_REF_KEY
?
4803 "ROOT_REF" : "ROOT_BACKREF",
4804 ref_key
->objectid
, ref_key
->offset
);
4808 backref
= btrfs_item_ptr(path
.nodes
[0], path
.slots
[0],
4809 struct btrfs_root_ref
);
4810 backref_dirid
= btrfs_root_ref_dirid(path
.nodes
[0], backref
);
4811 backref_seq
= btrfs_root_ref_sequence(path
.nodes
[0], backref
);
4812 backref_namelen
= btrfs_root_ref_name_len(path
.nodes
[0], backref
);
4814 if (backref_namelen
<= BTRFS_NAME_LEN
) {
4815 len
= backref_namelen
;
4817 len
= BTRFS_NAME_LEN
;
4818 warning("%s[%llu %llu] ref_name too long",
4819 key
.type
== BTRFS_ROOT_REF_KEY
?
4820 "ROOT_REF" : "ROOT_BACKREF",
4821 key
.objectid
, key
.offset
);
4823 read_extent_buffer(path
.nodes
[0], backref_name
,
4824 (unsigned long)(backref
+ 1), len
);
4826 if (ref_dirid
!= backref_dirid
|| ref_seq
!= backref_seq
||
4827 ref_namelen
!= backref_namelen
||
4828 strncmp(ref_name
, backref_name
, len
)) {
4829 err
|= ROOT_REF_MISMATCH
;
4830 error("%s[%llu %llu] mismatch relative ref",
4831 ref_key
->type
== BTRFS_ROOT_REF_KEY
?
4832 "ROOT_REF" : "ROOT_BACKREF",
4833 ref_key
->objectid
, ref_key
->offset
);
4836 btrfs_release_path(&path
);
4841 * Check all fs/file tree in low_memory mode.
4843 * 1. for fs tree root item, call check_fs_root()
4844 * 2. for fs tree root ref/backref, call check_root_ref()
4846 * Return 0 if no error occurred.
4848 int check_fs_roots_lowmem(struct btrfs_fs_info
*fs_info
)
4850 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
4851 struct btrfs_root
*cur_root
= NULL
;
4852 struct btrfs_path path
;
4853 struct btrfs_key key
;
4854 struct extent_buffer
*node
;
4859 btrfs_init_path(&path
);
4860 key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
4862 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4864 ret
= btrfs_search_slot(NULL
, tree_root
, &key
, &path
, 0, 0);
4868 } else if (ret
> 0) {
4874 node
= path
.nodes
[0];
4875 slot
= path
.slots
[0];
4876 btrfs_item_key_to_cpu(node
, &key
, slot
);
4877 if (key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
4879 if (key
.type
== BTRFS_ROOT_ITEM_KEY
&&
4880 fs_root_objectid(key
.objectid
)) {
4881 if (key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
4882 cur_root
= btrfs_read_fs_root_no_cache(fs_info
,
4885 key
.offset
= (u64
)-1;
4886 cur_root
= btrfs_read_fs_root(fs_info
, &key
);
4889 if (IS_ERR(cur_root
)) {
4890 error("Fail to read fs/subvol tree: %lld",
4896 ret
= check_fs_root(cur_root
);
4899 if (key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
4900 btrfs_free_fs_root(cur_root
);
4901 } else if (key
.type
== BTRFS_ROOT_REF_KEY
||
4902 key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
4903 ret
= check_root_ref(tree_root
, &key
, node
, slot
);
4907 ret
= btrfs_next_item(tree_root
, &path
);
4917 btrfs_release_path(&path
);
4922 * Low memory usage version check_chunks_and_extents.
4924 int check_chunks_and_extents_lowmem(struct btrfs_fs_info
*fs_info
)
4926 struct btrfs_path path
;
4927 struct btrfs_key old_key
;
4928 struct btrfs_key key
;
4929 struct btrfs_root
*root1
;
4930 struct btrfs_root
*root
;
4931 struct btrfs_root
*cur_root
;
4935 root
= fs_info
->fs_root
;
4937 root1
= root
->fs_info
->chunk_root
;
4938 ret
= check_btrfs_root(root1
, 1);
4941 root1
= root
->fs_info
->tree_root
;
4942 ret
= check_btrfs_root(root1
, 1);
4945 btrfs_init_path(&path
);
4946 key
.objectid
= BTRFS_EXTENT_TREE_OBJECTID
;
4948 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4950 ret
= btrfs_search_slot(NULL
, root1
, &key
, &path
, 0, 0);
4952 error("cannot find extent tree in tree_root");
4957 btrfs_item_key_to_cpu(path
.nodes
[0], &key
, path
.slots
[0]);
4958 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
)
4961 key
.offset
= (u64
)-1;
4963 if (key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
4964 cur_root
= btrfs_read_fs_root_no_cache(root
->fs_info
,
4967 cur_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
4968 if (IS_ERR(cur_root
) || !cur_root
) {
4969 error("failed to read tree: %lld", key
.objectid
);
4973 ret
= check_btrfs_root(cur_root
, 1);
4976 if (key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
4977 btrfs_free_fs_root(cur_root
);
4979 btrfs_release_path(&path
);
4980 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
4981 &old_key
, &path
, 0, 0);
4985 ret
= btrfs_next_item(root1
, &path
);
4992 ret
= end_avoid_extents_overwrite(fs_info
);
4997 reset_cached_block_groups(fs_info
);
4998 /* update block accounting */
4999 ret
= repair_block_accounting(fs_info
);
5003 err
&= ~BG_ACCOUNTING_ERROR
;
5006 btrfs_release_path(&path
);