nuttx/Documentation/components/filesystem
Marco Casaroli 6da4269c46 fs/vfs: Add ioctldir for volume ioctls via the mountpoint directory.
FIOC_REFORMAT, FIOC_OPTIMIZE, FIOC_INTEGRITY and FIOC_DUMP act on a volume,
not on any one file, but the only route into a file system has been the
per-file ioctl method.  A caller therefore has to open an unrelated file
just to name the volume it means.

For nxffs that is not merely awkward, it is a dead end.  nxffs_ioctl()
refuses FIOC_REFORMAT while any file on the volume is open:

    if (volume->ofiles)
      {
        ferr("ERROR: Open files\n");
        ret = -EBUSY;

and every open file is on that list (nxffs_open.c).  The descriptor used to
issue the command is itself such a file, so the check can never pass and
FIOC_REFORMAT is unreachable through the only interface that exposes it.

Add an optional ioctldir method to struct mountpt_operations, reached by
issuing the ioctl on a descriptor for the mountpoint directory:

    fd = open("/mnt/nxffs", O_RDONLY | O_DIRECTORY);
    ioctl(fd, FIOC_REFORMAT, 0);

It takes the same (mountpt, dir) pair as opendir/readdir/rewinddir, so it
reads as a member of the directory-operations family; the file system
recovers the volume from the mountpoint inode and may ignore dir.  The
member is placed at the end of the structure rather than beside the other
directory operations on purpose: every file system initialises
mountpt_operations positionally, so a member inserted mid-structure would
force all of them to add a slot for a method they do not implement.
Appending keeps the change to one file system.

dir_ioctl() gives that method the first chance at every command when the
directory belongs to a mounted volume and the file system provides one, and
falls back to its own handling of FIOC_FILEPATH and BIOC_FLUSH when the file
system answers -ENOTTY.  Trying the file system first is what lets a file
system override a command the VFS would otherwise answer generically; the
-ENOTTY fallback is what keeps the generic answers available to everyone
else.  A file system that leaves the method NULL is unaffected: the VFS
answers exactly as before.

The existing per-file method could not simply be reused for this.  It takes
a struct file, and every implementation that has an ioctl -- fat, romfs,
tmpfs, spiffs among them -- asserts on filep->f_priv and dereferences it,
so handing it a directory descriptor with no open file behind it would
fault.  Making the entry point separate keeps that contract intact and
makes support explicit rather than assumed.

nxffs implements it, which is what makes its FIOC_REFORMAT reachable.  The
per-file path is left in place and both share one implementation, so
nothing that works today stops working.  spiffs, which has the same shape
of volume commands, can follow.

Measured on sim:nxffs, with one file written to the volume and then the
same sequence of ioctls issued on a file descriptor, on a descriptor for the
mountpoint directory, and on a descriptor for a pseudo file system directory.
Before:

    FIOC_REFORMAT via file fd:  ret=-1 errno=16 (EBUSY, as expected)
    FIOC_REFORMAT via dir fd:   ret=-1 errno=25
    FIOC_FILEPATH via dir fd:   ret=0  "/mnt/nxffs//"
    BIOC_FLUSH    via dir fd:   ret=0
    bogus cmd     via dir fd:   ret=-1 errno=25
    FIOC_FILEPATH via /dev fd:  ret=0  "/dev//"
    bogus cmd     via /dev fd:  ret=-1 errno=25
    name still in the raw MTD image afterwards: yes

After:

    FIOC_REFORMAT via file fd:  ret=-1 errno=16 (EBUSY, as expected)
    FIOC_REFORMAT via dir fd:   ret=0
    FIOC_FILEPATH via dir fd:   ret=0  "/mnt/nxffs//"
    BIOC_FLUSH    via dir fd:   ret=0
    bogus cmd     via dir fd:   ret=-1 errno=25
    FIOC_FILEPATH via /dev fd:  ret=0  "/dev//"
    bogus cmd     via /dev fd:  ret=-1 errno=25
    name still in the raw MTD image afterwards: no

Only the FIOC_REFORMAT line on the directory descriptor changes, and the raw
MTD image confirms the volume really was erased.  FIOC_FILEPATH and
BIOC_FLUSH on a directory still answer even though nxffs is now consulted
ahead of them, an unrecognised command is still refused rather than
forwarded blindly, and a directory in the pseudo file system, which has no
ioctldir at all, is untouched.

Assisted-by: Claude Code:claude-opus-4-8
Assisted-by: Claude Code:claude-fable-5
Assisted-by: Claude Code:claude-opus-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-07-25 07:28:22 -03:00
..
nuttxfs Doc: migration filesystem doc 2024-11-11 10:37:56 +08:00
nxffs Doc: migration filesystem doc 2024-11-11 10:37:56 +08:00
nxflat Doc: migration filesystem doc 2024-11-11 10:37:56 +08:00
aio.rst Documentation: add dummy pages for missing filesystems and drivers 2023-10-27 13:21:40 -03:00
binfs.rst
cromfs.rst style: Fix "the the" typo across the codebase. 2026-03-23 11:07:49 +01:00
fat.rst style: fix typos 2025-04-30 13:45:46 +08:00
hostfs.rst Documentation: add dummy pages for missing filesystems and drivers 2023-10-27 13:21:40 -03:00
how_nxffs_works.rst Doc: migration filesystem doc 2024-11-11 10:37:56 +08:00
index.rst fs/vfs: Add ioctldir for volume ioctls via the mountpoint directory. 2026-07-25 07:28:22 -03:00
inotify.rst documentation/inotify: add newly supported events 2024-10-08 08:14:19 +08:00
littlefs.rst fs/littlefs: Enforce open permissions and set create ownership 2026-06-21 10:39:42 +08:00
mmap.rst
mnemofs.rst Documentation: fix spelling 2025-05-15 11:33:41 +08:00
nfs.rst Documentation: add dummy pages for missing filesystems and drivers 2023-10-27 13:21:40 -03:00
nuttxfs.rst Doc: migration filesystem doc 2024-11-11 10:37:56 +08:00
nxffs.rst fs/vfs: Add ioctldir for volume ioctls via the mountpoint directory. 2026-07-25 07:28:22 -03:00
nxflat.rst Doc: migration filesystem doc 2024-11-11 10:37:56 +08:00
partition.rst Documentation: Fix typos 2023-10-29 10:35:51 +08:00
procfs.rst
profiler.rst fs: Add Kernel-level VFS Performance Profiler 2026-04-26 11:50:09 -03:00
pseudofs.rst doc: Migrating the rest of documentation from cwiki. 2026-05-28 09:34:04 +08:00
romfs.rst Documentation: add dummy pages for missing filesystems and drivers 2023-10-27 13:21:40 -03:00
rpmsgfs.rst docs/rpmsgfs: initial docs on RPMsg file system usage. 2024-02-20 18:25:18 -08:00
shmfs.rst docs/comments: add simple document and fix typo 2024-03-03 01:55:07 +08:00
smartfs.rst doc: Migrating the rest of documentation from cwiki. 2026-05-28 09:34:04 +08:00
special_files_dev_num.rst Doc: migration filesystem doc 2024-11-11 10:37:56 +08:00
spiffs.rst
tmpfs.rst Documentation: fix spelling 2025-05-15 11:33:41 +08:00
unionfs.rst Documentation: remove readme references 2023-10-27 13:21:40 -03:00
userfs.rst Documentation: add dummy pages for missing filesystems and drivers 2023-10-27 13:21:40 -03:00
v9fs.rst v9fs:add Documentation 2025-01-20 17:12:14 +08:00
zipfs.rst