Commit Graph

346 Commits (d00a875ed2cba63637d768fdfab42bec20d989b4)

Author SHA1 Message Date
Lu Fengqi 528ba342ac btrfs-progs: tests: Fix fuzz-test for bko-161821.raw.txt
Fuzzed image bko-161821.raw causes btrfs check to get segmentation fault.

The function check_owner_ref attempts to access a non-exist quota tree
when dealing with extent_item [4198400 4096] in the corrupted filesystem.

The function btrfs_new_fs_info always allocates memory for
fs_info->quota_root regardless of whether quota_tree exists or not.
Additionally, the function btrfs_read_fs_root will directly return
fs_info->quota_root if location->objectid == BTRFS_QUOTA_TREE_OBJECTID.

This patch does the following things:
1. Do extra check and return ENOENT if quota tree does not exist in the
   function btrfs_read_fs_root.
2. Free useless fs_info->quota_root in the function btrfs_setup_all_roots
   to reduce confusion.
3. free_extent_buffer even if check_child_node failed in the function
   walk_down_tree.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-05-02 16:29:38 +02:00
Qu Wenruo f80b1f1db6 btrfs-progs: check: Fix memory leak in check_chunks_and_extents
fsck/003-shift-offsets makes valgrinds complaining about memory leaks.
==5910==
==5910== HEAP SUMMARY:
==5910==     in use at exit: 1,112 bytes in 11 blocks
==5910==   total heap usage: 161 allocs, 150 frees, 164,800 bytes allocated
==5910==
==5910== 216 (72 direct, 144 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 5
==5910==    at 0x4C2AF1F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5910==    by 0x4815A3: add_root_item_to_list (cmds-check.c:9683)
==5910==    by 0x481CE2: check_chunks_and_extents (cmds-check.c:9886)
==5910==    by 0x48888B: cmd_check (cmds-check.c:12977)
==5910==    by 0x40A8C5: main (btrfs.c:246)
==5910==

The check_chunks_and_extents() memory leaks are caused by not freeing
added root items of normal_trees and dropping_trees.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-05-02 16:27:56 +02:00
Qu Wenruo ad60ed92d1 btrfs-progs: check: lowmem, fix false alert about backref lost for SHARED_DATA_REF
In check_extent_data_item(), after checking extent item of one data
extent, we search inlined data backref, then EXTENT_DATA_REF_KEY.

But we didn't search SHARED_DATA_REF, so if the backref is
SHARED_DATA_REF, then we will raise a false alert about backref lost.

Fix by also checking SHARED_DATA_REF_KEY in check_extent_data_item().

Reported-by: Chris Murphy <chris@colorremedies.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-31 14:17:48 +02:00
Su Yue eafdeff3e3 btrfs-progs: check: enhance return values of walk_down_tree_v2
In lowmem mode, 'walk_down_tree_v2' returns negative values wheather
the error is fatal or not. It causes the loop where 'walk_down_tree_v2'
is to break even the error is tolerated and then subsequent nodes process
will be skipped.

Fix it by redefining meanings of values 'walk_down_tree_v2' returns.
Do a similar fix for 'process_one_leaf_v2'.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:49 +01:00
Qu Wenruo 3aed1a1ced btrfs-progs: check: lowmem: Fix silent error if first inode item missing
If first inode item is missing, lowmem check will detect it but does not
output any error message.

Add error message for it.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:49 +01:00
Qu Wenruo 7fe1e45258 btrfs-progs: check: lowmem: Fix false alert on inline compressed extent
Old lowmem check doesn't check if the inline extent is compressed and
always checks extent numbytes against inline item size.

And when it finds the extent numbytes mismatch with inline item size it
doesn't output any error message, just return error silently, making it
quite hard to debug.

Fix it by only checking extent numbytes against inline item size when
the extent is not compressed, and output error message.

Reported-by: Christoph Anton Mitterer <calestyo@scientia.net>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
Qu Wenruo 4a749360cd btrfs-progs: check: lowmem: Fix extent item size false alert
If one extent item has no inline ref, btrfs lowmem mode check can give
false alert without outputting any error message.

The problem is lowmem mode always assumes that extent item has inline
refs, and when it encounters such case it flags the extent item has
wrong size, but doesn't output the error message.

Although we already have such image submitted, at the commit time due to
another bug in cmds-check return value, it doesn't detect it until that
bug is fixed.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
Qu Wenruo eec058075f btrfs-progs: check: lowmem: Fix false alert in checking data extent csums
Btrfs lowmem check can report false csum error like:
ERROR: root 5 EXTENT_DATA[257 0] datasum missing
ERROR: root 5 EXTENT_DATA[257 4096] prealloc shouldn't have datasum

This is because lowmem check code always compare the found csum size
with the whole extent which data extents points to.

Normally it's OK, but when prealloc extent is written, or reflink is
done, data extent can points to part of a larger extent, making the csum
check wrong.

To fix it, the csum check part is modified to handle plain and
compressed extents in different ways:

1) Plain extent
   Only search csums for the range it refers to.
   So the search range is from (disk_bytenr + extent_offset) and search
   length is (extent_num_bytes)

