Skip to contentSkip to conversation

fsconfig04: Check FSCONFIG_SET_PATH

PATCH Superseded Wei Gao via ltp 3 participants 6 messages Nov 2 – Nov 12 Library code

v18 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v18 cover fsconfig04: Check FSCONFIG_SET_PATH

v17->v18:
- lib: Add tst_attach_loopdev() to find an unused loop device and attach a backing file with exponential backoff retry on EBUSY, preventing race conditions between lookup and attachment.
- newlib_tests: Add unit test verifying tst_attach_loopdev().
- fsconfig04: Update create_and_attach_loopdev() to use tst_attach_loopdev().

Wei Gao (3):
lib: Add tst_attach_loopdev()
newlib_tests: Add tst_attach_loopdev test
fsconfig04: Check FSCONFIG_SET_PATH

 include/tst_device.h                          |  13 ++
 lib/newlib_tests/.gitignore                   |   1 +
 lib/newlib_tests/runtest.sh                   |   1 +
 lib/newlib_tests/tst_attach_loopdev.c         | 133 ++++++++++++++
 lib/tst_device.c                              |  87 ++++++++-
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/fsconfig/.gitignore |   1 +
 .../kernel/syscalls/fsconfig/fsconfig04.c     | 166 ++++++++++++++++++
 8 files changed, 395 insertions(+), 8 deletions(-)
 create mode 100644 lib/newlib_tests/tst_attach_loopdev.c
 create mode 100644 testcases/kernel/syscalls/fsconfig/fsconfig04.c
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v18 1/3 lib: Add tst_attach_loopdev()

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().

---
v17->v18:
- New patch in v18.

 include/tst_device.h | 13 +++++++
 lib/tst_device.c     | 87 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 92 insertions(+), 8 deletions(-)
include/tst_device.h +13 -0
index 0989e4501..2e7dadd59 100644
@@ -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.
lib/tst_device.c +79 -8

119 lines not on this page · raw message

Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] lib: Add tst_attach_loopdev()

Hi Wei,

Quoted 1 line · Wei Gao

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH v18 1/3] lib: Add tst_attach_loopdev()

In reply to Wei Gao via ltp

Hi!

Quoted 120 lines

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().

---
v17->v18:
- New patch in v18.

 include/tst_device.h | 13 +++++++
 lib/tst_device.c     | 87 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 92 insertions(+), 8 deletions(-)
include/tst_device.h +13 -0
index 0989e4501..2e7dadd59 100644
@@ -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.
lib/tst_device.c +38 -4
index 00304711b..199c14059 100644
@@ -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) {

lib/tst_device.c int tst_attach_device(const char *dev, const char *file) · line 253

Quoted 10 lines
lib/tst_device.c +10 -0
+		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);
Quoted 47 lines
lib/tst_device.c +30 -4
+	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;
Signature · 1 line
2.55.0
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v18 2/3 newlib_tests: Add tst_attach_loopdev test

In reply to Wei Gao via 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.

---
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
lib/newlib_tests/.gitignore +1 -0
index 3a6e1643c..2d4b3d43f 100644
@@ -11,6 +11,7 @@ test11
 test13
 test14
 test15
+tst_attach_loopdev
 tst_capability01
 tst_capability02
 tst_cgroup01
lib/newlib_tests/runtest.sh +1 -0
index 71808ef8b..18b64c07a 100755
@@ -16,6 +16,7 @@ test_zero_hugepage.sh
 test0[579]
 test1[59]
 test2[04]
+tst_attach_loopdev
 tst_bool_expr
 tst_capability02
 tst_checkpoint
lib/newlib_tests/tst_attach_loopdev.c new file +133 -0

133 lines not on this page · raw message

Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v18 3/3 fsconfig04: Check FSCONFIG_SET_PATH

In reply to Wei Gao via 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.

---
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
runtest/syscalls +1 -0
index 2bdb4a9a2..1903f1ac6 100644
@@ -452,6 +452,7 @@ fremovexattr02 fremovexattr02
 fsconfig01 fsconfig01
 fsconfig02 fsconfig02
 fsconfig03 fsconfig03
+fsconfig04 fsconfig04
 
 fsmount01 fsmount01
 fsmount02 fsmount02
testcases/kernel/syscalls/fsconfig/.gitignore +1 -0
index cfedae5f7..bd3754c34 100644
@@ -1,3 +1,4 @@
 /fsconfig01
 /fsconfig02
 /fsconfig03
+/fsconfig04
testcases/kernel/syscalls/fsconfig/fsconfig04.c new file +166 -0

166 lines not on this page · raw message

Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp