jingfei195887 opened a new pull request, #21999:
URL: https://github.com/apache/nuttx/pull/21999

   ## 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 8a282a51d4):
   
   ```
   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 b27c8384a2 (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
   
   * [x] This PR introduces only one functional change.
   * [x] I have updated all required description fields above.
   * [x] 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).
   * [x] My PR is ready for review and can be safely merged into a codebase.
   


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