Commit graph

5684 commits

Author SHA1 Message Date
David Sterba 419cb3011c btrfs-progs: open code btrfs_list_get_path_rootid
The function btrfs_list_get_path_rootid is exported to libbtrfs so it
needs to stay, but we can inline the implementation.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-08 16:35:47 +02:00
David Sterba 400b66689e btrfs-progs: move btrfsck.h to check/
This is part of the checker and unfortunately also the public header, so
we can only copy it to the right directory.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:49:33 +02:00
David Sterba 50292bc009 btrfs-progs: merge props.c to cmds/property.c
The property definitions and handlers are for the command line
processing, so merge it with the main source file.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:41:24 +02:00
David Sterba 022a560033 btrfs-progs: move props.h to cmds/
This is included only by the command line handler, move it there.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:33:24 +02:00
David Sterba ee17bcec33 btrfs-progs: remove stale declaration from send.h
We don't use this header for kernel compilation so the guarded
declaration is pointless.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:27:59 +02:00
David Sterba e86425242f btrfs-progs: move send.h to kernel-shared/
The header contains the protocol definitions and is almost exactly the
same as the kernel version, move it to the proper directory.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:26:46 +02:00
David Sterba c1953c36dd btrfs-progs: move all private definitions to cmds/qgroup.c
Move everything related to the output formatting and filtering out of
qgroup.h and leave only the structures used by the public API.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:21:14 +02:00
David Sterba f67e5da2fa btrfs-progs: remove prefix from all static qgroup helpers
The exported functions provided by qgroups have been changed, now remove
the prefix from the local helpers.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:15:55 +02:00
David Sterba 49671bf29e btrfs-progs: add prefixes to exported qgroup helpers
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:09:33 +02:00
David Sterba 353b55a5cf btrfs-progs: unexport local qgroup helpers
After merging the files, many functions can be made static, leaving only
a few helpers that are used by subvolume.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 19:07:54 +02:00
David Sterba ae56c6a071 btrfs-progs: merge qgroup.c into cmds/qgroup.c
The contents of top level qgroups.c is only for command line output and
filtering, we already have cmds/qgroup.c for that so merge the files.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 18:59:43 +02:00
David Sterba 6e0e0d467f btrfs-progs: move qgroup.h to cmds/
There are declarations that are namely for the command line out put,
filters and formatting. Move it to cmds/.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 18:45:59 +02:00
David Sterba 9d98ad1db5 btrfs-progs: tests: add templates for most common test types
Create a ready-to-use templates for the most common tests, copied from
readme.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 18:10:56 +02:00
David Sterba 5b0e18ce6c btrfs-progs: tests: properly load dm-thin in mkfs/017
The test started to fail some time ago when dm-thin was changed to
dm-thin-pool that loads 2 targets dm-thin and dm-thin-pool. We can now
properly detect both and run the test.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 16:58:43 +02:00
David Sterba 37e0aa576c btrfs-progs: tests: allow alternate name for dm target detection
Some dm target name and module do not match exacly, extend the helper to
take optional 2nd parameter that will be checked in case loading by the
first parameter fails.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 16:57:06 +02:00
David Sterba 6694fe5de9 btrfs-progs: tests: do dm target detection by one
Switch the helper to take only one parameter, the target name. This can
be used to extend the helper with additional parameters for the target.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 16:48:42 +02:00
David Sterba 76ab1fa364 btrfs-progs: rename and move group_profile_max_safe_loss
The helper belongs to the others that translate bg flags to the raid
attr table member.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 16:38:56 +02:00
David Sterba a5becbde35 btrfs-progs: use raid attr table in group_profile_max_safe_loss
The helper open codes what we already have in the raid attr table, so
use it. We assume a valid flags so there's no error value.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 16:32:55 +02:00
David Sterba 45d034d774 btrfs-progs: move btrfs_tree_search2_ioctl_supported to fsfeatures.c
The helper detects a feature support, so put it to the right file.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 16:20:17 +02:00
David Sterba 9be1b1c442 btrfs-progs: qgroup create: accept only valid qgroupid
Many qgroup commands accept the level/id format and also a path to
subvolume, the qgroup id is derived from that. This does not make sense
for the create command as we can't create the 0/subvolid qgroup (thus
can't be derived from the path), only the higher levels.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 14:20:42 +02:00
David Sterba 47b04b747d btrfs-progs: move parse_qgroupid to parse utils
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 14:20:42 +02:00
David Sterba cb5a542871 btrfs-progs: factor out plain qgroupid parsing
We'll use plain qgroupid parsing function elsewhere so split that part
from parse_qgroupid_or_path. The parsing is slightly reworked and goes
from start to end, while previously it looked up the slash and worked
from there. In case a valid qgroupid is also a valid path, the path must
be specified as absolute.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 14:20:42 +02:00
David Sterba ebf500b43d btrfs-progs: rename parse_qgroupid
This helper can parse a qgroupid or a path, so rename it accordingly, so
a plain qgroupid parsing can be factored out as a standalone helper.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 14:20:42 +02:00
Qu Wenruo 9a11b1b792 btrfs-progs: backport btrfs_check_node() from kernel
The btrfs_check_node() has far less meaningful error message compared to
kernel counterpart, and it even lacks certain checks like level check.

