Commit graph

532 commits

Author SHA1 Message Date
David Sterba 5cdaf19529 btrfs-progs: inspect list-chunks: better sorting, updated output
Enhance the sorting capabilities of 'inspect list-chunks' to allow
multiple keys. Drop the gaps, this works only for pstart and it's hard
to make it work with arbitrary sort keys.

Usage is printed by default, assuming this is an interesting info and
even if it slows down the output (due to extra lookups) it's more
convenient to print it rather than not.

The options related to usage and empty were removed.

Output changes:

- rename Number to PNumber, meaning physical number on the device
- print Devid, device number, can be also sort key

Examples:

btrfs inspect list-chunks /mnt
btrfs inspect list-chunks --sort length,usage
btrfs inspect list-chunks --sort lstart

Depending on the sort key order, the output can be wild, for that the
PNumber and LNumber give some hint where the chunks lie in their space.

Example output:

$ sudo ./btrfs inspect list-chunks --sort length,usage /
Devid PNumber      Type/profile    PStart    Length      PEnd LNumber    LStart Usage%
----- ------- ----------------- --------- --------- --------- ------- --------- ------
    1       7       Data/single   1.52GiB  16.00MiB   1.54GiB      69 191.68GiB  86.04
    1       3     System/DUP    117.00MiB  32.00MiB 149.00MiB      40 140.17GiB   0.05
    1       2     System/DUP     85.00MiB  32.00MiB 117.00MiB      39 140.17GiB   0.05
    1      15       Data/single   8.04GiB  64.00MiB   8.10GiB      61 188.60GiB  94.46
    1       1       Data/single   1.00MiB  84.00MiB  85.00MiB      68 191.60GiB  74.24
    1       5   Metadata/DUP    341.00MiB 192.00MiB 533.00MiB      60 188.41GiB  82.58
    1       4   Metadata/DUP    149.00MiB 192.00MiB 341.00MiB      59 188.41GiB  82.58
    1      20   Metadata/DUP      9.29GiB 256.00MiB   9.54GiB      38 139.92GiB  57.76
    1      19   Metadata/DUP      9.04GiB 256.00MiB   9.29GiB      37 139.92GiB  57.76
    1      22   Metadata/DUP      9.79GiB 256.00MiB  10.04GiB      25 113.15GiB  57.93
    1      21   Metadata/DUP      9.54GiB 256.00MiB   9.79GiB      24 113.15GiB  57.93
    1      46   Metadata/DUP     29.29GiB 256.00MiB  29.54GiB      43 142.71GiB  62.38

Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-20 10:15:52 +09:30
David Sterba 4c4e6f1053 btrfs-progs: fi df: extended information about profile types
There's more information available in sysfs
(/sys/fs/btrfs/FSID/allocation) that we can print in 'fi df'. This is
still meant for debugging or deeper analysis of the filesystem, the
values need to be correctly interpreted with respect to the profiles,
persistence and other conditonal features.

The extended output is not printed by default and for now is behind the
verbosity options:

$ btrfs -vv fi df /mnt
Data, single: total=47.06GiB, used=25.32GiB
System, DUP: total=32.00MiB, used=16.00KiB
Metadata, DUP: total=1.44GiB, used=961.20MiB
GlobalReserve, single: total=125.62MiB, used=0.00B
Data:
  bg_reclaim_threshold                   0%
  bytes_may_use                     8.00KiB
  bytes_pinned                        0.00B
  bytes_readonly                   64.00KiB
  bytes_reserved                      0.00B
  bytes_used                       25.32GiB
  bytes_zone_unusable                 0.00B
  chunk_size                       10.00GiB
  disk_total                       47.06GiB
  disk_used                        25.32GiB
  total_bytes                      47.06GiB
Metadata:
  bg_reclaim_threshold                   0%
  bytes_may_use                   126.62MiB
  bytes_pinned                        0.00B
  bytes_readonly                      0.00B
  bytes_reserved                      0.00B
  bytes_used                      961.20MiB
  bytes_zone_unusable                 0.00B
  chunk_size                      256.00MiB
  disk_total                        2.88GiB
  disk_used                         1.88GiB
  total_bytes                       1.44GiB
