Skip to content

fs: Set st_ino in procfs, d_ino in every readdir() and st_dev per mounted volume - #21682

Merged
acassis merged 5 commits into
apache:masterfrom
casaroli:fs-inode-numbers-up
Nov 12, 2026
Merged

acassis merged 5 commits into
apache:masterfrom
casaroli:fs-inode-numbers-up

Conversation

@casaroli

@casaroli casaroli commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

depends-on: /pull/21729

Summary

POSIX says that st_dev and st_ino together identify a file, and that readdir() gives the file serial number in d_ino. On master procfs gives st_ino 0 for every path, readdir() of most mounted file systems returns the old content of the buffer in d_ino (often 0), the files of all volumes have st_dev 0, and stat("/") gives st_ino 0. Programs that walk a tree (find, tar, cp -r, ftw()) compare st_dev and st_ino of a directory with those of its parents: each subdirectory of /proc looked like /proc, and find said "File system loop detected". glob() skips an entry with d_ino 0.

  • Commit 1 (fs/vfs): readdir() sets d_ino to 0 before the file system reads the entry; if the file system leaves it 0, d_ino is a hash (FNV-1a) of the path of the entry. The hash is inode_hash() in fs/inode/inode.h.
  • Commit 2 (fs/procfs): the file serial number of a procfs path is the hash of the path in procfs. stat(), fstat() and readdir() give the same number. open() keeps it for fstat(); opendir() keeps the hash of the directory path, and readdir() adds the name.
  • Commit 3 (fs/vfs): if the file system gives st_dev 0, stat(), lstat() and fstat() give the file serial number of the mount point. The root of the first tmpfs and the second node of the pseudo file system both have st_ino 1; with st_dev 0 for both they were one file.
  • Commit 4 (fs/inode): the nodes of the pseudo file system are numbered from 1, so stat("/") gives st_ino 1. This needs commit 3: else / and the root of the first tmpfs have the same st_dev and st_ino.
  • Commit 5: Documentation of the procfs numbers and of st_dev (special_files_dev_num.rst said that NuttX cannot support st_dev).

Impact

The changes are in common code: flat, protected and kernel builds behave the same on every architecture.

  • readdir(): d_ino is never the old content of the buffer. File systems whose readdir() sets no d_ino (fat, romfs, littlefs, cromfs, binfs, nxffs, smartfs, spiffs, nfs, xipfs, zipfs, mnemofs, and unionfs or userfs over them) now give a hash of the path. The pseudo file system, tmpfs, v9fs, hostfs and rpmsgfs set their own numbers and do not change.
  • procfs: st_ino and d_ino are the hash of the path (was 0). /proc/<pid> of a new task with an old PID has the number of the old one. struct procfs_file_s and struct procfs_dir_priv_s (include/nuttx/fs/procfs.h) have one more 4-byte field; the handlers outside fs/procfs allocate them with sizeof, so they need no change.
  • st_dev: the files of a mounted volume get the file serial number of the mount point (was 0). hostfs and rpmsgfs keep the device number that the host gives. The files of the pseudo file system keep st_dev 0.
  • Pseudo file system: every node number is 1 more than before; / is 1 (was 0).
  • A mount point: readdir() of its parent gives the number of the node of the pseudo file system, and stat() gives the root of the volume (another st_dev), as on Linux.
  • Users in nuttx and nuttx-apps: only ftok() (libs/libc/misc/lib_ftok.c, uses st_dev and st_ino) and the same-file check of rename() (fs/vfs/fs_rename.c, file system stat() without the VFS st_dev, unchanged) read these fields; nothing reads d_ino. ftok() gives other keys for the files of mounted volumes, and different keys for different procfs files (all procfs paths had one key).
  • Where st_ino stays 0: the files of fat, romfs, littlefs, cromfs, binfs, nxffs, smartfs, spiffs, nfs, xipfs and zipfs (they have no numbers; d_ino is now the hash, so it differs from st_ino there); eventfd, signalfd and timerfd (one static node each); sockets. Each of these is a change in its own file system.
  • Size (lm3s6965-ek:qemu-protected, with procfs, -Os): text 127652 -> 127892 (+240): fs_dir.o +78, fs_stat.o +8, fs_fstat.o +8, fs_procfs.o +130. rv-virt:nsh (rv32) with procfs and tmpfs: text 242554 -> 242782 (+228).

Testing

Test: apache/nuttx-apps#4023 (testing/fs/inotest): for /, /dev, /proc and a new tmpfs, and one level into their subdirectories: st_ino and d_ino not 0 and stable; d_ino = st_ino of lstat() (not for mount points); fstat() = lstat(); two entries are two files; a subdirectory does not have the numbers of a parent; a renamed file keeps its number. QEMU, 1 CPU, with tmpfs:

Configuration master 03aea78 this PR
qemu-armv8a:knsh (kernel, HVF) pass 901, fail 186 pass 475, fail 0
qemu-armv8a:nsh (flat, HVF) pass 419, fail 192 pass 473, fail 0
mps2-an500:knsh (protected, armv7-m, TCG) pass 355, fail 159 pass 405, fail 0
ostest, qemu-armv8a:knsh and qemu-armv8a:nsh status 0 status 0

The master failures: st_ino 0 for / and every procfs path, d_ino 0 in procfs, the subdirectories of /proc with the numbers of /proc, and the tmpfs root with the st_dev and st_ino of a pseudo node. The same test passes on Linux (glibc 2.43).

Builds, each OK: rv-virt:nsh (flat, rv32, with procfs, tmpfs and the test) and lm3s6965-ek:qemu-protected (protected, armv7-m) with EXTRAFLAGS=-Werror; qemu-armv8a:nsh with CMake (Ninja) and the test, no warning.

tools/checkpatch.sh -c -u -m -g origin/master..HEAD passes.

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels Nov 11, 2026
Comment thread fs/procfs/fs_procfs.c Resolved
@casaroli
casaroli force-pushed the fs-inode-numbers-up branch from efe071b to e0a9e73 Compare November 11, 2026 10:22
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text -6 B (-0.0%, 66,981 B / 262,144 B, total: 26% used)

esp32-devkitc

  • ROM: .flash.text +220 B (+0.2%, 130,436 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +220 B (+0.2%, 96,372 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +124 B (+0.1%, 86,592 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .text.dir_read +112 B, .text.file_fstat +16 B, .text.nx_stat +16 B, .text.procfs_enum -4 B, .text.procfs_fstat +8 B, .text.procfs_open +20 B, .text.procfs_opendir +52 B, .text.procfs_readdir +24 B, .text.procfs_stat +32 B, .text.uptime_dup +8 B (+0.1%, 364,496 B)

qemu-intel64

  • Code: .text +268 B (+0.0%, 8,696,756 B)

rx65n-rsk2mb

  • ROM: .text +80 B (+0.1%, 92,140 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +784 B (+0.2%, 391,920 B)

No memory changes detected for:


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

Most mounted file systems (fat, romfs, littlefs, procfs and others) do
not set d_ino in readdir().  readdir() then returned the old content of
the buffer of the caller, which opendir() allocates with malloc().
Programs take an entry with d_ino 0 as a deleted entry: glob() of a
shell found no file in such a directory.

Set d_ino to 0 before the file system reads the entry.  If the file
system leaves it 0, give a hash (FNV-1a) of the path of the entry.
inode_hash() in fs/inode/inode.h computes the hash, so that a file
system can use the same hash for its own numbers.  The pseudo file
system, tmpfs, v9fs, hostfs and rpmsgfs set their own numbers and do not
change.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
stat() and fstat() in procfs gave st_ino 0 for every path, and readdir()
left d_ino alone.  A program that walks a tree compares the device and
the serial number of a directory with those of its parent directories.
Each subdirectory of /proc then looked like /proc: find said "File
system loop detected" and did not go into it.

Make the number the hash of the path in procfs (inode_hash()).  stat()
computes it from the path, and open() keeps it in struct procfs_file_s
for fstat(); the dup() of each handler copies that structure.  opendir()
keeps the hash of the directory path in struct procfs_dir_priv_s, and
readdir() adds the name of the entry to it.  So readdir() gives in
d_ino the same number that stat() gives in st_ino.

Each open file and each open directory of procfs takes 4 bytes more.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
POSIX says that st_dev and st_ino together identify a file.  st_dev was
0 for every file, but each file system counts st_ino on its own: the
root of the first tmpfs and the second node of the pseudo file system
both have the serial number 1.  A program that compares st_dev and
st_ino (find, tar, cp -r, or ftw() of another libc) took them for one
file.

If the file system gives st_dev 0, stat(), lstat() and fstat() now give
the serial number of the mount point.  The pseudo file system keeps
st_dev 0.  hostfs and rpmsgfs keep the device number that the host
gives.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
The root node of the pseudo file system is the first node, and it got
the serial number 0: stat("/") gave st_ino 0.  Some programs take a
serial number 0 as "no number".  Give the first node the number 1.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
procfs.rst: each path of procfs has a file serial number, the same in
stat(), fstat() and readdir().

special_files_dev_num.rst said that NuttX cannot support st_dev.  The
files of a mounted volume now have a device number; only the files of
the pseudo-file system keep st_dev 0.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@casaroli
casaroli force-pushed the fs-inode-numbers-up branch from e0a9e73 to 7dc84bc Compare November 11, 2026 13:13
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/43027249137

@acassis
acassis merged commit 3549d76 into apache:master Nov 12, 2026
87 of 93 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation 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