Commit graph

250 commits

Author SHA1 Message Date
Josef Bacik d1235141ab btrfs-progs: tree-stats: initialize the key properly
I started hitting a segfault on fuzz test 006 because we couldn't find
the extent root.  This is because the global root search stuff expects
the actual key to be setup properly, not just an objectid.  Fix this by
initializing the key properly so we can find the extent root and other
trees properly.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-03-08 18:38:13 +01:00
Howard 1bce4429be btrfs-progs: subvolume snapshot: added clarity how snapshot path is interprted
A snapshot could be created in an existing directory, explain the
difference in the command line help options.

Pull-request: #117
Author: Howard <hwj@BridgeportContractor.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-02-16 22:48:01 +01:00
Rosen Penev 56760e8dca btrfs-progs: fix 64-bit mips and powerpc types
The kernel uses 'unsigned long' for u64 specifically for ppc64 and
mips64.

Remove asm/types.h include as it will get included properly later.

Fixe -Wformat warnings.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-02-16 22:44:58 +01:00
David Sterba 193cd24b24 btrfs-progs: subvol delete: more fine grained check when looking for default subvol
The search for default subvolume could fail for two reasons, the lack of
CAP_SYS_ADMIN for TREE_SEARCH ioctl is one but the default subvolume
could be unset as well, thus no restrictions for deletion.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-02-01 18:41:56 +01:00
Adam Borowski 0e66228959 btrfs-progs: subvol delete: hide a warning on an unprivileged delete
Checking the default subvolume uses TREE_SEARCH which is a CAP_SYS_ADMIN
only operation, and thus will fail when unprivileged, even if we have
permissions to actually delete the subvolume.

This produces a warning even if all is ok.  Let's hide it if we're not
root (root but !CAP is odd enough to warn).

Fixes 87804a3f06 ("btrfs-progs: subvolume: check deleting default subvolume")
Link: https://bugs.debian.org/998840
Signed-off-by: Adam Borowski <kilobyte@angband.pl>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-02-01 18:41:55 +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
David Sterba 433321ee8f btrfs-progs: send: properly handle an ERR_PTR in find_good_parent
Pointer returned from get_parent needs additional handling otherwise
we could return an error and then try to free it. Reset the pointer when
the error occurs so the cleanup is always done on a valid pointer.

Issue: #423
Signed-off-by: David Sterba <dsterba@suse.com>
2022-01-11 17:36:15 +01:00
Goffredo Baroncelli 817483211a btrfs-progs: prop: allow autodetect_object_types() to handle link
The function autodetect_object_types() tries to detect the type of
btrfs object passed. If it is an "inode" type (e.g. file) this function
returns the type as "inode". If it is a block device, it return it as
"block device".
However it doesn't handle the case where the object passed is a link
to a block device (which could be a valid btrfs device). For example
LVM/DM creates link to block devices. In this case it should return
the type as "block device".

This patch replace the lstat() call with a stat().

Reported-by: Boris Burkov <boris@bur.io>
Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-01-10 18:40:52 +01:00
Dāvis Mosāns 07320bd20f btrfs-progs: receive: properly check ERR return value in process_snapshot
When some error happens when trying to search for parent subvolume
then parent_subvol will contain errno so don't try to free that

Crash backtrace would look like:
0  process_snapshot at cmds/receive.c:358
    358		free(parent_subvol->path);
1  0x00005646898aaa67 in read_and_process_cmd at common/send-stream.c:348
2  btrfs_read_and_process_send_stream at common/send-stream.c:525
3  0x00005646898c9b8b in do_receive at cmds/receive.c:1113
4  cmd_receive at cmds/receive.c:1316
5  0x00005646898750b1 in cmd_execute at cmds/commands.h:125
6  main at btrfs.c:405