System:
  bg_reclaim_threshold                   0%
  bytes_may_use                       0.00B
  bytes_pinned                        0.00B
  bytes_readonly                      0.00B
  bytes_reserved                      0.00B
  bytes_used                       16.00KiB
  bytes_zone_unusable                 0.00B
  chunk_size                       32.00MiB
  disk_total                       64.00MiB
  disk_used                        32.00KiB
  total_bytes                      32.00MiB

Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-20 10:15:52 +09:30
David Sterba a382eed70d btrfs-progs: receive dump: fix formatting of encoded write message
The separator of key=value is only one or more space character, the
'encoded_write' also uses ',' which is inconsistent with the rest.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-20 10:15:52 +09:30
David Sterba a95e6d307b btrfs-progs: receive dump: encode clone path and xattr name/value
The xattr names are user strings but still can potentially contain
special characters (as reported). There doesn't seem to be a restriction
on the name defined.

The xattr values care length-encoded byte arrays so escaping needs be
done.

The clone source is a path and by mistake lacked the encoding.

Issue: #818
Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-20 10:15:52 +09:30
David Sterba 55f22f2106 btrfs-progs: use strncpy_null everywhere
Use the safe version of strncpy that makes sure the string is
terminated.

To be noted:

- the conversion in scrub path handling was skipped
- sizes of device paths in some ioctl related structures is
  BTRFS_DEVICE_PATH_NAME_MAX + 1

Recently gcc 13.3 started to detect problems with our use of strncpy
potentially lacking the null terminator, warnings like:

cmds/inspect.c: In function ‘cmd_inspect_logical_resolve’:
cmds/inspect.c:294:33: warning: ‘__builtin_strncpy’ specified bound 4096 equals destination size [-Wstringop-truncation]
  294 |                                 strncpy(mount_path, mounted, PATH_MAX);
      |                                 ^

Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-20 10:15:52 +09:30
David Sterba 4748d400c4 btrfs-progs: rename and move __strncpy_null to string-utils
Now that there's only __strncpy_null we can drop the underscore and move
it to string-utils as it's a generic string function rather than
something for paths.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-20 10:15:52 +09:30
David Sterba 0d37a301f0 btrfs-progs: use explicit length parameters for string copy
The macro strncpy_null uses sizeof the first argument for the length,
but there are no checks and this works only for buffers with static
length, i.e. not pointers. This is error prone.  Use the open coded
variant that makes the sizeof visible.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-06-20 10:15:52 +09:30
David Sterba 3e91948c01 btrfs-progs: use unaligned LE access almost everywhere
Use unaligned access helper for code that potentially or actually
accesses data that come from on-disk structures. This is for image or
chunk restore. This may pessimize some cases but is in general safer on
strict alignment architectures and has no effect on other architectures.

Related issue #770.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-05-30 18:57:10 +02:00
David Sterba c147aaa59b btrfs-progs: clarify unlinked and deleted terminology for subvolumes
Recent patches updated stale qgroup handling, using 'unlinked' and
'dropped' where we otherwise use 'deleted' and 'cleaned'.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-05-17 18:13:18 +02:00
Qu Wenruo 7f3ab46400 btrfs-progs: qgroup: add more special statuses for qgroups
Currently `btrfs qgroup show` command shows any 0 level qgroup without a
root backref as `<stale>`, which is not correct.

There are several more cases:

- Under deletion
  The subvolume is not yet full dropped, but unlinked.
  In that case we would not have a root backref item, but the qgroup is
  not stale.

- Squota space holder
  This is for squota mode, that a fully dropped subvolume still have
  extents accounting on the already-gone subvolume.
  In this case it's not stale either, and future accounting relies on
  it.

