All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v1 00/10] add loop ioctl test
@ 2020-04-02 15:06 Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag Yang Xu
                   ` (9 more replies)
  0 siblings, 10 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

This is a set of test for the loop ioctl.

Yang Xu (10):
  lapi: Add a configure check and fallback for loop ioctl and flag
  syscalls/ioctl:add common c file for loop ioctl
  syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
    test
  syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test
  syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode
  syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test
  syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO ioctl test
  syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test
  syscalls/ioctl_loop07: Add dio with logic block size error test
  syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test

 configure.ac                                  |   1 +
 include/lapi/loop.h                           |  35 +++++
 runtest/syscalls                              |  10 +-
 testcases/kernel/syscalls/ioctl/.gitignore    |   8 +
 testcases/kernel/syscalls/ioctl/Makefile      |   3 +
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 127 +++++++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop02.c      | 148 ++++++++++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop03.c      |  76 +++++++++
 .../kernel/syscalls/ioctl/ioctl_loop04.c      |  97 ++++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop05.c      | 120 ++++++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop06.c      |  90 +++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop07.c      |  85 ++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop08.c      |  96 ++++++++++++
 .../syscalls/ioctl/ioctl_loop_support.c       |  74 +++++++++
 .../syscalls/ioctl/ioctl_loop_support.h       |  14 ++
 15 files changed, 983 insertions(+), 1 deletion(-)
 create mode 100644 include/lapi/loop.h
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop01.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop02.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop03.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop04.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop05.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop06.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop07.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop08.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.h

-- 
2.23.0




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

* [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-03 11:44   ` Cyril Hrubis
  2020-04-09  8:47   ` Martin Doucha
  2020-04-02 15:06 ` [LTP] [PATCH v1 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
                   ` (8 subsequent siblings)
  9 siblings, 2 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 configure.ac        |  1 +
 include/lapi/loop.h | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 include/lapi/loop.h

diff --git a/configure.ac b/configure.ac
index e1069b57e..e088cb564 100644
--- a/configure.ac
+++ b/configure.ac
@@ -50,6 +50,7 @@ AC_CHECK_HEADERS([ \
     linux/if_ether.h \
     linux/if_packet.h \
     linux/keyctl.h \
+    linux/loop.h \
     linux/mempolicy.h \
     linux/module.h \
     linux/netlink.h \
diff --git a/include/lapi/loop.h b/include/lapi/loop.h
new file mode 100644
index 000000000..bc6d9950d
--- /dev/null
+++ b/include/lapi/loop.h
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+#ifndef LAPI_LOOP_H
+#define LAPI_LOOP_H
+
+#include <linux/types.h>
+#ifdef HAVE_LINUX_LOOP_H
+# include <linux/loop.h>
+#endif
+
+#ifndef LO_FLAGS_PARTSCAN
+# define LO_FLAGS_PARTSCAN 8
+#endif
+
+#ifndef LO_FLAGS_DIRECT_IO
+# define LO_FLAGS_DIRECT_IO 16
+#endif
+
+#ifndef LOOP_SET_CAPACITY
+# define LOOP_SET_CAPACITY 0x4C07
+#endif
+
+#ifndef LOOP_SET_DIRECT_IO
+# define LOOP_SET_DIRECT_IO 0x4C08
+#endif
+
+#ifndef LOOP_SET_BLOCK_SIZE
+# define LOOP_SET_BLOCK_SIZE 0x4C09
+#endif
+
+#endif
+
-- 
2.23.0




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

* [LTP] [PATCH v1 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-03 12:16   ` Cyril Hrubis
  2020-04-02 15:06 ` [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/ioctl/Makefile      |  3 +
 .../syscalls/ioctl/ioctl_loop_support.c       | 74 +++++++++++++++++++
 .../syscalls/ioctl/ioctl_loop_support.h       | 14 ++++
 3 files changed, 91 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.h

diff --git a/testcases/kernel/syscalls/ioctl/Makefile b/testcases/kernel/syscalls/ioctl/Makefile
index c2ff6c8e7..05a75d1b4 100644
--- a/testcases/kernel/syscalls/ioctl/Makefile
+++ b/testcases/kernel/syscalls/ioctl/Makefile
@@ -7,6 +7,9 @@ include $(top_srcdir)/include/mk/testcases.mk
 
 INSTALL_TARGETS		+= test_ioctl
 
+MAKE_TARGETS            := $(patsubst $(abs_srcdir)/%.c,%,$(wildcard $(abs_srcdir)/ioctl_loop[0-9]*.c))
+$(MAKE_TARGETS): %: ioctl_loop_support.o
+
 ifeq ($(ANDROID),1)
 FILTER_OUT_MAKE_TARGETS	+= ioctl02
 endif
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
new file mode 100644
index 000000000..4099bd364
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+#define TST_NO_DEFAULT_MAIN
+#include "ioctl_loop_support.h"
+#include "tst_test.h"
+
+void check_sys_value(char *path, int setvalue)
+{
+	int getvalue;
+
+	SAFE_FILE_SCANF(path, "%d", &getvalue);
+	if (setvalue == getvalue)
+		tst_res(TPASS, "%s value is %d", path, setvalue);
+	else
+		tst_res(TFAIL, "%s value expected %d got %d", path, setvalue, getvalue);
+}
+
+void check_sys_string(char *path, char *setmessage)
+{
+	char getmessage[1024];
+
+	SAFE_FILE_SCANF(path, "%s", getmessage);
+	if (strcmp(setmessage, getmessage))
+		tst_res(TFAIL, "%s expected %s got %s", path, setmessage, getmessage);
+	else
+		tst_res(TPASS, "%s string is %s", path, getmessage);
+}
+
+void safe_set_status(int dev_fd, struct loop_info loopinfo)
+{
+	int sleep_us = 4096;
+	int ret = 0;
+
+	/*
+	 * It may have dirty page, so loop dirver may get EAGAIN error
+	 * when we use different offset or sizelimit.
+	 */
+	ret = ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo);
+	while (ret != 0 && errno == EAGAIN && sleep_us < 100000) {
+		ret = ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo);
+		usleep(sleep_us);
+		sleep_us *= 2;
+	}
+}
+
+void safe_set_status64(int dev_fd, struct loop_info64 loopinfo)
+{
+	int sleep_us = 4096;
+	int ret = 0;
+
+	/*
+	 * It may have dirty page, so loop dirver may get EAGAIN error
+	 * when we use different offset or sizelimit.
+	 */
+	ret = ioctl(dev_fd, LOOP_SET_STATUS64, &loopinfo);
+	while (ret != 0 && errno == EAGAIN && sleep_us < 100000) {
+		ret = ioctl(dev_fd, LOOP_SET_STATUS64, &loopinfo);
+		usleep(sleep_us);
+		sleep_us *= 2;
+	}
+}
+
+void check_support_cmd(int dev_fd, int ioctl_flag, int value, char *message)
+{
+	int ret = 0;
+
+	ret = ioctl(dev_fd, ioctl_flag, value);
+	if (ret && errno == EINVAL)
+		tst_brk(TCONF, "Current environment doesn't support this flag(%s)",
+				message);
+}
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
new file mode 100644
index 000000000..44445af8a
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+#ifndef IOCTL_LOOP_H
+#define IOCTL_lOOP_H
+#include <linux/loop.h>
+void check_sys_value(char *path, int setvalue);
+void check_sys_string(char *path, char *setmessage);
+void safe_set_status(int dev_fd, struct loop_info loopinfo);
+void safe_set_status64(int dev_fd, struct loop_info64 loopinfo);
+void check_support_cmd(int dev_fd, int ioctl_flag, int value, char *message);
+#endif
-- 
2.23.0




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

* [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-03 11:55   ` Cyril Hrubis
  2020-04-27  1:24   ` [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test Yang Xu
                   ` (6 subsequent siblings)
  9 siblings, 2 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
directory and also get lo_flags by using LOOP_GET_STATUS.

For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
But we also check whether we can scan partition table correctly ie check
whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |   3 +-
 testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 127 ++++++++++++++++++
 3 files changed, 130 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop01.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 777f0ac6a..7fd67450e 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -524,10 +524,11 @@ ioctl03      ioctl03
 ioctl04      ioctl04
 ioctl05      ioctl05
 ioctl06      ioctl06
-
 ioctl07      ioctl07
 ioctl08      ioctl08
 
+ioctl_loop01 ioctl_loop01
+
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
 ioctl_ns03 ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 97fbb9681..4cfef2839 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -6,6 +6,7 @@
 /ioctl06
 /ioctl07
 /ioctl08
+/ioctl_loop01
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop01.c b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
new file mode 100644
index 000000000..e806e0443
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ * It is designed to test LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN flag.
+ *
+ * For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
+ * directory and also get lo_flags by using LOOP_GET_STATUS.
+ *
+ * For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
+ * But we also check whether we can scan partition table correctly ie check
+ * whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024], backing_path[1024], backing_file_path[1024];
+static int dev_num, attach_flag, dev_fd;
+/*
+ *In drivers/block/loop.c code, set status function doesn't handle
+ *LO_FLAGS_READ_ONLY flag and ingore it. Only loop_set_fd with rondonly
+ *mode file_fd, lo_flags will include LO_FLAGS_READ_ONLY.
+ */
+#define set_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN | LO_FLAGS_READ_ONLY | LO_FLAGS_DIRECT_IO)
+#define get_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
+
+static char partscan_path[1024], autoclear_path[1024];
+static char loop_partpath[1026], sys_loop_partpath[1026];
+
+static void verify_ioctl_loop(void)
+{
+	int ret;
+	struct loop_info loopinfo, loopinfoget;
+
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	check_sys_value(partscan_path, 0);
+	check_sys_value(autoclear_path, 0);
+	check_sys_string(backing_path, backing_file_path);
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	memset(&loopinfo, 0, sizeof(loopinfo));
+	memset(&loopinfo, 0, sizeof(loopinfoget));
+
+	loopinfo.lo_flags = set_flags;
+	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
+
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
+
+	if (loopinfoget.lo_flags & ~get_flags)
+		tst_res(TFAIL, "expect %d but got %d", get_flags, loopinfoget.lo_flags);
+	else
+		tst_res(TPASS, "get expected lo_flag %d", loopinfoget.lo_flags);
+
+	ret = access(loop_partpath, F_OK);
+	if (ret == 0)
+		tst_res(TPASS, "access %s succeeds", loop_partpath);
+	else
+		tst_res(TFAIL, "access %s fails", loop_partpath);
+
+	ret = access(sys_loop_partpath, F_OK);
+	if (ret == 0)
+		tst_res(TPASS, "access %s succeeds", sys_loop_partpath);
+	else
+		tst_res(TFAIL, "access %s fails", sys_loop_partpath);
+
+	check_sys_value(partscan_path, 1);
+	check_sys_value(autoclear_path, 1);
+
+	SAFE_CLOSE(dev_fd);
+	tst_detach_device(dev_path);
+	attach_flag = 0;
+}
+
+static void setup(void)
+{
+	const char *const cmd_dd[] = {"dd", "if=/dev/zero", "of=test.img", "bs=1M", "count=10", NULL};
+	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
+						"primary", "ext4", "1M", "10M", NULL};
+
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	SAFE_CMD(cmd_dd, NULL, NULL);
+	SAFE_CMD(cmd_parted, NULL, NULL);
+
+	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
+	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
+	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
+	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);
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_cmds = (const char *const []) {
+		"dd",
+		"parted",
+		NULL
+	},
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	},
+	.needs_tmpdir = 1,
+};
-- 
2.23.0




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

