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