Commit Graph

142 Commits (59e067ada7e8af5894a9f865a2c8d65be57746c3)

Author SHA1 Message Date
Qu Wenruo 02b58051e6 btrfs-progs: Refactor find_next_chunk to get rid of parameter root and objectid
Function find_next_chunk() is used to find next chunk start position,
which should only do search on chunk tree and objectid is set to
BTRFS_FIRST_CHUNK_TREE_OBJECTID.

So refactor the parameter list to get rid of @root, which should be
obtained from fs_info->chunk_root, and @objectid, which is set to
BTRFS_FIRST_CHUNK_TREE_OBJECTID.

Signed-off-by: Qu Wenruo <quwenruo.btrfs@gmx.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-09-25 15:17:48 +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
Nikolay Borisov 960870cc57 btrfs-progs: Use already defined BTRFS_BLOCK_GROUP_PROFILE_MASK
Instead of opencoding it. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-08-24 19:06:54 +02:00
Justin Maggard e2fd79c243 btrfs-progs: Fix an infinite loop in btrfs_next_bg
I've run into a couple filesystems where btrfs-find-root would spin
indefinitely.

If the first cache extent start location is 0, we end up in an infinite
loop in btrfs_next_bg().  Fix it by checking for that situation, and
jumping to the next bg if necessary.

Fixes: e2e0dae9 (btrfs-progs: volume: Fix a bug causing btrfs-find-root to skip first chunk)
Signed-off-by: Justin Maggard <jmaggard@netgear.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-20 17:43:43 +02:00
Qu Wenruo 0544aafcbf btrfs-progs: Refactor chunk creation functions to use btrfs_fs_info
4 functions are involved in this refactor: btrfs_make_block_group()
btrfs_make_block_groups(), btrfs_alloc_chunk, btrfs_alloc_data_chunk().

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:54:16 +02:00
Qu Wenruo 9b3959d72a btrfs-progs: Refactor btrfs_add_device() to use btrfs_fs_info
BTW, there is a duplicated definition of btrfs_add_device() in
volumes.h, also remove it.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-12 17:53:54 +02:00
Qu Wenruo 7a36a1216b btrfs-progs: Refactor btrfs_chunk_readonly 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:50 +02:00
Qu Wenruo 37ddab31fd btrfs-progs: Refactor btrfs_add_system_chunk 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:44 +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 de5d0cea24 btrfs-progs: Refactor btrfs_find_device 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:36 +02:00
Qu Wenruo 505639ee62 btrfs-progs: Refactor btrfs_check_chunk_valid 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:33 +02:00
Qu Wenruo 824a300ec8 btrfs-progs: Refactor btrfs_next_bg 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:52:37 +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 6e7b1bbdec btrfs-progs: Introduce function to get correct stripe length
Introduce a new function, btrfs_get_chunk_stripe_len() to get correct
stripe length.
This is very handy for lowmem mode, which checks the mapping between
device extent and chunk item.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-03 13:35:11 +02:00
Qu Wenruo 383b2b9c56 btrfs-progs: Enhance chunk item validation check
btrfs_check_chunk_valid() doesn't check if
1) chunk flag has conflicting flags
   For example chunk type DATA|METADATA|RAID1|RAID10 is completely
   invalid, while current check_chunk_valid() can't detect it.
2) num_stripes is invalid for RAID10
   Num_stripes 5 is not valid for RAID10.

This patch will enhance btrfs_check_chunk_valid() to handle above cases.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-03 13:35:11 +02:00
Qu Wenruo 5e2a2acc9a btrfs-progs: raid56: Introduce raid56 header for later recovery usage
Introduce a new header, kernel-lib/raid56.h, for later raid56 works.

It contains 2 functions, from original btrfs-progs code:
void raid6_gen_syndrome(int disks, size_t bytes, void **ptrs);
int raid5_gen_result(int nr_devs, size_t stripe_len, int dest, void **data);

Will be expanded later and some part of it(RAID6 recover part) may keep
sync with kernel later.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[ unify gpl header, rename header macro ]
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-03 13:35:11 +02:00
Qu Wenruo aca37c7915 btrfs-progs: Refactor sectorsize users in volumes.c
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
2017-07-03 13:35:10 +02:00
Qu Wenruo 8f41197b92 btrfs-progs: Cleanup open-coded btrfs_chunk_item_size
In btrfs_check_chunk_valid() we calculate chunk item using open code,
use an existing helper btrfs_chunk_item_size() instead.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-07-03 13:35:10 +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 bcafc565f3 btrfs-progs: remove unused argument from btrfs_add_system_chunk
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 16c21852f1 btrfs-progs: find_free_dev_extent() closer to kernel code
This solves an ENOSPC issue with nearly full filesystems.

