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 | #define __submit_prelude \
|
---|
104 | if( 0 != (submit_flags & LINK_FLAGS) ) { errno = ENOTSUP; return -1; } \
|
---|
105 | (void)timeout; (void)cancellation; \
|
---|
106 | if( !context ) context = __get_io_context(); \
|
---|
107 | __io_user_data_t data = { 0, active_thread() }; \
|
---|
108 | struct __io_data & ring = *context->thrd.ring; \
|
---|
109 | struct io_uring_sqe * sqe; \
|
---|
110 | uint32_t idx; \
|
---|
111 | uint8_t sflags = REGULAR_FLAGS & submit_flags; \
|
---|
112 | [sqe, idx] = __submit_alloc( ring, (uint64_t)(uintptr_t)&data ); \
|
---|
113 | sqe->flags = sflags;
|
---|
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->opcode = IORING_OP_READV;
|
---|
189 | sqe->ioprio = 0;
|
---|
190 | sqe->fd = fd;
|
---|
191 | sqe->off = offset;
|
---|
192 | sqe->addr = (uint64_t)(uintptr_t)iov;
|
---|
193 | sqe->len = iovcnt;
|
---|
194 | sqe->rw_flags = 0;
|
---|
195 | sqe->__pad2[0] = sqe->__pad2[1] = sqe->__pad2[2] = 0;
|
---|
196 |
|
---|
197 | __submit_wait
|
---|
198 | #endif
|
---|
199 | }
|
---|
200 | #endif
|
---|
201 |
|
---|
202 | #if defined(HAVE_PWRITEV2)
|
---|
203 | 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) {
|
---|
204 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_WRITEV)
|
---|
205 | return pwritev2(fd, iov, iovcnt, offset, flags);
|
---|
206 | #else
|
---|
207 | __submit_prelude
|
---|
208 |
|
---|
209 | (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset };
|
---|
210 |
|
---|
211 | __submit_wait
|
---|
212 | #endif
|
---|
213 | }
|
---|
214 | #endif
|
---|
215 |
|
---|
216 | int cfa_fsync(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
217 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FSYNC)
|
---|
218 | return fsync(fd);
|
---|
219 | #else
|
---|
220 | __submit_prelude
|
---|
221 |
|
---|
222 | (*sqe){ IORING_OP_FSYNC, fd };
|
---|
223 |
|
---|
224 | __submit_wait
|
---|
225 | #endif
|
---|
226 | }
|
---|
227 |
|
---|
228 | 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) {
|
---|
229 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SYNC_FILE_RANGE)
|
---|
230 | return sync_file_range(fd, offset, nbytes, flags);
|
---|
231 | #else
|
---|
232 | __submit_prelude
|
---|
233 |
|
---|
234 | (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd };
|
---|
235 | sqe->off = offset;
|
---|
236 | sqe->len = nbytes;
|
---|
237 | sqe->sync_range_flags = flags;
|
---|
238 |
|
---|
239 | __submit_wait
|
---|
240 | #endif
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
245 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SENDMSG)
|
---|
246 | return sendmsg(sockfd, msg, flags);
|
---|
247 | #else
|
---|
248 | __submit_prelude
|
---|
249 |
|
---|
250 | (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 };
|
---|
251 | sqe->msg_flags = flags;
|
---|
252 |
|
---|
253 | __submit_wait
|
---|
254 | #endif
|
---|
255 | }
|
---|
256 |
|
---|
257 | ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
258 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_RECVMSG)
|
---|
259 | return recvmsg(sockfd, msg, flags);
|
---|
260 | #else
|
---|
261 | __submit_prelude
|
---|
262 |
|
---|
263 | (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 };
|
---|
264 | sqe->msg_flags = flags;
|
---|
265 |
|
---|
266 | __submit_wait
|
---|
267 | #endif
|
---|
268 | }
|
---|
269 |
|
---|
270 | 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) {
|
---|
271 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SEND)
|
---|
272 | return send( sockfd, buf, len, flags );
|
---|
273 | #else
|
---|
274 | __submit_prelude
|
---|
275 |
|
---|
276 | (*sqe){ IORING_OP_SEND, sockfd };
|
---|
277 | sqe->addr = (uint64_t)buf;
|
---|
278 | sqe->len = len;
|
---|
279 | sqe->msg_flags = flags;
|
---|
280 |
|
---|
281 | __submit_wait
|
---|
282 | #endif
|
---|
283 | }
|
---|
284 |
|
---|
285 | ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
286 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_RECV)
|
---|
287 | return recv( sockfd, buf, len, flags );
|
---|
288 | #else
|
---|
289 | __submit_prelude
|
---|
290 |
|
---|
291 | (*sqe){ IORING_OP_RECV, sockfd };
|
---|
292 | sqe->addr = (uint64_t)buf;
|
---|
293 | sqe->len = len;
|
---|
294 | sqe->msg_flags = flags;
|
---|
295 |
|
---|
296 | __submit_wait
|
---|
297 | #endif
|
---|
298 | }
|
---|
299 |
|
---|
300 | int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
301 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_ACCEPT)
|
---|
302 | return accept4( sockfd, addr, addrlen, flags );
|
---|
303 | #else
|
---|
304 | __submit_prelude
|
---|
305 |
|
---|
306 | (*sqe){ IORING_OP_ACCEPT, sockfd };
|
---|
307 | sqe->addr = (uint64_t)(uintptr_t)addr;
|
---|
308 | sqe->addr2 = (uint64_t)(uintptr_t)addrlen;
|
---|
309 | sqe->accept_flags = flags;
|
---|
310 |
|
---|
311 | __submit_wait
|
---|
312 | #endif
|
---|
313 | }
|
---|
314 |
|
---|
315 | int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
316 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_CONNECT)
|
---|
317 | return connect( sockfd, addr, addrlen );
|
---|
318 | #else
|
---|
319 | __submit_prelude
|
---|
320 |
|
---|
321 | (*sqe){ IORING_OP_CONNECT, sockfd };
|
---|
322 | sqe->addr = (uint64_t)(uintptr_t)addr;
|
---|
323 | sqe->off = (uint64_t)(uintptr_t)addrlen;
|
---|
324 |
|
---|
325 | __submit_wait
|
---|
326 | #endif
|
---|
327 | }
|
---|
328 |
|
---|
329 | int cfa_fallocate(int fd, int mode, uint64_t offset, uint64_t len, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
330 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FALLOCATE)
|
---|
331 | return fallocate( fd, mode, offset, len );
|
---|
332 | #else
|
---|
333 | __submit_prelude
|
---|
334 |
|
---|
335 | #warning FALLOCATE documentation for linux 5.7 is incorrect, and does not handle mode
|
---|
336 |
|
---|
337 | (*sqe){ IORING_OP_FALLOCATE, fd };
|
---|
338 | sqe->off = offset;
|
---|
339 | sqe->len = mode;
|
---|
340 | sqe->addr = len;
|
---|
341 |
|
---|
342 | __submit_wait
|
---|
343 | #endif
|
---|
344 | }
|
---|
345 |
|
---|
346 | int cfa_fadvise(int fd, uint64_t offset, uint64_t len, int advice, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
347 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FADVISE)
|
---|
348 | return posix_fadvise( fd, offset, len, advice );
|
---|
349 | #else
|
---|
350 | __submit_prelude
|
---|
351 |
|
---|
352 | (*sqe){ IORING_OP_FADVISE, fd };
|
---|
353 | sqe->off = (uint64_t)offset;
|
---|
354 | sqe->len = len;
|
---|
355 | sqe->fadvise_advice = advice;
|
---|
356 |
|
---|
357 | __submit_wait
|
---|
358 | #endif
|
---|
359 | }
|
---|
360 |
|
---|
361 | int cfa_madvise(void *addr, size_t length, int advice, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
362 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_MADVISE)
|
---|
363 | return madvise( addr, length, advice );
|
---|
364 | #else
|
---|
365 | __submit_prelude
|
---|
366 |
|
---|
367 | (*sqe){ IORING_OP_MADVISE, 0 };
|
---|
368 | sqe->addr = (uint64_t)addr;
|
---|
369 | sqe->len = length;
|
---|
370 | sqe->fadvise_advice = advice;
|
---|
371 |
|
---|
372 | __submit_wait
|
---|
373 | #endif
|
---|
374 | }
|
---|
375 |
|
---|
376 | int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
377 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_OPENAT)
|
---|
378 | return openat( dirfd, pathname, flags, mode );
|
---|
379 | #else
|
---|
380 | __submit_prelude
|
---|
381 |
|
---|
382 | (*sqe){ IORING_OP_OPENAT, dirfd };
|
---|
383 | sqe->addr = (uint64_t)pathname;
|
---|
384 | sqe->open_flags = flags;
|
---|
385 | sqe->len = mode;
|
---|
386 |
|
---|
387 | __submit_wait
|
---|
388 | #endif
|
---|
389 | }
|
---|
390 |
|
---|
391 | int cfa_close(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
392 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_CLOSE)
|
---|
393 | return close( fd );
|
---|
394 | #else
|
---|
395 | __submit_prelude
|
---|
396 |
|
---|
397 | (*sqe){ IORING_OP_CLOSE, fd };
|
---|
398 |
|
---|
399 | __submit_wait
|
---|
400 | #endif
|
---|
401 | }
|
---|
402 |
|
---|
403 | // Forward declare in case it is not supported
|
---|
404 | struct statx;
|
---|
405 | 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) {
|
---|
406 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_STATX)
|
---|
407 | #if defined(__NR_statx)
|
---|
408 | return syscall( __NR_statx, dirfd, pathname, flags, mask, statxbuf );
|
---|
409 | #else
|
---|
410 | errno = ENOTSUP;
|
---|
411 | return -1;
|
---|
412 | #endif
|
---|
413 | #else
|
---|
414 | __submit_prelude
|
---|
415 |
|
---|
416 | (*sqe){ IORING_OP_STATX, dirfd, pathname, mask, (uint64_t)statxbuf };
|
---|
417 | sqe->statx_flags = flags;
|
---|
418 |
|
---|
419 | __submit_wait
|
---|
420 | #endif
|
---|
421 | }
|
---|
422 |
|
---|
423 | ssize_t cfa_read(int fd, void *buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
424 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_READ)
|
---|
425 | return read( fd, buf, count );
|
---|
426 | #else
|
---|
427 | __submit_prelude
|
---|
428 |
|
---|
429 | (*sqe){ IORING_OP_READ, fd, buf, count, 0 };
|
---|
430 |
|
---|
431 | __submit_wait
|
---|
432 | #endif
|
---|
433 | }
|
---|
434 |
|
---|
435 | ssize_t cfa_write(int fd, void *buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
436 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_WRITE)
|
---|
437 | return read( fd, buf, count );
|
---|
438 | #else
|
---|
439 | __submit_prelude
|
---|
440 |
|
---|
441 | (*sqe){ IORING_OP_WRITE, fd, buf, count, 0 };
|
---|
442 |
|
---|
443 | __submit_wait
|
---|
444 | #endif
|
---|
445 | }
|
---|
446 |
|
---|
447 | 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) {
|
---|
448 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SPLICE)
|
---|
449 | return splice( fd_in, off_in, fd_out, off_out, len, flags );
|
---|
450 | #else
|
---|
451 | __submit_prelude
|
---|
452 |
|
---|
453 | (*sqe){ IORING_OP_SPLICE, fd_out };
|
---|
454 | if( off_out ) {
|
---|
455 | sqe->off = *off_out;
|
---|
456 | }
|
---|
457 | else {
|
---|
458 | sqe->off = (uint64_t)-1;
|
---|
459 | }
|
---|
460 | sqe->len = len;
|
---|
461 | sqe->splice_fd_in = fd_in;
|
---|
462 | if( off_in ) {
|
---|
463 | sqe->splice_off_in = *off_in;
|
---|
464 | }
|
---|
465 | else {
|
---|
466 | sqe->splice_off_in = (uint64_t)-1;
|
---|
467 | }
|
---|
468 | sqe->splice_flags = flags | (SPLICE_FLAGS & submit_flags);
|
---|
469 |
|
---|
470 | __submit_wait
|
---|
471 | #endif
|
---|
472 | }
|
---|
473 |
|
---|
474 | 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) {
|
---|
475 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_TEE)
|
---|
476 | return tee( fd_in, fd_out, len, flags );
|
---|
477 | #else
|
---|
478 | __submit_prelude
|
---|
479 |
|
---|
480 | (*sqe){ IORING_OP_TEE, fd_out, 0p, len, 0 };
|
---|
481 | sqe->splice_fd_in = fd_in;
|
---|
482 | sqe->splice_flags = flags | (SPLICE_FLAGS & submit_flags);
|
---|
483 |
|
---|
484 | __submit_wait
|
---|
485 | #endif
|
---|
486 | }
|
---|
487 |
|
---|
488 | //-----------------------------------------------------------------------------
|
---|
489 | // Check if a function is asynchronous
|
---|
490 |
|
---|
491 | // Macro magic to reduce the size of the following switch case
|
---|
492 | #define IS_DEFINED_APPLY(f, ...) f(__VA_ARGS__)
|
---|
493 | #define IS_DEFINED_SECOND(first, second, ...) second
|
---|
494 | #define IS_DEFINED_TEST(expansion) _CFA_IO_FEATURE_##expansion
|
---|
495 | #define IS_DEFINED(macro) IS_DEFINED_APPLY( IS_DEFINED_SECOND,IS_DEFINED_TEST(macro) false, true)
|
---|
496 |
|
---|
497 | bool has_user_level_blocking( fptr_t func ) {
|
---|
498 | #if defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
499 | #if defined(HAVE_PREADV2)
|
---|
500 | if( /*func == (fptr_t)preadv2 || */
|
---|
501 | func == (fptr_t)cfa_preadv2 )
|
---|
502 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_READV ,
|
---|
503 | return IS_DEFINED(CFA_HAVE_IORING_OP_READV);
|
---|
504 | #endif
|
---|
505 |
|
---|
506 | #if defined(HAVE_PWRITEV2)
|
---|
507 | if( /*func == (fptr_t)pwritev2 || */
|
---|
508 | func == (fptr_t)cfa_pwritev2 )
|
---|
509 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_WRITEV ,
|
---|
510 | return IS_DEFINED(CFA_HAVE_IORING_OP_WRITEV);
|
---|
511 | #endif
|
---|
512 |
|
---|
513 | if( /*func == (fptr_t)fsync || */
|
---|
514 | func == (fptr_t)cfa_fsync )
|
---|
515 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FSYNC ,
|
---|
516 | return IS_DEFINED(CFA_HAVE_IORING_OP_FSYNC);
|
---|
517 |
|
---|
518 | if( /*func == (fptr_t)ync_file_range || */
|
---|
519 | func == (fptr_t)cfa_sync_file_range )
|
---|
520 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SYNC_FILE_RANGE ,
|
---|
521 | return IS_DEFINED(CFA_HAVE_IORING_OP_SYNC_FILE_RANGE);
|
---|
522 |
|
---|
523 | if( /*func == (fptr_t)sendmsg || */
|
---|
524 | func == (fptr_t)cfa_sendmsg )
|
---|
525 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SENDMSG ,
|
---|
526 | return IS_DEFINED(CFA_HAVE_IORING_OP_SENDMSG);
|
---|
527 |
|
---|
528 | if( /*func == (fptr_t)recvmsg || */
|
---|
529 | func == (fptr_t)cfa_recvmsg )
|
---|
530 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_RECVMSG ,
|
---|
531 | return IS_DEFINED(CFA_HAVE_IORING_OP_RECVMSG);
|
---|
532 |
|
---|
533 | if( /*func == (fptr_t)send || */
|
---|
534 | func == (fptr_t)cfa_send )
|
---|
535 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SEND ,
|
---|
536 | return IS_DEFINED(CFA_HAVE_IORING_OP_SEND);
|
---|
537 |
|
---|
538 | if( /*func == (fptr_t)recv || */
|
---|
539 | func == (fptr_t)cfa_recv )
|
---|
540 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_RECV ,
|
---|
541 | return IS_DEFINED(CFA_HAVE_IORING_OP_RECV);
|
---|
542 |
|
---|
543 | if( /*func == (fptr_t)accept4 || */
|
---|
544 | func == (fptr_t)cfa_accept4 )
|
---|
545 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_ACCEPT ,
|
---|
546 | return IS_DEFINED(CFA_HAVE_IORING_OP_ACCEPT);
|
---|
547 |
|
---|
548 | if( /*func == (fptr_t)connect || */
|
---|
549 | func == (fptr_t)cfa_connect )
|
---|
550 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_CONNECT ,
|
---|
551 | return IS_DEFINED(CFA_HAVE_IORING_OP_CONNECT);
|
---|
552 |
|
---|
553 | if( /*func == (fptr_t)fallocate || */
|
---|
554 | func == (fptr_t)cfa_fallocate )
|
---|
555 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FALLOCATE ,
|
---|
556 | return IS_DEFINED(CFA_HAVE_IORING_OP_FALLOCATE);
|
---|
557 |
|
---|
558 | if( /*func == (fptr_t)posix_fadvise || */
|
---|
559 | func == (fptr_t)cfa_fadvise )
|
---|
560 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FADVISE ,
|
---|
561 | return IS_DEFINED(CFA_HAVE_IORING_OP_FADVISE);
|
---|
562 |
|
---|
563 | if( /*func == (fptr_t)madvise || */
|
---|
564 | func == (fptr_t)cfa_madvise )
|
---|
565 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_MADVISE ,
|
---|
566 | return IS_DEFINED(CFA_HAVE_IORING_OP_MADVISE);
|
---|
567 |
|
---|
568 | if( /*func == (fptr_t)openat || */
|
---|
569 | func == (fptr_t)cfa_openat )
|
---|
570 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_OPENAT ,
|
---|
571 | return IS_DEFINED(CFA_HAVE_IORING_OP_OPENAT);
|
---|
572 |
|
---|
573 | if( /*func == (fptr_t)close || */
|
---|
574 | func == (fptr_t)cfa_close )
|
---|
575 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_CLOSE ,
|
---|
576 | return IS_DEFINED(CFA_HAVE_IORING_OP_CLOSE);
|
---|
577 |
|
---|
578 | if( /*func == (fptr_t)read || */
|
---|
579 | func == (fptr_t)cfa_read )
|
---|
580 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_READ ,
|
---|
581 | return IS_DEFINED(CFA_HAVE_IORING_OP_READ);
|
---|
582 |
|
---|
583 | if( /*func == (fptr_t)write || */
|
---|
584 | func == (fptr_t)cfa_write )
|
---|
585 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_WRITE ,
|
---|
586 | return IS_DEFINED(CFA_HAVE_IORING_OP_WRITE);
|
---|
587 |
|
---|
588 | if( /*func == (fptr_t)splice || */
|
---|
589 | func == (fptr_t)cfa_splice )
|
---|
590 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SPLICE ,
|
---|
591 | return IS_DEFINED(CFA_HAVE_IORING_OP_SPLICE);
|
---|
592 |
|
---|
593 | if( /*func == (fptr_t)tee || */
|
---|
594 | func == (fptr_t)cfa_tee )
|
---|
595 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_TEE ,
|
---|
596 | return IS_DEFINED(CFA_HAVE_IORING_OP_TEE);
|
---|
597 | #endif
|
---|
598 |
|
---|
599 | return false;
|
---|
600 | }
|
---|