From c69ff543edee85ac79a8ae8cbf61fc8d68d56b91 Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Mon, 1 Jul 2024 12:10:40 +0200 Subject: [PATCH] Revert "posix: device_io: implement pread() and pwrite()" This reverts commit 2d729665162a21cf0bb5e5dc2f6ab36880e09ddd. PR #73978 introduced a regression. Unfortunately this PR cannot be reverted without reverting also Let's revert both PRs to stabilize main again towards the 3.7 release. For more details on the issue see https://github.com/zephyrproject-rtos/zephyr/issues/75205 Signed-off-by: Alberto Escolar Piedras --- lib/os/fdtable.c | 54 +++++++++-------------------------- lib/posix/options/device_io.c | 32 +++------------------ 2 files changed, 17 insertions(+), 69 deletions(-) diff --git a/lib/os/fdtable.c b/lib/os/fdtable.c index 6edf98066e1..f65b7dc1ae2 100644 --- a/lib/os/fdtable.c +++ b/lib/os/fdtable.c @@ -308,21 +308,9 @@ int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable) return fd; } -static bool supports_pread_pwrite(uint32_t mode) +static ssize_t zvfs_rw(int fd, void *buf, size_t sz, bool is_write) { - switch (mode & ZVFS_MODE_IFMT) { - case ZVFS_MODE_IFSHM: - return true; - default: - return false; - } -} - -static ssize_t zvfs_rw(int fd, void *buf, size_t sz, bool is_write, const size_t *from_offset) -{ - bool prw; ssize_t res; - const size_t *off; if (_check_fd(fd) < 0) { return -1; @@ -330,40 +318,24 @@ static ssize_t zvfs_rw(int fd, void *buf, size_t sz, bool is_write, const size_t (void)k_mutex_lock(&fdtable[fd].lock, K_FOREVER); - prw = supports_pread_pwrite(fdtable[fd].mode); - if (from_offset != NULL && !prw) { - /* - * Seekable file types should support pread() / pwrite() and per-fd offset passing. - * Otherwise, it's a bug. - */ - errno = ENOTSUP; - res = -1; - goto unlock; - } - - /* If there is no specified from_offset, then use the current offset of the fd */ - off = (from_offset == NULL) ? &fdtable[fd].offset : from_offset; - if (is_write) { - if (fdtable[fd].vtable->write_offs == NULL) { + if (fdtable[fd].vtable->write_offset == NULL) { res = -1; errno = EIO; } else { - res = fdtable[fd].vtable->write_offs(fdtable[fd].obj, buf, sz, *off); + res = fdtable[fd].vtable->write_offset(fdtable[fd].obj, buf, sz, + fdtable[fd].offset); } } else { - if (fdtable[fd].vtable->read_offs == NULL) { + if (fdtable[fd].vtable->read == NULL) { res = -1; errno = EIO; } else { - res = fdtable[fd].vtable->read_offs(fdtable[fd].obj, buf, sz, *off); + res = fdtable[fd].vtable->read_offset(fdtable[fd].obj, buf, sz, + fdtable[fd].offset); } } - if (res > 0 && prw && from_offset == NULL) { - /* - * only update the fd offset when from_offset is not specified - * See pread() / pwrite() - */ + if (res > 0) { fdtable[fd].offset += res; } @@ -373,14 +345,14 @@ unlock: return res; } -ssize_t zvfs_read(int fd, void *buf, size_t sz, const size_t *from_offset) +ssize_t zvfs_read(int fd, void *buf, size_t sz) { - return zvfs_rw(fd, buf, sz, false, from_offset); + return zvfs_rw(fd, buf, sz, false); } -ssize_t zvfs_write(int fd, const void *buf, size_t sz, const size_t *from_offset) +ssize_t zvfs_write(int fd, const void *buf, size_t sz) { - return zvfs_rw(fd, (void *)buf, sz, true, from_offset); + return zvfs_rw(fd, (void *)buf, sz, true); } int zvfs_close(int fd) @@ -522,7 +494,7 @@ static ssize_t stdinout_read_vmeth(void *obj, void *buffer, size_t count) static ssize_t stdinout_write_vmeth(void *obj, const void *buffer, size_t count) { #if defined(CONFIG_BOARD_NATIVE_POSIX) - return zvfs_write(1, buffer, count, NULL); + return zvfs_write(1, buffer, count); #elif defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_ARCMWDT_LIBC) return z_impl_zephyr_write_stdout(buffer, count); #else diff --git a/lib/posix/options/device_io.c b/lib/posix/options/device_io.c index e2c7510d7ad..585dc7000c2 100644 --- a/lib/posix/options/device_io.c +++ b/lib/posix/options/device_io.c @@ -15,8 +15,8 @@ /* prototypes for external, not-yet-public, functions in fdtable.c or fs.c */ int zvfs_close(int fd); int zvfs_open(const char *name, int flags); -ssize_t zvfs_read(int fd, void *buf, size_t sz, size_t *from_offset); -ssize_t zvfs_write(int fd, const void *buf, size_t sz, size_t *from_offset); +ssize_t zvfs_read(int fd, void *buf, size_t sz); +ssize_t zvfs_write(int fd, const void *buf, size_t sz); int close(int fd) { @@ -41,33 +41,9 @@ int poll(struct pollfd *fds, int nfds, int timeout) return zsock_poll(fds, nfds, timeout); } -ssize_t pread(int fd, void *buf, size_t count, off_t offset) -{ - size_t off = (size_t)offset; - - if (offset < 0) { - errno = EINVAL; - return -1; - } - - return zvfs_read(fd, buf, count, (size_t *)&off); -} - -ssize_t pwrite(int fd, void *buf, size_t count, off_t offset) -{ - size_t off = (size_t)offset; - - if (offset < 0) { - errno = EINVAL; - return -1; - } - - return zvfs_write(fd, buf, count, (size_t *)&off); -} - ssize_t read(int fd, void *buf, size_t sz) { - return zvfs_read(fd, buf, sz, NULL); + return zvfs_read(fd, buf, sz); } #ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_READ FUNC_ALIAS(read, _read, ssize_t); @@ -81,7 +57,7 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc ssize_t write(int fd, const void *buf, size_t sz) { - return zvfs_write(fd, buf, sz, NULL); + return zvfs_write(fd, buf, sz); } #ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_WRITE FUNC_ALIAS(write, _write, ssize_t);