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