4 * IO engine that transfers data by doing splices to/from pipes and
18 #ifdef FIO_HAVE_SPLICE
20 struct spliceio_data {
23 int vmsplice_to_user_map;
27 * vmsplice didn't use to support splicing to user space, this is the old
28 * variant of getting that job done. Doesn't make a lot of sense, but it
29 * uses splices to move data from the source into a pipe.
31 static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
33 struct spliceio_data *sd = td->io_ops->data;
34 struct fio_file *f = io_u->file;
35 int ret, ret2, buflen;
39 offset = io_u->offset;
40 buflen = io_u->xfer_buflen;
43 int this_len = buflen;
45 if (this_len > SPLICE_DEF_SIZE)
46 this_len = SPLICE_DEF_SIZE;
48 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
50 if (errno == ENODATA || errno == EAGAIN)
59 ret2 = read(sd->pipe[0], p, ret);
68 return io_u->xfer_buflen;
72 * We can now vmsplice into userspace, so do the transfer by splicing into
73 * a pipe and vmsplicing that into userspace.
75 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
77 struct spliceio_data *sd = td->io_ops->data;
78 struct fio_file *f = io_u->file;
80 int ret , buflen, mmap_len;
86 offset = io_u->offset;
87 mmap_len = buflen = io_u->xfer_buflen;
89 if (sd->vmsplice_to_user_map) {
90 map = mmap(io_u->xfer_buf, buflen, PROT_READ, MAP_PRIVATE|OS_MAP_ANON, 0, 0);
91 if (map == MAP_FAILED) {
92 td_verror(td, errno, "mmap io_u");
103 int this_len = buflen;
106 if (this_len > SPLICE_DEF_SIZE) {
107 this_len = SPLICE_DEF_SIZE;
108 flags = SPLICE_F_MORE;
111 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len,flags);
113 if (errno == ENODATA || errno == EAGAIN)
116 td_verror(td, errno, "splice-from-fd");
125 while (iov.iov_len) {
126 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
128 if (errno == EFAULT && sd->vmsplice_to_user_map) {
129 sd->vmsplice_to_user_map = 0;
130 munmap(map, mmap_len);
133 if (errno == EBADF) {
137 td_verror(td, errno, "vmsplice");
140 td_verror(td, ENODATA, "vmsplice");
152 if (sd->vmsplice_to_user_map && munmap(map, mmap_len) < 0) {
153 td_verror(td, errno, "munnap io_u");
159 return io_u->xfer_buflen;
163 * For splice writing, we can vmsplice our data buffer directly into a
164 * pipe and then splice that to a file.
166 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
168 struct spliceio_data *sd = td->io_ops->data;
170 .iov_base = io_u->xfer_buf,
171 .iov_len = io_u->xfer_buflen,
173 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
174 struct fio_file *f = io_u->file;
175 off_t off = io_u->offset;
178 while (iov.iov_len) {
179 if (poll(&pfd, 1, -1) < 0)
182 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
190 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
198 return io_u->xfer_buflen;
201 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
203 struct spliceio_data *sd = td->io_ops->data;
206 fio_ro_check(td, io_u);
208 if (io_u->ddir == DDIR_READ) {
209 if (sd->vmsplice_to_user) {
210 ret = fio_splice_read(td, io_u);
212 * This kernel doesn't support vmsplice to user
213 * space. Reset the vmsplice_to_user flag, so that
214 * we retry below and don't hit this path again.
217 sd->vmsplice_to_user = 0;
219 if (!sd->vmsplice_to_user)
220 ret = fio_splice_read_old(td, io_u);
221 } else if (io_u->ddir == DDIR_WRITE)
222 ret = fio_splice_write(td, io_u);
224 ret = fsync(io_u->file->fd);
226 if (ret != (int) io_u->xfer_buflen) {
228 io_u->resid = io_u->xfer_buflen - ret;
230 return FIO_Q_COMPLETED;
236 td_verror(td, io_u->error, "xfer");
238 return FIO_Q_COMPLETED;
241 static void fio_spliceio_cleanup(struct thread_data *td)
243 struct spliceio_data *sd = td->io_ops->data;
252 static int fio_spliceio_init(struct thread_data *td)
254 struct spliceio_data *sd = malloc(sizeof(*sd));
256 if (pipe(sd->pipe) < 0) {
257 td_verror(td, errno, "pipe");
263 * Assume this work, we'll reset this if it doesn't
265 sd->vmsplice_to_user = 1;
268 * Works with "real" vmsplice to user, eg mapping pages directly.
271 sd->vmsplice_to_user_map = 1;
274 * And if vmsplice_to_user works, we definitely need aligned
275 * buffers. Just set ->odirect to force that.
280 td->io_ops->data = sd;
284 static struct ioengine_ops ioengine = {
286 .version = FIO_IOOPS_VERSION,
287 .init = fio_spliceio_init,
288 .queue = fio_spliceio_queue,
289 .cleanup = fio_spliceio_cleanup,
290 .open_file = generic_open_file,
291 .close_file = generic_close_file,
295 #else /* FIO_HAVE_SPLICE */
298 * When we have a proper configure system in place, we simply wont build
299 * and install this io engine. For now install a crippled version that
300 * just complains and fails to load.
302 static int fio_spliceio_init(struct thread_data fio_unused *td)
304 fprintf(stderr, "fio: splice not available\n");
308 static struct ioengine_ops ioengine = {
310 .version = FIO_IOOPS_VERSION,
311 .init = fio_spliceio_init,
316 static void fio_init fio_spliceio_register(void)
318 register_ioengine(&ioengine);
321 static void fio_exit fio_spliceio_unregister(void)
323 unregister_ioengine(&ioengine);