* [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev()
2026-11-02 2:22 [LTP] [PATCH v18 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
@ 2026-11-02 2:22 ` Wei Gao via ltp
2026-11-02 8:25 ` [LTP] " linuxtestproject.agent
2026-11-12 13:05 ` [LTP] [PATCH v18 1/3] " Cyril Hrubis
2026-11-02 2:22 ` [LTP] [PATCH v18 2/3] newlib_tests: Add tst_attach_loopdev test Wei Gao via ltp
2026-11-02 2:22 ` [LTP] [PATCH v18 3/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2 siblings, 2 replies; 7+ messages in thread
From: Wei Gao via ltp @ 2026-11-02 2:22 UTC (permalink / raw)
To: ltp
Calling tst_find_free_loopdev() followed by tst_attach_device() has
a race window where another process can claim the loop device before
LOOP_SET_FD is issued, causing attachment to fail with EBUSY.
Introduce tst_attach_loopdev() which finds an unused loop device and
attaches a backing file to it, retrying the lookup with exponential
backoff if the candidate device is claimed concurrently (EBUSY). In
addition, only populate the caller's device path buffer on successful
attachment, and clear it on failure to prevent accidental cleanup of
unattached devices.
Update tst_acquire_loop_device() to use tst_attach_loopdev().
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Wei Gao <wegao@suse.com>
---
v17->v18:
- New patch in v18.
include/tst_device.h | 13 +++++++
lib/tst_device.c | 87 ++++++++++++++++++++++++++++++++++++++++----
2 files changed, 92 insertions(+), 8 deletions(-)
diff --git a/include/tst_device.h b/include/tst_device.h
index 0989e4501..2e7dadd59 100644
--- a/include/tst_device.h
+++ b/include/tst_device.h
@@ -110,6 +110,19 @@ int tst_find_free_loopdev(char *path, size_t path_len);
*/
int tst_attach_device(const char *dev_path, const char *file_path);
+/**
+ * tst_attach_loopdev() - Find a free loop device and attach a file to it.
+ * @dev_path: If non-NULL, buffer filled with the attached loop device path.
+ * @dev_path_len: Size of @dev_path buffer.
+ * @file_path: Path to a file (e.g. disk.img).
+ *
+ * Finds an unused loop device and attaches @file_path to it, automatically
+ * retrying the lookup if the chosen device is claimed concurrently (EBUSY).
+ *
+ * Return: Zero on success, non-zero otherwise.
+ */
+int tst_attach_loopdev(char *dev_path, size_t dev_path_len, const char *file_path);
+
/**
* tst_get_device_size() - Get device size in megabytes.
* @dev_path: Device path.
diff --git a/lib/tst_device.c b/lib/tst_device.c
index 00304711b..199c14059 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -156,7 +156,7 @@ int tst_find_free_loopdev(char *path, size_t path_len)
return -1;
}
-int tst_attach_device(const char *dev, const char *file)
+static int attach_device(const char *dev, const char *file, int warn_on_ebusy)
{
int dev_fd, file_fd;
struct loop_info loopinfo;
@@ -168,7 +168,8 @@ int tst_attach_device(const char *dev, const char *file)
dev_fd = TST_RETRY_FN_EXP_BACKOFF(open(dev, O_RDWR), TST_RETVAL_GE0, 1);
if (dev_fd < 0) {
- tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
+ if (warn_on_ebusy || errno != EBUSY)
+ tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
return 1;
}
@@ -180,10 +181,16 @@ int tst_attach_device(const char *dev, const char *file)
}
if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
+ int err = errno;
+
close(dev_fd);
close(file_fd);
- tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
- dev, file);
+ errno = err;
+
+ if (warn_on_ebusy || errno != EBUSY) {
+ tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
+ dev, file);
+ }
return 1;
}
@@ -207,6 +214,73 @@ int tst_attach_device(const char *dev, const char *file)
return 0;
}
+int tst_attach_device(const char *dev, const char *file)
+{
+ return attach_device(dev, file, 1);
+}
+
+static int attach_loopdev(char *dev, size_t dev_len, const char *file)
+{
+ if (tst_find_free_loopdev(dev, dev_len) == -1)
+ return -1;
+
+ if (attach_device(dev, file, 0) == 0)
+ return 0;
+
+ if (errno == EBUSY)
+ return 1;
+
+ return -1;
+}
+
+int tst_attach_loopdev(char *dev_path, size_t dev_path_len, const char *file)
+{
+ char dev[PATH_MAX];
+ unsigned int delay = 1;
+ unsigned int max_delay = tst_multiply_timeout(1000000);
+ int ret, fd;
+
+ if (strlen(file) >= LO_NAME_SIZE) {
+ tst_brkm(TBROK, NULL, "Device name can't be longer than %u chars",
+ LO_NAME_SIZE);
+ }
+
+ fd = open(file, O_RDWR);
+ if (fd < 0) {
+ tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", file);
+ goto fail;
+ }
+ close(fd);
+
+ for (;;) {
+ ret = attach_loopdev(dev, sizeof(dev), file);
+ if (ret <= 0)
+ break;
+
+ if (delay < max_delay) {
+ usleep(delay);
+ delay *= 2;
+ } else {
+ ret = -1;
+ break;
+ }
+ }
+
+ if (ret == 0) {
+ if (dev_path && dev_path_len) {
+ strncpy(dev_path, dev, dev_path_len);
+ dev_path[dev_path_len - 1] = '\0';
+ }
+ return 0;
+ }
+
+fail:
+ if (dev_path && dev_path_len)
+ dev_path[0] = '\0';
+
+ return 1;
+}
+
uint64_t tst_get_device_size(const char *dev_path)
{
int fd;
@@ -390,10 +464,7 @@ const char *tst_acquire_loop_device(unsigned int size, const char *filename)
return NULL;
}
- if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
- return NULL;
-
- if (tst_attach_device(dev_path, filename))
+ if (tst_attach_loopdev(dev_path, sizeof(dev_path), filename))
return NULL;
return dev_path;
--
2.55.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [LTP] lib: Add tst_attach_loopdev()
2026-11-02 2:22 ` [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
@ 2026-11-02 8:25 ` linuxtestproject.agent
2026-11-12 13:05 ` [LTP] [PATCH v18 1/3] " Cyril Hrubis
1 sibling, 0 replies; 7+ messages in thread
From: linuxtestproject.agent @ 2026-11-02 8:25 UTC (permalink / raw)
To: Wei Gao; +Cc: ltp
Hi Wei,
On Mon Nov 2 02:22:39 2026 +0000, Wei Gao <wegao@suse.com> wrote:
> lib: Add tst_attach_loopdev()
Verdict - Reviewed
---
Note:
The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev()
2026-11-02 2:22 ` [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
2026-11-02 8:25 ` [LTP] " linuxtestproject.agent
@ 2026-11-12 13:05 ` Cyril Hrubis
1 sibling, 0 replies; 7+ messages in thread
From: Cyril Hrubis @ 2026-11-12 13:05 UTC (permalink / raw)
To: Wei Gao; +Cc: ltp
Hi!
> Calling tst_find_free_loopdev() followed by tst_attach_device() has
> a race window where another process can claim the loop device before
> LOOP_SET_FD is issued, causing attachment to fail with EBUSY.
>
> Introduce tst_attach_loopdev() which finds an unused loop device and
> attaches a backing file to it, retrying the lookup with exponential
> backoff if the candidate device is claimed concurrently (EBUSY). In
> addition, only populate the caller's device path buffer on successful
> attachment, and clear it on failure to prevent accidental cleanup of
> unattached devices.
>
> Update tst_acquire_loop_device() to use tst_attach_loopdev().
>
> Suggested-by: Cyril Hrubis <chrubis@suse.cz>
> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
> v17->v18:
> - New patch in v18.
>
> include/tst_device.h | 13 +++++++
> lib/tst_device.c | 87 ++++++++++++++++++++++++++++++++++++++++----
> 2 files changed, 92 insertions(+), 8 deletions(-)
>
> diff --git a/include/tst_device.h b/include/tst_device.h
> index 0989e4501..2e7dadd59 100644
> --- a/include/tst_device.h
> +++ b/include/tst_device.h
> @@ -110,6 +110,19 @@ int tst_find_free_loopdev(char *path, size_t path_len);
> */
> int tst_attach_device(const char *dev_path, const char *file_path);
>
> +/**
> + * tst_attach_loopdev() - Find a free loop device and attach a file to it.
> + * @dev_path: If non-NULL, buffer filled with the attached loop device path.
> + * @dev_path_len: Size of @dev_path buffer.
> + * @file_path: Path to a file (e.g. disk.img).
> + *
> + * Finds an unused loop device and attaches @file_path to it, automatically
> + * retrying the lookup if the chosen device is claimed concurrently (EBUSY).
> + *
> + * Return: Zero on success, non-zero otherwise.
> + */
> +int tst_attach_loopdev(char *dev_path, size_t dev_path_len, const char *file_path);
> +
> /**
> * tst_get_device_size() - Get device size in megabytes.
> * @dev_path: Device path.
> diff --git a/lib/tst_device.c b/lib/tst_device.c
> index 00304711b..199c14059 100644
> --- a/lib/tst_device.c
> +++ b/lib/tst_device.c
> @@ -156,7 +156,7 @@ int tst_find_free_loopdev(char *path, size_t path_len)
> return -1;
> }
>
> -int tst_attach_device(const char *dev, const char *file)
> +static int attach_device(const char *dev, const char *file, int warn_on_ebusy)
> {
> int dev_fd, file_fd;
> struct loop_info loopinfo;
> @@ -168,7 +168,8 @@ int tst_attach_device(const char *dev, const char *file)
>
> dev_fd = TST_RETRY_FN_EXP_BACKOFF(open(dev, O_RDWR), TST_RETVAL_GE0, 1);
> if (dev_fd < 0) {
> - tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
> + if (warn_on_ebusy || errno != EBUSY)
> + tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
> return 1;
> }
>
> @@ -180,10 +181,16 @@ int tst_attach_device(const char *dev, const char *file)
> }
>
> if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
> + int err = errno;
> +
> close(dev_fd);
> close(file_fd);
> - tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
> - dev, file);
> + errno = err;
> +
> + if (warn_on_ebusy || errno != EBUSY) {
> + tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
> + dev, file);
> + }
> return 1;
> }
>
> @@ -207,6 +214,73 @@ int tst_attach_device(const char *dev, const char *file)
> return 0;
> }
>
> +int tst_attach_device(const char *dev, const char *file)
> +{
> + return attach_device(dev, file, 1);
> +}
> +
> +static int attach_loopdev(char *dev, size_t dev_len, const char *file)
> +{
> + if (tst_find_free_loopdev(dev, dev_len) == -1)
> + return -1;
> +
> + if (attach_device(dev, file, 0) == 0)
> + return 0;
> +
> + if (errno == EBUSY)
> + return 1;
> +
> + return -1;
> +}
> +
> +int tst_attach_loopdev(char *dev_path, size_t dev_path_len, const char *file)
> +{
> + char dev[PATH_MAX];
> + unsigned int delay = 1;
> + unsigned int max_delay = tst_multiply_timeout(1000000);
> + int ret, fd;
> +
> + if (strlen(file) >= LO_NAME_SIZE) {
> + tst_brkm(TBROK, NULL, "Device name can't be longer than %u chars",
> + LO_NAME_SIZE);
> + }
> +
> + fd = open(file, O_RDWR);
> + if (fd < 0) {
> + tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", file);
> + goto fail;
> + }
> + close(fd);
I would drop this part, it's making the API ugly. The attach loopdev
should ideally just take the file name as an parameter and return newly
allocated string with the path to the device it was attached to or NULL
in a case of a failure so that we can call it as:
dev = tst_attach_loopdev(file);
if (!dev)
tst_brk();
Or even have:
dev = SAFE_ATTACH_LOOPDEV(file);
> + for (;;) {
> + ret = attach_loopdev(dev, sizeof(dev), file);
> + if (ret <= 0)
> + break;
> +
> + if (delay < max_delay) {
> + usleep(delay);
> + delay *= 2;
> + } else {
> + ret = -1;
> + break;
> + }
> + }
> +
> + if (ret == 0) {
> + if (dev_path && dev_path_len) {
> + strncpy(dev_path, dev, dev_path_len);
> + dev_path[dev_path_len - 1] = '\0';
> + }
> + return 0;
> + }
> +
> +fail:
> + if (dev_path && dev_path_len)
> + dev_path[0] = '\0';
> +
> + return 1;
> +}
> +
> uint64_t tst_get_device_size(const char *dev_path)
> {
> int fd;
> @@ -390,10 +464,7 @@ const char *tst_acquire_loop_device(unsigned int size, const char *filename)
> return NULL;
> }
>
> - if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
> - return NULL;
> -
> - if (tst_attach_device(dev_path, filename))
> + if (tst_attach_loopdev(dev_path, sizeof(dev_path), filename))
> return NULL;
>
> return dev_path;
> --
> 2.55.0
>
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 7+ messages in thread
* [LTP] [PATCH v18 2/3] newlib_tests: Add tst_attach_loopdev test
2026-11-02 2:22 [LTP] [PATCH v18 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2026-11-02 2:22 ` [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
@ 2026-11-02 2:22 ` Wei Gao via ltp
2026-11-02 2:22 ` [LTP] [PATCH v18 3/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2 siblings, 0 replies; 7+ messages in thread
From: Wei Gao via ltp @ 2026-11-02 2:22 UTC (permalink / raw)
To: ltp
Add a unit test in lib/newlib_tests/ to verify the newly added
tst_attach_loopdev() function.
The test covers:
- Concurrent allocation of multiple loop devices without collisions.
- Parallel worker stress testing to exercise the exponential backoff
retry loop upon transient EBUSY conditions.
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Wei Gao <wegao@suse.com>
---
v17->v18:
- New patch in v18.
lib/newlib_tests/.gitignore | 1 +
lib/newlib_tests/runtest.sh | 1 +
lib/newlib_tests/tst_attach_loopdev.c | 133 ++++++++++++++++++++++++++
3 files changed, 135 insertions(+)
create mode 100644 lib/newlib_tests/tst_attach_loopdev.c
diff --git a/lib/newlib_tests/.gitignore b/lib/newlib_tests/.gitignore
index 3a6e1643c..2d4b3d43f 100644
--- a/lib/newlib_tests/.gitignore
+++ b/lib/newlib_tests/.gitignore
@@ -11,6 +11,7 @@ test11
test13
test14
test15
+tst_attach_loopdev
tst_capability01
tst_capability02
tst_cgroup01
diff --git a/lib/newlib_tests/runtest.sh b/lib/newlib_tests/runtest.sh
index 71808ef8b..18b64c07a 100755
--- a/lib/newlib_tests/runtest.sh
+++ b/lib/newlib_tests/runtest.sh
@@ -16,6 +16,7 @@ test_zero_hugepage.sh
test0[579]
test1[59]
test2[04]
+tst_attach_loopdev
tst_bool_expr
tst_capability02
tst_checkpoint
diff --git a/lib/newlib_tests/tst_attach_loopdev.c b/lib/newlib_tests/tst_attach_loopdev.c
new file mode 100644
index 000000000..dbc711cb9
--- /dev/null
+++ b/lib/newlib_tests/tst_attach_loopdev.c
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 SUSE LLC <wegao@suse.com>
+ */
+
+/*\
+ * Unit test for tst_attach_loopdev().
+ *
+ * Verifies that:
+ * - Multiple backing files can be attached simultaneously to distinct
+ * loop devices without collision.
+ * - Concurrent workers attaching and detaching loop devices in parallel
+ * succeed without unhandled EBUSY race conditions.
+ */
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "tst_test.h"
+
+#define BACKING_FILE1 "test_loop1.img"
+#define BACKING_FILE2 "test_loop2.img"
+
+#define DEV_SIZE_MB 1
+#define NUM_WORKERS 4
+#define WORKER_LOOPS 5
+
+static char dev1[PATH_MAX];
+static char dev2[PATH_MAX];
+
+static void cleanup(void)
+{
+ if (dev1[0])
+ tst_detach_device(dev1);
+
+ if (dev2[0])
+ tst_detach_device(dev2);
+}
+
+static void setup(void)
+{
+ if (tst_prealloc_file(BACKING_FILE1, 1024 * 1024, DEV_SIZE_MB))
+ tst_brk(TBROK, "Failed to create %s", BACKING_FILE1);
+
+ if (tst_prealloc_file(BACKING_FILE2, 1024 * 1024, DEV_SIZE_MB))
+ tst_brk(TBROK, "Failed to create %s", BACKING_FILE2);
+}
+
+static void test_multiple_devices(void)
+{
+ if (tst_attach_loopdev(dev1, sizeof(dev1), BACKING_FILE1)) {
+ tst_res(TFAIL, "Failed to attach %s to dev1", BACKING_FILE1);
+ return;
+ }
+
+ if (tst_attach_loopdev(dev2, sizeof(dev2), BACKING_FILE2)) {
+ tst_res(TFAIL, "Failed to attach %s to dev2", BACKING_FILE2);
+ goto detach1;
+ }
+
+ if (!strcmp(dev1, dev2))
+ tst_res(TFAIL, "dev1 and dev2 assigned identical device: '%s'", dev1);
+ else
+ tst_res(TPASS, "Assigned distinct loop devices: '%s' and '%s'", dev1, dev2);
+
+ if (tst_detach_device(dev2)) {
+ tst_res(TFAIL, "Failed to detach dev2 '%s'", dev2);
+ } else {
+ tst_res(TPASS, "Detached dev2 '%s' successfully", dev2);
+ dev2[0] = '\0';
+ }
+
+detach1:
+ if (tst_detach_device(dev1)) {
+ tst_res(TFAIL, "Failed to detach dev1 '%s'", dev1);
+ } else {
+ tst_res(TPASS, "Detached dev1 '%s' successfully", dev1);
+ dev1[0] = '\0';
+ }
+}
+
+static void test_concurrent_contention(void)
+{
+ pid_t pids[NUM_WORKERS];
+ int i;
+
+ for (i = 0; i < NUM_WORKERS; i++) {
+ pids[i] = SAFE_FORK();
+ if (!pids[i]) {
+ char file[32];
+ char dev[PATH_MAX];
+ int j;
+
+ snprintf(file, sizeof(file), "worker_%d.img", i);
+ if (tst_prealloc_file(file, 1024 * 1024, DEV_SIZE_MB))
+ tst_brk(TBROK, "Worker %d failed to prealloc %s", i, file);
+
+ for (j = 0; j < WORKER_LOOPS; j++) {
+ dev[0] = '\0';
+ if (tst_attach_loopdev(dev, sizeof(dev), file))
+ tst_brk(TBROK, "Worker %d loop %d failed attach", i, j);
+
+ if (tst_detach_device(dev))
+ tst_brk(TBROK, "Worker %d loop %d failed detach", i, j);
+ }
+
+ exit(0);
+ }
+ }
+
+ for (i = 0; i < NUM_WORKERS; i++)
+ SAFE_WAITPID(pids[i], NULL, 0);
+
+ tst_res(TPASS, "All %d workers attached and detached loop devices concurrently",
+ NUM_WORKERS);
+}
+
+static void run(void)
+{
+ test_multiple_devices();
+ test_concurrent_contention();
+}
+
+static struct tst_test test = {
+ .needs_root = 1,
+ .needs_tmpdir = 1,
+ .forks_child = 1,
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run,
+};
--
2.55.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 7+ messages in thread* [LTP] [PATCH v18 3/3] fsconfig04: Check FSCONFIG_SET_PATH
2026-11-02 2:22 [LTP] [PATCH v18 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2026-11-02 2:22 ` [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
2026-11-02 2:22 ` [LTP] [PATCH v18 2/3] newlib_tests: Add tst_attach_loopdev test Wei Gao via ltp
@ 2026-11-02 2:22 ` Wei Gao via ltp
2 siblings, 0 replies; 7+ messages in thread
From: Wei Gao via ltp @ 2026-11-02 2:22 UTC (permalink / raw)
To: ltp
The fsconfig01.c does not test if FSCONFIG_SET_PATH has any effect;
most of the calls there just set a dummy "sync" parameter. This test
case aims to verify if the FSCONFIG_SET_PATH operation can be used
to dynamically change the external journal device of an ext3 or ext4
filesystem.
Closes: https://github.com/linux-test-project/ltp/issues/1169
Signed-off-by: Wei Gao <wegao@suse.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
---
v17->v18:
- Use tst_attach_loopdev() to set up loop devices.
runtest/syscalls | 1 +
testcases/kernel/syscalls/fsconfig/.gitignore | 1 +
.../kernel/syscalls/fsconfig/fsconfig04.c | 166 ++++++++++++++++++
3 files changed, 168 insertions(+)
create mode 100644 testcases/kernel/syscalls/fsconfig/fsconfig04.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 2bdb4a9a2..1903f1ac6 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -452,6 +452,7 @@ fremovexattr02 fremovexattr02
fsconfig01 fsconfig01
fsconfig02 fsconfig02
fsconfig03 fsconfig03
+fsconfig04 fsconfig04
fsmount01 fsmount01
fsmount02 fsmount02
diff --git a/testcases/kernel/syscalls/fsconfig/.gitignore b/testcases/kernel/syscalls/fsconfig/.gitignore
index cfedae5f7..bd3754c34 100644
--- a/testcases/kernel/syscalls/fsconfig/.gitignore
+++ b/testcases/kernel/syscalls/fsconfig/.gitignore
@@ -1,3 +1,4 @@
/fsconfig01
/fsconfig02
/fsconfig03
+/fsconfig04
diff --git a/testcases/kernel/syscalls/fsconfig/fsconfig04.c b/testcases/kernel/syscalls/fsconfig/fsconfig04.c
new file mode 100644
index 000000000..11984d40e
--- /dev/null
+++ b/testcases/kernel/syscalls/fsconfig/fsconfig04.c
@@ -0,0 +1,166 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Wei Gao <wegao@suse.com>
+ */
+
+/*\
+ * This test aims to validate :manpage:`fsconfig(2)` with the
+ * ``FSCONFIG_SET_PATH`` operation in dynamically altering the external
+ * journal device of an ext3 or ext4 filesystem.
+ *
+ * Root privileges are required because the test creates and formats loop
+ * devices, and configures filesystems.
+ *
+ * [Algorithm]
+ *
+ * - Acquire three loop devices (``tst_device->dev`` from framework, ``dev1``, ``dev2``).
+ * - Format ``dev1`` and ``dev2`` as external journal devices with the same UUID
+ * (``-O journal_dev -U <uuid>``).
+ * - Format ``tst_device->dev`` three times to cycle journal associations:
+ * ``dev1`` -> ``dev2`` -> ``dev1``, so both journal superblocks are consistent.
+ * - Open ``tst_device->dev`` via :manpage:`fsopen(2)` and set the source and journal_path using
+ * :manpage:`fsconfig(2)` (``FSCONFIG_SET_STRING``) and
+ * :manpage:`fsconfig(2)` (``FSCONFIG_SET_PATH``).
+ * - Apply ``FSCONFIG_CMD_CREATE`` and verify with ``tune2fs`` that ``tst_device->dev``'s
+ * journal device is now ``dev2``.
+ *
+ * Implementation notes:
+ *
+ * - To avoid ``journal UUID does not match`` error when switching external
+ * journal device, we have to assign the same UUID to ``dev1``/``dev2``.
+ * - Before the :manpage:`fsconfig(2)` test, we have to format ``tst_device->dev`` associating to
+ * ``dev1`` -> ``dev2`` -> ``dev1``. This ensures that both ``dev1``/``dev2`` superblocks contain
+ * correct content. Otherwise, you will encounter errors such as
+ * ``EXT4-fs (loop0): External journal has more than one user (unsupported)``
+ * when switching the external journal device using :manpage:`fsconfig(2)`.
+ */
+
+#include <stdbool.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
+#include "lapi/fsmount.h"
+
+#define LOOP_DEV_SIZE 10
+#define UUID "d73c9e5e-97e4-4a9c-b17e-75a931b02660"
+
+static int fd = -1;
+static char dev1[PATH_MAX];
+static char dev2[PATH_MAX];
+
+static const char *const mkfs_opts_set_UUID[] = {"-F", "-U", UUID, "-O", "journal_dev", NULL};
+
+static void cleanup(void)
+{
+ if (fd != -1)
+ SAFE_CLOSE(fd);
+
+ if (dev1[0])
+ tst_detach_device(dev1);
+
+ if (dev2[0])
+ tst_detach_device(dev2);
+}
+
+static void create_and_attach_loopdev(const char *filename, char *dev_path, size_t dev_path_len)
+{
+ if (tst_prealloc_file(filename, 1024 * 1024, LOOP_DEV_SIZE))
+ tst_brk(TBROK, "Failed to create %s", filename);
+
+ if (tst_attach_loopdev(dev_path, dev_path_len, filename))
+ tst_brk(TBROK, "Failed to attach loop device for %s", filename);
+}
+
+static void format_journal_device(const char *dev, const char *journal)
+{
+ char journal_opt[PATH_MAX + 16];
+ const char *const opts[] = {"-F", "-J", journal_opt, NULL};
+
+ snprintf(journal_opt, sizeof(journal_opt), "device=%s", journal);
+ SAFE_MKFS(dev, tst_device->fs_type, opts, NULL);
+}
+
+static void setup(void)
+{
+ fsopen_supported_by_kernel();
+
+ create_and_attach_loopdev("dev1_file", dev1, sizeof(dev1));
+ create_and_attach_loopdev("dev2_file", dev2, sizeof(dev2));
+}
+
+static void run(void)
+{
+ /* Reset dev1, dev2 and tst_device->dev superblocks before each test run iteration */
+ SAFE_MKFS(dev1, tst_device->fs_type, mkfs_opts_set_UUID, NULL);
+ SAFE_MKFS(dev2, tst_device->fs_type, mkfs_opts_set_UUID, NULL);
+ format_journal_device(tst_device->dev, dev1);
+ format_journal_device(tst_device->dev, dev2);
+ format_journal_device(tst_device->dev, dev1);
+
+ TEST(fd = fsopen(tst_device->fs_type, 0));
+ if (fd == -1)
+ tst_brk(TBROK | TTERRNO, "fsopen() failed");
+
+ TEST(fsconfig(fd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
+ if (TST_RET == -1)
+ tst_brk(TBROK | TTERRNO, "fsconfig(FSCONFIG_SET_STRING) failed");
+
+ TEST(fsconfig(fd, FSCONFIG_SET_PATH, "journal_path", dev2, AT_FDCWD));
+ if (TST_RET == -1) {
+ if (TST_ERR == EOPNOTSUPP)
+ tst_brk(TCONF, "fsconfig(FSCONFIG_SET_PATH) not supported");
+ else
+ tst_brk(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_PATH) failed");
+ }
+
+ TEST(fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
+ if (TST_RET == -1)
+ tst_brk(TFAIL | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
+
+ SAFE_CLOSE(fd);
+
+ char path[PATH_MAX + 32];
+ char device_str[NAME_MAX];
+ bool found = false;
+ struct stat st;
+
+ SAFE_STAT(dev2, &st);
+ unsigned int device_num = (minor(st.st_rdev) & 0xff) | (major(st.st_rdev) << 8) |
+ ((minor(st.st_rdev) & ~0xff) << 12);
+ snprintf(device_str, sizeof(device_str), "0x%04x", device_num);
+
+ char line[PATH_MAX];
+ FILE *tune2fs;
+
+ snprintf(path, sizeof(path), "tune2fs -l %s 2>&1", tst_device->dev);
+ tune2fs = SAFE_POPEN(path, "r");
+
+ while (fgets(line, PATH_MAX, tune2fs)) {
+ if (*line && strstr(line, "Journal device:") && strstr(line, device_str)) {
+ found = true;
+ break;
+ }
+ }
+
+ TST_EXP_EXPR(found, "Device found in journal");
+
+ pclose(tune2fs);
+}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .cleanup = cleanup,
+ .needs_root = 1,
+ .needs_device = 1,
+ .needs_cmds = (struct tst_cmd []) {
+ {.cmd = "tune2fs"},
+ {}
+ },
+ .filesystems = (struct tst_fs []) {
+ {.type = "ext3"},
+ {.type = "ext4"},
+ {}
+ },
+};
--
2.55.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 7+ messages in thread