Commit graph

503 commits

Author SHA1 Message Date
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
Jonas Malaco 1d37d3ef91 btrfs-progs: scrub start: consider the lowest non-zero limit
On multi-device filesystems, a scrub limit may be applied to any of the
devices. Ensure that any limit found is not disregarded.

Since it's more intuitive, keep the lowest non-zero limit found, even
though at the present we don't actually use the exact value.

Pull-request: #733
Issue: #727
Fixes: 7e4a235df1 ("btrfs-progs: scrub status: print device speed limit in status if set")
Signed-off-by: Jonas Malaco <jonas@protocubo.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-15 17:07:22 +01:00
Jonas Malaco 96011debe4 btrfs-progs: scrub status: consider the lowest non-zero limit
On multi-device filesystems, a scrub limit may be applied to any of the
devices. Ensure that any limit found is not disregarded.

Since it's more intuitive, keep the lowest non-zero limit found, even
though at the present we don't actually use the exact value.

Pull-request: #733
Issue: #727
Fixes: 7e4a235df1 ("btrfs-progs: scrub status: print device speed limit in status if set")
Signed-off-by: Jonas Malaco <jonas@protocubo.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-15 17:07:22 +01:00
Jonas Malaco 8bca55d1e7 btrfs-progs: scrub status: only report limits if at least one exists
On multi-device filesystems, scrub status should report "some limits
set" if at least one device has a scrub limit set.

However, with btrfs-progs 6.6.3, this was being reported regardless of
whether any limit actually being set:

    # sudo btrfs scrub limit /more/butter
    UUID: 989129d9-c96f-4d52-9d68-cbb6d9b2c499
    Id  Limit       Path
    --  -----  ---------
     1      -  /dev/sdc1
     2      -  /dev/sdd1

    # sudo btrfs scrub status /more/butter/
    UUID:             989129d9-c96f-4d52-9d68-cbb6d9b2c499
    Scrub started:    Mon Jan 15 02:00:30 2024
    Status:           running
    Duration:         6:23:19
    Time left:        0:49:08
    ETA:              Mon Jan 15 09:12:57 2024
    Total to scrub:   9.83TiB
    Bytes scrubbed:   8.72TiB  (88.64%)
    Rate:             397.47MiB/s (some device limits set)
    Error summary:    no errors found

Fix it by only setting `limit` to the special marker value 1 if at least
one actual limit is found.

Pull-request: #733
Issue: #727
Fixes: 7e4a235df1 ("btrfs-progs: scrub status: print device speed limit in status if set")
Signed-off-by: Jonas Malaco <jonas@protocubo.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-15 17:07:19 +01:00
Qu Wenruo 3f988c9176 btrfs-progs: subvolume: fix return value when the target exists
[BUG]
When try to create a subvolume where the target path already exists, the
"btrfs" command doesn't return error code correctly.

  # mkfs.btrfs -f $dev
  # mount $dev $mnt
  # touch $mnt/subv1
  # btrfs subvolume create $mnt/subv1
  ERROR: target path already exists: $mnt/subv1
  # echo $?
  0

[CAUSE]
The check on whether target exists is done by path_is_dir(), if it
returns 0 or 1, it means there is something in that path already.