This patch would add above special cases, and add an extra `SPECIAL
PATHS` section to explain all the cases, including `<stale>`.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-05-17 18:01:49 +02:00
Qu Wenruo 82f7d6c1d7 btrfs-progs: qgroup: handle stale qgroup deletion more accurately
The current stale qgroup deletion doesn't handle the following cases at
all:

- It doesn't detect stale qgroups correctly
  The current check is using the root backref, which means unlinked but
  not yet fully dropped subvolumes would mark its corresponding qgroups
  stale.

  This is incorrect. The real stale check should be based on the root
  item, not root backref.

- Squota non-empty but stale qgroups
  Such qgroups can not and should not be deleted, as future accounting
  still require them.

- Full accounting mode, stale qgroups but not empty
  Since qgroup numbers are inconsistent already, it's common to have
  such stale qgroups with non-zero numbers.

  Now it's dependent on the kernel to determine whether such qgroup can
  be deleted.

Address the above problems:

- Do root_item based detection
  So that btrfs_qgroup::stale would properly indicate if there is a
  subvolume root item for the qgroup.

- Do not attempt to delete squota stale but non-empty qgroups

- Attempt to delete stale but non-empty qgroups for full accounting mode
  And deletion failure would not count as an error.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-05-17 17:55:04 +02:00
Qu Wenruo 468bbb6b25 btrfs-progs: qgroup: add qgroup_lookup::flags member
This allows the users to identify if the running qgroup mode and whether
the numbers are already inconsistent.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-05-17 17:50:26 +02:00
Qu Wenruo 1297013cea btrfs-progs: qgroup clear-stale: sync the fs before doing qgroup search
Since qgroup numbers are only updated at transaction commit time, it's
better to do a sync before reading the quota tree, to reduce the chance
of uncommitted qgroup changes.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-05-17 17:49:35 +02:00
Qu Wenruo 99447fb708 Revert "btrfs-progs: subvol delete: add options to delete the qgroup"
This reverts commit 9da773aa46.

There are several problems related to the --delete-qgroup option:

- Currently kernel doesn't allow to delete non-empty qgroups

- A qgroup can only be empty after fully dropped and a transaction is
  committed
  The tool doesn't take either factor into consideration

- Things like drop_subtree_threshold or other operations can mark qgroup
  inconsistent and skip accounting
  This can mean the target qgroup will never be empty until next rescan

On the other hand, even we do it the proper way, it would hugely delay
the command (wait until the subvolume to be cleaned).

Furthermore, even if the waiting is handled properly,
drop_subtree_threshold can still prevent us deleting the qgroup (qgroup
numbers are inconsistent, and accounting is skipped completely).

So the qgroup cleanup needs kernel to make it work properly.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-30 21:49:15 +02:00
David Sterba 7f396f5ced btrfs-progs: reorder key initializations
Use the objectid, type, offset natural order as it's more readable and
we're used to read keys like that.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-30 21:49:15 +02:00
David Sterba 884a609a77 btrfs-progs: add basename wrappers for unified semantics
What basename(3) does with the argument depends on _GNU_SOURCE and
inclusion of libgen.h. This is problematic on Musl (1.2.5) as reported.

We want the GNU semantics that does not modify the argument. Common way
to make it portable is to add own helper. This is now implemented in
path_basename() that does not use the libc provided basename but preserves
the semantics. The path_dirname() is just for parity, otherwise same as
dirname().

Sources:
- https://bugs.gentoo.org/926288
- https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

Issue: #778
Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-30 19:34:52 +02:00
oluceps 6088039cf8 btrfs-progs: property set: fix typo in help message
Correct a typo by replacing "then" with "than".

Signed-off-by: oluceps <linux@oluceps.uk>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-30 19:34:52 +02:00
David Sterba 29b40262ba btrfs-progs: unify error handling in cmd_inspect_list_chunks()
Reported by 'gcc -fanalyzer':
cmds/inspect.c:1193:1: warning: leak of ‘ctx.stats’ [CWE-401] [-Wanalyzer-malloc-leak]

There are mixed returns and gotos for error handling and the returns
miss freeing of the ctx.stats. Unify all paths to the single label that
frees the buffers and rename it.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-18 19:16:15 +02:00
David Sterba 72086bdacc btrfs-progs: scrub start: use local buffer for data file path in scrub_start()
Reported by 'gcc -fanalyzer':
cmds/scrub.c:1150:25: warning: use of possibly-NULL ‘path’ where non-null expected [CWE-690] [-Wanalyzer-possible-null-argument]

Initialization of the datafile path is done from a static string but the
strdup() call is not handled. Store the path directly to the buffer,
it's later modified by mkdir_p().

Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-18 19:16:15 +02:00
David Sterba 8767dda133 btrfs-progs: handle strndup() failure in ino_resolve()
Reported by 'gcc -fanalyzer':
cmds/subvolume.c:1078:39: warning: use of possibly-NULL ‘name’ where non-null expected [CWE-690] [-Wanalyzer-possible-null-argument]

The failure name duplication is not handled and can potentially lead to
a NULL dereference later. Handle the error properly and return template
error message.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-18 19:16:15 +02:00
David Sterba 2bdb838d54 btrfs-progs: fix double file descriptor close in cmd_replace_start()
Reported by 'gcc -fanalyzer':
cmds/replace.c:357:17: warning: double ‘close’ of file descriptor ‘fdmnt’ [CWE-1341] [-Wanalyzer-fd-double-close]

The first close is done right before going to the label
'leave_with_error' but the variable is not reset to -1 so in the exit
block close() is called again.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-18 19:16:15 +02:00
David Sterba 2c99d8078f btrfs-progs: prop set: enhance error message when changing subvolume from ro->rw
The error message for ro->rw is not clear enough, add more context for
the received_uuid and send and point to the manual page that explains
more.

Asked at:
https://www.reddit.com/r/btrfs/comments/1bkqor2/received_uuid_error_after_btrfs_send/

[ci skip]

Issue: #763
Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-25 23:13:46 +01:00
David Sterba f3ece218b6 btrfs-progs: unify tree search header access
Use a local copy of the search header for proper aligned access instead
of the unaligned helpers, move the definitions to the closest scope.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-16 18:15:51 +01:00
David Sterba 005aeb8cb0 btrfs-progs: use TREE_SEARCH ioctl wrappers for all non-library code
Use tree search ioctl wrappers for code that is considered internal, ie.
leaving out libbtrfs (legacy), libbtrfsutil (needs own API for that).

Conversion is mostly direct of what the API provides.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-16 18:15:51 +01:00
David Sterba 4ab1159db0 btrfs-progs: chunk-recover: proper error handling for cache insertions
Replace BUG_ONs with proper error handling.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-15 01:53:32 +01:00
David Sterba 2edd439617 btrfs-progs: use unsigned types for bit shifts
Bit shifts should be done on unsigned type as a matter of good practice
to avoid any problems with bit overflowing to the sign bit.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-12 22:05:09 +01:00
Qu Wenruo 5f87b467a9 btrfs-progs: subvolume: output the prompt line only when the ioctl succeeded
[BUG]
With the latest kernel patch to reject invalid qgroupids in
btrfs_qgroup_inherit structure, "btrfs subvolume create" or "btrfs
subvolume snapshot" can lead to the following output:

 # mkfs.btrfs -O quota -f $dev
 # mount $dev $mnt
 # btrfs subvolume create -i 2/0 $mnt/subv1
 Create subvolume '/mnt/btrfs/subv1'
 ERROR: cannot create subvolume: No such file or directory

The "btrfs subvolume" command output the first line, seemingly to
indicate a successful subvolume creation, then followed by an error
message.

This can be a little confusing on whether if the subvolume is created or
not.

[FIX]
Fix the output by only outputting the regular line if the ioctl
succeeded.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-04 22:55:07 +01:00
Qu Wenruo 4da5f22b23 btrfs-progs: qgroups: remove support for num_ref_copies/num_excl_copies
Remove btrfs_qgroup_inherit_add_copy() and the command line interface.

