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