Swatantra-66 opened a new pull request, #20820:
URL: https://github.com/apache/nuttx/pull/20820

   ## Summary
   
   Ref: #20414
   
   Adds `CONFIG_FS_DIRENT_LITE`, a new Kconfig option for flat-build 
configurations that provides a lightweight direct-pointer directory stream API, 
eliminating file descriptor allocation and `file` proxy structure overhead for 
directory streams.
   
   ### Architecture & Motivation
   In standard configurations, `opendir()` allocates a file descriptor and 
binds a `struct file` proxy to `g_dir_inode`. On small embedded 
microcontrollers with constrained memory or descriptor tables, this imposes 
noticeable overhead:
   1. Allocating an entry in the file descriptor table.
   2. Holding descriptor slots that count against process limits.
   3. Multiple layers of indirect traversal through the VFS descriptor table on 
every directory operation.
   
   With `CONFIG_FS_DIRENT_LITE`:
   - Directory streams (`DIR`) store a lightweight `FAR struct fs_dirent_s *` 
pointer directly.
   - Operations dispatch to dedicated kernel routines: `nx_opendir()`, 
`nx_readdir()`, `nx_closedir()`, `nx_rewinddir()`, `nx_seekdir()`, and 
`nx_telldir()`.
   - Standard POSIX wrappers (`opendir`, `readdir`, `closedir`, `rewinddir`, 
`seekdir`, `telldir`, `dirfd`, `fdopendir`) transparently support the 
lightweight mode.
   
   ### Key Implementation Details & CodeRabbit Fixes Addressed
   - **Deadlock prevention in `nx_rewinddir`:** Inode references are taken 
prior to unlocking, and previous inode release (`inode_release`) occurs outside 
`inode_lock()` to prevent recursion deadlocks during inode destruction.
   - **Memory safety on mountpoints:** Eliminated duplicate `fs_heap_free(dir)` 
in `nx_closedir()`, properly delegating lifecycle management to the 
filesystem's `closedir` driver hook.
   - **Use-after-free & dangling pointer fixes:** In `nx_closedir()`, `fd_path` 
is cleared before filesystem close and freed after close notifications.
   - **Path correctness for `fs_notify`:** Absolute paths are resolved via 
`inode_getpath()` during `nx_opendir()` so notifications remain valid across 
working directory changes (`chdir`).
   - **Signal safety in inotify:** `notify_read2()` checks 
`TCB_FLAG_SIGNAL_ACTION` and masks with `notify_check_mask(IN_ACCESS)` prior to 
taking `g_inotify.lock`, avoiding self-deadlock when `readdir()` is invoked 
from signal handlers.
   - **Input validation & POSIX error contracts:** `nx_opendir()` rejects 
`NULL` path or dir pointers with `-EINVAL` prior to search setup.
   - **Hard-link resolution:** Under `CONFIG_FS_LINKS`, hard links pointing to 
pseudo-directories are dereferenced (`INODE_IS_HARDLINK`) so child entries of 
target directories are enumerated correctly.
   - **Kconfig documentation:** Clear documentation in `fs/Kconfig` that 
`seekdir`/`telldir` set `errno = ENOSYS`, `open(..., O_DIRECTORY)` returns 
`-ENOTSUP`, directory descriptors are not reclaimed automatically on task exit 
without `closedir()`, and descriptor-based APIs (`dirfd`, `fdopendir`) return 
`-ENOTSUP`.
   
   ---
   
   ## Impact
   
   - **Is new feature added?** Yes (`CONFIG_FS_DIRENT_LITE`).
   - **User Impact:** None when disabled (default `n`). When enabled on flat 
builds, reduces memory footprint and descriptor usage for directory operations.
   - **Build Process:** No impact. Compatible with both CMake/Ninja and 
Makefile build systems.
   - **Hardware:** Targets resource-constrained MCUs in flat builds 
(`BUILD_FLAT`).
   - **Compatibility:**
     - Standard directory iteration (`opendir`, `readdir`, `closedir`, 
`rewinddir`) works seamlessly.
     - Descriptor-specific APIs (`dirfd()`, `fdopendir()`, `open(O_DIRECTORY)`) 
return `-ENOTSUP`.
     - `seekdir()` and `telldir()` set `errno = ENOSYS`.
     - Directory streams must be explicitly closed with `closedir()` (not 
auto-reclaimed on task termination).
   
   ---
   
   ## Testing
   
   ### Host Environment
   - **Host OS:** Linux / WSL2 (Ubuntu, x86_64)
   - **Compiler:** GCC 15.2.0 (`/usr/bin/gcc`)
   - **Build System:** CMake 4.2.3 + Ninja 1.13.2
   
   ### Static Analysis & Coding Standards
   - Verified all 17 modified C and header files with official Apache NuttX 
coding standard linter (`nxstyle`):
     ```bash
     tools/nxstyle fs/vfs/fs_dir.c
     tools/nxstyle libs/libc/dirent/*.c
     # Result: 0 errors, 0 warnings
     ```
   
   ### Build Verification Logs
   
   1. **Standard Build (`sim:nsh`, non-lite default)**:
      ```text
      cmake -B /var/tmp/nuttx-build -DBOARD_CONFIG=sim:nsh -GNinja 
/path/to/nuttx
      ninja -C /var/tmp/nuttx-build nuttx
      [1260/1260] Linking C executable nuttx
      Result: SUCCESS (0 errors, 0 warnings)
      ```
   
   2. **Lite Build (`sim:nsh` with `CONFIG_FS_DIRENT_LITE=y`)**:
      ```text
      sed -i 's/# CONFIG_FS_DIRENT_LITE is not set/CONFIG_FS_DIRENT_LITE=y/' 
.config
      ninja -C /var/tmp/nuttx-build nuttx
      [1250/1250] Linking C executable nuttx
      Result: SUCCESS (0 errors, 0 warnings)
      ```
   


-- 
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