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