Fix splice engine compile
[fio.git] / engines / splice.c
1 /*
2  * splice engine
3  *
4  * IO engine that transfers data by doing splices to/from pipes and
5  * the files.
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>
14
15 #include "../fio.h"
16
17 #ifdef FIO_HAVE_SPLICE
18
19 struct spliceio_data {
20         int pipe[2];
21         int vmsplice_to_user;
22 };
23
24 /*
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.
28  */
29 static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
30 {
31         struct spliceio_data *sd = td->io_ops->data;
32         struct fio_file *f = io_u->file;
33         int ret, ret2, buflen;
34         off_t offset;
35         void *p;
36
37         offset = io_u->offset;
38         buflen = io_u->xfer_buflen;
39         p = io_u->xfer_buf;
40         while (buflen) {
41                 int this_len = buflen;
42
43                 if (this_len > SPLICE_DEF_SIZE)
44                         this_len = SPLICE_DEF_SIZE;
45
46                 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
47                 if (ret < 0) {
48                         if (errno == ENODATA || errno == EAGAIN)
49                                 continue;
50
51                         return -errno;
52                 }
53
54                 buflen -= ret;
55
56                 while (ret) {
57                         ret2 = read(sd->pipe[0], p, ret);
58                         if (ret2 < 0)
59                                 return -errno;
60
61                         ret -= ret2;
62                         p += ret2;
63                 }
64         }
65
66         return io_u->xfer_buflen;
67 }
68
69 static void splice_unmap_io_u(struct thread_data *td, struct io_u *io_u)
70 {
71         struct spliceio_data *sd = td->io_ops->data;
72         struct iovec iov = {
73                 .iov_base = io_u->xfer_buf,
74                 .iov_len = io_u->xfer_buflen,
75         };
76
77         vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_UNMAP);
78 }
79
80 /*
81  * We can now vmsplice into userspace, so do the transfer by splicing into
82  * a pipe and vmsplicing that into userspace.
83  */
84 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
85 {
86         struct spliceio_data *sd = td->io_ops->data;
87         struct fio_file *f = io_u->file;
88         struct iovec iov;
89         int ret, buflen;
90         off_t offset;
91         void *p;
92
93         offset = io_u->offset;
94         buflen = io_u->xfer_buflen;
95         p = io_u->xfer_buf;
96         io_u->xfer_buf = NULL;
97         while (buflen) {
98                 int this_len = buflen;
99
100                 if (this_len > SPLICE_DEF_SIZE)
101                         this_len = SPLICE_DEF_SIZE;
102
103                 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
104                 if (ret < 0) {
105                         if (errno == ENODATA || errno == EAGAIN)
106                                 continue;
107
108                         return -errno;
109                 }
110
111                 buflen -= ret;
112                 iov.iov_base = p;
113                 iov.iov_len = ret;
114                 p += ret;
115
116                 while (iov.iov_len) {
117                         ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
118                         if (ret < 0)
119                                 return -errno;
120                         else if (!ret)
121                                 return -ENODATA;
122
123                         if (!io_u->xfer_buf)
124                                 io_u->xfer_buf = iov.iov_base;
125                         iov.iov_len -= ret;
126                         iov.iov_base += ret;
127                 }
128         }
129
130         io_u->unmap = splice_unmap_io_u;
131         return io_u->xfer_buflen;
132 }
133
134 /*
135  * For splice writing, we can vmsplice our data buffer directly into a
136  * pipe and then splice that to a file.
137  */
138 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
139 {
140         struct spliceio_data *sd = td->io_ops->data;
141         struct iovec iov = {
142                 .iov_base = io_u->xfer_buf,
143                 .iov_len = io_u->xfer_buflen,
144         };
145         struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
146         struct fio_file *f = io_u->file;
147         off_t off = io_u->offset;
148         int ret, ret2;
149
150         while (iov.iov_len) {
151                 if (poll(&pfd, 1, -1) < 0)
152                         return errno;
153
154                 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
155                 if (ret < 0)
156                         return -errno;
157
158                 iov.iov_len -= ret;
159                 iov.iov_base += ret;
160
161                 while (ret) {
162                         ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
163                         if (ret2 < 0)
164                                 return -errno;
165
166                         ret -= ret2;
167                 }
168         }
169
170         return io_u->xfer_buflen;
171 }
172
173 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
174 {
175         struct spliceio_data *sd = td->io_ops->data;
176         int ret;
177
178         if (io_u->ddir == DDIR_READ) {
179                 if (sd->vmsplice_to_user) {
180                         ret = fio_splice_read(td, io_u);
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)
190                         ret = fio_splice_read_old(td, io_u);
191         } else if (io_u->ddir == DDIR_WRITE)
192                 ret = fio_splice_write(td, io_u);
193         else
194                 ret = fsync(io_u->file->fd);
195
196         if (ret != (int) io_u->xfer_buflen) {
197                 if (ret >= 0) {
198                         io_u->resid = io_u->xfer_buflen - ret;
199                         io_u->error = 0;
200                         return FIO_Q_COMPLETED;
201                 } else
202                         io_u->error = errno;
203         }
204
205         if (io_u->error)
206                 td_verror(td, io_u->error, "xfer");
207
208         return FIO_Q_COMPLETED;
209 }
210
211 static 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
223 static int fio_spliceio_init(struct thread_data *td)
224 {
225         struct spliceio_data *sd = malloc(sizeof(*sd));
226
227         if (pipe(sd->pipe) < 0) {
228                 td_verror(td, errno, "pipe");
229                 free(sd);
230                 return 1;
231         }
232
233         /*
234          * Assume this work, we'll reset this if it doesn't
235          */
236         sd->vmsplice_to_user = 1;
237
238         td->io_ops->data = sd;
239         return 0;
240 }
241
242 static struct ioengine_ops ioengine = {
243         .name           = "splice",
244         .version        = FIO_IOOPS_VERSION,
245         .init           = fio_spliceio_init,
246         .queue          = fio_spliceio_queue,
247         .cleanup        = fio_spliceio_cleanup,
248         .open_file      = generic_open_file,
249         .close_file     = generic_close_file,
250         .flags          = FIO_SYNCIO,
251 };
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  */
260 static int fio_spliceio_init(struct thread_data fio_unused *td)
261 {
262         fprintf(stderr, "fio: splice not available\n");
263         return 1;
264 }
265
266 static struct ioengine_ops ioengine = {
267         .name           = "splice",
268         .version        = FIO_IOOPS_VERSION,
269         .init           = fio_spliceio_init,
270 };
271
272 #endif
273
274 static void fio_init fio_spliceio_register(void)
275 {
276         register_ioengine(&ioengine);
277 }
278
279 static void fio_exit fio_spliceio_unregister(void)
280 {
281         unregister_ioengine(&ioengine);
282 }