diff options
Diffstat (limited to 'src/kernel/tests/testcases/kernel/syscalls/ioctl')
8 files changed, 937 insertions, 0 deletions
diff --git a/src/kernel/tests/testcases/kernel/syscalls/ioctl/CMakeLists.txt b/src/kernel/tests/testcases/kernel/syscalls/ioctl/CMakeLists.txt new file mode 100644 index 0000000..6f7c5bb --- /dev/null +++ b/src/kernel/tests/testcases/kernel/syscalls/ioctl/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.10) + +project(xloop-kernel-test-ioctl_xloop) + +# test ioctl_xloop01 +add_executable(ioctl_xloop01 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_xloop01.c) +target_link_libraries(ioctl_xloop01 LINK_PUBLIC libltp) +install(TARGETS ioctl_xloop01 DESTINATION bin + COMPONENT test) + +# test ioctl_xloop02 +add_executable(ioctl_xloop02 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_xloop02.c) +target_link_libraries(ioctl_xloop02 LINK_PUBLIC libltp) +install(TARGETS ioctl_xloop02 DESTINATION bin + COMPONENT test) + +# test ioctl_xloop03 +add_executable(ioctl_xloop03 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_xloop03.c) +target_link_libraries(ioctl_xloop03 LINK_PUBLIC libltp) +install(TARGETS ioctl_xloop03 DESTINATION bin + COMPONENT test) + +# test ioctl_xloop04 +add_executable(ioctl_xloop04 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_xloop04.c) +target_link_libraries(ioctl_xloop04 LINK_PUBLIC libltp) +install(TARGETS ioctl_xloop04 DESTINATION bin + COMPONENT test) + +# test ioctl_xloop05 +add_executable(ioctl_xloop05 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_xloop05.c) +target_link_libraries(ioctl_xloop05 LINK_PUBLIC libltp) +install(TARGETS ioctl_xloop05 DESTINATION bin + COMPONENT test) + +# test ioctl_xloop06 +add_executable(ioctl_xloop06 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_xloop06.c) +target_link_libraries(ioctl_xloop06 LINK_PUBLIC libltp) +install(TARGETS ioctl_xloop06 DESTINATION bin + COMPONENT test) + +# test ioctl_xloop07 +add_executable(ioctl_xloop07 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_xloop07.c) +target_link_libraries(ioctl_xloop07 LINK_PUBLIC libltp) +install(TARGETS ioctl_xloop07 DESTINATION bin + COMPONENT test) diff --git a/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop01.c b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop01.c new file mode 100644 index 0000000..58bb692 --- /dev/null +++ b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop01.c @@ -0,0 +1,156 @@ +// 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 xloopdevice. + * It is designed to test XLO_FLAGS_AUTOCLEAR and XLO_FLAGS_PARTSCAN flag. + * + * For XLO_FLAGS_AUTOCLEAR flag, we only check autoclear field value in sys + * directory and also get xlo_flags by using XLOOP_GET_STATUS. + * + * For XLO_FLAGS_PARTSCAN flag, it is the same as XLO_FLAGS_AUTOCLEAR flag. + * But we also check whether we can scan partition table correctly ie check + * whether /dev/xloopnp1 and /sys/bloclk/xloop0/xloop0p1 existed. + * + * For XLO_FLAGS_AUTOCLEAR flag, it can be clear. For XLO_FLAGS_PARTSCAN flag, + * it cannot be clear. We also check this. + * + * It is also a regression test for kernel + * commit 10c70d95c0f2 ("block: remove the bd_openers checks in blk_drop_partitions") + * commit 6ac92fb5cdff ("xloop: Fix wrong masking of status flags"). + */ + +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include "lapi/xloop.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/xloop.c code, set status function doesn't handle + * XLO_FLAGS_READ_ONLY flag and ingore it. Only xloop_set_fd with read only mode + * file_fd, xlo_flags will include XLO_FLAGS_READ_ONLY and it's the same for + * XLO_FLAGS_DIRECT_IO. + */ +#define SET_FLAGS (XLO_FLAGS_AUTOCLEAR | XLO_FLAGS_PARTSCAN | XLO_FLAGS_READ_ONLY | XLO_FLAGS_DIRECT_IO) +#define GET_FLAGS (XLO_FLAGS_AUTOCLEAR | XLO_FLAGS_PARTSCAN) + +static char partscan_path[1024], autoclear_path[1024]; +static char xloop_partpath[1026], sys_xloop_partpath[1026]; + +static void check_xloop_value(int set_flag, int get_flag, int autoclear_field) +{ + struct xloop_info xloopinfo = {0}, xloopinfoget = {0}; + int ret; + + xloopinfo.xlo_flags = set_flag; + SAFE_IOCTL(dev_fd, XLOOP_SET_STATUS, &xloopinfo); + SAFE_IOCTL(dev_fd, XLOOP_GET_STATUS, &xloopinfoget); + + if (xloopinfoget.xlo_flags & ~get_flag) + tst_res(TFAIL, "expect %d but got %d", get_flag, xloopinfoget.xlo_flags); + else + tst_res(TPASS, "get expected xlo_flag %d", xloopinfoget.xlo_flags); + + TST_ASSERT_INT(partscan_path, 1); + TST_ASSERT_INT(autoclear_path, autoclear_field); + + if (!parted_sup) { + tst_res(TINFO, "Current environment doesn't have parted disk, skip it"); + return; + } + + ret = TST_RETRY_FN_EXP_BACKOFF(access(xloop_partpath, F_OK), TST_RETVAL_EQ0, 30); + if (ret == 0) + tst_res(TPASS, "access %s succeeds", xloop_partpath); + else + tst_res(TFAIL, "access %s fails", xloop_partpath); + + ret = TST_RETRY_FN_EXP_BACKOFF(access(sys_xloop_partpath, F_OK), TST_RETVAL_EQ0, 30); + if (ret == 0) + tst_res(TPASS, "access %s succeeds", sys_xloop_partpath); + else + tst_res(TFAIL, "access %s fails", sys_xloop_partpath); +} + +static void verify_ioctl_xloop(void) +{ + tst_attach_device(dev_path, "test.img"); + attach_flag = 1; + + TST_ASSERT_INT(partscan_path, 0); + TST_ASSERT_INT(autoclear_path, 0); + TST_ASSERT_STR(backing_path, backing_file_path); + + check_xloop_value(SET_FLAGS, GET_FLAGS, 1); + + tst_res(TINFO, "Test flag can be clear"); + check_xloop_value(0, XLO_FLAGS_PARTSCAN, 0); + + tst_detach_device_by_fd(dev_path, dev_fd); + 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_xloopdev(dev_path, sizeof(dev_path)); + if (dev_num < 0) + tst_brk(TBROK, "Failed to find free xloop 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/xloop%d/xloop/partscan", dev_num); + sprintf(autoclear_path, "/sys/block/xloop%d/xloop/autoclear", dev_num); + sprintf(backing_path, "/sys/block/xloop%d/xloop/backing_file", dev_num); + sprintf(sys_xloop_partpath, "/sys/block/xloop%d/xloop%dp1", dev_num, dev_num); + sprintf(backing_file_path, "%s/test.img", tst_get_tmpdir()); + sprintf(xloop_partpath, "%sp1", dev_path); + dev_fd = SAFE_OPEN(dev_path, O_RDWR); +} + +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_xloop, + .needs_root = 1, + .needs_drivers = (const char *const []) { + "xloop", + "xloop_file_fmt_raw", + NULL + }, + .tags = (const struct tst_tag[]) { + {"linux-git", "10c70d95c0f2"}, + {"linux-git", "6ac92fb5cdff"}, + {} + }, + .needs_tmpdir = 1, +}; diff --git a/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop02.c b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop02.c new file mode 100644 index 0000000..895f7f2 --- /dev/null +++ b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop02.c @@ -0,0 +1,165 @@ +// 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 xloopdevice. + * + * It is designed to test XLO_FLAGS_READ_ONLY (similar as xlosetup -r) + * and XLOOP_CHANGE_FD. + * + * For XLOOP_CHANGE_FD, this operation is possible only if the xloop device + * is read-only and the new backing store is the same size and type as the + * old backing store. + * + * If using XLOOP_CONFIGURE ioctl, we can set XLO_FLAGS_READ_ONLY + * flag even though backing file with write mode. + */ + +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <stdlib.h> +#include "lapi/xloop.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, xloop_configure_sup = 1; +static char xloop_ro_path[1024], dev_path[1024]; +static struct xloop_config xloopconfig; + +static struct tcase { + int mode; + int ioctl; + char *message; +} tcases[] = { + {O_RDONLY, XLOOP_SET_FD, "Using XLOOP_SET_FD to setup xloopdevice"}, + {O_RDWR, XLOOP_CONFIGURE, "Using XLOOP_CONFIGURE with read_only flag"}, +}; + +static void verify_ioctl_xloop(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + struct xloop_info xloopinfoget; + + if (tc->ioctl == XLOOP_CONFIGURE && !xloop_configure_sup) { + tst_res(TCONF, "XLOOP_CONFIGURE ioctl not supported"); + return; + } + + tst_res(TINFO, "%s", tc->message); + file_fd = SAFE_OPEN("test.img", tc->mode); + + if (tc->ioctl == XLOOP_SET_FD) { + SAFE_IOCTL(dev_fd, XLOOP_SET_FD, file_fd); + } else { + xloopconfig.fd = file_fd; + SAFE_IOCTL(dev_fd, XLOOP_CONFIGURE, &xloopconfig); + } + attach_flag = 1; + + TST_ASSERT_INT(xloop_ro_path, 1); + TST_ASSERT_STR(backing_path, backing_file_path); + + memset(&xloopinfoget, 0, sizeof(xloopinfoget)); + + SAFE_IOCTL(dev_fd, XLOOP_GET_STATUS, &xloopinfoget); + + if (xloopinfoget.xlo_flags & ~XLO_FLAGS_READ_ONLY) + tst_res(TFAIL, "xlo_flags has unexpected %d flag", xloopinfoget.xlo_flags); + else + tst_res(TPASS, "xlo_flags only has default XLO_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, XLOOP_CHANGE_FD, file_change_fd)); + if (TST_RET) { + tst_res(TFAIL | TTERRNO, "XLOOP_CHANGE_FD failed"); + } else { + tst_res(TPASS, "XLOOP_CHANGE_FD succeeded"); + TST_ASSERT_INT(xloop_ro_path, 1); + TST_ASSERT_STR(backing_path, backing_file_change_path); + } + + TEST(ioctl(dev_fd, XLOOP_CHANGE_FD, file_fd_invalid)); + if (TST_RET) { + if (TST_ERR == EINVAL) + tst_res(TPASS | TTERRNO, "XLOOP_CHANGE_FD failed as expected"); + else + tst_res(TFAIL | TTERRNO, "XLOOP_CHANGE_FD failed expected EINVAL got"); + } else { + tst_res(TFAIL, "XLOOP_CHANGE_FD succeeded"); + } + + SAFE_CLOSE(file_fd); + tst_detach_device_by_fd(dev_path, dev_fd); + attach_flag = 0; +} + +static void setup(void) +{ + int dev_num; + int ret; + + char *tmpdir = tst_get_tmpdir(); + dev_num = tst_find_free_xloopdev(dev_path, sizeof(dev_path)); + if (dev_num < 0) + tst_brk(TBROK, "Failed to find free xloop 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/xloop%d/xloop/backing_file", dev_num); + sprintf(backing_file_path, "%s/test.img", tmpdir); + sprintf(backing_file_change_path, "%s/test1.img", tmpdir); + sprintf(xloop_ro_path, "/sys/block/xloop%d/ro", dev_num); + + free(tmpdir); + + file_change_fd = SAFE_OPEN("test1.img", O_RDWR); + file_fd_invalid = SAFE_OPEN("test2.img", O_RDWR); + + dev_fd = SAFE_OPEN(dev_path, O_RDWR); + xloopconfig.fd = -1; + ret = ioctl(dev_fd, XLOOP_CONFIGURE, &xloopconfig); + + if (ret && errno != EBADF) { + tst_res(TINFO | TERRNO, "XLOOP_CONFIGURE is not supported"); + xloop_configure_sup = 0; + } + xloopconfig.info.xlo_flags = XLO_FLAGS_READ_ONLY; +} + +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, + .tcnt = ARRAY_SIZE(tcases), + .test = verify_ioctl_xloop, + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "xloop", + "xloop_file_fmt_raw", + NULL + } +}; diff --git a/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop03.c b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop03.c new file mode 100644 index 0000000..9447cbc --- /dev/null +++ b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop03.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 xloopdevice. + * + * It is designed to test XLOOP_CHANGE_FD can not succeed (get EINVAL error) + * when xloop_dev is not read only. + */ + +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include "lapi/xloop.h" +#include "tst_test.h" + +static char dev_path[1024]; +static int dev_num, dev_fd, file_fd, attach_flag; + +static void verify_ioctl_xloop(void) +{ + TEST(ioctl(dev_fd, XLOOP_CHANGE_FD, file_fd)); + if (TST_RET == 0) { + tst_res(TFAIL, "XLOOP_CHANGE_FD succeeded unexpectedly"); + return; + } + if (TST_ERR == EINVAL) + tst_res(TPASS | TTERRNO, "XLOOP_CHANGE_FD failed as expected"); + else + tst_res(TFAIL | TTERRNO, "XLOOP_CHANGE_FD failed expected EINVAL got"); +} + +static void setup(void) +{ + struct xloop_info xloopinfoget; + + memset(&xloopinfoget, 0, sizeof(xloopinfoget)); + dev_num = tst_find_free_xloopdev(dev_path, sizeof(dev_path)); + if (dev_num < 0) + tst_brk(TBROK, "Failed to find free xloop 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, XLOOP_GET_STATUS, &xloopinfoget); + + if (xloopinfoget.xlo_flags & XLO_FLAGS_READ_ONLY) + tst_brk(TCONF, "Current environment has unexpected XLO_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_xloop, + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "xloop", + "xloop_file_fmt_raw", + NULL + } +}; diff --git a/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop04.c b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop04.c new file mode 100644 index 0000000..adf5cdf --- /dev/null +++ b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop04.c @@ -0,0 +1,99 @@ +// 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 xloopdevice. + * + * It is designed to test XLOOP_SET_CAPACITY can update a live + * xloop 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 "lapi/xloop.h" +#include "tst_test.h" + +#define OLD_SIZE 10240 +#define NEW_SIZE 5120 + +static char dev_path[1024], sys_xloop_sizepath[1024]; +static char *wrbuf; +static int dev_num, dev_fd, file_fd, attach_flag; + +static void verify_ioctl_xloop(void) +{ + struct xloop_info xloopinfoget; + + memset(&xloopinfoget, 0, sizeof(xloopinfoget)); + tst_fill_file("test.img", 0, 1024, OLD_SIZE/1024); + tst_attach_device(dev_path, "test.img"); + attach_flag = 1; + + TST_ASSERT_INT(sys_xloop_sizepath, OLD_SIZE/512); + file_fd = SAFE_OPEN("test.img", O_RDWR); + SAFE_IOCTL(dev_fd, XLOOP_GET_STATUS, &xloopinfoget); + + if (xloopinfoget.xlo_flags & XLO_FLAGS_READ_ONLY) + tst_brk(TCONF, "Current environment has unexpected XLO_FLAGS_READ_ONLY flag"); + + SAFE_TRUNCATE("test.img", NEW_SIZE); + SAFE_IOCTL(dev_fd, XLOOP_SET_CAPACITY); + + SAFE_LSEEK(dev_fd, 0, SEEK_SET); + + /*check that we can't write data beyond 5K into xloop device*/ + TEST(write(dev_fd, wrbuf, OLD_SIZE)); + if (TST_RET == NEW_SIZE) { + tst_res(TPASS, "XLOOP_SET_CAPACITY set xloop size to %d", NEW_SIZE); + } else { + tst_res(TFAIL, "XLOOP_SET_CAPACITY didn't set xloop size to %d, its size is %ld", + NEW_SIZE, TST_RET); + } + + TST_ASSERT_INT(sys_xloop_sizepath, NEW_SIZE/512); + + SAFE_CLOSE(file_fd); + tst_detach_device_by_fd(dev_path, dev_fd); + unlink("test.img"); + attach_flag = 0; +} + +static void setup(void) +{ + dev_num = tst_find_free_xloopdev(dev_path, sizeof(dev_path)); + if (dev_num < 0) + tst_brk(TBROK, "Failed to find free xloop device"); + + wrbuf = SAFE_MALLOC(OLD_SIZE); + memset(wrbuf, 'x', OLD_SIZE); + sprintf(sys_xloop_sizepath, "/sys/block/xloop%d/size", dev_num); + dev_fd = SAFE_OPEN(dev_path, O_RDWR); +} + +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_xloop, + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "xloop", + "xloop_file_fmt_raw", + NULL + } +}; diff --git a/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop05.c b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop05.c new file mode 100644 index 0000000..62ee0c8 --- /dev/null +++ b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop05.c @@ -0,0 +1,156 @@ +// 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 xloopdevice. + * + * It is designed to test XLOOP_SET_DIRECT_IO can update a live + * xloop device dio mode. It needs the backing file also supports + * dio mode and the xlo_offset is aligned with the logical block size. + * + * The direct I/O error handling is a bit messy on Linux, some filesystems + * return error when it coudln't be enabled, some silently fall back to regular + * buffered I/O. + * + * The XLOOP_SET_DIRECT_IO ioctl() may ignore all checks if it cannot get the + * logical block size which is the case if the block device pointer in the + * backing file inode is not set. In this case the direct I/O appears to be + * enabled but falls back to buffered I/O later on. This is the case at least + * for Btrfs. Because of that the test passes both with failure as well as + * success with non-zero offset. + */ + +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <stdlib.h> +#include <sys/mount.h> +#include "lapi/xloop.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_xloop_diopath[1024], backing_file_path[1024];; +static int dev_num, dev_fd, block_devfd, attach_flag, logical_block_size; + +static void check_dio_value(int flag) +{ + struct xloop_info xloopinfoget; + + memset(&xloopinfoget, 0, sizeof(xloopinfoget)); + + SAFE_IOCTL(dev_fd, XLOOP_GET_STATUS, &xloopinfoget); + tst_res(TINFO, "%s", flag ? DIO_MESSAGE : NON_DIO_MESSAGE); + + if (xloopinfoget.xlo_flags & XLO_FLAGS_DIRECT_IO) + tst_res(flag ? TPASS : TFAIL, "xlo_flags has XLO_FLAGS_DIRECT_IO flag"); + else + tst_res(flag ? TFAIL : TPASS, "xlo_flags doesn't have XLO_FLAGS_DIRECT_IO flag"); + + TST_ASSERT_INT(sys_xloop_diopath, flag); +} + +static void verify_ioctl_xloop(void) +{ + struct xloop_info xloopinfo; + + memset(&xloopinfo, 0, sizeof(xloopinfo)); + TST_RETRY_FUNC(ioctl(dev_fd, XLOOP_SET_STATUS, &xloopinfo), TST_RETVAL_EQ0); + + tst_res(TINFO, "Without setting xlo_offset or sizelimit"); + SAFE_IOCTL(dev_fd, XLOOP_SET_DIRECT_IO, 1); + check_dio_value(1); + + SAFE_IOCTL(dev_fd, XLOOP_SET_DIRECT_IO, 0); + check_dio_value(0); + + tst_res(TINFO, "With offset equal to logical_block_size"); + xloopinfo.xlo_offset = logical_block_size; + TST_RETRY_FUNC(ioctl(dev_fd, XLOOP_SET_STATUS, &xloopinfo), TST_RETVAL_EQ0); + TEST(ioctl(dev_fd, XLOOP_SET_DIRECT_IO, 1)); + if (TST_RET == 0) { + tst_res(TPASS, "XLOOP_SET_DIRECT_IO succeeded"); + check_dio_value(1); + SAFE_IOCTL(dev_fd, XLOOP_SET_DIRECT_IO, 0); + } else { + tst_res(TFAIL | TTERRNO, "XLOOP_SET_DIRECT_IO failed"); + } + + tst_res(TINFO, "With nonzero offset less than logical_block_size"); + xloopinfo.xlo_offset = logical_block_size / 2; + TST_RETRY_FUNC(ioctl(dev_fd, XLOOP_SET_STATUS, &xloopinfo), TST_RETVAL_EQ0); + + TEST(ioctl(dev_fd, XLOOP_SET_DIRECT_IO, 1)); + if (TST_RET == 0) { + tst_res(TPASS, "XLOOP_SET_DIRECT_IO succeeded, offset is ignored"); + SAFE_IOCTL(dev_fd, XLOOP_SET_DIRECT_IO, 0); + return; + } + if (TST_ERR == EINVAL) + tst_res(TPASS | TTERRNO, "XLOOP_SET_DIRECT_IO failed as expected"); + else + tst_res(TFAIL | TTERRNO, "XLOOP_SET_DIRECT_IO failed expected EINVAL got"); +} + +static void setup(void) +{ + char bd_path[100]; + + if (tst_fs_type(".") == TST_TMPFS_MAGIC) + tst_brk(TCONF, "tmpfd doesn't support O_DIRECT flag"); + + dev_num = tst_find_free_xloopdev(dev_path, sizeof(dev_path)); + if (dev_num < 0) + tst_brk(TBROK, "Failed to find free xloop device"); + + sprintf(sys_xloop_diopath, "/sys/block/xloop%d/xloop/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); + + if (ioctl(dev_fd, XLOOP_SET_DIRECT_IO, 0) && errno == EINVAL) + tst_brk(TCONF, "XLOOP_SET_DIRECT_IO is not supported"); + + /* + * from __xloop_update_dio(): + * We support direct I/O only if xlo_offset is aligned with the + * logical I/O size of backing device, and the logical block + * size of xloop is bigger than the backing device's and the xloop + * needn't transform transfer. + */ + sprintf(backing_file_path, "%s/test.img", tst_get_tmpdir()); + tst_find_backing_dev(backing_file_path, bd_path); + block_devfd = SAFE_OPEN(bd_path, O_RDWR); + SAFE_IOCTL(block_devfd, BLKSSZGET, &logical_block_size); + tst_res(TINFO, "backing dev(%s) logical_block_size is %d", bd_path, logical_block_size); + SAFE_CLOSE(block_devfd); + if (logical_block_size > 512) + TST_RETRY_FUNC(ioctl(dev_fd, XLOOP_SET_BLOCK_SIZE, logical_block_size), TST_RETVAL_EQ0); +} + +static void cleanup(void) +{ + if (dev_fd > 0) + SAFE_CLOSE(dev_fd); + if (block_devfd > 0) + SAFE_CLOSE(block_devfd); + if (attach_flag) + tst_detach_device(dev_path); +} + +static struct tst_test test = { + .setup = setup, + .cleanup = cleanup, + .test_all = verify_ioctl_xloop, + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "xloop", + "xloop_file_fmt_raw", + NULL + } +}; diff --git a/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop06.c b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop06.c new file mode 100644 index 0000000..4bfa63e --- /dev/null +++ b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop06.c @@ -0,0 +1,144 @@ +// 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 error test about the invalid block size of xloopdevice + * by using XLOOP_SET_BLOCK_SIZE or XLOOP_CONFIGURE ioctl. + */ + +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <stdlib.h> +#include "lapi/xloop.h" +#include "tst_test.h" + +static char dev_path[1024]; +static int dev_num, dev_fd, file_fd, attach_flag, xloop_configure_sup = 1; +static unsigned int invalid_value, half_value, unalign_value; +static struct xloop_config xloopconfig; + +static struct tcase { + unsigned int *setvalue; + int ioctl_flag; + char *message; +} tcases[] = { + {&half_value, XLOOP_SET_BLOCK_SIZE, + "Using XLOOP_SET_BLOCK_SIZE with arg < 512"}, + + {&invalid_value, XLOOP_SET_BLOCK_SIZE, + "Using XLOOP_SET_BLOCK_SIZE with arg > PAGE_SIZE"}, + + {&unalign_value, XLOOP_SET_BLOCK_SIZE, + "Using XLOOP_SET_BLOCK_SIZE with arg != power_of_2"}, + + {&half_value, XLOOP_CONFIGURE, + "Using XLOOP_CONFIGURE with block_size < 512"}, + + {&invalid_value, XLOOP_CONFIGURE, + "Using XLOOP_CONFIGURE with block_size > PAGE_SIZE"}, + + {&unalign_value, XLOOP_CONFIGURE, + "Using XLOOP_CONFIGURE with block_size != power_of_2"}, +}; + +static void verify_ioctl_xloop(unsigned int n) +{ + if (tcases[n].ioctl_flag == XLOOP_CONFIGURE) + TEST(ioctl(dev_fd, XLOOP_CONFIGURE, &xloopconfig)); + else + TEST(ioctl(dev_fd, XLOOP_SET_BLOCK_SIZE, *(tcases[n].setvalue))); + + if (TST_RET == 0) { + tst_res(TFAIL, "Set block size succeed unexpectedly"); + if (tcases[n].ioctl_flag == XLOOP_CONFIGURE) + tst_detach_device_by_fd(dev_path, dev_fd); + return; + } + if (TST_ERR == EINVAL) + tst_res(TPASS | TTERRNO, "Set block size failed as expected"); + else + tst_res(TFAIL | TTERRNO, "Set block size failed expected EINVAL got"); +} + +static void run(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + + tst_res(TINFO, "%s", tc->message); + if (tc->ioctl_flag == XLOOP_SET_BLOCK_SIZE) { + if (!attach_flag) { + tst_attach_device(dev_path, "test.img"); + attach_flag = 1; + } + verify_ioctl_xloop(n); + return; + } + + if (tc->ioctl_flag == XLOOP_CONFIGURE && !xloop_configure_sup) { + tst_res(TCONF, "XLOOP_CONFIGURE ioctl not supported"); + return; + } + if (attach_flag) { + tst_detach_device_by_fd(dev_path, dev_fd); + attach_flag = 0; + } + xloopconfig.block_size = *(tc->setvalue); + verify_ioctl_xloop(n); +} + +static void setup(void) +{ + unsigned int pg_size; + int ret; + + dev_num = tst_find_free_xloopdev(dev_path, sizeof(dev_path)); + if (dev_num < 0) + tst_brk(TBROK, "Failed to find free xloop device"); + + tst_fill_file("test.img", 0, 1024, 1024); + half_value = 256; + pg_size = getpagesize(); + invalid_value = pg_size * 2 ; + unalign_value = pg_size - 1; + + dev_fd = SAFE_OPEN(dev_path, O_RDWR); + + if (ioctl(dev_fd, XLOOP_SET_BLOCK_SIZE, 512) && errno == EINVAL) + tst_brk(TCONF, "XLOOP_SET_BLOCK_SIZE is not supported"); + + file_fd = SAFE_OPEN("test.img", O_RDWR); + xloopconfig.fd = -1; + ret = ioctl(dev_fd, XLOOP_CONFIGURE, &xloopconfig); + if (ret && errno != EBADF) { + tst_res(TINFO | TERRNO, "XLOOP_CONFIGURE is not supported"); + xloop_configure_sup = 0; + return; + } + xloopconfig.fd = file_fd; +} + +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 = run, + .tcnt = ARRAY_SIZE(tcases), + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "xloop", + "xloop_file_fmt_raw", + NULL + } +}; diff --git a/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop07.c b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop07.c new file mode 100644 index 0000000..b51a970 --- /dev/null +++ b/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop07.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 xloopdevice XLOOP_SET_STATUS64 + * and XLOOP_GET_STATUS64. + * Test its xlo_sizelimit field. If xlo_sizelimit is 0,it means max + * available. If sizelimit is less than xloop_size, xloopsize will + * be truncated. + */ + +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <stdlib.h> +#include "lapi/xloop.h" +#include "tst_test.h" + +static char dev_path[1024], sys_xloop_sizepath[1024], sys_xloop_sizelimitpath[1024]; +static int dev_num, dev_fd, file_fd, attach_flag; + +static struct tcase { + unsigned int set_sizelimit; + unsigned int exp_xloopsize; + char *message; +} tcases[] = { + {1024 * 4096, 2048, "When sizelimit is greater than xloopsize "}, + {1024 * 512, 1024, "When sizelimit is less than xloopsize"}, +}; + +static void verify_ioctl_xloop(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + struct xloop_info64 xloopinfo, xloopinfoget; + + tst_res(TINFO, "%s", tc->message); + memset(&xloopinfo, 0, sizeof(xloopinfo)); + memset(&xloopinfoget, 0, sizeof(xloopinfoget)); + + xloopinfo.xlo_sizelimit = tc->set_sizelimit; + TST_RETRY_FUNC(ioctl(dev_fd, XLOOP_SET_STATUS64, &xloopinfo), TST_RETVAL_EQ0); + + TST_ASSERT_INT(sys_xloop_sizepath, tc->exp_xloopsize); + TST_ASSERT_INT(sys_xloop_sizelimitpath, tc->set_sizelimit); + SAFE_IOCTL(dev_fd, XLOOP_GET_STATUS64, &xloopinfoget); + if (xloopinfoget.xlo_sizelimit == tc->set_sizelimit) + tst_res(TPASS, "XLOOP_GET_STATUS64 gets correct xlo_sizelimit(%d)", tc->set_sizelimit); + else + tst_res(TFAIL, "XLOOP_GET_STATUS64 gets wrong xlo_sizelimit(%llu), expect %d", + xloopinfoget.xlo_sizelimit, tc->set_sizelimit); + /*Reset*/ + xloopinfo.xlo_sizelimit = 0; + TST_RETRY_FUNC(ioctl(dev_fd, XLOOP_SET_STATUS, &xloopinfo), TST_RETVAL_EQ0); +} + +static void setup(void) +{ + dev_num = tst_find_free_xloopdev(dev_path, sizeof(dev_path)); + if (dev_num < 0) + tst_brk(TBROK, "Failed to find free xloop device"); + + tst_fill_file("test.img", 0, 1024 * 1024, 1); + tst_attach_device(dev_path, "test.img"); + attach_flag = 1; + + sprintf(sys_xloop_sizepath, "/sys/block/xloop%d/size", dev_num); + sprintf(sys_xloop_sizelimitpath, "/sys/block/xloop%d/xloop/sizelimit", dev_num); + + dev_fd = SAFE_OPEN(dev_path, O_RDWR); + tst_res(TINFO, "original xloop 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_xloop, + .tcnt = ARRAY_SIZE(tcases), + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "xloop", + "xloop_file_fmt_raw", + NULL + } +}; |