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