* [LTP] [PATCH v1 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
                   ` (2 preceding siblings ...)
  2020-04-02 15:06 ` [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-03 13:34   ` Cyril Hrubis
  2020-04-02 15:06 ` [LTP] [PATCH v1 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode Yang Xu
                   ` (5 subsequent siblings)
  9 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

For LOOP_CHANGE_FD, this operation is possible only if the loop device
is read-only and the new backing store is the same size and type as the
old backing store.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
 .../kernel/syscalls/ioctl/ioctl_loop02.c      | 148 ++++++++++++++++++
 3 files changed, 150 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 7fd67450e..719953497 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -528,6 +528,7 @@ ioctl07      ioctl07
 ioctl08      ioctl08
 
 ioctl_loop01 ioctl_loop01
+ioctl_loop02 ioctl_loop02
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 4cfef2839..534c4ab34 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -7,6 +7,7 @@
 /ioctl07
 /ioctl08
 /ioctl_loop01
+/ioctl_loop02
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop02.c b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
new file mode 100644
index 000000000..ba3636aa9
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ *
+ * It is designed to test LO_FLAGS_READ_ONLY(similar as losetup -r)
+ * and LOOP_CHANGE_FD.
+ *
+ * For LOOP_CHANGE_FD, this operation is possible only if the loop device
+ * is read-only and the new backing store is the same size and type as the
+ * old backing store.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static int file_fd, file_change_fd, file_fd_invalid;
+static char backing_path[1024], backing_file_path[1024], backing_file_change_path[1024];
+static int attach_flag, dev_fd, file_fd;
+static char loop_ro_path[1024], dev_path[1024];
+static void attach_device(char *);
+
+static void verify_ioctl_loop(void)
+{
+	struct loop_info loopinfoget;
+
+	attach_device("test.img");
+	attach_flag = 1;
+
+	check_sys_value(loop_ro_path, 1);
+	check_sys_string(backing_path, backing_file_path);
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	file_change_fd = SAFE_OPEN("test1.img", O_RDWR);
+	file_fd_invalid = SAFE_OPEN("test2.img", O_RDWR);
+
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+	/*
+	 *In drivers/block/loop.c code, set status function doesn't handle
+	 *LO_FLAGS_READ_ONLY flag and ignore it. Only loop_set_fd with rondonly
+	 *mode, lo_flags will include LO_FLAGS_READ_ONLY.
+	 */
+
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
+
+	if (loopinfoget.lo_flags & ~LO_FLAGS_READ_ONLY)
+		tst_res(TFAIL, "lo_flags has unexpected %d flag", loopinfoget.lo_flags);
+	else
+		tst_res(TPASS, "lo_flags only has default LO_FLAGS_READ_ONLY flag");
+
+	TEST(write(dev_fd, "xx", 2));
+	if (TST_RET != -1)
+		tst_res(TFAIL, "write succeed unexpectedly");
+	else
+		tst_res(TPASS | TTERRNO, "Can not write data in RO mode");
+
+	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_change_fd));
+	if (TST_RET) {
+		tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed");
+	} else {
+		tst_res(TPASS, "LOOP_CHANGE_FD succeeded");
+		check_sys_value(loop_ro_path, 1);
+		check_sys_string(backing_path, backing_file_change_path);
+	}
+
+	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_fd_invalid));
+	if (TST_RET) {
+		if (TST_ERR == EINVAL)
+			tst_res(TPASS | TTERRNO, "LOOP_CHANGE_FD failed as expected");
+		else
+			tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed expected EINVAL got");
+	} else {
+		tst_res(TFAIL, "LOOP_CHANGE_FD succeeded");
+	}
+
+	SAFE_CLOSE(dev_fd);
+	SAFE_CLOSE(file_change_fd);
+	SAFE_CLOSE(file_fd_invalid);
+	tst_detach_device(dev_path);
+	attach_flag = 0;
+}
+
+/*
+ * This function is different from tst_attach_dev lib/tst_device.c.
+ * It only opens file_path with O_RDONLY mode before loop_set_fd.
+ * So we can generate LO_FLAGS_READ_ONLY flag.
+ */
+static void attach_device(char *img)
+{
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	file_fd = SAFE_OPEN(img, O_RDONLY);
+
+	SAFE_IOCTL(dev_fd, LOOP_SET_FD, file_fd);
+
+	SAFE_CLOSE(dev_fd);
+	SAFE_CLOSE(file_fd);
+}
+
+static void setup(void)
+{
+	int dev_num;
+
+	char *tmpdir = tst_get_tmpdir();
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	tst_fill_file("test.img", 0, 1024, 10);
+	tst_fill_file("test1.img", 0, 1024, 10);
+	tst_fill_file("test2.img", 0, 2048, 20);
+
+	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
+	sprintf(backing_file_path, "%s/test.img", tmpdir);
+	sprintf(backing_file_change_path, "%s/test1.img", tmpdir);
+	sprintf(loop_ro_path, "/sys/block/loop%d/ro", dev_num);
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (file_fd > 0)
+		SAFE_CLOSE(file_fd);
+	if (file_change_fd > 0)
+		SAFE_CLOSE(file_change_fd);
+	if (file_fd_invalid > 0)
+		SAFE_CLOSE(file_fd_invalid);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v1 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
                   ` (3 preceding siblings ...)
  2020-04-02 15:06 ` [LTP] [PATCH v1 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 06/10] syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test Yang Xu
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

It is designed to test LOOP_CHANGE_FD can not succeed(get EINAL error)
when loop_dev is not read only.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop03.c      | 76 +++++++++++++++++++
 3 files changed, 78 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 719953497..a4591dd62 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -529,6 +529,7 @@ ioctl08      ioctl08
 
 ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
+ioctl_loop03 ioctl_loop03
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 534c4ab34..1501c2779 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -8,6 +8,7 @@
 /ioctl08
 /ioctl_loop01
 /ioctl_loop02
+/ioctl_loop03
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop03.c b/testcases/kernel/syscalls/ioctl/ioctl_loop03.c
new file mode 100644
index 000000000..4e7c29cde
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop03.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ *
+ * It is designed to test LOOP_CHANGE_FD can not succeed(get EINAL error)
+ * when loop_dev is not read only.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024];
+static int dev_num, dev_fd, file_fd, attach_flag;
+
+static void verify_ioctl_loop(void)
+{
+	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_fd));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "LOOP_CHANGE_FD succeeded unexpectedly");
+		return;
+	}
+	if (TST_ERR == EINVAL)
+		tst_res(TPASS | TTERRNO, "LOOP_CHANGE_FD failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed expected EINVAL got");
+}
+
+static void setup(void)
+{
+	struct loop_info loopinfoget;
+
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	tst_fill_file("test.img", 0, 1024, 10);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	file_fd = SAFE_OPEN("test.img", O_RDWR);
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
+
+	if (loopinfoget.lo_flags & LO_FLAGS_READ_ONLY)
+		tst_brk(TCONF, "Current environment has unexpected LO_FLAGS_READ_ONLY flag");
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (file_fd > 0)
+		SAFE_CLOSE(file_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v1 06/10] syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
                   ` (4 preceding siblings ...)
  2020-04-02 15:06 ` [LTP] [PATCH v1 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO " Yang Xu
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

LOOP_SET_CAPACITY can update a live loop device size when we
change the size of the underlying backing file. Also check sys
value.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop04.c      | 97 +++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop04.c

diff --git a/runtest/syscalls b/runtest/syscalls
index a4591dd62..6e8d71d44 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -530,6 +530,7 @@ ioctl08      ioctl08
 ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
 ioctl_loop03 ioctl_loop03
+ioctl_loop04 ioctl_loop04
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 1501c2779..039a5251c 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -9,6 +9,7 @@
 /ioctl_loop01
 /ioctl_loop02
 /ioctl_loop03
+/ioctl_loop04
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop04.c b/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
new file mode 100644
index 000000000..ae110570e
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ *
+ * It is designed to test LOOP_SET_CAPACITY can update a live
+ * loop device size when we change the size of the underlying
+ * backing file. Also check sys value.
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+#define OLD_SIZE 10240
+#define NEW_SIZE 5120
+
+static char dev_path[1024], sys_loop_sizepath[1024];
+static char *wrbuf;
+static int dev_num, dev_fd, file_fd, attach_flag;
+
+static void verify_ioctl_loop(void)
+{
+	struct loop_info loopinfoget;
+
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+	tst_fill_file("test.img", 0, 1024, OLD_SIZE/1024);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	check_sys_value(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);
+
+	if (loopinfoget.lo_flags & LO_FLAGS_READ_ONLY)
+		tst_brk(TCONF, "Current environment has unexpected LO_FLAGS_READ_ONLY flag");
+
+	SAFE_TRUNCATE("test.img", NEW_SIZE);
+	SAFE_IOCTL(dev_fd, LOOP_SET_CAPACITY);
+
+	/*check that we can't write data beyond 5K into loop device*/
+	TEST(write(dev_fd, wrbuf, OLD_SIZE));
+	if (TST_RET == NEW_SIZE) {
+		tst_res(TPASS, "LOOP_SET_CAPACITY has updated loop size(%d) to %d", OLD_SIZE, NEW_SIZE);
+		check_sys_value(sys_loop_sizepath, NEW_SIZE/512);
+	} else {
+		tst_res(TFAIL, "LOOP_SET_CAPACITY doesn't updated loop size to %d, its size is %ld",
+				NEW_SIZE, TST_RET);
+	}
+
+	SAFE_CLOSE(file_fd);
+	SAFE_CLOSE(dev_fd);
+	tst_detach_device(dev_path);
+	unlink("test.img");
+	attach_flag = 0;
+}
+
+static void setup(void)
+{
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	wrbuf = SAFE_MALLOC(OLD_SIZE);
+	memset(wrbuf, 'x', OLD_SIZE);
+	sprintf(sys_loop_sizepath, "/sys/block/loop%d/size", dev_num);
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (file_fd > 0)
+		SAFE_CLOSE(file_fd);
+	if (wrbuf)
+		free(wrbuf);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v1 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO ioctl test
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
                   ` (5 preceding siblings ...)
  2020-04-02 15:06 ` [LTP] [PATCH v1 06/10] syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-03 16:44   ` Cyril Hrubis
  2020-04-02 15:06 ` [LTP] [PATCH v1 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test Yang Xu
                   ` (2 subsequent siblings)
  9 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

LOOP_SET_DIRECT_IO can updata a live loop device dio mode. It needs the
backing file also supports dio mode and the lo_offset is aligned with
the logical I/O size.

It was introduced into kernel since 4.10
commit ab1cb278bc70 ("block: loop: introduce ioctl command of LOOP_SET_DIRECT_IO").

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
 .../kernel/syscalls/ioctl/ioctl_loop05.c      | 120 ++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop05.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 6e8d71d44..9644588f3 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -531,6 +531,7 @@ ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
 ioctl_loop03 ioctl_loop03
 ioctl_loop04 ioctl_loop04
+ioctl_loop05 ioctl_loop05
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 039a5251c..f484d98d6 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -10,6 +10,7 @@
 /ioctl_loop02
 /ioctl_loop03
 /ioctl_loop04
+/ioctl_loop05
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop05.c b/testcases/kernel/syscalls/ioctl/ioctl_loop05.c
new file mode 100644
index 000000000..43bad6c18
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop05.c
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ *
+ * It is designed to test LOOP_SET_DIRECT_IO can updata a live
+ * loop device dio mode. It need the backing file also supports
+ * dio mode and the lo_offset is aligned with the logical I/O size.
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+#define DIO_MESSAGE "In dio mode"
+#define NON_DIO_MESSAGE "In non dio mode"
+
+static char dev_path[1024], sys_loop_diopath[1024];
+static int dev_num, dev_fd, attach_flag;
+
+static void check_dio_value(int flag)
+{
+	struct loop_info loopinfoget;
+
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
+	tst_res(TINFO, "%s", flag ? DIO_MESSAGE : NON_DIO_MESSAGE);
+
+	if (loopinfoget.lo_flags & LO_FLAGS_DIRECT_IO)
+		tst_res(flag ? TPASS : TFAIL, "lo_flags has LO_FLAGS_DIRECT_IO flag");
+	else
+		tst_res(flag ? TFAIL : TPASS, "lo_flags doesn't have LO_FLAGS_DIRECT_IO flag");
+
+	check_sys_value(sys_loop_diopath, flag);
+}
+
+static void verify_ioctl_loop(void)
+{
+	struct loop_info loopinfo;
+
+	memset(&loopinfo, 0, sizeof(loopinfo));
+
+	SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
+	check_dio_value(0);
+	tst_res(TINFO, "Without setting lo_offset or sizelimit");
+	SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 1);
+	check_dio_value(1);
+
+	tst_res(TINFO, "With offset equal to sector size");
+	loopinfo.lo_offset = 512;
+	safe_set_status(dev_fd, loopinfo);
+	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
+	if (TST_RET == 0) {
+		tst_res(TPASS, "LOOP_SET_DIRECT_IO succeeded");
+		check_dio_value(1);
+	} else {
+		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed");
+	}
+
+	tst_res(TINFO, "With offset less than or unalign sector size");
+	loopinfo.lo_offset = 256;
+	safe_set_status(dev_fd, loopinfo);
+
+	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "LOOP_SET_DIRECT_IO succeeded unexpectedly");
+		return;
+	}
+	if (TST_ERR == EINVAL)
+		tst_res(TPASS | TTERRNO, "LOOP_SET_DIRECT_IO failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed expected EINVAL got");
+
+	loopinfo.lo_offset = 0;
+	safe_set_status(dev_fd, loopinfo);
+}
+
+static void setup(void)
+{
+	if (tst_fs_type(".") == TST_TMPFS_MAGIC)
+		tst_brk(TCONF, "tmpfd doesn't support O_DIRECT flag, skip it");
+
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	sprintf(sys_loop_diopath, "/sys/block/loop%d/loop/dio", dev_num);
+	tst_fill_file("test.img", 0, 1024, 1024);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	check_support_cmd(dev_fd, LOOP_SET_DIRECT_IO, 0, "LOOP_SET_DIRECT_IO");
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+	unlink("test.img");
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+//	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v1 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
                   ` (6 preceding siblings ...)
  2020-04-02 15:06 ` [LTP] [PATCH v1 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO " Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 09/10] syscalls/ioctl_loop07: Add dio with logic block size " Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test Yang Xu
  9 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop06.c      | 90 +++++++++++++++++++
 3 files changed, 92 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop06.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 9644588f3..41774a494 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -532,6 +532,7 @@ ioctl_loop02 ioctl_loop02
 ioctl_loop03 ioctl_loop03
 ioctl_loop04 ioctl_loop04
 ioctl_loop05 ioctl_loop05
+ioctl_loop06 ioctl_loop06
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index f484d98d6..ec19b4901 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -11,6 +11,7 @@
 /ioctl_loop03
 /ioctl_loop04
 /ioctl_loop05
+/ioctl_loop06
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop06.c b/testcases/kernel/syscalls/ioctl/ioctl_loop06.c
new file mode 100644
index 000000000..ef98efbca
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop06.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl error test about loopdevice
+ * LOOP_SET_BLOCK_SIZE.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024], sys_loop_logicalpath[1024];
+static int dev_num, dev_fd, attach_flag;
+static unsigned int invalid_value, half_value, unalign_value;
+
+static struct tcase {
+	unsigned int *setvalue;
+	int exp_err;
+	char *message;
+} tcases[] = {
+	{&half_value, EINVAL, "arg < 512"},
+	{&invalid_value, EINVAL, "arg > PAGE_SIZE"},
+	{&unalign_value, EINVAL, "arg != power_of_2"},
+};
+
+static void verify_ioctl_loop(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	tst_res(TINFO, "%s", tc->message);
+	TEST(ioctl(dev_fd, LOOP_SET_BLOCK_SIZE, *(tc->setvalue)));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "LOOP_SET_BLOCK_SIZE succeed unexpectedly");
+		return;
+	}
+	if (TST_ERR == tc->exp_err)
+		tst_res(TPASS | TTERRNO, "LOOP_SET_BLOCK_SIZE failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "LOOP_SET_BLOCK_SIZE failed expected %s got",
+				tst_strerrno(tc->exp_err));
+}
+
+static void setup(void)
+{
+	unsigned int pg_size;
+
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	sprintf(sys_loop_logicalpath, "/sys/block/loop%d/logical_block_size", dev_num);
+	tst_fill_file("test.img", 0, 1024, 1024);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+	half_value = 256;
+	pg_size = getpagesize();
+	invalid_value = pg_size * 2 ;
+	unalign_value = pg_size - 1;
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	check_support_cmd(dev_fd, LOOP_SET_BLOCK_SIZE, 512, "LOOP_SET_BLOCK_SIZE");
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+	unlink("test.img");
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_ioctl_loop,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v1 09/10] syscalls/ioctl_loop07: Add dio with logic block size error test
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
                   ` (7 preceding siblings ...)
  2020-04-02 15:06 ` [LTP] [PATCH v1 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  2020-04-02 15:06 ` [LTP] [PATCH v1 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test Yang Xu
  9 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

This is a basic ioctl test about loopdevice LOOP_SET_BLOCK_SIZE
and LOOP_SET_DIRECT_IO. When blocksize is 1024(default align size
is 512), set dio with  512 offset will fail.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop07.c      | 85 +++++++++++++++++++
 3 files changed, 87 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop07.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 41774a494..0e4dbaa77 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -533,6 +533,7 @@ ioctl_loop03 ioctl_loop03
 ioctl_loop04 ioctl_loop04
 ioctl_loop05 ioctl_loop05
 ioctl_loop06 ioctl_loop06
+ioctl_loop07 ioctl_loop07
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index ec19b4901..3a3d49adc 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -12,6 +12,7 @@
 /ioctl_loop04
 /ioctl_loop05
 /ioctl_loop06
+/ioctl_loop07
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop07.c b/testcases/kernel/syscalls/ioctl/ioctl_loop07.c
new file mode 100644
index 000000000..7a168d35d
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop07.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice LOOP_SET_BLOCK_SIZE
+ * and LOOP_SET_DIRECT_IO.
+ * When blocksize is 1024(default align size is 512), set dio with
+ * 512 offset will fail.
+ */
+
+#include <stdio.h>
+#include <linux/loop.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024], sys_loop_logicalpath[1024];
+static int dev_num, dev_fd, attach_flag;
+
+static void verify_ioctl_loop(void)
+{
+	SAFE_IOCTL(dev_fd, LOOP_SET_BLOCK_SIZE, 1024);
+	check_sys_value(sys_loop_logicalpath, 1024);
+
+	/*
+	 * update dio with offset 512 to check this value is effective,
+	 * it should get EINVAL error.
+	 */
+	tst_res(TINFO, "logical block size is 1024");
+	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "LOOP_SET_DIRECT_IO succeeded unexpectedly");
+		return;
+	}
+	if (TST_ERR == EINVAL)
+		tst_res(TPASS | TTERRNO, "LOOP_SET_DIRECT_IO failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed expected EINVAL got");
+}
+
+static void setup(void)
+{
+	struct loop_info loopinfo;
+
+	memset(&loopinfo, 0, sizeof(loopinfo));
+	loopinfo.lo_offset = 512;
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	sprintf(sys_loop_logicalpath, "/sys/block/loop%d/queue/logical_block_size", dev_num);
+	tst_fill_file("test.img", 0, 1024, 1024);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	safe_set_status(dev_fd, loopinfo);
+	check_support_cmd(dev_fd, LOOP_SET_DIRECT_IO, 0, "LOOP_SET_DIRECT_IO");
+	check_support_cmd(dev_fd, LOOP_SET_BLOCK_SIZE, 512, "LOOP_SET_BLOCK_SIZE");
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+	unlink("test.img");
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v1 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test
  2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
                   ` (8 preceding siblings ...)
  2020-04-02 15:06 ` [LTP] [PATCH v1 09/10] syscalls/ioctl_loop07: Add dio with logic block size " Yang Xu
@ 2020-04-02 15:06 ` Yang Xu
  9 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-02 15:06 UTC (permalink / raw)
  To: ltp

Test its lo_sizelimit field. If lo_sizelimit is 0,it means max
available. If sizelimit is less than loop_size, loopsize will
be truncated.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop08.c      | 96 +++++++++++++++++++
 3 files changed, 98 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop08.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 0e4dbaa77..d88848cb9 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -534,6 +534,7 @@ ioctl_loop04 ioctl_loop04
 ioctl_loop05 ioctl_loop05
 ioctl_loop06 ioctl_loop06
 ioctl_loop07 ioctl_loop07
+ioctl_loop08 ioctl_loop08
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 3a3d49adc..97134aa0b 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -13,6 +13,7 @@
 /ioctl_loop05
 /ioctl_loop06
 /ioctl_loop07
+/ioctl_loop08
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop08.c b/testcases/kernel/syscalls/ioctl/ioctl_loop08.c
new file mode 100644
index 000000000..37e6c02b3
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop08.c
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice LOOP_SET_STATUS64
+ * and LOOP_GET_STATUS64.
+ * Test its lo_sizelimit field. If lo_sizelimit is 0,it means max
+ * available. If sizelimit is less than loop_size, loopsize will
+ * be truncated.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024], sys_loop_sizepath[1024], sys_loop_sizelimitpath[1024];
+static int dev_num, dev_fd, file_fd, attach_flag;
+
+static struct tcase {
+	unsigned int set_sizelimit;
+	unsigned int exp_loopsize;
+	char *message;
+} tcases[] = {
+	{1024 * 4096, 2048, "When sizelimit is greater than loopsize "},
+	{1024 * 512, 1024, "When sizelimit is less than loopsize"},
+};
+
+static void verify_ioctl_loop(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+	struct loop_info64 loopinfo, loopinfoget;
+
+	tst_res(TINFO, "%s", tc->message);
+	memset(&loopinfo, 0, sizeof(loopinfo));
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+
+	loopinfo.lo_sizelimit = tc->set_sizelimit;
+	safe_set_status64(dev_fd, loopinfo);
+
+	check_sys_value(sys_loop_sizepath, tc->exp_loopsize);
+	check_sys_value(sys_loop_sizelimitpath, tc->set_sizelimit);
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS64, &loopinfoget);
+	if (loopinfoget.lo_sizelimit == tc->set_sizelimit)
+		tst_res(TPASS, "LOOP_GET_STATUS64 gets correct lo_sizelimit(%d)", tc->set_sizelimit);
+	else
+		tst_res(TFAIL, "LOOP_GET_STATUS64 gets wrong lo_sizelimit(%llu), expect %d",
+				loopinfoget.lo_sizelimit, tc->set_sizelimit);
+	/*Reset*/
+	loopinfo.lo_sizelimit = 0;
+	safe_set_status64(dev_fd, loopinfo);
+}
+
+static void setup(void)
+{
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	tst_fill_file("test.img", 0, 1024, 1024);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	sprintf(sys_loop_sizepath, "/sys/block/loop%d/size", dev_num);
+	sprintf(sys_loop_sizelimitpath, "/sys/block/loop%d/loop/sizelimit", dev_num);
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	tst_res(TINFO, "original loop size 2048 sectors");
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (file_fd > 0)
+		SAFE_CLOSE(file_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_ioctl_loop,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag
  2020-04-02 15:06 ` [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag Yang Xu
@ 2020-04-03 11:44   ` Cyril Hrubis
  2020-04-06  6:01     ` Yang Xu
  2020-04-09  8:47   ` Martin Doucha
  1 sibling, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-03 11:44 UTC (permalink / raw)
  To: ltp

Hi!
> ---
>  configure.ac        |  1 +
>  include/lapi/loop.h | 35 +++++++++++++++++++++++++++++++++++
>  2 files changed, 36 insertions(+)
>  create mode 100644 include/lapi/loop.h
> 
> diff --git a/configure.ac b/configure.ac
> index e1069b57e..e088cb564 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -50,6 +50,7 @@ AC_CHECK_HEADERS([ \
>      linux/if_ether.h \
>      linux/if_packet.h \
>      linux/keyctl.h \
> +    linux/loop.h \

I do wonder if there is actually a distrbution without the linux/loop.h
header, have you seen one?

>      linux/mempolicy.h \
>      linux/module.h \
>      linux/netlink.h \
> diff --git a/include/lapi/loop.h b/include/lapi/loop.h
> new file mode 100644
> index 000000000..bc6d9950d
> --- /dev/null
> +++ b/include/lapi/loop.h
> @@ -0,0 +1,35 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + */
> +#ifndef LAPI_LOOP_H
> +#define LAPI_LOOP_H
> +
> +#include <linux/types.h>
> +#ifdef HAVE_LINUX_LOOP_H
> +# include <linux/loop.h>
> +#endif
> +
> +#ifndef LO_FLAGS_PARTSCAN
> +# define LO_FLAGS_PARTSCAN 8
> +#endif
> +
> +#ifndef LO_FLAGS_DIRECT_IO
> +# define LO_FLAGS_DIRECT_IO 16
> +#endif
> +
> +#ifndef LOOP_SET_CAPACITY
> +# define LOOP_SET_CAPACITY 0x4C07
> +#endif
> +
> +#ifndef LOOP_SET_DIRECT_IO
> +# define LOOP_SET_DIRECT_IO 0x4C08
> +#endif
> +
> +#ifndef LOOP_SET_BLOCK_SIZE
> +# define LOOP_SET_BLOCK_SIZE 0x4C09
> +#endif
> +
> +#endif
> +
> -- 
> 2.23.0
> 
> 
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test
  2020-04-02 15:06 ` [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
@ 2020-04-03 11:55   ` Cyril Hrubis
  2020-04-06  6:27     ` Yang Xu
  2020-04-27  1:24   ` [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
  1 sibling, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-03 11:55 UTC (permalink / raw)
  To: ltp

Hi!
> For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
> directory and also get lo_flags by using LOOP_GET_STATUS.
> 
> For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
> But we also check whether we can scan partition table correctly ie check
> whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
> 
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
>  runtest/syscalls                              |   3 +-
>  testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
>  .../kernel/syscalls/ioctl/ioctl_loop01.c      | 127 ++++++++++++++++++
>  3 files changed, 130 insertions(+), 1 deletion(-)
>  create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop01.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 777f0ac6a..7fd67450e 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -524,10 +524,11 @@ ioctl03      ioctl03
>  ioctl04      ioctl04
>  ioctl05      ioctl05
>  ioctl06      ioctl06
> -
>  ioctl07      ioctl07
>  ioctl08      ioctl08
>  
> +ioctl_loop01 ioctl_loop01
> +
>  ioctl_ns01 ioctl_ns01
>  ioctl_ns02 ioctl_ns02
>  ioctl_ns03 ioctl_ns03
> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
> index 97fbb9681..4cfef2839 100644
> --- a/testcases/kernel/syscalls/ioctl/.gitignore
> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
> @@ -6,6 +6,7 @@
>  /ioctl06
>  /ioctl07
>  /ioctl08
> +/ioctl_loop01
>  /ioctl_ns01
>  /ioctl_ns02
>  /ioctl_ns03
> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop01.c b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
> new file mode 100644
> index 000000000..e806e0443
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
> @@ -0,0 +1,127 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
> + *
> + * This is a basic ioctl test about loopdevice.
> + * It is designed to test LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN flag.
> + *
> + * For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
> + * directory and also get lo_flags by using LOOP_GET_STATUS.
> + *
> + * For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
> + * But we also check whether we can scan partition table correctly ie check
> + * whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
> + */
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include "ioctl_loop_support.h"
> +#include "lapi/loop.h"
> +#include "tst_test.h"
> +
> +static char dev_path[1024], backing_path[1024], backing_file_path[1024];
> +static int dev_num, attach_flag, dev_fd;
> +/*
> + *In drivers/block/loop.c code, set status function doesn't handle
> + *LO_FLAGS_READ_ONLY flag and ingore it. Only loop_set_fd with rondonly
> + *mode file_fd, lo_flags will include LO_FLAGS_READ_ONLY.
> + */
> +#define set_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN | LO_FLAGS_READ_ONLY | LO_FLAGS_DIRECT_IO)
> +#define get_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
> +
> +static char partscan_path[1024], autoclear_path[1024];
> +static char loop_partpath[1026], sys_loop_partpath[1026];
> +
> +static void verify_ioctl_loop(void)
> +{
> +	int ret;
> +	struct loop_info loopinfo, loopinfoget;
> +
> +	tst_attach_device(dev_path, "test.img");
> +	attach_flag = 1;
> +
> +	check_sys_value(partscan_path, 0);
> +	check_sys_value(autoclear_path, 0);
> +	check_sys_string(backing_path, backing_file_path);
> +
> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
> +	memset(&loopinfo, 0, sizeof(loopinfo));
> +	memset(&loopinfo, 0, sizeof(loopinfoget));
> +
> +	loopinfo.lo_flags = set_flags;
> +	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);

I guess that it would be safer to memset() the loopinfo_get here, also
if we zero the structure we may as well reuse the loopinfo here as well.

> +	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
> +
> +	if (loopinfoget.lo_flags & ~get_flags)
> +		tst_res(TFAIL, "expect %d but got %d", get_flags, loopinfoget.lo_flags);
> +	else
> +		tst_res(TPASS, "get expected lo_flag %d", loopinfoget.lo_flags);
> +
> +	ret = access(loop_partpath, F_OK);
> +	if (ret == 0)
> +		tst_res(TPASS, "access %s succeeds", loop_partpath);
> +	else
> +		tst_res(TFAIL, "access %s fails", loop_partpath);
> +
> +	ret = access(sys_loop_partpath, F_OK);
> +	if (ret == 0)
> +		tst_res(TPASS, "access %s succeeds", sys_loop_partpath);
> +	else
> +		tst_res(TFAIL, "access %s fails", sys_loop_partpath);
> +
> +	check_sys_value(partscan_path, 1);
> +	check_sys_value(autoclear_path, 1);
> +
> +	SAFE_CLOSE(dev_fd);
> +	tst_detach_device(dev_path);
> +	attach_flag = 0;
> +}
> +
> +static void setup(void)
> +{
> +	const char *const cmd_dd[] = {"dd", "if=/dev/zero", "of=test.img", "bs=1M", "count=10", NULL};
> +	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
> +						"primary", "ext4", "1M", "10M", NULL};
> +
> +	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
> +	if (dev_num < 0)
> +		tst_brk(TBROK, "Failed to find free loop device");
> +
> +	SAFE_CMD(cmd_dd, NULL, NULL);

Why not tst_fill_file("test.img", 0, 1024 * 1024, 10); ?

> +	SAFE_CMD(cmd_parted, NULL, NULL);

I wonder if we can avoid dependency on parted by having a look at the
changes in the image after the call to parted and writing a few bytes
ourselves instead.

> +	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
> +	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
> +	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
> +	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);
> +}
> +
> +static void cleanup(void)
> +{
> +	if (dev_fd > 0)
> +		SAFE_CLOSE(dev_fd);
> +	if (attach_flag)
> +		tst_detach_device(dev_path);
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = verify_ioctl_loop,
> +	.needs_root = 1,
> +	.needs_cmds = (const char *const []) {
> +		"dd",
> +		"parted",
> +		NULL
> +	},
> +	.needs_drivers = (const char *const []) {
> +		"loop",
> +		NULL
> +	},
> +	.needs_tmpdir = 1,
> +};
> -- 
> 2.23.0
> 
> 
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v1 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-02 15:06 ` [LTP] [PATCH v1 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
@ 2020-04-03 12:16   ` Cyril Hrubis
  2020-04-09  2:42     ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-03 12:16 UTC (permalink / raw)
  To: ltp

Hi!
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/ioctl/Makefile      |  3 +
>  .../syscalls/ioctl/ioctl_loop_support.c       | 74 +++++++++++++++++++
>  .../syscalls/ioctl/ioctl_loop_support.h       | 14 ++++
>  3 files changed, 91 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
>  create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
> 
> diff --git a/testcases/kernel/syscalls/ioctl/Makefile b/testcases/kernel/syscalls/ioctl/Makefile
> index c2ff6c8e7..05a75d1b4 100644
> --- a/testcases/kernel/syscalls/ioctl/Makefile
> +++ b/testcases/kernel/syscalls/ioctl/Makefile
> @@ -7,6 +7,9 @@ include $(top_srcdir)/include/mk/testcases.mk
>  
>  INSTALL_TARGETS		+= test_ioctl
>  
> +MAKE_TARGETS            := $(patsubst $(abs_srcdir)/%.c,%,$(wildcard $(abs_srcdir)/ioctl_loop[0-9]*.c))
> +$(MAKE_TARGETS): %: ioctl_loop_support.o

I guess that we should use anything else than MAKE_TARGETS because
changing that variable will disable rest of the ioctl tests from build
right?

As this only expresses dependency on the object file we should use any
variable name that is not used by the test library itself.

>  ifeq ($(ANDROID),1)
>  FILTER_OUT_MAKE_TARGETS	+= ioctl02
>  endif
> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
> new file mode 100644
> index 000000000..4099bd364
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
> @@ -0,0 +1,74 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + */
> +#define TST_NO_DEFAULT_MAIN
> +#include "ioctl_loop_support.h"
> +#include "tst_test.h"
> +
> +void check_sys_value(char *path, int setvalue)
> +{
> +	int getvalue;
> +
> +	SAFE_FILE_SCANF(path, "%d", &getvalue);
> +	if (setvalue == getvalue)
> +		tst_res(TPASS, "%s value is %d", path, setvalue);
> +	else
> +		tst_res(TFAIL, "%s value expected %d got %d", path, setvalue, getvalue);
> +}
> +
> +void check_sys_string(char *path, char *setmessage)
> +{
> +	char getmessage[1024];
> +
> +	SAFE_FILE_SCANF(path, "%s", getmessage);
> +	if (strcmp(setmessage, getmessage))
> +		tst_res(TFAIL, "%s expected %s got %s", path, setmessage, getmessage);
> +	else
> +		tst_res(TPASS, "%s string is %s", path, getmessage);
> +}
> +
> +void safe_set_status(int dev_fd, struct loop_info loopinfo)
> +{
> +	int sleep_us = 4096;
> +	int ret = 0;
> +
> +	/*
> +	 * It may have dirty page, so loop dirver may get EAGAIN error
> +	 * when we use different offset or sizelimit.
> +	 */
> +	ret = ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo);
> +	while (ret != 0 && errno == EAGAIN && sleep_us < 100000) {
> +		ret = ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo);
> +		usleep(sleep_us);
> +		sleep_us *= 2;
> +	}

TBROK here if we failed to set the status?

Also we should use the TST_RETRY_FUNC() instead because that one uses
the timeout multiplier environment variable.

> +}
> +
> +void safe_set_status64(int dev_fd, struct loop_info64 loopinfo)
> +{
> +	int sleep_us = 4096;
> +	int ret = 0;
> +
> +	/*
> +	 * It may have dirty page, so loop dirver may get EAGAIN error
> +	 * when we use different offset or sizelimit.
> +	 */
> +	ret = ioctl(dev_fd, LOOP_SET_STATUS64, &loopinfo);
> +	while (ret != 0 && errno == EAGAIN && sleep_us < 100000) {
> +		ret = ioctl(dev_fd, LOOP_SET_STATUS64, &loopinfo);
> +		usleep(sleep_us);
> +		sleep_us *= 2;
> +	}

Here as well.

> +}
> +
> +void check_support_cmd(int dev_fd, int ioctl_flag, int value, char *message)
> +{
> +	int ret = 0;
> +
> +	ret = ioctl(dev_fd, ioctl_flag, value);
> +	if (ret && errno == EINVAL)
> +		tst_brk(TCONF, "Current environment doesn't support this flag(%s)",
> +				message);
> +}
> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
> new file mode 100644
> index 000000000..44445af8a
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
> @@ -0,0 +1,14 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + */
> +#ifndef IOCTL_LOOP_H
> +#define IOCTL_lOOP_H
> +#include <linux/loop.h>
> +void check_sys_value(char *path, int setvalue);
> +void check_sys_string(char *path, char *setmessage);
> +void safe_set_status(int dev_fd, struct loop_info loopinfo);
> +void safe_set_status64(int dev_fd, struct loop_info64 loopinfo);
> +void check_support_cmd(int dev_fd, int ioctl_flag, int value, char *message);
> +#endif
> -- 
> 2.23.0
> 
> 
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v1 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test
  2020-04-02 15:06 ` [LTP] [PATCH v1 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test Yang Xu
@ 2020-04-03 13:34   ` Cyril Hrubis
  2020-04-06  7:24     ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-03 13:34 UTC (permalink / raw)
  To: ltp

