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