Commit Graph

394 Commits

Author SHA1 Message Date
Naohiro Aota 2d3b31d604 btrfs-progs: use round_down for allocation calcs
Several calculations in the chunk allocation process use this pattern.

    x /= y;
    x *= y;

Replace this pattern with round_down().

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Naohiro Aota 907c5fd7a4 btrfs-progs: fix to use half the available space for DUP profile
In the DUP profile, we can use only half of the space available in a
device extent. Fix the calculation of calc_size for it.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Naohiro Aota 13a6cff8b6 btrfs-progs: rewrite btrfs_alloc_data_chunk() using create_chunk()
btrfs_alloc_data_chunk() and create_chunk() have the most part in common.
Let's rewrite btrfs_alloc_data_chunk() using create_chunk().

There are two differences between btrfs_alloc_data_chunk() and
create_chunk(). create_chunk() uses find_next_chunk() to decide the
logical address of the chunk, and it uses btrfs_alloc_dev_extent() to
decide the physical address of a device extent. On the other hand,
btrfs_alloc_data_chunk() uses *start for both logical and physical
addresses.

To support the btrfs_alloc_data_chunk()'s use case, we use ctl->start
and ctl->dev_offset. If these values are set (non-zero), use the
specified values as the address. It is safe to use 0 to indicate the
value is not set here. Because both lower addresses of logical

  (0..BTRFS_FIRST_CHUNK_TREE_OBJECT_ID) and physical
  (0..BTRFS_BLOCK_RESERVED_1M_FOR_SUPER) are reserved.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Naohiro Aota 1e344dc8cf btrfs-progs: factor out create_chunk()
Factor out create_chunk() from btrfs_alloc_chunk(). This new function
creates a chunk.

There is no functional changes.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Naohiro Aota 0e3865206e btrfs-progs: factor out decide_stripe_size()
Factor out decide_stripe_size() from btrfs_alloc_chunk(). This new
function calculates the actual stripe size to allocate and decides the
size of a stripe (ctl->calc_size).

This commit has no functional changes.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Naohiro Aota 3acac33e8c btrfs-progs: consolidate parameter initialization of regular allocator
Move parameter initialization code for regular allocator to
init_alloc_chunk_ctl_policy_regular(). This will help adding another
allocator in the future.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Naohiro Aota 605ffad6f0 btrfs-progs: convert type of alloc_chunk_ctl::type
Convert alloc_chunk_ctl::type to take the original type in
btrfs_alloc_chunk(). This will help refactoring in the following commits.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Naohiro Aota ff65b83306 btrfs-progs: refactor find_free_dev_extent_start()
Factor out the function dev_extent_search_start() from
find_free_dev_extent_start() to decide the starting position of a device
extent search.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Naohiro Aota 1da9fede64 btrfs-progs: introduce chunk allocation policy
Introduce chunk allocation policy for btrfs. This policy controls how
chunks and device extents are allocated from devices.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:44 +02:00
Nikolay Borisov 0595309541 btrfs-progs: fix null pointer deref in balance_level
In case the right buffer is emptied it's first set to NULL and
subsequently it's dereferenced to get its size to pass to root_sub_used.
This naturally leads to a NULL pointer dereference. The correct thing to
do is to pass the stashed right->len in "blocksize".

Issue: #296
Pull-request: #360
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-04-19 18:58:26 +02:00
Johannes Thumshirn 94b60b67a9 btrfs-progs: pass in fs_info to btrfs_csum_data
For passing authentication keys to the checksumming functions we need a
container for the key.

Pass in a btrfs_fs_info to btrfs_csum_data() so we can use the fs_info
as a container for the authentication key.

Note this is not always possible for all callers of btrfs_csum_data() so
we're just passing in NULL for now