Hi!
> For LOOP_CHANGE_FD, this operation is possible only if the loop device
> is read-only and the new backing store is the same size and type as the
> old backing store.
> 
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
>  runtest/syscalls                              |   1 +
>  testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
>  .../kernel/syscalls/ioctl/ioctl_loop02.c      | 148 ++++++++++++++++++
>  3 files changed, 150 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop02.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 7fd67450e..719953497 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -528,6 +528,7 @@ ioctl07      ioctl07
>  ioctl08      ioctl08
>  
>  ioctl_loop01 ioctl_loop01
> +ioctl_loop02 ioctl_loop02
>  
>  ioctl_ns01 ioctl_ns01
>  ioctl_ns02 ioctl_ns02
> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
> index 4cfef2839..534c4ab34 100644
> --- a/testcases/kernel/syscalls/ioctl/.gitignore
> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
> @@ -7,6 +7,7 @@
>  /ioctl07
>  /ioctl08
>  /ioctl_loop01
> +/ioctl_loop02
>  /ioctl_ns01
>  /ioctl_ns02
>  /ioctl_ns03
> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop02.c b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
> new file mode 100644
> index 000000000..ba3636aa9
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
> @@ -0,0 +1,148 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
> + *
> + * This is a basic ioctl test about loopdevice.
> + *
> + * It is designed to test LO_FLAGS_READ_ONLY(similar as losetup -r)
> + * and LOOP_CHANGE_FD.
> + *
> + * For LOOP_CHANGE_FD, this operation is possible only if the loop device
> + * is read-only and the new backing store is the same size and type as the
> + * old backing store.
> + */
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include "ioctl_loop_support.h"
> +#include "lapi/loop.h"
> +#include "tst_test.h"
> +
> +static int file_fd, file_change_fd, file_fd_invalid;
> +static char backing_path[1024], backing_file_path[1024], backing_file_change_path[1024];
> +static int attach_flag, dev_fd, file_fd;
> +static char loop_ro_path[1024], dev_path[1024];
> +static void attach_device(char *);

Just move the function here, there is no point in having the helper
function after the test function.

> +static void verify_ioctl_loop(void)
> +{
> +	struct loop_info loopinfoget;
> +
> +	attach_device("test.img");
> +	attach_flag = 1;
> +
> +	check_sys_value(loop_ro_path, 1);
> +	check_sys_string(backing_path, backing_file_path);
> +
> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
> +	file_change_fd = SAFE_OPEN("test1.img", O_RDWR);
> +	file_fd_invalid = SAFE_OPEN("test2.img", O_RDWR);
> +
> +	memset(&loopinfoget, 0, sizeof(loopinfoget));
> +	/*
> +	 *In drivers/block/loop.c code, set status function doesn't handle
> +	 *LO_FLAGS_READ_ONLY flag and ignore it. Only loop_set_fd with rondonly
                                                                       ^
								       readonly
> +	 *mode, lo_flags will include LO_FLAGS_READ_ONLY.
> +	 */

Also it's customary to add a space between the asterisk and the comment
text.

> +	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
> +
> +	if (loopinfoget.lo_flags & ~LO_FLAGS_READ_ONLY)
> +		tst_res(TFAIL, "lo_flags has unexpected %d flag", loopinfoget.lo_flags);
> +	else
> +		tst_res(TPASS, "lo_flags only has default LO_FLAGS_READ_ONLY flag");
> +
> +	TEST(write(dev_fd, "xx", 2));
> +	if (TST_RET != -1)
> +		tst_res(TFAIL, "write succeed unexpectedly");
> +	else
> +		tst_res(TPASS | TTERRNO, "Can not write data in RO mode");
> +
> +	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_change_fd));
> +	if (TST_RET) {
> +		tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed");
> +	} else {
> +		tst_res(TPASS, "LOOP_CHANGE_FD succeeded");
> +		check_sys_value(loop_ro_path, 1);
> +		check_sys_string(backing_path, backing_file_change_path);
> +	}
> +
> +	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_fd_invalid));
> +	if (TST_RET) {
> +		if (TST_ERR == EINVAL)
> +			tst_res(TPASS | TTERRNO, "LOOP_CHANGE_FD failed as expected");
> +		else
> +			tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed expected EINVAL got");
> +	} else {
> +		tst_res(TFAIL, "LOOP_CHANGE_FD succeeded");
> +	}
> +
> +	SAFE_CLOSE(dev_fd);
> +	SAFE_CLOSE(file_change_fd);
> +	SAFE_CLOSE(file_fd_invalid);
> +	tst_detach_device(dev_path);
> +	attach_flag = 0;
> +}
> +
> +/*
> + * This function is different from tst_attach_dev lib/tst_device.c.
> + * It only opens file_path with O_RDONLY mode before loop_set_fd.
> + * So we can generate LO_FLAGS_READ_ONLY flag.
> + */
> +static void attach_device(char *img)
> +{
> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
> +	file_fd = SAFE_OPEN(img, O_RDONLY);
> +
> +	SAFE_IOCTL(dev_fd, LOOP_SET_FD, file_fd);
> +
> +	SAFE_CLOSE(dev_fd);
> +	SAFE_CLOSE(file_fd);
> +}
> +
> +static void setup(void)
> +{
> +	int dev_num;
> +
> +	char *tmpdir = tst_get_tmpdir();
> +	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
> +	if (dev_num < 0)
> +		tst_brk(TBROK, "Failed to find free loop device");
> +
> +	tst_fill_file("test.img", 0, 1024, 10);
> +	tst_fill_file("test1.img", 0, 1024, 10);
> +	tst_fill_file("test2.img", 0, 2048, 20);
> +
> +	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
> +	sprintf(backing_file_path, "%s/test.img", tmpdir);
> +	sprintf(backing_file_change_path, "%s/test1.img", tmpdir);
> +	sprintf(loop_ro_path, "/sys/block/loop%d/ro", dev_num);

We should free the tmpdir here.

> +}
> +
> +static void cleanup(void)
> +{
> +	if (dev_fd > 0)
> +		SAFE_CLOSE(dev_fd);
> +	if (file_fd > 0)
> +		SAFE_CLOSE(file_fd);
> +	if (file_change_fd > 0)
> +		SAFE_CLOSE(file_change_fd);
> +	if (file_fd_invalid > 0)
> +		SAFE_CLOSE(file_fd_invalid);
> +	if (attach_flag)
> +		tst_detach_device(dev_path);
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = verify_ioctl_loop,
> +	.needs_root = 1,
> +	.needs_tmpdir = 1,
> +	.needs_drivers = (const char *const []) {
> +		"loop",
> +		NULL
> +	}
> +};

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v1 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO ioctl test
  2020-04-02 15:06 ` [LTP] [PATCH v1 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO " Yang Xu
@ 2020-04-03 16:44   ` Cyril Hrubis
  2020-04-06  7:53     ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-03 16:44 UTC (permalink / raw)
  To: ltp

Hi!
> LOOP_SET_DIRECT_IO can updata a live loop device dio mode. It needs the
> backing file also supports dio mode and the lo_offset is aligned with
> the logical I/O size.
> 
> It was introduced into kernel since 4.10
> commit ab1cb278bc70 ("block: loop: introduce ioctl command of LOOP_SET_DIRECT_IO").
> 
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
>  runtest/syscalls                              |   1 +
>  testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
>  .../kernel/syscalls/ioctl/ioctl_loop05.c      | 120 ++++++++++++++++++
>  3 files changed, 122 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop05.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 6e8d71d44..9644588f3 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -531,6 +531,7 @@ ioctl_loop01 ioctl_loop01
>  ioctl_loop02 ioctl_loop02
>  ioctl_loop03 ioctl_loop03
>  ioctl_loop04 ioctl_loop04
> +ioctl_loop05 ioctl_loop05
>  
>  ioctl_ns01 ioctl_ns01
>  ioctl_ns02 ioctl_ns02
> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
> index 039a5251c..f484d98d6 100644
> --- a/testcases/kernel/syscalls/ioctl/.gitignore
> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
> @@ -10,6 +10,7 @@
>  /ioctl_loop02
>  /ioctl_loop03
>  /ioctl_loop04
> +/ioctl_loop05
>  /ioctl_ns01
>  /ioctl_ns02
>  /ioctl_ns03
> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop05.c b/testcases/kernel/syscalls/ioctl/ioctl_loop05.c
> new file mode 100644
> index 000000000..43bad6c18
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop05.c
> @@ -0,0 +1,120 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
> + *
> + * This is a basic ioctl test about loopdevice.
> + *
> + * It is designed to test LOOP_SET_DIRECT_IO can updata a live
> + * loop device dio mode. It need the backing file also supports
> + * dio mode and the lo_offset is aligned with the logical I/O size.
> + */
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include <stdlib.h>
> +#include "ioctl_loop_support.h"
> +#include "lapi/loop.h"
> +#include "tst_test.h"
> +
> +#define DIO_MESSAGE "In dio mode"
> +#define NON_DIO_MESSAGE "In non dio mode"
> +
> +static char dev_path[1024], sys_loop_diopath[1024];
> +static int dev_num, dev_fd, attach_flag;
> +
> +static void check_dio_value(int flag)
> +{
> +	struct loop_info loopinfoget;
> +
> +	memset(&loopinfoget, 0, sizeof(loopinfoget));
> +
> +	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
> +	tst_res(TINFO, "%s", flag ? DIO_MESSAGE : NON_DIO_MESSAGE);
> +
> +	if (loopinfoget.lo_flags & LO_FLAGS_DIRECT_IO)
> +		tst_res(flag ? TPASS : TFAIL, "lo_flags has LO_FLAGS_DIRECT_IO flag");
> +	else
> +		tst_res(flag ? TFAIL : TPASS, "lo_flags doesn't have LO_FLAGS_DIRECT_IO flag");
> +
> +	check_sys_value(sys_loop_diopath, flag);
> +}
> +
> +static void verify_ioctl_loop(void)
> +{
> +	struct loop_info loopinfo;
> +
> +	memset(&loopinfo, 0, sizeof(loopinfo));
> +
> +	SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
> +	check_dio_value(0);
> +	tst_res(TINFO, "Without setting lo_offset or sizelimit");
> +	SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 1);
> +	check_dio_value(1);
> +
> +	tst_res(TINFO, "With offset equal to sector size");
> +	loopinfo.lo_offset = 512;

We should use BLKSSZGET ioctl() to get the block size for direct I/O
otherwise I would expect that it would break on one of the less common
architectures. See also discussion at the end of man 2 open.

> +	safe_set_status(dev_fd, loopinfo);
> +	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
> +	if (TST_RET == 0) {
> +		tst_res(TPASS, "LOOP_SET_DIRECT_IO succeeded");
> +		check_dio_value(1);
> +	} else {
> +		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed");
> +	}
> +
> +	tst_res(TINFO, "With offset less than or unalign sector size");
> +	loopinfo.lo_offset = 256;
> +	safe_set_status(dev_fd, loopinfo);
> +
> +	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
> +	if (TST_RET == 0) {
> +		tst_res(TFAIL, "LOOP_SET_DIRECT_IO succeeded unexpectedly");
> +		return;
> +	}
> +	if (TST_ERR == EINVAL)
> +		tst_res(TPASS | TTERRNO, "LOOP_SET_DIRECT_IO failed as expected");
> +	else
> +		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed expected EINVAL got");
> +
> +	loopinfo.lo_offset = 0;
> +	safe_set_status(dev_fd, loopinfo);
> +}
> +
> +static void setup(void)
> +{
> +	if (tst_fs_type(".") == TST_TMPFS_MAGIC)
> +		tst_brk(TCONF, "tmpfd doesn't support O_DIRECT flag, skip it");
> +
> +	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
> +	if (dev_num < 0)
> +		tst_brk(TBROK, "Failed to find free loop device");
> +
> +	sprintf(sys_loop_diopath, "/sys/block/loop%d/loop/dio", dev_num);
> +	tst_fill_file("test.img", 0, 1024, 1024);
> +	tst_attach_device(dev_path, "test.img");
> +	attach_flag = 1;
> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
> +	check_support_cmd(dev_fd, LOOP_SET_DIRECT_IO, 0, "LOOP_SET_DIRECT_IO");
> +}
> +
> +static void cleanup(void)
> +{
> +	if (dev_fd > 0)
> +		SAFE_CLOSE(dev_fd);
> +	if (attach_flag)
> +		tst_detach_device(dev_path);
> +	unlink("test.img");

We don't have to remove the image here once the needs_tmpdir has been
uncommented below, right?

> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = verify_ioctl_loop,
> +	.needs_root = 1,
> +//	.needs_tmpdir = 1,

Looks like a forgotten debug measure.

> +	.needs_drivers = (const char *const []) {
> +		"loop",
> +		NULL
> +	}
> +};
> -- 
> 2.23.0
> 
> 
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag
  2020-04-03 11:44   ` Cyril Hrubis
@ 2020-04-06  6:01     ` Yang Xu
  0 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-06  6:01 UTC (permalink / raw)
  To: ltp

Hi Cyril

