blob: 3b15faef4050d6a9bb757b458775fc612a872645 [file] [log] [blame]
subrata_modakc5792c92008-07-11 20:21:42 +00001/*
2 *
3 * Copyright (c) Crackerjack Project., 2007
Cyril Hrubisd8a90be2011-03-16 18:13:38 +01004 * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
subrata_modakc5792c92008-07-11 20:21:42 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modakc5792c92008-07-11 20:21:42 +000019 */
20
21/* Porting from Crackerjack to LTP is done
22 by Masatake YAMATO <yamato@redhat.com> */
23
subrata_modak79cefc12009-01-20 10:48:41 +000024#include "config.h"
subrata_modakc5792c92008-07-11 20:21:42 +000025#include "test.h"
subrata_modakc5792c92008-07-11 20:21:42 +000026
Cyril Hrubisd8a90be2011-03-16 18:13:38 +010027char *TCID = "io_submit01";
subrata_modakc5792c92008-07-11 20:21:42 +000028
subrata_modak18dc0c22008-07-21 10:55:34 +000029int TST_TOTAL = 3;
subrata_modakc5792c92008-07-11 20:21:42 +000030
subrata_modak79cefc12009-01-20 10:48:41 +000031#ifdef HAVE_LIBAIO_H
32#include <libaio.h>
33#include <errno.h>
34#include <string.h>
Caspar Zhangadfd02e2011-04-14 22:28:46 +080035#include <fcntl.h>
36
37#define TESTFILE "testfile"
subrata_modak79cefc12009-01-20 10:48:41 +000038
Cyril Hrubisd8a90be2011-03-16 18:13:38 +010039static void cleanup(void)
subrata_modakc5792c92008-07-11 20:21:42 +000040{
Caspar Zhangadfd02e2011-04-14 22:28:46 +080041 tst_rmdir();
subrata_modakc5792c92008-07-11 20:21:42 +000042}
43
Cyril Hrubisd8a90be2011-03-16 18:13:38 +010044static void setup(void)
subrata_modakc5792c92008-07-11 20:21:42 +000045{
Caspar Zhangadfd02e2011-04-14 22:28:46 +080046 int fd;
47
subrata_modak56207ce2009-03-23 13:35:39 +000048 tst_sig(NOFORK, DEF_HANDLER, cleanup);
subrata_modakc5792c92008-07-11 20:21:42 +000049
subrata_modak56207ce2009-03-23 13:35:39 +000050 TEST_PAUSE;
Caspar Zhangadfd02e2011-04-14 22:28:46 +080051
52 tst_tmpdir();
53
Wanlong Gao354ebb42012-12-07 10:10:04 +080054 fd = open(TESTFILE, O_CREAT | O_RDWR, 0755);
Caspar Zhangadfd02e2011-04-14 22:28:46 +080055 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080056 tst_brkm(TBROK | TERRNO, cleanup, "open");
Caspar Zhangadfd02e2011-04-14 22:28:46 +080057 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080058 tst_brkm(TBROK | TERRNO, cleanup, "close");
Garrett Cooper2c282152010-12-16 00:55:50 -080059}
subrata_modakc5792c92008-07-11 20:21:42 +000060
Caspar Zhangadfd02e2011-04-14 22:28:46 +080061static void check_result(long exp, long act)
62{
63 if (exp >= 0) {
64 if (act == exp)
65 tst_resm(TPASS, "expected success - "
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 "returned value = %ld", act);
Caspar Zhangadfd02e2011-04-14 22:28:46 +080067 else
68 tst_resm(TFAIL, "unexpected failure - "
Wanlong Gao354ebb42012-12-07 10:10:04 +080069 "returned value = %ld : %s",
70 act, strerror(-1 * act));
Caspar Zhangadfd02e2011-04-14 22:28:46 +080071 return;
72 }
subrata_modakc5792c92008-07-11 20:21:42 +000073
Caspar Zhangadfd02e2011-04-14 22:28:46 +080074 /* if return value is expected to be < 0 */
75 if (act == exp)
76 tst_resm(TPASS, "expected failure - "
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 "returned value = %ld : %s", act, strerror(-1 * act));
Caspar Zhangadfd02e2011-04-14 22:28:46 +080078 else if (act == 0)
79 tst_resm(TFAIL, "call succeeded unexpectedly");
80 else
81 tst_resm(TFAIL, "unexpected failure - "
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 "returned value = %ld : %s, "
83 "expected value = %ld : %s",
84 act, strerror(-1 * act), exp, strerror(-1 * exp));
Caspar Zhangadfd02e2011-04-14 22:28:46 +080085}
Cyril Hrubisd8a90be2011-03-16 18:13:38 +010086
Cyril Hrubisd8a90be2011-03-16 18:13:38 +010087int main(int argc, char *argv[])
88{
89 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020090 const char *msg;
Cyril Hrubisd8a90be2011-03-16 18:13:38 +010091
Caspar Zhangadfd02e2011-04-14 22:28:46 +080092 int rval, fd;
subrata_modakc5792c92008-07-11 20:21:42 +000093 char buf[256];
94 struct iocb iocb;
95 struct iocb *iocbs[1];
Cyril Hrubisd8a90be2011-03-16 18:13:38 +010096 io_context_t ctx;
97
Garrett Cooper54587dd2010-12-21 11:37:16 -080098 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakc5792c92008-07-11 20:21:42 +0000100
101 setup();
102
subrata_modakc5792c92008-07-11 20:21:42 +0000103 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800104 tst_count = 0;
subrata_modakc5792c92008-07-11 20:21:42 +0000105
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800106 /* 1 - EINVAL */
107 /* 1.1 - EINVAL: invalid ctx */
Jan Stancekf1fd7672014-05-06 13:13:25 +0200108 memset(&ctx, 0, sizeof(ctx));
subrata_modak56207ce2009-03-23 13:35:39 +0000109 TEST(io_submit(ctx, 0, NULL));
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800110 check_result(-EINVAL, TEST_RETURN);
subrata_modakc5792c92008-07-11 20:21:42 +0000111
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800112 /* 1.2 - EINVAL: invalid nr */
subrata_modak56207ce2009-03-23 13:35:39 +0000113 rval = io_setup(1, &ctx);
Garrett Cooper54587dd2010-12-21 11:37:16 -0800114 if (rval != 0)
subrata_modakc5792c92008-07-11 20:21:42 +0000115 tst_brkm(TBROK, cleanup, "io_setup failed: %d", rval);
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800116 TEST(io_submit(ctx, -1, NULL));
117 check_result(-EINVAL, TEST_RETURN);
subrata_modakc5792c92008-07-11 20:21:42 +0000118
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800119 /* 1.3 - EINVAL: uninitialized iocb */
120 iocbs[0] = &iocb;
Jan Stancek42c58982014-05-06 13:07:20 +0200121
122 /* There are multiple checks we can hit with uninitialized
123 * iocb, but with "random" data it's not 100%. Make sure we
124 * fail eventually in opcode check. */
125 iocb.aio_lio_opcode = -1;
126
subrata_modak56207ce2009-03-23 13:35:39 +0000127 TEST(io_submit(ctx, 1, iocbs));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128 switch (TEST_RETURN) {
Jan Stancek07694a12012-03-15 10:15:55 +0100129 case -EINVAL:
130 case -EBADF:
131 case -EFAULT:
132 tst_resm(TPASS, "expected failure - "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 "returned value = %ld : %s",
134 TEST_RETURN, strerror(-1 * TEST_RETURN));
Jan Stancek07694a12012-03-15 10:15:55 +0100135 break;
136 default:
137 tst_resm(TFAIL, "unexpected failure - "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 "returned value = %ld : %s, "
139 "expected one of -EINVAL, -EBADF, -EFAULT",
140 TEST_RETURN, strerror(-1 * TEST_RETURN));
Jan Stancek07694a12012-03-15 10:15:55 +0100141 }
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800142
143 /* 2 - EFAULT: iocb points to invalid data */
144 TEST(io_submit(ctx, 1, (struct iocb **)-1));
145 check_result(-EFAULT, TEST_RETURN);
146
147 /*
148 * 3 - Special case EFAULT or EINVAL (indetermination)
149 *
150 * The errno depends on the per architecture implementation
151 * of io_submit. On the architecture using compat_sys_io_submit
152 * as its implementation, errno is set to -EINVAL.
153 */
154 TEST(io_submit(ctx, -1, (struct iocb **)-1));
Garrett Cooper54587dd2010-12-21 11:37:16 -0800155 if (TEST_RETURN == 0)
subrata_modakc5792c92008-07-11 20:21:42 +0000156 tst_resm(TFAIL, "call succeeded unexpectedly");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 else if (TEST_RETURN == -EFAULT || TEST_RETURN == -EINVAL)
subrata_modakc5792c92008-07-11 20:21:42 +0000158 tst_resm(TPASS, "expected failure - "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 "returned value = %ld : %s",
160 TEST_RETURN, strerror(-1 * TEST_RETURN));
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800161 else
162 tst_resm(TFAIL, "unexpected failure - "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 "returned value = %ld : %s, "
164 "expected = %d : %s or %d : %s",
165 TEST_RETURN, strerror(-1 * TEST_RETURN),
166 -EFAULT, strerror(EFAULT),
167 -EINVAL, strerror(EINVAL));
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800168
169 /*
170 * 4 - EBADF: fd in iocb is invalid
171 */
172 io_prep_pread(&iocb, -1, buf, sizeof(buf), 0);
173 iocbs[0] = &iocb;
174 TEST(io_submit(ctx, 1, iocbs));
175 check_result(-EBADF, TEST_RETURN);
176
177 /* 5 - Positive test: nr == 0 */
178 TEST(io_submit(ctx, 0, NULL));
179 check_result(0, TEST_RETURN);
180
181 /* 6 - Positive test: valid fd */
182 fd = open(TESTFILE, O_RDONLY);
183 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 tst_resm(TBROK | TERRNO, "open");
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800185 io_prep_pread(&iocb, fd, buf, sizeof(buf), 0);
186 iocbs[0] = &iocb;
187 TEST(io_submit(ctx, 1, iocbs));
188 check_result(1, TEST_RETURN);
189 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 tst_resm(TBROK | TERRNO, "close");
Caspar Zhangadfd02e2011-04-14 22:28:46 +0800191
subrata_modakc5792c92008-07-11 20:21:42 +0000192 }
193 cleanup();
194
Garrett Cooper2c282152010-12-16 00:55:50 -0800195 tst_exit();
subrata_modakc5792c92008-07-11 20:21:42 +0000196}
subrata_modak79cefc12009-01-20 10:48:41 +0000197#else
Cyril Hrubisd8a90be2011-03-16 18:13:38 +0100198int main(int argc, char *argv[])
subrata_modak79cefc12009-01-20 10:48:41 +0000199{
Garrett Cooper54587dd2010-12-21 11:37:16 -0800200 tst_brkm(TCONF, NULL, "System doesn't support execution of the test");
subrata_modak79cefc12009-01-20 10:48:41 +0000201}
Garrett Cooper54587dd2010-12-21 11:37:16 -0800202#endif