* [LTP] [PATCH v19 1/3] lib: Add tst_attach_loopdev()
2026-11-16 8:56 [LTP] [PATCH v19 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
@ 2026-11-16 8:56 ` Wei Gao via ltp
2026-11-16 12:16 ` [LTP] " linuxtestproject.agent
2026-11-16 8:56 ` [LTP] [PATCH v19 2/3] newlib_tests: Add tst_attach_loopdev test Wei Gao via ltp
2026-11-16 8:56 ` [LTP] [PATCH v19 3/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2 siblings, 1 reply; 6+ messages in thread
From: Wei Gao via ltp @ 2026-11-16 8:56 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).
The function returns a newly allocated string containing the device
path on success, or NULL on failure. Also introduce the
SAFE_ATTACH_LOOPDEV() helper macro.
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>
---
include/tst_device.h | 26 ++++++++++++++
lib/tst_device.c | 82 ++++++++++++++++++++++++++++++++++++++++----
2 files changed, 101 insertions(+), 7 deletions(-)
diff --git a/include/tst_device.h b/include/tst_device.h
index 0989e4501..4e2e14b89 100644
--- a/include/tst_device.h
+++ b/include/tst_device.h
@@ -110,6 +110,32 @@ 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.
+ * @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: Newly allocated string containing the device path on success,
+ * or NULL on failure. Caller is responsible for free()-ing the string.
+ */
+char *tst_attach_loopdev(const char *file_path);
+
+/**
+ * safe_attach_loopdev() - Safe wrapper for tst_attach_loopdev().
+ * @file: Source file where called.
+ * @lineno: Line number where called.
+ * @file_path: Path to a file (e.g. disk.img).
+ *
+ * Return: Newly allocated string containing the device path. Calls tst_brk()
+ * on failure. Caller is responsible for free()-ing the string.
+ */
+char *safe_attach_loopdev(const char *file, const int lineno, const char *file_path);
+
+#define SAFE_ATTACH_LOOPDEV(file_path) \
+ safe_attach_loopdev(__FILE__, __LINE__, (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 bf77c9ded..c751e576f 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,64 @@ 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;
+}
+
+char *tst_attach_loopdev(const char *file)
+{
+ char dev[PATH_MAX];
+ unsigned int delay = 1;
+ unsigned int max_delay = tst_multiply_timeout(1000000);
+ int ret;
+
+ 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)
+ return strdup(dev);
+
+ return NULL;
+}
+
+char *safe_attach_loopdev(const char *file, const int lineno, const char *file_path)
+{
+ char *dev = tst_attach_loopdev(file_path);
+
+ if (!dev) {
+ tst_brkm_(file, lineno, TBROK, NULL,
+ "Failed to attach %s to loop device", file_path);
+ }
+
+ return dev;
+}
+
uint64_t tst_get_device_size(const char *dev_path)
{
int fd;
@@ -386,17 +451,20 @@ int tst_dev_sync(int fd)
const char *tst_acquire_loop_device(unsigned int size, const char *filename)
{
unsigned int acq_dev_size = size ? size : DEV_SIZE_MB;
+ char *dev;
if (tst_prealloc_file(filename, 1024 * 1024, acq_dev_size)) {
tst_resm(TWARN | TERRNO, "Failed to create %s", filename);
return NULL;
}
- if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
+ dev = tst_attach_loopdev(filename);
+ if (!dev)
return NULL;
- if (tst_attach_device(dev_path, filename))
- return NULL;
+ strncpy(dev_path, dev, sizeof(dev_path) - 1);
+ dev_path[sizeof(dev_path) - 1] = '\0';
+ free(dev);
return dev_path;
}
--
2.55.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 6+ messages in thread* [LTP] [PATCH v19 2/3] newlib_tests: Add tst_attach_loopdev test
2026-11-16 8:56 [LTP] [PATCH v19 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2026-11-16 8:56 ` [LTP] [PATCH v19 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
@ 2026-11-16 8:56 ` Wei Gao via ltp
2026-11-16 8:56 ` [LTP] [PATCH v19 3/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2 siblings, 0 replies; 6+ messages in thread
From: Wei Gao via ltp @ 2026-11-16 8:56 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>
---
lib/newlib_tests/.gitignore | 1 +
lib/newlib_tests/runtest.sh | 1 +
lib/newlib_tests/tst_attach_loopdev.c | 145 ++++++++++++++++++++++++++
3 files changed, 147 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..37bbcc6b9
--- /dev/null
+++ b/lib/newlib_tests/tst_attach_loopdev.c
@@ -0,0 +1,145 @@
+// 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;
+static char *dev2;
+
+static void cleanup(void)
+{
+ if (dev1) {
+ tst_detach_device(dev1);
+ free(dev1);
+ dev1 = NULL;
+ }
+
+ if (dev2) {
+ tst_detach_device(dev2);
+ free(dev2);
+ dev2 = NULL;
+ }
+}
+
+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)
+{
+ dev1 = tst_attach_loopdev(BACKING_FILE1);
+ if (!dev1) {
+ tst_res(TFAIL, "Failed to attach %s to dev1", BACKING_FILE1);
+ return;
+ }
+
+ dev2 = tst_attach_loopdev(BACKING_FILE2);
+ if (!dev2) {
+ 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);
+
+ free(dev2);
+ dev2 = NULL;
+
+detach1:
+ if (tst_detach_device(dev1))
+ tst_res(TFAIL, "Failed to detach dev1 '%s'", dev1);
+ else
+ tst_res(TPASS, "Detached dev1 '%s' successfully", dev1);
+
+ free(dev1);
+ dev1 = NULL;
+}
+
+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;
+ 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 = SAFE_ATTACH_LOOPDEV(file);
+
+ if (tst_detach_device(dev)) {
+ free(dev);
+ tst_brk(TBROK, "Worker %d loop %d failed detach", i, j);
+ }
+
+ free(dev);
+ }
+
+ 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] 6+ messages in thread* [LTP] [PATCH v19 3/3] fsconfig04: Check FSCONFIG_SET_PATH
2026-11-16 8:56 [LTP] [PATCH v19 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2026-11-16 8:56 ` [LTP] [PATCH v19 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
2026-11-16 8:56 ` [LTP] [PATCH v19 2/3] newlib_tests: Add tst_attach_loopdev test Wei Gao via ltp
@ 2026-11-16 8:56 ` Wei Gao via ltp
2 siblings, 0 replies; 6+ messages in thread
From: Wei Gao via ltp @ 2026-11-16 8:56 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>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/fsconfig/.gitignore | 1 +
.../kernel/syscalls/fsconfig/fsconfig04.c | 167 ++++++++++++++++++
3 files changed, 169 insertions(+)
create mode 100644 testcases/kernel/syscalls/fsconfig/fsconfig04.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 7b6a7d376..58d26571d 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..fa6466cff
--- /dev/null
+++ b/testcases/kernel/syscalls/fsconfig/fsconfig04.c
@@ -0,0 +1,167 @@
+// 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;
+static char *dev2;
+
+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) {
+ tst_detach_device(dev1);
+ free(dev1);
+ }
+
+ if (dev2) {
+ tst_detach_device(dev2);
+ free(dev2);
+ }
+}
+
+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();
+
+ if (tst_prealloc_file("dev1_file", 1024 * 1024, LOOP_DEV_SIZE))
+ tst_brk(TBROK, "Failed to create dev1_file");
+
+ if (tst_prealloc_file("dev2_file", 1024 * 1024, LOOP_DEV_SIZE))
+ tst_brk(TBROK, "Failed to create dev2_file");
+
+ dev1 = SAFE_ATTACH_LOOPDEV("dev1_file");
+ dev2 = SAFE_ATTACH_LOOPDEV("dev2_file");
+}
+
+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] 6+ messages in thread