Commit Graph

289 Commits

Author SHA1 Message Date
Qu Wenruo
6dfc69104b btrfs-progs: add extra chunk alignment checks
Recently we had a scrub use-after-free caused by unaligned chunk
length, although the fix was submitted, we may want to do extra checks
for a chunk's alignment.

This patch adds such check for the starting bytenr and length of a
chunk, to make sure they are properly aligned to 64K stripe boundary.

By default, the check only leads to a warning but is not treated as an
error, as we expect kernel to handle such unalignment without any
problem.

But if the new debug environmental variable,
BTRFS_PROGS_DEBUG_STRICT_CHUNK_ALIGNMENT, is specified, then we will
treat it as an error.  So that we can detect unexpected chunks from
btrfs-progs, and fix them before reaching the end users.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-16 19:14:37 +01:00
David Sterba
87dba20daf btrfs-progs: change all sysfs helpers to return errno
To be consistent with the rest of the code the sysfs helper should
return the -errno instead of passing -1 from various syscalls. Update
callers that relied on -1 as the invalid file descriptor.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-09 01:17:22 +01:00
David Sterba
4576029dfd btrfs-progs: add write helpers for sysfs files
Add convenience wrappers for writing a buffer or u64 to toplevel or FSID
file in sysfs.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-09 01:17:22 +01:00
David Sterba
8ea9a3e2c6 btrfs-progs: print a message when enqueued operation is waiting
The enqueue option should let the user know that the expected operation
hasn't started yet and that it's waiting for another one. Although the
exclusive operations can take long, the two reason should be
distinguished.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-01 03:22:29 +01:00
David Sterba
a0468efe86 btrfs-progs: reset errno before strtoull()
strtoull may return the boundary values, if the callers could expect
that and verify it then the errno must be reset before the call.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-01 02:13:37 +01:00
David Sterba
e9a66cde91 btrfs-progs: use get_sysfs_proto_supported() in device_get_zone_unusable()
Use the sysfs helper to read u64 values of the zoned stats.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-01 02:08:37 +01:00
Sam James
ae9ead781d btrfs-progs: fix -Walloc-size warnings reported by gcc 14
GCC 14 introduces a new -Walloc-size included in -Wextra which gives:
```
common/utils.c:983:15: warning: allocation of insufficient size ‘1’ for type ‘struct config_param’ with size ‘32’ [-Walloc-size]
cmds/qgroup.c:1644:13: warning: allocation of insufficient size ‘1’ for type ‘struct btrfs_qgroup_inherit’ with size ‘72’ [-Walloc-size]
```

The calloc prototype is:
```
void *calloc(size_t nmemb, size_t size);
```

So, just swap the number of members and size arguments to match the prototype, as
we're initialising 1 struct of size `sizeof(struct ...)`. GCC then sees we're not
doing anything wrong.

Pull-request: #707
Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-14 13:14:24 +01:00
David Sterba
f240c6114d btrfs-progs: open path in non-blocking mode when reading fs id
There's a report that reading properties from a sound device the system
is stuck and then gets rebooted by watchdog. Reading from fifo files
gets stuck as well, although this would not trigger the watchdog.

The reason is that open() on fifo files is blocking until the other end
of the pipe is opened. For device nodes it's driver specific, most
device nodes fail right away:

  $ btrfs prop get /dev/tty
  ERROR: object is not a btrfs object: /dev/tty

In case of the sound device the consequences were fatal. We can fix that
by opening the path on non-blocking mode. This is only for reading the
fsid, the fd is closed right after the ioctl so the non-blocking mode
does not affect other operation.

The blocking mode must be used for block devices as e.g. loop devices
may not be finalized when the open() call returns and get_fsid fails.
The known problematic devices are character and fifos.

Issue: #699
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
David Sterba
39c63831a0 btrfs-progs: add support for dry-run options
Some commands could be run in a dry-run mode, i.e. not doing any
write/change actions, only printing the steps and ignoring errors.

There are two possibilities where to put the option:

- as a global one: btrfs --dry-run subvolume delete /path
- local option:    btrfs subvolume delete --dry-run /path

As we have several global options already, let's put it there, dry-run
should not be very common so the slight inconvenience of writing the
option out of order of command arguments should be acceptable.

Issue: #629
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
David Sterba
75a451240e btrfs-progs: common: drop unsigned long long casts for printf
The %llu specifier does not need the typecast for ULL for a long time,
remove it.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
David Sterba
12b1b03974 btrfs-progs: add support for generic global parameters
./btrfs --param key=value command ...
./btrfs --param key command ...

To pass various tuning data for testing and debugging, undocumented
for regular users.

