Skip to contentSkip to conversation

Allow detach file from loop device with opening dev_fd

PATCH New Yang Xu 2 participants 4 messages Jul 23, 2020 Library code

lore
Share & tools
lore

Yang Xu

author
#0 lore raw

PATCH 1/2 Allow detach file from loop device with opening dev_fd

Before this api, it will report the following error when using tst_attach_device with opening device fd because the device was opened twice at this point.

tst_device.c:223: WARN: ioctl(/dev/loop0, LOOP_CLR_FD, 0) no ENXIO for

Quoted 1 line
Quoted 1 line

too long

Add a tst_detach_device_by_fd() and change the library so that tst_detach_device() opens/close the dev_path and calls tst_detach_device_by_fd() internally.

---
 include/tst_device.h | 11 +++++++++++
 lib/tst_device.c     | 31 ++++++++++++++++++-------------
 2 files changed, 29 insertions(+), 13 deletions(-)
include/tst_device.h +11 -0
index 6a1fc5186..59cc5b96c 100644
@@ -57,11 +57,22 @@ int tst_find_free_loopdev(const char *path, size_t path_len);
  */
 int tst_attach_device(const char *dev_path, const char *file_path);
 
+/*
+ * Detaches a file from a loop device fd.
+ *
+ * @dev_path Path to the loop device e.g. /dev/loop0
+ * @dev_fd a open fd for the loop device
+ * @return Zero on succes, non-zero otherwise.
+ * */
+
+int tst_detach_device_by_fd(const char *dev_path, int dev_fd);
+
 /*
  * Detaches a file from a loop device.
  *
  * @dev_path Path to the loop device e.g. /dev/loop0
  * @return Zero on succes, non-zero otherwise.
+ * In internal, call tst_detach_device_by_fd api.
  */
 int tst_detach_device(const char *dev_path);
 
lib/tst_device.c +18 -13
index 131a223be..8d8bc5b40 100644
@@ -187,43 +187,48 @@ int tst_attach_device(const char *dev, const char *file)
 	return 0;
 }
 
-int tst_detach_device(const char *dev)
+int tst_detach_device_by_fd(const char *dev, int dev_fd)
 {
-	int dev_fd, ret, i;
-
-	dev_fd = open(dev, O_RDONLY);
-	if (dev_fd < 0) {
-		tst_resm(TWARN | TERRNO, "open(%s) failed", dev);
-		return 1;
-	}
+	int ret, i;
 
 	/* keep trying to clear LOOPDEV until we get ENXIO, a quick succession
 	 * of attach/detach might not give udev enough time to complete */
 	for (i = 0; i < 40; i++) {
 		ret = ioctl(dev_fd, LOOP_CLR_FD, 0);
 
-		if (ret && (errno == ENXIO)) {
-			close(dev_fd);
+		if (ret && (errno == ENXIO))
 			return 0;
-		}
 
 		if (ret && (errno != EBUSY)) {
 			tst_resm(TWARN,
 				 "ioctl(%s, LOOP_CLR_FD, 0) unexpectedly failed with: %s",
 				 dev, tst_strerrno(errno));
-			close(dev_fd);
 			return 1;
 		}
 
 		usleep(50000);
 	}
 
-	close(dev_fd);
 	tst_resm(TWARN,
 		"ioctl(%s, LOOP_CLR_FD, 0) no ENXIO for too long", dev);
 	return 1;
 }
 
+int tst_detach_device(const char *dev)
+{
+	int dev_fd, ret;
+
+	dev_fd = open(dev, O_RDONLY);
+	if (dev_fd < 0) {
+		tst_resm(TWARN | TERRNO, "open(%s) failed", dev);
+		return 1;
+	}
+
+	ret = tst_detach_device_by_fd(dev, dev_fd);
+	close(dev_fd);
+	return ret;
+}
+
 int tst_dev_sync(int fd)
 {
 	return syscall(__NR_syncfs, fd);
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 2/2 syscalls/ioctl: Use tst_detach_device_by_fd api

---
 testcases/kernel/syscalls/ioctl/ioctl09.c      | 5 ++---
 testcases/kernel/syscalls/ioctl/ioctl_loop01.c | 6 ++----
 testcases/kernel/syscalls/ioctl/ioctl_loop02.c | 5 +----
 testcases/kernel/syscalls/ioctl/ioctl_loop04.c | 5 ++---
 4 files changed, 7 insertions(+), 14 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl09.c +2 -3
index 6a7f4042b..151618df4 100644
@@ -70,7 +70,6 @@ static void verify_ioctl(void)
 	tst_attach_device(dev_path, "test.img");
 	attach_flag = 1;
 
-	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
 	loopinfo.lo_flags =  LO_FLAGS_PARTSCAN;
 	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
 	check_partition(1, true);
@@ -81,8 +80,7 @@ static void verify_ioctl(void)
 	check_partition(1, true);
 	check_partition(2, true);
 
-	SAFE_CLOSE(dev_fd);
-	tst_detach_device(dev_path);
+	tst_detach_device_by_fd(dev_path, dev_fd);
 	attach_flag = 0;
 }
 
