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 */
7
8 /*\
9 * [Description]
10 *
11 * Test io_submit() invoked via libaio:
12 *
13 * - io_submit fails and returns -EINVAL if ctx is invalid.
14 * - io_submit fails and returns -EINVAL if nr is invalid.
15 * - io_submit fails and returns -EFAULT if iocbpp pointer is invalid.
16 * - io_submit fails and returns -EBADF if fd is invalid.
17 * - io_submit succeeds and returns the number of iocbs submitted.
18 * - io_submit succeeds and returns 0 if nr is zero.
19 */
20
21 #include <errno.h>
22 #include <string.h>
23 #include <fcntl.h>
24
25 #include "config.h"
26 #include "tst_test.h"
27
28 #ifdef HAVE_LIBAIO
29 #include <libaio.h>
30
31 static io_context_t ctx;
32 static io_context_t invalid_ctx;
33
34 static struct iocb iocb;
35 static struct iocb *iocbs[] = {&iocb};
36
37 static struct iocb inv_fd_iocb;
38 static struct iocb *inv_fd_iocbs[] = {&inv_fd_iocb};
39
40 static int rdonly_fd;
41 static struct iocb rdonly_fd_iocb;
42 static struct iocb *rdonly_fd_iocbs[] = {&rdonly_fd_iocb};
43
44 static int wronly_fd;
45 static struct iocb wronly_fd_iocb;
46 static struct iocb *wronly_fd_iocbs[] = {&wronly_fd_iocb};
47
48 static struct iocb zero_buf_iocb;
49 static struct iocb *zero_buf_iocbs[] = {&zero_buf_iocb};
50
51 static struct iocb *zero_iocbs[1];
52
53 static char buf[100];
54
55 static struct tcase {
56 io_context_t *ctx;
57 long nr;
58 struct iocb **iocbs;
59 int exp_errno;
60 const char *desc;
61 } tcases[] = {
62 /* Invalid ctx */
63 {&invalid_ctx, 1, iocbs, -EINVAL, "invalid ctx"},
64 /* Invalid nr */
65 {&ctx, -1, iocbs, -EINVAL, "invalid nr"},
66 /* Invalid pointer */
67 {&ctx, 1, (void*)-1, -EFAULT, "invalid iocbpp pointer"},
68 {&ctx, 1, zero_iocbs, -EFAULT, "NULL iocb pointers"},
69 /* Invalid fd */
70 {&ctx, 1, inv_fd_iocbs, -EBADF, "invalid fd"},
71 {&ctx, 1, rdonly_fd_iocbs, -EBADF, "readonly fd for write"},
72 {&ctx, 1, wronly_fd_iocbs, -EBADF, "writeonly fd for read"},
73 /* No-op but should work fine */
74 {&ctx, 1, zero_buf_iocbs, 1, "zero buf size"},
75 {&ctx, 0, NULL, 0, "zero nr"},
76 };
77
setup(void)78 static void setup(void)
79 {
80 TEST(io_setup(1, &ctx));
81 if (TST_RET == -ENOSYS)
82 tst_brk(TCONF, "io_setup(): AIO not supported by kernel");
83 else if (TST_RET) {
84 tst_brk(TBROK, "io_setup() returned %ld(%s)",
85 TST_RET, tst_strerrno(-TST_RET));
86 }
87
88 io_prep_pread(&inv_fd_iocb, -1, buf, sizeof(buf), 0);
89
90 rdonly_fd = SAFE_OPEN("rdonly_file", O_RDONLY | O_CREAT, 0777);
91 io_prep_pwrite(&rdonly_fd_iocb, rdonly_fd, buf, sizeof(buf), 0);
92
93 io_prep_pread(&zero_buf_iocb, rdonly_fd, buf, 0, 0);
94
95 wronly_fd = SAFE_OPEN("wronly_file", O_WRONLY | O_CREAT, 0777);
96 io_prep_pread(&wronly_fd_iocb, wronly_fd, buf, sizeof(buf), 0);
97 }
98
cleanup(void)99 static void cleanup(void)
100 {
101 if (rdonly_fd > 0)
102 SAFE_CLOSE(rdonly_fd);
103
104 if (wronly_fd > 0)
105 SAFE_CLOSE(wronly_fd);
106 }
107
errno_name(int err)108 static const char *errno_name(int err)
109 {
110 if (err <= 0)
111 return tst_strerrno(-err);
112
113 return "SUCCESS";
114 }
115
verify_io_submit(unsigned int n)116 static void verify_io_submit(unsigned int n)
117 {
118 struct tcase *t = &tcases[n];
119 struct io_event evbuf;
120 struct timespec timeout = { .tv_sec = 1 };
121 int i, ret;
122
123 ret = io_submit(*t->ctx, t->nr, t->iocbs);
124
125 if (ret == t->exp_errno) {
126 tst_res(TPASS, "io_submit() with %s failed with %s",
127 t->desc, errno_name(t->exp_errno));
128
129 for (i = 0; i < ret; i++)
130 io_getevents(*t->ctx, 1, 1, &evbuf, &timeout);
131
132 return;
133 }
134
135 tst_res(TFAIL, "io_submit() returned %i(%s), expected %s(%i)",
136 ret, ret < 0 ? tst_strerrno(-ret) : "SUCCESS",
137 errno_name(t->exp_errno), t->exp_errno);
138 }
139
140 static struct tst_test test = {
141 .setup = setup,
142 .cleanup = cleanup,
143 .test = verify_io_submit,
144 .tcnt = ARRAY_SIZE(tcases),
145 .needs_tmpdir = 1,
146 };
147
148 #else
149 TST_TEST_TCONF("test requires libaio and it's development packages");
150 #endif
151