(gdb) p parent_subvol
$1 = (struct subvol_info *) 0xfffffffffffffffe

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Dāvis Mosāns <davispuh@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-01-03 15:55:35 +01:00
Josef Bacik ec0eaae673 btrfs-progs: add definitions for the block group tree
Add the on disk definitions for the block group tree.  This will be part
of the super block so we need to add the appropriate helpers to the
super block, as well as adding it to the backup roots.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-30 19:08:39 +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
Sidong Yang bc2382eff9 btrfs-progs: fi du: skip inaccessible files
Filesystem du command fails and exits when it access file that has
permission denied. But it can continue the command except the files.
This patch prints error message just like /bin/du does and it continues
if it can.

Issue: #421
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Sidong Yang <realwakka@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-30 18:40:18 +01:00
Josef Bacik 639b1fc2e7 btrfs-progs: stop accessing ->csum_root directly
With extent tree v2 we will have per-block group checksums, so add a
helper to access the csum root and rename the fs_info csum_root to
_csum_root to catch all the places that are accessing it directly.
Convert everybody to use the helper except for internal things.

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 44aecdc20c btrfs-progs: fi show: close ctree once we're done
Running with ASAN we won't pass the self tests because we leak the whole
fs_info with btrfs filesystem show.  Fix this by making sure we close
out the fs_info and clean up all of the memory and such.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-22 21:45:37 +01: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
Nikolay Borisov 1b194bb620 btrfs-progs: fi usage: fix calculation of chunk size for RAID1/DUP profiles
Current formula calculates the stripe size, however that's not what we
want in the case of RAID1/DUP profiles. In those cases since chunk are
mirrored across devices we want the full size of the chunk. Without this
patch the 'btrfs fi usage' output from an fs which is using RAID1 is:

	Data,RAID1: Size:2.00GiB, Used:1.00GiB (50.03%)
	   /dev/vdc	   1.00GiB
	   /dev/vdf	   1.00GiB

	Metadata,RAID1: Size:256.00MiB, Used:1.34MiB (0.52%)
	   /dev/vdc	 128.00MiB
	   /dev/vdf	 128.00MiB

	System,RAID1: Size:8.00MiB, Used:16.00KiB (0.20%)
	   /dev/vdc	   4.00MiB
	   /dev/vdf	   4.00MiB

	Unallocated:
	   /dev/vdc	   8.87GiB
	   /dev/vdf	   8.87GiB

So a 2 gigabyte RAID1 chunk actually will take up 4 gigabytes on the
actual disks 2 each. In this case this is being miscalculated as taking
up 1GiB on each device.

This also leads to erroneously calculated unallocated space. The correct
output in this case is:

	Data,RAID1: Size:2.00GiB, Used:1.00GiB (50.03%)
	   /dev/vdc	   2.00GiB
	   /dev/vdf	   2.00GiB

	Metadata,RAID1: Size:256.00MiB, Used:1.34MiB (0.52%)
	   /dev/vdc	 256.00MiB
	   /dev/vdf	 256.00MiB

	System,RAID1: Size:8.00MiB, Used:16.00KiB (0.20%)
	   /dev/vdc	   8.00MiB
	   /dev/vdf	   8.00MiB

	Unallocated:
	   /dev/vdc	   7.74GiB
	   /dev/vdf	   7.74GiB

Fix it by only utilising the chunk formula for profiles which are not
RAID1/DUP.

Issue: #422
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-18 10:17:33 +01:00
Nikolay Borisov 6df189771d btrfs-progs: fi usage: don't reset ratio to 1 if we don't have RAID56 profile
Commit 80714610f3 ("btrfs-progs: use raid table for ncopies")
slightly broke how raid ratio are being calculated since the resulting
code would always reset ratio to be 1 in case we didn't have RAID56
profile. The correct behavior is to simply set it to 0 if we have RAID56
as the calculation is different in this case and leave it intact
otherwise.

This bug manifests by doing all size-related calculation for 'btrfs
filesystem usage' command as if all block groups are of type SINGLE. Fix
this by only resetting ratio 0 in case of RAID56.