To add support add reading of the parameter value after option parsing
bconf_param_value("key") and convert to what you need.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
David Sterba
b421fdff95 btrfs-progs: move raid-stripe-tree and squota build out of experimental
The kernel patches for RST and squota are queued for 6.7, we need to be
able to test the features so it's not necessary to hide the mkfs support
under experimental build. The kernel may still need debug build to
enable mount.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-17 19:33:59 +02:00
David Sterba
10bc66034f btrfs-progs: add sysfs name for raid-stripe-tree
The raid-stripe-tree sysfs export was missing in the descriptor.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-17 19:33:59 +02:00
David Sterba
23475af2be btrfs-progs: fix sysfs name of squota
Current kernel version of squota exports the feature as 'simple_quota'.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-17 19:33:59 +02:00
David Sterba
166db10ceb btrfs-progs: mkfs: add short aliases for long feature names
While we like to have the descriptive names also add short aliases that
we also use for reference in changelogs and documentation.

  $ mkfs.btrfs -O list-all
  Filesystem features available:
  mixed-bg            - mixed data and metadata block groups (compat=2.6.37, safe=2.6.37)
  quota               - quota support (qgroups) (compat=3.4)
  extref              - increased hardlink limit per file to 65536 (compat=3.7, safe=3.12, default=3.12)
  raid56              - raid56 extended format (compat=3.9)
  skinny-metadata     - reduced-size metadata extent refs (compat=3.10, safe=3.18, default=3.18)
  no-holes            - no explicit hole extents for files (compat=3.14, safe=4.0, default=5.15)
  fst                 - free-space-tree alias
  free-space-tree     - free space tree (space_cache=v2) (compat=4.5, safe=4.9, default=5.15)
  raid1c34            - RAID1 with 3 or 4 copies (compat=5.5)
  zoned               - support zoned devices (compat=5.12)
  extent-tree-v2      - new extent tree format (compat=5.15)
  bgt                 - block-group-tree alias
  block-group-tree    - block group tree to reduce mount time (compat=6.1)
  rst                 - raid-stripe-tree alias
  raid-stripe-tree    - raid stripe tree (compat=6.7)
  squota              - squota support (simple accounting qgroups) (compat=6.7)

Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-17 19:33:59 +02:00
David Sterba
723137547b btrfs-progs: mkfs: sort features by compat version
The list 'mkfs -O list-all' should be sorted by version of kernel
support (compat) so it's clear what's new.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-17 19:33:59 +02:00
Qu Wenruo
146cca7e16 btrfs-progs: move clear-cache.[ch] from check/ to common/ directory
The clear-cache functionality is shared by several commands:

- btrfs check
  For --clear-cache and --clear-ino-cache.

- btrfstune
  Mostly for block-group-tree feature conversion.

- btrfs-convert
  To enable the now default v2 space cache.

Thus it's no longer proper to keep clear-cache.[ch] under check/
directory, move them to common/ directory.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-13 18:13:12 +02:00
Qu Wenruo
c788977878 btrfs-progs: pull in the full max/min/clamp implementation from kernel
The current implementation would introduce variable shadowing due to
both max() and min() are using the same __x and __y.

This may not be a big deal, but since kernel is already handling it
properly using __UNIQUE_ID() macro, and has more checks, we can
cross-port the kernel version to btrfs-progs.

There are some dependency needed, they are all small enough thus can be
put into the helper.

- __PASTE()
- __UNIQUE_ID()
- BUILD_BUG_ON_ZERO()
- __is_constexpr()

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-10 19:16:29 +02:00
David Sterba
21aa6777b2 btrfs-progs: clean up includes, using include-what-you-use
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-03 01:11:57 +02:00
Josef Bacik
3808db2b3e btrfs-progs: move btrfs_record_file_extent and code into a new file
This function and it's related functions only exist for the utilities
that populate existing file systems, and do not exist in the upstream
kernel.  Move this function and the related function into it's own
common source file and out of the kernel-shared sources, and then update
all of the users to include the new location of this code.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-03 01:11:56 +02:00
Boris Burkov
14ac1a6051 btrfs-progs: mkfs: add support for squota
Add the ability to enable simple quotas from mkfs with '-O squota'

There is some complication around handling enable_gen while still
counting the root node of an fs. To handle this, employ a hack of doing
a no-op write on the root node to bump its generation up above that of
the qgroup enable generation, which results in counting it properly.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-03 01:11:55 +02:00
Johannes Thumshirn
b4ab282686 btrfs-progs: allow zoned RAID
Allow for RAID levels 0, 1 and 10 on zoned devices if the RAID stripe tree
is used.

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-02 18:41:08 +02:00
David Sterba
8d3193da89 btrfs-progs: increase size of BTRFS_FEATURE_STRING_BUF_SIZE
We're getting more features and the string size limit will not be
sufficient, so extend enough that we won't have to care for some time.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-02 18:41:08 +02:00
David Sterba
03f41ac508 btrfs-progs: detect PCLMUL CPU support for accelerated crc32c
The accelerated crc32c needs to check for two CPU features, the crc32c
instructions is in SSE 4.2 and 'pclmulqdq' is a separate. There's still
old hardware used that does not have the PCLMUL instructions. Detect it
and make it the condition.

