Commit graph

341 commits

Author SHA1 Message Date
Qu Wenruo 0bfd7004d3 btrfs-progs: hide block group tree behind experimental feature
The block group tree doesn't yet have full bi-directional conversion
support from btrfstune, and it seems we may want one or two release
cycles to rule out some extra bugs before really releasing the progs
support.

This patch will hide the block group tree feature behind experimental
flag for the following tools:

- btrfstune
  "-b" option to convert to bg tree.

- mkfs.btrfs
  hide "block-group-tree" feature from both -O (the new default position
  for all features) and -R (the old, soon to be deprecated one).

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:08 +02:00
David Sterba 4f23f0e711 btrfs-progs: use warning helper for multiple profile messages
Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:07 +02:00
David Sterba 0e38e1c4f2 btrfs-progs: use error helper for messages in non-kernel code
Lots of code still uses fprintf(stderr, "...") that should be the
error() helper. The kernel-shared code is left out of the conversion for
now.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:07 +02:00
David Sterba b5aac254c7 btrfs-progs: common: update include lists, part 1
The tool IWYU (include what you use) suggests to remove and add some
includes. This is only partial to avoid accidental build breakage, the
includes are entangled and will have to be cleaned in the future again.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:07 +02:00
David Sterba 40c4ba74ec btrfs-progs: cmds: update include lists
The tool IWYU (include what you use) suggests to remove and add some
includes.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:07:59 +02:00
David Sterba d04a1fa10e btrfs-progs: factor filesystem helpers out of utils.c
Group helpers that retrieve information from the filesystem out of
utils.c.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:06:13 +02:00
David Sterba eac3cb577c btrfs-progs: move parse_qgroupid_or_path to parse-utils
Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:06:13 +02:00
David Sterba 6edd4b2121 btrfs-progs: factor string helpers out of utils.c
Utils is the catch-all file, we can now separate some string utility
functions.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:06:13 +02:00
David Sterba 8d8f8c7f73 btrfs-progs: prepare merging compat feature lists
The features are split to -O and -R but it does not make much sense from
user POV, there are different levels of compatibility but it does not
need to be selected that way. Merge the tables into one but hide it
behind experimental build until the conversion is complete.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:06:13 +02:00
David Sterba b121ac4404 btrfs-progs: fix path to internal libbtrfsutil includes
All files include the <btrfsutil.h> which could be confused with the
system-wide installation. Drop the -I path from build and use full path
for any libbtrfsutil headers.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:06:12 +02:00
David Sterba 272bff8978 btrfs-progs: common: reorder includes
The preferred order:
- system headers
- standard headers
- libraries
- kernel library
- kernel shared
- common headers
- other tools
- own headers

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:06:12 +02:00
David Sterba e331037d40 btrfs-progs: factor out and export rotational/ssd device helper
The helper belongs to device utils, move it from the mkfs.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:06:11 +02:00
Qu Wenruo d8f3355734 btrfs-progs: unexport csum_tree_block()
The function csum_tree_block() is not really utilized by anyone, all
current callers just use csum_tree_block_size().

Furthermore there is a stale definition in common/utils.h which is using
the old "struct btrfs_root" as the first argument, while we have already
migrated to "struct btrfs_fs_info".

So just unexport csum_tree_block() and remove the stale definition.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:06:11 +02:00
David Sterba 4d96291de4 btrfs-progs: mkfs: drop numeric flag from -O and -R option list
The raw number of the features in the list of 'mkfs.btrfs -O list-all'
and for -R is not that useful, it's an implementation detail or can be
put to documentation.

Now looks like:

Filesystem features available:
mixed-bg            - mixed data and metadata block groups (compat=2.6.37, safe=2.6.37)
extref              - increased hardlink limit per file to 65536 (compat=3.7, safe=3.12, default=3.12)
raid56              - raid56 extended format (compat=3.9)
...

Signed-off-by: David Sterba <dsterba@suse.com>
2022-09-12 18:25:32 +02:00
Qu Wenruo 1430b41427 btrfs-progs: separate block group tree from extent tree v2
Block group tree feature is completely a standalone feature, and it has
been over 5 years before the initial introduction to solve the long
mount time.

I don't really want to waste another 5 years waiting for a feature which
may or may not work, but definitely not properly reviewed for its
preparation patches.

So this patch will separate the block group tree feature into a
standalone compat RO feature.

There is a catch, in mkfs create_block_group_tree(), current
tree-checker only accepts block group item with valid chunk_objectid,
but the existing code from extent-tree-v2 didn't properly initialize it.

This patch will also fix above mentioned problem so kernel can mount it
correctly.

Now mkfs/fsck should be able to handle the fs with block group tree.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-09-12 18:25:32 +02:00
Khem Raj b4c8b9054d btrfs-progs: use linux mount.h instead of sys/mount.h
This file includes linux/fs.h which includes linux/mount.h and with
glibc 2.36 linux/mount.h and glibc mount.h are not compatible [1]
therefore try to avoid including both headers

[1] https://sourceware.org/glibc/wiki/Release/2.36

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-08-16 15:18:12 +02:00
David Sterba 8356c423e6 btrfs-progs: receive: implement FILEATTR command
The initial proposal for file attributes was built on simply doing
SETFLAGS but this builds on an old and non-extensible interface that has
no direct mapping for all inode flags. There's a unified interface
fileattr that covers file attributes and xflags, it should be possible
to add new bits.

On the protocol level the value is copied as-is in the original inode
but this does not provide enough information how to apply the bits on
the receiving side. Eg. IMMUTABLE flag prevents any changes to the file
and has to be handled manually.

The receiving side does not apply the bits yet, only parses it from the
stream.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-08-16 15:18:11 +02:00
David Sterba a33af50c52 btrfs-progs: add constant for initial getopt values
Add constant for initial value to avoid unexpected clashes with user
defined getopt values and shift the common size getopt values.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-08-16 15:18:11 +02:00
Boris Burkov 7a6fb356dc btrfs-progs: receive: process setflags ioctl commands
In send stream v2, send can emit a command for setting inode flags via
the setflags ioctl. Pass the flags attribute through to the ioctl call
in receive.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-06-07 13:59:32 +02:00
Boris Burkov 54db5c0397 btrfs-progs: receive: process fallocate commands
Send stream v2 can emit fallocate commands, so receive must support them
as well. The implementation simply passes along the arguments to the
syscall. Note that mode is encoded as a u32 in send stream but fallocate
takes an int, so there is a unsigned->signed conversion there.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-06-07 13:59:32 +02:00
Boris Burkov e30f7d3ed2 btrfs-progs: receive: process encoded_write commands
Add a new btrfs_send_op and support for both dumping and proper receive
processing which does actual encoded writes.

Encoded writes are only allowed on a file descriptor opened with an
extra flag that allows encoded writes, so we also add support for this
flag when opening or reusing a file for writing.

Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-06-07 13:59:32 +02:00
Boris Burkov aa1ca3789e btrfs-progs: receive: support v2 send stream DATA tlv format
The new format privileges the BTRFS_SEND_A_DATA attribute by
guaranteeing it will always be the last attribute in any command that
needs it, and by implicitly encoding the data length as the difference
between the total command length in the command header and the sizes of
the rest of the attributes (and of course the tlv_type identifying the
DATA attribute). To parse the new stream, we must read the tlv_type and
if it is not DATA, we proceed normally, but if it is DATA, we don't
parse a tlv_len but simply compute the length.

In addition, we add some bounds checking when parsing each chunk of
data, as well as for the tlv_len itself.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-06-07 13:59:32 +02:00
Boris Burkov a82996e1b6 btrfs-progs: receive: dynamically allocate sctx->read_buf
In send stream v2, write commands can now be an arbitrary size. For that
reason, we can no longer allocate a fixed array in sctx for read_cmd.
Instead, read_cmd dynamically allocates sctx->read_buf. To avoid
needless reallocations, we reuse read_buf between read_cmd calls by also
keeping track of the size of the allocated buffer in sctx->read_buf_sz.

We do the first allocation of the old default size at the start of
processing the stream, and we only reallocate if we encounter a command
that needs a larger buffer.

Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-06-07 13:59:31 +02:00
Boris Burkov cf269aa47b btrfs-progs: receive: support v2 send stream larger tlv_len
An encoded extent can be up to 128K in length, which exceeds the largest
value expressible by the current send stream format's 16 bit tlv_len
field. Since encoded writes cannot be split into multiple writes by
btrfs send, the send stream format must change to accommodate encoded
writes.

