Commit Graph

251 Commits (de34a33a8204af9e675e5247c88eb727955beae1)

Author SHA1 Message Date
Qu Wenruo 64f3dc0136 btrfs-progs: disk-io: Fix read_extent_data() error handler for missing device
When device is missing, read_extent_data() (function exported from old
btrfs check code) has the following problems:

1) Modifies @len parameter if device is missing
   If device returned in @multi is missing, @len can be larger than
   @max_len (originl length).

   This could confuse caller and underflow in the read loop.

2) Still returns 0 for missing device
   It only handles read error, missing device is not handled and 0 is
   returned.

3) Wrong check for device->fd
   In fact, 0 is also a valid fd.
   Although not possible under most cases, but still needs fix.

Fix them all.

Fixes: 1bad2f2f2d ("Btrfs-progs: fsck: add an option to check data csums")
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-03-30 22:15:55 +02:00
Rosen Penev e4df433b8a btrfs-progs: treewide: Replace strerror(errno) with %m.
As btrfs is specific to Linux, %m can be used instead of strerror(errno)
in format strings. This has some size reduction benefits for embedded
systems.

glibc, musl, and uclibc-ng all support %m as a modifier to printf.
A quick glance at the BIONIC libc source indicates that it has
support for %m as well. BSDs and Windows do not but I do believe
them to be beyond the scope of btrfs-progs.

Compiled sizes on Ubuntu 16.04:

Before:
3916512 btrfs
233688  libbtrfs.so.0.1
4899    bcp
2367672 btrfs-convert
2208488 btrfs-corrupt-block
13302   btrfs-debugfs
2152160 btrfs-debug-tree
2136024 btrfs-find-root
2287592 btrfs-image
2144600 btrfs-map-logical
2130760 btrfs-select-super
2152608 btrfstune
2131760 btrfs-zero-log
2277752 mkfs.btrfs
9166    show-blocks

After:
3908744 btrfs
233256  libbtrfs.so.0.1
4899    bcp
2366560 btrfs-convert
2207432 btrfs-corrupt-block
13302   btrfs-debugfs
2151104 btrfs-debug-tree
2134968 btrfs-find-root
2281864 btrfs-image
2143536 btrfs-map-logical
2129704 btrfs-select-super
2151552 btrfstune
2130696 btrfs-zero-log
2276272 mkfs.btrfs
9166    show-blocks

Total savings: 23928 (24 kilo)bytes

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-01-31 15:14:03 +01:00
Nikolay Borisov 90a16b10b3 btrfs-progs: Document logic of btrfs_read_dev_super
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2018-01-31 15:14:01 +01:00
Nikolay Borisov 8075fd4da1 btrfs-progs: Replace usage of list_for_each with list_for_each_entry
There are a couple of places where instead of the more succinct
list_for_each_entry the code uses list_for_each. This results in
slightly more code with no additional benefit as well as no
coherent pattern. This patch makes the code uniform. No functional
changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
[ remove unused variable in uuid_search ]
Signed-off-by: David Sterba <dsterba@suse.com>
2018-01-31 15:14:01 +01:00
David Sterba c6487a7d1d btrfs-progs: handle transaction start failure in close_ctree
Closing the fs will try to commit a pending transaction, but may fail to
do so if the filesystem state is not well defined. This will eg.  fail
for some fuzz tests. The data structures are freed but no furhter
attempt to commit is made.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba f2b0cbe8e8 btrfs-progs: move transaction code out of disk-io
Temporarily export the low-level helpers.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba 448999d84d btrfs-progs: add crude error handling when transaction start fails
Currently transaction bugs out insided btrfs_start_transaction in case
of error, we want to lift the error handling to the callers. This patch
adds the BUG_ON anywhere it's been missing so far. This is not the best
way of course. Transforming BUG_ON to a proper error handling highly
depends on the caller and should be dealt with case by case.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba 9b47f18aec btrfs-progs: handle blockgroup errors when opening filesystem
The call to btrfs_read_block_groups could loop if the metadata are
damaged (reported eg. for an unaligned block), due to lack of error
handling. We have to check for restored images or currently created
filesystems, that do not contain the blockgroups.