Backport btrfs_check_node() to btrfs-progs to not only unify the code
but greatly improve the readability of the error messages.

Extra modification includes:

- No fs_info needed
  As we don't need to output fsid.

- Remove unlikely() macro

- Extra BTRFS_TREE_BLOCK_* error type

- Btrfs-progs specific error handling
  To record the corrupted tree blocks.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 14:20:41 +02:00
Qu Wenruo 8f8cafa2ce btrfs-progs: backport btrfs_check_leaf() from kernel
Currently btrfs_check_leaf() provides almost meaningless messages for
things like invalid item offset:

  incorrect offsets 8492 3707786077

While kernel tree-checker is doing a way better job, so it's wise to
backport btrfs_check_leaf() from kernel.

There are some modification needed:

- New generic_err() helper

- Remove unlikely() macro

- Remove empty essential tree check
  Mkfs still needs to create empty essential trees.

- Using BTRFS_TREE_BLOCK_* return value
  Original mode check still relies on them to do certain repair.

- No need for btrfs_fs_info
  We no longer need fsid output, thus no need for btrfs_fs_info.

- No item contents check

- Still using the fail: label for btrfs-progs specific error handling

The new output looks like:

  corrupt leaf: root=2 block=72164753408 slot=109, unexpected item end, have 3707786077 expect 8492

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 14:19:54 +02:00
Qu Wenruo 1f8dfe681f btrfs-progs: use btrfs_key for btrfs_check_node() and btrfs_check_leaf()
In kernel space we hardly use btrfs_disk_key, unless for very lowlevel
code.

There is no need to intentionally use btrfs_disk_key in btrfs-progs
either.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 13:58:44 +02:00
David Sterba c3ee6a8a09 btrfs-progs: unify GPL header comments
Add the GPL v2 header to files where it was missing and is not from an
external source, update to the most recent version with the address.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 13:58:44 +02:00
David Sterba f3a132fa1b btrfs-progs: factor out compression type name parsing to common utils
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 13:58:44 +02:00
David Sterba 0c3991e171 btrfs-progs: mkfs: use common parser of bg profiles
Replace open-coded profile parser with the common one.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 13:58:44 +02:00
David Sterba 31df7dc295 btrfs-progs: factor out profile parsing to common utils
There are some duplicate parsers of the profile names, factor out the
one from balance to the common code.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 13:58:44 +02:00
Sidong Yang dd3b931b1c btrfs-progs: props: init compression prop_handlers with field name
Compression prop_handler is initialized without field name. This patch
corrects that it's initialized with field name.

