All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v1 1/2] Allow detach file from loop device with opening dev_fd
@ 2020-07-23  8:30 Yang Xu
  2020-07-23  8:30 ` [LTP] [PATCH v1 2/2] syscalls/ioctl: Use tst_detach_device_by_fd api Yang Xu
  2020-07-23  9:25 ` [LTP] [PATCH v1 1/2] Allow detach file from loop device with opening dev_fd Cyril Hrubis
  0 siblings, 2 replies; 4+ messages in thread
From: Yang Xu @ 2020-07-23  8:30 UTC (permalink / raw)
  To: ltp

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

Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 include/tst_device.h | 11 +++++++++++
 lib/tst_device.c     | 31 ++++++++++++++++++-------------
 2 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/include/tst_device.h b/include/tst_device.h
index 6a1fc5186..59cc5b96c 100644
--- a/include/tst_device.h
+++ b/include/tst_device.h
@@ -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);
 
diff --git a/lib/tst_device.c b/lib/tst_device.c
index 131a223be..8d8bc5b40 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -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);
-- 
2.23.0




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [LTP] [PATCH v1 2/2] syscalls/ioctl: Use tst_detach_device_by_fd api
  2020-07-23  8:30 [LTP] [PATCH v1 1/2] Allow detach file from loop device with opening dev_fd Yang Xu
@ 2020-07-23  8:30 ` Yang Xu
  2020-07-23  9:26   ` Cyril Hrubis
  2020-07-23  9:25 ` [LTP] [PATCH v1 1/2] Allow detach file from loop device with opening dev_fd Cyril Hrubis
  1 sibling, 1 reply; 4+ messages in thread
From: Yang Xu @ 2020-07-23  8:30 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 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(-)

diff --git a/testcases/kernel/syscalls/ioctl/ioctl09.c b/testcases/kernel/syscalls/ioctl/ioctl09.c
index 6a7f4042b..151618df4 100644
--- a/testcases/kernel/syscalls/ioctl/ioctl09.c
+++ b/testcases/kernel/syscalls/ioctl/ioctl09.c
@@ -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)
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop01.c b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
index f334b5eb2..845a1399b 100644
--- a/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
@@ -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)
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop02.c b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
index 3a03d052a..ac6184216 100644
--- a/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
@@ -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)
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop04.c b/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
index b882728fc..af3e90ddc 100644
--- a/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
@@ -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)
-- 
2.23.0




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [LTP] [PATCH v1 1/2] Allow detach file from loop device with opening dev_fd
  2020-07-23  8:30 [LTP] [PATCH v1 1/2] Allow detach file from loop device with opening dev_fd Yang Xu
  2020-07-23  8:30 ` [LTP] [PATCH v1 2/2] syscalls/ioctl: Use tst_detach_device_by_fd api Yang Xu
@ 2020-07-23  9:25 ` Cyril Hrubis
  1 sibling, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2020-07-23  9:25 UTC (permalink / raw)
  To: ltp

Hi!
I've added a bit more verbose explanation to the comments in the header
and pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [LTP] [PATCH v1 2/2] syscalls/ioctl: Use tst_detach_device_by_fd api
  2020-07-23  8:30 ` [LTP] [PATCH v1 2/2] syscalls/ioctl: Use tst_detach_device_by_fd api Yang Xu
@ 2020-07-23  9:26   ` Cyril Hrubis
  0 siblings, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2020-07-23  9:26 UTC (permalink / raw)
  To: ltp

Hi!
Pushed with fix for ioctl_loop04, thanks.

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

diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop04.c b/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
index af3e90ddc..b4ab44a74 100644
--- a/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
@@ -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) {

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-07-23  9:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-23  8:30 [LTP] [PATCH v1 1/2] Allow detach file from loop device with opening dev_fd Yang Xu
2020-07-23  8:30 ` [LTP] [PATCH v1 2/2] syscalls/ioctl: Use tst_detach_device_by_fd api Yang Xu
2020-07-23  9:26   ` Cyril Hrubis
2020-07-23  9:25 ` [LTP] [PATCH v1 1/2] Allow detach file from loop device with opening dev_fd Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.