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