Signed-off-by: Sidong Yang <realwakka@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 13:58:44 +02:00
David Sterba 6b93a7336c btrfs-progs: move number and range parsing helpers to parse-utils.c
Some of the parsers in cmds/balance.c are generic enough for the common
part.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-07 13:58:43 +02:00
David Sterba af56460de8 btrfs-progs: split parsing helpers from utils.c
There are various parsing helpers scattered everywhere, unify them to
one file and start with helpers already in utils.c.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-06 17:15:51 +02:00
David Sterba d15b7b0d58 btrfs-progs: parse profile name from the raid table
We can use the raid table to match profile names, additionally make the
test case insensitive. The single profile is not represented as a bit
and must be set manually for now.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-06 17:15:13 +02:00
David Sterba 7572839a74 btrfs-progs: add and use bit masks for RAID1 and RAID56 profiles
Many test conditions can be simplified in case they check all the
related profiles.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-06 16:36:18 +02:00
David Sterba 7fe4396467 btrfs-progs: copy some raid_attr helpers from kernel
There are convenience helpers for the raid attr table, copy them from
kernel for further cleanups.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-06 16:36:17 +02:00
Josef Bacik 79e534def9 btrfs-progs: add the incompat flag for extent tree v2
I will have a lot of preparatory patches to reduce the review pain of
this large feature.  In order to enable that work define the incompat
flag.  Once all of the work lands to support the feature there will be a
patch to actually enable us to select it and manipulate file systems
with that incompat flag set.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-06 16:36:17 +02:00
Josef Bacik 4b6cf2a3eb btrfs-progs: mkfs: generate free space tree at make_btrfs() time
With extent-tree-v2 we won't be able to cache block groups based on the
extent tree, so we need to have a valid free space tree before we open
the temporary file system to finish setting the file system up.  Set up
the basic free space entries for our temporary system chunk if we have
the free space tree enabled and stop generating the tree after the fact.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-06 16:36:17 +02:00
Josef Bacik 826e466028 btrfs-progs: add add_block_group_free_space helper
This exists in the kernel free-space-tree.c but not in progs.  We need
it to generate the free space items for new block groups, which is
needed when we start creating the free space tree in make_btrfs().

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-06 16:36:17 +02:00
Josef Bacik abc2b30443 btrfs-progs: mkfs: add the block group item in make_btrfs()
Currently we build a bare-bones file system in make_btrfs(), and then we
load it up and fill in the rest of the file system after the fact.  One
thing we omit in make_btrfs() is the block group item for the temporary
system chunk we allocate, because we just add it after we've opened the
file system.

However I want to be able to generate the free space tree at
make_btrfs() time, because extent tree v2 will not have an extent tree
that has every block allocated in the system.  In order to do this I
need to make sure that the free space tree entries are added on block
group creation, which is annoying if we have to add this chunk after
I've created a free space tree.

So make future work simpler by simply adding our block group item at
make_btrfs() time, this way I can do the right things with the free
space tree in the generic make block group code without needing a
special case for our temporary system chunk.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-06 16:36:17 +02:00
Josef Bacik 3d870a491f btrfs-progs: make sure track_dirty and ref_cows is set properly
Adding support for the per-block group roots means we will be reading
the roots directly in different places.  Make sure we set ->track_dirty
and ->ref_cows properly in the helper so we don't have to do this
everywhere.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-03 15:33:53 +02:00
Josef Bacik 41c3fa5d0b btrfs-progs: mkfs: add helper for writing empty tree nodes
With extent tree v2 we're going to be writing some more empty trees for
the initial mkfs step, so take this common code and make it a helper.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-03 15:33:30 +02:00
Josef Bacik 9b4207a0f9 btrfs-progs: mkfs: set nritems based on root items written
For the root tree we were just hard setting the nritems to 4, which will
change when we move to extent tree v2.  Instead set the nritems after
we've added all the root items we need to the root tree.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-03 15:32:40 +02:00
Josef Bacik 6ee697fb25 btrfs-progs: mkfs: use blocks_nr to determine the super used bytes
We were setting the super block's used bytes to a static number.
However the number of blocks we have to write has the correct used size,
so just add up the total number of blocks we're allocating as we
determine their offsets.  This value will be used later which is why I'm
calculating it this way instead of doing the math to set the bytes_super
specifically.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-03 15:32:16 +02:00
Josef Bacik 5a164401e9 btrfs-progs: mkfs: get rid of MKFS_SUPER_BLOCK
We use these block's in order to keep track of which blocks need to be
added to the extent tree and where their roots need to be written.
However we skip MKFS_SUPER_BLOCK for all of these helpers, and we don't
actually need to keep track of the specific block we allocated because
it is always BTRFS_SUPER_INFO_OFFSET.  Remove this enum as we don't need
it.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-03 15:30:54 +02:00
Josef Bacik 5e63118215 btrfs-progs: mkfs: use an associative array for init blocks
Allow creating trees more flexible, eg. in no fixed order.  To handle
this we want to rework the initial mkfs step to take an array of the
blocks we want to create and use the array to keep track of which blocks
we need to create. Use that for current format, make it ready for extent
tree v2.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-03 15:26:35 +02:00
David Sterba 0534441e2e btrfs-progs: tests: add mkfs test for raid0/1 and raid10/2
Extend basic tests with the degenerate raid0 and raid10, coming in 5.15.
Mount of a freshly created filesystem works even on older kernels too.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-08-27 15:40:53 +02:00
David Sterba a177ef7dd4 btrfs-progs: mkfs: allow degenerate raid0/raid10
Kernel patch b2f78e88052bc0bee ("btrfs: allow degenerate raid0/raid10")
in
5.15 will allow mounting and converting to single device raid0 or two
device raid10.  Let mkfs create such filesystem.