Functions calling btrfs_csum_data() with a NULL fs_info argument are
currently not supported in the context of an authenticated file system.

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-03-24 22:20:19 +01:00
David Sterba 6bb6d1215d btrfs-progs: factor open_ctree parameters to a structure
Extending open_ctree with more parameters would be difficult, we'll need
to add more so factor out the parameters to a structure for easier
extension.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-03-24 22:20:19 +01:00
Dāvis Mosāns a2ccf96d76 btrfs-progs: fix checksum output for "checksum verify failed"
Currently only single checksum byte is printed.  Fix it so that
the whole checksum is printed, in the order as the bytes are stored in
the buffer. This matches what kernel does, though it might not
correspond to the cases of CRC32C and XXHASH as if they were stored in
integer variable and printed in the native format. For consistency we
need to print the same format.

Signed-off-by: Dāvis Mosāns <davispuh@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-03-15 14:59:40 +01:00
Marek Behún 08fb534eb7 btrfs-progs: do not fail when offset of a ROOT_ITEM is not -1
When the btrfs_read_fs_root() function is searching a ROOT_ITEM with
location key offset other than -1, it currently fails via BUG_ON.

The offset can have other value than -1, though. This can happen for
example if a subvolume is renamed:

  $ btrfs subvolume create X && sync
  Create subvolume './X'
  $ btrfs inspect-internal dump-tree /dev/root | grep -B 2 'name: X$
        location key (270 ROOT_ITEM 18446744073709551615) type DIR
        transid 283 data_len 0 name_len 1
        name: X
  $ mv X Y && sync
  $ btrfs inspect-internal dump-tree /dev/root | grep -B 2 'name: Y$
        location key (270 ROOT_ITEM 0) type DIR
        transid 285 data_len 0 name_len 1
        name: Y

As can be seen the offset changed from -1ULL to 0.

Do not fail in this case.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
CC: Qu Wenruo <wqu@suse.com>
CC: Tom Rini <trini@konsulko.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-19 15:24:42 +01:00
Su Yue be6710f89d btrfs-progs: print bytenr of child eb if mismatched level found in read_node_slot
If btrfs check reported like

  ERROR: child eb corrupted: parent bytenr=178081 item=246 parent level=1 child level=2

It's hard to find which eb is corrupted without bytenr in dump tree
information:

  node 178081 level 1 items 424 free 69 generation 44495 owner EXTENT_TREE
  fs uuid 7d9dbe1b-dea6-4141-807b-026325123ad8
  chunk uuid 97a3e3aa-7105-4101-aaf7-50204a240e69
	  key (16613126144 EXTENT_ITEM 4096) block 177939087360 gen 44433
	  key (16632803328 EXTENT_ITEM 4096) block 177939120128 gen 44433
	  key (16654548992 EXTENT_ITEM 8192) block 177970380800 gen 44336
	  key (16697884672 EXTENT_ITEM 8192) block 177970397184 gen 44336
	  key (16714223616 EXTENT_ITEM 16384) block 177970413568 gen 44336
	  key (16721760256 EXTENT_ITEM 16384) block 177943855104 gen 44436
	  key (16857755648 EXTENT_ITEM 4096) block 177857544192 gen 44416
  ...

For easier lookup, print bytenr of child eb if its level is not equal
to parent's level - 1 in read_node_slot().

Signed-off-by: Su Yue <l@damenly.su>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-01-13 22:33:10 +01:00
Josef Bacik 9cc9c9ab32 btrfs-progs: print the eb flags for nodes as well
While debugging a corruption problem I realized we don't spit out the
flags for nodes, which is needed when debugging relocation problems so
we know which nodes are the RELOC root items and which are the actual fs
tree's items.  Fix this by unifying the header printing helper so both
leaf's and nodes get the same information printed out.

  node 41070940160 level 1 items 34 free space 87 generation 7709536 owner ROOT_TREE
  node 41070940160 flags 0x1(WRITTEN) backref revision 1

Same for leaves:

  leaf 41070944256 items 12 free space 515 generation 7709536 owner ROOT_TREE
  leaf 41070944256 flags 0x1(WRITTEN) backref revision 1

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-12-16 17:08:53 +01:00
Boris Burkov 92d92e99b7 btrfs-progs: mkfs: support free space tree as -R option
Add a runtime feature (-R) flag for the free space tree. A filesystem
that is mkfs'd with -R free-space-tree then mounted with no options has
the same contents as one mkfs'd without the option, then mounted with
'-o space_cache=v2'.

