Skip to content

fs: add umount_all() to force-unmount all mountpoints - #22013

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
jingfei195887:fs/umount-all
Nov 19, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
jingfei195887:fs/umount-all

Conversation

@jingfei195887

Copy link
Copy Markdown
Contributor

Summary

  • fs: add umount_all() to force-unmount all mountpoints.
    Add an fs-internal umount_all() in fs/mount/fs_umount2.c for the
    reboot/poweroff path. It force-unmounts every mountpoint so that each
    filesystem's unbind() runs, which a plain sync() does not reach.

    umount_all() drives foreach_mountpoint() with the inode write lock
    held for the whole traversal. foreach_inode() and
    foreach_mountpoint() get a readonly argument that selects the read
    lock (as before) or the write lock. nx_umount2() takes the write lock
    again inside the callback, which is safe because the rwsem is recursive
    for the current writer. The traversal now visits children before their
    parent and saves the sibling link before each callback, so a callback may
    unmount and free the inode it is handed. A mountpoint that cannot be
    unmounted is left in the tree and stepped over. nx_umount2() also calls
    the filesystem's syncfs before unbind().

  • fs/mount: drop statfs from mountpoint traversal.
    mountpoint_filter() called statfs on every mountpoint while the
    traversal lock was held. With the write lock taken by umount_all(), a
    filesystem whose statfs blocks on a remote peer can deadlock with that
    peer. foreach_mountpoint_t now passes only the path; the procfs
    fs/mount, fs/blocks and fs/usage handlers call statfs()
    themselves. Mountpoints without a statfs method are no longer skipped
    by the traversal.

umount_all() has no caller in the tree yet; it is intended for the
filesystem reboot notifier.

Note: this changes the signature of the fs-internal foreach_inode().
#21999 adds a new foreach_inode(mountpt_syncfs, NULL) call in
fs/vfs/fs_sync.c. Whichever of the two PRs lands second has to be
rebased and pass true (read lock) there.

Impact

  • New feature / API change: new fs-internal umount_all(); fs-internal
    foreach_inode(), foreach_mountpoint() and foreach_mountpoint_t
    signatures change. No public API change.
  • Behavior: umount2() now calls syncfs before unbind(). The mount
    listings in procfs are unchanged.
  • Build, hardware, documentation, security, compatibility: NO.

Testing

Host: Ubuntu 22.04 x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0.

sim:nsh with tmpfs, hostfs and procfs, and a small test app whose
umountall command calls umount_all() (flat build):

mount -t tmpfs /t1
mount -t hostfs -o fs=/tmp/fsup_umount /h
echo hello > /t1/a.txt
cat /proc/fs/mount
fsuptest umountall
ls /h
ls /t1
ls /proc

Testing logs before change (master fc8d354, umount_all() absent):

nsh> fsuptest umountall
umountall: not available
nsh> ls /t1
/t1:
 a.txt

Testing logs after change:

nsh> cat /proc/fs/mount
  /bin type binfs
  /data type hostfs
  /etc type romfs
  /h type hostfs
  /proc type procfs
  /t1 type tmpfs
  /tmp type tmpfs
nsh> fsuptest umountall
umountall: done
nsh> ls /h
nsh: ls: stat failed: 2
nsh> ls /t1
nsh: ls: stat failed: 2
nsh> ls /proc
nsh: ls: stat failed: 2

The cat /proc/fs/mount output is identical before and after the change.

  • ./tools/checkpatch.sh -g master..HEAD: all checks pass.
  • sim:nsh with CONFIG_FS_PROCFS and CONFIG_FS_TMPFS: Make and CMake
    builds pass with no new warnings in the changed files; ostest exits
    with status 0.
  • Verified on sim only.

PR verification Self-Check

  • This PR introduces only one functional change.
  • I have updated all required description fields above.
  • My PR adheres to Contributing Guidelines and Documentation (git commit title and message, coding standard, etc).
  • My PR is still work in progress (not ready for review).
  • My PR is ready for review and can be safely merged into a codebase.

@github-actions github-actions Bot added Area: File System File System issues Size: M The size of the change in this PR is medium labels Nov 18, 2026
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text +292 B (+0.2%, 133,148 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +292 B (+0.3%, 98,828 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +208 B (+0.2%, 87,088 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .rodata +16 B, .text.blocks_entry +8 B, .text.foreach_inode +32 B, .text.foreach_inodelevel +8 B, .text.foreach_mountpoint +16 B, .text.mount_entry +8 B, .text.mount_read +4 B, .text.mountpoint_filter -72 B, .text.nx_umount2 +20 B, .text.usage_entry +8 B (+0.1%, 372,356 B)

qemu-intel64

  • Code: .text +447 B (+0.0%, 8,701,515 B)

rx65n-rsk2mb

  • ROM: .text +368 B (+0.4%, 92,236 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +784 B (+0.2%, 407,008 B)

No memory changes detected for:


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

Add umount_all() in fs/mount/fs_umount2.c for the reboot/poweroff path,
where every mountpoint must be force-unmounted so that each filesystem's
unbind() runs (e.g. metadata a filesystem only writes at unmount), which
a bare sync() does not reach.

umount_all() drives the existing foreach_mountpoint() enumerator and
takes the inode write lock once for the whole drain. nx_umount2() takes
the write lock again inside the callback, but the NuttX rwsem is
recursive for the current writer (holder == tid), so the nested
acquisition does not deadlock. The traversal now visits children before
their parent and saves the sibling link before each callback, so the
callback may unmount and free the inode it is handed. A mountpoint that
cannot be unmounted (e.g. a read-only or proxy filesystem whose unbind
returns an error) is left in the tree and stepped over, so a single
traversal always terminates. nx_umount2() also calls syncfs() before
unbind().

No dynamic allocation is used on this path.

Signed-off-by: jingfei <[email protected]>
umount_all() drives foreach_mountpoint() with the inode write lock held
for the whole traversal. mountpoint_filter() issued a statfs() on every
mountpoint inside that lock. For a proxy filesystem whose statfs is a
blocking cross-core RPC, the reboot thread then waits (no timeout) for a
reply while holding the write lock, and the peer core cannot make
progress because the handler that serves its requests is itself blocked
on the same lock as a reader -- a ring wait that hangs the system.

statfs is not needed by the traversal itself. Make foreach_mountpoint_t
hand only the mountpoint path; the only consumer that needs statfs
(procfs fs/mount, fs/blocks, fs/usage) now calls statfs() itself, under
its own read-lock traversal where recursive read is safe. This also
unmounts mountpoints whose fs has no statfs method, which the old
statfs-gated filter silently skipped.

Signed-off-by: jingfei <[email protected]>
@xiaoxiang781216
xiaoxiang781216 merged commit 8d7916e into apache:master Nov 19, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues 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