"The motivation is to allow to preserve the profile type as long as it
 possible for some intermediate state (device removal, conversion), or
 when there are disks of different size, with raid0 the otherwise
 unusable space of the last device will be used too.  Similarly for
 raid10, though the two largest devices would need to be the same."

Signed-off-by: David Sterba <dsterba@suse.com>
2021-08-27 15:40:53 +02:00
Li Zhang b199123b33 btrfs-progs: build: fix detection of ext4 i_{a,c,a}time_extra
Running convert-tests.sh Reported that the 019-ext4-copy-timestamps test
failed:

  ...
  mount -o loop -t ext4 btrfs-progs/tests/test.img btrfs-progs/tests/mnt
  ====== RUN CHECK touch btrfs-progs/tests/mnt/file
  ====== RUN CHECK stat btrfs-progs/tests/mnt/file
  File: 'btrfs-progs/tests/mnt/file'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
  Device: 700h/1792d  Inode: 13          Links: 1
  Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
  Context: unconfined_u:object_r:unlabeled_t:s0
  Access: 2021-08-24 22:10:21.999209679 +0800
  Modify: 2021-08-24 22:10:21.999209679 +0800
  Change: 2021-08-24 22:10:21.999209679 +0800
  ...
  btrfs-progs/btrfs-convert btrfs-progs/tests/test.img
  ...
  ====== RUN CHECK mount -t btrfs -o loop btrfs-progs/tests/test.img btrfs-progs/tests/mnt
  ====== RUN CHECK stat btrfs-progs/tests/mnt/file
  File: 'btrfs-progs/tests/mnt/file'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
  Device: 2ch/44d Inode: 267         Links: 1
  Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
  Context: unconfined_u:object_r:unlabeled_t:s0
  Access: 2021-08-24 22:10:21.000000000 +0800
  Modify: 2021-08-24 22:10:21.000000000 +0800
  Change: 2021-08-24 22:10:21.000000000 +0800
  ...
  atime on converted inode does not match
  test failed for case 019-ext4-copy-timestamps

Obviously, the log says that btrfs-convert does not support nanoseconds.
I looked at the source code and found that only if ext2_fs.h defines
EXT4_EPOCH_MASK btrfs-convert to support nanoseconds. But in e2fsprogs,
EXT4_EPOCH_MASK was introduced in v1.43, but in some older versions,
such as v1.40, e2fsprogs actually supports nanoseconds. It seems that if
struct ext2_inode_large contains the i_atime_extra member, ext4 is
supports nanoseconds, so I updated the logic to determine whether the
current ext4 file system supports nanosecond precision.  In addition, I
imported some definitions to encode and decode tv_nsec (copied from
e2fsprogs source code).

Author: Li Zhang <zhanglikernel@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-08-26 20:34:36 +02:00
Qu Wenruo 325dba6432 btrfs-progs: check: output proper csum values for --check-data-csum
[BUG]
When running "btrfs check --check-data-csum" on fs with corrupted data,
the error message almost makes no sense:

  $ btrfs check --check-data-csum /dev/test/test
  Opening filesystem to check...
  Checking filesystem on /dev/test/test
  UUID: c31afe0a-55bc-4e7d-aba0-9dfa9ddf8090
  [1/7] checking root items
  [2/7] checking extents
  [3/7] checking free space cache
  [4/7] checking fs roots
  [5/7] checking csums against data
  mirror 1 bytenr 13631488 csum 19 expected csum 152 <<<
  ERROR: errors found in csum tree
  [6/7] checking root refs
  [7/7] checking quota groups skipped (not enabled on this FS)
  found 147456 bytes used, error(s) found
  total csum bytes: 16
  total tree bytes: 131072
  total fs tree bytes: 32768
  total extent tree bytes: 16384
  btree space waste bytes: 124799
  file data blocks allocated: 16384
   referenced 16384

[CAUSE]
We're just outputting the first byte and in decimal, which is completely
different from what we did in kernel space, nor what we did for metadata
csum mismatch.

[FIX]
Use btrfs_format_csum() for btrfs-check to output csum.

Now the result looks much better:

  [5/7] checking csums against data
  mirror 1 bytenr 13631488 csum 0x13fec125 expected csum 0x98757625

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-08-26 14:27:40 +02:00