btrfs-progs/check
Qu Wenruo e79f18a4a7 btrfs-progs: introduce a basic metadata free space reservation check
Unlike kernel, in btrfs-progs btrfs_start_transaction() never checks if
there is enough metadata space.

This can lead to very dangerous situation where there is no metadata
space left at all, deadlocking future tree operations.

This patch introduces a very basic version of metadata/system free space
check by:

- Check if there is enough metadata/system space left
  If there is enough, go as usual.

- If there is not enough space left, try allocating a new chunk

- Recheck if the new space can meet our demand
  If not, return ERR_PTR(-ENOSPC).
  Otherwise, allocate a new trans handle to the caller.

This is possible thanks to the simplified transaction model in
btrfs-progs:

- We don't allow joining a transaction
  This means we don't need to handle complex cases like data ordered
  extents, which need to reserve space first, then join the current
  transaction and use the reserved blocks.

- We don't allow multiple transaction handles for one transaction
  Since btrfs-progs is single threaded, we always start a transaction
  and then commit it.

However there is a feature that must be an exception for the new
metadata/system free space check:

- btrfs check --init-extent-tree
  As all the meta/system free space check is based on the space info,
  which is loaded from block group items.
  Thus when rebuilding extent tree, we can no longer have an accurate
  view, thus we have to disable the feature for the whole execution if
  we're rebuilding the extent tree.

For now, there is no regression exposed during the self tests, but I
really hope this can be an extra safety net to prevent causing ENOSPC
deadlock in btrfs-progs.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-17 19:33:59 +02:00
..
common.h btrfs-progs: sync extent-io-tree.[ch] and misc.h from the kernel 2023-05-26 18:02:29 +02:00
main.c btrfs-progs: introduce a basic metadata free space reservation check 2023-10-17 19:33:59 +02:00
mode-common.c btrfs-progs: clean up includes, using include-what-you-use 2023-10-03 01:11:57 +02:00
mode-common.h btrfs-progs: clean up includes, using include-what-you-use 2023-10-03 01:11:57 +02:00
mode-lowmem.c btrfs-progs: fix all variable shadowing 2023-10-10 19:16:29 +02:00
mode-lowmem.h btrfs-progs: check: update include lists 2022-10-11 09:06:12 +02:00
mode-original.h btrfs-progs: check: add check for metadata level mismatch 2023-09-12 23:18:34 +02:00
qgroup-verify.c btrfs-progs: fix all variable shadowing 2023-10-10 19:16:29 +02:00
qgroup-verify.h btrfs-progs: check: update include lists 2022-10-11 09:06:12 +02:00
repair.c btrfs-progs: fix all variable shadowing 2023-10-10 19:16:29 +02:00
repair.h btrfs-progs: clean up includes, using include-what-you-use 2023-10-03 01:11:57 +02:00