But unfortunately commit 5aa959fb34 ("btrfs-progs: subvolume create:
accept multiple arguments") only changed the out label, which would
directly return @ret, not updating the return value correctly.

[FIX]
Make sure all error out branch has their @ret manually updated.

Fixes: 5aa959fb34 ("btrfs-progs: subvolume create: accept multiple arguments")
Issue: #730
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-01-10 04:16:13 +01:00
David Sterba eaa54973b6 btrfs-progs: scrub limit: add option to apply the limit to all devices
Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-09 01:57:29 +01:00
David Sterba 30d1a2d390 btrfs-progs: scrub limit: allow to set the limit
Add new options to set the per-device limit (requires root privileges as
it writes to the sysfs files).

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-09 01:57:27 +01:00
David Disseldorp b3b38751e9 btrfs-progs: scrub status: improve Rate reporting for sub-second durations
Scrubs which complete in under one second may carry a duration rounded
down to zero. This subsequently results in a bytes_per_sec value of
zero, which corresponds to the Rate metric output, causing intermittent
tests/btrfs/282 failures.

This change ensures that Rate reflects any sub-second bytes processed.
Time left and ETA metrics are also affected by this change, in that they
increase to account for (sub-second) bytes_per_sec.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Disseldorp <ddiss@suse.de>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-09 01:17:22 +01:00
David Sterba 9a044fa0ff btrfs-progs: scrub start: print device limit if set
Print one message per scrubbed device and also print the limit if set:

  $ btrfs scrub start /mnt
  scrub started on /mnt, fsid 9ee93131-f680-4d6c-8ca4-a194506e3081 (pid=27257)
  Starting scrub on devid 1 (limit 100.00MiB/s)

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-09 01:17:22 +01:00
David Sterba fbf211459a btrfs-progs: srcub: new subcommand limit
Add new command to read the scrub limits set via the sysfs file (no root
access needed).

Example output:

  $ btrfs scrub limit /mnt
  UUID: 57a05502-9e81-4b21-ad9d-0fc31863ed11
  Id  Limit            Path
  --  -----  --------------
   1      -  /dev/nvme0n1p1
   2      -  /dev/nvme0n1p2
   3      -  /dev/nvme0n1p3
   4      -  /dev/nvme2n1p4
   5      -  /dev/nvme0n1p5
   6      -  /dev/nvme0n1p6

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-09 01:17:22 +01:00
David Sterba b5909e62b2 btrfs-progs: use statvfs() in print_filesystem_usage_overall
The statfs(2) syscall is deprecated by LSB in favor of statvfs(2),
however we can't replace all uses because we still need the
statfs::f_type to determine the filesystem by magic numer.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-08 17:08:55 +01:00
David Sterba 71ad6f2f53 btrfs-progs: subvol sync: check if the filesystem is writable
The subvolume cleaning is done by polling but it's possible that the
filesystem turns to read-only (as reported), either due to an error
intentionally. In that case the waiting would be indefinite without an
obvious reason.

To fix that check if the filesystem is still writable in each iteration.

Issue: #535
Link: https://github.com/btrfs/fstests/issues/40
Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-08 17:08:55 +01:00
David Sterba 7e4a235df1 btrfs-progs: scrub status: print device speed limit in status if set
When there's a speed limit set for a device via
/sysfs/fs/btrfs/FSID/devinfo/scrub_speed_max, show it in the scrub status
output like below:

  $ btrfs scrub status -d /mnt
  ...
  Rate:              47.98MiB/s (limit 60MiB/s)
  ...

If the limit is 0 this means unlimited and is not printed.

For a single device filesystem the limit is printed even without '-d' as
it's clear which device limit applies. For multi-device filesysetms,
without any limits nothing is printed, if there at least one device
limit set then the following is printed:

  Rate:             36.37MiB/s (some device limits set)

More details with the -d option.

Issue: #531
Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-08 17:08:33 +01:00
David Sterba f8711016eb btrfs-progs: receive: properly report lack of zstd support
If zstd is not compiled in then a stream fails with a generic error
message:

ERROR: unknown compression: 2

Where BTRFS_ENCODED_IO_COMPRESSION_ZSTD is 2 and there's a case for that
but behind the '#if COMPRESSION_ZSTD'.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-07 14:29:54 +01:00
David Sterba 5c91264d2d btrfs-progs: subvol delete: print the id of the deleted subvolume
Currently the path of deleted subvolume is printed, we should also print
the numeric id as it's another identifier commonly found and can be used
for a cross reference. In connection with the qgroup deletion it's
making the output clear:

...
Delete subvolume 258 (no-commit): '/mnt/subv1'
Delete qgroup 0/258
...

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-05 17:08:42 +01:00
David Sterba 9da773aa46 btrfs-progs: subvol delete: add options to delete the qgroup
The 0/subvolid qgroups are not automatically deleted when the subvolume
is deleted, for historical reasons. There's a command to clean up all
such stale qgroups (btrfs qgroup clean-stale) but this should be also
possible with the subvolume deletion.

With the options we can switch the default to delete the qgroup by
default eventually, if somebody depends on the not deleting behaviour
the negation option can be used.

Issue: #366
Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-05 12:36:44 +01:00
David Sterba 9e55abfd77 btrfs-progs: device delete: add timeout when removing multiple devices
Reported on IRC, that it's unexpected that passing several devices on
command line for 'btrfs device delete' still uses some of the devices
during deletion. The expectation was that they'd be removed at once (and
thus not used for the intermediate chunk relocation).

