This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 72ba9b9d95f fs/vfs: sync mounted filesystems in sync()
72ba9b9d95f is described below
commit 72ba9b9d95fc6fe486e6ec51363621883b6cb5b3
Author: jingfei <[email protected]>
AuthorDate: Wed Nov 18 17:36:36 2026 +0800
fs/vfs: sync mounted filesystems in sync()
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]>
---
fs/inode/fs_files.c | 55 ----------------------
fs/vfs/CMakeLists.txt | 1 +
fs/vfs/Make.defs | 2 +-
fs/vfs/fs_sync.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 128 insertions(+), 56 deletions(-)
diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c
index dd6dc883dce..a02e0ddcbe6 100644
--- a/fs/inode/fs_files.c
+++ b/fs/inode/fs_files.c
@@ -291,47 +291,6 @@ static void fdlist_install(FAR struct fdlist *list, int fd,
file_put(filep1);
}
-/****************************************************************************
- * Name: task_fssync
- ****************************************************************************/
-
-static void task_fssync(FAR struct tcb_s *tcb, FAR void *arg)
-{
- FAR struct tcb_s *ctcb;
- int pid = tcb->pid;
- uint8_t rows;
- int i;
- int j;
-
- if (tcb->group == NULL)
- {
- return;
- }
-
- rows = tcb->group->tg_fdlist.fl_rows;
-
- for (i = 0; i < rows; i++)
- {
- for (j = 0; j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; j++)
- {
- FAR struct file *filep;
-
- ctcb = nxsched_get_tcb(pid);
- if (ctcb == NULL || ctcb->group == NULL || ctcb != tcb)
- {
- return;
- }
-
- filep = fdlist_get_by_index(&ctcb->group->tg_fdlist, i, j, NULL);
- if (filep != NULL)
- {
- file_fsync(filep);
- file_put(filep);
- }
- }
- }
-}
-
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -1061,17 +1020,3 @@ int fdlist_close(FAR struct fdlist *list, int fd)
return file_put(filep);
}
-
-/****************************************************************************
- * Name: sync
- *
- * Description:
- * sync() causes all pending modifications to filesystem metadata and
- * cached file data to be written to the underlying filesystems.
- *
- ****************************************************************************/
-
-void sync(void)
-{
- nxsched_foreach(task_fssync, NULL);
-}
diff --git a/fs/vfs/CMakeLists.txt b/fs/vfs/CMakeLists.txt
index cff84c164c9..d6ceb313355 100644
--- a/fs/vfs/CMakeLists.txt
+++ b/fs/vfs/CMakeLists.txt
@@ -49,6 +49,7 @@ set(SRCS
fs_write.c
fs_fsync.c
fs_syncfs.c
+ fs_sync.c
fs_truncate.c
fs_link.c)
diff --git a/fs/vfs/Make.defs b/fs/vfs/Make.defs
index 89012c6a7bb..b667e04f49c 100644
--- a/fs/vfs/Make.defs
+++ b/fs/vfs/Make.defs
@@ -27,7 +27,7 @@ CSRCS += fs_epoll.c fs_fchstat.c fs_fstat.c fs_fstatfs.c
fs_ioctl.c fs_lseek.c
CSRCS += fs_mkdir.c fs_open.c fs_poll.c fs_pread.c fs_pwrite.c fs_read.c
CSRCS += fs_rename.c fs_rmdir.c fs_select.c fs_sendfile.c fs_stat.c
CSRCS += fs_statfs.c fs_unlink.c fs_write.c fs_fsync.c
-CSRCS += fs_syncfs.c fs_truncate.c fs_link.c
+CSRCS += fs_syncfs.c fs_sync.c fs_truncate.c fs_link.c
ifeq ($(CONFIG_FS_UIO),y)
CSRCS += fs_uio.c
diff --git a/fs/vfs/fs_sync.c b/fs/vfs/fs_sync.c
new file mode 100644
index 00000000000..06e0748d131
--- /dev/null
+++ b/fs/vfs/fs_sync.c
@@ -0,0 +1,126 @@
+/****************************************************************************
+ * fs/vfs/fs_sync.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <limits.h>
+#include <unistd.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/sched.h>
+
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: task_fssync
+ *
+ * Description:
+ * nxsched_foreach() callback that fsyncs every open file of one task.
+ *
+ ****************************************************************************/
+
+static void task_fssync(FAR struct tcb_s *tcb, FAR void *arg)
+{
+ FAR struct tcb_s *ctcb;
+ int pid = tcb->pid;
+ int count;
+ int fd;
+
+ if (tcb->group == NULL)
+ {
+ return;
+ }
+
+ count = fdlist_count(&tcb->group->tg_fdlist);
+
+ for (fd = 0; fd < count; fd++)
+ {
+ FAR struct file *filep;
+
+ /* file_fsync() may block and let the task exit meanwhile */
+
+ ctcb = nxsched_get_tcb(pid);
+ if (ctcb == NULL || ctcb->group == NULL || ctcb != tcb)
+ {
+ return;
+ }
+
+ if (fdlist_get(&ctcb->group->tg_fdlist, fd, &filep) >= 0)
+ {
+ file_fsync(filep);
+ file_put(filep);
+ }
+ }
+}
+
+#ifndef CONFIG_DISABLE_MOUNTPOINT
+/****************************************************************************
+ * Name: mountpt_syncfs
+ *
+ * Description:
+ * foreach_inode() callback that flushes one mounted filesystem. syncfs()
+ * also reaches state that no open descriptor of any task refers to.
+ *
+ ****************************************************************************/
+
+static int mountpt_syncfs(FAR struct inode *inode,
+ FAR char dirpath[PATH_MAX], FAR void *arg)
+{
+ if (INODE_IS_MOUNTPT(inode) && inode->u.i_mops != NULL &&
+ inode->u.i_mops->syncfs != NULL)
+ {
+ inode->u.i_mops->syncfs(inode);
+ }
+
+ return 0;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sync
+ *
+ * Description:
+ * sync() causes all pending modifications to filesystem metadata and
+ * cached file data to be written to the underlying filesystems.
+ *
+ ****************************************************************************/
+
+void sync(void)
+{
+ nxsched_foreach(task_fssync, NULL);
+
+#ifndef CONFIG_DISABLE_MOUNTPOINT
+ foreach_inode(mountpt_syncfs, NULL);
+#endif
+}