Can be reproduced by fuzzed image bko-155551.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=155551
Reported-by: Lukas Lueg <lukas.lueg@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba 145a5969bd btrfs-progs: drop blocksize argument from readahead_tree_block
Tree blocks are always nodesize. As readahead is only an optimization,
exact size is not required and is only advisory.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba 2030f49751 btrfs-progs: drop blocksize argument from btrfs_find_create_tree_block
Metadata blocks are always nodesize. When reading the
superblock::sys_array, the actual size of data is fixed to 4k and
smaller than nodesize, but otherwise everything works as before.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba d1b968dcba btrfs-progs: drop redundant check of blocksize in read_tree_block
The tree blocks are supposed to be always of nodesize. Before the
parameter has been dropped, it was unlikely but possible to pass a
misaligned value.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba 98909c21d7 btrfs-progs: drop blocksize from read_tree_block
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
David Sterba 1fa9653dc8 btrfs-progs: drop local blocksize variables if they're nodesize
Prep work so we can drop the blocksize argument from several functions.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-08 16:15:05 +02:00
Qu Wenruo 5b4c9ccce9 btrfs-progs: Refactor btrfs_read_sys_array/chunk_tree to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:53:41 +02:00
Qu Wenruo a0d9de4e40 btrfs-progs: Refactor write_all_supers and its callers to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:53:28 +02:00
Qu Wenruo bb3ecd59f2 btrfs-progs: Refactor btrfs_readahead_tree_block to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:53:21 +02:00
Qu Wenruo 6aaf1b10e7 btrfs-progs: Refactor btrfs_find_tree_block to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:53:18 +02:00
Qu Wenruo b350e8fd76 btrfs-progs: Refactor read_extent_data to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:53:13 +02:00
Qu Wenruo f85fc6e378 btrfs-progs: Refactor write_tree_block to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:52:54 +02:00
Qu Wenruo 790b5950f3 btrfs-progs: Refactor write_and_map_eb to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:52:49 +02:00
Qu Wenruo 5c8d747cc6 btrfs-progs: Refactor csum_tree_block to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:52:41 +02:00
Qu Wenruo a30579b1a7 btrfs-progs: Refactor btrfs_num_copies to use btrfs_fs_info
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:52:15 +02:00
Qu Wenruo 3e611c6983 btrfs-progs: Refactor btrfs_map_block and its variants to use btrfs_fs_info
Just to keep the 1st paramter the same as kernel.

We can also save a few lines since the parameter is shorter now.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:52:09 +02:00
Qu Wenruo 8690c887d1 btrfs-progs: Refactor read_tree_block to get rid of btrfs_root
The only reasom read_tree_block() needs a btrfs_root parameter is to get
its node/sector size.

And long ago, I have already introduced a compactible interface,
read_tree_block_fs_info() to pass btrfs_fs_info instead of btrfs_root.

Since we have cleaned up all root->sector/node/stripesize users, we
should be OK to refactor read_tree_block() function.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:11 +02:00
Qu Wenruo fa5fd68021 btrfs-progs: Remove block size members in btrfs_root
Finally, we can get rid of per tree block size members now.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:11 +02:00
Qu Wenruo d2ed05c9ae btrfs-progs: Refactor block sizes users in disk-io.c
Since we have cached block sizes in fs_info, there is no need to specify
these sizes in btrfs_setup_root() function.

And refactor all root->sector/node/stripesize users in disk-io.c.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:10 +02:00
Qu Wenruo c4893f15ce btrfs-progs: Introduce sectorsize nodesize and stripesize members for
btrfs_fs_info

Just like what we do in kernel, since we will not support different
leaf/node/stripe size per tree, there is no need to store these block
sizes in btrfs_root.

This patch will introduce these block size members into btrfs_fs_info
structure, allowing us to convert such usage in later patches.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:10 +02:00
Qu Wenruo 3c7a04e592 btrfs-progs: Remove deprecated leafsize usage
Leafsize is deprecated for a long time, and kernel has already updated
ctree.h to rename sb->leafsize to sb->__unused_leafsize.

This patch will remove normal users of leafsize:
1) Remove leafsize member from btrfs_root structure
   Now only root->nodesize and root->sectorisze.
   No longer root->leafsize.

2) Remove @leafsize parameter from btrfs_setup_root() function
   Since no root->leafsize, no need for @leafsize parameter.

The remaining user of leafsize will be:
1) btrfs inspect-internal dump-super
   Reformat the "leafsize" output to "leafsize (deprecated)" and
   use le32_to_cpu() to do the cast manually.

2) mkfs
   We still need to set sb->__unused_leafsize to nodesize.
   Do the manual cast too.

