Revert "posix: device_io: implement pread() and pwrite()"

This reverts commit 2d72966516.

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 <alberto.escolar.piedras@nordicsemi.no>
This commit is contained in:
Alberto Escolar Piedras 2024-07-01 12:10:40 +02:00 committed by Anas Nashif
parent 1df86af309
commit c69ff543ed
2 changed files with 17 additions and 69 deletions

View file

@ -308,21 +308,9 @@ int zvfs_alloc_fd(void *obj, const struct fd_op_vtable *vtable)
return fd; 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; ssize_t res;
const size_t *off;
if (_check_fd(fd) < 0) { if (_check_fd(fd) < 0) {
return -1; 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); (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 (is_write) {
if (fdtable[fd].vtable->write_offs == NULL) { if (fdtable[fd].vtable->write_offset == NULL) {
res = -1; res = -1;
errno = EIO; errno = EIO;
} else { } 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 { } else {
if (fdtable[fd].vtable->read_offs == NULL) { if (fdtable[fd].vtable->read == NULL) {
res = -1; res = -1;
errno = EIO; errno = EIO;
} else { } 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) { if (res > 0) {
/*
* only update the fd offset when from_offset is not specified
* See pread() / pwrite()
*/
fdtable[fd].offset += res; fdtable[fd].offset += res;
} }
@ -373,14 +345,14 @@ unlock:
return res; 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) 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) static ssize_t stdinout_write_vmeth(void *obj, const void *buffer, size_t count)
{ {
#if defined(CONFIG_BOARD_NATIVE_POSIX) #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) #elif defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_ARCMWDT_LIBC)
return z_impl_zephyr_write_stdout(buffer, count); return z_impl_zephyr_write_stdout(buffer, count);
#else #else

View file

@ -15,8 +15,8 @@
/* prototypes for external, not-yet-public, functions in fdtable.c or fs.c */ /* prototypes for external, not-yet-public, functions in fdtable.c or fs.c */
int zvfs_close(int fd); int zvfs_close(int fd);
int zvfs_open(const char *name, int flags); 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_read(int fd, void *buf, size_t sz);
ssize_t zvfs_write(int fd, const void *buf, size_t sz, size_t *from_offset); ssize_t zvfs_write(int fd, const void *buf, size_t sz);
int close(int fd) int close(int fd)
{ {
@ -41,33 +41,9 @@ int poll(struct pollfd *fds, int nfds, int timeout)
return zsock_poll(fds, nfds, 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) 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 #ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_READ
FUNC_ALIAS(read, _read, ssize_t); 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) 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 #ifdef CONFIG_POSIX_DEVICE_IO_ALIAS_WRITE
FUNC_ALIAS(write, _write, ssize_t); FUNC_ALIAS(write, _write, ssize_t);