splice: fix problem with current mainline kernels
[fio.git] / engines / splice.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * splice engine
3 *
4 * IO engine that transfers data by doing splices to/from pipes and
5 * the files.
2866c82d
JA
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
13#include <sys/poll.h>
81887d5d 14#include <sys/mman.h>
5f350952
JA
15
16#include "../fio.h"
2866c82d 17
34cfcdaf
JA
18#ifdef FIO_HAVE_SPLICE
19
2866c82d 20struct spliceio_data {
2866c82d 21 int pipe[2];
f24254e1 22 int vmsplice_to_user;
8b850243 23 int vmsplice_to_user_map;
2866c82d
JA
24};
25
2866c82d 26/*
f24254e1
JA
27 * vmsplice didn't use to support splicing to user space, this is the old
28 * variant of getting that job done. Doesn't make a lot of sense, but it
29 * uses splices to move data from the source into a pipe.
2866c82d 30 */
f24254e1 31static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
2866c82d
JA
32{
33 struct spliceio_data *sd = td->io_ops->data;
53cdc686 34 struct fio_file *f = io_u->file;
2866c82d
JA
35 int ret, ret2, buflen;
36 off_t offset;
37 void *p;
38
39 offset = io_u->offset;
cec6b55d
JA
40 buflen = io_u->xfer_buflen;
41 p = io_u->xfer_buf;
2866c82d
JA
42 while (buflen) {
43 int this_len = buflen;
44
45 if (this_len > SPLICE_DEF_SIZE)
46 this_len = SPLICE_DEF_SIZE;
47
53cdc686 48 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
2866c82d
JA
49 if (ret < 0) {
50 if (errno == ENODATA || errno == EAGAIN)
51 continue;
52
b4ba5f30 53 return -errno;
2866c82d
JA
54 }
55
56 buflen -= ret;
57
58 while (ret) {
59 ret2 = read(sd->pipe[0], p, ret);
60 if (ret2 < 0)
b4ba5f30 61 return -errno;
2866c82d
JA
62
63 ret -= ret2;
64 p += ret2;
65 }
66 }
67
cec6b55d 68 return io_u->xfer_buflen;
2866c82d
JA
69}
70
f24254e1
JA
71/*
72 * We can now vmsplice into userspace, so do the transfer by splicing into
73 * a pipe and vmsplicing that into userspace.
74 */
75static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
76{
77 struct spliceio_data *sd = td->io_ops->data;
78 struct fio_file *f = io_u->file;
79 struct iovec iov;
8b850243 80 int ret , buflen, mmap_len;
f24254e1 81 off_t offset;
81887d5d 82 void *p, *map;
f24254e1 83
8b850243
JA
84restart:
85 ret = 0;
f24254e1 86 offset = io_u->offset;
81887d5d
JA
87 mmap_len = buflen = io_u->xfer_buflen;
88
8b850243
JA
89 if (sd->vmsplice_to_user_map) {
90 map = mmap(io_u->xfer_buf, buflen, PROT_READ, MAP_PRIVATE|OS_MAP_ANON, 0, 0);
91 if (map == MAP_FAILED) {
92 td_verror(td, errno, "mmap io_u");
93 return -1;
94 }
95
96 p = map;
97 } else {
98 map = NULL;
99 p = io_u->xfer_buf;
81887d5d
JA
100 }
101
f24254e1
JA
102 while (buflen) {
103 int this_len = buflen;
81887d5d 104 int flags = 0;
f24254e1 105
81887d5d 106 if (this_len > SPLICE_DEF_SIZE) {
f24254e1 107 this_len = SPLICE_DEF_SIZE;
81887d5d
JA
108 flags = SPLICE_F_MORE;
109 }
f24254e1 110
81887d5d 111 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len,flags);
f24254e1
JA
112 if (ret < 0) {
113 if (errno == ENODATA || errno == EAGAIN)
114 continue;
115
81887d5d
JA
116 td_verror(td, errno, "splice-from-fd");
117 break;
f24254e1
JA
118 }
119
120 buflen -= ret;
121 iov.iov_base = p;
122 iov.iov_len = ret;
123 p += ret;
124
125 while (iov.iov_len) {
126 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
81887d5d 127 if (ret < 0) {
8b850243
JA
128 if (errno == EFAULT && sd->vmsplice_to_user_map) {
129 sd->vmsplice_to_user_map = 0;
130 munmap(map, mmap_len);
131 goto restart;
132 }
81887d5d
JA
133 td_verror(td, errno, "vmsplice");
134 break;
135 } else if (!ret) {
136 td_verror(td, ENODATA, "vmsplice");
137 ret = -1;
138 break;
139 }
f24254e1
JA
140
141 iov.iov_len -= ret;
142 iov.iov_base += ret;
143 }
81887d5d
JA
144 if (ret < 0)
145 break;
146 }
147
8b850243 148 if (sd->vmsplice_to_user_map && munmap(map, mmap_len) < 0) {
81887d5d
JA
149 td_verror(td, errno, "munnap io_u");
150 return -1;
f24254e1 151 }
81887d5d
JA
152 if (ret < 0)
153 return ret;
f24254e1
JA
154
155 return io_u->xfer_buflen;
156}
157
2866c82d
JA
158/*
159 * For splice writing, we can vmsplice our data buffer directly into a
160 * pipe and then splice that to a file.
161 */
162static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
163{
164 struct spliceio_data *sd = td->io_ops->data;
f24254e1
JA
165 struct iovec iov = {
166 .iov_base = io_u->xfer_buf,
167 .iov_len = io_u->xfer_buflen,
2866c82d
JA
168 };
169 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
53cdc686 170 struct fio_file *f = io_u->file;
2866c82d
JA
171 off_t off = io_u->offset;
172 int ret, ret2;
173
f24254e1 174 while (iov.iov_len) {
2866c82d
JA
175 if (poll(&pfd, 1, -1) < 0)
176 return errno;
177
f24254e1 178 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
2866c82d 179 if (ret < 0)
b4ba5f30 180 return -errno;
2866c82d 181
f24254e1
JA
182 iov.iov_len -= ret;
183 iov.iov_base += ret;
2866c82d
JA
184
185 while (ret) {
53cdc686 186 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
2866c82d 187 if (ret2 < 0)
b4ba5f30 188 return -errno;
2866c82d
JA
189
190 ret -= ret2;
191 }
192 }
193
cec6b55d 194 return io_u->xfer_buflen;
2866c82d
JA
195}
196
197static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
198{
f24254e1 199 struct spliceio_data *sd = td->io_ops->data;
cec6b55d 200 int ret;
2866c82d 201
7101d9c2
JA
202 fio_ro_check(td, io_u);
203
f24254e1 204 if (io_u->ddir == DDIR_READ) {
43688575 205 if (sd->vmsplice_to_user) {
f24254e1 206 ret = fio_splice_read(td, io_u);
43688575
JA
207 /*
208 * This kernel doesn't support vmsplice to user
209 * space. Reset the vmsplice_to_user flag, so that
210 * we retry below and don't hit this path again.
211 */
212 if (ret == -EBADF)
213 sd->vmsplice_to_user = 0;
214 }
215 if (!sd->vmsplice_to_user)
f24254e1
JA
216 ret = fio_splice_read_old(td, io_u);
217 } else if (io_u->ddir == DDIR_WRITE)
2866c82d 218 ret = fio_splice_write(td, io_u);
87dc1ab1
JA
219 else
220 ret = fsync(io_u->file->fd);
2866c82d 221
cec6b55d 222 if (ret != (int) io_u->xfer_buflen) {
22819ec2 223 if (ret >= 0) {
cec6b55d
JA
224 io_u->resid = io_u->xfer_buflen - ret;
225 io_u->error = 0;
36167d82 226 return FIO_Q_COMPLETED;
2866c82d
JA
227 } else
228 io_u->error = errno;
229 }
230
36167d82 231 if (io_u->error)
e1161c32 232 td_verror(td, io_u->error, "xfer");
2866c82d 233
36167d82 234 return FIO_Q_COMPLETED;
2866c82d
JA
235}
236
237static void fio_spliceio_cleanup(struct thread_data *td)
238{
239 struct spliceio_data *sd = td->io_ops->data;
240
241 if (sd) {
242 close(sd->pipe[0]);
243 close(sd->pipe[1]);
244 free(sd);
245 td->io_ops->data = NULL;
246 }
247}
248
249static int fio_spliceio_init(struct thread_data *td)
250{
251 struct spliceio_data *sd = malloc(sizeof(*sd));
252
2866c82d 253 if (pipe(sd->pipe) < 0) {
e1161c32 254 td_verror(td, errno, "pipe");
2866c82d
JA
255 free(sd);
256 return 1;
257 }
258
f24254e1 259 /*
43688575 260 * Assume this work, we'll reset this if it doesn't
f24254e1 261 */
43688575 262 sd->vmsplice_to_user = 1;
f24254e1 263
8b850243
JA
264 /*
265 * Works with "real" vmsplice to user, eg mapping pages directly.
266 * Reset if we fail.
267 */
268 sd->vmsplice_to_user_map = 1;
269
81887d5d
JA
270 /*
271 * And if vmsplice_to_user works, we definitely need aligned
272 * buffers. Just set ->odirect to force that.
273 */
274 if (td_read(td))
275 td->o.odirect = 1;
276
2866c82d
JA
277 td->io_ops->data = sd;
278 return 0;
279}
280
5f350952 281static struct ioengine_ops ioengine = {
2866c82d
JA
282 .name = "splice",
283 .version = FIO_IOOPS_VERSION,
284 .init = fio_spliceio_init,
285 .queue = fio_spliceio_queue,
2866c82d 286 .cleanup = fio_spliceio_cleanup,
b5af8293
JA
287 .open_file = generic_open_file,
288 .close_file = generic_close_file,
2866c82d
JA
289 .flags = FIO_SYNCIO,
290};
34cfcdaf
JA
291
292#else /* FIO_HAVE_SPLICE */
293
294/*
295 * When we have a proper configure system in place, we simply wont build
296 * and install this io engine. For now install a crippled version that
297 * just complains and fails to load.
298 */
299static int fio_spliceio_init(struct thread_data fio_unused *td)
300{
301 fprintf(stderr, "fio: splice not available\n");
302 return 1;
303}
304
5f350952 305static struct ioengine_ops ioengine = {
34cfcdaf
JA
306 .name = "splice",
307 .version = FIO_IOOPS_VERSION,
308 .init = fio_spliceio_init,
309};
310
311#endif
5f350952
JA
312
313static void fio_init fio_spliceio_register(void)
314{
315 register_ioengine(&ioengine);
316}
317
318static void fio_exit fio_spliceio_unregister(void)
319{
320 unregister_ioengine(&ioengine);
321}