The only things missing from the function is contains_pending_extent()
which should not be required in this case.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:35 +01:00
Qu Wenruo c00278a9e8 btrfs-progs: volumes: Use new raid5_gen_result to calculate raid5 parity
Use thew raid5_gen_result() function to calculate raid5 parity.

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
Qu Wenruo 388cdce509 btrfs-progs: Fix memory leak in write_raid56_with_parity
Ebs and pointers are allocated, but if any of the allocation failed, we
should free the allocated memory.

Resolves-Coverity-CID: 1374101
Resolves-Coverity-CID: 1374100
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-11 16:23:30 +01:00
David Sterba 92576929fb btrfs-progs: fix unaligned u64 access in btrfs_alloc_data_chunk
The key.offset member is not well-aligned as the key is packed, use a
temporary variable to pass the argument. Reported by ASAN in misc test
002.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-11-09 13:47:34 +01:00
Qu Wenruo 4d958295d2 btrfs-progs: volumes: Remove BUG_ON in raid56 write routine
Remove various BUG_ON in raid56 write routine, including:
1) Memory allocation error
   Old codes allocates memory when code needs new memory in a loop, and
   catch the error using BUG_ON().
   New codes allocates memory in a allocation loop first, if any failure
   is caught, freeing already allocated memories then throw -ENOMEM.

2) Write error
   Change BUG_ON() to correct return value.

3) Validation check
   Same as write error.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-25 14:28:25 +02:00
David Sterba ea0fd88b5a btrfs-progs: don't access freed memory in btrfs_close_devices
Caught by ASAN, as we don't access fs_devices directly, there's no null
deref but 0x30 is still wrong.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-05 12:39:01 +02:00
David Sterba 890f4a61d7 btrfs-progs: cleanup, kill trivial btrfs_key_type helper
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 11:33:15 +02:00
David Sterba df36cbdad2 btrfs-progs: improve error handling in btrfs_alloc_data_chunk
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 11:33:15 +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
Qu Wenruo 2f242115d1 btrfs-progs: Do extra chunk check before processing chunk item
Current we only do chunk validation check at mount time.

It's good for most case, but for fuzzed or manually crafted images, we
can insert a CHUNK_ITEM key into root tree.

Since mount time check will only check chunk tree, it will not check
CHUNK_ITEM in root tree.

Even with previous key type check against leaf owner, it is still
possible to modify the leaf owner to by-pass it.

So we still need to check chunk validation before processing it.

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:16 +02:00
David Sterba 059832da5f btrfs-progs: make superblock reading/scanning api more generic
We'll add more modes that affect scanning.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-08-24 14:36:58 +02:00
Justin Maggard c52762b4c6 btrfs-progs: fix memory leak with missing device
In read_one_chunk(), we may add an empty entry for a missing device.
However, this entry wasn't being added to the dev_list, and so it never
got freed.

Signed-off-by: Justin Maggard <jmaggard@netgear.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-07-29 15:04:54 +02:00
David Sterba 24f1713777 btrfs-progs: fix unaligned access calculating raid56 data
The extent_buffer::data might be unaligned wrt unsigned long, depends on
acutal layout of the structure and width of the int types. Use explicit
unaligned access helpers.

Reported-by: Anatoly Pugachev <matorola@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-07-28 14:08:30 +02:00
Tsutomu Itoh dabd283583 btrfs-progs: add the error message when open fails
When open in btrfs_open_devices failed, only the following message is
displayed. Therefore the user doesn't understand the reason why open
failed.

  # btrfs check /dev/sdb8
  Couldn't open file system

This patch adds the error message when open fails.

Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-07-13 18:44:52 +02:00
Qu Wenruo f07c814971 btrfs-progs: Introduce function to create convert data chunks
Introduce new function, make_convert_data_chunks(), to build up data
chunks for convert.

It will call a modified version of btrfs_alloc_data_chunk() to force
data chunks to covert all known ext* data.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-07 18:15:19 +02:00
Nicholas D Steeves bd2cc320af btrfs-progs: typo review of strings and comments
Signed-off-by: Nicholas D Steeves <nsteeves@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-01 14:56:56 +02:00
Liu Bo 9988284574 btrfs-progs: add validation checks for chunk loading
To survive fuzz filesystem images, we need various validation checks to
make btrfsck detect any invalid value inside chunks including those in
 sys_array.

Note that these checks may not be sufficient to cover all corner cases,
we may need to add more later.

This also refractor previous various checks into a helper function so
that we can add more checks into it in the future.

Reported-by: Vegard Nossum <vegard.nossum@oracle.com>
Reported-by: Quentin Casasnovas <quentin.casasnovas@oracle.com>
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-05-11 16:37:23 +02:00
David Sterba 5a3cbe16db btrfs-progs: don't print message for a missing device
The message

	"warning devid %llu not found already\n",

does not seem to be too useful, it appears during several commands and
sometimes repeatedly.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 13:42:47 +01:00
Qu Wenruo 9db13dca2f btrfs-progs: Add support for tree block operations on fs_info without roots
Since open_ctree_fs_info() now may return a fs_info even without any
roots, modify functions like read_tree_block() to operate with such
fs_info.

This provides the basis for btrfs-find-root to operate on chunk tree
with corrupted fs.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[ coding style adjustments, unified declarations ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-02-26 17:27:58 +01:00
Qu Wenruo e2e0dae926 btrfs-progs: volume: Fix a bug causing btrfs-find-root to skip first chunk
There is a small bug from 2011, where btrfs_next_bg (formally
btrfs_next_metadata) function will always skip the first chunk.

That's OK for that time, as there is always 3 empty temporary chunks.
But now, we may ended up with only one metadata or system chunk, with
empty chunk auto-remove from kernel or new mkfs.btrfs.

So fix it by checking the initial value so btrfs_next_bg() will return
the first chunk if its *logical parameter is 0.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-02-26 17:27:58 +01:00
Qu Wenruo 61d6ed4fe7 btrfs-progs: Enhance chunk validation check
Enhance chunk validation:
1) Num_stripes
   We already have such check but it's only in super block sys chunk
   array.
   Now check all on-disk chunks.

2) Chunk logical
   It should be aligned to sector size.
   This behavior should be *DOUBLE CHECKED* for 64K sector size like
   PPC64 or AArch64.
   Maybe we can found some hidden bugs.

3) Chunk length
   Same as chunk logical, should be aligned to sector size.

4) Stripe length
   It should be power of 2.

5) Chunk type
   Any bit out of TYPE_MAS | PROFILE_MASK is invalid.

With all these much restrict rules, several fuzzed image reported in
mail list should no longer cause btrfsck error.

Reported-by: Vegard Nossum <vegard.nossum@oracle.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:01:04 +01:00
David Sterba 9bb3e6bc3c btrfs-progs: verbose error when we find an unexpected item in sys_array
Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-04 11:28:24 +01:00
David Sterba 81e43dabc8 btrfs-progs: handle invalid num_stripes in sys_array
We can handle the special case of num_stripes == 0 directly inside
btrfs_read_sys_array. The BUG_ON in btrfs_chunk_item_size is there to
catch other unhandled cases where we fail to validate external data,
like in btrfs-show-super.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-04 11:28:24 +01:00
David Sterba d9463cfeaa btrfs-progs: add more checks to btrfs_read_sys_array
Port of kernel commit e3540eab29e1b2260bc4b9b3979a49a00e3e3af8

Verify that the sys_array has enough bytes to read the next item.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-04 11:28:24 +01:00
David Sterba 22d9c7091b btrfs-progs: cleanup, rename a few variables in btrfs_read_sys_array
Port of kernel commit 1ffb22cf8c322bbfea6b35fe23d025841b49fede

There's a pointer to buffer, integer offset and offset passed as
pointer, try to find matching names for them.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-04 11:28:24 +01:00
David Sterba 9c655bfc1c btrfs-progs: update btrfs_read_sys_array to match kernel implementation
There are some sanity checks missing on both sides, kernel/userspace.
Preparation to port the missing changes.

Sync code with parent of kernel commit
1ffb22cf8c322bbfea6b35fe23d025841b49fede ("btrfs: cleanup, rename a few
variables in btrfs_read_sys_array")

This effectively reverts progs commit
be96777126 ("btrfs-progs: Cleanup unneeded
extra variant in btrfs_read_sys_array") so we can apply more of the
kernel patches.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-04 11:28:24 +01:00
Qu Wenruo 2143084229 btrfs-progs: find-root: Add support to search chunk root
Add support to search chunk root, as we only need to search tree roots
in system chunk, which should be very easy to add, just iterate in
system chunks.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[ renamed to btrfs_next_bg_* ]
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-16 14:23:45 +01:00
David Sterba 7ae478d8af btrfs-progs: use on-stack buffer for btrfs_scan_one_device
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-13 18:24:30 +01:00