The pclmul is not supported on old compilers so also add a
configure-time detection and leave the SSE 4.2 only implementation as
the accelerated one if possible.

Issue: #676
Signed-off-by: David Sterba <dsterba@suse.com>
2023-09-13 00:38:50 +02:00
Anand Jain
a11d468e98 btrfs-progs: rename fs_devices::list to match the kernel
Aligning with the kernel's struct btrfs_fs_devices:fs_list, rename
btrfs_fs_devices::list to btrfs_fs_devices::fs_list.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:58 +02:00
David Sterba
5b9206727b btrfs-progs: rename and move get_device_info
The helper belongs to the device-utils, move it there and use the common
prefix.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:24 +02:00
David Sterba
83ac6e0a72 btrfs-progs: crypto: make the PCL implementation default for crc32c
Drop the old native intel implementation and use the PCL one. Remove the
artifical CPU flags.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:24 +02:00
David Sterba
992be8b50a btrfs-progs: crypto: add PCL based implementation for crc32c
Copy faster implementation of crc32c from linux kernel as of 6.5-rc7
(x86_64, arch/x86/crypto/crc32c-pcl-intel-asm_64.S). This needs
assembler build support, so detect target architecture so
cross-compilation still works.

Add a special CPU flag so the old and new implementations can be
benchmarked and verified separately.

Sample benchmark:

CPU flags: 0x1ff
CPU features: SSE2 SSSE3 SSE41 SSE42 SHA AVX AVX2 CRC32C_PCL
Block size:     4096
Iterations:     1000000
Implementation: builtin
Units:          CPU cycles

      NULL-NOP: cycles:     77177218, cycles/i       77
   NULL-MEMCPY: cycles:    226313072, cycles/i      226,    62133.395 MiB/s
    CRC32C-ref: cycles:  24418596066, cycles/i    24418,      575.859 MiB/s
     CRC32C-NI: cycles:   1188335920, cycles/i     1188,    11833.073 MiB/s
    CRC32C-PCL: cycles:    463193456, cycles/i      463,    30358.037 MiB/s
        XXHASH: cycles:    851606646, cycles/i      851,    16511.916 MiB/s
    SHA256-ref: cycles:  74476234956, cycles/i    74476,      188.808 MiB/s
     SHA256-NI: cycles:  34198637428, cycles/i    34198,      411.177 MiB/s
    BLAKE2-ref: cycles:  14761411664, cycles/i    14761,      952.597 MiB/s
   BLAKE2-SSE2: cycles:  18101896796, cycles/i    18101,      776.807 MiB/s
  BLAKE2-SSE41: cycles:  12599091062, cycles/i    12599,     1116.087 MiB/s
   BLAKE2-AVX2: cycles:   9668247506, cycles/i     9668,     1454.418 MiB/s

The new implementation is about 2.5x faster.

Note: there new version does not work on musl because of linkage
problems (relocations in .rodata), so it's still using the old
implementation.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:24 +02:00
David Sterba
8461513b9a btrfs-progs: rename prefixcmp to string_has_prefix
We already have the string_* namespace for such helpers.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:24 +02:00
David Sterba
5aa2ea7db0 btrfs-progs: add more helpers to read values from sysfs
In some places we want to read a single u64 value from a sysfs path, or
from fsid directory. Add helpers that do that in one go.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:24 +02:00
David Sterba
4a96a935ad btrfs-progs: move sysfs related helpers to own file
The sysfs could use more convenience helpers so move the current code to
own file before adding more helpers.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:24 +02:00
David Sterba
7f9bcab309 btrfs-progs: add sorting API
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:24 +02:00
David Sterba
a5d95a73f9 btrfs-progs: add an extensible pointer array struct
API for extensible array of pointers for covenience. A simple wrapper
around a (void *) array with length.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:24 +02:00
David Sterba
ede538f98d btrfs-progs: print null value for empty uuid in json
This is a potentially breaking change to json output. An all zeros uuid
was printed as "-" but we can utilize native json type null for that.
Note the va_copy must be used as va_arg advances the pointer.

{
	"nulluuid": null
}

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba
70ae158ec8 btrfs-progs: rename time-long format name to date-time
Make the timestamp format more descriptive what is actually printed. We
may need separate date or time in the future.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba
25fc470252 btrfs-progs: print numeric values unquoted in json
The json spec allows numeric values and it's recommended to use them
instead of the stringified numbers. This is a potentially breaking change
if some tools relied on the string value.

