diff options
Diffstat (limited to 'kernel/tests/testcases/kernel/syscalls')
8 files changed, 917 insertions, 0 deletions
diff --git a/kernel/tests/testcases/kernel/syscalls/ioctl/CMakeLists.txt b/kernel/tests/testcases/kernel/syscalls/ioctl/CMakeLists.txt new file mode 100644 index 0000000..e0687ec --- /dev/null +++ b/kernel/tests/testcases/kernel/syscalls/ioctl/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 3.12) + +project(xloop-kernel-test-ioctl-xloop) + +# test ioctl_xloop01 +add_executable(tst_ioctl_xloop01 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_loop01.c) +target_link_libraries(tst_ioctl_xloop01 LINK_PUBLIC libltp) +install(TARGETS tst_ioctl_xloop01 DESTINATION bin) + +# test ioctl_xloop02 +add_executable(tst_ioctl_xloop02 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_loop02.c) +target_link_libraries(tst_ioctl_xloop02 LINK_PUBLIC libltp) + +# test ioctl_xloop03 +add_executable(tst_ioctl_xloop03 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_loop03.c) +target_link_libraries(tst_ioctl_xloop03 LINK_PUBLIC libltp) + +# test ioctl_xloop04 +add_executable(tst_ioctl_xloop04 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_loop04.c) +target_link_libraries(tst_ioctl_xloop04 LINK_PUBLIC libltp) + +# test ioctl_xloop05 +add_executable(tst_ioctl_xloop05 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_loop05.c) +target_link_libraries(tst_ioctl_xloop05 LINK_PUBLIC libltp) + +# test ioctl_xloop06 +add_executable(tst_ioctl_xloop06 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_loop06.c) +target_link_libraries(tst_ioctl_xloop06 LINK_PUBLIC libltp) + +# test ioctl_xloop07 +add_executable(tst_ioctl_xloop07 ${CMAKE_CURRENT_SOURCE_DIR}/ioctl_loop07.c) +target_link_libraries(tst_ioctl_xloop07 LINK_PUBLIC libltp)
\ No newline at end of file diff --git a/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop01.c b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop01.c new file mode 100644 index 0000000..cf71184 --- /dev/null +++ b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop01.c @@ -0,0 +1,155 @@ +// 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 field value 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. + * + * For LO_FLAGS_AUTOCLEAR flag, it can be clear. For LO_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 ("loop: Fix wrong masking of status flags"). + */ + +#include <stdio.h> +#include <unistd.h> +#include <string.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 read only mode + * file_fd, lo_flags will include LO_FLAGS_READ_ONLY and it's the same for + * LO_FLAGS_DIRECT_IO. + */ +#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 check_loop_value(int set_flag, int get_flag, int autoclear_field) +{ + struct loop_info loopinfo = {0}, loopinfoget = {0}; + int ret; + + loopinfo.lo_flags = set_flag; + SAFE_IOCTL(dev_fd, LOOP_SET_STATUS, &loopinfo); + SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget); + + if (loopinfoget.lo_flags & ~get_flag) + tst_res(TFAIL, "expect %d but got %d", get_flag, loopinfoget.lo_flags); + else + tst_res(TPASS, "get expected lo_flag %d", loopinfoget.lo_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(loop_partpath, F_OK), TST_RETVAL_EQ0, 30); + if (ret == 0) + tst_res(TPASS, "access %s succeeds", loop_partpath); + else + tst_res(TFAIL, "access %s fails", loop_partpath); + + ret = TST_RETRY_FN_EXP_BACKOFF(access(sys_loop_partpath, F_OK), TST_RETVAL_EQ0, 30); + if (ret == 0) + tst_res(TPASS, "access %s succeeds", sys_loop_partpath); + else + tst_res(TFAIL, "access %s fails", sys_loop_partpath); +} + +static void verify_ioctl_loop(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_loop_value(SET_FLAGS, GET_FLAGS, 1); + + tst_res(TINFO, "Test flag can be clear"); + check_loop_value(0, LO_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_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); + 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_loop, + .needs_root = 1, + .needs_drivers = (const char *const []) { + "loop", + NULL + }, + .tags = (const struct tst_tag[]) { + {"linux-git", "10c70d95c0f2"}, + {"linux-git", "6ac92fb5cdff"}, + {} + }, + .needs_tmpdir = 1, +}; diff --git a/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop02.c b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop02.c new file mode 100644 index 0000000..ac61842 --- /dev/null +++ b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop02.c @@ -0,0 +1,164 @@ +// 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. + * + * If using LOOP_CONFIGURE ioctl, we can set LO_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/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, loop_configure_sup = 1; +static char loop_ro_path[1024], dev_path[1024]; +static struct loop_config loopconfig; + +static struct tcase { + int mode; + int ioctl; + char *message; +} tcases[] = { + {O_RDONLY, LOOP_SET_FD, "Using LOOP_SET_FD to setup loopdevice"}, + {O_RDWR, LOOP_CONFIGURE, "Using LOOP_CONFIGURE with read_only flag"}, +}; + +static void verify_ioctl_loop(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + struct loop_info loopinfoget; + + if (tc->ioctl == LOOP_CONFIGURE && !loop_configure_sup) { + tst_res(TCONF, "LOOP_CONFIGURE ioctl not supported"); + return; + } + + tst_res(TINFO, "%s", tc->message); + file_fd = SAFE_OPEN("test.img", tc->mode); + + if (tc->ioctl == LOOP_SET_FD) { + SAFE_IOCTL(dev_fd, LOOP_SET_FD, file_fd); + } else { + loopconfig.fd = file_fd; + SAFE_IOCTL(dev_fd, LOOP_CONFIGURE, &loopconfig); + } + attach_flag = 1; + + TST_ASSERT_INT(loop_ro_path, 1); + TST_ASSERT_STR(backing_path, backing_file_path); + + memset(&loopinfoget, 0, sizeof(loopinfoget)); + + 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"); + TST_ASSERT_INT(loop_ro_path, 1); + TST_ASSERT_STR(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(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_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); + + 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); + loopconfig.fd = -1; + ret = ioctl(dev_fd, LOOP_CONFIGURE, &loopconfig); + + if (ret && errno != EBADF) { + tst_res(TINFO | TERRNO, "LOOP_CONFIGURE is not supported"); + loop_configure_sup = 0; + } + loopconfig.info.lo_flags = LO_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_loop, + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "loop", + NULL + } +}; diff --git a/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop03.c b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop03.c new file mode 100644 index 0000000..9cf5a41 --- /dev/null +++ b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop03.c @@ -0,0 +1,75 @@ +// 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 EINVAL error) + * when loop_dev is not read only. + */ + +#include <stdio.h> +#include <unistd.h> +#include <string.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 + } +}; diff --git a/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop04.c b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop04.c new file mode 100644 index 0000000..b4ab44a --- /dev/null +++ b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop04.c @@ -0,0 +1,98 @@ +// 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 "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; + + TST_ASSERT_INT(sys_loop_sizepath, OLD_SIZE/512); + 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); + + SAFE_LSEEK(dev_fd, 0, SEEK_SET); + + /*check that we can't write data beyond 5K into loop device*/ + TEST(write(dev_fd, wrbuf, OLD_SIZE)); + if (TST_RET == NEW_SIZE) { + tst_res(TPASS, "LOOP_SET_CAPACITY set loop size to %d", NEW_SIZE); + } else { + tst_res(TFAIL, "LOOP_SET_CAPACITY didn't set loop size to %d, its size is %ld", + NEW_SIZE, TST_RET); + } + + TST_ASSERT_INT(sys_loop_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_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); + 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_loop, + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "loop", + NULL + } +}; diff --git a/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop05.c b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop05.c new file mode 100644 index 0000000..e3c14fa --- /dev/null +++ b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop05.c @@ -0,0 +1,155 @@ +// 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 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. + * + * 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 LOOP_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/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], 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 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"); + + TST_ASSERT_INT(sys_loop_diopath, flag); +} + +static void verify_ioctl_loop(void) +{ + struct loop_info loopinfo; + + memset(&loopinfo, 0, sizeof(loopinfo)); + TST_RETRY_FUNC(ioctl(dev_fd, LOOP_SET_STATUS, &loopinfo), TST_RETVAL_EQ0); + + 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 nonzero offset less than 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(TPASS, "LOOP_SET_DIRECT_IO succeeded, offset is ignored"); + 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"); +} + +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_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); + + if (ioctl(dev_fd, LOOP_SET_DIRECT_IO, 0) && errno == EINVAL) + tst_brk(TCONF, "LOOP_SET_DIRECT_IO is not supported"); + + /* + * from __loop_update_dio(): + * 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. + */ + 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, LOOP_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_loop, + .needs_root = 1, + .needs_tmpdir = 1, + .needs_drivers = (const char *const []) { + "loop", + NULL + } +}; diff --git a/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop06.c b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop06.c new file mode 100644 index 0000000..bd0d289 --- /dev/null +++ b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop06.c @@ -0,0 +1,143 @@ +// 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 loopdevice + * by using LOOP_SET_BLOCK_SIZE or LOOP_CONFIGURE ioctl. + */ + +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <stdlib.h> +#include "lapi/loop.h" +#include "tst_test.h" + +static char dev_path[1024]; +static int dev_num, dev_fd, file_fd, attach_flag, loop_configure_sup = 1; +static unsigned int invalid_value, half_value, unalign_value; +static struct loop_config loopconfig; + +static struct tcase { + unsigned int *setvalue; + int ioctl_flag; + char *message; +} tcases[] = { + {&half_value, LOOP_SET_BLOCK_SIZE, + "Using LOOP_SET_BLOCK_SIZE with arg < 512"}, + + {&invalid_value, LOOP_SET_BLOCK_SIZE, + "Using LOOP_SET_BLOCK_SIZE with arg > PAGE_SIZE"}, + + {&unalign_value, LOOP_SET_BLOCK_SIZE, + "Using LOOP_SET_BLOCK_SIZE with arg != power_of_2"}, + + {&half_value, LOOP_CONFIGURE, + "Using LOOP_CONFIGURE with block_size < 512"}, + + {&invalid_value, LOOP_CONFIGURE, + "Using LOOP_CONFIGURE with block_size > PAGE_SIZE"}, + + {&unalign_value, LOOP_CONFIGURE, + "Using LOOP_CONFIGURE with block_size != power_of_2"}, +}; + +static void verify_ioctl_loop(unsigned int n) +{ + if (tcases[n].ioctl_flag == LOOP_CONFIGURE) + TEST(ioctl(dev_fd, LOOP_CONFIGURE, &loopconfig)); + else + TEST(ioctl(dev_fd, LOOP_SET_BLOCK_SIZE, *(tcases[n].setvalue))); + + if (TST_RET == 0) { + tst_res(TFAIL, "Set block size succeed unexpectedly"); + if (tcases[n].ioctl_flag == LOOP_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 == LOOP_SET_BLOCK_SIZE) { + if (!attach_flag) { + tst_attach_device(dev_path, "test.img"); + attach_flag = 1; + } + verify_ioctl_loop(n); + return; + } + + if (tc->ioctl_flag == LOOP_CONFIGURE && !loop_configure_sup) { + tst_res(TCONF, "LOOP_CONFIGURE ioctl not supported"); + return; + } + if (attach_flag) { + tst_detach_device_by_fd(dev_path, dev_fd); + attach_flag = 0; + } + loopconfig.block_size = *(tc->setvalue); + verify_ioctl_loop(n); +} + +static void setup(void) +{ + unsigned int pg_size; + int ret; + + 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); + 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, LOOP_SET_BLOCK_SIZE, 512) && errno == EINVAL) + tst_brk(TCONF, "LOOP_SET_BLOCK_SIZE is not supported"); + + file_fd = SAFE_OPEN("test.img", O_RDWR); + loopconfig.fd = -1; + ret = ioctl(dev_fd, LOOP_CONFIGURE, &loopconfig); + if (ret && errno != EBADF) { + tst_res(TINFO | TERRNO, "LOOP_CONFIGURE is not supported"); + loop_configure_sup = 0; + return; + } + loopconfig.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 []) { + "loop", + NULL + } +}; diff --git a/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop07.c b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop07.c new file mode 100644 index 0000000..ce4b476 --- /dev/null +++ b/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_loop07.c @@ -0,0 +1,95 @@ +// 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 "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); + + TST_ASSERT_INT(sys_loop_sizepath, tc->exp_loopsize); + TST_ASSERT_INT(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 + } +}; |