Skip to contentSkip to conversation

btrfs-progs: subvolume create: accept multiple arguments

PATCH In review Qu Wenruo 2 participants 6 messages Nov 2, 2023 – Nov 23, 2023

Subsystems (2)
lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs-progs: subvolume create: accept multiple arguments

This patchset adds the ability to accept multiple arguments for "btrfs subvolume create" command, just like "mkdir".

And also we follow the error reporting part of "mkdir", any failure would make the command to return 1 for error.

[PATCHSET STRUCTURE]
During the development, I found two missing error handling for strdup(), thus here comes the first patch to fix them.

Then the 2nd patch implements the main part.

Finally the last patch is add the new test case for the error handling part.

Qu Wenruo (3):
btrfs-progs: subvolume create: handle failure for strdup()
btrfs-progs: subvolume create: accept multiple arguments
btrfs-progs: cli-tests: add test case for subvolume create multiple
arguments

 Documentation/btrfs-subvolume.rst             |  11 +-
 cmds/subvolume.c                              | 223 ++++++++++--------
 .../021-subvolume-multiple-arguments/test.sh  |  37 +++
 3 files changed, 170 insertions(+), 101 deletions(-)
 create mode 100755 tests/cli-tests/021-subvolume-multiple-arguments/test.sh

--
2.42.0

author
#1 lore raw

PATCH 1/3 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.

---
 cmds/subvolume.c | 9 +++++++++
 1 file changed, 9 insertions(+)
cmds/subvolume.c +9 -0
index 8504c380c9ee..bccc4968dad3 100644
@@ -194,8 +194,17 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a
 	}
 
 	dupname = strdup(dst);
+	if (!dupname) {
+		error("out of memory when duplicating %s", dst);
+		goto out;
+	}
 	newname = basename(dupname);
+
 	dupdir = strdup(dst);
+	if (!dupdir) {
+		error("out of memory when duplicating %s", dst);
+		goto out;
+	}
 	dstdir = dirname(dupdir);
 
 	if (!test_issubvolname(newname)) {
Signature · 1 line
2.42.0
author
#3 lore raw

PATCH 2/3 btrfs-progs: subvolume create: accept multiple arguments

In reply to Qu Wenruo

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

---
 Documentation/btrfs-subvolume.rst |  11 ++-
 cmds/subvolume.c                  | 150 +++++++++++++++++-------------
 2 files changed, 92 insertions(+), 69 deletions(-)
Documentation/btrfs-subvolume.rst +9 -2
index 6da00be8dc86..8b434475337b 100644
@@ -49,12 +49,19 @@ do not affect the files in the original subvolume.
 SUBCOMMAND
 -----------
 
-create [options] [<dest>/]<name>
-        Create a subvolume *name* in *dest*.
+create [options] [<dest>/]<name> [[<dest2>/]<name2>] ...
+        Create subvolume(s) at the destination(s).
 
         If *dest* is not given, subvolume *name* will be created in the current
         directory.
 
+	If multiple desinations are given, then the options are applied to all
+	subvolumes.
+
+	If failure happened for any of the destinations, the command would
+	still retry the remaining destinations, but would return 1 to indicate
+	the failure.
+
         ``Options``
 
         -i <qgroupid>
cmds/subvolume.c +83 -67

207 lines not on this page · raw message

Signature · 1 line
2.42.0
author
#4 lore raw

PATCH 3/3 btrfs-progs: cli-tests: add test case for subvolume create multiple arguments

In reply to Qu Wenruo

The test case would verify the following behaviors:

- Partial failure
Should return 1, but the remaining valid destinations would still be created.

- All success
That's as usual.

---
 .../021-subvolume-multiple-arguments/test.sh  | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100755 tests/cli-tests/021-subvolume-multiple-arguments/test.sh
tests/cli-tests/021-subvolume-multiple-arguments/test.sh new file +37 -0
new file mode 100755
index 000000000000..f86763829d31
@@ -0,0 +1,37 @@
+#!/bin/bash
+# Create subvolume with multiple destinations
+
+source "$TEST_TOP/common" || exit
+
+setup_root_helper
+prepare_test_dev
+
+run_check_mkfs_test_dev
+run_check_mount_test_dev
+
+# Create one invalid subvolume with 2 valid ones.
+# The command should return 1 but the 2 valid ones should be created.
+run_mustfail "should report error for any failed subvolume creation" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create \
+	"$TEST_MNT/non-exist-dir/subv0" \
+	"$TEST_MNT/subv1" \
+	"$TEST_MNT/subv2"
+
+run_check $SUDO_HELPER stat "$TEST_MNT/subv1"
+run_check $SUDO_HELPER stat "$TEST_MNT/subv2"
+
+# Create multiple subvolumes with parent
+run_check $SUDO_HELPER "$TOP/btrfs" subvolume create -p \
+	"$TEST_MNT/non-exist-dir/subv0" \
+	"$TEST_MNT/subv1/subv3" \
+	"$TEST_MNT/subv4" \
+
+run_check $SUDO_HELPER stat "$TEST_MNT/non-exist-dir/subv0"
+run_check $SUDO_HELPER stat "$TEST_MNT/subv1/subv3"
+run_check $SUDO_HELPER stat "$TEST_MNT/subv4"
+
+run_check $SUDO_HELPER "$TOP/btrfs" subvolume create -p "$TEST_MNT/dir3/dir1/./..//.///subv3//////"
+run_check $SUDO_HELPER stat "$TEST_MNT/dir3/dir1"
+run_check $SUDO_HELPER stat "$TEST_MNT/dir3/subv3"
+run_check find "$TEST_MNT" -ls
+run_check_umount_test_dev
Signature · 1 line
2.42.0
#2 lore raw

Re: [PATCH 1/3] btrfs-progs: subvolume create: handle failure for strdup()

In reply to Qu Wenruo
Quoted 13 lines · Qu Wenruo

The function strdup() can return NULL if the system is out of memory, thus we need to hanle the rare but possible -ENOMEM case.

---
 cmds/subvolume.c | 9 +++++++++
 1 file changed, 9 insertions(+)
cmds/subvolume.c +0 -0
index 8504c380c9ee..bccc4968dad3 100644

cmds/subvolume.c static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a · line 198

Quoted 5 lines
cmds/subvolume.c +2 -0
@@ -194,8 +194,17 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a
 	}
 
 	dupname = strdup(dst);
+	if (!dupname) {
+		error("out of memory when duplicating %s", dst);

There are message templats for the most common errors, so this is now

error_msg(ERROR_MSG_MEMORY, "duplicating %s", dst);

#5 lore raw
In reply to Qu Wenruo
Quoted 12 lines · Qu Wenruo

This patchset adds the ability to accept multiple arguments for "btrfs subvolume create" command, just like "mkdir".

And also we follow the error reporting part of "mkdir", any failure would make the command to return 1 for error.

[PATCHSET STRUCTURE]
During the development, I found two missing error handling for strdup(), thus here comes the first patch to fix them.

Then the 2nd patch implements the main part.

Quoted 8 lines

Finally the last patch is add the new test case for the error handling part.

Qu Wenruo (3):
btrfs-progs: subvolume create: handle failure for strdup()
btrfs-progs: subvolume create: accept multiple arguments
btrfs-progs: cli-tests: add test case for subvolume create multiple
arguments

Added to devel, thanks.