Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * gcc -Wall -O2 -D_GNU_SOURCE -o io_uring-cp io_uring-cp.c -luring |
| 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <errno.h> |
Jens Axboe | b7e86eb | 2019-01-17 18:14:21 -0700 | [diff] [blame] | 11 | #include <inttypes.h> |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 14 | #include <sys/ioctl.h> |
Stefan Hajnoczi | c31c7ec | 2019-07-24 09:24:50 +0100 | [diff] [blame] | 15 | #include "liburing.h" |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 16 | |
| 17 | #define QD 64 |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 18 | #define BS (32*1024) |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 19 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 20 | static int infd, outfd; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 21 | |
Jens Axboe | 6cdce17 | 2019-01-10 09:32:41 -0700 | [diff] [blame] | 22 | struct io_data { |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 23 | int read; |
| 24 | off_t first_offset, offset; |
| 25 | size_t first_len; |
| 26 | struct iovec iov; |
Jens Axboe | 6cdce17 | 2019-01-10 09:32:41 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Jens Axboe | d5b4ae1 | 2019-01-10 14:28:10 -0700 | [diff] [blame] | 29 | static int setup_context(unsigned entries, struct io_uring *ring) |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 30 | { |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 31 | int ret; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 32 | |
Jens Axboe | a992ffa | 2019-01-10 15:11:07 -0700 | [diff] [blame] | 33 | ret = io_uring_queue_init(entries, ring, 0); |
Jens Axboe | 66a7d05 | 2019-01-08 15:59:09 -0700 | [diff] [blame] | 34 | if (ret < 0) { |
| 35 | fprintf(stderr, "queue_init: %s\n", strerror(-ret)); |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 36 | return -1; |
| 37 | } |
| 38 | |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static int get_file_size(int fd, off_t *size) |
| 43 | { |
| 44 | struct stat st; |
| 45 | |
| 46 | if (fstat(fd, &st) < 0) |
| 47 | return -1; |
| 48 | if (S_ISREG(st.st_mode)) { |
| 49 | *size = st.st_size; |
| 50 | return 0; |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 51 | } else if (S_ISBLK(st.st_mode)) { |
| 52 | unsigned long long bytes; |
| 53 | |
| 54 | if (ioctl(fd, BLKGETSIZE64, &bytes) != 0) |
| 55 | return -1; |
| 56 | |
| 57 | *size = bytes; |
| 58 | return 0; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return -1; |
| 62 | } |
| 63 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 64 | static void queue_prepped(struct io_uring *ring, struct io_data *data) |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 65 | { |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 66 | struct io_uring_sqe *sqe; |
| 67 | |
| 68 | sqe = io_uring_get_sqe(ring); |
| 69 | assert(sqe); |
| 70 | |
| 71 | if (data->read) |
| 72 | io_uring_prep_readv(sqe, infd, &data->iov, 1, data->offset); |
| 73 | else |
| 74 | io_uring_prep_writev(sqe, outfd, &data->iov, 1, data->offset); |
| 75 | |
| 76 | io_uring_sqe_set_data(sqe, data); |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 79 | static int queue_read(struct io_uring *ring, off_t size, off_t offset) |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 80 | { |
Jens Axboe | 7bf7e8e | 2019-01-09 15:26:20 -0700 | [diff] [blame] | 81 | struct io_uring_sqe *sqe; |
Jens Axboe | 6cdce17 | 2019-01-10 09:32:41 -0700 | [diff] [blame] | 82 | struct io_data *data; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 83 | |
zhangliguang | 4d32556 | 2019-05-22 13:32:32 +0800 | [diff] [blame] | 84 | data = malloc(size + sizeof(*data)); |
| 85 | if (!data) |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 86 | return 1; |
| 87 | |
zhangliguang | 4d32556 | 2019-05-22 13:32:32 +0800 | [diff] [blame] | 88 | sqe = io_uring_get_sqe(ring); |
| 89 | if (!sqe) { |
| 90 | free(data); |
| 91 | return 1; |
| 92 | } |
| 93 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 94 | data->read = 1; |
| 95 | data->offset = data->first_offset = offset; |
Jens Axboe | 6cdce17 | 2019-01-10 09:32:41 -0700 | [diff] [blame] | 96 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 97 | data->iov.iov_base = data + 1; |
| 98 | data->iov.iov_len = size; |
| 99 | data->first_len = size; |
| 100 | |
| 101 | io_uring_prep_readv(sqe, infd, &data->iov, 1, offset); |
Jens Axboe | 5789a63 | 2019-01-17 18:12:22 -0700 | [diff] [blame] | 102 | io_uring_sqe_set_data(sqe, data); |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 106 | static void queue_write(struct io_uring *ring, struct io_data *data) |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 107 | { |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 108 | data->read = 0; |
| 109 | data->offset = data->first_offset; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 110 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 111 | data->iov.iov_base = data + 1; |
| 112 | data->iov.iov_len = data->first_len; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 113 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 114 | queue_prepped(ring, data); |
| 115 | io_uring_submit(ring); |
| 116 | } |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 117 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 118 | static int copy_file(struct io_uring *ring, off_t insize) |
| 119 | { |
| 120 | unsigned long reads, writes; |
| 121 | struct io_uring_cqe *cqe; |
| 122 | off_t write_left, offset; |
| 123 | int ret; |
| 124 | |
| 125 | write_left = insize; |
| 126 | writes = reads = offset = 0; |
| 127 | |
| 128 | while (insize || write_left) { |
| 129 | int had_reads, got_comp; |
| 130 | |
| 131 | /* |
| 132 | * Queue up as many reads as we can |
| 133 | */ |
| 134 | had_reads = reads; |
| 135 | while (insize) { |
| 136 | off_t this_size = insize; |
| 137 | |
| 138 | if (reads + writes >= QD) |
| 139 | break; |
| 140 | if (this_size > BS) |
| 141 | this_size = BS; |
| 142 | else if (!this_size) |
| 143 | break; |
| 144 | |
| 145 | if (queue_read(ring, this_size, offset)) |
| 146 | break; |
| 147 | |
| 148 | insize -= this_size; |
| 149 | offset += this_size; |
| 150 | reads++; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 151 | } |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 152 | |
| 153 | if (had_reads != reads) { |
| 154 | ret = io_uring_submit(ring); |
| 155 | if (ret < 0) { |
| 156 | fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret)); |
| 157 | break; |
| 158 | } |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 159 | } |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 160 | |
| 161 | /* |
Jens Axboe | a231fdc | 2019-03-06 08:31:26 -0700 | [diff] [blame] | 162 | * Queue is full at this point. Find at least one completion. |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 163 | */ |
| 164 | got_comp = 0; |
| 165 | while (write_left) { |
| 166 | struct io_data *data; |
| 167 | |
| 168 | if (!got_comp) { |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 169 | ret = io_uring_wait_cqe(ring, &cqe); |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 170 | got_comp = 1; |
Jens Axboe | b76db3f | 2019-08-30 11:09:46 -0600 | [diff] [blame] | 171 | } else { |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 172 | ret = io_uring_peek_cqe(ring, &cqe); |
Jens Axboe | b76db3f | 2019-08-30 11:09:46 -0600 | [diff] [blame] | 173 | if (ret == -EAGAIN) { |
| 174 | cqe = NULL; |
| 175 | ret = 0; |
| 176 | } |
| 177 | } |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 178 | if (ret < 0) { |
Jens Axboe | 39e0ebd | 2019-04-18 08:32:06 -0600 | [diff] [blame] | 179 | fprintf(stderr, "io_uring_peek_cqe: %s\n", |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 180 | strerror(-ret)); |
| 181 | return 1; |
| 182 | } |
| 183 | if (!cqe) |
| 184 | break; |
| 185 | |
Jens Axboe | db11f11 | 2019-04-07 18:36:43 -0600 | [diff] [blame] | 186 | data = io_uring_cqe_get_data(cqe); |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 187 | if (cqe->res < 0) { |
| 188 | if (cqe->res == -EAGAIN) { |
| 189 | queue_prepped(ring, data); |
Jens Axboe | 76b61eb | 2019-04-17 09:25:32 -0600 | [diff] [blame] | 190 | io_uring_cqe_seen(ring, cqe); |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 191 | continue; |
| 192 | } |
| 193 | fprintf(stderr, "cqe failed: %s\n", |
| 194 | strerror(-cqe->res)); |
| 195 | return 1; |
| 196 | } else if (cqe->res != data->iov.iov_len) { |
Jens Axboe | a231fdc | 2019-03-06 08:31:26 -0700 | [diff] [blame] | 197 | /* Short read/write, adjust and requeue */ |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 198 | data->iov.iov_base += cqe->res; |
| 199 | data->iov.iov_len -= cqe->res; |
| 200 | data->offset += cqe->res; |
| 201 | queue_prepped(ring, data); |
Jens Axboe | 76b61eb | 2019-04-17 09:25:32 -0600 | [diff] [blame] | 202 | io_uring_cqe_seen(ring, cqe); |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 203 | continue; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * All done. if write, nothing else to do. if read, |
| 208 | * queue up corresponding write. |
| 209 | */ |
| 210 | if (data->read) { |
| 211 | queue_write(ring, data); |
| 212 | write_left -= data->first_len; |
| 213 | reads--; |
| 214 | writes++; |
| 215 | } else { |
| 216 | free(data); |
| 217 | writes--; |
| 218 | } |
Jens Axboe | 76b61eb | 2019-04-17 09:25:32 -0600 | [diff] [blame] | 219 | io_uring_cqe_seen(ring, cqe); |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 220 | } |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 226 | int main(int argc, char *argv[]) |
| 227 | { |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 228 | struct io_uring ring; |
| 229 | off_t insize; |
| 230 | int ret; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 231 | |
| 232 | if (argc < 3) { |
| 233 | printf("%s: infile outfile\n", argv[0]); |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | infd = open(argv[1], O_RDONLY); |
| 238 | if (infd < 0) { |
| 239 | perror("open infile"); |
| 240 | return 1; |
| 241 | } |
| 242 | outfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 243 | if (outfd < 0) { |
| 244 | perror("open outfile"); |
| 245 | return 1; |
| 246 | } |
| 247 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 248 | if (setup_context(QD, &ring)) |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 249 | return 1; |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 250 | if (get_file_size(infd, &insize)) |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 251 | return 1; |
| 252 | |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 253 | ret = copy_file(&ring, insize); |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 254 | |
| 255 | close(infd); |
| 256 | close(outfd); |
Jens Axboe | f7dac56 | 2019-03-05 20:13:57 -0700 | [diff] [blame] | 257 | io_uring_queue_exit(&ring); |
| 258 | return ret; |
Jens Axboe | 357ca59 | 2019-01-08 15:41:14 -0700 | [diff] [blame] | 259 | } |