@@ -92,6 +90,7 @@ static void setup(void)
 	if (dev_num < 0)
 		tst_brk(TBROK, "Failed to find free loop device");
 	tst_prealloc_file("test.img", 1024 * 1024, 20);
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +2 -4
index f334b5eb2..845a1399b 100644
@@ -86,15 +86,12 @@ static void verify_ioctl_loop(void)
 	TST_ASSERT_INT(autoclear_path, 0);
 	TST_ASSERT_STR(backing_path, backing_file_path);
 
-	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
-
 	check_loop_value(SET_FLAGS, GET_FLAGS, 1);
 
 	tst_res(TINFO, "Test flag can be clear");
 	check_loop_value(0, LO_FLAGS_PARTSCAN, 0);
 
-	SAFE_CLOSE(dev_fd);
-	tst_detach_device(dev_path);
+	tst_detach_device_by_fd(dev_path, dev_fd);
 	attach_flag = 0;
 }
 
@@ -129,6 +126,7 @@ static void setup(void)
 	sprintf(sys_loop_partpath, "/sys/block/loop%d/loop%dp1", dev_num, dev_num);
 	sprintf(backing_file_path, "%s/test.img", tst_get_tmpdir());
 	sprintf(loop_partpath, "%sp1", dev_path);
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/ioctl/ioctl_loop02.c +1 -4
index 3a03d052a..ac6184216 100644
@@ -50,7 +50,6 @@ static void verify_ioctl_loop(unsigned int n)
 
 	tst_res(TINFO, "%s", tc->message);
 	file_fd = SAFE_OPEN("test.img", tc->mode);
-	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
 
 	if (tc->ioctl == LOOP_SET_FD) {
 		SAFE_IOCTL(dev_fd, LOOP_SET_FD, file_fd);
@@ -97,9 +96,8 @@ static void verify_ioctl_loop(unsigned int n)
 		tst_res(TFAIL, "LOOP_CHANGE_FD succeeded");
 	}
 
-	SAFE_CLOSE(dev_fd);
 	SAFE_CLOSE(file_fd);
-	tst_detach_device(dev_path);
+	tst_detach_device_by_fd(dev_path, dev_fd);
 	attach_flag = 0;
 }
 
@@ -136,7 +134,6 @@ static void setup(void)
 		loop_configure_sup = 0;
 	}
 	loopconfig.info.lo_flags = LO_FLAGS_READ_ONLY;
-	SAFE_CLOSE(dev_fd);
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/ioctl/ioctl_loop04.c +2 -3
index b882728fc..af3e90ddc 100644
@@ -33,7 +33,6 @@ static void verify_ioctl_loop(void)
 	attach_flag = 1;
 
 	TST_ASSERT_INT(sys_loop_sizepath, OLD_SIZE/512);
-	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
 	file_fd = SAFE_OPEN("test.img", O_RDWR);
 	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
 
@@ -55,8 +54,7 @@ static void verify_ioctl_loop(void)
 	TST_ASSERT_INT(sys_loop_sizepath, NEW_SIZE/512);
 
 	SAFE_CLOSE(file_fd);
-	SAFE_CLOSE(dev_fd);
-	tst_detach_device(dev_path);
+	tst_detach_device_by_fd(dev_path, dev_fd);
 	unlink("test.img");
 	attach_flag = 0;
 }
@@ -70,6 +68,7 @@ static void setup(void)
 	wrbuf = SAFE_MALLOC(OLD_SIZE);
 	memset(wrbuf, 'x', OLD_SIZE);
 	sprintf(sys_loop_sizepath, "/sys/block/loop%d/size", dev_num);
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
 }
 
 static void cleanup(void)
Signature · 1 line
2.23.0
#2 lore raw

PATCH 2/2 syscalls/ioctl: Use tst_detach_device_by_fd api

Hi!
Pushed with fix for ioctl_loop04, thanks.

I've fixed the -i 2 case for ioctl_loop04 with:

testcases/kernel/syscalls/ioctl/ioctl_loop04.c +2 -0
index af3e90ddc..b4ab44a74 100644
@@ -42,6 +42,8 @@ static void verify_ioctl_loop(void)
 	SAFE_TRUNCATE("test.img", NEW_SIZE);
 	SAFE_IOCTL(dev_fd, LOOP_SET_CAPACITY);
 
+	SAFE_LSEEK(dev_fd, 0, SEEK_SET);
+
 	/*check that we can't write data beyond 5K into loop device*/
 	TEST(write(dev_fd, wrbuf, OLD_SIZE));
 	if (TST_RET == NEW_SIZE) {
Signature · 2 lines
Cyril Hrubis
[email protected]