[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 |
|
---|
[4998155] | 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)));
|
---|
[31bb2e1] | 36 |
|
---|
[4998155] | 37 | static inline void ?{}(struct io_uring_sqe & this, __u8 opcode, int fd) {
|
---|
[4b84e35] | 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 |
|
---|
[4998155] | 53 | static inline void ?{}(struct io_uring_sqe & this, __u8 opcode, int fd, void * addr, __u32 len, __u64 off ) {
|
---|
[4b84e35] | 54 | (this){ opcode, fd };
|
---|
| 55 | this.off = off;
|
---|
[4998155] | 56 | this.addr = (__u64)(uintptr_t)addr;
|
---|
[4b84e35] | 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(); \
|
---|
[4998155] | 107 | __io_user_data_t data = { 0 }; \
|
---|
[f00b26d4] | 108 | struct __io_data & ring = *context->thrd.ring; \
|
---|
[31bb2e1] | 109 | struct io_uring_sqe * sqe; \
|
---|
[4998155] | 110 | __u32 idx; \
|
---|
| 111 | __u8 sflags = REGULAR_FLAGS & submit_flags; \
|
---|
| 112 | [sqe, idx] = __submit_alloc( ring, (__u64)(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 );*/ \
|
---|
[4998155] | 117 | verify( sqe->user_data == (__u64)(uintptr_t)&data ); \
|
---|
[f00b26d4] | 118 | __submit( context, idx ); \
|
---|
[4998155] | 119 | wait( data.sem ); \
|
---|
[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);
|
---|
[4998155] | 151 |
|
---|
[133a161] | 152 | #if __OFF_T_MATCHES_OFF64_T
|
---|
| 153 | typedef __off64_t off_t;
|
---|
| 154 | #else
|
---|
| 155 | typedef __off_t off_t;
|
---|
| 156 | #endif
|
---|
[4998155] | 157 | typedef __off64_t off64_t;
|
---|
| 158 | extern int sync_file_range(int fd, off64_t offset, off64_t nbytes, unsigned int flags);
|
---|
[4b84e35] | 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 |
|
---|
[4998155] | 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);
|
---|
[4b84e35] | 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);
|
---|
[0a92c78] | 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);
|
---|
[4b84e35] | 180 | }
|
---|
[31bb2e1] | 181 |
|
---|
[4b84e35] | 182 | //=============================================================================================
|
---|
| 183 | // I/O Interface
|
---|
| 184 | //=============================================================================================
|
---|
[31bb2e1] | 185 |
|
---|
[4b84e35] | 186 | //-----------------------------------------------------------------------------
|
---|
| 187 | // Asynchronous operations
|
---|
| 188 | #if defined(HAVE_PREADV2)
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 190 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_READV)
|
---|
[20ab637] | 191 | return preadv2(fd, iov, iovcnt, offset, flags);
|
---|
| 192 | #else
|
---|
| 193 | __submit_prelude
|
---|
| 194 |
|
---|
[2606a03] | 195 | sqe->opcode = IORING_OP_READV;
|
---|
| 196 | sqe->ioprio = 0;
|
---|
| 197 | sqe->fd = fd;
|
---|
| 198 | sqe->off = offset;
|
---|
[4998155] | 199 | sqe->addr = (__u64)iov;
|
---|
[2606a03] | 200 | sqe->len = iovcnt;
|
---|
| 201 | sqe->rw_flags = 0;
|
---|
| 202 | sqe->__pad2[0] = sqe->__pad2[1] = sqe->__pad2[2] = 0;
|
---|
[20ab637] | 203 |
|
---|
| 204 | __submit_wait
|
---|
| 205 | #endif
|
---|
| 206 | }
|
---|
[4b84e35] | 207 | #endif
|
---|
[31bb2e1] | 208 |
|
---|
[4b84e35] | 209 | #if defined(HAVE_PWRITEV2)
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 211 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_WRITEV)
|
---|
[4b84e35] | 212 | return pwritev2(fd, iov, iovcnt, offset, flags);
|
---|
[31bb2e1] | 213 | #else
|
---|
| 214 | __submit_prelude
|
---|
| 215 |
|
---|
[4998155] | 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;
|
---|
[31bb2e1] | 224 |
|
---|
| 225 | __submit_wait
|
---|
| 226 | #endif
|
---|
| 227 | }
|
---|
[4b84e35] | 228 | #endif
|
---|
[31bb2e1] | 229 |
|
---|
[f00b26d4] | 230 | int cfa_fsync(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 231 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FSYNC)
|
---|
[4b84e35] | 232 | return fsync(fd);
|
---|
| 233 | #else
|
---|
| 234 | __submit_prelude
|
---|
[31bb2e1] | 235 |
|
---|
[4998155] | 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;
|
---|
[31bb2e1] | 244 |
|
---|
[4b84e35] | 245 | __submit_wait
|
---|
| 246 | #endif
|
---|
| 247 | }
|
---|
[31bb2e1] | 248 |
|
---|
[4998155] | 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) {
|
---|
[5751a56] | 250 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SYNC_FILE_RANGE)
|
---|
[4b84e35] | 251 | return sync_file_range(fd, offset, nbytes, flags);
|
---|
| 252 | #else
|
---|
| 253 | __submit_prelude
|
---|
[31bb2e1] | 254 |
|
---|
[4b84e35] | 255 | (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd };
|
---|
| 256 | sqe->off = offset;
|
---|
| 257 | sqe->len = nbytes;
|
---|
| 258 | sqe->sync_range_flags = flags;
|
---|
[31bb2e1] | 259 |
|
---|
[4b84e35] | 260 | __submit_wait
|
---|
| 261 | #endif
|
---|
| 262 | }
|
---|
[31bb2e1] | 263 |
|
---|
| 264 |
|
---|
[f00b26d4] | 265 | ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 266 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SENDMSG)
|
---|
[4b84e35] | 267 | return sendmsg(sockfd, msg, flags);
|
---|
| 268 | #else
|
---|
| 269 | __submit_prelude
|
---|
[31bb2e1] | 270 |
|
---|
[4b84e35] | 271 | (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 };
|
---|
| 272 | sqe->msg_flags = flags;
|
---|
[31bb2e1] | 273 |
|
---|
[4b84e35] | 274 | __submit_wait
|
---|
| 275 | #endif
|
---|
| 276 | }
|
---|
[31bb2e1] | 277 |
|
---|
[f00b26d4] | 278 | ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, 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_RECVMSG)
|
---|
[4b84e35] | 280 | return recvmsg(sockfd, msg, flags);
|
---|
| 281 | #else
|
---|
| 282 | __submit_prelude
|
---|
[31bb2e1] | 283 |
|
---|
[4b84e35] | 284 | (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 };
|
---|
| 285 | sqe->msg_flags = flags;
|
---|
[31bb2e1] | 286 |
|
---|
[4b84e35] | 287 | __submit_wait
|
---|
| 288 | #endif
|
---|
| 289 | }
|
---|
[31bb2e1] | 290 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 292 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_SEND)
|
---|
[4b84e35] | 293 | return send( sockfd, buf, len, flags );
|
---|
| 294 | #else
|
---|
| 295 | __submit_prelude
|
---|
[31bb2e1] | 296 |
|
---|
[4b84e35] | 297 | (*sqe){ IORING_OP_SEND, sockfd };
|
---|
[4998155] | 298 | sqe->addr = (__u64)buf;
|
---|
[4b84e35] | 299 | sqe->len = len;
|
---|
| 300 | sqe->msg_flags = flags;
|
---|
[31bb2e1] | 301 |
|
---|
[4b84e35] | 302 | __submit_wait
|
---|
| 303 | #endif
|
---|
| 304 | }
|
---|
[31bb2e1] | 305 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 307 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_RECV)
|
---|
[4b84e35] | 308 | return recv( sockfd, buf, len, flags );
|
---|
| 309 | #else
|
---|
| 310 | __submit_prelude
|
---|
[31bb2e1] | 311 |
|
---|
[4b84e35] | 312 | (*sqe){ IORING_OP_RECV, sockfd };
|
---|
[4998155] | 313 | sqe->addr = (__u64)buf;
|
---|
[4b84e35] | 314 | sqe->len = len;
|
---|
| 315 | sqe->msg_flags = flags;
|
---|
[31bb2e1] | 316 |
|
---|
[4b84e35] | 317 | __submit_wait
|
---|
| 318 | #endif
|
---|
| 319 | }
|
---|
[31bb2e1] | 320 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 322 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_ACCEPT)
|
---|
[4b84e35] | 323 | return accept4( sockfd, addr, addrlen, flags );
|
---|
| 324 | #else
|
---|
| 325 | __submit_prelude
|
---|
[31bb2e1] | 326 |
|
---|
[4b84e35] | 327 | (*sqe){ IORING_OP_ACCEPT, sockfd };
|
---|
[4998155] | 328 | sqe->addr = (__u64)addr;
|
---|
| 329 | sqe->addr2 = (__u64)addrlen;
|
---|
[4b84e35] | 330 | sqe->accept_flags = flags;
|
---|
[31bb2e1] | 331 |
|
---|
[4b84e35] | 332 | __submit_wait
|
---|
| 333 | #endif
|
---|
| 334 | }
|
---|
[31bb2e1] | 335 |
|
---|
[f00b26d4] | 336 | int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 337 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_CONNECT)
|
---|
[4b84e35] | 338 | return connect( sockfd, addr, addrlen );
|
---|
| 339 | #else
|
---|
| 340 | __submit_prelude
|
---|
[31bb2e1] | 341 |
|
---|
[4b84e35] | 342 | (*sqe){ IORING_OP_CONNECT, sockfd };
|
---|
[4998155] | 343 | sqe->addr = (__u64)addr;
|
---|
| 344 | sqe->off = (__u64)addrlen;
|
---|
[31bb2e1] | 345 |
|
---|
[4b84e35] | 346 | __submit_wait
|
---|
| 347 | #endif
|
---|
| 348 | }
|
---|
[31bb2e1] | 349 |
|
---|
[4998155] | 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) {
|
---|
[5751a56] | 351 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FALLOCATE)
|
---|
[4b84e35] | 352 | return fallocate( fd, mode, offset, len );
|
---|
| 353 | #else
|
---|
| 354 | __submit_prelude
|
---|
[31bb2e1] | 355 |
|
---|
[85eafc5] | 356 | #warning FALLOCATE documentation for linux 5.7 is incorrect, and does not handle mode
|
---|
[3f850d7] | 357 |
|
---|
[4b84e35] | 358 | (*sqe){ IORING_OP_FALLOCATE, fd };
|
---|
| 359 | sqe->off = offset;
|
---|
[3f850d7] | 360 | sqe->len = mode;
|
---|
| 361 | sqe->addr = len;
|
---|
[31bb2e1] | 362 |
|
---|
[4b84e35] | 363 | __submit_wait
|
---|
| 364 | #endif
|
---|
| 365 | }
|
---|
[31bb2e1] | 366 |
|
---|
[4998155] | 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) {
|
---|
[5751a56] | 368 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_FADVISE)
|
---|
[4b84e35] | 369 | return posix_fadvise( fd, offset, len, advice );
|
---|
| 370 | #else
|
---|
| 371 | __submit_prelude
|
---|
[31bb2e1] | 372 |
|
---|
[4b84e35] | 373 | (*sqe){ IORING_OP_FADVISE, fd };
|
---|
[4998155] | 374 | sqe->off = (__u64)offset;
|
---|
[3f850d7] | 375 | sqe->len = len;
|
---|
[4b84e35] | 376 | sqe->fadvise_advice = advice;
|
---|
[31bb2e1] | 377 |
|
---|
[4b84e35] | 378 | __submit_wait
|
---|
| 379 | #endif
|
---|
| 380 | }
|
---|
[31bb2e1] | 381 |
|
---|
[f00b26d4] | 382 | int cfa_madvise(void *addr, size_t length, int advice, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 383 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_MADVISE)
|
---|
[4b84e35] | 384 | return madvise( addr, length, advice );
|
---|
| 385 | #else
|
---|
| 386 | __submit_prelude
|
---|
[31bb2e1] | 387 |
|
---|
[4b84e35] | 388 | (*sqe){ IORING_OP_MADVISE, 0 };
|
---|
[4998155] | 389 | sqe->addr = (__u64)addr;
|
---|
[4b84e35] | 390 | sqe->len = length;
|
---|
| 391 | sqe->fadvise_advice = advice;
|
---|
[31bb2e1] | 392 |
|
---|
[4b84e35] | 393 | __submit_wait
|
---|
| 394 | #endif
|
---|
| 395 | }
|
---|
[31bb2e1] | 396 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 398 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_OPENAT)
|
---|
[4b84e35] | 399 | return openat( dirfd, pathname, flags, mode );
|
---|
| 400 | #else
|
---|
| 401 | __submit_prelude
|
---|
[31bb2e1] | 402 |
|
---|
[4b84e35] | 403 | (*sqe){ IORING_OP_OPENAT, dirfd };
|
---|
[4998155] | 404 | sqe->addr = (__u64)pathname;
|
---|
[4b84e35] | 405 | sqe->open_flags = flags;
|
---|
[3f850d7] | 406 | sqe->len = mode;
|
---|
[31bb2e1] | 407 |
|
---|
[4b84e35] | 408 | __submit_wait
|
---|
| 409 | #endif
|
---|
| 410 | }
|
---|
[31bb2e1] | 411 |
|
---|
[f00b26d4] | 412 | int cfa_close(int fd, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 413 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_CLOSE)
|
---|
[4b84e35] | 414 | return close( fd );
|
---|
| 415 | #else
|
---|
| 416 | __submit_prelude
|
---|
[31bb2e1] | 417 |
|
---|
[4b84e35] | 418 | (*sqe){ IORING_OP_CLOSE, fd };
|
---|
| 419 |
|
---|
| 420 | __submit_wait
|
---|
| 421 | #endif
|
---|
| 422 | }
|
---|
[31bb2e1] | 423 |
|
---|
[efc171d1] | 424 | // Forward declare in case it is not supported
|
---|
| 425 | struct statx;
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 427 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_STATX)
|
---|
[5db836e] | 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
|
---|
[519f11c] | 434 | #else
|
---|
| 435 | __submit_prelude
|
---|
| 436 |
|
---|
[4998155] | 437 | (*sqe){ IORING_OP_STATX, dirfd, pathname, mask, (__u64)statxbuf };
|
---|
[f00b26d4] | 438 | sqe->statx_flags = flags;
|
---|
[519f11c] | 439 |
|
---|
| 440 | __submit_wait
|
---|
| 441 | #endif
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[f00b26d4] | 444 | ssize_t cfa_read(int fd, void *buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 445 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_READ)
|
---|
[4b84e35] | 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 |
|
---|
[f00b26d4] | 456 | ssize_t cfa_write(int fd, void *buf, size_t count, int submit_flags, Duration timeout, io_cancellation * cancellation, io_context * context) {
|
---|
[5751a56] | 457 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_WRITE)
|
---|
[4b84e35] | 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 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 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 |
|
---|
[3f850d7] | 474 | (*sqe){ IORING_OP_SPLICE, fd_out };
|
---|
| 475 | if( off_out ) {
|
---|
| 476 | sqe->off = *off_out;
|
---|
| 477 | }
|
---|
| 478 | else {
|
---|
[4998155] | 479 | sqe->off = (__u64)-1;
|
---|
[3f850d7] | 480 | }
|
---|
| 481 | sqe->len = len;
|
---|
[5751a56] | 482 | sqe->splice_fd_in = fd_in;
|
---|
[3f850d7] | 483 | if( off_in ) {
|
---|
| 484 | sqe->splice_off_in = *off_in;
|
---|
| 485 | }
|
---|
| 486 | else {
|
---|
[4998155] | 487 | sqe->splice_off_in = (__u64)-1;
|
---|
[3f850d7] | 488 | }
|
---|
[f00b26d4] | 489 | sqe->splice_flags = flags | (SPLICE_FLAGS & submit_flags);
|
---|
[5751a56] | 490 |
|
---|
| 491 | __submit_wait
|
---|
| 492 | #endif
|
---|
| 493 | }
|
---|
| 494 |
|
---|
[f00b26d4] | 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) {
|
---|
[5751a56] | 496 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_TEE)
|
---|
[0a92c78] | 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;
|
---|
[f00b26d4] | 503 | sqe->splice_flags = flags | (SPLICE_FLAGS & submit_flags);
|
---|
[0a92c78] | 504 |
|
---|
| 505 | __submit_wait
|
---|
| 506 | #endif
|
---|
| 507 | }
|
---|
| 508 |
|
---|
[4b84e35] | 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 ) {
|
---|
[5751a56] | 519 | #if defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
[4b84e35] | 520 | #if defined(HAVE_PREADV2)
|
---|
| 521 | if( /*func == (fptr_t)preadv2 || */
|
---|
| 522 | func == (fptr_t)cfa_preadv2 )
|
---|
[5751a56] | 523 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_READV ,
|
---|
| 524 | return IS_DEFINED(CFA_HAVE_IORING_OP_READV);
|
---|
[31bb2e1] | 525 | #endif
|
---|
| 526 |
|
---|
[4b84e35] | 527 | #if defined(HAVE_PWRITEV2)
|
---|
| 528 | if( /*func == (fptr_t)pwritev2 || */
|
---|
| 529 | func == (fptr_t)cfa_pwritev2 )
|
---|
[5751a56] | 530 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_WRITEV ,
|
---|
| 531 | return IS_DEFINED(CFA_HAVE_IORING_OP_WRITEV);
|
---|
[31bb2e1] | 532 | #endif
|
---|
| 533 |
|
---|
[4b84e35] | 534 | if( /*func == (fptr_t)fsync || */
|
---|
| 535 | func == (fptr_t)cfa_fsync )
|
---|
[5751a56] | 536 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FSYNC ,
|
---|
| 537 | return IS_DEFINED(CFA_HAVE_IORING_OP_FSYNC);
|
---|
[4b84e35] | 538 |
|
---|
| 539 | if( /*func == (fptr_t)ync_file_range || */
|
---|
| 540 | func == (fptr_t)cfa_sync_file_range )
|
---|
[5751a56] | 541 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SYNC_FILE_RANGE ,
|
---|
| 542 | return IS_DEFINED(CFA_HAVE_IORING_OP_SYNC_FILE_RANGE);
|
---|
[4b84e35] | 543 |
|
---|
| 544 | if( /*func == (fptr_t)sendmsg || */
|
---|
| 545 | func == (fptr_t)cfa_sendmsg )
|
---|
[5751a56] | 546 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SENDMSG ,
|
---|
| 547 | return IS_DEFINED(CFA_HAVE_IORING_OP_SENDMSG);
|
---|
[4b84e35] | 548 |
|
---|
| 549 | if( /*func == (fptr_t)recvmsg || */
|
---|
| 550 | func == (fptr_t)cfa_recvmsg )
|
---|
[5751a56] | 551 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_RECVMSG ,
|
---|
| 552 | return IS_DEFINED(CFA_HAVE_IORING_OP_RECVMSG);
|
---|
[4b84e35] | 553 |
|
---|
| 554 | if( /*func == (fptr_t)send || */
|
---|
| 555 | func == (fptr_t)cfa_send )
|
---|
[5751a56] | 556 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SEND ,
|
---|
| 557 | return IS_DEFINED(CFA_HAVE_IORING_OP_SEND);
|
---|
[4b84e35] | 558 |
|
---|
| 559 | if( /*func == (fptr_t)recv || */
|
---|
| 560 | func == (fptr_t)cfa_recv )
|
---|
[5751a56] | 561 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_RECV ,
|
---|
| 562 | return IS_DEFINED(CFA_HAVE_IORING_OP_RECV);
|
---|
[4b84e35] | 563 |
|
---|
| 564 | if( /*func == (fptr_t)accept4 || */
|
---|
| 565 | func == (fptr_t)cfa_accept4 )
|
---|
[5751a56] | 566 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_ACCEPT ,
|
---|
| 567 | return IS_DEFINED(CFA_HAVE_IORING_OP_ACCEPT);
|
---|
[4b84e35] | 568 |
|
---|
| 569 | if( /*func == (fptr_t)connect || */
|
---|
| 570 | func == (fptr_t)cfa_connect )
|
---|
[5751a56] | 571 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_CONNECT ,
|
---|
| 572 | return IS_DEFINED(CFA_HAVE_IORING_OP_CONNECT);
|
---|
[4b84e35] | 573 |
|
---|
| 574 | if( /*func == (fptr_t)fallocate || */
|
---|
| 575 | func == (fptr_t)cfa_fallocate )
|
---|
[5751a56] | 576 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FALLOCATE ,
|
---|
| 577 | return IS_DEFINED(CFA_HAVE_IORING_OP_FALLOCATE);
|
---|
[4b84e35] | 578 |
|
---|
| 579 | if( /*func == (fptr_t)posix_fadvise || */
|
---|
| 580 | func == (fptr_t)cfa_fadvise )
|
---|
[5751a56] | 581 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_FADVISE ,
|
---|
| 582 | return IS_DEFINED(CFA_HAVE_IORING_OP_FADVISE);
|
---|
[4b84e35] | 583 |
|
---|
| 584 | if( /*func == (fptr_t)madvise || */
|
---|
| 585 | func == (fptr_t)cfa_madvise )
|
---|
[5751a56] | 586 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_MADVISE ,
|
---|
| 587 | return IS_DEFINED(CFA_HAVE_IORING_OP_MADVISE);
|
---|
[4b84e35] | 588 |
|
---|
| 589 | if( /*func == (fptr_t)openat || */
|
---|
| 590 | func == (fptr_t)cfa_openat )
|
---|
[5751a56] | 591 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_OPENAT ,
|
---|
| 592 | return IS_DEFINED(CFA_HAVE_IORING_OP_OPENAT);
|
---|
[4b84e35] | 593 |
|
---|
| 594 | if( /*func == (fptr_t)close || */
|
---|
| 595 | func == (fptr_t)cfa_close )
|
---|
[5751a56] | 596 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_CLOSE ,
|
---|
| 597 | return IS_DEFINED(CFA_HAVE_IORING_OP_CLOSE);
|
---|
[4b84e35] | 598 |
|
---|
| 599 | if( /*func == (fptr_t)read || */
|
---|
| 600 | func == (fptr_t)cfa_read )
|
---|
[5751a56] | 601 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_READ ,
|
---|
| 602 | return IS_DEFINED(CFA_HAVE_IORING_OP_READ);
|
---|
[4b84e35] | 603 |
|
---|
| 604 | if( /*func == (fptr_t)write || */
|
---|
| 605 | func == (fptr_t)cfa_write )
|
---|
[5751a56] | 606 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_WRITE ,
|
---|
| 607 | return IS_DEFINED(CFA_HAVE_IORING_OP_WRITE);
|
---|
[0a92c78] | 608 |
|
---|
| 609 | if( /*func == (fptr_t)splice || */
|
---|
[f00b26d4] | 610 | func == (fptr_t)cfa_splice )
|
---|
[5751a56] | 611 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_SPLICE ,
|
---|
| 612 | return IS_DEFINED(CFA_HAVE_IORING_OP_SPLICE);
|
---|
[0a92c78] | 613 |
|
---|
| 614 | if( /*func == (fptr_t)tee || */
|
---|
| 615 | func == (fptr_t)cfa_tee )
|
---|
[5751a56] | 616 | #define _CFA_IO_FEATURE_CFA_HAVE_IORING_OP_TEE ,
|
---|
| 617 | return IS_DEFINED(CFA_HAVE_IORING_OP_TEE);
|
---|
[4b84e35] | 618 | #endif
|
---|
| 619 |
|
---|
| 620 | return false;
|
---|
[85eafc5] | 621 | }
|
---|