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