2) Compressed extent
   Search the whole extent.
   Search range is from (disk_bytner) and search length is
   (disk_num_bytes)

Reported-by: Chris Murphy <chris@colorremedies.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
Qu Wenruo 18b668d6d5 btrfs-progs: check: Output verbose error when fsck found a bug in any tree
Although we output error like "errors found in extent allocation tree or
chunk allocation", but we lacks such output for other trees, but leaving
the final "found error is %d" to catch the last return value(and
sometime it's cleared)

This patch adds extra error message for top level error path, and modify
the last "found error is %d" to "error(s) found" or "no error found".

Cc: Christoph Anton Mitterer <calestyo@scientia.net>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
Qu Wenruo dd4c7680db btrfs-progs: check: lowmem: Fix several bugs related to afterward search
Since btrfs_search_slot() can point to the slot which is beyond the
leaves' capacity, we should pay extra attention when doing afterward
search.

While for lowmem check, several places uses afterward search:
1) Block group item used space check
2) Device item used space check
3) Data extent backref check.

In the following case for block group item check, btrfs lowmem mode
check will skip the block group and report false alert:

leaf 29405184 items 37 free space 1273 generation 11 owner 2
...
        item 36 key (77594624 EXTENT_ITEM 2097152)
                extent refs 1 gen 8 flags DATA
                extent data backref root 5 objectid 265 offset 0 count 1
leaf 29409280 items 43 free space 670 generation 11 owner 2
        item 0 key (96468992 EXTENT_ITEM 2097152)
                extent refs 1 gen 8 flags DATA
                extent data backref root 5 objectid 274 offset 0 count 1
        item 1 key (96468992 BLOCK_GROUP_ITEM 33554432)
                block group used 2265088 chunk_objectid 256 flags DATA

When checking block group item, we will search key (96468992 0 0) to
start from the first item in the block group.

While search_slot() will point to leaf 29405184, slot 37 which is beyond
leaf capacity.

And when reading key from slot 37, uninitialized data can be read out
and cause us to exit block group item check, leading to false alert.

Fix it by checking path.slot[0] before reading out the key.

Reported-by: Christoph Anton Mitterer <calestyo@scientia.net>
Reported-by: Chris Murphy <chris@colorremedies.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba 134f8331b2 btrfs-progs: check: remove unused argument from maybe_repair_root_item
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba 61c39c5fae btrfs-progs: check: remove unused argument from process_duplicates
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba d10e5b2155 btrfs-progs: check: remove unused argument from calc_extent_flag
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba 9f3091ba7c btrfs-progs: check: remove unused argument from fix_item_offset
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba 2cb94a9123 btrfs-progs: check: remove unused argument from delete_bogus_item
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba 4b9e29b9d0 btrfs-progs: check: remove unused argument from fix_key_order
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba e69d5a7b07 btrfs-progs: check: remove unused argument from free_extent_record_cache
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba adbb2b4371 btrfs-progs: check: remove unused argument from create_inode_item
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba ea62fb1e62 btrfs-progs: check: remove unused arguments from delete_dir_index
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba cc2b0e5989 btrfs-progs: check: remove unused argument from check_child_node
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:48 +01:00
David Sterba e9ca9978b2 btrfs-progs: check: remove unused argument from process_dir_item
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 0acce60a28 btrfs-progs: remove unused argument from clear_extent_dirty
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba e1a67a59a9 btrfs-progs: remove unused argument from set_extent_dirty
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 6e1510e0fe btrfs-progs: remove unused argument from btrfs_csum_data
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 886a8565e0 btrfs-progs: move ulist.[ch] to kernel-shared
The implementation of ulist_* is same for kernel and userspace, without
dependencies, so we can keep it separately for code sync.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
Goldwyn Rodrigues 2db52a6a48 btrfs-progs: Remove unused function arg in delete_extent_records
new_len is not used in delete_extent_records().

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 99063d88af btrfs-progs: drop unused argument from btrfs_del_ptr
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 1c880f34f1 btrfs-progs: move help defines to own header
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:45 +01:00
Qu Wenruo b73d67f20a btrfs-progs: check: fix false alert on dropped leaf in lowmem mode
For btrfs-progs test case 021-partially-dropped-snapshot-case, if the
first leaf is already dropped, btrfs check low-memory mode will report
false alert:

