1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Test case for SQPOLL missing a 'ret' clear in case of busy.
4 *
5 * Heavily based on a test case from
6 * Xiaoguang Wang <[email protected]>
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <string.h>
13
14 #include "helpers.h"
15 #include "liburing.h"
16
17 #define FILE_SIZE (128 * 1024)
18
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21 struct io_uring ring;
22 int i, fd, ret;
23 struct io_uring_sqe *sqe;
24 struct io_uring_cqe *cqe;
25 struct iovec *iovecs;
26 struct io_uring_params p;
27 char *fname;
28 void *buf;
29
30 memset(&p, 0, sizeof(p));
31 p.flags = IORING_SETUP_SQPOLL;
32 ret = t_create_ring_params(4, &ring, &p);
33 if (ret == T_SETUP_SKIP)
34 return 0;
35 else if (ret < 0)
36 return 1;
37
38 if (argc > 1) {
39 fname = argv[1];
40 } else {
41 fname = ".sqpoll.tmp";
42 t_create_file(fname, FILE_SIZE);
43 }
44
45 fd = open(fname, O_RDONLY | O_DIRECT);
46 if (fname != argv[1])
47 unlink(fname);
48 if (fd < 0) {
49 perror("open");
50 goto out;
51 }
52
53 iovecs = t_calloc(10, sizeof(struct iovec));
54 for (i = 0; i < 10; i++) {
55 t_posix_memalign(&buf, 4096, 4096);
56 iovecs[i].iov_base = buf;
57 iovecs[i].iov_len = 4096;
58 }
59
60 ret = io_uring_register_files(&ring, &fd, 1);
61 if (ret < 0) {
62 fprintf(stderr, "register files %d\n", ret);
63 goto out;
64 }
65
66 for (i = 0; i < 10; i++) {
67 sqe = io_uring_get_sqe(&ring);
68 if (!sqe)
69 break;
70
71 io_uring_prep_readv(sqe, 0, &iovecs[i], 1, 0);
72 sqe->flags |= IOSQE_FIXED_FILE;
73
74 ret = io_uring_submit(&ring);
75 usleep(1000);
76 }
77
78 for (i = 0; i < 10; i++) {
79 ret = io_uring_wait_cqe(&ring, &cqe);
80 if (ret) {
81 fprintf(stderr, "wait_cqe=%d\n", ret);
82 break;
83 }
84 if (cqe->res != 4096) {
85 fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
86 ret = 1;
87 break;
88 }
89 io_uring_cqe_seen(&ring, cqe);
90 }
91
92 close(fd);
93 out:
94 io_uring_queue_exit(&ring);
95 return ret;
96 }
97