The only tricky thing is in exactly how to call the tree creation code.
Using btrfs_create_free_space_tree as is did not quite work, because an
extra reference to the eb (root->commit_root) is leaked, which mkfs
complains about with a warning. I opted to follow how the uuid tree is
created by adding it to the dirty roots list for cleanup by
commit_tree_roots in commit_transaction. As a result,
btrfs_create_free_space_tree no longer exactly matches the version in
the kernel sources.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-09-08 22:06:04 +02:00
Marcos Paulo de Souza c655b5e4b1 btrfs-progs: make btrfs_lookup_dir_index in parity with kernel code
This function exists in kernel side but using the _item suffix, and
objectid argument is placed before the name argument. Change the
function to reflect the kernel version.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:09:49 +02:00
David Sterba 0144bcb713 btrfs-progs: move volumes.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:06 +02:00
David Sterba 6069bc52a9 btrfs-progs: move transaction.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:06 +02:00
David Sterba 978f300c21 btrfs-progs: move inode.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:05 +02:00
David Sterba abb670f883 btrfs-progs: move ctree.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:05 +02:00
David Sterba c03619b864 btrfs-progs: move file.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:05 +02:00
David Sterba 772f0da6df btrfs-progs: move disk-io.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:05 +02:00
David Sterba cf529f36ad btrfs-progs: move print-tree.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:05 +02:00
David Sterba 7dd4abc3c5 btrfs-progs: move extent-tree.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:04 +02:00
David Sterba 4e49bd703d btrfs-progs: move extent_io.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:04 +02:00
David Sterba da90f38ad9 btrfs-progs: move free-space-tree.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:04 +02:00
David Sterba f7fe7de64c btrfs-progs: move uuid-tree.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-09 22:19:09 +02:00
David Sterba a656166d11 btrfs-progs: move root-tree.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-09 22:19:09 +02:00
David Sterba fdf058ac0f btrfs-progs: move inode-item.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-09 22:19:09 +02:00
David Sterba a067ecef0a btrfs-progs: move file-item.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-09 22:19:09 +02:00
David Sterba 925bf01f5d btrfs-progs: move dir-item.c to kernel-shared/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-09 22:19:08 +02:00
Qu Wenruo ccad599701 btrfs-progs: rename btrfs_block_group_cache to btrfs_block_group
To keep the same naming across kernel and btrfs-progs.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-11 20:50:00 +02:00
Qu Wenruo 5bc44891c9 btrfs-progs: kill block_group_cache::key
This would sync the code between kernel and btrfs-progs, and save at
least 1 byte for each btrfs_block_group_cache.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-11 20:49:50 +02:00
David Sterba 8c85b34420 btrfs-progs: move backref.[ch] to kernel-shared/
The files are very close to kernel versions, for that keep them in the
shared directory.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 20:48:35 +02:00
David Sterba 58bcd4260f btrfs-progs: move free-space-tree.[ch] to kernel-shared/
The files are very close to kernel versions, for that keep them in the
shared directory.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-03-31 18:37:34 +02:00
David Sterba 019489a143 btrfs-progs: move delayed-ref.[ch] to kernel-shared/
The files are very close to kernel versions, for that keep them in the
shared directory.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-03-31 18:37:34 +02:00
David Sterba 94fced6353 btrfs-progs: build: drop kernel-lib from -I and update paths
Include the files by full path to avoid any confusion in case of
potentially duplicate names.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:04 +02:00
David Sterba 440e8b9830 btrfs-progs: shared: rename ulist_fini to ulist_release
Sync the file with kernel version.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-01-16 21:16:14 +01:00
David Sterba 2fefed2601 btrfs-progs: shared: cleanup includes in ulist.c
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 390db8a346 btrfs-progs: shared: remove debug code from ulist
Sync with kernel sources, we don't define CONFIG_BTRFS_DEBUG in
userspace anyway.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:47 +01:00
David Sterba 4049542d4f btrfs-progs: shared: copy ulist_del from kernel
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