As most formats we now have are '%llu' and it's convenient to just pass
it to vprintf, don't add a special type for ints. Any new int type must
be added to the list.

{
	"number": 1234
}

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba
155eb56417 btrfs-progs: document json format str
The 'str' type was added in ecbb6a7fcd ("btrfs-progs: add json
formatter for escaped string") but not documented. It should be used
e.g. for paths or strings from unknown origin.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba
be5cb7dfe9 btrfs-progs: add bool type to json
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba
b890183072 btrfs-progs: support unquoted values in json
For null or boolean values the "..." quoting must not be done, add
support for that. This is detected internally for each printed value.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
Christoph Heiss
27a5722fe5 btrfs-progs: common: document time-long output format
Seems this was forgotten; rectify that.

Signed-off-by: Christoph Heiss <christoph@c8h4.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba
c135550c28 btrfs-progs: help: indent option description newlines
A newline character in option description text will break line and then
indent the text properly, can be used for lists or paragraphs.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-07-27 14:45:29 +02:00
David Sterba
8922ab5d6a btrfs-progs: add simple error injection framework
To be able to test errors at specific locations, add a simple way to
check for a condition in code and controlled from user space environment
variable INJECT. For now a single value is accepted.

Use like:

	if (inject_error(0x1234)) {
		do_something();
		return -ERROR;
	}

This is enabled in debugging build by default (make D=1) and can be
enabled on demand too (make EXTRA_CFLAGS=-DINJECT).

Signed-off-by: David Sterba <dsterba@suse.com>
2023-07-27 14:45:29 +02:00
David Sterba
b3b041302e btrfs-progs: add btrfs-find-root to btrfs.box
There's a report that btrfs-find-root does not work as built-in tool in
btrfs.box, while it's advertised in the help:

  $ ./btrfs.box help --box
  Standalone tools built-in in the busybox style:
  - mkfs.btrfs
  - btrfs-image
  - btrfs-convert
  - btrfstune
  - btrfs-find-root

Add the support as it might be useful tool sometimes. In the future the
command should be moved to e.g. inspect-internal or rescue.

Issue: #648
Signed-off-by: David Sterba <dsterba@suse.com>
2023-07-26 15:00:48 +02:00
Anand Jain
77f366c9da btrfs-progs: add noscan parameter to check_where_mounted
The function check_where_mounted() scans the system for all other btrfs
devices, which is necessary for its operation.  However, in certain
cases, devices remaining in the scanned state is undesirable.  Introduce
the 'noscan' argument to make devices unscanned before return.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-07-26 15:00:48 +02:00
Anand Jain
e408cfb49b btrfs-progs: factor out btrfs_scan_argv_devices
To prepare for handling command line given devices factor out
btrfs_scan_argv_devices().

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-07-26 15:00:48 +02:00
Anand Jain
626d53fc75 btrfs-progs: switch is_btrfs to bool in check_mounted_where
The variable 'is_btrfs' is declared as an integer but should be a boolean
instead.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-07-26 15:00:47 +02:00
David Sterba
e4bb48dcf3 btrfs-progs: sync send.h from kernel
The send.h for libbtrfs has been separated some time ago so we're now
free to keep up with kernel, 6.4-rc1.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-05-26 18:02:32 +02:00
Qu Wenruo
3ce08b2ff6 btrfs-progs: constify the buffer pointer for write functions
The following functions accept a buffer for write, which can be marked
as const:

- btrfs_pwrite()
- write_data_to_disk()

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-05-26 18:02:31 +02:00
Qu Wenruo
2b51d006cd btrfs-progs: split btrfs_direct_pio() functions into read and write
It's not a common practice to use the same io function for both read and
write (we have pread() and pwrite(), not pio()).

Furthermore the original function has the following problems:

- Not returning proper error number
  If we had ioctl/stat errors we just return 0 with errno set.
  Thus caller would treat it as a short read, not a proper error.

- Unnecessary @ret_rw
  This is not that obvious if we have different handling for read and
  write, but if we split them it's super obvious we can reuse @ret.

- No proper copy back for short read

- Unable to constify the @buf pointer for write operation

All those problems would be addressed in this patch.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-05-26 18:02:31 +02:00
Qu Wenruo
b3327119ec btrfs-progs: fix -Wmissing-prototypes warnings
The fixes involve the following changes:

- Unexport functions which are not utilized out of the file
  * print_path_column()
  * parse_reflink_range()
  * btrfs_list_setup_print_column()
  * device_get_partition_size_sysfs()
  * max_zone_append_size()

- Include related headers before implementing the function
  * change-uuid.c
  * convert-bgt.c
  * seed.h

- Add missing headers caused by the above header changes
  * include <uuid/uuid.h> for tune/tune.h.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-05-26 18:02:31 +02:00