This was designed to add a pair of source/destination qgroups into
btrfs_qgroup_inherit structure, so that rfer/excl numbers would be
copied from the source qgroup into the destination one.

This behavior has been intentionally hidden since 2016, as such copy will
cause qgroup inconsistent immediately and a rescan would reset whatever
numbers copied anyway.

Now we're going to reject the copy behavior from kernel, there is no
need to keep those hidden (and already disabled for "subvolume create")
case.

Remove btrfs_qgroup_inherit_add_copy() call, and cleanup the
undocumented options.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-03-04 22:55:07 +01:00
David Sterba 3c8200302b btrfs-progs: rename btrfs_open_fd2() to btrfs_open_path()
Use a more descriptive name, the interface is generic so it should use
the generic term for file/directory.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:59:29 +01:00
David Sterba 7b9a3669e1 btrfs-progs: drop _fd from btrfs_open_file_or_dir_fd()
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:58:27 +01:00
David Sterba 1669446fd9 btrfs-progs: drop _fd from btrfs_open_mnt_fd()
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:57:50 +01:00
David Sterba 590bcf20bf btrfs-progs: drop _fd from btrfs_open_dir_fd()
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:56:24 +01:00
David Sterba 221114c36f btrfs-progs: drop verbosity parameter from btrfs_open_fd2()
All calls now pass true, drop the parameter.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:47:46 +01:00
David Sterba 9b5f104558 btrfs-progs: always print error messages from btrfs_open_fd2()
There are some cases that disable verbosity (of errors) and then print
own message. Enable the verbose error messages printed by
btrfs_open_fd2() as they are specific.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:47:39 +01:00
David Sterba 3fbcce4544 btrfs-progs: drop verbosity parameter from btrfs_open_mnt_fd()
All calls now pass true, drop the parameter.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:47:35 +01:00
David Sterba 9f9b5028d5 btrfs-progs: scrub start: use global verbosity options
The code in scrub predates the global verbosity options and sets its own
quiet status. This is still used only for error messages that should be
printed even with -q. Drop that or replace with bconf.verbose status
check.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:30:44 +01:00
David Sterba 58b1c4de03 btrfs-progs: use btrfs_open_file_or_dir_fd for error verbosity
There are many places that pass false as verbosity argument and then
print an error message, or don't print any message in error cases.
Use btrfs_open_file_or_dir_fd() that will be verbose in case of an error
with the same semantics.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:19:14 +01:00
David Sterba 75522225f7 btrfs-progs: switch open helper functions to return negative errno
It's commonly used elsewhere in the code to return the -errno values if
possible, do that for the open helpers too.

Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:19:14 +01:00
Goffredo Baroncelli 89867394e5 btrfs-progs: remove open_file_or_dir3 from du_add_file
For historical reasons the helpers [btrfs_]open_dir... return also
the 'DIR *dirstream' value when a directory is opened.

However this is never used. So avoid calling diropen() and return
only the fd.

Replace the last reference to btrfs_open_file_or_dir3() with
btrfs_open_fd2().

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:19:14 +01:00
Goffredo Baroncelli 6a08424a50 btrfs-progs: replace open_file_or_dir with btrfs_open_fd2
For historical reasons the helpers [btrfs_]open_dir... return also
the 'DIR *dirstream' value when a directory is opened.

However this is never used. So avoid calling diropen() and return
only the fd.

Replace open_file_or_dir() with btrfs_open_fd2() removing any reference
to the unused/useless dirstream variables.  btrfs_open_fd2() is required
to avoid spurious error messages.

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:19:14 +01:00
Goffredo Baroncelli cafef41840 btrfs-progs: replace btrfs_open_file_or_dir with btrfs_open_file_or_dir_fd
For historical reasons the helpers [btrfs_]open_dir... return also
the 'DIR *dirstream' value when a directory is opened.

However this is never used. So avoid calling diropen() and return
only the fd.

