1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Crackerjack Project., 2007
4 * Ported from Crackerjack to LTP by Masatake YAMATO <yamato@redhat.com>
5 * Copyright (c) 2011-2017 Cyril Hrubis <chrubis@suse.cz>
6 * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Test io_submit invoked via syscall(2):
13 *
14 * 1. io_submit() returns the number of iocbs submitted.
15 * 2. io_submit() returns 0 if nr is zero.
16 */
17
18 #include <linux/aio_abi.h>
19
20 #include "config.h"
21 #include "tst_test.h"
22 #include "lapi/syscalls.h"
23
24 #define TEST_FILE "test_file"
25 #define MODE 0777
26
27 static int fd;
28 static char buf[100];
29
30 static aio_context_t ctx;
31 static struct iocb iocb;
32 static struct iocb *iocbs[] = {&iocb};
33
34 static struct tcase {
35 aio_context_t *ctx;
36 long nr;
37 struct iocb **iocbs;
38 const char *desc;
39 } tc[] = {
40 {&ctx, 1, iocbs, "returns the number of iocbs submitted"},
41 {&ctx, 0, NULL, "returns 0 if nr is zero"},
42 };
43
io_prep_option(struct iocb * cb,int fd,void * buf,size_t count,long long offset,unsigned opcode)44 static inline void io_prep_option(struct iocb *cb, int fd, void *buf,
45 size_t count, long long offset, unsigned opcode)
46 {
47 memset(cb, 0, sizeof(*cb));
48 cb->aio_fildes = fd;
49 cb->aio_lio_opcode = opcode;
50 cb->aio_buf = (uint64_t)buf;
51 cb->aio_offset = offset;
52 cb->aio_nbytes = count;
53 }
54
setup(void)55 static void setup(void)
56 {
57 TST_EXP_PASS_SILENT(tst_syscall(__NR_io_setup, 1, &ctx));
58 fd = SAFE_OPEN(TEST_FILE, O_RDONLY | O_CREAT, MODE);
59 io_prep_option(&iocb, fd, buf, 0, 0, IOCB_CMD_PREAD);
60 }
61
cleanup(void)62 static void cleanup(void)
63 {
64 if (fd > 0)
65 SAFE_CLOSE(fd);
66
67 if (tst_syscall(__NR_io_destroy, ctx))
68 tst_brk(TBROK | TERRNO, "io_destroy() failed");
69 }
70
run(unsigned int i)71 static void run(unsigned int i)
72 {
73 TEST(tst_syscall(__NR_io_submit, *tc[i].ctx, tc[i].nr, tc[i].iocbs));
74
75 if (TST_RET == tc[i].nr)
76 tst_res(TPASS, "io_submit() %s", tc[i].desc);
77 else
78 tst_res(TFAIL, "io_submit() returns %ld, expected %ld", TST_RET, tc[i].nr);
79 }
80
81 static struct tst_test test = {
82 .tcnt = ARRAY_SIZE(tc),
83 .needs_tmpdir = 1,
84 .needs_kconfigs = (const char *[]) {
85 "CONFIG_AIO=y",
86 NULL
87 },
88 .setup = setup,
89 .cleanup = cleanup,
90 .test = run,
91 };
92