xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/io_submit/io_submit02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 <[email protected]>
5  * Copyright (c) 2011-2017 Cyril Hrubis <[email protected]>
6  * Copyright (c) 2021 Xie Ziyao <[email protected]>
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 	struct io_event evbuf;
74 	struct timespec timeout = { .tv_sec = 1 };
75 	long j;
76 
77 	TEST(tst_syscall(__NR_io_submit, *tc[i].ctx, tc[i].nr, tc[i].iocbs));
78 
79 	if (TST_RET == tc[i].nr)
80 		tst_res(TPASS, "io_submit() %s", tc[i].desc);
81 	else
82 		tst_res(TFAIL | TTERRNO, "io_submit() returns %ld, expected %ld", TST_RET, tc[i].nr);
83 
84 	for (j = 0; j < TST_RET; j++) {
85 		tst_syscall(__NR_io_getevents, *tc[i].ctx, 1, 1, &evbuf,
86 			&timeout);
87 	}
88 }
89 
90 static struct tst_test test = {
91 	.tcnt = ARRAY_SIZE(tc),
92 	.needs_tmpdir = 1,
93 	.needs_kconfigs = (const char *[]) {
94 		"CONFIG_AIO=y",
95 		NULL
96 	},
97 	.setup = setup,
98 	.cleanup = cleanup,
99 	.test = run,
100 };
101