Get rid of syslet-rw compile warnings on 32-bit
[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         fio_ro_check(td, io_u);
189
190         if (io_u->ddir == DDIR_READ) {
191                 if (sd->vmsplice_to_user) {
192                         ret = fio_splice_read(td, io_u);
193                         /*
194                          * This kernel doesn't support vmsplice to user
195                          * space. Reset the vmsplice_to_user flag, so that
196                          * we retry below and don't hit this path again.
197                          */
198                         if (ret == -EBADF)
199                                 sd->vmsplice_to_user = 0;
200                 }
201                 if (!sd->vmsplice_to_user)
202                         ret = fio_splice_read_old(td, io_u);
203         } else if (io_u->ddir == DDIR_WRITE)
204                 ret = fio_splice_write(td, io_u);
205         else
206                 ret = fsync(io_u->file->fd);
207
208         if (ret != (int) io_u->xfer_buflen) {
209                 if (ret >= 0) {
210                         io_u->resid = io_u->xfer_buflen - ret;
211                         io_u->error = 0;
212                         return FIO_Q_COMPLETED;
213                 } else
214                         io_u->error = errno;
215         }
216
217         if (io_u->error)
218                 td_verror(td, io_u->error, "xfer");
219
220         return FIO_Q_COMPLETED;
221 }
222
223 static void fio_spliceio_cleanup(struct thread_data *td)
224 {
225         struct spliceio_data *sd = td->io_ops->data;
226
227         if (sd) {
228                 close(sd->pipe[0]);
229                 close(sd->pipe[1]);
230                 free(sd);
231                 td->io_ops->data = NULL;
232         }
233 }
234
235 static int fio_spliceio_init(struct thread_data *td)
236 {
237         struct spliceio_data *sd = malloc(sizeof(*sd));
238
239         if (pipe(sd->pipe) < 0) {
240                 td_verror(td, errno, "pipe");
241                 free(sd);
242                 return 1;
243         }
244
245         /*
246          * Assume this work, we'll reset this if it doesn't
247          */
248         sd->vmsplice_to_user = 1;
249
250         /*
251          * And if vmsplice_to_user works, we definitely need aligned
252          * buffers. Just set ->odirect to force that.
253          */
254         if (td_read(td))
255                 td->o.odirect = 1;
256
257         td->io_ops->data = sd;
258         return 0;
259 }
260
261 static struct ioengine_ops ioengine = {
262         .name           = "splice",
263         .version        = FIO_IOOPS_VERSION,
264         .init           = fio_spliceio_init,
265         .queue          = fio_spliceio_queue,
266         .cleanup        = fio_spliceio_cleanup,
267         .open_file      = generic_open_file,
268         .close_file     = generic_close_file,
269         .flags          = FIO_SYNCIO,
270 };
271
272 #else /* FIO_HAVE_SPLICE */
273
274 /*
275  * When we have a proper configure system in place, we simply wont build
276  * and install this io engine. For now install a crippled version that
277  * just complains and fails to load.
278  */
279 static int fio_spliceio_init(struct thread_data fio_unused *td)
280 {
281         fprintf(stderr, "fio: splice not available\n");
282         return 1;
283 }
284
285 static struct ioengine_ops ioengine = {
286         .name           = "splice",
287         .version        = FIO_IOOPS_VERSION,
288         .init           = fio_spliceio_init,
289 };
290
291 #endif
292
293 static void fio_init fio_spliceio_register(void)
294 {
295         register_ioengine(&ioengine);
296 }
297
298 static void fio_exit fio_spliceio_unregister(void)
299 {
300         unregister_ioengine(&ioengine);
301 }