libaio engine: queue init error handling
[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 #include "../os.h"
17
18 #ifdef FIO_HAVE_SPLICE
19
20 struct spliceio_data {
21         int pipe[2];
22 };
23
24 /*
25  * For splice reading, we unfortunately cannot (yet) vmsplice the other way.
26  * So just splice the data from the file into the pipe, and use regular
27  * read to fill the buffer. Doesn't make a lot of sense, but...
28  */
29 static int fio_splice_read(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  * For splice writing, we can vmsplice our data buffer directly into a
71  * pipe and then splice that to a file.
72  */
73 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
74 {
75         struct spliceio_data *sd = td->io_ops->data;
76         struct iovec iov[1] = {
77                 {
78                         .iov_base = io_u->xfer_buf,
79                         .iov_len = io_u->xfer_buflen,
80                 }
81         };
82         struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
83         struct fio_file *f = io_u->file;
84         off_t off = io_u->offset;
85         int ret, ret2;
86
87         while (iov[0].iov_len) {
88                 if (poll(&pfd, 1, -1) < 0)
89                         return errno;
90
91                 ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK);
92                 if (ret < 0)
93                         return errno;
94
95                 iov[0].iov_len -= ret;
96                 iov[0].iov_base += ret;
97
98                 while (ret) {
99                         ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
100                         if (ret2 < 0)
101                                 return errno;
102
103                         ret -= ret2;
104                 }
105         }
106
107         return io_u->xfer_buflen;
108 }
109
110 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
111 {
112         int ret;
113
114         if (io_u->ddir == DDIR_READ)
115                 ret = fio_splice_read(td, io_u);
116         else if (io_u->ddir == DDIR_WRITE)
117                 ret = fio_splice_write(td, io_u);
118         else
119                 ret = fsync(io_u->file->fd);
120
121         if (ret != (int) io_u->xfer_buflen) {
122                 if (ret >= 0) {
123                         io_u->resid = io_u->xfer_buflen - ret;
124                         io_u->error = 0;
125                         return FIO_Q_COMPLETED;
126                 } else
127                         io_u->error = errno;
128         }
129
130         if (io_u->error)
131                 td_verror(td, io_u->error, "xfer");
132
133         return FIO_Q_COMPLETED;
134 }
135
136 static void fio_spliceio_cleanup(struct thread_data *td)
137 {
138         struct spliceio_data *sd = td->io_ops->data;
139
140         if (sd) {
141                 close(sd->pipe[0]);
142                 close(sd->pipe[1]);
143                 free(sd);
144                 td->io_ops->data = NULL;
145         }
146 }
147
148 static int fio_spliceio_init(struct thread_data *td)
149 {
150         struct spliceio_data *sd = malloc(sizeof(*sd));
151
152         if (pipe(sd->pipe) < 0) {
153                 td_verror(td, errno, "pipe");
154                 free(sd);
155                 return 1;
156         }
157
158         td->io_ops->data = sd;
159         return 0;
160 }
161
162 static struct ioengine_ops ioengine = {
163         .name           = "splice",
164         .version        = FIO_IOOPS_VERSION,
165         .init           = fio_spliceio_init,
166         .queue          = fio_spliceio_queue,
167         .cleanup        = fio_spliceio_cleanup,
168         .open_file      = generic_open_file,
169         .close_file     = generic_close_file,
170         .flags          = FIO_SYNCIO,
171 };
172
173 #else /* FIO_HAVE_SPLICE */
174
175 /*
176  * When we have a proper configure system in place, we simply wont build
177  * and install this io engine. For now install a crippled version that
178  * just complains and fails to load.
179  */
180 static int fio_spliceio_init(struct thread_data fio_unused *td)
181 {
182         fprintf(stderr, "fio: splice not available\n");
183         return 1;
184 }
185
186 static struct ioengine_ops ioengine = {
187         .name           = "splice",
188         .version        = FIO_IOOPS_VERSION,
189         .init           = fio_spliceio_init,
190 };
191
192 #endif
193
194 static void fio_init fio_spliceio_register(void)
195 {
196         register_ioengine(&ioengine);
197 }
198
199 static void fio_exit fio_spliceio_unregister(void)
200 {
201         unregister_ioengine(&ioengine);
202 }