[PATCH] Fix stack overflow
[fio.git] / engines / splice.c
1 /*
2  * splice io engine
3  *
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <assert.h>
10 #include <sys/poll.h>
11
12 #include "../fio.h"
13 #include "../os.h"
14
15 #ifdef FIO_HAVE_SPLICE
16
17 struct spliceio_data {
18         struct io_u *last_io_u;
19         int pipe[2];
20 };
21
22 static int fio_spliceio_getevents(struct thread_data *td, int fio_unused min,
23                                   int max, struct timespec fio_unused *t)
24 {
25         assert(max <= 1);
26
27         /*
28          * we can only have one finished io_u for sync io, since the depth
29          * is always 1
30          */
31         if (list_empty(&td->io_u_busylist))
32                 return 0;
33
34         return 1;
35 }
36
37 static struct io_u *fio_spliceio_event(struct thread_data *td, int event)
38 {
39         struct spliceio_data *sd = td->io_ops->data;
40
41         assert(event == 0);
42
43         return sd->last_io_u;
44 }
45
46 /*
47  * For splice reading, we unfortunately cannot (yet) vmsplice the other way.
48  * So just splice the data from the file into the pipe, and use regular
49  * read to fill the buffer. Doesn't make a lot of sense, but...
50  */
51 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
52 {
53         struct spliceio_data *sd = td->io_ops->data;
54         struct fio_file *f = io_u->file;
55         int ret, ret2, buflen;
56         off_t offset;
57         void *p;
58
59         offset = io_u->offset;
60         buflen = io_u->xfer_buflen;
61         p = io_u->xfer_buf;
62         while (buflen) {
63                 int this_len = buflen;
64
65                 if (this_len > SPLICE_DEF_SIZE)
66                         this_len = SPLICE_DEF_SIZE;
67
68                 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
69                 if (ret < 0) {
70                         if (errno == ENODATA || errno == EAGAIN)
71                                 continue;
72
73                         return errno;
74                 }
75
76                 buflen -= ret;
77
78                 while (ret) {
79                         ret2 = read(sd->pipe[0], p, ret);
80                         if (ret2 < 0)
81                                 return errno;
82
83                         ret -= ret2;
84                         p += ret2;
85                 }
86         }
87
88         return io_u->xfer_buflen;
89 }
90
91 /*
92  * For splice writing, we can vmsplice our data buffer directly into a
93  * pipe and then splice that to a file.
94  */
95 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
96 {
97         struct spliceio_data *sd = td->io_ops->data;
98         struct iovec iov[1] = {
99                 {
100                         .iov_base = io_u->xfer_buf,
101                         .iov_len = io_u->xfer_buflen,
102                 }
103         };
104         struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
105         struct fio_file *f = io_u->file;
106         off_t off = io_u->offset;
107         int ret, ret2;
108
109         while (iov[0].iov_len) {
110                 if (poll(&pfd, 1, -1) < 0)
111                         return errno;
112
113                 ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK);
114                 if (ret < 0)
115                         return errno;
116
117                 iov[0].iov_len -= ret;
118                 iov[0].iov_base += ret;
119
120                 while (ret) {
121                         ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
122                         if (ret2 < 0)
123                                 return errno;
124
125                         ret -= ret2;
126                 }
127         }
128
129         return io_u->xfer_buflen;
130 }
131
132 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
133 {
134         struct spliceio_data *sd = td->io_ops->data;
135         int ret;
136
137         if (io_u->ddir == DDIR_READ)
138                 ret = fio_splice_read(td, io_u);
139         else if (io_u->ddir == DDIR_WRITE)
140                 ret = fio_splice_write(td, io_u);
141         else
142                 ret = fsync(io_u->file->fd);
143
144         if (ret != (int) io_u->xfer_buflen) {
145                 if (ret > 0) {
146                         io_u->resid = io_u->xfer_buflen - ret;
147                         io_u->error = 0;
148                         return ret;
149                 } else
150                         io_u->error = errno;
151         }
152
153         if (!io_u->error)
154                 sd->last_io_u = io_u;
155
156         return io_u->error;
157 }
158
159 static void fio_spliceio_cleanup(struct thread_data *td)
160 {
161         struct spliceio_data *sd = td->io_ops->data;
162
163         if (sd) {
164                 close(sd->pipe[0]);
165                 close(sd->pipe[1]);
166                 free(sd);
167                 td->io_ops->data = NULL;
168         }
169 }
170
171 static int fio_spliceio_init(struct thread_data *td)
172 {
173         struct spliceio_data *sd = malloc(sizeof(*sd));
174
175         sd->last_io_u = NULL;
176         if (pipe(sd->pipe) < 0) {
177                 td_verror(td, errno);
178                 free(sd);
179                 return 1;
180         }
181
182         td->io_ops->data = sd;
183         return 0;
184 }
185
186 static struct ioengine_ops ioengine = {
187         .name           = "splice",
188         .version        = FIO_IOOPS_VERSION,
189         .init           = fio_spliceio_init,
190         .queue          = fio_spliceio_queue,
191         .getevents      = fio_spliceio_getevents,
192         .event          = fio_spliceio_event,
193         .cleanup        = fio_spliceio_cleanup,
194         .flags          = FIO_SYNCIO,
195 };
196
197 #else /* FIO_HAVE_SPLICE */
198
199 /*
200  * When we have a proper configure system in place, we simply wont build
201  * and install this io engine. For now install a crippled version that
202  * just complains and fails to load.
203  */
204 static int fio_spliceio_init(struct thread_data fio_unused *td)
205 {
206         fprintf(stderr, "fio: splice not available\n");
207         return 1;
208 }
209
210 static struct ioengine_ops ioengine = {
211         .name           = "splice",
212         .version        = FIO_IOOPS_VERSION,
213         .init           = fio_spliceio_init,
214 };
215
216 #endif
217
218 static void fio_init fio_spliceio_register(void)
219 {
220         register_ioengine(&ioengine);
221 }
222
223 static void fio_exit fio_spliceio_unregister(void)
224 {
225         unregister_ioengine(&ioengine);
226 }