Supporting this changed format requires retooling how we store the
commands we have processed. We currently store pointers to the struct
btrfs_tlv_headers in the command buffer. This is not sufficient to
represent the new BTRFS_SEND_A_DATA format. Instead, parse the attribute
headers and store them in a new struct btrfs_send_attribute which has a
32bit length field. This is transparent to users of the various TLV_GET
macros.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-06-06 19:21:39 +02:00
David Sterba 0f65bf66be btrfs-progs: libbtrfs: drop ifdef BTRFS_FLAT_INCLUDES where not necessary
Headers that are only exported and not used for build do not need the
BTRFS_FLAT_INCLUDES switch (between local and installed headers). Now
that there are local copies of the shared headers drop the respective
part from local headers.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-06-06 15:48:52 +02:00
David Sterba e278e0755f btrfs-progs: make device add and paused balance work together
Kernel commit efc0e69c2fea ("btrfs: introduce exclusive operation
BALANCE_PAUSED state") allows to start a device add when there's a
paused balance, eg. to let the balance finish when there's not enough
chunk space. Add the support for that, though this needs an updated
kernel to export the 'balance paused' in sysfs.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-05-03 22:48:14 +02:00
Josef Bacik c36d8ea235 btrfs-progs: check: handle the block group tree properly
We need to make sure we process the block group root, and mark its
blocks as used for the free space tree checking.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-03-09 18:07:12 +01:00
Josef Bacik 3aacdc4404 btrfs-progs: repair: bail if we find an unaligned extent
The fuzz-test/003 was infinite looping when I reworked the code to
re-calculate the used bytes for the superblock.  This is because fsck
wasn't properly fixing the bad extent before my change, it just happened
to error out nicely, whereas my change made it so we go the wrong bytes
used count and just infinite looped trying to fix the problem.

Fix this by sanity checking the extent when we try to re-calculate the
bytes_used.  This makes us no longer infinite loop so we can get through
the fuzz tests.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-03-08 18:18:01 +01:00
Adam Borowski 6da5478a38 btrfs-progs: fix a bunch of typos
These have been detected by lintian and codespell.

Signed-off-by: Adam Borowski <kilobyte@angband.pl>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-02-01 18:40:53 +01:00
Josef Bacik 3337b7993b btrfs-progs: common: allow users to select extent-tree-v2 option
We want to enable developers to test the extent tree v2 features as they
are added, add the ability to mkfs an extent tree v2 fs if we have
experimental enabled.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-30 19:07:34 +01:00
Josef Bacik 9dbfb6c162 btrfs-progs: search all extent roots for marking used space
We could have multiple extent roots, so add a helper to mark all the
used space in the FS based on any extent roots we find, and then use
this extent io tree to fixup the block group accounting.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-30 19:00:12 +01:00
Josef Bacik db2ab47823 btrfs-progs: stop accessing ->extent_root directly
When we switch to multiple global trees we'll need to access the
appropriate extent root depending on the block group or possibly root.
To handle this, use a helper in most places and then the actual root in
places where it is required.  We will whittle down the direct accessors
with future patches, but this does the bulk of the preparatory work.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-30 18:56:54 +01:00
Josef Bacik 368cd3b3d8 btrfs-progs: mark reloc roots as used
btrfs_mark_used_tree_blocks skips the reloc roots for some reason, which
causes problems because these blocks are in use, and we use this helper
to determine if the block accounting is correct with extent tree v2.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-22 21:45:37 +01:00
Josef Bacik d67bf1b365 btrfs-progs: check: move btrfs_mark_used_tree_blocks to common
This is going to be used for the extent tree v2 stuff more commonly, so
move it out so that it is accessible from everywhere that we need it.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-22 21:45:37 +01:00
Josef Bacik 550fd48136 btrfs-progs: move btrfs_fix_block_accounting to repair.c
We have this helper sitting in extent-tree.c, but it's a repair
function.  I'm going to need to make changes to this for extent-tree-v2
and would rather this live outside of the code we need to share with the
kernel.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-22 21:45:37 +01:00
Wang Yugui 683f41bfb0 btrfs-progs: fix discard support check
[BUG]
mkfs.btrfs v5.15  outputs a message even if the disk is a HDD without
TRIM/DISCARD support:

  Performing full device TRIM /dev/sdc2 (326.03GiB) ...

[CAUSE]
mkfs.btrfs check TRIM/DISCARD support through the content of
queue/discard_granularity, but compare it against a wrong value.

When HDD without TRIM/DISCARD support, the content of
queue/discard_granularity is '0' '\n' '\0', rather than '0' '\0'.

[FIX]

- compare the value based on atoi() to provide more robustness
- delete unnecessary '\n' in pr_verbose()

Fixes: c50c448518 ("btrfs-progs: do sysfs detection of device discard capability")
Signed-off-by: Wang Yugui <wangyugui@e16-tech.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-18 10:17:21 +01:00
Qu Wenruo 636b2e6027 btrfs-progs: remove temporary buffer for super block
There are a lot of call sites where we use the following code snippet:

	u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
	struct btrfs_super_block *sb;
	u64 ret;

	sb = (struct btrfs_super_block *)super_block_data;

The reason for this is, structure btrfs_super_block was smaller than
BTRFS_SUPER_INFO_SIZE.

Thus for anything with csum involved, we have to use a proper 4K buffer.

Since the recent unification of sizeof(struct btrfs_super_block), we no
longer need such workaround, and can use struct btrfs_super_block
directly to do any operation.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-05 12:50:03 +01:00
Qu Wenruo b1c944657c btrfs-progs: make "btrfs filesystem df" command show upper case profile
[BUG]
Since commit dad03fac3b ("btrfs-progs: switch btrfs_group_profile_str
to use raid table"), fstests/btrfs/023 and btrfs/151 will always fail.

The failure of btrfs/151 explains the reason pretty well:

btrfs/151 1s ... - output mismatch
    --- tests/btrfs/151.out	2019-10-22 15:18:14.068965341 +0800
    +++ ~/xfstests-dev/results//btrfs/151.out.bad	2021-11-02 17:13:43.879999994 +0800
    @@ -1,2 +1,2 @@
     QA output created by 151
    -Data, RAID1
    +Data, raid1
    ...
    (Run 'diff -u ~/xfstests-dev/tests/btrfs/151.out ~/xfstests-dev/results//btrfs/151.out.bad'  to see the entire diff)

[CAUSE]
Commit dad03fac3b ("btrfs-progs: switch btrfs_group_profile_str to use
raid table") will use btrfs_raid_array[index].raid_name, which is all
lower case.

[FIX]
There is no need to bring such output format change.

So here we split the btrfs_raid_attr::raid_name[] into upper_name[] and
lower_name[], and make upper and lower case helpers for callers to use.

Now there are several types of callers referring to lower_name and
upper_name:

- parse_bg_profile()
  It uses strcasecmp(), either case would be fine.

- btrfs_group_profile_str()
  Originally it uses upper case for all profiles except "single".
  Now unified to upper case.

- sprint_profiles()
  It uses lower case.

- bg_flags_to_str()
  It uses upper case.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-05 12:50:03 +01:00
Wang Yugui b1d8f945c9 btrfs-progs: mask out all unwanted profiles in btrfs_group_profile_str
Commit ("btrfs-progs: switch btrfs_group_profile_str to use raid table")
introduced a regression that raid profile of GlobalReserve will be
printed as 'unknown'.

  $ btrfs filesystem df /mnt/test
  Data, single: total=5.02TiB, used=4.98TiB
  System, single: total=4.00MiB, used=624.00KiB
  Metadata, single: total=11.01GiB, used=6.94GiB
  GlobalReserve, unknown: total=512.00MiB, used=0.00B

Fix it by:

- take BTRFS_BLOCK_GROUP_RESERVED into account when masking the block
  group flags
- update the define of BTRFS_BLOCK_GROUP_RESERVED too so it's same as in
  kernel

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Wang Yugui <wangyugui@e16-tech.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-05 12:50:03 +01:00
David Sterba 17aab2e428 btrfs-progs: receive: add missing unused inode number reads from the stream
Kernel emits inode number for all mkfile/mkdir/... commands but the
receive part does not pass it to the callbacks. At least document that
and read it from the stream in case we'd like to use it in the future.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-29 17:29:57 +02:00
David Sterba 11fcdbc35e btrfs-progs: introduce helper to get allowed profiles for a given device number
Use the raid table helper to avoid hard coding profiles for the given
number of devices in test_num_disk_vs_raid.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:24 +02:00
David Sterba b29f1603b0 btrfs-progs: use raid table for devs_min and replace local helper
Another duplication of the raid table, in this case missing the changes
to raid10 and raid0 minimum devices changed in a177ef7dd4
("btrfs-progs: mkfs: allow degenerate raid0/raid10").

Define and use a helper using the table value.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:23 +02:00
Naohiro Aota ae0dfb246d btrfs-progs: introduce btrfs_pread wrapper for pread
Wrap pread with btrfs_pread as well.

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-10-20 18:59:23 +02:00
Naohiro Aota c821e5545f btrfs-progs: introduce btrfs_pwrite wrapper for pwrite
Wrap pwrite with btrfs_pwrite(). It simply calls pwrite() on non-zoned
btrfs (opened without O_DIRECT). On zoned mode (opened with O_DIRECT),
it allocates an aligned bounce buffer, copies the contents and uses it
for direct-IO writing.

Writes in device_zero_blocks() and btrfs_wipe_existing_sb() are a little
tricky. We don't have fs_info on our hands, so use zinfo to determine it
is a zoned device or not.

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-10-20 18:59:23 +02:00
Naohiro Aota 2a550d4ccd btrfs-progs: drop ZONED flag from BTRFS_CONVERT_ALLOWED_FEATURES
Since we cannot create ext*/reiserfs on a zoned device, it is useless to
allow ZONED feature when converting a file system. Drop ZONED flag from
BTRFS_CONVERT_ALLOWED_FEATURES.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba b2dc5d0037 btrfs-progs: remove c++ protection from internal headers
We don't need the c++ name mangling protection in headers that are not
part of libbtrfs.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba 0fc4bb4d91 btrfs-progs: remove unused subvol_uuid_search_add
After removal of the fallback uuid search code the helper is unused in
the internal code.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba ac5954ee36 btrfs-progs: merge subvol_uuid_search helpers
Due to ambiguity of error values in the public API subvol_uuid_search,
there was second version added. There's a separate copy in libbtrfs and
we don't have to distinguish in the internal code. All callers check for
IS_ERR and NULL, so we can safely merge the helpers into one.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba e491d9cf25 btrfs-progs: simplify struct subvol_uuid_search use
After removing uuid search fallback code the structure has become
trivial and copies the fd that all callers have in their context.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba 22f5600649 btrfs-progs: open code subvol_uuid_search_init
The helper is trivial after removing the uuid search fallback code,
open code it.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba f18dbe2bc6 btrfs-progs: remove empty subvol_uuid_search_finit
After the uuid search fallback code has been removed, the finit helper
has become empty and can be removed.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba 522945efc8 btrfs-progs: remove unused prototypes from send-utils.h
The functions are part of path-utils.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba a8dceec381 btrfs-progs: drop slow subvol uuid search
There's a lot of code under BTRFS_COMPAT_SEND_NO_UUID_TREE to support
kernels < 3.12 that don't have uuid tree and the subvolume uuids are
searched in a slow way, building the uuid tree on the userspace side.

As the uuid tree is always created, the fallback code was not exercised
anyway due to 'uuid_tree_existed' check in subvol_uuid_search2.

Delete the code from the internal copy of send-utils. The support still
stays for libbtrfs and will be removed in the future.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba a1f2dd12ad btrfs-progs: use btrfs_subvolid_resolve instead of btrfs_list_path_for_root
The btrfs_list_* functions come with some overhead and for simple path
resolution we can use btrfs_subvolid_resolve.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba 8bb13015bd btrfs-progs: don't include btrfs-list.h unless necessary
We don't need to include this besides btrfs-list.c itself and
subvolume.c that does use the btrfs_list_* API.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba a3efed2adb btrfs-progs: libbtrfs: merge utils-lib.c back to utils.c
The separate file was needed for libbtrfs in the past to avoid pulling
utils.c in, but this is not needed after recent cleanups.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba 00610f5853 btrfs-progs: libbtrfs: remove unneeded BTRFS_FLAT_INCLUDES protections
Remove the switch for local and system-wide headers in headers that are
not part of libbtrfs anymore.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba 72260bc3b9 btrfs-progs: mkfs: enable space_cache=v2 (free-space-tree) by default
The free space tree is a better way to track the free space and has been
tested in the wild for a long time. The backward compatibility is
sufficient, several long term kernels. On-line conversion from v1 to v2
can be done by mount, switching from v2 to v1 can be done by 'btrfs
check'.

Issue: #295
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba 1f918def34 btrfs-progs: mkfs: enable NO_HOLES by default
The no-holes feature reduces consumption of metadata by not representing
file holes. Reducing metadata is a good thing in general, this is the
main goal to enable this by default.

There's a drawback, related to the missing information about holes. The
'check' tool cannot use it to cross-reference extent information and in
some cases may not be able to detect a problem.

The no-hole feature can be also enabled by 'btrfstune -n' on an
unmounted filesystem.

Issue: #405
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:02 +02:00
David Sterba 6d2bf4cbce btrfs-progs: refactor feature table initializations to C99-style
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:02 +02:00
David Sterba 35882be40a btrfs-progs: remove BTRFS_FLAT_INCLUDES from internal send headers
There's a separate copy of the headers, we can remove the include switch
from the internal copies.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:46:34 +02:00
David Sterba dad03fac3b btrfs-progs: switch btrfs_group_profile_str to use raid table
Use the profile names from the table, preserving the whole semantics of
unknown flags detection.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:46:34 +02:00
David Sterba 5664631b5b btrfs-progs: clean up test_uuid_unique
Move the declaration to the right header, constify argument and document
the function.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:46:33 +02:00
David Sterba c6c2e3da0a btrfs-progs: device scan: rename is_path_device
The term 'path' is confusing as we normally use it for filesystem paths,
while for multipath it's more related to the physical path by which the
devices are connected (though it also shows up as another path in the
filesystem).

Rename the helper doing the multipath detection so it's clear what path
is meant by that.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-06 16:50:42 +02:00
Nikolay Borisov 1b7128c152 btrfs-progs: add fallback code for multipath device detection for static build
Since libudev doesn't provide a static version of the library for static
build btrfs-progs will have to provide manual fallback. This change does
this by parsing the udev database files hosted at /run/udev/data/.
Under that directory every block device should have a file with the
following name: bMAJ:MIN. So implement the bare minimum code necessary
to parse this file and search for the presence of DM_MULTIPATH_DEVICE_PATH
udev attribute. This could likely be racy since access to the udev
database is done outside of libudev but that's the best that can be
done when implementing this manually and is only for a limited usecase
where static build has to be used.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-06 16:50:37 +02:00
Nikolay Borisov 681b4bd5e1 btrfs-progs: ignore devices representing paths in multipath
Currently btrfs-progs will happily enumerate any device which has a
btrfs filesystem on it irrespective of its type. For the majority of
use cases that's fine and there haven't been any problems with that.
However, there was a recent report that in multipath scenario when
running "btrfs fi show" after a path flap (path going down and then
coming back up) instead of the multipath device being show the device
which represents the flapped path is shown. So a multipath filesystem
might look like:

  Label: none  uuid: d3c1261f-18be-4015-9fef-6b35759dfdba
	  Total devices 1 FS bytes used 192.00KiB
	  devid    1 size 10.00GiB used 536.00MiB path /dev/mapper/3600140501cc1f49e5364f0093869c763

/dev/mapper/xxx is actually backed by an arbitrary number of paths,
which in turn are presented to the system as ordinary SCSI devices i.e
/dev/sdX. If a path flaps and a user re-runs 'btrfs fi show' the output
would look like:

  Label: none  uuid: d3c1261f-18be-4015-9fef-6b35759dfdba
	  Total devices 1 FS bytes used 192.00KiB
	  devid    1 size 10.00GiB used 536.00MiB path /dev/sdd

This only occurs on unmounted filesystems as those are enumerated by
btrfs-progs, for mounted filesystem the kernel properly deals only with
the actual multipath device.

Turns out the output of this command is consumed by libraries and the
presence of a path device rather than the actual multipath causes
issues.

Fix this by checking for the presence of DM_MULTIPATH_DEVICE_PATH
udev attribute as multipath path devices are tagged with this attribute
by the multipath udev scripts.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-06 16:50:35 +02:00
David Sterba c50c448518 btrfs-progs: do sysfs detection of device discard capability
The detection of the discard status of a device is done by issuing a
real discard request but on an empty range. This works in most cases.
However there's a case of a VirtualBox driver that returns 'Operation
not supported' in that case, and then discard is skipped during mkfs.

The other tools like fstrim check the sysfs queue file
discard_granularity which is the recommended way. Do that as well.

Issue: #390
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-06 16:49:30 +02:00
Naohiro Aota 53ec59ead0 btrfs-progs: do not zone reset on emulated zoned mode
We cannot zone reset a regular file with emulated zones. So, mkfs.btrfs
on such a file causes the following error.

  ERROR: zoned: failed to reset device '/home/naota/tmp/btrfs.img' zones: Inappropriate ioctl for device

Introduce btrfs_zoned_device_info->emulated to distinguish the zones are
emulated or not. And, use it to decide it needs zone reset or not.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-06 16:48:56 +02:00
David Sterba 4980de3a5f btrfs-progs: read partition size from sysfs as fallback
Reading partition size using an ioctl requires the device open, but that
does not work for unprivileged users. This leads to 0 size in device
info structures filled by device_get_partition_size.

As a consequence, this also misreports such devices as missing in 'fi
us' overview:

 $ btrfs fi us /
 WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
 Overall:
     Device size:		 411.35GiB
     Device allocated:		  53.01GiB
     Device unallocated:	 358.34GiB
     Device missing:		 411.35GiB
     Used:			  31.99GiB
     Free (estimated):		 379.16GiB	(min: 379.16GiB)
     Free (statfs, df):		 379.35GiB
     Data ratio:		      1.00
     Metadata ratio:		      1.00
     Global reserve:		 194.77MiB	(used: 0.00B)
     Multiple profiles:		        no

There should be 0 for 'Device missing'.

Add a fallback to read the device size from sysfs in case the ioctl is
not available.

Issue: #395
Signed-off-by: David Sterba <dsterba@suse.com>
2021-09-10 13:09:05 +02:00
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 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 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 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
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 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
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 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
Qu Wenruo 773afad3e6 btrfs-progs: slightly enhance btrfs_format_csum()
- Change it void
  The old one always return csum_size.

- Use snprintf()

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-08-26 14:27:01 +02:00
Qu Wenruo 991a598f53 btrfs-progs: move btrfs_format_csum() to common/utils.[ch]
Function btrfs_format_csum() is a special helper only used in
btrfs-progs.

Move it to common/utils.[ch] other than leaving it in
kernel-shared/disk-io.c.

Since we're moving the code, also introduce a macro,
BTRFS_CSUM_STRING_LEN, to replace open-coded string length calculation.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-08-26 14:26:13 +02:00
Goldwyn Rodrigues a7ed5b0ced btrfs-progs: correct check_running_fs_exclop() return value
check_running_fs_exclop() can return 1 when exclop is changed to "none"
The ret is set by the return value of the select() operation. Checking
the exclusive op changes just the exclop variable while ret is still
set to 1.

Set ret = 0 if exclop is set to BTRFS_EXCL_NONE or BTRFS_EXCL_UNKNOWN.
Remove unnecessary continue statement at the end of the block.

The command appears to have executed, but does not. This was found when
balance which typically reports chunks relocated did not print anything
on screen.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-07-02 17:27:53 +02:00
David Sterba 3d52313591 btrfs-progs: add helper to read zone size from sysfs
Sysfs hides the zone size of a block device in the queue/chunk_sectors
file, so add a helper that will read it for us when given the short
device name (that can be found in FSID/devices).

Signed-off-by: David Sterba <dsterba@suse.com>
2021-07-02 17:27:53 +02:00
David Sterba e25c8f00a3 btrfs-progs: add helper for opening sysfs fsid directory
There are several directories in /sys/fs/btrfs/FSID that contain more
than one file/directory. Add a helper to open the directory so that the
file descriptor can be used for fdopendir.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-07-02 17:27:53 +02:00
Su Yue 80a86f1b47 btrfs-progs: do not BUG_ON if btrfs_add_to_fsid succeeded to write superblock
Commit 8ef9313cf2 ("btrfs-progs: zoned: implement log-structured
superblock") changed to write BTRFS_SUPER_INFO_SIZE bytes to device.
The before num of bytes to be written is sectorsize.
It causes mkfs.btrfs failed on my 16k pagesize kvm:

  $ /usr/bin/mkfs.btrfs -s 16k -f -mraid0 /dev/vdb2 /dev/vdb3
  btrfs-progs v5.12
  See http://btrfs.wiki.kernel.org for more information.

  ERROR: superblock magic doesn't match
  ERROR: superblock magic doesn't match
  common/device-scan.c:195: btrfs_add_to_fsid: BUG_ON `ret != sectorsize`
  triggered, value 1
  /usr/bin/mkfs.btrfs(btrfs_add_to_fsid+0x274)[0xaaab4fe8a5fc]
  /usr/bin/mkfs.btrfs(main+0x1188)[0xaaab4fe4dc8c]
  /usr/lib/libc.so.6(__libc_start_main+0xe8)[0xffff7223c538]
  /usr/bin/mkfs.btrfs(+0xc558)[0xaaab4fe4c558]

  [1]    225842 abort (core dumped)  /usr/bin/mkfs.btrfs -s 16k -f -mraid0
  /dev/vdb2 /dev/vdb3

btrfs_add_to_fsid() now always calls sbwrite() to write
BTRFS_SUPER_INFO_SIZE bytes to device, so change condition of
the BUG_ON().
Also add comments for sbread() and sbwrite().

Signed-off-by: Su Yue <l@damenly.su>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-12 16:00:14 +02:00
David Sterba 3dad6bd6e9 btrfs-progs: use proper array designator for exclop initialization
Compiling with clang produces warnings like:

  common/utils.c:1670:31: warning: use of GNU 'missing =' extension in designator [-Wgnu-designator]
	  [BTRFS_EXCLOP_SWAP_ACTIVATE]    "swap activate",
					  ^
					  =

Use the form with '=' to fix the warning.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-08 00:58:51 +02:00
David Sterba a2495d1e6e btrfs-progs: export get_zone_unusable and move to utils.c
Getting the per bg type zone unusable space will be used in other size
reports like 'fi us', so export it to the device utils.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-08 00:58:50 +02:00
David Sterba c19ac510a7 btrfs-progs: move repair.[ch] to common/
Move the file to common as it's used by several parts, while still
keeping the name 'repair' although the only thing it does is adding a
corrupted extent.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:47 +02:00
David Sterba cfbcfaa4e4 btrfs-progs: mkfs: move btrfs_make_root_dir from utils.c
The helper is used in several tools but logically belongs to mkfs, so
put it to the common section.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:47 +02:00
David Sterba d591cd7c08 btrfs-progs: split unit related helpers from utils.c
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:47 +02:00
David Sterba 7fa07e2abb btrfs-progs: split open/close helpers from utils.c
There's a group of functions that are related to opening filesystem in
various modes, this can be moved to a separate file.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:47 +02:00
David Sterba b19a603d62 btrfs-progs: remove unnecessary linux/*.h includes
Decrease dependency on system headers, remove where they're not needed
or became stale after code moved. The path-utils.h encapsulate path
operations so include linux/limits.h here, that's where PATH_MAX is
defined.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:47 +02:00
David Sterba 600d2dba6f btrfs-progs: add fd version of device_get_partition_size
The helper wraps a raw ioctl but some users may already have the fd and
not necessarily the path. Add a suitable helper for convenience.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
David Sterba 2cfd248ddf btrfs-progs: remove unused disk_size
This helper hasn't been used since 63bbf2931d ("btrfs-progs: rework
calculations of fi usage") a few years ago and we don't need the statfs
based calculations anywhere.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
David Sterba a2dbbcfe88 btrfs-progs: update comments for device helpers
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
David Sterba 51c0ece9f6 btrfs-progs: add prefix to get_partition_size
This is a public helper for devices, add the prefix to make it clear.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
David Sterba c7b5f884e0 btrfs-progs: add prefix to zero_blocks
This is a public helper for devices, add the prefix to make it clear.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
David Sterba 2b5d4f2e6f btrfs-progs: add prefix to discard_blocks
This is a helper for devices, make it clear in the function name.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
David Sterba bc6864967b btrfs-progs: add prefix to exported queue_param
As this is a public helper, add a prefix that makes it clear what is the
queue related to.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
Naohiro Aota b42b7fbc32 btrfs-progs: zoned: support wiping superblock on sequential write zone
We cannot overwrite superblock magic in a sequential required zone.
Instead, we can reset the zone to wipe it.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
Naohiro Aota 8bbb0c5744 btrfs-progs: zoned: support zero out on zoned block device
If we zero out a region in a sequential write required zone, we cannot
write to the region until we reset the zone. Thus, we must prohibit zeroing
out to a sequential write required zone.

zero_dev_clamped() is modified to take the zone information and it calls
zero_zone_blocks() if the device is host managed to avoid writing to
sequential write required zones.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
Naohiro Aota 58ec593892 btrfs-progs: zoned: support resetting zoned device
All zones of zoned block devices should be reset before writing. Support
this by introducing PREP_DEVICE_ZONED.

btrfs_reset_all_zones() walk all the zones on a device, and reset a zone if
it is sequential required zone, or discard the zone range otherwise.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:46 +02:00
Naohiro Aota 8ef9313cf2 btrfs-progs: zoned: implement log-structured superblock
Superblock (and its copies) is the only data structure in btrfs which has a
fixed location on a device. Since we cannot overwrite in a sequential write
required zone, we cannot place superblock in the zone.  One easy solution
is limiting superblock and copies to be placed only in conventional zones.

However, this method has two downsides: one is reduced number of superblock
copies. The location of the second copy of superblock is 256GB, which is in
a sequential write required zone on typical devices in the market today.
So, the number of superblock and copies is limited to be two.  Second
downside is that we cannot support devices which have no conventional zones
at all.

To solve these two problems, we employ superblock log writing. It uses two
adjacent zones as a circular buffer to write updated superblocks.  Once the
first zone is filled up, start writing into the second one.  Then, when
both zones are filled up and before starting to write to the first zone
again, reset the first zone.

We can determine the position of the latest superblock by reading write
pointer information from a device. One corner case is when both zones are
full. For this situation, we read out the last superblock of each zone, and
compare them to determine which zone is older.

The following zones are reserved as the circular buffer on ZONED btrfs.

- primary superblock: offset   0B (and the following zone)
- first copy:         offset 512G (and the following zone)
- Second copy:        offset   4T (4096G, and the following zone)

If these reserved zones are conventional, superblock is written fixed at
the start of the zone without logging.

Currently, superblock reading/writing is done by pread/pwrite. This
commit replace the call sites with sbread/sbwrite to wrap the functions.
For zoned btrfs, btrfs_sb_io which is called from sbread/sbwrite
reverses the IO position back to a mirror number, maps the mirror number
into the superblock logging position, and do the IO.

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:45 +02:00
Naohiro Aota 384840b9c0 btrfs-progs: zoned: get zone information of zoned block devices
Get the zone information (number of zones and zone size) from all the
devices, if the volume contains a zoned block device. To avoid costly
run-time zone report commands to test the device zones type during block
allocation, it also records all the zone status (zone type, write
pointer position, etc.).

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:45 +02:00
Naohiro Aota 242c8328bc btrfs-progs: zoned: add new ZONED feature flag
With the zoned feature enabled, a zoned block device-aware btrfs
allocates block groups aligned to the device zones and always written in
sequential zones at the zone write pointer position.

It also supports "emulated" zoned mode on a non-zoned device. In the
emulated mode, btrfs emulates conventional zones by slicing the device
into fixed-size zones.

We don't support conversion from the ext4 volume with the zoned feature
because we can't be sure all the converted block groups are aligned to
zone boundaries.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:45 +02:00
Naohiro Aota acdd22ab68 btrfs-progs: provide fs_info from btrfs_device
Likewise in the kernel code, provide fs_info access from struct
btrfs_device. This will help to unify the code between the kernel and
the userland.

Since fs_info can be NULL at the time of btrfs_add_to_fsid(), let's use
btrfs_open_devices() to set fs_info to the devices.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:45 +02:00
Naohiro Aota c4d2704c9a btrfs-progs: utils: introduce queue_param helper function
Introduce the queue_param helper function to get a device request queue
parameter. This helper will be used later to query information of a zoned
device.

Furthermore, rewrite is_ssd() using the helper function.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
[Naohiro] fixed error return value
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:41:45 +02:00
Qu Wenruo 82611f51a5 btrfs-progs: mkfs: only output the warning if the sectorsize is not supported
Currently mkfs.btrfs will output a warning message if the sectorsize is
not the same as page size:

  WARNING: the filesystem may not be mountable, sectorsize 4096 doesn't match page size 65536

But since btrfs subpage support for 64K page size is coming, this output
is populating the golden output of fstests, causing tons of false
alerts.

This patch will teach mkfs.btrfs to check
/sys/fs/btrfs/features/supported_sectorsizes and check if the sector
size is supported.

Then only output above warning message if the sector size is not
supported or the file is not found (ie. kernel does not export the file
yet).

Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-05-06 16:40:51 +02:00
David Sterba ce2de8282b btrfs-progs: change formatting for plain text lines
Line continuations and not simple "\n" for the json output, this got
inherited to the plain text output, but this is not necessary.

This also caused problems in fstests btrfs/006 where the extra newline
does not match the golden output and the test fails, when printing
device stats that now use the output formatter.

Change the plain text formatting to always expect that a fmt_print or a
manual line print (like is for the device stats) will append the newline
and remove it from the end of formatting.

Link: https://lore.kernel.org/linux-btrfs/CAL3q7H4b7QhL02aSOpN0-k_9P2EAbj1t+NkA6VwidKEg4S996w@mail.gmail.com
Reported-by: Filipe Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-22 16:22:21 +01:00
Boris Burkov feb8f56ba2 btrfs-progs: receive: fix btrfs_mount_root substring bug
The current mount detection code in btrfs receive is not quite perfect.
For example, suppose /tmp is mounted as a tmpfs. In that case,
btrfs receive /tmp2 will find /tmp as the longest mount that matches a
prefix of /tmp2 and blow up because it is not a btrfs filesystem, even
if /tmp2 is just a directory in / mounted as btrfs.

Fix this by replacing the substring check with a dirname recursion to
only check the directories in the path of the dir, rather than every
substring.

Add a new test for this case.

Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-19 16:29:40 +01:00
Nikolay Borisov 3e653e801c btrfs-progs: remove duplicate checks from cmd_filesystem_resize
btrfs_open_dir already has a check whether the passed path is a
directory and if so it returns a specific error code (-3) when such an
error occurs. Use this instead of open-coding the directory check. To
avoid regression in cli/003 test also move directory checks before fs
type in btrfs_open.

Output before this check:

  ERROR: resize works on mounted filesystems and accepts only
  directories as argument. Passing file containing a btrfs image
  would resize the underlying filesystem instead of the image.

After:

  ERROR: not a directory: /root/btrfs-progs/tests/test.img
  ERROR: resize works on mounted filesystems and accepts only
  directories as argument. Passing file containing a btrfs image
  would resize the underlying filesystem instead of the image.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-19 15:24:42 +01:00
David Sterba b604e0eda2 btrfs-progs: remove workarounds for libmount and static build
Partial revert of 922eaa7b54 ("btrfs-progs: build: fix linking with
static libmount"), remove the necessary workarounds like the weak
symbols and link time warnings. Symbols renamed not to clash with
libmount (parse_size, canonicalize_path) haven't been reverted because
the new names are acceptable.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-19 15:24:42 +01:00
David Sterba e31679c1ec btrfs-progs: reimplement find_mount_fsroot without libmount
In commit 57cfe29e69 ("btrfs-progs: utils: introduce
find_mount_fsroot") the entries in /proc/self/mountinfo are parsed by a
convenience library libmount, because getmntent does not provide the
information we need to distinguish bind mounts.

Using libmount turned out to be problematic in several ways:

- static build got broken due to clashing symbols, eg. for parsing size
  or path canonicalization (#333)

- long-term distros do not have libmount new enough (2.24+) to provide
  some functions (mnt_table_is_empty, #334)

- libmount internally uses getgrnam_r/mnt_get_uid/... that are not
  static-build friendly, a warning is printed during link time for each
  binary; we don't use any of the functions

- libmount has further library dependencies that we don't need:

  $ ldd /usr/lib64/libmount.so.1
	  linux-vdso.so.1 (0x00007fff4f175000)
	  libc.so.6 => /lib64/libc.so.6 (0x00007f44a1763000)
	  libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f44a1730000)
	  libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007f44a1704000)
	  /lib64/ld-linux-x86-64.so.2 (0x00007f44a1998000)
	  libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f44a166c000)
	  libdl.so.2 => /lib64/libdl.so.2 (0x00007f44a1666000)

  namely selinux, pcre and dl.

Summing it up, libmount causes more trouble than it's worth using a
convenience library, we want to keep the dependencies minimal so the
custom mountinfo parser was inevitable.

Issue: #333
Issue: #334
Issue: #336
Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-19 15:24:40 +01:00
David Sterba 2347b34af4 btrfs-progs: fix device mapper path canonicalization
Commit 922eaa7b54 ("btrfs-progs: build: fix linking with static
libmount") broke path canonicalization, that prevented eg 'device add
/dev/dm-0' to properly recognize the device mapper names.

Issue: #339
Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-19 15:18:39 +01:00
David Sterba 922eaa7b54 btrfs-progs: build: fix linking with static libmount
The libmount dependency has been added in commit 61ecaff036
("btrfs-progs: build: add libmount dependency"), and static build got
broken. There are functions that do basically the same thing and also
share the name, which in turn fails at link time.

  ld: /../lib64/libmount.a(libcommon_la-canonicalize.o): in function `canonicalize_dm_name':
  util-linux-2.34/lib/canonicalize.c:58: multiple definition of `canonicalize_dm_name';
	  common/path-utils.static.o:btrfs-progs/common/path-utils.c:286: first defined here

In case the collision can be resolved by renaming, it's done
(canonicalize_path and parse_size). There are 2 symbols from selinux
that are substituted by a weak aliases during the static build.

There's one new warning due to use of getgrnam_r in libmount that
depends on dynamic linking and may not work properly with static build.
We're not using the related functions directly or indirectly, so it
should be safe to ignore the warnings.

  ld: ../lib64/libmount.a(la-utils.o): in function `mnt_get_gid':
  util-linux-2.34/libmount/src/utils.c:625: warning: Using 'getgrnam_r' in statically linked applications
  +requires at runtime the shared libraries from the glibc version used for linking

Issue: #333
Signed-off-by: David Sterba <dsterba@suse.com>
2021-01-25 23:31:56 +01:00
Sheng Mao 9870d504d8 btrfs-progs: align receive buffer to enable fast CRC
To use optimized CRC implementation, the input buffer must be
unsigned long aligned. btrfs receive calculates checksum based on
read_buf, including btrfs_cmd_header (with zeroed CRC field)
and command content.

Reorder the buffer to the beginning of the structure and force the
alignment to 64, this should be cacheline friendly and could speed up
the data transfers.

Interesting parts from the report:

Sending host:
Fedora 33
AMD ThreadRipper 1920X - 128GB RAM
2x10GBit Ethernet, bonded
MegaRaid 9270
6x16TB Seagate Exos in RAID5

Receiving host:
Fedora 33
Intel i3-7300 - HT enabled - 32GB RAM
10GBit Ethernet, single connection
MegaRaid 9260
12x8TB WD NAS drives in RAID5

The 2 hosts are connected to the same 10G switch. The sender could definitely
saturate a 10GBit link. The practically achievable writes on the backup host
would be lower, but still at least 400MB/s. The file system contains mostly
large files of 1GB+, so there is little meta-data.

With btrfs send/receive I'm getting a steady transfer rate of 60MB/s. The copy
has been running for a little over 5 days now, having only transferred some
25TB. This is way too slow for this setup.

Analyzing resource usage, the sender side is fine, both the btrfs send and the
corresponding ssh process only use about 10-10% CPU, which on a 24 threaded
machine is virtually nothing. However, the receiver is running with a load of
~2.6, with the sshd using 30-50% CPU and the btrfs receive a further 60-70%.
The rest of the load comes from IO wait. So the bottleneck is the btrfs receive
clearly.

Issue: #324
Signed-off-by: Sheng Mao <shngmao@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-01-18 17:49:22 +01:00
Marcos Paulo de Souza 57cfe29e69 btrfs-progs: utils: introduce find_mount_fsroot
This new function checks for filesystem path name that was mounted, thus
being different from find_mount_root. By using libmount we can easily
parse /proc/self/mountinfo file and check for the pathname field.

The function is useful to filter bind mounts with content different from
the original mount, thus making it safe to assume that the reported path
can be accessed by the user, with the right content.

Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-01-18 17:49:22 +01:00
David Sterba 29e2dd7a23 btrfs-progs: initialize formatter context properly
In cases where the compiler does not initialize the formatter context to
all zeros, there could be garbage values left on the depth 0 that is not
explicitly initialized. This could lead to mistakenly printing a ","
separator before the last closing "}", like

{
  "__header": {
    "version": "1"
  },
}

Signed-off-by: David Sterba <dsterba@suse.com>
2021-01-13 22:33:09 +01:00
Sidong Yang 2e8906829a btrfs-progs: extend fmt_print_start_group to handle unnamed group
Extends fmt_print_start_group() so it can handle when name argument is
NULL. It is useful for printing unnamed array or map.

Signed-off-by: Sidong Yang <realwakka@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-12-16 17:08:53 +01:00
David Sterba e198c6674a btrfs-progs: add enqueue parameter for exclusive ops
The exclusive ops will not start if there's one already running. Now
that we have the sysfs export (since kernel 5.10) to check if there's
one already running, use it to allow enqueueing of the operations as a
convenience.

Supported enqueuing:

  btrfs balance start --enqueue
  btrfs filesystem resize --enqueue
  btrfs device add --enqueue
  btrfs device delete --enqueue
  btrfs replace start --enqueue

This patch implements the functionality based on Goldwyn's patch
https://lore.kernel.org/linux-btrfs/?q=20200825150338.32610-4-rgoldwyn%40suse.de
but on top of previous preparatory patches.

Note that 'filesystem resize' options could confuse getopt as the
negative size change looks like a series of short options and there's no
way to make getopt ignore the short options, so there's a custom option
parser.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.de>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-12-16 17:08:12 +01:00
Qu Wenruo 90020a7605 btrfs-progs: mkfs: refactor how we handle sectorsize override
There are several problems for current sectorsize check:
- No check at all for sectorsize
  This means you can even specify "-s 62k".

- No way to specify sectorsize smaller than page size

Fix all these problems by:
- Introduce btrfs_check_sectorsize()
  To do:
  * power of 2 check for sectorsize
  * lower and upper boundary check for sectorsize
  * warn about sectorsize mismatch with page size

- Remove the max() between page size and sectorsize
  This allows us to override the sectorsize for 64K page systems.

- Make nodesize calculation based on sectorsize
  No need to use page size any more.
  Users who specify sectorsize manually really know what they are doing,
  and we have warned them already.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-12-10 17:10:30 +01:00
David Sterba 3850054385 btrfs-progs: add helper to check or wait for exclusive operation
Add helper that will either check a running operation or wait until it's
done, so that commands can be started and enqueued. If there are more
enqueued, an attempt to avoid racing is done based on the remaining
waiting time of each command.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-12-10 17:10:29 +01:00
David Sterba 639ddf61ca btrfs-progs: add helpers for parsing filesystem exclusive operation
Since kernel 5.10, the file /sys/fs/btrfs/FSID/exclusive_operation
exports textual id of the running exclusive operation (balance, device
add/remove, ...). Add definitions and parsing functions so they can be
used to check before another operation is started and potentially
blocks.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-12-10 17:10:26 +01:00
Goldwyn Rodrigues 34f0594bca btrfs-progs: add sysfs file reading helpers
Add helpers to open and read sysfs files from the per-fs directory.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-11-10 16:45:03 +01:00
David Sterba fbc12fd9ec btrfs-progs: move path_cat_out helpers to path-utils
The path-util.[ch] is the right place, keep the send-utils.h prototypes
as it's part of libbtrfs headers.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-11-04 23:26:43 +01:00
Goldwyn Rodrigues b79713a33e btrfs-progs: add get_fsid_fd() for getting fsid using fd
Add a function get_fsid_fd() to use an open file fd to get the
fsid of the mounted filesystem.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-11-04 22:46:44 +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
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 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 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 cdaf906d68 btrfs-progs: move send-utils.c to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:04 +02:00
David Sterba f6009c4cb3 btrfs-progs: move send-stream.c to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:04 +02:00
David Sterba 687ca4d127 btrfs-progs: move utils-lib.c to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:04 +02:00
David Sterba a4122790ac btrfs-progs: move extent-cache.c to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-08-31 17:01:04 +02:00
Qu Wenruo 2eb48d8c81 btrfs-progs: fix seemly wrong format overflow warning
[WARNING]
When compiling btrfs-progs, the following warning pops up:
  In file included from /usr/include/stdio.h:867,
                   from ./kerncompat.h:22,
                   from common/fsfeatures.c:17:
  In function 'printf',
      inlined from 'process_features' at common/fsfeatures.c:192:4,
      inlined from 'btrfs_process_runtime_features' at common/fsfeatures.c:205:2:
  /usr/include/bits/stdio2.h:107:10: warning: '%s' directive argument is null [-Wformat-overflow=]
    107 |   return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
        |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This only occur with default make parameters. If compiling with D=1, the
warning just disappears.

The involved tool chain is:
- GCC 10.1.0

[CAUSE]
The offending code is:
  static void process_features(u64 flags, enum feature_source source)
  {
  ...
		if (flags & feat->flag) {
			printf("Turning ON incompat feature '%s': %s\n",
				feat->name, feat->desc);
		}
  ...
  }

Currently, there is no runtime/fs feature without a name nor
description.  So we shouldn't hit a feature with NULL as name nor
description.

This looks like a bug in GCC though.

[WORKAROUND]
However can workaround it by doing an explicit check on feat->name and
feat->desc to teach GCC not to do a wrong warning.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-29 17:45:39 +02:00
David Sterba 79bb50787a btrfs-progs: add separate verbosity level for on-by-default messages
For backward compatibility with tools that may rely on the messages we
need a special level to print the message unless the verbosity settings
haven't been set on command line.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-12 19:32:29 +02:00
Anand Jain aeb2242b70 btrfs-progs: refactor btrfs_scan_devices() to accept verbose argument
Function btrfs_scan_devices() is being used by commands such as
'btrfs filesystem' and 'btrfs device', by having the verbose argument in
the btrfs_scan_devices() we can control which threads to print the
messages when verbose is enabled by the global option.

Add an option %verbose to btrfs_scan_devices().

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-12 19:31:58 +02:00
Anand Jain 018448ed62 btrfs-progs: add global verbose and quiet options and helper functions
Add --verbose and --quiet command options to show verbose or no output
from the subcommands.  By introducing global a bconf::verbose memeber to
propagate the same down to the subcommand.

Further the added helper function pr_verbose() helps to logs the verbose
messages, based on the state of the %bconf::verbose. And further HELPINFO_
defines are provided for the usage.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-12 19:30:43 +02:00
Anand Jain b5a6cff94d btrfs-progs: split global help HELPINFO_INSERT_GLOBALS
As of now the define HELPINFO_INSERT_GLOBALS if used as in the example
as below (as of now its not been used anywhere) will print the help
texts as shown below

$ ./btrfs fi show --help

 <snip>

    Global options:
    --format TYPE      where TYPE is: text

So in preparation to add --verbose and --quiet global options, and
apparently --format is not being used yet, this patch splits the global
options into two defines.

                                       "Global options:"

So that the currently added global options --verbose and --quiet can use
the define HELPINFO_INSERT_GLOBALS header as shown below.

$ ./btrfs fi show --help
<snip>

    Global options:

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-12 19:30:36 +02:00
Qu Wenruo 6ee4ab24da btrfs-progs: mkfs: introduce quota runtime feature
Add support for enabling quotas at mkfs time. The qgroup accounting will
be consistent, ie. works with --rootdir.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-28 19:59:56 +02:00
Qu Wenruo 7ddd0a1d1e btrfs-progs: fsfeatures: introduce runtime features
Make the features structures more generic to allow mkfs-time and
mount-time sets to be defined.

This provides base for later mkfs support of mount-time features like
quotas.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-28 19:59:56 +02:00
David Sterba 3f92fe88c3 btrfs-progs: fi usage: list multiple profiles type
Update the summary of 'fi usage' where the multiple profiles will be
listed by type, like:

    Multiple profiles:                 yes      (data, metadata)

The string is returned from btrfs_test_for_multiple_profiles so the
callers don't have to assemble it together from the other profile
strings.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-05 17:04:12 +02:00
David Sterba da7da3f661 btrfs-progs: fix detection of multiple profiles when generating the strings
The warning header was printed always, even if there weren't multiple
block group profiles. This also fixes the mixed block group profile
detection.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 22:12:28 +02:00
David Sterba 3e6c9bed5c btrfs-progs: adjust multiple block group warning format
Use simpler output format for easier parsing and place each block group
type on a separate line.

Example output:

WARNING: Multiple block group profiles detected, see 'man btrfs(5)'.
WARNING:   Data: single, raid1

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 22:05:43 +02:00
David Sterba 047dc287ec btrfs-progs: reorder single to be first in multiple bg list
Move 'single' as the first in the list of the multiple block groups, as
it's the default block group and the simplest.

Example output:

  WARNING: data -> [single, raid1], metadata -> [single], system -> [single]

  WARNING: data+metadata -> [single], system -> [raid1]

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 22:05:43 +02:00
David Sterba 297df4f01a btrfs-progs: simplify string separator checks in sprint_profiles
We don't need an extra variable to track if there's already some output
in the buffer.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 22:05:43 +02:00
David Sterba 512a45dad2 btrfs-progs: simplify string dump of block group profiles
Simpify sprint_profiles so it does not take the output parameters
optionally and add stubs to btrfs_test_for_multiple_profiles_by_fd.
This allows to remove all conditionals and reduce parameters of
sprint_profiles so that the output is returned directly.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 22:05:43 +02:00
David Sterba 2432ed89ba btrfs-progs: unexport btrfs_get_string_for_multiple_profiles
It's used only by the interface functions so does not need to be
exported.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 20:49:02 +02:00
David Sterba 5268a125ad btrfs-progs: rename helpers for multiple block group checks
The term 'mixed' is confusing as it's commonly used for mised block
group profiles created by 'mkfs.btrfs --mixed'. We're interested in
multiple profiles for each type, so use the term 'multiple'.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 20:49:02 +02:00
David Sterba bb8995000c btrfs-progs: remove unused function btrfs_check_for_mixed_profiles_by_path
It's not used by any of the commands printing the warning as the fd is
always available.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 20:49:02 +02:00
Goffredo Baroncelli b499dbe8e1 btrfs-progs: add code for checking mixed profile function
Add code to show a warning if a mixed profiles filesystem is detected.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-05-04 20:48:24 +02:00
Adam Borowski 3d379b1341 btrfs-progs: lots of typo fixes (codespell)
Signed-off-by: Adam Borowski <kilobyte@angband.pl>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-03-31 18:37:38 +02:00
David Sterba 35ba1f5fb6 btrfs-progs: move common-defs to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2020-03-31 18:37:35 +02:00
David Sterba 1f5094bb5c btrfs-progs: add support for raid1c3 and raid1c4
Add support for 3- and 4- copy variants of RAID1. This adds resiliency
against 2 or resp. 3 devices lost or damaged.

$ ./mkfs.btrfs -m raid1c4 -d raid1c3 /dev/sd[abcd]

Label:              (null)
UUID:               f1f988ab-6750-4bc2-957b-98a4ebe98631
Node size:          16384
Sector size:        4096
Filesystem size:    8.00GiB
Block group profiles:
  Data:             RAID1C3         273.06MiB
  Metadata:         RAID1C4         204.75MiB
  System:           RAID1C4           8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata, raid1c34
Number of devices:  4
Devices:
   ID        SIZE  PATH
    1     2.00GiB  /dev/sda
    2     2.00GiB  /dev/sdb
    3     2.00GiB  /dev/sdc
    4     2.00GiB  /dev/sdd

Signed-off-by: David Sterba <dsterba@suse.com>
2019-11-22 19:09:50 +01:00
David Sterba 65efb419a2 btrfs-progs: move parse_csum_type to utils
This will be used by convert.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-11-18 19:21:09 +01:00
Marcos Paulo de Souza 575b6e0e51 btrfs-progs: utils: Replace __attribute__(fallthrough)
When compiling with clang, this warning is shown:

common/utils.c:404:3: warning: declaration does not declare anything [-Wmissing-declarations]
                __attribute__ ((fallthrough));

This attribute seems to silence the same warning in GCC. Changing this
attribute with /* fallthrough */ fixes the warning for both gcc and
clang.

Full support for the attribute will be in clang 10, gcc supports that
now. Let's use what works for both and switch to the attribute in the
future.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-11-18 19:21:08 +01:00
Johannes Thumshirn c04bcdcacc btrfs-progs: move crc32c implementation to crypto/
With the introduction of xxhash64 to btrfs-progs we created a crypto/
directory for all the hashes used in btrfs (although no
cryptographically secure hash is there yet).

Move the crc32c implementation from kernel-lib/ to crypto/ as well so we
have all hashes consolidated.

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-11-18 19:20:02 +01:00
Rosen Penev 5d72055066 btrfs-progs: Fix printf formats
Discovered with cppcheck. Fix signed/unsigned int mismatches, sizeof and
long formats.

Pull-request: #197
Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-10-14 17:31:05 +02:00
Jeff Mahoney f41f929854 btrfs-progs: constify argument of parse_size
The argument isn't changed inside the function.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
[ split from the original patch ]
Signed-off-by: David Sterba <dsterba@suse.com>
2019-10-14 17:27:50 +02:00
David Sterba 88fc7becac btrfs-progs: pretty print device size in overflow error message
Signed-off-by: David Sterba <dsterba@suse.com>
2019-09-04 16:08:31 +02:00
Jeff Mahoney d29f475108 btrfs-progs: check if adding device would overflow while scanning
It's theoretically possible to add multiple devices with sizes that add up
to or exceed 16EiB.  A file system will be created successfully but will
have a superblock with incorrect values for total_bytes and other fields.

Kernels up to v5.0 will crash when they encounter this scenario.

We need to check for overflow and reject the device if it would overflow.

Bugzilla: https://bugzilla.suse.com/show_bug.cgi?id=1099147
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-09-04 16:08:23 +02:00
Adam Borowski 1e6246eaec btrfs-progs: fix a printf format string fatal warning
At least in Debian, default build flags include -Werror=format-security,
for good reasons in most cases.  Here, the string comes from strftime --
and though I don't suspect any locale would be crazy enough to have %X
include a '%' char, the compiler has no way to know that.

Signed-off-by: Adam Borowski <kilobyte@angband.pl>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-26 17:46:41 +02:00
David Sterba bd4a386ec5 btrfs-progs: build most common tools into one binary (busybox style)
Build several standalone tools into one binary and switch the function
by name (symlink or hardlink).

* btrfs
* mkfs.btrfs
* btrfs-image
* btrfs-convert
* btrfstune

The static target is also supported. The name of resulting boxed
binaries is btrfs.box and btrfs.box.static . All the binaries can be
built at the same time without prior configuration.

   text    data     bss     dec     hex filename
 822454   27000   19724  869178   d433a btrfs
 927314   28816   20812  976942   ee82e btrfs.box
2067745   58004   44736 2170485  211e75 btrfs.static
2627198   61724   83800 2772722  2a4ef2 btrfs.box.static

File sizes:

  857496  btrfs
  968536  btrfs.box
 2141400  btrfs.static
 2704472  btrfs.box.static

Standalone utilities:

  512504  btrfs-convert
  495960  btrfs-image
  471224  btrfstune
  491864  mkfs.btrfs

 1747720  btrfs-convert.static
 1411416  btrfs-image.static
 1304256  btrfstune.static
 1361696  mkfs.btrfs.static

So the shared 900K binary saves ~2M, or ~5.7M for static build.

Signed-off-by: David Sterba <dsterba@suse.cz>
2019-07-04 15:30:40 +02:00
David Sterba a62b7854de btrfs-progs: utils: split device scanning functions to own file
All helpers and data structures that are used for device registration.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:34 +02:00
David Sterba ccbea0977b btrfs-progs: utils: split device handling functions to own file
Helpers that read size, do zeoring, trim or prepare/finalize the device.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:34 +02:00
David Sterba b98a984f0b btrfs-progs: path-utils: rename test_isdir
Rename so it follows the path_ conventions.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:33 +02:00
David Sterba fc86737f91 btrfs-progs: path-utils: rename is_existing_blk_or_reg_file
Rename and add path_ prefix.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:33 +02:00
David Sterba 116e27a87a btrfs-progs: path-utils: rename is_path_exist
Rename so it fits the path_ group.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:33 +02:00
David Sterba facc0b130e btrfs-progs: path-utils: rename is_mount_point
Add path_ prefix and update callers.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:33 +02:00
David Sterba 89ad5e9dba btrfs-progs: path-utils: rename is_reg_file
Add path_ prefix and update callers.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:33 +02:00
David Sterba c3b0e6970b btrfs-progs: path-utils: rename is_block_device
Add the path_ prefix and update all callers.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:33 +02:00
David Sterba f83e81c61b btrfs-progs: utils: split path related utils to own file
Group helpers that return some status of a given path to own file so we
debloat utils.c a bit.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-04 02:06:31 +02:00
David Sterba 3122085a79 btrfs-progs: output formatter infrastructure
Add structures and API for unified output definition and multiple
formatting backends. Currently there's plain text and json.

The format of each row is defined in struct rowspec, selected using a
key and formatted according to the type. There are extended types for
eg. UUID or pretty size, while direct printf format specifiers work too.

Due to different nature of the outputs, the context structure members
are not always used.

* text output mostly uses indentation and formats the name to a given
  width

* json output tracks nesting depth and keeps stack of previous groups
  (list or array) and how many member have been printed, as the
  separators are allowed only between values and must not preced the
  group closing bracket
  the nesting depth is hardcoded to 16, counting the global group

The API provides functions to print simple values and some helpers to
format more complex structures.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:04 +02:00
David Sterba 17aee13fcd btrfs-progs: define new output format: json
Add the definition, the format is not implemented in any command.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:04 +02:00
David Sterba eb6a09050f btrfs-progs: help: add helpinfo marker for global options
Global options should be printed right after the command options, but
there could be text following the options. Add a marker that will allow
to order the options before that text.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:04 +02:00
Jeff Mahoney a1a5000984 btrfs-progs: add support for output formats
This adds a global --format option to request extended output formats
from each command.

We currently only support text mode.  Command help reports what
output formats are available for each command.  Global help reports
what valid formats are.

If an invalid format is requested, an error is reported and lists the
valid formats.

Each command sets a bitmask that describes which formats it is capable
of outputting.  If a globally valid format is requested of a command
that doesn't support it, an error is reported and command usage dumped.

Commands don't need to specify that they support text output.  All
commands are required to output text.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
[ use global config instead of passing cmd_context ]
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:04 +02:00
David Sterba c8bea2b84b btrfs-progs: move rbtree-utils.[ch] to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:04 +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 c07960c8be btrfs-progs: move utils.[ch] to common/
Update include paths and remove some duplicates.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:04 +02:00
David Sterba 408cec5086 btrfs-progs: move string-table.[ch] to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:03 +02:00
David Sterba 07ca08ffba btrfs-progs: move fsfeatures.[ch] to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:03 +02:00
David Sterba aac564aca6 btrfs-progs: move commonh to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:03 +02:00
David Sterba f93b471143 btrfs-progs: move help.[ch] to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:03 +02:00
David Sterba d0970a05cd btrfs-progs: move task-utils.[ch] to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:03 +02:00
David Sterba d1efe50d0a btrfs-progs: move messages.[ch] to common/
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:03 +02:00
David Sterba f63f29e9e9 btrfs-progs: move internal.h to common/
Create directory for all sources that can be used by anything that's not
rellated to a relevant kernel part, all common functions, helpers,
utilities that do not fit any other specific category.

The traditional location would be probably lib/ with all things that are
statically linked to the main binaries, but we have libbtrfs and
libbtrfsutil so this would be confusing.

Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:03 +02:00