Repository navigation
fs: Set st_ino in procfs, d_ino in every readdir() and st_dev per mounted volume - #21682
Merged
Merged
Conversation
casaroli
requested review from
Donny9,
GUIDINGLI,
jerpelea,
pkarashchenko,
xiaoxiang781216 and
yamt
as code owners
November 11, 2026 07:43
casaroli
force-pushed
the
fs-inode-numbers-up
branch
from
November 11, 2026 10:22
efe071b to
e0a9e73
Compare
|
xiaoxiang781216
approved these changes
Nov 11, 2026
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
force-pushed
the
fs-inode-numbers-up
branch
from
November 11, 2026 13:13
e0a9e73 to
7dc84bc
Compare
🔗 Cross-repo PR dependenciesThe read-only Build run reported the following dependent PR(s) and fetched head SHA(s): |
acassis
approved these changes
Nov 12, 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.
depends-on: /pull/21729
Summary
POSIX says that
st_devandst_inotogether identify a file, and thatreaddir()gives the file serial number ind_ino. On master procfs givesst_ino0 for every path,readdir()of most mounted file systems returns the old content of the buffer ind_ino(often 0), the files of all volumes havest_dev0, andstat("/")givesst_ino0. Programs that walk a tree (find,tar,cp -r,ftw()) comparest_devandst_inoof a directory with those of its parents: each subdirectory of/proclooked like/proc, andfindsaid "File system loop detected".glob()skips an entry withd_ino0.readdir()setsd_inoto 0 before the file system reads the entry; if the file system leaves it 0,d_inois a hash (FNV-1a) of the path of the entry. The hash isinode_hash()infs/inode/inode.h.stat(),fstat()andreaddir()give the same number.open()keeps it forfstat();opendir()keeps the hash of the directory path, andreaddir()adds the name.st_dev0,stat(),lstat()andfstat()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 havest_ino1; withst_dev0 for both they were one file.stat("/")givesst_ino1. This needs commit 3: else/and the root of the first tmpfs have the samest_devandst_ino.st_dev(special_files_dev_num.rst said that NuttX cannot supportst_dev).Impact
The changes are in common code: flat, protected and kernel builds behave the same on every architecture.
readdir():d_inois never the old content of the buffer. File systems whosereaddir()sets nod_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.st_inoandd_inoare 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_sandstruct procfs_dir_priv_s(include/nuttx/fs/procfs.h) have one more 4-byte field; the handlers outside fs/procfs allocate them withsizeof, 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 keepst_dev0./is 1 (was 0).readdir()of its parent gives the number of the node of the pseudo file system, andstat()gives the root of the volume (anotherst_dev), as on Linux.ftok()(libs/libc/misc/lib_ftok.c, usesst_devandst_ino) and the same-file check ofrename()(fs/vfs/fs_rename.c, file systemstat()without the VFSst_dev, unchanged) read these fields; nothing readsd_ino.ftok()gives other keys for the files of mounted volumes, and different keys for different procfs files (all procfs paths had one key).st_inostays 0: the files of fat, romfs, littlefs, cromfs, binfs, nxffs, smartfs, spiffs, nfs, xipfs and zipfs (they have no numbers;d_inois now the hash, so it differs fromst_inothere); eventfd, signalfd and timerfd (one static node each); sockets. Each of these is a change in its own file system.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,/procand a new tmpfs, and one level into their subdirectories:st_inoandd_inonot 0 and stable;d_ino=st_inooflstat()(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:qemu-armv8a:knsh(kernel, HVF)qemu-armv8a:nsh(flat, HVF)mps2-an500:knsh(protected, armv7-m, TCG)qemu-armv8a:knshandqemu-armv8a:nshThe master failures:
st_ino0 for/and every procfs path,d_ino0 in procfs, the subdirectories of/procwith the numbers of/proc, and the tmpfs root with thest_devandst_inoof 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) andlm3s6965-ek:qemu-protected(protected, armv7-m) withEXTRAFLAGS=-Werror;qemu-armv8a:nshwith CMake (Ninja) and the test, no warning.tools/checkpatch.sh -c -u -m -g origin/master..HEADpasses.