checking fs roots
checksum verify failed on 29917184 found E4E3BDB6 wanted 00000000
checksum verify failed on 29917184 found E4E3BDB6 wanted 00000000
checksum verify failed on 29917184 found E4E3BDB6 wanted 00000000
checksum verify failed on 29917184 found E4E3BDB6 wanted 00000000

This is caused by we are calling check_fs_first_inode() function,
unlike the rest part of check_fs_root_v2(), it doesn't have enough check
on dropping progress, and caused the false alert.

Fix it by checking dropping progress before searching slot.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-01-25 09:48:03 +01:00
Goldwyn Rodrigues e325c74bb8 btrfs-progs: check: get the highest inode for lost+found
root->highest_inode is not accurate at the time of creating a lost+found
and it fails because the highest_inode+1 is already present. This could be
because of fixes after highest_inode is set. Instead, search
for the highest inode in the tree and use it for lost+found.

This makes root->highest_inode unnecessary and hence deleted.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-01-25 09:47:19 +01:00
Qu Wenruo e27189a391 btrfs-progs: check: Fix false alert on generation mismatch for tree reloc tree
For fs with tree reloc tree(under balancing), lowmem mode will report
false alert like:
ERROR: extent[62914560 4096] backref generation mismatch, wanted: <=9,
have: 13

This is because lowmem mode adds a more restrict check, to ensure
generation in fs tree won't be smaller than extent tree.

In fact such assumption is not right for tree reloc tree, so remove such
check.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:21:26 +01:00
Qu Wenruo b6aa956779 btrfs-progs: check: Fix lowmem false alert on tree reloc tree
Lowmem mode will report false alert if the fs has tree reloc tree like:
ERROR: shared extent[30011392 4096] lost its parent (parent: 30011392,
level: 1)

The problem is check_shared_block_backref() can't handle tree reloc
tree's self-pointing backref.

And still try to read out the tree block then seeking for the
referencer.

The correct method for it is to check if it's tree reloc root.
In that case, we should check found the ROOT_ITEM of tree reloc tree in
root tree.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:21:26 +01:00
Qu Wenruo 54c8f9152f btrfs-progs: check: Fix lowmem mode stack overflow caused by fsck/023
Lowmem mode fsck will overflow its stack since it will do infinite
backref check for tree reloc root.
We should not check backref if it's pointing to itself for tree reloc
root.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:21:26 +01:00
Qu Wenruo 0d8601ab6c btrfs-progs: check: Fix assert when using lowmem on fs with tree reloc tree
When using lowmem mode, btrfs check will report ASSERT for calling
btrfs_read_fs_root() on tree reloc tree.