As it works now, the ioctl removes only one device. As a workaround, add
a timeout (like we have for the full balance and others) when there are
more devices passed on the command line. This can be skipped by the
--force parameter.

Issue: #708
Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-01 02:54:09 +01:00
David Sterba 9da7e92af9 btrfs-progs: use get_sysfs_proto_supported() in get_sysfs_proto_supported()
Use the sysfs helper to read u64 value of send stream.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-12-01 01:58:56 +01:00
Qu Wenruo 5aa959fb34 btrfs-progs: subvolume create: accept multiple arguments
This patch would make "btrfs subvolume create" to accept multiple
arguments, just like "mkdir".

The existing options like "-i <qgroupid>" and "-p" would all be applied
to all subvolume(s).

If one destination failed, the command would return 1, while still retry
the remaining destinations.

Issue: #695
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-23 20:15:47 +01:00
Qu Wenruo 943a69ef2d btrfs-progs: subvolume create: handle failure for strdup()
The function strdup() can return NULL if the system is out of memory,
thus we need to hanle the rare but possible -ENOMEM case.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-23 20:11:17 +01:00
David Sterba 4d7ea19ca0 btrfs-progs: property set: skip opening char devices completely
Previous fix for char devices and properties opens the path in non
blocking mode but this still triggers the watchdog, as reported. Add a
workaround to properties to completely skip opening the path and just
stat() it.

Issue: #699
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-14 15:48:00 +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 c08071e7be btrfs-progs: resize: properly return error from check_resize_args
The function almost always returns 0 even for errors as the ret value is
not used in the final return. This was attempted to be fixed in
55438f3930 ("btrfs-progs: resize: return error value from
check_resize_args()") but this broke 'resize cancel' when devid 1 was
missing and was later reverted as 4286eb552e ("Revert "btrfs-progs:
resize: return error value from check_resize_args()"").

The devid fallback has been fixed so the proper return value can be
returned now.

Issue: #539
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
David Sterba b64f8437b6 btrfs-progs: resize: fallback to lowest devid when 1 does not exist
The implicit devid is 1 but when it does not exist then the command
'btrfs fi resize max /path' fails and requires the user to specify the
number (and finding it elsewhere, e.g. in 'btrfs fi us -T' output).

This is a usability bug, we can verify if devid 1 exists and use the
lowest devid as a fallback. This does what user would expected, though
there's still a warning. Kernel has the hardcoded devid 1 when none is
specified, with this fix in user space the kernel does not need to be
changed (or could behave the same eventually).

Example use:

  $ btrfs fi us -T .
		Data      Metadata  System
  Id Path       single    single    single   Unallocated Total   Slack
  -- ---------- --------- --------- -------- ----------- ------- -----
   4 /dev/loop3         -         -        -     4.00GiB 4.00GiB     -
  -- ---------- --------- --------- -------- ----------- ------- -----
     Total      416.00MiB 256.00MiB 64.00MiB     4.00GiB 4.00GiB 0.00B
     Used           0.00B 128.00KiB 16.00KiB

  $ btrfs fi resize max .
  WARNING: no devid specified means devid 1 which does not exist, using
           lowest devid 4 as a fallback
  Resize device id 4 (/dev/loop3) from 4.00GiB to max

Issue: #470
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
David Sterba 93f263650c btrfs-progs: fi defrag: add new option to process the range in steps
Add a convenience option to processing the range in smaller steps than
the whole file, where a flush is done after each steps. This could be
potentially used to measure progress with 'btrfs -vv fi defrag'.

Issue: #616
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04: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
David Sterba f7fb93f1ac btrfs-progs: remove old syntax of 'balance /path'
There was a support for a short syntax of 'btrfs balance' that accepted
a path where normally would be the mandatory subcommand. This was a
heuristic and nowadays everybody should be using the
'btrfs balance action' syntax. The warning was in place for a year, it's
time to remove the short syntax completely.

Issue: #517
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
David Sterba c9ebef41ec btrfs-progs: use unsigned type for bit shift values
Bit shifts should be done on unsigned types, do that in remaining code.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
David Sterba e899c5f89c btrfs-progs: subvolume delete: implement dry-run global option
Allow user to do dry-run deletion, doing any pre-checks and printing
which subvolumes would be deleted. Lack of access rights can still lead
to errors.

Issue: #629
Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00