[31bb2e1] | 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 |
|
---|
[3e2b9c9] | 16 | #define __cforall_thread__
|
---|
| 17 |
|
---|
[5877b3e] | 18 | #include "bits/defs.hfa"
|
---|
[1a39a5a] | 19 | #include "kernel.hfa"
|
---|
[5877b3e] | 20 |
|
---|
[4b84e35] | 21 | //=============================================================================================
|
---|
| 22 | // I/O uring backend
|
---|
| 23 | //=============================================================================================
|
---|
| 24 |
|
---|
[5751a56] | 25 | #if defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
[3e2b9c9] | 26 | #include <assert.h>
|
---|
[31bb2e1] | 27 | #include <stdint.h>
|
---|
[f00b26d4] | 28 | #include <errno.h>
|
---|
[31bb2e1] | 29 | #include <linux/io_uring.h>
|
---|
| 30 |
|
---|
[3e2b9c9] | 31 | #include "kernel/fwd.hfa"
|
---|
| 32 | #include "io/types.hfa"
|
---|
[31bb2e1] | 33 |
|
---|
| 34 | extern [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data );
|
---|
[f00b26d4] | 35 | extern void __submit( struct io_context * ctx, uint32_t idx ) __attribute__((nonnull (1)));
|
---|
[31bb2e1] | 36 |
|
---|
[4b84e35] | 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 | }
|
---|
[31bb2e1] | 59 |
|
---|
[3e2b9c9] | 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 | }
|
---|
[f00b26d4] | 67 |
|
---|
| 68 |
|
---|
[97c3159] | 69 | #if defined(CFA_HAVE_IOSQE_FIXED_FILE) && defined(CFA_HAVE_IOSQE_IO_DRAIN) && defined(CFA_HAVE_IOSQE_ASYNC)
|
---|
[f00b26d4] | 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)
|
---|
[97c3159] | 73 | #elif defined(CFA_HAVE_IOSQE_FIXED_FILE) && defined(CFA_HAVE_IOSQE_IO_DRAIN)
|
---|
[f00b26d4] | 74 | #define REGULAR_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_DRAIN)
|
---|
[97c3159] | 75 | #elif defined(CFA_HAVE_IOSQE_IO_DRAIN) && defined(CFA_HAVE_IOSQE_ASYNC)
|
---|
[f00b26d4] | 76 | #define REGULAR_FLAGS (IOSQE_IO_DRAIN | IOSQE_ASYNC)
|
---|
| 77 | #elif defined(CFA_HAVE_IOSQE_FIXED_FILE)
|
---|
| 78 | #define REGULAR_FLAGS (IOSQE_FIXED_FILE)
|
---|
[97c3159] | 79 | #elif defined(CFA_HAVE_IOSQE_IO_DRAIN)
|
---|
[f00b26d4] | 80 | #define REGULAR_FLAGS (IOSQE_IO_DRAIN)
|
---|
[97c3159] | 81 | #elif defined(CFA_HAVE_IOSQE_ASYNC)
|
---|
[f00b26d4] | 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 |
|
---|
[31bb2e1] | 103 | #define __submit_prelude \
|
---|
[f00b26d4] | 104 | if( 0 != (submit_flags & LINK_FLAGS) ) { errno = ENOTSUP; return -1; } \
|
---|
| 105 | (void)timeout; (void)cancellation; \
|
---|
| 106 | if( !context ) context = __get_io_context(); \
|
---|
[31bb2e1] | 107 | __io_user_data_t data = { 0, active_thread() }; \
|
---|
[f00b26d4] | 108 | struct __io_data & ring = *context->thrd.ring; \
|
---|
[31bb2e1] | 109 | struct io_uring_sqe * sqe; \
|
---|
| 110 | uint32_t idx; \
|
---|
[2606a03] | 111 | uint8_t sflags = REGULAR_FLAGS & submit_flags; \
|
---|
[f00b26d4] | 112 | [sqe, idx] = __submit_alloc( ring, (uint64_t)(uintptr_t)&data ); \
|
---|
[2606a03] | 113 | sqe->flags = sflags;
|
---|
[31bb2e1] | 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 ); \
|
---|
[f00b26d4] | 118 | __submit( context, idx ); \
|
---|
[31bb2e1] | 119 | park( __cfaabi_dbg_ctx ); \
|
---|
[f00b26d4] | 120 | if( data.result < 0 ) { \
|
---|
| 121 | errno = -data.result; \
|
---|
| 122 | return -1; \
|
---|
| 123 | } \
|
---|
[31bb2e1] | 124 | return data.result;
|
---|
[4b84e35] | 125 | #endif
|
---|
[31bb2e1] | 126 |
|
---|
[4b84e35] | 127 | //=============================================================================================
|
---|
| 128 | // I/O Forwards
|
---|
| 129 | //=============================================================================================
|
---|
[f00b26d4] | 130 | #include <time.hfa>
|
---|
[31bb2e1] | 131 |
|
---|
[4b84e35] | 132 | // Some forward declarations
|
---|
[cf48a14] | 133 | #include <errno.h>
|
---|
[4b84e35] | 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);
|
---|
[0a92c78] | 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);
|
---|
[4b84e35] | 173 | }
|
---|
[31bb2e1] | 174 |
|
---|
[4b84e35] | 175 | //=============================================================================================
|
---|
| 176 | // I/O Interface
|
---|
| 177 | //=============================================================================================
|
---|
[31bb2e1] | 178 |
|
---|
[4b84e35] | 179 | //-----------------------------------------------------------------------------
|
---|
| 180 | // Asynchronous operations
|
---|
| 181 | #if defined(HAVE_PREADV2)
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 183 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_READV)
|
---|
[20ab637] | 184 | return preadv2(fd, iov, iovcnt, offset, flags);
|
---|
| 185 | #else
|
---|
| 186 | __submit_prelude
|
---|
| 187 |
|
---|
[2606a03] | 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;
|
---|
[20ab637] | 196 |
|
---|
| 197 | __submit_wait
|
---|
| 198 | #endif
|
---|
| 199 | }
|
---|
[4b84e35] | 200 | #endif
|
---|
[31bb2e1] | 201 |
|
---|
[4b84e35] | 202 | #if defined(HAVE_PWRITEV2)
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 204 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_WRITEV)
|
---|
[4b84e35] | 205 | return pwritev2(fd, iov, iovcnt, offset, flags);
|
---|
[31bb2e1] | 206 | #else
|
---|
| 207 | __submit_prelude
|
---|
| 208 |
|
---|
[4b84e35] | 209 | (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset };
|
---|
[31bb2e1] | 210 |
|
---|
| 211 | __submit_wait
|
---|
| 212 | #endif
|
---|
| 213 | }
|
---|
[4b84e35] | 214 | #endif
|
---|
[31bb2e1] | 215 |
|
---|
[f00b26d4] | 216 | int cfa_fsync(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 217 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FSYNC)
|
---|
[4b84e35] | 218 | return fsync(fd);
|
---|
| 219 | #else
|
---|
| 220 | __submit_prelude
|
---|
[31bb2e1] | 221 |
|
---|
[4b84e35] | 222 | (*sqe){ IORING_OP_FSYNC, fd };
|
---|
[31bb2e1] | 223 |
|
---|
[4b84e35] | 224 | __submit_wait
|
---|
| 225 | #endif
|
---|
| 226 | }
|
---|
[31bb2e1] | 227 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 229 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SYNC_FILE_RANGE)
|
---|
[4b84e35] | 230 | return sync_file_range(fd, offset, nbytes, flags);
|
---|
| 231 | #else
|
---|
| 232 | __submit_prelude
|
---|
[31bb2e1] | 233 |
|
---|
[4b84e35] | 234 | (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd };
|
---|
| 235 | sqe->off = offset;
|
---|
| 236 | sqe->len = nbytes;
|
---|
| 237 | sqe->sync_range_flags = flags;
|
---|
[31bb2e1] | 238 |
|
---|
[4b84e35] | 239 | __submit_wait
|
---|
| 240 | #endif
|
---|
| 241 | }
|
---|
[31bb2e1] | 242 |
|
---|
| 243 |
|
---|
[f00b26d4] | 244 | ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 245 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SENDMSG)
|
---|
[4b84e35] | 246 | return sendmsg(sockfd, msg, flags);
|
---|
| 247 | #else
|
---|
| 248 | __submit_prelude
|
---|
[31bb2e1] | 249 |
|
---|
[4b84e35] | 250 | (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 };
|
---|
| 251 | sqe->msg_flags = flags;
|
---|
[31bb2e1] | 252 |
|
---|
[4b84e35] | 253 | __submit_wait
|
---|
| 254 | #endif
|
---|
| 255 | }
|
---|
[31bb2e1] | 256 |
|
---|
[f00b26d4] | 257 | ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 258 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_RECVMSG)
|
---|
[4b84e35] | 259 | return recvmsg(sockfd, msg, flags);
|
---|
| 260 | #else
|
---|
| 261 | __submit_prelude
|
---|
[31bb2e1] | 262 |
|
---|
[4b84e35] | 263 | (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 };
|
---|
| 264 | sqe->msg_flags = flags;
|
---|
[31bb2e1] | 265 |
|
---|
[4b84e35] | 266 | __submit_wait
|
---|
| 267 | #endif
|
---|
| 268 | }
|
---|
[31bb2e1] | 269 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 271 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SEND)
|
---|
[4b84e35] | 272 | return send( sockfd, buf, len, flags );
|
---|
| 273 | #else
|
---|
| 274 | __submit_prelude
|
---|
[31bb2e1] | 275 |
|
---|
[4b84e35] | 276 | (*sqe){ IORING_OP_SEND, sockfd };
|
---|
| 277 | sqe->addr = (uint64_t)buf;
|
---|
| 278 | sqe->len = len;
|
---|
| 279 | sqe->msg_flags = flags;
|
---|
[31bb2e1] | 280 |
|
---|
[4b84e35] | 281 | __submit_wait
|
---|
| 282 | #endif
|
---|
| 283 | }
|
---|
[31bb2e1] | 284 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 286 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_RECV)
|
---|
[4b84e35] | 287 | return recv( sockfd, buf, len, flags );
|
---|
| 288 | #else
|
---|
| 289 | __submit_prelude
|
---|
[31bb2e1] | 290 |
|
---|
[4b84e35] | 291 | (*sqe){ IORING_OP_RECV, sockfd };
|
---|
| 292 | sqe->addr = (uint64_t)buf;
|
---|
| 293 | sqe->len = len;
|
---|
| 294 | sqe->msg_flags = flags;
|
---|
[31bb2e1] | 295 |
|
---|
[4b84e35] | 296 | __submit_wait
|
---|
| 297 | #endif
|
---|
| 298 | }
|
---|
[31bb2e1] | 299 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 301 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_ACCEPT)
|
---|
[4b84e35] | 302 | return accept4( sockfd, addr, addrlen, flags );
|
---|
| 303 | #else
|
---|
| 304 | __submit_prelude
|
---|
[31bb2e1] | 305 |
|
---|
[4b84e35] | 306 | (*sqe){ IORING_OP_ACCEPT, sockfd };
|
---|
[3f850d7] | 307 | sqe->addr = (uint64_t)(uintptr_t)addr;
|
---|
| 308 | sqe->addr2 = (uint64_t)(uintptr_t)addrlen;
|
---|
[4b84e35] | 309 | sqe->accept_flags = flags;
|
---|
[31bb2e1] | 310 |
|
---|
[4b84e35] | 311 | __submit_wait
|
---|
| 312 | #endif
|
---|
| 313 | }
|
---|
[31bb2e1] | 314 |
|
---|
[f00b26d4] | 315 | int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 316 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_CONNECT)
|
---|
[4b84e35] | 317 | return connect( sockfd, addr, addrlen );
|
---|
| 318 | #else
|
---|
| 319 | __submit_prelude
|
---|
[31bb2e1] | 320 |
|
---|
[4b84e35] | 321 | (*sqe){ IORING_OP_CONNECT, sockfd };
|
---|
[3f850d7] | 322 | sqe->addr = (uint64_t)(uintptr_t)addr;
|
---|
| 323 | sqe->off = (uint64_t)(uintptr_t)addrlen;
|
---|
[31bb2e1] | 324 |
|
---|
[4b84e35] | 325 | __submit_wait
|
---|
| 326 | #endif
|
---|
| 327 | }
|
---|
[31bb2e1] | 328 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 330 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FALLOCATE)
|
---|
[4b84e35] | 331 | return fallocate( fd, mode, offset, len );
|
---|
| 332 | #else
|
---|
| 333 | __submit_prelude
|
---|
[31bb2e1] | 334 |
|
---|
[85eafc5] | 335 | #warning FALLOCATE documentation for linux 5.7 is incorrect, and does not handle mode
|
---|
[3f850d7] | 336 |
|
---|
[4b84e35] | 337 | (*sqe){ IORING_OP_FALLOCATE, fd };
|
---|
| 338 | sqe->off = offset;
|
---|
[3f850d7] | 339 | sqe->len = mode;
|
---|
| 340 | sqe->addr = len;
|
---|
[31bb2e1] | 341 |
|
---|
[4b84e35] | 342 | __submit_wait
|
---|
| 343 | #endif
|
---|
| 344 | }
|
---|
[31bb2e1] | 345 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 347 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FADVISE)
|
---|
[4b84e35] | 348 | return posix_fadvise( fd, offset, len, advice );
|
---|
| 349 | #else
|
---|
| 350 | __submit_prelude
|
---|
[31bb2e1] | 351 |
|
---|
[4b84e35] | 352 | (*sqe){ IORING_OP_FADVISE, fd };
|
---|
| 353 | sqe->off = (uint64_t)offset;
|
---|
[3f850d7] | 354 | sqe->len = len;
|
---|
[4b84e35] | 355 | sqe->fadvise_advice = advice;
|
---|
[31bb2e1] | 356 |
|
---|
[4b84e35] | 357 | __submit_wait
|
---|
| 358 | #endif
|
---|
| 359 | }
|
---|
[31bb2e1] | 360 |
|
---|
[f00b26d4] | 361 | int cfa_madvise(void *addr, size_t length, int advice, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 362 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_MADVISE)
|
---|
[4b84e35] | 363 | return madvise( addr, length, advice );
|
---|
| 364 | #else
|
---|
| 365 | __submit_prelude
|
---|
[31bb2e1] | 366 |
|
---|
[4b84e35] | 367 | (*sqe){ IORING_OP_MADVISE, 0 };
|
---|
| 368 | sqe->addr = (uint64_t)addr;
|
---|
| 369 | sqe->len = length;
|
---|
| 370 | sqe->fadvise_advice = advice;
|
---|
[31bb2e1] | 371 |
|
---|
[4b84e35] | 372 | __submit_wait
|
---|
| 373 | #endif
|
---|
| 374 | }
|
---|
[31bb2e1] | 375 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 377 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_OPENAT)
|
---|
[4b84e35] | 378 | return openat( dirfd, pathname, flags, mode );
|
---|
| 379 | #else
|
---|
| 380 | __submit_prelude
|
---|
[31bb2e1] | 381 |
|
---|
[4b84e35] | 382 | (*sqe){ IORING_OP_OPENAT, dirfd };
|
---|
| 383 | sqe->addr = (uint64_t)pathname;
|
---|
| 384 | sqe->open_flags = flags;
|
---|
[3f850d7] | 385 | sqe->len = mode;
|
---|
[31bb2e1] | 386 |
|
---|
[4b84e35] | 387 | __submit_wait
|
---|
| 388 | #endif
|
---|
| 389 | }
|
---|
[31bb2e1] | 390 |
|
---|
[f00b26d4] | 391 | int cfa_close(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 392 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_CLOSE)
|
---|
[4b84e35] | 393 | return close( fd );
|
---|
| 394 | #else
|
---|
| 395 | __submit_prelude
|
---|
[31bb2e1] | 396 |
|
---|
[4b84e35] | 397 | (*sqe){ IORING_OP_CLOSE, fd };
|
---|
| 398 |
|
---|
| 399 | __submit_wait
|
---|
| 400 | #endif
|
---|
| 401 | }
|
---|
[31bb2e1] | 402 |
|
---|
[efc171d1] | 403 | // Forward declare in case it is not supported
|
---|
| 404 | struct statx;
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 406 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_STATX)
|
---|
[5db836e] | 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
|
---|
[519f11c] | 413 | #else
|
---|
| 414 | __submit_prelude
|
---|
| 415 |
|
---|
| 416 | (*sqe){ IORING_OP_STATX, dirfd, pathname, mask, (uint64_t)statxbuf };
|
---|
[f00b26d4] | 417 | sqe->statx_flags = flags;
|
---|
[519f11c] | 418 |
|
---|
| 419 | __submit_wait
|
---|
| 420 | #endif
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[f00b26d4] | 423 | ssize_t cfa_read(int fd, void *buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 424 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_READ)
|
---|
[4b84e35] | 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 |
|
---|
[f00b26d4] | 435 | ssize_t cfa_write(int fd, void *buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 436 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_WRITE)
|
---|
[4b84e35] | 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 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 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 |
|
---|
[3f850d7] | 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;
|
---|
[5751a56] | 461 | sqe->splice_fd_in = fd_in;
|
---|
[3f850d7] | 462 | if( off_in ) {
|
---|
| 463 | sqe->splice_off_in = *off_in;
|
---|
| 464 | }
|
---|
| 465 | else {
|
---|
| 466 | sqe->splice_off_in = (uint64_t)-1;
|
---|
| 467 | }
|
---|
[f00b26d4] | 468 | sqe->splice_flags = flags | (SPLICE_FLAGS & submit_flags);
|
---|
[5751a56] | 469 |
|
---|
| 470 | __submit_wait
|
---|
| 471 | #endif
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 475 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_TEE)
|
---|
[0a92c78] | 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;
|
---|
[f00b26d4] | 482 | sqe->splice_flags = flags | (SPLICE_FLAGS & submit_flags);
|
---|
[0a92c78] | 483 |
|
---|
| 484 | __submit_wait
|
---|
| 485 | #endif
|
---|
| 486 | }
|
---|
| 487 |
|
---|
[4b84e35] | 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 ) {
|
---|
[5751a56] | 498 | #if defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
[4b84e35] | 499 | #if defined(HAVE_PREADV2)
|
---|
| 500 | if( /*func == (fptr_t)preadv2 || */
|
---|
| 501 | func == (fptr_t)cfa_preadv2 )
|
---|
[5751a56] | 502 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_READV ,
|
---|
| 503 | return IS_DEFINED(CFA_HAVE_IORING_OP_READV);
|
---|
[31bb2e1] | 504 | #endif
|
---|
| 505 |
|
---|
[4b84e35] | 506 | #if defined(HAVE_PWRITEV2)
|
---|
| 507 | if( /*func == (fptr_t)pwritev2 || */
|
---|
| 508 | func == (fptr_t)cfa_pwritev2 )
|
---|
[5751a56] | 509 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_WRITEV ,
|
---|
| 510 | return IS_DEFINED(CFA_HAVE_IORING_OP_WRITEV);
|
---|
[31bb2e1] | 511 | #endif
|
---|
| 512 |
|
---|
[4b84e35] | 513 | if( /*func == (fptr_t)fsync || */
|
---|
| 514 | func == (fptr_t)cfa_fsync )
|
---|
[5751a56] | 515 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FSYNC ,
|
---|
| 516 | return IS_DEFINED(CFA_HAVE_IORING_OP_FSYNC);
|
---|
[4b84e35] | 517 |
|
---|
| 518 | if( /*func == (fptr_t)ync_file_range || */
|
---|
| 519 | func == (fptr_t)cfa_sync_file_range )
|
---|
[5751a56] | 520 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SYNC_FILE_RANGE ,
|
---|
| 521 | return IS_DEFINED(CFA_HAVE_IORING_OP_SYNC_FILE_RANGE);
|
---|
[4b84e35] | 522 |
|
---|
| 523 | if( /*func == (fptr_t)sendmsg || */
|
---|
| 524 | func == (fptr_t)cfa_sendmsg )
|
---|
[5751a56] | 525 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SENDMSG ,
|
---|
| 526 | return IS_DEFINED(CFA_HAVE_IORING_OP_SENDMSG);
|
---|
[4b84e35] | 527 |
|
---|
| 528 | if( /*func == (fptr_t)recvmsg || */
|
---|
| 529 | func == (fptr_t)cfa_recvmsg )
|
---|
[5751a56] | 530 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_RECVMSG ,
|
---|
| 531 | return IS_DEFINED(CFA_HAVE_IORING_OP_RECVMSG);
|
---|
[4b84e35] | 532 |
|
---|
| 533 | if( /*func == (fptr_t)send || */
|
---|
| 534 | func == (fptr_t)cfa_send )
|
---|
[5751a56] | 535 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SEND ,
|
---|
| 536 | return IS_DEFINED(CFA_HAVE_IORING_OP_SEND);
|
---|
[4b84e35] | 537 |
|
---|
| 538 | if( /*func == (fptr_t)recv || */
|
---|
| 539 | func == (fptr_t)cfa_recv )
|
---|
[5751a56] | 540 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_RECV ,
|
---|
| 541 | return IS_DEFINED(CFA_HAVE_IORING_OP_RECV);
|
---|
[4b84e35] | 542 |
|
---|
| 543 | if( /*func == (fptr_t)accept4 || */
|
---|
| 544 | func == (fptr_t)cfa_accept4 )
|
---|
[5751a56] | 545 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_ACCEPT ,
|
---|
| 546 | return IS_DEFINED(CFA_HAVE_IORING_OP_ACCEPT);
|
---|
[4b84e35] | 547 |
|
---|
| 548 | if( /*func == (fptr_t)connect || */
|
---|
| 549 | func == (fptr_t)cfa_connect )
|
---|
[5751a56] | 550 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_CONNECT ,
|
---|
| 551 | return IS_DEFINED(CFA_HAVE_IORING_OP_CONNECT);
|
---|
[4b84e35] | 552 |
|
---|
| 553 | if( /*func == (fptr_t)fallocate || */
|
---|
| 554 | func == (fptr_t)cfa_fallocate )
|
---|
[5751a56] | 555 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FALLOCATE ,
|
---|
| 556 | return IS_DEFINED(CFA_HAVE_IORING_OP_FALLOCATE);
|
---|
[4b84e35] | 557 |
|
---|
| 558 | if( /*func == (fptr_t)posix_fadvise || */
|
---|
| 559 | func == (fptr_t)cfa_fadvise )
|
---|
[5751a56] | 560 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FADVISE ,
|
---|
| 561 | return IS_DEFINED(CFA_HAVE_IORING_OP_FADVISE);
|
---|
[4b84e35] | 562 |
|
---|
| 563 | if( /*func == (fptr_t)madvise || */
|
---|
| 564 | func == (fptr_t)cfa_madvise )
|
---|
[5751a56] | 565 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_MADVISE ,
|
---|
| 566 | return IS_DEFINED(CFA_HAVE_IORING_OP_MADVISE);
|
---|
[4b84e35] | 567 |
|
---|
| 568 | if( /*func == (fptr_t)openat || */
|
---|
| 569 | func == (fptr_t)cfa_openat )
|
---|
[5751a56] | 570 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_OPENAT ,
|
---|
| 571 | return IS_DEFINED(CFA_HAVE_IORING_OP_OPENAT);
|
---|
[4b84e35] | 572 |
|
---|
| 573 | if( /*func == (fptr_t)close || */
|
---|
| 574 | func == (fptr_t)cfa_close )
|
---|
[5751a56] | 575 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_CLOSE ,
|
---|
| 576 | return IS_DEFINED(CFA_HAVE_IORING_OP_CLOSE);
|
---|
[4b84e35] | 577 |
|
---|
| 578 | if( /*func == (fptr_t)read || */
|
---|
| 579 | func == (fptr_t)cfa_read )
|
---|
[5751a56] | 580 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_READ ,
|
---|
| 581 | return IS_DEFINED(CFA_HAVE_IORING_OP_READ);
|
---|
[4b84e35] | 582 |
|
---|
| 583 | if( /*func == (fptr_t)write || */
|
---|
| 584 | func == (fptr_t)cfa_write )
|
---|
[5751a56] | 585 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_WRITE ,
|
---|
| 586 | return IS_DEFINED(CFA_HAVE_IORING_OP_WRITE);
|
---|
[0a92c78] | 587 |
|
---|
| 588 | if( /*func == (fptr_t)splice || */
|
---|
[f00b26d4] | 589 | func == (fptr_t)cfa_splice )
|
---|
[5751a56] | 590 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SPLICE ,
|
---|
| 591 | return IS_DEFINED(CFA_HAVE_IORING_OP_SPLICE);
|
---|
[0a92c78] | 592 |
|
---|
| 593 | if( /*func == (fptr_t)tee || */
|
---|
| 594 | func == (fptr_t)cfa_tee )
|
---|
[5751a56] | 595 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_TEE ,
|
---|
| 596 | return IS_DEFINED(CFA_HAVE_IORING_OP_TEE);
|
---|
[4b84e35] | 597 | #endif
|
---|
| 598 |
|
---|
| 599 | return false;
|
---|
[85eafc5] | 600 | }
|
---|