1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Crackerjack Project., 2007
4f08c3bdfSopenharmony_ci * Ported from Crackerjack to LTP by Masatake YAMATO <yamato@redhat.com>
5f08c3bdfSopenharmony_ci * Copyright (c) 2011-2017 Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci/*\
10f08c3bdfSopenharmony_ci * [Description]
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Test io_submit invoked via syscall(2):
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * 1. io_submit fails and returns EINVAL if ctx is invalid.
15f08c3bdfSopenharmony_ci * 2. io_submit fails and returns EINVAL if nr is invalid.
16f08c3bdfSopenharmony_ci * 3. io_submit fails and returns EFAULT if iocbpp pointer is invalid.
17f08c3bdfSopenharmony_ci * 4. io_submit fails and returns EBADF if fd is invalid.
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include <linux/aio_abi.h>
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include "config.h"
23f08c3bdfSopenharmony_ci#include "tst_test.h"
24f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#define RDONLY_FILE "rdonly_file"
27f08c3bdfSopenharmony_ci#define WRONLY_FILE "wronly_file"
28f08c3bdfSopenharmony_ci#define MODE 0777
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic char buf[100];
31f08c3bdfSopenharmony_cistatic aio_context_t ctx;
32f08c3bdfSopenharmony_cistatic aio_context_t invalid_ctx;
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic struct iocb iocb;
35f08c3bdfSopenharmony_cistatic struct iocb *iocbs[] = {&iocb};
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic struct iocb inv_fd_iocb;
38f08c3bdfSopenharmony_cistatic struct iocb *inv_fd_iocbs[] = {&inv_fd_iocb};
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic int rdonly_fd;
41f08c3bdfSopenharmony_cistatic struct iocb rdonly_fd_iocb;
42f08c3bdfSopenharmony_cistatic struct iocb *rdonly_fd_iocbs[] = {&rdonly_fd_iocb};
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic int wronly_fd;
45f08c3bdfSopenharmony_cistatic struct iocb wronly_fd_iocb;
46f08c3bdfSopenharmony_cistatic struct iocb *wronly_fd_iocbs[] = {&wronly_fd_iocb};
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic struct iocb *zero_iocbs[1];
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic struct tcase {
51f08c3bdfSopenharmony_ci	aio_context_t *ctx;
52f08c3bdfSopenharmony_ci	long nr;
53f08c3bdfSopenharmony_ci	struct iocb **iocbs;
54f08c3bdfSopenharmony_ci	int exp_errno;
55f08c3bdfSopenharmony_ci	const char *desc;
56f08c3bdfSopenharmony_ci} tc[] = {
57f08c3bdfSopenharmony_ci	/* Invalid ctx */
58f08c3bdfSopenharmony_ci	{&invalid_ctx, 1, iocbs, EINVAL, "invalid ctx"},
59f08c3bdfSopenharmony_ci	/* Invalid nr */
60f08c3bdfSopenharmony_ci	{&ctx, -1, iocbs, EINVAL, "invalid nr"},
61f08c3bdfSopenharmony_ci	/* Invalid pointer */
62f08c3bdfSopenharmony_ci	{&ctx, 1, (void*)-1, EFAULT, "invalid iocbpp pointer"},
63f08c3bdfSopenharmony_ci	{&ctx, 1, zero_iocbs, EFAULT, "NULL iocb pointers"},
64f08c3bdfSopenharmony_ci	/* Invalid fd */
65f08c3bdfSopenharmony_ci	{&ctx, 1, inv_fd_iocbs, EBADF, "invalid fd"},
66f08c3bdfSopenharmony_ci	{&ctx, 1, rdonly_fd_iocbs, EBADF, "readonly fd for write"},
67f08c3bdfSopenharmony_ci	{&ctx, 1, wronly_fd_iocbs, EBADF, "writeonly fd for read"},
68f08c3bdfSopenharmony_ci};
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic inline void io_prep_option(struct iocb *cb, int fd, void *buf,
71f08c3bdfSopenharmony_ci			size_t count, long long offset, unsigned opcode)
72f08c3bdfSopenharmony_ci{
73f08c3bdfSopenharmony_ci	memset(cb, 0, sizeof(*cb));
74f08c3bdfSopenharmony_ci	cb->aio_fildes = fd;
75f08c3bdfSopenharmony_ci	cb->aio_lio_opcode = opcode;
76f08c3bdfSopenharmony_ci	cb->aio_buf = (uint64_t)buf;
77f08c3bdfSopenharmony_ci	cb->aio_offset = offset;
78f08c3bdfSopenharmony_ci	cb->aio_nbytes = count;
79f08c3bdfSopenharmony_ci}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cistatic void setup(void)
82f08c3bdfSopenharmony_ci{
83f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(tst_syscall(__NR_io_setup, 1, &ctx));
84f08c3bdfSopenharmony_ci	io_prep_option(&inv_fd_iocb, -1, buf, sizeof(buf), 0, IOCB_CMD_PREAD);
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	rdonly_fd = SAFE_OPEN(RDONLY_FILE, O_RDONLY | O_CREAT, MODE);
87f08c3bdfSopenharmony_ci	io_prep_option(&rdonly_fd_iocb, rdonly_fd, buf, sizeof(buf), 0, IOCB_CMD_PWRITE);
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	wronly_fd = SAFE_OPEN(WRONLY_FILE, O_WRONLY | O_CREAT, MODE);
90f08c3bdfSopenharmony_ci	io_prep_option(&wronly_fd_iocb, wronly_fd, buf, sizeof(buf), 0, IOCB_CMD_PREAD);
91f08c3bdfSopenharmony_ci}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_cistatic void cleanup(void)
94f08c3bdfSopenharmony_ci{
95f08c3bdfSopenharmony_ci	if (rdonly_fd > 0)
96f08c3bdfSopenharmony_ci		SAFE_CLOSE(rdonly_fd);
97f08c3bdfSopenharmony_ci	if (wronly_fd > 0)
98f08c3bdfSopenharmony_ci		SAFE_CLOSE(wronly_fd);
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	if (tst_syscall(__NR_io_destroy, ctx))
101f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "io_destroy() failed");
102f08c3bdfSopenharmony_ci}
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_cistatic void run(unsigned int i)
105f08c3bdfSopenharmony_ci{
106f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(tst_syscall(__NR_io_submit, *tc[i].ctx, tc[i].nr, tc[i].iocbs),
107f08c3bdfSopenharmony_ci		     tc[i].exp_errno, "io_submit() with %s", tc[i].desc);
108f08c3bdfSopenharmony_ci}
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_cistatic struct tst_test test = {
111f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tc),
112f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
113f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
114f08c3bdfSopenharmony_ci		"CONFIG_AIO=y",
115f08c3bdfSopenharmony_ci		NULL
116f08c3bdfSopenharmony_ci	},
117f08c3bdfSopenharmony_ci	.setup = setup,
118f08c3bdfSopenharmony_ci	.cleanup = cleanup,
119f08c3bdfSopenharmony_ci	.test = run,
120f08c3bdfSopenharmony_ci};
121