This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 6be5713b49441949b1b50c89f17a599523271fa5 Author: jingfei <[email protected]> AuthorDate: Tue Aug 11 09:20:08 2026 +0800 fs: add umount_all() to force-unmount all mountpoints 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]> --- fs/inode/fs_foreachinode.c | 82 ++++++++++++++++++++++++++++-------- fs/inode/inode.h | 2 +- fs/mount/fs_foreachmountpoint.c | 5 ++- fs/mount/fs_procfs_mount.c | 2 +- fs/mount/fs_umount2.c | 92 ++++++++++++++++++++++++++++++++++++++++- fs/mount/mount.h | 26 +++++++++++- 6 files changed, 186 insertions(+), 23 deletions(-) diff --git a/fs/inode/fs_foreachinode.c b/fs/inode/fs_foreachinode.c index 7e61177c081..6d7e5dcefcc 100644 --- a/fs/inode/fs_foreachinode.c +++ b/fs/inode/fs_foreachinode.c @@ -81,27 +81,21 @@ struct inode_path_s static int foreach_inodelevel(FAR struct inode *inode, FAR struct inode_path_s *info) { + FAR struct inode *next; int ret = OK; /* Visit each node at this level */ - for (; inode; inode = inode->i_peer) + for (; inode; inode = next) { - /* Give the next inode to the callback */ - - ret = info->handler(inode, info->path, info->arg); - - /* Break out of the loop early if the handler returns a non-zero - * value. + /* Snapshot the sibling link before the callback: the handler may + * unmount and free 'inode', after which inode->i_peer cannot be read. */ - if (ret != 0) - { - break; - } + next = inode->i_peer; /* If there is a level 'beneath' this one, then recurse to visit all - * of the inodes at that level. + * of the inodes at that level (children before their parent). */ if (inode->i_child) @@ -138,6 +132,19 @@ static int foreach_inodelevel(FAR struct inode *inode, break; } } + + /* Give the inode to the callback */ + + ret = info->handler(inode, info->path, info->arg); + + /* Break out of the loop early if the handler returns a non-zero + * value. + */ + + if (ret != 0) + { + break; + } } /* Return the result of the traversal. */ @@ -157,6 +164,15 @@ static int foreach_inodelevel(FAR struct inode *inode, * when the callback 'handler' returns a non-zero value, or when all of * the inodes have been visited. * + * 'readonly' selects the lock held across the whole traversal: true takes + * the inode READ lock (read-only visitors, so they do not serialize + * behind an exclusive lock); false takes the WRITE lock so the handler may + * mutate the tree in place -- specifically call nx_umount2(), whose own + * inode_lock() nests safely because the NuttX rwsem is recursive for the + * current writer. The traversal core snapshots the sibling/child links + * before each callback, so with readonly=false a handler may unmount (and + * free) the inode it is handed. + * * NOTE 1: Use with caution... The pseudo-file system is locked throughout * the traversal. * NOTE 2: The search algorithm is recursive and could, in principle, use @@ -165,7 +181,7 @@ static int foreach_inodelevel(FAR struct inode *inode, * ****************************************************************************/ -int foreach_inode(foreach_inode_t handler, FAR void *arg) +int foreach_inode(foreach_inode_t handler, FAR void *arg, bool readonly) { #ifdef ENUM_INODE_ALLOC FAR struct inode_path_s *info; @@ -187,9 +203,25 @@ int foreach_inode(foreach_inode_t handler, FAR void *arg) /* Start the recursion at the root inode */ - inode_rlock(); + if (readonly) + { + inode_rlock(); + } + else + { + inode_lock(); + } + ret = foreach_inodelevel(g_root_inode->i_child, info); - inode_runlock(); + + if (readonly) + { + inode_runlock(); + } + else + { + inode_unlock(); + } /* Free the info structure and return the result */ @@ -208,9 +240,25 @@ int foreach_inode(foreach_inode_t handler, FAR void *arg) /* Start the recursion at the root inode */ - inode_rlock(); + if (readonly) + { + inode_rlock(); + } + else + { + inode_lock(); + } + ret = foreach_inodelevel(g_root_inode->i_child, &info); - inode_runlock(); + + if (readonly) + { + inode_runlock(); + } + else + { + inode_unlock(); + } return ret; diff --git a/fs/inode/inode.h b/fs/inode/inode.h index fb41cac38da..2ecdde4ea9d 100644 --- a/fs/inode/inode.h +++ b/fs/inode/inode.h @@ -529,7 +529,7 @@ int fs_checkopenperm(uid_t owner, gid_t group, mode_t mode, int oflags); * ****************************************************************************/ -int foreach_inode(foreach_inode_t handler, FAR void *arg); +int foreach_inode(foreach_inode_t handler, FAR void *arg, bool readonly); /**************************************************************************** * Name: inode_hash diff --git a/fs/mount/fs_foreachmountpoint.c b/fs/mount/fs_foreachmountpoint.c index a2f304c7f58..9607acf0944 100644 --- a/fs/mount/fs_foreachmountpoint.c +++ b/fs/mount/fs_foreachmountpoint.c @@ -142,7 +142,8 @@ static int mountpoint_filter(FAR struct inode *node, * ****************************************************************************/ -int foreach_mountpoint(foreach_mountpoint_t handler, FAR void *arg) +int foreach_mountpoint(foreach_mountpoint_t handler, FAR void *arg, + bool readonly) { struct enum_mountpoint_s info; @@ -151,7 +152,7 @@ int foreach_mountpoint(foreach_mountpoint_t handler, FAR void *arg) info.handler = handler; info.arg = arg; - return foreach_inode(mountpoint_filter, (FAR void *)&info); + return foreach_inode(mountpoint_filter, (FAR void *)&info, readonly); } #endif diff --git a/fs/mount/fs_procfs_mount.c b/fs/mount/fs_procfs_mount.c index 8ae573cfaa1..0bdbc0c1590 100644 --- a/fs/mount/fs_procfs_mount.c +++ b/fs/mount/fs_procfs_mount.c @@ -515,7 +515,7 @@ static ssize_t mount_read(FAR struct file *filep, FAR char *buffer, /* Generate each entry in the routing table */ - foreach_mountpoint(handler, &info); + foreach_mountpoint(handler, &info, true); ret = info.totalsize; /* Update the file offset */ diff --git a/fs/mount/fs_umount2.c b/fs/mount/fs_umount2.c index c143763e65b..d313f4ab6dc 100644 --- a/fs/mount/fs_umount2.c +++ b/fs/mount/fs_umount2.c @@ -27,16 +27,68 @@ #include <nuttx/config.h> #include <sys/mount.h> +#include <sys/statfs.h> #include <stdbool.h> #include <errno.h> #include <assert.h> +#include <nuttx/debug.h> #include <nuttx/fs/fs.h> #include <nuttx/sched.h> #include "inode/inode.h" +#include "mount/mount.h" #include "vfs/vfs.h" +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifndef CONFIG_DISABLE_MOUNTPOINT + +/**************************************************************************** + * Name: umount_all_handler + * + * Description: + * foreach_mountpoint() callback for umount_all() (readonly=false). + * Flushes and force-unmounts the mountpoint it is handed, then returns 0 + * so the single traversal continues to the next one. + * + * This is safe even though nx_umount2() may remove (and free) this very + * inode: the traversal core snapshots each inode's sibling/child links + * before invoking the callback, so it never dereferences the freed node. + * A mountpoint that cannot be unmounted (e.g. a read-only or proxy fs + * whose unbind returns an error) is simply left in the tree; returning 0 + * moves on without revisiting it, so the traversal always terminates. + * + ****************************************************************************/ + +static int umount_all_handler(FAR const char *mountpoint, + FAR struct statfs *statbuf, FAR void *arg) +{ + int ret; + + UNUSED(arg); + UNUSED(statbuf); + + /* Force-unmount the mountpoint. nx_umount2() re-resolves the path and + * flushes (syncfs) before unbind under the inode write lock that + * foreach_mountpoint() holds for the whole traversal (readonly=false); + * the NuttX rwsem is recursive for the current holder, so the nested + * acquisition is safe. + */ + + ret = nx_umount2(mountpoint, MNT_FORCE); + if (ret < 0) + { + ferr("Failed to unmount %s during reboot: %d\n", mountpoint, ret); + } + + return 0; +} + +#endif /* CONFIG_DISABLE_MOUNTPOINT */ + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -134,6 +186,11 @@ int nx_umount2(FAR const char *target, unsigned int flags) goto errout_with_lock; } + if (mountpt_inode->u.i_mops->syncfs) + { + mountpt_inode->u.i_mops->syncfs(mountpt_inode); + } + ret = mountpt_inode->u.i_mops->unbind(mountpt_inode->i_private, &blkdrvr_inode, flags); if (ret < 0) @@ -148,7 +205,7 @@ int nx_umount2(FAR const char *target, unsigned int flags) goto errout_with_lock; } - /* Successfully unbound. Convert the mountpoint inode to regular + /* Successfully unbound. Convert the mountpoint inode to a regular * pseudo-file inode. */ @@ -257,3 +314,36 @@ int umount2(FAR const char *target, unsigned int flags) return ret; } + +#ifndef CONFIG_DISABLE_MOUNTPOINT + +/**************************************************************************** + * Name: umount_all + * + * Description: + * Flush (syncfs) and force-unmount every mountpoint. Intended for the + * reboot/poweroff path so that every filesystem's unbind() logic runs + * (e.g. metadata a filesystem only writes at unmount), which a bare + * sync() does not reach. + * + * foreach_mountpoint() with readonly=false holds the inode write lock for + * whole traversal; nx_umount2() then re-takes the write lock inside the + * callback, which is safe because the NuttX rwsem is recursive for the + * current writer (holder == tid). Holding one lock across the (now + * bounded) filesystem operations also serializes the whole teardown, which + * is exactly what the reboot path wants. + * + * A single traversal unmounts every mountpoint: the handler unmounts the + * inode it is handed and returns 0 to continue. The traversal core + * snapshots each inode's sibling/child links before the callback, so + * freeing the current inode from nx_umount2() is safe, and a mountpoint + * that cannot be unmounted is simply left in the tree and stepped over. + * + ****************************************************************************/ + +void umount_all(void) +{ + foreach_mountpoint(umount_all_handler, NULL, false); +} + +#endif /* CONFIG_DISABLE_MOUNTPOINT */ diff --git a/fs/mount/mount.h b/fs/mount/mount.h index d6a2247f1fd..b74023fc545 100644 --- a/fs/mount/mount.h +++ b/fs/mount/mount.h @@ -29,6 +29,8 @@ #include <nuttx/config.h> +#include <stdbool.h> + #ifndef CONFIG_DISABLE_MOUNTPOINT /**************************************************************************** @@ -36,6 +38,7 @@ ****************************************************************************/ struct statfs; /* Forward reference */ +struct inode; /* Forward reference */ /* Callback used by foreach_mountpoints to traverse all mountpoints in the * pseudo-file system. @@ -67,9 +70,30 @@ typedef int (*foreach_mountpoint_t)(FAR const char *mountpoint, * an indeterminant amount of stack space. This will not usually be a * real work issue. * + * 'readonly' selects the lock held across the traversal: true takes the + * inode read lock (read-only visitors such as the mount command); false + * takes the write lock so the callback may unmount the mountpoint it is + * handed (used by the reboot path, umount_all()). + * + ****************************************************************************/ + +int foreach_mountpoint(foreach_mountpoint_t handler, FAR void *arg, + bool readonly); + +/**************************************************************************** + * Name: umount_all + * + * Description: + * Flush (syncfs) and force-unmount every mountpoint. Intended for the + * reboot/poweroff path so that every filesystem's unbind() logic runs + * (e.g. metadata a filesystem only writes at unmount), which a bare + * sync() does not reach. Drives foreach_mountpoint() with readonly=false + * so the callback can unmount each mountpoint in place during a single + * traversal. + * ****************************************************************************/ -int foreach_mountpoint(foreach_mountpoint_t handler, FAR void *arg); +void umount_all(void); /**************************************************************************** * Name: fs_gettype