Replace btrfs_open_file_or_dir() with btrfs_open_file_or_dir_fd()
removing any references to the unused/useless dirstream variables.

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:19:14 +01:00
Goffredo Baroncelli 50770da81e btrfs-progs: replace open_file_or_dir3 with btrfs_open_fd2
For historical reasons the helpers [btrfs_]open_dir... return also
the 'DIR *dirstream' value when a directory is opened.

However this is never used. So avoid calling diropen() and return
only the fd.

Replace open_file_or_dir3() with btrfs_open_fd2() removing any reference
to the unused/useless dirstream variables.  btrfs_open_fd2() is needed
because sometime the callers need to set the RDONLY/RDWRITE mode, and to
avoid spurious messages.

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 12:19:09 +01:00
Goffredo Baroncelli 900f21b4f1 btrfs-progs: replace open_path_or_dev_mnt with btrfs_open_mnt_fd
For historical reasons the helpers [btrfs_]open_dir... return also
the 'DIR *dirstream' value when a directory is opened.

However this is never used. So avoid calling diropen() and return
only the fd.

Replace open_path_or_dev_mnt() with btrfs_open_mnt_fd() removing
any reference to the unused/useless dirstream variables.

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 11:40:08 +01:00
Goffredo Baroncelli 43dbe63dba btrfs-progs: replace btrfs_open_dir with btrfs_open_dir_fd
For historical reasons the helpers [btrfs_]open_dir... return also
the 'DIR *dirstream' value when a directory is opened.

However this is never used. So avoid calling diropen() and return
only the fd.

Replace the last btrfs_open_dir() call with btrfs_open_dir_fd()
removing any reference to the unused/useless dirstream variables.

Also update the add_seen_fsid() function removing any reference to dir
stream (again this is never used).

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 11:40:08 +01:00
Goffredo Baroncelli 372c79e7ad btrfs-progs: replace btrfs_open_dir with btrfs_open_dir_fd
For historical reasons the helpers [btrfs_]open_dir... return also
the 'DIR *dirstream' value when a directory is opened.

Replace btrfs_open_dir() with btrfs_open_dir_fd() removing
any reference to the unused/useless dirstream variables.
Calling btrfs_open_dir_fd() with only the path is equivalent to
btrfs_open_dir(_, _, 1).

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-02-20 11:40:08 +01:00
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 cb740b3dfb btrfs-progs: fi show: canonicalize path when using blkid and -d
There's a report that passing raw device mapper path and -d don't work
together:

  yyy@xxx ~ $ sudo btrfs filesystem show /dev/dm-0
  Label: none  uuid: a7fbb8d6-ec5d-4e88-bd8b-c686553e0dc7
	  Total devices 1 FS bytes used 144.00KiB
	  devid    1 size 256.00MiB used 88.00MiB path /dev/mapper/da0972636816-LogVol00

  With --all-devices

  yyy@xxx ~ $ sudo btrfs filesystem show --all-devices /dev/dm-0
  ERROR: not a valid btrfs filesystem: /dev/dm-0

Where dm-0 corresponds to the LogVol00 device from above.

Passing the option -d skips some steps but still uses the real path of
the device that is required for scanning and identification, while
blkid uses the canonicalized path.

The combination of raw device name and -d was not handled as the raw
path is not in cache and thus not recognized. Canonicalization fixes
that although this changes the device name in the output.

