casaroli opened a new pull request, #21682:
URL: https://github.com/apache/nuttx/pull/21682

   ## 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#NNNN (`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 03aea786390 | 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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to