3) convert
   Same as mkfs, these two superblock setup should be merged later

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:10 +02:00
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
Omar Sandoval 7fbb89d167 btrfs-progs: report I/O errors when closing the filesystem
If the final fsync() on the Btrfs device fails, we just swallow the
error and don't alert the user in any way. This was uncovered by xfstest
generic/405, which checks that mkfs fails when it encounters EIO.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:36:49 +01:00
David Sterba 16ce9d6069 btrfs-progs: remove unused function wait_on_tree_block_writeback
This used to be a kernel stub but has been removed from kernel long time
ago.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba b53af22c24 btrfs-progs: remove unused argument from write_and_map_eb
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 497f974294 btrfs-progs: opencode trivial helper btrfs_free_transaction
It also does not exist in kernel code.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
Qu Wenruo a2203246ae btrfs-progs: Introduce kernel sizes to cleanup large intermediate number
Large numbers like (1024 * 1024 * 1024) may cost reader/reviewer to
waste one second to convert to 1G.

Introduce kernel include/linux/sizes.h to replace any intermediate
number larger than 4096 (not including 4096) to SZ_*.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:45 +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
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
Omar Sandoval 441d8aea8f btrfs-progs: add OPEN_CTREE_INVALIDATE_FST flag
If this flag is passed to open_ctree(), we'll clear the
FREE_SPACE_TREE_VALID compat_ro bit. The kernel will then reconstruct
the free space tree the next time the filesystem is mounted.

Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-23 11:07:05 +01:00
Karel Zak dcc3df7e6d btrfs-progs: rename btrfs_scan_lblkid() to btrfs_scan_devices()
It seems like bad idea to use a library name (lblkid) within generic
function name. The currently used scanning library is implementation
detail and this detail should be hidden for rest of the code.

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2016-10-25 14:28:36 +02:00
David Sterba 805ac27248 btrfs-progs: fix printf format of sizeof on 32bit build
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-10 09:46:54 +02:00
David Sterba 5e96864dfd btrfs-progs: rename __setup_root and change to return void
Exported function should use a prefix.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-05 12:39:01 +02:00
David Sterba 6cca2ea9be btrfs-progs: more sanity checks in read_tree_block_fs_info
If blocksize is 0, it passes the IS_ALIGNED check but fails later as the
length of ebs will be zero.

Reported-by: Lukas Lueg <lukas.lueg@gmail.com>
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=169311
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 15:07:24 +02:00
Qu Wenruo b2fbec8be1 btrfs-progs: Remove unnecessary parameter to clear_extent_uptodate
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 15:07:23 +02:00
Qu Wenruo 50c8c995ea btrfs-progs: Return more meaningful value for btrfs_read_deve_super
btrfs_read_dev_super() only returns 0 or -1, which doesn't really help,
caller won't know if it's caused by bad superblock or superblock out of
range.

Return -errno if pread64() return -1, and return -EOF if none or part of
the super is read out, and return what check_super() returned.

So caller can get -EIO to catch real corrupted super blocks.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 15:07:22 +02:00
Domagoj Tršan ff9ee51677 btrfs-progs: change btrfs_csum_final result param type to u8
Signed-off-by: Domagoj Tršan <domagoj.trsan@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 11:35:50 +02:00
David Sterba 8a167ec579 btrfs-progs: catch invalid flags in open_ctree_fd
Signed-off-by: David Sterba <dsterba@suse.com>
2016-09-21 14:12:38 +02:00
David Sterba 1ef93ea863 btrfs-progs: handle errors from btrfs_alloc_path
All functions already return an error condition, so the callers should
expect that.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-09-21 14:12:38 +02:00
David Sterba 3f100c5251 btrfs-progs: better error report in btrfs_scan_fs_devices
Print the found sizes.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-09-21 11:49:48 +02:00
David Sterba db7b7cd95f btrfs-progs: remove stray newline from message in check_super
Signed-off-by: David Sterba <dsterba@suse.com>
2016-09-21 11:48:48 +02:00
Qu Wenruo 245cf06dfa btrfs-progs: check: do early check for read_tree_block
Although we have enhanced read_tree_block() from a lot of different
aspects, it lacks the early bytenr/blocksize alignment check.

And the lack of such check can lead to strange use-after-free bugs, due
to the fact that alloc_extent_buffer() will free overlapping extent
buffers, and allocate new eb for the usage.

So we should not allow invalid bytenr/blocksize even passed to
btrfs_find_create_tree_block().

This patch will add such check so we won't trigger use-after-free bug
then.

Reported-by: Lukas Lueg <lukas.lueg@gmail.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-09-05 10:04:24 +02:00