Issue: #732
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-31 11:07:39 +01:00
Goffredo Baroncelli fdf7f35d3e btrfs-progs: fi usage: wrong values in case of raid56 and not root
In case of a raid5/6 filesystem 'btrfs fi us' returns wrong values
without the root capabilities:

  $ sudo btrfs fi us /tmp/raid5fs  # as root
  Overall:
      Device size:                   3.00GiB
      Device allocated:              1.51GiB		<--- OK
      Device unallocated:            1.49GiB		<--- OK
      Device missing:                  0.00B
      Device slack:                    0.00B
      Used:                        769.03MiB		<--- OK
      Free (estimated):              1.32GiB      (min: 1.32GiB) <-OK
      Free (statfs, df):             1.32GiB
      Data ratio:                       1.50		<--- OK
      Metadata ratio:                   1.50		<--- OK
      Global reserve:                5.50MiB      (used: 0.00B)
      Multiple profiles:                  no
  [...]

  $ btrfs fi us /tmp/raid5fs      # as user
  WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
  Overall:
      Device size:                   3.00GiB
      Device allocated:                0.00B		<--- WRONG
      Device unallocated:            3.00GiB		<--- WRONG
      Device missing:                  0.00B
      Device slack:                    0.00B
      Used:                            0.00B		<--- WRONG
      Free (estimated):                0.00B      (min: 8.00EiB) <- WRONG
      Free (statfs, df):             1.32GiB
      Data ratio:                       0.00		<--- WRONG
      Metadata ratio:                   0.00		<--- WRONG
      Global reserve:                5.50MiB      (used: 0.00B)
      Multiple profiles:                  no
  [...]

The reason is that the BTRFS_IOC_SPACE_INFO ioctl doesn't return enough
information. To bypass it a scan of the chunks is required when a
raid5/6 profile is present.

To avoid providing wrong information, in case of a raid5/6 filesystem
without the root capabilities the "btrfs fi us" is not executed at all
and a warning with a suggestion to run it as root is printed.

  $ ./btrfs fi us /tmp/t/
  WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
  WARNING: due to the presence of a raid5/raid6 profile, we cannots compute some values;
  WARNING: run as root instead.

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-18 02:14:23 +01:00
Goffredo Baroncelli c653596cff btrfs-progs: dev usage: don't print incorrect unallocated data
If "btrfs dev us" is invoked by a not root user, it is impossible to
collect the chunk info data (not enough privileges). This causes
"btrfs dev us" to print as "Unallocated" value the size of the disk.

This patch handles the case where print_device_chunks() is invoked
without the chunk info data, printing "Unallocated N/A":

Before the patch:

  $ btrfs dev us t/
  WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
  /dev/loop0, ID: 1
     Device size:             5.00GiB
     Device slack:              0.00B
     Unallocated:             5.00GiB  <-- Wrong

  $ sudo btrfs dev us t/
  [sudo] password for ghigo:
  /dev/loop0, ID: 1
     Device size:             5.00GiB
     Device slack:              0.00B
     Data,single:             8.00MiB
     Metadata,DUP:          512.00MiB
     System,DUP:             16.00MiB
     Unallocated:             4.48GiB  <-- Correct

After the patch:

  $ ./btrfs dev us /tmp/t/
  WARNING: cannot read detailed chunk info, per-device usage will not be shown, run as root
  /dev/loop0, ID: 1
     Device size:             5.00GiB
     Device slack:              0.00B
     Unallocated:                 N/A

  $ sudo ./btrfs dev us /tmp/t/
  [sudo] password for ghigo:
  /dev/loop0, ID: 1
     Device size:             5.00GiB
     Device slack:              0.00B
     Data,single:             8.00MiB
     Metadata,DUP:          512.00MiB
     System,DUP:             16.00MiB
     Unallocated:             4.48GiB

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-18 02:14:23 +01:00
Qu Wenruo 389c959d6d btrfs-progs: implement arg_strtou64_with_suffix() with a new helper
This patch introduces a new parser helper, parse_u64_with_suffix(),
which has a better error handling, following all the parse_*()
helpers to return non-zero value for errors.

This new helper is going to replace parse_size_from_string(), which
would directly call exit(1) to stop the whole program.

Furthermore most callers of parse_size_from_string() are expecting
exit(1) for error, so that they can skip the error handling.

For those call sites, introduce a wrapper, arg_strtou64_with_suffix(),
to do that.  The only disadvantage is a little less detailed error
report for why the parse failed, but for most cases the generic error
string should be enough.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-18 02:14:23 +01:00