Skip to content

fs/vfs: sync mounted filesystems in sync() - #21999

Merged
GUIDINGLI merged 1 commit into
apache:masterfrom
jingfei195887:fs/sync-mountpoints
Nov 19, 2026
Merged

GUIDINGLI merged 1 commit into
apache:masterfrom
jingfei195887:fs/sync-mountpoints

Conversation

@jingfei195887

@jingfei195887 jingfei195887 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • fs/vfs: sync mounted filesystems in sync().
    sync() only called file_fsync() on the open descriptors of each task,
    so state that no task descriptor refers to was never flushed. For
    example, the backing file of a loop device on hostfs is opened by the
    kernel with file_open(), so sync() never flushed it to the host.

    Move sync() and its task_fssync() helper from fs/inode/fs_files.c
    into a new fs/vfs/fs_sync.c, next to fs_syncfs.c. After the per-task
    fsync, sync() now walks the inode tree with foreach_inode() and calls
    the syncfs method of every mountpoint that has one (today tmpfs and
    hostfs).

    task_fssync() now uses the public fdlist_count() and fdlist_get()
    instead of the private fdlist_get_by_index(). With CONFIG_FDCHECK,
    fdcheck_restore() returns a bare descriptor (below OPEN_MAX, at most
    255) unchanged, so the walk visits the same slots as before.

    The syncfs methods run with the inode tree read lock held by
    foreach_inode(), as statfs already does for the mount listing in
    procfs; mount and umount wait until the walk ends.

Impact

  • New feature / API change: NO new API. sync() now also flushes every
    mounted filesystem that implements syncfs.
  • Build: new file fs/vfs/fs_sync.c, added to both Make and CMake.
  • 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 (hostfs, loop device and FAT are already enabled there) with a
small test app whose sync command calls sync(). A FAT image on the
host is attached as a loop device through hostfs, so its host descriptor
is held by the loop driver, not by any task:

mount -t hostfs -o fs=/tmp/fsup_sync /h
losetup /dev/loop0 /h/fat.img
mount -t vfat /dev/loop0 /mnt
echo sync-test > /mnt/a.txt
fsuptest sync
poweroff

sync() runs twice in this script: the sync command and the reboot
notifier at poweroff. The simulator was run under
strace -f -e trace=openat,fsync,fdatasync to count host fsync() calls
on the image descriptor.

Testing logs before change (master 8a282a5):

openat(AT_FDCWD, "/tmp/fsup_sync/fat.img", O_RDWR|O_CLOEXEC) = 3
(no fsync(3))

Testing logs after change:

openat(AT_FDCWD, "/tmp/fsup_sync/fat.img", O_RDWR|O_CLOEXEC) = 3
fsync(3)                        = 0
fsync(3)                        = 0

The branch was then rebased onto master fc8d354 (no conflicts, patch
unchanged) and the build checks below were rerun there.

  • ./tools/checkpatch.sh -g master..HEAD: all checks pass.
  • sim:nsh with CONFIG_FS_HOSTFS 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 →

arduino-mega2560

  • flash: .text +8 B (+0.0%, 68,651 B / 262,144 B, total: 26% used)

esp32-devkitc

  • ROM: .flash.text +48 B (+0.0%, 132,904 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +48 B (+0.0%, 98,584 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +48 B (+0.1%, 86,928 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +60 B (+0.1%, 68,412 B / 131,072 B, total: 52% used)

qemu-armv8a

  • Code: .text.sync +16 B, .text.task_fssync -16 B (+0.0%, 372,072 B)

qemu-intel64

  • Code: .text +68 B (+0.0%, 8,701,136 B)

rx65n-rsk2mb

  • ROM: .text +224 B (+0.2%, 92,092 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +144 B (+0.0%, 406,368 B)

stm32-nucleo-f103rb

  • flash: .text +216 B (+0.6%, 35,476 B / 131,072 B, total: 27% used)

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

sync() only fsync'd open descriptors, so filesystem-level state that no
open fd carries (e.g. a file the kernel opened for a loop device) was
never persisted.  Move sync() into fs_sync.c and have it also syncfs()
every mountpoint.

Signed-off-by: jingfei <[email protected]>
@GUIDINGLI
GUIDINGLI merged commit 72ba9b9 into apache:master Nov 19, 2026
54 checks passed
@raiden00pl

Copy link
Copy Markdown
Member

@xiaoxiang781216 @jingfei195887 @GUIDINGLI

This change pulls the whole printf engine into images that don't use any filesystem.

sync() is always linked, because fs_reboot_initialize() registers it as a reboot notifier. Now sync() also calls foreach_inode(), and foreach_inodelevel() builds paths with snprintf(). Images without a console or any printf() call now link snprintf() and the vsprintf code.

Measured on nucleo-f302r8 (CONFIG_DEFAULT_SMALL=y, LTO), GCC 16.2, master 1eedefa with and without this PR (#21999 + #22046 reverted):

image without this PR with this PR
minimal, no console, no files 7272 B 9148 B (+1876 B, +26%)
hello world with console 21744 B 21988 B (+244 B)

In the minimal image: snprintf +1236 B, foreach_inode/foreach_inodelevel +328 B, plus sync() code. With CONFIG_DISABLE_MOUNTPOINT=y the image goes back to 7260 B.

So a minimal image grows by a quarter, to walk mountpoints that don't exist. Could the path in foreach_inodelevel() be built without snprintf() ("/%s" needs only strlcpy()), or could sync() skip mountpoints when nothing is mounted?

@jingfei195887

Copy link
Copy Markdown
Contributor Author

@raiden00pl Thanks for measuring this, I reproduced it. The fix is in #22166.

nucleo-f302r8:nsh reduced to a minimal image (CONFIG_DEFAULT_SMALL=y, CONFIG_LTO_FULL=y, CONFIG_INIT_NONE=y, no USART/console, no /dev nodes, no procfs/romfs/tmpfs), master b2b0bde. Flash = text + data:

image gcc 10.3 gcc 13.2 printf in image
master with #21999 + #22046 reverted 12152 12048 no
master 15672 (+3520) 15576 (+3528) yes
master + #22166 13220 (+1068) 13116 (+1068) no
master + #22166 + CONFIG_DISABLE_MOUNTPOINT=y 12124 12024 no

The growth is larger than in your numbers because with these compilers the printf engine also pulls in __udivmoddi4 (~700 B).

#22166 builds the path in foreach_inodelevel() with strlcpy() instead of snprintf(), which removes the printf engine (about 2.4 KB here). The remaining ~1 KB is the mountpoint walk itself: sync() (inlined into the reboot handler), foreach_inodelevel() and mountpt_syncfs(). Images that need to drop that too can set CONFIG_DISABLE_MOUNTPOINT=y, which brings them back to the size before #21999.

Note that in images that register any driver or open a file, printf is already linked by inode_search_setup() and dir_allocate(), so #22166 only saves size where foreach_inode() was the last printf user, as in your minimal image.

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.

4 participants