Skip to contentSkip to conversation

btrfs-progs: fix the return value of "btrfs

PATCH Superseded Qu Wenruo 2 participants 6 messages Jan 10, 2024 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs-progs: fix the return value of "btrfs

There is a bug report ("https://github.com/kdave/btrfs-progs/issues/730") that after commit 5aa959fb3440 ("btrfs-progs: subvolume create: accept multiple arguments"), "btrfs subvolume create" no longer properly return 1 for error cases.

It turns out that the offending commit changed how we determine the return code, thus for several error cases, we still return 0 for create_one_subvolume().

Fix it and add a test case for it.

Qu Wenruo (2):
btrfs-progs: cmd/subvolume: ix return value when the target exists
btrfs-progs: cli-tests: add test case for return value of "btrfs
subvlume create"

 cmds/subvolume.c                              | 11 ++++++-
 .../025-subvolume-create-failures/test.sh     | 30 +++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100755 tests/cli-tests/025-subvolume-create-failures/test.sh

--
2.43.0

author
#1 lore raw

PATCH 1/2 btrfs-progs: cmd/subvolume: ix return value when the target exists

[BUG]
When try to create a subvolume where the target path already exists, the "btrfs" comannd 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 return 0 or 1, it means there is something in that path already.

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

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

Fixes 5aa959fb3440 ("btrfs-progs: subvolume create: accept multiple arguments")

Issue: #730

---
 cmds/subvolume.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
cmds/subvolume.c +10 -1
index 57be9eac5e56..b01d5c80f63d 100644
@@ -160,12 +160,14 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
 	}
 	if (ret >= 0) {
 		error("target path already exists: %s", dst);
+		ret = -EEXIST;
 		goto out;
 	}
 
 	dupname = strdup(dst);
 	if (!dupname) {
 		error_msg(ERROR_MSG_MEMORY, "duplicating %s", dst);
+		ret = -ENOMEM;
 		goto out;
 	}
 	newname = basename(dupname);
@@ -173,18 +175,21 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
 	dupdir = strdup(dst);
 	if (!dupdir) {
 		error_msg(ERROR_MSG_MEMORY, "duplicating %s", dst);
+		ret = -ENOMEM;
 		goto out;
 	}
 	dstdir = dirname(dupdir);
 
 	if (!test_issubvolname(newname)) {
 		error("invalid subvolume name: %s", newname);
+		ret = -EINVAL;
 		goto out;
 	}
 
 	len = strlen(newname);
 	if (len > BTRFS_VOL_NAME_MAX) {
 		error("subvolume name too long: %s", newname);
+		ret = -EINVAL;
 		goto out;
 	}
 
@@ -208,6 +213,8 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
 					goto out;
 				}
 			} else if (ret <= 0) {
+				if (ret == 0)
+					ret = -EEXIST;
 				errno = ret ;
 				error("failed to check directory %s before creation: %m", p);
 				goto out;
@@ -218,8 +225,10 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
 	}
 
 	fddst = btrfs_open_dir(dstdir, &dirstream, 1);
