splice engine: add unmapping for vmsplice-to-user
[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 /*
70  * We can now vmsplice into userspace, so do the transfer by splicing into
71  * a pipe and vmsplicing that into userspace.
72  */
73 static 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;
85         io_u->xfer_buf = NULL;
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
112                         if (!io_u->xfer_buf)
113                                 io_u->xfer_buf = iov.iov_base;
114                         iov.iov_len -= ret;
115                         iov.iov_base += ret;
116                 }
117         }
118
119         io_u->unmap = splice_unmap_io_u;
120         return io_u->xfer_buflen;
121 }
122
123 /*
124  * For splice writing, we can vmsplice our data buffer directly into a
125  * pipe and then splice that to a file.
126  */
127 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
128 {
129         struct spliceio_data *sd = td->io_ops->data;
130         struct iovec iov = {
131                 .iov_base = io_u->xfer_buf,
132                 .iov_len = io_u->xfer_buflen,
133         };
134         struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
135         struct fio_file *f = io_u->file;
136         off_t off = io_u->offset;
137         int ret, ret2;
138
139         while (iov.iov_len) {
140                 if (poll(&pfd, 1, -1) < 0)
141                         return errno;
142
143                 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
144                 if (ret < 0)
145                         return -errno;
146
147                 iov.iov_len -= ret;
148                 iov.iov_base += ret;
149
150                 while (ret) {
151                         ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
152                         if (ret2 < 0)
153                                 return -errno;
154
155                         ret -= ret2;
156                 }
157         }
158
159         return io_u->xfer_buflen;
160 }
161
162 static 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
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 }