Merge tag 'kbuild-fixes-v5.8' of git://git.kernel.org/pub/scm/linux/kernel/git/masahi...
[linux/fpc-iii.git] / tools / io_uring / io_uring-cp.c
blob81461813ec620c1ba2c21719e15d0a10b5157d7e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
7 */
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <assert.h>
14 #include <errno.h>
15 #include <inttypes.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
20 #include "liburing.h"
22 #define QD 64
23 #define BS (32*1024)
25 static int infd, outfd;
27 struct io_data {
28 int read;
29 off_t first_offset, offset;
30 size_t first_len;
31 struct iovec iov;
34 static int setup_context(unsigned entries, struct io_uring *ring)
36 int ret;
38 ret = io_uring_queue_init(entries, ring, 0);
39 if (ret < 0) {
40 fprintf(stderr, "queue_init: %s\n", strerror(-ret));
41 return -1;
44 return 0;
47 static int get_file_size(int fd, off_t *size)
49 struct stat st;
51 if (fstat(fd, &st) < 0)
52 return -1;
53 if (S_ISREG(st.st_mode)) {
54 *size = st.st_size;
55 return 0;
56 } else if (S_ISBLK(st.st_mode)) {
57 unsigned long long bytes;
59 if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
60 return -1;
62 *size = bytes;
63 return 0;
66 return -1;
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);
74 assert(sqe);
76 if (data->read)
77 io_uring_prep_readv(sqe, infd, &data->iov, 1, data->offset);
78 else
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;
87 struct io_data *data;
89 data = malloc(size + sizeof(*data));
90 if (!data)
91 return 1;
93 sqe = io_uring_get_sqe(ring);
94 if (!sqe) {
95 free(data);
96 return 1;
99 data->read = 1;
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);
108 return 0;
111 static void queue_write(struct io_uring *ring, struct io_data *data)
113 data->read = 0;
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;
128 int ret;
130 write_left = insize;
131 writes = reads = offset = 0;
133 while (insize || write_left) {
134 unsigned long had_reads;
135 int got_comp;
138 * Queue up as many reads as we can
140 had_reads = reads;
141 while (insize) {
142 off_t this_size = insize;
144 if (reads + writes >= QD)
145 break;
146 if (this_size > BS)
147 this_size = BS;
148 else if (!this_size)
149 break;
151 if (queue_read(ring, this_size, offset))
152 break;
154 insize -= this_size;
155 offset += this_size;
156 reads++;
159 if (had_reads != reads) {
160 ret = io_uring_submit(ring);
161 if (ret < 0) {
162 fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
163 break;
168 * Queue is full at this point. Find at least one completion.
170 got_comp = 0;
171 while (write_left) {
172 struct io_data *data;
174 if (!got_comp) {
175 ret = io_uring_wait_cqe(ring, &cqe);
176 got_comp = 1;
177 } else
178 ret = io_uring_peek_cqe(ring, &cqe);
179 if (ret < 0) {
180 fprintf(stderr, "io_uring_peek_cqe: %s\n",
181 strerror(-ret));
182 return 1;
184 if (!cqe)
185 break;
187 data = io_uring_cqe_get_data(cqe);
188 if (cqe->res < 0) {
189 if (cqe->res == -EAGAIN) {
190 queue_prepped(ring, data);
191 io_uring_cqe_seen(ring, cqe);
192 continue;
194 fprintf(stderr, "cqe failed: %s\n",
195 strerror(-cqe->res));
196 return 1;
197 } else if ((size_t) cqe->res != data->iov.iov_len) {
198 /* Short read/write, adjust and requeue */
199 data->iov.iov_base += cqe->res;
200 data->iov.iov_len -= cqe->res;
201 data->offset += cqe->res;
202 queue_prepped(ring, data);
203 io_uring_cqe_seen(ring, cqe);
204 continue;
208 * All done. if write, nothing else to do. if read,
209 * queue up corresponding write.
211 if (data->read) {
212 queue_write(ring, data);
213 write_left -= data->first_len;
214 reads--;
215 writes++;
216 } else {
217 free(data);
218 writes--;
220 io_uring_cqe_seen(ring, cqe);
224 return 0;
227 int main(int argc, char *argv[])
229 struct io_uring ring;
230 off_t insize;
231 int ret;
233 if (argc < 3) {
234 printf("%s: infile outfile\n", argv[0]);
235 return 1;
238 infd = open(argv[1], O_RDONLY);
239 if (infd < 0) {
240 perror("open infile");
241 return 1;
243 outfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
244 if (outfd < 0) {
245 perror("open outfile");
246 return 1;
249 if (setup_context(QD, &ring))
250 return 1;
251 if (get_file_size(infd, &insize))
252 return 1;
254 ret = copy_file(&ring, insize);
256 close(infd);
257 close(outfd);
258 io_uring_queue_exit(&ring);
259 return ret;