-	if (fddst < 0)
+	if (fddst < 0) {
+		ret = fddst;
 		goto out;
+	}
 
 	pr_verbose(LOG_DEFAULT, "Create subvolume '%s/%s'\n", dstdir, newname);
 	if (inherit) {
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/2 btrfs-progs: cli-tests: add test case for return value of "btrfs subvlume create"

In reply to Qu Wenruo

The test case would check if "btrfs subvolume create":

- Report error on an existing path
- Still report error if mulitple paths are given and one of them already exists
- For above case, still created a subvolume for the good parameter

---
 .../025-subvolume-create-failures/test.sh     | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100755 tests/cli-tests/025-subvolume-create-failures/test.sh
tests/cli-tests/025-subvolume-create-failures/test.sh new file +30 -0
new file mode 100755
index 000000000000..b268a069ba37
@@ -0,0 +1,30 @@
+#!/bin/bash
+# Create subvolume failure cases to make sure the return value is correct
+
+source "$TEST_TOP/common" || exit
+
+setup_root_helper
+prepare_test_dev
+
+run_check_mkfs_test_dev
+run_check_mount_test_dev
+
+# Create one subvolume and one file as place holder for later subvolume
+# creation to fail.
+run_check $SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv1"
+run_check $SUDO_HELPER touch "$TEST_MNT/subv2"
+
+# Using existing path to create a subvolume must fail
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv1"
+
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv2"
+
+# Using multiple subvolumes in one create go, the good one "subv3" should be
+# created
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create \
+	"$TEST_MNT/subv1" "$TEST_MNT/subv2" "$TEST_MNT/subv3"
+
+run_check $SUDO_HELPER stat "$TEST_MNT/subv3"
Signature · 1 line
2.43.0
author
#3 lore raw

Re: [PATCH 2/2] btrfs-progs: cli-tests: add test case for return value of "btrfs subvlume create"

Quoted 39 lines · Qu Wenruo

The test case would check if "btrfs subvolume create":

- Report error on an existing path
- Still report error if mulitple paths are given and one of them already exists
- For above case, still created a subvolume for the good parameter

---
  .../025-subvolume-create-failures/test.sh     | 30 +++++++++++++++++++
  1 file changed, 30 insertions(+)

create mode 100755 tests/cli-tests/025-subvolume-create-failures/test.sh

tests/cli-tests/025-subvolume-create-failures/test.sh new file +20 -0
new file mode 100755
index 000000000000..b268a069ba37
@@ -0,0 +1,30 @@
+#!/bin/bash
+# Create subvolume failure cases to make sure the return value is correct
+
+source "$TEST_TOP/common" || exit
+
+setup_root_helper
+prepare_test_dev
+
+run_check_mkfs_test_dev
+run_check_mount_test_dev
+
+# Create one subvolume and one file as place holder for later subvolume
+# creation to fail.
+run_check $SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv1"
+run_check $SUDO_HELPER touch "$TEST_MNT/subv2"
+
+# Using existing path to create a subvolume must fail
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv1"
+

tests/cli-tests/025-subvolume-create-failures/test.sh line 30

Quoted 10 lines
tests/cli-tests/025-subvolume-create-failures/test.sh new file +10 -0
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv2"
+
+# Using multiple subvolumes in one create go, the good one "subv3" should be
+# created
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create \
+	"$TEST_MNT/subv1" "$TEST_MNT/subv2" "$TEST_MNT/subv3"
+
+run_check $SUDO_HELPER stat "$TEST_MNT/subv3"

My bad, I forgot to unmount the test dev

David, mind to add the following line to clean it up?

run_check_umount_test_dev

Thanks,
Qu

#4 lore raw

Re: [PATCH 2/2] btrfs-progs: cli-tests: add test case for return value of "btrfs subvlume create"

Quoted 55 lines · Qu Wenruo
Quoted 39 lines · Qu Wenruo

The test case would check if "btrfs subvolume create":

- Report error on an existing path
- Still report error if mulitple paths are given and one of them already exists
- For above case, still created a subvolume for the good parameter

---
  .../025-subvolume-create-failures/test.sh     | 30 +++++++++++++++++++
  1 file changed, 30 insertions(+)

create mode 100755 tests/cli-tests/025-subvolume-create-failures/test.sh

tests/cli-tests/025-subvolume-create-failures/test.sh new file +20 -0
new file mode 100755
index 000000000000..b268a069ba37
@@ -0,0 +1,30 @@
+#!/bin/bash
+# Create subvolume failure cases to make sure the return value is correct
+
+source "$TEST_TOP/common" || exit
+
+setup_root_helper
+prepare_test_dev
+
+run_check_mkfs_test_dev
+run_check_mount_test_dev
+
+# Create one subvolume and one file as place holder for later subvolume
+# creation to fail.
+run_check $SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv1"
+run_check $SUDO_HELPER touch "$TEST_MNT/subv2"
+
+# Using existing path to create a subvolume must fail
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv1"
+

tests/cli-tests/025-subvolume-create-failures/test.sh line 30

Quoted 10 lines
tests/cli-tests/025-subvolume-create-failures/test.sh new file +10 -0
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create "$TEST_MNT/subv2"
+
+# Using multiple subvolumes in one create go, the good one "subv3" should be
+# created
+run_mustfail "should report error when target path already exists" \
+	$SUDO_HELPER "$TOP/btrfs" subvolume create \
+	"$TEST_MNT/subv1" "$TEST_MNT/subv2" "$TEST_MNT/subv3"
+
+run_check $SUDO_HELPER stat "$TEST_MNT/subv3"

My bad, I forgot to unmount the test dev

Quoted 3 lines

David, mind to add the following line to clean it up?

run_check_umount_test_dev

Updated, thanks.

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

There is a bug report ("https://github.com/kdave/btrfs-progs/issues/730") that after commit 5aa959fb3440 ("btrfs-progs: subvolume create: accept multiple arguments"), "btrfs subvolume create" no longer properly return 1 for error cases.

It turns out that the offending commit changed how we determine the return code, thus for several error cases, we still return 0 for create_one_subvolume().

Quoted 6 lines

Fix it and add a test case for it.

Qu Wenruo (2):
btrfs-progs: cmd/subvolume: ix return value when the target exists
btrfs-progs: cli-tests: add test case for return value of "btrfs
subvlume create"

Added to devel, thanks.