• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 | TRERRNO, "io_setup(): AIO not supported by kernel");
83 	else if (TST_RET)
84 		tst_brk(TBROK | TRERRNO, "io_setup() failed");
85 
86 	io_prep_pread(&inv_fd_iocb, -1, buf, sizeof(buf), 0);
87 
88 	rdonly_fd = SAFE_OPEN("rdonly_file", O_RDONLY | O_CREAT, 0777);
89 	io_prep_pwrite(&rdonly_fd_iocb, rdonly_fd, buf, sizeof(buf), 0);
90 
91 	io_prep_pread(&zero_buf_iocb, rdonly_fd, buf, 0, 0);
92 
93 	wronly_fd = SAFE_OPEN("wronly_file", O_WRONLY | O_CREAT, 0777);
94 	io_prep_pread(&wronly_fd_iocb, wronly_fd, buf, sizeof(buf), 0);
95 }
96 
cleanup(void)97 static void cleanup(void)
98 {
99 	if (rdonly_fd > 0)
100 		SAFE_CLOSE(rdonly_fd);
101 
102 	if (wronly_fd > 0)
103 		SAFE_CLOSE(wronly_fd);
104 }
105 
errno_name(int err)106 static const char *errno_name(int err)
107 {
108 	if (err <= 0)
109 		return tst_strerrno(-err);
110 
111 	return "SUCCESS";
112 }
113 
verify_io_submit(unsigned int n)114 static void verify_io_submit(unsigned int n)
115 {
116 	struct tcase *t = &tcases[n];
117 	int ret;
118 
119 	ret = io_submit(*t->ctx, t->nr, t->iocbs);
120 
121 	if (ret == t->exp_errno) {
122 		tst_res(TPASS, "io_submit() with %s failed with %s",
123 			t->desc, errno_name(t->exp_errno));
124 		return;
125 	}
126 
127 	tst_res(TFAIL, "io_submit() returned %i(%s), expected %s(%i)",
128 		ret, ret < 0 ? tst_strerrno(-ret) : "SUCCESS",
129 		errno_name(t->exp_errno), t->exp_errno);
130 }
131 
132 static struct tst_test test = {
133 	.setup = setup,
134 	.cleanup = cleanup,
135 	.test = verify_io_submit,
136 	.tcnt = ARRAY_SIZE(tcases),
137 	.needs_tmpdir = 1,
138 };
139 
140 #else
141 	TST_TEST_TCONF("test requires libaio and it's development packages");
142 #endif
143