Thanks for your review.
> Hi!
>> ---
>>   configure.ac        |  1 +
>>   include/lapi/loop.h | 35 +++++++++++++++++++++++++++++++++++
>>   2 files changed, 36 insertions(+)
>>   create mode 100644 include/lapi/loop.h
>>
>> diff --git a/configure.ac b/configure.ac
>> index e1069b57e..e088cb564 100644
>> --- a/configure.ac
>> +++ b/configure.ac
>> @@ -50,6 +50,7 @@ AC_CHECK_HEADERS([ \
>>       linux/if_ether.h \
>>       linux/if_packet.h \
>>       linux/keyctl.h \
>> +    linux/loop.h \
> 
> I do wonder if there is actually a distrbution without the linux/loop.h
> header, have you seen one?
> 
No, I don't see this distribution. We can remove this.
>>       linux/mempolicy.h \
>>       linux/module.h \
>>       linux/netlink.h \
>> diff --git a/include/lapi/loop.h b/include/lapi/loop.h
>> new file mode 100644
>> index 000000000..bc6d9950d
>> --- /dev/null
>> +++ b/include/lapi/loop.h
>> @@ -0,0 +1,35 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> + */
>> +#ifndef LAPI_LOOP_H
>> +#define LAPI_LOOP_H
>> +
>> +#include <linux/types.h>
>> +#ifdef HAVE_LINUX_LOOP_H
>> +# include <linux/loop.h>
>> +#endif
>> +
>> +#ifndef LO_FLAGS_PARTSCAN
>> +# define LO_FLAGS_PARTSCAN 8
>> +#endif
>> +
>> +#ifndef LO_FLAGS_DIRECT_IO
>> +# define LO_FLAGS_DIRECT_IO 16
>> +#endif
>> +
>> +#ifndef LOOP_SET_CAPACITY
>> +# define LOOP_SET_CAPACITY 0x4C07
>> +#endif
>> +
>> +#ifndef LOOP_SET_DIRECT_IO
>> +# define LOOP_SET_DIRECT_IO 0x4C08
>> +#endif
>> +
>> +#ifndef LOOP_SET_BLOCK_SIZE
>> +# define LOOP_SET_BLOCK_SIZE 0x4C09
>> +#endif
>> +
>> +#endif
>> +
>> -- 
>> 2.23.0
>>
>>
>>
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp
> 


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

* [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test
  2020-04-03 11:55   ` Cyril Hrubis
@ 2020-04-06  6:27     ` Yang Xu
  2020-04-09  6:10       ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-06  6:27 UTC (permalink / raw)
  To: ltp

Hi Cyril

> Hi!
>> For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
>> directory and also get lo_flags by using LOOP_GET_STATUS.
>>
>> For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
>> But we also check whether we can scan partition table correctly ie check
>> whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
>>
>> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> ---
>>   runtest/syscalls                              |   3 +-
>>   testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
>>   .../kernel/syscalls/ioctl/ioctl_loop01.c      | 127 ++++++++++++++++++
>>   3 files changed, 130 insertions(+), 1 deletion(-)
>>   create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop01.c
>>
>> diff --git a/runtest/syscalls b/runtest/syscalls
>> index 777f0ac6a..7fd67450e 100644
>> --- a/runtest/syscalls
>> +++ b/runtest/syscalls
>> @@ -524,10 +524,11 @@ ioctl03      ioctl03
>>   ioctl04      ioctl04
>>   ioctl05      ioctl05
>>   ioctl06      ioctl06
>> -
>>   ioctl07      ioctl07
>>   ioctl08      ioctl08
>>   
>> +ioctl_loop01 ioctl_loop01
>> +
>>   ioctl_ns01 ioctl_ns01
>>   ioctl_ns02 ioctl_ns02
>>   ioctl_ns03 ioctl_ns03
>> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
>> index 97fbb9681..4cfef2839 100644
>> --- a/testcases/kernel/syscalls/ioctl/.gitignore
>> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
>> @@ -6,6 +6,7 @@
>>   /ioctl06
>>   /ioctl07
>>   /ioctl08
>> +/ioctl_loop01
>>   /ioctl_ns01
>>   /ioctl_ns02
>>   /ioctl_ns03
>> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop01.c b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
>> new file mode 100644
>> index 000000000..e806e0443
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
>> @@ -0,0 +1,127 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>> + * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
>> + *
>> + * This is a basic ioctl test about loopdevice.
>> + * It is designed to test LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN flag.
>> + *
>> + * For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
>> + * directory and also get lo_flags by using LOOP_GET_STATUS.
>> + *
>> + * For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
>> + * But we also check whether we can scan partition table correctly ie check
>> + * whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
>> + */
>> +
>> +#include <stdio.h>
>> +#include <unistd.h>
>> +#include <string.h>
>> +#include "ioctl_loop_support.h"
>> +#include "lapi/loop.h"
>> +#include "tst_test.h"
>> +
>> +static char dev_path[1024], backing_path[1024], backing_file_path[1024];
>> +static int dev_num, attach_flag, dev_fd;
>> +/*
>> + *In drivers/block/loop.c code, set status function doesn't handle
>> + *LO_FLAGS_READ_ONLY flag and ingore it. Only loop_set_fd with rondonly
>> + *mode file_fd, lo_flags will include LO_FLAGS_READ_ONLY.
>> + */
>> +#define set_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN | LO_FLAGS_READ_ONLY | LO_FLAGS_DIRECT_IO)
>> +#define get_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
>> +
>> +static char partscan_path[1024], autoclear_path[1024];
>> +static char loop_partpath[1026], sys_loop_partpath[1026];
>> +
>> +static void verify_ioctl_loop(void)
>> +{
>> +	int ret;
>> +	struct loop_info loopinfo, loopinfoget;
>> +
>> +	tst_attach_device(dev_path, "test.img");
>> +	attach_flag = 1;
>> +
>> +	check_sys_value(partscan_path, 0);
>> +	check_sys_value(autoclear_path, 0);
>> +	check_sys_string(backing_path, backing_file_path);
>> +
>> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
>> +	memset(&loopinfo, 0, sizeof(loopinfo));
>> +	memset(&loopinfo, 0, sizeof(loopinfoget));
>> +
>> +	loopinfo.lo_flags = set_flags;
>> +	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
> 
> I guess that it would be safer to memset() the loopinfo_get here, also
> if we zero the structure we may as well reuse the loopinfo here as well.
>
of coure, I will add memset in here.

>> +	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
>> +
>> +	if (loopinfoget.lo_flags & ~get_flags)
>> +		tst_res(TFAIL, "expect %d but got %d", get_flags, loopinfoget.lo_flags);
>> +	else
>> +		tst_res(TPASS, "get expected lo_flag %d", loopinfoget.lo_flags);
>> +
>> +	ret = access(loop_partpath, F_OK);
>> +	if (ret == 0)
>> +		tst_res(TPASS, "access %s succeeds", loop_partpath);
>> +	else
>> +		tst_res(TFAIL, "access %s fails", loop_partpath);
>> +
>> +	ret = access(sys_loop_partpath, F_OK);
>> +	if (ret == 0)
>> +		tst_res(TPASS, "access %s succeeds", sys_loop_partpath);
>> +	else
>> +		tst_res(TFAIL, "access %s fails", sys_loop_partpath);
>> +
>> +	check_sys_value(partscan_path, 1);
>> +	check_sys_value(autoclear_path, 1);
>> +
>> +	SAFE_CLOSE(dev_fd);
>> +	tst_detach_device(dev_path);
>> +	attach_flag = 0;
>> +}
>> +
>> +static void setup(void)
>> +{
>> +	const char *const cmd_dd[] = {"dd", "if=/dev/zero", "of=test.img", "bs=1M", "count=10", NULL};
>> +	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
>> +						"primary", "ext4", "1M", "10M", NULL};
>> +
>> +	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
>> +	if (dev_num < 0)
>> +		tst_brk(TBROK, "Failed to find free loop device");
>> +
>> +	SAFE_CMD(cmd_dd, NULL, NULL);
> 
> Why not tst_fill_file("test.img", 0, 1024 * 1024, 10); ?
>
I will try this.

>> +	SAFE_CMD(cmd_parted, NULL, NULL);
> 
> I wonder if we can avoid dependency on parted by having a look at the
> changes in the image after the call to parted and writing a few bytes
> ourselves instead.
I will see kernel code to figure out this, if no other good way, we can 
split this check into a new single test. it can make this case less couping.
> 
>> +	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
>> +	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
>> +	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
>> +	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);
>> +}
>> +
>> +static void cleanup(void)
>> +{
>> +	if (dev_fd > 0)
>> +		SAFE_CLOSE(dev_fd);
>> +	if (attach_flag)
>> +		tst_detach_device(dev_path);
>> +}
>> +
>> +static struct tst_test test = {
>> +	.setup = setup,
>> +	.cleanup = cleanup,
>> +	.test_all = verify_ioctl_loop,
>> +	.needs_root = 1,
>> +	.needs_cmds = (const char *const []) {
>> +		"dd",
>> +		"parted",
>> +		NULL
>> +	},
>> +	.needs_drivers = (const char *const []) {
>> +		"loop",
>> +		NULL
>> +	},
>> +	.needs_tmpdir = 1,
>> +};
>> -- 
>> 2.23.0
>>
>>
>>
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp
> 


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

* [LTP] [PATCH v1 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test
  2020-04-03 13:34   ` Cyril Hrubis
@ 2020-04-06  7:24     ` Yang Xu
  0 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-06  7:24 UTC (permalink / raw)
  To: ltp

Hi Cyril

> Hi!
>> For LOOP_CHANGE_FD, this operation is possible only if the loop device
>> is read-only and the new backing store is the same size and type as the
>> old backing store.
>>
>> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> ---
>>   runtest/syscalls                              |   1 +
>>   testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
>>   .../kernel/syscalls/ioctl/ioctl_loop02.c      | 148 ++++++++++++++++++
>>   3 files changed, 150 insertions(+)
>>   create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop02.c
>>
>> diff --git a/runtest/syscalls b/runtest/syscalls
>> index 7fd67450e..719953497 100644
>> --- a/runtest/syscalls
>> +++ b/runtest/syscalls
>> @@ -528,6 +528,7 @@ ioctl07      ioctl07
>>   ioctl08      ioctl08
>>   
>>   ioctl_loop01 ioctl_loop01
>> +ioctl_loop02 ioctl_loop02
>>   
>>   ioctl_ns01 ioctl_ns01
>>   ioctl_ns02 ioctl_ns02
>> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
>> index 4cfef2839..534c4ab34 100644
>> --- a/testcases/kernel/syscalls/ioctl/.gitignore
>> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
>> @@ -7,6 +7,7 @@
>>   /ioctl07
>>   /ioctl08
>>   /ioctl_loop01
>> +/ioctl_loop02
>>   /ioctl_ns01
>>   /ioctl_ns02
>>   /ioctl_ns03
>> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop02.c b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
>> new file mode 100644
>> index 000000000..ba3636aa9
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
>> @@ -0,0 +1,148 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>> + * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
>> + *
>> + * This is a basic ioctl test about loopdevice.
>> + *
>> + * It is designed to test LO_FLAGS_READ_ONLY(similar as losetup -r)
>> + * and LOOP_CHANGE_FD.
>> + *
>> + * For LOOP_CHANGE_FD, this operation is possible only if the loop device
>> + * is read-only and the new backing store is the same size and type as the
>> + * old backing store.
>> + */
>> +
>> +#include <stdio.h>
>> +#include <unistd.h>
>> +#include <string.h>
>> +#include "ioctl_loop_support.h"
>> +#include "lapi/loop.h"
>> +#include "tst_test.h"
>> +
>> +static int file_fd, file_change_fd, file_fd_invalid;
>> +static char backing_path[1024], backing_file_path[1024], backing_file_change_path[1024];
>> +static int attach_flag, dev_fd, file_fd;
>> +static char loop_ro_path[1024], dev_path[1024];
>> +static void attach_device(char *);
> 
> Just move the function here, there is no point in having the helper
> function after the test function.
>
OK.

>> +static void verify_ioctl_loop(void)
>> +{
>> +	struct loop_info loopinfoget;
>> +
>> +	attach_device("test.img");
>> +	attach_flag = 1;
>> +
>> +	check_sys_value(loop_ro_path, 1);
>> +	check_sys_string(backing_path, backing_file_path);
>> +
>> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
>> +	file_change_fd = SAFE_OPEN("test1.img", O_RDWR);
>> +	file_fd_invalid = SAFE_OPEN("test2.img", O_RDWR);
>> +
>> +	memset(&loopinfoget, 0, sizeof(loopinfoget));
>> +	/*
>> +	 *In drivers/block/loop.c code, set status function doesn't handle
>> +	 *LO_FLAGS_READ_ONLY flag and ignore it. Only loop_set_fd with rondonly
>                                                                         ^
> 								       readonly
>> +	 *mode, lo_flags will include LO_FLAGS_READ_ONLY.
>> +	 */
> 
> Also it's customary to add a space between the asterisk and the comment
> text.
> 
OK.
>> +	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
>> +
>> +	if (loopinfoget.lo_flags & ~LO_FLAGS_READ_ONLY)
>> +		tst_res(TFAIL, "lo_flags has unexpected %d flag", loopinfoget.lo_flags);
>> +	else
>> +		tst_res(TPASS, "lo_flags only has default LO_FLAGS_READ_ONLY flag");
>> +
>> +	TEST(write(dev_fd, "xx", 2));
>> +	if (TST_RET != -1)
>> +		tst_res(TFAIL, "write succeed unexpectedly");
>> +	else
>> +		tst_res(TPASS | TTERRNO, "Can not write data in RO mode");
>> +
>> +	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_change_fd));
>> +	if (TST_RET) {
>> +		tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed");
>> +	} else {
>> +		tst_res(TPASS, "LOOP_CHANGE_FD succeeded");
>> +		check_sys_value(loop_ro_path, 1);
>> +		check_sys_string(backing_path, backing_file_change_path);
>> +	}
>> +
>> +	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_fd_invalid));
>> +	if (TST_RET) {
>> +		if (TST_ERR == EINVAL)
>> +			tst_res(TPASS | TTERRNO, "LOOP_CHANGE_FD failed as expected");
>> +		else
>> +			tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed expected EINVAL got");
>> +	} else {
>> +		tst_res(TFAIL, "LOOP_CHANGE_FD succeeded");
>> +	}
>> +
>> +	SAFE_CLOSE(dev_fd);
>> +	SAFE_CLOSE(file_change_fd);
>> +	SAFE_CLOSE(file_fd_invalid);
>> +	tst_detach_device(dev_path);
>> +	attach_flag = 0;
>> +}
>> +
>> +/*
>> + * This function is different from tst_attach_dev lib/tst_device.c.
>> + * It only opens file_path with O_RDONLY mode before loop_set_fd.
>> + * So we can generate LO_FLAGS_READ_ONLY flag.
>> + */
>> +static void attach_device(char *img)
>> +{
>> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
>> +	file_fd = SAFE_OPEN(img, O_RDONLY);
>> +
>> +	SAFE_IOCTL(dev_fd, LOOP_SET_FD, file_fd);
>> +
>> +	SAFE_CLOSE(dev_fd);
>> +	SAFE_CLOSE(file_fd);
>> +}
>> +
>> +static void setup(void)
>> +{
>> +	int dev_num;
>> +
>> +	char *tmpdir = tst_get_tmpdir();
>> +	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
>> +	if (dev_num < 0)
>> +		tst_brk(TBROK, "Failed to find free loop device");
>> +
>> +	tst_fill_file("test.img", 0, 1024, 10);
>> +	tst_fill_file("test1.img", 0, 1024, 10);
>> +	tst_fill_file("test2.img", 0, 2048, 20);
>> +
>> +	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
>> +	sprintf(backing_file_path, "%s/test.img", tmpdir);
>> +	sprintf(backing_file_change_path, "%s/test1.img", tmpdir);
>> +	sprintf(loop_ro_path, "/sys/block/loop%d/ro", dev_num);
> 
> We should free the tmpdir here.
Yes, sorry for forgetting this.
> 
>> +}
>> +
>> +static void cleanup(void)
>> +{
>> +	if (dev_fd > 0)
>> +		SAFE_CLOSE(dev_fd);
>> +	if (file_fd > 0)
>> +		SAFE_CLOSE(file_fd);
>> +	if (file_change_fd > 0)
>> +		SAFE_CLOSE(file_change_fd);
>> +	if (file_fd_invalid > 0)
>> +		SAFE_CLOSE(file_fd_invalid);
>> +	if (attach_flag)
>> +		tst_detach_device(dev_path);
>> +}
>> +
>> +static struct tst_test test = {
>> +	.setup = setup,
>> +	.cleanup = cleanup,
>> +	.test_all = verify_ioctl_loop,
>> +	.needs_root = 1,
>> +	.needs_tmpdir = 1,
>> +	.needs_drivers = (const char *const []) {
>> +		"loop",
>> +		NULL
>> +	}
>> +};
> 


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

* [LTP] [PATCH v1 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO ioctl test
  2020-04-03 16:44   ` Cyril Hrubis
@ 2020-04-06  7:53     ` Yang Xu
  2020-04-06  8:30       ` Cyril Hrubis
  0 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-06  7:53 UTC (permalink / raw)
  To: ltp

Hi Cyril

> Hi!
>> LOOP_SET_DIRECT_IO can updata a live loop device dio mode. It needs the
>> backing file also supports dio mode and the lo_offset is aligned with
>> the logical I/O size.
>>
>> It was introduced into kernel since 4.10
>> commit ab1cb278bc70 ("block: loop: introduce ioctl command of LOOP_SET_DIRECT_IO").
>>
>> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> ---
>>   runtest/syscalls                              |   1 +
>>   testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
>>   .../kernel/syscalls/ioctl/ioctl_loop05.c      | 120 ++++++++++++++++++
>>   3 files changed, 122 insertions(+)
>>   create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop05.c
>>
>> diff --git a/runtest/syscalls b/runtest/syscalls
>> index 6e8d71d44..9644588f3 100644
>> --- a/runtest/syscalls
>> +++ b/runtest/syscalls
>> @@ -531,6 +531,7 @@ ioctl_loop01 ioctl_loop01
>>   ioctl_loop02 ioctl_loop02
>>   ioctl_loop03 ioctl_loop03
>>   ioctl_loop04 ioctl_loop04
>> +ioctl_loop05 ioctl_loop05
>>   
>>   ioctl_ns01 ioctl_ns01
>>   ioctl_ns02 ioctl_ns02
>> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
>> index 039a5251c..f484d98d6 100644
>> --- a/testcases/kernel/syscalls/ioctl/.gitignore
>> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
>> @@ -10,6 +10,7 @@
>>   /ioctl_loop02
>>   /ioctl_loop03
>>   /ioctl_loop04
>> +/ioctl_loop05
>>   /ioctl_ns01
>>   /ioctl_ns02
>>   /ioctl_ns03
>> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop05.c b/testcases/kernel/syscalls/ioctl/ioctl_loop05.c
>> new file mode 100644
>> index 000000000..43bad6c18
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop05.c
>> @@ -0,0 +1,120 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>> + * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
>> + *
>> + * This is a basic ioctl test about loopdevice.
>> + *
>> + * It is designed to test LOOP_SET_DIRECT_IO can updata a live
>> + * loop device dio mode. It need the backing file also supports
>> + * dio mode and the lo_offset is aligned with the logical I/O size.
>> + */
>> +#include <stdio.h>
>> +#include <unistd.h>
>> +#include <string.h>
>> +#include <stdlib.h>
>> +#include "ioctl_loop_support.h"
>> +#include "lapi/loop.h"
>> +#include "tst_test.h"
>> +
>> +#define DIO_MESSAGE "In dio mode"
>> +#define NON_DIO_MESSAGE "In non dio mode"
>> +
>> +static char dev_path[1024], sys_loop_diopath[1024];
>> +static int dev_num, dev_fd, attach_flag;
>> +
>> +static void check_dio_value(int flag)
>> +{
>> +	struct loop_info loopinfoget;
>> +
>> +	memset(&loopinfoget, 0, sizeof(loopinfoget));
>> +
>> +	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
>> +	tst_res(TINFO, "%s", flag ? DIO_MESSAGE : NON_DIO_MESSAGE);
>> +
>> +	if (loopinfoget.lo_flags & LO_FLAGS_DIRECT_IO)
>> +		tst_res(flag ? TPASS : TFAIL, "lo_flags has LO_FLAGS_DIRECT_IO flag");
>> +	else
>> +		tst_res(flag ? TFAIL : TPASS, "lo_flags doesn't have LO_FLAGS_DIRECT_IO flag");
>> +
>> +	check_sys_value(sys_loop_diopath, flag);
>> +}
>> +
>> +static void verify_ioctl_loop(void)
>> +{
>> +	struct loop_info loopinfo;
>> +
>> +	memset(&loopinfo, 0, sizeof(loopinfo));
>> +
>> +	SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
>> +	check_dio_value(0);
>> +	tst_res(TINFO, "Without setting lo_offset or sizelimit");
>> +	SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 1);
>> +	check_dio_value(1);
>> +
>> +	tst_res(TINFO, "With offset equal to sector size");
>> +	loopinfo.lo_offset = 512;
> 
> We should use BLKSSZGET ioctl() to get the block size for direct I/O
> otherwise I would expect that it would break on one of the less common
> architectures. See also discussion at the end of man 2 open.
>
I see. Also, when testing LOOP_SET_BLOCK_SIZE ioctl, we can also call 
this to check whether this ioctl succeed.

>> +	safe_set_status(dev_fd, loopinfo);
>> +	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
>> +	if (TST_RET == 0) {
>> +		tst_res(TPASS, "LOOP_SET_DIRECT_IO succeeded");
>> +		check_dio_value(1);
>> +	} else {
>> +		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed");
>> +	}
>> +
>> +	tst_res(TINFO, "With offset less than or unalign sector size");
>> +	loopinfo.lo_offset = 256;
>> +	safe_set_status(dev_fd, loopinfo);
>> +
>> +	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
>> +	if (TST_RET == 0) {
>> +		tst_res(TFAIL, "LOOP_SET_DIRECT_IO succeeded unexpectedly");
>> +		return;
>> +	}
>> +	if (TST_ERR == EINVAL)
>> +		tst_res(TPASS | TTERRNO, "LOOP_SET_DIRECT_IO failed as expected");
>> +	else
>> +		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed expected EINVAL got");
>> +
>> +	loopinfo.lo_offset = 0;
>> +	safe_set_status(dev_fd, loopinfo);
>> +}
>> +
>> +static void setup(void)
>> +{
>> +	if (tst_fs_type(".") == TST_TMPFS_MAGIC)
>> +		tst_brk(TCONF, "tmpfd doesn't support O_DIRECT flag, skip it");
>> +
>> +	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
>> +	if (dev_num < 0)
>> +		tst_brk(TBROK, "Failed to find free loop device");
>> +
>> +	sprintf(sys_loop_diopath, "/sys/block/loop%d/loop/dio", dev_num);
>> +	tst_fill_file("test.img", 0, 1024, 1024);
>> +	tst_attach_device(dev_path, "test.img");
>> +	attach_flag = 1;
>> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
>> +	check_support_cmd(dev_fd, LOOP_SET_DIRECT_IO, 0, "LOOP_SET_DIRECT_IO");
>> +}
>> +
>> +static void cleanup(void)
>> +{
>> +	if (dev_fd > 0)
>> +		SAFE_CLOSE(dev_fd);
>> +	if (attach_flag)
>> +		tst_detach_device(dev_path);
>> +	unlink("test.img");
> 
> We don't have to remove the image here once the needs_tmpdir has been
> uncommented below, right?
Yes.
> 
>> +}
>> +
>> +static struct tst_test test = {
>> +	.setup = setup,
>> +	.cleanup = cleanup,
>> +	.test_all = verify_ioctl_loop,
>> +	.needs_root = 1,
>> +//	.needs_tmpdir = 1,
> 
> Looks like a forgotten debug measure.
> 
Yes, because some distribution use tmpfs in tmp directory, then this 
case will TCONF(tmpfs doesn't supprt DIO).  I am indecisive, or I should 
  use all_filesystem =1, so it will not report TCONF? Or, keep it?
>> +	.needs_drivers = (const char *const []) {
>> +		"loop",
>> +		NULL
>> +	}
>> +};
>> -- 
>> 2.23.0
>>
>>
>>
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp
> 


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

* [LTP] [PATCH v1 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO ioctl test
  2020-04-06  7:53     ` Yang Xu
@ 2020-04-06  8:30       ` Cyril Hrubis
  0 siblings, 0 replies; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-06  8:30 UTC (permalink / raw)
  To: ltp

Hi!
> >> +}
> >> +
> >> +static struct tst_test test = {
> >> +	.setup = setup,
> >> +	.cleanup = cleanup,
> >> +	.test_all = verify_ioctl_loop,
> >> +	.needs_root = 1,
> >> +//	.needs_tmpdir = 1,
> > 
> > Looks like a forgotten debug measure.
> > 
> Yes, because some distribution use tmpfs in tmp directory, then this 
> case will TCONF(tmpfs doesn't supprt DIO).  I am indecisive, or I should 
>   use all_filesystem =1, so it will not report TCONF? Or, keep it?

That will just create a loop device formatted with a filesystem and
another loop device in that filesystem in most cases. I doubt that it
will even work if we are doing all of that on the top of tmpfs anyways.

-- 
chrubis@suse.cz

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

* [LTP] [PATCH v1 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-03 12:16   ` Cyril Hrubis
@ 2020-04-09  2:42     ` Yang Xu
  0 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09  2:42 UTC (permalink / raw)
  To: ltp

Hi Cyril

> Hi!
>> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> ---
>>   testcases/kernel/syscalls/ioctl/Makefile      |  3 +
>>   .../syscalls/ioctl/ioctl_loop_support.c       | 74 +++++++++++++++++++
>>   .../syscalls/ioctl/ioctl_loop_support.h       | 14 ++++
>>   3 files changed, 91 insertions(+)
>>   create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
>>   create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
>>
>> diff --git a/testcases/kernel/syscalls/ioctl/Makefile b/testcases/kernel/syscalls/ioctl/Makefile
>> index c2ff6c8e7..05a75d1b4 100644
>> --- a/testcases/kernel/syscalls/ioctl/Makefile
>> +++ b/testcases/kernel/syscalls/ioctl/Makefile
>> @@ -7,6 +7,9 @@ include $(top_srcdir)/include/mk/testcases.mk
>>   
>>   INSTALL_TARGETS		+= test_ioctl
>>   
>> +MAKE_TARGETS            := $(patsubst $(abs_srcdir)/%.c,%,$(wildcard $(abs_srcdir)/ioctl_loop[0-9]*.c))
>> +$(MAKE_TARGETS): %: ioctl_loop_support.o
> 
> I guess that we should use anything else than MAKE_TARGETS because
> changing that variable will disable rest of the ioctl tests from build
> right?
> 
Yes, you are right.
> As this only expresses dependency on the object file we should use any
> variable name that is not used by the test library itself.
I think the following code maybe ok.
+FILTER_OUT_MAKE_TARGETS         := ioctl_loop_support
+
+LOOP_TARGETS            := $(patsubst $(abs_srcdir)/%.c,%,$(wildcard 
$(abs_srcdir)/ioctl_loop*[0-9].c))
+$(LOOP_TARGETS): %: ioctl_loop_support.o


> 
>>   ifeq ($(ANDROID),1)
>>   FILTER_OUT_MAKE_TARGETS	+= ioctl02
>>   endif
>> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
>> new file mode 100644
>> index 000000000..4099bd364
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
>> @@ -0,0 +1,74 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> + */
>> +#define TST_NO_DEFAULT_MAIN
>> +#include "ioctl_loop_support.h"
>> +#include "tst_test.h"
>> +
>> +void check_sys_value(char *path, int setvalue)
>> +{
>> +	int getvalue;
>> +
>> +	SAFE_FILE_SCANF(path, "%d", &getvalue);
>> +	if (setvalue == getvalue)
>> +		tst_res(TPASS, "%s value is %d", path, setvalue);
>> +	else
>> +		tst_res(TFAIL, "%s value expected %d got %d", path, setvalue, getvalue);
>> +}
>> +
>> +void check_sys_string(char *path, char *setmessage)
>> +{
>> +	char getmessage[1024];
>> +
>> +	SAFE_FILE_SCANF(path, "%s", getmessage);
>> +	if (strcmp(setmessage, getmessage))
>> +		tst_res(TFAIL, "%s expected %s got %s", path, setmessage, getmessage);
>> +	else
>> +		tst_res(TPASS, "%s string is %s", path, getmessage);
>> +}
>> +
>> +void safe_set_status(int dev_fd, struct loop_info loopinfo)
>> +{
>> +	int sleep_us = 4096;
>> +	int ret = 0;
>> +
>> +	/*
>> +	 * It may have dirty page, so loop dirver may get EAGAIN error
>> +	 * when we use different offset or sizelimit.
>> +	 */
>> +	ret = ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo);
>> +	while (ret != 0 && errno == EAGAIN && sleep_us < 100000) {
>> +		ret = ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo);
>> +		usleep(sleep_us);
>> +		sleep_us *= 2;
>> +	}
> 
> TBROK here if we failed to set the status?
Yes.
> 
> Also we should use the TST_RETRY_FUNC() instead because that one uses
> the timeout multiplier environment variable.
Ok, I will use TST_RETRY_FUNC macro.
> 
>> +}
>> +
>> +void safe_set_status64(int dev_fd, struct loop_info64 loopinfo)
>> +{
>> +	int sleep_us = 4096;
>> +	int ret = 0;
>> +
>> +	/*
>> +	 * It may have dirty page, so loop dirver may get EAGAIN error
>> +	 * when we use different offset or sizelimit.
>> +	 */
>> +	ret = ioctl(dev_fd, LOOP_SET_STATUS64, &loopinfo);
>> +	while (ret != 0 && errno == EAGAIN && sleep_us < 100000) {
>> +		ret = ioctl(dev_fd, LOOP_SET_STATUS64, &loopinfo);
>> +		usleep(sleep_us);
>> +		sleep_us *= 2;
>> +	}
> 
> Here as well.

> 
>> +}
>> +
>> +void check_support_cmd(int dev_fd, int ioctl_flag, int value, char *message)
>> +{
>> +	int ret = 0;
>> +
>> +	ret = ioctl(dev_fd, ioctl_flag, value);
>> +	if (ret && errno == EINVAL)
>> +		tst_brk(TCONF, "Current environment doesn't support this flag(%s)",
>> +				message);
>> +}
>> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
>> new file mode 100644
>> index 000000000..44445af8a
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
>> @@ -0,0 +1,14 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> + */
>> +#ifndef IOCTL_LOOP_H
>> +#define IOCTL_lOOP_H
>> +#include <linux/loop.h>
>> +void check_sys_value(char *path, int setvalue);
>> +void check_sys_string(char *path, char *setmessage);
>> +void safe_set_status(int dev_fd, struct loop_info loopinfo);
>> +void safe_set_status64(int dev_fd, struct loop_info64 loopinfo);
>> +void check_support_cmd(int dev_fd, int ioctl_flag, int value, char *message);
>> +#endif
>> -- 
>> 2.23.0
>>
>>
>>
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp
> 



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

* [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test
  2020-04-06  6:27     ` Yang Xu
@ 2020-04-09  6:10       ` Yang Xu
  2020-04-09  7:55         ` Cyril Hrubis
  0 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-09  6:10 UTC (permalink / raw)
  To: ltp

Hi Cyril

> Hi Cyril
> 
>> Hi!
>>> For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
>>> directory and also get lo_flags by using LOOP_GET_STATUS.
>>>
>>> For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
>>> But we also check whether we can scan partition table correctly ie check
>>> whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
>>>
>>> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>>> ---
>>> ? runtest/syscalls????????????????????????????? |?? 3 +-
>>> ? testcases/kernel/syscalls/ioctl/.gitignore??? |?? 1 +
>>> ? .../kernel/syscalls/ioctl/ioctl_loop01.c????? | 127 ++++++++++++++++++
>>> ? 3 files changed, 130 insertions(+), 1 deletion(-)
>>> ? create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop01.c
>>>
>>> diff --git a/runtest/syscalls b/runtest/syscalls
>>> index 777f0ac6a..7fd67450e 100644
>>> --- a/runtest/syscalls
>>> +++ b/runtest/syscalls
>>> @@ -524,10 +524,11 @@ ioctl03????? ioctl03
>>> ? ioctl04????? ioctl04
>>> ? ioctl05????? ioctl05
>>> ? ioctl06????? ioctl06
>>> -
>>> ? ioctl07????? ioctl07
>>> ? ioctl08????? ioctl08
>>> +ioctl_loop01 ioctl_loop01
>>> +
>>> ? ioctl_ns01 ioctl_ns01
>>> ? ioctl_ns02 ioctl_ns02
>>> ? ioctl_ns03 ioctl_ns03
>>> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore 
>>> b/testcases/kernel/syscalls/ioctl/.gitignore
>>> index 97fbb9681..4cfef2839 100644
>>> --- a/testcases/kernel/syscalls/ioctl/.gitignore
>>> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
>>> @@ -6,6 +6,7 @@
>>> ? /ioctl06
>>> ? /ioctl07
>>> ? /ioctl08
>>> +/ioctl_loop01
>>> ? /ioctl_ns01
>>> ? /ioctl_ns02
>>> ? /ioctl_ns03
>>> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop01.c 
>>> b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
>>> new file mode 100644
>>> index 000000000..e806e0443
>>> --- /dev/null
>>> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
>>> @@ -0,0 +1,127 @@
>>> +// SPDX-License-Identifier: GPL-2.0-or-later
>>> +/*
>>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>>> + * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
>>> + *
>>> + * This is a basic ioctl test about loopdevice.
>>> + * It is designed to test LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN 
>>> flag.
>>> + *
>>> + * For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue 
>>> in sys
>>> + * directory and also get lo_flags by using LOOP_GET_STATUS.
>>> + *
>>> + * For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR 
>>> flag.
>>> + * But we also check whether we can scan partition table correctly 
>>> ie check
>>> + * whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
>>> + */
>>> +
>>> +#include <stdio.h>
>>> +#include <unistd.h>
>>> +#include <string.h>
>>> +#include "ioctl_loop_support.h"
>>> +#include "lapi/loop.h"
>>> +#include "tst_test.h"
>>> +
>>> +static char dev_path[1024], backing_path[1024], 
>>> backing_file_path[1024];
>>> +static int dev_num, attach_flag, dev_fd;
>>> +/*
>>> + *In drivers/block/loop.c code, set status function doesn't handle
>>> + *LO_FLAGS_READ_ONLY flag and ingore it. Only loop_set_fd with rondonly
>>> + *mode file_fd, lo_flags will include LO_FLAGS_READ_ONLY.
>>> + */
>>> +#define set_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN | 
>>> LO_FLAGS_READ_ONLY | LO_FLAGS_DIRECT_IO)
>>> +#define get_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
>>> +
>>> +static char partscan_path[1024], autoclear_path[1024];
>>> +static char loop_partpath[1026], sys_loop_partpath[1026];
>>> +
>>> +static void verify_ioctl_loop(void)
>>> +{
>>> +??? int ret;
>>> +??? struct loop_info loopinfo, loopinfoget;
>>> +
>>> +??? tst_attach_device(dev_path, "test.img");
>>> +??? attach_flag = 1;
>>> +
>>> +??? check_sys_value(partscan_path, 0);
>>> +??? check_sys_value(autoclear_path, 0);
>>> +??? check_sys_string(backing_path, backing_file_path);
>>> +
>>> +??? dev_fd = SAFE_OPEN(dev_path, O_RDWR);
>>> +??? memset(&loopinfo, 0, sizeof(loopinfo));
>>> +??? memset(&loopinfo, 0, sizeof(loopinfoget));
>>> +
>>> +??? loopinfo.lo_flags = set_flags;
>>> +??? SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
>>
>> I guess that it would be safer to memset() the loopinfo_get here, also
>> if we zero the structure we may as well reuse the loopinfo here as well.
>>
> of coure, I will add memset in here.
> 
>>> +??? SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
>>> +
>>> +??? if (loopinfoget.lo_flags & ~get_flags)
>>> +??????? tst_res(TFAIL, "expect %d but got %d", get_flags, 
>>> loopinfoget.lo_flags);
>>> +??? else
>>> +??????? tst_res(TPASS, "get expected lo_flag %d", 
>>> loopinfoget.lo_flags);
>>> +
>>> +??? ret = access(loop_partpath, F_OK);
>>> +??? if (ret == 0)
>>> +??????? tst_res(TPASS, "access %s succeeds", loop_partpath);
>>> +??? else
>>> +??????? tst_res(TFAIL, "access %s fails", loop_partpath);
>>> +
>>> +??? ret = access(sys_loop_partpath, F_OK);
>>> +??? if (ret == 0)
>>> +??????? tst_res(TPASS, "access %s succeeds", sys_loop_partpath);
>>> +??? else
>>> +??????? tst_res(TFAIL, "access %s fails", sys_loop_partpath);
>>> +
>>> +??? check_sys_value(partscan_path, 1);
>>> +??? check_sys_value(autoclear_path, 1);
>>> +
>>> +??? SAFE_CLOSE(dev_fd);
>>> +??? tst_detach_device(dev_path);
>>> +??? attach_flag = 0;
>>> +}
>>> +
>>> +static void setup(void)
>>> +{
>>> +??? const char *const cmd_dd[] = {"dd", "if=/dev/zero", 
>>> "of=test.img", "bs=1M", "count=10", NULL};
>>> +??? const char *const cmd_parted[] = {"parted", "-s", "test.img", 
>>> "mklabel", "msdos", "mkpart",
>>> +??????????????????????? "primary", "ext4", "1M", "10M", NULL};
>>> +
>>> +??? dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
>>> +??? if (dev_num < 0)
>>> +??????? tst_brk(TBROK, "Failed to find free loop device");
>>> +
>>> +??? SAFE_CMD(cmd_dd, NULL, NULL);
>>
>> Why not tst_fill_file("test.img", 0, 1024 * 1024, 10); ?
>>
> I will try this.

> 
>>> +??? SAFE_CMD(cmd_parted, NULL, NULL);
>>
>> I wonder if we can avoid dependency on parted by having a look at the
>> changes in the image after the call to parted and writing a few bytes
>> ourselves instead.
> I will see kernel code to figure out this, if no other good way, we can 
> split this check into a new single test. it can make this case less 
> couping.
Sorry for misunderstanding,.  I prefer to add a parted test.img(10K is 
ok on my environment) into ltp or add a flag to skip this 
check(/dev/loop0p1) when parted command is not available.  What do you 
think about the two ways?

Best Regards
Yang Xu
>>
>>> +??? sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
>>> +??? sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", 
>>> dev_num);
>>> +??? sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", 
>>> dev_num);
>>> +??? 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);
>>> +}
>>> +
>>> +static void cleanup(void)
>>> +{
>>> +??? if (dev_fd > 0)
>>> +??????? SAFE_CLOSE(dev_fd);
>>> +??? if (attach_flag)
>>> +??????? tst_detach_device(dev_path);
>>> +}
>>> +
>>> +static struct tst_test test = {
>>> +??? .setup = setup,
>>> +??? .cleanup = cleanup,
>>> +??? .test_all = verify_ioctl_loop,
>>> +??? .needs_root = 1,
>>> +??? .needs_cmds = (const char *const []) {
>>> +??????? "dd",
>>> +??????? "parted",
>>> +??????? NULL
>>> +??? },
>>> +??? .needs_drivers = (const char *const []) {
>>> +??????? "loop",
>>> +??????? NULL
>>> +??? },
>>> +??? .needs_tmpdir = 1,
>>> +};
>>> -- 
>>> 2.23.0
>>>
>>>
>>>
>>>
>>> -- 
>>> Mailing list info: https://lists.linux.it/listinfo/ltp
>>
> 
> 
> 



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

