1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // iocall.cfa -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Wed Jul 1 14:51:00 2020 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #define __cforall_thread__ |
---|
17 | |
---|
18 | #include "bits/defs.hfa" |
---|
19 | #include "kernel.hfa" |
---|
20 | |
---|
21 | //============================================================================================= |
---|
22 | // I/O uring backend |
---|
23 | //============================================================================================= |
---|
24 | |
---|
25 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
26 | #include <assert.h> |
---|
27 | #include <stdint.h> |
---|
28 | #include <errno.h> |
---|
29 | #include <linux/io_uring.h> |
---|
30 | |
---|
31 | #include "kernel/fwd.hfa" |
---|
32 | #include "io/types.hfa" |
---|
33 | |
---|
34 | extern [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data ); |
---|
35 | extern void __submit( struct io_context * ctx, uint32_t idx ) __attribute__((nonnull (1))); |
---|
36 | |
---|
37 | static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd) { |
---|
38 | this.opcode = opcode; |
---|
39 | #if !defined(IOSQE_ASYNC) |
---|
40 | this.flags = 0; |
---|
41 | #else |
---|
42 | this.flags = IOSQE_ASYNC; |
---|
43 | #endif |
---|
44 | this.ioprio = 0; |
---|
45 | this.fd = fd; |
---|
46 | this.off = 0; |
---|
47 | this.addr = 0; |
---|
48 | this.len = 0; |
---|
49 | this.rw_flags = 0; |
---|
50 | this.__pad2[0] = this.__pad2[1] = this.__pad2[2] = 0; |
---|
51 | } |
---|
52 | |
---|
53 | static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd, void * addr, uint32_t len, uint64_t off ) { |
---|
54 | (this){ opcode, fd }; |
---|
55 | this.off = off; |
---|
56 | this.addr = (uint64_t)(uintptr_t)addr; |
---|
57 | this.len = len; |
---|
58 | } |
---|
59 | |
---|
60 | static inline io_context * __get_io_context( void ) { |
---|
61 | cluster * cltr = active_cluster(); |
---|
62 | /* paranoid */ verifyf( cltr, "No active cluster for io operation\n"); |
---|
63 | assertf( cltr->io.cnt > 0, "Cluster %p has no default io contexts and no context was specified\n", cltr ); |
---|
64 | /* paranoid */ verifyf( cltr->io.ctxs, "default io contexts for cluster %p are missing\n", cltr); |
---|
65 | return &cltr->io.ctxs[ __tls_rand() % cltr->io.cnt ]; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | #if defined(CFA_HAVE_IOSQE_FIXED_FILE) && defined(CFA_HAVE_IOSQE_IO_DRAIN) && defined(CFA_HAVE_IOSQE_ASYNC) |
---|
70 | #define REGULAR_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_DRAIN | IOSQE_ASYNC) |
---|
71 | #elif defined(CFA_HAVE_IOSQE_FIXED_FILE) && defined(CFA_HAVE_IOSQE_ASYNC) |
---|
72 | #define REGULAR_FLAGS (IOSQE_FIXED_FILE | IOSQE_ASYNC) |
---|
73 | #elif defined(CFA_HAVE_IOSQE_FIXED_FILE) && defined(CFA_HAVE_IOSQE_IO_DRAIN) |
---|
74 | #define REGULAR_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_DRAIN) |
---|
75 | #elif defined(CFA_HAVE_IOSQE_IO_DRAIN) && defined(CFA_HAVE_IOSQE_ASYNC) |
---|
76 | #define REGULAR_FLAGS (IOSQE_IO_DRAIN | IOSQE_ASYNC) |
---|
77 | #elif defined(CFA_HAVE_IOSQE_FIXED_FILE) |
---|
78 | #define REGULAR_FLAGS (IOSQE_FIXED_FILE) |
---|
79 | #elif defined(CFA_HAVE_IOSQE_IO_DRAIN) |
---|
80 | #define REGULAR_FLAGS (IOSQE_IO_DRAIN) |
---|
81 | #elif defined(CFA_HAVE_IOSQE_ASYNC) |
---|
82 | #define REGULAR_FLAGS (IOSQE_ASYNC) |
---|
83 | #else |
---|
84 | #define REGULAR_FLAGS (0) |
---|
85 | #endif |
---|
86 | |
---|
87 | #if defined(CFA_HAVE_IOSQE_IO_LINK) && defined(CFA_HAVE_IOSQE_IO_HARDLINK) |
---|
88 | #define LINK_FLAGS (IOSQE_IO_LINK | IOSQE_IO_HARDLINK) |
---|
89 | #elif defined(CFA_HAVE_IOSQE_IO_LINK) |
---|
90 | #define LINK_FLAGS (IOSQE_IO_LINK) |
---|
91 | #elif defined(CFA_HAVE_IOSQE_IO_HARDLINK) |
---|
92 | #define LINK_FLAGS (IOSQE_IO_HARDLINK) |
---|
93 | #else |
---|
94 | #define LINK_FLAGS (0) |
---|
95 | #endif |
---|
96 | |
---|
97 | #if defined(CFA_HAVE_SPLICE_F_FD_IN_FIXED) |
---|
98 | #define SPLICE_FLAGS (SPLICE_F_FD_IN_FIXED) |
---|
99 | #else |
---|
100 | #define SPLICE_FLAGS (0) |
---|
101 | #endif |
---|
102 | |
---|
103 | |
---|
104 | #define __submit_prelude \ |
---|
105 | if( 0 != (submit_flags & LINK_FLAGS) ) { errno = ENOTSUP; return -1; } \ |
---|
106 | (void)timeout; (void)cancellation; \ |
---|
107 | if( !context ) context = __get_io_context(); \ |
---|
108 | __io_user_data_t data = { 0, active_thread() }; \ |
---|
109 | struct __io_data & ring = *context->thrd.ring; \ |
---|
110 | struct io_uring_sqe * sqe; \ |
---|
111 | uint32_t idx; \ |
---|
112 | [sqe, idx] = __submit_alloc( ring, (uint64_t)(uintptr_t)&data ); \ |
---|
113 | sqe->flags = REGULAR_FLAGS & submit_flags; |
---|
114 | |
---|
115 | #define __submit_wait \ |
---|
116 | /*__cfaabi_bits_print_safe( STDERR_FILENO, "Preparing user data %p for %p\n", &data, data.thrd );*/ \ |
---|
117 | verify( sqe->user_data == (uint64_t)(uintptr_t)&data ); \ |
---|
118 | __submit( context, idx ); \ |
---|
119 | park( __cfaabi_dbg_ctx ); \ |
---|
120 | if( data.result < 0 ) { \ |
---|
121 | errno = -data.result; \ |
---|
122 | return -1; \ |
---|
123 | } \ |
---|
124 | return data.result; |
---|
125 | #endif |
---|
126 | |
---|
127 | //============================================================================================= |
---|
128 | // I/O Forwards |
---|
129 | //============================================================================================= |
---|
130 | #include <time.hfa> |
---|
131 | |
---|
132 | // Some forward declarations |
---|
133 | #include <errno.h> |
---|
134 | #include <unistd.h> |
---|
135 | |
---|
136 | extern "C" { |
---|
137 | #include <sys/types.h> |
---|
138 | #include <sys/socket.h> |
---|
139 | #include <sys/syscall.h> |
---|
140 | |
---|
141 | #if defined(HAVE_PREADV2) |
---|
142 | struct iovec; |
---|
143 | extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
144 | #endif |
---|
145 | #if defined(HAVE_PWRITEV2) |
---|
146 | struct iovec; |
---|
147 | extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
148 | #endif |
---|
149 | |
---|
150 | extern int fsync(int fd); |
---|
151 | extern int sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags); |
---|
152 | |
---|
153 | struct msghdr; |
---|
154 | struct sockaddr; |
---|
155 | extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); |
---|
156 | extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); |
---|
157 | extern ssize_t send(int sockfd, const void *buf, size_t len, int flags); |
---|
158 | extern ssize_t recv(int sockfd, void *buf, size_t len, int flags); |
---|
159 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); |
---|
160 | extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
---|
161 | |
---|
162 | extern int fallocate(int fd, int mode, uint64_t offset, uint64_t len); |
---|
163 | extern int posix_fadvise(int fd, uint64_t offset, uint64_t len, int advice); |
---|
164 | extern int madvise(void *addr, size_t length, int advice); |
---|
165 | |
---|
166 | extern int openat(int dirfd, const char *pathname, int flags, mode_t mode); |
---|
167 | extern int close(int fd); |
---|
168 | |
---|
169 | extern ssize_t read (int fd, void *buf, size_t count); |
---|
170 | |
---|
171 | extern ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags); |
---|
172 | extern ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags); |
---|
173 | } |
---|
174 | |
---|
175 | //============================================================================================= |
---|
176 | // I/O Interface |
---|
177 | //============================================================================================= |
---|
178 | |
---|
179 | //----------------------------------------------------------------------------- |
---|
180 | // Asynchronous operations |
---|
181 | #if defined(HAVE_PREADV2) |
---|
182 | ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
183 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_READV) |
---|
184 | return preadv2(fd, iov, iovcnt, offset, flags); |
---|
185 | #else |
---|
186 | __submit_prelude |
---|
187 | |
---|
188 | (*sqe){ IORING_OP_READV, fd, iov, iovcnt, offset }; |
---|
189 | |
---|
190 | __submit_wait |
---|
191 | #endif |
---|
192 | } |
---|
193 | #endif |
---|
194 | |
---|
195 | #if defined(HAVE_PWRITEV2) |
---|
196 | ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
197 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_WRITEV) |
---|
198 | return pwritev2(fd, iov, iovcnt, offset, flags); |
---|
199 | #else |
---|
200 | __submit_prelude |
---|
201 | |
---|
202 | (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset }; |
---|
203 | |
---|
204 | __submit_wait |
---|
205 | #endif |
---|
206 | } |
---|
207 | #endif |
---|
208 | |
---|
209 | int cfa_fsync(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
210 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FSYNC) |
---|
211 | return fsync(fd); |
---|
212 | #else |
---|
213 | __submit_prelude |
---|
214 | |
---|
215 | (*sqe){ IORING_OP_FSYNC, fd }; |
---|
216 | |
---|
217 | __submit_wait |
---|
218 | #endif |
---|
219 | } |
---|
220 | |
---|
221 | int cfa_sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
222 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SYNC_FILE_RANGE) |
---|
223 | return sync_file_range(fd, offset, nbytes, flags); |
---|
224 | #else |
---|
225 | __submit_prelude |
---|
226 | |
---|
227 | (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd }; |
---|
228 | sqe->off = offset; |
---|
229 | sqe->len = nbytes; |
---|
230 | sqe->sync_range_flags = flags; |
---|
231 | |
---|
232 | __submit_wait |
---|
233 | #endif |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
238 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SENDMSG) |
---|
239 | return sendmsg(sockfd, msg, flags); |
---|
240 | #else |
---|
241 | __submit_prelude |
---|
242 | |
---|
243 | (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 }; |
---|
244 | sqe->msg_flags = flags; |
---|
245 | |
---|
246 | __submit_wait |
---|
247 | #endif |
---|
248 | } |
---|
249 | |
---|
250 | ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
251 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_RECVMSG) |
---|
252 | return recvmsg(sockfd, msg, flags); |
---|
253 | #else |
---|
254 | __submit_prelude |
---|
255 | |
---|
256 | (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 }; |
---|
257 | sqe->msg_flags = flags; |
---|
258 | |
---|
259 | __submit_wait |
---|
260 | #endif |
---|
261 | } |
---|
262 | |
---|
263 | ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
264 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SEND) |
---|
265 | return send( sockfd, buf, len, flags ); |
---|
266 | #else |
---|
267 | __submit_prelude |
---|
268 | |
---|
269 | (*sqe){ IORING_OP_SEND, sockfd }; |
---|
270 | sqe->addr = (uint64_t)buf; |
---|
271 | sqe->len = len; |
---|
272 | sqe->msg_flags = flags; |
---|
273 | |
---|
274 | __submit_wait |
---|
275 | #endif |
---|
276 | } |
---|
277 | |
---|
278 | ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
279 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_RECV) |
---|
280 | return recv( sockfd, buf, len, flags ); |
---|
281 | #else |
---|
282 | __submit_prelude |
---|
283 | |
---|
284 | (*sqe){ IORING_OP_RECV, sockfd }; |
---|
285 | sqe->addr = (uint64_t)buf; |
---|
286 | sqe->len = len; |
---|
287 | sqe->msg_flags = flags; |
---|
288 | |
---|
289 | __submit_wait |
---|
290 | #endif |
---|
291 | } |
---|
292 | |
---|
293 | int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
294 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_ACCEPT) |
---|
295 | return accept4( sockfd, addr, addrlen, flags ); |
---|
296 | #else |
---|
297 | __submit_prelude |
---|
298 | |
---|
299 | (*sqe){ IORING_OP_ACCEPT, sockfd }; |
---|
300 | sqe->addr = (uint64_t)(uintptr_t)addr; |
---|
301 | sqe->addr2 = (uint64_t)(uintptr_t)addrlen; |
---|
302 | sqe->accept_flags = flags; |
---|
303 | |
---|
304 | __submit_wait |
---|
305 | #endif |
---|
306 | } |
---|
307 | |
---|
308 | int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
309 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_CONNECT) |
---|
310 | return connect( sockfd, addr, addrlen ); |
---|
311 | #else |
---|
312 | __submit_prelude |
---|
313 | |
---|
314 | (*sqe){ IORING_OP_CONNECT, sockfd }; |
---|
315 | sqe->addr = (uint64_t)(uintptr_t)addr; |
---|
316 | sqe->off = (uint64_t)(uintptr_t)addrlen; |
---|
317 | |
---|
318 | __submit_wait |
---|
319 | #endif |
---|
320 | } |
---|
321 | |
---|
322 | int cfa_fallocate(int fd, int mode, uint64_t offset, uint64_t len, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
323 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FALLOCATE) |
---|
324 | return fallocate( fd, mode, offset, len ); |
---|
325 | #else |
---|
326 | __submit_prelude |
---|
327 | |
---|
328 | #warning FALLOCATE documentation for linux 5.7 is incorrect, and does not handle mode |
---|
329 | |
---|
330 | (*sqe){ IORING_OP_FALLOCATE, fd }; |
---|
331 | sqe->off = offset; |
---|
332 | sqe->len = mode; |
---|
333 | sqe->addr = len; |
---|
334 | |
---|
335 | __submit_wait |
---|
336 | #endif |
---|
337 | } |
---|
338 | |
---|
339 | int cfa_fadvise(int fd, uint64_t offset, uint64_t len, int advice, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
340 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FADVISE) |
---|
341 | return posix_fadvise( fd, offset, len, advice ); |
---|
342 | #else |
---|
343 | __submit_prelude |
---|
344 | |
---|
345 | (*sqe){ IORING_OP_FADVISE, fd }; |
---|
346 | sqe->off = (uint64_t)offset; |
---|
347 | sqe->len = len; |
---|
348 | sqe->fadvise_advice = advice; |
---|
349 | |
---|
350 | __submit_wait |
---|
351 | #endif |
---|
352 | } |
---|
353 | |
---|
354 | int cfa_madvise(void *addr, size_t length, int advice, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
355 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_MADVISE) |
---|
356 | return madvise( addr, length, advice ); |
---|
357 | #else |
---|
358 | __submit_prelude |
---|
359 | |
---|
360 | (*sqe){ IORING_OP_MADVISE, 0 }; |
---|
361 | sqe->addr = (uint64_t)addr; |
---|
362 | sqe->len = length; |
---|
363 | sqe->fadvise_advice = advice; |
---|
364 | |
---|
365 | __submit_wait |
---|
366 | #endif |
---|
367 | } |
---|
368 | |
---|
369 | int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
370 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_OPENAT) |
---|
371 | return openat( dirfd, pathname, flags, mode ); |
---|
372 | #else |
---|
373 | __submit_prelude |
---|
374 | |
---|
375 | (*sqe){ IORING_OP_OPENAT, dirfd }; |
---|
376 | sqe->addr = (uint64_t)pathname; |
---|
377 | sqe->open_flags = flags; |
---|
378 | sqe->len = mode; |
---|
379 | |
---|
380 | __submit_wait |
---|
381 | #endif |
---|
382 | } |
---|
383 | |
---|
384 | int cfa_close(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
385 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_CLOSE) |
---|
386 | return close( fd ); |
---|
387 | #else |
---|
388 | __submit_prelude |
---|
389 | |
---|
390 | (*sqe){ IORING_OP_CLOSE, fd }; |
---|
391 | |
---|
392 | __submit_wait |
---|
393 | #endif |
---|
394 | } |
---|
395 | |
---|
396 | // Forward declare in case it is not supported |
---|
397 | struct statx; |
---|
398 | int cfa_statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
399 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_STATX) |
---|
400 | #if defined(__NR_statx) |
---|
401 | return syscall( __NR_statx, dirfd, pathname, flags, mask, statxbuf ); |
---|
402 | #else |
---|
403 | errno = ENOTSUP; |
---|
404 | return -1; |
---|
405 | #endif |
---|
406 | #else |
---|
407 | __submit_prelude |
---|
408 | |
---|
409 | (*sqe){ IORING_OP_STATX, dirfd, pathname, mask, (uint64_t)statxbuf }; |
---|
410 | sqe->statx_flags = flags; |
---|
411 | |
---|
412 | __submit_wait |
---|
413 | #endif |
---|
414 | } |
---|
415 | |
---|
416 | ssize_t cfa_read(int fd, void *buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
417 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_READ) |
---|
418 | return read( fd, buf, count ); |
---|
419 | #else |
---|
420 | __submit_prelude |
---|
421 | |
---|
422 | (*sqe){ IORING_OP_READ, fd, buf, count, 0 }; |
---|
423 | |
---|
424 | __submit_wait |
---|
425 | #endif |
---|
426 | } |
---|
427 | |
---|
428 | ssize_t cfa_write(int fd, void *buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
429 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_WRITE) |
---|
430 | return read( fd, buf, count ); |
---|
431 | #else |
---|
432 | __submit_prelude |
---|
433 | |
---|
434 | (*sqe){ IORING_OP_WRITE, fd, buf, count, 0 }; |
---|
435 | |
---|
436 | __submit_wait |
---|
437 | #endif |
---|
438 | } |
---|
439 | |
---|
440 | ssize_t cfa_splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
441 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SPLICE) |
---|
442 | return splice( fd_in, off_in, fd_out, off_out, len, flags ); |
---|
443 | #else |
---|
444 | __submit_prelude |
---|
445 | |
---|
446 | (*sqe){ IORING_OP_SPLICE, fd_out }; |
---|
447 | if( off_out ) { |
---|
448 | sqe->off = *off_out; |
---|
449 | } |
---|
450 | else { |
---|
451 | sqe->off = (uint64_t)-1; |
---|
452 | } |
---|
453 | sqe->len = len; |
---|
454 | sqe->splice_fd_in = fd_in; |
---|
455 | if( off_in ) { |
---|
456 | sqe->splice_off_in = *off_in; |
---|
457 | } |
---|
458 | else { |
---|
459 | sqe->splice_off_in = (uint64_t)-1; |
---|
460 | } |
---|
461 | sqe->splice_flags = flags | (SPLICE_FLAGS & submit_flags); |
---|
462 | |
---|
463 | __submit_wait |
---|
464 | #endif |
---|
465 | } |
---|
466 | |
---|
467 | ssize_t cfa_tee(int fd_in, int fd_out, size_t len, unsigned int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) { |
---|
468 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_TEE) |
---|
469 | return tee( fd_in, fd_out, len, flags ); |
---|
470 | #else |
---|
471 | __submit_prelude |
---|
472 | |
---|
473 | (*sqe){ IORING_OP_TEE, fd_out, 0p, len, 0 }; |
---|
474 | sqe->splice_fd_in = fd_in; |
---|
475 | sqe->splice_flags = flags | (SPLICE_FLAGS & submit_flags); |
---|
476 | |
---|
477 | __submit_wait |
---|
478 | #endif |
---|
479 | } |
---|
480 | |
---|
481 | //----------------------------------------------------------------------------- |
---|
482 | // Check if a function is asynchronous |
---|
483 | |
---|
484 | // Macro magic to reduce the size of the following switch case |
---|
485 | #define IS_DEFINED_APPLY(f, ...) f(__VA_ARGS__) |
---|
486 | #define IS_DEFINED_SECOND(first, second, ...) second |
---|
487 | #define IS_DEFINED_TEST(expansion) _CFA_IO_FEATURE_##expansion |
---|
488 | #define IS_DEFINED(macro) IS_DEFINED_APPLY( IS_DEFINED_SECOND,IS_DEFINED_TEST(macro) false, true) |
---|
489 | |
---|
490 | bool has_user_level_blocking( fptr_t func ) { |
---|
491 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
492 | #if defined(HAVE_PREADV2) |
---|
493 | if( /*func == (fptr_t)preadv2 || */ |
---|
494 | func == (fptr_t)cfa_preadv2 ) |
---|
495 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_READV , |
---|
496 | return IS_DEFINED(CFA_HAVE_IORING_OP_READV); |
---|
497 | #endif |
---|
498 | |
---|
499 | #if defined(HAVE_PWRITEV2) |
---|
500 | if( /*func == (fptr_t)pwritev2 || */ |
---|
501 | func == (fptr_t)cfa_pwritev2 ) |
---|
502 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_WRITEV , |
---|
503 | return IS_DEFINED(CFA_HAVE_IORING_OP_WRITEV); |
---|
504 | #endif |
---|
505 | |
---|
506 | if( /*func == (fptr_t)fsync || */ |
---|
507 | func == (fptr_t)cfa_fsync ) |
---|
508 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FSYNC , |
---|
509 | return IS_DEFINED(CFA_HAVE_IORING_OP_FSYNC); |
---|
510 | |
---|
511 | if( /*func == (fptr_t)ync_file_range || */ |
---|
512 | func == (fptr_t)cfa_sync_file_range ) |
---|
513 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SYNC_FILE_RANGE , |
---|
514 | return IS_DEFINED(CFA_HAVE_IORING_OP_SYNC_FILE_RANGE); |
---|
515 | |
---|
516 | if( /*func == (fptr_t)sendmsg || */ |
---|
517 | func == (fptr_t)cfa_sendmsg ) |
---|
518 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SENDMSG , |
---|
519 | return IS_DEFINED(CFA_HAVE_IORING_OP_SENDMSG); |
---|
520 | |
---|
521 | if( /*func == (fptr_t)recvmsg || */ |
---|
522 | func == (fptr_t)cfa_recvmsg ) |
---|
523 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_RECVMSG , |
---|
524 | return IS_DEFINED(CFA_HAVE_IORING_OP_RECVMSG); |
---|
525 | |
---|
526 | if( /*func == (fptr_t)send || */ |
---|
527 | func == (fptr_t)cfa_send ) |
---|
528 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SEND , |
---|
529 | return IS_DEFINED(CFA_HAVE_IORING_OP_SEND); |
---|
530 | |
---|
531 | if( /*func == (fptr_t)recv || */ |
---|
532 | func == (fptr_t)cfa_recv ) |
---|
533 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_RECV , |
---|
534 | return IS_DEFINED(CFA_HAVE_IORING_OP_RECV); |
---|
535 | |
---|
536 | if( /*func == (fptr_t)accept4 || */ |
---|
537 | func == (fptr_t)cfa_accept4 ) |
---|
538 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_ACCEPT , |
---|
539 | return IS_DEFINED(CFA_HAVE_IORING_OP_ACCEPT); |
---|
540 | |
---|
541 | if( /*func == (fptr_t)connect || */ |
---|
542 | func == (fptr_t)cfa_connect ) |
---|
543 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_CONNECT , |
---|
544 | return IS_DEFINED(CFA_HAVE_IORING_OP_CONNECT); |
---|
545 | |
---|
546 | if( /*func == (fptr_t)fallocate || */ |
---|
547 | func == (fptr_t)cfa_fallocate ) |
---|
548 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FALLOCATE , |
---|
549 | return IS_DEFINED(CFA_HAVE_IORING_OP_FALLOCATE); |
---|
550 | |
---|
551 | if( /*func == (fptr_t)posix_fadvise || */ |
---|
552 | func == (fptr_t)cfa_fadvise ) |
---|
553 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FADVISE , |
---|
554 | return IS_DEFINED(CFA_HAVE_IORING_OP_FADVISE); |
---|
555 | |
---|
556 | if( /*func == (fptr_t)madvise || */ |
---|
557 | func == (fptr_t)cfa_madvise ) |
---|
558 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_MADVISE , |
---|
559 | return IS_DEFINED(CFA_HAVE_IORING_OP_MADVISE); |
---|
560 | |
---|
561 | if( /*func == (fptr_t)openat || */ |
---|
562 | func == (fptr_t)cfa_openat ) |
---|
563 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_OPENAT , |
---|
564 | return IS_DEFINED(CFA_HAVE_IORING_OP_OPENAT); |
---|
565 | |
---|
566 | if( /*func == (fptr_t)close || */ |
---|
567 | func == (fptr_t)cfa_close ) |
---|
568 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_CLOSE , |
---|
569 | return IS_DEFINED(CFA_HAVE_IORING_OP_CLOSE); |
---|
570 | |
---|
571 | if( /*func == (fptr_t)read || */ |
---|
572 | func == (fptr_t)cfa_read ) |
---|
573 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_READ , |
---|
574 | return IS_DEFINED(CFA_HAVE_IORING_OP_READ); |
---|
575 | |
---|
576 | if( /*func == (fptr_t)write || */ |
---|
577 | func == (fptr_t)cfa_write ) |
---|
578 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_WRITE , |
---|
579 | return IS_DEFINED(CFA_HAVE_IORING_OP_WRITE); |
---|
580 | |
---|
581 | if( /*func == (fptr_t)splice || */ |
---|
582 | func == (fptr_t)cfa_splice ) |
---|
583 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SPLICE , |
---|
584 | return IS_DEFINED(CFA_HAVE_IORING_OP_SPLICE); |
---|
585 | |
---|
586 | if( /*func == (fptr_t)tee || */ |
---|
587 | func == (fptr_t)cfa_tee ) |
---|
588 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_TEE , |
---|
589 | return IS_DEFINED(CFA_HAVE_IORING_OP_TEE); |
---|
590 | #endif |
---|
591 | |
---|
592 | return false; |
---|
593 | } |
---|