Jens Axboe | e502435 | 2020-02-11 20:34:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Description: -EAGAIN handling |
| 4 | * |
| 5 | */ |
| 6 | #include <errno.h> |
| 7 | #include <stdio.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <fcntl.h> |
| 12 | |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame^] | 13 | #include "helpers.h" |
Stefan Hajnoczi | c31c7ec | 2019-07-24 09:24:50 +0100 | [diff] [blame] | 14 | #include "liburing.h" |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 15 | |
Jens Axboe | 60b3305 | 2019-05-01 10:07:37 -0600 | [diff] [blame] | 16 | #define BLOCK 4096 |
| 17 | |
Jens Axboe | 7b989f3 | 2019-05-01 16:12:06 -0600 | [diff] [blame] | 18 | #ifndef RWF_NOWAIT |
| 19 | #define RWF_NOWAIT 8 |
| 20 | #endif |
| 21 | |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 22 | static int get_file_fd(void) |
| 23 | { |
Jens Axboe | 60b3305 | 2019-05-01 10:07:37 -0600 | [diff] [blame] | 24 | ssize_t ret; |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 25 | char *buf; |
| 26 | int fd; |
| 27 | |
| 28 | fd = open("testfile", O_RDWR | O_CREAT, 0644); |
| 29 | if (fd < 0) { |
| 30 | perror("open file"); |
| 31 | return -1; |
| 32 | } |
| 33 | |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame^] | 34 | buf = io_uring_malloc(BLOCK); |
Jens Axboe | 60b3305 | 2019-05-01 10:07:37 -0600 | [diff] [blame] | 35 | ret = write(fd, buf, BLOCK); |
| 36 | if (ret != BLOCK) { |
| 37 | if (ret < 0) |
| 38 | perror("write"); |
| 39 | else |
| 40 | printf("Short write\n"); |
| 41 | goto err; |
| 42 | } |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 43 | fsync(fd); |
| 44 | |
| 45 | if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) { |
| 46 | perror("fadvise"); |
Jens Axboe | 60b3305 | 2019-05-01 10:07:37 -0600 | [diff] [blame] | 47 | err: |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 48 | close(fd); |
| 49 | free(buf); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | free(buf); |
| 54 | return fd; |
| 55 | } |
| 56 | |
| 57 | static void put_file_fd(int fd) |
| 58 | { |
| 59 | close(fd); |
| 60 | unlink("testfile"); |
| 61 | } |
| 62 | |
| 63 | int main(int argc, char *argv[]) |
| 64 | { |
| 65 | struct io_uring ring; |
| 66 | struct io_uring_sqe *sqe; |
Jens Axboe | ce8e2bc | 2019-04-30 14:50:36 -0600 | [diff] [blame] | 67 | struct io_uring_cqe *cqe; |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 68 | struct iovec iov; |
| 69 | int ret, fd; |
| 70 | |
Jens Axboe | a2141fc | 2020-05-19 17:36:19 -0600 | [diff] [blame] | 71 | if (argc > 1) |
| 72 | return 0; |
| 73 | |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame^] | 74 | iov.iov_base = io_uring_malloc(4096); |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 75 | iov.iov_len = 4096; |
| 76 | |
| 77 | ret = io_uring_queue_init(2, &ring, 0); |
| 78 | if (ret) { |
| 79 | printf("ring setup failed\n"); |
| 80 | return 1; |
| 81 | |
| 82 | } |
| 83 | |
| 84 | sqe = io_uring_get_sqe(&ring); |
| 85 | if (!sqe) { |
| 86 | printf("get sqe failed\n"); |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | fd = get_file_fd(); |
| 91 | if (fd < 0) |
| 92 | return 1; |
| 93 | |
| 94 | io_uring_prep_readv(sqe, fd, &iov, 1, 0); |
| 95 | sqe->rw_flags = RWF_NOWAIT; |
| 96 | |
| 97 | ret = io_uring_submit(&ring); |
Jens Axboe | ce8e2bc | 2019-04-30 14:50:36 -0600 | [diff] [blame] | 98 | if (ret != 1) { |
| 99 | printf("Got submit %d, expected 1\n", ret); |
| 100 | goto err; |
| 101 | } |
| 102 | |
| 103 | ret = io_uring_peek_cqe(&ring, &cqe); |
| 104 | if (ret) { |
| 105 | printf("Ring peek got %d\n", ret); |
| 106 | goto err; |
| 107 | } |
| 108 | |
Jens Axboe | 9e1d69e | 2020-07-30 10:44:10 -0600 | [diff] [blame] | 109 | if (cqe->res != -EAGAIN && cqe->res != 4096) { |
Jens Axboe | ce8e2bc | 2019-04-30 14:50:36 -0600 | [diff] [blame] | 110 | printf("cqe error: %d\n", cqe->res); |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 111 | goto err; |
| 112 | } |
| 113 | |
| 114 | put_file_fd(fd); |
| 115 | return 0; |
| 116 | err: |
| 117 | put_file_fd(fd); |
| 118 | return 1; |
| 119 | } |