* [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test
  2020-04-09  6:10       ` Yang Xu
@ 2020-04-09  7:55         ` Cyril Hrubis
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-09  7:55 UTC (permalink / raw)
  To: ltp

Hi!
> >> I wonder if we can avoid dependency on parted by having a look at the
> >> changes in the image after the call to parted and writing a few bytes
> >> ourselves instead.
> > I will see kernel code to figure out this, if no other good way, we can 
> > split this check into a new single test. it can make this case less 
> > couping.
> Sorry for misunderstanding,.  I prefer to add a parted test.img(10K is 
> ok on my environment) into ltp or add a flag to skip this 
> check(/dev/loop0p1) when parted command is not available.  What do you 
> think about the two ways?

I it couldn't be easily done without parted then it's fine if the test
depends on it.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag
  2020-04-02 15:06 ` [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag Yang Xu
  2020-04-03 11:44   ` Cyril Hrubis
@ 2020-04-09  8:47   ` Martin Doucha
  2020-04-09 10:09     ` Cyril Hrubis
  2020-04-09 10:14     ` Yang Xu
  1 sibling, 2 replies; 58+ messages in thread
From: Martin Doucha @ 2020-04-09  8:47 UTC (permalink / raw)
  To: ltp

Hi,

On 02. 04. 20 17:06, Yang Xu wrote:
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
>  configure.ac        |  1 +
>  include/lapi/loop.h | 35 +++++++++++++++++++++++++++++++++++
>  2 files changed, 36 insertions(+)
>  create mode 100644 include/lapi/loop.h
> 
> diff --git a/configure.ac b/configure.ac
> index e1069b57e..e088cb564 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -50,6 +50,7 @@ AC_CHECK_HEADERS([ \
>      linux/if_ether.h \
>      linux/if_packet.h \
>      linux/keyctl.h \
> +    linux/loop.h \
>      linux/mempolicy.h \
>      linux/module.h \
>      linux/netlink.h \
> diff --git a/include/lapi/loop.h b/include/lapi/loop.h
> new file mode 100644
> index 000000000..bc6d9950d
> --- /dev/null
> +++ b/include/lapi/loop.h
> @@ -0,0 +1,35 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.

Here's a little legal issue for LTP: "GPL" and "All rights reserved" are
mutually exclusive. Those two lines are effectively saying that your
code is both open-source and proprietary at the same time. Please pick
one, or the other. It can't be both.

> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + */
> +#ifndef LAPI_LOOP_H
> +#define LAPI_LOOP_H
> +
> +#include <linux/types.h>
> +#ifdef HAVE_LINUX_LOOP_H
> +# include <linux/loop.h>
> +#endif
> +
> +#ifndef LO_FLAGS_PARTSCAN
> +# define LO_FLAGS_PARTSCAN 8
> +#endif
> +
> +#ifndef LO_FLAGS_DIRECT_IO
> +# define LO_FLAGS_DIRECT_IO 16
> +#endif
> +
> +#ifndef LOOP_SET_CAPACITY
> +# define LOOP_SET_CAPACITY 0x4C07
> +#endif
> +
> +#ifndef LOOP_SET_DIRECT_IO
> +# define LOOP_SET_DIRECT_IO 0x4C08
> +#endif
> +
> +#ifndef LOOP_SET_BLOCK_SIZE
> +# define LOOP_SET_BLOCK_SIZE 0x4C09
> +#endif
> +
> +#endif
> +
> 


-- 
Martin Doucha   mdoucha@suse.cz
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

* [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag
  2020-04-09  8:47   ` Martin Doucha
@ 2020-04-09 10:09     ` Cyril Hrubis
  2020-04-09 10:14     ` Yang Xu
  1 sibling, 0 replies; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-09 10:09 UTC (permalink / raw)
  To: ltp

Hi!
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*
> > + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> 
> Here's a little legal issue for LTP: "GPL" and "All rights reserved" are
> mutually exclusive. Those two lines are effectively saying that your
> code is both open-source and proprietary at the same time. Please pick
> one, or the other. It can't be both.

Actually the "All rights reserved" should not have any legal effects.
The Buenos Aires Convention required that text for estabilishing
copyright which you would have needed before you could license your work
under GPL. However it's long obsolete now and has no legal effect as far
as I can tell.

Btw "All rights reserved" is part of the BSD MIT license because of
these reasons.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag
  2020-04-09  8:47   ` Martin Doucha
  2020-04-09 10:09     ` Cyril Hrubis
@ 2020-04-09 10:14     ` Yang Xu
  1 sibling, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:14 UTC (permalink / raw)
  To: ltp

Hi Martin

> Hi,
> 
> On 02. 04. 20 17:06, Yang Xu wrote:
>> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> ---
>>   configure.ac        |  1 +
>>   include/lapi/loop.h | 35 +++++++++++++++++++++++++++++++++++
>>   2 files changed, 36 insertions(+)
>>   create mode 100644 include/lapi/loop.h
>>
>> diff --git a/configure.ac b/configure.ac
>> index e1069b57e..e088cb564 100644
>> --- a/configure.ac
>> +++ b/configure.ac
>> @@ -50,6 +50,7 @@ AC_CHECK_HEADERS([ \
>>       linux/if_ether.h \
>>       linux/if_packet.h \
>>       linux/keyctl.h \
>> +    linux/loop.h \
>>       linux/mempolicy.h \
>>       linux/module.h \
>>       linux/netlink.h \
>> diff --git a/include/lapi/loop.h b/include/lapi/loop.h
>> new file mode 100644
>> index 000000000..bc6d9950d
>> --- /dev/null
>> +++ b/include/lapi/loop.h
>> @@ -0,0 +1,35 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> 
> Here's a little legal issue for LTP: "GPL" and "All rights reserved" are
> mutually exclusive. Those two lines are effectively saying that your
> code is both open-source and proprietary at the same time. Please pick
> one, or the other. It can't be both.
I never think this is a problem before. ltp has some cases also using 
this style. Also, xfstests have many cases like this.  Even kernel, it 
also has the two line. Is it a problem?

Best Regards
Yang Xu
> 
>> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> + */
>> +#ifndef LAPI_LOOP_H
>> +#define LAPI_LOOP_H
>> +
>> +#include <linux/types.h>
>> +#ifdef HAVE_LINUX_LOOP_H
>> +# include <linux/loop.h>
>> +#endif
>> +
>> +#ifndef LO_FLAGS_PARTSCAN
>> +# define LO_FLAGS_PARTSCAN 8
>> +#endif
>> +
>> +#ifndef LO_FLAGS_DIRECT_IO
>> +# define LO_FLAGS_DIRECT_IO 16
>> +#endif
>> +
>> +#ifndef LOOP_SET_CAPACITY
>> +# define LOOP_SET_CAPACITY 0x4C07
>> +#endif
>> +
>> +#ifndef LOOP_SET_DIRECT_IO
>> +# define LOOP_SET_DIRECT_IO 0x4C08
>> +#endif
>> +
>> +#ifndef LOOP_SET_BLOCK_SIZE
>> +# define LOOP_SET_BLOCK_SIZE 0x4C09
>> +#endif
>> +
>> +#endif
>> +
>>
> 
> 



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

* [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag
  2020-04-09  7:55         ` Cyril Hrubis
@ 2020-04-09 10:44           ` Yang Xu
  2020-04-09 10:44             ` [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
                               ` (11 more replies)
  0 siblings, 12 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 include/lapi/loop.h | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 include/lapi/loop.h

diff --git a/include/lapi/loop.h b/include/lapi/loop.h
new file mode 100644
index 000000000..bc6d9950d
--- /dev/null
+++ b/include/lapi/loop.h
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+#ifndef LAPI_LOOP_H
+#define LAPI_LOOP_H
+
+#include <linux/types.h>
+#ifdef HAVE_LINUX_LOOP_H
+# include <linux/loop.h>
+#endif
+
+#ifndef LO_FLAGS_PARTSCAN
+# define LO_FLAGS_PARTSCAN 8
+#endif
+
+#ifndef LO_FLAGS_DIRECT_IO
+# define LO_FLAGS_DIRECT_IO 16
+#endif
+
+#ifndef LOOP_SET_CAPACITY
+# define LOOP_SET_CAPACITY 0x4C07
+#endif
+
+#ifndef LOOP_SET_DIRECT_IO
+# define LOOP_SET_DIRECT_IO 0x4C08
+#endif
+
+#ifndef LOOP_SET_BLOCK_SIZE
+# define LOOP_SET_BLOCK_SIZE 0x4C09
+#endif
+
+#endif
+
-- 
2.23.0




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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-17 15:10               ` Cyril Hrubis
  2020-04-09 10:44             ` [LTP] [PATCH v2 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
                               ` (10 subsequent siblings)
  11 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/ioctl/Makefile      |  5 +++
 .../syscalls/ioctl/ioctl_loop_support.c       | 40 +++++++++++++++++++
 .../syscalls/ioctl/ioctl_loop_support.h       | 12 ++++++
 3 files changed, 57 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.h

diff --git a/testcases/kernel/syscalls/ioctl/Makefile b/testcases/kernel/syscalls/ioctl/Makefile
index c2ff6c8e7..7bdc7daf0 100644
--- a/testcases/kernel/syscalls/ioctl/Makefile
+++ b/testcases/kernel/syscalls/ioctl/Makefile
@@ -7,6 +7,11 @@ include $(top_srcdir)/include/mk/testcases.mk
 
 INSTALL_TARGETS		+= test_ioctl
 
+FILTER_OUT_MAKE_TARGETS += ioctl_loop_support
+
+LOOP_TARGETS            := $(patsubst $(abs_srcdir)/%.c,%,$(wildcard $(abs_srcdir)/ioctl_loop[0-9]*.c))
+$(LOOP_TARGETS): %: ioctl_loop_support.o
+
 ifeq ($(ANDROID),1)
 FILTER_OUT_MAKE_TARGETS	+= ioctl02
 endif
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
new file mode 100644
index 000000000..75c7b1b9a
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+#define TST_NO_DEFAULT_MAIN
+#include "ioctl_loop_support.h"
+#include "tst_test.h"
+
+void check_sys_value(char *path, int setvalue)
+{
+	int getvalue;
+
+	SAFE_FILE_SCANF(path, "%d", &getvalue);
+	if (setvalue == getvalue)
+		tst_res(TPASS, "%s value is %d", path, setvalue);
+	else
+		tst_res(TFAIL, "%s value expected %d got %d", path, setvalue, getvalue);
+}
+
+void check_sys_string(char *path, char *setmessage)
+{
+	char getmessage[1024];
+
+	SAFE_FILE_SCANF(path, "%s", getmessage);
+	if (strcmp(setmessage, getmessage))
+		tst_res(TFAIL, "%s expected %s got %s", path, setmessage, getmessage);
+	else
+		tst_res(TPASS, "%s string is %s", path, getmessage);
+}
+
+void check_support_cmd(int dev_fd, int ioctl_flag, int value, char *message)
+{
+	int ret = 0;
+
+	ret = ioctl(dev_fd, ioctl_flag, value);
+	if (ret && errno == EINVAL)
+		tst_brk(TCONF, "Current environment doesn't support this flag(%s)",
+				message);
+}
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
new file mode 100644
index 000000000..ade64e82e
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop_support.h
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+#ifndef IOCTL_LOOP_H
+#define IOCTL_lOOP_H
+#include <linux/loop.h>
+void check_sys_value(char *path, int setvalue);
+void check_sys_string(char *path, char *setmessage);
+void check_support_cmd(int dev_fd, int ioctl_flag, int value, char *message);
+#endif
-- 
2.23.0




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

* [LTP] [PATCH v2 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
  2020-04-09 10:44             ` [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-09 10:44             ` [LTP] [PATCH v2 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test Yang Xu
                               ` (9 subsequent siblings)
  11 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
directory and also get lo_flags by using LOOP_GET_STATUS.

For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
But we also check whether we can scan partition table correctly ie check
whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |   3 +-
 testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 140 ++++++++++++++++++
 3 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop01.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 44254d7da..8d1ce0c9a 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -525,10 +525,11 @@ ioctl03      ioctl03
 ioctl04      ioctl04
 ioctl05      ioctl05
 ioctl06      ioctl06
-
 ioctl07      ioctl07
 ioctl08      ioctl08
 
+ioctl_loop01 ioctl_loop01
+
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
 ioctl_ns03 ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 97fbb9681..4cfef2839 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -6,6 +6,7 @@
 /ioctl06
 /ioctl07
 /ioctl08
+/ioctl_loop01
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop01.c b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
new file mode 100644
index 000000000..fb71b7b1e
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
@@ -0,0 +1,140 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ * It is designed to test LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN flag.
+ *
+ * For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
+ * directory and also get lo_flags by using LOOP_GET_STATUS.
+ *
+ * For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
+ * But we also check whether we can scan partition table correctly ie check
+ * whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024], backing_path[1024], backing_file_path[1024];
+static int dev_num, attach_flag, dev_fd, parted_sup;
+/*
+ *In drivers/block/loop.c code, set status function doesn't handle
+ *LO_FLAGS_READ_ONLY flag and ingore it. Only loop_set_fd with rondonly
+ *mode file_fd, lo_flags will include LO_FLAGS_READ_ONLY.
+ */
+#define set_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN | LO_FLAGS_READ_ONLY | LO_FLAGS_DIRECT_IO)
+#define get_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
+
+static char partscan_path[1024], autoclear_path[1024];
+static char loop_partpath[1026], sys_loop_partpath[1026];
+
+static void verify_ioctl_loop(void)
+{
+	int ret;
+	struct loop_info loopinfo, loopinfoget;
+
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	check_sys_value(partscan_path, 0);
+	check_sys_value(autoclear_path, 0);
+	check_sys_string(backing_path, backing_file_path);
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	memset(&loopinfo, 0, sizeof(loopinfo));
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+
+	loopinfo.lo_flags = set_flags;
+	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
+
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
+
+	if (loopinfoget.lo_flags & ~get_flags)
+		tst_res(TFAIL, "expect %d but got %d", get_flags, loopinfoget.lo_flags);
+	else
+		tst_res(TPASS, "get expected lo_flag %d", loopinfoget.lo_flags);
+
+	check_sys_value(partscan_path, 1);
+	check_sys_value(autoclear_path, 1);
+
+	if (!parted_sup) {
+		tst_res(TINFO, "Current environment doesn't have parted disk, skip it");
+		goto detach_device;
+	}
+
+	ret = access(loop_partpath, F_OK);
+	if (ret == 0)
+		tst_res(TPASS, "access %s succeeds", loop_partpath);
+	else
+		tst_res(TFAIL, "access %s fails", loop_partpath);
+
+	ret = access(sys_loop_partpath, F_OK);
+	if (ret == 0)
+		tst_res(TPASS, "access %s succeeds", sys_loop_partpath);
+	else
+		tst_res(TFAIL, "access %s fails", sys_loop_partpath);
+
+detach_device:
+	SAFE_CLOSE(dev_fd);
+	tst_detach_device(dev_path);
+	attach_flag = 0;
+}
+
+static void setup(void)
+{
+	int ret;
+	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
+						"primary", "ext4", "1M", "10M", NULL};
+
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
+	switch (ret) {
+	case 0:
+		parted_sup = 1;
+	break;
+	case 255:
+		tst_res(TCONF, "parted binary not installed or failed");
+	break;
+	default:
+		tst_res(TCONF, "parted exited with %i", ret);
+	break;
+	}
+
+	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
+	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
+	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
+	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);
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	},
+	.needs_tmpdir = 1,
+};
-- 
2.23.0




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

* [LTP] [PATCH v2 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
  2020-04-09 10:44             ` [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
  2020-04-09 10:44             ` [LTP] [PATCH v2 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-09 10:44             ` [LTP] [PATCH v2 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode Yang Xu
                               ` (8 subsequent siblings)
  11 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

For LOOP_CHANGE_FD, this operation is possible only if the loop device
is read-only and the new backing store is the same size and type as the
old backing store.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
 .../kernel/syscalls/ioctl/ioctl_loop02.c      | 150 ++++++++++++++++++
 3 files changed, 152 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 8d1ce0c9a..80005227d 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -529,6 +529,7 @@ ioctl07      ioctl07
 ioctl08      ioctl08
 
 ioctl_loop01 ioctl_loop01
+ioctl_loop02 ioctl_loop02
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 4cfef2839..534c4ab34 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -7,6 +7,7 @@
 /ioctl07
 /ioctl08
 /ioctl_loop01
+/ioctl_loop02
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop02.c b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
new file mode 100644
index 000000000..683171501
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop02.c
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ *
+ * It is designed to test LO_FLAGS_READ_ONLY(similar as losetup -r)
+ * and LOOP_CHANGE_FD.
+ *
+ * For LOOP_CHANGE_FD, this operation is possible only if the loop device
+ * is read-only and the new backing store is the same size and type as the
+ * old backing store.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static int file_fd, file_change_fd, file_fd_invalid;
+static char backing_path[1024], backing_file_path[1024], backing_file_change_path[1024];
+static int attach_flag, dev_fd, file_fd;
+static char loop_ro_path[1024], dev_path[1024];
+
+/*
+ * This function is different from tst_attach_dev lib/tst_device.c.
+ * It only opens file_path with O_RDONLY mode before loop_set_fd.
+ * So we can generate LO_FLAGS_READ_ONLY flag.
+ */
+static void attach_device(char *img)
+{
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	file_fd = SAFE_OPEN(img, O_RDONLY);
+
+	SAFE_IOCTL(dev_fd, LOOP_SET_FD, file_fd);
+
+	SAFE_CLOSE(dev_fd);
+	SAFE_CLOSE(file_fd);
+}
+
+static void verify_ioctl_loop(void)
+{
+	struct loop_info loopinfoget;
+
+	attach_device("test.img");
+	attach_flag = 1;
+
+	check_sys_value(loop_ro_path, 1);
+	check_sys_string(backing_path, backing_file_path);
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	file_change_fd = SAFE_OPEN("test1.img", O_RDWR);
+	file_fd_invalid = SAFE_OPEN("test2.img", O_RDWR);
+
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+
+	/*
+	 * In drivers/block/loop.c code, set status function doesn't handle
+	 * LO_FLAGS_READ_ONLY flag and ignore it. Only loop_set_fd with readonly
+	 * mode, lo_flags will include LO_FLAGS_READ_ONLY.
+	 */
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
+
+	if (loopinfoget.lo_flags & ~LO_FLAGS_READ_ONLY)
+		tst_res(TFAIL, "lo_flags has unexpected %d flag", loopinfoget.lo_flags);
+	else
+		tst_res(TPASS, "lo_flags only has default LO_FLAGS_READ_ONLY flag");
+
+	TEST(write(dev_fd, "xx", 2));
+	if (TST_RET != -1)
+		tst_res(TFAIL, "write succeed unexpectedly");
+	else
+		tst_res(TPASS | TTERRNO, "Can not write data in RO mode");
+
+	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_change_fd));
+	if (TST_RET) {
+		tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed");
+	} else {
+		tst_res(TPASS, "LOOP_CHANGE_FD succeeded");
+		check_sys_value(loop_ro_path, 1);
+		check_sys_string(backing_path, backing_file_change_path);
+	}
+
+	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_fd_invalid));
+	if (TST_RET) {
+		if (TST_ERR == EINVAL)
+			tst_res(TPASS | TTERRNO, "LOOP_CHANGE_FD failed as expected");
+		else
+			tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed expected EINVAL got");
+	} else {
+		tst_res(TFAIL, "LOOP_CHANGE_FD succeeded");
+	}
+
+	SAFE_CLOSE(dev_fd);
+	SAFE_CLOSE(file_change_fd);
+	SAFE_CLOSE(file_fd_invalid);
+	tst_detach_device(dev_path);
+	attach_flag = 0;
+}
+
+static void setup(void)
+{
+	int dev_num;
+
+	char *tmpdir = tst_get_tmpdir();
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	tst_fill_file("test.img", 0, 1024, 10);
+	tst_fill_file("test1.img", 0, 1024, 10);
+	tst_fill_file("test2.img", 0, 2048, 20);
+
+	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
+	sprintf(backing_file_path, "%s/test.img", tmpdir);
+	sprintf(backing_file_change_path, "%s/test1.img", tmpdir);
+	sprintf(loop_ro_path, "/sys/block/loop%d/ro", dev_num);
+	if (tmpdir)
+		free(tmpdir);
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (file_fd > 0)
+		SAFE_CLOSE(file_fd);
+	if (file_change_fd > 0)
+		SAFE_CLOSE(file_change_fd);
+	if (file_fd_invalid > 0)
+		SAFE_CLOSE(file_fd_invalid);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v2 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (2 preceding siblings ...)
  2020-04-09 10:44             ` [LTP] [PATCH v2 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-17 15:12               ` Cyril Hrubis
  2020-04-09 10:44             ` [LTP] [PATCH v2 06/10] syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test Yang Xu
                               ` (7 subsequent siblings)
  11 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

It is designed to test LOOP_CHANGE_FD can not succeed(get EINAL error)
when loop_dev is not read only.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop03.c      | 76 +++++++++++++++++++
 3 files changed, 78 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 80005227d..46d877fa3 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -530,6 +530,7 @@ ioctl08      ioctl08
 
 ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
+ioctl_loop03 ioctl_loop03
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 534c4ab34..1501c2779 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -8,6 +8,7 @@
 /ioctl08
 /ioctl_loop01
 /ioctl_loop02
+/ioctl_loop03
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop03.c b/testcases/kernel/syscalls/ioctl/ioctl_loop03.c
new file mode 100644
index 000000000..4e7c29cde
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop03.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ *
+ * It is designed to test LOOP_CHANGE_FD can not succeed(get EINAL error)
+ * when loop_dev is not read only.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024];
+static int dev_num, dev_fd, file_fd, attach_flag;
+
+static void verify_ioctl_loop(void)
+{
+	TEST(ioctl(dev_fd, LOOP_CHANGE_FD, file_fd));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "LOOP_CHANGE_FD succeeded unexpectedly");
+		return;
+	}
+	if (TST_ERR == EINVAL)
+		tst_res(TPASS | TTERRNO, "LOOP_CHANGE_FD failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "LOOP_CHANGE_FD failed expected EINVAL got");
+}
+
+static void setup(void)
+{
+	struct loop_info loopinfoget;
+
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	tst_fill_file("test.img", 0, 1024, 10);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	file_fd = SAFE_OPEN("test.img", O_RDWR);
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
+
+	if (loopinfoget.lo_flags & LO_FLAGS_READ_ONLY)
+		tst_brk(TCONF, "Current environment has unexpected LO_FLAGS_READ_ONLY flag");
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (file_fd > 0)
+		SAFE_CLOSE(file_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v2 06/10] syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (3 preceding siblings ...)
  2020-04-09 10:44             ` [LTP] [PATCH v2 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-09 10:44             ` [LTP] [PATCH v2 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO " Yang Xu
                               ` (6 subsequent siblings)
  11 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

LOOP_SET_CAPACITY can update a live loop device size when we
change the size of the underlying backing file. Also check sys
value.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop04.c      | 97 +++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop04.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 46d877fa3..51ad8ff69 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -531,6 +531,7 @@ ioctl08      ioctl08
 ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
 ioctl_loop03 ioctl_loop03
+ioctl_loop04 ioctl_loop04
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 1501c2779..039a5251c 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -9,6 +9,7 @@
 /ioctl_loop01
 /ioctl_loop02
 /ioctl_loop03
+/ioctl_loop04
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop04.c b/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
new file mode 100644
index 000000000..ae110570e
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop04.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ *
+ * It is designed to test LOOP_SET_CAPACITY can update a live
+ * loop device size when we change the size of the underlying
+ * backing file. Also check sys value.
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+#define OLD_SIZE 10240
+#define NEW_SIZE 5120
+
+static char dev_path[1024], sys_loop_sizepath[1024];
+static char *wrbuf;
+static int dev_num, dev_fd, file_fd, attach_flag;
+
+static void verify_ioctl_loop(void)
+{
+	struct loop_info loopinfoget;
+
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+	tst_fill_file("test.img", 0, 1024, OLD_SIZE/1024);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	check_sys_value(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);
+
+	if (loopinfoget.lo_flags & LO_FLAGS_READ_ONLY)
+		tst_brk(TCONF, "Current environment has unexpected LO_FLAGS_READ_ONLY flag");
+
+	SAFE_TRUNCATE("test.img", NEW_SIZE);
+	SAFE_IOCTL(dev_fd, LOOP_SET_CAPACITY);
+
+	/*check that we can't write data beyond 5K into loop device*/
+	TEST(write(dev_fd, wrbuf, OLD_SIZE));
+	if (TST_RET == NEW_SIZE) {
+		tst_res(TPASS, "LOOP_SET_CAPACITY has updated loop size(%d) to %d", OLD_SIZE, NEW_SIZE);
+		check_sys_value(sys_loop_sizepath, NEW_SIZE/512);
+	} else {
+		tst_res(TFAIL, "LOOP_SET_CAPACITY doesn't updated loop size to %d, its size is %ld",
+				NEW_SIZE, TST_RET);
+	}
+
+	SAFE_CLOSE(file_fd);
+	SAFE_CLOSE(dev_fd);
+	tst_detach_device(dev_path);
+	unlink("test.img");
+	attach_flag = 0;
+}
+
+static void setup(void)
+{
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	wrbuf = SAFE_MALLOC(OLD_SIZE);
+	memset(wrbuf, 'x', OLD_SIZE);
+	sprintf(sys_loop_sizepath, "/sys/block/loop%d/size", dev_num);
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (file_fd > 0)
+		SAFE_CLOSE(file_fd);
+	if (wrbuf)
+		free(wrbuf);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v2 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO ioctl test
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (4 preceding siblings ...)
  2020-04-09 10:44             ` [LTP] [PATCH v2 06/10] syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-09 10:44             ` [LTP] [PATCH v2 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test Yang Xu
                               ` (5 subsequent siblings)
  11 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

LOOP_SET_DIRECT_IO can update a live loop device dio mode. It needs the
backing file also supports dio mode and the lo_offset is aligned with
the logical block size(getting by BLKSSZGET ioctl).

It was introduced into kernel since 4.10
commit ab1cb278bc70 ("block: loop: introduce ioctl command of LOOP_SET_DIRECT_IO").

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
 .../kernel/syscalls/ioctl/ioctl_loop05.c      | 125 ++++++++++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop05.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 51ad8ff69..8b120b43a 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -532,6 +532,7 @@ ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
 ioctl_loop03 ioctl_loop03
 ioctl_loop04 ioctl_loop04
+ioctl_loop05 ioctl_loop05
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 039a5251c..f484d98d6 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -10,6 +10,7 @@
 /ioctl_loop02
 /ioctl_loop03
 /ioctl_loop04
+/ioctl_loop05
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop05.c b/testcases/kernel/syscalls/ioctl/ioctl_loop05.c
new file mode 100644
index 000000000..16c7a468c
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop05.c
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice.
+ *
+ * It is designed to test LOOP_SET_DIRECT_IO can updata a live
+ * loop device dio mode. It need the backing file also supports
+ * dio mode and the lo_offset is aligned with the logical block size.
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/mount.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+#define DIO_MESSAGE "In dio mode"
+#define NON_DIO_MESSAGE "In non dio mode"
+
+static char dev_path[1024], sys_loop_diopath[1024];
+static int dev_num, dev_fd, attach_flag, logical_block_size;
+
+static void check_dio_value(int flag)
+{
+	struct loop_info loopinfoget;
+
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
+	tst_res(TINFO, "%s", flag ? DIO_MESSAGE : NON_DIO_MESSAGE);
+
+	if (loopinfoget.lo_flags & LO_FLAGS_DIRECT_IO)
+		tst_res(flag ? TPASS : TFAIL, "lo_flags has LO_FLAGS_DIRECT_IO flag");
+	else
+		tst_res(flag ? TFAIL : TPASS, "lo_flags doesn't have LO_FLAGS_DIRECT_IO flag");
+
+	check_sys_value(sys_loop_diopath, flag);
+}
+
+static void verify_ioctl_loop(void)
+{
+	struct loop_info loopinfo;
+
+	memset(&loopinfo, 0, sizeof(loopinfo));
+
+	tst_res(TINFO, "Without setting lo_offset or sizelimit");
+	SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 1);
+	check_dio_value(1);
+
+	SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
+	check_dio_value(0);
+
+	tst_res(TINFO, "With offset equal to logical_block_size");
+	loopinfo.lo_offset = logical_block_size;
+	TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
+	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
+	if (TST_RET == 0) {
+		tst_res(TPASS, "LOOP_SET_DIRECT_IO succeeded");
+		check_dio_value(1);
+		SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
+	} else {
+		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed");
+	}
+
+	tst_res(TINFO, "With offset less than or unalign logical_block_size");
+	loopinfo.lo_offset = logical_block_size / 2;
+	TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
+
+	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "LOOP_SET_DIRECT_IO succeeded unexpectedly");
+		SAFE_IOCTL(dev_fd, LOOP_SET_DIRECT_IO, 0);
+		return;
+	}
+	if (TST_ERR == EINVAL)
+		tst_res(TPASS | TTERRNO, "LOOP_SET_DIRECT_IO failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed expected EINVAL got");
+
+	loopinfo.lo_offset = 0;
+	TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
+}
+
+static void setup(void)
+{
+	if (tst_fs_type(".") == TST_TMPFS_MAGIC)
+		tst_brk(TCONF, "tmpfd doesn't support O_DIRECT flag, skip it");
+
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	sprintf(sys_loop_diopath, "/sys/block/loop%d/loop/dio", dev_num);
+	tst_fill_file("test.img", 0, 1024, 1024);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	check_support_cmd(dev_fd, LOOP_SET_DIRECT_IO, 0, "LOOP_SET_DIRECT_IO");
+	SAFE_IOCTL(dev_fd, BLKSSZGET, &logical_block_size);
+	tst_res(TINFO, "%s default logical_block_size is %d", dev_path, logical_block_size);
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v2 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (5 preceding siblings ...)
  2020-04-09 10:44             ` [LTP] [PATCH v2 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO " Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-22 13:57               ` Cyril Hrubis
  2020-04-09 10:44             ` [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size " Yang Xu
                               ` (4 subsequent siblings)
  11 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop06.c      | 90 +++++++++++++++++++
 3 files changed, 92 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop06.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 8b120b43a..e9b0a8f60 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -533,6 +533,7 @@ ioctl_loop02 ioctl_loop02
 ioctl_loop03 ioctl_loop03
 ioctl_loop04 ioctl_loop04
 ioctl_loop05 ioctl_loop05
+ioctl_loop06 ioctl_loop06
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index f484d98d6..ec19b4901 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -11,6 +11,7 @@
 /ioctl_loop03
 /ioctl_loop04
 /ioctl_loop05
+/ioctl_loop06
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop06.c b/testcases/kernel/syscalls/ioctl/ioctl_loop06.c
new file mode 100644
index 000000000..ef98efbca
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop06.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl error test about loopdevice
+ * LOOP_SET_BLOCK_SIZE.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024], sys_loop_logicalpath[1024];
+static int dev_num, dev_fd, attach_flag;
+static unsigned int invalid_value, half_value, unalign_value;
+
+static struct tcase {
+	unsigned int *setvalue;
+	int exp_err;
+	char *message;
+} tcases[] = {
+	{&half_value, EINVAL, "arg < 512"},
+	{&invalid_value, EINVAL, "arg > PAGE_SIZE"},
+	{&unalign_value, EINVAL, "arg != power_of_2"},
+};
+
+static void verify_ioctl_loop(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	tst_res(TINFO, "%s", tc->message);
+	TEST(ioctl(dev_fd, LOOP_SET_BLOCK_SIZE, *(tc->setvalue)));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "LOOP_SET_BLOCK_SIZE succeed unexpectedly");
+		return;
+	}
+	if (TST_ERR == tc->exp_err)
+		tst_res(TPASS | TTERRNO, "LOOP_SET_BLOCK_SIZE failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "LOOP_SET_BLOCK_SIZE failed expected %s got",
+				tst_strerrno(tc->exp_err));
+}
+
+static void setup(void)
+{
+	unsigned int pg_size;
+
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	sprintf(sys_loop_logicalpath, "/sys/block/loop%d/logical_block_size", dev_num);
+	tst_fill_file("test.img", 0, 1024, 1024);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+	half_value = 256;
+	pg_size = getpagesize();
+	invalid_value = pg_size * 2 ;
+	unalign_value = pg_size - 1;
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	check_support_cmd(dev_fd, LOOP_SET_BLOCK_SIZE, 512, "LOOP_SET_BLOCK_SIZE");
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+	unlink("test.img");
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_ioctl_loop,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size error test
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (6 preceding siblings ...)
  2020-04-09 10:44             ` [LTP] [PATCH v2 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-22 13:58               ` Cyril Hrubis
  2020-04-09 10:44             ` [LTP] [PATCH v2 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test Yang Xu
                               ` (3 subsequent siblings)
  11 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

This is a basic ioctl test about loopdevice LOOP_SET_BLOCK_SIZE
and LOOP_SET_DIRECT_IO. When logical blocksize is 1024, set dio
with  512 offset will fail.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop07.c      | 85 +++++++++++++++++++
 3 files changed, 87 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop07.c

diff --git a/runtest/syscalls b/runtest/syscalls
index e9b0a8f60..f83561724 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -534,6 +534,7 @@ ioctl_loop03 ioctl_loop03
 ioctl_loop04 ioctl_loop04
 ioctl_loop05 ioctl_loop05
 ioctl_loop06 ioctl_loop06
+ioctl_loop07 ioctl_loop07
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index ec19b4901..3a3d49adc 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -12,6 +12,7 @@
 /ioctl_loop04
 /ioctl_loop05
 /ioctl_loop06
+/ioctl_loop07
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop07.c b/testcases/kernel/syscalls/ioctl/ioctl_loop07.c
new file mode 100644
index 000000000..c58a92b3c
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop07.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice LOOP_SET_BLOCK_SIZE
+ * and LOOP_SET_DIRECT_IO.
+ * When blocksize is 1024(default align size is 512), set dio with
+ * 512 offset will fail.
+ */
+
+#include <stdio.h>
+#include <linux/loop.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <sys/mount.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024], sys_loop_logicalpath[1024];
+static int dev_num, dev_fd, attach_flag, logical_block_size;
+
+static void verify_ioctl_loop(void)
+{
+	check_sys_value(sys_loop_logicalpath, 1024);
+	SAFE_IOCTL(dev_fd, BLKSSZGET, &logical_block_size);
+	if (logical_block_size == 1024)
+		tst_res(TPASS, "logical block size is 1024 by using BLKSSZGET ioctl");
+	else
+		tst_res(TFAIL, "expected logical block size is 1024 but got %d by using BLKSSZGET ioctl",
+				logical_block_size);
+	TEST(ioctl(dev_fd, LOOP_SET_DIRECT_IO, 1));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "LOOP_SET_DIRECT_IO succeeded unexpectedly");
+		return;
+	}
+	if (TST_ERR == EINVAL)
+		tst_res(TPASS | TTERRNO, "LOOP_SET_DIRECT_IO failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "LOOP_SET_DIRECT_IO failed expected EINVAL got");
+}
+
+static void setup(void)
+{
+	struct loop_info loopinfo;
+
+	memset(&loopinfo, 0, sizeof(loopinfo));
+	loopinfo.lo_offset = 512;
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	sprintf(sys_loop_logicalpath, "/sys/block/loop%d/queue/logical_block_size", dev_num);
+	tst_fill_file("test.img", 0, 1024 * 1024, 1);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
+	check_support_cmd(dev_fd, LOOP_SET_DIRECT_IO, 0, "LOOP_SET_DIRECT_IO");
+	check_support_cmd(dev_fd, LOOP_SET_BLOCK_SIZE, 512, "LOOP_SET_BLOCK_SIZE");
+	TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_BLOCK_SIZE, 1024), TST_RETVAL_EQ0);
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl_loop,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v2 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (7 preceding siblings ...)
  2020-04-09 10:44             ` [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size " Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-28 15:44               ` Cyril Hrubis
  2020-04-09 10:44             ` [LTP] [PATCH v2 00/10] add loop ioctl test Yang Xu
                               ` (2 subsequent siblings)
  11 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

Test its lo_sizelimit field. If lo_sizelimit is 0, it means max
available. If sizelimit is less than loop_size, loopsize will
be truncated.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/ioctl/.gitignore    |  1 +
 .../kernel/syscalls/ioctl/ioctl_loop08.c      | 96 +++++++++++++++++++
 3 files changed, 98 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop08.c

diff --git a/runtest/syscalls b/runtest/syscalls
index f83561724..d77c191d2 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -535,6 +535,7 @@ ioctl_loop04 ioctl_loop04
 ioctl_loop05 ioctl_loop05
 ioctl_loop06 ioctl_loop06
 ioctl_loop07 ioctl_loop07
+ioctl_loop08 ioctl_loop08
 
 ioctl_ns01 ioctl_ns01
 ioctl_ns02 ioctl_ns02
diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
index 3a3d49adc..97134aa0b 100644
--- a/testcases/kernel/syscalls/ioctl/.gitignore
+++ b/testcases/kernel/syscalls/ioctl/.gitignore
@@ -13,6 +13,7 @@
 /ioctl_loop05
 /ioctl_loop06
 /ioctl_loop07
+/ioctl_loop08
 /ioctl_ns01
 /ioctl_ns02
 /ioctl_ns03
diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop08.c b/testcases/kernel/syscalls/ioctl/ioctl_loop08.c
new file mode 100644
index 000000000..150cfa0ef
--- /dev/null
+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop08.c
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
+ *
+ * This is a basic ioctl test about loopdevice LOOP_SET_STATUS64
+ * and LOOP_GET_STATUS64.
+ * Test its lo_sizelimit field. If lo_sizelimit is 0,it means max
+ * available. If sizelimit is less than loop_size, loopsize will
+ * be truncated.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include "ioctl_loop_support.h"
+#include "lapi/loop.h"
+#include "tst_test.h"
+
+static char dev_path[1024], sys_loop_sizepath[1024], sys_loop_sizelimitpath[1024];
+static int dev_num, dev_fd, file_fd, attach_flag;
+
+static struct tcase {
+	unsigned int set_sizelimit;
+	unsigned int exp_loopsize;
+	char *message;
+} tcases[] = {
+	{1024 * 4096, 2048, "When sizelimit is greater than loopsize "},
+	{1024 * 512, 1024, "When sizelimit is less than loopsize"},
+};
+
+static void verify_ioctl_loop(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+	struct loop_info64 loopinfo, loopinfoget;
+
+	tst_res(TINFO, "%s", tc->message);
+	memset(&loopinfo, 0, sizeof(loopinfo));
+	memset(&loopinfoget, 0, sizeof(loopinfoget));
+
+	loopinfo.lo_sizelimit = tc->set_sizelimit;
+	TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS64, &loopinfo), TST_RETVAL_EQ0);
+
+	check_sys_value(sys_loop_sizepath, tc->exp_loopsize);
+	check_sys_value(sys_loop_sizelimitpath, tc->set_sizelimit);
+	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS64, &loopinfoget);
+	if (loopinfoget.lo_sizelimit == tc->set_sizelimit)
+		tst_res(TPASS, "LOOP_GET_STATUS64 gets correct lo_sizelimit(%d)", tc->set_sizelimit);
+	else
+		tst_res(TFAIL, "LOOP_GET_STATUS64 gets wrong lo_sizelimit(%llu), expect %d",
+				loopinfoget.lo_sizelimit, tc->set_sizelimit);
+	/*Reset*/
+	loopinfo.lo_sizelimit = 0;
+	TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0);
+}
+
+static void setup(void)
+{
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
+	if (dev_num < 0)
+		tst_brk(TBROK, "Failed to find free loop device");
+
+	tst_fill_file("test.img", 0, 1024 * 1024, 1);
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	sprintf(sys_loop_sizepath, "/sys/block/loop%d/size", dev_num);
+	sprintf(sys_loop_sizelimitpath, "/sys/block/loop%d/loop/sizelimit", dev_num);
+
+	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
+	tst_res(TINFO, "original loop size 2048 sectors");
+}
+
+static void cleanup(void)
+{
+	if (dev_fd > 0)
+		SAFE_CLOSE(dev_fd);
+	if (file_fd > 0)
+		SAFE_CLOSE(file_fd);
+	if (attach_flag)
+		tst_detach_device(dev_path);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_ioctl_loop,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_drivers = (const char *const []) {
+		"loop",
+		NULL
+	}
+};
-- 
2.23.0




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

* [LTP] [PATCH v2 00/10] add loop ioctl test
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (8 preceding siblings ...)
  2020-04-09 10:44             ` [LTP] [PATCH v2 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test Yang Xu
@ 2020-04-09 10:44             ` Yang Xu
  2020-04-09 10:50             ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
  2020-04-17 15:11             ` Cyril Hrubis
  11 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:44 UTC (permalink / raw)
  To: ltp

This is a set of test for the loop ioctl.

v1->v2:
1. remove linux/loop.h detection
2. use TST_RETRY_FUNC instead of safe_set_status
3. modify makefile
4. free tmpdir
5. use BLKSSZGET ioctl() to get logical block size

Yang Xu (10):
  lapi/loop.h: Add fallback for loop ioctl and flag
  syscalls/ioctl:add common c file for loop ioctl
  syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
    test
  syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test
  syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode
  syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test
  syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO ioctl test
  syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test
  syscalls/ioctl_loop07: Add dio with logic block size error test
  syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test

 include/lapi/loop.h                           |  35 ++++
 runtest/syscalls                              |  10 +-
 testcases/kernel/syscalls/ioctl/.gitignore    |   8 +
 testcases/kernel/syscalls/ioctl/Makefile      |   5 +
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 140 ++++++++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop02.c      | 150 ++++++++++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop03.c      |  76 +++++++++
 .../kernel/syscalls/ioctl/ioctl_loop04.c      |  97 +++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop05.c      | 125 +++++++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop06.c      |  90 +++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop07.c      |  85 ++++++++++
 .../kernel/syscalls/ioctl/ioctl_loop08.c      |  96 +++++++++++
 .../syscalls/ioctl/ioctl_loop_support.c       |  40 +++++
 .../syscalls/ioctl/ioctl_loop_support.h       |  12 ++
 14 files changed, 968 insertions(+), 1 deletion(-)
 create mode 100644 include/lapi/loop.h
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop01.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop02.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop03.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop04.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop05.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop06.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop07.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop08.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.c
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop_support.h

-- 
2.23.0




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

* [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (9 preceding siblings ...)
  2020-04-09 10:44             ` [LTP] [PATCH v2 00/10] add loop ioctl test Yang Xu
@ 2020-04-09 10:50             ` Yang Xu
  2020-04-17 15:11             ` Cyril Hrubis
  11 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-09 10:50 UTC (permalink / raw)
  To: ltp

Hi All

> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
>   include/lapi/loop.h | 35 +++++++++++++++++++++++++++++++++++
>   1 file changed, 35 insertions(+)
>   create mode 100644 include/lapi/loop.h
> 
> diff --git a/include/lapi/loop.h b/include/lapi/loop.h
> new file mode 100644
> index 000000000..bc6d9950d
> --- /dev/null
> +++ b/include/lapi/loop.h
> @@ -0,0 +1,35 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + */
> +#ifndef LAPI_LOOP_H
> +#define LAPI_LOOP_H
> +
> +#include <linux/types.h>
> +#ifdef HAVE_LINUX_LOOP_H
Sorry, this should be removed.
> +# include <linux/loop.h>
> +#endif
> +
> +#ifndef LO_FLAGS_PARTSCAN
> +# define LO_FLAGS_PARTSCAN 8
> +#endif
> +
> +#ifndef LO_FLAGS_DIRECT_IO
> +# define LO_FLAGS_DIRECT_IO 16
> +#endif
> +
> +#ifndef LOOP_SET_CAPACITY
> +# define LOOP_SET_CAPACITY 0x4C07
> +#endif
> +
> +#ifndef LOOP_SET_DIRECT_IO
> +# define LOOP_SET_DIRECT_IO 0x4C08
> +#endif
> +
> +#ifndef LOOP_SET_BLOCK_SIZE
> +# define LOOP_SET_BLOCK_SIZE 0x4C09
> +#endif
> +
> +#endif
> +
> 



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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-09 10:44             ` [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
@ 2020-04-17 15:10               ` Cyril Hrubis
  2020-04-20  2:23                 ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-17 15:10 UTC (permalink / raw)
  To: ltp

Hi!
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + */
> +#define TST_NO_DEFAULT_MAIN
> +#include "ioctl_loop_support.h"
> +#include "tst_test.h"
> +
> +void check_sys_value(char *path, int setvalue)
> +{
> +	int getvalue;
> +
> +	SAFE_FILE_SCANF(path, "%d", &getvalue);
> +	if (setvalue == getvalue)
> +		tst_res(TPASS, "%s value is %d", path, setvalue);
> +	else
> +		tst_res(TFAIL, "%s value expected %d got %d", path, setvalue, getvalue);
> +}
> +
> +void check_sys_string(char *path, char *setmessage)
> +{
> +	char getmessage[1024];
> +
> +	SAFE_FILE_SCANF(path, "%s", getmessage);
> +	if (strcmp(setmessage, getmessage))
> +		tst_res(TFAIL, "%s expected %s got %s", path, setmessage, getmessage);
> +	else
> +		tst_res(TPASS, "%s string is %s", path, getmessage);
> +}

In the end I've renamed and moved these functions into the test library
because the functionality is generic enough and I doubt that these
tests would be the only one using it.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag
  2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
                               ` (10 preceding siblings ...)
  2020-04-09 10:50             ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
@ 2020-04-17 15:11             ` Cyril Hrubis
  11 siblings, 0 replies; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-17 15:11 UTC (permalink / raw)
  To: ltp

Hi!
> +#include <linux/types.h>
> +#ifdef HAVE_LINUX_LOOP_H
> +# include <linux/loop.h>
> +#endif

I've removed this ifdef and pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode
  2020-04-09 10:44             ` [LTP] [PATCH v2 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode Yang Xu
@ 2020-04-17 15:12               ` Cyril Hrubis
  0 siblings, 0 replies; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-17 15:12 UTC (permalink / raw)
  To: ltp

Hi!
I did a minor fixes, typo correction and code simplifications and pushed
first three tests, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-17 15:10               ` Cyril Hrubis
@ 2020-04-20  2:23                 ` Yang Xu
  2020-04-20  6:08                   ` Yang Xu
  2020-04-20 12:55                   ` Cyril Hrubis
  0 siblings, 2 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-20  2:23 UTC (permalink / raw)
  To: ltp

Hi Cyril


> Hi!
>> +/*
>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>> + */
>> +#define TST_NO_DEFAULT_MAIN
>> +#include "ioctl_loop_support.h"
>> +#include "tst_test.h"
>> +
>> +void check_sys_value(char *path, int setvalue)
>> +{
>> +	int getvalue;
>> +
>> +	SAFE_FILE_SCANF(path, "%d", &getvalue);
>> +	if (setvalue == getvalue)
>> +		tst_res(TPASS, "%s value is %d", path, setvalue);
>> +	else
>> +		tst_res(TFAIL, "%s value expected %d got %d", path, setvalue, getvalue);
>> +}
>> +
>> +void check_sys_string(char *path, char *setmessage)
>> +{
>> +	char getmessage[1024];
>> +
>> +	SAFE_FILE_SCANF(path, "%s", getmessage);
>> +	if (strcmp(setmessage, getmessage))
>> +		tst_res(TFAIL, "%s expected %s got %s", path, setmessage, getmessage);
>> +	else
>> +		tst_res(TPASS, "%s string is %s", path, getmessage);
>> +}
> 
> In the end I've renamed and moved these functions into the test library
> because the functionality is generic enough and I doubt that these
> tests would be the only one using it.
That's great.  I remember prctl cases use this function. Also, in some 
cap cases, it needs bitwise operators(I only know prctl08.c). Maybe we 
can add TST_ASSERT_BITWISE?
> 



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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-20  2:23                 ` Yang Xu
@ 2020-04-20  6:08                   ` Yang Xu
  2020-04-20 13:01                     ` Cyril Hrubis
  2020-04-20 12:55                   ` Cyril Hrubis
  1 sibling, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-20  6:08 UTC (permalink / raw)
  To: ltp

Hi Cyril


> Hi Cyril
> 
> 
>> Hi!
>>> +/*
>>> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
>>> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
>>> + */
>>> +#define TST_NO_DEFAULT_MAIN
>>> +#include "ioctl_loop_support.h"
>>> +#include "tst_test.h"
>>> +
>>> +void check_sys_value(char *path, int setvalue)
>>> +{
>>> +??? int getvalue;
>>> +
>>> +??? SAFE_FILE_SCANF(path, "%d", &getvalue);
>>> +??? if (setvalue == getvalue)
>>> +??????? tst_res(TPASS, "%s value is %d", path, setvalue);
>>> +??? else
>>> +??????? tst_res(TFAIL, "%s value expected %d got %d", path, 
>>> setvalue, getvalue);
>>> +}
>>> +
>>> +void check_sys_string(char *path, char *setmessage)
>>> +{
>>> +??? char getmessage[1024];
>>> +
>>> +??? SAFE_FILE_SCANF(path, "%s", getmessage);
>>> +??? if (strcmp(setmessage, getmessage))
>>> +??????? tst_res(TFAIL, "%s expected %s got %s", path, setmessage, 
>>> getmessage);
>>> +??? else
>>> +??????? tst_res(TPASS, "%s string is %s", path, getmessage);
>>> +}
>>
>> In the end I've renamed and moved these functions into the test library
>> because the functionality is generic enough and I doubt that these
>> tests would be the only one using it.
> That's great.? I remember prctl cases use this function. Also, in some 
> cap cases, it needs bitwise operators(I only know prctl08.c). Maybe we 
> can add TST_ASSERT_BITWISE?
This situation is rarely seen. I want to add two functions such as 
TST_ASSERT_FILE_STR/INT to compare file field string with expected 
string. So, I can use this api for prctl NoNewPrivs and Seccomp field in 
/proc/[pid]/status. What do you think about this?

--- a/lib/tst_assert.c
+++ b/lib/tst_assert.c
@@ -22,6 +22,20 @@ void tst_assert_int(const char *file, const int 
lineno, const char *path, int va
         tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, 
sys_val);
  }

+void tst_assert_file_int(const char *file, const int lineno, const char 
*path, const char *buf, int val)
+{
+       int sys_val;
+
+       SAFE_FILE_LINES_SCANF(path, buf, &sys_val);
+
+       if (val == sys_val) {
+               tst_res_(file, lineno, TPASS, "%s %s field = %d", path, 
buf, sys_val);
+               return;
+       }
+
+       tst_res_(file, lineno, TFAIL, "%s %s field != %d got %d", path, 
buf, val, sys_val);
+}
+
  void tst_assert_str(const char *file, const int lineno, const char 
*path, const char *val)
  {
         char sys_val[1024];
@@ -34,3 +48,17 @@ void tst_assert_str(const char *file, const int 
lineno, const char *path, const

         tst_res_(file, lineno, TFAIL, "%s != '%s' got '%s'", path, val, 
sys_val);
  }
+
+void tst_assert_file_str(const char *file, const int lineno, const char 
*path, const char *buf, const char *val)
+{
+       char sys_val[1024];
+
+       SAFE_FILE_LINES_SCANF(path, buf, sys_val);
+
+       if (!strcmp(val, sys_val)) {
+               tst_res_(file, lineno, TPASS, "%s %s field = %s", path, 
buf, sys_val);
+               return;
+       }
+
+       tst_res_(file, lineno, TFAIL, "%s %s field != %s got %s", path, 
buf, val, sys_val);
+}


Best Regards
Yang Xu

>>



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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-20  2:23                 ` Yang Xu
  2020-04-20  6:08                   ` Yang Xu
@ 2020-04-20 12:55                   ` Cyril Hrubis
  1 sibling, 0 replies; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-20 12:55 UTC (permalink / raw)
  To: ltp

Hi!
> > In the end I've renamed and moved these functions into the test library
> > because the functionality is generic enough and I doubt that these
> > tests would be the only one using it.
> That's great.  I remember prctl cases use this function. Also, in some 
> cap cases, it needs bitwise operators(I only know prctl08.c). Maybe we 
> can add TST_ASSERT_BITWISE?

I guess that we would need to pass to numbers to the bitwise operation,
one would be mask for which bits should be used for the comparsion and
one with the actual bits. Which tests would need that? I looked at
capset and capget tests but there does not seem to any bitwise checks on
values on proc or sys files.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-20  6:08                   ` Yang Xu
@ 2020-04-20 13:01                     ` Cyril Hrubis
  2020-04-21  6:19                       ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-20 13:01 UTC (permalink / raw)
  To: ltp

Hi!
> +void tst_assert_file_str(const char *file, const int lineno, const char 
> *path, const char *buf, const char *val)
> +{
> +       char sys_val[1024];
> +
> +       SAFE_FILE_LINES_SCANF(path, buf, sys_val);

We should call the file_lines_scanf() here and pass the filename and
lineno that this functions takes as parameter instead.

Also I'm not sure that it's a good idea to pass the fmt to this
function. I guess that it would be better to pass a prefix string such
as "foo=" and to append the "%s" in this function. That way we could
also encode the buffer length to the fmt string as well, in that case we
would append "%1024s".

> +       if (!strcmp(val, sys_val)) {
> +               tst_res_(file, lineno, TPASS, "%s %s field = %s", path, 
> buf, sys_val);
> +               return;
> +       }
> +
> +       tst_res_(file, lineno, TFAIL, "%s %s field != %s got %s", path, 
> buf, val, sys_val);
> +}



-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-20 13:01                     ` Cyril Hrubis
@ 2020-04-21  6:19                       ` Yang Xu
  2020-04-21  8:55                         ` Cyril Hrubis
  0 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-21  6:19 UTC (permalink / raw)
  To: ltp

Hi Cyril


> Hi!
>> +void tst_assert_file_str(const char *file, const int lineno, const char
>> *path, const char *buf, const char *val)
>> +{
>> +       char sys_val[1024];
>> +
>> +       SAFE_FILE_LINES_SCANF(path, buf, sys_val);
> 
> We should call the file_lines_scanf() here and pass the filename and
> lineno that this functions takes as parameter instead.
I don't understand the meaning of adding filename and line parameter(If 
fail, it will report tst_assert.c: 29). Do you want to report the 
correct parsed filename when failed?

If so, I think lineno is meaningless and it also overides the place of 
the code meet problem . I prefer to add some path info in safe_file_ops.c.

> 
> Also I'm not sure that it's a good idea to pass the fmt to this
> function. I guess that it would be better to pass a prefix string such
> as "foo=" and to append the "%s" in this function. That way we could
> also encode the buffer length to the fmt string as well, in that case we
> would append "%1024s".
> 
Yes, it is a better way.

ps: I will send a patch about this api.

Best Regards
Yang Xu
>> +       if (!strcmp(val, sys_val)) {
>> +               tst_res_(file, lineno, TPASS, "%s %s field = %s", path,
>> buf, sys_val);
>> +               return;
>> +       }
>> +
>> +       tst_res_(file, lineno, TFAIL, "%s %s field != %s got %s", path,
>> buf, val, sys_val);
>> +}
> 
> 
> 



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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-21  6:19                       ` Yang Xu
@ 2020-04-21  8:55                         ` Cyril Hrubis
  2020-04-21 10:35                           ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-21  8:55 UTC (permalink / raw)
  To: ltp

Hi!
> >> +void tst_assert_file_str(const char *file, const int lineno, const char
> >> *path, const char *buf, const char *val)
> >> +{
> >> +       char sys_val[1024];
> >> +
> >> +       SAFE_FILE_LINES_SCANF(path, buf, sys_val);
> > 
> > We should call the file_lines_scanf() here and pass the filename and
> > lineno that this functions takes as parameter instead.
> I don't understand the meaning of adding filename and line parameter(If 
> fail, it will report tst_assert.c: 29). Do you want to report the 
> correct parsed filename when failed?
> 
> If so, I think lineno is meaningless and it also overides the place of 
> the code meet problem . I prefer to add some path info in safe_file_ops.c.

What I want is to show the test source filename and line on failure, for
that we have to pass the lineno and file to the file_lines_scanf()
function. I think that it makes much more sense to print the line in a
test where the problem has happened rather than some location in the
library code.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-21  8:55                         ` Cyril Hrubis
@ 2020-04-21 10:35                           ` Yang Xu
  2020-04-21 12:12                             ` Cyril Hrubis
  0 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-21 10:35 UTC (permalink / raw)
  To: ltp

Hi Cyril


> Hi!
>>>> +void tst_assert_file_str(const char *file, const int lineno, const char
>>>> *path, const char *buf, const char *val)
>>>> +{
>>>> +       char sys_val[1024];
>>>> +
>>>> +       SAFE_FILE_LINES_SCANF(path, buf, sys_val);
>>>
>>> We should call the file_lines_scanf() here and pass the filename and
>>> lineno that this functions takes as parameter instead.
>> I don't understand the meaning of adding filename and line parameter(If
>> fail, it will report tst_assert.c: 29). Do you want to report the
>> correct parsed filename when failed?
>>
>> If so, I think lineno is meaningless and it also overides the place of
>> the code meet problem . I prefer to add some path info in safe_file_ops.c.
> 
> What I want is to show the test source filename and line on failure, for
> that we have to pass the lineno and file to the file_lines_scanf()
> function. I think that it makes much more sense to print the line in a
> test where the problem has happened rather than some location in the
> library code.
Oh, I see. ie TST_ASSERT_INT, modify ioctl_loop01.c
TST_ASSERT_INT(__FILE__, __LINE__, partscan_path, 0);
if partscan_path doesn't exist, it will report error as below:
safe_file_ops.c:142: BROK: Failed to open FILE 
'/sys/block/loop0/loop1/partscan' for reading at ioctl_loop01.c:46: 
ENOENT (2)

--- a/lib/tst_assert.c
+++ b/lib/tst_assert.c
-void tst_assert_int(const char *file, const int lineno, const char 
*path, int val)
+void tst_assert_int(const char *file, const int lineno, const char 
*source_path,
+                   const int source_pos, const char *path, int val)
  {
         int sys_val;

-       SAFE_FILE_SCANF(path, "%d", &sys_val);
+       safe_file_scanf(source_path, source_pos, NULL, path, "%d", &sys_val

diff --git a/include/tst_assert.h b/include/tst_assert.h
index 913fff1b5..03e57bbd9 100644
--- a/include/tst_assert.h
+++ b/include/tst_assert.h
@@ -7,8 +7,8 @@
  #ifndef TST_ASSERT_H__
  #define TST_ASSERT_H__

-#define TST_ASSERT_INT(path, val) \
-       tst_assert_int(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_INT(source_path, source_pos, path, val) \
+       tst_assert_int(__FILE__, __LINE__, source_path, source_pos, 
path, val)

  /*
   * Asserts that integer value stored in file pointed by path equals to the
@@ -16,6 +16,7 @@
   * values in sysfs, procfs, etc.
   */
  void tst_assert_int(const char *file, const int lineno,
+                   const char *source_path, const int source_pos,
                     const char *path, int val);

  #define TST_ASSERT_FILE_INT(path, buf, val)

> 



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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-21 10:35                           ` Yang Xu
@ 2020-04-21 12:12                             ` Cyril Hrubis
  2020-04-22  3:12                               ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-21 12:12 UTC (permalink / raw)
  To: ltp

Hi!
> >>>> +void tst_assert_file_str(const char *file, const int lineno, const char
> >>>> *path, const char *buf, const char *val)
> >>>> +{
> >>>> +       char sys_val[1024];
> >>>> +
> >>>> +       SAFE_FILE_LINES_SCANF(path, buf, sys_val);
> >>>
> >>> We should call the file_lines_scanf() here and pass the filename and
> >>> lineno that this functions takes as parameter instead.
> >> I don't understand the meaning of adding filename and line parameter(If
> >> fail, it will report tst_assert.c: 29). Do you want to report the
> >> correct parsed filename when failed?
> >>
> >> If so, I think lineno is meaningless and it also overides the place of
> >> the code meet problem . I prefer to add some path info in safe_file_ops.c.
> > 
> > What I want is to show the test source filename and line on failure, for
> > that we have to pass the lineno and file to the file_lines_scanf()
> > function. I think that it makes much more sense to print the line in a
> > test where the problem has happened rather than some location in the
> > library code.
> Oh, I see. ie TST_ASSERT_INT, modify ioctl_loop01.c
> TST_ASSERT_INT(__FILE__, __LINE__, partscan_path, 0);
> if partscan_path doesn't exist, it will report error as below:
> safe_file_ops.c:142: BROK: Failed to open FILE 
> '/sys/block/loop0/loop1/partscan' for reading at ioctl_loop01.c:46: 
> ENOENT (2)

I still think that we are not on the same page.

The macros are to be used from tests, such as TST_ASSERT_INT() these
macros call the corresponding functions with __FILE__ and __LINE__, in
this case tst_assert_int(). From that point on anything that is called
from inside of the tst_assert_int() function should pass down the file
and lineno so that we get the filename and lineno from the line the call
has been called in the test.

So if we do TST_ASSERT_INT() in the test, we call tst_brk_() and pass
the file and lineno so that we get the correct test line in case of a
failure.

And for the same reason if we are going to get an value from a file from
inside of the assert function we have to call the safe_file_scanf() and
pass down the file and lineno so that if the call fails the test
filename and the line on which the call originated in the test is
printed.

Is it clear now?

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl
  2020-04-21 12:12                             ` Cyril Hrubis
@ 2020-04-22  3:12                               ` Yang Xu
  0 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-22  3:12 UTC (permalink / raw)
  To: ltp

Hi Cyril

> Hi!
>>>>>> +void tst_assert_file_str(const char *file, const int lineno, const char
>>>>>> *path, const char *buf, const char *val)
>>>>>> +{
>>>>>> +       char sys_val[1024];
>>>>>> +
>>>>>> +       SAFE_FILE_LINES_SCANF(path, buf, sys_val);
>>>>>
>>>>> We should call the file_lines_scanf() here and pass the filename and
>>>>> lineno that this functions takes as parameter instead.
>>>> I don't understand the meaning of adding filename and line parameter(If
>>>> fail, it will report tst_assert.c: 29). Do you want to report the
>>>> correct parsed filename when failed?
>>>>
>>>> If so, I think lineno is meaningless and it also overides the place of
>>>> the code meet problem . I prefer to add some path info in safe_file_ops.c.
>>>
>>> What I want is to show the test source filename and line on failure, for
>>> that we have to pass the lineno and file to the file_lines_scanf()
>>> function. I think that it makes much more sense to print the line in a
>>> test where the problem has happened rather than some location in the
>>> library code.
>> Oh, I see. ie TST_ASSERT_INT, modify ioctl_loop01.c
>> TST_ASSERT_INT(__FILE__, __LINE__, partscan_path, 0);
>> if partscan_path doesn't exist, it will report error as below:
>> safe_file_ops.c:142: BROK: Failed to open FILE
>> '/sys/block/loop0/loop1/partscan' for reading at ioctl_loop01.c:46:
>> ENOENT (2)
> 
> I still think that we are not on the same page.
> 
> The macros are to be used from tests, such as TST_ASSERT_INT() these
> macros call the corresponding functions with __FILE__ and __LINE__, in
> this case tst_assert_int(). From that point on anything that is called
> from inside of the tst_assert_int() function should pass down the file
> and lineno so that we get the filename and lineno from the line the call
> has been called in the test.
> 
> So if we do TST_ASSERT_INT() in the test, we call tst_brk_() and pass
> the file and lineno so that we get the correct test line in case of a
> failure.

> 
> And for the same reason if we are going to get an value from a file from
> inside of the assert function we have to call the safe_file_scanf() and
> pass down the file and lineno so that if the call fails the test
> filename and the line on which the call originated in the test is
> printed.
> 
> Is it clear now?
OK. I guess I only need to use safe_file_scanf(file, lineno, NULL, path, 
"%d", &sys_val) in tst_assert_int function as you suggested at the 
beginning.
> 



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

* [LTP] [PATCH v2 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test
  2020-04-09 10:44             ` [LTP] [PATCH v2 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test Yang Xu
@ 2020-04-22 13:57               ` Cyril Hrubis
  0 siblings, 0 replies; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-22 13:57 UTC (permalink / raw)
  To: ltp

Hi!
Pushed, next three tests with minor changes, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size error test
  2020-04-09 10:44             ` [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size " Yang Xu
@ 2020-04-22 13:58               ` Cyril Hrubis
  2020-04-23  6:06                 ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-22 13:58 UTC (permalink / raw)
  To: ltp

Hi!
This test fails for me, i.e. the attempt to enabe direct IO succeeds. I
will have to have a closer look later on.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size error test
  2020-04-22 13:58               ` Cyril Hrubis
@ 2020-04-23  6:06                 ` Yang Xu
  2020-04-23 10:12                   ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-23  6:06 UTC (permalink / raw)
  To: ltp

Hi Cyril


> Hi!
> This test fails for me, i.e. the attempt to enabe direct IO succeeds. I
> will have to have a closer look later on.
Now, I use 5.7.0-rc2 kernel to test, this case fails, I guess loop 
driver has changed its logic. I am trying.

ps: when run ioctl_loop01.c, I also met another problem that 
/dev/loop0p1 and  the corresponding sys file doesn't existed. dmesg 
report error "loop_reread_partitions: partition scan of loop0 () failed 
(rc=-16)". Also trying .
> 



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

* [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size error test
  2020-04-23  6:06                 ` Yang Xu
@ 2020-04-23 10:12                   ` Yang Xu
  2020-04-24  4:43                     ` Yang Xu
  0 siblings, 1 reply; 58+ messages in thread
From: Yang Xu @ 2020-04-23 10:12 UTC (permalink / raw)
  To: ltp

Hi Cyril

> Hi Cyril
> 
> 
>> Hi!
>> This test fails for me, i.e. the attempt to enabe direct IO succeeds. I
>> will have to have a closer look later on.
> Now, I use 5.7.0-rc2 kernel to test, this case fails, I guess loop 
> driver has changed its logic. I am trying.

I write this test on fedora31(tmp directory use tmpfs), so I got a 
pseudo pass when setting dio.

I misunderstood the meaning of the comment in the kernel.
kernel commet as below:
"We support direct I/O only if lo_offset is aligned with the
logical I/O size of backing device, and the logical block
size of loop is bigger than the backing device's and the loop
needn't transform transfer."

sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);  //get block 
size from backing device
dio_align = sb_bsize - 1;

I use logic block size of loop(1024 > 512) , so this test pass.

Now, I think this test is meaningless and we can remove this.

Best Regards
Yang Xu
> 
> ps: when run ioctl_loop01.c, I also met another problem that 
> /dev/loop0p1 and? the corresponding sys file doesn't existed. dmesg 
> report error "loop_reread_partitions: partition scan of loop0 () failed 
> (rc=-16)". Also trying .
>>



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

* [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size error test
  2020-04-23 10:12                   ` Yang Xu
@ 2020-04-24  4:43                     ` Yang Xu
  0 siblings, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-24  4:43 UTC (permalink / raw)
  To: ltp

Hi Cyril


> Hi Cyril
> 
>> Hi Cyril
>>
>>
>>> Hi!
>>> This test fails for me, i.e. the attempt to enabe direct IO succeeds. I
>>> will have to have a closer look later on.
>> Now, I use 5.7.0-rc2 kernel to test, this case fails, I guess loop 
>> driver has changed its logic. I am trying.
> 
> I write this test on fedora31(tmp directory use tmpfs), so I got a 
> pseudo pass when setting dio.
> 
> I misunderstood the meaning of the comment in the kernel.
> kernel commet as below:
> "We support direct I/O only if lo_offset is aligned with the
> logical I/O size of backing device, and the logical block
> size of loop is bigger than the backing device's and the loop
> needn't transform transfer."
> 
> sb_bsize = bdev_logical_block_size(inode->i_sb->s_bdev);? //get block 
> size from backing device
> dio_align = sb_bsize - 1;
> 
> I use logic block size of loop(1024 > 512) , so this test pass.
> 
> Now, I think this test is meaningless and we can remove this.
> 
> Best Regards
> Yang Xu
>>
>> ps: when run ioctl_loop01.c, I also met another problem that 
>> /dev/loop0p1 and? the corresponding sys file doesn't existed. dmesg 
>> report error "loop_reread_partitions: partition scan of loop0 () 
>> failed (rc=-16)". Also trying .
Kernel commit d3ef55362("block: fix busy device checking in 
blk_drop_partitions")  and commit cb6b771b05("block: fix busy device 
checking in blk_drop_partitions again") introduced this 
failure(ioctl_loop01, EBUSY error). more info see[1]

[1]https://lkml.org/lkml/2020/4/23/380
>>>



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

* [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test
  2020-04-02 15:06 ` [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
  2020-04-03 11:55   ` Cyril Hrubis
@ 2020-04-27  1:24   ` Yang Xu
  1 sibling, 0 replies; 58+ messages in thread
From: Yang Xu @ 2020-04-27  1:24 UTC (permalink / raw)
  To: ltp

Hi Christoph

Recently, this test[1](has merged into ltp master) failed on kernel 
5.7.0-rc2. I found your kernel patch
d3ef55362("block: fix busy device checking in blk_drop_partitions")  and 
commit cb6b771b05("block: fix busy device checking in 
blk_drop_partitions again") introduced this failure.

dmesg as below:
"loop_reread_partitions: partition scan of loop0 () failed (rc=-16)"

It look likes we use bd_dev->bd_openers instead of bd_dev->bd_super in 
blk_drop_partitions after your patch.  When running this test, the 
bd_opener is equal to 3(I don't figure out the reason).
I don't know how to fix this(do special judement for loopdev by using 
MAJOR((bdev->bd_dev) ?) or should I modify test code? Please give me 
some advise.

[1]https://github.com/linux-test-project/ltp/commit/4f3ff1f323cbe4ae4dc6542e940902dca1280e9d

Best Regards
Yang Xu
> For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
> directory and also get lo_flags by using LOOP_GET_STATUS.
> 
> For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
> But we also check whether we can scan partition table correctly ie check
> whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
> 
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
>   runtest/syscalls                              |   3 +-
>   testcases/kernel/syscalls/ioctl/.gitignore    |   1 +
>   .../kernel/syscalls/ioctl/ioctl_loop01.c      | 127 ++++++++++++++++++
>   3 files changed, 130 insertions(+), 1 deletion(-)
>   create mode 100644 testcases/kernel/syscalls/ioctl/ioctl_loop01.c
> 
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 777f0ac6a..7fd67450e 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -524,10 +524,11 @@ ioctl03      ioctl03
>   ioctl04      ioctl04
>   ioctl05      ioctl05
>   ioctl06      ioctl06
> -
>   ioctl07      ioctl07
>   ioctl08      ioctl08
>   
> +ioctl_loop01 ioctl_loop01
> +
>   ioctl_ns01 ioctl_ns01
>   ioctl_ns02 ioctl_ns02
>   ioctl_ns03 ioctl_ns03
> diff --git a/testcases/kernel/syscalls/ioctl/.gitignore b/testcases/kernel/syscalls/ioctl/.gitignore
> index 97fbb9681..4cfef2839 100644
> --- a/testcases/kernel/syscalls/ioctl/.gitignore
> +++ b/testcases/kernel/syscalls/ioctl/.gitignore
> @@ -6,6 +6,7 @@
>   /ioctl06
>   /ioctl07
>   /ioctl08
> +/ioctl_loop01
>   /ioctl_ns01
>   /ioctl_ns02
>   /ioctl_ns03
> diff --git a/testcases/kernel/syscalls/ioctl/ioctl_loop01.c b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
> new file mode 100644
> index 000000000..e806e0443
> --- /dev/null
> +++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c
> @@ -0,0 +1,127 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
> + *
> + * This is a basic ioctl test about loopdevice.
> + * It is designed to test LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN flag.
> + *
> + * For LO_FLAGS_AUTOCLEAR flag, we only check autoclear fieldvalue in sys
> + * directory and also get lo_flags by using LOOP_GET_STATUS.
> + *
> + * For LO_FLAGS_PARTSCAN flag, it is the same as LO_FLAGS_AUTOCLEAR flag.
> + * But we also check whether we can scan partition table correctly ie check
> + * whether /dev/loopnp1 and /sys/bloclk/loop0/loop0p1 existed.
> + */
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include "ioctl_loop_support.h"
> +#include "lapi/loop.h"
> +#include "tst_test.h"
> +
> +static char dev_path[1024], backing_path[1024], backing_file_path[1024];
> +static int dev_num, attach_flag, dev_fd;
> +/*
> + *In drivers/block/loop.c code, set status function doesn't handle
> + *LO_FLAGS_READ_ONLY flag and ingore it. Only loop_set_fd with rondonly
> + *mode file_fd, lo_flags will include LO_FLAGS_READ_ONLY.
> + */
> +#define set_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN | LO_FLAGS_READ_ONLY | LO_FLAGS_DIRECT_IO)
> +#define get_flags (LO_FLAGS_AUTOCLEAR | LO_FLAGS_PARTSCAN)
> +
> +static char partscan_path[1024], autoclear_path[1024];
> +static char loop_partpath[1026], sys_loop_partpath[1026];
> +
> +static void verify_ioctl_loop(void)
> +{
> +	int ret;
> +	struct loop_info loopinfo, loopinfoget;
> +
> +	tst_attach_device(dev_path, "test.img");
> +	attach_flag = 1;
> +
> +	check_sys_value(partscan_path, 0);
> +	check_sys_value(autoclear_path, 0);
> +	check_sys_string(backing_path, backing_file_path);
> +
> +	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
> +	memset(&loopinfo, 0, sizeof(loopinfo));
> +	memset(&loopinfo, 0, sizeof(loopinfoget));
> +
> +	loopinfo.lo_flags = set_flags;
> +	SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo);
> +
> +	SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget);
> +
> +	if (loopinfoget.lo_flags & ~get_flags)
> +		tst_res(TFAIL, "expect %d but got %d", get_flags, loopinfoget.lo_flags);
> +	else
> +		tst_res(TPASS, "get expected lo_flag %d", loopinfoget.lo_flags);
> +
> +	ret = access(loop_partpath, F_OK);
> +	if (ret == 0)
> +		tst_res(TPASS, "access %s succeeds", loop_partpath);
> +	else
> +		tst_res(TFAIL, "access %s fails", loop_partpath);
> +
> +	ret = access(sys_loop_partpath, F_OK);
> +	if (ret == 0)
> +		tst_res(TPASS, "access %s succeeds", sys_loop_partpath);
> +	else
> +		tst_res(TFAIL, "access %s fails", sys_loop_partpath);
> +
> +	check_sys_value(partscan_path, 1);
> +	check_sys_value(autoclear_path, 1);
> +
> +	SAFE_CLOSE(dev_fd);
> +	tst_detach_device(dev_path);
> +	attach_flag = 0;
> +}
> +
> +static void setup(void)
> +{
> +	const char *const cmd_dd[] = {"dd", "if=/dev/zero", "of=test.img", "bs=1M", "count=10", NULL};
> +	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
> +						"primary", "ext4", "1M", "10M", NULL};
> +
> +	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
> +	if (dev_num < 0)
> +		tst_brk(TBROK, "Failed to find free loop device");
> +
> +	SAFE_CMD(cmd_dd, NULL, NULL);
> +	SAFE_CMD(cmd_parted, NULL, NULL);
> +
> +	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
> +	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
> +	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
> +	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);
> +}
> +
> +static void cleanup(void)
> +{
> +	if (dev_fd > 0)
> +		SAFE_CLOSE(dev_fd);
> +	if (attach_flag)
> +		tst_detach_device(dev_path);
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = verify_ioctl_loop,
> +	.needs_root = 1,
> +	.needs_cmds = (const char *const []) {
> +		"dd",
> +		"parted",
> +		NULL
> +	},
> +	.needs_drivers = (const char *const []) {
> +		"loop",
> +		NULL
> +	},
> +	.needs_tmpdir = 1,
> +};
> 



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

* [LTP] [PATCH v2 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test
  2020-04-09 10:44             ` [LTP] [PATCH v2 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test Yang Xu
@ 2020-04-28 15:44               ` Cyril Hrubis
  0 siblings, 0 replies; 58+ messages in thread
From: Cyril Hrubis @ 2020-04-28 15:44 UTC (permalink / raw)
  To: ltp

Hi!
I've renamed the test to ioctl_loop07 and pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

end of thread, other threads:[~2020-04-28 15:44 UTC | newest]

Thread overview: 58+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-02 15:06 [LTP] [PATCH v1 00/10] add loop ioctl test Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 01/10] lapi: Add a configure check and fallback for loop ioctl and flag Yang Xu
2020-04-03 11:44   ` Cyril Hrubis
2020-04-06  6:01     ` Yang Xu
2020-04-09  8:47   ` Martin Doucha
2020-04-09 10:09     ` Cyril Hrubis
2020-04-09 10:14     ` Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
2020-04-03 12:16   ` Cyril Hrubis
2020-04-09  2:42     ` Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
2020-04-03 11:55   ` Cyril Hrubis
2020-04-06  6:27     ` Yang Xu
2020-04-09  6:10       ` Yang Xu
2020-04-09  7:55         ` Cyril Hrubis
2020-04-09 10:44           ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
2020-04-09 10:44             ` [LTP] [PATCH v2 02/10] syscalls/ioctl:add common c file for loop ioctl Yang Xu
2020-04-17 15:10               ` Cyril Hrubis
2020-04-20  2:23                 ` Yang Xu
2020-04-20  6:08                   ` Yang Xu
2020-04-20 13:01                     ` Cyril Hrubis
2020-04-21  6:19                       ` Yang Xu
2020-04-21  8:55                         ` Cyril Hrubis
2020-04-21 10:35                           ` Yang Xu
2020-04-21 12:12                             ` Cyril Hrubis
2020-04-22  3:12                               ` Yang Xu
2020-04-20 12:55                   ` Cyril Hrubis
2020-04-09 10:44             ` [LTP] [PATCH v2 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
2020-04-09 10:44             ` [LTP] [PATCH v2 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test Yang Xu
2020-04-09 10:44             ` [LTP] [PATCH v2 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode Yang Xu
2020-04-17 15:12               ` Cyril Hrubis
2020-04-09 10:44             ` [LTP] [PATCH v2 06/10] syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test Yang Xu
2020-04-09 10:44             ` [LTP] [PATCH v2 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO " Yang Xu
2020-04-09 10:44             ` [LTP] [PATCH v2 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test Yang Xu
2020-04-22 13:57               ` Cyril Hrubis
2020-04-09 10:44             ` [LTP] [PATCH v2 09/10] syscalls/ioctl_loop07: Add dio with logic block size " Yang Xu
2020-04-22 13:58               ` Cyril Hrubis
2020-04-23  6:06                 ` Yang Xu
2020-04-23 10:12                   ` Yang Xu
2020-04-24  4:43                     ` Yang Xu
2020-04-09 10:44             ` [LTP] [PATCH v2 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test Yang Xu
2020-04-28 15:44               ` Cyril Hrubis
2020-04-09 10:44             ` [LTP] [PATCH v2 00/10] add loop ioctl test Yang Xu
2020-04-09 10:50             ` [LTP] [PATCH v2 01/10] lapi/loop.h: Add fallback for loop ioctl and flag Yang Xu
2020-04-17 15:11             ` Cyril Hrubis
2020-04-27  1:24   ` [LTP] [PATCH v1 03/10] syscalls/ioctl_loop01: Add LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN test Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 04/10] syscalls/ioctl_loop02: Add LO_FLAGS_READ_ONLY and LOOP_CHANGE_FD test Yang Xu
2020-04-03 13:34   ` Cyril Hrubis
2020-04-06  7:24     ` Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 05/10] syscalls/ioctl_loop03: Add LOOP_CHANGE_FD test with WR mode Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 06/10] syscalls/ioctl_loop04: Add LOOP_SET_CAPACITY ioctl test Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 07/10] syscalls/ioctl_loop05: Add LOOP_SET_DIRECT_IO " Yang Xu
2020-04-03 16:44   ` Cyril Hrubis
2020-04-06  7:53     ` Yang Xu
2020-04-06  8:30       ` Cyril Hrubis
2020-04-02 15:06 ` [LTP] [PATCH v1 08/10] syscalls/ioctl_loop06: Add LOOP_SET_BLOCK_SIZE error test Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 09/10] syscalls/ioctl_loop07: Add dio with logic block size " Yang Xu
2020-04-02 15:06 ` [LTP] [PATCH v1 10/10] syscalls/ioctl_loop08: Add LOOP_SET/GET_STATUS64 sizelimit field test Yang Xu

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.