Repository navigation
fs/vfs: Fail lseek() of a pipe with ESPIPE and open() of a directory for writing. - #21143
Merged
Merged
Conversation
casaroli
requested review from
Donny9,
anchao,
hartmannathan,
jerpelea and
xiaoxiang781216
as code owners
October 29, 2026 21:14
|
casaroli
force-pushed
the
g8-vfs-errno-up
branch
from
October 30, 2026 08:54
95da3bd to
e7f7b0f
Compare
xiaoxiang781216
previously approved these changes
Oct 30, 2026
open() with O_DIRECTORY opened the directory whatever the access mode. O_TMPFILE includes O_DIRECTORY, so open(dir, O_RDWR | O_TMPFILE) gave a directory descriptor instead of an unnamed file: open() returned success and every write() to it failed. A program that uses mkstemp() only when O_TMPFILE fails then could not write its temporary file. Now an O_DIRECTORY open with write access does not open the directory. It checks the type of the path (the stat method of a mounted file system, the inode type in the pseudo file system) and fails: with ENOTDIR for a path that is not a directory, else with EOPNOTSUPP for O_TMPFILE, as Linux does for a file system without it (no file system makes an unnamed file), else with EISDIR, as POSIX specifies for a directory opened with O_WRONLY or O_RDWR. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
nxstyle reports "Missing blank line after declarations" twice in fs_sendfile.c. The errors are older than the next change, which edits this file. Whitespace only. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
The AIO workers read and wrote with file_pread() and file_pwrite() at aio_offset. For a file that cannot seek, a pipe, a FIFO or a socket, POSIX ignores aio_offset. file_pread() and file_pwrite() seek, and once lseek() of a pipe fails with ESPIPE, the request would fail at once instead of reading or writing. Add INODE_IS_NOSEEK() for the inodes that have no file position: pipes, FIFOs and sockets. The AIO workers read and write these with file_read() and file_write() at the current position. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
The pipe, FIFO and socket drivers have no seek method, so lseek() set the common file position and returned it: lseek(fd, 10, SEEK_CUR) of a pipe returned 10, and SEEK_END failed with ENOSYS. POSIX specifies ESPIPE for a pipe, a FIFO or a socket. A program that skips data in a stream with lseek() and reads it only when lseek() fails took the skip for done and lost the data. Return -ESPIPE for these inodes (INODE_IS_NOSEEK()) before the seek method or the common file position. pread(), pwrite() and sendfile() with an offset then also fail with ESPIPE for them, as POSIX and Linux specify. The sendfile() path of a TCP socket (CONFIG_NET_SENDFILE) seeks its input to send data again. For an input that cannot seek, sendfile() does not use that path: it uses the read and write copy. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
stm32f334-disco:buckboost and nucleo-f334r8:highpri fill their 64 KiB
flash: buckboost uses 65496 bytes on master with GCC 13.2, and the clang
build of both is larger. A small change in common code, such as the
ESPIPE and O_DIRECTORY checks of the previous commits, does not fit any
more ("region `flash' overflowed by 16 bytes" with clang in CI).
Disable CONFIG_ARCH_STACKDUMP in these two application configurations.
It frees about 2.2 KiB in each. The nsh configurations of both boards
keep the stack dump.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
xiaoxiang781216
approved these changes
Oct 31, 2026
casaroli
force-pushed
the
g8-vfs-errno-up
branch
from
October 31, 2026 09:10
1152ba7 to
0251a90
Compare
acassis
approved these changes
Oct 31, 2026
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two results of the VFS that POSIX specifies and that programs use to choose a fallback:
open()withO_TMPFILEor withO_DIRECTORYand write access, andlseek()of a pipe, a FIFO or a socket.open()withO_DIRECTORYopened the directory whatever the access mode.O_TMPFILEincludesO_DIRECTORY, soopen(dir, O_RDWR | O_TMPFILE)gave a directory descriptor instead of an unnamed file:open()returned success and everywrite()to it failed. A program that usesmkstemp()only whenO_TMPFILEfails could not write its temporary file. Now anO_DIRECTORYopen with write access does not open the directory: it checks the type of the path (thestatmethod of a mounted file system, the inode type in the pseudo file system) and fails withENOTDIRfor a path that is not a directory, else withEOPNOTSUPPforO_TMPFILE, as Linux does for a file system without it (no NuttX file system makes an unnamed file), else withEISDIR, as POSIX specifies for a directory opened withO_WRONLYorO_RDWR.file_pread()andfile_pwrite()ataio_offset. These seek, so after the last commit they would fail withESPIPEfor a pipe. POSIX ignoresaio_offsetfor a file that cannot seek. A newINODE_IS_NOSEEK()(pipes, FIFOs and sockets) selectsfile_read()andfile_write()at the current position for them. This commit comes first so that no commit breaks AIO on a pipe.lseek()set the common file position and returned it:lseek(fd, 10, SEEK_CUR)of a pipe returned 10, andSEEK_ENDfailed withENOSYS. POSIX specifiesESPIPE. A program that skips data withlseek()and reads only whenlseek()fails lost the data.file_seek()now returns-ESPIPEfor these inodes (INODE_IS_NOSEEK()); other drivers without a seek method keep the common file position.pread(),pwrite()andsendfile()with an offset then also fail withESPIPEfor them, as POSIX and Linux specify. Thesendfile()path of a TCP socket (CONFIG_NET_SENDFILE) seeks its input to send data again: for an input that cannot seek,sendfile()does not use that path and uses the read and write copy.region `flash' overflowed by 16 bytesin the clang build of CI; buckboost with GCC 13.2: 65496 bytes on master, 72 bytes over with this PR).CONFIG_ARCH_STACKDUMPoff frees about 2.2 KiB in each (buckboost GCC: 63384 bytes, highpri GCC: 47596 bytes). The nsh configurations of both boards keep the stack dump.Impact
Possible breaking change (results and
errnovalues of the flat API too, every build mode and architecture):open(dir, O_RDWR | O_TMPFILE)EOPNOTSUPPopen(dir, O_WRONLY or O_RDWR | O_DIRECTORY)EISDIRopen("/dev/null", O_WRONLY | O_DIRECTORY)ENOTDIRlseek()of a pipe, FIFO or socketSEEK_END:ENOSYS)ESPIPEpread()/pwrite()of a pipe, FIFO or socketESPIPEftell()of a stream on a pipeESPIPEsendfile()from a pipe with an offsetESPIPEO_RDONLY | O_DIRECTORY(opendir()) does not change; reads and writes of pipes, FIFOs and sockets do not change;sendfile()from a pipe without an offset still works (also to a TCP socket withCONFIG_NET_SENDFILE).In-tree users, nuttx and nuttx-apps master: no code uses
O_TMPFILE, and everyO_DIRECTORYopen is read-only. Of thelseek(),fseek(),ftell(),pread(),pwrite()andfile_seek()callers, these can see a pipe, a FIFO or a socket:fs/aioand the TCPsendfile(): changed in this PR (see above).fcntl(F_SETFL)withO_APPENDseeks to the end: on a pipe it failed before (ENOSYS) and fails now (ESPIPE).fwrite()after buffered reads andfseek()seek back over the read-ahead; on a pipe this failed before (EINVAL: the position became negative) and fails now (ESPIPE).dd skip=Nwith a pipe as input: before,ddcopied from the start without skipping; now it stops with "failed to lseek".while/until) in a script that is a FIFO: never worked; now NSH prints anlseekerror for each line.All other callers seek regular files or block and character devices, which do not change.
Testing
Tested on master 3122150 and on this PR rebased on it, with QEMU (HVF):
qemu-armv8a:knsh(kernel build, programs on v9fs) andqemu-armv8a:nsh(flat build, the programs as NSH built-ins), withCONFIG_FS_TMPFSandCONFIG_PIPES. The flat build also hasCONFIG_FS_AIOand TCP over the loopback device withCONFIG_NET_SENDFILE, for the socket cases.tmpfile.c:open()of a tmpfs and of a v9fs directory (the same result on both):open(dir, O_RDWR | O_TMPFILE | O_EXCL, 0600)write()fails withEINVAL: FAILEOPNOTSUPP: PASSopen(dir, O_RDWR | O_DIRECTORY)EISDIR: PASSopen(dir, O_RDONLY | O_DIRECTORY)pipeseek.c:lseek(read end, 10, SEEK_CUR)ESPIPE: PASSlseek(write end, 0, SEEK_END)ENOSYS: FAILESPIPE: PASSlseek()lseek()of a FIFOESPIPE: PASSlseek()of a socket (flat build)ESPIPE: PASSg8extra.c, more cases:open(regular file, O_RDWR | O_DIRECTORY)ENOTDIRENOTDIRopen(regular file, O_RDWR | O_TMPFILE)ENOTDIRENOTDIRopen("/dev", O_WRONLY | O_DIRECTORY)EISDIRopen("/dev/null", O_WRONLY | O_DIRECTORY)ENOTDIRopen(tmpfs mount point, O_WRONLY | O_DIRECTORY)EISDIRopen(tmpfs dir, O_RDWR | O_DIRECTORY)EISDIRopen(missing path, O_RDWR | O_DIRECTORY)ENOENTENOENTumount()umount()0umount()0 (no reference leaked)pread()/pwrite()of a pipeESPIPEftell()of a stream on a pipeESPIPEsendfile(file, pipe, NULL, 5)sendfile(file, pipe, &off, 5)ESPIPEsendfile(TCP socket, pipe, NULL, 5)(flat build)aiotest.c(flat build; AIO writes and reads,lio_listio(), and requests on a pipe): the same result on master and with this PR (3 of 6 cases pass on both; the other three fail with the same values on master).ostest exits with status 0 on
qemu-armv8a:knshandqemu-armv8a:nsh, on master and with this PR.Builds, each OK and without new warnings:
lm3s6965-ek:nsh(flat, Cortex-M3) withCONFIG_FS_AIO,CONFIG_NET_SENDFILE,CONFIG_PIPESandCONFIG_FS_TMPFS;lm3s6965-ek:qemu-protected(protected, Cortex-M3);lm3s6965-ek:qemu-kostest(protected,CONFIG_DISABLE_MOUNTPOINT);rv-virt:pnsh64(protected, RISC-V) andrv-virt:knsh64(kernel, RISC-V) withCONFIG_PIPESandCONFIG_FS_TMPFS.tools/checkpatch.sh -c -u -m -gpasses.