Skip to content

fs/vfs: Fail lseek() of a pipe with ESPIPE and open() of a directory for writing. - #21143

Merged
xiaoxiang781216 merged 5 commits into
apache:masterfrom
casaroli:g8-vfs-errno-up
Oct 31, 2026
Merged

xiaoxiang781216 merged 5 commits into
apache:masterfrom
casaroli:g8-vfs-errno-up

Conversation

@casaroli

@casaroli casaroli commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes two results of the VFS that POSIX specifies and that programs use to choose a fallback: open() with O_TMPFILE or with O_DIRECTORY and write access, and lseek() of a pipe, a FIFO or a socket.

  • fs/vfs: Fail an open of a directory for writing or with O_TMPFILE. 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 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 NuttX file system makes an unnamed file), else with EISDIR, as POSIX specifies for a directory opened with O_WRONLY or O_RDWR.
  • fs/vfs: Add the blank lines after declarations in fs_sendfile.c. Whitespace only: two older nxstyle errors in a file that the last commit edits.
  • fs/aio: Read and write a file that cannot seek at its position. The AIO workers used file_pread() and file_pwrite() at aio_offset. These seek, so after the last commit they would fail with ESPIPE for a pipe. POSIX ignores aio_offset for a file that cannot seek. A new INODE_IS_NOSEEK() (pipes, FIFOs and sockets) selects file_read() and file_write() at the current position for them. This commit comes first so that no commit breaks AIO on a pipe.
  • fs/vfs: Fail lseek() of a pipe, a FIFO or a socket with ESPIPE. These 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. A program that skips data with lseek() and reads only when lseek() fails lost the data. file_seek() now returns -ESPIPE for these inodes (INODE_IS_NOSEEK()); other drivers without a seek method keep 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 and uses the read and write copy.
  • boards/stm32f3: Disable the stack dump in buckboost and highpri. stm32f334-disco:buckboost and nucleo-f334r8:highpri fill their 64 KiB flash, and the checks above no longer fit (region `flash' overflowed by 16 bytes in the clang build of CI; buckboost with GCC 13.2: 65496 bytes on master, 72 bytes over with this PR). CONFIG_ARCH_STACKDUMP off 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 errno values of the flat API too, every build mode and architecture):

Call before after
open(dir, O_RDWR | O_TMPFILE) a directory descriptor that cannot be written -1, EOPNOTSUPP
open(dir, O_WRONLY or O_RDWR | O_DIRECTORY) a directory descriptor -1, EISDIR
open("/dev/null", O_WRONLY | O_DIRECTORY) a descriptor -1, ENOTDIR
lseek() of a pipe, FIFO or socket a made-up position (SEEK_END: ENOSYS) -1, ESPIPE
pread()/pwrite() of a pipe, FIFO or socket read or write, the offset ignored -1, ESPIPE
ftell() of a stream on a pipe a made-up position -1, ESPIPE
sendfile() from a pipe with an offset the offset ignored -1, ESPIPE

O_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 with CONFIG_NET_SENDFILE).

In-tree users, nuttx and nuttx-apps master: no code uses O_TMPFILE, and every O_DIRECTORY open is read-only. Of the lseek(), fseek(), ftell(), pread(), pwrite() and file_seek() callers, these can see a pipe, a FIFO or a socket:

  • fs/aio and the TCP sendfile(): changed in this PR (see above).
  • fcntl(F_SETFL) with O_APPEND seeks to the end: on a pipe it failed before (ENOSYS) and fails now (ESPIPE).
  • stdio: fwrite() after buffered reads and fseek() seek back over the read-ahead; on a pipe this failed before (EINVAL: the position became negative) and fails now (ESPIPE).
  • dd skip=N with a pipe as input: before, dd copied from the start without skipping; now it stops with "failed to lseek".
  • NSH loops (while/until) in a script that is a FIFO: never worked; now NSH prints an lseek error 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) and qemu-armv8a:nsh (flat build, the programs as NSH built-ins), with CONFIG_FS_TMPFS and CONFIG_PIPES. The flat build also has CONFIG_FS_AIO and TCP over the loopback device with CONFIG_NET_SENDFILE, for the socket cases.

tmpfile.c: open() of a tmpfs and of a v9fs directory (the same result on both):

Case master this PR
open(dir, O_RDWR | O_TMPFILE | O_EXCL, 0600) a descriptor; write() fails with EINVAL: FAIL -1, EOPNOTSUPP: PASS
open(dir, O_RDWR | O_DIRECTORY) a descriptor: FAIL -1, EISDIR: PASS
open(dir, O_RDONLY | O_DIRECTORY) a descriptor: PASS a descriptor: PASS

pipeseek.c:

Case master this PR
lseek(read end, 10, SEEK_CUR) 10: FAIL -1, ESPIPE: PASS
lseek(write end, 0, SEEK_END) -1, ENOSYS: FAIL -1, ESPIPE: PASS
the 16 bytes in the pipe are read after the lseek() PASS PASS
lseek() of a FIFO 0: FAIL -1, ESPIPE: PASS
lseek() of a socket (flat build) 0: FAIL -1, ESPIPE: PASS

g8extra.c, more cases:

Case master this PR
open(regular file, O_RDWR | O_DIRECTORY) -1, ENOTDIR -1, ENOTDIR
open(regular file, O_RDWR | O_TMPFILE) -1, ENOTDIR -1, ENOTDIR
open("/dev", O_WRONLY | O_DIRECTORY) a descriptor -1, EISDIR
open("/dev/null", O_WRONLY | O_DIRECTORY) a descriptor -1, ENOTDIR
open(tmpfs mount point, O_WRONLY | O_DIRECTORY) a descriptor -1, EISDIR
open(tmpfs dir, O_RDWR | O_DIRECTORY) a descriptor -1, EISDIR
open(missing path, O_RDWR | O_DIRECTORY) -1, ENOENT -1, ENOENT
200 failed directory opens on a tmpfs, then umount() the opens succeed; umount() 0 the opens fail; umount() 0 (no reference leaked)
pread() / pwrite() of a pipe 5 / 1 -1, ESPIPE
ftell() of a stream on a pipe 0 -1, ESPIPE
sendfile(file, pipe, NULL, 5) 5 5
sendfile(file, pipe, &off, 5) 5 -1, ESPIPE
sendfile(TCP socket, pipe, NULL, 5) (flat build) 5 5 (the read and write copy)

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:knsh and qemu-armv8a:nsh, on master and with this PR.

Builds, each OK and without new warnings: lm3s6965-ek:nsh (flat, Cortex-M3) with CONFIG_FS_AIO, CONFIG_NET_SENDFILE, CONFIG_PIPES and CONFIG_FS_TMPFS; lm3s6965-ek:qemu-protected (protected, Cortex-M3); lm3s6965-ek:qemu-kostest (protected, CONFIG_DISABLE_MOUNTPOINT); rv-virt:pnsh64 (protected, RISC-V) and rv-virt:knsh64 (kernel, RISC-V) with CONFIG_PIPES and CONFIG_FS_TMPFS.

tools/checkpatch.sh -c -u -m -g passes.

@github-actions github-actions Bot added Area: File System File System issues Size: S The size of the change in this PR is small labels Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +26 B (+0.0%, 65,412 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.text +136 B (+0.1%, 128,204 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +136 B (+0.1%, 94,076 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +112 B (+0.1%, 84,920 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +28 B (+0.0%, 67,296 B / 131,072 B, total: 51% used)

qemu-armv8a

  • Code: .text.file_seek +28 B, .text.file_vopen +132 B (+0.0%, 354,304 B)

qemu-intel64

  • Code: .text +183 B (+0.0%, 8,687,724 B)

rx65n-rsk2mb

  • ROM: .text +112 B (+0.1%, 90,168 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +480 B (+0.1%, 384,224 B)

stm32-nucleo-f103rb

  • flash: .text +84 B (+0.2%, 34,248 B / 131,072 B, total: 26% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

Comment thread fs/vfs/fs_open.c Resolved
Comment thread fs/aio/aio_write.c Resolved
Comment thread include/nuttx/fs/fs.h Resolved
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: S The size of the change in this PR is small labels 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
xiaoxiang781216 merged commit aed8c30 into apache:master Oct 31, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues Board: arm Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants