1 // SPDX-License-Identifier: GPL-2.0
3 * Simple test program that demonstrates a file copy through io_uring. This
4 * uses the API exposed by liburing.
6 * Copyright (C) 2018-2019 Jens Axboe
16 #include <sys/types.h>
18 #include <sys/ioctl.h>
25 static int infd, outfd;
29 off_t first_offset, offset;
34 static int setup_context(unsigned entries, struct io_uring *ring)
38 ret = io_uring_queue_init(entries, ring, 0);
40 fprintf(stderr, "queue_init: %s\n", strerror(-ret));
47 static int get_file_size(int fd, off_t *size)
51 if (fstat(fd, &st) < 0)
53 if (S_ISREG(st.st_mode)) {
56 } else if (S_ISBLK(st.st_mode)) {
57 unsigned long long bytes;
59 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
69 static void queue_prepped(struct io_uring *ring, struct io_data *data)
71 struct io_uring_sqe *sqe;
73 sqe = io_uring_get_sqe(ring);
77 io_uring_prep_readv(sqe, infd, &data->iov, 1, data->offset);
79 io_uring_prep_writev(sqe, outfd, &data->iov, 1, data->offset);
81 io_uring_sqe_set_data(sqe, data);
84 static int queue_read(struct io_uring *ring, off_t size, off_t offset)
86 struct io_uring_sqe *sqe;
89 data = malloc(size + sizeof(*data));
93 sqe = io_uring_get_sqe(ring);
100 data->offset = data->first_offset = offset;
102 data->iov.iov_base = data + 1;
103 data->iov.iov_len = size;
104 data->first_len = size;
106 io_uring_prep_readv(sqe, infd, &data->iov, 1, offset);
107 io_uring_sqe_set_data(sqe, data);
111 static void queue_write(struct io_uring *ring, struct io_data *data)
114 data->offset = data->first_offset;
116 data->iov.iov_base = data + 1;
117 data->iov.iov_len = data->first_len;
119 queue_prepped(ring, data);
120 io_uring_submit(ring);
123 static int copy_file(struct io_uring *ring, off_t insize)
125 unsigned long reads, writes;
126 struct io_uring_cqe *cqe;
127 off_t write_left, offset;
131 writes = reads = offset = 0;
133 while (insize || write_left) {
134 int had_reads, got_comp;
137 * Queue up as many reads as we can
141 off_t this_size = insize;
143 if (reads + writes >= QD)
150 if (queue_read(ring, this_size, offset))
158 if (had_reads != reads) {
159 ret = io_uring_submit(ring);
161 fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
167 * Queue is full at this point. Find at least one completion.
171 struct io_data *data;
174 ret = io_uring_wait_cqe(ring, &cqe);
177 ret = io_uring_peek_cqe(ring, &cqe);
178 if (ret == -EAGAIN) {
184 fprintf(stderr, "io_uring_peek_cqe: %s\n",
191 data = io_uring_cqe_get_data(cqe);
193 if (cqe->res == -EAGAIN) {
194 queue_prepped(ring, data);
195 io_uring_cqe_seen(ring, cqe);
198 fprintf(stderr, "cqe failed: %s\n",
199 strerror(-cqe->res));
201 } else if (cqe->res != data->iov.iov_len) {
202 /* Short read/write, adjust and requeue */
203 data->iov.iov_base += cqe->res;
204 data->iov.iov_len -= cqe->res;
205 data->offset += cqe->res;
206 queue_prepped(ring, data);
207 io_uring_cqe_seen(ring, cqe);
212 * All done. if write, nothing else to do. if read,
213 * queue up corresponding write.
216 queue_write(ring, data);
217 write_left -= data->first_len;
224 io_uring_cqe_seen(ring, cqe);
228 /* wait out pending writes */
230 struct io_data *data;
232 ret = io_uring_wait_cqe(ring, &cqe);
234 fprintf(stderr, "wait_cqe=%d\n", ret);
238 fprintf(stderr, "write res=%d\n", cqe->res);
241 data = io_uring_cqe_get_data(cqe);
244 io_uring_cqe_seen(ring, cqe);
250 int main(int argc, char *argv[])
252 struct io_uring ring;
257 printf("%s: infile outfile\n", argv[0]);
261 infd = open(argv[1], O_RDONLY);
263 perror("open infile");
266 outfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
268 perror("open outfile");
272 if (setup_context(QD, &ring))
274 if (get_file_size(infd, &insize))
277 ret = copy_file(&ring, insize);
281 io_uring_queue_exit(&ring);