Issue: #422
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-18 10:17:21 +01:00
Qu Wenruo c4ff87c3d1 btrfs-progs: cache csum_size and csum_type in btrfs_fs_info
Just like kernel commit 22b6331d9617 ("btrfs: store precalculated
csum_size in fs_info"), we can cache csum_size and csum_type in
btrfs_fs_info.

Furthermore, there is already a 32 bits hole in btrfs_fs_info, and we
can fit csum_type and csum_size into the hole without increase the size
of btrfs_fs_info.

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 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
David Sterba dca6b12f0a btrfs-progs: fix read-write subvol and received_uuid flags check
There's a report that a read-only subvolume with a received_uuid set
emits the warning in command 'btrfs subvolume show', which is obviously
wrong.

The reason is that there are different types of root item flags,
depending on how we read them. The check in cmd_subvol_show uses the
ioctl GET_SUBVOL_INFO and the appropriate flag is raw
BTRFS_ROOT_SUBVOL_RDONLY (0x1), while there's another SUBVOL_GETFLAGS that
maps the flags and the raw value is different (BTRFS_SUBVOL_RDONLY, 0x2).

Due to this the warning was issued. Fix that by using the right flag
constant. The test has been extended to check for all combinations of
read-write and received_uuid.

Issue: #419
Signed-off-by: David Sterba <dsterba@suse.com>
2021-11-04 20:56:42 +01:00
David Sterba 7674317139 btrfs-progs: fi usage: use one formula for chunk size calculation
The profile descriptions allow us to use a single formula to calculate
chunk size. Right now there are no profiles with parity (raid5-like) and
sub_stripes (raid10-like), which makes it easier.

- parity stripes are subtracted from the total count
- then divided by number of sub stripes

Practically speaking, 1:1 copy profiles do not have any adjustments.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:24 +02:00
David Sterba 7d10a07455 btrfs-progs: chunk-recover: use btrfs_bg_type_to_sub_stripes
We should use the raid table value instead of hard coding the value.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:24 +02:00
David Sterba b738de4446 btrfs-progs: use btrfs_bg_type_is_stripey for enumerated lists
The striped profiles covering arbitrary number of devices are often
hardcoded so use the new helper btrfs_bg_type_is_stripey for that.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:24 +02:00
David Sterba 86f7cc0ee1 btrfs-progs: fi usage: use btrfs_bg_type_to_sub_stripes in calc_chunk_size
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:24 +02:00
David Sterba 14d53ce450 btrfs-progs: chunk-recover: use btrfs_bg_type_to_nparity in calc_data_offset
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:23 +02:00
David Sterba daa08f42c9 btrfs-progs: chunk-recover: use btrfs_bg_type_to_nparity in btrfs_calc_stripe_index
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:23 +02:00
David Sterba a5d4fff0df btrfs-progs: use btrfs_bg_type_to_nparity in calc_chunk_size
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:23 +02:00
David Sterba 754436f1ea btrfs-progs: use btrfs_bg_type_to_nparity get_raid56_space_info
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:23 +02:00
David Sterba 80714610f3 btrfs-progs: use raid table for ncopies
There's opencoded value of raid table ncopies in
print_filesystem_usage_overall, add a helper and use it.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-20 18:59:23 +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 649ccbfa2e btrfs-progs: subvol list: open code list_subvol_fill_paths in its caller
The helper is quite simple and there's only one caller, so merge them.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba fad9a54f88 btrfs-progs: subvol list: move sort order to the caller
All the comparators switch the result based on is_descending, but that
can be factored to the caller to simplify the comparators.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba 26f8e56194 btrfs-progs: subvol list: constify comparator parameters
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba 92c53aa50f btrfs-progs: subvol list: simplify root lookup structures
The struct root_lookup wraps only the rb-tree node which is pointless
indirection, so replace the type.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:04 +02:00
David Sterba 7e0a8f3401 btrfs-progs: subvol list: merge the rest of btrfs-list.c
The remaining functions are too entangled to be moved separately without
too much churn making them exported and not, so move all the code at
once. No refactoring or coding style fixups.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba 2c63c90517 btrfs-progs: subvol list: copy btrfs_list_alloc_filter_set from btrfs-list.c
There's only one caller of btrfs_list_alloc_filter_set so move it there.
Also move the definitions of BTRFS_LIST_* to the header so they can be
used by both btrfs-list and subvolume.c.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba acefefce61 btrfs-progs: subvol list: copy btrfs_list_alloc_comparer_set from btrfs-list.c
There's only one caller of btrfs_list_alloc_comparer_set so move it
there. Also move the definitions of BTRFS_LIST_* to the header so they
can be used by both btrfs-list and subvolume.c.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba fb0bfb967f btrfs-progs: subvol find-new: copy code from btrfs-list.c
The actual implementation of find-new functionality is outside of
subvolume.c, copy it where it's supposed to be. No reformatting or style
changes.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +02:00
David Sterba 03650851c5 btrfs-progs: split subvolume list to its own file
The main functionality of subvolume listing is now in btrfs-list.c but
there are no other commands using the API so this will be merged. It's a
lot of code so split it to another file.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-08 20:47:03 +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 cb9bd8e326 btrfs-progs: subvol show: print a warning with rw and receive_uuid
Add a slightly more convenient way to identify the subvolumes with bad
combination of flags and received uuid.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-07 18:40:34 +02:00
David Sterba 3b90ebc2d7 btrfs-progs: prop set: ro->rw and received_uuid
Implement safety check when a read-only subvolume is getting switched
to read-write and there's received_uuid set.

This prevents accidental breakage of incremental send use case but
allows user to do the rw change anyway but resets the received_uuid in
that case.

As this is implemented entirely in userspace, it's racy and using the
raw ioctl won't prevent it nor reset the received_uuid. A change in the
ioctl implementation might do that in the future.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-07 18:40:26 +02:00
David Sterba 332c193c47 btrfs-progs: prop set: add force parameter
Add option support to force the value change. This allows to do safety
checks by default and warn user that something might break. Using the
force will override that and changing the property should do change
itself and additionally any other changes that could break some
use cases.

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-07 18:40:24 +02:00
David Sterba ad4d5b163b btrfs-progs: subvol show: print send and receive generation and timestamp
There are some send/receive related data not printed in subvol show,
while they're exported by the ioctls. Print them for convenience:

  $ btrfs subvol show test
  test
	  Name: 		test
	  UUID: 		dc16dd1b-825f-3245-94a8-557672d6cf85
	  Parent UUID: 		-
	  Received UUID: 	-
	  Creation time: 	2021-05-17 16:17:14 +0200
	  Subvolume ID: 	19112
	  Generation: 		7730702
	  Gen at creation:	7730701
	  Parent ID: 		5
	  Top level ID: 	5
	  Flags: 		-
	  Send transid: 	0
	  Send time: 		2021-05-17 16:17:14 +0200
	  Receive transid: 	0
	  Receive time: 	-
	  Snapshot(s):
				  test-snap

Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-07 18:39:38 +02:00
Anand Jain edd57bf494 btrfs-progs: fix comments in cmd_filesystem_show
I had to go back to find what BTRFS_ARG_REG is, add a comment for that.

And, search_umounted_fs_uuids() is also to find the seed device, so bring
the related comment above it.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-06 16:50:46 +02:00
David Sterba 2d50b98189 btrfs-progs: replace start: add option -K/--nodiscard
The commands initializing a new device (mkfs, device add) do discard by
default, while this is missing from replace start. For parity add the
options with same name and semantics.

Issue: #390
Signed-off-by: David Sterba <dsterba@suse.com>
2021-10-06 16:49:35 +02:00