Commit graph

95 commits

Author SHA1 Message Date
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
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
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 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
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 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 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 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 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 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 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 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 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
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 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
David Sterba b764ba3c4a btrfs-progs: cmds: drop unsigned long long casts for printf
The %llu specifier does not need the typecast for ULL for a long time,
remove it.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-11-03 18:04:37 +01:00
Sidong Yang ac4bb90e7b btrfs-progs: subvolume create: add option -p option to create parent directories
Add new option -p to 'subvolume create' so it behaves like 'mkdir -p'
and create all missing path components before the subvolume.

Issue: #429
Signed-off-by: Sidong Yang <realwakka@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-21 15:51:07 +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
David Sterba 7ed79f2b3b btrfs-progs: use escaped format for subvolume path strings in json
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba 70ae158ec8 btrfs-progs: rename time-long format name to date-time
Make the timestamp format more descriptive what is actually printed. We
may need separate date or time in the future.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba 29060ec176 btrfs-progs: subvolume show: print all items unconditionally in json
The json output could be easily filtered on the user side so we'll print
everything unconditionally.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba 42d0861e31 btrfs-progs: subvolume: rename some json key names
Don't abbreviate generation and use qgroup where it's related to the
qgroup itself and not quotas in general.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
David Sterba 6120fdadc2 btrfs-progs: subvolume: move json format output under experimental
The format of the json data needs some time to be finalized so it'll
be under experimental.

Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
Christoph Heiss 328333f9cf btrfs-progs: subvol show: implement json format output
Implements JSON-formatted output for the `subvolume list` command using
the `--format json` global option, much like it is implemented for other
commands.

Signed-off-by: Christoph Heiss <christoph@c8h4.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
Christoph Heiss f134c86411 btrfs-progs: subvol get-default: implement json format output
Implements JSON-formatted output for the `subvolume get-default` command
using the `--format json` global option, much like it is implemented for
other commands.

Signed-off-by: Christoph Heiss <christoph@c8h4.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
Christoph Heiss da440e66fb btrfs-progs: subvol: introduce rowspec definition for json output
List and export all fields that may be needed for any subvolume related
json output.

Signed-off-by: Christoph Heiss <christoph@c8h4.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
Christoph Heiss 7c983d2352 btrfs-progs: subvol show: factor out text printing to own function
Prepare for switching the plain and json output. The format is slightly
different so we can't utilize the unified fmt_* helpers and two separate
printer functions make more sense.

Signed-off-by: Christoph Heiss <christoph@c8h4.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
Christoph Heiss 8b73f22484 btrfs-progs: subvol show: remove duplicated quotas error check
The exact same check is repeated here, with the second being dead code.
Keep the second instance, as that informs the user what is happening.

Signed-off-by: Christoph Heiss <christoph@c8h4.io>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-08-28 17:24:23 +02:00
Josef Bacik a754fe29d9 btrfs-progs: sync uapi/btrfs.h into btrfs-progs
We want to keep this file locally as we want to be uptodate with
upstream, so we can build btrfs-progs regardless of which kernel is
currently installed.  Sync this with the upstream version and put it in
kernel-shared/uapi to maintain some semblance of where this file comes
from.

There are some changes that need to be synced back to kernel. A local
definition of static_assert is used to avoid compilation problems on gcc
(< 9) due to mandatory 2nd parameter.

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 938c6a19a9 btrfs-progs: subvol snapshot: adjust error message when there's an active swapfile
Attempting to create a snapshot of subvolume with an active swapfile
prints the errno message corresponding to ETXTBSY but this is confusing
so change it to be more descriptove to:

  ERROR: cannot snapshot '/hibernate': source subvolume contains an active swapfile (Text file busy)

Issue: #607
Signed-off-by: David Sterba <dsterba@suse.com>
2023-04-25 16:59:42 +02:00
David Sterba f7b5e6924a btrfs-progs: subvolume: convert help text to option formatter
Signed-off-by: David Sterba <dsterba@suse.com>
2023-02-28 20:11:24 +01:00
Qu Wenruo f61b90aff9 btrfs-progs: make usage call properly return an exit value
[BUG]
Currently cli/009 test case failed with different exit number:

  ====== RUN CHECK /home/adam/btrfs-progs/btrfstune --help
  usage: btrfstune [options] device
  [...]
  failed: /home/adam/btrfs-progs/btrfstune --help
  test failed for case 009-btrfstune

[CAUSE]
In tune/main.c, we have the following call on usage():

  static void print_usage(int ret)
  {
	usage(&tune_cmd);
	exit(ret);
  }

However usage() itself would always call exit(1):

  void usage(const struct cmd_struct *cmd)
  {
	usage_command_usagestr(cmd->usagestr, NULL, 0, true, true);
	exit(1);
  }

This makes prevents any caller of usage() to modify its exit number.

[FIX]
Add a new argument @error for print_usage(), so we can properly return 0
for -h/--help usage.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-02-28 20:11:23 +01:00
David Sterba 7305c69cf9 btrfs-progs: subvol sync: print count and progress
By default print how many subvolumes are considered for checks, either
found or specified on the command line. Once a subvolume is removed from
the list print the progress from the total count.

  $ btrfs subvolume sync /path
  Waiting for 130 subvolumes
  Subvolume id 256 is gone (1/130)
  Subvolume id 257 is gone (2/130)
  ...
  Subvolume id 384 is gone (129/130)
  Subvolume id 385 is gone (130/130)

Signed-off-by: David Sterba <dsterba@suse.com>
2023-01-25 19:55:47 +01:00
David Sterba e78fe2d92e Revert "btrfs-progs: rename qgroup items to match the kernel naming scheme"
This reverts commit 03451430de.
(It's not 1:1, there are some additional trivial fixups in cmds/qgroup.c)

This breaks a lot of 3rd party tools that depend on it as Neal reports:

* btrfs-assistant
* buildah
* cri-o
* podman
* skopeo
* containerd
* moby/docker
* snapper
* source-to-image

Link: https://lore.kernel.org/linux-btrfs/CAEg-Je8L7jieKdoWoZBuBZ6RdXwvwrx04AB0fOZF1fr5Pb-o1g@mail.gmail.com/
Reported-by: Neal Gompa <ngompa@fedoraproject.org>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-01-03 13:10:54 +01:00
Josef Bacik 03451430de btrfs-progs: rename qgroup items to match the kernel naming scheme
We're going to sync the kernel source into btrfs-progs, and in the
kernel we have all these qgroup fields named with short names instead of
the full name, so rename

referenced -> rfer
compressed -> cmpr
exclusive -> excl

to match the kernel and update all the users, this will make the sync
cleaner.

ioctl.h is a public header but there are no users of the
btrfs_qgroup_limit structure.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-11-28 18:57:43 +01:00
David Sterba 8bc13b39aa btrfs-progs: subvol: fix help text reference to subvolume
The commonly used reference to subvolume id is the one without a dash.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-26 10:15:40 +02:00
David Sterba f6a212b8e6 btrfs-progs: unify naming of subvolume command definitions
Use full 'subvolume' in all cmd defintions to unify that with other
commands.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-25 20:45:00 +02:00
David Sterba 447f976472 btrfs-progs: subvol delete: update EPERM error message
The message could be confusing in case there's no send in progress and
the real reason is lack of permissions when deleting a subvolume.
Mention the permissions as first reason. Also update documentation.

Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-24 15:14:47 +02: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 f83e5d9c7c btrfs-progs: subvolume: 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