Fix it by checking objectid and call btrfs_read_fs_root_no_cache() for
tree reloc tree.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:21:26 +01:00
Goldwyn Rodrigues 11315213aa btrfs-progs: Initialize ret to suppress compiler warning
The path that leaves ret unintialized goes through the second if block
and requires dback->found_ref to be 0. Quick search leads to several
places where it's set according to found items so we won't reach the for
loop with found_ref 0.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
[ updated changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:35 +01:00
Goldwyn Rodrigues 617af9e92e btrfs-progs: check: fix extents after finding all errors
Simplifying the logic of fixing.

Calling fixup_extent_ref() after encountering every error causes
more error messages after the extent is fixed. In case of multiple errors,
this is confusing because the error message is displayed after the fix
message and it works on stale data. It is best to show all errors and
then fix the extents.

Set a variable and call fixup_extent_ref() if it is set. err is not used,
so cleared it.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:35 +01:00
David Sterba 4e2275bd27 btrfs-progs: check: use on-stack path buffer in check_fs_roots_v2
We don't need to conserve stack space too much unlike kernel, also
remove one error condition.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
David Sterba e42f8c6a3c btrfs-progs: check: use on-stack path buffer in check_fs_root_v2
We don't need to conserve stack space too much unlike kernel, also
remove one error condition.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
David Sterba f73c5e075e btrfs-progs: check: use on-stack path buffer in check_fs_first_inode
We don't need to conserve stack space too much unlike kernel, also
remove one error condition.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
David Sterba dfbdde35fc btrfs-progs: make incompat bit wrappers more compact
Use the same macro tricks as in kernel code.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
David Sterba f6f9cd0cb7 btrfs-progs: use symbolic name for first inode number when searching
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
David Sterba 86d2e4b64b btrfs-progs: remove extra newline from messages
The common message helpers add the newline.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
Qu Wenruo a2883ea16d btrfs-progs: check: fix NULL pointer dereference for possible memory allocation failure
We didn't check 'path' allocated in check_root_ref(), which can cause
NULL pointer dereference if the memory allocation failed.

Fix it by using stack memory, since the function should return error
bitmap not minus error code, we don't want memory allocation to be an
exception.

Resolves-Coverity-CID: 1372510
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
Qu Wenruo 45f782abf3 btrfs-progs: fsck: Fix patch allocation check and leak in check_fs_first_inode
Allocated 'path' in check_fs_first_inode() is not checked and for
btrfs_search_slot() error, it will leak 'path'.

Fix it.

Resolves-Coverity-CID: 1374098
Resolves-Coverity-CID: 1374099
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
Qu Wenruo 8b125cdd7e btrfs-progs: check: Enhance leaf traversal function to handle missing inode item
The leaf traversal function in lowmem mode will skip to the first inode
item of leaf and begin to check the inode.

That's designed to avoid checking overlapping part of a leaf.

But that will cause problem in fsck/010 test case, as in that case inode
item of the first inode(256) is missing.
So above traversal will check from inode 257 and found nothing wrong.

The fix is done in 2 part:
1) Manually check the first inode
   To avoid case like fsck/010

2) Check inode if ino changes from the first ino of a leaf
   To avoid missing inode_item problem.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
Wang Xiaoguang 5e2dc77047 btrfs-progs: check: skip shared node or leaf check for low_memory mode
The basic idea is simple. Assume a middle tree node A is shared and
its referencing fs/file tree root ids are 5, 258 and 260, then we
only check node A in the tree who has the smallest root id. That means
in this case, when checking root tree(5), we check inode A, for root
tree 258 and 260, we can just skip it.

Notice even with this patch, we still may visit a shared node or leaf
multiple times. This happens when a inode metadata occupies multiple
leaves.

                 leaf_A     leaf_B
When checking inode item in leaf_A, assume inode[512] have file extents
in leaf_B, and leaf_B is shared. In the case, for inode[512], we must
visit leaf_B to have inode item checked. After finishing inode[512] check,
here we walk down tree root to leaf_B to check whether node or leaf
is shared, if some node or leaf is shared, we can just skip it and below
nodes or leaf's check.

I also fill a disk partition with linux source codes and create 3
snapshots in it. Before this patch, it averagely took 46s to finish one
btrfsck execution, with this patch, it averagely took 15s.

Signed-off-by: Wang Xiaoguang <wangxg.fnst@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:33 +01:00
Lu Fengqi d1e8227ceb btrfs-progs: check: fix the return value bug of cmd_check()
The function cmd_check() is called by the main function of btrfs.c, its
return value will be returned by exit(). Resulting in the loss of
significant bits in some cases, for example this value is greater than
0377. If use a bool value "err" to store all of the return value, this
will solve the problem.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:33 +01:00
Lu Fengqi 21ca38e7df btrfs-progs: check: introduce low_memory mode fs_tree check
Introduce a new function check_fs_roots_v2() for check fs_tree in
low_memory mode. It call check_fs_root_v2() to check fs_root, and call
check_root_ref() to check root_ref.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:33 +01:00
Lu Fengqi 2f94517bc4 btrfs-progs: check: introduce function to check root ref
Introduce a new function check_root_ref() to check
root_ref/root_backref.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:33 +01:00
Lu Fengqi 262425b650 btrfs-progs: check: introduce function to check fs root
Introduce a new function check_fs_root_v2() to check fs root,
and call check_inode_item to check the items in the tree.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:33 +01:00