Commit graph

34 commits

Author SHA1 Message Date
Qu Wenruo da1d497a16 btrfs-progs: rescue: properly close the fs for clear-ino-cache
[BUG]
In cmd_rescue_clear_ino_cache(), we opened the fs, but without
closing it using close_ctree().

[CAUSE]

This was introduced in 42404a4e44 ("btrfs-progs: move inode cache
removal to rescue group"), the original code inside btrfs check
had a "goto out_close;" to properly close the fs.

[FIX]
Manually call close_ctree() on the fs_info->tree_root.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-08 08:30:37 +01:00
David Sterba cdb416b72e btrfs-progs: rescue: replace kfree by free in clear_uuid_tree
The kmalloc/kfree helpers should not be used in code not related to
kernel.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
Qu Wenruo 87b3197d81 btrfs-progs: move space cache removal to rescue group
The option "--clear-space-cache" is not really that suitable for "btrfs
check" group, as there are some concerns:

- Allowing transid mismatch
- No leaf item checks

  Thoe behaviour are inherited from the default open ctree flags for
  "btrfs check", which can be unsafe if the end user just wants to clear
  the cache.

- Unclear if the cache clearing would happen along with repair

  Thankfully the clearing of space cache is done without any repair

Thus there is a proposal to move space cache removal to rescue group,
and this patch would do that exactly.

However this would lead to some behavior changes:

- Transid mismatch would be treated as error
- Leaf items size/offset would still be checked

  If we hit any above error, we should just abort without doing any
  write.

These change would increase the safety of the space cache removal, thus
I believe it's worthy to introduce such behavior change.

Since we're here, also add a small explanation on why we need this
dedicated tool to clear space cache (especially for v1 cache).

Issue: #698
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
Qu Wenruo 42404a4e44 btrfs-progs: move inode cache removal to rescue group
The option "--clear-ino-cache" is not really that suitable for "btrfs
check" group.

Let's move it to "btrfs rescue" group to fix those small hiccups, just
like the existing "btrfs rescue fix-device-size" command.

For now, "btrfs check --clear-ino-cache" would still work, with one
extra warning referring to "btrfs rescue clear-ino-cache".
This is mostly to reduce the surprise, and keep script users (I doubt if
there is any though) happy for now.

In the next or two releases, we would fully remove the support in "btrfs
check" group.

Another small change is, in the documents, we refer to the feature as
"inode map", which doesn't match with the mount option documents.
Since we're here, unify them to "inode cache" feature.

Issue: #669
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-13 18:13:12 +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 f94ad0c516 btrfs-progs: pass btrfs_trans_handle through btrfs_clear_buffer_dirty
This is the calling convention in the kernel because we track dirty
blocks per transaction instead of globally in the fs_info.  Simply
mirror what we do in the kernel to make it easier to sync ctree.c
locally.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-03 01:11:55 +02:00
Anand Jain d46a0ef6a0 btrfs-progs: rename struct open_ctree_flags to open_ctree_args
The struct open_ctree_flags currently holds arguments for
open_ctree_fs_info(), it can be confusing when mixed with a local variable
named open_ctree_flags as below in the function cmd_inspect_dump_tree().

  cmd_inspect_dump_tree()
  ::
  struct open_ctree_flags ocf = { 0 };
  ::
  unsigned open_ctree_flags;

So rename struct open_ctree_flags to struct open_ctree_args.

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
Josef Bacik 6da52f41c7 btrfs-progs: pass root_id for btrfs_free_tree_block
In the kernel we pass in the root_id for btrfs_free_tree_block instead
of the root itself.  Update the btrfs-progs version of the helper to
match what we do in the kernel.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-05-26 18:02:30 +02:00
Josef Bacik 656938b665 btrfs-progs: rename clear_extent_buffer_dirty to btrfs_clear_buffer_dirty
This is a mirror of the change I've done in the kernel, but in progs
it's even more simply because clean_tree_block was just a wrapper around
clear_extent_buffer_dirty.  Change this to btrfs_clear_buffer_dirty, and
then update all the callers to use this helper instead of
clean_tree_block and clear_extent_buffer_dirty.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-05-26 18:02:29 +02:00
Josef Bacik bf0f3db765 btrfs-progs: introduce UASSERT() for purely userspace code
While syncing messages.[ch] I had to back out the ASSERT() code in
kerncompat.h, which means we now rely on the kernel code for ASSERT().
In order to maintain some semblance of separation introduce UASSERT()
and use that in all the purely userspace code.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-05-26 18:02:28 +02:00
David Sterba dc6a4265bf btrfs-progs: rescue: convert help text to option formatter
Signed-off-by: David Sterba <dsterba@suse.com>
2023-02-28 20:11:24 +01:00
David Sterba b2729396d2 btrfs-progs: cmds: use bool for status variables
Using bool for on/off variables is cleaner, convert all in cmds/ .

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:12 +02:00
David Sterba b0fcdb9224 btrfs-progs: cmds: remove unnecessary casts for u64
The (unsigned long long) type casts can be dropped, printf understands
%llu and u64 and does not warn. In cases where the type is not u64 keep
the cast.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:11 +02:00
David Sterba 9ef6b38726 btrfs-progs: rescue: use pr_verbose for messages
Replace printf by the level-aware helper. No change for commands that
don't have the global -q/-v options, otherwise the output can be
quieted.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:11 +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 3df6617b9b btrfs-progs: cmds: 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
Qu Wenruo 77db6bd402 btrfs-progs: rescue: introduce clear-uuid-tree
[BUG]
There is a bug report that a corrupted key type (expected
UUID_KEY_SUBVOL, has EXTENT_ITEM) causing newer kernel to reject a
mount.

Although the root cause is not determined yet, with roll out of v5.11
kernel to various distros, such problem should be prevented by
tree-checker, no matter if it's hardware problem or not.

And older kernel with "-o uuid_rescan" mount option won't help, as
uuid_rescan will only delete items with
UUID_KEY_SUBVOL/UUID_KEY_RECEIVED_SUBVOL key types, not deleting such
corrupted key.

[FIX]
To fix such problem we have to rely on offline tool, thus there we
introduce a new rescue tool, clear-uuid-tree, to empty and then remove
uuid tree.

Kernel will re-generate the correct uuid tree at next mount.

Reported-by: S. <sb56637@gmail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-22 21:45:37 +01: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 6bb6d1215d btrfs-progs: factor open_ctree parameters to a structure
Extending open_ctree with more parameters would be difficult, we'll need
to add more so factor out the parameters to a structure for easier
extension.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-03-24 22:20:19 +01:00
Daniel Xu c11874ae81 btrfs-progs: rescue: add create-control-device subcommand
Add a new subcommand 'btrfs rescue create-control-device' that creates
/dev/btrfs-control. This is helpful on systems that may not have `mknod`
installed and the device node is missing for some reason.

Issue: #223
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
[ update docs ]
Signed-off-by: David Sterba <dsterba@suse.com>
2021-02-19 16:19:37 +01: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 68e26c1917 btrfs-progs: fixup spacing in help texts
Unify spacing between options and texts.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-29 17:45:40 +02:00
David Sterba 27a65e016e btrfs-progs: deprecate subcommand specific verbose/quiet options
Many subcommands have their own verbosity options that are being
superseded by the global options. Update the help text to reflect that
where applicable.

Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-29 17:45:40 +02:00
Anand Jain 7c3b845f9d btrfs-progs: super-recover: add global verbose option
Propagate global --verbose option down to the btrfs rescue super-recover
subcommand.

Both global and local verbose options are now supported:

  btrfs -v rescue super-recover
  btrfs rescue super-recover -v

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-12 19:31:37 +02:00
Anand Jain 968721ad77 btrfs-progs: chunk-recover: add global verbose option
Propagate global --verbose option down to the btrfs rescue chunk-recover
subcommand.

Both global and local verbose options are now supported and aliases:

 btrfs -v rescue chunk-recover
 btrfs rescue chunk-recover -v

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2020-06-12 19:31:33 +02:00
Qu Wenruo 9b01db7785 btrfs-progs: rescue/zero-log: Manually write all supers to handle extent tree error more gracefully
[BUG]
Even "btrfs rescue zero-log" only reset btrfs_super_block::log_root and
btrfs_super_block::log_root_level, we still use trasction to write all
super blocks for all devices.

This means we can't handle things like corrupted extent tree:

  checksum verify failed on 2172747776 found 000000B6 wanted 00000000
  checksum verify failed on 2172747776 found 000000B6 wanted 00000000
  bad tree block 2172747776, bytenr mismatch, want=2172747776, have=0
  WARNING: could not setup extent tree, skipping it
  Clearing log on /dev/nvme/btrfs, previous log_root 0, level 0
  ERROR: Corrupted fs, no valid METADATA block group found
  ERROR: attempt to start transaction over already running one

[CAUSE]
Because we have extra check in transaction code to ensure we have valid
METADATA block groups.

In fact we don't really need transaction at all.

[FIX]
Instead of commit transaction, we can just call write_all_supers()
manually, so we can still handle multi-device fs while avoid above
error.

Also, add OPEN_CTREE_NO_BLOCK_GROUPS open ctree flag to make it more
robust.

Link: https://lore.kernel.org/linux-btrfs/CAKbQEqG35D_=8raTFH75-yCYoqH2OvpPEmpj2dxgo+PTc=cfhA@mail.gmail.com/
Reported-by: Christian Pernegger <pernegger@gmail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2019-11-22 19:07:21 +01:00
David Sterba add5079974 btrfs-progs: fix helpinfo formats, short and options separation
For options that do not have the long description, the empty string is
required to mark where the options start. Some commands were missing
that.

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 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 605b8c1295 btrfs-progs: move cmds-rescue.c to cmds/
Signed-off-by: David Sterba <dsterba@suse.com>
2019-07-03 20:49:02 +02:00
Renamed from cmds-rescue.c (Browse further)